cancel
Showing results for 
Search instead for 
Did you mean: 

STM32G4 HAL SPI HandleTypedef

KKerr.2
Associate II

Hello ST MCU Community,

I'm a beginner to C and coding on a MCU and have run into what I hope is a simple problem. I have a small project where I'm trying to have a STM32G474 Nucleo board interface to a SPI display (http://www.lcdwiki.com/2.8inch_SPI_Module_ILI9341_SKU:MSP2807). At first I wanted to check my setup so I wrote a few functions that initialized the display based off the example psuedo-code provide. I then verified that I could change the color of the display to verify that I was actually writing to the display. I didn't have a problem but I had created all my functions in main.c.

Now I want to be able to add text to my display and have seen that there are multiple drivers located on Github. One driver that I would like to use can be found at: https://github.com/martnak/STM32-ILI9341

The code from Github looks to be dated from 2017 and I think there may have been some updates to CUBEMX since then. From what I can gather at that time, the SPI handler was not automatically initiated and the end user had to do it manually. The driver I found creates a spi_handletypedef in the driver l while my current implementation creates a spi_handetypedef in main.c.

Is there a way to use the hspi that was created in main.c in another file? The only way I could think of is to pass the hspi to each function in that file or to create the hspi at the driver file.

I'm hoping there may be a simpler solution before I edit the drivers from github to accept hspi from main.c.

Any guidance or help would be very much appreciated.

Thank you,

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

> Is there a way to use the hspi that was created in main.c in another file?

You can declare it as extern in a header and use it within the other source file.

in main.h:

extern SPI_HandleTypeDef hspi1;

in main.c:

SPI_HandleTypeDef hspi1;

in other_file.c:

#include "main.h"

...

HAL_SPI_Transmit(&hspi1, ...);

Actually, this is exactly what the library is doing

https://github.com/martnak/STM32-ILI9341/blob/master/Inc/spi.h#L48

https://github.com/martnak/STM32-ILI9341/blob/master/Src/spi.c#L44

https://github.com/martnak/STM32-ILI9341/blob/master/Src/ILI9341/ILI9341_STM32_Driver.c#L85

Initializing peripherals in their own files as opposed to in main.c is an option from within CubeMX. Nothing has changed there.

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

4 REPLIES 4
TDK
Guru

> Is there a way to use the hspi that was created in main.c in another file?

You can declare it as extern in a header and use it within the other source file.

in main.h:

extern SPI_HandleTypeDef hspi1;

in main.c:

SPI_HandleTypeDef hspi1;

in other_file.c:

#include "main.h"

...

HAL_SPI_Transmit(&hspi1, ...);

Actually, this is exactly what the library is doing

https://github.com/martnak/STM32-ILI9341/blob/master/Inc/spi.h#L48

https://github.com/martnak/STM32-ILI9341/blob/master/Src/spi.c#L44

https://github.com/martnak/STM32-ILI9341/blob/master/Src/ILI9341/ILI9341_STM32_Driver.c#L85

Initializing peripherals in their own files as opposed to in main.c is an option from within CubeMX. Nothing has changed there.

If you feel a post has answered your question, please click "Accept as Solution".

HI TDK,

Thank you for the nice explanation! This was the missing piece I needed. I added extern SPI_HandleTypeDef hspi1; to main.h as you suggested and was able to integrate the drivers from Github.

Thanks for the clarification on CubeMX. I've mainly used CubeIDE, do you know if there is an option to initialize peripherals in their own files in CubeIDE as well?

CubeMX is integrated within CubeIDE. Same functionality.
The option is in Project Manager -> Code Generator -> Generated files -> Generate peripheral initialization as a pair...
If you feel a post has answered your question, please click "Accept as Solution".
OKada.1
Associate

Hi How to write ads1274 driver ?