tmc2130.cpp 15 KB

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