You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Trinamic_TMC2130.cpp 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. /**************************************************************************/
  2. /*!
  3. @file Trinamic_TMC2130.cpp
  4. @author Moritz Walter
  5. @license GPLv3 (see license.txt)
  6. SPI configuration tool for Trinamic TMC2130 Motor Drivers
  7. @section HISTORY
  8. v0.1 - it works
  9. */
  10. /**************************************************************************/
  11. #if ARDUINO >= 100
  12. #include "Arduino.h"
  13. #else
  14. #include "WProgram.h"
  15. #endif
  16. #include <SPI.h>
  17. #include "Trinamic_TMC2130.h"
  18. #include "Trinamic_TMC2130_registers.h"
  19. Trinamic_TMC2130::Trinamic_TMC2130(uint8_t csPin)
  20. {
  21. _csPin=csPin;
  22. _status=0;
  23. _debug="";
  24. }
  25. // initialize the driver with its CS/SS pin
  26. void Trinamic_TMC2130::init() {
  27. pinMode(_csPin, OUTPUT);
  28. digitalWrite(_csPin, HIGH);
  29. init_SPI();
  30. read_STAT();
  31. }
  32. // initialize SPI
  33. void Trinamic_TMC2130::init_SPI() {
  34. SPI.setDataMode(TMC_SPI_DATA_MODE);
  35. SPI.setBitOrder(TMC_SPI_BIT_ORDER);
  36. SPI.setClockDivider(TMC_SPI_CLOCK_DIVIDER);
  37. SPI.begin();
  38. }
  39. // read status
  40. uint8_t Trinamic_TMC2130::read_STAT()
  41. {
  42. init_SPI();
  43. digitalWrite(_csPin, LOW);
  44. // read address
  45. _status = SPI.transfer(0x00);
  46. // flush 4 bytes
  47. for(int i=0;i<4;i++){
  48. SPI.transfer(0x00);
  49. }
  50. digitalWrite(_csPin, HIGH);
  51. return _status;
  52. }
  53. // read a register
  54. uint8_t Trinamic_TMC2130::read_REG(uint8_t address, uint32_t *data)
  55. {
  56. init_SPI();
  57. digitalWrite(_csPin, LOW);
  58. // read address
  59. _status = SPI.transfer(address&~TMC_WRITE);
  60. // flush 4 bytes
  61. for(int i=0;i<4;i++){
  62. SPI.transfer(0x00);
  63. }
  64. digitalWrite(_csPin, HIGH);
  65. // restart transmission
  66. digitalWrite(_csPin, LOW);
  67. // read address
  68. _status = SPI.transfer(address&~TMC_WRITE);
  69. // retrieve data
  70. *data = SPI.transfer(0x00)&0xFF;
  71. *data <<=8;
  72. *data |= SPI.transfer(0x00)&0xFF;
  73. *data <<=8;
  74. *data |= SPI.transfer(0x00)&0xFF;
  75. *data <<=8;
  76. *data |= SPI.transfer(0x00)&0xFF;
  77. digitalWrite(_csPin, HIGH);
  78. return _status;
  79. }
  80. // write to a register
  81. uint8_t Trinamic_TMC2130::write_REG(uint8_t address, uint32_t data)
  82. {
  83. digitalWrite(_csPin, LOW);
  84. // write address
  85. _status = SPI.transfer(address|TMC_WRITE);
  86. // write data
  87. SPI.transfer((data>>24UL)&0xFF);
  88. SPI.transfer((data>>16UL)&0xFF);
  89. SPI.transfer((data>> 8UL)&0xFF);
  90. SPI.transfer((data>> 0UL)&0xFF);
  91. digitalWrite(_csPin, HIGH);
  92. return _status;
  93. }
  94. // alter a register using a bitmask
  95. uint8_t Trinamic_TMC2130::alter_REG(uint8_t address, uint32_t data, uint32_t mask)
  96. {
  97. uint32_t oldData, newData;
  98. read_REG( address, &oldData );
  99. newData = ( oldData&~mask ) | ( data&mask );
  100. write_REG( address, newData );
  101. return _status;
  102. }
  103. // set single bits in the GCONF register
  104. uint8_t Trinamic_TMC2130::set_GCONF(uint8_t position, uint8_t value)
  105. {
  106. alter_REG(TMC_REG_GCONF, uint32_t(value)<<position, 0x1UL<<position);
  107. return _status;
  108. }
  109. // set single bits or values in the chopconf register (constraining masks are applied if necessary)
  110. uint8_t Trinamic_TMC2130::set_CHOPCONF(uint8_t position, uint8_t value)
  111. {
  112. alter_REG(TMC_REG_CHOPCONF, uint32_t(value)<<position, TMC_CHOPCONF_MASKS[position]<<position);
  113. return _status;
  114. }
  115. //////////
  116. // GCONF
  117. //////////
  118. uint8_t Trinamic_TMC2130::set_I_scale_analog(uint8_t value)
  119. {
  120. set_GCONF(TMC_GCONF_I_SCALE_ANALOG, value);
  121. return _status;
  122. }
  123. uint8_t Trinamic_TMC2130::set_internal_Rsense(uint8_t value)
  124. {
  125. set_GCONF(TMC_GCONF_INTERNAL_RSENSE, value);
  126. return _status;
  127. }
  128. uint8_t Trinamic_TMC2130::set_en_pwm_mode(uint8_t value)
  129. {
  130. set_GCONF(TMC_GCONF_EN_PWM_MODE, value);
  131. return _status;
  132. }
  133. uint8_t Trinamic_TMC2130::set_enc_commutation(uint8_t value)
  134. {
  135. set_GCONF(TMC_GCONF_ENC_COMMUTATION, value);
  136. return _status;
  137. }
  138. uint8_t Trinamic_TMC2130::set_shaft(uint8_t value)
  139. {
  140. set_GCONF(TMC_GCONF_SHAFT, value);
  141. return _status;
  142. }
  143. uint8_t Trinamic_TMC2130::set_diag0_error(uint8_t value)
  144. {
  145. set_GCONF(TMC_GCONF_DIAG0_ERROR, value);
  146. return _status;
  147. }
  148. uint8_t Trinamic_TMC2130::set_diag0_otpw(uint8_t value)
  149. {
  150. set_GCONF(TMC_GCONF_DIAG0_OTPW, value);
  151. return _status;
  152. }
  153. uint8_t Trinamic_TMC2130::set_diag0_stall(uint8_t value)
  154. {
  155. set_GCONF(TMC_GCONF_DIAG0_STALL, value);
  156. return _status;
  157. }
  158. uint8_t Trinamic_TMC2130::set_diag1_stall(uint8_t value)
  159. {
  160. set_GCONF(TMC_GCONF_DIAG1_STALL, value);
  161. return _status;
  162. }
  163. uint8_t Trinamic_TMC2130::set_diag1_index(uint8_t value)
  164. {
  165. set_GCONF(TMC_GCONF_DIAG1_INDEX, value);
  166. return _status;
  167. }
  168. uint8_t Trinamic_TMC2130::set_diag1_onstate(uint8_t value)
  169. {
  170. set_GCONF(TMC_GCONF_DIAG1_ONSTATE, value);
  171. return _status;
  172. }
  173. uint8_t Trinamic_TMC2130::set_diag1_steps_skipped(uint8_t value)
  174. {
  175. set_GCONF(TMC_GCONF_DIAG1_STEPS_SKIPPED, value);
  176. return _status;
  177. }
  178. uint8_t Trinamic_TMC2130::set_diag0_int_pushpull(uint8_t value)
  179. {
  180. set_GCONF(TMC_GCONF_DIAG0_INT_PUSHPULL, value);
  181. return _status;
  182. }
  183. uint8_t Trinamic_TMC2130::set_diag1_int_pushpull(uint8_t value)
  184. {
  185. set_GCONF(TMC_GCONF_DIAG1_INT_PUSHPULL, value);
  186. return _status;
  187. }
  188. uint8_t Trinamic_TMC2130::set_small_hysteresis(uint8_t value)
  189. {
  190. set_GCONF(TMC_GCONF_SMALL_HYSTERESIS, value);
  191. return _status;
  192. }
  193. uint8_t Trinamic_TMC2130::set_stop_enable(uint8_t value)
  194. {
  195. set_GCONF(TMC_GCONF_STOP_ENABLE, value);
  196. return _status;
  197. }
  198. uint8_t Trinamic_TMC2130::set_direct_mode(uint8_t value)
  199. {
  200. set_GCONF(TMC_GCONF_DIRECT_MODE, value);
  201. return _status;
  202. }
  203. /*
  204. uint8_t Trinamic_TMC2130::set_test_mode(uint8_t value)
  205. {
  206. set_GCONF(TMC_GCONF_TEST_MODE, value);
  207. return _status;
  208. }
  209. */
  210. //////////
  211. // IHOLD_IRUN
  212. //////////
  213. uint8_t Trinamic_TMC2130::set_IHOLD_IRUN(uint8_t ihold, uint8_t irun, uint8_t iholddelay)
  214. {
  215. uint32_t data;
  216. // adding ihold
  217. data = (( uint32_t(ihold)&TMC_IHOLD_MASK )<<TMC_IHOLD );
  218. // adding irun
  219. data |= (( uint32_t(irun)&TMC_IRUN_MASK )<<TMC_IRUN );
  220. // adding iholddelay
  221. data |= (( uint32_t(iholddelay)&TMC_IHOLDDELAY_MASK )<<TMC_IHOLDDELAY );
  222. // writing data
  223. write_REG(TMC_REG_IHOLD_IRUN, data);
  224. return _status;
  225. }
  226. //////////
  227. // TPOWERDOWN
  228. //////////
  229. uint8_t Trinamic_TMC2130::set_TPOWERDOWN(uint8_t value)
  230. {
  231. uint32_t data;
  232. data = value & TMC_TPOWERDOWN_MASK;
  233. write_REG(TMC_REG_TPOWERDOWN, data);
  234. return _status;
  235. }
  236. //////////
  237. // TSTEP
  238. //////////
  239. uint32_t Trinamic_TMC2130::get_TSTEP()
  240. {
  241. uint32_t data;
  242. read_REG(TMC_REG_TPOWERDOWN, &data);
  243. data &= TMC_TSTEP_MASK;
  244. return data;
  245. }
  246. //////////
  247. // TPWMTHRS
  248. //////////
  249. uint8_t Trinamic_TMC2130::set_TPWMTHRS(uint32_t value)
  250. {
  251. uint32_t data;
  252. data = value & TMC_TPWMTHRS_MASK;
  253. write_REG(TMC_REG_TPOWERDOWN, data);
  254. return _status;
  255. }
  256. //////////
  257. // TCOOLTHRS
  258. //////////
  259. uint8_t Trinamic_TMC2130::set_TCOOLTHRS(uint32_t value)
  260. {
  261. uint32_t data;
  262. data = value & TMC_TCOOLTHRS_MASK;
  263. write_REG(TMC_REG_TCOOLTHRS, data);
  264. return _status;
  265. }
  266. //////////
  267. // THIGH
  268. //////////
  269. uint8_t Trinamic_TMC2130::set_THIGH(uint32_t value)
  270. {
  271. uint32_t data;
  272. data = value & TMC_THIGH_MASK;
  273. write_REG(TMC_REG_THIGH, data);
  274. return _status;
  275. }
  276. //////////
  277. // XDIRECT
  278. //////////
  279. uint8_t Trinamic_TMC2130::set_XDIRECT(int16_t coil_a, int16_t coil_b)
  280. {
  281. uint32_t data;
  282. data = 0x0;
  283. data |= ( coil_b & TMC_XDIRECT_COIL_B_MASK );
  284. data = data << TMC_XDIRECT_COIL_B;
  285. data |= ( coil_a & TMC_XDIRECT_COIL_A_MASK );
  286. data &= TMC_XDIRECT_MASK;
  287. write_REG(TMC_REG_XDIRECT, data);
  288. return _status;
  289. }
  290. uint8_t Trinamic_TMC2130::set_XDIRECT(uint32_t value)
  291. {
  292. uint32_t data;
  293. data = value;// & TMC_XDIRECT_MASK;
  294. write_REG(TMC_REG_XDIRECT, data);
  295. return _status;
  296. }
  297. int32_t Trinamic_TMC2130::get_XDIRECT()
  298. {
  299. uint32_t data;
  300. read_REG(TMC_REG_XDIRECT, &data);
  301. data &= TMC_XDIRECT_MASK;
  302. return data;
  303. }
  304. //////////
  305. // VDCMIN
  306. //////////
  307. uint8_t Trinamic_TMC2130::set_VDCMIN(int32_t value)
  308. {
  309. int32_t data;
  310. data = value & TMC_VDCMIN_MASK;
  311. write_REG(TMC_REG_VDCMIN, data);
  312. return _status;
  313. }
  314. //////////
  315. // MSLUT
  316. //////////
  317. uint8_t Trinamic_TMC2130::set_MSLUT0(uint32_t value)
  318. {
  319. write_REG(TMC_REG_MSLUT0, value);
  320. return _status;
  321. }
  322. uint8_t Trinamic_TMC2130::set_MSLUT1(uint32_t value)
  323. {
  324. write_REG(TMC_REG_MSLUT1, value);
  325. return _status;
  326. }
  327. uint8_t Trinamic_TMC2130::set_MSLUT2(uint32_t value)
  328. {
  329. write_REG(TMC_REG_MSLUT2, value);
  330. return _status;
  331. }
  332. uint8_t Trinamic_TMC2130::set_MSLUT3(uint32_t value)
  333. {
  334. write_REG(TMC_REG_MSLUT3, value);
  335. return _status;
  336. }
  337. uint8_t Trinamic_TMC2130::set_MSLUT4(uint32_t value)
  338. {
  339. write_REG(TMC_REG_MSLUT4, value);
  340. return _status;
  341. }
  342. uint8_t Trinamic_TMC2130::set_MSLUT5(uint32_t value)
  343. {
  344. write_REG(TMC_REG_MSLUT5, value);
  345. return _status;
  346. }
  347. uint8_t Trinamic_TMC2130::set_MSLUT6(uint32_t value)
  348. {
  349. write_REG(TMC_REG_MSLUT6, value);
  350. return _status;
  351. }
  352. uint8_t Trinamic_TMC2130::set_MSLUT7(uint32_t value)
  353. {
  354. write_REG(TMC_REG_MSLUT7, value);
  355. return _status;
  356. }
  357. //////////
  358. // MSLUTSEL
  359. //////////
  360. uint8_t Trinamic_TMC2130::set_MSLUTSEL(uint32_t value)
  361. {
  362. write_REG(TMC_REG_MSLUTSEL, value);
  363. return _status;
  364. }
  365. //////////
  366. // MSLUTSTART
  367. //////////
  368. uint8_t Trinamic_TMC2130::set_MSLUTSTART(uint8_t start_sin, uint8_t start_sin90)
  369. {
  370. uint32_t data;
  371. data = ( uint32_t(start_sin90) & TMC_MSLUTSTART_START_SIN90_MASK );
  372. data = data<<TMC_MSLUTSTART_START_SIN90;
  373. data |= ( uint32_t(start_sin) & TMC_MSLUTSTART_START_SIN_MASK );
  374. data &= TMC_MSLUTSTART_MASK;
  375. write_REG(TMC_REG_MSLUTSTART, data);
  376. return _status;
  377. }
  378. //////////
  379. // MSCNT
  380. //////////
  381. uint16_t Trinamic_TMC2130::get_MSCNT()
  382. {
  383. uint32_t data;
  384. read_REG(TMC_REG_MSCNT, &data);
  385. data &= TMC_MSCNT_MASK;
  386. data = uint16_t(data);
  387. return data;
  388. }
  389. //////////
  390. // MSCURACT
  391. //////////
  392. int32_t Trinamic_TMC2130::get_MSCURACT()
  393. {
  394. uint32_t data;
  395. read_REG(TMC_REG_MSCURACT, &data);
  396. data &= TMC_MSCURACT_MASK;
  397. return data;
  398. }
  399. //////////
  400. // CHOPCONF
  401. //////////
  402. uint8_t Trinamic_TMC2130::set_diss2g(uint8_t value)
  403. {
  404. set_CHOPCONF(TMC_CHOPCONF_DISS2G, value);
  405. return _status;
  406. }
  407. uint8_t Trinamic_TMC2130::set_dedge(uint8_t value)
  408. {
  409. set_CHOPCONF(TMC_CHOPCONF_DEDGE, value);
  410. return _status;
  411. }
  412. uint8_t Trinamic_TMC2130::set_intpol(uint8_t value)
  413. {
  414. set_CHOPCONF(TMC_CHOPCONF_INTPOL, value);
  415. return _status;
  416. }
  417. // setting the microstep resolution
  418. uint8_t Trinamic_TMC2130::set_mres(uint16_t value)
  419. {
  420. uint8_t data = 0;
  421. switch(value){
  422. case 1:
  423. data = 8;
  424. break;
  425. case 2:
  426. data = 7;
  427. break;
  428. case 4:
  429. data = 6;
  430. break;
  431. case 8:
  432. data = 5;
  433. break;
  434. case 16:
  435. data = 4;
  436. break;
  437. case 32:
  438. data = 3;
  439. break;
  440. case 64:
  441. data = 2;
  442. break;
  443. case 128:
  444. data = 1;
  445. break;
  446. }
  447. set_CHOPCONF(TMC_CHOPCONF_MRES, data);
  448. return _status;
  449. }
  450. uint8_t Trinamic_TMC2130::set_sync(uint8_t value)
  451. {
  452. set_CHOPCONF(TMC_CHOPCONF_SYNC, value);
  453. return _status;
  454. }
  455. uint8_t Trinamic_TMC2130::set_vhighchm(uint8_t value)
  456. {
  457. set_CHOPCONF(TMC_CHOPCONF_VHIGHCHM, value);
  458. return _status;
  459. }
  460. uint8_t Trinamic_TMC2130::set_vhighfs(uint8_t value)
  461. {
  462. set_CHOPCONF(TMC_CHOPCONF_VHIGHFS, value);
  463. return _status;
  464. }
  465. uint8_t Trinamic_TMC2130::set_vsense(uint8_t value)
  466. {
  467. set_CHOPCONF(TMC_CHOPCONF_VSENSE, value);
  468. return _status;
  469. }
  470. uint8_t Trinamic_TMC2130::set_tbl(uint8_t value)
  471. {
  472. set_CHOPCONF(TMC_CHOPCONF_TBL, value);
  473. return _status;
  474. }
  475. uint8_t Trinamic_TMC2130::set_chm(uint8_t value)
  476. {
  477. set_CHOPCONF(TMC_CHOPCONF_CHM, value);
  478. return _status;
  479. }
  480. uint8_t Trinamic_TMC2130::set_rndtf(uint8_t value)
  481. {
  482. set_CHOPCONF(TMC_CHOPCONF_RNDTF, value);
  483. return _status;
  484. }
  485. uint8_t Trinamic_TMC2130::set_disfdcc(uint8_t value)
  486. {
  487. set_CHOPCONF(TMC_CHOPCONF_DISFDCC, value);
  488. return _status;
  489. }
  490. uint8_t Trinamic_TMC2130::set_fd(uint8_t value)
  491. {
  492. set_CHOPCONF(TMC_CHOPCONF_FD, value);
  493. return _status;
  494. }
  495. uint8_t Trinamic_TMC2130::set_hend(uint8_t value)
  496. {
  497. set_CHOPCONF(TMC_CHOPCONF_HEND, value);
  498. return _status;
  499. }
  500. uint8_t Trinamic_TMC2130::set_hstrt(uint8_t value)
  501. {
  502. set_CHOPCONF(TMC_CHOPCONF_HSTRT, value);
  503. return _status;
  504. }
  505. uint8_t Trinamic_TMC2130::set_toff(uint8_t value)
  506. {
  507. set_CHOPCONF(TMC_CHOPCONF_TOFF, value);
  508. return _status;
  509. }
  510. //////////
  511. // COOLCONF
  512. //////////
  513. // alter coolconf
  514. uint8_t Trinamic_TMC2130::alter_COOLCONF(uint32_t data, uint32_t mask)
  515. {
  516. uint32_t newData;
  517. _coolconf = ( _coolconf & ~mask ) | ( data & mask );
  518. write_REG( TMC_REG_COOLCONF, _coolconf );
  519. return _status;
  520. }
  521. // set coolconf
  522. uint8_t Trinamic_TMC2130::set_COOLCONF(uint8_t position, uint8_t value)
  523. {
  524. alter_COOLCONF( uint32_t(value)<<position, TMC_CHOPCONF_MASKS[position]<<position);
  525. return _status;
  526. }
  527. uint8_t Trinamic_TMC2130::set_sfilt(uint8_t value)
  528. {
  529. //set_CHOPCONF(TMC_COOLCONF_SFILT, value);
  530. set_COOLCONF(TMC_COOLCONF_SFILT, value);
  531. return _status;
  532. }
  533. uint8_t Trinamic_TMC2130::set_sgt(uint8_t value)
  534. {
  535. //set_CHOPCONF(TMC_COOLCONF_SGT, value);
  536. set_COOLCONF(TMC_COOLCONF_SGT, value);
  537. return _status;
  538. }
  539. uint8_t Trinamic_TMC2130::set_seimin(uint8_t value)
  540. {
  541. set_COOLCONF(TMC_COOLCONF_SEIMIN, value);
  542. return _status;
  543. }
  544. uint8_t Trinamic_TMC2130::set_sedn(uint8_t value)
  545. {
  546. set_COOLCONF(TMC_COOLCONF_SEDN, value);
  547. return _status;
  548. }
  549. uint8_t Trinamic_TMC2130::set_semax(uint8_t value)
  550. {
  551. set_COOLCONF(TMC_COOLCONF_SEMAX, value);
  552. return _status;
  553. }
  554. uint8_t Trinamic_TMC2130::set_seup(uint8_t value)
  555. {
  556. set_COOLCONF(TMC_COOLCONF_SEUP, value);
  557. return _status;
  558. }
  559. uint8_t Trinamic_TMC2130::set_semin(uint8_t value)
  560. {
  561. set_COOLCONF(TMC_COOLCONF_SEMIN, value);
  562. return _status;
  563. }
  564. //////////
  565. // DCCTRL
  566. //////////
  567. uint8_t Trinamic_TMC2130::set_DCCTRL(uint16_t dc_time, uint16_t dc_sg)
  568. {
  569. uint32_t data;
  570. data = ( uint32_t(dc_sg) & TMC_DCCTRL_DC_SG_MASK );
  571. data = data<<TMC_DCCTRL_DC_SG;
  572. data |= ( uint32_t(dc_time) & TMC_DCCTRL_DC_TIME_MASK );
  573. data &= TMC_DCCTRL_MASK;
  574. write_REG(TMC_REG_DCCTRL, data);
  575. return _status;
  576. }
  577. //////////
  578. // PWMCONF
  579. //////////
  580. // alter pwmconf
  581. uint8_t Trinamic_TMC2130::alter_PWMCONF(uint32_t data, uint32_t mask)
  582. {
  583. uint32_t newData;
  584. _pwmconf = ( _pwmconf & ~mask ) | ( data & mask );
  585. write_REG( TMC_REG_PWMCONF, _pwmconf );
  586. return _status;
  587. }
  588. // set pwmconf
  589. uint8_t Trinamic_TMC2130::set_PWMCONF(uint8_t position, uint8_t value)
  590. {
  591. alter_PWMCONF( uint32_t(value)<<position, TMC_CHOPCONF_MASKS[position]<<position );
  592. return _status;
  593. }
  594. uint8_t Trinamic_TMC2130::set_freewheel(uint8_t value)
  595. {
  596. set_PWMCONF(TMC_PWMCONF_FREEWHEEL, value);
  597. return _status;
  598. }
  599. uint8_t Trinamic_TMC2130::set_pwm_symmetric(uint8_t value)
  600. {
  601. set_PWMCONF(TMC_PWMCONF_PWM_SYMMETRIC, value);
  602. return _status;
  603. }
  604. uint8_t Trinamic_TMC2130::set_pwm_autoscale(uint8_t value)
  605. {
  606. set_PWMCONF(TMC_PWMCONF_PWM_AUTOSCALE, value);
  607. return _status;
  608. }
  609. uint8_t Trinamic_TMC2130::set_pwm_freq(uint8_t value)
  610. {
  611. set_PWMCONF(TMC_PWMCONF_PWM_FREQ, value);
  612. return _status;
  613. }
  614. uint8_t Trinamic_TMC2130::set_PWM_GRAD(uint8_t value)
  615. {
  616. set_PWMCONF(TMC_PWMCONF_PWM_GRAD, value);
  617. return _status;
  618. }
  619. uint8_t Trinamic_TMC2130::set_PWM_AMPL(uint8_t value)
  620. {
  621. set_PWMCONF(TMC_PWMCONF_PWM_AMPL, value);
  622. return _status;
  623. }
  624. //////////
  625. // ENCM_CTRL
  626. //////////
  627. uint8_t Trinamic_TMC2130::set_ENCM_CTRL(uint8_t value)
  628. {
  629. uint8_t data;
  630. data = value & TMC_ENCM_CTRL_MASK;
  631. write_REG(TMC_REG_ENCM_CTRL, data);
  632. return _status;
  633. }
  634. //////////
  635. // STATUS
  636. //////////
  637. // check the reset status
  638. boolean Trinamic_TMC2130::isReset()
  639. {
  640. return _status&TMC_SPISTATUS_RESET_MASK ? true : false;
  641. }
  642. // check the error status
  643. boolean Trinamic_TMC2130::isError()
  644. {
  645. return _status&TMC_SPISTATUS_ERROR_MASK ? true : false;
  646. }
  647. // check the stallguard status
  648. boolean Trinamic_TMC2130::isStallguard()
  649. {
  650. return _status&TMC_SPISTATUS_STALLGUARD_MASK ? true : false;
  651. }
  652. // check the standstill status
  653. boolean Trinamic_TMC2130::isStandstill()
  654. {
  655. return _status&TMC_SPISTATUS_STANDSTILL_MASK ? true : false;
  656. }
  657. // get debug messages
  658. String Trinamic_TMC2130::debug()
  659. {
  660. return _debug;
  661. }