source: hvcontrol/src/HV.cc@ 196

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