I2C Examples

Discussions about the STM32generic core
zmemw16
Posts: 1674
Joined: Wed Jul 08, 2015 2:09 pm
Location: St Annes, Lancs,UK

I2C Examples

Post by zmemw16 » Thu Apr 27, 2017 11:05 am

I2C Scanner
code is the discovery scanner with their specific I2S stuff etc commented out
i have a tmp102 and a 128x64 0.98 I2C oled SSD1306, with 4k7 pullups and 3v3 plugged in
this is SERIALINTERFACE #defined as SerialUSB

Code: Select all

...
0 Unknown error at address 0x3B
0 I2C device found at address 0x3C  !
0 Unknown error at address 0x3D
...
...
0 Unknown error at address 0x47
0 I2C device found at address 0x48  !
0 Unknown error at address 0x49
...
0 Unknown error at address 0x7E
thoughts?
stephen

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

Re: [STM32GENERIC] I2C Examples

Post by zmemw16 » Thu Apr 27, 2017 12:47 pm

looking in Wire.cpp, the return values are

Code: Select all

0 ok
1 timeout
4 default for other
adding true to the endTransmission call and expanding the result handling still shows 2 devices and the Unknown error on all other addresses, no Timeouts.

i mentioned that i was outputting to UART1 as well, cutecom shows that output as solid 0x00's repeatedly.
stephen

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

Re: [STM32GENERIC] I2C Examples

Post by danieleff » Thu Apr 27, 2017 12:52 pm

zmemw16 wrote:I2C Scanner
code is the discovery scanner with their specific I2S stuff etc commented out
i have a tmp102 and a 128x64 0.98 I2C oled SSD1306, with 4k7 pullups and 3v3 plugged in
this is SERIALINTERFACE #defined as SerialUSB

Code: Select all

...
0 Unknown error at address 0x3B
0 I2C device found at address 0x3C  !
0 Unknown error at address 0x3D
...
...
0 Unknown error at address 0x47
0 I2C device found at address 0x48  !
0 Unknown error at address 0x49
...
0 Unknown error at address 0x7E
thoughts?
stephen
This is correct. It found your 2 devices.
The "Unknown error" just means that on those addresses nobody is listening, no device for 0x01, 0x02, 0x03...

For UART, do you have correct baud rate set? Serialxxx.begin(115200), and on your PC also 115200

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

Re: [STM32GENERIC] I2C Examples

Post by ChrisMicro » Thu Apr 27, 2017 4:28 pm

On the STM32F4 Discovery it shows the same behaviour.
"Unknown error at address 0x.." when there is no device responding.

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

Re: [STM32GENERIC] I2C Examples

Post by zmemw16 » Thu Apr 27, 2017 5:17 pm

but 124 times? as i recall todbot's didn't
stephen

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

Re: [STM32GENERIC] I2C Examples

Post by ChrisMicro » Fri Apr 28, 2017 8:05 am

but 124 times? as i recall todbot's didn't
stephen
The STM32GENERIC example is originally from this Arduino site and is modified for the STM.

Hmm .. the I2C address field is 7 bits witch gives use 128 addresses. I know, not all are usable.
0 Unknown error at address 0x47
Probably the I2C driver is working different compared to the Arduino-Uno implementation and returns different status messages ?

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

Re: [STM32GENERIC] I2C Examples

Post by zmemw16 » Fri Apr 28, 2017 10:04 am

apols, i harkened back to my AVR days and then it was Todbot's that i used.
istr trying 3 or 4 before finding one that worked at all on STM, even then it wasn't both hard and soft i2c.
btw that's a get out for not remembering which :D

there seem to be 3 responses from the HAL routine
0. ok - definitely a device present
4. nothing there
1. timeout - but what then, something possibly there

i can understand reporting the first and last, but why report nothing?

stephen

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

Re: [STM32GENERIC] I2C Examples

Post by ChrisMicro » Fri Apr 28, 2017 1:03 pm

The Arduino I2C API Specification says for endTransmission()
Returns

byte, which indicates the status of the transmission:

0:success
1:data too long to fit in transmit buffer
2:received NACK on transmit of address
3:received NACK on transmit of data
4:other error
So we should probably alter the text message ( into "other error" ) in the I2C scanner program.

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

Re: [STM32GENERIC] I2C Examples

Post by Pito » Fri Apr 28, 2017 2:43 pm

With I2C2 on PB10 clk and PB11 data.

Code: Select all

// --------------------------------------
// i2c_scanner
..
#include <Wire.h>

TwoWire Wire2(I2C2, PB11, PB10);

#define Wire Wire2
#define Serial SerialUART1

void setup()
..
    else if (error==4)
    {
      //Serial.print("Unknow error at address 0x");
      //if (address<16)
      //  Serial.print("0");
      //Serial.println(address,HEX);
    }
  }
..
Found the DS3231 module:

Code: Select all

Scanning...
I2C device found at address 0x57  !
I2C device found at address 0x68  !
done
Pukao Hats Cleaning Services Ltd.

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

Re: [STM32GENERIC] I2C Examples

Post by Pito » Fri Apr 28, 2017 10:20 pm

I've spent 5h messing with the DS3232RTC library I like :)
The issue is following:
How to build a demo, reading time off DS3231/2, when using I2C2 (not the default I2C1).
When playing with simple stuff like above, no prob, but this is a chicken-egg-situation (or Catch22 one)..

TwoWire Wire2(I2C2, pina, pinb); creates a new instance Wire2 on the second I2C controller.
Tried with #define Wire2 Wire, but after a hundred various combinations I still get an error with Wire redefinition..

But how to organize the code such it compiles with the DS3232RTC library????
PS: with I2C1 and with the default Wire it compiles fine.. (except you have to adjust for our __STM32F4__ and add the _BV macro definition)..

Code: Select all

22:24:50
28/4/2017
22:24:51
28/4/2017
Pukao Hats Cleaning Services Ltd.

Post Reply