nrf_drv_ppi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. #include <stdlib.h>
  13. #include "nrf.h"
  14. #include "nrf_drv_ppi.h"
  15. #include "nrf_drv_common.h"
  16. #include "nrf_ppi.h"
  17. #include "app_util_platform.h"
  18. #include "sdk_common.h"
  19. static nrf_drv_state_t m_drv_state; /**< Driver state */
  20. static uint32_t m_channels_allocated; /**< Bitmap representing channels availability. 1 when a channel is allocated, 0 otherwise. */
  21. static uint8_t m_groups_allocated; /**< Bitmap representing groups availability. 1 when a group is allocated, 0 otherwise.*/
  22. /**@brief Compute a group mask (needed for driver internals, not used for NRF_PPI registers).
  23. * @param[in] group Group number to transform to a mask.
  24. * @retval Group mask.
  25. */
  26. __STATIC_INLINE uint32_t group_to_mask(nrf_ppi_channel_group_t group)
  27. {
  28. return (1uL << (uint32_t) group);
  29. }
  30. /**@brief Check whether a channel is a programmable channel and can be used by an application.
  31. * @param[in] channel Channel to check.
  32. * @retval true The channel is a programmable application channel.
  33. * false The channel is used by a SoftDevice or is preprogrammed.
  34. */
  35. __STATIC_INLINE bool is_programmable_app_channel(nrf_ppi_channel_t channel)
  36. {
  37. return ((NRF_PPI_PROG_APP_CHANNELS_MASK & nrf_drv_ppi_channel_to_mask(channel)) != 0);
  38. }
  39. /**@brief Check whether a channels can be used by an application.
  40. * @param[in] channel Channel mask to check.
  41. * @retval true All specified channels can be used by an application.
  42. * false At least one specified channel is used by a SoftDevice.
  43. */
  44. __STATIC_INLINE bool are_app_channels(uint32_t channel_mask)
  45. {
  46. //lint -e(587)
  47. return ((~(NRF_PPI_ALL_APP_CHANNELS_MASK) & channel_mask) == 0);
  48. }
  49. /**@brief Check whether a channel can be used by an application.
  50. * @param[in] channel Channel to check.
  51. * @retval true The channel can be used by an application.
  52. * false The channel is used by a SoftDevice.
  53. */
  54. __STATIC_INLINE bool is_app_channel(nrf_ppi_channel_t channel)
  55. {
  56. return are_app_channels(nrf_drv_ppi_channel_to_mask(channel));
  57. }
  58. /**@brief Check whether a channel group can be used by an application.
  59. * @param[in] group Group to check.
  60. * @retval true The group is an application group.
  61. * false The group is not an application group (this group either does not exist or
  62. * it is used by a SoftDevice).
  63. */
  64. __STATIC_INLINE bool is_app_group(nrf_ppi_channel_group_t group)
  65. {
  66. return ((NRF_PPI_ALL_APP_GROUPS_MASK & group_to_mask(group)) != 0);
  67. }
  68. /**@brief Check whether a channel is allocated.
  69. * @param[in] channel_num Channel number to check.
  70. * @retval true The channel is allocated.
  71. * false The channel is not allocated.
  72. */
  73. __STATIC_INLINE bool is_allocated_channel(nrf_ppi_channel_t channel)
  74. {
  75. return ((m_channels_allocated & nrf_drv_ppi_channel_to_mask(channel)) != 0);
  76. }
  77. /**@brief Set channel allocated indication.
  78. * @param[in] channel_num Specifies the channel to set the "allocated" indication.
  79. */
  80. __STATIC_INLINE void channel_allocated_set(nrf_ppi_channel_t channel)
  81. {
  82. m_channels_allocated |= nrf_drv_ppi_channel_to_mask(channel);
  83. }
  84. /**@brief Clear channel allocated indication.
  85. * @param[in] channel_num Specifies the channel to clear the "allocated" indication.
  86. */
  87. __STATIC_INLINE void channel_allocated_clr(nrf_ppi_channel_t channel)
  88. {
  89. m_channels_allocated &= ~nrf_drv_ppi_channel_to_mask(channel);
  90. }
  91. /**@brief Clear all allocated channels.
  92. */
  93. __STATIC_INLINE void channel_allocated_clr_all(void)
  94. {
  95. m_channels_allocated &= ~NRF_PPI_ALL_APP_CHANNELS_MASK;
  96. }
  97. /**@brief Check whether a group is allocated.
  98. * @param[in] group_num Group number to check.
  99. * @retval true The group is allocated.
  100. * false The group is not allocated.
  101. */
  102. __STATIC_INLINE bool is_allocated_group(nrf_ppi_channel_group_t group)
  103. {
  104. return ((m_groups_allocated & group_to_mask(group)) != 0);
  105. }
  106. /**@brief Set group allocated indication.
  107. * @param[in] group_num Specifies the group to set the "allocated" indication.
  108. */
  109. __STATIC_INLINE void group_allocated_set(nrf_ppi_channel_group_t group)
  110. {
  111. m_groups_allocated |= group_to_mask(group);
  112. }
  113. /**@brief Clear group allocated indication.
  114. * @param[in] group_num Specifies the group to clear the "allocated" indication.
  115. */
  116. __STATIC_INLINE void group_allocated_clr(nrf_ppi_channel_group_t group)
  117. {
  118. m_groups_allocated &= ~group_to_mask(group);
  119. }
  120. /**@brief Clear all allocated groups.
  121. */
  122. __STATIC_INLINE void group_allocated_clr_all()
  123. {
  124. m_groups_allocated &= ~NRF_PPI_ALL_APP_GROUPS_MASK;
  125. }
  126. uint32_t nrf_drv_ppi_init(void)
  127. {
  128. uint32_t err_code;
  129. if (m_drv_state == NRF_DRV_STATE_UNINITIALIZED)
  130. {
  131. m_drv_state = NRF_DRV_STATE_INITIALIZED;
  132. err_code = NRF_SUCCESS;
  133. }
  134. else
  135. {
  136. err_code = MODULE_ALREADY_INITIALIZED;
  137. }
  138. return err_code;
  139. }
  140. uint32_t nrf_drv_ppi_uninit(void)
  141. {
  142. uint32_t mask = NRF_PPI_ALL_APP_GROUPS_MASK;
  143. nrf_ppi_channel_group_t group;
  144. if (m_drv_state == NRF_DRV_STATE_UNINITIALIZED)
  145. {
  146. return NRF_ERROR_INVALID_STATE;
  147. }
  148. m_drv_state = NRF_DRV_STATE_UNINITIALIZED;
  149. // Disable all channels and groups
  150. nrf_ppi_channels_disable(NRF_PPI_ALL_APP_CHANNELS_MASK);
  151. for (group = NRF_PPI_CHANNEL_GROUP0; mask != 0; mask &= ~group_to_mask(group), group++)
  152. {
  153. if(mask & group_to_mask(group))
  154. {
  155. nrf_ppi_channel_group_clear(group);
  156. }
  157. }
  158. channel_allocated_clr_all();
  159. group_allocated_clr_all();
  160. return NRF_SUCCESS;
  161. }
  162. uint32_t nrf_drv_ppi_channel_alloc(nrf_ppi_channel_t * p_channel)
  163. {
  164. uint32_t err_code;
  165. nrf_ppi_channel_t channel;
  166. uint32_t mask = 0;
  167. err_code = NRF_ERROR_NO_MEM;
  168. mask = NRF_PPI_PROG_APP_CHANNELS_MASK;
  169. for (channel = NRF_PPI_CHANNEL0; mask != 0; mask &= ~nrf_drv_ppi_channel_to_mask(channel), channel++)
  170. {
  171. CRITICAL_REGION_ENTER();
  172. if ((mask & nrf_drv_ppi_channel_to_mask(channel)) && (!is_allocated_channel(channel)))
  173. {
  174. channel_allocated_set(channel);
  175. *p_channel = channel;
  176. err_code = NRF_SUCCESS;
  177. }
  178. CRITICAL_REGION_EXIT();
  179. if (err_code == NRF_SUCCESS)
  180. {
  181. break;
  182. }
  183. }
  184. return err_code;
  185. }
  186. uint32_t nrf_drv_ppi_channel_free(nrf_ppi_channel_t channel)
  187. {
  188. if (!is_programmable_app_channel(channel))
  189. {
  190. return NRF_ERROR_INVALID_PARAM;
  191. }
  192. // First disable this channel
  193. nrf_ppi_channel_disable(channel);
  194. CRITICAL_REGION_ENTER();
  195. channel_allocated_clr(channel);
  196. CRITICAL_REGION_EXIT();
  197. return NRF_SUCCESS;
  198. }
  199. uint32_t nrf_drv_ppi_channel_assign(nrf_ppi_channel_t channel, uint32_t eep, uint32_t tep)
  200. {
  201. VERIFY_PARAM_NOT_NULL((uint32_t *)eep);
  202. VERIFY_PARAM_NOT_NULL((uint32_t *)tep);
  203. if (!is_programmable_app_channel(channel))
  204. {
  205. return NRF_ERROR_INVALID_PARAM;
  206. }
  207. if (!is_allocated_channel(channel))
  208. {
  209. return NRF_ERROR_INVALID_STATE;
  210. }
  211. nrf_ppi_channel_endpoint_setup(channel, eep, tep);
  212. return NRF_SUCCESS;
  213. }
  214. uint32_t nrf_drv_ppi_channel_fork_assign(nrf_ppi_channel_t channel, uint32_t fork_tep)
  215. {
  216. #ifdef NRF51
  217. return NRF_ERROR_NOT_SUPPORTED;
  218. #else
  219. if (!is_programmable_app_channel(channel))
  220. {
  221. return NRF_ERROR_INVALID_PARAM;
  222. }
  223. if (!is_allocated_channel(channel))
  224. {
  225. return NRF_ERROR_INVALID_STATE;
  226. }
  227. nrf_ppi_fork_endpoint_setup(channel, fork_tep);
  228. return NRF_SUCCESS;
  229. #endif
  230. }
  231. uint32_t nrf_drv_ppi_channel_enable(nrf_ppi_channel_t channel)
  232. {
  233. if (!is_app_channel(channel))
  234. {
  235. return NRF_ERROR_INVALID_PARAM;
  236. }
  237. if (is_programmable_app_channel(channel) && !is_allocated_channel(channel))
  238. {
  239. return NRF_ERROR_INVALID_STATE;
  240. }
  241. nrf_ppi_channel_enable(channel);
  242. return NRF_SUCCESS;
  243. }
  244. uint32_t nrf_drv_ppi_channel_disable(nrf_ppi_channel_t channel)
  245. {
  246. if (!is_app_channel(channel))
  247. {
  248. return NRF_ERROR_INVALID_PARAM;
  249. }
  250. if (is_programmable_app_channel(channel) && !is_allocated_channel(channel))
  251. {
  252. return NRF_ERROR_INVALID_STATE;
  253. }
  254. nrf_ppi_channel_disable(channel);
  255. return NRF_SUCCESS;
  256. }
  257. uint32_t nrf_drv_ppi_group_alloc(nrf_ppi_channel_group_t * p_group)
  258. {
  259. uint32_t err_code;
  260. uint32_t mask = 0;
  261. nrf_ppi_channel_group_t group;
  262. err_code = NRF_ERROR_NO_MEM;
  263. mask = NRF_PPI_ALL_APP_GROUPS_MASK;
  264. for (group = NRF_PPI_CHANNEL_GROUP0; mask != 0; mask &= ~group_to_mask(group), group++)
  265. {
  266. CRITICAL_REGION_ENTER();
  267. if ((mask & group_to_mask(group)) && (!is_allocated_group(group)))
  268. {
  269. group_allocated_set(group);
  270. *p_group = group;
  271. err_code = NRF_SUCCESS;
  272. }
  273. CRITICAL_REGION_EXIT();
  274. if (err_code == NRF_SUCCESS)
  275. {
  276. break;
  277. }
  278. }
  279. return err_code;
  280. }
  281. uint32_t nrf_drv_ppi_group_free(nrf_ppi_channel_group_t group)
  282. {
  283. if (!is_app_group(group))
  284. {
  285. return NRF_ERROR_INVALID_PARAM;
  286. }
  287. if (!is_allocated_group(group))
  288. {
  289. return NRF_ERROR_INVALID_STATE;
  290. }
  291. else
  292. nrf_ppi_group_disable(group);
  293. CRITICAL_REGION_ENTER();
  294. group_allocated_clr(group);
  295. CRITICAL_REGION_EXIT();
  296. return NRF_SUCCESS;
  297. }
  298. uint32_t nrf_drv_ppi_group_enable(nrf_ppi_channel_group_t group)
  299. {
  300. if (!is_app_group(group))
  301. {
  302. return NRF_ERROR_INVALID_PARAM;
  303. }
  304. if (!is_allocated_group(group))
  305. {
  306. return NRF_ERROR_INVALID_STATE;
  307. }
  308. nrf_ppi_group_enable(group);
  309. return NRF_SUCCESS;
  310. }
  311. uint32_t nrf_drv_ppi_group_disable(nrf_ppi_channel_group_t group)
  312. {
  313. if (!is_app_group(group))
  314. {
  315. return NRF_ERROR_INVALID_PARAM;
  316. }
  317. nrf_ppi_group_disable(group);
  318. return NRF_SUCCESS;
  319. }
  320. uint32_t nrf_drv_ppi_channels_remove_from_group(uint32_t channel_mask,
  321. nrf_ppi_channel_group_t group)
  322. {
  323. if (!is_app_group(group))
  324. {
  325. return NRF_ERROR_INVALID_PARAM;
  326. }
  327. if (!is_allocated_group(group))
  328. {
  329. return NRF_ERROR_INVALID_STATE;
  330. }
  331. if (!are_app_channels(channel_mask))
  332. {
  333. return NRF_ERROR_INVALID_PARAM;
  334. }
  335. CRITICAL_REGION_ENTER();
  336. nrf_ppi_channels_remove_from_group(channel_mask, group);
  337. CRITICAL_REGION_EXIT();
  338. return NRF_SUCCESS;
  339. }
  340. uint32_t nrf_drv_ppi_channels_include_in_group(uint32_t channel_mask,
  341. nrf_ppi_channel_group_t group)
  342. {
  343. if (!is_app_group(group))
  344. {
  345. return NRF_ERROR_INVALID_PARAM;
  346. }
  347. if (!is_allocated_group(group))
  348. {
  349. return NRF_ERROR_INVALID_STATE;
  350. }
  351. if (!are_app_channels(channel_mask))
  352. {
  353. return NRF_ERROR_INVALID_PARAM;
  354. }
  355. CRITICAL_REGION_ENTER();
  356. nrf_ppi_channels_include_in_group(channel_mask, group);
  357. CRITICAL_REGION_EXIT();
  358. return NRF_SUCCESS;
  359. }