]> Pileus Git - ~andy/csm213a-hw/commitdiff
Make slave mode configurable
authorAndy Spencer <andy753421@gmail.com>
Sat, 15 Mar 2014 07:19:22 +0000 (07:19 +0000)
committerAndy Spencer <andy753421@gmail.com>
Sat, 15 Mar 2014 07:19:24 +0000 (07:19 +0000)
hw2/control.c
hw2/main_comm.c
hw2/makefile
hw2/messages.h
hw2/tester.cpp

index ea10f9b9cff340510b2489276baac3ce0d1521c0..bae3e98fdda288b459f53d54166b929b4df71541 100644 (file)
@@ -30,7 +30,8 @@ int main(int argc, char **argv)
        char *opt_tty    = argv[1];
        int   opt_device = atoi(argv[2]);
        int   opt_sync   = argv[3] && !strcmp(argv[3], "sync") ?
-                          MSG_VALID_SYNC : 0;
+                          MSG_CTL_VALID_SYNC : 0;
+       int   opt_relay  = opt_device == 2;
 
        // Lookup current wall-clock time
        struct timespec ts;
@@ -41,17 +42,18 @@ int main(int argc, char **argv)
        init_msg_t body = {};
 
        // Set message header
-       head.header = MSG_HEADER;
-       head.msgid  = MSG_ID_INIT;
-       head.length = sizeof(init_msg_t);
-       head.cksum  = 0; // todo
+       head.header  = MSG_HEADER;
+       head.msgid   = MSG_ID_INIT;
+       head.length  = sizeof(init_msg_t);
+       head.cksum   = 0; // todo
 
        // Set valid flags
-       body.valid  = MSG_VALID_DEVICE
-                   | MSG_VALID_START
-                   | MSG_VALID_PERIOD
-                   | MSG_VALID_WORLD
-                   | opt_sync;
+       body.control = MSG_CTL_VALID_DEVICE
+                    | MSG_CTL_VALID_START
+                    | MSG_CTL_VALID_PERIOD
+                    | MSG_CTL_VALID_WORLD
+                    | opt_relay
+                    | opt_sync;
 
        // Set message body
        body.device         = opt_device;
index 5e261b40e07bfe2cc8a9ad2106fb8dedacdd1b31..e42d48e8388a923afc32413ba65b01d5743cae37 100644 (file)
@@ -44,7 +44,8 @@
  * Communication functions *
  ***************************/
 
-static uint32_t comm_device_id   = 0;
+static int      comm_device_id   = 0;
+static int      comm_relay_mode  = 0;
 
 const  uint64_t comm_sync_delay  = NSEC_PER_SEC / 100;
 static uint64_t comm_sync_due    = 0;
@@ -214,34 +215,39 @@ void comm_handle_init(header_t *head, init_msg_t *body)
 
        // Debug output
        sirq_printf("initialize: %s %s %s %s %s\r\n",
-               body->valid & MSG_VALID_DEVICE ? "DEV"    : "dev",
-               body->valid & MSG_VALID_START  ? "START"  : "start",
-               body->valid & MSG_VALID_PERIOD ? "PERIOD" : "period",
-               body->valid & MSG_VALID_WORLD  ? "WORLD"  : "world",
-               body->valid & MSG_VALID_SYNC   ? "SYNC"   : "sync");
+               body->control & MSG_CTL_VALID_DEVICE ? "DEV"    : "dev",
+               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");
        sirq_printf("  dev    -- %d\r\n", body->device);
        time_printf("  start ", comm_read_time(body->start));
        time_printf("  period", comm_read_time(body->period));
        time_printf("  world ", comm_read_time(body->world));
 
        // Validate message parts and initialize
-       if (body->valid & MSG_VALID_DEVICE)
+       if (body->control & MSG_CTL_VALID_DEVICE)
                comm_device_id = body->device;
 
