Hello again,
I am hoping someone will be able to help me out here.
I am currently in week 14 of a basic C course and a few weeks ago we built little circuit boards with LEDs and a 7-segment display on them. These boards connect to the parallel port on the box and we write programs to do funky things with the LEDs (eg. convert binary to decimal, display the output of calculations, etc).
The rest of the class is using Windows and Dev-C while I am using Linux (fedora core 4) and GCC command line (actually, I normally use Xcode and MacOSX but the Mac doesn't have a parallel port so I am using my linux box for the exercises that require the parallel port).
The teacher wrote instructions for the class on how to install the correct header files to access the port and has given us a test program (a simple binary to decimal converter) to test our boards. However, the instructions and the code were written with the assumption that everyone would be using Windows and Dev-C.
Here is the program...
Code:
#include <stdio.h>
#include <ioports.h>
/* NOTE:
Don't forget to add the -l ioports option under
Tools->Compiler options
OR from the command line:
gcc binary.c -l ioports -o binary.exe
*/
/* Define which port to write to */
#define IO_PORT 0x378
main(void)
{
int num;
char instr[25];
printf("press ctrl-C to stop\n\n");
/* Place port into standard output mode */
outb(IO_PORT+2, inb(IO_PORT+2) & ~32);
while (1) {
printf("Enter a number (prefix with 0x for hex) :");
fgets(instr, sizeof(instr), stdin);
/* Jump out of loop if q entered */
if (toupper(instr[0]) == 'Q')
break;
sscanf(instr, "%i", &num);
/* now output it to the port */
outb(IO_PORT, num); /* write value to port */
}
return(0);
}
I have had to make a change or two to the code above due to the fact that I am using Linux instead of Windows. After some research I found that I needed to substitute the line that says...
Code:
#include <ioports.h>
for...
Code:
#include <sys/io.h>
because, as I understand it, Linux and Windows use different header files to access their ports. Assuming that this is correct and I have made the correct changes, so far so good (incidentally, if anyone can tell me if I have done this correctly I'd appreciate the feedback as I am working somewhat in the dark. My knowledge in this area is thin at best).
However, when I run the program I get a segmentation fault.
I am not sure exactly what is causing this but I did a bit more research and it seems that seg faults usually occur when you are trying to access a chunk of memory illegally. Once I found that out, I suspected that the problem was probably this line...
Code:
#define IO_PORT 0x378
I'm not 100% sure what this does but I have a hunch that it tells the program where the start address of the parallel port is. Sound likely?
If that is the case, I suspect that the teacher has put that address into the code because that is the address that the parallel port starts at under Windows. Of course, I am using Linux so it is incorrect for me and therefore causes a segmentation fault.
Can anyone tell me whether or not this is a likely hypothesis or not?
Assuming I have deduced correctly, can anyone tell me what address Linux uses as the start address for it's parallel port? Failing that can anyone point me to a source where I could find out?
I apologise if I've made some silly assumptions or come to some boneheaded conclusions in any of the above. As I have said, my knowledge in this area is very very thin and I'm having a real hard time just trying to fit it all together.
Any and all help gratefully appreciated.
Cheers,
TV