tmc2130.cpp 35 KB

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