cancel
Showing results for 
Search instead for 
Did you mean: 

Cannot write data into buffer using HAL_I2C_Mem_Read_IT

delsey
Associate

Hi All,

I'm currently trying to interface with a VL53L4CD sensor using the ULD provided by ST. I understand that it is required to write your own code in the "platform" files to interface with the API, but I cannot seem to get my I2C implementation writing into a buffer. It is probably really trivial, but here is the code:

In main.c:

	status = VL53L4CD_GetSensorId(dev, &sensor_id);
	if(status || (sensor_id != 0xEBAA))
	{
		printf("\r\n VL53L4CD not detected at requested address \n");
		return status;
	}

And in plaform.c:

uint8_t VL53L4CD_RdWord(Dev_t dev, uint16_t RegisterAdress, uint16_t *value)
{
	uint8_t status = 255;

	/* To be filled by customer. Return 0 if OK */
	/* Warning : For big endian platforms, fields 'RegisterAdress' and 'value' need to be swapped. */

	HAL_I2C_Mem_Read_IT(&hi2c1, dev, RegisterAdress, 2, (uint8_t*)&value, 2);

	status = 0;

	return status;
}

The function "GetSensorId" has index of 0x010F, which is successfully sent to the device. Data being read back from the sensor is 0xEBAA. This all ties up with what I'm expecting, yet the value of sensor_id remains as 0. Attached is a screenshot from my Logic Analyser.

Is there something else that i'm missing from how the HAL_I2C library writes to buffers? Any help would be greatly appreciated!

2 REPLIES 2
TDK
Super User

Use blocking code such as HAL_I2C_Mem_Read to ensure the data is written before the function returns.

> (uint8_t*)&value

This should be (uint8_t*)value.

If you feel a post has answered your question, please click "Accept as Solution".
delsey
Associate

Hi TDK,

Thank you for your swift response! I can confirm that has fixed the issue. However, I would like to take advantage of the DMA/IT functions, as I may end up with multiple sensors and want to avoid the application hanging around whilst waiting for data. There is some processing required on the data being read, and wanted to ask how to implement the "callback" functions:

  • HAL_I2C_MemTxCpltCallback()
  • HAL_I2C_MemRxCpltCallback()

It says to not override the functions in the HAL, but how would one go about defining this in my application to be called each time data is received?

I'm trying to transfer a project I've been working on into STM32, and its been a bit of a steep learning curve and I'm doing an embedded C course at the same time, so apologies for the newbie questions!

Kind regards

Dan