|
|
|
#include "system_func.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static volatile uint8_t usTickCountDiv;
|
|
|
|
static volatile uint32_t msTickCount;
|
|
|
|
|
|
|
|
static void SystemTimerInitialization(void);
|
|
|
|
static void SystemTimer_Interrput_Handler(void);
|
|
|
|
static uint32_t SystemTimer_GetPeriodCount(uint32_t prescalar, uint32_t frequency);
|
|
|
|
|
|
|
|
void SystemCoreClockSet(SYSTEM_CORE_CLOCK_LIST clock)
|
|
|
|
{
|
|
|
|
switch(clock)
|
|
|
|
{
|
|
|
|
case HIRC_96MHZ:
|
|
|
|
usTickCountDiv = 48;
|
|
|
|
LP_SetOperatingVoltage(LP_OVR_1_1);
|
|
|
|
break;
|
|
|
|
case HIRC_48MHZ:
|
|
|
|
usTickCountDiv = 24;
|
|
|
|
LP_SetOperatingVoltage(LP_OVR_1_0);
|
|
|
|
break;
|
|
|
|
case HIRC_24MHZ:
|
|
|
|
usTickCountDiv = 12;
|
|
|
|
LP_SetOperatingVoltage(LP_OVR_0_9);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usTickCountDiv = 48;
|
|
|
|
LP_SetOperatingVoltage(LP_OVR_1_1);
|
|
|
|
break;;
|
|
|
|
}
|
|
|
|
SystemTimerInitialization();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t SystemTimer_Get_TickCount(void)
|
|
|
|
{
|
|
|
|
return msTickCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
static void SystemTimerInitialization(void)
|
|
|
|
{
|
|
|
|
// Declare variables
|
|
|
|
tmr_cfg_t tmr;
|
|
|
|
int tmr_id = MXC_TMR_GET_IDX(SYSTEM_TIMER_INSTANCE);
|
|
|
|
uint32_t periodTicks = SystemTimer_GetPeriodCount(1, 1000);
|
|
|
|
|
|
|
|
/*
|
|
|
|
Steps for configuring a timer for PWM mode:
|
|
|
|
1. Disable the timer
|
|
|
|
2. Set the prescale value
|
|
|
|
3 Configure the timer for continuous mode
|
|
|
|
4. Set polarity, timer parameters
|
|
|
|
5. Enable Timer
|
|
|
|
*/
|
|
|
|
|
|
|
|
TMR_Disable(SYSTEM_TIMER_INSTANCE);
|
|
|
|
TMR_Init(SYSTEM_TIMER_INSTANCE, TMR_PRES_1, 0);
|
|
|
|
|
|
|
|
tmr.mode = TMR_MODE_CONTINUOUS;
|
|
|
|
tmr.cmp_cnt = (periodTicks - 1);
|
|
|
|
tmr.pol = 0;
|
|
|
|
|
|
|
|
TMR_Config(SYSTEM_TIMER_INSTANCE, &tmr);
|
|
|
|
|
|
|
|
if(tmr_id == 0){
|
|
|
|
NVIC_SetVector(TMR0_IRQn, SystemTimer_Interrput_Handler);
|
|
|
|
NVIC_EnableIRQ(TMR0_IRQn);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if(tmr_id == 1)
|
|
|
|
{
|
|
|
|
NVIC_SetVector(TMR1_IRQn, SystemTimer_Interrput_Handler);
|
|
|
|
NVIC_EnableIRQ(TMR1_IRQn);
|
|
|
|
}
|
|
|
|
else if(tmr_id == 2)
|
|
|
|
{
|
|
|
|
NVIC_SetVector(TMR2_IRQn, SystemTimer_Interrput_Handler);
|
|
|
|
NVIC_EnableIRQ(TMR2_IRQn);
|
|
|
|
}
|
|
|
|
|
|
|
|
msTickCount = 0;
|
|
|
|
TMR_Enable(SYSTEM_TIMER_INSTANCE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void SystemTimer_Interrput_Handler(void)
|
|
|
|
{
|
|
|
|
TMR_IntClear(SYSTEM_TIMER_INSTANCE);
|
|
|
|
msTickCount++;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static uint32_t SystemTimer_GetPeriodCount(uint32_t prescalar, uint32_t frequency)
|
|
|
|
{
|
|
|
|
uint32_t retVal, clkFreq;
|
|
|
|
clkFreq = PeripheralClock;
|
|
|
|
retVal = clkFreq / (prescalar * frequency);
|
|
|
|
return retVal;
|
|
|
|
}
|