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 <helpers/nrfx_gppi.h>
36 #include <nrfx_timer.h>
37 #include <nrfx_gpiote.h>
38
39 #define NRFX_LOG_MODULE EXAMPLE
40 #define NRFX_EXAMPLE_CONFIG_LOG_ENABLED 1
41 #define NRFX_EXAMPLE_CONFIG_LOG_LEVEL 3
42 #include <nrfx_log.h>
43
44 /**
45 * @defgroup nrfx_gppi_one_to_one_example One-to-one GPPI example
46 * @{
47 * @ingroup nrfx_gppi_examples
48 *
49 * @brief Example showing basic functionality of a nrfx_gppi helper.
50 *
51 * @details Application initializes nrfx_gpiote, nrfx_timer drivers and nrfx_gppi helper in a way that
52 * TIMER compare event is set up to be forwarded via PPI/DPPI to GPIOTE and toggle a pin.
53 */
54
55 /** @brief Symbol specifying timer instance to be used. */
56 #define TIMER_INST_IDX 0
57
58 /** @brief Symbol specifying time in milliseconds to wait for handler execution. */
59 #define TIME_TO_WAIT_MS 1000UL
60
61 /** @brief Symbol specifying GPIOTE instance to be used. */
62 #define GPIOTE_INST_IDX 0
63
64 /** @brief Symbol specifying ouput pin associated with the task. */
65 #define OUTPUT_PIN LED1_PIN
66
67 /**
68 * @brief Function for handling TIMER driver events.
69 *
70 * @param[in] event_type Timer event.
71 * @param[in] p_context General purpose parameter set during initialization of the timer.
72 * This parameter can be used to pass additional information to the handler
73 * function for example the timer ID.
74 */
timer_handler(nrf_timer_event_t event_type,void * p_context)75 static void timer_handler(nrf_timer_event_t event_type, void * p_context)
76 {
77 if (event_type == NRF_TIMER_EVENT_COMPARE0)
78 {
79 char * p_msg = p_context;
80 NRFX_LOG_INFO("Timer finished. Context passed to the handler: >%s<", p_msg);
81 NRFX_LOG_INFO("GPIOTE output pin: %d is %s", OUTPUT_PIN,
82 nrfx_gpiote_in_is_set(OUTPUT_PIN) ? "high" : "low");
83 }
84 }
85
86 /**
87 * @brief Function for application main entry.
88 *
89 * @return Nothing.
90 */
main(void)91 int main(void)
92 {
93 nrfx_err_t status;
94 (void)status;
95
96 uint8_t out_channel;
97 uint8_t gppi_channel;
98
99 #if defined(__ZEPHYR__)
100 IRQ_CONNECT(NRFX_IRQ_NUMBER_GET(NRF_TIMER_INST_GET(TIMER_INST_IDX)), IRQ_PRIO_LOWEST,
101 NRFX_TIMER_INST_HANDLER_GET(TIMER_INST_IDX), 0, 0);
102 IRQ_CONNECT(NRFX_IRQ_NUMBER_GET(NRF_GPIOTE_INST_GET(GPIOTE_INST_IDX)), IRQ_PRIO_LOWEST,
103 NRFX_GPIOTE_INST_HANDLER_GET(GPIOTE_INST_IDX), 0, 0);
104 #endif
105
106 NRFX_EXAMPLE_LOG_INIT();
107
108 NRFX_LOG_INFO("Starting nrfx_gppi basic one-to-one example.");
109 NRFX_EXAMPLE_LOG_PROCESS();
110
111 nrfx_gpiote_t const gpiote_inst = NRFX_GPIOTE_INSTANCE(GPIOTE_INST_IDX);
112 status = nrfx_gpiote_init(&gpiote_inst, NRFX_GPIOTE_DEFAULT_CONFIG_IRQ_PRIORITY);
113 NRFX_ASSERT(status == NRFX_SUCCESS);
114 NRFX_LOG_INFO("GPIOTE status: %s",
115 nrfx_gpiote_init_check(&gpiote_inst) ? "initialized" : "not initialized");
116
117 status = nrfx_gpiote_channel_alloc(&gpiote_inst, &out_channel);
118 NRFX_ASSERT(status == NRFX_SUCCESS);
119
120 /*
121 * Initialize output pin. The SET task will turn the LED on,
122 * CLR will turn it off and OUT will toggle it.
123 */
124 static const nrfx_gpiote_output_config_t output_config =
125 {
126 .drive = NRF_GPIO_PIN_S0S1,
127 .input_connect = NRF_GPIO_PIN_INPUT_DISCONNECT,
128 .pull = NRF_GPIO_PIN_NOPULL,
129 };
130
131 const nrfx_gpiote_task_config_t task_config =
132 {
133 .task_ch = out_channel,
134 .polarity = NRF_GPIOTE_POLARITY_TOGGLE,
135 .init_val = NRF_GPIOTE_INITIAL_VALUE_HIGH,
136 };
137
138 status = nrfx_gpiote_output_configure(&gpiote_inst, OUTPUT_PIN, &output_config, &task_config);
139 NRFX_ASSERT(status == NRFX_SUCCESS);
140
141 nrfx_gpiote_out_task_enable(&gpiote_inst, OUTPUT_PIN);
142
143 nrfx_timer_t timer_inst = NRFX_TIMER_INSTANCE(TIMER_INST_IDX);
144 uint32_t base_frequency = NRF_TIMER_BASE_FREQUENCY_GET(timer_inst.p_reg);
145 nrfx_timer_config_t timer_config = NRFX_TIMER_DEFAULT_CONFIG(base_frequency);
146 timer_config.bit_width = NRF_TIMER_BIT_WIDTH_32;
147 timer_config.p_context = "Some context";
148
149 status = nrfx_timer_init(&timer_inst, &timer_config, timer_handler);
150 NRFX_ASSERT(status == NRFX_SUCCESS);
151
152 nrfx_timer_clear(&timer_inst);
153
154 /* Creating variable desired_ticks to store the output of nrfx_timer_ms_to_ticks function. */
155 uint32_t desired_ticks = nrfx_timer_ms_to_ticks(&timer_inst, TIME_TO_WAIT_MS);
156 NRFX_LOG_INFO("Time to wait: %lu ms", TIME_TO_WAIT_MS);
157
158 /*
159 * Setting the timer channel NRF_TIMER_CC_CHANNEL0 in the extended compare mode to clear
160 * the timer and to trigger an interrupt if the internal counter register is equal to
161 * desired_ticks.
162 */
163 nrfx_timer_extended_compare(&timer_inst, NRF_TIMER_CC_CHANNEL0, desired_ticks,
164 NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, true);
165
166 status = nrfx_gppi_channel_alloc(&gppi_channel);
167 NRFX_ASSERT(status == NRFX_SUCCESS);
168
169 /*
170 * Configure endpoints of the channel so that the input timer event is connected with the output
171 * pin OUT task. This means that each time the timer interrupt occurs, the LED pin will be toggled.
172 */
173 nrfx_gppi_channel_endpoints_setup(gppi_channel,
174 nrfx_timer_compare_event_address_get(&timer_inst, NRF_TIMER_CC_CHANNEL0),
175 nrfx_gpiote_out_task_address_get(&gpiote_inst, OUTPUT_PIN));
176
177 nrfx_gppi_channels_enable(BIT(gppi_channel));
178
179 nrfx_timer_enable(&timer_inst);
180 NRFX_LOG_INFO("Timer status: %s", nrfx_timer_is_enabled(&timer_inst) ? "enabled" : "disabled");
181
182 while (1)
183 {
184 NRFX_EXAMPLE_LOG_PROCESS();
185 }
186 }
187
188 /** @} */
189