]> jspc29.x-matter.uni-frankfurt.de Git - trbnettools.git/commitdiff
new init_port, port.{c,h} are deprecated from now on, all FIFO control is done by...
authorhadaq <hadaq>
Tue, 10 Nov 2009 20:22:37 +0000 (20:22 +0000)
committerhadaq <hadaq>
Tue, 10 Nov 2009 20:22:37 +0000 (20:22 +0000)
libtrbnet/trberror.c
libtrbnet/trberror.h
libtrbnet/trbnet.c

index 56ede2575cd0d4704de5143c0073bb7b83e46d4f..062aef4d73092a739efba8ef12e27ecac450981f 100644 (file)
@@ -51,10 +51,11 @@ const char* trb_strerror(int trberrno)
     "ReadMem invalid size",
     "Invalid data-length give by Header",
     "FIFO Incomplete, missing",
-    "SEMAPHORE Error"
+    "SEMAPHORE Error",
+    "FIFO Shared Memory Error"
   };
   
-  if (trberrno < 25) {
+  if (trberrno < 26) {
     return errorstring[trberrno];
   } else {
     return "Unknown Errno";
index 78b3cea7fb05bf87cccc919834c44af8a140e5bf..de40459f7e1768acb31a0f7c16429afc575bad2c 100644 (file)
@@ -28,7 +28,8 @@ typedef enum {
   TRB_READMEM_INVALID_SIZE = 21,
   TRB_HDR_DLEN = 22,
   TRB_FIFO_INCOMPLETE = 23,
-  TRB_SEMAPHORE = 24
+  TRB_SEMAPHORE = 24,
+  TRB_FIFO_SHARED_MEM = 25
 } TRB_ERROR;
 
 extern int trb_errno;
index 61299b17da78ae40ae557867a142c9125a2f309e..74215d8710ea034e88a07be1bba3a86451960845 100644 (file)
@@ -1,16 +1,17 @@
-const char trbnet_version[] = "$Revision: 2.52 $";
+const char trbnet_version[] = "$Revision: 2.53 $";
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <signal.h>
+#include <unistd.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
+#include <sys/mman.h>
 #include <sys/ipc.h>
 #include <sys/sem.h>
 #include <errno.h>
 
-#include <port.h>
 #include <trberror.h>
 
 #include "trbnet.h"
@@ -190,11 +191,39 @@ typedef enum {
   Status_Ch3_NoData = 5                  /* nomoredata                  */
 } Status_CH3;
 
-/* ------ Internal Functions FPGA Access -------------------------------- */
+/* ------ Internal Functions for FPGA access ------------------------------ */
 
 #define FIFO_TOGGLE_BIT 0x10000
 
 static uint32_t fifoToggleBit = 0;
+static volatile uint32_t* GPIOB_IN_OFFSET = NULL; 
+static volatile uint32_t* GPIOC_IN_OFFSET = NULL;
+static volatile uint32_t* GPIOC_OUT_OFFSET = NULL;
+
+static inline uint32_t readPC()
+{
+  return *GPIOC_IN_OFFSET;
+}
+
+static inline void writePC(uint32_t data)
+{
+  *GPIOC_OUT_OFFSET = data;
+}
+
+static inline uint32_t readPB()
+{
+  return *GPIOB_IN_OFFSET;
+}
+
+static inline void setbitsPC(uint32_t bitmask)
+{
+  *GPIOC_OUT_OFFSET |= bitmask;
+}
+
+static inline void clrbitsPC(uint32_t bitmask)
+{
+  *GPIOC_OUT_OFFSET &= ~bitmask;
+}
 
 static inline void write32_to_FPGA(uint16_t address, uint32_t value)
 {
@@ -215,7 +244,6 @@ static inline void write32_to_FPGA(uint16_t address, uint32_t value)
   writePC((value & 0xffff) | fifoToggleBit);
 }
 
-
 static inline void read32_from_FPGA(uint16_t address, uint32_t* value)
 {
   /* reads a 32bit word from a given address on a given device */
@@ -930,17 +958,23 @@ static int unlockPorts()
 /* Intit FPGA Interface */
 int init_ports()
 {
+  static const uint32_t GPIO_OFFSET = 0x1a000 / sizeof(uint32_t);
+  static const uint32_t GPIOB_OE_PINS = 0x0;
+  static const uint32_t GPIOC_OE_PINS = 0x3ffff;
+    
+  volatile uint32_t* GPIOC_OE_OFFSET = NULL;
+  volatile uint32_t* GPIOB_OE_OFFSET = NULL;
+  uint32_t* GPIO_PTR = NULL;
+  
+  int memfd;
+  uint32_t *mem = NULL;
+  
   /* Set signal mask for blocking */
   sigemptyset(&blockSet);
   sigaddset(&blockSet, SIGINT);
   sigaddset(&blockSet, SIGTERM);
   sigemptyset(&blockSetOld);
-
-  if (port_init_ports() != 0) {
-    /* Set trberrno ?? */
-    return -1;
-  }
-
+  
   /* Get / Create semaphore */
   if ((semid = semget(sem_key, 1,
                       IPC_CREAT | IPC_EXCL |
@@ -966,15 +1000,63 @@ int init_ports()
     }
   }
 
-  /* Reset Ports */
-  com_reset();
+  /* Open shared memory and initialize FIFO pointers */
+  memfd = open("/dev/mem", O_RDWR);
+  if (memfd < 0) {
+    trb_errno = TRB_FIFO_SHARED_MEM;
+    return -1;
+  }
+  
+  mem = mmap((void*)0,
+             2 * 4 * 4 * 8192,
+             PROT_READ | PROT_WRITE,
+             MAP_SHARED,
+             memfd,
+             0xb0000000);
   
+  if ((void*)mem == MAP_FAILED) {
+    trb_errno = TRB_FIFO_SHARED_MEM;
+    return -1;
+  }
+  
+  /* GPIO */
+  GPIO_PTR = mem + GPIO_OFFSET;
+  
+  GPIOB_IN_OFFSET  = (0x24 / sizeof(uint32_t)) + GPIO_PTR;
+  GPIOC_OUT_OFFSET = (0x30 / sizeof(uint32_t)) + GPIO_PTR;
+  GPIOB_OE_OFFSET  = (0x28 / sizeof(uint32_t)) + GPIO_PTR;
+
+  GPIOC_IN_OFFSET  = (0x34 / sizeof(uint32_t)) + GPIO_PTR;
+  GPIOC_OE_OFFSET  = (0x38 / sizeof(uint32_t)) + GPIO_PTR;
+  
+  close(memfd);
+
+  if (lockPorts() == -1) return -1;
+
+  /* Set output enabled if not done yet */
+  if ((*GPIOB_OE_OFFSET != GPIOB_OE_PINS) ||
+      (*GPIOC_OE_OFFSET != GPIOC_OE_PINS)) {
+    /* read: all bits on portB */
+    *GPIOB_OE_OFFSET = GPIOB_OE_PINS;
+    
+    /* write: portC[17-0] */
+    *GPIOC_OE_OFFSET = GPIOC_OE_PINS;
+    
+    /* Reset Ports */
+    setbitsPC(0x30000);
+    clrbitsPC(0x30000);  
+  }
+  
+  if (unlockPorts() == -1) return -1;
+    
   return 0;
 }
 
 void close_ports()
 {
-  port_close_ports();
+  GPIOB_IN_OFFSET = NULL; 
+  GPIOC_IN_OFFSET = NULL;
+  GPIOC_OUT_OFFSET = NULL;
 }
 
 int trb_fifo_flush(uint8_t channel)