Skip to content

Commit

Permalink
I2C volatiles for critical region state
Browse files Browse the repository at this point in the history
  • Loading branch information
pmantoine committed May 31, 2024
1 parent 2cfcba5 commit 03a2d01
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
10 changes: 5 additions & 5 deletions I2CManager.h
Original file line number Diff line number Diff line change
@@ -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
*
Expand Down Expand Up @@ -519,14 +519,14 @@ class I2CManagerClass {

// 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;
Expand Down
6 changes: 3 additions & 3 deletions I2CManager_NonBlocking.h
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions I2CManager_STM32.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 03a2d01

Please sign in to comment.