tmc2130.cpp 20 KB

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