rtc_module.c 5.5 KB

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