AnsweredAssumed Answered

Setup of gyro (L3GD20H) for SPI using the STM32F303 Discovery board

Question asked by Ronny Landsverk on Sep 1, 2017
Latest reply on Sep 4, 2017 by Ronny Landsverk

I am trying to read values from the gyro included on my Discovery board.

I am using HAL_SPI_Transmit() to prepare the sensor and HAL_SPI_Receive() to read data values.

The peripherals are specified using CubeMX.
The SPI1 peripheral is connected to the pins (serial clock, MOSI and MISO) of the sensor L3GD20H. 
The chip enable pin (PE3) is set/reset using HAL_GPIO_WritePin()

The code is based in an example from a youtube video https://www.youtube.com/watch?v=OBuuWzyPMPg where Mohamed reads values from a LIS302DL accelerometer on a STM32F4 Discovery board.
The STM32F303VC cannot read its accelerometer using SPI. Hence, I try to read the gyro.

The main code that is user defined is as follows

   /* Private variables -------------------------------------------------------*/

    uint8_t data_ctrl1, data_ctrl2, address_ctrl1, address_ctrl2;

  uint8_t xdata1, address_out1, xdata2, address_out2;

  /* USER CODE BEGIN 2 */

   HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, GPIO_PIN_RESET);

   address_ctrl1 = 0x20;

   HAL_SPI_Transmit(&hspi1, &address_ctrl1, 1, 50);

   data_ctrl1 = 0x0f;

   HAL_SPI_Transmit(&hspi1, &data_ctrl1, 1, 50);

   HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, GPIO_PIN_SET);

   /* USER CODE END 2 */

   while(1)

   {

      HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, GPIO_PIN_RESET);

      address_out1 = 0x28 | 0x80;      // MSB high: read mode

      HAL_SPI_Transmit(&hspi1, &address_out1, 1, 50);

      HAL_SPI_Receive(&hspi1, &xdata1, 1, 50);

      HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, GPIO_PIN_SET);

      HAL_Delay(50);

   }

The remaining code, that is the initialization code, is not shown since it is generated by CubeMX...

In debugging, the value xdata1 is 255 always and does not change when tilting the board.

 

Please note that I am still relatively new at embedded systems. 

Outcomes