-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathxl3_util.c
395 lines (356 loc) · 10.7 KB
/
xl3_util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "include/Record_Info.h"
#include "include/xl_regs.h"
#include "penn_daq.h"
#include "xl3_util.h"
#include "net_util.h"
#include "db.h"
int set_location(char *buffer)
{
char *words,*words2;
words = strtok(buffer, " ");
while (words != NULL){
if (words[0] == '-'){
if (words[1] == 'p'){
printsend("location set to penn test stand\n");
current_location = 2;
}
if (words[1] == 'u'){
printsend("location set to underground\n");
current_location = 1;
}
if (words[1] == 'a'){
printsend("location set to above ground test stand\n");
current_location = 0;
}
if (words[1] == 'h'){
printsend("Usage: set_location"
"-a (above ground) -u (under ground) -p (penn)\n");
return 0;
}
}
words = strtok(NULL, " ");
}
return 0;
}
int spec_cmd(char *buffer)
{
char *words,*words2;
uint32_t address = 0x02000007;
uint32_t data = 0x00000000;
uint32_t result = 0x0;
int errors;
int crate_num = 2;
words = strtok(buffer, " ");
while (words != NULL){
if (words[0] == '-'){
if (words[1] == 'c')
crate_num = atoi(strtok(NULL, " "));
if (words[1] == 'a'){
words2 = strtok(NULL, " ");
address = strtoul(words2,(char**)NULL,16);
}
if (words[1] == 'd'){
words2 = strtok(NULL, " ");
data = strtoul(words2,(char**)NULL,16);
}
if (words[1] == 'h'){
printsend("Usage: spec_cmd -c"
" [crate_num] -a [address] -d [data]\n");
return 0;
}
}
words = strtok(NULL, " ");
}
errors = xl3_rw(address, data, &result, crate_num);
if (errors == 0){
printsend( "result was %08x\n",result);
}
else
printsend("there was a bus error!\n");
return 0;
}
int add_cmd(char *buffer)
{
char *words,*words2;
uint32_t address = 0x02000007;
uint32_t data = 0x00000000;
uint32_t result = 0x0;
int errors;
int crate_num = 2;
words = strtok(buffer, " ");
while (words != NULL){
if (words[0] == '-'){
if (words[1] == 'c')
crate_num = atoi(strtok(NULL, " "));
if (words[1] == 'a'){
words2 = strtok(NULL, " ");
address = strtoul(words2,(char**)NULL,16);
}
if (words[1] == 'd'){
words2 = strtok(NULL, " ");
data = strtoul(words2,(char**)NULL,16);
}
if (words[1] == 'h'){
printsend("Usage: add_cmd -c"
" [crate_num] -a [address] -d [data]\n");
return 0;
}
}
words = strtok(NULL, " ");
}
XL3_Packet packet;
MultiFC *commands = (MultiFC *) packet.payload;
commands->howmany = 1;
commands->cmd[0].address = address;
commands->cmd[0].data = data;
SwapLongBlock(&(commands->cmd[0].address),1);
SwapLongBlock(&(commands->cmd[0].data),1);
SwapLongBlock(&(commands->howmany),1);
packet.cmdHeader.packet_type = FEC_CMD_ID;
do_xl3_cmd(&packet,crate_num);
printsend("going into receive data\n");
receive_data(1,command_number-1,crate_num,&result);
printsend("result was %08x\n",result);
return 0;
}
int sm_reset(char *buffer)
{
char *words,*words2;
int crate_num = 2;
words = strtok(buffer, " ");
while (words != NULL){
if (words[0] == '-'){
if (words[1] == 'c'){
words2 = strtok(NULL, " ");
crate_num = atoi(words2);
}
if (words[1] == 'h'){
printsend( "Usage: sm_reset -c [crate num]\n");
return -1;
}
}
words = strtok(NULL, " ");
}
XL3_Packet packet;
uint32_t *p = (uint32_t *) packet.payload;
packet.cmdHeader.packet_type = STATE_MACHINE_RESET_ID;
do_xl3_cmd(&packet,crate_num);
return 0;
}
int debugging_mode(char *buffer, uint32_t onoff)
{
char *words,*words2;
uint32_t crate_num=2;
XL3_Packet debug_packet;
debug_packet.cmdHeader.packet_type = DEBUGGING_MODE_ID;
uint32_t *payload_ptr = (uint32_t *) debug_packet.payload;
*payload_ptr = onoff;
SwapLongBlock(payload_ptr,1);
words = strtok(buffer, " ");
while (words != NULL){
if (words[0] == '-'){
if (words[1] == 'c')
crate_num = atoi(strtok(NULL," "));
if (words[1] == 'h'){
printsend("Usage: debugging_on/off -c [crate_num]\n");
return 0;
}
}
words = strtok(NULL, " ");
}
do_xl3_cmd(&debug_packet,crate_num);
if (onoff == 1)
printsend("Debugging turned on\n");
else
printsend("Debugging turned off\n");
return 0;
}
void send_pong(int xl3_num)
{
XL3_Packet aPacket;
aPacket.cmdHeader.packet_type = PONG_ID;
do_xl3_cmd_no_response(&aPacket,xl3_num);
}
int deselect_fecs(int crate_num){
XL3_Packet tempPacket;
tempPacket.cmdHeader.packet_type = DESELECT_FECS_ID;
do_xl3_cmd(&tempPacket, crate_num);
return 0;
}
int change_mode(char *buffer)
{
XL3_Packet mode_packet;
uint32_t crate_num=2;
char *words,*words2;
mode_packet.cmdHeader.packet_type = CHANGE_MODE_ID;
uint32_t *payload_ptr = (uint32_t *) mode_packet.payload;
int norm_init = 1;
*payload_ptr = 0x1;
*(payload_ptr+1) = 0x0;
words = strtok(buffer, " ");
while (words != NULL){
if (words[0] == '-'){
if (words[1] == 'c')
crate_num = atoi(strtok(NULL," "));
if (words[1] == 'n')
norm_init = 2;
if (words[1] == 's')
*(payload_ptr+1) = strtoul(strtok(NULL, " "),(char**)NULL,16);
if (words[1] == 'h'){
printsend("Usage: change_mode -c"
" [crate_num] -n [normal mode] -i [init mode] -s [data avail mask]\n");
return 0;
}
}
words = strtok(NULL, " ");
}
*payload_ptr = norm_init;
SwapLongBlock(payload_ptr,2);
do_xl3_cmd(&mode_packet,crate_num);
if (norm_init == 1)
printsend("Mode changed to init mode\n");
else
printsend("Mode changed to normal mode\n");
return 0;
}
int hv_readback(char *buffer)
{
char *words,*words2;
int crate_num = 2;
uint32_t supply_select = 0;
words = strtok(buffer, " ");
while (words != NULL){
if (words[0] == '-'){
if (words[1] == 'c'){
words2 = strtok(NULL, " ");
crate_num = atoi(words2);
}
if (words[1] == 'a'){
supply_select += 1;
}
if (words[1] == 'b'){
supply_select += 2;
}
if (words[1] == 'h'){
printsend( "Usage: read_local_voltage -c [crate num] -a (supply a) -b (supply b)\n");
return -1;
}
}
words = strtok(NULL, " ");
}
XL3_Packet packet;
float voltage_a,voltage_b,current_a,current_b;
voltage_a = 0.;
voltage_b = 0.;
current_a = 0.;
current_b = 0.;
float *p = (float *) packet.payload;
int i;
int nt = 1;
for (i=0;i<nt;i++){
packet.cmdHeader.packet_type = HV_READBACK_ID;
do_xl3_cmd(&packet,crate_num);
SwapLongBlock(p,4);
voltage_a += *p;
voltage_b += *(p+1);
current_a += *(p+2);
current_b += *(p+3);
}
voltage_a /= (float) nt;
voltage_b /= (float) nt;
current_a /= (float) nt;
current_b /= (float) nt;
if (supply_select != 2){
printsend("Supply A - Voltage: %6.3f volts, Current: %6.4f ma\n",voltage_a*300.0,current_a*10.0);
}
if (supply_select > 1){
printsend("Supply B - Voltage: %6.3f volts, Current: %6.4f ma\n",voltage_a*300.0,current_a*10.0);
}
return 0;
}
int read_local_voltage(char *buffer)
{
char *words,*words2;
int crate_num = 2;
uint32_t v_select = 0;
words = strtok(buffer, " ");
while (words != NULL){
if (words[0] == '-'){
if (words[1] == 'c'){
words2 = strtok(NULL, " ");
crate_num = atoi(words2);
}
if (words[1] == 's'){
words2 = strtok(NULL, " ");
v_select = atoi(words2);
}
if (words[1] == 'h'){
printsend( "Usage: read_local_voltage -c [crate num] -s [voltage number]\n");
return -1;
}
}
words = strtok(NULL, " ");
}
XL3_Packet packet;
uint32_t *p = (uint32_t *) packet.payload;
packet.cmdHeader.packet_type = READ_LOCAL_VOLTAGE_ID;
*p = v_select;
SwapLongBlock(p,1);
do_xl3_cmd(&packet,crate_num);
return 0;
}
int hv_ramp_map(char *buffer)
{
char *words,*words2;
int crate_num = 2;
words = strtok(buffer, " ");
while (words != NULL){
if (words[0] == '-'){
if (words[1] == 'c'){
words2 = strtok(NULL, " ");
crate_num = atoi(words2);
}
if (words[1] == 'h'){
printsend( "Usage: read_local_voltage -c [crate num] -s [voltage number]\n");
return -1;
}
}
words = strtok(NULL, " ");
}
float voltage[2817];
float current[2817];
XL3_Packet packet;
float *p = (float *) packet.payload;
uint32_t current_voltage = 0;
uint32_t result;
xl3_rw(0x02000007, 0xFFFFFFFF, &result, crate_num);
xl3_rw(0x02000009, 0x0, &result, crate_num);
xl3_rw(0x02000008, 0x1, &result, crate_num);
int i,j;
for (i=0;i<2817;i+=256){
if (0x1*i < 0xb01){
xl3_rw(0x02000009,0x1*i,&result,crate_num);
}else{
printsend("too high!\n");
}
for (j=0;j<20;j++)
usleep(5000);
packet.cmdHeader.packet_type = HV_READBACK_ID;
do_xl3_cmd(&packet,crate_num);
SwapLongBlock(p,4);
voltage[i]= *p;
current[i] = *(p+2);
}
for (i=0;i<2817;i+=256){
printsend("%d %6.3f %6.3f\n",i,voltage[i]*300.,current[i]*10.);
}
xl3_rw(0x02000009,0x800,&result,crate_num);
xl3_rw(0x02000009,0x500,&result,crate_num);
xl3_rw(0x02000009,0x0,&result,crate_num);
xl3_rw(0x02000008,0x0,&result,crate_num);
return 0;
}