tmc2130.cpp 16 KB

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