tmc2130.cpp 12 KB

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