esp_timer_impl_systimer.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <sys/param.h>
  7. #include "sdkconfig.h"
  8. #include "esp_timer_impl.h"
  9. #include "esp_err.h"
  10. #include "esp_timer.h"
  11. #include "esp_attr.h"
  12. #include "esp_intr_alloc.h"
  13. #include "esp_log.h"
  14. #include "esp_compiler.h"
  15. #include "soc/periph_defs.h"
  16. #include "soc/soc_caps.h"
  17. #include "esp_private/esp_clk.h"
  18. #include "esp_private/systimer.h"
  19. #include "esp_private/periph_ctrl.h"
  20. #include "freertos/FreeRTOS.h"
  21. #include "hal/systimer_ll.h"
  22. #include "hal/systimer_types.h"
  23. #include "hal/systimer_hal.h"
  24. /**
  25. * @file esp_timer_systimer.c
  26. * @brief Implementation of esp_timer using systimer.
  27. *
  28. * This timer is a 64-bit up-counting timer, with a programmable compare value (called 'alarm' hereafter).
  29. * When the timer reaches compare value, interrupt is raised.
  30. * The timer can be configured to produce an edge interrupt.
  31. *
  32. * @note systimer counter0 and alarm2 are adopted to implemented esp_timer
  33. */
  34. static const char *TAG = "esp_timer_systimer";
  35. #define NOT_USED 0xBAD00FAD
  36. /* Interrupt handle returned by the interrupt allocator */
  37. #ifdef CONFIG_ESP_TIMER_ISR_AFFINITY_NO_AFFINITY
  38. #define ISR_HANDLERS (portNUM_PROCESSORS)
  39. #else
  40. #define ISR_HANDLERS (1)
  41. #endif
  42. static intr_handle_t s_timer_interrupt_handle[ISR_HANDLERS] = { NULL };
  43. /* Function from the upper layer to be called when the interrupt happens.
  44. * Registered in esp_timer_impl_init.
  45. */
  46. static intr_handler_t s_alarm_handler = NULL;
  47. /* Systimer HAL layer object */
  48. static systimer_hal_context_t systimer_hal;
  49. /* Spinlock used to protect access to the hardware registers. */
  50. static portMUX_TYPE s_time_update_lock = portMUX_INITIALIZER_UNLOCKED;
  51. void esp_timer_impl_lock(void)
  52. {
  53. portENTER_CRITICAL(&s_time_update_lock);
  54. }
  55. void esp_timer_impl_unlock(void)
  56. {
  57. portEXIT_CRITICAL(&s_time_update_lock);
  58. }
  59. uint64_t IRAM_ATTR esp_timer_impl_get_counter_reg(void)
  60. {
  61. return systimer_hal_get_counter_value(&systimer_hal, SYSTIMER_COUNTER_ESPTIMER);
  62. }
  63. int64_t IRAM_ATTR esp_timer_impl_get_time(void)
  64. {
  65. // we hope the execution time of this function won't > 1us
  66. // thus, to save one function call, we didn't use the existing `systimer_hal_get_time`
  67. return systimer_hal.ticks_to_us(systimer_hal_get_counter_value(&systimer_hal, SYSTIMER_COUNTER_ESPTIMER));
  68. }
  69. int64_t esp_timer_get_time(void) __attribute__((alias("esp_timer_impl_get_time")));
  70. void IRAM_ATTR esp_timer_impl_set_alarm_id(uint64_t timestamp, unsigned alarm_id)
  71. {
  72. static uint64_t timestamp_id[2] = { UINT64_MAX, UINT64_MAX };
  73. portENTER_CRITICAL_SAFE(&s_time_update_lock);
  74. timestamp_id[alarm_id] = timestamp;
  75. timestamp = MIN(timestamp_id[0], timestamp_id[1]);
  76. systimer_hal_set_alarm_target(&systimer_hal, SYSTIMER_ALARM_ESPTIMER, timestamp);
  77. portEXIT_CRITICAL_SAFE(&s_time_update_lock);
  78. }
  79. void IRAM_ATTR esp_timer_impl_set_alarm(uint64_t timestamp)
  80. {
  81. esp_timer_impl_set_alarm_id(timestamp, 0);
  82. }
  83. static void IRAM_ATTR timer_alarm_isr(void *arg)
  84. {
  85. #if ISR_HANDLERS == 1
  86. // clear the interrupt
  87. systimer_ll_clear_alarm_int(systimer_hal.dev, SYSTIMER_ALARM_ESPTIMER);
  88. /* Call the upper layer handler */
  89. (*s_alarm_handler)(arg);
  90. #else
  91. static volatile uint32_t processed_by = NOT_USED;
  92. static volatile bool pending_alarm = false;
  93. /* CRITICAL section ensures the read/clear is atomic between cores */
  94. portENTER_CRITICAL_ISR(&s_time_update_lock);
  95. if (systimer_ll_is_alarm_int_fired(systimer_hal.dev, SYSTIMER_ALARM_ESPTIMER)) {
  96. // Clear interrupt status
  97. systimer_ll_clear_alarm_int(systimer_hal.dev, SYSTIMER_ALARM_ESPTIMER);
  98. // Is the other core already processing a previous alarm?
  99. if (processed_by == NOT_USED) {
  100. // Current core is not processing an alarm yet
  101. processed_by = xPortGetCoreID();
  102. do {
  103. pending_alarm = false;
  104. // Clear interrupt status
  105. systimer_ll_clear_alarm_int(systimer_hal.dev, SYSTIMER_ALARM_ESPTIMER);
  106. portEXIT_CRITICAL_ISR(&s_time_update_lock);
  107. (*s_alarm_handler)(arg);
  108. portENTER_CRITICAL_ISR(&s_time_update_lock);
  109. // Another alarm could have occurred while were handling the previous alarm.
  110. // Check if we need to call the s_alarm_handler again:
  111. // 1) if the alarm has already been fired, it helps to handle it immediately without an additional ISR call.
  112. // 2) handle pending alarm that was cleared by the other core in time when this core worked with the current alarm.
  113. } while (systimer_ll_is_alarm_int_fired(systimer_hal.dev, SYSTIMER_ALARM_ESPTIMER) || pending_alarm);
  114. processed_by = NOT_USED;
  115. } else {
  116. // Current core arrived at ISR but the other core is still handling a previous alarm.
  117. // Once we already cleared the ISR status we need to let the other core know that it was.
  118. // Set the flag to handle the current alarm by the other core later.
  119. pending_alarm = true;
  120. }
  121. }
  122. portEXIT_CRITICAL_ISR(&s_time_update_lock);
  123. #endif // ISR_HANDLERS != 1
  124. }
  125. void IRAM_ATTR esp_timer_impl_update_apb_freq(uint32_t apb_ticks_per_us)
  126. {
  127. #if !SOC_SYSTIMER_FIXED_DIVIDER
  128. systimer_hal_on_apb_freq_update(&systimer_hal, apb_ticks_per_us);
  129. #endif
  130. }
  131. void esp_timer_impl_set(uint64_t new_us)
  132. {
  133. portENTER_CRITICAL_SAFE(&s_time_update_lock);
  134. systimer_counter_value_t new_count = {
  135. .val = systimer_hal.us_to_ticks(new_us),
  136. };
  137. systimer_ll_set_counter_value(systimer_hal.dev, SYSTIMER_COUNTER_ESPTIMER, new_count.val);
  138. systimer_ll_apply_counter_value(systimer_hal.dev, SYSTIMER_COUNTER_ESPTIMER);
  139. portEXIT_CRITICAL_SAFE(&s_time_update_lock);
  140. }
  141. void esp_timer_impl_advance(int64_t time_diff_us)
  142. {
  143. portENTER_CRITICAL_SAFE(&s_time_update_lock);
  144. systimer_hal_counter_value_advance(&systimer_hal, SYSTIMER_COUNTER_ESPTIMER, time_diff_us);
  145. portEXIT_CRITICAL_SAFE(&s_time_update_lock);
  146. }
  147. esp_err_t esp_timer_impl_early_init(void)
  148. {
  149. periph_module_enable(PERIPH_SYSTIMER_MODULE);
  150. systimer_hal_tick_rate_ops_t ops = {
  151. .ticks_to_us = systimer_ticks_to_us,
  152. .us_to_ticks = systimer_us_to_ticks,
  153. };
  154. systimer_hal_init(&systimer_hal);
  155. systimer_hal_set_tick_rate_ops(&systimer_hal, &ops);
  156. #if !SOC_SYSTIMER_FIXED_DIVIDER
  157. assert(esp_clk_xtal_freq() == (40 * 1000000) &&
  158. "update the step for xtal to support other XTAL:APB frequency ratios");
  159. systimer_hal_set_steps_per_tick(&systimer_hal, 0, 2); // for xtal
  160. systimer_hal_set_steps_per_tick(&systimer_hal, 1, 1); // for pll
  161. #endif
  162. systimer_hal_enable_counter(&systimer_hal, SYSTIMER_COUNTER_ESPTIMER);
  163. systimer_hal_select_alarm_mode(&systimer_hal, SYSTIMER_ALARM_ESPTIMER, SYSTIMER_ALARM_MODE_ONESHOT);
  164. systimer_hal_connect_alarm_counter(&systimer_hal, SYSTIMER_ALARM_ESPTIMER, SYSTIMER_COUNTER_ESPTIMER);
  165. return ESP_OK;
  166. }
  167. esp_err_t esp_timer_impl_init(intr_handler_t alarm_handler)
  168. {
  169. if (s_timer_interrupt_handle[(ISR_HANDLERS == 1) ? 0 : xPortGetCoreID()] != NULL) {
  170. ESP_EARLY_LOGE(TAG, "timer ISR is already initialized");
  171. return ESP_ERR_INVALID_STATE;
  172. }
  173. int isr_flags = ESP_INTR_FLAG_INTRDISABLED
  174. | ((1 << CONFIG_ESP_TIMER_INTERRUPT_LEVEL) & ESP_INTR_FLAG_LEVELMASK)
  175. #if !SOC_SYSTIMER_INT_LEVEL
  176. | ESP_INTR_FLAG_EDGE
  177. #endif
  178. | ESP_INTR_FLAG_IRAM;
  179. esp_err_t err = esp_intr_alloc(ETS_SYSTIMER_TARGET2_EDGE_INTR_SOURCE, isr_flags,
  180. &timer_alarm_isr, NULL,
  181. &s_timer_interrupt_handle[(ISR_HANDLERS == 1) ? 0 : xPortGetCoreID()]);
  182. if (err != ESP_OK) {
  183. ESP_EARLY_LOGE(TAG, "esp_intr_alloc failed (0x%x)", err);
  184. return err;
  185. }
  186. if (s_alarm_handler == NULL) {
  187. s_alarm_handler = alarm_handler;
  188. /* TODO: if SYSTIMER is used for anything else, access to SYSTIMER_INT_ENA_REG has to be
  189. * protected by a shared spinlock. Since this code runs as part of early startup, this
  190. * is practically not an issue.
  191. */
  192. systimer_hal_enable_alarm_int(&systimer_hal, SYSTIMER_ALARM_ESPTIMER);
  193. }
  194. err = esp_intr_enable(s_timer_interrupt_handle[(ISR_HANDLERS == 1) ? 0 : xPortGetCoreID()]);
  195. if (err != ESP_OK) {
  196. ESP_EARLY_LOGE(TAG, "Can not enable ISR (0x%0x)", err);
  197. }
  198. return err;
  199. }
  200. void esp_timer_impl_deinit(void)
  201. {
  202. systimer_ll_enable_alarm(systimer_hal.dev, SYSTIMER_ALARM_ESPTIMER, false);
  203. /* TODO: may need a spinlock, see the note related to SYSTIMER_INT_ENA_REG in systimer_hal_init */
  204. systimer_ll_enable_alarm_int(systimer_hal.dev, SYSTIMER_ALARM_ESPTIMER, false);
  205. for (unsigned i = 0; i < ISR_HANDLERS; i++) {
  206. if (s_timer_interrupt_handle[i] != NULL) {
  207. esp_intr_disable(s_timer_interrupt_handle[i]);
  208. esp_intr_free(s_timer_interrupt_handle[i]);
  209. s_timer_interrupt_handle[i] = NULL;
  210. }
  211. }
  212. s_alarm_handler = NULL;
  213. }
  214. uint64_t IRAM_ATTR esp_timer_impl_get_min_period_us(void)
  215. {
  216. return 50;
  217. }
  218. uint64_t esp_timer_impl_get_alarm_reg(void)
  219. {
  220. portENTER_CRITICAL_SAFE(&s_time_update_lock);
  221. uint64_t val = systimer_hal_get_alarm_value(&systimer_hal, SYSTIMER_ALARM_ESPTIMER);
  222. portEXIT_CRITICAL_SAFE(&s_time_update_lock);
  223. return val;
  224. }
  225. void esp_timer_private_update_apb_freq(uint32_t apb_ticks_per_us) __attribute__((alias("esp_timer_impl_update_apb_freq")));
  226. void esp_timer_private_set(uint64_t new_us) __attribute__((alias("esp_timer_impl_set")));
  227. void esp_timer_private_advance(int64_t time_diff_us) __attribute__((alias("esp_timer_impl_advance")));
  228. void esp_timer_private_lock(void) __attribute__((alias("esp_timer_impl_lock")));
  229. void esp_timer_private_unlock(void) __attribute__((alias("esp_timer_impl_unlock")));