From 0f5947ee393ead640a330b8622368f1c9fbfd6a9 Mon Sep 17 00:00:00 2001 From: Benjamin Krill Date: Thu, 5 Dec 2013 11:09:23 +0100 Subject: [PATCH] add regfile access utility --- linux/apps/Makefile | 10 +++- linux/apps/strm_regfile.c | 112 ++++++++++++++++++++++++++++++++++++++ linux/libstrm/strm.h | 6 ++ 3 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 linux/apps/strm_regfile.c diff --git a/linux/apps/Makefile b/linux/apps/Makefile index 917e219..f7dfc26 100644 --- a/linux/apps/Makefile +++ b/linux/apps/Makefile @@ -8,10 +8,18 @@ BIN_STRM_DDR2 = strm_ddr2 SRC_STRM_DDR2 = strm_ddr2.c OBJ_STRM_DDR2 = $(SRC_STRM_DDR2:%.c=%.o) -all: ${BIN_STRM_DDR2} +BIN_STRM_REGFILE = strm_regfile +SRC_STRM_REGFILE = strm_regfile.c +OBJ_STRM_REGFILE = $(SRC_STRM_REGFILE:%.c=%.o) + +all: ${BIN_STRM_DDR2} ${BIN_STRM_REGFILE} ${BIN_STRM_DDR2}: ${OBJ_STRM_DDR2} ${LD} ${LDFLAGS} $^ -o $@ ${LDFLAGS_POST} +${BIN_STRM_REGFILE}: ${OBJ_STRM_REGFILE} + ${LD} ${LDFLAGS} $^ -o $@ ${LDFLAGS_POST} + + clean: rm -f *.o ${BIN_STRM_DDR2} diff --git a/linux/apps/strm_regfile.c b/linux/apps/strm_regfile.c new file mode 100644 index 0000000..8d5436d --- /dev/null +++ b/linux/apps/strm_regfile.c @@ -0,0 +1,112 @@ +/* ***************************************************************************** + * Copyright (c) 2013 Benjamin Krill + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * ****************************************************************************/ +#include +#include +#include +#include +#include +#include +#include + +static void +show_usage(char *name) +{ + printf("USAGE: %s []\n" + "\treg_address: address of the register\n" + "\tvalue: value to write, when not set register will be read\n" + , name); + return; +} + +int +main(int argc, char **argv) +{ + uint32_t *buf, value; + int address, write = 0; + int ret = -1; + usb_dev_handle *hd; + + if (argc < 2 || argc > 3) { + show_usage(argv[0]); + exit(1); + } + + /* check for */ + if (argc == 2) + write = 1; + + /* init usb connection */ + if ((hd = strm_init(0x04b4, 0x2342)) == NULL) { + perror("cannot init usb2strm interface"); + exit(1); + } + + address = strtoull(argv[1], NULL, 0); + + buf = (uint32_t*)malloc(4+8); + if (buf == NULL) { + perror("cannot allocate memory\n"); + goto out; + } + + if (write == 1) { + value = strtoull(argv[2], NULL, 0); + + buf[0] = STRM_TYPE_REGFILE << 28 | 2; + buf[1] = STRM_REGFILE_WRITE_HDR_INIT(address >> 2); + buf[2] = value; + + printf("address=0x%x value=0x%04x - ", address, value); + + if ((ret = usb_bulk_write(hd, 2, (const char *)buf, 4+8, 1000)) < 0) { + fprintf(stderr, "\nFAILED: write bulk transfer (%d) [%s].\n\t%s\n", ret, + __func__, usb_strerror()); + } + + } else { + + buf[0] = STRM_TYPE_REGFILE << 28 | 1; + buf[1] = STRM_DDR2_READ_HDR_INIT(address >> 2); + + printf("address=0x%x ", address); + + if ((ret = usb_bulk_write(hd, 2, (const char *)buf, 8, 1000)) < 0) { + fprintf(stderr, "\nFAILED: write bulk transfer (%d) [%s].\n\t%s\n", ret, + __func__, usb_strerror()); + goto free; + } + + if ((ret = usb_bulk_read(hd, 6, (char *)buf, 4+4, 6000000)) < 0) { + fprintf(stderr, "\nFAILED: read bulk transfer (%d) [%s].\n\t%s\n", ret, + __func__, usb_strerror()); + goto free; + } + + printf("value=0x%04x\n", buf[1]); + } + +free: + free(buf); +out: + strm_fini(); + return 0; +} diff --git a/linux/libstrm/strm.h b/linux/libstrm/strm.h index dbbad27..5ed34b9 100644 --- a/linux/libstrm/strm.h +++ b/linux/libstrm/strm.h @@ -31,6 +31,12 @@ #define STRM_DDR2_READ_HDR_INIT(addr) \ (uint32_t)(~(0x1 << 31) & (addr & 0x0fffffff)) +#define STRM_TYPE_REGFILE 0x2 +#define STRM_REGFILE_WRITE_HDR_INIT(addr) \ + (uint32_t)(0x80000000 | (addr & 0x0fffffff)) +#define STRM_REGFILE_READ_HDR_INIT(addr) \ + (uint32_t)(~(0x1 << 31) & (addr & 0x0fffffff)) + /* prototypes */ usb_dev_handle *strm_init(int vendor, int product); int strm_fini(void);