nrfx_ppi.h 12 KB

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