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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2.6 by kusanagi, Mon Jan 3 14:20:11 2005 UTC revision 3.0 by kusanagi, Fri Mar 4 15:54:11 2005 UTC
# Line 1  Line 1 
1  /*******************************v1.1********************************  /*******************************v1.3********************************
2    
3  Functions to read AC physics data (ACphysics) and AC calibration  Functions to read AC physics data (ACphysics) and AC calibration
4  data (ACcalib).  data (ACcalib).
# Line 6  Author: Petter Hofverberg, petter@partic Line 6  Author: Petter Hofverberg, petter@partic
6    
7  0410 v1.0 alive  0410 v1.0 alive
8  0412 v1.1 fixed problem with found, and changed headers  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")  Errors: (returned to the main program as the variable "err")
13  err=  err=
# Line 14  err= Line 16  err=
16  0x0F - only data from extra card found  0x0F - only data from extra card found
17  0x00 - no data found  0x00 - no data found
18    
19    CRCcheck:  1 - OK, 0 - error
20  ******************************************************************/  ******************************************************************/
21    
22  #include <stdio.h>  #include <stdio.h>
23  #include "AC.h"  #include "AC.h"
24    #define TRUE 1
25    #define FALSE 0
26    
27  int i,j,found;  int i,j,found;
28  unsigned short buffer[1000];  unsigned short buffer[8300];
29  unsigned short tmp1,tmp2;  unsigned short tmp1,tmp2,check;
30  unsigned char err;  unsigned char err;
31    
32  struct physicsstruct physicsdata[2];  struct physicsstruct physicsdata[2];
# Line 30  struct physicsstruct *physicspointer; Line 35  struct physicsstruct *physicspointer;
35  struct calibstruct calibdata;  struct calibstruct calibdata;
36  struct calibstruct *calibpointer;  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  //Fill physicsdata from buffer
131  void fillphys(int k,unsigned short *buff)  void fillphys(int k,unsigned short *buff)
132  {  {
# Line 45  void fillphys(int k,unsigned short *buff Line 142  void fillphys(int k,unsigned short *buff
142    for(i=0;i<2;i++) physicsdata[k].temp[i]=buff[53+i];    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];    for(i=0;i<8;i++) physicsdata[k].DAC[i]=buff[55+i];
144    physicsdata[k].CRC=buff[63];    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  //Fill calibdata from buffer
# Line 65  void fillcalib(unsigned short *buff) Line 165  void fillcalib(unsigned short *buff)
165    calibdata.iCRC=buff[8254];    calibdata.iCRC=buff[8254];
166    calibdata.tail=buff[8255];    calibdata.tail=buff[8255];
167    calibdata.CRC=buff[8256];    calibdata.CRC=buff[8256];
168    //printf("tail:CRC > %hx:%hx\n",buff[8255],buff[8256]);    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)  unsigned char ACphysics(int length,unsigned char* datapointer,struct physicsstruct* physicspointer)
# Line 73  unsigned char ACphysics(int length,unsig Line 176  unsigned char ACphysics(int length,unsig
176    
177    err=0;    err=0;
178    found=0;    found=0;
179      check=0;
180    int iter=2;    int iter=2;
181    for(i=0;i<1000;i++)    for(i=0;i<8300;i++)
182      buffer[i]=0;      buffer[i]=0;
183    fillphys(0,&buffer[0]);    fillphys(0,&buffer[0]);
184    fillphys(1,&buffer[0]);    fillphys(1,&buffer[0]);
185    
   //point struct-pointer to physicsdata  
   //physicspointer[0]=physicsdata[0];  //card1  
   //physicspointer[1]=physicsdata[1];  //card2  
   //physicspointer=&physicsdata[0];  
   
186    //look for top header ACAC, then unpack data to main card buffer or extra card buffer depending on the subheader    //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    //AC11 for main, AC22 for extra
188    while(found<2 && iter<length)    while(found<2 && iter<length)
# Line 95  unsigned char ACphysics(int length,unsig Line 194  unsigned char ACphysics(int length,unsig
194            for(i=0;i<64;i++)            for(i=0;i<64;i++)
195              {              {
196                buffer[i]=((*datapointer) << 8) | *(datapointer+1);                buffer[i]=((*datapointer) << 8) | *(datapointer+1);
197                  if(i<63)
198                    check=crc(check,buffer[i]);
199                datapointer=datapointer+2;                datapointer=datapointer+2;
200              }              }
             
201            if(tmp2==0xAC11){          //main card            if(tmp2==0xAC11){          //main card
202              fillphys(0,&buffer[0]);              fillphys(0,&buffer[0]);
203              err |= 0xF0;              err |= 0xF0;
# Line 107  unsigned char ACphysics(int length,unsig Line 207  unsigned char ACphysics(int length,unsig
207              err |= 0x0F;              err |= 0x0F;
208            }            }
209            tmp1=tmp2=0x0000;            tmp1=tmp2=0x0000;
210              check=0x0;
211            found++;            found++;
212          }          }
213        else{        else{
# Line 127  unsigned char ACcalib(int length, unsign Line 228  unsigned char ACcalib(int length, unsign
228    
229    err=0;    err=0;
230    found=0;    found=0;
231      check=0;
232    int iter=2;    int iter=2;
233    for(i=0;i<1000;i++)    for(i=0;i<8300;i++)
234      buffer[i]=0;      buffer[i]=0;
235    fillcalib(&buffer[0]);    fillcalib(&buffer[0]);
236    
   //point struct-pointer to calibdata  
   //calibpointer[0]=calibdata[0];  //card1  
   //calibpointer[1]=calibdata[1];  //card2  
   
237    //look for the top header 0xACCA, then the subheaders 0xAC11 and 0xAC22, and unpack the data    //look for the top header 0xACCA, then the subheaders 0xAC11 and 0xAC22, and unpack the data
238    //to each card depending on these.    //to each card depending on these.
239    while(found<1 && iter<length)    while(found<1 && iter<length)
240      {      {
241        tmp1 = ((*datapointer) << 8) | *(datapointer+1);        tmp1 = ((*datapointer) << 8) | *(datapointer+1);
242        tmp2 = ((*(datapointer+2)) << 8) | *(datapointer+3);        tmp2 = ((*(datapointer+2)) << 8) | *(datapointer+3);
243          
244        if(tmp1 == 0xACCA && ((tmp2 == 0xAC11 && (err&0xF0)==0x0) || (tmp2 == 0xACA2 && (err&0x0F)==0x0)))        if(tmp1 == 0xACCA && ((tmp2 == 0xAC11 && (err&0xF0)==0x0) || (tmp2 == 0xACA2 && (err&0x0F)==0x0)))
245          {          {
246            for(i=0;i<8258;i++)            for(i=0;i<8257;i++)
247              {              {
248                buffer[i]=((*datapointer) << 8) | *(datapointer+1);                buffer[i]=((*datapointer) << 8) | *(datapointer+1);
249                  if(i<8256) //8257
250                    check=crc(check,buffer[i]);
251                datapointer=datapointer+2;                datapointer=datapointer+2;
252              }                }
253                      
254            if(tmp2==0xAC11){          //main            if(tmp2==0xAC11){          //main
255              fillcalib(&buffer[0]);              fillcalib(&buffer[0]);
256              err |= 0xF0;              err |= 0xF0;
# Line 158  unsigned char ACcalib(int length, unsign Line 258  unsigned char ACcalib(int length, unsign
258            else{                      //extra            else{                      //extra
259              fillcalib(&buffer[0]);              fillcalib(&buffer[0]);
260              err |= 0x0F;              err |= 0x0F;
261            }              }
262             tmp1=tmp2=0x0000;            tmp1=tmp2=0x0000;
263            found++;            found++;
264          }          }
265        else{        else{
266          datapointer++;          datapointer++;
267          iter=iter+1;}          iter=iter+1;}
268      }      }
269    *calibpointer=calibdata;  //card1    memcpy(calibpointer,&calibdata,sizeof(calibdata));
270      
271    return err;    return err;
272  }  }
273    

Legend:
Removed from v.2.6  
changed lines
  Added in v.3.0

  ViewVC Help
Powered by ViewVC 1.1.23