/* * Copyright (c) 2022 - 2024, Nordic Semiconductor ASA * All rights reserved. * * SPDX-License-Identifier: BSD-3-Clause * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * 3. Neither the name of the copyright holder nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include #include #define NRFX_LOG_MODULE EXAMPLE #define NRFX_EXAMPLE_CONFIG_LOG_ENABLED 1 #define NRFX_EXAMPLE_CONFIG_LOG_LEVEL 3 #include /** * @defgroup nrfx_timer_example Timer example * @{ * @ingroup nrfx_timer_examples * * @brief Example showing basic functionality of nrfx_timer driver in Timer Mode. * * @details Application initializes nrfx_timer driver. The @ref timer_handler() is executed after * the specified time. */ /** @brief Symbol specifying timer instance to be used. */ #define TIMER_INST_IDX 0 /** @brief Symbol specifying time in milliseconds to wait for handler execution. */ #define TIME_TO_WAIT_MS 5000UL /** * @brief Function for handling TIMER driver events. * * @param[in] event_type Timer event. * @param[in] p_context General purpose parameter set during initialization of * the timer. This parameter can be used to pass * additional information to the handler function, for * example, the timer ID. */ static void timer_handler(nrf_timer_event_t event_type, void * p_context) { if(event_type == NRF_TIMER_EVENT_COMPARE0) { char * p_msg = p_context; NRFX_LOG_INFO("Timer finished. Context passed to the handler: >%s<", p_msg); } } /** * @brief Function for application main entry. * * @return Nothing. */ int main(void) { nrfx_err_t status; (void)status; #if defined(__ZEPHYR__) IRQ_CONNECT(NRFX_IRQ_NUMBER_GET(NRF_TIMER_INST_GET(TIMER_INST_IDX)), IRQ_PRIO_LOWEST, NRFX_TIMER_INST_HANDLER_GET(TIMER_INST_IDX), 0, 0); #endif NRFX_EXAMPLE_LOG_INIT(); NRFX_LOG_INFO("Starting nrfx_timer basic example:"); NRFX_EXAMPLE_LOG_PROCESS(); nrfx_timer_t timer_inst = NRFX_TIMER_INSTANCE(TIMER_INST_IDX); uint32_t base_frequency = NRF_TIMER_BASE_FREQUENCY_GET(timer_inst.p_reg); nrfx_timer_config_t config = NRFX_TIMER_DEFAULT_CONFIG(base_frequency); config.bit_width = NRF_TIMER_BIT_WIDTH_32; config.p_context = "Some context"; status = nrfx_timer_init(&timer_inst, &config, timer_handler); NRFX_ASSERT(status == NRFX_SUCCESS); nrfx_timer_clear(&timer_inst); /* Creating variable desired_ticks to store the output of nrfx_timer_ms_to_ticks function */ uint32_t desired_ticks = nrfx_timer_ms_to_ticks(&timer_inst, TIME_TO_WAIT_MS); NRFX_LOG_INFO("Time to wait: %lu ms", TIME_TO_WAIT_MS); /* * Setting the timer channel NRF_TIMER_CC_CHANNEL0 in the extended compare mode to stop the timer and * trigger an interrupt if internal counter register is equal to desired_ticks. */ nrfx_timer_extended_compare(&timer_inst, NRF_TIMER_CC_CHANNEL0, desired_ticks, NRF_TIMER_SHORT_COMPARE0_STOP_MASK, true); nrfx_timer_enable(&timer_inst); NRFX_LOG_INFO("Timer status: %s", nrfx_timer_is_enabled(&timer_inst) ? "enabled" : "disabled"); while (1) { NRFX_EXAMPLE_LOG_PROCESS(); } } /** @} */