tmc2130.cpp 11 KB

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