tmc2130.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. #include "Marlin.h"
  2. #ifdef TMC2130
  3. #include "tmc2130.h"
  4. #include <SPI.h>
  5. #define TMC2130_GCONF_NORMAL 0x00000000 // spreadCycle
  6. #define TMC2130_GCONF_SGSENS 0x00003180 // spreadCycle with stallguard (stall activates DIAG0 and DIAG1 [pushpull])
  7. #define TMC2130_GCONF_SILENT 0x00000004 // stealthChop
  8. //externals for debuging
  9. extern float current_position[4];
  10. extern void st_get_position_xy(long &x, long &y);
  11. extern long st_get_position(uint8_t axis);
  12. //chipselect pins
  13. uint8_t tmc2130_cs[4] = { X_TMC2130_CS, Y_TMC2130_CS, Z_TMC2130_CS, E0_TMC2130_CS };
  14. //diag pins
  15. uint8_t tmc2130_diag[4] = { X_TMC2130_DIAG, Y_TMC2130_DIAG, Z_TMC2130_DIAG, E0_TMC2130_DIAG };
  16. //mode
  17. uint8_t tmc2130_mode = TMC2130_MODE_NORMAL;
  18. //holding currents
  19. uint8_t tmc2130_current_h[4] = TMC2130_CURRENTS_H;
  20. //running currents
  21. uint8_t tmc2130_current_r[4] = TMC2130_CURRENTS_R;
  22. //axis stalled flags
  23. uint8_t tmc2130_axis_stalled[3] = {0, 0, 0};
  24. //pwm_ampl
  25. uint8_t tmc2130_pwm_ampl[2] = {TMC2130_PWM_AMPL_X, TMC2130_PWM_AMPL_Y};
  26. //pwm_grad
  27. uint8_t tmc2130_pwm_grad[2] = {TMC2130_PWM_GRAD_X, TMC2130_PWM_GRAD_Y};
  28. //pwm_auto
  29. uint8_t tmc2130_pwm_auto[2] = {TMC2130_PWM_AUTO_X, TMC2130_PWM_AUTO_Y};
  30. //pwm_freq
  31. uint8_t tmc2130_pwm_freq[2] = {TMC2130_PWM_FREQ_X, TMC2130_PWM_FREQ_Y};
  32. uint8_t tmc2131_axis_sg_thr[3] = {TMC2130_SG_THRS_X, TMC2130_SG_THRS_Y, TMC2130_SG_THRS_Z};
  33. uint32_t tmc2131_axis_sg_pos[3] = {0, 0, 0};
  34. uint8_t sg_homing_axes_mask = 0x00;
  35. bool skip_debug_msg = false;
  36. //TMC2130 registers
  37. #define TMC2130_REG_GCONF 0x00 // 17 bits
  38. #define TMC2130_REG_GSTAT 0x01 // 3 bits
  39. #define TMC2130_REG_IOIN 0x04 // 8+8 bits
  40. #define TMC2130_REG_IHOLD_IRUN 0x10 // 5+5+4 bits
  41. #define TMC2130_REG_TPOWERDOWN 0x11 // 8 bits
  42. #define TMC2130_REG_TSTEP 0x12 // 20 bits
  43. #define TMC2130_REG_TPWMTHRS 0x13 // 20 bits
  44. #define TMC2130_REG_TCOOLTHRS 0x14 // 20 bits
  45. #define TMC2130_REG_THIGH 0x15 // 20 bits
  46. #define TMC2130_REG_XDIRECT 0x2d // 32 bits
  47. #define TMC2130_REG_VDCMIN 0x33 // 23 bits
  48. #define TMC2130_REG_MSLUT0 0x60 // 32 bits
  49. #define TMC2130_REG_MSLUT1 0x61 // 32 bits
  50. #define TMC2130_REG_MSLUT2 0x62 // 32 bits
  51. #define TMC2130_REG_MSLUT3 0x63 // 32 bits
  52. #define TMC2130_REG_MSLUT4 0x64 // 32 bits
  53. #define TMC2130_REG_MSLUT5 0x65 // 32 bits
  54. #define TMC2130_REG_MSLUT6 0x66 // 32 bits
  55. #define TMC2130_REG_MSLUT7 0x67 // 32 bits
  56. #define TMC2130_REG_MSLUTSEL 0x68 // 32 bits
  57. #define TMC2130_REG_MSLUTSTART 0x69 // 8+8 bits
  58. #define TMC2130_REG_MSCNT 0x6a // 10 bits
  59. #define TMC2130_REG_MSCURACT 0x6b // 9+9 bits
  60. #define TMC2130_REG_CHOPCONF 0x6c // 32 bits
  61. #define TMC2130_REG_COOLCONF 0x6d // 25 bits
  62. #define TMC2130_REG_DCCTRL 0x6e // 24 bits
  63. #define TMC2130_REG_DRV_STATUS 0x6f // 32 bits
  64. #define TMC2130_REG_PWMCONF 0x70 // 22 bits
  65. #define TMC2130_REG_PWM_SCALE 0x71 // 8 bits
  66. #define TMC2130_REG_ENCM_CTRL 0x72 // 2 bits
  67. #define TMC2130_REG_LOST_STEPS 0x73 // 20 bits
  68. uint16_t tmc2130_rd_TSTEP(uint8_t cs);
  69. uint16_t tmc2130_rd_MSCNT(uint8_t cs);
  70. uint16_t tmc2130_rd_DRV_STATUS(uint8_t cs);
  71. void tmc2130_wr_CHOPCONF(uint8_t cs, 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);
  72. void tmc2130_wr_PWMCONF(uint8_t cs, uint8_t pwm_ampl, uint8_t pwm_grad, uint8_t pwm_freq, uint8_t pwm_auto, uint8_t pwm_symm, uint8_t freewheel);
  73. void tmc2130_wr_TPWMTHRS(uint8_t cs, uint32_t val32);
  74. void tmc2130_wr_THIGH(uint8_t cs, uint32_t val32);
  75. uint8_t tmc2130_axis_by_cs(uint8_t cs);
  76. uint8_t tmc2130_mres(uint16_t microstep_resolution);
  77. uint8_t tmc2130_wr(uint8_t cs, uint8_t addr, uint32_t wval);
  78. uint8_t tmc2130_rd(uint8_t cs, uint8_t addr, uint32_t* rval);
  79. uint8_t tmc2130_txrx(uint8_t cs, uint8_t addr, uint32_t wval, uint32_t* rval);
  80. void tmc2130_init()
  81. {
  82. MYSERIAL.print("tmc2130_init mode=");
  83. MYSERIAL.println(tmc2130_mode, DEC);
  84. WRITE(X_TMC2130_CS, HIGH);
  85. WRITE(Y_TMC2130_CS, HIGH);
  86. WRITE(Z_TMC2130_CS, HIGH);
  87. WRITE(E0_TMC2130_CS, HIGH);
  88. SET_OUTPUT(X_TMC2130_CS);
  89. SET_OUTPUT(Y_TMC2130_CS);
  90. SET_OUTPUT(Z_TMC2130_CS);
  91. SET_OUTPUT(E0_TMC2130_CS);
  92. SET_INPUT(X_TMC2130_DIAG);
  93. SET_INPUT(Y_TMC2130_DIAG);
  94. SET_INPUT(Z_TMC2130_DIAG);
  95. SET_INPUT(E0_TMC2130_DIAG);
  96. SPI.begin();
  97. for (int i = 0; i < 2; i++) // X Y axes
  98. {
  99. uint8_t mres = tmc2130_mres(TMC2130_USTEPS_XY);
  100. tmc2130_wr_CHOPCONF(tmc2130_cs[i], 3, 5, 1, 0, 0, 0, 0, 2, 1, 0, 0, 0, mres, TMC2130_INTPOL_XY, 0, 0);
  101. tmc2130_wr(tmc2130_cs[i], TMC2130_REG_IHOLD_IRUN, 0x000f0000 | ((tmc2130_current_r[i] & 0x1f) << 8) | (tmc2130_current_h[i] & 0x1f));
  102. tmc2130_wr(tmc2130_cs[i], TMC2130_REG_TPOWERDOWN, 0x00000000);
  103. tmc2130_wr(tmc2130_cs[i], TMC2130_REG_GCONF, (tmc2130_mode == TMC2130_MODE_SILENT)?TMC2130_GCONF_SILENT:TMC2130_GCONF_SGSENS);
  104. tmc2130_wr(tmc2130_cs[i], TMC2130_REG_TCOOLTHRS, (tmc2130_mode == TMC2130_MODE_SILENT)?0:TMC2130_TCOOLTHRS);
  105. tmc2130_wr_PWMCONF(tmc2130_cs[i], tmc2130_pwm_ampl[i], tmc2130_pwm_grad[i], tmc2130_pwm_freq[i], tmc2130_pwm_auto[i], 0, 0);
  106. tmc2130_wr_TPWMTHRS(tmc2130_cs[i], TMC2130_TPWMTHRS);
  107. //tmc2130_wr_THIGH(tmc2130_cs[i], TMC2130_THIGH);
  108. }
  109. for (int i = 2; i < 3; i++) // Z axis
  110. {
  111. uint8_t mres = tmc2130_mres(TMC2130_USTEPS_Z);
  112. if (tmc2130_current_r[i] <= 31)
  113. {
  114. tmc2130_wr_CHOPCONF(tmc2130_cs[i], 3, 5, 1, 0, 0, 0, 0, 2, 1, 0, 0, 0, mres, TMC2130_INTPOL_Z, 0, 0);
  115. tmc2130_wr(tmc2130_cs[i], TMC2130_REG_IHOLD_IRUN, 0x000f0000 | ((tmc2130_current_r[i] & 0x1f) << 8) | (tmc2130_current_h[i] & 0x1f));
  116. }
  117. else
  118. {
  119. tmc2130_wr_CHOPCONF(tmc2130_cs[i], 3, 5, 1, 0, 0, 0, 0, 2, 0, 0, 0, 0, mres, TMC2130_INTPOL_Z, 0, 0);
  120. tmc2130_wr(tmc2130_cs[i], TMC2130_REG_IHOLD_IRUN, 0x000f0000 | (((tmc2130_current_r[i] >> 1) & 0x1f) << 8) | ((tmc2130_current_h[i] >> 1) & 0x1f));
  121. }
  122. tmc2130_wr(tmc2130_cs[i], TMC2130_REG_TPOWERDOWN, 0x00000000);
  123. tmc2130_wr(tmc2130_cs[i], TMC2130_REG_GCONF, TMC2130_GCONF_SGSENS);
  124. }
  125. for (int i = 3; i < 4; i++) // E axis
  126. {
  127. uint8_t mres = tmc2130_mres(TMC2130_USTEPS_E);
  128. tmc2130_wr_CHOPCONF(tmc2130_cs[i], 3, 5, 1, 0, 0, 0, 0, 2, 1, 0, 0, 0, mres, TMC2130_INTPOL_E, 0, 0);
  129. tmc2130_wr(tmc2130_cs[i], TMC2130_REG_IHOLD_IRUN, 0x000f0000 | ((tmc2130_current_r[i] & 0x1f) << 8) | (tmc2130_current_h[i] & 0x1f));
  130. tmc2130_wr(tmc2130_cs[i], TMC2130_REG_TPOWERDOWN, 0x00000000);
  131. tmc2130_wr(tmc2130_cs[i], TMC2130_REG_GCONF, 0x00000000);
  132. }
  133. }
  134. void tmc2130_update_sg_axis(uint8_t axis)
  135. {
  136. if (!tmc2130_axis_stalled[axis])
  137. {
  138. uint8_t cs = tmc2130_cs[axis];
  139. uint16_t tstep = tmc2130_rd_TSTEP(cs);
  140. if (tstep < TMC2130_TCOOLTHRS)
  141. {
  142. long pos = st_get_position(axis);
  143. if (abs(pos - tmc2131_axis_sg_pos[axis]) > TMC2130_SG_DELTA)
  144. {
  145. uint16_t sg = tmc2130_rd_DRV_STATUS(cs) & 0x3ff;
  146. if (sg == 0)
  147. tmc2130_axis_stalled[axis] = true;
  148. }
  149. }
  150. }
  151. }
  152. bool tmc2130_update_sg()
  153. {
  154. #ifdef TMC2130_SG_HOMING_SW_XY
  155. if (sg_homing_axes_mask & X_AXIS_MASK) tmc2130_update_sg_axis(X_AXIS);
  156. if (sg_homing_axes_mask & Y_AXIS_MASK) tmc2130_update_sg_axis(Y_AXIS);
  157. #endif //TMC2130_SG_HOMING_SW_XY
  158. #ifdef TMC2130_SG_HOMING_SW_Z
  159. if (sg_homing_axes_mask & Z_AXIS_MASK) tmc2130_update_sg_axis(Z_AXIS);
  160. #endif //TMC2130_SG_HOMING_SW_Z
  161. #if (defined(TMC2130_SG_HOMING) && defined(TMC2130_SG_HOMING_SW_XY))
  162. if (sg_homing_axes_mask == 0) return false;
  163. #ifdef TMC2130_DEBUG
  164. MYSERIAL.print("tmc2130_update_sg mask=0x");
  165. MYSERIAL.print((int)sg_homing_axes_mask, 16);
  166. MYSERIAL.print(" stalledX=");
  167. MYSERIAL.print((int)tmc2130_axis_stalled[0]);
  168. MYSERIAL.print(" stalledY=");
  169. MYSERIAL.println((int)tmc2130_axis_stalled[1]);
  170. #endif //TMC2130_DEBUG
  171. for (uint8_t axis = X_AXIS; axis <= Y_AXIS; axis++) //only X and Y axes
  172. {
  173. uint8_t mask = (X_AXIS_MASK << axis);
  174. if (sg_homing_axes_mask & mask)
  175. {
  176. if (!tmc2130_axis_stalled[axis])
  177. {
  178. uint8_t cs = tmc2130_cs[axis];
  179. uint16_t tstep = tmc2130_rd_TSTEP(cs);
  180. if (tstep < TMC2130_TCOOLTHRS)
  181. {
  182. long pos = st_get_position(axis);
  183. if (abs(pos - tmc2131_axis_sg_pos[axis]) > TMC2130_SG_DELTA)
  184. {
  185. uint16_t sg = tmc2130_rd_DRV_STATUS(cs) & 0x3ff;
  186. if (sg == 0)
  187. {
  188. tmc2130_axis_stalled[axis] = true;
  189. #ifdef TMC2130_DEBUG
  190. MYSERIAL.print("tmc2130_update_sg AXIS STALLED ");
  191. MYSERIAL.println((int)axis);
  192. #endif //TMC2130_DEBUG
  193. }
  194. }
  195. }
  196. }
  197. }
  198. }
  199. return true;
  200. #endif
  201. return false;
  202. }
  203. void tmc2130_home_enter(uint8_t axes_mask)
  204. {
  205. #ifdef TMC2130_DEBUG
  206. MYSERIAL.print("tmc2130_home_enter mask=0x");
  207. MYSERIAL.println((int)axes_mask, 16);
  208. #endif //TMC2130_DEBUG
  209. #ifdef TMC2130_SG_HOMING
  210. for (uint8_t axis = X_AXIS; axis <= Z_AXIS; axis++) //X Y and Z axes
  211. {
  212. uint8_t mask = (X_AXIS_MASK << axis);
  213. uint8_t cs = tmc2130_cs[axis];
  214. if (axes_mask & mask)
  215. {
  216. sg_homing_axes_mask |= mask;
  217. tmc2131_axis_sg_pos[axis] = st_get_position(axis);
  218. tmc2130_axis_stalled[axis] = false;
  219. //Configuration to spreadCycle
  220. tmc2130_wr(cs, TMC2130_REG_GCONF, TMC2130_GCONF_NORMAL);
  221. tmc2130_wr(cs, TMC2130_REG_COOLCONF, ((unsigned long)tmc2131_axis_sg_thr[axis]) << 16);
  222. tmc2130_wr(cs, TMC2130_REG_TCOOLTHRS, TMC2130_TCOOLTHRS);
  223. #ifndef TMC2130_SG_HOMING_SW_XY
  224. if (mask & (X_AXIS_MASK | Y_AXIS_MASK))
  225. tmc2130_wr(cs, TMC2130_REG_GCONF, TMC2130_GCONF_SGSENS); //stallguard output DIAG1, DIAG1 = pushpull
  226. #endif //TMC2130_SG_HOMING_SW_XY
  227. }
  228. }
  229. #endif //TMC2130_SG_HOMING
  230. }
  231. void tmc2130_home_exit()
  232. {
  233. #ifdef TMC2130_DEBUG
  234. MYSERIAL.print("tmc2130_home_exit mask=0x");
  235. MYSERIAL.println((int)sg_homing_axes_mask, 16);
  236. #endif //TMC2130_DEBUG
  237. #ifdef TMC2130_SG_HOMING
  238. if (sg_homing_axes_mask)
  239. {
  240. for (uint8_t axis = X_AXIS; axis <= Z_AXIS; axis++) //X Y and Z axes
  241. {
  242. uint8_t mask = (X_AXIS_MASK << axis);
  243. if (sg_homing_axes_mask & mask & (X_AXIS_MASK | Y_AXIS_MASK))
  244. {
  245. if (tmc2130_mode == TMC2130_MODE_SILENT)
  246. tmc2130_wr(tmc2130_cs[axis], TMC2130_REG_GCONF, TMC2130_GCONF_SILENT); // Configuration back to stealthChop
  247. else
  248. #ifdef TMC2130_SG_HOMING_SW_XY
  249. tmc2130_wr(tmc2130_cs[axis], TMC2130_REG_GCONF, TMC2130_GCONF_NORMAL);
  250. #else //TMC2130_SG_HOMING_SW_XY
  251. tmc2130_wr(tmc2130_cs[axis], TMC2130_REG_GCONF, TMC2130_GCONF_SGSENS);
  252. #endif //TMC2130_SG_HOMING_SW_XY
  253. }
  254. tmc2130_axis_stalled[axis] = false;
  255. }
  256. sg_homing_axes_mask = 0x00;
  257. }
  258. #endif
  259. }
  260. void tmc2130_home_restart(uint8_t axis)
  261. {
  262. tmc2131_axis_sg_pos[axis] = st_get_position(axis);
  263. tmc2130_axis_stalled[axis] = false;
  264. }
  265. void tmc2130_check_overtemp()
  266. {
  267. const static char TMC_OVERTEMP_MSG[] PROGMEM = "TMC DRIVER OVERTEMP ";
  268. static uint32_t checktime = 0;
  269. if (millis() - checktime > 1000 )
  270. {
  271. for (int i = 0; i < 4; i++)
  272. {
  273. uint32_t drv_status = 0;
  274. skip_debug_msg = true;
  275. tmc2130_rd(tmc2130_cs[i], TMC2130_REG_DRV_STATUS, &drv_status);
  276. if (drv_status & ((uint32_t)1 << 26))
  277. { // BIT 26 - over temp prewarning ~120C (+-20C)
  278. SERIAL_ERRORRPGM(TMC_OVERTEMP_MSG);
  279. SERIAL_ECHOLN(i);
  280. for (int j = 0; j < 4; j++)
  281. tmc2130_wr(tmc2130_cs[j], TMC2130_REG_CHOPCONF, 0x00010000);
  282. kill(TMC_OVERTEMP_MSG);
  283. }
  284. }
  285. checktime = millis();
  286. }
  287. }
  288. void tmc2130_set_current_h(uint8_t axis, uint8_t current)
  289. {
  290. MYSERIAL.print("tmc2130_set_current_h ");
  291. MYSERIAL.print((int)axis);
  292. MYSERIAL.print(" ");
  293. MYSERIAL.println((int)current);
  294. tmc2130_current_h[axis] = current;
  295. tmc2130_wr(tmc2130_cs[axis], TMC2130_REG_IHOLD_IRUN, 0x000f0000 | ((tmc2130_current_r[axis] & 0x1f) << 8) | (tmc2130_current_h[axis] & 0x1f));
  296. }
  297. void tmc2130_set_current_r(uint8_t axis, uint8_t current)
  298. {
  299. MYSERIAL.print("tmc2130_set_current_r ");
  300. MYSERIAL.print((int)axis);
  301. MYSERIAL.print(" ");
  302. MYSERIAL.println((int)current);
  303. tmc2130_current_r[axis] = current;
  304. tmc2130_wr(tmc2130_cs[axis], TMC2130_REG_IHOLD_IRUN, 0x000f0000 | ((tmc2130_current_r[axis] & 0x1f) << 8) | (tmc2130_current_h[axis] & 0x1f));
  305. }
  306. void tmc2130_print_currents()
  307. {
  308. MYSERIAL.println("tmc2130_print_currents");
  309. MYSERIAL.println("\tH\rR");
  310. MYSERIAL.print("X\t");
  311. MYSERIAL.print((int)tmc2130_current_h[0]);
  312. MYSERIAL.print("\t");
  313. MYSERIAL.println((int)tmc2130_current_r[0]);
  314. MYSERIAL.print("Y\t");
  315. MYSERIAL.print((int)tmc2130_current_h[1]);
  316. MYSERIAL.print("\t");
  317. MYSERIAL.println((int)tmc2130_current_r[1]);
  318. MYSERIAL.print("Z\t");
  319. MYSERIAL.print((int)tmc2130_current_h[2]);
  320. MYSERIAL.print("\t");
  321. MYSERIAL.println((int)tmc2130_current_r[2]);
  322. MYSERIAL.print("E\t");
  323. MYSERIAL.print((int)tmc2130_current_h[3]);
  324. MYSERIAL.print("\t");
  325. MYSERIAL.println((int)tmc2130_current_r[3]);
  326. }
  327. void tmc2130_set_pwm_ampl(uint8_t axis, uint8_t pwm_ampl)
  328. {
  329. MYSERIAL.print("tmc2130_set_pwm_ampl ");
  330. MYSERIAL.print((int)axis);
  331. MYSERIAL.print(" ");
  332. MYSERIAL.println((int)pwm_ampl);
  333. tmc2130_pwm_ampl[axis] = pwm_ampl;
  334. if (((axis == 0) || (axis == 1)) && (tmc2130_mode == TMC2130_MODE_SILENT))
  335. tmc2130_wr_PWMCONF(tmc2130_cs[axis], tmc2130_pwm_ampl[axis], tmc2130_pwm_grad[axis], tmc2130_pwm_freq[axis], tmc2130_pwm_auto[axis], 0, 0);
  336. }
  337. void tmc2130_set_pwm_grad(uint8_t axis, uint8_t pwm_grad)
  338. {
  339. MYSERIAL.print("tmc2130_set_pwm_grad ");
  340. MYSERIAL.print((int)axis);
  341. MYSERIAL.print(" ");
  342. MYSERIAL.println((int)pwm_grad);
  343. tmc2130_pwm_grad[axis] = pwm_grad;
  344. if (((axis == 0) || (axis == 1)) && (tmc2130_mode == TMC2130_MODE_SILENT))
  345. tmc2130_wr_PWMCONF(tmc2130_cs[axis], tmc2130_pwm_ampl[axis], tmc2130_pwm_grad[axis], tmc2130_pwm_freq[axis], tmc2130_pwm_auto[axis], 0, 0);
  346. }
  347. uint16_t tmc2130_rd_TSTEP(uint8_t cs)
  348. {
  349. uint32_t val32 = 0;
  350. tmc2130_rd(cs, TMC2130_REG_TSTEP, &val32);
  351. if (val32 & 0x000f0000) return 0xffff;
  352. return val32 & 0xffff;
  353. }
  354. uint16_t tmc2130_rd_MSCNT(uint8_t cs)
  355. {
  356. uint32_t val32 = 0;
  357. tmc2130_rd(cs, TMC2130_REG_MSCNT, &val32);
  358. return val32 & 0x3ff;
  359. }
  360. uint16_t tmc2130_rd_DRV_STATUS(uint8_t cs)
  361. {
  362. uint32_t val32 = 0;
  363. tmc2130_rd(cs, TMC2130_REG_DRV_STATUS, &val32);
  364. return val32;
  365. }
  366. void tmc2130_wr_CHOPCONF(uint8_t cs, 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)
  367. {
  368. uint32_t val = 0;
  369. val |= (uint32_t)(toff & 15);
  370. val |= (uint32_t)(hstrt & 7) << 4;
  371. val |= (uint32_t)(hend & 15) << 7;
  372. val |= (uint32_t)(fd3 & 1) << 11;
  373. val |= (uint32_t)(disfdcc & 1) << 12;
  374. val |= (uint32_t)(rndtf & 1) << 13;
  375. val |= (uint32_t)(chm & 1) << 14;
  376. val |= (uint32_t)(tbl & 3) << 15;
  377. val |= (uint32_t)(vsense & 1) << 17;
  378. val |= (uint32_t)(vhighfs & 1) << 18;
  379. val |= (uint32_t)(vhighchm & 1) << 19;
  380. val |= (uint32_t)(sync & 15) << 20;
  381. val |= (uint32_t)(mres & 15) << 24;
  382. val |= (uint32_t)(intpol & 1) << 28;
  383. val |= (uint32_t)(dedge & 1) << 29;
  384. val |= (uint32_t)(diss2g & 1) << 30;
  385. tmc2130_wr(cs, TMC2130_REG_CHOPCONF, val);
  386. }
  387. //void tmc2130_wr_PWMCONF(uint8_t cs, uint8_t PWMautoScale, uint8_t PWMfreq, uint8_t PWMgrad, uint8_t PWMampl)
  388. void tmc2130_wr_PWMCONF(uint8_t cs, uint8_t pwm_ampl, uint8_t pwm_grad, uint8_t pwm_freq, uint8_t pwm_auto, uint8_t pwm_symm, uint8_t freewheel)
  389. {
  390. uint32_t val = 0;
  391. val |= (uint32_t)(pwm_ampl & 255);
  392. val |= (uint32_t)(pwm_grad & 255) << 8;
  393. val |= (uint32_t)(pwm_freq & 3) << 16;
  394. val |= (uint32_t)(pwm_auto & 1) << 18;
  395. val |= (uint32_t)(pwm_symm & 1) << 19;
  396. val |= (uint32_t)(freewheel & 3) << 20;
  397. tmc2130_wr(cs, TMC2130_REG_PWMCONF, val);
  398. // tmc2130_wr(cs, 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
  399. }
  400. void tmc2130_wr_TPWMTHRS(uint8_t cs, uint32_t val32)
  401. {
  402. tmc2130_wr(cs, TMC2130_REG_TPWMTHRS, val32);
  403. }
  404. void tmc2130_wr_THIGH(uint8_t cs, uint32_t val32)
  405. {
  406. tmc2130_wr(cs, TMC2130_REG_THIGH, val32);
  407. }
  408. #if defined(TMC2130_DEBUG_RD) || defined(TMC2130_DEBUG_WR)
  409. uint8_t tmc2130_axis_by_cs(uint8_t cs)
  410. {
  411. switch (cs)
  412. {
  413. case X_TMC2130_CS: return 0;
  414. case Y_TMC2130_CS: return 1;
  415. case Z_TMC2130_CS: return 2;
  416. case E0_TMC2130_CS: return 3;
  417. }
  418. return -1;
  419. }
  420. #endif //TMC2130_DEBUG
  421. uint8_t tmc2130_mres(uint16_t microstep_resolution)
  422. {
  423. if (microstep_resolution == 256) return 0b0000;
  424. if (microstep_resolution == 128) return 0b0001;
  425. if (microstep_resolution == 64) return 0b0010;
  426. if (microstep_resolution == 32) return 0b0011;
  427. if (microstep_resolution == 16) return 0b0100;
  428. if (microstep_resolution == 8) return 0b0101;
  429. if (microstep_resolution == 4) return 0b0110;
  430. if (microstep_resolution == 2) return 0b0111;
  431. if (microstep_resolution == 1) return 0b1000;
  432. return 0;
  433. }
  434. uint8_t tmc2130_wr(uint8_t cs, uint8_t addr, uint32_t wval)
  435. {
  436. uint8_t stat = tmc2130_txrx(cs, addr | 0x80, wval, 0);
  437. #ifdef TMC2130_DEBUG_WR
  438. MYSERIAL.print("tmc2130_wr(");
  439. MYSERIAL.print((unsigned char)tmc2130_axis_by_cs(cs), DEC);
  440. MYSERIAL.print(", 0x");
  441. MYSERIAL.print((unsigned char)addr, HEX);
  442. MYSERIAL.print(", 0x");
  443. MYSERIAL.print((unsigned long)wval, HEX);
  444. MYSERIAL.print(")=0x");
  445. MYSERIAL.println((unsigned char)stat, HEX);
  446. #endif //TMC2130_DEBUG_WR
  447. return stat;
  448. }
  449. uint8_t tmc2130_rd(uint8_t cs, uint8_t addr, uint32_t* rval)
  450. {
  451. uint32_t val32 = 0;
  452. uint8_t stat = tmc2130_txrx(cs, addr, 0x00000000, &val32);
  453. if (rval != 0) *rval = val32;
  454. #ifdef TMC2130_DEBUG_RD
  455. if (!skip_debug_msg)
  456. {
  457. MYSERIAL.print("tmc2130_rd(");
  458. MYSERIAL.print((unsigned char)tmc2130_axis_by_cs(cs), DEC);
  459. MYSERIAL.print(", 0x");
  460. MYSERIAL.print((unsigned char)addr, HEX);
  461. MYSERIAL.print(", 0x");
  462. MYSERIAL.print((unsigned long)val32, HEX);
  463. MYSERIAL.print(")=0x");
  464. MYSERIAL.println((unsigned char)stat, HEX);
  465. }
  466. skip_debug_msg = false;
  467. #endif //TMC2130_DEBUG_RD
  468. return stat;
  469. }
  470. uint8_t tmc2130_txrx(uint8_t cs, uint8_t addr, uint32_t wval, uint32_t* rval)
  471. {
  472. //datagram1 - request
  473. SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE3));
  474. digitalWrite(cs, LOW);
  475. SPI.transfer(addr); // address
  476. SPI.transfer((wval >> 24) & 0xff); // MSB
  477. SPI.transfer((wval >> 16) & 0xff);
  478. SPI.transfer((wval >> 8) & 0xff);
  479. SPI.transfer(wval & 0xff); // LSB
  480. digitalWrite(cs, HIGH);
  481. SPI.endTransaction();
  482. //datagram2 - response
  483. SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE3));
  484. digitalWrite(cs, LOW);
  485. uint8_t stat = SPI.transfer(0); // status
  486. uint32_t val32 = 0;
  487. val32 = SPI.transfer(0); // MSB
  488. val32 = (val32 << 8) | SPI.transfer(0);
  489. val32 = (val32 << 8) | SPI.transfer(0);
  490. val32 = (val32 << 8) | SPI.transfer(0); // LSB
  491. digitalWrite(cs, HIGH);
  492. SPI.endTransaction();
  493. if (rval != 0) *rval = val32;
  494. return stat;
  495. }
  496. #endif //TMC2130