1 /*
2  * Copyright (c) 2018 - 2025, 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_DPPI_H__
35 #define NRFX_DPPI_H__
36 
37 #include <nrfx.h>
38 #include <haly/nrfy_dppi.h>
39 
40 /* On devices with single instance (with no ID) use instance 0. */
41 #if defined(NRF_DPPIC) && defined(NRFX_DPPI_ENABLED) && !defined(NRFX_DPPI0_ENABLED)
42 #define NRFX_DPPI0_ENABLED 1
43 #endif
44 
45 /**
46  * @defgroup nrfx_dppi DPPI allocator
47  * @{
48  * @ingroup nrf_dppi
49  * @brief   Distributed Programmable Peripheral Interconnect (DPPI) allocator.
50  */
51 
52 /** @brief Data structure of the Distributed programmable peripheral interconnect (DPPI) driver instance. */
53 typedef struct
54 {
55     NRF_DPPIC_Type * p_reg;        ///< Pointer to a structure containing DPPIC registers.
56     uint8_t          drv_inst_idx; ///< Index of the driver instance. For internal use only.
57 } nrfx_dppi_t;
58 
59 #ifndef __NRFX_DOXYGEN__
60 enum {
61     /* List all enabled driver instances (in the format NRFX_\<instance_name\>_INST_IDX). */
62     NRFX_INSTANCE_ENUM_LIST(DPPI)
63     NRFX_DPPI_ENABLED_COUNT
64 };
65 #endif
66 
67 /** @brief Macro for creating an instance of the DPPIC driver. */
68 #define NRFX_DPPI_INSTANCE(id)                            \
69 {                                                          \
70     .p_reg        = NRFX_CONCAT(NRF_, DPPIC, id),          \
71     .drv_inst_idx = NRFX_CONCAT(NRFX_DPPI, id, _INST_IDX), \
72 }
73 
74 #ifdef __cplusplus
75 extern "C" {
76 #endif
77 
78 #if NRFX_API_VER_AT_LEAST(3, 8, 0) || defined(__NRFX_DOXYGEN__)
79 
80 /**
81  * @brief Function for freeing all allocated channels and groups.
82  *
83  * @param[in]  p_instance Pointer to the driver instance structure.
84  */
85 void nrfx_dppi_free(nrfx_dppi_t const * p_instance);
86 
87 /**
88  * @brief Function for allocating a DPPI channel.
89  * @details This function allocates the first unused DPPI channel.
90  *
91  * @note Function is thread safe as it uses @ref nrfx_flag32_alloc.
92  *
93  * @param[in]  p_instance Pointer to the driver instance structure.
94  * @param[out] p_channel  Pointer to the DPPI channel number that has been allocated.
95  *
96  * @retval NRFX_SUCCESS      The channel was successfully allocated.
97  * @retval NRFX_ERROR_NO_MEM There is no available channel to be used.
98  */
99 nrfx_err_t nrfx_dppi_channel_alloc(nrfx_dppi_t const * p_instance, uint8_t * p_channel);
100 
101 /**
102  * @brief Function for freeing a DPPI channel.
103  * @details This function also disables the chosen channel. Configuration in
104  *          PUBLISH/SUBSCRIBE registers used for the channel is not cleared.
105  *
106  * @note Function is thread safe as it uses @ref nrfx_flag32_free.
107  *
108  * @param[in] p_instance Pointer to the driver instance structure.
109  * @param[in] channel    DPPI channel to be freed.
110  *
111  * @retval NRFX_SUCCESS             The channel was successfully freed.
112  * @retval NRFX_ERROR_INVALID_PARAM The specified channel is not allocated.
113  */
114 nrfx_err_t nrfx_dppi_channel_free(nrfx_dppi_t const * p_instance, uint8_t channel);
115 
116 /**
117  * @brief Function for enabling a DPPI channel.
118  *
119  * @param[in] p_instance Pointer to the driver instance structure.
120  * @param[in] channel    DPPI channel to be enabled.
121  *
122  * @retval NRFX_SUCCESS             The channel was successfully enabled.
123  * @retval NRFX_ERROR_INVALID_PARAM The specified channel is not allocated.
124  */
125 nrfx_err_t nrfx_dppi_channel_enable(nrfx_dppi_t const * p_instance, uint8_t channel);
126 
127 /**
128  * @brief Function for disabling a DPPI channel.
129  *
130  * @note Disabling channel does not modify PUBLISH/SUBSCRIBE registers configured to use
131  *       that channel.
132  *
133  * @param[in] p_instance Pointer to the driver instance structure.
134  * @param[in] channel    DPPI channel to be disabled.
135  *
136  * @retval NRFX_SUCCESS             The channel was successfully disabled.
137  * @retval NRFX_ERROR_INVALID_PARAM The specified channel is not allocated.
138  */
139 nrfx_err_t nrfx_dppi_channel_disable(nrfx_dppi_t const * p_instance, uint8_t channel);
140 
141 /**
142  * @brief Function for allocating a DPPI channel group.
143  * @details This function allocates the first unused DPPI group.
144  *
145  * @note Function is thread safe as it uses @ref nrfx_flag32_alloc.
146  *
147  * @param[in]  p_instance Pointer to the driver instance structure.
148  * @param[out] p_group    Pointer to the DPPI channel group that has been allocated.
149  *
150  * @retval NRFX_SUCCESS      The channel group was successfully allocated.
151  * @retval NRFX_ERROR_NO_MEM There is no available channel group to be used.
152  */
153 nrfx_err_t nrfx_dppi_group_alloc(nrfx_dppi_t const *        p_instance,
154                                  nrf_dppi_channel_group_t * p_group);
155 
156 /**
157  * @brief Function for freeing a DPPI channel group.
158  * @details This function also disables the chosen group.
159  *
160  * @note Function is thread safe as it uses @ref nrfx_flag32_free.
161  *
162  * @param[in] p_instance Pointer to the driver instance structure.
163  * @param[in] group      DPPI channel group to be freed.
164  *
165  * @retval NRFX_SUCCESS             The channel group was successfully freed.
166  * @retval NRFX_ERROR_INVALID_PARAM The specified group is not allocated.
167  */
168 nrfx_err_t nrfx_dppi_group_free(nrfx_dppi_t const * p_instance, nrf_dppi_channel_group_t group);
169 
170 /**
171  * @brief Function for including a DPPI channel in a channel group.
172  *
173  * @param[in] p_instance Pointer to the driver instance structure.
174  * @param[in] channel    DPPI channel to be added.
175  * @param[in] group      Channel group in which to include the channel.
176  *
177  * @warning Channel group configuration can be modified only if subscriptions for tasks
178  *          associated with this group are disabled.
179  *
180  * @retval NRFX_SUCCESS             The channel was successfully included.
181  * @retval NRFX_ERROR_INVALID_PARAM The specified group or channel is not allocated.
182  */
183 nrfx_err_t nrfx_dppi_channel_include_in_group(nrfx_dppi_t const *      p_instance,
184                                               uint8_t                  channel,
185                                               nrf_dppi_channel_group_t group);
186 
187 /**
188  * @brief Function for removing a DPPI channel from a channel group.
189  *
190  * @param[in] p_instance Pointer to the driver instance structure.
191  * @param[in] channel    DPPI channel to be removed.
192  * @param[in] group      Channel group from which to remove the channel.
193  *
194  * @warning Channel group configuration can be modified only if subscriptions for tasks
195  *          associated with this group are disabled.
196  *
197  * @retval NRFX_SUCCESS             The channel was successfully removed.
198  * @retval NRFX_ERROR_INVALID_PARAM The specified group or channel is not allocated.
199  */
200 nrfx_err_t nrfx_dppi_channel_remove_from_group(nrfx_dppi_t const *      p_instance,
201                                                uint8_t                  channel,
202                                                nrf_dppi_channel_group_t group);
203 
204 /**
205  * @brief Function for clearing a DPPI channel group.
206  *
207  * @param[in] p_instance Pointer to the driver instance structure.
208  * @param[in] group      Channel group to be cleared.
209  *
210  * @warning Channel group configuration can be modified only if subscriptions for tasks
211  *          associated with this group are disabled.
212  *
213  * @retval NRFX_SUCCESS             The group was successfully cleared.
214  * @retval NRFX_ERROR_INVALID_PARAM The specified group is not allocated.
215  */
216 nrfx_err_t nrfx_dppi_group_clear(nrfx_dppi_t const * p_instance, nrf_dppi_channel_group_t group);
217 
218 /**
219  * @brief Function for enabling a DPPI channel group.
220  *
221  * @param[in] p_instance Pointer to the driver instance structure.
222  * @param[in] group      Channel group to be enabled.
223  *
224  * @retval NRFX_SUCCESS             The group was successfully enabled.
225  * @retval NRFX_ERROR_INVALID_PARAM The specified group is not allocated.
226  */
227 nrfx_err_t nrfx_dppi_group_enable(nrfx_dppi_t const *      p_instance,
228                                   nrf_dppi_channel_group_t group);
229 
230 /**
231  * @brief Function for disabling a DPPI channel group.
232  *
233  * @param[in] p_instance Pointer to the driver instance structure.
234  * @param[in] group      Channel group to be disabled.
235  *
236  * @retval NRFX_SUCCESS             The group was successfully disabled.
237  * @retval NRFX_ERROR_INVALID_PARAM The specified group is not allocated.
238  */
239 nrfx_err_t nrfx_dppi_group_disable(nrfx_dppi_t const *      p_instance,
240                                    nrf_dppi_channel_group_t group);
241 
242 #else
243 
244 #if !defined(NRF_DPPIC_INDEX)
245 /* Choose the instance to use in case of using deprecated single-instance driver variant. */
246 #if defined(HALTIUM_XXAA)
247 #define NRF_DPPIC_INDEX 130
248 #elif defined(LUMOS_XXAA)
249 #define NRF_DPPIC_INDEX 20
250 #else
251 #define NRF_DPPIC_INDEX 0
252 #endif
253 #endif
254 
255 void nrfx_dppi_free(void);
256 
257 nrfx_err_t nrfx_dppi_channel_alloc(uint8_t * p_channel);
258 
259 nrfx_err_t nrfx_dppi_channel_free(uint8_t channel);
260 
261 nrfx_err_t nrfx_dppi_channel_enable(uint8_t channel);
262 
263 nrfx_err_t nrfx_dppi_channel_disable(uint8_t channel);
264 
265 nrfx_err_t nrfx_dppi_group_alloc(nrf_dppi_channel_group_t * p_group);
266 
267 nrfx_err_t nrfx_dppi_group_free(nrf_dppi_channel_group_t group);
268 
269 nrfx_err_t nrfx_dppi_channel_include_in_group(uint8_t                  channel,
270                                               nrf_dppi_channel_group_t group);
271 
272 nrfx_err_t nrfx_dppi_channel_remove_from_group(uint8_t                  channel,
273                                                nrf_dppi_channel_group_t group);
274 
275 nrfx_err_t nrfx_dppi_group_clear(nrf_dppi_channel_group_t group);
276 
277 nrfx_err_t nrfx_dppi_group_enable(nrf_dppi_channel_group_t group);
278 
279 nrfx_err_t nrfx_dppi_group_disable(nrf_dppi_channel_group_t group);
280 
281 #endif
282 
283 /** @} */
284 
285 #ifdef __cplusplus
286 }
287 #endif
288 
289 #endif // NRFX_DPPI_H__
290