tmc2130.cpp 13 KB

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