Transmit data on UART without using any HAL function
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-05-25 02:29 AM
Dear all,
I am working on STM32F413 microcontroller.
I want to transmit data on UART without using any HAL function.
And I want to send data in non blocking mode means on interrupt mode not in polling mode.
Can anyone help me out in this?
Thanks
Solved! Go to Solution.
- Labels:
-
STM32F4 Series
-
UART-USART
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-05-25 12:57 PM
First
- read RCC, GPIO, UART chapters in RM
In the global variables:
- prepare a circular buffer for Tx, i.e. define an array of bytes of a given size, and two variables for buffer head and tail
In startup part of main():
- enable GPIO clocks in RCC
- enable UART clock in RCC
- set up UART pins in respective GPIO_MODER as AF, and the AF per pin assignment table in datasheet
- set UART baudrate, and enable UART
- don't enable UART Tx interrupt in UART yet
- enable UART interrupt in NVIC, optionally set its priority
In the program:
- write UART ISR, make sure its name matches the one in the vector table in startup file
- in the ISR, read in the status bits and check if the Tx empty bit is set; if not, simply exit the ISR
- check if there are bytes to transmit (i.e. buffer tail does not equal head), if yes, pick byte, place it into the transmit data register, increment (wrapping) tail and exit ISR
- if there are no bytes to transmit, disable Tx interrupt in USART and exit
In main, during runtime, whenever need to transmit arises:
- place the data into the circular buffer, after checking if there's space for it (i.e. if the new head won't go past tail)
- store the new head
- enable the Tx interrupt in USART
JW
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-05-25 06:51 AM
Here is an example:
https://github.com/STMicroelectronics/STM32CubeF4/blob/2f3b26f16559f7af495727a98253067a31182cfc/Projects/STM324x9I_EVAL/Examples/UART/UART_Hyperterminal_IT/Src/main.c
Edit: oh, without HAL.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-05-25 08:06 AM
UART examples by Tilen Majerle. Mostly LL, no HAL. And DMA :thumbs_up:
https://github.com/MaJerle/stm32-usart-uart-dma-rx-tx/tree/master/projects
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-05-25 09:40 AM
>>Can anyone help me out in this?
I'm not interested in writing all this code, if you want to do it without the HAL at least understand how the HAL implements it at a register level, and how the Reference Manual describes the registers. And how the NVIC works on the CM4.
If you want to do register level coding you need to own the task
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-05-25 11:48 AM
A good way to do this stuff is to use cube IDE to generate the code, and then chop all the garbage (like error conditions which cannot happen unless the silicon is faulty) out of it, then paste the remainder into your project, and throw away the original "project". I have never done this but know somebody who does.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-05-25 12:57 PM
First
- read RCC, GPIO, UART chapters in RM
In the global variables:
- prepare a circular buffer for Tx, i.e. define an array of bytes of a given size, and two variables for buffer head and tail
In startup part of main():
- enable GPIO clocks in RCC
- enable UART clock in RCC
- set up UART pins in respective GPIO_MODER as AF, and the AF per pin assignment table in datasheet
- set UART baudrate, and enable UART
- don't enable UART Tx interrupt in UART yet
- enable UART interrupt in NVIC, optionally set its priority
In the program:
- write UART ISR, make sure its name matches the one in the vector table in startup file
- in the ISR, read in the status bits and check if the Tx empty bit is set; if not, simply exit the ISR
- check if there are bytes to transmit (i.e. buffer tail does not equal head), if yes, pick byte, place it into the transmit data register, increment (wrapping) tail and exit ISR
- if there are no bytes to transmit, disable Tx interrupt in USART and exit
In main, during runtime, whenever need to transmit arises:
- place the data into the circular buffer, after checking if there's space for it (i.e. if the new head won't go past tail)
- store the new head
- enable the Tx interrupt in USART
JW