From e6b182aa9fc3a8f812a8580a0c25152418f3c2bc Mon Sep 17 00:00:00 2001 From: Yuri D'Elia Date: Tue, 18 Jun 2019 17:11:01 +0200 Subject: [PATCH] Implement proper step/delay pauses in tmc2130 functions Add constants for the various required delays in tmc2130.h, which will come in handy for stepper.cpp as well. Move the delays in the _set functions and remove the pauses from the various calling points and macros. Note that the hard-coded pause wouldn't cut it for the stepper ISR, but it's fine for other use cases. --- Firmware/tmc2130.cpp | 39 ++++++++++++++++++++------------------- Firmware/tmc2130.h | 4 ++++ 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/Firmware/tmc2130.cpp b/Firmware/tmc2130.cpp index 20c23704..15646b92 100755 --- a/Firmware/tmc2130.cpp +++ b/Firmware/tmc2130.cpp @@ -696,31 +696,31 @@ static uint8_t tmc2130_rx(uint8_t axis, uint8_t addr, uint32_t* rval) #define _GET_PWR_Z (READ(Z_ENABLE_PIN) == Z_ENABLE_ON) #define _GET_PWR_E (READ(E0_ENABLE_PIN) == E_ENABLE_ON) -#define _SET_PWR_X(ena) { WRITE(X_ENABLE_PIN, ena?X_ENABLE_ON:!X_ENABLE_ON); asm("nop"); } -#define _SET_PWR_Y(ena) { WRITE(Y_ENABLE_PIN, ena?Y_ENABLE_ON:!Y_ENABLE_ON); asm("nop"); } -#define _SET_PWR_Z(ena) { WRITE(Z_ENABLE_PIN, ena?Z_ENABLE_ON:!Z_ENABLE_ON); asm("nop"); } -#define _SET_PWR_E(ena) { WRITE(E0_ENABLE_PIN, ena?E_ENABLE_ON:!E_ENABLE_ON); asm("nop"); } +#define _SET_PWR_X(ena) WRITE(X_ENABLE_PIN, ena?X_ENABLE_ON:!X_ENABLE_ON) +#define _SET_PWR_Y(ena) WRITE(Y_ENABLE_PIN, ena?Y_ENABLE_ON:!Y_ENABLE_ON) +#define _SET_PWR_Z(ena) WRITE(Z_ENABLE_PIN, ena?Z_ENABLE_ON:!Z_ENABLE_ON) +#define _SET_PWR_E(ena) WRITE(E0_ENABLE_PIN, ena?E_ENABLE_ON:!E_ENABLE_ON) #define _GET_DIR_X (READ(X_DIR_PIN) == INVERT_X_DIR) #define _GET_DIR_Y (READ(Y_DIR_PIN) == INVERT_Y_DIR) #define _GET_DIR_Z (READ(Z_DIR_PIN) == INVERT_Z_DIR) #define _GET_DIR_E (READ(E0_DIR_PIN) == INVERT_E0_DIR) -#define _SET_DIR_X(dir) { WRITE(X_DIR_PIN, dir?INVERT_X_DIR:!INVERT_X_DIR); asm("nop"); } -#define _SET_DIR_Y(dir) { WRITE(Y_DIR_PIN, dir?INVERT_Y_DIR:!INVERT_Y_DIR); asm("nop"); } -#define _SET_DIR_Z(dir) { WRITE(Z_DIR_PIN, dir?INVERT_Z_DIR:!INVERT_Z_DIR); asm("nop"); } -#define _SET_DIR_E(dir) { WRITE(E0_DIR_PIN, dir?INVERT_E0_DIR:!INVERT_E0_DIR); asm("nop"); } +#define _SET_DIR_X(dir) WRITE(X_DIR_PIN, dir?INVERT_X_DIR:!INVERT_X_DIR) +#define _SET_DIR_Y(dir) WRITE(Y_DIR_PIN, dir?INVERT_Y_DIR:!INVERT_Y_DIR) +#define _SET_DIR_Z(dir) WRITE(Z_DIR_PIN, dir?INVERT_Z_DIR:!INVERT_Z_DIR) +#define _SET_DIR_E(dir) WRITE(E0_DIR_PIN, dir?INVERT_E0_DIR:!INVERT_E0_DIR) #ifdef TMC2130_DEDGE_STEPPING -#define _DO_STEP_X { TOGGLE(X_STEP_PIN); asm("nop"); } -#define _DO_STEP_Y { TOGGLE(Y_STEP_PIN); asm("nop"); } -#define _DO_STEP_Z { TOGGLE(Z_STEP_PIN); asm("nop"); } -#define _DO_STEP_E { TOGGLE(E0_STEP_PIN); asm("nop"); } +#define _DO_STEP_X TOGGLE(X_STEP_PIN) +#define _DO_STEP_Y TOGGLE(Y_STEP_PIN) +#define _DO_STEP_Z TOGGLE(Z_STEP_PIN) +#define _DO_STEP_E TOGGLE(E0_STEP_PIN) #else -#define _DO_STEP_X { WRITE(X_STEP_PIN, !INVERT_X_STEP_PIN); asm("nop"); WRITE(X_STEP_PIN, INVERT_X_STEP_PIN); asm("nop"); } -#define _DO_STEP_Y { WRITE(Y_STEP_PIN, !INVERT_Y_STEP_PIN); asm("nop"); WRITE(Y_STEP_PIN, INVERT_Y_STEP_PIN); asm("nop"); } -#define _DO_STEP_Z { WRITE(Z_STEP_PIN, !INVERT_Z_STEP_PIN); asm("nop"); WRITE(Z_STEP_PIN, INVERT_Z_STEP_PIN); asm("nop"); } -#define _DO_STEP_E { WRITE(E0_STEP_PIN, !INVERT_E_STEP_PIN); asm("nop"); WRITE(E0_STEP_PIN, INVERT_E_STEP_PIN); asm("nop"); } +#define _DO_STEP_X { WRITE(X_STEP_PIN, !INVERT_X_STEP_PIN); delayMicroseconds(TMC2130_MINIMUM_PULSE); WRITE(X_STEP_PIN, INVERT_X_STEP_PIN); } +#define _DO_STEP_Y { WRITE(Y_STEP_PIN, !INVERT_Y_STEP_PIN); delayMicroseconds(TMC2130_MINIMUM_PULSE); WRITE(Y_STEP_PIN, INVERT_Y_STEP_PIN); } +#define _DO_STEP_Z { WRITE(Z_STEP_PIN, !INVERT_Z_STEP_PIN); delayMicroseconds(TMC2130_MINIMUM_PULSE); WRITE(Z_STEP_PIN, INVERT_Z_STEP_PIN); } +#define _DO_STEP_E { WRITE(E0_STEP_PIN, !INVERT_E_STEP_PIN); delayMicroseconds(TMC2130_MINIMUM_PULSE); WRITE(E0_STEP_PIN, INVERT_E_STEP_PIN); } #endif @@ -762,6 +762,7 @@ void tmc2130_set_pwr(uint8_t axis, uint8_t pwr) case Z_AXIS: _SET_PWR_Z(pwr); break; case E_AXIS: _SET_PWR_E(pwr); break; } + delayMicroseconds(TMC2130_SET_PWR_DELAY); } uint8_t tmc2130_get_inv(uint8_t axis) @@ -798,6 +799,7 @@ void tmc2130_set_dir(uint8_t axis, uint8_t dir) case Z_AXIS: _SET_DIR_Z(dir); break; case E_AXIS: _SET_DIR_E(dir); break; } + delayMicroseconds(TMC2130_SET_DIR_DELAY); } void tmc2130_do_step(uint8_t axis) @@ -813,8 +815,8 @@ void tmc2130_do_step(uint8_t axis) void tmc2130_do_steps(uint8_t axis, uint16_t steps, uint8_t dir, uint16_t delay_us) { - tmc2130_set_dir(axis, dir); - delayMicroseconds(100); + if (tmc2130_get_dir(axis) != dir) + tmc2130_set_dir(axis, dir); while (steps--) { tmc2130_do_step(axis); @@ -845,7 +847,6 @@ void tmc2130_goto_step(uint8_t axis, uint8_t step, uint8_t dir, uint16_t delay_u cnt = steps; } tmc2130_set_dir(axis, dir); - delayMicroseconds(100); mscnt = tmc2130_rd_MSCNT(axis); while ((cnt--) && ((mscnt >> shift) != step)) { diff --git a/Firmware/tmc2130.h b/Firmware/tmc2130.h index ed4c0d7c..4b5b764c 100644 --- a/Firmware/tmc2130.h +++ b/Firmware/tmc2130.h @@ -29,6 +29,10 @@ extern uint8_t tmc2130_sg_homing_axes_mask; #define TMC2130_WAVE_FAC1000_MAX 200 #define TMC2130_WAVE_FAC1000_STP 1 +#define TMC2130_MINIMUM_PULSE 0 // minimum pulse width in uS +#define TMC2130_SET_DIR_DELAY 20 // minimum delay after setting direction in uS +#define TMC2130_SET_PWR_DELAY 0 // minimum delay after changing pwr mode in uS + extern uint8_t tmc2130_home_enabled; extern uint8_t tmc2130_home_origin[2]; extern uint8_t tmc2130_home_bsteps[2];