rtc_module.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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/xtensa_api.h"
  26. #include "freertos/semphr.h"
  27. #include "freertos/timers.h"
  28. #include "esp_intr_alloc.h"
  29. #include "sys/lock.h"
  30. #include "driver/rtc_cntl.h"
  31. #ifndef NDEBUG
  32. // Enable built-in checks in queue.h in debug builds
  33. #define INVARIANTS
  34. #endif
  35. #include "sys/queue.h"
  36. portMUX_TYPE rtc_spinlock = portMUX_INITIALIZER_UNLOCKED;
  37. /*---------------------------------------------------------------
  38. INTERRUPT HANDLER
  39. ---------------------------------------------------------------*/
  40. typedef struct rtc_isr_handler_ {
  41. uint32_t mask;
  42. intr_handler_t handler;
  43. void* handler_arg;
  44. SLIST_ENTRY(rtc_isr_handler_) next;
  45. } rtc_isr_handler_t;
  46. static SLIST_HEAD(rtc_isr_handler_list_, rtc_isr_handler_) s_rtc_isr_handler_list =
  47. SLIST_HEAD_INITIALIZER(s_rtc_isr_handler_list);
  48. portMUX_TYPE s_rtc_isr_handler_list_lock = portMUX_INITIALIZER_UNLOCKED;
  49. static intr_handle_t s_rtc_isr_handle;
  50. static void rtc_isr(void* arg)
  51. {
  52. uint32_t status = REG_READ(RTC_CNTL_INT_ST_REG);
  53. rtc_isr_handler_t* it;
  54. portENTER_CRITICAL_ISR(&s_rtc_isr_handler_list_lock);
  55. SLIST_FOREACH(it, &s_rtc_isr_handler_list, next) {
  56. if (it->mask & status) {
  57. portEXIT_CRITICAL_ISR(&s_rtc_isr_handler_list_lock);
  58. (*it->handler)(it->handler_arg);
  59. portENTER_CRITICAL_ISR(&s_rtc_isr_handler_list_lock);
  60. }
  61. }
  62. portEXIT_CRITICAL_ISR(&s_rtc_isr_handler_list_lock);
  63. REG_WRITE(RTC_CNTL_INT_CLR_REG, status);
  64. }
  65. static esp_err_t rtc_isr_ensure_installed(void)
  66. {
  67. esp_err_t err = ESP_OK;
  68. portENTER_CRITICAL(&s_rtc_isr_handler_list_lock);
  69. if (s_rtc_isr_handle) {
  70. goto out;
  71. }
  72. REG_WRITE(RTC_CNTL_INT_ENA_REG, 0);
  73. REG_WRITE(RTC_CNTL_INT_CLR_REG, UINT32_MAX);
  74. err = esp_intr_alloc(ETS_RTC_CORE_INTR_SOURCE, 0, &rtc_isr, NULL, &s_rtc_isr_handle);
  75. if (err != ESP_OK) {
  76. goto out;
  77. }
  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)
  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 = malloc(sizeof(*item));
  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. portENTER_CRITICAL(&s_rtc_isr_handler_list_lock);
  96. SLIST_INSERT_HEAD(&s_rtc_isr_handler_list, item, next);
  97. portEXIT_CRITICAL(&s_rtc_isr_handler_list_lock);
  98. return ESP_OK;
  99. }
  100. esp_err_t rtc_isr_deregister(intr_handler_t handler, void* handler_arg)
  101. {
  102. rtc_isr_handler_t* it;
  103. rtc_isr_handler_t* prev = NULL;
  104. bool found = false;
  105. portENTER_CRITICAL(&s_rtc_isr_handler_list_lock);
  106. SLIST_FOREACH(it, &s_rtc_isr_handler_list, next) {
  107. if (it->handler == handler && it->handler_arg == handler_arg) {
  108. if (it == SLIST_FIRST(&s_rtc_isr_handler_list)) {
  109. SLIST_REMOVE_HEAD(&s_rtc_isr_handler_list, next);
  110. } else {
  111. SLIST_REMOVE_AFTER(prev, next);
  112. }
  113. found = true;
  114. free(it);
  115. break;
  116. }
  117. prev = it;
  118. }
  119. portEXIT_CRITICAL(&s_rtc_isr_handler_list_lock);
  120. return found ? ESP_OK : ESP_ERR_INVALID_STATE;
  121. }