genericdevice_uartregtest.ino 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. Advanced example of using bstracted transport for reading and writing
  3. register data from a UART-based device such as a TMC2209
  4. Written with help by Claude!
  5. https://claude.ai/chat/335f50b1-3dd8-435e-9139-57ec7ca26a3c (at this time
  6. chats are not shareable :(
  7. */
  8. #include "Adafruit_BusIO_Register.h"
  9. #include "Adafruit_GenericDevice.h"
  10. // Debugging macros
  11. #define DEBUG_SERIAL Serial
  12. #ifdef DEBUG_SERIAL
  13. #define DEBUG_PRINT(x) DEBUG_SERIAL.print(x)
  14. #define DEBUG_PRINTLN(x) DEBUG_SERIAL.println(x)
  15. #define DEBUG_PRINT_HEX(x) \
  16. do { \
  17. if (x < 0x10) \
  18. DEBUG_SERIAL.print('0'); \
  19. DEBUG_SERIAL.print(x, HEX); \
  20. DEBUG_SERIAL.print(' '); \
  21. } while (0)
  22. #else
  23. #define DEBUG_PRINT(x)
  24. #define DEBUG_PRINTLN(x)
  25. #define DEBUG_PRINT_HEX(x)
  26. #endif
  27. #define TMC2209_IOIN 0x06
  28. class TMC2209_UART {
  29. private:
  30. Stream *_uart_stream;
  31. uint8_t _addr;
  32. static bool uart_read(void *thiz, uint8_t *buffer, size_t len) {
  33. TMC2209_UART *dev = (TMC2209_UART *)thiz;
  34. uint16_t timeout = 100;
  35. while (dev->_uart_stream->available() < len && timeout--) {
  36. delay(1);
  37. }
  38. if (timeout == 0) {
  39. DEBUG_PRINTLN("Read timeout!");
  40. return false;
  41. }
  42. DEBUG_PRINT("Reading: ");
  43. for (size_t i = 0; i < len; i++) {
  44. buffer[i] = dev->_uart_stream->read();
  45. DEBUG_PRINT_HEX(buffer[i]);
  46. }
  47. DEBUG_PRINTLN("");
  48. return true;
  49. }
  50. static bool uart_write(void *thiz, const uint8_t *buffer, size_t len) {
  51. TMC2209_UART *dev = (TMC2209_UART *)thiz;
  52. DEBUG_PRINT("Writing: ");
  53. for (size_t i = 0; i < len; i++) {
  54. DEBUG_PRINT_HEX(buffer[i]);
  55. }
  56. DEBUG_PRINTLN("");
  57. dev->_uart_stream->write(buffer, len);
  58. return true;
  59. }
  60. static bool uart_readreg(void *thiz, uint8_t *addr_buf, uint8_t addrsiz,
  61. uint8_t *data, uint16_t datalen) {
  62. TMC2209_UART *dev = (TMC2209_UART *)thiz;
  63. while (dev->_uart_stream->available())
  64. dev->_uart_stream->read();
  65. uint8_t packet[4] = {0x05, uint8_t(dev->_addr << 1), addr_buf[0], 0x00};
  66. packet[3] = calcCRC(packet, 3);
  67. if (!uart_write(thiz, packet, 4))
  68. return false;
  69. // Read back echo
  70. uint8_t echo[4];
  71. if (!uart_read(thiz, echo, 4))
  72. return false;
  73. // Verify echo
  74. for (uint8_t i = 0; i < 4; i++) {
  75. if (echo[i] != packet[i]) {
  76. DEBUG_PRINTLN("Echo mismatch");
  77. return false;
  78. }
  79. }
  80. uint8_t response[8]; // sync + 0xFF + reg + 4 data bytes + CRC
  81. if (!uart_read(thiz, response, 8))
  82. return false;
  83. // Verify response
  84. if (response[0] != 0x05) {
  85. DEBUG_PRINTLN("Invalid sync byte");
  86. return false;
  87. }
  88. if (response[1] != 0xFF) {
  89. DEBUG_PRINTLN("Invalid reply address");
  90. return false;
  91. }
  92. if (response[2] != addr_buf[0]) {
  93. DEBUG_PRINTLN("Register mismatch");
  94. return false;
  95. }
  96. uint8_t crc = calcCRC(response, 7);
  97. if (crc != response[7]) {
  98. DEBUG_PRINTLN("CRC mismatch");
  99. return false;
  100. }
  101. memcpy(data, &response[3], 4);
  102. return true;
  103. }
  104. static bool uart_writereg(void *thiz, uint8_t *addr_buf, uint8_t addrsiz,
  105. const uint8_t *data, uint16_t datalen) {
  106. TMC2209_UART *dev = (TMC2209_UART *)thiz;
  107. while (dev->_uart_stream->available())
  108. dev->_uart_stream->read();
  109. uint8_t packet[8] = {0x05,
  110. uint8_t(dev->_addr << 1),
  111. uint8_t(addr_buf[0] | 0x80),
  112. data[0],
  113. data[1],
  114. data[2],
  115. data[3],
  116. 0x00};
  117. packet[7] = calcCRC(packet, 7);
  118. if (!uart_write(thiz, packet, 8))
  119. return false;
  120. uint8_t echo[8];
  121. if (!uart_read(thiz, echo, 8))
  122. return false;
  123. for (uint8_t i = 0; i < 8; i++) {
  124. if (echo[i] != packet[i]) {
  125. DEBUG_PRINTLN("Write echo mismatch");
  126. return false;
  127. }
  128. }
  129. return true;
  130. }
  131. static uint8_t calcCRC(uint8_t *data, uint8_t length) {
  132. uint8_t crc = 0;
  133. for (uint8_t i = 0; i < length; i++) {
  134. uint8_t currentByte = data[i];
  135. for (uint8_t j = 0; j < 8; j++) {
  136. if ((crc >> 7) ^ (currentByte & 0x01)) {
  137. crc = (crc << 1) ^ 0x07;
  138. } else {
  139. crc = crc << 1;
  140. }
  141. currentByte = currentByte >> 1;
  142. }
  143. }
  144. return crc;
  145. }
  146. public:
  147. TMC2209_UART(Stream *serial, uint8_t addr)
  148. : _uart_stream(serial), _addr(addr) {}
  149. Adafruit_GenericDevice *createDevice() {
  150. return new Adafruit_GenericDevice(this, uart_read, uart_write, uart_readreg,
  151. uart_writereg);
  152. }
  153. };
  154. void setup() {
  155. Serial.begin(115200);
  156. while (!Serial)
  157. ;
  158. delay(100);
  159. Serial.println("TMC2209 Generic Device register read/write test!");
  160. Serial1.begin(115200);
  161. TMC2209_UART uart(&Serial1, 0);
  162. Adafruit_GenericDevice *device = uart.createDevice();
  163. device->begin();
  164. // Create register object for IOIN
  165. Adafruit_BusIO_Register ioin_reg(device,
  166. TMC2209_IOIN, // device and register address
  167. 4, // width = 4 bytes
  168. MSBFIRST, // byte order
  169. 1); // address width = 1 byte
  170. Serial.print("IOIN = 0x");
  171. Serial.println(ioin_reg.read(), HEX);
  172. // Create RegisterBits for VERSION field (bits 31:24)
  173. Adafruit_BusIO_RegisterBits version_bits(
  174. &ioin_reg, 8, 24); // 8 bits wide, starting at bit 24
  175. Serial.println("Reading VERSION...");
  176. uint8_t version = version_bits.read();
  177. Serial.print("VERSION = 0x");
  178. Serial.println(version, HEX);
  179. }
  180. void loop() { delay(1000); }