Due to the impact of the coronavirus (COVID-19) epidemic, the office building strictly controls the entry and exit of staff. Everyone should wear a face mask and measure body temperature to enter.
During the COVID-19 period, Makerfans provide free face masks to employees, but some people don't always wear masks. That's why I made this project.
The staff must wear the NFC masks specially customized by Makerfabs, which cannot be accessed by outsiders. And we made a mask detector. If you wear an NFC mask, the door of the office building can be opened automatically. You can enter and exit freely, otherwise if you don't, you will not be able to enter.
Step 1: SuppliesHardware:
- Maduino Zero NFC ( get it from this link: https://www.makerfabs.com/maduino-zero-nfc.html )
- L298N Motor Driver Board ( get it from this link: https://www.makerfabs.com/l298n-motor-driver-board.html )
- NFC Sticker ( get it from this link: https://www.makerfabs.com/nfc-sticker-white-wet-inlay.html )
- Electromagnetic Lock ( get it from this link: https://www.makerfabs.com/electromagnetic-lock-tap-a.html )
Software:
- Arduino IDE
Download link: https://www.arduino.cc/en/Main/Software
Step 2: ConnectL298N module -- Maduino Zero NFC
- ENA -- D4 (GPIO4)
- IN1 -- D5 (GPIO5)
- IN2 -- D6 (GPIO6)
The electromagnetic lock is connected to L298N OUT2.
Step 3: Customized NFC MasksMakerfabs customized the NFC masks, nails sized NFC stickers attached, which supply free to employees.
Step 4: Install the NFC Mask DetectorThe Maduino Zero NFC module is fixed on the door, and the NFC antenna is attached to the side of the door to detect whether the staff is wearing a mask.
Step 5: TestStaff wearing masks near the door, the instrument detecting if he/she is wearing a mask, if yes, the door opens automatically. (Also a few seconds later, the door will automatically close.)
As my friend said: "NFC-enabled masks is a fun idea! It serves the purpose of being an effective reminder without being overly complex or privacy-invasive."
If you have other fun ideas, let me know!
We can also help with your customized PCBA boards based on your special request, pls contact us via service@makerfabs.com.
#include <Wire.h>
#include <SPI.h>
#include "Adafruit_PN532.h"
#define PN532_SS (9)
// If using the breakout or shield with I2C, define just the pins connected
// to the IRQ and reset lines. Use the values below (2, 3) for the shield!
#define PN532_IRQ (2)
#define PN532_RESET (11) // Not connected by default on the NFC Shield
Adafruit_PN532 nfc(PN532_SS);
//L298N
const int ENA = 6 ;
const int IN1 = 5 ;
const int IN2 = 4 ;
#if defined(ARDUINO_ARCH_SAMD)
// for Zero, output on USB Serial console, remove line below if using programming port to program the Zero!
// also change #define in Adafruit_PN532.cpp library file
#define Serial SerialUSB
#endif
void setup(void) {
pinMode(ENA, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
Serial.begin(115200);
SerialUSB.begin(115200);
SerialUSB.println("Hello, Maduino Zero NFC! Makerfabs");
nfc.begin();
uint32_t versiondata = nfc.getFirmwareVersion();
if (! versiondata) {
SerialUSB.print("Didn't find PN53x board");
while (1); // halt
}
// Got ok data, print it out!
SerialUSB.print("Found chip PN5"); SerialUSB.println((versiondata>>24) & 0xFF, HEX);
SerialUSB.print("Firmware ver. "); SerialUSB.print((versiondata>>16) & 0xFF, DEC);
SerialUSB.print('.'); SerialUSB.println((versiondata>>8) & 0xFF, DEC);
// Set the max number of retry attempts to read from a card
// This prevents us from waiting forever for a card, which is
// the default behaviour of the PN532.
nfc.setPassiveActivationRetries(0xFF);
// configure board to read RFID tags
nfc.SAMConfig();
SerialUSB.println("Waiting for an ISO14443A card");
}
void loop(void) {
boolean result_success=0;
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)
// 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)
result_success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength);
if (result_success) {
SerialUSB.println("Found a card!");
SerialUSB.print("UID Length: ");SerialUSB.print(uidLength, DEC);SerialUSB.println(" bytes");
SerialUSB.print("UID Value: ");
for (uint8_t i=0; i < uidLength; i++)
{
SerialUSB.print(" 0x");SerialUSB.print(uid[i], HEX);
}
SerialUSB.println("");
result_success=0;
//analogWrite(ENA, 255);
digitalWrite(ENA, HIGH);
digitalWrite(IN1, LOW); //Switch direction
digitalWrite(IN2, HIGH);
delay(2000);
// Wait 1 second before continuing
delay(1000);
}
else
{
// PN532 probably timed out waiting for a card
SerialUSB.println("Timed out waiting for a card");
}
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
}
/**************************************************************************/
/*!
@file Adafruit_PN532.cpp
@author Adafruit Industries
@license BSD (see license.txt)
Driver for NXP's PN532 NFC/13.56MHz RFID Transceiver
This is a library 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 or I2C to communicate.
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
@section HISTORY
v2.1 - Added NTAG2xx helper functions
v2.0 - Refactored to add I2C support from Adafruit_NFCShield_I2C library.
v1.4 - Added setPassiveActivationRetries()
v1.2 - Added writeGPIO()
- Added readGPIO()
v1.1 - Changed readPassiveTargetID() to handle multiple UID sizes
- Added the following helper functions for text display
static void PrintHex(const byte * data, const uint32_t numBytes)
static void PrintHexChar(const byte * pbtData, const uint32_t numBytes)
- Added the following Mifare Classic functions:
bool mifareclassic_IsFirstBlock (uint32_t uiBlock)
bool mifareclassic_IsTrailerBlock (uint32_t uiBlock)
uint8_t mifareclassic_AuthenticateBlock (uint8_t * uid, uint8_t uidLen, uint32_t blockNumber, uint8_t keyNumber, uint8_t * keyData)
uint8_t mifareclassic_ReadDataBlock (uint8_t blockNumber, uint8_t * data)
uint8_t mifareclassic_WriteDataBlock (uint8_t blockNumber, uint8_t * data)
- Added the following Mifare Ultalight functions:
uint8_t mifareultralight_ReadPage (uint8_t page, uint8_t * buffer)
*/
/**************************************************************************/
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <Wire.h>
#if defined(__AVR__) || defined(__i386__) || defined(ARDUINO_ARCH_SAMD) || defined(ESP8266) || defined(ARDUINO_ARCH_STM32)
#define WIRE Wire
#else // Arduino Due
#define WIRE Wire1
#endif
#include <SPI.h>
#include "Adafruit_PN532.h"
byte pn532ack[] = {0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00};
byte pn532response_firmwarevers[] = {0x00, 0xFF, 0x06, 0xFA, 0xD5, 0x03};
// Uncomment these lines to enable debug output for PN532(SPI) and/or MIFARE related code
// #define PN532DEBUG
// #define MIFAREDEBUG
// If using Native Port on Arduino Zero or Due define as SerialUSB
#define PN532DEBUGPRINT Serial
//#define PN532DEBUGPRINT SerialUSB
// Hardware SPI-specific configuration:
#ifdef SPI_HAS_TRANSACTION
#define PN532_SPI_SETTING SPISettings(1000000, LSBFIRST, SPI_MODE0)
#else
#define PN532_SPI_CLOCKDIV SPI_CLOCK_DIV16
#endif
#define PN532_PACKBUFFSIZ 64
byte pn532_packetbuffer[PN532_PACKBUFFSIZ];
#ifndef _BV
#define _BV(bit) (1<<(bit))
#endif
/**************************************************************************/
/*!
@brief Sends a single byte via I2C
@param x The byte to send
*/
/**************************************************************************/
static inline void i2c_send(uint8_t x)
{
#if ARDUINO >= 100
WIRE.write((uint8_t)x);
#else
WIRE.send(x);
#endif
}
/**************************************************************************/
/*!
@brief Reads a single byte via I2C
*/
/**************************************************************************/
static inline uint8_t i2c_recv(void)
{
#if ARDUINO >= 100
return WIRE.read();
#else
return WIRE.receive();
#endif
}
/**************************************************************************/
/*!
@brief Instantiates a new PN532 class using software SPI.
@param clk SPI clock pin (SCK)
@param miso SPI MISO pin
@param mosi SPI MOSI pin
@param ss SPI chip select pin (CS/SSEL)
*/
/**************************************************************************/
Adafruit_PN532::Adafruit_PN532(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t ss):
_clk(clk),
_miso(miso),
_mosi(mosi),
_ss(ss),
_irq(0),
_reset(0),
_usingSPI(true),
_hardwareSPI(false)
{
pinMode(_ss, OUTPUT);
digitalWrite(_ss, HIGH);
pinMode(_clk, OUTPUT);
pinMode(_mosi, OUTPUT);
pinMode(_miso, INPUT);
}
/**************************************************************************/
/*!
@brief Instantiates a new PN532 class using I2C.
@param irq Location of the IRQ pin
@param reset Location of the RSTPD_N pin
*/
/**************************************************************************/
Adafruit_PN532::Adafruit_PN532(uint8_t irq, uint8_t reset):
_clk(0),
_miso(0),
_mosi(0),
_ss(0),
_irq(irq),
_reset(reset),
_usingSPI(false),
_hardwareSPI(false)
{
pinMode(_irq, INPUT);
pinMode(_reset, OUTPUT);
}
/**************************************************************************/
/*!
@brief Instantiates a new PN532 class using hardware SPI.
@param ss SPI chip select pin (CS/SSEL)
*/
/**************************************************************************/
Adafruit_PN532::Adafruit_PN532(uint8_t ss):
_clk(0),
_miso(0),
_mosi(0),
_ss(ss),
_irq(0),
_reset(0),
_usingSPI(true),
_hardwareSPI(true)
{
pinMode(_ss, OUTPUT);
digitalWrite(_ss, HIGH);
}
/**************************************************************************/
/*!
@brief Setups the HW
*/
/**************************************************************************/
void Adafruit_PN532::begin() {
if (_usingSPI) {
// SPI initialization
if (_hardwareSPI) {
SPI.begin();
#ifdef SPI_HAS_TRANSACTION
SPI.beginTransaction(PN532_SPI_SETTING);
#else
SPI.setDataMode(SPI_MODE0);
SPI.setBitOrder(LSBFIRST);
SPI.setClockDivider(PN532_SPI_CLOCKDIV);
#endif
}
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_COMMAND_GETFIRMWAREVERSION;
sendCommandCheckAck(pn532_packetbuffer, 1);
// ignore response!
digitalWrite(_ss, HIGH);
#ifdef SPI_HAS_TRANSACTION
if (_hardwareSPI) SPI.endTransaction();
#endif
}
else {
// I2C initialization.
WIRE.begin();
// Reset the PN532
digitalWrite(_reset, HIGH);
digitalWrite(_reset, LOW);
delay(400);
digitalWrite(_reset, HIGH);
delay(10); // Small delay required before taking other actions after reset.
// See timing diagram on page 209 of the datasheet, section 12.23.
}
}
/**************************************************************************/
/*!
@brief Prints a hexadecimal value in plain characters
@param data Pointer to the byte data
@param numBytes Data length in bytes
*/
/**************************************************************************/
void Adafruit_PN532::PrintHex(const byte * data, const uint32_t numBytes)
{
uint32_t szPos;
for (szPos=0; szPos < numBytes; szPos++)
{
PN532DEBUGPRINT.print(F("0x"));
// Append leading 0 for small values
if (data[szPos] <= 0xF)
PN532DEBUGPRINT.print(F("0"));
PN532DEBUGPRINT.print(data[szPos]&0xff, HEX);
if ((numBytes > 1) && (szPos != numBytes - 1))
{
PN532DEBUGPRINT.print(F(" "));
}
}
PN532DEBUGPRINT.println();
}
/**************************************************************************/
/*!
@brief Prints a hexadecimal value in plain characters, along with
the char equivalents in the following format
00 00 00 00 00 00 ......
@param data Pointer to the byte data
@param numBytes Data length in bytes
*/
/**************************************************************************/
void Adafruit_PN532::PrintHexChar(const byte * data, const uint32_t numBytes)
{
uint32_t szPos;
for (szPos=0; szPos < numBytes; szPos++)
{
// Append leading 0 for small values
if (data[szPos] <= 0xF)
PN532DEBUGPRINT.print(F("0"));
PN532DEBUGPRINT.print(data[szPos], HEX);
if ((numBytes > 1) && (szPos != numBytes - 1))
{
PN532DEBUGPRINT.print(F(" "));
}
}
PN532DEBUGPRINT.print(F(" "));
for (szPos=0; szPos < numBytes; szPos++)
{
if (data[szPos] <= 0x1F)
PN532DEBUGPRINT.print(F("."));
else
PN532DEBUGPRINT.print((char)data[szPos]);
}
PN532DEBUGPRINT.println();
}
/**************************************************************************/
/*!
@brief Checks the firmware version of the PN5xx chip
@returns The chip's firmware version and ID
*/
/**************************************************************************/
uint32_t Adafruit_PN532::getFirmwareVersion(void) {
uint32_t response;
pn532_packetbuffer[0] = PN532_COMMAND_GETFIRMWAREVERSION;
if (! sendCommandCheckAck(pn532_packetbuffer, 1)) {
return 0;
}
// read data packet
readdata(pn532_packetbuffer, 12);
// check some basic stuff
if (0 != strncmp((char *)pn532_packetbuffer, (char *)pn532response_firmwarevers, 6)) {
#ifdef PN532DEBUG
PN532DEBUGPRINT.println(F("Firmware doesn't match!"));
#endif
return 0;
}
int offset = _usingSPI ? 6 : 7; // Skip a response byte when using I2C to ignore extra data.
response = pn532_packetbuffer[offset++];
response <<= 8;
response |= pn532_packetbuffer[offset++];
response <<= 8;
response |= pn532_packetbuffer[offset++];
response <<= 8;
response |= pn532_packetbuffer[offset++];
return response;
}
/**************************************************************************/
/*!
@brief Sends a command and waits a specified period for the ACK
@param cmd Pointer to the command buffer
@param cmdlen The size of the command in bytes
@param timeout timeout before giving up
@returns 1 if everything is OK, 0 if timeout occured before an
ACK was recieved
*/
/**************************************************************************/
// default timeout of one second
bool Adafruit_PN532::sendCommandCheckAck(uint8_t *cmd, uint8_t cmdlen, uint16_t timeout) {
uint16_t timer = 0;
// write the command
writecommand(cmd, cmdlen);
// Wait for chip to say its ready!
if (!waitready(timeout)) {
return false;
}
#ifdef PN532DEBUG
if (!_usingSPI) {
PN532DEBUGPRINT.println(F("IRQ received"));
}
#endif
// read acknowledgement
if (!readack()) {
#ifdef PN532DEBUG
PN532DEBUGPRINT.println(F("No ACK frame received!"));
#endif
return false;
}
// For SPI only wait for the chip to be ready again.
// This is unnecessary with I2C.
if (_usingSPI) {
if (!waitready(timeout)) {
return false;
}
}
return true; // ack'd command
}
/**************************************************************************/
/*!
Writes an 8-bit value that sets the state of the PN532's GPIO pins
@warning This function is provided exclusively for board testing and
is dangerous since it will throw an error if any pin other
than the ones marked "Can be used as GPIO" are modified! All
pins that can not be used as GPIO should ALWAYS be left high
(value = 1) or the system will become unstable and a HW reset
will be required to recover the PN532.
pinState[0] = P30 Can be used as GPIO
pinState[1] = P31 Can be used as GPIO
pinState[2] = P32 *** RESERVED (Must be 1!) ***
pinState[3] = P33 Can be used as GPIO
pinState[4] = P34 *** RESERVED (Must be 1!) ***
pinState[5] = P35 Can be used as GPIO
@returns 1 if everything executed properly, 0 for an error
*/
/**************************************************************************/
bool Adafruit_PN532::writeGPIO(uint8_t pinstate) {
uint8_t errorbit;
// Make sure pinstate does not try to toggle P32 or P34
pinstate |= (1 << PN532_GPIO_P32) | (1 << PN532_GPIO_P34);
// Fill command buffer
pn532_packetbuffer[0] = PN532_COMMAND_WRITEGPIO;
pn532_packetbuffer[1] = PN532_GPIO_VALIDATIONBIT | pinstate; // P3 Pins
pn532_packetbuffer[2] = 0x00; // P7 GPIO Pins (not used ... taken by SPI)
#ifdef PN532DEBUG
PN532DEBUGPRINT.print(F("Writing P3 GPIO: ")); PN532DEBUGPRINT.println(pn532_packetbuffer[1], HEX);
#endif
// Send the WRITEGPIO command (0x0E)
if (! sendCommandCheckAck(pn532_packetbuffer, 3))
return 0x0;
// Read response packet (00 FF PLEN PLENCHECKSUM D5 CMD+1(0x0F) DATACHECKSUM 00)
readdata(pn532_packetbuffer, 8);
#ifdef PN532DEBUG
PN532DEBUGPRINT.print(F("Received: "));
PrintHex(pn532_packetbuffer, 8);
PN532DEBUGPRINT.println();
#endif
int offset = _usingSPI ? 5 : 6;
return (pn532_packetbuffer[offset] == 0x0F);
}
/**************************************************************************/
/*!
Reads the state of the PN532's GPIO pins
@returns An 8-bit value containing the pin state where:
pinState[0] = P30
pinState[1] = P31
pinState[2] = P32
pinState[3] = P33
pinState[4] = P34
pinState[5] = P35
*/
/**************************************************************************/
uint8_t Adafruit_PN532::readGPIO(void) {
pn532_packetbuffer[0] = PN532_COMMAND_READGPIO;
// Send the READGPIO command (0x0C)
if (! sendCommandCheckAck(pn532_packetbuffer, 1))
return 0x0;
// Read response packet (00 FF PLEN PLENCHECKSUM D5 CMD+1(0x0D) P3 P7 IO1 DATACHECKSUM 00)
readdata(pn532_packetbuffer, 11);
/* READGPIO response should be in the following format:
byte Description
------------- ------------------------------------------
b0..5 Frame header and preamble (with I2C there is an extra 0x00)
b6 P3 GPIO Pins
b7 P7 GPIO Pins (not used ... taken by SPI)
b8 Interface Mode Pins (not used ... bus select pins)
b9..10 checksum */
int p3offset = _usingSPI ? 6 : 7;
#ifdef PN532DEBUG
PN532DEBUGPRINT.print(F("Received: "));
PrintHex(pn532_packetbuffer, 11);
PN532DEBUGPRINT.println();
PN532DEBUGPRINT.print(F("P3 GPIO: 0x")); PN532DEBUGPRINT.println(pn532_packetbuffer[p3offset], HEX);
PN532DEBUGPRINT.print(F("P7 GPIO: 0x")); PN532DEBUGPRINT.println(pn532_packetbuffer[p3offset+1], HEX);
PN532DEBUGPRINT.print(F("IO GPIO: 0x")); PN532DEBUGPRINT.println(pn532_packetbuffer[p3offset+2], HEX);
// Note: You can use the IO GPIO value to detect the serial bus being used
switch(pn532_packetbuffer[p3offset+2])
{
case 0x00: // Using UART
PN532DEBUGPRINT.println(F("Using UART (IO = 0x00)"));
break;
case 0x01: // Using I2C
PN532DEBUGPRINT.println(F("Using I2C (IO = 0x01)"));
break;
case 0x02: // Using SPI
PN532DEBUGPRINT.println(F("Using SPI (IO = 0x02)"));
break;
}
#endif
return pn532_packetbuffer[p3offset];
}
/**************************************************************************/
/*!
@brief Configures the SAM (Secure Access Module)
*/
/**************************************************************************/
bool Adafruit_PN532::SAMConfig(void) {
pn532_packetbuffer[0] = PN532_COMMAND_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
readdata(pn532_packetbuffer, 8);
int offset = _usingSPI ? 5 : 6;
return (pn532_packetbuffer[offset] == 0x15);
}
/**************************************************************************/
/*!
Sets the MxRtyPassiveActivation byte of the RFConfiguration register
@param maxRetries 0xFF to wait forever, 0x00..0xFE to timeout
after mxRetries
@returns 1 if everything executed properly, 0 for an error
*/
/**************************************************************************/
bool Adafruit_PN532::setPassiveActivationRetries(uint8_t maxRetries) {
pn532_packetbuffer[0] = PN532_COMMAND_RFCONFIGURATION;
pn532_packetbuffer[1] = 5; // Config item 5 (MaxRetries)
pn532_packetbuffer[2] = 0xFF; // MxRtyATR (default = 0xFF)
pn532_packetbuffer[3] = 0x01; // MxRtyPSL (default = 0x01)
pn532_packetbuffer[4] = maxRetries;
#ifdef MIFAREDEBUG
PN532DEBUGPRINT.print(F("Setting MxRtyPassiveActivation to ")); PN532DEBUGPRINT.print(maxRetries, DEC); PN532DEBUGPRINT.println(F(" "));
#endif
if (! sendCommandCheckAck(pn532_packetbuffer, 5))
return 0x0; // no ACK
return 1;
}
/***** ISO14443A Commands ******/
/**************************************************************************/
/*!
Waits for an ISO14443A target to enter the field
@param cardBaudRate Baud rate of the card
@param uid Pointer to the array that will be populated
with the card's UID (up to 7 bytes)
@param uidLength Pointer to the variable that will hold the
length of the card's UID.
@returns 1 if everything executed properly, 0 for an error
*/
/**************************************************************************/
bool Adafruit_PN532::readPassiveTargetID(uint8_t cardbaudrate, uint8_t * uid, uint8_t * uidLength, uint16_t timeout) {
pn532_packetbuffer[0] = PN532_COMMAND_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, timeout))
{
#ifdef PN532DEBUG
PN532DEBUGPRINT.println(F("No card(s) read"));
#endif
return 0x0; // no cards read
}
// wait for a card to enter the field (only possible with I2C)
if (!_usingSPI) {
#ifdef PN532DEBUG
PN532DEBUGPRINT.println(F("Waiting for IRQ (indicates card presence)"));
#endif
if (!waitready(timeout)) {
#ifdef PN532DEBUG
PN532DEBUGPRINT.println(F("IRQ Timeout"));
#endif
return 0x0;
}
}
// read data packet
readdata(pn532_packetbuffer, 20);
// check some basic stuff
/* ISO14443A card response should be in the following format:
byte Description
------------- ------------------------------------------
b0..6 Frame header and preamble
b7 Tags Found
b8 Tag Number (only one used in this example)
b9..10 SENS_RES
b11 SEL_RES
b12 NFCID Length
b13..NFCIDLen NFCID */
#ifdef MIFAREDEBUG
PN532DEBUGPRINT.print(F("Found ")); PN532DEBUGPRINT.print(pn532_packetbuffer[7], DEC); PN532DEBUGPRINT.println(F(" tags"));
#endif
if (pn532_packetbuffer[7] != 1)
return 0;
uint16_t sens_res = pn532_packetbuffer[9];
sens_res <<= 8;
sens_res |= pn532_packetbuffer[10];
#ifdef MIFAREDEBUG
PN532DEBUGPRINT.print(F("ATQA: 0x")); PN532DEBUGPRINT.println(sens_res, HEX);
PN532DEBUGPRINT.print(F("SAK: 0x")); PN532DEBUGPRINT.println(pn532_packetbuffer[11], HEX);
#endif
/* Card appears to be Mifare Classic */
*uidLength = pn532_packetbuffer[12];
#ifdef MIFAREDEBUG
PN532DEBUGPRINT.print(F("UID:"));
#endif
for (uint8_t i=0; i < pn532_packetbuffer[12]; i++)
{
uid[i] = pn532_packetbuffer[13+i];
#ifdef MIFAREDEBUG
PN532DEBUGPRINT.print(F(" 0x"));PN532DEBUGPRINT.print(uid[i], HEX);
#endif
}
#ifdef MIFAREDEBUG
PN532DEBUGPRINT.println();
#endif
return 1;
}
/**************************************************************************/
/*!
@brief Exchanges an APDU with the currently inlisted peer
@param send Pointer to data to send
@param sendLength Length of the data to send
@param response Pointer to response data
@param responseLength Pointer to the response data length
*/
/**************************************************************************/
bool Adafruit_PN532::inDataExchange(uint8_t * send, uint8_t sendLength, uint8_t * response, uint8_t * responseLength) {
if (sendLength > PN532_PACKBUFFSIZ-2) {
#ifdef PN532DEBUG
PN532DEBUGPRINT.println(F("APDU length too long for packet buffer"));
#endif
return false;
}
uint8_t i;
pn532_packetbuffer[0] = 0x40; // PN532_COMMAND_INDATAEXCHANGE;
pn532_packetbuffer[1] = _inListedTag;
for (i=0; i<sendLength; ++i) {
pn532_packetbuffer[i+2] = send[i];
}
if (!sendCommandCheckAck(pn532_packetbuffer,sendLength+2,1000)) {
#ifdef PN532DEBUG
PN532DEBUGPRINT.println(F("Could not send APDU"));
#endif
return false;
}
if (!waitready(1000)) {
#ifdef PN532DEBUG
PN532DEBUGPRINT.println(F("Response never received for APDU..."));
#endif
return false;
}
readdata(pn532_packetbuffer,sizeof(pn532_packetbuffer));
if (pn532_packetbuffer[0] == 0 && pn532_packetbuffer[1] == 0 && pn532_packetbuffer[2] == 0xff) {
uint8_t length = pn532_packetbuffer[3];
if (pn532_packetbuffer[4]!=(uint8_t)(~length+1)) {
#ifdef PN532DEBUG
PN532DEBUGPRINT.println(F("Length check invalid"));
PN532DEBUGPRINT.println(length,HEX);
PN532DEBUGPRINT.println((~length)+1,HEX);
#endif
return false;
}
if (pn532_packetbuffer[5]==PN532_PN532TOHOST && pn532_packetbuffer[6]==PN532_RESPONSE_INDATAEXCHANGE) {
if ((pn532_packetbuffer[7] & 0x3f)!=0) {
#ifdef PN532DEBUG
PN532DEBUGPRINT.println(F("Status code indicates an error"));
#endif
return false;
}
length -= 3;
if (length > *responseLength) {
length = *responseLength; // silent truncation...
}
for (i=0; i<length; ++i) {
response[i] = pn532_packetbuffer[8+i];
}
*responseLength = length;
return true;
}
else {
PN532DEBUGPRINT.print(F("Don't know how to handle this command: "));
PN532DEBUGPRINT.println(pn532_packetbuffer[6],HEX);
return false;
}
}
else {
PN532DEBUGPRINT.println(F("Preamble missing"));
return false;
}
}
/**************************************************************************/
/*!
@brief 'InLists' a passive target. PN532 acting as reader/initiator,
peer acting as card/responder.
*/
/**************************************************************************/
bool Adafruit_PN532::inListPassiveTarget() {
pn532_packetbuffer[0] = PN532_COMMAND_INLISTPASSIVETARGET;
pn532_packetbuffer[1] = 1;
pn532_packetbuffer[2] = 0;
#ifdef PN532DEBUG
PN532DEBUGPRINT.print(F("About to inList passive target"));
#endif
if (!sendCommandCheckAck(pn532_packetbuffer,3,1000)) {
#ifdef PN532DEBUG
PN532DEBUGPRINT.println(F("Could not send inlist message"));
#endif
return false;
}
if (!waitready(30000)) {
return false;
}
readdata(pn532_packetbuffer,sizeof(pn532_packetbuffer));
if (pn532_packetbuffer[0] == 0 && pn532_packetbuffer[1] == 0 && pn532_packetbuffer[2] == 0xff) {
uint8_t length = pn532_packetbuffer[3];
if (pn532_packetbuffer[4]!=(uint8_t)(~length+1)) {
#ifdef PN532DEBUG
PN532DEBUGPRINT.println(F("Length check invalid"));
PN532DEBUGPRINT.println(length,HEX);
PN532DEBUGPRINT.println((~length)+1,HEX);
#endif
return false;
}
if (pn532_packetbuffer[5]==PN532_PN532TOHOST && pn532_packetbuffer[6]==PN532_RESPONSE_INLISTPASSIVETARGET) {
if (pn532_packetbuffer[7] != 1) {
#ifdef PN532DEBUG
PN532DEBUGPRINT.println(F("Unhandled number of targets inlisted"));
#endif
PN532DEBUGPRINT.println(F("Number of tags inlisted:"));
PN532DEBUGPRINT.println(pn532_packetbuffer[7]);
return false;
}
_inListedTag = pn532_packetbuffer[8];
PN532DEBUGPRINT.print(F("Tag number: "));
PN532DEBUGPRINT.println(_inListedTag);
return true;
} else {
#ifdef PN532DEBUG
PN532DEBUGPRINT.print(F("Unexpected response to inlist passive host"));
#endif
return false;
}
}
else {
#ifdef PN532DEBUG
PN532DEBUGPRINT.println(F("Preamble missing"));
#endif
return false;
}
return true;
}
/***** Mifare Classic Functions ******/
/**************************************************************************/
/*!
Indicates whether the specified block number is the first block
in the sector (block 0 relative to the current sector)
*/
/**************************************************************************/
bool Adafruit_PN532::mifareclassic_IsFirstBlock (uint32_t uiBlock)
{
// Test if we are in the small or big sectors
if (uiBlock < 128)
return ((uiBlock) % 4 == 0);
else
return ((uiBlock) % 16 == 0);
}
/**************************************************************************/
/*!
Indicates whether the specified block number is the sector trailer
*/
/**************************************************************************/
bool Adafruit_PN532::mifareclassic_IsTrailerBlock (uint32_t uiBlock)
{
// Test if we are in the small or big sectors
if (uiBlock < 128)
return ((uiBlock + 1) % 4 == 0);
else
return ((uiBlock + 1) % 16 == 0);
}
/**************************************************************************/
/*!
Tries to authenticate a block of memory on a MIFARE card using the
INDATAEXCHANGE command. See section 7.3.8 of the PN532 User Manual
for more information on sending MIFARE and other commands.
@param uid Pointer to a byte array containing the card UID
@param uidLen The length (in bytes) of the card's UID (Should
be 4 for MIFARE Classic)
@param blockNumber The block number to authenticate. (0..63 for
1KB cards, and 0..255 for 4KB cards).
@param keyNumber Which key type to use during authentication
(0 = MIFARE_CMD_AUTH_A, 1 = MIFARE_CMD_AUTH_B)
@param keyData Pointer to a byte array containing the 6 byte
key value
@returns 1 if everything executed properly, 0 for an error
*/
/**************************************************************************/
uint8_t Adafruit_PN532::mifareclassic_AuthenticateBlock (uint8_t * uid, uint8_t uidLen, uint32_t blockNumber, uint8_t keyNumber, uint8_t * keyData)
{
uint8_t len;
uint8_t i;
// Hang on to the key and uid data
memcpy (_key, keyData, 6);
memcpy (_uid, uid, uidLen);
_uidLen = uidLen;
#ifdef MIFAREDEBUG
PN532DEBUGPRINT.print(F("Trying to authenticate card "));
Adafruit_PN532::PrintHex(_uid, _uidLen);
PN532DEBUGPRINT.print(F("Using authentication KEY "));PN532DEBUGPRINT.print(keyNumber ? 'B' : 'A');PN532DEBUGPRINT.print(F(": "));
Adafruit_PN532::PrintHex(_key, 6);
#endif
// Prepare the authentication command //
pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE; /* Data Exchange Header */
pn532_packetbuffer[1] = 1; /* Max card numbers */
pn532_packetbuffer[2] = (keyNumber) ? MIFARE_CMD_AUTH_B : MIFARE_CMD_AUTH_A;
pn532_packetbuffer[3] = blockNumber; /* Block Number (1K = 0..63, 4K = 0..255 */
memcpy (pn532_packetbuffer+4, _key, 6);
for (i = 0; i < _uidLen; i++)
{
pn532_packetbuffer[10+i] = _uid[i]; /* 4 byte card ID */
}
if (! sendCommandCheckAck(pn532_packetbuffer, 10+_uidLen))
return 0;
// Read the response packet
readdata(pn532_packetbuffer, 12);
// check if the response is valid and we are authenticated???
// for an auth success it should be bytes 5-7: 0xD5 0x41 0x00
// Mifare auth error is technically byte 7: 0x14 but anything other and 0x00 is not good
if (pn532_packetbuffer[7] != 0x00)
{
#ifdef PN532DEBUG
PN532DEBUGPRINT.print(F("Authentification failed: "));
Adafruit_PN532::PrintHexChar(pn532_packetbuffer, 12);
#endif
return 0;
}
return 1;
}
/**************************************************************************/
/*!
Tries to read an entire 16-byte data block at the specified block
address.
@param blockNumber The block number to authenticate. (0..63 for
1KB cards, and 0..255 for 4KB cards).
@param data Pointer to the byte array that will hold the
retrieved data (if any)
@returns 1 if everything executed properly, 0 for an error
*/
/**************************************************************************/
uint8_t Adafruit_PN532::mifareclassic_ReadDataBlock (uint8_t blockNumber, uint8_t * data)
{
#ifdef MIFAREDEBUG
PN532DEBUGPRINT.print(F("Trying to read 16 bytes from block "));PN532DEBUGPRINT.println(blockNumber);
#endif
/* Prepare the command */
pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE;
pn532_packetbuffer[1] = 1; /* Card number */
pn532_packetbuffer[2] = MIFARE_CMD_READ; /* Mifare Read command = 0x30 */
pn532_packetbuffer[3] = blockNumber; /* Block Number (0..63 for 1K, 0..255 for 4K) */
/* Send the command */
if (! sendCommandCheckAck(pn532_packetbuffer, 4))
{
#ifdef MIFAREDEBUG
PN532DEBUGPRINT.println(F("Failed to receive ACK for read command"));
#endif
return 0;
}
/* Read the response packet */
readdata(pn532_packetbuffer, 26);
/* If byte 8 isn't 0x00 we probably have an error */
if (pn532_packetbuffer[7] != 0x00)
{
#ifdef MIFAREDEBUG
PN532DEBUGPRINT.println(F("Unexpected response"));
Adafruit_PN532::PrintHexChar(pn532_packetbuffer, 26);
#endif
return 0;
}
/* Copy the 16 data bytes to the output buffer */
/* Block content starts at byte 9 of a valid response */
memcpy (data, pn532_packetbuffer+8, 16);
/* Display data for debug if requested */
#ifdef MIFAREDEBUG
PN532DEBUGPRINT.print(F("Block "));
PN532DEBUGPRINT.println(blockNumber);
Adafruit_PN532::PrintHexChar(data, 16);
#endif
return 1;
}
/**************************************************************************/
/*!
Tries to write an entire 16-byte data block at the specified block
address.
@param blockNumber The block number to authenticate. (0..63 for
1KB cards, and 0..255 for 4KB cards).
@param data The byte array that contains the data to write.
@returns 1 if everything executed properly, 0 for an error
*/
/**************************************************************************/
uint8_t Adafruit_PN532::mifareclassic_WriteDataBlock (uint8_t blockNumber, uint8_t * data)
{
#ifdef MIFAREDEBUG
PN532DEBUGPRINT.print(F("Trying to write 16 bytes to block "));PN532DEBUGPRINT.println(blockNumber);
#endif
/* 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] = blockNumber; /* Block Number (0..63 for 1K, 0..255 for 4K) */
memcpy (pn532_packetbuffer+4, data, 16); /* Data Payload */
/* Send the command */
if (! sendCommandCheckAck(pn532_packetbuffer, 20))
{
#ifdef MIFAREDEBUG
PN532DEBUGPRINT.println(F("Failed to receive ACK for write command"));
#endif
return 0;
}
delay(10);
/* Read the response packet */
readdata(pn532_packetbuffer, 26);
return 1;
}
/**************************************************************************/
/*!
Formats a Mifare Classic card to store NDEF Records
@returns 1 if everything executed properly, 0 for an error
*/
/**************************************************************************/
...
This file has been truncated, please download it to see its full contents.
/**************************************************************************/
/*!
@file Adafruit_PN532.h
@author Adafruit Industries
@license BSD (see license.txt)
This is a library 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 or I2C to communicate.
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
@section HISTORY
v2.0 - Refactored to add I2C support from Adafruit_NFCShield_I2C library.
v1.1 - Added full command list
- Added 'verbose' mode flag to constructor to toggle debug output
- Changed readPassiveTargetID() to return variable length values
*/
/**************************************************************************/
#ifndef ADAFRUIT_PN532_H
#define ADAFRUIT_PN532_H
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#define PN532_PREAMBLE (0x00)
#define PN532_STARTCODE1 (0x00)
#define PN532_STARTCODE2 (0xFF)
#define PN532_POSTAMBLE (0x00)
#define PN532_HOSTTOPN532 (0xD4)
#define PN532_PN532TOHOST (0xD5)
// PN532 Commands
#define PN532_COMMAND_DIAGNOSE (0x00)
#define PN532_COMMAND_GETFIRMWAREVERSION (0x02)
#define PN532_COMMAND_GETGENERALSTATUS (0x04)
#define PN532_COMMAND_READREGISTER (0x06)
#define PN532_COMMAND_WRITEREGISTER (0x08)
#define PN532_COMMAND_READGPIO (0x0C)
#define PN532_COMMAND_WRITEGPIO (0x0E)
#define PN532_COMMAND_SETSERIALBAUDRATE (0x10)
#define PN532_COMMAND_SETPARAMETERS (0x12)
#define PN532_COMMAND_SAMCONFIGURATION (0x14)
#define PN532_COMMAND_POWERDOWN (0x16)
#define PN532_COMMAND_RFCONFIGURATION (0x32)
#define PN532_COMMAND_RFREGULATIONTEST (0x58)
#define PN532_COMMAND_INJUMPFORDEP (0x56)
#define PN532_COMMAND_INJUMPFORPSL (0x46)
#define PN532_COMMAND_INLISTPASSIVETARGET (0x4A)
#define PN532_COMMAND_INATR (0x50)
#define PN532_COMMAND_INPSL (0x4E)
#define PN532_COMMAND_INDATAEXCHANGE (0x40)
#define PN532_COMMAND_INCOMMUNICATETHRU (0x42)
#define PN532_COMMAND_INDESELECT (0x44)
#define PN532_COMMAND_INRELEASE (0x52)
#define PN532_COMMAND_INSELECT (0x54)
#define PN532_COMMAND_INAUTOPOLL (0x60)
#define PN532_COMMAND_TGINITASTARGET (0x8C)
#define PN532_COMMAND_TGSETGENERALBYTES (0x92)
#define PN532_COMMAND_TGGETDATA (0x86)
#define PN532_COMMAND_TGSETDATA (0x8E)
#define PN532_COMMAND_TGSETMETADATA (0x94)
#define PN532_COMMAND_TGGETINITIATORCOMMAND (0x88)
#define PN532_COMMAND_TGRESPONSETOINITIATOR (0x90)
#define PN532_COMMAND_TGGETTARGETSTATUS (0x8A)
#define PN532_RESPONSE_INDATAEXCHANGE (0x41)
#define PN532_RESPONSE_INLISTPASSIVETARGET (0x4B)
#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_I2C_ADDRESS (0x48 >> 1)
#define PN532_I2C_READBIT (0x01)
#define PN532_I2C_BUSY (0x00)
#define PN532_I2C_READY (0x01)
#define PN532_I2C_READYTIMEOUT (20)
#define PN532_MIFARE_ISO14443A (0x00)
// Mifare Commands
#define MIFARE_CMD_AUTH_A (0x60)
#define MIFARE_CMD_AUTH_B (0x61)
#define MIFARE_CMD_READ (0x30)
#define MIFARE_CMD_WRITE (0xA0)
#define MIFARE_CMD_TRANSFER (0xB0)
#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)
#define NDEF_URIPREFIX_HTTP_WWWDOT (0x01)
#define NDEF_URIPREFIX_HTTPS_WWWDOT (0x02)
#define NDEF_URIPREFIX_HTTP (0x03)
#define NDEF_URIPREFIX_HTTPS (0x04)
#define NDEF_URIPREFIX_TEL (0x05)
#define NDEF_URIPREFIX_MAILTO (0x06)
#define NDEF_URIPREFIX_FTP_ANONAT (0x07)
#define NDEF_URIPREFIX_FTP_FTPDOT (0x08)
#define NDEF_URIPREFIX_FTPS (0x09)
#define NDEF_URIPREFIX_SFTP (0x0A)
#define NDEF_URIPREFIX_SMB (0x0B)
#define NDEF_URIPREFIX_NFS (0x0C)
#define NDEF_URIPREFIX_FTP (0x0D)
#define NDEF_URIPREFIX_DAV (0x0E)
#define NDEF_URIPREFIX_NEWS (0x0F)
#define NDEF_URIPREFIX_TELNET (0x10)
#define NDEF_URIPREFIX_IMAP (0x11)
#define NDEF_URIPREFIX_RTSP (0x12)
#define NDEF_URIPREFIX_URN (0x13)
#define NDEF_URIPREFIX_POP (0x14)
#define NDEF_URIPREFIX_SIP (0x15)
#define NDEF_URIPREFIX_SIPS (0x16)
#define NDEF_URIPREFIX_TFTP (0x17)
#define NDEF_URIPREFIX_BTSPP (0x18)
#define NDEF_URIPREFIX_BTL2CAP (0x19)
#define NDEF_URIPREFIX_BTGOEP (0x1A)
#define NDEF_URIPREFIX_TCPOBEX (0x1B)
#define NDEF_URIPREFIX_IRDAOBEX (0x1C)
#define NDEF_URIPREFIX_FILE (0x1D)
#define NDEF_URIPREFIX_URN_EPC_ID (0x1E)
#define NDEF_URIPREFIX_URN_EPC_TAG (0x1F)
#define NDEF_URIPREFIX_URN_EPC_PAT (0x20)
#define NDEF_URIPREFIX_URN_EPC_RAW (0x21)
#define NDEF_URIPREFIX_URN_EPC (0x22)
#define NDEF_URIPREFIX_URN_NFC (0x23)
#define PN532_GPIO_VALIDATIONBIT (0x80)
#define PN532_GPIO_P30 (0)
#define PN532_GPIO_P31 (1)
#define PN532_GPIO_P32 (2)
#define PN532_GPIO_P33 (3)
#define PN532_GPIO_P34 (4)
#define PN532_GPIO_P35 (5)
class Adafruit_PN532{
public:
Adafruit_PN532(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t ss); // Software SPI
Adafruit_PN532(uint8_t irq, uint8_t reset); // Hardware I2C
Adafruit_PN532(uint8_t ss); // Hardware SPI
void begin(void);
// Generic PN532 functions
bool SAMConfig(void);
uint32_t getFirmwareVersion(void);
bool sendCommandCheckAck(uint8_t *cmd, uint8_t cmdlen, uint16_t timeout = 1000);
bool writeGPIO(uint8_t pinstate);
uint8_t readGPIO(void);
bool setPassiveActivationRetries(uint8_t maxRetries);
// ISO14443A functions
bool readPassiveTargetID(uint8_t cardbaudrate, uint8_t * uid, uint8_t * uidLength, uint16_t timeout = 0); //timeout 0 means no timeout - will block forever.
bool inDataExchange(uint8_t * send, uint8_t sendLength, uint8_t * response, uint8_t * responseLength);
bool inListPassiveTarget();
uint8_t AsTarget();
uint8_t getDataTarget(uint8_t* cmd, uint8_t* cmdlen);
uint8_t setDataTarget(uint8_t * cmd, uint8_t cmdlen);
// Mifare Classic functions
bool mifareclassic_IsFirstBlock (uint32_t uiBlock);
bool mifareclassic_IsTrailerBlock (uint32_t uiBlock);
uint8_t mifareclassic_AuthenticateBlock (uint8_t * uid, uint8_t uidLen, uint32_t blockNumber, uint8_t keyNumber, uint8_t * keyData);
uint8_t mifareclassic_ReadDataBlock (uint8_t blockNumber, uint8_t * data);
uint8_t mifareclassic_WriteDataBlock (uint8_t blockNumber, uint8_t * data);
uint8_t mifareclassic_FormatNDEF (void);
uint8_t mifareclassic_WriteNDEFURI (uint8_t sectorNumber, uint8_t uriIdentifier, const char * url);
// Mifare Ultralight functions
uint8_t mifareultralight_ReadPage (uint8_t page, uint8_t * buffer);
uint8_t mifareultralight_WritePage (uint8_t page, uint8_t * data);
// NTAG2xx functions
uint8_t ntag2xx_ReadPage (uint8_t page, uint8_t * buffer);
uint8_t ntag2xx_WritePage (uint8_t page, uint8_t * data);
uint8_t ntag2xx_WriteNDEFURI (uint8_t uriIdentifier, char * url, uint8_t dataLen);
// Help functions to display formatted text
static void PrintHex(const byte * data, const uint32_t numBytes);
static void PrintHexChar(const byte * pbtData, const uint32_t numBytes);
private:
uint8_t _ss, _clk, _mosi, _miso;
uint8_t _irq, _reset;
uint8_t _uid[7]; // ISO14443A uid
uint8_t _uidLen; // uid len
uint8_t _key[6]; // Mifare Classic key
uint8_t _inListedTag; // Tg number of inlisted tag.
bool _usingSPI; // True if using SPI, false if using I2C.
bool _hardwareSPI; // True is using hardware SPI, false if using software SPI.
// Low level communication functions that handle both SPI and I2C.
void readdata(uint8_t* buff, uint8_t n);
void writecommand(uint8_t* cmd, uint8_t cmdlen);
bool isready();
bool waitready(uint16_t timeout);
bool readack();
// SPI-specific functions.
void spi_write(uint8_t c);
uint8_t spi_read(void);
// Note there are i2c_read and i2c_write inline functions defined in the .cpp file.
};
#endif
Comments
Please log in or sign up to comment.