rtc_module.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * SPDX-FileCopyrightText: 2016-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdlib.h>
  7. #include <ctype.h>
  8. #include "sdkconfig.h"
  9. #include "esp_types.h"
  10. #include "esp_log.h"
  11. #include "soc/rtc_periph.h"
  12. #include "soc/rtc.h"
  13. #include "soc/periph_defs.h"
  14. #include "freertos/FreeRTOS.h"
  15. #include "freertos/semphr.h"
  16. #include "freertos/timers.h"
  17. #include "esp_intr_alloc.h"
  18. #include "sys/lock.h"
  19. #include "esp_private/rtc_ctrl.h"
  20. #include "esp_attr.h"
  21. #ifndef NDEBUG
  22. // Enable built-in checks in queue.h in debug builds
  23. #define INVARIANTS
  24. #endif
  25. #include "sys/queue.h"
  26. #if CONFIG_IDF_TARGET_ESP32C6 || CONFIG_IDF_TARGET_ESP32H2 || CONFIG_IDF_TARGET_ESP32P4// TODO: IDF-8008
  27. static const char *TAG = "rtc_module";
  28. #endif
  29. // rtc_spinlock is used by other peripheral drivers
  30. portMUX_TYPE rtc_spinlock = portMUX_INITIALIZER_UNLOCKED;
  31. #if !CONFIG_IDF_TARGET_ESP32C6 && !CONFIG_IDF_TARGET_ESP32H2 && !CONFIG_IDF_TARGET_ESP32P4 // TODO: IDF-8008
  32. #define NOT_REGISTERED (-1)
  33. // Disable the interrupt which cannot work without cache.
  34. static DRAM_ATTR uint32_t rtc_intr_cache;
  35. static DRAM_ATTR uint32_t rtc_intr_enabled;
  36. static DRAM_ATTR int rtc_isr_cpu = NOT_REGISTERED; // Unused number
  37. static void s_rtc_isr_noniram_hook(uint32_t rtc_intr_mask);
  38. static void s_rtc_isr_noniram_hook_relieve(uint32_t rtc_intr_mask);
  39. /*---------------------------------------------------------------
  40. INTERRUPT HANDLER
  41. ---------------------------------------------------------------*/
  42. typedef struct rtc_isr_handler_ {
  43. uint32_t mask;
  44. intr_handler_t handler;
  45. void* handler_arg;
  46. uint32_t flags;
  47. SLIST_ENTRY(rtc_isr_handler_) next;
  48. } rtc_isr_handler_t;
  49. static DRAM_ATTR SLIST_HEAD(rtc_isr_handler_list_, rtc_isr_handler_) s_rtc_isr_handler_list =
  50. SLIST_HEAD_INITIALIZER(s_rtc_isr_handler_list);
  51. static DRAM_ATTR portMUX_TYPE s_rtc_isr_handler_list_lock = portMUX_INITIALIZER_UNLOCKED;
  52. static intr_handle_t s_rtc_isr_handle;
  53. IRAM_ATTR static void rtc_isr(void* arg)
  54. {
  55. uint32_t status = REG_READ(RTC_CNTL_INT_ST_REG);
  56. rtc_isr_handler_t* it;
  57. portENTER_CRITICAL_ISR(&s_rtc_isr_handler_list_lock);
  58. SLIST_FOREACH(it, &s_rtc_isr_handler_list, next) {
  59. if (it->mask & status) {
  60. portEXIT_CRITICAL_ISR(&s_rtc_isr_handler_list_lock);
  61. (*it->handler)(it->handler_arg);
  62. portENTER_CRITICAL_ISR(&s_rtc_isr_handler_list_lock);
  63. }
  64. }
  65. portEXIT_CRITICAL_ISR(&s_rtc_isr_handler_list_lock);
  66. REG_WRITE(RTC_CNTL_INT_CLR_REG, status);
  67. }
  68. static esp_err_t rtc_isr_ensure_installed(void)
  69. {
  70. esp_err_t err = ESP_OK;
  71. portENTER_CRITICAL(&s_rtc_isr_handler_list_lock);
  72. if (s_rtc_isr_handle) {
  73. goto out;
  74. }
  75. REG_WRITE(RTC_CNTL_INT_ENA_REG, 0);
  76. REG_WRITE(RTC_CNTL_INT_CLR_REG, UINT32_MAX);
  77. err = esp_intr_alloc(ETS_RTC_CORE_INTR_SOURCE, ESP_INTR_FLAG_IRAM, &rtc_isr, NULL, &s_rtc_isr_handle);
  78. if (err != ESP_OK) {
  79. goto out;
  80. }
  81. rtc_isr_cpu = esp_intr_get_cpu(s_rtc_isr_handle);
  82. out:
  83. portEXIT_CRITICAL(&s_rtc_isr_handler_list_lock);
  84. return err;
  85. }
  86. #endif // !CONFIG_IDF_TARGET_ESP32C6 TODO: IDF-8008
  87. esp_err_t rtc_isr_register(intr_handler_t handler, void* handler_arg, uint32_t rtc_intr_mask, uint32_t flags)
  88. {
  89. #if CONFIG_IDF_TARGET_ESP32C6 || CONFIG_IDF_TARGET_ESP32H2 || CONFIG_IDF_TARGET_ESP32P4 // TODO: IDF-8008
  90. ESP_EARLY_LOGW(TAG, "rtc_isr_register() has not been implemented yet");
  91. return ESP_OK;
  92. #else
  93. esp_err_t err = rtc_isr_ensure_installed();
  94. if (err != ESP_OK) {
  95. return err;
  96. }
  97. rtc_isr_handler_t* item = heap_caps_malloc(sizeof(*item), MALLOC_CAP_INTERNAL);
  98. if (item == NULL) {
  99. return ESP_ERR_NO_MEM;
  100. }
  101. item->handler = handler;
  102. item->handler_arg = handler_arg;
  103. item->mask = rtc_intr_mask;
  104. item->flags = flags;
  105. portENTER_CRITICAL(&s_rtc_isr_handler_list_lock);
  106. if (flags & RTC_INTR_FLAG_IRAM) {
  107. s_rtc_isr_noniram_hook(rtc_intr_mask);
  108. } else {
  109. s_rtc_isr_noniram_hook_relieve(rtc_intr_mask);
  110. }
  111. SLIST_INSERT_HEAD(&s_rtc_isr_handler_list, item, next);
  112. portEXIT_CRITICAL(&s_rtc_isr_handler_list_lock);
  113. return ESP_OK;
  114. #endif
  115. }
  116. esp_err_t rtc_isr_deregister(intr_handler_t handler, void* handler_arg)
  117. {
  118. #if CONFIG_IDF_TARGET_ESP32C6 || CONFIG_IDF_TARGET_ESP32H2 || CONFIG_IDF_TARGET_ESP32P4 // TODO: IDF-8008
  119. ESP_EARLY_LOGW(TAG, "rtc_isr_deregister() has not been implemented yet");
  120. return ESP_OK;
  121. #else
  122. rtc_isr_handler_t* it;
  123. rtc_isr_handler_t* prev = NULL;
  124. bool found = false;
  125. portENTER_CRITICAL(&s_rtc_isr_handler_list_lock);
  126. SLIST_FOREACH(it, &s_rtc_isr_handler_list, next) {
  127. if (it->handler == handler && it->handler_arg == handler_arg) {
  128. if (it == SLIST_FIRST(&s_rtc_isr_handler_list)) {
  129. SLIST_REMOVE_HEAD(&s_rtc_isr_handler_list, next);
  130. } else {
  131. SLIST_REMOVE_AFTER(prev, next);
  132. }
  133. found = true;
  134. if (it->flags & RTC_INTR_FLAG_IRAM) {
  135. s_rtc_isr_noniram_hook_relieve(it->mask);
  136. }
  137. free(it);
  138. break;
  139. }
  140. prev = it;
  141. }
  142. portEXIT_CRITICAL(&s_rtc_isr_handler_list_lock);
  143. return found ? ESP_OK : ESP_ERR_INVALID_STATE;
  144. #endif
  145. }
  146. #if !CONFIG_IDF_TARGET_ESP32C6 && !CONFIG_IDF_TARGET_ESP32H2 && !CONFIG_IDF_TARGET_ESP32P4 // TODO: IDF-8008
  147. /**
  148. * @brief This helper function can be used to avoid the interrupt to be triggered with cache disabled.
  149. * There are lots of different signals on RTC module (i.e. sleep_wakeup, wdt, brownout_detect, etc.)
  150. * We might want some of them can be triggered with cache disabled, some are not. Therefore, this function
  151. * is created to avoid those which do not want to be triggered with cache disabled.
  152. *
  153. * @param rtc_intr_mask the mask of the rtc interrupt.
  154. */
  155. static void s_rtc_isr_noniram_hook(uint32_t rtc_intr_mask)
  156. {
  157. rtc_intr_cache |= rtc_intr_mask;
  158. }
  159. static void s_rtc_isr_noniram_hook_relieve(uint32_t rtc_intr_mask)
  160. {
  161. rtc_intr_cache &= ~rtc_intr_mask;
  162. }
  163. #endif
  164. IRAM_ATTR void rtc_isr_noniram_disable(uint32_t cpu)
  165. {
  166. #if !CONFIG_IDF_TARGET_ESP32C6 && !CONFIG_IDF_TARGET_ESP32H2 && !CONFIG_IDF_TARGET_ESP32P4 // TODO: IDF-8008
  167. if (rtc_isr_cpu == cpu) {
  168. rtc_intr_enabled |= RTCCNTL.int_ena.val;
  169. RTCCNTL.int_ena.val &= rtc_intr_cache;
  170. }
  171. #endif
  172. }
  173. IRAM_ATTR void rtc_isr_noniram_enable(uint32_t cpu)
  174. {
  175. #if !CONFIG_IDF_TARGET_ESP32C6 && !CONFIG_IDF_TARGET_ESP32H2 && !CONFIG_IDF_TARGET_ESP32P4 // TODO: IDF-8008
  176. if (rtc_isr_cpu == cpu) {
  177. RTCCNTL.int_ena.val = rtc_intr_enabled;
  178. rtc_intr_enabled = 0;
  179. }
  180. #endif
  181. }