|
| 1 | +#include "Adafruit_BusIO_Register.h" |
| 2 | +#include "Adafruit_GenericDevice.h" |
| 3 | + |
| 4 | +// Debugging macros |
| 5 | +//#define DEBUG_SERIAL Serial |
| 6 | + |
| 7 | +#ifdef DEBUG_SERIAL |
| 8 | +#define DEBUG_PRINT(x) DEBUG_SERIAL.print(x) |
| 9 | +#define DEBUG_PRINTLN(x) DEBUG_SERIAL.println(x) |
| 10 | +#define DEBUG_PRINT_HEX(x) \ |
| 11 | + do { \ |
| 12 | + if (x < 0x10) \ |
| 13 | + DEBUG_SERIAL.print('0'); \ |
| 14 | + DEBUG_SERIAL.print(x, HEX); \ |
| 15 | + DEBUG_SERIAL.print(' '); \ |
| 16 | + } while (0) |
| 17 | +#else |
| 18 | +#define DEBUG_PRINT(x) |
| 19 | +#define DEBUG_PRINTLN(x) |
| 20 | +#define DEBUG_PRINT_HEX(x) |
| 21 | +#endif |
| 22 | + |
| 23 | +// Add IOIN register definition |
| 24 | +#define TMC2209_IOIN 0x06 |
| 25 | + |
| 26 | +class TMC2209_UART { |
| 27 | +private: |
| 28 | + static TMC2209_UART *_instance; |
| 29 | + Stream *_uart_stream; |
| 30 | + uint8_t _addr; |
| 31 | + |
| 32 | + static bool uart_read_impl(uint8_t *buffer, size_t len) { |
| 33 | + return _instance->uart_read_fn(buffer, len); |
| 34 | + } |
| 35 | + |
| 36 | + static bool uart_write_impl(const uint8_t *buffer, size_t len) { |
| 37 | + return _instance->uart_write_fn(buffer, len); |
| 38 | + } |
| 39 | + |
| 40 | + static bool uart_readreg_impl(uint8_t *addr_buf, uint8_t addrsiz, |
| 41 | + uint8_t *data, uint16_t datalen) { |
| 42 | + return _instance->uart_readreg_fn(addr_buf, addrsiz, data, datalen); |
| 43 | + } |
| 44 | + |
| 45 | + static bool uart_writereg_impl(uint8_t *addr_buf, uint8_t addrsiz, |
| 46 | + const uint8_t *data, uint16_t datalen) { |
| 47 | + return _instance->uart_writereg_fn(addr_buf, addrsiz, data, datalen); |
| 48 | + } |
| 49 | + |
| 50 | + bool uart_read_fn(uint8_t *buffer, size_t len) { |
| 51 | + uint16_t timeout = 100; |
| 52 | + while (_uart_stream->available() < len && timeout--) { |
| 53 | + delay(1); |
| 54 | + } |
| 55 | + if (timeout == 0) { |
| 56 | + DEBUG_PRINTLN("Read timeout!"); |
| 57 | + return false; |
| 58 | + } |
| 59 | + |
| 60 | + DEBUG_PRINT("Reading: "); |
| 61 | + for (size_t i = 0; i < len; i++) { |
| 62 | + buffer[i] = _uart_stream->read(); |
| 63 | + DEBUG_PRINT_HEX(buffer[i]); |
| 64 | + } |
| 65 | + DEBUG_PRINTLN(""); |
| 66 | + |
| 67 | + return true; |
| 68 | + } |
| 69 | + |
| 70 | + bool uart_write_fn(const uint8_t *buffer, size_t len) { |
| 71 | + DEBUG_PRINT("Writing: "); |
| 72 | + for (size_t i = 0; i < len; i++) { |
| 73 | + DEBUG_PRINT_HEX(buffer[i]); |
| 74 | + } |
| 75 | + DEBUG_PRINTLN(""); |
| 76 | + |
| 77 | + _uart_stream->write(buffer, len); |
| 78 | + return true; |
| 79 | + } |
| 80 | + |
| 81 | + bool uart_readreg_fn(uint8_t *addr_buf, uint8_t addrsiz, uint8_t *data, |
| 82 | + uint16_t datalen) { |
| 83 | + while (_uart_stream->available()) |
| 84 | + _uart_stream->read(); |
| 85 | + |
| 86 | + uint8_t packet[4] = {0x05, uint8_t(_addr << 1), addr_buf[0], 0x00}; |
| 87 | + |
| 88 | + packet[3] = calcCRC(packet, 3); |
| 89 | + if (!uart_write_impl(packet, 4)) |
| 90 | + return false; |
| 91 | + |
| 92 | + // Read back echo |
| 93 | + uint8_t echo[4]; |
| 94 | + if (!uart_read_impl(echo, 4)) |
| 95 | + return false; |
| 96 | + |
| 97 | + // Verify echo |
| 98 | + for (uint8_t i = 0; i < 4; i++) { |
| 99 | + if (echo[i] != packet[i]) { |
| 100 | + DEBUG_PRINTLN("Echo mismatch"); |
| 101 | + return false; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + uint8_t response[8]; // sync + 0xFF + reg + 4 data bytes + CRC |
| 106 | + if (!uart_read_impl(response, 8)) |
| 107 | + return false; |
| 108 | + |
| 109 | + // Verify response |
| 110 | + if (response[0] != 0x05) { |
| 111 | + DEBUG_PRINTLN("Invalid sync byte"); |
| 112 | + return false; |
| 113 | + } |
| 114 | + |
| 115 | + // Verify 0xFF address byte |
| 116 | + if (response[1] != 0xFF) { |
| 117 | + DEBUG_PRINTLN("Invalid reply address"); |
| 118 | + return false; |
| 119 | + } |
| 120 | + |
| 121 | + // Verify register address matches our request |
| 122 | + if (response[2] != addr_buf[0]) { |
| 123 | + DEBUG_PRINTLN("Register mismatch"); |
| 124 | + return false; |
| 125 | + } |
| 126 | + |
| 127 | + // Verify CRC |
| 128 | + uint8_t crc = calcCRC(response, 7); // Calculate CRC of all but last byte |
| 129 | + if (crc != response[7]) { |
| 130 | + DEBUG_PRINTLN("CRC mismatch"); |
| 131 | + return false; |
| 132 | + } |
| 133 | + |
| 134 | + // Copy the data bytes |
| 135 | + memcpy(data, &response[3], 4); |
| 136 | + |
| 137 | + return true; |
| 138 | + } |
| 139 | + |
| 140 | + bool uart_writereg_fn(uint8_t *addr_buf, uint8_t addrsiz, const uint8_t *data, |
| 141 | + uint16_t datalen) { |
| 142 | + while (_uart_stream->available()) |
| 143 | + _uart_stream->read(); |
| 144 | + |
| 145 | + uint8_t packet[8] = {0x05, |
| 146 | + uint8_t(_addr << 1), |
| 147 | + uint8_t(addr_buf[0] | 0x80), |
| 148 | + data[0], |
| 149 | + data[1], |
| 150 | + data[2], |
| 151 | + data[3], |
| 152 | + 0x00}; |
| 153 | + |
| 154 | + packet[7] = calcCRC(packet, 7); |
| 155 | + if (!uart_write_impl(packet, 8)) |
| 156 | + return false; |
| 157 | + |
| 158 | + // Read and verify echo |
| 159 | + uint8_t echo[8]; |
| 160 | + if (!uart_read_impl(echo, 8)) |
| 161 | + return false; |
| 162 | + |
| 163 | + // Verify echo matches what we sent |
| 164 | + for (uint8_t i = 0; i < 8; i++) { |
| 165 | + if (echo[i] != packet[i]) { |
| 166 | + DEBUG_PRINTLN("Write echo mismatch"); |
| 167 | + return false; |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + return true; |
| 172 | + } |
| 173 | + |
| 174 | + static uint8_t calcCRC(uint8_t *data, uint8_t length) { |
| 175 | + uint8_t crc = 0; |
| 176 | + for (uint8_t i = 0; i < length; i++) { |
| 177 | + uint8_t currentByte = data[i]; |
| 178 | + for (uint8_t j = 0; j < 8; j++) { |
| 179 | + if ((crc >> 7) ^ (currentByte & 0x01)) { |
| 180 | + crc = (crc << 1) ^ 0x07; |
| 181 | + } else { |
| 182 | + crc = crc << 1; |
| 183 | + } |
| 184 | + currentByte = currentByte >> 1; |
| 185 | + } |
| 186 | + } |
| 187 | + return crc; |
| 188 | + } |
| 189 | + |
| 190 | +public: |
| 191 | + TMC2209_UART(Stream *serial, uint8_t addr) |
| 192 | + : _uart_stream(serial), _addr(addr) { |
| 193 | + _instance = this; |
| 194 | + } |
| 195 | + |
| 196 | + Adafruit_GenericDevice *createDevice() { |
| 197 | + return new Adafruit_GenericDevice(uart_read_impl, uart_write_impl, |
| 198 | + uart_readreg_impl, uart_writereg_impl); |
| 199 | + } |
| 200 | +}; |
| 201 | + |
| 202 | +TMC2209_UART *TMC2209_UART::_instance = nullptr; |
| 203 | + |
| 204 | +void setup() { |
| 205 | + Serial.begin(115200); |
| 206 | + while (!Serial) |
| 207 | + ; |
| 208 | + delay(100); |
| 209 | + Serial.println("TMC2209 Generic Device register read/write test!"); |
| 210 | + |
| 211 | + Serial2.begin(115200); |
| 212 | + |
| 213 | + TMC2209_UART uart(&Serial2, 0); |
| 214 | + Adafruit_GenericDevice *device = uart.createDevice(); |
| 215 | + device->begin(); |
| 216 | + |
| 217 | + // Create register object for IOIN |
| 218 | + Adafruit_BusIO_Register ioin_reg(device, |
| 219 | + TMC2209_IOIN, // device and register address |
| 220 | + 4, // width = 4 bytes |
| 221 | + MSBFIRST, // byte order |
| 222 | + 1); // address width = 1 byte |
| 223 | + Serial.print("IOIN = 0x"); |
| 224 | + Serial.println(ioin_reg.read(), HEX); |
| 225 | + |
| 226 | + // Create RegisterBits for VERSION field (bits 28:24) |
| 227 | + Adafruit_BusIO_RegisterBits version_bits( |
| 228 | + &ioin_reg, 8, 24); // 8 bits wide, starting at bit 24 |
| 229 | + |
| 230 | + Serial.println("Reading VERSION..."); |
| 231 | + uint8_t version = version_bits.read(); |
| 232 | + |
| 233 | + Serial.print("VERSION = 0x"); |
| 234 | + Serial.println(version, HEX); |
| 235 | +} |
| 236 | + |
| 237 | +void loop() { delay(1000); } |
0 commit comments