Cat
ICECALv3.h
Go to the documentation of this file.
1 //$Id: ICECALv3.cpp,v 1.0 2013/11/11 13:11:24 jmauricio Exp $
2 //------------------------------------------------------------------------------
3 //
4 // Package : ICECALv3
5 //
6 // Description:
7 //
8 // Author(s) : Joan Mauricio
9 // Date : 2013/11/11
10 //
11 //------------------------------------------------------------------------------
12 
13 #ifndef __ICECALv3_H_
14 #define __ICECALv3_H_
15 
16 #include "Element.h"
17 #include "Register.h"
18 #include <stdlib.h>
19 #include <string>
20 #include <python2.7/Python.h>
21 #include <iostream>
22 #include <fstream>
23 #include <time.h>
24 
25 using namespace std;
26 
27 #define CALIBRATION_FILE_VER "0.99"
28 #define SPI_N_RETRIES 2
29 
30 
31 //Commands:
32 #define SC_SOFT_RST 0x40
33 #define SC_MOSI_MISO_BYPASS 0xFF
34 
35 //Status addresses
36 #define ICECAL_VERSION_ADDR 0xB0
37 #define BXID_RESYNCH_ADDR 0xB1
38 
39  //Data:
40  //Configuration Data//
41  //SPI @ of the delay line channels.
42  const int delayLineAddrCh[4] = { 28, 29, 30, 31 };
43 
44  //SPI @ of ICECAL analog channels.
45  const int icecalLSBAddrCh[4] = { 0, 1, 2, 3 };
46 
47  const int icecalMSBAddrCh[4] = { 4, 5, 6, 7 };
48 
49  //SPI @ of the Main ICECAL chip registers.
50  const int icecalMainAddr[4] = { 8, 9, 10, 11 };
51 
52 
53  typedef unsigned long U32;
54  typedef unsigned short U16;
55  typedef unsigned char U8;
56 
57 
58 class ICECALv3 : public Element {
59 public:
60 
61  //Constructor
62  ICECALv3();
63 
64  //Parameter settings...
65  void setAddress (U8 address) { confReg->setAddress(address); }
66  void setNRetries(int nRet ) { nRetries = nRet; }
67 
68  //Slow Control Commands:
69  void resetPumps(){ spiWrite(SC_SOFT_RST,0); };
70  void bypassMisoMosi(U16 writeData){ spiWrite(SC_MOSI_MISO_BYPASS,writeData); };
71 
72  //Slow Control Data:
73  //Functions to Read/Write data from delay line registers...
74  PyObject* getDelayLineCh(int ch);
75  StatusCode setDelayLineCh(int ch, PyObject*);
76 
77  //Functions to Read/Write data from ICECAL Analog channels...
78  PyObject* getAnalogCh(int ch);
79  StatusCode setAnalogCh(int ch, PyObject*);
80 
81  //Functions to Read/Write data from ICECAL Main Register...
82  PyObject* getMainReg();
83  StatusCode setMainReg(PyObject*);
84 
85  //FER (Frame Error Rate) Test. For each of the 'nTest', all the configuration registers will be checked.
86  PyObject* spiFERTest(long nTest);
87 
88  //Stores the current ASIC configuration into a text file.
89  StatusCode dumpConfig(string configFile, PyObject* chipId);
90 
91  //Loads the configuration from a file.
92  PyObject* loadConfig(string configFile);
93 
94  double version();
95  bool bxidResynchStatus();
96  void spiAddressScan();
97 
99  }
100 
104  void help() { info("ICECALv3 "+name()+". No help.","ICECALv3::help"); };
105 
112  return StatusCode::SUCCESS;
113  };
114 
122  void reset();
123 
127  void update () {
128 
129  };
130 
131 private:
132 
133  //Pointer where ICECAL configuration registers are mapped.
134  Register* confReg;
135 
136  //Number of attempts that we will perform before considering TX ERROR.
137  int nRetries;
138 
139  bool err;
140 
141  //Low-level read/write functions.
142  U16 spiRead (U8 confRegAddr, U8 offsetAddr=128);
143  void spiWrite (U8 confRegAddr, U16 confRegData);
144 
145  //Safe SPI Write function: write + readback + retry (if needed).
146  bool spiWriteSafe(U8 confRegAddr, U16 confRegData);
147 
148  //Writes the obtained parameters from configuration register(s).
149  bool writeAsicParams(string fileName, PyObject* params);
150 
151  //Generates a python list of parameter values to configure electronics.
152  int parseParameterList(string configFile, string paramName[64], int paramValue[64]);
153  PyObject* fillParams(string paramListRet[],int paramListLen,string paramName[64],int paramValue[64],int fileParamLen);
154 
155  bool checkChNumber(int ch)
156  {
157  if(ch < 0 || ch > 3) {
158  error(itos(ch)+" is not a valid ICECAL channel number. Valid numbers [0-3].","ICECALv3::checkChNumber");
159  return false;
160  }
161  else return true;
162  }
163 
164  std::string itohs(int value)
165  {
166  char buff[32];
167  if (value < 256) sprintf(buff,"%02X",value);
168  else if (value < 65536) sprintf(buff,"%04X",value);
169  else sprintf(buff,"%08X",value);
170  return std::string(buff);
171  }
172 };
173 
174 #endif
175 
int nRetries
Definition: ICECALv3.h:137
std::string itos(int)
Definition: Tools.cpp:46
StatusCode init()
Definition: ICECALv3.h:111
bool checkChNumber(int ch)
Definition: ICECALv3.h:155
Register * confReg
Definition: ICECALv3.h:129
const int icecalMSBAddrCh[4]
Definition: ICECALv3.h:47
void setAddress(U8 address)
Definition: ICECALv3.h:65
const int icecalMainAddr[4]
Definition: ICECALv3.h:50
void setNRetries(int nRet)
Definition: ICECALv3.h:66
void bypassMisoMosi(U16 writeData)
Definition: ICECALv3.h:70
unsigned char U8
Definition: ICECALv3.h:55
const int icecalLSBAddrCh[4]
Definition: ICECALv3.h:45
~ICECALv3()
Definition: ICECALv3.h:98
void update()
Definition: ICECALv3.h:127
void resetPumps()
Definition: ICECALv3.h:69
std::string itohs(int value)
Definition: ICECALv3.h:164
const int delayLineAddrCh[4]
Definition: ICECALv3.h:42
#define SC_MOSI_MISO_BYPASS
Definition: ICECALv3.h:33
unsigned long U32
Definition: ICECALv3.h:53
#define SC_SOFT_RST
Definition: ICECALv3.h:32
void help()
Definition: ICECALv3.h:104
unsigned short U16
Definition: ICECALv3.h:54
def reset(path='')
Definition: shell.py:212
bool err
Definition: ICECALv3.h:139