Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: TMCStepper mRotaryEncoder-os
source/TMC2208Stepper.cpp@0:3f4cfbeda9d3, 2021-02-04 (annotated)
- Committer:
- charly
- Date:
- Thu Feb 04 19:46:56 2021 +0000
- Revision:
- 0:3f4cfbeda9d3
Working Version with TMC2209
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| charly | 0:3f4cfbeda9d3 | 1 | #include "TMCStepper.h" |
| charly | 0:3f4cfbeda9d3 | 2 | #include "TMC_MACROS.h" |
| charly | 0:3f4cfbeda9d3 | 3 | //#include "SERIAL_SWITCH.h" |
| charly | 0:3f4cfbeda9d3 | 4 | |
| charly | 0:3f4cfbeda9d3 | 5 | // Protected |
| charly | 0:3f4cfbeda9d3 | 6 | // addr needed for TMC2209 |
| charly | 0:3f4cfbeda9d3 | 7 | TMC2208Stepper::TMC2208Stepper(Stream * SerialPort, float RS, uint8_t addr) : |
| charly | 0:3f4cfbeda9d3 | 8 | TMCStepper(RS), |
| charly | 0:3f4cfbeda9d3 | 9 | slave_address(addr) |
| charly | 0:3f4cfbeda9d3 | 10 | { |
| charly | 0:3f4cfbeda9d3 | 11 | HWSerial = SerialPort; |
| charly | 0:3f4cfbeda9d3 | 12 | defaults(); |
| charly | 0:3f4cfbeda9d3 | 13 | } |
| charly | 0:3f4cfbeda9d3 | 14 | |
| charly | 0:3f4cfbeda9d3 | 15 | TMC2208Stepper::TMC2208Stepper(Stream * SerialPort, float RS, uint8_t addr, uint16_t mul_pin1, uint16_t mul_pin2) : |
| charly | 0:3f4cfbeda9d3 | 16 | TMC2208Stepper(SerialPort, RS) |
| charly | 0:3f4cfbeda9d3 | 17 | { |
| charly | 0:3f4cfbeda9d3 | 18 | // SSwitch *SMulObj = new SSwitch(mul_pin1, mul_pin2, addr); |
| charly | 0:3f4cfbeda9d3 | 19 | // sswitch = SMulObj; |
| charly | 0:3f4cfbeda9d3 | 20 | } |
| charly | 0:3f4cfbeda9d3 | 21 | |
| charly | 0:3f4cfbeda9d3 | 22 | #if SW_CAPABLE_PLATFORM |
| charly | 0:3f4cfbeda9d3 | 23 | // Protected |
| charly | 0:3f4cfbeda9d3 | 24 | // addr needed for TMC2209 |
| charly | 0:3f4cfbeda9d3 | 25 | TMC2208Stepper::TMC2208Stepper(PinName SW_RX_pin, PinName SW_TX_pin, float RS, uint8_t addr) : |
| charly | 0:3f4cfbeda9d3 | 26 | TMCStepper(RS), |
| charly | 0:3f4cfbeda9d3 | 27 | RXTX_pin(SW_RX_pin == SW_TX_pin ? SW_RX_pin : 0), |
| charly | 0:3f4cfbeda9d3 | 28 | slave_address(addr) |
| charly | 0:3f4cfbeda9d3 | 29 | { |
| charly | 0:3f4cfbeda9d3 | 30 | BufferedSerial *SWSerialObj = new BufferedSerial(SW_TX_pin, SW_RX_pin); |
| charly | 0:3f4cfbeda9d3 | 31 | SWSerial = SWSerialObj; |
| charly | 0:3f4cfbeda9d3 | 32 | defaults(); |
| charly | 0:3f4cfbeda9d3 | 33 | } |
| charly | 0:3f4cfbeda9d3 | 34 | |
| charly | 0:3f4cfbeda9d3 | 35 | void TMC2208Stepper::beginSerial(uint32_t baudrate) { |
| charly | 0:3f4cfbeda9d3 | 36 | if (SWSerial != nullptr) |
| charly | 0:3f4cfbeda9d3 | 37 | { |
| charly | 0:3f4cfbeda9d3 | 38 | //SWSerial->begin(baudrate); |
| charly | 0:3f4cfbeda9d3 | 39 | //SWSerial->end(); |
| charly | 0:3f4cfbeda9d3 | 40 | SWSerial->set_baud(baudrate); |
| charly | 0:3f4cfbeda9d3 | 41 | SWSerial->set_format( |
| charly | 0:3f4cfbeda9d3 | 42 | /* bits */ 8, |
| charly | 0:3f4cfbeda9d3 | 43 | /* parity */ BufferedSerial::None, |
| charly | 0:3f4cfbeda9d3 | 44 | /* stop bit */ 1 |
| charly | 0:3f4cfbeda9d3 | 45 | ); |
| charly | 0:3f4cfbeda9d3 | 46 | SWSerial->set_blocking(true); |
| charly | 0:3f4cfbeda9d3 | 47 | } |
| charly | 0:3f4cfbeda9d3 | 48 | // #if defined(ARDUINO_ARCH_AVR) |
| charly | 0:3f4cfbeda9d3 | 49 | // if (RXTX_pin > 0) { |
| charly | 0:3f4cfbeda9d3 | 50 | // digitalWrite(RXTX_pin, HIGH); |
| charly | 0:3f4cfbeda9d3 | 51 | // pinMode(RXTX_pin, OUTPUT); |
| charly | 0:3f4cfbeda9d3 | 52 | // } |
| charly | 0:3f4cfbeda9d3 | 53 | // #endif |
| charly | 0:3f4cfbeda9d3 | 54 | } |
| charly | 0:3f4cfbeda9d3 | 55 | #endif |
| charly | 0:3f4cfbeda9d3 | 56 | |
| charly | 0:3f4cfbeda9d3 | 57 | void TMC2208Stepper::begin() { |
| charly | 0:3f4cfbeda9d3 | 58 | #if SW_CAPABLE_PLATFORM |
| charly | 0:3f4cfbeda9d3 | 59 | beginSerial(115200); |
| charly | 0:3f4cfbeda9d3 | 60 | //beginSerial(9600); |
| charly | 0:3f4cfbeda9d3 | 61 | #endif |
| charly | 0:3f4cfbeda9d3 | 62 | pdn_disable(true); |
| charly | 0:3f4cfbeda9d3 | 63 | mstep_reg_select(true); |
| charly | 0:3f4cfbeda9d3 | 64 | //Wait to initialize |
| charly | 0:3f4cfbeda9d3 | 65 | wait_us(replyDelay*1000); |
| charly | 0:3f4cfbeda9d3 | 66 | |
| charly | 0:3f4cfbeda9d3 | 67 | } |
| charly | 0:3f4cfbeda9d3 | 68 | |
| charly | 0:3f4cfbeda9d3 | 69 | void TMC2208Stepper::defaults() { |
| charly | 0:3f4cfbeda9d3 | 70 | GCONF_register.i_scale_analog = 1; |
| charly | 0:3f4cfbeda9d3 | 71 | GCONF_register.internal_rsense = 0; // OTP |
| charly | 0:3f4cfbeda9d3 | 72 | GCONF_register.en_spreadcycle = 0; // OTP |
| charly | 0:3f4cfbeda9d3 | 73 | GCONF_register.multistep_filt = 1; // OTP |
| charly | 0:3f4cfbeda9d3 | 74 | IHOLD_IRUN_register.iholddelay = 1; // OTP |
| charly | 0:3f4cfbeda9d3 | 75 | TPOWERDOWN_register.sr = 20; |
| charly | 0:3f4cfbeda9d3 | 76 | CHOPCONF_register.sr = 0x10000053; |
| charly | 0:3f4cfbeda9d3 | 77 | PWMCONF_register.sr = 0xC10D0024; |
| charly | 0:3f4cfbeda9d3 | 78 | //MSLUT0_register.sr = ???; |
| charly | 0:3f4cfbeda9d3 | 79 | //MSLUT1_register.sr = ???; |
| charly | 0:3f4cfbeda9d3 | 80 | //MSLUT2_register.sr = ???; |
| charly | 0:3f4cfbeda9d3 | 81 | //MSLUT3_register.sr = ???; |
| charly | 0:3f4cfbeda9d3 | 82 | //MSLUT4_register.sr = ???; |
| charly | 0:3f4cfbeda9d3 | 83 | //MSLUT5_register.sr = ???; |
| charly | 0:3f4cfbeda9d3 | 84 | //MSLUT6_register.sr = ???; |
| charly | 0:3f4cfbeda9d3 | 85 | //MSLUT7_register.sr = ???; |
| charly | 0:3f4cfbeda9d3 | 86 | //MSLUTSTART_register.start_sin90 = 247; |
| charly | 0:3f4cfbeda9d3 | 87 | } |
| charly | 0:3f4cfbeda9d3 | 88 | |
| charly | 0:3f4cfbeda9d3 | 89 | void TMC2208Stepper::push() { |
| charly | 0:3f4cfbeda9d3 | 90 | GCONF(GCONF_register.sr); |
| charly | 0:3f4cfbeda9d3 | 91 | IHOLD_IRUN(IHOLD_IRUN_register.sr); |
| charly | 0:3f4cfbeda9d3 | 92 | SLAVECONF(SLAVECONF_register.sr); |
| charly | 0:3f4cfbeda9d3 | 93 | TPOWERDOWN(TPOWERDOWN_register.sr); |
| charly | 0:3f4cfbeda9d3 | 94 | TPWMTHRS(TPWMTHRS_register.sr); |
| charly | 0:3f4cfbeda9d3 | 95 | VACTUAL(VACTUAL_register.sr); |
| charly | 0:3f4cfbeda9d3 | 96 | CHOPCONF(CHOPCONF_register.sr); |
| charly | 0:3f4cfbeda9d3 | 97 | PWMCONF(PWMCONF_register.sr); |
| charly | 0:3f4cfbeda9d3 | 98 | } |
| charly | 0:3f4cfbeda9d3 | 99 | |
| charly | 0:3f4cfbeda9d3 | 100 | bool TMC2208Stepper::isEnabled() { return !enn() && toff(); } |
| charly | 0:3f4cfbeda9d3 | 101 | |
| charly | 0:3f4cfbeda9d3 | 102 | uint8_t TMC2208Stepper::calcCRC(uint8_t datagram[], uint8_t len) { |
| charly | 0:3f4cfbeda9d3 | 103 | uint8_t crc = 0; |
| charly | 0:3f4cfbeda9d3 | 104 | for (uint8_t i = 0; i < len; i++) { |
| charly | 0:3f4cfbeda9d3 | 105 | uint8_t currentByte = datagram[i]; |
| charly | 0:3f4cfbeda9d3 | 106 | for (uint8_t j = 0; j < 8; j++) { |
| charly | 0:3f4cfbeda9d3 | 107 | if ((crc >> 7) ^ (currentByte & 0x01)) { |
| charly | 0:3f4cfbeda9d3 | 108 | crc = (crc << 1) ^ 0x07; |
| charly | 0:3f4cfbeda9d3 | 109 | } else { |
| charly | 0:3f4cfbeda9d3 | 110 | crc = (crc << 1); |
| charly | 0:3f4cfbeda9d3 | 111 | } |
| charly | 0:3f4cfbeda9d3 | 112 | crc &= 0xff; |
| charly | 0:3f4cfbeda9d3 | 113 | currentByte = currentByte >> 1; |
| charly | 0:3f4cfbeda9d3 | 114 | } |
| charly | 0:3f4cfbeda9d3 | 115 | } |
| charly | 0:3f4cfbeda9d3 | 116 | return crc; |
| charly | 0:3f4cfbeda9d3 | 117 | } |
| charly | 0:3f4cfbeda9d3 | 118 | |
| charly | 0:3f4cfbeda9d3 | 119 | __attribute__((weak)) |
| charly | 0:3f4cfbeda9d3 | 120 | int TMC2208Stepper::available() { |
| charly | 0:3f4cfbeda9d3 | 121 | int out = 0; |
| charly | 0:3f4cfbeda9d3 | 122 | #if SW_CAPABLE_PLATFORM |
| charly | 0:3f4cfbeda9d3 | 123 | if (SWSerial != nullptr) { |
| charly | 0:3f4cfbeda9d3 | 124 | // out = SWSerial->available(); |
| charly | 0:3f4cfbeda9d3 | 125 | out = 1; |
| charly | 0:3f4cfbeda9d3 | 126 | } else |
| charly | 0:3f4cfbeda9d3 | 127 | #endif |
| charly | 0:3f4cfbeda9d3 | 128 | if (HWSerial != nullptr) { |
| charly | 0:3f4cfbeda9d3 | 129 | // out = HWSerial->available(); |
| charly | 0:3f4cfbeda9d3 | 130 | out = 1; |
| charly | 0:3f4cfbeda9d3 | 131 | } |
| charly | 0:3f4cfbeda9d3 | 132 | |
| charly | 0:3f4cfbeda9d3 | 133 | return out; |
| charly | 0:3f4cfbeda9d3 | 134 | } |
| charly | 0:3f4cfbeda9d3 | 135 | |
| charly | 0:3f4cfbeda9d3 | 136 | __attribute__((weak)) |
| charly | 0:3f4cfbeda9d3 | 137 | void TMC2208Stepper::preWriteCommunication() { |
| charly | 0:3f4cfbeda9d3 | 138 | if (HWSerial != nullptr) { |
| charly | 0:3f4cfbeda9d3 | 139 | // if (sswitch != nullptr) |
| charly | 0:3f4cfbeda9d3 | 140 | // sswitch->active(); |
| charly | 0:3f4cfbeda9d3 | 141 | } |
| charly | 0:3f4cfbeda9d3 | 142 | } |
| charly | 0:3f4cfbeda9d3 | 143 | |
| charly | 0:3f4cfbeda9d3 | 144 | __attribute__((weak)) |
| charly | 0:3f4cfbeda9d3 | 145 | void TMC2208Stepper::preReadCommunication() { |
| charly | 0:3f4cfbeda9d3 | 146 | #if SW_CAPABLE_PLATFORM |
| charly | 0:3f4cfbeda9d3 | 147 | if (SWSerial != nullptr) { |
| charly | 0:3f4cfbeda9d3 | 148 | // SWSerial->listen(); |
| charly | 0:3f4cfbeda9d3 | 149 | } else |
| charly | 0:3f4cfbeda9d3 | 150 | #endif |
| charly | 0:3f4cfbeda9d3 | 151 | if (HWSerial != nullptr) { |
| charly | 0:3f4cfbeda9d3 | 152 | // if (sswitch != nullptr) |
| charly | 0:3f4cfbeda9d3 | 153 | // sswitch->active(); |
| charly | 0:3f4cfbeda9d3 | 154 | } |
| charly | 0:3f4cfbeda9d3 | 155 | } |
| charly | 0:3f4cfbeda9d3 | 156 | |
| charly | 0:3f4cfbeda9d3 | 157 | __attribute__((weak)) |
| charly | 0:3f4cfbeda9d3 | 158 | int16_t TMC2208Stepper::serial_read() { |
| charly | 0:3f4cfbeda9d3 | 159 | int16_t out = 0; |
| charly | 0:3f4cfbeda9d3 | 160 | int16_t count = 0; |
| charly | 0:3f4cfbeda9d3 | 161 | |
| charly | 0:3f4cfbeda9d3 | 162 | #if SW_CAPABLE_PLATFORM |
| charly | 0:3f4cfbeda9d3 | 163 | if (SWSerial != nullptr) { |
| charly | 0:3f4cfbeda9d3 | 164 | // out = SWSerial->read(); |
| charly | 0:3f4cfbeda9d3 | 165 | count = SWSerial->read(&out, 1); // read one character |
| charly | 0:3f4cfbeda9d3 | 166 | // if (SWSerial->readable()) { |
| charly | 0:3f4cfbeda9d3 | 167 | // SWSerial->read(&out, 1); // read one character |
| charly | 0:3f4cfbeda9d3 | 168 | // } |
| charly | 0:3f4cfbeda9d3 | 169 | |
| charly | 0:3f4cfbeda9d3 | 170 | } else |
| charly | 0:3f4cfbeda9d3 | 171 | #endif |
| charly | 0:3f4cfbeda9d3 | 172 | if (HWSerial != nullptr) { |
| charly | 0:3f4cfbeda9d3 | 173 | // out = HWSerial->read(); |
| charly | 0:3f4cfbeda9d3 | 174 | HWSerial->read(&out, 1); // read one character |
| charly | 0:3f4cfbeda9d3 | 175 | } |
| charly | 0:3f4cfbeda9d3 | 176 | if (count >= 1) { |
| charly | 0:3f4cfbeda9d3 | 177 | // printf("<%02X|",out); |
| charly | 0:3f4cfbeda9d3 | 178 | return out; |
| charly | 0:3f4cfbeda9d3 | 179 | } else { |
| charly | 0:3f4cfbeda9d3 | 180 | return -1; |
| charly | 0:3f4cfbeda9d3 | 181 | } |
| charly | 0:3f4cfbeda9d3 | 182 | } |
| charly | 0:3f4cfbeda9d3 | 183 | |
| charly | 0:3f4cfbeda9d3 | 184 | __attribute__((weak)) |
| charly | 0:3f4cfbeda9d3 | 185 | uint8_t TMC2208Stepper::serial_write(const uint8_t data) { |
| charly | 0:3f4cfbeda9d3 | 186 | int out = 0; |
| charly | 0:3f4cfbeda9d3 | 187 | #if SW_CAPABLE_PLATFORM |
| charly | 0:3f4cfbeda9d3 | 188 | if (SWSerial != nullptr) { |
| charly | 0:3f4cfbeda9d3 | 189 | // printf(">%02X|",data); |
| charly | 0:3f4cfbeda9d3 | 190 | return SWSerial->write(&data,1); |
| charly | 0:3f4cfbeda9d3 | 191 | } else |
| charly | 0:3f4cfbeda9d3 | 192 | #endif |
| charly | 0:3f4cfbeda9d3 | 193 | if (HWSerial != nullptr) { |
| charly | 0:3f4cfbeda9d3 | 194 | return HWSerial->write(&data,1); |
| charly | 0:3f4cfbeda9d3 | 195 | } |
| charly | 0:3f4cfbeda9d3 | 196 | |
| charly | 0:3f4cfbeda9d3 | 197 | return out; |
| charly | 0:3f4cfbeda9d3 | 198 | } |
| charly | 0:3f4cfbeda9d3 | 199 | |
| charly | 0:3f4cfbeda9d3 | 200 | __attribute__((weak)) |
| charly | 0:3f4cfbeda9d3 | 201 | void TMC2208Stepper::postWriteCommunication() {} |
| charly | 0:3f4cfbeda9d3 | 202 | |
| charly | 0:3f4cfbeda9d3 | 203 | __attribute__((weak)) |
| charly | 0:3f4cfbeda9d3 | 204 | void TMC2208Stepper::postReadCommunication() { |
| charly | 0:3f4cfbeda9d3 | 205 | #if SW_CAPABLE_PLATFORM |
| charly | 0:3f4cfbeda9d3 | 206 | // if (SWSerial != nullptr) { |
| charly | 0:3f4cfbeda9d3 | 207 | // SWSerial->end(); |
| charly | 0:3f4cfbeda9d3 | 208 | // } |
| charly | 0:3f4cfbeda9d3 | 209 | #endif |
| charly | 0:3f4cfbeda9d3 | 210 | } |
| charly | 0:3f4cfbeda9d3 | 211 | |
| charly | 0:3f4cfbeda9d3 | 212 | void TMC2208Stepper::write(uint8_t addr, uint32_t regVal) { |
| charly | 0:3f4cfbeda9d3 | 213 | uint8_t len = 7; |
| charly | 0:3f4cfbeda9d3 | 214 | addr |= TMC_WRITE; |
| charly | 0:3f4cfbeda9d3 | 215 | 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}; |
| charly | 0:3f4cfbeda9d3 | 216 | |
| charly | 0:3f4cfbeda9d3 | 217 | datagram[len] = calcCRC(datagram, len); |
| charly | 0:3f4cfbeda9d3 | 218 | |
| charly | 0:3f4cfbeda9d3 | 219 | preWriteCommunication(); |
| charly | 0:3f4cfbeda9d3 | 220 | |
| charly | 0:3f4cfbeda9d3 | 221 | for(uint8_t i=0; i<=len; i++) { |
| charly | 0:3f4cfbeda9d3 | 222 | bytesWritten += serial_write(datagram[i]); |
| charly | 0:3f4cfbeda9d3 | 223 | } |
| charly | 0:3f4cfbeda9d3 | 224 | postWriteCommunication(); |
| charly | 0:3f4cfbeda9d3 | 225 | |
| charly | 0:3f4cfbeda9d3 | 226 | //delay(replyDelay); |
| charly | 0:3f4cfbeda9d3 | 227 | wait_us(replyDelay*1000); |
| charly | 0:3f4cfbeda9d3 | 228 | } |
| charly | 0:3f4cfbeda9d3 | 229 | |
| charly | 0:3f4cfbeda9d3 | 230 | uint64_t TMC2208Stepper::_sendDatagram(uint8_t datagram[], const uint8_t len, uint16_t timeout) { |
| charly | 0:3f4cfbeda9d3 | 231 | |
| charly | 0:3f4cfbeda9d3 | 232 | // while (available() > 0) serial_read(); // Flush |
| charly | 0:3f4cfbeda9d3 | 233 | SWSerial->sync(); |
| charly | 0:3f4cfbeda9d3 | 234 | /* uint8_t dummy; |
| charly | 0:3f4cfbeda9d3 | 235 | while (SWSerial->readable()) { |
| charly | 0:3f4cfbeda9d3 | 236 | SWSerial->read(&dummy, 1); // read one character |
| charly | 0:3f4cfbeda9d3 | 237 | } |
| charly | 0:3f4cfbeda9d3 | 238 | */ |
| charly | 0:3f4cfbeda9d3 | 239 | /* uint8_t dummy; |
| charly | 0:3f4cfbeda9d3 | 240 | while (SWSerial->read(&dummy, 1) >= 0) { |
| charly | 0:3f4cfbeda9d3 | 241 | ; |
| charly | 0:3f4cfbeda9d3 | 242 | } |
| charly | 0:3f4cfbeda9d3 | 243 | */ |
| charly | 0:3f4cfbeda9d3 | 244 | |
| charly | 0:3f4cfbeda9d3 | 245 | #if defined(ARDUINO_ARCH_AVR) |
| charly | 0:3f4cfbeda9d3 | 246 | if (RXTX_pin > 0) { |
| charly | 0:3f4cfbeda9d3 | 247 | digitalWrite(RXTX_pin, HIGH); |
| charly | 0:3f4cfbeda9d3 | 248 | pinMode(RXTX_pin, OUTPUT); |
| charly | 0:3f4cfbeda9d3 | 249 | } |
| charly | 0:3f4cfbeda9d3 | 250 | #endif |
| charly | 0:3f4cfbeda9d3 | 251 | |
| charly | 0:3f4cfbeda9d3 | 252 | for(int i=0; i<=len; i++) serial_write(datagram[i]); |
| charly | 0:3f4cfbeda9d3 | 253 | |
| charly | 0:3f4cfbeda9d3 | 254 | #if defined(ARDUINO_ARCH_AVR) |
| charly | 0:3f4cfbeda9d3 | 255 | if (RXTX_pin > 0) { |
| charly | 0:3f4cfbeda9d3 | 256 | pinMode(RXTX_pin, INPUT_PULLUP); |
| charly | 0:3f4cfbeda9d3 | 257 | } |
| charly | 0:3f4cfbeda9d3 | 258 | #endif |
| charly | 0:3f4cfbeda9d3 | 259 | |
| charly | 0:3f4cfbeda9d3 | 260 | //delay(this->replyDelay); |
| charly | 0:3f4cfbeda9d3 | 261 | wait_us(this->replyDelay*1000*2); |
| charly | 0:3f4cfbeda9d3 | 262 | |
| charly | 0:3f4cfbeda9d3 | 263 | // scan for the rx frame and read it |
| charly | 0:3f4cfbeda9d3 | 264 | |
| charly | 0:3f4cfbeda9d3 | 265 | // uint32_t ms = millis(); |
| charly | 0:3f4cfbeda9d3 | 266 | uint32_t sync_target = (static_cast<uint32_t>(datagram[0])<<16) | 0xFF00 | datagram[2]; |
| charly | 0:3f4cfbeda9d3 | 267 | uint32_t sync = 0; |
| charly | 0:3f4cfbeda9d3 | 268 | |
| charly | 0:3f4cfbeda9d3 | 269 | |
| charly | 0:3f4cfbeda9d3 | 270 | do { |
| charly | 0:3f4cfbeda9d3 | 271 | /* uint32_t ms2 = millis(); |
| charly | 0:3f4cfbeda9d3 | 272 | if (ms2 != ms) { |
| charly | 0:3f4cfbeda9d3 | 273 | // 1ms tick |
| charly | 0:3f4cfbeda9d3 | 274 | ms = ms2; |
| charly | 0:3f4cfbeda9d3 | 275 | timeout--; |
| charly | 0:3f4cfbeda9d3 | 276 | } |
| charly | 0:3f4cfbeda9d3 | 277 | if (!timeout) return 0; |
| charly | 0:3f4cfbeda9d3 | 278 | */ |
| charly | 0:3f4cfbeda9d3 | 279 | int16_t res = serial_read(); |
| charly | 0:3f4cfbeda9d3 | 280 | if (res < 0) continue; |
| charly | 0:3f4cfbeda9d3 | 281 | |
| charly | 0:3f4cfbeda9d3 | 282 | sync <<= 8; |
| charly | 0:3f4cfbeda9d3 | 283 | sync |= res & 0xFF; |
| charly | 0:3f4cfbeda9d3 | 284 | sync &= 0xFFFFFF; |
| charly | 0:3f4cfbeda9d3 | 285 | |
| charly | 0:3f4cfbeda9d3 | 286 | } while (sync != sync_target); |
| charly | 0:3f4cfbeda9d3 | 287 | |
| charly | 0:3f4cfbeda9d3 | 288 | uint64_t out = sync; |
| charly | 0:3f4cfbeda9d3 | 289 | // ms = millis(); |
| charly | 0:3f4cfbeda9d3 | 290 | // timeout = this->abort_window; |
| charly | 0:3f4cfbeda9d3 | 291 | |
| charly | 0:3f4cfbeda9d3 | 292 | for(uint8_t i=0; i<5;) { |
| charly | 0:3f4cfbeda9d3 | 293 | /* uint32_t ms2 = millis(); |
| charly | 0:3f4cfbeda9d3 | 294 | if (ms2 != ms) { |
| charly | 0:3f4cfbeda9d3 | 295 | // 1ms tick |
| charly | 0:3f4cfbeda9d3 | 296 | ms = ms2; |
| charly | 0:3f4cfbeda9d3 | 297 | timeout--; |
| charly | 0:3f4cfbeda9d3 | 298 | } |
| charly | 0:3f4cfbeda9d3 | 299 | if (!timeout) return 0; |
| charly | 0:3f4cfbeda9d3 | 300 | */ |
| charly | 0:3f4cfbeda9d3 | 301 | int16_t res = serial_read(); |
| charly | 0:3f4cfbeda9d3 | 302 | if (res < 0) continue; |
| charly | 0:3f4cfbeda9d3 | 303 | |
| charly | 0:3f4cfbeda9d3 | 304 | out <<= 8; |
| charly | 0:3f4cfbeda9d3 | 305 | out |= res & 0xFF; |
| charly | 0:3f4cfbeda9d3 | 306 | |
| charly | 0:3f4cfbeda9d3 | 307 | i++; |
| charly | 0:3f4cfbeda9d3 | 308 | } |
| charly | 0:3f4cfbeda9d3 | 309 | |
| charly | 0:3f4cfbeda9d3 | 310 | #if defined(ARDUINO_ARCH_AVR) |
| charly | 0:3f4cfbeda9d3 | 311 | if (RXTX_pin > 0) { |
| charly | 0:3f4cfbeda9d3 | 312 | digitalWrite(RXTX_pin, HIGH); |
| charly | 0:3f4cfbeda9d3 | 313 | pinMode(RXTX_pin, OUTPUT); |
| charly | 0:3f4cfbeda9d3 | 314 | } |
| charly | 0:3f4cfbeda9d3 | 315 | #endif |
| charly | 0:3f4cfbeda9d3 | 316 | |
| charly | 0:3f4cfbeda9d3 | 317 | // while (available() > 0) serial_read(); // Flush |
| charly | 0:3f4cfbeda9d3 | 318 | SWSerial->sync(); |
| charly | 0:3f4cfbeda9d3 | 319 | |
| charly | 0:3f4cfbeda9d3 | 320 | return out; |
| charly | 0:3f4cfbeda9d3 | 321 | |
| charly | 0:3f4cfbeda9d3 | 322 | |
| charly | 0:3f4cfbeda9d3 | 323 | } |
| charly | 0:3f4cfbeda9d3 | 324 | |
| charly | 0:3f4cfbeda9d3 | 325 | uint32_t TMC2208Stepper::read(uint8_t addr) { |
| charly | 0:3f4cfbeda9d3 | 326 | constexpr uint8_t len = 3; |
| charly | 0:3f4cfbeda9d3 | 327 | addr |= TMC_READ; |
| charly | 0:3f4cfbeda9d3 | 328 | uint8_t datagram[] = {TMC2208_SYNC, slave_address, addr, 0x00}; |
| charly | 0:3f4cfbeda9d3 | 329 | datagram[len] = calcCRC(datagram, len); |
| charly | 0:3f4cfbeda9d3 | 330 | uint64_t out = 0x00000000UL; |
| charly | 0:3f4cfbeda9d3 | 331 | |
| charly | 0:3f4cfbeda9d3 | 332 | for (uint8_t i = 0; i < max_retries; i++) { |
| charly | 0:3f4cfbeda9d3 | 333 | preReadCommunication(); |
| charly | 0:3f4cfbeda9d3 | 334 | out = _sendDatagram(datagram, len, abort_window); |
| charly | 0:3f4cfbeda9d3 | 335 | postReadCommunication(); |
| charly | 0:3f4cfbeda9d3 | 336 | |
| charly | 0:3f4cfbeda9d3 | 337 | // delay(replyDelay); |
| charly | 0:3f4cfbeda9d3 | 338 | // wait_us(replyDelay*1000); |
| charly | 0:3f4cfbeda9d3 | 339 | |
| charly | 0:3f4cfbeda9d3 | 340 | CRCerror = false; |
| charly | 0:3f4cfbeda9d3 | 341 | uint8_t out_datagram[] = { |
| charly | 0:3f4cfbeda9d3 | 342 | static_cast<uint8_t>(out>>56), |
| charly | 0:3f4cfbeda9d3 | 343 | static_cast<uint8_t>(out>>48), |
| charly | 0:3f4cfbeda9d3 | 344 | static_cast<uint8_t>(out>>40), |
| charly | 0:3f4cfbeda9d3 | 345 | static_cast<uint8_t>(out>>32), |
| charly | 0:3f4cfbeda9d3 | 346 | static_cast<uint8_t>(out>>24), |
| charly | 0:3f4cfbeda9d3 | 347 | static_cast<uint8_t>(out>>16), |
| charly | 0:3f4cfbeda9d3 | 348 | static_cast<uint8_t>(out>> 8), |
| charly | 0:3f4cfbeda9d3 | 349 | static_cast<uint8_t>(out>> 0) |
| charly | 0:3f4cfbeda9d3 | 350 | }; |
| charly | 0:3f4cfbeda9d3 | 351 | uint8_t crc = calcCRC(out_datagram, 7); |
| charly | 0:3f4cfbeda9d3 | 352 | if ((crc != static_cast<uint8_t>(out)) || crc == 0 ) { |
| charly | 0:3f4cfbeda9d3 | 353 | CRCerror = true; |
| charly | 0:3f4cfbeda9d3 | 354 | out = 0; |
| charly | 0:3f4cfbeda9d3 | 355 | } else { |
| charly | 0:3f4cfbeda9d3 | 356 | break; |
| charly | 0:3f4cfbeda9d3 | 357 | } |
| charly | 0:3f4cfbeda9d3 | 358 | } |
| charly | 0:3f4cfbeda9d3 | 359 | |
| charly | 0:3f4cfbeda9d3 | 360 | return out>>8; |
| charly | 0:3f4cfbeda9d3 | 361 | } |
| charly | 0:3f4cfbeda9d3 | 362 | |
| charly | 0:3f4cfbeda9d3 | 363 | uint8_t TMC2208Stepper::IFCNT() { |
| charly | 0:3f4cfbeda9d3 | 364 | return read(IFCNT_t::address); |
| charly | 0:3f4cfbeda9d3 | 365 | } |
| charly | 0:3f4cfbeda9d3 | 366 | |
| charly | 0:3f4cfbeda9d3 | 367 | void TMC2208Stepper::SLAVECONF(uint16_t input) { |
| charly | 0:3f4cfbeda9d3 | 368 | SLAVECONF_register.sr = input&0xF00; |
| charly | 0:3f4cfbeda9d3 | 369 | write(SLAVECONF_register.address, SLAVECONF_register.sr); |
| charly | 0:3f4cfbeda9d3 | 370 | } |
| charly | 0:3f4cfbeda9d3 | 371 | uint16_t TMC2208Stepper::SLAVECONF() { |
| charly | 0:3f4cfbeda9d3 | 372 | return SLAVECONF_register.sr; |
| charly | 0:3f4cfbeda9d3 | 373 | } |
| charly | 0:3f4cfbeda9d3 | 374 | void TMC2208Stepper::senddelay(uint8_t B) { SLAVECONF_register.senddelay = B; write(SLAVECONF_register.address, SLAVECONF_register.sr); } |
| charly | 0:3f4cfbeda9d3 | 375 | uint8_t TMC2208Stepper::senddelay() { return SLAVECONF_register.senddelay; } |
| charly | 0:3f4cfbeda9d3 | 376 | |
| charly | 0:3f4cfbeda9d3 | 377 | void TMC2208Stepper::OTP_PROG(uint16_t input) { |
| charly | 0:3f4cfbeda9d3 | 378 | write(OTP_PROG_t::address, input); |
| charly | 0:3f4cfbeda9d3 | 379 | } |
| charly | 0:3f4cfbeda9d3 | 380 | |
| charly | 0:3f4cfbeda9d3 | 381 | uint32_t TMC2208Stepper::OTP_READ() { |
| charly | 0:3f4cfbeda9d3 | 382 | return read(OTP_READ_t::address); |
| charly | 0:3f4cfbeda9d3 | 383 | } |
| charly | 0:3f4cfbeda9d3 | 384 | |
| charly | 0:3f4cfbeda9d3 | 385 | uint32_t TMC2208Stepper::IOIN() { |
| charly | 0:3f4cfbeda9d3 | 386 | return read(TMC2208_n::IOIN_t::address); |
| charly | 0:3f4cfbeda9d3 | 387 | } |
| charly | 0:3f4cfbeda9d3 | 388 | bool TMC2208Stepper::enn() { TMC2208_n::IOIN_t r{0}; r.sr = IOIN(); return r.enn; } |
| charly | 0:3f4cfbeda9d3 | 389 | bool TMC2208Stepper::ms1() { TMC2208_n::IOIN_t r{0}; r.sr = IOIN(); return r.ms1; } |
| charly | 0:3f4cfbeda9d3 | 390 | bool TMC2208Stepper::ms2() { TMC2208_n::IOIN_t r{0}; r.sr = IOIN(); return r.ms2; } |
| charly | 0:3f4cfbeda9d3 | 391 | bool TMC2208Stepper::diag() { TMC2208_n::IOIN_t r{0}; r.sr = IOIN(); return r.diag; } |
| charly | 0:3f4cfbeda9d3 | 392 | bool TMC2208Stepper::pdn_uart() { TMC2208_n::IOIN_t r{0}; r.sr = IOIN(); return r.pdn_uart; } |
| charly | 0:3f4cfbeda9d3 | 393 | bool TMC2208Stepper::step() { TMC2208_n::IOIN_t r{0}; r.sr = IOIN(); return r.step; } |
| charly | 0:3f4cfbeda9d3 | 394 | bool TMC2208Stepper::sel_a() { TMC2208_n::IOIN_t r{0}; r.sr = IOIN(); return r.sel_a; } |
| charly | 0:3f4cfbeda9d3 | 395 | bool TMC2208Stepper::dir() { TMC2208_n::IOIN_t r{0}; r.sr = IOIN(); return r.dir; } |
| charly | 0:3f4cfbeda9d3 | 396 | uint8_t TMC2208Stepper::version() { TMC2208_n::IOIN_t r{0}; r.sr = IOIN(); return r.version; } |
| charly | 0:3f4cfbeda9d3 | 397 | |
| charly | 0:3f4cfbeda9d3 | 398 | /* |
| charly | 0:3f4cfbeda9d3 | 399 | uint32_t TMC2224Stepper::IOIN() { |
| charly | 0:3f4cfbeda9d3 | 400 | return read(TMC2224_n::IOIN_t::address); |
| charly | 0:3f4cfbeda9d3 | 401 | } |
| charly | 0:3f4cfbeda9d3 | 402 | bool TMC2224Stepper::enn() { TMC2224_n::IOIN_t r{0}; r.sr = IOIN(); return r.enn; } |
| charly | 0:3f4cfbeda9d3 | 403 | bool TMC2224Stepper::ms1() { TMC2224_n::IOIN_t r{0}; r.sr = IOIN(); return r.ms1; } |
| charly | 0:3f4cfbeda9d3 | 404 | bool TMC2224Stepper::ms2() { TMC2224_n::IOIN_t r{0}; r.sr = IOIN(); return r.ms2; } |
| charly | 0:3f4cfbeda9d3 | 405 | bool TMC2224Stepper::pdn_uart() { TMC2224_n::IOIN_t r{0}; r.sr = IOIN(); return r.pdn_uart; } |
| charly | 0:3f4cfbeda9d3 | 406 | bool TMC2224Stepper::spread() { TMC2224_n::IOIN_t r{0}; r.sr = IOIN(); return r.spread; } |
| charly | 0:3f4cfbeda9d3 | 407 | bool TMC2224Stepper::step() { TMC2224_n::IOIN_t r{0}; r.sr = IOIN(); return r.step; } |
| charly | 0:3f4cfbeda9d3 | 408 | bool TMC2224Stepper::sel_a() { TMC2224_n::IOIN_t r{0}; r.sr = IOIN(); return r.sel_a; } |
| charly | 0:3f4cfbeda9d3 | 409 | bool TMC2224Stepper::dir() { TMC2224_n::IOIN_t r{0}; r.sr = IOIN(); return r.dir; } |
| charly | 0:3f4cfbeda9d3 | 410 | uint8_t TMC2224Stepper::version() { TMC2224_n::IOIN_t r{0}; r.sr = IOIN(); return r.version; } |
| charly | 0:3f4cfbeda9d3 | 411 | */ |
| charly | 0:3f4cfbeda9d3 | 412 | uint16_t TMC2208Stepper::FACTORY_CONF() { |
| charly | 0:3f4cfbeda9d3 | 413 | return read(FACTORY_CONF_register.address); |
| charly | 0:3f4cfbeda9d3 | 414 | } |
| charly | 0:3f4cfbeda9d3 | 415 | void TMC2208Stepper::FACTORY_CONF(uint16_t input) { |
| charly | 0:3f4cfbeda9d3 | 416 | FACTORY_CONF_register.sr = input; |
| charly | 0:3f4cfbeda9d3 | 417 | write(FACTORY_CONF_register.address, FACTORY_CONF_register.sr); |
| charly | 0:3f4cfbeda9d3 | 418 | } |
| charly | 0:3f4cfbeda9d3 | 419 | void TMC2208Stepper::fclktrim(uint8_t B){ FACTORY_CONF_register.fclktrim = B; write(FACTORY_CONF_register.address, FACTORY_CONF_register.sr); } |
| charly | 0:3f4cfbeda9d3 | 420 | void TMC2208Stepper::ottrim(uint8_t B) { FACTORY_CONF_register.ottrim = B; write(FACTORY_CONF_register.address, FACTORY_CONF_register.sr); } |
| charly | 0:3f4cfbeda9d3 | 421 | uint8_t TMC2208Stepper::fclktrim() { FACTORY_CONF_t r{0}; r.sr = FACTORY_CONF(); return r.fclktrim; } |
| charly | 0:3f4cfbeda9d3 | 422 | uint8_t TMC2208Stepper::ottrim() { FACTORY_CONF_t r{0}; r.sr = FACTORY_CONF(); return r.ottrim; } |
| charly | 0:3f4cfbeda9d3 | 423 | |
| charly | 0:3f4cfbeda9d3 | 424 | void TMC2208Stepper::VACTUAL(uint32_t input) { |
| charly | 0:3f4cfbeda9d3 | 425 | VACTUAL_register.sr = input; |
| charly | 0:3f4cfbeda9d3 | 426 | write(VACTUAL_register.address, VACTUAL_register.sr); |
| charly | 0:3f4cfbeda9d3 | 427 | } |
| charly | 0:3f4cfbeda9d3 | 428 | uint32_t TMC2208Stepper::VACTUAL() { |
| charly | 0:3f4cfbeda9d3 | 429 | return VACTUAL_register.sr; |
| charly | 0:3f4cfbeda9d3 | 430 | } |
| charly | 0:3f4cfbeda9d3 | 431 | |
| charly | 0:3f4cfbeda9d3 | 432 | uint32_t TMC2208Stepper::PWM_SCALE() { |
| charly | 0:3f4cfbeda9d3 | 433 | return read(TMC2208_n::PWM_SCALE_t::address); |
| charly | 0:3f4cfbeda9d3 | 434 | } |
| charly | 0:3f4cfbeda9d3 | 435 | uint8_t TMC2208Stepper::pwm_scale_sum() { |
| charly | 0:3f4cfbeda9d3 | 436 | TMC2208_n::PWM_SCALE_t r{0}; |
| charly | 0:3f4cfbeda9d3 | 437 | r.sr = PWM_SCALE(); |
| charly | 0:3f4cfbeda9d3 | 438 | return r.pwm_scale_sum; |
| charly | 0:3f4cfbeda9d3 | 439 | } |
| charly | 0:3f4cfbeda9d3 | 440 | |
| charly | 0:3f4cfbeda9d3 | 441 | int16_t TMC2208Stepper::pwm_scale_auto() { |
| charly | 0:3f4cfbeda9d3 | 442 | TMC2208_n::PWM_SCALE_t r{0}; |
| charly | 0:3f4cfbeda9d3 | 443 | r.sr = PWM_SCALE(); |
| charly | 0:3f4cfbeda9d3 | 444 | return r.pwm_scale_auto; |
| charly | 0:3f4cfbeda9d3 | 445 | // Not two's complement? 9nth bit determines sign |
| charly | 0:3f4cfbeda9d3 | 446 | /* |
| charly | 0:3f4cfbeda9d3 | 447 | uint32_t d = PWM_SCALE(); |
| charly | 0:3f4cfbeda9d3 | 448 | int16_t response = (d>>PWM_SCALE_AUTO_bp)&0xFF; |
| charly | 0:3f4cfbeda9d3 | 449 | if (((d&PWM_SCALE_AUTO_bm) >> 24) & 0x1) return -response; |
| charly | 0:3f4cfbeda9d3 | 450 | else return response; |
| charly | 0:3f4cfbeda9d3 | 451 | */ |
| charly | 0:3f4cfbeda9d3 | 452 | } |
| charly | 0:3f4cfbeda9d3 | 453 | |
| charly | 0:3f4cfbeda9d3 | 454 | // R: PWM_AUTO |
| charly | 0:3f4cfbeda9d3 | 455 | uint32_t TMC2208Stepper::PWM_AUTO() { |
| charly | 0:3f4cfbeda9d3 | 456 | return read(PWM_AUTO_t::address); |
| charly | 0:3f4cfbeda9d3 | 457 | } |
| charly | 0:3f4cfbeda9d3 | 458 | uint8_t TMC2208Stepper::pwm_ofs_auto() { PWM_AUTO_t r{0}; r.sr = PWM_AUTO(); return r.pwm_ofs_auto; } |
| charly | 0:3f4cfbeda9d3 | 459 | uint8_t TMC2208Stepper::pwm_grad_auto() { PWM_AUTO_t r{0}; r.sr = PWM_AUTO(); return r.pwm_grad_auto; } |