gavinlyonsrepo / SSD1306_OLED_RPI

C++ Library to support the I2C 128X64 OLED Display Module driven by the SSD1306 controller for the Raspberry pi SBC eco-system.
https://gavinlyonsrepo.github.io/
GNU General Public License v3.0
22 stars 4 forks source link

I2C devices not detected after running SSD1306_OLED_RPI example #3

Closed leon-anavi closed 8 months ago

leon-anavi commented 8 months ago

Hi,

It looks like the I2C bus is somehow messed up after running an example application like HELLO_WORLD and as a result the I2C devices are no longer properly detected. Here are the steps to reproduce the issue:

leon@rpi-leon:~ $ cd SSD1306_OLED_RPI/examples/
leon@rpi-leon:~/SSD1306_OLED_RPI/examples $ i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:                         -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- 3c -- -- -- 
40: 40 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --                         
leon@rpi-leon:~/SSD1306_OLED_RPI/examples $ make run
sudo ./bin/test
OLED Begin
OLED End
leon@rpi-leon:~/SSD1306_OLED_RPI/examples $ i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:                         08 -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --   

The test was performed using SSD1306_OLED_RPI branch main (commit e3cfd9afaa53f276fca88028034ae09e6674a065).

Is this a know issue? Is there a way to prevent it while using the library right now?

Best regards, Leon

gavinlyonsrepo commented 8 months ago

Hello

I was able to replicate this I did not notice it before, The "I2cdetect" program does not initialise the I2C bus.

The SSD1306_OLED_RPI library switches on and off the I2C at the end of each operation. So when the main.cpp finishes the I2C is off. The command "bcm2835_i2c_end()" is run internally which End I2C operations. I2C pins P1-03 (SDA) and P1-05 (SCL) are returned to their default INPUT behavior. I made the member function's that turn on and off the I2C private to the class.

In order to restart the bus before exiting add command

bcm2835_i2c_begin();

before bcm2835_close(); in EndTest() method. I2cdetect will work after test if this is done.

I am working in a new version which will give user greater control over I2C bus.

regards

leon-anavi commented 8 months ago

Thank you for the quick feedback! It works and I will submit a pull request during the holidays with this upgrade.

gavinlyonsrepo commented 8 months ago

Hi

I just released a new version 1.5.

https://github.com/gavinlyonsrepo/SSD1306_OLED_RPI/releases/tag/1.5

The I2C on and off methods are now public and can be controlled optionally by user in their program. I also created a settings method that is now called internally to turn on address for write operations. In the Endtests() method of example files. You can leave out OLED_I2C_OFF() if you want I2C bus On at end of program.

    bool OLED_I2C_ON(void);
    void OLED_I2C_Settings(void);
    void OLED_I2C_OFF(void);
leon-anavi commented 8 months ago

Hi @gavinlyonsrepo,

Thank you for the changes and the provided details. I removed myOLED.OLED_I2C_OFF from HELLO_WORLD and i2cdetect now works after running the example. However, it reports an extra device at address 08 (aka the lowest actual i2c address):

leon@rpi-leon:~/SSD1306_OLED_RPI/examples$ i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:                         -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- 3c -- -- --
40: 40 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
leon@rpi-leon:~/SSD1306_OLED_RPI/examples$ make run
sudo ./bin/test
OLED Test Begin
SSD1306 library Version Number :: 150
bcm2835 library Version Number :: 10073
OLED Test End
leon@rpi-leon:~/SSD1306_OLED_RPI/examples$ i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:                         08 -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- 3c -- -- --
40: 40 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

No hardware modifications have been done therefore the expected second output of i2cdetect was the same as the first one.

Do you have an idea why 08 is reported by i2cdetect in this case?

Best regards, Leon

gavinlyonsrepo commented 8 months ago

Hi

The OLED library uses the bcm2835 library to interface with I2C. The Bcm2835 library is here and its online help and discussion are here

If we search for i2cdetect we get

This library works directly on the SPI/I2C devices. It is not using any Linux device drivers. When relevant Linux drivers are loaded or activated then it can create unexpected behavior from other software.

The bcm2835 library will work with I2C interface disabled in raspberry-pi configuration tool. I wrote a tool just now which detects I2C with the bcm2835 library , appears to be working https://github.com/gavinlyonsrepo/RPI_Tools/tree/main/src/i2cdetect

regards

Regards

leon-anavi commented 8 months ago

Hi @gavinlyonsrepo,

Thank you for the provided details and explanation. I gave a star to the RPI_Tools project - it looks like a great and very convenient project based on bcm2835 library.

In the mean time I had a look at alternative libraries and probably in long-term I will prefer to go with ssd1306_linux because it depends on libi2c which makes it more portable across other single board computers capable of running Linux.

Best regards, Leon