4 #include "serial_irq.h"
5 #include "serial_dma.h"
10 * Devices 1 and 2 synchronize clocks using serial messages.
12 * 1. Each serial message timestamped using the hardware timer capture
13 * registers in both the sender and receiver.
14 * 2. The sender transmits the send timestamp during the next time-sync
16 * 3. The receiver then compares the senders timestamp with it's own
17 * timestamp for the corresponding messages and calculates an offset.
18 * 4. The offset is used to compensate the receivers local clock.
20 * Time synchronization is performed in both directions.
27 #define NSEC_PER_SEC 1000000000ULL
29 uint64_t time_last_local; // timestamp at last time sync
30 uint64_t time_last_world; // offset at last time sync
33 * Generate time stamp for an async event:
34 * local: drift compensated wall-clock time
35 * world: nanoseconds in world time world
36 * valid: local timestamp at valid valid
38 //uint64_t time_to_local(uint64_t world, uint64_t valid)
45 * Generate time stamp for an async event:
46 * time: drift compensated wall-clock time
47 * stamp: event timestamp from PIT Module
49 uint64_t time_to_world(uint64_t local)
51 uint64_t elapsed = local - time_last_local;
52 return time_last_world + elapsed;
56 * Compensate the Real-Time-Clock oscillator for
57 * temperature and drift errors. Called at 1Hz and
58 * synchronous to the RTC 1Hz output.
60 void time_rtc_comp(void)
66 * Synchronize the timer internal state with updates
67 * from an external time sync message.
68 * local: our internal timestamp for the event
69 * world: reference timestamp from the other device
71 void time_ext_init(uint64_t local, uint64_t world)
73 sirq_printf("initialize clocks: %d -> %d\r\n",
74 (int)(local/NSEC_PER_SEC),
75 (int)(world/NSEC_PER_SEC));
77 time_last_local = local;
78 time_last_world = world;
82 * Synchronize the timer internal state with updates
83 * from an external time sync message.
84 * local: our internal timestamp for the event
85 * world: reference timestamp from the other device
87 void time_ext_sync(uint64_t local, uint64_t world)
89 sirq_printf("syncing clocks: %d -> %d\r\n",
90 (int)(local/NSEC_PER_SEC),
91 (int)(world/NSEC_PER_SEC));
93 time_last_local = local;
94 time_last_world = world;
97 /************************
98 * Serial I/O functions *
99 ************************/
107 const uint64_t serial_sync_delay = NSEC_PER_SEC * 2; // 1hz
108 static uint64_t serial_sync_due = 0;
110 static tdma_t *serial_tdma_rcv = NULL;
111 static tdma_t *serial_tdma_xmt = NULL;
113 static uint64_t serial_prev_local = 0;
114 static uint64_t serial_prev_seq = 0;
116 static uint64_t serial_xmt_local = 0;
117 static uint64_t serial_xmt_seq = 0;
120 * Output time sync message
122 void serial_send_sync(sirq_t *port, uint64_t now)
124 if (serial_sync_due == 0 || now < serial_sync_due)
127 // Calculate world time
128 uint64_t world = time_to_world(serial_xmt_local);
134 // Transmit sync message
135 head.header = MSG_HEADER;
136 head.msgid = MSG_ID_SYNC;
137 head.length = sizeof(body);
138 head.cksum = 0; // todo
140 body.seq = serial_xmt_seq;
141 body.time.seconds = world / NSEC_PER_SEC;
142 body.time.nanosec = world % NSEC_PER_SEC;
144 sirq_write(port, &head, sizeof(head));
145 sirq_write(port, &body, sizeof(body));
151 sirq_printf("sync msg transmit\r\n");
153 // save transmit time
154 for (int i = 0; i < 1000; i++)
156 int valid = tdma_stamp(serial_tdma_xmt, &serial_xmt_local);
158 sirq_printf("missing sync transmit time\r\n");
159 tdma_reset(serial_tdma_xmt);
163 * Output external event received message
164 * event: id of the received event
165 * time: compensated timestamp of the event
167 void serial_send_event(uint16_t event, uint64_t local)
169 uint64_t world = time_to_world(local);
172 time.seconds = (uint32_t)(world / NSEC_PER_SEC);
173 time.nanosec = (uint32_t)(world % NSEC_PER_SEC);
175 sirq_printf("event received - %08x:%08x - %u.%u\r\n",
176 (uint32_t)(local >> 32), (uint32_t)local,
177 time.seconds, time.nanosec);
182 * Handle sync message
184 void serial_handle_sync(sync_msg_t *msg)
186 // Read receive timestamp for next time sync message
187 uint64_t current = 0;
188 int valid = tdma_stamp(serial_tdma_rcv, ¤t);
190 sirq_printf("missing sync receive time\r\n");
191 tdma_reset(serial_tdma_rcv);
194 uint64_t world = ((uint64_t)msg->time.seconds) * NSEC_PER_SEC
195 + ((uint64_t)msg->time.nanosec);
199 uint64_t local = tdma_time();
200 time_ext_init(local, world);
203 // Valid times timestamp
204 if (serial_prev_seq == (msg->seq-1)) {
205 uint64_t local = serial_prev_local;
206 time_ext_sync(local, world);
209 // Queue transmit to other board
210 serial_sync_due = tdma_time() + serial_sync_delay;
213 serial_prev_local = current;
214 serial_prev_seq = msg->seq;
218 * Handle event message
220 void serial_handle_event(event_msg_t *msg)
227 void serial_deliver(int msgid, void *body)
231 sirq_printf("received sync msg\r\n");
232 serial_handle_sync((sync_msg_t*)body);
235 sirq_printf("received event msg\r\n");
236 serial_handle_event((event_msg_t*)body);
242 * Process serial receive messages
244 void serial_receive(parser_t *parser, int byte)
246 //sirq_printf("serial_receive - %02x\r\n", byte);
249 header_t *head = (header_t*)parser->buffer;
250 void *body = (void*)(head+1);
251 const int max_length = sizeof(parser->buffer)-sizeof(header_t);
253 // Process uart messages
254 parser->buffer[parser->index++] = byte;
255 switch (parser->state) {
257 if (parser->index == sizeof(uint16_t)) {
258 if (head->header == MSG_HEADER) {
261 parser->buffer[0] = parser->buffer[1];
267 if (parser->index == sizeof(header_t)) {
268 if (head->length <= max_length &&
269 head->msgid <= MSG_MAXID) {
278 if (parser->index == (int)sizeof(header_t)+head->length) {
279 serial_deliver(head->msgid, body);
287 /********************
289 ********************/
292 DigitalOut led1(LED1);
293 DigitalOut led2(LED2);
298 parser_t parser_mbed;
314 void task_serial(uint64_t local, uint64_t world)
316 while (sirq_ready(sirq_dbg)) {
317 //sirq_printf("serial recv - dbg\r\n");
318 serial_receive(&parser_dbg, sirq_getc(sirq_dbg));
321 while (sirq_ready(sirq_bbb)) {
322 //sirq_printf("serial recv - bbb\r\n");
323 serial_receive(&parser_bbb, sirq_getc(sirq_bbb));
326 while (sirq_ready(sirq_mbed)) {
327 //sirq_printf("serial recv - mbed\r\n");
328 serial_receive(&parser_mbed, sirq_getc(sirq_mbed));
332 void task_events(uint64_t local, uint64_t world)
337 if (tdma_stamp(tdma_evt, &event)) {
338 sirq_printf("event received - evt\r\n");
339 if (tdma_stamp(tdma_rcv, &event))
340 sirq_printf("event received - rcv\r\n");
341 if (tdma_stamp(tdma_xmt, &event))
342 sirq_printf("event received - xmt\r\n");
345 if (tdma_stamp(tdma_evt, &event))
346 serial_send_event(0, event);
347 tdma_reset(tdma_evt);
350 void task_sync(uint64_t local, uint64_t world)
352 serial_send_sync(sirq_bbb, local);
355 void task_leds(uint64_t local, uint64_t world)
357 static uint32_t which = 0;
363 void task_debug(uint64_t local, uint64_t world)
365 //tdma_debug(tdma_rcv);
366 sirq_printf("background - %6d.%02d -> %d.%02d\r\n",
367 (uint32_t)(local / NSEC_PER_SEC),
368 (uint32_t)(local % NSEC_PER_SEC / 10000000),
369 (uint32_t)(world / NSEC_PER_SEC),
370 (uint32_t)(world % NSEC_PER_SEC / 10000000));
377 #define N_ELEM(x) (sizeof(x) / sizeof((x)[0]))
379 extern void test_main(void);
380 extern serial_t stdio_uart;
383 void (*task)(uint64_t, uint64_t);
387 { task_serial, 0 }, // always
388 { task_events, 1000000000 }, // always
389 { task_sync, 0 }, // always
390 { task_leds, 100000000 }, // 10hz
391 { task_debug, 1000000000 }, // 1hz
394 void background(void)
397 uint64_t local = tdma_time();
398 uint64_t world = time_to_world(local);
401 for (unsigned i = 0; i < N_ELEM(tasks); i++) {
402 if (local >= tasks[i].due) {
403 tasks[i].task(local, world);
404 tasks[i].due += tasks[i].period;
409 int main(int argc, char **argv)
414 sirq_dbg = sirq_open(SIRQ_UART0, USBTX, USBRX, 115200); // to pc
415 sirq_bbb = sirq_open(SIRQ_UART1, PTD3, PTD2, 115200); // to bbb
416 sirq_mbed = sirq_open(SIRQ_UART2, PTE0, PTE1, 115200); // to mbed
420 tdma_evt = tdma_open(TDMA_CHAN0, 3, PTC9, PullDown); // async event
423 tdma_rcv = tdma_open(TDMA_CHAN2, 3, PTD2, PullUp); // time sync rcv
424 tdma_xmt = tdma_open(TDMA_CHAN3, 3, PTD3, PullUp); // time sync xmt
427 //tdma_rcv = tdma_open(TDMA_CHAN2, 2, USBRX, PullUp); // time sync rcv
428 //tdma_xmt = tdma_open(TDMA_CHAN3, 2, USBTX, PullUp); // time sync xmt
430 // Serial timestamping
431 serial_tdma_rcv = tdma_rcv;
432 serial_tdma_xmt = tdma_xmt;
434 // Run background loop