nrfx_ppi.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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_PPI_H__
  41. #define NRFX_PPI_H__
  42. #include <nrfx.h>
  43. #include <hal/nrf_ppi.h>
  44. /**
  45. * @defgroup nrfx_ppi PPI allocator
  46. * @{
  47. * @ingroup nrf_ppi
  48. * @brief Programmable Peripheral Interconnect (PPI) allocator.
  49. */
  50. #ifdef __cplusplus
  51. extern "C" {
  52. #endif
  53. #if !defined (NRFX_PPI_CHANNELS_USED) || defined(__NRFX_DOXYGEN__)
  54. /** @brief Bitfield representing PPI channels used by external modules. */
  55. #define NRFX_PPI_CHANNELS_USED 0
  56. #endif
  57. #if !defined(NRFX_PPI_GROUPS_USED) || defined(__NRFX_DOXYGEN__)
  58. /** @brief Bitfield representing PPI groups used by external modules. */
  59. #define NRFX_PPI_GROUPS_USED 0
  60. #endif
  61. #if (PPI_CH_NUM > 16) || defined(__NRFX_DOXYGEN__)
  62. /** @brief Bitfield representing all PPI channels available to the application. */
  63. #define NRFX_PPI_ALL_APP_CHANNELS_MASK ((uint32_t)0xFFFFFFFFuL & ~(NRFX_PPI_CHANNELS_USED))
  64. /** @brief Bitfield representing programmable PPI channels available to the application. */
  65. #define NRFX_PPI_PROG_APP_CHANNELS_MASK ((uint32_t)0x000FFFFFuL & ~(NRFX_PPI_CHANNELS_USED))
  66. #else
  67. #define NRFX_PPI_ALL_APP_CHANNELS_MASK ((uint32_t)0xFFF0FFFFuL & ~(NRFX_PPI_CHANNELS_USED))
  68. #define NRFX_PPI_PROG_APP_CHANNELS_MASK ((uint32_t)0x0000FFFFuL & ~(NRFX_PPI_CHANNELS_USED))
  69. #endif
  70. /** @brief Bitfield representing all PPI groups available to the application. */
  71. #define NRFX_PPI_ALL_APP_GROUPS_MASK (((1uL << PPI_GROUP_NUM) - 1) & ~(NRFX_PPI_GROUPS_USED))
  72. /**
  73. * @brief Function for uninitializing the PPI module.
  74. *
  75. * This function disables all channels and clears the channel groups.
  76. */
  77. void nrfx_ppi_free_all(void);
  78. /**
  79. * @brief Function for allocating a PPI channel.
  80. * @details This function allocates the first unused PPI channel.
  81. *
  82. * @param[out] p_channel Pointer to the PPI channel that has been allocated.
  83. *
  84. * @retval NRFX_SUCCESS The channel was successfully allocated.
  85. * @retval NRFX_ERROR_NO_MEM There is no available channel to be used.
  86. */
  87. nrfx_err_t nrfx_ppi_channel_alloc(nrf_ppi_channel_t * p_channel);
  88. /**
  89. * @brief Function for freeing a PPI channel.
  90. * @details This function also disables the chosen channel.
  91. *
  92. * @param[in] channel PPI channel to be freed.
  93. *
  94. * @retval NRFX_SUCCESS The channel was successfully freed.
  95. * @retval NRFX_ERROR_INVALID_PARAM The channel is not user-configurable.
  96. */
  97. nrfx_err_t nrfx_ppi_channel_free(nrf_ppi_channel_t channel);
  98. /**
  99. * @brief Function for assigning task and event endpoints to the PPI channel.
  100. *
  101. * @param[in] channel PPI channel to be assigned endpoints.
  102. * @param[in] eep Event endpoint address.
  103. * @param[in] tep Task endpoint address.
  104. *
  105. * @retval NRFX_SUCCESS The channel was successfully assigned.
  106. * @retval NRFX_ERROR_INVALID_STATE The channel is not allocated for the user.
  107. * @retval NRFX_ERROR_INVALID_PARAM The channel is not user-configurable.
  108. */
  109. nrfx_err_t nrfx_ppi_channel_assign(nrf_ppi_channel_t channel, uint32_t eep, uint32_t tep);
  110. /**
  111. * @brief Function for assigning fork endpoint to the PPI channel or clearing it.
  112. *
  113. * @param[in] channel PPI channel to be assigned endpoints.
  114. * @param[in] fork_tep Fork task endpoint address or 0 to clear.
  115. *
  116. * @retval NRFX_SUCCESS The channel was successfully assigned.
  117. * @retval NRFX_ERROR_INVALID_STATE The channel is not allocated for the user.
  118. * @retval NRFX_ERROR_NOT_SUPPORTED Function is not supported.
  119. */
  120. nrfx_err_t nrfx_ppi_channel_fork_assign(nrf_ppi_channel_t channel, uint32_t fork_tep);
  121. /**
  122. * @brief Function for enabling a PPI channel.
  123. *
  124. * @param[in] channel PPI channel to be enabled.
  125. *
  126. * @retval NRFX_SUCCESS The channel was successfully enabled.
  127. * @retval NRFX_ERROR_INVALID_STATE The user-configurable channel is not allocated.
  128. * @retval NRFX_ERROR_INVALID_PARAM The channel cannot be enabled by the user.
  129. */
  130. nrfx_err_t nrfx_ppi_channel_enable(nrf_ppi_channel_t channel);
  131. /**
  132. * @brief Function for disabling a PPI channel.
  133. *
  134. * @param[in] channel PPI channel to be disabled.
  135. *
  136. * @retval NRFX_SUCCESS The channel was successfully disabled.
  137. * @retval NRFX_ERROR_INVALID_STATE The user-configurable channel is not allocated.
  138. * @retval NRFX_ERROR_INVALID_PARAM The channel cannot be disabled by the user.
  139. */
  140. nrfx_err_t nrfx_ppi_channel_disable(nrf_ppi_channel_t channel);
  141. /**
  142. * @brief Function for allocating a PPI channel group.
  143. * @details This function allocates the first unused PPI group.
  144. *
  145. * @param[out] p_group Pointer to the PPI channel group that has been allocated.
  146. *
  147. * @retval NRFX_SUCCESS The channel group was successfully allocated.
  148. * @retval NRFX_ERROR_NO_MEM There is no available channel group to be used.
  149. */
  150. nrfx_err_t nrfx_ppi_group_alloc(nrf_ppi_channel_group_t * p_group);
  151. /**
  152. * @brief Function for freeing a PPI channel group.
  153. * @details This function also disables the chosen group.
  154. *
  155. * @param[in] group PPI channel group to be freed.
  156. *
  157. * @retval NRFX_SUCCESS The channel group was successfully freed.
  158. * @retval NRFX_ERROR_INVALID_PARAM The channel group is not user-configurable.
  159. */
  160. nrfx_err_t nrfx_ppi_group_free(nrf_ppi_channel_group_t group);
  161. /**
  162. * @brief Compute a channel mask for NRF_PPI registers.
  163. *
  164. * @param[in] channel Channel number to transform to a mask.
  165. *
  166. * @return Channel mask.
  167. */
  168. __STATIC_INLINE uint32_t nrfx_ppi_channel_to_mask(nrf_ppi_channel_t channel)
  169. {
  170. return (1uL << (uint32_t) channel);
  171. }
  172. /**
  173. * @brief Function for including multiple PPI channels in a channel group.
  174. *
  175. * @param[in] channel_mask PPI channels to be added.
  176. * @param[in] group Channel group in which to include the channels.
  177. *
  178. * @retval NRFX_SUCCESS The channels was successfully included.
  179. * @retval NRFX_ERROR_INVALID_PARAM Group is not an application group or channels are not an
  180. * application channels.
  181. * @retval NRFX_ERROR_INVALID_STATE Group is not an allocated group.
  182. */
  183. nrfx_err_t nrfx_ppi_channels_include_in_group(uint32_t channel_mask,
  184. nrf_ppi_channel_group_t group);
  185. /**
  186. * @brief Function for including a PPI channel in a channel group.
  187. *
  188. * @param[in] channel PPI channel to be added.
  189. * @param[in] group Channel group in which to include the channel.
  190. *
  191. * @retval NRFX_SUCCESS The channel was successfully included.
  192. * @retval NRFX_ERROR_INVALID_PARAM Group is not an application group or channel is not an
  193. * application channel.
  194. * @retval NRFX_ERROR_INVALID_STATE Group is not an allocated group.
  195. */
  196. __STATIC_INLINE nrfx_err_t nrfx_ppi_channel_include_in_group(nrf_ppi_channel_t channel,
  197. nrf_ppi_channel_group_t group)
  198. {
  199. return nrfx_ppi_channels_include_in_group(nrfx_ppi_channel_to_mask(channel), group);
  200. }
  201. /**
  202. * @brief Function for removing multiple PPI channels from a channel group.
  203. *
  204. * @param[in] channel_mask PPI channels to be removed.
  205. * @param[in] group Channel group from which to remove the channels.
  206. *
  207. * @retval NRFX_SUCCESS The channel was successfully removed.
  208. * @retval NRFX_ERROR_INVALID_PARAM Group is not an application group or channels are not an
  209. * application channels.
  210. * @retval NRFX_ERROR_INVALID_STATE Group is not an allocated group.
  211. */
  212. nrfx_err_t nrfx_ppi_channels_remove_from_group(uint32_t channel_mask,
  213. nrf_ppi_channel_group_t group);
  214. /**
  215. * @brief Function for removing a single PPI channel from a channel group.
  216. *
  217. * @param[in] channel PPI channel to be removed.
  218. * @param[in] group Channel group from which to remove the channel.
  219. *
  220. * @retval NRFX_SUCCESS The channel was successfully removed.
  221. * @retval NRFX_ERROR_INVALID_PARAM Group is not an application group or channel is not an
  222. * application channel.
  223. * @retval NRFX_ERROR_INVALID_STATE Group is not an allocated group.
  224. */
  225. __STATIC_INLINE nrfx_err_t nrfx_ppi_channel_remove_from_group(nrf_ppi_channel_t channel,
  226. nrf_ppi_channel_group_t group)
  227. {
  228. return nrfx_ppi_channels_remove_from_group(nrfx_ppi_channel_to_mask(channel), group);
  229. }
  230. /**
  231. * @brief Function for clearing a PPI channel group.
  232. *
  233. * @param[in] group Channel group to be cleared.
  234. *
  235. * @retval NRFX_SUCCESS The group was successfully cleared.
  236. * @retval NRFX_ERROR_INVALID_PARAM Group is not an application group.
  237. * @retval NRFX_ERROR_INVALID_STATE Group is not an allocated group.
  238. */
  239. __STATIC_INLINE nrfx_err_t nrfx_ppi_group_clear(nrf_ppi_channel_group_t group)
  240. {
  241. return nrfx_ppi_channels_remove_from_group(NRFX_PPI_ALL_APP_CHANNELS_MASK, group);
  242. }
  243. /**
  244. * @brief Function for enabling a PPI channel group.
  245. *
  246. * @param[in] group Channel group to be enabled.
  247. *
  248. * @retval NRFX_SUCCESS The group was successfully enabled.
  249. * @retval NRFX_ERROR_INVALID_PARAM Group is not an application group.
  250. * @retval NRFX_ERROR_INVALID_STATE Group is not an allocated group.
  251. */
  252. nrfx_err_t nrfx_ppi_group_enable(nrf_ppi_channel_group_t group);
  253. /**
  254. * @brief Function for disabling a PPI channel group.
  255. *
  256. * @param[in] group Channel group to be disabled.
  257. *
  258. * @retval NRFX_SUCCESS The group was successfully disabled.
  259. * @retval NRFX_ERROR_INVALID_PARAM Group is not an application group.
  260. * @retval NRFX_ERROR_INVALID_STATE Group is not an allocated group.
  261. */
  262. nrfx_err_t nrfx_ppi_group_disable(nrf_ppi_channel_group_t group);
  263. /**
  264. * @brief Function for getting the address of a PPI task.
  265. *
  266. * @param[in] task Task.
  267. *
  268. * @return Task address.
  269. */
  270. __STATIC_INLINE uint32_t nrfx_ppi_task_addr_get(nrf_ppi_task_t task)
  271. {
  272. return (uint32_t) nrf_ppi_task_address_get(task);
  273. }
  274. /**
  275. * @brief Function for getting the address of the enable task of a PPI group.
  276. *
  277. * @param[in] group PPI channel group
  278. *
  279. * @return Task address.
  280. */
  281. __STATIC_INLINE uint32_t nrfx_ppi_task_addr_group_enable_get(nrf_ppi_channel_group_t group)
  282. {
  283. return (uint32_t) nrf_ppi_task_group_enable_address_get(group);
  284. }
  285. /**
  286. * @brief Function for getting the address of the enable task of a PPI group.
  287. *
  288. * @param[in] group PPI channel group
  289. *
  290. * @return Task address.
  291. */
  292. __STATIC_INLINE uint32_t nrfx_ppi_task_addr_group_disable_get(nrf_ppi_channel_group_t group)
  293. {
  294. return (uint32_t) nrf_ppi_task_group_disable_address_get(group);
  295. }
  296. /** @} */
  297. #ifdef __cplusplus
  298. }
  299. #endif
  300. #endif // NRFX_PPI_H__