3 #define T0_PCF 8 //period for the pcf7931 in us
6 int DemodPCF7931(uint8_t **outBlocks
) {
8 uint8_t bits
[256] = {0x00};
10 uint8_t *dest
= BigBuf_get_addr();
12 int GraphTraceLen
= BigBuf_max_traceLen();
13 if ( GraphTraceLen
> 18000 )
14 GraphTraceLen
= 18000;
16 int i
, j
, lastval
, bitidx
, half_switch
;
18 int tolerance
= clock
/ 8;
22 int lmin
=128, lmax
=128;
25 BigBuf_Clear_keep_EM();
27 LFSetupFPGAForADC(95, true);
28 DoAcquisition_default(0, true);
35 /* Find first local max/min */
36 if(dest
[1] > dest
[0]) {
37 while(i
< GraphTraceLen
) {
38 if( !(dest
[i
] > dest
[i
-1]) && dest
[i
] > lmax
)
45 while(i
< GraphTraceLen
) {
46 if( !(dest
[i
] < dest
[i
-1]) && dest
[i
] < lmin
)
58 for (bitidx
= 0; i
< GraphTraceLen
; i
++)
60 if ( (dest
[i
-1] > dest
[i
] && dir
== 1 && dest
[i
] > lmax
) || (dest
[i
-1] < dest
[i
] && dir
== 0 && dest
[i
] < lmin
))
65 // Switch depending on lc length:
66 // Tolerance is 1/8 of clock rate (arbitrary)
67 if (ABS(lc
-clock
/4) < tolerance
) {
69 if((i
- pmc
) == lc
) { /* 16T0 was previous one */
71 i
+= (128+127+16+32+33+16)-1;
79 } else if (ABS(lc
-clock
/2) < tolerance
) {
81 if((i
- pmc
) == lc
) { /* 16T0 was previous one */
83 i
+= (128+127+16+32+33)-1;
88 else if(half_switch
== 1) {
94 } else if (ABS(lc
-clock
) < tolerance
) {
102 Dbprintf("Error: too many detection errors, aborting.");
107 if(block_done
== 1) {
109 for(j
=0; j
<16; j
++) {
110 blocks
[num_blocks
][j
] = 128*bits
[j
*8+7]+
126 if(i
< GraphTraceLen
)
127 dir
=(dest
[i
-1] > dest
[i
]) ? 0 : 1;
132 if(num_blocks
== 4) break;
134 memcpy(outBlocks
, blocks
, 16*num_blocks
);
138 int IsBlock0PCF7931(uint8_t *Block
) {
139 // Assume RFU means 0 :)
140 if((memcmp(Block
, "\x00\x00\x00\x00\x00\x00\x00\x01", 8) == 0) && memcmp(Block
+9, "\x00\x00\x00\x00\x00\x00\x00", 7) == 0) // PAC enabled
142 if((memcmp(Block
+9, "\x00\x00\x00\x00\x00\x00\x00", 7) == 0) && Block
[7] == 0) // PAC disabled, can it *really* happen ?
147 int IsBlock1PCF7931(uint8_t *Block
) {
148 // Assume RFU means 0 :)
149 if( Block
[10] == 0 &&
153 if ( (Block
[14] & 0x7f) <= 9 && Block
[15] <= 9)
159 uint8_t Blocks
[8][17];
160 uint8_t tmpBlocks
[4][16];
161 int i
, j
, ind
, ind2
, n
;
168 memset(Blocks
, 0, 8*17*sizeof(uint8_t));
171 memset(tmpBlocks
, 0, 4*16*sizeof(uint8_t));
172 n
= DemodPCF7931((uint8_t**)tmpBlocks
);
175 if(error
==10 && num_blocks
== 0) {
176 Dbprintf("Error, no tag or bad tag");
179 else if (tries
==20 || error
==10) {
180 Dbprintf("Error reading the tag");
181 Dbprintf("Here is the partial content");
186 Dbprintf("(dbg) %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
187 tmpBlocks
[i
][0], tmpBlocks
[i
][1], tmpBlocks
[i
][2], tmpBlocks
[i
][3], tmpBlocks
[i
][4], tmpBlocks
[i
][5], tmpBlocks
[i
][6], tmpBlocks
[i
][7],
188 tmpBlocks
[i
][8], tmpBlocks
[i
][9], tmpBlocks
[i
][10], tmpBlocks
[i
][11], tmpBlocks
[i
][12], tmpBlocks
[i
][13], tmpBlocks
[i
][14], tmpBlocks
[i
][15]);
191 if(IsBlock0PCF7931(tmpBlocks
[i
])) {
193 if(i
< n
-1 && IsBlock1PCF7931(tmpBlocks
[i
+1])) {
197 memcpy(Blocks
[0], tmpBlocks
[i
], 16);
198 Blocks
[0][ALLOC
] = 1;
199 memcpy(Blocks
[1], tmpBlocks
[i
+1], 16);
200 Blocks
[1][ALLOC
] = 1;
201 max_blocks
= MAX((Blocks
[1][14] & 0x7f), Blocks
[1][15]) + 1;
203 Dbprintf("(dbg) Max blocks: %d", max_blocks
);
205 // Handle following blocks
206 for(j
=i
+2, ind2
=2; j
!=i
; j
++, ind2
++, num_blocks
++) {
209 memcpy(Blocks
[ind2
], tmpBlocks
[j
], 16);
210 Blocks
[ind2
][ALLOC
] = 1;
218 for(i
=0; i
<n
; i
++) { // Look for identical block in known blocks
219 if(memcmp(tmpBlocks
[i
], "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16)) { // Block is not full of 00
220 for(j
=0; j
<max_blocks
; j
++) {
221 if(Blocks
[j
][ALLOC
] == 1 && !memcmp(tmpBlocks
[i
], Blocks
[j
], 16)) {
222 // Found an identical block
223 for(ind
=i
-1,ind2
=j
-1; ind
>= 0; ind
--,ind2
--) {
226 if(!Blocks
[ind2
][ALLOC
]) { // Block ind2 not already found
227 // Dbprintf("Tmp %d -> Block %d", ind, ind2);
228 memcpy(Blocks
[ind2
], tmpBlocks
[ind
], 16);
229 Blocks
[ind2
][ALLOC
] = 1;
231 if(num_blocks
== max_blocks
) goto end
;
234 for(ind
=i
+1,ind2
=j
+1; ind
< n
; ind
++,ind2
++) {
235 if(ind2
> max_blocks
)
237 if(!Blocks
[ind2
][ALLOC
]) { // Block ind2 not already found
238 // Dbprintf("Tmp %d -> Block %d", ind, ind2);
239 memcpy(Blocks
[ind2
], tmpBlocks
[ind
], 16);
240 Blocks
[ind2
][ALLOC
] = 1;
242 if(num_blocks
== max_blocks
) goto end
;
251 if (BUTTON_PRESS()) return;
252 } while (num_blocks
!= max_blocks
);
254 Dbprintf("-----------------------------------------");
255 Dbprintf("Memory content:");
256 Dbprintf("-----------------------------------------");
257 for(i
=0; i
<max_blocks
; i
++) {
258 if(Blocks
[i
][ALLOC
]==1)
259 Dbprintf("%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
260 Blocks
[i
][0], Blocks
[i
][1], Blocks
[i
][2], Blocks
[i
][3], Blocks
[i
][4], Blocks
[i
][5], Blocks
[i
][6], Blocks
[i
][7],
261 Blocks
[i
][8], Blocks
[i
][9], Blocks
[i
][10], Blocks
[i
][11], Blocks
[i
][12], Blocks
[i
][13], Blocks
[i
][14], Blocks
[i
][15]);
263 Dbprintf("<missing block %d>", i
);
265 Dbprintf("-----------------------------------------");
267 cmd_send(CMD_ACK
,0,0,0,0,0);
271 /* Write on a byte of a PCF7931 tag
272 * @param address : address of the block to write
273 @param byte : address of the byte to write
274 @param data : data to write
276 void WritePCF7931(uint8_t pass1
, uint8_t pass2
, uint8_t pass3
, uint8_t pass4
, uint8_t pass5
, uint8_t pass6
, uint8_t pass7
, uint16_t init_delay
, int32_t l
, int32_t p
, uint8_t address
, uint8_t byte
, uint8_t data
)
278 uint32_t tab
[1024] = {0}; // data times frame
283 //BUILD OF THE DATA FRAME
285 //alimentation of the tag (time for initializing)
286 AddPatternPCF7931(init_delay
, 0, 8192/2*T0_PCF
, tab
);
289 Dbprintf("Initialization delay : %d us", init_delay
);
290 AddPatternPCF7931(8192/2*T0_PCF
+ 319*T0_PCF
+70, 3*T0_PCF
, 29*T0_PCF
, tab
);
292 Dbprintf("Offsets : %d us on the low pulses width, %d us on the low pulses positions", l
, p
);
294 //password indication bit
295 AddBitPCF7931(1, tab
, l
, p
);
297 //password (on 56 bits)
298 Dbprintf("Password (LSB first on each byte) : %02x %02x %02x %02x %02x %02x %02x", pass1
,pass2
,pass3
,pass4
,pass5
,pass6
,pass7
);
299 AddBytePCF7931(pass1
, tab
, l
, p
);
300 AddBytePCF7931(pass2
, tab
, l
, p
);
301 AddBytePCF7931(pass3
, tab
, l
, p
);
302 AddBytePCF7931(pass4
, tab
, l
, p
);
303 AddBytePCF7931(pass5
, tab
, l
, p
);
304 AddBytePCF7931(pass6
, tab
, l
, p
);
305 AddBytePCF7931(pass7
, tab
, l
, p
);
308 //programming mode (0 or 1)
309 AddBitPCF7931(0, tab
, l
, p
);
311 //block adress on 6 bits
312 Dbprintf("Block address : %02x", address
);
315 if (address
&(1<<u
)) { // bit 1
317 AddBitPCF7931(1, tab
, l
, p
);
319 AddBitPCF7931(0, tab
, l
, p
);
323 //byte address on 4 bits
324 Dbprintf("Byte address : %02x", byte
);
327 if (byte
&(1<<u
)) { // bit 1
329 AddBitPCF7931(1, tab
, l
, p
);
331 AddBitPCF7931(0, tab
, l
, p
);
336 Dbprintf("Data : %02x", data
);
339 if (data
&(1<<u
)) { // bit 1
341 AddBitPCF7931(1, tab
, l
, p
);
343 AddBitPCF7931(0, tab
, l
, p
);
350 AddBitPCF7931(0, tab
, l
, p
); //even parity
352 AddBitPCF7931(1, tab
, l
, p
);//odd parity
356 AddPatternPCF7931(5120+2680, 0, 0, tab
);
358 //conversion of the scale time
360 tab
[u
]=(tab
[u
] * 3)/2;
363 //compensation of the counter reload
366 for(u
=0;tab
[u
]!=0;u
++){
379 /* Send a trame to a PCF7931 tags
380 * @param tab : array of the data frame
383 void SendCmdPCF7931(uint32_t * tab
){
384 uint16_t u
=0, tempo
=0;
386 Dbprintf("Sending data frame...");
388 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
390 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
392 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_PASSTHRU
);
396 // steal this pin from the SSP and use it to control the modulation
397 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DOUT
;
398 AT91C_BASE_PIOA
->PIO_OER
= GPIO_SSC_DOUT
;
400 //initialization of the timer
401 AT91C_BASE_PMC
->PMC_PCER
|= (0x1 << AT91C_ID_TC0
);
402 AT91C_BASE_TCB
->TCB_BMR
= AT91C_TCB_TC0XC0S_NONE
| AT91C_TCB_TC1XC1S_TIOA0
| AT91C_TCB_TC2XC2S_NONE
;
403 AT91C_BASE_TC0
->TC_CCR
= AT91C_TC_CLKDIS
; // timer disable
404 AT91C_BASE_TC0
->TC_CMR
= AT91C_TC_CLKS_TIMER_DIV3_CLOCK
; //clock at 48/32 MHz
405 AT91C_BASE_TC0
->TC_CCR
= AT91C_TC_CLKEN
;
406 AT91C_BASE_TCB
->TCB_BCR
= 1;
409 tempo
= AT91C_BASE_TC0
->TC_CV
;
410 for( u
= 0; tab
[u
] != 0; u
+= 3){
414 while(tempo
!= tab
[u
]) tempo
= AT91C_BASE_TC0
->TC_CV
;
416 // stop modulating antenna
418 while(tempo
!= tab
[u
+1]) tempo
= AT91C_BASE_TC0
->TC_CV
;
422 while(tempo
!= tab
[u
+2]) tempo
= AT91C_BASE_TC0
->TC_CV
;
426 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
429 AT91C_BASE_TC0
->TC_CCR
= AT91C_TC_CLKDIS
; // timer disable
434 /* Add a byte for building the data frame of PCF7931 tags
435 * @param b : byte to add
436 * @param tab : array of the data frame
437 * @param l : offset on low pulse width
438 * @param p : offset on low pulse positioning
441 bool AddBytePCF7931(uint8_t byte
, uint32_t * tab
, int32_t l
, int32_t p
){
446 if (byte
&(1<<u
)) { //bit à 1
447 if( AddBitPCF7931(1, tab
, l
, p
)==1) return 1;
449 if (AddBitPCF7931(0, tab
, l
, p
)==1) return 1;
455 /* Add a bits for building the data frame of PCF7931 tags
456 * @param b : bit to add
457 * @param tab : array of the data frame
458 * @param l : offset on low pulse width
459 * @param p : offset on low pulse positioning
461 bool AddBitPCF7931(bool b
, uint32_t * tab
, int32_t l
, int32_t p
){
464 //we put the cursor at the last value of the array
465 for ( u
= 0; tab
[u
] != 0; u
+= 3 ) { }
467 if ( b
== 1 ) { //add a bit 1
469 tab
[u
] = 34 * T0_PCF
+ p
;
471 tab
[u
] = 34 * T0_PCF
+ tab
[u
-1] + p
;
473 tab
[u
+1] = 6 * T0_PCF
+ tab
[u
] + l
;
474 tab
[u
+2] = 88 * T0_PCF
+ tab
[u
+1] - l
- p
;
476 } else { //add a bit 0
479 tab
[u
] = 98 * T0_PCF
+ p
;
481 tab
[u
] = 98 * T0_PCF
+ tab
[u
-1] + p
;
483 tab
[u
+1] = 6 * T0_PCF
+ tab
[u
] + l
;
484 tab
[u
+2] = 24 * T0_PCF
+ tab
[u
+1] - l
- p
;
490 /* Add a custom pattern in the data frame
491 * @param a : delay of the first high pulse
492 * @param b : delay of the low pulse
493 * @param c : delay of the last high pulse
494 * @param tab : array of the data frame
496 bool AddPatternPCF7931(uint32_t a
, uint32_t b
, uint32_t c
, uint32_t * tab
){
498 for(u
= 0; tab
[u
] != 0; u
+= 3){} //we put the cursor at the last value of the array
500 tab
[u
] = (u
== 0) ? a
: a
+ tab
[u
-1];
501 tab
[u
+1] = b
+ tab
[u
];
502 tab
[u
+2] = c
+ tab
[u
+1];