Skip to content

Commit

Permalink
Fixed step count, timing and direction issues
Browse files Browse the repository at this point in the history
  • Loading branch information
MitchBradley committed Oct 10, 2024
1 parent 6c07910 commit 621e955
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
11 changes: 9 additions & 2 deletions FluidNC/esp32/StepTimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,21 @@ static void IRAM_ATTR timer_isr(void* arg) {
}
}

// Possibly-unnecessary optimization to avoid rewriting the alarm value
static uint32_t old_ticks = 0xffffffff;

void IRAM_ATTR stepTimerStart() {
timer_ll_set_alarm_value(&TIMERG0, TIMER_0, 10ULL); // Interrupt very soon to start the stepping
old_ticks = 10ULL;
timer_ll_set_alarm_enable(&TIMERG0, TIMER_0, true);
timer_ll_set_counter_enable(&TIMERG0, TIMER_0, true);
}

void IRAM_ATTR stepTimerSetTicks(uint32_t ticks) {
timer_ll_set_alarm_value(&TIMERG0, TIMER_0, (uint64_t)ticks);
if (ticks != old_ticks) {
timer_ll_set_alarm_value(&TIMERG0, TIMER_0, (uint64_t)ticks);
old_ticks = ticks;
}
}

void IRAM_ATTR stepTimerStop() {
Expand All @@ -61,7 +68,7 @@ void stepTimerInit(uint32_t frequency, bool (*callback)(void)) {
timer_isr_callback = callback;

esp_intr_alloc_intrstatus(timer_group_periph_signals.groups[TIMER_GROUP_0].t0_irq_id,
ESP_INTR_FLAG_IRAM,
ESP_INTR_FLAG_IRAM | ESP_INTR_FLAG_LEVEL3,
timer_ll_get_intr_status_reg(&TIMERG0),
1 << TIMER_0,
timer_isr,
Expand Down
18 changes: 10 additions & 8 deletions FluidNC/esp32/i2s_engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -455,22 +455,24 @@ static IRAM_ATTR void finish_dir() {
// push _pulse_counts copies of the memory variable to the
// I2S FIFO, thus creating a pulse of the desired length.
static IRAM_ATTR void finish_step() {
for (int i = 0; i < _pulse_counts; i++) {
I2S0.fifo_wr = new_port_data;
if (new_port_data == i2s_out_port_data) {
return;
}
for (int i = 0; i < _pulse_counts; i++) {
I2S0.fifo_wr = i2s_out_port_data;
I2S0.fifo_wr = new_port_data;
}
// There is no need for multiple "step off" samples since the timer will not fire
// until the next time for a pulse.
I2S0.fifo_wr = i2s_out_port_data;
}

static IRAM_ATTR int start_unstep() {
#if 1
return 1;
#else
return 0;
#endif
}

// Not called since start_unstep() returns 1
static IRAM_ATTR void finish_unstep() {}

static uint32_t max_pulses_per_sec() {
return 1000000 / (2 * _pulse_counts * I2S_OUT_USEC_PER_PULSE);
}
Expand All @@ -486,7 +488,7 @@ step_engine_t engine = {
set_step_pin,
finish_step,
start_unstep,
finish_step, // finish_step and finish_unstep are the same
finish_unstep,
max_pulses_per_sec
};

Expand Down
2 changes: 1 addition & 1 deletion FluidNC/src/Machine/Axis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace Machine {
void Axis::group(Configuration::HandlerBase& handler) {
handler.item("steps_per_mm", _stepsPerMm, 0.001, 100000.0);
handler.item("max_rate_mm_per_min", _maxRate, 0.001, 100000.0);
handler.item("max_rate_mm_per_min", _maxRate, 0.001, 200000.0);
handler.item("acceleration_mm_per_sec2", _acceleration, 0.001, 100000.0);
handler.item("max_travel_mm", _maxTravel, 0.1, 10000000.0);
handler.item("soft_limits", _softLimits);
Expand Down
5 changes: 3 additions & 2 deletions FluidNC/src/Stepping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ void IRAM_ATTR Stepping::step(uint8_t step_mask, uint8_t dir_mask) {
previous_dir_mask = dir_mask;
}

step_engine->start_step();

// Turn on step pulses for motors that are supposed to step now
for (size_t axis = 0; axis < _n_active_axes; axis++) {
if (bitnum_is_true(step_mask, axis)) {
Expand Down Expand Up @@ -193,8 +195,7 @@ void IRAM_ATTR Stepping::waitDirection() {}
// Called only from Stepper::pulse_func when a new segment is loaded
// The argument is in units of ticks of the timer that generates ISRs
void IRAM_ATTR Stepping::setTimerPeriod(uint16_t timerTicks) {
// stepTimerSetTicks((uint32_t)timerTicks * 3 / 10);
stepTimerSetTicks((uint32_t)timerTicks / 2);
stepTimerSetTicks((uint32_t)timerTicks);
}

// Called only from Stepper::wake_up which is not used in ISR context
Expand Down

0 comments on commit 621e955

Please sign in to comment.