nrfx_swi.h 6.7 KB

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