Standard Parallel Port Control with Port IO

   


Standard Parallel Port Control with Port I/O

I/O port operations with Linux couldn't be simpler. There's one command for input inb(address) and one for output outb(value, address). C macro expansion implements these two commands without involving any libraries. Use of inb and outb does require a couple extra lines of code, though. The ioperm command requests and relinquishes port access from the kernel.1 Writing a byte the parallel port could be as simple as these three lines of code

 if (ioperm(0x378, 3, 1)) exit(1); outb(argv[1][0], 0x378); if (ioperm(0x378, 3, 0)) exit(1); 

Likewise, reading a byte is simple. You can use the following code to read the parallel port's status lines:

 if (ioperm(0x378, 3, 1)) exit(1); putc(inb(0x379)); if (ioperm(0x378, 3, 0)) exit(1); 

This code may output the ASCII value 0 and is likely to affect bash scripts. Simple additions to these two code fragments allow for lift operation monitoring and control of snow-making equipment.

Monitoring Lift Operation Using Port I/O

Using the inb and outb port I/O calls, the current status of a lift can be read using the interface circuit. liftoperationmonitor.c, shown in Listing 7.1, performs an input operation by configuring the parallel port data port for input, asserting the input buffer's enable signal, INPUT_ENABLE and reading the data lines D7 D0. It outputs a script-friendly version of the data port bits as eight ASCII characters, either 1s or 0s.

TIP

If you experience problems controlling your PC's parallel port, verify its configuration by using your PC's BIOS setup program. For the code in this chapter, you should configure your printer port at address 0x378 and SPP mode (sometimes called output only; bidirectional mode will also work). Also, use the gdb debugger to step through your code while you monitor voltage changes on the parallel port pins with your voltmeter. Verify that the parallel port is functioning correctly one step at a time.


Listing 7.1 The liftoperationmonitor.c Program
 /*  * liftoperationmonitor v0.1 9/25/01  * www.embeddedlinuxinterfacing.com  *  * The original location of this code is  * http://www.embeddedlinuxinterfacing.com/chapters/07/liftoperationmonitor.c  *  *  * Copyright (C) 2001 by Craig Hollabaugh  *  * This program is free software; you can redistribute it and/or modify  * it under the terms of the GNU Library General Public License as  * published by the Free Software Foundation; either version 2 of the  * License, or (at your option) any later version.  *  * This program is distributed in the hope that it will be useful, but  * WITHOUT ANY WARRANTY; without even the implied warranty of  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU  * Library General Public License for more details.  *  * You should have received a copy of the GNU Library General Public  * License along with this program; if not, write to the  * Free Software Foundation, Inc.,  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA  */ /* liftoperationmonitor  * liftoperationmonitor uses inb and outb to control an interface  * circuit connected the PC parallel printer port. The port's  * control port controls the input/output operation on the  * interface circuit's data bus.  */ /* remember to compile with -O2 for proper inb/outb macro expansion gcc -O2 -o liftoperationmonitor liftoperationmonitor.c */ #include <asm/io.h> #define SPPDATAPORT    0x378 #define SPPSTATUSPORT  (SPPDATAPORT + 1) #define SPPCONTROLPORT (SPPDATAPORT + 2) /* these are the control port bit defs */ #define OUTPUTENABLE 0x02 #define OUTPUTLATCH  0x04 #define INPUTENABLE  0x08 #define SPPPORTREAD  0x20 int main(void) {   unsigned char v,i; /* get permission from the OS to use the  * data, status and control ports*/   if (ioperm(SPPDATAPORT, 3, 1))   {     perror("ioperm");     exit(1);   } /* this asserts three items.  * 1. SSPPORTREAD, this configures the bidirectional data port as input  * 2. INPUTENABLE, this enables the input buffer to drive the data bus  * 3. OUTPUTENABLE, enable the output latch, driving the output rack  *                  if you don't assert this, the output modules will  *                  turn off  */   outb(SPPPORTREAD |  INPUTENABLE | OUTPUTENABLE,SPPCONTROLPORT); /* The input buffer is now driving the bus, so do a read */   v = inb(SPPSTATUSPORT); /* Deassert SPPORTREAD and INPUTENABLE.  * Use OUTPUTENABLE to keep output latch enabled  */   outb(OUTPUTENABLE  ,SPPCONTROLPORT); /* loop through the bits in v and output 8 0s or 1s, like 01000001  * MSB, D7 is output first  */   for (i = 0; i < 8; i++)   {     if ( v & 0x80 )       putchar('1');     else       putchar('0');     v <<= 1; /* shift bits */   } /* let OS know we're done with the port */   if (ioperm(SPPDATAPORT, 3, 0))   {     perror("ioperm");     exit(1);   } } 

