TMCStepper
TMC2130Stepper.cpp
Go to the documentation of this file.
1 #include "TMCStepper.h"
2 #include "TMC_MACROS.h"
3 
5 uint32_t TMC2130Stepper::spi_speed = 16000000/8;
6 
7 TMC2130Stepper::TMC2130Stepper(uint16_t pinCS, float RS, int8_t link) :
8  TMCStepper(RS),
9  _pinCS(pinCS),
10  link_index(link)
11  {
12  defaults();
13 
14  if (link > chain_length)
15  chain_length = link;
16  }
17 
18 TMC2130Stepper::TMC2130Stepper(uint16_t pinCS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, int8_t link) :
19  TMCStepper(default_RS),
20  _pinCS(pinCS),
21  link_index(link)
22  {
23  SW_SPIClass *SW_SPI_Obj = new SW_SPIClass(pinMOSI, pinMISO, pinSCK);
24  TMC_SW_SPI = SW_SPI_Obj;
25  defaults();
26 
27  if (link > chain_length)
28  chain_length = link;
29  }
30 
31 TMC2130Stepper::TMC2130Stepper(uint16_t pinCS, float RS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, int8_t link) :
32  TMCStepper(RS),
33  _pinCS(pinCS),
34  link_index(link)
35  {
36  SW_SPIClass *SW_SPI_Obj = new SW_SPIClass(pinMOSI, pinMISO, pinSCK);
37  TMC_SW_SPI = SW_SPI_Obj;
38  defaults();
39 
40  if (link > chain_length)
41  chain_length = link;
42  }
43 
45  //MSLUT0_register.sr = ???;
46  //MSLUT1_register.sr = ???;
47  //MSLUT2_register.sr = ???;
48  //MSLUT3_register.sr = ???;
49  //MSLUT4_register.sr = ???;
50  //MSLUT5_register.sr = ???;
51  //MSLUT6_register.sr = ???;
52  //MSLUT7_register.sr = ???;
53  //MSLUTSTART_register.start_sin90 = 247;
54  PWMCONF_register.sr = 0x00050480;
55 }
56 
57 void TMC2130Stepper::setSPISpeed(uint32_t speed) {
58  spi_speed = speed;
59 }
60 
61 void TMC2130Stepper::switchCSpin(bool state) {
62  digitalWrite(_pinCS, state);
63 }
64 
65 uint32_t TMC2130Stepper::read(uint8_t addressByte) {
66  uint32_t out = 0UL;
67  int8_t i = 1;
68 
69  if (TMC_SW_SPI != nullptr) {
70  switchCSpin(LOW);
71  TMC_SW_SPI->transfer(addressByte & 0xFF);
72  TMC_SW_SPI->transfer16(0x0000); // Clear SPI
73  TMC_SW_SPI->transfer16(0x0000);
74 
75  // Shift the written data to the correct driver in chain
76  // Default link_index = -1 and no shifting happens
77  while(i < link_index) {
78  TMC_SW_SPI->transfer16(0x0000);
79  TMC_SW_SPI->transfer16(0x0000);
80  TMC_SW_SPI->transfer(0x00);
81  i++;
82  }
83 
84  switchCSpin(HIGH);
85  switchCSpin(LOW);
86 
87  // Shift data from target link into the last one...
88  while(i < chain_length) {
89  TMC_SW_SPI->transfer16(0x0000);
90  TMC_SW_SPI->transfer16(0x0000);
91  TMC_SW_SPI->transfer(0x00);
92  i++;
93  }
94 
95  // ...and once more to MCU
96  status_response = TMC_SW_SPI->transfer(addressByte & 0xFF); // Send the address byte again
97  out = TMC_SW_SPI->transfer(0x00);
98  out <<= 8;
99  out |= TMC_SW_SPI->transfer(0x00);
100  out <<= 8;
101  out |= TMC_SW_SPI->transfer(0x00);
102  out <<= 8;
103  out |= TMC_SW_SPI->transfer(0x00);
104 
105  } else {
106  SPI.beginTransaction(SPISettings(spi_speed, MSBFIRST, SPI_MODE3));
107  switchCSpin(LOW);
108  SPI.transfer(addressByte & 0xFF);
109  SPI.transfer16(0x0000); // Clear SPI
110  SPI.transfer16(0x0000);
111 
112  // Shift the written data to the correct driver in chain
113  // Default link_index = -1 and no shifting happens
114  while(i < link_index) {
115  SPI.transfer16(0x0000);
116  SPI.transfer16(0x0000);
117  SPI.transfer(0x0000);
118  i++;
119  }
120 
121  switchCSpin(HIGH);
122  switchCSpin(LOW);
123 
124  // Shift data from target link into the last one...
125  while(i < chain_length) {
126  SPI.transfer16(0x0000);
127  SPI.transfer16(0x0000);
128  SPI.transfer(0x0000);
129  i++;
130  }
131 
132  // ...and once more to MCU
133  status_response = SPI.transfer(addressByte & 0xFF); // Send the address byte again
134  out = SPI.transfer(0x00);
135  out <<= 8;
136  out |= SPI.transfer(0x00);
137  out <<= 8;
138  out |= SPI.transfer(0x00);
139  out <<= 8;
140  out |= SPI.transfer(0x00);
141 
142  SPI.endTransaction();
143  }
144  switchCSpin(HIGH);
145  return out;
146 }
147 
148 void TMC2130Stepper::write(uint8_t addressByte, uint32_t config) {
149  addressByte |= TMC_WRITE;
150  int8_t i = 1;
151  if (TMC_SW_SPI != nullptr) {
152  switchCSpin(LOW);
153  status_response = TMC_SW_SPI->transfer(addressByte & 0xFF);
154  TMC_SW_SPI->transfer16((config>>16) & 0xFFFF);
155  TMC_SW_SPI->transfer16(config & 0xFFFF);
156 
157  // Shift the written data to the correct driver in chain
158  // Default link_index = -1 and no shifting happens
159  while(i < link_index) {
160  TMC_SW_SPI->transfer16(0x0000);
161  TMC_SW_SPI->transfer16(0x0000);
162  TMC_SW_SPI->transfer(0x00);
163  i++;
164  }
165  } else {
166  SPI.beginTransaction(SPISettings(spi_speed, MSBFIRST, SPI_MODE3));
167  switchCSpin(LOW);
168  status_response = SPI.transfer(addressByte & 0xFF);
169  SPI.transfer16((config>>16) & 0xFFFF);
170  SPI.transfer16(config & 0xFFFF);
171 
172  // Shift the written data to the correct driver in chain
173  // Default link_index = -1 and no shifting happens
174  while(i < link_index) {
175  SPI.transfer16(0x0000);
176  SPI.transfer16(0x0000);
177  SPI.transfer(0x0000);
178  i++;
179  }
180 
181  SPI.endTransaction();
182  }
183  switchCSpin(HIGH);
184 }
185 
187  //set pins
188  pinMode(_pinCS, OUTPUT);
189  switchCSpin(HIGH);
190 
191  if (TMC_SW_SPI != nullptr) TMC_SW_SPI->init();
192 
193  GCONF(GCONF_register.sr);
194  CHOPCONF(CHOPCONF_register.sr);
195  COOLCONF(COOLCONF_register.sr);
196  PWMCONF(PWMCONF_register.sr);
197  IHOLD_IRUN(IHOLD_IRUN_register.sr);
198 
199  toff(8); //off_time(8);
200  tbl(1); //blank_time(24);
201 }
202 
207 bool TMC2130Stepper::isEnabled() { return !drv_enn_cfg6() && toff(); }
208 
210  GCONF(GCONF_register.sr);
211  IHOLD_IRUN(IHOLD_IRUN_register.sr);
212  TPOWERDOWN(TPOWERDOWN_register.sr);
213  TPWMTHRS(TPWMTHRS_register.sr);
214  TCOOLTHRS(TCOOLTHRS_register.sr);
215  THIGH(THIGH_register.sr);
216  XDIRECT(XDIRECT_register.sr);
217  VDCMIN(VDCMIN_register.sr);
218  CHOPCONF(CHOPCONF_register.sr);
219  COOLCONF(COOLCONF_register.sr);
220  DCCTRL(DCCTRL_register.sr);
221  PWMCONF(PWMCONF_register.sr);
222  ENCM_CTRL(ENCM_CTRL_register.sr);
223 }
224 
226 // R: IOIN
228 bool TMC2130Stepper::step() { IOIN_t r{0}; r.sr = IOIN(); return r.step; }
229 bool TMC2130Stepper::dir() { IOIN_t r{0}; r.sr = IOIN(); return r.dir; }
230 bool TMC2130Stepper::dcen_cfg4() { IOIN_t r{0}; r.sr = IOIN(); return r.dcen_cfg4; }
231 bool TMC2130Stepper::dcin_cfg5() { IOIN_t r{0}; r.sr = IOIN(); return r.dcin_cfg5; }
232 bool TMC2130Stepper::drv_enn_cfg6() { IOIN_t r{0}; r.sr = IOIN(); return r.drv_enn_cfg6; }
233 bool TMC2130Stepper::dco() { IOIN_t r{0}; r.sr = IOIN(); return r.dco; }
234 uint8_t TMC2130Stepper::version() { IOIN_t r{0}; r.sr = IOIN(); return r.version; }
236 // W: TCOOLTHRS
237 uint32_t TMC2130Stepper::TCOOLTHRS() { return TCOOLTHRS_register.sr; }
238 void TMC2130Stepper::TCOOLTHRS(uint32_t input) {
239  TCOOLTHRS_register.sr = input;
240  write(TCOOLTHRS_register.address, TCOOLTHRS_register.sr);
241 }
243 // W: THIGH
244 uint32_t TMC2130Stepper::THIGH() { return THIGH_register.sr; }
245 void TMC2130Stepper::THIGH(uint32_t input) {
246  THIGH_register.sr = input;
247  write(THIGH_register.address, THIGH_register.sr);
248 }
250 // RW: XDIRECT
252  return read(XDIRECT_register.address);
253 }
254 void TMC2130Stepper::XDIRECT(uint32_t input) {
255  XDIRECT_register.sr = input;
256  write(XDIRECT_register.address, XDIRECT_register.sr);
257 }
258 void TMC2130Stepper::coil_A(int16_t B) { XDIRECT_register.coil_A = B; write(XDIRECT_register.address, XDIRECT_register.sr); }
259 void TMC2130Stepper::coil_B(int16_t B) { XDIRECT_register.coil_B = B; write(XDIRECT_register.address, XDIRECT_register.sr); }
260 int16_t TMC2130Stepper::coil_A() { XDIRECT_t r{0}; r.sr = XDIRECT(); return r.coil_A; }
261 int16_t TMC2130Stepper::coil_B() { XDIRECT_t r{0}; r.sr = XDIRECT(); return r.coil_B; }
263 // W: VDCMIN
264 uint32_t TMC2130Stepper::VDCMIN() { return VDCMIN_register.sr; }
265 void TMC2130Stepper::VDCMIN(uint32_t input) {
266  VDCMIN_register.sr = input;
267  write(VDCMIN_register.address, VDCMIN_register.sr);
268 }
270 // RW: DCCTRL
271 void TMC2130Stepper::DCCTRL(uint32_t input) {
272  DCCTRL_register.sr = input;
273  write(DCCTRL_register.address, DCCTRL_register.sr);
274 }
275 void TMC2130Stepper::dc_time(uint16_t input) {
276  DCCTRL_register.dc_time = input;
277  write(DCCTRL_register.address, DCCTRL_register.sr);
278 }
279 void TMC2130Stepper::dc_sg(uint8_t input) {
280  DCCTRL_register.dc_sg = input;
281  write(DCCTRL_register.address, DCCTRL_register.sr);
282 }
283 
285  return read(DCCTRL_register.address);
286 }
288  DCCTRL_t r{0};
289  r.sr = DCCTRL();
290  return r.dc_time;
291 }
293  DCCTRL_t r{0};
294  r.sr = DCCTRL();
295  return r.dc_sg;
296 }
298 // R: PWM_SCALE
301 // W: ENCM_CTRL
302 uint8_t TMC2130Stepper::ENCM_CTRL() { return ENCM_CTRL_register.sr; }
303 void TMC2130Stepper::ENCM_CTRL(uint8_t input) {
304  ENCM_CTRL_register.sr = input;
305  write(ENCM_CTRL_register.address, ENCM_CTRL_register.sr);
306 }
307 void TMC2130Stepper::inv(bool B) { ENCM_CTRL_register.inv = B; write(ENCM_CTRL_register.address, ENCM_CTRL_register.sr); }
308 void TMC2130Stepper::maxspeed(bool B) { ENCM_CTRL_register.maxspeed = B; write(ENCM_CTRL_register.address, ENCM_CTRL_register.sr); }
309 bool TMC2130Stepper::inv() { return ENCM_CTRL_register.inv; }
310 bool TMC2130Stepper::maxspeed() { return ENCM_CTRL_register.maxspeed; }
312 // R: LOST_STEPS
314 
316  switch(value) {
317  case 32: sedn(0b00); break;
318  case 8: sedn(0b01); break;
319  case 2: sedn(0b10); break;
320  case 1: sedn(0b11); break;
321  }
322 }
324  switch(sedn()) {
325  case 0b00: return 32;
326  case 0b01: return 8;
327  case 0b10: return 2;
328  case 0b11: return 1;
329  }
330  return 0;
331 }
TMC2130Stepper::defaults
void defaults()
Definition: TMC2130Stepper.cpp:44
SW_SPIClass::transfer16
uint16_t transfer16(uint16_t data)
Definition: SW_SPI.cpp:46
TMC2130Stepper::step
bool step()
Definition: TMC2130Stepper.cpp:228
TMC2130Stepper::toff
uint8_t toff()
Definition: CHOPCONF.cpp:32
TMCStepper
Definition: TMCStepper.h:51
TMC2130Stepper::IOIN
uint32_t IOIN()
Definition: TMC2130Stepper.cpp:227
TMC2130Stepper::THIGH
uint32_t THIGH()
Definition: TMC2130Stepper.cpp:244
TMC2130Stepper::sg_current_decrease
uint8_t sg_current_decrease()
Definition: TMC2130Stepper.cpp:323
TMC2130Stepper::push
void push()
Definition: TMC2130Stepper.cpp:209
TMC2130Stepper::coil_A
int16_t coil_A()
Definition: TMC2130Stepper.cpp:260
TMC2130Stepper::status_response
uint8_t status_response
Definition: TMCStepper.h:329
TMC2130Stepper::ENCM_CTRL
uint8_t ENCM_CTRL()
Definition: TMC2130Stepper.cpp:302
TMC2130Stepper::dco
bool dco()
Definition: TMC2130Stepper.cpp:233
TMC2130Stepper::DCCTRL
uint32_t DCCTRL()
Definition: TMC2130Stepper.cpp:284
TMC2130Stepper::TMC_SW_SPI
SW_SPIClass * TMC_SW_SPI
Definition: TMCStepper.h:353
TMC2130Stepper::TMC2130Stepper
TMC2130Stepper(uint16_t pinCS, float RS=default_RS, int8_t link_index=-1)
Definition: TMC2130Stepper.cpp:7
TMC2130Stepper::spi_speed
static uint32_t spi_speed
Definition: TMCStepper.h:351
SW_SPIClass
Definition: SW_SPI.h:6
XDIRECT_t::sr
uint32_t sr
Definition: TMC2130_bitfields.h:108
TMC2130Stepper::dc_time
uint16_t dc_time()
Definition: TMC2130Stepper.cpp:287
TMC2130Stepper::COOLCONF
uint32_t COOLCONF()
Definition: COOLCONF.cpp:8
XDIRECT_t
Definition: TMC2130_bitfields.h:105
TMCStepper.h
TMC2130Stepper::chain_length
static int8_t chain_length
Definition: TMCStepper.h:357
TMC2130Stepper::link_index
int8_t link_index
Definition: TMCStepper.h:356
TMC2130Stepper::VDCMIN
uint32_t VDCMIN()
Definition: TMC2130Stepper.cpp:264
TMC2130Stepper::switchCSpin
void switchCSpin(bool state)
Definition: TMC2130Stepper.cpp:61
TMC2130Stepper::drv_enn_cfg6
bool drv_enn_cfg6()
Definition: TMC2130Stepper.cpp:232
TMC2130Stepper::coil_B
int16_t coil_B()
Definition: TMC2130Stepper.cpp:261
TMC2130Stepper::version
uint8_t version()
Definition: TMC2130Stepper.cpp:234
TMC2130Stepper::tbl
uint8_t tbl()
Definition: CHOPCONF.cpp:39
TMC2130Stepper::read
uint32_t read(uint8_t addressByte)
Definition: TMC2130Stepper.cpp:65
TMC2130Stepper::GCONF
uint32_t GCONF()
Definition: GCONF.cpp:7
TMC_MACROS.h
IOIN_t
Definition: TMC2130_bitfields.h:67
SW_SPIClass::transfer
uint8_t transfer(uint8_t ulVal)
Definition: SW_SPI.cpp:24
TMCStepper::TMC_WRITE
static constexpr uint8_t TMC_WRITE
Definition: TMCStepper.h:114
TMC2130Stepper::CHOPCONF
uint32_t CHOPCONF()
Definition: CHOPCONF.cpp:7
IOIN_t::sr
uint32_t sr
Definition: TMC2130_bitfields.h:70
TMC2130Stepper::dc_sg
uint8_t dc_sg()
Definition: TMC2130Stepper.cpp:292
TMC2130Stepper::dcen_cfg4
bool dcen_cfg4()
Definition: TMC2130Stepper.cpp:230
SW_SPIClass::init
void init()
Definition: SW_SPI.cpp:9
TMC2130Stepper::sedn
uint8_t sedn()
Definition: COOLCONF.cpp:25
TMC2130Stepper::setSPISpeed
void setSPISpeed(uint32_t speed)
Definition: TMC2130Stepper.cpp:57
DCCTRL_t::sr
uint32_t sr
Definition: TMC2130_bitfields.h:176
TMC2130Stepper::PWM_SCALE
uint8_t PWM_SCALE()
Definition: TMC2130Stepper.cpp:299
DCCTRL_t
Definition: TMC2130_bitfields.h:173
TMC2130Stepper::isEnabled
bool isEnabled()
Definition: TMC2130Stepper.cpp:207
TMCStepper::TPOWERDOWN
uint8_t TPOWERDOWN()
Definition: TMCStepper.cpp:123
TMC2130Stepper::TCOOLTHRS
uint32_t TCOOLTHRS()
Definition: TMC2130Stepper.cpp:237
TMC2130Stepper::LOST_STEPS
uint32_t LOST_STEPS()
Definition: TMC2130Stepper.cpp:313
TMC2130Stepper::inv
bool inv()
Definition: TMC2130Stepper.cpp:309
TMC2130Stepper::PWM_SCALE_t::address
constexpr static uint8_t address
Definition: TMCStepper.h:347
TMC2130Stepper::begin
void begin()
Definition: TMC2130Stepper.cpp:186
TMC2130Stepper::PWMCONF
uint32_t PWMCONF()
Definition: PWMCONF.cpp:8
TMC2130Stepper::_pinCS
const uint16_t _pinCS
Definition: TMCStepper.h:352
TMC2130Stepper::dir
bool dir()
Definition: TMC2130Stepper.cpp:229
TMC2130Stepper::maxspeed
bool maxspeed()
Definition: TMC2130Stepper.cpp:310
IOIN_t::address
constexpr static uint8_t address
Definition: TMC2130_bitfields.h:68
TMC2130Stepper::write
void write(uint8_t addressByte, uint32_t config)
Definition: TMC2130Stepper.cpp:148
TMC2130Stepper::LOST_STEPS_t::address
constexpr static uint8_t address
Definition: TMCStepper.h:348
TMCStepper::IHOLD_IRUN
uint32_t IHOLD_IRUN()
Definition: IHOLD_IRUN.cpp:8
TMC2130Stepper::dcin_cfg5
bool dcin_cfg5()
Definition: TMC2130Stepper.cpp:231
TMCStepper::TPWMTHRS
uint32_t TPWMTHRS()
Definition: TMCStepper.cpp:133
TMC2130Stepper::XDIRECT
uint32_t XDIRECT()
Definition: TMC2130Stepper.cpp:251