rtc_module.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright 2016-2018 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_types.h>
  15. #include <stdlib.h>
  16. #include <ctype.h>
  17. #include "esp_log.h"
  18. #include "soc/rtc_periph.h"
  19. #include "soc/sens_periph.h"
  20. #include "soc/syscon_periph.h"
  21. #include "soc/rtc.h"
  22. #include "soc/periph_defs.h"
  23. #include "freertos/FreeRTOS.h"
  24. #include "freertos/xtensa_api.h"
  25. #include "freertos/semphr.h"
  26. #include "freertos/timers.h"
  27. #include "esp_intr_alloc.h"
  28. #include "sys/lock.h"
  29. #include "driver/rtc_cntl.h"
  30. #include "sdkconfig.h"
  31. #if CONFIG_IDF_TARGET_ESP32
  32. #include "esp32/rom/ets_sys.h"
  33. #elif CONFIG_IDF_TARGET_ESP32S2
  34. #include "esp32s2/rom/ets_sys.h"
  35. #endif
  36. #ifndef NDEBUG
  37. // Enable built-in checks in queue.h in debug builds
  38. #define INVARIANTS
  39. #endif
  40. #include "sys/queue.h"
  41. portMUX_TYPE rtc_spinlock = portMUX_INITIALIZER_UNLOCKED;
  42. /*---------------------------------------------------------------
  43. INTERRUPT HANDLER
  44. ---------------------------------------------------------------*/
  45. typedef struct rtc_isr_handler_ {
  46. uint32_t mask;
  47. intr_handler_t handler;
  48. void* handler_arg;
  49. SLIST_ENTRY(rtc_isr_handler_) next;
  50. } rtc_isr_handler_t;
  51. static SLIST_HEAD(rtc_isr_handler_list_, rtc_isr_handler_) s_rtc_isr_handler_list =
  52. SLIST_HEAD_INITIALIZER(s_rtc_isr_handler_list);
  53. portMUX_TYPE s_rtc_isr_handler_list_lock = portMUX_INITIALIZER_UNLOCKED;
  54. static intr_handle_t s_rtc_isr_handle;
  55. static void rtc_isr(void* arg)
  56. {
  57. uint32_t status = REG_READ(RTC_CNTL_INT_ST_REG);
  58. rtc_isr_handler_t* it;
  59. portENTER_CRITICAL_ISR(&s_rtc_isr_handler_list_lock);
  60. SLIST_FOREACH(it, &s_rtc_isr_handler_list, next) {
  61. if (it->mask & status) {
  62. portEXIT_CRITICAL_ISR(&s_rtc_isr_handler_list_lock);
  63. (*it->handler)(it->handler_arg);
  64. portENTER_CRITICAL_ISR(&s_rtc_isr_handler_list_lock);
  65. }
  66. }
  67. portEXIT_CRITICAL_ISR(&s_rtc_isr_handler_list_lock);
  68. REG_WRITE(RTC_CNTL_INT_CLR_REG, status);
  69. }
  70. static esp_err_t rtc_isr_ensure_installed(void)
  71. {
  72. esp_err_t err = ESP_OK;
  73. portENTER_CRITICAL(&s_rtc_isr_handler_list_lock);
  74. if (s_rtc_isr_handle) {
  75. goto out;
  76. }
  77. REG_WRITE(RTC_CNTL_INT_ENA_REG, 0);
  78. REG_WRITE(RTC_CNTL_INT_CLR_REG, UINT32_MAX);
  79. err = esp_intr_alloc(ETS_RTC_CORE_INTR_SOURCE, 0, &rtc_isr, NULL, &s_rtc_isr_handle);
  80. if (err != ESP_OK) {
  81. goto out;
  82. }
  83. out:
  84. portEXIT_CRITICAL(&s_rtc_isr_handler_list_lock);
  85. return err;
  86. }
  87. esp_err_t rtc_isr_register(intr_handler_t handler, void* handler_arg, uint32_t rtc_intr_mask)
  88. {
  89. esp_err_t err = rtc_isr_ensure_installed();
  90. if (err != ESP_OK) {
  91. return err;
  92. }
  93. rtc_isr_handler_t* item = malloc(sizeof(*item));
  94. if (item == NULL) {
  95. return ESP_ERR_NO_MEM;
  96. }
  97. item->handler = handler;
  98. item->handler_arg = handler_arg;
  99. item->mask = rtc_intr_mask;
  100. portENTER_CRITICAL(&s_rtc_isr_handler_list_lock);
  101. SLIST_INSERT_HEAD(&s_rtc_isr_handler_list, item, next);
  102. portEXIT_CRITICAL(&s_rtc_isr_handler_list_lock);
  103. return ESP_OK;
  104. }
  105. esp_err_t rtc_isr_deregister(intr_handler_t handler, void* handler_arg)
  106. {
  107. rtc_isr_handler_t* it;
  108. rtc_isr_handler_t* prev = NULL;
  109. bool found = false;
  110. portENTER_CRITICAL(&s_rtc_isr_handler_list_lock);
  111. SLIST_FOREACH(it, &s_rtc_isr_handler_list, next) {
  112. if (it->handler == handler && it->handler_arg == handler_arg) {
  113. if (it == SLIST_FIRST(&s_rtc_isr_handler_list)) {
  114. SLIST_REMOVE_HEAD(&s_rtc_isr_handler_list, next);
  115. } else {
  116. SLIST_REMOVE_AFTER(prev, next);
  117. }
  118. found = true;
  119. free(it);
  120. break;
  121. }
  122. prev = it;
  123. }
  124. portEXIT_CRITICAL(&s_rtc_isr_handler_list_lock);
  125. return found ? ESP_OK : ESP_ERR_INVALID_STATE;
  126. }