Skip to content

drivers: stepper: introduce stepper_drv api to facilitate implementing motion controllers as device drivers #91979

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions drivers/stepper/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@ zephyr_syscall_header(${ZEPHYR_BASE}/include/zephyr/drivers/stepper.h)
add_subdirectory_ifdef(CONFIG_STEPPER_ADI_TMC adi_tmc)
add_subdirectory_ifdef(CONFIG_STEPPER_ALLEGRO allegro)
add_subdirectory_ifdef(CONFIG_STEPPER_TI ti)
# zephyr-keep-sorted-stop

# zephyr-keep-sorted-start
add_subdirectory_ifdef(CONFIG_STEPPER_TIMING_SOURCES stepper_timing_sources)
add_subdirectory_ifdef(CONFIG_STEP_DIR_STEPPER step_dir)
# zephyr-keep-sorted-stop

zephyr_library()
zephyr_library_property(ALLOW_EMPTY TRUE)

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_FAKE_STEPPER fake_stepper_drv.c)
zephyr_library_sources_ifdef(CONFIG_FAKE_STEPPER_CONTROLLER fake_stepper_controller.c)
zephyr_library_sources_ifdef(CONFIG_H_BRIDGE_STEPPER h_bridge_stepper_driver.c)
zephyr_library_sources_ifdef(CONFIG_STEPPER_SHELL stepper_shell.c)
zephyr_library_sources_ifdef(CONFIG_ZEPHYR_STEPPER_MOTION_CONTROL zephyr_stepper_motion_controller.c)
7 changes: 6 additions & 1 deletion drivers/stepper/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,17 @@ config STEPPER_SHELL
comment "Stepper Driver Common"

rsource "step_dir/Kconfig"
rsource "stepper_timing_sources/Kconfig"

comment "Stepper Drivers"

# zephyr-keep-sorted-start
rsource "Kconfig.fake"
rsource "Kconfig.gpio"
rsource "Kconfig.h_bridge"
rsource "Kconfig.zephyr_stepper_motion_controller"
# zephyr-keep-sorted-stop

# zephyr-keep-sorted-start
rsource "adi_tmc/Kconfig"
rsource "allegro/Kconfig"
rsource "ti/Kconfig"
Expand Down
7 changes: 7 additions & 0 deletions drivers/stepper/Kconfig.fake
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,10 @@ config FAKE_STEPPER
depends on DT_HAS_ZEPHYR_FAKE_STEPPER_ENABLED
help
Enable support for the FFF-based fake stepper driver.

config FAKE_STEPPER_CONTROLLER
bool "Fake stepper controller driver"
default y
depends on DT_HAS_ZEPHYR_FAKE_STEPPER_CONTROLLER_ENABLED
help
Enable support for the FFF-based fake stepper controller driver.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# SPDX-FileCopyrightText: Copyright (c) 2024 Jilay Sandeep Pandya
# SPDX-License-Identifier: Apache-2.0

config GPIO_STEPPER
config H_BRIDGE_STEPPER
bool "Activate driver for gpio stepper control"
depends on DT_HAS_ZEPHYR_GPIO_STEPPER_ENABLED
depends on DT_HAS_ZEPHYR_H_BRIDGE_STEPPER_ENABLED
default y
18 changes: 0 additions & 18 deletions drivers/stepper/Kconfig.stepper_event_template

This file was deleted.

24 changes: 24 additions & 0 deletions drivers/stepper/Kconfig.zephyr_stepper_motion_controller
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# SPDX-FileCopyrightText: Copyright (c) 2025 Jilay Sandeep Pandya
# SPDX-License-Identifier: Apache-2.0

config ZEPHYR_STEPPER_MOTION_CONTROL
bool "Zephyr CPU based Stepper motion control"
depends on DT_HAS_ZEPHYR_STEPPER_MOTION_CONTROL_ENABLED
select STEPPER_TIMING_SOURCES
default y

config ZEPHYR_STEPPER_MOTION_CONTROL_GENERATE_ISR_SAFE_EVENTS
bool "Guarantee non ISR callbacks upon stepper events"
help
Enable the dispatch of stepper generated events via
a message queue to guarantee that the event handler
code is not run inside of an ISR. Can be disabled, but
then registered stepper event callback must be ISR safe.

config ZEPHYR_STEPPER_MOTION_CONTROL_EVENT_QUEUE_LEN
int "Maximum number of pending stepper events"
default 4
depends on ZEPHYR_STEPPER_MOTION_CONTROL_GENERATE_ISR_SAFE_EVENTS
help
The maximum number of stepper events that can be pending before new events
are dropped.
1 change: 0 additions & 1 deletion drivers/stepper/adi_tmc/Kconfig.tmc_rampgen_template
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

config STEPPER_ADI_$(module)_RAMPSTAT_POLL_INTERVAL_IN_MSEC
int "$(module-str) poll ramp status interval in ms"
depends on !$(dt_compat_any_has_prop,$(DT_COMPAT_ADI_$(module)),diag0-gpios)
default 100
help
When DIAG0 pin is not available, the driver automatically falls back to
Expand Down
17 changes: 4 additions & 13 deletions drivers/stepper/adi_tmc/tmc22xx.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ struct tmc22xx_config {
};

struct tmc22xx_data {
struct step_dir_stepper_common_data common;
enum stepper_micro_step_resolution resolution;
};

STEP_DIR_STEPPER_STRUCT_CHECK(struct tmc22xx_config, struct tmc22xx_data);
STEP_DIR_STEPPER_STRUCT_CHECK(struct tmc22xx_config);

static int tmc22xx_stepper_enable(const struct device *dev)
{
Expand Down Expand Up @@ -147,18 +146,11 @@ static int tmc22xx_stepper_init(const struct device *dev)
return 0;
}

static DEVICE_API(stepper, tmc22xx_stepper_api) = {
static DEVICE_API(stepper_drv, tmc22xx_stepper_api) = {
.enable = tmc22xx_stepper_enable,
.disable = tmc22xx_stepper_disable,
.move_by = step_dir_stepper_common_move_by,
.is_moving = step_dir_stepper_common_is_moving,
.set_reference_position = step_dir_stepper_common_set_reference_position,
.get_actual_position = step_dir_stepper_common_get_actual_position,
.move_to = step_dir_stepper_common_move_to,
.set_microstep_interval = step_dir_stepper_common_set_microstep_interval,
.run = step_dir_stepper_common_run,
.stop = step_dir_stepper_common_stop,
.set_event_callback = step_dir_stepper_common_set_event_callback,
.step = step_dir_stepper_common_step,
.set_direction = step_dir_stepper_common_set_direction,
.set_micro_step_res = tmc22xx_stepper_set_micro_step_res,
.get_micro_step_res = tmc22xx_stepper_get_micro_step_res,
};
Expand All @@ -183,7 +175,6 @@ static DEVICE_API(stepper, tmc22xx_stepper_api) = {
(.msx_pins = tmc22xx_stepper_msx_pins_##inst)) \
}; \
static struct tmc22xx_data tmc22xx_data_##inst = { \
.common = STEP_DIR_STEPPER_DT_INST_COMMON_DATA_INIT(inst), \
.resolution = DT_INST_PROP(inst, micro_step_res), \
}; \
DEVICE_DT_INST_DEFINE(inst, tmc22xx_stepper_init, NULL, &tmc22xx_data_##inst, \
Expand Down
Loading