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_example Timer example
44 * @{
45 * @ingroup nrfx_timer_examples
46 *
47 * @brief Example showing basic functionality of nrfx_timer driver in Timer Mode.
48 *
49 * @details Application initializes nrfx_timer driver. The @ref timer_handler() is executed after
50 * the specified time.
51 */
52
53 /** @brief Symbol specifying timer instance to be used. */
54 #define TIMER_INST_IDX 0
55
56 /** @brief Symbol specifying time in milliseconds to wait for handler execution. */
57 #define TIME_TO_WAIT_MS 5000UL
58
59 /**
60 * @brief Function for handling TIMER driver events.
61 *
62 * @param[in] event_type Timer event.
63 * @param[in] p_context General purpose parameter set during initialization of
64 * the timer. This parameter can be used to pass
65 * additional information to the handler function, for
66 * example, the timer ID.
67 */
timer_handler(nrf_timer_event_t event_type,void * p_context)68 static void timer_handler(nrf_timer_event_t event_type, void * p_context)
69 {
70 if(event_type == NRF_TIMER_EVENT_COMPARE0)
71 {
72 char * p_msg = p_context;
73 NRFX_LOG_INFO("Timer finished. Context passed to the handler: >%s<", p_msg);
74 }
75 }
76
77 /**
78 * @brief Function for application main entry.
79 *
80 * @return Nothing.
81 */
main(void)82 int main(void)
83 {
84 nrfx_err_t status;
85 (void)status;
86
87 #if defined(__ZEPHYR__)
88 IRQ_CONNECT(NRFX_IRQ_NUMBER_GET(NRF_TIMER_INST_GET(TIMER_INST_IDX)), IRQ_PRIO_LOWEST,
89 NRFX_TIMER_INST_HANDLER_GET(TIMER_INST_IDX), 0, 0);
90 #endif
91
92 NRFX_EXAMPLE_LOG_INIT();
93
94 NRFX_LOG_INFO("Starting nrfx_timer basic example:");
95 NRFX_EXAMPLE_LOG_PROCESS();
96
97 nrfx_timer_t timer_inst = NRFX_TIMER_INSTANCE(TIMER_INST_IDX);
98 uint32_t base_frequency = NRF_TIMER_BASE_FREQUENCY_GET(timer_inst.p_reg);
99 nrfx_timer_config_t config = NRFX_TIMER_DEFAULT_CONFIG(base_frequency);
100 config.bit_width = NRF_TIMER_BIT_WIDTH_32;
101 config.p_context = "Some context";
102
103 status = nrfx_timer_init(&timer_inst, &config, timer_handler);
104 NRFX_ASSERT(status == NRFX_SUCCESS);
105
106 nrfx_timer_clear(&timer_inst);
107
108 /* Creating variable desired_ticks to store the output of nrfx_timer_ms_to_ticks function */
109 uint32_t desired_ticks = nrfx_timer_ms_to_ticks(&timer_inst, TIME_TO_WAIT_MS);
110 NRFX_LOG_INFO("Time to wait: %lu ms", TIME_TO_WAIT_MS);
111
112 /*
113 * Setting the timer channel NRF_TIMER_CC_CHANNEL0 in the extended compare mode to stop the timer and
114 * trigger an interrupt if internal counter register is equal to desired_ticks.
115 */
116 nrfx_timer_extended_compare(&timer_inst, NRF_TIMER_CC_CHANNEL0, desired_ticks,
117 NRF_TIMER_SHORT_COMPARE0_STOP_MASK, true);
118
119 nrfx_timer_enable(&timer_inst);
120 NRFX_LOG_INFO("Timer status: %s", nrfx_timer_is_enabled(&timer_inst) ? "enabled" : "disabled");
121
122 while (1)
123 {
124 NRFX_EXAMPLE_LOG_PROCESS();
125 }
126 }
127
128 /** @} */
129