Arduino_Core_STM32F1 & I2C

The official STMicroelectronics Arduino core
caniggia
Posts: 27
Joined: Tue Dec 06, 2016 2:38 pm

Arduino_Core_STM32F1 & I2C

Post by 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
Posts: 224
Joined: Fri Sep 16, 2016 12:33 pm
Location: Le Mans, France

Re: Arduino_Core_STM32F1 & I2C

Post by 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
Posts: 27
Joined: Tue Dec 06, 2016 2:38 pm

Re: Arduino_Core_STM32F1 & I2C

Post by 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

User avatar
Wi6Labs
Posts: 25
Joined: Fri Sep 16, 2016 11:39 am
Location: Rennes, France
Contact:

Re: Arduino_Core_STM32F1 & I2C

Post by 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.
Wi6Labs team

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

Re: Arduino_Core_STM32F1 & I2C

Post by 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

User avatar
Wi6Labs
Posts: 25
Joined: Fri Sep 16, 2016 11:39 am
Location: Rennes, France
Contact:

Re: Arduino_Core_STM32F1 & I2C

Post by 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.
Wi6Labs team

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

Re: Arduino_Core_STM32F1 & I2C

Post by caniggia » Thu Jun 01, 2017 4:07 pm

@Wi6Labs

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

caniggia

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

Re: Arduino_Core_STM32F1 & I2C

Post by 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:

Code: Select all

#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);
}
Result on master serial output:

Code: Select all

x is <25>
x is <26>
hello 
x is <28>
x is <29>
x is <30>
x is <31>
x is  
hello 
x is "
x is #
x is $
x is %
x is &
hello 
x is (
x is )
x is *
x is +
x is ,
x is -
x is .
x is /
x is 0
x is 1
hello 
x is 3
x is 4
x is 5
x is 6
x is 7
x is 8
x is 9
x is :
x is ;
x is <
hello 
As you can see i get mkixed "hello " and data that master sends.
is there missing some buffer clearing?
Slave actualy send this data("x is ?"):
Image

And also, after some time the communication just stops (SCL goes to LOW and stays there).

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

Re: Arduino_Core_STM32F1 & I2C

Post by caniggia » Fri Jun 02, 2017 11:57 am

Maybe this will help.

I've changed delay from 20ms to 100ms:

Code: Select all

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
It's better but still.

With delay 200ms didn't se any problem, but comunication hangs.
With delay 300ms didn't se any problem, and comunication still running after 1h.

300ms is very long time to wait after writing the data and before sending "requestFrom".
Why such a huge delay is needed?

thnx
caniggia

User avatar
Wi6Labs
Posts: 25
Joined: Fri Sep 16, 2016 11:39 am
Location: Rennes, France
Contact:

Re: Arduino_Core_STM32F1 & I2C

Post by 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.
Wi6Labs team

Post Reply