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/device.h>
9 #include <zephyr/drivers/can.h>
10 #include <zephyr/kernel.h>
11 
12 /**
13  * Test bitrates in bits/second.
14  */
15 #define TEST_BITRATE_1 125000
16 #define TEST_BITRATE_2 250000
17 #define TEST_BITRATE_3 1000000
18 
19 /**
20  * Test sample points in per mille.
21  */
22 #define TEST_SAMPLE_POINT 875
23 #define TEST_SAMPLE_POINT_2 750
24 
25 /**
26  * @brief Test timeouts.
27  */
28 #define TEST_SEND_TIMEOUT    K_MSEC(100)
29 #define TEST_RECEIVE_TIMEOUT K_MSEC(100)
30 #define TEST_RECOVER_TIMEOUT K_MSEC(100)
31 
32 /**
33  * @brief Standard (11-bit) CAN IDs and masks used for testing.
34  */
35 #define TEST_CAN_STD_ID_1      0x555
36 #define TEST_CAN_STD_ID_2      0x565
37 #define TEST_CAN_STD_MASK_ID_1 0x55A
38 #define TEST_CAN_STD_MASK_ID_2 0x56A
39 #define TEST_CAN_STD_MASK      0x7F0
40 #define TEST_CAN_SOME_STD_ID   0x123
41 
42 /**
43  * @brief Extended (29-bit) CAN IDs and masks used for testing.
44  */
45 #define TEST_CAN_EXT_ID_1      0x15555555
46 #define TEST_CAN_EXT_ID_2      0x15555565
47 #define TEST_CAN_EXT_MASK_ID_1 0x1555555A
48 #define TEST_CAN_EXT_MASK_ID_2 0x1555556A
49 #define TEST_CAN_EXT_MASK      0x1FFFFFF0
50 
51 /**
52  * @brief Common variables.
53  */
54 extern ZTEST_DMEM const struct device *const can_dev;
55 extern ZTEST_DMEM const struct device *const can_phy;
56 extern struct k_sem rx_callback_sem;
57 extern struct k_sem tx_callback_sem;
58 extern struct k_msgq can_msgq;
59 
60 /**
61  * @brief Standard (11-bit) CAN ID frame 1.
62  */
63 extern const struct can_frame test_std_frame_1;
64 
65 /**
66  * @brief Standard (11-bit) CAN ID frame 2.
67  */
68 extern const struct can_frame test_std_frame_2;
69 
70 /**
71  * @brief Extended (29-bit) CAN ID frame 1.
72  */
73 extern const struct can_frame test_ext_frame_1;
74 
75 /**
76  * @brief Extended (29-bit) CAN ID frame 1.
77  */
78 extern const struct can_frame test_ext_frame_2;
79 
80 /**
81  * @brief Standard (11-bit) CAN ID RTR frame 1.
82  */
83 extern const struct can_frame test_std_rtr_frame_1;
84 
85 /**
86  * @brief Extended (29-bit) CAN ID RTR frame 1.
87  */
88 extern const struct can_frame test_ext_rtr_frame_1;
89 
90 #ifdef CONFIG_CAN_FD_MODE
91 /**
92  * @brief Standard (11-bit) CAN ID frame 1 with CAN FD payload.
93  */
94 extern const struct can_frame test_std_fdf_frame_1;
95 
96 /**
97  * @brief Standard (11-bit) CAN ID frame 2 with CAN FD payload.
98  */
99 extern const struct can_frame test_std_fdf_frame_2;
100 #endif /* CONFIG_CAN_FD_MODE */
101 
102 /**
103  * @brief Standard (11-bit) CAN ID filter 1. This filter matches
104  * ``test_std_frame_1`` and ``test_std_fdf_frame_1``.
105  */
106 extern const struct can_filter test_std_filter_1;
107 
108 /**
109  * @brief Standard (11-bit) CAN ID filter 2. This filter matches
110  * ``test_std_frame_2`` and ``test_std_fdf_frame_2``.
111  */
112 extern const struct can_filter test_std_filter_2;
113 
114 /**
115  * @brief Standard (11-bit) CAN ID masked filter 1. This filter matches
116  * ``test_std_frame_1``.
117  */
118 extern const struct can_filter test_std_masked_filter_1;
119 
120 /**
121  * @brief Standard (11-bit) CAN ID masked filter 2. This filter matches
122  * ``test_std_frame_2``.
123  */
124 extern const struct can_filter test_std_masked_filter_2;
125 
126 /**
127  * @brief Extended (29-bit) CAN ID filter 1. This filter matches
128  * ``test_ext_frame_1``.
129  */
130 extern const struct can_filter test_ext_filter_1;
131 
132 /**
133  * @brief Extended (29-bit) CAN ID filter 2. This filter matches
134  * ``test_ext_frame_2``.
135  */
136 extern const struct can_filter test_ext_filter_2;
137 
138 /**
139  * @brief Extended (29-bit) CAN ID masked filter 1. This filter matches
140  * ``test_ext_frame_1``.
141  */
142 extern const struct can_filter test_ext_masked_filter_1;
143 
144 /**
145  * @brief Extended (29-bit) CAN ID masked filter 2. This filter matches
146  * ``test_ext_frame_2``.
147  */
148 extern const struct can_filter test_ext_masked_filter_2;
149 
150 /**
151  * @brief Standard (11-bit) CAN ID filter. This filter matches
152  * ``TEST_CAN_SOME_STD_ID``.
153  */
154 extern const struct can_filter test_std_some_filter;
155 
156 /**
157  * @brief Assert that two CAN frames are equal given a CAN ID mask.
158  *
159  * @param frame1  First CAN frame.
160  * @param frame2  Second CAN frame.
161  * @param id_mask CAN ID mask.
162  */
163 void assert_frame_equal(const struct can_frame *frame1,
164 			const struct can_frame *frame2,
165 			uint32_t id_mask);
166 
167 /**
168  * @brief Common setup function for the CAN controller device under test.
169  *
170  * @param initial_mode Initial CAN controller operational mode.
171  */
172 void can_common_test_setup(can_mode_t initial_mode);
173