tmc2130.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  1. #include "Marlin.h"
  2. #ifdef TMC2130
  3. #include "tmc2130.h"
  4. #include "LiquidCrystal_Prusa.h"
  5. #include "ultralcd.h"
  6. #include "language.h"
  7. #include "spi.h"
  8. extern LiquidCrystal_Prusa lcd;
  9. #define TMC2130_GCONF_NORMAL 0x00000000 // spreadCycle
  10. #define TMC2130_GCONF_SGSENS 0x00003180 // spreadCycle with stallguard (stall activates DIAG0 and DIAG1 [pushpull])
  11. #define TMC2130_GCONF_SILENT 0x00000004 // stealthChop
  12. //externals for debuging
  13. extern float current_position[4];
  14. extern void st_get_position_xy(long &x, long &y);
  15. extern long st_get_position(uint8_t axis);
  16. extern void crashdet_stop_and_save_print();
  17. extern void crashdet_stop_and_save_print2();
  18. //mode
  19. uint8_t tmc2130_mode = TMC2130_MODE_NORMAL;
  20. //holding currents
  21. uint8_t tmc2130_current_h[4] = TMC2130_CURRENTS_H;
  22. //running currents
  23. uint8_t tmc2130_current_r[4] = TMC2130_CURRENTS_R;
  24. //running currents for homing
  25. uint8_t tmc2130_current_r_home[4] = {8, 10, 20, 18};
  26. //pwm_ampl
  27. uint8_t tmc2130_pwm_ampl[4] = {TMC2130_PWM_AMPL_X, TMC2130_PWM_AMPL_Y, TMC2130_PWM_AMPL_Z, TMC2130_PWM_AMPL_E};
  28. //pwm_grad
  29. uint8_t tmc2130_pwm_grad[4] = {TMC2130_PWM_GRAD_X, TMC2130_PWM_GRAD_Y, TMC2130_PWM_GRAD_Z, TMC2130_PWM_GRAD_E};
  30. //pwm_auto
  31. uint8_t tmc2130_pwm_auto[4] = {TMC2130_PWM_AUTO_X, TMC2130_PWM_AUTO_Y, TMC2130_PWM_AUTO_Z, TMC2130_PWM_AUTO_E};
  32. //pwm_freq
  33. uint8_t tmc2130_pwm_freq[4] = {TMC2130_PWM_FREQ_X, TMC2130_PWM_FREQ_Y, TMC2130_PWM_FREQ_Z, TMC2130_PWM_FREQ_E};
  34. uint8_t tmc2130_mres[4] = {0, 0, 0, 0}; //will be filed at begin of init
  35. uint8_t tmc2130_sg_thr[4] = {TMC2130_SG_THRS_X, TMC2130_SG_THRS_Y, TMC2130_SG_THRS_Z, TMC2130_SG_THRS_E};
  36. uint8_t tmc2130_sg_thr_home[4] = {3, 3, TMC2130_SG_THRS_Z, TMC2130_SG_THRS_E};
  37. uint8_t tmc2130_sg_homing_axes_mask = 0x00;
  38. uint8_t tmc2130_sg_meassure = 0xff;
  39. uint32_t tmc2130_sg_meassure_cnt = 0;
  40. uint32_t tmc2130_sg_meassure_val = 0;
  41. uint8_t tmc2130_home_enabled = 0;
  42. uint8_t tmc2130_home_origin[2] = {0, 0};
  43. uint8_t tmc2130_home_bsteps[2] = {48, 48};
  44. uint8_t tmc2130_home_fsteps[2] = {48, 48};
  45. uint8_t tmc2130_wave_fac[4] = {0, 0, 0, 0};
  46. tmc2130_chopper_config_t tmc2130_chopper_config[4] = {
  47. {TMC2130_TOFF_XYZ, 5, 1, 2, 0},
  48. {TMC2130_TOFF_XYZ, 5, 1, 2, 0},
  49. {TMC2130_TOFF_XYZ, 5, 1, 2, 0},
  50. {TMC2130_TOFF_E, 5, 1, 2, 0}
  51. };
  52. bool tmc2130_sg_stop_on_crash = true;
  53. uint8_t tmc2130_sg_diag_mask = 0x00;
  54. uint8_t tmc2130_sg_crash = 0;
  55. uint16_t tmc2130_sg_err[4] = {0, 0, 0, 0};
  56. uint16_t tmc2130_sg_cnt[4] = {0, 0, 0, 0};
  57. bool tmc2130_sg_change = false;
  58. bool skip_debug_msg = false;
  59. #define DBG(args...) printf_P(args)
  60. #ifndef _n
  61. #define _n PSTR
  62. #endif //_n
  63. #ifndef _i
  64. #define _i PSTR
  65. #endif //_i
  66. //TMC2130 registers
  67. #define TMC2130_REG_GCONF 0x00 // 17 bits
  68. #define TMC2130_REG_GSTAT 0x01 // 3 bits
  69. #define TMC2130_REG_IOIN 0x04 // 8+8 bits
  70. #define TMC2130_REG_IHOLD_IRUN 0x10 // 5+5+4 bits
  71. #define TMC2130_REG_TPOWERDOWN 0x11 // 8 bits
  72. #define TMC2130_REG_TSTEP 0x12 // 20 bits
  73. #define TMC2130_REG_TPWMTHRS 0x13 // 20 bits
  74. #define TMC2130_REG_TCOOLTHRS 0x14 // 20 bits
  75. #define TMC2130_REG_THIGH 0x15 // 20 bits
  76. #define TMC2130_REG_XDIRECT 0x2d // 32 bits
  77. #define TMC2130_REG_VDCMIN 0x33 // 23 bits
  78. #define TMC2130_REG_MSLUT0 0x60 // 32 bits
  79. #define TMC2130_REG_MSLUT1 0x61 // 32 bits
  80. #define TMC2130_REG_MSLUT2 0x62 // 32 bits
  81. #define TMC2130_REG_MSLUT3 0x63 // 32 bits
  82. #define TMC2130_REG_MSLUT4 0x64 // 32 bits
  83. #define TMC2130_REG_MSLUT5 0x65 // 32 bits
  84. #define TMC2130_REG_MSLUT6 0x66 // 32 bits
  85. #define TMC2130_REG_MSLUT7 0x67 // 32 bits
  86. #define TMC2130_REG_MSLUTSEL 0x68 // 32 bits
  87. #define TMC2130_REG_MSLUTSTART 0x69 // 8+8 bits
  88. #define TMC2130_REG_MSCNT 0x6a // 10 bits
  89. #define TMC2130_REG_MSCURACT 0x6b // 9+9 bits
  90. #define TMC2130_REG_CHOPCONF 0x6c // 32 bits
  91. #define TMC2130_REG_COOLCONF 0x6d // 25 bits
  92. #define TMC2130_REG_DCCTRL 0x6e // 24 bits
  93. #define TMC2130_REG_DRV_STATUS 0x6f // 32 bits
  94. #define TMC2130_REG_PWMCONF 0x70 // 22 bits
  95. #define TMC2130_REG_PWM_SCALE 0x71 // 8 bits
  96. #define TMC2130_REG_ENCM_CTRL 0x72 // 2 bits
  97. #define TMC2130_REG_LOST_STEPS 0x73 // 20 bits
  98. uint16_t tmc2130_rd_TSTEP(uint8_t axis);
  99. uint16_t tmc2130_rd_MSCNT(uint8_t axis);
  100. uint32_t tmc2130_rd_MSCURACT(uint8_t axis);
  101. 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);
  102. 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);
  103. void tmc2130_wr_TPWMTHRS(uint8_t axis, uint32_t val32);
  104. void tmc2130_wr_THIGH(uint8_t axis, uint32_t val32);
  105. #define tmc2130_rd(axis, addr, rval) tmc2130_rx(axis, addr, rval)
  106. #define tmc2130_wr(axis, addr, wval) tmc2130_tx(axis, addr | 0x80, wval)
  107. uint8_t tmc2130_tx(uint8_t axis, uint8_t addr, uint32_t wval);
  108. uint8_t tmc2130_rx(uint8_t axis, uint8_t addr, uint32_t* rval);
  109. void tmc2130_setup_chopper(uint8_t axis, uint8_t mres, uint8_t current_h, uint8_t current_r);
  110. uint16_t __tcoolthrs(uint8_t axis)
  111. {
  112. switch (axis)
  113. {
  114. case X_AXIS: return TMC2130_TCOOLTHRS_X;
  115. case Y_AXIS: return TMC2130_TCOOLTHRS_Y;
  116. case Z_AXIS: return TMC2130_TCOOLTHRS_Z;
  117. }
  118. return 0;
  119. }
  120. void tmc2130_init()
  121. {
  122. DBG(_n("tmc2130_init(), mode=%S\n"), tmc2130_mode?_n("STEALTH"):_n("NORMAL"));
  123. WRITE(X_TMC2130_CS, HIGH);
  124. WRITE(Y_TMC2130_CS, HIGH);
  125. WRITE(Z_TMC2130_CS, HIGH);
  126. WRITE(E0_TMC2130_CS, HIGH);
  127. SET_OUTPUT(X_TMC2130_CS);
  128. SET_OUTPUT(Y_TMC2130_CS);
  129. SET_OUTPUT(Z_TMC2130_CS);
  130. SET_OUTPUT(E0_TMC2130_CS);
  131. SET_INPUT(X_TMC2130_DIAG);
  132. SET_INPUT(Y_TMC2130_DIAG);
  133. SET_INPUT(Z_TMC2130_DIAG);
  134. SET_INPUT(E0_TMC2130_DIAG);
  135. for (int axis = 0; axis < 2; axis++) // X Y axes
  136. {
  137. tmc2130_setup_chopper(axis, tmc2130_mres[axis], tmc2130_current_h[axis], tmc2130_current_r[axis]);
  138. tmc2130_wr(axis, TMC2130_REG_TPOWERDOWN, 0x00000000);
  139. tmc2130_wr(axis, TMC2130_REG_COOLCONF, (((uint32_t)tmc2130_sg_thr[axis]) << 16));
  140. tmc2130_wr(axis, TMC2130_REG_TCOOLTHRS, (tmc2130_mode == TMC2130_MODE_SILENT)?0:__tcoolthrs(axis));
  141. tmc2130_wr(axis, TMC2130_REG_GCONF, (tmc2130_mode == TMC2130_MODE_SILENT)?TMC2130_GCONF_SILENT:TMC2130_GCONF_SGSENS);
  142. tmc2130_wr_PWMCONF(axis, tmc2130_pwm_ampl[axis], tmc2130_pwm_grad[axis], tmc2130_pwm_freq[axis], tmc2130_pwm_auto[axis], 0, 0);
  143. tmc2130_wr_TPWMTHRS(axis, TMC2130_TPWMTHRS);
  144. //tmc2130_wr_THIGH(axis, TMC2130_THIGH);
  145. }
  146. for (int axis = 2; axis < 3; axis++) // Z axis
  147. {
  148. tmc2130_setup_chopper(axis, tmc2130_mres[axis], tmc2130_current_h[axis], tmc2130_current_r[axis]);
  149. tmc2130_wr(axis, TMC2130_REG_TPOWERDOWN, 0x00000000);
  150. #ifndef TMC2130_STEALTH_Z
  151. tmc2130_wr(axis, TMC2130_REG_GCONF, TMC2130_GCONF_SGSENS);
  152. #else //TMC2130_STEALTH_Z
  153. tmc2130_wr(axis, TMC2130_REG_COOLCONF, (((uint32_t)tmc2130_sg_thr[axis]) << 16));
  154. tmc2130_wr(axis, TMC2130_REG_TCOOLTHRS, (tmc2130_mode == TMC2130_MODE_SILENT)?0:__tcoolthrs(axis));
  155. tmc2130_wr(axis, TMC2130_REG_GCONF, (tmc2130_mode == TMC2130_MODE_SILENT)?TMC2130_GCONF_SILENT:TMC2130_GCONF_SGSENS);
  156. tmc2130_wr_PWMCONF(axis, tmc2130_pwm_ampl[axis], tmc2130_pwm_grad[axis], tmc2130_pwm_freq[axis], tmc2130_pwm_auto[axis], 0, 0);
  157. tmc2130_wr_TPWMTHRS(axis, TMC2130_TPWMTHRS);
  158. #endif //TMC2130_STEALTH_Z
  159. }
  160. for (int axis = 3; axis < 4; axis++) // E axis
  161. {
  162. tmc2130_setup_chopper(axis, tmc2130_mres[axis], tmc2130_current_h[axis], tmc2130_current_r[axis]);
  163. tmc2130_wr(axis, TMC2130_REG_TPOWERDOWN, 0x00000000);
  164. #ifndef TMC2130_STEALTH_E
  165. tmc2130_wr(axis, TMC2130_REG_GCONF, TMC2130_GCONF_SGSENS);
  166. #else //TMC2130_STEALTH_E
  167. tmc2130_wr(axis, TMC2130_REG_COOLCONF, (((uint32_t)tmc2130_sg_thr[axis]) << 16));
  168. tmc2130_wr(axis, TMC2130_REG_TCOOLTHRS, 0);
  169. tmc2130_wr(axis, TMC2130_REG_GCONF, TMC2130_GCONF_SILENT);
  170. tmc2130_wr_PWMCONF(axis, tmc2130_pwm_ampl[axis], tmc2130_pwm_grad[axis], tmc2130_pwm_freq[axis], tmc2130_pwm_auto[axis], 0, 0);
  171. tmc2130_wr_TPWMTHRS(axis, TMC2130_TPWMTHRS);
  172. #endif //TMC2130_STEALTH_E
  173. }
  174. tmc2130_sg_err[0] = 0;
  175. tmc2130_sg_err[1] = 0;
  176. tmc2130_sg_err[2] = 0;
  177. tmc2130_sg_err[3] = 0;
  178. tmc2130_sg_cnt[0] = 0;
  179. tmc2130_sg_cnt[1] = 0;
  180. tmc2130_sg_cnt[2] = 0;
  181. tmc2130_sg_cnt[3] = 0;
  182. #ifdef TMC2130_LINEARITY_CORRECTION
  183. #ifdef TMC2130_LINEARITY_CORRECTION_XYZ
  184. tmc2130_set_wave(X_AXIS, 247, tmc2130_wave_fac[X_AXIS]);
  185. tmc2130_set_wave(Y_AXIS, 247, tmc2130_wave_fac[Y_AXIS]);
  186. tmc2130_set_wave(Z_AXIS, 247, tmc2130_wave_fac[Z_AXIS]);
  187. #endif //TMC2130_LINEARITY_CORRECTION_XYZ
  188. tmc2130_set_wave(E_AXIS, 247, tmc2130_wave_fac[E_AXIS]);
  189. #endif //TMC2130_LINEARITY_CORRECTION
  190. }
  191. uint8_t tmc2130_sample_diag()
  192. {
  193. uint8_t mask = 0;
  194. if (READ(X_TMC2130_DIAG)) mask |= X_AXIS_MASK;
  195. if (READ(Y_TMC2130_DIAG)) mask |= Y_AXIS_MASK;
  196. // if (READ(Z_TMC2130_DIAG)) mask |= Z_AXIS_MASK;
  197. // if (READ(E0_TMC2130_DIAG)) mask |= E_AXIS_MASK;
  198. return mask;
  199. }
  200. extern bool is_usb_printing;
  201. void tmc2130_st_isr(uint8_t last_step_mask)
  202. {
  203. if (tmc2130_mode == TMC2130_MODE_SILENT || tmc2130_sg_stop_on_crash == false) return;
  204. uint8_t crash = 0;
  205. uint8_t diag_mask = tmc2130_sample_diag();
  206. // for (uint8_t axis = X_AXIS; axis <= E_AXIS; axis++)
  207. for (uint8_t axis = X_AXIS; axis <= Z_AXIS; axis++)
  208. {
  209. uint8_t mask = (X_AXIS_MASK << axis);
  210. if (diag_mask & mask) tmc2130_sg_err[axis]++;
  211. else
  212. if (tmc2130_sg_err[axis] > 0) tmc2130_sg_err[axis]--;
  213. if (tmc2130_sg_cnt[axis] < tmc2130_sg_err[axis])
  214. {
  215. tmc2130_sg_cnt[axis] = tmc2130_sg_err[axis];
  216. tmc2130_sg_change = true;
  217. uint8_t sg_thr = 64;
  218. // if (axis == Y_AXIS) sg_thr = 64;
  219. if (tmc2130_sg_err[axis] >= sg_thr)
  220. {
  221. tmc2130_sg_err[axis] = 0;
  222. crash |= mask;
  223. }
  224. }
  225. }
  226. if (tmc2130_sg_homing_axes_mask == 0)
  227. {
  228. if (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. uint32_t val32 = 0;
  241. tmc2130_rd(tmc2130_sg_meassure, TMC2130_REG_DRV_STATUS, &val32);
  242. tmc2130_sg_meassure_val += (val32 & 0x3ff);
  243. tmc2130_sg_meassure_cnt++;
  244. return true;
  245. }
  246. return false;
  247. }
  248. void tmc2130_home_enter(uint8_t axes_mask)
  249. {
  250. printf_P(PSTR("tmc2130_home_enter(axes_mask=0x%02x)\n"), axes_mask);
  251. #ifdef TMC2130_SG_HOMING
  252. if (axes_mask & 0x03) //X or Y
  253. tmc2130_wait_standstill_xy(1000);
  254. for (uint8_t axis = X_AXIS; axis <= Z_AXIS; axis++) //X Y and Z axes
  255. {
  256. uint8_t mask = (X_AXIS_MASK << axis);
  257. if (axes_mask & mask)
  258. {
  259. tmc2130_sg_homing_axes_mask |= mask;
  260. //Configuration to spreadCycle
  261. tmc2130_wr(axis, TMC2130_REG_GCONF, TMC2130_GCONF_NORMAL);
  262. tmc2130_wr(axis, TMC2130_REG_COOLCONF, (((uint32_t)tmc2130_sg_thr_home[axis]) << 16));
  263. // tmc2130_wr(axis, TMC2130_REG_COOLCONF, (((uint32_t)tmc2130_sg_thr[axis]) << 16) | ((uint32_t)1 << 24));
  264. tmc2130_wr(axis, TMC2130_REG_TCOOLTHRS, __tcoolthrs(axis));
  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(axis, 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 tmc2130_sg_homing_axes_mask=0x%02x\n"), tmc2130_sg_homing_axes_mask);
  275. #ifdef TMC2130_SG_HOMING
  276. if (tmc2130_sg_homing_axes_mask & 0x03) //X or Y
  277. tmc2130_wait_standstill_xy(1000);
  278. if (tmc2130_sg_homing_axes_mask)
  279. {
  280. for (uint8_t axis = X_AXIS; axis <= Z_AXIS; axis++) //X Y and Z axes
  281. {
  282. uint8_t mask = (X_AXIS_MASK << axis);
  283. if (tmc2130_sg_homing_axes_mask & mask & (X_AXIS_MASK | Y_AXIS_MASK | Z_AXIS_MASK))
  284. {
  285. #ifndef TMC2130_STEALTH_Z
  286. if ((tmc2130_mode == TMC2130_MODE_SILENT) && (axis != Z_AXIS))
  287. #else //TMC2130_STEALTH_Z
  288. if (tmc2130_mode == TMC2130_MODE_SILENT)
  289. #endif //TMC2130_STEALTH_Z
  290. {
  291. tmc2130_wr(axis, TMC2130_REG_GCONF, TMC2130_GCONF_SILENT); // Configuration back to stealthChop
  292. tmc2130_wr(axis, TMC2130_REG_TCOOLTHRS, 0);
  293. // tmc2130_wr_PWMCONF(i, tmc2130_pwm_ampl[i], tmc2130_pwm_grad[i], tmc2130_pwm_freq[i], tmc2130_pwm_auto[i], 0, 0);
  294. }
  295. else
  296. {
  297. // tmc2130_wr(axis, TMC2130_REG_GCONF, TMC2130_GCONF_NORMAL);
  298. tmc2130_setup_chopper(axis, tmc2130_mres[axis], tmc2130_current_h[axis], tmc2130_current_r[axis]);
  299. // tmc2130_wr(axis, TMC2130_REG_COOLCONF, (((uint32_t)tmc2130_sg_thr[axis]) << 16) | ((uint32_t)1 << 24));
  300. tmc2130_wr(axis, TMC2130_REG_COOLCONF, (((uint32_t)tmc2130_sg_thr[axis]) << 16));
  301. tmc2130_wr(axis, TMC2130_REG_TCOOLTHRS, __tcoolthrs(axis));
  302. tmc2130_wr(axis, TMC2130_REG_GCONF, TMC2130_GCONF_SGSENS);
  303. }
  304. }
  305. }
  306. tmc2130_sg_homing_axes_mask = 0x00;
  307. }
  308. tmc2130_sg_crash = false;
  309. #endif
  310. }
  311. void tmc2130_sg_meassure_start(uint8_t axis)
  312. {
  313. tmc2130_sg_meassure = axis;
  314. tmc2130_sg_meassure_cnt = 0;
  315. tmc2130_sg_meassure_val = 0;
  316. }
  317. uint16_t tmc2130_sg_meassure_stop()
  318. {
  319. tmc2130_sg_meassure = 0xff;
  320. return tmc2130_sg_meassure_val / tmc2130_sg_meassure_cnt;
  321. }
  322. bool tmc2130_wait_standstill_xy(int timeout)
  323. {
  324. // DBG(_n("tmc2130_wait_standstill_xy(timeout=%d)\n"), timeout);
  325. bool standstill = false;
  326. while (!standstill && (timeout > 0))
  327. {
  328. uint32_t drv_status_x = 0;
  329. uint32_t drv_status_y = 0;
  330. tmc2130_rd(X_AXIS, TMC2130_REG_DRV_STATUS, &drv_status_x);
  331. tmc2130_rd(Y_AXIS, TMC2130_REG_DRV_STATUS, &drv_status_y);
  332. // DBG(_n("\tdrv_status_x=0x%08x drv_status_x=0x%08x\n"), drv_status_x, drv_status_y);
  333. standstill = (drv_status_x & 0x80000000) && (drv_status_y & 0x80000000);
  334. tmc2130_check_overtemp();
  335. timeout--;
  336. }
  337. return standstill;
  338. }
  339. void tmc2130_check_overtemp()
  340. {
  341. static uint32_t checktime = 0;
  342. if (millis() - checktime > 1000 )
  343. {
  344. for (int i = 0; i < 4; i++)
  345. {
  346. uint32_t drv_status = 0;
  347. skip_debug_msg = true;
  348. tmc2130_rd(i, TMC2130_REG_DRV_STATUS, &drv_status);
  349. if (drv_status & ((uint32_t)1 << 26))
  350. { // BIT 26 - over temp prewarning ~120C (+-20C)
  351. SERIAL_ERRORRPGM(MSG_TMC_OVERTEMP);
  352. SERIAL_ECHOLN(i);
  353. for (int j = 0; j < 4; j++)
  354. tmc2130_wr(j, TMC2130_REG_CHOPCONF, 0x00010000);
  355. kill(MSG_TMC_OVERTEMP);
  356. }
  357. }
  358. checktime = millis();
  359. tmc2130_sg_change = true;
  360. }
  361. #ifdef DEBUG_CRASHDET_COUNTERS
  362. if (tmc2130_sg_change)
  363. {
  364. for (int i = 0; i < 4; i++)
  365. {
  366. tmc2130_sg_change = false;
  367. lcd_set_cursor(0 + i*4, 3);
  368. lcd_print(itostr3(tmc2130_sg_cnt[i]));
  369. lcd_print(' ');
  370. }
  371. }
  372. #endif //DEBUG_CRASHDET_COUNTERS
  373. }
  374. void tmc2130_setup_chopper(uint8_t axis, uint8_t mres, uint8_t current_h, uint8_t current_r)
  375. {
  376. uint8_t intpol = 1;
  377. uint8_t toff = tmc2130_chopper_config[axis].toff; // toff = 3 (fchop = 27.778kHz)
  378. uint8_t hstrt = tmc2130_chopper_config[axis].hstr; //initial 4, modified to 5
  379. uint8_t hend = tmc2130_chopper_config[axis].hend; //original value = 1
  380. uint8_t fd3 = 0;
  381. uint8_t rndtf = 0; //random off time
  382. uint8_t chm = 0; //spreadCycle
  383. uint8_t tbl = tmc2130_chopper_config[axis].tbl; //blanking time, original value = 2
  384. if (axis == E_AXIS)
  385. {
  386. #ifdef TMC2130_CNSTOFF_E
  387. // fd = 0 (slow decay only)
  388. hstrt = 0; //fd0..2
  389. fd3 = 0; //fd3
  390. hend = 0; //sine wave offset
  391. chm = 1; // constant off time mod
  392. #endif //TMC2130_CNSTOFF_E
  393. // toff = TMC2130_TOFF_E; // toff = 3-5
  394. // rndtf = 1;
  395. }
  396. DBG(_n("tmc2130_setup_chopper(axis=%hhd, mres=%hhd, curh=%hhd, curr=%hhd\n"), axis, mres, current_h, current_r);
  397. DBG(_n(" toff=%hhd, hstr=%hhd, hend=%hhd, tbl=%hhd\n"), toff, hstrt, hend, tbl);
  398. if (current_r <= 31)
  399. {
  400. tmc2130_wr_CHOPCONF(axis, toff, hstrt, hend, fd3, 0, rndtf, chm, tbl, 1, 0, 0, 0, mres, intpol, 0, 0);
  401. tmc2130_wr(axis, TMC2130_REG_IHOLD_IRUN, 0x000f0000 | ((current_r & 0x1f) << 8) | (current_h & 0x1f));
  402. }
  403. else
  404. {
  405. tmc2130_wr_CHOPCONF(axis, toff, hstrt, hend, fd3, 0, rndtf, chm, tbl, 0, 0, 0, 0, mres, intpol, 0, 0);
  406. tmc2130_wr(axis, TMC2130_REG_IHOLD_IRUN, 0x000f0000 | (((current_r >> 1) & 0x1f) << 8) | ((current_h >> 1) & 0x1f));
  407. }
  408. }
  409. void tmc2130_set_current_h(uint8_t axis, uint8_t current)
  410. {
  411. DBG(_n("tmc2130_set_current_h(axis=%d, current=%d\n"), axis, current);
  412. tmc2130_current_h[axis] = current;
  413. tmc2130_setup_chopper(axis, tmc2130_mres[axis], tmc2130_current_h[axis], tmc2130_current_r[axis]);
  414. }
  415. void tmc2130_set_current_r(uint8_t axis, uint8_t current)
  416. {
  417. DBG(_n("tmc2130_set_current_r(axis=%d, current=%d\n"), axis, current);
  418. tmc2130_current_r[axis] = current;
  419. tmc2130_setup_chopper(axis, tmc2130_mres[axis], tmc2130_current_h[axis], tmc2130_current_r[axis]);
  420. }
  421. void tmc2130_print_currents()
  422. {
  423. 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"),
  424. tmc2130_current_h[0], tmc2130_current_r[0],
  425. tmc2130_current_h[1], tmc2130_current_r[1],
  426. tmc2130_current_h[2], tmc2130_current_r[2],
  427. tmc2130_current_h[3], tmc2130_current_r[3]
  428. );
  429. }
  430. void tmc2130_set_pwm_ampl(uint8_t axis, uint8_t pwm_ampl)
  431. {
  432. DBG(_n("tmc2130_set_pwm_ampl(axis=%hhd, pwm_ampl=%hhd\n"), axis, pwm_ampl);
  433. tmc2130_pwm_ampl[axis] = pwm_ampl;
  434. if (((axis == 0) || (axis == 1)) && (tmc2130_mode == TMC2130_MODE_SILENT))
  435. tmc2130_wr_PWMCONF(axis, tmc2130_pwm_ampl[axis], tmc2130_pwm_grad[axis], tmc2130_pwm_freq[axis], tmc2130_pwm_auto[axis], 0, 0);
  436. }
  437. void tmc2130_set_pwm_grad(uint8_t axis, uint8_t pwm_grad)
  438. {
  439. DBG(_n("tmc2130_set_pwm_grad(axis=%hhd, pwm_grad=%hhd\n"), axis, pwm_grad);
  440. tmc2130_pwm_grad[axis] = pwm_grad;
  441. if (((axis == 0) || (axis == 1)) && (tmc2130_mode == TMC2130_MODE_SILENT))
  442. tmc2130_wr_PWMCONF(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 axis)
  445. {
  446. uint32_t val32 = 0;
  447. tmc2130_rd(axis, TMC2130_REG_TSTEP, &val32);
  448. if (val32 & 0x000f0000) return 0xffff;
  449. return val32 & 0xffff;
  450. }
  451. uint16_t tmc2130_rd_MSCNT(uint8_t axis)
  452. {
  453. uint32_t val32 = 0;
  454. tmc2130_rd(axis, TMC2130_REG_MSCNT, &val32);
  455. return val32 & 0x3ff;
  456. }
  457. uint32_t tmc2130_rd_MSCURACT(uint8_t axis)
  458. {
  459. uint32_t val32 = 0;
  460. tmc2130_rd(axis, TMC2130_REG_MSCURACT, &val32);
  461. return val32;
  462. }
  463. void tmc2130_wr_MSLUTSTART(uint8_t axis, uint8_t start_sin, uint8_t start_sin90)
  464. {
  465. uint32_t val = 0;
  466. val |= (uint32_t)start_sin;
  467. val |= ((uint32_t)start_sin90) << 16;
  468. tmc2130_wr(axis, TMC2130_REG_MSLUTSTART, val);
  469. //printf_P(PSTR("MSLUTSTART=%08lx (start_sin=%d start_sin90=%d)\n"), val, start_sin, start_sin90);
  470. }
  471. 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)
  472. {
  473. uint32_t val = 0;
  474. val |= ((uint32_t)w0);
  475. val |= ((uint32_t)w1) << 2;
  476. val |= ((uint32_t)w2) << 4;
  477. val |= ((uint32_t)w3) << 6;
  478. val |= ((uint32_t)x1) << 8;
  479. val |= ((uint32_t)x2) << 16;
  480. val |= ((uint32_t)x3) << 24;
  481. tmc2130_wr(axis, TMC2130_REG_MSLUTSEL, val);
  482. //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);
  483. }
  484. void tmc2130_wr_MSLUT(uint8_t axis, uint8_t i, uint32_t val)
  485. {
  486. tmc2130_wr(axis, TMC2130_REG_MSLUT0 + (i & 7), val);
  487. //printf_P(PSTR("MSLUT[%d]=%08lx\n"), i, val);
  488. }
  489. 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)
  490. {
  491. uint32_t val = 0;
  492. val |= (uint32_t)(toff & 15);
  493. val |= (uint32_t)(hstrt & 7) << 4;
  494. val |= (uint32_t)(hend & 15) << 7;
  495. val |= (uint32_t)(fd3 & 1) << 11;
  496. val |= (uint32_t)(disfdcc & 1) << 12;
  497. val |= (uint32_t)(rndtf & 1) << 13;
  498. val |= (uint32_t)(chm & 1) << 14;
  499. val |= (uint32_t)(tbl & 3) << 15;
  500. val |= (uint32_t)(vsense & 1) << 17;
  501. val |= (uint32_t)(vhighfs & 1) << 18;
  502. val |= (uint32_t)(vhighchm & 1) << 19;
  503. val |= (uint32_t)(sync & 15) << 20;
  504. val |= (uint32_t)(mres & 15) << 24;
  505. val |= (uint32_t)(intpol & 1) << 28;
  506. val |= (uint32_t)(dedge & 1) << 29;
  507. val |= (uint32_t)(diss2g & 1) << 30;
  508. tmc2130_wr(axis, TMC2130_REG_CHOPCONF, val);
  509. }
  510. //void tmc2130_wr_PWMCONF(uint8_t axis, uint8_t PWMautoScale, uint8_t PWMfreq, uint8_t PWMgrad, uint8_t PWMampl)
  511. 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)
  512. {
  513. uint32_t val = 0;
  514. val |= (uint32_t)(pwm_ampl & 255);
  515. val |= (uint32_t)(pwm_grad & 255) << 8;
  516. val |= (uint32_t)(pwm_freq & 3) << 16;
  517. val |= (uint32_t)(pwm_auto & 1) << 18;
  518. val |= (uint32_t)(pwm_symm & 1) << 19;
  519. val |= (uint32_t)(freewheel & 3) << 20;
  520. tmc2130_wr(axis, TMC2130_REG_PWMCONF, val);
  521. // 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
  522. }
  523. void tmc2130_wr_TPWMTHRS(uint8_t axis, uint32_t val32)
  524. {
  525. tmc2130_wr(axis, TMC2130_REG_TPWMTHRS, val32);
  526. }
  527. void tmc2130_wr_THIGH(uint8_t axis, uint32_t val32)
  528. {
  529. tmc2130_wr(axis, TMC2130_REG_THIGH, val32);
  530. }
  531. uint8_t tmc2130_usteps2mres(uint16_t usteps)
  532. {
  533. uint8_t mres = 8; while (mres && (usteps >>= 1)) mres--;
  534. return mres;
  535. }
  536. inline void tmc2130_cs_low(uint8_t axis)
  537. {
  538. switch (axis)
  539. {
  540. case X_AXIS: WRITE(X_TMC2130_CS, LOW); break;
  541. case Y_AXIS: WRITE(Y_TMC2130_CS, LOW); break;
  542. case Z_AXIS: WRITE(Z_TMC2130_CS, LOW); break;
  543. case E_AXIS: WRITE(E0_TMC2130_CS, LOW); break;
  544. }
  545. }
  546. inline void tmc2130_cs_high(uint8_t axis)
  547. {
  548. switch (axis)
  549. {
  550. case X_AXIS: WRITE(X_TMC2130_CS, HIGH); break;
  551. case Y_AXIS: WRITE(Y_TMC2130_CS, HIGH); break;
  552. case Z_AXIS: WRITE(Z_TMC2130_CS, HIGH); break;
  553. case E_AXIS: WRITE(E0_TMC2130_CS, HIGH); break;
  554. }
  555. }
  556. //spi
  557. #define TMC2130_SPI_ENTER() spi_setup(TMC2130_SPCR, TMC2130_SPSR)
  558. #define TMC2130_SPI_TXRX spi_txrx
  559. #define TMC2130_SPI_LEAVE()
  560. uint8_t tmc2130_tx(uint8_t axis, uint8_t addr, uint32_t wval)
  561. {
  562. //datagram1 - request
  563. TMC2130_SPI_ENTER();
  564. tmc2130_cs_low(axis);
  565. TMC2130_SPI_TXRX(addr); // address
  566. TMC2130_SPI_TXRX((wval >> 24) & 0xff); // MSB
  567. TMC2130_SPI_TXRX((wval >> 16) & 0xff);
  568. TMC2130_SPI_TXRX((wval >> 8) & 0xff);
  569. TMC2130_SPI_TXRX(wval & 0xff); // LSB
  570. tmc2130_cs_high(axis);
  571. TMC2130_SPI_LEAVE();
  572. }
  573. uint8_t tmc2130_rx(uint8_t axis, uint8_t addr, uint32_t* rval)
  574. {
  575. //datagram1 - request
  576. TMC2130_SPI_ENTER();
  577. tmc2130_cs_low(axis);
  578. TMC2130_SPI_TXRX(addr); // address
  579. TMC2130_SPI_TXRX(0); // MSB
  580. TMC2130_SPI_TXRX(0);
  581. TMC2130_SPI_TXRX(0);
  582. TMC2130_SPI_TXRX(0); // LSB
  583. tmc2130_cs_high(axis);
  584. TMC2130_SPI_LEAVE();
  585. //datagram2 - response
  586. TMC2130_SPI_ENTER();
  587. tmc2130_cs_low(axis);
  588. uint8_t stat = TMC2130_SPI_TXRX(0); // status
  589. uint32_t val32 = 0;
  590. val32 = TMC2130_SPI_TXRX(0); // MSB
  591. val32 = (val32 << 8) | TMC2130_SPI_TXRX(0);
  592. val32 = (val32 << 8) | TMC2130_SPI_TXRX(0);
  593. val32 = (val32 << 8) | TMC2130_SPI_TXRX(0); // LSB
  594. tmc2130_cs_high(axis);
  595. TMC2130_SPI_LEAVE();
  596. if (rval != 0) *rval = val32;
  597. return stat;
  598. }
  599. void tmc2130_eeprom_load_config()
  600. {
  601. }
  602. void tmc2130_eeprom_save_config()
  603. {
  604. }
  605. #define _GET_PWR_X (READ(X_ENABLE_PIN) == X_ENABLE_ON)
  606. #define _GET_PWR_Y (READ(Y_ENABLE_PIN) == Y_ENABLE_ON)
  607. #define _GET_PWR_Z (READ(Z_ENABLE_PIN) == Z_ENABLE_ON)
  608. #define _GET_PWR_E (READ(E0_ENABLE_PIN) == E_ENABLE_ON)
  609. #define _SET_PWR_X(ena) { WRITE(X_ENABLE_PIN, ena?X_ENABLE_ON:!X_ENABLE_ON); asm("nop"); }
  610. #define _SET_PWR_Y(ena) { WRITE(Y_ENABLE_PIN, ena?Y_ENABLE_ON:!Y_ENABLE_ON); asm("nop"); }
  611. #define _SET_PWR_Z(ena) { WRITE(Z_ENABLE_PIN, ena?Z_ENABLE_ON:!Z_ENABLE_ON); asm("nop"); }
  612. #define _SET_PWR_E(ena) { WRITE(E0_ENABLE_PIN, ena?E_ENABLE_ON:!E_ENABLE_ON); asm("nop"); }
  613. #define _GET_DIR_X (READ(X_DIR_PIN) == INVERT_X_DIR)
  614. #define _GET_DIR_Y (READ(Y_DIR_PIN) == INVERT_Y_DIR)
  615. #define _GET_DIR_Z (READ(Z_DIR_PIN) == INVERT_Z_DIR)
  616. #define _GET_DIR_E (READ(E0_DIR_PIN) == INVERT_E0_DIR)
  617. #define _SET_DIR_X(dir) { WRITE(X_DIR_PIN, dir?INVERT_X_DIR:!INVERT_X_DIR); asm("nop"); }
  618. #define _SET_DIR_Y(dir) { WRITE(Y_DIR_PIN, dir?INVERT_Y_DIR:!INVERT_Y_DIR); asm("nop"); }
  619. #define _SET_DIR_Z(dir) { WRITE(Z_DIR_PIN, dir?INVERT_Z_DIR:!INVERT_Z_DIR); asm("nop"); }
  620. #define _SET_DIR_E(dir) { WRITE(E0_DIR_PIN, dir?INVERT_E0_DIR:!INVERT_E0_DIR); asm("nop"); }
  621. #define _DO_STEP_X { WRITE(X_STEP_PIN, !INVERT_X_STEP_PIN); asm("nop"); WRITE(X_STEP_PIN, INVERT_X_STEP_PIN); asm("nop"); }
  622. #define _DO_STEP_Y { WRITE(Y_STEP_PIN, !INVERT_Y_STEP_PIN); asm("nop"); WRITE(Y_STEP_PIN, INVERT_Y_STEP_PIN); asm("nop"); }
  623. #define _DO_STEP_Z { WRITE(Z_STEP_PIN, !INVERT_Z_STEP_PIN); asm("nop"); WRITE(Z_STEP_PIN, INVERT_Z_STEP_PIN); asm("nop"); }
  624. #define _DO_STEP_E { WRITE(E0_STEP_PIN, !INVERT_E_STEP_PIN); asm("nop"); WRITE(E0_STEP_PIN, INVERT_E_STEP_PIN); asm("nop"); }
  625. uint16_t tmc2130_get_res(uint8_t axis)
  626. {
  627. return tmc2130_mres2usteps(tmc2130_mres[axis]);
  628. }
  629. void tmc2130_set_res(uint8_t axis, uint16_t res)
  630. {
  631. tmc2130_mres[axis] = tmc2130_usteps2mres(res);
  632. // uint32_t u = micros();
  633. tmc2130_setup_chopper(axis, tmc2130_mres[axis], tmc2130_current_h[axis], tmc2130_current_r[axis]);
  634. // u = micros() - u;
  635. // printf_P(PSTR("tmc2130_setup_chopper %c %lu us"), "XYZE"[axis], u);
  636. }
  637. uint8_t tmc2130_get_pwr(uint8_t axis)
  638. {
  639. switch (axis)
  640. {
  641. case X_AXIS: return _GET_PWR_X;
  642. case Y_AXIS: return _GET_PWR_Y;
  643. case Z_AXIS: return _GET_PWR_Z;
  644. case E_AXIS: return _GET_PWR_E;
  645. }
  646. return 0;
  647. }
  648. void tmc2130_set_pwr(uint8_t axis, uint8_t pwr)
  649. {
  650. switch (axis)
  651. {
  652. case X_AXIS: _SET_PWR_X(pwr); break;
  653. case Y_AXIS: _SET_PWR_Y(pwr); break;
  654. case Z_AXIS: _SET_PWR_Z(pwr); break;
  655. case E_AXIS: _SET_PWR_E(pwr); break;
  656. }
  657. }
  658. uint8_t tmc2130_get_inv(uint8_t axis)
  659. {
  660. switch (axis)
  661. {
  662. case X_AXIS: return INVERT_X_DIR;
  663. case Y_AXIS: return INVERT_Y_DIR;
  664. case Z_AXIS: return INVERT_Z_DIR;
  665. case E_AXIS: return INVERT_E0_DIR;
  666. }
  667. return 0;
  668. }
  669. uint8_t tmc2130_get_dir(uint8_t axis)
  670. {
  671. switch (axis)
  672. {
  673. case X_AXIS: return _GET_DIR_X;
  674. case Y_AXIS: return _GET_DIR_Y;
  675. case Z_AXIS: return _GET_DIR_Z;
  676. case E_AXIS: return _GET_DIR_E;
  677. }
  678. return 0;
  679. }
  680. void tmc2130_set_dir(uint8_t axis, uint8_t dir)
  681. {
  682. switch (axis)
  683. {
  684. case X_AXIS: _SET_DIR_X(dir); break;
  685. case Y_AXIS: _SET_DIR_Y(dir); break;
  686. case Z_AXIS: _SET_DIR_Z(dir); break;
  687. case E_AXIS: _SET_DIR_E(dir); break;
  688. }
  689. }
  690. void tmc2130_do_step(uint8_t axis)
  691. {
  692. switch (axis)
  693. {
  694. case X_AXIS: _DO_STEP_X; break;
  695. case Y_AXIS: _DO_STEP_Y; break;
  696. case Z_AXIS: _DO_STEP_Z; break;
  697. case E_AXIS: _DO_STEP_E; break;
  698. }
  699. }
  700. void tmc2130_do_steps(uint8_t axis, uint16_t steps, uint8_t dir, uint16_t delay_us)
  701. {
  702. tmc2130_set_dir(axis, dir);
  703. delayMicroseconds(100);
  704. while (steps--)
  705. {
  706. tmc2130_do_step(axis);
  707. delayMicroseconds(delay_us);
  708. }
  709. }
  710. void tmc2130_goto_step(uint8_t axis, uint8_t step, uint8_t dir, uint16_t delay_us, uint16_t microstep_resolution)
  711. {
  712. printf_P(PSTR("tmc2130_goto_step %d %d %d %d \n"), axis, step, dir, delay_us, microstep_resolution);
  713. uint8_t shift; for (shift = 0; shift < 8; shift++) if (microstep_resolution == (256 >> shift)) break;
  714. uint16_t cnt = 4 * (1 << (8 - shift));
  715. uint16_t mscnt = tmc2130_rd_MSCNT(axis);
  716. if (dir == 2)
  717. {
  718. dir = tmc2130_get_inv(axis)?0:1;
  719. int steps = (int)step - (int)(mscnt >> shift);
  720. if (steps < 0)
  721. {
  722. dir ^= 1;
  723. steps = -steps;
  724. }
  725. if (steps > (cnt / 2))
  726. {
  727. dir ^= 1;
  728. steps = cnt - steps;
  729. }
  730. cnt = steps;
  731. }
  732. tmc2130_set_dir(axis, dir);
  733. delayMicroseconds(100);
  734. mscnt = tmc2130_rd_MSCNT(axis);
  735. while ((cnt--) && ((mscnt >> shift) != step))
  736. {
  737. tmc2130_do_step(axis);
  738. delayMicroseconds(delay_us);
  739. mscnt = tmc2130_rd_MSCNT(axis);
  740. }
  741. }
  742. void tmc2130_get_wave(uint8_t axis, uint8_t* data, FILE* stream)
  743. {
  744. uint8_t pwr = tmc2130_get_pwr(axis);
  745. tmc2130_set_pwr(axis, 0);
  746. tmc2130_setup_chopper(axis, tmc2130_usteps2mres(256), tmc2130_current_h[axis], tmc2130_current_r[axis]);
  747. tmc2130_goto_step(axis, 0, 2, 100, 256);
  748. tmc2130_set_dir(axis, tmc2130_get_inv(axis)?0:1);
  749. for (int i = 0; i <= 255; i++)
  750. {
  751. uint32_t val = tmc2130_rd_MSCURACT(axis);
  752. uint16_t mscnt = tmc2130_rd_MSCNT(axis);
  753. int curA = (val & 0xff) | ((val << 7) & 0x8000);
  754. if (stream)
  755. {
  756. if (mscnt == i)
  757. fprintf_P(stream, PSTR("%d\t%d\n"), i, curA);
  758. else //TODO - remove this check
  759. fprintf_P(stream, PSTR("!! (i=%d MSCNT=%d)\n"), i, mscnt);
  760. }
  761. if (data) *(data++) = curA;
  762. tmc2130_do_step(axis);
  763. delayMicroseconds(100);
  764. }
  765. tmc2130_setup_chopper(axis, tmc2130_mres[axis], tmc2130_current_h[axis], tmc2130_current_r[axis]);
  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. uint8_t d0 = 0; //delta0
  780. uint8_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. uint8_t dA; //delta value
  786. int i; //microstep index
  787. uint32_t reg; //tmc2130 register
  788. tmc2130_wr_MSLUTSTART(axis, 0, amp);
  789. for (i = 0; i < 256; i++)
  790. {
  791. if ((i & 31) == 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, 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. #endif //TMC2130