1 /*
2  * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /*******************************************************************************
8  * NOTICE
9  * The hal is not public api, don't use in application code.
10  * See readme.md in hal/include/hal/readme.md
11  ******************************************************************************/
12 
13 // The HAL layer for RTC IO master (common part)
14 
15 #pragma once
16 
17 #include <esp_err.h>
18 #include "sdkconfig.h"
19 
20 #include "soc/soc_caps.h"
21 #if SOC_RTCIO_INPUT_OUTPUT_SUPPORTED
22 #include "hal/rtc_io_ll.h"
23 #include "hal/rtc_io_types.h"
24 #endif
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 #if SOC_RTCIO_INPUT_OUTPUT_SUPPORTED
31 /**
32  * Select the rtcio function.
33  *
34  * @note The RTC function must be selected before the pad analog function is enabled.
35  * @param rtcio_num The index of rtcio. 0 ~ SOC_RTCIO_PIN_COUNT.
36  * @param func Select pin function.
37  */
38 #define rtcio_hal_function_select(rtcio_num, func) rtcio_ll_function_select(rtcio_num, func)
39 
40 /**
41  * Enable rtcio output.
42  *
43  * @param rtcio_num The index of rtcio. 0 ~ SOC_RTCIO_PIN_COUNT.
44  */
45 #define rtcio_hal_output_enable(rtcio_num) rtcio_ll_output_enable(rtcio_num)
46 
47 /**
48  * Disable rtcio output.
49  *
50  * @param rtcio_num The index of rtcio. 0 ~ SOC_RTCIO_PIN_COUNT.
51  */
52 #define rtcio_hal_output_disable(rtcio_num) rtcio_ll_output_disable(rtcio_num)
53 
54 /**
55  * Set RTCIO output level.
56  *
57  * @param rtcio_num The index of rtcio. 0 ~ SOC_RTCIO_PIN_COUNT.
58  * @param level 0: output low; ~0: output high.
59  */
60 #define rtcio_hal_set_level(rtcio_num, level) rtcio_ll_set_level(rtcio_num, level)
61 
62 /**
63  * Enable rtcio input.
64  *
65  * @param rtcio_num The index of rtcio. 0 ~ SOC_RTCIO_PIN_COUNT.
66  */
67 #define rtcio_hal_input_enable(rtcio_num) rtcio_ll_input_enable(rtcio_num)
68 
69 /**
70  * Disable rtcio input.
71  *
72  * @param rtcio_num The index of rtcio. 0 ~ SOC_RTCIO_PIN_COUNT.
73  */
74 #define rtcio_hal_input_disable(rtcio_num) rtcio_ll_input_disable(rtcio_num)
75 
76 /**
77  * Get RTCIO input level.
78  *
79  * @param rtcio_num The index of rtcio. 0 ~ SOC_RTCIO_PIN_COUNT.
80  * @return 0: input low; ~0: input high.
81  */
82 #define rtcio_hal_get_level(rtcio_num) rtcio_ll_get_level(rtcio_num)
83 
84 /**
85  * @brief Set RTC GPIO pad drive capability.
86  *
87  * @param rtcio_num The index of rtcio. 0 ~ SOC_RTCIO_PIN_COUNT.
88  * @param strength Drive capability of the pad. Range: 0 ~ 3.
89  */
90 #define rtcio_hal_set_drive_capability(rtcio_num, strength) rtcio_ll_set_drive_capability(rtcio_num, strength)
91 
92 /**
93  * @brief Get RTC GPIO pad drive capability.
94  *
95  * @param rtcio_num The index of rtcio. 0 ~ SOC_RTCIO_PIN_COUNT.
96  * @return Drive capability of the pad. Range: 0 ~ 3.
97  */
98 #define rtcio_hal_get_drive_capability(rtcio_num) rtcio_ll_get_drive_capability(rtcio_num)
99 
100 /**
101  * Set RTCIO output level.
102  *
103  * @param rtcio_num The index of rtcio. 0 ~ SOC_RTCIO_PIN_COUNT.
104  * @param level 0: output low; ~0: output high.
105  */
106 #define rtcio_hal_set_level(rtcio_num, level) rtcio_ll_set_level(rtcio_num, level)
107 
108 /**
109  * Get RTCIO input level.
110  *
111  * @param rtcio_num The index of rtcio. 0 ~ SOC_RTCIO_PIN_COUNT.
112  * @return 0: input low; ~0: input high.
113  */
114 #define rtcio_hal_get_level(rtcio_num) rtcio_ll_get_level(rtcio_num)
115 
116 /**
117  * Set RTC IO direction.
118  *
119  * Configure RTC IO direction, such as output only, input only,
120  * output and input.
121  *
122  * @param rtcio_num The index of rtcio. 0 ~ SOC_RTCIO_PIN_COUNT.
123  * @param mode IO direction.
124  */
125 void rtcio_hal_set_direction(int rtcio_num, rtc_gpio_mode_t mode);
126 
127 /**
128  * Set RTC IO direction in deep sleep or disable sleep status.
129  *
130  * NOTE: ESP32 support INPUT_ONLY mode.
131  *       ESP32S2 support INPUT_ONLY, OUTPUT_ONLY, INPUT_OUTPUT mode.
132  *
133  * @param rtcio_num The index of rtcio. 0 ~ SOC_RTCIO_PIN_COUNT.
134  * @param mode IO direction.
135  */
136 void rtcio_hal_set_direction_in_sleep(int rtcio_num, rtc_gpio_mode_t mode);
137 
138 /**
139  * RTC GPIO pullup enable.
140  *
141  * @param rtcio_num The index of rtcio. 0 ~ SOC_RTCIO_PIN_COUNT.
142  */
143 #define rtcio_hal_pullup_enable(rtcio_num) rtcio_ll_pullup_enable(rtcio_num)
144 
145 /**
146  * RTC GPIO pullup disable.
147  *
148  * @param rtcio_num The index of rtcio. 0 ~ SOC_RTCIO_PIN_COUNT.
149  */
150 #define rtcio_hal_pullup_disable(rtcio_num) rtcio_ll_pullup_disable(rtcio_num)
151 
152 /**
153  * RTC GPIO pulldown enable.
154  *
155  * @param rtcio_num The index of rtcio. 0 ~ SOC_RTCIO_PIN_COUNT.
156  */
157 #define rtcio_hal_pulldown_enable(rtcio_num) rtcio_ll_pulldown_enable(rtcio_num)
158 
159 /**
160  * RTC GPIO pulldown disable.
161  *
162  * @param rtcio_num The index of rtcio. 0 ~ SOC_RTCIO_PIN_COUNT.
163  */
164 #define rtcio_hal_pulldown_disable(rtcio_num) rtcio_ll_pulldown_disable(rtcio_num)
165 
166 #endif // SOC_RTCIO_INPUT_OUTPUT_SUPPORTED
167 
168 #if SOC_RTCIO_HOLD_SUPPORTED
169 
170 /**
171  * Enable force hold function on an RTC IO pad.
172  *
173  * Enabling HOLD function will cause the pad to lock current status, such as,
174  * input/output enable, input/output value, function, drive strength values.
175  * This function is useful when going into light or deep sleep mode to prevent
176  * the pin configuration from changing.
177  *
178  * @param rtcio_num The index of rtcio. 0 ~ SOC_RTCIO_PIN_COUNT.
179  */
180 #define rtcio_hal_hold_enable(rtcio_num) rtcio_ll_force_hold_enable(rtcio_num)
181 
182 /**
183  * Disable hold function on an RTC IO pad.
184  *
185  * @note If disable the pad hold, the status of pad maybe changed in sleep mode.
186  * @param rtcio_num The index of rtcio. 0 ~ SOC_RTCIO_PIN_COUNT.
187  */
188 #define rtcio_hal_hold_disable(rtcio_num) rtcio_ll_force_hold_disable(rtcio_num)
189 
190 /**
191  * Enable force hold function on all RTC IO pads.
192  *
193  * Enabling HOLD function will cause the pad to lock current status, such as,
194  * input/output enable, input/output value, function, drive strength values.
195  * This function is useful when going into light or deep sleep mode to prevent
196  * the pin configuration from changing.
197  *
198  * @param rtcio_num The index of rtcio. 0 ~ SOC_RTCIO_PIN_COUNT.
199  */
200 #define rtcio_hal_hold_all() rtcio_ll_force_hold_all()
201 
202 /**
203  * Disable hold function on all RTC IO pads.
204  *
205  * @note If disable the pad hold, the status of pad maybe changed in sleep mode.
206  * @param rtcio_num The index of rtcio. 0 ~ SOC_RTCIO_PIN_COUNT.
207  */
208 #define rtcio_hal_unhold_all() rtcio_ll_force_unhold_all()
209 #endif // SOC_RTCIO_HOLD_SUPPORTED
210 
211 #if SOC_RTCIO_WAKE_SUPPORTED
212 
213 /**
214  * Enable wakeup function and set wakeup type from light sleep status for rtcio.
215  *
216  * @param rtcio_num The index of rtcio. 0 ~ SOC_RTCIO_PIN_COUNT.
217  * @param type  Wakeup on high level or low level.
218  */
219 #define rtcio_hal_wakeup_enable(rtcio_num, type) rtcio_ll_wakeup_enable(rtcio_num, type)
220 
221 /**
222  * Disable wakeup function from light sleep status for rtcio.
223  *
224  * @param rtcio_num The index of rtcio. 0 ~ SOC_RTCIO_PIN_COUNT.
225  */
226 #define rtcio_hal_wakeup_disable(rtcio_num) rtcio_ll_wakeup_disable(rtcio_num)
227 
228 /**
229  * Set specific logic level on an RTC IO pin as a wakeup trigger.
230  *
231  * @param rtcio_num The index of rtcio. 0 ~ SOC_RTCIO_PIN_COUNT.
232  * @param level Logic level (0)
233  */
234 #define rtcio_hal_ext0_set_wakeup_pin(rtcio_num, level)     rtcio_ll_ext0_set_wakeup_pin(rtcio_num, level)
235 
236 #endif
237 
238 #if SOC_RTCIO_HOLD_SUPPORTED || SOC_RTCIO_INPUT_OUTPUT_SUPPORTED
239 
240 /**
241  * Helper function to disconnect internal circuits from an RTC IO
242  * This function disables input, output, pullup, pulldown, and enables
243  * hold feature for an RTC IO.
244  * Use this function if an RTC IO needs to be disconnected from internal
245  * circuits in deep sleep, to minimize leakage current.
246  *
247  * In particular, for ESP32-WROVER module, call
248  * rtc_gpio_isolate(GPIO_NUM_12) before entering deep sleep, to reduce
249  * deep sleep current.
250  *
251  * @param rtcio_num The index of rtcio. 0 ~ SOC_RTCIO_PIN_COUNT.
252  */
253 void rtcio_hal_isolate(int rtc_num);
254 
255 #endif
256 
257 #if SOC_GPIO_SUPPORT_DEEPSLEEP_WAKEUP && (SOC_RTCIO_PIN_COUNT > 0)
258 
259 #define gpio_hal_deepsleep_wakeup_enable(hal, gpio_num, intr_type)  rtcio_hal_wakeup_enable(gpio_num, intr_type)
260 #define gpio_hal_deepsleep_wakeup_disable(hal, gpio_num)            rtcio_hal_wakeup_disable(gpio_num)
261 #define gpio_hal_deepsleep_wakeup_is_enabled(hal, gpio_num)         rtcio_hal_wakeup_is_enabled(gpio_num)
262 #define rtc_hal_gpio_get_wakeup_status()                            rtcio_hal_get_interrupt_status()
263 #define rtc_hal_gpio_clear_wakeup_status()                          rtcio_hal_clear_interrupt_status()
264 
265 /**
266  * @brief Get the status of whether an IO is used for sleep wake-up.
267  *
268  * @param hw Peripheral GPIO hardware instance address.
269  * @param rtcio_num GPIO number
270  * @return True if the pin is enabled to wake up from deep-sleep
271  */
272 #define rtcio_hal_wakeup_is_enabled(rtcio_num) rtcio_ll_wakeup_is_enabled(rtcio_num)
273 
274 /**
275  * @brief Get the rtc io interrupt status
276  *
277  * @return  bit 0~7 corresponding to 0 ~ SOC_RTCIO_PIN_COUNT.
278  */
279 #define rtcio_hal_get_interrupt_status()        rtcio_ll_get_interrupt_status()
280 
281 /**
282  * @brief Clear all LP IO pads status
283  */
284 #define rtcio_hal_clear_interrupt_status()      rtcio_ll_clear_interrupt_status()
285 
286 #endif //SOC_GPIO_SUPPORT_DEEPSLEEP_WAKEUP
287 #ifdef __cplusplus
288 }
289 #endif
290