Skip to content

Commit 761c6fb

Browse files
jilaypandyakartben
authored andcommitted
tests: stepper_api: test stop, set_micro_step_res, set_micro_step_interval
unify error codes from all drivers based on stepper specification add a generic test case for set_micro_step_interval and stop Signed-off-by: Jilay Pandya <[email protected]>
1 parent f780df4 commit 761c6fb

File tree

5 files changed

+77
-28
lines changed

5 files changed

+77
-28
lines changed

drivers/stepper/adi_tmc/adi_tmc22xx_stepper_controller.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ static int tmc22xx_stepper_set_micro_step_res(const struct device *dev,
7171
}
7272

7373
LOG_ERR("Unsupported microstep resolution: %d", micro_step_res);
74-
return -EINVAL;
74+
return -ENOTSUP;
7575
}
7676

7777
static int tmc22xx_stepper_get_micro_step_res(const struct device *dev,

drivers/stepper/gpio_stepper_controller.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ static int gpio_stepper_set_micro_step_res(const struct device *dev,
289289
enum stepper_micro_step_resolution micro_step_res)
290290
{
291291
struct gpio_stepper_data *data = dev->data;
292+
int err = 0;
292293

293294
K_SPINLOCK(&data->lock) {
294295
switch (micro_step_res) {
@@ -298,10 +299,10 @@ static int gpio_stepper_set_micro_step_res(const struct device *dev,
298299
break;
299300
default:
300301
LOG_ERR("Unsupported micro step resolution %d", micro_step_res);
301-
return -ENOTSUP;
302+
err = -ENOTSUP;
302303
}
303304
}
304-
return 0;
305+
return err;
305306
}
306307

307308
static int gpio_stepper_get_micro_step_res(const struct device *dev,

drivers/stepper/ti/drv8424.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ static int drv8424_set_micro_step_res(const struct device *dev,
212212
m1_value = 2;
213213
break;
214214
default:
215-
return -EINVAL;
215+
return -ENOTSUP;
216216
};
217217

218218
ret = drv8424_set_microstep_pin(dev, &config->m0_pin, m0_value);

tests/drivers/stepper/drv8424/api/src/main.c

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,6 @@ ZTEST_F(drv8424_api, test_micro_step_res_set)
7979
res);
8080
}
8181

82-
ZTEST_F(drv8424_api, test_micro_step_res_set_incorrect_value_detection)
83-
{
84-
int ret = 0;
85-
86-
ret = stepper_set_micro_step_res(fixture->dev, 3);
87-
zassert_equal(ret, -EINVAL, "Command should fail with error %d but returned %d", -EINVAL,
88-
ret);
89-
}
90-
9182
ZTEST_F(drv8424_api, test_actual_position_set)
9283
{
9384
int32_t pos = 100u;

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

Lines changed: 72 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: Copyright (c) 2024 Jilay Sandeep Pandya
3-
*
2+
* SPDX-FileCopyrightText: Copyright (c) 2025 Jilay Sandeep Pandya
43
* SPDX-License-Identifier: Apache-2.0
54
*/
65

@@ -19,6 +18,18 @@ struct k_poll_signal stepper_signal;
1918
struct k_poll_event stepper_event;
2019
void *user_data_received;
2120

21+
#define POLL_AND_CHECK_SIGNAL(signal, event, expected_event, timeout) \
22+
({ \
23+
do { \
24+
(void)k_poll(&(event), 1, timeout); \
25+
unsigned int signaled; \
26+
int result; \
27+
k_poll_signal_check(&(signal), &signaled, &result); \
28+
zassert_equal(signaled, 1, "Signal not set"); \
29+
zassert_equal(result, (expected_event), "Signal not set"); \
30+
} while (0); \
31+
})
32+
2233
static void stepper_print_event_callback(const struct device *dev, enum stepper_event event,
2334
void *user_data)
2435
{
@@ -38,12 +49,15 @@ static void stepper_print_event_callback(const struct device *dev, enum stepper_
3849
case STEPPER_EVENT_STALL_DETECTED:
3950
k_poll_signal_raise(&stepper_signal, STEPPER_EVENT_STALL_DETECTED);
4051
break;
52+
case STEPPER_EVENT_STOPPED:
53+
k_poll_signal_raise(&stepper_signal, STEPPER_EVENT_STOPPED);
54+
break;
4155
default:
4256
break;
4357
}
4458

4559
LOG_DBG("Event %d, %s called for %s, expected for %s\n", event, __func__,
46-
dev_callback->name, dev->name);
60+
dev_callback->name, dev->name);
4761
}
4862

4963
static void *stepper_setup(void)
@@ -56,7 +70,7 @@ static void *stepper_setup(void)
5670
k_poll_signal_init(&stepper_signal);
5771
k_poll_event_init(&stepper_event, K_POLL_TYPE_SIGNAL, K_POLL_MODE_NOTIFY_ONLY,
5872
&stepper_signal);
59-
user_data_received = NULL;
73+
6074
zassert_not_null(fixture.dev);
6175
(void)stepper_enable(fixture.dev, true);
6276
return &fixture;
@@ -66,46 +80,89 @@ static void stepper_before(void *f)
6680
{
6781
struct stepper_fixture *fixture = f;
6882
(void)stepper_set_reference_position(fixture->dev, 0);
83+
6984
k_poll_signal_reset(&stepper_signal);
85+
86+
user_data_received = NULL;
7087
}
7188

7289
ZTEST_SUITE(stepper, NULL, stepper_setup, stepper_before, NULL, NULL);
7390

74-
ZTEST_F(stepper, test_micro_step_res)
91+
ZTEST_F(stepper, test_set_micro_step_res_incorrect)
92+
{
93+
int ret = stepper_set_micro_step_res(fixture->dev, 127);
94+
95+
zassert_equal(ret, -ENOTSUP, "Incorrect micro step resolution should return -ENOTSUP");
96+
}
97+
98+
ZTEST_F(stepper, test_get_micro_step_res)
7599
{
76100
enum stepper_micro_step_resolution res;
77101
(void)stepper_get_micro_step_res(fixture->dev, &res);
78102
zassert_equal(res, DT_PROP(DT_ALIAS(stepper), micro_step_res),
79103
"Micro step resolution not set correctly");
80104
}
81105

106+
ZTEST_F(stepper, test_set_micro_step_interval_invalid_zero)
107+
{
108+
int err = stepper_set_microstep_interval(fixture->dev, 0);
109+
110+
zassert_equal(err, -EINVAL, "ustep interval cannot be zero");
111+
}
112+
82113
ZTEST_F(stepper, test_actual_position)
83114
{
84115
int32_t pos = 100u;
116+
85117
(void)stepper_set_reference_position(fixture->dev, pos);
86118
(void)stepper_get_actual_position(fixture->dev, &pos);
87119
zassert_equal(pos, 100u, "Actual position not set correctly");
88120
}
89121

90-
ZTEST_F(stepper, test_target_position)
122+
ZTEST_F(stepper, test_target_position_w_fixed_step_interval)
91123
{
92-
int32_t pos = 100u;
124+
int32_t pos = 10u;
93125

94-
(void)stepper_set_microstep_interval(fixture->dev, 10000);
126+
(void)stepper_set_microstep_interval(fixture->dev, 100 * USEC_PER_SEC);
95127

96128
/* Pass the function name as user data */
97129
(void)stepper_set_event_callback(fixture->dev, fixture->callback, (void *)fixture->dev);
98130

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

101-
(void)k_poll(&stepper_event, 1, K_SECONDS(5));
102-
unsigned int signaled;
103-
int result;
133+
/* timeout is set with 20% tolerance */
134+
POLL_AND_CHECK_SIGNAL(stepper_signal, stepper_event, STEPPER_EVENT_STEPS_COMPLETED,
135+
K_MSEC(pos * 120));
104136

105-
k_poll_signal_check(&stepper_signal, &signaled, &result);
106-
zassert_equal(signaled, 1, "Signal not set");
107-
zassert_equal(result, STEPPER_EVENT_STEPS_COMPLETED, "Signal not set");
108137
(void)stepper_get_actual_position(fixture->dev, &pos);
109-
zassert_equal(pos, 100u, "Target position should be %d but is %d", 100u, pos);
138+
zassert_equal(pos, 10u, "Target position should be %d but is %d", 10u, pos);
110139
zassert_equal(user_data_received, fixture->dev, "User data not received");
111140
}
141+
142+
ZTEST_F(stepper, test_stop)
143+
{
144+
(void)stepper_set_event_callback(fixture->dev, fixture->callback, (void *)fixture->dev);
145+
146+
/* Run the stepper in positive direction */
147+
(void)stepper_run(fixture->dev, STEPPER_DIRECTION_POSITIVE);
148+
149+
/* Stop the stepper */
150+
int ret = stepper_stop(fixture->dev);
151+
bool is_moving;
152+
153+
if (ret == 0) {
154+
POLL_AND_CHECK_SIGNAL(stepper_signal, stepper_event, STEPPER_EVENT_STOPPED,
155+
K_NO_WAIT);
156+
zassert_equal(user_data_received, fixture->dev, "User data not received");
157+
158+
/* Check if the stepper is stopped */
159+
stepper_is_moving(fixture->dev, &is_moving);
160+
zassert_equal(is_moving, false, "Stepper is still moving");
161+
} else if (ret == -ENOSYS) {
162+
stepper_is_moving(fixture->dev, &is_moving);
163+
zassert_equal(is_moving, true,
164+
"Stepper should be moving since stop is not implemented");
165+
} else {
166+
zassert_unreachable("Stepper stop failed");
167+
}
168+
}

0 commit comments

Comments
 (0)