tmc2130.cpp 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  1. //! @file
  2. #include "Marlin.h"
  3. #ifdef TMC2130
  4. #include "tmc2130.h"
  5. #include "ultralcd.h"
  6. #include "language.h"
  7. #include "spi.h"
  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. //mode
  12. uint8_t tmc2130_mode = TMC2130_MODE_NORMAL;
  13. //holding currents
  14. uint8_t tmc2130_current_h[4] = TMC2130_CURRENTS_H;
  15. //running currents
  16. uint8_t tmc2130_current_r[4] = TMC2130_CURRENTS_R;
  17. //running currents for homing
  18. uint8_t tmc2130_current_r_home[4] = TMC2130_CURRENTS_R_HOME;
  19. //pwm_ampl
  20. uint8_t tmc2130_pwm_ampl[4] = {TMC2130_PWM_AMPL_X, TMC2130_PWM_AMPL_Y, TMC2130_PWM_AMPL_Z, TMC2130_PWM_AMPL_E};
  21. //pwm_grad
  22. uint8_t tmc2130_pwm_grad[4] = {TMC2130_PWM_GRAD_X, TMC2130_PWM_GRAD_Y, TMC2130_PWM_GRAD_Z, TMC2130_PWM_GRAD_E};
  23. //pwm_auto
  24. uint8_t tmc2130_pwm_auto[4] = {TMC2130_PWM_AUTO_X, TMC2130_PWM_AUTO_Y, TMC2130_PWM_AUTO_Z, TMC2130_PWM_AUTO_E};
  25. //pwm_freq
  26. uint8_t tmc2130_pwm_freq[4] = {TMC2130_PWM_FREQ_X, TMC2130_PWM_FREQ_Y, TMC2130_PWM_FREQ_Z, TMC2130_PWM_FREQ_E};
  27. uint8_t tmc2130_mres[4] = {0, 0, 0, 0}; //will be filed at begin of init
  28. uint8_t tmc2130_sg_thr[4] = {TMC2130_SG_THRS_X, TMC2130_SG_THRS_Y, TMC2130_SG_THRS_Z, TMC2130_SG_THRS_E};
  29. uint8_t tmc2130_sg_thr_home[4] = TMC2130_SG_THRS_HOME;
  30. uint8_t tmc2130_sg_homing_axes_mask = 0x00;
  31. uint8_t tmc2130_sg_meassure = 0xff;
  32. uint32_t tmc2130_sg_meassure_cnt = 0;
  33. uint32_t tmc2130_sg_meassure_val = 0;
  34. uint8_t tmc2130_home_enabled = 0;
  35. uint8_t tmc2130_home_origin[2] = {0, 0};
  36. uint8_t tmc2130_home_bsteps[2] = {48, 48};
  37. uint8_t tmc2130_home_fsteps[2] = {48, 48};
  38. uint8_t tmc2130_wave_fac[4] = {0, 0, 0, 0};
  39. tmc2130_chopper_config_t tmc2130_chopper_config[4] = {
  40. {TMC2130_TOFF_XYZ, 5, 1, 2, 0},
  41. {TMC2130_TOFF_XYZ, 5, 1, 2, 0},
  42. {TMC2130_TOFF_XYZ, 5, 1, 2, 0},
  43. {TMC2130_TOFF_E, 5, 1, 2, 0}
  44. };
  45. bool tmc2130_sg_stop_on_crash = true;
  46. uint8_t tmc2130_sg_diag_mask = 0x00;
  47. uint8_t tmc2130_sg_crash = 0;
  48. uint16_t tmc2130_sg_err[4] = {0, 0, 0, 0};
  49. uint16_t tmc2130_sg_cnt[4] = {0, 0, 0, 0};
  50. bool tmc2130_sg_change = false;
  51. bool skip_debug_msg = false;
  52. #define DBG(args...)
  53. //printf_P(args)
  54. #ifndef _n
  55. #define _n PSTR
  56. #endif //_n
  57. #ifndef _i
  58. #define _i PSTR
  59. #endif //_i
  60. //TMC2130 registers
  61. #define TMC2130_REG_GCONF 0x00 // 17 bits
  62. #define TMC2130_REG_GSTAT 0x01 // 3 bits
  63. #define TMC2130_REG_IOIN 0x04 // 8+8 bits
  64. #define TMC2130_REG_IHOLD_IRUN 0x10 // 5+5+4 bits
  65. #define TMC2130_REG_TPOWERDOWN 0x11 // 8 bits
  66. #define TMC2130_REG_TSTEP 0x12 // 20 bits
  67. #define TMC2130_REG_TPWMTHRS 0x13 // 20 bits
  68. #define TMC2130_REG_TCOOLTHRS 0x14 // 20 bits
  69. #define TMC2130_REG_THIGH 0x15 // 20 bits
  70. #define TMC2130_REG_XDIRECT 0x2d // 32 bits
  71. #define TMC2130_REG_VDCMIN 0x33 // 23 bits
  72. #define TMC2130_REG_MSLUT0 0x60 // 32 bits
  73. #define TMC2130_REG_MSLUT1 0x61 // 32 bits
  74. #define TMC2130_REG_MSLUT2 0x62 // 32 bits
  75. #define TMC2130_REG_MSLUT3 0x63 // 32 bits
  76. #define TMC2130_REG_MSLUT4 0x64 // 32 bits
  77. #define TMC2130_REG_MSLUT5 0x65 // 32 bits
  78. #define TMC2130_REG_MSLUT6 0x66 // 32 bits
  79. #define TMC2130_REG_MSLUT7 0x67 // 32 bits
  80. #define TMC2130_REG_MSLUTSEL 0x68 // 32 bits
  81. #define TMC2130_REG_MSLUTSTART 0x69 // 8+8 bits
  82. #define TMC2130_REG_MSCNT 0x6a // 10 bits
  83. #define TMC2130_REG_MSCURACT 0x6b // 9+9 bits
  84. #define TMC2130_REG_CHOPCONF 0x6c // 32 bits
  85. #define TMC2130_REG_COOLCONF 0x6d // 25 bits
  86. #define TMC2130_REG_DCCTRL 0x6e // 24 bits
  87. #define TMC2130_REG_DRV_STATUS 0x6f // 32 bits
  88. #define TMC2130_REG_PWMCONF 0x70 // 22 bits
  89. #define TMC2130_REG_PWM_SCALE 0x71 // 8 bits
  90. #define TMC2130_REG_ENCM_CTRL 0x72 // 2 bits
  91. #define TMC2130_REG_LOST_STEPS 0x73 // 20 bits
  92. uint16_t tmc2130_rd_TSTEP(uint8_t axis);
  93. uint16_t tmc2130_rd_MSCNT(uint8_t axis);
  94. uint32_t tmc2130_rd_MSCURACT(uint8_t axis);
  95. void tmc2130_wr_CHOPCONF(uint8_t axis, 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);
  96. void tmc2130_wr_PWMCONF(uint8_t axis, uint8_t pwm_ampl, uint8_t pwm_grad, uint8_t pwm_freq, uint8_t pwm_auto, uint8_t pwm_symm, uint8_t freewheel);
  97. void tmc2130_wr_TPWMTHRS(uint8_t axis, uint32_t val32);
  98. void tmc2130_wr_THIGH(uint8_t axis, uint32_t val32);
  99. #define tmc2130_rd(axis, addr, rval) tmc2130_rx(axis, addr, rval)
  100. #define tmc2130_wr(axis, addr, wval) tmc2130_tx(axis, (addr) | 0x80, wval)
  101. static void tmc2130_tx(uint8_t axis, uint8_t addr, uint32_t wval);
  102. static uint8_t tmc2130_rx(uint8_t axis, uint8_t addr, uint32_t* rval);
  103. void tmc2130_setup_chopper(uint8_t axis, uint8_t mres, uint8_t current_h, uint8_t current_r);
  104. uint16_t __tcoolthrs(uint8_t axis)
  105. {
  106. switch (axis)
  107. {
  108. case X_AXIS: return TMC2130_TCOOLTHRS_X;
  109. case Y_AXIS: return TMC2130_TCOOLTHRS_Y;
  110. case Z_AXIS: return TMC2130_TCOOLTHRS_Z;
  111. }
  112. return 0;
  113. }
  114. #ifdef PSU_Delta
  115. void tmc2130_init(bool bSupressFlag)
  116. #else
  117. void tmc2130_init()
  118. #endif
  119. {
  120. // DBG(_n("tmc2130_init(), mode=%S\n"), tmc2130_mode?_n("STEALTH"):_n("NORMAL"));
  121. WRITE(X_TMC2130_CS, HIGH);
  122. WRITE(Y_TMC2130_CS, HIGH);
  123. WRITE(Z_TMC2130_CS, HIGH);
  124. WRITE(E0_TMC2130_CS, HIGH);
  125. SET_OUTPUT(X_TMC2130_CS);
  126. SET_OUTPUT(Y_TMC2130_CS);
  127. SET_OUTPUT(Z_TMC2130_CS);
  128. SET_OUTPUT(E0_TMC2130_CS);
  129. SET_INPUT(X_TMC2130_DIAG);
  130. SET_INPUT(Y_TMC2130_DIAG);
  131. SET_INPUT(Z_TMC2130_DIAG);
  132. SET_INPUT(E0_TMC2130_DIAG);
  133. for (uint_least8_t axis = 0; axis < 2; axis++) // X Y axes
  134. {
  135. tmc2130_setup_chopper(axis, tmc2130_mres[axis], tmc2130_current_h[axis], tmc2130_current_r[axis]);
  136. tmc2130_wr(axis, TMC2130_REG_TPOWERDOWN, 0x00000000);
  137. tmc2130_wr(axis, TMC2130_REG_COOLCONF, (((uint32_t)tmc2130_sg_thr[axis]) << 16));
  138. tmc2130_wr(axis, TMC2130_REG_TCOOLTHRS, (tmc2130_mode == TMC2130_MODE_SILENT)?0:__tcoolthrs(axis));
  139. tmc2130_wr(axis, TMC2130_REG_GCONF, (tmc2130_mode == TMC2130_MODE_SILENT)?TMC2130_GCONF_SILENT:TMC2130_GCONF_SGSENS);
  140. tmc2130_wr_PWMCONF(axis, tmc2130_pwm_ampl[axis], tmc2130_pwm_grad[axis], tmc2130_pwm_freq[axis], tmc2130_pwm_auto[axis], 0, 0);
  141. tmc2130_wr_TPWMTHRS(axis, TMC2130_TPWMTHRS);
  142. //tmc2130_wr_THIGH(axis, TMC2130_THIGH);
  143. }
  144. for (uint_least8_t axis = 2; axis < 3; axis++) // Z axis
  145. {
  146. tmc2130_setup_chopper(axis, tmc2130_mres[axis], tmc2130_current_h[axis], tmc2130_current_r[axis]);
  147. tmc2130_wr(axis, TMC2130_REG_TPOWERDOWN, 0x00000000);
  148. #ifndef TMC2130_STEALTH_Z
  149. tmc2130_wr(axis, TMC2130_REG_GCONF, TMC2130_GCONF_SGSENS);
  150. #else //TMC2130_STEALTH_Z
  151. tmc2130_wr(axis, TMC2130_REG_COOLCONF, (((uint32_t)tmc2130_sg_thr[axis]) << 16));
  152. tmc2130_wr(axis, TMC2130_REG_TCOOLTHRS, (tmc2130_mode == TMC2130_MODE_SILENT)?0:__tcoolthrs(axis));
  153. tmc2130_wr(axis, TMC2130_REG_GCONF, (tmc2130_mode == TMC2130_MODE_SILENT)?TMC2130_GCONF_SILENT:TMC2130_GCONF_SGSENS);
  154. tmc2130_wr_PWMCONF(axis, tmc2130_pwm_ampl[axis], tmc2130_pwm_grad[axis], tmc2130_pwm_freq[axis], tmc2130_pwm_auto[axis], 0, 0);
  155. tmc2130_wr_TPWMTHRS(axis, TMC2130_TPWMTHRS);
  156. #endif //TMC2130_STEALTH_Z
  157. }
  158. for (uint_least8_t axis = 3; axis < 4; axis++) // E axis
  159. {
  160. tmc2130_setup_chopper(axis, tmc2130_mres[axis], tmc2130_current_h[axis], tmc2130_current_r[axis]);
  161. tmc2130_wr(axis, TMC2130_REG_TPOWERDOWN, 0x00000000);
  162. #ifndef TMC2130_STEALTH_E
  163. tmc2130_wr(axis, TMC2130_REG_GCONF, TMC2130_GCONF_SGSENS);
  164. #else //TMC2130_STEALTH_E
  165. tmc2130_wr(axis, TMC2130_REG_COOLCONF, (((uint32_t)tmc2130_sg_thr[axis]) << 16));
  166. tmc2130_wr(axis, TMC2130_REG_TCOOLTHRS, 0);
  167. tmc2130_wr(axis, TMC2130_REG_GCONF, TMC2130_GCONF_SILENT);
  168. tmc2130_wr_PWMCONF(axis, tmc2130_pwm_ampl[axis], tmc2130_pwm_grad[axis], tmc2130_pwm_freq[axis], tmc2130_pwm_auto[axis], 0, 0);
  169. tmc2130_wr_TPWMTHRS(axis, TMC2130_TPWMTHRS);
  170. #endif //TMC2130_STEALTH_E
  171. }
  172. tmc2130_sg_err[0] = 0;
  173. tmc2130_sg_err[1] = 0;
  174. tmc2130_sg_err[2] = 0;
  175. tmc2130_sg_err[3] = 0;
  176. tmc2130_sg_cnt[0] = 0;
  177. tmc2130_sg_cnt[1] = 0;
  178. tmc2130_sg_cnt[2] = 0;
  179. tmc2130_sg_cnt[3] = 0;
  180. #ifdef TMC2130_LINEARITY_CORRECTION
  181. #ifdef TMC2130_LINEARITY_CORRECTION_XYZ
  182. tmc2130_set_wave(X_AXIS, 247, tmc2130_wave_fac[X_AXIS]);
  183. tmc2130_set_wave(Y_AXIS, 247, tmc2130_wave_fac[Y_AXIS]);
  184. tmc2130_set_wave(Z_AXIS, 247, tmc2130_wave_fac[Z_AXIS]);
  185. #endif //TMC2130_LINEARITY_CORRECTION_XYZ
  186. tmc2130_set_wave(E_AXIS, 247, tmc2130_wave_fac[E_AXIS]);
  187. #endif //TMC2130_LINEARITY_CORRECTION
  188. #ifdef PSU_Delta
  189. if(!bSupressFlag)
  190. check_force_z();
  191. #endif // PSU_Delta
  192. }
  193. uint8_t tmc2130_sample_diag()
  194. {
  195. uint8_t mask = 0;
  196. if (READ(X_TMC2130_DIAG)) mask |= X_AXIS_MASK;
  197. if (READ(Y_TMC2130_DIAG)) mask |= Y_AXIS_MASK;
  198. // if (READ(Z_TMC2130_DIAG)) mask |= Z_AXIS_MASK;
  199. // if (READ(E0_TMC2130_DIAG)) mask |= E_AXIS_MASK;
  200. return mask;
  201. }
  202. extern bool is_usb_printing;
  203. void tmc2130_st_isr()
  204. {
  205. if (tmc2130_mode == TMC2130_MODE_SILENT || tmc2130_sg_stop_on_crash == false) return;
  206. uint8_t crash = 0;
  207. uint8_t diag_mask = tmc2130_sample_diag();
  208. // for (uint8_t axis = X_AXIS; axis <= E_AXIS; axis++)
  209. for (uint8_t axis = X_AXIS; axis <= Z_AXIS; axis++)
  210. {
  211. uint8_t mask = (X_AXIS_MASK << axis);
  212. if (diag_mask & mask) tmc2130_sg_err[axis]++;
  213. else
  214. if (tmc2130_sg_err[axis] > 0) tmc2130_sg_err[axis]--;
  215. if (tmc2130_sg_cnt[axis] < tmc2130_sg_err[axis])
  216. {
  217. tmc2130_sg_cnt[axis] = tmc2130_sg_err[axis];
  218. tmc2130_sg_change = true;
  219. uint8_t sg_thr = 64;
  220. // if (axis == Y_AXIS) sg_thr = 64;
  221. if (tmc2130_sg_err[axis] >= sg_thr)
  222. {
  223. tmc2130_sg_err[axis] = 0;
  224. crash |= mask;
  225. }
  226. }
  227. }
  228. if (tmc2130_sg_homing_axes_mask == 0)
  229. {
  230. if (tmc2130_sg_stop_on_crash && crash)
  231. {
  232. tmc2130_sg_crash = crash;
  233. tmc2130_sg_stop_on_crash = false;
  234. crashdet_stop_and_save_print();
  235. }
  236. }
  237. }
  238. bool tmc2130_update_sg()
  239. {
  240. if (tmc2130_sg_meassure <= E_AXIS)
  241. {
  242. uint32_t val32 = 0;
  243. tmc2130_rd(tmc2130_sg_meassure, TMC2130_REG_DRV_STATUS, &val32);
  244. tmc2130_sg_meassure_val += (val32 & 0x3ff);
  245. tmc2130_sg_meassure_cnt++;
  246. return true;
  247. }
  248. return false;
  249. }
  250. void tmc2130_home_enter(uint8_t axes_mask)
  251. {
  252. printf_P(PSTR("tmc2130_home_enter(axes_mask=0x%02x)\n"), axes_mask);
  253. #ifdef TMC2130_SG_HOMING
  254. if (axes_mask & 0x03) //X or Y
  255. tmc2130_wait_standstill_xy(1000);
  256. for (uint8_t axis = X_AXIS; axis <= Z_AXIS; axis++) //X Y and Z axes
  257. {
  258. uint8_t mask = (X_AXIS_MASK << axis);
  259. if (axes_mask & mask)
  260. {
  261. tmc2130_sg_homing_axes_mask |= mask;
  262. //Configuration to spreadCycle
  263. tmc2130_wr(axis, TMC2130_REG_GCONF, TMC2130_GCONF_NORMAL);
  264. tmc2130_wr(axis, TMC2130_REG_COOLCONF, (((uint32_t)tmc2130_sg_thr_home[axis]) << 16));
  265. // tmc2130_wr(axis, TMC2130_REG_COOLCONF, (((uint32_t)tmc2130_sg_thr[axis]) << 16) | ((uint32_t)1 << 24));
  266. tmc2130_wr(axis, TMC2130_REG_TCOOLTHRS, __tcoolthrs(axis));
  267. tmc2130_setup_chopper(axis, tmc2130_mres[axis], tmc2130_current_h[axis], tmc2130_current_r_home[axis]);
  268. if (mask & (X_AXIS_MASK | Y_AXIS_MASK | Z_AXIS_MASK))
  269. tmc2130_wr(axis, TMC2130_REG_GCONF, TMC2130_GCONF_SGSENS); //stallguard output DIAG1, DIAG1 = pushpull
  270. }
  271. }
  272. #endif //TMC2130_SG_HOMING
  273. }
  274. void tmc2130_home_exit()
  275. {
  276. printf_P(PSTR("tmc2130_home_exit tmc2130_sg_homing_axes_mask=0x%02x\n"), tmc2130_sg_homing_axes_mask);
  277. #ifdef TMC2130_SG_HOMING
  278. if (tmc2130_sg_homing_axes_mask & 0x03) //X or Y
  279. tmc2130_wait_standstill_xy(1000);
  280. if (tmc2130_sg_homing_axes_mask)
  281. {
  282. for (uint8_t axis = X_AXIS; axis <= Z_AXIS; axis++) //X Y and Z axes
  283. {
  284. uint8_t mask = (X_AXIS_MASK << axis);
  285. if (tmc2130_sg_homing_axes_mask & mask & (X_AXIS_MASK | Y_AXIS_MASK | Z_AXIS_MASK))
  286. {
  287. #ifndef TMC2130_STEALTH_Z
  288. if ((tmc2130_mode == TMC2130_MODE_SILENT) && (axis != Z_AXIS))
  289. #else //TMC2130_STEALTH_Z
  290. if (tmc2130_mode == TMC2130_MODE_SILENT)
  291. #endif //TMC2130_STEALTH_Z
  292. {
  293. tmc2130_wr(axis, TMC2130_REG_GCONF, TMC2130_GCONF_SILENT); // Configuration back to stealthChop
  294. tmc2130_wr(axis, TMC2130_REG_TCOOLTHRS, 0);
  295. // tmc2130_wr_PWMCONF(i, tmc2130_pwm_ampl[i], tmc2130_pwm_grad[i], tmc2130_pwm_freq[i], tmc2130_pwm_auto[i], 0, 0);
  296. }
  297. else
  298. {
  299. // tmc2130_wr(axis, TMC2130_REG_GCONF, TMC2130_GCONF_NORMAL);
  300. tmc2130_setup_chopper(axis, tmc2130_mres[axis], tmc2130_current_h[axis], tmc2130_current_r[axis]);
  301. // tmc2130_wr(axis, TMC2130_REG_COOLCONF, (((uint32_t)tmc2130_sg_thr[axis]) << 16) | ((uint32_t)1 << 24));
  302. tmc2130_wr(axis, TMC2130_REG_COOLCONF, (((uint32_t)tmc2130_sg_thr[axis]) << 16));
  303. tmc2130_wr(axis, TMC2130_REG_TCOOLTHRS, __tcoolthrs(axis));
  304. tmc2130_wr(axis, TMC2130_REG_GCONF, TMC2130_GCONF_SGSENS);
  305. }
  306. }
  307. }
  308. tmc2130_sg_homing_axes_mask = 0x00;
  309. }
  310. tmc2130_sg_crash = false;
  311. #endif
  312. }
  313. void tmc2130_sg_meassure_start(uint8_t axis)
  314. {
  315. tmc2130_sg_meassure = axis;
  316. tmc2130_sg_meassure_cnt = 0;
  317. tmc2130_sg_meassure_val = 0;
  318. }
  319. uint16_t tmc2130_sg_meassure_stop()
  320. {
  321. tmc2130_sg_meassure = 0xff;
  322. return tmc2130_sg_meassure_val / tmc2130_sg_meassure_cnt;
  323. }
  324. bool tmc2130_wait_standstill_xy(int timeout)
  325. {
  326. // DBG(_n("tmc2130_wait_standstill_xy(timeout=%d)\n"), timeout);
  327. bool standstill = false;
  328. while (!standstill && (timeout > 0))
  329. {
  330. uint32_t drv_status_x = 0;
  331. uint32_t drv_status_y = 0;
  332. tmc2130_rd(X_AXIS, TMC2130_REG_DRV_STATUS, &drv_status_x);
  333. tmc2130_rd(Y_AXIS, TMC2130_REG_DRV_STATUS, &drv_status_y);
  334. // DBG(_n("\tdrv_status_x=0x%08x drv_status_x=0x%08x\n"), drv_status_x, drv_status_y);
  335. standstill = (drv_status_x & 0x80000000) && (drv_status_y & 0x80000000);
  336. tmc2130_check_overtemp();
  337. timeout--;
  338. }
  339. return standstill;
  340. }
  341. void tmc2130_check_overtemp()
  342. {
  343. static uint32_t checktime = 0;
  344. if (_millis() - checktime > 1000 )
  345. {
  346. for (uint_least8_t i = 0; i < 4; i++)
  347. {
  348. uint32_t drv_status = 0;
  349. skip_debug_msg = true;
  350. tmc2130_rd(i, TMC2130_REG_DRV_STATUS, &drv_status);
  351. if (drv_status & ((uint32_t)1 << 26))
  352. { // BIT 26 - over temp prewarning ~120C (+-20C)
  353. SERIAL_ERRORRPGM(MSG_TMC_OVERTEMP);
  354. SERIAL_ECHOLN(i);
  355. for (uint_least8_t j = 0; j < 4; j++)
  356. tmc2130_wr(j, TMC2130_REG_CHOPCONF, 0x00010000);
  357. kill(MSG_TMC_OVERTEMP);
  358. }
  359. }
  360. checktime = _millis();
  361. tmc2130_sg_change = true;
  362. }
  363. #ifdef DEBUG_CRASHDET_COUNTERS
  364. if (tmc2130_sg_change)
  365. {
  366. for (int i = 0; i < 4; i++)
  367. {
  368. tmc2130_sg_change = false;
  369. lcd_set_cursor(0 + i*4, 3);
  370. lcd_print(itostr3(tmc2130_sg_cnt[i]));
  371. lcd_print(' ');
  372. }
  373. }
  374. #endif //DEBUG_CRASHDET_COUNTERS
  375. }
  376. void tmc2130_setup_chopper(uint8_t axis, uint8_t mres, uint8_t current_h, uint8_t current_r)
  377. {
  378. uint8_t intpol = (mres != 0); // intpol to 256 only if microsteps aren't 256
  379. uint8_t toff = tmc2130_chopper_config[axis].toff; // toff = 3 (fchop = 27.778kHz)
  380. uint8_t hstrt = tmc2130_chopper_config[axis].hstr; //initial 4, modified to 5
  381. uint8_t hend = tmc2130_chopper_config[axis].hend; //original value = 1
  382. uint8_t fd3 = 0;
  383. uint8_t rndtf = 0; //random off time
  384. uint8_t chm = 0; //spreadCycle
  385. uint8_t tbl = tmc2130_chopper_config[axis].tbl; //blanking time, original value = 2
  386. if (axis == E_AXIS)
  387. {
  388. #ifdef TMC2130_CNSTOFF_E
  389. // fd = 0 (slow decay only)
  390. hstrt = 0; //fd0..2
  391. fd3 = 0; //fd3
  392. hend = 0; //sine wave offset
  393. chm = 1; // constant off time mod
  394. #endif //TMC2130_CNSTOFF_E
  395. // toff = TMC2130_TOFF_E; // toff = 3-5
  396. // rndtf = 1;
  397. }
  398. // DBG(_n("tmc2130_setup_chopper(axis=%hhd, mres=%hhd, curh=%hhd, curr=%hhd\n"), axis, mres, current_h, current_r);
  399. // DBG(_n(" toff=%hhd, hstr=%hhd, hend=%hhd, tbl=%hhd\n"), toff, hstrt, hend, tbl);
  400. if (current_r <= 31)
  401. {
  402. tmc2130_wr_CHOPCONF(axis, toff, hstrt, hend, fd3, 0, rndtf, chm, tbl, 1, 0, 0, 0, mres, intpol, 0, 0);
  403. tmc2130_wr(axis, TMC2130_REG_IHOLD_IRUN, 0x000f0000 | ((current_r & 0x1f) << 8) | (current_h & 0x1f));
  404. }
  405. else
  406. {
  407. tmc2130_wr_CHOPCONF(axis, toff, hstrt, hend, fd3, 0, rndtf, chm, tbl, 0, 0, 0, 0, mres, intpol, 0, 0);
  408. tmc2130_wr(axis, TMC2130_REG_IHOLD_IRUN, 0x000f0000 | (((current_r >> 1) & 0x1f) << 8) | ((current_h >> 1) & 0x1f));
  409. }
  410. }
  411. void tmc2130_set_current_h(uint8_t axis, uint8_t current)
  412. {
  413. // DBG(_n("tmc2130_set_current_h(axis=%d, current=%d\n"), axis, current);
  414. tmc2130_current_h[axis] = current;
  415. tmc2130_setup_chopper(axis, tmc2130_mres[axis], tmc2130_current_h[axis], tmc2130_current_r[axis]);
  416. }
  417. void tmc2130_set_current_r(uint8_t axis, uint8_t current)
  418. {
  419. // DBG(_n("tmc2130_set_current_r(axis=%d, current=%d\n"), axis, current);
  420. tmc2130_current_r[axis] = current;
  421. tmc2130_setup_chopper(axis, tmc2130_mres[axis], tmc2130_current_h[axis], tmc2130_current_r[axis]);
  422. }
  423. void tmc2130_print_currents()
  424. {
  425. printf_P(_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"),
  426. tmc2130_current_h[0], tmc2130_current_r[0],
  427. tmc2130_current_h[1], tmc2130_current_r[1],
  428. tmc2130_current_h[2], tmc2130_current_r[2],
  429. tmc2130_current_h[3], tmc2130_current_r[3]
  430. );
  431. }
  432. void tmc2130_set_pwm_ampl(uint8_t axis, uint8_t pwm_ampl)
  433. {
  434. // DBG(_n("tmc2130_set_pwm_ampl(axis=%hhd, pwm_ampl=%hhd\n"), axis, pwm_ampl);
  435. tmc2130_pwm_ampl[axis] = pwm_ampl;
  436. if (((axis == 0) || (axis == 1)) && (tmc2130_mode == TMC2130_MODE_SILENT))
  437. tmc2130_wr_PWMCONF(axis, tmc2130_pwm_ampl[axis], tmc2130_pwm_grad[axis], tmc2130_pwm_freq[axis], tmc2130_pwm_auto[axis], 0, 0);
  438. }
  439. void tmc2130_set_pwm_grad(uint8_t axis, uint8_t pwm_grad)
  440. {
  441. // DBG(_n("tmc2130_set_pwm_grad(axis=%hhd, pwm_grad=%hhd\n"), axis, pwm_grad);
  442. tmc2130_pwm_grad[axis] = pwm_grad;
  443. if (((axis == 0) || (axis == 1)) && (tmc2130_mode == TMC2130_MODE_SILENT))
  444. tmc2130_wr_PWMCONF(axis, tmc2130_pwm_ampl[axis], tmc2130_pwm_grad[axis], tmc2130_pwm_freq[axis], tmc2130_pwm_auto[axis], 0, 0);
  445. }
  446. uint16_t tmc2130_rd_TSTEP(uint8_t axis)
  447. {
  448. uint32_t val32 = 0;
  449. tmc2130_rd(axis, TMC2130_REG_TSTEP, &val32);
  450. if (val32 & 0x000f0000) return 0xffff;
  451. return val32 & 0xffff;
  452. }
  453. uint16_t tmc2130_rd_MSCNT(uint8_t axis)
  454. {
  455. uint32_t val32 = 0;
  456. tmc2130_rd(axis, TMC2130_REG_MSCNT, &val32);
  457. return val32 & 0x3ff;
  458. }
  459. uint32_t tmc2130_rd_MSCURACT(uint8_t axis)
  460. {
  461. uint32_t val32 = 0;
  462. tmc2130_rd(axis, TMC2130_REG_MSCURACT, &val32);
  463. return val32;
  464. }
  465. void tmc2130_wr_MSLUTSTART(uint8_t axis, uint8_t start_sin, uint8_t start_sin90)
  466. {
  467. uint32_t val = 0;
  468. val |= (uint32_t)start_sin;
  469. val |= ((uint32_t)start_sin90) << 16;
  470. tmc2130_wr(axis, TMC2130_REG_MSLUTSTART, val);
  471. //printf_P(PSTR("MSLUTSTART=%08lx (start_sin=%d start_sin90=%d)\n"), val, start_sin, start_sin90);
  472. }
  473. void tmc2130_wr_MSLUTSEL(uint8_t axis, uint8_t x1, uint8_t x2, uint8_t x3, uint8_t w0, uint8_t w1, uint8_t w2, uint8_t w3)
  474. {
  475. uint32_t val = 0;
  476. val |= ((uint32_t)w0);
  477. val |= ((uint32_t)w1) << 2;
  478. val |= ((uint32_t)w2) << 4;
  479. val |= ((uint32_t)w3) << 6;
  480. val |= ((uint32_t)x1) << 8;
  481. val |= ((uint32_t)x2) << 16;
  482. val |= ((uint32_t)x3) << 24;
  483. tmc2130_wr(axis, TMC2130_REG_MSLUTSEL, val);
  484. //printf_P(PSTR("MSLUTSEL=%08lx (x1=%d x2=%d x3=%d w0=%d w1=%d w2=%d w3=%d)\n"), val, x1, x2, x3, w0, w1, w2, w3);
  485. }
  486. void tmc2130_wr_MSLUT(uint8_t axis, uint8_t i, uint32_t val)
  487. {
  488. tmc2130_wr(axis, TMC2130_REG_MSLUT0 + (i & 7), val);
  489. //printf_P(PSTR("MSLUT[%d]=%08lx\n"), i, val);
  490. }
  491. void tmc2130_wr_CHOPCONF(uint8_t axis, 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)
  492. {
  493. uint32_t val = 0;
  494. val |= (uint32_t)(toff & 15);
  495. val |= (uint32_t)(hstrt & 7) << 4;
  496. val |= (uint32_t)(hend & 15) << 7;
  497. val |= (uint32_t)(fd3 & 1) << 11;
  498. val |= (uint32_t)(disfdcc & 1) << 12;
  499. val |= (uint32_t)(rndtf & 1) << 13;
  500. val |= (uint32_t)(chm & 1) << 14;
  501. val |= (uint32_t)(tbl & 3) << 15;
  502. val |= (uint32_t)(vsense & 1) << 17;
  503. val |= (uint32_t)(vhighfs & 1) << 18;
  504. val |= (uint32_t)(vhighchm & 1) << 19;
  505. val |= (uint32_t)(sync & 15) << 20;
  506. val |= (uint32_t)(mres & 15) << 24;
  507. val |= (uint32_t)(intpol & 1) << 28;
  508. val |= (uint32_t)(dedge & 1) << 29;
  509. val |= (uint32_t)(diss2g & 1) << 30;
  510. tmc2130_wr(axis, TMC2130_REG_CHOPCONF, val);
  511. }
  512. //void tmc2130_wr_PWMCONF(uint8_t axis, uint8_t PWMautoScale, uint8_t PWMfreq, uint8_t PWMgrad, uint8_t PWMampl)
  513. void tmc2130_wr_PWMCONF(uint8_t axis, uint8_t pwm_ampl, uint8_t pwm_grad, uint8_t pwm_freq, uint8_t pwm_auto, uint8_t pwm_symm, uint8_t freewheel)
  514. {
  515. uint32_t val = 0;
  516. val |= (uint32_t)(pwm_ampl & 255);
  517. val |= (uint32_t)(pwm_grad & 255) << 8;
  518. val |= (uint32_t)(pwm_freq & 3) << 16;
  519. val |= (uint32_t)(pwm_auto & 1) << 18;
  520. val |= (uint32_t)(pwm_symm & 1) << 19;
  521. val |= (uint32_t)(freewheel & 3) << 20;
  522. tmc2130_wr(axis, TMC2130_REG_PWMCONF, val);
  523. // tmc2130_wr(axis, 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
  524. }
  525. void tmc2130_wr_TPWMTHRS(uint8_t axis, uint32_t val32)
  526. {
  527. tmc2130_wr(axis, TMC2130_REG_TPWMTHRS, val32);
  528. }
  529. void tmc2130_wr_THIGH(uint8_t axis, uint32_t val32)
  530. {
  531. tmc2130_wr(axis, TMC2130_REG_THIGH, val32);
  532. }
  533. uint8_t tmc2130_usteps2mres(uint16_t usteps)
  534. {
  535. uint8_t mres = 8; while (usteps >>= 1) mres--;
  536. return mres;
  537. }
  538. inline void tmc2130_cs_low(uint8_t axis)
  539. {
  540. switch (axis)
  541. {
  542. case X_AXIS: WRITE(X_TMC2130_CS, LOW); break;
  543. case Y_AXIS: WRITE(Y_TMC2130_CS, LOW); break;
  544. case Z_AXIS: WRITE(Z_TMC2130_CS, LOW); break;
  545. case E_AXIS: WRITE(E0_TMC2130_CS, LOW); break;
  546. }
  547. }
  548. inline void tmc2130_cs_high(uint8_t axis)
  549. {
  550. switch (axis)
  551. {
  552. case X_AXIS: WRITE(X_TMC2130_CS, HIGH); break;
  553. case Y_AXIS: WRITE(Y_TMC2130_CS, HIGH); break;
  554. case Z_AXIS: WRITE(Z_TMC2130_CS, HIGH); break;
  555. case E_AXIS: WRITE(E0_TMC2130_CS, HIGH); break;
  556. }
  557. }
  558. //spi
  559. #define TMC2130_SPI_ENTER() spi_setup(TMC2130_SPCR, TMC2130_SPSR)
  560. #define TMC2130_SPI_TXRX spi_txrx
  561. #define TMC2130_SPI_LEAVE()
  562. static void tmc2130_tx(uint8_t axis, uint8_t addr, uint32_t wval)
  563. {
  564. //datagram1 - request
  565. TMC2130_SPI_ENTER();
  566. tmc2130_cs_low(axis);
  567. TMC2130_SPI_TXRX(addr); // address
  568. TMC2130_SPI_TXRX((wval >> 24) & 0xff); // MSB
  569. TMC2130_SPI_TXRX((wval >> 16) & 0xff);
  570. TMC2130_SPI_TXRX((wval >> 8) & 0xff);
  571. TMC2130_SPI_TXRX(wval & 0xff); // LSB
  572. tmc2130_cs_high(axis);
  573. TMC2130_SPI_LEAVE();
  574. }
  575. static uint8_t tmc2130_rx(uint8_t axis, uint8_t addr, uint32_t* rval)
  576. {
  577. //datagram1 - request
  578. TMC2130_SPI_ENTER();
  579. tmc2130_cs_low(axis);
  580. TMC2130_SPI_TXRX(addr); // address
  581. TMC2130_SPI_TXRX(0); // MSB
  582. TMC2130_SPI_TXRX(0);
  583. TMC2130_SPI_TXRX(0);
  584. TMC2130_SPI_TXRX(0); // LSB
  585. tmc2130_cs_high(axis);
  586. TMC2130_SPI_LEAVE();
  587. //datagram2 - response
  588. TMC2130_SPI_ENTER();
  589. tmc2130_cs_low(axis);
  590. uint8_t stat = TMC2130_SPI_TXRX(0); // status
  591. uint32_t val32 = 0;
  592. val32 = TMC2130_SPI_TXRX(0); // MSB
  593. val32 = (val32 << 8) | TMC2130_SPI_TXRX(0);
  594. val32 = (val32 << 8) | TMC2130_SPI_TXRX(0);
  595. val32 = (val32 << 8) | TMC2130_SPI_TXRX(0); // LSB
  596. tmc2130_cs_high(axis);
  597. TMC2130_SPI_LEAVE();
  598. if (rval != 0) *rval = val32;
  599. return stat;
  600. }
  601. #define _GET_PWR_X (READ(X_ENABLE_PIN) == X_ENABLE_ON)
  602. #define _GET_PWR_Y (READ(Y_ENABLE_PIN) == Y_ENABLE_ON)
  603. #define _GET_PWR_Z (READ(Z_ENABLE_PIN) == Z_ENABLE_ON)
  604. #define _GET_PWR_E (READ(E0_ENABLE_PIN) == E_ENABLE_ON)
  605. #define _SET_PWR_X(ena) { WRITE(X_ENABLE_PIN, ena?X_ENABLE_ON:!X_ENABLE_ON); asm("nop"); }
  606. #define _SET_PWR_Y(ena) { WRITE(Y_ENABLE_PIN, ena?Y_ENABLE_ON:!Y_ENABLE_ON); asm("nop"); }
  607. #define _SET_PWR_Z(ena) { WRITE(Z_ENABLE_PIN, ena?Z_ENABLE_ON:!Z_ENABLE_ON); asm("nop"); }
  608. #define _SET_PWR_E(ena) { WRITE(E0_ENABLE_PIN, ena?E_ENABLE_ON:!E_ENABLE_ON); asm("nop"); }
  609. #define _GET_DIR_X (READ(X_DIR_PIN) == INVERT_X_DIR)
  610. #define _GET_DIR_Y (READ(Y_DIR_PIN) == INVERT_Y_DIR)
  611. #define _GET_DIR_Z (READ(Z_DIR_PIN) == INVERT_Z_DIR)
  612. #define _GET_DIR_E (READ(E0_DIR_PIN) == INVERT_E0_DIR)
  613. #define _SET_DIR_X(dir) { WRITE(X_DIR_PIN, dir?INVERT_X_DIR:!INVERT_X_DIR); asm("nop"); }
  614. #define _SET_DIR_Y(dir) { WRITE(Y_DIR_PIN, dir?INVERT_Y_DIR:!INVERT_Y_DIR); asm("nop"); }
  615. #define _SET_DIR_Z(dir) { WRITE(Z_DIR_PIN, dir?INVERT_Z_DIR:!INVERT_Z_DIR); asm("nop"); }
  616. #define _SET_DIR_E(dir) { WRITE(E0_DIR_PIN, dir?INVERT_E0_DIR:!INVERT_E0_DIR); asm("nop"); }
  617. #define _DO_STEP_X { WRITE(X_STEP_PIN, !INVERT_X_STEP_PIN); asm("nop"); WRITE(X_STEP_PIN, INVERT_X_STEP_PIN); asm("nop"); }
  618. #define _DO_STEP_Y { WRITE(Y_STEP_PIN, !INVERT_Y_STEP_PIN); asm("nop"); WRITE(Y_STEP_PIN, INVERT_Y_STEP_PIN); asm("nop"); }
  619. #define _DO_STEP_Z { WRITE(Z_STEP_PIN, !INVERT_Z_STEP_PIN); asm("nop"); WRITE(Z_STEP_PIN, INVERT_Z_STEP_PIN); asm("nop"); }
  620. #define _DO_STEP_E { WRITE(E0_STEP_PIN, !INVERT_E_STEP_PIN); asm("nop"); WRITE(E0_STEP_PIN, INVERT_E_STEP_PIN); asm("nop"); }
  621. uint16_t tmc2130_get_res(uint8_t axis)
  622. {
  623. return tmc2130_mres2usteps(tmc2130_mres[axis]);
  624. }
  625. void tmc2130_set_res(uint8_t axis, uint16_t res)
  626. {
  627. tmc2130_mres[axis] = tmc2130_usteps2mres(res);
  628. // uint32_t u = _micros();
  629. tmc2130_setup_chopper(axis, tmc2130_mres[axis], tmc2130_current_h[axis], tmc2130_current_r[axis]);
  630. // u = _micros() - u;
  631. // printf_P(PSTR("tmc2130_setup_chopper %c %lu us"), "XYZE"[axis], u);
  632. }
  633. uint8_t tmc2130_get_pwr(uint8_t axis)
  634. {
  635. switch (axis)
  636. {
  637. case X_AXIS: return _GET_PWR_X;
  638. case Y_AXIS: return _GET_PWR_Y;
  639. case Z_AXIS: return _GET_PWR_Z;
  640. case E_AXIS: return _GET_PWR_E;
  641. }
  642. return 0;
  643. }
  644. //! @par pwr motor power
  645. //! * 0 disabled
  646. //! * non-zero enabled
  647. void tmc2130_set_pwr(uint8_t axis, uint8_t pwr)
  648. {
  649. switch (axis)
  650. {
  651. case X_AXIS: _SET_PWR_X(pwr); break;
  652. case Y_AXIS: _SET_PWR_Y(pwr); break;
  653. case Z_AXIS: _SET_PWR_Z(pwr); break;
  654. case E_AXIS: _SET_PWR_E(pwr); break;
  655. }
  656. }
  657. uint8_t tmc2130_get_inv(uint8_t axis)
  658. {
  659. switch (axis)
  660. {
  661. case X_AXIS: return INVERT_X_DIR;
  662. case Y_AXIS: return INVERT_Y_DIR;
  663. case Z_AXIS: return INVERT_Z_DIR;
  664. case E_AXIS: return INVERT_E0_DIR;
  665. }
  666. return 0;
  667. }
  668. uint8_t tmc2130_get_dir(uint8_t axis)
  669. {
  670. switch (axis)
  671. {
  672. case X_AXIS: return _GET_DIR_X;
  673. case Y_AXIS: return _GET_DIR_Y;
  674. case Z_AXIS: return _GET_DIR_Z;
  675. case E_AXIS: return _GET_DIR_E;
  676. }
  677. return 0;
  678. }
  679. void tmc2130_set_dir(uint8_t axis, uint8_t dir)
  680. {
  681. switch (axis)
  682. {
  683. case X_AXIS: _SET_DIR_X(dir); break;
  684. case Y_AXIS: _SET_DIR_Y(dir); break;
  685. case Z_AXIS: _SET_DIR_Z(dir); break;
  686. case E_AXIS: _SET_DIR_E(dir); break;
  687. }
  688. }
  689. void tmc2130_do_step(uint8_t axis)
  690. {
  691. switch (axis)
  692. {
  693. case X_AXIS: _DO_STEP_X; break;
  694. case Y_AXIS: _DO_STEP_Y; break;
  695. case Z_AXIS: _DO_STEP_Z; break;
  696. case E_AXIS: _DO_STEP_E; break;
  697. }
  698. }
  699. void tmc2130_do_steps(uint8_t axis, uint16_t steps, uint8_t dir, uint16_t delay_us)
  700. {
  701. tmc2130_set_dir(axis, dir);
  702. delayMicroseconds(100);
  703. while (steps--)
  704. {
  705. tmc2130_do_step(axis);
  706. delayMicroseconds(delay_us);
  707. }
  708. }
  709. void tmc2130_goto_step(uint8_t axis, uint8_t step, uint8_t dir, uint16_t delay_us, uint16_t microstep_resolution)
  710. {
  711. printf_P(PSTR("tmc2130_goto_step %d %d %d %d \n"), axis, step, dir, delay_us, microstep_resolution);
  712. uint8_t shift; for (shift = 0; shift < 8; shift++) if (microstep_resolution == (256u >> shift)) break;
  713. uint16_t cnt = 4 * (1 << (8 - shift));
  714. uint16_t mscnt = tmc2130_rd_MSCNT(axis);
  715. if (dir == 2)
  716. {
  717. dir = tmc2130_get_inv(axis)?0:1;
  718. int steps = (int)step - (int)(mscnt >> shift);
  719. if (steps > static_cast<int>(cnt / 2))
  720. {
  721. dir ^= 1;
  722. steps = cnt - steps; // This can create a negative step value
  723. }
  724. if (steps < 0)
  725. {
  726. dir ^= 1;
  727. steps = -steps;
  728. }
  729. cnt = steps;
  730. }
  731. tmc2130_set_dir(axis, dir);
  732. delayMicroseconds(100);
  733. mscnt = tmc2130_rd_MSCNT(axis);
  734. while ((cnt--) && ((mscnt >> shift) != step))
  735. {
  736. tmc2130_do_step(axis);
  737. delayMicroseconds(delay_us);
  738. mscnt = tmc2130_rd_MSCNT(axis);
  739. }
  740. }
  741. void tmc2130_get_wave(uint8_t axis, uint8_t* data, FILE* stream)
  742. {
  743. uint8_t pwr = tmc2130_get_pwr(axis);
  744. tmc2130_set_pwr(axis, 0);
  745. tmc2130_setup_chopper(axis, tmc2130_usteps2mres(256), tmc2130_current_h[axis], tmc2130_current_r[axis]);
  746. tmc2130_goto_step(axis, 0, 2, 100, 256);
  747. tmc2130_set_dir(axis, tmc2130_get_inv(axis)?0:1);
  748. for (unsigned int i = 0; i <= 255; i++)
  749. {
  750. uint32_t val = tmc2130_rd_MSCURACT(axis);
  751. uint16_t mscnt = tmc2130_rd_MSCNT(axis);
  752. int curA = (val & 0xff) | ((val << 7) & 0x8000);
  753. if (stream)
  754. {
  755. if (mscnt == i)
  756. fprintf_P(stream, PSTR("%d\t%d\n"), i, curA);
  757. else //TODO - remove this check
  758. fprintf_P(stream, PSTR("!! (i=%d MSCNT=%d)\n"), i, mscnt);
  759. }
  760. if (data) *(data++) = curA;
  761. tmc2130_do_step(axis);
  762. delayMicroseconds(100);
  763. }
  764. tmc2130_setup_chopper(axis, tmc2130_mres[axis], tmc2130_current_h[axis], tmc2130_current_r[axis]);
  765. tmc2130_set_pwr(axis, pwr);
  766. }
  767. void tmc2130_set_wave(uint8_t axis, uint8_t amp, uint8_t fac1000)
  768. {
  769. // TMC2130 wave compression algorithm
  770. // optimized for minimal memory requirements
  771. // printf_P(PSTR("tmc2130_set_wave %hhd %hhd\n"), axis, fac1000);
  772. if (fac1000 < TMC2130_WAVE_FAC1000_MIN) fac1000 = 0;
  773. if (fac1000 > TMC2130_WAVE_FAC1000_MAX) fac1000 = TMC2130_WAVE_FAC1000_MAX;
  774. float fac = 0;
  775. if (fac1000) fac = ((float)((uint16_t)fac1000 + 1000) / 1000); //correction factor
  776. // printf_P(PSTR(" factor: %s\n"), ftostr43(fac));
  777. uint8_t vA = 0; //value of currentA
  778. uint8_t va = 0; //previous vA
  779. int8_t d0 = 0; //delta0
  780. int8_t d1 = 1; //delta1
  781. uint8_t w[4] = {1,1,1,1}; //W bits (MSLUTSEL)
  782. uint8_t x[3] = {255,255,255}; //X segment bounds (MSLUTSEL)
  783. uint8_t s = 0; //current segment
  784. int8_t b; //encoded bit value
  785. int8_t dA; //delta value
  786. int i; //microstep index
  787. uint32_t reg = 0; //tmc2130 register
  788. tmc2130_wr_MSLUTSTART(axis, 0, amp);
  789. for (i = 0; i < 256; i++)
  790. {
  791. if ((i & 0x1f) == 0)
  792. reg = 0;
  793. // calculate value
  794. if (fac == 0) // default TMC wave
  795. vA = (uint8_t)((amp+1) * sin((2*PI*i + PI)/1024) + 0.5) - 1;
  796. else // corrected wave
  797. vA = (uint8_t)(amp * pow(sin(2*PI*i/1024), fac) + 0.5);
  798. dA = vA - va; // calculate delta
  799. va = vA;
  800. b = -1;
  801. if (dA == d0) b = 0; //delta == delta0 => bit=0
  802. else if (dA == d1) b = 1; //delta == delta1 => bit=1
  803. else
  804. {
  805. if (dA < d0) // delta < delta0 => switch wbit down
  806. {
  807. //printf("dn\n");
  808. b = 0;
  809. switch (dA)
  810. {
  811. case -1: d0 = -1; d1 = 0; w[s+1] = 0; break;
  812. case 0: d0 = 0; d1 = 1; w[s+1] = 1; break;
  813. case 1: d0 = 1; d1 = 2; w[s+1] = 2; break;
  814. default: b = -1; break;
  815. }
  816. if (b >= 0) { x[s] = i; s++; }
  817. }
  818. else if (dA > d1) // delta > delta0 => switch wbit up
  819. {
  820. //printf("up\n");
  821. b = 1;
  822. switch (dA)
  823. {
  824. case 1: d0 = 0; d1 = 1; w[s+1] = 1; break;
  825. case 2: d0 = 1; d1 = 2; w[s+1] = 2; break;
  826. case 3: d0 = 2; d1 = 3; w[s+1] = 3; break;
  827. default: b = -1; break;
  828. }
  829. if (b >= 0) { x[s] = i; s++; }
  830. }
  831. }
  832. if (b < 0) break; // delta out of range (<-1 or >3)
  833. if (s > 3) break; // segment out of range (> 3)
  834. //printf("%d\n", vA);
  835. if (b == 1) reg |= 0x80000000;
  836. if ((i & 31) == 31)
  837. tmc2130_wr_MSLUT(axis, (uint8_t)(i >> 5), reg);
  838. else
  839. reg >>= 1;
  840. // printf("%3d\t%3d\t%2d\t%2d\t%2d\t%2d %08x\n", i, vA, dA, b, w[s], s, reg);
  841. }
  842. tmc2130_wr_MSLUTSEL(axis, x[0], x[1], x[2], w[0], w[1], w[2], w[3]);
  843. }
  844. void bubblesort_uint8(uint8_t* data, uint8_t size, uint8_t* data2)
  845. {
  846. uint8_t changed = 1;
  847. while (changed)
  848. {
  849. changed = 0;
  850. for (uint8_t i = 0; i < (size - 1); i++)
  851. if (data[i] > data[i+1])
  852. {
  853. uint8_t register d = data[i];
  854. data[i] = data[i+1];
  855. data[i+1] = d;
  856. if (data2)
  857. {
  858. d = data2[i];
  859. data2[i] = data2[i+1];
  860. data2[i+1] = d;
  861. }
  862. changed = 1;
  863. }
  864. }
  865. }
  866. uint8_t clusterize_uint8(uint8_t* data, uint8_t size, uint8_t* ccnt, uint8_t* cval, uint8_t tol)
  867. {
  868. uint8_t cnt = 1;
  869. uint16_t sum = data[0];
  870. uint8_t cl = 0;
  871. for (uint8_t i = 1; i < size; i++)
  872. {
  873. uint8_t d = data[i];
  874. uint8_t val = sum / cnt;
  875. uint8_t dif = 0;
  876. if (val > d) dif = val - d;
  877. else dif = d - val;
  878. if (dif <= tol)
  879. {
  880. cnt += 1;
  881. sum += d;
  882. }
  883. else
  884. {
  885. if (ccnt) ccnt[cl] = cnt;
  886. if (cval) cval[cl] = val;
  887. cnt = 1;
  888. sum = d;
  889. cl += 1;
  890. }
  891. }
  892. if (ccnt) ccnt[cl] = cnt;
  893. if (cval) cval[cl] = sum / cnt;
  894. return ++cl;
  895. }
  896. bool tmc2130_home_calibrate(uint8_t axis)
  897. {
  898. uint8_t step[16];
  899. uint8_t cnt[16];
  900. uint8_t val[16];
  901. homeaxis(axis, true, 16, step);
  902. bubblesort_uint8(step, 16, 0);
  903. printf_P(PSTR("sorted samples:\n"));
  904. for (uint8_t i = 0; i < 16; i++)
  905. printf_P(PSTR(" i=%2d step=%2d\n"), i, step[i]);
  906. uint8_t cl = clusterize_uint8(step, 16, cnt, val, 1);
  907. printf_P(PSTR("clusters:\n"));
  908. for (uint8_t i = 0; i < cl; i++)
  909. printf_P(PSTR(" i=%2d cnt=%2d val=%2d\n"), i, cnt[i], val[i]);
  910. bubblesort_uint8(cnt, cl, val);
  911. tmc2130_home_origin[axis] = val[cl-1];
  912. printf_P(PSTR("result value: %d\n"), tmc2130_home_origin[axis]);
  913. if (axis == X_AXIS) eeprom_update_byte((uint8_t*)EEPROM_TMC2130_HOME_X_ORIGIN, tmc2130_home_origin[X_AXIS]);
  914. else if (axis == Y_AXIS) eeprom_update_byte((uint8_t*)EEPROM_TMC2130_HOME_Y_ORIGIN, tmc2130_home_origin[Y_AXIS]);
  915. return true;
  916. }
  917. //! @brief Translate current to tmc2130 vsense and IHOLD or IRUN
  918. //! @param cur current in mA
  919. //! @return 0 .. 63
  920. //! @n most significant bit is CHOPCONF vsense bit (sense resistor voltage based current scaling)
  921. //! @n rest is to be used in IRUN or IHOLD register
  922. //!
  923. //! | mA | trinamic register | note |
  924. //! | --- | --- | --- |
  925. //! | 0 | 0 | doesn't mean current off, lowest current is 1/32 current with vsense low range |
  926. //! | 30 | 1 | |
  927. //! | 40 | 2 | |
  928. //! | 60 | 3 | |
  929. //! | 90 | 4 | |
  930. //! | 100 | 5 | |
  931. //! | 120 | 6 | |
  932. //! | 130 | 7 | |
  933. //! | 150 | 8 | |
  934. //! | 180 | 9 | |
  935. //! | 190 | 10 | |
  936. //! | 210 | 11 | |
  937. //! | 230 | 12 | |
  938. //! | 240 | 13 | |
  939. //! | 250 | 13 | |
  940. //! | 260 | 14 | |
  941. //! | 280 | 15 | |
  942. //! | 300 | 16 | |
  943. //! | 320 | 17 | |
  944. //! | 340 | 18 | |
  945. //! | 350 | 19 | |
  946. //! | 370 | 20 | |
  947. //! | 390 | 21 | |
  948. //! | 410 | 22 | |
  949. //! | 430 | 23 | |
  950. //! | 450 | 24 | |
  951. //! | 460 | 25 | |
  952. //! | 480 | 26 | |
  953. //! | 500 | 27 | |
  954. //! | 520 | 28 | |
  955. //! | 535 | 29 | |
  956. //! | N/D | 30 | extruder default |
  957. //! | 540 | 33 | |
  958. //! | 560 | 34 | |
  959. //! | 580 | 35 | |
  960. //! | 590 | 36 | farm mode extruder default |
  961. //! | 610 | 37 | |
  962. //! | 630 | 38 | |
  963. //! | 640 | 39 | |
  964. //! | 660 | 40 | |
  965. //! | 670 | 41 | |
  966. //! | 690 | 42 | |
  967. //! | 710 | 43 | |
  968. //! | 720 | 44 | |
  969. //! | 730 | 45 | |
  970. //! | 760 | 46 | |
  971. //! | 770 | 47 | |
  972. //! | 790 | 48 | |
  973. //! | 810 | 49 | |
  974. //! | 820 | 50 | |
  975. //! | 840 | 51 | |
  976. //! | 850 | 52 | |
  977. //! | 870 | 53 | |
  978. //! | 890 | 54 | |
  979. //! | 900 | 55 | |
  980. //! | 920 | 56 | |
  981. //! | 940 | 57 | |
  982. //! | 950 | 58 | |
  983. //! | 970 | 59 | |
  984. //! | 980 | 60 | |
  985. //! | 1000 | 61 | |
  986. //! | 1020 | 62 | |
  987. //! | 1029 | 63 | |
  988. uint8_t tmc2130_cur2val(float cur)
  989. {
  990. if (cur < 0) cur = 0; //limit min
  991. if (cur > 1029) cur = 1029; //limit max
  992. //540mA is threshold for switch from high sense to low sense
  993. //for higher currents is maximum current 1029mA
  994. if (cur >= 540) return 63 * (float)cur / 1029;
  995. //for lower currents must be the value divided by 1.125 (= 0.18*2/0.32)
  996. return 63 * (float)cur / (1029 * 1.125);
  997. }
  998. float tmc2130_val2cur(uint8_t val)
  999. {
  1000. float rsense = 0.2; //0.2 ohm sense resistors
  1001. uint8_t vsense = (val & 0x20)?0:1; //vsense bit = val>31
  1002. float vfs = vsense?0.18:0.32; //vfs depends on vsense bit
  1003. uint8_t val2 = vsense?val:(val >> 1); //vals 32..63 shifted right (16..31)
  1004. // equation from datasheet (0.7071 ~= 1/sqrt(2))
  1005. float cur = ((float)(val2 + 1)/32) * (vfs/(rsense + 0.02)) * 0.7071;
  1006. return cur * 1000; //return current in mA
  1007. }
  1008. #endif //TMC2130