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 static 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 bitrate limits.
480 */
ZTEST_USER(can_classic,test_bitrate_limits)481 ZTEST_USER(can_classic, test_bitrate_limits)
482 {
483 uint32_t min = can_get_bitrate_min(can_dev);
484 uint32_t max = can_get_bitrate_max(can_dev);
485
486 zassert_true(min <= max, "min bitrate must be lower or equal to max bitrate");
487 }
488
489 /**
490 * @brief Test setting a too low bitrate.
491 */
ZTEST_USER(can_classic,test_set_bitrate_too_low)492 ZTEST_USER(can_classic, test_set_bitrate_too_low)
493 {
494 uint32_t min = can_get_bitrate_min(can_dev);
495 int err;
496
497 if (min == 0) {
498 ztest_test_skip();
499 }
500
501 err = can_stop(can_dev);
502 zassert_equal(err, 0, "failed to stop CAN controller (err %d)", err);
503
504 err = can_set_bitrate(can_dev, min - 1);
505 zassert_equal(err, -ENOTSUP, "too low bitrate accepted");
506
507 err = can_set_bitrate(can_dev, CONFIG_CAN_DEFAULT_BITRATE);
508 zassert_equal(err, 0, "failed to restore default bitrate");
509
510 err = can_start(can_dev);
511 zassert_equal(err, 0, "failed to start CAN controller (err %d)", err);
512 }
513
514 /**
515 * @brief Test setting a too high bitrate.
516 */
ZTEST_USER(can_classic,test_set_bitrate_too_high)517 ZTEST_USER(can_classic, test_set_bitrate_too_high)
518 {
519 uint32_t max = can_get_bitrate_max(can_dev);
520 int err;
521
522 err = can_stop(can_dev);
523 zassert_equal(err, 0, "failed to stop CAN controller (err %d)", err);
524
525 err = can_set_bitrate(can_dev, max + 1);
526 zassert_equal(err, -ENOTSUP, "too high bitrate accepted");
527
528 err = can_start(can_dev);
529 zassert_equal(err, 0, "failed to start CAN controller (err %d)", err);
530 }
531
532 /**
533 * @brief Test using an invalid sample point.
534 */
ZTEST_USER(can_classic,test_invalid_sample_point)535 ZTEST_USER(can_classic, test_invalid_sample_point)
536 {
537 struct can_timing timing;
538 int err;
539
540 err = can_calc_timing(can_dev, &timing, TEST_BITRATE_1, 1000);
541 zassert_equal(err, -EINVAL, "invalid sample point of 100.0% accepted (err %d)", err);
542 }
543
544 /**
545 * @brief Test setting bitrate.
546 */
ZTEST_USER(can_classic,test_set_bitrate)547 ZTEST_USER(can_classic, test_set_bitrate)
548 {
549 int err;
550
551 err = can_stop(can_dev);
552 zassert_equal(err, 0, "failed to stop CAN controller (err %d)", err);
553
554 err = can_set_bitrate(can_dev, TEST_BITRATE_1);
555 zassert_equal(err, 0, "failed to set bitrate");
556
557 err = can_set_bitrate(can_dev, CONFIG_CAN_DEFAULT_BITRATE);
558 zassert_equal(err, 0, "failed to restore default bitrate");
559
560 err = can_start(can_dev);
561 zassert_equal(err, 0, "failed to start CAN controller (err %d)", err);
562 }
563
564 /**
565 * @brief Test that the minimum timing values can be set.
566 */
ZTEST_USER(can_classic,test_set_timing_min)567 ZTEST_USER(can_classic, test_set_timing_min)
568 {
569 int err;
570
571 err = can_stop(can_dev);
572 zassert_equal(err, 0, "failed to stop CAN controller (err %d)", err);
573
574 err = can_set_timing(can_dev, can_get_timing_min(can_dev));
575 zassert_equal(err, 0, "failed to set minimum timing parameters (err %d)", err);
576
577 err = can_set_bitrate(can_dev, CONFIG_CAN_DEFAULT_BITRATE);
578 zassert_equal(err, 0, "failed to restore default bitrate");
579
580 err = can_start(can_dev);
581 zassert_equal(err, 0, "failed to start CAN controller (err %d)", err);
582 }
583
584 /**
585 * @brief Test that the maximum timing values can be set.
586 */
ZTEST_USER(can_classic,test_set_timing_max)587 ZTEST_USER(can_classic, test_set_timing_max)
588 {
589 int err;
590
591 err = can_stop(can_dev);
592 zassert_equal(err, 0, "failed to stop CAN controller (err %d)", err);
593
594 err = can_set_timing(can_dev, can_get_timing_max(can_dev));
595 zassert_equal(err, 0, "failed to set maximum timing parameters (err %d)", err);
596
597 err = can_set_bitrate(can_dev, CONFIG_CAN_DEFAULT_BITRATE);
598 zassert_equal(err, 0, "failed to restore default bitrate");
599
600 err = can_start(can_dev);
601 zassert_equal(err, 0, "failed to start CAN controller (err %d)", err);
602 }
603
604 /**
605 * @brief Test sending a message with no filters installed.
606 *
607 * This basic test work since the CAN controller is in loopback mode and
608 * therefore ACKs its own frame.
609 */
ZTEST_USER(can_classic,test_send_and_forget)610 ZTEST_USER(can_classic, test_send_and_forget)
611 {
612 send_test_frame(can_dev, &test_std_frame_1);
613 }
614
615 /**
616 * @brief Test adding basic filters.
617 *
618 * Test each filter type but only one filter at a time.
619 */
ZTEST(can_classic,test_add_filter)620 ZTEST(can_classic, test_add_filter)
621 {
622 int filter_id;
623
624 filter_id = add_rx_filter(can_dev, &test_std_filter_1, rx_std_callback_1);
625 can_remove_rx_filter(can_dev, filter_id);
626
627 filter_id = add_rx_filter(can_dev, &test_ext_filter_1, rx_ext_callback_1);
628 can_remove_rx_filter(can_dev, filter_id);
629
630 filter_id = add_rx_msgq(can_dev, &test_std_filter_1);
631 can_remove_rx_filter(can_dev, filter_id);
632
633 filter_id = add_rx_msgq(can_dev, &test_ext_filter_1);
634 can_remove_rx_filter(can_dev, filter_id);
635
636 filter_id = add_rx_filter(can_dev, &test_std_masked_filter_1, rx_std_mask_callback_1);
637 can_remove_rx_filter(can_dev, filter_id);
638
639 filter_id = add_rx_filter(can_dev, &test_ext_masked_filter_1, rx_ext_mask_callback_1);
640 can_remove_rx_filter(can_dev, filter_id);
641 }
642
643 /**
644 * @brief Test adding filter without callback.
645 */
ZTEST(can_classic,test_add_filter_without_callback)646 ZTEST(can_classic, test_add_filter_without_callback)
647 {
648 int err;
649
650 Z_TEST_SKIP_IFNDEF(CONFIG_RUNTIME_ERROR_CHECKS);
651
652 err = can_add_rx_filter(can_dev, NULL, NULL, &test_std_filter_1);
653 zassert_equal(err, -EINVAL, "added filter with NULL callback");
654 }
655
656 /**
657 * @brief Test adding an invalid CAN RX filter.
658 *
659 * @param dev Pointer to the device structure for the driver instance.
660 * @param frame Pointer to the CAN RX filter.
661 */
add_invalid_rx_filter(const struct device * dev,const struct can_filter * filter)662 static void add_invalid_rx_filter(const struct device *dev, const struct can_filter *filter)
663 {
664 int filter_id;
665
666 Z_TEST_SKIP_IFNDEF(CONFIG_RUNTIME_ERROR_CHECKS);
667
668 filter_id = can_add_rx_filter(dev, rx_std_callback_1, NULL, filter);
669 zassert_equal(filter_id, -EINVAL, "added invalid filter");
670 }
671
672 /**
673 * @brief Test adding NULL filter.
674 */
ZTEST(can_classic,test_add_invalid_null_filter)675 ZTEST(can_classic, test_add_invalid_null_filter)
676 {
677 add_invalid_rx_filter(can_dev, NULL);
678 }
679
680 /**
681 * @brief Test adding invalid standard (11-bit) filters.
682 */
ZTEST(can_classic,test_add_invalid_std_filter)683 ZTEST(can_classic, test_add_invalid_std_filter)
684 {
685 struct can_filter filter = {
686 .flags = 0U,
687 };
688
689 filter.id = CAN_STD_ID_MASK;
690 filter.mask = CAN_STD_ID_MASK + 1U;
691 add_invalid_rx_filter(can_dev, &filter);
692
693 filter.id = CAN_STD_ID_MASK + 1U;
694 filter.mask = CAN_STD_ID_MASK;
695 add_invalid_rx_filter(can_dev, &filter);
696 }
697
698 /**
699 * @brief Test adding invalid extended (29-bit) filters.
700 */
ZTEST(can_classic,test_add_invalid_ext_filter)701 ZTEST(can_classic, test_add_invalid_ext_filter)
702 {
703 struct can_filter filter = {
704 .flags = CAN_FILTER_IDE,
705 };
706
707 filter.id = CAN_EXT_ID_MASK;
708 filter.mask = CAN_EXT_ID_MASK + 1U;
709 add_invalid_rx_filter(can_dev, &filter);
710
711 filter.id = CAN_EXT_ID_MASK + 1U;
712 filter.mask = CAN_EXT_ID_MASK;
713 add_invalid_rx_filter(can_dev, &filter);
714 }
715
716 /**
717 * @brief Test adding up to and above the maximum number of RX filters.
718 *
719 * @param ide standard (11-bit) CAN ID filters if false, or extended (29-bit) CAN ID filters if
720 * true.
721 * @param id_mask filter
722 */
add_remove_max_filters(bool ide)723 static void add_remove_max_filters(bool ide)
724 {
725 uint32_t id_mask = ide ? CAN_EXT_ID_MASK : CAN_STD_ID_MASK;
726 struct can_filter filter = {
727 .flags = (ide ? CAN_FILTER_IDE : 0),
728 .id = 0,
729 .mask = id_mask,
730 };
731 int filter_id;
732 int max;
733 int i;
734
735 max = can_get_max_filters(can_dev, ide);
736 if (max == -ENOSYS || max == 0) {
737 /*
738 * Skip test if max is not known or no filters of the given type
739 * is supported.
740 */
741 ztest_test_skip();
742 }
743
744 zassert_true(max > 0, "failed to get max filters (err %d)", max);
745
746 int filter_ids[max];
747
748 for (i = 0; i < max; i++) {
749 filter.id++;
750 filter_ids[i] = add_rx_msgq(can_dev, &filter);
751 }
752
753 filter.id++;
754 filter_id = can_add_rx_filter_msgq(can_dev, &can_msgq, &filter);
755 zassert_equal(filter_id, -ENOSPC, "added more than max filters");
756
757 for (i = 0; i < max; i++) {
758 can_remove_rx_filter(can_dev, filter_ids[i]);
759 }
760 }
761
762 /**
763 * @brief Test max standard (11-bit) CAN RX filters.
764 */
ZTEST_USER(can_classic,test_max_std_filters)765 ZTEST_USER(can_classic, test_max_std_filters)
766 {
767 add_remove_max_filters(false);
768 }
769
770 /**
771 * @brief Test max extended (29-bit) CAN RX filters.
772 */
ZTEST_USER(can_classic,test_max_ext_filters)773 ZTEST_USER(can_classic, test_max_ext_filters)
774 {
775 add_remove_max_filters(true);
776 }
777
778 /**
779 * @brief Test that no message is received when nothing was sent.
780 */
ZTEST_USER(can_classic,test_receive_timeout)781 ZTEST_USER(can_classic, test_receive_timeout)
782 {
783 struct can_frame frame;
784 int filter_id;
785 int err;
786
787 filter_id = add_rx_msgq(can_dev, &test_std_filter_1);
788
789 err = k_msgq_get(&can_msgq, &frame, TEST_RECEIVE_TIMEOUT);
790 zassert_equal(err, -EAGAIN, "received a frame without sending one");
791
792 can_remove_rx_filter(can_dev, filter_id);
793 }
794
795 /**
796 * @brief Test that transmit callback function is called.
797 */
ZTEST(can_classic,test_send_callback)798 ZTEST(can_classic, test_send_callback)
799 {
800 int err;
801
802 k_sem_reset(&tx_callback_sem);
803
804 send_test_frame_nowait(can_dev, &test_std_frame_1, tx_std_callback_1);
805
806 err = k_sem_take(&tx_callback_sem, TEST_SEND_TIMEOUT);
807 zassert_equal(err, 0, "missing TX callback");
808 }
809
810 /**
811 * @brief Test sending an invalid CAN frame.
812 *
813 * @param dev Pointer to the device structure for the driver instance.
814 * @param frame Pointer to the CAN frame to send.
815 */
send_invalid_frame(const struct device * dev,const struct can_frame * frame)816 static void send_invalid_frame(const struct device *dev, const struct can_frame *frame)
817 {
818 int err;
819
820 Z_TEST_SKIP_IFNDEF(CONFIG_RUNTIME_ERROR_CHECKS);
821
822 err = can_send(dev, frame, TEST_SEND_TIMEOUT, NULL, NULL);
823 zassert_equal(err, -EINVAL, "wrong error on sending invalid frame (err %d)", err);
824 }
825
826 /**
827 * @brief Test sending NULL frame.
828 */
ZTEST(can_classic,test_send_null_frame)829 ZTEST(can_classic, test_send_null_frame)
830 {
831 send_invalid_frame(can_dev, NULL);
832 }
833
834 /**
835 * @brief Test sending frame with standard (11-bit) CAN ID out-of-range.
836 */
ZTEST(can_classic,test_send_std_id_out_of_range)837 ZTEST(can_classic, test_send_std_id_out_of_range)
838 {
839 struct can_frame frame = {
840 .id = CAN_STD_ID_MASK + 1U,
841 };
842
843 send_invalid_frame(can_dev, &frame);
844 }
845
846 /**
847 * @brief Test sending frame with extended (29-bit) CAN ID out-of-range.
848 */
ZTEST(can_classic,test_send_ext_id_out_of_range)849 ZTEST(can_classic, test_send_ext_id_out_of_range)
850 {
851 struct can_frame frame = {
852 .flags = CAN_FRAME_IDE,
853 .id = CAN_EXT_ID_MASK + 1U,
854 };
855
856 send_invalid_frame(can_dev, &frame);
857 }
858
859 /**
860 * @brief Test sending standard (11-bit ID) CAN frame with too big payload.
861 */
ZTEST(can_classic,test_send_std_id_dlc_of_range)862 ZTEST(can_classic, test_send_std_id_dlc_of_range)
863 {
864 struct can_frame frame = {
865 .id = TEST_CAN_STD_ID_1,
866 .dlc = CAN_MAX_DLC + 1U,
867 };
868
869 send_invalid_frame(can_dev, &frame);
870 }
871
872 /**
873 * @brief Test sending extended (29-bit ID) CAN frame with too big payload.
874 */
ZTEST(can_classic,test_send_ext_id_dlc_of_range)875 ZTEST(can_classic, test_send_ext_id_dlc_of_range)
876 {
877 struct can_frame frame = {
878 .flags = CAN_FRAME_IDE,
879 .id = TEST_CAN_EXT_ID_1,
880 .dlc = CAN_MAX_DLC + 1U,
881 };
882
883 send_invalid_frame(can_dev, &frame);
884 }
885
886 /**
887 * @brief Test send/receive with standard (11-bit) CAN IDs.
888 */
ZTEST(can_classic,test_send_receive_std_id)889 ZTEST(can_classic, test_send_receive_std_id)
890 {
891 send_receive(&test_std_filter_1, &test_std_filter_2,
892 &test_std_frame_1, &test_std_frame_2);
893 }
894
895 /**
896 * @brief Test send/receive with standard (11-bit) CAN IDs without no data.
897 */
ZTEST(can_classic,test_send_receive_std_id_no_data)898 ZTEST(can_classic, test_send_receive_std_id_no_data)
899 {
900 const struct can_frame frame = {
901 .flags = 0,
902 .id = TEST_CAN_STD_ID_1,
903 .dlc = 0,
904 .data = { 0 }
905 };
906 struct can_frame frame_buffer;
907 int filter_id;
908 int err;
909
910 filter_id = add_rx_msgq(can_dev, &test_std_filter_1);
911 err = can_send(can_dev, &frame, TEST_SEND_TIMEOUT, NULL, NULL);
912 zassert_equal(err, 0, "failed to send frame without data (err %d)", err);
913
914 err = k_msgq_get(&can_msgq, &frame_buffer, TEST_RECEIVE_TIMEOUT);
915 zassert_equal(err, 0, "receive timeout");
916
917 assert_frame_equal(&frame, &frame_buffer, 0);
918
919 can_remove_rx_filter(can_dev, filter_id);
920 }
921
922 /**
923 * @brief Test send/receive with extended (29-bit) CAN IDs.
924 */
ZTEST(can_classic,test_send_receive_ext_id)925 ZTEST(can_classic, test_send_receive_ext_id)
926 {
927 send_receive(&test_ext_filter_1, &test_ext_filter_2,
928 &test_ext_frame_1, &test_ext_frame_2);
929 }
930
931 /**
932 * @brief Test send/receive with standard (11-bit) masked CAN IDs.
933 */
ZTEST(can_classic,test_send_receive_std_id_masked)934 ZTEST(can_classic, test_send_receive_std_id_masked)
935 {
936 send_receive(&test_std_masked_filter_1, &test_std_masked_filter_2,
937 &test_std_frame_1, &test_std_frame_2);
938 }
939
940 /**
941 * @brief Test send/receive with extended (29-bit) masked CAN IDs.
942 */
ZTEST(can_classic,test_send_receive_ext_id_masked)943 ZTEST(can_classic, test_send_receive_ext_id_masked)
944 {
945 send_receive(&test_ext_masked_filter_1, &test_ext_masked_filter_2,
946 &test_ext_frame_1, &test_ext_frame_2);
947 }
948
949 /**
950 * @brief Test send/receive with messages buffered in a CAN message queue.
951 */
ZTEST_USER(can_classic,test_send_receive_msgq)952 ZTEST_USER(can_classic, test_send_receive_msgq)
953 {
954 struct k_msgq_attrs attrs;
955 struct can_frame frame;
956 int filter_id;
957 int nframes;
958 int err;
959 int i;
960
961 filter_id = add_rx_msgq(can_dev, &test_std_filter_1);
962
963 k_msgq_get_attrs(&can_msgq, &attrs);
964 nframes = attrs.max_msgs;
965
966 for (i = 0; i < nframes; i++) {
967 send_test_frame(can_dev, &test_std_frame_1);
968 }
969
970 for (i = 0; i < nframes; i++) {
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
976 for (i = 0; i < nframes; i++) {
977 send_test_frame(can_dev, &test_std_frame_1);
978 }
979
980 for (i = 0; i < nframes; i++) {
981 err = k_msgq_get(&can_msgq, &frame, TEST_RECEIVE_TIMEOUT);
982 zassert_equal(err, 0, "receive timeout");
983 assert_frame_equal(&frame, &test_std_frame_1, 0);
984 }
985
986 can_remove_rx_filter(can_dev, filter_id);
987 }
988
989 /**
990 * @brief Test send/receive with standard (11-bit) CAN IDs and remote transmission request (RTR).
991 */
ZTEST_USER(can_classic,test_send_receive_std_id_rtr)992 ZTEST_USER(can_classic, test_send_receive_std_id_rtr)
993 {
994 Z_TEST_SKIP_IFNDEF(CONFIG_CAN_ACCEPT_RTR);
995
996 send_receive_rtr(&test_std_filter_1, &test_std_frame_1, &test_std_rtr_frame_1);
997 }
998
999 /**
1000 * @brief Test send/receive with extended (29-bit) CAN IDs and remote transmission request (RTR).
1001 */
ZTEST_USER(can_classic,test_send_receive_ext_id_rtr)1002 ZTEST_USER(can_classic, test_send_receive_ext_id_rtr)
1003 {
1004 Z_TEST_SKIP_IFNDEF(CONFIG_CAN_ACCEPT_RTR);
1005
1006 send_receive_rtr(&test_ext_filter_1, &test_ext_frame_1, &test_ext_rtr_frame_1);
1007 }
1008
1009 /**
1010 * @brief Test rejection of standard (11-bit) CAN IDs and remote transmission request (RTR).
1011 */
ZTEST_USER(can_classic,test_reject_std_id_rtr)1012 ZTEST_USER(can_classic, test_reject_std_id_rtr)
1013 {
1014 struct can_frame frame_buffer;
1015 int filter_id;
1016 int err;
1017
1018 Z_TEST_SKIP_IFDEF(CONFIG_CAN_ACCEPT_RTR);
1019
1020 filter_id = add_rx_msgq(can_dev, &test_std_filter_1);
1021
1022 err = can_send(can_dev, &test_std_rtr_frame_1, TEST_SEND_TIMEOUT, NULL, NULL);
1023 if (err == -ENOTSUP) {
1024 /* Not all drivers support transmission of RTR frames */
1025 can_remove_rx_filter(can_dev, filter_id);
1026 ztest_test_skip();
1027 }
1028 zassert_equal(err, 0, "failed to send RTR frame (err %d)", err);
1029
1030 err = k_msgq_get(&can_msgq, &frame_buffer, TEST_RECEIVE_TIMEOUT);
1031 zassert_equal(err, -EAGAIN, "received a frame that should be rejected");
1032
1033 can_remove_rx_filter(can_dev, filter_id);
1034 }
1035
1036 /**
1037 * @brief Test rejection of extended (29-bit) CAN IDs and remote transmission request (RTR).
1038 */
ZTEST_USER(can_classic,test_reject_ext_id_rtr)1039 ZTEST_USER(can_classic, test_reject_ext_id_rtr)
1040 {
1041 struct can_frame frame_buffer;
1042 int filter_id;
1043 int err;
1044
1045 Z_TEST_SKIP_IFDEF(CONFIG_CAN_ACCEPT_RTR);
1046
1047 filter_id = add_rx_msgq(can_dev, &test_ext_filter_1);
1048
1049 err = can_send(can_dev, &test_ext_rtr_frame_1, TEST_SEND_TIMEOUT, NULL, NULL);
1050 if (err == -ENOTSUP) {
1051 /* Not all drivers support transmission of RTR frames */
1052 can_remove_rx_filter(can_dev, filter_id);
1053 ztest_test_skip();
1054 }
1055 zassert_equal(err, 0, "failed to send RTR frame (err %d)", err);
1056
1057 err = k_msgq_get(&can_msgq, &frame_buffer, TEST_RECEIVE_TIMEOUT);
1058 zassert_equal(err, -EAGAIN, "received a frame that should be rejected");
1059
1060 can_remove_rx_filter(can_dev, filter_id);
1061 }
1062
1063 /**
1064 * @brief Test that non-matching CAN frames do not pass a filter.
1065 */
ZTEST(can_classic,test_send_receive_wrong_id)1066 ZTEST(can_classic, test_send_receive_wrong_id)
1067 {
1068 struct can_frame frame_buffer;
1069 int filter_id;
1070 int err;
1071
1072 filter_id = add_rx_msgq(can_dev, &test_std_filter_1);
1073
1074 send_test_frame(can_dev, &test_std_frame_2);
1075
1076 err = k_msgq_get(&can_msgq, &frame_buffer, TEST_RECEIVE_TIMEOUT);
1077 zassert_equal(err, -EAGAIN, "received a frame that should not pass the filter");
1078
1079 can_remove_rx_filter(can_dev, filter_id);
1080 }
1081
1082 /**
1083 * @brief Test that frames with invalid Data Length Code (DLC) are rejected.
1084 */
ZTEST_USER(can_classic,test_send_invalid_dlc)1085 ZTEST_USER(can_classic, test_send_invalid_dlc)
1086 {
1087 struct can_frame frame = {0};
1088 int err;
1089
1090 frame.id = TEST_CAN_STD_ID_1;
1091 frame.dlc = CAN_MAX_DLC + 1;
1092
1093 err = can_send(can_dev, &frame, TEST_SEND_TIMEOUT, NULL, NULL);
1094 zassert_equal(err, -EINVAL, "sent a frame with an invalid DLC");
1095 }
1096
1097 /**
1098 * @brief Test that CAN FD format frames are rejected in non-FD mode.
1099 */
ZTEST_USER(can_classic,test_send_fd_format)1100 ZTEST_USER(can_classic, test_send_fd_format)
1101 {
1102 struct can_frame frame = {0};
1103 int err;
1104
1105 frame.id = TEST_CAN_STD_ID_1;
1106 frame.dlc = 0;
1107 frame.flags = CAN_FRAME_FDF;
1108
1109 err = can_send(can_dev, &frame, TEST_SEND_TIMEOUT, NULL, NULL);
1110 zassert_equal(err, -ENOTSUP, "sent a CAN FD format frame in non-FD mode");
1111 }
1112
1113 /**
1114 * @brief Test CAN controller bus recovery.
1115 *
1116 * It is not possible to provoke a bus off state, but verify the API call return codes.
1117 */
ZTEST_USER(can_classic,test_recover)1118 ZTEST_USER(can_classic, test_recover)
1119 {
1120 can_mode_t cap;
1121 int err;
1122
1123 Z_TEST_SKIP_IFNDEF(CONFIG_CAN_MANUAL_RECOVERY_MODE);
1124
1125 err = can_get_capabilities(can_dev, &cap);
1126 zassert_equal(err, 0, "failed to get CAN capabilities (err %d)", err);
1127
1128 if ((cap & CAN_MODE_MANUAL_RECOVERY) != 0U) {
1129 /* Check that manual recovery fails when not in manual recovery mode */
1130 err = can_recover(can_dev, TEST_RECOVER_TIMEOUT);
1131 zassert_equal(err, -ENOTSUP, "wrong error return code (err %d)", err);
1132
1133 err = can_stop(can_dev);
1134 zassert_equal(err, 0, "failed to stop CAN controller (err %d)", err);
1135
1136 /* Enter manual recovery mode */
1137 err = can_set_mode(can_dev, CAN_MODE_NORMAL | CAN_MODE_MANUAL_RECOVERY);
1138 zassert_equal(err, 0, "failed to set manual recovery mode (err %d)", err);
1139 zassert_equal(CAN_MODE_NORMAL | CAN_MODE_MANUAL_RECOVERY, can_get_mode(can_dev));
1140
1141 err = can_start(can_dev);
1142 zassert_equal(err, 0, "failed to start CAN controller (err %d)", err);
1143 }
1144
1145 err = can_recover(can_dev, TEST_RECOVER_TIMEOUT);
1146
1147 if ((cap & CAN_MODE_MANUAL_RECOVERY) != 0U) {
1148 zassert_equal(err, 0, "failed to recover (err %d)", err);
1149
1150 err = can_stop(can_dev);
1151 zassert_equal(err, 0, "failed to stop CAN controller (err %d)", err);
1152
1153 /* Restore loopback mode */
1154 err = can_set_mode(can_dev, CAN_MODE_LOOPBACK);
1155 zassert_equal(err, 0, "failed to set loopback-mode (err %d)", err);
1156 zassert_equal(CAN_MODE_LOOPBACK, can_get_mode(can_dev));
1157
1158 err = can_start(can_dev);
1159 zassert_equal(err, 0, "failed to start CAN controller (err %d)", err);
1160 } else {
1161 /* Check that manual recovery fails when not supported */
1162 zassert_equal(err, -ENOSYS, "wrong error return code (err %d)", err);
1163 }
1164 }
1165
1166 /**
1167 * @brief Test retrieving the state of the CAN controller.
1168 */
ZTEST_USER(can_classic,test_get_state)1169 ZTEST_USER(can_classic, test_get_state)
1170 {
1171 struct can_bus_err_cnt err_cnt;
1172 enum can_state state;
1173 int err;
1174
1175 err = can_get_state(can_dev, NULL, NULL);
1176 zassert_equal(err, 0, "failed to get CAN state without destinations (err %d)", err);
1177
1178 err = can_get_state(can_dev, &state, NULL);
1179 zassert_equal(err, 0, "failed to get CAN state (err %d)", err);
1180
1181 err = can_get_state(can_dev, NULL, &err_cnt);
1182 zassert_equal(err, 0, "failed to get CAN error counters (err %d)", err);
1183
1184 err = can_get_state(can_dev, &state, &err_cnt);
1185 zassert_equal(err, 0, "failed to get CAN state + error counters (err %d)", err);
1186 }
1187
1188 /**
1189 * @brief Test that CAN RX filters are preserved through CAN controller mode changes.
1190 */
ZTEST_USER(can_classic,test_filters_preserved_through_mode_change)1191 ZTEST_USER(can_classic, test_filters_preserved_through_mode_change)
1192 {
1193 struct can_frame frame;
1194 enum can_state state;
1195 int filter_id;
1196 int err;
1197
1198 filter_id = add_rx_msgq(can_dev, &test_std_filter_1);
1199 send_test_frame(can_dev, &test_std_frame_1);
1200
1201 err = k_msgq_get(&can_msgq, &frame, TEST_RECEIVE_TIMEOUT);
1202 zassert_equal(err, 0, "receive timeout");
1203 assert_frame_equal(&frame, &test_std_frame_1, 0);
1204
1205 err = can_stop(can_dev);
1206 zassert_equal(err, 0, "failed to stop CAN controller (err %d)", err);
1207
1208 err = can_get_state(can_dev, &state, NULL);
1209 zassert_equal(err, 0, "failed to get CAN state (err %d)", err);
1210 zassert_equal(state, CAN_STATE_STOPPED, "CAN controller not stopped");
1211
1212 err = can_set_mode(can_dev, CAN_MODE_NORMAL);
1213 zassert_equal(err, 0, "failed to set normal mode (err %d)", err);
1214 zassert_equal(CAN_MODE_NORMAL, can_get_mode(can_dev));
1215
1216 err = can_set_mode(can_dev, CAN_MODE_LOOPBACK);
1217 zassert_equal(err, 0, "failed to set loopback-mode (err %d)", err);
1218 zassert_equal(CAN_MODE_LOOPBACK, can_get_mode(can_dev));
1219
1220 err = can_start(can_dev);
1221 zassert_equal(err, 0, "failed to start CAN controller (err %d)", err);
1222
1223 send_test_frame(can_dev, &test_std_frame_1);
1224
1225 err = k_msgq_get(&can_msgq, &frame, TEST_RECEIVE_TIMEOUT);
1226 zassert_equal(err, 0, "receive timeout");
1227 assert_frame_equal(&frame, &test_std_frame_1, 0);
1228
1229 can_remove_rx_filter(can_dev, filter_id);
1230 }
1231
1232 /**
1233 * @brief Test that CAN RX filters are preserved through CAN controller bitrate changes.
1234 */
ZTEST_USER(can_classic,test_filters_preserved_through_bitrate_change)1235 ZTEST_USER(can_classic, test_filters_preserved_through_bitrate_change)
1236 {
1237 struct can_frame frame;
1238 enum can_state state;
1239 int filter_id;
1240 int err;
1241
1242 filter_id = add_rx_msgq(can_dev, &test_std_filter_1);
1243 send_test_frame(can_dev, &test_std_frame_1);
1244
1245 err = k_msgq_get(&can_msgq, &frame, TEST_RECEIVE_TIMEOUT);
1246 zassert_equal(err, 0, "receive timeout");
1247 assert_frame_equal(&frame, &test_std_frame_1, 0);
1248
1249 err = can_stop(can_dev);
1250 zassert_equal(err, 0, "failed to stop CAN controller (err %d)", err);
1251
1252 err = can_get_state(can_dev, &state, NULL);
1253 zassert_equal(err, 0, "failed to get CAN state (err %d)", err);
1254 zassert_equal(state, CAN_STATE_STOPPED, "CAN controller not stopped");
1255
1256 err = can_set_bitrate(can_dev, TEST_BITRATE_2);
1257 zassert_equal(err, 0, "failed to set bitrate 2");
1258
1259 err = can_set_bitrate(can_dev, TEST_BITRATE_1);
1260 zassert_equal(err, 0, "failed to set bitrate 1");
1261
1262 err = can_set_bitrate(can_dev, CONFIG_CAN_DEFAULT_BITRATE);
1263 zassert_equal(err, 0, "failed to restore default bitrate");
1264
1265 err = can_start(can_dev);
1266 zassert_equal(err, 0, "failed to start CAN controller (err %d)", err);
1267
1268 send_test_frame(can_dev, &test_std_frame_1);
1269
1270 err = k_msgq_get(&can_msgq, &frame, TEST_RECEIVE_TIMEOUT);
1271 zassert_equal(err, 0, "receive timeout");
1272 assert_frame_equal(&frame, &test_std_frame_1, 0);
1273
1274 can_remove_rx_filter(can_dev, filter_id);
1275 }
1276
1277 /**
1278 * @brief Test that CAN RX filters can be added while CAN controller is stopped.
1279 */
ZTEST_USER(can_classic,test_filters_added_while_stopped)1280 ZTEST_USER(can_classic, test_filters_added_while_stopped)
1281 {
1282 struct can_frame frame;
1283 int filter_id;
1284 int err;
1285
1286 err = can_stop(can_dev);
1287 zassert_equal(err, 0, "failed to stop CAN controller (err %d)", err);
1288
1289 filter_id = add_rx_msgq(can_dev, &test_std_filter_1);
1290
1291 err = can_start(can_dev);
1292 zassert_equal(err, 0, "failed to start CAN controller (err %d)", err);
1293
1294 send_test_frame(can_dev, &test_std_frame_1);
1295
1296 err = k_msgq_get(&can_msgq, &frame, TEST_RECEIVE_TIMEOUT);
1297 zassert_equal(err, 0, "receive timeout");
1298 assert_frame_equal(&frame, &test_std_frame_1, 0);
1299
1300 can_remove_rx_filter(can_dev, filter_id);
1301 }
1302
1303 /**
1304 * @brief Test stopping is not allowed while stopped.
1305 */
ZTEST_USER(can_classic,test_stop_while_stopped)1306 ZTEST_USER(can_classic, test_stop_while_stopped)
1307 {
1308 int err;
1309
1310 err = can_stop(can_dev);
1311 zassert_equal(err, 0, "failed to stop CAN controller (err %d)", err);
1312
1313 err = can_stop(can_dev);
1314 zassert_not_equal(err, 0, "stopped CAN controller while stopped");
1315 zassert_equal(err, -EALREADY, "wrong error return code (err %d)", err);
1316
1317 err = can_start(can_dev);
1318 zassert_equal(err, 0, "failed to start CAN controller (err %d)", err);
1319 }
1320
1321 /**
1322 * @brief Test starting is not allowed while started.
1323 */
ZTEST_USER(can_classic,test_start_while_started)1324 ZTEST_USER(can_classic, test_start_while_started)
1325 {
1326 int err;
1327
1328 err = can_start(can_dev);
1329 zassert_not_equal(err, 0, "started CAN controller while started");
1330 zassert_equal(err, -EALREADY, "wrong error return code (err %d)", err);
1331 }
1332
1333 /**
1334 * @brief Test recover is not allowed while started.
1335 */
ZTEST_USER(can_classic,test_recover_while_stopped)1336 ZTEST_USER(can_classic, test_recover_while_stopped)
1337 {
1338 can_mode_t cap;
1339 int err;
1340
1341 Z_TEST_SKIP_IFNDEF(CONFIG_CAN_MANUAL_RECOVERY_MODE);
1342
1343 err = can_get_capabilities(can_dev, &cap);
1344 zassert_equal(err, 0, "failed to get CAN capabilities (err %d)", err);
1345
1346 if ((cap & CAN_MODE_MANUAL_RECOVERY) == 0U) {
1347 ztest_test_skip();
1348 }
1349
1350 err = can_stop(can_dev);
1351 zassert_equal(err, 0, "failed to stop CAN controller (err %d)", err);
1352
1353 err = can_recover(can_dev, K_NO_WAIT);
1354 zassert_not_equal(err, 0, "recovered bus while stopped");
1355 zassert_equal(err, -ENETDOWN, "wrong error return code (err %d)", err);
1356
1357 err = can_start(can_dev);
1358 zassert_equal(err, 0, "failed to start CAN controller (err %d)", err);
1359 }
1360
1361 /**
1362 * @brief Test sending is not allowed while stopped.
1363 */
ZTEST_USER(can_classic,test_send_while_stopped)1364 ZTEST_USER(can_classic, test_send_while_stopped)
1365 {
1366 int err;
1367
1368 err = can_stop(can_dev);
1369 zassert_equal(err, 0, "failed to stop CAN controller (err %d)", err);
1370
1371 err = can_send(can_dev, &test_std_frame_1, TEST_SEND_TIMEOUT, NULL, NULL);
1372 zassert_not_equal(err, 0, "sent a frame in stopped state");
1373 zassert_equal(err, -ENETDOWN, "wrong error return code (err %d)", err);
1374
1375 err = can_start(can_dev);
1376 zassert_equal(err, 0, "failed to start CAN controller (err %d)", err);
1377 }
1378
1379 /**
1380 * @brief Test setting bitrate is not allowed while started.
1381 */
ZTEST_USER(can_classic,test_set_bitrate_while_started)1382 ZTEST_USER(can_classic, test_set_bitrate_while_started)
1383 {
1384 int err;
1385
1386 err = can_set_bitrate(can_dev, TEST_BITRATE_2);
1387 zassert_not_equal(err, 0, "changed bitrate while started");
1388 zassert_equal(err, -EBUSY, "wrong error return code (err %d)", err);
1389 }
1390
1391 /**
1392 * @brief Test setting timing is not allowed while started.
1393 */
ZTEST_USER(can_classic,test_set_timing_while_started)1394 ZTEST_USER(can_classic, test_set_timing_while_started)
1395 {
1396 struct can_timing timing = { 0 };
1397 int err;
1398
1399 err = can_calc_timing(can_dev, &timing, TEST_BITRATE_1, TEST_SAMPLE_POINT);
1400 zassert_ok(err, "failed to calculate timing (err %d)", err);
1401
1402 err = can_set_timing(can_dev, &timing);
1403 zassert_not_equal(err, 0, "changed timing while started");
1404 zassert_equal(err, -EBUSY, "wrong error return code (err %d)", err);
1405 }
1406
1407 /**
1408 * @brief Test setting mode is not allowed while started.
1409 */
ZTEST_USER(can_classic,test_set_mode_while_started)1410 ZTEST_USER(can_classic, test_set_mode_while_started)
1411 {
1412 int err;
1413
1414 err = can_set_mode(can_dev, CAN_MODE_NORMAL);
1415 zassert_not_equal(err, 0, "changed mode while started");
1416 zassert_equal(err, -EBUSY, "wrong error return code (err %d)", err);
1417 }
1418
can_classic_setup(void)1419 void *can_classic_setup(void)
1420 {
1421 can_common_test_setup(CAN_MODE_LOOPBACK);
1422
1423 return NULL;
1424 }
1425
1426 ZTEST_SUITE(can_classic, NULL, can_classic_setup, NULL, NULL, NULL);
1427