-       if (body->valid & MSG_VALID_START ||
-           body->valid & MSG_VALID_PERIOD) {
+       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->valid & MSG_VALID_WORLD) {
+       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->valid & MSG_VALID_SYNC)
+       if (body->control & MSG_CTL_RELAY_MODE)
+               comm_relay_mode = 1;
+       else
+               comm_relay_mode = 0;
+
+       if (body->control & MSG_CTL_VALID_SYNC)
                comm_sync_due = tdma_time() + comm_sync_delay;
 }
 
@@ -277,9 +283,7 @@ void comm_handle_sync(header_t *head, sync_msg_t *body)
  */
 void comm_handle_event(header_t *head, event_msg_t *body)
 {
-       // Relay event from mbed to bbb
-       if (comm_device_id == 1) {
-               sirq_write(comm_sirq_bbb, head, sizeof(*head));
-               sirq_write(comm_sirq_bbb, body, sizeof(*body));
-       }
+       // Relay events from other mbeds
+       sirq_write(comm_sirq_bbb, head, sizeof(*head));
+       sirq_write(comm_sirq_bbb, body, sizeof(*body));
 }
index d6dd6ec30339e3df3774e3eaaf44dabd72ca56d6..fef6181cf37059e3ce8b0b7a3eac18ed075289df 100644 (file)
@@ -26,7 +26,7 @@ mbed-run: mbed.bin control install.sh
        @./install.sh $<
        @./control $(UART0) 1
        @./control $(UART0) 2
-       @./control $(UART0) 1 sync
+       @./control $(UART0) 2 sync
 
 # Testing mbed (mbed3)
 tester.elf: tester.o serial_irq.o timer_dma.o messages.o
index 9f7dd712e9518bd7040635da21af9c5f4ca8de07..ef81129aedba89b27c281149cb69ca99e189b5b1 100644 (file)
@@ -11,13 +11,14 @@ extern "C" {
  * Message Definitions *
  ***********************/
 
-#define MSG_HEADER       0x1234
+#define MSG_HEADER           0x1234
 
-#define MSG_VALID_DEVICE 0x0001  // device id is valid
-#define MSG_VALID_WORLD  0x0002  // world time is valid
-#define MSG_VALID_START  0x0004  // start time is valid
-#define MSG_VALID_PERIOD 0x0008  // period is valid
-#define MSG_VALID_SYNC   0x8000  // begin time sync
+#define MSG_CTL_VALID_DEVICE 0x0001  // device id is valid
+#define MSG_CTL_VALID_WORLD  0x0002  // world time is valid
+#define MSG_CTL_VALID_START  0x0004  // start time is valid
+#define MSG_CTL_VALID_PERIOD 0x0008  // period is valid
+#define MSG_CTL_RELAY_MODE   0x4000  // relay output messages
+#define MSG_CTL_VALID_SYNC   0x8000  // begin time sync
 
 #pragma pack(1)
 
@@ -41,7 +42,7 @@ typedef struct {
 } header_t;
 
 typedef struct {
-       uint16_t valid;    // Message valid bits
+       uint16_t control;  // Message control bits
        uint16_t device;   // Device ID to use
        ntime_t  world;    // World time (since 1970)
        ntime_t  start;    // Transmit start time 
index 7e766fc6d6a0dcd0220ad66d16950057ffe77681..096b3366f28a0d0f6807b3b88a2a90ffced521e0 100644 (file)
@@ -55,7 +55,7 @@ int main(int arc, char **argv)
        serial_baud(&stdio_uart, 115200);
 
        // Setup BBB uart port
-       Sirq_Bbb   = sirq_open(SIRQ_UART1, PTE0,  PTE1,  115200);
+       Sirq_Bbb   = sirq_open(SIRQ_UART1, PTE0,  PTE1,  115200, 0);
 
        // Setup time tags
        Tdma_Mbed1 = tdma_open(TDMA_CHAN2, 3, PTD2, PullUp); // TODO -- Should be on PTC