esp_timer_impl_systimer.c 5.7 KB

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