From fd55dd9e9ddede76c525ed07a03e055cc3926876 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Thu, 8 May 2025 08:32:02 -0400 Subject: [PATCH] stepper: Also ensure minimum time after dir change and next step In practice the host will not schedule any steps immediately after a direction change (due to acceleration limits and the host "step+dir+step filter"). However, there is also no harm in enforcing a minimum duration in the mcu. Signed-off-by: Kevin O'Connor --- src/stepper.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/stepper.c b/src/stepper.c index b74a3ced..c965a837 100644 --- a/src/stepper.c +++ b/src/stepper.c @@ -109,10 +109,18 @@ stepper_load_next(struct stepper *s) shutdown("Stepper too far in past"); s->time.waketime = min_next_time; } - if (was_active && need_dir_change && s->flags & SF_SINGLE_SCHED) + if (was_active && need_dir_change) { // Must ensure minimum time between step change and dir change - while (timer_is_before(timer_read_time(), min_next_time)) - ; + if (s->flags & SF_SINGLE_SCHED) + while (timer_is_before(timer_read_time(), min_next_time)) + ; + gpio_out_toggle_noirq(s->dir_pin); + uint32_t curtime = timer_read_time(); + min_next_time = curtime + s->step_pulse_ticks; + if (timer_is_before(s->time.waketime, min_next_time)) + s->time.waketime = min_next_time; + return SF_RESCHEDULE; + } } // Set new direction (if needed)