]> Pileus Git - ~andy/csm213a-hw/blob - hw2/main.cpp
4f42dd26110b517d368c74bc9754b04b519a07ae
[~andy/csm213a-hw] / hw2 / main.cpp
1 #include "mbed.h"
2 #include "serial_dma.h"
3 #include "timer_dma.h"
4
5 /**
6  * Mode of operation:
7  *   Devices 1 and 2 synchronize clocks using serial messages.
8  *
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
12  *      message.
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.
16  *
17  *   Time synchronization is performed in both directions.
18  */
19
20 /***********************
21  * Message Definitions *
22  ***********************/
23
24 #define MSG_HEADER 0x1234
25
26 typedef enum {
27         MSG_ID_SYNC,       // Time synchronization
28         MSG_ID_EVENT,      // Event occurred
29 } msgid_t;
30
31 typedef struct {
32         uint32_t seconds;  // Seconds since 1970 (without leap seconds)
33         uint32_t nanosec;  // Nanoseconds since 'seconds'
34 } ntime_t;
35
36 typedef struct {
37         uint16_t header;   // Message Header
38         uint16_t mesgid;   // Message ID
39         uint16_t length;   // Body length
40         uint16_t cksum;    // Body checksum
41 } header_t;
42
43 typedef struct {
44         uint16_t seq;      // Current sequence counter
45         uint16_t prev;     // Sequence of previous message
46         ntime_t  time;     // Time of previous message
47 } sync_msg_t;
48
49 typedef struct {
50         uint16_t device;   // Device ID
51         uint16_t event;    // Event ID
52         ntime_t  time;     // Timestamp
53 } event_msg_t;
54
55 /*******************
56  * Timer functions *
57  *******************/
58
59 /**
60  * Generate time stamp for an async event:
61  *   time:  drift compensated wall-clock time
62  *   stamp: event timestamp from Timer/PWM Module
63  */
64 void time_stamp(ntime_t *time, uint32_t stamp)
65 {
66         // todo
67 }
68
69 /**
70  * Compensate the Real-Time-Clock oscillator for
71  * temperature and drift errors. Called at 1Hz and
72  * synchronous to the RTC 1Hz output.
73  */
74 void time_rtc_comp(void)
75 {
76         // todo
77 }
78
79 /**
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
84  */
85 void time_ext_sync(ntime_t *ours, ntime_t *ref)
86 {
87         // todo
88 }
89
90 /************************
91  * Serial I/O functions *
92  ************************/
93
94 /**
95  * Output time sync message
96  */
97 void serial_send_sync(void)
98 {
99 }
100
101 /**
102  * Output external event received message
103  *   event: id of the received event
104  *   time:  compensated timestamp of the event
105  */
106 void serial_send_event(uint16_t event, ntime_t *time)
107 {
108 }
109
110 /**
111  * Process serial receive messages
112  */
113 void serial_receive(void)
114 {
115 }
116
117 /********************
118  * Data definitions *
119  ********************/
120
121 // LEDs
122 DigitalOut led1(LED1);
123 DigitalOut led2(LED2);
124
125 // UARTs         tx      rx
126 Serial     uart0(USBTX,  USBRX);
127 Serial     uart1(PTE0,   PTE1);
128 Serial     uart2(PTD3,   PTD2);
129
130 // Serial DMA
131 sdma_t    *sdma0;
132 sdma_t    *sdma1;
133 sdma_t    *sdma2;
134
135 // Timer DMA
136 tdma_t    *tdma0;
137 tdma_t    *tdma1;
138 tdma_t    *tdma2;
139 tdma_t    *tdma3;
140
141 /********
142  * Main *
143  ********/
144
145 void test_main(void);
146
147 int main(int argc, char **argv)
148 {
149         uart0.baud(115200);
150         uart1.baud(115200);
151         uart2.baud(115200);
152
153         test_main();
154
155         return 0;
156 }