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/kernel.h>
10 #include <zephyr/ztest.h>
11
12 #include "common.h"
13
14 /**
15 * @brief Global variables.
16 */
17 ZTEST_DMEM const struct device *const can_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_canbus));
18 ZTEST_DMEM const struct device *const can_phy =
19 DEVICE_DT_GET_OR_NULL(DT_PHANDLE(DT_CHOSEN(zephyr_canbus), phys));
20 struct k_sem rx_callback_sem;
21 struct k_sem tx_callback_sem;
22
23 CAN_MSGQ_DEFINE(can_msgq, 5);
24
25 /**
26 * @brief Standard (11-bit) CAN ID frame 1.
27 */
28 const struct can_frame test_std_frame_1 = {
29 .flags = 0,
30 .id = TEST_CAN_STD_ID_1,
31 .dlc = 8,
32 .data = {1, 2, 3, 4, 5, 6, 7, 8}
33 };
34
35 /**
36 * @brief Standard (11-bit) CAN ID frame 2.
37 */
38 const struct can_frame test_std_frame_2 = {
39 .flags = 0,
40 .id = TEST_CAN_STD_ID_2,
41 .dlc = 8,
42 .data = {1, 2, 3, 4, 5, 6, 7, 8}
43 };
44
45 /**
46 * @brief Extended (29-bit) CAN ID frame 1.
47 */
48 const struct can_frame test_ext_frame_1 = {
49 .flags = CAN_FRAME_IDE,
50 .id = TEST_CAN_EXT_ID_1,
51 .dlc = 8,
52 .data = {1, 2, 3, 4, 5, 6, 7, 8}
53 };
54
55 /**
56 * @brief Extended (29-bit) CAN ID frame 1.
57 */
58 const struct can_frame test_ext_frame_2 = {
59 .flags = CAN_FRAME_IDE,
60 .id = TEST_CAN_EXT_ID_2,
61 .dlc = 8,
62 .data = {1, 2, 3, 4, 5, 6, 7, 8}
63 };
64
65 /**
66 * @brief Standard (11-bit) CAN ID RTR frame 1.
67 */
68 const struct can_frame test_std_rtr_frame_1 = {
69 .flags = CAN_FRAME_RTR,
70 .id = TEST_CAN_STD_ID_1,
71 .dlc = 0,
72 .data = {0}
73 };
74
75 /**
76 * @brief Extended (29-bit) CAN ID RTR frame 1.
77 */
78 const struct can_frame test_ext_rtr_frame_1 = {
79 .flags = CAN_FRAME_IDE | CAN_FRAME_RTR,
80 .id = TEST_CAN_EXT_ID_1,
81 .dlc = 0,
82 .data = {0}
83 };
84
85 #ifdef CONFIG_CAN_FD_MODE
86 /**
87 * @brief Standard (11-bit) CAN ID frame 1 with CAN FD payload.
88 */
89 const struct can_frame test_std_fdf_frame_1 = {
90 .flags = CAN_FRAME_FDF | CAN_FRAME_BRS,
91 .id = TEST_CAN_STD_ID_1,
92 .dlc = 0xf,
93 .data = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
94 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
95 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45,
96 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
97 61, 62, 63, 64 }
98 };
99
100 /**
101 * @brief Standard (11-bit) CAN ID frame 1 with CAN FD payload.
102 */
103 const struct can_frame test_std_fdf_frame_2 = {
104 .flags = CAN_FRAME_FDF | CAN_FRAME_BRS,
105 .id = TEST_CAN_STD_ID_2,
106 .dlc = 0xf,
107 .data = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
108 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
109 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45,
110 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
111 61, 62, 63, 64 }
112 };
113 #endif /* CONFIG_CAN_FD_MODE */
114
115 /**
116 * @brief Standard (11-bit) CAN ID filter 1. This filter matches
117 * ``test_std_frame_1``.
118 */
119 const struct can_filter test_std_filter_1 = {
120 .flags = 0U,
121 .id = TEST_CAN_STD_ID_1,
122 .mask = CAN_STD_ID_MASK
123 };
124
125 /**
126 * @brief Standard (11-bit) CAN ID filter 2. This filter matches
127 * ``test_std_frame_2``.
128 */
129 const struct can_filter test_std_filter_2 = {
130 .flags = 0U,
131 .id = TEST_CAN_STD_ID_2,
132 .mask = CAN_STD_ID_MASK
133 };
134
135 /**
136 * @brief Standard (11-bit) CAN ID masked filter 1. This filter matches
137 * ``test_std_frame_1``.
138 */
139 const struct can_filter test_std_masked_filter_1 = {
140 .flags = 0U,
141 .id = TEST_CAN_STD_MASK_ID_1,
142 .mask = TEST_CAN_STD_MASK
143 };
144
145 /**
146 * @brief Standard (11-bit) CAN ID masked filter 2. This filter matches
147 * ``test_std_frame_2``.
148 */
149 const struct can_filter test_std_masked_filter_2 = {
150 .flags = 0U,
151 .id = TEST_CAN_STD_MASK_ID_2,
152 .mask = TEST_CAN_STD_MASK
153 };
154
155 /**
156 * @brief Extended (29-bit) CAN ID filter 1. This filter matches
157 * ``test_ext_frame_1``.
158 */
159 const struct can_filter test_ext_filter_1 = {
160 .flags = CAN_FILTER_IDE,
161 .id = TEST_CAN_EXT_ID_1,
162 .mask = CAN_EXT_ID_MASK
163 };
164
165 /**
166 * @brief Extended (29-bit) CAN ID filter 2. This filter matches
167 * ``test_ext_frame_2``.
168 */
169 const struct can_filter test_ext_filter_2 = {
170 .flags = CAN_FILTER_IDE,
171 .id = TEST_CAN_EXT_ID_2,
172 .mask = CAN_EXT_ID_MASK
173 };
174
175 /**
176 * @brief Extended (29-bit) CAN ID masked filter 1. This filter matches
177 * ``test_ext_frame_1``.
178 */
179 const struct can_filter test_ext_masked_filter_1 = {
180 .flags = CAN_FILTER_IDE,
181 .id = TEST_CAN_EXT_MASK_ID_1,
182 .mask = TEST_CAN_EXT_MASK
183 };
184
185 /**
186 * @brief Extended (29-bit) CAN ID masked filter 2. This filter matches
187 * ``test_ext_frame_2``.
188 */
189 const struct can_filter test_ext_masked_filter_2 = {
190 .flags = CAN_FILTER_IDE,
191 .id = TEST_CAN_EXT_MASK_ID_2,
192 .mask = TEST_CAN_EXT_MASK
193 };
194
195 /**
196 * @brief Standard (11-bit) CAN ID filter. This filter matches
197 * ``TEST_CAN_SOME_STD_ID``.
198 */
199 const struct can_filter test_std_some_filter = {
200 .flags = 0U,
201 .id = TEST_CAN_SOME_STD_ID,
202 .mask = CAN_STD_ID_MASK
203 };
204
205 /**
206 * @brief Assert that two CAN frames are equal given a CAN ID mask.
207 *
208 * @param frame1 First CAN frame.
209 * @param frame2 Second CAN frame.
210 * @param id_mask CAN ID mask.
211 */
assert_frame_equal(const struct can_frame * frame1,const struct can_frame * frame2,uint32_t id_mask)212 void assert_frame_equal(const struct can_frame *frame1,
213 const struct can_frame *frame2,
214 uint32_t id_mask)
215 {
216 zassert_equal(frame1->flags, frame2->flags, "Flags do not match");
217 zassert_equal(frame1->id | id_mask, frame2->id | id_mask, "ID does not match");
218 zassert_equal(frame1->dlc, frame2->dlc, "DLC does not match");
219
220 if ((frame1->flags & CAN_FRAME_RTR) == 0U) {
221 zassert_mem_equal(frame1->data, frame2->data, can_dlc_to_bytes(frame1->dlc),
222 "Received data differ");
223 }
224 }
225
can_common_test_setup(can_mode_t initial_mode)226 void can_common_test_setup(can_mode_t initial_mode)
227 {
228 int err;
229
230 k_sem_init(&rx_callback_sem, 0, 2);
231 k_sem_init(&tx_callback_sem, 0, 2);
232
233 k_object_access_grant(&can_msgq, k_current_get());
234 k_object_access_grant(can_dev, k_current_get());
235
236 zassert_true(device_is_ready(can_dev), "CAN device not ready");
237
238 (void)can_stop(can_dev);
239
240 err = can_set_mode(can_dev, initial_mode);
241 zassert_equal(err, 0, "failed to set initial mode (err %d)", err);
242 zassert_equal(initial_mode, can_get_mode(can_dev));
243
244 err = can_start(can_dev);
245 zassert_equal(err, 0, "failed to start CAN controller (err %d)", err);
246 }
247