2 #include "serial_dma.h"
7 * Devices 1 and 2 synchronize clocks using serial messages.
9 * 1. Each serial message timestamped using the hardware timer capture
10 * registers in both the sender and receiver.
11 * 2. The sender transmits the send timestamp during the next time-sync
13 * 3. The receiver then compares the senders timestamp with it's own
14 * timestamp for the corresponding messages and calculates an offset.
15 * 4. The offset is used to compensate the receivers local clock.
17 * Time synchronization is performed in both directions.
20 /***********************
21 * Message Definitions *
22 ***********************/
24 #define MSG_HEADER 0x1234
27 MSG_ID_SYNC, // Time synchronization
28 MSG_ID_EVENT, // Event occurred
32 uint32_t seconds; // Seconds since 1970 (without leap seconds)
33 uint32_t nanosec; // Nanoseconds since 'seconds'
37 uint16_t header; // Message Header
38 uint16_t mesgid; // Message ID
39 uint16_t length; // Body length
40 uint16_t cksum; // Body checksum
44 uint16_t seq; // Current sequence counter
45 uint16_t prev; // Sequence of previous message
46 ntime_t time; // Time of previous message
50 uint16_t device; // Device ID
51 uint16_t event; // Event ID
52 ntime_t time; // Timestamp
60 * Generate time stamp for an async event:
61 * time: drift compensated wall-clock time
62 * stamp: event timestamp from Timer/PWM Module
64 void time_stamp(ntime_t *time, uint32_t stamp)
70 * Compensate the Real-Time-Clock oscillator for
71 * temperature and drift errors. Called at 1Hz and
72 * synchronous to the RTC 1Hz output.
74 void time_rtc_comp(void)
80 * Synchronize the timer internal state with updates
81 * from an external time sync message.
82 * ours: our internal timestamp for the event
83 * ref: reference timestamp from the other device
85 void time_ext_sync(ntime_t *ours, ntime_t *ref)
90 /************************
91 * Serial I/O functions *
92 ************************/
95 * Output time sync message
97 void serial_send_sync(void)
102 * Output external event received message
103 * event: id of the received event
104 * time: compensated timestamp of the event
106 void serial_send_event(uint16_t event, ntime_t *time)
111 * Process serial receive messages
113 void serial_receive(void)
117 /********************
119 ********************/
122 DigitalOut led1(LED1);
123 DigitalOut led2(LED2);
126 Serial uart0(USBTX, USBRX);
127 Serial uart1(PTE0, PTE1);
128 Serial uart2(PTD3, PTD2);
145 void test_main(void);
147 int main(int argc, char **argv)