Skip to content

Commit 8f6b0b2

Browse files
dipakgmxkartben
authored andcommitted
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 efdca63 commit 8f6b0b2

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
@@ -338,53 +338,6 @@ static int tmc50xx_stepper_is_moving(const struct device *dev, bool *is_moving)
338338
return 0;
339339
}
340340

341-
static int tmc50xx_stepper_move_by(const struct device *dev, const int32_t micro_steps)
342-
{
343-
const struct tmc50xx_stepper_config *config = dev->config;
344-
struct tmc50xx_stepper_data *data = dev->data;
345-
int err;
346-
347-
if (config->is_sg_enabled) {
348-
err = stallguard_enable(dev, false);
349-
if (err != 0) {
350-
return -EIO;
351-
}
352-
}
353-
354-
int32_t position;
355-
356-
err = stepper_get_actual_position(dev, &position);
357-
if (err != 0) {
358-
return -EIO;
359-
}
360-
int32_t target_position = position + micro_steps;
361-
362-
err = tmc50xx_write(config->controller, TMC50XX_RAMPMODE(config->index),
363-
TMC5XXX_RAMPMODE_POSITIONING_MODE);
364-
if (err != 0) {
365-
return -EIO;
366-
}
367-
LOG_DBG("Stepper motor controller %s moved to %d by steps: %d", dev->name, target_position,
368-
micro_steps);
369-
err = tmc50xx_write(config->controller, TMC50XX_XTARGET(config->index), target_position);
370-
if (err != 0) {
371-
return -EIO;
372-
}
373-
374-
if (config->is_sg_enabled) {
375-
k_work_reschedule(&data->stallguard_dwork,
376-
K_MSEC(config->sg_velocity_check_interval_ms));
377-
}
378-
#ifdef CONFIG_STEPPER_ADI_TMC50XX_RAMPSTAT_POLL
379-
if (data->callback) {
380-
k_work_reschedule(
381-
&data->rampstat_callback_dwork,
382-
K_MSEC(CONFIG_STEPPER_ADI_TMC50XX_RAMPSTAT_POLL_INTERVAL_IN_MSEC));
383-
}
384-
#endif
385-
return 0;
386-
}
387-
388341
int tmc50xx_stepper_set_max_velocity(const struct device *dev, uint32_t velocity)
389342
{
390343
const struct tmc50xx_stepper_config *config = dev->config;
@@ -497,7 +450,7 @@ static int tmc50xx_stepper_get_actual_position(const struct device *dev, int32_t
497450

498451
static int tmc50xx_stepper_move_to(const struct device *dev, const int32_t micro_steps)
499452
{
500-
LOG_DBG("Stepper motor controller %s set target position to %d", dev->name, micro_steps);
453+
LOG_DBG("%s set target position to %d", dev->name, micro_steps);
501454
const struct tmc50xx_stepper_config *config = dev->config;
502455
struct tmc50xx_stepper_data *data = dev->data;
503456
int err;
@@ -530,6 +483,22 @@ static int tmc50xx_stepper_move_to(const struct device *dev, const int32_t micro
530483
return 0;
531484
}
532485

486+
static int tmc50xx_stepper_move_by(const struct device *dev, const int32_t micro_steps)
487+
{
488+
int err;
489+
int32_t position;
490+
491+
err = stepper_get_actual_position(dev, &position);
492+
if (err != 0) {
493+
return -EIO;
494+
}
495+
int32_t target_position = position + micro_steps;
496+
497+
LOG_DBG("%s moved to %d by steps: %d", dev->name, target_position, micro_steps);
498+
499+
return tmc50xx_stepper_move_to(dev, target_position);
500+
}
501+
533502
static int tmc50xx_stepper_run(const struct device *dev, const enum stepper_direction direction)
534503
{
535504
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)