Skip to main content
RLee.2
Associate
March 6, 2020
Solved

How to read E-compass on STM32F3DISCOVERY board?

  • March 6, 2020
  • 3 replies
  • 1725 views

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!

 

This topic has been closed for replies.
Best answer by Ozone

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.

3 replies

Ozone
OzoneBest answer
Principal
March 6, 2020

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.

RLee.2
RLee.2Author
Associate
March 17, 2020

Thanks Ozone, this worked for me!

waclawek.jan
Super User
March 6, 2020

Another option is to try to bitbang the I2C.

JW