Skip to content

Commit

Permalink
Integrate MiFare ultralight write page function from pull #18 on depr…
Browse files Browse the repository at this point in the history
…ecated NFCShield_I2C library.
  • Loading branch information
tdicola committed Mar 12, 2015
1 parent fe5972b commit 6318da7
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
54 changes: 54 additions & 0 deletions Adafruit_PN532.cpp
Expand Up @@ -1127,6 +1127,60 @@ uint8_t Adafruit_PN532::mifareultralight_ReadPage (uint8_t page, uint8_t * buffe
return 1;
}

/**************************************************************************/
/*!
Tries to write an entire 4-byte page at the specified block
address.
@param page The page number to write. (0..63 for most cases)
@param data The byte array that contains the data to write.
Should be exactly 4 bytes long.
@returns 1 if everything executed properly, 0 for an error
*/
/**************************************************************************/
uint8_t Adafruit_PN532::mifareultralight_WritePage (uint8_t page, uint8_t * data)
{

if (page >= 64)
{
#ifdef MIFAREDEBUG
Serial.println(F("Page value out of range"));
#endif
// Return Failed Signal
return 0;
}

#ifdef MIFAREDEBUG
Serial.print(F("Trying to write 4 byte page"));Serial.println(page);
#endif

/* Prepare the first command */
pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE;
pn532_packetbuffer[1] = 1; /* Card number */
pn532_packetbuffer[2] = MIFARE_ULTRALIGHT_CMD_WRITE; /* Mifare Ultralight Write command = 0xA2 */
pn532_packetbuffer[3] = page; /* Page Number (0..63 for most cases) */
memcpy (pn532_packetbuffer+4, data, 4); /* Data Payload */

/* Send the command */
if (! sendCommandCheckAck(pn532_packetbuffer, 8))
{
#ifdef MIFAREDEBUG
Serial.println(F("Failed to receive ACK for write command"));
#endif

// Return Failed Signal
return 0;
}
delay(10);

/* Read the response packet */
readdata(pn532_packetbuffer, 26);

// Return OK Signal
return 1;
}



/************** high level communication functions (handles both I2C and SPI) */
Expand Down
2 changes: 2 additions & 0 deletions Adafruit_PN532.h
Expand Up @@ -105,6 +105,7 @@
#define MIFARE_CMD_DECREMENT (0xC0)
#define MIFARE_CMD_INCREMENT (0xC1)
#define MIFARE_CMD_STORE (0xC2)
#define MIFARE_ULTRALIGHT_CMD_WRITE (0xA2)

// Prefixes for NDEF Records (to identify record type)
#define NDEF_URIPREFIX_NONE (0x00)
Expand Down Expand Up @@ -182,6 +183,7 @@ class Adafruit_PN532{

// Mifare Ultralight functions
uint8_t mifareultralight_ReadPage (uint8_t page, uint8_t * buffer);
uint8_t mifareultralight_WritePage (uint8_t page, uint8_t * data);

// Help functions to display formatted text
static void PrintHex(const byte * data, const uint32_t numBytes);
Expand Down

0 comments on commit 6318da7

Please sign in to comment.