rtc_module.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 <stdlib.h>
  15. #include <ctype.h>
  16. #include "sdkconfig.h"
  17. #include "esp_types.h"
  18. #include "esp_log.h"
  19. #include "soc/rtc_periph.h"
  20. #include "soc/sens_periph.h"
  21. #include "soc/syscon_periph.h"
  22. #include "soc/rtc.h"
  23. #include "soc/periph_defs.h"
  24. #include "freertos/FreeRTOS.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. #ifndef NDEBUG
  31. // Enable built-in checks in queue.h in debug builds
  32. #define INVARIANTS
  33. #endif
  34. #include "sys/queue.h"
  35. portMUX_TYPE rtc_spinlock = portMUX_INITIALIZER_UNLOCKED;
  36. /*---------------------------------------------------------------
  37. INTERRUPT HANDLER
  38. ---------------------------------------------------------------*/
  39. typedef struct rtc_isr_handler_ {
  40. uint32_t mask;
  41. intr_handler_t handler;
  42. void* handler_arg;
  43. SLIST_ENTRY(rtc_isr_handler_) next;
  44. } rtc_isr_handler_t;
  45. static SLIST_HEAD(rtc_isr_handler_list_, rtc_isr_handler_) s_rtc_isr_handler_list =
  46. SLIST_HEAD_INITIALIZER(s_rtc_isr_handler_list);
  47. portMUX_TYPE s_rtc_isr_handler_list_lock = portMUX_INITIALIZER_UNLOCKED;
  48. static intr_handle_t s_rtc_isr_handle;
  49. 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, 0, &rtc_isr, NULL, &s_rtc_isr_handle);
  74. if (err != ESP_OK) {
  75. goto out;
  76. }
  77. out:
  78. portEXIT_CRITICAL(&s_rtc_isr_handler_list_lock);
  79. return err;
  80. }
  81. esp_err_t rtc_isr_register(intr_handler_t handler, void* handler_arg, uint32_t rtc_intr_mask)
  82. {
  83. esp_err_t err = rtc_isr_ensure_installed();
  84. if (err != ESP_OK) {
  85. return err;
  86. }
  87. rtc_isr_handler_t* item = malloc(sizeof(*item));
  88. if (item == NULL) {
  89. return ESP_ERR_NO_MEM;
  90. }
  91. item->handler = handler;
  92. item->handler_arg = handler_arg;
  93. item->mask = rtc_intr_mask;
  94. portENTER_CRITICAL(&s_rtc_isr_handler_list_lock);
  95. SLIST_INSERT_HEAD(&s_rtc_isr_handler_list, item, next);
  96. portEXIT_CRITICAL(&s_rtc_isr_handler_list_lock);
  97. return ESP_OK;
  98. }
  99. esp_err_t rtc_isr_deregister(intr_handler_t handler, void* handler_arg)
  100. {
  101. rtc_isr_handler_t* it;
  102. rtc_isr_handler_t* prev = NULL;
  103. bool found = false;
  104. portENTER_CRITICAL(&s_rtc_isr_handler_list_lock);
  105. SLIST_FOREACH(it, &s_rtc_isr_handler_list, next) {
  106. if (it->handler == handler && it->handler_arg == handler_arg) {
  107. if (it == SLIST_FIRST(&s_rtc_isr_handler_list)) {
  108. SLIST_REMOVE_HEAD(&s_rtc_isr_handler_list, next);
  109. } else {
  110. SLIST_REMOVE_AFTER(prev, next);
  111. }
  112. found = true;
  113. free(it);
  114. break;
  115. }
  116. prev = it;
  117. }
  118. portEXIT_CRITICAL(&s_rtc_isr_handler_list_lock);
  119. return found ? ESP_OK : ESP_ERR_INVALID_STATE;
  120. }