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 
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 ioctlFileName[]="/dev/sensorpars"; 
00022 // const char trailer[] = {0xff,0xd9};
00023 
00024   int buff_fd, ioctl_fd;
00025   int sensor_state;
00026   int frame_number;
00027   int jpeg_wp, jpeg_rp;
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 head_fd;
00033 //  int head_size;
00034 //  int jpeg_full_size;
00035 //  char * jpeg_copy;
00036   int l;
00037   double tim;
00038   
00039   
00040 
00041     ioctl_fd = open(ioctlFileName, O_RDWR);
00042     if (ioctl_fd<0) { // check control OK
00043        printf ("Error opening %s\n", ioctlFileName);
00044        return -1;
00045     }
00046 
00047 // Read current sensor state - defined in c313a.h
00048     sensor_state = ioctl(ioctl_fd, _IO(CMOSCAM_IOCTYPE, IO_CCAM_MONITOR_SEQ ), 0);
00049     printf ("Sensor state is %d\n",sensor_state);
00050     if (sensor_state !=CAMSEQ_RUN)
00051       printf ("Constant compression is not on. You may use 'fpcf -jpeg a' to start it\n");
00052 // Read current frame number acquired
00053     frame_number= ioctl(ioctl_fd, _CCCMD( CCAM_RPARS , P_FRAME), 0);
00054     printf ("Frame number acquired is %d\n",frame_number);
00055 
00056 // Read last known (approximately) write pointer (fpga->memory). Actually it is an index in circular buffer array
00057     jpeg_wp= ioctl(ioctl_fd, _CCCMD( CCAM_RPARS , P_JPEG_WP), 0);
00058     printf ("Write pointer was 0x%x\n",jpeg_wp);
00059     close(ioctl_fd); 
00060 
00062     buff_fd = open(BuffFileName, O_RDWR);
00063     if (buff_fd<0) { // check control OK
00064        printf ("Error opening %s\n", BuffFileName);
00065        return -1;
00066     }
00067 #if 0
00068 #define CIRCLSEEK_TORP  1
00069 #define CIRCLSEEK_TOWP  2
00070 #define CIRCLSEEK_PREV  3
00071 #define CIRCLSEEK_NEXT  4
00072 #define CIRCLSEEK_LAST  5
00073 #define CIRCLSEEK_FIRST 6
00074 #define CIRCLSEEK_SETP  7
00075 #define CIRCLSEEK_VRFY  8
00076 #define CIRCLSEEK_WAIT  9
00077 #endif
00078 
00079 // find total buffer length (it is in defines, actually in c313a.h
00080     buff_size=lseek(buff_fd,0,2);
00081     jpeg_rp=lseek(buff_fd,CIRCLSEEK_TORP,2);
00082     if (jpeg_rp<0) { 
00083         printf ("Read pointer was invalid, recalculating\n");
00084         jpeg_rp=lseek(buff_fd,CIRCLSEEK_FIRST,2);    
00085     }
00086     jpeg_rp =jpeg_rp >> 2; // to words
00087     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);
00088 // now let's try mmap itself
00089 
00090 D0(sleep(5));
00091    ccam_dma_buf = (unsigned long *) mmap(0, buff_size, PROT_READ, MAP_OPTIONS, buff_fd, 0);
00092 //   ccam_dma_buf = (unsigned long *) mmap(0, buff_size, PROT_READ, MAP_SHARED, buff_fd, 0);
00093 
00094    if((int)ccam_dma_buf == -1) {
00095      printf("Error in mmap %s\r\n",BuffFileName);
00096      close(buff_fd);
00097      return -1;
00098    }
00099 // display a portion of the buffer around the jpeg_wp
00100    printf ("[] shows the value pointed by the last frame's DMA address pointer, <> - length of the last frame,\n" );
00101    alen=jpeg_wp-9; if (alen<0) alen+= (buff_size>>2);
00102    i0=(jpeg_wp>>3)<<3;
00103 D0(sleep(5));
00104    for (i=(i0-64);i<(i0+32);i++) {
00105      a=i;
00106      if (i<0) a+=(buff_size>>2);
00107      else if (i>=(buff_size>>2)) a-=(buff_size>>2);
00108      if ((i & 7)==0) printf("\n%06x: ",a);
00109      if      (a==alen) printf("<%08lx>",ccam_dma_buf[a]);
00110      else if (a==jpeg_wp) printf("[%08lx]",ccam_dma_buf[a]);
00111      else                 printf(" %08lx ",ccam_dma_buf[a]);
00112    }
00113    printf("\n");
00114 D0(sleep(5));
00115 
00116 // let's get the last frame here
00117    jpeg_len=ccam_dma_buf[alen] & 0xffffff; 
00118    tim=ccam_dma_buf[alen-1];
00119    tim=ccam_dma_buf[alen-2]+(0.000001*tim);
00120 //   jpeg_start=(alen & 0xfffffff8)- (((jpeg_len+7) & 0xffffffe0)>>2);
00121    jpeg_start=(alen & 0xfffffff8)- (((jpeg_len + CCAM_MMAP_META + 3) & 0xffffffe0)>>2);
00122    if (jpeg_start<0) jpeg_start+=(buff_size>>2);
00123    printf ("JPEG length=%d (0x%x)\n",jpeg_len,jpeg_len);
00124    printf ("timestamp=%f\n",tim);
00125    for (i=(jpeg_start-16);i<(jpeg_start+16);i++) {
00126      a=i;
00127      if (i<0) a+=(buff_size>>2);
00128      else if (i>=(buff_size>>2)) a-=(buff_size>>2);
00129      if ((i & 7)==0) printf("\n%06x: ",a);
00130      if      (a==jpeg_start) printf("{%08lx}",ccam_dma_buf[a]);
00131      else                 printf(" %08lx ",ccam_dma_buf[a]);
00132    }
00133    printf("\n");
00134 
00135    if (jpeg_rp>=0) {
00136    printf("\n");
00137      for (i=(jpeg_rp-16);i<(jpeg_rp+8);i++) {
00138        a=i;
00139        if (i<0) a+=(buff_size>>2);
00140        else if (i>=(buff_size>>2)) a-=(buff_size>>2);
00141        if ((i & 7)==0) printf("\n%06x: ",a);
00142        if      (a==jpeg_rp) printf("(%08lx)",ccam_dma_buf[a]);
00143        else                 printf(" %08lx ",ccam_dma_buf[a]);
00144      }
00145      printf("\n");
00146    }
00147 
00148 D0(sleep(5));
00149    l= munmap(ccam_dma_buf, buff_size);
00150 D0(sleep(5));
00151    printf ("munmap(0, %d) returned %d\n",buff_size,l);
00152 D0(sleep(5));
00153 
00154    close (buff_fd);              
00155 D0(sleep(5));
00156    return 0;
00157 }
00158 

Generated on Thu Aug 7 16:18:59 2008 for elphel by  doxygen 1.5.1