Skip to main content
Associate
July 24, 2023
Solved

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

  • July 24, 2023
  • 1 reply
  • 13707 views

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

 

  
    This topic has been closed for replies.
    Best answer by Tesla DeLorean

    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

    1 reply

    Tesla DeLorean
    Tesla DeLoreanBest answer
    Guru
    July 24, 2023

    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 VenmoUp vote any posts that you find helpful, it shows what's working..
    Associate
    July 25, 2023

    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

     

    Tesla DeLorean
    Guru
    July 25, 2023

    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 VenmoUp vote any posts that you find helpful, it shows what's working..