nrf_gpiote.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. #ifndef NRF_GPIOTE_H__
  13. #define NRF_GPIOTE_H__
  14. #include "nrf.h"
  15. #include <stdint.h>
  16. #include <stddef.h>
  17. #include <stdbool.h>
  18. /**
  19. * @defgroup nrf_gpiote_abs GPIOTE abstraction
  20. * @{
  21. * @ingroup nrf_gpiote
  22. * @brief GPIOTE abstraction for configuration of channels.
  23. */
  24. #ifdef NRF51
  25. #define NUMBER_OF_GPIO_TE 4
  26. #elif defined(NRF52)
  27. #define NUMBER_OF_GPIO_TE 8
  28. #else
  29. #error "Chip family not specified"
  30. #endif
  31. /**
  32. * @enum nrf_gpiote_polarity_t
  33. * @brief Polarity for the GPIOTE channel.
  34. */
  35. typedef enum
  36. {
  37. NRF_GPIOTE_POLARITY_LOTOHI = GPIOTE_CONFIG_POLARITY_LoToHi, ///< Low to high.
  38. NRF_GPIOTE_POLARITY_HITOLO = GPIOTE_CONFIG_POLARITY_HiToLo, ///< High to low.
  39. NRF_GPIOTE_POLARITY_TOGGLE = GPIOTE_CONFIG_POLARITY_Toggle ///< Toggle.
  40. } nrf_gpiote_polarity_t;
  41. /**
  42. * @enum nrf_gpiote_outinit_t
  43. * @brief Initial output value for the GPIOTE channel.
  44. */
  45. typedef enum
  46. {
  47. NRF_GPIOTE_INITIAL_VALUE_LOW = GPIOTE_CONFIG_OUTINIT_Low, ///< Low to high.
  48. NRF_GPIOTE_INITIAL_VALUE_HIGH = GPIOTE_CONFIG_OUTINIT_High ///< High to low.
  49. } nrf_gpiote_outinit_t;
  50. /**
  51. * @brief Tasks.
  52. */
  53. typedef enum /*lint -save -e30 -esym(628,__INTADDR__) */
  54. {
  55. NRF_GPIOTE_TASKS_OUT_0 = offsetof(NRF_GPIOTE_Type, TASKS_OUT[0]), /**< Out task 0.*/
  56. NRF_GPIOTE_TASKS_OUT_1 = offsetof(NRF_GPIOTE_Type, TASKS_OUT[1]), /**< Out task 1.*/
  57. NRF_GPIOTE_TASKS_OUT_2 = offsetof(NRF_GPIOTE_Type, TASKS_OUT[2]), /**< Out task 2.*/
  58. NRF_GPIOTE_TASKS_OUT_3 = offsetof(NRF_GPIOTE_Type, TASKS_OUT[3]), /**< Out task 3.*/
  59. #if (NUMBER_OF_GPIO_TE == 8)
  60. NRF_GPIOTE_TASKS_OUT_4 = offsetof(NRF_GPIOTE_Type, TASKS_OUT[4]), /**< Out task 4.*/
  61. NRF_GPIOTE_TASKS_OUT_5 = offsetof(NRF_GPIOTE_Type, TASKS_OUT[5]), /**< Out task 5.*/
  62. NRF_GPIOTE_TASKS_OUT_6 = offsetof(NRF_GPIOTE_Type, TASKS_OUT[6]), /**< Out task 6.*/
  63. NRF_GPIOTE_TASKS_OUT_7 = offsetof(NRF_GPIOTE_Type, TASKS_OUT[7]), /**< Out task 7.*/
  64. #endif
  65. #ifdef NRF52
  66. NRF_GPIOTE_TASKS_SET_0 = offsetof(NRF_GPIOTE_Type, TASKS_SET[0]), /**< Set task 0.*/
  67. NRF_GPIOTE_TASKS_SET_1 = offsetof(NRF_GPIOTE_Type, TASKS_SET[1]), /**< Set task 1.*/
  68. NRF_GPIOTE_TASKS_SET_2 = offsetof(NRF_GPIOTE_Type, TASKS_SET[2]), /**< Set task 2.*/
  69. NRF_GPIOTE_TASKS_SET_3 = offsetof(NRF_GPIOTE_Type, TASKS_SET[3]), /**< Set task 3.*/
  70. NRF_GPIOTE_TASKS_SET_4 = offsetof(NRF_GPIOTE_Type, TASKS_SET[4]), /**< Set task 4.*/
  71. NRF_GPIOTE_TASKS_SET_5 = offsetof(NRF_GPIOTE_Type, TASKS_SET[5]), /**< Set task 5.*/
  72. NRF_GPIOTE_TASKS_SET_6 = offsetof(NRF_GPIOTE_Type, TASKS_SET[6]), /**< Set task 6.*/
  73. NRF_GPIOTE_TASKS_SET_7 = offsetof(NRF_GPIOTE_Type, TASKS_SET[7]), /**< Set task 7.*/
  74. NRF_GPIOTE_TASKS_CLR_0 = offsetof(NRF_GPIOTE_Type, TASKS_CLR[0]), /**< Clear task 0.*/
  75. NRF_GPIOTE_TASKS_CLR_1 = offsetof(NRF_GPIOTE_Type, TASKS_CLR[1]), /**< Clear task 1.*/
  76. NRF_GPIOTE_TASKS_CLR_2 = offsetof(NRF_GPIOTE_Type, TASKS_CLR[2]), /**< Clear task 2.*/
  77. NRF_GPIOTE_TASKS_CLR_3 = offsetof(NRF_GPIOTE_Type, TASKS_CLR[3]), /**< Clear task 3.*/
  78. NRF_GPIOTE_TASKS_CLR_4 = offsetof(NRF_GPIOTE_Type, TASKS_CLR[4]), /**< Clear task 4.*/
  79. NRF_GPIOTE_TASKS_CLR_5 = offsetof(NRF_GPIOTE_Type, TASKS_CLR[5]), /**< Clear task 5.*/
  80. NRF_GPIOTE_TASKS_CLR_6 = offsetof(NRF_GPIOTE_Type, TASKS_CLR[6]), /**< Clear task 6.*/
  81. NRF_GPIOTE_TASKS_CLR_7 = offsetof(NRF_GPIOTE_Type, TASKS_CLR[7]), /**< Clear task 7.*/
  82. #endif
  83. /*lint -restore*/
  84. } nrf_gpiote_tasks_t;
  85. /**
  86. * @brief Events.
  87. */
  88. typedef enum /*lint -save -e30 -esym(628,__INTADDR__) */
  89. {
  90. NRF_GPIOTE_EVENTS_IN_0 = offsetof(NRF_GPIOTE_Type, EVENTS_IN[0]), /**< In event 0.*/
  91. NRF_GPIOTE_EVENTS_IN_1 = offsetof(NRF_GPIOTE_Type, EVENTS_IN[1]), /**< In event 1.*/
  92. NRF_GPIOTE_EVENTS_IN_2 = offsetof(NRF_GPIOTE_Type, EVENTS_IN[2]), /**< In event 2.*/
  93. NRF_GPIOTE_EVENTS_IN_3 = offsetof(NRF_GPIOTE_Type, EVENTS_IN[3]), /**< In event 3.*/
  94. #if (NUMBER_OF_GPIO_TE == 8)
  95. NRF_GPIOTE_EVENTS_IN_4 = offsetof(NRF_GPIOTE_Type, EVENTS_IN[4]), /**< In event 4.*/
  96. NRF_GPIOTE_EVENTS_IN_5 = offsetof(NRF_GPIOTE_Type, EVENTS_IN[5]), /**< In event 5.*/
  97. NRF_GPIOTE_EVENTS_IN_6 = offsetof(NRF_GPIOTE_Type, EVENTS_IN[6]), /**< In event 6.*/
  98. NRF_GPIOTE_EVENTS_IN_7 = offsetof(NRF_GPIOTE_Type, EVENTS_IN[7]), /**< In event 7.*/
  99. #endif
  100. NRF_GPIOTE_EVENTS_PORT = offsetof(NRF_GPIOTE_Type, EVENTS_PORT), /**< Port event.*/
  101. /*lint -restore*/
  102. } nrf_gpiote_events_t;
  103. /**
  104. * @enum nrf_gpiote_int_t
  105. * @brief GPIOTE interrupts.
  106. */
  107. typedef enum
  108. {
  109. NRF_GPIOTE_INT_IN0_MASK = GPIOTE_INTENSET_IN0_Msk, /**< GPIOTE interrupt from IN0. */
  110. NRF_GPIOTE_INT_IN1_MASK = GPIOTE_INTENSET_IN1_Msk, /**< GPIOTE interrupt from IN1. */
  111. NRF_GPIOTE_INT_IN2_MASK = GPIOTE_INTENSET_IN2_Msk, /**< GPIOTE interrupt from IN2. */
  112. NRF_GPIOTE_INT_IN3_MASK = GPIOTE_INTENSET_IN3_Msk, /**< GPIOTE interrupt from IN3. */
  113. #if (NUMBER_OF_GPIO_TE == 8)
  114. NRF_GPIOTE_INT_IN4_MASK = GPIOTE_INTENSET_IN4_Msk, /**< GPIOTE interrupt from IN4. */
  115. NRF_GPIOTE_INT_IN5_MASK = GPIOTE_INTENSET_IN5_Msk, /**< GPIOTE interrupt from IN5. */
  116. NRF_GPIOTE_INT_IN6_MASK = GPIOTE_INTENSET_IN6_Msk, /**< GPIOTE interrupt from IN6. */
  117. NRF_GPIOTE_INT_IN7_MASK = GPIOTE_INTENSET_IN7_Msk, /**< GPIOTE interrupt from IN7. */
  118. #endif
  119. NRF_GPIOTE_INT_PORT_MASK = (int)GPIOTE_INTENSET_PORT_Msk, /**< GPIOTE interrupt from PORT event. */
  120. } nrf_gpiote_int_t;
  121. #if (NUMBER_OF_GPIO_TE == 4)
  122. #define NRF_GPIOTE_INT_IN_MASK (NRF_GPIOTE_INT_IN0_MASK | NRF_GPIOTE_INT_IN1_MASK |\
  123. NRF_GPIOTE_INT_IN2_MASK | NRF_GPIOTE_INT_IN3_MASK)
  124. #elif (NUMBER_OF_GPIO_TE == 8)
  125. #define NRF_GPIOTE_INT_IN_MASK (NRF_GPIOTE_INT_IN0_MASK | NRF_GPIOTE_INT_IN1_MASK |\
  126. NRF_GPIOTE_INT_IN2_MASK | NRF_GPIOTE_INT_IN3_MASK |\
  127. NRF_GPIOTE_INT_IN4_MASK | NRF_GPIOTE_INT_IN5_MASK |\
  128. NRF_GPIOTE_INT_IN6_MASK | NRF_GPIOTE_INT_IN7_MASK)
  129. #else
  130. #error "Unexpected number of GPIO Tasks and Events"
  131. #endif
  132. /**
  133. * @brief Function for activating a specific GPIOTE task.
  134. *
  135. * @param[in] task Task.
  136. */
  137. __STATIC_INLINE void nrf_gpiote_task_set(nrf_gpiote_tasks_t task);
  138. /**
  139. * @brief Function for getting the address of a specific GPIOTE task.
  140. *
  141. * @param[in] task Task.
  142. *
  143. * @returns Address.
  144. */
  145. __STATIC_INLINE uint32_t nrf_gpiote_task_addr_get(nrf_gpiote_tasks_t task);
  146. /**
  147. * @brief Function for getting the state of a specific GPIOTE event.
  148. *
  149. * @param[in] event Event.
  150. */
  151. __STATIC_INLINE bool nrf_gpiote_event_is_set(nrf_gpiote_events_t event);
  152. /**
  153. * @brief Function for clearing a specific GPIOTE event.
  154. *
  155. * @param[in] event Event.
  156. */
  157. __STATIC_INLINE void nrf_gpiote_event_clear(nrf_gpiote_events_t event);
  158. /**
  159. * @brief Function for getting the address of a specific GPIOTE event.
  160. *
  161. * @param[in] event Event.
  162. *
  163. * @return Address
  164. */
  165. __STATIC_INLINE uint32_t nrf_gpiote_event_addr_get(nrf_gpiote_events_t event);
  166. /**@brief Function for enabling interrupts.
  167. *
  168. * @param[in] mask Interrupt mask to be enabled.
  169. */
  170. __STATIC_INLINE void nrf_gpiote_int_enable(uint32_t mask);
  171. /**@brief Function for disabling interrupts.
  172. *
  173. * @param[in] mask Interrupt mask to be disabled.
  174. */
  175. __STATIC_INLINE void nrf_gpiote_int_disable(uint32_t mask);
  176. /**@brief Function for checking if interrupts are enabled.
  177. *
  178. * @param[in] mask Mask of interrupt flags to check.
  179. *
  180. * @return Mask with enabled interrupts.
  181. */
  182. __STATIC_INLINE uint32_t nrf_gpiote_int_is_enabled(uint32_t mask);
  183. /**@brief Function for enabling a GPIOTE event.
  184. *
  185. * @param[in] idx Task-Event index.
  186. */
  187. __STATIC_INLINE void nrf_gpiote_event_enable(uint32_t idx);
  188. /**@brief Function for disabling a GPIOTE event.
  189. *
  190. * @param[in] idx Task-Event index.
  191. */
  192. __STATIC_INLINE void nrf_gpiote_event_disable(uint32_t idx);
  193. /**@brief Function for configuring a GPIOTE event.
  194. *
  195. * @param[in] idx Task-Event index.
  196. * @param[in] pin Pin associated with event.
  197. * @param[in] polarity Transition that should generate an event.
  198. */
  199. __STATIC_INLINE void nrf_gpiote_event_configure(uint32_t idx, uint32_t pin,
  200. nrf_gpiote_polarity_t polarity);
  201. /**@brief Function for getting the pin associated with a GPIOTE event.
  202. *
  203. * @param[in] idx Task-Event index.
  204. *
  205. * @return Pin number.
  206. */
  207. __STATIC_INLINE uint32_t nrf_gpiote_event_pin_get(uint32_t idx);
  208. /**@brief Function for getting the polarity associated with a GPIOTE event.
  209. *
  210. * @param[in] idx Task-Event index.
  211. *
  212. * @return Polarity.
  213. */
  214. __STATIC_INLINE nrf_gpiote_polarity_t nrf_gpiote_event_polarity_get(uint32_t idx);
  215. /**@brief Function for enabling a GPIOTE task.
  216. *
  217. * @param[in] idx Task-Event index.
  218. */
  219. __STATIC_INLINE void nrf_gpiote_task_enable(uint32_t idx);
  220. /**@brief Function for disabling a GPIOTE task.
  221. *
  222. * @param[in] idx Task-Event index.
  223. */
  224. __STATIC_INLINE void nrf_gpiote_task_disable(uint32_t idx);
  225. /**@brief Function for configuring a GPIOTE task.
  226. * @note Function is not configuring mode field so task is disabled after this function is called.
  227. *
  228. * @param[in] idx Task-Event index.
  229. * @param[in] pin Pin associated with event.
  230. * @param[in] polarity Transition that should generate an event.
  231. * @param[in] init_val Initial value of pin.
  232. */
  233. __STATIC_INLINE void nrf_gpiote_task_configure(uint32_t idx, uint32_t pin,
  234. nrf_gpiote_polarity_t polarity,
  235. nrf_gpiote_outinit_t init_val);
  236. /**@brief Function for forcing a specific state on the pin connected to GPIOTE.
  237. *
  238. * @param[in] idx Task-Event index.
  239. * @param[in] init_val Pin state.
  240. */
  241. __STATIC_INLINE void nrf_gpiote_task_force(uint32_t idx, nrf_gpiote_outinit_t init_val);
  242. /**@brief Function for resetting a GPIOTE task event configuration to the default state.
  243. *
  244. * @param[in] idx Task-Event index.
  245. */
  246. __STATIC_INLINE void nrf_gpiote_te_default(uint32_t idx);
  247. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  248. __STATIC_INLINE void nrf_gpiote_task_set(nrf_gpiote_tasks_t task)
  249. {
  250. *(__IO uint32_t *)((uint32_t)NRF_GPIOTE + task) = 0x1UL;
  251. }
  252. __STATIC_INLINE uint32_t nrf_gpiote_task_addr_get(nrf_gpiote_tasks_t task)
  253. {
  254. return ((uint32_t)NRF_GPIOTE + task);
  255. }
  256. __STATIC_INLINE bool nrf_gpiote_event_is_set(nrf_gpiote_events_t event)
  257. {
  258. return (*(uint32_t *)nrf_gpiote_event_addr_get(event) == 0x1UL) ? true : false;
  259. }
  260. __STATIC_INLINE void nrf_gpiote_event_clear(nrf_gpiote_events_t event)
  261. {
  262. *(uint32_t *)nrf_gpiote_event_addr_get(event) = 0;
  263. #if __CORTEX_M == 0x04
  264. volatile uint32_t dummy = *((volatile uint32_t *)nrf_gpiote_event_addr_get(event));
  265. (void)dummy;
  266. #endif
  267. }
  268. __STATIC_INLINE uint32_t nrf_gpiote_event_addr_get(nrf_gpiote_events_t event)
  269. {
  270. return ((uint32_t)NRF_GPIOTE + event);
  271. }
  272. __STATIC_INLINE void nrf_gpiote_int_enable(uint32_t mask)
  273. {
  274. NRF_GPIOTE->INTENSET = mask;
  275. }
  276. __STATIC_INLINE void nrf_gpiote_int_disable(uint32_t mask)
  277. {
  278. NRF_GPIOTE->INTENCLR = mask;
  279. }
  280. __STATIC_INLINE uint32_t nrf_gpiote_int_is_enabled(uint32_t mask)
  281. {
  282. return (NRF_GPIOTE->INTENSET & mask);
  283. }
  284. __STATIC_INLINE void nrf_gpiote_event_enable(uint32_t idx)
  285. {
  286. NRF_GPIOTE->CONFIG[idx] |= GPIOTE_CONFIG_MODE_Event;
  287. }
  288. __STATIC_INLINE void nrf_gpiote_event_disable(uint32_t idx)
  289. {
  290. NRF_GPIOTE->CONFIG[idx] &= ~GPIOTE_CONFIG_MODE_Event;
  291. }
  292. __STATIC_INLINE void nrf_gpiote_event_configure(uint32_t idx, uint32_t pin, nrf_gpiote_polarity_t polarity)
  293. {
  294. NRF_GPIOTE->CONFIG[idx] &= ~(GPIOTE_CONFIG_PSEL_Msk | GPIOTE_CONFIG_POLARITY_Msk);
  295. NRF_GPIOTE->CONFIG[idx] |= ((pin << GPIOTE_CONFIG_PSEL_Pos) & GPIOTE_CONFIG_PSEL_Msk) |
  296. ((polarity << GPIOTE_CONFIG_POLARITY_Pos) & GPIOTE_CONFIG_POLARITY_Msk);
  297. }
  298. __STATIC_INLINE uint32_t nrf_gpiote_event_pin_get(uint32_t idx)
  299. {
  300. return ((NRF_GPIOTE->CONFIG[idx] & GPIOTE_CONFIG_PSEL_Msk) >> GPIOTE_CONFIG_PSEL_Pos);
  301. }
  302. __STATIC_INLINE nrf_gpiote_polarity_t nrf_gpiote_event_polarity_get(uint32_t idx)
  303. {
  304. return (nrf_gpiote_polarity_t)((NRF_GPIOTE->CONFIG[idx] & GPIOTE_CONFIG_POLARITY_Msk) >> GPIOTE_CONFIG_POLARITY_Pos);
  305. }
  306. __STATIC_INLINE void nrf_gpiote_task_enable(uint32_t idx)
  307. {
  308. uint32_t final_config = NRF_GPIOTE->CONFIG[idx] | GPIOTE_CONFIG_MODE_Task;
  309. /* Workaround for the OUTINIT PAN. When nrf_gpiote_task_config() is called a glitch happens
  310. on the GPIO if the GPIO in question is already assigned to GPIOTE and the pin is in the
  311. correct state in GPIOTE but not in the OUT register. */
  312. /* Configure channel to Pin31, not connected to the pin, and configure as a tasks that will set it to proper level */
  313. NRF_GPIOTE->CONFIG[idx] = final_config | ((31 << GPIOTE_CONFIG_PSEL_Pos) & GPIOTE_CONFIG_PSEL_Msk);
  314. __NOP();
  315. __NOP();
  316. __NOP();
  317. NRF_GPIOTE->CONFIG[idx] = final_config;
  318. }
  319. __STATIC_INLINE void nrf_gpiote_task_disable(uint32_t idx)
  320. {
  321. NRF_GPIOTE->CONFIG[idx] &= ~GPIOTE_CONFIG_MODE_Task;
  322. }
  323. __STATIC_INLINE void nrf_gpiote_task_configure(uint32_t idx, uint32_t pin,
  324. nrf_gpiote_polarity_t polarity,
  325. nrf_gpiote_outinit_t init_val)
  326. {
  327. NRF_GPIOTE->CONFIG[idx] &= ~(GPIOTE_CONFIG_PSEL_Msk |
  328. GPIOTE_CONFIG_POLARITY_Msk |
  329. GPIOTE_CONFIG_OUTINIT_Msk);
  330. NRF_GPIOTE->CONFIG[idx] |= ((pin << GPIOTE_CONFIG_PSEL_Pos) & GPIOTE_CONFIG_PSEL_Msk) |
  331. ((polarity << GPIOTE_CONFIG_POLARITY_Pos) & GPIOTE_CONFIG_POLARITY_Msk) |
  332. ((init_val << GPIOTE_CONFIG_OUTINIT_Pos) & GPIOTE_CONFIG_OUTINIT_Msk);
  333. }
  334. __STATIC_INLINE void nrf_gpiote_task_force(uint32_t idx, nrf_gpiote_outinit_t init_val)
  335. {
  336. NRF_GPIOTE->CONFIG[idx] = (NRF_GPIOTE->CONFIG[idx] & ~GPIOTE_CONFIG_OUTINIT_Msk)
  337. | ((init_val << GPIOTE_CONFIG_OUTINIT_Pos) & GPIOTE_CONFIG_OUTINIT_Msk);
  338. }
  339. __STATIC_INLINE void nrf_gpiote_te_default(uint32_t idx)
  340. {
  341. NRF_GPIOTE->CONFIG[idx] = 0;
  342. }
  343. #endif //SUPPRESS_INLINE_IMPLEMENTATION
  344. /** @} */
  345. #endif