nRF5340 RTC with external crystal clock

I am using nRF5340 RTC with external crystal. I have verified with following piece of code that LXFO is working. During normal scenario RTC is working fine as a counter.

	union {
		unsigned int raw;
		nrf_clock_lfclk_t lf;
		nrf_clock_hfclk_t hf;
	} src;
	enum clock_control_status clkstat;
	bool running;

	clkstat = clock_control_get_status(clock0, CLOCK_CONTROL_NRF_SUBSYS_LF);
	running = nrf_clock_is_running(NRF_CLOCK, NRF_CLOCK_DOMAIN_LFCLK, &src.lf);

I want to retain the RTC counter value when there is software reset e.g NVIC_SystemReset. Is it possible to achieve it with external crystal clock enabled?

Also we have two RTC peripherals in nRF5340, Is there anyway we can implement the Real time clock using both RTCs? Or we must have to use the external RTC for clock purpose?

  • Hi,

    I want to retain the RTC counter value when there is software reset e.g NVIC_SystemReset. Is it possible to achieve it with external crystal clock enabled?

    No, that is not possible. The RTC peripheral is reset to the default values, so it will start counting from 0. (This is regardless of the clock source.)

    Also we have two RTC peripherals in nRF5340, Is there anyway we can implement the Real time clock using both RTCs? Or we must have to use the external RTC for clock purpose?

    By real time clock here I assume you mean something like what you would get from an external RTC with calendar functionality etc? If so, you would have to implement this in software, with the help of an RTC to know how much time has elapsed since last time. Then you could use k_uptime_get(). But you would have to implement the calendar functionality yourself. And a reset would mean you loos track of time. If you need to consistently keep track of calendar (date and time) across resets, you should consider an external RTC with calendar functionality.

    By the way, there are 4 RTC instance, 2 for the app core and 2 for the net core. Depending on what you do, several of these may be unavailable though, as Zephyr use RTC for keeping track of time, and also the SoftDevice Controller if you are using bluetooth. 

  • Thanks for the info.

    We have the nRF9160 as well on the same board. I see also the nRF9160 have 2 RTC as well. From description it looks like that its same as nRF5340.

    Before we go ahead for the hardware update, I just want to be sure that is there a way to add calendar support persistent to soft reset? Will the nRF9160 be helpful in this scenario?

  • Hi,

    The RTC's in the nRF9160 is the same, they are just low frequency counters that are reset on a system reset. As you have an nRF9160 on the board, an option could perhaps be to get the time and date from the network? You may find this thread useful in that regard.

Related