cancel
Showing results for 
Search instead for 
Did you mean: 

Why do my I2C HAL commands to the LSM303AH not work?

JBonn
Associate III

When power is applied to the STM32L476, 3.3 v is also applied to the LSM303AH pins

2             CS

9             Vdd

10           Vdd IO

These commands work:

MX_I2C3_Init();                      Goes to code created by CubeMX

HAL_I2C_GetState(&hi2c3);  it is ready

HAL_I2C_Init(&hi2c3);

I get timeout error (32) with the following commands. What did I do wrong?

HAL_I2C_Mem_Reads          All time out

HAL_I2C_Mem_Writes         All time out

HAL_I2C_Master_Receives     All time out

HAL_I2C_Master_Transmits    All time out

Examples:

HAL_I2C_Mem_Read(&hi2c3,0x1D, CTRL_REG1_A, 1, &Data_In, 1, 100)

HAL_I2C_Mem_Write(&hi2c3, 0x1D, CTRL_REG2_A, 1, &Data_Out, 1, 100);

HAL_I2C_Master_Transmit(&hi2c3, CTRL_REG2_A, &Data_Out, 1, 100);

HAL_I2C_Master_Receive(&hi2c3, WHO_AM_I_A, &WAI_A, 1, 100);

This is the I2C3 code created by CubeMX: 

hi2c3.Instance = I2C3;

hi2c3.Init.Timing = 0x10909CEC;

hi2c3.Init.OwnAddress1 = 58;

hi2c3.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;

hi2c3.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;

hi2c3.Init.OwnAddress2 = 0;

hi2c3.Init.OwnAddress2Masks = I2C_OA2_NOMASK;

hi2c3.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;

hi2c3.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;

if (HAL_I2C_Init(&hi2c3) != HAL_OK)

Here is the CubeMX settings:

Timing

               Custom timing                                       Enabled

               I2C Speed Mode                     Standard Mode

               I2C Speed Frequency KHz      100

               Analog Filter                                          Enabled

               Timing                                                    0x10909CDC                           Determined by CubeMX

Slave Features

Clock No Stretch Mode                                          Disabled

General Call Address Detection                             disabled

Primary Address Length Selection        7 bit

Dual Address Acknowledged                 Disabled

Primary slave Address                                           0x1D

Clock Configuration:                                                              no external chip used

LSI RC     32           HCLK MHZ 80 

HIS RC    16           

What is wrong with this setup and what is the first HAL command that should be made?

1 ACCEPTED SOLUTION

Accepted Solutions
Eleon BORLINI
ST Employee

Hi @JBonn​ ,

you might refer to the STM32L4 MEMS interface examples in the firmware function pack FP-SNS-ALLMEMS1, and in particular to the project folder \Projects\STM32L476RG-SensorTile\Applications\ALLMEMS11, which contains example of MEMS devices on I2C. You can also have a look to the lsm303ah_STdC C examples on Github.

-Eleon

View solution in original post

1 REPLY 1
Eleon BORLINI
ST Employee

Hi @JBonn​ ,

you might refer to the STM32L4 MEMS interface examples in the firmware function pack FP-SNS-ALLMEMS1, and in particular to the project folder \Projects\STM32L476RG-SensorTile\Applications\ALLMEMS11, which contains example of MEMS devices on I2C. You can also have a look to the lsm303ah_STdC C examples on Github.

-Eleon