-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
336 lines (231 loc) · 7.15 KB
/
test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include <stdio.h>
#include <stdlib.h>
static int is_rx_set;
static int is_tx_set;
static int timer_started;
static int listening_rx_pin;
#define START_UART_TIMER (timer_started = 1)
#define STOP_UART_TIMER (timer_started = 0)
#define START_LISTEN_RX_CHANGE (listening_rx_pin = 1)
#define STOP_LISTEN_RX_CHANGE (listening_rx_pin = 0)
static void error(const char *);
#define NO_BYTE (-1)
#define MESSAGE_LIMIT 1000
static int expected_byte = NO_BYTE;
static int received_byte = NO_BYTE;
static void byte_received(unsigned char byte) {
received_byte = byte;
if (expected_byte == NO_BYTE) {
error("unexpected byte received");
} else if (expected_byte != byte) {
char message[MESSAGE_LIMIT];
snprintf(message, MESSAGE_LIMIT, "unexpected byte received: %d, expected: %d", (int)byte, expected_byte);
error(message);
}
}
static int rx_sampled_count;
int get_rx_set() {
++rx_sampled_count;
return is_rx_set;
}
#define IS_BIT_ON_RX_SET get_rx_set()
#define TX_UP (is_tx_set = 1)
#define TX_DOWN (is_tx_set = 0)
void (*sent_action)();
void uart_byte_sent() {
if (sent_action) sent_action();
}
#include "uart-inline.h"
unsigned char buffer[2];
unsigned int sending_byte;
unsigned int pending_bytes;
unsigned int bytes_sent_callback_called_times;
void start_test(const char *message) {
rx_sampled_count = 0;
is_rx_set = 1;
is_tx_set = 0;
timer_started = 0;
listening_rx_pin = 0;
bytes_sent_callback_called_times = 0;
sending_byte = 0;
pending_bytes = 0;
expected_byte = NO_BYTE;
received_byte = NO_BYTE;
puts(message);
uart_reset();
}
void assert(const char *assertion, const int condition) {
if (!condition) {
char message[MESSAGE_LIMIT];
snprintf(message, MESSAGE_LIMIT, "%s assertion failed", assertion);
error(message);
}
}
void assertTxUp() {
assert("tx up",
is_tx_set);
}
void assertTxDown() {
assert("tx down",
!is_tx_set);
}
void assertTxIs(const int expected) {
if (expected) assertTxUp();
else assertTxDown();
}
void assertTimerStarted() {
assert("timer started",
timer_started);
}
void assertTimerStopped() {
assert("timer stopped",
!timer_started);
}
void assertListeningStarted() {
assert("rx listening started",
listening_rx_pin);
}
void assertListeningStopped() {
assert("rx listening stopped",
!listening_rx_pin);
}
void test_receive_bit(const int bit) {
is_rx_set = bit;
rx_sampled_count = 0;
for (int i = 0; i < 7; ++i) UART_TIMER_CALLBACK_NAME();
assert("receiving bit unexpected rx sample", rx_sampled_count == 0);
UART_TIMER_CALLBACK_NAME();
assert("receiving bit expected rx sample wasn't performed", rx_sampled_count == 1);
}
void test_simple_receive(unsigned char byte_transferred) {
char message[MESSAGE_LIMIT];
snprintf(message, MESSAGE_LIMIT, "testing simple receive of byte: %x", byte_transferred);
start_test(message);
/* imitate dropping of RX line down */
is_rx_set = 0;
UART_RX_PIN_CHANGED_CALLBACK_NAME();
assert("rx sampled by pin change callback", rx_sampled_count == 1);
assertListeningStopped();
assertTimerStarted();
/**
* as we started from down callback, we no need to sample rx again. to
* get into middle of start bit (to confirm start) we will need to do 4
* more checks (as bit is divided to 8 Parts):
*
* ,- here UART_TIMER_CALLBACK_NAME() executed and started timer
* /
* | | | | | | | | |
* \ \ \ \
* `----`----`----`--- here timer checks are expected
*
* then we are expecting no RX sampling until middle of first data bit,
* which is 12 ticks after RX line down
*/
/* simulate 4 tics */
rx_sampled_count = 0;
for (int i = 0; i < 4; ++i) UART_TIMER_CALLBACK_NAME();
/* verify that rx sampled 4 times */
assert("count rx sampled", rx_sampled_count == 4);
int byte_buffer = byte_transferred;
expected_byte = byte_transferred;
for (int i = 0; i < 8; ++i) {
test_receive_bit(byte_buffer & 1);
byte_buffer >>= 1;
}
assert("byte correct", received_byte == expected_byte);
}
void test_sending(unsigned char byte_transferred) {
char message[MESSAGE_LIMIT];
snprintf(message, MESSAGE_LIMIT, "testing simple send of byte: %x", byte_transferred);
start_test(message);
assertTimerStopped();
assertListeningStarted();
assertTxUp();
SEND_BYTE_FUNC_NAME(byte_transferred);
assertTimerStarted();
assertListeningStopped();
assertTxDown();
/* sending start bit */
for (int i = 0; i < 7; ++i) {
UART_TIMER_CALLBACK_NAME();
assertTxDown();
}
for (int i = 0; i < 8; ++i) {
int expectedState = byte_transferred & 1;
byte_transferred >>= 1;
/* 8 ticks to next state */
for (int b = 0; b < 8; ++b){
UART_TIMER_CALLBACK_NAME();
assertTxIs(expectedState);
}
}
/* eight bytes up, stop bit */
for (int i = 0; i < 8; ++i) {
UART_TIMER_CALLBACK_NAME();
assertTxUp();
assertTimerStarted();
}
UART_TIMER_CALLBACK_NAME();
assertTimerStopped();
assertListeningStarted();
}
void send_next() {
bytes_sent_callback_called_times++;
if (sending_byte < pending_bytes) {
SEND_BYTE_FUNC_NAME(buffer[sending_byte]);
sending_byte++;
}
}
void test_sending_interleaved_with_receiving(unsigned char first_send_byte,
unsigned char second_send_byte,
unsigned char receive_byte) {
start_test("testing sending interleaved with receiving");
buffer[0] = first_send_byte;
buffer[1] = second_send_byte;
sending_byte = 0;
pending_bytes = 2;
assertTimerStopped();
assertListeningStarted();
assertTxUp();
sent_action = &send_next;
send_next();
assertTimerStarted();
assertListeningStopped();
assertTxDown();
for (int i = 0; i < 16; ++i) UART_TIMER_CALLBACK_NAME();
/* and start receiving by lowering the rx line */
is_rx_set = 0;
/* and continue sending the start bit */
for (int i = 0; i < 8; ++i) UART_TIMER_CALLBACK_NAME();
expected_byte = receive_byte;
for (int i = 0; i < 8; ++i) {
is_rx_set = receive_byte & 1;
receive_byte >>= 1;
for (int b = 0; b < 8; ++b) UART_TIMER_CALLBACK_NAME();
}
assert("correct byte received", expected_byte == received_byte);
/* last ticks to make it complete sending */
for (int i = 0; i < 71; ++i) {
UART_TIMER_CALLBACK_NAME();
assertTimerStarted();
assertListeningStopped();
}
UART_TIMER_CALLBACK_NAME();
assertTimerStopped();
assertListeningStarted();
assert("correct count of callback called times",
bytes_sent_callback_called_times == 3);
}
int main() {
/* ensure every byte received correctly */
for (int i = 0; i < 0x100; ++i) {
test_simple_receive(i);
test_sending(i);
}
test_sending_interleaved_with_receiving(0x35, 0xf1, 0xd4);
return 0;
}
void error(const char *data) {
puts(data);
exit(1);
}