UART
UART
Application note
Implementing an emulated UART
on STM32F4 microcontrollers
Introduction
This application note describes how to implement an emulated universal asynchronous
receiver transmitter (UART) on microcontrollers of STM32F4 Series.
Such an emulation is needed in applications that require more UARTs than the ones
available on STM32F4 microcontrollers.
The emulated UART is full-duplex, supports up to 9 data bits and baud rates up to
115200 bps. It also offers high flexibility, as any I/O pin can be used as TX or RX line. In
addition, this UART emulation uses DMA to minimize CPU usage.
This application note provides a basic example of communication between a hardware and
a software UART, as well as a summary of CPU load and firmware footprint.
The STSW-STM32156 firmware package associated with this document contains the
source code of the UART emulator with all firmware modules required to run the example.
Contents
2 Software description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.1 Implementation structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11
2.2 Transmission . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.2.1 Frame transmission . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.2.2 Transmission routine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.3 Reception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.3.1 Frame reception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.3.2 Reception routine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.4 UART emulator peripherals and main functions . . . . . . . . . . . . . . . . . . . . 16
2.4.1 Peripheral settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.4.2 Initialization and configuration function . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.4.3 UART main functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.1 Hardware requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.2 Software settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3.3 Running the example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3.4 Frame waveforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
5 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
6 Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
List of tables
List of figures
The principle consists in emulating the UART protocol via GPIO, EXTI, timer and DMA
peripherals.
5HFHSWLRQ5RXWLQH
)RUPDW'DWD
3( )( 5& 7& 7UDQVPLVVLRQ5RXWLQH
6WDWXV)ODJV
&257(;0
(;7,
5;
*3,2
7;
7,0 65$0
'0$
8$57(PXODWRU
06Y9
• Software modules
They include the routines used for transmitting/receiving data and for formatting the
data to be sent by DMA or stored into SRAM. Flags indicating the UART status are also
available:
– TC: Transmission Complete
This flag is set by software when data transmission is complete.
– RC: Reception Complete
This flag is set by software when data reception is complete.
– FE: Framing Error
This flag allows frame error detection.
– PE: Parity Error
This flag is set by software when a parity error occurs in receive mode.
• Hardware modules
They consist of all the STM32F4 peripherals involved in UART emulation:
– GPIO: I/O ports used as transmitter or receiver pins.
– EXTI: in reception mode, the start bit falling edge is detected via an EXTI.
– DMA: data transfers are performed by DMA.
– TIM: it ensures that data are transmitted and received at the required speed.
– SRAM: data are formatted and stored in the SRAM.
2QHELW
GXUDWLRQ
'DWD ELWV
6HFRQG 75T 75T 75T 75T 75T 75T 75T 75T 75T
)LUVW75T7LPHU
75T
5HTXHVWV'0$WR
VHWWKH)LUVWELW
06Y9
'DWD ELWV
7LPHU5HTXHVWV
7LPHU5HTXHVWV'0$ '0$WRJHWWKHODVW
WRJHWWKHVWELW ELW
06Y9
1.5 Baudrate
The UART emulator supports baudrates up to 115200 bps.
The baudrate generation is ensured by the timer using different parameters, such as clock
division, prescaler and period (ARR value).
The calculation of timer period (bit duration) is based on the following formula:
Example
• Configuration
Timer prescaler = 0
Timer clock division = 0
AHB clock = system clock = 84 MHz
APB2 frequency = 21 MHz
UART baudrate = 9600 bps
• Result
APB2 prescaler = 4
as a result
and
2 Software description
$33/,&$7,21
VWPI[[BKDOBXDUWBHPXOF
VWPI[[BKDOBXDUWBHPXOK
8$57HPXODWRU
06Y9
2.2 Transmission
7UDQVPLW)UDPH
QR 7[;IHU&RXQWLV \HV
(YHQ"
'0$VRXUFH '0$VRXUFH
)LUVW%XIIHUB7[ 6HFRQG%XIIHUB7[
(QDEOH'0$7&,7
(QDEOH'0$DQG7LPHU
(1'
06Y9
8$57(08/7;
,QLWLDOL]HSDUDPHWHUV
8$576WDWXV QR
UHDG\"
+DOB(UURU
\HV
)RUPDWIUDPH
7UDQVPLW)UDPH
7[;IHU&RXQW QR
GDWDBVL]H"
'LVDEOHLQWHUUXSWV
\HV
6HW8$577&)ODJ
5HVHW7[;IHU&RXQW
6HW8$577&&DOO
EDFN
8SGDWH8$576WDWXV
(1'
06Y9
2.3 Reception
5HFHLYH)UDPH
QR 5[7UDQVIHU&RXQW \HV
LV(YHQ"
'0$GHVWLQDWLRQ '0$GHVWLQDWLRQ
)LUVW%XIIHUB5[ 6HFRQG%XIIHUB5[
(QDEOH'0$7&,7
(QDEOH'0$DQG7LPHU
(1'
06Y9
8$57(08/5;
,QLWLDOL]H
SDUDPHWHUV
8$57
6WDWXV QR
UHDG\"
+DOB(UURU
\HV
(QDEOH(;7,
%LWVWDUW QR
GHWHFWLRQ"
\HV
5HFHLYH%\WH
)RUPDW%\WH
5[;IHU&RXQW QR
GDWDBVL]H"
'LVDEOHLQWHUUXSWV
\HV
6HW8$575&)ODJ
5HVHW5[;IHU&RXQW
6HW8$575&&DOO
EDFN
8SGDWH8$57
6WDWXV
(1'
06Y9
Callback functions are also available. They allow the user to implement his own code in the
user file.
3 Example
The example provided in STSW-STM32156 illustrates the data exchange between the
UART emulator and the hardware UART.
1XFOHRERDUG 1XFOHRERDUG
$ %
8$57 7; 5; +DUGZDUH
HPXODWRU 5; 7; 8$57
3XVKEXWWRQ
5(6(7
5(6(7
/('
/('
*1' *1'
06Y9
ELWV'DWD
6WDUW 6WRS
ELW ELW
06Y9
ELWV'DWD
6WRS
ELW
6WDUW
ELW 3DULW\ELW
06Y9
The UART emulator uses the CPU for several tasks such as data formatting, DMA interrupt
handling both transmission and reception, and EXTI interrupt handling for reception.
4800 2.4 2 4 3
9600 5 4 8.8 7
115200 6 5 9 7.5
2872 400
5 Conclusion
This application note demonstrates that the product capability can be increased by adding
an emulated UART.
This solution has many advantages, among them reduced CPU load by using DMA for data
transfer. In addition, the user can configure any GPIO as UART transmitter or receiver.
6 Revision history
STMicroelectronics NV and its subsidiaries (“ST”) reserve the right to make changes, corrections, enhancements, modifications, and
improvements to ST products and/or to this document at any time without notice. Purchasers should obtain the latest relevant information on
ST products before placing orders. ST products are sold pursuant to ST’s terms and conditions of sale in place at the time of order
acknowledgement.
Purchasers are solely responsible for the choice, selection, and use of ST products and ST assumes no liability for application assistance or
the design of Purchasers’ products.
Resale of ST products with provisions different from the information set forth herein shall void any warranty granted by ST for such product.
ST and the ST logo are trademarks of ST. All other product or service names are the property of their respective owners.
Information in this document supersedes and replaces information previously supplied in any prior versions of this document.