📄 driver_test.c
字号:
/* Sean Haycock 26/03/2009User application to test 'diosis' device driver. Sets up a signal handler so that when a SIGIO signal (sent from the diopsis driver, triggered when a genlock pulse arrives at ioport) has been received will read the encoder data at theioport of the diopsis driver, then output this value on screen and to an output file16/04/2009 SH added - mmap function so that user can access data in enc_buf*/#include <stdio.h>#include <signal.h>#include <sys/types.h>#include <sys/socket.h>#include <sys/un.h>#include <fcntl.h>#include <unistd.h>#include <stdio.h>#include <sys/mman.h>#include <errno.h>#define SIZE sizeof(int)void my_handler(int sig); /* function that is triggered when a SIGIO signal is received */int my_read(void); /* read encoder data from driver */int my_write(void); /* write encoder data to output file */char enc_buff_local[30]; /* buffer to store encoder data locally */int data;/* pointers to memory allocation for driver *//* encoder channel 0 */char *kadr_ptr_0; /* pointer to address of kmalloc in driver */char shrd_mem_buf_0[100]; /* buffer to store value in driver buffer *//* encoder channel 1 */char *kadr_ptr_1;char shrd_mem_buf_1[100];/* encoder channel 2 */char *kadr_ptr_2;char shrd_mem_buf_2[100];/* encoder channel 3 */char *kadr_ptr_3;char shrd_mem_buf_3[100];/*------------------------------------------*/int sighandler_cnt = 0; /* singla handler counter - TEST purpose */int main(){ int fd; /* file descriptor */ int flags; /* fcntl command flags */ int len = 16 * getpagesize(); fd= open("/dev/diopsis" ,O_RDWR); /*------------------------------------------------------------------ | Mapping Shared Memory from Kernel space to User space | |-----------------------------------------------------------------*/ /*---- shared memory for channel 0 encoder data -------------------*/ kadr_ptr_0 = mmap(0,len, PROT_READ , MAP_FILE | MAP_SHARED, fd,0); if(kadr_ptr_0 == MAP_FAILED) { printf(" < mmap ptr_0 = %d > \n", (int) kadr_ptr_0); printf("error: %s\n", strerror(errno)); close(fd); return 0; } /*---- shared memory for channel 1 encoder data -------------------*/ kadr_ptr_1 = mmap(0,len, PROT_READ , MAP_FILE | MAP_SHARED, fd,len); if(kadr_ptr_1 == MAP_FAILED) { printf(" < mmap ptr_1 = %d > \n", (int) kadr_ptr_1); printf("error: %s\n", strerror(errno)); close(fd); return 0; } /*---- shared memory for channel 2 encoder data -------------------*/ kadr_ptr_2 = mmap(0,len, PROT_READ , MAP_FILE | MAP_SHARED, fd,2*len); if(kadr_ptr_2 == MAP_FAILED) { printf(" < mmap ptr_2 = %d > \n", (int) kadr_ptr_2); printf("error: %s\n", strerror(errno)); close(fd); return 0; } /*---- shared memory for channel 3 encoder data -------------------*/ kadr_ptr_3 = mmap(0,len, PROT_READ , MAP_FILE | MAP_SHARED, fd,3*len); if(kadr_ptr_3 == MAP_FAILED) { printf(" < mmap ptr_3 = %d > \n", (int) kadr_ptr_3); printf("error: %s\n", strerror(errno)); close(fd); return 0; }/*--------------------------------------------------------------------------------*/ /* catches any SIGIO sent, implements 'my_handler' function when signalled */ signal(SIGIO, my_handler); /* enabling asynchronous notification */ fcntl(fd, F_SETOWN, getpid()); /* necassary for kernel to know who to notify */ flags = fcntl(fd, F_GETFL); /* set read flags */ fcntl(fd, F_SETFL, flags | FASYNC); /* need to set FASYNC flag to enable notification */ /* used for testing when interrupt function not used, so that a kill_fasync() can be used to send SIGIO (see driver for further information) comment when interrupt function working */// int p= fopen("/dev/diopsis","r"); //pause(); /* pauses code until awakened by SIGIO */ /* loop to print output, until SIGIO signalled, not implemented if pause() is uncommented */ while(1) { // printf("catching SIGIO\n"); }}/* signal handler - when SIGIO has been signalled by interrupt function of device driver, indicating that a pulse from the genlock has be seen */void my_handler(int sig){ //sighandler_cnt++; /* signal handler count- TEST purpose*/ //printf("signal handler cnt:%d\n", sighandler_cnt); printf("\nreceived SIGIO, number %d\n", sig); printf("\nGenlock singal received, reading encoder data function\n"); my_read(); /* read encoder data from driver */ /* copy driver buffers to user space */ memcpy(shrd_mem_buf_0,kadr_ptr_0, SIZE); memcpy(shrd_mem_buf_1 ,kadr_ptr_1, SIZE); memcpy(shrd_mem_buf_2 ,kadr_ptr_2, SIZE); memcpy(shrd_mem_buf_3 ,kadr_ptr_3, SIZE); /* show buffer data */ /* printf("data in kernel buffer (enc_buf_0) < %d > \n", *((int*)shrd_mem_buf_0)); printf("data in kernel buffer (enc_buf_1) < %d > \n", *((int*)shrd_mem_buf_1)); printf("data in kernel buffer (enc_buf_2) < %d > \n", *((int*)shrd_mem_buf_2)); printf("data in kernel buffer (enc_buf_3) < %d > \n", *((int*)shrd_mem_buf_3));*/// my_write(); /* output encoder data to file */ return 0; //exit(0); //"return 0" to loop back rather than exit}/* read function to read encoder data from ioport */int my_read(void){ FILE *driver_test= fopen("/dev/diopsis","r"); printf("Program opening /dev/diopsis...\n"); printf("reading from /dev/diopsis \n"); /* read data from driver, store in 'enc_buff_local' */ data = fread(enc_buff_local,1,sizeof(enc_buff_local), driver_test); enc_buff_local[data]= '\0'; printf("bytes read: %d\n", data); printf("encoder data : < %s >\n", enc_buff_local); printf("...program closing /dev/diopsis\n"); fclose(driver_test);}/* write function to output encoder data to file */int my_write(void){ FILE *data_file= fopen("enc_data","w"); printf("\nProgram opening /enc_data...\n"); /* write encoder data stored in 'enc_buff' to file */ fwrite(enc_buff_local, 1, strlen(enc_buff_local),data_file); printf("encoder data writen to /enc_data...\n\n"); fclose(data_file);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -