]> Pileus Git - ~andy/csm213a-hw/blobdiff - hw2/main_comm.c
Fix event scheduling bug
[~andy/csm213a-hw] / hw2 / main_comm.c
index a9dbf7757baf97eb27d12629ec2003ab8d445ff5..118f6b3cd42f2499528829e5568fd7c1f00a490e 100644 (file)
@@ -9,7 +9,7 @@
 #include "main_emit.h"
 
 /**
- * Communcation overview:
+ * Communication overview:
  *
  *     Initialization:
  *        bbb --init1-->  mbed1
@@ -26,7 +26,7 @@
  * Initialization:
  *     Each mbed is initialized by the BBB by receiving an initialization
  *     message.  The Device ID must be non-zero, and is saved for future
- *     messages. If the device is already initialized and a recevied Device ID
+ *     messages. If the device is already initialized and a received Device ID
  *     does not match the configured Device ID, the messages is relayed to the
  *     second mbed.
  *
@@ -159,7 +159,7 @@ void comm_send_sync(uint64_t local)
  */
 void comm_send_event(uint16_t event, uint64_t local)
 {
-       //time_printf("event received", local);
+       time_printf("event received", local);
 
        // Convert timestamp
        uint64_t world = time_to_world(local);
@@ -217,7 +217,8 @@ void comm_handle_init(header_t *head, init_msg_t *body)
                body->control & MSG_CTL_VALID_START  ? "START"  : "start",
                body->control & MSG_CTL_VALID_PERIOD ? "PERIOD" : "period",
                body->control & MSG_CTL_VALID_WORLD  ? "WORLD"  : "world",
-               body->control & MSG_CTL_VALID_SYNC   ? "SYNC"   : "sync");
+               body->control & MSG_CTL_RELAY_MODE   ? "RELAY"  : "relay",
+               body->control & MSG_CTL_BEGIN_SYNC   ? "SYNC"   : "sync");
        sirq_printf("  dev    -- %d\r\n", body->device);
        time_printf("  start ", comm_read_time(body->start));
        time_printf("  period", comm_read_time(body->period));
@@ -227,26 +228,23 @@ 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_START ||
-           body->control & MSG_CTL_VALID_PERIOD) {
-               uint64_t start  = comm_read_time(body->start);
-               uint64_t period = comm_read_time(body->period);
-               emit_enable(start, period);
-       }
-
-       if (body->control & MSG_CTL_VALID_WORLD) {
-               uint64_t world = comm_read_time(body->world);
-               uint64_t local = tdma_time();
-               time_ext_init(local, world);
-       }
+       if (body->control & MSG_CTL_VALID_WORLD)
+               time_ext_init(tdma_time(), comm_read_time(body->world));
 
        if (body->control & MSG_CTL_RELAY_MODE)
                comm_relay_mode = 1;
        else
                comm_relay_mode = 0;
 
-       if (body->control & MSG_CTL_VALID_SYNC)
+       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));
 }
 
 /**