AlcapDAQ  1
Data Structures | Functions | Variables
dl40x.cpp File Reference
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include "midas.h"
#include "crate.h"
#include "vme.h"
#include "dl40x.h"
#include "diag.h"
#include "odb_wrapper.h"

Go to the source code of this file.

Data Structures

struct  dl401
 
struct  dl403
 

Functions

INT dl40x_bor ()
 
INT dl40x_eor ()
 
INT dl40x_read (char *pevent)
 
INT dl401_bor1 (struct dl401 *d)
 
INLINE int dl401_setPedestals (struct dl401 *d, u_short p[], int np)
 
INLINE u_short dl401_getCounts (struct dl401 *d)
 
INLINE u_short dl401_getMode (struct dl401 *d)
 
INLINE void dl401_reset (struct dl401 *d)
 
INLINE void dl401_clear (struct dl401 *d)
 
INLINE void dl401_convert (struct dl401 *d)
 
INLINE int dl401_finished (struct dl401 *d)
 
int dl401_read (struct dl401 *d, u_int *data)
 
INT dl403_init (struct dl403 *d)
 
void dl403_setClockRate (struct dl403 *d, u_short clk1, u_short clk2, u_short clk3)
 
void dl403_setClockCycle (struct dl403 *d, u_short c1, u_short c2, u_short c3)
 
INLINE short dl403_getMode (struct dl403 *d)
 
INLINE void dl403_asyncMode (struct dl403 *d)
 
INLINE void dl403_contMode (struct dl403 *d)
 
INLINE void dl403_steadyMode (struct dl403 *d)
 
INLINE void dl403_start (struct dl403 *d)
 
INLINE void dl403_stop (struct dl403 *d)
 
INLINE void dl403_reset (struct dl403 *d)
 
INLINE short dl403_runBit (struct dl403 *d)
 
INLINE short dl403_running (struct dl403 *d)
 

Variables

struct readout_module dl40x_module
 
static struct dl401 DL401 [DL401_MAXMODULES]
 
static struct dl403 clk
 

Function Documentation

INT dl401_bor1 ( struct dl401 d)

Definition at line 168 of file dl40x.cpp.

References dl401::a16_base, dl401::a16_handle, dl401::a32_base, dl401::a32_handle, crate_number, diag_print(), DL401_AM_EXT_MODE, DL401_BASE, dl401_clear(), DL401_DEFMODE, DL401_LWIOENA, DL401_MASK, DL401_MODE, DL401_NCHAN, dl401_reset(), dl401_setPedestals(), odb_get_dword(), odb_get_word(), dl401::odb_name, ped, SUCCESS, vme_open(), vme_write_d16(), VMEMAP_ASPACE_A16, VMEMAP_ASPACE_A32, VMEMAP_DWIDTH_32, VMEMAP_PRGDATAAM_DATA, VMEMAP_SUPUSERAM_SUPER, and VMEMAP_SUPUSERAM_USER.

Referenced by dl40x_bor().

169 {
170  // Find base addresses. This module has separate A16 (for configuration)
171  // and A32 (for data memory) address regions. The A16 address is set by
172  // jumpers while the A32 is programmed by a register accessed through the
173  // A16 space.
174  d->a16_base = odb_get_word ("/Equipment/Crate %d/Settings/%s/a16 base address",
175  crate_number, d->odb_name);
176  d->a32_base = odb_get_dword ("/Equipment/Crate %d/Settings/%s/a32 base address",
177  crate_number, d->odb_name);
178 
179  // Get VME handles to access module
180  struct vme_mapping_ctrl a16_mapping = {
185  };
186  d->a16_handle = vme_open(d->a16_base, a16_mapping, 0x100, 0);
187 
188  struct vme_mapping_ctrl a32_mapping = {
193  };
194  d->a32_handle = vme_open(d->a32_base, a32_mapping, 4096, 0);
195 
196  // Set up the A32 base address
197  vme_write_d16 (d->a16_handle, d->a16_base | DL401_BASE, d->a32_base >> 16);
198 
199  // Set up the address modifiers
202 
203  // Set the default mode bits
205 
206  // find and set pedestals
208  for (int k = 0; k < DL401_NCHAN; k++)
209  {
210  ped[k] = odb_get_word ("/Equipment/Crate %d/Settings/%s/Pedestal",
211  crate_number, d->odb_name);
212  }
213  dl401_setPedestals (d, ped, DL401_NCHAN);
214 
215  // standard reset and clear
216  dl401_reset (d);
217  dl401_clear (d);
218 
219  diag_print(2, "dl401_bor1 a32_base=0x%08lx a16_base=0x%08lx\n",
220  d->a32_base, d->a16_base);
221 
222  return SUCCESS;
223 }
INLINE void dl401_clear ( struct dl401 d)

