is it possible to trigger RTC compare event for every 10millisecond

Hi team,

I would like to stream(through USB) data every 10millisecond using RTC interrupt.

is this possible or should i use other timers for this purpose ?

Can i configure the rtc compare event to trigger every 10millisecond using this nrf_drv_rtc_cc_set()?

Please suggest the best way for this ?

Thanks

Parents
  • yes you can. but you need to set the prescaler accordingly. you can look into RTC example for that. RTC count is 32 bit. so if you set the prescaler, you can increase or decrease resolution. and then you can calculate the required ticks for 10 ms and use that value in nrf_drv_cc_set function.

  • I have configured the prescaler value as 0 which means i will get tick interrupt every 30.5micro second.

    this is happening properly

    now i want to configure my compare event to occur every 10millisecond but i get compare event only once

    below is my code snippnet

    void rtc_handler(nrfx_rtc_int_type_t int_type)
    {
    
        switch (int_type)
        {
            case NRF_DRV_RTC_INT_COMPARE0:
                nrf_gpio_pin_toggle( NRF_GPIO_PIN_MAP(0, 14));
                nrf_drv_rtc_cc_set(&rtc_handle, 0,328, true);
                nrf_drv_rtc_int_enable(&rtc_handle, RTC_CHANNEL_INT_MASK(0));
                break;
            case NRF_DRV_RTC_INT_COMPARE1:
                case NRF_DRV_RTC_INT_COMPARE2:
                case NRF_DRV_RTC_INT_COMPARE3:
    
                break;
            case NRF_DRV_RTC_INT_OVERFLOW:
                /* RTC overflow event */
                rtc_overflow = (rtc_overflow != 255) ? (rtc_overflow + 1) : 0;
                break;
    
            case NRF_DRV_RTC_INT_TICK:
                /* RTC tick event */
                nrf_gpio_pin_toggle( NRF_GPIO_PIN_MAP(0, 13));
                rtc_count = (rtc_overflow << RTC_COUNTER_BITS) | nrf_drv_rtc_counter_get(&rtc_handle);
                break;
    
            default:
                /* Unknown RTC event. Added for completeness */
                break;
        }
    }
    
    void rtc_config(void)
    {
        uint32_t err_code;
    
        nrfx_rtc_config_t rtc_config_struct = NRFX_RTC_DEFAULT_CONFIG;
    
        /* Configure the prescaler to generate ticks for a specific time unit */
        /* if prescalar = 1, tick resolution =  32768 / (1 + 0) = 32768Hz = 30.5us */
        rtc_config_struct.prescaler = RTC_PRESCALAR;
    
        /* Initialize the RTC and pass the configurations along with the interrupt handler */
        err_code = nrfx_rtc_init(&rtc_handle, &rtc_config_struct, rtc_handler);
        if (err_code == NRFX_SUCCESS)
        {
            /* Generate a tick event on each tick */
            nrfx_rtc_tick_enable(&rtc_handle, true);
    
            err_code = nrf_drv_rtc_cc_set(&rtc_handle, 0,328, true);//30.5us*328=~10ms
            if (err_code == NRFX_SUCCESS)
            {
                nrfx_rtc_overflow_enable(&rtc_handle, true);
    
                /* start the RTC */
                nrfx_rtc_enable(&rtc_handle);
            }
    
        }
        (void)err_code;
    }
    

    what am i doing wrong? please guide me on this 

  • You need to clear the RTC. Call nrf_rtc_task_trigger(&rtc_handle, NRF_RTC_TASK_CLEAR) in your RTC event handler, or connect the COMPARE0 event to the RTC's CLEAR task with PPI. 

Reply Children
  • Even after adding that i m getting compare event only once

    case NRF_DRV_RTC_INT_COMPARE0:
                nrf_gpio_pin_toggle( NRF_GPIO_PIN_MAP(0, 14));
                nrf_rtc_task_trigger(rtc_handle.p_reg, NRF_RTC_TASK_CLEAR);
                nrf_drv_rtc_cc_set(&rtc_handle, 0,328, true);
    
                nrf_drv_rtc_int_enable(&rtc_handle, RTC_CHANNEL_INT_MASK(0));
                break;

    whether the compare value(328) which i give for nrf_drv_cc_set is fine ?

  • You only need to set the compare value and enable the interrupt once, like in the RTC example in the sdk. I suggest you try to get that example running first before you merge it with the rest of your code. 

  • i tried the same example given in SDK even in that compare event is getting triggered only once

    As per my understanding compare event should occur every 3seconds please correct me if i m wrong

    #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);
        }
        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();
        }
    }
    

  • You need to add nrf_rtc_task_trigger(&rtc, NRF_RTC_TASK_CLEAR); to line 34 of your snippet.

Related