Skip to main content
SANA SRIKAR
Associate
Dec 15, 2016 12:42 PM UTC
Question

uart HAL api's

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
This topic has been closed for replies.

18 replies

SANA SRIKAR
Associate
Jan 1, 2016 4:33 PM UTC
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
Jan 3, 2017 11:49 AM UTC
Posted on January 03, 2017 at 11:49

If you cannot find what sets UartReady then I suggest you stop looking at anything else until you solve this 'problem'.

Assuming you are looking at the standard STM UART_TwoBoards_comIT code then it is clear, particularly if you read and understand my post on the 16th Dec. 

If you need help searching using Keil then.......

http://lmgtfy.com/?q=keil+IDE+%20search

toby2
Senior
Dec 15, 2016 3:05 PM UTC
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)

SANA SRIKAR
Associate
Dec 16, 2016 12:58 AM UTC
Posted on December 16, 2016 at 00:58

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

Walid FTITI_O
Visitor II
Dec 15, 2016 4:34 PM UTC
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

Tesla DeLorean
Guru
Dec 15, 2016 6:51 PM UTC
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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
SANA SRIKAR
Associate
Dec 16, 2016 12:23 AM UTC
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

toby2
Senior
Dec 16, 2016 9:58 AM UTC
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.......

Geoff Espin
Visitor II
Dec 22, 2016 4:54 PM UTC
Posted on December 22, 2016 at 16:54

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

SANA SRIKAR
Associate
Dec 24, 2016 2:33 PM UTC
Posted on December 24, 2016 at 15:33

Mack.Toby

what happens if we put timeout =0 in uart receivesfunction?

toby2
Senior
Dec 24, 2016 5:18 PM UTC
Posted on December 24, 2016 at 17:18

You mean using HAL_USART_Receive() rather than

HAL_USART_

Receive_it()?

I've not tried it but I assume HAL_USART_Receive() would then always return HAL_TIMEOUT (with no data) as you would expect.

Mind you, having looked at code for HAL_USART_Receive() again I am confused. It would appear that for every byte it receives on the USART it sends an 0xFF byte on the TX first eg.....

/* Wait until TXE flag is set to send dummy byte in order to generate the clock for the slave to send data */

if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)

{

return HAL_TIMEOUT;

}

/* Send Dummy Byte in order to generate clock */

husart->Instance->DR = (DUMMY_DATA & (uint16_t)0x00FFU);

To be honest I cannot remember the last time I used a STM32 usart syncronously so it might be necessary for that but if using asyncronously that looks like a great way of messing up the tx data stream.

Surely this cannot be right? Am I missing something?

SANA SRIKAR
Associate
Dec 25, 2016 5:15 AM UTC
Posted on December 25, 2016 at 06:15

sir back to the post where you told that i was in a confusion. yeah that is for sure i was in confusion and one more help to solve it. i have been learning those API's whenever i used to have free time but i got stuck here

before telling my doubt ...

disclaimer : Everything i am talking here refers to the code which is present in the Examples folder of the Keil

correct path for the code is:C:\Keil_v5\ARM\Pack\Keil\STM32F4xx_DFP\2.6.0\Projects\STM32F4-Discovery\Examples\UART\UART_TwoBoards_ComIT

SIR my only doubt in this code now is

if(HAL_UART_Transmit_IT(&UartHandle, (uint8_t*)aTxBuffer, TXBUFFERSIZE)!= HAL_OK)\\\{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();}�?�?�?�?�?�?�?�?�?�?�?�?�?

what is that i dontunderstand is how is UartReady is getting updated everytime.you told me that some function is updating it but what is that function ? and in which file is it getting updated ? i am not being able to see it.

the only thing i know so far is it is getting its value from the UART_HandleTypeDef structure where there is one member called state ::gstate. but where is it happening pls show me the way sir

thanks for the immense help.......................

and i have read the post regarding your circular buffer my question first of all why do we need a circular buffer.? i am a novice in this so pls excuse me if i am asking silly questions

thank you sir

toby2
Senior
Dec 26, 2016 12:45 PM UTC
Posted on December 26, 2016 at 12:45

I cannot answer those questions as I do not have the code but I made a guess in my post above. You really need to do as Clive suggested and learn to use the search function, without that you really are not going to learn much. 

Don't worry about circular buffers yet. They are just an easy way of buffering small but unknown packets of data. You can learn about them using a Google search.

Alexey Ostrejkovsky
Associate II
Jan 3, 2017 11:58 AM UTC
Posted on January 03, 2017 at 12:58

Pleas, see video 

https://youtu.be/IdKx4oWNJfw

 
SANA SRIKAR
Associate
Jan 4, 2017 12:36 AM UTC
Posted on January 04, 2017 at 01:36

Yes I found it  out. Sorry for troubling you guys with silly thing it is being done at Txcptlcallback and Rxcptlcallback   function in the program in main.c file...............

Now pls answer the above question regarding interrupts . All are given same  priority and sub priority what is the side effect of that?

toby2
Senior
Jan 4, 2017 8:02 AM UTC
Posted on January 04, 2017 at 08:02

Have a read of the sections on the exception model and the NVIC in the programming manual for your processor. Also this document gives some useful info on the NVIC:

http://www.eng.auburn.edu/~nelson/courses/elec5260_6260/slides/ARM%20STM32F407%20Interrupts.pdf

The HAL_MspInit() you show is not really doing anything as it is only setting the priorities to their default state. The idea being that you can change the priorities if you think it is necessary by modifying this routine.

You only need to do this if you need certain interrupts to be higher priority than others. For example, if you are using a timer for some critical timing but just using the USART for debug data you might make the timer interrupt higher priority than the USART. That way the timer IRQ will always be serviced even if it already has an active USART interrupt.

Alternatively, if the timer interrupt is writing data to the USART you might need the USART to be higher priority than the timer so it can always clear the buffers.

Unless you have a good reason just leave the interrupt priorities as they are.