nrf_drv_swi.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 "nrf_drv_common.h"
  13. #include "nrf_error.h"
  14. #include "nrf_assert.h"
  15. #include <stdbool.h>
  16. #include <stdint.h>
  17. #include <stdlib.h>
  18. #include "nrf_drv_swi.h"
  19. #include "app_util_platform.h"
  20. STATIC_ASSERT(SWI_COUNT > 0);
  21. STATIC_ASSERT(SWI_COUNT <= SWI_MAX);
  22. STATIC_ASSERT(SWI_MAX_FLAGS <= sizeof(nrf_swi_flags_t) * 8);
  23. #ifdef SWI_DISABLE0
  24. #undef SWI_DISABLE0
  25. #define SWI_DISABLE0 1uL
  26. #else
  27. #if SWI_COUNT > 0
  28. #define SWI_DISABLE0 0uL
  29. #else
  30. #define SWI_DISABLE0 1uL
  31. #endif
  32. #endif
  33. #ifdef SWI_DISABLE1
  34. #undef SWI_DISABLE1
  35. #define SWI_DISABLE1 1uL
  36. #else
  37. #if SWI_COUNT > 1
  38. #define SWI_DISABLE1 0uL
  39. #else
  40. #define SWI_DISABLE1 1uL
  41. #endif
  42. #endif
  43. #ifdef SWI_DISABLE2
  44. #undef SWI_DISABLE2
  45. #define SWI_DISABLE2 1uL
  46. #else
  47. #if SWI_COUNT > 2
  48. #define SWI_DISABLE2 0uL
  49. #else
  50. #define SWI_DISABLE2 1uL
  51. #endif
  52. #endif
  53. #ifdef SWI_DISABLE3
  54. #undef SWI_DISABLE3
  55. #define SWI_DISABLE3 1uL
  56. #else
  57. #if SWI_COUNT > 3
  58. #define SWI_DISABLE3 0uL
  59. #else
  60. #define SWI_DISABLE3 1uL
  61. #endif
  62. #endif
  63. #ifdef SWI_DISABLE4
  64. #undef SWI_DISABLE4
  65. #define SWI_DISABLE4 1uL
  66. #else
  67. #if SWI_COUNT > 4
  68. #define SWI_DISABLE4 0uL
  69. #else
  70. #define SWI_DISABLE4 1uL
  71. #endif
  72. #endif
  73. #ifdef SWI_DISABLE5
  74. #undef SWI_DISABLE5
  75. #define SWI_DISABLE5 1uL
  76. #else
  77. #if SWI_COUNT > 5
  78. #define SWI_DISABLE5 0uL
  79. #else
  80. #define SWI_DISABLE5 1uL
  81. #endif
  82. #endif
  83. #define SWI_START_NUMBER ( (SWI_DISABLE0) \
  84. + (SWI_DISABLE0 * SWI_DISABLE1) \
  85. + (SWI_DISABLE0 * SWI_DISABLE1 * SWI_DISABLE2) \
  86. + (SWI_DISABLE0 * SWI_DISABLE1 * SWI_DISABLE2 * SWI_DISABLE3) \
  87. + (SWI_DISABLE0 * SWI_DISABLE1 * SWI_DISABLE2 * SWI_DISABLE3 * SWI_DISABLE4) \
  88. + (SWI_DISABLE0 * SWI_DISABLE1 * SWI_DISABLE2 * SWI_DISABLE3 * SWI_DISABLE4 \
  89. * SWI_DISABLE5) )
  90. #define SWI_ARRAY_SIZE (SWI_COUNT - SWI_START_NUMBER)
  91. #if (SWI_COUNT <= SWI_START_NUMBER)
  92. #undef SWI_ARRAY_SIZE
  93. #define SWI_ARRAY_SIZE 1
  94. #endif
  95. static nrf_drv_state_t m_drv_state = NRF_DRV_STATE_UNINITIALIZED;
  96. static nrf_swi_handler_t m_swi_handlers[SWI_ARRAY_SIZE];
  97. #if !EGU_ENABLED
  98. static nrf_swi_flags_t m_swi_flags[SWI_ARRAY_SIZE];
  99. #endif
  100. #if EGU_ENABLED > 0
  101. /**@brief Get the specific EGU instance. */
  102. __STATIC_INLINE NRF_EGU_Type * egu_instance_get(nrf_swi_t swi)
  103. {
  104. return (NRF_EGU_Type*) (NRF_EGU0_BASE + (((uint32_t) swi) * (NRF_EGU1_BASE - NRF_EGU0_BASE)));
  105. }
  106. /**@brief Software interrupt handler (using EGU). */
  107. static void nrf_drv_swi_process(nrf_swi_t swi)
  108. {
  109. ASSERT(m_swi_handlers[swi - SWI_START_NUMBER]);
  110. nrf_swi_flags_t flags = 0;
  111. NRF_EGU_Type * NRF_EGUx = egu_instance_get(swi);
  112. for (uint8_t i = 0; i < NRF_EGU_CHANNEL_COUNT; ++i)
  113. {
  114. nrf_egu_event_t egu_event = nrf_egu_event_triggered_get(i);
  115. if (nrf_egu_event_check(NRF_EGUx, egu_event))
  116. {
  117. flags |= (1u << i);
  118. nrf_egu_event_clear(NRF_EGUx, egu_event);
  119. }
  120. }
  121. m_swi_handlers[swi - SWI_START_NUMBER](swi, flags);
  122. }
  123. #define SWI_HANDLER_TEMPLATE(NUM) void SWI##NUM##_EGU##NUM##_IRQHandler(void) \
  124. { \
  125. nrf_drv_swi_process(NUM); \
  126. }
  127. #else
  128. /**@brief Software interrupt handler (without EGU). */
  129. static void nrf_drv_swi_process(nrf_swi_t swi, nrf_swi_flags_t flags)
  130. {
  131. ASSERT(m_swi_handlers[swi - SWI_START_NUMBER]);
  132. m_swi_flags[swi - SWI_START_NUMBER] &= ~flags;
  133. m_swi_handlers[swi - SWI_START_NUMBER](swi, flags);
  134. }
  135. #define SWI_HANDLER_TEMPLATE(NUM) void SWI##NUM##_IRQHandler(void) \
  136. { \
  137. nrf_drv_swi_process((NUM), m_swi_flags[(NUM) - SWI_START_NUMBER]); \
  138. }
  139. #endif
  140. #if SWI_DISABLE0 == 0
  141. SWI_HANDLER_TEMPLATE(0)
  142. #endif
  143. #if SWI_DISABLE1 == 0
  144. SWI_HANDLER_TEMPLATE(1)
  145. #endif
  146. #if SWI_DISABLE2 == 0
  147. SWI_HANDLER_TEMPLATE(2)
  148. #endif
  149. #if SWI_DISABLE3 == 0
  150. SWI_HANDLER_TEMPLATE(3)
  151. #endif
  152. #if SWI_DISABLE4 == 0
  153. SWI_HANDLER_TEMPLATE(4)
  154. #endif
  155. #if SWI_DISABLE5 == 0
  156. SWI_HANDLER_TEMPLATE(5)
  157. #endif
  158. #define AVAILABLE_SWI (0x3FuL & ~( \
  159. (SWI_DISABLE0 << 0) | (SWI_DISABLE1 << 1) | (SWI_DISABLE2 << 2) \
  160. | (SWI_DISABLE3 << 3) | (SWI_DISABLE4 << 4) | (SWI_DISABLE5 << 5) \
  161. ))
  162. #if (AVAILABLE_SWI == 0)
  163. #warning No available SWIs.
  164. #endif
  165. /**@brief Function for converting SWI number to system interrupt number.
  166. *
  167. * @param[in] swi SWI number.
  168. *
  169. * @retval IRQ number.
  170. */
  171. __STATIC_INLINE IRQn_Type nrf_drv_swi_irq_of(nrf_swi_t swi)
  172. {
  173. return (IRQn_Type)((uint32_t)SWI0_IRQn + (uint32_t)swi);
  174. }
  175. /**@brief Function for checking if given SWI is allocated.
  176. *
  177. * @param[in] swi SWI number.
  178. */
  179. __STATIC_INLINE bool swi_is_allocated(nrf_swi_t swi)
  180. {
  181. ASSERT(swi < SWI_COUNT);
  182. #if SWI_START_NUMBER > 0
  183. if (swi < SWI_START_NUMBER)
  184. {
  185. return false;
  186. }
  187. #endif
  188. /*lint -e(661) out of range case handled by assert above*/
  189. return m_swi_handlers[swi - SWI_START_NUMBER];
  190. }
  191. ret_code_t nrf_drv_swi_init(void)
  192. {
  193. if (m_drv_state == NRF_DRV_STATE_UNINITIALIZED)
  194. {
  195. m_drv_state = NRF_DRV_STATE_INITIALIZED;
  196. return NRF_SUCCESS;
  197. }
  198. return MODULE_ALREADY_INITIALIZED;
  199. }
  200. void nrf_drv_swi_uninit(void)
  201. {
  202. ASSERT(m_drv_state != NRF_DRV_STATE_UNINITIALIZED)
  203. for (uint32_t i = SWI_START_NUMBER; i < SWI_COUNT; ++i)
  204. {
  205. m_swi_handlers[i - SWI_START_NUMBER] = NULL;
  206. nrf_drv_common_irq_disable(nrf_drv_swi_irq_of((nrf_swi_t) i));
  207. #if EGU_ENABLED > 0
  208. NRF_EGU_Type * NRF_EGUx = egu_instance_get(i);
  209. nrf_egu_int_disable(NRF_EGUx, NRF_EGU_INT_ALL);
  210. #endif
  211. }
  212. m_drv_state = NRF_DRV_STATE_UNINITIALIZED;
  213. return;
  214. }
  215. void nrf_drv_swi_free(nrf_swi_t * p_swi)
  216. {
  217. ASSERT(swi_is_allocated(*p_swi));
  218. nrf_drv_common_irq_disable(nrf_drv_swi_irq_of(*p_swi));
  219. m_swi_handlers[(*p_swi) - SWI_START_NUMBER] = NULL;
  220. *p_swi = NRF_SWI_UNALLOCATED;
  221. }
  222. ret_code_t nrf_drv_swi_alloc(nrf_swi_t * p_swi, nrf_swi_handler_t event_handler, uint32_t priority)
  223. {
  224. ASSERT(event_handler);
  225. uint32_t err_code = NRF_ERROR_NO_MEM;
  226. for (uint32_t i = SWI_START_NUMBER; i < SWI_COUNT; i++)
  227. {
  228. CRITICAL_REGION_ENTER();
  229. if ((!swi_is_allocated(i)) && (AVAILABLE_SWI & (1 << i)))
  230. {
  231. m_swi_handlers[i - SWI_START_NUMBER] = event_handler;
  232. *p_swi = (nrf_swi_t) i;
  233. nrf_drv_common_irq_enable(nrf_drv_swi_irq_of(*p_swi), priority);
  234. #if EGU_ENABLED > 0
  235. NRF_EGU_Type * NRF_EGUx = egu_instance_get(i);
  236. nrf_egu_int_enable(NRF_EGUx, NRF_EGU_INT_ALL);
  237. #endif
  238. err_code = NRF_SUCCESS;
  239. }
  240. CRITICAL_REGION_EXIT();
  241. if (err_code == NRF_SUCCESS)
  242. {
  243. break;
  244. }
  245. }
  246. return err_code;
  247. }
  248. void nrf_drv_swi_trigger(nrf_swi_t swi, uint8_t flag_number)
  249. {
  250. ASSERT(swi_is_allocated((uint32_t) swi));
  251. #if EGU_ENABLED > 0
  252. ASSERT(flag_number < NRF_EGU_CHANNEL_COUNT);
  253. NRF_EGU_Type * NRF_EGUx = egu_instance_get(swi);
  254. nrf_egu_task_trigger(NRF_EGUx, nrf_egu_task_trigger_get(flag_number));
  255. #else
  256. ASSERT(flag_number < SWI_MAX_FLAGS);
  257. m_swi_flags[swi - SWI_START_NUMBER] |= (1 << flag_number);
  258. NVIC_SetPendingIRQ(nrf_drv_swi_irq_of(swi));
  259. #endif
  260. }
  261. #if EGU_ENABLED > 0
  262. uint32_t nrf_drv_swi_task_trigger_address_get(nrf_swi_t swi, uint8_t channel)
  263. {
  264. NRF_EGU_Type * NRF_EGUx = egu_instance_get(swi);
  265. return (uint32_t) nrf_egu_task_trigger_addres_get(NRF_EGUx, channel);
  266. }
  267. uint32_t nrf_drv_swi_event_triggered_address_get(nrf_swi_t swi, uint8_t channel)
  268. {
  269. NRF_EGU_Type * NRF_EGUx = egu_instance_get(swi);
  270. return (uint32_t) nrf_egu_event_triggered_addres_get(NRF_EGUx, channel);
  271. }
  272. #endif