nrfx_gpiote.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /**
  2. * Copyright (c) 2015 - 2020, 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 GPIO Task Event (GPIOTE) peripheral driver.
  53. */
  54. /** @brief Input pin configuration. */
  55. typedef struct
  56. {
  57. nrf_gpiote_polarity_t sense; /**< Transition that triggers the 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. /**
  64. * @brief Macro for configuring a pin to use a GPIO IN or PORT EVENT to detect low-to-high transition.
  65. * @details Set hi_accu to true to use IN_EVENT.
  66. */
  67. #define NRFX_GPIOTE_CONFIG_IN_SENSE_LOTOHI(hi_accu) \
  68. { \
  69. .sense = NRF_GPIOTE_POLARITY_LOTOHI, \
  70. .pull = NRF_GPIO_PIN_NOPULL, \
  71. .is_watcher = false, \
  72. .hi_accuracy = hi_accu, \
  73. .skip_gpio_setup = false, \
  74. }
  75. /**
  76. * @brief Macro for configuring a pin to use a GPIO IN or PORT EVENT to detect high-to-low transition.
  77. * @details Set hi_accu to true to use IN_EVENT.
  78. */
  79. #define NRFX_GPIOTE_CONFIG_IN_SENSE_HITOLO(hi_accu) \
  80. { \
  81. .sense = NRF_GPIOTE_POLARITY_HITOLO, \
  82. .pull = NRF_GPIO_PIN_NOPULL, \
  83. .is_watcher = false, \
  84. .hi_accuracy = hi_accu, \
  85. .skip_gpio_setup = false, \
  86. }
  87. /**
  88. * @brief Macro for configuring a pin to use a GPIO IN or PORT EVENT to detect any change on the pin.
  89. * @details Set hi_accu to true to use IN_EVENT.
  90. */
  91. #define NRFX_GPIOTE_CONFIG_IN_SENSE_TOGGLE(hi_accu) \
  92. { \
  93. .sense = NRF_GPIOTE_POLARITY_TOGGLE, \
  94. .pull = NRF_GPIO_PIN_NOPULL, \
  95. .is_watcher = false, \
  96. .hi_accuracy = hi_accu, \
  97. .skip_gpio_setup = false, \
  98. }
  99. /**
  100. * @brief Macro for configuring a pin to use a GPIO IN or PORT EVENT to detect low-to-high transition.
  101. * @details Set hi_accu to true to use IN_EVENT.
  102. * @note This macro prepares configuration that skips the GPIO setup.
  103. */
  104. #define NRFX_GPIOTE_RAW_CONFIG_IN_SENSE_LOTOHI(hi_accu) \
  105. { \
  106. .sense = NRF_GPIOTE_POLARITY_LOTOHI, \
  107. .pull = NRF_GPIO_PIN_NOPULL, \
  108. .is_watcher = false, \
  109. .hi_accuracy = hi_accu, \
  110. .skip_gpio_setup = true, \
  111. }
  112. /**
  113. * @brief Macro for configuring a pin to use a GPIO IN or PORT EVENT to detect high-to-low transition.
  114. * @details Set hi_accu to true to use IN_EVENT.
  115. * @note This macro prepares configuration that skips the GPIO setup.
  116. */
  117. #define NRFX_GPIOTE_RAW_CONFIG_IN_SENSE_HITOLO(hi_accu) \
  118. { \
  119. .sense = NRF_GPIOTE_POLARITY_HITOLO, \
  120. .pull = NRF_GPIO_PIN_NOPULL, \
  121. .is_watcher = false, \
  122. .hi_accuracy = hi_accu, \
  123. .skip_gpio_setup = true, \
  124. }
  125. /**
  126. * @brief Macro for configuring a pin to use a GPIO IN or PORT EVENT to detect any change on the pin.
  127. * @details Set hi_accu to true to use IN_EVENT.
  128. * @note This macro prepares configuration that skips the GPIO setup.
  129. */
  130. #define NRFX_GPIOTE_RAW_CONFIG_IN_SENSE_TOGGLE(hi_accu) \
  131. { \
  132. .sense = NRF_GPIOTE_POLARITY_TOGGLE, \
  133. .pull = NRF_GPIO_PIN_NOPULL, \
  134. .is_watcher = false, \
  135. .hi_accuracy = hi_accu, \
  136. .skip_gpio_setup = true, \
  137. }
  138. /** @brief Output pin configuration. */
  139. typedef struct
  140. {
  141. nrf_gpiote_polarity_t action; /**< Configuration of the pin task. */
  142. nrf_gpiote_outinit_t init_state; /**< Initial state of the output pin. */
  143. bool task_pin; /**< True if the pin is controlled by a GPIOTE task. */
  144. } nrfx_gpiote_out_config_t;
  145. /** @brief Macro for configuring a pin to use as output. GPIOTE is not used for the pin. */
  146. #define NRFX_GPIOTE_CONFIG_OUT_SIMPLE(init_high) \
  147. { \
  148. .action = NRF_GPIOTE_POLARITY_LOTOHI, \
  149. .init_state = init_high ? NRF_GPIOTE_INITIAL_VALUE_HIGH : NRF_GPIOTE_INITIAL_VALUE_LOW, \
  150. .task_pin = false, \
  151. }
  152. /**
  153. * @brief Macro for configuring a pin to use the GPIO OUT TASK to change the state from high to low.
  154. * @details The task will clear the pin. Therefore, the pin is set initially.
  155. */
  156. #define NRFX_GPIOTE_CONFIG_OUT_TASK_LOW \
  157. { \
  158. .init_state = NRF_GPIOTE_INITIAL_VALUE_HIGH, \
  159. .task_pin = true, \
  160. .action = NRF_GPIOTE_POLARITY_HITOLO, \
  161. }
  162. /**
  163. * @brief Macro for configuring a pin to use the GPIO OUT TASK to change the state from low to high.
  164. * @details The task will set the pin. Therefore, the pin is cleared initially.
  165. */
  166. #define NRFX_GPIOTE_CONFIG_OUT_TASK_HIGH \
  167. { \
  168. .action = NRF_GPIOTE_POLARITY_LOTOHI, \
  169. .init_state = NRF_GPIOTE_INITIAL_VALUE_LOW, \
  170. .task_pin = true, \
  171. }
  172. /**
  173. * @brief Macro for configuring a pin to use the GPIO OUT TASK to toggle the pin state.
  174. * @details The initial pin state must be provided.
  175. */
  176. #define NRFX_GPIOTE_CONFIG_OUT_TASK_TOGGLE(init_high) \
  177. { \
  178. .action = NRF_GPIOTE_POLARITY_TOGGLE, \
  179. .init_state = init_high ? NRF_GPIOTE_INITIAL_VALUE_HIGH : NRF_GPIOTE_INITIAL_VALUE_LOW, \
  180. .task_pin = true, \
  181. }
  182. /** @brief Pin. */
  183. typedef uint32_t nrfx_gpiote_pin_t;
  184. /**
  185. * @brief Pin event handler prototype.
  186. *
  187. * @param[in] pin Pin that triggered this event.
  188. * @param[in] action Action that led to triggering this event.
  189. */
  190. typedef void (*nrfx_gpiote_evt_handler_t)(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action);
  191. /**
  192. * @brief Function for initializing the GPIOTE module.
  193. *
  194. * @details Only static configuration is supported to prevent the shared
  195. * resource being customized by the initiator.
  196. *
  197. * @retval NRFX_SUCCESS Initialization was successful.
  198. * @retval NRFX_ERROR_INVALID_STATE The driver was already initialized.
  199. */
  200. nrfx_err_t nrfx_gpiote_init(void);
  201. /**
  202. * @brief Function for checking if the GPIOTE module is initialized.
  203. *
  204. * @details The GPIOTE module is a shared module. Therefore, check if
  205. * the module is already initialized and skip initialization if it is.
  206. *
  207. * @retval true The module is already initialized.
  208. * @retval false The module is not initialized.
  209. */
  210. bool nrfx_gpiote_is_init(void);
  211. /** @brief Function for uninitializing the GPIOTE module. */
  212. void nrfx_gpiote_uninit(void);
  213. /**
  214. * @brief Function for initializing a GPIOTE output pin.
  215. * @details The output pin can be controlled by the CPU or by PPI. The initial
  216. * configuration specifies which mode is used. If PPI mode is used, the driver
  217. * attempts to allocate one of the available GPIOTE channels. If no channel is
  218. * available, an error is returned.
  219. *
  220. * @param[in] pin Pin.
  221. * @param[in] p_config Initial configuration.
  222. *
  223. * @retval NRFX_SUCCESS Initialization was successful.
  224. * @retval NRFX_ERROR_INVALID_STATE The driver is not initialized or the pin is already used.
  225. * @retval NRFX_ERROR_NO_MEM No GPIOTE channel is available.
  226. */
  227. nrfx_err_t nrfx_gpiote_out_init(nrfx_gpiote_pin_t pin,
  228. nrfx_gpiote_out_config_t const * p_config);
  229. /**
  230. * @brief Function for uninitializing a GPIOTE output pin.
  231. * @details The driver frees the GPIOTE channel if the output pin was using one.
  232. *
  233. * @param[in] pin Pin.
  234. */
  235. void nrfx_gpiote_out_uninit(nrfx_gpiote_pin_t pin);
  236. /**
  237. * @brief Function for setting a GPIOTE output pin.
  238. *
  239. * @param[in] pin Pin.
  240. */
  241. void nrfx_gpiote_out_set(nrfx_gpiote_pin_t pin);
  242. /**
  243. * @brief Function for clearing a GPIOTE output pin.
  244. *
  245. * @param[in] pin Pin.
  246. */
  247. void nrfx_gpiote_out_clear(nrfx_gpiote_pin_t pin);
  248. /**
  249. * @brief Function for toggling a GPIOTE output pin.
  250. *
  251. * @param[in] pin Pin.
  252. */
  253. void nrfx_gpiote_out_toggle(nrfx_gpiote_pin_t pin);
  254. /**
  255. * @brief Function for enabling a GPIOTE output pin task.
  256. *
  257. * @param[in] pin Pin.
  258. */
  259. void nrfx_gpiote_out_task_enable(nrfx_gpiote_pin_t pin);
  260. /**
  261. * @brief Function for disabling a GPIOTE output pin task.
  262. *
  263. * @param[in] pin Pin.
  264. */
  265. void nrfx_gpiote_out_task_disable(nrfx_gpiote_pin_t pin);
  266. /**
  267. * @brief Function for getting the OUT task for the specified output pin.
  268. *
  269. * @details The returned task identifier can be used within @ref nrf_gpiote_hal,
  270. * for example, to configure a DPPI channel.
  271. *
  272. * @param[in] pin Pin.
  273. *
  274. * @return OUT task associated with the specified output pin.
  275. */
  276. nrf_gpiote_tasks_t nrfx_gpiote_out_task_get(nrfx_gpiote_pin_t pin);
  277. /**
  278. * @brief Function for getting the address of the OUT task for the specified output pin.
  279. *
  280. * @param[in] pin Pin.
  281. *
  282. * @return Address of OUT task.
  283. */
  284. uint32_t nrfx_gpiote_out_task_addr_get(nrfx_gpiote_pin_t pin);
  285. #if defined(GPIOTE_FEATURE_SET_PRESENT) || defined(__NRFX_DOXYGEN__)
  286. /**
  287. * @brief Function for getting the SET task for the specified output pin.
  288. *
  289. * @details The returned task identifier can be used within @ref nrf_gpiote_hal,
  290. * for example, to configure a DPPI channel.
  291. *
  292. * @param[in] pin Pin.
  293. *
  294. * @return SET task associated with the specified output pin.
  295. */
  296. nrf_gpiote_tasks_t nrfx_gpiote_set_task_get(nrfx_gpiote_pin_t pin);
  297. /**
  298. * @brief Function for getting the address of the SET task for the specified output pin.
  299. *
  300. * @param[in] pin Pin.
  301. *
  302. * @return Address of SET task.
  303. */
  304. uint32_t nrfx_gpiote_set_task_addr_get(nrfx_gpiote_pin_t pin);
  305. #endif // defined(GPIOTE_FEATURE_SET_PRESENT) || defined(__NRFX_DOXYGEN__)
  306. #if defined(GPIOTE_FEATURE_CLR_PRESENT) || defined(__NRFX_DOXYGEN__)
  307. /**
  308. * @brief Function for getting the CLR task for the specified output pin.
  309. *
  310. * @details The returned task identifier can be used within @ref nrf_gpiote_hal,
  311. * for example, to configure a DPPI channel.
  312. *
  313. * @param[in] pin Pin.
  314. *
  315. * @return CLR task associated with the specified output pin.
  316. */
  317. nrf_gpiote_tasks_t nrfx_gpiote_clr_task_get(nrfx_gpiote_pin_t pin);
  318. /**
  319. * @brief Function for getting the address of the SET task for the specified output pin.
  320. *
  321. * @param[in] pin Pin.
  322. *
  323. * @return Address of CLR task.
  324. */
  325. uint32_t nrfx_gpiote_clr_task_addr_get(nrfx_gpiote_pin_t pin);
  326. #endif // defined(GPIOTE_FEATURE_CLR_PRESENT) || defined(__NRFX_DOXYGEN__)
  327. /**
  328. * @brief Function for initializing a GPIOTE input pin.
  329. * @details The input pin can act in two ways:
  330. * - lower accuracy but low power (high frequency clock not needed)
  331. * - higher accuracy (high frequency clock required)
  332. *
  333. * The initial configuration specifies which mode is used.
  334. * If high-accuracy mode is used, the driver attempts to allocate one
  335. * of the available GPIOTE channels. If no channel is
  336. * available, an error is returned.
  337. * In low accuracy mode SENSE feature is used. In this case, only one active pin
  338. * can be detected at a time. It can be worked around by setting all of the used
  339. * low accuracy pins to toggle mode.
  340. * For more information about SENSE functionality, refer to Product Specification.
  341. *
  342. * @param[in] pin Pin.
  343. * @param[in] p_config Initial configuration.
  344. * @param[in] evt_handler User function to be called when the configured transition occurs.
  345. *
  346. * @retval NRFX_SUCCESS Initialization was successful.
  347. * @retval NRFX_ERROR_INVALID_STATE The driver is not initialized or the pin is already used.
  348. * @retval NRFX_ERROR_NO_MEM No GPIOTE channel is available.
  349. */
  350. nrfx_err_t nrfx_gpiote_in_init(nrfx_gpiote_pin_t pin,
  351. nrfx_gpiote_in_config_t const * p_config,
  352. nrfx_gpiote_evt_handler_t evt_handler);
  353. /**
  354. * @brief Function for uninitializing a GPIOTE input pin.
  355. * @details The driver frees the GPIOTE channel if the input pin was using one.
  356. *
  357. * @param[in] pin Pin.
  358. */
  359. void nrfx_gpiote_in_uninit(nrfx_gpiote_pin_t pin);
  360. /**
  361. * @brief Function for enabling sensing of a GPIOTE input pin.
  362. *
  363. * @details If the input pin is configured as high-accuracy pin, the function
  364. * enables an IN_EVENT. Otherwise, the function enables the GPIO sense mechanism.
  365. * The PORT event is shared between multiple pins, therefore the interrupt is always enabled.
  366. *
  367. * @param[in] pin Pin.
  368. * @param[in] int_enable True to enable the interrupt. Always valid for a high-accuracy pin.
  369. */
  370. void nrfx_gpiote_in_event_enable(nrfx_gpiote_pin_t pin, bool int_enable);
  371. /**
  372. * @brief Function for disabling a GPIOTE input pin.
  373. *
  374. * @param[in] pin Pin.
  375. */
  376. void nrfx_gpiote_in_event_disable(nrfx_gpiote_pin_t pin);
  377. /**
  378. * @brief Function for checking if a GPIOTE input pin is set.
  379. *
  380. * @param[in] pin Pin.
  381. *
  382. * @retval true The input pin is set.
  383. * @retval false The input pin is not set.
  384. */
  385. bool nrfx_gpiote_in_is_set(nrfx_gpiote_pin_t pin);
  386. /**
  387. * @brief Function for getting the GPIOTE event for the specified input pin.
  388. *
  389. * @details The returned event identifier can be used within @ref nrf_gpiote_hal,
  390. * for example, to configure a DPPI channel.
  391. * If the pin is configured to use low-accuracy mode, the PORT event
  392. * is returned.
  393. *
  394. * @param[in] pin Pin.
  395. *
  396. * @return Event associated with the specified input pin.
  397. */
  398. nrf_gpiote_events_t nrfx_gpiote_in_event_get(nrfx_gpiote_pin_t pin);
  399. /**
  400. * @brief Function for getting the address of a GPIOTE input pin event.
  401. * @details If the pin is configured to use low-accuracy mode, the address of the PORT event is returned.
  402. *
  403. * @param[in] pin Pin.
  404. *
  405. * @return Address of the specified input pin event.
  406. */
  407. uint32_t nrfx_gpiote_in_event_addr_get(nrfx_gpiote_pin_t pin);
  408. /**
  409. * @brief Function for forcing a specific state on the pin configured as task.
  410. *
  411. * @param[in] pin Pin.
  412. * @param[in] state Pin state.
  413. */
  414. void nrfx_gpiote_out_task_force(nrfx_gpiote_pin_t pin, uint8_t state);
  415. /**
  416. * @brief Function for triggering the task OUT manually.
  417. *
  418. * @param[in] pin Pin.
  419. */
  420. void nrfx_gpiote_out_task_trigger(nrfx_gpiote_pin_t pin);
  421. #if defined(GPIOTE_FEATURE_SET_PRESENT) || defined(__NRFX_DOXYGEN__)
  422. /**
  423. * @brief Function for triggering the task SET manually.
  424. *
  425. * @param[in] pin Pin.
  426. */
  427. void nrfx_gpiote_set_task_trigger(nrfx_gpiote_pin_t pin);
  428. #endif // defined(GPIOTE_FEATURE_SET_PRESENT) || defined(__NRFX_DOXYGEN__)
  429. #if defined(GPIOTE_FEATURE_CLR_PRESENT) || defined(__NRFX_DOXYGEN__)
  430. /**
  431. * @brief Function for triggering the task CLR manually.
  432. *
  433. * @param[in] pin Pin.
  434. */
  435. void nrfx_gpiote_clr_task_trigger(nrfx_gpiote_pin_t pin);
  436. #endif // defined(GPIOTE_FEATURE_CLR_PRESENT) || defined(__NRFX_DOXYGEN__)
  437. /** @} */
  438. void nrfx_gpiote_irq_handler(void);
  439. #ifdef __cplusplus
  440. }
  441. #endif
  442. #endif // NRFX_GPIOTE_H__