Definition at line 280 of file dl40x.cpp.

Referenced by dl401_bor1(), and dl40x_read().

281 {
282  vme_write_d16 (d->a16_handle, d->a16_base | DL401_CLIF, 0xffff);
283 }
INLINE void dl401_convert ( struct dl401 d)

Definition at line 286 of file dl40x.cpp.

287 {
289 }
INLINE int dl401_finished ( struct dl401 d)

Definition at line 292 of file dl40x.cpp.

Referenced by dl40x_read().

293 {
294  WORD mode = vme_read_d16 (d->a16_handle, d->a16_base | DL401_MODE);
295  return (mode & DL401_INTREQ);
296 }
INLINE u_short dl401_getCounts ( struct dl401 d)

Definition at line 261 of file dl40x.cpp.

Referenced by dl401_read().

262 {
263  WORD rmad = vme_read_d16 (d->a16_handle, d->a16_base | DL401_RMAD);
264  return rmad & DL401_CNTMASK;
265 }
INLINE u_short dl401_getMode ( struct dl401 d)

Definition at line 268 of file dl40x.cpp.

269 {
270  return vme_read_d16 (d->a16_handle, d->a16_base | DL401_MODE);
271 }
int dl401_read ( struct dl401 d,
u_int *  data 
)

Definition at line 300 of file dl40x.cpp.

Referenced by dl40x_read().

301 {
302  register u_int i = 0;
303  register u_int *addr;
304  int counts;
305 
306  counts = dl401_getCounts (d);
307  diag_print (2, "dl401_read: [%d] base=%p counts=%d\n", d, addr, counts);
308 
309  while (i < DL401_CNTMAX - counts)
310  {
311  data[i++] = vme_read_d32 (d->a32_handle, d->a32_base + (i + counts)*4);
312  }
313 
314  i = 0;
315  while (i < counts)
316  {
317  data[DL401_CNTMAX - counts + i] =
318  vme_read_d32 (d->a32_handle, d->a32_base + i*4);
319  i++;
320  }
321  diag_print (2, "--> done\n");
322  return (DL401_CNTMAX);
323 }
INLINE void dl401_reset ( struct dl401 d)

Definition at line 274 of file dl40x.cpp.

Referenced by dl401_bor1(), and dl40x_read().

275 {
276  vme_write_d16 (d->a16_handle, d->a16_base | DL401_MRST, 0xffff);
277 }
INLINE int dl401_setPedestals ( struct dl401 d,
u_short  p[],
int  np 
)

Definition at line 226 of file dl40x.cpp.

Referenced by dl401_bor1().

227 {
228  if (np != DL401_NCHAN)
229  return (-1);
230 
231  // compute word to write to pedestal DAC
232  DWORD parPed = 0;
233  for (int i = 0; i < np; i++) {
234  parPed |= (u_long) (p[i] & DL401_PEDCHANMASK) << (i * DL401_PEDCHANBITS);
235  }
236  parPed &= DL401_PEDMASK;
237  diag_print (2, "dl401_setPedestal: [%d] 0%08lo (", d, parPed);
238 
239  // enable writing to DAC
242 
243  // shift bits into DAC one at a time; it's a serial interface
244  for (int i = 0; i < DL401_PEDBITS; i++)
245  {
246  u_short bit =
247  ((parPed << i) & DL401_PEDBITMASK) >> (DL401_PEDBITS - 1);
248  vme_write_d16 (d->a16_handle, d->a16_base | DL401_WDAC, bit & 0x0001);
249  diag_print (2, "%1x", bit);
250  }
251 
252  // disable writing to DAC
255  diag_print (2, ")\n");
256 
257  return SUCCESS;
258 }
INLINE void dl403_asyncMode ( struct dl403 d)

Definition at line 436 of file dl40x.cpp.

