universal asynchronous receiver transmitter...
TRANSCRIPT
© G. J. Minden 2013
Universal Asynchronous Receiver Transmitter
UART
1
Gary J. MindenFebruary 11, 2014
© G. J. Minden 2013
Serial Communications• A few example protocols
• A few example principles
• Focus on Universal Asynchronous Receiver Transmitter (UART)• Overview
• Hardware Interface
• Software Programming
• Technical Details• Chapter 12 of TI_Stellaris_LM3S1968.pdf
• Chapter 28 of TI_Stellaris_DriverLib.pdf
2
© G. J. Minden 2013
Communicate Between Processors
3
Host RxData
Host Tx
•Asynchronous -- Receiver does not know when data will arrive
•Protocol -- agreement between transmitter and receiver on the format of the data
© G. J. Minden 2013
Common Means of Communications
4
• Serial -- one bit at a time• UARTs• USB• Ethernet• SSI -- Synchronous Serial Interface• I2C• CAN -- Component Area Network• PCIe -- PCI Express
• Parallel• Multiple Buses• HDMI• APB -- Advanced Peripheral Bus• DDR -- DRAM/Processor Bus
© G. J. Minden 2013
Common Protocols -- Asynchronous• Tx and Rx do not share common clock
• Receiver does not “know” when data will arrive• Start of transmission indication
• UART, Ethernet, CAN
• Receiver selection
• Parallel bus, I2C, SPI
• Clocks at Transmitter and Receiver can be (slightly) different• Tx and Rx agree on nominal clock rate, e.g. 115,200 bps
• Account for clock differences (UART, CAN)
• Embed clock in signals (Ethernet)
5
© G. J. Minden 2013
UART Serial
6
StartProtocolBit Time
SampleTime
© G. J. Minden 2013
Common Protocols -- Synchronous• Transmitter and Receiver share a common clock
• Start of transmission synchronized to common clock
• Data transmission synchronized to common clock
7
0 0 1 0 0 1
E.g. Manchester Encoding (Ethernet)
1 0
31X
1 1
Ethernet Preamble -- 64 bits
© G. J. Minden 2013
Link Protocols• Single Protocol Data Unit (PDU)
• UART, I2C, SPI
• Short Protocol Data Units (PDUs)• CAN (1-8 Bytes)
• Long Protocol Data Units (PDUs)• USB, Ethernet, PCIe, HDMI
8
© G. J. Minden 2013
Focus on UARTs• Functions
• Start of transmission
• Transmission of data
• Error detection
• End of transmission
• Parameters• Bit Rate (BAUD Rate)
• Number of bits (5-8)
• Parity (Even, Odd, None)
• Stop bits (1-2)
9
© G. J. Minden 2013
Transmitting ASCII ‘e’
10
• ASCII ‘e’ is 0x65
• Even Parity, total number of ‘1’ bits is even
• LSB transmitted first
• 1 Stop Bit
I St 1 0 1 0 0 1 1 0 0 Sp I
© G. J. Minden 2013
Transmitting ASCII ‘e’
11
• Bit duration (time)
• Number of bits
• Parity
• Number of Stop bits
I St 1 0 1 0 0 1 1 0 0 Sp I
Parity
Stop Bits
© G. J. Minden 2013
Stellaris UART Features• A 16x12 bit receive FIFO and a 16x8 bit transmit FIFO.• Programmable baud rate generator.• Automatic generation and stripping of start, stop, and parity bits.• Line break generation and detection.• Programmable serial interface• 5, 6, 7, or 8 data bits• Even, odd, stick, or no parity bit generation and detection• 1 or 2 stop bit generation• Modem control/flow control• IrDA serial-IR (SIR) encoder/decoder.• DMA interface• 9-bit operation
12
© G. J. Minden 2013
LM3S1968 Memory Map
13
0x0000 0000
0x400F EFFF
0x4000 0000
0x2000 FFFF
0x0003 FFFF
0x2000 0000SRAM
Flash
Peripherals
64 KB
256 KB•Programs•Constants•Interrupt Vectors
•Variables•Stack
Base Addresses:
■ UART0: 0x4000.C000■ UART1: 0x4000.D000■ UART2: 0x4000.E000
Base Addr in: inc/hw_memmap.h
© G. J. Minden 2013
uartstdio.c include files
14
#include <stdarg.h>#include "inc/hw_ints.h"#include "inc/hw_memmap.h"#include "inc/hw_types.h"#include "inc/hw_uart.h"#include "driverlib/debug.h"#include "driverlib/interrupt.h"#include "driverlib/rom.h"#include "driverlib/rom_map.h"#include "driverlib/sysctl.h"#include "driverlib/uart.h"#include "utils/uartstdio.h"
Defines UART Base Addr
Defines UART Offset Addr
Defines DriverLib UART Symbols
© G. J. Minden 2013
UART Block Diagram
15
Ctrl/Status
Data
Bit Rate
Tx Data
Rx Data
SignalProc.
© G. J. Minden 2013
UART Register Map
16
Offsets defined in: inc/hw_uart.h
© G. J. Minden 2013
Example Control Register (1)
17
© G. J. Minden 2013
Example Control Register (2)
18
© G. J. Minden 2013
Example Control Register (3)
19
© G. J. Minden 2013
API Functions
20
© G. J. Minden 2013
Initialize the UART with UARTStdio
21
//*****************************************************************************//// Define the UARTStdio initization subroutine. Initialize the UART_0 on// PortA<1..0>.////*****************************************************************************
extern uint32_t UART_Initialization() {
if ( UARTStdioInitFlag == 0 ) {
// // Ensure the processor is initialized. // Processor_Initialization();
// // Enable UART0, to be used as a serial console. // SysCtlPeripheralEnable( SYSCTL_PERIPH_GPIOA ); SysCtlPeripheralEnable( SYSCTL_PERIPH_UART0 );
GPIOPinConfigure( GPIO_PA0_U0RX ); GPIOPinConfigure( GPIO_PA1_U0TX ); GPIOPinTypeUART( GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1 );
© G. J. Minden 2013
Initialize the UART with UARTStdio
22
UARTClockSourceSet( UART0_BASE, UART_CLOCK_SYSTEM );
// // Initialize UARTStdio // UARTStdioConfig( 0, 115200, g_ulSystemClock );
UARTStdioInitFlag = 1; // Set flag indicating initialization complete. }
return( 1 );}
© G. J. Minden 2013
uartstdio.c -- UARTprintf(const char *pcString, ...)
23
//*****************************************************************************////! A simple UART based printf function supporting \%c, \%d, \%p, \%s, \%u,//! \%x, and \%X.//!//! \param pcString is the format string.//! \param ... are the optional arguments, which depend on the contents of the//! format string.//!//! This function is very similar to the C library <tt>fprintf()</tt> function.//! All of its output will be sent to the UART. Only the following formatting//! characters are supported://!//! – \%c to print a character//! – \%d to print a decimal value//! – \%s to print a string//! – \%u to print an unsigned decimal value//! – \%x to print a hexadecimal value using lower case letters//! – \%X to print a hexadecimal value using lower case letters (not upper case//! letters as would typically be used)//! – \%p to print a pointer as a hexadecimal value//! – \%\% to print out a \% character//!//! For \%s, \%d, \%u, \%p, \%x, and \%X, an optional number may reside//! between the \% and the format character, which specifies the minimum number//! of characters to use for that value; if preceded by a 0 then the extra//! characters will be filled with zeros instead of spaces. For example,//! ``\%8d'' will use eight characters to print the decimal value with spaces//! added to reach eight; ``\%08d'' will use eight characters as well but will//! add zeroes instead of spaces.//!//! The type of the arguments after \e pcString must match the requirements of//! the format string. For example, if an integer was passed where a string//! was expected, an error of some kind will most likely occur.//!//! \return None.////*****************************************************************************