time synchronization across different nodes in a mesh network, single hop as well as multi hop.

raghav gravatar image

asked 2017-05-15 10:05:54 +0100

updated 2017-05-15 10:07:26 +0100

I am working on time synchronizations between different nodes in a mesh network. I have earlier worked on the radio and the ppi examples given. I wish to generate time stamps at each send and receive events between two different nodes for which i am using the nRF radio along with two crystal oscillators, 16 M XOSC as well as 32.768 KHz XOSC with quartz crystal. Could you let me know that if i am using the ppi with the radio functionality, how do i define the two nodes in my code between which i want to send packets. then can i extend this case for n nodes too.

edit retag flag offensive close delete report spam


Hi Khosla,

How do you plan to connect your mesh network ? How would the re-transmission between nodes in the network work ?

It you only plan to synchronize between 2 device (or multiple devices but not mesh) you can have a look here.

Hung Bui ( 2017-05-16 10:56:59 +0100 )editconvert to answer

Thanks for this. Seems good enough as a guide!!!

I am using the ppi code with radio in segger as seen below. Considering both the the transmit as well as receive cases in the same code. I have to collect the timestamps for each of the send as well as receive events for each of the two crystal oscillators, 16 MHz as well as 32.768 KHz XOSC as i mentioned earlier. Just let me know ur comments on how appropriate the code is. Basically, my task is to synchronize between two neighboring nodes (single hop) as well as then extend this to synchronize between 7-8 nodes.

Lemme know if something can be added in the code. One more thing is that i would be using only hardware clocks as you defined too, the timer and RTC.

Here is the code:

/* Copyright (c) 2014 Nordic Semiconductor. All Rights Reserved.

  • The ...
khosla ( 2017-05-16 12:36:32 +0100 )editconvert to answer

3 answers

Sort by » oldest newest most voted
hungbui gravatar image

answered 2017-05-16 16:27:20 +0100

raghav gravatar image

updated 2017-05-17 12:15:00 +0100

Please attach your code as a text file. Please have look again at your comment above, how would you expect me to read what you pasted ?

You still haven't described clearly enough what you are planing to do and what the difficulties you have. Please try to follow the tutorial and get back if you have issues.

edit flag offensive delete publish link more



This is the code i would be using for my work. I looked at the link you gave me. But it would be good if u could see mine and lemme know whether its appropriate enough. As far as the main problem is concerned, my task is synchronizing all events in a mesh network. I need to timestamp the events using hardware timers. Then once i have the time values, i can verify them by sniffing packets on air and seeing results on the Logic Analyzer. Then getting the data out from the board to the pc. Using certain algorithms to estimate the drift and the offset variations with regard to the time events recorded. Hope this will clarify your queries.

khosla ( 2017-05-17 12:14:10 +0100 )editconvert to answer

In your code I can't see how the mesh work. You simply send broadcast data. What about the receiver? How does the mesh work ?? Is there any retransmission , rebroadcast ?? I don't see in your code where you put the timestamp into the broadcast data, or maybe I misread it.

Hung Bui ( 2017-05-18 14:07:36 +0100 )editconvert to answer

You can find the receiver functionality slightly above the main function. Since i am still experimenting on this, i was earlier thinking to collect the timestamp information (send and receive times) with the help of wireshark and analyser. But this won't be precise enough since there is no idea of the clocks analyser is using and that wireshark receive times can't be relied on. Although this could be done as a verification part only. I now ought to add some more timer functionality to achieve this. But i had another query that if i use the capture functionality in hardware timer, how do i go about it without CPU intervention, as we are normally doing this by calling TASKS_CAPTURE for each capture event in the code. But this involves CPU. What's the other CPU independent way for doing this. Would appreciate your advice!!!

khosla ( 2017-05-18 15:42:56 +0100 )editconvert to answer
hungbui gravatar image

answered 2017-05-19 09:38:49 +0100

raghav gravatar image

updated 2017-05-19 11:38:01 +0100

Yes I can see start_reciever() but I don't see when it's called.

Yes, using wireshark and analyser is for verification. The question is how you are planing to do the actual time synchronization ? Do you have an idea of what you are trying to achieve ?

Basically please explain what you meant here: I wish to generate time stamps at each send and receive events between two different nodes for which i am using the nRF radio

The main point of time synchronization is to calibrate multiple devices to start a timer at the same time. By having the synchronized timer we can add a time stamp on every data we sample or execute a command at the same time.

What we do in the example I pointed you to (which only takes 15 minutes to read and 5 minutes to test ) is to do TASKS_CAPTURE on the timer of the master, add the latency (consistent time delta by using another timer), then put that value in the radio packet.

On the receiver/slave side, when receiving the packet, it uses that timer captured value to update its own timer with that value (includes the delta). After that the timers on both device should be synchronized.

Note that this solution is only for a start network, where one single node can update multiple nodes around it. I don't know how do you plan to do it for a mesh network. That why I keep asking how do you plan to do your mesh.

And yes, we don't have a solution to avoid CPU involve here. We need the CPU to put the captured timer value to radio packet. But we can have a consistent time for doing that by using another timer and PPI to start radio transmission a fixed time after the capture task.

edit flag offensive delete publish link more



This is a part of my thesis work basically. I am using the tiny sync algorithm to achieve this. Refer to fig 1 and then section 4 on page 1270. basically i need to collect the three time stamps, t_o, t_b and t_r for each probe message sent. The packet itself would be carrying the timestamp to the destination.

U mean to say that while using timers too for all events we would always be requiring the CPU. Although timers have the inbuilt functionality to minimize CPU intervention. There are probably some related tasks of Nordic in the SDK itself that have been done without using CPU. But i still need to figure out how to do the capture tasks independent of the CPU.

khosla ( 2017-05-19 11:49:31 +0100 )editconvert to answer

Also i would be just using capture events for capturing the event times. So no compare events will be involved.

khosla ( 2017-05-19 12:05:02 +0100 )editconvert to answer

Thanks for providing the document. My understanding is that you need to capture time stamp on both side and your approach require time-stamp packet from both direction to do calculation and estimate a12, b12 from that.

Our approach is a little bit different that we only use packet from one direction and try to reset the timer on node 2 to set the offset b12 to zero using the time stamp. We reduce the affect of the drift a12 by keep sending time stamp packet so that the drift create by the timer won't exceed the real time requirement.

You can re-use what we implemented in our example on the transmission side. Use PPI to trigger a TASK_CAPTURE of the free run timer from COMPARE event of another timer (TIMER2) (You can ignore this, and trigger TASK_CAPTURE your way) Then use CPU to put the Captured value of the ...(more)

Hung Bui ( 2017-05-19 13:07:11 +0100 )editconvert to answer

Good enough. I will remain in touch with you on this if you don't mind. The CPU intervention problem is solved by using the the PPI itself. But perhaps i need to in initialize it earlier in the process so as to avoid delay. But when we are using timer 0, 1 and 2, are these individual timers present at each of the nodes or these are just separate instances of the same timer on one node. Because as u mentioned we are using one timer and one RTC for each node right.

khosla ( 2017-05-19 14:24:14 +0100 )editconvert to answer

Also as per the algorithm, i need to generate a timestamp t_o just when the packet arrives at node 1. So will it be done using a timer itself and if yes, at what point in the code i need to load the timestamp in the packet.

khosla ( 2017-05-19 14:44:26 +0100 )editconvert to answer

In our example we use 2 TIMERs to improve accuracy. The time stamp is only taken on one timer (the free runner) the other timer is just to do the task of triggering TASK_CAPTURE and RADIO->TASKS_START. They are individual hardware timers.

I don't understand this "generate a timestamp t_o just when the packet arrives at node 1" . What do you mean by arrive at node 1 ? I thought t_o is when you send the packet from node 1 ?

Hung Bui ( 2017-05-19 16:53:21 +0100 )editconvert to answer

ya but if u see fig 1 in the doc, u see t_o as the the time of send event being generated at node 1 interface. t_o being the time just before the packet is sent as labelled. i propose to follow the algorithm but will make some modifications at later stages in this algorithm. you are right. i wrote it the wrong way. sorry!!!! now i will try working on this the coming weekend and then discuss further if required. thanks alot for ur support.

khosla ( 2017-05-19 17:48:10 +0100 )editconvert to answer

If I understand you correctly, you want to generate the time stamp at exact moment when it's sent from Node 1 , right ? It's impossible to capture the timer at exact time when the packet is sent and put it into the radio packet at the same time. But we can do calculation and estimate the exact value of the timer when the radio packet is sent. The number of cycles from the time we capture the timer value until the time we trigger TASK_START on the radio is determined (by using the second timer and PPI), so we can calculate the exact time stamp.

Hung Bui ( 2017-05-22 12:40:21 +0100 )editconvert to answer
raghav gravatar image

answered 2017-05-31 14:24:23 +0100

updated 2017-05-31 17:06:38 +0100

button.txtHi if i use the same code for both the transmitter and receiver, as u saw in the earlier paper i sent u, there would be confusion regarding whether node 1 is transmitting to node 2 or node 2 is transmitting back to 1. Since there are 2 transmit and 2 receive events taking place in one full cycle as per the figure. How to fix this? Probably one way could be that in the main function, i define which button ticks corresponding to which node. But how to define it in the code.

The second thing that i need to load the timestamp in the packet just before it is sent, where exactly the timestamp value loaded should be shown in my case ?


edit flag offensive delete publish link more


I don't think using same code works here. One must be the initiator and the other one response when it receive the first packet.

I tried to explain you how the time stamp sampling and how we calculated the latency, add them up and put it to radio packet, several times in my comments earlier. Please try to test our example to understand how it works.

Hung Bui ( 2017-05-31 15:56:01 +0100 )editconvert to answer

ya that could be done but what if we modify the button functionality to distinguish between the events, since i need to devise something different in my thesis work than what has been done. can something be done about that?

khosla ( 2017-05-31 16:13:14 +0100 )editconvert to answer

What do you mean by "button ticks" ? which button are you talking about here ?

Hung Bui ( 2017-05-31 16:58:27 +0100 )editconvert to answer

the led buttons on the board in my main function i use the functionality

khosla ( 2017-05-31 17:05:17 +0100 )editconvert to answer

have attached the appbutton.c file with the main.txt file

khosla ( 2017-05-31 17:07:25 +0100 )editconvert to answer

So you want to use the button to choose which role your device is ? Please clearly state the purpose of the button.

Hung Bui ( 2017-06-01 11:12:17 +0100 )editconvert to answer

hi basically it goes like this: In the main function, I call app button push (0) callback function. The idea is to wait and listen for button or reception. If button, then transmit. If reception, then you are node 2 and probably want to transmit back

khosla ( 2017-06-01 11:41:22 +0100 )editconvert to answer

Yes, you can implement that ( I don't see anything ab that in your source code) but then both device have to be in receiving mode all the time, so when you press the button to transmit it would be able to receive the packet.

Hung Bui ( 2017-06-01 14:27:54 +0100 )editconvert to answer

more or less in this manner. i have modified my code lately.

but this seems feasible. will try to get the samples and see how it goes.then, i will get back.


khosla ( 2017-06-01 14:32:59 +0100 )editconvert to answer

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer. Do not ask a new question or reply to an answer here.

[hide preview]

User menu

    or sign up

Recent questions

Question Tools


Asked: 2017-05-15 10:05:54 +0100

Seen: 197 times

Last updated: mai 31 '17