437 {
439 }
INLINE void dl403_contMode ( struct dl403 d)

Definition at line 442 of file dl40x.cpp.

443 {
445 }
INLINE short dl403_getMode ( struct dl403 d)

Definition at line 430 of file dl40x.cpp.

References dl403::a16_base, DL403_MODE, dl403::vme_handle, and vme_read_d16().

431 {
432  return vme_read_d16 (d->vme_handle, d->a16_base | DL403_MODE);
433 }
INT dl403_init ( struct dl403 d)

Definition at line 352 of file dl40x.cpp.

Referenced by dl40x_bor().

353 {
354  // Do we have an FADC clock?
355  if (odb_find_key("/Equipment/Crate %d/Settings/FADC CLOCK", crate_number)) {
356  d->enabled = TRUE;
357  } else {
358  d->enabled = FALSE;
359  return SUCCESS;
360  }
361 
362  // find base address
363  d->a16_base =
364  odb_get_word ("/Equipment/Crate %d/Settings/FADC CLOCK/a16 base address",
365  crate_number);
366 
367  // Open a VME handle
368  struct vme_mapping_ctrl a16_mapping = {
373  };
374  d->vme_handle = vme_open(d->a16_base, a16_mapping, 0x100, 0);
375 
376  // Set DL403 clock frequency
377  INT fadc_frequency =
378  odb_get_int ("/Equipment/Crate %d/Settings/FADC CLOCK/frequency", crate_number);
379  switch (fadc_frequency)
380  {
381  case 100:
382  dl403_setClockRate (d, DL403_100MHZ, 0x0, 0x0);
383  break;
384  case 50:
385  dl403_setClockRate (d, DL403_50MHZ, 0x0, 0x0);
386  break;
387  case 25:
388  dl403_setClockRate (d, DL403_SELECTED, 0x0, 0x0);
389  break;
390 
391  default:
392  cm_msg (MERROR, frontend_name,
393  "FADC clock frequency must be 25, 50, or 100 MHz (currently %d)\n",
394  fadc_frequency);
395  dl403_setClockRate (d, DL403_50MHZ, 0x0, 0x0);
396  }
397 
398  dl403_setClockCycle (d, 0x800, 0x800, 0x800);
399  dl403_steadyMode (d);
400  dl403_start (d);
401 
402  diag_print (2, "dl403_init 0x%08lx\n", d->a16_base);
403  return SUCCESS;
404 }
INLINE void dl403_reset ( struct dl403 d)

Definition at line 466 of file dl40x.cpp.

467 {
469 }
INLINE short dl403_runBit ( struct dl403 d)

Definition at line 472 of file dl40x.cpp.

473 {
474  WORD mode = vme_read_d16 (d->vme_handle, d->a16_base | DL403_MODE);
475  return (mode & DL403_RUN);
476 }
INLINE short dl403_running ( struct dl403 d)

Definition at line 479 of file dl40x.cpp.

Referenced by dl40x_read().

480 {
481  WORD mode = vme_read_d16 (d->vme_handle, d->a16_base | DL403_MODE);
482  return !(mode & DL403_RUN);
483 }
void dl403_setClockCycle ( struct dl403 d,
u_short  c1,
u_short  c2,
u_short  c3 
)

Definition at line 417 of file dl40x.cpp.

Referenced by dl403_init().

418 {
422 
423  diag_print (2,
424  "dl403_setClockCycles: [%d] cyc1=0x%04hx cyc2=0x%04hx cyc3=0x%04hx\n",
425  d, c1, c2, c3);
426 
427 }
void dl403_setClockRate ( struct dl403 d,
u_short  clk1,
u_short  clk2,
u_short  clk3 
)

Definition at line 407 of file dl40x.cpp.

Referenced by dl403_init().

408 {
409  diag_print (2, "dl403_setClockRate: [%d] rate=0x%02hx\n", d,
410  (clk1 | (clk2 << 2) | (clk3 << 4)));
411 
413  clk1 | (clk2 << 2) | (clk3 << 4));
414 }
INLINE void dl403_start ( struct dl403 d)

Definition at line 454 of file dl40x.cpp.

Referenced by dl403_init(), and dl40x_read().

455 {
457 }
INLINE void dl403_steadyMode ( struct dl403 d)

