Skip to content

Commit 11ed7d6

Browse files
henrikbrixandersencarlescufi
authored andcommitted
tests: drivers: can: add test for the CAN shell module
Add test for the CAN shell module. The tests execute a set of CAN shell module commands against a CAN controller driver mock, verifies that the expected CAN controller driver API functions are called, and that their arguments are as expected. Signed-off-by: Henrik Brix Andersen <[email protected]>
1 parent b0c1b97 commit 11ed7d6

File tree

9 files changed

+840
-0
lines changed

9 files changed

+840
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
5+
project(can_shell)
6+
7+
FILE(GLOB app_sources src/*.c)
8+
target_sources(app PRIVATE ${app_sources})

tests/drivers/can/shell/Kconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright (c) 2022 Vestas Wind Systems A/S
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
config TEST_FAKE_CAN
5+
bool
6+
default y
7+
depends on DT_HAS_TEST_FAKE_CAN_ENABLED
8+
select CAN_HAS_RX_TIMESTAMP
9+
select CAN_HAS_CANFD
10+
help
11+
Enable support for the fake CAN driver.
12+
13+
source "Kconfig.zephyr"

tests/drivers/can/shell/app.overlay

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright (c) 2022 Vestas Wind Systems A/S
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/{
8+
fake_can: fake_can {
9+
compatible = "test-fake-can";
10+
status = "okay";
11+
sjw = <1>;
12+
sample-point = <875>;
13+
bus-speed = <125000>;
14+
sample-point = <875>;
15+
sjw-data = <1>;
16+
bus-speed-data = <1000000>;
17+
sample-point-data = <750>;
18+
};
19+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Copyright (c) 2022 Vestas Wind Systems A/S
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: |
5+
This binding provides resources required to build and run the
6+
tests/drivers/can/shell test in Zephyr.
7+
8+
compatible: "test-fake-can"
9+
10+
include: can-fd-controller.yaml

tests/drivers/can/shell/prj.conf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CONFIG_SHELL=y
2+
CONFIG_SHELL_BACKEND_SERIAL=n
3+
CONFIG_SHELL_BACKEND_DUMMY=y
4+
CONFIG_CAN=y
5+
CONFIG_CAN_AUTO_BUS_OFF_RECOVERY=n
6+
CONFIG_CAN_FD_MODE=y
7+
CONFIG_CAN_SHELL=y
8+
CONFIG_ZTEST=y
9+
CONFIG_ZTEST_NEW_API=y
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*
2+
* Copyright (c) 2022 Vestas Wind Systems A/S
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/device.h>
8+
#include <zephyr/drivers/can.h>
9+
#include <zephyr/fff.h>
10+
11+
#ifdef CONFIG_ZTEST_NEW_API
12+
#include <zephyr/ztest.h>
13+
#endif /* CONFIG_ZTEST_NEW_API */
14+
15+
#include "fake_can.h"
16+
17+
#define DT_DRV_COMPAT test_fake_can
18+
19+
DEFINE_FAKE_VALUE_FUNC(int, fake_can_set_timing, const struct device *, const struct can_timing *);
20+
21+
DEFINE_FAKE_VALUE_FUNC(int, fake_can_set_timing_data, const struct device *,
22+
const struct can_timing *);
23+
24+
DEFINE_FAKE_VALUE_FUNC(int, fake_can_get_capabilities, const struct device *, can_mode_t *);
25+
26+
DEFINE_FAKE_VALUE_FUNC(int, fake_can_set_mode, const struct device *, can_mode_t);
27+
28+
DEFINE_FAKE_VALUE_FUNC(int, fake_can_send, const struct device *, const struct can_frame *,
29+
k_timeout_t, can_tx_callback_t, void *);
30+
31+
DEFINE_FAKE_VALUE_FUNC(int, fake_can_add_rx_filter, const struct device *, can_rx_callback_t,
32+
void *, const struct can_filter *);
33+
34+
DEFINE_FAKE_VOID_FUNC(fake_can_remove_rx_filter, const struct device *, int);
35+
36+
DEFINE_FAKE_VALUE_FUNC(int, fake_can_recover, const struct device *, k_timeout_t);
37+
38+
DEFINE_FAKE_VALUE_FUNC(int, fake_can_get_state, const struct device *, enum can_state *,
39+
struct can_bus_err_cnt *);
40+
41+
DEFINE_FAKE_VOID_FUNC(fake_can_set_state_change_callback, const struct device *,
42+
can_state_change_callback_t, void *);
43+
44+
DEFINE_FAKE_VALUE_FUNC(int, fake_can_get_max_filters, const struct device *, enum can_ide);
45+
46+
#ifdef CONFIG_ZTEST_NEW_API
47+
static void fake_can_reset_rule_before(const struct ztest_unit_test *test, void *fixture)
48+
{
49+
ARG_UNUSED(test);
50+
ARG_UNUSED(fixture);
51+
52+
RESET_FAKE(fake_can_get_capabilities);
53+
RESET_FAKE(fake_can_set_mode);
54+
RESET_FAKE(fake_can_set_timing);
55+
RESET_FAKE(fake_can_set_timing_data);
56+
RESET_FAKE(fake_can_send);
57+
RESET_FAKE(fake_can_add_rx_filter);
58+
RESET_FAKE(fake_can_remove_rx_filter);
59+
RESET_FAKE(fake_can_get_state);
60+
RESET_FAKE(fake_can_recover);
61+
RESET_FAKE(fake_can_set_state_change_callback);
62+
RESET_FAKE(fake_can_get_max_filters);
63+
}
64+
65+
ZTEST_RULE(fake_can_reset_rule, fake_can_reset_rule_before, NULL);
66+
#endif /* CONFIG_ZTEST_NEW_API */
67+
68+
static int fake_can_get_core_clock(const struct device *dev, uint32_t *rate)
69+
{
70+
ARG_UNUSED(dev);
71+
72+
*rate = 16000000;
73+
74+
return 0;
75+
}
76+
77+
static int fake_can_get_max_bitrate(const struct device *dev, uint32_t *max_bitrate)
78+
{
79+
ARG_UNUSED(dev);
80+
81+
*max_bitrate = 5000000;
82+
83+
return 0;
84+
}
85+
86+
static const struct can_driver_api fake_can_driver_api = {
87+
.get_capabilities = fake_can_get_capabilities,
88+
.set_mode = fake_can_set_mode,
89+
.set_timing = fake_can_set_timing,
90+
.send = fake_can_send,
91+
.add_rx_filter = fake_can_add_rx_filter,
92+
.remove_rx_filter = fake_can_remove_rx_filter,
93+
.get_state = fake_can_get_state,
94+
#ifndef CONFIG_CAN_AUTO_BUS_OFF_RECOVERY
95+
.recover = fake_can_recover,
96+
#endif /* CONFIG_CAN_AUTO_BUS_OFF_RECOVERY */
97+
.set_state_change_callback = fake_can_set_state_change_callback,
98+
.get_core_clock = fake_can_get_core_clock,
99+
.get_max_filters = fake_can_get_max_filters,
100+
.get_max_bitrate = fake_can_get_max_bitrate,
101+
.timing_min = {
102+
.sjw = 0x01,
103+
.prop_seg = 0x01,
104+
.phase_seg1 = 0x01,
105+
.phase_seg2 = 0x01,
106+
.prescaler = 0x01
107+
},
108+
.timing_max = {
109+
.sjw = 0x0f,
110+
.prop_seg = 0x0f,
111+
.phase_seg1 = 0x0f,
112+
.phase_seg2 = 0x0f,
113+
.prescaler = 0xffff
114+
},
115+
#ifdef CONFIG_CAN_FD_MODE
116+
.set_timing_data = fake_can_set_timing_data,
117+
.timing_data_min = {
118+
.sjw = 0x01,
119+
.prop_seg = 0x01,
120+
.phase_seg1 = 0x01,
121+
.phase_seg2 = 0x01,
122+
.prescaler = 0x01
123+
},
124+
.timing_data_max = {
125+
.sjw = 0x0f,
126+
.prop_seg = 0x0f,
127+
.phase_seg1 = 0x0f,
128+
.phase_seg2 = 0x0f,
129+
.prescaler = 0xffff
130+
},
131+
#endif /* CONFIG_CAN_FD_MODE */
132+
};
133+
134+
static int fake_can_init(const struct device *dev)
135+
{
136+
ARG_UNUSED(dev);
137+
138+
return 0;
139+
}
140+
141+
#define FAKE_CAN_INIT(inst) \
142+
DEVICE_DT_INST_DEFINE(inst, &fake_can_init, NULL, NULL, NULL, \
143+
POST_KERNEL, CONFIG_CAN_INIT_PRIORITY, \
144+
&fake_can_driver_api);
145+
146+
DT_INST_FOREACH_STATUS_OKAY(FAKE_CAN_INIT)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (c) 2022 Vestas Wind Systems A/S
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef ZEPHYR_TESTS_DRIVERS_CAN_SHELL_FAKE_CAN_H_
8+
#define ZEPHYR_TESTS_DRIVERS_CAN_SHELL_FAKE_CAN_H_
9+
10+
#include <zephyr/drivers/can.h>
11+
#include <zephyr/fff.h>
12+
13+
#ifdef __cplusplus
14+
extern "C" {
15+
#endif
16+
17+
DECLARE_FAKE_VALUE_FUNC(int, fake_can_set_timing, const struct device *, const struct can_timing *);
18+
19+
DECLARE_FAKE_VALUE_FUNC(int, fake_can_set_timing_data, const struct device *,
20+
const struct can_timing *);
21+
22+
DECLARE_FAKE_VALUE_FUNC(int, fake_can_get_capabilities, const struct device *, can_mode_t *);
23+
24+
DECLARE_FAKE_VALUE_FUNC(int, fake_can_set_mode, const struct device *, can_mode_t);
25+
26+
DECLARE_FAKE_VALUE_FUNC(int, fake_can_send, const struct device *, const struct can_frame *,
27+
k_timeout_t, can_tx_callback_t, void *);
28+
29+
DECLARE_FAKE_VALUE_FUNC(int, fake_can_add_rx_filter, const struct device *, can_rx_callback_t,
30+
void *, const struct can_filter *);
31+
32+
DECLARE_FAKE_VOID_FUNC(fake_can_remove_rx_filter, const struct device *, int);
33+
34+
DECLARE_FAKE_VALUE_FUNC(int, fake_can_recover, const struct device *, k_timeout_t);
35+
36+
DECLARE_FAKE_VALUE_FUNC(int, fake_can_get_state, const struct device *, enum can_state *,
37+
struct can_bus_err_cnt *);
38+
39+
DECLARE_FAKE_VOID_FUNC(fake_can_set_state_change_callback, const struct device *,
40+
can_state_change_callback_t, void *);
41+
42+
DECLARE_FAKE_VALUE_FUNC(int, fake_can_get_max_filters, const struct device *, enum can_ide);
43+
44+
#ifdef __cplusplus
45+
}
46+
#endif
47+
48+
#endif /* ZEPHYR_TESTS_DRIVERS_CAN_SHELL_FAKE_CAN_H_ */

0 commit comments

Comments
 (0)