Skip to content

Commit e7f9016

Browse files
authored
tmc2209 bug fixes (#755)
* Make write buffer big enough for crc * crc according to datasheet * fix build * a correct crc func already exists
1 parent 156d6e7 commit e7f9016

File tree

1 file changed

+7
-15
lines changed

1 file changed

+7
-15
lines changed

tmc2209/uartcomm.go

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,10 @@ func (comm *UARTComm) WriteRegister(register uint8, value uint32, driverIndex ui
5757
byte((value >> 16) & 0xFF), // Middle byte
5858
byte((value >> 8) & 0xFF), // Next byte
5959
byte(value & 0xFF), // LSB of value
60+
0, // CRC
6061
}
6162

62-
// Calculate checksum by XORing all bytes
63-
checksum := byte(0)
64-
for _, b := range buffer[:7] {
65-
checksum ^= b
66-
}
67-
buffer[7] = checksum // Set checksum byte
63+
buffer[7] = CalculateCRC(buffer[:7])
6864

6965
// Write the data to the TMC2209
7066
done := make(chan error, 1)
@@ -86,10 +82,10 @@ func (comm *UARTComm) WriteRegister(register uint8, value uint32, driverIndex ui
8682
// ReadRegister sends a register read command to the TMC2209 with a timeout.
8783
func (comm *UARTComm) ReadRegister(register uint8, driverIndex uint8) (uint32, error) {
8884
var writeBuffer [4]byte
89-
writeBuffer[0] = 0x05 // Sync byte
90-
writeBuffer[1] = 0x00 // Slave address
91-
writeBuffer[2] = register & 0x7F // Read command (MSB clear for read)
92-
writeBuffer[3] = writeBuffer[0] ^ writeBuffer[1] ^ writeBuffer[2] // Checksum
85+
writeBuffer[0] = 0x05 // Sync byte
86+
writeBuffer[1] = 0x00 // Slave address
87+
writeBuffer[2] = register & 0x7F // Read command (MSB clear for read)
88+
writeBuffer[3] = CalculateCRC(writeBuffer[:3])
9389

9490
// Send the read command
9591
done := make(chan []byte, 1)
@@ -103,11 +99,7 @@ func (comm *UARTComm) ReadRegister(register uint8, driverIndex uint8) (uint32, e
10399
// Implementing timeout using a 100ms timer
104100
select {
105101
case readBuffer := <-done:
106-
// Validate checksum
107-
checksum := byte(0)
108-
for i := 0; i < 7; i++ {
109-
checksum ^= readBuffer[i]
110-
}
102+
checksum := CalculateCRC(readBuffer[:7])
111103
if checksum != readBuffer[7] {
112104
return 0, CustomError("checksum error")
113105
}

0 commit comments

Comments
 (0)