Skip to content

Commit 4b34f56

Browse files
committed
Fix hanging with multiple I2C buses
Make the _mutex non-static and remove _owner and acquire() When two or more I2C buses are used then static__mutex and _owner are shared between all I2C class instances in the program. That wastes time to reconfigure periphery on every transfer. Make _mutex non-static. Remove _owner and acquire() method because in non-static case they have no practical meaning.
1 parent cecc47b commit 4b34f56

File tree

2 files changed

+1
-26
lines changed

2 files changed

+1
-26
lines changed

drivers/include/drivers/I2C.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,12 +231,10 @@ class I2C : private NonCopyable<I2C> {
231231

232232
#if !defined(DOXYGEN_ONLY)
233233
protected:
234-
void aquire();
235234

236235
i2c_t _i2c;
237-
static I2C *_owner;
238236
int _hz;
239-
static SingletonPtr<PlatformMutex> _mutex;
237+
SingletonPtr<PlatformMutex> _mutex;
240238
PinName _sda;
241239
PinName _scl;
242240

drivers/source/I2C.cpp

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@
2727

2828
namespace mbed {
2929

30-
I2C *I2C::_owner = NULL;
31-
SingletonPtr<PlatformMutex> I2C::_mutex;
32-
3330
I2C::I2C(PinName sda, PinName scl) :
3431
#if DEVICE_I2C_ASYNCH
3532
_irq(this), _usage(DMA_USAGE_NEVER), _deep_sleep_locked(false),
@@ -42,8 +39,6 @@ I2C::I2C(PinName sda, PinName scl) :
4239
_scl = scl;
4340
recover(sda, scl);
4441
i2c_init(&_i2c, _sda, _scl);
45-
// Used to avoid unnecessary frequency updates
46-
_owner = this;
4742
unlock();
4843
}
4944

@@ -59,8 +54,6 @@ I2C::I2C(const i2c_pinmap_t &static_pinmap) :
5954
_scl = static_pinmap.scl_pin;
6055
recover(static_pinmap.sda_pin, static_pinmap.scl_pin);
6156
i2c_init_direct(&_i2c, &static_pinmap);
62-
// Used to avoid unnecessary frequency updates
63-
_owner = this;
6457
unlock();
6558
}
6659

@@ -72,26 +65,13 @@ void I2C::frequency(int hz)
7265
// We want to update the frequency even if we are already the bus owners
7366
i2c_frequency(&_i2c, _hz);
7467

75-
// Updating the frequency of the bus we become the owners of it
76-
_owner = this;
77-
unlock();
78-
}
79-
80-
void I2C::aquire()
81-
{
82-
lock();
83-
if (_owner != this) {
84-
i2c_frequency(&_i2c, _hz);
85-
_owner = this;
86-
}
8768
unlock();
8869
}
8970

9071
// write - Master Transmitter Mode
9172
int I2C::write(int address, const char *data, int length, bool repeated)
9273
{
9374
lock();
94-
aquire();
9575

9676
int stop = (repeated) ? 0 : 1;
9777
int written = i2c_write(&_i2c, address, data, length, stop);
@@ -112,7 +92,6 @@ int I2C::write(int data)
11292
int I2C::read(int address, char *data, int length, bool repeated)
11393
{
11494
lock();
115-
aquire();
11695

11796
int stop = (repeated) ? 0 : 1;
11897
int read = i2c_read(&_i2c, address, data, length, stop);
@@ -137,7 +116,6 @@ int I2C::read(int ack)
137116
void I2C::start(void)
138117
{
139118
lock();
140-
aquire();
141119
i2c_start(&_i2c);
142120
unlock();
143121
}
@@ -215,7 +193,6 @@ int I2C::transfer(int address, const char *tx_buffer, int tx_length, char *rx_bu
215193
return -1; // transaction ongoing
216194
}
217195
lock_deep_sleep();
218-
aquire();
219196

220197
_callback = callback;
221198
int stop = (repeated) ? 0 : 1;

0 commit comments

Comments
 (0)