cancel
Showing results for 
Search instead for 
Did you mean: 

How to read E-compass on STM32F3DISCOVERY board?

RLee.2
Associate II

Hi,

I'm evaluating on the STM32F3DISCOVERY board using STM32CubeIDE but I'm having trouble interfacing with the onboard E-compass. In the Device Configuration Tool, I've enabled I2C1, since the user manual states that the MCU controls the E-compass via I2C. However, when I try to read data from the E-compass using HAL_I2C_Master_Receive(), it always returns HAL_ERROR. Furthermore, when I call HAL_I2C_IsDeviceReady(), the E-compass never returns that it is ready? Below is my relevant code:

void I2CTask(void* argument) {
	uint16_t CompassAddress = hi2c1.Init.OwnAddress1;
	uint8_t CompassData[255];
	uint16_t CompassSize = sizeof(CompassData);
	uint32_t CompassTimeout = 200;
	HAL_StatusTypeDef CompassStatus;
	init_printf(NULL, putc);
//	Wait for compass to be ready
	while(HAL_I2C_IsDeviceReady(&hi2c1, CompassAddress, 64, HAL_MAX_DELAY)!= HAL_OK);
	for (;;) {
//		TODO: Debug compass read error
		CompassStatus = HAL_I2C_Master_Receive(&hi2c1, CompassAddress, &CompassData[0], CompassSize, CompassTimeout);
		if (CompassStatus)
		{
			printf("HAL_I2C_Master_Receive failed with error %d\n", CompassStatus);
		}
		else
		{
			for (int i = 0; i < CompassSize; i++)
			{
				printf("%d\n", CompassData[i]);
			}
		}
		HAL_Delay(200);
	}
}

Thanks!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ozone
Lead

The demo application which comes (came) with the F3 Discovery as source should cover your issue.

I re-used the magnetometer code for an own project.

You can still download it as STM32F3-Discovery_FW_Vx.x on ST's website.

My last version is V1.1.0.

I'm not into Cube at all.

View solution in original post

3 REPLIES 3
Ozone
Lead

The demo application which comes (came) with the F3 Discovery as source should cover your issue.

I re-used the magnetometer code for an own project.

You can still download it as STM32F3-Discovery_FW_Vx.x on ST's website.

My last version is V1.1.0.

I'm not into Cube at all.

Another option is to try to bitbang the I2C.

JW

Thanks Ozone, this worked for me!