1 /*
2  * Copyright (c) 2019 - 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_EGU_H__
35 #define NRFX_EGU_H__
36 
37 #include <nrfx.h>
38 #include <hal/nrf_egu.h>
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44 /**
45  * @defgroup nrfx_egu EGU driver
46  * @{
47  * @ingroup  nrf_egu
48  *
49  * @brief    Event Generator Unit (EGU) peripheral driver.
50  */
51 
52 /** @brief Structure for the EGU driver instance. */
53 typedef struct
54 {
55     NRF_EGU_Type * p_reg;        ///< Pointer to a structure with EGU registers.
56     uint8_t        drv_inst_idx; ///< Index of the driver instance. For internal use only.
57 } nrfx_egu_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(EGU)
63     NRFX_EGU_ENABLED_COUNT
64 };
65 #endif
66 
67 /** @brief Macro for creating an EGU driver instance. */
68 #define NRFX_EGU_INSTANCE(id)                                 \
69 {                                                             \
70     .p_reg        = NRFX_CONCAT(NRF_, EGU, id),               \
71     .drv_inst_idx = NRFX_CONCAT(NRFX_EGU, id, _INST_IDX),     \
72 }
73 
74 /**
75  * @brief EGU driver event handler.
76  *
77  * @param[in] event_idx Index of the event that generated the interrupt.
78  * @param[in] p_context Context passed to the event handler. Set on initialization.
79  */
80 typedef void (*nrfx_egu_event_handler_t)(uint8_t event_idx, void * p_context);
81 
82 /**
83  * @brief Function for initializing the EGU driver instance.
84  *
85  * @param[in] p_instance         Pointer to the driver instance structure.
86  * @param[in] interrupt_priority Interrupt priority.
87  * @param[in] event_handler      Event handler provided by the user. In case of providing NULL,
88  *                               event notifications are not done and EGU interrupts are disabled.
89  * @param[in] p_context          Context passed to the event handler.
90  *
91  * @retval NRFX_SUCCESS             Initialization was successful.
92  * @retval NRFX_ERROR_ALREADY       The driver is already initialized.
93  * @retval NRFX_ERROR_INVALID_STATE The driver is already initialized.
94  *                                  Deprecated - use @ref NRFX_ERROR_ALREADY instead.
95  */
96 nrfx_err_t nrfx_egu_init(nrfx_egu_t const *       p_instance,
97                          uint8_t                  interrupt_priority,
98                          nrfx_egu_event_handler_t event_handler,
99                          void *                   p_context);
100 
101 /**
102  * @brief Function for enabling interrupts on specified events of a given EGU driver instance.
103  *
104  * @param[in] p_instance Pointer to the driver instance structure.
105  * @param[in] mask       Mask of events with interrupts to be enabled.
106  */
107 void nrfx_egu_int_enable(nrfx_egu_t const * p_instance, uint32_t mask);
108 
109 /**
110  * @brief Function for getting the address of the specified EGU task.
111  *
112  * @param[in] p_instance Pointer to the driver instance structure.
113  * @param[in] task       EGU task.
114  *
115  * @return Task address.
116  */
117 NRFX_STATIC_INLINE uint32_t nrfx_egu_task_address_get(nrfx_egu_t const * p_instance,
118                                                       nrf_egu_task_t     task);
119 
120 /**
121  * @brief Function for getting the address of the specified EGU event.
122  *
123  * @param[in] p_instance Pointer to the driver instance structure.
124  * @param[in] event      EGU event.
125  *
126  * @return Event address.
127  */
128 NRFX_STATIC_INLINE uint32_t nrfx_egu_event_address_get(nrfx_egu_t const * p_instance,
129                                                        nrf_egu_event_t    event);
130 
131 /**
132  * @brief Function for disabling interrupts on specified events of a given EGU driver instance.
133  *
134  * @param[in] p_instance Pointer to the driver instance structure.
135  * @param[in] mask       Mask of events with interrupts to be disabled.
136  */
137 void nrfx_egu_int_disable(nrfx_egu_t const * p_instance, uint32_t mask);
138 
139 /**
140  * @brief Function for triggering an event specified by @c event_idx of a given EGU driver instance.
141  *
142  * @param[in] p_instance Pointer to the driver instance structure.
143  * @param[in] event_idx  Index of the event to be triggered.
144  */
145 void nrfx_egu_trigger(nrfx_egu_t const * p_instance, uint8_t event_idx);
146 
147 /**
148  * @brief Function for uninitializing the EGU driver instance.
149  *
150  * @param[in] p_instance Pointer to the driver instance structure.
151  */
152 void nrfx_egu_uninit(nrfx_egu_t const * p_instance);
153 
154 /**
155  * @brief Function for checking if the EGU driver instance is initialized.
156  *
157  * @param[in] p_instance Pointer to the driver instance structure.
158  *
159  * @retval true  Instance is already initialized.
160  * @retval false Instance is not initialized.
161  */
162 bool nrfx_egu_init_check(nrfx_egu_t const * p_instance);
163 
164 /**
165  * @brief Macro returning EGU interrupt handler.
166  *
167  * param[in] idx EGU index.
168  *
169  * @return Interrupt handler.
170  */
171 #define NRFX_EGU_INST_HANDLER_GET(idx) NRFX_CONCAT_3(nrfx_egu_, idx, _irq_handler)
172 
173 #ifndef NRFX_DECLARE_ONLY
nrfx_egu_task_address_get(nrfx_egu_t const * p_instance,nrf_egu_task_t task)174 NRFX_STATIC_INLINE uint32_t nrfx_egu_task_address_get(nrfx_egu_t const * p_instance,
175                                                       nrf_egu_task_t     task)
176 {
177     return nrf_egu_task_address_get(p_instance->p_reg, task);
178 }
179 
nrfx_egu_event_address_get(nrfx_egu_t const * p_instance,nrf_egu_event_t event)180 NRFX_STATIC_INLINE uint32_t nrfx_egu_event_address_get(nrfx_egu_t const * p_instance,
181                                                        nrf_egu_event_t    event)
182 {
183     return nrf_egu_event_address_get(p_instance->p_reg, event);
184 }
185 #endif // NRFX_DECLARE_ONLY
186 
187 /** @} */
188 
189 /*
190  * Declare interrupt handlers for all enabled driver instances in the following format:
191  * nrfx_\<periph_name\>_\<idx\>_irq_handler (for example, nrfx_egu_0_irq_handler).
192  *
193  * A specific interrupt handler for the driver instance can be retrieved by using
194  * the NRFX_EGU_INST_HANDLER_GET macro.
195  *
196  * Here is a sample of using the NRFX_EGU_INST_HANDLER_GET macro to map an interrupt handler
197  * in a Zephyr application:
198  *
199  * IRQ_CONNECT(NRFX_IRQ_NUMBER_GET(NRF_EGU_INST_GET(\<instance_index\>)), \<priority\>,
200  *             NRFX_EGU_INST_HANDLER_GET(\<instance_index\>), 0, 0);
201  */
202 NRFX_INSTANCE_IRQ_HANDLERS_DECLARE(EGU, egu)
203 
204 #ifdef __cplusplus
205 }
206 #endif
207 
208 #endif // NRFX_EGU_H__
209