I2C Examples

Discussions about the STM32generic core
danieleff
Posts: 336
Joined: Thu Sep 01, 2016 8:52 pm
Location: Hungary
Contact:

Re: [STM32GENERIC] I2C Examples

Post by danieleff » Sat Apr 29, 2017 3:21 am

For existing libraries that use hardcoded Wire:

Code: Select all

Wire.stm32SetInstance(I2Cx);
Wire.stm32SetSDA(sda);
Wire.stm32SetSCL(scl);
rtc....()
...
Or if you can rewrite the whole library, add a private TwoWire *wire, add a setter, and use that.

User avatar
Pito
Posts: 1627
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: [STM32GENERIC] I2C Examples

Post by Pito » Sat Apr 29, 2017 5:02 am

Ok, thanks it works :)

FYI - an enhanced I2C library which supports master/slave in immediate/interrupt/dma mode - for Teensy CM4 however.. https://github.com/nox771/i2c_t3
Pukao Hats Cleaning Services Ltd.

ChrisMicro
Posts: 308
Joined: Fri Mar 24, 2017 4:51 pm
Location: Germany

Re: I2C Examples

Post by ChrisMicro » Sat Jul 01, 2017 6:39 am

I just encountered a problem:

How can I use 2 I2C interfaces?

E.g. F7 Disco has an internal I2C bus for the touch which is use in the form

Code: Select all

    Wire.stm32SetInstance(I2C3);
    Wire.stm32SetSDA(PH8);
    Wire.stm32SetSCL(PH7);
    Wire.begin();
But now I need the main I2C on the Arduino connector additionally. How can I have this 2 instances at the same time?

danieleff
Posts: 336
Joined: Thu Sep 01, 2016 8:52 pm
Location: Hungary
Contact:

Re: I2C Examples

Post by danieleff » Sat Jul 01, 2017 7:04 am

Code: Select all

TwoWire Wire2(I2C2, sda2, scl2); // check chip documentation and board schematic
TwoWire Wire3(I2C3, sda3, scl3); // check chip documentation and board schematic
TwoWire Wire4(I2C4, sda4, scl4); // check chip documentation and board schematic
Problem is every library is written with the assumption that there is only one I2C, and it is called Wire.
Disclaimer: I actually not tried to use multiple at once.

zmemw16
Posts: 1486
Joined: Wed Jul 08, 2015 2:09 pm
Location: St Annes, Lancs,UK

Re: I2C Examples

Post by zmemw16 » Sat Jul 01, 2017 12:46 pm

how is it done with HAL ?
i suspect they'll have completely separate i2c structures.

for us, why not do it in same way as multiple spi usage.

stephen

ChrisMicro
Posts: 308
Joined: Fri Mar 24, 2017 4:51 pm
Location: Germany

Re: I2C Examples

Post by ChrisMicro » Sun Jul 02, 2017 5:06 am

Thank your for the response :D

"Problem is every library is written with the assumption that there is only one I2C, and it is called Wire.
Disclaimer: I actually not tried to use multiple at once."

For me it seems quite clear on how to handle it: On boards which have a Arduino compatible connector the I2C which is connected to this connector shall have the name "Wire".
The names of the others shall be based on the interface number.

E.g.

Wire1
Wire2 ==> Wire on this exampel board
Wire3

chrissb
Posts: 3
Joined: Sun Mar 05, 2017 7:37 am

Re: I2C Examples

Post by chrissb » Thu Jul 20, 2017 12:32 pm

Has anyone done any testing on the I2C slave side of things?

I'm trying to use this functionality on an F0, and it seems that on address match SCK is held low indefinitely. Is this the intended behaviour, and if so is there something I should be doing in a callback to release the bus?

Simple example with nothing more than initializing the Wire library as an I2C slave shows this behaviour for me:

Code: Select all

#include "Wire.h"

void setup() {
    Wire.begin(30);
}

void loop() { }

danieleff
Posts: 336
Joined: Thu Sep 01, 2016 8:52 pm
Location: Hungary
Contact:

Re: I2C Examples

Post by danieleff » Thu Jul 20, 2017 3:28 pm

Try to respond to the request

Code: Select all

void receive(int bytes) {
    // Received bytes
}

void request() {
    uint8_t response[1] = {9};
    Wire.write(response, sizeof(response)); // responding
}

void setup() {
    Wire.onReceive(receive);
    Wire.onRequest(request);

    Wire.begin(30);
}
But I have not done much testing with either slave mode I2C or F0.

caniggia
Posts: 27
Joined: Tue Dec 06, 2016 2:38 pm

Re: I2C Examples

Post by caniggia » Wed Nov 15, 2017 1:49 pm

Hi

With changes(26 Sep) to Wire.cpp it won't compile I2C examples for any board (except F0 probably):
https://github.com/danieleff/STM32GENER ... ire.cpp#L7

You cannot check #ifndef I2C1_EV_IRQn, because I2C1_EV_IRQn is enum not #define.

The same for #ifndef I2C2_EV_IRQn.

Maybe change it to:

Code: Select all

#if defined(STM32F0)
#define I2C1_EV_IRQn I2C1_IRQn
#define I2C2_EV_IRQn I2C1_IRQn
#endif
BR
caniggia

Post Reply