From cecdb668d99aa535b3e2a6f77fe55f62f15b1a03 Mon Sep 17 00:00:00 2001 From: hadaq Date: Fri, 16 Jul 2010 16:04:01 +0000 Subject: [PATCH] initial release --- trbnetd/Makefile | 123 ++++ trbnetd/pulser.c | 204 ++++++ trbnetd/trb_i2c.c | 306 ++++++++ trbnetd/trbcmd.c | 1343 +++++++++++++++++++++++++++++++++++ trbnetd/trbdhcp.c | 222 ++++++ trbnetd/trberror.c | 169 +++++ trbnetd/trberror.h | 66 ++ trbnetd/trbflash.c | 1553 +++++++++++++++++++++++++++++++++++++++++ trbnetd/trbflash.log | 4 + trbnetd/trbnet.h | 251 +++++++ trbnetd/trbnetrpc.c | 309 ++++++++ trbnetd/trbrichcmd.c | 280 ++++++++ trbnetd/trbrpc.h | 246 +++++++ trbnetd/trbrpc.x | 76 ++ trbnetd/trbrpc_clnt.c | 172 +++++ trbnetd/trbrpc_svc.c | 283 ++++++++ trbnetd/trbrpc_xdr.c | 179 +++++ 17 files changed, 5786 insertions(+) create mode 100644 trbnetd/Makefile create mode 100644 trbnetd/pulser.c create mode 100644 trbnetd/trb_i2c.c create mode 100644 trbnetd/trbcmd.c create mode 100644 trbnetd/trbdhcp.c create mode 100644 trbnetd/trberror.c create mode 100644 trbnetd/trberror.h create mode 100644 trbnetd/trbflash.c create mode 100644 trbnetd/trbflash.log create mode 100644 trbnetd/trbnet.h create mode 100644 trbnetd/trbnetrpc.c create mode 100644 trbnetd/trbrichcmd.c create mode 100644 trbnetd/trbrpc.h create mode 100644 trbnetd/trbrpc.x create mode 100644 trbnetd/trbrpc_clnt.c create mode 100644 trbnetd/trbrpc_svc.c create mode 100644 trbnetd/trbrpc_xdr.c diff --git a/trbnetd/Makefile b/trbnetd/Makefile new file mode 100644 index 0000000..d5ec032 --- /dev/null +++ b/trbnetd/Makefile @@ -0,0 +1,123 @@ +# ------------ Compiler / Linker Options ------------------------------- + +INCDIR = -I. +CPPFLAGS = + +CC = gcc +CFLAGS = -pipe -g -Wall -O3 + +CXX = g++ +CXXFLAGS = -pipe -g -Wall + +LD = $(CC) +#LD = $(CXX) + +LDFLAGS = +LIBDIR = -L. +LOADLIBES = + +# ------------ TARGETS ------------------------------------------------- + +TARGETS = trbcmd trbrichcmd trbflash trb_i2c trbdhcp pulser + +LIB_TARGETS = libtrbnet.a + +# ------------ Libaries ------------------------------------------------ + +AR = ar +ARFLAGS = -srv + +# ------------ Pattern Rules ------------------------------------------- + +# C Code: +%.o: %.c + $(CC) $< -c $(CFLAGS) $(CPPFLAGS) $(INCDIR) -o $@ + +# C++ Code: +%.o: %.cpp + $(CXX) $< -c $(CXXFLAGS) $(CPPFLAGS) $(INCDIR) -o $@ + +%.o: %.cc + $(CXX) $< -c $(CXXFLAGS) $(CPPFLAGS) $(INCDIR) -o $@ + +%.o: %.C + $(CXX) $< -c $(CXXFLAGS) $(CPPFLAGS) $(INCDIR) -o $@ + +# C/C++ Objects (set LD accordingly) +%: %.o + @echo LINKING $@ + $(LD) $^ $(LDFLAGS) $(LIBDIR) $(LOADLIBES) -o $@ + @echo DONE! + +# Libaries +%.a: $% + @echo CREATING library $@ + $(AR) $(ARFLAGS) $@ $^ + @echo DONE! + +%.so: $% + @echo CREATING shared library $@ + $(LD) -shared -O $^ -o $@ + @echo DONE! + +# RPCGEN +%.h: %.x + rpcgen -N -M $^ + +# ------------ Targets ------------------------------------------------- + +.PHONY: all +all: $(LIB_TARGETS) $(TARGETS) + +.PHONY: clean +clean: + rm -f *.o core core.* + rcsclean + +.PHONY: distclean +distclean: clean + rm -f $(TARGETS) $(LIB_TARGETS) + rcsclean -u + +.PHONY: cleanrpc +cleanrpc: + rm -f trbrpc_clnt.c trbrpc_svc.c trbrpc.h trbrpc_xdr.c + +.PHONY: depend +depend: + $(CC) -MM $(CFLAGS) $(CPPFLAGS) $(INCDIR) *.c + +# ------------ Dependencies -------------------------------------------- + +trbcmd: trbcmd.o libtrbnet.a +trbcmd.o: trbcmd.c + +trbdhcp: trbdhcp.o libtrbnet.a +trbdhcp.o: trbdhcp.c + +trbflash: trbflash.o libtrbnet.a +trbflash.o: trbflash.c + +trbrichcmd: trbrichcmd.o libtrbnet.a +trbrichcmd.o: trbrichcmd.c + +trb_i2c: trb_i2c.o libtrbnet.a +trb_i2c.o: trb_i2c.c + +pulser: pulser.o libtrbnet.a +pulser.o: pulser.c + +# ---------------------------------------------------------------------- + +libtrbnet.a: trbnetrpc.o trbrpc_clnt.o trbrpc_xdr.o trberror.o + +trbnetrpc.o: trbnetrpc.c trbrpc.h + +trbrpc_clnt.o: trbrpc_clnt.c trbrpc.h + +trbrpc.o: trbrpc.c trbrpc.h + +trbrpc.h: trbrpc.x + +trberror.o: trberror.c + diff --git a/trbnetd/pulser.c b/trbnetd/pulser.c new file mode 100644 index 0000000..295fe30 --- /dev/null +++ b/trbnetd/pulser.c @@ -0,0 +1,204 @@ +#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 new file mode 100644 index 0000000..573bc6f --- /dev/null +++ b/trbnetd/trb_i2c.c @@ -0,0 +1,306 @@ +#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 new file mode 100644 index 0000000..d9db3fd --- /dev/null +++ b/trbnetd/trbcmd.c @@ -0,0 +1,1343 @@ +/* + * 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 new file mode 100644 index 0000000..fac0022 --- /dev/null +++ b/trbnetd/trbdhcp.c @@ -0,0 +1,222 @@ +#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 new file mode 100644 index 0000000..e8f1282 --- /dev/null +++ b/trbnetd/trberror.c @@ -0,0 +1,169 @@ +#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 new file mode 100644 index 0000000..695b798 --- /dev/null +++ b/trbnetd/trberror.h @@ -0,0 +1,66 @@ +#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 new file mode 100644 index 0000000..1c3f306 --- /dev/null +++ b/trbnetd/trbflash.c @@ -0,0 +1,1553 @@ +#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/trbflash.log b/trbnetd/trbflash.log new file mode 100644 index 0000000..d8a8a85 --- /dev/null +++ b/trbnetd/trbflash.log @@ -0,0 +1,4 @@ + + +------------ Sat Jun 12 00:28:24 2010 ---------------------------------- + diff --git a/trbnetd/trbnet.h b/trbnetd/trbnet.h new file mode 100644 index 0000000..f6cc54c --- /dev/null +++ b/trbnetd/trbnet.h @@ -0,0 +1,251 @@ +#ifndef TRBNET_H +#define TRBNET_H + +extern const char trbnet_version[]; + +#include +#include + +extern unsigned int trb_debug; +extern unsigned int trb_dma; + +#ifdef __cplusplus +extern "C" { +#endif + +/* ---------------------------------------------------------------------- */ + +int trb_connect(const char* server); + +int init_ports(); + +int trb_register_read(uint16_t trb_address, + uint16_t reg_address, + uint32_t* data, + unsigned int dsize); + +int trb_register_read_mem(uint16_t trb_address, + uint16_t reg_address, + uint8_t option, + uint16_t size, + uint32_t* data, + unsigned int dsize); + + +int trb_register_write(uint16_t trb_address, + uint16_t reg_address, + uint32_t value); + +int trb_register_write_mem(uint16_t trb_address, + uint16_t reg_address, + uint8_t option, + const uint32_t* data, + uint16_t size); + +int trb_read_uid(uint16_t trb_address, + uint32_t* data, + unsigned int dsize); + +int trb_set_address(uint64_t uid, + uint8_t endpoint, + uint16_t trb_address); + + +int trb_ipu_data_read(uint8_t type, + uint8_t trg_info, + uint8_t trg_random, + uint16_t trg_number, + uint32_t* data, + unsigned int dsize); + +int trb_send_trigger(uint8_t type, + uint32_t info, + uint8_t random, + uint16_t number); + +int trb_send_trigger_rich(uint8_t input, + uint8_t type, + uint32_t info, + uint8_t random, + uint16_t number); + +int fpga_register_read(uint16_t reg_address, uint32_t* value); + +int fpga_register_write(uint16_t reg_address, uint32_t value); + +int trb_fifo_flush(uint8_t channel); + +int network_reset(); + +int com_reset(); + +int trb_register_modify(uint16_t trb_address, + uint16_t reg_address, + int mode, + uint32_t bitMask, + uint32_t bitValue); + +/* ---------------------------------------------------------------------- */ + +/* This library provides several function to access the trbnet on a + Etrax-Board. + +*/ + +/************************************************************************/ +/* In case of any error the gloabl varianble 'trb_errno' will be set, */ +/* see trberror.h for details. */ +/************************************************************************/ + + +/************************************************************************/ +/* int trb_register_read(uint16_t trb_address, + uint16_t reg_address, + uint32_t* data, + unsigned int dsize); + + trb_address: TRB-Address of the TRB-Endpoint + reg_address: Register-Address to be read from, broadcasts are supported + data: Pointer to a uint32_t Data-Buffer + dsize: Size of the Data-Buffer in units of 32bit-words + + ReturnValue: == -1 on error, trberrno will be set + >= 0 number of 32bit-words which were stored in Data-Buffer + + TRB-Channel used: slow control (3) + + reads the register reg_address of a TRB-Endpoint with address + trb_address. The received data is stored in the Data-Buffer data. + + The format of the Data-Buffer is: + first word: TRB-Address of the sender + second word: register value + + --> The size of the Data-Buffer must be at least >= 2 + +*/ + + +/************************************************************************/ +/* int trb_register_read_mem(uint16_t trb_address, + uint16_t reg_address, + uint8_t option, + uint16_t size, + uint32_t* data, + unsigned int dsize); + + trb_address: TRB-Address of the TRB-Endpoint + reg_address: Register-Address to be read from, broadcasts are supported + uint8_t option: + uint16_t size: Number of 32Bit-words to be read + data: Pointer to a uint32_t Data-Buffer + dsize: Size of the Data-Buffer in units of 32bit-words + + ReturnValue: == -1 on error, trberrno will be set + >= 0 number of 32bit-words which were stored in Data-Buffer + + TRB-Channel used: slow control (3) + + reads the register reg_address of a TRB-Endpoint with address + trb_address. The received data is stored in the Data-Buffer data. + + The format of the Data-Buffer is: + first word: TRB-Address of the sender (Lower 2Bytes), len (Upper 2bytes) + second word: register value + + --> The size of the Data-Buffer must be at least >= 2 + +*/ + + +/************************************************************************/ +/* int trb_register_write(uint16_t trb_address, + uint16_t reg_address, + uint32_t value); + + trb_address: TRB-Address of the TRB-Endpoint, broadcasts are supported + reg_address: Register-Address to be written to + value: 32bit-word to be writen to the register + + ReturnValue: == -1 on error, trberrno will be set + + TRB-Channel used: slow control (3) + + writes the value to the register reg_address of a TRB-Endpoint with address + trb_address. +*/ + +/************************************************************************/ +/* int trb_read_uid(uint16_t trb_address, + uint32_t* data, + unsigned int dsize); + + trb_address: TRB-Address of the TRB-Endpoint, broadcasts are supported + data: Pointer to a uint32_t Data-Buffer + dsize: Size of the Data-Buffer in units of 32bit-words + + ReturnValue: == -1 on error, trberrno will be set + >= 0 number of 32bit-words which were stored in Data-Buffer + + TRB-Channel used: slow control (3) + + reads the Unique-Id of a TRB-Endpoint with address trb_address. The + received data is stored in the Data-Buffer data. + + The format of the Data-Buffer is: + first word: UID High-32Bit Word + second word: UID Low-32Bit Word + third word: Endpoint Number + fourth word: TRB-Address of the sender + + --> The size of the Data-Buffer must be at least >= 4 + +*/ + +/************************************************************************/ +/* int trb_set_address(uint64_t uid, + uint8_t endpoint, + uint16_t trb_address); + + uint64_t uid: the UID of the Endpoint + uint8_t endpoint: Number of the TRBNet-Endpoint + uint16_t trb_address: the new TRB-Netaddress, broadcasts are not supported + + ReturnValue: == -1 on error, trberrno will be set + == 0 on success + + TRB-Channel used: slow control (3) + + sets a new TRB-Address trb_address of the give TRFNet-endpoint. +*/ + +/************************************************************************/ +/* int trb_ipu_data_read(uint8_t type, + uint8_t trg_info, + uint8_t trg_random, + uint16_t trg_number, + uint32_t* data, + unsigned int dsize); + + data: Pointer to a uint32_t Data-Buffer + dsize: Size of the Data-Buffer in units of 32bit-words + + ReturnValue: == -1 on error, trberrno will be set + >= 0 0 number of 32bit-words which were stored in Data-Buffer + + send a request to all TRBNet-Endpoints to readout the IPU-Data. The IPU + Datastream will be stored in the user DataBufer. +*/ + +/************************************************************************/ +/* int trb_send_trigger(uint8_t type, + uint32_t info, + uint8_t random, + uint16_t number); +*/ + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/trbnetd/trbnetrpc.c b/trbnetd/trbnetrpc.c new file mode 100644 index 0000000..75dd2bb --- /dev/null +++ b/trbnetd/trbnetrpc.c @@ -0,0 +1,309 @@ +#include +#include /* always needed */ +#include "trbrpc.h" /* will be generated by rpcgen */ + +#include + +#include "trbnet.h" + +const char trbnet_version[] = "$Revision: 1.1 $"; +unsigned int trb_debug = 0; +unsigned int trb_dma = 0; + +static CLIENT *trb_client = NULL; +static char trb_server[128] = ""; + +static void copyStatus(const Status* status) +{ + trb_errno = status->trb_errno; + trb_term.status_common = status->status_common; + trb_term.status_channel = status->status_channel; + trb_term.sequence = status->sequence; + trb_term.channel = status->channel; +} + +int init_ports() +{ + return trb_connect(NULL); +} + +int trb_connect(const char *server) +{ + if (server == NULL) { + char *tmp; + tmp = getenv("TRBNETSERVER"); + if (tmp != NULL) { + strncpy(trb_server, tmp, 128); + } else { + sprintf(trb_server, "trb1"); + } + } else { + strncpy(trb_server, server, 128); + } + trb_client = clnt_create(trb_server, TRBNETRPCPROG, TRBNETRPCVERS, "tcp"); + if (trb_client == NULL) { + /* + * Couldn't establish connection with server. + * Print error message and stop. + */ + clnt_pcreateerror(trb_server); + return -1; + } + + return 0; +} + + +int trb_register_read(uint16_t trb_address, + uint16_t reg_address, + uint32_t * data, unsigned int dsize) +{ + enum clnt_stat status; + RetVal retVal; + + if (trb_client == NULL) + return -1; + + retVal.data.Buffer_val = data; + retVal.data.Buffer_len = dsize; + + status = + register_read_1(trb_address, reg_address, dsize, + &retVal, trb_client); + copyStatus(&retVal.status); + + if (status != RPC_SUCCESS) { + /* + * An error occurred while calling the server. + * Print error message and stop. + */ + clnt_perror(trb_client, trb_server); + return -2; + } + + return trb_errno == TRB_NONE ? retVal.data.Buffer_len : -1; +} + +int trb_register_read_mem(uint16_t trb_address, + uint16_t reg_address, + uint8_t option, + uint16_t size, + uint32_t* data, + unsigned int dsize) +{ + enum clnt_stat status; + RetVal retVal; + + if (trb_client == NULL) + return -1; + + retVal.data.Buffer_val = data; + retVal.data.Buffer_len = dsize; + + status = + register_read_mem_1(trb_address, reg_address, option, size, dsize, + &retVal, trb_client); + copyStatus(&retVal.status); + + if (status != RPC_SUCCESS) { + /* + * An error occurred while calling the server. + * Print error message and stop. + */ + clnt_perror(trb_client, trb_server); + return -2; + } + + return trb_errno == TRB_NONE ? retVal.data.Buffer_len : -1; +} + + +int trb_read_uid(uint16_t trb_address, + uint32_t* data, + unsigned int dsize) +{ + enum clnt_stat status; + RetVal retVal; + + if (trb_client == NULL) return -1; + + retVal.data.Buffer_val = data; + retVal.data.Buffer_len = dsize; + + status = read_uid_1(trb_address, dsize, &retVal, trb_client); + copyStatus(&retVal.status); + + if (status != RPC_SUCCESS) { + /* + * An error occurred while calling the server. + * Print error message and stop. + */ + clnt_perror(trb_client, trb_server); + return -2; + } + + return trb_errno == TRB_NONE ? retVal.data.Buffer_len : -1; +} + + +int trb_register_write(uint16_t trb_address, + uint16_t reg_address, + uint32_t value) +{ + enum clnt_stat status; + Status retVal; + + if (trb_client == NULL) return -1; + + status = register_write_1(trb_address, reg_address, value, + &retVal, trb_client); + copyStatus(&retVal); + + if (status != RPC_SUCCESS) { + /* + * An error occurred while calling the server. + * Print error message and stop. + */ + clnt_perror(trb_client, trb_server); + return -1; + } + + return trb_errno == TRB_NONE ? 0 : -1; +} + +int trb_register_write_mem(uint16_t trb_address, + uint16_t reg_address, + uint8_t option, + const uint32_t* data, + uint16_t size) +{ + enum clnt_stat status; + Status retVal; + Buffer buffer; + + if (trb_client == NULL) return -1; + + buffer.Buffer_val = data; + buffer.Buffer_len = size; + + status = register_write_mem_1(trb_address, reg_address, option, buffer, + &retVal, trb_client); + copyStatus(&retVal); + + if (status != RPC_SUCCESS) { + /* + * An error occurred while calling the server. + * Print error message and stop. + */ + clnt_perror(trb_client, trb_server); + return -1; + } + + return trb_errno == TRB_NONE ? 0 : -1; +} + +int trb_set_address(uint64_t uid, + uint8_t endpoint, + uint16_t trb_address) +{ + enum clnt_stat status; + Status retVal; + + if (trb_client == NULL) return -1; + + status = set_address_1(uid, endpoint, trb_address, + &retVal, trb_client); + copyStatus(&retVal); + + if (status != RPC_SUCCESS) { + /* + * An error occurred while calling the server. + * Print error message and stop. + */ + clnt_perror(trb_client, trb_server); + return -1; + } + + return trb_errno == TRB_NONE ? 0 : -1; +} + + +int trb_ipu_data_read(uint8_t type, + uint8_t trg_info, + uint8_t trg_random, + uint16_t trg_number, + uint32_t* data, + unsigned int dsize) +{ + return -1; +} + +int trb_send_trigger(uint8_t type, + uint32_t info, + uint8_t random, + uint16_t number) +{ + return -1; +} + +int trb_send_trigger_rich(uint8_t input, + uint8_t type, + uint32_t info, + uint8_t random, + uint16_t number) +{ + return -1; +} + +int fpga_register_read(uint16_t reg_address, uint32_t* value) +{ + return -1; +} + + +int fpga_register_write(uint16_t reg_address, uint32_t value) +{ + return -1; +} + +int trb_fifo_flush(uint8_t channel) +{ + return -1; +} + +int network_reset() +{ + enum clnt_stat status; + Status retVal; + + if (trb_client == NULL) return -1; + + status = network_reset_1(&retVal, trb_client); + + copyStatus(&retVal); + + if (status != RPC_SUCCESS) { + /* + * An error occurred while calling the server. + * Print error message and stop. + */ + clnt_perror(trb_client, trb_server); + return -1; + } + + return trb_errno == TRB_NONE ? 0 : -1; +} + +int com_reset() +{ + return -1; +} + +int trb_register_modify(uint16_t trb_address, + uint16_t reg_address, + int mode, + uint32_t bitMask, + uint32_t bitValue) +{ + return -1; +} diff --git a/trbnetd/trbrichcmd.c b/trbnetd/trbrichcmd.c new file mode 100644 index 0000000..2fa9628 --- /dev/null +++ b/trbnetd/trbrichcmd.c @@ -0,0 +1,280 @@ +#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); +} diff --git a/trbnetd/trbrpc.h b/trbnetd/trbrpc.h new file mode 100644 index 0000000..2525d85 --- /dev/null +++ b/trbnetd/trbrpc.h @@ -0,0 +1,246 @@ +/* + * Please do not edit this file. + * It was generated using rpcgen. + */ + +#ifndef _TRBRPC_H_RPCGEN +#define _TRBRPC_H_RPCGEN + +#include + +#include + +#ifdef __cplusplus +extern "C" { +#endif + + +typedef struct { + u_int Buffer_len; + uint32_t *Buffer_val; +} Buffer; + +struct Status { + int trb_errno; + uint16_t status_common; + uint16_t status_channel; + uint16_t sequence; + uint8_t channel; +}; +typedef struct Status Status; + +struct RetVal { + Status status; + Buffer data; +}; +typedef struct RetVal RetVal; + +struct register_read_1_argument { + uint16_t arg1; + uint16_t arg2; + u_int arg3; +}; +typedef struct register_read_1_argument register_read_1_argument; + +struct register_read_mem_1_argument { + uint16_t arg1; + uint16_t arg2; + uint8_t arg3; + uint16_t arg4; + u_int arg5; +}; +typedef struct register_read_mem_1_argument register_read_mem_1_argument; + +struct register_write_1_argument { + uint16_t arg1; + uint16_t arg2; + uint32_t arg3; +}; +typedef struct register_write_1_argument register_write_1_argument; + +struct register_write_mem_1_argument { + uint16_t arg1; + uint16_t arg2; + uint8_t arg3; + Buffer arg4; +}; +typedef struct register_write_mem_1_argument register_write_mem_1_argument; + +struct read_uid_1_argument { + uint16_t arg1; + u_int arg2; +}; +typedef struct read_uid_1_argument read_uid_1_argument; + +struct set_address_1_argument { + uint64_t arg1; + uint8_t arg2; + uint16_t arg3; +}; +typedef struct set_address_1_argument set_address_1_argument; + +struct send_trigger_1_argument { + uint8_t arg1; + uint32_t arg2; + uint8_t arg3; + uint16_t arg4; +}; +typedef struct send_trigger_1_argument send_trigger_1_argument; + +struct ipu_data_read_1_argument { + uint8_t arg1; + uint32_t arg2; + uint8_t arg3; + uint16_t arg4; + u_int arg5; +}; +typedef struct ipu_data_read_1_argument ipu_data_read_1_argument; + +struct fpga_register_write_1_argument { + uint16_t arg1; + uint32_t arg2; +}; +typedef struct fpga_register_write_1_argument fpga_register_write_1_argument; + +struct register_modify_1_argument { + uint16_t arg1; + uint16_t arg2; + int arg3; + uint32_t arg4; + uint32_t arg5; +}; +typedef struct register_modify_1_argument register_modify_1_argument; + +#define TRBNETRPCPROG 0x20000099 +#define TRBNETRPCVERS 1 + +#if defined(__STDC__) || defined(__cplusplus) +#define REGISTER_READ 1 +extern enum clnt_stat register_read_1(uint16_t , uint16_t , u_int , RetVal *, CLIENT *); +extern bool_t register_read_1_svc(uint16_t , uint16_t , u_int , RetVal *, struct svc_req *); +#define REGISTER_READ_MEM 2 +extern enum clnt_stat register_read_mem_1(uint16_t , uint16_t , uint8_t , uint16_t , u_int , RetVal *, CLIENT *); +extern bool_t register_read_mem_1_svc(uint16_t , uint16_t , uint8_t , uint16_t , u_int , RetVal *, struct svc_req *); +#define REGISTER_WRITE 3 +extern enum clnt_stat register_write_1(uint16_t , uint16_t , uint32_t , Status *, CLIENT *); +extern bool_t register_write_1_svc(uint16_t , uint16_t , uint32_t , Status *, struct svc_req *); +#define REGISTER_WRITE_MEM 4 +extern enum clnt_stat register_write_mem_1(uint16_t , uint16_t , uint8_t , Buffer , Status *, CLIENT *); +extern bool_t register_write_mem_1_svc(uint16_t , uint16_t , uint8_t , Buffer , Status *, struct svc_req *); +#define READ_UID 5 +extern enum clnt_stat read_uid_1(uint16_t , u_int , RetVal *, CLIENT *); +extern bool_t read_uid_1_svc(uint16_t , u_int , RetVal *, struct svc_req *); +#define SET_ADDRESS 6 +extern enum clnt_stat set_address_1(uint64_t , uint8_t , uint16_t , Status *, CLIENT *); +extern bool_t set_address_1_svc(uint64_t , uint8_t , uint16_t , Status *, struct svc_req *); +#define SEND_TRIGGER 7 +extern enum clnt_stat send_trigger_1(uint8_t , uint32_t , uint8_t , uint16_t , Status *, CLIENT *); +extern bool_t send_trigger_1_svc(uint8_t , uint32_t , uint8_t , uint16_t , Status *, struct svc_req *); +#define IPU_DATA_READ 8 +extern enum clnt_stat ipu_data_read_1(uint8_t , uint32_t , uint8_t , uint16_t , u_int , RetVal *, CLIENT *); +extern bool_t ipu_data_read_1_svc(uint8_t , uint32_t , uint8_t , uint16_t , u_int , RetVal *, struct svc_req *); +#define FPGA_REGISTER_READ 9 +extern enum clnt_stat fpga_register_read_1(uint16_t , RetVal *, CLIENT *); +extern bool_t fpga_register_read_1_svc(uint16_t , RetVal *, struct svc_req *); +#define FPGA_REGISTER_WRITE 10 +extern enum clnt_stat fpga_register_write_1(uint16_t , uint32_t , Status *, CLIENT *); +extern bool_t fpga_register_write_1_svc(uint16_t , uint32_t , Status *, struct svc_req *); +#define TRB_FIFO_FLUSH 11 +extern enum clnt_stat trb_fifo_flush_1(uint8_t , Status *, CLIENT *); +extern bool_t trb_fifo_flush_1_svc(uint8_t , Status *, struct svc_req *); +#define NETWORK_RESET 12 +extern enum clnt_stat network_reset_1(Status *, CLIENT *); +extern bool_t network_reset_1_svc(Status *, struct svc_req *); +#define COM_RESET 13 +extern enum clnt_stat com_reset_1(Status *, CLIENT *); +extern bool_t com_reset_1_svc(Status *, struct svc_req *); +#define REGISTER_MODIFY 14 +extern enum clnt_stat register_modify_1(uint16_t , uint16_t , int , uint32_t , uint32_t , Status *, CLIENT *); +extern bool_t register_modify_1_svc(uint16_t , uint16_t , int , uint32_t , uint32_t , Status *, struct svc_req *); +extern int trbnetrpcprog_1_freeresult (SVCXPRT *, xdrproc_t, caddr_t); + +#else /* K&R C */ +#define REGISTER_READ 1 +extern enum clnt_stat register_read_1(); +extern bool_t register_read_1_svc(); +#define REGISTER_READ_MEM 2 +extern enum clnt_stat register_read_mem_1(); +extern bool_t register_read_mem_1_svc(); +#define REGISTER_WRITE 3 +extern enum clnt_stat register_write_1(); +extern bool_t register_write_1_svc(); +#define REGISTER_WRITE_MEM 4 +extern enum clnt_stat register_write_mem_1(); +extern bool_t register_write_mem_1_svc(); +#define READ_UID 5 +extern enum clnt_stat read_uid_1(); +extern bool_t read_uid_1_svc(); +#define SET_ADDRESS 6 +extern enum clnt_stat set_address_1(); +extern bool_t set_address_1_svc(); +#define SEND_TRIGGER 7 +extern enum clnt_stat send_trigger_1(); +extern bool_t send_trigger_1_svc(); +#define IPU_DATA_READ 8 +extern enum clnt_stat ipu_data_read_1(); +extern bool_t ipu_data_read_1_svc(); +#define FPGA_REGISTER_READ 9 +extern enum clnt_stat fpga_register_read_1(); +extern bool_t fpga_register_read_1_svc(); +#define FPGA_REGISTER_WRITE 10 +extern enum clnt_stat fpga_register_write_1(); +extern bool_t fpga_register_write_1_svc(); +#define TRB_FIFO_FLUSH 11 +extern enum clnt_stat trb_fifo_flush_1(); +extern bool_t trb_fifo_flush_1_svc(); +#define NETWORK_RESET 12 +extern enum clnt_stat network_reset_1(); +extern bool_t network_reset_1_svc(); +#define COM_RESET 13 +extern enum clnt_stat com_reset_1(); +extern bool_t com_reset_1_svc(); +#define REGISTER_MODIFY 14 +extern enum clnt_stat register_modify_1(); +extern bool_t register_modify_1_svc(); +extern int trbnetrpcprog_1_freeresult (); +#endif /* K&R C */ + +/* the xdr functions */ + +#if defined(__STDC__) || defined(__cplusplus) +extern bool_t xdr_Buffer (XDR *, Buffer*); +extern bool_t xdr_Status (XDR *, Status*); +extern bool_t xdr_RetVal (XDR *, RetVal*); +extern bool_t xdr_register_read_1_argument (XDR *, register_read_1_argument*); +extern bool_t xdr_register_read_mem_1_argument (XDR *, register_read_mem_1_argument*); +extern bool_t xdr_register_write_1_argument (XDR *, register_write_1_argument*); +extern bool_t xdr_register_write_mem_1_argument (XDR *, register_write_mem_1_argument*); +extern bool_t xdr_read_uid_1_argument (XDR *, read_uid_1_argument*); +extern bool_t xdr_set_address_1_argument (XDR *, set_address_1_argument*); +extern bool_t xdr_send_trigger_1_argument (XDR *, send_trigger_1_argument*); +extern bool_t xdr_ipu_data_read_1_argument (XDR *, ipu_data_read_1_argument*); +extern bool_t xdr_fpga_register_write_1_argument (XDR *, fpga_register_write_1_argument*); +extern bool_t xdr_register_modify_1_argument (XDR *, register_modify_1_argument*); + +#else /* K&R C */ +extern bool_t xdr_Buffer (); +extern bool_t xdr_Status (); +extern bool_t xdr_RetVal (); +extern bool_t xdr_register_read_1_argument (); +extern bool_t xdr_register_read_mem_1_argument (); +extern bool_t xdr_register_write_1_argument (); +extern bool_t xdr_register_write_mem_1_argument (); +extern bool_t xdr_read_uid_1_argument (); +extern bool_t xdr_set_address_1_argument (); +extern bool_t xdr_send_trigger_1_argument (); +extern bool_t xdr_ipu_data_read_1_argument (); +extern bool_t xdr_fpga_register_write_1_argument (); +extern bool_t xdr_register_modify_1_argument (); + +#endif /* K&R C */ + +#ifdef __cplusplus +} +#endif + +#endif /* !_TRBRPC_H_RPCGEN */ diff --git a/trbnetd/trbrpc.x b/trbnetd/trbrpc.x new file mode 100644 index 0000000..1edbf9b --- /dev/null +++ b/trbnetd/trbrpc.x @@ -0,0 +1,76 @@ +/* RPC interface definitions of the remote trbnet functions */ + +typedef uint32_t Buffer<>; + +struct Status { + int trb_errno; + uint16_t status_common; + uint16_t status_channel; + uint16_t sequence; + uint8_t channel; +}; + +struct RetVal { + Status status; + Buffer data; +}; + +program TRBNETRPCPROG { + version TRBNETRPCVERS { + + RetVal REGISTER_READ(uint16_t, + uint16_t, + unsigned int) = 1; + + RetVal REGISTER_READ_MEM(uint16_t, + uint16_t, + uint8_t, + uint16_t, + unsigned int) = 2; + + Status REGISTER_WRITE(uint16_t, + uint16_t, + uint32_t) = 3; + + Status REGISTER_WRITE_MEM(uint16_t, + uint16_t, + uint8_t, + Buffer) = 4; + + RetVal READ_UID(uint16_t, + unsigned int) = 5; + + Status SET_ADDRESS(uint64_t, + uint8_t, + uint16_t) = 6; + + Status SEND_TRIGGER(uint8_t, + uint32_t, + uint8_t, + uint16_t) = 7; + + RetVal IPU_DATA_READ(uint8_t, + uint32_t, + uint8_t, + uint16_t, + unsigned int) = 8; + + RetVal FPGA_REGISTER_READ(uint16_t) = 9; + + Status FPGA_REGISTER_WRITE(uint16_t, + uint32_t) = 10; + + Status TRB_FIFO_FLUSH(uint8_t) = 11; + + Status NETWORK_RESET(void) = 12; + + Status COM_RESET(void) = 13; + + Status REGISTER_MODIFY(uint16_t, + uint16_t, + int, + uint32_t, + uint32_t) = 14; + } = 1; +} = 0x20000099; + diff --git a/trbnetd/trbrpc_clnt.c b/trbnetd/trbrpc_clnt.c new file mode 100644 index 0000000..2834fa5 --- /dev/null +++ b/trbnetd/trbrpc_clnt.c @@ -0,0 +1,172 @@ +/* + * Please do not edit this file. + * It was generated using rpcgen. + */ + +#include /* for memset */ +#include "trbrpc.h" + +/* Default timeout can be changed using clnt_control() */ +static struct timeval TIMEOUT = { 25, 0 }; + +enum clnt_stat +register_read_1(uint16_t arg1, uint16_t arg2, u_int arg3, RetVal *clnt_res, CLIENT *clnt) +{ + register_read_1_argument arg; + arg.arg1 = arg1; + arg.arg2 = arg2; + arg.arg3 = arg3; + return (clnt_call (clnt, REGISTER_READ, (xdrproc_t) xdr_register_read_1_argument, (caddr_t) &arg, + (xdrproc_t) xdr_RetVal, (caddr_t) clnt_res, + TIMEOUT)); +} + +enum clnt_stat +register_read_mem_1(uint16_t arg1, uint16_t arg2, uint8_t arg3, uint16_t arg4, u_int arg5, RetVal *clnt_res, CLIENT *clnt) +{ + register_read_mem_1_argument arg; + arg.arg1 = arg1; + arg.arg2 = arg2; + arg.arg3 = arg3; + arg.arg4 = arg4; + arg.arg5 = arg5; + return (clnt_call (clnt, REGISTER_READ_MEM, (xdrproc_t) xdr_register_read_mem_1_argument, (caddr_t) &arg, + (xdrproc_t) xdr_RetVal, (caddr_t) clnt_res, + TIMEOUT)); +} + +enum clnt_stat +register_write_1(uint16_t arg1, uint16_t arg2, uint32_t arg3, Status *clnt_res, CLIENT *clnt) +{ + register_write_1_argument arg; + arg.arg1 = arg1; + arg.arg2 = arg2; + arg.arg3 = arg3; + return (clnt_call (clnt, REGISTER_WRITE, (xdrproc_t) xdr_register_write_1_argument, (caddr_t) &arg, + (xdrproc_t) xdr_Status, (caddr_t) clnt_res, + TIMEOUT)); +} + +enum clnt_stat +register_write_mem_1(uint16_t arg1, uint16_t arg2, uint8_t arg3, Buffer arg4, Status *clnt_res, CLIENT *clnt) +{ + register_write_mem_1_argument arg; + arg.arg1 = arg1; + arg.arg2 = arg2; + arg.arg3 = arg3; + arg.arg4 = arg4; + return (clnt_call (clnt, REGISTER_WRITE_MEM, (xdrproc_t) xdr_register_write_mem_1_argument, (caddr_t) &arg, + (xdrproc_t) xdr_Status, (caddr_t) clnt_res, + TIMEOUT)); +} + +enum clnt_stat +read_uid_1(uint16_t arg1, u_int arg2, RetVal *clnt_res, CLIENT *clnt) +{ + read_uid_1_argument arg; + arg.arg1 = arg1; + arg.arg2 = arg2; + return (clnt_call (clnt, READ_UID, (xdrproc_t) xdr_read_uid_1_argument, (caddr_t) &arg, + (xdrproc_t) xdr_RetVal, (caddr_t) clnt_res, + TIMEOUT)); +} + +enum clnt_stat +set_address_1(uint64_t arg1, uint8_t arg2, uint16_t arg3, Status *clnt_res, CLIENT *clnt) +{ + set_address_1_argument arg; + arg.arg1 = arg1; + arg.arg2 = arg2; + arg.arg3 = arg3; + return (clnt_call (clnt, SET_ADDRESS, (xdrproc_t) xdr_set_address_1_argument, (caddr_t) &arg, + (xdrproc_t) xdr_Status, (caddr_t) clnt_res, + TIMEOUT)); +} + +enum clnt_stat +send_trigger_1(uint8_t arg1, uint32_t arg2, uint8_t arg3, uint16_t arg4, Status *clnt_res, CLIENT *clnt) +{ + send_trigger_1_argument arg; + arg.arg1 = arg1; + arg.arg2 = arg2; + arg.arg3 = arg3; + arg.arg4 = arg4; + return (clnt_call (clnt, SEND_TRIGGER, (xdrproc_t) xdr_send_trigger_1_argument, (caddr_t) &arg, + (xdrproc_t) xdr_Status, (caddr_t) clnt_res, + TIMEOUT)); +} + +enum clnt_stat +ipu_data_read_1(uint8_t arg1, uint32_t arg2, uint8_t arg3, uint16_t arg4, u_int arg5, RetVal *clnt_res, CLIENT *clnt) +{ + ipu_data_read_1_argument arg; + arg.arg1 = arg1; + arg.arg2 = arg2; + arg.arg3 = arg3; + arg.arg4 = arg4; + arg.arg5 = arg5; + return (clnt_call (clnt, IPU_DATA_READ, (xdrproc_t) xdr_ipu_data_read_1_argument, (caddr_t) &arg, + (xdrproc_t) xdr_RetVal, (caddr_t) clnt_res, + TIMEOUT)); +} + +enum clnt_stat +fpga_register_read_1(uint16_t arg1, RetVal *clnt_res, CLIENT *clnt) +{ + return (clnt_call(clnt, FPGA_REGISTER_READ, + (xdrproc_t) xdr_uint16_t, (caddr_t) &arg1, + (xdrproc_t) xdr_RetVal, (caddr_t) clnt_res, + TIMEOUT)); +} + +enum clnt_stat +fpga_register_write_1(uint16_t arg1, uint32_t arg2, Status *clnt_res, CLIENT *clnt) +{ + fpga_register_write_1_argument arg; + arg.arg1 = arg1; + arg.arg2 = arg2; + return (clnt_call (clnt, FPGA_REGISTER_WRITE, (xdrproc_t) xdr_fpga_register_write_1_argument, (caddr_t) &arg, + (xdrproc_t) xdr_Status, (caddr_t) clnt_res, + TIMEOUT)); +} + +enum clnt_stat +trb_fifo_flush_1(uint8_t arg1, Status *clnt_res, CLIENT *clnt) +{ + return (clnt_call(clnt, TRB_FIFO_FLUSH, + (xdrproc_t) xdr_uint8_t, (caddr_t) &arg1, + (xdrproc_t) xdr_Status, (caddr_t) clnt_res, + TIMEOUT)); +} + +enum clnt_stat +network_reset_1(Status *clnt_res, CLIENT *clnt) +{ + return (clnt_call (clnt, NETWORK_RESET, (xdrproc_t) xdr_void, (caddr_t) NULL, + (xdrproc_t) xdr_Status, (caddr_t) clnt_res, + TIMEOUT)); + +} + +enum clnt_stat +com_reset_1(Status *clnt_res, CLIENT *clnt) +{ + return (clnt_call (clnt, COM_RESET, (xdrproc_t) xdr_void, (caddr_t) NULL, + (xdrproc_t) xdr_Status, (caddr_t) clnt_res, + TIMEOUT)); + +} + +enum clnt_stat +register_modify_1(uint16_t arg1, uint16_t arg2, int arg3, uint32_t arg4, uint32_t arg5, Status *clnt_res, CLIENT *clnt) +{ + register_modify_1_argument arg; + arg.arg1 = arg1; + arg.arg2 = arg2; + arg.arg3 = arg3; + arg.arg4 = arg4; + arg.arg5 = arg5; + return (clnt_call (clnt, REGISTER_MODIFY, (xdrproc_t) xdr_register_modify_1_argument, (caddr_t) &arg, + (xdrproc_t) xdr_Status, (caddr_t) clnt_res, + TIMEOUT)); +} diff --git a/trbnetd/trbrpc_svc.c b/trbnetd/trbrpc_svc.c new file mode 100644 index 0000000..9aa55d9 --- /dev/null +++ b/trbnetd/trbrpc_svc.c @@ -0,0 +1,283 @@ +/* + * Please do not edit this file. + * It was generated using rpcgen. + */ + +#include "trbrpc.h" +#include +#include +#include +#include +#include +#include +#include + +#ifndef SIG_PF +#define SIG_PF void(*)(int) +#endif + +int +_register_read_1 (register_read_1_argument *argp, void *result, struct svc_req *rqstp) +{ + return (register_read_1_svc(argp->arg1, argp->arg2, argp->arg3, result, rqstp)); +} + +int +_register_read_mem_1 (register_read_mem_1_argument *argp, void *result, struct svc_req *rqstp) +{ + return (register_read_mem_1_svc(argp->arg1, argp->arg2, argp->arg3, argp->arg4, argp->arg5, result, rqstp)); +} + +int +_register_write_1 (register_write_1_argument *argp, void *result, struct svc_req *rqstp) +{ + return (register_write_1_svc(argp->arg1, argp->arg2, argp->arg3, result, rqstp)); +} + +int +_register_write_mem_1 (register_write_mem_1_argument *argp, void *result, struct svc_req *rqstp) +{ + return (register_write_mem_1_svc(argp->arg1, argp->arg2, argp->arg3, argp->arg4, result, rqstp)); +} + +int +_read_uid_1 (read_uid_1_argument *argp, void *result, struct svc_req *rqstp) +{ + return (read_uid_1_svc(argp->arg1, argp->arg2, result, rqstp)); +} + +int +_set_address_1 (set_address_1_argument *argp, void *result, struct svc_req *rqstp) +{ + return (set_address_1_svc(argp->arg1, argp->arg2, argp->arg3, result, rqstp)); +} + +int +_send_trigger_1 (send_trigger_1_argument *argp, void *result, struct svc_req *rqstp) +{ + return (send_trigger_1_svc(argp->arg1, argp->arg2, argp->arg3, argp->arg4, result, rqstp)); +} + +int +_ipu_data_read_1 (ipu_data_read_1_argument *argp, void *result, struct svc_req *rqstp) +{ + return (ipu_data_read_1_svc(argp->arg1, argp->arg2, argp->arg3, argp->arg4, argp->arg5, result, rqstp)); +} + +int +_fpga_register_read_1 (uint16_t *argp, void *result, struct svc_req *rqstp) +{ + return (fpga_register_read_1_svc(*argp, result, rqstp)); +} + +int +_fpga_register_write_1 (fpga_register_write_1_argument *argp, void *result, struct svc_req *rqstp) +{ + return (fpga_register_write_1_svc(argp->arg1, argp->arg2, result, rqstp)); +} + +int +_trb_fifo_flush_1 (uint8_t *argp, void *result, struct svc_req *rqstp) +{ + return (trb_fifo_flush_1_svc(*argp, result, rqstp)); +} + +int +_network_reset_1 (void *argp, void *result, struct svc_req *rqstp) +{ + return (network_reset_1_svc(result, rqstp)); +} + +int +_com_reset_1 (void *argp, void *result, struct svc_req *rqstp) +{ + return (com_reset_1_svc(result, rqstp)); +} + +int +_register_modify_1 (register_modify_1_argument *argp, void *result, struct svc_req *rqstp) +{ + return (register_modify_1_svc(argp->arg1, argp->arg2, argp->arg3, argp->arg4, argp->arg5, result, rqstp)); +} + +static void +trbnetrpcprog_1(struct svc_req *rqstp, register SVCXPRT *transp) +{ + union { + register_read_1_argument register_read_1_arg; + register_read_mem_1_argument register_read_mem_1_arg; + register_write_1_argument register_write_1_arg; + register_write_mem_1_argument register_write_mem_1_arg; + read_uid_1_argument read_uid_1_arg; + set_address_1_argument set_address_1_arg; + send_trigger_1_argument send_trigger_1_arg; + ipu_data_read_1_argument ipu_data_read_1_arg; + uint16_t fpga_register_read_1_arg; + fpga_register_write_1_argument fpga_register_write_1_arg; + uint8_t trb_fifo_flush_1_arg; + register_modify_1_argument register_modify_1_arg; + } argument; + union { + RetVal register_read_1_res; + RetVal register_read_mem_1_res; + Status register_write_1_res; + Status register_write_mem_1_res; + RetVal read_uid_1_res; + Status set_address_1_res; + Status send_trigger_1_res; + RetVal ipu_data_read_1_res; + RetVal fpga_register_read_1_res; + Status fpga_register_write_1_res; + Status trb_fifo_flush_1_res; + Status network_reset_1_res; + Status com_reset_1_res; + Status register_modify_1_res; + } result; + bool_t retval; + xdrproc_t _xdr_argument, _xdr_result; + bool_t (*local)(char *, void *, struct svc_req *); + + switch (rqstp->rq_proc) { + case NULLPROC: + (void) svc_sendreply (transp, (xdrproc_t) xdr_void, (char *)NULL); + return; + + case REGISTER_READ: + _xdr_argument = (xdrproc_t) xdr_register_read_1_argument; + _xdr_result = (xdrproc_t) xdr_RetVal; + local = (bool_t (*) (char *, void *, struct svc_req *))_register_read_1; + break; + + case REGISTER_READ_MEM: + _xdr_argument = (xdrproc_t) xdr_register_read_mem_1_argument; + _xdr_result = (xdrproc_t) xdr_RetVal; + local = (bool_t (*) (char *, void *, struct svc_req *))_register_read_mem_1; + break; + + case REGISTER_WRITE: + _xdr_argument = (xdrproc_t) xdr_register_write_1_argument; + _xdr_result = (xdrproc_t) xdr_Status; + local = (bool_t (*) (char *, void *, struct svc_req *))_register_write_1; + break; + + case REGISTER_WRITE_MEM: + _xdr_argument = (xdrproc_t) xdr_register_write_mem_1_argument; + _xdr_result = (xdrproc_t) xdr_Status; + local = (bool_t (*) (char *, void *, struct svc_req *))_register_write_mem_1; + break; + + case READ_UID: + _xdr_argument = (xdrproc_t) xdr_read_uid_1_argument; + _xdr_result = (xdrproc_t) xdr_RetVal; + local = (bool_t (*) (char *, void *, struct svc_req *))_read_uid_1; + break; + + case SET_ADDRESS: + _xdr_argument = (xdrproc_t) xdr_set_address_1_argument; + _xdr_result = (xdrproc_t) xdr_Status; + local = (bool_t (*) (char *, void *, struct svc_req *))_set_address_1; + break; + + case SEND_TRIGGER: + _xdr_argument = (xdrproc_t) xdr_send_trigger_1_argument; + _xdr_result = (xdrproc_t) xdr_Status; + local = (bool_t (*) (char *, void *, struct svc_req *))_send_trigger_1; + break; + + case IPU_DATA_READ: + _xdr_argument = (xdrproc_t) xdr_ipu_data_read_1_argument; + _xdr_result = (xdrproc_t) xdr_RetVal; + local = (bool_t (*) (char *, void *, struct svc_req *))_ipu_data_read_1; + break; + + case FPGA_REGISTER_READ: + _xdr_argument = (xdrproc_t) xdr_uint16_t; + _xdr_result = (xdrproc_t) xdr_RetVal; + local = (bool_t (*) (char *, void *, struct svc_req *))_fpga_register_read_1; + break; + + case FPGA_REGISTER_WRITE: + _xdr_argument = (xdrproc_t) xdr_fpga_register_write_1_argument; + _xdr_result = (xdrproc_t) xdr_Status; + local = (bool_t (*) (char *, void *, struct svc_req *))_fpga_register_write_1; + break; + + case TRB_FIFO_FLUSH: + _xdr_argument = (xdrproc_t) xdr_uint8_t; + _xdr_result = (xdrproc_t) xdr_Status; + local = (bool_t (*) (char *, void *, struct svc_req *))_trb_fifo_flush_1; + break; + + case NETWORK_RESET: + _xdr_argument = (xdrproc_t) xdr_void; + _xdr_result = (xdrproc_t) xdr_Status; + local = (bool_t (*) (char *, void *, struct svc_req *))_network_reset_1; + break; + + case COM_RESET: + _xdr_argument = (xdrproc_t) xdr_void; + _xdr_result = (xdrproc_t) xdr_Status; + local = (bool_t (*) (char *, void *, struct svc_req *))_com_reset_1; + break; + + case REGISTER_MODIFY: + _xdr_argument = (xdrproc_t) xdr_register_modify_1_argument; + _xdr_result = (xdrproc_t) xdr_Status; + local = (bool_t (*) (char *, void *, struct svc_req *))_register_modify_1; + break; + + default: + svcerr_noproc (transp); + return; + } + memset ((char *)&argument, 0, sizeof (argument)); + if (!svc_getargs (transp, (xdrproc_t) _xdr_argument, (caddr_t) &argument)) { + svcerr_decode (transp); + return; + } + retval = (bool_t) (*local)((char *)&argument, (void *)&result, rqstp); + if (retval > 0 && !svc_sendreply(transp, (xdrproc_t) _xdr_result, (char *)&result)) { + svcerr_systemerr (transp); + } + if (!svc_freeargs (transp, (xdrproc_t) _xdr_argument, (caddr_t) &argument)) { + fprintf (stderr, "%s", "unable to free arguments"); + exit (1); + } + if (!trbnetrpcprog_1_freeresult (transp, _xdr_result, (caddr_t) &result)) + fprintf (stderr, "%s", "unable to free results"); + + return; +} + +int +main (int argc, char **argv) +{ + register SVCXPRT *transp; + + pmap_unset (TRBNETRPCPROG, TRBNETRPCVERS); + + transp = svcudp_create(RPC_ANYSOCK); + if (transp == NULL) { + fprintf (stderr, "%s", "cannot create udp service."); + exit(1); + } + if (!svc_register(transp, TRBNETRPCPROG, TRBNETRPCVERS, trbnetrpcprog_1, IPPROTO_UDP)) { + fprintf (stderr, "%s", "unable to register (TRBNETRPCPROG, TRBNETRPCVERS, udp)."); + exit(1); + } + + transp = svctcp_create(RPC_ANYSOCK, 0, 0); + if (transp == NULL) { + fprintf (stderr, "%s", "cannot create tcp service."); + exit(1); + } + if (!svc_register(transp, TRBNETRPCPROG, TRBNETRPCVERS, trbnetrpcprog_1, IPPROTO_TCP)) { + fprintf (stderr, "%s", "unable to register (TRBNETRPCPROG, TRBNETRPCVERS, tcp)."); + exit(1); + } + + svc_run (); + fprintf (stderr, "%s", "svc_run returned"); + exit (1); + /* NOTREACHED */ +} diff --git a/trbnetd/trbrpc_xdr.c b/trbnetd/trbrpc_xdr.c new file mode 100644 index 0000000..715e24a --- /dev/null +++ b/trbnetd/trbrpc_xdr.c @@ -0,0 +1,179 @@ +/* + * Please do not edit this file. + * It was generated using rpcgen. + */ + +#include "trbrpc.h" + +bool_t +xdr_Buffer (XDR *xdrs, Buffer *objp) +{ + register int32_t *buf; + + if (!xdr_array (xdrs, (char **)&objp->Buffer_val, (u_int *) &objp->Buffer_len, ~0, + sizeof (uint32_t), (xdrproc_t) xdr_uint32_t)) + return FALSE; + return TRUE; +} + +bool_t +xdr_Status (XDR *xdrs, Status *objp) +{ + register int32_t *buf; + + if (!xdr_int (xdrs, &objp->trb_errno)) + return FALSE; + if (!xdr_uint16_t (xdrs, &objp->status_common)) + return FALSE; + if (!xdr_uint16_t (xdrs, &objp->status_channel)) + return FALSE; + if (!xdr_uint16_t (xdrs, &objp->sequence)) + return FALSE; + if (!xdr_uint8_t (xdrs, &objp->channel)) + return FALSE; + return TRUE; +} + +bool_t +xdr_RetVal (XDR *xdrs, RetVal *objp) +{ + register int32_t *buf; + + if (!xdr_Status (xdrs, &objp->status)) + return FALSE; + if (!xdr_Buffer (xdrs, &objp->data)) + return FALSE; + return TRUE; +} + +bool_t +xdr_register_read_1_argument (XDR *xdrs, register_read_1_argument *objp) +{ + if (!xdr_uint16_t (xdrs, &objp->arg1)) + return FALSE; + if (!xdr_uint16_t (xdrs, &objp->arg2)) + return FALSE; + if (!xdr_u_int (xdrs, &objp->arg3)) + return FALSE; + return TRUE; +} + +bool_t +xdr_register_read_mem_1_argument (XDR *xdrs, register_read_mem_1_argument *objp) +{ + if (!xdr_uint16_t (xdrs, &objp->arg1)) + return FALSE; + if (!xdr_uint16_t (xdrs, &objp->arg2)) + return FALSE; + if (!xdr_uint8_t (xdrs, &objp->arg3)) + return FALSE; + if (!xdr_uint16_t (xdrs, &objp->arg4)) + return FALSE; + if (!xdr_u_int (xdrs, &objp->arg5)) + return FALSE; + return TRUE; +} + +bool_t +xdr_register_write_1_argument (XDR *xdrs, register_write_1_argument *objp) +{ + if (!xdr_uint16_t (xdrs, &objp->arg1)) + return FALSE; + if (!xdr_uint16_t (xdrs, &objp->arg2)) + return FALSE; + if (!xdr_uint32_t (xdrs, &objp->arg3)) + return FALSE; + return TRUE; +} + +bool_t +xdr_register_write_mem_1_argument (XDR *xdrs, register_write_mem_1_argument *objp) +{ + if (!xdr_uint16_t (xdrs, &objp->arg1)) + return FALSE; + if (!xdr_uint16_t (xdrs, &objp->arg2)) + return FALSE; + if (!xdr_uint8_t (xdrs, &objp->arg3)) + return FALSE; + if (!xdr_Buffer (xdrs, &objp->arg4)) + return FALSE; + return TRUE; +} + +bool_t +xdr_read_uid_1_argument (XDR *xdrs, read_uid_1_argument *objp) +{ + if (!xdr_uint16_t (xdrs, &objp->arg1)) + return FALSE; + if (!xdr_u_int (xdrs, &objp->arg2)) + return FALSE; + return TRUE; +} + +bool_t +xdr_set_address_1_argument (XDR *xdrs, set_address_1_argument *objp) +{ + if (!xdr_uint64_t (xdrs, &objp->arg1)) + return FALSE; + if (!xdr_uint8_t (xdrs, &objp->arg2)) + return FALSE; + if (!xdr_uint16_t (xdrs, &objp->arg3)) + return FALSE; + return TRUE; +} + +bool_t +xdr_send_trigger_1_argument (XDR *xdrs, send_trigger_1_argument *objp) +{ + if (!xdr_uint8_t (xdrs, &objp->arg1)) + return FALSE; + if (!xdr_uint32_t (xdrs, &objp->arg2)) + return FALSE; + if (!xdr_uint8_t (xdrs, &objp->arg3)) + return FALSE; + if (!xdr_uint16_t (xdrs, &objp->arg4)) + return FALSE; + return TRUE; +} + +bool_t +xdr_ipu_data_read_1_argument (XDR *xdrs, ipu_data_read_1_argument *objp) +{ + if (!xdr_uint8_t (xdrs, &objp->arg1)) + return FALSE; + if (!xdr_uint32_t (xdrs, &objp->arg2)) + return FALSE; + if (!xdr_uint8_t (xdrs, &objp->arg3)) + return FALSE; + if (!xdr_uint16_t (xdrs, &objp->arg4)) + return FALSE; + if (!xdr_u_int (xdrs, &objp->arg5)) + return FALSE; + return TRUE; +} + +bool_t +xdr_fpga_register_write_1_argument (XDR *xdrs, fpga_register_write_1_argument *objp) +{ + if (!xdr_uint16_t (xdrs, &objp->arg1)) + return FALSE; + if (!xdr_uint32_t (xdrs, &objp->arg2)) + return FALSE; + return TRUE; +} + +bool_t +xdr_register_modify_1_argument (XDR *xdrs, register_modify_1_argument *objp) +{ + if (!xdr_uint16_t (xdrs, &objp->arg1)) + return FALSE; + if (!xdr_uint16_t (xdrs, &objp->arg2)) + return FALSE; + if (!xdr_int (xdrs, &objp->arg3)) + return FALSE; + if (!xdr_uint32_t (xdrs, &objp->arg4)) + return FALSE; + if (!xdr_uint32_t (xdrs, &objp->arg5)) + return FALSE; + return TRUE; +} -- 2.43.0