Line data Source code
1 : /********************************************************************\
2 :
3 : Name: cd_rf.c
4 : Created by: Brian Lee
5 :
6 : Based on the Generic class
7 : Contents: TITAN RF Generator Class Driver
8 : Handles the Fine Grid Detector frontend card
9 : Used for midas/mscb/embedded/Experiements/rf/
10 :
11 :
12 :
13 : Requires CMD_GET_EXTERNALTEMP/CMD_GET_INTERNALTEMP in the midas.h
14 : uses the mscbrf.c and standard mscb suite.
15 : (mscb, mscbrpc, musbstd)
16 :
17 : $Id: cd_rf.c 3764 2007-07-20 23:53:16Z amaudruz $
18 :
19 : \********************************************************************/
20 :
21 : #include <stdio.h>
22 : #include <math.h>
23 : #include <string.h> // memcpy()
24 : #include <assert.h>
25 : #include "midas.h"
26 :
27 : //command definitions
28 : #define CMD_SET_VBIAS CMD_SET_FIRST+5
29 : #define CMD_SET_CONTROL CMD_SET_FIRST+6
30 : #define CMD_SET_ASUMDACTH CMD_SET_FIRST+7
31 : #define CMD_SET_CHPUMPDAC CMD_SET_FIRST+8
32 : #define CMD_SET_BIAS_EN CMD_SET_FIRST+9
33 : #define CMD_SET_PARAMS CMD_SET_FIRST+10
34 :
35 : #define CMD_GET_INTERNALTEMP CMD_GET_FIRST+2
36 : #define CMD_GET_ADCMEAS CMD_GET_FIRST+3
37 : #define CMD_GET_CONTROL CMD_GET_FIRST+4
38 : #define CMD_GET_BIASEN CMD_GET_FIRST+5
39 : #define CMD_GET_CHPUMPDAC CMD_GET_FIRST+6
40 : #define CMD_GET_ASUMDACTH CMD_GET_FIRST+7
41 : #define CMD_GET_VBIAS CMD_GET_FIRST+8
42 : #define CMD_GET_ACTUALVBIAS CMD_GET_FIRST+9
43 : #define CMD_GET_PARAMS CMD_GET_FIRST+10
44 : #define CMD_GET_TEMPERATURE1 CMD_GET_FIRST+11
45 : #define CMD_GET_TEMPERATURE2 CMD_GET_FIRST+12
46 : #define CMD_GET_TEMPERATURE3 CMD_GET_FIRST+13
47 : #define CMD_GET_TEMPERATURE4 CMD_GET_FIRST+14
48 : #define CMD_GET_TEMPERATURE5 CMD_GET_FIRST+15
49 : #define CMD_GET_TEMPERATURE6 CMD_GET_FIRST+16
50 : #define CMD_GET_EXTERNALTEMP CMD_GET_FIRST+17
51 :
52 : //define number of RF generators used in the experiment
53 : #define NUMBER_OF_RFGEN 2
54 :
55 : #define REFRESH_MODE 0
56 :
57 : typedef struct
58 : {
59 :
60 : /* ODB keys */
61 : HNDLE hKeyRoot, hKeyParams[NUMBER_OF_RFGEN][7], hKeyLadderParams[NUMBER_OF_RFGEN][5], hKeyLadderBurstParams[NUMBER_OF_RFGEN][4], hKeyFMParams[NUMBER_OF_RFGEN][3],
62 : hKeyBurstParams[NUMBER_OF_RFGEN][3], hKeySweepParams[NUMBER_OF_RFGEN][4], hKeyMeasured[4], hKeyExtTemp, hKeyControl[NUMBER_OF_RFGEN][8], hKeyControlMng[NUMBER_OF_RFGEN];
63 :
64 : /* globals */
65 : INT num_channels;
66 : INT format;
67 : INT last_channel;
68 : char *channel_select[5];
69 :
70 : /* items in /Variables record */
71 : float *params[NUMBER_OF_RFGEN][8];
72 : double *ladderParams[NUMBER_OF_RFGEN][5];
73 : double *burstParams[NUMBER_OF_RFGEN][3];
74 : double *sweepParams[NUMBER_OF_RFGEN][4];
75 : double *fmParams[NUMBER_OF_RFGEN][3];
76 : double *ladderBurstParams[NUMBER_OF_RFGEN][4];
77 : float *measured[9];
78 : double *control[NUMBER_OF_RFGEN][8];
79 : double *controlMng[NUMBER_OF_RFGEN];
80 : float *externalTemp;
81 :
82 : /* items in /Settings */
83 : char *names;
84 : float *update_threshold;
85 :
86 : /* mirror arrays */
87 : float *params_mirror[NUMBER_OF_RFGEN][7];
88 : float *measured_mirror[9];
89 : double *control_mirror[NUMBER_OF_RFGEN][8];
90 : double *controlMng_mirror[NUMBER_OF_RFGEN];
91 :
92 : DEVICE_DRIVER **driver;
93 : INT *channel_offset;
94 :
95 : } TITANRF_INFO;
96 :
97 : #ifndef abs
98 : #define abs(a) (((a) < 0) ? -(a) : (a))
99 : #endif
100 :
101 : /*------------------------------------------------------------------*/
102 :
103 0 : static void free_mem(TITANRF_INFO * rf_info)
104 : {
105 0 : int i = 0;
106 0 : INT rfNumber = 0;
107 :
108 0 : for(i = 0; i < 5; i++) free(rf_info->channel_select[i]);
109 0 : for(i = 0; i < 4; i++) free(rf_info->measured[i]);
110 0 : free(rf_info->externalTemp);
111 0 : free(rf_info->update_threshold);
112 :
113 0 : for(rfNumber = 0; rfNumber < NUMBER_OF_RFGEN; rfNumber++)
114 : {
115 0 : free(rf_info->names);
116 0 : for(i = 0; i < 7; i++) free(rf_info->params[rfNumber][i]);
117 0 : for(i = 0; i < 7; i++) free(rf_info->params_mirror[rfNumber][i]);
118 0 : for(i = 0; i < 5; i++) free(rf_info->ladderParams[rfNumber][i]);
119 0 : for(i = 0; i < 3; i++) free(rf_info->burstParams[rfNumber][i]);
120 0 : for(i = 0; i < 4; i++) free(rf_info->sweepParams[rfNumber][i]);
121 0 : for(i = 0; i < 3; i++) free(rf_info->fmParams[rfNumber][i]);
122 0 : for(i = 0; i < 4; i++) free(rf_info->ladderBurstParams[rfNumber][i]);
123 0 : for(i = 0; i < 8; i++) free(rf_info->control_mirror[rfNumber][i]);
124 0 : free(rf_info->controlMng_mirror[rfNumber]);
125 0 : for(i = 0; i < 8; i++) free(rf_info->control[rfNumber][i]);
126 0 : free(rf_info->controlMng[rfNumber]);
127 : }
128 :
129 0 : free(rf_info->channel_offset);
130 0 : free(rf_info->driver);
131 :
132 0 : free(rf_info);
133 0 : }
134 :
135 : /*------------------------------------------------------------------*/
136 : // Read current
137 0 : INT rf_read(EQUIPMENT * pequipment, int channel)
138 : {
139 : //INT numChannel = 0, varIndex = 0;
140 : //static DWORD last_time = 0;
141 0 : int status = 0;
142 : //TITANRF_INFO *rf_info;
143 : //HNDLE hDB;
144 :
145 : // read measured value
146 : //rf_info = (TITANRF_INFO *) pequipment->cd_info;
147 : //cm_get_experiment_database(&hDB, NULL);
148 :
149 : //for (numChannel = 0; numChannel < 4; numChannel++)
150 : //{
151 : // varIndex = numChannel + 13;
152 : // status = device_driver(rf_info->driver[channel], CMD_GET_ADCMEAS,
153 : // varIndex, rf_info->measured[numChannel]);
154 :
155 : // db_set_data(hDB, rf_info->hKeyMeasured[numChannel], rf_info->measured[numChannel],
156 : // sizeof(float) * rf_info->num_channels, rf_info->num_channels,
157 : // TID_FLOAT);
158 :
159 : // pequipment->odb_out++;
160 : //}
161 :
162 : //// Get the temperatures
163 : //if ((ss_time() - last_time) > 1)
164 : //{
165 : // channel = 0;
166 : // status = device_driver(rf_info->driver[channel], CMD_GET_EXTERNALTEMP,
167 : // channel - rf_info->channel_offset[channel], &rf_info->externalTemp[channel]);
168 : // db_set_data(hDB, rf_info->hKeyExtTemp, rf_info->externalTemp,
169 : // sizeof(float) * rf_info->num_channels, rf_info->num_channels,
170 : // TID_FLOAT);
171 : // last_time = ss_time();
172 : //}
173 :
174 0 : return status;
175 : }
176 :
177 : /*------------------------------------------------------------------*/
178 :
179 0 : void rf_setParams(INT hDB, INT hKey, void *info)
180 : {
181 0 : INT numChannel = 0;
182 : INT i, status, varIndex, rfNumber;
183 : TITANRF_INFO *rf_info;
184 : EQUIPMENT *pequipment;
185 :
186 0 : pequipment = (EQUIPMENT *) info;
187 0 : rf_info = (TITANRF_INFO *) pequipment->cd_info;
188 :
189 : /* set individual channels only if params value differs */
190 0 : for (rfNumber = 0; rfNumber < NUMBER_OF_RFGEN; rfNumber++)
191 : {
192 0 : for (numChannel = 0; numChannel < 7; numChannel++)
193 : {
194 0 : for (i = 0; i < rf_info->num_channels; i++)
195 : {
196 0 : if (*(rf_info->params[rfNumber][numChannel]) != *(rf_info->params_mirror[rfNumber][numChannel]))
197 : {
198 0 : if ((rf_info->driver[i]->flags & DF_READ_ONLY) == 0)
199 : {
200 0 : varIndex = numChannel + 3;
201 :
202 0 : status = device_driver(rf_info->driver[i], CMD_SET_PARAMS, // Set Parameters
203 0 : (rfNumber * 100) + varIndex, *(rf_info->params[rfNumber][numChannel]));
204 : }
205 0 : *(rf_info->params_mirror[rfNumber][numChannel]) = *(rf_info->params[rfNumber][numChannel]);
206 : }
207 : }
208 : }
209 : }
210 :
211 0 : pequipment->odb_in++;
212 :
213 : (void)status; // defeat warning "set but unused"
214 0 : }
215 :
216 : /*------------------------------------------------------------------*/
217 :
218 0 : void rf_setControl(INT hDB, INT hKey, void *info)
219 : {
220 : INT status, i, varIndex;
221 0 : unsigned char toBeSent2 = 0;
222 :
223 0 : EQUIPMENT* pequipment = (EQUIPMENT *) info;
224 0 : TITANRF_INFO* rf_info = (TITANRF_INFO *) pequipment->cd_info;
225 0 : i = 0;
226 :
227 0 : for(int rfNumber = 0; rfNumber < NUMBER_OF_RFGEN; rfNumber++)
228 : {
229 0 : if((*(rf_info->controlMng[rfNumber]) != *(rf_info->controlMng_mirror[rfNumber])) || (*(rf_info->controlMng[rfNumber]) == REFRESH_MODE))
230 : {
231 0 : if((*(rf_info->controlMng[rfNumber]) == REFRESH_MODE) && (*(rf_info->controlMng[rfNumber]) != *(rf_info->controlMng_mirror[rfNumber])))
232 : {
233 0 : *(rf_info->controlMng[rfNumber]) = *(rf_info->controlMng_mirror[rfNumber]);
234 0 : db_set_data(hDB, rf_info->hKeyControlMng[rfNumber], rf_info->controlMng[rfNumber],
235 0 : sizeof(double) * rf_info->num_channels, rf_info->num_channels,
236 : TID_DOUBLE);
237 : }
238 :
239 0 : toBeSent2 = (unsigned char) *(rf_info->controlMng[rfNumber]);
240 :
241 0 : if(toBeSent2 & 0x01)
242 : {
243 0 : varIndex = 3;
244 0 : device_driver(rf_info->driver[i], CMD_SET_PARAMS,
245 0 : (rfNumber * 100) + varIndex, *(rf_info->ladderParams[rfNumber][0]));
246 0 : varIndex = 4;
247 0 : device_driver(rf_info->driver[i], CMD_SET_PARAMS,
248 0 : (rfNumber * 100) + varIndex, *(rf_info->ladderParams[rfNumber][1]));
249 0 : varIndex = 5;
250 0 : device_driver(rf_info->driver[i], CMD_SET_PARAMS,
251 0 : (rfNumber * 100) + varIndex, *(rf_info->ladderParams[rfNumber][2]));
252 0 : varIndex = 6;
253 0 : device_driver(rf_info->driver[i], CMD_SET_PARAMS,
254 0 : (rfNumber * 100) + varIndex, *(rf_info->ladderParams[rfNumber][3]));
255 0 : varIndex = 7;
256 0 : device_driver(rf_info->driver[i], CMD_SET_PARAMS,
257 0 : (rfNumber * 100) + varIndex, *(rf_info->ladderParams[rfNumber][4]));
258 : }
259 0 : else if(toBeSent2 & 0x02)
260 : {
261 0 : varIndex = 3;
262 0 : device_driver(rf_info->driver[i], CMD_SET_PARAMS, // Voltage
263 0 : (rfNumber * 100) + varIndex, *(rf_info->sweepParams[rfNumber][0]));
264 0 : varIndex = 5;
265 0 : device_driver(rf_info->driver[i], CMD_SET_PARAMS, // Voltage
266 0 : (rfNumber * 100) + varIndex, *(rf_info->sweepParams[rfNumber][1]));
267 0 : varIndex = 6;
268 0 : device_driver(rf_info->driver[i], CMD_SET_PARAMS, // Voltage
269 0 : (rfNumber * 100) + varIndex, *(rf_info->sweepParams[rfNumber][2]));
270 0 : varIndex = 7;
271 0 : device_driver(rf_info->driver[i], CMD_SET_PARAMS, // Voltage
272 0 : (rfNumber * 100) + varIndex, *(rf_info->sweepParams[rfNumber][3]));
273 : }
274 0 : else if(toBeSent2 & 0x04)
275 : {
276 0 : varIndex = 7;
277 0 : device_driver(rf_info->driver[i], CMD_SET_PARAMS, // Voltage
278 0 : (rfNumber * 100) + varIndex, *(rf_info->burstParams[rfNumber][0]));
279 0 : varIndex = 8;
280 0 : device_driver(rf_info->driver[i], CMD_SET_PARAMS, // Voltage
281 0 : (rfNumber * 100) + varIndex, *(rf_info->burstParams[rfNumber][1]));
282 0 : varIndex = 9;
283 0 : device_driver(rf_info->driver[i], CMD_SET_PARAMS, // Voltage
284 0 : (rfNumber * 100) + varIndex, *(rf_info->burstParams[rfNumber][2]));
285 : }
286 0 : else if(toBeSent2 & 0x08)
287 : {
288 0 : varIndex = 4;
289 0 : device_driver(rf_info->driver[i], CMD_SET_PARAMS, // Voltage
290 0 : (rfNumber * 100) + varIndex, *(rf_info->ladderBurstParams[rfNumber][0]));
291 0 : varIndex = 7;
292 0 : device_driver(rf_info->driver[i], CMD_SET_PARAMS, // Voltage
293 0 : (rfNumber * 100) + varIndex, *(rf_info->ladderBurstParams[rfNumber][1]));
294 0 : varIndex = 8;
295 0 : device_driver(rf_info->driver[i], CMD_SET_PARAMS, // Voltage
296 0 : (rfNumber * 100) + varIndex, *(rf_info->ladderBurstParams[rfNumber][2]));
297 0 : varIndex = 9;
298 0 : device_driver(rf_info->driver[i], CMD_SET_PARAMS, // Voltage
299 0 : (rfNumber * 100) + varIndex, *(rf_info->ladderBurstParams[rfNumber][3]));
300 : }
301 0 : else if(toBeSent2 & 0x10)
302 : {
303 0 : varIndex = 7;
304 0 : device_driver(rf_info->driver[i], CMD_SET_PARAMS, // Voltage
305 0 : (rfNumber * 100) + varIndex, *(rf_info->fmParams[rfNumber][0]));
306 0 : varIndex = 5;
307 0 : device_driver(rf_info->driver[i], CMD_SET_PARAMS, // Voltage
308 0 : (rfNumber * 100) + varIndex, *(rf_info->fmParams[rfNumber][1]));
309 0 : varIndex = 6;
310 0 : device_driver(rf_info->driver[i], CMD_SET_PARAMS, // Voltage
311 0 : (rfNumber * 100) + varIndex, *(rf_info->fmParams[rfNumber][2]));
312 : }
313 0 : varIndex = 0;
314 0 : status = device_driver(rf_info->driver[i], CMD_SET_CONTROL, // Control
315 0 : rfNumber, *(rf_info->controlMng[rfNumber]), rf_info);
316 0 : *(rf_info->controlMng_mirror[rfNumber]) = *(rf_info->controlMng[rfNumber]);
317 :
318 : }
319 : }
320 :
321 0 : pequipment->odb_in++;
322 :
323 : (void)status; // defeat warning "set but unused"
324 0 : }
325 :
326 : /*------------------------------------------------------------------*/
327 0 : void rf_dummy(INT hDB, INT hKey, void *info)
328 : {
329 : //TITANRF_INFO *rf_info;
330 :
331 0 : EQUIPMENT* pequipment = (EQUIPMENT *) info;
332 : //rf_info = (TITANRF_INFO *) pequipment->cd_info;
333 :
334 0 : pequipment->odb_in++;
335 0 : }
336 :
337 : /*------------------------------------------------------------------*/
338 0 : void rf_update_label(INT hDB, INT hKey, void *info)
339 : {
340 : INT i, status;
341 : TITANRF_INFO *rf_info;
342 : EQUIPMENT *pequipment;
343 :
344 0 : pequipment = (EQUIPMENT *) info;
345 0 : rf_info = (TITANRF_INFO *) pequipment->cd_info;
346 :
347 : /* update channel labels based on the midas channel names */
348 0 : for (i = 0; i < rf_info->num_channels; i++)
349 0 : status = device_driver(rf_info->driver[i], CMD_SET_LABEL,
350 0 : i - rf_info->channel_offset[i],
351 0 : rf_info->names + NAME_LENGTH * i);
352 :
353 : (void)status; // defeat warning "set but not used"
354 0 : }
355 :
356 : /*------------------------------------------------------------------*/
357 0 : INT rf_init(EQUIPMENT * pequipment)
358 : {
359 : int status, size, i, j, k, index, offset, rfNumber, varIndex;
360 : char str[256];
361 : HNDLE hDB, hKey, hNames, hThreshold;
362 : TITANRF_INFO *rf_info;
363 : char paramsString[32];
364 : char measString[32];
365 : char ctrlString[32];
366 : char ctrlMngString[32];
367 : unsigned char *controlRead;
368 :
369 : /* allocate private data */
370 :
371 0 : pequipment->cd_info = calloc(1, sizeof(TITANRF_INFO));
372 0 : rf_info = (TITANRF_INFO *) pequipment->cd_info;
373 :
374 : /* get class driver root key */
375 :
376 0 : cm_get_experiment_database(&hDB, NULL);
377 0 : sprintf(str, "/Equipment/%s", pequipment->name);
378 0 : db_create_key(hDB, 0, str, TID_KEY);
379 0 : db_find_key(hDB, 0, str, &rf_info->hKeyRoot);
380 :
381 : /* save event format */
382 0 : size = sizeof(str);
383 0 : db_get_value(hDB, rf_info->hKeyRoot, "Common/Format", str, &size, TID_STRING, TRUE);
384 :
385 0 : if (equal_ustring(str, "Fixed"))
386 : {
387 0 : rf_info->format = FORMAT_FIXED;
388 : }
389 0 : else if (equal_ustring(str, "MIDAS"))
390 : {
391 0 : rf_info->format = FORMAT_MIDAS;
392 : }
393 : else
394 : {
395 0 : rf_info->format = 0;
396 0 : assert(!"unknown ODB Common/Format");
397 : }
398 :
399 : /* count total number of channels */
400 0 : for (i = 0, rf_info->num_channels = 0; pequipment->driver[i].name[0]; i++)
401 : {
402 0 : if (pequipment->driver[i].channels == 0)
403 : {
404 0 : cm_msg(MERROR, "rf_init", "Driver with zero channels not allowed");
405 0 : return FE_ERR_ODB;
406 : }
407 :
408 0 : rf_info->num_channels += pequipment->driver[i].channels;
409 : }
410 :
411 : //rf_info->num_channels = 2;
412 :
413 0 : if (rf_info->num_channels == 0)
414 : {
415 0 : cm_msg(MERROR, "rf_init", "No channels found in device driver list");
416 0 : return FE_ERR_ODB;
417 : }
418 :
419 : /* Allocate memory for buffers */
420 :
421 0 : for(rfNumber = 0; rfNumber < NUMBER_OF_RFGEN; rfNumber++)
422 : {
423 0 : for(i = 0; i < 5; i++) rf_info->ladderParams[rfNumber][i] = (double *) calloc(rf_info->num_channels, sizeof(double));
424 0 : for(i = 0; i < 3; i++) rf_info->burstParams[rfNumber][i] = (double *) calloc(rf_info->num_channels, sizeof(double));
425 0 : for(i = 0; i < 4; i++) rf_info->sweepParams[rfNumber][i] = (double *) calloc(rf_info->num_channels, sizeof(double));
426 0 : for(i = 0; i < 3; i++) rf_info->fmParams[rfNumber][i] = (double *) calloc(rf_info->num_channels, sizeof(double));
427 0 : for(i = 0; i < 4; i++) rf_info->ladderBurstParams[rfNumber][i] = (double *) calloc(rf_info->num_channels, sizeof(double));
428 0 : for(i = 0; i < 7; i++) rf_info->params[rfNumber][i] = (float *) calloc(rf_info->num_channels, sizeof(float));
429 :
430 0 : rf_info->controlMng[rfNumber] = (double *) calloc(rf_info->num_channels, sizeof(double));
431 :
432 0 : for(i = 0; i < 8; i++) rf_info->control[rfNumber][i] = (double *) calloc(rf_info->num_channels, sizeof(double));
433 0 : for(i = 0; i < 7; i++) rf_info->params_mirror[rfNumber][i] = (float *) calloc(rf_info->num_channels, sizeof(float));
434 0 : for(i = 0; i < 8; i++) rf_info->control_mirror[rfNumber][i] = (double *) calloc(rf_info->num_channels, sizeof(double));
435 0 : rf_info->controlMng_mirror[rfNumber] = (double *) calloc(rf_info->num_channels, sizeof(double));
436 : }
437 :
438 0 : rf_info->names = (char *) calloc(rf_info->num_channels, NAME_LENGTH);
439 0 : for(i = 0; i < 5; i++) rf_info->channel_select[i] = (char *) calloc(rf_info->num_channels, 32 * sizeof(char));
440 :
441 0 : for(i = 0; i < 9; i++) rf_info->measured[i] = (float *) calloc(rf_info->num_channels, sizeof(float));
442 0 : rf_info->externalTemp = (float *) calloc(rf_info->num_channels, sizeof(float));
443 0 : rf_info->update_threshold = (float *) calloc(rf_info->num_channels, sizeof(float));
444 0 : for(i = 0; i < 9; i++) rf_info->measured_mirror[i] = (float *) calloc(rf_info->num_channels, sizeof(float));
445 0 : rf_info->channel_offset = (INT *) calloc(rf_info->num_channels, sizeof(INT));
446 0 : rf_info->driver = (DEVICE_DRIVER**) calloc(rf_info->num_channels, sizeof(void *));
447 :
448 0 : if (!rf_info->driver)
449 : {
450 0 : cm_msg(MERROR, "rf_init", "Not enough memory");
451 0 : return FE_ERR_ODB;
452 : }
453 :
454 :
455 : /*---- Initialize device drivers ----*/
456 :
457 : /* call init method */
458 0 : for (i = 0; pequipment->driver[i].name[0]; i++)
459 : {
460 0 : sprintf(str, "Settings/Devices/%s", pequipment->driver[i].name);
461 0 : status = db_find_key(hDB, rf_info->hKeyRoot, str, &hKey);
462 0 : if (status != DB_SUCCESS)
463 : {
464 0 : db_create_key(hDB, rf_info->hKeyRoot, str, TID_KEY);
465 0 : status = db_find_key(hDB, rf_info->hKeyRoot, str, &hKey);
466 0 : if (status != DB_SUCCESS) {
467 0 : cm_msg(MERROR, "hv_init", "Cannot create %s entry in online database", str);
468 0 : free_mem(rf_info);
469 0 : return FE_ERR_ODB;
470 : }
471 : }
472 :
473 0 : status = device_driver(&pequipment->driver[i], CMD_INIT, hKey);
474 0 : if (status != FE_SUCCESS)
475 : {
476 0 : free_mem(rf_info);
477 0 : return status;
478 : }
479 : }
480 :
481 : /* compose device driver channel assignment */
482 0 : for (i = 0, j = 0, index = 0, offset = 0; i < rf_info->num_channels; i++, j++)
483 : {
484 0 : while (j >= pequipment->driver[index].channels && pequipment->driver[index].name[0])
485 : {
486 0 : offset += j;
487 0 : index++;
488 0 : j = 0;
489 : }
490 :
491 0 : rf_info->driver[i] = &pequipment->driver[index];
492 0 : rf_info->channel_offset[i] = offset;
493 : }
494 :
495 :
496 : // define channel selects
497 0 : rf_info->channel_select[0] = strdup("RF1");
498 0 : rf_info->channel_select[1] = strdup("RF2");
499 0 : rf_info->channel_select[2] = strdup("RF3");
500 0 : rf_info->channel_select[3] = strdup("RF4");
501 0 : rf_info->channel_select[4] = strdup("Temps");
502 :
503 0 : for(k = 0, rfNumber = 0; k < NUMBER_OF_RFGEN; rfNumber++, k++)
504 : {
505 : /*---- create params variables ----*/
506 0 : for(j = 0; j < 7; j++)
507 : {
508 : /* Assign params names */
509 0 : if(j == 0) sprintf(paramsString, "Variables/%s/sweepDur", rf_info->channel_select[k]);
510 0 : if(j == 1) sprintf(paramsString, "Variables/%s/numSteps", rf_info->channel_select[k]);
511 0 : if(j == 2) sprintf(paramsString, "Variables/%s/startFreq", rf_info->channel_select[k]);
512 0 : if(j == 3) sprintf(paramsString, "Variables/%s/endFreq", rf_info->channel_select[k]);
513 0 : if(j == 4) sprintf(paramsString, "Variables/%s/rfAmplitude", rf_info->channel_select[k]);
514 0 : if(j == 5) sprintf(paramsString, "Variables/%s/burstFreq", rf_info->channel_select[k]);
515 0 : if(j == 6) sprintf(paramsString, "Variables/%s/numCycles", rf_info->channel_select[k]);
516 :
517 : /* find the key and get data */
518 0 : db_merge_data(hDB, rf_info->hKeyRoot, paramsString,
519 0 : rf_info->params[rfNumber][j], sizeof(float) * rf_info->num_channels,
520 : rf_info->num_channels, TID_FLOAT);
521 0 : status = db_find_key(hDB, rf_info->hKeyRoot, paramsString, &rf_info->hKeyParams[rfNumber][j]);
522 :
523 0 : size = sizeof(float) * rf_info->num_channels;
524 :
525 : //initially read in the values
526 0 : varIndex = 3 + j;
527 0 : device_driver(rf_info->driver[0], CMD_GET_PARAMS, (rfNumber * 100) + varIndex, rf_info->params[rfNumber][j]); // Params
528 0 : db_set_data(hDB, rf_info->hKeyParams[rfNumber][j], rf_info->params[rfNumber][j], size,
529 : rf_info->num_channels, TID_FLOAT);
530 :
531 0 : db_open_record(hDB, rf_info->hKeyParams[rfNumber][j], rf_info->params[rfNumber][j],
532 0 : rf_info->num_channels * sizeof(float), MODE_READ, rf_setParams,
533 : pequipment);
534 : }
535 :
536 : /*---- create ladder params variables ----*/
537 0 : for(j = 0; j < 5; j++)
538 : {
539 : /* Assign params names */
540 0 : if(j == 0) sprintf(paramsString, "Variables/%s/lsweepDur", rf_info->channel_select[k]);
541 0 : if(j == 1) sprintf(paramsString, "Variables/%s/lnumSteps", rf_info->channel_select[k]);
542 0 : if(j == 2) sprintf(paramsString, "Variables/%s/lstartFreq", rf_info->channel_select[k]);
543 0 : if(j == 3) sprintf(paramsString, "Variables/%s/lendFreq", rf_info->channel_select[k]);
544 0 : if(j == 4) sprintf(paramsString, "Variables/%s/lrfAmplitude", rf_info->channel_select[k]);
545 :
546 : /* find the key and get data */
547 0 : db_merge_data(hDB, rf_info->hKeyRoot, paramsString,
548 0 : rf_info->ladderParams[rfNumber][j], sizeof(double) * rf_info->num_channels,
549 : rf_info->num_channels, TID_DOUBLE);
550 0 : status = db_find_key(hDB, rf_info->hKeyRoot, paramsString, &rf_info->hKeyLadderParams[rfNumber][j]);
551 0 : db_open_record(hDB, rf_info->hKeyLadderParams[rfNumber][j], rf_info->ladderParams[rfNumber][j],
552 0 : rf_info->num_channels * sizeof(double), MODE_READ, rf_dummy,
553 : pequipment);
554 : }
555 :
556 : /*---- create burst params variables ----*/
557 0 : for(j = 0; j < 3; j++)
558 : {
559 : /* Assign params names */
560 0 : if(j == 0) sprintf(paramsString, "Variables/%s/brfAmplitude", rf_info->channel_select[k]);
561 0 : if(j == 1) sprintf(paramsString, "Variables/%s/bburstFreq", rf_info->channel_select[k]);
562 0 : if(j == 2) sprintf(paramsString, "Variables/%s/bnumCycles", rf_info->channel_select[k]);
563 :
564 : /* find the key and get data */
565 0 : db_merge_data(hDB, rf_info->hKeyRoot, paramsString,
566 0 : rf_info->burstParams[rfNumber][j], sizeof(double) * rf_info->num_channels,
567 : rf_info->num_channels, TID_DOUBLE);
568 0 : status = db_find_key(hDB, rf_info->hKeyRoot, paramsString, &rf_info->hKeyBurstParams[rfNumber][j]);
569 0 : db_open_record(hDB, rf_info->hKeyBurstParams[rfNumber][j], rf_info->burstParams[rfNumber][j],
570 0 : rf_info->num_channels * sizeof(double), MODE_READ, rf_dummy,
571 : pequipment);
572 : }
573 :
574 : /*---- create sweep params variables ----*/
575 0 : for(j = 0; j < 4; j++)
576 : {
577 : /* Assign params names */
578 0 : if(j == 0) sprintf(paramsString, "Variables/%s/ssweepDur", rf_info->channel_select[k]);
579 0 : if(j == 1) sprintf(paramsString, "Variables/%s/sstartFreq", rf_info->channel_select[k]);
580 0 : if(j == 2) sprintf(paramsString, "Variables/%s/sendFreq", rf_info->channel_select[k]);
581 0 : if(j == 3) sprintf(paramsString, "Variables/%s/srfAmplitude", rf_info->channel_select[k]);
582 :
583 : /* find the key and get data */
584 0 : db_merge_data(hDB, rf_info->hKeyRoot, paramsString,
585 0 : rf_info->sweepParams[rfNumber][j], sizeof(double) * rf_info->num_channels,
586 : rf_info->num_channels, TID_DOUBLE);
587 0 : status = db_find_key(hDB, rf_info->hKeyRoot, paramsString, &rf_info->hKeySweepParams[rfNumber][j]);
588 0 : db_open_record(hDB, rf_info->hKeySweepParams[rfNumber][j], rf_info->sweepParams[rfNumber][j],
589 0 : rf_info->num_channels * sizeof(double), MODE_READ, rf_dummy,
590 : pequipment);
591 : }
592 :
593 : /*---- create fm params variables ----*/
594 0 : for(j = 0; j < 3; j++)
595 : {
596 : /* Assign params names */
597 0 : if(j == 0) sprintf(paramsString, "Variables/%s/fmrfAmplitude", rf_info->channel_select[k]);
598 0 : if(j == 1) sprintf(paramsString, "Variables/%s/fmstartFreq", rf_info->channel_select[k]);
599 0 : if(j == 2) sprintf(paramsString, "Variables/%s/fmendFreq", rf_info->channel_select[k]);
600 :
601 : /* find the key and get data */
602 0 : db_merge_data(hDB, rf_info->hKeyRoot, paramsString,
603 0 : rf_info->fmParams[rfNumber][j], sizeof(double) * rf_info->num_channels,
604 : rf_info->num_channels, TID_DOUBLE);
605 0 : status = db_find_key(hDB, rf_info->hKeyRoot, paramsString, &rf_info->hKeyFMParams[rfNumber][j]);
606 0 : db_open_record(hDB, rf_info->hKeyFMParams[rfNumber][j], rf_info->fmParams[rfNumber][j],
607 0 : rf_info->num_channels * sizeof(double), MODE_READ, rf_dummy,
608 : pequipment);
609 : }
610 :
611 : /*---- create ladder burst params variables ----*/
612 0 : for(j = 0; j < 4; j++)
613 : {
614 : /* Assign params names */
615 0 : if(j == 0) sprintf(paramsString, "Variables/%s/lbnumSteps", rf_info->channel_select[k]);
616 0 : if(j == 1) sprintf(paramsString, "Variables/%s/lbrfAmplitude", rf_info->channel_select[k]);
617 0 : if(j == 2) sprintf(paramsString, "Variables/%s/lbburstFreq", rf_info->channel_select[k]);
618 0 : if(j == 3) sprintf(paramsString, "Variables/%s/lbnumCycles", rf_info->channel_select[k]);
619 :
620 : /* find the key and get data */
621 0 : db_merge_data(hDB, rf_info->hKeyRoot, paramsString,
622 0 : rf_info->ladderBurstParams[rfNumber][j], sizeof(double) * rf_info->num_channels,
623 : rf_info->num_channels, TID_DOUBLE);
624 0 : status = db_find_key(hDB, rf_info->hKeyRoot, paramsString, &rf_info->hKeyLadderBurstParams[rfNumber][j]);
625 0 : db_open_record(hDB, rf_info->hKeyLadderBurstParams[rfNumber][j], rf_info->ladderBurstParams[rfNumber][j],
626 0 : rf_info->num_channels * sizeof(double), MODE_READ, rf_dummy,
627 : pequipment);
628 : }
629 :
630 : /*---- create control variables ----*/
631 : //initially read in the current control values
632 0 : controlRead = (unsigned char*) calloc(1, sizeof(unsigned char *));
633 0 : device_driver(rf_info->driver[0], CMD_GET_CONTROL, rf_info->channel_offset[0], controlRead); // Control
634 :
635 0 : for(j = 0; j < 8; j++)
636 : {
637 : /* Assign control names */
638 0 : if(j == 0) sprintf(ctrlString, "Variables/%s/LadderStep", rf_info->channel_select[k]);
639 0 : if(j == 1) sprintf(ctrlString, "Variables/%s/Sweep", rf_info->channel_select[k]);
640 0 : if(j == 2) sprintf(ctrlString, "Variables/%s/Burst", rf_info->channel_select[k]);
641 0 : if(j == 3) sprintf(ctrlString, "Variables/%s/LadderBurst", rf_info->channel_select[k]);
642 0 : if(j == 4) sprintf(ctrlString, "Variables/%s/fmSweep", rf_info->channel_select[k]);
643 0 : if(j == 5) sprintf(ctrlString, "Variables/%s/None1", rf_info->channel_select[k]);
644 0 : if(j == 6) sprintf(ctrlString, "Variables/%s/None2", rf_info->channel_select[k]);
645 0 : if(j == 7) sprintf(ctrlString, "Variables/%s/KLock", rf_info->channel_select[k]);
646 :
647 :
648 0 : db_merge_data(hDB, rf_info->hKeyRoot, ctrlString,
649 0 : rf_info->control[rfNumber][j], sizeof(double) * rf_info->num_channels,
650 : rf_info->num_channels, TID_DOUBLE);
651 0 : status = db_find_key(hDB, rf_info->hKeyRoot, ctrlString, &rf_info->hKeyControl[rfNumber][j]);
652 :
653 : //distribute the read control values to each element in the control[] array
654 0 : if((*controlRead & (unsigned char)(1 << j))) *(rf_info->control[rfNumber][j]) = 1;
655 : //update the Variables (Control, 8bits)
656 0 : db_set_data(hDB, rf_info->hKeyControl[rfNumber][j], rf_info->control[rfNumber][j],
657 0 : sizeof(double) * rf_info->num_channels, rf_info->num_channels,
658 : TID_DOUBLE);
659 :
660 0 : size = sizeof(double) * rf_info->num_channels;
661 0 : db_get_data(hDB, rf_info->hKeyControl[rfNumber][j], rf_info->control[rfNumber][j], &size, TID_DOUBLE);
662 0 : db_open_record(hDB, rf_info->hKeyControl[rfNumber][j], rf_info->control[rfNumber][j],
663 0 : rf_info->num_channels * sizeof(double), MODE_READ, rf_setControl,
664 : pequipment);
665 :
666 : //copy all control values into the control mirror
667 0 : *(rf_info->control_mirror[rfNumber][j]) = *(rf_info->control[rfNumber][j]);
668 : }
669 :
670 : /*---- create controlMng variable ----*/
671 0 : sprintf(ctrlMngString, "Variables/%s/controlMng", rf_info->channel_select[k]);
672 :
673 0 : db_merge_data(hDB, rf_info->hKeyRoot, ctrlMngString,
674 0 : rf_info->controlMng[rfNumber], sizeof(double) * rf_info->num_channels,
675 : rf_info->num_channels, TID_DOUBLE);
676 0 : status = db_find_key(hDB, rf_info->hKeyRoot, ctrlMngString, &rf_info->hKeyControlMng[rfNumber]);
677 0 : db_open_record(hDB, rf_info->hKeyControlMng[rfNumber], rf_info->controlMng[rfNumber],
678 0 : rf_info->num_channels * sizeof(double), MODE_READ, rf_setControl,
679 : pequipment);
680 :
681 : //end of the rfNumber++ forloop
682 : }
683 :
684 :
685 : /*---- create measured variables ----*/
686 0 : for(j = 0; j < 4; j++)
687 : {
688 : /* Assign adc measurement channel names */
689 0 : if(j == 0) strcpy(measString, "Variables/RF1Monitor");
690 0 : if(j == 1) strcpy(measString, "Variables/RF2Monitor");
691 0 : if(j == 2) strcpy(measString, "Variables/RF3Monitor");
692 0 : if(j == 3) strcpy(measString, "Variables/RF4Monitor");
693 :
694 0 : db_merge_data(hDB, rf_info->hKeyRoot, measString,
695 0 : rf_info->measured[j], sizeof(float) * rf_info->num_channels,
696 : rf_info->num_channels, TID_FLOAT);
697 0 : status = db_find_key(hDB, rf_info->hKeyRoot, measString, &rf_info->hKeyMeasured[j]);
698 :
699 0 : memcpy(rf_info->measured_mirror[j], rf_info->measured[j],
700 0 : rf_info->num_channels * sizeof(float));
701 : }
702 :
703 : /*---- create ExtTemp measured variables ----*/
704 0 : db_merge_data(hDB, rf_info->hKeyRoot, "Variables/ExtTemp",
705 0 : rf_info->externalTemp, sizeof(float) * rf_info->num_channels,
706 : rf_info->num_channels, TID_FLOAT);
707 0 : db_find_key(hDB, rf_info->hKeyRoot, "Variables/ExtTemp", &rf_info->hKeyExtTemp);
708 :
709 : /*---- get default names from device driver ----*/
710 0 : for (i = 0; i < rf_info->num_channels; i++)
711 : {
712 0 : sprintf(rf_info->names + NAME_LENGTH * i, "Default%%CH %d", i);
713 0 : device_driver(rf_info->driver[i], CMD_GET_LABEL,
714 0 : i - rf_info->channel_offset[i], rf_info->names + NAME_LENGTH * i);
715 : }
716 0 : db_merge_data(hDB, rf_info->hKeyRoot, "Settings/Names",
717 0 : rf_info->names, NAME_LENGTH * rf_info->num_channels,
718 : rf_info->num_channels, TID_STRING);
719 :
720 : /*---- set labels form midas SC names ----*/
721 0 : for (i = 0; i < rf_info->num_channels; i++)
722 : {
723 0 : rf_info = (TITANRF_INFO *) pequipment->cd_info;
724 0 : device_driver(rf_info->driver[i], CMD_SET_LABEL,
725 0 : i - rf_info->channel_offset[i], rf_info->names + NAME_LENGTH * i);
726 : }
727 :
728 : /* open hotlink on channel names */
729 0 : if (db_find_key(hDB, rf_info->hKeyRoot, "Settings/Names", &hNames) == DB_SUCCESS)
730 : {
731 0 : db_open_record(hDB, hNames, rf_info->names, NAME_LENGTH*rf_info->num_channels,
732 : MODE_READ, rf_update_label, pequipment);
733 : }
734 :
735 : /*---- get default update threshold from device driver ----*/
736 0 : for (i = 0; i < rf_info->num_channels; i++)
737 : {
738 0 : rf_info->update_threshold[i] = 1.f; /* default 1 unit */
739 0 : device_driver(rf_info->driver[i], CMD_GET_THRESHOLD,
740 0 : i - rf_info->channel_offset[i], &rf_info->update_threshold[i]);
741 : }
742 :
743 0 : db_merge_data(hDB, rf_info->hKeyRoot, "Settings/Update Threshold Measured",
744 0 : rf_info->update_threshold, sizeof(float)*rf_info->num_channels,
745 : rf_info->num_channels, TID_FLOAT);
746 :
747 : /* open hotlink on update threshold */
748 0 : if (db_find_key(hDB, rf_info->hKeyRoot, "Settings/Update Threshold Measured", &hThreshold) == DB_SUCCESS)
749 : {
750 0 : db_open_record(hDB, hThreshold, rf_info->update_threshold, sizeof(float)*rf_info->num_channels,
751 : MODE_READ, NULL, NULL);
752 : }
753 :
754 : /*---- set initial params values ----*/
755 : // rf_setParamsAndControl(hDB, rf_info->hKeyParams[rfNumber][j], pequipment);
756 :
757 : /* initially read all channels */
758 : //for (i = 0; i < rf_info->num_channels; i++)
759 : //rf_read(pequipment, i);
760 :
761 0 : return FE_SUCCESS;
762 : }
763 :
764 : /*----------------------------------------------------------------------------*/
765 :
766 0 : INT rf_start(EQUIPMENT * pequipment)
767 : {
768 : INT i;
769 :
770 : /* call start method of device drivers */
771 0 : for (i = 0; pequipment->driver[i].dd != NULL ; i++)
772 0 : if (pequipment->driver[i].flags & DF_MULTITHREAD) {
773 0 : pequipment->driver[i].pequipment = &pequipment->info;
774 0 : device_driver(&pequipment->driver[i], CMD_START);
775 : }
776 :
777 0 : return FE_SUCCESS;
778 : }
779 :
780 : /*----------------------------------------------------------------------------*/
781 :
782 0 : INT rf_stop(EQUIPMENT * pequipment)
783 : {
784 : INT i;
785 :
786 : /* call stop method of device drivers */
787 0 : for (i = 0; pequipment->driver[i].dd != NULL && pequipment->driver[i].flags & DF_MULTITHREAD ; i++)
788 0 : device_driver(&pequipment->driver[i], CMD_STOP);
789 :
790 0 : return FE_SUCCESS;
791 : }
792 :
793 : /*------------------------------------------------------------------*/
794 :
795 0 : INT rf_exit(EQUIPMENT * pequipment)
796 : {
797 : INT i;
798 :
799 0 : free_mem((TITANRF_INFO *) pequipment->cd_info);
800 :
801 : /* call exit method of device drivers */
802 0 : for (i = 0; pequipment->driver[i].dd != NULL; i++)
803 0 : device_driver(&pequipment->driver[i], CMD_EXIT);
804 :
805 0 : return FE_SUCCESS;
806 : }
807 :
808 : /*------------------------------------------------------------------*/
809 :
810 0 : INT rf_idle(EQUIPMENT * pequipment)
811 : {
812 : INT act, status;
813 : TITANRF_INFO *rf_info;
814 :
815 0 : rf_info = (TITANRF_INFO *) pequipment->cd_info;
816 :
817 : /* select next measurement channel */
818 0 : act = (rf_info->last_channel + 1) % rf_info->num_channels;
819 :
820 : /* measure channel */
821 0 : status = rf_read(pequipment, act);
822 0 : rf_info->last_channel = act;
823 :
824 0 : return status;
825 : }
826 :
827 : /*------------------------------------------------------------------*/
828 :
829 0 : INT cd_rf_read(char *pevent, int offset)
830 : {
831 : float *pdata;
832 0 : INT rfNumber = 0;
833 : TITANRF_INFO *rf_info;
834 : EQUIPMENT *pequipment;
835 0 : int j = 0;
836 :
837 0 : pequipment = *((EQUIPMENT **) pevent);
838 0 : rf_info = (TITANRF_INFO *) pequipment->cd_info;
839 :
840 0 : if (rf_info->format == FORMAT_FIXED)
841 : {
842 0 : for(rfNumber = 0; rfNumber < NUMBER_OF_RFGEN; rfNumber++)
843 : {
844 0 : for(j = 0; j < 8; j++)
845 : {
846 0 : memcpy(pevent, rf_info->params[rfNumber][j], sizeof(float) * rf_info->num_channels);
847 0 : pevent += sizeof(float) * rf_info->num_channels;
848 : }
849 : }
850 :
851 0 : for(j = 0; j < 9; j++)
852 : {
853 0 : memcpy(pevent, rf_info->measured[j], sizeof(float) * rf_info->num_channels);
854 0 : pevent += sizeof(float) * rf_info->num_channels;
855 : }
856 :
857 0 : return 2 * sizeof(float) * rf_info->num_channels;
858 : }
859 0 : else if (rf_info->format == FORMAT_MIDAS)
860 : {
861 0 : bk_init(pevent);
862 :
863 : /* create VDMD bank */
864 0 : for(rfNumber = 0; rfNumber < NUMBER_OF_RFGEN; rfNumber++)
865 : {
866 0 : for(j = 0; j < 8; j++)
867 : {
868 0 : bk_create(pevent, "VDMD", TID_FLOAT, (void**) &pdata);
869 0 : memcpy(pdata, rf_info->params[rfNumber][j], sizeof(float) * rf_info->num_channels);
870 0 : pdata += rf_info->num_channels;
871 0 : bk_close(pevent, pdata);
872 : }
873 : }
874 :
875 : /* create IMES bank */
876 0 : for(j = 0; j < 9; j++)
877 : {
878 0 : bk_create(pevent, "IMES", TID_FLOAT, (void**) &pdata);
879 0 : memcpy(pdata, rf_info->measured[j], sizeof(float) * rf_info->num_channels);
880 0 : pdata += rf_info->num_channels;
881 0 : bk_close(pevent, pdata);
882 : }
883 :
884 : /* create TEMP2 bank */
885 0 : bk_create(pevent, "TEM2", TID_FLOAT, (void**) &pdata);
886 0 : memcpy(pdata, rf_info->externalTemp, sizeof(float) * rf_info->num_channels);
887 0 : pdata += rf_info->num_channels;
888 0 : bk_close(pevent, pdata);
889 :
890 0 : return bk_size(pevent);
891 : }
892 : else
893 : {
894 0 : return 0;
895 : }
896 : }
897 :
898 : /*------------------------------------------------------------------*/
899 :
900 0 : INT cd_rf(INT cmd, EQUIPMENT * pequipment)
901 : {
902 : INT status;
903 :
904 0 : switch (cmd) {
905 0 : case CMD_INIT:
906 0 : status = rf_init(pequipment);
907 0 : break;
908 :
909 0 : case CMD_START:
910 0 : status = rf_start(pequipment);
911 0 : break;
912 :
913 0 : case CMD_STOP:
914 0 : status = rf_stop(pequipment);
915 0 : break;
916 :
917 0 : case CMD_EXIT:
918 0 : status = rf_exit(pequipment);
919 0 : break;
920 :
921 0 : case CMD_IDLE:
922 0 : status = rf_idle(pequipment);
923 0 : break;
924 :
925 0 : default:
926 0 : cm_msg(MERROR, "Generic class driver", "Received unknown command %d", cmd);
927 0 : status = FE_ERR_DRIVER;
928 0 : break;
929 : }
930 :
931 0 : return status;
932 : }
|