tmc2130.cpp 33 KB

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