From: hadaq Date: Mon, 19 Jul 2010 19:19:45 +0000 (+0000) Subject: delete old files X-Git-Tag: v6.0~243 X-Git-Url: https://jspc29.x-matter.uni-frankfurt.de/git/?a=commitdiff_plain;h=6eab69f391205a41d5207801db7c5ac5e30fd14a;p=trbnettools.git delete old files --- diff --git a/trbnetd/pulser.c b/trbnetd/pulser.c deleted file mode 100644 index 295fe30..0000000 --- a/trbnetd/pulser.c +++ /dev/null @@ -1,204 +0,0 @@ -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include - -static const char pulser_version[] = "$Revision: 1.1 $"; - -/* 6MByte for 6 Sectors should be enough */ -#define BUFFER_SIZE (6 * 1024 * 1024 / 4) - -/* ------ MAIN ---------------------------------------------------------- */ - -static FILE *hldFile = NULL; -static unsigned int evtCounter = 0; -static uint32_t *buffer = NULL; - -static void atexit0() -{ - if (hldFile != NULL) { - fclose(hldFile); - } - free(buffer); - - fprintf(stderr, "%d Triggers were send\n", evtCounter); -} - -static void sigHandler(int sig) -{ - if (sig == SIGTERM) fprintf(stderr, "caught SIGTERM\n"); - if (sig == SIGINT) fprintf(stderr, "caught SIGINT\n"); - - exit(128 + sig); -} - -void usage(const char *progName) -{ - printf("Usage: %s [-h] [-d level] [-f outFileName] [-n numEvents] " - "[-t triggerType] [-i input] [-V]\n", - progName); - printf("Options:\n"); - printf(" -h give this help\n"); - printf(" -d turn on Debugging Information\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"); - printf(" -V Version number\n"); -} - -int main(int argc, char ** argv) -{ - char hldFileName[256] = "pulser.hld"; - uint16_t trg_number = 0; - uint8_t trg_info = 0x00; - uint8_t trg_random = 0; - uint8_t triggerType = 0; - uint8_t input = 0; - unsigned int numEvts = UINT_MAX; - sigset_t blockSet; - int size; - int writeToStdout = 0; - unsigned int trgCtr = 0; - int i; - - trb_debug = 0; - - /* Parse Arguments */ - while ((i = getopt(argc, argv, "+hd:f:n:t:i:V")) != -1) { - switch (i) { - case '?': - usage(basename(argv[0])); - exit(EXIT_FAILURE); - case 'h': - usage(basename(argv[0])); - exit(EXIT_SUCCESS); - case 'd': - trb_debug = strtoul(optarg, NULL, 0); - break; - case 'f': - strncpy(hldFileName, optarg, 256); - break; - 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; - case 'V': - printf("%s %s, using libtrbnet %s\n", - basename(argv[0]), pulser_version, trbnet_version); - exit(EXIT_SUCCESS); - break; - default: - break; - } - } - - /* open port */ - init_ports(); - - /* Open HLD-File */ - if (strncmp(hldFileName, "stdout", 256) != 0) { - hldFile = fopen(hldFileName, "w"); - if (hldFile == NULL) { - perror("File Open"); - } - } else { - writeToStdout = 1; - } - - /* Set Signalhandler */ - atexit(atexit0); - signal(SIGINT, sigHandler); - signal(SIGTERM, sigHandler); - - /* Set signal mask for blocking */ - sigemptyset(&blockSet); - sigaddset(&blockSet, SIGINT); - sigaddset(&blockSet, SIGTERM); - - /* Prepare trg_number */ - if (trb_register_write(0xfffb, 0x0020, 0x08) == -1) { - trb_error("Error reseting trigger_number\n"); - exit(EXIT_FAILURE); - } - - /* allocate buffer */ - buffer = (uint32_t*)malloc(sizeof(uint32_t) * BUFFER_SIZE); - if (buffer == NULL) abort(); - - trg_number = 0; - srand(time(NULL)); - - /* Loop Triggers */ - while(evtCounter < numEvts) { - unsigned int len; - - trg_random = (uint8_t)(random() & 0xff); - - /* Block signals */ - sigprocmask(SIG_BLOCK, &blockSet, NULL); - - /* Send Trigger and get Data */ - if (trb_send_trigger_rich(input, triggerType, trg_info, - trg_random, trg_number) == -1) { - trb_error("Error send_trigger"); - exit(EXIT_FAILURE); - } - - size = trb_ipu_data_read(triggerType, trg_info, trg_random, trg_number, - buffer, BUFFER_SIZE); - if (size == -1) { - trb_error("Error IPU Read"); - exit(EXIT_FAILURE); - } - - /* There must be a DHDR --> size must be at least >= 2 */ - if (size < 2) { - fprintf(stderr, "DHRD error, size=%d\n", size); - exit(EXIT_FAILURE); - } - len = ((buffer[1] >> 16) & 0xffff) + 2; - if (len != size) { - fprintf(stderr, "DHRD len error, len=%d size=%d\n", len, size); - exit(EXIT_FAILURE); - } - - if (writeToStdout == 1) { - fprintf(stdout, "Trigger# %d, Random: %d\n", trg_number, trg_random); - for (i = 0; i < len; i++) { - fprintf(stdout, "0x%08x\n", buffer[i]); - } - } else { - if (fwrite((void*)buffer, 4, len, hldFile) != len) { - perror("Writing to File failed"); - exit(EXIT_FAILURE); - } - } - - /* Unblock signals */ - sigprocmask(SIG_UNBLOCK, &blockSet, NULL); - - trgCtr++; - evtCounter++; - if (evtCounter % 100 == 0) { - fprintf(stderr, "%d triggers send\n", trgCtr); - } - - trg_number++; - } - - exit(EXIT_SUCCESS); -} diff --git a/trbnetd/trb_i2c.c b/trbnetd/trb_i2c.c deleted file mode 100644 index 573bc6f..0000000 --- a/trbnetd/trb_i2c.c +++ /dev/null @@ -1,306 +0,0 @@ -#define _GNU_SOURCE -#include -#include -#include - -#include -#include -#include -#include -#include - -#define BUFFER_SIZE (6 * 4096) - -static const char trb_i2c_version[] = "$Revision: 1.1 $"; - -static const uint16_t trb_i2c_register = 0x8040; - -static const unsigned int timeout = 1000; - -void usage(const char *progName) -{ - printf("Usage: %s [-h] [-d level] [-V] \n", progName); - printf("Options:\n"); - printf(" -h give this help\n"); - printf(" -d turn on Debugging Information\n"); - printf(" -V Version number\n"); - printf("\nCommands:\n"); - printf(" w -> " - "write to \n"); - printf(" r -> " - "read from \n"); - printf(" c " - "clear I2C-Bus on \n"); -} - -/* ------ MAIN ---------------------------------------------------------- */ - -/* - Write bit definition - ==================== - - D[31] I2C_GO 0 => don't do anything on I2C, 1 => start I2C access - D[30] I2C_ACTION 0 => write byte, 1 => read byte - D[29:24] I2C_SPEED set to all '1' - D[23:16] I2C_ADDRESS address of I2C chip - D[15:8] I2C_CMD command byte for access - D[7:0] I2C_DATA data to be written - - - Read bit definition - =================== - - D[31:24] status status information - D[31] RUNNING whatever - D[30] I2C_DONE whatever - D[29] ERROR_RADDACK no acknowledge for repeated address byte - D[28] ERROR_RSTART generation of repeated START condition failed - D[27] ERROR_DATACK no acknowledge for data byte - D[26] ERROR_CMDACK no acknowledge for command byte - D[25] ERROR_ADDACK no acknowledge for address byte - D[24] ERROR_START generation of START condition failed - D[23:21] reserved reserved - D[20:16] debug subject to change, don't use - D[15:8] reserved reserved - D[7:0] I2C_DATA result of I2C read operation -*/ - -typedef enum I2C_ERROR { - ERROR_RADDACK, - ERROR_RSTART, - ERROR_DATACK, - ERROR_CMDACK, - ERROR_ADDACK, - ERROR_START -} I2C_STATUS_BITS; - -static int readI2CRegister(uint16_t trb_address, int checkStatus, - uint32_t* buffer, unsigned int bufferSize) -{ - unsigned int ctr = 0; - int status; - int error = 0; - int i; - - /* Wait until NoMoreData is withdrawn */ - do { - if ((status = trb_register_read(trb_address, trb_i2c_register, - buffer, BUFFER_SIZE)) == -1) { - trb_error("Error I2C not ready"); - return -1; - } - - /* Check timeout */ - if (ctr >= timeout) { - fprintf(stderr, "Error I2C not ready, timeout\n"); - return -1; - } - ctr++; - } while (trb_term.status_channel != 0); - - if (checkStatus == 0) return status; - - /* Check all StatusBits */ - for (i = 0; i < status; i += 2) { - if (((buffer[i + 1] >> 24) & 0x3f) != 0) { - fprintf(stderr, "ErrorStatusBit set on 0x%04x: 0x%02x\n", - buffer[i], (buffer[i + 1] >> 24 & 0x3f) - ); - error = 1; - } - } - - return error == 0 ? status : -1; -} - -int clearI2C(uint16_t trb_address) -{ - if (trb_register_write(trb_address, trb_i2c_register, 0x3f000000) == -1) { - trb_error("Error clearing I2C"); - return -1; - } - - return 0; -} - -int main(int argc, char** argv) -{ - static const uint16_t trb_i2c_register = 0x8040; - - int i; - - trb_debug = 0; - - /* Parse Arguments */ - while ((i = getopt(argc, argv, "+hd:V")) != -1) { - switch (i) { - case '?': - usage(basename(argv[0])); - exit(EXIT_FAILURE); - case 'h': - usage(basename(argv[0])); - exit(EXIT_SUCCESS); - case 'd': - trb_debug = strtoul(optarg, NULL, 0); - break; - case 'V': - printf("%s %s, using libtrbnet %s\n", - basename(argv[0]), trb_i2c_version, trbnet_version); - exit(EXIT_SUCCESS); - break; - default: - break; - } - } - - if (optind >= argc) { - usage(basename(argv[0])); - exit(EXIT_FAILURE); - } - - /* Open port */ - init_ports(); - - if (strcmp(argv[optind], "w") == 0) { - - /*************************************************/ - /* I2C write */ - /*************************************************/ - - uint32_t *buffer = NULL; - uint32_t value = 0; - uint16_t trb_address = 0; - uint8_t i2c_chip = 0; - uint8_t i2c_register = 0; - uint8_t i2c_value = 0; - int status = 0; - - if (argc - optind != 5) { - usage(argv[0]); - exit(EXIT_FAILURE); - } - - buffer = (uint32_t*)malloc(sizeof(uint32_t) * BUFFER_SIZE); - if (buffer == NULL) abort(); - - trb_address = (uint16_t)strtoul(argv[optind + 1], NULL, 0); - i2c_chip = (uint8_t)strtoul(argv[optind + 2], NULL, 0); - i2c_register = (uint8_t)strtoul(argv[optind + 3], NULL, 0); - i2c_value = (uint8_t)strtoul(argv[optind + 4], NULL, 0); - - /* Check whether I2C is ready */ - if (readI2CRegister(trb_address, 0, buffer, BUFFER_SIZE) == -1) { - free(buffer); - exit(EXIT_FAILURE); - } - - /* Write value */ - value = 0xbf000000 | (i2c_chip << 16) | (i2c_register << 8) | i2c_value; - if (trb_register_write(trb_address, trb_i2c_register, value) == -1) { - trb_error("Error writing value"); - clearI2C(trb_address); - free(buffer); - exit(EXIT_FAILURE); - } - - /* Wait for ACK */ - if ((status = readI2CRegister(trb_address, 1, buffer, BUFFER_SIZE)) - == -1) { - clearI2C(trb_address); - free(buffer); - exit(EXIT_FAILURE); - } - - /* Clear IC2 bus */ - if (clearI2C(trb_address) == -1) { - free(buffer); - exit(EXIT_FAILURE); - } - - free(buffer); - } else if (strcmp(argv[optind], "r") == 0) { - - /*************************************************/ - /* I2C read */ - /*************************************************/ - - uint32_t *buffer = NULL; - int status = 0; - uint16_t trb_address = 0; - uint32_t value = 0; - uint8_t i2c_chip = 0; - uint8_t i2c_register = 0; - - int i; - - if (argc - optind != 4) { - usage(argv[0]); - exit(EXIT_FAILURE); - } - - buffer = (uint32_t*)malloc(sizeof(uint32_t) * BUFFER_SIZE); - if (buffer == NULL) abort(); - - trb_address = (uint16_t)strtoul(argv[optind + 1], NULL, 0); - i2c_chip = (uint8_t)strtoul(argv[optind + 2], NULL, 0); - i2c_register = (uint8_t)strtoul(argv[optind + 3], NULL, 0); - - /* Check whether I2C is ready */ - if (readI2CRegister(trb_address, 0, buffer, BUFFER_SIZE) == -1) { - trb_error("I2C not ready"); - free(buffer); - exit(EXIT_FAILURE); - } - - /* Read Value */ - value = 0xff000000 | (i2c_chip << 16) | (i2c_register << 8); - if (trb_register_write(trb_address, trb_i2c_register, value) == -1) { - trb_error("Error reading value"); - clearI2C(trb_address); - free(buffer); - exit(EXIT_FAILURE); - } - - /* Wait for ACK */ - if ((status = readI2CRegister(trb_address, 1, buffer, BUFFER_SIZE)) - == -1) { - clearI2C(trb_address); - free(buffer); - exit(EXIT_FAILURE); - } - - /* Clear IC2 bus */ - if (clearI2C(trb_address) == -1) { - free(buffer); - exit(EXIT_FAILURE); - } - - /* Print results */ - for (i = 0; i < status; i += 2) { - printf("0x%04x 0x%02x\n", buffer[i], buffer[i + 1] & 0xff); - } - - free(buffer); - } else if (strcmp(argv[optind], "c") == 0) { - - /*************************************************/ - /* I2C clear */ - /*************************************************/ - - uint16_t trb_address; - - if (argc - optind != 2) { - usage(argv[0]); - exit(EXIT_FAILURE); - } - - trb_address = (uint16_t)strtoul(argv[optind + 1], NULL, 0); - exit((clearI2C(trb_address) == 0 ? EXIT_SUCCESS : EXIT_FAILURE)); - - } else { - usage(basename(argv[0])); - exit(EXIT_FAILURE); - } - - exit(EXIT_SUCCESS); -} diff --git a/trbnetd/trbcmd.c b/trbnetd/trbcmd.c deleted file mode 100644 index d9db3fd..0000000 --- a/trbnetd/trbcmd.c +++ /dev/null @@ -1,1343 +0,0 @@ -/* - * Changes by Boris and Jan: - * - * 1. Original code is in a sub-routine called start() - * 2. main() can enter a tcp loop (if called with attribute \"tcp\") or - * just call start() (otherwise) - * - * - */ - -#define _GNU_SOURCE -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -#ifndef HEXMODE -#define HEXMODE 0 -#endif - -static int hexMode = HEXMODE; - -static const char trbcmd_version[] = "$Revision: 1.1 $"; - -#define BACKLOG 10 -static uint16_t tcp_port = 55555; -static int tcp_debug = 0; /* turn to 0 to suppress TCP/IP output */ - -/* ---- User Buffer Size ------------------------------------------------ */ - -static const size_t NUM_ENDPOINTS = 1024; /* Maximum of 16KByte */ -static size_t USER_BUFFER_SIZE = 0; - -/* ---- Error Handling -------------------------------------------------- */ - -static FILE *scriptFile = NULL; -static unsigned int lineCtr = 0; - -typedef enum { - INVALID = 0, - DEBUG = 1, - WARNING = 2, - ERROR = 3, -} ErrType; - -static const char errTypeString[4][32] = { - "INVALID", - "DEBUG", - "WARNING", - "ERROR" -}; - -static void logError(int type, const char* format, ...) -{ - char fmt[512] = ""; - va_list args; - - va_start(args, format); - - if (scriptFile != NULL) { - snprintf(fmt, 512, "%s: Line #%d: %s", - errTypeString[type], lineCtr, format); - } else { - snprintf(fmt, 512, "%s: %s", errTypeString[type], format); - } - - vfprintf(stderr, fmt, args); - va_end(args); -} - -/* ------ MAIN ---------------------------------------------------------- */ - -void usage(const char *progName) -{ - fprintf(stdout, "Usage: %s [-h] [-f script-file] [-n number] [-d level] " - "[-H] [-V] \n", progName); - fprintf(stdout, "Options:\n"); - fprintf(stdout, " -h give this help\n"); - fprintf(stdout, " -f execute commands given in script-file\n"); - fprintf(stdout, " -n repeat COMMAND number times, -1 = endless loop\n"); - fprintf(stdout, - " -M turn on HighMemoryMode (maximum usage is 20MByte, default: " - "3MByte)\n"); - fprintf(stdout, " -d turn on Debugging Information\n"); - fprintf(stdout, " level 1: TRB_Package debugging\n"); - fprintf(stdout, " level 2: +FIFO debugging\n"); - fprintf(stdout, " -D FIFO DMA-Mode\n"); - fprintf(stdout, - " -H hex-mode: all arguments will be interpreted " - "as hexadecimal-numbers\n"); - fprintf(stdout, " -V version number\n"); - fprintf(stdout, "\nCommands:\n"); - fprintf(stdout, " r -> " - "read register\n"); - fprintf(stdout, " w -> " - "write register\n"); - fprintf(stdout, " rm -> " - "read register-memory\n"); - fprintf(stdout, " wm -> " - "write to register-memory\n" - " " - "from ASCII-file\n" - " " - "('-' = stdin)\n"); - fprintf(stdout, " i -> " - "read unique ID\n"); - fprintf(stdout, " s -> " - "set trb-address\n"); - fprintf(stdout, " T -> " - "send trigger\n", '%'); - fprintf(stdout, " TR -> " - "send trigger to RICH only\n", '%'); - fprintf(stdout, " I -> " - "read IPU data\n", '%'); - - fprintf(stdout, " setbit -> " - "set bits of a register\n"); - fprintf(stdout, " clearbit -> " - "clear bits of a register\n"); - fprintf(stdout, " loadbit -> " - "load bits of a register\n"); - - fprintf(stdout, " reload -> " - "reload FPGA\n"); - fprintf(stdout, " reset -> " - "reset TRBNetwork\n"); - fprintf(stdout, " comreset -> " - "reset Etrax-FIFO Logic\n"); - fprintf(stdout, " f -> " - "flush FIFO of channel\n"); - fprintf(stdout, " R -> " - "read register of the FPGA\n"); - fprintf(stdout, " W -> " - "write to register of the\n" - " " - "FPGA\n"); - fprintf(stdout, " exit/quit -> " - "disconnect from server\n" - " " - "(tcp-server mode only)\n\n"); - - fprintf(stdout, "Start as TCP/IP-Server:\n"); - fprintf(stdout, "Usage: %s [-h] [-d] [-p portnumber] [-b] [-V] tcp\n", - progName); - fprintf(stdout, "Options:\n"); - fprintf(stdout, " -h give this help\n"); - fprintf(stdout, " -p tcp port number (default: 55555)\n"); - fprintf(stdout, " -b run in background as daemon\n"); - fprintf(stdout, " -d turn on debugging informations (default: off)\n"); - fprintf(stdout, " -V version number\n"); -} - -#define CMD_SIZE 256 -#define CMD_MAX_NUM 10 - -int start(int argc, char **argv) -{ - char scriptFileName[256] = ""; - char cmd[CMD_MAX_NUM][CMD_SIZE]; - char *cmdLine = NULL; - size_t cmdLineLen = 0; - unsigned int cmdLen = 0; - uint16_t trb_address = 0; - uint16_t reg_address = 0; - int loop = 1; - int loopCtr = 0; - uint16_t trgCtr = 0; /* counter for the %ctr option */ - int opt; - int i; - - for (i = 0; i < CMD_MAX_NUM; i++) { - cmd[i][0] = 0; - } - - /* LowMem Settings, i.e. 3 MBye maximum */ - USER_BUFFER_SIZE = 786432; /* 0xc000 * 4 Byte */ - - trb_debug = 0; - - /* Parse Arguments */ - optind = 1; - while ((opt = getopt(argc, argv, "+hf:n:d:DHMV")) != -1) { - switch (opt) { - case '?': - usage(basename(argv[0])); - return -1; - case 'h': - usage(basename(argv[0])); - return 0; - case 'f': - strncpy(scriptFileName, optarg, 256); - break; - case 'n': - loop = strtol(optarg, NULL, 0); - break; - case 'd': - trb_debug = strtoul(optarg, NULL, 0); - break; - case 'D': - trb_dma = 1; - break; - case 'H': - hexMode = 1; - break; - case 'V': - printf("%s %s, using libtrbnet %s\n", - basename(argv[0]), trbcmd_version, trbnet_version); - return 0; - break; - case 'M': - /* HighMem Setting, i.e. 20 MByte maximum 0x500000 * 4 */ - USER_BUFFER_SIZE = 5242880; - break; - default: - break; - } - } - - /* Open scriptFile if requested */ - if (strlen(scriptFileName) > 0) { - if (strncmp(scriptFileName, "-", 256) == 0) { - scriptFile = stdin; - } else { - scriptFile = fopen(scriptFileName, "r"); - if (scriptFile == NULL) { - logError(ERROR, "opening ScriptFile '%s': %s\n", - scriptFileName, strerror(errno)); - return -1; - } - } - } - - /* Start repeat-loop */ - while ((loop == -1) || (loopCtr++ < loop)) { - 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 { - char *c = NULL; - unsigned int i; - - /* Get next command from file */ - lineCtr++; - /* Initialize */ - for (i = 0; i < CMD_MAX_NUM; i++) { - cmd[i][0] = '\0'; - } - - if ((scriptStatus = - getline(&cmdLine, &cmdLineLen, scriptFile)) == -1) { - if (feof(scriptFile) != 0) { - /* EOF reached */ - rewind(scriptFile); - continue; - } else { - /* Error reading line */ - logError(ERROR, "reading rewind script-file\n"); - return -1; - } - } - - /* 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; - } - - if (scriptFile != NULL) { - fprintf(stdout, "#Line %d: %s\n", lineCtr, cmdLine); - } - } - - if (strncmp(cmd[0], "w", CMD_SIZE) == 0) { - - /*******************************************/ - /* Register Write */ - /*******************************************/ - - uint32_t value = 0; - - if (cmdLen != 4) { - logError(ERROR, "Invalid command, try -h option\n"); - return -1; - } - - 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); - } - - if (trb_register_write(trb_address, reg_address, value) == -1) { - logError(ERROR, "write_register failed %s\n"); - return -1; - } else { - if (trb_errno == TRB_STATUS_WARNING) { - logError(WARNING, "Status-Bit(s) have been set:\n%s\n", - trb_strterm(trb_term)); - } - } - - } else if (strncmp(cmd[0], "r", CMD_SIZE) == 0) { - - /*******************************************/ - /* Register Read */ - /*******************************************/ - - int status = 0; - uint32_t *data = NULL; - int i; - - if (cmdLen != 3) { - logError(ERROR, "Invalid command, try -h option\n"); - return -1; - } - - 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); - } - - data = (uint32_t *) malloc(sizeof(uint32_t) * NUM_ENDPOINTS * 2); - if (data == NULL) - abort(); - - status = trb_register_read(trb_address, reg_address, - data, NUM_ENDPOINTS * 2); - if (status == -1) { - logError(ERROR, "read_register failed %s\n", - trb_strerror(trb_errno)); - } else { - for (i = 0; i < status; i += 2) { - fprintf(stdout, "0x%04x 0x%08x\n", data[i], data[i + 1]); - } - - /* Check Status-Bits */ - if (trb_errno == TRB_STATUS_WARNING) { - logError(WARNING, "Status-Bit(s) have been set:\n%s\n", - trb_strterm(trb_term)); - } - } - free(data); - - } 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 (cmdLen != 5) { - logError(ERROR, "Invalid command, try -h option\n"); - return -1; - } - - 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); - } - - data = (uint32_t *) malloc(sizeof(uint32_t) * USER_BUFFER_SIZE); - if (data == NULL) - abort(); - - status = trb_register_read_mem(trb_address, reg_address, option, - size, data, USER_BUFFER_SIZE); - if (status == -1) { - logError(ERROR, "read_register_mem failed: %s\n", - trb_strerror(trb_errno)); - return -1; - } else { - /* Print data-buffer */ - p = data; - end = p + status; - while (p < end) { - len = (*p >> 16) & 0xffff; - fprintf(stdout, "H: 0x%04x 0x%04x\n", (*p++) & 0xffff, len); - for (i = 0; (i < len) && (p < end); i++) { - fprintf(stdout, "0x%04x 0x%08x\n", - (option == 0 ? reg_address + i : i), - *p++); - } - } - /* Check Status-Bits */ - if (trb_errno == TRB_STATUS_WARNING) { - logError(WARNING, "Status-Bits are active:\n%s\n", - trb_strterm(trb_term)); - } - } - 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 (cmdLen != 5) { - logError(ERROR, "Invalid command, try -h option\n"); - return -1; - } - - 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) { - logError(ERROR, "opening file '%s': %s\n", - fileName, strerror(errno)); - return -1; - } - } - - data = (uint32_t *) malloc(sizeof(uint32_t) * dataSize); - if (data == NULL) - abort(); - - while (getline(&line, &len, file) != -1) { - if (size >= dataSize) { - dataSize += 64; - data = (uint32_t *) realloc(data, sizeof(uint32_t) * dataSize); - if (data == NULL) - abort(); - } - data[size++] = strtoul(line, NULL, hexMode == 1 ? 16 : 0); - } - free(line); - fclose(file); - - /* 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 (status == -1) { - logError(ERROR, "write_register_memory failed: %s\n", - trb_strerror(trb_errno)); - return -1; - } else { - /* Check Status-Bits */ - if (trb_errno == TRB_STATUS_WARNING) { - logError(WARNING, "Status-Bits are active:\n%s\n", - trb_strterm(trb_term)); - } - } - - free(data); - - } else if (strncmp(cmd[0], "i", CMD_SIZE) == 0) { - - /*******************************************/ - /* ReadUId */ - /*******************************************/ - - uint32_t *uidBuffer = NULL; - int status; - unsigned int i; - - if (cmdLen != 2) { - logError(ERROR, "Invalid command, try -h option\n"); - return -1; - } - - 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); - } - - uidBuffer = (uint32_t *) malloc(sizeof(uint32_t) * NUM_ENDPOINTS * 4); - if (uidBuffer == NULL) - abort(); - - status = trb_read_uid(trb_address, uidBuffer, NUM_ENDPOINTS * 4); - if (status == -1) { - logError(ERROR, "read_uid failed: %s\n", - trb_strerror(trb_errno)); - return -1; - } else { - for (i = 0; - ((i < (unsigned int)status) && (i < NUM_ENDPOINTS * 4)); - i += 4) { - fprintf(stdout, "0x%04x 0x%08x%08x 0x%02x\n", - uidBuffer[i + 3], - uidBuffer[i], uidBuffer[i + 1], uidBuffer[i + 2]); - } - /* Check Status-Bits */ - if (trb_errno == TRB_STATUS_WARNING) { - logError(WARNING, "Status-Bits are active:\n%s\n", - trb_strterm(trb_term)); - } - } - - free(uidBuffer); - - } else if (strncmp(cmd[0], "s", CMD_SIZE) == 0) { - - /*******************************************/ - /* SetAddress */ - /*******************************************/ - - uint64_t uid = 0; - uint8_t endpoint = 0; - uint16_t trb_address = 0; - - if (cmdLen != 4) { - logError(ERROR, "Invalid command, try -h option\n"); - return -1; - } - - 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) { - logError(ERROR, "set_address failed: %s\n", - trb_strerror(trb_errno)); - return -1; - } else { - /* Check Status-Bits */ - if (trb_errno == TRB_STATUS_WARNING) { - logError(WARNING, "Status-Bits are active:\n%s\n", - trb_strterm(trb_term)); - } - } - - } else if (strncmp(cmd[0], "T", CMD_SIZE) == 0) { - - /*******************************************/ - /* Send Trigger */ - /*******************************************/ - - uint8_t type; - uint8_t random = 0; - uint32_t info = 0; - uint16_t number = 0; - - if (cmdLen != 5) { - logError(ERROR, "Invalid command, try -h option\n"); - return -1; - } - - 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); - if (strncmp(cmd[4], "%ctr", CMD_SIZE) == 0) { - if (scriptFile == NULL) { - usage(basename(argv[0])); - return -1; - } - number = trgCtr; - } else { - number = strtoul(cmd[4], NULL, hexMode == 1 ? 16 : 0); - } - - /* DEBUG Info */ - if (trb_debug > 0) { - fprintf(stderr, - "Command: SEND_TRIGGER: " - "type: 0x%01x, " - "random: 0x%02x, " - "info: 0x%06x, " - "number: 0x%04x\n", type, random, info, number); - } - - if (trb_send_trigger(type, info, random, number) == -1) { - logError(ERROR, "send_trigger failed: %s\n", - trb_strerror(trb_errno)); - return -1; - } else { - /* Check Status-Bits */ - if (trb_errno == TRB_STATUS_WARNING) { - logError(WARNING, "Status-Bits are active:\n%s\n", - trb_strterm(trb_term)); - } - } - - } else if (strncmp(cmd[0], "reset", CMD_SIZE) == 0) { - - /*******************************************/ - /* TRBNet Reset */ - /*******************************************/ - - if (cmdLen != 1) { - logError(ERROR, "Invalid command, try -h option\n"); - return -1; - } - - /* DEBUG Info */ - if (trb_debug > 0) { - fprintf(stderr, "Command: RESET:\n"); - } - - if (network_reset() == -1) { - logError(ERROR, "TRBNet RESET failed\n"); - return -1; - } - - } else if (strncmp(cmd[0], "comreset", CMD_SIZE) == 0) { - - /*******************************************/ - /* Etrax-FIFO Reset */ - /*******************************************/ - - if (cmdLen != 1) { - logError(ERROR, "Invalid command, try -h option\n"); - return -1; - } - - /* DEBUG Info */ - if (trb_debug > 0) { - fprintf(stderr, "Command: COMRESET:\n"); - } - - if (com_reset() == -1) { - logError(ERROR, "Etrax RESET failed: %s\n", - trb_strerror(trb_errno)); - return -1; - } - - } else if (strncmp(cmd[0], "reload", CMD_SIZE) == 0) { - - /*********************************************/ - /* FPGA Reload */ - /*********************************************/ - - if (cmdLen != 2) { - logError(ERROR, "Invalid command, try -h option\n"); - return -1; - } - - trb_address = strtoul(cmd[1], NULL, hexMode == 1 ? 16 : 0); - - /* DEBUG Info */ - if (trb_debug > 0) { - fprintf(stderr, - "Command: RELOAD: trb_address: 0x%04x\n", trb_address); - } - - if (trb_register_write(trb_address, 0x0020, 0x8000) == -1) { - logError(ERROR, "FPGA reload failed: %s\n", - trb_strerror(trb_errno)); - return -1; - } else { - /* Check Status-Bits */ - if (trb_errno == TRB_STATUS_WARNING) { - logError(WARNING, "Status-Bits are active:\n%s\n", - trb_strterm(trb_term)); - } - } - - } 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; - uint32_t info = 0; - uint16_t number = 0; - - if (cmdLen != 6) { - logError(ERROR, "Invalid command, try -h option\n"); - return -1; - } - - 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); - - if (strncmp(cmd[5], "%ctr", CMD_SIZE) == 0) { - if (scriptFile == NULL) { - usage(basename(argv[0])); - return -1; - } - number = trgCtr; - } else { - 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%06x, " - "number: 0x%04x\n", input, type, random, info, number); - } - - if (trb_send_trigger_rich(input, type, info, random, number) == -1) { - logError(ERROR, "send_trigger failed: %s\n", - trb_strerror(trb_errno)); - return -1; - } else { - /* Check Status-Bits */ - if (trb_errno == TRB_STATUS_WARNING) { - logError(WARNING, "Status-Bits are active:\n%s\n", - trb_strterm(trb_term)); - } - } - - } else if (strncmp(cmd[0], "I", CMD_SIZE) == 0) { - - /*******************************************/ - /* IPU channel readout */ - /*******************************************/ - - uint32_t *buffer = NULL; - uint8_t type = 0; - uint8_t random = 0; - uint8_t info = 0; - uint16_t number = 0; - int status = 0; - int i; - - if (cmdLen != 5) { - logError(ERROR, "Invalid command, try -h option\n"); - return -1; - } - - 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); - if (strncmp(cmd[4], "%ctr", CMD_SIZE) == 0) { - if (scriptFile == NULL) { - usage(basename(argv[0])); - return -1; - } - number = trgCtr; - } else { - 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); - } - - buffer = (uint32_t *) malloc(sizeof(uint32_t) * USER_BUFFER_SIZE); - if (buffer == NULL) - abort(); - - status = trb_ipu_data_read(type, info, random, number, - buffer, USER_BUFFER_SIZE); - if (status == -1) { - logError(ERROR, "read_ipu_data failed: %s\n", - trb_strerror(trb_errno)); - return -1; - } else { - for (i = 0; i < status; i++) { - fprintf(stdout, "0x%08x\n", buffer[i]); - } - /* Check Status-Bits */ - if (trb_errno == TRB_STATUS_WARNING) { - logError(WARNING, "Status-Bits are active:\n%s\n", - trb_strterm(trb_term)); - } - } - - free(buffer); - - } else if (strncmp(cmd[0], "f", CMD_SIZE) == 0) { - - /*******************************************/ - /* Flush FIFO Channel */ - /*******************************************/ - - uint8_t channel = 0; - - if (cmdLen != 2) { - logError(ERROR, "Invalid command, try -h option\n"); - return -1; - } - - channel = strtoul(cmd[1], NULL, hexMode == 1 ? 16 : 0); - if (trb_debug > 0) { - fprintf(stderr, "Command: FIFO_FLUSH_CHANNEL #%d\n", channel); - } - - if (trb_fifo_flush(channel) == -1) { - logError(ERROR, "trb_fifo_flush failed: %s\n", - trb_strerror(trb_errno)); - return -1; - } - - } else if (strncmp(cmd[0], "R", CMD_SIZE) == 0) { - - /*******************************************/ - /* Read FIFO Register */ - /*******************************************/ - - uint32_t value = 0; - uint16_t reg_address = 0; - - if (cmdLen != 2) { - logError(ERROR, "Invalid command, try -h option\n"); - return -1; - } - - 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); - } - - if (fpga_register_read(reg_address, &value) == -1) { - logError(ERROR, "fpga_register_read failed: %s\n", - trb_strerror(trb_errno)); - return -1; - } else { - fprintf(stdout, "0x%04x 0x%08x\n", reg_address, value); - } - - } else if (strncmp(cmd[0], "W", CMD_SIZE) == 0) { - - /*******************************************/ - /* Write FIFO Register */ - /*******************************************/ - - uint32_t value = 0; - uint16_t reg_address = 0; - - if (cmdLen != 3) { - logError(ERROR, "Invalid command, try -h option\n"); - return -1; - } - - reg_address = strtoul(cmd[1], NULL, hexMode == 1 ? 16 : 0); - value = strtoul(cmd[2], NULL, hexMode == 1 ? 16 : 0); - - if (trb_debug > 0) { - fprintf(stderr, - "Command: WRITE_FIFO_REGISTER:" - "reg_address: 0x%04x, " - "value: 0x%08x\n", reg_address, value); - } - - if (fpga_register_write(reg_address, value) == -1) { - logError(ERROR, "fpga_register_write failed\n"); - return -1; - } - - } else if (strncmp(cmd[0], "setbit", CMD_SIZE) == 0) { - - /*******************************************/ - /* Register Set Bits */ - /*******************************************/ - - int status = 0; - - uint32_t bitMask; - - if (cmdLen != 4) { - logError(ERROR, "Invalid command, try -h option\n"); - return -1; - } - - trb_address = strtoul(cmd[1], NULL, hexMode == 1 ? 16 : 0); - reg_address = strtoul(cmd[2], NULL, hexMode == 1 ? 16 : 0); - bitMask = strtoul(cmd[3], NULL, hexMode == 1 ? 16 : 0); - - /* DEBUG Info */ - if (trb_debug > 0) { - fprintf(stderr, - "Command: SETBIT: trb_address: 0x%04x, " - "reg_address: 0x%04x, bitMask: 0x%04x\n", - trb_address, reg_address, bitMask); - } - - status = trb_register_modify(trb_address, reg_address, - 1, bitMask, 0); - if (status == -1) { - logError(ERROR, "setbit of register failed: %s\n", - trb_strerror(trb_errno)); - return -1; - } - - } else if (strncmp(cmd[0], "clearbit", CMD_SIZE) == 0) { - - /*******************************************/ - /* Register Clear Bits */ - /*******************************************/ - - int status = 0; - - uint32_t bitMask; - - if (cmdLen != 4) { - logError(ERROR, "Invalid command, try -h option\n"); - return -1; - } - - trb_address = strtoul(cmd[1], NULL, hexMode == 1 ? 16 : 0); - reg_address = strtoul(cmd[2], NULL, hexMode == 1 ? 16 : 0); - bitMask = strtoul(cmd[3], NULL, hexMode == 1 ? 16 : 0); - - /* DEBUG Info */ - if (trb_debug > 0) { - fprintf(stderr, - "Command: CLEARBIT: trb_address: 0x%04x, " - "reg_address: 0x%04x, bitMask: 0x%04x\n", - trb_address, reg_address, bitMask); - } - - status = trb_register_modify(trb_address, reg_address, - 2, bitMask, 0); - if (status == -1) { - logError(ERROR, "clearbit of register failed: %s\n", - trb_strerror(trb_errno)); - - return -1; - } - - } else if (strncmp(cmd[0], "loadbit", CMD_SIZE) == 0) { - - /*******************************************/ - /* Register Load Bits */ - /*******************************************/ - - int status = 0; - - uint32_t bitMask; - uint32_t bitValue; - - if (cmdLen != 5) { - logError(ERROR, "Invalid command, try -h option\n"); - return -1; - } - - trb_address = strtoul(cmd[1], NULL, hexMode == 1 ? 16 : 0); - reg_address = strtoul(cmd[2], NULL, hexMode == 1 ? 16 : 0); - bitMask = strtoul(cmd[3], NULL, hexMode == 1 ? 16 : 0); - bitValue = strtoul(cmd[4], NULL, hexMode == 1 ? 16 : 0); - - /* DEBUG Info */ - if (trb_debug > 0) { - fprintf(stderr, - "Command: LOADBIT: trb_address: 0x%04x, " - "reg_address: 0x%04x, bitMask: 0x%04x bitValue: 0x%04x\n", - trb_address, reg_address, bitMask, bitValue); - } - - status = trb_register_modify(trb_address, reg_address, - 3, bitMask, bitValue); - if (status == -1) { - logError(ERROR, "loadbit of register failed: %s\n", - trb_strerror(trb_errno)); - return -1; - } - - } else { - - /*******************************************/ - /* Not a valid command */ - /*******************************************/ - - logError(ERROR, "Invalid command, try -h option\n"); - return -1; - } - } /* End script-file-loop */ - - trgCtr++; - } /* End repeat-loop */ - - /* Cleanup */ - if (scriptFile != NULL) { - fclose(scriptFile); - free(cmdLine); - } - - return 0; -} - -static int sockfd = -1; -static pid_t myPid = -1; -static int myFd = -1; - -static void atexit0(void) -{ - shutdown(myFd, SHUT_RDWR); - if (close(myFd) == -1) perror("atexit0 close"); -} - -static void sigHandler(int sig) -{ - if (sig == SIGCHLD) { - pid_t pid = -1; - while ((pid = waitpid(WAIT_ANY, NULL, WNOHANG)) > 0) { - if (tcp_debug == 1) { - fprintf(stderr, "caught SIGCHLD, pid: %d has terminated\n", pid); - } - } - } -} - -int main(int argc, char **argv) -{ - unsigned int i; - - stdout = stdout; - stderr = stderr; - - if (strcmp(argv[argc - 1], "tcp") != 0) { - /* Run normal TRBCMD */ - if (init_ports() == -1) exit(EXIT_FAILURE); - - if (start(argc, argv) == 0) { - exit(EXIT_SUCCESS); - } - exit(EXIT_FAILURE); - } else { - /* Run TCP/IP server */ - struct sockaddr_in my_addr; - struct sockaddr_in their_addr; - int yes = 1; - int daemonMode = 0; - int opt; - - /* Parse Arguments */ - optind = 1; - while ((opt = getopt(argc, argv, "+hdVbp:")) != -1) { - switch (opt) { - case '?': - usage(basename(argv[0])); - exit(EXIT_FAILURE); - case 'h': - usage(basename(argv[0])); - exit(EXIT_SUCCESS); - case 'V': - printf("%s %s, using libtrbnet %s\n", - basename(argv[0]), trbcmd_version, trbnet_version); - exit(EXIT_SUCCESS); - break; - case 'p': - tcp_port = strtol(optarg, NULL, 0); - break; - case 'd': - tcp_debug = 1; - break; - case 'b': - daemonMode = 1; - break; - default: - break; - } - } - - if (daemonMode == 1) { - /* fork the first time */ - if ((myPid = fork()) == -1) { /* Error fork, Mother exit, no child */ - perror(argv[0]); - exit(EXIT_FAILURE); - } - if (myPid > 0) { - exit(EXIT_SUCCESS); /* Mother exit */ - } - /* fork the second time */ - setsid(); - if ((myPid = fork()) == -1) { /* Error fork, Mother exit, no child */ - perror(argv[0]); - exit(EXIT_FAILURE); - } - if (myPid > 0) { - exit(EXIT_SUCCESS); /* Mother exit */ - } - /* close stdin, stdout, stderr, change dir */ - chdir("/"); - freopen("/dev/null", "r", stdin); - freopen("/dev/null", "a", stdout); - freopen("/dev/console", "a", stderr); - tcp_debug = 0; - } - - sockfd = socket(AF_INET, SOCK_STREAM, 0); - if (sockfd == -1) { - perror("socket() error!"); - exit(EXIT_FAILURE); - } else if (tcp_debug > 0) { - fprintf(stderr, "\nsocket() OK...\n"); - } - - my_addr.sin_family = AF_INET; - my_addr.sin_port = htons(tcp_port); - my_addr.sin_addr.s_addr = INADDR_ANY; - memset(&(my_addr.sin_zero), 0, 8); - - if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) - == -1) { - perror("setsockopt() error!"); - exit(EXIT_FAILURE); - } else if (tcp_debug > 0) { - fprintf(stderr, "setsockopt() OK...\n"); - } - - if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == - -1) { - perror("bind() error!"); - exit(EXIT_FAILURE); - } else if (tcp_debug > 0) { - fprintf(stderr, "bind() OK...\n"); - } - - signal(SIGCHLD, sigHandler); - - if (listen(sockfd, BACKLOG) == -1) { - perror("listen() error!"); - exit(EXIT_FAILURE); - } else if (tcp_debug > 0) { - fprintf(stderr, - "listen() OK...\n\n* server initialized" - " to port %d [OK]\n\n", - tcp_port); - } - - if (init_ports() == -1) exit(EXIT_FAILURE); - - /* Enter an infinite loop to accept clients */ - while (1) { - socklen_t sin_size = sizeof(struct sockaddr_in); - ssize_t msgLen = 0; - char command[256]; - - if (tcp_debug > 0) { - fprintf(stderr, "\n\nlisten...\n\n"); - } - myFd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size); - if (myFd == -1) { - perror("accept() error!"); - } else if (tcp_debug > 0) { - fprintf(stderr, "accept() OK...\n"); - } - if ((myPid = fork()) == -1) { /* Error fork, Mother exit, no child */ - perror("Error fork"); - exit(EXIT_FAILURE); - } - if (myPid > 0) { - /* Mother process: close myFd and continue accepting connections */ - if (close(myFd) == -1) { - perror("Error close fd"); - exit(EXIT_FAILURE); - } - continue; - } else { - /* Child process */ - FILE* clientout = NULL; - myPid = getpid(); - write(myFd, "\n", 1); - atexit(atexit0); - - clientout = fdopen(myFd, "a"); - - /* redirect output */ - fclose(stdout); - fclose(stderr); - stdout = clientout; - stderr = clientout; - - while (1) { - msgLen = recv(myFd, (void *)command, 256, 0); - if (msgLen == -1) { - perror("Error recv"); - exit(EXIT_FAILURE); - } - if (msgLen == 0) { - if (tcp_debug > 0) { - fprintf(stderr, "Client disconnected\n"); - } - exit(EXIT_SUCCESS); - } - if ((tcp_debug > 0) && (msgLen <= 2)) { - fprintf(stderr, "%d: empty package\n", myPid); - continue; - } - msgLen -= 2; - command[msgLen] = '\0'; - - int start_argc = 0; - char *start_argv[256]; - int start_arg = 0; - - start_argv[start_argc++] = argv[0]; - for (i = 0; i < (unsigned int)msgLen; i++) { - if (isspace((int)command[i]) != 0) { - command[i] = '\0'; - } - if (command[i] != '\0') { - if (start_arg == 0) { - start_argv[start_argc++] = &command[i]; - start_arg = 1; - } else { - continue; - } - } else { - command[i] = '\0'; - start_arg = 0; - } - } - - if (tcp_debug > 0) { - fprintf(stderr, "%d: received: %i\n", myPid, msgLen); - if (msgLen > 0) { - fprintf(stderr, "%d: command: ", myPid); - for (i = 1; i < (unsigned int)start_argc; i++) { - fprintf(stderr, "%s ", start_argv[i]); - } - fprintf(stderr, "\n"); - } - } - - /* Execute command */ - if (start_argc == 1) continue; - if ((strcmp(start_argv[1], "exit") == 0) || - (strcmp(start_argv[1], "quit") == 0)) { - if (tcp_debug > 0) { - fprintf(stderr, "%d: client disconnected\n", myPid); - } - exit(EXIT_SUCCESS); - } - - start(start_argc, start_argv); - fprintf(clientout, "\n----------------------------\n\n"); - fflush(clientout); - } - } - } - } -} diff --git a/trbnetd/trbdhcp.c b/trbnetd/trbdhcp.c deleted file mode 100644 index fac0022..0000000 --- a/trbnetd/trbdhcp.c +++ /dev/null @@ -1,222 +0,0 @@ -#define _GNU_SOURCE -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -static const char trbdhcp_version[] = "$Revision: 1.1 $"; - -typedef struct { - uint16_t address; - uint64_t uid; - uint8_t endPoint; -} Trb_Endpoint; - -static Trb_Endpoint* endPointList = NULL; -static unsigned int listSize = 0; -static uint32_t* trbBuffer = NULL; - -#define NUM_ENDPOINTS 2048 - -static int readConfigFile(const char* fileName) -{ - FILE* configFile = NULL; - char *line = NULL; - size_t lineLen = 0; - unsigned int lineCtr = 0; - unsigned int counter = 0; - uint32_t address; - uint64_t uid; - uint32_t endPoint; - - listSize = 0; - - if (endPointList == NULL) return -1; - - /* Open scriptFile if requested */ - configFile = fopen(fileName, "r"); - if (configFile == NULL) { - fprintf(stderr, "Error opening configFile '%s': %s\n", - fileName, strerror(errno)); - return -1; - } - - while(1) { - char *c = NULL; - lineCtr++; - if (getline(&line, &lineLen, configFile) == -1) { - if (feof(configFile) != 0) { - /* EOF reached */ - break; - } else { - /* Error reading line */ - fprintf(stderr, "Error reading configFile '%s' line %d\n", - fileName, lineCtr); - return -1; - } - } - - /* Remove newline and comments */ - if ((c = strchr(line, '\n')) != NULL) { - *c = '\0'; - } - if ((c = strchr(line, '#')) != NULL) { - *c = '\0'; - } - - if (sscanf(line, "%x %llx %x", &address, &uid, &endPoint) != 3) { - continue; - } - if (counter < NUM_ENDPOINTS) { - endPointList[counter].address = address; - endPointList[counter].uid = uid; - endPointList[counter].endPoint = endPoint; - } else { - abort(); - } - - counter++; - } - - free(line); - - listSize = counter; - - return counter; -} - -static void dumpList() -{ - unsigned int i; - for (i = 0; i < listSize; i++) { - fprintf(stdout, "Entry #%d: 0x%04x 0x%08llx 0x%02x\n", i, - endPointList[i].address, - endPointList[i].uid, - endPointList[i].endPoint); - } -} - -/* ------ MAIN ---------------------------------------------------------- */ - -void usage(const char *progName) -{ - printf("Usage: %s [-h] [-f configFile] [-a trb-address] [-V]\n", progName); - printf("Options:\n"); - printf(" -h give this help\n"); - printf(" -f name of configFile (default: trbdhcp.conf)\n"); - printf(" -a use trb-address as broadcast (default: 0xffff)\n"); - printf(" -V Version number\n"); - printf("\nConfigFile Format:\n"); - printf("# New-Address UID EndPoint\n\n"); -} - -int main(int argc, char ** argv) -{ - char configFileName[256] = "trbdhcp.conf"; - uint16_t trbAddress = 0xffff; - int status; - unsigned int i; - int opt; - - trb_debug = 0; - - /* Parse Arguments */ - while ((opt = getopt(argc, argv, "+hd:f:a:V")) != -1) { - switch (opt) { - case '?': - usage(basename(argv[0])); - exit(EXIT_FAILURE); - case 'h': - usage(basename(argv[0])); - exit(EXIT_SUCCESS); - case 'd': - trb_debug = strtoul(optarg, NULL, 0); - break; - case 'f': - strncpy(configFileName, optarg, 256); - break; - case 'a': - trbAddress = strtoul(optarg, NULL, 0); - break; - case 'V': - printf("%s %s, using libtrbnet %s\n", - basename(argv[0]), trbdhcp_version, trbnet_version); - exit(EXIT_SUCCESS); - break; - default: - break; - } - } - - /* Allocat Memory */ - if ((endPointList = malloc(sizeof(Trb_Endpoint) * NUM_ENDPOINTS)) == NULL) { - abort(); - } - - if ((trbBuffer = malloc(sizeof(uint32_t) * 4 * NUM_ENDPOINTS)) == NULL) { - abort(); - } - - /* Read Config-File */ - if (readConfigFile(configFileName) <= 0) { - exit(EXIT_FAILURE); - } - if (trb_debug > 0) { - dumpList(); - } - - /* Open ports */ - init_ports(); - - /* Read present UIDs */ - status = trb_read_uid(trbAddress, trbBuffer, - sizeof(uint32_t) * 4 * NUM_ENDPOINTS); - if (status == -1) { - trb_error("read_uid failed"); - exit(EXIT_FAILURE); - } - - /* Assign addresses */ - for (i = 0; i < status; i += 4) { - uint16_t currentAddress = 0; - uint64_t uid = 0; - uint8_t endPoint = 0; - unsigned int j; - - uid = ((uint64_t)trbBuffer[i] << 32) | trbBuffer[i + 1]; - endPoint = trbBuffer[i + 2]; - currentAddress = trbBuffer[i + 3]; - - for (j = 0; j < listSize; j++) { - if ((uid == endPointList[j].uid) && - (endPoint == endPointList[j].endPoint)) { - - if (currentAddress != endPointList[j].address) { - if (trb_debug > 0) { - fprintf(stderr, "set address: 0x%04x 0x%08llx 0x%02x\n", - currentAddress, uid, endPoint); - } - - /* Set new Address */ - if (trb_set_address(uid, endPoint, endPointList[j].address) == -1) { - trb_error("Set address failed"); - continue; - } - } - } - } - } - - /* Cleanup */ - free(endPointList); - free(trbBuffer); - - - exit(EXIT_SUCCESS); -} diff --git a/trbnetd/trberror.c b/trbnetd/trberror.c deleted file mode 100644 index e8f1282..0000000 --- a/trbnetd/trberror.c +++ /dev/null @@ -1,169 +0,0 @@ -#include -#include -#include - -#include "trberror.h" - -/* Error Handling */ - -int trb_errno = TRB_NONE; - -TRB_TERM trb_term = {0, 0, 0, 0}; - -void trb_error(const char *s) -{ - if (s != NULL) { - fprintf(stderr, "%s: %s\n", s, trb_strerror(trb_errno)); - } else { - fprintf(stderr, "%s\n", trb_strerror(trb_errno)); - } - - /* Print Statusbits */ - if (trb_errno == TRB_STATUS_ERROR) { - fprintf(stderr, "%s\n", trb_strterm(trb_term)); - } -} - -const char* trb_strerror(int trberrno) -{ - static const char errorstring[][80] = { - "No Error", - "TX Busy", - "FIFO Not Empty", - "FIFO Timeout", - "FIFO Error, invalid H0 header(s)", - "FIFO Error, invalid sequence", - "FIFO Error, invalid read-data-mode", - "FIFO Incomplete Package", - "FIFO Invalid Header-Type", - "FIFO Termination-Package missing", - "FAILED WAIT_IS_VALID", - "FAILED WAIT_IS_NOT_VALID", - "User-Buffer Overflow", - "Invalid Channel", - "Invalid number of Packages returned", - "Termination Status Error", - "Invalid TRB-Address", - "Invalid Data-Buffer Length", - "Endpoint not reached", - "DMA not available (check whether module 'can_module.ko' is loaded)", - "DMA-Timeout", - "ReadMem invalid size", - "Invalid data-length give by Header", - "FIFO Incomplete, missing", - "SEMAPHORE Error", - "FIFO Shared Memory Error", - "Termination Status Warning" - }; - - if (trberrno < 27) { - return errorstring[trberrno]; - } else { - return "Unknown Errno"; - } -} - -const char* trb_strterm(TRB_TERM term) -{ - static const char commonStatusBits[16][64] = { - "COM_EndpointReached: no endpoint has been reached", /* status */ - "COM_Collision: collision has been detected", /* error */ - "COM_WordMissing: mismatch of packet counters (EOB)", /* error */ - "COM_Checksum: checksum error", /* error */ - "COM_DontUnderstand: endpoint doesn't understand data", /* error */ - "COM_BufferMismatch: buffer numbers (EOB and ACK) mismatch", /* error */ - "COM_AnswerMissing: One endpoint didn't react", /* status */ - "INVALID", "INVALID", "INVALID", "INVALID", "INVALID", "INVALID", - "INVALID", "INVALID", "INVALID" - }; - - static const char ch0StatusBits[16][64] = { - "Ch0_TrigCtr: trigger counter mismatch", /* status */ - "Ch0_TrigMissing: timing trigger missing", /* status */ - "INVALID", - "INVALID", - "Ch0_BuffersHalfFull: data-buffers half full", /* status */ - "Ch0_BuffersFull: data-buffers almost full", /* status */ - "Ch0_NotConfigured: endpoint is not configured", /* status */ - "INVALID", "INVALID", "INVALID", "INVALID", "INVALID", "INVALID", - "INVALID", "INVALID", "INVALID" - }; - - static const char ch1StatusBits[16][64] = { - "Ch1_EvtNum: data stream event number mismatch", /* status */ - "Ch1_TrigCode: trigger code / random mismatch", /* status */ - "Ch1_Length: invalid length", /* status */ - "Ch1_NoAnswer: answer missing", /* status */ - "Ch1_NotFound: requested event number mismatch", /* status */ - "Ch1_DataMissing: parts of the data are missing",/* status */ - "Ch1_Sync: serious sync problem detected", /* status */ - "Ch1_EvtBroken: event data corrupted", /* status */ - "INVALID", "INVALID", "INVALID", "INVALID", "INVALID", - "INVALID", "INVALID", "INVALID" - }; - - static const char ch2StatusBits[16][64] = { - "INVALID", "INVALID", "INVALID", "INVALID", "INVALID", "INVALID", - "INVALID", "INVALID", "INVALID", "INVALID", "INVALID", "INVALID", - "INVALID", "INVALID", "INVALID", "INVALID" - }; - - static const char ch3StatusBits[16][64] = { - "Ch3_Address: unknown address", /* status */ - "Ch3_Timeout: timeout error", /* error */ - "Ch3_NoMoreData: no more data" /* status */ - "INVALID", "INVALID", "INVALID", "INVALID", "INVALID", "INVALID", - "INVALID", "INVALID", "INVALID", "INVALID", "INVALID", "INVALID", - "INVALID" - }; - - static const char *chStatusBits[4] = { - &ch0StatusBits[0][0], - &ch1StatusBits[0][0], - &ch2StatusBits[0][0], - &ch3StatusBits[0][0] - }; - - static char buffer[2048] = ""; - unsigned int i; - - if (term.channel >= 4) { - sprintf(buffer, "INVALID ChannelId %d", term.channel); - return buffer; - } - - sprintf(buffer, - "CommonStatusBits: 0x%04x, Channel#%d StatusBits: 0x%04x", - term.status_common, - term.channel, - term.status_channel); - - for (i = 0; i < 16; i++) { - if ((term.status_common & (0x01 << i)) != (i != 0 ? 0 : 1)) { - strcat(buffer, "\n "); - strcat(buffer, commonStatusBits[i]); - } - } - for (i = 0; i < 16; i++) { - if ((term.status_channel & (0x01 << i)) != 0) { - strcat(buffer, "\n "); - strcat(buffer, chStatusBits[term.channel] + 64 * i); - } - } - - return buffer; -} - -const char* trb_strerrorf() -{ - static char buffer[4096] = ""; - - if (trb_errno == TRB_STATUS_ERROR) { - snprintf(buffer, 4096, "%s\n%s", - trb_strerror(trb_errno), trb_strterm(trb_term)); - } else { - snprintf(buffer, 4096, "%s", trb_strerror(trb_errno)); - } - - return buffer; -} diff --git a/trbnetd/trberror.h b/trbnetd/trberror.h deleted file mode 100644 index 695b798..0000000 --- a/trbnetd/trberror.h +++ /dev/null @@ -1,66 +0,0 @@ -#ifndef TRBERROR_H -#define TRBERROR_H - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -typedef enum { - TRB_NONE = 0, - TRB_TX_BUSY = 1, - TRB_FIFO_NOT_EMPTY = 2, - TRB_FIFO_TIMEOUT = 3, - TRB_FIFO_HEADERS = 4, - TRB_FIFO_SEQUENZ = 5, - TRB_FIFO_INVALID_MODE = 6, - TRB_FIFO_INCOMPLETE_PACKAGE = 7, - TRB_FIFO_INVALID_HEADER = 8, - TRB_FIFO_MISSING_TERM_HEADER = 9, - TRB_FAILED_WAIT_IS_VALID = 10, - TRB_FAILED_WAIT_IS_NOT_VALID = 11, - TRB_USER_BUFFER_OVF = 12, - TRB_INVALID_CHANNEL = 13, - TRB_INVALID_PKG_NUMBER = 14, - TRB_STATUS_ERROR = 15, - TRB_INVALID_ADDRESS = 16, - TRB_INVALID_LENGTH = 17, - TRB_ENDPOINT_NOT_REACHED = 18, - TRB_DMA_UNAVAILABLE = 19, - TRB_DMA_TIMEOUT = 20, - TRB_READMEM_INVALID_SIZE = 21, - TRB_HDR_DLEN = 22, - TRB_FIFO_INCOMPLETE = 23, - TRB_SEMAPHORE = 24, - TRB_FIFO_SHARED_MEM = 25, - TRB_STATUS_WARNING = 26 -} TRB_ERROR; - -extern int trb_errno; - -void trb_error(const char *s); - -const char* trb_strerror(int trberrno); - - -/* last TRBNet-TermPackage */ - -typedef struct { - uint16_t status_common; - uint16_t status_channel; - uint16_t sequence; - uint8_t channel; -} TRB_TERM; - -extern TRB_TERM trb_term; - -const char* trb_strterm(TRB_TERM term); - -const char* trb_strerrorf(); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/trbnetd/trbflash.c b/trbnetd/trbflash.c deleted file mode 100644 index 1c3f306..0000000 --- a/trbnetd/trbflash.c +++ /dev/null @@ -1,1553 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -static const uint16_t HardwareId = 0x0042; -static const uint16_t CtrlReg = 0xd000; -static const uint16_t SetupReg = 0xd001; -static const uint16_t BlockRam = 0xd100; -static const uint16_t MDCFlashRomSelect = 0xd200; - -static uint32_t* trbBuffer = NULL; -static uint8_t** pageBuffer = NULL; -static uint16_t* pageBufferAddress = NULL; - -static uint8_t* imageBuffer = NULL; - -/* Maximum number of endpoints */ -#define NUM_ENDPOINTS 1024 - -/* 32-Bit Words: */ -#define BLOCKRAM_SIZE 64 -#define TRB_BUFFER_SIZE (BLOCKRAM_SIZE * NUM_ENDPOINTS + NUM_ENDPOINTS) - -/* Bytes: */ -#define PAGE_SIZE 256 -static unsigned int NUM_PAGES = 0; - -#define BLOCK_SIZE (64 * 1024) -static unsigned int NUM_BLOCKS = 0; - -#define PAGE_BUFFER_SIZE (PAGE_SIZE * NUM_ENDPOINTS) - -static FILE *logFile = NULL; -static const char logFileName[256] = "trbflash.log"; - -static const char busy[4][5] = { - "|\b", - "/\b", - "-\b", - "\\\b" -}; - -static const unsigned int timeout = 10000; - -typedef enum { - FLASH_INVALID = 0, - - FLASH_RICH_ADCM_V1 = 1, - FLASH_RICH_ADCM_V2 = 2, - FLASH_RICH_ADCM_V3 = 3, - - FLASH_MDC_HUB_V2_FPGA1234 = 4, - FLASH_MDC_HUB_V2_FPGA5 = 5, - - FLASH_MDC_OEP_V2 = 6, - FLASH_MDC_OEP_V3 = 7, - - FLASH_SHOWER_ADDON_V2_FPGA1 = 8, - FLASH_SHOWER_ADDON_V2_FPGA2 = 9, - FLASH_SHOWER_ADDON_V2_FPGA3 = 10, - - FLASH_CTS_FPGA1 = 11, - FLASH_CTS_FPGA2 = 12 - -} FlashType; - -static const char FlashTypeStr[16][32] = - { - "INVALID", - "RICH_ADCM_V1", - "RICH_ADCM_V2", - "RICH_ADCM_V3", - "MDC_HUB_V2_FPGA1234", - "MDC_HUB_V2_FPGA5", - "MDC_OEP_V2", - "MDC_OEP_V3", - "SHOWER_ADDON_V2_FPGA1", - "SHOWER_ADDON_V2_FPGA2", - "SHOWER_ADDON_V2_FPGA3", - "CTS_FPGA1", - "CTS_FPGA2" - }; - -static FlashType flashType = FLASH_INVALID; -static uint32_t manId = 0; -static const char trbflash_version[] = "$Revision: 1.1 $"; - -static uint32_t mdcFlashSelect = 1; - -static int yesToAll = 0; - -static int skipFirmwareIdCheck = 0; - -static int skipHwIdCheck = 0; - -/* ------ Local Functions ----------------------------------------------- */ - -static void atexit0() -{ - unsigned int i; - - /* Close files */ - if ((logFile != NULL) && (logFile != stderr)) { - fclose(logFile); - } - - /* Free memory */ - free(pageBufferAddress); - for (i = 0; i < NUM_ENDPOINTS; i++) { - free(pageBuffer[i]); - } - free(pageBuffer); - free(trbBuffer); - free(imageBuffer); -} - -static int writeSetupRegister(uint16_t trb_address, uint8_t value) -{ - unsigned int ctr = 0; - int status = -1; - - /* Wait until NoMoreData is withdrawn */ - do { - if ((status = trb_register_write(trb_address, SetupReg, - (uint32_t)(value << 24))) == -1) { - fprintf(logFile, "Error > writeSetupRegister: TRBNet %s\n", - trb_strerrorf()); - return -1; - } - - /* Check timeout */ - if (ctr >= timeout) { - fprintf(logFile, "Error > writeSetupRegister: timeout\n"); - return -1; - } - ctr++; - } while (trb_term.status_channel != 0); - - if (trb_term.status_channel != 0) { - fprintf(logFile, - "Error > writeSetupRegister: invalid Status returned %s\n", - trb_strerrorf()); - return -1; - } - - return 0; -} - -static int readCtrlRegister(uint16_t trb_address, - uint32_t value[NUM_ENDPOINTS]) -{ - unsigned int ctr = 0; - int status = -1; - int c; - int i; - - /* Wait until NoMoreData is withdrawn */ - do { - if ((status = trb_register_read(trb_address, CtrlReg, - trbBuffer, TRB_BUFFER_SIZE)) == -1) { - fprintf(logFile, "Error > readCtrlRegister: TRBNet %s\n", - trb_strerrorf()); - return -1; - } - - /* Check timeout */ - if (ctr >= timeout) { - fprintf(logFile, "Error > readCtrlRegister: timeout\n"); - return -1; - } - ctr++; - } while (trb_term.status_channel != 0); - - if (status <= 0) { - fprintf(logFile, "Error > readCtrlRegister: invalid length returned\n"); - return -1; - } - - c = 0; - for (i = 1; i < status; i += 2) { - if (value != NULL) value[c] = trbBuffer[i]; - c++; - } - - return c; -} - -static int writeCtrlRegister(uint16_t trb_address, uint32_t value) -{ - unsigned int ctr = 0; - int status = -1; - - /* Wait until NoMoreData is withdrawn */ - do { - if ((status = trb_register_write(trb_address, CtrlReg, - value)) == -1) { - fprintf(logFile, "Error > writeCtrlRegister: TRBNet %s\n", - trb_strerrorf()); - return -1; - } - - /* Check timeout */ - if (ctr >= timeout) { - fprintf(logFile, "Error writeCtrlRegister, timeout\n"); - return -1; - } - ctr++; - } while (trb_term.status_channel != 0); - - return 0; -} - -static int sendCommand(uint16_t trb_address, uint32_t cmd, uint8_t max) -{ - if (writeSetupRegister(trb_address, max) == -1) { - return -1; - } - if (writeCtrlRegister(trb_address, cmd) == -1) { - return -1; - } - if (readCtrlRegister(trb_address, NULL) == -1) { - return -1; - } - - return 0; -} - -static int checkStatus(uint16_t trb_address) -{ - uint32_t trbcmd; - int status; - int ret; - int i; - - /* Read Status Register */ - trbcmd = 0x05 << 24; - if (sendCommand(trb_address, trbcmd, 0) == -1) { - return -1; - } - - if ((status = - trb_register_read(trb_address, BlockRam, trbBuffer, - TRB_BUFFER_SIZE)) == -1) { - fprintf(logFile, "Error > checkStatus: TRBNet %s\n", - trb_strerrorf()); - return -1; - } - - if (status <= 0) { - return -1; - } - - ret = 0; - for (i = 0; i < status; i += 2) { - if ((manId == 0x01461f) || - (manId == 0x00471f)) { - /* Check EPE Bit (ADCM and SHOWER) */ - if (((trbBuffer[i + 1] >> 5) & 0x01) == 1) { - fprintf(logFile, "Error > checkStatus: Erase or program error on " - "EndPoint 0x%04x\n", trbBuffer[i] & 0xffff); - return -1; /* Fatal Error */ - } - } - if ((trbBuffer[i + 1] & 0x01) == 1) { - ret = -2; /* One is busy */ - } - } - - return ret; -} - -static int writeStatusRegister(uint16_t trb_address, uint8_t value) -{ - int status; - - /* Enable writing */ - if (sendCommand(trb_address, 0x06 << 24, 0) == -1) { - fprintf(logFile, "Error > writeStatusRegister: write enable\n"); - return -1; - } - - if (trb_register_write(trb_address, BlockRam, (uint32_t)value) == -1) { - fprintf(logFile, "Error > writeStatusRegister: TRBNet %s\n", - trb_strerrorf()); - return -1; - } - if (sendCommand(trb_address, 0x01 << 24, 0) == -1) { - fprintf(logFile, "Error > writeStatusRegister: sendCommand\n"); - return -1; - } - - /* Wait for not busy and check status */ - while ((status = checkStatus(trb_address) == -2)) {} - - if (status != 0) { - fprintf(logFile, "Error > writeStatusRegister: invalid status\n"); - return -1; - } - - return 0; -} - -static int initTransfer(uint16_t trb_address) -{ - /* Find Endpoint(s) ManId and allocate needed memory */ - uint32_t trbcmd; - int status; - unsigned int counter = 0; - int i; - - /* Read HardwareIds from all Boards and validate (all must be the same) */ - if ((status = - trb_register_read(trb_address, HardwareId, trbBuffer, - TRB_BUFFER_SIZE)) == -1) { - fprintf(logFile, "Error > initTransfer, read HardwareIds: TRBNet %s\n", - trb_strerrorf()); - return -1; - } - - flashType = FLASH_INVALID; - for (i = 0; i < status; i += 2) { - FlashType fType = FLASH_INVALID; - - uint16_t hardwareId = (uint16_t)((trbBuffer[i + 1] >> 16) & 0xffff); - switch (hardwareId) { - - /* Rich */ - case 0x3100: - fType = FLASH_RICH_ADCM_V1; - break; - case 0x3200: - fType = FLASH_RICH_ADCM_V2; - break; - case 0x3300: - case 0x0002: - fType = FLASH_RICH_ADCM_V3; - break; - - /* MDC HUB */ - case 0x1210: - fType = FLASH_MDC_HUB_V2_FPGA1234; - break; - case 0x1250: - fType = FLASH_MDC_HUB_V2_FPGA5; - break; - - /* MDC OEP */ - case 0x2200: - fType = FLASH_MDC_OEP_V2; - break; - case 0x2300: - case 0x1234: - fType = FLASH_MDC_OEP_V3; - break; - - /* SHOWER */ - case 0x4210: - fType = FLASH_SHOWER_ADDON_V2_FPGA1; - break; - case 0x4220: - fType = FLASH_SHOWER_ADDON_V2_FPGA2; - break; - case 0x4230: - fType = FLASH_SHOWER_ADDON_V2_FPGA3; - break; - - /* CTS */ - case 5100: - fType = FLASH_CTS_FPGA1; - break; - case 5200: - fType = FLASH_CTS_FPGA1; - break; - - default: - /* Error: Unknown FlashType */ - fprintf(logFile, "Error > initTransfer: " - "Unsupported HardwareId 0x%04x on EndPoint 0x%04x\n", - hardwareId, trbBuffer[i] & 0xffff); - return -1; - } - - if (flashType == FLASH_INVALID) { - flashType = fType; - continue; - } - if (fType != flashType) { - fprintf(logFile, "Error > initTransfer: " - "HardwareId 0x%04x differs on EndPoint 0x%04x\n", - hardwareId, trbBuffer[i] & 0xffff); - return -1; - } - } - - /* Read ManIds from all Boards and validate (all must be the same as well) */ - trbcmd = 0x9f << 24; - if (sendCommand(trb_address, trbcmd, 2) == -1) { - return -1; - } - - if ((status = - trb_register_read(trb_address, BlockRam, trbBuffer, - TRB_BUFFER_SIZE)) == -1) { - fprintf(logFile, "Error > initTransfer, read ManIds: TRBNet %s\n", - trb_strerrorf()); - return -1; - } - - if (status <= 0) { - return -1; - } - - manId = 0; - counter = 0; - for (i = 0; i < status; i += 2,counter++) { - uint32_t tmpManId = (trbBuffer[i + 1] & 0x00ffffff); - - if (!((tmpManId == 0x01461f) || - (tmpManId == 0x00471f) || - (tmpManId == 0x1520c2))) { - fprintf(logFile, "Error > initTransfer: " - "Unsupported ManId(s) 0x%04x on EndPoint 0x%04x\n", - tmpManId, trbBuffer[i] & 0xffff); - return -1; - } - if (manId == 0) { - manId = tmpManId; - continue; - } - if (tmpManId != manId) { - fprintf(logFile, "Error > initTransfer: ManId 0x%04x differs" - "on EndPoint 0x%04x\n", - manId, trbBuffer[i] & 0xffff); - return -1; - } - } - - /* Set NUM_PAGES */ - switch (manId) { - - case 0x01461f: - NUM_PAGES = 8192; - break; - - case 0x00471f: - NUM_PAGES = 16384; - break; - - case 0x1520c2: - NUM_PAGES = 8192; - break; - - default: - abort(); - } - - - NUM_BLOCKS = (NUM_PAGES * PAGE_SIZE) / BLOCK_SIZE; - - /* Buffer holding the entire rom-image */ - imageBuffer = - (uint8_t*)malloc(sizeof(uint8_t) * (PAGE_SIZE * (NUM_PAGES + 2))); - if (imageBuffer == NULL) { - abort(); - } - - fprintf(stderr, "Found %d Endpoint(s) of type %s\n", - counter, FlashTypeStr[flashType]); - - return 0; -} - -static int readPage(uint16_t trb_address, uint32_t pageNumber, - uint32_t numBytes) -{ - uint32_t* temp = NULL; - uint32_t* end = NULL; - unsigned int endPoint; - uint16_t size = 0; - uint32_t trbcmd; - int status = -1; - - if (pageNumber >= NUM_PAGES) return -1; - if ((numBytes > PAGE_SIZE) || (numBytes == 0)) return -1; - - trbcmd = (0x03 << 24) | ((pageNumber * PAGE_SIZE) & 0xffffff); - if (sendCommand(trb_address, trbcmd, numBytes - 1) == -1) { - return -1; - } - - size = (numBytes / 4) + (numBytes % 4 != 0 ? 1 : 0); - if ((status = trb_register_read_mem(trb_address, BlockRam, 0, size, - trbBuffer, TRB_BUFFER_SIZE)) - == -1) { - fprintf(logFile, "Error > readPage: TRBNet %s\n", - trb_strerrorf()); - return -1; - } - - if (status <= 0) { - return -1; - } - - /* Copy trbBuffer to pageBuffer and check content */ - endPoint = 0; - temp = trbBuffer; - end = trbBuffer + status; - while (temp < trbBuffer + status) { - unsigned int len = (*temp >> 16) & 0xffff; - unsigned int address = *temp & 0xffff; - unsigned int c; - - if (len != size) { - fprintf(logFile, - "Error > readPage: Invalid len %d returned by endpoint 0x%04x\n", - len, address); - } - - /* read one page */ - pageBufferAddress[endPoint] = address; - for (c = 0; c < (len * 4); c++) { - if ((c % 4) == 0) { - temp++; - } - pageBuffer[endPoint][c] = (*temp >> ((c % 4) * 8)) & 0xff; - } - temp++; - endPoint++; - } - - return endPoint; -} - - -static int writePage(uint16_t trb_address, uint32_t pageNumber, - const uint8_t* pageBuffer, uint32_t numBytes) -{ - uint32_t* temp = NULL; - uint16_t size = 0; - uint32_t trbcmd; - unsigned int c; - int status = -1; - - if (pageNumber >= NUM_PAGES) return -1; - if ((numBytes > PAGE_SIZE) || (numBytes == 0)) return -1; - - /* Copy pageBuffer to trbBuffer */ - temp = trbBuffer - 1; - for (c = 0; c < numBytes; c++) { - if ((c % 4) == 0) { - temp++; - *temp = 0; - } - *temp |= (pageBuffer[c] << ((c % 4) * 8)); - } - - /* Transfer trbBuffer */ - size = temp - trbBuffer + 1; - if ((status = trb_register_write_mem(trb_address, BlockRam, 0, - trbBuffer, size)) - == -1) { - fprintf(logFile, "Error > writePage: TRBNet %s\n", - trb_strerrorf()); - return -1; - } - - /* Enable writing */ - if (sendCommand(trb_address, 0x06 << 24, 0) == -1) { - fprintf(logFile, "Error > writePage: write enable\n"); - return -1; - } - - /* Write page */ - trbcmd = (0x02 << 24) | ((pageNumber * PAGE_SIZE) & 0xffffff); - if (sendCommand(trb_address, trbcmd, numBytes - 1) == -1) { - fprintf(logFile, "Error > writePage: write page\n"); - return -1; - } - - /* Wait for not busy and check status */ - while ((status = checkStatus(trb_address) == -2)) { - /* fprintf(logFile, "Wait pageProgram ...\n"); */ - } - - if (status != 0) { - fprintf(logFile, "Error > pageWrite: invalid status\n"); - return -1; - } - - return 0; -} - -typedef enum { - PMODE_PROGRAM, - PMODE_PROGRAM_FULL, - PMODE_VERIFY -} PMode; - -static int programImageBuffer(uint16_t trb_address, unsigned int size, - PMode mode) -{ - unsigned int block; - int status; - int errorCtr = 0; - unsigned int i; - unsigned int page; - int bytesWritten = size; - int tmp; - - if ((mode != PMODE_VERIFY) && (yesToAll == 0)) { - /* Be nice and ask before start flashing the roms */ - char c; - if ((flashType == FLASH_MDC_OEP_V2) || - (flashType == FLASH_MDC_OEP_V3)) { - if (mdcFlashSelect == 0) { - fprintf(stdout, "You decided to reprogram the FlashRom(s) #0 of " - "MDC_OEP, the so called 'GoldenImage'. " - "Do you really know what you're about to do [N,y]: "); - } - } else { - fprintf(stdout, "You decided to reprogram the FlashRom(s) of " - "%s, are you sure [N,y]: ", FlashTypeStr[flashType]); - } - - c = getc(stdin); - if (!((c == 'Y') || (c == 'y'))) { - fprintf(stderr, "\nAborting on user request\n"); - return -1; - } - } - - if (mode != PMODE_VERIFY) { - fprintf(stderr, "Programming Endpoint(s) @ Address 0x%04x\n", - trb_address); - fprintf(stderr, "Symbols:\n" - " E: Erasing\n" - " P: Programming\n" - " V: Verifying\n" - " X: Failed (see logfile 'trbflash.log' for details)\n" - " @: Success\n" - " .: Skipped\n\n"); - } else { - fprintf(stderr, "Verifying Endpoint(s) @ Address 0x%04x\n", - trb_address); - fprintf(stderr, "Symbols:\n" - " V: Verifying\n" - " X: Failed (see logfile 'trbflash.log' for details)\n" - " @: Success\n" - " .: Skipped\n\n"); - } - fprintf(stderr, "Block: 0 1 2 3 4 5 6 7 8 9 A B C D E F"); - - errorCtr = 0; - - if ((mode != PMODE_VERIFY) && (manId == 0x1520c2)) { - /* Unprotect all Sectors */ - if (writeStatusRegister(trb_address, 0) == -1) { - fprintf(stderr, - "\nError > program: unprotect all sectors, aborting\n"); - return -1; - } - } - - for (block = 0; (block < NUM_BLOCKS); block++) { - int error = 0; - int writeInfoPage = (block == NUM_BLOCKS - 1) && (mode == PMODE_PROGRAM) - ? 1 : 0; - - if (block % 16 == 0) { - fprintf(stderr, "\n%x ", block / 16); - } - fprintf(stderr, ".\b"); - - if (!(bytesWritten > 0) && (writeInfoPage == 0)) { - fprintf(stderr, ". "); - continue; - } - - if ((mode != PMODE_VERIFY) && - ((bytesWritten > 0) || (writeInfoPage == 1))) { - if ((manId == 0x01461f) || - (manId == 0x00471f)) { - /* Enable writing */ - if (sendCommand(trb_address, 0x06 << 24, 0) == -1) { - fprintf(stderr, "\nError > program: write enable, aborting\n"); - return -1; - } - - /* Unprotect sector */ - if (sendCommand(trb_address, 0x39 << 24 | (BLOCK_SIZE * block), 3) - == -1) { - fprintf(stderr, - "\nError > program: unprotect sector #%d, aborting\n", - block); - return -1; - } - } - - /* Enable writing */ - if (sendCommand(trb_address, 0x06 << 24, 0) == -1) { - fprintf(stderr, "\nError > program: write enable, aborting\n"); - return -1; - } - - /* Erase block */ - fprintf(stderr, "E\b"); - if (sendCommand(trb_address, 0xd8 << 24 | (BLOCK_SIZE * block), 3) - == -1) { - fprintf(stderr, "\nError > program: erase block #%d, aborting\n", - block); - return -1; - } - - /* Wait for not busy and check status */ - while ((status = checkStatus(trb_address) == -2)) {} - - if (status != 0) { - fprintf(stderr, "\nError > program: invalid status, aborting\n"); - return -1; - } - - /* Now write pages */ - fprintf(stderr, "P\b"); - tmp = bytesWritten; - for (i = 0, page = (block * BLOCK_SIZE) / PAGE_SIZE; - (i < (BLOCK_SIZE / PAGE_SIZE)) && (tmp > 0); - i++, page++) { - int bytes = tmp < PAGE_SIZE ? tmp : PAGE_SIZE; - tmp -= bytes; - status = writePage(trb_address, page, - imageBuffer + page * PAGE_SIZE, bytes); - if (status == -1) { - fprintf(stderr, - "\nError > program: pageProgram page #%d, aborting\n", - page); - return -1; - } - } - - if (writeInfoPage == 1) { - /* Write Info-Page */ - status = writePage(trb_address, (NUM_PAGES - 1), - imageBuffer + (NUM_PAGES - 1) * - PAGE_SIZE, PAGE_SIZE); - if (status == -1) { - fprintf(stderr, - "\nError > program: pageProgram page #%d, aborting\n", - page); - return -1; - } - } - if ((manId == 0x01461f) || - (manId == 0x00471f)) { - /* Enable writing */ - if (sendCommand(trb_address, 0x06 << 24, 0x00) == -1) { - fprintf(stderr, "\nError > program: write enable, aborting\n"); - return -1; - } - - /* Protect sector */ - if (sendCommand(trb_address, 0x36 << 24 | (BLOCK_SIZE * block), 3) - == -1) { - fprintf(stderr, - "\nError > program: unprotect sector #%d, aborting\n", - block); - return -1; - } - } - } - - /* Verify pages */ - fprintf(stderr, "V\b"); - tmp = bytesWritten; - for (i = 0, page = (block * BLOCK_SIZE) / PAGE_SIZE; - (i < (BLOCK_SIZE / PAGE_SIZE)) && (tmp > 0); - i++, page++) { - unsigned int endPoint; - unsigned int c; - int bytes = tmp < PAGE_SIZE ? tmp : PAGE_SIZE; - tmp -= bytes; - - if ((status = readPage(trb_address, page, bytes)) == -1) { - fprintf(stderr, "\nError > program: reading Page #%d, aborting\n", - page); - return -1; - } - - for (endPoint = 0; endPoint < status; endPoint++) { - for (c = 0; c < bytes; c++) { - if (pageBuffer[endPoint][c] != imageBuffer[page * PAGE_SIZE + c]) { - error = -1; - errorCtr++; - fprintf(logFile, - "verify failed page #%d, byte #%d " - "(exp: 0x%02x rec: 0x%02x), EndPoint: 0x%04x\n", - page, c, - imageBuffer[page * PAGE_SIZE + c], - pageBuffer[endPoint][c], - pageBufferAddress[endPoint]); - } - } - } - - bytesWritten = tmp; - } - - if (writeInfoPage == 1) { - /* Verify Info-Page */ - unsigned int endPoint; - unsigned int c; - int bytes = tmp < PAGE_SIZE ? tmp : PAGE_SIZE; - tmp -= bytes; - - if ((status = readPage(trb_address, NUM_PAGES - 1, PAGE_SIZE)) == -1) { - fprintf(stderr, "\nError > program: reading InfoPage, aborting\n"); - return -1; - } - - for (endPoint = 0; endPoint < status; endPoint++) { - for (c = 0; c < PAGE_SIZE; c++) { - if (pageBuffer[endPoint][c] != - imageBuffer[(NUM_PAGES - 1) * PAGE_SIZE + c]) { - error = -1; - errorCtr++; - fprintf(logFile, - "verify failed InfoPage, byte #%d " - "(exp: 0x%02x rec: 0x%02x), EndPoint: 0x%04x\n", - c, - imageBuffer[(NUM_PAGES - 1) * PAGE_SIZE + c], - pageBuffer[endPoint][c], - pageBufferAddress[endPoint]); - } - } - } - } - - if (error == 0) { - fprintf(stderr, "@ "); - } else { - fprintf(stderr, "X "); - } - } - - if ((mode != PMODE_VERIFY) && (manId == 0x1520c2)) { - /* Protect all Sectors i.e. write 0x3c to statusRegister */ - if (writeStatusRegister(trb_address, 0x00) == -1) { - fprintf(stderr, "\nError > program: protect all sectors, aborting\n"); - return -1; - } - } - - if (errorCtr == 0) { - fprintf(stderr, "\n\nSuccess\n\n"); - } else { - fprintf(stderr, - "\n\n%d Errors have occured, see logFile %s for details\n\n", - errorCtr, logFileName); - } - - return errorCtr == 0 ? 0 : -1; -} - - -static int readImageFile(const char *imageFileName) -{ - FILE *imageFile = NULL; - int imageSize; - unsigned int i; - - /* Cleanup Buffer */ - for (i = 0; i < (PAGE_SIZE * (NUM_PAGES + 2)); i++) imageBuffer[i] = 0; - - /* Read in image */ - imageFile = fopen(imageFileName, "r"); - if (imageFile == NULL) { - fprintf(logFile, - "Error > readImageFile: Could not open ImageFile %s: %s\n", - imageFileName, strerror(errno)); - return -1; - } - - imageSize = 0; - do { - imageSize += fread((void*)(imageBuffer + imageSize), sizeof(uint8_t), - PAGE_SIZE, imageFile); - } while ((feof(imageFile) == 0) && (imageSize <= PAGE_SIZE * NUM_PAGES)); - - fclose(imageFile); - - if (imageSize > (PAGE_SIZE * NUM_PAGES)) { - fprintf(logFile, - "Error > readImageFile: Imagefile '%s' is too large (%d bytes)\n", - imageFileName, imageSize); - return -1; - } - - return imageSize; -} - -static int prepareImageBuffer() -{ - char* strId = "INVALID"; - int found; - unsigned int end; - unsigned int i; - - /* Verify imageFile Id */ - switch (flashType) { - - case FLASH_RICH_ADCM_V1: - strId = "adcmv1"; - break; - - case FLASH_RICH_ADCM_V2: - strId = "adcmv2"; - break; - - case FLASH_RICH_ADCM_V3: - strId = "adcmv3"; - break; - - case FLASH_MDC_HUB_V2_FPGA1234: - strId = "mdchub_fpga1234"; - break; - case FLASH_MDC_HUB_V2_FPGA5: - strId = "mdchub_fpga5"; - break; - - case FLASH_MDC_OEP_V2: - case FLASH_MDC_OEP_V3: - strId = "mdc_oepb"; - break; - - case FLASH_SHOWER_ADDON_V2_FPGA1: - strId = "shower_fpga1"; - break; - case FLASH_SHOWER_ADDON_V2_FPGA2: - strId = "shower_fpga2"; - break; - case FLASH_SHOWER_ADDON_V2_FPGA3: - strId = "shower_fpga3"; - break; - - case FLASH_CTS_FPGA1: - strId = "cts_fpga1"; - case FLASH_CTS_FPGA2: - strId = "cts_fpga2"; - break; - - default: - abort(); - break; - } - - /* Verify imageFile Id */ - found = 0; - for (i = 0; i < 2 * PAGE_SIZE; i++) { - if (memcmp(imageBuffer + i, strId, strlen(strId)) == 0) { - found = 1; - break; - } - } - - if ((skipFirmwareIdCheck == 0) && (found == 0)) { - fprintf(logFile, "Error > prepareImageBuffer: " - "invalid Firmware-Id of Image-File, should be: %s\n", strId); - return -1; - } - - /* Overwrite Header with 0xff */ - end = 0; - for (i = 0; i < 2 * PAGE_SIZE; i++) { - if (strncmp((char*)(imageBuffer + i), "Bitstream CRC: 0x", 17) == 0) { - end = i + 22; - break; - } - } - if (end == 0) { - fprintf(logFile, "Error > prepareImageBuffer: invalid Firmware, " - "CRC not found\n"); - } - for (i = 0; i < end; i++) { - imageBuffer[i] = 0xff; - } - - return 0; -} - -static int createInfoPage(const char* fileName, const char* userString) -{ - char* buffer = (char*)(imageBuffer + (NUM_PAGES - 1) * PAGE_SIZE); - struct stat statBuf; - - unsigned int i; - for (i = 0; i < PAGE_SIZE; i++) { - buffer[i] = '\0'; - } - - if (stat(fileName, &statBuf) == -1) { - fprintf(logFile, "Error > prepareInfoPage: statCall failed: %s\n", - strerror(errno)); - return -1; - } - snprintf(buffer + 0, 63, "NAME: %s", basename((char*)fileName)); - snprintf(buffer + 64, 31, "DATE: %s", ctime(&statBuf.st_mtime)); - snprintf(buffer + 96, 159, "USER: %s", userString); - - fprintf(stderr, "%s\n", buffer); - fprintf(stderr, "%s\n", buffer + 64); - fprintf(stderr, "%s\n", buffer + 96); - - return 0; -} - -static int openLog() { - time_t datet; - char date[128]; - - /* Open LogFile */ - logFile = fopen(logFileName, "w+"); - if (logFile == NULL) { - fprintf(stderr, "Could not open logFile %s: %s\n", - logFileName, strerror(errno)); - return -1; - } - - datet = time(NULL); - ctime_r(&datet, date); - date[strlen(date) - 1] = '\0'; - fprintf(logFile, - "\n\n------------ %s ----------------------------------\n\n", date); - - return 0; -} - -static int selectMdcFlashRom(uint16_t trb_address, uint8_t number) { - /* Select MDC-FlashRom */ - if (trb_register_write(trb_address, MDCFlashRomSelect, - (uint32_t)number) == -1) { - fprintf(logFile, "\nError > selcetMdcFlashRom: TRBNet %s\n", - trb_strerrorf()); - return -1; - } - - return 0; -} - -/* ------ MAIN ---------------------------------------------------------- */ - -void usage(const char *progName) -{ - printf("Usage: %s [-s str] [-g] [-y] [-f] [-h] [-V] \n", - progName); - printf("Options:\n"); - printf(" -s set user-string of info page (default: empty string)\n"); - printf(" -g select 'FlashRom #0 (the 'Golden Image') " - "(MDC only, default = no)\n"); - printf(" -y assume yes to all questions which might will be asked\n"); - printf(" -f do not validate FirmwareId of ImageFile (DON'T USE, for " - "EXPERTS ONLY)\n"); - /* printf(" -F do not crosscheck HardwareIDs of Endpoints " - "(DON'T USE, for EXPERTS ONLY)\n"); */ - printf(" -h give this help\n"); - printf(" -V Version number\n"); - printf("\nCommands:\n"); - printf(" program -> " - "program bit-file to flash-memory\n"); - printf(" verify -> " - "compare bit-file with flash-memory\n"); - printf(" backup -> " - "write entire flash-memory to raw-file\n"); - printf(" restore -> " - "write backuped raw-file to flash-memory\n"); - printf(" info -> " - "dump content of info-page to stdout\n"); - printf(" dumppage pagenumber -> " - "dump one flash-memory page to stdout\n"); -} - -int main(int argc, char ** argv) -{ - unsigned int i; - char userInfoStr[256] = ""; - - trb_debug = 0; - logFile = stderr; - mdcFlashSelect = 1; - yesToAll = 0; - skipFirmwareIdCheck = 0; - skipHwIdCheck = 0; - - /* Parse Arguments */ - while ((i = getopt(argc, argv, "+hd:s:gyVf")) != -1) { - switch (i) { - case '?': - usage(basename(argv[0])); - exit(EXIT_FAILURE); - case 'h': - usage(basename(argv[0])); - exit(EXIT_SUCCESS); - case 's': - strncpy(userInfoStr, optarg, 256); - break; - case 'g': - mdcFlashSelect = 0; - break; - case 'd': - trb_debug = strtoul(optarg, NULL, 0); - break; - case 'y': - yesToAll = 1; - break; - case 'f': - skipFirmwareIdCheck = 1; - break; - case 'F': - skipHwIdCheck = 2; - break; - case 'V': - printf("%s %s, using libtrbnet %s\n", - basename(argv[0]), trbflash_version, trbnet_version); - exit(EXIT_SUCCESS); - break; - default: - usage(basename(argv[0])); - exit(EXIT_FAILURE); - break; - } - } - - if (argc - optind < 1) { - usage(basename(argv[0])); - exit(EXIT_FAILURE); - } - - if (init_ports() == -1) { - trb_error("Init_Ports"); - exit(EXIT_FAILURE); - } - - atexit(atexit0); - - /* Allocate memory for buffers */ - - /* Buffer holding the TRBNet packages, e.g. use by trb_read_register */ - trbBuffer = (uint32_t*)malloc(sizeof(uint32_t) * TRB_BUFFER_SIZE); - if (trbBuffer == NULL) { - abort(); - } - - /* Buffer holding the pages of the endpoints */ - pageBuffer = (uint8_t**)malloc(sizeof(uint8_t*) * NUM_ENDPOINTS); - if (pageBuffer == NULL) { - abort(); - } - for (i = 0; i < NUM_ENDPOINTS; i++) { - pageBuffer[i] = (uint8_t*)malloc(sizeof(uint8_t) * PAGE_SIZE); - if (pageBuffer[i] == NULL) abort(); - } - - /* Buffer holding the corresponding TRBAddresses */ - pageBufferAddress = (uint16_t*)malloc(sizeof(uint16_t) * NUM_ENDPOINTS); - if (pageBufferAddress == NULL) { - abort(); - } - - /* Execute command */ - if (strcmp(argv[optind], "program") == 0) { - - /*********************************************************/ - /* Program Image */ - /*********************************************************/ - - uint16_t trb_address; - char* imageFileName = NULL; - int size; - - if (argc - optind != 3) { - usage(basename(argv[0])); - exit(EXIT_FAILURE); - } - - trb_address = strtoul(argv[optind + 1], NULL, 0); - imageFileName = argv[optind + 2]; - - /* Check for correct ManId */ - if (initTransfer(trb_address) == -1) { - fprintf(stderr, "InitTransfer failed, aborting\n"); - exit(EXIT_FAILURE); - } - - /* Read imageFile to imageBuffer */ - if ((size = readImageFile(imageFileName)) == -1) { - exit(EXIT_FAILURE); - } - if (size > (PAGE_SIZE * (NUM_PAGES - 1))) { - fprintf(stderr, "ImageFile to large (%d bytes, maximum is %d bytes)\n", - size, PAGE_SIZE * (NUM_PAGES - 1)); - exit(EXIT_FAILURE); - } - - /* Validate Id and prepare imageBuffer */ - if (prepareImageBuffer() == -1) { - exit(EXIT_FAILURE); - } - - /* Create InfoPage */ - createInfoPage(imageFileName, userInfoStr); - - /* In case of MDC: select FlashRom */ - if ((flashType == FLASH_MDC_OEP_V2) || - (flashType == FLASH_MDC_OEP_V3)) { - if (selectMdcFlashRom(trb_address, mdcFlashSelect) == -1) { - exit(EXIT_FAILURE); - } - } - - /* Open LogFile */ - if (openLog() == -1) { - exit(EXIT_FAILURE); - } - - fprintf(stderr, "Start programming ImageFile '%s'\n", - imageFileName); - - if (programImageBuffer(trb_address, size, PMODE_PROGRAM) == -1) { - exit(EXIT_FAILURE); - } - - } else if (strcmp(argv[optind], "verify") == 0) { - - /*********************************************************/ - /* Verify Image */ - /*********************************************************/ - - uint16_t trb_address; - char* imageFileName = NULL; - int size; - - if (argc - optind != 3) { - usage(basename(argv[0])); - exit(EXIT_FAILURE); - } - - trb_address = strtoul(argv[optind + 1], NULL, 0); - imageFileName = argv[optind + 2]; - - /* Check for correct ManId */ - if (initTransfer(trb_address) == -1) { - fprintf(stderr, "InitTransfer failed, aborting\n"); - exit(EXIT_FAILURE); - } - - /* Read imageFile to imageBuffer */ - if ((size = readImageFile(imageFileName)) == -1) { - exit(EXIT_FAILURE); - } - if (size > (PAGE_SIZE * (NUM_PAGES - 1))) { - fprintf(stderr, "ImageFile to large (%d bytes, maximum is %d bytes)\n", - size, PAGE_SIZE * (NUM_PAGES - 1)); - exit(EXIT_FAILURE); - } - - /* Validate Id and prepare imageBuffer */ - if (prepareImageBuffer() == -1) { - exit(EXIT_FAILURE); - } - - /* In case of MDC: select FlashRom */ - if ((flashType == FLASH_MDC_OEP_V2) || - (flashType == FLASH_MDC_OEP_V3)) { - if (selectMdcFlashRom(trb_address, mdcFlashSelect) == -1) { - exit(EXIT_FAILURE); - } - } - - /* Open LogFile */ - if (openLog() == -1) { - exit(EXIT_FAILURE); - } - - fprintf(stderr, "Start verifying ImageFile '%s'\n", - imageFileName); - - if (programImageBuffer(trb_address, size, PMODE_VERIFY) == -1) { - exit(EXIT_FAILURE); - } - - } else if (strcmp(argv[optind], "backup") == 0) { - - /*********************************************************/ - /* Backup Image */ - /*********************************************************/ - - uint16_t trb_address; - char* imageFileName; - FILE* imageFile = NULL; - unsigned int page; - - if (argc - optind != 3) { - usage(basename(argv[0])); - exit(EXIT_FAILURE); - } - trb_address = strtoul(argv[optind + 1], NULL, 0); - imageFileName = argv[optind + 2]; - - if (trb_address >= 0xff00) { - fprintf(stderr, - "Broadcast addresses are not supported by this command\n"); - exit(EXIT_FAILURE); - } - - /* In case of MDC: select FlashRom */ - if ((flashType == FLASH_MDC_OEP_V2) || - (flashType == FLASH_MDC_OEP_V3)) { - if (selectMdcFlashRom(trb_address, mdcFlashSelect) == -1) { - exit(EXIT_FAILURE); - } - } - - if (initTransfer(trb_address) == -1) { - fprintf(stderr, "InitTransfer failed, aborting\n"); - exit(EXIT_FAILURE); - } - - if (checkStatus(trb_address) < 0) { - exit(EXIT_FAILURE); - } - - /* Write Pages to file */ - fprintf(stderr, - "Dumping entire flashrom of EndPoint 0x%04x to file '%s' ", - trb_address, imageFileName); - - imageFile = fopen(imageFileName, "w+"); - if (imageFile == NULL) { - fprintf(stderr, "Could not open Imagefile %s: %s\n", - imageFileName, strerror(errno)); - exit(EXIT_FAILURE); - } - - for (page = 0; page < NUM_PAGES; page++) { - if (readPage(trb_address, page, PAGE_SIZE) == -1) { - fprintf(stderr, "\nError reading Page #%d, aborting..\n", page); - fclose(imageFile); - exit(EXIT_FAILURE); - } - if (fwrite((void*)pageBuffer[0], PAGE_SIZE, 1, imageFile) != 1) { - fprintf(stderr, - "\nError writing Page #%d to file, aborting..\n", page); - fclose(imageFile); - exit(EXIT_FAILURE); - } - - fprintf(stderr, "%s", busy[page % 4]); - } - fprintf(stderr, " \nDone\n"); - - fclose(imageFile); - - } else if (strcmp(argv[optind], "restore") == 0) { - - /*********************************************************/ - /* Restore Image */ - /*********************************************************/ - - uint16_t trb_address; - char* imageFileName; - int size; - - if (argc - optind != 3) { - usage(basename(argv[0])); - exit(EXIT_FAILURE); - } - trb_address = strtoul(argv[optind + 1], NULL, 0); - imageFileName = argv[optind + 2]; - - /* Check for correct ManId */ - if (initTransfer(trb_address) == -1) { - fprintf(stderr, "InitTransfer failed, aborting\n"); - exit(EXIT_FAILURE); - } - - /* Read imageFile to imageBuffer */ - if ((size = readImageFile(imageFileName)) == -1) { - exit(EXIT_FAILURE); - } - - if (size != PAGE_SIZE * NUM_PAGES) { - fprintf(stderr, "Invalid ImageFile %s, aborting\n", imageFileName); - exit(EXIT_FAILURE); - } - - /* Check ImageBuffer ??? */ - - /* In case of MDC: select FlashRom */ - if ((flashType == FLASH_MDC_OEP_V2) || - (flashType == FLASH_MDC_OEP_V3)) { - if (selectMdcFlashRom(trb_address, mdcFlashSelect) == -1) { - exit(EXIT_FAILURE); - } - } - - /* Open LogFile */ - if (openLog() == -1) { - exit(EXIT_FAILURE); - } - - fprintf(stderr, "Start restoring ImageFile '%s'\n", - imageFileName); - - if (programImageBuffer(trb_address, size, PMODE_PROGRAM_FULL) == -1) { - exit(EXIT_FAILURE); - } - - } else if (strcmp(argv[optind], "info") == 0) { - - /*********************************************************/ - /* Dump Info-Page */ - /*********************************************************/ - - uint16_t trb_address; - unsigned int i; - int status; - - if (argc - optind != 2) { - usage(basename(argv[0])); - exit(EXIT_FAILURE); - } - trb_address = strtoul(argv[optind + 1], NULL, 0); - - /* Check for correct ManId */ - if (initTransfer(trb_address) == -1) { - fprintf(stderr, "InitTransfer failed, aborting\n"); - exit(EXIT_FAILURE); - } - - /* In case of MDC: select FlashRom */ - if ((flashType == FLASH_MDC_OEP_V2) || - (flashType == FLASH_MDC_OEP_V3)) { - if (selectMdcFlashRom(trb_address, mdcFlashSelect) == -1) { - exit(EXIT_FAILURE); - } - } - - if ((status = readPage(trb_address, NUM_PAGES - 1, PAGE_SIZE)) == -1) { - fprintf(stderr, "Error reading InfoPage, aborting\n"); - exit(EXIT_FAILURE); - } - - for (i = 0; i < status; i++) { - pageBuffer[i][63] = '\0'; - pageBuffer[i][95] = '\0'; - pageBuffer[i][255] = '\0'; - fprintf(stdout, "\nEndPoint: 0x%04x InfoPage\n", - pageBufferAddress[i]); - if ( - (strncmp((char*)(pageBuffer[i] + 0), "NAME:", 5) != 0) || - (strncmp((char*)(pageBuffer[i] + 64), "DATE:", 5) != 0) || - (strncmp((char*)(pageBuffer[i] + 96), "USER:", 5) != 0) - ) { - fprintf(stdout, "INVALID CONTENT\n"); - continue; - } - - fprintf(stdout, "%s\n", pageBuffer[i]); - fprintf(stdout, "%s\n", pageBuffer[i] + 64); - fprintf(stdout, "%s\n", pageBuffer[i] + 96); - } - fprintf(stdout, "\n"); - - } else if (strcmp(argv[optind], "dumppage") == 0) { - - /*********************************************************/ - /* Read and dump a page */ - /*********************************************************/ - - uint16_t trb_address; - uint32_t pageNumber; - unsigned int i; - int status; - - if (argc - optind != 3) { - usage(basename(argv[0])); - exit(EXIT_FAILURE); - } - trb_address = strtoul(argv[optind + 1], NULL, 0); - pageNumber = strtoul(argv[optind + 2], NULL, 0); - - /* Check for correct ManId */ - if (initTransfer(trb_address) == -1) { - fprintf(stderr, "InitTransfer failed, aborting\n"); - exit(EXIT_FAILURE); - } - - /* In case of MDC: select FlashRom */ - if ((flashType == FLASH_MDC_OEP_V2) || - (flashType == FLASH_MDC_OEP_V3)) { - if (selectMdcFlashRom(trb_address, mdcFlashSelect) == -1) { - exit(EXIT_FAILURE); - } - } - - if ((status = readPage(trb_address, pageNumber, PAGE_SIZE)) == -1) { - fprintf(stderr, "Error reading Page, aborting\n"); - exit(EXIT_FAILURE); - } - - for (i = 0; i < status; i++) { - char text[32] = ""; - char cc[2] = " \0"; - int c; - fprintf(stdout, "\nEndPoint: 0x%04x Page #%d\n", - pageBufferAddress[i], pageNumber); - for (c = 0; c < PAGE_SIZE; c++) { - if ((c % 16) == 0) { - text[0] = '\0'; - fprintf(stdout, "0x%02x ", c); - } - fprintf(stdout, "%02x ", pageBuffer[i][c]); - cc[0] = isprint((char)pageBuffer[i][c]) == 0 - ? '.' : (char)pageBuffer[i][c]; - strncat(text, cc, 32); - if ((c % 16) == 15) { - fprintf(stdout, " %s\n", text); - } - } - } - fprintf(stdout, "\n"); - - } else { - - /* Invalid command */ - usage(basename(argv[0])); - exit(EXIT_FAILURE); - } - - exit(EXIT_SUCCESS); -} diff --git a/trbnetd/trbrichcmd.c b/trbnetd/trbrichcmd.c deleted file mode 100644 index 2fa9628..0000000 --- a/trbnetd/trbrichcmd.c +++ /dev/null @@ -1,280 +0,0 @@ -#define _GNU_SOURCE -#include -#include -#include - -#include -#include -#include -#include -#include - -#define BUFFER_SIZE (10 * (6 * 16 * 5 * 2)) - -#define TEMP_RESOLUTION 0.0625 - -static const char trbrichcmd_version[] = "$Revision: 1.1 $"; - -static const uint16_t adcm_uid_register = 0xc000; - -void usage(const char *progName) -{ - printf("Usage: %s [-h] [-V] \n", progName); - printf("Options:\n"); - printf(" -h give this help\n"); - printf(" -V Version number\n"); - printf("\nCommands:\n"); - printf(" uid " - "-> read uids from APV-FrondEnds\n" - " (default-Address:0xfffb)\n"); - printf(" temp " - "-> read temperatures from APV-FrondEnds\n" - " (default-Address:0xfffb)\n"); - printf(" adcsnoop " - "-> ADC Sniffer Tool\n"); -} - -/* ------ MAIN ---------------------------------------------------------- */ - -int main(int argc, char** argv) -{ - int i; - - trb_debug = 0; - - /* Parse Arguments */ - while ((i = getopt(argc, argv, "+hd:V")) != -1) { - switch (i) { - case '?': - usage(basename(argv[0])); - exit(EXIT_FAILURE); - case 'h': - usage(basename(argv[0])); - exit(EXIT_SUCCESS); - case 'd': - trb_debug = strtoul(optarg, NULL, 0); - break; - case 'V': - printf("%s %s, using libtrbnet %s\n", - basename(argv[0]), trbrichcmd_version, trbnet_version); - exit(EXIT_SUCCESS); - break; - default: - break; - } - } - - if (optind >= argc) { - usage(basename(argv[0])); - exit(EXIT_FAILURE); - } - - /* Open port */ - init_ports(); - - if (strcmp(argv[optind], "uid") == 0) { - - /*************************************************/ - /* Read UIDs */ - /*************************************************/ - - uint32_t *buffer = NULL; - uint32_t *tmp = NULL; - uint32_t *end = NULL; - uint16_t trb_address = 0xfffb; - int status = 0; - - if (argc - optind == 2) { - trb_address = (uint16_t)strtoul(argv[optind + 1], NULL, 0); - } else if (argc - optind > 2) { - usage(argv[0]); - exit(EXIT_FAILURE); - } - - if ((buffer = (uint32_t*)malloc(sizeof(uint32_t) * BUFFER_SIZE)) == NULL) { - abort(); - } - - if (trb_register_write(trb_address, adcm_uid_register, 0) == -1) { - trb_error("Error read temperature"); - exit(EXIT_FAILURE); - } - - status = trb_register_read_mem(trb_address, adcm_uid_register, 0, 64, - buffer, BUFFER_SIZE); - if (status == -1) { - trb_error("Error read temperature"); - exit(EXIT_FAILURE); - } - - tmp = buffer; - end = buffer + status; - while (tmp < end) { - unsigned int ctr; - unsigned int i; - unsigned int len = *tmp >> 16; - printf("EndPoint: 0x%04x\n", *tmp & 0xffff); - tmp++; - for (i = 0, ctr = 0; (i < len) && (tmp < end); i += 4, tmp += 4, ctr++) { - if ((tmp[2] & 0x8000) > 0 ) { - printf("%02d 0x%08x%08x\n", ctr, tmp[1], tmp[0]); - } else { - printf("%02d ------------------\n", ctr); - } - } - printf("\n"); - } - - } else if (strcmp(argv[optind], "temp") == 0) { - - /*************************************************/ - /* Read Temperatures */ - /*************************************************/ - - uint32_t *buffer = NULL; - uint32_t *tmp = NULL; - uint32_t *end = NULL; - int status = 0; - uint16_t trb_address = 0xfffb; - - if (argc - optind == 2) { - trb_address = (uint16_t)strtoul(argv[optind + 1], NULL, 0); - } else if (argc - optind > 2) { - usage(argv[0]); - exit(EXIT_FAILURE); - } - - if ((buffer = (uint32_t*)malloc(sizeof(uint32_t) * BUFFER_SIZE)) == NULL) { - abort(); - } - - if (trb_register_write(trb_address, adcm_uid_register, 0) == -1) { - trb_error("Error read temperature"); - exit(EXIT_FAILURE); - } - - status = trb_register_read_mem(trb_address, adcm_uid_register, 0, 64, - buffer, BUFFER_SIZE); - if (status == -1) { - trb_error("Error read temperature"); - exit(EXIT_FAILURE); - } - - tmp = buffer; - end = buffer + status; - while (tmp < end) { - unsigned int ctr; - unsigned int i; - unsigned int len = *tmp >> 16; - printf("EndPoint: 0x%04x\n", *tmp & 0xffff); - tmp++; - for (i = 0, ctr = 0; (i < len) && (tmp < end); i += 4, tmp += 4, ctr++) { - if ((tmp[2] & 0x8000) > 0 ) { - int16_t temperature = tmp[2] & 0x7fff; - temperature = (temperature & 0x0800) == 0 - ? temperature - : temperature | 0xf000; - printf("%02d %.04f\n", ctr, temperature * TEMP_RESOLUTION); - } else { - printf("%02d -------\n", ctr); - } - } - printf("\n"); - } - - } else if (strcmp(argv[optind], "adcsnoop") == 0) { - - /*************************************************/ - /* Read ADC-Samples */ - /*************************************************/ - - uint32_t *sampleBuffer[16]; - uint32_t *buffer = NULL; - uint16_t trb_address = 0; - unsigned int ch = 0; - unsigned int i = 0; - int status = 0; - - if (argc - optind != 2) { - usage(argv[0]); - exit(EXIT_FAILURE); - } - - trb_address = (uint16_t)strtoul(argv[optind + 1], NULL, 0); - - if ((buffer = (uint32_t*)malloc(sizeof(uint32_t) * 1025)) == NULL) { - abort(); - } - - for (ch = 0; ch < 16; ch++) { - uint16_t reg_address = 0xf000 + (0x800 * (ch / 8)); - uint32_t value = (ch % 8); - uint32_t start = 0; - if (trb_register_write(trb_address, reg_address, 0x4000 | value) == -1) { - trb_error("Error start sampling"); - exit(EXIT_FAILURE); - } - usleep(1000000); - if (trb_register_write(trb_address, reg_address, value) == -1) { - trb_error("Error stop sampling"); - exit(EXIT_FAILURE); - } - - status = trb_register_read_mem(trb_address, reg_address, - 0, 1024, buffer, 1025); - if (status == -1) { - trb_error("Error reading sampleBuffer"); - exit(EXIT_FAILURE); - } - if (status != 1025) { - fprintf(stderr, "Error reading data from channel %d\n", ch); - exit(EXIT_FAILURE); - } - - if ((sampleBuffer[ch] = - (uint32_t*)malloc(sizeof(uint32_t) * 1024)) == NULL) { - abort(); - } - start = ((buffer[1] >> 16) & 0x3ff) + 1; - fprintf(stderr, "ch: %2d start=%4d 0x%08x\n", ch, start, buffer[1]); - if (start > 1024) { - fprintf(stderr, "Invalid start: %d\n", start); - exit(EXIT_FAILURE); - } - if (start == 1024) start = 0; - for (i = 0; i < 1024; i++) { - sampleBuffer[ch][i] = buffer[start + 1] & 0x3fff; - start = start >= 1023 ? 0 : start + 1; - } - } - -#if 1 - for (ch = 0; ch < 16; ch++) { - fprintf(stdout, "# channel: %2d\n", ch); - for (i = 0; i < 1024; i++) { - fprintf(stdout, "%4d %4d\n", i, sampleBuffer[ch][i]); - } - fprintf(stdout, "\n\n"); - } -#else - for (i = 0; i < 1024; i++) { - fprintf(stdout, "%4d ", i); - for (ch = 0; ch < 16; ch++) { - fprintf(stdout, " %4d", sampleBuffer[ch * 1024 + i]); - } - fprintf(stdout, "\n"); - } -#endif - - for (ch = 0; ch < 16; ch++) { - free(sampleBuffer[ch]); - sampleBuffer[ch] = NULL; - } - - } else { - usage(basename(argv[0])); - exit(EXIT_FAILURE); - } - - exit(EXIT_SUCCESS); -}