nrfx_swi.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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_SWI_H__
  41. #define NRFX_SWI_H__
  42. #include <nrfx.h>
  43. #if NRFX_CHECK(NRFX_EGU_ENABLED)
  44. #include <hal/nrf_egu.h>
  45. #endif
  46. #ifndef SWI_COUNT
  47. #define SWI_COUNT EGU_COUNT
  48. #endif
  49. #ifdef __cplusplus
  50. extern "C" {
  51. #endif
  52. /**
  53. * @defgroup nrfx_swi SWI driver
  54. * @{
  55. * @ingroup nrf_swi_egu
  56. *
  57. * @brief Driver for managing software interrupts (SWI).
  58. */
  59. /** @brief SWI instance. */
  60. typedef uint8_t nrfx_swi_t;
  61. /**
  62. * @brief SWI user flags.
  63. *
  64. * User flags are set during the SWI trigger and passed to the callback function as an argument.
  65. */
  66. typedef uint16_t nrfx_swi_flags_t;
  67. /** @brief Unallocated instance value. */
  68. #define NRFX_SWI_UNALLOCATED ((nrfx_swi_t)0xFFuL)
  69. /** @brief Default SWI priority. */
  70. #define NRFX_SWI_DEFAULT_PRIORITY APP_IRQ_PRIORITY_LOWEST
  71. /**
  72. * @brief SWI handler function.
  73. *
  74. * @param swi SWI instance.
  75. * @param flags User flags.
  76. */
  77. typedef void (*nrfx_swi_handler_t)(nrfx_swi_t swi, nrfx_swi_flags_t flags);
  78. /**
  79. * @brief Function for allocating the first unused SWI instance and setting a handler.
  80. *
  81. * If provided handler is not NULL, an allocated SWI has its interrupt enabled by default.
  82. * The interrupt can be disabled by @ref nrfx_swi_int_disable.
  83. *
  84. * @param[out] p_swi Points to a place where the allocated SWI instance
  85. * number is to be stored.
  86. * @param[in] event_handler Event handler function.
  87. * If NULL, no interrupt will be enabled.
  88. * It can be NULL only if the EGU driver is enabled.
  89. * For classic SWI, it must be a valid handler pointer.
  90. * @param[in] irq_priority Interrupt priority.
  91. *
  92. * @retval NRFX_SUCCESS If the SWI was successfully allocated.
  93. * @retval NRFX_ERROR_NO_MEM If there is no available SWI to be used.
  94. */
  95. nrfx_err_t nrfx_swi_alloc(nrfx_swi_t * p_swi,
  96. nrfx_swi_handler_t event_handler,
  97. uint32_t irq_priority);
  98. /**
  99. * @brief Function for disabling an allocated SWI interrupt.
  100. *
  101. * Use @ref nrfx_swi_int_enable to re-enable the interrupt.
  102. *
  103. * @param[in] swi SWI instance.
  104. */
  105. void nrfx_swi_int_disable(nrfx_swi_t swi);
  106. /**
  107. * @brief Function for enabling an allocated SWI interrupt.
  108. *
  109. * @param[in] swi SWI instance.
  110. */
  111. void nrfx_swi_int_enable(nrfx_swi_t swi);
  112. /**
  113. * @brief Function for freeing a previously allocated SWI.
  114. *
  115. * @param[in,out] p_swi SWI instance to free. The value is changed to
  116. * @ref NRFX_SWI_UNALLOCATED on success.
  117. */
  118. void nrfx_swi_free(nrfx_swi_t * p_swi);
  119. /** @brief Function for freeing all allocated SWIs. */
  120. void nrfx_swi_all_free(void);
  121. /**
  122. * @brief Function for triggering the SWI.
  123. *
  124. * @param[in] swi SWI to trigger.
  125. * @param[in] flag_number Number of user flag to trigger.
  126. */
  127. void nrfx_swi_trigger(nrfx_swi_t swi,
  128. uint8_t flag_number);
  129. /**
  130. * @brief Function for checking if the specified SWI is currently allocated.
  131. *
  132. * @param[in] swi SWI instance.
  133. *
  134. * @retval true If the SWI instance is allocated.
  135. * @retval false Otherwise.
  136. */
  137. bool nrfx_swi_is_allocated(nrfx_swi_t swi);
  138. #if NRFX_CHECK(NRFX_EGU_ENABLED) || defined(__NRFX_DOXYGEN__)
  139. /**
  140. * @brief Function for returning the base address of the EGU peripheral
  141. * associated with the specified SWI instance.
  142. *
  143. * @param[in] swi SWI instance.
  144. *
  145. * @returns EGU base address or NULL if the specified SWI instance number
  146. * is too high.
  147. */
  148. __STATIC_INLINE NRF_EGU_Type * nrfx_swi_egu_instance_get(nrfx_swi_t swi)
  149. {
  150. #if (EGU_COUNT < SWI_COUNT)
  151. if (swi >= EGU_COUNT)
  152. {
  153. return NULL;
  154. }
  155. #endif
  156. uint32_t offset = ((uint32_t)swi) * ((uint32_t)NRF_EGU1 - (uint32_t)NRF_EGU0);
  157. return (NRF_EGU_Type *)((uint32_t)NRF_EGU0 + offset);
  158. }
  159. /**
  160. * @brief Function for returning the EGU trigger task address.
  161. *
  162. * @param[in] swi SWI instance.
  163. * @param[in] channel Number of the EGU channel.
  164. *
  165. * @returns Address of EGU trigger task.
  166. */
  167. __STATIC_INLINE uint32_t nrfx_swi_task_trigger_address_get(nrfx_swi_t swi,
  168. uint8_t channel)
  169. {
  170. NRFX_ASSERT(nrfx_swi_is_allocated(swi));
  171. NRF_EGU_Type * p_egu = nrfx_swi_egu_instance_get(swi);
  172. #if (EGU_COUNT < SWI_COUNT)
  173. if (p_egu == NULL)
  174. {
  175. return 0;
  176. }
  177. #endif
  178. return (uint32_t)nrf_egu_task_trigger_address_get(p_egu, channel);
  179. }
  180. /**
  181. * @brief Function for returning the EGU triggered event address.
  182. *
  183. * @param[in] swi SWI instance.
  184. * @param[in] channel Number of the EGU channel.
  185. *
  186. * @returns Address of EGU triggered event.
  187. */
  188. __STATIC_INLINE uint32_t nrfx_swi_event_triggered_address_get(nrfx_swi_t swi,
  189. uint8_t channel)
  190. {
  191. NRFX_ASSERT(nrfx_swi_is_allocated(swi));
  192. NRF_EGU_Type * p_egu = nrfx_swi_egu_instance_get(swi);
  193. #if (EGU_COUNT < SWI_COUNT)
  194. if (p_egu == NULL)
  195. {
  196. return 0;
  197. }
  198. #endif
  199. return (uint32_t)nrf_egu_event_triggered_address_get(p_egu, channel);
  200. }
  201. #endif // NRFX_CHECK(NRFX_EGU_ENABLED) || defined(__NRFX_DOXYGEN__)
  202. void nrfx_swi_0_irq_handler(void);
  203. void nrfx_swi_1_irq_handler(void);
  204. void nrfx_swi_2_irq_handler(void);
  205. void nrfx_swi_3_irq_handler(void);
  206. void nrfx_swi_4_irq_handler(void);
  207. void nrfx_swi_5_irq_handler(void);
  208. /** @} */
  209. #ifdef __cplusplus
  210. }
  211. #endif
  212. #endif // NRFX_SWI_H__