cancel
Showing results for 
Search instead for 
Did you mean: 

Writing and reading register via I2C using HAL library

ECiot.1
Associate II

Hi, I have to write the value ADC_CONFIG_REG = 0x9FFF in a register with dimension of 16 bit. I write the following line of code but I receive a warning because the fuction want an uint8_t pointer but in my case the register and the &ADC_CONFIG_REG is 16 bit. Can I ignore the warning or I have to modify my code? Moreover can I read a register with the function HAL_I2C_Mem_Read or there are best function to use?

HAL_I2C_Mem_Write(&hi2c1, addr_dut1_V, ADDR_ADC_CONFIG_REG, I2C_MEMADD_SIZE_8BIT, &ADC_CONFIG_REG, sizeof(ADC_CONFIG_REG), HAL_MAX_DELAY);

 

 

 

2 REPLIES 2
STea
ST Employee

Hello @ECiot.1 ,

you can modify  the ode and cascade the value of the reg as shown in here :

int16_t* reg_ptr = (uint16_t*)&ADC_CONFIG_REG;

HAL_I2C_Mem_Write(&hi2c1, addr_dut1_V, ADDR_ADC_CONFIG_REG, I2C_MEMADD_SIZE_8BIT, (uint8_t*)reg_ptr, sizeof(*reg_ptr), HAL_MAX_DELAY); to avoid the warning .

still this depends on which register you are trying to write because I2C does not have direct access to the registers so you need to read the register configuration sent by I2C and write it to the register by software (by the cpu). so if you could give more details about your use-case this might help to further understand and support you .

BR

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
EXUE.2
ST Employee

generally, it is better don't ignore the warning, because this is easy to lead wrong address to the low level function.

additionally, there has one function as below as master to read one register value by the DUT:

/**
* @brief Receives in master mode an amount of data in blocking mode.
* @PAram hi2c Pointer to a I2C_HandleTypeDef structure that contains
* the configuration information for the specified I2C.
* @PAram DevAddress Target device address: The device 7 bits address value
* in datasheet must be shifted to the left before calling the interface
* @PAram pData Pointer to data buffer
* @PAram Size Amount of data to be sent
* @PAram Timeout Timeout duration
* @retval HAL status
*/
HAL_StatusTypeDef HAL_I2C_Master_Receive(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData,
uint16_t Size, uint32_t Timeout)