tmc2130.cpp 36 KB

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