1 /*
2  * Copyright (c) 2015 - 2020, 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_PPI_H__
33 #define NRFX_PPI_H__
34 
35 #include <nrfx.h>
36 #include <hal/nrf_ppi.h>
37 
38 /**
39  * @defgroup nrfx_ppi PPI allocator
40  * @{
41  * @ingroup nrf_ppi
42  * @brief   Programmable Peripheral Interconnect (PPI) allocator.
43  */
44 
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48 
49 #if !defined (NRFX_PPI_CHANNELS_USED) || defined(__NRFX_DOXYGEN__)
50 /** @brief Bitfield representing PPI channels used by external modules. */
51 #define NRFX_PPI_CHANNELS_USED 0
52 #endif
53 
54 #if !defined(NRFX_PPI_GROUPS_USED) || defined(__NRFX_DOXYGEN__)
55 /** @brief Bitfield representing PPI groups used by external modules. */
56 #define NRFX_PPI_GROUPS_USED 0
57 #endif
58 
59 #if (PPI_CH_NUM > 16) || defined(__NRFX_DOXYGEN__)
60 /** @brief Bitfield representing all PPI channels available to the application. */
61 #define NRFX_PPI_ALL_APP_CHANNELS_MASK   ((uint32_t)0xFFFFFFFFuL & ~(NRFX_PPI_CHANNELS_USED))
62 /** @brief Bitfield representing programmable PPI channels available to the application. */
63 #define NRFX_PPI_PROG_APP_CHANNELS_MASK  ((uint32_t)0x000FFFFFuL & ~(NRFX_PPI_CHANNELS_USED))
64 #else
65 #define NRFX_PPI_ALL_APP_CHANNELS_MASK   ((uint32_t)0xFFF0FFFFuL & ~(NRFX_PPI_CHANNELS_USED))
66 #define NRFX_PPI_PROG_APP_CHANNELS_MASK  ((uint32_t)0x0000FFFFuL & ~(NRFX_PPI_CHANNELS_USED))
67 #endif
68 
69 /** @brief Bitfield representing all PPI groups available to the application. */
70 #define NRFX_PPI_ALL_APP_GROUPS_MASK     (((1uL << PPI_GROUP_NUM) - 1) & ~(NRFX_PPI_GROUPS_USED))
71 
72 /**
73  * @brief Function for uninitializing the PPI module.
74  *
75  * This function disables all channels and clears the channel groups.
76  */
77 void nrfx_ppi_free_all(void);
78 
79 /**
80  * @brief Function for allocating a PPI channel.
81  * @details This function allocates the first unused PPI channel.
82  *
83  * @param[out] p_channel Pointer to the PPI channel that has been allocated.
84  *
85  * @retval NRFX_SUCCESS      The channel was successfully allocated.
86  * @retval NRFX_ERROR_NO_MEM There is no available channel to be used.
87  */
88 nrfx_err_t nrfx_ppi_channel_alloc(nrf_ppi_channel_t * p_channel);
89 
90 /**
91  * @brief Function for freeing a PPI channel.
92  * @details This function also disables the chosen channel.
93  *
94  * @param[in] channel PPI channel to be freed.
95  *
96  * @retval NRFX_SUCCESS             The channel was successfully freed.
97  * @retval NRFX_ERROR_INVALID_PARAM The channel is not user-configurable.
98  */
99 nrfx_err_t nrfx_ppi_channel_free(nrf_ppi_channel_t channel);
100 
101 /**
102  * @brief Function for assigning task and event endpoints to the PPI channel.
103  *
104  * @param[in] channel PPI channel to be assigned endpoints.
105  * @param[in] eep     Event endpoint address.
106  * @param[in] tep     Task endpoint address.
107  *
108  * @retval NRFX_SUCCESS             The channel was successfully assigned.
109  * @retval NRFX_ERROR_INVALID_STATE The channel is not allocated for the user.
110  * @retval NRFX_ERROR_INVALID_PARAM The channel is not user-configurable.
111  */
112 nrfx_err_t nrfx_ppi_channel_assign(nrf_ppi_channel_t channel, uint32_t eep, uint32_t tep);
113 
114 /**
115  * @brief Function for assigning fork endpoint to the PPI channel or clearing it.
116  *
117  * @param[in] channel  PPI channel to be assigned endpoints.
118  * @param[in] fork_tep Fork task endpoint address or 0 to clear.
119  *
120  * @retval NRFX_SUCCESS             The channel was successfully assigned.
121  * @retval NRFX_ERROR_INVALID_STATE The channel is not allocated for the user.
122  * @retval NRFX_ERROR_NOT_SUPPORTED Function is not supported.
123  */
124 nrfx_err_t nrfx_ppi_channel_fork_assign(nrf_ppi_channel_t channel, uint32_t fork_tep);
125 
126 /**
127  * @brief Function for enabling a PPI channel.
128  *
129  * @param[in] channel PPI channel to be enabled.
130  *
131  * @retval NRFX_SUCCESS             The channel was successfully enabled.
132  * @retval NRFX_ERROR_INVALID_STATE The user-configurable channel is not allocated.
133  * @retval NRFX_ERROR_INVALID_PARAM The channel cannot be enabled by the user.
134  */
135 nrfx_err_t nrfx_ppi_channel_enable(nrf_ppi_channel_t channel);
136 
137 /**
138  * @brief Function for disabling a PPI channel.
139  *
140  * @param[in] channel PPI channel to be disabled.
141  *
142  * @retval NRFX_SUCCESS             The channel was successfully disabled.
143  * @retval NRFX_ERROR_INVALID_STATE The user-configurable channel is not allocated.
144  * @retval NRFX_ERROR_INVALID_PARAM The channel cannot be disabled by the user.
145  */
146 nrfx_err_t nrfx_ppi_channel_disable(nrf_ppi_channel_t channel);
147 
148 /**
149  * @brief Function for allocating a PPI channel group.
150  * @details This function allocates the first unused PPI group.
151  *
152  * @param[out] p_group Pointer to the PPI channel group that has been allocated.
153  *
154  * @retval NRFX_SUCCESS      The channel group was successfully allocated.
155  * @retval NRFX_ERROR_NO_MEM There is no available channel group to be used.
156  */
157 nrfx_err_t nrfx_ppi_group_alloc(nrf_ppi_channel_group_t * p_group);
158 
159 /**
160  * @brief Function for freeing a PPI channel group.
161  * @details This function also disables the chosen group.
162  *
163  * @param[in] group PPI channel group to be freed.
164  *
165  * @retval NRFX_SUCCESS             The channel group was successfully freed.
166  * @retval NRFX_ERROR_INVALID_PARAM The channel group is not user-configurable.
167  */
168 nrfx_err_t nrfx_ppi_group_free(nrf_ppi_channel_group_t group);
169 
170 /**
171  * @brief  Compute a channel mask for NRF_PPI registers.
172  *
173  * @param[in] channel Channel number to transform to a mask.
174  *
175  * @return Channel mask.
176  */
177 NRFX_STATIC_INLINE uint32_t nrfx_ppi_channel_to_mask(nrf_ppi_channel_t channel);
178 
179 /**
180  * @brief Function for including multiple PPI channels in a channel group.
181  *
182  * @param[in] channel_mask PPI channels to be added.
183  * @param[in] group        Channel group in which to include the channels.
184  *
185  * @retval NRFX_SUCCESS             The channels was successfully included.
186  * @retval NRFX_ERROR_INVALID_PARAM Group is not an application group or channels are not an
187  *                                  application channels.
188  * @retval NRFX_ERROR_INVALID_STATE Group is not an allocated group.
189  */
190 nrfx_err_t nrfx_ppi_channels_include_in_group(uint32_t                channel_mask,
191                                               nrf_ppi_channel_group_t group);
192 
193 /**
194  * @brief Function for including a PPI channel in a channel group.
195  *
196  * @param[in] channel PPI channel to be added.
197  * @param[in] group   Channel group in which to include the channel.
198  *
199  * @retval NRFX_SUCCESS             The channel was successfully included.
200  * @retval NRFX_ERROR_INVALID_PARAM Group is not an application group or channel is not an
201  *                                  application channel.
202  * @retval NRFX_ERROR_INVALID_STATE Group is not an allocated group.
203  */
204 NRFX_STATIC_INLINE nrfx_err_t nrfx_ppi_channel_include_in_group(nrf_ppi_channel_t       channel,
205                                                                 nrf_ppi_channel_group_t group);
206 
207 /**
208  * @brief Function for removing multiple PPI channels from a channel group.
209  *
210  * @param[in] channel_mask PPI channels to be removed.
211  * @param[in] group        Channel group from which to remove the channels.
212  *
213  * @retval NRFX_SUCCESS             The channel was successfully removed.
214  * @retval NRFX_ERROR_INVALID_PARAM Group is not an application group or channels are not an
215  *                                  application channels.
216  * @retval NRFX_ERROR_INVALID_STATE Group is not an allocated group.
217  */
218 nrfx_err_t nrfx_ppi_channels_remove_from_group(uint32_t                channel_mask,
219                                                nrf_ppi_channel_group_t group);
220 
221 /**
222  * @brief Function for removing a single PPI channel from a channel group.
223  *
224  * @param[in] channel PPI channel to be removed.
225  * @param[in] group   Channel group from which to remove the channel.
226  *
227  * @retval NRFX_SUCCESS             The channel was successfully removed.
228  * @retval NRFX_ERROR_INVALID_PARAM Group is not an application group or channel is not an
229  *                                  application channel.
230  * @retval NRFX_ERROR_INVALID_STATE Group is not an allocated group.
231  */
232 NRFX_STATIC_INLINE nrfx_err_t nrfx_ppi_channel_remove_from_group(nrf_ppi_channel_t       channel,
233                                                                  nrf_ppi_channel_group_t group);
234 
235 /**
236  * @brief Function for clearing a PPI channel group.
237  *
238  * @param[in] group Channel group to be cleared.
239  *
240  * @retval NRFX_SUCCESS             The group was successfully cleared.
241  * @retval NRFX_ERROR_INVALID_PARAM Group is not an application group.
242  * @retval NRFX_ERROR_INVALID_STATE Group is not an allocated group.
243  */
244 NRFX_STATIC_INLINE nrfx_err_t nrfx_ppi_group_clear(nrf_ppi_channel_group_t group);
245 
246 /**
247  * @brief Function for enabling a PPI channel group.
248  *
249  * @param[in] group Channel group to be enabled.
250  *
251  * @retval NRFX_SUCCESS             The group was successfully enabled.
252  * @retval NRFX_ERROR_INVALID_PARAM Group is not an application group.
253  * @retval NRFX_ERROR_INVALID_STATE Group is not an allocated group.
254  */
255 nrfx_err_t nrfx_ppi_group_enable(nrf_ppi_channel_group_t group);
256 
257 /**
258  * @brief Function for disabling a PPI channel group.
259  *
260  * @param[in] group Channel group to be disabled.
261  *
262  * @retval NRFX_SUCCESS             The group was successfully disabled.
263  * @retval NRFX_ERROR_INVALID_PARAM Group is not an application group.
264  * @retval NRFX_ERROR_INVALID_STATE Group is not an allocated group.
265  */
266 nrfx_err_t nrfx_ppi_group_disable(nrf_ppi_channel_group_t group);
267 
268 /**
269  * @brief Function for getting the address of a PPI task.
270  *
271  * @param[in] task Task.
272  *
273  * @return Task address.
274  */
275 NRFX_STATIC_INLINE uint32_t nrfx_ppi_task_addr_get(nrf_ppi_task_t task);
276 
277 /**
278  * @brief Function for getting the address of the enable task of a PPI group.
279  *
280  * @param[in] group PPI channel group
281  *
282  * @return Task address.
283  */
284 NRFX_STATIC_INLINE uint32_t nrfx_ppi_task_addr_group_enable_get(nrf_ppi_channel_group_t group);
285 
286 /**
287  * @brief Function for getting the address of the enable task of a PPI group.
288  *
289  * @param[in] group PPI channel group
290  *
291  * @return Task address.
292  */
293 NRFX_STATIC_INLINE uint32_t nrfx_ppi_task_addr_group_disable_get(nrf_ppi_channel_group_t group);
294 
295 #ifndef NRFX_DECLARE_ONLY
nrfx_ppi_channel_to_mask(nrf_ppi_channel_t channel)296 NRFX_STATIC_INLINE uint32_t nrfx_ppi_channel_to_mask(nrf_ppi_channel_t channel)
297 {
298     return (1uL << (uint32_t) channel);
299 }
300 
nrfx_ppi_channel_include_in_group(nrf_ppi_channel_t channel,nrf_ppi_channel_group_t group)301 NRFX_STATIC_INLINE nrfx_err_t nrfx_ppi_channel_include_in_group(nrf_ppi_channel_t       channel,
302                                                                 nrf_ppi_channel_group_t group)
303 {
304     return nrfx_ppi_channels_include_in_group(nrfx_ppi_channel_to_mask(channel), group);
305 }
306 
nrfx_ppi_channel_remove_from_group(nrf_ppi_channel_t channel,nrf_ppi_channel_group_t group)307 NRFX_STATIC_INLINE nrfx_err_t nrfx_ppi_channel_remove_from_group(nrf_ppi_channel_t       channel,
308                                                                  nrf_ppi_channel_group_t group)
309 {
310     return nrfx_ppi_channels_remove_from_group(nrfx_ppi_channel_to_mask(channel), group);
311 }
312 
nrfx_ppi_group_clear(nrf_ppi_channel_group_t group)313 NRFX_STATIC_INLINE nrfx_err_t nrfx_ppi_group_clear(nrf_ppi_channel_group_t group)
314 {
315     return nrfx_ppi_channels_remove_from_group(NRFX_PPI_ALL_APP_CHANNELS_MASK, group);
316 }
317 
nrfx_ppi_task_addr_get(nrf_ppi_task_t task)318 NRFX_STATIC_INLINE uint32_t nrfx_ppi_task_addr_get(nrf_ppi_task_t task)
319 {
320     return nrf_ppi_task_address_get(NRF_PPI, task);
321 }
322 
nrfx_ppi_task_addr_group_enable_get(nrf_ppi_channel_group_t group)323 NRFX_STATIC_INLINE uint32_t nrfx_ppi_task_addr_group_enable_get(nrf_ppi_channel_group_t group)
324 {
325     return nrf_ppi_task_group_enable_address_get(NRF_PPI, group);
326 }
327 
nrfx_ppi_task_addr_group_disable_get(nrf_ppi_channel_group_t group)328 NRFX_STATIC_INLINE uint32_t nrfx_ppi_task_addr_group_disable_get(nrf_ppi_channel_group_t group)
329 {
330     return nrf_ppi_task_group_disable_address_get(NRF_PPI, group);
331 }
332 #endif // NRFX_DECLARE_ONLY
333 
334 /** @} */
335 
336 #ifdef __cplusplus
337 }
338 #endif
339 
340 #endif // NRFX_PPI_H__
341