tmc2130.cpp 32 KB

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