1 /*
2 * Copyright (c) 2022 Vestas Wind Systems A/S
3 * Copyright (c) 2019 Alexander Wachter
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #include <zephyr/drivers/can.h>
9 #include <zephyr/ztest.h>
10
11 #include "common.h"
12
13 /**
14 * @addtogroup t_can_driver
15 * @{
16 * @defgroup t_can_classic test_can_classic
17 * @}
18 */
19
20 /**
21 * @brief Standard (11-bit) CAN ID transmit callback 1.
22 *
23 * See @a can_tx_callback_t() for argument description.
24 */
tx_std_callback_1(const struct device * dev,int error,void * user_data)25 static void tx_std_callback_1(const struct device *dev, int error, void *user_data)
26 {
27 const struct can_frame *frame = user_data;
28
29 k_sem_give(&tx_callback_sem);
30
31 zassert_equal(dev, can_dev, "CAN device does not match");
32 zassert_equal(frame->id, TEST_CAN_STD_ID_1, "ID does not match");
33 }
34
35 /**
36 * @brief Standard (11-bit) CAN ID transmit callback 2.
37 *
38 * See @a can_tx_callback_t() for argument description.
39 */
tx_std_callback_2(const struct device * dev,int error,void * user_data)40 static void tx_std_callback_2(const struct device *dev, int error, void *user_data)
41 {
42 const struct can_frame *frame = user_data;
43
44 k_sem_give(&tx_callback_sem);
45
46 zassert_equal(dev, can_dev, "CAN device does not match");
47 zassert_equal(frame->id, TEST_CAN_STD_ID_2, "ID does not match");
48 }
49
50 /**
51 * @brief Extended (29-bit) CAN ID transmit callback 1.
52 *
53 * See @a can_tx_callback_t() for argument description.
54 */
tx_ext_callback_1(const struct device * dev,int error,void * user_data)55 static void tx_ext_callback_1(const struct device *dev, int error, void *user_data)
56 {
57 const struct can_frame *frame = user_data;
58
59 k_sem_give(&tx_callback_sem);
60
61 zassert_equal(dev, can_dev, "CAN device does not match");
62 zassert_equal(frame->id, TEST_CAN_EXT_ID_1, "ID does not match");
63 }
64
65 /**
66 * @brief Extended (29-bit) CAN ID transmit callback 2.
67 *
68 * See @a can_tx_callback_t() for argument description.
69 */
tx_ext_callback_2(const struct device * dev,int error,void * user_data)70 static void tx_ext_callback_2(const struct device *dev, int error, void *user_data)
71 {
72 const struct can_frame *frame = user_data;
73
74 k_sem_give(&tx_callback_sem);
75
76 zassert_equal(dev, can_dev, "CAN device does not match");
77 zassert_equal(frame->id, TEST_CAN_EXT_ID_2, "ID does not match");
78 }
79
80 /**
81 * @brief Standard (11-bit) CAN ID receive callback 1.
82 *
83 * See @a can_rx_callback_t() for argument description.
84 */
rx_std_callback_1(const struct device * dev,struct can_frame * frame,void * user_data)85 static void rx_std_callback_1(const struct device *dev, struct can_frame *frame,
86 void *user_data)
87 {
88 struct can_filter *filter = user_data;
89
90 assert_frame_equal(frame, &test_std_frame_1, 0);
91 zassert_equal(dev, can_dev, "CAN device does not match");
92 zassert_equal_ptr(filter, &test_std_filter_1, "filter does not match");
93
94 k_sem_give(&rx_callback_sem);
95 }
96
97 /**
98 * @brief Standard (11-bit) CAN ID receive callback 2.
99 *
100 * See @a can_rx_callback_t() for argument description.
101 */
rx_std_callback_2(const struct device * dev,struct can_frame * frame,void * user_data)102 static void rx_std_callback_2(const struct device *dev, struct can_frame *frame,
103 void *user_data)
104 {
105 struct can_filter *filter = user_data;
106
107 assert_frame_equal(frame, &test_std_frame_2, 0);
108 zassert_equal(dev, can_dev, "CAN device does not match");
109 zassert_equal_ptr(filter, &test_std_filter_2, "filter does not match");
110
111 k_sem_give(&rx_callback_sem);
112 }
113
114 /**
115 * @brief Standard (11-bit) masked CAN ID receive callback 1.
116 *
117 * See @a can_rx_callback_t() for argument description.
118 */
rx_std_mask_callback_1(const struct device * dev,struct can_frame * frame,void * user_data)119 static void rx_std_mask_callback_1(const struct device *dev, struct can_frame *frame,
120 void *user_data)
121 {
122 struct can_filter *filter = user_data;
123
124 assert_frame_equal(frame, &test_std_frame_1, 0x0F);
125 zassert_equal(dev, can_dev, "CAN device does not match");
126 zassert_equal_ptr(filter, &test_std_masked_filter_1, "filter does not match");
127
128 k_sem_give(&rx_callback_sem);
129 }
130
131 /**
132 * @brief Standard (11-bit) masked CAN ID receive callback 2.
133 *
134 * See @a can_rx_callback_t() for argument description.
135 */
rx_std_mask_callback_2(const struct device * dev,struct can_frame * frame,void * user_data)136 static void rx_std_mask_callback_2(const struct device *dev, struct can_frame *frame,
137 void *user_data)
138 {
139 struct can_filter *filter = user_data;
140
141 assert_frame_equal(frame, &test_std_frame_2, 0x0F);
142 zassert_equal(dev, can_dev, "CAN device does not match");
143 zassert_equal_ptr(filter, &test_std_masked_filter_2, "filter does not match");
144
145 k_sem_give(&rx_callback_sem);
146 }
147
148 /**
149 * @brief Extended (29-bit) CAN ID receive callback 1.
150 *
151 * See @a can_rx_callback_t() for argument description.
152 */
rx_ext_callback_1(const struct device * dev,struct can_frame * frame,void * user_data)153 static void rx_ext_callback_1(const struct device *dev, struct can_frame *frame,
154 void *user_data)
155 {
156 struct can_filter *filter = user_data;
157
158 assert_frame_equal(frame, &test_ext_frame_1, 0);
159 zassert_equal(dev, can_dev, "CAN device does not match");
160 zassert_equal_ptr(filter, &test_ext_filter_1, "filter does not match");
161
162 k_sem_give(&rx_callback_sem);
163 }
164
165 /**
166 * @brief Extended (29-bit) CAN ID receive callback 2.
167 *
168 * See @a can_rx_callback_t() for argument description.
169 */
rx_ext_callback_2(const struct device * dev,struct can_frame * frame,void * user_data)170 static void rx_ext_callback_2(const struct device *dev, struct can_frame *frame,
171 void *user_data)
172 {
173 struct can_filter *filter = user_data;
174
175 assert_frame_equal(frame, &test_ext_frame_2, 0);
176 zassert_equal(dev, can_dev, "CAN device does not match");
177 zassert_equal_ptr(filter, &test_ext_filter_2, "filter does not match");
178
179 k_sem_give(&rx_callback_sem);
180 }
181
182 /**
183 * @brief Extended (29-bit) masked CAN ID receive callback 1.
184 *
185 * See @a can_rx_callback_t() for argument description.
186 */
rx_ext_mask_callback_1(const struct device * dev,struct can_frame * frame,void * user_data)187 static void rx_ext_mask_callback_1(const struct device *dev, struct can_frame *frame,
188 void *user_data)
189 {
190 struct can_filter *filter = user_data;
191
192 assert_frame_equal(frame, &test_ext_frame_1, 0x0F);
193 zassert_equal(dev, can_dev, "CAN device does not match");
194 zassert_equal_ptr(filter, &test_ext_masked_filter_1, "filter does not match");
195
196 k_sem_give(&rx_callback_sem);
197 }
198
199 /**
200 * @brief Extended (29-bit) masked CAN ID receive callback 2.
201 *
202 * See @a can_rx_callback_t() for argument description.
203 */
rx_ext_mask_callback_2(const struct device * dev,struct can_frame * frame,void * user_data)204 static void rx_ext_mask_callback_2(const struct device *dev, struct can_frame *frame,
205 void *user_data)
206 {
207 struct can_filter *filter = user_data;
208
209 assert_frame_equal(frame, &test_ext_frame_2, 0x0F);
210 zassert_equal(dev, can_dev, "CAN device does not match");
211 zassert_equal_ptr(filter, &test_ext_masked_filter_2, "filter does not match");
212
213 k_sem_give(&rx_callback_sem);
214 }
215
216 /**
217 * @brief Send a CAN test frame with asserts.
218 *
219 * This function will block until the frame is transmitted or a test timeout
220 * occurs.
221 *
222 * @param dev Pointer to the device structure for the driver instance.
223 * @param frame Pointer to the CAN frame to send.
224 */
send_test_frame(const struct device * dev,const struct can_frame * frame)225 static void send_test_frame(const struct device *dev, const struct can_frame *frame)
226 {
227 int err;
228
229 err = can_send(dev, frame, TEST_SEND_TIMEOUT, NULL, NULL);
230 zassert_not_equal(err, -EBUSY, "arbitration lost in loopback mode");
231 zassert_equal(err, 0, "failed to send frame (err %d)", err);
232 }
233
234 /**
235 * @brief Send a CAN test frame with asserts.
236 *
237 * This function will block until the frame is queued or a test timeout
238 * occurs.
239 *
240 * @param dev Pointer to the device structure for the driver instance.
241 * @param frame Pointer to the CAN frame to send.
242 * @param callback Transmit callback function.
243 */
send_test_frame_nowait(const struct device * dev,const struct can_frame * frame,can_tx_callback_t callback)244 static void send_test_frame_nowait(const struct device *dev, const struct can_frame *frame,
245 can_tx_callback_t callback)
246 {
247 int err;
248
249 err = can_send(dev, frame, TEST_SEND_TIMEOUT, callback, (void *)frame);
250 zassert_not_equal(err, -EBUSY, "arbitration lost in loopback mode");
251 zassert_equal(err, 0, "failed to send frame (err %d)", err);
252 }
253
254 /**
255 * @brief Add a CAN message queue with asserts.
256 *
257 * @param dev Pointer to the device structure for the driver instance.
258 * @param filter CAN filter for the CAN message queue.
259 *
260 * @return CAN filter ID.
261 */
add_rx_msgq(const struct device * dev,const struct can_filter * filter)262 static inline int add_rx_msgq(const struct device *dev, const struct can_filter *filter)
263 {
264 int filter_id;
265
266 filter_id = can_add_rx_filter_msgq(dev, &can_msgq, filter);
267 zassert_not_equal(filter_id, -ENOSPC, "no filters available");
268 zassert_true(filter_id >= 0, "negative filter number");
269
270 return filter_id;
271 }
272
273 /**
274 * @brief Add a CAN filter with asserts.
275 *
276 * @param dev Pointer to the device structure for the driver instance.
277 * @param filter CAN filter.
278 * @param callback Receive callback function.
279 *
280 * @return CAN filter ID.
281 */
add_rx_filter(const struct device * dev,const struct can_filter * filter,can_rx_callback_t callback)282 static inline int add_rx_filter(const struct device *dev,
283 const struct can_filter *filter,
284 can_rx_callback_t callback)
285 {
286 int filter_id;
287
288 k_sem_reset(&rx_callback_sem);
289
290 filter_id = can_add_rx_filter(dev, callback, (void *)filter, filter);
291 zassert_not_equal(filter_id, -ENOSPC, "no filters available");
292 zassert_true(filter_id >= 0, "negative filter number");
293
294 return filter_id;
295 }
296
297 /**
298 * @brief Perform a send/receive test with a set of CAN ID filters and CAN frames.
299 *
300 * @param filter1 CAN filter 1
301 * @param filter2 CAN filter 2
302 * @param frame1 CAN frame 1
303 * @param frame2 CAN frame 2
304 */
send_receive(const struct can_filter * filter1,const struct can_filter * filter2,const struct can_frame * frame1,const struct can_frame * frame2)305 static void send_receive(const struct can_filter *filter1,
306 const struct can_filter *filter2,
307 const struct can_frame *frame1,
308 const struct can_frame *frame2)
309 {
310 struct can_frame frame_buffer;
311 uint32_t mask = 0U;
312 int filter_id_1;
313 int filter_id_2;
314 int err;
315
316 filter_id_1 = add_rx_msgq(can_dev, filter1);
317 zassert_not_equal(filter_id_1, -ENOSPC, "no filters available");
318 zassert_true(filter_id_1 >= 0, "negative filter number");
319 send_test_frame(can_dev, frame1);
320
321 err = k_msgq_get(&can_msgq, &frame_buffer, TEST_RECEIVE_TIMEOUT);
322 zassert_equal(err, 0, "receive timeout");
323
324 if ((filter1->flags & CAN_FILTER_IDE) != 0) {
325 if (filter1->mask != CAN_EXT_ID_MASK) {
326 mask = 0x0F;
327 }
328 } else {
329 if (filter1->mask != CAN_STD_ID_MASK) {
330 mask = 0x0F;
331 }
332 }
333
334 assert_frame_equal(&frame_buffer, frame1, mask);
335 can_remove_rx_filter(can_dev, filter_id_1);
336
337 k_sem_reset(&tx_callback_sem);
338
339 if ((frame1->flags & CAN_FRAME_IDE) != 0) {
340 if (filter1->mask == CAN_EXT_ID_MASK) {
341 filter_id_1 = add_rx_filter(can_dev, filter1, rx_ext_callback_1);
342 filter_id_2 = add_rx_filter(can_dev, filter2, rx_ext_callback_2);
343 send_test_frame_nowait(can_dev, frame1, tx_ext_callback_1);
344 send_test_frame_nowait(can_dev, frame2, tx_ext_callback_2);
345 } else {
346 filter_id_1 = add_rx_filter(can_dev, filter1, rx_ext_mask_callback_1);
347 filter_id_2 = add_rx_filter(can_dev, filter2, rx_ext_mask_callback_2);
348 send_test_frame_nowait(can_dev, frame1, tx_ext_callback_1);
349 send_test_frame_nowait(can_dev, frame2, tx_ext_callback_2);
350 }
351 } else {
352 if (filter1->mask == CAN_STD_ID_MASK) {
353 filter_id_1 = add_rx_filter(can_dev, filter1, rx_std_callback_1);
354 filter_id_2 = add_rx_filter(can_dev, filter2, rx_std_callback_2);
355 send_test_frame_nowait(can_dev, frame1, tx_std_callback_1);
356 send_test_frame_nowait(can_dev, frame2, tx_std_callback_2);
357 } else {
358 filter_id_1 = add_rx_filter(can_dev, filter1, rx_std_mask_callback_1);
359 filter_id_2 = add_rx_filter(can_dev, filter2, rx_std_mask_callback_2);
360 send_test_frame_nowait(can_dev, frame1, tx_std_callback_1);
361 send_test_frame_nowait(can_dev, frame2, tx_std_callback_2);
362 }
363 }
364
365 zassert_not_equal(filter_id_1, -ENOSPC, "no filters available");
366 zassert_true(filter_id_1 >= 0, "negative filter number");
367
368 zassert_not_equal(filter_id_2, -ENOSPC, "no filters available");
369 zassert_true(filter_id_2 >= 0, "negative filter number");
370
371 err = k_sem_take(&rx_callback_sem, TEST_RECEIVE_TIMEOUT);
372 zassert_equal(err, 0, "receive timeout");
373
374 err = k_sem_take(&rx_callback_sem, TEST_RECEIVE_TIMEOUT);
375 zassert_equal(err, 0, "receive timeout");
376
377 err = k_sem_take(&tx_callback_sem, TEST_SEND_TIMEOUT);
378 zassert_equal(err, 0, "missing TX callback");
379
380 err = k_sem_take(&tx_callback_sem, TEST_SEND_TIMEOUT);
381 zassert_equal(err, 0, "missing TX callback");
382
383 can_remove_rx_filter(can_dev, filter_id_1);
384 can_remove_rx_filter(can_dev, filter_id_2);
385 }
386
387 /**
388 * @brief Perform a send/receive test with a set of CAN ID filters and CAN frames, RTR and data
389 * frames.
390 *
391 * @param data_filter CAN data filter
392 * @param rtr_filter CAN RTR filter
393 * @param data_frame CAN data frame
394 * @param rtr_frame CAN RTR frame
395 */
send_receive_rtr(const struct can_filter * filter,const struct can_frame * data_frame,const struct can_frame * rtr_frame)396 void send_receive_rtr(const struct can_filter *filter,
397 const struct can_frame *data_frame,
398 const struct can_frame *rtr_frame)
399 {
400 struct can_frame frame;
401 int filter_id;
402 int err;
403
404 filter_id = can_add_rx_filter_msgq(can_dev, &can_msgq, filter);
405 zassert_not_equal(filter_id, -ENOSPC, "no filters available");
406 zassert_true(filter_id >= 0, "negative filter number");
407
408 /* Verify that filter matches RTR frame */
409 err = can_send(can_dev, rtr_frame, TEST_SEND_TIMEOUT, NULL, NULL);
410 if (err == -ENOTSUP) {
411 /* Not all drivers support transmission of RTR frames */
412 can_remove_rx_filter(can_dev, filter_id);
413 ztest_test_skip();
414 }
415 zassert_equal(err, 0, "failed to send RTR frame (err %d)", err);
416
417 err = k_msgq_get(&can_msgq, &frame, TEST_RECEIVE_TIMEOUT);
418 zassert_equal(err, 0, "receive timeout");
419 assert_frame_equal(&frame, rtr_frame, 0);
420
421 /* Verify that filter matches data frame */
422 send_test_frame(can_dev, data_frame);
423 err = k_msgq_get(&can_msgq, &frame, TEST_RECEIVE_TIMEOUT);
424 zassert_equal(err, 0, "receive timeout");
425 assert_frame_equal(&frame, data_frame, 0);
426
427 can_remove_rx_filter(can_dev, filter_id);
428 }
429
430 /**
431 * @brief Test getting the CAN core clock rate.
432 */
ZTEST_USER(can_classic,test_get_core_clock)433 ZTEST_USER(can_classic, test_get_core_clock)
434 {
435 uint32_t rate;
436 int err;
437
438 err = can_get_core_clock(can_dev, &rate);
439 zassert_equal(err, 0, "failed to get CAN core clock rate (err %d)", err);
440 zassert_not_equal(rate, 0, "CAN core clock rate is 0");
441 }
442
443 /**
444 * @brief Test getting the CAN controller capabilities.
445 */
ZTEST_USER(can_classic,test_classic_get_capabilities)446 ZTEST_USER(can_classic, test_classic_get_capabilities)
447 {
448 can_mode_t cap;
449 int err;
450
451 err = can_get_capabilities(can_dev, &cap);
452 zassert_equal(err, 0, "failed to get CAN capabilities (err %d)", err);
453 zassert_not_equal(cap & CAN_MODE_LOOPBACK, 0, "CAN loopback mode not supported");
454 }
455
456 /**
457 * @brief CAN state change callback.
458 */
state_change_callback(const struct device * dev,enum can_state state,struct can_bus_err_cnt err_cnt,void * user_data)459 static void state_change_callback(const struct device *dev, enum can_state state,
460 struct can_bus_err_cnt err_cnt, void *user_data)
461 {
462 ARG_UNUSED(dev);
463 ARG_UNUSED(state);
464 ARG_UNUSED(err_cnt);
465 ARG_UNUSED(user_data);
466 }
467
468 /**
469 * @brief Test setting the CAN state change callback.
470 */
ZTEST(can_classic,test_set_state_change_callback)471 ZTEST(can_classic, test_set_state_change_callback)
472 {
473 /* It is not possible to provoke a change of state, but test the API call */
474 can_set_state_change_callback(can_dev, state_change_callback, NULL);
475 can_set_state_change_callback(can_dev, NULL, NULL);
476 }
477
478 /**
479 * @brief Test setting a too high bitrate.
480 */
ZTEST_USER(can_classic,test_set_bitrate_too_high)481 ZTEST_USER(can_classic, test_set_bitrate_too_high)
482 {
483 uint32_t max = 0U;
484 int err;
485
486 err = can_get_max_bitrate(can_dev, &max);
487 if (err == -ENOSYS) {
488 ztest_test_skip();
489 }
490
491 zassert_equal(err, 0, "failed to get max bitrate (err %d)", err);
492 zassert_not_equal(max, 0, "max bitrate is 0");
493
494 err = can_stop(can_dev);
495 zassert_equal(err, 0, "failed to stop CAN controller (err %d)", err);
496
497 err = can_set_bitrate(can_dev, max + 1);
498 zassert_equal(err, -ENOTSUP, "too high bitrate accepted");
499
500 err = can_start(can_dev);
501 zassert_equal(err, 0, "failed to start CAN controller (err %d)", err);
502 }
503
504 /**
505 * @brief Test setting bitrate.
506 */
ZTEST_USER(can_classic,test_set_bitrate)507 ZTEST_USER(can_classic, test_set_bitrate)
508 {
509 int err;
510
511 err = can_stop(can_dev);
512 zassert_equal(err, 0, "failed to stop CAN controller (err %d)", err);
513
514 err = can_set_bitrate(can_dev, TEST_BITRATE_1);
515 zassert_equal(err, 0, "failed to set bitrate");
516
517 err = can_start(can_dev);
518 zassert_equal(err, 0, "failed to start CAN controller (err %d)", err);
519 }
520
521 /**
522 * @brief Test sending a message with no filters installed.
523 *
524 * This basic test work since the CAN controller is in loopback mode and
525 * therefore ACKs its own frame.
526 */
ZTEST_USER(can_classic,test_send_and_forget)527 ZTEST_USER(can_classic, test_send_and_forget)
528 {
529 send_test_frame(can_dev, &test_std_frame_1);
530 }
531
532 /**
533 * @brief Test adding basic filters.
534 *
535 * Test each filter type but only one filter at a time.
536 */
ZTEST(can_classic,test_add_filter)537 ZTEST(can_classic, test_add_filter)
538 {
539 int filter_id;
540
541 filter_id = add_rx_filter(can_dev, &test_std_filter_1, rx_std_callback_1);
542 can_remove_rx_filter(can_dev, filter_id);
543
544 filter_id = add_rx_filter(can_dev, &test_ext_filter_1, rx_ext_callback_1);
545 can_remove_rx_filter(can_dev, filter_id);
546
547 filter_id = add_rx_msgq(can_dev, &test_std_filter_1);
548 can_remove_rx_filter(can_dev, filter_id);
549
550 filter_id = add_rx_msgq(can_dev, &test_ext_filter_1);
551 can_remove_rx_filter(can_dev, filter_id);
552
553 filter_id = add_rx_filter(can_dev, &test_std_masked_filter_1, rx_std_mask_callback_1);
554 can_remove_rx_filter(can_dev, filter_id);
555
556 filter_id = add_rx_filter(can_dev, &test_ext_masked_filter_1, rx_ext_mask_callback_1);
557 can_remove_rx_filter(can_dev, filter_id);
558 }
559
560 /**
561 * @brief Test adding up to and above the maximum number of RX filters.
562 *
563 * @param ide standard (11-bit) CAN ID filters if false, or extended (29-bit) CAN ID filters if
564 * true.
565 * @param id_mask filter
566 */
add_remove_max_filters(bool ide)567 static void add_remove_max_filters(bool ide)
568 {
569 uint32_t id_mask = ide ? CAN_EXT_ID_MASK : CAN_STD_ID_MASK;
570 struct can_filter filter = {
571 .flags = (ide ? CAN_FILTER_IDE : 0),
572 .id = 0,
573 .mask = id_mask,
574 };
575 int filter_id;
576 int max;
577 int i;
578
579 max = can_get_max_filters(can_dev, ide);
580 if (max == -ENOSYS || max == 0) {
581 /*
582 * Skip test if max is not known or no filters of the given type
583 * is supported.
584 */
585 ztest_test_skip();
586 }
587
588 zassert_true(max > 0, "failed to get max filters (err %d)", max);
589
590 int filter_ids[max];
591
592 for (i = 0; i < max; i++) {
593 filter.id++;
594 filter_ids[i] = add_rx_msgq(can_dev, &filter);
595 }
596
597 filter.id++;
598 filter_id = can_add_rx_filter_msgq(can_dev, &can_msgq, &filter);
599 zassert_equal(filter_id, -ENOSPC, "added more than max filters");
600
601 for (i = 0; i < max; i++) {
602 can_remove_rx_filter(can_dev, filter_ids[i]);
603 }
604 }
605
606 /**
607 * @brief Test max standard (11-bit) CAN RX filters.
608 */
ZTEST_USER(can_classic,test_max_std_filters)609 ZTEST_USER(can_classic, test_max_std_filters)
610 {
611 add_remove_max_filters(false);
612 }
613
614 /**
615 * @brief Test max extended (29-bit) CAN RX filters.
616 */
ZTEST_USER(can_classic,test_max_ext_filters)617 ZTEST_USER(can_classic, test_max_ext_filters)
618 {
619 add_remove_max_filters(true);
620 }
621
622 /**
623 * @brief Test that no message is received when nothing was sent.
624 */
ZTEST_USER(can_classic,test_receive_timeout)625 ZTEST_USER(can_classic, test_receive_timeout)
626 {
627 struct can_frame frame;
628 int filter_id;
629 int err;
630
631 filter_id = add_rx_msgq(can_dev, &test_std_filter_1);
632
633 err = k_msgq_get(&can_msgq, &frame, TEST_RECEIVE_TIMEOUT);
634 zassert_equal(err, -EAGAIN, "received a frame without sending one");
635
636 can_remove_rx_filter(can_dev, filter_id);
637 }
638
639 /**
640 * @brief Test that transmit callback function is called.
641 */
ZTEST(can_classic,test_send_callback)642 ZTEST(can_classic, test_send_callback)
643 {
644 int err;
645
646 k_sem_reset(&tx_callback_sem);
647
648 send_test_frame_nowait(can_dev, &test_std_frame_1, tx_std_callback_1);
649
650 err = k_sem_take(&tx_callback_sem, TEST_SEND_TIMEOUT);
651 zassert_equal(err, 0, "missing TX callback");
652 }
653
654 /**
655 * @brief Test send/receive with standard (11-bit) CAN IDs.
656 */
ZTEST(can_classic,test_send_receive_std_id)657 ZTEST(can_classic, test_send_receive_std_id)
658 {
659 send_receive(&test_std_filter_1, &test_std_filter_2,
660 &test_std_frame_1, &test_std_frame_2);
661 }
662
663 /**
664 * @brief Test send/receive with extended (29-bit) CAN IDs.
665 */
ZTEST(can_classic,test_send_receive_ext_id)666 ZTEST(can_classic, test_send_receive_ext_id)
667 {
668 send_receive(&test_ext_filter_1, &test_ext_filter_2,
669 &test_ext_frame_1, &test_ext_frame_2);
670 }
671
672 /**
673 * @brief Test send/receive with standard (11-bit) masked CAN IDs.
674 */
ZTEST(can_classic,test_send_receive_std_id_masked)675 ZTEST(can_classic, test_send_receive_std_id_masked)
676 {
677 send_receive(&test_std_masked_filter_1, &test_std_masked_filter_2,
678 &test_std_frame_1, &test_std_frame_2);
679 }
680
681 /**
682 * @brief Test send/receive with extended (29-bit) masked CAN IDs.
683 */
ZTEST(can_classic,test_send_receive_ext_id_masked)684 ZTEST(can_classic, test_send_receive_ext_id_masked)
685 {
686 send_receive(&test_ext_masked_filter_1, &test_ext_masked_filter_2,
687 &test_ext_frame_1, &test_ext_frame_2);
688 }
689
690 /**
691 * @brief Test send/receive with messages buffered in a CAN message queue.
692 */
ZTEST_USER(can_classic,test_send_receive_msgq)693 ZTEST_USER(can_classic, test_send_receive_msgq)
694 {
695 struct k_msgq_attrs attrs;
696 struct can_frame frame;
697 int filter_id;
698 int nframes;
699 int err;
700 int i;
701
702 filter_id = add_rx_msgq(can_dev, &test_std_filter_1);
703
704 k_msgq_get_attrs(&can_msgq, &attrs);
705 nframes = attrs.max_msgs;
706
707 for (i = 0; i < nframes; i++) {
708 send_test_frame(can_dev, &test_std_frame_1);
709 }
710
711 for (i = 0; i < nframes; i++) {
712 err = k_msgq_get(&can_msgq, &frame, TEST_RECEIVE_TIMEOUT);
713 zassert_equal(err, 0, "receive timeout");
714 assert_frame_equal(&frame, &test_std_frame_1, 0);
715 }
716
717 for (i = 0; i < nframes; i++) {
718 send_test_frame(can_dev, &test_std_frame_1);
719 }
720
721 for (i = 0; i < nframes; i++) {
722 err = k_msgq_get(&can_msgq, &frame, TEST_RECEIVE_TIMEOUT);
723 zassert_equal(err, 0, "receive timeout");
724 assert_frame_equal(&frame, &test_std_frame_1, 0);
725 }
726
727 can_remove_rx_filter(can_dev, filter_id);
728 }
729
730 /**
731 * @brief Test send/receive with standard (11-bit) CAN IDs and remote transmission request (RTR).
732 */
ZTEST_USER(can_classic,test_send_receive_std_id_rtr)733 ZTEST_USER(can_classic, test_send_receive_std_id_rtr)
734 {
735 Z_TEST_SKIP_IFNDEF(CONFIG_CAN_ACCEPT_RTR);
736
737 send_receive_rtr(&test_std_filter_1, &test_std_frame_1, &test_std_rtr_frame_1);
738 }
739
740 /**
741 * @brief Test send/receive with extended (29-bit) CAN IDs and remote transmission request (RTR).
742 */
ZTEST_USER(can_classic,test_send_receive_ext_id_rtr)743 ZTEST_USER(can_classic, test_send_receive_ext_id_rtr)
744 {
745 Z_TEST_SKIP_IFNDEF(CONFIG_CAN_ACCEPT_RTR);
746
747 send_receive_rtr(&test_ext_filter_1, &test_ext_frame_1, &test_ext_rtr_frame_1);
748 }
749
750 /**
751 * @brief Test rejection of standard (11-bit) CAN IDs and remote transmission request (RTR).
752 */
ZTEST_USER(can_classic,test_reject_std_id_rtr)753 ZTEST_USER(can_classic, test_reject_std_id_rtr)
754 {
755 struct can_frame frame_buffer;
756 int filter_id;
757 int err;
758
759 Z_TEST_SKIP_IFDEF(CONFIG_CAN_ACCEPT_RTR);
760
761 filter_id = add_rx_msgq(can_dev, &test_std_filter_1);
762
763 err = can_send(can_dev, &test_std_rtr_frame_1, TEST_SEND_TIMEOUT, NULL, NULL);
764 if (err == -ENOTSUP) {
765 /* Not all drivers support transmission of RTR frames */
766 can_remove_rx_filter(can_dev, filter_id);
767 ztest_test_skip();
768 }
769 zassert_equal(err, 0, "failed to send RTR frame (err %d)", err);
770
771 err = k_msgq_get(&can_msgq, &frame_buffer, TEST_RECEIVE_TIMEOUT);
772 zassert_equal(err, -EAGAIN, "received a frame that should be rejected");
773
774 can_remove_rx_filter(can_dev, filter_id);
775 }
776
777 /**
778 * @brief Test rejection of extended (29-bit) CAN IDs and remote transmission request (RTR).
779 */
ZTEST_USER(can_classic,test_reject_ext_id_rtr)780 ZTEST_USER(can_classic, test_reject_ext_id_rtr)
781 {
782 struct can_frame frame_buffer;
783 int filter_id;
784 int err;
785
786 Z_TEST_SKIP_IFDEF(CONFIG_CAN_ACCEPT_RTR);
787
788 filter_id = add_rx_msgq(can_dev, &test_ext_filter_1);
789
790 err = can_send(can_dev, &test_ext_rtr_frame_1, TEST_SEND_TIMEOUT, NULL, NULL);
791 if (err == -ENOTSUP) {
792 /* Not all drivers support transmission of RTR frames */
793 can_remove_rx_filter(can_dev, filter_id);
794 ztest_test_skip();
795 }
796 zassert_equal(err, 0, "failed to send RTR frame (err %d)", err);
797
798 err = k_msgq_get(&can_msgq, &frame_buffer, TEST_RECEIVE_TIMEOUT);
799 zassert_equal(err, -EAGAIN, "received a frame that should be rejected");
800
801 can_remove_rx_filter(can_dev, filter_id);
802 }
803
804 /**
805 * @brief Test that non-matching CAN frames do not pass a filter.
806 */
ZTEST(can_classic,test_send_receive_wrong_id)807 ZTEST(can_classic, test_send_receive_wrong_id)
808 {
809 struct can_frame frame_buffer;
810 int filter_id;
811 int err;
812
813 filter_id = add_rx_msgq(can_dev, &test_std_filter_1);
814
815 send_test_frame(can_dev, &test_std_frame_2);
816
817 err = k_msgq_get(&can_msgq, &frame_buffer, TEST_RECEIVE_TIMEOUT);
818 zassert_equal(err, -EAGAIN, "received a frame that should not pass the filter");
819
820 can_remove_rx_filter(can_dev, filter_id);
821 }
822
823 /**
824 * @brief Test that frames with invalid Data Length Code (DLC) are rejected.
825 */
ZTEST_USER(can_classic,test_send_invalid_dlc)826 ZTEST_USER(can_classic, test_send_invalid_dlc)
827 {
828 struct can_frame frame = {0};
829 int err;
830
831 frame.id = TEST_CAN_STD_ID_1;
832 frame.dlc = CAN_MAX_DLC + 1;
833
834 err = can_send(can_dev, &frame, TEST_SEND_TIMEOUT, NULL, NULL);
835 zassert_equal(err, -EINVAL, "sent a frame with an invalid DLC");
836 }
837
838 /**
839 * @brief Test that CAN FD format frames are rejected in non-FD mode.
840 */
ZTEST_USER(can_classic,test_send_fd_format)841 ZTEST_USER(can_classic, test_send_fd_format)
842 {
843 struct can_frame frame = {0};
844 int err;
845
846 frame.id = TEST_CAN_STD_ID_1;
847 frame.dlc = 0;
848 frame.flags = CAN_FRAME_FDF;
849
850 err = can_send(can_dev, &frame, TEST_SEND_TIMEOUT, NULL, NULL);
851 zassert_equal(err, -ENOTSUP, "sent a CAN FD format frame in non-FD mode");
852 }
853
854 /**
855 * @brief Test CAN controller bus recovery.
856 */
ZTEST_USER(can_classic,test_recover)857 ZTEST_USER(can_classic, test_recover)
858 {
859 int err;
860
861 /* It is not possible to provoke a bus off state, but test the API call */
862 err = can_recover(can_dev, TEST_RECOVER_TIMEOUT);
863 if (err == -ENOTSUP) {
864 ztest_test_skip();
865 }
866
867 zassert_equal(err, 0, "failed to recover (err %d)", err);
868 }
869
870 /**
871 * @brief Test retrieving the state of the CAN controller.
872 */
ZTEST_USER(can_classic,test_get_state)873 ZTEST_USER(can_classic, test_get_state)
874 {
875 struct can_bus_err_cnt err_cnt;
876 enum can_state state;
877 int err;
878
879 err = can_get_state(can_dev, NULL, NULL);
880 zassert_equal(err, 0, "failed to get CAN state without destinations (err %d)", err);
881
882 err = can_get_state(can_dev, &state, NULL);
883 zassert_equal(err, 0, "failed to get CAN state (err %d)", err);
884
885 err = can_get_state(can_dev, NULL, &err_cnt);
886 zassert_equal(err, 0, "failed to get CAN error counters (err %d)", err);
887
888 err = can_get_state(can_dev, &state, &err_cnt);
889 zassert_equal(err, 0, "failed to get CAN state + error counters (err %d)", err);
890 }
891
892 /**
893 * @brief Test that CAN RX filters are preserved through CAN controller mode changes.
894 */
ZTEST_USER(can_classic,test_filters_preserved_through_mode_change)895 ZTEST_USER(can_classic, test_filters_preserved_through_mode_change)
896 {
897 struct can_frame frame;
898 enum can_state state;
899 int filter_id;
900 int err;
901
902 filter_id = add_rx_msgq(can_dev, &test_std_filter_1);
903 send_test_frame(can_dev, &test_std_frame_1);
904
905 err = k_msgq_get(&can_msgq, &frame, TEST_RECEIVE_TIMEOUT);
906 zassert_equal(err, 0, "receive timeout");
907 assert_frame_equal(&frame, &test_std_frame_1, 0);
908
909 err = can_stop(can_dev);
910 zassert_equal(err, 0, "failed to stop CAN controller (err %d)", err);
911
912 err = can_get_state(can_dev, &state, NULL);
913 zassert_equal(err, 0, "failed to get CAN state (err %d)", err);
914 zassert_equal(state, CAN_STATE_STOPPED, "CAN controller not stopped");
915
916 err = can_set_mode(can_dev, CAN_MODE_NORMAL);
917 zassert_equal(err, 0, "failed to set normal mode (err %d)", err);
918 zassert_equal(CAN_MODE_NORMAL, can_get_mode(can_dev));
919
920 err = can_set_mode(can_dev, CAN_MODE_LOOPBACK);
921 zassert_equal(err, 0, "failed to set loopback-mode (err %d)", err);
922 zassert_equal(CAN_MODE_LOOPBACK, can_get_mode(can_dev));
923
924 err = can_start(can_dev);
925 zassert_equal(err, 0, "failed to start CAN controller (err %d)", err);
926
927 send_test_frame(can_dev, &test_std_frame_1);
928
929 err = k_msgq_get(&can_msgq, &frame, TEST_RECEIVE_TIMEOUT);
930 zassert_equal(err, 0, "receive timeout");
931 assert_frame_equal(&frame, &test_std_frame_1, 0);
932
933 can_remove_rx_filter(can_dev, filter_id);
934 }
935
936 /**
937 * @brief Test that CAN RX filters are preserved through CAN controller bitrate changes.
938 */
ZTEST_USER(can_classic,test_filters_preserved_through_bitrate_change)939 ZTEST_USER(can_classic, test_filters_preserved_through_bitrate_change)
940 {
941 struct can_frame frame;
942 enum can_state state;
943 int filter_id;
944 int err;
945
946 filter_id = add_rx_msgq(can_dev, &test_std_filter_1);
947 send_test_frame(can_dev, &test_std_frame_1);
948
949 err = k_msgq_get(&can_msgq, &frame, TEST_RECEIVE_TIMEOUT);
950 zassert_equal(err, 0, "receive timeout");
951 assert_frame_equal(&frame, &test_std_frame_1, 0);
952
953 err = can_stop(can_dev);
954 zassert_equal(err, 0, "failed to stop CAN controller (err %d)", err);
955
956 err = can_get_state(can_dev, &state, NULL);
957 zassert_equal(err, 0, "failed to get CAN state (err %d)", err);
958 zassert_equal(state, CAN_STATE_STOPPED, "CAN controller not stopped");
959
960 err = can_set_bitrate(can_dev, TEST_BITRATE_2);
961 zassert_equal(err, 0, "failed to set bitrate");
962
963 err = can_set_bitrate(can_dev, TEST_BITRATE_1);
964 zassert_equal(err, 0, "failed to set bitrate");
965
966 err = can_start(can_dev);
967 zassert_equal(err, 0, "failed to start CAN controller (err %d)", err);
968
969 send_test_frame(can_dev, &test_std_frame_1);
970
971 err = k_msgq_get(&can_msgq, &frame, TEST_RECEIVE_TIMEOUT);
972 zassert_equal(err, 0, "receive timeout");
973 assert_frame_equal(&frame, &test_std_frame_1, 0);
974
975 can_remove_rx_filter(can_dev, filter_id);
976 }
977
978 /**
979 * @brief Test that CAN RX filters can be added while CAN controller is stopped.
980 */
ZTEST_USER(can_classic,test_filters_added_while_stopped)981 ZTEST_USER(can_classic, test_filters_added_while_stopped)
982 {
983 struct can_frame frame;
984 int filter_id;
985 int err;
986
987 err = can_stop(can_dev);
988 zassert_equal(err, 0, "failed to stop CAN controller (err %d)", err);
989
990 filter_id = add_rx_msgq(can_dev, &test_std_filter_1);
991
992 err = can_start(can_dev);
993 zassert_equal(err, 0, "failed to start CAN controller (err %d)", err);
994
995 send_test_frame(can_dev, &test_std_frame_1);
996
997 err = k_msgq_get(&can_msgq, &frame, TEST_RECEIVE_TIMEOUT);
998 zassert_equal(err, 0, "receive timeout");
999 assert_frame_equal(&frame, &test_std_frame_1, 0);
1000
1001 can_remove_rx_filter(can_dev, filter_id);
1002 }
1003
1004 /**
1005 * @brief Test stopping is not allowed while stopped.
1006 */
ZTEST_USER(can_classic,test_stop_while_stopped)1007 ZTEST_USER(can_classic, test_stop_while_stopped)
1008 {
1009 int err;
1010
1011 err = can_stop(can_dev);
1012 zassert_equal(err, 0, "failed to stop CAN controller (err %d)", err);
1013
1014 err = can_stop(can_dev);
1015 zassert_not_equal(err, 0, "stopped CAN controller while stopped");
1016 zassert_equal(err, -EALREADY, "wrong error return code (err %d)", err);
1017
1018 err = can_start(can_dev);
1019 zassert_equal(err, 0, "failed to start CAN controller (err %d)", err);
1020 }
1021
1022 /**
1023 * @brief Test starting is not allowed while started.
1024 */
ZTEST_USER(can_classic,test_start_while_started)1025 ZTEST_USER(can_classic, test_start_while_started)
1026 {
1027 int err;
1028
1029 err = can_start(can_dev);
1030 zassert_not_equal(err, 0, "started CAN controller while started");
1031 zassert_equal(err, -EALREADY, "wrong error return code (err %d)", err);
1032 }
1033
1034 /**
1035 * @brief Test recover is not allowed while started.
1036 */
ZTEST_USER(can_classic,test_recover_while_stopped)1037 ZTEST_USER(can_classic, test_recover_while_stopped)
1038 {
1039 int err;
1040
1041 err = can_stop(can_dev);
1042 zassert_equal(err, 0, "failed to stop CAN controller (err %d)", err);
1043
1044 err = can_recover(can_dev, K_NO_WAIT);
1045 zassert_not_equal(err, 0, "recovered bus while stopped");
1046 zassert_equal(err, -ENETDOWN, "wrong error return code (err %d)", err);
1047
1048 err = can_start(can_dev);
1049 zassert_equal(err, 0, "failed to start CAN controller (err %d)", err);
1050 }
1051
1052 /**
1053 * @brief Test sending is not allowed while stopped.
1054 */
ZTEST_USER(can_classic,test_send_while_stopped)1055 ZTEST_USER(can_classic, test_send_while_stopped)
1056 {
1057 int err;
1058
1059 err = can_stop(can_dev);
1060 zassert_equal(err, 0, "failed to stop CAN controller (err %d)", err);
1061
1062 err = can_send(can_dev, &test_std_frame_1, TEST_SEND_TIMEOUT, NULL, NULL);
1063 zassert_not_equal(err, 0, "sent a frame in stopped state");
1064 zassert_equal(err, -ENETDOWN, "wrong error return code (err %d)", err);
1065
1066 err = can_start(can_dev);
1067 zassert_equal(err, 0, "failed to start CAN controller (err %d)", err);
1068 }
1069
1070 /**
1071 * @brief Test setting bitrate is not allowed while started.
1072 */
ZTEST_USER(can_classic,test_set_bitrate_while_started)1073 ZTEST_USER(can_classic, test_set_bitrate_while_started)
1074 {
1075 int err;
1076
1077 err = can_set_bitrate(can_dev, TEST_BITRATE_2);
1078 zassert_not_equal(err, 0, "changed bitrate while started");
1079 zassert_equal(err, -EBUSY, "wrong error return code (err %d)", err);
1080 }
1081
1082 /**
1083 * @brief Test setting timing is not allowed while started.
1084 */
ZTEST_USER(can_classic,test_set_timing_while_started)1085 ZTEST_USER(can_classic, test_set_timing_while_started)
1086 {
1087 struct can_timing timing = { 0 };
1088 int err;
1089
1090 err = can_calc_timing(can_dev, &timing, TEST_BITRATE_1, TEST_SAMPLE_POINT);
1091 zassert_ok(err, "failed to calculate timing (err %d)", err);
1092
1093 err = can_set_timing(can_dev, &timing);
1094 zassert_not_equal(err, 0, "changed timing while started");
1095 zassert_equal(err, -EBUSY, "wrong error return code (err %d)", err);
1096 }
1097
1098 /**
1099 * @brief Test setting mode is not allowed while started.
1100 */
ZTEST_USER(can_classic,test_set_mode_while_started)1101 ZTEST_USER(can_classic, test_set_mode_while_started)
1102 {
1103 int err;
1104
1105 err = can_set_mode(can_dev, CAN_MODE_NORMAL);
1106 zassert_not_equal(err, 0, "changed mode while started");
1107 zassert_equal(err, -EBUSY, "wrong error return code (err %d)", err);
1108 }
1109
can_classic_setup(void)1110 void *can_classic_setup(void)
1111 {
1112 int err;
1113
1114 k_sem_init(&rx_callback_sem, 0, 2);
1115 k_sem_init(&tx_callback_sem, 0, 2);
1116
1117 k_object_access_grant(&can_msgq, k_current_get());
1118 k_object_access_grant(can_dev, k_current_get());
1119
1120 zassert_true(device_is_ready(can_dev), "CAN device not ready");
1121
1122 (void)can_stop(can_dev);
1123
1124 err = can_set_mode(can_dev, CAN_MODE_LOOPBACK);
1125 zassert_equal(err, 0, "failed to set loopback mode (err %d)", err);
1126 zassert_equal(CAN_MODE_LOOPBACK, can_get_mode(can_dev));
1127
1128 err = can_start(can_dev);
1129 zassert_equal(err, 0, "failed to start CAN controller (err %d)", err);
1130
1131 return NULL;
1132 }
1133
1134 ZTEST_SUITE(can_classic, NULL, can_classic_setup, NULL, NULL, NULL);
1135