1 /*
2 * Copyright (c) 2015 - 2018, Nordic Semiconductor ASA
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its
16 * contributors may be used to endorse or promote products derived from this
17 * software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #ifndef NRFX_SWI_H__
33 #define NRFX_SWI_H__
34
35 #include <nrfx.h>
36
37 #if NRFX_CHECK(NRFX_EGU_ENABLED)
38 #include <hal/nrf_egu.h>
39 #endif
40
41 #ifndef SWI_COUNT
42 #define SWI_COUNT EGU_COUNT
43 #endif
44
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48
49 /**
50 * @defgroup nrfx_swi SWI driver
51 * @{
52 * @ingroup nrf_swi_egu
53 *
54 * @brief Driver for managing software interrupts (SWI).
55 */
56
57 /** @brief SWI instance. */
58 typedef uint8_t nrfx_swi_t;
59
60 /**
61 * @brief SWI user flags.
62 *
63 * User flags are set during the SWI trigger and passed to the callback function as an argument.
64 */
65 typedef uint16_t nrfx_swi_flags_t;
66
67 /** @brief Unallocated instance value. */
68 #define NRFX_SWI_UNALLOCATED ((nrfx_swi_t)0xFFuL)
69
70 /** @brief Default SWI priority. */
71 #define NRFX_SWI_DEFAULT_PRIORITY APP_IRQ_PRIORITY_LOWEST
72
73 /**
74 * @brief SWI handler function.
75 *
76 * @param swi SWI instance.
77 * @param flags User flags.
78 */
79 typedef void (*nrfx_swi_handler_t)(nrfx_swi_t swi, nrfx_swi_flags_t flags);
80
81
82 /**
83 * @brief Function for allocating the first unused SWI instance and setting a handler.
84 *
85 * If provided handler is not NULL, an allocated SWI has its interrupt enabled by default.
86 * The interrupt can be disabled by @ref nrfx_swi_int_disable.
87 *
88 * @param[out] p_swi Points to a place where the allocated SWI instance
89 * number is to be stored.
90 * @param[in] event_handler Event handler function.
91 * If NULL, no interrupt will be enabled.
92 * It can be NULL only if the EGU driver is enabled.
93 * For classic SWI, it must be a valid handler pointer.
94 * @param[in] irq_priority Interrupt priority.
95 *
96 * @retval NRFX_SUCCESS If the SWI was successfully allocated.
97 * @retval NRFX_ERROR_NO_MEM If there is no available SWI to be used.
98 */
99 nrfx_err_t nrfx_swi_alloc(nrfx_swi_t * p_swi,
100 nrfx_swi_handler_t event_handler,
101 uint32_t irq_priority);
102
103 /**
104 * @brief Function for disabling an allocated SWI interrupt.
105 *
106 * Use @ref nrfx_swi_int_enable to re-enable the interrupt.
107 *
108 * @param[in] swi SWI instance.
109 */
110 void nrfx_swi_int_disable(nrfx_swi_t swi);
111
112 /**
113 * @brief Function for enabling an allocated SWI interrupt.
114 *
115 * @param[in] swi SWI instance.
116 */
117 void nrfx_swi_int_enable(nrfx_swi_t swi);
118
119 /**
120 * @brief Function for freeing a previously allocated SWI.
121 *
122 * @param[in,out] p_swi SWI instance to free. The value is changed to
123 * @ref NRFX_SWI_UNALLOCATED on success.
124 */
125 void nrfx_swi_free(nrfx_swi_t * p_swi);
126
127 /** @brief Function for freeing all allocated SWIs. */
128 void nrfx_swi_all_free(void);
129
130 /**
131 * @brief Function for triggering the SWI.
132 *
133 * @param[in] swi SWI to trigger.
134 * @param[in] flag_number Number of user flag to trigger.
135 */
136 void nrfx_swi_trigger(nrfx_swi_t swi,
137 uint8_t flag_number);
138
139 /**
140 * @brief Function for checking if the specified SWI is currently allocated.
141 *
142 * @param[in] swi SWI instance.
143 *
144 * @retval true If the SWI instance is allocated.
145 * @retval false Otherwise.
146 */
147 bool nrfx_swi_is_allocated(nrfx_swi_t swi);
148
149 #if NRFX_CHECK(NRFX_EGU_ENABLED) || defined(__NRFX_DOXYGEN__)
150
151 /**
152 * @brief Function for returning the base address of the EGU peripheral
153 * associated with the specified SWI instance.
154 *
155 * @param[in] swi SWI instance.
156 *
157 * @returns EGU base address or NULL if the specified SWI instance number
158 * is too high.
159 */
nrfx_swi_egu_instance_get(nrfx_swi_t swi)160 __STATIC_INLINE NRF_EGU_Type * nrfx_swi_egu_instance_get(nrfx_swi_t swi)
161 {
162 #if (EGU_COUNT < SWI_COUNT)
163 if (swi >= EGU_COUNT)
164 {
165 return NULL;
166 }
167 #endif
168 uint32_t offset = ((uint32_t)swi) * ((uint32_t)NRF_EGU1 - (uint32_t)NRF_EGU0);
169 return (NRF_EGU_Type *)((uint32_t)NRF_EGU0 + offset);
170 }
171
172 /**
173 * @brief Function for returning the EGU trigger task address.
174 *
175 * @param[in] swi SWI instance.
176 * @param[in] channel Number of the EGU channel.
177 *
178 * @returns Address of EGU trigger task.
179 */
nrfx_swi_task_trigger_address_get(nrfx_swi_t swi,uint8_t channel)180 __STATIC_INLINE uint32_t nrfx_swi_task_trigger_address_get(nrfx_swi_t swi,
181 uint8_t channel)
182 {
183 NRFX_ASSERT(nrfx_swi_is_allocated(swi));
184
185 NRF_EGU_Type * p_egu = nrfx_swi_egu_instance_get(swi);
186 #if (EGU_COUNT < SWI_COUNT)
187 if (p_egu == NULL)
188 {
189 return 0;
190 }
191 #endif
192
193 return (uint32_t)nrf_egu_task_trigger_address_get(p_egu, channel);
194 }
195
196 /**
197 * @brief Function for returning the EGU triggered event address.
198 *
199 * @param[in] swi SWI instance.
200 * @param[in] channel Number of the EGU channel.
201 *
202 * @returns Address of EGU triggered event.
203 */
nrfx_swi_event_triggered_address_get(nrfx_swi_t swi,uint8_t channel)204 __STATIC_INLINE uint32_t nrfx_swi_event_triggered_address_get(nrfx_swi_t swi,
205 uint8_t channel)
206 {
207 NRFX_ASSERT(nrfx_swi_is_allocated(swi));
208
209 NRF_EGU_Type * p_egu = nrfx_swi_egu_instance_get(swi);
210 #if (EGU_COUNT < SWI_COUNT)
211 if (p_egu == NULL)
212 {
213 return 0;
214 }
215 #endif
216
217 return (uint32_t)nrf_egu_event_triggered_address_get(p_egu, channel);
218 }
219
220 #endif // NRFX_CHECK(NRFX_EGU_ENABLED) || defined(__NRFX_DOXYGEN__)
221
222
223 void nrfx_swi_0_irq_handler(void);
224 void nrfx_swi_1_irq_handler(void);
225 void nrfx_swi_2_irq_handler(void);
226 void nrfx_swi_3_irq_handler(void);
227 void nrfx_swi_4_irq_handler(void);
228 void nrfx_swi_5_irq_handler(void);
229
230 /** @} */
231
232 #ifdef __cplusplus
233 }
234 #endif
235
236 #endif // NRFX_SWI_H__
237