Skip to content

Commit

Permalink
Ensure tick_disable() upon timeout; fix locking. (#730)
Browse files Browse the repository at this point in the history
* Ensure that the tick_disable() gets called if the I2C transaction times out.

* Fix mutual exclusion with "tick" interrupts.

* Move Atomic scope to lowest required.
  • Loading branch information
bakerstu committed Aug 20, 2023
1 parent 6a1b281 commit 4426b20
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions src/freertos_drivers/common/BitBangI2C.hxx
Expand Up @@ -202,7 +202,9 @@ protected:
/// @endcode
///
/// @tparam HW hardware interface to the access the SCL and SDA I/O lines
template <class HW> class BitBangI2C : protected BitBangI2CStates, public I2C
template <class HW> class BitBangI2C : protected BitBangI2CStates
, public I2C
, private Atomic
{
public:
/// Constructor.
Expand Down Expand Up @@ -702,17 +704,23 @@ inline int BitBangI2C<HW>::transfer(struct i2c_msg *msg, bool stop)
return -EINVAL;
}

// Reset state for a start/restart.
msg_ = msg;
count_ = 0;
stop_ = stop;
state_ = State::START;
stateStart_ = StateStart::FIRST;

// Flush/reset semaphore.
while (sem_.timedwait(0) == 0);
// Enable tick timer.
HW::tick_enable();
while (sem_.timedwait(0) == 0)
{
}

{
AtomicHolder h(this);
// Reset state for a start/restart.
msg_ = msg;
count_ = 0;
stop_ = stop;
state_ = State::START;
stateStart_ = StateStart::FIRST;

// Enable tick timer.
HW::tick_enable();
}

// We wait a minimum of 10 msec to account for any rounding in the "tick"
// rate conversion. msg_->len is at least 1. We assume that worst ~50kHz
Expand All @@ -729,7 +737,11 @@ inline int BitBangI2C<HW>::transfer(struct i2c_msg *msg, bool stop)
// stop_ must be false for the next call of this method. It should only
// be true on first entry at start to "reset" the bus to a known state.
// On a timeout, it may not have been reset back to false.
stop_ = false;
{
AtomicHolder h(this);
stop_ = false;
HW::tick_disable();
}
return -ETIMEDOUT;
}
}
Expand Down

0 comments on commit 4426b20

Please sign in to comment.