I am having many problems on the slave(STM/MBED) side of my I2C communication. I am trying to connect a Raspberry Pi 3B+ (Master) to an STMF303K8 (Slave). On the Rpi side, I have successfully connected to an arduino uno and transferred data back and forth but when I replace the arduino with the stm I get no detection of an I2c device from the the R Pi. I try setting the STM clock frequency to 100KHz but it says there is no ‘frequency’ function in I2CSlave class. I don’t know how to initialize my STM slave device. My code is below
#include "mbed.h"
#include "I2CSlave.h"
using namespace std;
Serial pc(USBTX, USBRX);//-----serial monitor connection
I2CSlave slave(PB_7,PB_6); //Configure I2C slave
DigitalOut LD(PA_12); //-----------LD Toggle on/off---------[D2 pin5]
char i2c;
int main() {
//slave.frquency(100000); // Error here: "Class 'mbed::I2CSlave' has no member 'frequency'"
slave.address(0x05);
//slave.start(); // Error here: "Class 'mbed::I2CSlave' has no member 'start'"
printf("STM STARTING/r/n");
printf("STM Ready/r/n");
while(1){
i2c = slave.read();
printf("%c",i2c);
}
}
Mbed’s side:
Nucleo-F303R8 with MbedOS 6.2.1 bare metal profile.
Copy&Paste example from documentation I2CSlave API (same what I wrote in your previous post)
I changed only pins in the constructor of I2C object from D14,D15 to D4,D5 according to Nucleo’s page
RPI’s side:
Raspberry PI 3 model B.
After long time I installed Raspbian (now called Raspberry Pi OS) again. In the clean installation I enabled I2C in a raspi-config according to this guide.
Then I wrote sudo i2cdetect -y 1 to RPI’s terminal and the Mbed slave device was found on address 0x50h(7bit) and that is 0xA0h (8bit) from the example .
Finally:
I’m not 100% sure but it looks like the problem what you have is the address what you chose because with your address 0x05h of the mbed slave device it not work.
If I correctly understand the description of address method of I2C API, the address must be placed in the 8 bit format. So if you want 0x05h (7bit) you need to place 0x0Ah (8 bit) into address method.
Your address 0x05h (8bit) was used as 0x02h(7bit) and 0x00h-0x02h are probably reserved addresses. So as a minimum you can use 0x06h (8bit).
I tried it on Mbed OS 6.2.1, you probably use a lower version then 6 but it will be similar.
Tips:
I use 4k7 pull-ups connected to 3.3V but 5V is also working.
When you use only printf witout an object you not need to declare the Serial object.
Because I was curious, I went deeper…
I found a library called PIGPIO which is already implemented in Raspberry PI OS. This library seems to be friendly and easy to use from terminal or a script.
Simple test what can be used from Raspberry’s terminal an cooperate together with Mbed’s I2CSlave example
sudo pigpiod activate the library
i2cdetect -y 1 - show address of each slave devices available on the I2C bus. In my case the slave addess is 0x03
pigs i2co 1 0x03 0 - open the i2c interface on the slave address with flag and return a handle
pigs i2crd 0 6 - read 6 bytes (String from Mbed’s example ‘Slave!’) from the handle
pigs i2cws 0 83 - write a single byte to the handle
The info about pigs commands are here or type pigs h to the terminal.
Tested with 100000hz of I2C bus.
Thank you. I followed your checklist here and believe I just didn’t build the appropriate circuit with pull up resistors as well as not understanding the 7 vs 8 bit addresses. Appreciate the help! Got it working!
Thank you for your feedback, I am glad my time was not wasted and you achieved what you wanted.
Please be so kinde and mark as Solution a post what was helpful for you. That will mark the topic as closed and will inform someone else “That is working” in the future.