cancel
Showing results for 
Search instead for 
Did you mean: 

uart HAL api's

SANA SRIKAR
Associate II
Posted on December 15, 2016 at 13:42

 i am new to stm32f4 HAL api's. I have some trouble learning them. Anything useful for me is really really appreciated. 

i  was looking at the uart api. i was referring to the examples provided in the keilv5  directory. 

there in the uart using interrupt coding (FILE NAME: UART_TwoBoards_ComIT )  

path in your pc's:C:\Keil_v5\ARM\Pack\Keil\STM32F4xx_DFP\2.6.0\Projects\STM32F4-Discovery\Examples\UART

here are my doubts     __IO ITStatus UartReady = RESET; 

    __IO ITStatus UartReady = RESET;   in this what is __IO and what is ITStatus in which header file can i find about it and how to know the different types present in it  ?

2. what is the difference between HAL_UART_Receive_IT and HAL_UART_Receive (other than blocking and unblocking ) 

and in that program  they are sending data and receiving it using UART interrupts but i dont find any IRQHandler in it .

but in the other c file which is linked to it called stm32xx_it.c 

the function is given as like this

void USARTx_IRQHandler(void)

{

HAL_UART_IRQHandler(& UartHandle);

}

what does HAL_UART_IRQHandler() do ? where is it defined ?

And if they are sending and receiving data using interrupts whyarent 

HAL_UART_Receive_IT() function and HAL_UART_Transmit_IT() arent mentioned inside the IRQ handler?

I would really love to have the entire code explanation if possible (if anyone with some time to spend and more patience) and I really thank for  such kind action  ......

and sorry if my doubts are very stupid.... pls provide me some tutorial links and videos from st site and i forgot to mention i am using stm32f4VG......... and stm32f4 discovery board......

#stm32f4 #uart #hal
18 REPLIES 18
SANA SRIKAR
Associate II
Posted on January 01, 2017 at 17:33

i have used the way which clive suggested and yet i havent found a way i am still trying to figure it out and one more thing i was making a code with STM32CUBEMX and there in the stm......msp.c file there is some thing like this......

void HAL_MspInit(void){ /* USER CODE BEGIN MspInit 0 */ /* USER CODE END MspInit 0 */ HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_0); /* System interrupt init*/ /* MemoryManagement_IRQn interrupt configuration */ HAL_NVIC_SetPriority(MemoryManagement_IRQn, 0, 0); /* BusFault_IRQn interrupt configuration */ HAL_NVIC_SetPriority(BusFault_IRQn, 0, 0); /* UsageFault_IRQn interrupt configuration */ HAL_NVIC_SetPriority(UsageFault_IRQn, 0, 0); /* SVCall_IRQn interrupt configuration */ HAL_NVIC_SetPriority(SVCall_IRQn, 0, 0); /* DebugMonitor_IRQn interrupt configuration */ HAL_NVIC_SetPriority(DebugMonitor_IRQn, 0, 0); /* PendSV_IRQn interrupt configuration */ HAL_NVIC_SetPriority(PendSV_IRQn, 0, 0); /* SysTick_IRQn interrupt configuration */ HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0); /* USER CODE BEGIN MspInit 1 */ /* USER CODE END MspInit 1 */}�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

what is this everything is having the same priority and same sub priority what happens if we give like this ? (actually the code is working just fine ................. can it be like this ?)

and wish you ahappy new year

toby2
Senior
Posted on December 15, 2016 at 16:05

I have no experience of using Keil but in general......

in this what is __IO

If I remember correctly it is just the equivalent of volatile. So you variable is treated as volatile.

and what is ITStatus in which header file can i find about it

It is in stm32f4xx.h (or the equivalent for your processor) and is just an enum defining SET and RESET. 

how to know the different types present in it  ?

