2020-03-09 02:53 AM
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
Solved! Go to Solution.
2020-03-09 03:11 AM
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
2020-03-09 03:03 AM
> 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.
2020-03-09 03:11 AM
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
2020-03-09 03:22 AM
Thank you Guenael, I did not know I could extern the UART_HandleTypeDef. Your answer solved my problem.