2023-11-28 02:12 AM
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);
It should be 0XD5
2023-11-28 06:34 AM
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.
2023-11-28 04:52 PM
我知道这一点,
我的问题是,地址 bito 会设置为 1 吗,
通过 HAL_ I2C_ Mem_ Read 从地址发送 bito 设置为 0,
我不知道为什么
2023-11-28 04:52 PM
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
2023-11-28 07:19 PM - edited 2023-11-28 07:19 PM
Okay I'll try again.
HAL_I2C_Mem_Read does two things:
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.