LEDs to test the use of the parallel port:-
In this section I’ll detail the
construction of a piece of hardware that can be used to visualize the state of
the parallel port with some simple LEDs.
WARNING: Connecting devices to the
parallel port can harm your computer. Make sure that you are properly earthed
and your computer is turned off when connecting the device. Any problems that
arise due to undertaking these experiments is your sole responsibility.
The circuit to build is shown in
figure 3 You can also read “PC & Electronics: Connecting Your PC to the
Outside World” by Zoller as reference.
In order to use it, you must first
ensure that all hardware is correctly connected. Next, switch off the PC and
connect the device to the parallel port. The PC can then be turned on and all
device drivers related to the parallel port should be removed (for example, lp,
parport, parport_pc, etc.). The hotplug module of
the Debian Sarge distribution is particularly annoying and should be removed.
If the file /dev/parlelport does not exist, it must be created as root with the
command:
# mknod
/dev/parlelport c 61 0
Then it needs to be made readable
and writable by anybody with:
# chmod
666 /dev/parlelport
The module can now be installed, parlelport. You can check that it is effectively reserving the
input/output port addresses 0x378 with the command:
$ cat
/proc/ioports
To turn on the LEDs and check that
the system is working, execute the command:
$ echo -n
A >/dev/parlelport
This should turn on LED zero and
six, leaving all of the others off.
You can check the state of the
parallel port issuing the command:
$ cat
/dev/parlelport
Final application: flashing lights
Finally, I’ll develop a pretty
application which will make the LEDs flash in succession. To achieve this, a
program in user space needs to be written with which only one bit at a time
will be written to the /dev/parlelport device.
<lights.c> =
#include
<stdio.h>
#include
<unistd.h></p>
int
main() {
unsigned char byte,dummy;
FILE * PARLELPORT;
/* Opening the device parlelport */
PARLELPORT=fopen("/dev/parlelport","w");
/* We remove the buffer from the file i/o */
setvbuf(PARLELPORT,&dummy,_IONBF,1);
/* Initializing the variable to one */
byte=1;
/* We make an infinite loop */
while (1) {
/* Writing to the parallel port */
/* to turn on a LED */
printf("Byte value is
%d\n",byte);
fwrite(&byte,1,1,PARLELPORT);
sleep(1);
/* Updating the byte value */
byte<<=1;
if (byte == 0) byte = 1;
}
fclose(PARLELPORT);
}
It can be compiled in the usual way:
$ gcc -o
lights lights.c
and can be executed with the
command:
$ lights
The lights will flash successively
one after the other! The flashing LEDs and the Linux computer running this
program are shown in figure 4.
Conclusion
Having followed this brief tutorial
you should now be capable of writing your own complete device driver for simple
hardware like a relay board (see Appendix C), or a minimal device driver for
complex hardware. Learning to understand some of these simple concepts behind
the Linux kernel allows you, in a quick and easy way, to get up to speed with
respect to writing device drivers. And, this will bring you another step closer
to becoming a true Linux kernel developer.
Figure
4: Flashing LEDs mounted on the circuit board and the computer running Linux.
Two terminals are shown: one where the “parlelport” module is loaded and
another one where the “lights” program is run. Tux is closely following what is
going on
Bibliography
A. Rubini, J. Corbert. 2001. Linux device drivers (second edition). Ed. O’Reilly. This book is available for free on the
internet.
Jonathan Corbet. 2003/2004. Porting device drivers to the 2.6 kernel. This is a very valuable resource for porting drivers to
the new 2.6 Linux kernel and also for learning about Linux device drivers.
B. Zoller. 1998. PC &
Electronics: Connecting Your PC to the Outside World (Productivity Series).
Nowadays it is probably easier to surf the web for hardware projects like this
one.
M. Waite, S. Prata. 1990. C
Programming. Any other good book on C programming would suffice.
No comments:
Post a Comment