|
@@ -5,6 +5,10 @@
|
|
|
#include "tmc2130.h"
|
|
|
#include <SPI.h>
|
|
|
|
|
|
+#define TMC2130_GCONF_NORMAL 0x00000000 // spreadCycle
|
|
|
+#define TMC2130_GCONF_SGSENS 0x00003180 // spreadCycle with stallguard (stall activates DIAG0 and DIAG1 [pushpull])
|
|
|
+#define TMC2130_GCONF_SILENT 0x00000004 // stealthChop
|
|
|
+
|
|
|
//externals for debuging
|
|
|
extern float current_position[4];
|
|
|
extern void st_get_position_xy(long &x, long &y);
|
|
@@ -12,6 +16,8 @@ extern long st_get_position(uint8_t axis);
|
|
|
|
|
|
//chipselect pins
|
|
|
uint8_t tmc2130_cs[4] = { X_TMC2130_CS, Y_TMC2130_CS, Z_TMC2130_CS, E0_TMC2130_CS };
|
|
|
+//diag pins
|
|
|
+uint8_t tmc2130_diag[4] = { X_TMC2130_DIAG, Y_TMC2130_DIAG, Z_TMC2130_DIAG, E0_TMC2130_DIAG };
|
|
|
//mode
|
|
|
uint8_t tmc2130_mode = TMC2130_MODE_NORMAL;
|
|
|
//holding currents
|
|
@@ -20,8 +26,6 @@ uint8_t tmc2130_current_h[4] = TMC2130_CURRENTS_H;
|
|
|
uint8_t tmc2130_current_r[4] = TMC2130_CURRENTS_R;
|
|
|
//axis stalled flags
|
|
|
uint8_t tmc2130_axis_stalled[2] = {0, 0};
|
|
|
-//last homing stalled
|
|
|
-uint8_t tmc2130_LastHomingStalled = 0;
|
|
|
|
|
|
//pwm_ampl
|
|
|
uint8_t tmc2130_pwm_ampl[2] = {TMC2130_PWM_AMPL_XY, TMC2130_PWM_AMPL_XY};
|
|
@@ -105,6 +109,10 @@ void tmc2130_init()
|
|
|
SET_OUTPUT(Y_TMC2130_CS);
|
|
|
SET_OUTPUT(Z_TMC2130_CS);
|
|
|
SET_OUTPUT(E0_TMC2130_CS);
|
|
|
+ SET_INPUT(X_TMC2130_DIAG);
|
|
|
+ SET_INPUT(Y_TMC2130_DIAG);
|
|
|
+ SET_INPUT(Z_TMC2130_DIAG);
|
|
|
+ SET_INPUT(E0_TMC2130_DIAG);
|
|
|
SPI.begin();
|
|
|
for (int i = 0; i < 2; i++) // X Y axes
|
|
|
{
|
|
@@ -112,7 +120,7 @@ void tmc2130_init()
|
|
|
tmc2130_wr_CHOPCONF(tmc2130_cs[i], 3, 5, 1, 0, 0, 0, 0, 2, 1, 0, 0, 0, mres, TMC2130_INTPOL_XY, 0, 0);
|
|
|
tmc2130_wr(tmc2130_cs[i], TMC2130_REG_IHOLD_IRUN, 0x000f0000 | ((tmc2130_current_r[i] & 0x1f) << 8) | (tmc2130_current_h[i] & 0x1f));
|
|
|
tmc2130_wr(tmc2130_cs[i], TMC2130_REG_TPOWERDOWN, 0x00000000);
|
|
|
- tmc2130_wr(tmc2130_cs[i], TMC2130_REG_GCONF, (tmc2130_mode == TMC2130_MODE_SILENT)?0x00000004:0x00000000);
|
|
|
+ tmc2130_wr(tmc2130_cs[i], TMC2130_REG_GCONF, (tmc2130_mode == TMC2130_MODE_SILENT)?TMC2130_GCONF_SILENT:TMC2130_GCONF_SGSENS);
|
|
|
tmc2130_wr_PWMCONF(tmc2130_cs[i], tmc2130_pwm_ampl[i], tmc2130_pwm_grad[i], tmc2130_pwm_freq[i], tmc2130_pwm_auto[i], 0, 0);
|
|
|
tmc2130_wr_TPWMTHRS(tmc2130_cs[i], TMC2130_TPWMTHRS);
|
|
|
//tmc2130_wr_THIGH(tmc2130_cs[i], TMC2130_THIGH);
|
|
@@ -172,64 +180,21 @@ bool tmc2130_update_sg()
|
|
|
uint16_t sg = tmc2130_rd_DRV_STATUS(cs) & 0x3ff;
|
|
|
if (sg == 0)
|
|
|
{
|
|
|
-
|
|
|
tmc2130_axis_stalled[axis] = true;
|
|
|
- tmc2130_LastHomingStalled = true;
|
|
|
#ifdef TMC2130_DEBUG
|
|
|
MYSERIAL.print("tmc2130_update_sg AXIS STALLED ");
|
|
|
MYSERIAL.println((int)axis);
|
|
|
#endif //TMC2130_DEBUG
|
|
|
}
|
|
|
-// else
|
|
|
-// tmc2130_axis_stalled[axis] = false;
|
|
|
}
|
|
|
}
|
|
|
-// else
|
|
|
-// tmc2130_axis_stalled[axis] = false;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return true;
|
|
|
-// else
|
|
|
-// {
|
|
|
-// tmc2130_axis_stalled[X_AXIS] = false;
|
|
|
-// tmc2130_axis_stalled[Y_AXIS] = false;
|
|
|
-// }
|
|
|
#endif
|
|
|
}
|
|
|
|
|
|
-void tmc2130_check_overtemp()
|
|
|
-{
|
|
|
- const static char TMC_OVERTEMP_MSG[] PROGMEM = "TMC DRIVER OVERTEMP ";
|
|
|
- uint8_t cs[4] = { X_TMC2130_CS, Y_TMC2130_CS, Z_TMC2130_CS, E0_TMC2130_CS };
|
|
|
- static uint32_t checktime = 0;
|
|
|
- //drivers_disabled[0] = 1; //TEST
|
|
|
- if( millis() - checktime > 1000 )
|
|
|
- {
|
|
|
- //SERIAL_ECHOLNPGM("drv_status:");
|
|
|
- for(int i=0;i<4;i++)
|
|
|
- {
|
|
|
- uint32_t drv_status = 0;
|
|
|
-
|
|
|
- skip_debug_msg = true;
|
|
|
-
|
|
|
- tmc2130_rd(cs[i], TMC2130_REG_DRV_STATUS, &drv_status);
|
|
|
- //MYSERIAL.print(drv_status);
|
|
|
- //SERIAL_ECHOPGM(" ");
|
|
|
- if (drv_status & ((uint32_t)1<<26))
|
|
|
- { // BIT 26 - over temp prewarning ~120C (+-20C)
|
|
|
- SERIAL_ERRORRPGM(TMC_OVERTEMP_MSG);
|
|
|
- SERIAL_ECHOLN(i);
|
|
|
- for(int i=0; i < 4; i++)
|
|
|
- tmc2130_wr(tmc2130_cs[i], TMC2130_REG_CHOPCONF, 0x00010000);
|
|
|
- kill(TMC_OVERTEMP_MSG);
|
|
|
- }
|
|
|
- }
|
|
|
- //SERIAL_ECHOLNPGM("");
|
|
|
- checktime = millis();
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
void tmc2130_home_enter(uint8_t axes_mask)
|
|
|
{
|
|
|
#ifdef TMC2130_DEBUG
|
|
@@ -247,11 +212,11 @@ void tmc2130_home_enter(uint8_t axes_mask)
|
|
|
tmc2131_axis_sg_pos[axis] = st_get_position(axis);
|
|
|
tmc2130_axis_stalled[axis] = false;
|
|
|
//Configuration to spreadCycle
|
|
|
- tmc2130_wr(cs, TMC2130_REG_GCONF, 0x00000000);
|
|
|
+ tmc2130_wr(cs, TMC2130_REG_GCONF, TMC2130_GCONF_NORMAL);
|
|
|
tmc2130_wr(cs, TMC2130_REG_COOLCONF, ((axis == X_AXIS)?sg_thrs_x:sg_thrs_y) << 16);
|
|
|
tmc2130_wr(cs, TMC2130_REG_TCOOLTHRS, TMC2130_TCOOLTHRS);
|
|
|
#ifndef TMC2130_SG_HOMING_SW
|
|
|
- tmc2130_wr(cs, TMC2130_REG_GCONF, 0x00000080); //stallguard output to DIAG0
|
|
|
+ tmc2130_wr(cs, TMC2130_REG_GCONF, TMC2130_GCONF_SGSENS); //stallguard output DIAG1, DIAG1 = pushpull
|
|
|
#endif //TMC2130_SG_HOMING_SW
|
|
|
}
|
|
|
}
|
|
@@ -273,9 +238,13 @@ void tmc2130_home_exit()
|
|
|
if (sg_homing_axes_mask & mask)
|
|
|
{
|
|
|
if (tmc2130_mode == TMC2130_MODE_SILENT)
|
|
|
- tmc2130_wr(tmc2130_cs[axis], TMC2130_REG_GCONF, 0x00000004); // Configuration back to stealthChop
|
|
|
+ tmc2130_wr(tmc2130_cs[axis], TMC2130_REG_GCONF, TMC2130_GCONF_SILENT); // Configuration back to stealthChop
|
|
|
else
|
|
|
- tmc2130_wr(tmc2130_cs[axis], TMC2130_REG_GCONF, 0x00000000);
|
|
|
+#ifdef TMC2130_SG_HOMING_SW
|
|
|
+ tmc2130_wr(tmc2130_cs[axis], TMC2130_REG_GCONF, TMC2130_GCONF_NORMAL);
|
|
|
+#else //TMC2130_SG_HOMING_SW
|
|
|
+ tmc2130_wr(tmc2130_cs[axis], TMC2130_REG_GCONF, TMC2130_GCONF_SGSENS);
|
|
|
+#endif //TMC2130_SG_HOMING_SW
|
|
|
}
|
|
|
tmc2130_axis_stalled[axis] = false;
|
|
|
}
|
|
@@ -290,11 +259,28 @@ void tmc2130_home_restart(uint8_t axis)
|
|
|
tmc2130_axis_stalled[axis] = false;
|
|
|
}
|
|
|
|
|
|
-uint8_t tmc2130_didLastHomingStall()
|
|
|
+void tmc2130_check_overtemp()
|
|
|
{
|
|
|
- uint8_t ret = tmc2130_LastHomingStalled;
|
|
|
- tmc2130_LastHomingStalled = false;
|
|
|
- return ret;
|
|
|
+ const static char TMC_OVERTEMP_MSG[] PROGMEM = "TMC DRIVER OVERTEMP ";
|
|
|
+ static uint32_t checktime = 0;
|
|
|
+ if (millis() - checktime > 1000 )
|
|
|
+ {
|
|
|
+ for (int i = 0; i < 4; i++)
|
|
|
+ {
|
|
|
+ uint32_t drv_status = 0;
|
|
|
+ skip_debug_msg = true;
|
|
|
+ tmc2130_rd(tmc2130_cs[i], TMC2130_REG_DRV_STATUS, &drv_status);
|
|
|
+ if (drv_status & ((uint32_t)1 << 26))
|
|
|
+ { // BIT 26 - over temp prewarning ~120C (+-20C)
|
|
|
+ SERIAL_ERRORRPGM(TMC_OVERTEMP_MSG);
|
|
|
+ SERIAL_ECHOLN(i);
|
|
|
+ for (int j = 0; j < 4; j++)
|
|
|
+ tmc2130_wr(tmc2130_cs[j], TMC2130_REG_CHOPCONF, 0x00010000);
|
|
|
+ kill(TMC_OVERTEMP_MSG);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ checktime = millis();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
void tmc2130_set_current_h(uint8_t axis, uint8_t current)
|
|
@@ -303,7 +289,6 @@ void tmc2130_set_current_h(uint8_t axis, uint8_t current)
|
|
|
MYSERIAL.print((int)axis);
|
|
|
MYSERIAL.print(" ");
|
|
|
MYSERIAL.println((int)current);
|
|
|
-// if (current > 15) current = 15; //current>15 is unsafe
|
|
|
tmc2130_current_h[axis] = current;
|
|
|
tmc2130_wr(tmc2130_cs[axis], TMC2130_REG_IHOLD_IRUN, 0x000f0000 | ((tmc2130_current_r[axis] & 0x1f) << 8) | (tmc2130_current_h[axis] & 0x1f));
|
|
|
}
|
|
@@ -314,7 +299,6 @@ void tmc2130_set_current_r(uint8_t axis, uint8_t current)
|
|
|
MYSERIAL.print((int)axis);
|
|
|
MYSERIAL.print(" ");
|
|
|
MYSERIAL.println((int)current);
|
|
|
-// if (current > 15) current = 15; //current>15 is unsafe
|
|
|
tmc2130_current_r[axis] = current;
|
|
|
tmc2130_wr(tmc2130_cs[axis], TMC2130_REG_IHOLD_IRUN, 0x000f0000 | ((tmc2130_current_r[axis] & 0x1f) << 8) | (tmc2130_current_h[axis] & 0x1f));
|
|
|
}
|
|
@@ -431,7 +415,7 @@ void tmc2130_wr_THIGH(uint8_t cs, uint32_t val32)
|
|
|
tmc2130_wr(cs, TMC2130_REG_THIGH, val32);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+#ifdef TMC2130_DEBUG
|
|
|
uint8_t tmc2130_axis_by_cs(uint8_t cs)
|
|
|
{
|
|
|
switch (cs)
|
|
@@ -443,6 +427,7 @@ uint8_t tmc2130_axis_by_cs(uint8_t cs)
|
|
|
}
|
|
|
return -1;
|
|
|
}
|
|
|
+#endif //TMC2130_DEBUG
|
|
|
|
|
|
uint8_t tmc2130_mres(uint16_t microstep_resolution)
|
|
|
{
|