⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 commands.cpp

📁 linux下vme总线驱动代码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
//---------------------------------------------------------------------------//title: VME Debugger for XVME-655 //version: Linux 1.1//date: February 1998//designer: Michael Wyrick                                                      //programmer: Michael Wyrick//project: VMELinux Project in association with Chesapeake Research//platform: Linux 2.2.x, 2.4.x//language: GCC 2.95, GCC 3.0//module: //------------------------------------------------------------------------------  //  Purpose:                                                                    //  Docs:                                                                       //------------------------------------------------------------------------------  #include <stdlib.h>#include <stdio.h>#include <fcntl.h>#include <unistd.h>#include <string.h>#include <sys/ioctl.h>#include <asm/io.h>#include <malloc.h>#include "unilib.h"#include "commands.h"#include "universe.h"#include "vmeutils.h"#define CMDT_NULL  0#define CMDT_FUNC  1#define CMDT_MODC  2#define CMDT_MODI  3#define CMDT_MODL  4#define CMDT_MODB  5#define DEFCNT 128// Size of a DMA write file transfer#define WRITEBUFSIZE    1024int VMEspace = 1;        // 1 = A16, 2 = A24, 3 = A32char TestValueC = 0;int  TestValueI = 0;long TestValueL = 0;long FixedAdder = 0;long VME_Valid_Base = 0;long VME_Valid_Cnt  = 0;typedef void (*func)(char *,int);typedef struct {  int  CmdType;  char *command;  int  NumParams;  void *pointer;} COMMAND;COMMAND cmdTable[] = {  {CMDT_FUNC,"QUIT",0,(void*)quit},  {CMDT_FUNC,"Q",0,(void*)quit},  {CMDT_FUNC,"EXIT",0,(void*)quit},  {CMDT_FUNC,"E",0,(void*)quit},  {CMDT_FUNC,"HELP",0,(void*)help},  {CMDT_FUNC,"?",0,(void*)help},  {CMDT_FUNC,"STATUS",0,(void*)status},  {CMDT_FUNC,"REGS",0,(void*)regs},  {CMDT_FUNC,"CLS",0,(void*)clear},  {CMDT_FUNC,"D",2,(void*)Display},  {CMDT_FUNC,"R",2,(void*)Display},  {CMDT_FUNC,"RB",2,(void*)Display},  {CMDT_FUNC,"T",2,(void*)DisplayMonitor},  {CMDT_FUNC,"DW",2,(void*)DisplayWord},  {CMDT_FUNC,"RW",2,(void*)DisplayWord},  {CMDT_FUNC,"DL",2,(void*)DisplayLong},  {CMDT_FUNC,"RL",2,(void*)DisplayLong},  {CMDT_FUNC,"W",2,(void*)Write},  {CMDT_FUNC,"WB",2,(void*)Write},  {CMDT_FUNC,"WW",2,(void*)WriteWord},  {CMDT_FUNC,"WL",2,(void*)WriteLong},  {CMDT_FUNC,"RF",2,(void*)ReadFile},  {CMDT_FUNC,"WF",2,(void*)WriteFile},  {CMDT_FUNC,"WSRF",2,(void*)WriteSRecordsFile},  {CMDT_FUNC,"SEEK",1,(void*)Seek},  {CMDT_FUNC,"REGR",1,(void*)ReadReg},  {CMDT_FUNC,"REGW",2,(void*)WriteReg},  {CMDT_FUNC,"MAP",1,(void*)vmemap1},           {CMDT_FUNC,"TESTWRITE",1,(void*)TestWrite},           {CMDT_FUNC,"TESTREAD",1,(void*)TestRead},           {CMDT_FUNC,"TESTDMA",1,(void*)TestDMA},           {CMDT_FUNC,"TESTPROGRAMMED",1,(void*)TestProgrammed},           {CMDT_FUNC,"WINT", 1, (void*)wint},  {CMDT_MODL,"FA",1,&FixedAdder},           {CMDT_NULL,"",0,NULL}               // End of Table};  unsigned long lastpnt = 0; unsigned long VMEMapping = 0; extern int quiet;extern int default_mode;extern int binary;/*------------------------------------------------------------------------------ *  * *------------------------------------------------------------------------mjw-*/void clear(char *b,int l){}/*------------------------------------------------------------------------------ * ValidAddress *   return 1 if the ptr is in the current Mapping *------------------------------------------------------------------------mjw-*/int ValidAddress(long ptr){  if ((ptr >= VME_Valid_Base) &&       (ptr < VME_Valid_Base + VME_Valid_Cnt))    return(1);  else {    printf("  An attempt was made to access an invalid address. %0lX",ptr);    return(0);  }  }//----------------------SREC-------------#define ERROR 1#define BUFFER_SIZE             4096#define BYT(cp)                 ((Asc2Hex(cp[0]) << 4) | Asc2Hex(cp[1]))static FILE *infile;static char *buffer;static char *goffset;/*-------------------------------------------------------------------------*//*  Asc2Hex - convert a two byte ascii string to a byte                    *//*-------------------------------------------------------------------------*/static char Asc2Hex(char byt){  if ((byt < 0x3A) && (byt > 0x2F))    return(byt & 0x0F);  if ((byt < 'G') && (byt > '@'))    return((byt & 0x0F) + 9);  if ((byt < 'g') && (byt > 0x60))    return((byt & 0x0F) + 9);  return(0xEE);  /* Return Error */}/*-------------------------------------------------------------------------*//*  ComputeAddr - Get address from S Record                                *//*-------------------------------------------------------------------------*/static unsigned long ComputeAddr(char *cp, int siz){  unsigned long retval;  for (retval = 0L; siz > 0; siz--, cp += 2)    retval = (retval << 8) | BYT(cp);  return(retval);}/*-------------------------------------------------------------------------*//*  checkwrite - Write a word to memory and read it back to check that it  *//*               wrote correctly.                                          *//*---------------------------------------------------------------------mjw-*/void checkwrite(unsigned long a,unsigned char val){   int error;  wb(a,val,&error);  if (error)    printf("BUS Error during Write at address %08lX\n",a);}/*-------------------------------------------------------------------------*//*  DecodeS1 - Decode and place in memory S1 type records                  *//*     Checks for correct checksum value and will not write if incorrect   *//*---------------------------------------------------------------------mjw-*/static void DecodeS1(char *cp, long offset,long skip){  long address;  unsigned short val;   unsigned char *datastart, cnt, cntr, chksum;  unsigned long addr;  cnt = BYT(cp) - 3;          /* Get Length of Line */  chksum = BYT(cp);  cp += 2;  addr = ComputeAddr(cp,2) * skip;  printf("Address %lX\n",addr);  address = (addr + (unsigned long) goffset);  chksum += BYT(cp);          /* Checksum 1st address byte */  cp += 2;  chksum += BYT(cp);          /* Checksum 2nd address byte */  cp += 2;  datastart = (unsigned char *)cp;  /* Data starts here */  /* Calc rest of checksum please */  for (cntr = 0; cntr <= cnt; cntr++, cp += 2)    chksum += BYT(cp);  /* If checksum not valid, skip this line */  if (chksum != (unsigned char) 0xFF) {    printf("Checksum failed - Got %X\n",chksum);    return;   }  /* Every thing is cool, write data to memory */  for (cntr = 0; cntr < cnt; cntr++, datastart+=2, address+=skip) {    val = BYT(datastart);    checkwrite(address+offset,val);  }}/*-------------------------------------------------------------------------*//*  dsp_srec                                                               *//*      int srec(char *filename, char *offset)                             *//*---------------------------------------------------------------------mjw-*/int dsp_srec(char *filename, long offset, long skip){  char *cp;  int flag, pcount;  int count = 0;  int errcount = 0;  int scount[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};  buffer = (char *)malloc(BUFFER_SIZE);  if (buffer == (char *) 0) {    printf("\n\nLoad error: unable to get 4096 bytes of memory.\n\n\n");    return(ERROR);  }  infile = fopen(filename, "r");  if (infile == (FILE *) 0) {    printf("\n\nLoad error: unable to open file\n\n\n");    free(buffer);    return(ERROR);  }  flag = pcount = 0;  while ((fgets(buffer, BUFFER_SIZE, infile) != (char *) 0) && (flag == 0)) {    cp = strtok(buffer, " \t\f\n");    if (cp == (char *) 0)      continue;    if (cp[0] != 'S') {      errcount++;      continue;    }    count++;    cp++;           /* point to the record type */    switch (*cp) {    case '0':       /* optional introduction record. */      scount[0]++;      break;    case '1':       /* address count is 2 bytes long */      scount[1]++;      DecodeS1(++cp,offset,skip);      break;    case '2':       /* address count is 3 bytes long */      scount[2]++;      break;    case '3':       /* address is 4 bytes long */      scount[3]++;      break;    case '9':       /* End-Of-File record */      scount[4]++;      flag = 1;      break;    } /* switch */  } /* while */  fclose(infile);  free(buffer);  printf("\n%d lines processed\n",count);  printf("S0 - %d\nS1 - %d\nS2 - %d\nS3 - %d\nS9 - %d\n\n\n",         scount[0], scount[1], scount[2], scount[3], scount[9]);  return(count);}//----------------------SREC-------------void WriteSRecordsFile(char *b,int l){  int num;  long pnt,skip;  char fname[255];//  num = sscanf(b,"%lx %s %lx",&pnt,&fname,&skip);  num = sscanf(b,"%lx %s %lx",&pnt,fname,&skip);  pnt += FixedAdder;  if ((num < 2) || (num > 3)) {    printf("Usage: WriteSRecordFile address filename [skip]");  } else {    if (num == 2)      skip = 1;    if (ValidAddress(pnt)) {      dsp_srec(fname,pnt,skip);    }  }  }/*------------------------------------------------------------------------------ * WriteFile * *------------------------------------------------------------------------mjw-*/void WriteFile(char *b,int l){  int num,c;  long pnt;  char fname[255];  FILE *fin;  int error,n,num_written;  unsigned int *dma_buffer;  num = sscanf(b,"%lx %s",&pnt,fname);  pnt += FixedAdder;  if (num != 2) {    printf("Usage: WriteFile address filename");  } else {    if (ValidAddress(pnt)) {      fin = fopen(fname,"rb");      if (!fin) {        printf("Can't open file %s\n",fname);      } else {        // Programmed IO        if (default_mode == MODE_PROGRAMMED) {          while ((c = getc(fin)) != EOF) {            wb(pnt++,c,&error);            if (error) {              printf("Bus Error during Writefile.\n");              continue;            }          }          if (!quiet)            printf("Done Writing file to Memory");        } else {          // DMA          dma_buffer = (unsigned int *)malloc(WRITEBUFSIZE);          ioctl(vme_handle,IOCTL_SET_MODE,MODE_DMA);          lseek(vme_handle,pnt,SEEK_SET);          n = 1;   // Start the while loop on a good note please          while (n) {            // get data from file            n = fread(dma_buffer,1,WRITEBUFSIZE,fin);            // Do The DMA Transfer            if (n > 0) {              num_written = write(vme_handle,dma_buffer,n);              if (num_written < 1) {                printf("BUS Error during Write file DMA Transfer\n");              }            }          }          free(dma_buffer);          ioctl(vme_handle,IOCTL_SET_MODE,MODE_PROGRAMMED);        }        }    }  }  }/*------------------------------------------------------------------------------ * Display  *   usage: display, addr, number of times *------------------------------------------------------------------------jsh-*/void DisplayMonitor(char *b, int l){  unsigned int x;  for (x=0;x<200;x++) {    Display(b, 1);    printf("   %02X\n",l);  }}/*------------------------------------------------------------------------------ * Display  *   usage: display addr count *------------------------------------------------------------------------mjw-*/void Display(char *b,int l){  unsigned char *dma_buffer;  long pnt;  unsigned int count,x,num,n,a[16],y;  int error;  unsigned v;  num = sscanf(b,"%lx %x",&pnt,&count);  pnt += FixedAdder;  if (num < 1) {    pnt = lastpnt;    num = 1;  }  if (num == 1) {    count = DEFCNT;    num = 2;  }  if (num != 2) {    printf("Usage: d address [count]");  } else {    if (ValidAddress(pnt)) {      if (default_mode == MODE_PROGRAMMED) {        lastpnt = pnt + count;        for (x=0;x<count;x++) {          if (!binary) {            if (x % 16 == 0)              printf("%08lX: ",x+pnt);            if (x % 16 == 8)              printf("- ");            v = rb(pnt+x,&error);            if (!error) {              printf("%02X ",v);              a[x % 16] = v;            } else {              printf("** ");              a[x % 16] = 0;            }            // Show the character values to the right.            if (x % 16 == 15) {              printf(": ");              for (y=0;y<16;y++) {                if ( (a[y] > 31) && (a[y] < 127) )                  printf("%c",a[y]);                else                  printf(".");              }              printf("\n");            }          } else {            v = rb(pnt+x,&error);              if (!error)              printf("%c",v);            else              printf("*");          }                                         }        } else {  // end Mode Programmed        if (!quiet)          printf("--> Doing Read via DMA <--\n");        dma_buffer = (unsigned char *)malloc(count);        lastpnt = pnt + count;        // Do The DMA Transfer        ioctl(vme_handle,IOCTL_SET_MODE,MODE_DMA);        lseek(vme_handle,pnt,SEEK_SET);        n = read(vme_handle,dma_buffer,count);        ioctl(vme_handle,IOCTL_SET_MODE,MODE_PROGRAMMED);        if (n < 1) {          printf("BUS Error\n");        } else {          // Print the Results          for (x=0;x<n;x++) {            if (!binary) {              if (x % 16 == 0)                printf("%08lX: ",x+pnt);              if (x % 16 == 8)                printf("- ");              printf("%02X ",(unsigned char)dma_buffer[x]);              if (x % 16 == 15)                printf("\n");            } else {              printf("%c",(unsigned char)dma_buffer[x]);            }            }          }          free(dma_buffer);      } // end Mode DMA    }  }  }   /*------------------------------------------------------------------------------ * DisplayWord  *   usage: display addr count

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -