tmc2130.cpp 17 KB

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