[SOLVED] One more thread about I2C issues with the latest core

Post Reply
User avatar
Dimdim
Posts: 12
Joined: Mon May 09, 2016 10:34 am
Location: Athens, Greece
Contact:

[SOLVED] One more thread about I2C issues with the latest core

Post by Dimdim » Sat Dec 23, 2017 11:44 am

Hi guys,

For several months I'd been using a blue pill to control my custom AK4490 DAC ( http://www.dimdim.gr/2017/11/arduino-co ... ac-part-1/ ), making use of the hardware I2C2 port.

The other day I updated my stm32duino core to the current version and noticed things had changed a bit.

I read up on the changes that were made to the Wire library so that it became compatible with Arduino's Wire library.

I ran the i2c_scanner_wire sketch (set up to use I2C2) and sure enough, it detected all 6 I2C connected devices (2 dac chips, 2 i/o expanders, a flash chip and an Si570 programmable oscillator).

So I proceeded to change the #include statement and initialization code as per the i2c_scanner_wire example.

So this

Code: Select all

HardWire HWire(2, I2C_FAST_MODE);
got changed to

Code: Select all

TwoWire WIRE2(2);
#define Wire WIRE2
The code compiled just fine, but it appears to get stuck at runtime as soon as it gets to the I/O expander's library: https://github.com/adafruit/Adafruit-MCP23008-library

Commenting out everything related to the specific library, the same problem occurs when it gets to the Si570's library: https://github.com/afarhan/radiono/tree/master/radiono (slightly modified, by commenting out the digitalWrite(SDA, 0); & digitalWrite(SCL, 0); lines).

Searching for similar issues, I came across this thread: viewtopic.php?f=14&t=2976&hilit=i2c
The OP seems to have the same issues I'm having, with Adafruit's libraries.

So it seems that there is a problem with the libraries that is beyond my pay grade.

I could make available the full code but it's rather big and complicated. Instead, I am attaching an example sketch with the Si570 library that presents the same problem:
Si570_test.zip
(5.04 KiB) Downloaded 5 times
The full project will be made open source once I figure these things out.

Thanks in advance.

User avatar
BennehBoy
Posts: 501
Joined: Thu Jan 05, 2017 8:21 pm
Location: Yorkshire
Contact:

Re: One more thread about I2C issues with the latest core

Post by BennehBoy » Sat Dec 23, 2017 1:13 pm

I just refreshed my install from github and now also have a lockup when the two following i2c devices are initialised:

Adafruit_ADXL345
Adafruit_HMC5883

Works fine without them. (well, it doesn't crash without them, only 50% of the 8 SPI SSD1306's are now working however - some SPI changes too?).

I'll test them independently later, Mrs BennehBoy has other plans for me today that mostly involve male abject boredom.
-------------------------------------
https://github.com/BennehBoy

stevestrong
Posts: 2051
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany
Contact:

Re: One more thread about I2C issues with the latest core

Post by stevestrong » Sat Dec 23, 2017 5:19 pm

@Dimdim,
your example cannot work because you define I2C port 2 in your sketch:

Code: Select all

TwoWire WIRE2(2); 
#define Wire WIRE2
but the Si570 lib uses the default Wire object definition which is I2C port 1, as declared in line 106 of Wire.cpp:

Code: Select all

TwoWire Wire(1);
Or better I should say that the example should work on I2C 1, but not on port 2 as you probably expect.

User avatar
Dimdim
Posts: 12
Joined: Mon May 09, 2016 10:34 am
Location: Athens, Greece
Contact:

Re: One more thread about I2C issues with the latest core

Post by Dimdim » Sat Dec 23, 2017 7:21 pm

I see. I thought that the

Code: Select all

#define Wire WIRE2
would take care of that but apparently it won't (is that because it has effect only in the sketch and not on the #included libraries?).

How should I go about modifying the library? I tried replacing Wire with WIRE2 but no joy.

Like I said, obviously above my pay grade.

stevestrong
Posts: 2051
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany
Contact:

Re: One more thread about I2C issues with the latest core

Post by stevestrong » Sat Dec 23, 2017 10:13 pm

Dimdim wrote:
Sat Dec 23, 2017 7:21 pm
(is that because it has effect only in the sketch and not on the #included libraries?).
Yes.
The cleanest solution would be to change the lib so that you can pass the Wire instance as parameter.
The alternative solution is to use the default Wire instance in your sketch, too.

User avatar
Dimdim
Posts: 12
Joined: Mon May 09, 2016 10:34 am
Location: Athens, Greece
Contact:

Re: One more thread about I2C issues with the latest core

Post by Dimdim » Sat Dec 23, 2017 11:24 pm

It's not possible to use the I2C1 port since its lines are in use driving an 8-bit interfaced TFT (I did try to change the TFT's library so as to use other pins and failed). Plus I've already had PCBs made to support the TFT and the other peripherals.

Image

So it seems I'm left with passing the Wire instance as parameter. But I have no idea how to do that.

Can't I just do some #define or something in the library to force it to use I2C2? It's not like I'll be needing it to use any other I2C port.

Thanks a lot for your help! :)

User avatar
Dimdim
Posts: 12
Joined: Mon May 09, 2016 10:34 am
Location: Athens, Greece
Contact:

Re: One more thread about I2C issues with the latest core

Post by Dimdim » Sun Dec 24, 2017 12:20 am

OK, so I figured it out.

I just inserted this

Code: Select all

#define Wire WIRE2
before every

Code: Select all

#include <Wire.h>
in my libraries.

Everything works fine now. :)

Post Reply