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
  •  Hie Richard,    

    The internal oscillator is not the actual reason why you see different rate after each reboot.   As you suspected, the BLE stack is switching HFXO on and off around radio events which causes changes in the sampling rate.

    When HFXO is running, your TIMER achieves better accuracy but when it is off, the TIMER falls back to HFINT. [link : https://nrfconnectdocs.nordicsemi.com/ncs/latest/nrfxlib/mpsl/doc/clock.html]

    This explains both symptoms:

      1. Different rate after each reboot: Every time you reboot, your TIMER samples land at a random point relative to when BLE turns HFXO on and off.I f your sampling tick occurs when HFXO is on, accurate timingIf your sampling tick occurs when HFXO is off, inaccurate timing. Since this random alignment is different after every reboot, you get a different sampling rate each time.
      2. Rate changing mid-run:  Similarly if your TIMER samples sometimes land in an on window (accurate) and sometimes in an off window (inaccurate), so your sampling rate changes mid-run.

    FIX
    The solution is to keep HFXO always on, so your TIMER always has a stable, accurate clock regardless of BLE activity. 

    Please let us know if you have any follow up questions.
    BR
    Pallavi
Reply
  •  Hie Richard,    

    The internal oscillator is not the actual reason why you see different rate after each reboot.   As you suspected, the BLE stack is switching HFXO on and off around radio events which causes changes in the sampling rate.

    When HFXO is running, your TIMER achieves better accuracy but when it is off, the TIMER falls back to HFINT. [link : https://nrfconnectdocs.nordicsemi.com/ncs/latest/nrfxlib/mpsl/doc/clock.html]

    This explains both symptoms:

      1. Different rate after each reboot: Every time you reboot, your TIMER samples land at a random point relative to when BLE turns HFXO on and off.I f your sampling tick occurs when HFXO is on, accurate timingIf your sampling tick occurs when HFXO is off, inaccurate timing. Since this random alignment is different after every reboot, you get a different sampling rate each time.
      2. Rate changing mid-run:  Similarly if your TIMER samples sometimes land in an on window (accurate) and sometimes in an off window (inaccurate), so your sampling rate changes mid-run.

    FIX
    The solution is to keep HFXO always on, so your TIMER always has a stable, accurate clock regardless of BLE activity. 

    Please let us know if you have any follow up questions.
    BR
    Pallavi
Children
Related