tmc2130.cpp 35 KB

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