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