tmc2130.cpp 26 KB

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