1
1
/*
2
- Advanced example of using bstracted transport for reading and writing
2
+ Advanced example of using bstracted transport for reading and writing
3
3
register data from a UART-based device such as a TMC2209
4
4
5
- Written with help by Claude! https://claude.ai/chat/335f50b1-3dd8-435e-9139-57ec7ca26a3c
6
- (at this time chats are not shareable :(
5
+ Written with help by Claude!
6
+ https://claude.ai/chat/335f50b1-3dd8-435e-9139-57ec7ca26a3c (at this time
7
+ chats are not shareable :(
7
8
*/
8
9
9
-
10
10
#include " Adafruit_BusIO_Register.h"
11
11
#include " Adafruit_GenericDevice.h"
12
12
13
13
// Debugging macros
14
- // #define DEBUG_SERIAL Serial
14
+ #define DEBUG_SERIAL Serial
15
15
16
16
#ifdef DEBUG_SERIAL
17
17
#define DEBUG_PRINT (x ) DEBUG_SERIAL.print(x)
29
29
#define DEBUG_PRINT_HEX (x )
30
30
#endif
31
31
32
- // Add IOIN register definition
33
32
#define TMC2209_IOIN 0x06
34
33
35
34
class TMC2209_UART {
36
35
private:
37
- static TMC2209_UART *_instance;
38
36
Stream *_uart_stream;
39
37
uint8_t _addr;
40
38
41
- static bool uart_read_impl (uint8_t *buffer, size_t len) {
42
- return _instance->uart_read_fn (buffer, len);
43
- }
44
-
45
- static bool uart_write_impl (const uint8_t *buffer, size_t len) {
46
- return _instance->uart_write_fn (buffer, len);
47
- }
48
-
49
- static bool uart_readreg_impl (uint8_t *addr_buf, uint8_t addrsiz,
50
- uint8_t *data, uint16_t datalen) {
51
- return _instance->uart_readreg_fn (addr_buf, addrsiz, data, datalen);
52
- }
53
-
54
- static bool uart_writereg_impl (uint8_t *addr_buf, uint8_t addrsiz,
55
- const uint8_t *data, uint16_t datalen) {
56
- return _instance->uart_writereg_fn (addr_buf, addrsiz, data, datalen);
57
- }
58
-
59
- bool uart_read_fn (uint8_t *buffer, size_t len) {
39
+ static bool uart_read (void *thiz, uint8_t *buffer, size_t len) {
40
+ TMC2209_UART *dev = (TMC2209_UART *)thiz;
60
41
uint16_t timeout = 100 ;
61
- while (_uart_stream->available () < len && timeout--) {
42
+ while (dev-> _uart_stream ->available () < len && timeout--) {
62
43
delay (1 );
63
44
}
64
45
if (timeout == 0 ) {
@@ -68,39 +49,41 @@ private:
68
49
69
50
DEBUG_PRINT (" Reading: " );
70
51
for (size_t i = 0 ; i < len; i++) {
71
- buffer[i] = _uart_stream->read ();
52
+ buffer[i] = dev-> _uart_stream ->read ();
72
53
DEBUG_PRINT_HEX (buffer[i]);
73
54
}
74
55
DEBUG_PRINTLN (" " );
75
56
76
57
return true ;
77
58
}
78
59
79
- bool uart_write_fn (const uint8_t *buffer, size_t len) {
60
+ static bool uart_write (void *thiz, const uint8_t *buffer, size_t len) {
61
+ TMC2209_UART *dev = (TMC2209_UART *)thiz;
80
62
DEBUG_PRINT (" Writing: " );
81
63
for (size_t i = 0 ; i < len; i++) {
82
64
DEBUG_PRINT_HEX (buffer[i]);
83
65
}
84
66
DEBUG_PRINTLN (" " );
85
67
86
- _uart_stream->write (buffer, len);
68
+ dev-> _uart_stream ->write (buffer, len);
87
69
return true ;
88
70
}
89
71
90
- bool uart_readreg_fn (uint8_t *addr_buf, uint8_t addrsiz, uint8_t *data,
91
- uint16_t datalen) {
92
- while (_uart_stream->available ())
93
- _uart_stream->read ();
72
+ static bool uart_readreg (void *thiz, uint8_t *addr_buf, uint8_t addrsiz,
73
+ uint8_t *data, uint16_t datalen) {
74
+ TMC2209_UART *dev = (TMC2209_UART *)thiz;
75
+ while (dev->_uart_stream ->available ())
76
+ dev->_uart_stream ->read ();
94
77
95
- uint8_t packet[4 ] = {0x05 , uint8_t (_addr << 1 ), addr_buf[0 ], 0x00 };
78
+ uint8_t packet[4 ] = {0x05 , uint8_t (dev-> _addr << 1 ), addr_buf[0 ], 0x00 };
96
79
97
80
packet[3 ] = calcCRC (packet, 3 );
98
- if (!uart_write_impl ( packet, 4 ))
81
+ if (!uart_write (thiz, packet, 4 ))
99
82
return false ;
100
83
101
84
// Read back echo
102
85
uint8_t echo[4 ];
103
- if (!uart_read_impl ( echo, 4 ))
86
+ if (!uart_read (thiz, echo, 4 ))
104
87
return false ;
105
88
106
89
// Verify echo
@@ -112,7 +95,7 @@ private:
112
95
}
113
96
114
97
uint8_t response[8 ]; // sync + 0xFF + reg + 4 data bytes + CRC
115
- if (!uart_read_impl ( response, 8 ))
98
+ if (!uart_read (thiz, response, 8 ))
116
99
return false ;
117
100
118
101
// Verify response
@@ -121,38 +104,34 @@ private:
121
104
return false ;
122
105
}
123
106
124
- // Verify 0xFF address byte
125
107
if (response[1 ] != 0xFF ) {
126
108
DEBUG_PRINTLN (" Invalid reply address" );
127
109
return false ;
128
110
}
129
111
130
- // Verify register address matches our request
131
112
if (response[2 ] != addr_buf[0 ]) {
132
113
DEBUG_PRINTLN (" Register mismatch" );
133
114
return false ;
134
115
}
135
116
136
- // Verify CRC
137
- uint8_t crc = calcCRC (response, 7 ); // Calculate CRC of all but last byte
117
+ uint8_t crc = calcCRC (response, 7 );
138
118
if (crc != response[7 ]) {
139
119
DEBUG_PRINTLN (" CRC mismatch" );
140
120
return false ;
141
121
}
142
122
143
- // Copy the data bytes
144
123
memcpy (data, &response[3 ], 4 );
145
-
146
124
return true ;
147
125
}
148
126
149
- bool uart_writereg_fn (uint8_t *addr_buf, uint8_t addrsiz, const uint8_t *data,
150
- uint16_t datalen) {
151
- while (_uart_stream->available ())
152
- _uart_stream->read ();
127
+ static bool uart_writereg (void *thiz, uint8_t *addr_buf, uint8_t addrsiz,
128
+ const uint8_t *data, uint16_t datalen) {
129
+ TMC2209_UART *dev = (TMC2209_UART *)thiz;
130
+ while (dev->_uart_stream ->available ())
131
+ dev->_uart_stream ->read ();
153
132
154
133
uint8_t packet[8 ] = {0x05 ,
155
- uint8_t (_addr << 1 ),
134
+ uint8_t (dev-> _addr << 1 ),
156
135
uint8_t (addr_buf[0 ] | 0x80 ),
157
136
data[0 ],
158
137
data[1 ],
@@ -161,15 +140,13 @@ private:
161
140
0x00 };
162
141
163
142
packet[7 ] = calcCRC (packet, 7 );
164
- if (!uart_write_impl ( packet, 8 ))
143
+ if (!uart_write (thiz, packet, 8 ))
165
144
return false ;
166
145
167
- // Read and verify echo
168
146
uint8_t echo[8 ];
169
- if (!uart_read_impl ( echo, 8 ))
147
+ if (!uart_read (thiz, echo, 8 ))
170
148
return false ;
171
149
172
- // Verify echo matches what we sent
173
150
for (uint8_t i = 0 ; i < 8 ; i++) {
174
151
if (echo[i] != packet[i]) {
175
152
DEBUG_PRINTLN (" Write echo mismatch" );
@@ -198,18 +175,14 @@ private:
198
175
199
176
public:
200
177
TMC2209_UART (Stream *serial, uint8_t addr)
201
- : _uart_stream(serial), _addr(addr) {
202
- _instance = this ;
203
- }
178
+ : _uart_stream(serial), _addr(addr) {}
204
179
205
180
Adafruit_GenericDevice *createDevice () {
206
- return new Adafruit_GenericDevice (uart_read_impl, uart_write_impl ,
207
- uart_readreg_impl, uart_writereg_impl );
181
+ return new Adafruit_GenericDevice (this , uart_read, uart_write, uart_readreg ,
182
+ uart_writereg );
208
183
}
209
184
};
210
185
211
- TMC2209_UART *TMC2209_UART::_instance = nullptr ;
212
-
213
186
void setup () {
214
187
Serial.begin (115200 );
215
188
while (!Serial)
@@ -232,7 +205,7 @@ void setup() {
232
205
Serial.print (" IOIN = 0x" );
233
206
Serial.println (ioin_reg.read (), HEX);
234
207
235
- // Create RegisterBits for VERSION field (bits 28 :24)
208
+ // Create RegisterBits for VERSION field (bits 31 :24)
236
209
Adafruit_BusIO_RegisterBits version_bits (
237
210
&ioin_reg, 8 , 24 ); // 8 bits wide, starting at bit 24
238
211
@@ -243,4 +216,4 @@ void setup() {
243
216
Serial.println (version, HEX);
244
217
}
245
218
246
- void loop () { delay (1000 ); }
219
+ void loop () { delay (1000 ); }
0 commit comments