drivers: stepper: Add fake stepper driver
Add `zephyr,fake-stepper` compatible which can be used inside of unit tests. Signed-off-by: Fabian Blatz <fabianblatz@gmail.com>
This commit is contained in:
parent
c694858d06
commit
c525dc0813
6 changed files with 217 additions and 0 deletions
|
|
@ -5,6 +5,7 @@ zephyr_syscall_header(${ZEPHYR_BASE}/include/zephyr/drivers/stepper.h)
|
|||
|
||||
zephyr_library()
|
||||
|
||||
zephyr_library_sources_ifdef(CONFIG_FAKE_STEPPER fake_stepper_controller.c)
|
||||
zephyr_library_sources_ifdef(CONFIG_GPIO_STEPPER gpio_stepper_controller.c)
|
||||
|
||||
zephyr_library_sources_ifdef(CONFIG_STEPPER_SHELL stepper_shell.c)
|
||||
|
|
|
|||
|
|
@ -49,6 +49,7 @@ config STEPPER_SHELL_THREAD_PRIORITY
|
|||
|
||||
comment "Stepper Drivers"
|
||||
|
||||
rsource "Kconfig.fake"
|
||||
rsource "Kconfig.gpio"
|
||||
|
||||
endif
|
||||
|
|
|
|||
11
drivers/stepper/Kconfig.fake
Normal file
11
drivers/stepper/Kconfig.fake
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
# Fake stepper configuration options
|
||||
|
||||
# Copyright (c) 2024 Fabian Blatz <fabianblatz@gmail.com>
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
config FAKE_STEPPER
|
||||
bool "Fake stepper driver"
|
||||
default y
|
||||
depends on DT_HAS_ZEPHYR_FAKE_STEPPER_ENABLED
|
||||
help
|
||||
Enable support for the FFF-based fake stepper driver.
|
||||
147
drivers/stepper/fake_stepper_controller.c
Normal file
147
drivers/stepper/fake_stepper_controller.c
Normal file
|
|
@ -0,0 +1,147 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: Copyright (c) 2024 Fabian Blatz <fabianblatz@gmail.com>
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/drivers/stepper.h>
|
||||
#include <zephyr/drivers/stepper/stepper_fake.h>
|
||||
#include <zephyr/fff.h>
|
||||
|
||||
#ifdef CONFIG_ZTEST
|
||||
#include <zephyr/ztest.h>
|
||||
#endif /* CONFIG_ZTEST */
|
||||
|
||||
#define DT_DRV_COMPAT zephyr_fake_stepper
|
||||
|
||||
struct fake_stepper_data {
|
||||
enum micro_step_resolution micro_step_res;
|
||||
int32_t actual_position;
|
||||
};
|
||||
|
||||
DEFINE_FAKE_VALUE_FUNC(int, fake_stepper_enable, const struct device *, const bool);
|
||||
|
||||
DEFINE_FAKE_VALUE_FUNC(int, fake_stepper_is_moving, const struct device *, bool *);
|
||||
|
||||
DEFINE_FAKE_VALUE_FUNC(int, fake_stepper_move, const struct device *, const int32_t,
|
||||
struct k_poll_signal *);
|
||||
|
||||
DEFINE_FAKE_VALUE_FUNC(int, fake_stepper_set_max_velocity, const struct device *, const uint32_t);
|
||||
|
||||
DEFINE_FAKE_VALUE_FUNC(int, fake_stepper_set_micro_step_res, const struct device *,
|
||||
const enum micro_step_resolution);
|
||||
|
||||
DEFINE_FAKE_VALUE_FUNC(int, fake_stepper_get_micro_step_res, const struct device *,
|
||||
enum micro_step_resolution *);
|
||||
|
||||
DEFINE_FAKE_VALUE_FUNC(int, fake_stepper_set_actual_position, const struct device *, const int32_t);
|
||||
|
||||
DEFINE_FAKE_VALUE_FUNC(int, fake_stepper_get_actual_position, const struct device *, int32_t *);
|
||||
|
||||
DEFINE_FAKE_VALUE_FUNC(int, fake_stepper_set_target_position, const struct device *, const int32_t,
|
||||
struct k_poll_signal *);
|
||||
|
||||
DEFINE_FAKE_VALUE_FUNC(int, fake_stepper_enable_constant_velocity_mode, const struct device *,
|
||||
const enum stepper_direction, const uint32_t);
|
||||
|
||||
static int fake_stepper_set_micro_step_res_delegate(const struct device *dev,
|
||||
const enum micro_step_resolution res)
|
||||
{
|
||||
struct fake_stepper_data *data = dev->data;
|
||||
|
||||
data->micro_step_res = res;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int fake_stepper_get_micro_step_res_delegate(const struct device *dev,
|
||||
enum micro_step_resolution *res)
|
||||
{
|
||||
struct fake_stepper_data *data = dev->data;
|
||||
|
||||
*res = data->micro_step_res;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int fake_stepper_set_actual_position_delegate(const struct device *dev, const int32_t pos)
|
||||
{
|
||||
struct fake_stepper_data *data = dev->data;
|
||||
|
||||
data->actual_position = pos;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int fake_stepper_get_actual_position_delegate(const struct device *dev, int32_t *pos)
|
||||
{
|
||||
struct fake_stepper_data *data = dev->data;
|
||||
|
||||
*pos = data->actual_position;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef CONFIG_ZTEST
|
||||
static void fake_stepper_reset_rule_before(const struct ztest_unit_test *test, void *fixture)
|
||||
{
|
||||
ARG_UNUSED(test);
|
||||
ARG_UNUSED(fixture);
|
||||
|
||||
RESET_FAKE(fake_stepper_enable);
|
||||
RESET_FAKE(fake_stepper_move);
|
||||
RESET_FAKE(fake_stepper_is_moving);
|
||||
RESET_FAKE(fake_stepper_set_max_velocity);
|
||||
RESET_FAKE(fake_stepper_set_micro_step_res);
|
||||
RESET_FAKE(fake_stepper_get_micro_step_res);
|
||||
RESET_FAKE(fake_stepper_set_actual_position);
|
||||
RESET_FAKE(fake_stepper_get_actual_position);
|
||||
RESET_FAKE(fake_stepper_set_target_position);
|
||||
RESET_FAKE(fake_stepper_enable_constant_velocity_mode);
|
||||
|
||||
/* Install custom fakes for the setter and getter functions */
|
||||
fake_stepper_set_micro_step_res_fake.custom_fake = fake_stepper_set_micro_step_res_delegate;
|
||||
fake_stepper_get_micro_step_res_fake.custom_fake = fake_stepper_get_micro_step_res_delegate;
|
||||
fake_stepper_set_actual_position_fake.custom_fake =
|
||||
fake_stepper_set_actual_position_delegate;
|
||||
fake_stepper_get_actual_position_fake.custom_fake =
|
||||
fake_stepper_get_actual_position_delegate;
|
||||
}
|
||||
|
||||
ZTEST_RULE(fake_stepper_reset_rule, fake_stepper_reset_rule_before, NULL);
|
||||
#endif /* CONFIG_ZTEST */
|
||||
|
||||
static int fake_stepper_init(const struct device *dev)
|
||||
{
|
||||
fake_stepper_set_micro_step_res_fake.custom_fake = fake_stepper_set_micro_step_res_delegate;
|
||||
fake_stepper_get_micro_step_res_fake.custom_fake = fake_stepper_get_micro_step_res_delegate;
|
||||
fake_stepper_set_actual_position_fake.custom_fake =
|
||||
fake_stepper_set_actual_position_delegate;
|
||||
fake_stepper_get_actual_position_fake.custom_fake =
|
||||
fake_stepper_get_actual_position_delegate;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const struct stepper_driver_api fake_stepper_driver_api = {
|
||||
.enable = fake_stepper_enable,
|
||||
.move = fake_stepper_move,
|
||||
.is_moving = fake_stepper_is_moving,
|
||||
.set_max_velocity = fake_stepper_set_max_velocity,
|
||||
.set_micro_step_res = fake_stepper_set_micro_step_res,
|
||||
.get_micro_step_res = fake_stepper_get_micro_step_res,
|
||||
.set_actual_position = fake_stepper_set_actual_position,
|
||||
.get_actual_position = fake_stepper_get_actual_position,
|
||||
.set_target_position = fake_stepper_set_target_position,
|
||||
.enable_constant_velocity_mode = fake_stepper_enable_constant_velocity_mode,
|
||||
};
|
||||
|
||||
#define FAKE_STEPPER_INIT(inst) \
|
||||
\
|
||||
static struct fake_stepper_data fake_stepper_data_##inst; \
|
||||
\
|
||||
DEVICE_DT_INST_DEFINE(inst, fake_stepper_init, NULL, &fake_stepper_data_##inst, NULL, \
|
||||
POST_KERNEL, CONFIG_STEPPER_INIT_PRIORITY, \
|
||||
&fake_stepper_driver_api);
|
||||
|
||||
DT_INST_FOREACH_STATUS_OKAY(FAKE_STEPPER_INIT)
|
||||
10
dts/bindings/stepper/zephyr,fake-stepper.yaml
Normal file
10
dts/bindings/stepper/zephyr,fake-stepper.yaml
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
# Copyright (c) 2024 Fabian Blatz <fabianblatz@gmail.com>
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
description: |
|
||||
This binding provides a fake stepper controller for use as either a stub or a mock in
|
||||
Zephyr testing.
|
||||
|
||||
compatible: "zephyr,fake-stepper"
|
||||
|
||||
include: stepper-controller.yaml
|
||||
47
include/zephyr/drivers/stepper/stepper_fake.h
Normal file
47
include/zephyr/drivers/stepper/stepper_fake.h
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
/*
|
||||
* Copyright (c) 2024 Fabian Blatz <fabianblatz@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifndef ZEPHYR_INCLUDE_DRIVERS_STEPPER_STEPPER_FAKE_H_
|
||||
#define ZEPHYR_INCLUDE_DRIVERS_STEPPER_STEPPER_FAKE_H_
|
||||
|
||||
#include <zephyr/drivers/stepper.h>
|
||||
#include <zephyr/fff.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
DECLARE_FAKE_VALUE_FUNC(int, fake_stepper_enable, const struct device *, const bool);
|
||||
|
||||
DECLARE_FAKE_VALUE_FUNC(int, fake_stepper_move, const struct device *, const int32_t,
|
||||
struct k_poll_signal *);
|
||||
|
||||
DECLARE_FAKE_VALUE_FUNC(int, fake_stepper_set_max_velocity, const struct device *, const uint32_t);
|
||||
|
||||
DECLARE_FAKE_VALUE_FUNC(int, fake_stepper_set_micro_step_res, const struct device *,
|
||||
const enum micro_step_resolution);
|
||||
|
||||
DECLARE_FAKE_VALUE_FUNC(int, fake_stepper_get_micro_step_res, const struct device *,
|
||||
enum micro_step_resolution *);
|
||||
|
||||
DECLARE_FAKE_VALUE_FUNC(int, fake_stepper_set_actual_position, const struct device *,
|
||||
const int32_t);
|
||||
|
||||
DECLARE_FAKE_VALUE_FUNC(int, fake_stepper_get_actual_position, const struct device *, int32_t *);
|
||||
|
||||
DECLARE_FAKE_VALUE_FUNC(int, fake_stepper_set_target_position, const struct device *, const int32_t,
|
||||
struct k_poll_signal *);
|
||||
|
||||
DECLARE_FAKE_VALUE_FUNC(int, fake_stepper_is_moving, const struct device *, bool *);
|
||||
|
||||
DECLARE_FAKE_VALUE_FUNC(int, fake_stepper_enable_constant_velocity_mode, const struct device *,
|
||||
const enum stepper_direction, const uint32_t);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* ZEPHYR_INCLUDE_DRIVERS_STEPPER_STEPPER_FAKE_H_ */
|
||||
Loading…
Reference in a new issue