rtc_module.c 4.1 KB

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