/****************************************************************************** * * @name AddTimerQ * * @brief Adds Timer Object to Timer Queue * * @param pTimerObject : Pointer to Timer Object * * @return None ***************************************************************************** * Adds Timer Object to Timer Queue *****************************************************************************/ uint_8 AddTimerQ(PTIMER_OBJECT pTimerObject) { uint_8 index; if(pTimerObject == NULL) return (uint_8)ERR_INVALID_PARAM; if(pTimerObject->msCount == (unsigned int)INVALID_TIME_COUNT) return (uint_8)ERR_INVALID_PARAM; for(index = 0; index < MAX_TIMER_OBJECTS; index++) { /* Disable Timer Interrupts */ DisableTimerInterrupt(); if(g_TimerObjectArray[index].pfnTimerCallback == NULL) { (void)memcpy(&g_TimerObjectArray[index], pTimerObject, sizeof(TIMER_OBJECT)); /* Enable Timer Interrupts */ EnableTimerInterrupt(); break; } /* Enable Timer Interrupts */ EnableTimerInterrupt(); } if(index == MAX_TIMER_OBJECTS) return (uint_8)ERR_TIMER_QUEUE_FULL; return index; }
/*FUNCTION*------------------------------------------------------------------- * * Function Name : TimerInit * Returned Value : * Comments : Initialize timer module * * *END*----------------------------------------------------------------------*/ void TimerInit(void) { /* Enable LPT Module Clock */ SIM_SCGC5|=SIM_SCGC5_LPTIMER_MASK; /* Configure LPT */ /* Enable LPT Interrupt in NVIC*/ extern uint32 __VECTOR_RAM[]; //Get vector table that was copied to RAM __VECTOR_RAM[101]=(uint_32)Timer_ISR; //replace ISR NVICICPR2|=(1<<21); //Clear any pending interrupts on LPT NVICISER2|=(1<<21); //Enable interrupts from LPT module /* RTCSC_RTIF = 0x01; // Clear previous RTC Interrupt RTCMOD = 0x0B; // 1 ms Interrupt Generation // Start RTC by Reseting Counter to 0 RTCSC = 0x08; // Prescaler = 1000 Clock = 12MHz (external clock ) //RTCSC = 0xA8; // Prescaler = 1000 Clock = 12MHz (external clock ) */ EnableTimerInterrupt(); }
/*FUNCTION*------------------------------------------------------------------- * * Function Name : TimerInit * Returned Value : * Comments : Initialize timer module * * *END*----------------------------------------------------------------------*/ void TimerInit(void) { TPM2SC = 0; TPM2MOD = 1500; /* 1 ms time base */ TPM2SC_PS = 0x04; /* divide by 16 */ EnableTimerInterrupt(); TPM2SC_CLKSx = 0x1; /* Bus Clock Source 24 MHZ*/ }
int main() { InitializeLEDs(); InitializeTimer(); EnableTimerInterrupt(); while(1); }
/****************************************************************************** * * @name RemoveTimerQ * * @brief Removes Timer Object from Timer Queue * * @param index : Index of Timer Object * * @return None ***************************************************************************** * Removes Timer Object from Timer Queue *****************************************************************************/ uint_8 RemoveTimerQ(uint_8 index) { if(index >= MAX_TIMER_OBJECTS) return (uint_8)ERR_INVALID_PARAM; /* Disable Timer Interrupts */ DisableTimerInterrupt(); (void)memset(&g_TimerObjectArray[index], (int)NULL, sizeof(TIMER_OBJECT)); /* Enable Timer Interrupts */ EnableTimerInterrupt(); return (uint_8)ERR_SUCCESS; }
/*FUNCTION*------------------------------------------------------------------- * * Function Name : time_delay * Returned Value : * Comments : Wait until interrupt of timer occur * * *END*----------------------------------------------------------------------*/ void time_delay(uint_32 delay) { delay_count = delay * 10; EnableTimerInterrupt(); TPM2CNT = 0; while(1){ if(0 >= delay_count){ DisableTimerInterrupt(); break; } } }
/****************************************************************************** * @name TimerInit * * @brief This is RTC initialization function * * @return None * ****************************************************************************** * Intiializes the RTC module registers *****************************************************************************/ static uint_8 TimerInit(void) { PTAD_PTAD5 = 1; PTAD_PTAD6 = 1; PTBD_PTBD0 = 1; PTADD_PTADD5 = 1; PTADD_PTADD6 = 1; PTBDD_PTBDD0 = 1; TPM2SC = 0; TPM2MOD = 1500; TPM2SC_PS = 0x04; EnableTimerInterrupt(); TPM2SC_CLKSx = 0x1; /* Select Internal Clock Source */ return ERR_SUCCESS; }
/*FUNCTION*------------------------------------------------------------------- * * Function Name : TimerInit * Returned Value : * Comments : Initialize timer module * * *END*----------------------------------------------------------------------*/ void TimerInit(void) { /* Enable LPT Module Clock */ #ifdef MCU_MKL25Z4 SIM_SCGC5 |= SIM_SCGC5_LPTMR_MASK; #else SIM_SCGC5 |= SIM_SCGC5_LPTIMER_MASK; #endif /* Configure LPT */ LPTMR0_CMR = LPTMR_CMR_COMPARE(1); // Set compare value LPTMR0_PSR = LPTMR_PSR_PCS(0x1); //Use internal 1khz clock LPTMR0_CSR = LPTMR_CSR_TIE_MASK; //Enable LPT interrupt LPTMR0_CSR |= LPTMR_CSR_TEN_MASK; //Turn on LPT and start counting EnableTimerInterrupt(); }
/****************************************************************************** * @name Timer_ISR * * @brief This routine services RTC Interrupt * * @param None * * @return None * ****************************************************************************** * Services RTC Interrupt. If a Timer Object expires, then removes the object * from Timer Queue and Calls the callback function (if registered) *****************************************************************************/ void Timer_ISR(void) { uint_8 index; if(PIT_TFLG0 & PIT_TFLG_TIF_MASK) { /* Clear RTC Interrupt */ PIT_TFLG0 |= PIT_TFLG_TIF_MASK; DisableTimerInterrupt(); NVICICPR2|=(1<<4); /* Clear any pending interrupts on LPT */ NVICISER2|=(1<<4); /* Enable interrupts from LPT module */ /* Call Pending Timer CallBacks */ for (index = 0; index < MAX_TIMER_OBJECTS; index++) { PTIMER_OBJECT ptemp = &g_TimerObjectArray[index]; if(ptemp->pfnTimerCallback == NULL) { continue; } ptemp->msCount--; if (ptemp->msCount < 0) { PFNTIMER_CALLBACK pfnTimerCallback = ptemp->pfnTimerCallback; #ifdef TIMER_CALLBACK_ARG void *parg = ptemp->arg; #endif (void)RemoveTimerQ(index); #ifdef TIMER_CALLBACK_ARG pfnTimerCallback(parg); #else pfnTimerCallback(); #endif } } } EnableTimerInterrupt(); }
/*FUNCTION*------------------------------------------------------------------- * * Function Name : TimerInit * Returned Value : * Comments : Initialize timer module * * *END*----------------------------------------------------------------------*/ void TimerInit(void) { /* Enable LPT Module Clock */ SIM_SCGC5|=SIM_SCGC5_LPTIMER_MASK; /* Configure LPT */ /* Enable LPT Interrupt in NVIC*/ NVICICPR2|=(1<<21); //Clear any pending interrupts on LPT NVICISER2|=(1<<21); //Enable interrupts from LPT module /* RTCSC_RTIF = 0x01; // Clear previous RTC Interrupt RTCMOD = 0x0B; // 1 ms Interrupt Generation // Start RTC by Reseting Counter to 0 RTCSC = 0x08; // Prescaler = 1000 Clock = 12MHz (external clock ) //RTCSC = 0xA8; // Prescaler = 1000 Clock = 12MHz (external clock ) */ EnableTimerInterrupt(); }
/*FUNCTION*------------------------------------------------------------------- * * Function Name : TimerInit * Returned Value : * Comments : Initialize timer module * * *END*----------------------------------------------------------------------*/ void TimerInit(void) { /* Enable LPT Module Clock */ SIM_SCGC5|=SIM_SCGC5_LPTMR_MASK; /* Configure LPT */ /* Enable LPT Interrupt in NVIC*/ extern uint32 __VECTOR_RAM[]; //Get vector table that was copied to RAM __VECTOR_RAM[INT_LPTimer]=(uint_32)Timer_ISR; //replace ISR enable_irq(INT_LPTimer - 16); /* RTCSC_RTIF = 0x01; // Clear previous RTC Interrupt RTCMOD = 0x0B; // 1 ms Interrupt Generation // Start RTC by Reseting Counter to 0 RTCSC = 0x08; // Prescaler = 1000 Clock = 12MHz (external clock ) //RTCSC = 0xA8; // Prescaler = 1000 Clock = 12MHz (external clock ) */ EnableTimerInterrupt(); }