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 struct k_sem rx_callback_sem;
56 extern struct k_sem tx_callback_sem;
57 extern struct k_msgq can_msgq;
58 
59 /**
60  * @brief Standard (11-bit) CAN ID frame 1.
61  */
62 extern const struct can_frame test_std_frame_1;
63 
64 /**
65  * @brief Standard (11-bit) CAN ID frame 2.
66  */
67 extern const struct can_frame test_std_frame_2;
68 
69 /**
70  * @brief Extended (29-bit) CAN ID frame 1.
71  */
72 extern const struct can_frame test_ext_frame_1;
73 
74 /**
75  * @brief Extended (29-bit) CAN ID frame 1.
76  */
77 extern const struct can_frame test_ext_frame_2;
78 
79 /**
80  * @brief Standard (11-bit) CAN ID RTR frame 1.
81  */
82 extern const struct can_frame test_std_rtr_frame_1;
83 
84 /**
85  * @brief Extended (29-bit) CAN ID RTR frame 1.
86  */
87 extern const struct can_frame test_ext_rtr_frame_1;
88 
89 /**
90  * @brief Standard (11-bit) CAN ID frame 1 with CAN-FD payload.
91  */
92 extern const struct can_frame test_std_fdf_frame_1;
93 
94 /**
95  * @brief Standard (11-bit) CAN ID frame 2 with CAN-FD payload.
96  */
97 extern const struct can_frame test_std_fdf_frame_2;
98 
99 /**
100  * @brief Standard (11-bit) CAN ID filter 1. This filter matches
101  * ``test_std_frame_1``.
102  */
103 extern const struct can_filter test_std_filter_1;
104 
105 /**
106  * @brief Standard (11-bit) CAN ID filter 2. This filter matches
107  * ``test_std_frame_2``.
108  */
109 extern const struct can_filter test_std_filter_2;
110 
111 /**
112  * @brief Standard (11-bit) CAN ID masked filter 1. This filter matches
113  * ``test_std_frame_1``.
114  */
115 extern const struct can_filter test_std_masked_filter_1;
116 
117 /**
118  * @brief Standard (11-bit) CAN ID masked filter 2. This filter matches
119  * ``test_std_frame_2``.
120  */
121 extern const struct can_filter test_std_masked_filter_2;
122 
123 /**
124  * @brief Extended (29-bit) CAN ID filter 1. This filter matches
125  * ``test_ext_frame_1``.
126  */
127 extern const struct can_filter test_ext_filter_1;
128 
129 /**
130  * @brief Extended (29-bit) CAN ID filter 2. This filter matches
131  * ``test_ext_frame_2``.
132  */
133 extern const struct can_filter test_ext_filter_2;
134 
135 /**
136  * @brief Extended (29-bit) CAN ID masked filter 1. This filter matches
137  * ``test_ext_frame_1``.
138  */
139 extern const struct can_filter test_ext_masked_filter_1;
140 
141 /**
142  * @brief Extended (29-bit) CAN ID masked filter 2. This filter matches
143  * ``test_ext_frame_2``.
144  */
145 extern const struct can_filter test_ext_masked_filter_2;
146 
147 /**
148  * @brief Standard (11-bit) CAN ID RTR filter 1. This filter matches
149  * ``test_std_rtr_frame_1``.
150  */
151 extern const struct can_filter test_std_rtr_filter_1;
152 
153 /**
154  * @brief Extended (29-bit) CAN ID RTR filter 1. This filter matches
155  * ``test_ext_rtr_frame_1``.
156  */
157 extern const struct can_filter test_ext_rtr_filter_1;
158 
159 /**
160  * @brief Standard (11-bit) CAN ID filter. This filter matches
161  * ``TEST_CAN_SOME_STD_ID``.
162  */
163 extern const struct can_filter test_std_some_filter;
164 
165 /**
166  * @brief Standard (11-bit) CAN-FD ID filter 1. This filter matches
167  * ``test_std_fdf_frame_1``.
168  */
169 extern const struct can_filter test_std_fdf_filter_1;
170 
171 /**
172  * @brief Standard (11-bit) CAN-FD ID filter 2. This filter matches
173  * ``test_std_fdf_frame_2``.
174  */
175 extern const struct can_filter test_std_fdf_filter_2;
176 
177 /**
178  * @brief Assert that two CAN frames are equal given a CAN ID mask.
179  *
180  * @param frame1  First CAN frame.
181  * @param frame2  Second CAN frame.
182  * @param id_mask CAN ID mask.
183  */
184 void assert_frame_equal(const struct can_frame *frame1,
185 			const struct can_frame *frame2,
186 			uint32_t id_mask);
187