]> Pileus Git - ~andy/csm213a-hw/blob - hw2/main.cpp
aac88f2c0dccf3962b60f5656b42089b4507b082
[~andy/csm213a-hw] / hw2 / main.cpp
1 #include "mbed.h"\r
2 \r
3 /**\r
4  * Mode of operation:\r
5  *   Devices 1 and 2 synchronize clocks using serial messages.\r
6  *\r
7  *   1. Each serial message timestamped using the hardware timer capture\r
8  *      registers in both the sender and receiver.\r
9  *   2. The sender transmits the send timestamp during the next time-sync\r
10  *      message.\r
11  *   3. The receiver then compares the senders timestamp with it's own\r
12  *      timestamp for the corresponding messages and calculates an offset.\r
13  *   4. The offset is used to compensate the receivers local clock.\r
14  *\r
15  *   Time synchronization is performed in both directions.\r
16  */\r
17 \r
18 /***********************\r
19  * Message Definitions *\r
20  ***********************/\r
21 \r
22 #define MSG_HEADER 0x1234\r
23 \r
24 typedef enum {\r
25         MSG_ID_SYNC,       // Time synchronization\r
26         MSG_ID_EVENT,      // Event occurred\r
27 } msgid_t;\r
28 \r
29 typedef struct {\r
30         uint32_t seconds;  // Seconds since 1970 (without leap seconds)\r
31         uint32_t nanosec;  // Nanoseconds since 'seconds'\r
32 } ntime_t;\r
33 \r
34 typedef struct {\r
35         uint16_t header;   // Message Header\r
36         uint16_t mesgid;   // Message ID\r
37         uint16_t length;   // Body length\r
38         uint16_t cksum;    // Body checksum\r
39 } header_t;\r
40 \r
41 typedef struct {\r
42         uint16_t seq;      // Current sequence counter\r
43         uint16_t prev;     // Sequence of previous message\r
44         ntime_t  time;     // Time of previous message\r
45 } sync_msg_t;\r
46 \r
47 typedef struct {\r
48         uint16_t device;   // Device ID\r
49         uint16_t event;    // Event ID\r
50         ntime_t  time;     // Timestamp\r
51 } event_msg_t;\r
52 \r
53 /*******************\r
54  * Timer functions *\r
55  *******************/\r
56 \r
57 /**\r
58  * Generate time stamp for an async event:\r
59  *   time:  drift compensated wall-clock time\r
60  *   stamp: event timestamp from Timer/PWM Module\r
61  */\r
62 void time_stamp(ntime_t *time, uint32_t stamp)\r
63 {\r
64         // todo\r
65 }\r
66 \r
67 /**\r
68  * Compensate the Real-Time-Clock oscillator for\r
69  * temperature and drift errors. Called at 1Hz and\r
70  * synchronous to the RTC 1Hz output.\r
71  */\r
72 void time_rtc_comp(void)\r
73 {\r
74         // todo\r
75 }\r
76 \r
77 /**\r
78  * Synchronize the timer internal state with updates\r
79  * from an external time sync message.\r
80  *   ours: our internal timestamp for the event\r
81  *   ref:  reference timestamp from the other device\r
82  */\r
83 void time_ext_sync(ntime_t *ours, ntime_t *ref)\r
84 {\r
85         // todo\r
86 }\r
87 \r
88 /************************\r
89  * Serial I/O functions *\r
90  ************************/\r
91 \r
92 /**\r
93  * Output time sync message\r
94  */\r
95 void serial_send_sync(void)\r
96 {\r
97 }\r
98 \r
99 /**\r
100  * Output external event received message\r
101  *   event: id of the received event\r
102  *   time:  compensated timestamp of the event\r
103  */\r
104 void serial_send_event(uint16_t event, ntime_t *time)\r
105 {\r
106 }\r
107 \r
108 /**\r
109  * Process serial receive messages\r
110  */\r
111 void serial_receive(void)\r
112 {\r
113 }\r
114 \r
115 /********************\r
116  * Data definitions *\r
117  ********************/\r
118 \r
119 // LEDs\r
120 DigitalOut led1(LED1);\r
121 DigitalOut led2(LED2);\r
122 \r
123 // UARTs         tx      rx\r
124 Serial     uart0(USBTX,  USBRX);\r
125 Serial     uart1(PTE0,   PTE1);\r
126 Serial     uart2(PTE16,  PTE17);\r
127 \r
128 /********\r
129  * Main *\r
130  ********/\r
131 \r
132 void test_uart(void)\r
133 {\r
134         char xmt[32] = "hello, world";\r
135         char rcv[32] = {};\r
136 \r
137         printf("start\r\n");\r
138         for (int i = 0; xmt[i]; i++) {\r
139                 uart1.putc(xmt[i]);\r
140                 rcv[i] = uart2.getc();\r
141         }\r
142         printf("xmt: %s\r\n", xmt);\r
143         printf("rcv: %s\r\n", rcv);\r
144 }\r
145 \r
146 void test_leds(void)\r
147 {\r
148         led1 = 1; led2 = 0; wait(0.1);\r
149         led1 = 0; led2 = 1; wait(0.1);\r
150 }\r
151 \r
152 int main(int argc, char **argv)\r
153 {\r
154         uart0.baud(115200);\r
155         uart1.baud(115200);\r
156         uart2.baud(115200);\r
157 \r
158         test_uart();\r
159         test_leds();\r
160 \r
161         while (1) {\r
162                 printf("tick\r\n");\r
163                 test_leds();\r
164         }\r
165 }\r