Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linear advance with I2S streaming #24684

Merged
merged 13 commits into from
Sep 2, 2022
1 change: 1 addition & 0 deletions Marlin/Configuration_adv.h
Original file line number Diff line number Diff line change
Expand Up @@ -2042,6 +2042,7 @@
//#define LA_DEBUG // If enabled, this will generate debug information output over USB.
//#define EXPERIMENTAL_SCURVE // Enable this option to permit S-Curve Acceleration
//#define ALLOW_LOW_EJERK // Allow a DEFAULT_EJERK value of <10. Recommended for direct drive hotends.
//#define EXPERIMENTAL_I2S_LA // Enable this option to permit I2S_STEPPER_STREAM. LA performance degrades as the LA step rate increases above about 20kHz.
#endif

// @section leveling
Expand Down
35 changes: 29 additions & 6 deletions Marlin/src/HAL/ESP32/i2s.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,21 +139,44 @@ static void IRAM_ATTR i2s_intr_handler_default(void *arg) {
}

void stepperTask(void *parameter) {
uint32_t remaining = 0;
uint32_t nextMainISR = 0;
#if ENABLED(LIN_ADVANCE)
uint32_t nextAdvanceISR = Stepper::LA_ADV_NEVER;
#endif

while (1) {
xQueueReceive(dma.queue, &dma.current, portMAX_DELAY);
dma.rw_pos = 0;

while (dma.rw_pos < DMA_SAMPLE_COUNT) {
// Fill with the port data post pulse_phase until the next step
if (remaining) {
if (nextMainISR && TERN1(LIN_ADVANCE, nextAdvanceISR))
i2s_push_sample();
remaining--;
}
else {

// i2s_push_sample() is also called from Stepper::pulse_phase_isr() and Stepper::advance_isr()
// in a rare case where both are called, we need to double decrement the counters
const uint8_t push_count = !nextMainISR && TERN0(LIN_ADVANCE, !nextAdvanceISR) ? 2 : 1;

#if ENABLED(LIN_ADVANCE)
if (!nextAdvanceISR) {
Stepper::advance_isr();
nextAdvanceISR = Stepper::la_interval;
}
else if (nextAdvanceISR == Stepper::LA_ADV_NEVER)
nextAdvanceISR = Stepper::la_interval;
#endif

if (!nextMainISR) {
Stepper::pulse_phase_isr();
remaining = Stepper::block_phase_isr();
nextMainISR = Stepper::block_phase_isr();
}

LOOP_L_N(i, push_count) {
nextMainISR--;
#if ENABLED(LIN_ADVANCE)
if (nextAdvanceISR != Stepper::LA_ADV_NEVER)
thinkyhead marked this conversation as resolved.
Show resolved Hide resolved
nextAdvanceISR--;
#endif
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/HAL/ESP32/inc/SanityCheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@
#error "PULLDOWN pin mode is not available on ESP32 boards."
#endif

#if BOTH(I2S_STEPPER_STREAM, LIN_ADVANCE)
#if BOTH(I2S_STEPPER_STREAM, LIN_ADVANCE) && DISABLED(EXPERIMENTAL_I2S_LA)
#error "I2S stream is currently incompatible with LIN_ADVANCE."
#endif
9 changes: 8 additions & 1 deletion Marlin/src/module/stepper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ Stepper stepper; // Singleton
#include "../lcd/extui/ui_api.h"
#endif

#if ENABLED(I2S_STEPPER_STREAM)
#include "../HAL/ESP32/i2s.h"
#endif

// public:

#if EITHER(HAS_EXTRA_ENDSTOPS, Z_STEPPER_AUTO_ALIGN)
Expand Down Expand Up @@ -2468,7 +2472,8 @@ uint32_t Stepper::block_phase_isr() {
// the acceleration and speed values calculated in block_phase_isr().
// This helps keep LA in sync with, for example, S_CURVE_ACCELERATION.
la_delta_error += la_dividend;
if (la_delta_error >= 0) {
const bool step_needed = la_delta_error >= 0;
if (step_needed) {
count_position.e += count_direction.e;
la_advance_steps += count_direction.e;
la_delta_error -= advance_divisor;
Expand All @@ -2480,6 +2485,8 @@ uint32_t Stepper::block_phase_isr() {
E_STEP_WRITE(stepper_extruder, !INVERT_E_STEP_PIN);
#endif

TERN_(I2S_STEPPER_STREAM, i2s_push_sample());

// Enforce a minimum duration for STEP pulse ON
#if ISR_PULSE_CONTROL
USING_TIMED_PULSE();
Expand Down
1 change: 1 addition & 0 deletions Marlin/src/module/stepper.h
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ constexpr ena_mask_t enable_overlap[] = {
class Stepper {
friend class KinematicSystem;
friend class DeltaKinematicSystem;
friend void stepperTask(void *);

public:

Expand Down