0% found this document useful (0 votes)
19 views24 pages

6-UARTa

The document discusses serial communication methods, focusing on UART, SPI, and I2C protocols, highlighting the advantages of serial communication over parallel. It details the configuration and operation of UART on STM32 microcontrollers, including key registers and steps for setting up communication parameters. Additionally, it outlines practical activities for configuring UART to send and receive data, such as sending a name to a PC and controlling an onboard LED based on received characters.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views24 pages

6-UARTa

The document discusses serial communication methods, focusing on UART, SPI, and I2C protocols, highlighting the advantages of serial communication over parallel. It details the configuration and operation of UART on STM32 microcontrollers, including key registers and steps for setting up communication parameters. Additionally, it outlines practical activities for configuring UART to send and receive data, such as sending a name to a PC and controlling an onboard LED based on received characters.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

UART

With STM32

N Amanquah
Why communicate serially?
• CPU word size is 32 bits, sending all of a word’s bits simultaneously has
disadvantages:

• Cost and weight: larger IC package, more wires, larger connectors


• Mechanical reliability: more wires => more connector contacts to fail
• Timing complexity: some bits may arrive later than others due to
variations in capacitance and resistance across conductors
• Circuit complexity and power: may not want to have 16 different
transmitters + receivers in the system

• Communicating serially reduces number of signals needed


Serial Communications options
• Connection:
• UART (not a protocol)
• a physical layer with Transmit and Receive serial data lines
• Can be used with other protocols eg MODBUS
• Protocols:
• SPI
• I2C
Motorola
Serial Peripheral Interface
https://en.wikipedia.org/wiki/Serial_Peripheral_Interface

Serial Peripheral Interface (SPI) communications


This is synchronous, full duplex serial
communications
Applications: eg between to MCUs Single master to single slave: basic SPI bus example

MOSI= Master Out, Slave In


SS= slave select
SCLK= Serial Clock

Typical SPI bus: master and three independent slaves Daisy-chained SPI bus: master and cooperative slaves
Philips

Inter Integrated Circuit (I2C)


• Synchronous, multi-slave, multi-master, intra-board communications
• 2-wire bus
• SDA sending the actual data (Serial data)
• SCL clock signal (serial clock)
• Applications:
• I2C LCD
• Gyroscope & Accelerometer modules
• Between two microcontrollers eg between two Arduinos
• OLED display
• RTC
Universal Asynchronous Receiver-Transmitter
(UART)
• Configurable data format and transmission speeds
• Data frame fields
• Start bit (one bit)
• Data
• send LSB or MSB first?
• size – 7, 8, 9 bits)
• Optional parity bit is (Y or N), and if Y, Odd or Even?
• Stop bit (one or two bits)
• Specify Start & Stop bits (to deal with timing issues)

• Devices must use the same communications parameters


• E.g. communication speed (300 baud, 600, 1200, 2400, 9600, 14400,
19200, etc.)
UART key features
• A clock generator,
• Greater than, or multiple of the bit rate to allow sampling of data to be
transmitted.
• Input and output shift registers
• Receive and transmit data registers/buffers (USART_RDR, USART_TDR)
• Write into or read from these buffers
• May store Tx/Rx data in a queue (there is usually more than one byte to send/Rx)

• Data rates:
• Set baud rate in register (USART_BRR)
• Can generate interrupts & errors
• Overrun – could not process data before next arrived.
• Framing error – cannot find stop bit at proper place.
• Parity error- parity is not same as indicated in setup
• Status registers e.g. (USART_ISR)
Multiple UART peripherals on STM32

$27.4, page 702


UART Pin out on STM
USART: Chapter 27 of Ref

Key registers
Goal:
configure as 8N1 8bits, No parity, 1 stop bit
baud rate
Overview of steps
• Turn on Clock for port
• Set ALT MODER for UART
• Set the AFRL (or H) for the Pin (alternate function register)

• Turn on clock for UART


