nrf_ppi.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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_PPI_H__
  13. #define NRF_PPI_H__
  14. #include <stddef.h>
  15. #include "nrf.h"
  16. /**
  17. * @defgroup nrf_ppi_hal PPI HAL
  18. * @{
  19. * @ingroup nrf_ppi
  20. * @brief Hardware access layer for setting up Programmable Peripheral Interconnect (PPI) channels.
  21. */
  22. #define NRF_PPI_TASK_SET (1UL)
  23. /**
  24. * @enum nrf_ppi_channel_t
  25. * @brief PPI channels.
  26. */
  27. typedef enum
  28. {
  29. NRF_PPI_CHANNEL0 = PPI_CHEN_CH0_Pos, /**< Channel 0. */
  30. NRF_PPI_CHANNEL1 = PPI_CHEN_CH1_Pos, /**< Channel 1. */
  31. NRF_PPI_CHANNEL2 = PPI_CHEN_CH2_Pos, /**< Channel 2. */
  32. NRF_PPI_CHANNEL3 = PPI_CHEN_CH3_Pos, /**< Channel 3. */
  33. NRF_PPI_CHANNEL4 = PPI_CHEN_CH4_Pos, /**< Channel 4. */
  34. NRF_PPI_CHANNEL5 = PPI_CHEN_CH5_Pos, /**< Channel 5. */
  35. NRF_PPI_CHANNEL6 = PPI_CHEN_CH6_Pos, /**< Channel 6. */
  36. NRF_PPI_CHANNEL7 = PPI_CHEN_CH7_Pos, /**< Channel 7. */
  37. NRF_PPI_CHANNEL8 = PPI_CHEN_CH8_Pos, /**< Channel 8. */
  38. NRF_PPI_CHANNEL9 = PPI_CHEN_CH9_Pos, /**< Channel 9. */
  39. NRF_PPI_CHANNEL10 = PPI_CHEN_CH10_Pos, /**< Channel 10. */
  40. NRF_PPI_CHANNEL11 = PPI_CHEN_CH11_Pos, /**< Channel 11. */
  41. NRF_PPI_CHANNEL12 = PPI_CHEN_CH12_Pos, /**< Channel 12. */
  42. NRF_PPI_CHANNEL13 = PPI_CHEN_CH13_Pos, /**< Channel 13. */
  43. NRF_PPI_CHANNEL14 = PPI_CHEN_CH14_Pos, /**< Channel 14. */
  44. NRF_PPI_CHANNEL15 = PPI_CHEN_CH15_Pos, /**< Channel 15. */
  45. #ifdef NRF52
  46. NRF_PPI_CHANNEL16 = PPI_CHEN_CH16_Pos, /**< Channel 16. */
  47. NRF_PPI_CHANNEL17 = PPI_CHEN_CH17_Pos, /**< Channel 17. */
  48. NRF_PPI_CHANNEL18 = PPI_CHEN_CH18_Pos, /**< Channel 18. */
  49. NRF_PPI_CHANNEL19 = PPI_CHEN_CH19_Pos, /**< Channel 19. */
  50. #endif
  51. NRF_PPI_CHANNEL20 = PPI_CHEN_CH20_Pos, /**< Channel 20. */
  52. NRF_PPI_CHANNEL21 = PPI_CHEN_CH21_Pos, /**< Channel 21. */
  53. NRF_PPI_CHANNEL22 = PPI_CHEN_CH22_Pos, /**< Channel 22. */
  54. NRF_PPI_CHANNEL23 = PPI_CHEN_CH23_Pos, /**< Channel 23. */
  55. NRF_PPI_CHANNEL24 = PPI_CHEN_CH24_Pos, /**< Channel 24. */
  56. NRF_PPI_CHANNEL25 = PPI_CHEN_CH25_Pos, /**< Channel 25. */
  57. NRF_PPI_CHANNEL26 = PPI_CHEN_CH26_Pos, /**< Channel 26. */
  58. NRF_PPI_CHANNEL27 = PPI_CHEN_CH27_Pos, /**< Channel 27. */
  59. NRF_PPI_CHANNEL28 = PPI_CHEN_CH28_Pos, /**< Channel 28. */
  60. NRF_PPI_CHANNEL29 = PPI_CHEN_CH29_Pos, /**< Channel 29. */
  61. NRF_PPI_CHANNEL30 = PPI_CHEN_CH30_Pos, /**< Channel 30. */
  62. NRF_PPI_CHANNEL31 = PPI_CHEN_CH31_Pos /**< Channel 31. */
  63. } nrf_ppi_channel_t;
  64. /**
  65. * @enum nrf_ppi_channel_group_t
  66. * @brief PPI channel groups.
  67. */
  68. typedef enum
  69. {
  70. NRF_PPI_CHANNEL_GROUP0 = 0, /**< Channel group 0. */
  71. NRF_PPI_CHANNEL_GROUP1 = 1, /**< Channel group 1. */
  72. NRF_PPI_CHANNEL_GROUP2 = 2, /**< Channel group 2. */
  73. NRF_PPI_CHANNEL_GROUP3 = 3, /**< Channel group 3. */
  74. #ifdef NRF52
  75. NRF_PPI_CHANNEL_GROUP4 = 4, /**< Channel group 4. */
  76. NRF_PPI_CHANNEL_GROUP5 = 5 /**< Channel group 5. */
  77. #endif
  78. } nrf_ppi_channel_group_t;
  79. /**
  80. * @enum nrf_ppi_channel_include_t
  81. * @brief Definition of which PPI channels belong to a group.
  82. */
  83. typedef enum
  84. {
  85. NRF_PPI_CHANNEL_EXCLUDE = PPI_CHG_CH0_Excluded, /**< Channel excluded from a group. */
  86. NRF_PPI_CHANNEL_INCLUDE = PPI_CHG_CH0_Included /**< Channel included in a group. */
  87. } nrf_ppi_channel_include_t;
  88. /**
  89. * @enum nrf_ppi_channel_enable_t
  90. * @brief Definition if a PPI channel is enabled.
  91. */
  92. typedef enum
  93. {
  94. NRF_PPI_CHANNEL_DISABLED = PPI_CHEN_CH0_Disabled, /**< Channel disabled. */
  95. NRF_PPI_CHANNEL_ENABLED = PPI_CHEN_CH0_Enabled /**< Channel enabled. */
  96. } nrf_ppi_channel_enable_t;
  97. /**
  98. * @enum nrf_ppi_task_t
  99. * @brief PPI tasks.
  100. */
  101. typedef enum
  102. {
  103. /*lint -save -e30 -esym(628,__INTADDR__)*/
  104. NRF_PPI_TASK_CHG0_EN = offsetof(NRF_PPI_Type, TASKS_CHG[0].EN), /**< Task for enabling channel group 0 */
  105. NRF_PPI_TASK_CHG0_DIS = offsetof(NRF_PPI_Type, TASKS_CHG[0].DIS), /**< Task for disabling channel group 0 */
  106. NRF_PPI_TASK_CHG1_EN = offsetof(NRF_PPI_Type, TASKS_CHG[1].EN), /**< Task for enabling channel group 1 */
  107. NRF_PPI_TASK_CHG1_DIS = offsetof(NRF_PPI_Type, TASKS_CHG[1].DIS), /**< Task for disabling channel group 1 */
  108. NRF_PPI_TASK_CHG2_EN = offsetof(NRF_PPI_Type, TASKS_CHG[2].EN), /**< Task for enabling channel group 2 */
  109. NRF_PPI_TASK_CHG2_DIS = offsetof(NRF_PPI_Type, TASKS_CHG[2].DIS), /**< Task for disabling channel group 2 */
  110. NRF_PPI_TASK_CHG3_EN = offsetof(NRF_PPI_Type, TASKS_CHG[3].EN), /**< Task for enabling channel group 3 */
  111. NRF_PPI_TASK_CHG3_DIS = offsetof(NRF_PPI_Type, TASKS_CHG[3].DIS), /**< Task for disabling channel group 3 */
  112. #ifdef NRF52
  113. NRF_PPI_TASK_CHG4_EN = offsetof(NRF_PPI_Type, TASKS_CHG[4].EN), /**< Task for enabling channel group 4 */
  114. NRF_PPI_TASK_CHG4_DIS = offsetof(NRF_PPI_Type, TASKS_CHG[4].DIS), /**< Task for disabling channel group 4 */
  115. NRF_PPI_TASK_CHG5_EN = offsetof(NRF_PPI_Type, TASKS_CHG[5].EN), /**< Task for enabling channel group 5 */
  116. NRF_PPI_TASK_CHG5_DIS = offsetof(NRF_PPI_Type, TASKS_CHG[5].DIS) /**< Task for disabling channel group 5 */
  117. #endif
  118. /*lint -restore*/
  119. } nrf_ppi_task_t;
  120. /**
  121. * @brief Function for enabling a given PPI channel.
  122. *
  123. * @details This function enables only one channel.
  124. *
  125. * @param[in] channel Channel to enable.
  126. *
  127. * */
  128. __STATIC_INLINE void nrf_ppi_channel_enable(nrf_ppi_channel_t channel)
  129. {
  130. NRF_PPI->CHENSET = PPI_CHENSET_CH0_Set << ((uint32_t) channel);
  131. }
  132. /**
  133. * @brief Function for disabling a given PPI channel.
  134. *
  135. * @details This function disables only one channel.
  136. *
  137. * @param[in] channel Channel to disable.
  138. */
  139. __STATIC_INLINE void nrf_ppi_channel_disable(nrf_ppi_channel_t channel)
  140. {
  141. NRF_PPI->CHENCLR = PPI_CHENCLR_CH0_Clear << ((uint32_t) channel);
  142. }
  143. /**
  144. * @brief Function for checking if a given PPI channel is enabled.
  145. *
  146. * @details This function checks only one channel.
  147. *
  148. * @param[in] channel Channel to check.
  149. *
  150. * @retval NRF_PPI_CHANNEL_ENABLED If the channel is enabled.
  151. * @retval NRF_PPI_CHANNEL_DISABLED If the channel is not enabled.
  152. *
  153. */
  154. __STATIC_INLINE nrf_ppi_channel_enable_t nrf_ppi_channel_enable_get(nrf_ppi_channel_t channel)
  155. {
  156. if (NRF_PPI->CHEN & (PPI_CHEN_CH0_Msk << ((uint32_t) channel)))
  157. {
  158. return NRF_PPI_CHANNEL_ENABLED;
  159. }
  160. else
  161. {
  162. return NRF_PPI_CHANNEL_DISABLED;
  163. }
  164. }
  165. /**
  166. * @brief Function for disabling all PPI channels.
  167. */
  168. __STATIC_INLINE void nrf_ppi_channel_disable_all(void)
  169. {
  170. NRF_PPI->CHENCLR = ((uint32_t)0xFFFFFFFFuL);
  171. }
  172. /**
  173. * @brief Function for disabling multiple PPI channels.
  174. *
  175. * @param[in] mask Channel mask.
  176. */
  177. __STATIC_INLINE void nrf_ppi_channels_disable(uint32_t mask)
  178. {
  179. NRF_PPI->CHENCLR = mask;
  180. }
  181. /**
  182. * @brief Function for setting up event and task endpoints for a given PPI channel.
  183. *
  184. * @param[in] eep Event register address.
  185. *
  186. * @param[in] tep Task register address.
  187. *
  188. * @param[in] channel Channel to which the given endpoints are assigned.
  189. */
  190. __STATIC_INLINE void nrf_ppi_channel_endpoint_setup(nrf_ppi_channel_t channel,
  191. uint32_t eep,
  192. uint32_t tep)
  193. {
  194. NRF_PPI->CH[(uint32_t) channel].EEP = eep;
  195. NRF_PPI->CH[(uint32_t) channel].TEP = tep;
  196. }
  197. #ifdef NRF52
  198. /**
  199. * @brief Function for setting up task endpoint for a given PPI fork.
  200. *
  201. * @param[in] fork_tep Task register address.
  202. *
  203. * @param[in] channel Channel to which the given fork endpoint is assigned.
  204. */
  205. __STATIC_INLINE void nrf_ppi_fork_endpoint_setup(nrf_ppi_channel_t channel,
  206. uint32_t fork_tep)
  207. {
  208. NRF_PPI->FORK[(uint32_t) channel].TEP = fork_tep;
  209. }
  210. /**
  211. * @brief Function for setting up event and task endpoints for a given PPI channel and fork.
  212. *
  213. * @param[in] eep Event register address.
  214. *
  215. * @param[in] tep Task register address.
  216. *
  217. * @param[in] fork_tep Fork task register address (register value).
  218. *
  219. * @param[in] channel Channel to which the given endpoints are assigned.
  220. */
  221. __STATIC_INLINE void nrf_ppi_channel_and_fork_endpoint_setup(nrf_ppi_channel_t channel,
  222. uint32_t eep,
  223. uint32_t tep,
  224. uint32_t fork_tep)
  225. {
  226. nrf_ppi_channel_endpoint_setup(channel, eep, tep);
  227. nrf_ppi_fork_endpoint_setup(channel, fork_tep);
  228. }
  229. #endif
  230. /**
  231. * @brief Function for including a PPI channel in a channel group.
  232. *
  233. * @details This function adds only one channel to the group.
  234. *
  235. * @param[in] channel Channel to be included in the group.
  236. *
  237. * @param[in] channel_group Channel group.
  238. *
  239. */
  240. __STATIC_INLINE void nrf_ppi_channel_include_in_group(nrf_ppi_channel_t channel,
  241. nrf_ppi_channel_group_t channel_group)
  242. {
  243. NRF_PPI->CHG[(uint32_t) channel_group] =
  244. NRF_PPI->CHG[(uint32_t) channel_group] | (PPI_CHG_CH0_Included << ((uint32_t) channel));
  245. }
  246. /**
  247. * @brief Function for including multiple PPI channels in a channel group.
  248. *
  249. * @details This function adds all specified channels to the group.
  250. *
  251. * @param[in] channel_mask Channels to be included in the group.
  252. *
  253. * @param[in] channel_group Channel group.
  254. *
  255. */
  256. __STATIC_INLINE void nrf_ppi_channels_include_in_group(uint32_t channel_mask,
  257. nrf_ppi_channel_group_t channel_group)
  258. {
  259. NRF_PPI->CHG[(uint32_t) channel_group] =
  260. NRF_PPI->CHG[(uint32_t) channel_group] | (channel_mask);
  261. }
  262. /**
  263. * @brief Function for removing a PPI channel from a channel group.
  264. *
  265. * @details This function removes only one channel from the group.
  266. *
  267. * @param[in] channel Channel to be removed from the group.
  268. *
  269. * @param[in] channel_group Channel group.
  270. */
  271. __STATIC_INLINE void nrf_ppi_channel_remove_from_group(nrf_ppi_channel_t channel,
  272. nrf_ppi_channel_group_t channel_group)
  273. {
  274. NRF_PPI->CHG[(uint32_t) channel_group] =
  275. NRF_PPI->CHG[(uint32_t) channel_group] & ~(PPI_CHG_CH0_Included << ((uint32_t) channel));
  276. }
  277. /**
  278. * @brief Function for removing multiple PPI channels from a channel group.
  279. *
  280. * @details This function removes all specified channels from the group.
  281. *
  282. * @param[in] channel_mask Channels to be removed from the group.
  283. *
  284. * @param[in] channel_group Channel group.
  285. */
  286. __STATIC_INLINE void nrf_ppi_channels_remove_from_group(uint32_t channel_mask,
  287. nrf_ppi_channel_group_t channel_group)
  288. {
  289. NRF_PPI->CHG[(uint32_t) channel_group] =
  290. NRF_PPI->CHG[(uint32_t) channel_group] & ~(channel_mask);
  291. }
  292. /**
  293. * @brief Function for removing all PPI channels from a channel group.
  294. *
  295. * @param[in] group Channel group.
  296. *
  297. */
  298. __STATIC_INLINE void nrf_ppi_channel_group_clear(nrf_ppi_channel_group_t group)
  299. {
  300. NRF_PPI->CHG[(uint32_t) group] = 0;
  301. }
  302. /**
  303. * @brief Function for enabling a channel group.
  304. *
  305. * @param[in] group Channel group.
  306. *
  307. */
  308. __STATIC_INLINE void nrf_ppi_group_enable(nrf_ppi_channel_group_t group)
  309. {
  310. NRF_PPI->TASKS_CHG[(uint32_t) group].EN = NRF_PPI_TASK_SET;
  311. }
  312. /**
  313. * @brief Function for disabling a channel group.
  314. *
  315. * @param[in] group Channel group.
  316. *
  317. */
  318. __STATIC_INLINE void nrf_ppi_group_disable(nrf_ppi_channel_group_t group)
  319. {
  320. NRF_PPI->TASKS_CHG[(uint32_t) group].DIS = NRF_PPI_TASK_SET;
  321. }
  322. /**
  323. * @brief Function for setting a PPI task.
  324. *
  325. * @param[in] ppi_task PPI task to set.
  326. */
  327. __STATIC_INLINE void nrf_ppi_task_trigger(nrf_ppi_task_t ppi_task)
  328. {
  329. *((volatile uint32_t *) ((uint8_t *) NRF_PPI_BASE + (uint32_t) ppi_task)) = NRF_PPI_TASK_SET;
  330. }
  331. /**
  332. * @brief Function for returning the address of a specific PPI task register.
  333. *
  334. * @param[in] ppi_task PPI task.
  335. */
  336. __STATIC_INLINE uint32_t * nrf_ppi_task_address_get(nrf_ppi_task_t ppi_task)
  337. {
  338. return (uint32_t *) ((uint8_t *) NRF_PPI_BASE + (uint32_t) ppi_task);
  339. }
  340. /**
  341. * @brief Function for returning the PPI enable task address of a specific group.
  342. *
  343. * @param[in] group PPI group.
  344. */
  345. __STATIC_INLINE uint32_t * nrf_ppi_task_group_enable_address_get(nrf_ppi_channel_group_t group)
  346. {
  347. return (uint32_t *) &NRF_PPI->TASKS_CHG[(uint32_t) group].EN;
  348. }
  349. /**
  350. * @brief Function for returning the PPI disable task address of a specific group.
  351. *
  352. * @param[in] group PPI group.
  353. */
  354. __STATIC_INLINE uint32_t * nrf_ppi_task_group_disable_address_get(nrf_ppi_channel_group_t group)
  355. {
  356. return (uint32_t *) &NRF_PPI->TASKS_CHG[(uint32_t) group].DIS;
  357. }
  358. /**
  359. *@}
  360. **/
  361. /*lint --flb "Leave library region" */
  362. #endif // NRF_PPI_H__