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; } us_ticker_int_counter = (uint32_t)(delta_ticks >> 16); us_ticker_int_remainder = (uint16_t)(0xFFFF & delta_ticks); TPM_StopTimer(TPM2); TPM2->CNT = 0; if (us_ticker_int_counter > 0) { TPM2->MOD = 0xFFFF; us_ticker_int_counter--; } else { TPM2->MOD = us_ticker_int_remainder; us_ticker_int_remainder = 0; } /* Clear the count and set match value */ TPM_ClearStatusFlags(TPM2, kTPM_TimeOverflowFlag); TPM_EnableInterrupts(TPM2, kTPM_TimeOverflowInterruptEnable); TPM_StartTimer(TPM2, kTPM_SystemClock); }
void StackTimer_Disable(void) { #if FSL_FEATURE_SOC_FTM_COUNT FTM_StopTimer(mFtmBase[gStackTimerInstance_c]); #else TPM_StopTimer(mTpmBase[gStackTimerInstance_c]); #endif }
static void tpm_isr(void) { // Clear the TPM timer overflow flag TPM_ClearStatusFlags(TPM2, kTPM_TimeOverflowFlag); TPM_StopTimer(TPM2); if (us_ticker_int_counter > 0) { TPM2->MOD = 0xFFFF; TPM_StartTimer(TPM2, kTPM_SystemClock); us_ticker_int_counter--; } else { if (us_ticker_int_remainder > 0) { TPM2->MOD = us_ticker_int_remainder; TPM_StartTimer(TPM2, kTPM_SystemClock); us_ticker_int_remainder = 0; } else { // This function is going to disable the interrupts if there are // no other events in the queue us_ticker_irq_handler(); } } }
/************************************************************************************ ************************************************************************************* * Public functions ************************************************************************************* ************************************************************************************/ void StackTimer_Init(void (*cb)(void)) { IRQn_Type irqId; #if FSL_FEATURE_SOC_FTM_COUNT FTM_Type *ftmBaseAddr = (FTM_Type*)mFtmBase[gStackTimerInstance_c]; FTM_Init(ftmBaseAddr, &mFtmConfig); FTM_StopTimer(ftmBaseAddr); ftmBaseAddr->MOD = 0xFFFF; /* Configure channel to Software compare; output pin not used */ FTM_SetupOutputCompare(ftmBaseAddr, (ftm_chnl_t)gStackTimerChannel_c, kFTM_NoOutputSignal, 0x01); /* Install ISR */ irqId = mFtmIrqId[gStackTimerInstance_c]; FTM_EnableInterrupts(ftmBaseAddr, kFTM_TimeOverflowInterruptEnable | (1 << gStackTimerChannel_c)); #else TPM_Type *tpmBaseAddr = (TPM_Type*)mTpmBase[gStackTimerInstance_c]; TPM_Init(tpmBaseAddr, &mTpmConfig); TPM_StopTimer(tpmBaseAddr); /* Set the timer to be in free-running mode */ tpmBaseAddr->MOD = 0xFFFF; /* Configure channel to Software compare; output pin not used */ TPM_SetupOutputCompare(tpmBaseAddr, (tpm_chnl_t)gStackTimerChannel_c, kTPM_NoOutputSignal, 0x01); /* Install ISR */ irqId = mTpmIrqId[gStackTimerInstance_c]; TPM_EnableInterrupts(tpmBaseAddr, kTPM_TimeOverflowInterruptEnable | (1 << gStackTimerChannel_c)); #endif /* Overwrite old ISR */ OSA_InstallIntHandler(irqId, cb); /* set interrupt priority */ NVIC_SetPriority(irqId, gStackTimer_IsrPrio_c >> (8 - __NVIC_PRIO_BITS)); NVIC_ClearPendingIRQ(irqId); NVIC_EnableIRQ(irqId); }