UniversalAsynchronous Receiver/Transmitter

UART

ECEn/CS 224 20 UART © 2003-2006 Page 1 BYU WhyuseaUART?

• AUARTmaybeusedwhen: – Highspeedisnotrequired – Acheapcommunicationlinebetween two devices isrequired • Asynchronousserialcommunicationisverycheap – Requiresatransmitterand/orreceiver – Singlewireforeachdirection(plusgroundwire) – Relativelysimplehardware – Asynchronousbecausethe • PCdevicessuchasmiceandmodemsusedtooften beasynchronousserialdevices

ECEn/CS 224 20 UART © 2003-2006 Page 2 BYU UARTUses

• PCserialportisaUART! • Serializesdatatobesentoverserialcable – Deserializesreceiveddata

Serial Serial Cable Port

Serial Cable

Device

ECEn/CS 224 20 UART © 2003-2006 Page 3 BYU UARTUses

• Communicationbetweendistantcomputers – Serializesdatatobesenttomodem – Deserializesdatareceivedfrommodem

Serial Cable Phone Line

Phone Modem Serial Line Cable

Modem

ECEn/CS 224 20 UART © 2003-2006 Page 4 BYU UARTUses

• Usedtobecommonlyusedforinternetaccess

Phone Line InternetInternet

Phone Serial Line Cable

Modem

ECEn/CS 224 20 UART © 2003-2006 Page 5 BYU UARTUses

• Usedtobeusedformainframeaccess – Amainframecouldhavedozensofserialports

Mainframe

Serial Cables

Terminal Terminal Terminal Terminal

ECEn/CS 224 20 UART © 2003-2006 Page 6 BYU UARTUses

• Becomingmuchlesscommon • Largelybeenreplacedbyfaster,more sophisticatedinterfaces – PCs:USB (peripherals) ,Ethernet (networking) – Chiptochip:I2C,SPI • Stillusedtodaywhensimplelowspeed communicationisneeded

ECEn/CS 224 20 UART © 2003-2006 Page 7 BYU UARTFunctions

• Outbounddata – Convertfromparalleltoserial – Addstartandstopdelineators() – Addparity

• Inbounddata – Convertfromserialtoparallel – Removestartandstopdelineators(bits) – Checkandremoveparitybit

ECEn/CS 224 20 UART © 2003-2006 Page 8 BYU UARTCharacterTransmission

• Belowisatimingdiagramforthetransmission ofasingle • Usesasinglewirefortransmission • Eachblockrepresentsabitthatcanbea mark (logic‘1’,high)or space (logic‘0’,low)

1bittime mark space Time

ECEn/CS 224 20 UART © 2003-2006 Page 9 BYU UARTCharacterTransmission

• Eachbithasafixedtimedurationdetermined bythetransmissionrate • Example:a1200bps( bitspersecond )UARTwill havea1/1200sorabout833.3sbitwidth

1bittime

ECEn/CS 224 20 UART © 2003-2006 Page 10 BYU UARTCharacterTransmission

• The start bit marksthebeginningofanew word • Whendetected,thereceiversynchronizes withthenewdatastream

StartBit

ECEn/CS 224 20 UART © 2003-2006 Page 11 BYU UARTCharacterTransmission

• Nextfollowsthe data bits (7or8) • Theleastsignificantbitissentfirst

7DataBits

ECEn/CS 224 20 UART © 2003-2006 Page 12 BYU UARTCharacterTransmission

• The parity bit isaddedtomakethenumber of1’seven(evenparity)orodd(oddparity) • Thisbitcanbeusedbythereceivertocheck fortransmissionerrors

ParityBit

ECEn/CS 224 20 UART © 2003-2006 Page 13 BYU UARTCharacterTransmission

• The stop bit markstheendoftransmission • Receivercheckstomakesureitis‘1’ • Separatesonewordfromthestartbitofthe nextword

StopBit

ECEn/CS 224 20 UART © 2003-2006 Page 14 BYU UARTCharacterTransmission

• Intheconfigurationshown,ittakes10bitsto send7bitsofdata

Startbit 7databits Paritybit Stopbit

ECEn/CS 224 20 UART © 2003-2006 Page 15 BYU UARTTransmissionExample

• SendtheASCIIletter‘W’ (1010111)

Lineidling Startbit Paritybit (oddparity) Stopbit

Mark 1 1 1 0 1 0 1 0 Space Lineidlingagain 7databits– Leastsignificantbitfirst

ECEn/CS 224 20 UART © 2003-2006 Page 16 BYU UARTCharacterReception

