Skip to content

Commit ebf6203

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

File tree

3 files changed

+8
-47
lines changed

3 files changed

+8
-47
lines changed

drivers/stepper/adi_tmc/adi_tmc50xx_stepper_controller.c

+1-21
Original file line numberDiff line numberDiff line change
@@ -331,30 +331,10 @@ static int tmc50xx_stepper_move_by(const struct device *dev, const int32_t micro
331331
}
332332
int32_t target_position = position + micro_steps;
333333

334-
err = tmc50xx_write(config->controller, TMC50XX_RAMPMODE(config->index),
335-
TMC5XXX_RAMPMODE_POSITIONING_MODE);
336-
if (err != 0) {
337-
return -EIO;
338-
}
339334
LOG_DBG("Stepper motor controller %s moved to %d by steps: %d", dev->name, target_position,
340335
micro_steps);
341-
err = tmc50xx_write(config->controller, TMC50XX_XTARGET(config->index), target_position);
342-
if (err != 0) {
343-
return -EIO;
344-
}
345336

346-
if (config->is_sg_enabled) {
347-
k_work_reschedule(&data->stallguard_dwork,
348-
K_MSEC(config->sg_velocity_check_interval_ms));
349-
}
350-
#ifdef CONFIG_STEPPER_ADI_TMC50XX_RAMPSTAT_POLL
351-
if (data->callback) {
352-
k_work_reschedule(
353-
&data->rampstat_callback_dwork,
354-
K_MSEC(CONFIG_STEPPER_ADI_TMC50XX_RAMPSTAT_POLL_INTERVAL_IN_MSEC));
355-
}
356-
#endif
357-
return 0;
337+
return tmc50xx_stepper_move_to(dev, target_position);
358338
}
359339

360340
int tmc50xx_stepper_set_max_velocity(const struct device *dev, uint32_t velocity)

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)