/* * IOPortTest uCHobby 8.20.2010 * sets bit 0 and 1 of the Parallel port Data pins high for 10mS. * These two bits are pins 2 and 3 on the standard DB25 connector. * * Two methods are shown, the first using IOCTL calls,the second * uses direct I/O writes. To use direct writes we need to know * the IO port addresses, I found mine by hunting through boot * up messages. */ #include #include #include #include #include #include #include #include void ToggleWithIOCTL(void); void ToggleWithIORaw(void); int main() { ToggleWithIOCTL(); //Use the IOCTL driver method to toggle the pins sleep(1); //Sleep for one second ToggleWithIORaw(); //This time use the raw IO methods to toggle the pins exit(EXIT_SUCCESS); } //Use IOCTL calls to control printer port IO - Works well. void ToggleWithIOCTL (void){ int portHandle; //Handle to reference the parallel port as a file portHandle=open("/dev/parport0", O_RDWR); //Open the port. if (portHandle == -1) { //Did the port fail to open? perror ("open"); //Could not open, output error with an explanation } if (portHandle != -1) { //if the port opened OK, claim it for our purposes if (ioctl (portHandle, PPCLAIM)) { //Were we able to claim it? perror ("PPCLAIM"); //Cannot claim port, output an explanation close (portHandle); //Close the file we had portHandle=-1; //set the handle to show not-valid } } if(portHandle != -1){ //Do we have a valid file handle? int iTemp=0x03; //Just need to turn on the lower two bits, D0 and D1, //leave D2-D7 low. Use a temp because we have to pass //by reference. if(ioctl(portHandle, PPWDATA,&iTemp)) { //Set the port value printf("PPWDATA"); //Write failed, output error with an explanation } usleep(10000); //Sleep for 10 milliseconds, value is in microseconds. iTemp=0x00; //turn off all the bits D0-D7 if(ioctl(portHandle, PPWDATA,&iTemp)) { //Set the port value printf("PPWDATA"); //Write failed, output error with an explanation } } if(ioctl(portHandle, PPRELEASE)) { //Try and release the ppdev file handle printf("PPRELEASE"); //Did not release, output an explanation } close(portHandle); } #define PPDEV_BASEPORT 0xC400 //Found by hunting in boot messages near ppdev #define PPDEV_DATA (PPDEV_BASEPORT+0) //Define constant for the DATA port #define PPDEV_STATUS (PPDEV_BASEPORT+1) //Define constant for the STATUS port #define PPDEV_CONTROL (PPDEV_BASEPORT+2) //Define constant for the CONTROL port #define TRUE 1 //These should be defined somewhere in a generic module, #define FALSE 0 //for now I'm defining them here. //Use raw I/O method to control printer port IO void ToggleWithIORaw (void) { int portOK=FALSE; if(iopl(3)){ //Request permission to access IO ports using raw //functions. This may not be necessary perror("iopl"); //No permission, output error with an explanation } else { //We got permission /* Get access to the ports */ if (ioperm(PPDEV_BASEPORT, 3, 1)) { //Get permission to access the //specific ports we need. perror("ioperm"); //No permission, output error with an explanation exit(EXIT_FAILURE); //We are done, bad code should not exit here } else { portOK=TRUE; //We have permission and have claimed the ports we need } } if (portOK) { outb(0x03, PPDEV_DATA); //Set the lower two bits of DATA register. usleep(10000); //Sleep for 10 milliseconds, in microseconds. outb(0x00, PPDEV_DATA); //turn off all the bits D0-D7 } if (ioperm(PPDEV_BASEPORT, 3, 0)) { //Release the IO ports perror("ioperm"); //Could not release, output error with an explanation portOK=FALSE; //We had some trouble ending port access. } // return(portOK); //not returning value right now. }