I am new to learn about TMC2300 and i am facing issue to communicate via UART.
i have used sample code available for testing but could not rad the GCONF register.
Is there something I am missing or any link will help me to progress.
Note: I have tested UART is able to send message using Looping method where i am trying to see what data is ESP sending through CP.
Code:
#include <HardwareSerial.h>
#define TMC_UART_RX 18 // ESP32-S3 RX pin connected to TMC2300 TX
#define TMC_UART_TX 17 // ESP32-S3 TX pin connected to TMC2300 RX
HardwareSerial TMC_UART(1); // Use UART1
// TMC2300 register addresses
#define GCONF 0x00
#define GSTAT 0x01
#define IOIN 0x06
#define CHOPCONF 0x6C
#define DRVSTATUS 0x6F
void setup() {
Serial.begin(115200); // Start serial communication with computer
while (!Serial) {
; // Wait for serial port to connect
}
Serial.println("TMC2300 UART Communication Test");
TMC_UART.begin(115200, SERIAL_8N1, TMC_UART_RX, TMC_UART_RX);
delay(1000); // Wait for TMC2300 to initialize
Serial.println("Testing register read/write...");
}
void loop() {
// Main loop is empty as we're doing all tests in setup
testRegisterReadWrite();
}
uint32_t readRegister(uint8_t addr, int maxRetries = 5) {
for (int retry = 0; retry < maxRetries; retry++) {
uint8_t data[8];
data[0] = 0x05; // Sync byte
data[1] = 0x00; // Slave address (0 for TMC2300)
data[2] = addr; // Register address
data[3] = 0x00; // Read operation
TMC_UART.write(data, 4);
TMC_UART.flush();
delay(50); // Wait for response
Serial.print("Attempt ");
Serial.print(retry + 1);
Serial.print(": Bytes available: ");
Serial.println(TMC_UART.available());
if (TMC_UART.available() >= 8) {
TMC_UART.readBytes(data, 8);
Serial.print("Received: ");
for (int i = 0; i < 8; i++) {
Serial.print(data[i], HEX);
Serial.print(" ");
}
Serial.println();
return (uint32_t)data[4] << 24 | (uint32_t)data[5] << 16 | (uint32_t)data[6] << 8 | data[7];
} else {
Serial.println("Not enough data received, retrying...");
// Clear any partial data
while (TMC_UART.available()) {
TMC_UART.read();
}
}
delay(100); // Wait before retry
}
Serial.println("Failed to read after maximum retries");
return 0; // Return 0 if read fails
}
void writeRegister(uint8_t addr, uint32_t value) {
uint8_t data[8];
data[0] = 0x05; // Sync byte
data[1] = 0x00; // Slave address (0 for TMC2300)
data[2] = addr | 0x80; // Register address with write bit set
data[3] = (value >> 24) & 0xFF;
data[4] = (value >> 16) & 0xFF;
data[5] = (value >> 8) & 0xFF;
data[6] = value & 0xFF;
data[7] = calcCRC(data, 7); // CRC
TMC_UART.write(data, 8);
TMC_UART.flush();
}
uint8_t calcCRC(uint8_t* datagram, uint8_t len) {
uint8_t crc = 0;
for (uint8_t i = 0; i < len; i++) {
uint8_t currentByte = datagram[i];
for (uint8_t j = 0; j < 8; j++) {
if ((crc >> 7) ^ (currentByte & 0x01)) {
crc = (crc << 1) ^ 0x07;
} else {
crc = (crc << 1);
}
currentByte = currentByte >> 1;
}
}
return crc;
}
void testRegisterReadWrite() {
writeRegister(GCONF, 0x00000004);
delay(5000);
// Test GCONF register
Serial.println("\nTesting GCONF register");
uint32_t gconf = readRegister(GCONF);
Serial.print("GCONF initial value: 0x");
Serial.println(gconf, HEX);
// // Test CHOPCONF register
// Serial.println("\nTesting CHOPCONF register");
// writeRegister(CHOPCONF, 0x10000053);
// delay(5000);
// uint32_t chopconf = readRegister(CHOPCONF);
// delay(1000);
// Serial.print("CHOPCONF after write: 0x");
// Serial.println(chopconf, HEX);
// // Test IHOLD_IRUN register
// Serial.println("\nTesting IHOLD_IRUN register");
// writeRegister(0x10, 0x00061F0A);
// delay(10);
// uint32_t ihold_irun = readRegister(0x10);
// Serial.print("IHOLD_IRUN after write: 0x");
// Serial.println(ihold_irun, HEX);
// // Test TPOWERDOWN register
// Serial.println("\nTesting TPOWERDOWN register");
// writeRegister(0x11, 0x0000000A);
// delay(10);
// uint32_t tpowerdown = readRegister(0x11);
// Serial.print("TPOWERDOWN after write: 0x");
// Serial.println(tpowerdown, HEX);
}