|
| 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) |
0 commit comments