nrf_gpiote.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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 NRF_GPIOTE_H__
  41. #define NRF_GPIOTE_H__
  42. #include <nrfx.h>
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. /**
  47. * @defgroup nrf_gpiote_hal GPIOTE HAL
  48. * @{
  49. * @ingroup nrf_gpiote
  50. * @brief Hardware access layer for managing the GPIOTE peripheral.
  51. */
  52. #ifdef GPIOTE_CONFIG_PORT_Msk
  53. #define GPIOTE_CONFIG_PORT_PIN_Msk (GPIOTE_CONFIG_PORT_Msk | GPIOTE_CONFIG_PSEL_Msk)
  54. #else
  55. #define GPIOTE_CONFIG_PORT_PIN_Msk GPIOTE_CONFIG_PSEL_Msk
  56. #endif
  57. /**
  58. * @enum nrf_gpiote_polarity_t
  59. * @brief Polarity for the GPIOTE channel.
  60. */
  61. typedef enum
  62. {
  63. NRF_GPIOTE_POLARITY_LOTOHI = GPIOTE_CONFIG_POLARITY_LoToHi, ///< Low to high.
  64. NRF_GPIOTE_POLARITY_HITOLO = GPIOTE_CONFIG_POLARITY_HiToLo, ///< High to low.
  65. NRF_GPIOTE_POLARITY_TOGGLE = GPIOTE_CONFIG_POLARITY_Toggle ///< Toggle.
  66. } nrf_gpiote_polarity_t;
  67. /**
  68. * @enum nrf_gpiote_outinit_t
  69. * @brief Initial output value for the GPIOTE channel.
  70. */
  71. typedef enum
  72. {
  73. NRF_GPIOTE_INITIAL_VALUE_LOW = GPIOTE_CONFIG_OUTINIT_Low, ///< Low to high.
  74. NRF_GPIOTE_INITIAL_VALUE_HIGH = GPIOTE_CONFIG_OUTINIT_High ///< High to low.
  75. } nrf_gpiote_outinit_t;
  76. /**
  77. * @brief Tasks.
  78. */
  79. typedef enum /*lint -save -e30 -esym(628,__INTADDR__) */
  80. {
  81. NRF_GPIOTE_TASKS_OUT_0 = offsetof(NRF_GPIOTE_Type, TASKS_OUT[0]), /**< Out task 0.*/
  82. NRF_GPIOTE_TASKS_OUT_1 = offsetof(NRF_GPIOTE_Type, TASKS_OUT[1]), /**< Out task 1.*/
  83. NRF_GPIOTE_TASKS_OUT_2 = offsetof(NRF_GPIOTE_Type, TASKS_OUT[2]), /**< Out task 2.*/
  84. NRF_GPIOTE_TASKS_OUT_3 = offsetof(NRF_GPIOTE_Type, TASKS_OUT[3]), /**< Out task 3.*/
  85. #if (GPIOTE_CH_NUM > 4) || defined(__NRFX_DOXYGEN__)
  86. NRF_GPIOTE_TASKS_OUT_4 = offsetof(NRF_GPIOTE_Type, TASKS_OUT[4]), /**< Out task 4.*/
  87. NRF_GPIOTE_TASKS_OUT_5 = offsetof(NRF_GPIOTE_Type, TASKS_OUT[5]), /**< Out task 5.*/
  88. NRF_GPIOTE_TASKS_OUT_6 = offsetof(NRF_GPIOTE_Type, TASKS_OUT[6]), /**< Out task 6.*/
  89. NRF_GPIOTE_TASKS_OUT_7 = offsetof(NRF_GPIOTE_Type, TASKS_OUT[7]), /**< Out task 7.*/
  90. #endif
  91. #if defined(GPIOTE_FEATURE_SET_PRESENT) || defined(__NRFX_DOXYGEN__)
  92. NRF_GPIOTE_TASKS_SET_0 = offsetof(NRF_GPIOTE_Type, TASKS_SET[0]), /**< Set task 0.*/
  93. NRF_GPIOTE_TASKS_SET_1 = offsetof(NRF_GPIOTE_Type, TASKS_SET[1]), /**< Set task 1.*/
  94. NRF_GPIOTE_TASKS_SET_2 = offsetof(NRF_GPIOTE_Type, TASKS_SET[2]), /**< Set task 2.*/
  95. NRF_GPIOTE_TASKS_SET_3 = offsetof(NRF_GPIOTE_Type, TASKS_SET[3]), /**< Set task 3.*/
  96. NRF_GPIOTE_TASKS_SET_4 = offsetof(NRF_GPIOTE_Type, TASKS_SET[4]), /**< Set task 4.*/
  97. NRF_GPIOTE_TASKS_SET_5 = offsetof(NRF_GPIOTE_Type, TASKS_SET[5]), /**< Set task 5.*/
  98. NRF_GPIOTE_TASKS_SET_6 = offsetof(NRF_GPIOTE_Type, TASKS_SET[6]), /**< Set task 6.*/
  99. NRF_GPIOTE_TASKS_SET_7 = offsetof(NRF_GPIOTE_Type, TASKS_SET[7]), /**< Set task 7.*/
  100. #endif
  101. #if defined(GPIOTE_FEATURE_CLR_PRESENT) || defined(__NRFX_DOXYGEN__)
  102. NRF_GPIOTE_TASKS_CLR_0 = offsetof(NRF_GPIOTE_Type, TASKS_CLR[0]), /**< Clear task 0.*/
  103. NRF_GPIOTE_TASKS_CLR_1 = offsetof(NRF_GPIOTE_Type, TASKS_CLR[1]), /**< Clear task 1.*/
  104. NRF_GPIOTE_TASKS_CLR_2 = offsetof(NRF_GPIOTE_Type, TASKS_CLR[2]), /**< Clear task 2.*/
  105. NRF_GPIOTE_TASKS_CLR_3 = offsetof(NRF_GPIOTE_Type, TASKS_CLR[3]), /**< Clear task 3.*/
  106. NRF_GPIOTE_TASKS_CLR_4 = offsetof(NRF_GPIOTE_Type, TASKS_CLR[4]), /**< Clear task 4.*/
  107. NRF_GPIOTE_TASKS_CLR_5 = offsetof(NRF_GPIOTE_Type, TASKS_CLR[5]), /**< Clear task 5.*/
  108. NRF_GPIOTE_TASKS_CLR_6 = offsetof(NRF_GPIOTE_Type, TASKS_CLR[6]), /**< Clear task 6.*/
  109. NRF_GPIOTE_TASKS_CLR_7 = offsetof(NRF_GPIOTE_Type, TASKS_CLR[7]), /**< Clear task 7.*/
  110. #endif
  111. /*lint -restore*/
  112. } nrf_gpiote_tasks_t;
  113. /**
  114. * @brief Events.
  115. */
  116. typedef enum /*lint -save -e30 -esym(628,__INTADDR__) */
  117. {
  118. NRF_GPIOTE_EVENTS_IN_0 = offsetof(NRF_GPIOTE_Type, EVENTS_IN[0]), /**< In event 0.*/
  119. NRF_GPIOTE_EVENTS_IN_1 = offsetof(NRF_GPIOTE_Type, EVENTS_IN[1]), /**< In event 1.*/
  120. NRF_GPIOTE_EVENTS_IN_2 = offsetof(NRF_GPIOTE_Type, EVENTS_IN[2]), /**< In event 2.*/
  121. NRF_GPIOTE_EVENTS_IN_3 = offsetof(NRF_GPIOTE_Type, EVENTS_IN[3]), /**< In event 3.*/
  122. #if (GPIOTE_CH_NUM > 4) || defined(__NRFX_DOXYGEN__)
  123. NRF_GPIOTE_EVENTS_IN_4 = offsetof(NRF_GPIOTE_Type, EVENTS_IN[4]), /**< In event 4.*/
  124. NRF_GPIOTE_EVENTS_IN_5 = offsetof(NRF_GPIOTE_Type, EVENTS_IN[5]), /**< In event 5.*/
  125. NRF_GPIOTE_EVENTS_IN_6 = offsetof(NRF_GPIOTE_Type, EVENTS_IN[6]), /**< In event 6.*/
  126. NRF_GPIOTE_EVENTS_IN_7 = offsetof(NRF_GPIOTE_Type, EVENTS_IN[7]), /**< In event 7.*/
  127. #endif
  128. NRF_GPIOTE_EVENTS_PORT = offsetof(NRF_GPIOTE_Type, EVENTS_PORT), /**< Port event.*/
  129. /*lint -restore*/
  130. } nrf_gpiote_events_t;
  131. /**
  132. * @enum nrf_gpiote_int_t
  133. * @brief GPIOTE interrupts.
  134. */
  135. typedef enum
  136. {
  137. NRF_GPIOTE_INT_IN0_MASK = GPIOTE_INTENSET_IN0_Msk, /**< GPIOTE interrupt from IN0. */
  138. NRF_GPIOTE_INT_IN1_MASK = GPIOTE_INTENSET_IN1_Msk, /**< GPIOTE interrupt from IN1. */
  139. NRF_GPIOTE_INT_IN2_MASK = GPIOTE_INTENSET_IN2_Msk, /**< GPIOTE interrupt from IN2. */
  140. NRF_GPIOTE_INT_IN3_MASK = GPIOTE_INTENSET_IN3_Msk, /**< GPIOTE interrupt from IN3. */
  141. #if (GPIOTE_CH_NUM > 4) || defined(__NRFX_DOXYGEN__)
  142. NRF_GPIOTE_INT_IN4_MASK = GPIOTE_INTENSET_IN4_Msk, /**< GPIOTE interrupt from IN4. */
  143. NRF_GPIOTE_INT_IN5_MASK = GPIOTE_INTENSET_IN5_Msk, /**< GPIOTE interrupt from IN5. */
  144. NRF_GPIOTE_INT_IN6_MASK = GPIOTE_INTENSET_IN6_Msk, /**< GPIOTE interrupt from IN6. */
  145. NRF_GPIOTE_INT_IN7_MASK = GPIOTE_INTENSET_IN7_Msk, /**< GPIOTE interrupt from IN7. */
  146. #endif
  147. NRF_GPIOTE_INT_PORT_MASK = (int)GPIOTE_INTENSET_PORT_Msk, /**< GPIOTE interrupt from PORT event. */
  148. } nrf_gpiote_int_t;
  149. #define NRF_GPIOTE_INT_IN_MASK (NRF_GPIOTE_INT_IN0_MASK | NRF_GPIOTE_INT_IN1_MASK |\
  150. NRF_GPIOTE_INT_IN2_MASK | NRF_GPIOTE_INT_IN3_MASK)
  151. #if (GPIOTE_CH_NUM > 4)
  152. #undef NRF_GPIOTE_INT_IN_MASK
  153. #define NRF_GPIOTE_INT_IN_MASK (NRF_GPIOTE_INT_IN0_MASK | NRF_GPIOTE_INT_IN1_MASK |\
  154. NRF_GPIOTE_INT_IN2_MASK | NRF_GPIOTE_INT_IN3_MASK |\
  155. NRF_GPIOTE_INT_IN4_MASK | NRF_GPIOTE_INT_IN5_MASK |\
  156. NRF_GPIOTE_INT_IN6_MASK | NRF_GPIOTE_INT_IN7_MASK)
  157. #endif
  158. /**
  159. * @brief Function for activating a specific GPIOTE task.
  160. *
  161. * @param[in] task Task.
  162. */
  163. __STATIC_INLINE void nrf_gpiote_task_set(nrf_gpiote_tasks_t task);
  164. /**
  165. * @brief Function for getting the address of a specific GPIOTE task.
  166. *
  167. * @param[in] task Task.
  168. *
  169. * @returns Address.
  170. */
  171. __STATIC_INLINE uint32_t nrf_gpiote_task_addr_get(nrf_gpiote_tasks_t task);
  172. /**
  173. * @brief Function for getting the state of a specific GPIOTE event.
  174. *
  175. * @param[in] event Event.
  176. */
  177. __STATIC_INLINE bool nrf_gpiote_event_is_set(nrf_gpiote_events_t event);
  178. /**
  179. * @brief Function for clearing a specific GPIOTE event.
  180. *
  181. * @param[in] event Event.
  182. */
  183. __STATIC_INLINE void nrf_gpiote_event_clear(nrf_gpiote_events_t event);
  184. /**
  185. * @brief Function for getting the address of a specific GPIOTE event.
  186. *
  187. * @param[in] event Event.
  188. *
  189. * @return Address
  190. */
  191. __STATIC_INLINE uint32_t nrf_gpiote_event_addr_get(nrf_gpiote_events_t event);
  192. /**@brief Function for enabling interrupts.
  193. *
  194. * @param[in] mask Interrupt mask to be enabled.
  195. */
  196. __STATIC_INLINE void nrf_gpiote_int_enable(uint32_t mask);
  197. /**@brief Function for disabling interrupts.
  198. *
  199. * @param[in] mask Interrupt mask to be disabled.
  200. */
  201. __STATIC_INLINE void nrf_gpiote_int_disable(uint32_t mask);
  202. /**@brief Function for checking if interrupts are enabled.
  203. *
  204. * @param[in] mask Mask of interrupt flags to check.
  205. *
  206. * @return Mask with enabled interrupts.
  207. */
  208. __STATIC_INLINE uint32_t nrf_gpiote_int_is_enabled(uint32_t mask);
  209. #if defined(DPPI_PRESENT) || defined(__NRFX_DOXYGEN__)
  210. /**
  211. * @brief Function for setting the subscribe configuration for a given
  212. * GPIOTE task.
  213. *
  214. * @param[in] task Task for which to set the configuration.
  215. * @param[in] channel Channel through which to subscribe events.
  216. */
  217. __STATIC_INLINE void nrf_gpiote_subscribe_set(nrf_gpiote_tasks_t task,
  218. uint8_t channel);
  219. /**
  220. * @brief Function for clearing the subscribe configuration for a given
  221. * GPIOTE task.
  222. *
  223. * @param[in] task Task for which to clear the configuration.
  224. */
  225. __STATIC_INLINE void nrf_gpiote_subscribe_clear(nrf_gpiote_tasks_t task);
  226. /**
  227. * @brief Function for setting the publish configuration for a given
  228. * GPIOTE event.
  229. *
  230. * @param[in] event Event for which to set the configuration.
  231. * @param[in] channel Channel through which to publish the event.
  232. */
  233. __STATIC_INLINE void nrf_gpiote_publish_set(nrf_gpiote_events_t event,
  234. uint8_t channel);
  235. /**
  236. * @brief Function for clearing the publish configuration for a given
  237. * GPIOTE event.
  238. *
  239. * @param[in] event Event for which to clear the configuration.
  240. */
  241. __STATIC_INLINE void nrf_gpiote_publish_clear(nrf_gpiote_events_t event);
  242. #endif // defined(DPPI_PRESENT) || defined(__NRFX_DOXYGEN__)
  243. /**@brief Function for enabling a GPIOTE event.
  244. *
  245. * @param[in] idx Task-Event index.
  246. */
  247. __STATIC_INLINE void nrf_gpiote_event_enable(uint32_t idx);
  248. /**@brief Function for disabling a GPIOTE event.
  249. *
  250. * @param[in] idx Task-Event index.
  251. */
  252. __STATIC_INLINE void nrf_gpiote_event_disable(uint32_t idx);
  253. /**@brief Function for configuring a GPIOTE event.
  254. *
  255. * @param[in] idx Task-Event index.
  256. * @param[in] pin Pin associated with event.
  257. * @param[in] polarity Transition that should generate an event.
  258. */
  259. __STATIC_INLINE void nrf_gpiote_event_configure(uint32_t idx, uint32_t pin,
  260. nrf_gpiote_polarity_t polarity);
  261. /**@brief Function for getting the pin associated with a GPIOTE event.
  262. *
  263. * @param[in] idx Task-Event index.
  264. *
  265. * @return Pin number.
  266. */
  267. __STATIC_INLINE uint32_t nrf_gpiote_event_pin_get(uint32_t idx);
  268. /**@brief Function for getting the polarity associated with a GPIOTE event.
  269. *
  270. * @param[in] idx Task-Event index.
  271. *
  272. * @return Polarity.
  273. */
  274. __STATIC_INLINE nrf_gpiote_polarity_t nrf_gpiote_event_polarity_get(uint32_t idx);
  275. /**@brief Function for enabling a GPIOTE task.
  276. *
  277. * @param[in] idx Task-Event index.
  278. */
  279. __STATIC_INLINE void nrf_gpiote_task_enable(uint32_t idx);
  280. /**@brief Function for disabling a GPIOTE task.
  281. *
  282. * @param[in] idx Task-Event index.
  283. */
  284. __STATIC_INLINE void nrf_gpiote_task_disable(uint32_t idx);
  285. /**@brief Function for configuring a GPIOTE task.
  286. * @note Function is not configuring mode field so task is disabled after this function is called.
  287. *
  288. * @param[in] idx Task-Event index.
  289. * @param[in] pin Pin associated with event.
  290. * @param[in] polarity Transition that should generate an event.
  291. * @param[in] init_val Initial value of the pin.
  292. */
  293. __STATIC_INLINE void nrf_gpiote_task_configure(uint32_t idx, uint32_t pin,
  294. nrf_gpiote_polarity_t polarity,
  295. nrf_gpiote_outinit_t init_val);
  296. /**@brief Function for forcing a specific state on the pin connected to GPIOTE.
  297. *
  298. * @param[in] idx Task-Event index.
  299. * @param[in] init_val Pin state.
  300. */
  301. __STATIC_INLINE void nrf_gpiote_task_force(uint32_t idx, nrf_gpiote_outinit_t init_val);
  302. /**@brief Function for resetting a GPIOTE task event configuration to the default state.
  303. *
  304. * @param[in] idx Task-Event index.
  305. */
  306. __STATIC_INLINE void nrf_gpiote_te_default(uint32_t idx);
  307. /**@brief Function for checking if particular Task-Event is enabled.
  308. *
  309. * @param[in] idx Task-Event index.
  310. *
  311. * @retval true If the Task-Event mode is set to Task or Event.
  312. * @retval false If the Task-Event mode is set to Disabled.
  313. */
  314. __STATIC_INLINE bool nrf_gpiote_te_is_enabled(uint32_t idx);
  315. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  316. __STATIC_INLINE void nrf_gpiote_task_set(nrf_gpiote_tasks_t task)
  317. {
  318. *(__IO uint32_t *)((uint32_t)NRF_GPIOTE + task) = 0x1UL;
  319. }
  320. __STATIC_INLINE uint32_t nrf_gpiote_task_addr_get(nrf_gpiote_tasks_t task)
  321. {
  322. return ((uint32_t)NRF_GPIOTE + task);
  323. }
  324. __STATIC_INLINE bool nrf_gpiote_event_is_set(nrf_gpiote_events_t event)
  325. {
  326. return (*(uint32_t *)nrf_gpiote_event_addr_get(event) == 0x1UL) ? true : false;
  327. }
  328. __STATIC_INLINE void nrf_gpiote_event_clear(nrf_gpiote_events_t event)
  329. {
  330. *(uint32_t *)nrf_gpiote_event_addr_get(event) = 0;
  331. #if __CORTEX_M == 0x04
  332. volatile uint32_t dummy = *((volatile uint32_t *)nrf_gpiote_event_addr_get(event));
  333. (void)dummy;
  334. #endif
  335. }
  336. __STATIC_INLINE uint32_t nrf_gpiote_event_addr_get(nrf_gpiote_events_t event)
  337. {
  338. return ((uint32_t)NRF_GPIOTE + event);
  339. }
  340. __STATIC_INLINE void nrf_gpiote_int_enable(uint32_t mask)
  341. {
  342. NRF_GPIOTE->INTENSET = mask;
  343. }
  344. __STATIC_INLINE void nrf_gpiote_int_disable(uint32_t mask)
  345. {
  346. NRF_GPIOTE->INTENCLR = mask;
  347. }
  348. __STATIC_INLINE uint32_t nrf_gpiote_int_is_enabled(uint32_t mask)
  349. {
  350. return (NRF_GPIOTE->INTENSET & mask);
  351. }
  352. #if defined(DPPI_PRESENT)
  353. __STATIC_INLINE void nrf_gpiote_subscribe_set(nrf_gpiote_tasks_t task,
  354. uint8_t channel)
  355. {
  356. *((volatile uint32_t *) ((uint8_t *) NRF_GPIOTE + (uint32_t) task + 0x80uL)) =
  357. ((uint32_t)channel | GPIOTE_SUBSCRIBE_OUT_EN_Msk);
  358. }
  359. __STATIC_INLINE void nrf_gpiote_subscribe_clear(nrf_gpiote_tasks_t task)
  360. {
  361. *((volatile uint32_t *) ((uint8_t *) NRF_GPIOTE + (uint32_t) task + 0x80uL)) = 0;
  362. }
  363. __STATIC_INLINE void nrf_gpiote_publish_set(nrf_gpiote_events_t event,
  364. uint8_t channel)
  365. {
  366. *((volatile uint32_t *) ((uint8_t *) NRF_GPIOTE + (uint32_t) event + 0x80uL)) =
  367. ((uint32_t)channel | GPIOTE_PUBLISH_IN_EN_Msk);
  368. }
  369. __STATIC_INLINE void nrf_gpiote_publish_clear(nrf_gpiote_events_t event)
  370. {
  371. *((volatile uint32_t *) ((uint8_t *) NRF_GPIOTE + (uint32_t) event + 0x80uL)) = 0;
  372. }
  373. #endif // defined(DPPI_PRESENT)
  374. __STATIC_INLINE void nrf_gpiote_event_enable(uint32_t idx)
  375. {
  376. NRF_GPIOTE->CONFIG[idx] |= GPIOTE_CONFIG_MODE_Event;
  377. }
  378. __STATIC_INLINE void nrf_gpiote_event_disable(uint32_t idx)
  379. {
  380. NRF_GPIOTE->CONFIG[idx] &= ~GPIOTE_CONFIG_MODE_Event;
  381. }
  382. __STATIC_INLINE void nrf_gpiote_event_configure(uint32_t idx, uint32_t pin, nrf_gpiote_polarity_t polarity)
  383. {
  384. NRF_GPIOTE->CONFIG[idx] &= ~(GPIOTE_CONFIG_PORT_PIN_Msk | GPIOTE_CONFIG_POLARITY_Msk);
  385. NRF_GPIOTE->CONFIG[idx] |= ((pin << GPIOTE_CONFIG_PSEL_Pos) & GPIOTE_CONFIG_PORT_PIN_Msk) |
  386. ((polarity << GPIOTE_CONFIG_POLARITY_Pos) & GPIOTE_CONFIG_POLARITY_Msk);
  387. }
  388. __STATIC_INLINE uint32_t nrf_gpiote_event_pin_get(uint32_t idx)
  389. {
  390. return ((NRF_GPIOTE->CONFIG[idx] & GPIOTE_CONFIG_PORT_PIN_Msk) >> GPIOTE_CONFIG_PSEL_Pos);
  391. }
  392. __STATIC_INLINE nrf_gpiote_polarity_t nrf_gpiote_event_polarity_get(uint32_t idx)
  393. {
  394. return (nrf_gpiote_polarity_t)((NRF_GPIOTE->CONFIG[idx] & GPIOTE_CONFIG_POLARITY_Msk) >> GPIOTE_CONFIG_POLARITY_Pos);
  395. }
  396. __STATIC_INLINE void nrf_gpiote_task_enable(uint32_t idx)
  397. {
  398. uint32_t final_config = NRF_GPIOTE->CONFIG[idx] | GPIOTE_CONFIG_MODE_Task;
  399. #ifdef NRF51
  400. /* Workaround for the OUTINIT PAN. When nrf_gpiote_task_config() is called a glitch happens
  401. on the GPIO if the GPIO in question is already assigned to GPIOTE and the pin is in the
  402. correct state in GPIOTE but not in the OUT register. */
  403. /* Configure channel to not existing, not connected to the pin, and configure as a tasks that will set it to proper level */
  404. NRF_GPIOTE->CONFIG[idx] = final_config | (((31) << GPIOTE_CONFIG_PSEL_Pos) & GPIOTE_CONFIG_PORT_PIN_Msk);
  405. __NOP();
  406. __NOP();
  407. __NOP();
  408. #endif
  409. NRF_GPIOTE->CONFIG[idx] = final_config;
  410. }
  411. __STATIC_INLINE void nrf_gpiote_task_disable(uint32_t idx)
  412. {
  413. NRF_GPIOTE->CONFIG[idx] &= ~GPIOTE_CONFIG_MODE_Task;
  414. }
  415. __STATIC_INLINE void nrf_gpiote_task_configure(uint32_t idx, uint32_t pin,
  416. nrf_gpiote_polarity_t polarity,
  417. nrf_gpiote_outinit_t init_val)
  418. {
  419. NRF_GPIOTE->CONFIG[idx] &= ~(GPIOTE_CONFIG_PORT_PIN_Msk |
  420. GPIOTE_CONFIG_POLARITY_Msk |
  421. GPIOTE_CONFIG_OUTINIT_Msk);
  422. NRF_GPIOTE->CONFIG[idx] |= ((pin << GPIOTE_CONFIG_PSEL_Pos) & GPIOTE_CONFIG_PORT_PIN_Msk) |
  423. ((polarity << GPIOTE_CONFIG_POLARITY_Pos) & GPIOTE_CONFIG_POLARITY_Msk) |
  424. ((init_val << GPIOTE_CONFIG_OUTINIT_Pos) & GPIOTE_CONFIG_OUTINIT_Msk);
  425. }
  426. __STATIC_INLINE void nrf_gpiote_task_force(uint32_t idx, nrf_gpiote_outinit_t init_val)
  427. {
  428. NRF_GPIOTE->CONFIG[idx] = (NRF_GPIOTE->CONFIG[idx] & ~GPIOTE_CONFIG_OUTINIT_Msk)
  429. | ((init_val << GPIOTE_CONFIG_OUTINIT_Pos) & GPIOTE_CONFIG_OUTINIT_Msk);
  430. }
  431. __STATIC_INLINE void nrf_gpiote_te_default(uint32_t idx)
  432. {
  433. NRF_GPIOTE->CONFIG[idx] = 0;
  434. }
  435. __STATIC_INLINE bool nrf_gpiote_te_is_enabled(uint32_t idx)
  436. {
  437. return (NRF_GPIOTE->CONFIG[idx] & GPIOTE_CONFIG_MODE_Msk) != GPIOTE_CONFIG_MODE_Disabled;
  438. }
  439. #endif //SUPPRESS_INLINE_IMPLEMENTATION
  440. /** @} */
  441. #ifdef __cplusplus
  442. }
  443. #endif
  444. #endif