This is not as easy as it could be! I have found the HAL libraries slow to learn (so don't worry that you are struggling to start with). The easiest way to to to use example projects so can see how they are structured and just search for stuff like this. You will eventually get the idea where to find stuff.

2. what is the difference between HAL_UART_Receive_IT and HAL_UART_Receive (other than blocking and unblocking )

The first uses interrupts and, as you say, is not blocking. The 2nd just uses polling and is blocking. I find the best way to understand functions like this is to just look at the implementation of the functions. If you are lucky then they have sensible comments to help you (the uart stuff is reasonably well documented, to a point)

and in that program  they are sending data and receiving it using UART interrupts but i dont find any IRQHandler in it .

but in the other c file which is linked to it called stm32xx_it.c 

It is fairly standard to include things like stm32xx_it.c in the project. That is the user implementation of the interrupt handlers so can override anything in the libraries.

the function is given as like this

void USARTx_IRQHandler(void)

{

HAL_UART_IRQHandler(& UartHandle);

}

what does HAL_UART_IRQHandler() do ? where is it defined ?

It does what the name implies, ie. handles the UART interrupts for the HAL libraries. Hence it being called from the USARTx_IRQHandler().  It is in the stm32fxx_hal_uart.c module.

And if they are sending and receiving data using interrupts whyarent 

HAL_UART_Receive_IT() function and HAL_UART_Transmit_IT() arent mentioned inside the IRQ handler?

The receive_it() etc are the user functions to receive or transmit data under interrupts. So to transmit some data you call the transmit_IT() function with the data in a buffer. The HAL libraries do the rest. These functions should not be confused with the IRQ functions which actually handle the interrupts.

I would really love to have the entire code explanation if possible (if anyone with some time to spend and more patience) and I really thank for  such kind action  ......

I don't have time to explain in more depth I am afraid so I would suggest going through the example project in detail. Any function you don't understand then just search for it and read the code to understand it. Eventually all will be clearer!

Unfortunately the HAL libraries are sometimes difficult to understand without experience. They are designed to be flexible and give an standard interface across many devices but in doing so they become complex and difficult to follow.

However, as a general pointer it is worth knowing that t

he hal libraries make a lot of use of weak functions and call backs. So, for example, the 

USARTx_IRQHandler() is declared as a weak function that (I think) just does nothing. It is then up to the user to code (and hence over-ride) this function. This is done in your example code in the stm32f4xx_it.c module. In your examples case the programmer knew he wanted to use the standard HAL library to handle the uart interrupts so he just called the 

HAL_UART_IRQHandler(& UartHandle);. You could write your own interrupt handler here if you want to do it your own way (which I generally do for uart stuff as I don't like the hal way of doing things).

You will also probably find a stim32f4xx_hal_msp.c module in your example project. This is where you will find the user code for call backs for things like initialisation. So, for example, if your main() calls HAL_Init() this will do the basic stuff but then calls HAL_MspInit(). HAL_MspInit() is likely in your _hal_msp.c and will contain your specific initialisation code. This way the user can add to the basic HAL stuff without having to rewrite all the HAL functions just because they don't do quite what is wanted.

Sorry I cannot go into more detail but hopefully this helps a bit. (and I am not an expert, I just learnt all this myself over the last few months)

Walid FTITI_O
Senior II
Posted on December 15, 2016 at 17:34

Hi

SRIKAR.SANA

,

First, welcome to our STM32 Cummunity

The library that your using is Hal library called STM32Cube. Since the device is from the STM32F4xx series, you should refer to the STM32CubeF4 library (ou can download it from this ST website

http://www.st.com/en/embedded-software/stm32cubef4.html

)

To understand more about architecture of the library and how project files ( user files /drivers/startup file/ hw files) are classified and called from the project workspace, I recommend that you refer to the following:

If response is correct, press the correct botton

Posted on December 15, 2016 at 19:51

If you use Keil, and check the build with browser information check box, then you should be able to right-click functions and variables, etc and 'go to definition'. Also you can use 'Find in Files' to search the source files to find things.

Learn to navigate the source files in the editor, or use a File Manager or search tools to review the source provided by CubeMX and HAL. Consider using a static source code analysis tool to review and understand the software you are using.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
SANA SRIKAR
Associate II
Posted on December 16, 2016 at 01:23

Mack.Toby

FTITI.Walid

Turvey.Clive.002

thanks for your replies and one more doubt that is still present i wanted to ask it yesterday itself but i forgot to ask.

i just did what clive said to the function HAL_UART_IRQHandler()(rigt click >>advance>> got to definition) but the thing is it is pretty messed up....... as iam not being able to understand what has been written there. and HOW IS THIS HAL_UART_IRQHandler() able to execute the things HAL_Recieve_IT() which is not present in IRQHandler.................i am not even getting how it is working during interrupts?

(or)

in simple how is this code working

without beign present inside the IRQHandler ? i guess its benficial for me if some one explain atleast first few lines of the code how will IRQ_Handler come how does it call these things or will these things execute continously?

first of all what happens when some interrupt is arrived?

this is confusing me sir ..................................................................................................

THIS CODE IS PRESENT IN main.c IN

UART_TwoBoards_ComIT

it is just a piece of the main code

it should just work normally like when ever the code reaches the point 'LINE1' if no interrupt arises then it will execute Error_Handler() where infinite while loop is present.....thats it it got stuck but this is not the way the code is working so pls tell me and last

as toby said if we i wish to write my own irq handler can i use HAL_UART_Recive() in the interrupt handler?

/* The board sends the message and expects to receive it back */

/*##-2- Start the transmission process #####################################*/

/* While the UART in reception process, user can transmit data through

'aTxBuffer' buffer */

if(HAL_UART_Transmit_IT(&UartHandle, (uint8_t*)aTxBuffer, TXBUFFERSIZE)!= HAL_OK) //------------------->LINE1

{

Error_Handler();

}

/*##-3- Wait for the end of the transfer ###################################*/

while (UartReady != SET)

{

}

/* Reset transmission flag */

UartReady = RESET;

/*##-4- Put UART peripheral in reception process ###########################*/

if(HAL_UART_Receive_IT(&UartHandle, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK)

{

Error_Handler();

}

#else

/* The board receives the message and sends it back */

/*##-2- Put UART peripheral in reception process ###########################*/

if(HAL_UART_Receive_IT(&UartHandle, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK)

{

Error_Handler();

}

/*##-3- Wait for the end of the transfer ###################################*/

while (UartReady != SET)

{

}

/* Reset transmission flag */

UartReady = RESET;

/*##-4- Start the transmission process #####################################*/

/* While the UART in reception process, user can transmit data through

'aTxBuffer' buffer */

if(HAL_UART_Transmit_IT(&UartHandle, (uint8_t*)aTxBuffer, TXBUFFERSIZE)!= HAL_OK)

{

Error_Handler();

}

#endif /* TRANSMITTER_BOARD */

/*##-5- Wait for the end of the transfer ###################################*/

while (UartReady != SET)

{

}

/* Reset transmission flag */

UartReady = RESET;

thank you very much each and every answer is helpfull for me

Posted on December 16, 2016 at 00:58

thank you very much you just gave some information which I  wanted from this forum

Posted on December 16, 2016 at 00:59

pls look at the bottom reply or post sir once my last doubt

Posted on December 16, 2016 at 09:58

I think there is some confusion there. 

So when data is received by the UART it will call the vector in the vector table.(which is probably in startup_stm32f4???.s or similar). In your code it looks like it points to USARTx_IRQHandler(). This handler is in your _it.c module and all it does is call HAL_UART_IRQHandler(& UartHandle).

HAL_UART_IRQHandler() checks the uart to see why the interrupt occurred and if it was a receive it calls USART_Receive_IT(). This reads the data and puts it in the buffer given by UartHandle and, if has received the data wanted it then calls  HAL_USART_RxCpltCallback(husart);

So, if you are using HAL you don't normally change the irq handler, it just calls

HAL_UART_IRQHandler which does all the work for you. All you need to do is write the HAL_USART_RxCpltCallback function to do something with the data received. Looking at your example code I suspect it already has this function and it probably just sets 

UartReady although it might do something with the received data.

I think you just to need spend time doing what Clive suggested and reading the code, also read the reference manual for the micro you are using and make sure you understand exactly what the HAL code is doing. ie HAL_USART_Init, HAL_USART_Receive_IT, HAL_USART_IRQHandler and all the functions they call. These functions are over-complicated for basic stuff but should be quite readable if you understand how the USART works.

Also read section 65 of the hal description documenent Walid linked to.

Alternatively don't use HAL. It is way over complicated for a lot of uses as it is trying (and in my view failing) to be generic and cover all use cases and all error conditions. The HAL usart interrupt is over 100 lines of code. The one I often use is about 20 lines of code and handles circular buffers and allows the easy use of unknown packet lengths.......

Posted on December 22, 2016 at 16:54

Would love to see an alternate implementation of the HAL UART code!