From 03a2d01484b405812624e45ac617680e237188c7 Mon Sep 17 00:00:00 2001 From: pmantoine Date: Fri, 31 May 2024 11:31:36 +1000 Subject: [PATCH] I2C volatiles for critical region state --- I2CManager.h | 10 +++++----- I2CManager_NonBlocking.h | 6 +++--- I2CManager_STM32.h | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/I2CManager.h b/I2CManager.h index 08d81d4..6a32853 100644 --- a/I2CManager.h +++ b/I2CManager.h @@ -1,6 +1,6 @@ /* * © 2023, Neil McKechnie. All rights reserved. - * © 2022 Paul M Antoine + * © 2022-2024 Paul M Antoine * * This file is part of CommandStation-EX * @@ -519,14 +519,14 @@ private: // State is set to I2C_STATE_FREE when the interrupt handler has finished // the current request and is ready to complete. - uint8_t state = I2C_STATE_FREE; + uint8_t volatile state = I2C_STATE_FREE; // CompletionStatus may be set by the interrupt handler at any time but is // not written to the I2CRB until the state is I2C_STATE_FREE. - uint8_t completionStatus = I2C_STATUS_OK; - uint8_t overallStatus = I2C_STATUS_OK; + uint8_t volatile completionStatus = I2C_STATUS_OK; + uint8_t volatile overallStatus = I2C_STATUS_OK; - I2CRB * currentRequest = NULL; + I2CRB * volatile currentRequest = NULL; uint8_t txCount = 0; uint8_t rxCount = 0; uint8_t bytesToSend = 0; diff --git a/I2CManager_NonBlocking.h b/I2CManager_NonBlocking.h index 59bbcaf..6315915 100644 --- a/I2CManager_NonBlocking.h +++ b/I2CManager_NonBlocking.h @@ -1,6 +1,6 @@ /* * © 2023, Neil McKechnie - * © 2022 Paul M Antoine + * © 2022-2024, Paul M Antoine * All rights reserved. * * This file is part of CommandStation-EX @@ -245,8 +245,8 @@ void I2CManagerClass::checkForTimeout() { I2CRB *t = queueHead; if (state==I2C_STATE_ACTIVE && t!=0 && t==currentRequest && _timeout > 0) { // Check for timeout - int32_t elapsed = micros() - startTime; - if (elapsed > (int32_t)_timeout) { + uint32_t elapsed = micros() - startTime; + if (elapsed > _timeout) { #ifdef DIAG_IO //DIAG(F("I2CManager Timeout on %s"), t->i2cAddress.toString()); #endif diff --git a/I2CManager_STM32.h b/I2CManager_STM32.h index cb6a03e..ea7152b 100644 --- a/I2CManager_STM32.h +++ b/I2CManager_STM32.h @@ -117,11 +117,11 @@ void I2CManagerClass::I2C_setClock(uint32_t i2cClockSpeed) { // while (s->CR1 & I2C_CR1_STOP); // Prevents lockup by guarding further // writes to CR1 while STOP is being executed! // Should never happen, but wait for up to 500us only. - unsigned long startTime = micros(); + uint32_t startTime = micros(); bool timeout = false; while ((s->CR1 & I2C_CR1_STOP) != 0) { - if ((int32_t)(micros() - startTime) >= 500) { + if ((micros() - startTime) >= 500) { timeout = true; s->CR1 &= ~I2C_CR1_STOP; break;