Skip to content

Commit 1f1bec8

Browse files
jilaypandyaaescolar
authored andcommitted
drivers: stepper: shell: add stop function
Add stop function to stepper shell. align the function order to match the one in __subsystem stepper_driver_api struct Signed-off-by: Jilay Pandya <[email protected]>
1 parent 99cce69 commit 1f1bec8

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

drivers/stepper/fake_stepper_controller.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ DEFINE_FAKE_VALUE_FUNC(int, fake_stepper_move_to, const struct device *, int32_t
4141

4242
DEFINE_FAKE_VALUE_FUNC(int, fake_stepper_run, const struct device *, enum stepper_direction);
4343

44+
DEFINE_FAKE_VALUE_FUNC(int, fake_stepper_stop, const struct device *);
45+
4446
DEFINE_FAKE_VALUE_FUNC(int, fake_stepper_set_event_callback, const struct device *,
4547
stepper_event_callback_t, void *);
4648

@@ -98,6 +100,7 @@ static void fake_stepper_reset_rule_before(const struct ztest_unit_test *test, v
98100
RESET_FAKE(fake_stepper_get_actual_position);
99101
RESET_FAKE(fake_stepper_move_to);
100102
RESET_FAKE(fake_stepper_run);
103+
RESET_FAKE(fake_stepper_stop);
101104

102105
/* Install custom fakes for the setter and getter functions */
103106
fake_stepper_set_micro_step_res_fake.custom_fake = fake_stepper_set_micro_step_res_delegate;
@@ -134,6 +137,7 @@ static DEVICE_API(stepper, fake_stepper_driver_api) = {
134137
.get_actual_position = fake_stepper_get_actual_position,
135138
.move_to = fake_stepper_move_to,
136139
.run = fake_stepper_run,
140+
.stop = fake_stepper_stop,
137141
.set_event_callback = fake_stepper_set_event_callback,
138142
};
139143

drivers/stepper/stepper_shell.c

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ static void print_callback(const struct device *dev, const enum stepper_event ev
6161
case STEPPER_EVENT_RIGHT_END_STOP_DETECTED:
6262
shell_info(sh, "%s: Right limit switch pressed.", dev->name);
6363
break;
64+
case STEPPER_EVENT_STOPPED:
65+
shell_info(sh, "%s: Stepper stopped.", dev->name);
66+
break;
6467
default:
6568
shell_info(sh, "%s: Unknown signal received.", dev->name);
6669
break;
@@ -195,6 +198,30 @@ static int cmd_stepper_enable(const struct shell *sh, size_t argc, char **argv)
195198
return err;
196199
}
197200

201+
static int cmd_stepper_stop(const struct shell *sh, size_t argc, char **argv)
202+
{
203+
const struct device *dev;
204+
int err = 0;
205+
206+
err = parse_device_arg(sh, argv, &dev);
207+
if (err < 0) {
208+
return err;
209+
}
210+
211+
err = stepper_stop(dev);
212+
if (err) {
213+
shell_error(sh, "Error: %d", err);
214+
return err;
215+
}
216+
217+
err = stepper_set_event_callback(dev, print_callback, (void *)sh);
218+
if (err != 0) {
219+
shell_error(sh, "Failed to set callback: %d", err);
220+
}
221+
222+
return err;
223+
}
224+
198225
static int cmd_stepper_move_by(const struct shell *sh, size_t argc, char **argv)
199226
{
200227
const struct device *dev;
@@ -452,10 +479,6 @@ SHELL_STATIC_SUBCMD_SET_CREATE(
452479
stepper_cmds,
453480
SHELL_CMD_ARG(enable, &dsub_pos_stepper_motor_name, "<device> <on/off>", cmd_stepper_enable,
454481
3, 0),
455-
SHELL_CMD_ARG(move_by, &dsub_pos_stepper_motor_name, "<device> <microsteps>",
456-
cmd_stepper_move_by, 3, 0),
457-
SHELL_CMD_ARG(set_microstep_interval, &dsub_pos_stepper_motor_name,
458-
"<device> <microstep_interval_ns>", cmd_stepper_set_microstep_interval, 3, 0),
459482
SHELL_CMD_ARG(set_micro_step_res, &dsub_pos_stepper_motor_name_microstep,
460483
"<device> <resolution>", cmd_stepper_set_micro_step_res, 3, 0),
461484
SHELL_CMD_ARG(get_micro_step_res, &dsub_pos_stepper_motor_name, "<device>",
@@ -464,10 +487,15 @@ SHELL_STATIC_SUBCMD_SET_CREATE(
464487
cmd_stepper_set_reference_position, 3, 0),
465488
SHELL_CMD_ARG(get_actual_position, &dsub_pos_stepper_motor_name, "<device>",
466489
cmd_stepper_get_actual_position, 2, 0),
490+
SHELL_CMD_ARG(set_microstep_interval, &dsub_pos_stepper_motor_name,
491+
"<device> <microstep_interval_ns>", cmd_stepper_set_microstep_interval, 3, 0),
492+
SHELL_CMD_ARG(move_by, &dsub_pos_stepper_motor_name, "<device> <microsteps>",
493+
cmd_stepper_move_by, 3, 0),
467494
SHELL_CMD_ARG(move_to, &dsub_pos_stepper_motor_name, "<device> <microsteps>",
468495
cmd_stepper_move_to, 3, 0),
469496
SHELL_CMD_ARG(run, &dsub_pos_stepper_motor_name_dir, "<device> <direction>",
470497
cmd_stepper_run, 3, 0),
498+
SHELL_CMD_ARG(stop, &dsub_pos_stepper_motor_name, "<device>", cmd_stepper_stop, 2, 0),
471499
SHELL_CMD_ARG(info, &dsub_pos_stepper_motor_name, "<device>", cmd_stepper_info, 2, 0),
472500
SHELL_SUBCMD_SET_END);
473501

include/zephyr/drivers/stepper/stepper_fake.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ DECLARE_FAKE_VALUE_FUNC(int, fake_stepper_is_moving, const struct device *, bool
3636

3737
DECLARE_FAKE_VALUE_FUNC(int, fake_stepper_run, const struct device *, enum stepper_direction);
3838

39+
DECLARE_FAKE_VALUE_FUNC(int, fake_stepper_stop, const struct device *);
40+
3941
DECLARE_FAKE_VALUE_FUNC(int, fake_stepper_set_event_callback, const struct device *,
4042
stepper_event_callback_t, void *);
4143

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,15 @@ ZTEST(stepper_shell, test_stepper_run_invalid_direction)
145145
zassert_not_equal(err, 0, " executed run with invalid direction value");
146146
}
147147

148+
ZTEST(stepper_shell, test_stepper_stop)
149+
{
150+
const struct shell *sh = shell_backend_dummy_get_ptr();
151+
int err = shell_execute_cmd(sh, "stepper stop " FAKE_STEPPER_NAME);
152+
153+
ASSERT_STEPPER_FUNC_CALLED(fake_stepper_stop_fake, err);
154+
zassert_equal(err, 0, "stepper stop could not be executed");
155+
}
156+
148157
ZTEST(stepper_shell, test_stepper_info)
149158
{
150159
const struct shell *sh = shell_backend_dummy_get_ptr();

0 commit comments

Comments
 (0)