ICP  1
environment_card.cpp
Go to the documentation of this file.
1 #include "stdafx.h"
2 
3 #include "isisvme.h"
4 #include "environment_card.h"
5 
6 template <class EnvPeriodPolicy>
7 EnvironmentCard<EnvPeriodPolicy>::EnvironmentCard(int position, ISISVME* vme, DAEstatus& status) : DAE2Card(position, vme, status)
8 {
9  setLoggerName("EnvironmentCard");
10  std::ostringstream message;
11  isisU32_t value;
12  if (readRegister(FC0WINDLY, &value, status) == 1)
13  {
14  message << "This Card Supports Fast (Fermi) Chopper Vetos\n";
16  }
17  printStatus(message, status);
18  status.addInfo(FAC_ENVCARD, message.str());
19 }
20 
21 template <class EnvPeriodPolicy>
23 {
24  return setRegisterBits(RCONTROL, mask, preserve, status);
25 }
26 
27 template <class EnvPeriodPolicy>
29 {
30  return clearRegisterBits(RCONTROL, mask, status);
31 }
32 
33 template <class EnvPeriodPolicy>
35 {
36  return setAndClearRegisterBits(RCONTROL, mask, preserve, status);
37 }
38 
39 template <class EnvPeriodPolicy>
41 {
42  resetRunController(status);
43  return setRunControlBits(RCSTART, true, status);
44 }
45 
46 template <class EnvPeriodPolicy>
48 {
49  clearRunControlBits(RCSTART, status);
50  return resetRunController(status);
51 }
52 
53 template <class EnvPeriodPolicy>
55 {
56 // clearSMPVetoedFrames(status);
57 // clearExternalVetoedFrames(status);
58 // clearFIFOVetoedFrames(status);
59  return setAndClearRegisterBits(RCONTROL, RCFCLEAR | RCPCLEAR, true, status);
60 }
61 
62 template <class EnvPeriodPolicy>
64 {
65  return setAndClearRegisterBits(RCONTROL, RCPCLEAR, true, status);
66 }
67 
68 template <class EnvPeriodPolicy>
70 {
71  int stat = clearRunControlBits(RCFSSEL, status); // clear all FS bits first (sets to test clock)
72  switch(fs)
73  {
75  return stat;
76  break;
77 
78  case FrameSyncISIS:
79  return setRunControlBits(RCFSTOF, true, status);
80  break;
81 
83  return setRunControlBits(RCFSTOF1P, true, status);
84  break;
85 
86  case FrameSyncSMP:
87  return setRunControlBits(RCFSSMP, true, status);
88  break;
89 
90  default:
91  break;
92  }
93  status.addVa(FAC_DAE, SEV_ERROR, ERRTYPE_OUTOFMEM, "Invalid frame sync type %d\n", fs);
94  return DAEstatus::Failure;
95 }
96 
97 template <class EnvPeriodPolicy>
99 {
100  isisU32_t value;
101  readRegister(RCONTROL, &value, status);
102  if (value & RCSTART)
103  {
104  return true;
105  }
106  else
107  {
108  return false;
109  }
110 }
111 
112 template <class EnvPeriodPolicy>
114 {
115  isisU32_t value, value1, veto_reg;
116  DAE2Card::printStatus(os, status);
117  os << "This is an ENVIRONMENT card" << std::endl;
118  readRegister(RCONTROL, &value, status);
119  os << "RC register value = " << std::hex << "0x" << value << std::dec << std::endl;
120  os << "Status: " << (value & RCSTART ? "Running" : "Stopped") << std::endl;
121  os << "Frame Sync: " << ( value & RCFSSEL ? "External" : "Internal" ) << std::endl;
122  os << "Delayed start: " << (value & RCPERSZEQZERO ? "disabled" : "enabled") << std::endl;
123  getFrameSyncDelay(&value1, status);
124  os << "Frame sync delay: " << value1 << "\n";
125  readRegister(RVETO, &veto_reg, status);
126  printVetoDetails(os, status);
127  getGoodFrames(&value1, status);
128  getRawFrames(&value, status);
129  os << "GOOD/RAW frames: " << value1 << "/" << value << "\n";
130  os << "GOOD/RAW uamph: " << getGoodUAmpHours(status) << "/" << getRawUAmpHours(status) << "\n";
131  os << "Period Type: " << (usingHardwarePeriods(status) ? "Hardware" : "Software") << std::endl;
132 // print period type
133 }
134 
135 //int EnvironmentCard<EnvPeriodPolicy>::getRawFramesLower(isisU16_t* value, DAEstatus& status)
136 //{
137 // readRegister(FCOUNTR0, value, status);
138 // return 0;
139 //}
140 
141 //int EnvironmentCard<EnvPeriodPolicy>::getRawFramesUpper(isisU16_t* value, DAEstatus& status)
142 //{
143 // readRegister(FCOUNTR1, value, status);
144 // return 0;
145 //}
146 
147 template <class EnvPeriodPolicy>
149 {
150  return readRegister(FCOUNTR, value, status);
151 }
152 
153 //int EnvironmentCard<EnvPeriodPolicy>::getGoodFramesLower(isisU16_t* value, DAEstatus& status)
154 //{
155 // readRegister(FCOUNTG0, value, status);
156 // return 0;
157 //}
158 
159 //int EnvironmentCard<EnvPeriodPolicy>::getGoodFramesUpper(isisU16_t* value, DAEstatus& status)
160 //{
161 // readRegister(FCOUNTG1, value, status);
162 // return 0;
163 //}
164 
165 template <class EnvPeriodPolicy>
167 {
168  return readRegister(FCOUNTG, value, status);
169 }
170 
171 template <class EnvPeriodPolicy>
173 {
174  return readRegister(PCOUNTR0, value, status);
175 }
176 
177 template <class EnvPeriodPolicy>
179 {
180  isisU32_t value1 = 0;
181  readRegister(PCOUNTR1, &value1, status);
182  *value = value1 & 0xffff;
183  return 0;
184 }
185 
186 template <class EnvPeriodPolicy>
188 {
189  isisU32_t value0 = 0, value1 = 0;
190  readRegister(PCOUNTR0, &value0, status);
191  readRegister(PCOUNTR1, &value1, status);
192  *value = (isisU64_t)value0 | ((isisU64_t)value1 << 32);
193  return 0;
194 }
195 
196 
197 template <class EnvPeriodPolicy>
199 {
200  return readRegister(PCOUNTG0, value, status);
201 }
202 
203 template <class EnvPeriodPolicy>
205 {
206  isisU32_t value1 = 0;
207  readRegister(PCOUNTG1, &value1, status);
208  *value = value1 & 0xffff;
209  return 0;
210 }
211 
212 template <class EnvPeriodPolicy>
214 {
215  isisU32_t value0 = 0, value1 = 0;
216  readRegister(PCOUNTG0, &value0, status);
217  readRegister(PCOUNTG1, &value1, status);
218  *value = (isisU64_t)value0 | ((isisU64_t)value1 << 32);
219  return 0;
220 }
221 
222 template <class EnvPeriodPolicy>
224 {
225  return setAndClearRunControlBits(RCRESET, true, status);
226 }
227 
228 template <class EnvPeriodPolicy>
230 {
231  return setVetoRegisterBits(RVSMP, true, status);
232 }
233 
234 template <class EnvPeriodPolicy>
236 {
237  switch(veto_number)
238  {
239  case 0:
240  return setVetoRegisterBits(RVEXT0, true, status);
241  break;
242  case 1:
243  return setVetoRegisterBits(RVEXT1, true, status);
244  break;
245  case 2:
246  return setVetoRegisterBits(RVEXT2, true, status);
247  break;
248  case 3:
249  return setVetoRegisterBits(RVEXT3, true, status);
250  break;
251  default:
252  status.addWarningVa(FAC_DAE, "Attempting to enable invalid veto number %d", veto_number);
253  break;
254  }
255  return DAEstatus::Success;
256 }
257 
258 template <class EnvPeriodPolicy>
260 {
261  return setVetoRegisterBits(RVINT, true, status);
262 }
263 
264 template <class EnvPeriodPolicy>
266 {
267  return setVetoRegisterBits(RVFIFO, true, status);
268 }
269 
270 template <class EnvPeriodPolicy>
272 {
273  return setVetoRegisterBits(RVTS2P, true, status);
274 }
275 
276 template <class EnvPeriodPolicy>
278 {
279  return setVetoRegisterBits(RVHZ50, true, status);
280 }
281 
282 template <class EnvPeriodPolicy>
284 {
285  return clearVetoRegisterBits(RVSMP, status);
286 }
287 
288 template <class EnvPeriodPolicy>
290 {
291  switch(veto_number)
292  {
293  case 0:
294  return clearVetoRegisterBits(RVEXT0, status);
295  break;
296  case 1:
297  return clearVetoRegisterBits(RVEXT1, status);
298  break;
299  case 2:
300  return clearVetoRegisterBits(RVEXT2, status);
301  break;
302  case 3:
303  return clearVetoRegisterBits(RVEXT3, status);
304  break;
305  default:
306  status.addWarningVa(FAC_DAE, "Attempting to disable invalid veto number %d", veto_number);
307  break;
308  }
309  return DAEstatus::Success;
310 }
311 
312 template <class EnvPeriodPolicy>
314 {
315  return clearVetoRegisterBits(RVINT, status);
316 }
317 
318 template <class EnvPeriodPolicy>
320 {
321  return clearVetoRegisterBits(RVFIFO, status);
322 }
323 
324 template <class EnvPeriodPolicy>
326 {
327  return clearVetoRegisterBits(RVTS2P, status);
328 }
329 
330 template <class EnvPeriodPolicy>
332 {
333  return clearVetoRegisterBits(RVHZ50, status);
334 }
335 
336 template <class EnvPeriodPolicy>
338 {
339  return readRegister(SMPVETFRM, value, status);
340 }
341 
342 template <class EnvPeriodPolicy>
344 {
345 // return readRegister(EXTVETFRMTOT, value, status);
346  switch(veto_number)
347  {
348  case 0:
349  return readRegister(EXT0VETFRM, value, status);
350  break;
351  case 1:
352  return readRegister(EXT1VETFRM, value, status);
353  break;
354  case 2:
355  return readRegister(EXT2VETFRM, value, status);
356  break;
357  case 3:
358  return readRegister(EXT3VETFRM, value, status);
359  break;
360  default:
361  status.addWarningVa(FAC_DAE, "Attempting to read invalid veto number %d", veto_number);
362  break;
363  }
364  return DAEstatus::Success;
365 }
366 
367 template <class EnvPeriodPolicy>
369 {
370  return readRegister(FIFOVETFRM, value, status);
371 }
372 
373 template <class EnvPeriodPolicy>
375 {
376  return readRegister(TS2PVETFRM, value, status);
377 }
378 
379 template <class EnvPeriodPolicy>
381 {
382  return readRegister(HZ50VETFRM, value, status);
383 }
384 
385 template <class EnvPeriodPolicy>
387 {
388  isisU32_t value = 0;
389  writeRegister(SMPVETFRM, value, status);
390  return 0;
391 }
392 
393 template <class EnvPeriodPolicy>
395 {
396  isisU32_t value = 0;
397  writeRegister(EXT0VETFRM, value, status);
398  return 0;
399 }
400 
401 template <class EnvPeriodPolicy>
403 {
404  isisU32_t value = 0;
405  writeRegister(FIFOVETFRM, value, status);
406  return 0;
407 }
408 
409 template <class EnvPeriodPolicy>
411 {
412  if (value == 0)
413  {
414  value = 1; // probably a muon DAE
415  }
416  if ((value >> 19) != 0)
417  {
418  status.add(FAC_ENVCARD, SEV_ERROR, ERRTYPE_INVCARD, "Frame sync delay too big (> 18 bits wide)");
419  }
420  clearRunControlBits(RCFSENABLEOUT, status);
421  setAndClearRunControlBits(RCDELFSFIFORS, true, status);
422  writeRegister(FSDELAY, value, status);
423  setRunControlBits(RCFSENABLEOUT, true, status);
424  return 0;
425 }
426 
427 template <class EnvPeriodPolicy>
429 {
430  return readRegister(FSDELAY, value, status);
431 }
432 
433 template <class EnvPeriodPolicy>
435  {
436  return clearRunControlBits(RCPERSZEQZERO, status);
437  }
438 
439 template <class EnvPeriodPolicy>
441  {
442  return setRunControlBits(RCPERSZEQZERO, true, status);
443  }
444 
445 template <class EnvPeriodPolicy>
447  {
448  isisU32_t value;
449  readRegister(RCONTROL, &value, status);
450  if (value & RCPERSZEQZERO)
451  {
452  return false;
453  }
454  else
455  {
456  return true;
457  }
458  }
459 
460 // converion factor is
461 //
462 // 4e13 / 1024 * 1.602e-19 * 1e6 / 3600 = 1.738e-6
463 //
464 // 4e13/1024 is a calibration: 1024 for 10 bit ADC, 4e13 for full load proton reading
465 //
466 
467 #define PPP_TO_UAMPH 1.738E-6
468 
469 template <class EnvPeriodPolicy>
471 {
472  float good_uamps;
473  isisU32_t low = 0, upper = 0;
474  getGoodPPPLower(&low, status);
475  getGoodPPPUpper(&upper, status);
476  good_uamps = static_cast<float>(((65536.0 * 65536.0 * upper) + low) * PPP_TO_UAMPH);
477  return good_uamps;
478 }
479 
480 template <class EnvPeriodPolicy>
482 {
483  float raw_uamps;
484  isisU32_t low = 0, upper = 0;
485  getRawPPPLower(&low, status);
486  getRawPPPUpper(&upper, status);
487  raw_uamps = static_cast<float>(((65536.0 * 65536.0 * upper) + low) * PPP_TO_UAMPH);
488  return raw_uamps;
489 }
490 
491 // delay and width in 20ns steps
492 template <class EnvPeriodPolicy>
493 int EnvironmentCard<EnvPeriodPolicy>::enableFChopperVeto(int chopper_number, int delay, int width, DAEstatus& status)
494 {
495  static const isisU32_t delay_mapping[] = { FC0WINDLY, FC1WINDLY, FC2WINDLY, FC3WINDLY };
496  static const isisU32_t window_mapping[] = { FC0WINWTH, FC1WINWTH, FC2WINWTH, FC3WINWTH };
497  static const isisU32_t veto_mapping[] = { RVFCHOP0, RVFCHOP1, RVFCHOP2, RVFCHOP3 };
498 
500  {
501  writeRegister(delay_mapping[chopper_number], delay, status);
502  writeRegister(window_mapping[chopper_number], width, status);
503  status.addInfoVa(FAC_DAE, "Fermi chopper %d delay,width (us) = %f,%f\n", chopper_number, delay / 50.0, width / 50.0);
504  return setVetoRegisterBits(veto_mapping[chopper_number], true, status);
505  }
506  else
507  {
508  status.add(FAC_DAE, SEV_ERROR, ERRTYPE_OUTOFMEM, "Fermi Chopper Veto Not Available");
509  return DAEstatus::Failure;
510  }
511 }
512 
513 template <class EnvPeriodPolicy>
515 {
516  static const isisU32_t veto_mapping[] = { RVFCHOP0, RVFCHOP1, RVFCHOP2, RVFCHOP3 };
518  {
519  return clearVetoRegisterBits(veto_mapping[chopper_number], status);
520  }
521  else
522  {
523  return DAEstatus::Success;
524  }
525 }
526 
527 template <class EnvPeriodPolicy>
529 {
530  *value = 0;
531  static const isisU32_t mapping[] = { FC0VETFRM, FC1VETFRM, FC2VETFRM, FC3VETFRM };
533  {
534  readRegister(mapping[chopper_number], value, status);
535  }
536  return DAEstatus::Success;
537 }
538 
539 template <class EnvPeriodPolicy>
541 {
542  return setRegisterBits(RVETO, mask, preserve, status);
543 }
544 
545 template <class EnvPeriodPolicy>
547 {
548  return clearRegisterBits(RVETO, mask, status);
549 }
550 
551 template <class EnvPeriodPolicy>
553 {
554  int i;
555  isisU32_t veto_reg, frames;
556  readRegister(RVETO, &veto_reg, status);
557  for(i=0; veto_details[i].name != NULL; i++)
558  {
559  if (!veto_details[i].pc_only)
560  {
561  readRegister(veto_details[i].counter_addr, &frames, status);
562  os << veto_details[i].name << " is " << ((veto_reg & veto_details[i].mask) != 0 ? "ENABLED (vetoed " : "DISABLED (counted ") << frames << ") frames\n";
563  }
564  }
565  return DAEstatus::Success;
566 }
567 
568 template <class EnvPeriodPolicy>
570 {
571  int i;
572  isisU32_t veto_reg = 0, rf1 = 0, rf2 = 0;
573  double rfdiff;
574  int poll_time = 5; // in seconds
575  int nveto;
576  for(nveto = 0; veto_details[nveto].name != NULL; ++nveto)
577  ;
578  isisU32_t* vframes1 = new isisU32_t[nveto];
579  isisU32_t* vframes2 = new isisU32_t[nveto];
580  readRegister(RVETO, &veto_reg, status);
581  getRawFrames(&rf1, status);
582  for(i=0; i < nveto; i++)
583  {
584  if (!veto_details[i].pc_only)
585  {
586  vframes1[i] = 0;
587  readRegister(veto_details[i].counter_addr, &(vframes1[i]), status);
588  }
589  }
590  Sleep(poll_time * 1000);
591  for(i=0; i < nveto; i++)
592  {
593  if (!veto_details[i].pc_only)
594  {
595  vframes2[i] = 0;
596  readRegister(veto_details[i].counter_addr, &(vframes2[i]), status);
597  }
598  }
599  getRawFrames(&rf2, status);
600  rfdiff = rf2 - rf1;
601  for(i=0; i < nveto; i++)
602  {
603  if ( ((veto_reg & veto_details[i].mask) != 0) && !veto_details[i].pc_only )
604  {
605  os << veto_details[i].name << " vetoed ";
606  if (rfdiff > 0.0)
607  {
608  os << (int)(100.0 * (vframes2[i] - vframes1[i]) / rfdiff);
609  }
610  else
611  {
612  os << "<unknown>";
613  }
614  os << "% (last " << poll_time << " seconds), ";
615  if (rf2 > 0)
616  {
617  os << (int)(100.0 * vframes2[i] / rf2);
618  }
619  else
620  {
621  os << "<unknown>";
622  }
623  os << "% (since run start)\n";
624  }
625  }
626  delete[] vframes1;
627  delete[] vframes2;
628  return DAEstatus::Success;
629 }
630 
631 template <class EnvPeriodPolicy>
633 {
634  LOGSTR_INFORMATION("Nothing to do");
635  return ISISVME::Success;
636 }
637 
640 
int ClearFramesAndPPP(DAEstatus &status)
#define FAC_DAE
int getISIS50HzVetoedFrames(isisU32_t *value, DAEstatus &status)
#define ERRTYPE_OUTOFMEM
int clearSMPVetoedFrames(DAEstatus &status)
float getGoodUAmpHours(DAEstatus &status)
int clearPPP(DAEstatus &status)
int addVa(int facility, int severity, int errtype, const char *format,...)
Definition: DAEstatus.cpp:54
int getFIFOVetoedFrames(isisU32_t *value, DAEstatus &status)
float getRawUAmpHours(DAEstatus &status)
int clearRunControlBits(isisU32_t mask, DAEstatus &status)
int disableISIS50HzVeto(DAEstatus &status)
int enableInternalVeto(DAEstatus &status)
int getTS2PulseVetoedFrames(isisU32_t *value, DAEstatus &status)
EnvironmentCard(int position, ISISVME *vme, DAEstatus &status)
bool isRunning(DAEstatus &status)
int getGoodFrames(isisU32_t *value, DAEstatus &status)
int disableSMPVeto(DAEstatus &status)
int setRunControlBits(isisU32_t mask, bool preserve, DAEstatus &status)
int enableFIFOVeto(DAEstatus &status)
unsigned long isisU32_t
Definition: isisvme_types.h:8
static const int Failure
Definition: DAEstatus.h:141
int setAndClearRunControlBits(isisU32_t mask, bool preserve, DAEstatus &status)
int add(DAEstatus &dstatus, bool clear)
Definition: DAEstatus.cpp:131
bool usingHardwarePeriods(DAEstatus &status)
int getGoodPPP(isisU64_t *value, DAEstatus &status)
int getFChopperVetoedFrames(int chopper_number, isisU32_t *value, DAEstatus &status)
static const int Success
Definition: DAEstatus.h:140
int getRawFrames(isisU32_t *value, DAEstatus &status)
virtual void printStatus(std::ostream &os, DAEstatus &dstatus)
Definition: dae2_card.cpp:453
int disableTS2PulseVeto(DAEstatus &status)
int clearExternalVetoedFrames(DAEstatus &status)
#define PPP_TO_UAMPH
CardOptions m_options
int getRawPPPUpper(isisU32_t *value, DAEstatus &status)
int disableExternalVeto(int veto_number, DAEstatus &status)
#define SEV_ERROR
int disableFIFOVeto(DAEstatus &status)
int whichVeto(std::ostream &os, DAEstatus &status)
int startRun(DAEstatus &status)
#define LOGSTR_INFORMATION(__arg)
Definition: IsisBase.h:78
int readRegister(unsigned long address, isisU32_t *value, DAEstatus &status, bool little_endian=false, bool retry=true)
int clearVetoRegisterBits(isisU32_t mask, DAEstatus &status)
virtual int resetCardState(DAEstatus &status)
void printStatus(std::ostream &os, DAEstatus &status)
int setVetoRegisterBits(isisU32_t mask, bool preserve, DAEstatus &status)
int enableSMPVeto(DAEstatus &status)
FrameSync
Definition: isiscrpt_types.h:5
int printVetoDetails(std::ostream &os, DAEstatus &status)
int addWarningVa(int facility, const char *format,...)
Definition: DAEstatus.cpp:106
int disableFChopperVeto(int chopper_number, DAEstatus &status)
int getSMPVetoedFrames(isisU32_t *value, DAEstatus &status)
int getGoodPPPUpper(isisU32_t *value, DAEstatus &status)
int setFrameSync(FrameSync fs, DAEstatus &status)
int disableInternalVeto(DAEstatus &status)
int getGoodPPPLower(isisU32_t *value, DAEstatus &status)
ULONG64 isisU64_t
Definition: isisvme_types.h:10
int enableISIS50HzVeto(DAEstatus &status)
int clearFIFOVetoedFrames(DAEstatus &status)
int resetRunController(DAEstatus &status)
#define ERRTYPE_INVCARD
int getExternalVetoedFrames(int veto_number, isisU32_t *value, DAEstatus &status)
int getRawPPPLower(isisU32_t *value, DAEstatus &status)
#define FAC_ENVCARD
int getFrameSyncDelay(isisU32_t *value, DAEstatus &status)
void setLoggerName(const std::string &logger_name)
Definition: IsisBase.h:17
int enableFChopperVeto(int chopper_number, int delay, int width, DAEstatus &status)
int enableExternalVeto(int veto_number, DAEstatus &status)
int disableHardwarePeriods(DAEstatus &status)
int addInfo(int facility, const std::string &text)
Definition: DAEstatus.cpp:86
int setFrameSyncDelay(isisU32_t value, DAEstatus &status)
int enableTS2PulseVeto(DAEstatus &status)
int stopRun(DAEstatus &status)
int addInfoVa(int facility, const char *format,...)
Definition: DAEstatus.cpp:91
int enableHardwarePeriods(DAEstatus &status)
int getRawPPP(isisU64_t *value, DAEstatus &status)