Skip to content

Commit 705365c

Browse files
faxe1008kartben
authored andcommitted
drivers: stepper: Change stepper velocity to step interval
Change the stepper API to instead of changing the stepper speed based on the velocity in microsteps per second to use the delay in usec between successive steps. Also remove the velocity from the `stepper_run` function as typical API usage is enable -> set step interval -> run. Signed-off-by: Fabian Blatz <[email protected]>
1 parent 6e1bedd commit 705365c

18 files changed

+160
-217
lines changed

drivers/stepper/adi_tmc/adi_tmc22xx_stepper_controller.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ static DEVICE_API(stepper, tmc22xx_stepper_api) = {
150150
.set_reference_position = step_dir_stepper_common_set_reference_position,
151151
.get_actual_position = step_dir_stepper_common_get_actual_position,
152152
.move_to = step_dir_stepper_common_move_to,
153-
.set_max_velocity = step_dir_stepper_common_set_max_velocity,
153+
.set_microstep_interval = step_dir_stepper_common_set_microstep_interval,
154154
.run = step_dir_stepper_common_run,
155155
.set_event_callback = step_dir_stepper_common_set_event_callback,
156156
.set_micro_step_res = tmc22xx_stepper_set_micro_step_res,

drivers/stepper/adi_tmc/adi_tmc5041_stepper_controller.c

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ static int tmc5041_stepper_move_by(const struct device *dev, const int32_t micro
348348
return 0;
349349
}
350350

351-
static int tmc5041_stepper_set_max_velocity(const struct device *dev, uint32_t velocity)
351+
int tmc5041_stepper_set_max_velocity(const struct device *dev, uint32_t velocity)
352352
{
353353
const struct tmc5041_stepper_config *config = dev->config;
354354
const struct tmc5041_config *tmc5041_config = config->controller->config;
@@ -477,19 +477,13 @@ static int tmc5041_stepper_move_to(const struct device *dev, const int32_t micro
477477
return 0;
478478
}
479479

480-
static int tmc5041_stepper_run(const struct device *dev, const enum stepper_direction direction,
481-
const uint32_t velocity)
480+
static int tmc5041_stepper_run(const struct device *dev, const enum stepper_direction direction)
482481
{
483-
LOG_DBG("Stepper motor controller %s run with velocity %d", dev->name, velocity);
482+
LOG_DBG("Stepper motor controller %s run", dev->name);
484483
const struct tmc5041_stepper_config *config = dev->config;
485-
const struct tmc5041_config *tmc5041_config = config->controller->config;
486484
struct tmc5041_stepper_data *data = dev->data;
487-
const uint32_t clock_frequency = tmc5041_config->clock_frequency;
488-
uint32_t velocity_fclk;
489485
int err;
490486

491-
velocity_fclk = tmc5xxx_calculate_velocity_from_hz_to_fclk(velocity, clock_frequency);
492-
493487
if (config->is_sg_enabled) {
494488
err = stallguard_enable(dev, false);
495489
if (err != 0) {
@@ -504,10 +498,6 @@ static int tmc5041_stepper_run(const struct device *dev, const enum stepper_dire
504498
if (err != 0) {
505499
return -EIO;
506500
}
507-
err = tmc5041_write(config->controller, TMC5041_VMAX(config->index), velocity_fclk);
508-
if (err != 0) {
509-
return -EIO;
510-
}
511501
break;
512502

513503
case STEPPER_DIRECTION_NEGATIVE:
@@ -516,10 +506,6 @@ static int tmc5041_stepper_run(const struct device *dev, const enum stepper_dire
516506
if (err != 0) {
517507
return -EIO;
518508
}
519-
err = tmc5041_write(config->controller, TMC5041_VMAX(config->index), velocity_fclk);
520-
if (err != 0) {
521-
return -EIO;
522-
}
523509
break;
524510
}
525511

@@ -724,7 +710,6 @@ static int tmc5041_stepper_init(const struct device *dev)
724710
.enable = tmc5041_stepper_enable, \
725711
.is_moving = tmc5041_stepper_is_moving, \
726712
.move_by = tmc5041_stepper_move_by, \
727-
.set_max_velocity = tmc5041_stepper_set_max_velocity, \
728713
.set_micro_step_res = tmc5041_stepper_set_micro_step_res, \
729714
.get_micro_step_res = tmc5041_stepper_get_micro_step_res, \
730715
.set_reference_position = tmc5041_stepper_set_reference_position, \

drivers/stepper/fake_stepper_controller.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ DEFINE_FAKE_VALUE_FUNC(int, fake_stepper_is_moving, const struct device *, bool
2525

2626
DEFINE_FAKE_VALUE_FUNC(int, fake_stepper_move_by, const struct device *, int32_t);
2727

28-
DEFINE_FAKE_VALUE_FUNC(int, fake_stepper_set_max_velocity, const struct device *, uint32_t);
28+
DEFINE_FAKE_VALUE_FUNC(int, fake_stepper_set_microstep_interval, const struct device *, uint64_t);
2929

3030
DEFINE_FAKE_VALUE_FUNC(int, fake_stepper_set_micro_step_res, const struct device *,
3131
enum stepper_micro_step_resolution);
@@ -39,8 +39,7 @@ DEFINE_FAKE_VALUE_FUNC(int, fake_stepper_get_actual_position, const struct devic
3939

4040
DEFINE_FAKE_VALUE_FUNC(int, fake_stepper_move_to, const struct device *, int32_t);
4141

42-
DEFINE_FAKE_VALUE_FUNC(int, fake_stepper_run, const struct device *, enum stepper_direction,
43-
uint32_t);
42+
DEFINE_FAKE_VALUE_FUNC(int, fake_stepper_run, const struct device *, enum stepper_direction);
4443

4544
DEFINE_FAKE_VALUE_FUNC(int, fake_stepper_set_event_callback, const struct device *,
4645
stepper_event_callback_t, void *);
@@ -92,7 +91,7 @@ static void fake_stepper_reset_rule_before(const struct ztest_unit_test *test, v
9291
RESET_FAKE(fake_stepper_enable);
9392
RESET_FAKE(fake_stepper_move_by);
9493
RESET_FAKE(fake_stepper_is_moving);
95-
RESET_FAKE(fake_stepper_set_max_velocity);
94+
RESET_FAKE(fake_stepper_set_microstep_interval);
9695
RESET_FAKE(fake_stepper_set_micro_step_res);
9796
RESET_FAKE(fake_stepper_get_micro_step_res);
9897
RESET_FAKE(fake_stepper_set_reference_position);
@@ -128,7 +127,7 @@ static DEVICE_API(stepper, fake_stepper_driver_api) = {
128127
.enable = fake_stepper_enable,
129128
.move_by = fake_stepper_move_by,
130129
.is_moving = fake_stepper_is_moving,
131-
.set_max_velocity = fake_stepper_set_max_velocity,
130+
.set_microstep_interval = fake_stepper_set_microstep_interval,
132131
.set_micro_step_res = fake_stepper_set_micro_step_res,
133132
.get_micro_step_res = fake_stepper_get_micro_step_res,
134133
.set_reference_position = fake_stepper_set_reference_position,

drivers/stepper/gpio_stepper_controller.c

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -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

160160
static 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,

drivers/stepper/step_dir/step_dir_stepper_common.c

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -228,40 +228,36 @@ int step_dir_stepper_common_move_by(const struct device *dev, const int32_t micr
228228
struct step_dir_stepper_common_data *data = dev->data;
229229
const struct step_dir_stepper_common_config *config = dev->config;
230230

231-
if (data->max_velocity == 0) {
232-
LOG_ERR("Velocity not set or invalid velocity set");
231+
if (data->microstep_interval_ns == 0) {
232+
LOG_ERR("Step interval not set or invalid step interval set");
233233
return -EINVAL;
234234
}
235235

236236
K_SPINLOCK(&data->lock) {
237237
data->run_mode = STEPPER_RUN_MODE_POSITION;
238238
data->step_count = micro_steps;
239-
config->timing_source->update(dev, data->max_velocity);
239+
config->timing_source->update(dev, data->microstep_interval_ns);
240240
update_direction_from_step_count(dev);
241241
config->timing_source->start(dev);
242242
}
243243

244244
return 0;
245245
}
246246

247-
int step_dir_stepper_common_set_max_velocity(const struct device *dev, const uint32_t velocity)
247+
int step_dir_stepper_common_set_microstep_interval(const struct device *dev,
248+
const uint64_t microstep_interval_ns)
248249
{
249250
struct step_dir_stepper_common_data *data = dev->data;
250251
const struct step_dir_stepper_common_config *config = dev->config;
251252

252-
if (velocity == 0) {
253-
LOG_ERR("Velocity cannot be zero");
254-
return -EINVAL;
255-
}
256-
257-
if (velocity > USEC_PER_SEC) {
258-
LOG_ERR("Velocity cannot be greater than %d micro steps per second", USEC_PER_SEC);
253+
if (microstep_interval_ns == 0) {
254+
LOG_ERR("Step interval cannot be zero");
259255
return -EINVAL;
260256
}
261257

262258
K_SPINLOCK(&data->lock) {
263-
data->max_velocity = velocity;
264-
config->timing_source->update(dev, velocity);
259+
data->microstep_interval_ns = microstep_interval_ns;
260+
config->timing_source->update(dev, microstep_interval_ns);
265261
}
266262

267263
return 0;
@@ -294,15 +290,15 @@ int step_dir_stepper_common_move_to(const struct device *dev, const int32_t valu
294290
struct step_dir_stepper_common_data *data = dev->data;
295291
const struct step_dir_stepper_common_config *config = dev->config;
296292

297-
if (data->max_velocity == 0) {
298-
LOG_ERR("Velocity not set or invalid velocity set");
293+
if (data->microstep_interval_ns == 0) {
294+
LOG_ERR("Step interval not set or invalid step interval set");
299295
return -EINVAL;
300296
}
301297

302298
K_SPINLOCK(&data->lock) {
303299
data->run_mode = STEPPER_RUN_MODE_POSITION;
304300
data->step_count = value - data->actual_position;
305-
config->timing_source->update(dev, data->max_velocity);
301+
config->timing_source->update(dev, data->microstep_interval_ns);
306302
update_direction_from_step_count(dev);
307303
config->timing_source->start(dev);
308304
}
@@ -318,22 +314,16 @@ int step_dir_stepper_common_is_moving(const struct device *dev, bool *is_moving)
318314
return 0;
319315
}
320316

321-
int step_dir_stepper_common_run(const struct device *dev, const enum stepper_direction direction,
322-
const uint32_t velocity)
317+
int step_dir_stepper_common_run(const struct device *dev, const enum stepper_direction direction)
323318
{
324319
struct step_dir_stepper_common_data *data = dev->data;
325320
const struct step_dir_stepper_common_config *config = dev->config;
326321

327322
K_SPINLOCK(&data->lock) {
328323
data->run_mode = STEPPER_RUN_MODE_VELOCITY;
329324
data->direction = direction;
330-
data->max_velocity = velocity;
331-
config->timing_source->update(dev, velocity);
332-
if (velocity != 0) {
333-
config->timing_source->start(dev);
334-
} else {
335-
config->timing_source->stop(dev);
336-
}
325+
config->timing_source->update(dev, data->microstep_interval_ns);
326+
config->timing_source->start(dev);
337327
}
338328

339329
return 0;

drivers/stepper/step_dir/step_dir_stepper_common.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ struct step_dir_stepper_common_data {
7070
enum stepper_direction direction;
7171
enum stepper_run_mode run_mode;
7272
int32_t actual_position;
73-
uint32_t max_velocity;
73+
uint64_t microstep_interval_ns;
7474
int32_t step_count;
7575
stepper_event_callback_t callback;
7676
void *event_cb_user_data;
@@ -141,13 +141,14 @@ int step_dir_stepper_common_init(const struct device *dev);
141141
int step_dir_stepper_common_move_by(const struct device *dev, const int32_t micro_steps);
142142

143143
/**
144-
* @brief Set the maximum velocity in micro_steps per second.
144+
* @brief Set the step interval of the stepper motor.
145145
*
146146
* @param dev Pointer to the device structure.
147-
* @param velocity Maximum velocity in micro_steps per second.
147+
* @param microstep_interval_ns The step interval in nanoseconds.
148148
* @return 0 on success, or a negative error code on failure.
149149
*/
150-
int step_dir_stepper_common_set_max_velocity(const struct device *dev, const uint32_t velocity);
150+
int step_dir_stepper_common_set_microstep_interval(const struct device *dev,
151+
const uint64_t microstep_interval_ns);
151152

152153
/**
153154
* @brief Set the reference position of the stepper motor.
@@ -186,15 +187,13 @@ int step_dir_stepper_common_move_to(const struct device *dev, const int32_t valu
186187
int step_dir_stepper_common_is_moving(const struct device *dev, bool *is_moving);
187188

188189
/**
189-
* @brief Run the stepper with a given velocity in a given direction.
190+
* @brief Run the stepper with a given direction and step interval.
190191
*
191192
* @param dev Pointer to the device structure.
192193
* @param direction The direction of movement (positive or negative).
193-
* @param velocity The velocity in micro_steps per second.
194194
* @return 0 on success, or a negative error code on failure.
195195
*/
196-
int step_dir_stepper_common_run(const struct device *dev, const enum stepper_direction direction,
197-
const uint32_t velocity);
196+
int step_dir_stepper_common_run(const struct device *dev, const enum stepper_direction direction);
198197

199198
/**
200199
* @brief Set a callback function for stepper motor events.

drivers/stepper/step_dir/step_dir_stepper_counter_timing.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,19 @@ static void step_counter_top_interrupt(const struct device *dev, void *user_data
1717
stepper_handle_timing_signal(data->dev);
1818
}
1919

20-
int step_counter_timing_source_update(const struct device *dev, const uint32_t velocity)
20+
int step_counter_timing_source_update(const struct device *dev,
21+
const uint64_t microstep_interval_ns)
2122
{
2223
const struct step_dir_stepper_common_config *config = dev->config;
2324
struct step_dir_stepper_common_data *data = dev->data;
2425
int ret;
2526

26-
if (velocity == 0) {
27+
if (microstep_interval_ns == 0) {
2728
return -EINVAL;
2829
}
2930

30-
data->counter_top_cfg.ticks =
31-
DIV_ROUND_UP(counter_us_to_ticks(config->counter, USEC_PER_SEC), velocity);
31+
data->counter_top_cfg.ticks = DIV_ROUND_UP(
32+
counter_get_frequency(config->counter) * microstep_interval_ns, NSEC_PER_SEC);
3233

3334
/* Lock interrupts while modifying counter settings */
3435
int key = irq_lock();

drivers/stepper/step_dir/step_dir_stepper_timing_source.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ typedef int (*stepper_timing_source_init)(const struct device *dev);
2020
* @brief Update the stepper timing source.
2121
*
2222
* @param dev Pointer to the device structure.
23-
* @param velocity Velocity in microsteps per second.
23+
* @param microstep_interval_ns Step interval in nanoseconds.
2424
* @return 0 on success, or a negative error code on failure.
2525
*/
26-
typedef int (*stepper_timing_source_update)(const struct device *dev, uint32_t velocity);
26+
typedef int (*stepper_timing_source_update)(const struct device *dev,
27+
uint64_t microstep_interval_ns);
2728

2829
/**
2930
* @brief Start the stepper timing source.

0 commit comments

Comments
 (0)