-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
main.c
306 lines (259 loc) · 8.43 KB
/
main.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
/**
* @section License
*
* The MIT License (MIT)
*
* Copyright (c) 2014-2016, Erik Moqvist
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* This file is part of the Simba project.
*/
/**
* For Arduino Due:
*
* +---------------+
* | CAN0TX o----------o----------------+
* | | | |
* | | Schottky diode |
* | | | |
* | CAN0RX o----------o--------+ |
* | | | |
* | | R 3k3 R 3k3
* | | | |
* | D53 - CAN1TX o----------o------- o |
* | | | |
* | | Schottky diode |
* | | | |
* | DAC0 - CAN1RX o----------o----------------+
* +---------------+
*/
#include "simba.h"
/* Ping-pong can frame ids. */
#define PING_ID 0x23
#define PONG_ID 0x24
/* CAN receive buffers. */
static char can0_rx_buf[256];
static char can1_rx_buf[256];
struct can_driver_t can0;
struct can_driver_t can1;
static THRD_STACK(rx_thrd_stack, 1024);
/**
* Initialize and start the two CAN controllers, CAN0 and CAN1, with
* given baudrate.
*
* @apram[in] speed Baudrate to use.
*/
static int start(uint32_t speed)
{
BTASSERT(can_init(&can0,
&can_device[0],
speed,
can0_rx_buf,
sizeof(can0_rx_buf)) == 0);
BTASSERT(can_start(&can0) == 0);
BTASSERT(can_init(&can1,
&can_device[1],
speed,
can1_rx_buf,
sizeof(can1_rx_buf)) == 0);
BTASSERT(can_start(&can1) == 0);
return (0);
}
/**
* Stop the two CAN controllers, CAN0 and CAN1.
*/
static int stop()
{
BTASSERT(can_stop(&can0) == 0);
BTASSERT(can_stop(&can1) == 0);
return (0);
}
/**
* Thread that receives CAN frames on controller CAN1 and verifies its
* content.
*
* This thread is used by the test `test_max_throughput()`.
*/
static void *rx_thrd(void *arg_p)
{
struct can_frame_t frame;
int id = 0;
int i = 0;
thrd_set_name("rx_thrd");
while (1) {
BTASSERTN(can_read(&can1, &frame, sizeof(frame)) == sizeof(frame), NULL);
BTASSERTN(frame.id == id, NULL,
FSTR(" i = %d, frame.id = %d, id = %d\r\n"),
i,
frame.id,
id);
i++;
if (i == 10000) {
i = 0;
id = 0;
} else {
id++;
id %= 0x800;
}
}
return (NULL);
}
/**
* Ping-pong test. Starts the CAN controllers with given speed, sends
* a few CAN frames and then stops the CAN controllers.
*
* @param[in] speed Badrate to use.
*
* @param[in] extended_frame Use extendend CAN frame id if true(1),
* otherwise use standard CAN id.
*/
static int test_ping_pong(uint32_t speed, int extended_frame)
{
struct can_frame_t frame;
start(speed);
/* Ping is transmitted from can0 to can1. */
/* Write ping. */
frame.id = PING_ID;
frame.id <<= (10 * extended_frame);
frame.extended_frame = extended_frame;
frame.size = 1;
frame.data.u8[0] = 0xfe;
BTASSERT(can_write(&can0, &frame, sizeof(frame)) == sizeof(frame));
/* Read ping. */
memset(&frame, 0, sizeof(frame));
BTASSERT(can_read(&can1, &frame, sizeof(frame)) == sizeof(frame));
frame.id >>= (10 * extended_frame);
BTASSERT(frame.id == PING_ID);
BTASSERT(frame.extended_frame == extended_frame);
BTASSERT(frame.size == 1);
BTASSERT(frame.data.u8[0] == 0xfe);
/* Pong is transmitted from can1 to can0. */
/* Write pong. */
frame.id = PONG_ID;
frame.id <<= (10 * extended_frame);
frame.extended_frame = extended_frame;
frame.size = 0;
BTASSERT(can_write(&can1, &frame, sizeof(frame)) == sizeof(frame));
/* Read pong. */
memset(&frame, 0, sizeof(frame));
BTASSERT(can_read(&can0, &frame, sizeof(frame)) == sizeof(frame));
frame.id >>= (10 * extended_frame);
BTASSERT(frame.id == PONG_ID);
BTASSERT(frame.extended_frame == extended_frame);
BTASSERT(frame.size == 0);
stop();
return (0);
}
/**
* Ping-pong tests with differnet configurations.
*/
static int test_ping_pong_250k(struct harness_t *harness_p)
{
return (test_ping_pong(CAN_SPEED_250KBPS, 0));
}
static int test_ping_pong_500k(struct harness_t *harness_p)
{
return (test_ping_pong(CAN_SPEED_500KBPS, 0));
}
static int test_ping_pong_1000k(struct harness_t *harness_p)
{
return (test_ping_pong(CAN_SPEED_1000KBPS, 0));
}
static int test_ping_pong_1000k_extended_frame(struct harness_t *harness_p)
{
return (test_ping_pong(CAN_SPEED_1000KBPS, 1));
}
/**
* Test the maximum throughput on the CAN bus.
*/
static int test_max_throughput(struct harness_t *harness_p)
{
int i, j, k, id;
struct can_frame_t frames[8];
struct time_t start_time, stop_time, diff_time;
float frames_per_second;
float bits_per_second;
float data_bits_per_second;
int sizes[] = {0, 4, 8};
float elapsed_time;
thrd_spawn(rx_thrd,
NULL,
-1,
rx_thrd_stack,
sizeof(rx_thrd_stack));
start(CAN_SPEED_1000KBPS);
for (i = 0; i < membersof(sizes); i++) {
std_printf(FSTR("Writing 10000 frames with data size %d.\r\n"),
sizes[i]);
time_get(&start_time);
id = 0;
/* Prepare the array of frames. */
for (k = 0; k < membersof(frames); k++) {
frames[k].extended_frame = 0;
frames[k].size = sizes[i];
memset(frames[k].data.u8, 0xaa, frames[k].size);
}
/* Write the array of frames to the hardware. */
for (j = 0; j < 10000 / membersof(frames); j++) {
for (k = 0; k < membersof(frames); k++) {
frames[k].id = id;
id++;
}
BTASSERT(can_write(&can0, frames, sizeof(frames))
== sizeof(frames));
}
time_get(&stop_time);
/* Statistics. */
time_diff(&diff_time, &stop_time, &start_time);
elapsed_time = (diff_time.seconds
+ diff_time.nanoseconds / 1000000000.0);
frames_per_second = (10000.0 / elapsed_time);
bits_per_second = (frames_per_second *
(1 + 11 + 1 + 1 + 1 + 4 + 15 + 1 + 1 + 1 + 7
+ sizes[i] * 8));
data_bits_per_second = (frames_per_second * sizes[i] * 8);
std_printf(FSTR("elapsed time = %f s, %d frames/s, %d bits/s, "
"%d data bits/s\r\n"),
elapsed_time,
(int)frames_per_second,
(int)bits_per_second,
(int)data_bits_per_second);
}
stop();
return (0);
}
int main()
{
struct harness_t harness;
struct harness_testcase_t testcases[] = {
{ test_ping_pong_250k, "test_ping_pong_250k" },
{ test_ping_pong_500k, "test_ping_pong_500k" },
{ test_ping_pong_1000k, "test_ping_pong_1000k" },
{ test_ping_pong_1000k_extended_frame, "test_ping_pong_1000k_extended_frame" },
{ test_max_throughput, "test_max_throughput" },
{ NULL, NULL }
};
sys_start();
harness_init(&harness);
harness_run(&harness, testcases);
return (0);
}