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_timer.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_timer_counter_example Counter example
44 * @{
45 * @ingroup nrfx_timer_examples
46 *
47 * @brief Example showing basic functionality of nrfx_timer driver in Timer Mode and Counter Mode.
48 *
49 * @details Application initializes nrfx_timer driver. The @ref timer_handler() is executed
50 * regularly after specified time ( @ref TIME_TO_WAIT_MS ). Moreover, @ref timer_handler()
51 * is executed when the counter value is equal to @ref COUNTER_MAX_VAL.
52 */
53
54 /** @brief Symbol specifying timer instance to be used in timer mode (T). */
55 #define TIMER_T_INST_IDX 0
56
57 /** @brief Symbol specifying timer instance to be used in counter mode (C). */
58 #define TIMER_C_INST_IDX 1
59
60 /** @brief Symbol specifying time in milliseconds to wait for handler execution. */
61 #define TIME_TO_WAIT_MS 1000UL
62
63 /** @brief Symbol specifying maximum value of the timer. */
64 #define TIMER_MAX_VAL 3UL
65
66 /** @brief Symbol specifying maximum value of the counter. */
67 #define COUNTER_MAX_VAL 3UL
68
69 /** @brief Symbol specifying the initial value of the timer. */
70 #define TIMER_START_VAL 1UL
71
72 /** @brief Global variable that is used to increment the value of a counter. */
73 static volatile bool m_counter_inc_flag = false;
74
75 /**
76 * @brief Function for handling TIMER driver events.
77 *
78 * @param[in] event_type Timer event.
79 * @param[in] p_context General purpose parameter set during initialization of
80 * the timer. This parameter can be used to pass
81 * additional information to the handler function, for
82 * example, the timer ID. In this example p_context is used to
83 * pass address of Timer instance that calls this handler.
84 */
timer_handler(nrf_timer_event_t event_type,void * p_context)85 static void timer_handler(nrf_timer_event_t event_type, void * p_context)
86 {
87 nrfx_timer_t * timer_inst = p_context;
88 static uint32_t timer_val = TIMER_START_VAL;
89
90 /* Creating timer driver instances to ensure that timer_X_inst.instance_id field matches
91 * timer_inst->instance_id. For example if user enables only timer2 with timer1 and timer0
92 * disabled, then timer2->instance_id == 0.
93 */
94 nrfx_timer_t timer_t_inst = NRFX_TIMER_INSTANCE(TIMER_T_INST_IDX);
95 nrfx_timer_t timer_c_inst = NRFX_TIMER_INSTANCE(TIMER_C_INST_IDX);
96
97 if (timer_inst->instance_id == timer_t_inst.instance_id)
98 {
99 /* Configuring timer to count form TIMER_START_VAL to TIMER_MAX_VAL */
100 NRFX_LOG_INFO("Timer: %u", timer_val);
101 (timer_val == TIMER_MAX_VAL) ? timer_val = TIMER_START_VAL : timer_val++;
102
103 if (timer_val == TIMER_START_VAL)
104 {
105 m_counter_inc_flag = true;
106 }
107 }
108 else if (timer_inst->instance_id == timer_c_inst.instance_id)
109 {
110 NRFX_LOG_INFO("Counter finished");
111 }
112 }
113
114 /**
115 * @brief Function for application main entry.
116 *
117 * @return Nothing.
118 */
main(void)119 int main(void)
120 {
121 nrfx_err_t status;
122 (void)status;
123
124 #if defined(__ZEPHYR__)
125 IRQ_CONNECT(NRFX_IRQ_NUMBER_GET(NRF_TIMER_INST_GET(TIMER_T_INST_IDX)), IRQ_PRIO_LOWEST,
126 NRFX_TIMER_INST_HANDLER_GET(TIMER_T_INST_IDX), 0, 0);
127 IRQ_CONNECT(NRFX_IRQ_NUMBER_GET(NRF_TIMER_INST_GET(TIMER_C_INST_IDX)), IRQ_PRIO_LOWEST,
128 NRFX_TIMER_INST_HANDLER_GET(TIMER_C_INST_IDX), 0, 0);
129 #endif
130
131 NRFX_EXAMPLE_LOG_INIT();
132
133 NRFX_LOG_INFO("Starting nrfx_timer basic counter example.");
134 NRFX_EXAMPLE_LOG_PROCESS();
135
136 /* Configuring timer instances, one in timer mode and one in counter mode.
137 * Assigning their own addresses to p_context variable - in order to reference them
138 * in timer_handler() function. Setting frequency to the base frequency value of the
139 * specified timer instance.
140 */
141 nrfx_timer_t timer_t_inst = NRFX_TIMER_INSTANCE(TIMER_T_INST_IDX);
142 uint32_t base_frequency = NRF_TIMER_BASE_FREQUENCY_GET(timer_t_inst.p_reg);
143 nrfx_timer_config_t config = NRFX_TIMER_DEFAULT_CONFIG(base_frequency);
144 config.bit_width = NRF_TIMER_BIT_WIDTH_32;
145 config.p_context = &timer_t_inst;
146 status = nrfx_timer_init(&timer_t_inst, &config, timer_handler);
147 NRFX_ASSERT(status == NRFX_SUCCESS);
148
149 nrfx_timer_t timer_c_inst = NRFX_TIMER_INSTANCE(TIMER_C_INST_IDX);
150 config.frequency = NRF_TIMER_BASE_FREQUENCY_GET(timer_c_inst.p_reg);
151 config.mode = NRF_TIMER_MODE_COUNTER;
152 config.p_context = &timer_c_inst;
153 status = nrfx_timer_init(&timer_c_inst, &config, timer_handler);
154 NRFX_ASSERT(status == NRFX_SUCCESS);
155
156 NRFX_LOG_INFO("Time between timer ticks: %lu ms", TIME_TO_WAIT_MS);
157
158 /* Creating variable desired_ticks to store the output of nrfx_timer_ms_to_ticks function. */
159 uint32_t desired_ticks = nrfx_timer_ms_to_ticks(&timer_t_inst, TIME_TO_WAIT_MS);
160
161 /* Setting the timer (in timer mode) channel NRF_TIMER_CC_CHANNEL0 in the extended compare
162 * mode to clear the timer and trigger an interrupt if internal counter register is equal
163 * to desired_ticks.
164 */
165 nrfx_timer_extended_compare(&timer_t_inst, NRF_TIMER_CC_CHANNEL0, desired_ticks,
166 NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, true);
167
168 /* Setting the timer (in counter mode) channel NRF_TIMER_CC_CHANNEL0 in the extended compare
169 * mode to stop the timer and trigger an interrupt if internal counter register is equal
170 * to COUNTER_MAX_VAL.
171 */
172 nrfx_timer_extended_compare(&timer_c_inst, NRF_TIMER_CC_CHANNEL0, COUNTER_MAX_VAL,
173 NRF_TIMER_SHORT_COMPARE0_STOP_MASK, true);
174
175 nrfx_timer_enable(&timer_c_inst);
176 nrfx_timer_enable(&timer_t_inst);
177 NRFX_LOG_INFO("Timer status: %s",
178 nrfx_timer_is_enabled(&timer_t_inst) ? "enabled" : "disabled");
179 NRFX_LOG_INFO("Counter status: %s",
180 nrfx_timer_is_enabled(&timer_c_inst) ? "enabled" : "disabled");
181
182 uint32_t timer_c_curr_val = 0;
183
184 while (1)
185 {
186 if (m_counter_inc_flag)
187 {
188 nrfx_timer_increment(&timer_c_inst);
189 timer_c_curr_val = nrfx_timer_capture(&timer_c_inst, NRF_TIMER_CC_CHANNEL1);
190 NRFX_LOG_INFO("Counter: %u / %lu", timer_c_curr_val, COUNTER_MAX_VAL);
191 m_counter_inc_flag = false;
192
193 if (timer_c_curr_val >= COUNTER_MAX_VAL)
194 {
195 nrfx_timer_disable(&timer_t_inst);
196 nrfx_timer_disable(&timer_c_inst);
197 NRFX_LOG_INFO("Timer status: %s",
198 nrfx_timer_is_enabled(&timer_t_inst) ? "enabled" : "disabled");
199 NRFX_LOG_INFO("Counter status: %s",
200 nrfx_timer_is_enabled(&timer_c_inst) ? "enabled" : "disabled");
201 }
202 }
203 NRFX_EXAMPLE_LOG_PROCESS();
204 }
205 }
206
207 /** @} */
208