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 uint64_t guess = time_to_world(local);
91 time_last_local = local;
92 time_last_world = (guess/2) + (world/2);
93 //time_last_world = (guess * 3 / 4) + (world * 1 / 4);
95 // (guess - ( guess / 2)) +
96 // (world - (world - world / 2));
98 // (guess - (guess - guess / 4)) +
99 // (world - ( world / 4));
101 world = time_last_world;
105 uint64_t error = world > guess ? world - guess :
106 guess > world ? guess - world : 0;
107 int ahead = guess > world;
108 sirq_printf("syncing clocks: %6d=%d.%04u -> %d.%04u (err: %s%ld.%09lu)\r\n",
109 (int)((local / NSEC_PER_SEC)),
110 (int)((guess / NSEC_PER_SEC)),
111 (int)((guess % NSEC_PER_SEC)/(NSEC_PER_SEC/10000)),
112 (int)((world / NSEC_PER_SEC)),
113 (int)((world % NSEC_PER_SEC)/(NSEC_PER_SEC/10000)),
115 (int32_t )(error / (int64_t)NSEC_PER_SEC),
116 (uint32_t)(error % (int64_t)NSEC_PER_SEC));
121 void time_printf(const char *label, uint64_t local)
123 uint64_t world = time_to_world(local);
124 sirq_printf("%s -- %d.%09u -> %d.%09u\r\n",
126 (int)(local / NSEC_PER_SEC),
127 (int)(local % NSEC_PER_SEC),
128 (int)(world / NSEC_PER_SEC),
129 (int)(world % NSEC_PER_SEC));
132 /*********************
133 * Signal generation *
134 *********************/
136 static uint32_t *emit_pcr = 0; // transmit pin name
138 static uint64_t emit_start = 0; // transmit start time (world time)
139 static uint64_t emit_period = 0; // transmit period
140 static uint64_t emit_due = 0; // next transmit (world time)
142 static uint32_t emit_slack = 0; // how far ahead we need to schedule, in us
143 static uint32_t emit_worst = 0; // worst-case latency in task table
145 void emit_init(int alt, PinName pin, PinMode mode)
148 emit_pcr = (uint32_t*)(PORTA_BASE + pin);
151 SIM->SCGC6 |= SIM_SCGC6_TPM0_MASK;
153 SIM->SOPT2 |= SIM_SOPT2_TPMSRC(1);
154 SIM->SOPT4 |= SIM_SOPT4_TPM1CLKSEL_MASK;
157 emit_pcr[0] = PORT_PCR_ISF_MASK
161 // Setup Timer/PWM Module
162 TPM0->SC = TPM_SC_TOF_MASK
163 | TPM_SC_PS(1); // 24 MHz clock ?
164 TPM0->CNT = TPM_CNT_COUNT(0);
165 TPM0->MOD = TPM_MOD_MOD(0xFFFF);
167 TPM0->CONTROLS[0].CnSC = TPM_CnSC_CHF_MASK // clear flag
168 | TPM_CnSC_MSB_MASK // pulse output on match
169 | TPM_CnSC_MSA_MASK // ..
170 | TPM_CnSC_ELSA_MASK; // pulse high
172 TPM0->CONTROLS[0].CnV = 0xFFFF; // time delay
174 TPM0->STATUS = TPM_STATUS_CH0F_MASK
175 | TPM_STATUS_TOF_MASK;
177 TPM0->CONF = TPM_CONF_CSOO_MASK;
180 void emit_enable(uint64_t start, uint64_t period)
182 const int slack_tick = 0xC000; // tune based on emit_worst
185 emit_period = period;
186 emit_due = start + period;
188 emit_slack = slack_tick * 1000 / 24;
190 time_printf("emit scheduled", emit_due);
193 void emit_schedule(uint64_t when)
195 uint64_t now = time_to_world(tdma_time());
196 uint16_t delay = (uint16_t)(when-now);
198 // Clear pending flags
199 //emit_pcr[0] |= PORT_PCR_ISF_MASK
202 TPM0->SC = TPM_SC_TOF_MASK;
205 TPM0->CNT = TPM_CNT_COUNT(0);
206 TPM0->CONTROLS[0].CnV = delay;
209 TPM0->SC = TPM_SC_TOF_MASK
213 sirq_printf("emitting event\r\n");
216 void emit_transmit(uint64_t local, uint64_t world)
218 static uint64_t prev = 0;
220 // Record how how much time we have to reschedule
221 if (prev && (local-prev) > emit_worst)
222 emit_worst = (local-prev);
225 // Schedule task if needed
226 if (emit_due && emit_period &&
227 world+emit_slack > emit_due) {
228 emit_schedule(emit_due);
229 emit_due += emit_period;
233 /************************
234 * Serial I/O functions *
235 ************************/
243 static uint32_t serial_device_id = 0;
245 const uint64_t serial_sync_delay = NSEC_PER_SEC / 100; // 1hz
246 static uint64_t serial_sync_due = 0;
248 static tdma_t *serial_tdma_rcv = NULL;
249 static tdma_t *serial_tdma_xmt = NULL;
251 static uint64_t serial_prev_local = 0;
252 static uint64_t serial_prev_seq = 0;
254 static uint64_t serial_xmt_local = 0;
255 static uint64_t serial_xmt_seq = 0;
258 * Convert world to local time
260 uint64_t serial_read_time(ntime_t time)
262 return ((uint64_t)time.seconds) * NSEC_PER_SEC
263 + ((uint64_t)time.nanosec);
266 ntime_t serial_write_time(uint64_t time)
269 buf.seconds = time / NSEC_PER_SEC;
270 buf.nanosec = time % NSEC_PER_SEC;
275 * Output initialization message init message
277 void serial_send_init(uint16_t device, uint64_t local)
282 * Output time sync message
284 void serial_send_sync(sirq_t *port, uint64_t now)
286 if (serial_sync_due == 0 || now < serial_sync_due)
289 //sirq_printf("sending sync\r\n");
291 // Calculate world time
293 uint64_t world = time_to_world(serial_xmt_local);
299 // Transmit sync message
300 head.header = MSG_HEADER;
301 head.msgid = MSG_ID_SYNC;
302 head.length = sizeof(body);
303 head.cksum = 0; // todo
305 body.seq = serial_xmt_seq;
306 body.time.seconds = world / NSEC_PER_SEC;
307 body.time.nanosec = world % NSEC_PER_SEC;
309 tdma_stop(serial_tdma_rcv);
311 tdma_start(serial_tdma_xmt);
312 sirq_write(port, &head, sizeof(head));
313 sirq_write(port, &body, sizeof(body));
314 tdma_stop(serial_tdma_xmt);
316 // save transmit time
317 int valid = tdma_stamp(serial_tdma_xmt, &local);
319 sirq_printf("sync transmit time -- missed\r\n");
321 //time_printf("sync transmit time ", local);
323 tdma_start(serial_tdma_rcv);
327 serial_xmt_local = local;
331 * Output external event received message
332 * event: id of the received event
333 * time: compensated timestamp of the event
335 void serial_send_event(uint16_t event, uint64_t local)
337 time_printf("event received", local);
344 uint64_t world = time_to_world(local);
347 time.seconds = (uint32_t)(world / NSEC_PER_SEC);
348 time.nanosec = (uint32_t)(world % NSEC_PER_SEC);
350 // Transmit sync message
351 head.header = MSG_HEADER;
352 head.msgid = MSG_ID_SYNC;
353 head.length = sizeof(body);
354 head.cksum = 0; // todo
356 body.seq = serial_xmt_seq;
357 body.time.seconds = world / NSEC_PER_SEC;
358 body.time.nanosec = world % NSEC_PER_SEC;
360 tdma_stop(serial_tdma_rcv);
362 tdma_start(serial_tdma_xmt);
363 sirq_write(port, &head, sizeof(head));
364 sirq_write(port, &body, sizeof(body));
365 tdma_stop(serial_tdma_xmt);
370 * Handle init message
372 void serial_handle_init(init_msg_t *msg)
374 sirq_printf("initialize: %s %s %s %s %s\r\n",
375 msg->valid & MSG_VALID_DEVICE ? "DEV" : "dev",
376 msg->valid & MSG_VALID_START ? "START" : "start",
377 msg->valid & MSG_VALID_PERIOD ? "PERIOD" : "period",
378 msg->valid & MSG_VALID_WORLD ? "WORLD" : "world",
379 msg->valid & MSG_VALID_SYNC ? "SYNC" : "sync");
380 sirq_printf(" dev -- %d\r\n", msg->device);
381 time_printf(" start ", serial_read_time(msg->start));
382 time_printf(" period", serial_read_time(msg->period));
383 time_printf(" world ", serial_read_time(msg->world));
385 if (msg->valid & MSG_VALID_DEVICE)
386 serial_device_id = msg->device;
388 if (msg->valid & MSG_VALID_START ||
389 msg->valid & MSG_VALID_PERIOD) {
390 uint64_t start = serial_read_time(msg->start);
391 uint64_t period = serial_read_time(msg->period);
392 emit_enable(start, period);
395 if (msg->valid & MSG_VALID_WORLD) {
396 uint64_t world = serial_read_time(msg->world);
397 uint64_t local = tdma_time();
398 time_ext_init(local, world);
401 if (msg->valid & MSG_VALID_SYNC)
402 serial_sync_due = tdma_time() + serial_sync_delay;
406 * Handle sync message
408 void serial_handle_sync(sync_msg_t *msg)
410 // Read receive timestamp for next time sync message
411 uint64_t current = 0;
412 int valid = tdma_stamp(serial_tdma_rcv, ¤t);
414 sirq_printf("sync receive time -- missing\r\n");
416 // time_printf("sync receive time ", current);
417 tdma_stop(serial_tdma_rcv);
420 uint64_t world = ((uint64_t)msg->time.seconds) * NSEC_PER_SEC
421 + ((uint64_t)msg->time.nanosec);
423 // Valid times timestamp
424 if (serial_prev_seq == (msg->seq-1)) {
425 uint64_t local = serial_prev_local;
426 time_ext_sync(local, world);
429 // Queue transmit to other board
430 serial_sync_due = tdma_time() + serial_sync_delay;
433 serial_prev_local = current;
434 serial_prev_seq = msg->seq;
438 * Handle event message
440 void serial_handle_event(event_msg_t *msg)
447 void serial_deliver(int msgid, void *body)
451 //sirq_printf("received init msg\r\n");
452 serial_handle_init((init_msg_t*)body);
455 //sirq_printf("received sync msg\r\n");
456 serial_handle_sync((sync_msg_t*)body);
459 //sirq_printf("received event msg\r\n");
460 serial_handle_event((event_msg_t*)body);
466 * Process serial receive messages
468 void serial_receive(parser_t *parser, int byte)
470 //sirq_printf("serial_receive - %02x\r\n", byte);
473 header_t *head = (header_t*)parser->buffer;
474 void *body = (void*)(head+1);
475 const int max_length = sizeof(parser->buffer)-sizeof(header_t);
477 // Process uart messages
478 parser->buffer[parser->index++] = byte;
479 switch (parser->state) {
481 if (parser->index == sizeof(uint16_t)) {
482 if (head->header == MSG_HEADER) {
485 parser->buffer[0] = parser->buffer[1];
491 if (parser->index == sizeof(header_t)) {
492 if (head->length <= max_length &&
493 head->msgid <= MSG_MAX_ID) {
502 if (parser->index == (int)sizeof(header_t)+head->length) {
503 serial_deliver(head->msgid, body);
511 /********************
513 ********************/
516 DigitalOut led1(LED1);
517 DigitalOut led2(LED2);
522 parser_t parser_mbed;
538 void task_serial(uint64_t local, uint64_t world)
540 while (sirq_ready(sirq_dbg)) {
541 //sirq_printf("serial recv - dbg\r\n");
542 serial_receive(&parser_dbg, sirq_getc(sirq_dbg));
545 while (sirq_ready(sirq_bbb)) {
546 //sirq_printf("serial recv - bbb\r\n");
547 serial_receive(&parser_bbb, sirq_getc(sirq_bbb));
550 while (sirq_ready(sirq_mbed)) {
551 //sirq_printf("serial recv - mbed\r\n");
552 serial_receive(&parser_mbed, sirq_getc(sirq_mbed));
556 void task_events(uint64_t local, uint64_t world)
561 if (tdma_stamp(tdma_evt, &event)) {
562 sirq_printf("event received - evt\r\n");
563 if (tdma_stamp(tdma_rcv, &event))
564 sirq_printf("event received - rcv\r\n");
565 if (tdma_stamp(tdma_xmt, &event))
566 sirq_printf("event received - xmt\r\n");
569 if (tdma_stamp(tdma_evt, &event))
570 serial_send_event(0, event);
572 tdma_start(tdma_evt);
575 void task_sync(uint64_t local, uint64_t world)
577 serial_send_sync(sirq_mbed, local);
580 void task_leds(uint64_t local, uint64_t world)
582 static uint32_t which = 0;
588 void task_emit(uint64_t local, uint64_t world)
590 emit_transmit(local, world);
593 void task_debug(uint64_t local, uint64_t world)
595 //tdma_debug(tdma_rcv);
596 //tdma_debug(tdma_xmt);
598 //sirq_debug(sirq_mbed);
601 sirq_printf("background - %6u.%02u -> %u.%02u\r\n",
602 (uint32_t)(local / NSEC_PER_SEC),
603 (uint32_t)(local % NSEC_PER_SEC / 10000000),
604 (uint32_t)(world / NSEC_PER_SEC),
605 (uint32_t)(world % NSEC_PER_SEC / 10000000));
613 #define N_ELEM(x) (sizeof(x) / sizeof((x)[0]))
615 extern void test_main(void);
616 extern serial_t stdio_uart;
619 void (*task)(uint64_t, uint64_t);
623 { task_serial, 0 }, // always
624 { task_events, 0 }, // always -- testing
625 { task_sync, 0 }, // always
626 { task_emit, 0 }, // always
627 { task_leds, 100000000 }, // 10hz
628 { task_debug, 1000000000 }, // 1hz
631 void background(void)
634 uint64_t local = tdma_time();
635 uint64_t world = time_to_world(local);
638 for (unsigned i = 0; i < N_ELEM(tasks); i++) {
639 if (local >= tasks[i].due) {
640 tasks[i].task(local, world);
641 tasks[i].due += tasks[i].period;
646 int main(int argc, char **argv)
649 emit_init(4, PTC1, PullDown);
652 sirq_dbg = sirq_open(SIRQ_UART0, USBTX, USBRX, 115200); // to pc
653 sirq_bbb = sirq_open(SIRQ_UART1, PTE0, PTE1, 115200); // to bbb
654 sirq_mbed = sirq_open(SIRQ_UART2, PTD3, PTD2, 115200); // to mbed
657 tdma_evt = tdma_open(TDMA_CHAN0, 3, PTC9, PullDown); // async event
660 tdma_rcv = tdma_open(TDMA_CHAN2, 3, PTD2, PullUp); // time sync rcv
661 tdma_xmt = tdma_open(TDMA_CHAN3, 3, PTD3, PullUp); // time sync xmt
664 //tdma_rcv = tdma_open(TDMA_CHAN2, 2, USBRX, PullUp); // time sync rcv
665 //tdma_xmt = tdma_open(TDMA_CHAN3, 2, USBTX, PullUp); // time sync xmt
668 tdma_start(tdma_evt);
669 tdma_start(tdma_rcv);
670 tdma_start(tdma_xmt);
672 // Serial timestamping
673 serial_tdma_rcv = tdma_rcv;
674 serial_tdma_xmt = tdma_xmt;
677 //MCG->C1 = 0x05; // was 0x1A
678 //MCG->C2 = 0x2C; // was 0x24
679 //MCG->C3 = 0x91; // was 0x91
680 //MCG->C4 = 0x10; // was 0x10
681 //MCG->C5 = 0x01; // was 0x01
682 //MCG->C6 = 0x40; // was 0x40
683 //MCG->S = 0x6E; // was 0x6E
684 //MCG->SC = 0x02; // was 0x02
685 //MCG->ATCVH = 0x00; // was 0x00
686 //MCG->ATCVL = 0x00; // was 0x00
687 //MCG->C7 = 0x00; // was 0x00
688 //MCG->C8 = 0x80; // was 0x80
689 //MCG->C9 = 0x00; // was 0x00
690 //MCG->C10 = 0x00; // was 0x00
692 //sirq_printf("MGC - C1 %02hx\r\n", MCG->C1); // 1A
693 //sirq_printf("MGC - C2 %02hx\r\n", MCG->C2); // 24
694 //sirq_printf("MGC - C3 %02hx\r\n", MCG->C3); // 91
695 //sirq_printf("MGC - C4 %02hx\r\n", MCG->C4); // 10
696 //sirq_printf("MGC - C5 %02hx\r\n", MCG->C5); // 01
697 //sirq_printf("MGC - C6 %02hx\r\n", MCG->C6); // 40
698 //sirq_printf("MGC - S %02hx\r\n", MCG->S); // 6E
699 //sirq_printf("MGC - SC %02hx\r\n", MCG->SC); // 02
700 //sirq_printf("MGC - ATCVH %02hx\r\n", MCG->ATCVH); // 00
701 //sirq_printf("MGC - ATCVL %02hx\r\n", MCG->ATCVL); // 00
702 //sirq_printf("MGC - C7 %02hx\r\n", MCG->C7); // 00
703 //sirq_printf("MGC - C8 %02hx\r\n", MCG->C8); // 80
704 //sirq_printf("MGC - C9 %02hx\r\n", MCG->C9); // 00
705 //sirq_printf("MGC - C10 %02hx\r\n", MCG->C10); // 00
707 // Run background loop
711 // Performance testing
712 //uint64_t prev = 0, due = 0;
713 //uint64_t worst[10] = {};
716 // uint64_t local = tdma_time();
717 // if (prev && (local-prev) > worst[count])
718 // worst[count] = (local-prev);
720 // if (local > due) {
722 // static char str[] = "background background background\r\n";
723 // sirq_write(sirq_dbg, str, sizeof(str));
726 // sirq_printf("background\r\n");
727 // for (int i = 0; i < 10; i++) {
728 // sirq_printf(" worst[%d] = 0.%09u\r\n",
733 // due += NSEC_PER_SEC;
734 // count = (count + 1) % 10;