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 * time: drift compensated wall-clock time
35 * stamp: event timestamp from PIT Module
37 uint64_t time_to_world(uint64_t local)
39 uint64_t elapsed = local - time_last_local;
40 return time_last_world + elapsed;
44 * Synchronize the timer internal state with updates
45 * from an external time sync message.
46 * local: our internal timestamp for the event
47 * world: reference timestamp from the other device
49 void time_ext_init(uint64_t local, uint64_t world)
51 sirq_printf("initialize clocks: %d -> %d\r\n",
52 (int)(local/NSEC_PER_SEC),
53 (int)(world/NSEC_PER_SEC));
55 time_last_local = local;
56 time_last_world = world;
60 * Synchronize the timer internal state with updates
61 * from an external time sync message.
62 * local: our internal timestamp for the event
63 * world: reference timestamp from the other device
65 void time_ext_sync(uint64_t local, uint64_t world)
67 uint64_t guess = time_to_world(local);
69 time_last_local = local;
70 time_last_world = (guess/2) + (world/2);
71 //time_last_world = (guess * 3 / 4) + (world * 1 / 4);
73 // (guess - ( guess / 2)) +
74 // (world - (world - world / 2));
76 // (guess - (guess - guess / 4)) +
77 // (world - ( world / 4));
79 world = time_last_world;
83 uint64_t error = world > guess ? world - guess :
84 guess > world ? guess - world : 0;
85 int ahead = guess > world;
86 sirq_printf("syncing clocks: %6d=%d.%04u -> %d.%04u (err: %s%ld.%09lu)\r\n",
87 (int)((local / NSEC_PER_SEC)),
88 (int)((guess / NSEC_PER_SEC)),
89 (int)((guess % NSEC_PER_SEC)/(NSEC_PER_SEC/10000)),
90 (int)((world / NSEC_PER_SEC)),
91 (int)((world % NSEC_PER_SEC)/(NSEC_PER_SEC/10000)),
93 (int32_t )(error / (int64_t)NSEC_PER_SEC),
94 (uint32_t)(error % (int64_t)NSEC_PER_SEC));
99 void time_printf(const char *label, uint64_t local)
101 uint64_t world = time_to_world(local);
102 sirq_printf("%s -- %d.%09u -> %d.%09u\r\n",
104 (int)(local / NSEC_PER_SEC),
105 (int)(local % NSEC_PER_SEC),
106 (int)(world / NSEC_PER_SEC),
107 (int)(world % NSEC_PER_SEC));
110 /*********************
111 * Signal generation *
112 *********************/
114 #define EMIT_CLOCKS(nsec) ((uint16_t)((nsec) * 24 / 1000))
115 #define EMIT_NSEC(clocks) ((uint16_t)((clocks) * 1000 / 24))
117 static uint32_t *emit_pcr = 0; // transmit pin name
119 static uint64_t emit_start = 0; // transmit start time (world time)
120 static uint64_t emit_period = 0; // transmit period
121 static uint64_t emit_due = 0; // next transmit (world time)
123 static uint32_t emit_slack = 0; // how far ahead we need to schedule, in us
124 static uint32_t emit_worst = 0; // worst-case latency in task table
126 void emit_init(int alt, PinName pin, PinMode mode)
129 emit_pcr = (uint32_t*)(PORTA_BASE + pin);
132 SIM->SCGC6 |= SIM_SCGC6_TPM1_MASK;
133 SIM->SOPT2 |= SIM_SOPT2_TPMSRC(1);
136 emit_pcr[0] = PORT_PCR_ISF_MASK
140 // Setup Timer/PWM Module
141 TPM1->SC = TPM_SC_TOF_MASK;
142 TPM1->CNT = TPM_CNT_COUNT(0);
143 TPM1->MOD = TPM_MOD_MOD(0xFFFF);
145 TPM1->CONTROLS[0].CnSC = TPM_CnSC_CHF_MASK // clear flag
146 | TPM_CnSC_MSB_MASK // set output highon match,
147 | TPM_CnSC_ELSB_MASK // cleared on overflow
148 | TPM_CnSC_ELSA_MASK; // ..
150 TPM1->STATUS = TPM_STATUS_CH0F_MASK
151 | TPM_STATUS_TOF_MASK;
153 TPM1->CONF = TPM_CONF_CSOO_MASK;
156 void emit_enable(uint64_t start, uint64_t period)
158 const int slack_clocks = 0x8000; // tune based on emit_worst
161 emit_period = period;
162 emit_due = start + period;
164 // TODO - tune slack time
165 // TODO - check clock power
166 // TODO - TPM clock source
167 emit_slack = EMIT_NSEC(slack_clocks);
169 time_printf("emit scheduled", emit_due);
172 void emit_schedule(uint64_t when)
174 uint64_t now = time_to_world(tdma_time());
175 uint64_t start = when - now; // transmit time
176 uint64_t stop = start + 100000; // 100 us pulse
179 TPM1->SC = TPM_SC_TOF_MASK;
182 TPM1->CONTROLS[0].CnV = EMIT_CLOCKS(start);
183 TPM1->MOD = TPM_MOD_MOD(EMIT_CLOCKS(stop));
186 TPM1->SC = TPM_SC_TOF_MASK
191 //sirq_printf("emitting event\r\n");
194 void emit_transmit(uint64_t local, uint64_t world)
196 static uint64_t prev = 0;
198 // Record how how much time we have to reschedule
199 if (prev && (local-prev) > emit_worst)
200 emit_worst = (local-prev);
203 // Schedule task if needed
204 if (emit_due && emit_period &&
205 world+emit_slack > emit_due) {
206 emit_schedule(emit_due);
207 emit_due += emit_period;
211 /************************
212 * Serial I/O functions *
213 ************************/
221 static uint32_t serial_device_id = 0;
223 const uint64_t serial_sync_delay = NSEC_PER_SEC / 100;
224 static uint64_t serial_sync_due = 0;
226 static tdma_t *serial_tdma_rcv = NULL;
227 static tdma_t *serial_tdma_xmt = NULL;
230 * Convert world to local time
232 uint64_t serial_read_time(ntime_t time)
234 return ((uint64_t)time.seconds) * NSEC_PER_SEC
235 + ((uint64_t)time.nanosec);
238 ntime_t serial_write_time(uint64_t time)
241 buf.seconds = time / NSEC_PER_SEC;
242 buf.nanosec = time % NSEC_PER_SEC;
246 int serial_time_stamp(tdma_t *port, uint64_t *local, uint64_t *world,
249 int valid = tdma_stamp(port, local);
250 *world = time_to_world(*local);
253 sirq_printf("%s -- missing\r\n", msg);
255 // time_printf(msg, current);
261 * Output initialization message init message
263 void serial_send_init(uint16_t device, uint64_t local)
268 * Output time sync message
270 void serial_send_sync(sirq_t *port, uint64_t now)
272 if (serial_sync_due == 0 || now < serial_sync_due)
280 head.header = MSG_HEADER;
281 head.msgid = MSG_ID_SYNC;
282 head.length = sizeof(body);
283 head.cksum = 0; // todo
285 tdma_stop(serial_tdma_rcv, 0);
286 tdma_start(serial_tdma_xmt);
288 sirq_write(port, &head, sizeof(head));
290 tdma_stop(serial_tdma_xmt, 100);
291 tdma_start(serial_tdma_rcv);
293 // Save transmit time
294 uint64_t local = 0, world = 0;
295 serial_time_stamp(serial_tdma_xmt, &local, &world,
296 "sync time transmit");
299 //sirq_printf("sync time transmit\r\n");
300 //time_printf(" local", local);
301 //time_printf(" world", world);
303 // Write body with updated time and send
304 body.time = serial_write_time(world);
306 sirq_write(port, &body, sizeof(body));
308 // Queue next transmit time
313 * Output external event received message
314 * event: id of the received event
315 * time: compensated timestamp of the event
317 void serial_send_event(sirq_t *port, uint16_t event, uint64_t local)
319 time_printf("event received", local);
322 uint64_t world = time_to_world(local);
323 ntime_t ltime = serial_write_time(local);
324 ntime_t wtime = serial_write_time(world);
328 event_msg_t body = {};
330 // Transmit sync message
331 head.header = MSG_HEADER;
332 head.msgid = MSG_ID_EVENT;
333 head.length = sizeof(body);
334 head.cksum = 0; // todo
336 body.device = serial_device_id;
341 // Transmit message to BBB
342 sirq_write(port, &head, sizeof(head));
343 sirq_write(port, &body, sizeof(body));
347 * Handle init message
349 void serial_handle_init(init_msg_t *msg)
351 sirq_printf("initialize: %s %s %s %s %s\r\n",
352 msg->valid & MSG_VALID_DEVICE ? "DEV" : "dev",
353 msg->valid & MSG_VALID_START ? "START" : "start",
354 msg->valid & MSG_VALID_PERIOD ? "PERIOD" : "period",
355 msg->valid & MSG_VALID_WORLD ? "WORLD" : "world",
356 msg->valid & MSG_VALID_SYNC ? "SYNC" : "sync");
357 sirq_printf(" dev -- %d\r\n", msg->device);
358 time_printf(" start ", serial_read_time(msg->start));
359 time_printf(" period", serial_read_time(msg->period));
360 time_printf(" world ", serial_read_time(msg->world));
362 if (msg->valid & MSG_VALID_DEVICE)
363 serial_device_id = msg->device;
365 if (msg->valid & MSG_VALID_START ||
366 msg->valid & MSG_VALID_PERIOD) {
367 uint64_t start = serial_read_time(msg->start);
368 uint64_t period = serial_read_time(msg->period);
369 emit_enable(start, period);
372 if (msg->valid & MSG_VALID_WORLD) {
373 uint64_t world = serial_read_time(msg->world);
374 uint64_t local = tdma_time();
375 time_ext_init(local, world);
378 if (msg->valid & MSG_VALID_SYNC)
379 serial_sync_due = tdma_time() + serial_sync_delay;
383 * Handle sync message
385 void serial_handle_sync(sync_msg_t *msg)
387 // Read receive timestamp
388 uint64_t local = 0, world = 0;
389 serial_time_stamp(serial_tdma_rcv, &local, &world,
390 "sync time receive ");
391 tdma_stop(serial_tdma_rcv, 0);
393 // Lookup reference time from message
394 uint64_t reference = serial_read_time(msg->time);
397 //sirq_printf("sync time receive\r\n");
398 //time_printf(" local", local);
399 //time_printf(" world", world);
400 //time_printf(" ref ", reference);
402 // Synchronize the clocks
403 time_ext_sync(local, reference);
405 // Queue transmit to other board
406 serial_sync_due = tdma_time() + serial_sync_delay;
410 * Handle event message
412 void serial_handle_event(event_msg_t *msg)
419 void serial_deliver(int msgid, void *body)
423 //sirq_printf("received init msg\r\n");
424 serial_handle_init((init_msg_t*)body);
427 //sirq_printf("received sync msg\r\n");
428 serial_handle_sync((sync_msg_t*)body);
431 //sirq_printf("received event msg\r\n");
432 serial_handle_event((event_msg_t*)body);
438 * Process serial receive messages
440 void serial_receive(parser_t *parser, int byte)
442 //sirq_printf("serial_receive - %02x\r\n", byte);
445 header_t *head = (header_t*)parser->buffer;
446 void *body = (void*)(head+1);
447 const int max_length = sizeof(parser->buffer)-sizeof(header_t);
449 // Process uart messages
450 parser->buffer[parser->index++] = byte;
451 switch (parser->state) {
453 if (parser->index == sizeof(uint16_t)) {
454 if (head->header == MSG_HEADER) {
457 parser->buffer[0] = parser->buffer[1];
463 if (parser->index == sizeof(header_t)) {
464 if (head->length <= max_length &&
465 head->msgid <= MSG_MAX_ID) {
474 if (parser->index == (int)sizeof(header_t)+head->length) {
475 serial_deliver(head->msgid, body);
483 /********************
485 ********************/
488 DigitalOut led1(LED1);
489 DigitalOut led2(LED2);
494 parser_t parser_mbed;
510 void task_serial(uint64_t local, uint64_t world)
512 while (sirq_ready(sirq_dbg)) {
513 //sirq_printf("serial recv - dbg\r\n");
514 serial_receive(&parser_dbg, sirq_getc(sirq_dbg));
517 while (sirq_ready(sirq_bbb)) {
518 //sirq_printf("serial recv - bbb\r\n");
519 serial_receive(&parser_bbb, sirq_getc(sirq_bbb));
522 while (sirq_ready(sirq_mbed)) {
523 //sirq_printf("serial recv - mbed\r\n");
524 serial_receive(&parser_mbed, sirq_getc(sirq_mbed));
528 void task_events(uint64_t local, uint64_t world)
533 if (tdma_stamp(tdma_evt, &event)) {
534 sirq_printf("event received - evt\r\n");
535 if (tdma_stamp(tdma_rcv, &event))
536 sirq_printf("event received - rcv\r\n");
537 if (tdma_stamp(tdma_xmt, &event))
538 sirq_printf("event received - xmt\r\n");
541 if (tdma_stamp(tdma_evt, &event))
542 serial_send_event(sirq_bbb, 0, event);
543 tdma_stop(tdma_evt, 0);
544 tdma_start(tdma_evt);
547 void task_sync(uint64_t local, uint64_t world)
549 serial_send_sync(sirq_mbed, local);
552 void task_leds(uint64_t local, uint64_t world)
554 static uint32_t which = 0;
560 void task_emit(uint64_t local, uint64_t world)
562 emit_transmit(local, world);
565 void task_debug(uint64_t local, uint64_t world)
567 //tdma_debug(tdma_rcv);
568 //tdma_debug(tdma_xmt);
570 //sirq_debug(sirq_mbed);
573 sirq_printf("background - %6u.%02u -> %u.%02u\r\n",
574 (uint32_t)(local / NSEC_PER_SEC),
575 (uint32_t)(local % NSEC_PER_SEC / 10000000),
576 (uint32_t)(world / NSEC_PER_SEC),
577 (uint32_t)(world % NSEC_PER_SEC / 10000000));
585 #define N_ELEM(x) (sizeof(x) / sizeof((x)[0]))
587 extern void test_main(void);
588 extern serial_t stdio_uart;
591 void (*task)(uint64_t, uint64_t);
595 { task_serial, 0 }, // always
596 { task_events, 0 }, // always -- testing
597 { task_sync, 0 }, // always
598 { task_emit, 0 }, // always
599 { task_leds, 100000000 }, // 10hz
600 { task_debug, 1000000000 }, // 1hz
603 void background(void)
606 uint64_t local = tdma_time();
607 uint64_t world = time_to_world(local);
610 for (unsigned i = 0; i < N_ELEM(tasks); i++) {
611 if (local >= tasks[i].due) {
612 tasks[i].task(local, world);
613 tasks[i].due += tasks[i].period;
618 int main(int argc, char **argv)
621 emit_init(3, PTE20, PullDown);
626 sirq_dbg = sirq_open(SIRQ_UART0, USBTX, USBRX, 115200); // to pc
627 sirq_bbb = sirq_open(SIRQ_UART1, PTE0, PTE1, 115200); // to bbb
628 sirq_mbed = sirq_open(SIRQ_UART2, PTD3, PTD2, 115200); // to mbed
631 tdma_evt = tdma_open(TDMA_CHAN0, 3, PTC9, PullDown); // async event
634 tdma_rcv = tdma_open(TDMA_CHAN2, 3, PTD2, PullUp); // time sync rcv
635 tdma_xmt = tdma_open(TDMA_CHAN3, 3, PTD3, PullUp); // time sync xmt
638 //tdma_rcv = tdma_open(TDMA_CHAN2, 2, USBRX, PullUp); // time sync rcv
639 //tdma_xmt = tdma_open(TDMA_CHAN3, 2, USBTX, PullUp); // time sync xmt
642 tdma_start(tdma_evt);
643 tdma_start(tdma_rcv);
644 tdma_start(tdma_xmt);
646 // Serial timestamping
647 serial_tdma_rcv = tdma_rcv;
648 serial_tdma_xmt = tdma_xmt;
651 //MCG->C1 = 0x05; // was 0x1A
652 //MCG->C2 = 0x2C; // was 0x24
653 //MCG->C3 = 0x91; // was 0x91
654 //MCG->C4 = 0x10; // was 0x10
655 //MCG->C5 = 0x01; // was 0x01
656 //MCG->C6 = 0x40; // was 0x40
657 //MCG->S = 0x6E; // was 0x6E
658 //MCG->SC = 0x02; // was 0x02
659 //MCG->ATCVH = 0x00; // was 0x00
660 //MCG->ATCVL = 0x00; // was 0x00
661 //MCG->C7 = 0x00; // was 0x00
662 //MCG->C8 = 0x80; // was 0x80
663 //MCG->C9 = 0x00; // was 0x00
664 //MCG->C10 = 0x00; // was 0x00
666 //sirq_printf("MGC - C1 %02hx\r\n", MCG->C1); // 1A
667 //sirq_printf("MGC - C2 %02hx\r\n", MCG->C2); // 24
668 //sirq_printf("MGC - C3 %02hx\r\n", MCG->C3); // 91
669 //sirq_printf("MGC - C4 %02hx\r\n", MCG->C4); // 10
670 //sirq_printf("MGC - C5 %02hx\r\n", MCG->C5); // 01
671 //sirq_printf("MGC - C6 %02hx\r\n", MCG->C6); // 40
672 //sirq_printf("MGC - S %02hx\r\n", MCG->S); // 6E
673 //sirq_printf("MGC - SC %02hx\r\n", MCG->SC); // 02
674 //sirq_printf("MGC - ATCVH %02hx\r\n", MCG->ATCVH); // 00
675 //sirq_printf("MGC - ATCVL %02hx\r\n", MCG->ATCVL); // 00
676 //sirq_printf("MGC - C7 %02hx\r\n", MCG->C7); // 00
677 //sirq_printf("MGC - C8 %02hx\r\n", MCG->C8); // 80
678 //sirq_printf("MGC - C9 %02hx\r\n", MCG->C9); // 00
679 //sirq_printf("MGC - C10 %02hx\r\n", MCG->C10); // 00
681 // Run background loop
685 // Performance testing
686 //uint64_t prev = 0, due = 0;
687 //uint64_t worst[10] = {};
690 // uint64_t local = tdma_time();
691 // if (prev && (local-prev) > worst[count])
692 // worst[count] = (local-prev);
694 // if (local > due) {
696 // static char str[] = "background background background\r\n";
697 // sirq_write(sirq_dbg, str, sizeof(str));
700 // sirq_printf("background\r\n");
701 // for (int i = 0; i < 10; i++) {
702 // sirq_printf(" worst[%d] = 0.%09u\r\n",
707 // due += NSEC_PER_SEC;
708 // count = (count + 1) % 10;