gpio_hal.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. * NOTICE
  16. * The hal is not public api, don't use in application code.
  17. * See readme.md in soc/include/hal/readme.md
  18. ******************************************************************************/
  19. // The HAL layer for GPIO
  20. #pragma once
  21. #include "soc/gpio_periph.h"
  22. #include "hal/gpio_ll.h"
  23. #include "hal/gpio_types.h"
  24. #ifdef CONFIG_LEGACY_INCLUDE_COMMON_HEADERS
  25. #include "soc/rtc_io_reg.h"
  26. #endif
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. // Get GPIO hardware instance with giving gpio num
  31. #define GPIO_HAL_GET_HW(num) GPIO_LL_GET_HW(num)
  32. /**
  33. * Context that should be maintained by both the driver and the HAL
  34. */
  35. typedef struct {
  36. gpio_dev_t *dev;
  37. uint32_t version;
  38. } gpio_hal_context_t;
  39. /**
  40. * @brief Enable pull-up on GPIO.
  41. *
  42. * @param hal Context of the HAL layer
  43. * @param gpio_num GPIO number
  44. */
  45. #define gpio_hal_pullup_en(hal, gpio_num) gpio_ll_pullup_en((hal)->dev, gpio_num)
  46. /**
  47. * @brief Disable pull-up on GPIO.
  48. *
  49. * @param hal Context of the HAL layer
  50. * @param gpio_num GPIO number
  51. */
  52. #define gpio_hal_pullup_dis(hal, gpio_num) gpio_ll_pullup_dis((hal)->dev, gpio_num)
  53. /**
  54. * @brief Enable pull-down on GPIO.
  55. *
  56. * @param hal Context of the HAL layer
  57. * @param gpio_num GPIO number
  58. */
  59. #define gpio_hal_pulldown_en(hal, gpio_num) gpio_ll_pulldown_en((hal)->dev, gpio_num)
  60. /**
  61. * @brief Disable pull-down on GPIO.
  62. *
  63. * @param hal Context of the HAL layer
  64. * @param gpio_num GPIO number
  65. */
  66. #define gpio_hal_pulldown_dis(hal, gpio_num) gpio_ll_pulldown_dis((hal)->dev, gpio_num)
  67. /**
  68. * @brief GPIO set interrupt trigger type
  69. *
  70. * @param hal Context of the HAL layer
  71. * @param gpio_num GPIO number. If you want to set the trigger type of e.g. of GPIO16, gpio_num should be GPIO_NUM_16 (16);
  72. * @param intr_type Interrupt type, select from gpio_int_type_t
  73. */
  74. #define gpio_hal_set_intr_type(hal, gpio_num, intr_type) gpio_ll_set_intr_type((hal)->dev, gpio_num, intr_type)
  75. /**
  76. * @brief Get GPIO interrupt status
  77. *
  78. * @param hal Context of the HAL layer
  79. * @param core_id interrupt core id
  80. * @param status interrupt status
  81. */
  82. #define gpio_hal_get_intr_status(hal, core_id, status) gpio_ll_get_intr_status((hal)->dev, core_id, status)
  83. /**
  84. * @brief Get GPIO interrupt status high
  85. *
  86. * @param hal Context of the HAL layer
  87. * @param core_id interrupt core id
  88. * @param status interrupt status high
  89. */
  90. #define gpio_hal_get_intr_status_high(hal, core_id, status) gpio_ll_get_intr_status_high((hal)->dev, core_id, status)
  91. /**
  92. * @brief Clear GPIO interrupt status
  93. *
  94. * @param hal Context of the HAL layer
  95. * @param mask interrupt status clear mask
  96. */
  97. #define gpio_hal_clear_intr_status(hal, mask) gpio_ll_clear_intr_status((hal)->dev, mask)
  98. /**
  99. * @brief Clear GPIO interrupt status high
  100. *
  101. * @param hal Context of the HAL layer
  102. * @param mask interrupt status high clear mask
  103. */
  104. #define gpio_hal_clear_intr_status_high(hal, mask) gpio_ll_clear_intr_status_high((hal)->dev, mask)
  105. /**
  106. * @brief Enable GPIO module interrupt signal
  107. *
  108. * @param hal Context of the HAL layer
  109. * @param gpio_num GPIO number. If you want to enable the interrupt of e.g. GPIO16, gpio_num should be GPIO_NUM_16 (16);
  110. * @param core_id Interrupt enabled CPU to corresponding ID
  111. */
  112. void gpio_hal_intr_enable_on_core(gpio_hal_context_t *hal, gpio_num_t gpio_num, uint32_t core_id);
  113. /**
  114. * @brief Disable GPIO module interrupt signal
  115. *
  116. * @param hal Context of the HAL layer
  117. * @param gpio_num GPIO number. If you want to disable the interrupt of e.g. GPIO16, gpio_num should be GPIO_NUM_16 (16);
  118. */
  119. void gpio_hal_intr_disable(gpio_hal_context_t *hal, gpio_num_t gpio_num);
  120. /**
  121. * @brief Disable input mode on GPIO.
  122. *
  123. * @param hal Context of the HAL layer
  124. * @param gpio_num GPIO number
  125. */
  126. #define gpio_hal_input_disable(hal, gpio_num) gpio_ll_input_disable((hal)->dev, gpio_num)
  127. /**
  128. * @brief Enable input mode on GPIO.
  129. *
  130. * @param hal Context of the HAL layer
  131. * @param gpio_num GPIO number
  132. */
  133. #define gpio_hal_input_enable(hal, gpio_num) gpio_ll_input_enable((hal)->dev, gpio_num)
  134. /**
  135. * @brief Disable output mode on GPIO.
  136. *
  137. * @param hal Context of the HAL layer
  138. * @param gpio_num GPIO number
  139. */
  140. #define gpio_hal_output_disable(hal, gpio_num) gpio_ll_output_disable((hal)->dev, gpio_num)
  141. /**
  142. * @brief Enable output mode on GPIO.
  143. *
  144. * @param hal Context of the HAL layer
  145. * @param gpio_num GPIO number
  146. */
  147. #define gpio_hal_output_enable(hal, gpio_num) gpio_ll_output_enable((hal)->dev, gpio_num)
  148. /**
  149. * @brief Disable open-drain mode on GPIO.
  150. *
  151. * @param hal Context of the HAL layer
  152. * @param gpio_num GPIO number
  153. */
  154. #define gpio_hal_od_disable(hal, gpio_num) gpio_ll_od_disable((hal)->dev, gpio_num)
  155. /**
  156. * @brief Enable open-drain mode on GPIO.
  157. *
  158. * @param hal Context of the HAL layer
  159. * @param gpio_num GPIO number
  160. */
  161. #define gpio_hal_od_enable(hal, gpio_num) gpio_ll_od_enable((hal)->dev, gpio_num)
  162. /**
  163. * @brief GPIO set output level
  164. *
  165. * @param hal Context of the HAL layer
  166. * @param gpio_num GPIO number. If you want to set the output level of e.g. GPIO16, gpio_num should be GPIO_NUM_16 (16);
  167. * @param level Output level. 0: low ; 1: high
  168. */
  169. #define gpio_hal_set_level(hal, gpio_num, level) gpio_ll_set_level((hal)->dev, gpio_num, level)
  170. /**
  171. * @brief GPIO get input level
  172. *
  173. * @warning If the pad is not configured for input (or input and output) the returned value is always 0.
  174. *
  175. * @param hal Context of the HAL layer
  176. * @param gpio_num GPIO number. If you want to get the logic level of e.g. pin GPIO16, gpio_num should be GPIO_NUM_16 (16);
  177. *
  178. * @return
  179. * - 0 the GPIO input level is 0
  180. * - 1 the GPIO input level is 1
  181. */
  182. #define gpio_hal_get_level(hal, gpio_num) gpio_ll_get_level((hal)->dev, gpio_num)
  183. /**
  184. * @brief Enable GPIO wake-up function.
  185. *
  186. * @param hal Context of the HAL layer
  187. * @param gpio_num GPIO number.
  188. * @param intr_type GPIO wake-up type. Only GPIO_INTR_LOW_LEVEL or GPIO_INTR_HIGH_LEVEL can be used.
  189. */
  190. #define gpio_hal_wakeup_enable(hal, gpio_num, intr_type) gpio_ll_wakeup_enable((hal)->dev, gpio_num, intr_type)
  191. /**
  192. * @brief Disable GPIO wake-up function.
  193. *
  194. * @param hal Context of the HAL layer
  195. * @param gpio_num GPIO number
  196. */
  197. #define gpio_hal_wakeup_disable(hal, gpio_num) gpio_ll_wakeup_disable((hal)->dev, gpio_num)
  198. /**
  199. * @brief Set GPIO pad drive capability
  200. *
  201. * @param hal Context of the HAL layer
  202. * @param gpio_num GPIO number, only support output GPIOs
  203. * @param strength Drive capability of the pad
  204. */
  205. #define gpio_hal_set_drive_capability(hal, gpio_num, strength) gpio_ll_set_drive_capability((hal)->dev, gpio_num, strength)
  206. /**
  207. * @brief Get GPIO pad drive capability
  208. *
  209. * @param hal Context of the HAL layer
  210. * @param gpio_num GPIO number, only support output GPIOs
  211. * @param strength Pointer to accept drive capability of the pad
  212. */
  213. #define gpio_hal_get_drive_capability(hal, gpio_num, strength) gpio_ll_get_drive_capability((hal)->dev, gpio_num, strength)
  214. /**
  215. * @brief Enable gpio pad hold function.
  216. *
  217. * The gpio pad hold function works in both input and output modes, but must be output-capable gpios.
  218. * If pad hold enabled:
  219. * in output mode: the output level of the pad will be force locked and can not be changed.
  220. * in input mode: the input value read will not change, regardless the changes of input signal.
  221. *
  222. * The state of digital gpio cannot be held during Deep-sleep, and it will resume the hold function
  223. * when the chip wakes up from Deep-sleep. If the digital gpio also needs to be held during Deep-sleep,
  224. * `gpio_deep_sleep_hold_en` should also be called.
  225. *
  226. * Power down or call gpio_hold_dis will disable this function.
  227. *
  228. * @param hal Context of the HAL layer
  229. * @param gpio_num GPIO number, only support output GPIOs
  230. */
  231. #define gpio_hal_hold_en(hal, gpio_num) gpio_ll_hold_en((hal)->dev, gpio_num)
  232. /**
  233. * @brief Disable gpio pad hold function.
  234. *
  235. * When the chip is woken up from Deep-sleep, the gpio will be set to the default mode, so, the gpio will output
  236. * the default level if this function is called. If you don't want the level changes, the gpio should be configured to
  237. * a known state before this function is called.
  238. * e.g.
  239. * If you hold gpio18 high during Deep-sleep, after the chip is woken up and `gpio_hold_dis` is called,
  240. * gpio18 will output low level(because gpio18 is input mode by default). If you don't want this behavior,
  241. * you should configure gpio18 as output mode and set it to hight level before calling `gpio_hold_dis`.
  242. *
  243. * @param hal Context of the HAL layer
  244. * @param gpio_num GPIO number, only support output GPIOs
  245. */
  246. #define gpio_hal_hold_dis(hal, gpio_num) gpio_ll_hold_dis((hal)->dev, gpio_num)
  247. /**
  248. * @brief Enable all digital gpio pad hold function during Deep-sleep.
  249. *
  250. * When the chip is in Deep-sleep mode, all digital gpio will hold the state before sleep, and when the chip is woken up,
  251. * the status of digital gpio will not be held. Note that the pad hold feature only works when the chip is in Deep-sleep mode,
  252. * when not in sleep mode, the digital gpio state can be changed even you have called this function.
  253. *
  254. * Power down or call gpio_hold_dis will disable this function, otherwise, the digital gpio hold feature works as long as the chip enter Deep-sleep.
  255. *
  256. * @param hal Context of the HAL layer
  257. */
  258. #define gpio_hal_deep_sleep_hold_en(hal) gpio_ll_deep_sleep_hold_en((hal)->dev)
  259. /**
  260. * @brief Disable all digital gpio pad hold function during Deep-sleep.
  261. *
  262. * @param hal Context of the HAL layer
  263. */
  264. #define gpio_hal_deep_sleep_hold_dis(hal) gpio_ll_deep_sleep_hold_dis((hal)->dev)
  265. /**
  266. * @brief Set pad input to a peripheral signal through the IOMUX.
  267. *
  268. * @param hal Context of the HAL layer
  269. * @param gpio_num GPIO number of the pad.
  270. * @param signal_idx Peripheral signal id to input. One of the ``*_IN_IDX`` signals in ``soc/gpio_sig_map.h``.
  271. */
  272. #define gpio_hal_iomux_in(hal, gpio_num, signal_idx) gpio_ll_iomux_in((hal)->dev, gpio_num, signal_idx)
  273. /**
  274. * @brief Set peripheral output to an GPIO pad through the IOMUX.
  275. *
  276. * @param hal Context of the HAL layer
  277. * @param gpio_num gpio_num GPIO number of the pad.
  278. * @param func The function number of the peripheral pin to output pin.
  279. * One of the ``FUNC_X_*`` of specified pin (X) in ``soc/io_mux_reg.h``.
  280. * @param oen_inv True if the output enable needs to be inverted, otherwise False.
  281. */
  282. #define gpio_hal_iomux_out(hal, gpio_num, func, oen_inv) gpio_ll_iomux_out((hal)->dev, gpio_num, func, oen_inv)
  283. #if GPIO_SUPPORTS_FORCE_HOLD
  284. /**
  285. * @brief Force hold digital and rtc gpio pad.
  286. * @note GPIO force hold, whether the chip in sleep mode or wakeup mode.
  287. *
  288. * @param hal Context of the HAL layer
  289. * */
  290. #define gpio_hal_force_hold_all(hal) gpio_ll_force_hold_all((hal)->dev)
  291. /**
  292. * @brief Force unhold digital and rtc gpio pad.
  293. * @note GPIO force unhold, whether the chip in sleep mode or wakeup mode.
  294. *
  295. * @param hal Context of the HAL layer
  296. * */
  297. #define gpio_hal_force_unhold_all(hal) gpio_ll_force_unhold_all((hal)->dev)
  298. #endif
  299. #ifdef __cplusplus
  300. }
  301. #endif