Main Page   Modules   Alphabetical List   Data Structures   File List   Data Fields   Related Pages  

pcdcount.c

00001 /*pcdcount.c
00002 12/12/02
00003 
00004 Nathan Z. Gustavson
00005 ngustavson@emacinc.com
00006 Emac.inc
00007 www.emacinc.com
00008 
00009 Description:
00010 A C program for controlling 
00011 the counters of the pcde12
00012 
00013 ex:
00014 setup command
00015 pcdcount c num trigger threshold
00016 
00017 read command
00018 pcdcount r num
00019 
00020 */
00021 
00022 #include "counter.h"
00023 #include "pcddigital.h"
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 #include <string.h>
00027 #include <sys/fcntl.h>
00028 #include <sys/ioctl.h>
00029 #include <unistd.h>
00030 #include <signal.h>
00031 
00032 
00033 int zeros = 0;
00034 volatile int waiting = 1;
00035 
00036 static int help(void)
00037 {
00038 const char *help_table = "**********************************************
00039 *pcdcount                                    *
00040 *12/12/02 Nathan Z. Gustavson                *
00041 *Emac.inc                                    * 
00042 *                                            *   
00043 *usage:                                      *   
00044 *reading                                     *
00045 *pcdcount r counter                          *
00046 *setup                                       *   
00047 *pcdcount s counter rise/fall/both threshold *
00048 *control                                     *
00049 *pcdcount c counter start/stop/signal/nosig  *
00050 *load                                        *
00051 *pcdcount l counter value                    *
00052 *zerocounter                                 *
00053 *pcdcount z counter rise/fall/both threshold *
00054 *countdown                                   * 
00055 *pcdcount d counter rise/fall/both threshold *
00056 **********************************************\n";
00057 
00058 
00059 printf("%s",help_table);
00060 
00061 
00062 return 0;
00063 }
00064 
00065 #define ARGCHECK(num){if(argc<num){printf("invalid number of arguements\n");help();return -1;}}
00066 
00067 
00068 /*signal handlers
00069 for async IO settings z and d
00070 respectively
00071 */
00072 static void zero_counter(int signal){
00073 printf("zeros = %u\n",++zeros);
00074 }
00075 
00076 
00077 static void terminate(int signal){
00078 printf("countdown terminated\n");
00079 waiting = 0;
00080 }
00081 
00082 
00083 static __u16 get_trigger(char *string)
00084 {
00085 if(!strcmp(string,"rise"))
00086    return RISEE;
00087 if(!strcmp(string,"fall"))
00088    return FALLE;
00089 if(!strcmp(string,"both"))
00090    return ANYE;
00091 
00092    return NOE;
00093 
00094 }
00095 
00096 
00097 static __u16 get_command(char *string)
00098 {
00099 if(!strcmp(string,"start"))
00100    return ICSTART;
00101 if(!strcmp(string,"stop"))
00102    return ICSTOP;
00103 if(!strcmp(string,"signal"))
00104    return ICNOTIFYON;
00105 if(!strcmp(string,"nosig"))
00106    return ICNOTIFYOFF;
00107 
00108 
00109    return NULLCOMMAND;
00110 
00111 }
00112 
00113 
00114 
00115 
00116 
00117 int main(int argc,char **argv)
00118 {
00119 countconfig config;
00120 int counter,fd;
00121 __u16 count;
00122 char command;
00123 char device[20];
00124 __u16 threshold;
00125 long oflags;
00126 struct sigaction zero_signal;
00127 
00128 
00129 ARGCHECK(3);
00130 
00131 command = *argv[1];
00132 counter=strtoul(argv[2],NULL,0);
00133 
00134 if((counter>7)||(counter<0))
00135   {
00136 printf("invalid counter\n");
00137 return -1;
00138   }
00139 
00140 switch(command)
00141   {
00142   case 'r'://read command
00143     sprintf(device,"/dev/count%x",counter);
00144     
00145     if((fd = open(device, O_RDWR))<=0)
00146      {
00147        printf("couldn't open %s\n",device);
00148        return -1;
00149      }
00150 
00151 
00152     if(read(fd,&count,2)!=2)
00153       {
00154     printf("couldn't read from %s\n",device);
00155     return -1;
00156       }
00157 
00158     printf("%x\n",count);
00159     close(fd);
00160     break;
00161 
00162 
00163   case 's'://setup command
00164   
00165   ARGCHECK(5);
00166 
00167   config.counter = counter;
00168 
00169   if(!(config.trigger=get_trigger(argv[3])))
00170     {
00171       printf("invalid trigger %s\n",argv[4]);
00172       return -1;
00173     }
00174 
00175  if((config.threshold=strtoul(argv[4],NULL,0)))
00176    config.flags = AUTOLOAD;
00177  else
00178    config.flags = 0;
00179 
00180    
00181    if((fd = open("/dev/dig1", O_RDWR))<=0)
00182      {
00183        printf("couldn't open /dev/dig1\n");
00184        return -1;
00185      }
00186   
00187    ioctl(fd,PCD_COUNT,&config);//pass counter configuration structure to digital port
00188 
00189    close(fd);
00190 
00191    break;
00192  
00193   case 'c'://control 
00194 //printf("control packet\n");
00195     ARGCHECK(4);
00196 
00197 sprintf(device,"/dev/count%x",counter);    
00198 
00199 if((fd = open(device, O_RDWR))<=0)
00200      {
00201        printf("couldn't open %s\n",device);
00202        return -1;
00203      }  
00204     ioctl(fd,get_command(argv[3]),NULL);
00205 //printf("ioctl called\n");
00206     close(fd);
00207 
00208     break;
00209 
00210 
00211   case 'l':
00212     ARGCHECK(4);
00213 sprintf(device,"/dev/count%x",counter);    
00214 
00215 if((fd = open(device, O_RDWR))<=0)
00216      {
00217        printf("couldn't open %s\n",device);
00218        return -1;
00219      }  
00220 
00221 threshold = (__u16)strtoul(argv[3],NULL,0);
00222 
00223 write(fd,&threshold,sizeof(threshold));
00224 
00225 close(fd);
00226 
00227 break;
00228 
00229   case 'z':
00230 
00231 //configure counter with asychronous notification flag
00232  ARGCHECK(5);
00233 
00234   config.counter = counter;
00235 
00236   if(!(config.trigger=get_trigger(argv[3])))
00237     {
00238       printf("invalid trigger %s\n",argv[4]);
00239       return -1;
00240     }
00241 
00242  if((config.threshold=strtoul(argv[4],NULL,0)))
00243    config.flags = AUTOLOAD|ANOTE;
00244  else
00245    config.flags = ANOTE;
00246  
00247 
00248 
00249    if((fd = open("/dev/dig1", O_RDWR))<=0)
00250      {
00251        printf("couldn't open /dev/dig1\n");
00252        return -1;
00253      }
00254   
00255    ioctl(fd,PCD_COUNT,&config);//pass counter configuration structure to digital port
00256 
00257 close(fd);
00258 
00259 sprintf(device,"/dev/count%x",counter);    
00260 
00261 if((fd = open(device, O_RDWR))<=0)
00262      {
00263        printf("couldn't open %s\n",device);
00264        return -1;
00265      }  
00266 
00267 printf("Initializing \n");
00268 
00269 sleep(2);//flush any pending signals
00270 
00271 zero_signal.sa_handler = zero_counter;
00272 
00273 sigaction(SIGIO, &zero_signal,NULL); //install signal handler
00274 fcntl(fd, F_SETOWN,getpid()); //set signal ownership
00275 oflags = fcntl(fd, F_GETFL); //get current flags
00276 fcntl(fd,F_SETFL, oflags|FASYNC);//add FASYNC flag to them
00277 
00278 
00279 printf("hit Cntrl-C to exit\n");
00280 
00281 ioctl(fd,ICSTART,NULL);//start counter
00282 
00283 
00284 while(1); //let signal handlers work;
00285 
00286 
00287     break;
00288 
00289   case 'd':
00290  ARGCHECK(5);
00291 
00292   config.counter = counter;
00293 
00294   if(!(config.trigger=get_trigger(argv[3])))
00295     {
00296       printf("invalid trigger %s\n",argv[4]);
00297       return -1;
00298     }
00299 
00300  if((config.threshold=strtoul(argv[4],NULL,0)))
00301    config.flags = AUTOLOAD|ANOTE|AUTOSTOP;
00302  else
00303    config.flags = ANOTE|AUTOSTOP;
00304  
00305 
00306 
00307    if((fd = open("/dev/dig1", O_RDWR))<=0)
00308      {
00309        printf("couldn't open /dev/dig1\n");
00310        return -1;
00311      }
00312   
00313    ioctl(fd,PCD_COUNT,&config);//pass counter configuration structure to digital port
00314 
00315 close(fd);
00316 
00317 sprintf(device,"/dev/count%x",counter);    
00318 
00319 if((fd = open(device, O_RDWR))<=0)
00320      {
00321        printf("couldn't open %s\n",device);
00322        return -1;
00323      }  
00324 
00325 printf("Initializing \n");
00326 
00327 sleep(2);//flush any pending signals
00328 
00329 zero_signal.sa_handler = terminate;
00330 
00331 sigaction(SIGIO, &zero_signal,NULL); //install signal handler
00332 fcntl(fd, F_SETOWN,getpid()); //set signal ownership
00333 oflags = fcntl(fd, F_GETFL); //get current flags
00334 fcntl(fd,F_SETFL, oflags|FASYNC);//add FASYNC flag to them
00335 
00336 
00337 
00338 //printf("hit Cntrl-C to exit\n");
00339 
00340 printf("starting counter\n");
00341 ioctl(fd,ICSTART,NULL);//start counter
00342 
00343 
00344 while(waiting); //let signal handlers work;
00345 
00346     break;
00347 
00348   }
00349 
00350   
00351 
00352 close(fd);
00353 
00354 return 0;
00355 }
00356 
00357 
00358 
00359 
00360 
00361 
00362 

Generated on Thu Jan 8 09:51:00 2004 for PCM-37e12 by doxygen1.2.15