me8100_test_int/test.c
changeset 17 b51d4c6816e7
parent 15 b9baa645576a
equal deleted inserted replaced
16:6b2a0de29990 17:b51d4c6816e7
     1 /*
       
     2  * Source File : me8100_test_int.c                                            
       
     3  * Destination : me8100_test_int.out                                              
       
     4  * Author      : GG (Guenter Gebhardt)                                 
       
     5  *    
       
     6  *                                                                     
       
     7  * File History: Version   Date       Editor   Action                  
       
     8  *---------------------------------------------------------------------
       
     9  *               1.00.00   01.07.12   GG       first release           
       
    10  *                                                                     
       
    11  *---------------------------------------------------------------------
       
    12  *                                                                     
       
    13  * Description:
       
    14  *   This program shows the usage of the driver and the interrupt
       
    15  *   facility of the me8100. First the board is configured, in order to
       
    16  *   generate an interrupt when a bit pattern of 0x0001 on port a and a bit
       
    17  *   pattern of 0x0100 an port b is pending. Then the board is configured, 
       
    18  *   in order to generate an interrupt with a bit mask of 0x0001 on port a 
       
    19  *   and a bit mask of 0x0100 on port b. 
       
    20  *   We install a signal handler, which is informed by the interrupt routine 
       
    21  *   by signalling of the driver, when a interrupt occures.
       
    22  */
       
    23 #include <stdio.h>
     1 #include <stdio.h>
    24 #include <fcntl.h>
     2 #include <fcntl.h>
    25 #include <unistd.h>
     3 #include <unistd.h>
    26 #include <sys/ioctl.h>
     4 #include <sys/ioctl.h>
       
     5 #include <sys/poll.h>
    27 #include <signal.h>
     6 #include <signal.h>
    28 #include <stdlib.h>
     7 #include <stdlib.h>
    29 #include "me8100.h"
     8 #include "me8100.h"
    30 
     9 
       
    10 const char* DEVICE = "/dev/me8100_0a";
       
    11 const int MAX_FDS = 2;
    31 
    12 
    32 /* Prototypes */
    13 /* Prototypes */
    33 static void signal_handler(int);
    14 static void signal_handler(int);
    34 
    15 
    35 /* Counts the interrupts */
    16 /* Counts the interrupts */
    40 
    21 
    41 /* Path to the ME8100 board */
    22 /* Path to the ME8100 board */
    42 static int file_handle = -1;
    23 static int file_handle = -1;
    43 
    24 
    44 int main(void){
    25 int main(void){
    45   int err = 0;
    26   int oflags;
    46   int oflags = 0;
    27   int err;
       
    28 
       
    29   struct pollfd fds[MAX_FDS];
    47 
    30 
    48   printf("IRQ Test %d\n", getpid());
    31   printf("IRQ Test %d\n", getpid());
    49 
    32 
    50   file_handle = open("/dev/me8100_0a", O_RDONLY, 0);
    33   file_handle = open(DEVICE, O_RDONLY, 0);
    51 
    34 
    52   if(file_handle < 0){
    35   if(file_handle < 0){
    53     perror("Can't open path");
    36     fprintf(stderr, "Can't open path %s: %m\n", DEVICE);
    54     return 1;
    37     exit(EXIT_FAILURE);
    55   }
    38   }
    56 
    39 
    57   /*---------------------- general setup ------------------------------------*/
    40   /*---------------------- general setup ------------------------------------*/
    58 
    41 
    59   /* install the signal handler */
    42   /* install the signal handler */
    60   signal(SIGIO, signal_handler);
    43   signal(SIGIO, signal_handler);
    61 
    44 
    62   /* set current process as owner of the path */
    45   /* set current process as owner of the path */
    63   fcntl(file_handle, F_SETOWN, getpid());
    46   err = fcntl(file_handle, F_SETOWN, getpid());
       
    47   if (err < 0) {
       
    48     perror("fcntl SETOWN");
       
    49     exit(EXIT_FAILURE);
       
    50   }
    64 
    51 
    65   /* read the flags of the path */
    52   /* read the flags of the path */
    66   oflags = fcntl(file_handle, F_GETFL);
    53   oflags = fcntl(file_handle, F_GETFL);
       
    54   if (err < 0) {
       
    55     perror("fcntl GETFL");
       
    56     exit(EXIT_FAILURE);
       
    57   }
    67 
    58 
    68   /* Inform the driver to put the current process on the fasync queue */
    59   /* Inform the driver to put the current process on the fasync queue */
    69   fcntl(file_handle, F_SETFL, oflags | FASYNC); 
    60   err = fcntl(file_handle, F_SETFL, oflags | FASYNC); 
       
    61   if (err < 0) {
       
    62     perror("fcntl SETFL");
       
    63     exit(EXIT_FAILURE);
       
    64   }
       
    65 
       
    66   fds[0].fd = file_handle;
       
    67   fds[0].events = POLLIN;
    70 
    68 
    71   printf("Waiting for Interrupts\n\n");
    69   printf("Waiting for Interrupts\n\n");
    72 
    70 
    73   for(;;) {
    71   for(;;sleep(2)) {
    74     sleep(10);
    72     int ready;
       
    73     int i;
       
    74     unsigned short val;
       
    75 
       
    76     fds[0].revents = 0;
       
    77     printf("Polling on fd %d\n", file_handle);
       
    78     ready = poll(fds, 1, 1000);
       
    79     if (ready < 0) { perror("poll()"); exit(EXIT_FAILURE); }
       
    80 
       
    81     printf("polled, %d fds are ready\n", ready);
       
    82 
       
    83     for (i = 0; i < ready; ++i) {
       
    84       printf("fd %d: 0x%0x\n", i, fds[i].revents);
       
    85       if (fds[i].revents & POLLIN) {
       
    86 	read(file_handle, &val, sizeof(val));
       
    87 	printf("read: 0x%04x\n", val);
       
    88       }
       
    89     }
       
    90 
       
    91     printf("Waiting...\n");
    75   }
    92   }
    76 
    93 
    77   printf("Close path to me8100_0\n");
    94   printf("Close path to %s\n", DEVICE);
    78   err = close(file_handle);
    95   err = close(file_handle);
    79   if(err){
    96   if(err){
    80     printf("Kann Pfad nicht schliessen\n");
    97     fprintf(stderr, "Can't close %s: %m\n", DEVICE);
    81     return 1;
    98     exit(EXIT_FAILURE);
    82   }
    99   }
    83 
   100 
    84   return 1;
   101   exit(EXIT_SUCCESS);
    85 }
   102 }
    86 
   103 
    87 
   104 
    88 
   105 
    89 void signal_handler(int sig){
   106 void signal_handler(int sig){