@@ -29,6 +29,13 @@ struct stepper_direction_map {
29
29
enum stepper_direction direction ;
30
30
};
31
31
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
+
32
39
#define STEPPER_DIRECTION_MAP_ENTRY (_name , _dir ) \
33
40
{ \
34
41
.name = _name, \
@@ -41,6 +48,12 @@ struct stepper_direction_map {
41
48
.microstep = _microstep, \
42
49
}
43
50
51
+ #define STEPPER_CONTROL_IDX_MAP_ENTRY (_name , _idx ) \
52
+ { \
53
+ .name = _name, \
54
+ .stepper_idx = _idx, \
55
+ }
56
+
44
57
static void print_callback (const struct device * dev , const uint8_t stepper_idx ,
45
58
const enum stepper_event event , void * user_data )
46
59
{
@@ -71,7 +84,12 @@ static void print_callback(const struct device *dev, const uint8_t stepper_idx,
71
84
}
72
85
}
73
86
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 )
75
93
{
76
94
return DEVICE_API_IS (stepper , dev );
77
95
}
@@ -81,6 +99,12 @@ static const struct stepper_direction_map stepper_direction_map[] = {
81
99
STEPPER_DIRECTION_MAP_ENTRY ("negative" , STEPPER_DIRECTION_NEGATIVE ),
82
100
};
83
101
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
+
84
108
static const struct stepper_microstep_map stepper_microstep_map [] = {
85
109
STEPPER_MICROSTEP_MAP ("1" , STEPPER_MICRO_STEP_1 ),
86
110
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)
107
131
108
132
SHELL_DYNAMIC_CMD_CREATE (dsub_stepper_direction , cmd_stepper_direction );
109
133
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
+
110
162
static void cmd_stepper_microstep (size_t idx , struct shell_static_entry * entry )
111
163
{
112
164
if (idx < ARRAY_SIZE (stepper_microstep_map )) {
@@ -123,7 +175,7 @@ SHELL_DYNAMIC_CMD_CREATE(dsub_stepper_microstep, cmd_stepper_microstep);
123
175
124
176
static void cmd_pos_stepper_motor_name (size_t idx , struct shell_static_entry * entry )
125
177
{
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 );
127
179
128
180
entry -> syntax = (dev != NULL ) ? dev -> name : NULL ;
129
181
entry -> handler = NULL ;
@@ -133,9 +185,21 @@ static void cmd_pos_stepper_motor_name(size_t idx, struct shell_static_entry *en
133
185
134
186
SHELL_DYNAMIC_CMD_CREATE (dsub_pos_stepper_motor_name , cmd_pos_stepper_motor_name );
135
187
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 )
137
189
{
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 );
139
203
140
204
if (dev != NULL ) {
141
205
entry -> syntax = dev -> name ;
@@ -144,14 +208,14 @@ static void cmd_pos_stepper_motor_name_dir(size_t idx, struct shell_static_entry
144
208
}
145
209
entry -> handler = NULL ;
146
210
entry -> help = "List Devices" ;
147
- entry -> subcmd = & dsub_stepper_direction ;
211
+ entry -> subcmd = & dsub_stepper_idx_dir ;
148
212
}
149
213
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 );
151
215
152
216
static void cmd_pos_stepper_motor_name_microstep (size_t idx , struct shell_static_entry * entry )
153
217
{
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 );
155
219
156
220
if (dev != NULL ) {
157
221
entry -> syntax = dev -> name ;
@@ -212,23 +276,40 @@ static int cmd_stepper_disable(const struct shell *sh, size_t argc, char **argv)
212
276
return err ;
213
277
}
214
278
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
+ }
215
289
static int cmd_stepper_stop (const struct shell * sh , size_t argc , char * * argv )
216
290
{
217
291
const struct device * dev ;
292
+ uint8_t stepper_index ;
218
293
int err = 0 ;
219
294
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
+
220
301
err = parse_device_arg (sh , argv , & dev );
221
302
if (err < 0 ) {
222
303
return err ;
223
304
}
224
305
225
- err = stepper_stop (dev );
306
+ err = stepper_stop (dev , stepper_index );
226
307
if (err ) {
227
308
shell_error (sh , "Error: %d" , err );
228
309
return err ;
229
310
}
230
311
231
- err = stepper_set_event_callback (dev , print_callback , (void * )sh );
312
+ err = stepper_set_event_callback (dev , stepper_index , print_callback , (void * )sh );
232
313
if (err != 0 ) {
233
314
shell_error (sh , "Failed to set callback: %d" , err );
234
315
}
@@ -239,9 +320,15 @@ static int cmd_stepper_stop(const struct shell *sh, size_t argc, char **argv)
239
320
static int cmd_stepper_move_by (const struct shell * sh , size_t argc , char * * argv )
240
321
{
241
322
const struct device * dev ;
323
+ uint8_t stepper_index ;
242
324
int err = 0 ;
243
325
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
+
245
332
int32_t micro_steps = shell_strtol (argv [ARG_IDX_PARAM ], 10 , & err );
246
333
247
334
if (err < 0 ) {
@@ -269,8 +356,15 @@ static int cmd_stepper_move_by(const struct shell *sh, size_t argc, char **argv)
269
356
static int cmd_stepper_set_microstep_interval (const struct shell * sh , size_t argc , char * * argv )
270
357
{
271
358
const struct device * dev ;
359
+ uint8_t stepper_index ;
272
360
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
+
274
368
uint64_t step_interval = shell_strtoull (argv [ARG_IDX_PARAM ], 10 , & err );
275
369
276
370
if (err < 0 ) {
@@ -297,14 +391,16 @@ static int cmd_stepper_set_micro_step_res(const struct shell *sh, size_t argc, c
297
391
int err = - EINVAL ;
298
392
299
393
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 ) {
301
396
resolution = stepper_microstep_map [i ].microstep ;
302
397
err = 0 ;
303
398
break ;
304
399
}
305
400
}
306
401
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 ]);
308
404
return err ;
309
405
}
310
406
@@ -313,7 +409,7 @@ static int cmd_stepper_set_micro_step_res(const struct shell *sh, size_t argc, c
313
409
return err ;
314
410
}
315
411
316
- err = stepper_drv_set_micro_stepper_res (dev , resolution );
412
+ err = stepper_drv_set_micro_step_res (dev , resolution );
317
413
if (err ) {
318
414
shell_error (sh , "Error: %d" , err );
319
415
}
@@ -345,8 +441,15 @@ static int cmd_stepper_get_micro_step_res(const struct shell *sh, size_t argc, c
345
441
static int cmd_stepper_set_reference_position (const struct shell * sh , size_t argc , char * * argv )
346
442
{
347
443
const struct device * dev ;
444
+ uint8_t stepper_index ;
348
445
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
+
350
453
int32_t position = shell_strtol (argv [ARG_IDX_PARAM ], 10 , & err );
351
454
352
455
if (err < 0 ) {
@@ -369,8 +472,15 @@ static int cmd_stepper_set_reference_position(const struct shell *sh, size_t arg
369
472
static int cmd_stepper_get_actual_position (const struct shell * sh , size_t argc , char * * argv )
370
473
{
371
474
const struct device * dev ;
475
+ uint8_t stepper_index ;
372
476
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
+
374
484
int32_t actual_position ;
375
485
376
486
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,
391
501
static int cmd_stepper_move_to (const struct shell * sh , size_t argc , char * * argv )
392
502
{
393
503
const struct device * dev ;
504
+ uint8_t stepper_index ;
394
505
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
+
396
513
const int32_t position = shell_strtol (argv [ARG_IDX_PARAM ], 10 , & err );
397
514
398
515
if (err < 0 ) {
@@ -420,10 +537,18 @@ static int cmd_stepper_move_to(const struct shell *sh, size_t argc, char **argv)
420
537
static int cmd_stepper_run (const struct shell * sh , size_t argc , char * * argv )
421
538
{
422
539
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
+
425
549
enum stepper_direction direction = STEPPER_DIRECTION_POSITIVE ;
426
550
551
+ err = - EINVAL ;
427
552
for (int i = 0 ; i < ARRAY_SIZE (stepper_direction_map ); i ++ ) {
428
553
if (strcmp (argv [ARG_IDX_PARAM ], stepper_direction_map [i ].name ) == 0 ) {
429
554
direction = stepper_direction_map [i ].direction ;
@@ -458,10 +583,16 @@ static int cmd_stepper_run(const struct shell *sh, size_t argc, char **argv)
458
583
static int cmd_stepper_control_info (const struct shell * sh , size_t argc , char * * argv )
459
584
{
460
585
const struct device * dev ;
586
+ uint8_t stepper_index ;
461
587
int err ;
462
588
bool is_moving ;
463
589
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
+ }
465
596
466
597
err = parse_device_arg (sh , argv , & dev );
467
598
if (err < 0 ) {
@@ -492,8 +623,6 @@ static int cmd_stepper_info(const struct shell *sh, size_t argc, char **argv)
492
623
{
493
624
const struct device * dev ;
494
625
int err ;
495
- bool is_moving ;
496
- int32_t actual_position ;
497
626
enum stepper_micro_step_resolution micro_step_res ;
498
627
499
628
err = parse_device_arg (sh , argv , & dev );
@@ -522,22 +651,22 @@ SHELL_STATIC_SUBCMD_SET_CREATE(
522
651
"<device> <resolution>" , cmd_stepper_set_micro_step_res , 3 , 0 ),
523
652
SHELL_CMD_ARG (get_micro_step_res , & dsub_pos_stepper_motor_name , "<device>" ,
524
653
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>" ,
528
657
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 ,
530
659
"<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>" ,
532
661
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>" ,
534
663
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>" ,
536
665
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 ),
541
670
SHELL_SUBCMD_SET_END );
542
671
543
672
SHELL_CMD_REGISTER (stepper , & stepper_cmds , "Stepper motor commands" , NULL );
0 commit comments