Skip to main content
Graduate
March 5, 2022
Question

LSM6DSM Help?

  • March 5, 2022
  • 1 reply
  • 677 views

Hello....

I am trying to communicate with the above subject but I am not sure what CPOL and CPHA should be set to. STM uses MODE1 - 4 but SPI talks about MODE 0 - 1. I have the following basic code to just try and read the WHO_AM_I Register (0x15) which should return 0x6A. I am using an ST sensortile which is too small to get a logic analyzer on it to see what is going on. My code is pretty basic....I have tried all the variants of CPOL and CPHA and only get 0 or 255 back. Can someone please tell me what I am doing wrong? (using an STM32L476JG)

 HAL_GPIO_WritePin(SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_SET);
 /* USER CODE END 2 */
 
 /* Infinite loop */
 /* USER CODE BEGIN WHILE */
 while (1)
 {
	 HAL_GPIO_WritePin(SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_RESET);
	 HAL_SPI_Transmit(&hspi2, (uint8_t *)&WHO_AM_I, 1, 100);
	 HAL_SPI_Receive(&hspi2, (uint8_t *)&spiValue, 1, 100);
	 HAL_GPIO_WritePin(SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_SET);
 
	 HAL_Delay(1000);

    This topic has been closed for replies.

    1 reply

    ST Employee
    March 7, 2022

    Hi @SWenn.1​ ,

    As shown in the datasheet p, the working SPI Mode is Mode 3, i.e. with CPOL=1, CPHA=1:

    0693W00000Kbg28QAB.png 

    By the way, can we refer to this more recent question for this topic's discussion?

    -Eleon