tmc2130.cpp 23 KB

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