This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Using TRF7970a SPI-Mode to read Mifare Classic 1k

Other Parts Discussed in Thread: TRF7970A

Hi,

we are currently developing a RFID/NFC-Reader using the TRF7970a connected over SPI.
What we want to do is to read a Mifare Classic 1k (Sector 0 for UID and Sector 4 for userdata secured with key a).

Is there any sample code we can use?
With the code from the TRF7970A EVM i have written a small c-code which reads out the UID, do a anticollision and select the card.

Best rgards,

Patrick

  • I have tried to to the AUTH-command and this is what i get:

    UID: 6E F2 39 EB
    Key: FF FF FF FF FF FF
    Sector: 4
    Auth: 60 (key a)

    Send: 60 04 D1 3D (Auth)
    Card: 27 FF F3 E0 (Nt)
    Send: 20 56 BB C2 17 F4 54 6D (Ar)
    Card: 79 96 60 52
    => for my mind the Auth is working (if i use another key the card sends no answer after transmit of Ar)

    Now i send the Read-cmd (30 04)
    Send: 7B FB DE 5F
    Card: no answer...

    maybe there is a possibility to generate sample traces i can compare

    Patrick

  • ok, now i feel like a spammer..

    i have found a reply in another post that says there is a sourcecode for mifare 1k and 4k using direct mode 0.
    (http://e2e.ti.com/support/low_power_rf/f/667/t/163384.aspx)
    Is it possible to get this code?

    Patrick

  • Patrick - 

    are you using the EVM? if so i can send you a binary and modified GUI to use - and also, if you are working with TI sales - please ask them to email me directly so i can give them instructions on how to get you access to the code via TI extranet. if not - send me email directly so i can collect some info from you. josh.wyatt@ti.com 

  • Josh,

    I want to read only the UID of Mifare cards over SPI communication. My company acquire the EVM and now I am developing a code (based on the code to MSP430) to PIC that is a improve on a product.

    I can read cards with 5 bytes UID, but with some delay sometimes. Maybe can you help with some improvements in my code.

    I'm having different width pulse on IRQ (maybe my code is slow to read the IRQ Status) and when a put the tag over the antenna I can check the modulation with a pickup coil on scope and the IRQ pin appears don't sync with then and the card wasn't read.

    Sometimes I can read 7 bytes and 10 bytes UID, sometimes the reader don't recognize them and sometimes the IRQ status go down forever.

    Can you send me something to solution this?

    Thanks a lot.


    Best regards,

    F.A.Stock

  • load this on your EVM and use the attached GUI and let me know if this is better for you. '

    7851.TI-GUI-Mifare.zip

  • here is the other file, too (for loading on the EVM)

    http://e2e.ti.com/cfs-file.ashx/__key/communityserver-discussions-components-files/667/1541.TRF7970A_5F00_MIFARE.7z

    \

  • Josh,

    we have successfully tested the new GUI and the next step we want to try is to adapt this function to our board.
    Can you give me the sources so that we can try it?

    Best regards,

    Patrick

  • I will ask the same. With the EVM I can read Mifare even before test your update.

    But in project I'm having problems like collision detect with only one tag over antenna and the read time very long. I need some improvements in my code, so what can you send me  to help with source code to read UID of Mifare cards?

    Thanks a lot.

    Best regards,

    F.A.Stock

  • To set Direct Mode (0 or 1) do I really need set or clean the bit ASK/OOK? I can do this only writing to register 0x09, can't I?

    Thanks.

    Best regards,

    F.A. Stock

  • Hello,

    has anyone experience with reading mifare 1k cards?
    Our project is at a standstill because we can´t read this cards, and this is important for us.
    I need sample code and maybe new ideas if we use furthermore the trf7970a, maybe we must change to another chip.

    Regards,

    Patrick

  • Hi Patrick,

    With the exception of UID, I don't think you can read data from MIFARE cards with TRF7xxx configured for SPI communications.

    To access MIFARE(ISO14443A) secure data it is necessary to talk directly with the card (on mode 0) but you need to communicate at RFID carrier 13.56MHz. That can only be done with TRF7xxx configured for parallel bus communications.

    On my EVM and in my PCB project I don't have any problems with parallel bus, reading and writing mifare cards.

    TI guys, correct me if I'm wrong...

    Regards.

  • To write on Mifare cards the Special Direct Mode can be used (through I/O_5, I/O_3, I/O_2 and the pins to SPI communication). You will use almost all pins of the parallel bus, only the I/O_1 (VDD) and I/O_0 (VSS) are not connected to uC and there are more compatibility, codes and users developing on parallel bus. I'm using the SPI communication, but I'm interested only in UID.

    Best regards,

  • dear,

    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
    695
    696
    697
    698
    699
    700
    701
    702
    703
    704
    705
    706
    707
    708
    709
    710
    711
    712
    713
    714
    715
    716
    717
    718
    719
    720
    721
    722
    723
    724
    725
    726
    727
    728
    729
    730
    731
    732
    733
    734
    735
    736
    737
    738
    739
    740
    741
    742
    743
    744
    745
    746
    747
    748
    749
    750
    751
    752
    753
    754
    755
    756
    757
    758
    759
    760
    761
    762
    763
    764
    765
    766
    767
    768
    769
    770
    771
    772
    773
    774
    775
    776
    777
    778
    779
    780
    void
    Iso14443aFindTag(void)
    {
        //u08_t j;
        //u08_t tag_found = 0;
        Trf7970TurnRfOn();
        
        Trf7970WriteIsoControl(0x88);
        
        // When a PICC is exposed to an un-modulated operating field
        // it shall be able to accept a quest within 5 ms.
        // PCDs should periodically present an un-modulated field of at least
        // 5,1 ms duration. (ISO14443-3)
        McuDelayMillisecond(20);
        Iso14443aAnticollision(0x00);                       // do a complete anti-collision sequence as described1
        //tag_found = iso14443aAnticollision(0x00);
        // in ISO14443-3 standard for type A
        Trf7970TurnRfOff();
        Trf7970ResetIrqStatus();
    }
    //===============================================================
    // NAME: void Iso14443aAnticollision(u08_t reqa)
    //
    // BRIEF: Is used to start the ISO14443A Anticollision Loop.
    //
    // INPUTS:
    //  Parameters:
    //      u08_t       reqa        REQA or WUPA
    //
    // OUTPUTS:
    //
    // PROCESS: [1] send REQA or WUPA command
    //          [2] receive ATQA
    //          [3] perform bit frame anticollison loop
    //
    // NOTE: Collisions returned as “(z)”.
    //       Timeouts returned as “()”.
    //
    // CHANGE:
    // DATE         WHO DETAIL
    // 23Nov2010    RP  Original Code
    //===============================================================
    void Iso14443aAnticollision(u08_t reqa)
    {
        u08_t nvb = NVB_INIT;
        u08_t ui8BlockNumber;
        int cx;
        //u08_t found = 0;
        //u08_t SW1 = 0;
        rxtx_state = 1;
        Iso14443_config(NO_RX_CRC);
        McuDelayMillisecond(2);
        Iso14443a_command(WUPA);            //send WUPA (0x52) to wake up all tags
        if(i_reg == 0xFF || i_reg == 0x02)
        {
            uid_pos = 0;
            Iso14443aLoop(0x01, nvb, &buf[40]);         // cascade level 1
            if(stand_alone_flag == 1)
            {
    /*
                RATS();                                     //for ISO14443-4A card selection
                    NDEFApplicationSelect();                    //Selects NDEF Application D2760000850101
                    CapabilityContainerSelect();                //Selects the Capability Container
                    ReadBinary(0, 15);                          //for contents of the capability container
                    SelectNDEF();                               //Selects NDEF Application
                    len = ReadBinary(0, 2);                     //reads NDEF Application for length of message
                    ReadBinary2(2, len);                            //for NDEF content
                    UartSendCString("NDEF Message: ");
                    UartPutChar('[');
                    for(j = 8; j < len+1; j++)
                    //for(j = 34; j < 47; j++)
                    {
                        UartPutChar(buf[j]);
                    }
                        UartPutChar(']');
                        UartPutCrlf();
    */
                cx = 0;
                for (ui8BlockNumber = 0; ui8BlockNumber < 0x4; ui8BlockNumber=ui8BlockNumber + 4)    //for dynamically reading all data blocks on tag
                {
                    NFC_TYPE2_READ_4_BLOCKS(ui8BlockNumber);                                        //reference T2T operation specification (command 0x30)
                    cx = cx + snprintf(g_tag_content+cx, sizeof(g_tag_content), "%s", g_block_content);
                }
    //          UART_PRINT(g_tag_content);
    /*
                {
                    NFC_TYPE2_WRITE_BLOCK();                                        //reference T2T operation specification (command 0x30)
                }
    */
                McuDelayMillisecond(50);
            }
        }
        else
        {
            LED_14443A_OFF;
        }
        Iso14443_config(NO_RX_CRC);
        return;
    }                                           // Iso14443aAnticollision
    //===============================================================
    // NAME: void Iso14443aLoop(u08_t select, u08_t nvb, u08_t
    //          *uid)
    //
    // BRIEF: Is used to run through the cascade levels.
    //
    // INPUTS:
    //  Parameters:
    //      u08_t       select      indicates cascade level
    //      u08_t       nvb         number of valid bits
    //      u08_t       *uid        known part of UID
    // 
    // OUTPUTS:
    //  Globals:
    //      u08_t       complete_uid[14]    stores UID
    //
    // PROCESS: (ISO14443-3)
    //
    // NOTE: Collisions returned as “(z)”.
    //       Timeouts returned as “()”.
    //
    // CHANGE:
    // DATE         WHO DETAIL
    // 23Nov2010    RP  Original Code
    //===============================================================
    void
    Iso14443aLoop(u08_t cascade_level, u08_t nvb, u08_t *uid)
    {
        u08_t   i = 0;
        //u08_t j = 0;
        //u08_t   len = 0;
        u08_t   nvbytes = 0, nvbits = 0, xbits = 0, found = 0;
        u08_t new_uid[4];
        u08_t select, new_uid1[4], coll_poss1, nvbits1;
        u08_t cascade_level1;
    #ifdef ENABLE_HOST
        u08_t rssi[2];
    #endif
        while (cascade_level < 4)
        {
            switch (cascade_level)
            {
            case 1:
                select = 0x93;
                break;
            case 2:
                select = 0x95;
                break;
            case 3:
                select = 0x97;
                break;
            default:
                break;
            }
            if((nvb & 0x0F) != 0x00)
            {
                nvbytes = (nvb >> 4) - 2;         // the number of known valid bytes
                xbits = nvb & 0x07;                 // the number of known valid bits
                // Both are used in the UID calculation
                for(i = 0; i < xbits; i++)
                {
                    nvbits = (nvbits << 1) + 1;
                }
            }
            rx_error_flag = 0;
            coll_poss = 0x21;
            rxtx_state = 1;                         // The response will be stored in buf[1] upwards
            Iso14443aSelectCommand(select, nvb, uid);
            while (coll_poss < 0x20) {
                if(i_reg == 0x00){
                    break;
                }
            };
            if (coll_poss == 0x20)
                i_reg = 0x02;                       // In case coll_poss=0x20 means didn't receive response
            for(i = 0; i < 5; i++)
            {
                complete_uid[i+uid_pos] = buf[i + 1];
            }
            if(rx_error_flag == 0x02)
            {
                i_reg = 0x02;
            }
            if(i_reg == 0x02 || i_reg == 0x00)      // collision or timeout
            {
                break;
            }
            if(i_reg == 0xff)                       // if data received
            {
                for (i=0; i<nvbytes; i++)
                    complete_uid[i+uid_pos] = *(uid + i);
                complete_uid[nvbytes+uid_pos] = (buf[1] &~nvbits) | (*(uid + nvbytes) & nvbits);
                for (i=1; i<(5-nvbytes); i++)
                    complete_uid[i+nvbytes+uid_pos] = buf[1+i];
                nvb = NVB_FULL;
                rxtx_state = 1;
                Iso14443aSelectCommand(select, nvb, &complete_uid[uid_pos]);
                McuDelayMillisecond(6);
                if (buf[1] & BIT2)              //uid not complete
                {
                    cascade_level++;
                    uid_pos += 5;
                    nvb = NVB_INIT;
                }
                else                // uid completed, print UID to uart.
                {
                    g_tag_found = ISO14443A;
                    #ifdef ENABLE_HOST
                    char * cptr;
                    cptr = (char *) g_uid;
                    Tag_Count++;
    //              UartPutCrlf();
    //              UartSendCString("ISO14443A UID:  ");
    //              UartPutChar('[');
                    switch (cascade_level)
                    {
                    case 1:
                        for (i=0; i<4; i++)
                        {
    //                      UartPutByte(complete_uid[i]);
                            cptr = cptr + sprintf(cptr, "%x", (complete_uid[i] >> 4) & 0x0F);
                            cptr = cptr + sprintf(cptr, "%x", (complete_uid[i]) & 0x0F);
                        }
                        break;
                    case 2:
                        for (i=1; i<4; i++)
                        {
    //                      UartPutByte(complete_uid[i]);
                            cptr = cptr + sprintf(cptr, "%x", (complete_uid[i] >> 4) & 0x0F);
                            cptr = cptr + sprintf(cptr, "%x", (complete_uid[i]) & 0x0F);
                        }
                        for (i=5; i<9; i++)
                        {
    //                      UartPutByte(complete_uid[i]);
                            cptr = cptr + sprintf(cptr, "%x", (complete_uid[i] >> 4) & 0x0F);
                            cptr = cptr + sprintf(cptr, "%x", (complete_uid[i]) & 0x0F);
                        }
                        break;
                    case 3:
                        for (i=1; i<4; i++)
                        {
    //                      UartPutByte(complete_uid[i]);
                            cptr = cptr + sprintf(cptr, "%x", (complete_uid[i] >> 4) & 0x0F);
                            cptr = cptr + sprintf(cptr, "%x", (complete_uid[i]) & 0x0F);
                        }
                        for (i=6; i<9; i++)
                        {
    //                      UartPutByte(complete_uid[i]);
                            cptr = cptr + sprintf(cptr, "%x", (complete_uid[i] >> 4) & 0x0F);
                            cptr = cptr + sprintf(cptr, "%x", (complete_uid[i]) & 0x0F);
                        }
                        for (i=10; i<14; i++)
                        {
    //                      UartPutByte(complete_uid[i]);
                            cptr = cptr + sprintf(cptr, "%x", (complete_uid[i] >> 4) & 0x0F);
                            cptr = cptr + sprintf(cptr, "%x", (complete_uid[i]) & 0x0F);
                        }
                        break;
                    default:
                        break;
                    }
    //              UartPutChar(',');
                    rssi[0] = RSSI_LEVELS;          // read RSSI levels
                    Trf7970ReadSingle(rssi, 1);
    //              UartPutByte(rssi[0]);
                    cptr = (char*) g_rssi;
                    cptr = cptr + sprintf(cptr, "%x", (rssi[0] >> 4) & 0x0F);
                    cptr = cptr + sprintf(cptr, "%x", rssi[0] & 0x0F);
    //              UartPutChar(']');
    //              UartPutCrlf();
    //              Report((char*) g_uid);
                    #endif
        //      build_message(my_message, complete_uid, 14, 0,    "ISO14443A UID:  ");
                    if(stand_alone_flag == 1)
                        found = 1;
                    i_reg = 0x01;                   // do nothing after
                    break;
                }
            }
    //      _nop();
        }
        if(i_reg == 0x00)                           // timer interrupt
        {  
    //      if(stand_alone_flag == 1)
    //      {
    //          #ifdef ENABLE_HOST
    //              UartPutChar('(');
    //              UartSendCString("No tag found/Error");
    //              UartPutChar(')');
    //              UartPutCrlf();
    //          #endif
    //      }
        }
        if(i_reg == 0x02)                                   // if collision occurred go into anti-collision
        {
            for (i=0; i<4; i++)
            {
                new_uid[i] = buf[i+1];
            }
            // a RX interrupt will happen after collision interrupt
            McuDelayMillisecond(5);
            if (coll_poss == 0x60 || coll_poss == 0x70)         // if all of 4 or 5 bytes of last level were in collision
            {
                cascade_level++;
                uid_pos += 5;
                coll_poss = 0x20;
            }
            else
            {
                // combine UCLn collected of last loop
                for (i=0; i<nvbytes; i++)
                    new_uid[i] = *(uid + i);
                new_uid[nvbytes] = (new_uid[nvbytes] &~ nvbits) | (*(uid + nvbytes) & nvbits);
            }
            // calculate new parameters
            nvbytes = (coll_poss >> 4) - 2;               // how many bytes were collected
            xbits  = coll_poss & 0x07;                  // how many bits were collected
            coll_poss++;
            nvbits = 0;
            for (i = 0; i < xbits; i++)
            {
                nvbits = (nvbits << 1) + 1;               // left shift to make a mask for last broken byte (create all bit 1 belong to how many bit in broken byte)
            }
            nvbits1 = (nvbits << 1) + 1;
            nvbits1 = nvbits1 - nvbits;             // bit_mask1 use to separate next bit after last broken bit
            new_uid[nvbytes] = new_uid[nvbytes] & nvbits;       // only keep collision bits
            //back up before loop
            for (i=0; i<=4; i++)
            {
                new_uid1[i] = new_uid[i];
            }
            coll_poss1 = coll_poss;
            uid_pos1 = uid_pos;
            cascade_level1 = cascade_level;
            Iso14443aLoop(cascade_level, coll_poss, new_uid);       // recursive call for anti-collision procedure
            McuDelayMillisecond(6);
            Iso14443a_halt();
            i_reg = 0x01;
            Iso14443a_command(WUPA);
            McuDelayMillisecond(6);
            uid_pos = uid_pos1;
            new_uid1[nvbytes] = new_uid1[nvbytes] + nvbits1;
            Iso14443aLoop(cascade_level1, coll_poss1, new_uid1);        // recursive call for anti-collision procedure
        }
        if(stand_alone_flag == 1)
        {
            if(found == 1)
            {  
                LED_14443A_ON;
            }
            else
            {  
                LED_14443A_OFF;
            }
        }
    }                                                       // Iso14443aLoop
    void ISO14443IRQWaitTimeout(u08_t txtimeout, u08_t rxtimeout)
    {
        i_reg = 0x01;
        while(i_reg != 0x00)
        {
    //      McuCounterSet();
    //      COUNT_VALUE = COUNT_1ms * txtimeout;
            A2CounterLoad(COUNT_1ms * txtimeout);
            irq_flag = 0x00;
            START_COUNTER;                      // start timer up mode
            while(irq_flag == 0x00)             // wait for interrupt
            {
            }
        }                                       // wait for end of TX
        RESET_COUNTER;
        i_reg = 0x01;
        while(i_reg == 0x01)        // wait for end of RX or timeout
        {
    //      McuCounterSet();
    //      COUNT_VALUE = COUNT_1ms * rxtimeout;
            A2CounterLoad(COUNT_1ms * rxtimeout);
            irq_flag = 0x00;
            START_COUNTER;                      // start timer up mode
            while(irq_flag == 0x00)
            {
            }                                   // wait for interrupt
        }
        RESET_COUNTER;
    }
    void Iso14443aSelectCommand(u08_t select, u08_t nvb, u08_t *uid)
    {
        u08_t length;
        Iso14443_config(RX_CRC);
        //buf[0] = RX_SPECIAL_SETTINGS;
        //buf[1] = 0x88;
        //Trf7970WriteSingle(&buf[0], 2);
        length = 5 + (nvb >> 4);
        if((nvb & 0x0F) != 0x00)
        {
            length++;
        }
        buf[0] = 0x8F;                          // prepare the SELECT command
        if(nvb == 0x70)                         // select command, otherwise anti-collision command
        {
            buf[1] = 0x91;                      // transmit with CRC
        }
        else
        {
            buf[1] = 0x90;
        }
        buf[2] = 0x3D;
        buf[3] = 0x00;
        buf[4] = nvb & 0xF0;                    // number of complete bytes
        if((nvb & 0x07) != 0x00)
        {
            buf[4] |= ((nvb & 0x07) << 1) + 1;    // number of broken bits, last bit is 1 means broken byte
        }
        buf[5] = select;                        // can be 0x93, 0x95 or 0x97
        buf[6] = nvb;                           // number of valid bits
        buf[7] = *uid;
        buf[8] = *(uid + 1);
        buf[9] = *(uid + 2);
        buf[10] = *(uid + 3);
        buf[11] = *(uid + 4);
        Trf7970RawWrite(&buf[0], length);
        ISO14443IRQWaitTimeout(5,50);
        McuDelayMillisecond(1);
    }
    void Iso14443a_halt()
    {
        Iso14443_config(NO_RX_CRC);
        buf[0] = 0x8F;                          // prepare the SELECT command
            buf[1] = 0x90;
        buf[2] = 0x3D;
        buf[3] = 0x00;
        buf[4] = 0x20;                  // number of complete bytes
        buf[5] = 0x50;                      //halt
        buf[6] = 0x00;                          // number of valid bits
        Trf7970RawWrite(&buf[0], 7);
        i_reg = 0x01;
        while(i_reg != 0x00)
        {
    //      McuCounterSet();
    //      COUNT_VALUE = COUNT_1ms * 2;        // 2ms for TIMEOUT
            A2CounterLoad(COUNT_1ms * 2);
            irq_flag = 0x00;
            START_COUNTER;                      // start timer up mode
            while(irq_flag == 0x00)             // wait for interrupt
            {
            }
            RESET_COUNTER;
        }                                       // wait for end of TX
    }
    void Iso14443a_command(u08_t command)
    {
        buf[0] = 0x8F;
        buf[1] = 0x90;
        buf[2] = 0x3D;
        buf[3] = 0x00;
        buf[4] = 0x0F;
        buf[5] = command;
        rxtx_state = 1;
        Trf7970RawWrite(&buf[0], 6);
        IRQ_CLR;                                // PORT2 interrupt flag clear
        IRQ_ON;
        ISO14443IRQWaitTimeout(5,50);
    }
    void Iso14443_config(u08_t crc)
    {
        Trf7970WriteIsoControl(crc);
        Trf7970ReadSingle(buf,1);
    }
    //===============================================================
    // NAME: void NFC_TYPE2_READ_4_BLOCKS(void)
    //
    // BRIEF: Is used to read NFC Type 2 Tag Platform data blocks four at a time in stand alone mode.
    //
    // INPUTS: StartBlock
    //
    // OUTPUTS:4 Blocks Data at time
    //
    // PROCESS: [1] inside Find Tags Loop
    //          [2] NFC Forum TT2 BLock Data read sequence
    //          [3] Return data to Host
    //          [4] Loop to read all data blocks
    //
    // NOTE:
    //
    // CHANGE:
    // DATE         WHO DETAIL
    // 01/18/2014   JDW Original Code
    //===============================================================
    void NFC_TYPE2_READ_4_BLOCKS(u08_t ui8StartBlock)
    {
        u08_t   i = 1, j = 0;
        u16_t   k;
        int cx = 0;
        //buf[0] = SPECIAL_FUNCTION;                //to enable 4-bit RX
        //buf[1] = 0x04;                            //setting bit 2 in register 0x10
        //Trf7970WriteSingle(&buf[0], 2);
        buf[0] = 0x8F;                          //Reset FIFO
        buf[1] = 0x91;                          //Send with CRC
        buf[2] = 0x3D;                          //Write Continuous
        buf[3] = 0x00;                          //upper and middle nibbles of # of bytes going to FIFO
        buf[4] = 0x20;                          //lower nibble # and broken # of bytes going to FIFO (in this case 2 complete bytes are going to be transmitted)
        buf[5] = 0x30;                          //Read Command
        buf[6] = ui8StartBlock;                 //Starting from Block # (called Bno)
        rxtx_state = 1;
        Trf7970RawWrite(&buf[0], 7);
        IRQ_CLR;                                // PORT2 interrupt flag clear
        IRQ_ON;
        //ISO14443IRQWaitTimeout(5,50);
        i_reg = 0x01;
        irq_flag = 0x00;
        START_COUNTER;                                      //  Starting Timeout
        while(irq_flag == 0x00)
        {
        }                                                   // wait for end of TX interrupt
        RESET_COUNTER;
    //  McuCounterSet();                                    // TimerA set
    //  COUNT_VALUE = COUNT_1ms * 20;
        A2CounterLoad(COUNT_1ms * 20);
        START_COUNTER;                                      // start timer up mode
        irq_flag = 0x00;
        while(irq_flag == 0x00)
        {
        }                                                   // wait for interrupt
        RESET_COUNTER;
        while(i_reg == 0x01)                                // wait for RX complete
        {
            k++;
            if(k == 0xFFF0)
            {
                i_reg = 0x00;
                rx_error_flag = 0x00;
            }
        }
        if( i_reg == 0xFF)
            {       // if received block data in buffer
            if(stand_alone_flag == 1)
            {
                //found = 1;
                #ifdef ENABLE_HOST
                cx = 0;
                for(j = 0; j < 4; j++){
                    cx = cx + snprintf(g_block_content+cx, sizeof(g_block_content)-cx, "%s", "NFC Type2 Block ");
                    cx = cx + snprintf(g_block_content+cx, sizeof(g_block_content)-cx, "%x", (ui8StartBlock >> 4) & 0x0F );
                    cx = cx + snprintf(g_block_content+cx, sizeof(g_block_content)-cx, "%x", ui8StartBlock & 0x0F );
                    cx = cx + snprintf(g_block_content+cx, sizeof(g_block_content)-cx, "%s", " Data:      [" );
                    ui8StartBlock ++;
                    for(i = 1+(j*4); i < 5+(j*4); i++)
                    {
                        cx = cx + snprintf(g_block_content+cx, sizeof(g_block_content)-cx, "%x", (buf[i] >> 4) & 0x0F );
                        cx = cx + snprintf(g_block_content+cx, sizeof(g_block_content)-cx, "%x", buf[i] & 0x0F );
                    }
                    cx = cx + snprintf(g_block_content+cx, sizeof(g_block_content)-cx, "%s", "]\n" );
                }
    //              //UartSendCString("NFC Type2 Block 04: ");
    //              UartPutChar('[');
    //              for(i = 5; i < 9; i++)
    //              {
    //                  UartPutByte(buf[i]);        // send block data to host
    //              }
    //              UartPutChar(']');
    //              UartPutCrlf();
    //              //UartSendCString("NFC Type2 Block 05: ");
    //              UartPutChar('[');
    //              for(i = 9; i < 13; i++)
    //              {
    //                  UartPutByte(buf[i]);        // send block data to host
    //              }
    //              UartPutChar(']');
    //              UartPutCrlf();
    //              //UartSendCString("NFC Type2 Block 06: ");
    //              UartPutChar('[');
    //              for(i = 13; i < 17; i++)
    //              {
    //                  UartPutByte(buf[i]);        // send block data to host
    //              }
    //              UartPutChar(']');
    //              UartPutCrlf();
                #endif
            }
        }
    }
    //===============================================================
    // NAME: void NFC_TYPE2_WRITE_BLOCK(void)
    //
    // BRIEF: Is used to read NFC Type 2 Tag Platform data blocks four at a time in stand alone mode.
    //
    // INPUTS: StartBlock
    //
    // OUTPUTS:4 Blocks Data at time
    //
    // PROCESS: [1] inside Find Tags Loop
    //          [2] NFC Forum TT2 BLock Data write sequence
    //          [3] Return data to Host
    //          [4] Loop to read all data blocks
    //
    // NOTE:
    //
    // CHANGE:
    // DATE         WHO DETAIL
    // 01/18/2014   JDW Original Code
    //===============================================================
    void NFC_TYPE2_WRITE_BLOCK(void) //test function on one block for now (01/27/2014, JDW)
    {
        u08_t   i = 1, j = 0;
        u16_t   k;
        buf[0] = ISO_CONTROL;
        buf[1] = 0x88;                          //to RX with CRC
        Trf7970WriteSingle(&buf[0], 2);
        buf[0] = SPECIAL_FUNCTION;          //to enable 4-bit RX
        buf[1] = 0x04;                          //setting bit 2 in register 0x10
        Trf7970WriteSingle(&buf[0], 2);
        buf[0] = 0x8F;                          //Reset FIFO
        buf[1] = 0x91;                          //Send with CRC
        buf[2] = 0x3D;                          //Write Continuous
        buf[3] = 0x00;                          //upper and middle nibbles of # of bytes going to FIFO
        buf[4] = 0x60;                          //lower nibble # and broken # of bytes going to FIFO (in this case 2 complete bytes are going to be transmitted)
        buf[5] = 0xA2;                          //Write Command
        buf[6] = 0x03;                          //Block # (called Bno) to write
        buf[7] = 0xE1;                          // for Type 2 NDEF formatting, using on Page 3: E1:10:12:00 (Mifare Ultralight C) or E1:11:12:10 (Mifare Ultralight) THIS IS OTP BLOCK!
        buf[8] = 0x10;                          //                                      Page 4: 01:03:A0:10
        buf[9] = 0x12;                          //                                      Page 5: 44:03:00:FE (blocks 3, 4 & 5 required to be considered NDEF formatted and empty)
        buf[10] = 0x00;                         //
        rxtx_state = 1;
        Trf7970RawWrite(&buf[0], 11);
        IRQ_CLR;                                // PORT2 interrupt flag clear
        IRQ_ON;
        //ISO14443IRQWaitTimeout(5,50);
        i_reg = 0x01;
        irq_flag = 0x00;
        START_COUNTER;                                      //  Starting Timeout
        while(irq_flag == 0x00)
        {
        }                                                   // wait for end of TX interrupt
        RESET_COUNTER;
    //  McuCounterSet();                                    // TimerA set
    //  COUNT_VALUE = COUNT_1ms * 20;
        A2CounterLoad(COUNT_1ms * 20);
        START_COUNTER;                                      // start timer up mode
        irq_flag = 0x00;
        while(irq_flag == 0x00)
        {
        }                                                   // wait for interrupt
        RESET_COUNTER;
        while(i_reg == 0x01)                                // wait for RX complete
        {
            k++;
            if(k == 0xFFF0)
            {
                i_reg = 0x00;
                rx_error_flag = 0x00;
            }
        }
        if( i_reg == 0xFF)
            {       // if received block data in buffer
            if(stand_alone_flag == 1)
            {
                //found = 1;
                #ifdef ENABLE_HOST
                for(j = 0; j < 4; j++){
                    UartSendCString("NFC Type2 Block ");
                    //UartPutByte(ui8StartBlock++);
                    UartSendCString(":  [");
                    for(i = 1+(j*4); i < 5+(j*4); i++)
                    {
                        UartPutByte(buf[i]);        // send block data to host
                    }
                    UartPutChar(']');
                    UartPutCrlf();
                }
                #endif
            }
        }
    }



    this is the code for writing into the block