Browse Source

Support for FPGA registers

Suren A. Chilingaryan 13 năm trước cách đây
mục cha
commit
a008b10d84
12 tập tin đã thay đổi với 100 bổ sung34 xóa
  1. 1 0
      .bzrignore
  2. 1 1
      Makefile
  3. 9 8
      cli.c
  4. 39 0
      default.c
  5. 9 0
      default.h
  6. 1 0
      error.h
  7. 0 18
      ipecamera.c
  8. 9 4
      ipecamera.h
  9. 2 2
      pci.c
  10. 10 1
      pci.h
  11. 16 0
      tools.c
  12. 3 0
      tools.h

+ 1 - 0
.bzrignore

@@ -11,3 +11,4 @@ cli.d
 ipecamera.d
 pci.d
 tools.d
+*.d

+ 1 - 1
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$@"

+ 9 - 8
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;

+ 39 - 0
default.c

@@ -0,0 +1,39 @@
+#include <sys/time.h>
+#include <arpa/inet.h>
+#include <assert.h>
+
+#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;
+}

+ 9 - 0
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 */

+ 1 - 0
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,

+ 0 - 18
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) {

+ 9 - 4
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);
 

+ 2 - 2
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;
     }

+ 10 - 1
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 }
 };

+ 16 - 0
tools.c

@@ -3,10 +3,26 @@
 #include <unistd.h>
 #include <stdint.h>
 #include <assert.h>
+#include <ctype.h>
 #include <arpa/inet.h>
 
 #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));
 }

+ 3 - 0
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);