Skip to content

Commit 70ad317

Browse files
committed
some tests for uart mode
1 parent c602ea0 commit 70ad317

File tree

2 files changed

+304
-0
lines changed

2 files changed

+304
-0
lines changed
Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
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); }
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include "Adafruit_GenericDevice.h"
2+
3+
Stream *uart_stream; // Will hold the pointer to our Stream object
4+
5+
Adafruit_GenericDevice *create_uart_device(Stream *serial_port) {
6+
uart_stream = serial_port; // Store the Stream pointer
7+
8+
auto uart_write = [](const uint8_t *buffer, size_t len) -> bool {
9+
uart_stream->write(buffer, len);
10+
return true;
11+
};
12+
13+
auto uart_read = [](uint8_t *buffer, size_t len) -> bool {
14+
uint16_t timeout = 100;
15+
while (uart_stream->available() < len && timeout--) {
16+
delay(1);
17+
}
18+
if (timeout == 0) {
19+
return false;
20+
}
21+
for (size_t i = 0; i < len; i++) {
22+
buffer[i] = uart_stream->read();
23+
}
24+
return true;
25+
};
26+
27+
return new Adafruit_GenericDevice(uart_read, uart_write);
28+
}
29+
30+
void setup() {
31+
Serial.begin(115200);
32+
while (!Serial)
33+
;
34+
delay(100);
35+
36+
Serial.println("Generic Device test!");
37+
38+
Serial2.begin(115200);
39+
40+
Adafruit_GenericDevice *device = create_uart_device(&Serial2);
41+
device->begin();
42+
43+
uint8_t write_buf[4] = {0x5, 0x0, 0x0, 0x48};
44+
uint8_t read_buf[8];
45+
46+
Serial.println("Writing data...");
47+
if (!device->write(write_buf, 4)) {
48+
Serial.println("Write failed!");
49+
return;
50+
}
51+
52+
Serial.println("Reading response...");
53+
if (!device->read(read_buf, 8)) {
54+
Serial.println("Read failed!");
55+
return;
56+
}
57+
58+
Serial.print("Got response: ");
59+
for (int i = 0; i < 8; i++) {
60+
Serial.print("0x");
61+
Serial.print(read_buf[i], HEX);
62+
Serial.print(" ");
63+
}
64+
Serial.println();
65+
}
66+
67+
void loop() { delay(1000); }

0 commit comments

Comments
 (0)