1. I/O Ports on a PC 9 References 2 10 11 10 License of this Document Parallel Port on a PC C Programming for Engineers 1 I/O Ports on a PC • There are 216 = 65536 I/O addesses • each of these is called an I/O port • They are accessed with the in and out Intel assembly language instructions • The I/O ports are separate from ordinary memory addresses I/O Ports on a PC Nick Urbanik nicku@nicku.org This document Licensed under GPL—see slide 11 2005 October Outline – We say, “I/O ports have a separate address space from memory addresses”. • I/O ports usually connect to registers on integrated circuits on the motherboard or on cards plugged into the motherboard 2 3 3 4 5 5 6 6 7 7 8 9 9 9 10 10 Hardware of I/O ports • We cannot connect hardware directly to the data bus on the CPU • CPU may not source or sink enough current • but the main reason is that the data bus is changing all the time – Carries instructions and other data, continuously passing back and forth • For output: need a latch (set of flip-flops) to catch the data when the output instruction is executed, and hold the data steady • For input: a tristate buffer (e.g., 571) that connects input pin to data bus at the time the input instruction is executed Hardware of Output Port Contents 1 2 I/O Ports on a PC Parallel Port in a PC 2.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.2 The Three Printer Port Base Addresses . . . . . . . . . . . . . . . . . . . . The Three Registers 3.1 The Data Port . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.2 The Status Port . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.3 The Control Port . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using the Printer Port for General I/O The pins on the 25-pin connector Permissions Performing I/O in Windows XP, 2000, NT Using Andy Eager’s wrapper for logix4u inpout32.dll 8.1 Installing Andy Eager’s wrapper . . . . . . . . . . . . . . . . . . . . . . . 8.2 Using Andy Eager’s wrapper . . . . . . . . . . . . . . . . . . . . . . . . . 8.3 Using inpout32.dll without Andy’s wrapper . . . . . . . . . . . . . . . . . 3 4 5 6 7 8 2. Parallel Port in a PC octal latch data bus 3 2.2 The Three Printer Port Base Addresses • Newer parallel ports are standardised under IEEE standard 1284 4 8 DQ 8 output port pins to the outside world – released in 1994 • The standard defines five modes of operation: XIOW Compatibility mode — sometimes called “Centronics Mode” – can send data out only – upper limit: 50 kBps to 150 kBps, depending on hardware address decoder address bus 16 AEN nibble mode Can input 4 bits at a time byte mode can input a byte at a time using parallel port’s bi-directional feature EPP mode (Enhanced Parallel Port) — Uses additional hardware to perform handshaking ECP Mode (Extended Capabilities Port) Uses DMA and FIFO buffers to move data without using I/O instructions Handshaking with a printer in Compatibility Mode To output a byte from the parallel port to the printer in compatibity mode: 1. Write the byte to the Data Port • the latch “catches” the data and holds it when the output instruction is executed to the correct address • The XIOW control line from the CPU’s control bus is activated by the output instruction • This keeps the I/O addresses separate from memory addresses even when they have the same address number Hardware of Input Port data bus 8 8 input port pins from the outside world 2. Check if the BUSY line is active • If the printer is busy, the port will not accept any data, so any data sent to the data port will be lost 3. Take the STROBE line low • Tells printer that valid data is waiting on the data pins 2–9 XIOR address bus AEN 16 address decoder 4. Put STROBE high again after about 5 microseconds. 2.2 The Three Printer Port Base Addresses The Three Printer Port Base Addresses Address 0x3bc – 0x3bf 0x378 – 0x37f 0x278 – 0x27f Notes Used for parallel ports that were incorporated into video cards, and now an option for an additional port. Does not support ECP Usual address for LPT1 (first parallel port) Usual address for LPT2 (second parallel port) • The tristate buffer connects the input pin to the data bus only when the input instruction is executed with the appropriate address • The XIOR control line from the CPU’s control bus is activated by the input instruction 2 Parallel Port in a PC 2.1 Introduction Five modes of Operation 3. The Three Registers 5 3.2 The Status Port 6 3 The Three Registers • Data port – At printer port base address – all eight bits normally output – Can input data if port has bi-directional hardware • Status port – at base address + 1 – read only • Control Port – at base address + 2 – read and write, though was originally intented as a write only port. 3.2 The Status Port The Status Port • At: Base address + 1 • Read only There are three I/O Ports pin number Bit number bit 0 bit 1 bit 2 signal name reserved reserved IRQ ERROR SLCT PE (Paper End) ACK BUSY 15 13 12 10 11 bit 3 bit 4 bit 5 bit 6 bit 7 3.1 The Data Port The Data Port • At: base address of printer port • Write only, unless the port hardware is bi-directional 3.3 The Control Port The Control Port • At: base address + 2 • Read and Write pin number 2 3 4 5 6 7 8 9 Bit number bit 0 bit 1 bit 2 bit 3 bit 4 bit 5 bit 6 bit 7 signal name D0 D1 D2 D3 D4 D5 D6 D7 pin number 1 14 16 17 Bit number bit 0 bit 1 bit 2 bit 3 bit 4 bit 5 bit 6 bit 7 signal name STROBE AUTOFEED (Auto Linefeed) INIT PRN SELECT Enable IRQ via Ack Enable Bi-Directional Port Unused Unused 4. Using the Printer Port for General I/O 7 6. Permissions 8 4 Using the Printer Port for General I/O 13 12 25 24 11 23 10 22 9 21 8 20 7 19 6 18 5 17 4 16 3 15 2 14 1 Using the Printer Port for I/O • Here, we use the printer port in compatibility mode • In this mode, the three ports are not available as general purpose 8-bit input/output ports – They are set up to talk to a printer – But you can still use these ports for many purposes Signals and pin numbers for general purpose I/O Port Signal Name DB25 pin number Data base D0 D1 D2 D3 D4 D5 D6 D7 ERROR SLCT PE ACK BUSY STROBE AUTOFEED INIT PRN SELECT GND 2 3 4 5 6 7 8 9 15 13 12 10 11 1 14 16 17 18–25 View of female DB25 connector Pin Numbers on Parallel Port DB25 Pin No (D-Type Pin No SPP Signal 25) (Centronics) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 – 25 1 2 3 4 5 6 7 8 9 10 11 12 13 14 32 31 36 19 – 30 STROBE D0 D1 D2 D3 D4 D5 D6 D7 ACK BUSY PE (PaperEnd) SELECT AUTOFEED (Auto-Linefeed) ERROR / Fault INIT PRN SELECT Select-In Ground Direction (In or Out) In/Out Out Out Out Out Out Out Out Out In In In In In/Out In In/Out In/Out GND Register Control Data Data Data Data Data Data Data Data Status Status Status Status Control Status Control Control Inv? Yes Comments All outputs latched Yes Status bit 3 base + 1 bit 4 bit 5 bit 6 bit 7 Control bit 0 base + 2 bit 1 bit 2 bit 3 input input input input inverted input inverted output inverted output output inverted output Yes Yes 6 5 The pins on the 25-pin connector • This views the female connector • i.e., on the back of the computer Permissions • Normally, to access I/O ports requires administrator priveleges • . . . but it is a bad idea to do everything as the root or administrative user – A small mistake can stop the system from functioning correctly – In Windows XP/2000/NT, additionally, special unsupported software is required. Do not run your programs as root/Administrator Pin numbers on DB25 Connector 7. Performing I/O in Windows XP, 2000, NT 9 8.2 Using Andy Eager’s wrapper 10 – Linux provides a system call ioperm() that allows the root user to grant normal user access to particular ports – The ports must be at port address 0x3ff or below 8.2 Using Andy Eager’s wrapper Using Andy Eager’s wrapper • See the program ledscan.c in 7 Performing I/O in Windows XP, 2000, NT • Port I/O on Windows XP, Windows 2000, Windows NT is a complex, barely supported mess. • Use Linux if you want something simple, standardised and supported: http://linuxgazette.net/112/radcliffe.html • Several people have built device drivers to work around the limitations of Windows: – inpout32.dll: http://www.logix4u.net/inpout32.htm – PortTalk: http://www.beyondlogic.org/porttalk/porttalk.htm – io.dll: http://www.geekhideout.com/iodll.shtml – giveio.sys: http://www.physik.rwth-aachen.de/group/IIIphys/CMS/tracker/e – directio: http://www.direct-io.com/ • None of these are Open Source, but inpout32.dll seems to be best supported and have the most open license, so we will use that. http://www.linuxivr.com/c/week1/ioports.zip — use this as a model to see how to perform I/O • Compile your program g++ -Wall -lioports -o program with the program .cpp command: Performing I/O in Windows XP, 2000, NT 8.3 Using inpout32.dll without Andy’s wrapper Using inpout32.dll without Andy’s wrapper • This could (potentially) give better performance if you initialise the library once at the beginning and free the library once after all I/O is finished – However, Andy says the difference in speed is not detectable See the test program http://www.hytherion.com/beattidp/comput/pport/test2.c, and also the source to Andy’s wrapper at http://www.linuxivr.com/c/week1/install-io.html, and use them as a model for your program. 9 References References — Web 8 Using Andy Eager’s wrapper for logix4u inpout32.dll References [1] logix4u.net. Inpout32.dll for WIN NT/2000/XP • http://www.logix4u.net/inpout32.htm [2] Andrew Eager. Installing the logix4u http://linuxivr.com/c/week1/install-io.html IO — logix4u. interface. 8.1 Installing Andy Eager’s wrapper Installing Andy Eager’s wrapper for logix4u inpout32.dll Note: this is for use with Microsoft Windows. The procedure with Linux is different, simpler and faster: see the references. • Download Andy’s handy package from http://www.linuxivr.com/c/week1/ioport • Unzip this into a temporary directory • execute install.bat from a command prompt in that directory as the Administrator [3] logix4u.Parallel port Interfacing Tutorial.http://www.logix4u.net/parallelport [4] Joe D. Reeder. Controlling http://learn-c.com/ The Real World With Computers References 11 [5] Riku Saikkonen. Linux I/O port programming mini-HOWTO http://www.tldp.org/HOWTO/IO-Port-Programming.html [6] P. J. Radcliffe. Linux: A Clear Winner for Hardware I/O. Linux Gazette, Issue 112, March 2005. http://linuxgazette.net/112/radcliffe.html [7] David Chong and Philip Chong Linux Analog to Digital Converter. Linux Gazette, Issue 118, September 2005. http://linuxgazette.net/118/chong.html [8] Craig Peacock Interfacing the Standard Parallel Port. http://www.beyondlogic.org/spp/parallel.htm [9] Jan Axelson. The PC’s Parallel Port. http://www.lvr.com/parport.htm References — Books References [1] Steve Oualline. Practical C Programming. O’Reilly, 1993. [2] Paul Davies. The Indispensable Guide to C with Engineering Applications AddisonWesley, 1995. [3] Tom Adamson and James L. Antonakos and Kenneth C. Mansfield Jr. Structured C for Engineering and Technology, Third Edition. Prentice Hall, 1998. [4] Brian W. Kernighan and Dennis M. Ritchie. The C Programming Language. Prentice Hall, 1988. 10 License of this Document License covering this document Copyright c 2005, 2006 Nick Urbanik You can redistribute modified or unmodified copies of this document provided that this copyright notice and this permission notice are preserved on all copies under the terms of the GNU General Public License as published by the Free Software Foundation — either version 2 of the License or (at your option) any later version.