]> jspc29.x-matter.uni-frankfurt.de Git - trbnettools.git/commitdiff
updates
authorhadaq <hadaq>
Wed, 10 Jun 2009 16:55:37 +0000 (16:55 +0000)
committerhadaq <hadaq>
Wed, 10 Jun 2009 16:55:37 +0000 (16:55 +0000)
.

libtrbnet/pulser.c
libtrbnet/trbcmd.c
libtrbnet/trberror.c
libtrbnet/trberror.h
libtrbnet/trbnet.c
libtrbnet/trbnet.h

index 9c57a6b56ebf98e08f70fa7af89565d6d00a4286..1781302cd0acf41cd08e91079d1da0c3e29c3652 100644 (file)
@@ -36,19 +36,24 @@ static void sigHandler(int sig)
 
 void usage(const char *progName)
 {
-  printf("Usage: %s [-h] [-d level] [-f outFileName] [-n numEvents]\n",
+  printf("Usage: %s [-h] [-d level] [-f outFileName] [-n numEvents] "
+         "[-t triggerType] [-i input]\n",
          progName);
   printf("Options:\n");
   printf("  -h    give this help\n");
   printf("  -d    turn on Debugging Information\n");
-  printf("  -f    outFileName\n");
+  printf("  -f    write to outFileName\n");
   printf("  -n    process numEvents triggers\n");
+  printf("  -t    send triggerType (default 0)\n");
+  printf("  -i    use triggerInput input(default 0)\n");
 }
 
 int main(int argc, char ** argv)
 {
   char hldFileName[256] = "pulser.hld";
   uint16_t trgNumber = 0;
+  uint8_t triggerType = 0;
+  uint8_t input = 0;
   unsigned int numEvts = UINT_MAX;
   uint32_t buffer[bufferSize];
   sigset_t blockSet;
@@ -59,7 +64,7 @@ int main(int argc, char ** argv)
   trb_debug = 0;
 
   /* Parse Arguments */
-  while ((i = getopt(argc, argv, "+hd:f:n:")) != -1) {
+  while ((i = getopt(argc, argv, "+hd:f:n:t:i:")) != -1) {
     switch (i) {
     case '?':
       usage(basename(argv[0]));
@@ -76,6 +81,12 @@ int main(int argc, char ** argv)
     case 'n':
       numEvts = strtoul(optarg, NULL, 0);
       break;
+    case 't':
+      triggerType = (uint8_t)strtoul(optarg, NULL, 0);
+      break;
+    case 'i':
+      input = (uint8_t)strtoul(optarg, NULL, 0);
+      break;
     default:
       break;
     }
@@ -110,12 +121,13 @@ int main(int argc, char ** argv)
     sigprocmask(SIG_BLOCK, &blockSet, NULL);
     
     /* Send Trigger and get Data */
-    if (trb_send_trigger_rich(0, 0, 0xcc, 0xdd, 0xaabb) == -1) {
-      fprintf(stderr, "Error send_trigger\n");
+    if (trb_send_trigger_rich(input, triggerType, 0xcc, 0xdd, 0xaabb) == -1) {
+      trb_error("Error send_trigger");
       exit(EXIT_FAILURE);
     }
     
-    size = trb_ipu_data_read(0, 0xcc, 0xdd, 0xaabb, buffer, bufferSize);
+    size = trb_ipu_data_read(triggerType, 0xcc, 0xdd, 0xaabb, 
+                             buffer, bufferSize);
     if (size == -1) {
       fprintf(stderr, "Error IPU Read\n");
       exit(EXIT_FAILURE);
index 7134d3c6d31a01695a04e7d12ee9f7e2b2eab649..a1888444815b15f911b82437685af66570311e17 100644 (file)
@@ -21,9 +21,11 @@ static int hexMode = HEXMODE;
 
 void usage(const char *progName)
 {
-  printf("Usage: %s [-h] [-n number] [-d level] [-H] <COMMAND>\n", progName);
+  printf("Usage: %s [-h] [-f script-file] [-n number] [-d level] "
+         "[-H] <COMMAND>\n", progName);
   printf("Options:\n");
   printf("  -h    give this help\n");
+  printf("  -f    execute commands given in script-file\n");
   printf("  -n    repeat COMMAND number times, -1 = endless loop\n");
   printf("  -d    turn on Debugging Information\n");
   printf("        level 1: TRB_Package debugging\n");          
@@ -31,8 +33,8 @@ void usage(const char *progName)
   printf("  -D    FIFO DMA-Mode\n");
   printf("  -l    lazy-mode: skip most consistency-checks of packages\n");
   printf("  -H    hex-mode: all following arguments will be interpreted "
-         "as hexadecimal\n        numbers\n");
-  printf("Commands:\n");
+         "as hexadecimal-numbers\n");
+  printf("\nCommands:\n");
   printf("   r <trbaddress> <register>                  -> read register\n");
   printf("   w <trbaddress> <register> <data>           -> write register\n");
   printf("   rm <trbaddress> <register> <size> <mode>   -> read "
@@ -55,19 +57,28 @@ void usage(const char *progName)
          "  \n");
 }
 
+#define CMD_SIZE 256
+#define CMD_MAX_NUM 10
+#define READ_MEM_SIZE 1048576
+
 int main(int argc, char ** argv)
 {
+  FILE *scriptFile = NULL;
+  char scriptFileName[256] = "";
+  char cmd[CMD_MAX_NUM][CMD_SIZE];
+  char *cmdLine = NULL;
+  unsigned int cmdLen = 0;
   uint16_t trb_address = 0;
   uint16_t reg_address = 0; 
-  int cmdCtr = 0;
-  int repeat = 1;
+  int loop = 1;
+  int loopCtr = 0;
   int i;
   
   trb_debug = 0;
   trb_lazy = 0;
 
   /* Parse Arguments */
-  while ((i = getopt(argc, argv, "+hn:d:DlH")) != -1) {
+  while ((i = getopt(argc, argv, "+hf:n:d:DlH")) != -1) {
     switch (i) {
     case '?':
       usage(basename(argv[0]));
@@ -75,8 +86,11 @@ int main(int argc, char ** argv)
     case 'h':
       usage(basename(argv[0]));
       exit(EXIT_SUCCESS);
+    case 'f':
+      strncpy(scriptFileName, optarg, 256);
+      break;
     case 'n':
-      repeat = strtol(optarg, NULL, 0);
+      loop = strtol(optarg, NULL, 0);
       break;
     case 'd':
       trb_debug = strtoul(optarg, NULL, 0);
@@ -95,459 +109,637 @@ int main(int argc, char ** argv)
     }
   }
   
-  /* open port */
-  init_ports();  
-  
-  if (argc - optind < 1) {
-    usage(basename(argv[0]));
-    exit(EXIT_FAILURE);
+  /* Open scriptFile if requested */
+  if (strlen(scriptFileName) > 0) {
+    if (strncmp(scriptFileName, "-", 256) == 0) {
+      scriptFile = stdin;
+      fprintf(stderr, "name: %s\n", "STDIN");
+    } else {
+      scriptFile = fopen(scriptFileName, "r");
+      if (scriptFile == NULL) {
+        perror("Error opening ScriptFile");
+        fprintf(stderr, "name: '%s'\n", scriptFileName);
+        exit(EXIT_FAILURE);
+      }
+    }
   }
+  
+  /* Open ports */
+  init_ports();  
 
-  while ((repeat == -1) || (cmdCtr++ < repeat)) {
-    if (strcmp(argv[optind], "w") == 0) {
+  /* Start repeat-loop */
+  while ((loop == -1) || (loopCtr++ < loop)) {
+    unsigned int lineCtr = 0;
+    ssize_t scriptStatus = 0;
+    
+    /* Start script-file-loop */
+    while (scriptStatus != -1) { 
+      if (scriptFile == NULL) {    
+        /* Get command from function-call */
+        unsigned int i;
+        cmdLen = argc - optind;
+        for (i = 0; (i < cmdLen) && (i < CMD_MAX_NUM); i++) {
+          strncpy(cmd[i], argv[optind + i], CMD_SIZE); 
+        }
+        scriptStatus = -1;
+      } else {
+        /* Get next command from file */
+        char *c = NULL;
+        size_t len = 0;
+        unsigned int i;
+      
+        lineCtr++;
+        /* Initialize */
+        for (i = 0; i < CMD_MAX_NUM; i++) {
+          cmd[i][0] = '\0';
+        }
+      
+        if ((scriptStatus = getline(&cmdLine, &len, scriptFile)) == -1) {
+          if (feof(scriptFile) != 0) {
+            /* EOF reached */
+            rewind(scriptFile);
+            continue;
+          } else {
+            /* Error reading line */
+            fprintf(stderr, "Error reading script-file\n");
+            exit(EXIT_FAILURE);
+          }
+        }
+          
+        /* Remove newline and comments */
+        if ((c = strchr(cmdLine, '\n')) != NULL) {
+          *c = '\0';
+        }
+        if ((c = strchr(cmdLine, '#')) != NULL) {
+          *c = '\0';
+        }
+
+        /* Split up cmdLine */
+        sscanf(cmdLine, "%s %s %s %s %s %s %s %s %s %s", 
+               cmd[0], cmd[1], cmd[2], cmd[3], cmd[4],
+               cmd[5], cmd[6], cmd[7], cmd[8], cmd[9]);
+      
+        for (i = 0, cmdLen = 0; i < CMD_MAX_NUM; i++, cmdLen++) {
+          if (cmd[i][0] == '\0') break;
+        }
+        if (cmdLen == 0) {
+          /* Empty Line */
+          continue;
+        }
+        printf("#Line %d:  %s\n", lineCtr, cmdLine);
+      }
+
+      if (strncmp(cmd[0], "w", CMD_SIZE) == 0) {
     
-      /*******************************************/
-      /* Register Write                          */
-      /*******************************************/
+        /*******************************************/
+        /* Register Write                          */
+        /*******************************************/
     
-      uint32_t value = 0;
+        uint32_t value = 0;
     
-      if (argc - optind !=  4) {
-        usage(basename(argv[0]));
-        exit(EXIT_FAILURE);
-      }
+        if (cmdLen !=  4) {
+          if (scriptFile != NULL) {
+            fprintf(stderr, "Line #%d: Invalid command\n", lineCtr);
+          } else {
+            usage(basename(argv[0]));
+          }
+          exit(EXIT_FAILURE);
+        }
     
-      trb_address = strtoul(argv[optind + 1], NULL, hexMode == 1 ? 16 : 0);
-      reg_address = strtoul(argv[optind + 2], NULL, hexMode == 1 ? 16 : 0);
-      value = strtoul(argv[optind + 3], NULL, hexMode == 1 ? 16 : 0);
+        trb_address = strtoul(cmd[1], NULL, hexMode == 1 ? 16 : 0);
+        reg_address = strtoul(cmd[2], NULL, hexMode == 1 ? 16 : 0);
+        value = strtoul(cmd[3], NULL, hexMode == 1 ? 16 : 0);
     
-      /* DEBUG Info */
-      if (trb_debug > 0) {
-        fprintf(stderr, 
-                "Command: WRITE: trb_address: 0x%04x, reg_address: 0x%04x, "
-                "value: 0x%08x\n", 
-                trb_address, reg_address, value);
-      }
+        /* DEBUG Info */
+        if (trb_debug > 0) {
+          fprintf(stderr, 
+                  "Command: WRITE: trb_address: 0x%04x, reg_address: 0x%04x, "
+                  "value: 0x%08x\n", 
+                  trb_address, reg_address, value);
+        }
 
-      if (trb_register_write(trb_address, reg_address, value) == -1) {
-        trb_error("write failed");
-        exit(EXIT_FAILURE);
-      }
-    } else if (strcmp(argv[optind], "r") == 0) {
+        if (trb_register_write(trb_address, reg_address, value) == -1) {
+          if (scriptFile != NULL) {
+            fprintf(stderr, "Line #%d: ", lineCtr);
+          }
+          trb_error("write_register failed");
+          exit(EXIT_FAILURE);
+        }
+      } else if (strncmp(cmd[0], "r", CMD_SIZE) == 0) {
 
-      /*******************************************/
-      /* Register Read                           */
-      /*******************************************/
+        /*******************************************/
+        /* Register Read                           */
+        /*******************************************/
 
-      int status = 0;
-      uint32_t data[256];
-    
-      if (argc - optind != 3) {
-        usage(argv[0]);
-        exit(EXIT_FAILURE);
-      }
+        int status = 0;
+        uint32_t data[256];
+    
+        if (cmdLen != 3) {
+          if (scriptFile != NULL) {
+            fprintf(stderr, "Line #%d: Invalid command\n", lineCtr);
+          } else {
+            usage(basename(argv[0]));
+          }
+          exit(EXIT_FAILURE);
+        }
     
-      trb_address = strtoul(argv[optind + 1], NULL, hexMode == 1 ? 16 : 0);
-      reg_address = strtoul(argv[optind + 2], NULL, hexMode == 1 ? 16 : 0);
+        trb_address = strtoul(cmd[1], NULL, hexMode == 1 ? 16 : 0);
+        reg_address = strtoul(cmd[2], NULL, hexMode == 1 ? 16 : 0);
 
-      /* DEBUG Info */
-      if (trb_debug > 0) {
-        fprintf(stderr, 
-                "Command: READ: trb_address: 0x%04x, "
-                "reg_address: 0x%04x\n",
-                trb_address, reg_address);
-      }
-    
-      status = trb_register_read(trb_address, reg_address, data, 256);
-      if (status == -1) {
-        trb_error("read failed");
-        exit(EXIT_FAILURE);
-      }
-    
-      for (i = 0; i < status; i += 2) {
-        fprintf(stdout, "%s%04x  %s%08x\n",
-                hexMode == 1 ? "" : "0x",
-                data[i], 
-                hexMode == 1 ? "" : "0x",
-                data[i + 1]);
-      }
-    } else if (strcmp(argv[optind], "rm") == 0) {
+        /* DEBUG Info */
+        if (trb_debug > 0) {
+          fprintf(stderr, 
+                  "Command: READ: trb_address: 0x%04x, "
+                  "reg_address: 0x%04x\n",
+                  trb_address, reg_address);
+        }
     
-      /*******************************************/
-      /* Register Read Memory                    */
-      /*******************************************/
+        status = trb_register_read(trb_address, reg_address, data, 256);
+        if (status == -1) {
+          if (scriptFile != NULL) {
+            fprintf(stderr, "Line #%d: ", lineCtr);
+          }
+          trb_error("read_register failed");
+          exit(EXIT_FAILURE);
+        }
     
-      uint32_t *data = NULL;
-      uint16_t size = 0;
-      uint8_t option = 0;
-      int status;
-      unsigned int i;
+        for (i = 0; i < status; i += 2) {
+          fprintf(stdout, "0x%04x  0x%08x\n",
+                  data[i], data[i + 1]);
+        }
+      } else if (strncmp(cmd[0], "rm", CMD_SIZE) == 0) {
+      
+        /*******************************************/
+        /* Register Read Memory                    */
+        /*******************************************/
+    
+        uint32_t *data = NULL;
+        uint16_t size = 0;
+        uint8_t option = 0;
+        int status;
+        const uint32_t* p;
+        const uint32_t* end = NULL;
+        unsigned int len;
+        unsigned int i;
    
-      if (argc - optind != 5) {
-        usage(argv[0]);
-        exit(EXIT_FAILURE);
-      }
+        if (cmdLen != 5) {
+          if (scriptFile != NULL) {
+            fprintf(stderr, "Line #%d: Invalid command\n", lineCtr);
+          } else {
+            usage(basename(argv[0]));
+          }
+          exit(EXIT_FAILURE);
+        }
     
-      trb_address = strtoul(argv[optind + 1], NULL, hexMode == 1 ? 16 : 0);
-      reg_address = strtoul(argv[optind + 2], NULL, hexMode == 1 ? 16 : 0);
-      size = strtoul(argv[optind + 3], NULL, hexMode == 1 ? 16 : 0);
-      option = strtoul(argv[optind + 4], NULL, hexMode == 1 ? 16 : 0);
-    
-      /* DEBUG Info */
-      if (trb_debug > 0) {
-        fprintf(stderr, 
-                "Command: READ_MEM: trb_address: 0x%04x, "
-                "reg_address: 0x%04x, "
-                "size: 0x%04x, "
-                "option: %d\n",
-                trb_address, reg_address, size, option);
-      }
+        trb_address = strtoul(cmd[1], NULL, hexMode == 1 ? 16 : 0);
+        reg_address = strtoul(cmd[2], NULL, hexMode == 1 ? 16 : 0);
+        size = strtoul(cmd[3], NULL, hexMode == 1 ? 16 : 0);
+        option = strtoul(cmd[4], NULL, hexMode == 1 ? 16 : 0);
+    
+        /* DEBUG Info */
+        if (trb_debug > 0) {
+          fprintf(stderr, 
+                  "Command: READ_MEM: "
+                  "trb_address: 0x%04x, "
+                  "reg_address: 0x%04x, "
+                  "size: 0x%04x, "
+                  "option: %d\n",
+                  trb_address, reg_address, size, option);
+        }
     
-      if ((data = malloc(sizeof(uint32_t) * size)) == NULL) abort();
+        if ((data = malloc(sizeof(uint32_t) * READ_MEM_SIZE)) == NULL) abort();
     
-      status = 
-        trb_register_read_mem(trb_address, reg_address, option, data, size); 
-      if (status == -1) {
-        trb_error("read_mem failed");
-        if (data != NULL) free(data);
-        exit(EXIT_FAILURE);
-      }
+        status = 
+          trb_register_read_mem(trb_address, reg_address, option, 
+                                size, data, READ_MEM_SIZE);
+        if (status == -1) {
+          if (scriptFile != NULL) {
+            fprintf(stderr, "Line #%d: ", lineCtr);
+          }
+          trb_error("read_register_mem failed");
+          if (data != NULL) free(data);
+          exit(EXIT_FAILURE);
+        }
         
-      for (i = 0; i < status; i++) { 
-        fprintf(stdout, "%s%04x  %s%08x\n", 
-                hexMode == 1 ? "" : "0x", reg_address + i,
-                hexMode == 1 ? "" : "0x", data[i]);
-      }
-    
-      if (data != NULL) free(data);
-    } else if (strcmp(argv[optind], "wm") == 0) {
-    
-      /*******************************************/
-      /* Register Write Memory                   */
-      /*******************************************/
-    
-      FILE *file = NULL;
-      uint32_t *data = NULL;
-      unsigned int dataSize = 64; 
-      char *buffer = NULL;
-      size_t len = 0;
-      char *fileName = NULL;
-      uint8_t option = 0;
-      unsigned int size = 0;
-      int status;
+        /* Print data-buffer */
+        p = data;
+        end = p + status;
+        while (p < end) {
+          len = (*p >> 16) & 0xffff;
+          fprintf(stdout, "0x%04x  0x%04x\n", (*p++) & 0xffff, len);
+          for (i = 0; (i < len) && (p < end); i++) { 
+            fprintf(stdout, "0x%04x  0x%08x\n", 
+                    reg_address + i, *p++);
+          }
+        }
+        
+        if (data != NULL) free(data);
+      } else if (strncmp(cmd[0], "wm", CMD_SIZE) == 0) {
+    
+        /*******************************************/
+        /* Register Write Memory                   */
+        /*******************************************/
+    
+        FILE *file = NULL;
+        uint32_t *data = NULL;
+        unsigned int dataSize = 64; 
+        char *line = NULL;
+        size_t len = 0;
+        char *fileName = NULL;
+        uint8_t option = 0;
+        unsigned int size = 0;
+        int status;
 
-      if (argc - optind != 5) {
-        usage(argv[0]);
-        exit(EXIT_FAILURE);
-      }
-    
-      trb_address = strtoul(argv[optind + 1], NULL, hexMode == 1 ? 16 : 0);
-      reg_address = strtoul(argv[optind + 2], NULL, hexMode == 1 ? 16 : 0);
-      option = strtoul(argv[optind + 3], NULL, hexMode == 1 ? 16 : 0);
-      fileName = argv[optind + 4];
-    
-      /* Open inputFile and read Data into buffer */
-      if (strcmp(fileName, "-") == 0) {
-        file = stdin;
-      } else {
-        file = fopen(fileName, "r");
-        if (file == NULL) {
-          perror("Error opening file");
+        if (cmdLen != 5) {
+          if (scriptFile != NULL) {
+            fprintf(stderr, "Line #%d: Invalid command\n", lineCtr);
+          } else {
+            usage(basename(argv[0]));
+          }
           exit(EXIT_FAILURE);
         }
-      }
     
-      if ((data = malloc(sizeof(uint32_t) * dataSize)) == NULL) abort();
-      while (getline(&buffer, &len, file) != -1) {
-        if (size >= dataSize) {
-          dataSize += 64;
-          if ((data = realloc(data, sizeof(uint32_t) * dataSize)) == NULL) {
-            abort();
+        trb_address = strtoul(cmd[1], NULL, hexMode == 1 ? 16 : 0);
+        reg_address = strtoul(cmd[2], NULL, hexMode == 1 ? 16 : 0);
+        option = strtoul(cmd[3], NULL, hexMode == 1 ? 16 : 0);
+        fileName = cmd[4];
+    
+        /* Open inputFile and read Data into buffer */
+        if (strncmp(fileName, "-", CMD_SIZE) == 0) {
+          file = stdin;
+        } else {
+          file = fopen(fileName, "r");
+          if (file == NULL) {
+            perror("Error opening file");
+            exit(EXIT_FAILURE);
           }
         }
-        data[size++] = strtoul(buffer, NULL, hexMode == 1 ? 16 : 0);
-      }
-      if (buffer != NULL) free(buffer);
-    
-      /* DEBUG Info */
-      if (trb_debug > 0) {
-        fprintf(stderr, 
-                "Command: WRITE_MEM: trb_address: 0x%04x, "
-                "reg_address: 0x%04x, "
-                "option: %d, "
-                "size: 0x%04x\n",
-                trb_address, reg_address, option, size);
-      }
-
-      status = trb_register_write_mem(trb_address, reg_address, option, 
-                                      data, size);
-      if (data != NULL) free(data);
-    
-      if (status == -1) {
-        trb_error("write register-memory failed");
-        exit(EXIT_FAILURE);
-      }
-    } else if (strcmp(argv[optind], "i") == 0) {
-
-      /*******************************************/
-      /* ReadUId                                 */
-      /*******************************************/
     
-      uint32_t uidBuffer[512];
-      unsigned int size;
+        if ((data = malloc(sizeof(uint32_t) * dataSize)) == NULL) abort();
+        while (getline(&line, &len, file) != -1) {
+          if (size >= dataSize) {
+            dataSize += 64;
+            if ((data = realloc(data, sizeof(uint32_t) * dataSize)) == NULL) {
+              abort();
+            }
+          }
+          data[size++] = strtoul(line, NULL, hexMode == 1 ? 16 : 0);
+        }
+        if (line != NULL) free(line);
+      
+        /* DEBUG Info */
+        if (trb_debug > 0) {
+          fprintf(stderr, 
+                  "Command: WRITE_MEM: trb_address: 0x%04x, "
+                  "reg_address: 0x%04x, "
+                  "option: %d, "
+                  "file: '%s', "
+                  "size: 0x%04x\n",
+                  trb_address, reg_address, option, fileName, size);
+        }
+      
+        status = trb_register_write_mem(trb_address, reg_address, option, 
+                                        data, size);
+        if (data != NULL) free(data);
     
-      if (argc - optind != 2) {
-        usage(argv[0]);
-        exit(EXIT_FAILURE);
-      }
+        if (status == -1) {
+          if (scriptFile != NULL) {
+            fprintf(stderr, "Line #%d: ", lineCtr);
+          }
+          trb_error("write_register-memory failed");
+          exit(EXIT_FAILURE);
+        }
+      } else if (strncmp(cmd[0], "i", CMD_SIZE) == 0) {
+
+        /*******************************************/
+        /* ReadUId                                 */
+        /*******************************************/
+    
+        uint32_t uidBuffer[512];
+        unsigned int size;
+    
+        if (cmdLen != 2) {
+          if (scriptFile != NULL) {
+            fprintf(stderr, "Line #%d: Invalid command\n", lineCtr);
+          } else {
+            usage(basename(argv[0]));
+          }
+          exit(EXIT_FAILURE);
+        }
     
-      trb_address = strtoul(argv[optind + 1], NULL, hexMode == 1 ? 16 : 0);
+        trb_address = strtoul(cmd[1], NULL, hexMode == 1 ? 16 : 0);
     
-      /* DEBUG Info */
-      if (trb_debug > 0) {
-        fprintf(stderr, 
-                "Command: READ_UID: trb_address: 0x%04x\n",
-                trb_address);
-      }
+        /* DEBUG Info */
+        if (trb_debug > 0) {
+          fprintf(stderr, 
+                  "Command: READ_UID: trb_address: 0x%04x\n",
+                  trb_address);
+        }
     
-      size = trb_read_uid(trb_address, uidBuffer, 128);
-      if (size == -1) {
-        trb_error("read_uid failed");
-        exit(EXIT_FAILURE);
-      }
+        size = trb_read_uid(trb_address, uidBuffer, 128);
+        if (size == -1) {
+          if (scriptFile != NULL) {
+            fprintf(stderr, "Line #%d: ", lineCtr);
+          }
+          trb_error("read_uid failed");
+          continue;
+          exit(EXIT_FAILURE);
+        }
  
-      for (i = 0; (i < size) && (i < 128); i += 4) {
-        fprintf(stdout, "0x%04x  0x%08x%08x  0x%02x\n",
-                uidBuffer[i + 3],
-                uidBuffer[i], 
-                uidBuffer[i + 1], 
-                uidBuffer[i + 2]);
-      }
-    } else if (strcmp(argv[optind], "s") == 0) {
+        for (i = 0; (i < size) && (i < 128); i += 4) {
+          fprintf(stdout, "0x%04x  0x%08x%08x  0x%02x\n",
+                  uidBuffer[i + 3],
+                  uidBuffer[i], 
+                  uidBuffer[i + 1], 
+                  uidBuffer[i + 2]);
+        }
+      } else if (strncmp(cmd[0], "s", CMD_SIZE) == 0) {
     
-      /*******************************************/
-      /* SetAddress                              */
-      /*******************************************/
+        /*******************************************/
+        /* SetAddress                              */
+        /*******************************************/
     
-      uint64_t uid = 0;
-      uint8_t endpoint = 0;
-      uint16_t trb_address = 0;
+        uint64_t uid = 0;
+        uint8_t endpoint = 0;
+        uint16_t trb_address = 0;
     
-      if (argc - optind != 4) {
-        usage(argv[0]);
-        exit(EXIT_FAILURE);
-      }
+        if (cmdLen != 4) {
+          if (scriptFile != NULL) {
+            fprintf(stderr, "Line #%d: Invalid command\n", lineCtr);
+          } else {
+            usage(basename(argv[0]));
+          }
+          exit(EXIT_FAILURE);
+        }
     
-      uid = strtoull(argv[optind + 1], NULL, hexMode == 1 ? 16 : 0);
-      endpoint = strtoul(argv[optind + 2], NULL, hexMode == 1 ? 16 : 0);
-      trb_address = strtoul(argv[optind + 3], NULL, hexMode == 1 ? 16 : 0);
-    
-      /* DEBUG Info */
-      if (trb_debug > 0) {
-        fprintf(stderr, 
-                "Command: SET_ADDRESS: "
-                "uid: 0x%016llx, "
-                "endpoint: 0x%02x, "
-                "trb_address: 0x%04x\n",
-                uid, endpoint, trb_address);
-      }
+        uid = strtoull(cmd[1], NULL, hexMode == 1 ? 16 : 0);
+        endpoint = strtoul(cmd[2], NULL, hexMode == 1 ? 16 : 0);
+        trb_address = strtoul(cmd[3], NULL, hexMode == 1 ? 16 : 0);
+    
+        /* DEBUG Info */
+        if (trb_debug > 0) {
+          fprintf(stderr, 
+                  "Command: SET_ADDRESS: "
+                  "uid: 0x%016llx, "
+                  "endpoint: 0x%02x, "
+                  "trb_address: 0x%04x\n",
+                  uid, endpoint, trb_address);
+        }
     
-      if (trb_set_address(uid, endpoint, trb_address) == -1) {
-        trb_error("set_address failed");
-        exit(EXIT_FAILURE);
-      }
-    } else if (strcmp(argv[optind], "T") == 0) {
+        if (trb_set_address(uid, endpoint, trb_address) == -1) {
+          if (scriptFile != NULL) {
+            fprintf(stderr, "Line #%d: ", lineCtr);
+          }
+          trb_error("set_address failed");
+          exit(EXIT_FAILURE);
+        }
+      } else if (strncmp(cmd[0], "T", CMD_SIZE) == 0) {
     
-      /*******************************************/
-      /* Send Trigger                            */
-      /*******************************************/
+        /*******************************************/
+        /* Send Trigger                            */
+        /*******************************************/
       
-      uint8_t type;
-      uint8_t random = 0;
-      uint8_t info = 0;
-      uint16_t number = 0;
-    
-      if (argc - optind != 5) {
-        usage(argv[0]);
-        exit(EXIT_FAILURE);
-      }
+        uint8_t type;
+        uint8_t random = 0;
+        uint8_t info = 0;
+        uint16_t number = 0;
+    
+        if (cmdLen != 5) {
+          if (scriptFile != NULL) {
+            fprintf(stderr, "Line #%d: Invalid command\n", lineCtr);
+          } else {
+            usage(basename(argv[0]));
+          }
+          exit(EXIT_FAILURE);
+        }
       
-      type = strtoul(argv[optind + 2], NULL, hexMode == 1 ? 16 : 0) & 0x0f;
-      random = strtoul(argv[optind + 3], NULL, hexMode == 1 ? 16 : 0);
-      info = strtoul(argv[optind + 4], NULL, hexMode == 1 ? 16 : 0);
-      number = strtoul(argv[optind + 5], NULL, hexMode == 1 ? 16 : 0);
-    
-      /* DEBUG Info */
-      if (trb_debug > 0) {
-        fprintf(stderr, 
-                "Command: SEND_TRIGGER: "
-                "type: 0x%01x, "
-                "random: 0x%02x, "
-                "info: 0x%02x, "
-                "number: 0x%04x\n",
-                type, random, info, number);
-      }
+        type = strtoul(cmd[2], NULL, hexMode == 1 ? 16 : 0) & 0x0f;
+        random = strtoul(cmd[3], NULL, hexMode == 1 ? 16 : 0);
+        info = strtoul(cmd[4], NULL, hexMode == 1 ? 16 : 0);
+        number = strtoul(cmd[5], NULL, hexMode == 1 ? 16 : 0);
+    
+        /* DEBUG Info */
+        if (trb_debug > 0) {
+          fprintf(stderr, 
+                  "Command: SEND_TRIGGER: "
+                  "type: 0x%01x, "
+                  "random: 0x%02x, "
+                  "info: 0x%02x, "
+                  "number: 0x%04x\n",
+                  type, random, info, number);
+        }
     
-      if (trb_send_trigger(type, info, random, number) == -1) {
-        trb_error("send trigger failed");
-        exit(EXIT_FAILURE);
-      } 
-    } else if (strcmp(argv[optind], "TR") == 0) {
-    
-      /*********************************************/
-      /* Send Fake trigger function to RICH Subnet */
-      /*********************************************/
-    
-      uint8_t input = 0;
-      uint8_t type = 0;
-      uint8_t random = 0;
-      uint8_t info = 0;
-      uint16_t number = 0;
+        if (trb_send_trigger(type, info, random, number) == -1) {
+          if (scriptFile != NULL) {
+            fprintf(stderr, "Line #%d: ", lineCtr);
+          }
+          trb_error("send_trigger failed");
+          exit(EXIT_FAILURE);
+        } 
+      } else if (strncmp(cmd[0], "TR", CMD_SIZE) == 0) {
+    
+        /*********************************************/
+        /* Send Fake trigger function to RICH Subnet */
+        /*********************************************/
+    
+        uint8_t input = 0;
+        uint8_t type = 0;
+        uint8_t random = 0;
+        uint8_t info = 0;
+        uint16_t number = 0;
       
-      if (argc - optind != 6) {
-        usage(argv[0]);
-        exit(EXIT_FAILURE);
-      }
+        if (cmdLen != 6) {
+          if (scriptFile != NULL) {
+            fprintf(stderr, "Line #%d: Invalid command\n", lineCtr);
+          } else {
+            usage(basename(argv[0]));
+          }
+          exit(EXIT_FAILURE);
+        }
     
-      input = strtoul(argv[optind + 1], NULL, hexMode == 1 ? 16 : 0) & 0x03;
-      type = strtoul(argv[optind + 2], NULL, hexMode == 1 ? 16 : 0) & 0x0f;
-      random = strtoul(argv[optind + 3], NULL, hexMode == 1 ? 16 : 0);
-      info = strtoul(argv[optind + 4], NULL, hexMode == 1 ? 16 : 0);
-      number = strtoul(argv[optind + 5], NULL, hexMode == 1 ? 16 : 0);
-    
-      /* DEBUG Info */
-      if (trb_debug > 0) {
-        fprintf(stderr, 
-                "Command: SEND_TRIGGER: "
-                "input: 0x%01x, "
-                "type: 0x%01x, "
-                "random: 0x%02x, "
-                "info: 0x%02x, "
-                "number: 0x%04x\n",
-                input, type, random, info, number);
-      }
+        input = strtoul(cmd[1], NULL, hexMode == 1 ? 16 : 0) & 0x03;
+        type = strtoul(cmd[2], NULL, hexMode == 1 ? 16 : 0) & 0x0f;
+        random = strtoul(cmd[3], NULL, hexMode == 1 ? 16 : 0);
+        info = strtoul(cmd[4], NULL, hexMode == 1 ? 16 : 0);
+        number = strtoul(cmd[5], NULL, hexMode == 1 ? 16 : 0);
+    
+        /* DEBUG Info */
+        if (trb_debug > 0) {
+          fprintf(stderr, 
+                  "Command: SEND_TRIGGER: "
+                  "input: 0x%01x, "
+                  "type: 0x%01x, "
+                  "random: 0x%02x, "
+                  "info: 0x%02x, "
+                  "number: 0x%04x\n",
+                  input, type, random, info, number);
+        }
     
-      if (trb_send_trigger_rich(input, type, info, random, number) == -1) {
-        trb_error("send trigger failed");
-        exit(EXIT_FAILURE);
-      }
-    } else if (strcmp(argv[optind], "I") == 0) {
-    
-      /*******************************************/
-      /* IPU channel readout                     */
-      /*******************************************/
-    
-      uint32_t buffer[4096];
-      uint8_t type = 0;
-      uint8_t random = 0;
-      uint8_t info = 0;
-      uint16_t number = 0;
-      int size = 0;
+        if (trb_send_trigger_rich(input, type, info, random, number) == -1) {
+          if (scriptFile != NULL) {
+            fprintf(stderr, "Line #%d: ", lineCtr);
+          }
+          trb_error("send_trigger failed");
+          exit(EXIT_FAILURE);
+        }
+      } else if (strncmp(cmd[0], "I", CMD_SIZE) == 0) {
+    
+        /*******************************************/
+        /* IPU channel readout                     */
+        /*******************************************/
+    
+        uint32_t buffer[4096];
+        uint8_t type = 0;
+        uint8_t random = 0;
+        uint8_t info = 0;
+        uint16_t number = 0;
+        int size = 0;
 
-      if (argc - optind != 5) {
-        usage(argv[0]);
-        exit(EXIT_FAILURE);
-      }
-    
-      type = strtoul(argv[optind + 1], NULL, hexMode == 1 ? 16 : 0) & 0x0f;
-      random = strtoul(argv[optind + 2], NULL, hexMode == 1 ? 16 : 0);
-      info = strtoul(argv[optind + 3], NULL, hexMode == 1 ? 16 : 0);
-      number = strtoul(argv[optind + 4], NULL, hexMode == 1 ? 16 : 0);
-    
-      /* DEBUG Info */
-      if (trb_debug > 0) {
-        fprintf(stderr, 
-                "Command: READ_IPU_DATA: "
-                "type: 0x%01x, "
-                "random: 0x%02x, "
-                "info: 0x%02x, "
-                "number: 0x%04x\n",
-                type, random, info, number);
-      }
-      size = trb_ipu_data_read(type, info, random, number, buffer, 4096);
-      if (size == -1) {
-        trb_error("ipu read data failed");
-        exit(EXIT_FAILURE);
-      }
+        if (cmdLen != 5) {
+          if (scriptFile != NULL) {
+            fprintf(stderr, "Line #%d: Invalid command\n", lineCtr);
+          } else {
+            usage(basename(argv[0]));
+          }
+          exit(EXIT_FAILURE);
+        }
     
-      for (i = 0; i < size; i++) {
-        fprintf(stdout, "0x%08x\n", buffer[i]);
-      }
-    } else if (strcmp(argv[optind], "f") == 0) {
+        type = strtoul(cmd[1], NULL, hexMode == 1 ? 16 : 0) & 0x0f;
+        random = strtoul(cmd[2], NULL, hexMode == 1 ? 16 : 0);
+        info = strtoul(cmd[3], NULL, hexMode == 1 ? 16 : 0);
+        number = strtoul(cmd[4], NULL, hexMode == 1 ? 16 : 0);
+    
+        /* DEBUG Info */
+        if (trb_debug > 0) {
+          fprintf(stderr, 
+                  "Command: READ_IPU_DATA: "
+                  "type: 0x%01x, "
+                  "random: 0x%02x, "
+                  "info: 0x%02x, "
+                  "number: 0x%04x\n",
+                  type, random, info, number);
+        }
+        size = trb_ipu_data_read(type, info, random, number, buffer, 4096);
+        if (size == -1) {
+          if (scriptFile != NULL) {
+            fprintf(stderr, "Line #%d: ", lineCtr);
+          }
+          trb_error("read_ipu_data failed");
+          exit(EXIT_FAILURE);
+        }
     
-      /*******************************************/
-      /* Flush FIFO Channel                      */
-      /*******************************************/
+        for (i = 0; i < size; i++) {
+          fprintf(stdout, "0x%08x\n", buffer[i]);
+        }
+      } else if (strncmp(cmd[0], "f", CMD_SIZE) == 0) {
     
-      int status;
-      uint8_t channel = 0;
+        /*******************************************/
+        /* Flush FIFO Channel                      */
+        /*******************************************/
     
-      if (argc - optind != 2) {
-        usage(argv[0]);
-        exit(EXIT_FAILURE);
-      }
+        int status;
+        uint8_t channel = 0;
     
-      channel = strtoul(argv[optind + 1], NULL, hexMode == 1 ? 16 : 0);
-      if (trb_debug > 0) {
-        fprintf(stderr, "Command: FIFO_FLUSH_CHANNEL #%d\n", channel);
-      }
+        if (cmdLen != 2) {
+          if (scriptFile != NULL) {
+            fprintf(stderr, "Line #%d: Invalid command\n", lineCtr);
+          } else {
+            usage(basename(argv[0]));
+          }
+          exit(EXIT_FAILURE);
+        }
     
-      status = trb_fifo_flush(channel);
-      if (status == -1) {
-        trb_error("flush channel failed");
-        exit(EXIT_FAILURE);
-      }
-    } else if (strcmp(argv[optind], "R") == 0) {
+        channel = strtoul(cmd[1], NULL, hexMode == 1 ? 16 : 0);
+        if (trb_debug > 0) {
+          fprintf(stderr, "Command: FIFO_FLUSH_CHANNEL #%d\n", channel);
+        }
     
-      /*******************************************/
-      /* Read FIFO Register                      */
-      /*******************************************/
+        status = trb_fifo_flush(channel);
+        if (status == -1) {
+          if (scriptFile != NULL) {
+            fprintf(stderr, "Line #%d: ", lineCtr);
+          }
+          trb_error("flush_channel failed");
+          exit(EXIT_FAILURE);
+        }
+      } else if (strncmp(cmd[0], "R", CMD_SIZE) == 0) {
     
-      uint32_t value = 0;
-      uint16_t reg_address = 0;
+        /*******************************************/
+        /* Read FIFO Register                      */
+        /*******************************************/
     
-      if (argc - optind != 2) {
-        usage(argv[0]);
-        exit(EXIT_FAILURE);
-      }
-      reg_address = strtoul(argv[optind + 1], NULL, hexMode == 1 ? 16 : 0);
+        uint32_t value = 0;
+        uint16_t reg_address = 0;
     
-      read32_from_FPGA(reg_address, &value);
-      fprintf(stdout, "%s%04x  %s%04x\n", 
-              hexMode == 1 ? "" : "0x", reg_address,
-              hexMode == 1 ? "" : "0x", value);
-  
-    } else if (strcmp(argv[optind], "W") == 0) {
+        if (cmdLen != 2) {
+          if (scriptFile != NULL) {
+            fprintf(stderr, "Line #%d: Invalid command\n", lineCtr);
+          } else {
+            usage(basename(argv[0]));
+          }
+          exit(EXIT_FAILURE);
+        }
+
+        reg_address = strtoul(cmd[1], NULL, hexMode == 1 ? 16 : 0);
+
+        if (trb_debug > 0) {
+          fprintf(stderr, 
+                  "Command: READ_FIFO_REGISTER:"
+                  "reg_address: 0x%04x\n",
+                  reg_address);
+        }
     
-      /*******************************************/
-      /* Write FIFO Register                     */
-      /*******************************************/
+        read32_from_FPGA(reg_address, &value);
+        fprintf(stdout, "0x%04x  0x%08x\n", reg_address, value);
+      
+      } else if (strncmp(cmd[0], "W", CMD_SIZE) == 0) {
     
-      uint32_t value = 0;
-      uint16_t reg_address = 0;
+        /*******************************************/
+        /* Write FIFO Register                     */
+        /*******************************************/
     
-      if (argc - optind != 3) {
-        usage(argv[0]);
-        exit(EXIT_FAILURE);
-      }
-      reg_address = strtoul(argv[optind + 1], NULL, hexMode == 1 ? 16 : 0);
-      value = strtoul(argv[optind + 2], NULL, hexMode == 1 ? 16 : 0);
-   
-      write32_to_FPGA(reg_address, value);
+        uint32_t value = 0;
+        uint16_t reg_address = 0;
     
-      /*
-        fprintf(stdout, "%s%04x  %s%04x\n", 
-        hexMode == 1 ? "" : "0x", reg_address,
-        hexMode == 1 ? "" : "0x", value);
-      */
-    } else {
-      usage(argv[0]);
-      exit(EXIT_FAILURE);
-    }
-
+        if (cmdLen != 3) {
+          if (scriptFile != NULL) {
+            fprintf(stderr, "Line #%d: Invalid command\n", lineCtr);
+          } else {
+            usage(basename(argv[0]));
+          }
+          exit(EXIT_FAILURE);
+        }
+        
+        reg_address = strtoul(cmd[1], NULL, hexMode == 1 ? 16 : 0);
+        value = strtoul(cmd[2], NULL, hexMode == 1 ? 16 : 0);
 
-  } /* end repeat loop */ 
+        if (trb_debug > 0) {
+          fprintf(stderr, 
+                  "Command: READ_FIFO_REGISTER:"
+                  "reg_address: 0x%04x, "
+                  "value: 0x%08x\n",
+                  reg_address, value);
+        }
+      
+        write32_to_FPGA(reg_address, value);
+      } else {
+          if (scriptFile != NULL) {
+            fprintf(stderr, "Line #%d: Invalid command\n", lineCtr);
+          } else {
+            usage(basename(argv[0]));
+          }
+        exit(EXIT_FAILURE);
+     }
 
+    } /* End script-file-loop */
+         
+  } /* End repeat-loop */
+  
+  /* Cleanup */
+  if (scriptFile != NULL) {
+    fclose(scriptFile);
+  }
+  if (cmdLine != 0) free(cmdLine);
+  
   exit(EXIT_SUCCESS);
 }
index b84938a843ce2348854d339a7abff28e7b999925..9f26330270082a515454b0098900638eb19b675e 100644 (file)
@@ -34,7 +34,7 @@ const char* trb_strerror(int errno)
     "FIFO Error, invalid sequenze-number",
     "FIFO Error, invalid read-data-mode",
     "FIFO Broken-Package",
-    "FIFO Invalid-Content (no TERMINATION)",
+    "FIFO Invalid-Content",
     "FAILED WAIT_IS_VALID",
     "FAILED WAIT_IS_NOT_VALID",
     "User-Buffer Overflow",
@@ -45,10 +45,11 @@ const char* trb_strerror(int errno)
     "Invalid Data-Buffer Length",
     "Endpoint not reached",
     "DMA not available (check whether module 'can_module.ko' is loaded)",
-    "DMA-Timeout"
+    "DMA-Timeout",
+    "ReadMem invalid size"
   };
   
-  if (errno < 20) {
+  if (errno < 21) {
     return errorstring[errno];
   } else {
     return "Unknown Errno";
index c9947e786d305d9275dce433cd7f0439b480f737..b70cac984c8cee6f2d6ae74a327024a348a84b09 100644 (file)
@@ -23,7 +23,8 @@ typedef enum {
   TRB_INVALID_LENGTH = 16,
   TRB_ENDPOINT_NOT_REACHED = 17,
   TRB_DMA_UNAVAILABLE = 18,
-  TRB_DMA_TIMEOUT = 19
+  TRB_DMA_TIMEOUT = 19,
+  TRB_READMEM_INVALID_SIZE = 20
 } TRB_ERROR;
 
 extern int trb_errno;
index 2556fe2edbece5adc13f3a2d1566ed89e172f4ef..d7d9d3889e110c506e0e0f67fbcfe26c4cb8e5ea 100644 (file)
@@ -339,7 +339,7 @@ enum FIFO_READ_MODE {
 static int trb_fifo_read(uint8_t channel,
                          int mode,
                          uint32_t data[],
-                         unsigned int size)
+                         unsigned int dsize)
 {
   static uint32_t dataBuffer[DATA_BUFFER_SIZE];
   uint32_t *tmp = dataBuffer;
@@ -453,7 +453,7 @@ static int trb_fifo_read(uint8_t channel,
         fprintf(stderr, "-------------------------------------------------\n");
       }
       
-      /* First package (TRB-Header), headerType must be HDR or TRM */
+      /* First package: headerType must be HDR or TRM */
       if (trb_lazy == 0) {
         if (packageCtr == 0) {
           if (!((headerType == HEADER_HDR) | (headerType == HEADER_TRM))) {
@@ -477,43 +477,94 @@ static int trb_fifo_read(uint8_t channel,
       }
           
       /* Get Data F0 - F3 and store it in User-Data-Buffer if requested */
-      if (headerType != HEADER_TRM) {
-        switch (mode) {
+      switch (mode) {
+        
+      case FIFO_MODE_NONE:
+        break;
+        
+      case FIFO_MODE_REG_READ:
+        
+        switch (headerType) {
+        case HEADER_HDR:
+          if ((packageCtr - endPointCtr * 2) != 0) {
+            trb_fifo_flush(channel);
+            trb_errno = TRB_FIFO_INVALID_CONTENT;
+            return -1;
+          }
+          if (dataCtr < dsize) {
+            data[dataCtr++] = (uint32_t)package.F0;
+          } else {
+            trb_fifo_flush(channel);
+            trb_errno = TRB_USER_BUFFER_OVF;
+            return -1; 
+          }
+          break;
+            
+        case HEADER_DAT:
+          if ((packageCtr - endPointCtr * 2) != 1) {
+            trb_fifo_flush(channel);
+            trb_errno = TRB_FIFO_INVALID_CONTENT;
+            return -1;
+          }
+          if (dataCtr < dsize) {
+            data[dataCtr++] = (((uint32_t)package.F1 << 16) | 
+                               ((uint32_t)package.F2));
+            endPointCtr++;
+          } else {
+            trb_fifo_flush(channel);
+            trb_errno = TRB_USER_BUFFER_OVF;
+            return -1;
+          }
+          break;
           
-        case FIFO_MODE_NONE:
+        case HEADER_TRM:
           break;
           
-        case FIFO_MODE_REG_READ:
-
+        default:
+          trb_fifo_flush(channel);
+          trb_errno = TRB_FIFO_INVALID_CONTENT;
+          return -1;
+        }
+        
+        break;
+          
+      case FIFO_MODE_REG_READ_MEM:
+        {
+          static uint32_t* lastHeader = NULL;
+          static uint32_t memLen = 0;
+          
           switch (headerType) {
           case HEADER_HDR:
-            if ((packageCtr - endPointCtr * 2) != 0) {
-              trb_fifo_flush(channel);
-              trb_errno = TRB_FIFO_INVALID_CONTENT;
-              return -1;
-            }
-            if (dataCtr < size) {
+            if (dataCtr < dsize) {
+              if (lastHeader != NULL) {
+                *lastHeader |= (memLen << 16);
+              }
+              memLen = 0;
+              lastHeader = &data[dataCtr];
               data[dataCtr++] = (uint32_t)package.F0;
             } else {
               trb_fifo_flush(channel);
               trb_errno = TRB_USER_BUFFER_OVF;
-              return -1; 
+              fprintf(stderr, "HEADER\n");
+              return -1;  
             }
             break;
             
           case HEADER_DAT:
-            if ((packageCtr - endPointCtr * 2) != 1) {
-              trb_fifo_flush(channel);
-              trb_errno = TRB_FIFO_INVALID_CONTENT;
-            }
-            if (dataCtr < size) {
+            if (dataCtr < dsize) {
               data[dataCtr++] = (((uint32_t)package.F1 << 16) | 
                                  ((uint32_t)package.F2));
-              endPointCtr++;
+              memLen++;
             } else {
               trb_fifo_flush(channel);
               trb_errno = TRB_USER_BUFFER_OVF;
-              return -1;
+              return -1; 
+            }
+            break;
+            
+          case HEADER_TRM:
+            if (lastHeader != NULL) {
+              *lastHeader |= (memLen << 16);
             }
             break;
             
@@ -522,126 +573,115 @@ static int trb_fifo_read(uint8_t channel,
             trb_errno = TRB_FIFO_INVALID_CONTENT;
             return -1;
           }
+        }
+        break;
           
-          break;
-          
-        case FIFO_MODE_REG_READ_MEM:
-          if (packageCtr > 0) {
-            if (headerType != HEADER_DAT) {
-              trb_fifo_flush(channel);
-              trb_errno = TRB_FIFO_INVALID_CONTENT;
-              return -1;
-            }
-            if (dataCtr < size) {
-              data[dataCtr++] = (((uint32_t)package.F1 << 16) | 
-                                 ((uint32_t)package.F2));
-            } else {
-              trb_fifo_flush(channel);
-              trb_errno = TRB_USER_BUFFER_OVF;
-              return -1;
-            }
+      case FIFO_MODE_REG_WRITE:
+        if (headerType == HEADER_TRM) break;
+        
+        if (packageCtr > 1) {
+          trb_fifo_flush(channel);
+          trb_errno = TRB_INVALID_PKG_NUMBER;
+          return -1;
+        }
+        break;
+        
+      case FIFO_MODE_IPU_DATA:
+        if (headerType == HEADER_TRM) break;
+        
+        if ((headerType == HEADER_DAT) && (packageCtr > 0)) {
+          if (headerType != HEADER_DAT) {
+            trb_fifo_flush(channel);
+            trb_errno = TRB_FIFO_INVALID_CONTENT;
+            return -1;
           }
-          break;
-          
-        case FIFO_MODE_REG_WRITE:
-          if (packageCtr > 1) {
+          if ((dataCtr + 1) < dsize) {
+            data[dataCtr++] = (((uint32_t)package.F0 << 16) | 
+                               ((uint32_t)package.F1));
+            data[dataCtr++] = (((uint32_t)package.F2 << 16) | 
+                               ((uint32_t)package.F3));
+          } else {
             trb_fifo_flush(channel);
-            trb_errno = TRB_INVALID_PKG_NUMBER;
+            trb_errno = TRB_USER_BUFFER_OVF;
             return -1;
           }
-          break;
+        }
+        break;
+          
+      case FIFO_MODE_UID:
+        {
+          static uint32_t uidLow;
+          static uint32_t uidHigh;
+          static uint32_t sourceAddress;
           
-        case FIFO_MODE_IPU_DATA:
-          if (packageCtr > 0) {
-            if (headerType != HEADER_DAT) {
+          switch (headerType) {
+          case HEADER_HDR:
+            if ((packageCtr - endPointCtr * 3) != 0) {
               trb_fifo_flush(channel);
               trb_errno = TRB_FIFO_INVALID_CONTENT;
               return -1;
             }
-            if ((dataCtr + 1) < size) {
-              data[dataCtr++] = (((uint32_t)package.F0 << 16) | 
-                                 ((uint32_t)package.F1));
-              data[dataCtr++] = (((uint32_t)package.F2 << 16) | 
-                                 ((uint32_t)package.F3));
-            } else {
-              trb_fifo_flush(channel);
-              trb_errno = TRB_USER_BUFFER_OVF;
-              return -1;
+            sourceAddress = (uint32_t)package.F0;
+            break;
+            
+          case HEADER_DAT:
+            if ((packageCtr - endPointCtr * 3) == 1) {
+              uidHigh = (((uint32_t)package.F0 <<  0) |  
+                         ((uint32_t)package.F1 << 16));
+              uidLow = (((uint32_t)package.F2 <<  0) |  
+                        ((uint32_t)package.F3 << 16));
+              break;
             }
-          }
-          break;
-          
-        case FIFO_MODE_UID:
-          {
-            static uint32_t uidLow;
-            static uint32_t uidHigh;
-            static uint32_t sourceAddress;
             
-            switch (headerType) {
-            case HEADER_HDR:
-              if ((packageCtr - endPointCtr * 3) != 0) {
+            if ((packageCtr - endPointCtr * 3) == 2) {
+              if ((dataCtr + 3) < dsize) {
+                /* store uid, endPoint and sourceAddress in userDataBuffer */
+                data[dataCtr++] = uidLow;
+                data[dataCtr++] = uidHigh;
+                data[dataCtr++] = (uint32_t)package.F0;
+                data[dataCtr++] = (uint32_t)sourceAddress;
+                endPointCtr++;
+              } else {
                 trb_fifo_flush(channel);
-                trb_errno = TRB_FIFO_INVALID_CONTENT;
+                trb_errno = TRB_USER_BUFFER_OVF;
                 return -1;
               }
-              sourceAddress = (uint32_t)package.F0;
               break;
-              
-            case HEADER_DAT:
-              if ((packageCtr - endPointCtr * 3) == 1) {
-                uidHigh = (((uint32_t)package.F0 <<  0) |  
-                           ((uint32_t)package.F1 << 16));
-                uidLow = (((uint32_t)package.F2 <<  0) |  
-                          ((uint32_t)package.F3 << 16));
-                break;
-              }
-              
-              if ((packageCtr - endPointCtr * 3) == 2) {
-                if ((dataCtr + 3) < size) {
-                  /* store uid, endPoint and sourceAddress in userDataBuffer */
-                  data[dataCtr++] = uidLow;
-                  data[dataCtr++] = uidHigh;
-                  data[dataCtr++] = (uint32_t)package.F0;
-                  data[dataCtr++] = (uint32_t)sourceAddress;
-                  endPointCtr++;
-                } else {
-                  trb_fifo_flush(channel);
-                  trb_errno = TRB_USER_BUFFER_OVF;
-                  return -1;
-                }
-                break;
-              }
-              
-            default:
-              trb_fifo_flush(channel);
-              trb_errno = TRB_FIFO_INVALID_CONTENT;
-              return -1;
             }
+          case HEADER_TRM:
+            break;
+            
+          default:
+            trb_fifo_flush(channel);
+            trb_errno = TRB_FIFO_INVALID_CONTENT;
+            return -1;
           }
-          break;
+        }
+        break;
       
-        case FIFO_MODE_SET_ADDRESS:
-          if ((packageCtr == 1) && (headerType == HEADER_DAT)) {
-            if (package.F0 != NET_ACKADDRESS) {
-              trb_fifo_flush(channel);
-              return -1;
-            }
-          }
-          
-          if (packageCtr > 1) {
+      case FIFO_MODE_SET_ADDRESS:
+        if (headerType == HEADER_TRM) break;
+        
+        if ((packageCtr == 1) && (headerType == HEADER_DAT)) {
+          if (package.F0 != NET_ACKADDRESS) {
             trb_fifo_flush(channel);
-            trb_errno = TRB_INVALID_PKG_NUMBER;
             return -1;
           }
-          
-          dataCtr++;
-          break;
-
-        default:
+        }
+        
+        if (packageCtr > 1) {
           trb_fifo_flush(channel);
-          trb_errno = TRB_FIFO_INVALID_MODE;
+          trb_errno = TRB_INVALID_PKG_NUMBER;
           return -1;
         }
+        
+        dataCtr++;
+        break;
+        
+      default:
+        trb_fifo_flush(channel);
+        trb_errno = TRB_FIFO_INVALID_MODE;
+        return -1;
       }
     }
     
@@ -716,7 +756,7 @@ int trb_fifo_flush(uint8_t channel)
 int trb_register_read(uint16_t trb_address,
                       uint16_t reg_address,
                       uint32_t *data,
-                      unsigned int size)
+                      unsigned int dsize)
 {
   int status = 0;
   
@@ -744,7 +784,7 @@ int trb_register_read(uint16_t trb_address,
     fprintf(stderr, "CMD_REGISTER_READ started.\n");
   }
   
-  status = trb_fifo_read(3, FIFO_MODE_REG_READ, data, size);
+  status = trb_fifo_read(3, FIFO_MODE_REG_READ, data, dsize);
   
   if ((status > 0) && (status % 2 != 0)) {
     trb_errno = TRB_INVALID_PKG_NUMBER;
@@ -757,16 +797,15 @@ int trb_register_read(uint16_t trb_address,
 int trb_register_read_mem(uint16_t trb_address,                             
                           uint16_t reg_address,                             
                           uint8_t option,
+                          uint16_t size,
                           uint32_t *data,
-                          uint16_t size)
+                          unsigned int dsize)
 {
   uint16_t length;
+  int status;
+  const uint32_t *p = NULL;
+  const uint32_t *end = NULL;
   
-  /* Do not allow broadcasts within this function */
-  if (trb_address >= 0xff00) {
-    trb_errno = TRB_INVALID_ADDRESS;
-    return -1;
-  }
   
   /* check size and set reading-mode */
   length = size & 0x7fff;
@@ -800,7 +839,25 @@ int trb_register_read_mem(uint16_t trb_address,
     fprintf(stderr, "CMD_REGISTER_READ_MEM started.\n");
   }
   
-  return trb_fifo_read(3, FIFO_MODE_REG_READ_MEM, data, size);
+  status = trb_fifo_read(3, FIFO_MODE_REG_READ_MEM, data, dsize);
+  
+  if (status == -1) return status;
+  
+  /* Check size */
+  p = data;
+  end = p + status;
+  while (p < end) {
+    uint16_t len;
+    len = (*p >> 16) & 0xffff;
+    if (len > size) {
+      trb_errno = TRB_READMEM_INVALID_SIZE;
+      fprintf(stderr, "fuck fuck %d  %d\n", size, len);
+      return -1;
+    }
+    p += len + 1; 
+  }
+  
+  return status; 
 }
 
 int trb_register_write(uint16_t trb_address,
@@ -838,14 +895,14 @@ int trb_register_write_mem(uint16_t trb_address,
                            uint16_t reg_address,                             
                            uint8_t option,
                            const  uint32_t *data,
-                           uint16_t size)
+                           uint16_t dsize)
 {
   uint16_t config;
   uint16_t i;
   
   /* check size and set write-mode */
-  config = size & 0x7fff;
-  if ((size == 0) || (size != config)) {
+  config = dsize & 0x7fff;
+  if ((dsize == 0) || (dsize != config)) {
     trb_errno = TRB_INVALID_LENGTH;
     return -1;
   }
@@ -868,7 +925,7 @@ int trb_register_write_mem(uint16_t trb_address,
   write32_to_FPGA(CHANNEL_3_SENDER_DATA, config);
   write32_to_FPGA(CHANNEL_3_SENDER_DATA, 0x00000000);
   write32_to_FPGA(CHANNEL_3_SENDER_DATA, 0x00000000);
-  for (i = 0; i < size; i++) {
+  for (i = 0; i < dsize; i++) {
     write32_to_FPGA(CHANNEL_3_SENDER_DATA, 0x00000000); 
     write32_to_FPGA(CHANNEL_3_SENDER_DATA, (data[i] >> 16) & 0xffff);
     write32_to_FPGA(CHANNEL_3_SENDER_DATA, data[i] & 0xffff);
@@ -886,7 +943,7 @@ int trb_register_write_mem(uint16_t trb_address,
 
 int trb_read_uid(uint16_t trb_address,
                  uint32_t* uidBuffer,
-                 unsigned int size)
+                 unsigned int dsize)
 {
   int status;
   
@@ -914,7 +971,7 @@ int trb_read_uid(uint16_t trb_address,
     fprintf(stderr, "CMD_READ_UNIQUE_ID started.\n");
   }
   
-  status = trb_fifo_read(3, FIFO_MODE_UID, (uint32_t*)uidBuffer, size);
+  status = trb_fifo_read(3, FIFO_MODE_UID, (uint32_t*)uidBuffer, dsize);
   
   if ((status > 0) && (status % 4 != 0)) {
     trb_errno = TRB_INVALID_PKG_NUMBER;
@@ -962,7 +1019,7 @@ int trb_set_address(uint64_t uid,
   
   /* DEBUG INFO */
   if (trb_debug > 0) {
-     fprintf(stderr, "CMD_SETADDRESS started.\n");
+    fprintf(stderr, "CMD_SETADDRESS started.\n");
   }
   
   status = trb_fifo_read(3, FIFO_MODE_SET_ADDRESS, NULL, 0); 
@@ -982,7 +1039,7 @@ int trb_ipu_data_read(uint8_t type,
                       uint8_t trg_random,
                       uint16_t trg_number,
                       uint32_t *data,
-                      unsigned int size)
+                      unsigned int dsize)
 {
   int status;
   
@@ -1011,7 +1068,7 @@ int trb_ipu_data_read(uint8_t type,
     fprintf(stderr, "CMD_IPU_DATA_READ started.\n");
   }
   
-  status = trb_fifo_read(1, FIFO_MODE_IPU_DATA, data, size);
+  status = trb_fifo_read(1, FIFO_MODE_IPU_DATA, data, dsize);
   
   return status;
 }
index 8106585ad716086913f06c710d1cfe5625fee59d..fdef48fe7b2268113969fe3824ca5aa8c879334a 100644 (file)
@@ -15,13 +15,15 @@ int trb_fifo_flush(uint8_t channel);
 int trb_register_read(uint16_t trb_address,                             
                       uint16_t reg_address,                             
                       uint32_t* data,
-                      unsigned int size);                                 
+                      unsigned int dsize);                                 
 
 int trb_register_read_mem(uint16_t trb_address,                             
                           uint16_t reg_address,                             
                           uint8_t option,
+                          uint16_t size,
                           uint32_t* data,
-                          uint16_t size);
+                          unsigned int dsize);
+                          
                         
 int trb_register_write(uint16_t trb_address,                            
                        uint16_t reg_address,                            
@@ -31,11 +33,11 @@ int trb_register_write_mem(uint16_t trb_address,
                            uint16_t reg_address,                             
                            uint8_t option,
                            const  uint32_t* data,
-                           uint16_t size);
+                           uint16_t dsize);
 
 int trb_read_uid(uint16_t trb_address,                                  
                  uint32_t* uidBuffer,
-                 unsigned int size);                                 
+                 unsigned int dsize);                                 
 
 int trb_set_address(uint64_t uid,
                     uint8_t endpoint,
@@ -47,7 +49,7 @@ int trb_ipu_data_read(uint8_t type,
                       uint8_t trg_random,
                       uint16_t trg_number,
                       uint32_t* data, 
-                      unsigned int size);
+                      unsigned int dsize);
 
 int trb_send_trigger(uint8_t type,
                      uint8_t info,