Reputation: 1
I want to use the HAL_I2C_Mem_Read()
function to read a control register of ZSC31050 and then configure the control register by using the HAL_I2C_Mem_Write()
function.
The default device address of ZSC31050 is 78
hex.
It is supposed to be shifted left by 1 bit.
So while using the write function is it supposed to be f0
and while using read function as f1
?
Upvotes: 0
Views: 509
Reputation: 7
You need to shift the address by 1 bit.
The HAL library then sets the read/write bit by itself.
Here a shot example how I used it on a STM32F0G71:
uint16_t i2CAddress{0x44 << 1};
HAL_StatusTypeDef result = HAL_I2C_Mem_Read(handle_, i2CAddress, cmd, sizeof(cmd), dataBuffer, sizeof(dataBuffer), 10);
And for only writing:
HAL_StatusTypeDef result = HAL_I2C_Master_Transmit(handle_, i2CAddress, txData, sizeof(txData), 10);
Upvotes: 0
Reputation: 41
You're right on your understanding of I2C addresses, but not right about STM32 I2C functions.
You do not need to shift the address manually. HAL_I2C_Mem_Write() and HAL_I2C_Mem_Read() already do that for you.
See I2C HAL module driver for STM32F4
Upvotes: 0
Reputation: 4317
Yes, that is basically correct. For I2C using 7-bit addresses, the first 7 bits of the first byte are the address, and the least-significant bit is the read/write bit.
So as you said, you need to shift the address left by one, and then set the least significant bit for a read, and leave it clear for a write.
See e.g. this good description of I2C from Texas Instruments. (Particularly section 3.)
Upvotes: 0