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
15 #include <stdlib.h>
16 #include <ctype.h>
17 #include "sdkconfig.h"
18 #include "esp_types.h"
19 #include "esp_log.h"
20 #include "soc/rtc_periph.h"
21 #include "soc/syscon_periph.h"
22 #include "soc/rtc.h"
23 #include "soc/periph_defs.h"
24 #include "esp_osal/esp_osal.h"
25 #include "esp_osal/semphr.h"
26 #include "esp_osal/timers.h"
27 #include "esp_intr_alloc.h"
28 #include "sys/lock.h"
29 #include "driver/rtc_cntl.h"
30
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
37 portMUX_TYPE rtc_spinlock = portMUX_INITIALIZER_UNLOCKED;
38
39 /*---------------------------------------------------------------
40 INTERRUPT HANDLER
41 ---------------------------------------------------------------*/
42
43
44 typedef struct rtc_isr_handler_ {
45 uint32_t mask;
46 intr_handler_t handler;
47 void* handler_arg;
48 SLIST_ENTRY(rtc_isr_handler_) next;
49 } rtc_isr_handler_t;
50
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
rtc_isr(void * arg)56 static void rtc_isr(void* arg)
57 {
58 uint32_t status = REG_READ(RTC_CNTL_INT_ST_REG);
59 rtc_isr_handler_t* it;
60 portENTER_CRITICAL_ISR(&s_rtc_isr_handler_list_lock);
61 SLIST_FOREACH(it, &s_rtc_isr_handler_list, next) {
62 if (it->mask & status) {
63 portEXIT_CRITICAL_ISR(&s_rtc_isr_handler_list_lock);
64 (*it->handler)(it->handler_arg);
65 portENTER_CRITICAL_ISR(&s_rtc_isr_handler_list_lock);
66 }
67 }
68 portEXIT_CRITICAL_ISR(&s_rtc_isr_handler_list_lock);
69 REG_WRITE(RTC_CNTL_INT_CLR_REG, status);
70 }
71
rtc_isr_ensure_installed(void)72 static esp_err_t rtc_isr_ensure_installed(void)
73 {
74 esp_err_t err = ESP_OK;
75 portENTER_CRITICAL(&s_rtc_isr_handler_list_lock);
76 if (s_rtc_isr_handle) {
77 goto out;
78 }
79
80 REG_WRITE(RTC_CNTL_INT_ENA_REG, 0);
81 REG_WRITE(RTC_CNTL_INT_CLR_REG, UINT32_MAX);
82 err = esp_intr_alloc(ETS_RTC_CORE_INTR_SOURCE, 0, &rtc_isr, NULL, &s_rtc_isr_handle);
83 if (err != ESP_OK) {
84 goto out;
85 }
86
87 out:
88 portEXIT_CRITICAL(&s_rtc_isr_handler_list_lock);
89 return err;
90 }
91
92
rtc_isr_register(intr_handler_t handler,void * handler_arg,uint32_t rtc_intr_mask)93 esp_err_t rtc_isr_register(intr_handler_t handler, void* handler_arg, uint32_t rtc_intr_mask)
94 {
95 esp_err_t err = rtc_isr_ensure_installed();
96 if (err != ESP_OK) {
97 return err;
98 }
99
100 rtc_isr_handler_t* item = malloc(sizeof(*item));
101 if (item == NULL) {
102 return ESP_ERR_NO_MEM;
103 }
104 item->handler = handler;
105 item->handler_arg = handler_arg;
106 item->mask = rtc_intr_mask;
107 portENTER_CRITICAL(&s_rtc_isr_handler_list_lock);
108 SLIST_INSERT_HEAD(&s_rtc_isr_handler_list, item, next);
109 portEXIT_CRITICAL(&s_rtc_isr_handler_list_lock);
110 return ESP_OK;
111 }
112
rtc_isr_deregister(intr_handler_t handler,void * handler_arg)113 esp_err_t rtc_isr_deregister(intr_handler_t handler, void* handler_arg)
114 {
115 rtc_isr_handler_t* it;
116 rtc_isr_handler_t* prev = NULL;
117 bool found = false;
118 portENTER_CRITICAL(&s_rtc_isr_handler_list_lock);
119 SLIST_FOREACH(it, &s_rtc_isr_handler_list, next) {
120 if (it->handler == handler && it->handler_arg == handler_arg) {
121 if (it == SLIST_FIRST(&s_rtc_isr_handler_list)) {
122 SLIST_REMOVE_HEAD(&s_rtc_isr_handler_list, next);
123 } else {
124 SLIST_REMOVE_AFTER(prev, next);
125 }
126 found = true;
127 free(it);
128 break;
129 }
130 prev = it;
131 }
132 portEXIT_CRITICAL(&s_rtc_isr_handler_list_lock);
133 return found ? ESP_OK : ESP_ERR_INVALID_STATE;
134 }
135