From 16c8ca1137f58a073879208e039dac40c1660d14 Mon Sep 17 00:00:00 2001 From: Craig Swank Date: Sun, 4 May 2025 08:53:44 -0600 Subject: [PATCH 1/4] Make write buffer big enough for crc --- tmc2209/uartcomm.go | 1 + 1 file changed, 1 insertion(+) diff --git a/tmc2209/uartcomm.go b/tmc2209/uartcomm.go index e99947fca..1a4ab14c5 100644 --- a/tmc2209/uartcomm.go +++ b/tmc2209/uartcomm.go @@ -57,6 +57,7 @@ func (comm *UARTComm) WriteRegister(register uint8, value uint32, driverIndex ui byte((value >> 16) & 0xFF), // Middle byte byte((value >> 8) & 0xFF), // Next byte byte(value & 0xFF), // LSB of value + 0, // CRC } // Calculate checksum by XORing all bytes From 3117772f8ed30cf2fcc72162892b3d4fd51b19b9 Mon Sep 17 00:00:00 2001 From: Craig Swank Date: Sun, 4 May 2025 11:44:23 -0600 Subject: [PATCH 2/4] crc according to datasheet --- tmc2209/uartcomm.go | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/tmc2209/uartcomm.go b/tmc2209/uartcomm.go index 1a4ab14c5..5dbdf54b6 100644 --- a/tmc2209/uartcomm.go +++ b/tmc2209/uartcomm.go @@ -60,12 +60,7 @@ func (comm *UARTComm) WriteRegister(register uint8, value uint32, driverIndex ui 0, // CRC } - // Calculate checksum by XORing all bytes - checksum := byte(0) - for _, b := range buffer[:7] { - checksum ^= b - } - buffer[7] = checksum // Set checksum byte + buffer[7] = comm.crc(buffer[:7]) // Write the data to the TMC2209 done := make(chan error, 1) @@ -84,13 +79,29 @@ func (comm *UARTComm) WriteRegister(register uint8, value uint32, driverIndex ui } } +func (comm *UARTComm) crc(buf []byte) byte { + crc := byte(0) + for i := range buf { + currentByte := buf[i] + for j := 0; j < 8; j++ { + if (crc>>7)^(currentByte&0x01) > 0 { + crc = (crc << 1) ^ 0x07 + } else { + crc = crc << 1 + } + currentByte = currentByte >> 1 + } + } + return crc +} + // ReadRegister sends a register read command to the TMC2209 with a timeout. func (comm *UARTComm) ReadRegister(register uint8, driverIndex uint8) (uint32, error) { var writeBuffer [4]byte - writeBuffer[0] = 0x05 // Sync byte - writeBuffer[1] = 0x00 // Slave address - writeBuffer[2] = register & 0x7F // Read command (MSB clear for read) - writeBuffer[3] = writeBuffer[0] ^ writeBuffer[1] ^ writeBuffer[2] // Checksum + writeBuffer[0] = 0x05 // Sync byte + writeBuffer[1] = 0x00 // Slave address + writeBuffer[2] = register & 0x7F // Read command (MSB clear for read) + writeBuffer[3] = comm.crc(writeBuffer[:3]) // Send the read command done := make(chan []byte, 1) @@ -104,11 +115,7 @@ func (comm *UARTComm) ReadRegister(register uint8, driverIndex uint8) (uint32, e // Implementing timeout using a 100ms timer select { case readBuffer := <-done: - // Validate checksum - checksum := byte(0) - for i := 0; i < 7; i++ { - checksum ^= readBuffer[i] - } + checksum = comm.crc(readBuffer[:7]) if checksum != readBuffer[7] { return 0, CustomError("checksum error") } From 427b019bd5004d44ec48a25d27c492d615795581 Mon Sep 17 00:00:00 2001 From: Craig Swank Date: Sun, 4 May 2025 12:02:46 -0600 Subject: [PATCH 3/4] fix build --- tmc2209/uartcomm.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tmc2209/uartcomm.go b/tmc2209/uartcomm.go index 5dbdf54b6..e21de6ad8 100644 --- a/tmc2209/uartcomm.go +++ b/tmc2209/uartcomm.go @@ -115,7 +115,7 @@ func (comm *UARTComm) ReadRegister(register uint8, driverIndex uint8) (uint32, e // Implementing timeout using a 100ms timer select { case readBuffer := <-done: - checksum = comm.crc(readBuffer[:7]) + checksum := comm.crc(readBuffer[:7]) if checksum != readBuffer[7] { return 0, CustomError("checksum error") } From 964d2b6e987b6f55b4e00a02484ba475eea75681 Mon Sep 17 00:00:00 2001 From: Craig Swank Date: Mon, 5 May 2025 06:08:42 -0600 Subject: [PATCH 4/4] a correct crc func already exists --- tmc2209/uartcomm.go | 22 +++------------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/tmc2209/uartcomm.go b/tmc2209/uartcomm.go index e21de6ad8..f1afb8699 100644 --- a/tmc2209/uartcomm.go +++ b/tmc2209/uartcomm.go @@ -60,7 +60,7 @@ func (comm *UARTComm) WriteRegister(register uint8, value uint32, driverIndex ui 0, // CRC } - buffer[7] = comm.crc(buffer[:7]) + buffer[7] = CalculateCRC(buffer[:7]) // Write the data to the TMC2209 done := make(chan error, 1) @@ -79,29 +79,13 @@ func (comm *UARTComm) WriteRegister(register uint8, value uint32, driverIndex ui } } -func (comm *UARTComm) crc(buf []byte) byte { - crc := byte(0) - for i := range buf { - currentByte := buf[i] - for j := 0; j < 8; j++ { - if (crc>>7)^(currentByte&0x01) > 0 { - crc = (crc << 1) ^ 0x07 - } else { - crc = crc << 1 - } - currentByte = currentByte >> 1 - } - } - return crc -} - // ReadRegister sends a register read command to the TMC2209 with a timeout. func (comm *UARTComm) ReadRegister(register uint8, driverIndex uint8) (uint32, error) { var writeBuffer [4]byte writeBuffer[0] = 0x05 // Sync byte writeBuffer[1] = 0x00 // Slave address writeBuffer[2] = register & 0x7F // Read command (MSB clear for read) - writeBuffer[3] = comm.crc(writeBuffer[:3]) + writeBuffer[3] = CalculateCRC(writeBuffer[:3]) // Send the read command done := make(chan []byte, 1) @@ -115,7 +99,7 @@ func (comm *UARTComm) ReadRegister(register uint8, driverIndex uint8) (uint32, e // Implementing timeout using a 100ms timer select { case readBuffer := <-done: - checksum := comm.crc(readBuffer[:7]) + checksum := CalculateCRC(readBuffer[:7]) if checksum != readBuffer[7] { return 0, CustomError("checksum error") }