]> Pileus Git - ~andy/csm213a-hw/blob - hw2/main.cpp
Improve time sync accuracy
[~andy/csm213a-hw] / hw2 / main.cpp
1 #include "messages.h"
2
3 #include "mbed.h"
4 #include "serial_irq.h"
5 #include "serial_dma.h"
6 #include "timer_dma.h"
7
8 /**
9  * Mode of operation:
10  *   Devices 1 and 2 synchronize clocks using serial messages.
11  *
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
15  *      message.
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.
19  *
20  *   Time synchronization is performed in both directions.
21  */
22
23 /****************
24  * Testing vars *
25  ****************/
26
27 uint32_t test_xmt_enab  = 0;
28 uint64_t test_xmt_time0 = 0;
29 uint64_t test_xmt_time1 = 0;
30
31 uint32_t test_rcv_enab  = 0;
32 uint64_t test_rcv_time  = 0;
33
34 /*******************
35  * Timer functions *
36  *******************/
37
38 #define NSEC_PER_SEC 1000000000ULL
39
40 uint64_t time_last_local; // timestamp at last time sync
41 uint64_t time_last_world; // offset at last time sync
42
43 /**
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
48  */
49 //uint64_t time_to_local(uint64_t world, uint64_t valid)
50 //{
51 //      uint64_t now =
52 //      local =  + (stamp);
53 //}
54
55 /**
56  * Generate time stamp for an async event:
57  *   time:  drift compensated wall-clock time
58  *   stamp: event timestamp from PIT Module
59  */
60 uint64_t time_to_world(uint64_t local)
61 {
62         uint64_t elapsed = local - time_last_local;
63         return time_last_world + elapsed;
64 }
65
66 /**
67  * Compensate the Real-Time-Clock oscillator for
68  * temperature and drift errors. Called at 1Hz and
69  * synchronous to the RTC 1Hz output.
70  */
71 void time_rtc_comp(void)
72 {
73         // todo
74 }
75
76 /**
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
81  */
82 void time_ext_init(uint64_t local, uint64_t world)
83 {
84         sirq_printf("initialize clocks: %d -> %d\r\n",
85                         (int)(local/NSEC_PER_SEC),
86                         (int)(world/NSEC_PER_SEC));
87
88         time_last_local = local;
89         time_last_world = world;
90 }
91
92 /**
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
97  */
98 void time_ext_sync(uint64_t local, uint64_t world)
99 {
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;
104
105         time_last_local = local;
106         time_last_world = (guess/2) + (world/2);
107         //time_last_world = (guess * 3 / 4) + (world * 1 / 4);
108         //time_last_world =
109         //      (guess - (        guess / 2)) +
110         //      (world - (world - world / 2));
111         //time_last_world =
112         //      (guess - (guess - guess / 4)) +
113         //      (world - (        world / 4));
114
115         world = time_last_world;
116
117 //#ifdef VERBOSE
118 #if 0
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)),
125                         ahead ? "-" : " ",
126                         (int32_t )(error / (int64_t)NSEC_PER_SEC),
127                         (uint32_t)(error % (int64_t)NSEC_PER_SEC));
128 #endif
129 //#endif
130
131         // .000000284
132         // .000000253
133         // .000000264
134         // .000000451
135         // .000000284
136         // .000000267
137         // .000000223
138         // .000000326
139
140 }
141
142 void time_printf(const char *label, uint64_t local)
143 {
144         uint64_t world = time_to_world(local);
145         sirq_printf("%s -- %d.%09u -> %d.%09u\r\n",
146                         label,
147                         (int)(local / NSEC_PER_SEC),
148                         (int)(local % NSEC_PER_SEC),
149                         (int)(world / NSEC_PER_SEC),
150                         (int)(world % NSEC_PER_SEC));
151 }
152
153 /************************
154  * Serial I/O functions *
155  ************************/
156
157 typedef struct {
158         int      index;
159         int      state;
160         uint8_t  buffer[256];
161 } parser_t;
162
163 const  uint64_t serial_sync_delay = NSEC_PER_SEC / 100; // 1hz
164 static uint64_t serial_sync_due   = 0;
165
166 static tdma_t  *serial_tdma_rcv   = NULL;
167 static tdma_t  *serial_tdma_xmt   = NULL;
168
169 static uint64_t serial_prev_local = 0;
170 static uint64_t serial_prev_seq   = 0;
171
172 static uint64_t serial_xmt_local  = 0;
173 static uint64_t serial_xmt_seq    = 0;
174
175 /**
176  * Output time sync message
177  */
178 void serial_send_sync(sirq_t *port, uint64_t now)
179 {
180         if (serial_sync_due == 0 || now < serial_sync_due)
181                 return; // not ready
182
183         //sirq_printf("sending sync\r\n");
184
185         // Calculate world time
186         uint64_t local = 0;
187         uint64_t world = time_to_world(serial_xmt_local);
188
189         // Message data
190         header_t   head;
191         sync_msg_t body;
192
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
198
199         body.seq          = serial_xmt_seq;
200         body.time.seconds = world / NSEC_PER_SEC;
201         body.time.nanosec = world % NSEC_PER_SEC;
202
203         tdma_stop(serial_tdma_rcv);
204
205         test_xmt_enab  = 1;
206         test_xmt_time0 = 0;
207         test_xmt_time1 = 0;
208
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);
213
214         // save transmit time
215         //local = test_xmt_time1;
216         int valid = tdma_stamp(serial_tdma_xmt, &local);
217         if (!valid) {
218                 sirq_printf("sync transmit time -- missed\r\n");
219         } else {
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);
223         }
224
225         tdma_start(serial_tdma_rcv);
226
227         serial_xmt_seq  += 1;
228         serial_sync_due  = 0;
229         serial_xmt_local = local;
230 }
231
232 /**
233  * Output external event received message
234  *   event: id of the received event
235  *   time:  compensated timestamp of the event
236  */
237 void serial_send_event(uint16_t event, uint64_t local)
238 {
239         uint64_t world = time_to_world(local);
240
241         ntime_t time = {};
242         time.seconds = (uint32_t)(world / NSEC_PER_SEC);
243         time.nanosec = (uint32_t)(world % NSEC_PER_SEC);
244
245         sirq_printf("event received - %08x:%08x - %u.%09u\r\n",
246                 (uint32_t)(local >> 32), (uint32_t)local,
247                 time.seconds, time.nanosec);
248         // todo
249 }
250
251 /**
252  * Handle sync message
253  */
254 void serial_handle_sync(sync_msg_t *msg)
255 {
256         // Read receive timestamp for next time sync message
257         uint64_t current = 0;
258         int valid = tdma_stamp(serial_tdma_rcv, &current);
259         if (!valid)
260                 sirq_printf("sync receive time  -- missing\r\n");
261         //else
262         //      time_printf("sync receive time ", current);
263         tdma_stop(serial_tdma_rcv);
264
265         // Lookup times
266         uint64_t world = ((uint64_t)msg->time.seconds) * NSEC_PER_SEC
267                        + ((uint64_t)msg->time.nanosec);
268
269         // Initialize
270         if (msg->seq == 0) {
271                 uint64_t local = tdma_time();
272                 time_ext_init(local, world);
273         }
274
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);
279         }
280
281         // Queue transmit to other board
282         serial_sync_due   = tdma_time() + serial_sync_delay;
283
284         // Update states
285         serial_prev_local = current;
286         serial_prev_seq   = msg->seq;
287 }
288
289 /**
290  * Handle event message
291  */
292 void serial_handle_event(event_msg_t *msg)
293 {
294 }
295
296 /**
297  * Deliver message
298  */
299 void serial_deliver(int msgid, void *body)
300 {
301         switch (msgid) {
302                 case MSG_ID_SYNC:
303                         //sirq_printf("received sync msg\r\n");
304                         serial_handle_sync((sync_msg_t*)body);
305                         break;
306                 case MSG_ID_EVENT:
307                         //sirq_printf("received event msg\r\n");
308                         serial_handle_event((event_msg_t*)body);
309                         break;
310         }
311 }
312
313 /**
314  * Process serial receive messages
315  */
316 void serial_receive(parser_t *parser, int byte)
317 {
318         //sirq_printf("serial_receive - %02x\r\n", byte);
319
320         // Lookup pointers
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);
324
325         // Process uart messages
326         parser->buffer[parser->index++] = byte;
327         switch (parser->state) {
328                 case 0: // Search
329                         if (parser->index == sizeof(uint16_t)) {
330                                 if (head->header == MSG_HEADER) {
331                                         parser->state = 1;
332                                 } else {
333                                         parser->buffer[0] = parser->buffer[1];
334                                         parser->index = 1;
335                                 }
336                         }
337                         break;
338                 case 1: // Header
339                         if (parser->index == sizeof(header_t)) {
340                                 if (head->length <= max_length &&
341                                     head->msgid  <= MSG_MAXID) {
342                                         parser->state = 2;
343                                 } else {
344                                         parser->index = 0;
345                                         parser->state = 0;
346                                 }
347                         }
348                         break;
349                 case 2: // Data
350                         if (parser->index == (int)sizeof(header_t)+head->length) {
351                                 serial_deliver(head->msgid, body);
352                                 parser->index = 0;
353                                 parser->state = 0;
354                         }
355                         break;
356         }
357 }
358
359 /********************
360  * Data definitions *
361  ********************/
362
363 // LEDs
364 DigitalOut led1(LED1);
365 DigitalOut led2(LED2);
366
367 // Message Parsers
368 parser_t   parser_dbg;
369 parser_t   parser_bbb;
370 parser_t   parser_mbed;
371
372 // Serial IRQ
373 sirq_t    *sirq_dbg;
374 sirq_t    *sirq_bbb;
375 sirq_t    *sirq_mbed;
376
377 // Timer DMA
378 tdma_t    *tdma_evt;
379 tdma_t    *tdma_rcv;
380 tdma_t    *tdma_xmt;
381
382 /*********
383  * Tasks *
384  *********/
385
386 void task_serial(uint64_t local, uint64_t world)
387 {
388         while (sirq_ready(sirq_dbg)) {
389                 //sirq_printf("serial recv - dbg\r\n");
390                 serial_receive(&parser_dbg,  sirq_getc(sirq_dbg));
391         }
392
393         while (sirq_ready(sirq_bbb)) {
394                 //sirq_printf("serial recv - bbb\r\n");
395                 serial_receive(&parser_bbb,  sirq_getc(sirq_bbb));
396         }
397
398         while (sirq_ready(sirq_mbed)) {
399                 //sirq_printf("serial recv - mbed\r\n");
400                 serial_receive(&parser_mbed, sirq_getc(sirq_mbed));
401         }
402 }
403
404 void task_events(uint64_t local, uint64_t world)
405 {
406         uint64_t event = 0;
407
408 #ifdef VERBOSE
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");
415 #endif
416
417         if (tdma_stamp(tdma_evt, &event))
418                 serial_send_event(0, event);
419         tdma_stop(tdma_evt);
420         tdma_start(tdma_evt);
421 }
422
423 void task_sync(uint64_t local, uint64_t world)
424 {
425         serial_send_sync(sirq_mbed, local);
426 }
427
428 void task_leds(uint64_t local, uint64_t world)
429 {
430         static uint32_t which = 0;
431         led1 = (which == 0);
432         led2 = (which == 1);
433         which ^= 1;
434 }
435
436 void task_debug(uint64_t local, uint64_t world)
437 {
438         //tdma_debug(tdma_rcv);
439         //tdma_debug(tdma_xmt);
440
441         //sirq_debug(sirq_mbed);
442
443 #ifdef VERBOSE
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));
449 #endif
450 }
451
452 /********
453  * Main *
454  ********/
455
456 #define N_ELEM(x) (sizeof(x) / sizeof((x)[0]))
457
458 extern void test_main(void);
459 extern serial_t stdio_uart;
460
461 static struct {
462         void (*task)(uint64_t, uint64_t);
463         uint64_t period;
464         uint64_t due;
465 } tasks[] = {
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
471 };
472
473 void background(void)
474 {
475         // Debugging
476         uint64_t local = tdma_time();
477         uint64_t world = time_to_world(local);
478
479         // Run the scheduler
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;
484                 }
485         }
486 }
487
488 int main(int argc, char **argv)
489 {
490         tdma_init();
491
492         // Open serial ports
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
496
497         // Setup timers
498         tdma_evt   = tdma_open(TDMA_CHAN0, 3, PTC9,  PullUp); // async event
499
500         // mbed time sync
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
503
504         // host time sync
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
507
508         // start timers
509         tdma_start(tdma_evt);
510         tdma_start(tdma_rcv);
511         tdma_start(tdma_xmt);
512
513         // Serial timestamping
514         serial_tdma_rcv = tdma_rcv;
515         serial_tdma_xmt = tdma_xmt;
516
517         // Test clocks
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
532
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
547
548         // Run background loop
549         printf("hello");
550         while (true)
551                 background();
552
553         // Run tests
554         //test_main();
555
556         return 0;
557 }