rtc_module.c 3.6 KB

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