]> Pileus Git - ~andy/csm213a-hw/commitdiff
Update testing code
authorAndy Spencer <andy753421@gmail.com>
Mon, 10 Mar 2014 02:24:16 +0000 (02:24 +0000)
committerAndy Spencer <andy753421@gmail.com>
Mon, 10 Mar 2014 02:24:16 +0000 (02:24 +0000)
hw2/main.cpp

index 82f28f75a3d4e62bd722cd69459bab3120969cb1..108000ee6dfbf42a81bf5bb47f2181498d9f7f6d 100644 (file)
@@ -86,12 +86,25 @@ void time_ext_init(uint64_t local, uint64_t world)
  */
 void time_ext_sync(uint64_t local, uint64_t world)
 {
  */
 void time_ext_sync(uint64_t local, uint64_t world)
 {
-       sirq_printf("syncing clocks: %d -> %d\r\n",
-                       (int)(local/NSEC_PER_SEC),
-                       (int)(world/NSEC_PER_SEC));
+       uint64_t guess = time_to_world(local);
+       uint64_t error = world > guess ? world - guess :
+                        guess > world ? guess - world : 0;
+       int      ahead = guess > world;
 
        time_last_local = local;
 
        time_last_local = local;
-       time_last_world = world;
+       time_last_world = guess/2 + world/2;
+
+       world = time_last_world;
+
+       sirq_printf("syncing clocks: %d=%d.%04u -> %d.%04u (err: %s%ld.%09lu)\r\n",
+                       (int)((local / NSEC_PER_SEC)),
+                       (int)((guess / NSEC_PER_SEC)),
+                       (int)((guess % NSEC_PER_SEC)/(NSEC_PER_SEC/10000)),
+                       (int)((world / NSEC_PER_SEC)),
+                       (int)((world % NSEC_PER_SEC)/(NSEC_PER_SEC/10000)),
+                       ahead ? "-" : "",
+                       (int32_t )(error / (int64_t)NSEC_PER_SEC),
+                       (uint32_t)(error % (int64_t)NSEC_PER_SEC));
 }
 
 /************************
 }
 
 /************************
@@ -104,7 +117,7 @@ typedef struct {
        uint8_t  buffer[256];
 } parser_t;
 
        uint8_t  buffer[256];
 } parser_t;
 
-const  uint64_t serial_sync_delay = NSEC_PER_SEC * 2; // 1hz
+const  uint64_t serial_sync_delay = NSEC_PER_SEC / 10; // 1hz
 static uint64_t serial_sync_due   = 0;
 
 static tdma_t  *serial_tdma_rcv   = NULL;
 static uint64_t serial_sync_due   = 0;
 
 static tdma_t  *serial_tdma_rcv   = NULL;
@@ -148,7 +161,7 @@ void serial_send_sync(sirq_t *port, uint64_t now)
        serial_sync_due  = 0;
 
        // Debug
        serial_sync_due  = 0;
 
        // Debug
-       sirq_printf("sync msg transmit\r\n");
+       //sirq_printf("sync msg transmit\r\n");
 
        // save transmit time
        for (int i = 0; i < 1000; i++)
 
        // save transmit time
        for (int i = 0; i < 1000; i++)
@@ -156,6 +169,7 @@ void serial_send_sync(sirq_t *port, uint64_t now)
        int valid = tdma_stamp(serial_tdma_xmt, &serial_xmt_local);
        if (!valid)
                sirq_printf("missing sync transmit time\r\n");
        int valid = tdma_stamp(serial_tdma_xmt, &serial_xmt_local);
        if (!valid)
                sirq_printf("missing sync transmit time\r\n");
+       tdma_reset(serial_tdma_rcv);
        tdma_reset(serial_tdma_xmt);
 }
 
        tdma_reset(serial_tdma_xmt);
 }
 
@@ -172,7 +186,7 @@ void serial_send_event(uint16_t event, uint64_t local)
        time.seconds = (uint32_t)(world / NSEC_PER_SEC);
        time.nanosec = (uint32_t)(world % NSEC_PER_SEC);
 
        time.seconds = (uint32_t)(world / NSEC_PER_SEC);
        time.nanosec = (uint32_t)(world % NSEC_PER_SEC);
 
-       sirq_printf("event received - %08x:%08x - %u.%u\r\n",
+       sirq_printf("event received - %08x:%08x - %u.%09u\r\n",
                (uint32_t)(local >> 32), (uint32_t)local,
                time.seconds, time.nanosec);
        // todo
                (uint32_t)(local >> 32), (uint32_t)local,
                time.seconds, time.nanosec);
        // todo
@@ -189,6 +203,7 @@ void serial_handle_sync(sync_msg_t *msg)
        if (!valid)
                sirq_printf("missing sync receive time\r\n");
        tdma_reset(serial_tdma_rcv);
        if (!valid)
                sirq_printf("missing sync receive time\r\n");
        tdma_reset(serial_tdma_rcv);
+       tdma_reset(serial_tdma_xmt);
 
        // Lookup times
        uint64_t world = ((uint64_t)msg->time.seconds) * NSEC_PER_SEC
 
        // Lookup times
        uint64_t world = ((uint64_t)msg->time.seconds) * NSEC_PER_SEC
@@ -228,11 +243,11 @@ void serial_deliver(int msgid, void *body)
 {
        switch (msgid) {
                case MSG_ID_SYNC:
 {
        switch (msgid) {
                case MSG_ID_SYNC:
-                       sirq_printf("received sync msg\r\n");
+                       //sirq_printf("received sync msg\r\n");
                        serial_handle_sync((sync_msg_t*)body);
                        break;
                case MSG_ID_EVENT:
                        serial_handle_sync((sync_msg_t*)body);
                        break;
                case MSG_ID_EVENT:
-                       sirq_printf("received event msg\r\n");
+                       //sirq_printf("received event msg\r\n");
                        serial_handle_event((event_msg_t*)body);
                        break;
        }
                        serial_handle_event((event_msg_t*)body);
                        break;
        }
@@ -349,7 +364,7 @@ void task_events(uint64_t local, uint64_t world)
 
 void task_sync(uint64_t local, uint64_t world)
 {
 
 void task_sync(uint64_t local, uint64_t world)
 {
-       serial_send_sync(sirq_bbb, local);
+       serial_send_sync(sirq_mbed, local);
 }
 
 void task_leds(uint64_t local, uint64_t world)
 }
 
 void task_leds(uint64_t local, uint64_t world)
@@ -363,11 +378,13 @@ void task_leds(uint64_t local, uint64_t world)
 void task_debug(uint64_t local, uint64_t world)
 {
        //tdma_debug(tdma_rcv);
 void task_debug(uint64_t local, uint64_t world)
 {
        //tdma_debug(tdma_rcv);
-       sirq_printf("background - %6d.%02d -> %d.%02d\r\n",
+#ifdef VERBOSE
+       sirq_printf("background - %6u.%02u -> %u.%02u\r\n",
                        (uint32_t)(local / NSEC_PER_SEC),
                        (uint32_t)(local % NSEC_PER_SEC / 10000000),
                        (uint32_t)(world / NSEC_PER_SEC),
                        (uint32_t)(world % NSEC_PER_SEC / 10000000));
                        (uint32_t)(local / NSEC_PER_SEC),
                        (uint32_t)(local % NSEC_PER_SEC / 10000000),
                        (uint32_t)(world / NSEC_PER_SEC),
                        (uint32_t)(world % NSEC_PER_SEC / 10000000));
+#endif
 }
 
 /********
 }
 
 /********
@@ -412,8 +429,8 @@ int main(int argc, char **argv)
 
        // Open serial ports
        sirq_dbg   = sirq_open(SIRQ_UART0, USBTX, USBRX, 115200); // to pc
 
        // Open serial ports
        sirq_dbg   = sirq_open(SIRQ_UART0, USBTX, USBRX, 115200); // to pc
-       sirq_bbb   = sirq_open(SIRQ_UART1, PTD3,  PTD2,  115200); // to bbb
-       sirq_mbed  = sirq_open(SIRQ_UART2, PTE0,  PTE1,  115200); // to mbed
+       sirq_bbb   = sirq_open(SIRQ_UART1, PTE0,  PTE1,  115200); // to bbb
+       sirq_mbed  = sirq_open(SIRQ_UART2, PTD3,  PTD2,  115200); // to mbed
 
 
        // Setup timers
 
 
        // Setup timers