tmc2130.cpp 36 KB

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