How to access the TIMER3 in wireless timer sync?


I found this great blog entry about synchronizing timers on different devices:

It works just fine on a nRF52840DK and a nRF52DK. Timers on multiple nRF5 devices are synced by one device (master) sending sync packets containing captured timer values to other devices (nodes). (TIMER3 is free running, TIMER2 counts TIMER3 overflows).

At the moment I am struggeling to access this synchronized TIMER3 on the two boards. I am familiar with setting up a TIMER without wireless timer sync. 

So here are my questions:

1. How can I define CC, Prescaler, Mode, Bitmode, the interrupt handler and so forth when using wireless timer sync?

2. Can anybody provide me some advice how to access this synchronized TIMER3 properly? 

Thank you very much in advance.

  • Hello,

    can you share some more details regarding your use case? The following suggestions may be helpful for your case, otherwise we can see what options there are for you needs.

    If you want to get a timestamp value, you can use the timestamp function (either 32 or 64-bit version):

    If you want to trigger something at a given time, the ts_set_trigger function might be helpful. This lets you set up a PPI to be triggered at a given time in the future.

    There are two TIMERs involved in this code, TIMER2 is used in timer mode, while TIMER3 is used in counter mode. By default, TIMER2 has a quite short CC/reset of 40000 (=40000/16 MHz = 2.5 ms wraparound). The counter is incremented for every wraparound (2.5 ms). This means that the timestamp function uses both of these TIMER values to calculate the current time with 16 MHz tick resolution.

    Note that the ts_set_trigger function operates on the counter (TIMER3), which by default has a resolution of 2.5 ms. If you need finer resolution, you could reduce the TIME_SYNC_TIMER_MAX_VAL value.

    Prescaler, bitmode, etc. is currently hardcoded. I would generally not recommend making changes to those.

    Accessing the TIMERs directly is also not something I recommend, because it is quite tricky to handle the corner cases of TIMER wraparound and adjustment

  • Hello Audun,

    First of all, thank you so much for your reply and your great blog entry about wireless timer sync. Of course, I will check your suggestions out.

    Here is my use case:

    I have two development kits. A nRF52840DK (central) and a nRF52DK (peripheral). I flashed the provided "wireless timer sync" code from github on them. It works just fine.

    My goal is to have a similar counter on the central and the peripheral that increments every 1ms. The counter on central and peripheral should always contain the same value.

    I already found this ( I am not sure, but I think there was someone trying to do the same.

    What is the smartest way to achieve this goal?

    I appreciate any suggestions.

    Thank you very much in advance.


  • Hi Michael,

    thanks for your kind words! Always happy to hear the blog post and code is being read and used.

    Regarding the 1 ms counter in your use case, what will you do with this counter? I'm trying to understand if the timestamp or trigger functions in the current version of time_sync.h can support the functionality you are looking for.

    TIMER3 does almost cover the 1 ms timer function you are describing. There are two exceptions:

    1) The counter is currently running at 2.5ms, but this should be easy to adjust by setting TIME_SYNC_TIMER_MAX_VAL  to 16000 instead of 40000

    2) Although the TIMER3 counting rate will happen in a synchronized fashion on the Central and Peripheral, there will be an offset between the two counters. This offset is handled in software and accounted for when you use the timestamp and trigger functions, but not if you were to read out the NRF_TIMER3 registers directly.

    If you wanted to, for example toggle a GPIO for every 1 ms tick, this can be achieved by changing the TIME_SYNC_TIMER_MAX_VAL  as described, and using the ts_set_trigger() function. Similar to what is done here

    Hope this helps!


  • Hello Audun,

    thank you for your reply.

    Regarding the 1 ms counter in your use case, what will you do with this counter?

    I use the SAADC on the peripheral. The SAADC readings are sent from the peripheral to the central via NUS. Each NUS-paket gets a timestamp. The timestamp is the actual value of the 1ms counter.

    On the central, I read out two GPIOs as digital inputs and save them in an array. So in my application the SAADC on the peripheral samples at the same time as the central reads out the digital inputs.

    With the synchronized 1ms counter, I can restore the incoming NUS-pakets to the readings of the digital inputs. (Because incoming NUS-pakets are not time consistent)

    If you wanted to, for example toggle a GPIO for every 1 ms tick

    So I do not need to toggle a GPIO with a 1ms tick (But it would work if I change TIME_SYNC_TIMER_MAX_VAL to 16000).

    Instead I need to access this synchronized 1 ms counter in my main.c on my central and my peripheral.

    A offset up to +-0,5ms between the the counter on central and peripheral is okey. But the two counters should not start to drift away from each other.

    In conclusion, I need a 1ms counter on central and peripheral with the same value, and which are accessible in my main.c.

    Do you have any suggestions how to implement this properly?

    Thank you very much in advance.

  • Thanks for the details!

    It sounds like the timestamp functions would cover your need.

    When the time_sync is enabled, ts_timestamp_get_ticks() operates on the shared time base. If you call this function at exactly the same time on both peripheral and central, they should return the same value. (or, in reality almost the same value as the clocks are always drifting).

    Specifically, on the peripheral side call ts_timestamp_get_ticks() either when you make the SAADC measurement, or right before you call the NUS send() function. If you want millisecond resolution, you should round the timestamp value to the closest milliseconds (from the 16 MHz tick format). Include the rounded value in the NUS packet.

    On the Central side, call ts_timestamp_get_ticks() when you read the GPIOs, round the timestamp to the closest millisecond, and store in the array.

    When the Central receives the NUS packets, it can iterate through the array to figure out what the GPIO state was at the time of the NUS packet timestamp.

    You wont have to make any changes to TIME_SYNC_TIMER_MAX_VAL with this approach.

    Does this make sense?

    To achieve millisecond synchronization accuracy you could use a fairly slow sync packet transmit rate (1 Hz is probably more than enough). For example with the 10 ppm crystals on the Devkit the clocks should not drift more than 2 x 10 microseconds per second.

    Also, I wanted to mention that you might get the desired accuracy by simply keeping track of the BLE connection events. BLE is a synchronized protocol, where the Central always dictates the timing.

    Using native BLE connection event counting would save you some power, if this is a concern. Specifically, one can use the radio notification functionality to trigger an interrupt in the application at a fixed offset from every connection event. For example, with a 10 ms connection interval, you would get an interrupt every 10 ms. By keeping track of the number of these interrupts from the beginning of the BLE connection, and starting a timer in the interrupt, you can calculate the timestamp by n * 10 ms + timer value, where n is the connection event count.