USART introduction
The universal synchronous asynchronous receiver transmitter (USART) offers a flexible
means of full-duplex data exchange with external equipment requiring an industry standard
NRZ asynchronous serial data format. The USART offers a very wide range of baud rates
using a fractional baud rate generator.
It supports synchronous one-way communication and half-duplex single wire
communication. It also supports the LIN (local interconnection network), Smartcard Protocol
and IrDA (infrared data association) SIR ENDEC specifications, and modem operations
(CTS/RTS). It allows multiprocessor communication.
High speed data communication is possible by using the DMA for multi buffer configuration.
USART main features
- Full duplex, asynchronous communications
- NRZ standard format (Mark/Space)
- Configurable oversampling method by 16 or by 8 to give flexibility between speed and
- clock tolerance
- Fractional baud rate generator systems
- – Common programmable transmit and receive baud rate (refer to the datasheets
- for the value of the baud rate at the maximum APB frequency.
- Programmable data word length (8 or 9 bits)
- Configurable stop bits – support for 1 or 2 stop bits
- LIN Master Synchronous Break send capability and LIN slave break detection
- capability
- – 13-bit break generation and 10/11 bit break detection when USART is hardware
- configured for LIN
- Transmitter clock output for synchronous transmission
- IrDA SIR encoder decoder
- – Support for 3/16 bit duration for normal mode
- Smartcard emulation capability
- – The Smartcard interface supports the asynchronous protocol Smartcards as
- defined in the ISO 7816-3 standards
- – 0.5, 1.5 stop bits for Smartcard operation
- Single-wire half-duplex communication
- Configurable multibuffer communication using DMA (direct memory access)
How to use this driver
The UART HAL driver can be used as follows:
1. Declare a UART_HandleTypeDef handle structure (eg. UART_HandleTypeDef huart)
2. Initialize the UART low level resources by implementing the HAL_UART_MspInit() API:
a. Enable the USARTx interface clock.
b. UART pins configuration:
◦ Enable the clock for the UART GPIOs.
◦ Configure these UART pins (TX as alternate function pull-up, RX as alternate function Input).
c. NVIC configuration if you need to use interrupt process (HAL_UART_Transmit_IT() and
HAL_UART_Receive_IT() APIs):
◦ Configure the USARTx interrupt priority.
◦ Enable the NVIC USART IRQ handle.
d. DMA Configuration if you need to use DMA process (HAL_UART_Transmit_DMA() and
HAL_UART_Receive_DMA() APIs):
◦ Declare a DMA handle structure for the Tx/Rx stream.
◦ Enable the DMAx interface clock.
◦ Configure the declared DMA handle structure with the required Tx/Rx parameters.
◦ Configure the DMA Tx/Rx stream.
◦ Associate the initialized DMA handle to the UART DMA Tx/Rx handle.
◦ Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx
stream.
◦ Configure the USARTx interrupt priority and enable the NVIC USART IRQ handle (used for last
byte sending completion detection in DMA non circular mode)
3. Program the Baud Rate, Word Length, Stop Bit, Parity, Hardware flow control and Mode(Receiver/
Transmitter) in the huart Init structure.
4. For the UART asynchronous mode, initialize the UART registers by calling the HAL_UART_Init() API.
5. For the UART Half duplex mode, initialize the UART registers by calling the HAL_HalfDuplex_Init() API.
6. For the LIN mode, initialize the UART registers by calling the HAL_LIN_Init() API.
7. For the Multi-Processor mode, initialize the UART registers by calling the HAL_MultiProcessor_Init() API
Polling mode IO operation
• Send an amount of data in blocking mode using HAL_UART_Transmit()
• Receive an amount of data in blocking mode using HAL_UART_Receive()
For this blog we are going to use UART in polling mode , for demonstration purpose we will configure one DAC, output of DAC will be Input of ADC channel & Timer 2 is used to trigger adc conversion.
Creating STM32 executable projects steps are available on this link , please follow steps 1 to 10 as per blog, here we will start from step 11. PA9 & PA10 Pin used for debugging purpose as UART Tx & UART Rx respectively. this blog is specially for uart configuration & its usage in polling mode, here we have used proprietary library function for printing or scanning data from serial terminal.
UART in DMA mode click here , UART in Interrupt mode click here
11. Enable UART Configuration
Go to System mode > Connectivity > USART1 > Mode > Asynchronous
Go to System mode > Connectivity > USART1 > Parameter settings > Default
12. PA9 : USART_Tx & PA10: USART_Rx
13. PA4 as DAC & PA0 as ADC_IN0
14. Press below icon for code generation
15. Sample Code:
//Reference code from proprietary library functions //Receive Function char uart1_getch_debug(void) { uint8_t temp_data; HAL_UART_Receive(&huart1,&temp_data,1,1); return temp_data; } //Transmit Function void uart1_putch_debug(char data) { uint8_t temp_data = 0; if (data == '\n') { temp_data = '\r'; HAL_UART_Transmit(&huart1, &temp_data, 1,1000); } HAL_UART_Transmit(&huart1, &data, 1,1000); }
Software Tools:
- STM32CubeIDE
- STM32CubeMx
- Teraterm
Hardware Setup:
- STM32F429IDISCOVERY board
- Mini USB Cable
- Jumper wire
Conclusion:
Successfully demonstrated UART functionality in polling mode, for testing purpose we have used DAC to connect its output to ADC & UART for debug.
References:
- STM32 HAL Library
- STM32 UM1718 document