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

Extending RTC beyond 24 bits

I am looking to extend the RTC beyond 24 bits using the Overflow interrupt. The code in the rtc handler would look like:

static void rtc_handler(nrf_drv_rtc_int_type_t int_type)
{
    if (int_type == NRF_DRV_RTC_INT_OVERFLOW)
    {
        ticks_overflow++;
    }
}

And the code to read the extended timer would look like:

uint32_t get_rtc_count()
{
   return (( ticks_overflow << 24) + nrf_drv_rtc_counter_get(&rtc);
}

This would obviously create a race condition since the overflow interrupt could interrupt the get_rtc_count in the middle of performing the extension.

Now the obvious approach is to protect the get_rtc_count call with a critical region as in:

unit32_t nrf_get_rtc_count()
{
	uint32_t ticks;
	sd_nvic_critical_region_enter();
        ticks = ((ticks_overflow<<24) + nrf_drv_rtc_counter_get(&rtc));
	sd_nvic_critical_region_exit();
	return (ticks)
}

However this seems to be overkill since the only interrupt that needs to be disabled during the extension operation is the overflow interrupt.

So my question is, can the same operation be safely performed by disabling only the overflow interrupt as in:

unit32_t nrf_get_rtc_count()
{
	uint32_t ticks;
	nrf_drv_rtc_overflow_disable(&rtc);
    ticks = ((ticks_overflow<<24) + nrf_drv_rtc_counter_get(&rtc));
	nrf_drv_rtc_overflow_enable(&rtc,true);
	return (ticks)
}

What is not clear is, if the overflow is disabled, and an overflow condition occurs while the extension operation is performed, does the interrupt occur once the overflow interrupt is re-enabled? Or has that interrupt essentially been lost? Thanks in advance for any help.

Related