me8100_test_dio/wtest.c
changeset 23 8675c3e5d914
parent 22 7b722739e329
equal deleted inserted replaced
22:7b722739e329 23:8675c3e5d914
    19 #include <fcntl.h>
    19 #include <fcntl.h>
    20 #include <unistd.h>
    20 #include <unistd.h>
    21 #include <sys/ioctl.h>
    21 #include <sys/ioctl.h>
    22 #include <signal.h>
    22 #include <signal.h>
    23 #include <stdlib.h>
    23 #include <stdlib.h>
       
    24 #include <string.h>
    24 #include "me8100.h"
    25 #include "me8100.h"
    25 
    26 
    26 #define USE_WRITE
    27 #define USE_WRITE
    27 
    28 
    28 const char* DEVICE = "/dev/me8100_0a";
    29 const char* DEVICE = "/dev/me8100_0a";
    29 
    30 
    30 int main(int argc, char** argv) {
    31 int main(int argc, char** argv) {
    31   int err = 0;
    32   char dev = 0;
    32   static int file_handle = -1;
    33   static int file_handle;
    33 
    34   unsigned short value;
    34 #ifndef USE_WRITE
       
    35   unsigned short ctrl_a;
       
    36 #endif
       
    37   unsigned short value_a;
       
    38 
    35 
    39   if (argc > 1) DEVICE = argv[1];
    36   if (argc > 1) DEVICE = argv[1];
       
    37   dev = DEVICE[strlen(DEVICE) - 1];
       
    38 
    40   printf("Write test, to %s PID: %d\n", DEVICE, getpid());
    39   printf("Write test, to %s PID: %d\n", DEVICE, getpid());
    41 #ifdef USE_WRITE
       
    42   printf("Using write()\n");
    40   printf("Using write()\n");
    43 #endif
    41 
    44 
    42 
    45 
    43 
    46   if (-1 == (file_handle = open(DEVICE, O_WRONLY, 0))) {
    44   if (-1 == (file_handle = open(DEVICE, O_WRONLY, 0))) {
    47     fprintf(stderr, "Can't open %s: %m\n", DEVICE);
    45     fprintf(stderr, "Can't open %s: %m\n", DEVICE);
    48     exit(EXIT_FAILURE);
    46     exit(EXIT_FAILURE);
    49   }
    47   }
    50 
    48 
    51 #ifndef USE_WRITE
    49   for (value = 0x00;;++value) {
    52   ctrl_a = ME8100_CTRL_ENIO | ME8100_CTRL_SOURCE | ME8100_CTRL_IRQ_MASK;
    50     int val;
    53   err = ioctl(file_handle, ME8100_WRITE_CTRL_A, &ctrl_a);
    51     if (value > 0xff) value = 0;
    54   if (err) {
       
    55     fprintf(stderr, "Can't setup output to port A\n");
       
    56     return 1;
       
    57   }
       
    58 #endif
       
    59 
    52 
    60   value_a = 0xffff;
    53     val = value + (dev << 8);;
    61   write(file_handle, &value_a, sizeof(value_a));
    54     write(file_handle, &val, sizeof(val));
    62   exit(EXIT_SUCCESS);
    55     printf("Wrote %04x\n", val);
    63 
       
    64   for (value_a = 0x01; value_a < 0xffff; ++value_a) {
       
    65 #ifdef USE_WRITE
       
    66     write(file_handle, &value_a, sizeof(value_a));
       
    67 #else
       
    68     ioctl(file_handle, ME8100_WRITE_DO_A, &value_a);
       
    69 #endif
       
    70     printf("Wrote %04x\n", value_a);
       
    71     sleep(1);
    56     sleep(1);
    72   }
    57   }
    73 
    58 
    74   err = close(file_handle);
    59   if(-1 == close(file_handle)) {
    75   if(err){
    60     perror("Can't close file");
    76     printf("Kann Pfad nicht schliessen\n");
    61     exit(EXIT_FAILURE);
    77     return 1;
       
    78   }
    62   }
    79 
    63 
    80   return 0;
    64   return 0;
    81 }
    65 }
    82 /*
    66 /*