AlcapDAQ  1
Data Structures | Functions
vme.h File Reference
#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.

Referenced by caenV767_eor1(), dl40x_eor(), and sis3600_eor1().

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.

Referenced by caenV767_fifo_read(), sis3600_fifo_read(), vme_read_d16_checked(), vme_read_d32_checked(), and vme_read_d8_checked().

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.

Referenced by vme_write_d16_checked(), vme_write_d32_checked(), and vme_write_d8_checked().

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.

Referenced by caenV767_bor1(), dl401_bor1(), dl403_init(), sis3600_bor1(), and vmic_ttl_init().

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 40 of file vme.h.

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

Referenced by caenV767_opcode_read(), caenV767_opcode_write(), caenV767_read1(), dl401_convert(), dl401_finished(), dl401_getCounts(), dl401_getMode(), dl403_getMode(), dl403_runBit(), dl403_running(), and vmic_read_input().

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

Definition at line 103 of file vme.h.

References status, SUCCESS, and vme_dma_read().

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

Definition at line 32 of file vme.h.

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

Referenced by caenV767_fifo_read(), dl401_read(), sis3600_bor1(), and sis3600_fifo_read().

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

Definition at line 83 of file vme.h.

References status, SUCCESS, and vme_dma_read().

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

Definition at line 48 of file vme.h.

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

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

Definition at line 123 of file vme.h.

References status, SUCCESS, and vme_dma_read().

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

Definition at line 65 of file vme.h.

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

Referenced by caenV767_bor1(), caenV767_opcode_write(), caenV767_read1(), dl401_bor1(), dl401_clear(), dl401_reset(), dl401_setPedestals(), dl403_asyncMode(), dl403_contMode(), dl403_reset(), dl403_setClockCycle(), dl403_setClockRate(), dl403_start(), dl403_steadyMode(), dl403_stop(), and vmic_set().

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

Definition at line 158 of file vme.h.

References status, SUCCESS, and vme_dma_write().

Referenced by vmic_ttl_init().

161 {
162  int status = vme_dma_write(handle, vme_addr, (unsigned char *) &value,
163  sizeof(unsigned short));
164 
165  if(status == sizeof(unsigned short)) {
166  return SUCCESS;
167  } else {
168  return -1;
169  }
170 
171 }
void vme_write_d32 ( struct vme_handle handle,
unsigned long  vme_addr,
unsigned long  value 
)
inline

Definition at line 56 of file vme.h.

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

Referenced by sis3600_bor1(), and sis3600_read1().

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

Definition at line 143 of file vme.h.

References status, SUCCESS, and vme_dma_write().

147 {
148  int status = vme_dma_write(handle, vme_addr, (unsigned char *) &value,
149  sizeof(unsigned long));
150 
151  if(status == sizeof(unsigned long)) {
152  return SUCCESS;
153  } else {
154  return -1;
155  }
156 }
void vme_write_d8 ( struct vme_handle handle,
unsigned long  vme_addr,
unsigned char  value 
)
inline

Definition at line 74 of file vme.h.

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

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

Definition at line 173 of file vme.h.

References status, SUCCESS, and vme_dma_write().

176 {
177  int status = vme_dma_write(handle, vme_addr, (unsigned char *) &value,
178  sizeof(unsigned char));
179 
180  if(status == sizeof(unsigned char)) {
181  return SUCCESS;
182  } else {
183  return -1;
184  }
185 }