Skip to main content
Visitor II
November 28, 2023
Question

STM32U5 I2C

  • November 28, 2023
  • 3 replies
  • 4138 views

STM32U5  + LSM6dsl
I want to read the general accelerometer register of the I2C interface.

I think it can be achieved by using HAL_I2C_Mem_Read ().

I set the second argument DevAddress to the value of the accelerometer slave address shifted left by 1 bit.

This is a HAL rule.
At this time, is it unnecessary to set bit0 of DevAddress to 1?

HAL_ I2C_ Mem_ Read did not set the bit0 of DevAddress to 1.

I don't know why
Code
HAL_StatusTypeDef status = HAL_OK;
status = HAL_I2C_Mem_Read( &hi2c3, (0x6a<<1), reg_addr, I2C_MEMADD_SIZE_8BIT, data_ptr, len, I2Cx_FLAG_TIMEOUT);

caiChangKun_0-1701166262232.png

It should be 0XD5



    This topic has been closed for replies.

    3 replies

    Super User
    November 28, 2023

    HAL_I2C_Mem_Read is a two-stage process. First it writes the memory address to the chip, then it reads a byte. The first operation is a write, the second is a read.

    That's how I2C memory read operations are done on most chips, including this one.

    Visitor II
    November 29, 2023

    I know this,

    My question is, will the address bito be set to 1,

    Through HAL_ I2C_ Mem_ Read to send from address bito set to 0,

    I don't know why

    Visitor II
    November 29, 2023

    我知道这一点,

    我的问题是,地址 bito 会设置为 1 吗,

    通过 HAL_ I2C_ Mem_ Read 从地址发送 bito 设置为 0,

    我不知道为什么

     

    Super User
    November 29, 2023

    Okay I'll try again.

    HAL_I2C_Mem_Read does two things:

    • Write 1 byte
    • Read 1 or more bytes

    In the first transaction (write 1 byte), bit 0 will be 0.

    In the second transaction (read 1 or more bytes), bit 0 will be 1.

     

    In your case, it looks like you get a NAK on the first transaction so the function aborts.

     

    Use HAL_I2C_IsDeviceReady to verify the slave is on the bus and responding appropriately before using any other HAL_I2C_* calls. HAL_I2C_IsDeviceReady should return HAL_OK.