tmc2130.cpp 36 KB

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