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 uint32_t test_xmt_enab = 0;
28 uint64_t test_xmt_time0 = 0;
29 uint64_t test_xmt_time1 = 0;
31 uint32_t test_rcv_enab = 0;
32 uint64_t test_rcv_time = 0;
38 #define NSEC_PER_SEC 1000000000ULL
40 uint64_t time_last_local; // timestamp at last time sync
41 uint64_t time_last_world; // offset at last time sync
44 * Generate time stamp for an async event:
45 * local: drift compensated wall-clock time
46 * world: nanoseconds in world time world
47 * valid: local timestamp at valid valid
49 //uint64_t time_to_local(uint64_t world, uint64_t valid)
56 * Generate time stamp for an async event:
57 * time: drift compensated wall-clock time
58 * stamp: event timestamp from PIT Module
60 uint64_t time_to_world(uint64_t local)
62 uint64_t elapsed = local - time_last_local;
63 return time_last_world + elapsed;
67 * Compensate the Real-Time-Clock oscillator for
68 * temperature and drift errors. Called at 1Hz and
69 * synchronous to the RTC 1Hz output.
71 void time_rtc_comp(void)
77 * Synchronize the timer internal state with updates
78 * from an external time sync message.
79 * local: our internal timestamp for the event
80 * world: reference timestamp from the other device
82 void time_ext_init(uint64_t local, uint64_t world)
84 sirq_printf("initialize clocks: %d -> %d\r\n",
85 (int)(local/NSEC_PER_SEC),
86 (int)(world/NSEC_PER_SEC));
88 time_last_local = local;
89 time_last_world = world;
93 * Synchronize the timer internal state with updates
94 * from an external time sync message.
95 * local: our internal timestamp for the event
96 * world: reference timestamp from the other device
98 void time_ext_sync(uint64_t local, uint64_t world)
100 uint64_t guess = time_to_world(local);
101 uint64_t error = world > guess ? world - guess :
102 guess > world ? guess - world : 0;
103 int ahead = guess > world;
105 time_last_local = local;
106 time_last_world = (guess/2) + (world/2);
107 //time_last_world = (guess * 3 / 4) + (world * 1 / 4);
109 // (guess - ( guess / 2)) +
110 // (world - (world - world / 2));
112 // (guess - (guess - guess / 4)) +
113 // (world - ( world / 4));
115 world = time_last_world;
119 sirq_printf("syncing clocks: %6d=%d.%04u -> %d.%04u (err: %s%ld.%09lu)\r\n",
120 (int)((local / NSEC_PER_SEC)),
121 (int)((guess / NSEC_PER_SEC)),
122 (int)((guess % NSEC_PER_SEC)/(NSEC_PER_SEC/10000)),
123 (int)((world / NSEC_PER_SEC)),
124 (int)((world % NSEC_PER_SEC)/(NSEC_PER_SEC/10000)),
126 (int32_t )(error / (int64_t)NSEC_PER_SEC),
127 (uint32_t)(error % (int64_t)NSEC_PER_SEC));
142 void time_printf(const char *label, uint64_t local)
144 uint64_t world = time_to_world(local);
145 sirq_printf("%s -- %d.%09u -> %d.%09u\r\n",
147 (int)(local / NSEC_PER_SEC),
148 (int)(local % NSEC_PER_SEC),
149 (int)(world / NSEC_PER_SEC),
150 (int)(world % NSEC_PER_SEC));
153 /************************
154 * Serial I/O functions *
155 ************************/
163 const uint64_t serial_sync_delay = NSEC_PER_SEC / 100; // 1hz
164 static uint64_t serial_sync_due = 0;
166 static tdma_t *serial_tdma_rcv = NULL;
167 static tdma_t *serial_tdma_xmt = NULL;
169 static uint64_t serial_prev_local = 0;
170 static uint64_t serial_prev_seq = 0;
172 static uint64_t serial_xmt_local = 0;
173 static uint64_t serial_xmt_seq = 0;
176 * Output time sync message
178 void serial_send_sync(sirq_t *port, uint64_t now)
180 if (serial_sync_due == 0 || now < serial_sync_due)
183 //sirq_printf("sending sync\r\n");
185 // Calculate world time
187 uint64_t world = time_to_world(serial_xmt_local);
193 // Transmit sync message
194 head.header = MSG_HEADER;
195 head.msgid = MSG_ID_SYNC;
196 head.length = sizeof(body);
197 head.cksum = 0; // todo
199 body.seq = serial_xmt_seq;
200 body.time.seconds = world / NSEC_PER_SEC;
201 body.time.nanosec = world % NSEC_PER_SEC;
203 tdma_stop(serial_tdma_rcv);
209 tdma_start(serial_tdma_xmt);
210 sirq_write(port, &head, sizeof(head));
211 sirq_write(port, &body, sizeof(body));
212 tdma_stop(serial_tdma_xmt);
214 // save transmit time
215 //local = test_xmt_time1;
216 int valid = tdma_stamp(serial_tdma_xmt, &local);
218 sirq_printf("sync transmit time -- missed\r\n");
220 //time_printf("sync transmit time ", local);
221 //time_printf("sync transmit test0", test_xmt_time0);
222 //time_printf("sync transmit test1", test_xmt_time1);
225 tdma_start(serial_tdma_rcv);
229 serial_xmt_local = local;
233 * Output external event received message
234 * event: id of the received event
235 * time: compensated timestamp of the event
237 void serial_send_event(uint16_t event, uint64_t local)
239 uint64_t world = time_to_world(local);
242 time.seconds = (uint32_t)(world / NSEC_PER_SEC);
243 time.nanosec = (uint32_t)(world % NSEC_PER_SEC);
245 sirq_printf("event received - %08x:%08x - %u.%09u\r\n",
246 (uint32_t)(local >> 32), (uint32_t)local,
247 time.seconds, time.nanosec);
252 * Handle sync message
254 void serial_handle_sync(sync_msg_t *msg)
256 // Read receive timestamp for next time sync message
257 uint64_t current = 0;
258 int valid = tdma_stamp(serial_tdma_rcv, ¤t);
260 sirq_printf("sync receive time -- missing\r\n");
262 // time_printf("sync receive time ", current);
263 tdma_stop(serial_tdma_rcv);
266 uint64_t world = ((uint64_t)msg->time.seconds) * NSEC_PER_SEC
267 + ((uint64_t)msg->time.nanosec);
271 uint64_t local = tdma_time();
272 time_ext_init(local, world);
275 // Valid times timestamp
276 if (serial_prev_seq == (msg->seq-1)) {
277 uint64_t local = serial_prev_local;
278 time_ext_sync(local, world);
281 // Queue transmit to other board
282 serial_sync_due = tdma_time() + serial_sync_delay;
285 serial_prev_local = current;
286 serial_prev_seq = msg->seq;
290 * Handle event message
292 void serial_handle_event(event_msg_t *msg)
299 void serial_deliver(int msgid, void *body)
303 //sirq_printf("received sync msg\r\n");
304 serial_handle_sync((sync_msg_t*)body);
307 //sirq_printf("received event msg\r\n");
308 serial_handle_event((event_msg_t*)body);
314 * Process serial receive messages
316 void serial_receive(parser_t *parser, int byte)
318 //sirq_printf("serial_receive - %02x\r\n", byte);
321 header_t *head = (header_t*)parser->buffer;
322 void *body = (void*)(head+1);
323 const int max_length = sizeof(parser->buffer)-sizeof(header_t);
325 // Process uart messages
326 parser->buffer[parser->index++] = byte;
327 switch (parser->state) {
329 if (parser->index == sizeof(uint16_t)) {
330 if (head->header == MSG_HEADER) {
333 parser->buffer[0] = parser->buffer[1];
339 if (parser->index == sizeof(header_t)) {
340 if (head->length <= max_length &&
341 head->msgid <= MSG_MAXID) {
350 if (parser->index == (int)sizeof(header_t)+head->length) {
351 serial_deliver(head->msgid, body);
359 /********************
361 ********************/
364 DigitalOut led1(LED1);
365 DigitalOut led2(LED2);
370 parser_t parser_mbed;
386 void task_serial(uint64_t local, uint64_t world)
388 while (sirq_ready(sirq_dbg)) {
389 //sirq_printf("serial recv - dbg\r\n");
390 serial_receive(&parser_dbg, sirq_getc(sirq_dbg));
393 while (sirq_ready(sirq_bbb)) {
394 //sirq_printf("serial recv - bbb\r\n");
395 serial_receive(&parser_bbb, sirq_getc(sirq_bbb));
398 while (sirq_ready(sirq_mbed)) {
399 //sirq_printf("serial recv - mbed\r\n");
400 serial_receive(&parser_mbed, sirq_getc(sirq_mbed));
404 void task_events(uint64_t local, uint64_t world)
409 if (tdma_stamp(tdma_evt, &event)) {
410 sirq_printf("event received - evt\r\n");
411 if (tdma_stamp(tdma_rcv, &event))
412 sirq_printf("event received - rcv\r\n");
413 if (tdma_stamp(tdma_xmt, &event))
414 sirq_printf("event received - xmt\r\n");
417 if (tdma_stamp(tdma_evt, &event))
418 serial_send_event(0, event);
420 tdma_start(tdma_evt);
423 void task_sync(uint64_t local, uint64_t world)
425 serial_send_sync(sirq_mbed, local);
428 void task_leds(uint64_t local, uint64_t world)
430 static uint32_t which = 0;
436 void task_debug(uint64_t local, uint64_t world)
438 //tdma_debug(tdma_rcv);
439 //tdma_debug(tdma_xmt);
441 //sirq_debug(sirq_mbed);
444 sirq_printf("background - %6u.%02u -> %u.%02u\r\n",
445 (uint32_t)(local / NSEC_PER_SEC),
446 (uint32_t)(local % NSEC_PER_SEC / 10000000),
447 (uint32_t)(world / NSEC_PER_SEC),
448 (uint32_t)(world % NSEC_PER_SEC / 10000000));
456 #define N_ELEM(x) (sizeof(x) / sizeof((x)[0]))
458 extern void test_main(void);
459 extern serial_t stdio_uart;
462 void (*task)(uint64_t, uint64_t);
466 { task_serial, 0 }, // always
467 { task_events, 1000000000 }, // always
468 { task_sync, 0 }, // always
469 { task_leds, 100000000 }, // 10hz
470 { task_debug, 1000000000 }, // 1hz
473 void background(void)
476 uint64_t local = tdma_time();
477 uint64_t world = time_to_world(local);
480 for (unsigned i = 0; i < N_ELEM(tasks); i++) {
481 if (local >= tasks[i].due) {
482 tasks[i].task(local, world);
483 tasks[i].due += tasks[i].period;
488 int main(int argc, char **argv)
493 sirq_dbg = sirq_open(SIRQ_UART0, USBTX, USBRX, 115200); // to pc
494 sirq_bbb = sirq_open(SIRQ_UART1, PTE0, PTE1, 115200); // to bbb
495 sirq_mbed = sirq_open(SIRQ_UART2, PTD3, PTD2, 115200); // to mbed
498 tdma_evt = tdma_open(TDMA_CHAN0, 3, PTC9, PullUp); // async event
501 tdma_rcv = tdma_open(TDMA_CHAN2, 3, PTD2, PullUp); // time sync rcv
502 tdma_xmt = tdma_open(TDMA_CHAN3, 3, PTD3, PullUp); // time sync xmt
505 //tdma_rcv = tdma_open(TDMA_CHAN2, 2, USBRX, PullUp); // time sync rcv
506 //tdma_xmt = tdma_open(TDMA_CHAN3, 2, USBTX, PullUp); // time sync xmt
509 tdma_start(tdma_evt);
510 tdma_start(tdma_rcv);
511 tdma_start(tdma_xmt);
513 // Serial timestamping
514 serial_tdma_rcv = tdma_rcv;
515 serial_tdma_xmt = tdma_xmt;
518 //MCG->C1 = 0x05; // was 0x1A
519 //MCG->C2 = 0x2C; // was 0x24
520 //MCG->C3 = 0x91; // was 0x91
521 //MCG->C4 = 0x10; // was 0x10
522 //MCG->C5 = 0x01; // was 0x01
523 //MCG->C6 = 0x40; // was 0x40
524 //MCG->S = 0x6E; // was 0x6E
525 //MCG->SC = 0x02; // was 0x02
526 //MCG->ATCVH = 0x00; // was 0x00
527 //MCG->ATCVL = 0x00; // was 0x00
528 //MCG->C7 = 0x00; // was 0x00
529 //MCG->C8 = 0x80; // was 0x80
530 //MCG->C9 = 0x00; // was 0x00
531 //MCG->C10 = 0x00; // was 0x00
533 //sirq_printf("MGC - C1 %02hx\r\n", MCG->C1); // 1A
534 //sirq_printf("MGC - C2 %02hx\r\n", MCG->C2); // 24
535 //sirq_printf("MGC - C3 %02hx\r\n", MCG->C3); // 91
536 //sirq_printf("MGC - C4 %02hx\r\n", MCG->C4); // 10
537 //sirq_printf("MGC - C5 %02hx\r\n", MCG->C5); // 01
538 //sirq_printf("MGC - C6 %02hx\r\n", MCG->C6); // 40
539 //sirq_printf("MGC - S %02hx\r\n", MCG->S); // 6E
540 //sirq_printf("MGC - SC %02hx\r\n", MCG->SC); // 02
541 //sirq_printf("MGC - ATCVH %02hx\r\n", MCG->ATCVH); // 00
542 //sirq_printf("MGC - ATCVL %02hx\r\n", MCG->ATCVL); // 00
543 //sirq_printf("MGC - C7 %02hx\r\n", MCG->C7); // 00
544 //sirq_printf("MGC - C8 %02hx\r\n", MCG->C8); // 80
545 //sirq_printf("MGC - C9 %02hx\r\n", MCG->C9); // 00
546 //sirq_printf("MGC - C10 %02hx\r\n", MCG->C10); // 00
548 // Run background loop