/** * * @brief System clock periodic tick handler * * This routine handles the system clock periodic tick interrupt. It always * announces one tick. * * @return N/A */ void _timer_int_handler(void *unused) { ARG_UNUSED(unused); /* clear the interrupt by writing 0 to IP bit of the control register */ timer0_control_register_set(_ARC_V2_TMR_CTRL_NH | _ARC_V2_TMR_CTRL_IE); #ifdef CONFIG_TICKLESS_KERNEL if (!programmed_ticks) { if (_sys_clock_always_on) { _sys_clock_tick_count = _get_elapsed_clock_time(); program_max_cycles(); } return; } _sys_idle_elapsed_ticks = programmed_ticks; /* * Clear programmed ticks before announcing elapsed time so * that recursive calls to _update_elapsed_time() will not * announce already consumed elapsed time */ programmed_ticks = 0; timer_expired = 1; _sys_clock_tick_announce(); /* _sys_clock_tick_announce() could cause new programming */ if (!programmed_ticks && _sys_clock_always_on) { _sys_clock_tick_count = _get_elapsed_clock_time(); program_max_cycles(); } #else #if defined(CONFIG_TICKLESS_IDLE) timer0_limit_register_set(cycles_per_tick - 1); __ASSERT_EVAL({}, u32_t timer_count = timer0_count_register_get(), timer_count <= (cycles_per_tick - 1), "timer_count: %d, limit %d\n", timer_count, cycles_per_tick - 1); _sys_clock_final_tick_announce(); #else _sys_clock_tick_announce(); #endif update_accumulated_count(); #endif }
/* * Set RTC Counter Compare (CC) register to max value * and update the _sys_clock_tick_count. */ static inline void program_max_cycles(void) { u32_t max_cycles = _get_max_clock_time(); _sys_clock_tick_count = _get_elapsed_clock_time(); /* Update rtc_past to track rtc timer count*/ rtc_past = (_sys_clock_tick_count * sys_clock_hw_cycles_per_tick) & RTC_MASK; /* Programe RTC compare register to generate interrupt*/ rtc_compare_set(rtc_past + (max_cycles * sys_clock_hw_cycles_per_tick)); }
void _set_time(u32_t time) { if (!time) { programmed_ticks = 0; return; } programmed_ticks = time > max_system_ticks ? max_system_ticks : time; _sys_clock_tick_count = _get_elapsed_clock_time(); timer0_limit_register_set(programmed_ticks * cycles_per_tick); timer0_count_register_set(0); timer_expired = 0; }
void _set_time(u32_t time) { if (!time) { idle_original_ticks = 0; return; } idle_original_ticks = time > max_system_ticks ? max_system_ticks : time; _sys_clock_tick_count = _get_elapsed_clock_time(); /* clear overflow tracking flag as it is accounted */ timer_overflow = 0; sysTickStop(); sysTickReloadSet(idle_original_ticks * default_load_value); sysTickStart(); sys_tick_reload(); }
void _set_time(u32_t time) { if (!time) { expected_sys_ticks = 0; return; } /* Update expected_sys_ticls to time to programe*/ expected_sys_ticks = time; _sys_clock_tick_count = _get_elapsed_clock_time(); /* Update rtc_past to track rtc timer count*/ rtc_past = (_sys_clock_tick_count * sys_clock_hw_cycles_per_tick) & RTC_MASK; expected_sys_ticks = expected_sys_ticks > _get_max_clock_time() ? _get_max_clock_time() : expected_sys_ticks; /* Programe RTC compare register to generate interrupt*/ rtc_compare_set(rtc_past + (expected_sys_ticks * sys_clock_hw_cycles_per_tick)); }
/** * * @brief Handling of tickless idle when interrupted * * The routine, called by _sys_power_save_idle_exit, is responsible for taking * the timer out of idle mode and generating an interrupt at the next * tick interval. It is expected that interrupts have been disabled. * * Note that in this routine, _sys_idle_elapsed_ticks must be zero because the * ticker has done its work and consumed all the ticks. This has to be true * otherwise idle mode wouldn't have been entered in the first place. * * @return N/A */ void _timer_idle_exit(void) { #ifdef CONFIG_TICKLESS_KERNEL if (idle_mode == IDLE_TICKLESS) { idle_mode = IDLE_NOT_TICKLESS; if (!idle_original_ticks && _sys_clock_always_on) { _sys_clock_tick_count = _get_elapsed_clock_time(); timer_overflow = 0; sysTickReloadSet(max_load_value); sysTickStart(); sys_tick_reload(); } } #else u32_t count; /* timer's current count register value */ if (timer_mode == TIMER_MODE_PERIODIC) { /* * The timer interrupt handler is handling a completed tickless * idle or this has been called by mistake; there's nothing to * do here. */ return; } sysTickStop(); /* timer is in idle mode, adjust the ticks expired */ count = sysTickCurrentGet(); if ((count == 0) || (SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk)) { /* * The timer expired and/or wrapped around. Re-set the timer to * its default value and mode. */ sysTickReloadSet(default_load_value); timer_mode = TIMER_MODE_PERIODIC; /* * Announce elapsed ticks to the kernel. Note we are guaranteed * that the timer ISR will execute before the tick event is * serviced, so _sys_idle_elapsed_ticks is adjusted to account * for it. */ _sys_idle_elapsed_ticks = idle_original_ticks - 1; _sys_clock_tick_announce(); } else { u32_t elapsed; /* elapsed "counter time" */ u32_t remaining; /* remaining "counter time" */ elapsed = idle_original_count - count; remaining = elapsed % default_load_value; /* ensure that the timer will interrupt at the next tick */ if (remaining == 0) { /* * Idle was interrupted on a tick boundary. Re-set the * timer to its default value and mode. */ sysTickReloadSet(default_load_value); timer_mode = TIMER_MODE_PERIODIC; } else if (count > remaining) { /* * There is less time remaining to the next tick * boundary than time left for idle. Leave in "one * shot" mode. */ sysTickReloadSet(remaining); } _sys_idle_elapsed_ticks = elapsed / default_load_value; if (_sys_idle_elapsed_ticks) { _sys_clock_tick_announce(); } } idle_mode = IDLE_NOT_TICKLESS; sysTickStart(); #endif }
/** * * @brief System clock tick handler * * This routine handles the system clock tick interrupt. A TICK_EVENT event * is pushed onto the kernel stack. * * The symbol for this routine is either _timer_int_handler. * * @return N/A */ void _timer_int_handler(void *unused) { ARG_UNUSED(unused); #ifdef CONFIG_EXECUTION_BENCHMARKING extern void read_systick_start_of_tick_handler(void); read_systick_start_of_tick_handler(); #endif #ifdef CONFIG_KERNEL_EVENT_LOGGER_INTERRUPT extern void _sys_k_event_logger_interrupt(void); _sys_k_event_logger_interrupt(); #endif #ifdef CONFIG_SYS_POWER_MANAGEMENT s32_t numIdleTicks; /* * All interrupts are disabled when handling idle wakeup. * For tickless idle, this ensures that the calculation and programming * of * the device for the next timer deadline is not interrupted. * For non-tickless idle, this ensures that the clearing of the kernel * idle * state is not interrupted. * In each case, _sys_power_save_idle_exit is called with interrupts * disabled. */ __asm__(" cpsid i"); /* PRIMASK = 1 */ #ifdef CONFIG_TICKLESS_IDLE #if defined(CONFIG_TICKLESS_KERNEL) if (!idle_original_ticks) { if (_sys_clock_always_on) { _sys_clock_tick_count = _get_elapsed_clock_time(); /* clear overflow tracking flag as it is accounted */ timer_overflow = 0; sysTickStop(); idle_original_ticks = max_system_ticks; sysTickReloadSet(max_load_value); sysTickStart(); sys_tick_reload(); } __asm__(" cpsie i"); /* re-enable interrupts (PRIMASK = 0) */ _ExcExit(); return; } idle_mode = IDLE_NOT_TICKLESS; _sys_idle_elapsed_ticks = idle_original_ticks; /* * Clear programmed ticks before announcing elapsed time so * that recursive calls to _update_elapsed_time() will not * announce already consumed elapsed time */ idle_original_ticks = 0; _sys_clock_tick_announce(); /* _sys_clock_tick_announce() could cause new programming */ if (!idle_original_ticks && _sys_clock_always_on) { _sys_clock_tick_count = _get_elapsed_clock_time(); /* clear overflow tracking flag as it is accounted */ timer_overflow = 0; sysTickStop(); sysTickReloadSet(max_load_value); sysTickStart(); sys_tick_reload(); } #else /* * If this a wakeup from a completed tickless idle or after * _timer_idle_exit has processed a partial idle, return * to the normal tick cycle. */ if (timer_mode == TIMER_MODE_ONE_SHOT) { sysTickStop(); sysTickReloadSet(default_load_value); sysTickStart(); timer_mode = TIMER_MODE_PERIODIC; } /* set the number of elapsed ticks and announce them to the kernel */ if (idle_mode == IDLE_TICKLESS) { /* tickless idle completed without interruption */ idle_mode = IDLE_NOT_TICKLESS; _sys_idle_elapsed_ticks = idle_original_ticks + 1; /* actual # of idle ticks */ _sys_clock_tick_announce(); } else { _sys_clock_final_tick_announce(); } /* accumulate total counter value */ clock_accumulated_count += default_load_value * _sys_idle_elapsed_ticks; #endif #else /* !CONFIG_TICKLESS_IDLE */ /* * No tickless idle: * Update the total tick count and announce this tick to the kernel. */ clock_accumulated_count += sys_clock_hw_cycles_per_tick; _sys_clock_tick_announce(); #endif /* CONFIG_TICKLESS_IDLE */ numIdleTicks = _NanoIdleValGet(); /* get # of idle ticks requested */ if (numIdleTicks) { _NanoIdleValClear(); /* clear kernel idle setting */ /* * Complete idle processing. * Note that for tickless idle, nothing will be done in * _timer_idle_exit. */ _sys_power_save_idle_exit(numIdleTicks); } __asm__(" cpsie i"); /* re-enable interrupts (PRIMASK = 0) */ #else /* !CONFIG_SYS_POWER_MANAGEMENT */ /* accumulate total counter value */ clock_accumulated_count += sys_clock_hw_cycles_per_tick; /* * one more tick has occurred -- don't need to do anything special since * timer is already configured to interrupt on the following tick */ _sys_clock_tick_announce(); #endif /* CONFIG_SYS_POWER_MANAGEMENT */ #ifdef CONFIG_EXECUTION_BENCHMARKING extern void read_systick_end_of_tick_handler(void); read_systick_end_of_tick_handler(); #endif extern void _ExcExit(void); _ExcExit(); }