IPMUX1IPMUX2IPMUX3: opl_regmmap_test.c源文件

RTL96xx (RTL9607,...)

opl_regmmap_test.c

浏览该文件的文档。
00001 
00014 #include <stdlib.h>
00015 #include <sys/mman.h>
00016 #include <fcntl.h>
00017 
00018 #include "../include/opl_typedef.h"
00022 /*--------------------------Macro definition------------------------- */
00023 #define OPL_REG_DEVICE "/dev/opl_reg"
00024 #define OPL_REG_SIZE 0x300000
00025 
00026 
00027 
00028 
00034 /*--------------------------type definition------------------------- */
00035 
00036 
00037 
00038 
00039 
00045 /*-----------------global varible/function declaration--------------- */
00046 
00047 
00048 
00049 
00050 
00056 /*-------------------local  function declaration---------------------- */
00057 
00058 
00059 
00060 
00061 
00068 /*--------------local  variable declaration and definition------------- */
00069 u32 reg_read(char *reg_base,int offset);
00070 void reg_write(char *reg_base, int offset, u32 value);
00071 void opl_reg_main(int argc,char ** argv);
00072 int opl_reg_init(int *opl_reg_initialized);
00073 int opl_reg_exit(int *opl_reg_initialized);
00074 
00075 
00076 
00083 /*------------------global variable and function exported-------------- */
00085 char *opl_reg_base = NULL;
00086 char *OPL_REG_USAGE = "opl_reg_test -r offset  or\n\
00087                        opl_reg_test --read offset  or\n\
00088                        opl_reg_test -w offset value   or\n\
00089                        opl_reg_test --write offset value  \n\
00090                        Note: the offset and value can be decimal or hex!\n" ;
00091 
00092 
00094 int opl_reg_fd;
00095 
00103 /*----------------------local  function definition--------------------- */
00104 
00115 u32 reg_read(char *reg_base,int offset)
00116 {
00117   u32 value = 0;
00118   if(offset%4){
00119     printf("the alignment is not 4 bytes \n");
00120     return -1;
00121   }
00122   value = *(volatile u32 *)(reg_base + offset);
00123   return value;
00124 }
00134 void reg_write(char *reg_base, int offset, u32 value)
00135 {
00136   if(offset%4){
00137     printf("the alignment is not 4 bytes \n");
00138     return ;
00139   } 
00140   *(volatile u32 *)(reg_base + offset) = value;
00141 }
00151 void opl_reg_main(int argc,char ** argv)
00152 {
00153   int i = 0;
00154   int offset = 0;
00155   u32 value = 0;
00156     
00157   if(argc < 3){
00158     printf("the number of argument is not correct\n");
00159     printf(OPL_REG_USAGE);
00160     return;
00161   }
00162   
00163   if((argv[2][0] == '0') && ((argv[2][1] == 'x')||(argv[2][1] == 'X'))){
00164     /* TBD:should be check the ranged */
00165     sscanf(argv[2],"%x",&offset);
00166   }else{
00167     sscanf(argv[2],"%d",&offset);
00168   }
00169      
00170   if(!strcmp(argv[1],"--read")||!strcmp(argv[1], "-r")){
00171     if(argc != 3){
00172       printf("the number of argument is not correct\n");
00173       printf(OPL_REG_USAGE);
00174       return;
00175     }
00176     printf("the %x register's value is 0x%x\n",offset,reg_read(opl_reg_base,offset));
00177     
00178   }else if(!strcmp(argv[1],"--write")||!strcmp(argv[1], "-w")){
00179     if(argc != 4){
00180       printf("the number of argument is not correct\n");
00181       printf(OPL_REG_USAGE);
00182       return;
00183     }
00184     if(argv[3][0] == '0' && ((argv[3][1] == 'x')||(argv[3][1] == 'X'))){
00185       /* TBD:should be check the ranged */
00186       sscanf(argv[3],"%x",&value);
00187     }else{
00188       sscanf(argv[3],"%d",&value);
00189     }
00190     reg_write(opl_reg_base,offset,value);
00191     printf("write the %x register,write_value = 0x%x,check_value = 0x%x\n",offset,value,reg_read(opl_reg_base,offset));
00192   }
00193 }
00194 
00203 int opl_reg_init(int *opl_reg_initialized)
00204 {
00205   opl_reg_fd = open(OPL_REG_DEVICE,O_RDWR);
00206   if(opl_reg_fd<0){
00207     printf("open device file failed\n",OPL_REG_DEVICE);
00208     return -1;
00209   }
00210   opl_reg_base = (char *)mmap(0,OPL_REG_SIZE,PROT_READ|PROT_WRITE,MAP_SHARED,opl_reg_fd,0);
00211   if(!opl_reg_base){
00212     printf("mmap the opl register failed\n");
00213     close(opl_reg_fd);
00214     return -1;
00215   }
00216   *opl_reg_initialized = 1;
00217   return 0;
00218 }
00227 int opl_reg_exit(int *opl_reg_initialized)
00228 {
00229   int ret = 0;
00230   if(*opl_reg_initialized == 1){
00231     *opl_reg_initialized = 0;
00232     munmap(opl_reg_base,OPL_REG_SIZE);
00233     ret = close(opl_reg_fd);
00234   }
00235   return ret;
00236 }
00247 int main(int argc, char **argv)
00248 {
00249   int init = 0;
00250   opl_reg_init(&init);
00251   if(init){
00252     opl_reg_main(argc,argv);
00253   }
00254   opl_reg_exit(&init);
00255   return 0;
00256 }
00257 

Generated at Mon Jul 30 16:43:48 2007 for IPMUX1IPMUX2IPMUX3 by  doxygen 1.3.9.1