6 years ago.

I2c communication between STM32F401re can not be performed

I made code, but it does not work.

I would like to let you know if there is something wrong. Also, please tell me if necessary initialization processing, if there are any points to watch out for.

  1. Master

#include "mbed.h"
const char addr_arduino = 0x48 << 1; // LM75 address

Serial pc(USBTX, USBRX);
I2C i2c(I2C_SDA, I2C_SCL);
DigitalOut myled(LED1);

int main()
{
    char data[8];
    long data_write[2];

    /* Configure the Temperature sensor device STLM75:
    - Thermostat mode Interrupt
    - Fault tolerance: 0
    */
    while(1) {
        data_write[0] = 1000.0*1000;
        data_write[1] = 2000.0*1000;
        //data = 'a'
        data[0]  = char((data_write[0]         ) & 0xFF);
        data[1]  = char((data_write[0]    >>  8) & 0xFF);
        data[2]  = char((data_write[0]    >> 16) & 0xFF);
        data[3]  = char((data_write[0]    >> 24) & 0xFF);
        data[4]  = char((data_write[1]         ) & 0xFF);
        data[5]  = char((data_write[1]    >>  8) & 0xFF);
        data[6]  = char((data_write[1]    >> 16) & 0xFF);
        data[7]  = char((data_write[1]    >> 24) & 0xFF);

        int status = i2c.write(addr_arduino, data, 8);

        wait(2);
        if (status != 0) { // Error
            myled = !myled;
            wait(0.2);
        }
    }
}

____________________

  1. Slave

#include <mbed.h>

I2CSlave slave(I2C_SDA, I2C_SCL);

int main()
{
    char buf[10];
    char msg[] = "Slave!";

    slave.address(0x48 << 1);
    while (1) {
        int i = slave.receive();
        switch (i) {
            case I2CSlave::ReadAddressed:
                slave.write(msg, strlen(msg) + 1); // Includes null char
                pc.printf("ReadAddressed");
                break;
            case I2CSlave::WriteGeneral:
                slave.read(buf, 8);
                pc.printf("WriteGeneral");
                break;
            case I2CSlave::WriteAddressed:
                slave.read(buf, 8);
                pc.printf("WriteAddressed");
                break;
        }
        for(int i = 0; i < 10; i++) buf[i] = 0;    // Clear buffer
    }
}

Dear Mizuguchi-san - we have reformatted your post so it is more legible. In the future, please use the code tags as shown in the "Editing Tips".

posted by Ralph Fulchiero 05 Nov 2018

1 Answer

6 years ago.

Hello.

For i2c master test with:

const char addr_arduino = 0x48;  // LM75 address (7 bit address)

and

for i2c slave use:

slave.address(0x48);

Please test again.