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 = "";
}
}
Btw, normally only the master can write or read bytes from slave, but never vice versa.
It look like the Slave is acting as been a Master
Normally Slave should have onReceive() and/or onRequest() handler.
But it seems that Slave isn’t very implemented on STM32.
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 ?
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.
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 ?
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
#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
}

