esp_timer_esp32.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. // Copyright 2017 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "esp_err.h"
  15. #include "esp_timer.h"
  16. #include "esp_system.h"
  17. #include "esp_task.h"
  18. #include "esp_attr.h"
  19. #include "esp_intr_alloc.h"
  20. #include "esp_log.h"
  21. #include "esp_clk.h"
  22. #include "esp_timer_impl.h"
  23. #include "soc/frc_timer_reg.h"
  24. #include "soc/rtc.h"
  25. #include "freertos/FreeRTOS.h"
  26. #include "freertos/task.h"
  27. #include "freertos/semphr.h"
  28. /**
  29. * @file esp_timer_esp32.c
  30. * @brief Implementation of chip-specific part of esp_timer
  31. *
  32. * This implementation uses FRC2 (legacy) timer of the ESP32. This timer is
  33. * a 32-bit up-counting timer, with a programmable compare value (called 'alarm'
  34. * hereafter). When the timer reaches compare value, interrupt is raised.
  35. * The timer can be configured to produce an edge or a level interrupt.
  36. *
  37. * In this implementation the timer is used for two purposes:
  38. * 1. To generate interrupts at certain moments — the upper layer of esp_timer
  39. * uses this to trigger callbacks of esp_timer objects.
  40. *
  41. * 2. To keep track of time relative to application start. This facility is
  42. * used both by the upper layer of esp_timer and by time functions, such as
  43. * gettimeofday.
  44. *
  45. * Whenever an esp_timer timer is armed (configured to fire once or
  46. * periodically), timer_insert function of the upper layer calls
  47. * esp_timer_impl_set_alarm to enable the interrupt at the required moment.
  48. * This implementation sets up the timer interrupt to fire at the earliest of
  49. * two moments:
  50. * a) the time requested by upper layer
  51. * b) the time when the timer count reaches 0xffffffff (i.e. is about to overflow)
  52. *
  53. * Whenever the interrupt fires and timer overflow is detected, interrupt hander
  54. * increments s_time_base_us variable, which is used for timekeeping.
  55. *
  56. * When the interrupt fires, the upper layer is notified, and it dispatches
  57. * the callbacks (if any timers have expired) and sets new alarm value (if any
  58. * timers are still active).
  59. *
  60. * At any point in time, esp_timer_impl_get_time will return the current timer
  61. * value (expressed in microseconds) plus s_time_base_us. To account for the
  62. * case when the timer counter has overflown, but the interrupt has not fired
  63. * yet (for example, because interupts are temporarily disabled),
  64. * esp_timer_impl_get_time will also check timer overflow flag, and will add
  65. * s_timer_us_per_overflow to the returned value.
  66. *
  67. */
  68. /* Timer is clocked from APB. To allow for integer scaling factor between ticks
  69. * and microseconds, divider 1 is used. 16 or 256 would not work for APB
  70. * frequencies such as 40 or 26 or 2 MHz.
  71. */
  72. #define TIMER_DIV 1
  73. #define TIMER_DIV_CFG FRC_TIMER_PRESCALER_1
  74. /* ALARM_OVERFLOW_VAL is used as timer alarm value when there are not timers
  75. * enabled which need to fire within the next timer overflow period. This alarm
  76. * is used to perform timekeeping (i.e. to track timer overflows).
  77. * Due to the 0xffffffff cannot recognize the real overflow or the scenario that
  78. * ISR happens follow set_alarm, so change the ALARM_OVERFLOW_VAL to resolve this problem.
  79. * Set it to 0xefffffffUL. The remain 0x10000000UL(about 3 second) is enough to handle ISR.
  80. */
  81. #define ALARM_OVERFLOW_VAL 0xefffffffUL
  82. static const char* TAG = "esp_timer_impl";
  83. // Interrupt handle retuned by the interrupt allocator
  84. static intr_handle_t s_timer_interrupt_handle;
  85. // Function from the upper layer to be called when the interrupt happens.
  86. // Registered in esp_timer_impl_init.
  87. static intr_handler_t s_alarm_handler;
  88. // Time in microseconds from startup to the moment
  89. // when timer counter was last equal to 0. This variable is updated each time
  90. // when timer overflows, and when APB frequency switch is performed.
  91. static uint64_t s_time_base_us;
  92. // Number of timer ticks per microsecond. Calculated from APB frequency.
  93. static uint32_t s_timer_ticks_per_us;
  94. // Period between timer overflows, in microseconds.
  95. // Equal to 2^32 / s_timer_ticks_per_us.
  96. static uint32_t s_timer_us_per_overflow;
  97. // When frequency switch happens, timer counter is reset to 0, s_time_base_us
  98. // is updated, and alarm value is re-calculated based on the new APB frequency.
  99. // However because the frequency switch can happen before the final
  100. // interrupt handler is invoked, interrupt handler may see a different alarm
  101. // value than the one which caused an interrupt. This can cause interrupt handler
  102. // to consider that the interrupt has happened due to timer overflow, incrementing
  103. // s_time_base_us. To avoid this, frequency switch hook sets this flag if
  104. // it needs to set timer alarm value to ALARM_OVERFLOW_VAL. Interrupt hanler
  105. // will not increment s_time_base_us if this flag is set.
  106. static bool s_mask_overflow;
  107. //The timer_overflow_happened read alarm register to tell if overflow happened.
  108. //However, there is a monent that overflow happens, and before ISR function called
  109. //alarm register is set to another value, then you call timer_overflow_happened,
  110. //it will return false.
  111. //So we store the overflow value when new alarm is to be set.
  112. static bool s_overflow_happened;
  113. #ifdef CONFIG_PM_DFS_USE_RTC_TIMER_REF
  114. // If DFS is enabled, upon the first frequency change this value is set to the
  115. // difference between esp_timer value and RTC timer value. On every subsequent
  116. // frequency change, s_time_base_us is adjusted to maintain the same difference
  117. // between esp_timer and RTC timer. (All mentioned values are in microseconds.)
  118. static uint64_t s_rtc_time_diff = 0;
  119. #endif
  120. // Spinlock used to protect access to static variables above and to the hardware
  121. // registers.
  122. portMUX_TYPE s_time_update_lock = portMUX_INITIALIZER_UNLOCKED;
  123. //Use FRC_TIMER_LOAD_VALUE(1) instead of UINT32_MAX, convenience to change FRC TIMER for future
  124. #define TIMER_IS_AFTER_OVERFLOW(a) (ALARM_OVERFLOW_VAL < (a) && (a) <= FRC_TIMER_LOAD_VALUE(1))
  125. // Check if timer overflow has happened (but was not handled by ISR yet)
  126. static inline bool IRAM_ATTR timer_overflow_happened()
  127. {
  128. if (s_overflow_happened) {
  129. return true;
  130. }
  131. return ((REG_READ(FRC_TIMER_CTRL_REG(1)) & FRC_TIMER_INT_STATUS) != 0 &&
  132. ((REG_READ(FRC_TIMER_ALARM_REG(1)) == ALARM_OVERFLOW_VAL && TIMER_IS_AFTER_OVERFLOW(REG_READ(FRC_TIMER_COUNT_REG(1))) && !s_mask_overflow) ||
  133. (!TIMER_IS_AFTER_OVERFLOW(REG_READ(FRC_TIMER_ALARM_REG(1))) && TIMER_IS_AFTER_OVERFLOW(REG_READ(FRC_TIMER_COUNT_REG(1))))));
  134. }
  135. static inline void IRAM_ATTR timer_count_reload(void)
  136. {
  137. //this function should be only called the real overflow happened. And the count cannot be very approach to 0xffffffff.
  138. assert(TIMER_IS_AFTER_OVERFLOW(REG_READ(FRC_TIMER_COUNT_REG(1))));
  139. /* Restart the timer count by current time count minus ALARM_OVERFLOW_VAL(0xefffffff), it may cause error, if current tick is near boundary.
  140. * But even if the error happen 100% per overflow(the distance of each real overflow is about 50 second),
  141. * the error is 0.0125us*N per 50s(the FRC time clock is 80MHz), the N is the ticks run by the line following,
  142. * Normally, N is less than 10, assume N is 10, so the error accumulation is only 6.48ms per month.
  143. * In fact, if the CPU frequency is large than 80MHz. The error accumulation will be more less than 6.48ms per month.
  144. * so It can be adopted.
  145. */
  146. REG_WRITE(FRC_TIMER_LOAD_REG(1), REG_READ(FRC_TIMER_COUNT_REG(1)) - ALARM_OVERFLOW_VAL);
  147. }
  148. void esp_timer_impl_lock()
  149. {
  150. portENTER_CRITICAL(&s_time_update_lock);
  151. }
  152. void esp_timer_impl_unlock()
  153. {
  154. portEXIT_CRITICAL(&s_time_update_lock);
  155. }
  156. uint64_t IRAM_ATTR esp_timer_impl_get_time()
  157. {
  158. uint32_t timer_val;
  159. uint64_t time_base;
  160. uint32_t ticks_per_us;
  161. bool overflow;
  162. do {
  163. /* Read all values needed to calculate current time */
  164. timer_val = REG_READ(FRC_TIMER_COUNT_REG(1));
  165. time_base = s_time_base_us;
  166. overflow = timer_overflow_happened();
  167. ticks_per_us = s_timer_ticks_per_us;
  168. /* Read them again and compare */
  169. /* In this function, do not call timer_count_reload() when overflow is ture.
  170. * Because there's remain count enough to allow FRC_TIMER_COUNT_REG grow
  171. */
  172. if (REG_READ(FRC_TIMER_COUNT_REG(1)) > timer_val &&
  173. time_base == *((volatile uint64_t*) &s_time_base_us) &&
  174. ticks_per_us == *((volatile uint32_t*) &s_timer_ticks_per_us) &&
  175. overflow == timer_overflow_happened()) {
  176. break;
  177. }
  178. /* If any value has changed (other than the counter increasing), read again */
  179. } while(true);
  180. uint64_t result = time_base
  181. + timer_val / ticks_per_us;
  182. return result;
  183. }
  184. void IRAM_ATTR esp_timer_impl_set_alarm(uint64_t timestamp)
  185. {
  186. portENTER_CRITICAL(&s_time_update_lock);
  187. // Alarm time relative to the moment when counter was 0
  188. uint64_t time_after_timebase_us = timestamp - s_time_base_us;
  189. // Adjust current time if overflow has happened
  190. bool overflow = timer_overflow_happened();
  191. uint64_t cur_count = REG_READ(FRC_TIMER_COUNT_REG(1));
  192. uint32_t offset = s_timer_ticks_per_us * 2; //remain 2us for more safe
  193. //If overflow is going to happen in 1us, let's wait until it happens,
  194. //else we think it will not happen before new alarm set.
  195. //And we should wait current timer count less than ALARM_OVERFLOW_VAL,
  196. //maybe equals to 0.
  197. if (cur_count + offset >= ALARM_OVERFLOW_VAL) {
  198. do {
  199. overflow = timer_overflow_happened();
  200. cur_count = REG_READ(FRC_TIMER_COUNT_REG(1));
  201. } while(!overflow || cur_count == ALARM_OVERFLOW_VAL);
  202. }
  203. if (overflow) {
  204. assert(time_after_timebase_us > s_timer_us_per_overflow);
  205. time_after_timebase_us -= s_timer_us_per_overflow;
  206. s_overflow_happened = true;
  207. }
  208. // Calculate desired timer compare value (may exceed 2^32-1)
  209. uint64_t compare_val = time_after_timebase_us * s_timer_ticks_per_us;
  210. uint32_t alarm_reg_val = ALARM_OVERFLOW_VAL;
  211. // Use calculated alarm value if it is less than 2^32-1
  212. if (compare_val < ALARM_OVERFLOW_VAL) {
  213. // If we by the time we update ALARM_REG, COUNT_REG value is higher,
  214. // interrupt will not happen for another 2^32 timer ticks, so need to
  215. // check if alarm value is too close in the future (e.g. <1 us away).
  216. if (compare_val < cur_count + offset) {
  217. compare_val = cur_count + offset;
  218. }
  219. alarm_reg_val = (uint32_t) compare_val;
  220. }
  221. REG_WRITE(FRC_TIMER_ALARM_REG(1), alarm_reg_val);
  222. portEXIT_CRITICAL(&s_time_update_lock);
  223. }
  224. static void IRAM_ATTR timer_alarm_isr(void *arg)
  225. {
  226. portENTER_CRITICAL_ISR(&s_time_update_lock);
  227. // Timekeeping: adjust s_time_base_us if counter has passed ALARM_OVERFLOW_VAL
  228. if (timer_overflow_happened()) {
  229. timer_count_reload();
  230. s_time_base_us += s_timer_us_per_overflow;
  231. s_overflow_happened = false;
  232. }
  233. s_mask_overflow = false;
  234. // Clear interrupt status
  235. REG_WRITE(FRC_TIMER_INT_REG(1), FRC_TIMER_INT_CLR);
  236. // Set alarm to the next overflow moment. Later, upper layer function may
  237. // call esp_timer_impl_set_alarm to change this to an earlier value.
  238. REG_WRITE(FRC_TIMER_ALARM_REG(1), ALARM_OVERFLOW_VAL);
  239. portEXIT_CRITICAL_ISR(&s_time_update_lock);
  240. // Call the upper layer handler
  241. (*s_alarm_handler)(arg);
  242. }
  243. void IRAM_ATTR esp_timer_impl_update_apb_freq(uint32_t apb_ticks_per_us)
  244. {
  245. portENTER_CRITICAL_ISR(&s_time_update_lock);
  246. /* Bail out if the timer is not initialized yet */
  247. if (s_timer_interrupt_handle == NULL) {
  248. portEXIT_CRITICAL_ISR(&s_time_update_lock);
  249. return;
  250. }
  251. uint32_t new_ticks_per_us = apb_ticks_per_us / TIMER_DIV;
  252. uint32_t alarm = REG_READ(FRC_TIMER_ALARM_REG(1));
  253. uint32_t count = REG_READ(FRC_TIMER_COUNT_REG(1));
  254. uint64_t ticks_to_alarm = alarm - count;
  255. uint64_t new_ticks = (ticks_to_alarm * new_ticks_per_us) / s_timer_ticks_per_us;
  256. uint32_t new_alarm_val;
  257. if (alarm > count && new_ticks <= ALARM_OVERFLOW_VAL) {
  258. new_alarm_val = new_ticks;
  259. } else {
  260. new_alarm_val = ALARM_OVERFLOW_VAL;
  261. if (alarm != ALARM_OVERFLOW_VAL) {
  262. s_mask_overflow = true;
  263. }
  264. }
  265. REG_WRITE(FRC_TIMER_ALARM_REG(1), new_alarm_val);
  266. REG_WRITE(FRC_TIMER_LOAD_REG(1), 0);
  267. s_time_base_us += count / s_timer_ticks_per_us;
  268. #ifdef CONFIG_PM_DFS_USE_RTC_TIMER_REF
  269. // Due to the extra time required to read RTC time, don't attempt this
  270. // adjustment when switching to a higher frequency (which usually
  271. // happens in an interrupt).
  272. if (new_ticks_per_us < s_timer_ticks_per_us) {
  273. uint64_t rtc_time = esp_clk_rtc_time();
  274. uint64_t new_rtc_time_diff = s_time_base_us - rtc_time;
  275. if (s_rtc_time_diff != 0) {
  276. uint64_t correction = new_rtc_time_diff - s_rtc_time_diff;
  277. s_time_base_us -= correction;
  278. } else {
  279. s_rtc_time_diff = new_rtc_time_diff;
  280. }
  281. }
  282. #endif // CONFIG_PM_DFS_USE_RTC_TIMER_REF
  283. s_timer_ticks_per_us = new_ticks_per_us;
  284. s_timer_us_per_overflow = ALARM_OVERFLOW_VAL / new_ticks_per_us;
  285. portEXIT_CRITICAL_ISR(&s_time_update_lock);
  286. }
  287. void esp_timer_impl_advance(int64_t time_us)
  288. {
  289. assert(time_us > 0 && "negative adjustments not supported yet");
  290. portENTER_CRITICAL(&s_time_update_lock);
  291. uint64_t count = REG_READ(FRC_TIMER_COUNT_REG(1));
  292. /* Trigger an ISR to handle past alarms and set new one.
  293. * ISR handler will run once we exit the critical section.
  294. */
  295. REG_WRITE(FRC_TIMER_ALARM_REG(1), 0);
  296. REG_WRITE(FRC_TIMER_LOAD_REG(1), 0);
  297. s_time_base_us += count / s_timer_ticks_per_us + time_us;
  298. s_overflow_happened = false;
  299. portEXIT_CRITICAL(&s_time_update_lock);
  300. }
  301. esp_err_t esp_timer_impl_init(intr_handler_t alarm_handler)
  302. {
  303. s_alarm_handler = alarm_handler;
  304. esp_err_t err = esp_intr_alloc(ETS_TIMER2_INTR_SOURCE,
  305. ESP_INTR_FLAG_INTRDISABLED | ESP_INTR_FLAG_IRAM,
  306. &timer_alarm_isr, NULL, &s_timer_interrupt_handle);
  307. if (err != ESP_OK) {
  308. ESP_EARLY_LOGE(TAG, "esp_intr_alloc failed (0x%0x)", err);
  309. return err;
  310. }
  311. uint32_t apb_freq = rtc_clk_apb_freq_get();
  312. s_timer_ticks_per_us = apb_freq / 1000000 / TIMER_DIV;
  313. assert(s_timer_ticks_per_us > 0
  314. && apb_freq % TIMER_DIV == 0
  315. && "APB frequency does not result in a valid ticks_per_us value");
  316. s_timer_us_per_overflow = ALARM_OVERFLOW_VAL / s_timer_ticks_per_us;
  317. s_time_base_us = 0;
  318. REG_WRITE(FRC_TIMER_ALARM_REG(1), ALARM_OVERFLOW_VAL);
  319. REG_WRITE(FRC_TIMER_LOAD_REG(1), 0);
  320. REG_WRITE(FRC_TIMER_CTRL_REG(1),
  321. TIMER_DIV_CFG | FRC_TIMER_ENABLE | FRC_TIMER_LEVEL_INT);
  322. REG_WRITE(FRC_TIMER_INT_REG(1), FRC_TIMER_INT_CLR);
  323. ESP_ERROR_CHECK( esp_intr_enable(s_timer_interrupt_handle) );
  324. return ESP_OK;
  325. }
  326. void esp_timer_impl_deinit()
  327. {
  328. esp_intr_disable(s_timer_interrupt_handle);
  329. REG_WRITE(FRC_TIMER_CTRL_REG(1), 0);
  330. REG_WRITE(FRC_TIMER_ALARM_REG(1), 0);
  331. REG_WRITE(FRC_TIMER_LOAD_REG(1), 0);
  332. esp_intr_free(s_timer_interrupt_handle);
  333. s_timer_interrupt_handle = NULL;
  334. }
  335. // FIXME: This value is safe for 80MHz APB frequency.
  336. // Should be modified to depend on clock frequency.
  337. uint64_t IRAM_ATTR esp_timer_impl_get_min_period_us()
  338. {
  339. return 50;
  340. }