/[PAMELA software]/yoda/techmodel/forroutines/anticounter/AC.c
ViewVC logotype

Contents of /yoda/techmodel/forroutines/anticounter/AC.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2.8 - (show annotations) (download)
Tue Feb 8 08:23:13 2005 UTC (19 years, 11 months ago) by kusanagi
Branch: MAIN
Changes since 2.7: +120 -24 lines
File MIME type: text/plain
Update to version 1.2

1 /*******************************v1.2********************************
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 0412 v1.1 fixed problem with found, and changed headers
9 0502 v1.2 added a crc check
10
11 Errors: (returned to the main program as the variable "err")
12 err=
13 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
18 CRCcheck: 1 - OK, 0 - error
19 ******************************************************************/
20
21 #include <stdio.h>
22 #include "AC.h"
23 #define TRUE 1
24 #define FALSE 0
25
26 int i,j,found;
27 unsigned short buffer[1000];
28 unsigned short tmp1,tmp2,check;
29 unsigned char err;
30
31 struct physicsstruct physicsdata[2];
32 struct physicsstruct *physicspointer;
33
34 struct calibstruct calibdata;
35 struct calibstruct *calibpointer;
36
37
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 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^ d.bit.b10 ^ d.bit.b11 ^
95 d.bit.b13 ^ d.bit.b14 ^ d.bit.b15;
96
97 r.bit.b8 = c.bit.b3 ^ c.bit.b7 ^ c.bit.b8 ^ c.bit.b11 ^ c.bit.b12 ^
98 c.bit.b14 ^ c.bit.b15 ^
99 d.bit.b3 ^ d.bit.b7 ^ d.bit.b8 ^ d.bit.b11 ^ d.bit.b12 ^
100 d.bit.b14 ^ d.bit.b15;
101
102 r.bit.b9 = c.bit.b4 ^ 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 //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 if(check==physicsdata[k].CRC)
145 physicsdata[k].CRCcheck=TRUE;
146 else
147 physicsdata[k].CRCcheck=FALSE;
148 }
149
150 //Fill calibdata from buffer
151 void fillcalib(unsigned short *buff)
152 {
153 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 if(check==calibdata.CRC)
168 calibdata.CRCcheck=TRUE;
169 else
170 calibdata.CRCcheck=FALSE;
171 }
172
173 unsigned char ACphysics(int length,unsigned char* datapointer,struct physicsstruct* physicspointer)
174 {
175
176 err=0;
177 found=0;
178 check=0;
179 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 //point struct-pointer to physicsdata
186 //physicspointer[0]=physicsdata[0]; //card1
187 //physicspointer[1]=physicsdata[1]; //card2
188 //physicspointer=&physicsdata[0];
189
190 //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 {
194 tmp1 = ((*datapointer) << 8) | *(datapointer+1);
195 tmp2 = ((*(datapointer+2)) << 8) | *(datapointer+3);
196 if(tmp1 == 0xACAC && ((tmp2 == 0xAC11 && (err&0xF0)==0x0) || (tmp2 == 0xAC22 && (err&0x0F)==0x0)))
197 {
198 for(i=0;i<64;i++)
199 {
200 buffer[i]=((*datapointer) << 8) | *(datapointer+1);
201 if(i<63)
202 check=crc(check,buffer[i]);
203 datapointer=datapointer+2;
204 }
205 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 check=0x0;
215 found++;
216 }
217 else{
218 datapointer++;
219 iter=iter+1;}
220 }
221 //point struct-pointer to physicsdata
222 physicspointer[0]=physicsdata[0]; //card1
223 physicspointer[1]=physicsdata[1]; //card2
224
225 return err;
226
227 }
228
229
230 unsigned char ACcalib(int length, unsigned char* datapointer, struct calibstruct* calibpointer)
231 {
232
233 err=0;
234 found=0;
235 check=0;
236 int iter=2;
237 for(i=0;i<1000;i++)
238 buffer[i]=0;
239 fillcalib(&buffer[0]);
240
241 //point struct-pointer to calibdata
242 //calibpointer[0]=calibdata[0]; //card1
243 //calibpointer[1]=calibdata[1]; //card2
244
245 //look for the top header 0xACCA, then the subheaders 0xAC11 and 0xAC22, and unpack the data
246 //to each card depending on these.
247 while(found<1 && iter<length)
248 {
249 tmp1 = ((*datapointer) << 8) | *(datapointer+1);
250 tmp2 = ((*(datapointer+2)) << 8) | *(datapointer+3);
251
252 if(tmp1 == 0xACCA && ((tmp2 == 0xAC11 && (err&0xF0)==0x0) || (tmp2 == 0xACA2 && (err&0x0F)==0x0)))
253 {
254 for(i=0;i<8258;i++)
255 {
256 buffer[i]=((*datapointer) << 8) | *(datapointer+1);
257 if(i<8257)
258 check=crc(check,buffer[i]);
259 datapointer=datapointer+2;
260 }
261
262 if(tmp2==0xAC11){ //main
263 fillcalib(&buffer[0]);
264 err |= 0xF0;
265 }
266 else{ //extra
267 fillcalib(&buffer[0]);
268 err |= 0x0F;
269 }
270 tmp1=tmp2=0x0000;
271 found++;
272 }
273 else{
274 datapointer++;
275 iter=iter+1;}
276 }
277 *calibpointer=calibdata; //card1
278
279 return err;
280 }
281

  ViewVC Help
Powered by ViewVC 1.1.23