I am trying to read from a register in MAX17320 using I2C protocol but get always HAL_Error message. I have the chip MAX17320 and a microcontroller STM32F105, when I try to read from the register nDeviceName(121h) to see that the chip is working I get HAL_ERROR message.
As far as I understood, the slave address is 0x16 and the memory address for the specific register is 0x21 and the this register contains for a total of 5 words of data. I am writing this the code in STM32 Cube IDE.
It would be enormous help if someone could help me with this. This the link for the chip MAX17320: https://www.analog.com/media/en/technical-documentation/data-sheets/MAX17320.pdf
This is how I implemented the code:
HAL_StatusTypeDef MAX30205_readTemp(uint8_t dev_address)
{
HAL_StatusTypeDef ret;
uint8_t tempData[10];
// Get temperature data, reg 0, two bytes
ret = HAL_I2C_Mem_Read(&hi2c1, (uint16_t)(dev_address)|0x01, 0x21, 1, tempData, 10, 50);
if(ret != HAL_OK)
{
return ret;
}
}
Call the function
uint8_t dev_address = 0x16;
while (1)
{
MAX30205_readTemp(dev_address);
HAL_Delay(100);
}
thanx to @Justme and @Jim Fischer, for your answer, but unfortunately I couldn't figure it out what the problem is. this I try to provide more details if someone know what the problem is. The problem is that I still get a HAL_ERROR instead of HAL_OK when I debug the function:
ret = HAL_I2C_Mem_Read(&hi2c1, dev_address, 0x21, 1, tempData, 10, HAL_MAX_DELAY);
This is the whole code that I have written in STM32 CUbe IDE:
HAL_StatusTypeDef MAX30205_readTemp(uint8_t dev_address)
{
HAL_StatusTypeDef ret;
uint64_t tempData[10];
// read from the register sDeviceName
ret = HAL_I2C_Mem_Read(&hi2c1, dev_address, 0x21, 1, tempData, 10, HAL_MAX_DELAY);
if(ret != HAL_OK)
{
return ret;
}
return HAL_OK;
}
Here I call the function:
uint8_t dev_address = 0x16|0x01;
/* USER CODE BEGIN WHILE */
while (1)
{
MAX30205_readTemp(dev_address);
HAL_Delay(100);
}
/* USER CODE END 3 */
When I debug the code, I step into
ret = HAL_I2C_Mem_Read(&hi2c1, dev_address, 0x21, 1, tempData, 10, HAL_MAX_DELAY);
And after that, I come to these lines of code:
/* Send Slave Address and Memory Address */
if (I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, Timeout, tickstart) != HAL_OK)
{
return HAL_ERROR;
}
When I step into this if statement I come to this lines of code:
/* Send slave address */
hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(DevAddress);
/* Wait until ADDR flag is set */
if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK)
{
return HAL_ERROR;
}
then I step into the if statement, I come into this lines of code:
static HAL_StatusTypeDef I2C_WaitOnMasterAddressFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, uint32_t Timeout, uint32_t Tickstart)
{
while (__HAL_I2C_GET_FLAG(hi2c, Flag) == RESET)
{
if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
{
/* Generate Stop */
SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
/* Clear AF Flag */
__HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
hi2c->PreviousState = I2C_STATE_NONE;
hi2c->State = HAL_I2C_STATE_READY;
hi2c->Mode = HAL_I2C_MODE_NONE;
hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
/* Process Unlocked */
__HAL_UNLOCK(hi2c);
return HAL_ERROR;
}
After that it just return a HAL_ERROR and goes back to where I call the function. for me it seems that the device adress 0x16|0x10 = 0x17 does not acknowledges and that is why I got an HAL_ERROR, but maybe I am wrong. According to the datasheet for MAX17320, the device address is either 0x16 or 0x6C. And for this particular register sDeviceName (0x121) the device address 0x16 should be used. If someone know the problem please let me know.