-#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
-// LEDs\r
-DigitalOut led1(LED1);\r
-DigitalOut led2(LED2);\r
-\r
-// UARTs tx rx\r
-Serial uart0(USBTX, USBRX);\r
-Serial uart1(PTE0, PTE1);\r
-Serial uart2(PTE16, PTE17);\r
-\r
-/********\r
- * Main *\r
- ********/\r
-\r
-void test_uart(void)\r
-{\r
- char xmt[32] = "hello, world";\r
- char rcv[32] = {};\r
-\r
- printf("start\r\n");\r
- for (int i = 0; xmt[i]; i++) {\r
- uart1.putc(xmt[i]);\r
- rcv[i] = uart2.getc();\r
- }\r
- printf("xmt: %s\r\n", xmt);\r
- printf("rcv: %s\r\n", rcv);\r
-}\r
-\r
-void test_leds(void)\r
-{\r
- led1 = 1; led2 = 0; wait(0.1);\r
- led1 = 0; led2 = 1; wait(0.1);\r
-}\r
-\r
-int main(int argc, char **argv)\r
-{\r
- uart0.baud(115200);\r
- uart1.baud(115200);\r
- uart2.baud(115200);\r
-\r
- test_uart();\r
- test_leds();\r
-\r
- while (1) {\r
- printf("tick\r\n");\r
- test_leds();\r
- }\r
-}\r
+#include "messages.h"
+
+#include "mbed.h"
+#include "serial_irq.h"
+#include "serial_dma.h"
+#include "timer_dma.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.
+ */
+
+/*******************
+ * Timer functions *
+ *******************/
+
+#define NSEC_PER_SEC 1000000000ULL
+
+uint64_t time_last_local; // timestamp at last time sync
+uint64_t time_last_world; // offset at last time sync
+
+/**
+ * Generate time stamp for an async event:
+ * local: drift compensated wall-clock time
+ * world: nanoseconds in world time world
+ * valid: local timestamp at valid valid
+ */
+//uint64_t time_to_local(uint64_t world, uint64_t valid)
+//{
+// uint64_t now =
+// local = + (stamp);
+//}
+
+/**
+ * Generate time stamp for an async event:
+ * time: drift compensated wall-clock time
+ * stamp: event timestamp from PIT Module
+ */
+uint64_t time_to_world(uint64_t local)
+{
+ uint64_t elapsed = local - time_last_local;
+ return time_last_world + elapsed;
+}
+
+/**
+ * Compensate the Real-Time-Clock oscillator for
+ * temperature and drift errors. Called at 1Hz and
+ * synchronous to the RTC 1Hz output.
+ */
+void time_rtc_comp(void)
+{
+ // todo
+}
+
+/**
+ * Synchronize the timer internal state with updates
+ * from an external time sync message.
+ * local: our internal timestamp for the event
+ * world: reference timestamp from the other device
+ */
+void time_ext_init(uint64_t local, uint64_t world)
+{
+ sirq_printf("initialize clocks: %d -> %d\r\n",
+ (int)(local/NSEC_PER_SEC),
+ (int)(world/NSEC_PER_SEC));
+
+ time_last_local = local;
+ time_last_world = world;
+}
+
+/**
+ * Synchronize the timer internal state with updates
+ * from an external time sync message.
+ * local: our internal timestamp for the event
+ * world: reference timestamp from the other device
+ */
+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));
+
+ time_last_local = local;
+ time_last_world = world;
+}
+
+/************************
+ * Serial I/O functions *
+ ************************/
+
+typedef struct {
+ int index;
+ int state;
+ uint8_t buffer[256];
+} parser_t;
+
+const uint64_t serial_sync_delay = NSEC_PER_SEC * 2; // 1hz
+static uint64_t serial_sync_due = 0;
+
+static tdma_t *serial_tdma_rcv = NULL;
+static tdma_t *serial_tdma_xmt = NULL;
+
+static uint64_t serial_prev_local = 0;
+static uint64_t serial_prev_seq = 0;
+
+static uint64_t serial_xmt_local = 0;
+static uint64_t serial_xmt_seq = 0;
+
+/**
+ * Output time sync message
+ */
+void serial_send_sync(sirq_t *port, uint64_t now)
+{
+ if (serial_sync_due == 0 || now < serial_sync_due)
+ return; // not ready
+
+ // Calculate world time
+ uint64_t world = time_to_world(serial_xmt_local);
+
+ // Message data
+ header_t head;
+ sync_msg_t body;
+
+ // Transmit sync message
+ head.header = MSG_HEADER;
+ head.msgid = MSG_ID_SYNC;
+ head.length = sizeof(body);
+ head.cksum = 0; // todo
+
+ body.seq = serial_xmt_seq;
+ body.time.seconds = world / NSEC_PER_SEC;
+ body.time.nanosec = world % NSEC_PER_SEC;
+
+ sirq_write(port, &head, sizeof(head));
+ sirq_write(port, &body, sizeof(body));
+
+ serial_xmt_seq += 1;
+ serial_sync_due = 0;
+
+ // Debug
+ sirq_printf("sync msg transmit\r\n");
+
+ // save transmit time
+ for (int i = 0; i < 1000; i++)
+ asm("nop");
+ int valid = tdma_stamp(serial_tdma_xmt, &serial_xmt_local);
+ if (!valid)
+ sirq_printf("missing sync transmit time\r\n");
+ tdma_reset(serial_tdma_xmt);
+}
+
+/**
+ * Output external event received message
+ * event: id of the received event
+ * time: compensated timestamp of the event
+ */
+void serial_send_event(uint16_t event, uint64_t local)
+{
+ uint64_t world = time_to_world(local);
+
+ ntime_t time = {};
+ 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",
+ (uint32_t)(local >> 32), (uint32_t)local,
+ time.seconds, time.nanosec);
+ // todo
+}
+
+/**
+ * Handle sync message
+ */
+void serial_handle_sync(sync_msg_t *msg)
+{
+ // Read receive timestamp for next time sync message
+ uint64_t current = 0;
+ int valid = tdma_stamp(serial_tdma_rcv, ¤t);
+ if (!valid)
+ sirq_printf("missing sync receive time\r\n");
+ tdma_reset(serial_tdma_rcv);
+
+ // Lookup times
+ uint64_t world = ((uint64_t)msg->time.seconds) * NSEC_PER_SEC
+ + ((uint64_t)msg->time.nanosec);
+
+ // Initialize
+ if (msg->seq == 0) {
+ uint64_t local = tdma_time();
+ time_ext_init(local, world);
+ }
+
+ // Valid times timestamp
+ if (serial_prev_seq == (msg->seq-1)) {
+ uint64_t local = serial_prev_local;
+ time_ext_sync(local, world);
+ }
+
+ // Queue transmit to other board
+ serial_sync_due = tdma_time() + serial_sync_delay;
+
+ // Update states
+ serial_prev_local = current;
+ serial_prev_seq = msg->seq;
+}
+
+/**
+ * Handle event message
+ */
+void serial_handle_event(event_msg_t *msg)
+{
+}
+
+/**
+ * Deliver message
+ */
+void serial_deliver(int msgid, void *body)
+{
+ switch (msgid) {
+ case MSG_ID_SYNC:
+ sirq_printf("received sync msg\r\n");
+ serial_handle_sync((sync_msg_t*)body);
+ break;
+ case MSG_ID_EVENT:
+ sirq_printf("received event msg\r\n");
+ serial_handle_event((event_msg_t*)body);
+ break;
+ }
+}
+
+/**
+ * Process serial receive messages
+ */
+void serial_receive(parser_t *parser, int byte)
+{
+ //sirq_printf("serial_receive - %02x\r\n", byte);
+
+ // Lookup pointers
+ header_t *head = (header_t*)parser->buffer;
+ void *body = (void*)(head+1);
+ const int max_length = sizeof(parser->buffer)-sizeof(header_t);
+
+ // Process uart messages
+ parser->buffer[parser->index++] = byte;
+ switch (parser->state) {
+ case 0: // Search
+ if (parser->index == sizeof(uint16_t)) {
+ if (head->header == MSG_HEADER) {
+ parser->state = 1;
+ } else {
+ parser->buffer[0] = parser->buffer[1];
+ parser->index = 1;
+ }
+ }
+ break;
+ case 1: // Header
+ if (parser->index == sizeof(header_t)) {
+ if (head->length <= max_length &&
+ head->msgid <= MSG_MAXID) {
+ parser->state = 2;
+ } else {
+ parser->index = 0;
+ parser->state = 0;
+ }
+ }
+ break;
+ case 2: // Data
+ if (parser->index == (int)sizeof(header_t)+head->length) {
+ serial_deliver(head->msgid, body);
+ parser->index = 0;
+ parser->state = 0;
+ }
+ break;
+ }
+}
+
+/********************
+ * 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");
+ serial_receive(&parser_dbg, sirq_getc(sirq_dbg));
+ }
+
+ while (sirq_ready(sirq_bbb)) {
+ //sirq_printf("serial recv - bbb\r\n");
+ serial_receive(&parser_bbb, sirq_getc(sirq_bbb));
+ }
+
+ while (sirq_ready(sirq_mbed)) {
+ //sirq_printf("serial recv - mbed\r\n");
+ serial_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))
+ serial_send_event(0, event);
+ tdma_reset(tdma_evt);
+}
+
+void task_sync(uint64_t local, uint64_t world)
+{
+ serial_send_sync(sirq_bbb, 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_debug(uint64_t local, uint64_t world)
+{
+ //tdma_debug(tdma_rcv);
+ sirq_printf("background - %6d.%02d -> %d.%02d\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));
+}
+
+/********
+ * Main *
+ ********/
+
+#define N_ELEM(x) (sizeof(x) / sizeof((x)[0]))
+
+extern void test_main(void);
+extern serial_t stdio_uart;
+
+static struct {
+ void (*task)(uint64_t, uint64_t);
+ uint64_t period;
+ uint64_t due;
+} tasks[] = {
+ { task_serial, 0 }, // always
+ { task_events, 1000000000 }, // always
+ { task_sync, 0 }, // always
+ { task_leds, 100000000 }, // 10hz
+ { task_debug, 1000000000 }, // 1hz
+};
+
+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)
+{
+ tdma_init();
+
+ // 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
+
+
+ // Setup timers
+ tdma_evt = tdma_open(TDMA_CHAN0, 3, PTC9, PullDown); // async event
+
+ // mbed time sync
+ tdma_rcv = tdma_open(TDMA_CHAN2, 3, PTD2, PullUp); // time sync rcv
+ tdma_xmt = tdma_open(TDMA_CHAN3, 3, PTD3, PullUp); // time sync xmt
+
+ // host time sync
+ //tdma_rcv = tdma_open(TDMA_CHAN2, 2, USBRX, PullUp); // time sync rcv
+ //tdma_xmt = tdma_open(TDMA_CHAN3, 2, USBTX, PullUp); // time sync xmt
+
+ // Serial timestamping
+ serial_tdma_rcv = tdma_rcv;
+ serial_tdma_xmt = tdma_xmt;
+
+ // Run background loop
+ printf("hello");
+ while (true)
+ background();
+
+ // Run tests
+ //test_main();
+
+ return 0;
+}