From 8acb8d5bfa297a415214b0e92f0239206a67ff1f Mon Sep 17 00:00:00 2001 From: Paul Date: Thu, 19 Jan 2023 10:55:12 +0000 Subject: [PATCH 1/3] Suggested readRegisterRegion changes --- src/sfe_bus.cpp | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/sfe_bus.cpp b/src/sfe_bus.cpp index 4ed11ef..d0158cd 100644 --- a/src/sfe_bus.cpp +++ b/src/sfe_bus.cpp @@ -164,30 +164,29 @@ namespace sfe_KX13X return -1; int i; // counter in loop - bool bFirstInter = true; // Flag for first iteration - used to send register + int failCount = 0; // Keep track of how many times nReturned is != nChunk - while (numBytes > 0) + while ((numBytes > 0) && (failCount < 5)) // Give up after 5 bad requests { _i2cPort->beginTransmission(addr); - - if (bFirstInter) - { - _i2cPort->write(reg); - bFirstInter = false; - } - + _i2cPort->write(reg); // Write the register address we want to read from if (_i2cPort->endTransmission() != 0) - return -1; // error with the end transmission + return -1; // Fail immediately if the transmission isn't successful // We're chunking in data - keeping the max chunk to kMaxI2CBufferLength - nChunk = numBytes > kChunkSize ? kChunkSize : numBytes; + // The register address counts as one byte so limit nChunk to kChunkSize -1 + nChunk = numBytes > (kChunkSize -1) ? (kChunkSize -1) : numBytes; - nReturned = _i2cPort->requestFrom((int)addr, (int)nChunk, (int)true); + nReturned = _i2cPort->requestFrom((int)addr, (int)nChunk, (int)true); // Always send a stop // No data returned, no dice if (nReturned == 0) return -1; // error + // Check we got back as much data as was requested + if (nReturned != nChunk) + failCount++; // Increment the failCount + // Copy the retrieved data chunk to the current index in the data segment for (i = 0; i < nReturned; i++) { @@ -197,6 +196,9 @@ namespace sfe_KX13X // Decrement the amount of data recieved from the overall data request amount numBytes = numBytes - nReturned; + // Increment reg by the same ammount + reg += nReturned; + } // end while return 0; // Success From c316bdb0e0640d92fa301e4f7e9ce135f9e1dac8 Mon Sep 17 00:00:00 2001 From: Paul Date: Thu, 19 Jan 2023 11:09:36 +0000 Subject: [PATCH 2/3] Trap bad requests --- src/sfe_bus.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/sfe_bus.cpp b/src/sfe_bus.cpp index d0158cd..02934d2 100644 --- a/src/sfe_bus.cpp +++ b/src/sfe_bus.cpp @@ -163,10 +163,10 @@ namespace sfe_KX13X if (!_i2cPort) return -1; - int i; // counter in loop + int i; // counter in loop int failCount = 0; // Keep track of how many times nReturned is != nChunk - while ((numBytes > 0) && (failCount < 5)) // Give up after 5 bad requests + while ((numBytes > 0) && (failCount < 2)) // Give up after 2 bad requests { _i2cPort->beginTransmission(addr); _i2cPort->write(reg); // Write the register address we want to read from @@ -175,7 +175,7 @@ namespace sfe_KX13X // We're chunking in data - keeping the max chunk to kMaxI2CBufferLength // The register address counts as one byte so limit nChunk to kChunkSize -1 - nChunk = numBytes > (kChunkSize -1) ? (kChunkSize -1) : numBytes; + nChunk = numBytes > (kChunkSize - 1) ? (kChunkSize - 1) : numBytes; nReturned = _i2cPort->requestFrom((int)addr, (int)nChunk, (int)true); // Always send a stop @@ -201,7 +201,7 @@ namespace sfe_KX13X } // end while - return 0; // Success + return (numBytes == 0 ? 0 : -1); // 0 = success (all bytes read), -1 = error } ////////////////////////////////////////////////////////////////////////////////////////////////// From d0166fcce9b7ecd6bcef60795a6689acd7c31ce4 Mon Sep 17 00:00:00 2001 From: Paul Date: Thu, 19 Jan 2023 16:50:46 +0000 Subject: [PATCH 3/3] Fix kChunkSize - 1 --- src/sfe_bus.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/sfe_bus.cpp b/src/sfe_bus.cpp index 02934d2..667ed54 100644 --- a/src/sfe_bus.cpp +++ b/src/sfe_bus.cpp @@ -174,8 +174,7 @@ namespace sfe_KX13X return -1; // Fail immediately if the transmission isn't successful // We're chunking in data - keeping the max chunk to kMaxI2CBufferLength - // The register address counts as one byte so limit nChunk to kChunkSize -1 - nChunk = numBytes > (kChunkSize - 1) ? (kChunkSize - 1) : numBytes; + nChunk = numBytes > kChunkSize ? kChunkSize : numBytes; nReturned = _i2cPort->requestFrom((int)addr, (int)nChunk, (int)true); // Always send a stop @@ -183,7 +182,8 @@ namespace sfe_KX13X if (nReturned == 0) return -1; // error - // Check we got back as much data as was requested + // Check we got back as much data as was requested. + // (Fringe case. This should never happen... But, you know, it _could_...) if (nReturned != nChunk) failCount++; // Increment the failCount