void us_ticker_init(void) { if (us_ticker_inited) { return; } us_ticker_inited = 1; //Common for ticker/timer uint32_t busClock; // Structure to initialize PIT pit_config_t pitConfig; PIT_GetDefaultConfig(&pitConfig); PIT_Init(PIT, &pitConfig); busClock = CLOCK_GetFreq(kCLOCK_BusClk); //Timer PIT_SetTimerPeriod(PIT, kPIT_Chnl_0, busClock / 1000000 - 1); PIT_SetTimerPeriod(PIT, kPIT_Chnl_1, 0xFFFFFFFF); PIT_SetTimerChainMode(PIT, kPIT_Chnl_1, true); PIT_StartTimer(PIT, kPIT_Chnl_0); PIT_StartTimer(PIT, kPIT_Chnl_1); //Ticker PIT_SetTimerPeriod(PIT, kPIT_Chnl_2, busClock / 1000000 - 1); PIT_SetTimerChainMode(PIT, kPIT_Chnl_3, true); NVIC_SetVector(PIT0_IRQn, (uint32_t)pit_isr); NVIC_EnableIRQ(PIT0_IRQn); }
/** Initialize the high frequency ticker * */ void us_ticker_init(void) { /* Common for ticker/timer. */ uint32_t busClock; /* Structure to initialize PIT. */ pit_config_t pitConfig; PIT_GetDefaultConfig(&pitConfig); PIT_Init(PIT, &pitConfig); busClock = CLOCK_GetFreq(kCLOCK_BusClk); /* Let the timer to count if re-init. */ if (!us_ticker_inited) { PIT_SetTimerPeriod(PIT, kPIT_Chnl_0, busClock / 1000000 - 1); PIT_SetTimerPeriod(PIT, kPIT_Chnl_1, 0xFFFFFFFF); PIT_SetTimerChainMode(PIT, kPIT_Chnl_1, true); PIT_StartTimer(PIT, kPIT_Chnl_0); PIT_StartTimer(PIT, kPIT_Chnl_1); } /* Configure interrupt generation counters and disable ticker interrupts. */ PIT_StopTimer(PIT, kPIT_Chnl_3); PIT_StopTimer(PIT, kPIT_Chnl_2); PIT_SetTimerPeriod(PIT, kPIT_Chnl_2, busClock / 1000000 - 1); PIT_SetTimerChainMode(PIT, kPIT_Chnl_3, true); NVIC_SetVector(PIT3_IRQn, (uint32_t) pit_isr); NVIC_EnableIRQ(PIT3_IRQn); PIT_DisableInterrupts(PIT, kPIT_Chnl_3, kPIT_TimerInterruptEnable); us_ticker_inited = true; }
void us_ticker_set_interrupt(timestamp_t timestamp) { uint32_t now_us, delta_us; now_us = us_ticker_read(); delta_us = timestamp >= now_us ? timestamp - now_us : (uint32_t)((uint64_t)timestamp + 0xFFFFFFFF - now_us); PIT_StopTimer(PIT, kPIT_Chnl_3); PIT_StopTimer(PIT, kPIT_Chnl_2); PIT_SetTimerPeriod(PIT, kPIT_Chnl_3, (uint32_t)delta_us); PIT_EnableInterrupts(PIT, kPIT_Chnl_3, kPIT_TimerInterruptEnable); PIT_StartTimer(PIT, kPIT_Chnl_3); PIT_StartTimer(PIT, kPIT_Chnl_2); }
/** Set interrupt for specified timestamp * * @param timestamp The time in ticks when interrupt should be generated */ void us_ticker_set_interrupt(timestamp_t timestamp) { /* We get here absolute interrupt time which takes into account counter overflow. * Since we use additional count-down timer to generate interrupt we need to calculate * load value based on time-stamp. */ const uint32_t now_ticks = us_ticker_read(); uint32_t delta_ticks = timestamp >= now_ticks ? timestamp - now_ticks : (uint32_t)((uint64_t) timestamp + 0xFFFFFFFF - now_ticks); if (delta_ticks == 0) { /* The requested delay is less than the minimum resolution of this counter. */ delta_ticks = 1; } PIT_StopTimer(PIT, kPIT_Chnl_3); PIT_StopTimer(PIT, kPIT_Chnl_2); PIT_SetTimerPeriod(PIT, kPIT_Chnl_3, delta_ticks); PIT_EnableInterrupts(PIT, kPIT_Chnl_3, kPIT_TimerInterruptEnable); PIT_StartTimer(PIT, kPIT_Chnl_3); PIT_StartTimer(PIT, kPIT_Chnl_2); }
void kw41zAlarmInit(void) { pit_config_t config; uint32_t count = (CLOCK_GetBusClkFreq() / 1000) - 1; PIT_GetDefaultConfig(&config); PIT_Init(PIT, &config); PIT_SetTimerPeriod(PIT, kPIT_Chnl_0, count); PIT_StartTimer(PIT, kPIT_Chnl_0); PIT_EnableInterrupts(PIT, kPIT_Chnl_0, kPIT_TimerInterruptEnable); NVIC_ClearPendingIRQ(PIT_IRQn); NVIC_EnableIRQ(PIT_IRQn); }
void us_ticker_init(void) { /* Common for ticker/timer. */ uint32_t busClock; /* Structure to initialize PIT. */ pit_config_t pitConfig; if (us_ticker_inited) { /* calling init again should cancel current interrupt */ TPM_DisableInterrupts(TPM2, kTPM_TimeOverflowInterruptEnable); return; } PIT_GetDefaultConfig(&pitConfig); PIT_Init(PIT, &pitConfig); busClock = CLOCK_GetFreq(kCLOCK_BusClk); PIT_SetTimerPeriod(PIT, kPIT_Chnl_0, busClock / 1000000 - 1); PIT_SetTimerPeriod(PIT, kPIT_Chnl_1, 0xFFFFFFFF); PIT_SetTimerChainMode(PIT, kPIT_Chnl_1, true); PIT_StartTimer(PIT, kPIT_Chnl_0); PIT_StartTimer(PIT, kPIT_Chnl_1); /* Configure interrupt generation counters and disable ticker interrupts. */ tpm_config_t tpmConfig; TPM_GetDefaultConfig(&tpmConfig); /* Set to Div 32 to get 1MHz clock source for TPM */ tpmConfig.prescale = kTPM_Prescale_Divide_32; TPM_Init(TPM2, &tpmConfig); NVIC_SetVector(TPM2_IRQn, (uint32_t)tpm_isr); NVIC_EnableIRQ(TPM2_IRQn); us_ticker_inited = true; }
/*! * @brief Main function */ int main(void) { /* Structure of initialize PIT */ pit_config_t pitConfig; /* Initialize and enable LED */ LED_INIT(); /* Board pin, clock, debug console init */ BOARD_InitPins(); BOARD_BootClockRUN(); BOARD_InitDebugConsole(); /* * pitConfig.enableRunInDebug = false; */ PIT_GetDefaultConfig(&pitConfig); /* Init pit module */ PIT_Init(PIT, &pitConfig); /* Set timer period for channel 0 */ PIT_SetTimerPeriod(PIT, kPIT_Chnl_0, USEC_TO_COUNT(1000000U, PIT_SOURCE_CLOCK)); /* Enable timer interrupts for channel 0 */ PIT_EnableInterrupts(PIT, kPIT_Chnl_0, kPIT_TimerInterruptEnable); /* Enable at the NVIC */ EnableIRQ(PIT_IRQ_ID); /* Start channel 0 */ PRINTF("\r\nStarting channel No.0 ..."); PIT_StartTimer(PIT, kPIT_Chnl_0); while (true) { /* Check whether occur interupt and toggle LED */ if (true == pitIsrFlag) { PRINTF("\r\n Channel No.0 interrupt is occured !"); LED_TOGGLE(); pitIsrFlag = false; } } }