Regarding accuracy of RTC based on Internal clock

Hello,

I am using a BL653dvk (nrf52833 based) for my application development.

I need to have a timer module, which is at the least accurate to 1ms in time regulation, once I set the time.

But my observation is that, once I set the time, it varies with a difference of +/-43 secs in an hour.

So my question is,
1. Is this expected..?

2. If this can be improved upon, how do I improve on this..?
Please find my code as in below as I am using RTC:

bool overFlowEventHappened = false;
bool isOverflowEnabled = false;
bool debugOverflowReadback = false;
volatile uint64_t RTC2SystClock = 0;
uint64_t debugRTC2ClockCounter = 0;

void rtc_nrf_isr(const void *arg)
{
	ARG_UNUSED(arg);
  	overFlowEventHappened=  (bool)RTC->EVENTS_COMPARE[0];//(bool)*(volatile uint32_t *)((uint8_t *)RTC + (uint32_t)NRF_RTC_EVENT_OVERFLOW);
  	isOverflowEnabled = RTC->INTENSET & NRF_RTC_INT_OVERFLOW_MASK;
  	RTC2SystClock =CYC_PER_TICK;
	static uint8_t secondCounter = 0;  
    {
    	RTC->EVENTS_OVRFLW=0;
     	debugOverflowReadback = RTC->EVENTS_OVRFLW;
    	secondCounter++;
		if(secondCounter >=2)
		{ 
			#ifdef RTCGPIOTOGGLE
				gpio_pin_toggle( dev_gpio0, PIN_LED);
				debugHKSec++;
			#endif
			
			secondCounter = 0;
			overflow_cnt += NUM_SEC_TO_OVERFLOW;

			debugRTC2ClockCounter++; 
     	} 
  	}

  	RTC->EVENTS_COMPARE[0] = 0;
  	RTC->EVENTS_OVRFLW=0;

}

int sys_clock_driver_init()
{
	static const enum nrf_lfclk_start_mode mode =
	IS_ENABLED(CONFIG_SYSTEM_CLOCK_NO_WAIT) ?
		CLOCK_CONTROL_NRF_LF_START_NOWAIT :
		(IS_ENABLED(CONFIG_SYSTEM_CLOCK_WAIT_FOR_AVAILABILITY) ?
		CLOCK_CONTROL_NRF_LF_START_AVAILABLE :
		CLOCK_CONTROL_NRF_LF_START_STABLE);

	RTC->PRESCALER = 0;
	for (int32_t chan = 0; chan < CHAN_COUNT; chan++) 
	{
		cc_data[chan].target_time = TARGET_TIME_INVALID;
		RTC->INTENSET = RTC_CHANNEL_INT_MASK(chan);
	}


	RTC->INTENSET = NRF_RTC_INT_OVERFLOW_MASK; 

	enableRTCIRQ();
	TriggerRTCTask();

	int_mask = BIT_MASK(CHAN_COUNT);
	if (CONFIG_NRF_RTC_TIMER_USER_CHAN_COUNT) 
	{
		alloc_mask = BIT_MASK(EXT_CHAN_COUNT) << 1;
	}

	uint32_t initial_timeout = IS_ENABLED(CONFIG_TICKLESS_KERNEL) ?
		(COUNTER_HALF_SPAN - 1) :
		(counter() + CYC_PER_TICK);
		
	RTC->CC[0]= 16384*NUM_SEC_TO_OVERFLOW;
	z_nrf_clock_control_lf_on(mode);

	return 0;
}

void enableRTCIRQ()
{
	NVIC_ClearPendingIRQ(RTC_IRQn);
	IRQ_CONNECT(RTC_IRQn,4, rtc_nrf_isr, 0, 0);
	irq_enable(RTC_IRQn);	
}

void TriggerRTCTask()
{
	nrf_rtc_task_trigger(RTC, NRF_RTC_TASK_CLEAR);
	nrf_rtc_task_trigger(RTC, NRF_RTC_TASK_START);
}


Thanks

Parents Reply Children
No Data
Related