source: hvcontrol/src/HV.cc@ 8051

Last change on this file since 8051 was 264, checked in by ogrimm, 14 years ago
Removed mutex locking in hvcontrol, use lockf() in Communicate()
File size: 6.0 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 N, Ret = 0;
99 fd_set SelectDescriptor;
100 struct timeval WaitTime = {(long) fTimeOut, (long) ((fTimeOut-(long) fTimeOut)*1e6)};
101
102 // === Lock file descriptor ===
103 if (lockf(fDescriptor, F_LOCK, 0) == -1) {
104 m->Message(m->ERROR, "Failed to lock file descriptor (%s)", strerror(errno));
105 return 0;
106 }
107
108 // === Write data ===
109 if ((N = write(fDescriptor, wbuf, Bytes)) < Bytes) {
110 if (N == -1) m->Message(m->ERROR, "Could not write data to HV board (%s)", strerror(errno));
111 else m->Message(m->ERROR, "Could write only %d of %d bytes to HV board", N, Bytes);
112 ErrorCount++;
113 goto ExitCommunicate;
114 }
115
116 // === Try to read until time-out ===
117 FD_ZERO(&SelectDescriptor); FD_SET(fDescriptor, &SelectDescriptor);
118 if (select(fDescriptor+1, &SelectDescriptor, NULL, NULL, &WaitTime)==-1) {
119 m->Message(m->ERROR, "Error with select() (%s)", strerror(errno));
120 goto ExitCommunicate;
121 }
122 // Time-out expired?
123 if (!FD_ISSET(fDescriptor, &SelectDescriptor)) {
124 Ret = -1;
125 goto ExitCommunicate;
126 }
127
128 // Read error?
129 if (read(fDescriptor, &rbuf, 1) == -1) {
130 m->Message(m->ERROR, "Read error (%s)", strerror(errno));
131 ErrorCount++;
132 goto ExitCommunicate;
133 }
134
135 // === Update status information ===
136 if (LastWrapCount != -1) {
137 if ((LastWrapCount+1)%8 == (rbuf & 0X07)) WrapOK = true;
138 else WrapOK = false;
139 }
140 LastWrapCount = rbuf & 0X07;
141 for (int i=0; i<NUM_CHAINS; i++) Overcurrent[i] = (rbuf & (0X08 << i));
142 ResetButton = (bool) (rbuf & 0X80);
143 Ret = 1;
144
145 // === UnLock file descriptor ===
146 ExitCommunicate:
147 if (lockf(fDescriptor, F_LOCK, 0) == -1) {
148 m->Message(m->ERROR, "Failed to lock file descriptor (%s)", strerror(errno));
149 return 0;
150 }
151
152 return Ret;
153}
154
155
156// Reset HV board
157int HVBoard::Reset() {
158
159 unsigned char wbuf[] = {REG_RESET,0,0};
160 int ret;
161
162 if((ret = Communicate(wbuf, 3)) == 1) {
163 ClearVoltageArrays();
164 ErrorCount = 0;
165 }
166 return ret;
167}
168
169
170// Read status register
171int HVBoard::GetStatus() {
172
173 unsigned char wbuf[] = {REG_STATUS,0,0};
174
175 return Communicate(wbuf, 3);
176}
177
178
179// Set high voltage
180int HVBoard::SetHV(int chain, unsigned int channel, int hv) {
181
182 unsigned char wbuf[] = {0,0,0};
183 int ret;
184
185 if (hv<0 || hv > 0x3FFF) {
186 m->PrintMessage("Error: Voltage out of range 0-0x3FFF\n");
187 return 0;
188 }
189
190 switch (chain) {
191 case 0: wbuf[0] = REG_HV0; break;
192 case 1: wbuf[0] = REG_HV1; break;
193 case 2: wbuf[0] = REG_HV2; break;
194 case 3: wbuf[0] = REG_HV3; break;
195
196 default : m->PrintMessage(" Error: Chain %d does not exist\n",chain); return 0;
197 }
198
199 // Assemble bytes
200 wbuf[0] |= (unsigned char)((channel >> 2) & 0X00000007); // Add address [A4-A3]
201 wbuf[1] |= (unsigned char)((hv >> 8) & 0X000000FF); // Add [D13-D8]
202 wbuf[1] |= (unsigned char)((channel << 6) & 0X000000C0); // Add [A1-A0]
203 wbuf[2] |= (unsigned char)(hv & 0X000000FF); // Add [D7-D0]
204
205 if ((ret = Communicate(wbuf, 3)) == 1) HV[chain][channel] = hv;
206 return ret;
207}
208
209
210// Set all voltages of board to zero
211void HVBoard::ClearVoltageArrays() {
212
213 for (int j=0; j<NUM_CHAINS; j++) {
214 for (int k=0; k<NUM_CHANNELS; k++){
215 HV[j][k] = 0;
216 HVV[j][k] = 0.0;
217 }
218 }
219 // Update DIM services
220 BiasVolt->updateService();
221}
222
223
224// Set time-out to wait for read
225void HVBoard::SetTimeOut(double t) {
226
227 if (t >= MIN_TIMEOUT && t <= MAX_TIMEOUT) fTimeOut = t;
228 else fTimeOut = 1.0;
229}
Note: See TracBrowser for help on using the repository browser.