]> Pileus Git - ~andy/csm213a-hw/blob - hw2/main.cpp
Fix input events
[~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 0
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         time_printf("event received", local);
338
339 #if 0
340         // Message data
341         header_t    head;
342         event_msg_t body;
343
344         uint64_t world = time_to_world(local);
345
346         ntime_t time = {};
347         time.seconds = (uint32_t)(world / NSEC_PER_SEC);
348         time.nanosec = (uint32_t)(world % NSEC_PER_SEC);
349
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
355
356         body.seq          = serial_xmt_seq;
357         body.time.seconds = world / NSEC_PER_SEC;
358         body.time.nanosec = world % NSEC_PER_SEC;
359
360         tdma_stop(serial_tdma_rcv);
361
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);
366 #endif
367 }
368
369 /**
370  * Handle init message
371  */
372 void serial_handle_init(init_msg_t *msg)
373 {
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));
384
385         if (msg->valid & MSG_VALID_DEVICE)
386                 serial_device_id = msg->device;
387
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);
393         }
394
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);
399         }
400
401         if (msg->valid & MSG_VALID_SYNC)
402                 serial_sync_due = tdma_time() + serial_sync_delay;
403 }
404
405 /**
406  * Handle sync message
407  */
408 void serial_handle_sync(sync_msg_t *msg)
409 {
410         // Read receive timestamp for next time sync message
411         uint64_t current = 0;
412         int valid = tdma_stamp(serial_tdma_rcv, &current);
413         if (!valid)
414                 sirq_printf("sync receive time  -- missing\r\n");
415         //else
416         //      time_printf("sync receive time ", current);
417         tdma_stop(serial_tdma_rcv);
418
419         // Lookup times
420         uint64_t world = ((uint64_t)msg->time.seconds) * NSEC_PER_SEC
421                        + ((uint64_t)msg->time.nanosec);
422
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);
427         }
428
429         // Queue transmit to other board
430         serial_sync_due   = tdma_time() + serial_sync_delay;
431
432         // Update states
433         serial_prev_local = current;
434         serial_prev_seq   = msg->seq;
435 }
436
437 /**
438  * Handle event message
439  */
440 void serial_handle_event(event_msg_t *msg)
441 {
442 }
443
444 /**
445  * Deliver message
446  */
447 void serial_deliver(int msgid, void *body)
448 {
449         switch (msgid) {
450                 case MSG_ID_INIT:
451                         //sirq_printf("received init msg\r\n");
452                         serial_handle_init((init_msg_t*)body);
453                         break;
454                 case MSG_ID_SYNC:
455                         //sirq_printf("received sync msg\r\n");
456                         serial_handle_sync((sync_msg_t*)body);
457                         break;
458                 case MSG_ID_EVENT:
459                         //sirq_printf("received event msg\r\n");
460                         serial_handle_event((event_msg_t*)body);
461                         break;
462         }
463 }
464
465 /**
466  * Process serial receive messages
467  */
468 void serial_receive(parser_t *parser, int byte)
469 {
470         //sirq_printf("serial_receive - %02x\r\n", byte);
471
472         // Lookup pointers
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);
476
477         // Process uart messages
478         parser->buffer[parser->index++] = byte;
479         switch (parser->state) {
480                 case 0: // Search
481                         if (parser->index == sizeof(uint16_t)) {
482                                 if (head->header == MSG_HEADER) {
483                                         parser->state = 1;
484                                 } else {
485                                         parser->buffer[0] = parser->buffer[1];
486                                         parser->index = 1;
487                                 }
488                         }
489                         break;
490                 case 1: // Header
491                         if (parser->index == sizeof(header_t)) {
492                                 if (head->length <= max_length &&
493                                     head->msgid  <= MSG_MAX_ID) {
494                                         parser->state = 2;
495                                 } else {
496                                         parser->index = 0;
497                                         parser->state = 0;
498                                 }
499                         }
500                         break;
501                 case 2: // Data
502                         if (parser->index == (int)sizeof(header_t)+head->length) {
503                                 serial_deliver(head->msgid, body);
504                                 parser->index = 0;
505                                 parser->state = 0;
506                         }
507                         break;
508         }
509 }
510
511 /********************
512  * Data definitions *
513  ********************/
514
515 // LEDs
516 DigitalOut led1(LED1);
517 DigitalOut led2(LED2);
518
519 // Message Parsers
520 parser_t   parser_dbg;
521 parser_t   parser_bbb;
522 parser_t   parser_mbed;
523
524 // Serial IRQ
525 sirq_t    *sirq_dbg;
526 sirq_t    *sirq_bbb;
527 sirq_t    *sirq_mbed;
528
529 // Timer DMA
530 tdma_t    *tdma_evt;
531 tdma_t    *tdma_rcv;
532 tdma_t    *tdma_xmt;
533
534 /*********
535  * Tasks *
536  *********/
537
538 void task_serial(uint64_t local, uint64_t world)
539 {
540         while (sirq_ready(sirq_dbg)) {
541                 //sirq_printf("serial recv - dbg\r\n");
542                 serial_receive(&parser_dbg,  sirq_getc(sirq_dbg));
543         }
544
545         while (sirq_ready(sirq_bbb)) {
546                 //sirq_printf("serial recv - bbb\r\n");
547                 serial_receive(&parser_bbb,  sirq_getc(sirq_bbb));
548         }
549
550         while (sirq_ready(sirq_mbed)) {
551                 //sirq_printf("serial recv - mbed\r\n");
552                 serial_receive(&parser_mbed, sirq_getc(sirq_mbed));
553         }
554 }
555
556 void task_events(uint64_t local, uint64_t world)
557 {
558         uint64_t event = 0;
559
560 #ifdef VERBOSE
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");
567 #endif
568
569         if (tdma_stamp(tdma_evt, &event))
570                 serial_send_event(0, event);
571         tdma_stop(tdma_evt);
572         tdma_start(tdma_evt);
573 }
574
575 void task_sync(uint64_t local, uint64_t world)
576 {
577         serial_send_sync(sirq_mbed, local);
578 }
579
580 void task_leds(uint64_t local, uint64_t world)
581 {
582         static uint32_t which = 0;
583         led1 = (which == 0);
584         led2 = (which == 1);
585         which ^= 1;
586 }
587
588 void task_emit(uint64_t local, uint64_t world)
589 {
590         emit_transmit(local, world);
591 }
592
593 void task_debug(uint64_t local, uint64_t world)
594 {
595         //tdma_debug(tdma_rcv);
596         //tdma_debug(tdma_xmt);
597
598         //sirq_debug(sirq_mbed);
599
600 #ifdef VERBOSE
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));
606 #endif
607 }
608
609 /********
610  * Main *
611  ********/
612
613 #define N_ELEM(x) (sizeof(x) / sizeof((x)[0]))
614
615 extern void test_main(void);
616 extern serial_t stdio_uart;
617
618 static struct {
619         void (*task)(uint64_t, uint64_t);
620         uint64_t period;
621         uint64_t due;
622 } tasks[] = {
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
629 };
630
631 void background(void)
632 {
633         // Debugging
634         uint64_t local = tdma_time();
635         uint64_t world = time_to_world(local);
636
637         // Run the scheduler
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;
642                 }
643         }
644 }
645
646 int main(int argc, char **argv)
647 {
648         tdma_init();
649         emit_init(4, PTC1, PullDown);
650
651         // Open serial ports
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
655
656         // Setup timers
657         tdma_evt   = tdma_open(TDMA_CHAN0, 3, PTC9,  PullDown); // async event
658
659         // mbed time sync
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
662
663         // host time sync
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
666
667         // start timers
668         tdma_start(tdma_evt);
669         tdma_start(tdma_rcv);
670         tdma_start(tdma_xmt);
671
672         // Serial timestamping
673         serial_tdma_rcv = tdma_rcv;
674         serial_tdma_xmt = tdma_xmt;
675
676         // Test clocks
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
691
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
706
707         // Run background loop
708         while (true)
709                 background();
710
711         // Performance testing
712         //uint64_t prev = 0, due = 0;
713         //uint64_t worst[10] = {};
714         //int      count = 0;
715         //while (true) {
716         //      uint64_t local = tdma_time();
717         //      if (prev && (local-prev) > worst[count])
718         //              worst[count] = (local-prev);
719         //      prev = local;
720         //      if (local > due) {
721         //              if (count == 5) {
722         //                      static char str[] = "background background background\r\n";
723         //                      sirq_write(sirq_dbg, str, sizeof(str));
724         //              }
725         //              if (count == 9) {
726         //                      sirq_printf("background\r\n");
727         //                      for (int i = 0; i < 10; i++) {
728         //                              sirq_printf("  worst[%d] = 0.%09u\r\n",
729         //                                              i, worst[i]);
730         //                              worst[i] = 0;
731         //                      }
732         //              }
733         //              due += NSEC_PER_SEC;
734         //              count = (count + 1) % 10;
735         //      }
736         //}
737
738         // Run tests
739         //test_main();
740
741         return 0;
742 }