1 /*
2  * Copyright (c) 2019 - 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_EGU_H__
33 #define NRFX_EGU_H__
34 
35 #include <nrfx.h>
36 #include <hal/nrf_egu.h>
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
42 /**
43  * @defgroup nrfx_egu EGU driver
44  * @{
45  * @ingroup  nrf_egu
46  *
47  * @brief    Event Generator Unit (EGU) peripheral driver.
48  */
49 
50 /** @brief Structure for the EGU driver instance. */
51 typedef struct
52 {
53     NRF_EGU_Type * p_reg;        ///< Pointer to a structure with EGU registers.
54     uint8_t        drv_inst_idx; ///< Index of the driver instance. For internal use only.
55 } nrfx_egu_t;
56 
57 #ifndef __NRFX_DOXYGEN__
58 enum {
59 #if NRFX_CHECK(NRFX_EGU0_ENABLED)
60     NRFX_EGU0_INST_IDX,
61 #endif
62 #if NRFX_CHECK(NRFX_EGU1_ENABLED)
63     NRFX_EGU1_INST_IDX,
64 #endif
65 #if NRFX_CHECK(NRFX_EGU2_ENABLED)
66     NRFX_EGU2_INST_IDX,
67 #endif
68 #if NRFX_CHECK(NRFX_EGU3_ENABLED)
69     NRFX_EGU3_INST_IDX,
70 #endif
71 #if NRFX_CHECK(NRFX_EGU4_ENABLED)
72     NRFX_EGU4_INST_IDX,
73 #endif
74 #if NRFX_CHECK(NRFX_EGU5_ENABLED)
75     NRFX_EGU5_INST_IDX,
76 #endif
77     NRFX_EGU_ENABLED_COUNT
78 };
79 #endif
80 
81 /** @brief Macro for creating an EGU driver instance. */
82 #define NRFX_EGU_INSTANCE(id)                                 \
83 {                                                             \
84     .p_reg        = NRFX_CONCAT_2(NRF_EGU, id),               \
85     .drv_inst_idx = NRFX_CONCAT_3(NRFX_EGU, id, _INST_IDX),   \
86 }
87 
88 /**
89  * @brief EGU driver event handler.
90  *
91  * @param[in] event_idx Index of the event that generated the interrupt.
92  * @param[in] p_context Context passed to the event handler. Set on initialization.
93  */
94 typedef void (*nrfx_egu_event_handler_t)(uint8_t event_idx, void * p_context);
95 
96 /**
97  * @brief Function for initializing the EGU driver instance.
98  *
99  * @param[in] p_instance         Pointer to the driver instance structure.
100  * @param[in] interrupt_priority Interrupt priority.
101  * @param[in] event_handler      Event handler provided by the user. In case of providing NULL,
102  *                               event notifications are not done and EGU interrupts are disabled.
103  * @param[in] p_context          Context passed to the event handler.
104  *
105  * @retval NRFX_SUCCESS             Initialization was successful.
106  * @retval NRFX_ERROR_INVALID_STATE Driver is already initialized.
107  */
108 nrfx_err_t nrfx_egu_init(nrfx_egu_t const *       p_instance,
109                          uint8_t                  interrupt_priority,
110                          nrfx_egu_event_handler_t event_handler,
111                          void *                   p_context);
112 
113 /**
114  * @brief Function for enabling interrupts on specified events of a given EGU driver instance.
115  *
116  * @param[in] p_instance Pointer to the driver instance structure.
117  * @param[in] mask       Mask of events with interrupts to be enabled.
118  */
119 void nrfx_egu_int_enable(nrfx_egu_t const * p_instance, uint32_t mask);
120 
121 /**
122  * @brief Function for disabling interrupts on specified events of a given EGU driver instance.
123  *
124  * @param[in] p_instance Pointer to the driver instance structure.
125  * @param[in] mask       Mask of events with interrupts to be disabled.
126  */
127 void nrfx_egu_int_disable(nrfx_egu_t const * p_instance, uint32_t mask);
128 
129 /**
130  * @brief Function for triggering an event specified by @c event_idx of a given EGU driver instance.
131  *
132  * @param[in] p_instance Pointer to the driver instance structure.
133  * @param[in] event_idx  Index of the event to be triggered.
134  */
135 void nrfx_egu_trigger(nrfx_egu_t const * p_instance, uint8_t event_idx);
136 
137 /**
138  * @brief Function for uninitializing the EGU driver instance.
139  *
140  * @param[in] p_instance Pointer to the driver instance structure.
141  */
142 void nrfx_egu_uninit(nrfx_egu_t const * p_instance);
143 
144 /** @} */
145 
146 void nrfx_egu_0_irq_handler(void);
147 void nrfx_egu_1_irq_handler(void);
148 void nrfx_egu_2_irq_handler(void);
149 void nrfx_egu_3_irq_handler(void);
150 void nrfx_egu_4_irq_handler(void);
151 void nrfx_egu_5_irq_handler(void);
152 
153 #ifdef __cplusplus
154 }
155 #endif
156 
157 #endif // NRFX_EGU_H__
158