1 /*
2 * Copyright (c) 2015 - 2023, Nordic Semiconductor ASA
3 * All rights reserved.
4 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright notice, this
11 * list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the copyright holder nor the names of its
18 * contributors may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #ifndef NRFX_PPI_H__
35 #define NRFX_PPI_H__
36
37 #include <nrfx.h>
38 #include <hal/nrf_ppi.h>
39
40 /**
41 * @defgroup nrfx_ppi PPI allocator
42 * @{
43 * @ingroup nrf_ppi
44 * @brief Programmable Peripheral Interconnect (PPI) allocator.
45 */
46
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50
51 #if !defined (NRFX_PPI_CHANNELS_USED) && !defined(__NRFX_DOXYGEN__)
52 /* Bitfield representing PPI channels used by external modules. */
53 #define NRFX_PPI_CHANNELS_USED 0
54 #endif
55
56 #if !defined(NRFX_PPI_GROUPS_USED) && !defined(__NRFX_DOXYGEN__)
57 /* Bitfield representing PPI groups used by external modules. */
58 #define NRFX_PPI_GROUPS_USED 0
59 #endif
60
61 #if (PPI_CH_NUM > 16) || defined(__NRFX_DOXYGEN__)
62 /** @brief Bitfield representing all PPI channels available to the application. */
63 #define NRFX_PPI_ALL_APP_CHANNELS_MASK ((uint32_t)0xFFFFFFFFuL & ~(NRFX_PPI_CHANNELS_USED))
64 /** @brief Bitfield representing programmable PPI channels available to the application. */
65 #define NRFX_PPI_PROG_APP_CHANNELS_MASK ((uint32_t)0x000FFFFFuL & ~(NRFX_PPI_CHANNELS_USED))
66 #else
67 #define NRFX_PPI_ALL_APP_CHANNELS_MASK ((uint32_t)0xFFF0FFFFuL & ~(NRFX_PPI_CHANNELS_USED))
68 #define NRFX_PPI_PROG_APP_CHANNELS_MASK ((uint32_t)0x0000FFFFuL & ~(NRFX_PPI_CHANNELS_USED))
69 #endif
70
71 /** @brief Bitfield representing all PPI groups available to the application. */
72 #define NRFX_PPI_ALL_APP_GROUPS_MASK (((1uL << PPI_GROUP_NUM) - 1) & ~(NRFX_PPI_GROUPS_USED))
73
74 /**
75 * @brief Function for uninitializing the PPI module.
76 *
77 * This function disables all channels and clears the channel groups.
78 */
79 void nrfx_ppi_free_all(void);
80
81 /**
82 * @brief Function for allocating a PPI channel.
83 * @details This function allocates the first unused PPI channel.
84 *
85 * @note Function is thread safe as it uses @ref nrfx_flag32_alloc.
86 *
87 * @param[out] p_channel Pointer to the PPI channel that has been allocated.
88 *
89 * @retval NRFX_SUCCESS The channel was successfully allocated.
90 * @retval NRFX_ERROR_NO_MEM There is no available channel to be used.
91 */
92 nrfx_err_t nrfx_ppi_channel_alloc(nrf_ppi_channel_t * p_channel);
93
94 /**
95 * @brief Function for freeing a PPI channel.
96 * @details This function also disables the chosen channel.
97 *
98 * @note Function is thread safe as it uses @ref nrfx_flag32_free.
99 *
100 * @param[in] channel PPI channel to be freed.
101 *
102 * @retval NRFX_SUCCESS The channel was successfully freed.
103 * @retval NRFX_ERROR_INVALID_PARAM The channel is not user-configurable.
104 */
105 nrfx_err_t nrfx_ppi_channel_free(nrf_ppi_channel_t channel);
106
107 /**
108 * @brief Function for assigning task and event endpoints to the PPI channel.
109 *
110 * @param[in] channel PPI channel to be assigned endpoints.
111 * @param[in] eep Event endpoint address.
112 * @param[in] tep Task endpoint address.
113 *
114 * @retval NRFX_SUCCESS The channel was successfully assigned.
115 * @retval NRFX_ERROR_INVALID_STATE The channel is not allocated for the user.
116 * @retval NRFX_ERROR_INVALID_PARAM The channel is not user-configurable.
117 */
118 nrfx_err_t nrfx_ppi_channel_assign(nrf_ppi_channel_t channel, uint32_t eep, uint32_t tep);
119
120 /**
121 * @brief Function for assigning fork endpoint to the PPI channel or clearing it.
122 *
123 * @param[in] channel PPI channel to be assigned endpoints.
124 * @param[in] fork_tep Fork task endpoint address or 0 to clear.
125 *
126 * @retval NRFX_SUCCESS The channel was successfully assigned.
127 * @retval NRFX_ERROR_INVALID_STATE The channel is not allocated for the user.
128 * @retval NRFX_ERROR_NOT_SUPPORTED Function is not supported.
129 */
130 nrfx_err_t nrfx_ppi_channel_fork_assign(nrf_ppi_channel_t channel, uint32_t fork_tep);
131
132 /**
133 * @brief Function for enabling a PPI channel.
134 *
135 * @param[in] channel PPI channel to be enabled.
136 *
137 * @retval NRFX_SUCCESS The channel was successfully enabled.
138 * @retval NRFX_ERROR_INVALID_STATE The user-configurable channel is not allocated.
139 * @retval NRFX_ERROR_INVALID_PARAM The channel cannot be enabled by the user.
140 */
141 nrfx_err_t nrfx_ppi_channel_enable(nrf_ppi_channel_t channel);
142
143 /**
144 * @brief Function for disabling a PPI channel.
145 *
146 * @param[in] channel PPI channel to be disabled.
147 *
148 * @retval NRFX_SUCCESS The channel was successfully disabled.
149 * @retval NRFX_ERROR_INVALID_STATE The user-configurable channel is not allocated.
150 * @retval NRFX_ERROR_INVALID_PARAM The channel cannot be disabled by the user.
151 */
152 nrfx_err_t nrfx_ppi_channel_disable(nrf_ppi_channel_t channel);
153
154 /**
155 * @brief Function for allocating a PPI channel group.
156 * @details This function allocates the first unused PPI group.
157 *
158 * @note Function is thread safe as it uses @ref nrfx_flag32_alloc.
159 *
160 * @param[out] p_group Pointer to the PPI channel group that has been allocated.
161 *
162 * @retval NRFX_SUCCESS The channel group was successfully allocated.
163 * @retval NRFX_ERROR_NO_MEM There is no available channel group to be used.
164 */
165 nrfx_err_t nrfx_ppi_group_alloc(nrf_ppi_channel_group_t * p_group);
166
167 /**
168 * @brief Function for freeing a PPI channel group.
169 * @details This function also disables the chosen group.
170 *
171 * @note Function is thread safe as it uses @ref nrfx_flag32_free.
172 *
173 * @param[in] group PPI channel group to be freed.
174 *
175 * @retval NRFX_SUCCESS The channel group was successfully freed.
176 * @retval NRFX_ERROR_INVALID_PARAM The channel group is not user-configurable.
177 */
178 nrfx_err_t nrfx_ppi_group_free(nrf_ppi_channel_group_t group);
179
180 /**
181 * @brief Compute a channel mask for NRF_PPI registers.
182 *
183 * @param[in] channel Channel number to transform to a mask.
184 *
185 * @return Channel mask.
186 */
187 NRFX_STATIC_INLINE uint32_t nrfx_ppi_channel_to_mask(nrf_ppi_channel_t channel);
188
189 /**
190 * @brief Function for including multiple PPI channels in a channel group.
191 *
192 * @param[in] channel_mask PPI channels to be added.
193 * @param[in] group Channel group in which to include the channels.
194 *
195 * @retval NRFX_SUCCESS The channels was successfully included.
196 * @retval NRFX_ERROR_INVALID_PARAM Group is not an application group or channels are not an
197 * application channels.
198 * @retval NRFX_ERROR_INVALID_STATE Group is not an allocated group.
199 */
200 nrfx_err_t nrfx_ppi_channels_include_in_group(uint32_t channel_mask,
201 nrf_ppi_channel_group_t group);
202
203 /**
204 * @brief Function for including a PPI channel in a channel group.
205 *
206 * @param[in] channel PPI channel to be added.
207 * @param[in] group Channel group in which to include the channel.
208 *
209 * @retval NRFX_SUCCESS The channel was successfully included.
210 * @retval NRFX_ERROR_INVALID_PARAM Group is not an application group or channel is not an
211 * application channel.
212 * @retval NRFX_ERROR_INVALID_STATE Group is not an allocated group.
213 */
214 NRFX_STATIC_INLINE nrfx_err_t nrfx_ppi_channel_include_in_group(nrf_ppi_channel_t channel,
215 nrf_ppi_channel_group_t group);
216
217 /**
218 * @brief Function for removing multiple PPI channels from a channel group.
219 *
220 * @param[in] channel_mask PPI channels to be removed.
221 * @param[in] group Channel group from which to remove the channels.
222 *
223 * @retval NRFX_SUCCESS The channel was successfully removed.
224 * @retval NRFX_ERROR_INVALID_PARAM Group is not an application group or channels are not an
225 * application channels.
226 * @retval NRFX_ERROR_INVALID_STATE Group is not an allocated group.
227 */
228 nrfx_err_t nrfx_ppi_channels_remove_from_group(uint32_t channel_mask,
229 nrf_ppi_channel_group_t group);
230
231 /**
232 * @brief Function for removing a single PPI channel from a channel group.
233 *
234 * @param[in] channel PPI channel to be removed.
235 * @param[in] group Channel group from which to remove the channel.
236 *
237 * @retval NRFX_SUCCESS The channel was successfully removed.
238 * @retval NRFX_ERROR_INVALID_PARAM Group is not an application group or channel is not an
239 * application channel.
240 * @retval NRFX_ERROR_INVALID_STATE Group is not an allocated group.
241 */
242 NRFX_STATIC_INLINE nrfx_err_t nrfx_ppi_channel_remove_from_group(nrf_ppi_channel_t channel,
243 nrf_ppi_channel_group_t group);
244
245 /**
246 * @brief Function for clearing a PPI channel group.
247 *
248 * @param[in] group Channel group to be cleared.
249 *
250 * @retval NRFX_SUCCESS The group was successfully cleared.
251 * @retval NRFX_ERROR_INVALID_PARAM Group is not an application group.
252 * @retval NRFX_ERROR_INVALID_STATE Group is not an allocated group.
253 */
254 NRFX_STATIC_INLINE nrfx_err_t nrfx_ppi_group_clear(nrf_ppi_channel_group_t group);
255
256 /**
257 * @brief Function for enabling a PPI channel group.
258 *
259 * @param[in] group Channel group to be enabled.
260 *
261 * @retval NRFX_SUCCESS The group was successfully enabled.
262 * @retval NRFX_ERROR_INVALID_PARAM Group is not an application group.
263 * @retval NRFX_ERROR_INVALID_STATE Group is not an allocated group.
264 */
265 nrfx_err_t nrfx_ppi_group_enable(nrf_ppi_channel_group_t group);
266
267 /**
268 * @brief Function for disabling a PPI channel group.
269 *
270 * @param[in] group Channel group to be disabled.
271 *
272 * @retval NRFX_SUCCESS The group was successfully disabled.
273 * @retval NRFX_ERROR_INVALID_PARAM Group is not an application group.
274 * @retval NRFX_ERROR_INVALID_STATE Group is not an allocated group.
275 */
276 nrfx_err_t nrfx_ppi_group_disable(nrf_ppi_channel_group_t group);
277
278 /**
279 * @brief Function for getting the address of a PPI task.
280 *
281 * @param[in] task Task.
282 *
283 * @return Task address.
284 */
285 NRFX_STATIC_INLINE uint32_t nrfx_ppi_task_address_get(nrf_ppi_task_t task);
286
287 /**
288 * @brief Function for getting the address of the enable task of a PPI group.
289 *
290 * @param[in] group PPI channel group
291 *
292 * @return Task address.
293 */
294 NRFX_STATIC_INLINE uint32_t nrfx_ppi_task_group_enable_address_get(nrf_ppi_channel_group_t group);
295
296 /**
297 * @brief Function for getting the address of the enable task of a PPI group.
298 *
299 * @param[in] group PPI channel group
300 *
301 * @return Task address.
302 */
303 NRFX_STATIC_INLINE uint32_t nrfx_ppi_task_group_disable_address_get(nrf_ppi_channel_group_t group);
304
305 #ifndef NRFX_DECLARE_ONLY
nrfx_ppi_channel_to_mask(nrf_ppi_channel_t channel)306 NRFX_STATIC_INLINE uint32_t nrfx_ppi_channel_to_mask(nrf_ppi_channel_t channel)
307 {
308 return (1uL << (uint32_t) channel);
309 }
310
nrfx_ppi_channel_include_in_group(nrf_ppi_channel_t channel,nrf_ppi_channel_group_t group)311 NRFX_STATIC_INLINE nrfx_err_t nrfx_ppi_channel_include_in_group(nrf_ppi_channel_t channel,
312 nrf_ppi_channel_group_t group)
313 {
314 return nrfx_ppi_channels_include_in_group(nrfx_ppi_channel_to_mask(channel), group);
315 }
316
nrfx_ppi_channel_remove_from_group(nrf_ppi_channel_t channel,nrf_ppi_channel_group_t group)317 NRFX_STATIC_INLINE nrfx_err_t nrfx_ppi_channel_remove_from_group(nrf_ppi_channel_t channel,
318 nrf_ppi_channel_group_t group)
319 {
320 return nrfx_ppi_channels_remove_from_group(nrfx_ppi_channel_to_mask(channel), group);
321 }
322
nrfx_ppi_group_clear(nrf_ppi_channel_group_t group)323 NRFX_STATIC_INLINE nrfx_err_t nrfx_ppi_group_clear(nrf_ppi_channel_group_t group)
324 {
325 return nrfx_ppi_channels_remove_from_group(NRFX_PPI_ALL_APP_CHANNELS_MASK, group);
326 }
327
nrfx_ppi_task_address_get(nrf_ppi_task_t task)328 NRFX_STATIC_INLINE uint32_t nrfx_ppi_task_address_get(nrf_ppi_task_t task)
329 {
330 return nrf_ppi_task_address_get(NRF_PPI, task);
331 }
332
nrfx_ppi_task_group_enable_address_get(nrf_ppi_channel_group_t group)333 NRFX_STATIC_INLINE uint32_t nrfx_ppi_task_group_enable_address_get(nrf_ppi_channel_group_t group)
334 {
335 return nrf_ppi_task_group_enable_address_get(NRF_PPI, group);
336 }
337
nrfx_ppi_task_group_disable_address_get(nrf_ppi_channel_group_t group)338 NRFX_STATIC_INLINE uint32_t nrfx_ppi_task_group_disable_address_get(nrf_ppi_channel_group_t group)
339 {
340 return nrf_ppi_task_group_disable_address_get(NRF_PPI, group);
341 }
342 #endif // NRFX_DECLARE_ONLY
343
344 /** @} */
345
346 #ifdef __cplusplus
347 }
348 #endif
349
350 #endif // NRFX_PPI_H__
351