tmc2130.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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[2] = {0, 0};
  18. //last homing stalled
  19. uint8_t tmc2130_LastHomingStalled = 0;
  20. //pwm_ampl
  21. uint8_t tmc2130_pwm_ampl[2] = {TMC2130_PWM_AMPL_XY, TMC2130_PWM_AMPL_XY};
  22. //pwm_grad
  23. uint8_t tmc2130_pwm_grad[2] = {TMC2130_PWM_GRAD_XY, TMC2130_PWM_GRAD_XY};
  24. //pwm_auto
  25. uint8_t tmc2130_pwm_auto[2] = {TMC2130_PWM_AUTO_XY, TMC2130_PWM_AUTO_XY};
  26. //pwm_freq
  27. uint8_t tmc2130_pwm_freq[2] = {TMC2130_PWM_FREQ_XY, TMC2130_PWM_FREQ_XY};
  28. uint8_t sg_homing_axes_mask = 0x00;
  29. uint8_t sg_homing_delay = 0;
  30. uint8_t sg_thrs_x = TMC2130_SG_THRS_X;
  31. uint8_t sg_thrs_y = TMC2130_SG_THRS_Y;
  32. bool skip_debug_msg = false;
  33. //TMC2130 registers
  34. #define TMC2130_REG_GCONF 0x00 // 17 bits
  35. #define TMC2130_REG_GSTAT 0x01 // 3 bits
  36. #define TMC2130_REG_IOIN 0x04 // 8+8 bits
  37. #define TMC2130_REG_IHOLD_IRUN 0x10 // 5+5+4 bits
  38. #define TMC2130_REG_TPOWERDOWN 0x11 // 8 bits
  39. #define TMC2130_REG_TSTEP 0x12 // 20 bits
  40. #define TMC2130_REG_TPWMTHRS 0x13 // 20 bits
  41. #define TMC2130_REG_TCOOLTHRS 0x14 // 20 bits
  42. #define TMC2130_REG_THIGH 0x15 // 20 bits
  43. #define TMC2130_REG_XDIRECT 0x2d // 32 bits
  44. #define TMC2130_REG_VDCMIN 0x33 // 23 bits
  45. #define TMC2130_REG_MSLUT0 0x60 // 32 bits
  46. #define TMC2130_REG_MSLUT1 0x61 // 32 bits
  47. #define TMC2130_REG_MSLUT2 0x62 // 32 bits
  48. #define TMC2130_REG_MSLUT3 0x63 // 32 bits
  49. #define TMC2130_REG_MSLUT4 0x64 // 32 bits
  50. #define TMC2130_REG_MSLUT5 0x65 // 32 bits
  51. #define TMC2130_REG_MSLUT6 0x66 // 32 bits
  52. #define TMC2130_REG_MSLUT7 0x67 // 32 bits
  53. #define TMC2130_REG_MSLUTSEL 0x68 // 32 bits
  54. #define TMC2130_REG_MSLUTSTART 0x69 // 8+8 bits
  55. #define TMC2130_REG_MSCNT 0x6a // 10 bits
  56. #define TMC2130_REG_MSCURACT 0x6b // 9+9 bits
  57. #define TMC2130_REG_CHOPCONF 0x6c // 32 bits
  58. #define TMC2130_REG_COOLCONF 0x6d // 25 bits
  59. #define TMC2130_REG_DCCTRL 0x6e // 24 bits
  60. #define TMC2130_REG_DRV_STATUS 0x6f // 32 bits
  61. #define TMC2130_REG_PWMCONF 0x70 // 22 bits
  62. #define TMC2130_REG_PWM_SCALE 0x71 // 8 bits
  63. #define TMC2130_REG_ENCM_CTRL 0x72 // 2 bits
  64. #define TMC2130_REG_LOST_STEPS 0x73 // 20 bits
  65. uint16_t tmc2130_rd_TSTEP(uint8_t cs);
  66. uint16_t tmc2130_rd_DRV_STATUS(uint8_t chipselect);
  67. void tmc2130_wr_CHOPCONF(uint8_t cs, uint8_t toff = 3, uint8_t hstrt = 4, uint8_t hend = 1, uint8_t fd3 = 0, uint8_t disfdcc = 0, uint8_t rndtf = 0, uint8_t chm = 0, uint8_t tbl = 2, uint8_t vsense = 0, uint8_t vhighfs = 0, uint8_t vhighchm = 0, uint8_t sync = 0, uint8_t mres = 0b0100, uint8_t intpol = 1, uint8_t dedge = 0, uint8_t diss2g = 0);
  68. void tmc2130_wr_PWMCONF(uint8_t cs, uint8_t pwm_ampl, uint8_t pwm_grad, uint8_t pwm_freq, uint8_t pwm_auto, uint8_t pwm_symm, uint8_t freewheel);
  69. void tmc2130_wr_TPWMTHRS(uint8_t cs, uint32_t val32);
  70. void tmc2130_wr_THIGH(uint8_t cs, uint32_t val32);
  71. uint8_t tmc2130_axis_by_cs(uint8_t cs);
  72. uint8_t tmc2130_mres(uint16_t microstep_resolution);
  73. uint8_t tmc2130_wr(uint8_t cs, uint8_t addr, uint32_t wval);
  74. uint8_t tmc2130_rd(uint8_t cs, uint8_t addr, uint32_t* rval);
  75. uint8_t tmc2130_txrx(uint8_t cs, uint8_t addr, uint32_t wval, uint32_t* rval);
  76. void tmc2130_init()
  77. {
  78. MYSERIAL.print("tmc2130_init mode=");
  79. MYSERIAL.println(tmc2130_mode, DEC);
  80. WRITE(X_TMC2130_CS, HIGH);
  81. WRITE(Y_TMC2130_CS, HIGH);
  82. WRITE(Z_TMC2130_CS, HIGH);
  83. WRITE(E0_TMC2130_CS, HIGH);
  84. SET_OUTPUT(X_TMC2130_CS);
  85. SET_OUTPUT(Y_TMC2130_CS);
  86. SET_OUTPUT(Z_TMC2130_CS);
  87. SET_OUTPUT(E0_TMC2130_CS);
  88. SPI.begin();
  89. for (int i = 0; i < 2; i++) // X Y axes
  90. {
  91. uint8_t mres = tmc2130_mres(TMC2130_USTEPS_XY);
  92. tmc2130_wr_CHOPCONF(tmc2130_cs[i], 3, 5, 1, 0, 0, 0, 0, 2, 1, 0, 0, 0, mres, TMC2130_INTPOL_XY, 0, 0);
  93. tmc2130_wr(tmc2130_cs[i], TMC2130_REG_IHOLD_IRUN, 0x000f0000 | ((tmc2130_current_r[i] & 0x1f) << 8) | (tmc2130_current_h[i] & 0x1f));
  94. tmc2130_wr(tmc2130_cs[i], TMC2130_REG_TPOWERDOWN, 0x00000000);
  95. tmc2130_wr(tmc2130_cs[i], TMC2130_REG_GCONF, (tmc2130_mode == TMC2130_MODE_SILENT)?0x00000004:0x00000000);
  96. tmc2130_wr_PWMCONF(tmc2130_cs[i], tmc2130_pwm_ampl[i], tmc2130_pwm_grad[i], tmc2130_pwm_freq[i], tmc2130_pwm_auto[i], 0, 0);
  97. tmc2130_wr_TPWMTHRS(tmc2130_cs[i], TMC2130_TPWMTHRS);
  98. //tmc2130_wr_THIGH(tmc2130_cs[i], TMC2130_THIGH);
  99. }
  100. for (int i = 2; i < 3; i++) // Z axis
  101. {
  102. uint8_t mres = tmc2130_mres(TMC2130_USTEPS_Z);
  103. if (tmc2130_current_r[i] <= 31)
  104. {
  105. tmc2130_wr_CHOPCONF(tmc2130_cs[i], 3, 5, 1, 0, 0, 0, 0, 2, 1, 0, 0, 0, mres, TMC2130_INTPOL_Z, 0, 0);
  106. tmc2130_wr(tmc2130_cs[i], TMC2130_REG_IHOLD_IRUN, 0x000f0000 | ((tmc2130_current_r[i] & 0x1f) << 8) | (tmc2130_current_h[i] & 0x1f));
  107. }
  108. else
  109. {
  110. tmc2130_wr_CHOPCONF(tmc2130_cs[i], 3, 5, 1, 0, 0, 0, 0, 2, 0, 0, 0, 0, mres, TMC2130_INTPOL_Z, 0, 0);
  111. tmc2130_wr(tmc2130_cs[i], TMC2130_REG_IHOLD_IRUN, 0x000f0000 | (((tmc2130_current_r[i] >> 1) & 0x1f) << 8) | ((tmc2130_current_h[i] >> 1) & 0x1f));
  112. }
  113. tmc2130_wr(tmc2130_cs[i], TMC2130_REG_TPOWERDOWN, 0x00000000);
  114. tmc2130_wr(tmc2130_cs[i], TMC2130_REG_GCONF, 0x00000000);
  115. }
  116. for (int i = 3; i < 4; i++) // E axis
  117. {
  118. uint8_t mres = tmc2130_mres(TMC2130_USTEPS_E);
  119. tmc2130_wr_CHOPCONF(tmc2130_cs[i], 3, 5, 1, 0, 0, 0, 0, 2, 1, 0, 0, 0, mres, TMC2130_INTPOL_E, 0, 0);
  120. tmc2130_wr(tmc2130_cs[i], TMC2130_REG_IHOLD_IRUN, 0x000f0000 | ((tmc2130_current_r[i] & 0x1f) << 8) | (tmc2130_current_h[i] & 0x1f));
  121. tmc2130_wr(tmc2130_cs[i], TMC2130_REG_TPOWERDOWN, 0x00000000);
  122. tmc2130_wr(tmc2130_cs[i], TMC2130_REG_GCONF, 0x00000000);
  123. }
  124. }
  125. bool tmc2130_update_sg()
  126. {
  127. #if (defined(TMC2130_SG_HOMING) && defined(TMC2130_SG_HOMING_SW))
  128. if (sg_homing_axes_mask == 0) return false;
  129. #ifdef TMC2130_DEBUG
  130. MYSERIAL.print("tmc2130_update_sg mask=0x");
  131. MYSERIAL.println((int)sg_homing_axes_mask, 16);
  132. #endif //TMC2130_DEBUG
  133. for (uint8_t axis = X_AXIS; axis <= Y_AXIS; axis++) //only X and Y axes
  134. {
  135. uint8_t mask = (X_AXIS_MASK << axis);
  136. if (sg_homing_axes_mask & mask)
  137. {
  138. if (!tmc2130_axis_stalled[axis])
  139. {
  140. uint8_t cs = tmc2130_cs[axis];
  141. uint16_t tstep = tmc2130_rd_TSTEP(cs);
  142. if (tstep < TMC2130_TCOOLTHRS)
  143. {
  144. if(sg_homing_delay < TMC2130_SG_DELAY) // wait for a few tens microsteps until stallGuard is used //todo: read out microsteps directly, instead of delay counter
  145. sg_homing_delay++;
  146. else
  147. {
  148. uint16_t sg = tmc2130_rd_DRV_STATUS(cs) & 0x3ff;
  149. if (sg==0)
  150. {
  151. tmc2130_axis_stalled[axis] = true;
  152. tmc2130_LastHomingStalled = true;
  153. }
  154. // else
  155. // tmc2130_axis_stalled[axis] = false;
  156. }
  157. }
  158. // else
  159. // tmc2130_axis_stalled[axis] = false;
  160. }
  161. }
  162. }
  163. return true;
  164. // else
  165. // {
  166. // tmc2130_axis_stalled[X_AXIS] = false;
  167. // tmc2130_axis_stalled[Y_AXIS] = false;
  168. // }
  169. #endif
  170. }
  171. void tmc2130_check_overtemp()
  172. {
  173. const static char TMC_OVERTEMP_MSG[] PROGMEM = "TMC DRIVER OVERTEMP ";
  174. uint8_t cs[4] = { X_TMC2130_CS, Y_TMC2130_CS, Z_TMC2130_CS, E0_TMC2130_CS };
  175. static uint32_t checktime = 0;
  176. //drivers_disabled[0] = 1; //TEST
  177. if( millis() - checktime > 1000 )
  178. {
  179. //SERIAL_ECHOLNPGM("drv_status:");
  180. for(int i=0;i<4;i++)
  181. {
  182. uint32_t drv_status = 0;
  183. skip_debug_msg = true;
  184. tmc2130_rd(cs[i], TMC2130_REG_DRV_STATUS, &drv_status);
  185. //MYSERIAL.print(drv_status);
  186. //SERIAL_ECHOPGM(" ");
  187. if (drv_status & ((uint32_t)1<<26))
  188. { // BIT 26 - over temp prewarning ~120C (+-20C)
  189. SERIAL_ERRORRPGM(TMC_OVERTEMP_MSG);
  190. SERIAL_ECHOLN(i);
  191. for(int i=0; i < 4; i++)
  192. tmc2130_wr(tmc2130_cs[i], TMC2130_REG_CHOPCONF, 0x00010000);
  193. kill(TMC_OVERTEMP_MSG);
  194. }
  195. }
  196. //SERIAL_ECHOLNPGM("");
  197. checktime = millis();
  198. }
  199. }
  200. void tmc2130_home_enter(uint8_t axes_mask)
  201. {
  202. #ifdef TMC2130_DEBUG
  203. MYSERIAL.print("tmc2130_home_enter mask=0x");
  204. MYSERIAL.println((int)axes_mask, 16);
  205. #endif //TMC2130_DEBUG
  206. #ifdef TMC2130_SG_HOMING
  207. for (uint8_t axis = X_AXIS; axis <= Y_AXIS; axis++) //only X and Y axes
  208. {
  209. uint8_t mask = (X_AXIS_MASK << axis);
  210. if (axes_mask & mask)
  211. {
  212. uint8_t cs = tmc2130_cs[axis];
  213. sg_homing_axes_mask |= mask;
  214. sg_homing_delay = 0;
  215. tmc2130_axis_stalled[axis] = false;
  216. //Configuration to spreadCycle
  217. tmc2130_wr(cs, TMC2130_REG_GCONF, 0x00000000);
  218. tmc2130_wr(cs, TMC2130_REG_COOLCONF, ((axis == X_AXIS)?sg_thrs_x:sg_thrs_y) << 16);
  219. tmc2130_wr(cs, TMC2130_REG_TCOOLTHRS, TMC2130_TCOOLTHRS);
  220. #ifndef TMC2130_SG_HOMING_SW
  221. tmc2130_wr(cs, TMC2130_REG_GCONF, 0x00000080); //stallguard output to DIAG0
  222. #endif //TMC2130_SG_HOMING_SW
  223. }
  224. }
  225. #endif //TMC2130_SG_HOMING
  226. }
  227. void tmc2130_home_exit()
  228. {
  229. #ifdef TMC2130_DEBUG
  230. MYSERIAL.print("tmc2130_home_exit mask=0x");
  231. MYSERIAL.println((int)sg_homing_axes_mask, 16);
  232. #endif //TMC2130_DEBUG
  233. #ifdef TMC2130_SG_HOMING
  234. if (sg_homing_axes_mask)
  235. {
  236. for (uint8_t axis = X_AXIS; axis <= Y_AXIS; axis++) //only X and Y axes
  237. {
  238. uint8_t mask = (X_AXIS_MASK << axis);
  239. if (sg_homing_axes_mask & mask)
  240. {
  241. if (tmc2130_mode == TMC2130_MODE_SILENT)
  242. tmc2130_wr(tmc2130_cs[axis], TMC2130_REG_GCONF, 0x00000004); // Configuration back to stealthChop
  243. else
  244. tmc2130_wr(tmc2130_cs[axis], TMC2130_REG_GCONF, 0x00000000);
  245. }
  246. }
  247. sg_homing_axes_mask = 0x00;
  248. }
  249. #endif
  250. }
  251. uint8_t tmc2130_didLastHomingStall()
  252. {
  253. uint8_t ret = tmc2130_LastHomingStalled;
  254. tmc2130_LastHomingStalled = false;
  255. return ret;
  256. }
  257. void tmc2130_set_current_h(uint8_t axis, uint8_t current)
  258. {
  259. MYSERIAL.print("tmc2130_set_current_h ");
  260. MYSERIAL.print((int)axis);
  261. MYSERIAL.print(" ");
  262. MYSERIAL.println((int)current);
  263. // if (current > 15) current = 15; //current>15 is unsafe
  264. tmc2130_current_h[axis] = current;
  265. tmc2130_wr(tmc2130_cs[axis], TMC2130_REG_IHOLD_IRUN, 0x000f0000 | ((tmc2130_current_r[axis] & 0x1f) << 8) | (tmc2130_current_h[axis] & 0x1f));
  266. }
  267. void tmc2130_set_current_r(uint8_t axis, uint8_t current)
  268. {
  269. MYSERIAL.print("tmc2130_set_current_r ");
  270. MYSERIAL.print((int)axis);
  271. MYSERIAL.print(" ");
  272. MYSERIAL.println((int)current);
  273. // if (current > 15) current = 15; //current>15 is unsafe
  274. tmc2130_current_r[axis] = current;
  275. tmc2130_wr(tmc2130_cs[axis], TMC2130_REG_IHOLD_IRUN, 0x000f0000 | ((tmc2130_current_r[axis] & 0x1f) << 8) | (tmc2130_current_h[axis] & 0x1f));
  276. }
  277. void tmc2130_print_currents()
  278. {
  279. MYSERIAL.println("tmc2130_print_currents");
  280. MYSERIAL.println("\tH\rR");
  281. MYSERIAL.print("X\t");
  282. MYSERIAL.print((int)tmc2130_current_h[0]);
  283. MYSERIAL.print("\t");
  284. MYSERIAL.println((int)tmc2130_current_r[0]);
  285. MYSERIAL.print("Y\t");
  286. MYSERIAL.print((int)tmc2130_current_h[1]);
  287. MYSERIAL.print("\t");
  288. MYSERIAL.println((int)tmc2130_current_r[1]);
  289. MYSERIAL.print("Z\t");
  290. MYSERIAL.print((int)tmc2130_current_h[2]);
  291. MYSERIAL.print("\t");
  292. MYSERIAL.println((int)tmc2130_current_r[2]);
  293. MYSERIAL.print("E\t");
  294. MYSERIAL.print((int)tmc2130_current_h[3]);
  295. MYSERIAL.print("\t");
  296. MYSERIAL.println((int)tmc2130_current_r[3]);
  297. }
  298. void tmc2130_set_pwm_ampl(uint8_t axis, uint8_t pwm_ampl)
  299. {
  300. MYSERIAL.print("tmc2130_set_pwm_ampl ");
  301. MYSERIAL.print((int)axis);
  302. MYSERIAL.print(" ");
  303. MYSERIAL.println((int)pwm_ampl);
  304. tmc2130_pwm_ampl[axis] = pwm_ampl;
  305. if (((axis == 0) || (axis == 1)) && (tmc2130_mode == TMC2130_MODE_SILENT))
  306. tmc2130_wr_PWMCONF(tmc2130_cs[axis], tmc2130_pwm_ampl[axis], tmc2130_pwm_grad[axis], tmc2130_pwm_freq[axis], tmc2130_pwm_auto[axis], 0, 0);
  307. }
  308. void tmc2130_set_pwm_grad(uint8_t axis, uint8_t pwm_grad)
  309. {
  310. MYSERIAL.print("tmc2130_set_pwm_grad ");
  311. MYSERIAL.print((int)axis);
  312. MYSERIAL.print(" ");
  313. MYSERIAL.println((int)pwm_grad);
  314. tmc2130_pwm_grad[axis] = pwm_grad;
  315. if (((axis == 0) || (axis == 1)) && (tmc2130_mode == TMC2130_MODE_SILENT))
  316. tmc2130_wr_PWMCONF(tmc2130_cs[axis], tmc2130_pwm_ampl[axis], tmc2130_pwm_grad[axis], tmc2130_pwm_freq[axis], tmc2130_pwm_auto[axis], 0, 0);
  317. }
  318. uint16_t tmc2130_rd_TSTEP(uint8_t cs)
  319. {
  320. uint32_t val32 = 0;
  321. tmc2130_rd(cs, TMC2130_REG_TSTEP, &val32);
  322. if (val32 & 0x000f0000) return 0xffff;
  323. return val32 & 0xffff;
  324. }
  325. uint16_t tmc2130_rd_DRV_STATUS(uint8_t cs)
  326. {
  327. uint32_t val32 = 0;
  328. tmc2130_rd(cs, TMC2130_REG_DRV_STATUS, &val32);
  329. return val32;
  330. }
  331. void tmc2130_wr_CHOPCONF(uint8_t cs, uint8_t toff, uint8_t hstrt, uint8_t hend, uint8_t fd3, uint8_t disfdcc, uint8_t rndtf, uint8_t chm, uint8_t tbl, uint8_t vsense, uint8_t vhighfs, uint8_t vhighchm, uint8_t sync, uint8_t mres, uint8_t intpol, uint8_t dedge, uint8_t diss2g)
  332. {
  333. uint32_t val = 0;
  334. val |= (uint32_t)(toff & 15);
  335. val |= (uint32_t)(hstrt & 7) << 4;
  336. val |= (uint32_t)(hend & 15) << 7;
  337. val |= (uint32_t)(fd3 & 1) << 11;
  338. val |= (uint32_t)(disfdcc & 1) << 12;
  339. val |= (uint32_t)(rndtf & 1) << 13;
  340. val |= (uint32_t)(chm & 1) << 14;
  341. val |= (uint32_t)(tbl & 3) << 15;
  342. val |= (uint32_t)(vsense & 1) << 17;
  343. val |= (uint32_t)(vhighfs & 1) << 18;
  344. val |= (uint32_t)(vhighchm & 1) << 19;
  345. val |= (uint32_t)(sync & 15) << 20;
  346. val |= (uint32_t)(mres & 15) << 24;
  347. val |= (uint32_t)(intpol & 1) << 28;
  348. val |= (uint32_t)(dedge & 1) << 29;
  349. val |= (uint32_t)(diss2g & 1) << 30;
  350. tmc2130_wr(cs, TMC2130_REG_CHOPCONF, val);
  351. }
  352. //void tmc2130_wr_PWMCONF(uint8_t cs, uint8_t PWMautoScale, uint8_t PWMfreq, uint8_t PWMgrad, uint8_t PWMampl)
  353. void tmc2130_wr_PWMCONF(uint8_t cs, uint8_t pwm_ampl, uint8_t pwm_grad, uint8_t pwm_freq, uint8_t pwm_auto, uint8_t pwm_symm, uint8_t freewheel)
  354. {
  355. uint32_t val = 0;
  356. val |= (uint32_t)(pwm_ampl & 255);
  357. val |= (uint32_t)(pwm_grad & 255) << 8;
  358. val |= (uint32_t)(pwm_freq & 3) << 16;
  359. val |= (uint32_t)(pwm_auto & 1) << 18;
  360. val |= (uint32_t)(pwm_symm & 1) << 19;
  361. val |= (uint32_t)(freewheel & 3) << 20;
  362. tmc2130_wr(cs, TMC2130_REG_PWMCONF, val);
  363. // 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
  364. }
  365. void tmc2130_wr_TPWMTHRS(uint8_t cs, uint32_t val32)
  366. {
  367. tmc2130_wr(cs, TMC2130_REG_TPWMTHRS, val32);
  368. }
  369. void tmc2130_wr_THIGH(uint8_t cs, uint32_t val32)
  370. {
  371. tmc2130_wr(cs, TMC2130_REG_THIGH, val32);
  372. }
  373. uint8_t tmc2130_axis_by_cs(uint8_t cs)
  374. {
  375. switch (cs)
  376. {
  377. case X_TMC2130_CS: return 0;
  378. case Y_TMC2130_CS: return 1;
  379. case Z_TMC2130_CS: return 2;
  380. case E0_TMC2130_CS: return 3;
  381. }
  382. return -1;
  383. }
  384. uint8_t tmc2130_mres(uint16_t microstep_resolution)
  385. {
  386. if (microstep_resolution == 256) return 0b0000;
  387. if (microstep_resolution == 128) return 0b0001;
  388. if (microstep_resolution == 64) return 0b0010;
  389. if (microstep_resolution == 32) return 0b0011;
  390. if (microstep_resolution == 16) return 0b0100;
  391. if (microstep_resolution == 8) return 0b0101;
  392. if (microstep_resolution == 4) return 0b0110;
  393. if (microstep_resolution == 2) return 0b0111;
  394. if (microstep_resolution == 1) return 0b1000;
  395. return 0;
  396. }
  397. uint8_t tmc2130_wr(uint8_t cs, uint8_t addr, uint32_t wval)
  398. {
  399. uint8_t stat = tmc2130_txrx(cs, addr | 0x80, wval, 0);
  400. #ifdef TMC2130_DEBUG_WR
  401. MYSERIAL.print("tmc2130_wr(");
  402. MYSERIAL.print((unsigned char)tmc2130_axis_by_cs(cs), DEC);
  403. MYSERIAL.print(", 0x");
  404. MYSERIAL.print((unsigned char)addr, HEX);
  405. MYSERIAL.print(", 0x");
  406. MYSERIAL.print((unsigned long)wval, HEX);
  407. MYSERIAL.print(")=0x");
  408. MYSERIAL.println((unsigned char)stat, HEX);
  409. #endif //TMC2130_DEBUG_WR
  410. return stat;
  411. }
  412. uint8_t tmc2130_rd(uint8_t cs, uint8_t addr, uint32_t* rval)
  413. {
  414. uint32_t val32 = 0;
  415. uint8_t stat = tmc2130_txrx(cs, addr, 0x00000000, &val32);
  416. if (rval != 0) *rval = val32;
  417. #ifdef TMC2130_DEBUG_RD
  418. if (!skip_debug_msg)
  419. {
  420. MYSERIAL.print("tmc2130_rd(");
  421. MYSERIAL.print((unsigned char)tmc2130_axis_by_cs(cs), DEC);
  422. MYSERIAL.print(", 0x");
  423. MYSERIAL.print((unsigned char)addr, HEX);
  424. MYSERIAL.print(", 0x");
  425. MYSERIAL.print((unsigned long)val32, HEX);
  426. MYSERIAL.print(")=0x");
  427. MYSERIAL.println((unsigned char)stat, HEX);
  428. }
  429. skip_debug_msg = false;
  430. #endif //TMC2130_DEBUG_RD
  431. return stat;
  432. }
  433. uint8_t tmc2130_txrx(uint8_t cs, uint8_t addr, uint32_t wval, uint32_t* rval)
  434. {
  435. //datagram1 - request
  436. SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE3));
  437. digitalWrite(cs, LOW);
  438. SPI.transfer(addr); // address
  439. SPI.transfer((wval >> 24) & 0xff); // MSB
  440. SPI.transfer((wval >> 16) & 0xff);
  441. SPI.transfer((wval >> 8) & 0xff);
  442. SPI.transfer(wval & 0xff); // LSB
  443. digitalWrite(cs, HIGH);
  444. SPI.endTransaction();
  445. //datagram2 - response
  446. SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE3));
  447. digitalWrite(cs, LOW);
  448. uint8_t stat = SPI.transfer(0); // status
  449. uint32_t val32 = 0;
  450. val32 = SPI.transfer(0); // MSB
  451. val32 = (val32 << 8) | SPI.transfer(0);
  452. val32 = (val32 << 8) | SPI.transfer(0);
  453. val32 = (val32 << 8) | SPI.transfer(0); // LSB
  454. digitalWrite(cs, HIGH);
  455. SPI.endTransaction();
  456. if (rval != 0) *rval = val32;
  457. return stat;
  458. }
  459. #endif //HAVE_TMC2130_DRIVERS