rtc_io.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. // Copyright 2015-2016 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. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #ifndef _DRIVER_RTC_GPIO_H_
  14. #define _DRIVER_RTC_GPIO_H_
  15. #include <stdint.h>
  16. #include "esp_err.h"
  17. #include "driver/gpio.h"
  18. #include "soc/rtc_io_periph.h"
  19. #include "hal/rtc_io_types.h"
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. /**
  24. * @brief Determine if the specified GPIO is a valid RTC GPIO.
  25. *
  26. * @param gpio_num GPIO number
  27. * @return true if GPIO is valid for RTC GPIO use. false otherwise.
  28. */
  29. static inline bool rtc_gpio_is_valid_gpio(gpio_num_t gpio_num)
  30. {
  31. return (gpio_num < GPIO_PIN_COUNT
  32. && rtc_io_num_map[gpio_num] >= 0);
  33. }
  34. #define RTC_GPIO_IS_VALID_GPIO(gpio_num) rtc_gpio_is_valid_gpio(gpio_num) // Deprecated, use rtc_gpio_is_valid_gpio()
  35. /**
  36. * @brief Get RTC IO index number by gpio number.
  37. *
  38. * @param gpio_num GPIO number
  39. * @return
  40. * >=0: Index of rtcio.
  41. * -1 : The gpio is not rtcio.
  42. */
  43. static inline int rtc_io_number_get(gpio_num_t gpio_num)
  44. {
  45. return rtc_io_num_map[gpio_num];
  46. }
  47. /**
  48. * @brief Init a GPIO as RTC GPIO
  49. *
  50. * This function must be called when initializing a pad for an analog function.
  51. *
  52. * @param gpio_num GPIO number (e.g. GPIO_NUM_12)
  53. *
  54. * @return
  55. * - ESP_OK success
  56. * - ESP_ERR_INVALID_ARG GPIO is not an RTC IO
  57. */
  58. esp_err_t rtc_gpio_init(gpio_num_t gpio_num);
  59. /**
  60. * @brief Init a GPIO as digital GPIO
  61. *
  62. * @param gpio_num GPIO number (e.g. GPIO_NUM_12)
  63. *
  64. * @return
  65. * - ESP_OK success
  66. * - ESP_ERR_INVALID_ARG GPIO is not an RTC IO
  67. */
  68. esp_err_t rtc_gpio_deinit(gpio_num_t gpio_num);
  69. /**
  70. * @brief Get the RTC IO input level
  71. *
  72. * @param gpio_num GPIO number (e.g. GPIO_NUM_12)
  73. *
  74. * @return
  75. * - 1 High level
  76. * - 0 Low level
  77. * - ESP_ERR_INVALID_ARG GPIO is not an RTC IO
  78. */
  79. uint32_t rtc_gpio_get_level(gpio_num_t gpio_num);
  80. /**
  81. * @brief Set the RTC IO output level
  82. *
  83. * @param gpio_num GPIO number (e.g. GPIO_NUM_12)
  84. * @param level output level
  85. *
  86. * @return
  87. * - ESP_OK Success
  88. * - ESP_ERR_INVALID_ARG GPIO is not an RTC IO
  89. */
  90. esp_err_t rtc_gpio_set_level(gpio_num_t gpio_num, uint32_t level);
  91. /**
  92. * @brief RTC GPIO set direction
  93. *
  94. * Configure RTC GPIO direction, such as output only, input only,
  95. * output and input.
  96. *
  97. * @param gpio_num GPIO number (e.g. GPIO_NUM_12)
  98. * @param mode GPIO direction
  99. *
  100. * @return
  101. * - ESP_OK Success
  102. * - ESP_ERR_INVALID_ARG GPIO is not an RTC IO
  103. */
  104. esp_err_t rtc_gpio_set_direction(gpio_num_t gpio_num, rtc_gpio_mode_t mode);
  105. /**
  106. * @brief RTC GPIO set direction in deep sleep mode or disable sleep status (default).
  107. * In some application scenarios, IO needs to have another states during deep sleep.
  108. *
  109. * NOTE: ESP32 support INPUT_ONLY mode.
  110. * ESP32S2 support INPUT_ONLY, OUTPUT_ONLY, INPUT_OUTPUT mode.
  111. *
  112. * @param gpio_num GPIO number (e.g. GPIO_NUM_12)
  113. * @param mode GPIO direction
  114. *
  115. * @return
  116. * - ESP_OK Success
  117. * - ESP_ERR_INVALID_ARG GPIO is not an RTC IO
  118. */
  119. esp_err_t rtc_gpio_set_direction_in_sleep(gpio_num_t gpio_num, rtc_gpio_mode_t mode);
  120. /**
  121. * @brief RTC GPIO pullup enable
  122. *
  123. * This function only works for RTC IOs. In general, call gpio_pullup_en,
  124. * which will work both for normal GPIOs and RTC IOs.
  125. *
  126. * @param gpio_num GPIO number (e.g. GPIO_NUM_12)
  127. *
  128. * @return
  129. * - ESP_OK Success
  130. * - ESP_ERR_INVALID_ARG GPIO is not an RTC IO
  131. */
  132. esp_err_t rtc_gpio_pullup_en(gpio_num_t gpio_num);
  133. /**
  134. * @brief RTC GPIO pulldown enable
  135. *
  136. * This function only works for RTC IOs. In general, call gpio_pulldown_en,
  137. * which will work both for normal GPIOs and RTC IOs.
  138. *
  139. * @param gpio_num GPIO number (e.g. GPIO_NUM_12)
  140. *
  141. * @return
  142. * - ESP_OK Success
  143. * - ESP_ERR_INVALID_ARG GPIO is not an RTC IO
  144. */
  145. esp_err_t rtc_gpio_pulldown_en(gpio_num_t gpio_num);
  146. /**
  147. * @brief RTC GPIO pullup disable
  148. *
  149. * This function only works for RTC IOs. In general, call gpio_pullup_dis,
  150. * which will work both for normal GPIOs and RTC IOs.
  151. *
  152. * @param gpio_num GPIO number (e.g. GPIO_NUM_12)
  153. *
  154. * @return
  155. * - ESP_OK Success
  156. * - ESP_ERR_INVALID_ARG GPIO is not an RTC IO
  157. */
  158. esp_err_t rtc_gpio_pullup_dis(gpio_num_t gpio_num);
  159. /**
  160. * @brief RTC GPIO pulldown disable
  161. *
  162. * This function only works for RTC IOs. In general, call gpio_pulldown_dis,
  163. * which will work both for normal GPIOs and RTC IOs.
  164. *
  165. * @param gpio_num GPIO number (e.g. GPIO_NUM_12)
  166. *
  167. * @return
  168. * - ESP_OK Success
  169. * - ESP_ERR_INVALID_ARG GPIO is not an RTC IO
  170. */
  171. esp_err_t rtc_gpio_pulldown_dis(gpio_num_t gpio_num);
  172. /**
  173. * @brief Enable hold function on an RTC IO pad
  174. *
  175. * Enabling HOLD function will cause the pad to latch current values of
  176. * input enable, output enable, output value, function, drive strength values.
  177. * This function is useful when going into light or deep sleep mode to prevent
  178. * the pin configuration from changing.
  179. *
  180. * @param gpio_num GPIO number (e.g. GPIO_NUM_12)
  181. * @return
  182. * - ESP_OK Success
  183. * - ESP_ERR_INVALID_ARG GPIO is not an RTC IO
  184. */
  185. esp_err_t rtc_gpio_hold_en(gpio_num_t gpio_num);
  186. /**
  187. * @brief Disable hold function on an RTC IO pad
  188. *
  189. * Disabling hold function will allow the pad receive the values of
  190. * input enable, output enable, output value, function, drive strength from
  191. * RTC_IO peripheral.
  192. *
  193. * @param gpio_num GPIO number (e.g. GPIO_NUM_12)
  194. * @return
  195. * - ESP_OK Success
  196. * - ESP_ERR_INVALID_ARG GPIO is not an RTC IO
  197. */
  198. esp_err_t rtc_gpio_hold_dis(gpio_num_t gpio_num);
  199. /**
  200. * @brief Helper function to disconnect internal circuits from an RTC IO
  201. * This function disables input, output, pullup, pulldown, and enables
  202. * hold feature for an RTC IO.
  203. * Use this function if an RTC IO needs to be disconnected from internal
  204. * circuits in deep sleep, to minimize leakage current.
  205. *
  206. * In particular, for ESP32-WROVER module, call
  207. * rtc_gpio_isolate(GPIO_NUM_12) before entering deep sleep, to reduce
  208. * deep sleep current.
  209. *
  210. * @param gpio_num GPIO number (e.g. GPIO_NUM_12).
  211. * @return
  212. * - ESP_OK on success
  213. * - ESP_ERR_INVALID_ARG if GPIO is not an RTC IO
  214. */
  215. esp_err_t rtc_gpio_isolate(gpio_num_t gpio_num);
  216. /**
  217. * @brief Enable force hold signal for all RTC IOs
  218. *
  219. * Each RTC pad has a "force hold" input signal from the RTC controller.
  220. * If this signal is set, pad latches current values of input enable,
  221. * function, output enable, and other signals which come from the RTC mux.
  222. * Force hold signal is enabled before going into deep sleep for pins which
  223. * are used for EXT1 wakeup.
  224. */
  225. esp_err_t rtc_gpio_force_hold_all(void);
  226. /**
  227. * @brief Disable force hold signal for all RTC IOs
  228. */
  229. esp_err_t rtc_gpio_force_hold_dis_all(void);
  230. /**
  231. * @brief Set RTC GPIO pad drive capability
  232. *
  233. * @param gpio_num GPIO number, only support output GPIOs
  234. * @param strength Drive capability of the pad
  235. *
  236. * @return
  237. * - ESP_OK Success
  238. * - ESP_ERR_INVALID_ARG Parameter error
  239. */
  240. esp_err_t rtc_gpio_set_drive_capability(gpio_num_t gpio_num, gpio_drive_cap_t strength);
  241. /**
  242. * @brief Get RTC GPIO pad drive capability
  243. *
  244. * @param gpio_num GPIO number, only support output GPIOs
  245. * @param strength Pointer to accept drive capability of the pad
  246. *
  247. * @return
  248. * - ESP_OK Success
  249. * - ESP_ERR_INVALID_ARG Parameter error
  250. */
  251. esp_err_t rtc_gpio_get_drive_capability(gpio_num_t gpio_num, gpio_drive_cap_t* strength);
  252. /**
  253. * @brief Enable wakeup from sleep mode using specific GPIO
  254. * @param gpio_num GPIO number
  255. * @param intr_type Wakeup on high level (GPIO_INTR_HIGH_LEVEL) or low level
  256. * (GPIO_INTR_LOW_LEVEL)
  257. * @return
  258. * - ESP_OK on success
  259. * - ESP_ERR_INVALID_ARG if gpio_num is not an RTC IO, or intr_type is not
  260. * one of GPIO_INTR_HIGH_LEVEL, GPIO_INTR_LOW_LEVEL.
  261. */
  262. esp_err_t rtc_gpio_wakeup_enable(gpio_num_t gpio_num, gpio_int_type_t intr_type);
  263. /**
  264. * @brief Disable wakeup from sleep mode using specific GPIO
  265. * @param gpio_num GPIO number
  266. * @return
  267. * - ESP_OK on success
  268. * - ESP_ERR_INVALID_ARG if gpio_num is not an RTC IO
  269. */
  270. esp_err_t rtc_gpio_wakeup_disable(gpio_num_t gpio_num);
  271. #ifdef __cplusplus
  272. }
  273. #endif
  274. #endif