cancel
Showing results for 
Search instead for 
Did you mean: 

How do I use a uart instance declared in main.c from another custom .c file I created for my project?

Svan .1
Associate

I am trying to learn the in and outs of the STM32 Nucleo. I made my own .h and .c files to control an LCD screen atteched to my nucleo. However in these files I am trying to use HAL_UART_Transmit to write to the screen. I included the HAL.h file but the uart1 instance is declared in the main.c file. Is there a way other than passing the instance to every function I'm calling to use uart1 instance?

An example of what I am trying to do is included in a code snippet below.

void Clear_Screen()
{
	HAL_UART_Transmit(&huart1, clear_screen, sizeof(clear_screen), Timeout);
}

I get the error: "'huart1' undeclared (first use in this function)".

Thank you

1 ACCEPTED SOLUTION

Accepted Solutions
Guenael Cadier
ST Employee

Hello @Svan .1​ 

The parameter (type : pointer UART Instance handle) that is provided as input parameter of the HAL_UART_Transmit() function call, is describing the UART instance that is used for performing the transfer. This refers to an UART Handle structure, that should be initialized before. So you should ahve somewhere in your code another call to :

  HAL_UART_Init(&huart1);

So, huart1 variable should be visible from all files using HAL_UART_*** services.

For example, if the huart1 variable is declared inside your main.c, it could be made visible by adding :

extern UART_HandleTypeDef UartHandle;

in files requiring it.

Hope this helps

Guenael

View solution in original post

3 REPLIES 3
Ozone
Lead II

> I included the HAL.h file but the uart1 instance is declared in the main.c file. Is there a way other than passing the instance to every function I'm calling to use uart1 instance?

Yes, as it is usually done in C. You could declare the variable elsewhere as "extern".

However, defining it in one module and passing it as parameter usually (!) results in a cleaner interface, and a cleaner design.

Declaring a variable everywhere, and accessing it directly everywhere is called "side effect coding", and almost impossible to comprehend, debug, and maintain properly.

As it was with the first BASIC interpreters.

Guenael Cadier
ST Employee

Hello @Svan .1​ 

The parameter (type : pointer UART Instance handle) that is provided as input parameter of the HAL_UART_Transmit() function call, is describing the UART instance that is used for performing the transfer. This refers to an UART Handle structure, that should be initialized before. So you should ahve somewhere in your code another call to :

  HAL_UART_Init(&huart1);

So, huart1 variable should be visible from all files using HAL_UART_*** services.

For example, if the huart1 variable is declared inside your main.c, it could be made visible by adding :

extern UART_HandleTypeDef UartHandle;

in files requiring it.

Hope this helps

Guenael

Thank you Guenael, I did not know I could extern the UART_HandleTypeDef. Your answer solved my problem.