22 #if SW_CAPABLE_PLATFORM
27 RXTX_pin(SW_RX_pin == SW_TX_pin ? SW_RX_pin : 0),
30 SoftwareSerial *SWSerialObj =
new SoftwareSerial(SW_RX_pin, SW_TX_pin);
31 SWSerial = SWSerialObj;
36 if (SWSerial !=
nullptr)
38 SWSerial->begin(baudrate);
39 SWSerial->stopListening();
41 #if defined(ARDUINO_ARCH_AVR)
43 digitalWrite(RXTX_pin, HIGH);
44 pinMode(RXTX_pin, OUTPUT);
51 #if SW_CAPABLE_PLATFORM
60 GCONF_register.i_scale_analog = 1;
61 GCONF_register.internal_rsense = 0;
62 GCONF_register.en_spreadcycle = 0;
63 GCONF_register.multistep_filt = 1;
64 IHOLD_IRUN_register.iholddelay = 1;
65 TPOWERDOWN_register.sr = 20;
66 CHOPCONF_register.sr = 0x10000053;
67 PWMCONF_register.sr = 0xC10D0024;
80 GCONF(GCONF_register.sr);
94 for (uint8_t i = 0; i < len; i++) {
95 uint8_t currentByte = datagram[i];
96 for (uint8_t j = 0; j < 8; j++) {
97 if ((crc >> 7) ^ (currentByte & 0x01)) {
98 crc = (crc << 1) ^ 0x07;
103 currentByte = currentByte >> 1;
109 template<
class SERIAL_TYPE>
111 return serPtr.read();
113 template<
class SERIAL_TYPE>
115 return serPtr.write(data);
121 uint8_t datagram[] = {
TMC2208_SYNC,
slave_address, addr, (uint8_t)(regVal>>24), (uint8_t)(regVal>>16), (uint8_t)(regVal>>8), (uint8_t)(regVal>>0), 0x00};
123 datagram[len] =
calcCRC(datagram, len);
125 #if SW_CAPABLE_PLATFORM
126 if (SWSerial !=
nullptr) {
127 for(uint8_t i=0; i<=len; i++){
136 for(uint8_t i=0; i<=len; i++){
143 template<
typename SERIAL_TYPE>
145 while (serPtr.available() > 0)
serial_read(serPtr);
147 #if defined(ARDUINO_ARCH_AVR)
149 digitalWrite(RXTX_pin, HIGH);
150 pinMode(RXTX_pin, OUTPUT);
154 for(
int i=0; i<=len; i++)
serial_write(serPtr, datagram[i]);
156 #if defined(ARDUINO_ARCH_AVR)
158 pinMode(RXTX_pin, INPUT_PULLUP);
165 uint32_t ms = millis();
166 uint32_t sync_target = (
static_cast<uint32_t
>(datagram[0])<<16) | 0xFF00 | datagram[2];
170 uint32_t
ms2 = millis();
176 if (!timeout)
return 0;
179 if (res < 0)
continue;
185 }
while (sync != sync_target);
191 for(uint8_t i=0; i<5;) {
192 uint32_t
ms2 = millis();
198 if (!timeout)
return 0;
201 if (res < 0)
continue;
209 #if defined(ARDUINO_ARCH_AVR)
211 digitalWrite(RXTX_pin, HIGH);
212 pinMode(RXTX_pin, OUTPUT);
216 while (serPtr.available() > 0)
serial_read(serPtr);
222 constexpr uint8_t len = 3;
225 datagram[len] =
calcCRC(datagram, len);
226 uint64_t out = 0x00000000UL;
229 #if SW_CAPABLE_PLATFORM
230 if (SWSerial !=
nullptr) {
233 SWSerial->stopListening();
246 uint8_t out_datagram[] = {
247 static_cast<uint8_t
>(out>>56),
248 static_cast<uint8_t
>(out>>48),
249 static_cast<uint8_t
>(out>>40),
250 static_cast<uint8_t
>(out>>32),
251 static_cast<uint8_t
>(out>>24),
252 static_cast<uint8_t
>(out>>16),
253 static_cast<uint8_t
>(out>> 8),
254 static_cast<uint8_t
>(out>> 0)
256 uint8_t crc =
calcCRC(out_datagram, 7);
257 if ((crc !=
static_cast<uint8_t
>(out)) || crc == 0 ) {
273 SLAVECONF_register.sr = input&0xF00;
274 write(SLAVECONF_register.address, SLAVECONF_register.sr);
277 return SLAVECONF_register.sr;
317 return read(FACTORY_CONF_register.address);
320 FACTORY_CONF_register.sr = input;
321 write(FACTORY_CONF_register.address, FACTORY_CONF_register.sr);
329 VACTUAL_register.sr = input;
330 write(VACTUAL_register.address, VACTUAL_register.sr);
333 return VACTUAL_register.sr;
342 return r.pwm_scale_sum;
348 return r.pwm_scale_auto;