Showing results for 
Search instead for 
Did you mean: 

HAL_I2C_Mem_Read returns nothing?

Associate III

I use MAX9611 to measure current. It has 2 register to read current. 12 bits divides 2 bytes. MSB(8 bits) and LSB(4 bits). I tried to read only MSB but I couldn't, Can anyone help me?

 Note: there is no externall pull up res, I configure an internal pull up.
_legacyfs_online_stmicro_images_0693W00000dJpJhQAK.pngI write a simple code. HAL_I2C_IsDeviceReady return HAL_OK. But when I debug HAL_I2C_Mem_Read returns nothing

 uint16_t deviceAdress = 0xE1;
uint16_t regAdress = 0x02;
uint8_t CurVal;
void isDeviceReady()
	HAL_StatusTypeDef status;
	status = HAL_I2C_IsDeviceReady(&hi2c1, deviceAdress, 1, 50);
	if(status == HAL_OK)
		return 1;
		return 0;
	  HAL_GPIO_WritePin(ENABLE_M_GPIO_Port, ENABLE_M_Pin, 1); // Enables mosfet
	  HAL_I2C_Mem_Read(&hi2c1, deviceAdress, reg_adress, 1, &CurVal, 1, 100);

Pavel A.
Evangelist II

void isDeviceReady... returns 1? Really? Please make a habit to fix all compiler error AND warnings.

Call HAL_I2C_Mem_Read only after isDeviceReady returns 1.

> uint16_t deviceAdress = 0xE1;

Suspicious... the lowest bit of device address normally is 0.

Though the ST library always sets this address bit correctly for the function, so 0xE1 may work as is.

First of all thank you for your reply,

I wondered how could I check if line is idle and search for it. I saw it in a video and I implemented it. When I debug code, it returns HAL_OK every time. But I will check and make it in a proper way.

I check MAX9611's datasheet, to read data device adress 0XE1 and write adress is 0xE1 in my device's condition. MAX9611's A0 and A1 pins connected to GND.

For reading, first I send a data in int main, it is how it is written like this in datasheet.

And then in while(1)


HAL_I2C_Mem_Write(&hi2c1, 0xE0, 0x02, 1, CurVal, 1, 100);

HAL_GPIO_WritePin(ENABLE_M_GPIO_Port, ENABLE_M_Pin, 1); // Enables mosfet

HAL_I2C_Mem_Read(&hi2c1, 0xE1, 0x02, 1, CurVal, 1, 100);

I observe load draws 500mA from powersupply when I open mosfet. But buffer is empty.

When I read temperature register(0x08) it turns 128. This is not possible either.