Snow-Making Control Using Port I/O

By using only outb port I/O calls and the interface circuit, the output modules can be turned on or off. snowmakingcontrol.c, shown in Listing 7.2, performs an output operation by outputting a byte on the parallel port data bus then toggling the output latch signal, OUTPUT_LATCH. The latch then transfers the data lines, D7 D0, to its output, which turns on or off modules on the output module rack.

Listing 7.2 The snowmakingcontrol.c Program
 /*  * snowmakingcontrol v0.1 9/25/01  * www.embeddedlinuxinterfacing.com  *  * The original location of this code is  * http://www.embeddedlinuxinterfacing.com/chapters/07/snowmakingcontrol.c  *  *  * Copyright (C) 2001 by Craig Hollabaugh  *  * This program is free software; you can redistribute it and/or modify  * it under the terms of the GNU Library General Public License as  * published by the Free Software Foundation; either version 2 of the  * License, or (at your option) any later version.  *  * This program is distributed in the hope that it will be useful, but  * WITHOUT ANY WARRANTY; without even the implied warranty of  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU  * Library General Public License for more details.  *  * You should have received a copy of the GNU Library General Public  * License along with this program; if not, write to the  * Free Software Foundation, Inc.,  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA  */ /* snowmakingcontrol  * snowmakingcontrol uses outb system calls to control an interface  * circuit connected the PC parallel printer port. The port's  * control port drives the interface circuit's output latch signals.  */ /* remember to compile with -O2 for proper inb/outb macro expansion gcc -O2 -o snowmakingcontrol snowmakingcontrol.c */ #include <asm/io.h> #define SPPDATAPORT     0x378 #define SPPSTATUSPORT  (SPPDATAPORT + 1) #define SPPCONTROLPORT (SPPDATAPORT + 2) /* these are the control port bit defs */ #define OUTPUTENABLE 0x02 #define OUTPUTLATCH  0x04 #define INPUTENABLE  0x08 #define SPPPORTREAD  0x20 int main(int argc, char *argv[]) { /* get permission from the OS to use the  * data, status and control ports*/   if (ioperm(SPPDATAPORT, 3, 1))   {     perror("ioperm");     exit(1);   } /* Use OUTPUTENABLE to keep output latch enabled */   outb(OUTPUTENABLE               ,SPPCONTROLPORT); /* put the first char of command line argument 1 on the data bus */   outb(argv[1][0],SPPDATAPORT); /* assert OUTPUTLATCH, this latches the data bus to the latch's  * outputs, driving the output module rack */   outb(OUTPUTENABLE | OUTPUTLATCH ,SPPCONTROLPORT); //latch data /* Use OUTPUTENABLE to keep output latch enabled */   outb(OUTPUTENABLE               ,SPPCONTROLPORT); /* let OS know we're done with the port */   if (ioperm(SPPDATAPORT, 3, 0))   {     perror("ioperm");     exit(1);   } } 

SPP control with port I/O is very simple. The inb and outb commands offer an approach to control the parallel port registers and signals. With the interface circuit shown in Figure 7.3, the engineers can monitor lift operations and control snow-making equipment. There are three drawbacks to this port I/O approach. Developers must always assert the circuit's OUTPUT_ENABLE line to continually drive the output modules. This is a minor nuisance. The second, more difficult, drawback deals with the value of the latch output driving the output modules: There is no electrical mechanism to determine these bit values, other than to keep track of the last byte value written. If multiple bash scripts call the snowmakingcontrol program, somehow these scripts need to store and recall the last written byte value for their bit manipulation. The third drawback deals with use of the ioperm system call. Only the root user can call ioperm. This can affect non-root users' bash access to the parallel port. Accessing the parallel port using inb and outb system calls, with these three drawbacks, is not ideal. Improper control of snow-making equipment could lead to slope closures, operations failure, and expensive repair.

The parallel port control approach described in the next section using the parallel port device driver (ppdev) has the same two drawbacks as the port I/O approach. In the following section you'll implement snowmakingcontrol.c functionality using ppdev instead of outb. It's included here for Linux control completeness but isn't considered a viable option for snow-making control. The custom device driver, developed later in this chapter, has no drawbacks and implements a robust and eloquent solution for lift monitoring and snow-making control.


       
    Top


    Embedded LinuxR. Hardware, Software, and Interfacing
    Embedded LinuxR. Hardware, Software, and Interfacing
    ISBN: N/A
    EAN: N/A
    Year: 2001
    Pages: 103

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net