Skip to content

Commit 03c76c2

Browse files
committed
drivers: stepper: refactor stepper move_to to use move_by
Refactored stepper move_to calls to use the move_by function. Now, relative movement required is calculated and then redirected to move_by. Signed-off-by: Dipak Shetty <[email protected]>
1 parent 781011b commit 03c76c2

File tree

3 files changed

+12
-48
lines changed

3 files changed

+12
-48
lines changed

drivers/stepper/adi_tmc/adi_tmc50xx_stepper_controller.c

+5-22
Original file line numberDiff line numberDiff line change
@@ -463,32 +463,15 @@ static int tmc50xx_stepper_move_to(const struct device *dev, const int32_t micro
463463
struct tmc50xx_stepper_data *data = dev->data;
464464
int err;
465465

466-
if (config->is_sg_enabled) {
467-
stallguard_enable(dev, false);
468-
}
469-
470-
err = tmc50xx_write(config->controller, TMC50XX_RAMPMODE(config->index),
471-
TMC5XXX_RAMPMODE_POSITIONING_MODE);
472-
if (err != 0) {
473-
return -EIO;
474-
}
475-
err = tmc50xx_write(config->controller, TMC50XX_XTARGET(config->index), micro_steps);
466+
// Get current position to calculate relative movement
467+
err = stepper_get_actual_position(dev, &position);
476468
if (err != 0) {
477469
return -EIO;
478470
}
479471

480-
if (config->is_sg_enabled) {
481-
k_work_reschedule(&data->stallguard_dwork,
482-
K_MSEC(config->sg_velocity_check_interval_ms));
483-
}
484-
#ifdef CONFIG_STEPPER_ADI_TMC50XX_RAMPSTAT_POLL
485-
if (data->callback) {
486-
k_work_reschedule(
487-
&data->rampstat_callback_dwork,
488-
K_MSEC(CONFIG_STEPPER_ADI_TMC50XX_RAMPSTAT_POLL_INTERVAL_IN_MSEC));
489-
}
490-
#endif
491-
return 0;
472+
// Calculate steps needed to reach target position
473+
steps_to_move = micro_steps - position;
474+
return tmc50xx_stepper_move_by(dev, steps_to_move);
492475
}
493476

494477
static int tmc50xx_stepper_run(const struct device *dev, const enum stepper_direction direction)

drivers/stepper/gpio_stepper_controller.c

+3-14
Original file line numberDiff line numberDiff line change
@@ -223,23 +223,12 @@ static int gpio_stepper_get_actual_position(const struct device *dev, int32_t *p
223223
static int gpio_stepper_move_to(const struct device *dev, int32_t micro_steps)
224224
{
225225
struct gpio_stepper_data *data = dev->data;
226+
int32_t steps_to_move;
226227

227-
if (!data->is_enabled) {
228-
LOG_ERR("Stepper motor is not enabled");
229-
return -ECANCELED;
230-
}
231-
232-
if (data->delay_in_ns == 0) {
233-
LOG_ERR("Step interval not set or invalid step interval set");
234-
return -EINVAL;
235-
}
236228
K_SPINLOCK(&data->lock) {
237-
data->run_mode = STEPPER_RUN_MODE_POSITION;
238-
data->step_count = micro_steps - data->actual_position;
239-
update_direction_from_step_count(dev);
240-
(void)k_work_reschedule(&data->stepper_dwork, K_NO_WAIT);
229+
steps_to_move = micro_steps - data->actual_position;
241230
}
242-
return 0;
231+
return gpio_stepper_move_by(dev, steps_to_move);
243232
}
244233

245234
static int gpio_stepper_is_moving(const struct device *dev, bool *is_moving)

drivers/stepper/step_dir/step_dir_stepper_common.c

+4-12
Original file line numberDiff line numberDiff line change
@@ -288,22 +288,14 @@ int step_dir_stepper_common_get_actual_position(const struct device *dev, int32_
288288
int step_dir_stepper_common_move_to(const struct device *dev, const int32_t value)
289289
{
290290
struct step_dir_stepper_common_data *data = dev->data;
291-
const struct step_dir_stepper_common_config *config = dev->config;
292-
293-
if (data->microstep_interval_ns == 0) {
294-
LOG_ERR("Step interval not set or invalid step interval set");
295-
return -EINVAL;
296-
}
291+
int32_t steps_to_move;
297292

293+
/* Calculate the relative movement required */
298294
K_SPINLOCK(&data->lock) {
299-
data->run_mode = STEPPER_RUN_MODE_POSITION;
300-
data->step_count = value - data->actual_position;
301-
config->timing_source->update(dev, data->microstep_interval_ns);
302-
update_direction_from_step_count(dev);
303-
config->timing_source->start(dev);
295+
steps_to_move = value - data->actual_position;
304296
}
305297

306-
return 0;
298+
return step_dir_stepper_common_move_by(dev, steps_to_move);
307299
}
308300

309301
int step_dir_stepper_common_is_moving(const struct device *dev, bool *is_moving)

0 commit comments

Comments
 (0)