Arm Mbed OS support forum

How to read device registry with new i2c_api.h API?

How to read I2C device registry with the new i2c_api.h API? E.g. EEPROM or value from accelerometer.
It seems this API is missing function like HAL_I2C_Mem_Read() available in stm32f7xx_hal_i2c.h.
Or is it me missing something?

Below is what I have tried. The code fails while reading (value = -1).

#include "i2c_api.h"
#include "mbed_error.h"

i2c_t i2c;
i2c_init(&i2c, PB_7_ALT0, PD_12);

for (uint32_t offset = 0; offset < 256; offset++) {
   i2c_start(&i2c);
   //written = i2c_write(&i2c, Address, (char[]){offset}, 1, 0); // nack
   int written1 = i2c_byte_write(&i2c, Address);
   int written2 = i2c_byte_write(&i2c, offset);
   i2c_start(&i2c);
   int written3 = i2c_byte_write(&i2c, Address + 1);
   int value = i2c_byte_read(&i2c, 1);  // nack

   if (written1 != 1 || written2 != 1 || written2 != 1 || value < 0) {
      mbed_error_printf("Error writting to device. Write1: %d, Write2: %d, Write3: %d, Value: %d", written1, written2, written3, value);
      break;
   } else {
      mbed_error_printf("Device read: 0x%X = 0x%X\r\n", offset, value);
   }
}

What device are you using?

Looks like you are scanning all the i2c device address and not setting the internal registers to input and output data.

The device data sheet will give you the register and values to write to which will then output the data or wait for you to write data into the device.
There will probably a library somewhere to save you the trouble.

I use this for 24Cxx i2c eeprom’s:

https://os.mbed.com/users/bborredon/code/eeprom/

Let’s assume I need to read EEPROM.
I know how to do it using HAL_I2C_Mem_Read() function.
The question was if this was possible with the ‘new’ I2C API.

Ahoj,

I do not know what you mean with “new I2C API”, but Mbed’s I2C documentation of I2C API will help you.

BR, Jan