nrf_gpiote.h 21 KB

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