This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

RTC1 forever counter (64 bits). Detect RTC1 overflow with S130

Can I detect overflow on RTC1 when using timers with S130? In my App, I use timers but also count time with NRF_RTC1->COUNTER. I need to know If overflow happens. It seems that the IRQHandler doesn't mather about event.

void RTC1_IRQHandler(void) { // Clear all events (also unexpected ones) NRF_RTC1->EVENTS_COMPARE[0] = 0; NRF_RTC1->EVENTS_COMPARE[1] = 0; NRF_RTC1->EVENTS_COMPARE[2] = 0; NRF_RTC1->EVENTS_COMPARE[3] = 0; NRF_RTC1->EVENTS_TICK = 0; NRF_RTC1->EVENTS_OVRFLW = 0;

// Check for expired timers
timer_timeouts_check();

}

  • Hi,

    In this thread Clem Taylor has modified the app_timers (RTC1 library from SDK) to generate oerflows. Please read that thread. He has posted his code in the GitHub. I think this is exactly what you need.

  • Ok, thanks. Too difficult to understand. I do instead a 64 bits counter based on OVERFLOW event counter. Calling a new function return a long term 64 bits for ever counter.

    New variable for overflow counter:

    static uint32_t 											RTC1_overflow_counter=0x0;			// Counts number overflows
    

    Adding and Event ant interruption overflow:

    static void rtc1_start(void){
    
    NRF_RTC1->EVTENSET = RTC_EVTEN_COMPARE0_Msk | RTC_EVTEN_OVRFLW_Msk;;
    NRF_RTC1->INTENSET = RTC_INTENSET_COMPARE0_Msk | RTC_INTENSET_OVRFLW_Msk;
    
    	NVIC_ClearPendingIRQ(RTC1_IRQn);
    NVIC_EnableIRQ(RTC1_IRQn);
    
    NRF_RTC1->TASKS_START = 1;
    nrf_delay_us(MAX_RTC_TASKS_DELAY);
    
    m_rtc1_running = true;}
    

    Counting overflows in the IRQ:

    void RTC1_IRQHandler(void){
    // Clear all events (also unexpected ones)
    	if (NRF_RTC1->EVENTS_OVRFLW){
    		RTC1_overflow_counter++;
    	}     
    NRF_RTC1->EVENTS_COMPARE[0] = 0;
    NRF_RTC1->EVENTS_COMPARE[1] = 0;
    NRF_RTC1->EVENTS_COMPARE[2] = 0;
    NRF_RTC1->EVENTS_COMPARE[3] = 0;
    NRF_RTC1->EVENTS_TICK       = 0;
    NRF_RTC1->EVENTS_OVRFLW     = 0;
    timer_timeouts_check();}
    

    And the function for returning the 64 bits counter. Billions of years counter.

    uint32_t app_timer_cnt_get64(uint64_t * p_ticks){
    *p_ticks = (((uint64_t)RTC1_overflow_counter) << 24) + NRF_RTC1->COUNTER;
    return NRF_SUCCESS;}
    

    I test it and I think it works well. I'm not sure if doesn't mather calling timer_timeouts_check() at OVERFLOW interrupts, I think so.

  • what value did you put in compare0 register? max value?

  • I only modify app_timer.c adding a 64 bit counter based on overflow events. Compare0 register is managed by app_timer in the same way it did before. There must be at least one active timer to avoid RTC1 to stop.

  • looks good if your program can accept that sometimes when the overflow happens the interrupt can be masked by softdevice activity for some duration. If this limitation is acceptable, then you code looks good.

Related