esp_timer_impl_systimer.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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_err.h"
  9. #include "esp_timer.h"
  10. #include "esp_attr.h"
  11. #include "esp_intr_alloc.h"
  12. #include "esp_log.h"
  13. #include "esp_compiler.h"
  14. #include "soc/periph_defs.h"
  15. #include "soc/soc_caps.h"
  16. #include "esp_private/esp_clk.h"
  17. #include "freertos/FreeRTOS.h"
  18. #include "hal/systimer_ll.h"
  19. #include "hal/systimer_types.h"
  20. #include "hal/systimer_hal.h"
  21. /**
  22. * @file esp_timer_systimer.c
  23. * @brief Implementation of esp_timer using systimer.
  24. *
  25. * This timer is a 64-bit up-counting timer, with a programmable compare value (called 'alarm' hereafter).
  26. * When the timer reaches compare value, interrupt is raised.
  27. * The timer can be configured to produce an edge interrupt.
  28. *
  29. * @note systimer counter0 and alarm2 are adopted to implemented esp_timer
  30. */
  31. static const char *TAG = "esp_timer_systimer";
  32. /* Interrupt handle returned by the interrupt allocator */
  33. static intr_handle_t s_timer_interrupt_handle;
  34. /* Function from the upper layer to be called when the interrupt happens.
  35. * Registered in esp_timer_impl_init.
  36. */
  37. static intr_handler_t s_alarm_handler = NULL;
  38. /* Systimer HAL layer object */
  39. static systimer_hal_context_t systimer_hal;
  40. /* Spinlock used to protect access to the hardware registers. */
  41. portMUX_TYPE s_time_update_lock = portMUX_INITIALIZER_UNLOCKED;
  42. void esp_timer_impl_lock(void)
  43. {
  44. portENTER_CRITICAL(&s_time_update_lock);
  45. }
  46. void esp_timer_impl_unlock(void)
  47. {
  48. portEXIT_CRITICAL(&s_time_update_lock);
  49. }
  50. uint64_t IRAM_ATTR esp_timer_impl_get_counter_reg(void)
  51. {
  52. return systimer_hal_get_counter_value(&systimer_hal, SYSTIMER_LL_COUNTER_CLOCK);
  53. }
  54. int64_t IRAM_ATTR esp_timer_impl_get_time(void)
  55. {
  56. return systimer_hal_get_counter_value(&systimer_hal, SYSTIMER_LL_COUNTER_CLOCK) / SYSTIMER_LL_TICKS_PER_US;
  57. }
  58. int64_t esp_timer_get_time(void) __attribute__((alias("esp_timer_impl_get_time")));
  59. void IRAM_ATTR esp_timer_impl_set_alarm_id(uint64_t timestamp, unsigned alarm_id)
  60. {
  61. static uint64_t timestamp_id[2] = { UINT64_MAX, UINT64_MAX };
  62. portENTER_CRITICAL_SAFE(&s_time_update_lock);
  63. timestamp_id[alarm_id] = timestamp;
  64. timestamp = MIN(timestamp_id[0], timestamp_id[1]);
  65. if (timestamp != UINT64_MAX) {
  66. systimer_hal_set_alarm_target(&systimer_hal, SYSTIMER_LL_ALARM_CLOCK, timestamp);
  67. }
  68. portEXIT_CRITICAL_SAFE(&s_time_update_lock);
  69. }
  70. void IRAM_ATTR esp_timer_impl_set_alarm(uint64_t timestamp)
  71. {
  72. esp_timer_impl_set_alarm_id(timestamp, 0);
  73. }
  74. static void IRAM_ATTR timer_alarm_isr(void *arg)
  75. {
  76. // clear the interrupt
  77. systimer_ll_clear_alarm_int(systimer_hal.dev, SYSTIMER_LL_ALARM_CLOCK);
  78. /* Call the upper layer handler */
  79. (*s_alarm_handler)(arg);
  80. }
  81. void IRAM_ATTR esp_timer_impl_update_apb_freq(uint32_t apb_ticks_per_us)
  82. {
  83. #if !SOC_SYSTIMER_FIXED_TICKS_US
  84. systimer_hal_on_apb_freq_update(&systimer_hal, apb_ticks_per_us);
  85. #endif
  86. }
  87. void esp_timer_impl_advance(int64_t time_us)
  88. {
  89. portENTER_CRITICAL_SAFE(&s_time_update_lock);
  90. systimer_hal_counter_value_advance(&systimer_hal, SYSTIMER_LL_COUNTER_CLOCK, time_us);
  91. portEXIT_CRITICAL_SAFE(&s_time_update_lock);
  92. }
  93. esp_err_t esp_timer_impl_early_init(void)
  94. {
  95. systimer_hal_init(&systimer_hal);
  96. #if !SOC_SYSTIMER_FIXED_TICKS_US
  97. assert(esp_clk_xtal_freq() == (40 * 1000000) &&
  98. "update the step for xtal to support other XTAL:APB frequency ratios");
  99. systimer_hal_set_steps_per_tick(&systimer_hal, 0, 2); // for xtal
  100. systimer_hal_set_steps_per_tick(&systimer_hal, 1, 1); // for pll
  101. #endif
  102. systimer_hal_enable_counter(&systimer_hal, SYSTIMER_LL_COUNTER_CLOCK);
  103. systimer_hal_select_alarm_mode(&systimer_hal, SYSTIMER_LL_ALARM_CLOCK, SYSTIMER_ALARM_MODE_ONESHOT);
  104. systimer_hal_connect_alarm_counter(&systimer_hal, SYSTIMER_LL_ALARM_CLOCK, SYSTIMER_LL_COUNTER_CLOCK);
  105. return ESP_OK;
  106. }
  107. esp_err_t esp_timer_impl_init(intr_handler_t alarm_handler)
  108. {
  109. s_alarm_handler = alarm_handler;
  110. const int interrupt_lvl = (1 << CONFIG_ESP_TIMER_INTERRUPT_LEVEL) & ESP_INTR_FLAG_LEVELMASK;
  111. #if SOC_SYSTIMER_INT_LEVEL
  112. int int_type = 0;
  113. #else
  114. int int_type = ESP_INTR_FLAG_EDGE;
  115. #endif // SOC_SYSTIMER_INT_LEVEL
  116. esp_err_t err = esp_intr_alloc(ETS_SYSTIMER_TARGET2_EDGE_INTR_SOURCE,
  117. ESP_INTR_FLAG_INTRDISABLED | ESP_INTR_FLAG_IRAM | int_type | interrupt_lvl,
  118. &timer_alarm_isr, NULL, &s_timer_interrupt_handle);
  119. if (err != ESP_OK) {
  120. ESP_EARLY_LOGE(TAG, "esp_intr_alloc failed (0x%x)", err);
  121. goto err_intr_alloc;
  122. }
  123. /* TODO: if SYSTIMER is used for anything else, access to SYSTIMER_INT_ENA_REG has to be
  124. * protected by a shared spinlock. Since this code runs as part of early startup, this
  125. * is practically not an issue.
  126. */
  127. systimer_hal_enable_alarm_int(&systimer_hal, SYSTIMER_LL_ALARM_CLOCK);
  128. err = esp_intr_enable(s_timer_interrupt_handle);
  129. if (err != ESP_OK) {
  130. ESP_EARLY_LOGE(TAG, "esp_intr_enable failed (0x%x)", err);
  131. goto err_intr_en;
  132. }
  133. return ESP_OK;
  134. err_intr_en:
  135. systimer_ll_enable_alarm(systimer_hal.dev, SYSTIMER_LL_ALARM_CLOCK, false);
  136. /* TODO: may need a spinlock, see the note related to SYSTIMER_INT_ENA_REG in systimer_hal_init */
  137. systimer_ll_enable_alarm_int(systimer_hal.dev, SYSTIMER_LL_ALARM_CLOCK, false);
  138. esp_intr_free(s_timer_interrupt_handle);
  139. err_intr_alloc:
  140. s_alarm_handler = NULL;
  141. return err;
  142. }
  143. void esp_timer_impl_deinit(void)
  144. {
  145. esp_intr_disable(s_timer_interrupt_handle);
  146. systimer_ll_enable_alarm(systimer_hal.dev, SYSTIMER_LL_ALARM_CLOCK, false);
  147. /* TODO: may need a spinlock, see the note related to SYSTIMER_INT_ENA_REG in systimer_hal_init */
  148. systimer_ll_enable_alarm_int(systimer_hal.dev, SYSTIMER_LL_ALARM_CLOCK, false);
  149. esp_intr_free(s_timer_interrupt_handle);
  150. s_timer_interrupt_handle = NULL;
  151. s_alarm_handler = NULL;
  152. }
  153. uint64_t IRAM_ATTR esp_timer_impl_get_min_period_us(void)
  154. {
  155. return 50;
  156. }
  157. uint64_t esp_timer_impl_get_alarm_reg(void)
  158. {
  159. portENTER_CRITICAL_SAFE(&s_time_update_lock);
  160. uint64_t val = systimer_hal_get_alarm_value(&systimer_hal, SYSTIMER_LL_ALARM_CLOCK);
  161. portEXIT_CRITICAL_SAFE(&s_time_update_lock);
  162. return val;
  163. }
  164. void esp_timer_private_update_apb_freq(uint32_t apb_ticks_per_us) __attribute__((alias("esp_timer_impl_update_apb_freq")));
  165. void esp_timer_private_advance(int64_t time_us) __attribute__((alias("esp_timer_impl_advance")));
  166. void esp_timer_private_lock(void) __attribute__((alias("esp_timer_impl_lock")));
  167. void esp_timer_private_unlock(void) __attribute__((alias("esp_timer_impl_unlock")));