Test example

Dependencies:   mbed

Fork of readMifare by Interactive Device Design

main.cpp

Committer:
mjovanov1
Date:
2016-03-17
Revision:
5:4b238fd5f347
Parent:
4:70acff42d8b4
Child:
6:9109b95c3e97

File content as of revision 5:4b238fd5f347:

/**************************************************************************/
/*!

    @file          main.cpp
    @author        Adafruit Industries
    @modified_by   Ben Zhang <benzh@eecs.berkeley.edu>
    @license       BSD (see license.txt)
    
    This example will wait for any ISO14443A card or tag, and
    depending on the size of the UID will attempt to read from it.
   
    If the card has a 4-byte UID it is probably a Mifare
    Classic card, and the following steps are taken:
   
    - Authenticate block 4 (the first block of Sector 1) using
      the default KEYA of 0XFF 0XFF 0XFF 0XFF 0XFF 0XFF
    - If authentication succeeds, we can then read any of the
      4 blocks in that sector (though only block 4 is read here)
     
    If the card has a 7-byte UID it is probably a Mifare
    Ultralight card, and the 4 byte pages can be read directly.
    Page 4 is read by default since this is the first 'general-
    purpose' page on the tags.


This is an example sketch for the Adafruit PN532 NFC/RFID breakout boards
This library works with the Adafruit NFC breakout 
  ----> https://www.adafruit.com/products/364
 
Check out the links above for our tutorials and wiring diagrams 
These chips use SPI to communicate, 4 required to interface

Adafruit invests time and resources providing this open source code, 
please support Adafruit and open-source hardware by purchasing 
products from Adafruit!

*/
/**************************************************************************/

#include "mbed.h"
#include "Adafruit_PN532.h"

#define MOSI p11
#define MISO p12
#define SCK  p13
#define SS   p20

Serial pc(USBTX, USBRX);
Adafruit_PN532 nfc(SCK, MISO, MOSI, SS);

void loop(void);
int sectorToBlock(int sectorIndex);

int main() {
  pc.printf("Hello!\r\n");  
  // By default, no color

  nfc.begin();

  uint32_t versiondata = nfc.getFirmwareVersion();
  if (! versiondata) {
    pc.printf("Didn't find PN53x board");
    while (1); // halt
  }
  // Got ok data, print it out!
  pc.printf("Found chip PN5%2X with Firmware ver. %d.%d\r\n",
            versiondata >> 24,
            (versiondata >> 16) & 0xFF,
            (versiondata >> 8) & 0xFF);
  
  // configure board to read RFID tags
  nfc.SAMConfig();
  
  pc.printf("Waiting for an ISO14443A Card ...\r\n");
  
  while(1) { loop(); }
}


void loop(void) {
  // Turn back to no color 

  uint8_t success;
  uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
  uint8_t uidLength;                        // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
  int sector = 32;
  
  // Wait for an ISO14443A type cards (Mifare, etc.).  When one is found
  // 'uid' will be populated with the UID, and uidLength will indicate
  // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
  success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
  
  if (success) {
    // Display some basic information about the card
    pc.printf("\r\n\r\nFound an ISO14443A card\r\n");
    pc.printf("  UID Length: %d bytes", uidLength);
    pc.printf("  UID Value: ");
    nfc.PrintHex(uid, uidLength);
    pc.printf("\r\n");
    
    if (uidLength == 4)
    {
      // We probably have a Mifare Classic card ... 
      pc.printf("Seems to be a Mifare Classic card (4 byte UID)\r\n");
      
      // Now we need to try to authenticate it for read/write access
      // Try with the factory default KeyA: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
      pc.printf("Trying to authenticate sector %d with default KEYA value\r\n", sector);
      uint8_t keya[6] = { 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5 };
      int blockNumber = sectorToBlock(sector);
      
      // Start with block 4 (the first block of sector 1) since sector 0
      // contains the manufacturer data and it's probably better just
      // to leave it alone unless you know what you're doing
      success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, blockNumber, 0, keya);
        
      if (success)
      { 
        // Try to read the contents of block 4
            int respSize = 16;
            uint8_t data[respSize];
            
            success = nfc.mifareclassic_ReadDataBlock(blockNumber, data);
            
            if (success)
            {
              // Data seems to have been read ... spit it out
              pc.printf("Reading Block %d:\r\n\t", 128);
              nfc.PrintHexChar(data, respSize);
              pc.printf("\r\n");
              
              
              
              // Wait a bit before reading the card again
              wait(1);
            }
            else
            {
              pc.printf("Ooops ... unable to read the requested block.  Try another key?\r\n");
            }
      }
      else
      {
        pc.printf("Ooops ... authentication failed: Try another key?\r\n");
      }
    }
    
    if (uidLength == 7)
    {
      // We probably have a Mifare Ultralight card ...
      pc.printf("Seems to be a Mifare Ultralight tag (7 byte UID)\r\n");
      
      // Try to read the first general-purpose user page (#4)
      pc.printf("Reading page 4\r\n");
      uint8_t data[32];
      success = nfc.mifareultralight_ReadPage (4, data);
      if (success)
      {
        // Data seems to have been read ... spit it out
        nfc.PrintHexChar(data, 4);
        pc.printf("\r\n");
        
        // Wait a bit before reading the card again
        wait(1);
      }
      else
      {
        pc.printf("Ooops ... unable to read the requested page!?\r\n");
      }
    }
  }
}
  
int sectorToBlock(int sectorIndex) {
    if (sectorIndex < 32) {
        return sectorIndex * 4;
    } else {
        return 32 * 4 + (sectorIndex - 32) * 16;
    }
}