1 |
kusanagi |
2.8 |
/*******************************v1.2******************************** |
2 |
kusanagi |
2.2 |
|
3 |
|
|
Functions to read AC physics data (ACphysics) and AC calibration |
4 |
|
|
data (ACcalib). |
5 |
kusanagi |
2.1 |
Author: Petter Hofverberg, petter@particle.kth.se |
6 |
|
|
|
7 |
kusanagi |
2.2 |
0410 v1.0 alive |
8 |
kusanagi |
2.5 |
0412 v1.1 fixed problem with found, and changed headers |
9 |
kusanagi |
2.8 |
0502 v1.2 added a crc check |
10 |
kusanagi |
2.2 |
|
11 |
kusanagi |
2.1 |
Errors: (returned to the main program as the variable "err") |
12 |
|
|
err= |
13 |
kusanagi |
2.2 |
0xFF - data (physics or calibration) from both cards found |
14 |
|
|
0xF0 - only data from main card found |
15 |
|
|
0x0F - only data from extra card found |
16 |
|
|
0x00 - no data found |
17 |
kusanagi |
2.1 |
|
18 |
kusanagi |
2.8 |
CRCcheck: 1 - OK, 0 - error |
19 |
kusanagi |
2.1 |
******************************************************************/ |
20 |
|
|
|
21 |
|
|
#include <stdio.h> |
22 |
|
|
#include "AC.h" |
23 |
kusanagi |
2.8 |
#define TRUE 1 |
24 |
|
|
#define FALSE 0 |
25 |
kusanagi |
2.1 |
|
26 |
kusanagi |
2.5 |
int i,j,found; |
27 |
kusanagi |
2.1 |
unsigned short buffer[1000]; |
28 |
kusanagi |
2.8 |
unsigned short tmp1,tmp2,check; |
29 |
kusanagi |
2.2 |
unsigned char err; |
30 |
kusanagi |
2.1 |
|
31 |
kusanagi |
2.2 |
struct physicsstruct physicsdata[2]; |
32 |
kusanagi |
2.1 |
struct physicsstruct *physicspointer; |
33 |
|
|
|
34 |
kusanagi |
2.6 |
struct calibstruct calibdata; |
35 |
kusanagi |
2.1 |
struct calibstruct *calibpointer; |
36 |
|
|
|
37 |
kusanagi |
2.8 |
|
38 |
|
|
//CRC check function |
39 |
|
|
short crc(short old,short new) |
40 |
|
|
{ |
41 |
|
|
union crc_data { |
42 |
|
|
short word; |
43 |
|
|
struct bit_field { |
44 |
|
|
unsigned b0:1; |
45 |
|
|
unsigned b1:1; |
46 |
|
|
unsigned b2:1; |
47 |
|
|
unsigned b3:1; |
48 |
|
|
unsigned b4:1; |
49 |
|
|
unsigned b5:1; |
50 |
|
|
unsigned b6:1; |
51 |
|
|
unsigned b7:1; |
52 |
|
|
unsigned b8:1; |
53 |
|
|
unsigned b9:1; |
54 |
|
|
unsigned b10:1; |
55 |
|
|
unsigned b11:1; |
56 |
|
|
unsigned b12:1; |
57 |
|
|
unsigned b13:1; |
58 |
|
|
unsigned b14:1; |
59 |
|
|
unsigned b15:1; |
60 |
|
|
} bit; |
61 |
|
|
} c,d,r; |
62 |
|
|
|
63 |
|
|
c.word = old; |
64 |
|
|
d.word = new; |
65 |
|
|
r.word = 0; |
66 |
|
|
|
67 |
|
|
r.bit.b0 = c.bit.b0 ^ c.bit.b4 ^ c.bit.b8 ^ c.bit.b11 ^ c.bit.b12 ^ |
68 |
|
|
d.bit.b0 ^ d.bit.b4 ^ d.bit.b8 ^ d.bit.b11 ^ d.bit.b12; |
69 |
|
|
|
70 |
|
|
r.bit.b1 = c.bit.b1 ^ c.bit.b5 ^ c.bit.b9 ^ c.bit.b12 ^ c.bit.b13 ^ |
71 |
|
|
d.bit.b1 ^ d.bit.b5 ^ d.bit.b9 ^ d.bit.b12 ^ d.bit.b13; |
72 |
|
|
|
73 |
|
|
r.bit.b2 = c.bit.b2 ^ c.bit.b6 ^ c.bit.b10 ^ c.bit.b13 ^ c.bit.b14 ^ |
74 |
|
|
d.bit.b2 ^ d.bit.b6 ^ d.bit.b10 ^ d.bit.b13 ^ d.bit.b14; |
75 |
|
|
|
76 |
|
|
r.bit.b3 = c.bit.b3 ^ c.bit.b7 ^ c.bit.b11 ^ c.bit.b14 ^ c.bit.b15 ^ |
77 |
|
|
d.bit.b3 ^ d.bit.b7 ^ d.bit.b11 ^ d.bit.b14 ^ d.bit.b15; |
78 |
|
|
|
79 |
|
|
r.bit.b4 = c.bit.b4 ^ c.bit.b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c.bit.b8 ^ c.bit.b9 ^ c.bit.b12 ^ c.bit.b13 ^ |
103 |
|
|
c.bit.b15 ^ |
104 |
|
|
d.bit.b4 ^ d.bit.b8 ^ d.bit.b9 ^ d.bit.b12 ^ d.bit.b13 ^ |
105 |
|
|
d.bit.b15; |
106 |
|
|
|
107 |
|
|
r.bit.b10 = c.bit.b5 ^ c.bit.b9 ^ c.bit.b10 ^ c.bit.b13 ^ c.bit.b14 ^ |
108 |
|
|
d.bit.b5 ^ d.bit.b9 ^ d.bit.b10 ^ d.bit.b13 ^ d.bit.b14; |
109 |
|
|
|
110 |
|
|
r.bit.b11 = c.bit.b6 ^ c.bit.b10 ^ c.bit.b11 ^ c.bit.b14 ^ c.bit.b15 ^ |
111 |
|
|
d.bit.b6 ^ d.bit.b10 ^ d.bit.b11 ^ d.bit.b14 ^ d.bit.b15; |
112 |
|
|
|
113 |
|
|
r.bit.b12 = c.bit.b0 ^ c.bit.b4 ^ c.bit.b7 ^ c.bit.b8 ^ c.bit.b15 ^ |
114 |
|
|
d.bit.b0 ^ d.bit.b4 ^ d.bit.b7 ^ d.bit.b8 ^ d.bit.b15; |
115 |
|
|
|
116 |
|
|
r.bit.b13 = c.bit.b1 ^ c.bit.b5 ^ c.bit.b8 ^ c.bit.b9 ^ |
117 |
|
|
d.bit.b1 ^ d.bit.b5 ^ d.bit.b8 ^ d.bit.b9; |
118 |
|
|
|
119 |
|
|
r.bit.b14 = c.bit.b2 ^ c.bit.b6 ^ c.bit.b9 ^ c.bit.b10 ^ |
120 |
|
|
d.bit.b2 ^ d.bit.b6 ^ d.bit.b9 ^ d.bit.b10; |
121 |
|
|
|
122 |
|
|
r.bit.b15 = c.bit.b3 ^ c.bit.b7 ^ c.bit.b10 ^ c.bit.b11 ^ |
123 |
|
|
d.bit.b3 ^ d.bit.b7 ^ d.bit.b10 ^ d.bit.b11; |
124 |
|
|
|
125 |
|
|
return r.word; |
126 |
|
|
} |
127 |
|
|
|
128 |
|
|
|
129 |
kusanagi |
2.3 |
//Fill physicsdata from buffer |
130 |
|
|
void fillphys(int k,unsigned short *buff) |
131 |
|
|
{ |
132 |
|
|
for(i=0;i<2;i++) physicsdata[k].header[i]=buff[i]; |
133 |
|
|
physicsdata[k].status=buff[2]; |
134 |
|
|
physicsdata[k].hitmap=buff[3]; |
135 |
|
|
for(i=0;i<6;i++) physicsdata[k].regist[i]=buff[4+i]; |
136 |
|
|
for(i=0;i<16;i++) physicsdata[k].shift[i]=buff[10+i]; |
137 |
|
|
for(i=0;i<16;i++) physicsdata[k].counters[i]=buff[26+i]; |
138 |
|
|
for(i=0;i<8;i++) physicsdata[k].coinc[i]=buff[42+i]; |
139 |
|
|
physicsdata[k].trigg=buff[50]; |
140 |
|
|
for(i=0;i<2;i++) physicsdata[k].clock[i]=buff[51+i]; |
141 |
|
|
for(i=0;i<2;i++) physicsdata[k].temp[i]=buff[53+i]; |
142 |
|
|
for(i=0;i<8;i++) physicsdata[k].DAC[i]=buff[55+i]; |
143 |
|
|
physicsdata[k].CRC=buff[63]; |
144 |
kusanagi |
2.8 |
if(check==physicsdata[k].CRC) |
145 |
|
|
physicsdata[k].CRCcheck=TRUE; |
146 |
|
|
else |
147 |
|
|
physicsdata[k].CRCcheck=FALSE; |
148 |
kusanagi |
2.3 |
} |
149 |
|
|
|
150 |
|
|
//Fill calibdata from buffer |
151 |
kusanagi |
2.6 |
void fillcalib(unsigned short *buff) |
152 |
kusanagi |
2.3 |
{ |
153 |
kusanagi |
2.6 |
for(i=0;i<2;i++) calibdata.header[i]=buff[i]; |
154 |
|
|
for(i=0;i<5;i++) calibdata.status[i]=buff[2+i]; |
155 |
|
|
for(i=0;i<8;i++) calibdata.temp[i]=buff[7+i]; |
156 |
|
|
for(i=0;i<8;i++) calibdata.DAC1[i]=buff[15+i]; |
157 |
|
|
for(i=0;i<8;i++) calibdata.DAC2[i]=buff[23+i]; |
158 |
|
|
for(i=0;i<6;i++) calibdata.regist[i]=buff[31+i]; |
159 |
|
|
for(i=0;i<8;i++) calibdata.time[i]=buff[37+i]; |
160 |
|
|
calibdata.n_tr=buff[45]; |
161 |
|
|
for(i=0;i<16;i++) calibdata.hitmap_tr[i]=buff[46+i]; |
162 |
|
|
for(i=0;i<4096;i++) calibdata.curve1[i]=buff[62+i]; |
163 |
|
|
for(i=0;i<4096;i++) calibdata.curve2[i]=buff[4158+i]; |
164 |
|
|
calibdata.iCRC=buff[8254]; |
165 |
|
|
calibdata.tail=buff[8255]; |
166 |
|
|
calibdata.CRC=buff[8256]; |
167 |
kusanagi |
2.8 |
if(check==calibdata.CRC) |
168 |
|
|
calibdata.CRCcheck=TRUE; |
169 |
|
|
else |
170 |
|
|
calibdata.CRCcheck=FALSE; |
171 |
kusanagi |
2.3 |
} |
172 |
kusanagi |
2.1 |
|
173 |
kusanagi |
2.2 |
unsigned char ACphysics(int length,unsigned char* datapointer,struct physicsstruct* physicspointer) |
174 |
kusanagi |
2.1 |
{ |
175 |
|
|
|
176 |
kusanagi |
2.5 |
err=0; |
177 |
|
|
found=0; |
178 |
kusanagi |
2.8 |
check=0; |
179 |
kusanagi |
2.5 |
int iter=2; |
180 |
|
|
for(i=0;i<1000;i++) |
181 |
|
|
buffer[i]=0; |
182 |
|
|
fillphys(0,&buffer[0]); |
183 |
|
|
fillphys(1,&buffer[0]); |
184 |
|
|
|
185 |
kusanagi |
2.6 |
//point struct-pointer to physicsdata |
186 |
|
|
//physicspointer[0]=physicsdata[0]; //card1 |
187 |
|
|
//physicspointer[1]=physicsdata[1]; //card2 |
188 |
|
|
//physicspointer=&physicsdata[0]; |
189 |
kusanagi |
2.1 |
|
190 |
kusanagi |
2.2 |
//look for top header ACAC, then unpack data to main card buffer or extra card buffer depending on the subheader |
191 |
|
|
//AC11 for main, AC22 for extra |
192 |
|
|
while(found<2 && iter<length) |
193 |
kusanagi |
2.1 |
{ |
194 |
|
|
tmp1 = ((*datapointer) << 8) | *(datapointer+1); |
195 |
kusanagi |
2.2 |
tmp2 = ((*(datapointer+2)) << 8) | *(datapointer+3); |
196 |
kusanagi |
2.4 |
if(tmp1 == 0xACAC && ((tmp2 == 0xAC11 && (err&0xF0)==0x0) || (tmp2 == 0xAC22 && (err&0x0F)==0x0))) |
197 |
kusanagi |
2.1 |
{ |
198 |
|
|
for(i=0;i<64;i++) |
199 |
|
|
{ |
200 |
|
|
buffer[i]=((*datapointer) << 8) | *(datapointer+1); |
201 |
kusanagi |
2.8 |
if(i<63) |
202 |
|
|
check=crc(check,buffer[i]); |
203 |
kusanagi |
2.1 |
datapointer=datapointer+2; |
204 |
|
|
} |
205 |
kusanagi |
2.2 |
if(tmp2==0xAC11){ //main card |
206 |
|
|
fillphys(0,&buffer[0]); |
207 |
|
|
err |= 0xF0; |
208 |
|
|
} |
209 |
|
|
else{ //extra card |
210 |
|
|
fillphys(1,&buffer[0]); |
211 |
|
|
err |= 0x0F; |
212 |
|
|
} |
213 |
|
|
tmp1=tmp2=0x0000; |
214 |
kusanagi |
2.8 |
check=0x0; |
215 |
kusanagi |
2.2 |
found++; |
216 |
kusanagi |
2.1 |
} |
217 |
|
|
else{ |
218 |
|
|
datapointer++; |
219 |
|
|
iter=iter+1;} |
220 |
|
|
} |
221 |
kusanagi |
2.6 |
//point struct-pointer to physicsdata |
222 |
|
|
physicspointer[0]=physicsdata[0]; //card1 |
223 |
|
|
physicspointer[1]=physicsdata[1]; //card2 |
224 |
|
|
|
225 |
kusanagi |
2.1 |
return err; |
226 |
|
|
|
227 |
|
|
} |
228 |
|
|
|
229 |
|
|
|
230 |
kusanagi |
2.2 |
unsigned char ACcalib(int length, unsigned char* datapointer, struct calibstruct* calibpointer) |
231 |
kusanagi |
2.1 |
{ |
232 |
|
|
|
233 |
kusanagi |
2.5 |
err=0; |
234 |
|
|
found=0; |
235 |
kusanagi |
2.8 |
check=0; |
236 |
kusanagi |
2.1 |
int iter=2; |
237 |
kusanagi |
2.5 |
for(i=0;i<1000;i++) |
238 |
|
|
buffer[i]=0; |
239 |
kusanagi |
2.6 |
fillcalib(&buffer[0]); |
240 |
kusanagi |
2.5 |
|
241 |
kusanagi |
2.1 |
//point struct-pointer to calibdata |
242 |
kusanagi |
2.6 |
//calibpointer[0]=calibdata[0]; //card1 |
243 |
|
|
//calibpointer[1]=calibdata[1]; //card2 |
244 |
kusanagi |
2.1 |
|
245 |
kusanagi |
2.2 |
//look for the top header 0xACCA, then the subheaders 0xAC11 and 0xAC22, and unpack the data |
246 |
|
|
//to each card depending on these. |
247 |
kusanagi |
2.6 |
while(found<1 && iter<length) |
248 |
kusanagi |
2.1 |
{ |
249 |
|
|
tmp1 = ((*datapointer) << 8) | *(datapointer+1); |
250 |
kusanagi |
2.2 |
tmp2 = ((*(datapointer+2)) << 8) | *(datapointer+3); |
251 |
kusanagi |
2.8 |
|
252 |
kusanagi |
2.5 |
if(tmp1 == 0xACCA && ((tmp2 == 0xAC11 && (err&0xF0)==0x0) || (tmp2 == 0xACA2 && (err&0x0F)==0x0))) |
253 |
kusanagi |
2.1 |
{ |
254 |
kusanagi |
2.3 |
for(i=0;i<8258;i++) |
255 |
kusanagi |
2.1 |
{ |
256 |
|
|
buffer[i]=((*datapointer) << 8) | *(datapointer+1); |
257 |
kusanagi |
2.8 |
if(i<8257) |
258 |
|
|
check=crc(check,buffer[i]); |
259 |
kusanagi |
2.1 |
datapointer=datapointer+2; |
260 |
kusanagi |
2.8 |
} |
261 |
|
|
|
262 |
kusanagi |
2.2 |
if(tmp2==0xAC11){ //main |
263 |
kusanagi |
2.6 |
fillcalib(&buffer[0]); |
264 |
kusanagi |
2.2 |
err |= 0xF0; |
265 |
|
|
} |
266 |
|
|
else{ //extra |
267 |
kusanagi |
2.6 |
fillcalib(&buffer[0]); |
268 |
kusanagi |
2.2 |
err |= 0x0F; |
269 |
kusanagi |
2.8 |
} |
270 |
|
|
tmp1=tmp2=0x0000; |
271 |
kusanagi |
2.2 |
found++; |
272 |
kusanagi |
2.1 |
} |
273 |
|
|
else{ |
274 |
kusanagi |
2.2 |
datapointer++; |
275 |
|
|
iter=iter+1;} |
276 |
kusanagi |
2.1 |
} |
277 |
kusanagi |
2.6 |
*calibpointer=calibdata; //card1 |
278 |
kusanagi |
2.8 |
|
279 |
kusanagi |
2.1 |
return err; |
280 |
|
|
} |
281 |
kusanagi |
2.8 |
|