AlcapDAQ  1
s500.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 "diag.h"
14 
15 INT s500_bor();
16 INT s500_read(char *pevent);
17 
19  NULL, // init
20  NULL, // exit
21  NULL, // pre_bor
22  s500_bor, // bor
23  NULL, // eor
24  NULL, // poll_live
25  NULL, // poll_dead
26  NULL, // start_cycle
27  NULL, // stop_cycle
28  s500_read, // read
29 };
30 
31 #define N_CH_S500 6
32 
33 struct s500 {
34  BOOL enabled;
35  int crate;
36  int slot;
37  char odb_name[20];
39 };
40 
41 #define MAX_S500 8
42 struct s500 s500[MAX_S500];
43 
44 /*
45  * s500_bor1
46  *
47  * Initialize one S500 module.
48  */
49 INT s500_bor1(struct s500 *s500)
50 {
51  // Get the VME base address of the module
52  s500->crate = odb_get_int("/Equipment/Crate %d/Settings/%s/Crate",
53  crate_number, s500->odb_name);
54  s500->slot = odb_get_int("/Equipment/Crate %d/Settings/%s/Slot",
55  crate_number, s500->odb_name);
56 
57  // clear all channels
58  for(int i = 0; i < N_CH_S500; i++) {
59  camo(s500->crate, s500->slot, i, 9, 0);
60  }
61 
62  // clear the accumulated values
63  for(int i = 0; i < N_CH_S500; i++) {
64  s500->acum[i] = 0;
65  }
66 
67  return SUCCESS;
68 }
69 
70 /*
71  * s500_bor
72  *
73  * Called at the beginning of the run to discover S500 modules
74  * and initialize them.
75  */
76 INT s500_bor()
77 {
78  // Use the ODB to find any S500 modules
79  for(int j = 0; j < MAX_S500; j++) {
80 
81  bool enabled = false;
82 
83  if (odb_find_key("/Equipment/Crate %d/Settings/S500 %d", crate_number, j)) {
84  diag_print(1, "ODB says S500 %d is present, ", j);
85  enabled =
86  odb_get_bool("/Equipment/Crate %d/Settings/S500 %d/enabled status",
87  crate_number, j);
88  if (enabled) {
89  diag_print(1, "and is enabled. Initializing...\n");
90  } else {
91  diag_print(1, "but is disabled.\n");
92  }
93  }
94 
95  s500[j].enabled = enabled;
96 
97  // Set up the name of the MIDAS bank associated with the module
98  sprintf(s500[j].odb_name, "S500 %d", j);
99 
100  if(enabled) {
101  s500_bor1(&s500[j]);
102  }
103  }
104 
105  return SUCCESS;
106 }
107 
108 /*
109  * s500_read
110  *
111  * Called at the end of a block to assemble data from that block into a
112  * MIDAS event.
113  */
114 INT s500_read(char *pevent)
115 {
116  DWORD *pdata;
117 
118  // first fill in the raw values
119  bk_create(pevent, "S500", TID_DWORD, &pdata);
120  for(int i = 0; i < MAX_S500; i++) {
121  if(s500[i].enabled) {
122  for(int j = 0; j < N_CH_S500; j++) {
123  DWORD val;
124  cam24i(s500[i].crate, s500[i].slot, j, 2, &val);
125  *pdata++ = val;
126  s500[i].acum[j] += val;
127  }
128  }
129  }
130  bk_close(pevent, pdata);
131 
132  // now the accumulated values
133  bk_create(pevent, "ACUM", TID_DWORD, &pdata);
134  for(int i = 0; i < MAX_S500; i++) {
135  if(s500[i].enabled) {
136  for(int j = 0; j < N_CH_S500; j++) {
137  *pdata++ = s500[i].acum[j];
138  }
139  }
140  }
141  bk_close(pevent, pdata);
142 
143  return SUCCESS;
144 }