HAL: #4 How to - UART Interrupt

  Рет қаралды 85,637

Web learning

6 жыл бұрын

In this video, I will show how to use the HAL UART with Interrupt functions.
Before you watch this, please see the video on how to use the STM32CubeMX if you dont already know.
kzfaq.info/get/bejne/n9OIlNh0zt2plqc.html
UM1724 - STM32 Nucleo-64 board: www.st.com/content/ccc/resource/technical/document/user_manual/98/2e/fa/4b/e0/82/43/b7/DM00105823.pdf/files/DM00105823.pdf/jcr:content/translations/en.DM00105823.pdf
*** 2 Free Board giveaway is Closed****
2 Free Board giveaway is open. Make sure you subscribe and have
the notification button on so you will know when you will get a chance to win also a board next time.
See this link for rules and restrictions: kzfaq.info/get/bejne/mNF8h5V7mK-qmWQ.html
www.patreon.com/weblearning

Пікірлер: 73
@maharshisorathiya3205
@maharshisorathiya3205 4 жыл бұрын
Great video, but what to do if your receiving buffer has less bytes received than its capacity?, Timeout can be used in this case ?, There are many situations in which you have to send variable length messages.
@Weblearning1
@Weblearning1 4 жыл бұрын
Have a look at this: github.com/MaJerle/stm32-usart-uart-dma-rx-tx
@Bularistan4o
@Bularistan4o 5 жыл бұрын
After debugging for an hour I finally remembered that a while ago I went back to CubeMX and turned off the global interrupt... Im so dumb... Worked as shown, tnx a lot!
@aisyahnajma5722
@aisyahnajma5722 6 жыл бұрын
Nice explanation,especially with the diagram describing the HAL Library UART with IT receive flow
@edgargranados7704
@edgargranados7704 5 жыл бұрын
Thank You so Much for this great tutorial!!!!
@MilanKarakas
@MilanKarakas 5 жыл бұрын
Well done! This is first video for STM32 using CubeMX that shows more than just setup HAL. Yet, I still rather prefer to use direct register accessing.
@Weblearning1
@Weblearning1 5 жыл бұрын
Thanks
@trt969
@trt969 6 жыл бұрын
Thanks for the explanation!
@nikolaykostishen6402
@nikolaykostishen6402 6 жыл бұрын
Thank you! Excellent tutorial. I am waiting for the next one.
@Weblearning1
@Weblearning1 6 жыл бұрын
Hi. You are one of the free boards winner. Please send me your full address in a privet message.
@AbhishekKumar1902
@AbhishekKumar1902 6 жыл бұрын
great video, thanks
@bhquocanh4598
@bhquocanh4598 5 жыл бұрын
Well done!
@muhammetemresanci9575
@muhammetemresanci9575 6 жыл бұрын
Getting interested in the stm32 for studies ,wouldn't mind one of those demo boards if they're still going.
@undercrackers56
@undercrackers56 2 жыл бұрын
Thank you. I apologise for my previous comment. This was indeed the background information that I was looking for. I understand that this STM32 method of receiving data is efficient for bursts of data. However, I struggle to understand how to use the HAL UART to efficiently receive bursts of data of various lengths that are terminated by a known ASCII character. For example GPS data or keyboard input.
@Weblearning1
@Weblearning1 2 жыл бұрын
When you receive the data you need to search for the known char, and then close the data buffer and start a new one.
@NeuroMod
@NeuroMod 6 жыл бұрын
Suscribed.... very usefull and well explained... Will participate on next video giveway, thanks!
@enricoubaldino5856
@enricoubaldino5856 3 жыл бұрын
How is it possible to copy data from DMABuffer to a circular buffer (ex.UARTBuffer) both when the RxCpltCallback interrupt is triggered and when the IDLE DMA interrupt is triggered? How do you manage the writing? thank you
@naasikhendricks1501
@naasikhendricks1501 5 жыл бұрын
Can you please put a link for Dark light?
@Graham1904
@Graham1904 4 жыл бұрын
Great video. UART IT made easy :) How do you "switch off" the UART IT to say reconfigure using a different buffer? Say my RX buffer to start with is just 5-bytes but then I want to use a 30-byte buffer to get different data from a sensor?
@Weblearning1
@Weblearning1 4 жыл бұрын
See this answer: stackoverflow.com/questions/55394656/how-do-i-reset-the-stm32-hal-uart-driver-hal-state
@fnjyusername
@fnjyusername 6 жыл бұрын
great thanks! pls make uart using dma.
@Weblearning1
@Weblearning1 6 жыл бұрын
Its in the pipeline...
@RayMerrell68
@RayMerrell68 6 жыл бұрын
Getting interested in the stm32 for drone control and wouldn't mind one of those demo boards if they're still going.
@Weblearning1
@Weblearning1 6 жыл бұрын
ST has new demo board for drone: www.st.com/en/evaluation-tools/steval-fcu001v1.html I dont have it...
@RayMerrell68
@RayMerrell68 6 жыл бұрын
:) Looks nice.
@kallewirsch2263
@kallewirsch2263 4 жыл бұрын
NOTE: There are some serious API design flaws in the HAL libraries as provided by STM which make the whole HAL-USART module almost useless in the most common practical cases (with respect to interrupts). That is: unless you do not want a reliable program, which enables your µC to work flawless for months or even years - as it is quite common in the world of controller programming. Thus you might want to use the HAL functionality as a guidline of how to work with the USART on the register level but avoid the pitfall of having to deal with a receive interrupt which happens to happen while the processor is working in the transmit function (in which case the HAL uart 'object' is locked and you cannot restart the receive in the callback functions as required in order to continuously receive data). You might want to do this anyway since every sensible USART communication has to handle handshake protocols (either XON/XOFF or hardware handshake). Something the HAL functions ignore completely.
@asimabdulqayyum3360
@asimabdulqayyum3360 6 жыл бұрын
Hi ..I want to interface tcs3200 color sensor with STM32f746zg..Since the tcs3200 outputs a frequency based on the amount of light received i need some way to count pulses to determine a colour.In CubeMx i set PF6(s0) PF7(s1) PF8(s2) PF9(s3) and TIM2 input capture as (out) of the sensor.I also enabled tim2 globle interrupt in nvic window.following is my setting for output frequency scaling and filtertype outside the main function.. void TCSFREQ() { HAL_GPIO_WritePin(GPIOF, s0_Pin,GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOF, s1_Pin,GPIO_PIN_RESET); } void TCSRED() { HAL_GPIO_WritePin(GPIOF, s2_Pin,GPIO_PIN_RESET); HAL_GPIO_WritePin(GPIOF, s3_Pin,GPIO_PIN_RESET); } void TCSBLUE() { HAL_GPIO_WritePin(GPIOF, s2_Pin,GPIO_PIN_RESET); HAL_GPIO_WritePin(GPIOF, s3_Pin,GPIO_PIN_SET); } void GREEN() { HAL_GPIO_WritePin(GPIOF, s2_Pin,GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOF, s3_Pin,GPIO_PIN_SET); } void TCSCLEAR() { HAL_GPIO_WritePin(GPIOF, s2_Pin,GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOF, s3_Pin,GPIO_PIN_RESET); } Correct me if there is something wrong above. As i am new to micro controllers i am struggling how to count no of pulses in a time period let say 2s..it would be nice if you help me with my problem or suggest some alternative solution..thanks
@Kefford666
@Kefford666 5 жыл бұрын
How does the code automatically transmit the txBuffer? I understand that the tx interrupt signals that the byte has been sent and another one can be buffered in the register but I don't see in your main where this process is started. Also how does the code know the fill the rxBuffer and not just write over the first byte? It is working like DMA but without having the DMA set up. How could you handle it if multiple bytes were being received (e.g. a packet) and you wanted to check the validity of the bytes as they came in, this cannot be done with the receive callback as it needs to happen per-byte. Would you put thi logic in the IRQ handler in the stm32L0xx_it.c file? Thank you for sharing your knowledge :)
@bayremgharssellaoui238
@bayremgharssellaoui238 5 жыл бұрын
hello, as for your last question "you wanted to check the validity of the bytes as they came in", you can set the rxbuffer size to 1, that way the rx callback will be called each time you recieve a byte
@kallewirsch2263
@kallewirsch2263 4 жыл бұрын
The sending is handled by the HAL library. When calling Transmit_IT you pass the number of bytes to send. The HAL Interrupt handler will take care of that number and transfer all bytes into the USART. Only after all bytes have been sent, the TxCallBack function will be called. NOTE: There are some serious API design flaws in the HAL libraries as provided by STM which make the whole HAL-UART module almost useless in the most common practical cases with respect to interrupts. That is: unless you want a reliable program, which enables your µC to work flawless for months or even years - as it is quite common in the world of controller programming. Thus you might want to use the HAL functionality as a guidline of how to work with the UART on the register level but avoid the pitfall of having to deal with a receive interrupt which happens to happen while the processor is working in the transmit function (in which case the HAL uart 'object' is locked and you cannot restart the receive in the callback functions as required). You might want to do this anyway since every sensible UART communication has to handle handshake protocolls (either XON/XOFF or hardware handshake). Somthing the HAL functions ignore completely.
@armincal9834
@armincal9834 3 жыл бұрын
@@kallewirsch2263 are the said flaws still an issue with HAL USART?
@kallewirsch2263
@kallewirsch2263 3 жыл бұрын
@@armincal9834 I have just looked it up in the HAL library for the F2. They have fixed that issue by splitting the state variable into 2 variables. At least that problem is solved. That also seems to be true with the HAL library for the F7. But of course the issues with simply ignoring a possible handshake still remains.
@JoseCarlos-wg4tr
@JoseCarlos-wg4tr 4 жыл бұрын
Can it be possible to use a 115200 baud with interrupts, if yes what changes should I make (HW or FW wise), if not, why 9600? Thanks in advance, hope you can help me :)
@Weblearning1
@Weblearning1 4 жыл бұрын
It all depends on how fast is you core ruining and how much data you are passing. For every byte that you will send, you will get an IT. and except for the code you have total 12 clk cycle for the push and pop. so at some point, you will not have any time for your code. Id its very little data at that speed, then there is no problem. It all depends on your application.
@Don_Mateo45
@Don_Mateo45 4 жыл бұрын
Can you do circle buffer with interrupt? :)
@Weblearning1
@Weblearning1 4 жыл бұрын
Hi. Yes. Have a look at the video with ADC and DMA. Thanks.
@rohithsai7759
@rohithsai7759 3 жыл бұрын
I'm using rs485 as a uart tool and can't able to receive the interrupt if I go through the breakpoint at receiving function
@Weblearning1
@Weblearning1 2 жыл бұрын
Try looking at the RS485 examples in the repository.
@armincal9834
@armincal9834 3 жыл бұрын
Interestingly, when I follow your tutorial and declare uin8_t as the data type for my Rx buffer, I get gibberish in my terminal(some squares in the built-in terminal of stm32cubemx and some strange symbols in putty) but when I declare it as char, everything works perfectly. Does anyone know why?
@Weblearning1
@Weblearning1 3 жыл бұрын
What uart speed are you using? are you using HSI or HSE? what are you trying to print ( char or hex)?
@armincal9834
@armincal9834 3 жыл бұрын
@@Weblearning1 Tha KS for you answer. The problem was that the terminal was reading much slower than my USART was transferring data. I am trying to hook up my MCU to a sim module so I want to transfer AT commands (char data type). When I send the commands to my pc to debug everything is OK, I think when I send them to my Sim module it also works(I am sending AT and am expecting AT OK as response) however so far I have been able to capture the first letter only(A) I think my timing is off between transferring and reading I know you can't help me without my code but can you show me some resources where I can learn how to write my receive_it callback(I'm sure this function is the one which is causing problems.) the book I am reading has only 1 example which has nothing to do with my case
@yassin5568
@yassin5568 3 жыл бұрын
Great Video😊 Can you tell me please how can I delete the received data from Rx_buffer and to receive new data from Rx_buffer[0] ?
@Weblearning1
@Weblearning1 3 жыл бұрын
Reset the location of Rx_buffer. to clear the buffer: memset(Rx_buffer, '\0', Rx_buffer_SIZE); May be you can considerer subscribing to the channel?
@serhatyars8770
@serhatyars8770 3 жыл бұрын
Hi @Yassin You can consider to use the HAL_UART_Receive_IT(&huart2,rx_buff,10) func again in the HAL_UART_RxCpltCallback(). Therefore, you kind of reset the process. In addition, by this method, you don't need to delete something in the memory manually.
@yassin5568
@yassin5568 4 жыл бұрын
Hello, I would like to stock die new data in buffer always beginning form the first field (data[0]). It means, to read the old data, clear it and to read the new data beginning again from the first field. Thanks in advance, hope you can help me.
@yassin5568
@yassin5568 4 жыл бұрын
I use UART_IT and stm32f429zi
@Weblearning1
@Weblearning1 4 жыл бұрын
So after you read the buffer, return the buffer pointer back to 0.
@jan_mohorko
@jan_mohorko 4 жыл бұрын
@@Weblearning1 Thanks for this nice tutorial, how to "return the buffer pointer back to 0"
@jan_mohorko
@jan_mohorko 4 жыл бұрын
huart1.pRxBuffPtr = &rx_buff[0]
@aliaslmx
@aliaslmx 5 жыл бұрын
Let's assume I will receive a string formed of 10 to 500 characters. It has a start character another stop character. The length varies. How could I save the entire string? After this, I need to check if the stop character was received and call another function.
@Weblearning1
@Weblearning1 5 жыл бұрын
You need to use the timeout of the complete string. Put the string in an array and search for the stop character.
@aliaslmx
@aliaslmx 5 жыл бұрын
@@Weblearning1 Thank you for your prompt reply! Meanwhile, I used the following idea ISR { if(position == RX_BUFF_SIZE) position = 0; //RX_BUFF_SIZE 100 rx_buff[position] = in_char[0]; //rx_buff[2] but only first character is used position++; HAL_UART_Receive_IT(&hlpuart1, in_char, 1); } I prefer to keep the cpu busy with ISR.
@longli8580
@longli8580 6 жыл бұрын
Hello, I have written the same code but no data was printed on USART screen, I still don't konw why it's so after I checked it for hours.
@Weblearning1
@Weblearning1 6 жыл бұрын
What's is the speed of the uatt you are using. Did you follow everything I have showed or you have skipped few sections of the video?
@longli8580
@longli8580 6 жыл бұрын
Thanks for anserwing me ! yes I totally did this accoding to your code flow . I used Baudrate 9600 bits/s . And I also tried the blocking method by which still no data came out on PC terminal. So cofused with that :(
@Weblearning1
@Weblearning1 6 жыл бұрын
Then start slowly. Check that you can toggle the same pin with a LED or something that you can check. If that works, check you can see data with a scope and if you don't have one, then check your hardware ( wires, jumpers etc ) to see that everything is working. You can also make a short in PA9 and PA10 ( the UART pines ) and write in the PC. This will amke a feedback and you should see everything you type back in the PC. This will check the hardware all the way to the STM32.
@longli8580
@longli8580 6 жыл бұрын
thanks for suggestions yes I think i might find the problem. I use STM32F4Discovery board and I looked up the mannuel again which said UART Pins didn't connect with st-link chip ,if I want to use UART I must jump the wires or use a dongle . So I think basically that's why no communication between PC and my STM32 board can be built there.
@naasikhendricks1501
@naasikhendricks1501 5 жыл бұрын
@@longli8580 Uart2 pins is found on the STLink section unless you bridge the pins. (Assuming you using the nucleo board)
@dabdoube92
@dabdoube92 2 жыл бұрын
I'm stuck. Help ! I can fill the first buffer. But I can't receive anything once the buffer is filled ! Please help.
@Weblearning1
@Weblearning1 2 жыл бұрын
Did you follow all the instructions? If you use circular buffer it will keep on filling. If Normal, then you have to start it again.
@dabdoube92
@dabdoube92 Жыл бұрын
@@Weblearning1 Thank you !
@xDR1TeK
@xDR1TeK 4 жыл бұрын
Hi, hope anyone here can help, STM32F103C6 in Proteus with loaded hex from SW4, Transmit_IT not transmitting. I tried everything, saw multiple videos on YT, from ST and others. All do the same thing but still my TX IT wont work. Not sure what is wrong. using System Workbench - Eclipse Neon 3 release 4.6.3. UPDATE: it won't work in simulation, tried it on an STM32F103C8 and it worked on the fly.
@Weblearning1
@Weblearning1 4 жыл бұрын
First connect the TX to the RX with a cable, this will echo your data back. If this is not working, it your code. If it is working, then it your hardware to the outside world.
@xDR1TeK
@xDR1TeK 4 жыл бұрын
@@Weblearning1 it worked in real world, but not in sim. I had to apply IT on RX only, and regular TX. That worked fine for UART DMA, and SPI as well. Sim must be too demanding for processing, thus they perhaps limited the available functions in sim.
@iAnguel
@iAnguel 4 жыл бұрын
Great, but please slow down, I am not even able to pause that fast :-)
@Weblearning1
@Weblearning1 4 жыл бұрын
You can slow down the movie by pressing on the settings tab.
@T0R0RIKI
@T0R0RIKI 3 жыл бұрын
I wouldn't call this a tutorial, hard to follow. Just a recap show off if u are already used to those stuffs
@Weblearning1
@Weblearning1 2 жыл бұрын
You have to start from the beginning of the videos. If I would start on each video from start, they will be an hour long.
Did you believe it was real? #tiktok
00:25
Анастасия Тарасова
Рет қаралды 49 МЛН
Пробую самое сладкое вещество во Вселенной
00:41
ОСКАР vs БАДАБУМЧИК БОЙ!  УВЕЗЛИ на СКОРОЙ!
13:45
Бадабумчик
Рет қаралды 3,5 МЛН
Change!!😁 #shorts #レオ
0:46
レオ★スマイル
Рет қаралды 8 МЛН
孩子多的烦恼?#火影忍者 #家庭 #佐助
0:31
火影忍者一家
Рет қаралды 47 МЛН
Бездомный помог ребятам на дне рождения 🤯
0:39
Фильмы I Сериалы
Рет қаралды 3,5 МЛН
Duniya ki sab se pyari awaz 🕋 #azan #islamicprayer #quran
0:12
ashfaq Mughal
Рет қаралды 55 МЛН
Times Square
0:16
ARGEN
Рет қаралды 2,1 МЛН