/*******************************************************************************
* Project:      Amped'RF Bluetooth
* File name:    GKI_tim.c
* Description:  Timer abstraction layer
*
* Copyright (c) 2013 Amped'RF Inc., AR Technology  All rights reserved.
*******************************************************************************/
#include "SDK_Common.h"
#include "GKI_lib.h"



//------------------------------------------------------------------------------
// Function Name:   TM_SetTimerResolution
//
// Description:     This function will setup the interrupt resolution
//                  of a timer based on the PCLK
//------------------------------------------------------------------------------
void GKI_tim_SetResolution(tGKI_tim  tim, u32 TicksPerSecond)
{
    /*
    u32 Microseconds, CompareValue, n;
    u16 Prescaler;

    Microseconds = 1000000 / TicksPerSecond;
    n = Microseconds * (RCCU_FrequencyValue(RCCU_PCLK) / 1000000);
    FindFactors(n, &Prescaler, &CompareValue);

    TIM_Init(TIMx);                                 // Initialize Timer
    TIM_ClockSourceConfig(TIMx, TIM_INTERNAL);      // Set to Internal clock source
    TIM_PrescalerConfig(TIMx, (uint8)Prescaler-1);  // Set Prescaler
    TIMx->CNTR = 0xFFFC;                            // Set counter to reset value
    TIMx->OCAR = (uint16)CompareValue-4;            // Set compare value
    TIMx->CR2 |= TIM_OCA_IT;                        // Interrupt on output compare A
    TIM_FlagClear(TIM0, TIM_OCA_IT);                // Clear any pending compare interrupts
    TIM_CounterConfig(TIMx, TIM_START);             // Start it
*/
}




//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
void GKI_tim_SetDownTimer(u32 MicroSec)
{
    u16 PreScale;
    u32 Period;
	u32 TIMER_TICK_CLOCK;
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
    RCC_ClocksTypeDef RCC_Clocks;

	TIM_DeInit(TIM5);
    // Enable TIM5 clock
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
	memset(&TIM_TimeBaseInitStruct, 0, sizeof(TIM_TimeBaseInitStruct));

    // Determine timer settings for char timeout
    RCC_GetClocksFreq(&RCC_Clocks);

	TIMER_TICK_CLOCK = RCC_Clocks.PCLK1_Frequency;
	u32 APB1_PreScaler = (RCC->CFGR & RCC_CFGR_PPRE1);
	if (APB1_PreScaler != RCC_CFGR_PPRE1_DIV1 ) {
		TIMER_TICK_CLOCK *= 2;
	}
	PreScale = TIMER_TICK_CLOCK / 10000;
	Period = MicroSec * 10;

    // Set TIM5 timer period 1 sec
    TIM_TimeBaseInitStruct.TIM_Prescaler                    = PreScale - 1;
    TIM_TimeBaseInitStruct.TIM_CounterMode                  = TIM_CounterMode_Down;
    TIM_TimeBaseInitStruct.TIM_Period                       = Period;
    TIM_TimeBaseInitStruct.TIM_ClockDivision                = TIM_CKD_DIV1;
    TIM_TimeBaseInitStruct.TIM_RepetitionCounter            = 0;
    TIM_TimeBaseInit(TIM5, &TIM_TimeBaseInitStruct);

    // Turn on timer
    TIM_Cmd(TIM5, ENABLE);
}



//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
u32 GKI_tim_GetDownTimer(void)
{
    u32 counter = TIM_GetCounter(TIM5);
    if (counter==0) {
        TIM_Cmd(TIM5, DISABLE);
    }
    return counter;
}



//--------------------------------------------------------------
//