cancel
Showing results for 
Search instead for 
Did you mean: 

STM32CubeF4, SPI and MEMS - unable to communicate

giuseppe239955_st
Associate II
Posted on March 25, 2015 at 12:08

I am experimenting STM32 by using a STM32F429I discovery board, with a MEMS on board.

At the moment, any attempt to read the MEMS via SPI by using the Cube libraries has failed.

On the other hand, another example project (not Cube based) is working, so the MEMS itself is surely not defective.

The initialisation code should be OK because it was generated with CubeMX.

Please find here the code I am using, can anyone point me to the error?

Also note that I am executing the code with a debugger, so I can actually read the results of the

Apologies if my question is stupid, I've started with STM32 only since a few days...

    command[0] = 0x20;        // program control register 1

    command[1] = 0x0f;        // turn on device

    

    if (HAL_SPI_Transmit(&hspi5,command,2,100) != HAL_OK)

        (... warns the user of a problem and stop)

    command[0] = 0x8f;        // read whoami

    if (HAL_SPI_Transmit(&hspi5,command,1,100) == HAL_OK)

    {

        // result should be 0b11010100

        HAL_SPI_Receive(&hspi5,memsBuffer,1,100);

        whoami = memsBuffer[0];

  }

#mems #mems #spi #spi #stm32f429i-discovery #stm32cubef4
1 REPLY 1
alexalex91
Associate II
Posted on March 30, 2015 at 15:16

>The initialisation code should be OK because it was generated with CubeMX.

I assume that you call HAL_SPI_Init(&hspi5) before using SPI (because this is not generated by CubeMX) and that you have selected the correct SPI mode for your sensor:

MODE 0:

hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;

hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;

MODE 1:

hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;

hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;

MODE 2:

hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;

hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;

MODE 3:

hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;

hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;

The rest of the code is straightforward.

Please note that under /STM32Cube_FW_F4_V1.3.0/Drivers/BSP/STM32F429I-Discovery/ you find MEMS code for your board. There is also a demo available under /STM32Cube_FW_F4_V1.3.0/Drivers/BSP/STM32F429I-Discovery/

Hope this helps

PS: I noticed your italian name, I'm too despite my nick.