tmc2130.cpp 22 KB

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