tmc2130.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. tmc2130_wr_CHOPCONF(tmc2130_cs[i], 3, 5, 1, 0, 0, 0, 0, 2, 1, 0, 0, 0, mres, TMC2130_INTPOL_Z, 0, 0);
  104. tmc2130_wr(tmc2130_cs[i], TMC2130_REG_IHOLD_IRUN, 0x000f0000 | ((tmc2130_current_r[i] & 0x1f) << 8) | (tmc2130_current_h[i] & 0x1f));
  105. tmc2130_wr(tmc2130_cs[i], TMC2130_REG_TPOWERDOWN, 0x00000000);
  106. tmc2130_wr(tmc2130_cs[i], TMC2130_REG_GCONF, 0x00000000);
  107. }
  108. for (int i = 3; i < 4; i++) // E axis
  109. {
  110. uint8_t mres = tmc2130_mres(TMC2130_USTEPS_E);
  111. tmc2130_wr_CHOPCONF(tmc2130_cs[i], 3, 5, 1, 0, 0, 0, 0, 2, 1, 0, 0, 0, mres, TMC2130_INTPOL_E, 0, 0);
  112. tmc2130_wr(tmc2130_cs[i], TMC2130_REG_IHOLD_IRUN, 0x000f0000 | ((tmc2130_current_r[i] & 0x1f) << 8) | (tmc2130_current_h[i] & 0x1f));
  113. tmc2130_wr(tmc2130_cs[i], TMC2130_REG_TPOWERDOWN, 0x00000000);
  114. tmc2130_wr(tmc2130_cs[i], TMC2130_REG_GCONF, 0x00000000);
  115. }
  116. }
  117. bool tmc2130_update_sg()
  118. {
  119. #if (defined(TMC2130_SG_HOMING) && defined(TMC2130_SG_HOMING_SW))
  120. if ((sg_homing_axis == X_AXIS) || (sg_homing_axis == Y_AXIS))
  121. {
  122. uint8_t cs = tmc2130_cs[sg_homing_axis];
  123. uint16_t tstep = tmc2130_rd_TSTEP(cs);
  124. if (tstep < TMC2130_TCOOLTHRS)
  125. {
  126. 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
  127. sg_homing_delay++;
  128. else
  129. {
  130. uint16_t sg = tmc2130_rd_DRV_STATUS(cs) & 0x3ff;
  131. if (sg==0)
  132. {
  133. tmc2130_axis_stalled[sg_homing_axis] = true;
  134. tmc2130_LastHomingStalled = true;
  135. }
  136. else
  137. tmc2130_axis_stalled[sg_homing_axis] = false;
  138. }
  139. }
  140. else
  141. tmc2130_axis_stalled[sg_homing_axis] = false;
  142. return true;
  143. }
  144. else
  145. {
  146. tmc2130_axis_stalled[X_AXIS] = false;
  147. tmc2130_axis_stalled[Y_AXIS] = false;
  148. }
  149. #endif
  150. return false;
  151. }
  152. void tmc2130_check_overtemp()
  153. {
  154. const static char TMC_OVERTEMP_MSG[] PROGMEM = "TMC DRIVER OVERTEMP ";
  155. uint8_t cs[4] = { X_TMC2130_CS, Y_TMC2130_CS, Z_TMC2130_CS, E0_TMC2130_CS };
  156. static uint32_t checktime = 0;
  157. //drivers_disabled[0] = 1; //TEST
  158. if( millis() - checktime > 1000 )
  159. {
  160. //SERIAL_ECHOLNPGM("drv_status:");
  161. for(int i=0;i<4;i++)
  162. {
  163. uint32_t drv_status = 0;
  164. skip_debug_msg = true;
  165. tmc2130_rd(cs[i], TMC2130_REG_DRV_STATUS, &drv_status);
  166. //MYSERIAL.print(drv_status);
  167. //SERIAL_ECHOPGM(" ");
  168. if (drv_status & ((uint32_t)1<<26))
  169. { // BIT 26 - over temp prewarning ~120C (+-20C)
  170. SERIAL_ERRORRPGM(TMC_OVERTEMP_MSG);
  171. SERIAL_ECHOLN(i);
  172. for(int i=0; i < 4; i++)
  173. tmc2130_wr(tmc2130_cs[i], TMC2130_REG_CHOPCONF, 0x00010000);
  174. kill(TMC_OVERTEMP_MSG);
  175. }
  176. }
  177. //SERIAL_ECHOLNPGM("");
  178. checktime = millis();
  179. }
  180. }
  181. void tmc2130_home_enter(uint8_t axis)
  182. {
  183. MYSERIAL.print("tmc2130_home_enter ");
  184. MYSERIAL.println((int)axis);
  185. #ifdef TMC2130_SG_HOMING
  186. uint8_t cs = tmc2130_cs[axis];
  187. sg_homing_axis = axis;
  188. sg_homing_delay = 0;
  189. tmc2130_axis_stalled[X_AXIS] = false;
  190. tmc2130_axis_stalled[Y_AXIS] = false;
  191. //Configuration to spreadCycle
  192. tmc2130_wr(cs, TMC2130_REG_GCONF, 0x00000000);
  193. tmc2130_wr(cs, TMC2130_REG_COOLCONF, ((axis == X_AXIS)?sg_thrs_x:sg_thrs_y) << 16);
  194. tmc2130_wr(cs, TMC2130_REG_TCOOLTHRS, TMC2130_TCOOLTHRS);
  195. #ifndef TMC2130_SG_HOMING_SW
  196. tmc2130_wr(cs, TMC2130_REG_GCONF, 0x00000080); //stallguard output to DIAG0
  197. #endif
  198. #endif
  199. }
  200. void tmc2130_home_exit()
  201. {
  202. MYSERIAL.println("tmc2130_home_exit ");
  203. MYSERIAL.println((int)sg_homing_axis);
  204. #ifdef TMC2130_SG_HOMING
  205. if ((sg_homing_axis == X_AXIS) || (sg_homing_axis == Y_AXIS))
  206. {
  207. if (tmc2130_mode == TMC2130_MODE_SILENT)
  208. tmc2130_wr(tmc2130_cs[sg_homing_axis], TMC2130_REG_GCONF, 0x00000004); // Configuration back to stealthChop
  209. else
  210. tmc2130_wr(tmc2130_cs[sg_homing_axis], TMC2130_REG_GCONF, 0x00000000);
  211. sg_homing_axis = 0xff;
  212. }
  213. #endif
  214. }
  215. uint8_t tmc2130_didLastHomingStall()
  216. {
  217. uint8_t ret = tmc2130_LastHomingStalled;
  218. tmc2130_LastHomingStalled = false;
  219. return ret;
  220. }
  221. void tmc2130_set_current_h(uint8_t axis, uint8_t current)
  222. {
  223. MYSERIAL.print("tmc2130_set_current_h ");
  224. MYSERIAL.print((int)axis);
  225. MYSERIAL.print(" ");
  226. MYSERIAL.println((int)current);
  227. // if (current > 15) current = 15; //current>15 is unsafe
  228. tmc2130_current_h[axis] = current;
  229. tmc2130_wr(tmc2130_cs[axis], TMC2130_REG_IHOLD_IRUN, 0x000f0000 | ((tmc2130_current_r[axis] & 0x1f) << 8) | (tmc2130_current_h[axis] & 0x1f));
  230. }
  231. void tmc2130_set_current_r(uint8_t axis, uint8_t current)
  232. {
  233. MYSERIAL.print("tmc2130_set_current_r ");
  234. MYSERIAL.print((int)axis);
  235. MYSERIAL.print(" ");
  236. MYSERIAL.println((int)current);
  237. // if (current > 15) current = 15; //current>15 is unsafe
  238. tmc2130_current_r[axis] = current;
  239. tmc2130_wr(tmc2130_cs[axis], TMC2130_REG_IHOLD_IRUN, 0x000f0000 | ((tmc2130_current_r[axis] & 0x1f) << 8) | (tmc2130_current_h[axis] & 0x1f));
  240. }
  241. void tmc2130_print_currents()
  242. {
  243. MYSERIAL.println("tmc2130_print_currents");
  244. MYSERIAL.println("\tH\rR");
  245. MYSERIAL.print("X\t");
  246. MYSERIAL.print((int)tmc2130_current_h[0]);
  247. MYSERIAL.print("\t");
  248. MYSERIAL.println((int)tmc2130_current_r[0]);
  249. MYSERIAL.print("Y\t");
  250. MYSERIAL.print((int)tmc2130_current_h[1]);
  251. MYSERIAL.print("\t");
  252. MYSERIAL.println((int)tmc2130_current_r[1]);
  253. MYSERIAL.print("Z\t");
  254. MYSERIAL.print((int)tmc2130_current_h[2]);
  255. MYSERIAL.print("\t");
  256. MYSERIAL.println((int)tmc2130_current_r[2]);
  257. MYSERIAL.print("E\t");
  258. MYSERIAL.print((int)tmc2130_current_h[3]);
  259. MYSERIAL.print("\t");
  260. MYSERIAL.println((int)tmc2130_current_r[3]);
  261. }
  262. void tmc2130_set_pwm_ampl(uint8_t axis, uint8_t pwm_ampl)
  263. {
  264. MYSERIAL.print("tmc2130_set_pwm_ampl ");
  265. MYSERIAL.print((int)axis);
  266. MYSERIAL.print(" ");
  267. MYSERIAL.println((int)pwm_ampl);
  268. tmc2130_pwm_ampl[axis] = pwm_ampl;
  269. if (((axis == 0) || (axis == 1)) && (tmc2130_mode == TMC2130_MODE_SILENT))
  270. tmc2130_wr_PWMCONF(tmc2130_cs[axis], tmc2130_pwm_ampl[axis], tmc2130_pwm_grad[axis], tmc2130_pwm_freq[axis], tmc2130_pwm_auto[axis], 0, 0);
  271. }
  272. void tmc2130_set_pwm_grad(uint8_t axis, uint8_t pwm_grad)
  273. {
  274. MYSERIAL.print("tmc2130_set_pwm_grad ");
  275. MYSERIAL.print((int)axis);
  276. MYSERIAL.print(" ");
  277. MYSERIAL.println((int)pwm_grad);
  278. tmc2130_pwm_grad[axis] = pwm_grad;
  279. if (((axis == 0) || (axis == 1)) && (tmc2130_mode == TMC2130_MODE_SILENT))
  280. tmc2130_wr_PWMCONF(tmc2130_cs[axis], tmc2130_pwm_ampl[axis], tmc2130_pwm_grad[axis], tmc2130_pwm_freq[axis], tmc2130_pwm_auto[axis], 0, 0);
  281. }
  282. uint16_t tmc2130_rd_TSTEP(uint8_t cs)
  283. {
  284. uint32_t val32 = 0;
  285. tmc2130_rd(cs, TMC2130_REG_TSTEP, &val32);
  286. if (val32 & 0x000f0000) return 0xffff;
  287. return val32 & 0xffff;
  288. }
  289. uint16_t tmc2130_rd_DRV_STATUS(uint8_t cs)
  290. {
  291. uint32_t val32 = 0;
  292. tmc2130_rd(cs, TMC2130_REG_DRV_STATUS, &val32);
  293. return val32;
  294. }
  295. 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)
  296. {
  297. uint32_t val = 0;
  298. val |= (uint32_t)(toff & 15);
  299. val |= (uint32_t)(hstrt & 7) << 4;
  300. val |= (uint32_t)(hend & 15) << 7;
  301. val |= (uint32_t)(fd3 & 1) << 11;
  302. val |= (uint32_t)(disfdcc & 1) << 12;
  303. val |= (uint32_t)(rndtf & 1) << 13;
  304. val |= (uint32_t)(chm & 1) << 14;
  305. val |= (uint32_t)(tbl & 3) << 15;
  306. val |= (uint32_t)(vsense & 1) << 17;
  307. val |= (uint32_t)(vhighfs & 1) << 18;
  308. val |= (uint32_t)(vhighchm & 1) << 19;
  309. val |= (uint32_t)(sync & 15) << 20;
  310. val |= (uint32_t)(mres & 15) << 24;
  311. val |= (uint32_t)(intpol & 1) << 28;
  312. val |= (uint32_t)(dedge & 1) << 29;
  313. val |= (uint32_t)(diss2g & 1) << 30;
  314. tmc2130_wr(cs, TMC2130_REG_CHOPCONF, val);
  315. }
  316. //void tmc2130_wr_PWMCONF(uint8_t cs, uint8_t PWMautoScale, uint8_t PWMfreq, uint8_t PWMgrad, uint8_t PWMampl)
  317. 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)
  318. {
  319. uint32_t val = 0;
  320. val |= (uint32_t)(pwm_ampl & 255);
  321. val |= (uint32_t)(pwm_grad & 255) << 8;
  322. val |= (uint32_t)(pwm_freq & 3) << 16;
  323. val |= (uint32_t)(pwm_auto & 1) << 18;
  324. val |= (uint32_t)(pwm_symm & 1) << 19;
  325. val |= (uint32_t)(freewheel & 3) << 20;
  326. tmc2130_wr(cs, TMC2130_REG_PWMCONF, val);
  327. // 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
  328. }
  329. void tmc2130_wr_TPWMTHRS(uint8_t cs, uint32_t val32)
  330. {
  331. tmc2130_wr(cs, TMC2130_REG_TPWMTHRS, val32);
  332. }
  333. void tmc2130_wr_THIGH(uint8_t cs, uint32_t val32)
  334. {
  335. tmc2130_wr(cs, TMC2130_REG_THIGH, val32);
  336. }
  337. uint8_t tmc2130_axis_by_cs(uint8_t cs)
  338. {
  339. switch (cs)
  340. {
  341. case X_TMC2130_CS: return 0;
  342. case Y_TMC2130_CS: return 1;
  343. case Z_TMC2130_CS: return 2;
  344. case E0_TMC2130_CS: return 3;
  345. }
  346. return -1;
  347. }
  348. uint8_t tmc2130_mres(uint16_t microstep_resolution)
  349. {
  350. if (microstep_resolution == 256) return 0b0000;
  351. if (microstep_resolution == 128) return 0b0001;
  352. if (microstep_resolution == 64) return 0b0010;
  353. if (microstep_resolution == 32) return 0b0011;
  354. if (microstep_resolution == 16) return 0b0100;
  355. if (microstep_resolution == 8) return 0b0101;
  356. if (microstep_resolution == 4) return 0b0110;
  357. if (microstep_resolution == 2) return 0b0111;
  358. if (microstep_resolution == 1) return 0b1000;
  359. return 0;
  360. }
  361. uint8_t tmc2130_wr(uint8_t cs, uint8_t addr, uint32_t wval)
  362. {
  363. uint8_t stat = tmc2130_txrx(cs, addr | 0x80, wval, 0);
  364. #ifdef TMC2130_DEBUG_WR
  365. MYSERIAL.print("tmc2130_wr(");
  366. MYSERIAL.print((unsigned char)tmc2130_axis_by_cs(cs), DEC);
  367. MYSERIAL.print(", 0x");
  368. MYSERIAL.print((unsigned char)addr, HEX);
  369. MYSERIAL.print(", 0x");
  370. MYSERIAL.print((unsigned long)wval, HEX);
  371. MYSERIAL.print(")=0x");
  372. MYSERIAL.println((unsigned char)stat, HEX);
  373. #endif //TMC2130_DEBUG_WR
  374. return stat;
  375. }
  376. uint8_t tmc2130_rd(uint8_t cs, uint8_t addr, uint32_t* rval)
  377. {
  378. uint32_t val32 = 0;
  379. uint8_t stat = tmc2130_txrx(cs, addr, 0x00000000, &val32);
  380. if (rval != 0) *rval = val32;
  381. #ifdef TMC2130_DEBUG_RD
  382. if (!skip_debug_msg)
  383. {
  384. MYSERIAL.print("tmc2130_rd(");
  385. MYSERIAL.print((unsigned char)tmc2130_axis_by_cs(cs), DEC);
  386. MYSERIAL.print(", 0x");
  387. MYSERIAL.print((unsigned char)addr, HEX);
  388. MYSERIAL.print(", 0x");
  389. MYSERIAL.print((unsigned long)val32, HEX);
  390. MYSERIAL.print(")=0x");
  391. MYSERIAL.println((unsigned char)stat, HEX);
  392. }
  393. skip_debug_msg = false;
  394. #endif //TMC2130_DEBUG_RD
  395. return stat;
  396. }
  397. uint8_t tmc2130_txrx(uint8_t cs, uint8_t addr, uint32_t wval, uint32_t* rval)
  398. {
  399. //datagram1 - request
  400. SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE3));
  401. digitalWrite(cs, LOW);
  402. SPI.transfer(addr); // address
  403. SPI.transfer((wval >> 24) & 0xff); // MSB
  404. SPI.transfer((wval >> 16) & 0xff);
  405. SPI.transfer((wval >> 8) & 0xff);
  406. SPI.transfer(wval & 0xff); // LSB
  407. digitalWrite(cs, HIGH);
  408. SPI.endTransaction();
  409. //datagram2 - response
  410. SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE3));
  411. digitalWrite(cs, LOW);
  412. uint8_t stat = SPI.transfer(0); // status
  413. uint32_t val32 = 0;
  414. val32 = SPI.transfer(0); // MSB
  415. val32 = (val32 << 8) | SPI.transfer(0);
  416. val32 = (val32 << 8) | SPI.transfer(0);
  417. val32 = (val32 << 8) | SPI.transfer(0); // LSB
  418. digitalWrite(cs, HIGH);
  419. SPI.endTransaction();
  420. if (rval != 0) *rval = val32;
  421. return stat;
  422. }
  423. #endif //HAVE_TMC2130_DRIVERS