AlcapDAQ  1
Data Structures | Functions
vme_universe.h File Reference
#include <netinet/in.h>
#include "vme_io.h"

Go to the source code of this file.

Data Structures

struct  vme_handle
 

Functions

struct vme_handlevme_open (unsigned long vme_addr, struct vme_mapping_ctrl mapping, int size, int fifo_block_size=0)
 
void vme_close (struct vme_handle *handle)
 
int vme_dma_read (struct vme_handle *handle, unsigned long vme_addr, unsigned char *buffer, int size)
 
int vme_dma_write (struct vme_handle *handle, unsigned long vme_addr, unsigned char *buffer, int size)
 
unsigned long vme_read_d32 (struct vme_handle *handle, unsigned long vme_addr)
 
unsigned short vme_read_d16 (struct vme_handle *handle, unsigned long vme_addr)
 
unsigned char vme_read_d8 (struct vme_handle *handle, unsigned long vme_addr)
 
void vme_write_d32 (struct vme_handle *handle, unsigned long vme_addr, unsigned long value)
 
void vme_write_d16 (struct vme_handle *handle, unsigned long vme_addr, unsigned short value)
 
void vme_write_d8 (struct vme_handle *handle, unsigned long vme_addr, unsigned char value)
 
unsigned long vme_read_d32_checked (struct vme_handle *handle, unsigned long vme_addr, int *status=NULL)
 
unsigned short vme_read_d16_checked (struct vme_handle *handle, unsigned long vme_addr, int *status=NULL)
 
unsigned char vme_read_d8_checked (struct vme_handle *handle, unsigned long vme_addr, int *status=NULL)
 
int vme_write_d32_checked (struct vme_handle *handle, unsigned long vme_addr, unsigned long value)
 
int vme_write_d16_checked (struct vme_handle *handle, unsigned long vme_addr, unsigned short value)
 
int vme_write_d8_checked (struct vme_handle *handle, unsigned long vme_addr, unsigned char value)
 

Function Documentation

void vme_close ( struct vme_handle handle)

Definition at line 162 of file vme_universe.cpp.

163 {
164  handle->reference_count--;
165 
166  if(handle->reference_count == 0) {
167  int status = munmap((void *) handle->base, handle->size);
168  if(status < 0) {
169  perror("Error unmapping VME:");
170  }
171  status = close(handle->fd);
172  if(status < 0) {
173  perror("Error closing VME:");
174  }
175  handle->used = false;
176  }
177 }
int vme_dma_read ( struct vme_handle handle,
unsigned long  vme_addr,
unsigned char *  buffer,
int  size 
)

Definition at line 179 of file vme_universe.cpp.

183 {
184  struct vme_dma_req dma_req;
185 
186  dma_req.vme_addr = vme_addr;
187  dma_req.buf = buffer;
188  dma_req.count = size;
189  dma_req.fifo_block_size = handle->fifo_block_size;
190 
191  int bytes_read = ioctl(handle->fd, VMEIMG_DMA_READ, &dma_req);
192 
193  return bytes_read;
194 
195 #if 0
196  if(handle->fifo_block_size == 0) {
197  return pread(handle->fd, buffer, size, vme_addr - handle->vme_base);
198  } else {
199 
200  int bytes_read = 0;
201 
202  while(bytes_read < size) {
203 
204  int size_this_time = MIN(size - bytes_read, handle->fifo_block_size);
205 
206  int status = pread(handle->fd, buffer + bytes_read, size_this_time,
207  vme_addr - handle->vme_base);
208 
209  if(status > 0) {
210  bytes_read += status;
211  }
212 
213  if(status < size_this_time) {
214  break;
215  }
216  }
217 
218  return bytes_read;
219  }
220 #endif
221 }
int vme_dma_write ( struct vme_handle handle,
unsigned long  vme_addr,
unsigned char *  buffer,
int  size 
)

Definition at line 223 of file vme_universe.cpp.

227 {
228  struct vme_dma_req dma_req;
229 
230  dma_req.vme_addr = vme_addr;
231  dma_req.buf = buffer;
232  dma_req.count = size;
233  dma_req.fifo_block_size = handle->fifo_block_size;
234 
235  int bytes_written = ioctl(handle->fd, VMEIMG_DMA_WRITE, &dma_req);
236 
237  return bytes_written;
238 
239 #if 0
240  if(handle->fifo_block_size == 0) {
241  return pwrite(handle->fd, buffer, size, vme_addr - handle->vme_base);
242  } else {
243 
244  int bytes_written = 0;
245 
246  while(bytes_written < size) {
247 
248  int size_this_time = MIN(size - bytes_written, handle->fifo_block_size);
249 
250  int status = pwrite(handle->fd, buffer + bytes_written, size_this_time,
251  vme_addr - handle->vme_base);
252 
253  if(status > 0) {
254  bytes_written += status;
255  }
256 
257  if(status < size_this_time) {
258  break;
259  }
260  }
261 
262  return bytes_written;
263  }
264 #endif
265 }
struct vme_handle* vme_open ( unsigned long  vme_addr,
struct vme_mapping_ctrl  mapping,
int  size,
int  fifo_block_size = 0 
)

