tmc2209 bug fixes (#755)

* Make write buffer big enough for crc
* crc according to datasheet
* fix build
* a correct crc func already exists
This commit is contained in:
Craig Swank
2025-05-20 04:27:30 -06:00
committed by deadprogram
parent 8b6e8c81e3
commit 673367c317
+7 -15
View File
@@ -57,14 +57,10 @@ func (comm *UARTComm) WriteRegister(register uint8, value uint32, driverIndex ui
byte((value >> 16) & 0xFF), // Middle byte byte((value >> 16) & 0xFF), // Middle byte
byte((value >> 8) & 0xFF), // Next byte byte((value >> 8) & 0xFF), // Next byte
byte(value & 0xFF), // LSB of value byte(value & 0xFF), // LSB of value
0, // CRC
} }
// Calculate checksum by XORing all bytes buffer[7] = CalculateCRC(buffer[:7])
checksum := byte(0)
for _, b := range buffer[:7] {
checksum ^= b
}
buffer[7] = checksum // Set checksum byte
// Write the data to the TMC2209 // Write the data to the TMC2209
done := make(chan error, 1) done := make(chan error, 1)
@@ -86,10 +82,10 @@ func (comm *UARTComm) WriteRegister(register uint8, value uint32, driverIndex ui
// ReadRegister sends a register read command to the TMC2209 with a timeout. // ReadRegister sends a register read command to the TMC2209 with a timeout.
func (comm *UARTComm) ReadRegister(register uint8, driverIndex uint8) (uint32, error) { func (comm *UARTComm) ReadRegister(register uint8, driverIndex uint8) (uint32, error) {
var writeBuffer [4]byte var writeBuffer [4]byte
writeBuffer[0] = 0x05 // Sync byte writeBuffer[0] = 0x05 // Sync byte
writeBuffer[1] = 0x00 // Slave address writeBuffer[1] = 0x00 // Slave address
writeBuffer[2] = register & 0x7F // Read command (MSB clear for read) writeBuffer[2] = register & 0x7F // Read command (MSB clear for read)
writeBuffer[3] = writeBuffer[0] ^ writeBuffer[1] ^ writeBuffer[2] // Checksum writeBuffer[3] = CalculateCRC(writeBuffer[:3])
// Send the read command // Send the read command
done := make(chan []byte, 1) done := make(chan []byte, 1)
@@ -103,11 +99,7 @@ func (comm *UARTComm) ReadRegister(register uint8, driverIndex uint8) (uint32, e
// Implementing timeout using a 100ms timer // Implementing timeout using a 100ms timer
select { select {
case readBuffer := <-done: case readBuffer := <-done:
// Validate checksum checksum := CalculateCRC(readBuffer[:7])
checksum := byte(0)
for i := 0; i < 7; i++ {
checksum ^= readBuffer[i]
}
if checksum != readBuffer[7] { if checksum != readBuffer[7] {
return 0, CustomError("checksum error") return 0, CustomError("checksum error")
} }