]> Pileus Git - ~andy/csm213a-hw/blob - hw2/main.cpp
9292124da454b95e442519ba975843df35c051d1
[~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  * Timer functions *
25  *******************/
26
27 #define NSEC_PER_SEC 1000000000ULL
28
29 uint64_t time_last_local; // timestamp at last time sync
30 uint64_t time_last_world; // offset at last time sync
31
32 /**
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
37  */
38 //uint64_t time_to_local(uint64_t world, uint64_t valid)
39 //{
40 //      uint64_t now =
41 //      local =  + (stamp);
42 //}
43
44 /**
45  * Generate time stamp for an async event:
46  *   time:  drift compensated wall-clock time
47  *   stamp: event timestamp from PIT Module
48  */
49 uint64_t time_to_world(uint64_t local)
50 {
51         uint64_t elapsed = local - time_last_local;
52         return time_last_world + elapsed;
53 }
54
55 /**
56  * Compensate the Real-Time-Clock oscillator for
57  * temperature and drift errors. Called at 1Hz and
58  * synchronous to the RTC 1Hz output.
59  */
60 void time_rtc_comp(void)
61 {
62         // todo
63 }
64
65 /**
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
70  */
71 void time_ext_init(uint64_t local, uint64_t world)
72 {
73         sirq_printf("initialize clocks: %d -> %d\r\n",
74                         (int)(local/NSEC_PER_SEC),
75                         (int)(world/NSEC_PER_SEC));
76
77         time_last_local = local;
78         time_last_world = world;
79 }
80
81 /**
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
86  */
87 void time_ext_sync(uint64_t local, uint64_t world)
88 {
89         uint64_t guess = time_to_world(local);
90
91         time_last_local = local;
92         time_last_world = (guess/2) + (world/2);
93         //time_last_world = (guess * 3 / 4) + (world * 1 / 4);
94         //time_last_world =
95         //      (guess - (        guess / 2)) +
96         //      (world - (world - world / 2));
97         //time_last_world =
98         //      (guess - (guess - guess / 4)) +
99         //      (world - (        world / 4));
100
101         world = time_last_world;
102
103 //#ifdef VERBOSE
104 #if 1
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)),
114                         ahead ? "-" : " ",
115                         (int32_t )(error / (int64_t)NSEC_PER_SEC),
116                         (uint32_t)(error % (int64_t)NSEC_PER_SEC));
117 #endif
118 //#endif
119 }
120
121 void time_printf(const char *label, uint64_t local)
122 {
123         uint64_t world = time_to_world(local);
124         sirq_printf("%s -- %d.%09u -> %d.%09u\r\n",
125                         label,
126                         (int)(local / NSEC_PER_SEC),
127                         (int)(local % NSEC_PER_SEC),
128                         (int)(world / NSEC_PER_SEC),
129                         (int)(world % NSEC_PER_SEC));
130 }
131
132 /*********************
133  * Signal generation *
134  *********************/
135
136 static uint32_t *emit_pcr    = 0; // transmit pin name
137
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)
141
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
144
145 void emit_init(int alt, PinName pin, PinMode mode)
146 {
147         // Find pin
148         emit_pcr = (uint32_t*)(PORTA_BASE + pin);
149
150         // Enable clocks
151         SIM->SCGC6            |= SIM_SCGC6_TPM0_MASK;
152
153         SIM->SOPT2            |= SIM_SOPT2_TPMSRC(1);
154         SIM->SOPT4            |= SIM_SOPT4_TPM1CLKSEL_MASK;
155
156         // Set pin mode
157         emit_pcr[0]            = PORT_PCR_ISF_MASK
158                                | PORT_PCR_MUX(alt)
159                                | mode;
160
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);
166
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
171
172         TPM0->CONTROLS[0].CnV  = 0xFFFF;              // time delay
173
174         TPM0->STATUS           = TPM_STATUS_CH0F_MASK
175                                | TPM_STATUS_TOF_MASK;
176
177         TPM0->CONF             = TPM_CONF_CSOO_MASK;
178 }
179
180 void emit_enable(uint64_t start, uint64_t period)
181 {
182         const int slack_tick = 0xC000; // tune based on emit_worst
183
184         emit_start  = start;
185         emit_period = period;
186         emit_due    = start + period;
187
188         emit_slack  = slack_tick * 1000 / 24;
189
190         time_printf("emit scheduled", emit_due);
191 }
192
193 void emit_schedule(uint64_t when)
194 {
195         uint64_t now   = time_to_world(tdma_time());
196         uint16_t delay = (uint16_t)(when-now);
197
198         // Clear pending flags
199         //emit_pcr[0]           |= PORT_PCR_ISF_MASK
200
201         // Disable timer
202         TPM0->SC               = TPM_SC_TOF_MASK;
203
204         // Set transmit time
205         TPM0->CNT              = TPM_CNT_COUNT(0);
206         TPM0->CONTROLS[0].CnV  = delay;
207
208         // Start the timer
209         TPM0->SC               = TPM_SC_TOF_MASK
210                                | TPM_SC_CMOD(1);
211
212         // Debug
213         sirq_printf("emitting event\r\n");
214 }
215
216 void emit_transmit(uint64_t local, uint64_t world)
217 {
218         static uint64_t prev = 0;
219
220         // Record how how much time we have to reschedule
221         if (prev && (local-prev) > emit_worst)
222                 emit_worst = (local-prev);
223         prev = local;
224
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;
230         }
231 }
232
233 /************************
234  * Serial I/O functions *
235  ************************/
236
237 typedef struct {
238         int      index;
239         int      state;
240         uint8_t  buffer[256];
241 } parser_t;
242
243 static uint32_t serial_device_id   = 0;
244
245 const  uint64_t serial_sync_delay  = NSEC_PER_SEC / 100; // 1hz
246 static uint64_t serial_sync_due    = 0;
247
248 static tdma_t  *serial_tdma_rcv    = NULL;
249 static tdma_t  *serial_tdma_xmt    = NULL;
250
251 static uint64_t serial_prev_local  = 0;
252 static uint64_t serial_prev_seq    = 0;
253
254 static uint64_t serial_xmt_local   = 0;
255 static uint64_t serial_xmt_seq     = 0;
256
257 /**
258  * Convert world to local time
259  */
260 uint64_t serial_read_time(ntime_t time)
261 {
262         return ((uint64_t)time.seconds) * NSEC_PER_SEC
263              + ((uint64_t)time.nanosec);
264 }
265
266 ntime_t serial_write_time(uint64_t time)
267 {
268         ntime_t buf = {};
269         buf.seconds = time / NSEC_PER_SEC;
270         buf.nanosec = time % NSEC_PER_SEC;
271         return buf;
272 }
273
274 /**
275  * Output initialization message init message
276  */
277 void serial_send_init(uint16_t device, uint64_t local)
278 {
279 }
280
281 /**
282  * Output time sync message
283  */
284 void serial_send_sync(sirq_t *port, uint64_t now)
285 {
286         if (serial_sync_due == 0 || now < serial_sync_due)
287                 return; // not ready
288
289         //sirq_printf("sending sync\r\n");
290
291         // Calculate world time
292         uint64_t local = 0;
293         uint64_t world = time_to_world(serial_xmt_local);
294
295         // Message data
296         header_t   head;
297         sync_msg_t body;
298
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
304
305         body.seq          = serial_xmt_seq;
306         body.time.seconds = world / NSEC_PER_SEC;
307         body.time.nanosec = world % NSEC_PER_SEC;
308
309         tdma_stop(serial_tdma_rcv);
310
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);
315
316         // save transmit time
317         int valid = tdma_stamp(serial_tdma_xmt, &local);
318         if (!valid)
319                 sirq_printf("sync transmit time -- missed\r\n");
320         else
321                 //time_printf("sync transmit time ", local);
322
323         tdma_start(serial_tdma_rcv);
324
325         serial_xmt_seq  += 1;
326         serial_sync_due  = 0;
327         serial_xmt_local = local;
328 }
329
330 /**
331  * Output external event received message
332  *   event: id of the received event
333  *   time:  compensated timestamp of the event
334  */
335 void serial_send_event(uint16_t event, uint64_t local)
336 {
337 //      uint64_t world = time_to_world(local);
338
339         // Message data
340 #if 0
341         header_t    head;
342         event_msg_t body;
343
344         ntime_t time = {};
345         time.seconds = (uint32_t)(world / NSEC_PER_SEC);
346         time.nanosec = (uint32_t)(world % NSEC_PER_SEC);
347
348         sirq_printf("event received - %08x:%08x - %u.%09u\r\n",
349                 (uint32_t)(local >> 32), (uint32_t)local,
350                 time.seconds, time.nanosec);
351
352         // Transmit sync message
353         head.header = MSG_HEADER;
354         head.msgid  = MSG_ID_SYNC;
355         head.length = sizeof(body);
356         head.cksum  = 0; // todo
357
358         body.seq          = serial_xmt_seq;
359         body.time.seconds = world / NSEC_PER_SEC;
360         body.time.nanosec = world % NSEC_PER_SEC;
361
362         tdma_stop(serial_tdma_rcv);
363
364         tdma_start(serial_tdma_xmt);
365         sirq_write(port, &head, sizeof(head));
366         sirq_write(port, &body, sizeof(body));
367         tdma_stop(serial_tdma_xmt);
368 #endif
369 }
370
371 /**
372  * Handle init message
373  */
374 void serial_handle_init(init_msg_t *msg)
375 {
376         sirq_printf("initialize: %s %s %s %s %s\r\n",
377                 msg->valid & MSG_VALID_DEVICE ? "DEV"    : "dev",
378                 msg->valid & MSG_VALID_START  ? "START"  : "start",
379                 msg->valid & MSG_VALID_PERIOD ? "PERIOD" : "period",
380                 msg->valid & MSG_VALID_WORLD  ? "WORLD"  : "world",
381                 msg->valid & MSG_VALID_SYNC   ? "SYNC"   : "sync");
382         sirq_printf("  dev    -- %d\r\n", msg->device);
383         time_printf("  start ", serial_read_time(msg->start));
384         time_printf("  period", serial_read_time(msg->period));
385         time_printf("  world ", serial_read_time(msg->world));
386
387         if (msg->valid & MSG_VALID_DEVICE)
388                 serial_device_id = msg->device;
389
390         if (msg->valid & MSG_VALID_START ||
391             msg->valid & MSG_VALID_PERIOD) {
392                 uint64_t start  = serial_read_time(msg->start);
393                 uint64_t period = serial_read_time(msg->period);
394                 emit_enable(start, period);
395         }
396
397         if (msg->valid & MSG_VALID_WORLD) {
398                 uint64_t world = serial_read_time(msg->world);
399                 uint64_t local = tdma_time();
400                 time_ext_init(local, world);
401         }
402
403         if (msg->valid & MSG_VALID_SYNC)
404                 serial_sync_due = tdma_time() + serial_sync_delay;
405 }
406
407 /**
408  * Handle sync message
409  */
410 void serial_handle_sync(sync_msg_t *msg)
411 {
412         // Read receive timestamp for next time sync message
413         uint64_t current = 0;
414         int valid = tdma_stamp(serial_tdma_rcv, &current);
415         if (!valid)
416                 sirq_printf("sync receive time  -- missing\r\n");
417         //else
418         //      time_printf("sync receive time ", current);
419         tdma_stop(serial_tdma_rcv);
420
421         // Lookup times
422         uint64_t world = ((uint64_t)msg->time.seconds) * NSEC_PER_SEC
423                        + ((uint64_t)msg->time.nanosec);
424
425         // Valid times timestamp
426         if (serial_prev_seq == (msg->seq-1)) {
427                 uint64_t local = serial_prev_local;
428                 time_ext_sync(local, world);
429         }
430
431         // Queue transmit to other board
432         serial_sync_due   = tdma_time() + serial_sync_delay;
433
434         // Update states
435         serial_prev_local = current;
436         serial_prev_seq   = msg->seq;
437 }
438
439 /**
440  * Handle event message
441  */
442 void serial_handle_event(event_msg_t *msg)
443 {
444 }
445
446 /**
447  * Deliver message
448  */
449 void serial_deliver(int msgid, void *body)
450 {
451         switch (msgid) {
452                 case MSG_ID_INIT:
453                         //sirq_printf("received init msg\r\n");
454                         serial_handle_init((init_msg_t*)body);
455                         break;
456                 case MSG_ID_SYNC:
457                         //sirq_printf("received sync msg\r\n");
458                         serial_handle_sync((sync_msg_t*)body);
459                         break;
460                 case MSG_ID_EVENT:
461                         //sirq_printf("received event msg\r\n");
462                         serial_handle_event((event_msg_t*)body);
463                         break;
464         }
465 }
466
467 /**
468  * Process serial receive messages
469  */
470 void serial_receive(parser_t *parser, int byte)
471 {
472         //sirq_printf("serial_receive - %02x\r\n", byte);
473
474         // Lookup pointers
475         header_t *head = (header_t*)parser->buffer;
476         void     *body = (void*)(head+1);
477         const int max_length = sizeof(parser->buffer)-sizeof(header_t);
478
479         // Process uart messages
480         parser->buffer[parser->index++] = byte;
481         switch (parser->state) {
482                 case 0: // Search
483                         if (parser->index == sizeof(uint16_t)) {
484                                 if (head->header == MSG_HEADER) {
485                                         parser->state = 1;
486                                 } else {
487                                         parser->buffer[0] = parser->buffer[1];
488                                         parser->index = 1;
489                                 }
490                         }
491                         break;
492                 case 1: // Header
493                         if (parser->index == sizeof(header_t)) {
494                                 if (head->length <= max_length &&
495                                     head->msgid  <= MSG_MAX_ID) {
496                                         parser->state = 2;
497                                 } else {
498                                         parser->index = 0;
499                                         parser->state = 0;
500                                 }
501                         }
502                         break;
503                 case 2: // Data
504                         if (parser->index == (int)sizeof(header_t)+head->length) {
505                                 serial_deliver(head->msgid, body);
506                                 parser->index = 0;
507                                 parser->state = 0;
508                         }
509                         break;
510         }
511 }
512
513 /********************
514  * Data definitions *
515  ********************/
516
517 // LEDs
518 DigitalOut led1(LED1);
519 DigitalOut led2(LED2);
520
521 // Message Parsers
522 parser_t   parser_dbg;
523 parser_t   parser_bbb;
524 parser_t   parser_mbed;
525
526 // Serial IRQ
527 sirq_t    *sirq_dbg;
528 sirq_t    *sirq_bbb;
529 sirq_t    *sirq_mbed;
530
531 // Timer DMA
532 tdma_t    *tdma_evt;
533 tdma_t    *tdma_rcv;
534 tdma_t    *tdma_xmt;
535
536 /*********
537  * Tasks *
538  *********/
539
540 void task_serial(uint64_t local, uint64_t world)
541 {
542         while (sirq_ready(sirq_dbg)) {
543                 //sirq_printf("serial recv - dbg\r\n");
544                 serial_receive(&parser_dbg,  sirq_getc(sirq_dbg));
545         }
546
547         while (sirq_ready(sirq_bbb)) {
548                 //sirq_printf("serial recv - bbb\r\n");
549                 serial_receive(&parser_bbb,  sirq_getc(sirq_bbb));
550         }
551
552         while (sirq_ready(sirq_mbed)) {
553                 //sirq_printf("serial recv - mbed\r\n");
554                 serial_receive(&parser_mbed, sirq_getc(sirq_mbed));
555         }
556 }
557
558 void task_events(uint64_t local, uint64_t world)
559 {
560         uint64_t event = 0;
561
562 #ifdef VERBOSE
563         if (tdma_stamp(tdma_evt, &event)) {
564                 sirq_printf("event received - evt\r\n");
565         if (tdma_stamp(tdma_rcv, &event))
566                 sirq_printf("event received - rcv\r\n");
567         if (tdma_stamp(tdma_xmt, &event))
568                 sirq_printf("event received - xmt\r\n");
569 #endif
570
571         if (tdma_stamp(tdma_evt, &event))
572                 serial_send_event(0, event);
573         tdma_stop(tdma_evt);
574         tdma_start(tdma_evt);
575 }
576
577 void task_sync(uint64_t local, uint64_t world)
578 {
579         serial_send_sync(sirq_mbed, local);
580 }
581
582 void task_leds(uint64_t local, uint64_t world)
583 {
584         static uint32_t which = 0;
585         led1 = (which == 0);
586         led2 = (which == 1);
587         which ^= 1;
588 }
589
590 void task_emit(uint64_t local, uint64_t world)
591 {
592         emit_transmit(local, world);
593 }
594
595 void task_debug(uint64_t local, uint64_t world)
596 {
597         //tdma_debug(tdma_rcv);
598         //tdma_debug(tdma_xmt);
599
600         //sirq_debug(sirq_mbed);
601
602 #ifdef VERBOSE
603         sirq_printf("background - %6u.%02u -> %u.%02u\r\n",
604                         (uint32_t)(local / NSEC_PER_SEC),
605                         (uint32_t)(local % NSEC_PER_SEC / 10000000),
606                         (uint32_t)(world / NSEC_PER_SEC),
607                         (uint32_t)(world % NSEC_PER_SEC / 10000000));
608 #endif
609 }
610
611 /********
612  * Main *
613  ********/
614
615 #define N_ELEM(x) (sizeof(x) / sizeof((x)[0]))
616
617 extern void test_main(void);
618 extern serial_t stdio_uart;
619
620 static struct {
621         void (*task)(uint64_t, uint64_t);
622         uint64_t period;
623         uint64_t due;
624 } tasks[] = {
625         { task_serial, 0          }, // always
626         { task_events, 1000000000 }, // always -- testing
627         { task_sync,   0          }, // always
628         { task_emit,   0          }, // always
629         { task_leds,   100000000  }, // 10hz
630         { task_debug,  1000000000 }, // 1hz
631 };
632
633 void background(void)
634 {
635         // Debugging
636         uint64_t local = tdma_time();
637         uint64_t world = time_to_world(local);
638
639         // Run the scheduler
640         for (unsigned i = 0; i < N_ELEM(tasks); i++) {
641                 if (local >= tasks[i].due) {
642                         tasks[i].task(local, world);
643                         tasks[i].due += tasks[i].period;
644                 }
645         }
646 }
647
648 int main(int argc, char **argv)
649 {
650         tdma_init();
651         emit_init(4, PTC1, PullDown);
652
653         // Open serial ports
654         sirq_dbg   = sirq_open(SIRQ_UART0, USBTX, USBRX, 115200); // to pc
655         sirq_bbb   = sirq_open(SIRQ_UART1, PTE0,  PTE1,  115200); // to bbb
656         sirq_mbed  = sirq_open(SIRQ_UART2, PTD3,  PTD2,  115200); // to mbed
657
658         // Setup timers
659         tdma_evt   = tdma_open(TDMA_CHAN0, 3, PTC9,  PullUp); // async event
660
661         // mbed time sync
662         tdma_rcv   = tdma_open(TDMA_CHAN2, 3, PTD2,  PullUp);   // time sync rcv
663         tdma_xmt   = tdma_open(TDMA_CHAN3, 3, PTD3,  PullUp);   // time sync xmt
664
665         // host time sync
666         //tdma_rcv   = tdma_open(TDMA_CHAN2, 2, USBRX, PullUp); // time sync rcv
667         //tdma_xmt   = tdma_open(TDMA_CHAN3, 2, USBTX, PullUp); // time sync xmt
668
669         // start timers
670         tdma_start(tdma_evt);
671         tdma_start(tdma_rcv);
672         tdma_start(tdma_xmt);
673
674         // Serial timestamping
675         serial_tdma_rcv = tdma_rcv;
676         serial_tdma_xmt = tdma_xmt;
677
678         // Test clocks
679         //MCG->C1    = 0x05; // was 0x1A
680         //MCG->C2    = 0x2C; // was 0x24
681         //MCG->C3    = 0x91; // was 0x91
682         //MCG->C4    = 0x10; // was 0x10
683         //MCG->C5    = 0x01; // was 0x01
684         //MCG->C6    = 0x40; // was 0x40
685         //MCG->S     = 0x6E; // was 0x6E
686         //MCG->SC    = 0x02; // was 0x02
687         //MCG->ATCVH = 0x00; // was 0x00
688         //MCG->ATCVL = 0x00; // was 0x00
689         //MCG->C7    = 0x00; // was 0x00
690         //MCG->C8    = 0x80; // was 0x80
691         //MCG->C9    = 0x00; // was 0x00
692         //MCG->C10   = 0x00; // was 0x00
693
694         //sirq_printf("MGC - C1    %02hx\r\n", MCG->C1);     // 1A
695         //sirq_printf("MGC - C2    %02hx\r\n", MCG->C2);     // 24
696         //sirq_printf("MGC - C3    %02hx\r\n", MCG->C3);     // 91
697         //sirq_printf("MGC - C4    %02hx\r\n", MCG->C4);     // 10
698         //sirq_printf("MGC - C5    %02hx\r\n", MCG->C5);     // 01
699         //sirq_printf("MGC - C6    %02hx\r\n", MCG->C6);     // 40
700         //sirq_printf("MGC - S     %02hx\r\n", MCG->S);      // 6E
701         //sirq_printf("MGC - SC    %02hx\r\n", MCG->SC);     // 02
702         //sirq_printf("MGC - ATCVH %02hx\r\n", MCG->ATCVH);  // 00
703         //sirq_printf("MGC - ATCVL %02hx\r\n", MCG->ATCVL);  // 00
704         //sirq_printf("MGC - C7    %02hx\r\n", MCG->C7);     // 00
705         //sirq_printf("MGC - C8    %02hx\r\n", MCG->C8);     // 80
706         //sirq_printf("MGC - C9    %02hx\r\n", MCG->C9);     // 00
707         //sirq_printf("MGC - C10   %02hx\r\n", MCG->C10);    // 00
708
709         // Run background loop
710         while (true)
711                 background();
712
713         // Performance testing
714         //uint64_t prev = 0, due = 0;
715         //uint64_t worst[10] = {};
716         //int      count = 0;
717         //while (true) {
718         //      uint64_t local = tdma_time();
719         //      if (prev && (local-prev) > worst[count])
720         //              worst[count] = (local-prev);
721         //      prev = local;
722         //      if (local > due) {
723         //              if (count == 5) {
724         //                      static char str[] = "background background background\r\n";
725         //                      sirq_write(sirq_dbg, str, sizeof(str));
726         //              }
727         //              if (count == 9) {
728         //                      sirq_printf("background\r\n");
729         //                      for (int i = 0; i < 10; i++) {
730         //                              sirq_printf("  worst[%d] = 0.%09u\r\n",
731         //                                              i, worst[i]);
732         //                              worst[i] = 0;
733         //                      }
734         //              }
735         //              due += NSEC_PER_SEC;
736         //              count = (count + 1) % 10;
737         //      }
738         //}
739
740         // Run tests
741         //test_main();
742
743         return 0;
744 }