Skip to content

Commit 9675be9

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 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 9675be9

File tree

3 files changed

+24
-74
lines changed

3 files changed

+24
-74
lines changed

drivers/stepper/adi_tmc/adi_tmc50xx_stepper_controller.c

+17-48
Original file line numberDiff line numberDiff line change
@@ -310,53 +310,6 @@ static int tmc50xx_stepper_is_moving(const struct device *dev, bool *is_moving)
310310
return 0;
311311
}
312312

313-
static int tmc50xx_stepper_move_by(const struct device *dev, const int32_t micro_steps)
314-
{
315-
const struct tmc50xx_stepper_config *config = dev->config;
316-
struct tmc50xx_stepper_data *data = dev->data;
317-
int err;
318-
319-
if (config->is_sg_enabled) {
320-
err = stallguard_enable(dev, false);
321-
if (err != 0) {
322-
return -EIO;
323-
}
324-
}
325-
326-
int32_t position;
327-
328-
err = stepper_get_actual_position(dev, &position);
329-
if (err != 0) {
330-
return -EIO;
331-
}
332-
int32_t target_position = position + micro_steps;
333-
334-
err = tmc50xx_write(config->controller, TMC50XX_RAMPMODE(config->index),
335-
TMC5XXX_RAMPMODE_POSITIONING_MODE);
336-
if (err != 0) {
337-
return -EIO;
338-
}
339-
LOG_DBG("Stepper motor controller %s moved to %d by steps: %d", dev->name, target_position,
340-
micro_steps);
341-
err = tmc50xx_write(config->controller, TMC50XX_XTARGET(config->index), target_position);
342-
if (err != 0) {
343-
return -EIO;
344-
}
345-
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;
358-
}
359-
360313
int tmc50xx_stepper_set_max_velocity(const struct device *dev, uint32_t velocity)
361314
{
362315
const struct tmc50xx_stepper_config *config = dev->config;
@@ -458,7 +411,7 @@ static int tmc50xx_stepper_get_actual_position(const struct device *dev, int32_t
458411

459412
static int tmc50xx_stepper_move_to(const struct device *dev, const int32_t micro_steps)
460413
{
461-
LOG_DBG("Stepper motor controller %s set target position to %d", dev->name, micro_steps);
414+
LOG_DBG("%s set target position to %d", dev->name, micro_steps);
462415
const struct tmc50xx_stepper_config *config = dev->config;
463416
struct tmc50xx_stepper_data *data = dev->data;
464417
int err;
@@ -491,6 +444,22 @@ static int tmc50xx_stepper_move_to(const struct device *dev, const int32_t micro
491444
return 0;
492445
}
493446

447+
static int tmc50xx_stepper_move_by(const struct device *dev, const int32_t micro_steps)
448+
{
449+
int err;
450+
int32_t position;
451+
452+
err = stepper_get_actual_position(dev, &position);
453+
if (err != 0) {
454+
return -EIO;
455+
}
456+
int32_t target_position = position + micro_steps;
457+
458+
LOG_DBG("%s moved to %d by steps: %d", dev->name, target_position, micro_steps);
459+
460+
return tmc50xx_stepper_move_to(dev, target_position);
461+
}
462+
494463
static int tmc50xx_stepper_run(const struct device *dev, const enum stepper_direction direction)
495464
{
496465
LOG_DBG("Stepper motor controller %s run", dev->name);

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)