/[PAMELA software]/chewbacca/PamOffLineSW/forroutines/anticounter/AC.c
ViewVC logotype

Annotation of /chewbacca/PamOffLineSW/forroutines/anticounter/AC.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (hide annotations) (download)
Tue Sep 23 07:20:19 2008 UTC (16 years, 2 months ago) by mocchiut
Branch: MAIN
Branch point for: v0r00
File MIME type: text/plain
Initial revision

1 mocchiut 1.1 /*******************************v1.3********************************
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     0502 v1.3 increased buffer size
11    
12     Errors: (returned to the main program as the variable "err")
13     err=
14     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    
19     CRCcheck: 1 - OK, 0 - error
20     ******************************************************************/
21    
22     #include <stdio.h>
23     #include "AC.h"
24     #define TRUE 1
25     #define FALSE 0
26    
27     int i,j,found;
28     unsigned short buffer[8300];
29     unsigned short tmp1,tmp2,check;
30     unsigned char err;
31    
32     struct physicsstruct physicsdata[2];
33     struct physicsstruct *physicspointer;
34    
35     struct calibstruct calibdata;
36     struct calibstruct *calibpointer;
37    
38    
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     //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     if(check==physicsdata[k].CRC)
146     physicsdata[k].CRCcheck=TRUE;
147     else
148     physicsdata[k].CRCcheck=FALSE;
149     }
150    
151     //Fill calibdata from buffer
152     void fillcalib(unsigned short *buff)
153     {
154     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     if(check==calibdata.CRC)
169     calibdata.CRCcheck=TRUE;
170     else
171     calibdata.CRCcheck=FALSE;
172     }
173    
174     unsigned char ACphysics(int length,unsigned char* datapointer,struct physicsstruct* physicspointer)
175     {
176    
177     err=0;
178     found=0;
179     check=0;
180     int iter=2;
181     for(i=0;i<8300;i++)
182     buffer[i]=0;
183     fillphys(0,&buffer[0]);
184     fillphys(1,&buffer[0]);
185    
186     //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     {
190     tmp1 = ((*datapointer) << 8) | *(datapointer+1);
191     tmp2 = ((*(datapointer+2)) << 8) | *(datapointer+3);
192     if(tmp1 == 0xACAC && ((tmp2 == 0xAC11 && (err&0xF0)==0x0) || (tmp2 == 0xAC22 && (err&0x0F)==0x0)))
193     {
194     for(i=0;i<64;i++)
195     {
196     buffer[i]=((*datapointer) << 8) | *(datapointer+1);
197     if(i<63)
198     check=crc(check,buffer[i]);
199     datapointer=datapointer+2;
200     }
201     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     check=0x0;
211     found++;
212     }
213     else{
214     datapointer++;
215     iter=iter+1;}
216     }
217     //point struct-pointer to physicsdata
218     physicspointer[0]=physicsdata[0]; //card1
219     physicspointer[1]=physicsdata[1]; //card2
220    
221     return err;
222    
223     }
224    
225    
226     unsigned char ACcalib(int length, unsigned char* datapointer, struct calibstruct* calibpointer)
227     {
228    
229     err=0;
230     found=0;
231     check=0;
232     int iter=2;
233     for(i=0;i<8300;i++)
234     buffer[i]=0;
235     fillcalib(&buffer[0]);
236    
237     //look for the top header 0xACCA, then the subheaders 0xAC11 and 0xAC22, and unpack the data
238     //to each card depending on these.
239     while(found<1 && iter<length)
240     {
241     tmp1 = ((*datapointer) << 8) | *(datapointer+1);
242     tmp2 = ((*(datapointer+2)) << 8) | *(datapointer+3);
243    
244     if(tmp1 == 0xACCA && ((tmp2 == 0xAC11 && (err&0xF0)==0x0) || (tmp2 == 0xACA2 && (err&0x0F)==0x0)))
245     {
246     for(i=0;i<8257;i++)
247     {
248     buffer[i]=((*datapointer) << 8) | *(datapointer+1);
249     if(i<8256) //8257
250     check=crc(check,buffer[i]);
251     datapointer=datapointer+2;
252     }
253    
254     if(tmp2==0xAC11){ //main
255     fillcalib(&buffer[0]);
256     err |= 0xF0;
257     }
258     else{ //extra
259     fillcalib(&buffer[0]);
260     err |= 0x0F;
261     }
262     tmp1=tmp2=0x0000;
263     found++;
264     }
265     else{
266     datapointer++;
267     iter=iter+1;}
268     }
269     memcpy(calibpointer,&calibdata,sizeof(calibdata));
270    
271     return err;
272     }
273    

  ViewVC Help
Powered by ViewVC 1.1.23