#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 * ********************/ // LEDs DigitalOut led1(LED1); DigitalOut led2(LED2); // UARTs tx rx Serial uart0(USBTX, USBRX); Serial uart1(PTE0, PTE1); Serial uart2(PTE16, PTE17); /******** * Main * ********/ void test_uart(void) { char xmt[32] = "hello, world"; char rcv[32] = {}; printf("start\r\n"); for (int i = 0; xmt[i]; i++) { uart1.putc(xmt[i]); rcv[i] = uart2.getc(); } printf("xmt: %s\r\n", xmt); printf("rcv: %s\r\n", rcv); } void test_leds(void) { led1 = 1; led2 = 0; wait(0.1); led1 = 0; led2 = 1; wait(0.1); } int main(int argc, char **argv) { uart0.baud(115200); uart1.baud(115200); uart2.baud(115200); test_uart(); test_leds(); while (1) { printf("tick\r\n"); test_leds(); } }