This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

RTC compare event

I am testing the RTC example in nRF5 SDK v17.1.0.

This example shows LED1 switching at a rate of 8 Hz and LED2 lighting up after 3 seconds.

This is confirmed.

The programme shows that the action after 3 seconds is performed by a compare event interrupt, but only one entry is made to this interrupt.


I would like to repeat this interrupt every three seconds to make LED2 blink, but please tell me exactly how to change the programme.

Parents
  • Hi there,

    You need to clear the counter and reset the compare interrupt like this:

    /**
     * Copyright (c) 2014 - 2021, Nordic Semiconductor ASA
     *
     * All rights reserved.
     *
     * 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, except as embedded into a Nordic
     *    Semiconductor ASA integrated circuit in a product or a software update for
     *    such product, 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 Nordic Semiconductor ASA nor the names of its
     *    contributors may be used to endorse or promote products derived from this
     *    software without specific prior written permission.
     *
     * 4. This software, with or without modification, must only be used with a
     *    Nordic Semiconductor ASA integrated circuit.
     *
     * 5. Any software provided in binary form under this license must not be reverse
     *    engineered, decompiled, modified and/or disassembled.
     *
     * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
     * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
     * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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.
     *
     */
    /** @file
     * @defgroup rtc_example_main main.c
     * @{
     * @ingroup rtc_example
     * @brief Real Time Counter Example Application main file.
     *
     * This file contains the source code for a sample application using the Real Time Counter (RTC).
     *
     */
    
    #include "nrf.h"
    #include "nrf_gpio.h"
    #include "nrf_drv_rtc.h"
    #include "nrf_drv_clock.h"
    #include "boards.h"
    #include "app_error.h"
    #include <stdint.h>
    #include <stdbool.h>
    
    #define COMPARE_COUNTERTIME  (3UL)                                        /**< Get Compare event COMPARE_TIME seconds after the counter starts from 0. */
    
    #ifdef BSP_LED_0
        #define TICK_EVENT_OUTPUT     BSP_LED_0                                 /**< Pin number for indicating tick event. */
    #endif
    #ifndef TICK_EVENT_OUTPUT
        #error "Please indicate output pin"
    #endif
    #ifdef BSP_LED_1
        #define COMPARE_EVENT_OUTPUT   BSP_LED_1                                /**< Pin number for indicating compare event. */
    #endif
    #ifndef COMPARE_EVENT_OUTPUT
        #error "Please indicate output pin"
    #endif
    
    const nrf_drv_rtc_t rtc = NRF_DRV_RTC_INSTANCE(0); /**< Declaring an instance of nrf_drv_rtc for RTC0. */
    
    /** @brief: Function for handling the RTC0 interrupts.
     * Triggered on TICK and COMPARE0 match.
     */
    static void rtc_handler(nrf_drv_rtc_int_type_t int_type)
    {
        if (int_type == NRF_DRV_RTC_INT_COMPARE0)
        {
            nrf_gpio_pin_toggle(COMPARE_EVENT_OUTPUT);
            nrf_drv_rtc_counter_clear(&rtc);
            int err_code = nrf_drv_rtc_cc_set(&rtc,0,COMPARE_COUNTERTIME * 8,true);
            APP_ERROR_CHECK(err_code);
         
        }
        else if (int_type == NRF_DRV_RTC_INT_TICK)
        {
            nrf_gpio_pin_toggle(TICK_EVENT_OUTPUT);
        }
    }
    
    /** @brief Function configuring gpio for pin toggling.
     */
    static void leds_config(void)
    {
        bsp_board_init(BSP_INIT_LEDS);
    }
    
    /** @brief Function starting the internal LFCLK XTAL oscillator.
     */
    static void lfclk_config(void)
    {
        ret_code_t err_code = nrf_drv_clock_init();
        APP_ERROR_CHECK(err_code);
    
        nrf_drv_clock_lfclk_request(NULL);
    }
    
    /** @brief Function initialization and configuration of RTC driver instance.
     */
    static void rtc_config(void)
    {
        uint32_t err_code;
    
        //Initialize RTC instance
        nrf_drv_rtc_config_t config = NRF_DRV_RTC_DEFAULT_CONFIG;
        config.prescaler = 4095;
        err_code = nrf_drv_rtc_init(&rtc, &config, rtc_handler);
        APP_ERROR_CHECK(err_code);
    
        //Enable tick event & interrupt
        nrf_drv_rtc_tick_enable(&rtc,true);
    
        //Set compare channel to trigger interrupt after COMPARE_COUNTERTIME seconds
        err_code = nrf_drv_rtc_cc_set(&rtc,0,COMPARE_COUNTERTIME * 8,true);
        APP_ERROR_CHECK(err_code);
    
        //Power on RTC instance
        nrf_drv_rtc_enable(&rtc);
    }
    
    /**
     * @brief Function for application main entry.
     */
    int main(void)
    {
        leds_config();
    
        lfclk_config();
    
        rtc_config();
    
        while (true)
        {
            __SEV();
            __WFE();
            __WFE();
        }
    }
    
    
    /**  @} */
    

    Alternatively:

    static void rtc_handler(nrf_drv_rtc_int_type_t int_type)
    {
        static int counter = 2;
        if (int_type == NRF_DRV_RTC_INT_COMPARE0)
        {
            nrf_gpio_pin_toggle(COMPARE_EVENT_OUTPUT);
            //Set compare channel to trigger interrupt after COMPARE_COUNTERTIME seconds
            int err_code = nrf_drv_rtc_cc_set(&rtc,0,((COMPARE_COUNTERTIME * 8)*counter) % 0x1000000,true);
            APP_ERROR_CHECK(err_code);
            counter++;
        }
        else if (int_type == NRF_DRV_RTC_INT_TICK)
        {
            nrf_gpio_pin_toggle(TICK_EVENT_OUTPUT);
        }
    }

    regards

    Jared

