tmc2130.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. #include "Marlin.h"
  2. #ifdef HAVE_TMC2130_DRIVERS
  3. #include "tmc2130.h"
  4. #include <SPI.h>
  5. //externals for debuging
  6. extern float current_position[4];
  7. extern void st_get_position_xy(long &x, long &y);
  8. //chipselect pins
  9. uint8_t tmc2130_cs[4] = { X_TMC2130_CS, Y_TMC2130_CS, Z_TMC2130_CS, E0_TMC2130_CS };
  10. //holding currents
  11. uint8_t tmc2130_current_h[4] = TMC2130_CURRENTS_H;
  12. //running currents
  13. uint8_t tmc2130_current_r[4] = TMC2130_CURRENTS_R;
  14. //axis stalled flags
  15. uint8_t tmc2130_axis_stalled[4] = {0, 0, 0, 0};
  16. //last homing stalled
  17. uint8_t tmc2130_LastHomingStalled = 0;
  18. uint8_t sg_homing_axis = 0xff;
  19. uint8_t sg_homing_delay = 0;
  20. //TMC2130 registers
  21. #define TMC2130_REG_GCONF 0x00 // 17 bits
  22. #define TMC2130_REG_GSTAT 0x01 // 3 bits
  23. #define TMC2130_REG_IOIN 0x04 // 8+8 bits
  24. #define TMC2130_REG_IHOLD_IRUN 0x10 // 5+5+4 bits
  25. #define TMC2130_REG_TPOWERDOWN 0x11 // 8 bits
  26. #define TMC2130_REG_TSTEP 0x12 // 20 bits
  27. #define TMC2130_REG_TPWMTHRS 0x13 // 20 bits
  28. #define TMC2130_REG_TCOOLTHRS 0x14 // 20 bits
  29. #define TMC2130_REG_THIGH 0x15 // 20 bits
  30. #define TMC2130_REG_XDIRECT 0x2d // 32 bits
  31. #define TMC2130_REG_VDCMIN 0x33 // 23 bits
  32. #define TMC2130_REG_MSLUT0 0x60 // 32 bits
  33. #define TMC2130_REG_MSLUT1 0x61 // 32 bits
  34. #define TMC2130_REG_MSLUT2 0x62 // 32 bits
  35. #define TMC2130_REG_MSLUT3 0x63 // 32 bits
  36. #define TMC2130_REG_MSLUT4 0x64 // 32 bits
  37. #define TMC2130_REG_MSLUT5 0x65 // 32 bits
  38. #define TMC2130_REG_MSLUT6 0x66 // 32 bits
  39. #define TMC2130_REG_MSLUT7 0x67 // 32 bits
  40. #define TMC2130_REG_MSLUTSEL 0x68 // 32 bits
  41. #define TMC2130_REG_MSLUTSTART 0x69 // 8+8 bits
  42. #define TMC2130_REG_MSCNT 0x6a // 10 bits
  43. #define TMC2130_REG_MSCURACT 0x6b // 9+9 bits
  44. #define TMC2130_REG_CHOPCONF 0x6c // 32 bits
  45. #define TMC2130_REG_COOLCONF 0x6d // 25 bits
  46. #define TMC2130_REG_DCCTRL 0x6e // 24 bits
  47. #define TMC2130_REG_DRV_STATUS 0x6f // 32 bits
  48. #define TMC2130_REG_PWMCONF 0x70 // 22 bits
  49. #define TMC2130_REG_PWM_SCALE 0x71 // 8 bits
  50. #define TMC2130_REG_ENCM_CTRL 0x72 // 2 bits
  51. #define TMC2130_REG_LOST_STEPS 0x73 // 20 bits
  52. uint16_t tmc2130_rd_TSTEP(uint8_t cs);
  53. uint16_t tmc2130_rd_DRV_STATUS(uint8_t chipselect);
  54. void tmc2130_wr_CHOPCONF(uint8_t cs, bool extrapolate256 = 0, uint16_t microstep_resolution = 16);
  55. 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);
  56. void tmc2130_wr_TPWMTHRS(uint8_t cs, uint32_t val32);
  57. void tmc2130_wr_THIGH(uint8_t cs, uint32_t val32);
  58. uint8_t tmc2130_txrx(uint8_t cs, uint8_t addr, uint32_t wval, uint32_t* rval);
  59. uint8_t tmc2130_wr(uint8_t cs, uint8_t addr, uint32_t wval);
  60. uint8_t tmc2130_rd(uint8_t cs, uint8_t addr, uint32_t* rval);
  61. void tmc2130_init()
  62. {
  63. MYSERIAL.println("tmc2130_init");
  64. WRITE(X_TMC2130_CS, HIGH);
  65. WRITE(Y_TMC2130_CS, HIGH);
  66. WRITE(Z_TMC2130_CS, HIGH);
  67. WRITE(E0_TMC2130_CS, HIGH);
  68. SET_OUTPUT(X_TMC2130_CS);
  69. SET_OUTPUT(Y_TMC2130_CS);
  70. SET_OUTPUT(Z_TMC2130_CS);
  71. SET_OUTPUT(E0_TMC2130_CS);
  72. SPI.begin();
  73. for (int i = 0; i < 2; i++) // X Y axes
  74. {
  75. tmc2130_wr(tmc2130_cs[i], TMC2130_REG_GCONF, 0x00000004); //GCONF - bit 2 activate stealthChop
  76. tmc2130_wr(tmc2130_cs[i], TMC2130_REG_IHOLD_IRUN, 0x000f0000 | ((tmc2130_current_r[i] & 0x1f) << 8) | (tmc2130_current_h[i] & 0x1f));
  77. tmc2130_wr(tmc2130_cs[i], TMC2130_REG_TPOWERDOWN, 0x00000000);
  78. // tmc2130_wr_PWMCONF(tmc2130_cs[i], TMC2130_PWM_AUTO_XY, TMC2130_PWM_FREQ_XY, TMC2130_PWM_GRAD_XY, TMC2130_PWM_AMPL_XY); //PWM_CONF //reset default=0x00050480
  79. tmc2130_wr_PWMCONF(tmc2130_cs[i]); //PWM_CONF //reset default=0x00050480
  80. //tmc2130_wr_TPWMTHRS(tmc2130_cs[i], TMC2130_TPWMTHRS);
  81. //tmc2130_wr_THIGH(tmc2130_cs[i], TMC2130_THIGH);
  82. tmc2130_wr_CHOPCONF(tmc2130_cs[i], 1, 16);
  83. }
  84. for (int i = 2; i < 3; i++) // Z axis
  85. {
  86. tmc2130_wr(tmc2130_cs[i], TMC2130_REG_GCONF, 0x00000004); //GCONF - bit 2 activate stealthChop
  87. tmc2130_wr(tmc2130_cs[i], TMC2130_REG_IHOLD_IRUN, 0x000f0000 | ((tmc2130_current_r[i] & 0x1f) << 8) | (tmc2130_current_h[i] & 0x1f));
  88. tmc2130_wr(tmc2130_cs[i], TMC2130_REG_TPOWERDOWN, 0x00000000);
  89. tmc2130_wr_PWMCONF(tmc2130_cs[i]); //PWM_CONF //reset default=0x00050480
  90. //tmc2130_wr_TPWMTHRS(tmc2130_cs[i], TMC2130_TPWMTHRS);
  91. //tmc2130_wr_THIGH(tmc2130_cs[i], TMC2130_THIGH);
  92. tmc2130_wr_CHOPCONF(tmc2130_cs[i], 1, 16);
  93. }
  94. for (int i = 3; i < 4; i++) // E axis
  95. {
  96. tmc2130_wr(tmc2130_cs[i], TMC2130_REG_GCONF, 0x00000004); //GCONF - bit 2 activate stealthChop
  97. tmc2130_wr(tmc2130_cs[i], TMC2130_REG_IHOLD_IRUN, 0x000f0000 | ((tmc2130_current_r[i] & 0x1f) << 8) | (tmc2130_current_h[i] & 0x1f));
  98. tmc2130_wr(tmc2130_cs[i], TMC2130_REG_TPOWERDOWN, 0x00000000);
  99. tmc2130_wr_CHOPCONF(tmc2130_cs[i], 1, 16);
  100. }
  101. }
  102. bool tmc2130_update_sg()
  103. {
  104. if ((sg_homing_axis == X_AXIS) || (sg_homing_axis == Y_AXIS))
  105. {
  106. uint8_t cs = tmc2130_cs[sg_homing_axis];
  107. uint16_t tstep = tmc2130_rd_TSTEP(cs);
  108. if (tstep < TMC2130_TCOOLTHRS)
  109. {
  110. if(sg_homing_delay < 10) // wait for a few tens microsteps until stallGuard is used //todo: read out microsteps directly, instead of delay counter
  111. sg_homing_delay++;
  112. else
  113. {
  114. uint16_t sg = tmc2130_rd_DRV_STATUS(cs) & 0x3ff;
  115. if (sg==0)
  116. {
  117. tmc2130_axis_stalled[sg_homing_axis] = true;
  118. tmc2130_LastHomingStalled = true;
  119. }
  120. else
  121. tmc2130_axis_stalled[sg_homing_axis] = false;
  122. }
  123. }
  124. else
  125. tmc2130_axis_stalled[sg_homing_axis] = false;
  126. return true;
  127. }
  128. else
  129. {
  130. tmc2130_axis_stalled[X_AXIS] = false;
  131. tmc2130_axis_stalled[Y_AXIS] = false;
  132. }
  133. return false;
  134. }
  135. void tmc2130_check_overtemp()
  136. {
  137. const static char TMC_OVERTEMP_MSG[] PROGMEM = "TMC DRIVER OVERTEMP ";
  138. uint8_t cs[4] = { X_TMC2130_CS, Y_TMC2130_CS, Z_TMC2130_CS, E0_TMC2130_CS };
  139. static uint32_t checktime = 0;
  140. //drivers_disabled[0] = 1; //TEST
  141. if( millis() - checktime > 1000 )
  142. {
  143. for(int i=0;i<4;i++)
  144. {
  145. uint32_t drv_status = 0;
  146. tmc2130_rd(cs[i], TMC2130_REG_DRV_STATUS, &drv_status);
  147. if (drv_status & ((uint32_t)1<<26))
  148. { // BIT 26 - over temp prewarning ~120C (+-20C)
  149. SERIAL_ERRORRPGM(TMC_OVERTEMP_MSG);
  150. SERIAL_ECHOLN(i);
  151. for(int i=0; i < 4; i++)
  152. tmc2130_wr(tmc2130_cs[i], TMC2130_REG_CHOPCONF, 0x00010000);
  153. kill(TMC_OVERTEMP_MSG);
  154. }
  155. }
  156. checktime = millis();
  157. }
  158. }
  159. void tmc2130_home_enter(uint8_t axis)
  160. {
  161. MYSERIAL.print("tmc2130_home_enter ");
  162. MYSERIAL.println((int)axis);
  163. uint8_t cs = tmc2130_cs[axis];
  164. sg_homing_axis = axis;
  165. sg_homing_delay = 0;
  166. tmc2130_axis_stalled[X_AXIS] = false;
  167. tmc2130_axis_stalled[Y_AXIS] = false;
  168. //Configuration to spreadCycle
  169. tmc2130_wr(cs, TMC2130_REG_GCONF, 0x00000000);
  170. tmc2130_wr(cs, TMC2130_REG_COOLCONF, ((axis == X_AXIS)?TMC2130_SG_THRS_X:TMC2130_SG_THRS_Y) << 16);
  171. tmc2130_wr(cs, TMC2130_REG_TCOOLTHRS, TMC2130_TCOOLTHRS);
  172. }
  173. void tmc2130_home_exit()
  174. {
  175. MYSERIAL.println("tmc2130_home_exit");
  176. if ((sg_homing_axis == X_AXIS) || (sg_homing_axis == Y_AXIS))
  177. {
  178. // Configuration back to stealthChop
  179. tmc2130_wr(tmc2130_cs[sg_homing_axis], TMC2130_REG_GCONF, 0x00000004);
  180. sg_homing_axis = 0xff;
  181. }
  182. }
  183. extern uint8_t tmc2130_didLastHomingStall()
  184. {
  185. uint8_t ret = tmc2130_LastHomingStalled;
  186. tmc2130_LastHomingStalled = false;
  187. return ret;
  188. }
  189. void tmc2130_set_current_h(uint8_t axis, uint8_t current)
  190. {
  191. MYSERIAL.print("tmc2130_set_current_h ");
  192. MYSERIAL.print((int)axis);
  193. MYSERIAL.print(" ");
  194. MYSERIAL.println((int)current);
  195. if (current > 15) current = 15; //current>15 is unsafe
  196. tmc2130_current_h[axis] = current;
  197. tmc2130_wr(tmc2130_cs[axis], TMC2130_REG_IHOLD_IRUN, 0x000f0000 | ((tmc2130_current_r[axis] & 0x1f) << 8) | (tmc2130_current_h[axis] & 0x1f));
  198. }
  199. void tmc2130_set_current_r(uint8_t axis, uint8_t current)
  200. {
  201. MYSERIAL.print("tmc2130_set_current_r ");
  202. MYSERIAL.print((int)axis);
  203. MYSERIAL.print(" ");
  204. MYSERIAL.println((int)current);
  205. if (current > 15) current = 15; //current>15 is unsafe
  206. tmc2130_current_r[axis] = current;
  207. tmc2130_wr(tmc2130_cs[axis], TMC2130_REG_IHOLD_IRUN, 0x000f0000 | ((tmc2130_current_r[axis] & 0x1f) << 8) | (tmc2130_current_h[axis] & 0x1f));
  208. }
  209. void tmc2130_print_currents()
  210. {
  211. MYSERIAL.println("tmc2130_print_currents");
  212. MYSERIAL.println("\tH\rR");
  213. MYSERIAL.print("X\t");
  214. MYSERIAL.print((int)tmc2130_current_h[0]);
  215. MYSERIAL.print("\t");
  216. MYSERIAL.println((int)tmc2130_current_r[0]);
  217. MYSERIAL.print("Y\t");
  218. MYSERIAL.print((int)tmc2130_current_h[1]);
  219. MYSERIAL.print("\t");
  220. MYSERIAL.println((int)tmc2130_current_r[1]);
  221. MYSERIAL.print("Z\t");
  222. MYSERIAL.print((int)tmc2130_current_h[2]);
  223. MYSERIAL.print("\t");
  224. MYSERIAL.println((int)tmc2130_current_r[2]);
  225. MYSERIAL.print("E\t");
  226. MYSERIAL.print((int)tmc2130_current_h[3]);
  227. MYSERIAL.print("\t");
  228. MYSERIAL.println((int)tmc2130_current_r[3]);
  229. }
  230. uint16_t tmc2130_rd_TSTEP(uint8_t cs)
  231. {
  232. uint32_t val32 = 0;
  233. tmc2130_rd(cs, TMC2130_REG_TSTEP, &val32);
  234. if (val32 & 0x000f0000) return 0xffff;
  235. return val32 & 0xffff;
  236. }
  237. uint16_t tmc2130_rd_DRV_STATUS(uint8_t cs)
  238. {
  239. uint32_t val32 = 0;
  240. tmc2130_rd(cs, TMC2130_REG_DRV_STATUS, &val32);
  241. return val32;
  242. }
  243. void tmc2130_wr_CHOPCONF(uint8_t cs, bool extrapolate256, uint16_t microstep_resolution)
  244. {
  245. uint8_t mres=0b0100;
  246. if(microstep_resolution == 256) mres = 0b0000;
  247. if(microstep_resolution == 128) mres = 0b0001;
  248. if(microstep_resolution == 64) mres = 0b0010;
  249. if(microstep_resolution == 32) mres = 0b0011;
  250. if(microstep_resolution == 16) mres = 0b0100;
  251. if(microstep_resolution == 8) mres = 0b0101;
  252. if(microstep_resolution == 4) mres = 0b0110;
  253. if(microstep_resolution == 2) mres = 0b0111;
  254. if(microstep_resolution == 1) mres = 0b1000;
  255. mres |= extrapolate256 << 4; //bit28 intpol
  256. //tmc2130_write(cs,0x6C,mres,0x01,0x00,0xD3);
  257. // tmc2130_write(cs,0x6C,mres,0x01,0x00,0xC3);
  258. tmc2130_wr(cs,TMC2130_REG_CHOPCONF,((uint32_t)mres << 24) | 0x0100C3);
  259. }
  260. void tmc2130_wr_PWMCONF(uint8_t cs, uint8_t PWMautoScale, uint8_t PWMfreq, uint8_t PWMgrad, uint8_t PWMampl)
  261. {
  262. tmc2130_wr(cs,0x70,((uint32_t)(PWMautoScale+PWMfreq) << 16) | ((uint32_t)PWMgrad << 8) | PWMampl); // TMC LJ -> For better readability changed to 0x00 and added PWMautoScale and PWMfreq
  263. }
  264. void tmc2130_wr_TPWMTHRS(uint8_t cs, uint32_t val32)
  265. {
  266. tmc2130_wr(cs, TMC2130_REG_TPWMTHRS, val32);
  267. }
  268. void tmc2130_wr_THIGH(uint8_t cs, uint32_t val32)
  269. {
  270. tmc2130_wr(cs, TMC2130_REG_THIGH, val32);
  271. }
  272. uint8_t tmc2130_axis_by_cs(uint8_t cs)
  273. {
  274. switch (cs)
  275. {
  276. case X_TMC2130_CS: return 0;
  277. case Y_TMC2130_CS: return 1;
  278. case Z_TMC2130_CS: return 2;
  279. case E0_TMC2130_CS: return 3;
  280. }
  281. return -1;
  282. }
  283. uint8_t tmc2130_wr(uint8_t cs, uint8_t addr, uint32_t wval)
  284. {
  285. uint8_t stat = tmc2130_txrx(cs, addr | 0x80, wval, 0);
  286. #ifdef TMC2130_DEBUG_WR
  287. MYSERIAL.print("tmc2130_wr(");
  288. MYSERIAL.print((unsigned char)tmc2130_axis_by_cs(cs), DEC);
  289. MYSERIAL.print(", 0x");
  290. MYSERIAL.print((unsigned char)addr, HEX);
  291. MYSERIAL.print(", 0x");
  292. MYSERIAL.print((unsigned long)wval, HEX);
  293. MYSERIAL.print(")=0x");
  294. MYSERIAL.println((unsigned char)stat, HEX);
  295. #endif //TMC2130_DEBUG_WR
  296. return stat;
  297. }
  298. uint8_t tmc2130_rd(uint8_t cs, uint8_t addr, uint32_t* rval)
  299. {
  300. uint32_t val32 = 0;
  301. uint8_t stat = tmc2130_txrx(cs, addr, 0x00000000, &val32);
  302. if (rval != 0) *rval = val32;
  303. #ifdef TMC2130_DEBUG_RD
  304. MYSERIAL.print("tmc2130_rd(");
  305. MYSERIAL.print((unsigned char)tmc2130_axis_by_cs(cs), DEC);
  306. MYSERIAL.print(", 0x");
  307. MYSERIAL.print((unsigned char)addr, HEX);
  308. MYSERIAL.print(", 0x");
  309. MYSERIAL.print((unsigned long)val32, HEX);
  310. MYSERIAL.print(")=0x");
  311. MYSERIAL.println((unsigned char)stat, HEX);
  312. #endif //TMC2130_DEBUG_RD
  313. return stat;
  314. }
  315. uint8_t tmc2130_txrx(uint8_t cs, uint8_t addr, uint32_t wval, uint32_t* rval)
  316. {
  317. //datagram1 - request
  318. SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE3));
  319. digitalWrite(cs, LOW);
  320. SPI.transfer(addr); // address
  321. SPI.transfer((wval >> 24) & 0xff); // MSB
  322. SPI.transfer((wval >> 16) & 0xff);
  323. SPI.transfer((wval >> 8) & 0xff);
  324. SPI.transfer(wval & 0xff); // LSB
  325. digitalWrite(cs, HIGH);
  326. SPI.endTransaction();
  327. //datagram2 - response
  328. SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE3));
  329. digitalWrite(cs, LOW);
  330. uint8_t stat = SPI.transfer(0); // status
  331. uint32_t val32 = 0;
  332. val32 = SPI.transfer(0); // MSB
  333. val32 = (val32 << 8) | SPI.transfer(0);
  334. val32 = (val32 << 8) | SPI.transfer(0);
  335. val32 = (val32 << 8) | SPI.transfer(0); // LSB
  336. digitalWrite(cs, HIGH);
  337. SPI.endTransaction();
  338. if (rval != 0) *rval = val32;
  339. return stat;
  340. }
  341. #endif //HAVE_TMC2130_DRIVERS