AlcapDAQ  1
lrs2249.cpp
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 
5 #include <sys/time.h>
6 #include <time.h>
7 
8 #include "midas.h"
9 
10 #include "crate.h"
11 #include "mcstd.h"
12 #include "odb_wrapper.h"
13 #include "camac_wrapper.h"
14 #include "diag.h"
15 
16 INT lrs2249_bor();
17 INT lrs2249_read(char *pevent);
18 
20  NULL, // init
21  NULL, // exit
22  NULL, // pre_bor
23  lrs2249_bor, // bor
24  NULL, // eor
25  NULL, // poll_live
26  NULL, // poll_dead
27  NULL, // start_cycle
28  NULL, // stop_cycle
29  lrs2249_read, // read
30 };
31 
32 struct lrs2249 {
33  BOOL enabled;
34  int crate;
35  int slot;
36  char odb_name[20];
37  char bank_name[20];
38 };
39 
40 #define MAX_LRS2249 8
42 
43 #define N_CH_ADC 12
44 
45 /*
46  * lrs2249_bor1
47  *
48  * Initialize one LRS2249 module.
49  */
50 INT lrs2249_bor1(struct lrs2249 *adc)
51 {
52  // Get the address of the module
53  adc->crate = odb_get_int("/Equipment/Crate %d/Settings/%s/Crate",
54  crate_number, adc->odb_name);
55  adc->slot = odb_get_int("/Equipment/Crate %d/Settings/%s/Slot",
56  crate_number, adc->odb_name);
57 
58  cam_init_once();
59 
60  camc(adc->crate, adc->slot, 0, 9);
61 
62  return SUCCESS;
63 }
64 
65 /*
66  * lrs2249_bor
67  *
68  * Called at the beginning of the run to discover LRS2249 modules
69  * and initialize them.
70  */
72 {
73  // Use the ODB to find any LRS2249 modules
74  for(int j = 0; j < MAX_LRS2249; j++) {
75 
76  bool enabled = false;
77 
78  if (odb_find_key("/Equipment/Crate %d/Settings/LRS2249 %d", crate_number, j)) {
79  diag_print(1, "ODB says LRS2249 %d is present, ", j);
80  enabled =
81  odb_get_bool("/Equipment/Crate %d/Settings/LRS2249 %d/enabled status",
82  crate_number, j);
83  if (enabled) {
84  diag_print(1, "and is enabled. Initializing...\n");
85  } else {
86  diag_print(1, "but is disabled.\n");
87  }
88  }
89 
90  lrs2249[j].enabled = enabled;
91 
92  // Set up the name of the MIDAS bank associated with the module
93  sprintf(lrs2249[j].bank_name, "LAD%d", j);
94  sprintf(lrs2249[j].odb_name, "LRS2249 %d", j);
95 
96  if(enabled) {
97  lrs2249_bor1(&lrs2249[j]);
98  }
99  }
100 
101  return SUCCESS;
102 }
103 
104 /*
105  * lrs2249_read1
106  *
107  * Constructs the MIDAS bank for a single LRS2249.
108  */
109 INT lrs2249_read1(struct lrs2249 *adc, char *pevent)
110 {
111  // Check whether the ADC has converted; if not, skip it
112  INT q;
113  camc_q(adc->crate, adc->slot, 0, 8, &q);
114  if(!q) return SUCCESS;
115 
116  // Create the MIDAS bank
117  WORD *pdata;
118  bk_create(pevent, adc->bank_name, TID_WORD, &pdata);
119 
120  // Read ADC
121  for(int i = 0; i < N_CH_ADC; i++) {
122  cami(adc->crate, adc->slot, i, 0, pdata++);
123  }
124 
125  // Clear ADC
126  camc(adc->crate, adc->slot, 0, 9);
127 
128  // Close the bank
129  bk_close(pevent, pdata);
130 
131  return SUCCESS;
132 }
133 
134 /*
135  * lrs2249_read
136  *
137  * Called at the end of a block to assemble data from that block into a
138  * MIDAS event.
139  */
140 INT lrs2249_read(char *pevent)
141 {
142  for(int i = 0; i < MAX_LRS2249; i++) {
143  if(lrs2249[i].enabled) {
144  int status = lrs2249_read1(&lrs2249[i], pevent);
145  if(status != SUCCESS) {
146  return status;
147  }
148  }
149  }
150 
151  return SUCCESS;
152 }