@@ -14,22 +14,6 @@ static inline int step_dir_stepper_perform_step(const struct device *dev)
1414 struct step_dir_stepper_common_data * data = dev -> data ;
1515 int ret ;
1616
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-
3317 ret = gpio_pin_toggle_dt (& config -> step_pin );
3418 if (ret < 0 ) {
3519 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)
5337 return 0 ;
5438}
5539
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+
5665void stepper_trigger_callback (const struct device * dev , enum stepper_event event )
5766{
5867 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
227236{
228237 struct step_dir_stepper_common_data * data = dev -> data ;
229238 const struct step_dir_stepper_common_config * config = dev -> config ;
239+ int ret ;
230240
231241 if (data -> microstep_interval_ns == 0 ) {
232242 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
236246 K_SPINLOCK (& data -> lock ) {
237247 data -> run_mode = STEPPER_RUN_MODE_POSITION ;
238248 data -> step_count = micro_steps ;
239- config -> timing_source -> update (dev , data -> microstep_interval_ns );
240249 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 );
241255 config -> timing_source -> start (dev );
242256 }
243257
244- return 0 ;
258+ return ret ;
245259}
246260
247261int 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
310324{
311325 struct step_dir_stepper_common_data * data = dev -> data ;
312326 const struct step_dir_stepper_common_config * config = dev -> config ;
327+ int ret ;
313328
314329 K_SPINLOCK (& data -> lock ) {
315330 data -> run_mode = STEPPER_RUN_MODE_VELOCITY ;
316331 data -> direction = direction ;
332+ ret = update_dir_pin (dev );
333+ if (ret < 0 ) {
334+ K_SPINLOCK_BREAK ;
335+ }
317336 config -> timing_source -> update (dev , data -> microstep_interval_ns );
318337 config -> timing_source -> start (dev );
319338 }
320339
321- return 0 ;
340+ return ret ;
322341}
323342
324343int step_dir_stepper_common_stop (const struct device * dev )
0 commit comments