Reply
  • Hi there,

    You need to clear the counter and reset the compare interrupt like this:

    /**
     * Copyright (c) 2014 - 2021, Nordic Semiconductor ASA
     *
     * All rights reserved.
     *
     * 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, except as embedded into a Nordic
     *    Semiconductor ASA integrated circuit in a product or a software update for
     *    such product, 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 Nordic Semiconductor ASA nor the names of its
     *    contributors may be used to endorse or promote products derived from this
     *    software without specific prior written permission.
     *
     * 4. This software, with or without modification, must only be used with a
     *    Nordic Semiconductor ASA integrated circuit.
     *
     * 5. Any software provided in binary form under this license must not be reverse
     *    engineered, decompiled, modified and/or disassembled.
     *
     * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
     * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
     * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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.
     *
     */
    /** @file
     * @defgroup rtc_example_main main.c
     * @{
     * @ingroup rtc_example
     * @brief Real Time Counter Example Application main file.
     *
     * This file contains the source code for a sample application using the Real Time Counter (RTC).
     *
     */
    
    #include "nrf.h"
    #include "nrf_gpio.h"
    #include "nrf_drv_rtc.h"
    #include "nrf_drv_clock.h"
    #include "boards.h"
    #include "app_error.h"
    #include <stdint.h>
    #include <stdbool.h>
    
    #define COMPARE_COUNTERTIME  (3UL)                                        /**< Get Compare event COMPARE_TIME seconds after the counter starts from 0. */
    
    #ifdef BSP_LED_0
        #define TICK_EVENT_OUTPUT     BSP_LED_0                                 /**< Pin number for indicating tick event. */
    #endif
    #ifndef TICK_EVENT_OUTPUT
        #error "Please indicate output pin"
    #endif
    #ifdef BSP_LED_1
        #define COMPARE_EVENT_OUTPUT   BSP_LED_1                                /**< Pin number for indicating compare event. */
    #endif
    #ifndef COMPARE_EVENT_OUTPUT
        #error "Please indicate output pin"
    #endif
    
    const nrf_drv_rtc_t rtc = NRF_DRV_RTC_INSTANCE(0); /**< Declaring an instance of nrf_drv_rtc for RTC0. */
    
    /** @brief: Function for handling the RTC0 interrupts.
     * Triggered on TICK and COMPARE0 match.
     */
    static void rtc_handler(nrf_drv_rtc_int_type_t int_type)
    {
        if (int_type == NRF_DRV_RTC_INT_COMPARE0)
        {
            nrf_gpio_pin_toggle(COMPARE_EVENT_OUTPUT);
            nrf_drv_rtc_counter_clear(&rtc);
            int err_code = nrf_drv_rtc_cc_set(&rtc,0,COMPARE_COUNTERTIME * 8,true);
            APP_ERROR_CHECK(err_code);
         
        }
        else if (int_type == NRF_DRV_RTC_INT_TICK)
        {
            nrf_gpio_pin_toggle(TICK_EVENT_OUTPUT);
        }
    }
    
    /** @brief Function configuring gpio for pin toggling.
     */
    static void leds_config(void)
    {
        bsp_board_init(BSP_INIT_LEDS);
    }
    
    /** @brief Function starting the internal LFCLK XTAL oscillator.
     */
    static void lfclk_config(void)
    {
        ret_code_t err_code = nrf_drv_clock_init();
        APP_ERROR_CHECK(err_code);
    
        nrf_drv_clock_lfclk_request(NULL);
    }
    
    /** @brief Function initialization and configuration of RTC driver instance.
     */
    static void rtc_config(void)
    {
        uint32_t err_code;
    
        //Initialize RTC instance
        nrf_drv_rtc_config_t config = NRF_DRV_RTC_DEFAULT_CONFIG;
        config.prescaler = 4095;
        err_code = nrf_drv_rtc_init(&rtc, &config, rtc_handler);
        APP_ERROR_CHECK(err_code);
    
        //Enable tick event & interrupt
        nrf_drv_rtc_tick_enable(&rtc,true);
    
        //Set compare channel to trigger interrupt after COMPARE_COUNTERTIME seconds
        err_code = nrf_drv_rtc_cc_set(&rtc,0,COMPARE_COUNTERTIME * 8,true);
        APP_ERROR_CHECK(err_code);
    
        //Power on RTC instance
        nrf_drv_rtc_enable(&rtc);
    }
    
    /**
     * @brief Function for application main entry.
     */
    int main(void)
    {
        leds_config();
    
        lfclk_config();
    
        rtc_config();
    
        while (true)
        {
            __SEV();
            __WFE();
            __WFE();
        }
    }
    
    
    /**  @} */
    

    Alternatively:

    static void rtc_handler(nrf_drv_rtc_int_type_t int_type)
    {
        static int counter = 2;
        if (int_type == NRF_DRV_RTC_INT_COMPARE0)
        {
            nrf_gpio_pin_toggle(COMPARE_EVENT_OUTPUT);
            //Set compare channel to trigger interrupt after COMPARE_COUNTERTIME seconds
            int err_code = nrf_drv_rtc_cc_set(&rtc,0,((COMPARE_COUNTERTIME * 8)*counter) % 0x1000000,true);
            APP_ERROR_CHECK(err_code);
            counter++;
        }
        else if (int_type == NRF_DRV_RTC_INT_TICK)
        {
            nrf_gpio_pin_toggle(TICK_EVENT_OUTPUT);
        }
    }

    regards

    Jared

