Skip to main content
Visitor II
February 21, 2020
Question

LSM303AGR SPI Read Problem

  • February 21, 2020
  • 3 replies
  • 1353 views

Hi

I have lsm303agr mems sensor with stm32g4. It uses 3 wire SPI mode. I have problem with SPI communication. I used CubeMX with HAL mode and STMems_Standard_C_drivers for lsm303. The problem is that, when i read whoami register it reads 0x33 which is correct. But the next time i read as 0. Next time again 0x33 and so on. It reads 0x33 and 0 sequentially. SPI times out. I have seen HAL examples written only for Full duplex communication. So, i used CubeMX with SPI in half duplex mode to generate the project. This is my code snippet:

while(1)

{

HAL_GPIO_WritePin(GPIO_Sensor, CS_Sensor_Pin, GPIO_PIN_RESET);

HAL_Delay(1);

HAL_SPI_Transmit(&hspi1, &whoami_addr, 1, 1000);

HAL_SPI_Receive(&hspi1, &whoami_val, 1, 1000);

HAL_Delay(1);

HAL_GPIO_WritePin(GPIOA_Sensor CS_Sensor_Pin, GPIO_PIN_SET);

}

Am i missing something with SPI 3-Wire mode?

Best regards.

    This topic has been closed for replies.

    3 replies

    ST Employee
    February 25, 2020

    Hi @AAYKA.1​ , can you check how you configured the SPI at master side (8bit or 16bit)? Did you perform multiple readings continuously? And after which event does the SPI go in timeout? Here below is the 3-wire SPI data pattern (p. 41 of the datasheet), please check if you followed all the indications. Appreciated if you can share the pattern analyzer data.

    0690X00000DXGIdQAP.png

    Regards

    AAYKA.1Author
    Visitor II
    February 25, 2020

    Hi Eleon

    Thanks for your answer. I have found STML476 discovery boards which has magnetometer with 3 wire. And i used that in my code. Then i succeeded in SPI write/read. Now we have another problem. I use LSM303. I read accelerometer data, temperature data correctly. They give me reasonable values. But magnetometer values are not so reliable. For example i get y-axis values always negative. So my angle values are strange. I used STMems_Standard_C_drivers which has LSM303 driver and i used its initilization procedure to program registers. What can be the cause of this problem? By the way, I get this values from raw data, i didnt include Motion library yet.

    Best regards.

    AAYKA.1Author
    Visitor II
    February 25, 2020

    Hi Eleon,

    We have tested our LSM303 with MC and EC Libraries that ST provides. We see that our degree calculations accuracy is within +/- 4 degree. That is, we have degree which has deviation of 4 degree when it is stationary. We see that we need some calibration to improve its perfomance. How can we do that? I see your UNICLUEO program which is mentioned in UM2192. Do we have to use that program with our custom board for calibration? We have no issue with power consumption. We just need to achieve maximum measurement performance. For this, do you recommend any specific register configuration?

    Best regards.

    ST Employee
    February 26, 2020

    Hi @AAYKA.1​ , I tried to support you at this link. Please refer to this thread. Regards