/* Delay x useconds */ void __udelay(unsigned long usec) { ulong ini, end; ini = READ_TIMER(); end = ini + USEC_TO_COUNT(usec); while ((signed)(end - READ_TIMER()) > 0) ; }
/* delay x useconds */ void __udelay(unsigned long usec) { long tmo = USEC_TO_COUNT(usec); ulong now, last = read_timer(); while (tmo > 0) { now = read_timer(); if (now > last) /* normal (non rollover) */ tmo -= now - last; else /* rollover */ tmo -= TIMER_LOAD_VAL - last + now; last = now; } }
/** Set interrupt for specified timestamp * * @param timestamp The time in microseconds to be set */ void lp_ticker_set_interrupt(timestamp_t timestamp) { uint32_t now_us, delta_us, delta_ticks; if (!lp_ticker_inited) { lp_ticker_init(); } lptmr_schedule = 0; now_us = lp_ticker_read(); delta_us = timestamp > now_us ? timestamp - now_us : (uint32_t)((uint64_t)timestamp + 0xFFFFFFFF - now_us); /* Checking if LPTRM can handle this sleep */ delta_ticks = USEC_TO_COUNT(delta_us, CLOCK_GetFreq(kCLOCK_Er32kClk)); if (delta_ticks > MAX_LPTMR_SLEEP) { /* Using RTC if wait time is over 16b (2s @32kHz) */ uint32_t delta_sec; delta_us += COUNT_TO_USEC(RTC->TPR, CLOCK_GetFreq(kCLOCK_Er32kClk)); /* Accounting for started second */ delta_sec = delta_us / SEC_IN_USEC; delta_us -= delta_sec * SEC_IN_USEC; RTC->TAR = RTC->TSR + delta_sec - 1; RTC_EnableInterrupts(RTC, kRTC_AlarmInterruptEnable); /* Set aditional, subsecond, sleep time */ if (delta_us) { lptmr_schedule = USEC_TO_COUNT(delta_us, CLOCK_GetFreq(kCLOCK_Er32kClk)); } } else { /* Below RTC resolution using LPTMR */ LPTMR_SetTimerPeriod(LPTMR0, delta_ticks); LPTMR_EnableInterrupts(LPTMR0, kLPTMR_TimerInterruptEnable); LPTMR_StartTimer(LPTMR0); } }
/*! * @brief Main function */ int main(void) { uint32_t currentCounter = 0U; lptmr_config_t lptmrConfig; LED_INIT(); /* Board pin, clock, debug console init */ BOARD_InitPins(); BOARD_BootClockRUN(); BOARD_InitDebugConsole(); /* Configure LPTMR */ /* * lptmrConfig.timerMode = kLPTMR_TimerModeTimeCounter; * lptmrConfig.pinSelect = kLPTMR_PinSelectInput_0; * lptmrConfig.pinPolarity = kLPTMR_PinPolarityActiveHigh; * lptmrConfig.enableFreeRunning = false; * lptmrConfig.bypassPrescaler = true; * lptmrConfig.prescalerClockSource = kLPTMR_PrescalerClock_1; * lptmrConfig.value = kLPTMR_Prescale_Glitch_0; */ LPTMR_GetDefaultConfig(&lptmrConfig); /* Initialize the LPTMR */ LPTMR_Init(LPTMR0, &lptmrConfig); /* Set timer period */ LPTMR_SetTimerPeriod(LPTMR0, USEC_TO_COUNT(1000000U, LPTMR_SOURCE_CLOCK)); /* Enable timer interrupt */ LPTMR_EnableInterrupts(LPTMR0, kLPTMR_TimerInterruptEnable); /* Enable at the NVIC */ EnableIRQ(LPTMR0_IRQn); PRINTF("Low Power Timer Example\r\n"); /* Start counting */ LPTMR_StartTimer(LPTMR0); while (1) { if (currentCounter != lptmrCounter) { currentCounter = lptmrCounter; PRINTF("LPTMR interrupt No.%d \r\n", currentCounter); } } }
/*! * @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; } } }
ulong usec2ticks(unsigned long usec) { return USEC_TO_COUNT(usec); }