Skip to content

drivers: stepper: Fix stepper callbacks when using work_q #88835

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

Merged
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
4 changes: 2 additions & 2 deletions drivers/stepper/step_dir/step_dir_stepper_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,11 @@ static void position_mode_task(const struct device *dev)
(void)step_dir_stepper_perform_step(dev);
}

update_remaining_steps(dev->data);

if (config->timing_source->needs_reschedule(dev) && data->step_count != 0) {
(void)config->timing_source->start(dev);
}

update_remaining_steps(dev->data);
}

static void velocity_mode_task(const struct device *dev)
Expand Down
13 changes: 13 additions & 0 deletions tests/drivers/stepper/stepper_api/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright (c) 2024 Josselin Bunt
# SPDX-License-Identifier: Apache-2.0

mainmenu "Stepper API Test"

source "Kconfig.zephyr"

config STEPPER_TEST_TIMING_TIMEOUT_TOLERANCE_PCT
int "Stepper timing tolerance percentage"
default 20
help
Additional margin (%) added to step timing during test timeouts.
Accounts for execution and scheduling jitter.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) 2025 Josselin Bunt
* SPDX-License-Identifier: Apache-2.0
*/

#include "native_sim.overlay"

/ {
aliases {
stepper = &adi_tmc2209;
};
};

