]> Pileus Git - ~andy/csm213a-hw/commitdiff
Fix event scheduling bug
authorAndy Spencer <andy753421@gmail.com>
Sat, 15 Mar 2014 21:55:50 +0000 (21:55 +0000)
committerAndy Spencer <andy753421@gmail.com>
Sat, 15 Mar 2014 21:55:50 +0000 (21:55 +0000)
hw2/main_comm.c
hw2/main_emit.c

index d5c66b168c7a66f1cc89ff7988cf6ee4245dd359..118f6b3cd42f2499528829e5568fd7c1f00a490e 100644 (file)
@@ -228,12 +228,6 @@ void comm_handle_init(header_t *head, init_msg_t *body)
        if (body->control & MSG_CTL_VALID_DEVICE)
                comm_device_id = body->device;
 
        if (body->control & MSG_CTL_VALID_DEVICE)
                comm_device_id = body->device;
 
-       if (body->control & MSG_CTL_VALID_START)
-               emit_set_start(comm_read_time(body->start));
-
-       if (body->control & MSG_CTL_VALID_PERIOD)
-               emit_set_period(comm_read_time(body->period));
-
        if (body->control & MSG_CTL_VALID_WORLD)
                time_ext_init(tdma_time(), comm_read_time(body->world));
 
        if (body->control & MSG_CTL_VALID_WORLD)
                time_ext_init(tdma_time(), comm_read_time(body->world));
 
@@ -244,6 +238,13 @@ void comm_handle_init(header_t *head, init_msg_t *body)
 
        if (body->control & MSG_CTL_BEGIN_SYNC)
                comm_sync_due = tdma_time() + comm_sync_delay;
 
        if (body->control & MSG_CTL_BEGIN_SYNC)
                comm_sync_due = tdma_time() + comm_sync_delay;
+
+       // Run these after world time is valid
+       if (body->control & MSG_CTL_VALID_START)
+               emit_set_start(comm_read_time(body->start));
+
+       if (body->control & MSG_CTL_VALID_PERIOD)
+               emit_set_period(comm_read_time(body->period));
 }
 
 /**
 }
 
 /**
index bb751a65cd04614a0753514239dbdb3af1bdb79f..e7a8bac2851666598b3055402479829dec9f421d 100644 (file)
@@ -57,15 +57,14 @@ static void emit_setup(void)
        //      |       @  |       @  |       @  |
        //  utc-^                last-^ now-^
        if (emit_period) {
        //      |       @  |       @  |       @  |
        //  utc-^                last-^ now-^
        if (emit_period) {
-               uint64_t now   = tdma_time();
+               uint64_t now   = time_to_world(tdma_time());
                uint64_t phase = emit_start % emit_period;
                uint64_t last  = (now / emit_period) * emit_period;
                emit_due = last + phase;
                uint64_t phase = emit_start % emit_period;
                uint64_t last  = (now / emit_period) * emit_period;
                emit_due = last + phase;
+               time_printf("emit scheduled", emit_due);
        } else {
                emit_due = 0;
        }
        } else {
                emit_due = 0;
        }
-
-       time_printf("emit scheduled", emit_due);
 }
 
 void emit_init(int alt, PinName pin, PinMode mode)
 }
 
 void emit_init(int alt, PinName pin, PinMode mode)
@@ -192,12 +191,15 @@ void emit_transmit(uint64_t local, uint64_t world)
        prev = local;
 
        // Schedule task if needed
        prev = local;
 
        // Schedule task if needed
-       if (emit_due && emit_period &&
-           world+emit_slack > emit_due) {
+       if (emit_period && world+emit_slack > emit_due) {
                emit_schedule(emit_due);
                emit_due += emit_period;
                emit_schedule(emit_due);
                emit_due += emit_period;
-               if (emit_due < local)
-                       sirq_printf("missed emit deadline\r\n");
+       }
+       if (emit_period && emit_due < world) {
+               sirq_printf("missed emit deadline\r\n");
+               time_printf("  due   ", emit_due);
+               time_printf("  period", emit_period);
+               time_printf("  world ", world);
+               emit_setup();
        }
 }
        }
 }
-