Arduino_Core_STM32F1 & I2C

caniggia
Fri May 26, 2017 11:13 am
Hi

I have tried “Arduino_Core_STM32F1” repo:
https://github.com/stm32duino/Arduino_Core_STM32F1
, because i need uC as I2C slave.
I found out that I2C works only on PB8 & PB9.

As i have boards with SCL & SDA on PB6 & PB7 i have a question:
has anyone managed to get I2C working on PB6 & PB7 with this repo?

thx
caniggia


fpiSTM
Sat May 27, 2017 7:19 am
Hi @caniggia,

you’re right i2c pins are hardcoded.
https://github.com/stm32duino/Arduino_C … twi.c#L148

By changing, the g_i2c_init_info it should be possible to get it on PB6 & PB7.

FYI, this repo will be merged in the generic Arduino core for STM32 soon, in this repo i2c pins are not hardcoded.
https://github.com/stm32duino/Arduino_Core_STM32


caniggia
Mon May 29, 2017 9:02 am
Hi @fpiSTM
Thanks for your reply.

Before i try to change I2C to PB6 & PB7, i have tested Wire library again on original PB8 & PB9.

“master_writer” & “slave_receiver” are working OK.

But, “master_reader” & “slave_sender” don’t work.
Master just reads 6 bytes of 0x00(SDA is always LOW) and this 4-5 times then communication stops(SCL goes LOW and it stays there).
I have to reset slave to start it again.
Tested with 2x Bluepill and external pull-ups(4k7).

Can someone please confirm this and if possible give some advice/solution to whats wrong here?
Thnx
caniggia


Wi6Labs
Mon May 29, 2017 12:17 pm
Hi @caniggia

Unfortunately the Tx slave mode doesn’t work with STM32F1. It is a known issue.
Tx and Rx slave mode are not compatible in the current HAL version. So to be compatible with Wire library, we have decided to set Rx slave mode by default.
We are waiting for a new HAL version.

Please let me know if this feature is critical for you.


caniggia
Mon May 29, 2017 1:07 pm
Hi @Wi6Labs
Thanks for your reply.

Yes, it’s critical for me to have Rx Slave & Tx Slave mode.
When will the new HAL be ready?

Can you point me to any other library(Tx & Rx Slave support), even if non Arduino library.
Some simple example how to use library would be nice too.

thnx
caniggia


Wi6Labs
Thu Jun 01, 2017 1:53 pm
@caniggia

I fixed the problem with Tx slave mode because there was one indeed.
I added an example in Wire library to give a solution to use slave in Tx & Rx mode.

You can find the patch here for the moment: https://github.com/stm32duino/Arduino_C … F1/pull/46

However, as I said previously, you can’t enable Rx and Tx slave mode in same time.
I don’t know when a new HAL version will be released and whether this problem will be fixed.

I hope this may help you.


caniggia
Thu Jun 01, 2017 4:07 pm
@Wi6Labs

Thanks,
i’m gonna test it right tomorrow morning.

caniggia


caniggia
Fri Jun 02, 2017 10:45 am
Hi Wi6Labs

I’ve downloaded your 3 files and tried your slave_receiver_sender.ino.
What master sketch did you use to test it?
I have combined master_writer & master reader.
master_writer_reader:
#include <Wire.h>

void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600);
}

byte x = 0;

void loop()
{
Wire.beginTransmission(2); // transmit to device #4
Wire.write("x is "); // sends five bytes
Wire.write(x); // sends one byte
Wire.endTransmission(); // stop transmitting

delay(20); // had to add delay or slave won't respond to read request
Wire.requestFrom(2, 6); // request 6 bytes from slave device #4

while(Wire.available()) // slave may send less than requested
{
char c = Wire.read(); // receive a byte as character
Serial.print(c); // print the character
}

x++;
delay(500);
}


caniggia
Fri Jun 02, 2017 11:57 am
Maybe this will help.

I’ve changed delay from 20ms to 100ms:
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
x is b
x is c
x is d
x is e
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello


Wi6Labs
Wed Jun 07, 2017 8:54 am
Hi caniggia

I pushed my example of master reader & writer.

I tried with 10ms delay between each request and I found 2 issues:

  • The delay inside the slave loop example must be less than the delay between read and write command inside the loop of master example. Maybe you can just remove it.
  • The Serial speed must be increase to allow to exit the function receiveEvent() before the master send the next request (I tried with 115200 baud).

I hope this will solve your problem.


fpiSTM
Wed Jun 07, 2017 9:18 am
Hi @caniggia,
@Wi6Labs has raised an issue on the repo to follow this issue.
https://github.com/stm32duino/Arduino_C … /issues/47
If his correction is ok it will be merged.
Else as I mentioned, this point will be duplicated on the new repo and fixed on it when F1 will be merged.

caniggia
Thu Jun 08, 2017 8:44 am
Hi @Wi6Labs & @fpiSTM

Thanks for your effort to get this slave library to work.

The delay inside the slave loop example must be less than the delay between read and write command inside the loop of master example. Maybe you can just remove it.
The Serial speed must be increase to allow to exit the function receiveEvent() before the master send the next request (I tried with 115200 baud).

Sounds very logical.
I don’t know how did i missed delay in slave loop. I should realy get some glasses :)

I’ll try it as soon as i have some time, probably tommorow.

Thanks
caniggia


caniggia
Mon Jun 12, 2017 12:25 pm
Hi @Wi6Labs

In slave sketch i’ve commented out delay in loop.
And also Serial.print’s in receiveEvent.

It works OK even without delay between Read & Write in master.

Then i’ve changed the order in master. First Write then Read.
Because i’m gonna use it that way.
Master sends command and then get data depending on that command.
I’ve needed 100us(micro sec.) delay between Write & Read.

I’ve run it over the weekend and it looks OK to me.

Thnx
caniggia


Wi6Labs
Mon Jun 12, 2017 12:43 pm
Hi @caniggia

Thank you for your feedback.
Performances look like very good.
We will merge the patch.

Thanks
Wi6Labs


Leave a Reply

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