me8100_test_dio/me8100_test_dio.c
changeset 0 c9b8efdb5369
child 3 fc24e3b47731
equal deleted inserted replaced
-1:000000000000 0:c9b8efdb5369
       
     1 /*
       
     2  * Source File : me8100_test_dio.c
       
     3  * Destination : me8100_test_dio
       
     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 use of the driver and the digital inputs
       
    15  *   and outputs. First the outputs are tested in sink and source mode. 
       
    16  *   Then the inputs are tested.
       
    17  */
       
    18 #include <stdio.h>
       
    19 #include <fcntl.h>
       
    20 #include <unistd.h>
       
    21 #include <sys/ioctl.h>
       
    22 #include <signal.h>
       
    23 #include <linux/spinlock.h>
       
    24 
       
    25 #include "../me8100.h"
       
    26 
       
    27 int main(void){
       
    28   int err = 0;
       
    29   int minor = 0;
       
    30   int count = 0;
       
    31   int i;
       
    32   static int file_handle = -1;
       
    33 
       
    34   unsigned short ctrl_a;
       
    35   unsigned short ctrl_b;
       
    36   unsigned short value_a;
       
    37   unsigned short value_b;
       
    38 
       
    39   printf("%c%3s", 27, "[2J");
       
    40   printf("<<<--- ME8100 TESTPROGRAM FOR DIO --->>>\n\n");
       
    41 
       
    42   /*
       
    43    * You can select up to four me8100 baords, if installed.
       
    44    * 0 is the first board.
       
    45    */
       
    46   printf("Please type in the Minor Device Number of Board to open : ");
       
    47   count = scanf("%d", &minor);
       
    48   if(!count){
       
    49     printf("Invalid Input !\n");
       
    50     return 1;
       
    51   }
       
    52   printf("Open path /dev/me8100_%d !\n\n", minor);
       
    53   
       
    54   switch(minor){
       
    55   case 0:
       
    56     file_handle = open("/dev/me8100_0", O_RDWR, 0);
       
    57     break;
       
    58   case 1:
       
    59     file_handle = open("/dev/me8100_1", O_RDWR, 0);
       
    60     break;
       
    61   case 2:
       
    62     file_handle = open("/dev/me8100_2", O_RDWR, 0);
       
    63     break;
       
    64   case 3:
       
    65     file_handle = open("/dev/me8100_3", O_RDWR, 0);
       
    66     break;
       
    67   default:
       
    68     printf("Invalid Input !\n");
       
    69     return 1;
       
    70   }
       
    71 
       
    72   if(file_handle < 0){
       
    73     printf("Cannot open path !\n");
       
    74     return 1;
       
    75   }
       
    76 
       
    77   /*
       
    78    * DIGITAL I/O 
       
    79    *
       
    80    * Now that you have access to the me8100 have to configurate
       
    81    * the board according to your needs. 
       
    82    * The ME8100_B has got two output ports with 16 bit each. You can
       
    83    * decide wether the port is driven as sink or as source.
       
    84    * Additionally, you have to enable the outputs explicitly.
       
    85    */
       
    86 
       
    87 
       
    88   /*------------------ BOTH OUTPUT PORTS AS SOURCE --------------------------*/
       
    89 
       
    90   printf("\nPlease press return to test both output ports as source :\n");
       
    91   getchar();
       
    92   getchar();
       
    93   
       
    94   /* Setup for the first output port */
       
    95   ctrl_a = 0x90; /* We want to configure as source and to enable the outputs */
       
    96   err = ioctl(file_handle , ME8100_WRITE_CTRL_A, &ctrl_a);
       
    97   if(err){
       
    98     printf("Cannot setup output port A \n");
       
    99     return 1;
       
   100   }
       
   101 
       
   102   /* Setup for the second output port */
       
   103   ctrl_b = 0x90; /* We want to configure as source and to enable the outputs */
       
   104   err = ioctl(file_handle , ME8100_WRITE_CTRL_B, &ctrl_b);
       
   105   if(err){
       
   106     printf("Cannot setup output port B \n");
       
   107     return 1;
       
   108   }
       
   109 
       
   110   for(i = 0; i < 16; i++){
       
   111     /* Write to port A */
       
   112     value_a = 0x1 << i;
       
   113     err = ioctl(file_handle, ME8100_WRITE_DO_A, &value_a); /* Do the job */
       
   114     if(err){
       
   115       printf("Cannot write to output port A \n");
       
   116       return 1;
       
   117     }
       
   118     printf("Write to Port A : 0x%04X\n", value_a);
       
   119     
       
   120     /* Write to port B */
       
   121     value_b = 0x8000 >> i;
       
   122     err = ioctl(file_handle, ME8100_WRITE_DO_B, &value_b); /* Do the job */
       
   123     if(err){
       
   124       printf("Cannot write to port B \n");
       
   125       return 1;
       
   126     }
       
   127     printf("Write to Port B : 0x%04X\n\n", value_b);
       
   128     sleep(1);
       
   129   }
       
   130 
       
   131 
       
   132   /*-------------------- BOTH OUTPUT PORTS AS SINK --------------------------*/
       
   133 
       
   134   printf("Please press return to test both output ports as sink :\n");
       
   135   getchar();
       
   136   
       
   137   /* Setup for the first output port */
       
   138   ctrl_a = 0x80; /* We want to configure as sink and to enable the outputs */
       
   139   err = ioctl(file_handle , ME8100_WRITE_CTRL_A, &ctrl_a);
       
   140   if(err){
       
   141     printf("Cannot setup output port A \n");
       
   142     return 1;
       
   143   }
       
   144 
       
   145   /* Setup for the second output port */
       
   146   ctrl_b = 0x80; /* We want to configure as sink and to enable the outputs */
       
   147   err = ioctl(file_handle , ME8100_WRITE_CTRL_B, &ctrl_b);
       
   148   if(err){
       
   149     printf("Cannot setup output port B \n");
       
   150     return 1;
       
   151   }
       
   152 
       
   153   for(i = 0; i < 16; i++){
       
   154     /* Write to port A */
       
   155     value_a = 0x1 << i;
       
   156     err = ioctl(file_handle, ME8100_WRITE_DO_A, &value_a); /* Do the job */
       
   157     if(err){
       
   158       printf("Cannot write to output port A \n");
       
   159       return 1;
       
   160     }
       
   161     printf("Write to Port A : 0x%04X\n", value_a);
       
   162     
       
   163     /* Write to port B */
       
   164     value_b = 0x8000 >> i;
       
   165     err = ioctl(file_handle, ME8100_WRITE_DO_B, &value_b); /* Do the job */
       
   166     if(err){
       
   167       printf("Cannot write to port B \n");
       
   168       return 1;
       
   169     }
       
   170     printf("Write to Port B : 0x%04X\n\n", value_b);
       
   171     sleep(1);
       
   172   }
       
   173 
       
   174 
       
   175   /*--------------------------- READ FROM BOTH INPUT PORTS -----------------------*/
       
   176 
       
   177   printf("Please press return to read from both input ports :\n");
       
   178   getchar();
       
   179 
       
   180   for(i = 0; i < 20; i++){
       
   181     /* Read from port A */
       
   182     err = ioctl(file_handle, ME8100_READ_DI_A, &value_a);  /* Do the job */
       
   183     if(err){
       
   184       printf("Cannot read port A \n");
       
   185       return 1;
       
   186     }
       
   187     /* 
       
   188      * The result is put into value_a. 
       
   189      * We simply print the result as a hex number. 
       
   190      */
       
   191     printf("Read from Port A: 0x%04X\n", value_a);
       
   192     
       
   193     
       
   194     /* Read from port B */
       
   195     err = ioctl(file_handle, ME8100_READ_DI_B, &value_b);  /* Do the job */
       
   196     if(err){
       
   197       printf("Cannot read port B \n");
       
   198       return 1;
       
   199     }
       
   200     /* 
       
   201      * The result is put into value_b. 
       
   202      * We simply print the result as a hex number. 
       
   203      */
       
   204     printf("Read from Port B: 0x%04X\n\n", value_b);
       
   205     sleep(1);
       
   206   }
       
   207 
       
   208 
       
   209   /*-------------------------------- END ------------------------------------*/
       
   210 
       
   211   printf("Close path to me8100_%d\n", minor);
       
   212   err = close(file_handle);
       
   213   if(err){
       
   214     printf("Kann Pfad nicht schliessen\n");
       
   215     return 1;
       
   216   }
       
   217 
       
   218   return 0;
       
   219 }