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 --- .bzrignore | 1 + Makefile | 2 +- cli.c | 17 +++++++++-------- default.c | 39 +++++++++++++++++++++++++++++++++++++++ default.h | 9 +++++++++ error.h | 1 + ipecamera.c | 18 ------------------ ipecamera.h | 13 +++++++++---- pci.c | 4 ++-- pci.h | 11 ++++++++++- tools.c | 16 ++++++++++++++++ tools.h | 3 +++ 12 files changed, 100 insertions(+), 34 deletions(-) create mode 100644 default.c create mode 100644 default.h diff --git a/.bzrignore b/.bzrignore index 62c1825..8f0badc 100644 --- a/.bzrignore +++ b/.bzrignore @@ -11,3 +11,4 @@ cli.d ipecamera.d pci.d tools.d +*.d diff --git a/Makefile b/Makefile index ae98ee0..3dee6b5 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ include common.mk ############################################################### # Target definitions -OBJECTS = pci.o ipecamera.o tools.o +OBJECTS = pci.o ipecamera.o default.o tools.o libpcilib.so: $(OBJECTS) echo -e "LD \t$@" 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; diff --git a/default.c b/default.c new file mode 100644 index 0000000..d03858f --- /dev/null +++ b/default.c @@ -0,0 +1,39 @@ +#include +#include +#include + +#include "tools.h" +#include "default.h" +#include "error.h" + +#define BIT_MASK(bits) ((1l << (bits)) - 1) + +#define default_datacpy(dst, src, access, bank) pcilib_datacpy(dst, src, access, 1, bank->raw_endianess) + +int pcilib_default_read(pcilib_t *ctx, pcilib_register_bank_description_t *bank, pcilib_register_addr_t addr, uint8_t bits, pcilib_register_value_t *value) { + int err; + + char *ptr; + pcilib_register_value_t val = 0; + int access = bank->access / 8; + + ptr = pcilib_resolve_register_address(ctx, bank->read_addr + addr * access); + default_datacpy(&val, ptr, access, bank); + + *value = val&BIT_MASK(bits); + + return 0; +} + + +int pcilib_default_write(pcilib_t *ctx, pcilib_register_bank_description_t *bank, pcilib_register_addr_t addr, uint8_t bits, pcilib_register_value_t value) { + int err; + + char *ptr; + int access = bank->access / 8; + + ptr = pcilib_resolve_register_address(ctx, bank->write_addr + addr * access); + default_datacpy(ptr, &value, access, bank); + + return 0; +} diff --git a/default.h b/default.h new file mode 100644 index 0000000..6345ae8 --- /dev/null +++ b/default.h @@ -0,0 +1,9 @@ +#ifndef _PCILIB_DEFAULT_H +#define _PCILIB_DEFAULT_H + +#include "pci.h" + +int pcilib_default_read(pcilib_t *ctx, pcilib_register_bank_description_t *bank, pcilib_register_addr_t addr, uint8_t bits, pcilib_register_value_t *value); +int pcilib_default_write(pcilib_t *ctx, pcilib_register_bank_description_t *bank, pcilib_register_addr_t addr, uint8_t bits, pcilib_register_value_t value); + +#endif /* _PCILIB_DEFAULT_H */ diff --git a/error.h b/error.h index 715b6eb..d64c518 100644 --- a/error.h +++ b/error.h @@ -3,6 +3,7 @@ enum { PCILIB_ERROR_SUCCESS = 0, + PCILIB_ERROR_MEMORY, PCILIB_ERROR_INVALID_ADDRESS, PCILIB_ERROR_INVALID_BANK, PCILIB_ERROR_TIMEOUT, diff --git a/ipecamera.c b/ipecamera.c index 81cf7d7..9c238f5 100644 --- a/ipecamera.c +++ b/ipecamera.c @@ -15,24 +15,6 @@ #define ipecamera_datacpy(dst, src, bank) pcilib_datacpy(dst, src, 4, 1, bank->raw_endianess) -/* -int ipecamera_read_register_space(int handle, pcilib_model_t model, pcilib_register_t addr, pcilib_register_value_t *value) { - //ipelib_write_ - //(void *buf, int handle, pcilib_bar_t bar, uintptr_t addr, size_t size); - -} - -int ipecamera_write_register_space(int handle, pcilib_model_t model, pcilib_register_t addr, pcilib_register_value_t value) { -} - - -static int ipecamera_read_byte(int handle, int reg, int bits, uint8_t *value) { -} - -static int ipecamera_write_byte(int handle, int reg, int bits, uint8_t value) { -} -*/ - static pcilib_register_value_t ipecamera_bit_mask[9] = { 0, 1, 3, 7, 15, 31, 63, 127, 255 }; int ipecamera_read(pcilib_t *ctx, pcilib_register_bank_description_t *bank, pcilib_register_addr_t addr, uint8_t bits, pcilib_register_value_t *value) { diff --git a/ipecamera.h b/ipecamera.h index d3657e0..8929b85 100644 --- a/ipecamera.h +++ b/ipecamera.h @@ -9,11 +9,11 @@ #define IPECAMERA_REGISTER_WRITE (IPECAMERA_REGISTER_SPACE + 0) #define IPECAMERA_REGISTER_READ (IPECAMERA_REGISTER_WRITE + 4) - #ifdef _IPECAMERA_C pcilib_register_bank_description_t ipecamera_register_banks[] = { - { PCILIB_REGISTER_BANK0, 128, IPECAMERA_REGISTER_PROTOCOL, IPECAMERA_REGISTER_READ, IPECAMERA_REGISTER_WRITE, PCILIB_BIG_ENDIAN, 8, PCILIB_LITTLE_ENDIAN, "cmosis", "CMOSIS CMV2000 Registers" }, - { 0, 0, 0, 0, 0, 0, NULL, NULL } + { PCILIB_REGISTER_BANK0, 128, IPECAMERA_REGISTER_PROTOCOL, IPECAMERA_REGISTER_READ, IPECAMERA_REGISTER_WRITE, PCILIB_BIG_ENDIAN, 8, PCILIB_LITTLE_ENDIAN, "cmosis", "CMOSIS CMV2000 Registers" }, + { PCILIB_REGISTER_BANK1, 64, PCILIB_DEFAULT_PROTOCOL, IPECAMERA_REGISTER_SPACE, IPECAMERA_REGISTER_SPACE, PCILIB_BIG_ENDIAN, 32, PCILIB_LITTLE_ENDIAN, "fpga", "IPECamera Registers" }, + { 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL } }; pcilib_register_description_t ipecamera_registers[] = { @@ -61,6 +61,12 @@ pcilib_register_description_t ipecamera_registers[] = { {111, 1, 1, PCILIB_REGISTER_RW, PCILIB_REGISTER_BANK0, "bit_mode", ""}, {112, 2, 0, PCILIB_REGISTER_RW, PCILIB_REGISTER_BANK0, "adc_resolution", ""}, {126, 16, 0, PCILIB_REGISTER_RW, PCILIB_REGISTER_BANK0, "temp", ""}, +{0, 32, 0, PCILIB_REGISTER_RW, PCILIB_REGISTER_BANK1, "spi_conf_input", ""}, +{1, 32, 0, PCILIB_REGISTER_R, PCILIB_REGISTER_BANK1, "spi_conf_output", ""}, +{2, 32, 0, PCILIB_REGISTER_RW, PCILIB_REGISTER_BANK1, "spi_clk_speed", ""}, +{3, 32, 0, PCILIB_REGISTER_R, PCILIB_REGISTER_BANK1, "firmware_version", ""}, +{6, 16, 0, PCILIB_REGISTER_R, PCILIB_REGISTER_BANK1, "cmosis_temperature", ""}, +{7, 32, 0, PCILIB_REGISTER_RW, PCILIB_REGISTER_BANK1, "temperature_sample_timing", ""}, {0, 0, 0, 0, 0, NULL, NULL} }; @@ -74,7 +80,6 @@ extern pcilib_register_bank_description_t ipecamera_register_banks[]; extern pcilib_register_range_t ipecamera_register_ranges[]; #endif - int ipecamera_read(pcilib_t *ctx, pcilib_register_bank_description_t *bank, pcilib_register_addr_t addr, uint8_t bits, pcilib_register_value_t *value); int ipecamera_write(pcilib_t *ctx, pcilib_register_bank_description_t *bank, pcilib_register_addr_t addr, uint8_t bits, pcilib_register_value_t value); diff --git a/pci.c b/pci.c index 9ddc1d6..9b6b011 100644 --- a/pci.c +++ b/pci.c @@ -22,7 +22,7 @@ #include "ipecamera.h" #include "error.h" -#define BIT_MASK(bits) ((1 << (bits)) - 1) +#define BIT_MASK(bits) ((1l << (bits)) - 1) //#define PCILIB_FILE_IO @@ -232,7 +232,7 @@ pcilib_register_bank_t pcilib_find_bank(pcilib_t *ctx, const char *bank) { return -1; } - if (sscanf(bank,"%lx", &addr) == 1) { + if (pcilib_isxnumber(bank)&&(sscanf(bank,"%lx", &addr) == 1)) { res = pcilib_find_bank_by_addr(ctx, addr); if (res != PCILIB_REGISTER_BANK_INVALID) return res; } diff --git a/pci.h b/pci.h index 9625e72..2bb263f 100644 --- a/pci.h +++ b/pci.h @@ -9,6 +9,7 @@ #include "driver/pciDriver.h" #include "kernel.h" + #define pcilib_memcpy pcilib_memcpy32 #define pcilib_datacpy pcilib_datacpy32 @@ -41,6 +42,7 @@ typedef enum { typedef enum { + PCILIB_DEFAULT_PROTOCOL, IPECAMERA_REGISTER_PROTOCOL } pcilib_register_protocol_t; @@ -49,6 +51,9 @@ typedef enum { #define PCILIB_ADDRESS_INVALID ((uintptr_t)-1) #define PCILIB_REGISTER_BANK_INVALID ((pcilib_register_bank_t)-1) #define PCILIB_REGISTER_BANK0 0 +#define PCILIB_REGISTER_BANK1 1 +#define PCILIB_REGISTER_BANK2 2 +#define PCILIB_REGISTER_BANK3 3 typedef struct { pcilib_register_bank_addr_t addr; @@ -102,13 +107,17 @@ typedef struct { } pcilib_model_description_t; #ifdef _PCILIB_PCI_C +# include "ipecamera.h" +# include "default.h" + pcilib_model_description_t pcilib_model[3] = { { NULL, NULL, NULL }, { NULL, NULL, NULL }, { ipecamera_registers, ipecamera_register_banks, ipecamera_register_ranges } }; -pcilib_protocol_description_t pcilib_protocol[2] = { +pcilib_protocol_description_t pcilib_protocol[3] = { + { pcilib_default_read, pcilib_default_write }, { ipecamera_read, ipecamera_write }, { NULL, NULL } }; diff --git a/tools.c b/tools.c index 838172d..4609619 100644 --- a/tools.c +++ b/tools.c @@ -3,10 +3,26 @@ #include #include #include +#include #include #include "tools.h" +int pcilib_isnumber(const char *str) { + int i = 0; + for (i = 0; str[i]; i++) + if (!isdigit(str[i])) return 0; + return 1; +} + +int pcilib_isxnumber(const char *str) { + int i = 0; + for (i = 0; str[i]; i++) + if (!isxdigit(str[i])) return 0; + return 1; +} + + uint16_t pcilib_swap16(uint16_t x) { return (((x<<8)&0xFFFF) | ((x>>8)&0xFFFF)); } diff --git a/tools.h b/tools.h index 059675a..747fc37 100644 --- a/tools.h +++ b/tools.h @@ -6,6 +6,9 @@ #include "pci.h" +int pcilib_isnumber(const char *str); +int pcilib_isxnumber(const char *str); + uint16_t pcilib_swap16(uint16_t x); uint32_t pcilib_swap32(uint32_t x); uint64_t pcilib_swap64(uint64_t x); -- cgit v1.2.3