1 |
/*******************************v1.0******************************** |
2 |
|
3 |
Functions to read AC physics data (ACphysics) and AC calibration |
4 |
data (ACcalib). |
5 |
Author: Petter Hofverberg, petter@particle.kth.se |
6 |
|
7 |
0410 v1.0 alive |
8 |
|
9 |
Errors: (returned to the main program as the variable "err") |
10 |
err= |
11 |
0xFF - data (physics or calibration) from both cards found |
12 |
0xF0 - only data from main card found |
13 |
0x0F - only data from extra card found |
14 |
0x00 - no data found |
15 |
|
16 |
******************************************************************/ |
17 |
|
18 |
#include <stdio.h> |
19 |
#include "AC.h" |
20 |
|
21 |
int i,found,j; |
22 |
unsigned short buffer[1000]; |
23 |
unsigned short tmp1,tmp2; |
24 |
unsigned char err; |
25 |
|
26 |
struct physicsstruct physicsdata[2]; |
27 |
struct physicsstruct *physicspointer; |
28 |
|
29 |
struct calibstruct calibdata[2]; |
30 |
struct calibstruct *calibpointer; |
31 |
|
32 |
//Fill physicsdata from buffer |
33 |
void fillphys(int k,unsigned short *buff) |
34 |
{ |
35 |
for(i=0;i<2;i++) physicsdata[k].header[i]=buff[i]; |
36 |
physicsdata[k].status=buff[2]; |
37 |
physicsdata[k].hitmap=buff[3]; |
38 |
for(i=0;i<6;i++) physicsdata[k].regist[i]=buff[4+i]; |
39 |
for(i=0;i<16;i++) physicsdata[k].shift[i]=buff[10+i]; |
40 |
for(i=0;i<16;i++) physicsdata[k].counters[i]=buff[26+i]; |
41 |
for(i=0;i<8;i++) physicsdata[k].coinc[i]=buff[42+i]; |
42 |
physicsdata[k].trigg=buff[50]; |
43 |
for(i=0;i<2;i++) physicsdata[k].clock[i]=buff[51+i]; |
44 |
for(i=0;i<2;i++) physicsdata[k].temp[i]=buff[53+i]; |
45 |
for(i=0;i<8;i++) physicsdata[k].DAC[i]=buff[55+i]; |
46 |
physicsdata[k].CRC=buff[63]; |
47 |
|
48 |
} |
49 |
|
50 |
//Fill calibdata from buffer |
51 |
void fillcalib(int k,unsigned short *buff) |
52 |
{ |
53 |
for(i=0;i<2;i++) calibdata[k].header[i]=buff[i]; |
54 |
for(i=0;i<5;i++) calibdata[k].status[i]=buff[2+i]; |
55 |
for(i=0;i<8;i++) calibdata[k].temp[i]=buff[7+i]; |
56 |
for(i=0;i<8;i++) calibdata[k].DAC1[i]=buff[15+i]; |
57 |
for(i=0;i<8;i++) calibdata[k].DAC2[i]=buff[23+i]; |
58 |
for(i=0;i<6;i++) calibdata[k].regist[i]=buff[31+i]; |
59 |
for(i=0;i<8;i++) calibdata[k].time[i]=buff[37+i]; |
60 |
calibdata[k].n_tr=buff[45]; |
61 |
for(i=0;i<16;i++) calibdata[k].hitmap_tr[i]=buff[46+i]; |
62 |
for(i=0;i<4096;i++) calibdata[k].curve1[i]=buff[62+i]; |
63 |
for(i=0;i<4096;i++) calibdata[k].curve2[i]=buff[4158+i]; |
64 |
calibdata[k].iCRC=buff[8254]; |
65 |
calibdata[k].tail=buff[8255]; |
66 |
calibdata[k].CRC=buff[8256]; |
67 |
printf("tail:CRC > %hx:%hx\n",buff[8255],buff[8256]); |
68 |
} |
69 |
|
70 |
|
71 |
unsigned char ACphysics(int length,unsigned char* datapointer,struct physicsstruct* physicspointer) |
72 |
{ |
73 |
|
74 |
int iter=2; |
75 |
physicspointer[0]=physicsdata[0]; |
76 |
physicspointer[1]=physicsdata[1]; |
77 |
|
78 |
//look for top header ACAC, then unpack data to main card buffer or extra card buffer depending on the subheader |
79 |
//AC11 for main, AC22 for extra |
80 |
while(found<2 && iter<length) |
81 |
{ |
82 |
tmp1 = ((*datapointer) << 8) | *(datapointer+1); |
83 |
tmp2 = ((*(datapointer+2)) << 8) | *(datapointer+3); |
84 |
|
85 |
if(tmp1 == 0xAC11 && ((tmp2 == 0xAC11 && (err&0xF0)==0x0) || (tmp2 == 0xAC22 && (err&0x0F)==0x0))) |
86 |
{ |
87 |
|
88 |
for(i=0;i<64;i++) |
89 |
{ |
90 |
buffer[i]=((*datapointer) << 8) | *(datapointer+1); |
91 |
datapointer=datapointer+2; |
92 |
} |
93 |
|
94 |
if(tmp2==0xAC11){ //main card |
95 |
fillphys(0,&buffer[0]); |
96 |
err |= 0xF0; |
97 |
} |
98 |
else{ //extra card |
99 |
fillphys(1,&buffer[0]); |
100 |
err |= 0x0F; |
101 |
} |
102 |
tmp1=tmp2=0x0000; |
103 |
found++; |
104 |
} |
105 |
else{ |
106 |
datapointer++; |
107 |
iter=iter+1;} |
108 |
} |
109 |
|
110 |
return err; |
111 |
|
112 |
} |
113 |
|
114 |
|
115 |
unsigned char ACcalib(int length, unsigned char* datapointer, struct calibstruct* calibpointer) |
116 |
{ |
117 |
|
118 |
int iter=2; |
119 |
|
120 |
//point struct-pointer to calibdata |
121 |
calibpointer[0]=calibdata[0]; //card1 |
122 |
calibpointer[1]=calibdata[1]; //card2 |
123 |
|
124 |
//look for the top header 0xACCA, then the subheaders 0xAC11 and 0xAC22, and unpack the data |
125 |
//to each card depending on these. |
126 |
while(found<2 && iter<length) |
127 |
{ |
128 |
tmp1 = ((*datapointer) << 8) | *(datapointer+1); |
129 |
tmp2 = ((*(datapointer+2)) << 8) | *(datapointer+3); |
130 |
|
131 |
if(tmp1 == 0xACCA && ((tmp2 == 0xAC11 && (err&0xF0)==0x0) || (tmp2 == 0xAC22 && (err&0x0F)==0x0))) |
132 |
{ |
133 |
for(i=0;i<8258;i++) |
134 |
{ |
135 |
buffer[i]=((*datapointer) << 8) | *(datapointer+1); |
136 |
datapointer=datapointer+2; |
137 |
} |
138 |
|
139 |
if(tmp2==0xAC11){ //main |
140 |
fillcalib(0,&buffer[0]); |
141 |
err |= 0xF0; |
142 |
} |
143 |
else{ //extra |
144 |
fillcalib(1,&buffer[0]); |
145 |
err |= 0x0F; |
146 |
} |
147 |
tmp1=tmp2=0x0000; |
148 |
found++; |
149 |
} |
150 |
else{ |
151 |
datapointer++; |
152 |
iter=iter+1;} |
153 |
} |
154 |
|
155 |
return err; |
156 |
} |
157 |
|