1 // Copyright 2015-2019 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 /*******************************************************************************
16 * NOTICE
17 * The ll is not public api, don't use in application code.
18 * See readme.md in hal/include/hal/readme.md
19 ******************************************************************************/
20
21 #pragma once
22
23 #include <stdlib.h>
24 #include "soc/rtc_io_periph.h"
25 #include "hal/rtc_io_types.h"
26 #include "hal/gpio_types.h"
27
28 #define RTCIO_LL_PIN_FUNC 0
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34 typedef enum {
35 RTCIO_FUNC_RTC = 0x0, /*!< The pin controled by RTC module. */
36 RTCIO_FUNC_DIGITAL = 0x1, /*!< The pin controlled by DIGITAL module. */
37 } rtcio_ll_func_t;
38
39 typedef enum {
40 RTCIO_WAKEUP_DISABLE = 0, /*!< Disable GPIO interrupt */
41 RTCIO_WAKEUP_LOW_LEVEL = 0x4, /*!< GPIO interrupt type : input low level trigger */
42 RTCIO_WAKEUP_HIGH_LEVEL = 0x5, /*!< GPIO interrupt type : input high level trigger */
43 } rtcio_ll_wake_type_t;
44
45 typedef enum {
46 RTCIO_OUTPUT_NORMAL = 0, /*!< RTCIO output mode is normal. */
47 RTCIO_OUTPUT_OD = 0x1, /*!< RTCIO output mode is open-drain. */
48 } rtcio_ll_out_mode_t;
49
50 /**
51 * @brief Select the rtcio function.
52 *
53 * @note The RTC function must be selected before the pad analog function is enabled.
54 * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
55 * @param func Select pin function.
56 */
rtcio_ll_function_select(int rtcio_num,rtcio_ll_func_t func)57 static inline void rtcio_ll_function_select(int rtcio_num, rtcio_ll_func_t func)
58 {
59 if (func == RTCIO_FUNC_RTC) {
60 // 0: GPIO connected to digital GPIO module. 1: GPIO connected to analog RTC module.
61 SET_PERI_REG_MASK(rtc_io_desc[rtcio_num].reg, (rtc_io_desc[rtcio_num].mux));
62 //0:RTC FUNCTION 1,2,3:Reserved
63 SET_PERI_REG_BITS(rtc_io_desc[rtcio_num].reg, RTC_IO_TOUCH_PAD1_FUN_SEL_V, RTCIO_LL_PIN_FUNC, rtc_io_desc[rtcio_num].func);
64 } else if (func == RTCIO_FUNC_DIGITAL) {
65 CLEAR_PERI_REG_MASK(rtc_io_desc[rtcio_num].reg, (rtc_io_desc[rtcio_num].mux));
66 }
67 }
68
69 /**
70 * Enable rtcio output.
71 *
72 * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
73 */
rtcio_ll_output_enable(int rtcio_num)74 static inline void rtcio_ll_output_enable(int rtcio_num)
75 {
76 RTCIO.enable_w1ts.w1ts = (1U << rtcio_num);
77 }
78
79 /**
80 * Disable rtcio output.
81 *
82 * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
83 */
rtcio_ll_output_disable(int rtcio_num)84 static inline void rtcio_ll_output_disable(int rtcio_num)
85 {
86 RTCIO.enable_w1tc.w1tc = (1U << rtcio_num);
87 }
88
89 /**
90 * Set RTCIO output level.
91 *
92 * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
93 * @param level 0: output low; ~0: output high.
94 */
rtcio_ll_set_level(int rtcio_num,uint32_t level)95 static inline void rtcio_ll_set_level(int rtcio_num, uint32_t level)
96 {
97 if (level) {
98 RTCIO.out_w1ts.w1ts = (1U << rtcio_num);
99 } else {
100 RTCIO.out_w1tc.w1tc = (1U << rtcio_num);
101 }
102 }
103
104 /**
105 * Enable rtcio input.
106 *
107 * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
108 */
rtcio_ll_input_enable(int rtcio_num)109 static inline void rtcio_ll_input_enable(int rtcio_num)
110 {
111 SET_PERI_REG_MASK(rtc_io_desc[rtcio_num].reg, rtc_io_desc[rtcio_num].ie);
112 }
113
114 /**
115 * Disable rtcio input.
116 *
117 * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
118 */
rtcio_ll_input_disable(int rtcio_num)119 static inline void rtcio_ll_input_disable(int rtcio_num)
120 {
121 CLEAR_PERI_REG_MASK(rtc_io_desc[rtcio_num].reg, rtc_io_desc[rtcio_num].ie);
122 }
123
124 /**
125 * Get RTCIO input level.
126 *
127 * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
128 * @return 0: input low; ~0: input high.
129 */
rtcio_ll_get_level(int rtcio_num)130 static inline uint32_t rtcio_ll_get_level(int rtcio_num)
131 {
132 return (uint32_t)(RTCIO.in_val.in >> rtcio_num) & 0x1;
133 }
134
135 /**
136 * @brief Set RTC GPIO pad drive capability
137 *
138 * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
139 * @param strength Drive capability of the pad. Range: 0 ~ 3.
140 */
rtcio_ll_set_drive_capability(int rtcio_num,uint32_t strength)141 static inline void rtcio_ll_set_drive_capability(int rtcio_num, uint32_t strength)
142 {
143 if (rtc_io_desc[rtcio_num].drv_v) {
144 SET_PERI_REG_BITS(rtc_io_desc[rtcio_num].reg, rtc_io_desc[rtcio_num].drv_v, strength, rtc_io_desc[rtcio_num].drv_s);
145 }
146 }
147
148 /**
149 * @brief Get RTC GPIO pad drive capability.
150 *
151 * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
152 * @return Drive capability of the pad. Range: 0 ~ 3.
153 */
rtcio_ll_get_drive_capability(int rtcio_num)154 static inline uint32_t rtcio_ll_get_drive_capability(int rtcio_num)
155 {
156 return GET_PERI_REG_BITS2(rtc_io_desc[rtcio_num].reg, rtc_io_desc[rtcio_num].drv_v, rtc_io_desc[rtcio_num].drv_s);
157 }
158
159 /**
160 * @brief Set RTC GPIO pad output mode.
161 *
162 * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
163 * @return mode Output mode.
164 */
rtcio_ll_output_mode_set(int rtcio_num,rtcio_ll_out_mode_t mode)165 static inline void rtcio_ll_output_mode_set(int rtcio_num, rtcio_ll_out_mode_t mode)
166 {
167 RTCIO.pin[rtcio_num].pad_driver = mode;
168 }
169
170 /**
171 * RTC GPIO pullup enable.
172 *
173 * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
174 */
rtcio_ll_pullup_enable(int rtcio_num)175 static inline void rtcio_ll_pullup_enable(int rtcio_num)
176 {
177 if (rtc_io_desc[rtcio_num].pullup) {
178 SET_PERI_REG_MASK(rtc_io_desc[rtcio_num].reg, rtc_io_desc[rtcio_num].pullup);
179 }
180 }
181
182 /**
183 * RTC GPIO pullup disable.
184 *
185 * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
186 */
rtcio_ll_pullup_disable(int rtcio_num)187 static inline void rtcio_ll_pullup_disable(int rtcio_num)
188 {
189 if (rtc_io_desc[rtcio_num].pullup) {
190 CLEAR_PERI_REG_MASK(rtc_io_desc[rtcio_num].reg, rtc_io_desc[rtcio_num].pullup);
191 }
192 }
193
194 /**
195 * RTC GPIO pulldown enable.
196 *
197 * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
198 */
rtcio_ll_pulldown_enable(int rtcio_num)199 static inline void rtcio_ll_pulldown_enable(int rtcio_num)
200 {
201 if (rtc_io_desc[rtcio_num].pulldown) {
202 SET_PERI_REG_MASK(rtc_io_desc[rtcio_num].reg, rtc_io_desc[rtcio_num].pulldown);
203 }
204 }
205
206 /**
207 * RTC GPIO pulldown disable.
208 *
209 * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
210 */
rtcio_ll_pulldown_disable(int rtcio_num)211 static inline void rtcio_ll_pulldown_disable(int rtcio_num)
212 {
213 if (rtc_io_desc[rtcio_num].pulldown) {
214 CLEAR_PERI_REG_MASK(rtc_io_desc[rtcio_num].reg, rtc_io_desc[rtcio_num].pulldown);
215 }
216 }
217
218 /**
219 * Enable force hold function for RTC IO pad.
220 *
221 * Enabling HOLD function will cause the pad to lock current status, such as,
222 * input/output enable, input/output value, function, drive strength values.
223 * This function is useful when going into light or deep sleep mode to prevent
224 * the pin configuration from changing.
225 *
226 * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
227 */
rtcio_ll_force_hold_enable(int rtcio_num)228 static inline void rtcio_ll_force_hold_enable(int rtcio_num)
229 {
230 REG_SET_BIT(RTC_CNTL_HOLD_FORCE_REG, rtc_io_desc[rtcio_num].hold_force);
231 }
232
233 /**
234 * Disable hold function on an RTC IO pad
235 *
236 * @note If disable the pad hold, the status of pad maybe changed in sleep mode.
237 * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
238 */
rtcio_ll_force_hold_disable(int rtcio_num)239 static inline void rtcio_ll_force_hold_disable(int rtcio_num)
240 {
241 REG_CLR_BIT(RTC_CNTL_HOLD_FORCE_REG, rtc_io_desc[rtcio_num].hold_force);
242 REG_CLR_BIT(rtc_io_desc[rtcio_num].reg, rtc_io_desc[rtcio_num].hold);
243 }
244
245 /**
246 * Enable force hold function for RTC IO pad.
247 *
248 * Enabling HOLD function will cause the pad to lock current status, such as,
249 * input/output enable, input/output value, function, drive strength values.
250 * This function is useful when going into light or deep sleep mode to prevent
251 * the pin configuration from changing.
252 *
253 * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
254 */
rtcio_ll_force_hold_all(void)255 static inline void rtcio_ll_force_hold_all(void)
256 {
257 SET_PERI_REG_BITS(RTC_CNTL_HOLD_FORCE_REG, 0x3FFFF, 0x3FFFF, 0);
258 }
259
260 /**
261 * Disable hold function on an RTC IO pad
262 *
263 * @note If disable the pad hold, the status of pad maybe changed in sleep mode.
264 * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
265 */
rtcio_ll_force_unhold_all(void)266 static inline void rtcio_ll_force_unhold_all(void)
267 {
268 SET_PERI_REG_BITS(RTC_CNTL_HOLD_FORCE_REG, 0x3FFFF, 0, 0);
269 }
270
271 /**
272 * Enable wakeup function and set wakeup type from light sleep status for rtcio.
273 *
274 * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
275 * @param type Wakeup on high level or low level.
276 */
rtcio_ll_wakeup_enable(int rtcio_num,rtcio_ll_wake_type_t type)277 static inline void rtcio_ll_wakeup_enable(int rtcio_num, rtcio_ll_wake_type_t type)
278 {
279 RTCIO.pin[rtcio_num].wakeup_enable = 0x1;
280 RTCIO.pin[rtcio_num].int_type = type;
281 }
282
283 /**
284 * Disable wakeup function from light sleep status for rtcio.
285 *
286 * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
287 */
rtcio_ll_wakeup_disable(int rtcio_num)288 static inline void rtcio_ll_wakeup_disable(int rtcio_num)
289 {
290 RTCIO.pin[rtcio_num].wakeup_enable = 0;
291 RTCIO.pin[rtcio_num].int_type = RTCIO_WAKEUP_DISABLE;
292 }
293
294 /**
295 * Enable rtc io output in deep sleep.
296 *
297 * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
298 */
rtcio_ll_enable_output_in_sleep(gpio_num_t gpio_num)299 static inline void rtcio_ll_enable_output_in_sleep(gpio_num_t gpio_num)
300 {
301 if (rtc_io_desc[gpio_num].slpoe) {
302 SET_PERI_REG_MASK(rtc_io_desc[gpio_num].reg, rtc_io_desc[gpio_num].slpoe);
303 }
304 }
305
306 /**
307 * Disable rtc io output in deep sleep.
308 *
309 * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
310 */
rtcio_ll_in_sleep_disable_output(gpio_num_t gpio_num)311 static inline void rtcio_ll_in_sleep_disable_output(gpio_num_t gpio_num)
312 {
313 if (rtc_io_desc[gpio_num].slpoe) {
314 CLEAR_PERI_REG_MASK(rtc_io_desc[gpio_num].reg, rtc_io_desc[gpio_num].slpoe);
315 }
316 }
317
318 /**
319 * Enable rtc io input in deep sleep.
320 *
321 * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
322 */
rtcio_ll_in_sleep_enable_input(gpio_num_t gpio_num)323 static inline void rtcio_ll_in_sleep_enable_input(gpio_num_t gpio_num)
324 {
325 SET_PERI_REG_MASK(rtc_io_desc[gpio_num].reg, rtc_io_desc[gpio_num].slpie);
326 }
327
328 /**
329 * Disable rtc io input in deep sleep.
330 *
331 * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
332 */
rtcio_ll_in_sleep_disable_input(gpio_num_t gpio_num)333 static inline void rtcio_ll_in_sleep_disable_input(gpio_num_t gpio_num)
334 {
335 CLEAR_PERI_REG_MASK(rtc_io_desc[gpio_num].reg, rtc_io_desc[gpio_num].slpie);
336 }
337
338 /**
339 * Enable rtc io keep another setting in deep sleep.
340 *
341 * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
342 */
rtcio_ll_enable_sleep_setting(gpio_num_t gpio_num)343 static inline void rtcio_ll_enable_sleep_setting(gpio_num_t gpio_num)
344 {
345 SET_PERI_REG_MASK(rtc_io_desc[gpio_num].reg, rtc_io_desc[gpio_num].slpsel);
346 }
347
348 /**
349 * Disable rtc io keep another setting in deep sleep. (Default)
350 *
351 * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
352 */
rtcio_ll_disable_sleep_setting(gpio_num_t gpio_num)353 static inline void rtcio_ll_disable_sleep_setting(gpio_num_t gpio_num)
354 {
355 CLEAR_PERI_REG_MASK(rtc_io_desc[gpio_num].reg, rtc_io_desc[gpio_num].slpsel);
356 }
357
rtcio_ll_ext0_set_wakeup_pin(int rtcio_num,int level)358 static inline void rtcio_ll_ext0_set_wakeup_pin(int rtcio_num, int level)
359 {
360 REG_SET_FIELD(RTC_IO_EXT_WAKEUP0_REG, RTC_IO_EXT_WAKEUP0_SEL, rtcio_num);
361 // Set level which will trigger wakeup
362 SET_PERI_REG_BITS(RTC_CNTL_EXT_WAKEUP_CONF_REG, 0x1,
363 level , RTC_CNTL_EXT_WAKEUP0_LV_S);
364 }
365
366 #ifdef __cplusplus
367 }
368 #endif
369