I2C working examples

marcin.kasinski
Sat Sep 10, 2016 8:15 am
Hello.

Do you have any I2C working example (master or receive) ?

My below code throws compile error :class TwoWire’ has no member named ‘onReceive’ with blue pill . (no errors with arduino).

In lib examples I can see i2c scanner only.


#include <Wire.h>

void setup() {
Wire.begin(8); // join i2c bus with address #8
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600); // start serial for output
}

void loop() {
delay(100);
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) {
// while (1 < Wire.available()) { // loop through all but the last
while ( Wire.available()) {
char c = Wire.read(); // receive byte as a character
Serial.print(c); // print the character
}
// int x = Wire.read(); // receive byte as an integer
// Serial.println(x); // print the integer
Serial.println();
}


martinayotte
Sat Sep 10, 2016 1:35 pm
onReceive() is for Slave implementation and, unfortunately, Slave is not implemented yet …

marcin.kasinski
Sat Sep 10, 2016 7:19 pm
martinayotte wrote:onReceive() is for Slave implementation and, unfortunately, Slave is not implemented yet …

martinayotte
Sat Sep 10, 2016 9:43 pm
There is no other example code other than scanners into the github, but plenty Arduino ones on the internet that should work directly on STM32.
It is all depending what you wish to attach to I2C bus : EEPROMs, GPIO Expanders, RTC Clocks, ADC, DAC.
Which ones are you planning to use ?

marcin.kasinski
Mon Sep 12, 2016 8:10 am
I have simple scenario.

I want to connect blue pill to Lego EV3 using I2C.

It works ok using arduino (arduino as a slave).

Now I know that slave mode is not implemented.

I started from i2c_scanner_hwire.ino example to test I2C.

Problem is that code hangs on endTransmission().


martinayotte
Mon Sep 12, 2016 12:14 pm
Do you have PullUps on your I2C bus ?

marcin.kasinski
Mon Sep 12, 2016 7:23 pm
martinayotte wrote:Do you have PullUps on your I2C bus ?

martinayotte
Mon Sep 12, 2016 7:59 pm
Maybe you’ve inverted the SDA and SCK.

marcin.kasinski
Mon Sep 12, 2016 8:08 pm
martinayotte wrote:Maybe you’ve inverted the SDA and SCK.

martinayotte
Mon Sep 12, 2016 8:14 pm
You need PullUps on both.
But there is also possible mistake of crossing SDA and SCK.

marcin.kasinski
Mon Sep 12, 2016 9:09 pm
martinayotte wrote:You need PullUps on both.
But there is also possible mistake of crossing SDA and SCK.

martinayotte
Mon Sep 12, 2016 9:17 pm
So, if I understand, you don’t have any devices on the bus, your Master is still alone.

Do you still have Wire.begin(8) or since you are trying as Master you’ve replaced it with Wire.begin() ?

I’m running out of ideas … Wire library is so simple …


marcin.kasinski
Tue Sep 13, 2016 7:42 am
martinayotte wrote:So, if I understand, you don’t have any devices on the bus, your Master is still alone.

Do you still have Wire.begin(8) or since you are trying as Master you’ve replaced it with Wire.begin() ?

I’m running out of ideas … Wire library is so simple …


martinayotte
Tue Sep 13, 2016 1:53 pm
Which STM32 board or chip are you using ?
Because I2C on F1xx are either PB6/PB7 or PB10/PB11,
On F4xx, PB8/PB9 are alternate pins but not the default PB6/PB7. For using alternate, you will need to tweak core i2c.c file.

marcin.kasinski
Tue Sep 13, 2016 1:59 pm
martinayotte wrote:Which STM32 board or chip are you using ?
Because I2C on F1xx are either PB6/PB7 or PB10/PB11, but NOT PB8/PB9 … :ugeek:

martinayotte
Tue Sep 13, 2016 2:08 pm
Those PB8/PB9 needs to use the REMAP mode, therefore not the default.
Try PB6/PB7 instead …

marcin.kasinski
Wed Sep 14, 2016 6:41 am
martinayotte wrote:Those PB8/PB9 needs to use the REMAP mode, therefore not the default.
Try PB6/PB7 instead …

martinayotte
Wed Sep 14, 2016 12:24 pm
In the STM32F1/libraries/Wire/HardWire.cpp file, the call to i2c_master_xfer() has a 0 for timeout parameter, which means “infinite”.
You can change it to some other values in milliseconds.

Leave a Reply

Your email address will not be published. Required fields are marked *