-#include "mbed.h"\r
-\r
-/**\r
- * Mode of operation:\r
- * Devices 1 and 2 synchronize clocks using serial messages.\r
- *\r
- * 1. Each serial message timestamped using the hardware timer capture\r
- * registers in both the sender and receiver.\r
- * 2. The sender transmits the send timestamp during the next time-sync\r
- * message.\r
- * 3. The receiver then compares the senders timestamp with it's own\r
- * timestamp for the corresponding messages and calculates an offset.\r
- * 4. The offset is used to compensate the receivers local clock.\r
- *\r
- * Time synchronization is performed in both directions.\r
- */\r
-\r
-/***********************\r
- * Message Definitions *\r
- ***********************/\r
-\r
-#define MSG_HEADER 0x1234\r
-\r
-typedef enum {\r
- MSG_ID_SYNC, // Time synchronization\r
- MSG_ID_EVENT, // Event occurred\r
-} msgid_t;\r
-\r
-typedef struct {\r
- uint32_t seconds; // Seconds since 1970 (without leap seconds)\r
- uint32_t nanosec; // Nanoseconds since 'seconds'\r
-} ntime_t;\r
-\r
-typedef struct {\r
- uint16_t header; // Message Header\r
- uint16_t mesgid; // Message ID\r
- uint16_t length; // Body length\r
- uint16_t cksum; // Body checksum\r
-} header_t;\r
-\r
-typedef struct {\r
- uint16_t seq; // Current sequence counter\r
- uint16_t prev; // Sequence of previous message\r
- ntime_t time; // Time of previous message\r
-} sync_msg_t;\r
-\r
-typedef struct {\r
- uint16_t device; // Device ID\r
- uint16_t event; // Event ID\r
- ntime_t time; // Timestamp\r
-} event_msg_t;\r
-\r
-/*******************\r
- * Timer functions *\r
- *******************/\r
-\r
-/**\r
- * Generate time stamp for an async event:\r
- * time: drift compensated wall-clock time\r
- * stamp: event timestamp from Timer/PWM Module\r
- */\r
-void time_stamp(ntime_t *time, uint32_t stamp)\r
-{\r
- // todo\r
-}\r
-\r
-/**\r
- * Compensate the Real-Time-Clock oscillator for\r
- * temperature and drift errors. Called at 1Hz and\r
- * synchronous to the RTC 1Hz output.\r
- */\r
-void time_rtc_comp(void)\r
-{\r
- // todo\r
-}\r
-\r
-/**\r
- * Synchronize the timer internal state with updates\r
- * from an external time sync message.\r
- * ours: our internal timestamp for the event\r
- * ref: reference timestamp from the other device\r
- */\r
-void time_ext_sync(ntime_t *ours, ntime_t *ref)\r
-{\r
- // todo\r
-}\r
-\r
-/************************\r
- * Serial I/O functions *\r
- ************************/\r
-\r
-/**\r
- * Output time sync message\r
- */\r
-void serial_send_sync(void)\r
-{\r
-}\r
-\r
-/**\r
- * Output external event received message\r
- * event: id of the received event\r
- * time: compensated timestamp of the event \r
- */\r
-void serial_send_event(uint16_t event, ntime_t *time)\r
-{\r
-}\r
-\r
-/**\r
- * Process serial receive messages\r
- */\r
-void serial_receive(void)\r
-{\r
-}\r
-\r
-/********************\r
- * Data definitions *\r
- ********************/\r
-\r
-DigitalOut led1(LED1);\r
-DigitalOut led2(LED2);\r
-\r
-/********\r
- * Main *\r
- ********/\r
-\r
-int main(int argc, char **argv)\r
-{\r
- while (1) {\r
- led1 = 1; led2 = 0; wait(0.1);\r
- led1 = 0; led2 = 1; wait(0.1);\r
- }\r
-}\r
+#include "messages.h"
+
+#include "mbed.h"
+#include "serial_irq.h"
+#include "serial_dma.h"
+#include "timer_dma.h"
+
+#include "main_time.h"
+#include "main_emit.h"
+#include "main_comm.h"
+
+/**
+ * Mode of operation:
+ * Devices 1 and 2 synchronize clocks using serial messages.
+ *
+ * 1. Each serial message timestamped using the hardware timer capture
+ * registers in both the sender and receiver.
+ * 2. The sender transmits the send timestamp during the next time-sync
+ * message.
+ * 3. The receiver then compares the senders timestamp with it's own
+ * timestamp for the corresponding messages and calculates an offset.
+ * 4. The offset is used to compensate the receivers local clock.
+ *
+ * Time synchronization is performed in both directions.
+ */
+
+/*******************
+ * Tasks Scheduler *
+ *******************/
+
+// Macros
+#define N_ELEM(x) (sizeof(x) / sizeof((x)[0]))
+
+// Task entry
+typedef struct {
+ void (*task)(uint64_t, uint64_t);
+ uint64_t period;
+ uint64_t due;
+} task_t;
+
+// Task functions
+void task_serial(uint64_t local, uint64_t world);
+void task_events(uint64_t local, uint64_t world);
+void task_sync(uint64_t local, uint64_t world);
+void task_leds(uint64_t local, uint64_t world);
+void task_emit(uint64_t local, uint64_t world);
+void task_debug(uint64_t local, uint64_t world);
+
+// Task table
+task_t tasks[] = {
+ { task_serial, 0 }, // always
+ { task_events, 0 }, // always -- testing
+ { task_sync, 0 }, // always
+ { task_emit, 0 }, // always
+ { task_leds, 100000000 }, // 10hz
+ { task_debug, 1000000000 }, // 1hz
+};
+
+/********************
+ * Data definitions *
+ ********************/
+
+// LEDs
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+
+// Message Parsers
+parser_t parser_dbg;
+parser_t parser_bbb;
+parser_t parser_mbed;
+
+// Serial IRQ
+sirq_t *sirq_dbg;
+sirq_t *sirq_bbb;
+sirq_t *sirq_mbed;
+
+// Timer DMA
+tdma_t *tdma_evt;
+tdma_t *tdma_rcv;
+tdma_t *tdma_xmt;
+
+/*********
+ * Tasks *
+ *********/
+
+void task_serial(uint64_t local, uint64_t world)
+{
+ while (sirq_ready(sirq_dbg)) {
+ //sirq_printf("serial recv - dbg\r\n");
+ msg_receive(&parser_dbg, sirq_getc(sirq_dbg));
+ }
+
+ while (sirq_ready(sirq_bbb)) {
+ //sirq_printf("serial recv - bbb\r\n");
+ msg_receive(&parser_bbb, sirq_getc(sirq_bbb));
+ }
+
+ while (sirq_ready(sirq_mbed)) {
+ //sirq_printf("serial recv - mbed\r\n");
+ msg_receive(&parser_mbed, sirq_getc(sirq_mbed));
+ }
+}
+
+void task_events(uint64_t local, uint64_t world)
+{
+ uint64_t event = 0;
+
+#ifdef VERBOSE
+ if (tdma_stamp(tdma_evt, &event)) {
+ sirq_printf("event received - evt\r\n");
+ if (tdma_stamp(tdma_rcv, &event))
+ sirq_printf("event received - rcv\r\n");
+ if (tdma_stamp(tdma_xmt, &event))
+ sirq_printf("event received - xmt\r\n");
+#endif
+
+ if (tdma_stamp(tdma_evt, &event))
+ comm_send_event(0, event);
+ tdma_stop(tdma_evt, 0);
+ tdma_start(tdma_evt);
+}
+
+void task_sync(uint64_t local, uint64_t world)
+{
+ comm_send_sync(local);
+}
+
+void task_leds(uint64_t local, uint64_t world)
+{
+ static uint32_t which = 0;
+ led1 = (which == 0);
+ led2 = (which == 1);
+ which ^= 1;
+}
+
+void task_emit(uint64_t local, uint64_t world)
+{
+ emit_transmit(local, world);
+}
+
+void task_debug(uint64_t local, uint64_t world)
+{
+ //tdma_debug(tdma_rcv);
+ //tdma_debug(tdma_xmt);
+
+ //sirq_debug(sirq_mbed);
+
+ comm_send_event(1, local);
+
+#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));
+#endif
+}
+
+/********
+ * Main *
+ ********/
+
+void background(void)
+{
+ // Debugging
+ uint64_t local = tdma_time();
+ uint64_t world = time_to_world(local);
+
+ // Run the scheduler
+ for (unsigned i = 0; i < N_ELEM(tasks); i++) {
+ if (local >= tasks[i].due) {
+ tasks[i].task(local, world);
+ tasks[i].due += tasks[i].period;
+ }
+ }
+}
+
+int main(int argc, char **argv)
+{
+ // Open serial ports
+ sirq_dbg = sirq_open(SIRQ_UART0, USBTX, USBRX, 115200, 0); // to pc
+ sirq_bbb = sirq_open(SIRQ_UART1, PTE0, PTE1, 115200, 0); // to bbb
+ sirq_mbed = sirq_open(SIRQ_UART2, PTD3, PTD2, 115200, 1); // to mbed
+
+ // Setup timers
+ tdma_evt = tdma_open(TDMA_CHAN0, 3, PTC9, PullDown); // async event
+ tdma_rcv = tdma_open(TDMA_CHAN2, 3, PTD2, PullUp); // time sync rcv
+ tdma_xmt = tdma_open(TDMA_CHAN3, 3, PTD3, PullUp); // time sync xmt
+
+ // Setup event generation
+ time_init();
+ emit_init(3, PTE20, PullDown);
+ comm_init(sirq_dbg, sirq_bbb, sirq_mbed,
+ tdma_rcv, tdma_xmt);
+
+ // Register messages
+ msg_register(&parser_dbg, MSG_ID_INIT, (handler_t)comm_handle_init);
+ msg_register(&parser_dbg, MSG_ID_SYNC, (handler_t)comm_handle_sync);
+ msg_register(&parser_dbg, MSG_ID_EVENT, (handler_t)comm_handle_event);
+
+ msg_register(&parser_bbb, MSG_ID_INIT, (handler_t)comm_handle_init);
+
+ msg_register(&parser_mbed, MSG_ID_INIT, (handler_t)comm_handle_init);
+ msg_register(&parser_mbed, MSG_ID_SYNC, (handler_t)comm_handle_sync);
+ msg_register(&parser_mbed, MSG_ID_EVENT, (handler_t)comm_handle_event);
+
+ // start timers
+ tdma_start(tdma_evt);
+ tdma_start(tdma_rcv);
+ tdma_start(tdma_xmt);
+
+ // Run background loop
+ while (true)
+ background();
+
+ return 0;
+}