Hi!
I'm not good at C and microcontrollers programming. Could somebody clarify, if nrf_rtc_counter_get interrupt safe? It just reads value from rtc register->COUNTER (24bit), how many ticks it takes?
Thanks.
Hi!
I'm not good at C and microcontrollers programming. Could somebody clarify, if nrf_rtc_counter_get interrupt safe? It just reads value from rtc register->COUNTER (24bit), how many ticks it takes?
Thanks.
Hi,
Could somebody clarify, if nrf_rtc_counter_get interrupt safe? It just reads value from rtc register->COUNTER (24bit), how many ticks it takes?
As you mention, it reads the ->COUNTER register. It does not guard (disable / enable) against any interrupt that might occur while reading the register.
You can call this from any priority.
One tick is one cycle on the RTC (32.768k), and the register is read via the CPU, so it'll take alot less time than one tick to read. See Turbo J's excellent answer on this.
If you have a function that should not be interrupted, you can use the macros "CRITICAL_REGION_ENTER()" and "CRITICAL_REGION_EXIT()" to guard that function.
Kind regards,
Håkon
how many ticks it takes?
Should take much less than one RTC tick, as this register is latched in the PCLK16M clock domain.
From the NRF52840 PS (chapter 6.22.9): "Read takes the CPU 2 cycles in addition resulting in the COUNTER register read taking a fixed five PCLK16M clock cycles."
Note that this does not include setup instructions - you may need to put the register address into a core register first.
Thanks for answers. As I understood it's no guarantees that
counter = nrf_rtc_counter_get(&rct_inst);
is (upd)interrupt safe. Another questions, is it possible without "CRITICAL_REGION_..." to have inconsistent counter value? For example, coping value is two ticks, and between them another interrupt occur, and before second tick rtc overflow happend.
Hi, I posted question under latest answer, maybe you didn't notice it. https://devzone.nordicsemi.com/f/nordic-q-a/59937/is-nrf_rtc_counter_get-interrupt-safe/243846#243846
Hi,
It s a free-running timer, not tied to the CPU at all. Yes, you can read it out two consecutive times and get different value, for instance that the RTC has incremented its counter just after the first read, or if you have an interrupt occurring between the two reads.
Kind regards,
Håkon