1 /*
2  * Copyright (c) 2024 - 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_PPIB_H__
35 #define NRFX_PPIB_H__
36 
37 #include <nrfx.h>
38 #include <hal/nrf_ppib.h>
39 
40 /**
41  * @defgroup nrfx_ppib PPIB allocator
42  * @{
43  * @ingroup nrf_ppib
44  * @brief   Programmable Peripheral Interconnect Bridge (PPIB) allocator.
45  */
46 
47 /** @brief Data structure of the Programmable Peripheral Interconnect Bridge (PPIB) driver instance. */
48 typedef struct
49 {
50     NRF_PPIB_Type * p_reg; ///< Pointer to a structure containing PPIBC registers.
51 } nrfx_ppib_t;
52 
53 /** @brief Data structure of the pair of PPIB driver instances. */
54 typedef struct
55 {
56     nrfx_ppib_t left;         ///< First driver instance.
57     nrfx_ppib_t right;        ///< Second driver instance.
58     uint8_t     drv_inst_idx; ///< Index of the driver instance. For internal use only.
59 } nrfx_ppib_interconnect_t;
60 
61 #ifndef __NRFX_DOXYGEN__
62 enum {
63     /* List all enabled driver instances (in the format NRFX_\<instance_name\>_INST_IDX). */
64     NRFX_INSTANCE_ENUM_LIST(PPIB)
65     NRFX_PPIB_ENABLED_COUNT
66 };
67 
68 enum {
69     /* List all enabled interconnects. Smaller PPIB idx are always on the left. */
70 #if defined(NRF54L_SERIES) || defined(NRF7120_ENGA_XXAA)
71 #if NRFX_CHECK(NRFX_PPIB00_ENABLED) && NRFX_CHECK(NRFX_PPIB10_ENABLED)
72     NRFX_PPIB_INTERCONNECT_00_10_INST_IDX,
73 #endif
74 #if NRFX_CHECK(NRFX_PPIB01_ENABLED) && NRFX_CHECK(NRFX_PPIB20_ENABLED)
75     NRFX_PPIB_INTERCONNECT_01_20_INST_IDX,
76 #endif
77 #if NRFX_CHECK(NRFX_PPIB11_ENABLED) && NRFX_CHECK(NRFX_PPIB21_ENABLED)
78     NRFX_PPIB_INTERCONNECT_11_21_INST_IDX,
79 #endif
80 #if NRFX_CHECK(NRFX_PPIB22_ENABLED) && NRFX_CHECK(NRFX_PPIB30_ENABLED)
81     NRFX_PPIB_INTERCONNECT_22_30_INST_IDX,
82 #endif
83 #endif
84 #if defined(NRF54L20_ENGA_XXAA)
85 #if NRFX_CHECK(NRFX_PPIB02_ENABLED) && NRFX_CHECK(NRFX_PPIB03_ENABLED)
86     NRFX_PPIB_INTERCONNECT_02_03_INST_IDX,
87 #endif
88 #if NRFX_CHECK(NRFX_PPIB04_ENABLED) && NRFX_CHECK(NRFX_PPIB12_ENABLED)
89     NRFX_PPIB_INTERCONNECT_04_12_INST_IDX,
90 #endif
91 #endif
92 #if defined(HALTIUM_XXAA)
93 #if NRFX_CHECK(NRFX_PPIB020_ENABLED) && NRFX_CHECK(NRFX_PPIB030_ENABLED)
94     NRFX_PPIB_INTERCONNECT_020_030_INST_IDX,
95 #endif
96 #endif
97     NRFX_PPIB_INTERCONNECT_COUNT
98 };
99 #endif
100 
101 /** @brief Macro for creating an instance of the PPIB driver. */
102 #define NRFX_PPIB_INSTANCE(id)            \
103 {                                         \
104     .p_reg = NRFX_CONCAT(NRF_, PPIB, id), \
105 }
106 
107 /** @brief Macro for creating an instance of the PPIB interconnect driver. */
108 #define NRFX_PPIB_INTERCONNECT_INSTANCE(id_left, id_right)                                 \
109 {                                                                                          \
110     .left         = NRFX_PPIB_INSTANCE(id_left),                                           \
111     .right        = NRFX_PPIB_INSTANCE(id_right),                                          \
112     .drv_inst_idx = NRFX_CONCAT(NRFX_PPIB_INTERCONNECT_, id_left, _, id_right, _INST_IDX), \
113 }
114 
115 #ifdef __cplusplus
116 extern "C" {
117 #endif
118 
119 /**
120  * @brief Function for freeing all allocated channels for a given PPIB interconnection.
121  *
122  * @param[in] p_instance Pointer to the driver instance structure.
123  */
124 void nrfx_ppib_free(nrfx_ppib_interconnect_t const * p_instance);
125 
126 /**
127  * @brief Function for allocating a PPIB channel.
128  * @details This function allocates the highest available PPIB channel.
129  *
130  * @note Function is thread safe as it uses @ref nrfx_flag32_alloc.
131  *
132  * @param[in]  p_instance Pointer to the driver instance structure.
133  * @param[out] p_channel  Pointer to the PPIB channel number that has been allocated.
134  *
135  * @retval NRFX_SUCCESS      The channel was successfully allocated.
136  * @retval NRFX_ERROR_NO_MEM There is no available channel to be used.
137  */
138 nrfx_err_t nrfx_ppib_channel_alloc(nrfx_ppib_interconnect_t const * p_instance, uint8_t * p_channel);
139 
140 /**
141  * @brief Function for freeing a PPIB channel.
142  * @details This function also clears the PUBLISH/SUBSCRIBE configuration.
143  *
144  * @note Function is thread safe as it uses @ref nrfx_flag32_free.
145  *
146  * @param[in] p_instance Pointer to the driver instance structure.
147  * @param[in] channel    PPIB channel to be freed.
148  *
149  * @retval NRFX_SUCCESS             The channel was successfully freed.
150  * @retval NRFX_ERROR_INVALID_PARAM The specified channel is not allocated.
151  */
152 nrfx_err_t nrfx_ppib_channel_free(nrfx_ppib_interconnect_t const * p_instance, uint8_t channel);
153 
154 /**
155  * @brief Function for getting the PPIB SEND task for the specified channel.
156  *
157  * @details The returned task identifier can be used within @ref nrf_ppib_hal,
158  *          for example, to configure a DPPI channel.
159  *
160  * @param[in] p_instance Pointer to the driver instance structure.
161  * @param[in] channel    PPIB channel.
162  *
163  * @return SEND task associated with the specified channel.
164  */
165 NRFX_STATIC_INLINE nrf_ppib_task_t nrfx_ppib_send_task_get(nrfx_ppib_t const * p_instance,
166                                                            uint8_t             channel);
167 
168 /**
169  * @brief Function for getting the address of the PPIB SEND task for the specified channel.
170  *
171  * @param[in] p_instance Pointer to the driver instance structure.
172  * @param[in] channel    PPIB channel.
173  *
174  * @return Address of the specified SEND task.
175  */
176 NRFX_STATIC_INLINE uint32_t nrfx_ppib_send_task_address_get(nrfx_ppib_t const * p_instance,
177                                                             uint8_t             channel);
178 
179 /**
180  * @brief Function for getting the PPIB RECEIVE event for the specified channel.
181  *
182  * @details The returned event identifier can be used within @ref nrf_ppib_hal,
183  *          for example, to configure a DPPI channel.
184  *
185  * @param[in] p_instance Pointer to the driver instance structure.
186  * @param[in] channel    PPIB channel.
187  *
188  * @return RECEIVE event associated with the specified channel.
189  */
190 NRFX_STATIC_INLINE nrf_ppib_event_t nrfx_ppib_receive_event_get(nrfx_ppib_t const * p_instance,
191                                                                 uint8_t             channel);
192 
193 /**
194  * @brief Function for getting the address of a PPIB RECEIVE event.
195  *
196  * @param[in] p_instance Pointer to the driver instance structure.
197  * @param[in] channel    PPIB channel.
198  *
199  * @return Address of the specified RECEIVE event.
200  */
201 NRFX_STATIC_INLINE uint32_t nrfx_ppib_receive_event_address_get(nrfx_ppib_t const * p_instance,
202                                                                 uint8_t             channel);
203 
204 /**
205  * @brief Function for setting the subscribe configuration for a given
206  *        PPIB task.
207  *
208  * @param[in] p_instance Pointer to the driver instance structure.
209  * @param[in] task       Task for which to set the configuration.
210  * @param[in] channel    Channel through which to subscribe events.
211  */
212 NRFX_STATIC_INLINE void nrfx_ppib_subscribe_set(nrfx_ppib_t const * p_instance,
213                                                 nrf_ppib_task_t     task,
214                                                 uint8_t             channel);
215 
216 /**
217  * @brief Function for clearing the subscribe configuration for a given
218  *        PPIB task.
219  *
220  * @param[in] p_instance Pointer to the driver instance structure.
221  * @param[in] task       Task for which to clear the configuration.
222  */
223 NRFX_STATIC_INLINE void nrfx_ppib_subscribe_clear(nrfx_ppib_t const * p_instance,
224                                                   nrf_ppib_task_t     task);
225 
226 /**
227  * @brief Function for setting the publish configuration for a given event.
228  *
229  * @param[in] p_instance Pointer to the driver instance structure.
230  * @param[in] event      Event for which to set the configuration.
231  * @param[in] channel    PPIB channel through which to publish the event.
232  */
233 NRFX_STATIC_INLINE void nrfx_ppib_publish_set(nrfx_ppib_t const * p_instance,
234                                               nrf_ppib_event_t    event,
235                                               uint8_t             channel);
236 /**
237  * @brief Function for clearing the publish configuration for a given event.
238  *
239  * @param[in] p_instance Pointer to the driver instance structure.
240  * @param[in] event      Event for which to clear the configuration.
241  */
242 NRFX_STATIC_INLINE void nrfx_ppib_publish_clear(nrfx_ppib_t const * p_instance,
243                                                 nrf_ppib_event_t    event);
244 #ifndef NRFX_DECLARE_ONLY
245 
nrfx_ppib_send_task_get(nrfx_ppib_t const * p_instance,uint8_t channel)246 NRFX_STATIC_INLINE nrf_ppib_task_t nrfx_ppib_send_task_get(nrfx_ppib_t const * p_instance,
247                                                            uint8_t             channel)
248 {
249     (void) p_instance;
250 
251     return nrf_ppib_send_task_get(channel);
252 }
253 
nrfx_ppib_send_task_address_get(nrfx_ppib_t const * p_instance,uint8_t channel)254 NRFX_STATIC_INLINE uint32_t nrfx_ppib_send_task_address_get(nrfx_ppib_t const * p_instance,
255                                                             uint8_t             channel)
256 {
257     return nrf_ppib_task_address_get(p_instance->p_reg, nrf_ppib_send_task_get(channel));
258 }
259 
nrfx_ppib_receive_event_get(nrfx_ppib_t const * p_instance,uint8_t channel)260 NRFX_STATIC_INLINE nrf_ppib_event_t nrfx_ppib_receive_event_get(nrfx_ppib_t const * p_instance,
261                                                                 uint8_t             channel)
262 {
263     (void) p_instance;
264 
265     return nrf_ppib_receive_event_get(channel);
266 }
267 
nrfx_ppib_receive_event_address_get(nrfx_ppib_t const * p_instance,uint8_t channel)268 NRFX_STATIC_INLINE uint32_t nrfx_ppib_receive_event_address_get(nrfx_ppib_t const * p_instance,
269                                                                 uint8_t             channel)
270 {
271     return nrf_ppib_event_address_get(p_instance->p_reg, nrf_ppib_receive_event_get(channel));
272 }
273 
nrfx_ppib_subscribe_set(nrfx_ppib_t const * p_instance,nrf_ppib_task_t task,uint8_t channel)274 NRFX_STATIC_INLINE void nrfx_ppib_subscribe_set(nrfx_ppib_t const * p_instance,
275                                                 nrf_ppib_task_t     task,
276                                                 uint8_t             channel)
277 {
278     nrf_ppib_subscribe_set(p_instance->p_reg, task, channel);
279 }
280 
nrfx_ppib_subscribe_clear(nrfx_ppib_t const * p_instance,nrf_ppib_task_t task)281 NRFX_STATIC_INLINE void nrfx_ppib_subscribe_clear(nrfx_ppib_t const * p_instance,
282                                                   nrf_ppib_task_t     task)
283 {
284     nrf_ppib_subscribe_clear(p_instance->p_reg, task);
285 }
286 
nrfx_ppib_publish_set(nrfx_ppib_t const * p_instance,nrf_ppib_event_t event,uint8_t channel)287 NRFX_STATIC_INLINE void nrfx_ppib_publish_set(nrfx_ppib_t const * p_instance,
288                                               nrf_ppib_event_t    event,
289                                               uint8_t             channel)
290 {
291     nrf_ppib_publish_set(p_instance->p_reg, event, channel);
292 }
293 
nrfx_ppib_publish_clear(nrfx_ppib_t const * p_instance,nrf_ppib_event_t event)294 NRFX_STATIC_INLINE void nrfx_ppib_publish_clear(nrfx_ppib_t const * p_instance,
295                                                 nrf_ppib_event_t    event)
296 {
297     nrf_ppib_publish_clear(p_instance->p_reg, event);
298 }
299 
300 #endif
301 
302 /** @} */
303 
304 #ifdef __cplusplus
305 }
306 #endif
307 
308 #endif // NRFX_PPIB_H__
309