nrfx_ppi.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /**
  2. * Copyright (c) 2015 - 2018, 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_INVALID_PARAM If the channel is not user-configurable.
  114. * @retval NRFX_ERROR_NOT_SUPPORTED If function is not supported.
  115. */
  116. nrfx_err_t nrfx_ppi_channel_fork_assign(nrf_ppi_channel_t channel, uint32_t fork_tep);
  117. /**
  118. * @brief Function for enabling a PPI channel.
  119. *
  120. * @param[in] channel PPI channel to be enabled.
  121. *
  122. * @retval NRFX_SUCCESS If the channel was successfully enabled.
  123. * @retval NRFX_ERROR_INVALID_STATE If the user-configurable channel is not allocated.
  124. * @retval NRFX_ERROR_INVALID_PARAM If the channel cannot be enabled by the user.
  125. */
  126. nrfx_err_t nrfx_ppi_channel_enable(nrf_ppi_channel_t channel);
  127. /**
  128. * @brief Function for disabling a PPI channel.
  129. *
  130. * @param[in] channel PPI channel to be disabled.
  131. *
  132. * @retval NRFX_SUCCESS If the channel was successfully disabled.
  133. * @retval NRFX_ERROR_INVALID_STATE If the user-configurable channel is not allocated.
  134. * @retval NRFX_ERROR_INVALID_PARAM If the channel cannot be disabled by the user.
  135. */
  136. nrfx_err_t nrfx_ppi_channel_disable(nrf_ppi_channel_t channel);
  137. /**
  138. * @brief Function for allocating a PPI channel group.
  139. * @details This function allocates the first unused PPI group.
  140. *
  141. * @param[out] p_group Pointer to the PPI channel group that has been allocated.
  142. *
  143. * @retval NRFX_SUCCESS If the channel group was successfully allocated.
  144. * @retval NRFX_ERROR_NO_MEM If there is no available channel group to be used.
  145. */
  146. nrfx_err_t nrfx_ppi_group_alloc(nrf_ppi_channel_group_t * p_group);
  147. /**
  148. * @brief Function for freeing a PPI channel group.
  149. * @details This function also disables the chosen group.
  150. *
  151. * @param[in] group PPI channel group to be freed.
  152. *
  153. * @retval NRFX_SUCCESS If the channel group was successfully freed.
  154. * @retval NRFX_ERROR_INVALID_PARAM If the channel group is not user-configurable.
  155. */
  156. nrfx_err_t nrfx_ppi_group_free(nrf_ppi_channel_group_t group);
  157. /**
  158. * @brief Compute a channel mask for NRF_PPI registers.
  159. *
  160. * @param[in] channel Channel number to transform to a mask.
  161. *
  162. * @retval Channel mask.
  163. */
  164. __STATIC_INLINE uint32_t nrfx_ppi_channel_to_mask(nrf_ppi_channel_t channel)
  165. {
  166. return (1uL << (uint32_t) channel);
  167. }
  168. /**
  169. * @brief Function for including multiple PPI channels in a channel group.
  170. *
  171. * @param[in] channel_mask PPI channels to be added.
  172. * @param[in] group Channel group in which to include the channels.
  173. *
  174. * @retval NRFX_SUCCESS If the channels was successfully included.
  175. * @retval NRFX_ERROR_INVALID_PARAM If group is not an application group or channels are not an
  176. * application channels.
  177. * @retval NRFX_ERROR_INVALID_STATE If group is not an allocated group.
  178. */
  179. nrfx_err_t nrfx_ppi_channels_include_in_group(uint32_t channel_mask,
  180. nrf_ppi_channel_group_t group);
  181. /**
  182. * @brief Function for including a PPI channel in a channel group.
  183. *
  184. * @param[in] channel PPI channel to be added.
  185. * @param[in] group Channel group in which to include the channel.
  186. *
  187. * @retval NRFX_SUCCESS If the channel was successfully included.
  188. * @retval NRFX_ERROR_INVALID_PARAM If group is not an application group or channel is not an
  189. * application channel.
  190. * @retval NRFX_ERROR_INVALID_STATE If group is not an allocated group.
  191. */
  192. __STATIC_INLINE nrfx_err_t nrfx_ppi_channel_include_in_group(nrf_ppi_channel_t channel,
  193. nrf_ppi_channel_group_t group)
  194. {
  195. return nrfx_ppi_channels_include_in_group(nrfx_ppi_channel_to_mask(channel), group);
  196. }
  197. /**
  198. * @brief Function for removing multiple PPI channels from a channel group.
  199. *
  200. * @param[in] channel_mask PPI channels to be removed.
  201. * @param[in] group Channel group from which to remove the channels.
  202. *
  203. * @retval NRFX_SUCCESS If the channel was successfully removed.
  204. * @retval NRFX_ERROR_INVALID_PARAM If group is not an application group or channels are not an
  205. * application channels.
  206. * @retval NRFX_ERROR_INVALID_STATE If group is not an allocated group.
  207. */
  208. nrfx_err_t nrfx_ppi_channels_remove_from_group(uint32_t channel_mask,
  209. nrf_ppi_channel_group_t group);
  210. /**
  211. * @brief Function for removing a PPI channel from a channel group.
  212. *
  213. * @param[in] channel PPI channel to be removed.
  214. * @param[in] group Channel group from which to remove the channel.
  215. *
  216. * @retval NRFX_SUCCESS If the channel was successfully removed.
  217. * @retval NRFX_ERROR_INVALID_PARAM If group is not an application group or channel is not an
  218. * application channel.
  219. * @retval NRFX_ERROR_INVALID_STATE If group is not an allocated group.
  220. */
  221. __STATIC_INLINE nrfx_err_t nrfx_ppi_channel_remove_from_group(nrf_ppi_channel_t channel,
  222. nrf_ppi_channel_group_t group)
  223. {
  224. return nrfx_ppi_channels_remove_from_group(nrfx_ppi_channel_to_mask(channel), group);
  225. }
  226. /**
  227. * @brief Function for clearing a PPI channel group.
  228. *
  229. * @param[in] group Channel group to be cleared.
  230. *
  231. * @retval NRFX_SUCCESS If the group was successfully cleared.
  232. * @retval NRFX_ERROR_INVALID_PARAM If group is not an application group.
  233. * @retval NRFX_ERROR_INVALID_STATE If group is not an allocated group.
  234. */
  235. __STATIC_INLINE nrfx_err_t nrfx_ppi_group_clear(nrf_ppi_channel_group_t group)
  236. {
  237. return nrfx_ppi_channels_remove_from_group(NRFX_PPI_ALL_APP_CHANNELS_MASK, group);
  238. }
  239. /**
  240. * @brief Function for enabling a PPI channel group.
  241. *
  242. * @param[in] group Channel group to be enabled.
  243. *
  244. * @retval NRFX_SUCCESS If the group was successfully enabled.
  245. * @retval NRFX_ERROR_INVALID_PARAM If group is not an application group.
  246. * @retval NRFX_ERROR_INVALID_STATE If group is not an allocated group.
  247. */
  248. nrfx_err_t nrfx_ppi_group_enable(nrf_ppi_channel_group_t group);
  249. /**
  250. * @brief Function for disabling a PPI channel group.
  251. *
  252. * @param[in] group Channel group to be disabled.
  253. *
  254. * @retval NRFX_SUCCESS If the group was successfully disabled.
  255. * @retval NRFX_ERROR_INVALID_PARAM If group is not an application group.
  256. * @retval NRFX_ERROR_INVALID_STATE If group is not an allocated group.
  257. */
  258. nrfx_err_t nrfx_ppi_group_disable(nrf_ppi_channel_group_t group);
  259. /**
  260. * @brief Function for getting the address of a PPI task.
  261. *
  262. * @param[in] task Task.
  263. *
  264. * @retval Task address.
  265. */
  266. __STATIC_INLINE uint32_t nrfx_ppi_task_addr_get(nrf_ppi_task_t task)
  267. {
  268. return (uint32_t) nrf_ppi_task_address_get(task);
  269. }
  270. /**
  271. * @brief Function for getting the address of a PPI group enable task.
  272. *
  273. * @param[in] group PPI channel group
  274. *
  275. * @retval Task address.
  276. */
  277. __STATIC_INLINE uint32_t nrfx_ppi_task_addr_group_enable_get(nrf_ppi_channel_group_t group)
  278. {
  279. return (uint32_t) nrf_ppi_task_group_enable_address_get(group);
  280. }
  281. /**
  282. * @brief Function for getting the address of a PPI group enable task.
  283. *
  284. * @param[in] group PPI channel group
  285. *
  286. * @retval Task address.
  287. */
  288. __STATIC_INLINE uint32_t nrfx_ppi_task_addr_group_disable_get(nrf_ppi_channel_group_t group)
  289. {
  290. return (uint32_t) nrf_ppi_task_group_disable_address_get(group);
  291. }
  292. /** @} */
  293. #ifdef __cplusplus
  294. }
  295. #endif
  296. #endif // NRFX_PPI_H__