cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 I2C HAL read out data from HMC5883L

Stefan Meyre
Associate III
Posted on April 06, 2015 at 11:56

Hi all,

this sounds so simple but I did spend too many hours trying to just read data out from my  HMC5883L 3-axis Magnetometer. I admitt I just switched from 8bit to the 32bit world, so bare with me... The ''new'' HAL drivers lack a bit of description and exsamples on the web so I ask you for help. Here the code; Cube MX generated on a 32F303 discovery board.

/* I2C2 init function */

void MX_I2C2_Init(void)

{

  hi2c2.Instance = I2C2;

  hi2c2.Init.Timing = 0x00201D2D;

  hi2c2.Init.OwnAddress1 = 0;

  hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;

  hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;

  hi2c2.Init.OwnAddress2 = 0;

  hi2c2.Init.OwnAddress2Masks = I2C_OA2_NOMASK;

  hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;

  hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;

  HAL_I2C_Init(&hi2c2);

    /**Configure Analogue filter

    */

  HAL_I2CEx_AnalogFilter_Config(&hi2c2, I2C_ANALOGFILTER_DISABLED);

}

&sharpdefine HMC5883L_MASTER_ADDR   0x13

HAL_I2C_Mem_Read(&hi2c2,(uint16_t)HMC5883L_MASTER_ADDR,(uint16_t)X_MSB_Reg,I2C_MEMADD_SIZE_8BIT,(uint8_t*)readHMC5883LBuffer,6,100);

   X_axis = make_word(readHMC5883LBuffer[0],readHMC5883LBuffer[1]);

  Z_axis = make_word(readHMC5883LBuffer[2],readHMC5883LBuffer[3]);

  Y_axis = make_word(readHMC5883LBuffer[4],readHMC5883LBuffer[5]);

The SCL pin goes low a few cycles and then time out gets called...

Thanks for some suggestions!

#stm32-hal-i2c
2 REPLIES 2
noobee
Associate II
Posted on April 06, 2015 at 14:56

heh. i was also playing around with stm32cube HAL over the weekend, with the mpu6050 instead.

a couple of notes: - i think the common address for the 5883 is 0x1E? you mentioned 0x13 in your code. - in any case, you might need to shift the device address left by 1 when calling the HAL_I2C_* routines (see below). - you can call

HAL_I2C_IsDeviceReady()

to test if an i2c device is responding on the other side of the bus (just as a basic connectivity test)

while (HAL_I2C_IsDeviceReady(&hi2c2, (uint16_t) MPU6050_ADDRESS, 10, 100) != HAL_OK) {
...

where the MPU6050 I2C address is 0x68

#define MPU6050_ADDRESS (0x68 << 1)

- probably minor, i also initialized two fields in the setup

hi2c2.Init.ClockSpeed = 400000;
hi2c2.Init.DutyCycle = I2C_DUTYCYCLE_2;

Stefan Meyre
Associate III
Posted on April 07, 2015 at 10:23

thx noobee for your feedback! Yes silly me, 0x1E is the device address.

The Init.ClockSpeed and Init.DutyCycle is not member of my I2C_InitTypeDef. I guess my Init.Timing takes care of that.

Anyway I do not get any data on SDA line. The I2C bursts out bundles of 100kHz (normal mode) puls-packages but no SDA Responses so far...

0690X00000605GIQAY.png

okokok in the mean time I did change from hi2c2 to hi2c1 (on different port pins) and thats what I get...

0690X00000605FpQAI.png

looks good so far. It seems I do have a conflict on the discovery board with my PF0 pin. Thx for yr help. Stefan