Skip to content

Commit 6c26d1b

Browse files
committed
drivers: stepper: shell: add stepper index selection functionality
Add stepper index selection functionality in stepper shell to select different steppers for the same stepper motion controller Signed-off-by: Jilay Pandya <[email protected]>
1 parent 6afb7ee commit 6c26d1b

File tree

2 files changed

+182
-54
lines changed

2 files changed

+182
-54
lines changed

drivers/stepper/stepper_shell.c

Lines changed: 162 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ struct stepper_direction_map {
2929
enum stepper_direction direction;
3030
};
3131

32+
struct stepper_control_idx_map {
33+
const char *name;
34+
uint8_t stepper_idx;
35+
};
36+
37+
#define STEPPER_DRV_MICROSTEP_PARAM_IDX 2
38+
3239
#define STEPPER_DIRECTION_MAP_ENTRY(_name, _dir) \
3340
{ \
3441
.name = _name, \
@@ -41,6 +48,12 @@ struct stepper_direction_map {
4148
.microstep = _microstep, \
4249
}
4350

51+
#define STEPPER_CONTROL_IDX_MAP_ENTRY(_name, _idx) \
52+
{ \
53+
.name = _name, \
54+
.stepper_idx = _idx, \
55+
}
56+
4457
static void print_callback(const struct device *dev, const uint8_t stepper_idx,
4558
const enum stepper_event event, void *user_data)
4659
{
@@ -71,7 +84,12 @@ static void print_callback(const struct device *dev, const uint8_t stepper_idx,
7184
}
7285
}
7386

74-
static bool device_is_stepper(const struct device *dev)
87+
static bool device_is_stepper_drv(const struct device *dev)
88+
{
89+
return DEVICE_API_IS(stepper_drv, dev);
90+
}
91+
92+
static bool device_is_stepper_controller(const struct device *dev)
7593
{
7694
return DEVICE_API_IS(stepper, dev);
7795
}
@@ -81,6 +99,12 @@ static const struct stepper_direction_map stepper_direction_map[] = {
8199
STEPPER_DIRECTION_MAP_ENTRY("negative", STEPPER_DIRECTION_NEGATIVE),
82100
};
83101

102+
static const struct stepper_control_idx_map stepper_control_idx_map[] = {
103+
STEPPER_CONTROL_IDX_MAP_ENTRY("0", 0),
104+
STEPPER_CONTROL_IDX_MAP_ENTRY("1", 1),
105+
STEPPER_CONTROL_IDX_MAP_ENTRY("2", 2),
106+
};
107+
84108
static const struct stepper_microstep_map stepper_microstep_map[] = {
85109
STEPPER_MICROSTEP_MAP("1", STEPPER_MICRO_STEP_1),
86110
STEPPER_MICROSTEP_MAP("2", STEPPER_MICRO_STEP_2),
@@ -107,6 +131,34 @@ static void cmd_stepper_direction(size_t idx, struct shell_static_entry *entry)
107131

108132
SHELL_DYNAMIC_CMD_CREATE(dsub_stepper_direction, cmd_stepper_direction);
109133

134+
static void cmd_stepper_idx_dir(size_t idx, struct shell_static_entry *entry)
135+
{
136+
if (idx < ARRAY_SIZE(stepper_control_idx_map)) {
137+
entry->syntax = stepper_control_idx_map[idx].name;
138+
} else {
139+
entry->syntax = NULL;
140+
}
141+
entry->handler = NULL;
142+
entry->help = "Stepper direction";
143+
entry->subcmd = &dsub_stepper_direction;
144+
}
145+
146+
SHELL_DYNAMIC_CMD_CREATE(dsub_stepper_idx_dir, cmd_stepper_idx_dir);
147+
148+
static void cmd_stepper_idx(size_t idx, struct shell_static_entry *entry)
149+
{
150+
if (idx < ARRAY_SIZE(stepper_control_idx_map)) {
151+
entry->syntax = stepper_control_idx_map[idx].name;
152+
} else {
153+
entry->syntax = NULL;
154+
}
155+
entry->handler = NULL;
156+
entry->help = "Stepper direction";
157+
entry->subcmd = NULL;
158+
}
159+
160+
SHELL_DYNAMIC_CMD_CREATE(dsub_stepper_idx, cmd_stepper_idx);
161+
110162
static void cmd_stepper_microstep(size_t idx, struct shell_static_entry *entry)
111163
{
112164
if (idx < ARRAY_SIZE(stepper_microstep_map)) {
@@ -123,7 +175,7 @@ SHELL_DYNAMIC_CMD_CREATE(dsub_stepper_microstep, cmd_stepper_microstep);
123175

124176
static void cmd_pos_stepper_motor_name(size_t idx, struct shell_static_entry *entry)
125177
{
126-
const struct device *dev = shell_device_filter(idx, device_is_stepper);
178+
const struct device *dev = shell_device_filter(idx, device_is_stepper_drv);
127179

128180
entry->syntax = (dev != NULL) ? dev->name : NULL;
129181
entry->handler = NULL;
@@ -133,9 +185,21 @@ static void cmd_pos_stepper_motor_name(size_t idx, struct shell_static_entry *en
133185

134186
SHELL_DYNAMIC_CMD_CREATE(dsub_pos_stepper_motor_name, cmd_pos_stepper_motor_name);
135187

136-
static void cmd_pos_stepper_motor_name_dir(size_t idx, struct shell_static_entry *entry)
188+
static void cmd_pos_stepper_controller_name(size_t idx, struct shell_static_entry *entry)
137189
{
138-
const struct device *dev = shell_device_filter(idx, device_is_stepper);
190+
const struct device *dev = shell_device_filter(idx, device_is_stepper_controller);
191+
192+
entry->syntax = (dev != NULL) ? dev->name : NULL;
193+
entry->handler = NULL;
194+
entry->help = "List Devices";
195+
entry->subcmd = &dsub_stepper_idx;
196+
}
197+
198+
SHELL_DYNAMIC_CMD_CREATE(dsub_pos_stepper_controller_name, cmd_pos_stepper_controller_name);
199+
200+
static void cmd_pos_stepper_controller_name_dir(size_t idx, struct shell_static_entry *entry)
201+
{
202+
const struct device *dev = shell_device_filter(idx, device_is_stepper_controller);
139203

140204
if (dev != NULL) {
141205
entry->syntax = dev->name;
@@ -144,14 +208,14 @@ static void cmd_pos_stepper_motor_name_dir(size_t idx, struct shell_static_entry
144208
}
145209
entry->handler = NULL;
146210
entry->help = "List Devices";
147-
entry->subcmd = &dsub_stepper_direction;
211+
entry->subcmd = &dsub_stepper_idx_dir;
148212
}
149213

150-
SHELL_DYNAMIC_CMD_CREATE(dsub_pos_stepper_motor_name_dir, cmd_pos_stepper_motor_name_dir);
214+
SHELL_DYNAMIC_CMD_CREATE(dsub_pos_stepper_controller_name_dir, cmd_pos_stepper_controller_name_dir);
151215

152216
static void cmd_pos_stepper_motor_name_microstep(size_t idx, struct shell_static_entry *entry)
153217
{
154-
const struct device *dev = shell_device_filter(idx, device_is_stepper);
218+
const struct device *dev = shell_device_filter(idx, device_is_stepper_drv);
155219

156220
if (dev != NULL) {
157221
entry->syntax = dev->name;
@@ -212,23 +276,40 @@ static int cmd_stepper_disable(const struct shell *sh, size_t argc, char **argv)
212276
return err;
213277
}
214278

279+
static int get_stepper_index(char **argv, uint8_t *stepper_idx)
280+
{
281+
for (int i = 0; i < ARRAY_SIZE(stepper_control_idx_map); i++) {
282+
if (strcmp(argv[ARG_IDX_DEV_IDX], stepper_control_idx_map[i].name) == 0) {
283+
*stepper_idx = stepper_control_idx_map[i].stepper_idx;
284+
return 0;
285+
}
286+
}
287+
return -EINVAL;
288+
}
215289
static int cmd_stepper_stop(const struct shell *sh, size_t argc, char **argv)
216290
{
217291
const struct device *dev;
292+
uint8_t stepper_index;
218293
int err = 0;
219294

295+
err = get_stepper_index(argv, &stepper_index);
296+
if (err < 0) {
297+
shell_error(sh, "Invalid stepper index: %s", argv[ARG_IDX_DEV_IDX]);
298+
return err;
299+
}
300+
220301
err = parse_device_arg(sh, argv, &dev);
221302
if (err < 0) {
222303
return err;
223304
}
224305

225-
err = stepper_stop(dev);
306+
err = stepper_stop(dev, stepper_index);
226307
if (err) {
227308
shell_error(sh, "Error: %d", err);
228309
return err;
229310
}
230311

231-
err = stepper_set_event_callback(dev, print_callback, (void *)sh);
312+
err = stepper_set_event_callback(dev, stepper_index, print_callback, (void *)sh);
232313
if (err != 0) {
233314
shell_error(sh, "Failed to set callback: %d", err);
234315
}
@@ -239,9 +320,15 @@ static int cmd_stepper_stop(const struct shell *sh, size_t argc, char **argv)
239320
static int cmd_stepper_move_by(const struct shell *sh, size_t argc, char **argv)
240321
{
241322
const struct device *dev;
323+
uint8_t stepper_index;
242324
int err = 0;
243325

244-
const uint8_t stepper_index = shell_strtol(argv[ARG_IDX_DEV_IDX], 10, &err);
326+
err = get_stepper_index(argv, &stepper_index);
327+
if (err < 0) {
328+
shell_error(sh, "Invalid stepper index: %s", argv[ARG_IDX_DEV_IDX]);
329+
return err;
330+
}
331+
245332
int32_t micro_steps = shell_strtol(argv[ARG_IDX_PARAM], 10, &err);
246333

247334
if (err < 0) {
@@ -269,8 +356,15 @@ static int cmd_stepper_move_by(const struct shell *sh, size_t argc, char **argv)
269356
static int cmd_stepper_set_microstep_interval(const struct shell *sh, size_t argc, char **argv)
270357
{
271358
const struct device *dev;
359+
uint8_t stepper_index;
272360
int err = 0;
273-
const uint8_t stepper_index = shell_strtol(argv[ARG_IDX_DEV_IDX], 10, &err);
361+
362+
err = get_stepper_index(argv, &stepper_index);
363+
if (err < 0) {
364+
shell_error(sh, "Invalid stepper index: %s", argv[ARG_IDX_DEV_IDX]);
365+
return err;
366+
}
367+
274368
uint64_t step_interval = shell_strtoull(argv[ARG_IDX_PARAM], 10, &err);
275369

276370
if (err < 0) {
@@ -297,14 +391,16 @@ static int cmd_stepper_set_micro_step_res(const struct shell *sh, size_t argc, c
297391
int err = -EINVAL;
298392

299393
for (int i = 0; i < ARRAY_SIZE(stepper_microstep_map); i++) {
300-
if (strcmp(argv[ARG_IDX_PARAM], stepper_microstep_map[i].name) == 0) {
394+
if (strcmp(argv[STEPPER_DRV_MICROSTEP_PARAM_IDX], stepper_microstep_map[i].name) ==
395+
0) {
301396
resolution = stepper_microstep_map[i].microstep;
302397
err = 0;
303398
break;
304399
}
305400
}
306401
if (err != 0) {
307-
shell_error(sh, "Invalid microstep value %s", argv[ARG_IDX_PARAM]);
402+
shell_error(sh, "Invalid microstep value %s",
403+
argv[STEPPER_DRV_MICROSTEP_PARAM_IDX]);
308404
return err;
309405
}
310406

@@ -313,7 +409,7 @@ static int cmd_stepper_set_micro_step_res(const struct shell *sh, size_t argc, c
313409
return err;
314410
}
315411

316-
err = stepper_drv_set_micro_stepper_res(dev, resolution);
412+
err = stepper_drv_set_micro_step_res(dev, resolution);
317413
if (err) {
318414
shell_error(sh, "Error: %d", err);
319415
}
@@ -345,8 +441,15 @@ static int cmd_stepper_get_micro_step_res(const struct shell *sh, size_t argc, c
345441
static int cmd_stepper_set_reference_position(const struct shell *sh, size_t argc, char **argv)
346442
{
347443
const struct device *dev;
444+
uint8_t stepper_index;
348445
int err = 0;
349-
const uint8_t stepper_index = shell_strtol(argv[ARG_IDX_DEV_IDX], 10, &err);
446+
447+
err = get_stepper_index(argv, &stepper_index);
448+
if (err < 0) {
449+
shell_error(sh, "Invalid stepper index: %s", argv[ARG_IDX_DEV_IDX]);
450+
return err;
451+
}
452+
350453
int32_t position = shell_strtol(argv[ARG_IDX_PARAM], 10, &err);
351454

352455
if (err < 0) {
@@ -369,8 +472,15 @@ static int cmd_stepper_set_reference_position(const struct shell *sh, size_t arg
369472
static int cmd_stepper_get_actual_position(const struct shell *sh, size_t argc, char **argv)
370473
{
371474
const struct device *dev;
475+
uint8_t stepper_index;
372476
int err;
373-
const uint8_t stepper_index = shell_strtol(argv[ARG_IDX_DEV_IDX], 10, &err);
477+
478+
err = get_stepper_index(argv, &stepper_index);
479+
if (err < 0) {
480+
shell_error(sh, "Invalid stepper index: %s", argv[ARG_IDX_DEV_IDX]);
481+
return err;
482+
}
483+
374484
int32_t actual_position;
375485

376486
err = parse_device_arg(sh, argv, &dev);
@@ -391,8 +501,15 @@ static int cmd_stepper_get_actual_position(const struct shell *sh, size_t argc,
391501
static int cmd_stepper_move_to(const struct shell *sh, size_t argc, char **argv)
392502
{
393503
const struct device *dev;
504+
uint8_t stepper_index;
394505
int err = 0;
395-
const uint8_t stepper_index = shell_strtol(argv[ARG_IDX_DEV_IDX], 10, &err);
506+
507+
err = get_stepper_index(argv, &stepper_index);
508+
if (err < 0) {
509+
shell_error(sh, "Invalid stepper index: %s", argv[ARG_IDX_DEV_IDX]);
510+
return err;
511+
}
512+
396513
const int32_t position = shell_strtol(argv[ARG_IDX_PARAM], 10, &err);
397514

398515
if (err < 0) {
@@ -420,10 +537,18 @@ static int cmd_stepper_move_to(const struct shell *sh, size_t argc, char **argv)
420537
static int cmd_stepper_run(const struct shell *sh, size_t argc, char **argv)
421538
{
422539
const struct device *dev;
423-
int err = -EINVAL;
424-
const uint8_t stepper_index = shell_strtol(argv[ARG_IDX_DEV_IDX], 10, &err);
540+
uint8_t stepper_index;
541+
int err;
542+
543+
err = get_stepper_index(argv, &stepper_index);
544+
if (err < 0) {
545+
shell_error(sh, "Invalid stepper index: %s", argv[ARG_IDX_DEV_IDX]);
546+
return err;
547+
}
548+
425549
enum stepper_direction direction = STEPPER_DIRECTION_POSITIVE;
426550

551+
err = -EINVAL;
427552
for (int i = 0; i < ARRAY_SIZE(stepper_direction_map); i++) {
428553
if (strcmp(argv[ARG_IDX_PARAM], stepper_direction_map[i].name) == 0) {
429554
direction = stepper_direction_map[i].direction;
@@ -458,10 +583,16 @@ static int cmd_stepper_run(const struct shell *sh, size_t argc, char **argv)
458583
static int cmd_stepper_control_info(const struct shell *sh, size_t argc, char **argv)
459584
{
460585
const struct device *dev;
586+
uint8_t stepper_index;
461587
int err;
462588
bool is_moving;
463589
int32_t actual_position;
464-
const uint8_t stepper_index = shell_strtol(argv[ARG_IDX_DEV_IDX], 10, &err);
590+
591+
err = get_stepper_index(argv, &stepper_index);
592+
if (err < 0) {
593+
shell_error(sh, "Invalid stepper index: %s", argv[ARG_IDX_DEV_IDX]);
594+
return err;
595+
}
465596

466597
err = parse_device_arg(sh, argv, &dev);
467598
if (err < 0) {
@@ -492,8 +623,6 @@ static int cmd_stepper_info(const struct shell *sh, size_t argc, char **argv)
492623
{
493624
const struct device *dev;
494625
int err;
495-
bool is_moving;
496-
int32_t actual_position;
497626
enum stepper_micro_step_resolution micro_step_res;
498627

499628
err = parse_device_arg(sh, argv, &dev);
@@ -522,22 +651,22 @@ SHELL_STATIC_SUBCMD_SET_CREATE(
522651
"<device> <resolution>", cmd_stepper_set_micro_step_res, 3, 0),
523652
SHELL_CMD_ARG(get_micro_step_res, &dsub_pos_stepper_motor_name, "<device>",
524653
cmd_stepper_get_micro_step_res, 2, 0),
525-
SHELL_CMD_ARG(set_reference_position, &dsub_pos_stepper_motor_name, "<device> <position>",
526-
cmd_stepper_set_reference_position, 4, 0),
527-
SHELL_CMD_ARG(get_actual_position, &dsub_pos_stepper_motor_name, "<device>",
654+
SHELL_CMD_ARG(set_reference_position, &dsub_pos_stepper_controller_name,
655+
"<device> <position>", cmd_stepper_set_reference_position, 4, 0),
656+
SHELL_CMD_ARG(get_actual_position, &dsub_pos_stepper_controller_name, "<device>",
528657
cmd_stepper_get_actual_position, 3, 0),
529-
SHELL_CMD_ARG(set_microstep_interval, &dsub_pos_stepper_motor_name,
658+
SHELL_CMD_ARG(set_microstep_interval, &dsub_pos_stepper_controller_name,
530659
"<device> <microstep_interval_ns>", cmd_stepper_set_microstep_interval, 4, 0),
531-
SHELL_CMD_ARG(move_by, &dsub_pos_stepper_motor_name, "<device> <microsteps>",
660+
SHELL_CMD_ARG(move_by, &dsub_pos_stepper_controller_name, "<device> <microsteps>",
532661
cmd_stepper_move_by, 4, 0),
533-
SHELL_CMD_ARG(move_to, &dsub_pos_stepper_motor_name, "<device> <microsteps>",
662+
SHELL_CMD_ARG(move_to, &dsub_pos_stepper_controller_name, "<device> <microsteps>",
534663
cmd_stepper_move_to, 4, 0),
535-
SHELL_CMD_ARG(run, &dsub_pos_stepper_motor_name_dir, "<device> <direction>",
664+
SHELL_CMD_ARG(run, &dsub_pos_stepper_controller_name_dir, "<device> <direction>",
536665
cmd_stepper_run, 4, 0),
537-
SHELL_CMD_ARG(stop, &dsub_pos_stepper_motor_name, "<device>", cmd_stepper_stop, 3, 0),
538-
SHELL_CMD_ARG(info, &dsub_pos_stepper_motor_name, "<device>", cmd_stepper_control_info, 3,
539-
0),
540-
SHELL_CMD_ARG(info, &dsub_pos_stepper_motor_name, "<device>", cmd_stepper_info, 3, 0),
666+
SHELL_CMD_ARG(stop, &dsub_pos_stepper_controller_name, "<device>", cmd_stepper_stop, 3, 0),
667+
SHELL_CMD_ARG(control_info, &dsub_pos_stepper_controller_name, "<device>",
668+
cmd_stepper_control_info, 3, 0),
669+
SHELL_CMD_ARG(info, &dsub_pos_stepper_motor_name, "<device>", cmd_stepper_info, 2, 0),
541670
SHELL_SUBCMD_SET_END);
542671

543672
SHELL_CMD_REGISTER(stepper, &stepper_cmds, "Stepper motor commands", NULL);

0 commit comments

Comments
 (0)