I2C hardwire library communication between 2 (two) stm32 maple mini

konczakp
Thu Dec 08, 2016 1:37 pm
I have established communication between maple mini on port i2c2 with BMP085 using hardwire library and it’s working as it should. Now I need to establish a connection from i2c2 from this first maple mini to i2c1 on second maple mini. I used to work with i2c communication protocol on arduino using wire library but I cannot get this working on maple and hardwire. (I’m using hardwire library because wire library was not working with BMP085). My problem is that I’m trying to send data from second (let us name it as slave) to the first one (let say master). And I’m not getting any data at master. What am I doing wrong?

Slave code
#include <HardWire.h>

#define I2C_ADDRESS_ME 0x02
HardWire HWire(1, I2C_FAST_MODE); // I2C

String rfidout;

void setup() {
Serial1.begin(9600);
Serial.begin(115200);
HWire.begin(I2C_ADDRESS_ME);
delay(3000);
Serial1.write(0x04);
Serial.println("start");
}

void loop() {

while (Serial1.available()) {
byte C = Serial1.read();
rfidout += C;
}

if (rfidout.length() > 0){
if (rfidout == "4"){
//Serial.println("ok");
HWire.beginTransmission(0x01);
HWire.write('O');
HWire.endTransmission();
} else {
//Serial.println("err");
HWire.beginTransmission(0x01);
HWire.write('E');
HWire.endTransmission();
}
rfidout = "";
}

}


stevestrong
Thu Dec 08, 2016 7:26 pm
Shouldn’t match both (master and slave) addresses?
Btw, normally only the master can write or read bytes from slave, but never vice versa.

martinayotte
Thu Dec 08, 2016 8:10 pm
Those Master/Slave code look strange. Slave seems to send data even if Master doesn’t request it !
It look like the Slave is acting as been a Master :o
Normally Slave should have onReceive() and/or onRequest() handler.
But it seems that Slave isn’t very implemented on STM32.

konczakp
Thu Dec 08, 2016 9:16 pm
On Arduino this code was working. I was using I2C like UART and it was working like a charm. All devices were sending data to each other and there was no slave/master – they were not asking for data they were just sending them. So I wanted to achieve something like on arduino. If that is not a good approach then how should I do this ? Here is the scenario:

1st maple need to get data from BMP085 and other devices like RTC etc and also be able to send data to 2nd maple without request from 2nd maple
2nd maple need to get data from RFID and some other devices and send that data to 1st maple. This maple also can respond for request from 1st maple

So both maple are masters and slaves for me. Or Am I wrong ?

How should I do this ?


ddrown
Thu Dec 08, 2016 10:41 pm
I’m not sure if this will help you because it isn’t Arduino, but I have a code example for an i2c slave for STM32CubeMX: https://github.com/ddrown/stm32-output- … 2c_slave.c

I chose a very simple slave protocol, when the master signals a read, it just writes all registers out (the struct i2c_registers, 17 bytes in my case).

When the master signals a write, the slave expects the first byte for offset into the struct and then the data to put into the struct.


martinayotte
Thu Dec 08, 2016 11:10 pm
@konczakp , it is up to you to have such Multi-Masters design, but if there is no arbritation and/or handshaking, if all I2C devices can speak without a Master orchestration, then you can end-up with tons of collisions, on both data and clock lines.

konczakp
Fri Dec 09, 2016 8:51 am
Here is the approach for arduino working example: https://michael.bouvy.net/blog/en/2013/ … aster-i2c/ In my opinion there is no collision problem because each time when the device is trying to send data is becoming a Master and it will not became a master if there is a communication on the line going on. Then the new master is waiting until it will be free.
I need to get the same for stm32. Normally arduino wire lib has implementation for multi master I2C bus but in library for stm32 I cannot find this. For example Wire.onReceive is missing and I don’t know how to get this done. If it is impossible to do this in this way then how ?

RogerClark
Fri Dec 09, 2016 9:02 am
You could try using STM’s own / new Arduino core.

See this post

http://www.stm32duino.com/viewtopic.php?f=16&t=1553

Technically STM have only written it to work on the Nucleo F103 but the community (@danieleff etc) have added support for the BluePill and Maple mini

However I don’t think anyone has tested I2C slave functionality


martinayotte
Fri Dec 09, 2016 2:53 pm
@konczakp , even after looking at this link https://michael.bouvy.net/blog/en/2013/ … aster-i2c/ and reading the forum post mentioned, as well as original Arduino I2C implementation code, I don’t see any Multi-Master arbitration, so collisions risks are still present. I would rather stick with single master scheme with requestFrom() to avoid those risks.

konczakp
Fri Dec 09, 2016 3:16 pm
ok but there will be another problem with this because when I start I2C instance with HardWire lib and specify the address of the slave I’m not able to see that slave because when I use i2c hardwire scanner example it is only showing me my BMP085 and nothing else. Scanner is not showing me address of my maple slave

mrburnette
Sat Dec 10, 2016 3:06 pm
konczakp wrote:<…>I used to work with i2c communication protocol on arduino using wire library but I cannot get this working on maple and hardwire. (I’m using hardwire library because wire library was not working with BMP085).<…>

martinayotte
Sat Dec 10, 2016 3:11 pm
konczakp wrote:because when I use i2c hardwire scanner example it is only showing me my BMP085 and nothing else.

konczakp
Sat Dec 10, 2016 6:40 pm
#include <HardWire.h>

HardWire HWire(1, I2C_FAST_MODE); // I2c1

void setup() {
Serial.begin(115200);
HWire.begin(4);
}

void loop() {

while(1 < HWire.available()) // loop through all but the last
{
char c = HWire.read(); // receive byte as a character
Serial.print(c); // print the character
}
int x = HWire.read(); // receive byte as an integer
Serial.println(x); // print the integer

}


Leave a Reply

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