Definition at line 448 of file dl40x.cpp.

Referenced by dl403_init().

449 {
451 }
INLINE void dl403_stop ( struct dl403 d)

Definition at line 460 of file dl40x.cpp.

461 {
463 }
INT dl40x_bor ( )

Definition at line 59 of file dl40x.cpp.

References clk, crate_number, diag_print(), DL401, dl401_bor1(), DL401_MAXMODULES, dl403_init(), enabled, dl401::enabled, FALSE, odb_find_key(), odb_get_bool(), sprintf(), and SUCCESS.

60 {
61  /*
62  * Initialize DL403 clock generator.
63  */
64  if (dl403_init (&clk) != SUCCESS) {
65  return FE_ERR_HW;
66  }
67 
68  /*
69  * Initialize DL401 FADC modules.
70  */
71  for (int j = 0; j < DL401_MAXMODULES; j++)
72  {
73  sprintf (DL401[j].odb_name, "FADC %d", j);
74  DL401[j].enabled = FALSE;
75 
76  if (odb_find_key ("/Equipment/Crate %d/Settings/FADC %d", crate_number, j))
77  {
78  diag_print (1, "ODB says FADC #%d is present in crate 3, ", j);
79  DL401[j].enabled =
80  odb_get_bool ("/Equipment/Crate %d/Settings/FADC %d/enabled status",
81  crate_number, j);
82  if (DL401[j].enabled)
83  {
84  diag_print (1, "and is enabled. Initializing...\n");
85  dl401_bor1 (&DL401[j]);
86  }
87  else
88  {
89  diag_print (1, "but is disabled.\n");
90  }
91  }
92 
93  }
94 
95  return SUCCESS;
96 }
INT dl40x_eor ( )

Definition at line 99 of file dl40x.cpp.

References clk, DL401, DL401_MAXMODULES, enabled, dl403::enabled, SUCCESS, vme_close(), and dl403::vme_handle.

100 {
101  // release VME handles
102  if(clk.enabled) {
104  }
105 
106  for (int j = 0; j < DL401_MAXMODULES; j++) {
107  if(DL401[j].enabled) {
108  vme_close(DL401[j].a16_handle);
109  vme_close(DL401[j].a32_handle);
110  }
111  }
112 
113  return SUCCESS;
114 }
INT dl40x_read ( char *  pevent)

Definition at line 117 of file dl40x.cpp.

References clk, DL401, dl401_clear(), DL401_CNTMAX, dl401_finished(), DL401_MAXMODULES, dl401_read(), dl401_reset(), dl403_running(), dl403_start(), enabled, dl403::enabled, size, sprintf(), and SUCCESS.

118 {
119  if(!clk.enabled) {
120  return SUCCESS;
121  }
122 
123  // loop over FADC banks if the clock has stopped. Otherwise, they
124  // aren't part of the event, because they didn't get a trigger.
125  if (!dl403_running (&clk))
126  {
127  for (int j = 0; j < DL401_MAXMODULES; j++)
128  {
129  if (DL401[j].enabled)
130  {
131 #if 0
132  // wait for the ADCs to finish converting
133  while (!dl401_finished (&DL401[j]))
134  {
135  cm_yield(0);
136  }
137 #endif
138 
139  // perform readout
140  long *pdata;
141  char bk_name[8];
142  int size = DL401_CNTMAX;
143 
144  sprintf (bk_name, "ADC%d", j);
145  bk_create (pevent, bk_name, TID_DWORD, &pdata);
146  size = dl401_read (&DL401[j], (u_int *) pdata);
147  pdata += size;
148  bk_close (pevent, pdata);
149 
150  // Reset for next event
151  dl401_reset (&DL401[j]);
152  dl401_clear (&DL401[j]);
153  }
154  }
155  }
156 
157  // Restart FADCs for next event
158  dl403_start (&clk);
159 
160  return SUCCESS;
161 }

Variable Documentation

struct dl403 clk
static

Referenced by dl40x_bor(), dl40x_eor(), and dl40x_read().

struct dl401 DL401[DL401_MAXMODULES]
static

Referenced by dl40x_bor(), dl40x_eor(), and dl40x_read().

struct readout_module dl40x_module
Initial value:
= {
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
}

Definition at line 25 of file dl40x.cpp.