1 /*
2  * Copyright (c) 2022 - 2024, 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 #include <nrfx_example.h>
35 #include <nrfx_egu.h>
36 
37 #define NRFX_LOG_MODULE                 EXAMPLE
38 #define NRFX_EXAMPLE_CONFIG_LOG_ENABLED 1
39 #define NRFX_EXAMPLE_CONFIG_LOG_LEVEL   3
40 #include <nrfx_log.h>
41 
42 /**
43  * @defgroup nrfx_egu_example Basic EGU example
44  * @{
45  * @ingroup nrfx_egu_examples
46  *
47  * @brief Example showing basic functionality of nrfx_egu driver.
48  *
49  * @details Application initializes nrfx_egu driver instance and triggers
50  *          three subsequent EGU channels. Then @ref egu_handler() is executed once
51  *          for each triggered channel with relevant log message.
52  */
53 
54 /** @brief Symbol specifying EGU instance to use. */
55 #define EGU_INST_IDX 0
56 
57 /**
58  * @brief Function for handling EGU driver events.
59  *
60  * @param[in] event_idx Index of the event that generated the interrupt.
61  * @param[in] p_context Context passed to the event handler. Set on initialization.
62  */
egu_handler(uint8_t event_idx,void * p_context)63 static void egu_handler(uint8_t event_idx, void * p_context)
64 {
65     char * p_msg = p_context;
66     NRFX_LOG_INFO("EGU handler triggered on channel: %u. "
67                   "Context passed to the handler: >%s<",
68                   event_idx, p_msg);
69 }
70 
71 /**
72  * @brief Function for application main entry.
73  *
74  * @return Nothing.
75  */
main(void)76 int main(void)
77 {
78     nrfx_err_t status;
79     (void)status;
80 
81 #if defined(__ZEPHYR__)
82     IRQ_CONNECT(NRFX_IRQ_NUMBER_GET(NRF_EGU_INST_GET(EGU_INST_IDX)), IRQ_PRIO_LOWEST,
83                 NRFX_EGU_INST_HANDLER_GET(EGU_INST_IDX), 0, 0);
84 #endif
85 
86     NRFX_EXAMPLE_LOG_INIT();
87 
88     NRFX_LOG_INFO("Starting nrfx_egu example");
89     NRFX_EXAMPLE_LOG_PROCESS();
90 
91     nrfx_egu_t egu_inst = NRFX_EGU_INSTANCE(EGU_INST_IDX);
92     void * p_context = "Some context";
93     status = nrfx_egu_init(&egu_inst, NRFX_EGU_DEFAULT_CONFIG_IRQ_PRIORITY, egu_handler, p_context);
94     NRFX_ASSERT(status == NRFX_SUCCESS);
95 
96     uint32_t ch0_idx = 0;
97     uint32_t ch1_idx = 1;
98     uint32_t ch2_idx = 2;
99 
100     uint32_t channels_to_trigger = (1 << ch0_idx) | (1 << ch1_idx) | (1 << ch2_idx);
101     nrfx_egu_int_enable(&egu_inst, channels_to_trigger);
102 
103     nrfx_egu_trigger(&egu_inst, ch0_idx);
104     nrfx_egu_trigger(&egu_inst, ch1_idx);
105     nrfx_egu_trigger(&egu_inst, ch2_idx);
106 
107     while (1)
108     {
109         NRFX_EXAMPLE_LOG_PROCESS();
110     }
111 }
112 
113 /** @} */
114