Startbitsaysacharacteriscoming, receiverresetsitstimers

Receivershouldsampleinmiddleofbits

Mark

Space

Receiverusesatimer(counter)totimewhenitsamples. Transmissionrate(i.e.,bitwidth)mustbeknown!

ECEn/CS 224 20 UART © 2003-2006 Page 17 BYU UARTCharacterReception

Ifreceiversamplestooquickly,seewhathappens…

Mark

Space

ECEn/CS 224 20 UART © 2003-2006 Page 18 BYU UARTCharacterReception

Ifreceiversamplestooslowly,seewhathappens…

Mark

Space

Receiverresynchronizesoneverystartbit. Onlyhastobeaccurateenoughtoread9bits.

ECEn/CS 224 20 UART © 2003-2006 Page 19 BYU UARTCharacterReception

• Receiveralsoverifiesthatstopbitis‘1’ – Ifnot,reports“framingerror” tohostsystem

• Newstartbitcanappearimmediatelyafter stopbit – Receiverwillresynchronizeoneachstartbit

ECEn/CS 224 20 UART © 2003-2006 Page 20 BYU UARTOptions

• UARTs usuallyhaveprogrammableoptions: – Data: 7or8bits – Parity: even,odd,none,mark,space – Stop bits: 1,1.5,2 – Baud rate: 300,1200,2400,4800,9600, 19.2K,38.4k,57.6k,115.2k…

ECEn/CS 224 20 UART © 2003-2006 Page 21 BYU UARTOptions

• BaudRate – The“symbolrate” ofthetransmissionsystem – ForaUART,sameasthenumberofbitsper second(bps) – Eachbitis1/(rate)secondswide • Example: Notthedata throughputrate! – 9600baud  9600Hz – 9600bitspersecond(bps) – Eachbitis1/(9600Hz) ≈ 104.17 slong

ECEn/CS 224 20 UART © 2003-2006 Page 22 BYU UARTThroughput

• DataThroughputExample – Assume19200baud,8databits,noparity,1stopbit • 19200baud  19.2kbps • 1startbit+8databits+1stopbit  10bits – Ittakes10bitstosend8bits(1byte)ofdata – 19.2kbps• 8/10= 15.36 kbps • HowmanyKB(kilobytes)persecondisthis? – 1byte=8bits – 1KB=1,024 – So,1KB=1,024bytes• 8bits/byte=8,192bits – Finally,15,360bps• 1KB/8,192bits= 1.875 KB/s

ECEn/CS 224 20 UART © 2003-2006 Page 23 BYU Let’sDesignaUARTTransmitter!

Specifications

• Parameters:300baud,7databits,1stopbit,evenoroddparity

• Inputs: – Din[6:0] :7bitparalleldatainput – Send :instructstransmittertoinitiateatransmission – ParitySelect :selectsevenparity( ParitySelect=0 )oroddparity (ParitySelect=1 )

• Outputs: – Dout :serialdataoutput – Busy :tellshostbusysendingacharacter

ECEn/CS 224 20 UART © 2003-2006 Page 24 BYU SystemDiagram

Tohost system

Send Busy UART Dout ParitySelect Transmitter

Toserial Din 7 cable

ECEn/CS 224 20 UART © 2003-2006 Page 25 BYU Transmitter/SystemHandshaking

• Systemasserts Send andholdsithighwhenitwantsto sendabyte • UARTassertsBusysignalinresponse • WhenUARThasfinishedtransfer,UARTdeasserts Busy signal • Systemdeasserts Send signal

Send

Busy

ECEn/CS 224 20 UART © 2003-2006 Page 26 BYU TransmitterBlockDiagram

Tohost system NextBit 300HZ ResetTimer Timer Send Count10 Transmitter Increment Mod10 Busy State Counter Machine ResetCounter Shift

ParitySelect Dout Parity Load Generator Shift ParityBit Register Toserial cable Din 7

ECEn/CS 224 20 UART © 2003-2006 Page 27 BYU TheTimingGenerator

NextBit SystemClock 300Hz ResetTimer Timer

• Dividessystemclockdownto300Hz • Outputis NextBit signaltostatemachine – Goeshighforonesystemclockcycle300timesa second

• SimplyaMod( fclk /300)resetable counterwhere NextBit istherolloversignal • MoresophisticatedUARTs haveprogrammabletiming generatorsfordifferentbaudrates

ECEn/CS 224 20 UART © 2003-2006 Page 28 BYU TheMod10Counter

