Skip to content

Commit c497088

Browse files
jbr7rrkartben
authored andcommitted
drivers: stepper: Add unit tests for stepper API using work_q
This commit adds unit tests for the stepper API using work_q scheduler. Signed-off-by: Josselin Bunt <josselin@sensible.health>
1 parent 902824d commit c497088

File tree

6 files changed

+149
-3
lines changed

6 files changed

+149
-3
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright (c) 2024 Josselin Bunt
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
mainmenu "Stepper API Test"
5+
6+
source "Kconfig.zephyr"
7+
8+
config STEPPER_TEST_TIMING_TIMEOUT_TOLERANCE_PCT
9+
int "Stepper timing tolerance percentage"
10+
default 20
11+
help
12+
Additional margin (%) added to step timing during test timeouts.
13+
Accounts for execution and scheduling jitter.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (c) 2025 Josselin Bunt
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "native_sim.overlay"
7+
8+
/ {
9+
aliases {
10+
stepper = &adi_tmc2209;
11+
};
12+
};
13+
14+
/ {
15+
adi_tmc2209: adi_tmc2209 {
16+
status = "okay";
17+
compatible = "adi,tmc2209";
18+
micro-step-res = <32>;
19+
dir-gpios = <&gpio1 0 0>;
20+
step-gpios = <&gpio1 1 0>;
21+
en-gpios = <&gpio2 1 0>;
22+
msx-gpios = <&gpio3 0 0>, <&gpio4 1 0>;
23+
};
24+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (c) 2025 Josselin Bunt
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "native_sim.overlay"
7+
8+
/ {
9+
aliases {
10+
stepper = &allegro_a4979;
11+
};
12+
};
13+
14+
/ {
15+
allegro_a4979: allegro_a4979 {
16+
status = "okay";
17+
compatible = "allegro,a4979";
18+
micro-step-res = <1>;
19+
reset-gpios = <&gpio4 0 0>;
20+
dir-gpios = <&gpio1 0 0>;
21+
step-gpios = <&gpio1 1 0>;
22+
en-gpios = <&gpio2 1 0>;
23+
m0-gpios = <&gpio3 0 0>;
24+
m1-gpios = <&gpio3 1 0>;
25+
};
26+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (c) 2025 Josselin Bunt
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "native_sim.overlay"
7+
8+
/ {
9+
aliases {
10+
stepper = &ti_drv84xx;
11+
};
12+
};
13+
14+
/ {
15+
ti_drv84xx: ti_drv84xx {
16+
status = "okay";
17+
compatible = "ti,drv84xx";
18+
micro-step-res = <8>;
19+
dir-gpios = <&gpio1 0 0>;
20+
step-gpios = <&gpio1 1 0>;
21+
sleep-gpios = <&gpio2 0 GPIO_ACTIVE_LOW>;
22+
en-gpios = <&gpio2 1 0>;
23+
m0-gpios = <&gpio3 0 0>;
24+
m1-gpios = <&gpio3 1 0>;
25+
};
26+
};

tests/drivers/stepper/stepper_api/src/main.c

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,45 @@ ZTEST_F(stepper, test_target_position_w_fixed_step_interval)
140140

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

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

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

152+
ZTEST_F(stepper, test_move_by_positive_step_count)
153+
{
154+
int32_t steps = 20;
155+
156+
(void)stepper_set_microstep_interval(fixture->dev, 100 * USEC_PER_SEC);
157+
(void)stepper_set_event_callback(fixture->dev, fixture->callback, (void *)fixture->dev);
158+
(void)stepper_move_by(fixture->dev, steps);
159+
160+
POLL_AND_CHECK_SIGNAL(
161+
stepper_signal, stepper_event, STEPPER_EVENT_STEPS_COMPLETED,
162+
K_MSEC(steps * (100 + CONFIG_STEPPER_TEST_TIMING_TIMEOUT_TOLERANCE_PCT)));
163+
(void)stepper_get_actual_position(fixture->dev, &steps);
164+
zassert_equal(steps, 20u, "Target position should be %d but is %d", 20u, steps);
165+
}
166+
167+
ZTEST_F(stepper, test_move_by_negative_step_count)
168+
{
169+
int32_t steps = -20;
170+
171+
(void)stepper_set_microstep_interval(fixture->dev, 100 * USEC_PER_SEC);
172+
(void)stepper_set_event_callback(fixture->dev, fixture->callback, (void *)fixture->dev);
173+
(void)stepper_move_by(fixture->dev, steps);
174+
175+
POLL_AND_CHECK_SIGNAL(
176+
stepper_signal, stepper_event, STEPPER_EVENT_STEPS_COMPLETED,
177+
K_MSEC(-steps * (100 + CONFIG_STEPPER_TEST_TIMING_TIMEOUT_TOLERANCE_PCT)));
178+
(void)stepper_get_actual_position(fixture->dev, &steps);
179+
zassert_equal(steps, -20u, "Target position should be %d but is %d", -20u, steps);
180+
}
181+
152182
ZTEST_F(stepper, test_stop)
153183
{
154184
(void)stepper_set_event_callback(fixture->dev, fixture->callback, (void *)fixture->dev);

tests/drivers/stepper/stepper_api/testcase.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ tests:
1616
- CONFIG_STEPPER_STEP_DIR_GENERATE_ISR_SAFE_EVENTS=y
1717
platform_allow:
1818
- native_sim/native/64
19+
drivers.stepper.stepper_api.adi_tmc2209_work_q:
20+
extra_args:
21+
- platform:native_sim/native/64:DTC_OVERLAY_FILE="boards/native_sim_adi_tmc2209_work_q.overlay"
22+
extra_configs:
23+
- CONFIG_GPIO=y
24+
- CONFIG_STEPPER_STEP_DIR_GENERATE_ISR_SAFE_EVENTS=y
25+
- CONFIG_STEPPER_TEST_TIMING_TIMEOUT_TOLERANCE_PCT=30
26+
platform_allow:
27+
- native_sim/native/64
1928
drivers.stepper.stepper_api.allegro_a4979:
2029
extra_args:
2130
- platform:native_sim/native/64:DTC_OVERLAY_FILE="boards/native_sim_allegro_a4979.overlay"
@@ -25,6 +34,15 @@ tests:
2534
- CONFIG_STEPPER_STEP_DIR_GENERATE_ISR_SAFE_EVENTS=y
2635
platform_allow:
2736
- native_sim/native/64
37+
drivers.stepper.stepper_api.allegro_a4979_work_q:
38+
extra_args:
39+
- platform:native_sim/native/64:DTC_OVERLAY_FILE="boards/native_sim_allegro_a4979_work_q.overlay"
40+
extra_configs:
41+
- CONFIG_GPIO=y
42+
- CONFIG_STEPPER_STEP_DIR_GENERATE_ISR_SAFE_EVENTS=y
43+
- CONFIG_STEPPER_TEST_TIMING_TIMEOUT_TOLERANCE_PCT=30
44+
platform_allow:
45+
- native_sim/native/64
2846
drivers.stepper.stepper_api.ti_drv84xx:
2947
extra_args:
3048
- platform:native_sim/native/64:DTC_OVERLAY_FILE="boards/native_sim_ti_drv84xx.overlay"
@@ -34,6 +52,15 @@ tests:
3452
- CONFIG_STEPPER_STEP_DIR_GENERATE_ISR_SAFE_EVENTS=y
3553
platform_allow:
3654
- native_sim/native/64
55+
drivers.stepper.stepper_api.ti_drv84xx_work_q:
56+
extra_args:
57+
- platform:native_sim/native/64:DTC_OVERLAY_FILE="boards/native_sim_ti_drv84xx_work_q.overlay"
58+
extra_configs:
59+
- CONFIG_GPIO=y
60+
- CONFIG_STEPPER_STEP_DIR_GENERATE_ISR_SAFE_EVENTS=y
61+
- CONFIG_STEPPER_TEST_TIMING_TIMEOUT_TOLERANCE_PCT=30
62+
platform_allow:
63+
- native_sim/native/64
3764
drivers.stepper.stepper_api.zephyr_gpio_stepper:
3865
extra_args:
3966
- platform:native_sim/native/64:DTC_OVERLAY_FILE="boards/native_sim_zephyr_gpio_stepper.overlay"

0 commit comments

Comments
 (0)