Timer not running at correct rate

Hi,

I'm working on a project where I need to sample an ADC with an accurate sample rate. It essentially works except that I do not get the correct rate from the timer. It is close but not correct. I have verified this using the clock on my dev board (nRF54L15-DK) and by setting an output pin and measuring with an oscilloscope.

My timer is configured to trigger the ADC 400 times/s (2.5 ms), but when I run it, I most often get a rate around 404 triggers/s. Sometimes it runs at 398 and sometimes at 400.

My code is based on the DevAcademy "Exercise 3 – Interfacing with ADC using nrfx drivers and TIMER/PPI" (academy.nordicsemi.com/.../), and in fact to me it looks like the same problem exists in the example in the exercise. In the example code the timer runs with an interval of 50 us, the ADC fills buffers of 8000 samples which equals to a full buffer every 400 ms. I would expect the timestamps of the log to be 400 ms apart (with some jitter), but instead it looks like they are drifting over time. I see it when running the example on my nRF54L15-DK, and it can be seen in the log at the end of the exercise as well where there is about 399 ms between log entries.

Log from the exercise:
*** Booting nRF Connect SDK ***
[00:00:00.653,900] <inf> main: SAADC buffer at 0x20001150 filled with 8000 samples
[00:00:00.653,900] <inf> main: AVG=2064, MIN=2029, MAX=2099
[00:00:01.052,673] <inf> main: SAADC buffer at 0x20004fd0 filled with 8000 samples
[00:00:01.052,673] <inf> main: AVG=2064, MIN=2025, MAX=2097
[00:00:01.451,446] <inf> main: SAADC buffer at 0x20001150 filled with 8000 samples
[00:00:01.451,446] <inf> main: AVG=2064, MIN=2028, MAX=2094
[00:00:01.850,097] <inf> main: SAADC buffer at 0x20004fd0 filled with 8000 samples
[00:00:01.850,128] <inf> main: AVG=2062, MIN=2009, MAX=2100
[00:00:02.248,840] <inf> main: SAADC buffer at 0x20001150 filled with 8000 samples
[00:00:02.248,870] <inf> main: AVG=2064, MIN=2029, MAX=2103
[00:00:02.647,521] <inf> main: SAADC buffer at 0x20004fd0 filled with 8000 samples
[00:00:02.647,521] <inf> main: AVG=2064, MIN=2025, MAX=2098

Why do I see this drift? I hope someone can enlighten me.

br
Kristoffer
Parents
  • Hi,

    The timer in nRF54L15 runs from the internal oscillator, which is only accurate to ~1% which results in 404 or 398 samples. Thus, the continuous sampling interval offset accumulates which looks like "drift" in your timestamps.

    How to fix: Request the 32 MHz crystal (HFXO) from your application and keep it running while you sample. Here is LINK to set it up.
    Please let us know if you need further guidance.

    Best Regards,
    Pallavi

  • Hi Pallavi,

    thanks for the quick answer!

    Requesting the 32 MHz crystal does solve the problem. Thanks for the link!

    I'm still a bit curious though of why the rate is (was) different after each reboot, I assumed the internal oscillator mainly would change as a function of temperature, but I see the rate go up or down after a reboot which I found confusing, I have also seen cases where the timer runns at one rate for perhaps a minute and then suddenly changes to a different rate. 

    I do have the BT stack running as well in my code, and my understanding is that the HF clock is switched on/off by the BT implementation when the radio is used (I found the z_nrf_clock_bt_ctlr_hf_request() and z_nrf_clock_bt_ctlr_hf_release() functions). I suspect that this is what is changing the rate, is that correct?

    br

    Kristoffer

Reply
  • Hi Pallavi,

    thanks for the quick answer!

    Requesting the 32 MHz crystal does solve the problem. Thanks for the link!

    I'm still a bit curious though of why the rate is (was) different after each reboot, I assumed the internal oscillator mainly would change as a function of temperature, but I see the rate go up or down after a reboot which I found confusing, I have also seen cases where the timer runns at one rate for perhaps a minute and then suddenly changes to a different rate. 

    I do have the BT stack running as well in my code, and my understanding is that the HF clock is switched on/off by the BT implementation when the radio is used (I found the z_nrf_clock_bt_ctlr_hf_request() and z_nrf_clock_bt_ctlr_hf_release() functions). I suspect that this is what is changing the rate, is that correct?

    br

    Kristoffer

Children
No Data
Related