diff --git a/PN532.cpp b/PN532.cpp old mode 100644 new mode 100755 index 0577827..379de57 --- a/PN532.cpp +++ b/PN532.cpp @@ -1,279 +1,420 @@ -#include -#include "PN532.h" - -//#define PN532DEBUG 1 - -byte pn532ack[] = {0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00}; -byte pn532response_firmwarevers[] = {0x00, 0xFF, 0x06, 0xFA, 0xD5, 0x03}; - -#define PN532_PACKBUFFSIZ 64 -byte pn532_packetbuffer[PN532_PACKBUFFSIZ]; - -PN532::PN532(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t ss) { - _clk = clk; - _miso = miso; - _mosi = mosi; - _ss = ss; - - pinMode(_ss, OUTPUT); - pinMode(_clk, OUTPUT); - pinMode(_mosi, OUTPUT); - pinMode(_miso, INPUT); -} - -void PN532::begin() { - digitalWrite(_ss, LOW); - - delay(1000); - - // not exactly sure why but we have to send a dummy command to get synced up - pn532_packetbuffer[0] = PN532_FIRMWAREVERSION; - sendCommandCheckAck(pn532_packetbuffer, 1); - - // ignore response! -} - -uint32_t PN532::getFirmwareVersion(void) { - uint32_t response; - - pn532_packetbuffer[0] = PN532_FIRMWAREVERSION; - - if (! sendCommandCheckAck(pn532_packetbuffer, 1)) - return 0; - - // read data packet - readspidata(pn532_packetbuffer, 12); - // check some basic stuff - if (0 != strncmp((char *)pn532_packetbuffer, (char *)pn532response_firmwarevers, 6)) { - return 0; - } - - response = pn532_packetbuffer[6]; - response <<= 8; - response |= pn532_packetbuffer[7]; - response <<= 8; - response |= pn532_packetbuffer[8]; - response <<= 8; - response |= pn532_packetbuffer[9]; - - return response; -} - - -// default timeout of one second -boolean PN532::sendCommandCheckAck(uint8_t *cmd, uint8_t cmdlen, uint16_t timeout) { - uint16_t timer = 0; - - // write the command - spiwritecommand(cmd, cmdlen); - - // Wait for chip to say its ready! - while (readspistatus() != PN532_SPI_READY) { - if (timeout != 0) { - timer+=10; - if (timer > timeout) - return false; - } - delay(10); - } - - // read acknowledgement - if (!spi_readack()) { - return false; - } - - timer = 0; - // Wait for chip to say its ready! - while (readspistatus() != PN532_SPI_READY) { - if (timeout != 0) { - timer+=10; - if (timer > timeout) - return false; - } - delay(10); - } - - return true; // ack'd command -} - -boolean PN532::SAMConfig(void) { - pn532_packetbuffer[0] = PN532_SAMCONFIGURATION; - pn532_packetbuffer[1] = 0x01; // normal mode; - pn532_packetbuffer[2] = 0x14; // timeout 50ms * 20 = 1 second - pn532_packetbuffer[3] = 0x01; // use IRQ pin! - - if (! sendCommandCheckAck(pn532_packetbuffer, 4)) - return false; - - // read data packet - readspidata(pn532_packetbuffer, 8); - - return (pn532_packetbuffer[5] == 0x15); -} - - -uint32_t PN532::readPassiveTargetID(uint8_t cardbaudrate) { - uint32_t cid; - - pn532_packetbuffer[0] = PN532_INLISTPASSIVETARGET; - pn532_packetbuffer[1] = 1; // max 1 cards at once (we can set this to 2 later) - pn532_packetbuffer[2] = cardbaudrate; - - if (! sendCommandCheckAck(pn532_packetbuffer, 3)) - return 0x0; // no cards read - - // read data packet - readspidata(pn532_packetbuffer, 20); - // check some basic stuff - - Serial.print("Found "); Serial.print(pn532_packetbuffer[7], DEC); Serial.println(" tags"); - if (pn532_packetbuffer[7] != 1) - return 0; - - uint16_t sens_res = pn532_packetbuffer[9]; - sens_res <<= 8; - sens_res |= pn532_packetbuffer[10]; - Serial.print("Sens Response: 0x"); Serial.println(sens_res, HEX); - Serial.print("Sel Response: 0x"); Serial.println(pn532_packetbuffer[11], HEX); - cid = 0; - for (uint8_t i=0; i< pn532_packetbuffer[12]; i++) { - cid <<= 8; - cid |= pn532_packetbuffer[13+i]; - Serial.print(" 0x"); Serial.print(pn532_packetbuffer[13+i], HEX); - } - Serial.println(); - - return cid; -} - - -/************** high level SPI */ - - -boolean PN532::spi_readack() { - uint8_t ackbuff[6]; - - readspidata(ackbuff, 6); - - return (0 == strncmp((char *)ackbuff, (char *)pn532ack, 6)); -} - -/************** mid level SPI */ - -uint8_t PN532::readspistatus(void) { - digitalWrite(_ss, LOW); - delay(2); - spiwrite(PN532_SPI_STATREAD); - // read byte - uint8_t x = spiread(); - - digitalWrite(_ss, HIGH); - return x; -} - -void PN532::readspidata(uint8_t* buff, uint8_t n) { - digitalWrite(_ss, LOW); - delay(2); - spiwrite(PN532_SPI_DATAREAD); - -#ifdef PN532DEBUG - Serial.print("Reading: "); -#endif - for (uint8_t i=0; i +#include "PN532.h" + +//#define PN532DEBUG 1 + +byte pn532ack[] = {0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00}; +byte pn532response_firmwarevers[] = {0x00, 0xFF, 0x06, 0xFA, 0xD5, 0x03}; + +#define PN532_PACKBUFFSIZ 64 +byte pn532_packetbuffer[PN532_PACKBUFFSIZ]; + +PN532::PN532(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t ss) { + _clk = clk; + _miso = miso; + _mosi = mosi; + _ss = ss; + + pinMode(_ss, OUTPUT); + pinMode(_clk, OUTPUT); + pinMode(_mosi, OUTPUT); + pinMode(_miso, INPUT); +} + +void PN532::begin() { + digitalWrite(_ss, LOW); + + delay(1000); + + // not exactly sure why but we have to send a dummy command to get synced up + pn532_packetbuffer[0] = PN532_FIRMWAREVERSION; + sendCommandCheckAck(pn532_packetbuffer, 1); + + // ignore response! +} + +uint32_t PN532::getFirmwareVersion(void) { + uint32_t response; + + pn532_packetbuffer[0] = PN532_FIRMWAREVERSION; + + if (! sendCommandCheckAck(pn532_packetbuffer, 1)) + return 0; + + // read data packet + readspidata(pn532_packetbuffer, 12); + // check some basic stuff + if (0 != strncmp((char *)pn532_packetbuffer, (char *)pn532response_firmwarevers, 6)) { + return 0; + } + + response = pn532_packetbuffer[6]; + response <<= 8; + response |= pn532_packetbuffer[7]; + response <<= 8; + response |= pn532_packetbuffer[8]; + response <<= 8; + response |= pn532_packetbuffer[9]; + + return response; +} + + +// default timeout of one second +boolean PN532::sendCommandCheckAck(uint8_t *cmd, uint8_t cmdlen, uint16_t timeout) { + uint16_t timer = 0; + + // write the command + spiwritecommand(cmd, cmdlen); + + // Wait for chip to say its ready! + while (readspistatus() != PN532_SPI_READY) { + if (timeout != 0) { + timer+=10; + if (timer > timeout) + return false; + } + delay(10); + } + + // read acknowledgement + if (!spi_readack()) { + return false; + } + + timer = 0; + // Wait for chip to say its ready! + while (readspistatus() != PN532_SPI_READY) { + if (timeout != 0) { + timer+=10; + if (timer > timeout) + return false; + } + delay(10); + } + + return true; // ack'd command +} + +boolean PN532::SAMConfig(void) { + pn532_packetbuffer[0] = PN532_SAMCONFIGURATION; + pn532_packetbuffer[1] = 0x01; // normal mode; + pn532_packetbuffer[2] = 0x14; // timeout 50ms * 20 = 1 second + pn532_packetbuffer[3] = 0x01; // use IRQ pin! + + if (! sendCommandCheckAck(pn532_packetbuffer, 4)) + return false; + + // read data packet + readspidata(pn532_packetbuffer, 8); + + return (pn532_packetbuffer[5] == 0x15); +} + +uint32_t PN532::authenticateBlock(uint8_t cardnumber /*1 or 2*/,uint32_t cid /*Card NUID*/, uint8_t blockaddress /*0 to 63*/,uint8_t authtype/*Either KEY_A or KEY_B */, uint8_t * keys) { + pn532_packetbuffer[0] = PN532_INDATAEXCHANGE; + pn532_packetbuffer[1] = cardnumber; // either card 1 or 2 (tested for card 1) + if(authtype == KEY_A) + { + pn532_packetbuffer[2] = PN532_AUTH_WITH_KEYA; + } + else + { + pn532_packetbuffer[2] = PN532_AUTH_WITH_KEYB; + } + pn532_packetbuffer[3] = blockaddress; //This address can be 0-63 for MIFARE 1K card + + pn532_packetbuffer[4] = keys[0]; + pn532_packetbuffer[5] = keys[1]; + pn532_packetbuffer[6] = keys[2]; + pn532_packetbuffer[7] = keys[3]; + pn532_packetbuffer[8] = keys[4]; + pn532_packetbuffer[9] = keys[5]; + + pn532_packetbuffer[10] = ((cid >> 24) & 0xFF); + pn532_packetbuffer[11] = ((cid >> 16) & 0xFF); + pn532_packetbuffer[12] = ((cid >> 8) & 0xFF); + pn532_packetbuffer[13] = ((cid >> 0) & 0xFF); + + if (! sendCommandCheckAck(pn532_packetbuffer, 14)) + return false; + + // read data packet + readspidata(pn532_packetbuffer, 2+6); + +#ifdef PN532DEBUG + for(int iter=0;iter<14;iter++) + { + Serial.print(pn532_packetbuffer[iter], HEX); + Serial.print(" "); + } + Serial.println(); + // check some basic stuff + + Serial.println("AUTH"); + for(uint8_t i=0;i<2+6;i++) + { + Serial.print(pn532_packetbuffer[i], HEX); Serial.println(" "); + } +#endif + + if((pn532_packetbuffer[6] == 0x41) && (pn532_packetbuffer[7] == 0x00)) + { + return true; + } + else + { + return false; + } + +} + +uint32_t PN532::readMemoryBlock(uint8_t cardnumber /*1 or 2*/,uint8_t blockaddress /*0 to 63*/, uint8_t * block) { + pn532_packetbuffer[0] = PN532_INDATAEXCHANGE; + pn532_packetbuffer[1] = cardnumber; // either card 1 or 2 (tested for card 1) + pn532_packetbuffer[2] = PN532_MIFARE_READ; + pn532_packetbuffer[3] = blockaddress; //This address can be 0-63 for MIFARE 1K card + + if (! sendCommandCheckAck(pn532_packetbuffer, 4)) + return false; + + // read data packet + readspidata(pn532_packetbuffer, 18+6); + // check some basic stuff +#ifdef PN532DEBUG + Serial.println("READ"); +#endif + for(uint8_t i=8;i<18+6;i++) + { + block[i-8] = pn532_packetbuffer[i]; +#ifdef PN532DEBUG + Serial.print(pn532_packetbuffer[i], HEX); Serial.print(" "); +#endif + } + if((pn532_packetbuffer[6] == 0x41) && (pn532_packetbuffer[7] == 0x00)) + { + return true; //read successful + } + else + { + return false; + } + +} + +//Do not write to Sector Trailer Block unless you know what you are doing. +uint32_t PN532::writeMemoryBlock(uint8_t cardnumber /*1 or 2*/,uint8_t blockaddress /*0 to 63*/, uint8_t * block) { + pn532_packetbuffer[0] = PN532_INDATAEXCHANGE; + pn532_packetbuffer[1] = cardnumber; // either card 1 or 2 (tested for card 1) + pn532_packetbuffer[2] = PN532_MIFARE_WRITE; + pn532_packetbuffer[3] = blockaddress; + + for(uint8_t byte=0; byte <16; byte++) + { + pn532_packetbuffer[4+byte] = block[byte]; + } + + if (! sendCommandCheckAck(pn532_packetbuffer, 20)) + return false; + // read data packet + readspidata(pn532_packetbuffer, 2+6); + +#ifdef PN532DEBUG + // check some basic stuff + Serial.println("WRITE"); + for(uint8_t i=0;i<2+6;i++) + { + Serial.print(pn532_packetbuffer[i], HEX); Serial.println(" "); + } +#endif + + if((pn532_packetbuffer[6] == 0x41) && (pn532_packetbuffer[7] == 0x00)) + { + return true; //write successful + } + else + { + return false; + } +} + +uint32_t PN532::readPassiveTargetID(uint8_t cardbaudrate) { + uint32_t cid; + + pn532_packetbuffer[0] = PN532_INLISTPASSIVETARGET; + pn532_packetbuffer[1] = 1; // max 1 cards at once (we can set this to 2 later) + pn532_packetbuffer[2] = cardbaudrate; + + if (! sendCommandCheckAck(pn532_packetbuffer, 3)) + return 0x0; // no cards read + + // read data packet + readspidata(pn532_packetbuffer, 20); + // check some basic stuff + + Serial.print("Found "); Serial.print(pn532_packetbuffer[7], DEC); Serial.println(" tags"); + if (pn532_packetbuffer[7] != 1) + return 0; + + uint16_t sens_res = pn532_packetbuffer[9]; + sens_res <<= 8; + sens_res |= pn532_packetbuffer[10]; + Serial.print("Sens Response: 0x"); Serial.println(sens_res, HEX); + Serial.print("Sel Response: 0x"); Serial.println(pn532_packetbuffer[11], HEX); + cid = 0; + for (uint8_t i=0; i< pn532_packetbuffer[12]; i++) { + cid <<= 8; + cid |= pn532_packetbuffer[13+i]; + Serial.print(" 0x"); Serial.print(pn532_packetbuffer[13+i], HEX); + } + +#ifdef PN532DEBUG + Serial.println("TargetID"); + for(uint8_t i=0;i<20;i++) + { + Serial.print(pn532_packetbuffer[i], HEX); Serial.println(" "); + } +#endif + return cid; +} + + +/************** high level SPI */ + + +boolean PN532::spi_readack() { + uint8_t ackbuff[6]; + + readspidata(ackbuff, 6); + + return (0 == strncmp((char *)ackbuff, (char *)pn532ack, 6)); +} + +/************** mid level SPI */ + +uint8_t PN532::readspistatus(void) { + digitalWrite(_ss, LOW); + delay(2); + spiwrite(PN532_SPI_STATREAD); + // read byte + uint8_t x = spiread(); + + + + digitalWrite(_ss, HIGH); + return x; +} + +void PN532::readspidata(uint8_t* buff, uint8_t n) { + digitalWrite(_ss, LOW); + delay(2); + spiwrite(PN532_SPI_DATAREAD); + +#ifdef PN532DEBUG + Serial.print("Reading: "); +#endif + for (uint8_t i=0; i - -#define PN532_PREAMBLE 0x00 -#define PN532_STARTCODE1 0x00 -#define PN532_STARTCODE2 0xFF -#define PN532_POSTAMBLE 0x00 - -#define PN532_HOSTTOPN532 0xD4 - -#define PN532_FIRMWAREVERSION 0x02 -#define PN532_GETGENERALSTATUS 0x04 -#define PN532_SAMCONFIGURATION 0x14 -#define PN532_INLISTPASSIVETARGET 0x4A -#define PN532_WAKEUP 0x55 - - -#define PN532_SPI_STATREAD 0x02 -#define PN532_SPI_DATAWRITE 0x01 -#define PN532_SPI_DATAREAD 0x03 -#define PN532_SPI_READY 0x01 - -#define PN532_MIFARE_ISO14443A 0x0 - -class PN532{ - public: - PN532(uint8_t cs, uint8_t clk, uint8_t mosi, uint8_t miso); - - void begin(void); - - boolean SAMConfig(void); - uint32_t getFirmwareVersion(void); - uint32_t readPassiveTargetID(uint8_t cardbaudrate); - - boolean sendCommandCheckAck(uint8_t *cmd, uint8_t cmdlen, uint16_t timeout = 1000); - - // - - private: - uint8_t _ss, _clk, _mosi, _miso; - - boolean spi_readack(); - uint8_t readspistatus(void); - void readspidata(uint8_t* buff, uint8_t n); - void spiwritecommand(uint8_t* cmd, uint8_t cmdlen); - void spiwrite(uint8_t c); - uint8_t spiread(void); -}; +// PN532 library by adafruit/ladyada +// MIT license + +// authenticateBlock, readMemoryBlock, writeMemoryBlock contributed +// by Seeed Technology Inc (www.seeedstudio.com) + + +#include + +#define PN532_PREAMBLE 0x00 +#define PN532_STARTCODE1 0x00 +#define PN532_STARTCODE2 0xFF +#define PN532_POSTAMBLE 0x00 + +#define PN532_HOSTTOPN532 0xD4 + +#define PN532_FIRMWAREVERSION 0x02 +#define PN532_GETGENERALSTATUS 0x04 +#define PN532_SAMCONFIGURATION 0x14 +#define PN532_INLISTPASSIVETARGET 0x4A +#define PN532_INDATAEXCHANGE 0x40 +#define PN532_MIFARE_READ 0x30 +#define PN532_MIFARE_WRITE 0xA0 + +#define PN532_AUTH_WITH_KEYA 0x60 +#define PN532_AUTH_WITH_KEYB 0x61 + + +#define PN532_WAKEUP 0x55 + +#define PN532_SPI_STATREAD 0x02 +#define PN532_SPI_DATAWRITE 0x01 +#define PN532_SPI_DATAREAD 0x03 +#define PN532_SPI_READY 0x01 + +#define PN532_MIFARE_ISO14443A 0x0 + +#define KEY_A 1 +#define KEY_B 2 + + +class PN532{ +public: + PN532(uint8_t cs, uint8_t clk, uint8_t mosi, uint8_t miso); + + void begin(void); + + boolean SAMConfig(void); + uint32_t getFirmwareVersion(void); + uint32_t readPassiveTargetID(uint8_t cardbaudrate); + uint32_t authenticateBlock( uint8_t cardnumber /*1 or 2*/, + uint32_t cid /*Card NUID*/, + uint8_t blockaddress /*0 to 63*/, + uint8_t authtype /*Either KEY_A or KEY_B */, + uint8_t * keys); + + uint32_t readMemoryBlock(uint8_t cardnumber /*1 or 2*/,uint8_t blockaddress /*0 to 63*/, uint8_t * block); + uint32_t writeMemoryBlock(uint8_t cardnumber /*1 or 2*/,uint8_t blockaddress /*0 to 63*/, uint8_t * block); + + boolean sendCommandCheckAck(uint8_t *cmd, uint8_t cmdlen, uint16_t timeout = 1000); + + // + +private: + uint8_t _ss, _clk, _mosi, _miso; + + boolean spi_readack(); + uint8_t readspistatus(void); + void readspidata(uint8_t* buff, uint8_t n); + void spiwritecommand(uint8_t* cmd, uint8_t cmdlen); + void spiwrite(uint8_t c); + uint8_t spiread(void); +}; diff --git a/examples/readAllMemoryBlocks/readAllMemoryBlocks.pde b/examples/readAllMemoryBlocks/readAllMemoryBlocks.pde new file mode 100755 index 0000000..70b530a --- /dev/null +++ b/examples/readAllMemoryBlocks/readAllMemoryBlocks.pde @@ -0,0 +1,99 @@ +//This example reads all MIFARE memory block from 0x00 to 0x63. It is tested with a new MIFARE 1K cards. Uses default keys for authentication. +//Contributed by Seeed Technology Inc (www.seeedstudio.com) + +#include + +#define SCK 13 +#define MOSI 11 +#define SS 10 +#define MISO 12 + +PN532 nfc(SCK, MISO, MOSI, SS); + +void setup(void) { + Serial.begin(9600); + Serial.println("Hello!"); + + nfc.begin(); + + uint32_t versiondata = nfc.getFirmwareVersion(); + if (! versiondata) { + Serial.print("Didn't find PN53x board"); + while (1); // halt + } + // Got ok data, print it out! + Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX); + Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC); + Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC); + Serial.print("Supports "); Serial.println(versiondata & 0xFF, HEX); + + // configure board to read RFID tags and cards + nfc.SAMConfig(); +} + + +void loop(void) { + uint32_t id; + // look for MiFare type cards + id = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A); + + if (id != 0) + { + Serial.print("Read card #"); + Serial.println(id); + Serial.println(); + + uint8_t keys[]= {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}; // default key of a fresh card + for(uint8_t blockn=0;blockn<64;blockn++) { + if(nfc.authenticateBlock(1, id ,blockn,KEY_A,keys)) //authenticate block blockn + { + //if authentication successful + uint8_t block[16]; + //read memory block blockn + if(nfc.readMemoryBlock(1,blockn,block)) + { + //if read operation is successful + for(uint8_t i=0;i<16;i++) + { + //print memory block + Serial.print(block[i],HEX); + if(block[i] <= 0xF) //Data arrangement / beautify + { + Serial.print(" "); + } + else + { + Serial.print(" "); + } + } + + Serial.print("| Block "); + if(blockn <= 9) //Data arrangement / beautify + { + Serial.print(" "); + } + Serial.print(blockn,DEC); + Serial.print(" | "); + + if(blockn == 0) + { + Serial.println("Manufacturer Block"); + } + else + { + if(((blockn + 1) % 4) == 0) + { + Serial.println("Sector Trailer"); + } + else + { + Serial.println("Data Block"); + } + } + } + } + } + } + delay(2000); +} + diff --git a/examples/readMifare/readMifare.pde b/examples/readMifareMemory/readMifareMemory.pde old mode 100644 new mode 100755 similarity index 50% rename from examples/readMifare/readMifare.pde rename to examples/readMifareMemory/readMifareMemory.pde index b52ecdb..250aac7 --- a/examples/readMifare/readMifare.pde +++ b/examples/readMifareMemory/readMifareMemory.pde @@ -1,9 +1,12 @@ +//This example reads a MIFARE memory block. It is tested with a new MIFARE 1K cards. Uses default keys. +//Contributed by Seeed Technology Inc (www.seeedstudio.com) + #include -#define SCK 2 -#define MOSI 3 -#define SS 4 -#define MISO 5 +#define SCK 13 +#define MOSI 11 +#define SS 10 +#define MISO 12 PN532 nfc(SCK, MISO, MOSI, SS); @@ -24,7 +27,7 @@ void setup(void) { Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC); Serial.print("Supports "); Serial.println(versiondata & 0xFF, HEX); - // configure board to read RFID tags + // configure board to read RFID tags and cards nfc.SAMConfig(); } @@ -34,9 +37,30 @@ void loop(void) { // look for MiFare type cards id = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A); - if (id != 0) { + if (id != 0) + { Serial.print("Read card #"); Serial.println(id); + + uint8_t keys[]= {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}; + if(nfc.authenticateBlock(1, id ,0x08,KEY_A,keys)) //authenticate block 0x08 + { + //if authentication successful + uint8_t block[16]; + //read memory block 0x08 + if(nfc.readMemoryBlock(1,0x08,block)) + { + //if read operation is successful + for(uint8_t i=0;i<16;i++) + { + //print memory block + Serial.print(block[i],HEX); + Serial.print(" "); + } + Serial.println(); + } + } } + + delay(500); } - \ No newline at end of file diff --git a/examples/readMifareTargetID/readMifareTargetID.pde b/examples/readMifareTargetID/readMifareTargetID.pde new file mode 100755 index 0000000..60505b3 --- /dev/null +++ b/examples/readMifareTargetID/readMifareTargetID.pde @@ -0,0 +1,42 @@ +#include + +#define SCK 13 +#define MOSI 11 +#define SS 10 +#define MISO 12 + +PN532 nfc(SCK, MISO, MOSI, SS); + +void setup(void) { + Serial.begin(9600); + Serial.println("Hello!"); + + nfc.begin(); + + uint32_t versiondata = nfc.getFirmwareVersion(); + if (! versiondata) { + Serial.print("Didn't find PN53x board"); + while (1); // halt + } + // Got ok data, print it out! + Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX); + Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC); + Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC); + Serial.print("Supports "); Serial.println(versiondata & 0xFF, HEX); + + // configure board to read RFID tags and cards + nfc.SAMConfig(); +} + + +void loop(void) { + uint32_t id; + // look for MiFare type cards + id = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A); + + if (id != 0) { + Serial.print("Read card #"); Serial.println(id); + } +} + + diff --git a/examples/writeMifareMemory/writeMifareMemory.pde b/examples/writeMifareMemory/writeMifareMemory.pde new file mode 100755 index 0000000..cbd9d0b --- /dev/null +++ b/examples/writeMifareMemory/writeMifareMemory.pde @@ -0,0 +1,87 @@ +// This example writes a MIFARE memory block 0x08. It is tested with a new MIFARE 1K cards. Uses default keys. +// Note: Memory block 0 is readonly and contains manufacturer data. Do not write to Sector Trailer block +// unless you know what you are doing. Otherwise, the MIFARE card may be unusable in the future. + +//Contributed by Seeed Technology Inc (www.seeedstudio.com) + +#include + +#define SCK 13 +#define MOSI 11 +#define SS 10 +#define MISO 12 + +PN532 nfc(SCK, MISO, MOSI, SS); + +uint8_t written=0; + +void setup(void) { + Serial.begin(9600); + Serial.println("Hello!"); + + nfc.begin(); + + uint32_t versiondata = nfc.getFirmwareVersion(); + if (! versiondata) { + Serial.print("Didn't find PN53x board"); + while (1); // halt + } + // Got ok data, print it out! + Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX); + Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC); + Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC); + Serial.print("Supports "); Serial.println(versiondata & 0xFF, HEX); + + // configure board to read RFID tags and cards + nfc.SAMConfig(); +} + + +void loop(void) { + uint32_t id; + // look for MiFare type cards + id = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A); + + if (id != 0) + { + Serial.print("Read card #"); Serial.println(id); + Serial.println(); + + uint8_t keys[]= {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}; + uint8_t writeBuffer[16]; + for(uint8_t ii=0;ii<16;ii++) + { + writeBuffer[ii]=ii; //Fill buffer with 0,1,2....F + } + if(nfc.authenticateBlock(1, id ,0x08,KEY_A,keys)) //authenticate block 0x08 + { + //if authentication successful + + if(written == 0) //Not written + { + written = nfc.writeMemoryBlock(1,0x08,writeBuffer); // Write writeBuffer[] to block 0x08 + if(written) + Serial.println("Write Successful"); + } + + + uint8_t block[16]; + //read memory block 0x08 + if(nfc.readMemoryBlock(1,0x08,block)) + { + Serial.println("Read block 0x08:"); + //if read operation is successful + for(uint8_t i=0;i<16;i++) + { + //print memory block + Serial.print(block[i],HEX); + Serial.print(" "); + } + Serial.println(); + } + } + } + + delay(500); +} +