0

I use the sample code to read from MIFARE Ultralight and write to MIFARE Classic, with the definition in .h file:

#define PN532_RESPONSE_INDATAEXCHANGE       (0x41)
#define MIFARE_CMD_WRITE                    (0xA0)

but when I run the code below:

/**************************************************************************/
uint8_t PN532::mifareultralight_WritePage (uint8_t page, uint8_t *buffer1)
{
    /* Prepare the first command */
    pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE;
    pn532_packetbuffer[1] = 1;                      /* Card number */
    pn532_packetbuffer[2] = MIFARE_CMD_WRITE;       /* Mifare Write command = 0xA0 */
    pn532_packetbuffer[3] = page;                   /* Page Number (0..63 in most cases) */
    memcpy (pn532_packetbuffer + 4, buffer1, 4);        /* Data Payload */

    /* Send the command */
    if (HAL(writeCommand)(pn532_packetbuffer, 8)) {
        Serial.println(F("Go here 1"));
        return 0;
    }
    Serial.println(F("Go here 2"));
    /* Read the response packet */
    return (0 < HAL(readResponse)(pn532_packetbuffer, sizeof(pn532_packetbuffer)));
}

The result would be like this:

Scan a NFC tag

write:  4A 1 0
read:   4B 1 1 0 44 0 7 4 C1 37 CA 2C 2C 80
ATQA: 0x 44SAK: 0x 0
Writing Mifare Ultralight
write:  40 1 30 4
read:   41 0 2 0 0 10 0 6 1 10 11 FF 0 0 0 0 0 0
write:  40 1 30 3
read:   41 0 0 0 0 0 2 0 0 10 0 6 1 10 11 FF 0 0
Tag capacity 0 bytes
write:  40 1 A0 5 1 2 3 4
Go here 2
Write failed 0

It does not go into "Go here 1", it means that no write command to the reader, do anyone know why?

Michael Roland
  • 39,663
  • 10
  • 99
  • 206
Bao Doan
  • 87
  • 1
  • 2
  • 13

1 Answers1

2

That write command you are using seems to be wrong. You are using the COMPATIBILITY_WRITE command code (0xA0) but you pass the parameters of a WRITE command.

I suggest you stick to the WRITE command:

+-----------+------+------+---------------------+
| WRAPPING  | CMD  | ADDR | DATA (1 PAGE)       |
+-----------+------+------+---------------------+
| 0x40 0x01 | 0xA2 | 0x05 | 0x01 0x02 0x03 0x04 |
+-----------+------+------+---------------------+

Or you could also use the COMPATIBILITY_WRITE command:

  1. You start with sending the command and the address:

    +-----------+------+------+
    | WRAPPING  | CMD  | ADDR |
    +-----------+------+------+
    | 0x40 0x01 | 0xA0 | 0x05 |
    +-----------+------+------+
    

    You should then receive the ACK/NAK status from the tag.

  2. Then you send the data in a second frame:

    +-----------+---------------------+------------------------------------------------------------+
    | WRAPPING  | DATA (1 PAGE)       | FILLER (3 EMPTY PAGES)
    +-----------+---------------------+------------------------------------------------------------+
    | 0x40 0x01 | 0x01 0x02 0x03 0x04 | 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
    +-----------+---------------------+------------------------------------------------------------+
    
Michael Roland
  • 39,663
  • 10
  • 99
  • 206
  • Yeay, you are right. I did it successfully. Thank you :) – Bao Doan Mar 19 '14 at 00:19
  • Hi, now I am doing with the authentication, but I do not know the command for the authentication, can you explain the steps and command for authentication? I have already written authentication keys into the NFC tag and lock the tag. `buffer1[] = {0x07, 0x06, 0x05, 0x04}; buffer2[] = {0x03, 0x02, 0x01, 0x00}; buffer3[] = {0x0F, 0x0E, 0x0D, 0x0C}; buffer4[] = {0x0B, 0x0A, 0x09, 0x08}; nfc->mifareultralight_WritePage(0x2C, buffer1); nfc->mifareultralight_WritePage(0x2D, buffer2); nfc->mifareultralight_WritePage(0x2E, buffer3); nfc->mifareultralight_WritePage(0x2F, buffer4);` – Bao Doan Mar 30 '14 at 06:30