tmc2130.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. //SERIAL_ECHOLNPGM("drv_status:");
  150. for(int i=0;i<4;i++)
  151. {
  152. uint32_t drv_status = 0;
  153. tmc2130_rd(cs[i], TMC2130_REG_DRV_STATUS, &drv_status);
  154. //MYSERIAL.print(drv_status);
  155. //SERIAL_ECHOPGM(" ");
  156. if (drv_status & ((uint32_t)1<<26))
  157. { // BIT 26 - over temp prewarning ~120C (+-20C)
  158. SERIAL_ERRORRPGM(TMC_OVERTEMP_MSG);
  159. SERIAL_ECHOLN(i);
  160. for(int i=0; i < 4; i++)
  161. tmc2130_wr(tmc2130_cs[i], TMC2130_REG_CHOPCONF, 0x00010000);
  162. kill(TMC_OVERTEMP_MSG);
  163. }
  164. }
  165. //SERIAL_ECHOLNPGM("");
  166. checktime = millis();
  167. }
  168. }
  169. void tmc2130_home_enter(uint8_t axis)
  170. {
  171. MYSERIAL.print("tmc2130_home_enter ");
  172. MYSERIAL.println((int)axis);
  173. #ifdef TMC2130_SG_HOMING
  174. uint8_t cs = tmc2130_cs[axis];
  175. sg_homing_axis = axis;
  176. sg_homing_delay = 0;
  177. tmc2130_axis_stalled[X_AXIS] = false;
  178. tmc2130_axis_stalled[Y_AXIS] = false;
  179. //Configuration to spreadCycle
  180. tmc2130_wr(cs, TMC2130_REG_GCONF, 0x00000000);
  181. tmc2130_wr(cs, TMC2130_REG_COOLCONF, ((axis == X_AXIS)?sg_thrs_x:sg_thrs_y) << 16);
  182. tmc2130_wr(cs, TMC2130_REG_TCOOLTHRS, TMC2130_TCOOLTHRS);
  183. #ifndef TMC2130_SG_HOMING_SW
  184. tmc2130_wr(cs, TMC2130_REG_GCONF, 0x00000080); //stallguard output to DIAG0
  185. #endif
  186. #endif
  187. }
  188. void tmc2130_home_exit()
  189. {
  190. MYSERIAL.println("tmc2130_home_exit ");
  191. MYSERIAL.println((int)sg_homing_axis);
  192. #ifdef TMC2130_SG_HOMING
  193. if ((sg_homing_axis == X_AXIS) || (sg_homing_axis == Y_AXIS))
  194. {
  195. if (tmc2130_mode == TMC2130_MODE_SILENT)
  196. tmc2130_wr(tmc2130_cs[sg_homing_axis], TMC2130_REG_GCONF, 0x00000004); // Configuration back to stealthChop
  197. else
  198. tmc2130_wr(tmc2130_cs[sg_homing_axis], TMC2130_REG_GCONF, 0x00000000);
  199. sg_homing_axis = 0xff;
  200. }
  201. #endif
  202. }
  203. extern uint8_t tmc2130_didLastHomingStall()
  204. {
  205. uint8_t ret = tmc2130_LastHomingStalled;
  206. tmc2130_LastHomingStalled = false;
  207. return ret;
  208. }
  209. void tmc2130_set_current_h(uint8_t axis, uint8_t current)
  210. {
  211. MYSERIAL.print("tmc2130_set_current_h ");
  212. MYSERIAL.print((int)axis);
  213. MYSERIAL.print(" ");
  214. MYSERIAL.println((int)current);
  215. if (current > 15) current = 15; //current>15 is unsafe
  216. tmc2130_current_h[axis] = current;
  217. tmc2130_wr(tmc2130_cs[axis], TMC2130_REG_IHOLD_IRUN, 0x000f0000 | ((tmc2130_current_r[axis] & 0x1f) << 8) | (tmc2130_current_h[axis] & 0x1f));
  218. }
  219. void tmc2130_set_current_r(uint8_t axis, uint8_t current)
  220. {
  221. MYSERIAL.print("tmc2130_set_current_r ");
  222. MYSERIAL.print((int)axis);
  223. MYSERIAL.print(" ");
  224. MYSERIAL.println((int)current);
  225. if (current > 15) current = 15; //current>15 is unsafe
  226. tmc2130_current_r[axis] = current;
  227. tmc2130_wr(tmc2130_cs[axis], TMC2130_REG_IHOLD_IRUN, 0x000f0000 | ((tmc2130_current_r[axis] & 0x1f) << 8) | (tmc2130_current_h[axis] & 0x1f));
  228. }
  229. void tmc2130_print_currents()
  230. {
  231. MYSERIAL.println("tmc2130_print_currents");
  232. MYSERIAL.println("\tH\rR");
  233. MYSERIAL.print("X\t");
  234. MYSERIAL.print((int)tmc2130_current_h[0]);
  235. MYSERIAL.print("\t");
  236. MYSERIAL.println((int)tmc2130_current_r[0]);
  237. MYSERIAL.print("Y\t");
  238. MYSERIAL.print((int)tmc2130_current_h[1]);
  239. MYSERIAL.print("\t");
  240. MYSERIAL.println((int)tmc2130_current_r[1]);
  241. MYSERIAL.print("Z\t");
  242. MYSERIAL.print((int)tmc2130_current_h[2]);
  243. MYSERIAL.print("\t");
  244. MYSERIAL.println((int)tmc2130_current_r[2]);
  245. MYSERIAL.print("E\t");
  246. MYSERIAL.print((int)tmc2130_current_h[3]);
  247. MYSERIAL.print("\t");
  248. MYSERIAL.println((int)tmc2130_current_r[3]);
  249. }
  250. uint16_t tmc2130_rd_TSTEP(uint8_t cs)
  251. {
  252. uint32_t val32 = 0;
  253. tmc2130_rd(cs, TMC2130_REG_TSTEP, &val32);
  254. if (val32 & 0x000f0000) return 0xffff;
  255. return val32 & 0xffff;
  256. }
  257. uint16_t tmc2130_rd_DRV_STATUS(uint8_t cs)
  258. {
  259. uint32_t val32 = 0;
  260. tmc2130_rd(cs, TMC2130_REG_DRV_STATUS, &val32);
  261. return val32;
  262. }
  263. void tmc2130_wr_CHOPCONF(uint8_t cs, bool extrapolate256, uint16_t microstep_resolution)
  264. {
  265. uint8_t mres=0b0100;
  266. if(microstep_resolution == 256) mres = 0b0000;
  267. if(microstep_resolution == 128) mres = 0b0001;
  268. if(microstep_resolution == 64) mres = 0b0010;
  269. if(microstep_resolution == 32) mres = 0b0011;
  270. if(microstep_resolution == 16) mres = 0b0100;
  271. if(microstep_resolution == 8) mres = 0b0101;
  272. if(microstep_resolution == 4) mres = 0b0110;
  273. if(microstep_resolution == 2) mres = 0b0111;
  274. if(microstep_resolution == 1) mres = 0b1000;
  275. mres |= extrapolate256 << 4; //bit28 intpol
  276. //tmc2130_write(cs,0x6C,mres,0x01,0x00,0xD3);
  277. // tmc2130_write(cs,0x6C,mres,0x01,0x00,0xC3);
  278. tmc2130_wr(cs,TMC2130_REG_CHOPCONF,((uint32_t)mres << 24) | 0x0100C3);
  279. }
  280. void tmc2130_wr_PWMCONF(uint8_t cs, uint8_t PWMautoScale, uint8_t PWMfreq, uint8_t PWMgrad, uint8_t PWMampl)
  281. {
  282. 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
  283. }
  284. void tmc2130_wr_TPWMTHRS(uint8_t cs, uint32_t val32)
  285. {
  286. tmc2130_wr(cs, TMC2130_REG_TPWMTHRS, val32);
  287. }
  288. void tmc2130_wr_THIGH(uint8_t cs, uint32_t val32)
  289. {
  290. tmc2130_wr(cs, TMC2130_REG_THIGH, val32);
  291. }
  292. uint8_t tmc2130_axis_by_cs(uint8_t cs)
  293. {
  294. switch (cs)
  295. {
  296. case X_TMC2130_CS: return 0;
  297. case Y_TMC2130_CS: return 1;
  298. case Z_TMC2130_CS: return 2;
  299. case E0_TMC2130_CS: return 3;
  300. }
  301. return -1;
  302. }
  303. uint8_t tmc2130_wr(uint8_t cs, uint8_t addr, uint32_t wval)
  304. {
  305. uint8_t stat = tmc2130_txrx(cs, addr | 0x80, wval, 0);
  306. #ifdef TMC2130_DEBUG_WR
  307. MYSERIAL.print("tmc2130_wr(");
  308. MYSERIAL.print((unsigned char)tmc2130_axis_by_cs(cs), DEC);
  309. MYSERIAL.print(", 0x");
  310. MYSERIAL.print((unsigned char)addr, HEX);
  311. MYSERIAL.print(", 0x");
  312. MYSERIAL.print((unsigned long)wval, HEX);
  313. MYSERIAL.print(")=0x");
  314. MYSERIAL.println((unsigned char)stat, HEX);
  315. #endif //TMC2130_DEBUG_WR
  316. return stat;
  317. }
  318. uint8_t tmc2130_rd(uint8_t cs, uint8_t addr, uint32_t* rval)
  319. {
  320. uint32_t val32 = 0;
  321. uint8_t stat = tmc2130_txrx(cs, addr, 0x00000000, &val32);
  322. if (rval != 0) *rval = val32;
  323. #ifdef TMC2130_DEBUG_RD
  324. MYSERIAL.print("tmc2130_rd(");
  325. MYSERIAL.print((unsigned char)tmc2130_axis_by_cs(cs), DEC);
  326. MYSERIAL.print(", 0x");
  327. MYSERIAL.print((unsigned char)addr, HEX);
  328. MYSERIAL.print(", 0x");
  329. MYSERIAL.print((unsigned long)val32, HEX);
  330. MYSERIAL.print(")=0x");
  331. MYSERIAL.println((unsigned char)stat, HEX);
  332. #endif //TMC2130_DEBUG_RD
  333. return stat;
  334. }
  335. uint8_t tmc2130_txrx(uint8_t cs, uint8_t addr, uint32_t wval, uint32_t* rval)
  336. {
  337. //datagram1 - request
  338. SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE3));
  339. digitalWrite(cs, LOW);
  340. SPI.transfer(addr); // address
  341. SPI.transfer((wval >> 24) & 0xff); // MSB
  342. SPI.transfer((wval >> 16) & 0xff);
  343. SPI.transfer((wval >> 8) & 0xff);
  344. SPI.transfer(wval & 0xff); // LSB
  345. digitalWrite(cs, HIGH);
  346. SPI.endTransaction();
  347. //datagram2 - response
  348. SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE3));
  349. digitalWrite(cs, LOW);
  350. uint8_t stat = SPI.transfer(0); // status
  351. uint32_t val32 = 0;
  352. val32 = SPI.transfer(0); // MSB
  353. val32 = (val32 << 8) | SPI.transfer(0);
  354. val32 = (val32 << 8) | SPI.transfer(0);
  355. val32 = (val32 << 8) | SPI.transfer(0); // LSB
  356. digitalWrite(cs, HIGH);
  357. SPI.endTransaction();
  358. if (rval != 0) *rval = val32;
  359. return stat;
  360. }
  361. #endif //HAVE_TMC2130_DRIVERS