pn532: initial implementation

This driver was written using an Adafruit PN532 board:

http://www.adafruit.com/products/364

It is based heavily on their arduino code, with significant
modifications.

Signed-off-by: Jon Trulson <jtrulson@ics.com>
Signed-off-by: Zion Orent <zorent@ics.com>
Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
This commit is contained in:
Jon Trulson
2015-06-12 18:10:29 -06:00
committed by Mihai Tudor Panu
parent fc3a884024
commit c586cd5177
13 changed files with 2790 additions and 0 deletions

View File

@ -126,6 +126,8 @@ add_executable (m24lr64e-example m24lr64e.cxx)
add_executable (grovecircularled-example grovecircularled.cxx)
add_executable (rgbringcoder-example rgbringcoder.cxx)
add_executable (hp20x-example hp20x.cxx)
add_executable (pn532-example pn532.cxx)
add_executable (pn532-writeurl-example pn532-writeurl.cxx)
include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
include_directories (${PROJECT_SOURCE_DIR}/src/grove)
@ -230,6 +232,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/m24lr64e)
include_directories (${PROJECT_SOURCE_DIR}/src/grovecircularled)
include_directories (${PROJECT_SOURCE_DIR}/src/rgbringcoder)
include_directories (${PROJECT_SOURCE_DIR}/src/hp20x)
include_directories (${PROJECT_SOURCE_DIR}/src/pn532)
target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT})
@ -357,3 +360,5 @@ target_link_libraries (m24lr64e-example m24lr64e ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (grovecircularled-example grovecircularled ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (rgbringcoder-example rgbringcoder ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (hp20x-example hp20x ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (pn532-example pn532 ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (pn532-writeurl-example pn532 ${CMAKE_THREAD_LIBS_INIT})

View File

@ -0,0 +1,115 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2015 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <unistd.h>
#include <iostream>
#include "pn532.h"
using namespace std;
// the URL we want to add as an NDEF record
// NOTE: this cannot exceed 34 characters.
static char url[] = "iotdk.intel.com";
int main(int argc, char **argv)
{
//! [Interesting]
// Instantiate an PN532 on I2C bus 0 (default) using gpio 3 for the
// IRQ, and gpio 2 for the reset pin.
upm::PN532 *nfc = new upm::PN532(3, 2);
if (!nfc->init())
cerr << "init() failed" << endl;
uint32_t vers = nfc->getFirmwareVersion();
if (vers)
printf("Got firmware version: 0x%08x\n", vers);
else
{
printf("Could not identify PN532\n");
return 1;
}
// Now scan and identify any cards that come in range (1 for now)
// Retry forever
nfc->setPassiveActivationRetries(0xff);
nfc->SAMConfig();
uint8_t uidSize;
uint8_t uid[7];
bool foundCard = false;
while (!foundCard)
{
memset(uid, 0, 7);
if (nfc->readPassiveTargetID(nfc->BAUD_MIFARE_ISO14443A,
uid, &uidSize, 2000))
{
// found a card
printf("Found a card: UID len %d\n", uidSize);
printf("UID: ");
for (int i = 0; i < uidSize; i++)
printf("%02x ", uid[i]);
printf("\n");
printf("SAK: 0x%02x\n", nfc->getSAK());
printf("ATQA: 0x%04x\n\n", nfc->getATQA());
foundCard = true;
}
else
{
printf("Waiting for a card...\n");
}
}
if (uidSize != 7)
{
printf("This example will only write an NDEF URI to preformatted\n");
printf("Mifare Ultralight or NTAG2XX tags\n");
return 1;
}
// 48 bytes is maximum data area on ultralight cards, so we use that
// as the maximum datasize here. Obviously if you have a bigger
// card, you can write more data.
if (!nfc->ntag2xx_WriteNDEFURI(nfc->NDEF_URIPREFIX_HTTP, url, 48))
{
// failure
printf("Failed to write NDEF record tag.\n");
return 1;
}
printf("Success, URL record written to tag.\n");
//! [Interesting]
delete nfc;
return 0;
}

101
examples/c++/pn532.cxx Normal file
View File

@ -0,0 +1,101 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2015 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <unistd.h>
#include <signal.h>
#include <iostream>
#include "pn532.h"
using namespace std;
bool shouldRun = true;
void sig_handler(int signo)
{
if (signo == SIGINT)
shouldRun = false;
}
int main(int argc, char **argv)
{
signal(SIGINT, sig_handler);
//! [Interesting]
// Instantiate an PN532 on I2C bus 0 (default) using gpio 3 for the
// IRQ, and gpio 2 for the reset pin.
upm::PN532 *nfc = new upm::PN532(3, 2);
if (!nfc->init())
cerr << "init() failed" << endl;
uint32_t vers = nfc->getFirmwareVersion();
if (vers)
printf("Got firmware version: 0x%08x\n", vers);
else
{
printf("Could not identify PN532\n");
return 1;
}
// Now scan and identify any cards that come in range (1 for now)
// Retry forever
nfc->setPassiveActivationRetries(0xff);
nfc->SAMConfig();
uint8_t uidSize;
uint8_t uid[7];
while (shouldRun)
{
memset(uid, 0, 7);
if (nfc->readPassiveTargetID(nfc->BAUD_MIFARE_ISO14443A,
uid, &uidSize, 2000))
{
// found a card
printf("Found a card: UID len %d\n", uidSize);
printf("UID: ");
for (int i = 0; i < uidSize; i++)
printf("%02x ", uid[i]);
printf("\n");
printf("SAK: 0x%02x\n", nfc->getSAK());
printf("ATQA: 0x%04x\n\n", nfc->getATQA());
sleep(1);
}
else
{
printf("Waiting for a card...\n");
}
}
//! [Interesting]
delete nfc;
return 0;
}

View File

@ -0,0 +1,136 @@
/*jslint node:true, vars:true, bitwise:true, unparam:true */
/*jshint unused:true */
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2015 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// Load PN532 module
var pn532 = require('jsupm_pn532');
// Instantiate an PN532 on I2C bus 0 (default) using gpio 3 for the
// IRQ, and gpio 2 for the reset pin.
var myNFCObj = new pn532.PN532(3, 2);
function writeUrl()
{
if (uidSize.getitem(0) != 7)
{
console.log("This example will only write an NDEF URI to preformatted");
console.log("Mifare Ultralight or NTAG2XX tags");
exit();
}
// 48 bytes is maximum data area on ultralight cards, so we use that
// as the maximum datasize here. Obviously if you have a bigger
// card, you can write more data.
if (!myNFCObj.ntag2xx_WriteNDEFURI(pn532.PN532.NDEF_URIPREFIX_HTTP,
url, 48))
{
// failure
console.log("Failed to write NDEF record tag.");
exit(1);
}
console.log("Success, URL record written to tag.");
}
function toHex(d, pad)
{
// pad should be between 1 and 8
return ("00000000"+(Number(d).toString(16))).slice(-pad)
}
function exit()
{
clearInterval(myInterval);
myNFCObj = null;
pn532.cleanUp();
pn532 = null;
console.log("Exiting");
process.exit(0);
}
// When exiting: clear interval, and print message
process.on('SIGINT', function()
{
exit();
});
// "main"
if (!myNFCObj.init())
console.log("init() failed");
var vers = myNFCObj.getFirmwareVersion();
if (vers)
console.log("Got firmware version: " + toHex(vers, 8));
else
{
console.log("Could not identify PN532");
exit();
}
// Now scan and identify any cards that come in range (1 for now)
// Retry forever
myNFCObj.setPassiveActivationRetries(0xff);
myNFCObj.SAMConfig();
var uidSize = new pn532.uint8Array(0);
var uid = new pn532.uint8Array(7);
// the URL we want to add as an NDEF record
// NOTE: this cannot exceed 34 characters.
url = "iotdk.intel.com";
var myInterval = setInterval(function()
{
for (var x = 0; x < 7; x++)
uid.setitem(x, 0);
if (myNFCObj.readPassiveTargetID(pn532.PN532.BAUD_MIFARE_ISO14443A,
uid, uidSize, 2000))
{
// found a card
console.log("Found a card: UID len " + uidSize.getitem(0));
process.stdout.write("UID: ");
for (var i = 0; i < uidSize.getitem(0); i++)
{
var byteVal = uid.getitem(i);
process.stdout.write(toHex(byteVal, 2) + " ");
}
process.stdout.write("\n");
console.log("SAK: " + toHex(myNFCObj.getSAK(), 2));
console.log("ATQA: " + toHex(myNFCObj.getATQA(), 4));
console.log(" ");
// write the URL
writeUrl();
clearInterval(myInterval);
return;
}
else
console.log("Waiting for a card...");
}, 1000);

View File

@ -0,0 +1,102 @@
/*jslint node:true, vars:true, bitwise:true, unparam:true */
/*jshint unused:true */
/*
* Author: Zion Orent <zorent@ics.com>
* Copyright (c) 2015 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// Load PN532 module
var pn532 = require('jsupm_pn532');
// Instantiate an PN532 on I2C bus 0 (default) using gpio 3 for the
// IRQ, and gpio 2 for the reset pin.
var myNFCObj = new pn532.PN532(3, 2);
if (!myNFCObj.init())
console.log("init() failed");
var vers = myNFCObj.getFirmwareVersion();
if (vers)
console.log("Got firmware version: " + toHex(vers, 8));
else
{
console.log("Could not identify PN532");
exit();
}
// Now scan and identify any cards that come in range (1 for now)
// Retry forever
myNFCObj.setPassiveActivationRetries(0xff);
myNFCObj.SAMConfig();
var uidSize = new pn532.uint8Array(0);
var uid = new pn532.uint8Array(7);
var myInterval = setInterval(function()
{
for (var x = 0; x < 7; x++)
uid.setitem(x, 0);
if (myNFCObj.readPassiveTargetID(pn532.PN532.BAUD_MIFARE_ISO14443A,
uid, uidSize, 2000))
{
// found a card
console.log("Found a card: UID len " + uidSize.getitem(0));
process.stdout.write("UID: ");
for (var i = 0; i < uidSize.getitem(0); i++)
{
var byteVal = uid.getitem(i);
process.stdout.write(toHex(byteVal, 2) + " ");
}
process.stdout.write("\n");
console.log("SAK: " + toHex(myNFCObj.getSAK(), 2));
console.log("ATQA: " + toHex(myNFCObj.getATQA(), 4));
console.log(" ");
}
else
console.log("Waiting for a card...");
}, 1000);
function toHex(d, pad)
{
// pad should be between 1 and 8
return ("00000000"+(Number(d).toString(16))).slice(-pad)
}
function exit()
{
clearInterval(myInterval);
myNFCObj = null;
pn532.cleanUp();
pn532 = null;
console.log("Exiting");
process.exit(0);
}
// When exiting: clear interval, and print message
process.on('SIGINT', function()
{
exit();
});

View File

@ -0,0 +1,105 @@
#!/usr/bin/python
# Author: Jon Trulson <jtrulson@ics.com>
# Copyright (c) 2015 Intel Corporation.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import time, sys, signal, atexit
import pyupm_pn532 as upmPn532
# Instantiate an PN532 on I2C bus 0 (default) using gpio 3 for the
# IRQ, and gpio 2 for the reset pin.
myNFC = upmPn532.PN532(3, 2)
## Exit handlers ##
# This stops python from printing a stacktrace when you hit control-C
def SIGINTHandler(signum, frame):
raise SystemExit
# This lets you run code on exit
def exitHandler():
print "Exiting"
sys.exit(0)
# Register exit handlers
atexit.register(exitHandler)
signal.signal(signal.SIGINT, SIGINTHandler)
if (not myNFC.init()):
print "init() failed"
sys.exit(0)
vers = myNFC.getFirmwareVersion()
if (vers):
print "Got firmware version: %08x" % vers
else:
print "Could not identify PN532"
sys.exit(0)
# Now scan and identify any cards that come in range (1 for now)
# Retry forever
myNFC.setPassiveActivationRetries(0xff)
myNFC.SAMConfig()
uidSize = upmPn532.uint8Array(0)
uid = upmPn532.uint8Array(7)
# the URL we want to add as an NDEF record
# NOTE: this cannot exceed 34 characters.
url = "iotdk.intel.com"
foundCard = False
while (not foundCard):
for i in range(7):
uid.__setitem__(i, 0)
if (myNFC.readPassiveTargetID(upmPn532.PN532.BAUD_MIFARE_ISO14443A,
uid, uidSize, 2000)):
# found a card
print "Found a card: UID len", uidSize.__getitem__(0)
print "UID: ",
for i in range(uidSize.__getitem__(0)):
print "%02x" % uid.__getitem__(i),
print
print "SAK: %02x" % myNFC.getSAK()
print "ATQA: %04x" % myNFC.getATQA()
print
foundCard = True
else:
print "Waiting for a card...\n"
if (uidSize.__getitem__(0) != 7):
print "This example will only write an NDEF URI to preformatted"
print "Mifare Ultralight or NTAG2XX tags"
sys.exit(1)
# 48 bytes is maximum data area on ultralight cards, so we use that
# as the maximum datasize here. Obviously if you have a bigger
# card, you can write more data.
if (not myNFC.ntag2xx_WriteNDEFURI(upmPn532.PN532.NDEF_URIPREFIX_HTTP, url, 48)):
# failure
print "Failed to write NDEF record tag."
sys.exit(1)
print "Success, URL record written to tag."

85
examples/python/pn532.py Normal file
View File

@ -0,0 +1,85 @@
#!/usr/bin/python
# Author: Zion Orent <zorent@ics.com>
# Copyright (c) 2015 Intel Corporation.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import time, sys, signal, atexit
import pyupm_pn532 as upmPn532
# Instantiate an PN532 on I2C bus 0 (default) using gpio 3 for the
# IRQ, and gpio 2 for the reset pin.
myNFC = upmPn532.PN532(3, 2)
## Exit handlers ##
# This stops python from printing a stacktrace when you hit control-C
def SIGINTHandler(signum, frame):
raise SystemExit
# This lets you run code on exit
def exitHandler():
print "Exiting"
sys.exit(0)
# Register exit handlers
atexit.register(exitHandler)
signal.signal(signal.SIGINT, SIGINTHandler)
if (not myNFC.init()):
print "init() failed"
sys.exit(0)
vers = myNFC.getFirmwareVersion()
if (vers):
print "Got firmware version: %08x" % vers
else:
print "Could not identify PN532"
sys.exit(0)
# Now scan and identify any cards that come in range (1 for now)
# Retry forever
myNFC.setPassiveActivationRetries(0xff)
myNFC.SAMConfig()
uidSize = upmPn532.uint8Array(0)
uid = upmPn532.uint8Array(7)
while (1):
for i in range(7):
uid.__setitem__(i, 0)
if (myNFC.readPassiveTargetID(upmPn532.PN532.BAUD_MIFARE_ISO14443A,
uid, uidSize, 2000)):
# found a card
print "Found a card: UID len", uidSize.__getitem__(0)
print "UID: ",
for i in range(uidSize.__getitem__(0)):
print "%02x" % uid.__getitem__(i),
print
print "SAK: %02x" % myNFC.getSAK()
print "ATQA: %04x" % myNFC.getATQA()
print
time.sleep(1)
else:
print "Waiting for a card...\n"