venkat_330

Parallel port access via IOPERM

Aug 31st, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.39 KB | None | 0 0
  1. #include <errno.h>
  2. #include <fcntl.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <sys/ioctl.h>
  7. #include <sys/time.h>
  8. #include <time.h>
  9. #include <unistd.h>
  10. #include <stdint.h>
  11. #include <sys/io.h>
  12.  
  13. #define DEBUG_LOG 0
  14. #define TRACE_LOG 1
  15.  
  16. void debug_log(char *s, int t)
  17. {
  18.     printf("\n%s",s);
  19. }
  20.  
  21. #define PARALLEL_PORT_ADDR 0x378
  22. #define SENSOR_PORT_ADDR (PARALLEL_PORT_ADDR + 2)
  23. #define LOWPAPER_SENSOR_PORT (PARALLEL_PORT_ADDR + 1)
  24. #define MOTOR_PORT_ADDR (PARALLEL_PORT_ADDR + 2)
  25.  
  26. #define BIT(x) (1 << x)
  27. #define SENSOR_CTRL_BIT BIT(2)
  28. #define SENSOR_VALUE(x) (((x) & SENSOR_CTRL_BIT) >> 2)
  29. #define LOWPAPER_SENSOR_BIT BIT(3)
  30. #define LOWPAPER_SENSOR_VALUE(x) (((x) & LOWPAPER_SENSOR_BIT) >> 3)
  31. #define MOTOR_CTRL_BIT BIT(0)
  32.  
  33. #define SENSOR_CLEAR 1
  34. #define SENSOR_BLOCK 0
  35.  
  36. #define MOTOR_START 1
  37. #define MOTOR_STOP 0
  38.  
  39. int16_t readSensorVal(void)
  40. {
  41.     static int16_t portValPrev = 0;
  42.     int16_t portVal = 0;
  43.     char charBuf[128] = {'\0'};
  44.  
  45.     outb(inb(SENSOR_PORT_ADDR) | SENSOR_CTRL_BIT, MOTOR_PORT_ADDR);
  46.     usleep(5000);// Delay to make sure the value is reflected in any up-coming read
  47.     portVal = inb(SENSOR_PORT_ADDR);
  48.     if(portVal != portValPrev)
  49.     {
  50.         memset(charBuf, 0, sizeof(charBuf));
  51.         sprintf(charBuf, "PERIPHERAL:JOURNALPRINTER: portVal=%#x, Sensor=%d, Motor=%d\n", portVal, (portVal & 0x04) >> 2, portVal & 0x01);
  52.         debug_log(charBuf, DEBUG_LOG);
  53.         portValPrev = portVal;
  54.     }
  55.     portVal = SENSOR_VALUE(portVal);
  56.  
  57.     return portVal;
  58. }
  59.  
  60. void rewindMotorCtrl(uint8_t option)
  61. {
  62.     uint8_t portVal = 0;
  63.  
  64.     portVal = inb(MOTOR_PORT_ADDR);
  65.     if(option == MOTOR_START)
  66.         portVal |= MOTOR_CTRL_BIT;
  67.     else if(option == MOTOR_STOP)
  68.         portVal &= ~MOTOR_CTRL_BIT;
  69.     outb(portVal, MOTOR_PORT_ADDR);
  70.  
  71. }
  72.  
  73.  
  74. int main(int argc,char *argv[])
  75. {
  76.     int type = atoi(argv[1]);
  77.     unsigned int SensorValue = 0;
  78.     char debugBuf[1024] = {'\0'};
  79.    
  80.     if (ioperm(PARALLEL_PORT_ADDR, 3, 1))
  81.     {
  82.         perror("ioperm");
  83.         exit(1);
  84.     }
  85.  
  86.     printf("Type: %d", type);
  87.  
  88.     rewindMotorCtrl(type);
  89.  
  90.     SensorValue = readSensorVal();
  91.     if(SensorValue == SENSOR_CLEAR)
  92.     {
  93.         memset(debugBuf,0,sizeof(debugBuf));
  94.         sprintf(debugBuf,"SYSTEM:  Sensor Clear: %d", SensorValue);
  95.         debug_log(debugBuf,TRACE_LOG); 
  96.     }
  97.     else
  98.     {
  99.         memset(debugBuf,0,sizeof(debugBuf));
  100.         sprintf(debugBuf,"SYSTEM:  Sensor Block: %d", SensorValue);
  101.         debug_log(debugBuf,TRACE_LOG); 
  102.     }
  103.  
  104.     printf("\n");
  105.     return 0;
  106. }
Add Comment
Please, Sign In to add comment