@@ -57,14 +57,10 @@ func (comm *UARTComm) WriteRegister(register uint8, value uint32, driverIndex ui
57
57
byte ((value >> 16 ) & 0xFF ), // Middle byte
58
58
byte ((value >> 8 ) & 0xFF ), // Next byte
59
59
byte (value & 0xFF ), // LSB of value
60
+ 0 , // CRC
60
61
}
61
62
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 ])
68
64
69
65
// Write the data to the TMC2209
70
66
done := make (chan error , 1 )
@@ -86,10 +82,10 @@ func (comm *UARTComm) WriteRegister(register uint8, value uint32, driverIndex ui
86
82
// ReadRegister sends a register read command to the TMC2209 with a timeout.
87
83
func (comm * UARTComm ) ReadRegister (register uint8 , driverIndex uint8 ) (uint32 , error ) {
88
84
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 ])
93
89
94
90
// Send the read command
95
91
done := make (chan []byte , 1 )
@@ -103,11 +99,7 @@ func (comm *UARTComm) ReadRegister(register uint8, driverIndex uint8) (uint32, e
103
99
// Implementing timeout using a 100ms timer
104
100
select {
105
101
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 ])
111
103
if checksum != readBuffer [7 ] {
112
104
return 0 , CustomError ("checksum error" )
113
105
}
0 commit comments