Children
  • Reply, thank you.

    Regarding the first example.
    Regarding clearing the counter in the interrupt and resetting the compare interrupt, I was trying the same code (two lines of the following).
         nrf_drv_rtc_counter_clear(&rtc);
         int err_code = nrf_drv_rtc_cc_set(&rtc,0,COMPARE_COUNTERTIME * 8,true);
    I just did not have the 'APP_ERROR_CHECK(err_code)' statement.

    The result was the same as when I did it, only one time into the 'if (int_type == NRF_DRV_RTC_INT_COMPARE0)' interrupt.
    Thanks to the 'err code' I could see that it was stopping at 'NRF_BREAKPOINT_COND' in 'app_error_weak.c', but the cause was not clear to me.

    Regarding the second example.
    This method worked.
    I would like to try to apply this method to change the repetition cycle and send packets in that cycle. By the way, when transmitting, I plan to use ESB for the protocol with low consumption.

  • Hi,

    TML_YT said:
    The result was the same as when I did it, only one time into the 'if (int_type == NRF_DRV_RTC_INT_COMPARE0)' interrupt.
    Thanks to the 'err code' I could see that it was stopping at 'NRF_BREAKPOINT_COND' in 'app_error_weak.c', but the cause was not clear to me.

    Strange, I tested this method and it worked for me. Could share the error message and check the return value from nrf_drv_rtc_cc_set()?

    TML_YT said:
    Regarding the second example.
    This method worked.
    I would like to try to apply this method to change the repetition cycle and send packets in that cycle. By the way, when transmitting, I plan to use ESB for the protocol with low consumption.

    Good, then you have a working alternative Slight smile

    regards

    Jared

Related