|
@@ -11,6 +11,8 @@ extern void st_get_position_xy(long &x, long &y);
|
|
|
|
|
|
|
|
|
uint8_t tmc2130_cs[4] = { X_TMC2130_CS, Y_TMC2130_CS, Z_TMC2130_CS, E0_TMC2130_CS };
|
|
|
+
|
|
|
+uint8_t tmc2130_mode = TMC2130_MODE_NORMAL;
|
|
|
|
|
|
uint8_t tmc2130_current_h[4] = TMC2130_CURRENTS_H;
|
|
|
|
|
@@ -22,23 +24,62 @@ uint8_t tmc2130_LastHomingStalled = 0;
|
|
|
|
|
|
uint8_t sg_homing_axis = 0xff;
|
|
|
uint8_t sg_homing_delay = 0;
|
|
|
+uint8_t sg_thrs_x = TMC2130_SG_THRS_X;
|
|
|
+uint8_t sg_thrs_y = TMC2130_SG_THRS_Y;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#define TMC2130_REG_GCONF 0x00
|
|
|
+#define TMC2130_REG_GSTAT 0x01
|
|
|
+#define TMC2130_REG_IOIN 0x04
|
|
|
+#define TMC2130_REG_IHOLD_IRUN 0x10
|
|
|
+#define TMC2130_REG_TPOWERDOWN 0x11
|
|
|
+#define TMC2130_REG_TSTEP 0x12
|
|
|
+#define TMC2130_REG_TPWMTHRS 0x13
|
|
|
+#define TMC2130_REG_TCOOLTHRS 0x14
|
|
|
+#define TMC2130_REG_THIGH 0x15
|
|
|
+#define TMC2130_REG_XDIRECT 0x2d
|
|
|
+#define TMC2130_REG_VDCMIN 0x33
|
|
|
+#define TMC2130_REG_MSLUT0 0x60
|
|
|
+#define TMC2130_REG_MSLUT1 0x61
|
|
|
+#define TMC2130_REG_MSLUT2 0x62
|
|
|
+#define TMC2130_REG_MSLUT3 0x63
|
|
|
+#define TMC2130_REG_MSLUT4 0x64
|
|
|
+#define TMC2130_REG_MSLUT5 0x65
|
|
|
+#define TMC2130_REG_MSLUT6 0x66
|
|
|
+#define TMC2130_REG_MSLUT7 0x67
|
|
|
+#define TMC2130_REG_MSLUTSEL 0x68
|
|
|
+#define TMC2130_REG_MSLUTSTART 0x69
|
|
|
+#define TMC2130_REG_MSCNT 0x6a
|
|
|
+#define TMC2130_REG_MSCURACT 0x6b
|
|
|
+#define TMC2130_REG_CHOPCONF 0x6c
|
|
|
+#define TMC2130_REG_COOLCONF 0x6d
|
|
|
+#define TMC2130_REG_DCCTRL 0x6e
|
|
|
+#define TMC2130_REG_DRV_STATUS 0x6f
|
|
|
+#define TMC2130_REG_PWMCONF 0x70
|
|
|
+#define TMC2130_REG_PWM_SCALE 0x71
|
|
|
+#define TMC2130_REG_ENCM_CTRL 0x72
|
|
|
+#define TMC2130_REG_LOST_STEPS 0x73
|
|
|
+
|
|
|
+
|
|
|
+uint16_t tmc2130_rd_TSTEP(uint8_t cs);
|
|
|
+uint16_t tmc2130_rd_DRV_STATUS(uint8_t chipselect);
|
|
|
+
|
|
|
+void tmc2130_wr_CHOPCONF(uint8_t cs, bool extrapolate256 = 0, uint16_t microstep_resolution = 16);
|
|
|
+void tmc2130_wr_PWMCONF(uint8_t cs, uint8_t PWMautoScale = TMC2130_PWM_AUTO, uint8_t PWMfreq = TMC2130_PWM_FREQ, uint8_t PWMgrad = TMC2130_PWM_GRAD, uint8_t PWMampl = TMC2130_PWM_AMPL);
|
|
|
+void tmc2130_wr_TPWMTHRS(uint8_t cs, uint32_t val32);
|
|
|
+void tmc2130_wr_THIGH(uint8_t cs, uint32_t val32);
|
|
|
+
|
|
|
+uint8_t tmc2130_txrx(uint8_t cs, uint8_t addr, uint32_t wval, uint32_t* rval);
|
|
|
+uint8_t tmc2130_wr(uint8_t cs, uint8_t addr, uint32_t wval);
|
|
|
+uint8_t tmc2130_rd(uint8_t cs, uint8_t addr, uint32_t* rval);
|
|
|
|
|
|
|
|
|
-uint32_t tmc2130_read(uint8_t cs, uint8_t address);
|
|
|
-void tmc2130_write(uint8_t cs, uint8_t address, uint8_t wval1, uint8_t wval2, uint8_t wval3, uint8_t wval4);
|
|
|
-uint8_t tmc2130_read8(uint8_t cs, uint8_t address);
|
|
|
-uint32_t tmc2130_readRegister(uint8_t cs, uint8_t address);
|
|
|
-uint16_t tmc2130_readSG(uint8_t cs);
|
|
|
-uint16_t tmc2130_readTStep(uint8_t cs);
|
|
|
-void tmc2130_chopconf(uint8_t cs, bool extrapolate256 = 0, uint16_t microstep_resolution = 16);
|
|
|
-void tmc2130_PWMconf(uint8_t cs, uint8_t PWMautoScale = PWM_AUTOSCALE, uint8_t PWMfreq = PWM_FREQ, uint8_t PWMgrad = PWM_GRAD, uint8_t PWMampl = PWM_AMPL);
|
|
|
-void tmc2130_PWMthreshold(uint8_t cs);
|
|
|
-void tmc2130_disable_motor(uint8_t driver);
|
|
|
-
|
|
|
|
|
|
void tmc2130_init()
|
|
|
{
|
|
|
- MYSERIAL.println("tmc2130_init");
|
|
|
+ MYSERIAL.print("tmc2130_init mode=");
|
|
|
+ MYSERIAL.println(tmc2130_mode, DEC);
|
|
|
WRITE(X_TMC2130_CS, HIGH);
|
|
|
WRITE(Y_TMC2130_CS, HIGH);
|
|
|
WRITE(Z_TMC2130_CS, HIGH);
|
|
@@ -48,37 +89,49 @@ void tmc2130_init()
|
|
|
SET_OUTPUT(Z_TMC2130_CS);
|
|
|
SET_OUTPUT(E0_TMC2130_CS);
|
|
|
SPI.begin();
|
|
|
- for (int i = 0; i < 3; i++)
|
|
|
+ for (int i = 0; i < 2; i++)
|
|
|
+ {
|
|
|
+ tmc2130_wr(tmc2130_cs[i], TMC2130_REG_GCONF, (tmc2130_mode == TMC2130_MODE_SILENT)?0x00000004:0x00000000);
|
|
|
+ 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_PWMCONF(tmc2130_cs[i]);
|
|
|
+ tmc2130_wr_TPWMTHRS(tmc2130_cs[i], TMC2130_TPWMTHRS);
|
|
|
+
|
|
|
+ tmc2130_wr_CHOPCONF(tmc2130_cs[i], TMC2130_EXP256_XY, TMC2130_USTEPS_XY);
|
|
|
+ }
|
|
|
+ for (int i = 2; i < 3; i++)
|
|
|
{
|
|
|
- tmc2130_write(tmc2130_cs[i], 0x00, 0, 0, 0, 0x04);
|
|
|
- tmc2130_write(tmc2130_cs[i], 0x10, 0, 15, tmc2130_current_r[i], tmc2130_current_h[i]);
|
|
|
- tmc2130_write(tmc2130_cs[i], 0x11, 0, 0, 0, 0);
|
|
|
- tmc2130_PWMconf(tmc2130_cs[i]);
|
|
|
-
|
|
|
- tmc2130_chopconf(tmc2130_cs[i], 1, 16);
|
|
|
+ tmc2130_wr(tmc2130_cs[i], TMC2130_REG_GCONF, (tmc2130_mode == TMC2130_MODE_SILENT)?0x00000004:0x00000000);
|
|
|
+ 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_PWMCONF(tmc2130_cs[i]);
|
|
|
+ tmc2130_wr_TPWMTHRS(tmc2130_cs[i], TMC2130_TPWMTHRS);
|
|
|
+
|
|
|
+ tmc2130_wr_CHOPCONF(tmc2130_cs[i], TMC2130_EXP256_Z, TMC2130_USTEPS_Z);
|
|
|
}
|
|
|
- for (int i = 3; i < 4; i++)
|
|
|
+ for (int i = 3; i < 4; i++)
|
|
|
{
|
|
|
- tmc2130_write(tmc2130_cs[i], 0x00, 0, 0, 0, 0x00);
|
|
|
- tmc2130_write(tmc2130_cs[i], 0x10, 0, 15, tmc2130_current_r[i], tmc2130_current_h[i]);
|
|
|
- tmc2130_write(tmc2130_cs[i], 0x11, 0, 0, 0, 0);
|
|
|
- tmc2130_chopconf(tmc2130_cs[i], 1, 16);
|
|
|
+ tmc2130_wr(tmc2130_cs[i], TMC2130_REG_GCONF, 0x00000004);
|
|
|
+ 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_CHOPCONF(tmc2130_cs[i], TMC2130_EXP256_E, TMC2130_USTEPS_E);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
bool tmc2130_update_sg()
|
|
|
{
|
|
|
+#if (defined(TMC2130_SG_HOMING) && defined(TMC2130_SG_HOMING_SW))
|
|
|
if ((sg_homing_axis == X_AXIS) || (sg_homing_axis == Y_AXIS))
|
|
|
{
|
|
|
uint8_t cs = tmc2130_cs[sg_homing_axis];
|
|
|
- uint16_t tstep = tmc2130_readTStep(cs);
|
|
|
- if (tstep < TCOOLTHRS)
|
|
|
+ uint16_t tstep = tmc2130_rd_TSTEP(cs);
|
|
|
+ if (tstep < TMC2130_TCOOLTHRS)
|
|
|
{
|
|
|
if(sg_homing_delay < 10)
|
|
|
sg_homing_delay++;
|
|
|
else
|
|
|
{
|
|
|
- uint16_t sg = tmc2130_readSG(cs);
|
|
|
+ uint16_t sg = tmc2130_rd_DRV_STATUS(cs) & 0x3ff;
|
|
|
if (sg==0)
|
|
|
{
|
|
|
tmc2130_axis_stalled[sg_homing_axis] = true;
|
|
@@ -97,25 +150,29 @@ bool tmc2130_update_sg()
|
|
|
tmc2130_axis_stalled[X_AXIS] = false;
|
|
|
tmc2130_axis_stalled[Y_AXIS] = false;
|
|
|
}
|
|
|
+#endif
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
|
if( millis() - checktime > 1000 )
|
|
|
{
|
|
|
- for(int i = 0; i < 4; i++)
|
|
|
+ for(int i=0;i<4;i++)
|
|
|
{
|
|
|
- uint32_t drv_status = tmc2130_read(tmc2130_cs[i], 0x6F);
|
|
|
+ uint32_t drv_status = 0;
|
|
|
+ tmc2130_rd(cs[i], TMC2130_REG_DRV_STATUS, &drv_status);
|
|
|
if (drv_status & ((uint32_t)1<<26))
|
|
|
{
|
|
|
SERIAL_ERRORRPGM(TMC_OVERTEMP_MSG);
|
|
|
SERIAL_ECHOLN(i);
|
|
|
- for(int x = 0; x < 4; x++) tmc2130_disable_motor(x);
|
|
|
- kill(TMC_OVERTEMP_MSG);
|
|
|
+ for(int i=0; i < 4; i++)
|
|
|
+ tmc2130_wr(tmc2130_cs[i], TMC2130_REG_CHOPCONF, 0x00010000);
|
|
|
+ kill(TMC_OVERTEMP_MSG);
|
|
|
}
|
|
|
}
|
|
|
checktime = millis();
|
|
@@ -126,27 +183,36 @@ void tmc2130_home_enter(uint8_t axis)
|
|
|
{
|
|
|
MYSERIAL.print("tmc2130_home_enter ");
|
|
|
MYSERIAL.println((int)axis);
|
|
|
+#ifdef TMC2130_SG_HOMING
|
|
|
uint8_t cs = tmc2130_cs[axis];
|
|
|
sg_homing_axis = axis;
|
|
|
sg_homing_delay = 0;
|
|
|
tmc2130_axis_stalled[X_AXIS] = false;
|
|
|
tmc2130_axis_stalled[Y_AXIS] = false;
|
|
|
|
|
|
-
|
|
|
- tmc2130_write(cs, 0x0, 0, 0, 0, 0x00);
|
|
|
- tmc2130_write(cs, 0x6D, 0, (axis == X_AXIS)?SG_THRESHOLD_X:SG_THRESHOLD_Y,0,0);
|
|
|
- tmc2130_write(cs, 0x14, 0, 0, 0, TCOOLTHRS);
|
|
|
+ tmc2130_wr(cs, TMC2130_REG_GCONF, 0x00000000);
|
|
|
+ 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);
|
|
|
+#endif
|
|
|
+#endif
|
|
|
}
|
|
|
|
|
|
void tmc2130_home_exit()
|
|
|
{
|
|
|
- MYSERIAL.println("tmc2130_home_exit");
|
|
|
+ MYSERIAL.println("tmc2130_home_exit ");
|
|
|
+ MYSERIAL.println((int)sg_homing_axis);
|
|
|
+#ifdef TMC2130_SG_HOMING
|
|
|
if ((sg_homing_axis == X_AXIS) || (sg_homing_axis == Y_AXIS))
|
|
|
{
|
|
|
-
|
|
|
- tmc2130_write(tmc2130_cs[sg_homing_axis], 0x0, 0, 0, 0, 0x04);
|
|
|
+ if (tmc2130_mode == TMC2130_MODE_SILENT)
|
|
|
+ tmc2130_wr(tmc2130_cs[sg_homing_axis], TMC2130_REG_GCONF, 0x00000004);
|
|
|
+ else
|
|
|
+ tmc2130_wr(tmc2130_cs[sg_homing_axis], TMC2130_REG_GCONF, 0x00000000);
|
|
|
sg_homing_axis = 0xff;
|
|
|
}
|
|
|
+#endif
|
|
|
}
|
|
|
|
|
|
extern uint8_t tmc2130_didLastHomingStall()
|
|
@@ -164,7 +230,7 @@ void tmc2130_set_current_h(uint8_t axis, uint8_t current)
|
|
|
MYSERIAL.println((int)current);
|
|
|
if (current > 15) current = 15;
|
|
|
tmc2130_current_h[axis] = current;
|
|
|
- tmc2130_write(tmc2130_cs[axis], 0x10, 0, 15, tmc2130_current_r[axis], tmc2130_current_h[axis]);
|
|
|
+ tmc2130_wr(tmc2130_cs[axis], TMC2130_REG_IHOLD_IRUN, 0x000f0000 | ((tmc2130_current_r[axis] & 0x1f) << 8) | (tmc2130_current_h[axis] & 0x1f));
|
|
|
}
|
|
|
|
|
|
void tmc2130_set_current_r(uint8_t axis, uint8_t current)
|
|
@@ -175,230 +241,151 @@ void tmc2130_set_current_r(uint8_t axis, uint8_t current)
|
|
|
MYSERIAL.println((int)current);
|
|
|
if (current > 15) current = 15;
|
|
|
tmc2130_current_r[axis] = current;
|
|
|
- tmc2130_write(tmc2130_cs[axis], 0x10, 0, 15, tmc2130_current_r[axis], tmc2130_current_h[axis]);
|
|
|
+ tmc2130_wr(tmc2130_cs[axis], TMC2130_REG_IHOLD_IRUN, 0x000f0000 | ((tmc2130_current_r[axis] & 0x1f) << 8) | (tmc2130_current_h[axis] & 0x1f));
|
|
|
}
|
|
|
|
|
|
void tmc2130_print_currents()
|
|
|
{
|
|
|
MYSERIAL.println("tmc2130_print_currents");
|
|
|
MYSERIAL.println("\tH\rR");
|
|
|
-
|
|
|
MYSERIAL.print("X\t");
|
|
|
MYSERIAL.print((int)tmc2130_current_h[0]);
|
|
|
MYSERIAL.print("\t");
|
|
|
MYSERIAL.println((int)tmc2130_current_r[0]);
|
|
|
-
|
|
|
MYSERIAL.print("Y\t");
|
|
|
MYSERIAL.print((int)tmc2130_current_h[1]);
|
|
|
MYSERIAL.print("\t");
|
|
|
MYSERIAL.println((int)tmc2130_current_r[1]);
|
|
|
-
|
|
|
MYSERIAL.print("Z\t");
|
|
|
MYSERIAL.print((int)tmc2130_current_h[2]);
|
|
|
MYSERIAL.print("\t");
|
|
|
MYSERIAL.println((int)tmc2130_current_r[2]);
|
|
|
-
|
|
|
MYSERIAL.print("E\t");
|
|
|
MYSERIAL.print((int)tmc2130_current_h[3]);
|
|
|
MYSERIAL.print("\t");
|
|
|
MYSERIAL.println((int)tmc2130_current_r[3]);
|
|
|
}
|
|
|
|
|
|
-uint32_t tmc2130_read(uint8_t cs, uint8_t address)
|
|
|
+uint16_t tmc2130_rd_TSTEP(uint8_t cs)
|
|
|
{
|
|
|
- uint32_t val32;
|
|
|
- uint8_t val0;
|
|
|
- uint8_t val1;
|
|
|
- uint8_t val2;
|
|
|
- uint8_t val3;
|
|
|
- uint8_t val4;
|
|
|
-
|
|
|
- SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE3));
|
|
|
- digitalWrite(cs,LOW);
|
|
|
- SPI.transfer(address);
|
|
|
- SPI.transfer(0);
|
|
|
- SPI.transfer(0);
|
|
|
- SPI.transfer(0);
|
|
|
- SPI.transfer(0);
|
|
|
- digitalWrite(cs, HIGH);
|
|
|
- SPI.endTransaction();
|
|
|
-
|
|
|
- SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE3));
|
|
|
- digitalWrite(cs,LOW);
|
|
|
- val0 = SPI.transfer(0);
|
|
|
- val1 = SPI.transfer(0);
|
|
|
- val2 = SPI.transfer(0);
|
|
|
- val3 = SPI.transfer(0);
|
|
|
- val4 = SPI.transfer(0);
|
|
|
- digitalWrite(cs, HIGH);
|
|
|
- SPI.endTransaction();
|
|
|
-#ifdef TMC_DBG_READS
|
|
|
- MYSERIAL.print("SPIRead 0x");
|
|
|
- MYSERIAL.print(address,HEX);
|
|
|
- MYSERIAL.print(" Status:");
|
|
|
- MYSERIAL.print(val0 & 0b00000111,BIN);
|
|
|
- MYSERIAL.print(" ");
|
|
|
- MYSERIAL.print(val1,BIN);
|
|
|
- MYSERIAL.print(" ");
|
|
|
- MYSERIAL.print(val2,BIN);
|
|
|
- MYSERIAL.print(" ");
|
|
|
- MYSERIAL.print(val3,BIN);
|
|
|
- MYSERIAL.print(" ");
|
|
|
- MYSERIAL.print(val4,BIN);
|
|
|
-#endif
|
|
|
- val32 = (uint32_t)val1<<24 | (uint32_t)val2<<16 | (uint32_t)val3<<8 | (uint32_t)val4;
|
|
|
-#ifdef TMC_DBG_READS
|
|
|
- MYSERIAL.print(" 0x");
|
|
|
- MYSERIAL.println(val32,HEX);
|
|
|
-#endif
|
|
|
- return val32;
|
|
|
+ uint32_t val32 = 0;
|
|
|
+ tmc2130_rd(cs, TMC2130_REG_TSTEP, &val32);
|
|
|
+ if (val32 & 0x000f0000) return 0xffff;
|
|
|
+ return val32 & 0xffff;
|
|
|
}
|
|
|
|
|
|
-void tmc2130_write(uint8_t cs, uint8_t address,uint8_t wval1,uint8_t wval2,uint8_t wval3,uint8_t wval4)
|
|
|
+uint16_t tmc2130_rd_DRV_STATUS(uint8_t cs)
|
|
|
{
|
|
|
- uint32_t val32;
|
|
|
- uint8_t val0;
|
|
|
- uint8_t val1;
|
|
|
- uint8_t val2;
|
|
|
- uint8_t val3;
|
|
|
- uint8_t val4;
|
|
|
-
|
|
|
- SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE3));
|
|
|
- digitalWrite(cs,LOW);
|
|
|
- SPI.transfer(address+0x80);
|
|
|
- SPI.transfer(wval1);
|
|
|
- SPI.transfer(wval2);
|
|
|
- SPI.transfer(wval3);
|
|
|
- SPI.transfer(wval4);
|
|
|
- digitalWrite(cs, HIGH);
|
|
|
- SPI.endTransaction();
|
|
|
-
|
|
|
- SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE3));
|
|
|
- digitalWrite(cs,LOW);
|
|
|
- val0 = SPI.transfer(0);
|
|
|
- val1 = SPI.transfer(0);
|
|
|
- val2 = SPI.transfer(0);
|
|
|
- val3 = SPI.transfer(0);
|
|
|
- val4 = SPI.transfer(0);
|
|
|
- digitalWrite(cs, HIGH);
|
|
|
- SPI.endTransaction();
|
|
|
-#ifdef TMC_DBG_WRITE
|
|
|
- MYSERIAL.print("WriteRead 0x");
|
|
|
- MYSERIAL.print(address,HEX);
|
|
|
- MYSERIAL.print(" Status:");
|
|
|
- MYSERIAL.print(val0 & 0b00000111,BIN);
|
|
|
- MYSERIAL.print(" ");
|
|
|
- MYSERIAL.print(val1,BIN);
|
|
|
- MYSERIAL.print(" ");
|
|
|
- MYSERIAL.print(val2,BIN);
|
|
|
- MYSERIAL.print(" ");
|
|
|
- MYSERIAL.print(val3,BIN);
|
|
|
- MYSERIAL.print(" ");
|
|
|
- MYSERIAL.print(val4,BIN);
|
|
|
- val32 = (uint32_t)val1<<24 | (uint32_t)val2<<16 | (uint32_t)val3<<8 | (uint32_t)val4;
|
|
|
- MYSERIAL.print(" 0x");
|
|
|
- MYSERIAL.println(val32,HEX);
|
|
|
-#endif
|
|
|
+ uint32_t val32 = 0;
|
|
|
+ tmc2130_rd(cs, TMC2130_REG_DRV_STATUS, &val32);
|
|
|
+ return val32;
|
|
|
}
|
|
|
|
|
|
-uint8_t tmc2130_read8(uint8_t cs, uint8_t address)
|
|
|
+void tmc2130_wr_CHOPCONF(uint8_t cs, bool extrapolate256, uint16_t microstep_resolution)
|
|
|
{
|
|
|
-
|
|
|
- SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE3));
|
|
|
- digitalWrite(cs,LOW);
|
|
|
- SPI.transfer(address);
|
|
|
- SPI.transfer(0x00);
|
|
|
- SPI.transfer(0x00);
|
|
|
- SPI.transfer(0x00);
|
|
|
- SPI.transfer(0x00);
|
|
|
- digitalWrite(cs, HIGH);
|
|
|
- SPI.endTransaction();
|
|
|
- uint8_t val0;
|
|
|
-
|
|
|
- SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE3));
|
|
|
- digitalWrite(cs,LOW);
|
|
|
- val0 = SPI.transfer(0);
|
|
|
- digitalWrite(cs, HIGH);
|
|
|
- SPI.endTransaction();
|
|
|
- return val0;
|
|
|
+ uint8_t mres=0b0100;
|
|
|
+ if(microstep_resolution == 256) mres = 0b0000;
|
|
|
+ if(microstep_resolution == 128) mres = 0b0001;
|
|
|
+ if(microstep_resolution == 64) mres = 0b0010;
|
|
|
+ if(microstep_resolution == 32) mres = 0b0011;
|
|
|
+ if(microstep_resolution == 16) mres = 0b0100;
|
|
|
+ if(microstep_resolution == 8) mres = 0b0101;
|
|
|
+ if(microstep_resolution == 4) mres = 0b0110;
|
|
|
+ if(microstep_resolution == 2) mres = 0b0111;
|
|
|
+ if(microstep_resolution == 1) mres = 0b1000;
|
|
|
+ mres |= extrapolate256 << 4;
|
|
|
+
|
|
|
+
|
|
|
+ tmc2130_wr(cs,TMC2130_REG_CHOPCONF,((uint32_t)mres << 24) | 0x0100C3);
|
|
|
}
|
|
|
|
|
|
-uint32_t tmc2130_readRegister(uint8_t cs, uint8_t address)
|
|
|
+void tmc2130_wr_PWMCONF(uint8_t cs, uint8_t PWMautoScale, uint8_t PWMfreq, uint8_t PWMgrad, uint8_t PWMampl)
|
|
|
{
|
|
|
-
|
|
|
- SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE3));
|
|
|
- digitalWrite(cs,LOW);
|
|
|
- SPI.transfer(address);
|
|
|
- SPI.transfer(0x00);
|
|
|
- SPI.transfer(0x00);
|
|
|
- SPI.transfer(0x00);
|
|
|
- SPI.transfer(0x00);
|
|
|
- digitalWrite(cs, HIGH);
|
|
|
- SPI.endTransaction();
|
|
|
- uint32_t val0;
|
|
|
-
|
|
|
- SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE3));
|
|
|
- digitalWrite(cs,LOW);
|
|
|
- SPI.transfer(0);
|
|
|
- val0 = SPI.transfer(0);
|
|
|
- val0 = (val0 << 8) | SPI.transfer(0);
|
|
|
- val0 = (val0 << 8) | SPI.transfer(0);
|
|
|
- val0 = (val0 << 8) | SPI.transfer(0);
|
|
|
- digitalWrite(cs, HIGH);
|
|
|
- SPI.endTransaction();
|
|
|
- return val0;
|
|
|
+ tmc2130_wr(cs, TMC2130_REG_PWMCONF, ((uint32_t)(PWMautoScale+PWMfreq) << 16) | ((uint32_t)PWMgrad << 8) | PWMampl);
|
|
|
}
|
|
|
|
|
|
-uint16_t tmc2130_readSG(uint8_t cs)
|
|
|
+void tmc2130_wr_TPWMTHRS(uint8_t cs, uint32_t val32)
|
|
|
{
|
|
|
- uint8_t address = 0x6F;
|
|
|
- uint32_t registerValue = tmc2130_readRegister(cs, address);
|
|
|
- uint16_t val0 = registerValue & 0x3ff;
|
|
|
- return val0;
|
|
|
+ tmc2130_wr(cs, TMC2130_REG_TPWMTHRS, val32);
|
|
|
}
|
|
|
|
|
|
-uint16_t tmc2130_readTStep(uint8_t cs)
|
|
|
+void tmc2130_wr_THIGH(uint8_t cs, uint32_t val32)
|
|
|
{
|
|
|
- uint8_t address = 0x12;
|
|
|
- uint32_t registerValue = tmc2130_readRegister(cs, address);
|
|
|
- uint16_t val0 = 0;
|
|
|
- if(registerValue & 0x000f0000)
|
|
|
- val0 = 0xffff;
|
|
|
- else
|
|
|
- val0 = registerValue & 0xffff;
|
|
|
- return val0;
|
|
|
+ tmc2130_wr(cs, TMC2130_REG_THIGH, val32);
|
|
|
}
|
|
|
|
|
|
-void tmc2130_chopconf(uint8_t cs, bool extrapolate256, uint16_t microstep_resolution)
|
|
|
+
|
|
|
+uint8_t tmc2130_axis_by_cs(uint8_t cs)
|
|
|
{
|
|
|
- uint8_t mres = 0b0100;
|
|
|
- if(microstep_resolution == 256) mres = 0b0000;
|
|
|
- if(microstep_resolution == 128) mres = 0b0001;
|
|
|
- if(microstep_resolution == 64) mres = 0b0010;
|
|
|
- if(microstep_resolution == 32) mres = 0b0011;
|
|
|
- if(microstep_resolution == 16) mres = 0b0100;
|
|
|
- if(microstep_resolution == 8) mres = 0b0101;
|
|
|
- if(microstep_resolution == 4) mres = 0b0110;
|
|
|
- if(microstep_resolution == 2) mres = 0b0111;
|
|
|
- if(microstep_resolution == 1) mres = 0b1000;
|
|
|
- mres |= extrapolate256 << 4;
|
|
|
-
|
|
|
- tmc2130_write(cs, 0x6C, mres, 0x01, 0x00, 0xC3);
|
|
|
+ switch (cs)
|
|
|
+ {
|
|
|
+ case X_TMC2130_CS: return 0;
|
|
|
+ case Y_TMC2130_CS: return 1;
|
|
|
+ case Z_TMC2130_CS: return 2;
|
|
|
+ case E0_TMC2130_CS: return 3;
|
|
|
+ }
|
|
|
+ return -1;
|
|
|
}
|
|
|
|
|
|
-void tmc2130_PWMconf(uint8_t cs, uint8_t PWMautoScale, uint8_t PWMfreq, uint8_t PWMgrad, uint8_t PWMampl)
|
|
|
+uint8_t tmc2130_wr(uint8_t cs, uint8_t addr, uint32_t wval)
|
|
|
{
|
|
|
- tmc2130_write(cs, 0x70, 0x00, (PWMautoScale+PWMfreq), PWMgrad, PWMampl);
|
|
|
+ uint8_t stat = tmc2130_txrx(cs, addr | 0x80, wval, 0);
|
|
|
+#ifdef TMC2130_DEBUG_WR
|
|
|
+ MYSERIAL.print("tmc2130_wr(");
|
|
|
+ MYSERIAL.print((unsigned char)tmc2130_axis_by_cs(cs), DEC);
|
|
|
+ MYSERIAL.print(", 0x");
|
|
|
+ MYSERIAL.print((unsigned char)addr, HEX);
|
|
|
+ MYSERIAL.print(", 0x");
|
|
|
+ MYSERIAL.print((unsigned long)wval, HEX);
|
|
|
+ MYSERIAL.print(")=0x");
|
|
|
+ MYSERIAL.println((unsigned char)stat, HEX);
|
|
|
+#endif
|
|
|
+ return stat;
|
|
|
}
|
|
|
|
|
|
-void tmc2130_PWMthreshold(uint8_t cs)
|
|
|
+uint8_t tmc2130_rd(uint8_t cs, uint8_t addr, uint32_t* rval)
|
|
|
{
|
|
|
- tmc2130_write(cs, 0x13, 0x00, 0x00, 0x00, 0x00);
|
|
|
+ uint32_t val32 = 0;
|
|
|
+ uint8_t stat = tmc2130_txrx(cs, addr, 0x00000000, &val32);
|
|
|
+ if (rval != 0) *rval = val32;
|
|
|
+#ifdef TMC2130_DEBUG_RD
|
|
|
+ MYSERIAL.print("tmc2130_rd(");
|
|
|
+ MYSERIAL.print((unsigned char)tmc2130_axis_by_cs(cs), DEC);
|
|
|
+ MYSERIAL.print(", 0x");
|
|
|
+ MYSERIAL.print((unsigned char)addr, HEX);
|
|
|
+ MYSERIAL.print(", 0x");
|
|
|
+ MYSERIAL.print((unsigned long)val32, HEX);
|
|
|
+ MYSERIAL.print(")=0x");
|
|
|
+ MYSERIAL.println((unsigned char)stat, HEX);
|
|
|
+#endif
|
|
|
+ return stat;
|
|
|
}
|
|
|
|
|
|
-void tmc2130_disable_motor(uint8_t driver)
|
|
|
+uint8_t tmc2130_txrx(uint8_t cs, uint8_t addr, uint32_t wval, uint32_t* rval)
|
|
|
{
|
|
|
- tmc2130_write(tmc2130_cs[driver], 0x6C, 0, 01, 0, 0);
|
|
|
+
|
|
|
+ SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE3));
|
|
|
+ digitalWrite(cs, LOW);
|
|
|
+ SPI.transfer(addr);
|
|
|
+ SPI.transfer((wval >> 24) & 0xff);
|
|
|
+ SPI.transfer((wval >> 16) & 0xff);
|
|
|
+ SPI.transfer((wval >> 8) & 0xff);
|
|
|
+ SPI.transfer(wval & 0xff);
|
|
|
+ digitalWrite(cs, HIGH);
|
|
|
+ SPI.endTransaction();
|
|
|
+
|
|
|
+ SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE3));
|
|
|
+ digitalWrite(cs, LOW);
|
|
|
+ uint8_t stat = SPI.transfer(0);
|
|
|
+ uint32_t val32 = 0;
|
|
|
+ val32 = SPI.transfer(0);
|
|
|
+ val32 = (val32 << 8) | SPI.transfer(0);
|
|
|
+ val32 = (val32 << 8) | SPI.transfer(0);
|
|
|
+ val32 = (val32 << 8) | SPI.transfer(0);
|
|
|
+ digitalWrite(cs, HIGH);
|
|
|
+ SPI.endTransaction();
|
|
|
+ if (rval != 0) *rval = val32;
|
|
|
+ return stat;
|
|
|
}
|
|
|
|
|
|
#endif
|