2020-03-05 08:06 PM
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!
Solved! Go to Solution.
2020-03-05 10:14 PM
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.
2020-03-05 10:14 PM
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.
2020-03-06 12:48 AM
Another option is to try to bitbang the I2C.
JW
2020-03-16 08:49 PM
Thanks Ozone, this worked for me!