• Set baud rate divisor
• Set 8N1 (configure connection details)
• Enable transmitter/Rx
• Enable UART
USART control register 1 (USART_CR1)

PEIE- ParityError Interrupt Enable Related


TXEIE- Transmitter Interrupt is raised when TXE is 1 see later
TCIE –Transmission Complete Interrupt Enable slide
Bit 9 (PS): Parity selection. 0=Even, 1=Odd
RCNEIE- RXNE interrupt Enable
TE- transmit Enable
UE-UART enable
USART control register 2 (USART_CR2)

Fields such as
RTOEN: Receiver timeout enable

MSBFIRST: Most significant bit first (defualt is bit 0 is transmitted first)


DATAINV: Binary data inversion (default: positive is high)
SWAP: Swap TX/RX pins (default: use pins as specified on board)
STOP[1:0]: STOP bits (default: 00: 1 Stop bit)

CPOL: Clock polarity (used together to set clock phase and polarity)
CPHA: Clock phase
USART baud rate register (USART_BRR)

BRR = USARDIV (if oversampling by 16, the default, OVER8=0)


Baud rate calculation

• $27.5.4, p 716
• If oversampling by 16,
• Implies USARTDIV= fck/buad

• To derive USARTDIV from USART_BRR register values


• To obtain 9600 baud with fCK = 8 MHz.
• In case of oversampling by 16:
• USARTDIV = 8 000 000/9600
• BRR = USARTDIV = 833d = 0x0341
USART interrupt and status register
(USART_ISR)

BUSY: Busy flag: High when reception in progress


TXE: Transmit data register empty
This bit is set by hardware when the content of the USART_TDR register has been
transferred into the shift register for onward transmission. (check for when this is 1 and send new data to TDR.

TC: Transmission complete set when the transmission of a frame containing data is complete and if
TXE is set

RXNE: Read data register not empty (data has arrived) check for when this is 1 and read it
This bit is set when the content of the RDR shift register has been transferred to the USART_RDR register

Overrun, Noise, Framing and parity errors are also flagged.


USART interrupt flag clear register
(USART_ICR)

Bit 6 TCCF: Transmission complete clear flag


Writing 1 to this bit clears the TC flag in the USART_ISR register.

Overrun, Noise, Framing & Parity Error flags


USART receive data register (USART_RDR) bit [8:0]

USART transmit data register (USART_TDR) bits[8:0]


Transmission:

Transmit data registers: USART_TDR

The shift register shifts onto the communication channel.

TE bit must be set before writing the data to be transmitted to the USART_TDR
The TDR register is an interface between the internal bus and the output shift register

When TXE=1, it means TDR is empty (has been copied already to shift register)
This is a good time to write to TDR. Writing to TDR clears TXE.

After transmission by the sift register is complete (and TXE is also 1, i.e. no new data), the TC becomes 1
Reception:

Receive data register: USART_RDR


The RDR register is an interface between the internal bus and the input shift register

When character is received, RXNE is set to 1, (i.e. content of receive shift register has been copied to RDR)
This is a good time to read from RDR. (RXNEIE interrupt also thrown)

The RXNE flag is cleared when RDR is read.


Turn on Clock for port
Set ALT mode for UART
Set the AFRL (or H) for the Pin

Turn on clock for uART


Set baud rate divisor
Set 8N1
Enable transmitter/Rx
Enable UART
Overview of configuration
• Turn on Clock for port
• Set ALT mode for UART
• Set the AFRL (or H) for the Pin

• Turn on clock for UART


• Set baud rate divisor
• Set 8N1
• Enable transmitter/Rx
• Enable UART
Activity
• Configure a UART (Tx) to send your name to a PC
• Configure a UART (Rx) to turn on the onboard LED if you send a 1
and turn it off if you send a 0. (treat these as characters)

• Configure both Rx and Tx so that when you send any character, it


is repeated 3x back to the terminal, and then moves to the next
line.

You might also like