simple_putdata.c

Go to the documentation of this file.
00001 
00035 #include "edtinc.h"
00036 
00037 #include <string.h>
00038 #include <stdlib.h>
00039 #ifndef _NT_
00040 #include <unistd.h>
00041 #endif
00042 
00043 #define NUMBUFS 4   /* Four is the optimal number of buffers for EDT boards */
00044 int numbufs = NUMBUFS ;
00045 int buffers_inuse ;
00046 
00047 
00048 void
00049 usage()
00050 {
00051     puts("usage: putdata") ;
00052     puts("-b        - set byteswap on") ;
00053     puts("-s size       - specifies buffer size in bytes") ;
00054     puts("-k size       - specifies buffer size in kbytes") ;
00055     puts("-l loop       - specifies number of loops") ;
00056     puts("-i infile\t- specifies input filename (\"-i -\" uses stdandard input");
00057     puts("-h            - prints usage (help)") ;
00058 }
00059 
00060 void init_buf(u_char *buf, int size) ;
00061 
00062 int
00063 main(int argc, char **argv)
00064 {
00065     EdtDev *edt_p ;
00066     int unit ;
00067     int loop ;
00068         int loopcount ;
00069         int byteswap=0;
00070         double dtime ;
00071     unsigned int size ;
00072     u_char *buf ;
00073     char *infile ;
00074     int ifd = -1 ;
00075         int lstcnt ;
00076         int curcnt ;
00077         int timeouts ;
00078         int channel = 0;
00079         int fill;
00080         int done = 0;
00081         int total_buffers = 0;
00082         double num_bytes;
00083     /* 
00084      * set defaults 
00085      */
00086     unit = 0 ;
00087     infile = NULL ;
00088     /* default to 100 writes of 1 Megabyte */
00089     size = 1024*1024 ;
00090     loopcount = 100 ;
00091     timeouts = 0 ;
00092 
00093     --argc;
00094     ++argv;
00095     while (argc && argv[0][0] == '-')
00096     {
00097                 switch (argv[0][1])
00098                 {
00099                 case 'u':
00100                         ++argv;
00101                         --argc;
00102                         unit = atoi(argv[0]);
00103                         break ;
00104                 case 'b':
00105                         byteswap = 1;
00106                         break ;
00107                 case 's':
00108                         ++argv;
00109                         --argc;
00110                         size = atoi(argv[0]);
00111                         break ;
00112                 case 'k':
00113                         ++argv;
00114                         --argc;
00115                         size = atoi(argv[0]) * 1024;
00116                         break ;
00117                 case 'n':
00118                         ++argv;
00119                         --argc;
00120                         numbufs = atoi(argv[0]) ;
00121                         if (numbufs < 2)
00122                         {
00123                                 printf("numbufs should be at least two to take\n") ;
00124                                 printf("advantage of ring buffer mode\n") ;
00125                                 exit(1) ;
00126                         }
00127                         break ;
00128                 case 'l':
00129                         ++argv;
00130                         --argc;
00131                         loopcount = atoi(argv[0]) ;
00132                         break ;
00133                 case 'i':
00134                         ++argv ;
00135                         --argc ;
00136                         infile = strdup(argv[0]) ;
00137                         if (strcmp(infile, "-") == 0)
00138                                 ifd = fileno(stdin) ;
00139                         else
00140                         {
00141 #ifdef _NT_
00142                                 ifd =   (int)CreateFile(
00143                                         infile,
00144                                         GENERIC_READ | GENERIC_WRITE ,
00145                                         FILE_SHARE_READ | FILE_SHARE_WRITE,
00146                                         NULL,
00147                                         OPEN_EXISTING,
00148                                         FILE_ATTRIBUTE_NORMAL,
00149                                         NULL);
00150                                 if (ifd == (int)INVALID_HANDLE_VALUE)
00151                                 {
00152                                         fprintf(stderr, "failed open %s\n",infile) ;
00153                                         exit(1) ;
00154                                 }
00155 #else
00156                                 if ((ifd = open(infile, 0666)) < 0)
00157                                 {
00158                                         printf("create of %s failed\n", infile) ;
00159                                         perror("infile") ;
00160                                         exit(1) ;
00161                                 }
00162 #endif
00163                         }
00164                         break ;
00165                 case 'c':
00166                         ++argv ;
00167                         --argc ;
00168                         channel = atoi(argv[0]);
00169                         break;
00170 
00171                 case 'h':
00172                 case '?':
00173                 default:
00174                         usage() ;
00175                         exit(1) ;
00176                 }
00177                 --argc;
00178                 ++argv;
00179     }
00180 
00181     if (infile )
00182             printf("writing buffers until end of file of %d bytes from unit %d channel %d with %d bufs\n",
00183                  size, unit, channel, numbufs) ;
00184     else
00185             printf("writing %d buffers of %d bytes from unit %d channel %d with %d bufs\n",
00186                 loopcount, size, unit, channel, numbufs) ;
00187 
00188     if ((edt_p = edt_open_channel(EDT_INTERFACE, unit,channel)) == NULL)
00189     {
00190                 perror("edt_open") ;
00191                 exit(1) ;
00192     }
00193 
00194     /*
00195      * Have edt_configure_ring_buffers allocate the ring buffers by
00196      * passing the last argument as NULL.  Otherwise pass in an array
00197      * of pointers to the buffers.
00198      */
00199         
00200     if (edt_configure_ring_buffers(edt_p, size, numbufs, EDT_WRITE, NULL) == -1)
00201     {
00202         perror("edt_configure_ring_buffers") ;
00203         exit(1) ;
00204     }
00205 
00206         (void)edt_dtime();
00207 #ifdef PCD
00208         if (byteswap)
00209             pcd_set_byteswap(edt_p, 1) ;
00210 #endif
00211 
00212     buffers_inuse = 0 ;
00213         curcnt = edt_timeouts(edt_p) ;
00214         lstcnt = curcnt ;
00215     done = 0;
00216     if (infile)
00217         loopcount = 0;
00218 
00219     for(loop = 0 ; (loop < loopcount || loopcount == 0) && !done ; loop++)
00220     {
00221                 /*
00222                  * Initialize and start ring buffers until they're all in use,
00223                  * i.e. don't wait for a buffer until they're all being used.
00224                  */
00225                 if (buffers_inuse >= numbufs)
00226                 {
00227                         /* All ring buffers are in use; wait for the next one to finish */
00228                         edt_wait_for_buffers(edt_p, 1) ;
00229                         --buffers_inuse ;
00230                         curcnt = edt_timeouts(edt_p) ;
00231                         if (lstcnt != curcnt) 
00232                         {
00233                                 putchar('x') ;
00234                                 timeouts++ ;
00235                                 lstcnt = curcnt ;
00236                                 edt_reset_ring_buffers(edt_p,0) ;
00237                                 edt_flush_fifo(edt_p) ;
00238                                 edt_start_buffers(edt_p, 0) ; /* start the transfers in free running mode */
00239                         }
00240                         else putchar('.') ;
00241                         fflush(stdout) ;
00242                 }
00243 
00244                 /*
00245                  * Copy data into each ring buffer, then start DMA.
00246                  * Data could come from a file, a tape drive, etc,
00247                  * and is written to the external device connected to
00248                  * the EDT board continuously via the ring buffers.
00249                  */
00250                 for(fill=buffers_inuse; fill<numbufs; fill++)
00251                 {
00252                 buf = edt_next_writebuf(edt_p) ;
00253 
00254                 if (infile)
00255                 {
00256                         u_int Length ;
00257 #ifdef _NT_             
00258                         ReadFile((void *)ifd, buf, size, &Length, NULL) ;
00259 #else
00260                         Length = read(ifd, buf, size) ;
00261 #endif
00262                         buf += size;
00263                         if (Length != size)
00264                                 {
00265                                 /* change size on last if not full buf */
00266                                 printf("end of file\n") ;
00267                                 done = 1;
00268                                 }
00269                 }
00270                 else 
00271                         init_buf(buf, size);
00272                 }
00273 
00274 
00275                 if (done == 0) 
00276                 {
00277                         /* Start the transfers on this buffer */
00278                         edt_start_buffers(edt_p, numbufs - buffers_inuse); 
00279                         total_buffers = total_buffers + (numbufs - buffers_inuse);
00280                         buffers_inuse = numbufs;
00281                 }
00282     }
00283 
00284     /* Now wait for remaining buffers to finish */
00285     while (buffers_inuse)
00286     {
00287                 --buffers_inuse ;
00288                 edt_wait_for_buffers(edt_p, 1) ;
00289 
00290                 putchar('.') ;
00291                 fflush(stdout) ;
00292     }
00293 
00294         dtime = edt_dtime() ;
00295         num_bytes = (double)total_buffers * (double)size;
00296         printf("\ntotalbuffers %d %f bytes/sec %d timeouts\n",
00297                 total_buffers, num_bytes / dtime,timeouts) ;
00298         printf("\nput %f bytes in %f seconds for %f bytes/second\n",
00299                             num_bytes,
00300                             dtime,
00301                             num_bytes/dtime);
00302         edt_close(edt_p) ;
00303         exit(0) ;
00304         return(0) ;
00305 }
00306 
00307 /*
00308  * initialize buffer with 16-bit incremented data
00309  * could replace with read of data from file, other device, or ???
00310  */
00311 void
00312 init_buf(u_char *buf, int size)
00313 {
00314     u_short word = 0;
00315     u_short *p, *endp;
00316     static int count = 0 ;
00317 
00318     if (++count > numbufs)
00319         return ;
00320 
00321     endp = (u_short *) (buf + size);
00322     p = (u_short *) buf;
00323 
00324     while (p < endp)
00325     {
00326         *p++ = word++;
00327     }
00328 }

Generated on Mon May 12 16:38:59 2008 by  doxygen 1.5.1