summaryrefslogtreecommitdiff
path: root/nfclib/iso15693.c
blob: dc32578b3d1d3ea6529762af6d1160dca0ee1cb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
//*****************************************************************************
//
// iso15693.c - The top level API used to communicate with ISO15063 cards.
//
// Copyright (c) 2010-2014 Texas Instruments Incorporated.  All rights reserved.
// Software License Agreement
//
// Texas Instruments (TI) is supplying this software for use solely and
// exclusively on TI's microcontroller products. The software is owned by
// TI and/or its suppliers, and is protected under applicable copyright
// laws. You may not combine this software with "viral" open-source
// software in order to form a larger program.
//
// THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
// NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
// NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
// CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
// DAMAGES, FOR ANY REASON WHATSOEVER.
//
//*****************************************************************************

#include <string.h>
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/sysctl.h"
#include "trf79x0.h"
#include "nfclib/iso15693.h"

extern struct
{
    //
    // The actual string of bytes in the UID.
    //
    unsigned char pucUID[UID_SIZE];

    //
    // The number of valid bytes in the pucUID variable.
    //
    unsigned long ulUIDSize;

    //
    // The ASCII string that is used to display the UID on the screen.
    //
    char pcUIDStr[CARD_LABEL_SIZE];

    unsigned char ucSlot;
}g_sCard_15693[16];

//*****************************************************************************
//
// Command/Response and transmit/receive buffer.
//
//*****************************************************************************
static unsigned char g_pucCmd[16];

