5 years, 12 months ago.

STM32 Nucleo-64 F103RB I2C Slave ReadAddressed is not stable

Hello Good Day!

I have 2pcs of F103RB, one is working as Master device and the other is slave. I test the WriteAddressed and it works fine but for the ReadAddressed it is not stable. Sometimes the master can received the data correctly, sometimes not.

Here is the code for the master:

F103RB_I2C_Master_ReadAddress_Test/main.cpp

#include "mbed.h"
#define BaudRate 115200
#define Buffer 11
I2C i2c(I2C_SDA, I2C_SCL); // sda, scl
Serial pc(USBTX,USBRX,NULL,BaudRate);

int main() {
    while (1)
    {
        char command[1];
        char data[Buffer];
        command[0] = 0x07;
        i2c.write(0x57, command, 1, true);
        i2c.read(0x57, data, Buffer);
        /*
        for(char i = 0; i < Buffer; i++) {
            pc.printf("0x%02x ", data[i]);
        }
        pc.printf("\r\n");
        */
        pc.printf("%s\r\n", data);
        
        
            
    }
}

For Slave device:

F103RB_I2C_Slave_ReadAddress_Test/main.cpp

#include <mbed.h>

#define BaudRate 115200

I2CSlave slave(I2C_SDA,I2C_SCL);
Serial pc(USBTX,USBRX,NULL,BaudRate); //open serial port (optionally add device name and baud rate after specifying TX and RX pins)

char buf[10];
char msg[] = "abcd";


int main() {

    pc.printf("Slave I2C device\n\r");
    slave.address(0x57);
    
    while(1) {
        int i = slave.receive();
        switch(i)
        {
            case I2CSlave::ReadAddressed:
                slave.write(msg,strlen(msg)+1);
                printf("ReadAddressed\n\r");
                break;
            case I2CSlave::WriteGeneral:
                printf("WriteGeneral\n\r");
                slave.read(buf,10);
                printf("Read G: %s\n\r",buf);
                break;
            case I2CSlave::WriteAddressed:
                printf("WriteAddressed\n\r");
                slave.read(buf,10);
                printf("Read A: %s\n\r",buf);
                break;
        }
        for (int i=0; i<10; i++) buf[i] = 0;
    }
}

I'm still hoping that this issue will be fixed.

Best Regards, Lester Lecong

I have another problem. F103RB has 2 I2Cs (P_11 P_10 & P_9 P_8) and when both of them use to read sensor, one of them stopped working.

posted by Arnas MasRodjie 08 May 2018

Hi Lester - do you have external pull-ups on the SCL and SDA lines? These are likely required as we tried using the internal pull-ups but never see an ACK on the master write. Could you check the value returned by slave.write()?

posted by Ralph Fulchiero 08 May 2018
Be the first to answer this question.