/ {
adi_tmc2209: adi_tmc2209 {
status = "okay";
compatible = "adi,tmc2209";
micro-step-res = <32>;
dir-gpios = <&gpio1 0 0>;
step-gpios = <&gpio1 1 0>;
en-gpios = <&gpio2 1 0>;
msx-gpios = <&gpio3 0 0>, <&gpio4 1 0>;
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2025 Josselin Bunt
* SPDX-License-Identifier: Apache-2.0
*/

#include "native_sim.overlay"

/ {
aliases {
stepper = &allegro_a4979;
};
};

/ {
allegro_a4979: allegro_a4979 {
status = "okay";
compatible = "allegro,a4979";
micro-step-res = <1>;
reset-gpios = <&gpio4 0 0>;
dir-gpios = <&gpio1 0 0>;
step-gpios = <&gpio1 1 0>;
en-gpios = <&gpio2 1 0>;
m0-gpios = <&gpio3 0 0>;
m1-gpios = <&gpio3 1 0>;
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2025 Josselin Bunt
* SPDX-License-Identifier: Apache-2.0
*/

#include "native_sim.overlay"

/ {
aliases {
stepper = &ti_drv84xx;
};
};

/ {
ti_drv84xx: ti_drv84xx {
status = "okay";
compatible = "ti,drv84xx";
micro-step-res = <8>;
dir-gpios = <&gpio1 0 0>;
step-gpios = <&gpio1 1 0>;
sleep-gpios = <&gpio2 0 GPIO_ACTIVE_LOW>;
en-gpios = <&gpio2 1 0>;
m0-gpios = <&gpio3 0 0>;
m1-gpios = <&gpio3 1 0>;
};
};
36 changes: 33 additions & 3 deletions tests/drivers/stepper/stepper_api/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,45 @@ ZTEST_F(stepper, test_target_position_w_fixed_step_interval)

(void)stepper_move_to(fixture->dev, pos);

/* timeout is set with 20% tolerance */
POLL_AND_CHECK_SIGNAL(stepper_signal, stepper_event, STEPPER_EVENT_STEPS_COMPLETED,
K_MSEC(pos * 120));
POLL_AND_CHECK_SIGNAL(
stepper_signal, stepper_event, STEPPER_EVENT_STEPS_COMPLETED,
K_MSEC(pos * (100 + CONFIG_STEPPER_TEST_TIMING_TIMEOUT_TOLERANCE_PCT)));

(void)stepper_get_actual_position(fixture->dev, &pos);
zassert_equal(pos, 10u, "Target position should be %d but is %d", 10u, pos);
zassert_equal(user_data_received, fixture->dev, "User data not received");
}

ZTEST_F(stepper, test_move_by_positive_step_count)
{
int32_t steps = 20;

(void)stepper_set_microstep_interval(fixture->dev, 100 * USEC_PER_SEC);
(void)stepper_set_event_callback(fixture->dev, fixture->callback, (void *)fixture->dev);
(void)stepper_move_by(fixture->dev, steps);

POLL_AND_CHECK_SIGNAL(
stepper_signal, stepper_event, STEPPER_EVENT_STEPS_COMPLETED,
K_MSEC(steps * (100 + CONFIG_STEPPER_TEST_TIMING_TIMEOUT_TOLERANCE_PCT)));
(void)stepper_get_actual_position(fixture->dev, &steps);
zassert_equal(steps, 20u, "Target position should be %d but is %d", 20u, steps);
}

ZTEST_F(stepper, test_move_by_negative_step_count)
{
int32_t steps = -20;

(void)stepper_set_microstep_interval(fixture->dev, 100 * USEC_PER_SEC);
(void)stepper_set_event_callback(fixture->dev, fixture->callback, (void *)fixture->dev);
(void)stepper_move_by(fixture->dev, steps);

POLL_AND_CHECK_SIGNAL(
stepper_signal, stepper_event, STEPPER_EVENT_STEPS_COMPLETED,
K_MSEC(-steps * (100 + CONFIG_STEPPER_TEST_TIMING_TIMEOUT_TOLERANCE_PCT)));
(void)stepper_get_actual_position(fixture->dev, &steps);
zassert_equal(steps, -20u, "Target position should be %d but is %d", -20u, steps);
}

ZTEST_F(stepper, test_stop)
{
(void)stepper_set_event_callback(fixture->dev, fixture->callback, (void *)fixture->dev);
Expand Down
27 changes: 27 additions & 0 deletions tests/drivers/stepper/stepper_api/testcase.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ tests:
- CONFIG_STEPPER_STEP_DIR_GENERATE_ISR_SAFE_EVENTS=y
platform_allow:
- native_sim/native/64
drivers.stepper.stepper_api.adi_tmc2209_work_q:
extra_args:
- platform:native_sim/native/64:DTC_OVERLAY_FILE="boards/native_sim_adi_tmc2209_work_q.overlay"
extra_configs:
- CONFIG_GPIO=y
- CONFIG_STEPPER_STEP_DIR_GENERATE_ISR_SAFE_EVENTS=y
- CONFIG_STEPPER_TEST_TIMING_TIMEOUT_TOLERANCE_PCT=30
platform_allow:
- native_sim/native/64
drivers.stepper.stepper_api.allegro_a4979:
extra_args:
- platform:native_sim/native/64:DTC_OVERLAY_FILE="boards/native_sim_allegro_a4979.overlay"
Expand All @@ -25,6 +34,15 @@ tests:
- CONFIG_STEPPER_STEP_DIR_GENERATE_ISR_SAFE_EVENTS=y
platform_allow:
- native_sim/native/64
drivers.stepper.stepper_api.allegro_a4979_work_q:
extra_args:
- platform:native_sim/native/64:DTC_OVERLAY_FILE="boards/native_sim_allegro_a4979_work_q.overlay"
extra_configs:
- CONFIG_GPIO=y
- CONFIG_STEPPER_STEP_DIR_GENERATE_ISR_SAFE_EVENTS=y
- CONFIG_STEPPER_TEST_TIMING_TIMEOUT_TOLERANCE_PCT=30
platform_allow:
- native_sim/native/64
drivers.stepper.stepper_api.ti_drv84xx:
extra_args:
- platform:native_sim/native/64:DTC_OVERLAY_FILE="boards/native_sim_ti_drv84xx.overlay"
Expand All @@ -34,6 +52,15 @@ tests:
- CONFIG_STEPPER_STEP_DIR_GENERATE_ISR_SAFE_EVENTS=y
platform_allow:
- native_sim/native/64
drivers.stepper.stepper_api.ti_drv84xx_work_q:
extra_args:
- platform:native_sim/native/64:DTC_OVERLAY_FILE="boards/native_sim_ti_drv84xx_work_q.overlay"
extra_configs:
- CONFIG_GPIO=y
- CONFIG_STEPPER_STEP_DIR_GENERATE_ISR_SAFE_EVENTS=y
- CONFIG_STEPPER_TEST_TIMING_TIMEOUT_TOLERANCE_PCT=30
platform_allow:
- native_sim/native/64
drivers.stepper.stepper_api.zephyr_gpio_stepper:
extra_args:
- platform:native_sim/native/64:DTC_OVERLAY_FILE="boards/native_sim_zephyr_gpio_stepper.overlay"
Expand Down