apps/test_mmap/test_mmap.c

Go to the documentation of this file.
00001 #include <fcntl.h>  /*open*/
00002 #include <unistd.h> /* close, sleep */
00003 #include <stdio.h>
00004 #include <stdlib.h>
00005 #include <sys/ioctl.h>
00006 #include <sys/mman.h>           /* mmap */
00007 #include <string.h> // memcpy
00008 
00009 
00010 #include <asm/elphel/c313a.h>
00011 
00012 //#define D0(x) printf("%s:%d:",__FILE__,__LINE__);x
00013 #define D0(x)
00014 //#define GLOBALPARS(x) globalPars[(x)-FRAMEPAR_GLOBALS] // should work in drivers and applications defined in c313a.h
00015 
00016 //#define MAP_OPTIONS MAP_FILE|MAP_PRIVATE //Eeeks
00017 #define MAP_OPTIONS MAP_SHARED
00018 int main (int argc, char *argv[]) {
00019  const char BuffFileName[]="/dev/circbuf";
00020 // const char HeadFileName[]="/dev/jpeghead";
00021  const char ctlFileName[]="/dev/frameparsall";
00022 
00023   int buff_fd, fd_fparmsall;
00024   int sensor_state, compressor_state;
00025   int frame_number, frame8;
00026   int jpeg_wp, jpeg_rp;
00027 
00028   int buff_size;
00029   int i,i0, a, alen;
00030   unsigned long * ccam_dma_buf;                         /* mmapped array */
00031   int jpeg_len,jpeg_start;
00032   int l;
00033   double tim;
00034   struct framepars_all_t   *frameParsAll;
00035   struct framepars_t       *framePars;
00036   unsigned long            *globalPars;
00037 
00038 //  struct framepars_past_t  *pastPars;
00039 
00040   
00041 
00042     fd_fparmsall = open(ctlFileName, O_RDWR);
00043     if (fd_fparmsall<0) { // check control OK
00044        printf ("Error opening %s\n", ctlFileName);
00045        return -1;
00046     }
00047 
00048 // Read current sensor state - defined in c313a.h
00049     frame_number=lseek(fd_fparmsall, 0, SEEK_CUR );
00050     frame8= frame_number & PARS_FRAMES_MASK;
00051     printf ("Current frame number is %d\n",frame_number);
00053     frameParsAll = (struct framepars_all_t *) mmap(0, sizeof (struct framepars_all_t) , PROT_READ, MAP_SHARED, fd_fparmsall, 0);
00054     if((int)frameParsAll == -1) {
00055       frameParsAll=NULL;
00056       printf("Error in mmap /dev/frameparsall");
00057       close (fd_fparmsall);
00058       fd_fparmsall = -1;
00059       return -1;
00060     }
00061     framePars=  frameParsAll->framePars;
00062     globalPars= frameParsAll->globalPars;
00063 
00064     sensor_state=framePars[frame8].pars[P_SENSOR_RUN];
00065     printf ("Sensor state is %d (%s)\n",sensor_state, (sensor_state==0)?"SENSOR_RUN_STOP":((sensor_state==1)?"SENSOR_RUN_SINGLE":((sensor_state==2)?"SENSOR_RUN_CONT":"UNKNOWN")));
00066 
00067     compressor_state=framePars[frame8].pars[P_COMPRESSOR_RUN];
00068     printf ("Compressor state is %d (%s)\n",compressor_state, (compressor_state==0)?"COMPRESSOR_RUN_STOP":((compressor_state==1)?"COMPRESSOR_RUN_SINGLE":((compressor_state==2)?"COMPRESSOR_RUN_CONT":"UNKNOWN")));
00069 
00070    int circbuf_wp,circbuf_rp,circbuf_size,circbuf_free;
00072    circbuf_wp=GLOBALPARS(G_CIRCBUFWP);
00073    circbuf_rp=GLOBALPARS(G_CIRCBUFRP);
00074    circbuf_size=GLOBALPARS(G_CIRCBUFSIZE);
00075    circbuf_free=GLOBALPARS(G_FREECIRCBUF);
00076    printf("circbuf_size=0x%08x (0x%08x)\n",circbuf_size, circbuf_size>>2);
00077    printf("circbuf_wp=  0x%08x (0x%08x)\n",circbuf_wp,   circbuf_wp>>2);
00078    printf("circbuf_rp=  0x%08x (0x%08x)\n",circbuf_rp,   circbuf_rp>>2);
00079    printf("circbuf_free=0x%08x (0x%08x)\n",circbuf_free, circbuf_free>>2);
00080    jpeg_wp= circbuf_wp>>2;
00081    printf ("Write pointer was 0x%x\n",jpeg_wp);
00082    close(fd_fparmsall);
00083 
00085     buff_fd = open(BuffFileName, O_RDWR);
00086     if (buff_fd<0) { // check control OK
00087        printf ("Error opening %s\n", BuffFileName);
00088        return -1;
00089     }
00090 #if 0
00091 #define LSEEK_CIRC_TORP  1
00092 #define LSEEK_CIRC_TOWP  2
00093 #define LSEEK_CIRC_PREV  3
00094 #define LSEEK_CIRC_NEXT  4
00095 #define LSEEK_CIRC_LAST  5
00096 #define LSEEK_CIRC_FIRST 6
00097 #define LSEEK_CIRC_SETP  7
00098 #define LSEEK_CIRC_VRFY  8
00099 #define LSEEK_CIRC_WAIT  9
00100 #endif
00101 
00102 // find total buffer length (it is in defines, actually in c313a.h
00103     buff_size=lseek(buff_fd,0,2);
00104     jpeg_rp=lseek(buff_fd,LSEEK_CIRC_TORP,2);
00105     if (jpeg_rp<0) { 
00106         printf ("Read pointer was invalid, recalculating\n");
00107         jpeg_rp=lseek(buff_fd,LSEEK_CIRC_FIRST,2);    
00108     }
00109     jpeg_rp =jpeg_rp >> 2; // to words
00110     printf ("Circular buffer stores %d (0x%x) long values or %d (0x%x bytes), global read pointer is 0x%x\n",(buff_size>>2),(buff_size>>2),buff_size,buff_size, jpeg_rp);
00111 // now let's try mmap itself
00112 
00113 D0(sleep(5));
00114    ccam_dma_buf = (unsigned long *) mmap(0, buff_size, PROT_READ, MAP_OPTIONS, buff_fd, 0);
00115 //   ccam_dma_buf = (unsigned long *) mmap(0, buff_size, PROT_READ, MAP_SHARED, buff_fd, 0);
00116 
00117    if((int)ccam_dma_buf == -1) {
00118      printf("Error in mmap %s\r\n",BuffFileName);
00119      close(buff_fd);
00120      return -1;
00121    }
00122 // display a portion of the buffer around the jpeg_wp
00123    printf ("[] shows the value pointed by the last frame's DMA address pointer, <> - length of the last frame,\n" );
00124    alen=jpeg_wp-9; if (alen<0) alen+= (buff_size>>2);
00125    i0=(jpeg_wp>>3)<<3;
00126 D0(sleep(5));
00127    for (i=(i0-64);i<(i0+32);i++) {
00128      a=i;
00129      if (i<0) a+=(buff_size>>2);
00130      else if (i>=(buff_size>>2)) a-=(buff_size>>2);
00131      if ((i & 7)==0) printf("\n%06x: ",a);
00132      if      (a==alen) printf("<%08lx>",ccam_dma_buf[a]);
00133      else if (a==jpeg_wp) printf("[%08lx]",ccam_dma_buf[a]);
00134      else                 printf(" %08lx ",ccam_dma_buf[a]);
00135    }
00136    printf("\n");
00137 D0(sleep(5));
00138 
00139 // let's get the last frame here
00140    jpeg_len=ccam_dma_buf[alen] & 0xffffff; 
00141    tim=ccam_dma_buf[alen-1];
00142    tim=ccam_dma_buf[alen-2]+(0.000001*tim);
00143 //   jpeg_start=(alen & 0xfffffff8)- (((jpeg_len+7) & 0xffffffe0)>>2);
00144    jpeg_start=(alen & 0xfffffff8)- (((jpeg_len + CCAM_MMAP_META + 3) & 0xffffffe0)>>2);
00145    if (jpeg_start<0) jpeg_start+=(buff_size>>2);
00146    printf ("JPEG length=%d (0x%x)\n",jpeg_len,jpeg_len);
00147    printf ("timestamp=%f\n",tim);
00148    for (i=(jpeg_start-48);i<(jpeg_start+16);i++) {
00149      a=i;
00150      if (i<0) a+=(buff_size>>2);
00151      else if (i>=(buff_size>>2)) a-=(buff_size>>2);
00152      if ((i & 7)==0) printf("\n%06x: ",a);
00153      if      (a==jpeg_start) printf("{%08lx}",ccam_dma_buf[a]);
00154      else                 printf(" %08lx ",ccam_dma_buf[a]);
00155    }
00156    printf("\n");
00157 
00158    if (jpeg_rp>=0) {
00159    printf("\n");
00160      for (i=(jpeg_rp-16);i<(jpeg_rp+8);i++) {
00161        a=i;
00162        if (i<0) a+=(buff_size>>2);
00163        else if (i>=(buff_size>>2)) a-=(buff_size>>2);
00164        if ((i & 7)==0) printf("\n%06x: ",a);
00165        if      (a==jpeg_rp) printf("(%08lx)",ccam_dma_buf[a]);
00166        else                 printf(" %08lx ",ccam_dma_buf[a]);
00167      }
00168      printf("\n");
00169    }
00170 
00171 D0(sleep(5));
00172    l= munmap(ccam_dma_buf, buff_size);
00173 D0(sleep(5));
00174    printf ("munmap(0, %d) returned %d\n",buff_size,l);
00175 D0(sleep(5));
00176 
00177    close (buff_fd);              
00178 D0(sleep(5));
00179    return 0;
00180 }
00181 

Generated on Fri Nov 28 00:06:23 2008 for elphel by  doxygen 1.5.1