Parallel Port Pin Programming Library for Linux

Parallel Port Pin Programming Library for Linux

If you want a higher level approach to parallel port controlling than low level directly writign to control registers apprach, there are also tools for this available. Parapin is an easy to use parallel port pin programming library. You can find this software athttp://www.epanorama.net/counter.php?url=http://parapin.sourceforge.net/. Parapin makes it easy to write C code under Linux that controls individual pins on a PC parallel port. This kind of control is very useful for electronics projects that use the PC’s parallel port as a generic digital I/O interface.

Parapin goes to great lengths to insulate the programmer from the somewhat complex parallel port programming interface provided by the PC hardware, making it easy to use the parallel port for digital I/O.

Parapin provides a simple interface that lets programs use pins of the PC parallel port as digital inputs or outputs. Parapin makes it easy to write C code under Linux that controls individual pins on a PC parallel port. You can assert high or low TTL logic values on output pins or poll the state of input pins. This kind of control is very useful for electronics projects that use the PC’s parallel port as a generic digital I/O interface.

Parapin has two personalities: it can either be used as a user-space C library, or linked as part of a Linux kernel module. There is also a device driver that provides access to the kernel module from user-space, allowing the administrator to use filesystem permissions to control access to the port. Parapin was written with efficiency in mind, so that Parapin can be used in time-sensitive applications.

The user-space version of Parapin is compiled and linked very much like any other C library. If you installed Parapin on your system using “make install”, the library (libparapin.a) was probably installed in /usr/local/lib. The header file with the library’s function prototypes and other definitions, parapin.h, is probably also in /usr/local/include. To use the library, first make sure to #include parapin.h in your C source file. When linking, add -lparapin along with any other libraries you might be using. C library initialization is performed using the function

        int pin_init_user(int lp_base);

whose single argument, lp_base, specifies the base I/O address of the parallel port being controlled. In both the userspace C library version, and the kernel version, pins are configured using one of the following three functions:

        void pin_input_mode(int pins);
        void pin_output_mode(int pins);
        void pin_mode(int pins, int mode);

The pins argument of all three functions accepts the LP_PINnn constants Once Parapin has been initialized (Section 5), and pins have been configured as output pins (Section 6), values can be asserted on those pins using the following functions:

        void set_pin(int pins);
        void clear_pin(int pins);
        void change_pin(int pins, int state);

value being asserted by the “far end” can be queried using the following function. Please note that pin state may only be queried only if they are input pins (by hardware or set as input).

        int pin_is_set(int pins);

If you would like to use the kernel version of parapin from a userspace program without writing your own hardware-specific driver, you can load the parapindriver module after loading kparapin. This device driver exposes the functionality of kparapin through a normal character-device interface (except for interrupt handling). The primary advantages are that access to the parallel port functionality of parapin may be controlled through /dev filesystem permissions, and all interaction is done through standard device-oriented system calls such as open(), close(), and ioctl(). When building a userspace program that will make use of the parapindriver interface to kparapin, you must include parapindriver.h. This header file defines the device-specific ioctl commands used to communicate with the device driver. It also includes parapin.h, so your program can make use of all the “LP_*” constants. The parapindriver system calls take arguments using these constants and pass them unchanged down to the kparapin routines.

Once parapindriver is successfully loaded, and a corresponding /dev entry is in place, initialization and shutdown of the parallel port are easy. To initialize the parapin system, just call open(2) on the /dev entry as you would any other device file: int device; device = open(“/dev/”, 0); if (device < 0) { fprintf(stderr, “device open failed\n”); exit(-1); } The device name is typically parport0, parport1, parport2, etc… (as found in Fedora Core 4 Linux system). To shutdown the parapin system you simply have to call close(2):

       close(device);

When using the parapindriver device interface, all functionality related to pins on the parallel port is invoked via the ioctl(2) system call. The ioctl commands for parapindriver are defined in the parapindriver.h header file. The two used to set pins to be either output pins or input pins are “PPDRV_IOC_PINMODE_OUT” and “PPDRV_IOC_PINMODE_IN”, respectively. Examples:

        ioctl(device, PPDRV_IOC_PINMODE_OUT, LP_PIN01 | LP_PIN02);
        ioctl(device, PPDRV_IOC_PINMODE_IN, LP_PIN11);

Setting pin state through the device driver interface follows all the same rules as described above. The ioctl commands used are “PPDRV_IOC_PINSET” and “PPDRV_IOC_PINCLEAR”.

        ioctl(device, PPDRV_IOC_PINSET, LP_PIN01 | LP_PIN02);
        ioctl(device, PPDRV_IOC_PINCLEAR, LP_PIN01);

Querying pin state through the device driver interface follows all the same rules as described above. The ioctl command used is “PPDRV_IOC_PINGET”. Please note that pin state may only be queried only if they are input pins (by hardware or set as input).

        int value;
        value = ioctl(device, PPDRV_IOC_PINGET, DATA);

Arguments to these ioctl calls use exactly the same constants as are used by the userspace and kernel versions of parapin itself. Return values from these ioctl calls are the same as those defined for the corresponding functions in kparapin (plus value -ENOTTY indicates that an invalid ioctl command value was passed down).

The parallel port pins are accessed with constants in form of LP_PIN plus parallel port pin number. For example LP_PIN02 refers to parallel port pin 2 that is D0 data out pin. Programming tip: Usually, it is most convenient to use #define statements to give pins logical names that have meaning in the context of your application. Example:

        #include "parapin.h"

        #define VCC LP_PIN02
        #define CS  LP_PIN03
        #define CLK LP_PIN04
        #define D0  LP_PIN10 /* input pin */
        ...
        clear_pin(CS);  /* pull Chip Select low, tell it to acquire */
        ...
        set_pin(CLK);   /* clock it */
        clear_pin(CLK);

Parapin also supports interrupts through parallel port. Currently, Parapin only supports a single parallel port at a time. Many new Linux distributions include parport kernel device driver already integrated to the system ready to use.

Parapin docmentation is available at http://www.epanorama.net/counter.php?url=http://parapin.sourceforge.net/doc/parapin.html. Most of the parapin description give above ade edited form documentation found at that address.

Programming I/O

ใส่ความเห็น

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / เปลี่ยนแปลง )

Twitter picture

You are commenting using your Twitter account. Log Out / เปลี่ยนแปลง )

Facebook photo

You are commenting using your Facebook account. Log Out / เปลี่ยนแปลง )

Google+ photo

You are commenting using your Google+ account. Log Out / เปลี่ยนแปลง )

Connecting to %s

%d bloggers like this: