Skip to content

Commit 2959b1e

Browse files
committed
Implemented automatic retry when reading registers.
Automatically retry reading registers a few times if the CRC doesn't match.
1 parent a3250a7 commit 2959b1e

File tree

2 files changed

+33
-22
lines changed

2 files changed

+33
-22
lines changed

src/TMC2209.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ class TMC2209
285285
const static uint8_t STEPPER_DRIVER_FEATURE_OFF = 0;
286286
const static uint8_t STEPPER_DRIVER_FEATURE_ON = 1;
287287

288-
const static int MAX_READ_RETRIES = 5;
288+
const static uint8_t MAX_READ_RETRIES = 5;
289289
const static uint32_t READ_RETRY_DELAY_MS = 20;
290290

291291
// Datagrams

src/TMC2209/TMC2209.cpp

+32-21
Original file line numberDiff line numberDiff line change
@@ -916,32 +916,43 @@ uint32_t TMC2209::read(uint8_t register_address)
916916
read_request_datagram.rw = RW_READ;
917917
read_request_datagram.crc = calculateCrc(read_request_datagram, READ_REQUEST_DATAGRAM_SIZE);
918918

919-
sendDatagramBidirectional(read_request_datagram, READ_REQUEST_DATAGRAM_SIZE);
920-
921-
uint32_t reply_delay = 0;
922-
while ((serialAvailable() < WRITE_READ_REPLY_DATAGRAM_SIZE) and
923-
(reply_delay < REPLY_DELAY_MAX_MICROSECONDS))
919+
for (uint8_t retry = 0; retry < MAX_READ_RETRIES; retry++)
924920
{
925-
delayMicroseconds(REPLY_DELAY_INC_MICROSECONDS);
926-
reply_delay += REPLY_DELAY_INC_MICROSECONDS;
927-
}
921+
sendDatagramBidirectional(read_request_datagram, READ_REQUEST_DATAGRAM_SIZE);
928922

929-
if (reply_delay >= REPLY_DELAY_MAX_MICROSECONDS)
930-
{
931-
return 0;
932-
}
923+
uint32_t reply_delay = 0;
924+
while ((serialAvailable() < WRITE_READ_REPLY_DATAGRAM_SIZE) and
925+
(reply_delay < REPLY_DELAY_MAX_MICROSECONDS))
926+
{
927+
delayMicroseconds(REPLY_DELAY_INC_MICROSECONDS);
928+
reply_delay += REPLY_DELAY_INC_MICROSECONDS;
929+
}
933930

934-
uint64_t byte;
935-
uint8_t byte_count = 0;
936-
WriteReadReplyDatagram read_reply_datagram;
937-
read_reply_datagram.bytes = 0;
938-
for (uint8_t i=0; i<WRITE_READ_REPLY_DATAGRAM_SIZE; ++i)
939-
{
940-
byte = serialRead();
941-
read_reply_datagram.bytes |= (byte << (byte_count++ * BITS_PER_BYTE));
931+
if (reply_delay >= REPLY_DELAY_MAX_MICROSECONDS)
932+
{
933+
return 0;
934+
}
935+
936+
uint64_t byte;
937+
uint8_t byte_count = 0;
938+
WriteReadReplyDatagram read_reply_datagram;
939+
read_reply_datagram.bytes = 0;
940+
for (uint8_t i=0; i<WRITE_READ_REPLY_DATAGRAM_SIZE; ++i)
941+
{
942+
byte = serialRead();
943+
read_reply_datagram.bytes |= (byte << (byte_count++ * BITS_PER_BYTE));
944+
}
945+
946+
auto crc = calculateCrc(read_reply_datagram, WRITE_READ_REPLY_DATAGRAM_SIZE);
947+
if (crc == read_reply_datagram.crc)
948+
{
949+
return reverseData(read_reply_datagram.data);
950+
}
951+
952+
delay(READ_RETRY_DELAY_MS);
942953
}
943954

944-
return reverseData(read_reply_datagram.data);
955+
return 0;
945956
}
946957

947958
uint8_t TMC2209::percentToCurrentSetting(uint8_t percent)

0 commit comments

Comments
 (0)