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