1 |
/*******************************v1.1******************************** |
/*******************************v1.2******************************** |
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). |
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 |
|
|
11 |
Errors: (returned to the main program as the variable "err") |
Errors: (returned to the main program as the variable "err") |
12 |
err= |
err= |
15 |
0x0F - only data from extra card found |
0x0F - only data from extra card found |
16 |
0x00 - no data found |
0x00 - no data found |
17 |
|
|
18 |
|
CRCcheck: 1 - OK, 0 - error |
19 |
******************************************************************/ |
******************************************************************/ |
20 |
|
|
21 |
#include <stdio.h> |
#include <stdio.h> |
22 |
#include "AC.h" |
#include "AC.h" |
23 |
|
#define TRUE 1 |
24 |
|
#define FALSE 0 |
25 |
|
|
26 |
int i,j,found; |
int i,j,found; |
27 |
unsigned short buffer[1000]; |
unsigned short buffer[1000]; |
28 |
unsigned short tmp1,tmp2; |
unsigned short tmp1,tmp2,check; |
29 |
unsigned char err; |
unsigned char err; |
30 |
|
|
31 |
struct physicsstruct physicsdata[2]; |
struct physicsstruct physicsdata[2]; |
34 |
struct calibstruct calibdata; |
struct calibstruct calibdata; |
35 |
struct calibstruct *calibpointer; |
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 ^ 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.b8 ^ c.bit.b12 ^ c.bit.b15 ^ |
80 |
|
d.bit.b4 ^ d.bit.b8 ^ d.bit.b12 ^ d.bit.b15; |
81 |
|
|
82 |
|
r.bit.b5 = c.bit.b0 ^ c.bit.b4 ^ c.bit.b5 ^ c.bit.b8 ^ c.bit.b9 ^ |
83 |
|
c.bit.b11 ^ c.bit.b12 ^ c.bit.b13 ^ |
84 |
|
d.bit.b0 ^ d.bit.b4 ^ d.bit.b5 ^ d.bit.b8 ^ d.bit.b9 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11 = 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 |
//Fill physicsdata from buffer |
130 |
void fillphys(int k,unsigned short *buff) |
void fillphys(int k,unsigned short *buff) |
131 |
{ |
{ |
141 |
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]; |
142 |
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]; |
143 |
physicsdata[k].CRC=buff[63]; |
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 |
//Fill calibdata from buffer |
164 |
calibdata.iCRC=buff[8254]; |
calibdata.iCRC=buff[8254]; |
165 |
calibdata.tail=buff[8255]; |
calibdata.tail=buff[8255]; |
166 |
calibdata.CRC=buff[8256]; |
calibdata.CRC=buff[8256]; |
167 |
//printf("tail:CRC > %hx:%hx\n",buff[8255],buff[8256]); |
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) |
unsigned char ACphysics(int length,unsigned char* datapointer,struct physicsstruct* physicspointer) |
175 |
|
|
176 |
err=0; |
err=0; |
177 |
found=0; |
found=0; |
178 |
|
check=0; |
179 |
int iter=2; |
int iter=2; |
180 |
for(i=0;i<1000;i++) |
for(i=0;i<1000;i++) |
181 |
buffer[i]=0; |
buffer[i]=0; |
198 |
for(i=0;i<64;i++) |
for(i=0;i<64;i++) |
199 |
{ |
{ |
200 |
buffer[i]=((*datapointer) << 8) | *(datapointer+1); |
buffer[i]=((*datapointer) << 8) | *(datapointer+1); |
201 |
|
if(i<63) |
202 |
|
check=crc(check,buffer[i]); |
203 |
datapointer=datapointer+2; |
datapointer=datapointer+2; |
204 |
} |
} |
|
|
|
205 |
if(tmp2==0xAC11){ //main card |
if(tmp2==0xAC11){ //main card |
206 |
fillphys(0,&buffer[0]); |
fillphys(0,&buffer[0]); |
207 |
err |= 0xF0; |
err |= 0xF0; |
211 |
err |= 0x0F; |
err |= 0x0F; |
212 |
} |
} |
213 |
tmp1=tmp2=0x0000; |
tmp1=tmp2=0x0000; |
214 |
|
check=0x0; |
215 |
found++; |
found++; |
216 |
} |
} |
217 |
else{ |
else{ |
232 |
|
|
233 |
err=0; |
err=0; |
234 |
found=0; |
found=0; |
235 |
|
check=0; |
236 |
int iter=2; |
int iter=2; |
237 |
for(i=0;i<1000;i++) |
for(i=0;i<1000;i++) |
238 |
buffer[i]=0; |
buffer[i]=0; |
248 |
{ |
{ |
249 |
tmp1 = ((*datapointer) << 8) | *(datapointer+1); |
tmp1 = ((*datapointer) << 8) | *(datapointer+1); |
250 |
tmp2 = ((*(datapointer+2)) << 8) | *(datapointer+3); |
tmp2 = ((*(datapointer+2)) << 8) | *(datapointer+3); |
251 |
|
|
252 |
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))) |
253 |
{ |
{ |
254 |
for(i=0;i<8258;i++) |
for(i=0;i<8258;i++) |
255 |
{ |
{ |
256 |
buffer[i]=((*datapointer) << 8) | *(datapointer+1); |
buffer[i]=((*datapointer) << 8) | *(datapointer+1); |
257 |
|
if(i<8257) |
258 |
|
check=crc(check,buffer[i]); |
259 |
datapointer=datapointer+2; |
datapointer=datapointer+2; |
260 |
} |
} |
261 |
|
|
262 |
if(tmp2==0xAC11){ //main |
if(tmp2==0xAC11){ //main |
263 |
fillcalib(&buffer[0]); |
fillcalib(&buffer[0]); |
264 |
err |= 0xF0; |
err |= 0xF0; |
266 |
else{ //extra |
else{ //extra |
267 |
fillcalib(&buffer[0]); |
fillcalib(&buffer[0]); |
268 |
err |= 0x0F; |
err |= 0x0F; |
269 |
} |
} |
270 |
tmp1=tmp2=0x0000; |
tmp1=tmp2=0x0000; |
271 |
found++; |
found++; |
272 |
} |
} |
273 |
else{ |
else{ |
274 |
datapointer++; |
datapointer++; |
275 |
iter=iter+1;} |
iter=iter+1;} |
276 |
} |
} |
|
for (i = 0; i < 8200; i++){ |
|
|
printf("%i %x \n", i, datapointer[i]); |
|
|
} |
|
|
for (i = 0; i < 8200; i++){ |
|
|
printf("%i %x \n", i, buffer[i]); |
|
|
} |
|
|
for (i = 0; i < 8200; i++){ |
|
|
printf("%i %x \n", i, *(&calibdata + i)); |
|
|
} |
|
|
for (i = 0; i < 8200; i++){ |
|
|
printf("%i %x \n", i, *(&calibdata + i)); |
|
|
} |
|
|
//memcpy(calibpointer, &calibdata, sizeof(calibdata)); |
|
277 |
*calibpointer=calibdata; //card1 |
*calibpointer=calibdata; //card1 |
278 |
|
|
279 |
return err; |
return err; |
280 |
} |
} |
281 |
|
|