rtc_io.h 8.5 KB

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