old-www/HOWTO/IO-Port-Programming-9.html

68 lines
1.7 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<META NAME="GENERATOR" CONTENT="SGML-Tools 1.0.9">
<TITLE>Linux I/O port programming mini-HOWTO: Example code</TITLE>
<LINK HREF="IO-Port-Programming-10.html" REL=next>
<LINK HREF="IO-Port-Programming-8.html" REL=previous>
<LINK HREF="IO-Port-Programming.html#toc9" REL=contents>
</HEAD>
<BODY>
<A HREF="IO-Port-Programming-10.html">Next</A>
<A HREF="IO-Port-Programming-8.html">Previous</A>
<A HREF="IO-Port-Programming.html#toc9">Contents</A>
<HR>
<H2><A NAME="s9">9. Example code</A></H2>
<P>Here's a piece of simple example code for I/O port access:
<P>
<BLOCKQUOTE><CODE>
<HR>
<PRE>
/*
* example.c: very simple example of port I/O
*
* This code does nothing useful, just a port write, a pause,
* and a port read. Compile with `gcc -O2 -o example example.c',
* and run as root with `./example'.
*/
#include &lt;stdio.h>
#include &lt;unistd.h>
#include &lt;asm/io.h>
#define BASEPORT 0x378 /* lp1 */
int main()
{
/* Get access to the ports */
if (ioperm(BASEPORT, 3, 1)) {perror("ioperm"); exit(1);}
/* Set the data signals (D0-7) of the port to all low (0) */
outb(0, BASEPORT);
/* Sleep for a while (100 ms) */
usleep(100000);
/* Read from the status port (BASE+1) and display the result */
printf("status: %d\n", inb(BASEPORT + 1));
/* We don't need the ports anymore */
if (ioperm(BASEPORT, 3, 0)) {perror("ioperm"); exit(1);}
exit(0);
}
/* end of example.c */
</PRE>
<HR>
</CODE></BLOCKQUOTE>
<P>
<P>
<HR>
<A HREF="IO-Port-Programming-10.html">Next</A>
<A HREF="IO-Port-Programming-8.html">Previous</A>
<A HREF="IO-Port-Programming.html#toc9">Contents</A>
</BODY>
</HTML>