@@ -14,22 +14,6 @@ static inline int step_dir_stepper_perform_step(const struct device *dev)
14
14
struct step_dir_stepper_common_data * data = dev -> data ;
15
15
int ret ;
16
16
17
- switch (data -> direction ) {
18
- case STEPPER_DIRECTION_POSITIVE :
19
- ret = gpio_pin_set_dt (& config -> dir_pin , 1 ^ config -> invert_direction );
20
- break ;
21
- case STEPPER_DIRECTION_NEGATIVE :
22
- ret = gpio_pin_set_dt (& config -> dir_pin , 0 ^ config -> invert_direction );
23
- break ;
24
- default :
25
- LOG_ERR ("Unsupported direction: %d" , data -> direction );
26
- return - ENOTSUP ;
27
- }
28
- if (ret < 0 ) {
29
- LOG_ERR ("Failed to set direction: %d" , ret );
30
- return ret ;
31
- }
32
-
33
17
ret = gpio_pin_toggle_dt (& config -> step_pin );
34
18
if (ret < 0 ) {
35
19
LOG_ERR ("Failed to toggle step pin: %d" , ret );
@@ -53,6 +37,31 @@ static inline int step_dir_stepper_perform_step(const struct device *dev)
53
37
return 0 ;
54
38
}
55
39
40
+ static inline int update_dir_pin (const struct device * dev )
41
+ {
42
+ const struct step_dir_stepper_common_config * config = dev -> config ;
43
+ struct step_dir_stepper_common_data * data = dev -> data ;
44
+ int ret ;
45
+
46
+ switch (data -> direction ) {
47
+ case STEPPER_DIRECTION_POSITIVE :
48
+ ret = gpio_pin_set_dt (& config -> dir_pin , 1 ^ config -> invert_direction );
49
+ break ;
50
+ case STEPPER_DIRECTION_NEGATIVE :
51
+ ret = gpio_pin_set_dt (& config -> dir_pin , 0 ^ config -> invert_direction );
52
+ break ;
53
+ default :
54
+ LOG_ERR ("Unsupported direction: %d" , data -> direction );
55
+ return - ENOTSUP ;
56
+ }
57
+ if (ret < 0 ) {
58
+ LOG_ERR ("Failed to set direction: %d" , ret );
59
+ return ret ;
60
+ }
61
+
62
+ return ret ;
63
+ }
64
+
56
65
void stepper_trigger_callback (const struct device * dev , enum stepper_event event )
57
66
{
58
67
struct step_dir_stepper_common_data * data = dev -> data ;
@@ -227,6 +236,7 @@ int step_dir_stepper_common_move_by(const struct device *dev, const int32_t micr
227
236
{
228
237
struct step_dir_stepper_common_data * data = dev -> data ;
229
238
const struct step_dir_stepper_common_config * config = dev -> config ;
239
+ int ret ;
230
240
231
241
if (data -> microstep_interval_ns == 0 ) {
232
242
LOG_ERR ("Step interval not set or invalid step interval set" );
@@ -236,12 +246,16 @@ int step_dir_stepper_common_move_by(const struct device *dev, const int32_t micr
236
246
K_SPINLOCK (& data -> lock ) {
237
247
data -> run_mode = STEPPER_RUN_MODE_POSITION ;
238
248
data -> step_count = micro_steps ;
239
- config -> timing_source -> update (dev , data -> microstep_interval_ns );
240
249
update_direction_from_step_count (dev );
250
+ ret = update_dir_pin (dev );
251
+ if (ret < 0 ) {
252
+ K_SPINLOCK_BREAK ;
253
+ }
254
+ config -> timing_source -> update (dev , data -> microstep_interval_ns );
241
255
config -> timing_source -> start (dev );
242
256
}
243
257
244
- return 0 ;
258
+ return ret ;
245
259
}
246
260
247
261
int step_dir_stepper_common_set_microstep_interval (const struct device * dev ,
@@ -310,15 +324,20 @@ int step_dir_stepper_common_run(const struct device *dev, const enum stepper_dir
310
324
{
311
325
struct step_dir_stepper_common_data * data = dev -> data ;
312
326
const struct step_dir_stepper_common_config * config = dev -> config ;
327
+ int ret ;
313
328
314
329
K_SPINLOCK (& data -> lock ) {
315
330
data -> run_mode = STEPPER_RUN_MODE_VELOCITY ;
316
331
data -> direction = direction ;
332
+ ret = update_dir_pin (dev );
333
+ if (ret < 0 ) {
334
+ K_SPINLOCK_BREAK ;
335
+ }
317
336
config -> timing_source -> update (dev , data -> microstep_interval_ns );
318
337
config -> timing_source -> start (dev );
319
338
}
320
339
321
- return 0 ;
340
+ return ret ;
322
341
}
323
342
324
343
int step_dir_stepper_common_stop (const struct device * dev )
0 commit comments