drivers: can: make the fake CAN driver generally available
Make the fake CAN controller driver available for use in tests outside of the CAN shell test. Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
This commit is contained in:
parent
662aed81d2
commit
f30a5969d0
10 changed files with 29 additions and 29 deletions
|
@ -4,6 +4,7 @@ zephyr_library()
|
|||
zephyr_sources_ifdef(CONFIG_CAN_MCUX_MCAN can_mcux_mcan.c)
|
||||
|
||||
zephyr_library_sources_ifdef(CONFIG_CAN can_common.c)
|
||||
zephyr_library_sources_ifdef(CONFIG_CAN_FAKE can_fake.c)
|
||||
zephyr_library_sources_ifdef(CONFIG_CAN_LOOPBACK can_loopback.c)
|
||||
zephyr_library_sources_ifdef(CONFIG_CAN_MCAN can_mcan.c)
|
||||
zephyr_library_sources_ifdef(CONFIG_CAN_MCP2515 can_mcp2515.c)
|
||||
|
|
|
@ -111,6 +111,7 @@ source "drivers/can/Kconfig.native_posix_linux"
|
|||
source "drivers/can/Kconfig.sja1000"
|
||||
source "drivers/can/Kconfig.esp32"
|
||||
source "drivers/can/Kconfig.kvaser"
|
||||
source "drivers/can/Kconfig.fake"
|
||||
|
||||
source "drivers/can/transceiver/Kconfig"
|
||||
|
||||
|
|
13
drivers/can/Kconfig.fake
Normal file
13
drivers/can/Kconfig.fake
Normal file
|
@ -0,0 +1,13 @@
|
|||
# Fake CAN configuration options
|
||||
|
||||
# Copyright (c) 2022 Vestas Wind Systems A/S
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
config CAN_FAKE
|
||||
bool "Fake CAN driver"
|
||||
default y
|
||||
depends on DT_HAS_ZEPHYR_FAKE_CAN_ENABLED
|
||||
select CAN_HAS_CANFD
|
||||
select CAN_HAS_RX_TIMESTAMP
|
||||
help
|
||||
Enable support for the FFF-based fake CAN driver.
|
153
drivers/can/can_fake.c
Normal file
153
drivers/can/can_fake.c
Normal file
|
@ -0,0 +1,153 @@
|
|||
/*
|
||||
* Copyright (c) 2022 Vestas Wind Systems A/S
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/drivers/can.h>
|
||||
#include <zephyr/drivers/can/can_fake.h>
|
||||
#include <zephyr/fff.h>
|
||||
|
||||
#ifdef CONFIG_ZTEST_NEW_API
|
||||
#include <zephyr/ztest.h>
|
||||
#endif /* CONFIG_ZTEST_NEW_API */
|
||||
|
||||
#define DT_DRV_COMPAT zephyr_fake_can
|
||||
|
||||
DEFINE_FAKE_VALUE_FUNC(int, fake_can_start, const struct device *);
|
||||
|
||||
DEFINE_FAKE_VALUE_FUNC(int, fake_can_stop, const struct device *);
|
||||
|
||||
DEFINE_FAKE_VALUE_FUNC(int, fake_can_set_timing, const struct device *, const struct can_timing *);
|
||||
|
||||
DEFINE_FAKE_VALUE_FUNC(int, fake_can_set_timing_data, const struct device *,
|
||||
const struct can_timing *);
|
||||
|
||||
DEFINE_FAKE_VALUE_FUNC(int, fake_can_get_capabilities, const struct device *, can_mode_t *);
|
||||
|
||||
DEFINE_FAKE_VALUE_FUNC(int, fake_can_set_mode, const struct device *, can_mode_t);
|
||||
|
||||
DEFINE_FAKE_VALUE_FUNC(int, fake_can_send, const struct device *, const struct can_frame *,
|
||||
k_timeout_t, can_tx_callback_t, void *);
|
||||
|
||||
DEFINE_FAKE_VALUE_FUNC(int, fake_can_add_rx_filter, const struct device *, can_rx_callback_t,
|
||||
void *, const struct can_filter *);
|
||||
|
||||
DEFINE_FAKE_VOID_FUNC(fake_can_remove_rx_filter, const struct device *, int);
|
||||
|
||||
DEFINE_FAKE_VALUE_FUNC(int, fake_can_recover, const struct device *, k_timeout_t);
|
||||
|
||||
DEFINE_FAKE_VALUE_FUNC(int, fake_can_get_state, const struct device *, enum can_state *,
|
||||
struct can_bus_err_cnt *);
|
||||
|
||||
DEFINE_FAKE_VOID_FUNC(fake_can_set_state_change_callback, const struct device *,
|
||||
can_state_change_callback_t, void *);
|
||||
|
||||
DEFINE_FAKE_VALUE_FUNC(int, fake_can_get_max_filters, const struct device *, bool);
|
||||
|
||||
#ifdef CONFIG_ZTEST_NEW_API
|
||||
static void fake_can_reset_rule_before(const struct ztest_unit_test *test, void *fixture)
|
||||
{
|
||||
ARG_UNUSED(test);
|
||||
ARG_UNUSED(fixture);
|
||||
|
||||
RESET_FAKE(fake_can_start);
|
||||
RESET_FAKE(fake_can_stop);
|
||||
RESET_FAKE(fake_can_get_capabilities);
|
||||
RESET_FAKE(fake_can_set_mode);
|
||||
RESET_FAKE(fake_can_set_timing);
|
||||
RESET_FAKE(fake_can_set_timing_data);
|
||||
RESET_FAKE(fake_can_send);
|
||||
RESET_FAKE(fake_can_add_rx_filter);
|
||||
RESET_FAKE(fake_can_remove_rx_filter);
|
||||
RESET_FAKE(fake_can_get_state);
|
||||
RESET_FAKE(fake_can_recover);
|
||||
RESET_FAKE(fake_can_set_state_change_callback);
|
||||
RESET_FAKE(fake_can_get_max_filters);
|
||||
}
|
||||
|
||||
ZTEST_RULE(fake_can_reset_rule, fake_can_reset_rule_before, NULL);
|
||||
#endif /* CONFIG_ZTEST_NEW_API */
|
||||
|
||||
static int fake_can_get_core_clock(const struct device *dev, uint32_t *rate)
|
||||
{
|
||||
ARG_UNUSED(dev);
|
||||
|
||||
*rate = 16000000;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int fake_can_get_max_bitrate(const struct device *dev, uint32_t *max_bitrate)
|
||||
{
|
||||
ARG_UNUSED(dev);
|
||||
|
||||
*max_bitrate = 5000000;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const struct can_driver_api fake_can_driver_api = {
|
||||
.start = fake_can_start,
|
||||
.stop = fake_can_stop,
|
||||
.get_capabilities = fake_can_get_capabilities,
|
||||
.set_mode = fake_can_set_mode,
|
||||
.set_timing = fake_can_set_timing,
|
||||
.send = fake_can_send,
|
||||
.add_rx_filter = fake_can_add_rx_filter,
|
||||
.remove_rx_filter = fake_can_remove_rx_filter,
|
||||
.get_state = fake_can_get_state,
|
||||
#ifndef CONFIG_CAN_AUTO_BUS_OFF_RECOVERY
|
||||
.recover = fake_can_recover,
|
||||
#endif /* CONFIG_CAN_AUTO_BUS_OFF_RECOVERY */
|
||||
.set_state_change_callback = fake_can_set_state_change_callback,
|
||||
.get_core_clock = fake_can_get_core_clock,
|
||||
.get_max_filters = fake_can_get_max_filters,
|
||||
.get_max_bitrate = fake_can_get_max_bitrate,
|
||||
.timing_min = {
|
||||
.sjw = 0x01,
|
||||
.prop_seg = 0x01,
|
||||
.phase_seg1 = 0x01,
|
||||
.phase_seg2 = 0x01,
|
||||
.prescaler = 0x01
|
||||
},
|
||||
.timing_max = {
|
||||
.sjw = 0x0f,
|
||||
.prop_seg = 0x0f,
|
||||
.phase_seg1 = 0x0f,
|
||||
.phase_seg2 = 0x0f,
|
||||
.prescaler = 0xffff
|
||||
},
|
||||
#ifdef CONFIG_CAN_FD_MODE
|
||||
.set_timing_data = fake_can_set_timing_data,
|
||||
.timing_data_min = {
|
||||
.sjw = 0x01,
|
||||
.prop_seg = 0x01,
|
||||
.phase_seg1 = 0x01,
|
||||
.phase_seg2 = 0x01,
|
||||
.prescaler = 0x01
|
||||
},
|
||||
.timing_data_max = {
|
||||
.sjw = 0x0f,
|
||||
.prop_seg = 0x0f,
|
||||
.phase_seg1 = 0x0f,
|
||||
.phase_seg2 = 0x0f,
|
||||
.prescaler = 0xffff
|
||||
},
|
||||
#endif /* CONFIG_CAN_FD_MODE */
|
||||
};
|
||||
|
||||
static int fake_can_init(const struct device *dev)
|
||||
{
|
||||
ARG_UNUSED(dev);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define FAKE_CAN_INIT(inst) \
|
||||
DEVICE_DT_INST_DEFINE(inst, &fake_can_init, NULL, NULL, NULL, \
|
||||
POST_KERNEL, CONFIG_CAN_INIT_PRIORITY, \
|
||||
&fake_can_driver_api);
|
||||
|
||||
DT_INST_FOREACH_STATUS_OKAY(FAKE_CAN_INIT)
|
Loading…
Add table
Add a link
Reference in a new issue