source: hvcontrol/src/HV.cc@ 254

Last change on this file since 254 was 230, checked in by ogrimm, 14 years ago
Command reception logged by drsdaq and hvcontrol
File size: 5.7 KB
Line 
1
2/********************************************************************\
3
4 Name: HV.cc
5
6 Created by: Sebastian Commichau, November 2008
7 commichau@phys.ethz.ch
8
9 Contents: Main class for HV supply
10
11\********************************************************************/
12
13#include "HV.h"
14#include "ProcessIO.h" // Must be not in HV.h to avoid problem with declaring class ProcessIO
15
16using namespace std;
17
18// Constructor
19HVBoard::HVBoard(int DeviceNumber, string Name, class ProcessIO *PIO) {
20
21 struct termios tio;
22
23 m = PIO;
24 SetTimeOut(m->fTimeOut);
25 BoardNumber = DeviceNumber;
26 BoardName = new char [Name.size()+1];
27 strcpy(BoardName, Name.c_str());
28
29 // Create DIM services
30 stringstream ID;
31 ID << setfill('0') << setw(2) << BoardNumber;
32
33 NameService = new DimService ((SERVER_NAME"/NAME/ID"+ID.str()).c_str(), BoardName);
34 BiasVolt = new DimService ((char *) (SERVER_NAME"/VOLT/ID"+ID.str()).c_str(), (char *) "D", HVV, NUM_CHAINS*NUM_CHANNELS*sizeof(double));
35
36 // Initialise board variables
37 for (int i=0; i<NUM_CHAINS; i++) Overcurrent[i] = false;
38 ResetButton = false;
39 WrapOK = true;
40 LastWrapCount = -1;
41 ErrorCount = 0;
42
43 ClearVoltageArrays();
44
45 // Open device
46 if ((fDescriptor = open(("/dev/"+Name).c_str(), O_RDWR|O_NOCTTY|O_NDELAY)) == -1) {
47 if(errno != 2) m->PrintMessage("Error: Could not open device %d/%s (%s)\n", DeviceNumber, Name.c_str(), strerror(errno));
48 return;
49 }
50
51 // Get current serial port settings
52 if (tcgetattr(fDescriptor, &tio) == -1) {
53 m->PrintMessage("Error: tcgetattr() failed (%d/%s)\n", errno, strerror(errno));
54 return;
55 }
56
57 // Set baudrate and raw mode
58 if (cfsetspeed(&tio, BAUDRATE) == -1) m->PrintMessage("Error: Could not set baud rate (%s)\n", strerror(errno));
59 cfmakeraw(&tio);
60 if (tcsetattr(fDescriptor, TCSANOW, &tio ) == -1) m->PrintMessage("Error: tcsetattr() failed (%s)\n", strerror(errno));
61
62 // Synchronize HV board (if fails, closes device and sets fDescriptor to -2)
63 unsigned char wbuf = REG_STATUS;
64 int trial = 0, ret;
65
66 while(++trial<=3) {
67 if((ret = Communicate(&wbuf, 1)) == 1) {
68 Reset();
69 return;
70 }
71 if (ret==0) break;
72 wbuf = 0;
73 }
74 close(fDescriptor);
75 fDescriptor = -2;
76 return;
77}
78
79// Destructor (Resetting board)
80HVBoard::~HVBoard() {
81 if(fDescriptor >= 0) {
82 Reset();
83 close(fDescriptor);
84 }
85
86 delete NameService;
87 delete BiasVolt;
88 delete[] BoardName;
89}
90
91
92// Communicate: Write and read from HV Board until fTimeOut has been reached
93//
94// Returns: 0 error, 1 success, -1 fTimeOut exceeded
95int HVBoard::Communicate(unsigned char* wbuf, int Bytes) {
96
97 unsigned char rbuf;
98 int ret;
99 fd_set SelectDescriptor;
100
101 // === Write data ===
102 if ((ret=write(fDescriptor, wbuf, Bytes)) < Bytes) {
103 if (ret == -1) m->Message(m->ERROR, "Could not write data to HV board (%s)", strerror(errno));
104 else m->Message(m->ERROR, "Could write only %d of %d bytes to HV board", ret, Bytes);
105 ErrorCount++;
106 return 0;
107 }
108
109 // === Try to read until time-out ===
110 FD_ZERO(&SelectDescriptor); FD_SET(fDescriptor, &SelectDescriptor);
111 struct timeval WaitTime = {(long) fTimeOut, (long) ((fTimeOut-(long) fTimeOut)*1e6)};
112 if (select(fDescriptor+1, &SelectDescriptor, NULL, NULL, &WaitTime)==-1) {
113 m->Message(m->ERROR, "Error with select() (%s)", strerror(errno));
114 return 0;
115 }
116 // Time-out expired?
117 if (!FD_ISSET(fDescriptor, &SelectDescriptor)) return -1;
118
119 // Read error?
120 if ((ret = read(fDescriptor, &rbuf, 1)) == -1) {
121 m->Message(m->ERROR, "Read error (%s)", strerror(errno));
122 ErrorCount++;
123 return 0;
124 }
125
126 // === Update status information ===
127 if (LastWrapCount != -1) {
128 if ((LastWrapCount+1)%8 == (rbuf & 0X07)) WrapOK = true;
129 else WrapOK = false;
130 }
131 LastWrapCount = rbuf & 0X07;
132 for (int i=0; i<NUM_CHAINS; i++) Overcurrent[i] = (rbuf & (0X08 << i));
133 ResetButton = (bool) (rbuf & 0X80);
134
135 return 1;
136}
137
138
139/* Reset HV board - uses TRead() and has same return values */
140int HVBoard::Reset() {
141
142 unsigned char wbuf[] = {REG_RESET,0,0};
143 int ret;
144
145 if((ret = Communicate(wbuf, 3)) == 1) {
146 ClearVoltageArrays();
147 ErrorCount = 0;
148 }
149 return ret;
150}
151
152
153/* Read status register - uses TRead() and has same return values */
154int HVBoard::GetStatus() {
155
156 unsigned char wbuf[] = {REG_STATUS,0,0};
157
158 return Communicate(wbuf, 3);
159}
160
161
162/* Set high voltage - uses TRead() and has same return values */
163int HVBoard::SetHV(int chain, unsigned int channel, int hv) {
164
165 unsigned char wbuf[] = {0,0,0};
166 int ret;
167
168 if (hv<0 || hv > 0x3FFF) {
169 m->PrintMessage("Error: Voltage out of range 0-0x3FFF\n");
170 return 0;
171 }
172
173 switch (chain) {
174 case 0: wbuf[0] = REG_HV0; break;
175 case 1: wbuf[0] = REG_HV1; break;
176 case 2: wbuf[0] = REG_HV2; break;
177 case 3: wbuf[0] = REG_HV3; break;
178
179 default : m->PrintMessage(" Error: Chain %d does not exist\n",chain); return 0;
180 }
181
182 // Assemble bytes
183 wbuf[0] |= (unsigned char)((channel >> 2) & 0X00000007); // Add address [A4-A3]
184 wbuf[1] |= (unsigned char)((hv >> 8) & 0X000000FF); // Add [D13-D8]
185 wbuf[1] |= (unsigned char)((channel << 6) & 0X000000C0); // Add [A1-A0]
186 wbuf[2] |= (unsigned char)(hv & 0X000000FF); // Add [D7-D0]
187
188 if ((ret = Communicate(wbuf, 3)) == 1) HV[chain][channel] = hv;
189 return ret;
190}
191
192
193// Set all voltages of board to zero
194void HVBoard::ClearVoltageArrays() {
195
196 for (int j=0; j<NUM_CHAINS; j++) {
197 for (int k=0; k<NUM_CHANNELS; k++){
198 HV[j][k] = 0;
199 HVV[j][k] = 0.0;
200 }
201 }
202 // Update DIM services
203 BiasVolt->updateService();
204}
205
206
207// Set time-out to wait for read
208void HVBoard::SetTimeOut(double t) {
209
210 if (t >= MIN_TIMEOUT && t <= MAX_TIMEOUT) fTimeOut = t;
211 else fTimeOut = 1.0;
212}
Note: See TracBrowser for help on using the repository browser.