diff options
author | Kevin O'Connor <kevin@koconnor.net> | 2025-03-09 20:08:49 -0400 |
---|---|---|
committer | Kevin O'Connor <kevin@koconnor.net> | 2025-03-20 19:56:55 -0400 |
commit | 8faed8d9fe113e0b7dc306d08c36986150550ca8 (patch) | |
tree | d89f581a84650597ba92bcd84c46c1b5c9c37e47 /src/stepper.c | |
parent | 272e815522b0bc8e0806e052b73a5cc1af979cd7 (diff) | |
download | kutter-8faed8d9fe113e0b7dc306d08c36986150550ca8.tar.gz kutter-8faed8d9fe113e0b7dc306d08c36986150550ca8.tar.xz kutter-8faed8d9fe113e0b7dc306d08c36986150550ca8.zip |
stepper: Support step on both edges with custom minimum pulse duration
Add support for "step on both edges" to the main stepper_event_full()
code. This makes that mode of operation available even when the
micro-controller is not compiled for "optimized step on both edges".
It also enables the custom pulse duration support (step_pulse_ticks)
when in "step on both edges" mode.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Diffstat (limited to 'src/stepper.c')
-rw-r--r-- | src/stepper.c | 60 |
1 files changed, 38 insertions, 22 deletions
diff --git a/src/stepper.c b/src/stepper.c index 00a8ff01..f6f798c6 100644 --- a/src/stepper.c +++ b/src/stepper.c @@ -1,6 +1,6 @@ // Handling of stepper drivers. // -// Copyright (C) 2016-2021 Kevin O'Connor <kevin@koconnor.net> +// Copyright (C) 2016-2025 Kevin O'Connor <kevin@koconnor.net> // // This file may be distributed under the terms of the GNU GPLv3 license. @@ -14,17 +14,18 @@ #include "stepper.h" // stepper_event #include "trsync.h" // trsync_add_signal -#if CONFIG_INLINE_STEPPER_HACK && CONFIG_HAVE_STEPPER_BOTH_EDGE - #define HAVE_SINGLE_SCHEDULE 1 +DECL_CONSTANT("STEPPER_STEP_BOTH_EDGE", 1); + +#if CONFIG_INLINE_STEPPER_HACK && CONFIG_HAVE_STEPPER_OPTIMIZED_BOTH_EDGE + #define HAVE_OPTIMIZED_PATH 1 #define HAVE_EDGE_OPTIMIZATION 1 #define HAVE_AVR_OPTIMIZATION 0 - DECL_CONSTANT("STEPPER_BOTH_EDGE", 1); #elif CONFIG_INLINE_STEPPER_HACK && CONFIG_MACH_AVR - #define HAVE_SINGLE_SCHEDULE 1 + #define HAVE_OPTIMIZED_PATH 1 #define HAVE_EDGE_OPTIMIZATION 0 #define HAVE_AVR_OPTIMIZATION 1 #else - #define HAVE_SINGLE_SCHEDULE 0 + #define HAVE_OPTIMIZED_PATH 0 #define HAVE_EDGE_OPTIMIZATION 0 #define HAVE_AVR_OPTIMIZATION 0 #endif @@ -57,7 +58,7 @@ enum { POSITION_BIAS=0x40000000 }; enum { SF_LAST_DIR=1<<0, SF_NEXT_DIR=1<<1, SF_INVERT_STEP=1<<2, SF_NEED_RESET=1<<3, - SF_SINGLE_SCHED=1<<4, SF_HAVE_ADD=1<<5 + SF_SINGLE_SCHED=1<<4, SF_OPTIMIZED_PATH=1<<5, SF_HAVE_ADD=1<<6 }; // Setup a stepper for the next move in its queue @@ -75,7 +76,7 @@ stepper_load_next(struct stepper *s) struct stepper_move *m = container_of(mn, struct stepper_move, node); s->add = m->add; s->interval = m->interval + m->add; - if (HAVE_SINGLE_SCHEDULE && s->flags & SF_SINGLE_SCHED) { + if (HAVE_OPTIMIZED_PATH && s->flags & SF_OPTIMIZED_PATH) { s->time.waketime += m->interval; if (HAVE_AVR_OPTIMIZATION) s->flags = m->add ? s->flags|SF_HAVE_ADD : s->flags & ~SF_HAVE_ADD; @@ -85,7 +86,7 @@ stepper_load_next(struct stepper *s) // twice as many events. s->next_step_time += m->interval; s->time.waketime = s->next_step_time; - s->count = (uint32_t)m->count * 2; + s->count = s->flags & SF_SINGLE_SCHED ? m->count : (uint32_t)m->count*2; } // Add all steps to s->position (stepper_get_position() can calc mid-move) if (m->flags & MF_DIR) { @@ -99,8 +100,14 @@ stepper_load_next(struct stepper *s) return SF_RESCHEDULE; } +// Edge optimization only enabled when fastest rate notably slower than 100ns +#define EDGE_STEP_TICKS DIV_ROUND_UP(CONFIG_CLOCK_FREQ, 8000000) +#if HAVE_EDGE_OPTIMIZATION + DECL_CONSTANT("STEPPER_OPTIMIZED_EDGE", EDGE_STEP_TICKS); +#endif + // Optimized step function to step on each step pin edge -uint_fast8_t +static uint_fast8_t stepper_event_edge(struct timer *t) { struct stepper *s = container_of(t, struct stepper, time); @@ -115,7 +122,10 @@ stepper_event_edge(struct timer *t) return stepper_load_next(s); } -#define AVR_STEP_INSNS 40 // minimum instructions between step gpio pulses +#define AVR_STEP_TICKS 40 // minimum instructions between step gpio pulses +#if HAVE_AVR_OPTIMIZATION + DECL_CONSTANT("STEPPER_OPTIMIZED_UNSTEP", AVR_STEP_TICKS); +#endif // AVR optimized step function static uint_fast8_t @@ -137,8 +147,8 @@ stepper_event_avr(struct timer *t) return ret; } -// Regular "double scheduled" step function -uint_fast8_t +// Regular "fully scheduled" step function +static uint_fast8_t stepper_event_full(struct timer *t) { struct stepper *s = container_of(t, struct stepper, time); @@ -146,7 +156,7 @@ stepper_event_full(struct timer *t) uint32_t curtime = timer_read_time(); uint32_t min_next_time = curtime + s->step_pulse_ticks; s->count--; - if (likely(s->count & 1)) + if (likely(s->count & 1 && !(s->flags & SF_SINGLE_SCHED))) // Schedule unstep event goto reschedule_min; if (likely(s->count)) { @@ -186,20 +196,23 @@ command_config_stepper(uint32_t *args) { struct stepper *s = oid_alloc(args[0], command_config_stepper, sizeof(*s)); int_fast8_t invert_step = args[3]; - s->flags = invert_step > 0 ? SF_INVERT_STEP : 0; + if (invert_step > 0) + s->flags = SF_INVERT_STEP; + else if (invert_step < 0) + s->flags = SF_SINGLE_SCHED; s->step_pin = gpio_out_setup(args[1], s->flags & SF_INVERT_STEP); s->dir_pin = gpio_out_setup(args[2], 0); s->position = -POSITION_BIAS; s->step_pulse_ticks = args[4]; move_queue_setup(&s->mq, sizeof(struct stepper_move)); if (HAVE_EDGE_OPTIMIZATION) { - if (!s->step_pulse_ticks && invert_step < 0) - s->flags |= SF_SINGLE_SCHED; + if (invert_step < 0 && s->step_pulse_ticks <= EDGE_STEP_TICKS) + s->flags |= SF_OPTIMIZED_PATH; else s->time.func = stepper_event_full; } else if (HAVE_AVR_OPTIMIZATION) { - if (s->step_pulse_ticks <= AVR_STEP_INSNS) - s->flags |= SF_SINGLE_SCHED; + if (invert_step >= 0 && s->step_pulse_ticks <= AVR_STEP_TICKS) + s->flags |= SF_SINGLE_SCHED | SF_OPTIMIZED_PATH; else s->time.func = stepper_event_full; } else if (!CONFIG_INLINE_STEPPER_HACK) { @@ -284,7 +297,7 @@ stepper_get_position(struct stepper *s) { uint32_t position = s->position; // If stepper is mid-move, subtract out steps not yet taken - if (HAVE_SINGLE_SCHEDULE && s->flags & SF_SINGLE_SCHED) + if (s->flags & SF_SINGLE_SCHED) position -= s->count; else position -= s->count / 2; @@ -316,9 +329,12 @@ stepper_stop(struct trsync_signal *tss, uint8_t reason) s->next_step_time = s->time.waketime = 0; s->position = -stepper_get_position(s); s->count = 0; - s->flags = (s->flags & (SF_INVERT_STEP|SF_SINGLE_SCHED)) | SF_NEED_RESET; + s->flags = ((s->flags & (SF_INVERT_STEP|SF_SINGLE_SCHED|SF_OPTIMIZED_PATH)) + | SF_NEED_RESET); gpio_out_write(s->dir_pin, 0); - if (!(HAVE_EDGE_OPTIMIZATION && s->flags & SF_SINGLE_SCHED)) + if (!(s->flags & SF_SINGLE_SCHED) + || (HAVE_AVR_OPTIMIZATION && s->flags & SF_OPTIMIZED_PATH)) + // Must return step pin to "unstep" state gpio_out_write(s->step_pin, s->flags & SF_INVERT_STEP); while (!move_queue_empty(&s->mq)) { struct move_node *mn = move_queue_pop(&s->mq); |