Definition at line 51 of file vme_universe.cpp.

55 {
56  struct vme_handle *handle = NULL;
57  unsigned long new_vme_base = 0;
58  unsigned long new_vme_size = 0;
59 
60  vme_addr = page_round_down(vme_addr);
61  unsigned long vme_addr_end = page_round_up(vme_addr + size);
62 
63  // Search through the currently-allocated windows, looking for
64  // one that would be compatible.
65  for(int i = 0; i < MAX_VME_HANDLES; i++) {
66  if(vme_handles[i].used &&
67  vme_handles[i].mapping.address_space == mapping.address_space &&
68  vme_handles[i].mapping.supuseram == mapping.supuseram &&
69  vme_handles[i].mapping.prgdataam == mapping.prgdataam &&
71 
72  new_vme_base =
73  MIN(vme_addr, vme_handles[i].vme_base);
74  unsigned long new_vme_addr_end =
75  MAX(vme_addr_end, vme_handles[i].vme_base + vme_handles[i].size);
76  new_vme_size = new_vme_addr_end - new_vme_base;
77 
78  if(new_vme_size <= MAX_VME_MAPPED_SIZE) {
79  handle = &vme_handles[i];
80  break;
81  }
82  }
83  }
84 
85  // If we didn't find one, then look for a brand-new window
86  if(handle == NULL) {
87  new_vme_base = vme_addr;
88  new_vme_size = vme_addr_end - vme_addr;
89 
90  for(int i = 0; i < MAX_VME_HANDLES; i++) {
91  if(!(vme_handles[i].used)) {
92  handle = &vme_handles[i];
93  }
94  }
95  }
96 
97  // If the window is brand-new, then open a file descriptor for it.
98  if(!handle->used) {
99 
100  // Try each of the device special files
101  for(int i = 0; i < 8; i++) {
102  char filename[20];
103  sprintf(filename, "/dev/vme_a32_%d", i);
104  handle->fd = open(filename, O_RDWR);
105  if(handle->fd >= 0) {
106  break;
107  }
108  }
109 
110  // Check that we found one
111  if(handle->fd < 0) {
112  diag_print(0, "Unable to open a VME device file\n");
113  return NULL;
114  }
115  }
116  // Otherwise, unmap the associated memory
117  else {
118  munmap((void *) handle->base, handle->size);
119  }
120 
121  // Set the address space mapping
122  if(ioctl(handle->fd, VMEIMG_SETMAPPING, &mapping)) {
123  perror("ioctl VMEIMG_SETMAPPING");
124  return NULL;
125  }
126 
127  // Set the VME base address appropriately
128  if(ioctl(handle->fd, VMEIMG_SETVMEADDR, new_vme_base)) {
129  perror("ioctl VMEIMG_SETVMEADDR");
130  return NULL;
131  }
132 
133  // Retrieve the base address that the driver actually accepted
134  if(ioctl(handle->fd, VMEIMG_GETVMEADDR, &handle->vme_base)) {
135  perror("ioctl VMEIMG_GETVMEADDR");
136  return NULL;
137  }
138 
139  // Map some memory for the region of interest
140  handle->base = (volatile unsigned char *)
141  mmap(NULL, new_vme_size, PROT_WRITE | PROT_READ, MAP_SHARED, handle->fd, 0);
142  if(handle->base == (volatile unsigned char *) -1) {
143  perror("mmap VME address space");
144  return NULL;
145  }
146 
147  // Remember the basic properties of this handle
148  handle->used = true;
149  handle->mapping.max_datawidth = mapping.max_datawidth;
150  handle->mapping.address_space = mapping.address_space;
151  handle->mapping.supuseram = mapping.supuseram;
152  handle->mapping.prgdataam = mapping.prgdataam;
154  handle->size = new_vme_size;
155 
156  // Keep a reference count
157  handle->reference_count++;
158 
159  return handle;
160 }
unsigned short vme_read_d16 ( struct vme_handle handle,
unsigned long  vme_addr 
)
inline

Definition at line 42 of file vme_universe.h.

References vme_handle::base, and vme_handle::vme_base.

44 {
45  volatile unsigned short *p = (volatile unsigned short *)
46  (handle->base + (vme_addr - handle->vme_base));
47  return *p;
48 }
unsigned short vme_read_d16_checked ( struct vme_handle handle,
unsigned long  vme_addr,
int *  status = NULL 
)
inline

Definition at line 105 of file vme_universe.h.

References status, and vme_dma_read().

