esp_timer_impl_systimer.c 7.1 KB

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