@@ -37,7 +37,7 @@ struct gpio_stepper_data {
3737 uint8_t coil_charge ;
3838 struct k_work_delayable stepper_dwork ;
3939 int32_t actual_position ;
40- uint32_t delay_in_us ;
40+ uint64_t delay_in_ns ;
4141 int32_t step_count ;
4242 bool is_enabled ;
4343 stepper_event_callback_t callback ;
@@ -111,10 +111,10 @@ static void update_remaining_steps(struct gpio_stepper_data *data)
111111{
112112 if (data -> step_count > 0 ) {
113113 data -> step_count -- ;
114- (void )k_work_reschedule (& data -> stepper_dwork , K_USEC (data -> delay_in_us ));
114+ (void )k_work_reschedule (& data -> stepper_dwork , K_NSEC (data -> delay_in_ns ));
115115 } else if (data -> step_count < 0 ) {
116116 data -> step_count ++ ;
117- (void )k_work_reschedule (& data -> stepper_dwork , K_USEC (data -> delay_in_us ));
117+ (void )k_work_reschedule (& data -> stepper_dwork , K_NSEC (data -> delay_in_ns ));
118118 } else {
119119 if (!data -> callback ) {
120120 LOG_WRN_ONCE ("No callback set" );
@@ -154,7 +154,7 @@ static void velocity_mode_task(const struct device *dev)
154154
155155 (void )stepper_motor_set_coil_charge (dev );
156156 update_coil_charge (dev );
157- (void )k_work_reschedule (& data -> stepper_dwork , K_USEC (data -> delay_in_us ));
157+ (void )k_work_reschedule (& data -> stepper_dwork , K_NSEC (data -> delay_in_ns ));
158158}
159159
160160static void stepper_work_step_handler (struct k_work * work )
@@ -187,8 +187,8 @@ static int gpio_stepper_move_by(const struct device *dev, int32_t micro_steps)
187187 return - ECANCELED ;
188188 }
189189
190- if (data -> delay_in_us == 0 ) {
191- LOG_ERR ("Velocity not set or invalid velocity set" );
190+ if (data -> delay_in_ns == 0 ) {
191+ LOG_ERR ("Step interval not set or invalid step interval set" );
192192 return - EINVAL ;
193193 }
194194 K_SPINLOCK (& data -> lock ) {
@@ -229,8 +229,8 @@ static int gpio_stepper_move_to(const struct device *dev, int32_t micro_steps)
229229 return - ECANCELED ;
230230 }
231231
232- if (data -> delay_in_us == 0 ) {
233- LOG_ERR ("Velocity not set or invalid velocity set" );
232+ if (data -> delay_in_ns == 0 ) {
233+ LOG_ERR ("Step interval not set or invalid step interval set" );
234234 return - EINVAL ;
235235 }
236236 K_SPINLOCK (& data -> lock ) {
@@ -251,29 +251,24 @@ static int gpio_stepper_is_moving(const struct device *dev, bool *is_moving)
251251 return 0 ;
252252}
253253
254- static int gpio_stepper_set_max_velocity (const struct device * dev , uint32_t velocity )
254+ static int gpio_stepper_set_microstep_interval (const struct device * dev ,
255+ uint64_t microstep_interval_ns )
255256{
256257 struct gpio_stepper_data * data = dev -> data ;
257258
258- if (velocity == 0 ) {
259- LOG_ERR ("Velocity cannot be zero" );
260- return - EINVAL ;
261- }
262-
263- if (velocity > USEC_PER_SEC ) {
264- LOG_ERR ("Velocity cannot be greater than %d micro_steps_per_second" , USEC_PER_SEC );
259+ if (microstep_interval_ns == 0 ) {
260+ LOG_ERR ("Step interval is invalid." );
265261 return - EINVAL ;
266262 }
267263
268264 K_SPINLOCK (& data -> lock ) {
269- data -> delay_in_us = USEC_PER_SEC / velocity ;
265+ data -> delay_in_ns = microstep_interval_ns ;
270266 }
271- LOG_DBG ("Setting Motor Speed to %d " , velocity );
267+ LOG_DBG ("Setting Motor step interval to %llu " , microstep_interval_ns );
272268 return 0 ;
273269}
274270
275- static int gpio_stepper_run (const struct device * dev , const enum stepper_direction direction ,
276- const uint32_t velocity )
271+ static int gpio_stepper_run (const struct device * dev , const enum stepper_direction direction )
277272{
278273 struct gpio_stepper_data * data = dev -> data ;
279274
@@ -285,12 +280,7 @@ static int gpio_stepper_run(const struct device *dev, const enum stepper_directi
285280 K_SPINLOCK (& data -> lock ) {
286281 data -> run_mode = STEPPER_RUN_MODE_VELOCITY ;
287282 data -> direction = direction ;
288- if (velocity != 0 ) {
289- data -> delay_in_us = USEC_PER_SEC / velocity ;
290- (void )k_work_reschedule (& data -> stepper_dwork , K_NO_WAIT );
291- } else {
292- (void )k_work_cancel_delayable (& data -> stepper_dwork );
293- }
283+ (void )k_work_reschedule (& data -> stepper_dwork , K_NO_WAIT );
294284 }
295285 return 0 ;
296286}
@@ -377,7 +367,7 @@ static DEVICE_API(stepper, gpio_stepper_api) = {
377367 .set_reference_position = gpio_stepper_set_reference_position ,
378368 .get_actual_position = gpio_stepper_get_actual_position ,
379369 .move_to = gpio_stepper_move_to ,
380- .set_max_velocity = gpio_stepper_set_max_velocity ,
370+ .set_microstep_interval = gpio_stepper_set_microstep_interval ,
381371 .run = gpio_stepper_run ,
382372 .set_micro_step_res = gpio_stepper_set_micro_step_res ,
383373 .get_micro_step_res = gpio_stepper_get_micro_step_res ,
0 commit comments