How to share I2C bus if a SSD1306 OLED driver is already instantiated

I would like to use more than one I2C device at the same time.
I do have success with the SSD1306 OLED driver on its own (driver supplied by Wilderness labs).
I do have success with the DS3231 Real Time Clock on its own (using the I2C API provided by NETMF).
When trying to instantiate them both at the same time, I get a “'System.InvalidOperationException” when opening the second device (does not matter which of the two is second).
From reading on forums (e.g. http://forums.netduino.com/index.php?/topic/11688-controlling-multiple-i2c-devices/) it appears the I2CDevice is trying to reserve the output pins twice, which fails.
In this and other forums the problem is solved by only using one device instance, but changing its configuration each time a method gets called.
This will not work in my case since I’m using an SSD1306 OLED driver, which does not expose its I2CDevice to me. It also does not have a close/dispose method either (which hopefully releases its I2CDevice).
Here is the code that simply demonstrates the problem:

var oledAsDevice1 = new SSD1306(speed: 100, address: 0x3C, displayType: SSD1306.DisplayType.OLED128x64);
var device2 = new I2CDevice(new I2CDevice.Configuration(0x68, 100)); // Fails with System.InvalidOperationException when executing this line

If there was only a way to close/dispose the SSD1306 device, then I can at least re-instantiate each device whenever I need them (very inefficient but at least a workaround).

Can anybody please suggest a solution? It shall be much appreciated.

Solved it.
I used the alternative (a lower level) I2C library that uses I2CBus, rather then I2CDevice, to control the DS3231 Real Time Clock. It appears that library does not lock the I2C pins.
I came across this solution when realising the SSD1306 OLED driver uses the I2CBus based library.
Regards

1 Like

As you found, the I2CBus class in Netduino.Foundation has been designed to get around the problem.

Regard,
Mark

1 Like