nrfx_gpiote.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /**
  2. * Copyright (c) 2015 - 2019, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #ifndef NRFX_GPIOTE_H__
  41. #define NRFX_GPIOTE_H__
  42. #include <nrfx.h>
  43. #include <hal/nrf_gpiote.h>
  44. #include <hal/nrf_gpio.h>
  45. #ifdef __cplusplus
  46. extern "C" {
  47. #endif
  48. /**
  49. * @defgroup nrfx_gpiote GPIOTE driver
  50. * @{
  51. * @ingroup nrf_gpiote
  52. * @brief GPIOTE peripheral driver.
  53. */
  54. /**@brief Input pin configuration. */
  55. typedef struct
  56. {
  57. nrf_gpiote_polarity_t sense; /**< Transition that triggers interrupt. */
  58. nrf_gpio_pin_pull_t pull; /**< Pulling mode. */
  59. bool is_watcher : 1; /**< True when the input pin is tracking an output pin. */
  60. bool hi_accuracy : 1; /**< True when high accuracy (IN_EVENT) is used. */
  61. bool skip_gpio_setup : 1; /**< Do not change GPIO configuration */
  62. } nrfx_gpiote_in_config_t;
  63. /**@brief Macro for configuring a pin to use a GPIO IN or PORT EVENT to detect low-to-high transition.
  64. * @details Set hi_accu to true to use IN_EVENT. */
  65. #define NRFX_GPIOTE_CONFIG_IN_SENSE_LOTOHI(hi_accu) \
  66. { \
  67. .is_watcher = false, \
  68. .hi_accuracy = hi_accu, \
  69. .pull = NRF_GPIO_PIN_NOPULL, \
  70. .sense = NRF_GPIOTE_POLARITY_LOTOHI, \
  71. }
  72. /**@brief Macro for configuring a pin to use a GPIO IN or PORT EVENT to detect high-to-low transition.
  73. * @details Set hi_accu to true to use IN_EVENT. */
  74. #define NRFX_GPIOTE_CONFIG_IN_SENSE_HITOLO(hi_accu) \
  75. { \
  76. .is_watcher = false, \
  77. .hi_accuracy = hi_accu, \
  78. .pull = NRF_GPIO_PIN_NOPULL, \
  79. .sense = NRF_GPIOTE_POLARITY_HITOLO, \
  80. }
  81. /**@brief Macro for configuring a pin to use a GPIO IN or PORT EVENT to detect any change on the pin.
  82. * @details Set hi_accu to true to use IN_EVENT.*/
  83. #define NRFX_GPIOTE_CONFIG_IN_SENSE_TOGGLE(hi_accu) \
  84. { \
  85. .is_watcher = false, \
  86. .hi_accuracy = hi_accu, \
  87. .pull = NRF_GPIO_PIN_NOPULL, \
  88. .sense = NRF_GPIOTE_POLARITY_TOGGLE, \
  89. }
  90. /**@brief Macro for configuring a pin to use a GPIO IN or PORT EVENT to detect low-to-high transition.
  91. * @details Set hi_accu to true to use IN_EVENT.
  92. * @note This macro prepares configuration that skips GPIO setup. */
  93. #define NRFX_GPIOTE_RAW_CONFIG_IN_SENSE_LOTOHI(hi_accu) \
  94. { \
  95. .is_watcher = false, \
  96. .hi_accuracy = hi_accu, \
  97. .pull = NRF_GPIO_PIN_NOPULL, \
  98. .sense = NRF_GPIOTE_POLARITY_LOTOHI, \
  99. .skip_gpio_setup = true, \
  100. }
  101. /**@brief Macro for configuring a pin to use a GPIO IN or PORT EVENT to detect high-to-low transition.
  102. * @details Set hi_accu to true to use IN_EVENT.
  103. * @note This macro prepares configuration that skips GPIO setup. */
  104. #define NRFX_GPIOTE_RAW_CONFIG_IN_SENSE_HITOLO(hi_accu) \
  105. { \
  106. .is_watcher = false, \
  107. .hi_accuracy = hi_accu, \
  108. .pull = NRF_GPIO_PIN_NOPULL, \
  109. .sense = NRF_GPIOTE_POLARITY_HITOLO, \
  110. .skip_gpio_setup = true, \
  111. }
  112. /**@brief Macro for configuring a pin to use a GPIO IN or PORT EVENT to detect any change on the pin.
  113. * @details Set hi_accu to true to use IN_EVENT.
  114. * @note This macro prepares configuration that skips GPIO setup. */
  115. #define NRFX_GPIOTE_RAW_CONFIG_IN_SENSE_TOGGLE(hi_accu) \
  116. { \
  117. .is_watcher = false, \
  118. .hi_accuracy = hi_accu, \
  119. .pull = NRF_GPIO_PIN_NOPULL, \
  120. .sense = NRF_GPIOTE_POLARITY_TOGGLE, \
  121. .skip_gpio_setup = true, \
  122. }
  123. /**@brief Output pin configuration. */
  124. typedef struct
  125. {
  126. nrf_gpiote_polarity_t action; /**< Configuration of the pin task. */
  127. nrf_gpiote_outinit_t init_state; /**< Initial state of the output pin. */
  128. bool task_pin; /**< True if the pin is controlled by a GPIOTE task. */
  129. } nrfx_gpiote_out_config_t;
  130. /**@brief Macro for configuring a pin to use as output. GPIOTE is not used for the pin. */
  131. #define NRFX_GPIOTE_CONFIG_OUT_SIMPLE(init_high) \
  132. { \
  133. .init_state = init_high ? NRF_GPIOTE_INITIAL_VALUE_HIGH : NRF_GPIOTE_INITIAL_VALUE_LOW, \
  134. .task_pin = false, \
  135. }
  136. /**@brief Macro for configuring a pin to use the GPIO OUT TASK to change the state from high to low.
  137. * @details The task will clear the pin. Therefore, the pin is set initially. */
  138. #define NRFX_GPIOTE_CONFIG_OUT_TASK_LOW \
  139. { \
  140. .init_state = NRF_GPIOTE_INITIAL_VALUE_HIGH, \
  141. .task_pin = true, \
  142. .action = NRF_GPIOTE_POLARITY_HITOLO, \
  143. }
  144. /**@brief Macro for configuring a pin to use the GPIO OUT TASK to change the state from low to high.
  145. * @details The task will set the pin. Therefore, the pin is cleared initially. */
  146. #define NRFX_GPIOTE_CONFIG_OUT_TASK_HIGH \
  147. { \
  148. .init_state = NRF_GPIOTE_INITIAL_VALUE_LOW, \
  149. .task_pin = true, \
  150. .action = NRF_GPIOTE_POLARITY_LOTOHI, \
  151. }
  152. /**@brief Macro for configuring a pin to use the GPIO OUT TASK to toggle the pin state.
  153. * @details The initial pin state must be provided. */
  154. #define NRFX_GPIOTE_CONFIG_OUT_TASK_TOGGLE(init_high) \
  155. { \
  156. .init_state = init_high ? NRF_GPIOTE_INITIAL_VALUE_HIGH : NRF_GPIOTE_INITIAL_VALUE_LOW, \
  157. .task_pin = true, \
  158. .action = NRF_GPIOTE_POLARITY_TOGGLE, \
  159. }
  160. /** @brief Pin. */
  161. typedef uint32_t nrfx_gpiote_pin_t;
  162. /**
  163. * @brief Pin event handler prototype.
  164. *
  165. * @param pin Pin that triggered this event.
  166. * @param action Action that lead to triggering this event.
  167. */
  168. typedef void (*nrfx_gpiote_evt_handler_t)(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action);
  169. /**
  170. * @brief Function for initializing the GPIOTE module.
  171. *
  172. * @details Only static configuration is supported to prevent the shared
  173. * resource being customized by the initiator.
  174. *
  175. * @retval NRFX_SUCCESS If initialization was successful.
  176. * @retval NRFX_ERROR_INVALID_STATE If the driver was already initialized.
  177. */
  178. nrfx_err_t nrfx_gpiote_init(void);
  179. /**
  180. * @brief Function for checking if the GPIOTE module is initialized.
  181. *
  182. * @details The GPIOTE module is a shared module. Therefore, you should check if
  183. * the module is already initialized and skip initialization if it is.
  184. *
  185. * @retval true If the module is already initialized.
  186. * @retval false If the module is not initialized.
  187. */
  188. bool nrfx_gpiote_is_init(void);
  189. /**
  190. * @brief Function for uninitializing the GPIOTE module.
  191. */
  192. void nrfx_gpiote_uninit(void);
  193. /**
  194. * @brief Function for initializing a GPIOTE output pin.
  195. * @details The output pin can be controlled by the CPU or by PPI. The initial
  196. * configuration specifies which mode is used. If PPI mode is used, the driver
  197. * attempts to allocate one of the available GPIOTE channels. If no channel is
  198. * available, an error is returned.
  199. *
  200. * @param[in] pin Pin.
  201. * @param[in] p_config Initial configuration.
  202. *
  203. * @retval NRFX_SUCCESS If initialization was successful.
  204. * @retval NRFX_ERROR_INVALID_STATE If the driver is not initialized or the pin is already used.
  205. * @retval NRFX_ERROR_NO_MEM If no GPIOTE channel is available.
  206. */
  207. nrfx_err_t nrfx_gpiote_out_init(nrfx_gpiote_pin_t pin,
  208. nrfx_gpiote_out_config_t const * p_config);
  209. /**
  210. * @brief Function for uninitializing a GPIOTE output pin.
  211. * @details The driver frees the GPIOTE channel if the output pin was using one.
  212. *
  213. * @param[in] pin Pin.
  214. */
  215. void nrfx_gpiote_out_uninit(nrfx_gpiote_pin_t pin);
  216. /**
  217. * @brief Function for setting a GPIOTE output pin.
  218. *
  219. * @param[in] pin Pin.
  220. */
  221. void nrfx_gpiote_out_set(nrfx_gpiote_pin_t pin);
  222. /**
  223. * @brief Function for clearing a GPIOTE output pin.
  224. *
  225. * @param[in] pin Pin.
  226. */
  227. void nrfx_gpiote_out_clear(nrfx_gpiote_pin_t pin);
  228. /**
  229. * @brief Function for toggling a GPIOTE output pin.
  230. *
  231. * @param[in] pin Pin.
  232. */
  233. void nrfx_gpiote_out_toggle(nrfx_gpiote_pin_t pin);
  234. /**
  235. * @brief Function for enabling a GPIOTE output pin task.
  236. *
  237. * @param[in] pin Pin.
  238. */
  239. void nrfx_gpiote_out_task_enable(nrfx_gpiote_pin_t pin);
  240. /**
  241. * @brief Function for disabling a GPIOTE output pin task.
  242. *
  243. * @param[in] pin Pin.
  244. */
  245. void nrfx_gpiote_out_task_disable(nrfx_gpiote_pin_t pin);
  246. /**
  247. * @brief Function for getting the address of a configurable GPIOTE task.
  248. *
  249. * @param[in] pin Pin.
  250. *
  251. * @return Address of OUT task.
  252. */
  253. uint32_t nrfx_gpiote_out_task_addr_get(nrfx_gpiote_pin_t pin);
  254. #if defined(GPIOTE_FEATURE_SET_PRESENT) || defined(__NRFX_DOXYGEN__)
  255. /**
  256. * @brief Function for getting the address of a configurable GPIOTE task.
  257. *
  258. * @param[in] pin Pin.
  259. *
  260. * @return Address of SET task.
  261. */
  262. uint32_t nrfx_gpiote_set_task_addr_get(nrfx_gpiote_pin_t pin);
  263. #endif // defined(GPIOTE_FEATURE_SET_PRESENT) || defined(__NRFX_DOXYGEN__)
  264. #if defined(GPIOTE_FEATURE_CLR_PRESENT) || defined(__NRFX_DOXYGEN__)
  265. /**
  266. * @brief Function for getting the address of a configurable GPIOTE task.
  267. *
  268. * @param[in] pin Pin.
  269. *
  270. * @return Address of CLR task.
  271. */
  272. uint32_t nrfx_gpiote_clr_task_addr_get(nrfx_gpiote_pin_t pin);
  273. #endif // defined(GPIOTE_FEATURE_CLR_PRESENT) || defined(__NRFX_DOXYGEN__)
  274. /**
  275. * @brief Function for initializing a GPIOTE input pin.
  276. * @details The input pin can act in two ways:
  277. * - lower accuracy but low power (high frequency clock not needed)
  278. * - higher accuracy (high frequency clock required)
  279. *
  280. * The initial configuration specifies which mode is used.
  281. * If high-accuracy mode is used, the driver attempts to allocate one
  282. * of the available GPIOTE channels. If no channel is
  283. * available, an error is returned.
  284. * In low accuracy mode SENSE feature is used. In this case only one active pin
  285. * can be detected at a time. It can be worked around by setting all of the used
  286. * low accuracy pins to toggle mode.
  287. * For more information about SENSE functionality, refer to Product Specification.
  288. *
  289. * @param[in] pin Pin.
  290. * @param[in] p_config Initial configuration.
  291. * @param[in] evt_handler User function to be called when the configured transition occurs.
  292. *
  293. * @retval NRFX_SUCCESS If initialization was successful.
  294. * @retval NRFX_ERROR_INVALID_STATE If the driver is not initialized or the pin is already used.
  295. * @retval NRFX_ERROR_NO_MEM If no GPIOTE channel is available.
  296. */
  297. nrfx_err_t nrfx_gpiote_in_init(nrfx_gpiote_pin_t pin,
  298. nrfx_gpiote_in_config_t const * p_config,
  299. nrfx_gpiote_evt_handler_t evt_handler);
  300. /**
  301. * @brief Function for uninitializing a GPIOTE input pin.
  302. * @details The driver frees the GPIOTE channel if the input pin was using one.
  303. *
  304. * @param[in] pin Pin.
  305. */
  306. void nrfx_gpiote_in_uninit(nrfx_gpiote_pin_t pin);
  307. /**
  308. * @brief Function for enabling sensing of a GPIOTE input pin.
  309. *
  310. * @details If the input pin is configured as high-accuracy pin, the function
  311. * enables an IN_EVENT. Otherwise, the function enables the GPIO sense mechanism.
  312. * Note that a PORT event is shared between multiple pins, therefore the
  313. * interrupt is always enabled.
  314. *
  315. * @param[in] pin Pin.
  316. * @param[in] int_enable True to enable the interrupt. Always valid for a high-accuracy pin.
  317. */
  318. void nrfx_gpiote_in_event_enable(nrfx_gpiote_pin_t pin, bool int_enable);
  319. /**
  320. * @brief Function for disabling a GPIOTE input pin.
  321. *
  322. * @param[in] pin Pin.
  323. */
  324. void nrfx_gpiote_in_event_disable(nrfx_gpiote_pin_t pin);
  325. /**
  326. * @brief Function for checking if a GPIOTE input pin is set.
  327. *
  328. * @param[in] pin Pin.
  329. *
  330. * @retval true If the input pin is set.
  331. * @retval false If the input pin is not set.
  332. */
  333. bool nrfx_gpiote_in_is_set(nrfx_gpiote_pin_t pin);
  334. /**
  335. * @brief Function for getting the address of a GPIOTE input pin event.
  336. * @details If the pin is configured to use low-accuracy mode, the address of the PORT event is returned.
  337. *
  338. * @param[in] pin Pin.
  339. */
  340. uint32_t nrfx_gpiote_in_event_addr_get(nrfx_gpiote_pin_t pin);
  341. /**
  342. * @brief Function for forcing a specific state on the pin configured as task.
  343. *
  344. * @param[in] pin Pin.
  345. * @param[in] state Pin state.
  346. */
  347. void nrfx_gpiote_out_task_force(nrfx_gpiote_pin_t pin, uint8_t state);
  348. /**
  349. * @brief Function for triggering the task OUT manually.
  350. *
  351. * @param[in] pin Pin.
  352. */
  353. void nrfx_gpiote_out_task_trigger(nrfx_gpiote_pin_t pin);
  354. #if defined(GPIOTE_FEATURE_SET_PRESENT) || defined(__NRFX_DOXYGEN__)
  355. /**
  356. * @brief Function for triggering the task SET manually.
  357. *
  358. * @param[in] pin Pin.
  359. */
  360. void nrfx_gpiote_set_task_trigger(nrfx_gpiote_pin_t pin);
  361. #endif // defined(GPIOTE_FEATURE_SET_PRESENT) || defined(__NRFX_DOXYGEN__)
  362. #if defined(GPIOTE_FEATURE_CLR_PRESENT) || defined(__NRFX_DOXYGEN__)
  363. /**
  364. * @brief Function for triggering the task CLR manually.
  365. *
  366. * @param[in] pin Pin.
  367. */
  368. void nrfx_gpiote_clr_task_trigger(nrfx_gpiote_pin_t pin);
  369. #endif // defined(GPIOTE_FEATURE_CLR_PRESENT) || defined(__NRFX_DOXYGEN__)
  370. void nrfx_gpiote_irq_handler(void);
  371. /** @} */
  372. #ifdef __cplusplus
  373. }
  374. #endif
  375. #endif // NRFX_GPIOTE_H__