//*****************************************************************************
//
// The value that is written to the block if the "Erase" button is pressed.
// This will invalidate the block.
//
//*****************************************************************************
static const unsigned char g_pucValueEmpty[] =
{
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

static unsigned char ucCardFound = 0;

//*****************************************************************************
//
// Set up registers for ISO 15693 operation.  This function must
// be called after initializing the TRF79x0 (for example with TRF79x0Init()
// or TRF79x0Command() with argument \b TRF79X0_SOFT_INIT_CMD) and before
// calling any of the other ISO15963 functions.
//
//*****************************************************************************
void
ISO15693SetupRegisters(void)
{
	// actually, we can just use the default setting
#if 0
    //
    // Set the TX pulse to 9.44us (0x80 * 73.7ns).
    //
    TRF79x0WriteRegister(TRF79X0_TX_PULSE_LENGTH_CTRL_REG, 0x80);

    //
    // Set the RX No response wait time to 529us (0xe * 37.76us).
    //
    TRF79x0WriteRegister(TRF79X0_RX_NO_RESPONSE_WAIT_REG, 0x0e);

    //
    // Set the RX wait time to 293us (0x20 * 9.44us).
    //
    TRF79x0WriteRegister(TRF79X0_RX_WAIT_TIME_REG, 0x20);

    //
    // Configure the Special Settings Register.
    //
    TRF79x0WriteRegister(TRF79X0_RX_SPECIAL_SETTINGS_REG,
       (TRF79x0ReadRegister(TRF79X0_RX_SPECIAL_SETTINGS_REG) & 0x0f) |
       TRF79X0_RX_SP_SET_C424);

    //
    // Configure the Test Settings Register.
    //
    TRF79x0WriteRegister(TRF79X0_TEST_SETTING1_REG, 0x20);
#endif

    //
    // Set the SYSCLK to 6.78MHz and the Modulation Depth to OOK.
    //
    TRF79x0WriteRegister(TRF79X0_MODULATOR_CONTROL_REG,
                         (TRF79X0_MOD_CTRL_SYS_CLK_6_78MHZ |
                          TRF79X0_MOD_CTRL_MOD_ASK_10));

    //
    // Set the regulator voltage to be automatic.
    //
    TRF79x0WriteRegister(TRF79X0_REGULATOR_CONTROL_REG,
                         TRF79X0_REGULATOR_CTRL_AUTO_REG);

    //
    // Set the regulator voltage to be automatic.
    //
    TRF79x0WriteRegister(TRF79X0_CHIP_STATUS_CTRL_REG,
    					 TRF79X0_STATUS_CTRL_RF_ON | TRF79X0_STATUS_CTRL_RF_PWR_FULL
    					 | TRF79X0_STATUS_CTRL_5V_OPERATION);

    //
    // Set the ISO format to ISO15693 high bit rate, 26.48 kbps, one subcarrier, 1 out of 4
    //
    TRF79x0WriteRegister(TRF79X0_ISO_CONTROL_REG,
    		TRF79X0_ISO_CONTROL_15693_HIGH_1SUB_1OUT4);
}

//*****************************************************************************
//
//! Initializes the ISO15693 utility functions.
//!
//! This function prepares the ISO1593 utility functions so that they are
//! prepared for the remaining ISO1593 calls.  This function must be called once
//! before calling any other ISO1593 functions.
//!
//! \return None.
//
//*****************************************************************************
void
ISO15693Init(void)
{
    //
    // Initialize RFID hardware.
    //
    TRF79x0Init();

    //
    // Set up ISO 15693 operation.
    //
    ISO15693SetupRegisters();
}

void
ISO15693NextSlot(void)
{
    TRF79x0Command(TRF79X0_STOP_DECODERS_CMD);
    TRF79x0Command(TRF79X0_RUN_DECODERS_CMD);
    TRF79x0Command(TRF79X0_RESET_FIFO_CMD);
    TRF79x0Command(TRF79X0_TRANSMIT_NEXT_SLOT_CMD);
}

//
//	\ucSubCarrier,
//	0		A single sub-carrier frequency shall be used by the VICC
//	1		Two sub-carriers shall be used by the VICC
//	\ucDataRate
//	0        Low data rate shall be used
//	1        High data rate shall be used
//	\ucNbSlots
//	0        16 slots
//	1        1 slot
//
int
ISO15693InventoryAFI(unsigned char ucSubCarrier, unsigned char ucDataRate,
            	  unsigned char ucAfi, unsigned char ucNbSlots,
            	  unsigned char *pucMask, unsigned char ucMaskLen)
{
    unsigned char pucResponse[10];
    unsigned int uiRxSize, i, slot;

    uiRxSize = 10;

    //
    // Prepare Inventory command.
    //
	//	b5 	AFI_flag
	//	0          AFI Field is not present
	//	1          AFI Field is present
    g_pucCmd[0] = (ucNbSlots << 5) | (0x1 << 4) | (0x1 << 2) | (ucDataRate << 1) | ucSubCarrier;
    g_pucCmd[1] = 0x01;		// Command Code = 0x01 ---> Inventory
    g_pucCmd[2] = ucAfi;
    g_pucCmd[3] = ucMaskLen;

    //
    // Transmit Inventory, receive response
    //
    TRF79x0Transceive(g_pucCmd, 4, 0, pucResponse, &uiRxSize, 0, TRF79X0_TRANSCEIVE_CRC);

	//
	// check if needing to scan slot
	//
    slot = 1;
	while((uiRxSize != 10) && (slot < 16))
	{
	    uiRxSize = sizeof(pucResponse);

	    TRF79x0IRQClearCauses(TRF79X0_WAIT_RXEND);
	    //
	    // the order of the two function must not reversed, because the TRF79x0ReceiveAgain() called
	    // TRF79x0Command(TRF79X0_RESET_FIFO_CMD); to reset receive FIFO
	    // the most important action TRF79x0ReceiveAgain() done is to set g_sRXState.uiMaxLength as uiRxSize
	    // in order to enable the TRF7970A interrupt to continue receive data
	    //
		ISO15693NextSlot();
		TRF79x0ReceiveAgain(pucResponse, &uiRxSize);
		slot++;
	};

    if(uiRxSize == 10 )
    {
        //
        // Valid answer to Inventory command received, return it as an char, uiRxSize NOT including two CRC bytes
        // the first 2 byte in pucResponse is Flags & DSFI, the last 8 bytes is UID
		//
        if(pucMask != NULL)
        {
        	for(i = 0; i < 8; i++)
        		pucMask[i] = pucResponse[2 + i];
        }
        return(uiRxSize);
    }
    else
    {
    	return(0);
    }
}


//
//	\ucSubCarrier,
//	0		A single sub-carrier frequency shall be used by the VICC
//	1		Two sub-carriers shall be used by the VICC
//	\ucDataRate
//	0        Low data rate shall be used
//	1        High data rate shall be used
//	\ucNbSlots
//	0        16 slots
//	1        1 slot
//
int
ISO15693Inventory(unsigned char ucSubCarrier, unsigned char ucDataRate,
            	  unsigned char ucNbSlots, unsigned char *pucMask, unsigned char ucMaskLen)
{
    unsigned char pucResponse[10];
    unsigned char uiRxSize, i, slot;

    uiRxSize = sizeof(pucResponse);

    //
    // Prepare Inventory command.
    //
	//	b5 	AFI_flag
	//	0          AFI Field is not present
	//	1          AFI Field is present
    g_pucCmd[0] = (ucNbSlots << 5) | (0x1 << 2) | (ucDataRate << 1) | ucSubCarrier;
    g_pucCmd[1] = 0x01;		// Command Code = 0x01 ---> Inventory
    g_pucCmd[3] = ucMaskLen;

    //
    // Transmit Inventory, receive response
    //
    TRF79x0Transceive(g_pucCmd, 3, 0, pucResponse, &uiRxSize, 0, TRF79X0_TRANSCEIVE_CRC);

	//
	// check if needing to scan slot
	//
    slot = 1;
	while((uiRxSize != 10) && (slot < 16))
	{
	    uiRxSize = sizeof(pucResponse);

	    TRF79x0IRQClearCauses(TRF79X0_WAIT_RXEND);
	    //
	    // the order of the two function must not reversed, because the TRF79x0ReceiveAgain() called
	    // TRF79x0Command(TRF79X0_RESET_FIFO_CMD); to reset receive FIFO
	    // the most important action TRF79x0ReceiveAgain() done is to set g_sRXState.uiMaxLength as uiRxSize
	    // in order to enable the TRF7970A interrupt to continue receive data
	    //
		ISO15693NextSlot();
		TRF79x0ReceiveAgain(pucResponse, &uiRxSize);
		slot++;
	};

    if(uiRxSize == 10 )
    {
        //
        // Valid answer to Inventory command received, return it as an char, uiRxSize NOT including two CRC bytes
        //
        if(pucMask != NULL)
        {
        	for(i = 0; i < uiRxSize; i++)
        		pucMask[i] = pucResponse[i];
        }
        return(uiRxSize);
    }
    else
    {
    	return(0);
    }
}

int
ISO15693Anticollision16Slots(unsigned char ucSubCarrier, unsigned char ucDataRate,
            	  	  	  	 unsigned char *pucMask, unsigned char ucMaskLen)
{
    unsigned char pucResponse[10], ucMaskNew[8];
    unsigned int uiRxSize, uiTxSize, i, slot = 0;
    unsigned int uiFlagCollision = 0, uiSlotCollision = 0;

    uiRxSize = sizeof(pucResponse);

    //
    // Prepare Inventory command.
    //
	//	b5 	AFI_flag
	//	0          AFI Field is not present
	//	1          AFI Field is present
    g_pucCmd[0] = (0x1 << 2) | (ucDataRate << 1) | ucSubCarrier;
    g_pucCmd[1] = 0x01;		// Command Code = 0x01 ---> Inventory
    g_pucCmd[2] = ucMaskLen;

	uiTxSize = 3 + (((ucMaskLen >> 2) + 1) >> 1);

    if(uiTxSize > 3)
    {
    	for(i = 0; i < (uiTxSize - 3); i++)
    		g_pucCmd[3 + i] = pucMask[i];
    }

    //
    // Transmit Inventory, receive response
    //
    TRF79x0Transceive(g_pucCmd, uiTxSize, 0, pucResponse, &uiRxSize, 0, TRF79X0_TRANSCEIVE_CRC);

	//
	// check if needing to scan slot
	//
	while(slot < 16)
	{
	    if(TRF79x0IsCollision() == 1)
	    {
	    	uiFlagCollision = 1;
	    	uiSlotCollision = slot -1;
	    }
	    else if(uiRxSize == 10)
	    {
	    	for(i = 0; i < 8; i++)
	    		g_sCard_15693[ucCardFound + 1].pucUID[i] = pucResponse[2 + i];
	    	g_sCard_15693[ucCardFound + 1].ucSlot = slot;
	    	ucCardFound++;
	    }

	    uiRxSize = sizeof(pucResponse);

	    TRF79x0IRQClearCauses(TRF79X0_WAIT_RXEND);

	    //
	    // the order of the two function must not reversed, because the TRF79x0ReceiveAgain() called
	    // TRF79x0Command(TRF79X0_RESET_FIFO_CMD); to reset receive FIFO
	    // the most important action TRF79x0ReceiveAgain() done is to set g_sRXState.uiMaxLength as uiRxSize
	    // in order to enable the TRF7970A interrupt to continue receive data
	    //
		ISO15693NextSlot();
		TRF79x0ReceiveAgain(pucResponse, &uiRxSize);
		slot++;
	};

	//
	// only do ones cascade
	// TODO: NEED refer to the msp430 version to make more cascade anticollision function
	//
	if(uiFlagCollision && (ucMaskLen < 4))
	{
		uiFlagCollision = 0;
		ucMaskNew[0] = uiSlotCollision;
		ISO15693Anticollision16Slots(0, 1, &ucMaskNew[0], ucMaskLen + 4);
	}

    if(ucCardFound)
    {
    	ucCardFound = 0;
        return(1);
    }
    else
    {
    	return(0);
    }
}

int
ISO15693StayQuiet(unsigned char  *pucUID)
{
	int i;

    //
    // Prepare Stay Quiet command.
    //
	//	b6 	Address_flag		the bit sequence start from b1 not b0!
	//	1          Request is addressed. UID field is included. It shall be	executed only
	//			   by the VICC whose UID matches the UID specified in the request.
    g_pucCmd[0] = (1 << 5) | (1 << 1);
    // Command Code = 0x02 ---> Stay Quiet
    g_pucCmd[1] = 0x02;
    for(i = 0; i < 8; i++)
    	g_pucCmd[2 + i] = pucUID[i];

    //
    // Transmit Stay Quiet command, receive response
    //
    TRF79x0Transceive(g_pucCmd, 10, 0, 0, 0, 0, TRF79X0_TRANSCEIVE_TX_CRC);
}

//*****************************************************************************
//
// ! Reads a single block data from the selected card.
// !
// ! \param uiBlock is the address of the block to read.
// ! \param pucBuf is the output buffer to store the raw block contents into.
// ! This buffer must be able to store at least 32 bytes.
// !
// ! This function reads a ISO15693 block and returns the full contents
// ! of the block with no interpretation of the bytes. The function will
// ! return the number of valid bytes stored in the \e pucBuf parameter.
// !
//
//*****************************************************************************
int
BlockReadSingleUID(unsigned char  *pucUID, unsigned int uiBlock, unsigned char *pucBuf)
{
    unsigned char pucCmd[11];
    unsigned int uiRxBytes;
    unsigned int uiRxBits;
    int i;

    //
    // Reading 32 bytes and 0 bits.
    //
    uiRxBytes = 32;
    uiRxBits = 0;

    //
    // Prepare Read Single Block command.
    //
	//	b6 	Address_flag		the bit sequence start from b1 not b0!
	//	1          Request is addressed. UID field is included. It shall be	executed only
	//			   by the VICC whose UID matches the UID specified in the request.
    // b7	Option_flag
    // 1			Meaning is defined by the command description
    pucCmd[0] = (1 << 6) |(1 << 5) | (1 << 1);
    // Command Code = 0x20 ---> Read Single Block
    pucCmd[1] = 0x20;
    for(i = 0; i < 8; i++)
    	pucCmd[2 + i] = pucUID[i];
    pucCmd[10] = uiBlock;

    //
    // Transmit Read Single Block, receive response
    //
    TRF79x0Transceive(pucCmd, sizeof(pucCmd), 0, pucBuf, &uiRxBytes, &uiRxBits, TRF79X0_TRANSCEIVE_CRC);
    if(uiRxBytes == 0)
    {
        return(0);
    }

    return(uiRxBytes);
}

int
BlockReadSingle(unsigned int uiBlock, unsigned char *pucBuf)
{
    unsigned char pucCmd[3];
    unsigned int uiRxBytes;
    unsigned int uiRxBits;
    int i;

    //
    // Reading 32 bytes and 0 bits.
    //
    uiRxBytes = 32;
    uiRxBits = 0;

    //
    // Prepare Read Single Block command.
    //
    // b7	Option_flag
    // 1			Meaning is defined by the command description
    pucCmd[0] = (1 << 6) | (1 << 1);
    // Command Code = 0x20 ---> Read Single Block
    pucCmd[1] = 0x20;
    pucCmd[2] = uiBlock;

    //
    // Transmit Read Single Block, receive response
    //
    TRF79x0Transceive(pucCmd, sizeof(pucCmd), 0, pucBuf, &uiRxBytes, &uiRxBits, TRF79X0_TRANSCEIVE_CRC);
    if(uiRxBytes == 0)
    {
        return(0);
    }

    return(uiRxBytes);
}
//*****************************************************************************
//
// ! Write a single block data to the selected card.
// !
// ! \param uiBlock is the address of the block to write.
// ! \param pucBuf is the input buffer to store the raw block contents into.
// ! This buffer must be able to store at least 32 bytes.
// !
// ! This function write a ISO15693 block
// !
//
//*****************************************************************************
int
BlockWriteSingleUID(unsigned char  *pucUID, unsigned int uiBlock, unsigned char ucValueLen, unsigned char *pucBuf)
{
    unsigned char pucCmd[43];
    unsigned char pucResponse[2];
    unsigned int uiRxBytes;
    int i;

    //
    // transmit  bytes as most
    //
    uiRxBytes = 2;

    //
    // Prepare Write Single Block command.
    //
	//	b6 	Address_flag		the bit sequence start from b1 not b0!
	//	1          Request is addressed. UID field is included. It shall be	executed only
	//			   by the VICC whose UID matches the UID specified in the request.

    // b7	Option_flag			must be set for Write & Lock command
    // 1			Meaning is defined by the command description
    pucCmd[0] = (1 << 6) | (1 << 5) | (1 << 1);
    // Command Code = 0x21 ---> Write Single Block
    pucCmd[1] = 0x21;
    for(i = 0; i < 8; i++)
    	pucCmd[2 + i] = pucUID[i];
    pucCmd[10] = uiBlock;
    for(i = 0; i < ucValueLen; i++)
    	pucCmd[11 + i] = pucBuf[i];

    //
    // Transmit Read Single Block, receive response
    //
    TRF79x0TransceiveISO15693(pucCmd, 11 + ucValueLen, 0, pucResponse, &uiRxBytes, 0, TRF79X0_TRANSCEIVE_CRC);
    if(uiRxBytes == 0)
    {
        return(0);
    }

    return(uiRxBytes);
}

int
BlockWriteSingle(unsigned int uiBlock, unsigned char ucValueLen, unsigned char *pucBuf)
{
    unsigned char pucCmd[7];
    unsigned char pucResponse[2];
    unsigned int uiRxBytes;
    int i;

    //
    // transmit  bytes as most
    //
    uiRxBytes = 2;

    //
    // Prepare Write Single Block command.
    //
	//	b6 	Address_flag		the bit sequence start from b1 not b0!
	//	1          Request is addressed. UID field is included. It shall be	executed only
	//			   by the VICC whose UID matches the UID specified in the request.

    // b7	Option_flag			must be set for Write & Lock command
    // 1			Meaning is defined by the command description
    pucCmd[0] = (1 << 6) | (1 << 1);
    // Command Code = 0x21 ---> Write Single Block
    pucCmd[1] = 0x21;
    pucCmd[2] = uiBlock;
    for(i = 0; i < ucValueLen; i++)
    	pucCmd[3 + i] = pucBuf[i];

    //
    // Transmit Read Single Block, receive response
    //
    TRF79x0TransceiveISO15693(pucCmd, 3 + ucValueLen, 0, pucResponse, &uiRxBytes, 0, TRF79X0_TRANSCEIVE_CRC);
    if(uiRxBytes == 0)
    {
        return(0);
    }

    return(uiRxBytes);
}

int
BlockLockSingleUID(unsigned char  *pucUID, unsigned int uiBlock, unsigned char *pucResponse)
{
    unsigned char pucCmd[11];
    unsigned int uiRxBytes;
    unsigned int uiRxBits;
    int i;

    //
    // Reading 32 bytes and 0 bits.
    //
    uiRxBytes = 2;
    uiRxBits = 0;

    //
    // Prepare Read Single Block command.
    //
	//	b6 	Address_flag		the bit sequence start from b1 not b0!
	//	1          Request is addressed. UID field is included. It shall be	executed only
	//			   by the VICC whose UID matches the UID specified in the request.
    // b7	Option_flag			must be set for Write & Lock command
    // 1			Meaning is defined by the command description
    pucCmd[0] = (1 << 6) | (1 << 5) | (1 << 1);
    // Command Code = 0x22 ---> Lock Single Block
    pucCmd[1] = 0x22;
    for(i = 0; i < 8; i++)
    	pucCmd[2 + i] = pucUID[i];
    pucCmd[10] = uiBlock;

    //
    // Transmit Read Single Block, receive response
    //
    TRF79x0TransceiveISO15693(pucCmd, sizeof(pucCmd), 0, pucResponse, &uiRxBytes, &uiRxBits, TRF79X0_TRANSCEIVE_CRC);
    if(uiRxBytes == 0)
    {
        return(0);
    }

    return(uiRxBytes);
}

int
BlockLockSingle(unsigned int uiBlock, unsigned char *pucResponse)
{
    unsigned char pucCmd[3];
    unsigned int uiRxBytes;
    unsigned int uiRxBits;
    int i;

    //
    // Reading 32 bytes and 0 bits.
    //
    uiRxBytes = 2;
    uiRxBits = 0;

    //
    // Prepare Read Single Block command.
    //

    pucCmd[0] = (1 << 6)  | (1 << 1);
    // Command Code = 0x22 ---> Lock Single Block
    pucCmd[1] = 0x22;
    pucCmd[2] = uiBlock;

    //
    // Transmit Read Single Block, receive response
    //
    TRF79x0TransceiveISO15693(pucCmd, sizeof(pucCmd), 0, pucResponse, &uiRxBytes, &uiRxBits, TRF79X0_TRANSCEIVE_CRC);
    if(uiRxBytes == 0)
    {
        return(0);
    }

    return(uiRxBytes);
}