108 {
109  unsigned short result;
110 
111  int status1 = vme_dma_read(handle, vme_addr, (unsigned char *) &result,
112  sizeof(unsigned short));
113 
114  if(status != NULL) {
115  if(status1 == sizeof(unsigned short)) {
116  *status = 0;
117  } else {
118  *status = -1;
119  }
120  }
121 
122  return ntohs(result);
123 }
unsigned long vme_read_d32 ( struct vme_handle handle,
unsigned long  vme_addr 
)
inline

Definition at line 34 of file vme_universe.h.

References vme_handle::base, and vme_handle::vme_base.

36 {
37  volatile unsigned long *p = (volatile unsigned long *)
38  (handle->base + (vme_addr - handle->vme_base));
39  return *p;
40 }
unsigned long vme_read_d32_checked ( struct vme_handle handle,
unsigned long  vme_addr,
int *  status = NULL 
)
inline

Definition at line 85 of file vme_universe.h.

References status, and vme_dma_read().

88 {
89  unsigned long result;
90 
91  int status1 = vme_dma_read(handle, vme_addr, (unsigned char *) &result,
92  sizeof(unsigned long));
93 
94  if(status != NULL) {
95  if(status1 == sizeof(unsigned long)) {
96  *status = 0;
97  } else {
98  *status = -1;
99  }
100  }
101 
102  return ntohl(result);
103 }
unsigned char vme_read_d8 ( struct vme_handle handle,
unsigned long  vme_addr 
)
inline

Definition at line 50 of file vme_universe.h.

References vme_handle::base, and vme_handle::vme_base.

52 {
53  volatile unsigned char *p = (volatile unsigned char *)
54  (handle->base + (vme_addr - handle->vme_base));
55  return *p;
56 }
unsigned char vme_read_d8_checked ( struct vme_handle handle,
unsigned long  vme_addr,
int *  status = NULL 
)
inline

Definition at line 125 of file vme_universe.h.

References status, and vme_dma_read().

128 {
129  unsigned short result;
130 
131  int status1 = vme_dma_read(handle, vme_addr, (unsigned char *) &result,
132  sizeof(unsigned char));
133 
134  if(status != NULL) {
135  if(status1 == sizeof(unsigned char)) {
136  *status = 0;
137  } else {
138  *status = -1;
139  }
140  }
141 
142  return result;
143 }
void vme_write_d16 ( struct vme_handle handle,
unsigned long  vme_addr,
unsigned short  value 
)
inline

Definition at line 67 of file vme_universe.h.

References vme_handle::base, value, and vme_handle::vme_base.

70 {
71  volatile unsigned short *p = (volatile unsigned short *)
72  (handle->base + (vme_addr - handle->vme_base));
73  *p = value;
74 }
int vme_write_d16_checked ( struct vme_handle handle,
unsigned long  vme_addr,
unsigned short  value 
)
inline

Definition at line 162 of file vme_universe.h.

References status, and vme_dma_write().

165 {
166  unsigned short value1 = htons(value);
167 
168  int status = vme_dma_write(handle, vme_addr, (unsigned char *) &value1,
169  sizeof(unsigned short));
170 
171  if(status == sizeof(unsigned short)) {
172  return 0;
173  } else {
174  return -1;
175  }
176 
177 }
void vme_write_d32 ( struct vme_handle handle,
unsigned long  vme_addr,
unsigned long  value 
)
inline

Definition at line 58 of file vme_universe.h.

References vme_handle::base, value, and vme_handle::vme_base.

61 {
62  volatile unsigned long *p = (volatile unsigned long *)
63  (handle->base + (vme_addr - handle->vme_base));
64  *p = value;
65 }
int vme_write_d32_checked ( struct vme_handle handle,
unsigned long  vme_addr,
unsigned long  value 
)
inline

Definition at line 145 of file vme_universe.h.

References status, and vme_dma_write().

149 {
150  unsigned long value1 = htonl(value);
151 
152  int status = vme_dma_write(handle, vme_addr, (unsigned char *) &value1,
153  sizeof(unsigned long));
154 
155  if(status == sizeof(unsigned long)) {
156  return 0;
157  } else {
158  return -1;
159  }
160 }
void vme_write_d8 ( struct vme_handle handle,
unsigned long  vme_addr,
unsigned char  value 
)
inline

Definition at line 76 of file vme_universe.h.

References vme_handle::base, value, and vme_handle::vme_base.

79 {
80  volatile unsigned char *p = (volatile unsigned char *)
81  (handle->base + (vme_addr - handle->vme_base));
82  *p = value;
83 }
int vme_write_d8_checked ( struct vme_handle handle,
unsigned long  vme_addr,
unsigned char  value 
)
inline

Definition at line 179 of file vme_universe.h.

References status, and vme_dma_write().

182 {
183  int status = vme_dma_write(handle, vme_addr, (unsigned char *) &value,
184  sizeof(unsigned char));
185 
186  if(status == sizeof(unsigned char)) {
187  return 0;
188  } else {
189  return -1;
190  }
191 }