Count10 SystemClock Increment Mod10 ResetCounter Counter

• Resetsto0oncommandfromstatemachine • Incrementsoncommandfromstatemachine • Countsfrom0to9,thenrollsoverto0 • Tellsstatemachinewhenit’sgoingtorollover from9backto0(signal Count10 )

ECEn/CS 224 20 UART © 2003-2006 Page 29 BYU Mod10CounterinVerilog module mod10 (clk, reset, increment, count10); input clk, reset, increment; output reg count10;

wire [3:0] ns, q, qPlus1;

assign qPlus1 = (q == 9) ? 0 : q+1; assign ns = (reset) ? 0 : (increment) ? qPlus1 : q; regn #(4) R0 (clk, ns, q); // Assume this submodule exists

assign count10 = increment & (q == 9); endmodule

ThiscouldalsobewrittenusingbehaviorVerilog(analwaysblock)

ECEn/CS 224 20 UART © 2003-2006 Page 30 BYU TheParityGenerator

ParitySelect ParityBit Din Parity 7 Generator

• Combinationalcircuit • Generates ParityBit accordingtovalueof Din[6:0] and ParitySelect input

ECEn/CS 224 20 UART © 2003-2006 Page 31 BYU TheParityGenerator

• Thevalueof ParityBit isthebitneededto makethenumberof1’seven(ifevenparity) orodd(ifoddparity)

Even Parity Odd Parity (ParitySelect =0) (ParitySelect =1) Evennumberof‘1’s ParityBit =0 ParityBit =1 Oddnumberof‘1’s ParityBit =1 ParityBit =0

ECEn/CS 224 20 UART © 2003-2006 Page 32 BYU An8BitParityGenerator

Din[0] Odd/Even# Din[1] Din[2] Din[3] ParityBit Din[4] Din[5] Din[6] Willbe‘0’ if Din has evennumberof1’s, Din[7] 0ifoddnumber.

For7bitparity,tieDin[7]toa‘0’

ECEn/CS 224 20 UART © 2003-2006 Page 33 BYU 7bitParityGeneratorinVerilog

module parity_gen (data, oddeven, parity); input [6:0] data; input oddeven; output parity;

assign parity = (^data) ^ oddeven; endmodule

ReductionXOR operator

ECEn/CS 224 20 UART © 2003-2006 Page 34 BYU TheShiftRegister

• StandardParallelIn/SerialOut(PISO)shift register • Has4operations: – Donothing – Loadparalleldatafrom Din – Shiftright – Reset

ECEn/CS 224 20 UART © 2003-2006 Page 35 BYU TheShiftRegister

• Makeita9bitregister • Whenitloads: – Haveitload‘0’ forthestartbitontheright(LSB) – Haveitloadtheparitybitontheleft(MSB) – Haveitload7databitsinthemiddle • Whenitshifts: – Haveitshift‘1’ intotheleftsoastopbitissentattheend

‘1’ Dout P D6 D5 D4 D3 D2 D1 D0 ‘0’ Shift Register • Whenitresets: – Haveitloadall1’ssothatitsdefaultoutputisa‘1’ (lineidle value)

ECEn/CS 224 20 UART © 2003-2006 Page 36 BYU 9bitShiftRegisterModule

{Parity,7databits} module shiftReg (clk, loadData, load, shift, sout); input clk, load, shift; input [7:0] loadData; output sout; wire [8:0] ns, q; “Reset” assign ns = (load & shift) ? 9’b111111111 : load ? {loadData, 1’b0} : shift ? {1’b1, q[8:1]} : q; reg #(9) R0(clk, ns, q); assign sout = q[0]; endmodule

ECEn/CS 224 20 UART © 2003-2006 Page 37 BYU TransmitterFSM

Send’ Reset

Idle Send Load Load Busy Load Send’ Shift ResetCounter ResetTimer

Send Wait Busy Count NextBit’ NextBit Count10 Besuretochoosestate Shift Count10’ encodingsanduselogic minimizationthatensures Shift Busy signalwillhaveno Increment Busy hazards…

ECEn/CS 224 20 UART © 2003-2006 Page 38 BYU TheReceiver

• Leftforyouasanexercise! • ReceiverIssues: 1. Howtosamplethemiddleofbitperiods? 2. Howdoyoucheckifparityiscorrect? 3. Whatdoyoudoonaframingerror? 4. Whatdoyoudoonaparityerror? 5. Handshakingwithrestofsystem?

ECEn/CS 224 20 UART © 2003-2006 Page 39 BYU