8 years, 5 months ago.

F401RE I2C library does not work?

Hello,

For few days, i had problems with I2C communication. I try to write and read byte to 24LC02 EEPROMs with following code:

#include "mbed.h"

I2C eeprom(I2C_SDA, I2C_SCL);
Serial pc(USBTX, USBRX);

int main()
{
    pc.printf("Hello\r\n");
    eeprom.frequency(100000);
    
    int ack;
    char ret;
    int device_address_write = 0xA0;
    int device_address_read = 0xA1;
    int data = 0x6F;
    
    // WRITE OPERATION
    eeprom.start();
    ack = eeprom.write(device_address_write);
    wait_ms(10);
    printf("%d", ack);
    ack = eeprom.write(0x00);
    wait_ms(10);
    printf("%d", ack);
    ack = eeprom.write(data);
    wait_ms(10);
    printf("%d", ack);
    eeprom.stop();
    wait_ms(10);
    
    // READ OPERATION
    eeprom.start();
    ack = eeprom.write(device_address_write);
    wait_ms(5);
    printf("%d", ack);
    ack = eeprom.write(0x00);
    wait_ms(5);
    printf("%d", ack);
    eeprom.start();
    ack = eeprom.write(device_address_read);
    wait_ms(5);
    printf("%d", ack);
    ret = eeprom.read(0);
    wait_ms(5);
    eeprom.stop();
           
    pc.printf("Written:%2X,  Read:%2X", 0x6F, ret);
}

Are there any known issues in embed I2C library or am I making mistake(s)?

Hello again,

At last i manage to write and read 1 byte data to Microchip 24LC02.. The primitive read and write functions of I2C mbed library does not work.. So we use other wrtie and read functions.

The below code writes and read 100 (decimal) to EEPROM address 0x00. (random address read and write)

While writing 0x0A is the device address (10100000), while reading the address is 0xA1 (10100001).

#include "mbed.h"

I2C eeprom(I2C_SDA, I2C_SCL);
Serial pc(USBTX, USBRX);

int main()
{
    pc.printf("Hello\r\n");
    eeprom.frequency(100000);
    int ack;
    char data[2];
    data[0] = 0x00;
    data[1] = 100;
    
    
    char ret = 0x00;

    
    // WRITE OPERATION
    //eeprom.start();
    ack = eeprom.write(0xA0, data, 2, false);
    wait_ms(10);
    pc.printf("%d", ack);
    //eeprom.stop();
    
    
    // READ OPERATION
    //eeprom.start();
    ack = eeprom.write(0xA0, &data[0], 1, true);
    pc.printf("%d", ack);
    eeprom.read(0xA1, &ret, 1, false);
           
    pc.printf("Written:%d,  Read:%d", data[1], ret);
}
posted by Kamil M 14 Nov 2015

1 Answer

8 years, 5 months ago.

In general don't use the the 1 byte read/write functions. I think the only reason they haven't been removed yet is because a few devices use non-standard I2C communication. I don't know if it could be better supported in the F401, but I do know in enough mbed devices it is already supported with hacks, since most I2C peripherals were never intended to be controlled per byte.

It should be better to indicate 1 byte read/write operations are obsolete for some devices in I2C page of handbook. It cost me a very precious time to since only in one page of mbed questions it is discussed...

Thanks...

posted by Kamil M 14 Nov 2015