esp_timer_impl_lac.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 "esp_timer_impl.h"
  8. #include "esp_timer.h"
  9. #include "esp_err.h"
  10. #include "esp_system.h"
  11. #include "esp_task.h"
  12. #include "esp_attr.h"
  13. #include "esp_intr_alloc.h"
  14. #include "esp_log.h"
  15. #include "esp_private/esp_clk.h"
  16. #include "esp_private/periph_ctrl.h"
  17. #include "soc/soc.h"
  18. #include "soc/timer_group_reg.h"
  19. #include "soc/rtc.h"
  20. #include "freertos/FreeRTOS.h"
  21. /**
  22. * @file esp_timer_lac.c
  23. * @brief Implementation of chip-specific part of esp_timer
  24. *
  25. * This implementation uses TG0 LAC timer of the ESP32. This timer is
  26. * a 64-bit up-counting timer, with a programmable compare value (called 'alarm'
  27. * hereafter). When the timer reaches compare value, interrupt is raised.
  28. * The timer can be configured to produce an edge or a level interrupt.
  29. */
  30. /* Selects which Timer Group peripheral to use */
  31. #define LACT_MODULE 0
  32. #if LACT_MODULE == 0
  33. #define INTR_SOURCE_LACT ETS_TG0_LACT_LEVEL_INTR_SOURCE
  34. #define PERIPH_LACT PERIPH_TIMG0_MODULE
  35. #elif LACT_MODULE == 1
  36. #define INTR_SOURCE_LACT ETS_TG1_LACT_LEVEL_INTR_SOURCE
  37. #define PERIPH_LACT PERIPH_TIMG1_MODULE
  38. #else
  39. #error "Incorrect the number of LACT module (only 0 or 1)"
  40. #endif
  41. /* Desired number of timer ticks per microsecond.
  42. * This value should be small enough so that all possible APB frequencies
  43. * could be divided by it without remainder.
  44. * On the other hand, the smaller this value is, the longer we need to wait
  45. * after setting UPDATE_REG before the timer value can be read.
  46. * If TICKS_PER_US == 1, then we need to wait up to 1 microsecond, which
  47. * makes esp_timer_impl_get_time function take too much time.
  48. * The value TICKS_PER_US == 2 allows for most of the APB frequencies, and
  49. * allows reading the counter quickly enough.
  50. */
  51. #define TICKS_PER_US 2
  52. /* Shorter register names, used in this file */
  53. #define CONFIG_REG (TIMG_LACTCONFIG_REG(LACT_MODULE))
  54. #define RTC_STEP_REG (TIMG_LACTRTC_REG(LACT_MODULE))
  55. #define ALARM_LO_REG (TIMG_LACTALARMLO_REG(LACT_MODULE))
  56. #define ALARM_HI_REG (TIMG_LACTALARMHI_REG(LACT_MODULE))
  57. #define COUNT_LO_REG (TIMG_LACTLO_REG(LACT_MODULE))
  58. #define COUNT_HI_REG (TIMG_LACTHI_REG(LACT_MODULE))
  59. #define UPDATE_REG (TIMG_LACTUPDATE_REG(LACT_MODULE))
  60. #define LOAD_REG (TIMG_LACTLOAD_REG(LACT_MODULE))
  61. #define LOAD_LO_REG (TIMG_LACTLOADLO_REG(LACT_MODULE))
  62. #define LOAD_HI_REG (TIMG_LACTLOADHI_REG(LACT_MODULE))
  63. #define INT_ENA_REG (TIMG_INT_ENA_TIMERS_REG(LACT_MODULE))
  64. #define INT_ST_REG (TIMG_INT_ST_TIMERS_REG(LACT_MODULE))
  65. #define INT_CLR_REG (TIMG_INT_CLR_TIMERS_REG(LACT_MODULE))
  66. /* Helper type to convert between a 64-bit value and a pair of 32-bit values without shifts and masks */
  67. typedef struct {
  68. union {
  69. struct {
  70. uint32_t lo;
  71. uint32_t hi;
  72. };
  73. uint64_t val;
  74. };
  75. } timer_64b_reg_t;
  76. static const char* TAG = "esp_timer_impl";
  77. /* Interrupt handle returned by the interrupt allocator */
  78. static intr_handle_t s_timer_interrupt_handle;
  79. /* Function from the upper layer to be called when the interrupt happens.
  80. * Registered in esp_timer_impl_init.
  81. */
  82. static intr_handler_t s_alarm_handler = NULL;
  83. /* Spinlock used to protect access to the hardware registers. */
  84. portMUX_TYPE s_time_update_lock = portMUX_INITIALIZER_UNLOCKED;
  85. void esp_timer_impl_lock(void)
  86. {
  87. portENTER_CRITICAL(&s_time_update_lock);
  88. }
  89. void esp_timer_impl_unlock(void)
  90. {
  91. portEXIT_CRITICAL(&s_time_update_lock);
  92. }
  93. uint64_t IRAM_ATTR esp_timer_impl_get_counter_reg(void)
  94. {
  95. uint32_t lo, hi;
  96. uint32_t lo_start = REG_READ(COUNT_LO_REG);
  97. uint32_t div = REG_GET_FIELD(CONFIG_REG, TIMG_LACT_DIVIDER);
  98. /* The peripheral doesn't have a bit to indicate that the update is done, so we poll the
  99. * lower 32 bit part of the counter until it changes, or a timeout expires.
  100. */
  101. REG_WRITE(UPDATE_REG, 1);
  102. do {
  103. lo = REG_READ(COUNT_LO_REG);
  104. } while (lo == lo_start && div-- > 0);
  105. /* Since this function is called without a critical section, verify that LO and HI
  106. * registers are consistent. That is, if an interrupt happens between reading LO and
  107. * HI registers, and esp_timer_impl_get_time is called from an ISR, then try to
  108. * detect this by the change in LO register value, and re-read both registers.
  109. */
  110. do {
  111. lo_start = lo;
  112. hi = REG_READ(COUNT_HI_REG);
  113. lo = REG_READ(COUNT_LO_REG);
  114. } while (lo != lo_start);
  115. timer_64b_reg_t result = {
  116. .lo = lo,
  117. .hi = hi
  118. };
  119. return result.val;
  120. }
  121. int64_t IRAM_ATTR esp_timer_impl_get_time(void)
  122. {
  123. return esp_timer_impl_get_counter_reg() / TICKS_PER_US;
  124. }
  125. int64_t esp_timer_get_time(void) __attribute__((alias("esp_timer_impl_get_time")));
  126. void IRAM_ATTR esp_timer_impl_set_alarm_id(uint64_t timestamp, unsigned alarm_id)
  127. {
  128. static uint64_t timestamp_id[2] = { UINT64_MAX, UINT64_MAX };
  129. portENTER_CRITICAL_SAFE(&s_time_update_lock);
  130. timestamp_id[alarm_id] = timestamp;
  131. timestamp = MIN(timestamp_id[0], timestamp_id[1]);
  132. if (timestamp != UINT64_MAX) {
  133. int64_t offset = TICKS_PER_US * 2;
  134. uint64_t now_time = esp_timer_impl_get_counter_reg();
  135. timer_64b_reg_t alarm = { .val = MAX(timestamp * TICKS_PER_US, now_time + offset) };
  136. do {
  137. REG_CLR_BIT(CONFIG_REG, TIMG_LACT_ALARM_EN);
  138. REG_WRITE(ALARM_LO_REG, alarm.lo);
  139. REG_WRITE(ALARM_HI_REG, alarm.hi);
  140. REG_SET_BIT(CONFIG_REG, TIMG_LACT_ALARM_EN);
  141. now_time = esp_timer_impl_get_counter_reg();
  142. int64_t delta = (int64_t)alarm.val - (int64_t)now_time;
  143. if (delta <= 0 && REG_GET_FIELD(INT_ST_REG, TIMG_LACT_INT_ST) == 0) {
  144. // new alarm is less than the counter and the interrupt flag is not set
  145. offset += abs((int)delta) + TICKS_PER_US * 2;
  146. alarm.val = now_time + offset;
  147. } else {
  148. // finish if either (alarm > counter) or the interrupt flag is already set.
  149. break;
  150. }
  151. } while(1);
  152. }
  153. portEXIT_CRITICAL_SAFE(&s_time_update_lock);
  154. }
  155. void IRAM_ATTR esp_timer_impl_set_alarm(uint64_t timestamp)
  156. {
  157. esp_timer_impl_set_alarm_id(timestamp, 0);
  158. }
  159. static void IRAM_ATTR timer_alarm_isr(void *arg)
  160. {
  161. /* Clear interrupt status */
  162. REG_WRITE(INT_CLR_REG, TIMG_LACT_INT_CLR);
  163. /* Call the upper layer handler */
  164. (*s_alarm_handler)(arg);
  165. }
  166. void IRAM_ATTR esp_timer_impl_update_apb_freq(uint32_t apb_ticks_per_us)
  167. {
  168. portENTER_CRITICAL(&s_time_update_lock);
  169. assert(apb_ticks_per_us >= 3 && "divider value too low");
  170. assert(apb_ticks_per_us % TICKS_PER_US == 0 && "APB frequency (in MHz) should be divisible by TICK_PER_US");
  171. REG_SET_FIELD(CONFIG_REG, TIMG_LACT_DIVIDER, apb_ticks_per_us / TICKS_PER_US);
  172. portEXIT_CRITICAL(&s_time_update_lock);
  173. }
  174. void esp_timer_impl_advance(int64_t time_diff_us)
  175. {
  176. portENTER_CRITICAL(&s_time_update_lock);
  177. uint64_t now = esp_timer_impl_get_time();
  178. timer_64b_reg_t dst = { .val = (now + time_diff_us) * TICKS_PER_US };
  179. REG_WRITE(LOAD_LO_REG, dst.lo);
  180. REG_WRITE(LOAD_HI_REG, dst.hi);
  181. REG_WRITE(LOAD_REG, 1);
  182. portEXIT_CRITICAL(&s_time_update_lock);
  183. }
  184. esp_err_t esp_timer_impl_early_init(void)
  185. {
  186. periph_module_enable(PERIPH_LACT);
  187. REG_WRITE(CONFIG_REG, 0);
  188. REG_WRITE(LOAD_LO_REG, 0);
  189. REG_WRITE(LOAD_HI_REG, 0);
  190. REG_WRITE(ALARM_LO_REG, UINT32_MAX);
  191. REG_WRITE(ALARM_HI_REG, UINT32_MAX);
  192. REG_WRITE(LOAD_REG, 1);
  193. REG_SET_BIT(INT_CLR_REG, TIMG_LACT_INT_CLR);
  194. REG_SET_FIELD(CONFIG_REG, TIMG_LACT_DIVIDER, APB_CLK_FREQ / 1000000 / TICKS_PER_US);
  195. REG_SET_BIT(CONFIG_REG, TIMG_LACT_INCREASE |
  196. TIMG_LACT_LEVEL_INT_EN |
  197. TIMG_LACT_EN);
  198. return ESP_OK;
  199. }
  200. esp_err_t esp_timer_impl_init(intr_handler_t alarm_handler)
  201. {
  202. s_alarm_handler = alarm_handler;
  203. const int interrupt_lvl = (1 << CONFIG_ESP_TIMER_INTERRUPT_LEVEL) & ESP_INTR_FLAG_LEVELMASK;
  204. esp_err_t err = esp_intr_alloc(INTR_SOURCE_LACT,
  205. ESP_INTR_FLAG_INTRDISABLED | ESP_INTR_FLAG_IRAM | interrupt_lvl,
  206. &timer_alarm_isr, NULL, &s_timer_interrupt_handle);
  207. if (err != ESP_OK) {
  208. ESP_EARLY_LOGE(TAG, "esp_intr_alloc failed (0x%0x)", err);
  209. return err;
  210. }
  211. /* In theory, this needs a shared spinlock with the timer group driver.
  212. * However since esp_timer_impl_init is called early at startup, this
  213. * will not cause issues in practice.
  214. */
  215. REG_SET_BIT(INT_ENA_REG, TIMG_LACT_INT_ENA);
  216. esp_timer_impl_update_apb_freq(esp_clk_apb_freq() / 1000000);
  217. // Set the step for the sleep mode when the timer will work
  218. // from a slow_clk frequency instead of the APB frequency.
  219. uint32_t slowclk_ticks_per_us = esp_clk_slowclk_cal_get() * TICKS_PER_US;
  220. REG_SET_FIELD(RTC_STEP_REG, TIMG_LACT_RTC_STEP_LEN, slowclk_ticks_per_us);
  221. ESP_ERROR_CHECK( esp_intr_enable(s_timer_interrupt_handle) );
  222. return ESP_OK;
  223. }
  224. void esp_timer_impl_deinit(void)
  225. {
  226. REG_WRITE(CONFIG_REG, 0);
  227. REG_SET_BIT(INT_CLR_REG, TIMG_LACT_INT_CLR);
  228. /* TODO: also clear TIMG_LACT_INT_ENA; however see the note in esp_timer_impl_init. */
  229. esp_intr_disable(s_timer_interrupt_handle);
  230. esp_intr_free(s_timer_interrupt_handle);
  231. s_timer_interrupt_handle = NULL;
  232. }
  233. /* FIXME: This value is safe for 80MHz APB frequency, should be modified to depend on clock frequency. */
  234. uint64_t IRAM_ATTR esp_timer_impl_get_min_period_us(void)
  235. {
  236. return 50;
  237. }
  238. uint64_t esp_timer_impl_get_alarm_reg(void)
  239. {
  240. portENTER_CRITICAL_SAFE(&s_time_update_lock);
  241. timer_64b_reg_t alarm = {
  242. .lo = REG_READ(ALARM_LO_REG),
  243. .hi = REG_READ(ALARM_HI_REG)
  244. };
  245. portEXIT_CRITICAL_SAFE(&s_time_update_lock);
  246. return alarm.val;
  247. }
  248. void esp_timer_private_update_apb_freq(uint32_t apb_ticks_per_us) __attribute__((alias("esp_timer_impl_update_apb_freq")));
  249. void esp_timer_private_advance(int64_t time_us) __attribute__((alias("esp_timer_impl_advance")));
  250. void esp_timer_private_lock(void) __attribute__((alias("esp_timer_impl_lock")));
  251. void esp_timer_private_unlock(void) __attribute__((alias("esp_timer_impl_unlock")));