From 38389d8fef88fe74944eb65e920616454fc1b36b Mon Sep 17 00:00:00 2001 From: Andy Spencer Date: Mon, 3 Mar 2014 17:54:44 +0000 Subject: [PATCH] Start work on interfaces --- hw2/main.cpp | 120 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) diff --git a/hw2/main.cpp b/hw2/main.cpp index a43bc8e..436f750 100644 --- a/hw2/main.cpp +++ b/hw2/main.cpp @@ -1,8 +1,128 @@ #include "mbed.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. + */ + +/*********************** + * Message Definitions * + ***********************/ + +#define MSG_HEADER 0x1234 + +typedef enum { + MSG_ID_SYNC, // Time synchronization + MSG_ID_EVENT, // Event occurred +} msgid_t; + +typedef struct { + uint32_t seconds; // Seconds since 1970 (without leap seconds) + uint32_t nanosec; // Nanoseconds since 'seconds' +} ntime_t; + +typedef struct { + uint16_t header; // Message Header + uint16_t mesgid; // Message ID + uint16_t length; // Body length + uint16_t cksum; // Body checksum +} header_t; + +typedef struct { + uint16_t seq; // Current sequence counter + uint16_t prev; // Sequence of previous message + ntime_t time; // Time of previous message +} sync_msg_t; + +typedef struct { + uint16_t device; // Device ID + uint16_t event; // Event ID + ntime_t time; // Timestamp +} event_msg_t; + +/******************* + * Timer functions * + *******************/ + +/** + * Generate time stamp for an async event: + * time: drift compensated wall-clock time + * stamp: event timestamp from Timer/PWM Module + */ +void time_stamp(ntime_t *time, uint32_t stamp) +{ + // todo +} + +/** + * 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. + * ours: our internal timestamp for the event + * ref: reference timestamp from the other device + */ +void time_ext_sync(ntime_t *ours, ntime_t *ref) +{ + // todo +} + +/************************ + * Serial I/O functions * + ************************/ + +/** + * Output time sync message + */ +void serial_send_sync(void) +{ +} + +/** + * Output external event received message + * event: id of the received event + * time: compensated timestamp of the event + */ +void serial_send_event(uint16_t event, ntime_t *time) +{ +} + +/** + * Process serial receive messages + */ +void serial_receive(void) +{ +} + +/******************** + * Data definitions * + ********************/ + DigitalOut led1(LED1); DigitalOut led2(LED2); +/******** + * Main * + ********/ + int main(int argc, char **argv) { while (1) { -- 2.43.2