4: Serial I/O CET360 Microprocessor Engineering J. Sumey Introduction serial, i.e. bit-at-a-time, interfacing techniques are useful when parallel interfacing limitations become problematic distance limitations due to crosstalk cabling costs was popularized in the 1960s by teletypes and modem technologies standardized as RS-232C in 1969 by the EIA used by many I/O peripherals mice, printers, digital cameras, PDAs, GPS receivers, POS terminals (cash drawers & scanners) predecessor to USB 2 CET360 Microprocessor Engineering 1
Serial Interface Components at a minimum, a serial interface requires: 1. transmit / receive registers holds parallel data before/after serialization 2. PISO/SIPO shift registers handles serialization/deserialization 3. clock circuitry (Baud rate generator) timing signals used by shift registers referred to as UARTs or SCI Universal Asynchronous Receiver/Transmitter Serial Communication Interface (Motorola) also: COM ports on PCs 3 Example: Coldfire/S12 SCI 4 CET360 Microprocessor Engineering 2
Serial Data Transmission Issues a pair of devices communicating over a serial link must agree on a number of parameters 1. communication type 2. data coding 3. transmission (Baud) rate 4. clocking method 5. error handling 6. handshaking 5 1 Communication Type refers to how the 2 devices communicate with each other a. simplex fixed, 1-way over a single channel b. half-duplex 2-way over a single, shared (unidirectional but reversible) channel c. full-duplex 2-way over a pair of unidirectional channels 6 CET360 Microprocessor Engineering 3
2 Data Coding choice of binary code used to represent alphanumeric data ASCII American Standard Code for Information Interchange EBCDIC Extended Binary Coded Decimal Interchange Code (IBM) proprietary ex: credit card scanner also may refer to the logic levels used 1=mark, 0=space 7 Hex 0 1 2 3 4 5 6 7 0 NUL DLE 0 @ P ` p 1 SOH DC1! 1 A Q a q 2 STX DC2 2 B R b r 3 ETX DC3 # 3 C S c s 4 EOT DC4 $ 4 D T d t 5 ENQ NAK % 5 E U e u 6 ACK SYN & 6 F V f v 7 BEL ETB 7 G W g w 8 BS CAN ( 8 H X h x 9 HT EM ) 9 I Y i y A LF SUB * : J Z j z B VT ESC + ; K [ k { C FF FS, < L \ l D CR GS - = M ] m } E SO RS. > N ^ n ~ F SI US /? O _ o DEL KEY NUL = Null SOH = Start Of Header STX = Start of Transmission ETX = End of Transmission EOT = End of Text ENQ = Enquiry ACK = Acknowledge BEL = Audible Bell BS = BackSpace HT = Horizontal Tab LF = Line Feed VT = Vertical Tab FF = Form Feed CR = Carriage Return SO = Shift Out SI = Shift In DLE = Data Link Escape DCx = Device Control x NAK = Negative Acknowledge SYN = Synchronization char. ETB = End Transmission Block CAN = Cancel EM = End of Medium SUB = Substitute ESC = Escape FS = Field Separator GS = Group Separator RS = Record Separator US = Unit Separator DEL = Delete CET360 Microprocessor Engineering 4
3 Baud Rate speed at which bits (per sec.) are sent over a serial data link may (asynchronous) or may not (synchronous) include overhead bits bit time 1 baud rate also: bit rate = number of data bits per sec. common baud rates: 110, 300, 1200, 2400, 4800, 9600, 19.2k, 38.4k 9 4 Clocking Method determines how receiver is synchronized to transmitter synchronous same clock source is either sent along with or encoded within a continuous data stream, ex: Ethernet LAN bit rate = baud rate asynchronous no common clock, Tx & Rx run from separate clocks data bits are framed with Start/Stop bits Start bit: a fixed 0 bit prefixing the data bits Stop bit: a fixed 1 bit suffixing the data bits bit rate < baud rate (because of overhead) 10 CET360 Microprocessor Engineering 5
Asynchronous Format line is held in inactive mark state (1) when no data to send data bits are sent/received LSB to MSB characters may be sent back-to-back but not a requirement! ex 2 chars using 8 data, 1 stop, no parity: challenge: show waveform for the message Hi 11 5 Error Handling deals with how errors are detected and/or corrected parity each sent character may have an odd or even parity bit added to it, directly before the stop bit based on the total number of 1s in the character (including parity bit) used for simple error detection, only 50% accurate checksum better method based on 1 s complement of binary sum 8-bit performance: 255/256 accuracy, also 16-bit, 32-bit cyclic redundancy check (CRC) more sophisticated error detection mechanism based on polynomial math, 99.99%+ accurate 12 CET360 Microprocessor Engineering 6
Checksum Calculation /* * checksum8: calculate & return 8-bit checksum of data array * p: pointer to beginning of data byte "array" * len: array length in bytes * returns: 8-bit 1's complement checksum of array */ byte checksum8(void *p, int len) { byte *q = p; // so p can point to anything byte sum = 0; // running sum for (; len>0; --len) // iterate over each byte in array sum += *q++; // add byte value to running sum return (byte)~sum; // return 1's comp of sum } 13 6 - Handshaking additional control interface signals RTS/CTS Request To Send/Clear To Send DTR/DSR Data Terminal Ready/Data Set Ready DCD Data Carrier Detect RI Ring Indicate used to control / throttle the transmitter may be optional depending on hardware and software implementation 14 CET360 Microprocessor Engineering 7
Serial Communication Standards a number of industry-wide serial standards are commonly used, some have been defined by the Electronic Industries Alliance (EIA) 20/60ma current loop T.T.L. RS-232C (1969) RS-423 EIA-423 RS-422 EIA-422 RS-485 EIA-485 USB SATA 15 Serial Standards 20/60ma current loop original standard used by teletypes I=1, no I=0 once popular in (noisy) industrial settings T.T.L. simple, logic-level based +5VDC=1, 0VDC=0 good for on-pcb device-to-device transfer, otherwise not useful for much distance due to noise vulnerability 16 CET360 Microprocessor Engineering 8
RS-232C Standard defines many characteristics of a serial interface 20 signal names & functions: TxD, RxD, RTS, CTS, etc. connectors & pinouts: DB-25, DE-9, RJ-45 cable specs upto 20kbps upto 50 voltage levels 1=mark=off=-3..-25V, 0=space=on=+3..+25V notice avoidance of ground level! 17 RS-232C Standard classifies devices as being either: Data Terminal Equipment (DTE) or Data Communication Equipment (DCE) defines which pins transmit, receive, etc. requires interface drivers to convert logic levels ex: Maxim s MAX232 http://datasheets.maxim-ic.com/en/ds/max220-max249.pdf 18 CET360 Microprocessor Engineering 9
MAX232/MAX232A 5V Supply! Charge Pump Inverter Logic side EIA side 19 RS-232C Pinouts* Signal Name DB-25 DE-9 Transmitted Data TD 2 3 Received Data RD 3 2 Request To Send RTS 4 7 Clear To Send CTS 5 8 Data Set Ready DSR 6 6 Common Ground G 7 5 Carrier Detect DCD 8 1 Data Terminal Ready DTR 20 4 Ring Indicator RI 22 9 *with respect to DTE device 20 CET360 Microprocessor Engineering 10
EIA-423 Standard an improved version of RS-232 using highperformance drivers and receivers single-ended / unbalanced like RS-232 but with better performance provides up to 100 kbps and up to 4000' supports multi-drop connections up to 10 receivers on a single line 21 EIA-422 Standard similar to EIA-423 but with even better performance provides up to 10 Mbps and upto 4000' eliminates ground loop problems by transmitting bits as a voltage difference over a unidirectional wire pair has a very high common mode rejection ratio (CMRR) works better with long cable runs Note: Ethernet LANs work the same way! great for use as an RS-232 extender 22 CET360 Microprocessor Engineering 11
EIA-422 Example RS-422 4-wire network using balanced differential drivers (generators) and receivers 23 EIA-485 Standard similar to EIA-422 but allows multiple receivers and drivers (up to 32 each) to share a single 2-wire differential pair in half-duplex fashion thus, is multi-point readily supports low-cost local networking ex: PLCs on factory floors, home/building automation provides up to 35 Mbps and up to 4000' 24 CET360 Microprocessor Engineering 12
EIA-485 2-wire Network Example 25 Universal Serial Bus (USB) similar to EIA-422 (half-duplex, differential), but uses communication protocols to packetize data transfers used by current PCs to support connection of modern peripherals supports upto 127 devices, upto 16 4 different transfer speeds low speed (1.0) = 1.5Mbps full speed (1.1) = 12Mbps hi speed (2.0) = 480Mbps superspeed (3.0) = 4.8Gbps 26 CET360 Microprocessor Engineering 13
References Wikipedia Articles: http://en.wikipedia.org/wiki/current_loop http://en.wikipedia.org/wiki/rs-232 http://en.wikipedia.org/wiki/rs-485 RS-232 tutorial: http://www.radio- electronics.com/info/telecommunications_networks/rs232/rs232- serial-interface-basics-tutorial.php B&B Electronics RS-422 & RS-485 App Note: http://www.bb-elec.com/bb-elec/literature/tech/485appnote.pdf Maxim MAX-232 IC: http://datasheets.maxim-ic.com/en/ds/max220-max249.pdf 27 CET360 Microprocessor Engineering 14