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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4.4 - (hide annotations) (download)
Sat May 28 10:44:11 2005 UTC (19 years, 6 months ago) by kusanagi
Branch: MAIN
Changes since 4.0: +0 -0 lines
File MIME type: text/plain
Main features of this release are:
- updated classes documentations;
- major changes on the calibration fortran routine for the calorimeter
- update on the TMTC thermistors
- removed old classes as CalibTrkBoth and CalibTrd

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

  ViewVC Help
Powered by ViewVC 1.1.23