11/*
2- Advanced example of using bstracted transport for reading and writing
2+ Advanced example of using bstracted transport for reading and writing
33 register data from a UART-based device such as a TMC2209
44
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 :(
78*/
89
9-
1010#include " Adafruit_BusIO_Register.h"
1111#include " Adafruit_GenericDevice.h"
1212
1313// Debugging macros
14- // #define DEBUG_SERIAL Serial
14+ #define DEBUG_SERIAL Serial
1515
1616#ifdef DEBUG_SERIAL
1717#define DEBUG_PRINT (x ) DEBUG_SERIAL.print(x)
2929#define DEBUG_PRINT_HEX (x )
3030#endif
3131
32- // Add IOIN register definition
3332#define TMC2209_IOIN 0x06
3433
3534class TMC2209_UART {
3635private:
37- static TMC2209_UART *_instance;
3836 Stream *_uart_stream;
3937 uint8_t _addr;
4038
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;
6041 uint16_t timeout = 100 ;
61- while (_uart_stream->available () < len && timeout--) {
42+ while (dev-> _uart_stream ->available () < len && timeout--) {
6243 delay (1 );
6344 }
6445 if (timeout == 0 ) {
@@ -68,39 +49,41 @@ private:
6849
6950 DEBUG_PRINT (" Reading: " );
7051 for (size_t i = 0 ; i < len; i++) {
71- buffer[i] = _uart_stream->read ();
52+ buffer[i] = dev-> _uart_stream ->read ();
7253 DEBUG_PRINT_HEX (buffer[i]);
7354 }
7455 DEBUG_PRINTLN (" " );
7556
7657 return true ;
7758 }
7859
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;
8062 DEBUG_PRINT (" Writing: " );
8163 for (size_t i = 0 ; i < len; i++) {
8264 DEBUG_PRINT_HEX (buffer[i]);
8365 }
8466 DEBUG_PRINTLN (" " );
8567
86- _uart_stream->write (buffer, len);
68+ dev-> _uart_stream ->write (buffer, len);
8769 return true ;
8870 }
8971
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 ();
9477
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 };
9679
9780 packet[3 ] = calcCRC (packet, 3 );
98- if (!uart_write_impl ( packet, 4 ))
81+ if (!uart_write (thiz, packet, 4 ))
9982 return false ;
10083
10184 // Read back echo
10285 uint8_t echo[4 ];
103- if (!uart_read_impl ( echo, 4 ))
86+ if (!uart_read (thiz, echo, 4 ))
10487 return false ;
10588
10689 // Verify echo
@@ -112,7 +95,7 @@ private:
11295 }
11396
11497 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 ))
11699 return false ;
117100
118101 // Verify response
@@ -121,38 +104,34 @@ private:
121104 return false ;
122105 }
123106
124- // Verify 0xFF address byte
125107 if (response[1 ] != 0xFF ) {
126108 DEBUG_PRINTLN (" Invalid reply address" );
127109 return false ;
128110 }
129111
130- // Verify register address matches our request
131112 if (response[2 ] != addr_buf[0 ]) {
132113 DEBUG_PRINTLN (" Register mismatch" );
133114 return false ;
134115 }
135116
136- // Verify CRC
137- uint8_t crc = calcCRC (response, 7 ); // Calculate CRC of all but last byte
117+ uint8_t crc = calcCRC (response, 7 );
138118 if (crc != response[7 ]) {
139119 DEBUG_PRINTLN (" CRC mismatch" );
140120 return false ;
141121 }
142122
143- // Copy the data bytes
144123 memcpy (data, &response[3 ], 4 );
145-
146124 return true ;
147125 }
148126
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 ();
153132
154133 uint8_t packet[8 ] = {0x05 ,
155- uint8_t (_addr << 1 ),
134+ uint8_t (dev-> _addr << 1 ),
156135 uint8_t (addr_buf[0 ] | 0x80 ),
157136 data[0 ],
158137 data[1 ],
@@ -161,15 +140,13 @@ private:
161140 0x00 };
162141
163142 packet[7 ] = calcCRC (packet, 7 );
164- if (!uart_write_impl ( packet, 8 ))
143+ if (!uart_write (thiz, packet, 8 ))
165144 return false ;
166145
167- // Read and verify echo
168146 uint8_t echo[8 ];
169- if (!uart_read_impl ( echo, 8 ))
147+ if (!uart_read (thiz, echo, 8 ))
170148 return false ;
171149
172- // Verify echo matches what we sent
173150 for (uint8_t i = 0 ; i < 8 ; i++) {
174151 if (echo[i] != packet[i]) {
175152 DEBUG_PRINTLN (" Write echo mismatch" );
@@ -198,18 +175,14 @@ private:
198175
199176public:
200177 TMC2209_UART (Stream *serial, uint8_t addr)
201- : _uart_stream(serial), _addr(addr) {
202- _instance = this ;
203- }
178+ : _uart_stream(serial), _addr(addr) {}
204179
205180 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 );
208183 }
209184};
210185
211- TMC2209_UART *TMC2209_UART::_instance = nullptr ;
212-
213186void setup () {
214187 Serial.begin (115200 );
215188 while (!Serial)
@@ -232,7 +205,7 @@ void setup() {
232205 Serial.print (" IOIN = 0x" );
233206 Serial.println (ioin_reg.read (), HEX);
234207
235- // Create RegisterBits for VERSION field (bits 28 :24)
208+ // Create RegisterBits for VERSION field (bits 31 :24)
236209 Adafruit_BusIO_RegisterBits version_bits (
237210 &ioin_reg, 8 , 24 ); // 8 bits wide, starting at bit 24
238211
@@ -243,4 +216,4 @@ void setup() {
243216 Serial.println (version, HEX);
244217}
245218
246- void loop () { delay (1000 ); }
219+ void loop () { delay (1000 ); }
0 commit comments