cancel
Showing results for 
Search instead for 
Did you mean: 

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

Ronny Landsverk
Associate III
Posted on September 01, 2017 at 22:58

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. 

#l3gd20h #stm32f3-discovery #spi
5 REPLIES 5
Posted on September 02, 2017 at 02:33

Hello!!

Inside your F3 firmware there is an example using the L3GD20H for 303 discovery board

0690X00000607qYQAQ.png

It uses HAL and its a good starting point to make your project

Regards

vf

Posted on September 02, 2017 at 10:43

Thank you for responding. 

Perhaps I am to novice to learn from this example. It seems a bit complex.
Posted on September 02, 2017 at 22:27

>>> Perhaps I am to novice to learn from this example. It seems a bit complex.

It seems complex but nothing else.

You must not be disappointed from complexity.

As you mention you tried to make a project to read data from gyro.

Try to split your project into smaller discrete pieces.

I give you an example.

1. General initialization and SPI Initialization. As you mention you can use the CubeMX  Use the same settings as in example.

2. Interface module . Means the functions you need to call to communicate with L3GD20H . At least you need to make two functions with names like ReadRegister(uint8_t RegisterName, uint8_t* RegData, uint8_t RegDataSize) and WriteRegister(uint8_t RegisterName, uint8_t* RegData, uint8_t RegDataSize)  with same functionality as

 GYRO_IO_Read and GYRO_IO_Write from example.

You can copy of course the above functions from example without make yours (for further name compatibility)

3. Device specific module.  Must contain at least a Device initialization function  a read identity function, and a read X,Y,Z values function.  Study the  module l3gd20.c  from example to make yours, tailored to your needs.

4. And for the end , a human interface module.  This may be a watch window from your IDE to watch the gyro values from global variables,  a printf function retargeted to a debug window, or a series of LEDs like the example.

The program flow will be

1. General Initialization

2. SPI initialization (with CS pin)

3. Device initialization (device specific module)

4. Data xyz read

5. forward the xyz data to Human Interface.

All these are enough  to study the functionality of the gyro , to discover the capabilities, and to try to develop your own code.

The most tricky part from the above example, in my opinion, is the initialization part.

Clocks initialization , Stack, Heap, SPI port (polarities timings),  the CS pin ..

Best regards

vf

Posted on September 02, 2017 at 23:05

Thanks you so much for these pointers vf.

These will be helpful when diving deeper into STM32 convention/methods and C++ programming for larger projects. Due to your feedback, I will study this example carefully (tomorrow after a good night sleep).

Regards

Ronny

Ronny Landsverk
Associate III
Posted on September 04, 2017 at 21:56

I am shamed to admit, but the problem was that I had defined my chip enable, PE3 as a GPIO_Input rather than GPIO_Output ...