From 7f97ea07417de4c2ea260e2860e589011c732e04 Mon Sep 17 00:00:00 2001 From: "Suren A. Chilingaryan" Date: Tue, 8 Mar 2011 22:46:14 +0100 Subject: Initial support of IPECamera protocol --- cli.c | 198 +++++++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 118 insertions(+), 80 deletions(-) (limited to 'cli.c') diff --git a/cli.c b/cli.c index adf0f4b..2215c55 100644 --- a/cli.c +++ b/cli.c @@ -1,37 +1,25 @@ -/******************************************************************* - * This is a test program for the IOctl interface of the - * pciDriver. - * - * $Revision: 1.3 $ - * $Date: 2006-11-17 18:49:01 $ - * - *******************************************************************/ - -/******************************************************************* - * Change History: - * - * $Log: not supported by cvs2svn $ - * Revision 1.2 2006/10/16 16:56:09 marcus - * Added nice comment at the start. - * - *******************************************************************/ +#define _POSIX_C_SOURCE 200112L #include -#include #include +#include +#include #include #include #include #include +#include #include #include #include #include +#include #include #include "pci.h" #include "ipecamera.h" +#include "tools.h" /* defines */ @@ -48,7 +36,9 @@ #define BLOCK_SIZE 8 #define BENCHMARK_ITERATIONS 128 -//#define FILE_IO + + +typedef uint8_t access_t; typedef enum { MODE_INVALID, @@ -66,6 +56,7 @@ typedef enum { OPT_MODEL = 'm', OPT_BAR = 'b', OPT_ACCESS = 'a', + OPT_ENDIANESS = 'e', OPT_SIZE = 's', OPT_INFO = 'i', OPT_BENCHMARK = 'p', @@ -80,6 +71,7 @@ static struct option long_options[] = { {"model", required_argument, 0, OPT_MODEL }, {"bar", required_argument, 0, OPT_BAR }, {"access", required_argument, 0, OPT_ACCESS }, + {"endianess", required_argument, 0, OPT_ENDIANESS }, {"size", required_argument, 0, OPT_SIZE }, {"info", no_argument, 0, OPT_INFO }, {"list", no_argument, 0, OPT_LIST }, @@ -118,14 +110,15 @@ void Usage(int argc, char *argv[], const char *format, ...) { "\n" " Addressing:\n" " -d - FPGA device (/dev/fpga0)\n" -" -m - Memory model\n" -" pci - Plain (default)\n" +" -m - Memory model (autodetected)\n" +" pci - Plain\n" " ipecamera - IPE Camera\n" -" -b - Data bank (autodetected)\n" +" -b - Data/Register bank (autodetected)\n" "\n" " Options:\n" " -s - Number of words (default: 1)\n" " -a - Bits per word (default: 32)\n" +" -e - Endianess Little/Big (default: host)\n" "\n\n", argv[0]); @@ -146,9 +139,9 @@ void Error(const char *format, ...) { } -void List(int handle, pcilib_model_t model) { +void List(pcilib_t *handle, pcilib_model_t model) { int i; - pcilib_register_t *registers; + pcilib_register_description_t *registers; const pci_board_info *board_info = pcilib_get_board_info(handle); @@ -171,18 +164,18 @@ void List(int handle, pcilib_model_t model) { } printf("\n"); - registers = pcilib_model_description[model].registers; + registers = pcilib_model[model].registers; if (registers) { printf("Registers: \n"); - for (i = 0; registers[i].size; i++) { + for (i = 0; registers[i].bits; i++) { const char *mode; if (registers[i].mode == PCILIB_REGISTER_RW) mode = "RW"; else if (registers[i].mode == PCILIB_REGISTER_R) mode = "R "; else if (registers[i].mode == PCILIB_REGISTER_W) mode = " W"; else mode = " "; - printf(" 0x%02x (%2i %s) %s", registers[i].id, registers[i].size, mode, registers[i].name); + printf(" 0x%02x (%2i %s) %s", registers[i].addr, registers[i].bits, mode, registers[i].name); if ((registers[i].description)&&(registers[i].description[0])) { printf(": %s", registers[i].description); } @@ -193,7 +186,7 @@ void List(int handle, pcilib_model_t model) { } } -void Info(int handle, pcilib_model_t model) { +void Info(pcilib_t *handle, pcilib_model_t model) { const pci_board_info *board_info = pcilib_get_board_info(handle); printf("Vendor: %x, Device: %x, Interrupt Pin: %i, Interrupt Line: %i\n", board_info->vendor_id, board_info->device_id, board_info->interrupt_pin, board_info->interrupt_line); @@ -201,7 +194,7 @@ void Info(int handle, pcilib_model_t model) { } -int Benchmark(int handle, int bar) { +int Benchmark(pcilib_t *handle, pcilib_bar_t bar) { int err; int i, errors; void *data, *buf, *check; @@ -262,7 +255,7 @@ int Benchmark(int handle, int bar) { for (size = 4 ; size < max_size; size *= 8) { gettimeofday(&start,NULL); for (i = 0; i < BENCHMARK_ITERATIONS; i++) { - pcilib_read(buf, handle, bar, 0, size); + pcilib_read(handle, bar, 0, size, buf); } gettimeofday(&end,NULL); @@ -273,7 +266,7 @@ int Benchmark(int handle, int bar) { gettimeofday(&start,NULL); for (i = 0; i < BENCHMARK_ITERATIONS; i++) { - pcilib_write(buf, handle, bar, 0, size); + pcilib_write(handle, bar, 0, size, buf); } gettimeofday(&end,NULL); @@ -282,8 +275,8 @@ int Benchmark(int handle, int bar) { gettimeofday(&start,NULL); for (i = 0, errors = 0; i < BENCHMARK_ITERATIONS; i++) { - pcilib_write(buf, handle, bar, 0, size); - pcilib_read(check, handle, bar, 0, size); + pcilib_write(handle, bar, 0, size, buf); + pcilib_read(handle, bar, 0, size, check); if (memcmp(buf, check, size)) ++errors; } gettimeofday(&end,NULL); @@ -300,8 +293,10 @@ int Benchmark(int handle, int bar) { free(buf); } +#define pci2host16(endianess, value) endianess? + -int ReadData(int handle, int bar, unsigned long addr, int n, int access) { +int ReadData(pcilib_t *handle, pcilib_bar_t bar, uintptr_t addr, size_t n, access_t access, int endianess) { void *buf; int i, err; int size = n * abs(access); @@ -317,10 +312,10 @@ int ReadData(int handle, int bar, unsigned long addr, int n, int access) { // buf = alloca(size); err = posix_memalign( (void**)&buf, 256, size ); - if ((err)||(!buf)) Error("Allocation of %i bytes of memory have failed", size); - pcilib_read(buf, handle, bar, addr, size); + pcilib_read(handle, bar, addr, size, buf); + if (endianess) pcilib_swap(buf, buf, abs(access), n); for (i = 0; i < n; i++) { if (i) { @@ -346,17 +341,26 @@ int ReadData(int handle, int bar, unsigned long addr, int n, int access) { free(buf); } -int ReadRegister(int handle, pcilib_model_t model, const char *reg) { +int ReadRegister(pcilib_t *handle, pcilib_model_t model, const char *reg) { + int err; int i; + + pcilib_register_value_t value; - if (!reg) { - pcilib_register_t *registers = pcilib_model_description[model].registers; + if (reg) { + err = pcilib_read_register(handle, reg, &value); + if (err) printf("Error reading register %s\n", reg); + else printf("%s = %i\n", reg, value); + } else { + pcilib_register_description_t *registers = pcilib_model[model].registers; if (registers) { printf("Registers:\n"); - for (i = 0; registers[i].size; i++) { + for (i = 0; registers[i].bits; i++) { if (registers[i].mode & PCILIB_REGISTER_R) { - printf(" %s = %i [%i]", registers[i].name, 0, registers[i].defvalue); + err = pcilib_read_register_by_id(handle, i, &value); + if (err) printf(" %s = error reading value [%i]", registers[i].name, registers[i].defvalue); + else printf(" %s = %i [%i]", registers[i].name, value, registers[i].defvalue); } printf("\n"); } @@ -367,20 +371,17 @@ int ReadRegister(int handle, pcilib_model_t model, const char *reg) { } } -int ReadRegisterRange(int handle, pcilib_model_t model, int bar, unsigned long addr, int n, int access) { +int ReadRegisterRange(pcilib_t *handle, pcilib_model_t model, uintptr_t addr, size_t n) { } -int WriteData(int handle, int bar, unsigned long addr, int n, int access, char ** data) { +int WriteData(pcilib_t *handle, pcilib_bar_t bar, uintptr_t addr, size_t n, access_t access, int endianess, char ** data) { void *buf, *check; int res, i, err; int size = n * abs(access); - + err = posix_memalign( (void**)&buf, 256, size ); - if (!err) { - err = posix_memalign( (void**)&check, 256, size ); - } - if ((err)||(!buf)||(!check)) - Error("Allocation of %i bytes of memory have failed", size); + if (!err) err = posix_memalign( (void**)&check, 256, size ); + if ((err)||(!buf)||(!check)) Error("Allocation of %i bytes of memory have failed", size); for (i = 0; i < n; i++) { switch (access) { @@ -393,12 +394,14 @@ int WriteData(int handle, int bar, unsigned long addr, int n, int access, char * if (res != 1) Error("Can't parse data value at poition %i, (%s) is not valid hex number", i, data[i]); } - pcilib_write(buf, handle, bar, addr, size); - pcilib_read(check, handle, bar, addr, size); + if (endianess) pcilib_swap(buf, buf, abs(access), n); + pcilib_write(handle, bar, addr, size, buf); + pcilib_read(handle, bar, addr, size, check); + if (endianess) pcilib_swap(check, check, abs(access), n); if (memcmp(buf, check, size)) { printf("Write failed: the data written and read differ, the foolowing is read back:\n"); - ReadData(handle, bar, addr, n, access); + ReadData(handle, bar, addr, n, access, endianess); exit(-1); } @@ -406,32 +409,58 @@ int WriteData(int handle, int bar, unsigned long addr, int n, int access, char * free(buf); } -int WriteRegisterRange(int handle, pcilib_model_t model, int bar, unsigned long addr, int n, int access, char ** data) { +int WriteRegisterRange(pcilib_t *handle, pcilib_model_t model, uintptr_t addr, size_t n, char ** data) { } -int WriteRegister(int handle, pcilib_model_t model, const char *reg, char ** data) { +int WriteRegister(pcilib_t *handle, pcilib_model_t model, const char *reg, char ** data) { + int err; + int i; + + unsigned long val; + pcilib_register_value_t value; + + if (sscanf(*data, "%li", &val) != 1) { + Error("Can't parse data value (%s) is not valid decimal number", data[i]); + } + + value = val; + + err = pcilib_write_register(handle, reg, value); + if (err) Error("Error writting register %s\n", reg); + + err = pcilib_read_register(handle, reg, &value); + if (err) Error("Error reading back register %s for verification\n", reg); + + if (val != value) { + Error("Failed to write register %s: %lu is written and %lu is read back", reg, val, value); + } else { + printf("%s = %i\n", reg, value); + } + + return 0; } int main(int argc, char **argv) { + int i; + long itmp; unsigned char c; - pcilib_model_t model = (pcilib_model_t)-1; + pcilib_model_t model = PCILIB_MODEL_DETECT; MODE mode = MODE_INVALID; const char *fpga_device = DEFAULT_FPGA_DEVICE; - int bar = -1; + pcilib_bar_t bar = PCILIB_BAR_DETECT; const char *addr = NULL; - unsigned long start = -1; - int size = 1; - int access = 4; + const char *reg = NULL; + uintptr_t start = -1; + size_t size = 1; + access_t access = 4; int skip = 0; + int endianess = 0; - int i; - int handle; + pcilib_t *handle; - const char *reg = NULL; - - while ((c = getopt_long(argc, argv, "hilpr::w::d:b:a:s:", long_options, NULL)) != (unsigned char)-1) { + while ((c = getopt_long(argc, argv, "hilpr::w::d:m:b:a:s:e:", long_options, NULL)) != (unsigned char)-1) { extern int optind; switch (c) { case OPT_HELP: @@ -475,11 +504,12 @@ int main(int argc, char **argv) { else Usage(argc, argv, "Invalid memory model (%s) is specified", optarg);\ break; case OPT_BAR: - if ((sscanf(optarg,"%u", &bar) != 1)||(bar < 0)||(bar >= PCILIB_MAX_BANKS)) Usage(argc, argv, "Invalid data bank (%s) is specified", optarg); + if ((sscanf(optarg,"%li", &itmp) != 1)||(itmp < 0)||(itmp >= PCILIB_MAX_BANKS)) Usage(argc, argv, "Invalid data bank (%s) is specified", optarg); + else bar = itmp; break; case OPT_ACCESS: - if (sscanf(optarg, "%i", &access) != 1) access = 0; - switch (access) { + if (sscanf(optarg, "%li", &itmp) != 1) access = 0; + switch (itmp) { case 8: access = 1; break; case 16: access = 2; break; case 32: access = 4; break; @@ -488,9 +518,19 @@ int main(int argc, char **argv) { } break; case OPT_SIZE: - if (sscanf(optarg, "%u", &size) != 1) + if (sscanf(optarg, "%zu", &size) != 1) Usage(argc, argv, "Invalid size is specified (%s)", optarg); break; + case OPT_ENDIANESS: + if ((*optarg == 'b')||(*optarg == 'B')) { + if (ntohs(1) == 1) endianess = 0; + else endianess = 1; + } else if ((*optarg == 'l')||(*optarg == 'L')) { + if (ntohs(1) == 1) endianess = 1; + else endianess = 0; + } else Usage(argc, argv, "Invalid endianess is specified (%s)", optarg); + + break; default: Usage(argc, argv, "Unknown option (%s)", argv[optind]); } @@ -503,12 +543,10 @@ int main(int argc, char **argv) { pcilib_set_error_handler(&Error); - handle = pcilib_open(fpga_device); + handle = pcilib_open(fpga_device, model); if (handle < 0) Error("Failed to open FPGA device: %s", fpga_device); - if (model == (pcilib_model_t)-1) { - model = pcilib_detect_model(handle); - } + model = pcilib_get_model(handle); switch (mode) { case MODE_WRITE: @@ -530,7 +568,7 @@ int main(int argc, char **argv) { if (addr) { if (sscanf(addr, "%lx", &start) == 1) { // check if the address in the register range - pcilib_register_range_t *ranges = pcilib_model_description[model].ranges; + pcilib_register_range_t *ranges = pcilib_model[model].ranges; for (i = 0; ranges[i].start != ranges[i].end; i++) if ((start >= ranges[i].start)&&(start <= ranges[i].end)) break; @@ -538,11 +576,11 @@ int main(int argc, char **argv) { // register access in plain mode if (ranges[i].start != ranges[i].end) ++mode; } else { - if (pcilib_find_register(model, addr) >= 0) { + if (pcilib_find_register(handle, addr) == PCILIB_REGISTER_INVALID) { + Usage(argc, argv, "Invalid address (%s) is specified", addr); + } else { reg = addr; ++mode; - } else { - Usage(argc, argv, "Invalid address (%s) is specified", addr); } } } @@ -560,21 +598,21 @@ int main(int argc, char **argv) { break; case MODE_READ: if (addr) { - ReadData(handle, bar, start, size, access); + ReadData(handle, bar, start, size, access, endianess); } else { Error("Address to read is not specified"); } break; case MODE_READ_REGISTER: if ((reg)||(!addr)) ReadRegister(handle, model, reg); - else ReadRegisterRange(handle, model, bar, start, size, access); + else ReadRegisterRange(handle, model, start, size); break; case MODE_WRITE: - WriteData(handle, bar, start, size, access, argv + optind); + WriteData(handle, bar, start, size, access, endianess, argv + optind); break; case MODE_WRITE_REGISTER: if (reg) WriteRegister(handle, model, reg, argv + optind); - else WriteRegisterRange(handle, model, bar, start, size, access, argv + optind); + else WriteRegisterRange(handle, model, start, size, argv + optind); break; } -- cgit v1.2.3 From 1f39476f289cd6ac1f45da7f0d9b528b4bc9a978 Mon Sep 17 00:00:00 2001 From: "Suren A. Chilingaryan" Date: Wed, 9 Mar 2011 01:45:29 +0100 Subject: Better handling of register banks --- cli.c | 106 +++++++++++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 83 insertions(+), 23 deletions(-) (limited to 'cli.c') diff --git a/cli.c b/cli.c index 2215c55..3540239 100644 --- a/cli.c +++ b/cli.c @@ -139,8 +139,9 @@ void Error(const char *format, ...) { } -void List(pcilib_t *handle, pcilib_model_t model) { +void List(pcilib_t *handle, pcilib_model_t model, const char *bank) { int i; + pcilib_register_bank_description_t *banks; pcilib_register_description_t *registers; const pci_board_info *board_info = pcilib_get_board_info(handle); @@ -163,13 +164,43 @@ void List(pcilib_t *handle, pcilib_model_t model) { } } printf("\n"); + + if ((bank)&&(bank != (char*)-1)) banks = NULL; + else banks = pcilib_model[model].banks; + + if (banks) { + printf("Banks: \n"); + for (i = 0; banks[i].access; i++) { + printf(" 0x%02x %s", banks[i].addr, banks[i].name); + if ((banks[i].description)&&(banks[i].description[0])) { + printf(": %s", banks[i].description); + } + printf("\n"); + } + printf("\n"); + } - registers = pcilib_model[model].registers; + if (bank == (char*)-1) registers = NULL; + else registers = pcilib_model[model].registers; if (registers) { - printf("Registers: \n"); + pcilib_register_bank_addr_t bank_addr; + if (bank) { + pcilib_register_bank_t bank_id = pcilib_find_bank(handle, bank); + pcilib_register_bank_description_t *b = pcilib_model[model].banks + bank_id; + + bank_addr = b->addr; + if (b->description) printf("%s:\n", b->description); + else if (b->name) printf("Registers of bank %s:\n", b->name); + else printf("Registers of bank 0x%x:\n", b->addr); + } else { + printf("Registers: \n"); + } for (i = 0; registers[i].bits; i++) { const char *mode; + + if ((bank)&&(registers[i].bank != bank_addr)) continue; + if (registers[i].mode == PCILIB_REGISTER_RW) mode = "RW"; else if (registers[i].mode == PCILIB_REGISTER_R) mode = "R "; else if (registers[i].mode == PCILIB_REGISTER_W) mode = " W"; @@ -190,7 +221,7 @@ void Info(pcilib_t *handle, pcilib_model_t model) { const pci_board_info *board_info = pcilib_get_board_info(handle); printf("Vendor: %x, Device: %x, Interrupt Pin: %i, Interrupt Line: %i\n", board_info->vendor_id, board_info->device_id, board_info->interrupt_pin, board_info->interrupt_line); - List(handle, model); + List(handle, model, (char*)-1); } @@ -341,23 +372,31 @@ int ReadData(pcilib_t *handle, pcilib_bar_t bar, uintptr_t addr, size_t n, acces free(buf); } -int ReadRegister(pcilib_t *handle, pcilib_model_t model, const char *reg) { +int ReadRegister(pcilib_t *handle, pcilib_model_t model, const char *bank, const char *reg) { int err; int i; pcilib_register_value_t value; if (reg) { - err = pcilib_read_register(handle, reg, &value); + err = pcilib_read_register(handle, bank, reg, &value); if (err) printf("Error reading register %s\n", reg); else printf("%s = %i\n", reg, value); } else { - pcilib_register_description_t *registers = pcilib_model[model].registers; + pcilib_register_bank_t bank_id; + pcilib_register_bank_addr_t bank_addr; + + pcilib_register_description_t *registers = pcilib_model[model].registers; if (registers) { + if (bank) { + bank_id = pcilib_find_bank(handle, bank); + bank_addr = pcilib_model[model].banks[bank_id].addr; + } + printf("Registers:\n"); for (i = 0; registers[i].bits; i++) { - if (registers[i].mode & PCILIB_REGISTER_R) { + if ((registers[i].mode & PCILIB_REGISTER_R)&&((!bank)||(registers[i].bank == bank_addr))) { err = pcilib_read_register_by_id(handle, i, &value); if (err) printf(" %s = error reading value [%i]", registers[i].name, registers[i].defvalue); else printf(" %s = %i [%i]", registers[i].name, value, registers[i].defvalue); @@ -371,7 +410,13 @@ int ReadRegister(pcilib_t *handle, pcilib_model_t model, const char *reg) { } } -int ReadRegisterRange(pcilib_t *handle, pcilib_model_t model, uintptr_t addr, size_t n) { +int ReadRegisterRange(pcilib_t *handle, pcilib_model_t model, const char *bank, uintptr_t addr, size_t n) { +/* int err; + + pcilib_register_bank_t bank = pcilib_find_bank(handle, bank_addr); + pcilib_register_value_t buf[n]; + + err = pcilib_read_register_space(handle, bank, addr, n, buf);*/ } int WriteData(pcilib_t *handle, pcilib_bar_t bar, uintptr_t addr, size_t n, access_t access, int endianess, char ** data) { @@ -409,10 +454,10 @@ int WriteData(pcilib_t *handle, pcilib_bar_t bar, uintptr_t addr, size_t n, acce free(buf); } -int WriteRegisterRange(pcilib_t *handle, pcilib_model_t model, uintptr_t addr, size_t n, char ** data) { +int WriteRegisterRange(pcilib_t *handle, pcilib_model_t model, const char *bank, uintptr_t addr, size_t n, char ** data) { } -int WriteRegister(pcilib_t *handle, pcilib_model_t model, const char *reg, char ** data) { +int WriteRegister(pcilib_t *handle, pcilib_model_t model, const char *bank, const char *reg, char ** data) { int err; int i; @@ -425,10 +470,10 @@ int WriteRegister(pcilib_t *handle, pcilib_model_t model, const char *reg, char value = val; - err = pcilib_write_register(handle, reg, value); + err = pcilib_write_register(handle, bank, reg, value); if (err) Error("Error writting register %s\n", reg); - err = pcilib_read_register(handle, reg, &value); + err = pcilib_read_register(handle, bank, reg, &value); if (err) Error("Error reading back register %s for verification\n", reg); if (val != value) { @@ -452,6 +497,7 @@ int main(int argc, char **argv) { pcilib_bar_t bar = PCILIB_BAR_DETECT; const char *addr = NULL; const char *reg = NULL; + const char *bank = NULL; uintptr_t start = -1; size_t size = 1; access_t access = 4; @@ -504,8 +550,9 @@ int main(int argc, char **argv) { else Usage(argc, argv, "Invalid memory model (%s) is specified", optarg);\ break; case OPT_BAR: - if ((sscanf(optarg,"%li", &itmp) != 1)||(itmp < 0)||(itmp >= PCILIB_MAX_BANKS)) Usage(argc, argv, "Invalid data bank (%s) is specified", optarg); - else bar = itmp; + bank = optarg; +// if ((sscanf(optarg,"%li", &itmp) != 1)||(itmp < 0)||(itmp >= PCILIB_MAX_BANKS)) Usage(argc, argv, "Invalid data bank (%s) is specified", optarg); +// else bar = itmp; break; case OPT_ACCESS: if (sscanf(optarg, "%li", &itmp) != 1) access = 0; @@ -563,7 +610,6 @@ int main(int argc, char **argv) { default: if (argc > optind) Usage(argc, argv, "Invalid non-option parameters are supplied"); } - if (addr) { if (sscanf(addr, "%lx", &start) == 1) { @@ -576,7 +622,7 @@ int main(int argc, char **argv) { // register access in plain mode if (ranges[i].start != ranges[i].end) ++mode; } else { - if (pcilib_find_register(handle, addr) == PCILIB_REGISTER_INVALID) { + if (pcilib_find_register(handle, bank, addr) == PCILIB_REGISTER_INVALID) { Usage(argc, argv, "Invalid address (%s) is specified", addr); } else { reg = addr; @@ -584,14 +630,28 @@ int main(int argc, char **argv) { } } } - + if (bank) { + switch (mode) { + case MODE_BENCHMARK: + case MODE_READ: + case MODE_WRITE: + if ((sscanf(bank,"%li", &itmp) != 1)||(itmp < 0)||(itmp >= PCILIB_MAX_BANKS)) + Usage(argc, argv, "Invalid data bank (%s) is specified", bank); + else bar = itmp; + break; + default: + if (pcilib_find_bank(handle, bank) == PCILIB_REGISTER_BANK_INVALID) + Usage(argc, argv, "Invalid data bank (%s) is specified", bank); + } + } + switch (mode) { case MODE_INFO: Info(handle, model); break; case MODE_LIST: - List(handle, model); + List(handle, model, bank); break; case MODE_BENCHMARK: Benchmark(handle, bar); @@ -604,15 +664,15 @@ int main(int argc, char **argv) { } break; case MODE_READ_REGISTER: - if ((reg)||(!addr)) ReadRegister(handle, model, reg); - else ReadRegisterRange(handle, model, start, size); + if ((reg)||(!addr)) ReadRegister(handle, model, bank, reg); + else ReadRegisterRange(handle, model, bank, start, size); break; case MODE_WRITE: WriteData(handle, bar, start, size, access, endianess, argv + optind); break; case MODE_WRITE_REGISTER: - if (reg) WriteRegister(handle, model, reg, argv + optind); - else WriteRegisterRange(handle, model, start, size, argv + optind); + if (reg) WriteRegister(handle, model, bank, reg, argv + optind); + else WriteRegisterRange(handle, model, bank, start, size, argv + optind); break; } -- cgit v1.2.3 From 934bcc133abf774947070492a5f31677c9400c0d Mon Sep 17 00:00:00 2001 From: "Suren A. Chilingaryan" Date: Wed, 9 Mar 2011 02:13:26 +0100 Subject: Fixes write verification failure if byte-swapping is in force --- cli.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'cli.c') diff --git a/cli.c b/cli.c index 3540239..8a78a7a 100644 --- a/cli.c +++ b/cli.c @@ -442,10 +442,10 @@ int WriteData(pcilib_t *handle, pcilib_bar_t bar, uintptr_t addr, size_t n, acce if (endianess) pcilib_swap(buf, buf, abs(access), n); pcilib_write(handle, bar, addr, size, buf); pcilib_read(handle, bar, addr, size, check); - if (endianess) pcilib_swap(check, check, abs(access), n); if (memcmp(buf, check, size)) { printf("Write failed: the data written and read differ, the foolowing is read back:\n"); + if (endianess) pcilib_swap(check, check, abs(access), n); ReadData(handle, bar, addr, n, access, endianess); exit(-1); } -- cgit v1.2.3 From b0596cb0f01f885153abffaecfa248920cb8658b Mon Sep 17 00:00:00 2001 From: "Suren A. Chilingaryan" Date: Wed, 9 Mar 2011 14:53:05 +0100 Subject: Support writting and reading of register ranges --- cli.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 69 insertions(+), 3 deletions(-) (limited to 'cli.c') diff --git a/cli.c b/cli.c index 8a78a7a..157558b 100644 --- a/cli.c +++ b/cli.c @@ -411,12 +411,48 @@ int ReadRegister(pcilib_t *handle, pcilib_model_t model, const char *bank, const } int ReadRegisterRange(pcilib_t *handle, pcilib_model_t model, const char *bank, uintptr_t addr, size_t n) { -/* int err; + int err; + int i; + + pcilib_register_bank_description_t *banks = pcilib_model[model].banks; + pcilib_register_bank_t bank_id = pcilib_find_bank(handle, bank); + + if (bank_id == PCILIB_REGISTER_BANK_INVALID) { + if (bank) Error("Invalid register bank is specified (%s)", bank); + else Error("Register bank should be specified"); + } + + int access = banks[bank_id].access / 8; + int size = n * abs(access); + int block_width, blocks_per_line; + int numbers_per_block, numbers_per_line; - pcilib_register_bank_t bank = pcilib_find_bank(handle, bank_addr); + numbers_per_block = BLOCK_SIZE / access; + + block_width = numbers_per_block * ((access * 2) + SEPARATOR_WIDTH); + blocks_per_line = (LINE_WIDTH - 6) / (block_width + BLOCK_SEPARATOR_WIDTH); + if ((blocks_per_line > 1)&&(blocks_per_line % 2)) --blocks_per_line; + numbers_per_line = blocks_per_line * numbers_per_block; + + pcilib_register_value_t buf[n]; + err = pcilib_read_register_space(handle, bank, addr, n, buf); + if (err) Error("Error reading register space for bank \"%s\" at address %lx, size %lu", bank?bank:"default", addr, n); + - err = pcilib_read_register_space(handle, bank, addr, n, buf);*/ + for (i = 0; i < n; i++) { + if (i) { + if (i%numbers_per_line == 0) printf("\n"); + else { + printf("%*s", SEPARATOR_WIDTH, ""); + if (i%numbers_per_block == 0) printf("%*s", BLOCK_SEPARATOR_WIDTH, ""); + } + } + + if (i%numbers_per_line == 0) printf("%4lx: ", addr + i); + printf("%0*lx", access * 2, buf[i]); + } + printf("\n\n"); } int WriteData(pcilib_t *handle, pcilib_bar_t bar, uintptr_t addr, size_t n, access_t access, int endianess, char ** data) { @@ -455,6 +491,36 @@ int WriteData(pcilib_t *handle, pcilib_bar_t bar, uintptr_t addr, size_t n, acce } int WriteRegisterRange(pcilib_t *handle, pcilib_model_t model, const char *bank, uintptr_t addr, size_t n, char ** data) { + pcilib_register_value_t *buf, *check; + int res, i, err; + unsigned long value; + int size = n * sizeof(pcilib_register_value_t); + + err = posix_memalign( (void**)&buf, 256, size ); + if (!err) err = posix_memalign( (void**)&check, 256, size ); + if ((err)||(!buf)||(!check)) Error("Allocation of %i bytes of memory have failed", size); + + for (i = 0; i < n; i++) { + res = sscanf(data[i], "%lx", &value); + if (res != 1) Error("Can't parse data value at poition %i, (%s) is not valid hex number", i, data[i]); + buf[i] = value; + } + + err = pcilib_write_register_space(handle, bank, addr, n, buf); + if (err) Error("Error writting register space for bank \"%s\" at address %lx, size %lu", bank?bank:"default", addr, n); + + err = pcilib_read_register_space(handle, bank, addr, n, check); + if (err) Error("Error reading register space for bank \"%s\" at address %lx, size %lu", bank?bank:"default", addr, n); + + if (memcmp(buf, check, size)) { + printf("Write failed: the data written and read differ, the foolowing is read back:\n"); + ReadRegisterRange(handle, model, bank, addr, n); + exit(-1); + } + + free(check); + free(buf); + } int WriteRegister(pcilib_t *handle, pcilib_model_t model, const char *bank, const char *reg, char ** data) { -- cgit v1.2.3 From a008b10d8488ef905a43de00ee5c8efd03b03ed6 Mon Sep 17 00:00:00 2001 From: "Suren A. Chilingaryan" Date: Wed, 9 Mar 2011 16:55:27 +0100 Subject: Support for FPGA registers --- cli.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'cli.c') diff --git a/cli.c b/cli.c index 157558b..ff1b754 100644 --- a/cli.c +++ b/cli.c @@ -36,6 +36,8 @@ #define BLOCK_SIZE 8 #define BENCHMARK_ITERATIONS 128 +#define isnumber pcilib_isnumber +#define isxnumber pcilib_isxnumber typedef uint8_t access_t; @@ -471,8 +473,7 @@ int WriteData(pcilib_t *handle, pcilib_bar_t bar, uintptr_t addr, size_t n, acce case 4: res = sscanf(data[i], "%x", ((uint32_t*)buf)+i); break; case 8: res = sscanf(data[i], "%lx", ((uint64_t*)buf)+i); break; } - - if (res != 1) Error("Can't parse data value at poition %i, (%s) is not valid hex number", i, data[i]); + if ((res != 1)||(!isxnumber(data[i]))) Error("Can't parse data value at poition %i, (%s) is not valid hex number", i, data[i]); } if (endianess) pcilib_swap(buf, buf, abs(access), n); @@ -502,7 +503,7 @@ int WriteRegisterRange(pcilib_t *handle, pcilib_model_t model, const char *bank, for (i = 0; i < n; i++) { res = sscanf(data[i], "%lx", &value); - if (res != 1) Error("Can't parse data value at poition %i, (%s) is not valid hex number", i, data[i]); + if ((res != 1)||(!isxnumber(data[i]))) Error("Can't parse data value at poition %i, (%s) is not valid hex number", i, data[i]); buf[i] = value; } @@ -530,7 +531,7 @@ int WriteRegister(pcilib_t *handle, pcilib_model_t model, const char *bank, cons unsigned long val; pcilib_register_value_t value; - if (sscanf(*data, "%li", &val) != 1) { + if ((!isnumber(*data))||(sscanf(*data, "%li", &val) != 1)) { Error("Can't parse data value (%s) is not valid decimal number", data[i]); } @@ -621,7 +622,7 @@ int main(int argc, char **argv) { // else bar = itmp; break; case OPT_ACCESS: - if (sscanf(optarg, "%li", &itmp) != 1) access = 0; + if ((!isnumber(optarg))||(sscanf(optarg, "%li", &itmp) != 1)) access = 0; switch (itmp) { case 8: access = 1; break; case 16: access = 2; break; @@ -631,7 +632,7 @@ int main(int argc, char **argv) { } break; case OPT_SIZE: - if (sscanf(optarg, "%zu", &size) != 1) + if ((!isnumber(optarg))||(sscanf(optarg, "%zu", &size) != 1)) Usage(argc, argv, "Invalid size is specified (%s)", optarg); break; case OPT_ENDIANESS: @@ -678,7 +679,7 @@ int main(int argc, char **argv) { } if (addr) { - if (sscanf(addr, "%lx", &start) == 1) { + if ((isxnumber(addr))&&(sscanf(addr, "%lx", &start) == 1)) { // check if the address in the register range pcilib_register_range_t *ranges = pcilib_model[model].ranges; @@ -702,7 +703,7 @@ int main(int argc, char **argv) { case MODE_BENCHMARK: case MODE_READ: case MODE_WRITE: - if ((sscanf(bank,"%li", &itmp) != 1)||(itmp < 0)||(itmp >= PCILIB_MAX_BANKS)) + if ((!isnumber(bank))||(sscanf(bank,"%li", &itmp) != 1)||(itmp < 0)||(itmp >= PCILIB_MAX_BANKS)) Usage(argc, argv, "Invalid data bank (%s) is specified", bank); else bar = itmp; break; -- cgit v1.2.3 From 134e2cc87d524bbb63c2e73dc1bb1fd8c0449ae1 Mon Sep 17 00:00:00 2001 From: "Suren A. Chilingaryan" Date: Wed, 9 Mar 2011 17:32:01 +0100 Subject: Provide single header for library --- cli.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'cli.c') diff --git a/cli.c b/cli.c index ff1b754..0bf6193 100644 --- a/cli.c +++ b/cli.c @@ -20,7 +20,7 @@ #include "pci.h" #include "ipecamera.h" #include "tools.h" - +#include "kernel.h" /* defines */ #define MAX_KBUF 14 -- cgit v1.2.3