tmc2130.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. #include "Marlin.h"
  2. #ifdef HAVE_TMC2130_DRIVERS
  3. #include "tmc2130.h"
  4. #include <SPI.h>
  5. uint32_t tmc2130_read(uint8_t chipselect, uint8_t address)
  6. {
  7. uint32_t val32;
  8. uint8_t val0;
  9. uint8_t val1;
  10. uint8_t val2;
  11. uint8_t val3;
  12. uint8_t val4;
  13. //datagram1 - read request (address + dummy write)
  14. SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE3));
  15. digitalWrite(chipselect,LOW);
  16. SPI.transfer(address);
  17. SPI.transfer(0);
  18. SPI.transfer(0);
  19. SPI.transfer(0);
  20. SPI.transfer(0);
  21. digitalWrite(chipselect, HIGH);
  22. SPI.endTransaction();
  23. //datagram2 - response
  24. SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE3));
  25. digitalWrite(chipselect,LOW);
  26. val0 = SPI.transfer(0);
  27. val1 = SPI.transfer(0);
  28. val2 = SPI.transfer(0);
  29. val3 = SPI.transfer(0);
  30. val4 = SPI.transfer(0);
  31. digitalWrite(chipselect, HIGH);
  32. SPI.endTransaction();
  33. #ifdef TMC_DBG_READS
  34. MYSERIAL.print("SPIRead 0x");
  35. MYSERIAL.print(address,HEX);
  36. MYSERIAL.print(" Status:");
  37. MYSERIAL.print(val0 & 0b00000111,BIN);
  38. MYSERIAL.print(" ");
  39. MYSERIAL.print(val1,BIN);
  40. MYSERIAL.print(" ");
  41. MYSERIAL.print(val2,BIN);
  42. MYSERIAL.print(" ");
  43. MYSERIAL.print(val3,BIN);
  44. MYSERIAL.print(" ");
  45. MYSERIAL.print(val4,BIN);
  46. #endif
  47. val32 = (uint32_t)val1<<24 | (uint32_t)val2<<16 | (uint32_t)val3<<8 | (uint32_t)val4;
  48. #ifdef TMC_DBG_READS
  49. MYSERIAL.print(" 0x");
  50. MYSERIAL.println(val32,HEX);
  51. #endif
  52. return val32;
  53. }
  54. void tmc2130_write(uint8_t chipselect, uint8_t address,uint8_t wval1,uint8_t wval2,uint8_t wval3,uint8_t wval4)
  55. {
  56. uint32_t val32;
  57. uint8_t val0;
  58. uint8_t val1;
  59. uint8_t val2;
  60. uint8_t val3;
  61. uint8_t val4;
  62. //datagram1 - write
  63. SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE3));
  64. digitalWrite(chipselect,LOW);
  65. SPI.transfer(address+0x80);
  66. SPI.transfer(wval1);
  67. SPI.transfer(wval2);
  68. SPI.transfer(wval3);
  69. SPI.transfer(wval4);
  70. digitalWrite(chipselect, HIGH);
  71. SPI.endTransaction();
  72. //datagram2 - response
  73. SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE3));
  74. digitalWrite(chipselect,LOW);
  75. val0 = SPI.transfer(0);
  76. val1 = SPI.transfer(0);
  77. val2 = SPI.transfer(0);
  78. val3 = SPI.transfer(0);
  79. val4 = SPI.transfer(0);
  80. digitalWrite(chipselect, HIGH);
  81. SPI.endTransaction();
  82. #ifdef TMC_DBG_WRITE
  83. MYSERIAL.print("WriteRead 0x");
  84. MYSERIAL.print(address,HEX);
  85. MYSERIAL.print(" Status:");
  86. MYSERIAL.print(val0 & 0b00000111,BIN);
  87. MYSERIAL.print(" ");
  88. MYSERIAL.print(val1,BIN);
  89. MYSERIAL.print(" ");
  90. MYSERIAL.print(val2,BIN);
  91. MYSERIAL.print(" ");
  92. MYSERIAL.print(val3,BIN);
  93. MYSERIAL.print(" ");
  94. MYSERIAL.print(val4,BIN);
  95. val32 = (uint32_t)val1<<24 | (uint32_t)val2<<16 | (uint32_t)val3<<8 | (uint32_t)val4;
  96. MYSERIAL.print(" 0x");
  97. MYSERIAL.println(val32,HEX);
  98. #endif //TMC_DBG_READS
  99. }
  100. uint8_t tmc2130_read8(uint8_t chipselect, uint8_t address)
  101. {
  102. //datagram1 - write
  103. SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE3));
  104. digitalWrite(chipselect,LOW);
  105. SPI.transfer(address);
  106. SPI.transfer(0x00);
  107. SPI.transfer(0x00);
  108. SPI.transfer(0x00);
  109. SPI.transfer(0x00);
  110. digitalWrite(chipselect, HIGH);
  111. SPI.endTransaction();
  112. uint8_t val0;
  113. //datagram2 - response
  114. SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE3));
  115. digitalWrite(chipselect,LOW);
  116. val0 = SPI.transfer(0);
  117. digitalWrite(chipselect, HIGH);
  118. SPI.endTransaction();
  119. return val0;
  120. }
  121. uint32_t tmc2130_readRegister(uint8_t chipselect, uint8_t address)
  122. {
  123. //datagram1 - write
  124. SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE3));
  125. digitalWrite(chipselect,LOW);
  126. SPI.transfer(address);
  127. SPI.transfer(0x00);
  128. SPI.transfer(0x00);
  129. SPI.transfer(0x00);
  130. SPI.transfer(0x00);
  131. digitalWrite(chipselect, HIGH);
  132. SPI.endTransaction();
  133. uint32_t val0;
  134. //datagram2 - response
  135. SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE3));
  136. digitalWrite(chipselect,LOW);
  137. SPI.transfer(0); // ignore status bits
  138. val0 = SPI.transfer(0); // MSB
  139. val0 = (val0 << 8) | SPI.transfer(0);
  140. val0 = (val0 << 8) | SPI.transfer(0);
  141. val0 = (val0 << 8) | SPI.transfer(0); //LSB
  142. digitalWrite(chipselect, HIGH);
  143. SPI.endTransaction();
  144. return val0;
  145. }
  146. uint16_t tmc2130_readSG(uint8_t chipselect)
  147. {
  148. uint8_t address = 0x6F;
  149. uint32_t registerValue = tmc2130_readRegister(chipselect, address);
  150. uint16_t val0 = registerValue & 0x3ff;
  151. return val0;
  152. }
  153. uint16_t tmc2130_readTStep(uint8_t chipselect)
  154. {
  155. uint8_t address = 0x12;
  156. uint32_t registerValue = tmc2130_readRegister(chipselect, address);
  157. uint16_t val0 = 0;
  158. if(registerValue & 0x000f0000)
  159. val0 = 0xffff;
  160. else
  161. val0 = registerValue & 0xffff;
  162. return val0;
  163. }
  164. void tmc2130_chopconf(uint8_t cs, bool extrapolate256 = 0, uint16_t microstep_resolution = 16)
  165. {
  166. uint8_t mres=0b0100;
  167. if(microstep_resolution == 256) mres = 0b0000;
  168. if(microstep_resolution == 128) mres = 0b0001;
  169. if(microstep_resolution == 64) mres = 0b0010;
  170. if(microstep_resolution == 32) mres = 0b0011;
  171. if(microstep_resolution == 16) mres = 0b0100;
  172. if(microstep_resolution == 8) mres = 0b0101;
  173. if(microstep_resolution == 4) mres = 0b0110;
  174. if(microstep_resolution == 2) mres = 0b0111;
  175. if(microstep_resolution == 1) mres = 0b1000;
  176. mres |= extrapolate256 << 4; //bit28 intpol
  177. //tmc2130_write(cs,0x6C,mres,0x01,0x00,0xD3);
  178. tmc2130_write(cs,0x6C,mres,0x01,0x00,0xC3);
  179. }
  180. 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)
  181. {
  182. tmc2130_write(cs,0x70,0x00,(PWMautoScale+PWMfreq),PWMgrad,PWMampl); // TMC LJ -> For better readability changed to 0x00 and added PWMautoScale and PWMfreq
  183. }
  184. void tmc2130_PWMthreshold(uint8_t cs)
  185. {
  186. tmc2130_write(cs,0x13,0x00,0x00,0x00,0x00); // TMC LJ -> Adds possibility to swtich from stealthChop to spreadCycle automatically
  187. }
  188. uint8_t st_didLastHomingStall()
  189. {
  190. uint8_t returnValue = sg_lastHomingStalled;
  191. sg_lastHomingStalled = false;
  192. return returnValue;
  193. }
  194. void tmc2130_disable_motor(uint8_t driver)
  195. {
  196. uint8_t cs[4] = { X_TMC2130_CS, Y_TMC2130_CS, Z_TMC2130_CS, E0_TMC2130_CS };
  197. tmc2130_write(cs[driver],0x6C,0,01,0,0);
  198. }
  199. void tmc2130_check_overtemp()
  200. {
  201. const static char TMC_OVERTEMP_MSG[] PROGMEM = "TMC DRIVER OVERTEMP ";
  202. uint8_t cs[4] = { X_TMC2130_CS, Y_TMC2130_CS, Z_TMC2130_CS, E0_TMC2130_CS };
  203. static uint32_t checktime = 0;
  204. //drivers_disabled[0] = 1; //TEST
  205. if( millis() - checktime > 1000 )
  206. {
  207. for(int i=0;i<4;i++)
  208. {
  209. uint32_t drv_status = tmc2130_read(cs[i], 0x6F); //0x6F DRV_STATUS
  210. if (drv_status & ((uint32_t)1<<26))
  211. { // BIT 26 - over temp prewarning ~120C (+-20C)
  212. SERIAL_ERRORRPGM(TMC_OVERTEMP_MSG);
  213. SERIAL_ECHOLN(i);
  214. for(int x=0; x<4;x++) tmc2130_disable_motor(x);
  215. kill(TMC_OVERTEMP_MSG);
  216. }
  217. }
  218. checktime = millis();
  219. }
  220. }
  221. void tmc2130_init()
  222. {
  223. uint8_t cs[4] = { X_TMC2130_CS, Y_TMC2130_CS, Z_TMC2130_CS, E0_TMC2130_CS };
  224. // uint8_t current[4] = { 31, 31, 31, 31 };
  225. // uint8_t current_h[4] = { 12, 12, 12, 12 };
  226. // uint8_t current_r[4] = { 24, 24, 24, 24 };
  227. // uint8_t current_r[4] = { 32, 32, 32, 32 };
  228. // uint8_t current_h[4] = { 14, 14, 14, 14 };
  229. uint8_t current_h[4] = { 2, 2, 2, 4 };
  230. uint8_t current_r[4] = { 6, 6, 8, 8 };
  231. WRITE(X_TMC2130_CS, HIGH);
  232. WRITE(Y_TMC2130_CS, HIGH);
  233. WRITE(Z_TMC2130_CS, HIGH);
  234. WRITE(E0_TMC2130_CS, HIGH);
  235. SET_OUTPUT(X_TMC2130_CS);
  236. SET_OUTPUT(Y_TMC2130_CS);
  237. SET_OUTPUT(Z_TMC2130_CS);
  238. SET_OUTPUT(E0_TMC2130_CS);
  239. SPI.begin();
  240. /* for(int i=0;i<4;i++)
  241. {
  242. //tmc2130_write(cs[i],0x6C,0b10100,01,00,0xC5);
  243. tmc2130_chopconf(cs[i],1,16);
  244. tmc2130_write(cs[i],0x10,0,15,current_h[i],current_r[i]); //0x10 IHOLD_IRUN
  245. //tmc2130_write(cs[i],0x0,0,0,0,0x05); //address=0x0 GCONF EXT VREF
  246. tmc2130_write(cs[i],0x0,0,0,0,0x05); //address=0x0 GCONF EXT VREF - activate stealthChop
  247. //tmc2130_write(cs[i],0x11,0,0,0,0xA);
  248. // Uncomment lines below to use a different configuration (pwm_autoscale = 0) for XY axes
  249. if(i==0 || i==1)
  250. tmc2130_PWMconf(cs[i],PWM_AUTOSCALE_XY,PWM_FREQ_XY,PWM_GRAD_XY,PWM_AMPL_XY); //address=0x70 PWM_CONF //reset default=0x00050480
  251. else
  252. tmc2130_PWMconf(cs[i]); //address=0x70 PWM_CONF //reset default=0x00050480
  253. tmc2130_PWMthreshold(cs[i]);
  254. }
  255. */
  256. #ifdef MK3_TEST1
  257. for (int i=0;i<4;i++)
  258. {
  259. tmc2130_write(cs[i],0x0,0,0,0,0x00); //address=0x0 GCONF - bit 2 activate stealthChop
  260. tmc2130_write(cs[i],0x10,0,15,current_r[i],current_h[i]); //0x10 IHOLD_IRUN
  261. tmc2130_chopconf(cs[i],0,16);
  262. }
  263. #else //MK3_TEST1
  264. for (int i=0;i<3;i++)
  265. {
  266. tmc2130_write(cs[i],0x0,0,0,0,0x04); //address=0x0 GCONF - bit 2 activate stealthChop
  267. tmc2130_write(cs[i],0x10,0,15,current_r[i],current_h[i]); //0x10 IHOLD_IRUN
  268. tmc2130_write(cs[i],0x11,0,0,0,0);
  269. tmc2130_PWMconf(cs[i]); //address=0x70 PWM_CONF //reset default=0x00050480
  270. // tmc2130_PWMthreshold(cs[i]);
  271. tmc2130_chopconf(cs[i],1,16);
  272. }
  273. for (int i=3;i<4;i++)
  274. {
  275. tmc2130_write(cs[i],0x0,0,0,0,0x00); //address=0x0 GCONF - bit 2 activate stealthChop
  276. tmc2130_write(cs[i],0x10,0,15,current_r[i],current_h[i]); //0x10 IHOLD_IRUN
  277. tmc2130_write(cs[i],0x11,0,0,0,0);
  278. tmc2130_chopconf(cs[i],1,16);
  279. }
  280. #endif //MK3_TEST1
  281. }
  282. void tmc2130_st_synchronize()
  283. {
  284. uint8_t delay = 0;
  285. if(sg_homing_axis == X_AXIS || sg_homing_axis == Y_AXIS)
  286. {
  287. uint8_t axis;
  288. if(sg_homing_axis == X_AXIS)
  289. axis = X_TMC2130_CS;
  290. else
  291. axis = Y_TMC2130_CS;
  292. uint16_t tstep = tmc2130_readTStep(axis);
  293. // SERIAL_PROTOCOLLN(tstep);
  294. if(tstep < TCOOLTHRS)
  295. {
  296. if(delay < 255) // wait for a few tens microsteps until stallGuard is used //todo: read out microsteps directly, instead of delay counter
  297. delay++;
  298. else
  299. {
  300. uint16_t sg = tmc2130_readSG(axis);
  301. if(sg==0)
  302. {
  303. sg_axis_stalled[sg_homing_axis] = true;
  304. sg_lastHomingStalled = true;
  305. }
  306. else
  307. sg_axis_stalled[sg_homing_axis] = false;
  308. // SERIAL_PROTOCOLLN(sg);
  309. }
  310. }
  311. else
  312. {
  313. sg_axis_stalled[sg_homing_axis] = false;
  314. delay = 0;
  315. }
  316. }
  317. else
  318. {
  319. sg_axis_stalled[X_AXIS] = false;
  320. sg_axis_stalled[Y_AXIS] = false;
  321. }
  322. }
  323. void tmc2130_st_home_enter(uint8_t axis)
  324. {
  325. sg_homing_axis = axis;
  326. // Configuration to spreadCycle
  327. // tmc2130_write((axis == X_AXIS)? X_TMC2130_CS : Y_TMC2130_CS,0x0,0,0,0,0x01);
  328. tmc2130_write((axis == X_AXIS)? X_TMC2130_CS : Y_TMC2130_CS,0x0,0,0,0,0x00);
  329. tmc2130_write((axis == X_AXIS)? X_TMC2130_CS : Y_TMC2130_CS,0x6D,0,(axis == X_AXIS)?SG_THRESHOLD_X:SG_THRESHOLD_Y,0,0);
  330. tmc2130_write((axis == X_AXIS)? X_TMC2130_CS : Y_TMC2130_CS,0x14,0,0,0,TCOOLTHRS);
  331. }
  332. void tmc2130_st_home_exit()
  333. {
  334. if ((sg_homing_axis == X_AXIS) || (sg_homing_axis == X_AXIS))
  335. {
  336. // Configuration back to stealthChop
  337. tmc2130_write((sg_homing_axis == X_AXIS)? X_TMC2130_CS : Y_TMC2130_CS, 0x0, 0, 0, 0, 0x04);
  338. sg_homing_axis = 0xff;
  339. sg_axis_stalled[X_AXIS] = false;
  340. sg_axis_stalled[Y_AXIS] = false;
  341. }
  342. }
  343. #endif //HAVE_TMC2130_DRIVERS