cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_I2C_Mem_Read () library does not add the read bit when trying to read from a register.

Ali Suleimani
Associate II

Hi

I am using HAl_I2C_Mem_Read library to read data from a register. But the HAl-I2C_Mem_Read does not add the LSb bit for read bit. For example if the device adress is 0x6C, the 8th bit shout be 1 for reading but it is 0 and I get an Ack failer. Even if I do like 0x6C|0x01 to make the 8th bit 1 for reading it still is 0 and I get ACK failer. 

Hope that anyone can help with this.

This is the code. 

HAL_StatusTypeDef MAX30205_readTemp(uint8_t dev_address)
{
HAL_StatusTypeDef ret;
uint64_t tempData[2];
// read from the register sDeviceName
ret = HAL_I2C_Mem_Read(&hi2c1, dev_address, 0x121, 2, tempData, 2, HAL_MAX_DELAY);
if(ret != HAL_OK)
{
return ret;
}
return HAL_OK;
}

Here I call the function  MAX30205_readTemp(uint8_t dev_address) and send the device address:

 while (1)
  {
    /* USER CODE BEGIN 3 */
  MAX30205_readTemp(0x16);
/* USER CODE END 3 */
  }
And I also tried to decode it using ossilscop: 
AliSuleimani_0-1690218318021.jpegAliSuleimani_1-1690218318619.jpeg

 

  
1 ACCEPTED SOLUTION

Accepted Solutions

Cause it's WRITING the memory/register address FIRST...

ret = HAL_I2C_Mem_Read(&hi2c1, dev_address, 0x121, 2, tempData, 2, HAL_MAX_DELAY);

The SLAVE address is not 0x16 or 0x6C, and the REGISTER address is 8-bit wide.. Would strongly suggest reading some data sheets.. It's not responding because you're addressing it entirely incorrectly.

https://www.analog.com/media/en/technical-documentation/data-sheets/MAX30205.pdf

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

5 REPLIES 5

Cause it's WRITING the memory/register address FIRST...

ret = HAL_I2C_Mem_Read(&hi2c1, dev_address, 0x121, 2, tempData, 2, HAL_MAX_DELAY);

The SLAVE address is not 0x16 or 0x6C, and the REGISTER address is 8-bit wide.. Would strongly suggest reading some data sheets.. It's not responding because you're addressing it entirely incorrectly.

https://www.analog.com/media/en/technical-documentation/data-sheets/MAX30205.pdf

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Hi 

Thank you for your hint. Could you be more precise why I addressing it entirely incorrectly? The data sheet says that it should first write slave address, then memory address and then the read slave address. I thought the function ret = HAL_I2C_Mem_Read(&hi2c1, dev_address, 0x121, 2, tempData, 2, HAL_MAX_DELAY); do the writing and reading for me. 

AliSuleimani_0-1690282964151.png

 

Ok, cite your sources.. You're clearly looking a different documentation than I am.

Diagram how you have the parts wired up, it will impact the SLAVE ADDRESS. The default strapping presumably dev_address = 0x90; You are using a MAX30205 right? Not something else? If the address is wrong the slave won't ACK, it's how you determine a part is present and responding.

The Memory address is 8-bits wide (1-Byte), not 16-bit (2-Byte)

Surely the rational interpretation for the example diagram (2nd line) would be

 

uint8_t tempData[4];  // 4 consecutive bytes read
ret = HAL_I2C_Mem_Read(&hi2c1, dev_address, 0x1C, 1, tempData, sizeof(tempData), HAL_MAX_DELAY); 

 

 

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Hi Sir!

Sorry for being inconvenient, I am using MAX17320 not MAX30205. this is the link for the IC I use: https://www.analog.com/media/en/technical-documentation/data-sheets/MAX17320.pdf 

So I try to read the register value DevName register (021h) with slave address 0x6C and I write the function 

uint8_t tempData[4];  // 4 consecutive bytes read
ret = HAL_I2C_Mem_Read(&hi2c1, dev_address, 0x1C, 1, tempData, sizeof(tempData), HAL_MAX_DELAY);

This is a screenshot from the datasheet for the register DevName(o21h)

AliSuleimani_0-1690367736552.png

Suppose the slave address (0x6C) is correct, is it correct that to read from a register we write the

uint8_t tempData[2];

HAL_I2C_Mem_Read(&hi2c, 0x6C, 0x21, 1, tempData, sizeof(tempData), HAL_MAX_DELAY); or do we need to write the HAL_I2C_Mem_Write(....) function before the HAL_I2C_Mem_Read(...) function because we want to first write the register address to the chip?

 

 

HAL_I2C_Mem_Read() is a compound function, see library source, it writes the internal address as the first step, and then in a second step it reads the content.

The MAX17320 uses 8-bit addressing, and I2C and SMB, with a different I2C Slave Address depending on the subset of registers being used 0x6C for 0x00..0xFF, and 0x16 for 0x180..0x1FF (0x80..0xFF)

Register 0x0121 looks like it would need to be SMB protocol, not I2C

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..