PN532 Driver library This library provides an abstract API to drive the pn532 nfc chip, with I2C/HSU/SPI interface. Its based on the Seeed Studio's Arduino version.
Dependents: PN532_ReadUid Nfctest2
Revision 1:b5922b3b3257, committed 2016-09-13
- Comitter:
- dotnfc
- Date:
- Tue Sep 13 06:17:35 2016 +0000
- Parent:
- 0:db8030e71f55
- Commit message:
- Remove ununsed files.
Changed in this revision
diff -r db8030e71f55 -r b5922b3b3257 LICENSE --- a/LICENSE Tue Sep 13 06:01:19 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,21 +0,0 @@ -MIT License - -Copyright (c) 2016 dotnfc - -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.
diff -r db8030e71f55 -r b5922b3b3257 PN532/examples/android_hce/android_hce.ino --- a/PN532/examples/android_hce/android_hce.ino Tue Sep 13 06:01:19 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,135 +0,0 @@ -#include <SPI.h> -#include <PN532_SPI.h> -#include <PN532Interface.h> -#include <PN532.h> - -PN532_SPI pn532spi(SPI, 10); -PN532 nfc(pn532spi); - - -void setup() -{ - Serial.begin(115200); - Serial.println("-------Peer to Peer HCE--------"); - - 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); - - // 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(); -} - -void loop() -{ - bool success; - - uint8_t responseLength = 32; - - Serial.println("Waiting for an ISO14443A card"); - - // set shield to inListPassiveTarget - success = nfc.inListPassiveTarget(); - - if(success) { - - Serial.println("Found something!"); - - uint8_t selectApdu[] = { 0x00, /* CLA */ - 0xA4, /* INS */ - 0x04, /* P1 */ - 0x00, /* P2 */ - 0x07, /* Length of AID */ - 0xF0, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, /* AID defined on Android App */ - 0x00 /* Le */ }; - - uint8_t response[32]; - - success = nfc.inDataExchange(selectApdu, sizeof(selectApdu), response, &responseLength); - - if(success) { - - Serial.print("responseLength: "); Serial.println(responseLength); - - nfc.PrintHexChar(response, responseLength); - - do { - uint8_t apdu[] = "Hello from Arduino"; - uint8_t back[32]; - uint8_t length = 32; - - success = nfc.inDataExchange(apdu, sizeof(apdu), back, &length); - - if(success) { - - Serial.print("responseLength: "); Serial.println(length); - - nfc.PrintHexChar(back, length); - } - else { - - Serial.println("Broken connection?"); - } - } - while(success); - } - else { - - Serial.println("Failed sending SELECT AID"); - } - } - else { - - Serial.println("Didn't find anything!"); - } - - delay(1000); -} - -void printResponse(uint8_t *response, uint8_t responseLength) { - - String respBuffer; - - for (int i = 0; i < responseLength; i++) { - - if (response[i] < 0x10) - respBuffer = respBuffer + "0"; //Adds leading zeros if hex value is smaller than 0x10 - - respBuffer = respBuffer + String(response[i], HEX) + " "; - } - - Serial.print("response: "); Serial.println(respBuffer); -} - -void setupNFC() { - - 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); - - // configure board to read RFID tags - nfc.SAMConfig(); -}
diff -r db8030e71f55 -r b5922b3b3257 PN532/examples/emulate_tag_ndef/emulate_tag_ndef.ino --- a/PN532/examples/emulate_tag_ndef/emulate_tag_ndef.ino Tue Sep 13 06:01:19 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,69 +0,0 @@ - -#include "SPI.h" -#include "PN532_SPI.h" -#include "emulatetag.h" -#include "NdefMessage.h" - -PN532_SPI pn532spi(SPI, 10); -EmulateTag nfc(pn532spi); - -uint8_t ndefBuf[120]; -NdefMessage message; -int messageSize; - -uint8_t uid[3] = { 0x12, 0x34, 0x56 }; - -void setup() -{ - Serial.begin(115200); - Serial.println("------- Emulate Tag --------"); - - message = NdefMessage(); - message.addUriRecord("http://www.seeedstudio.com"); - messageSize = message.getEncodedSize(); - if (messageSize > sizeof(ndefBuf)) { - Serial.println("ndefBuf is too small"); - while (1) { } - } - - Serial.print("Ndef encoded message size: "); - Serial.println(messageSize); - - message.encode(ndefBuf); - - // comment out this command for no ndef message - nfc.setNdefFile(ndefBuf, messageSize); - - // uid must be 3 bytes! - nfc.setUid(uid); - - nfc.init(); -} - -void loop(){ - // uncomment for overriding ndef in case a write to this tag occured - //nfc.setNdefFile(ndefBuf, messageSize); - - // start emulation (blocks) - nfc.emulate(); - - // or start emulation with timeout - /*if(!nfc.emulate(1000)){ // timeout 1 second - Serial.println("timed out"); - }*/ - - // deny writing to the tag - // nfc.setTagWriteable(false); - - if(nfc.writeOccured()){ - Serial.println("\nWrite occured !"); - uint8_t* tag_buf; - uint16_t length; - - nfc.getContent(&tag_buf, &length); - NdefMessage msg = NdefMessage(tag_buf, length); - msg.print(); - } - - delay(1000); -}
diff -r db8030e71f55 -r b5922b3b3257 PN532/examples/iso14443a_uid/iso14443a_uid.pde --- a/PN532/examples/iso14443a_uid/iso14443a_uid.pde Tue Sep 13 06:01:19 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,78 +0,0 @@ -/**************************************************************************/ -/*! - This example will attempt to connect to an ISO14443A - card or tag and retrieve some basic information about it - that can be used to determine what type of card it is. - - Note that you need the baud rate to be 115200 because we need to print - out the data and read from the card at the same time! - - To enable debug message, define DEBUG in PN532/PN532_debug.h - -*/ -/**************************************************************************/ - -#include <SPI.h> -#include <PN532_SPI.h> -#include "PN532.h" - -PN532_SPI pn532spi(SPI, 10); -PN532 nfc(pn532spi); - -void setup(void) { - Serial.begin(115200); - 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); - - // 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(); - - Serial.println("Waiting for an ISO14443A card"); -} - -void loop(void) { - boolean 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) - - // 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[0], &uidLength); - - if (success) { - Serial.println("Found a card!"); - Serial.print("UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes"); - Serial.print("UID Value: "); - for (uint8_t i=0; i < uidLength; i++) - { - Serial.print(" 0x");Serial.print(uid[i], HEX); - } - Serial.println(""); - - // wait until the card is taken away - while (nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength)) {} - } - else - { - // PN532 probably timed out waiting for a card - Serial.println("Timed out waiting for a card"); - } -}
diff -r db8030e71f55 -r b5922b3b3257 PN532/examples/mifareclassic_formatndef/mifareclassic_formatndef.pde --- a/PN532/examples/mifareclassic_formatndef/mifareclassic_formatndef.pde Tue Sep 13 06:01:19 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,170 +0,0 @@ -/**************************************************************************/ -/*! - This example attempts to format a clean Mifare Classic 1K card as - an NFC Forum tag (to store NDEF messages that can be read by any - NFC enabled Android phone, etc.) - - Note that you need the baud rate to be 115200 because we need to print - out the data and read from the card at the same time! - - To enable debug message, define DEBUG in PN532/PN532_debug.h -*/ -/**************************************************************************/ - -#include <SPI.h> -#include <PN532_SPI.h> -#include "PN532.h" - -PN532_SPI pn532spi(SPI, 10); -PN532 nfc(pn532spi); - -/* - We can encode many different kinds of pointers to the card, - from a URL, to an Email address, to a phone number, and many more - check the library header .h file to see the large # of supported - prefixes! -*/ -// For a http://www. url: -const char * url = "seeedstudio.com"; -uint8_t ndefprefix = NDEF_URIPREFIX_HTTP_WWWDOT; - -// for an email address -//const char * url = "mail@example.com"; -//uint8_t ndefprefix = NDEF_URIPREFIX_MAILTO; - -// for a phone number -//const char * url = "+1 212 555 1212"; -//uint8_t ndefprefix = NDEF_URIPREFIX_TEL; - - -void setup(void) { - Serial.begin(115200); - Serial.println("Looking for PN532..."); - - 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); - - // configure board to read RFID tags - nfc.SAMConfig(); -} - -void loop(void) { - uint8_t success; // Flag to check if there was an error with the PN532 - 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) - bool authenticated = false; // Flag to indicate if the sector is authenticated - - // Use the default key - uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; - - Serial.println(""); - Serial.println("PLEASE NOTE: Formatting your card for NDEF records will change the"); - Serial.println("authentication keys. To reformat your NDEF tag as a clean Mifare"); - Serial.println("Classic tag, use the mifareclassic_ndeftoclassic example!"); - Serial.println(""); - Serial.println("Place your Mifare Classic card on the reader to format with NDEF"); - Serial.println("and press any key to continue ..."); - // Wait for user input before proceeding - while (!Serial.available()); - // a key was pressed1 - while (Serial.available()) Serial.read(); - - // Wait for an ISO14443A type card (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 - Serial.println("Found an ISO14443A card"); - Serial.print(" UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes"); - Serial.print(" UID Value: "); - nfc.PrintHex(uid, uidLength); - for (uint8_t i = 0; i < uidLength; i++) { - Serial.print(uid[i], HEX); - Serial.print(' '); - } - Serial.println(""); - - // Make sure this is a Mifare Classic card - if (uidLength != 4) - { - Serial.println("Ooops ... this doesn't seem to be a Mifare Classic card!"); - return; - } - - // We probably have a Mifare Classic card ... - Serial.println("Seems to be a Mifare Classic card (4 byte UID)"); - - // Try to format the card for NDEF data - success = nfc.mifareclassic_AuthenticateBlock (uid, uidLength, 0, 0, keya); - if (!success) - { - Serial.println("Unable to authenticate block 0 to enable card formatting!"); - return; - } - success = nfc.mifareclassic_FormatNDEF(); - if (!success) - { - Serial.println("Unable to format the card for NDEF"); - return; - } - - Serial.println("Card has been formatted for NDEF data using MAD1"); - - // Try to authenticate block 4 (first block of sector 1) using our key - success = nfc.mifareclassic_AuthenticateBlock (uid, uidLength, 4, 0, keya); - - // Make sure the authentification process didn't fail - if (!success) - { - Serial.println("Authentication failed."); - return; - } - - // Try to write a URL - Serial.println("Writing URI to sector 1 as an NDEF Message"); - - // Authenticated seems to have worked - // Try to write an NDEF record to sector 1 - // Use 0x01 for the URI Identifier Code to prepend "http://www." - // to the url (and save some space). For information on URI ID Codes - // see http://www.ladyada.net/wiki/private/articlestaging/nfc/ndef - if (strlen(url) > 38) - { - // The length is also checked in the WriteNDEFURI function, but lets - // warn users here just in case they change the value and it's bigger - // than it should be - Serial.println("URI is too long ... must be less than 38 characters long"); - return; - } - - // URI is within size limits ... write it to the card and report success/failure - success = nfc.mifareclassic_WriteNDEFURI(1, ndefprefix, url); - if (success) - { - Serial.println("NDEF URI Record written to sector 1"); - } - else - { - Serial.println("NDEF Record creation failed! :("); - } - } - - // Wait a bit before trying again - Serial.println("\n\nDone!"); - delay(1000); - Serial.flush(); - while(Serial.available()) Serial.read(); -} \ No newline at end of file
diff -r db8030e71f55 -r b5922b3b3257 PN532/examples/mifareclassic_memdump/mifareclassic_memdump.pde --- a/PN532/examples/mifareclassic_memdump/mifareclassic_memdump.pde Tue Sep 13 06:01:19 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,157 +0,0 @@ -/**************************************************************************/ -/*! - This example attempts to dump the contents of a Mifare Classic 1K card - - Note that you need the baud rate to be 115200 because we need to print - out the data and read from the card at the same time! - - To enable debug message, define DEBUG in PN532/PN532_debug.h -*/ -/**************************************************************************/ - -#include <SPI.h> -#include <PN532_SPI.h> -#include "PN532.h" - -PN532_SPI pn532spi(SPI, 10); -PN532 nfc(pn532spi); - -void setup(void) { - // has to be fast to dump the entire memory contents! - Serial.begin(115200); - Serial.println("Looking for PN532..."); - - 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); - - // configure board to read RFID tags - nfc.SAMConfig(); - - Serial.println("Waiting for an ISO14443A Card ..."); -} - - -void loop(void) { - uint8_t success; // Flag to check if there was an error with the PN532 - 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) - uint8_t currentblock; // Counter to keep track of which block we're on - bool authenticated = false; // Flag to indicate if the sector is authenticated - uint8_t data[16]; // Array to store block data during reads - - // Keyb on NDEF and Mifare Classic should be the same - uint8_t keyuniversal[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; - - // 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 - Serial.println("Found an ISO14443A card"); - Serial.print(" UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes"); - Serial.print(" UID Value: "); - for (uint8_t i = 0; i < uidLength; i++) { - Serial.print(uid[i], HEX); - Serial.print(' '); - } - Serial.println(""); - - if (uidLength == 4) - { - // We probably have a Mifare Classic card ... - Serial.println("Seems to be a Mifare Classic card (4 byte UID)"); - - // Now we try to go through all 16 sectors (each having 4 blocks) - // authenticating each sector, and then dumping the blocks - for (currentblock = 0; currentblock < 64; currentblock++) - { - // Check if this is a new block so that we can reauthenticate - if (nfc.mifareclassic_IsFirstBlock(currentblock)) authenticated = false; - - // If the sector hasn't been authenticated, do so first - if (!authenticated) - { - // Starting of a new sector ... try to to authenticate - Serial.print("------------------------Sector ");Serial.print(currentblock/4, DEC);Serial.println("-------------------------"); - if (currentblock == 0) - { - // This will be 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF for Mifare Classic (non-NDEF!) - // or 0xA0 0xA1 0xA2 0xA3 0xA4 0xA5 for NDEF formatted cards using key a, - // but keyb should be the same for both (0xFF 0xFF 0xFF 0xFF 0xFF 0xFF) - success = nfc.mifareclassic_AuthenticateBlock (uid, uidLength, currentblock, 1, keyuniversal); - } - else - { - // This will be 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF for Mifare Classic (non-NDEF!) - // or 0xD3 0xF7 0xD3 0xF7 0xD3 0xF7 for NDEF formatted cards using key a, - // but keyb should be the same for both (0xFF 0xFF 0xFF 0xFF 0xFF 0xFF) - success = nfc.mifareclassic_AuthenticateBlock (uid, uidLength, currentblock, 1, keyuniversal); - } - if (success) - { - authenticated = true; - } - else - { - Serial.println("Authentication error"); - } - } - // If we're still not authenticated just skip the block - if (!authenticated) - { - Serial.print("Block ");Serial.print(currentblock, DEC);Serial.println(" unable to authenticate"); - } - else - { - // Authenticated ... we should be able to read the block now - // Dump the data into the 'data' array - success = nfc.mifareclassic_ReadDataBlock(currentblock, data); - if (success) - { - // Read successful - Serial.print("Block ");Serial.print(currentblock, DEC); - if (currentblock < 10) - { - Serial.print(" "); - } - else - { - Serial.print(" "); - } - // Dump the raw data - nfc.PrintHexChar(data, 16); - } - else - { - // Oops ... something happened - Serial.print("Block ");Serial.print(currentblock, DEC); - Serial.println(" unable to read this block"); - } - } - } - } - else - { - Serial.println("Ooops ... this doesn't seem to be a Mifare Classic card!"); - } - } - // Wait a bit before trying again - Serial.println("\n\nSend a character to run the mem dumper again!"); - Serial.flush(); - while (!Serial.available()); - while (Serial.available()) { - Serial.read(); - } - Serial.flush(); -} \ No newline at end of file
diff -r db8030e71f55 -r b5922b3b3257 PN532/examples/mifareclassic_ndeftoclassic/mifareclassic_ndeftoclassic.pde --- a/PN532/examples/mifareclassic_ndeftoclassic/mifareclassic_ndeftoclassic.pde Tue Sep 13 06:01:19 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,172 +0,0 @@ -/**************************************************************************/ -/*! - This examples attempts to take a Mifare Classic 1K card that has been - formatted for NDEF messages using mifareclassic_formatndef, and resets - the authentication keys back to the Mifare Classic defaults - - To enable debug message, define DEBUG in PN532/PN532_debug.h -*/ -/**************************************************************************/ - -#include <SPI.h> -#include <PN532_SPI.h> -#include "PN532.h" - -PN532_SPI pn532spi(SPI, 10); -PN532 nfc(pn532spi); - - -#define NR_SHORTSECTOR (32) // Number of short sectors on Mifare 1K/4K -#define NR_LONGSECTOR (8) // Number of long sectors on Mifare 4K -#define NR_BLOCK_OF_SHORTSECTOR (4) // Number of blocks in a short sector -#define NR_BLOCK_OF_LONGSECTOR (16) // Number of blocks in a long sector - -// Determine the sector trailer block based on sector number -#define BLOCK_NUMBER_OF_SECTOR_TRAILER(sector) (((sector)<NR_SHORTSECTOR)? \ - ((sector)*NR_BLOCK_OF_SHORTSECTOR + NR_BLOCK_OF_SHORTSECTOR-1):\ - (NR_SHORTSECTOR*NR_BLOCK_OF_SHORTSECTOR + (sector-NR_SHORTSECTOR)*NR_BLOCK_OF_LONGSECTOR + NR_BLOCK_OF_LONGSECTOR-1)) - -// Determine the sector's first block based on the sector number -#define BLOCK_NUMBER_OF_SECTOR_1ST_BLOCK(sector) (((sector)<NR_SHORTSECTOR)? \ - ((sector)*NR_BLOCK_OF_SHORTSECTOR):\ - (NR_SHORTSECTOR*NR_BLOCK_OF_SHORTSECTOR + (sector-NR_SHORTSECTOR)*NR_BLOCK_OF_LONGSECTOR)) - -// The default Mifare Classic key -static const uint8_t KEY_DEFAULT_KEYAB[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; - -void setup(void) { - Serial.begin(115200); - Serial.println("Looking for PN532..."); - - 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); - - // configure board to read RFID tags - nfc.SAMConfig(); -} - -void loop(void) { - uint8_t success; // Flag to check if there was an error with the PN532 - 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) - bool authenticated = false; // Flag to indicate if the sector is authenticated - uint8_t blockBuffer[16]; // Buffer to store block contents - uint8_t blankAccessBits[3] = { 0xff, 0x07, 0x80 }; - uint8_t idx = 0; - uint8_t numOfSector = 16; // Assume Mifare Classic 1K for now (16 4-block sectors) - - Serial.println("Place your NDEF formatted Mifare Classic 1K card on the reader"); - Serial.println("and press any key to continue ..."); - - // Wait for user input before proceeding - while (!Serial.available()); - while (Serial.available()) Serial.read(); - - // Wait for an ISO14443A type card (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) - { - // We seem to have a tag ... - // Display some basic information about it - Serial.println("Found an ISO14443A card/tag"); - Serial.print(" UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes"); - Serial.print(" UID Value: "); - nfc.PrintHex(uid, uidLength); - Serial.println(""); - - // Make sure this is a Mifare Classic card - if (uidLength != 4) - { - Serial.println("Ooops ... this doesn't seem to be a Mifare Classic card!"); - return; - } - - Serial.println("Seems to be a Mifare Classic card (4 byte UID)"); - Serial.println(""); - Serial.println("Reformatting card for Mifare Classic (please don't touch it!) ... "); - - // Now run through the card sector by sector - for (idx = 0; idx < numOfSector; idx++) - { - // Step 1: Authenticate the current sector using key B 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF - success = nfc.mifareclassic_AuthenticateBlock (uid, uidLength, BLOCK_NUMBER_OF_SECTOR_TRAILER(idx), 1, (uint8_t *)KEY_DEFAULT_KEYAB); - if (!success) - { - Serial.print("Authentication failed for sector "); Serial.println(numOfSector); - return; - } - - // Step 2: Write to the other blocks - if (idx == 16) - { - memset(blockBuffer, 0, sizeof(blockBuffer)); - if (!(nfc.mifareclassic_WriteDataBlock((BLOCK_NUMBER_OF_SECTOR_TRAILER(idx)) - 3, blockBuffer))) - { - Serial.print("Unable to write to sector "); Serial.println(numOfSector); - return; - } - } - if ((idx == 0) || (idx == 16)) - { - memset(blockBuffer, 0, sizeof(blockBuffer)); - if (!(nfc.mifareclassic_WriteDataBlock((BLOCK_NUMBER_OF_SECTOR_TRAILER(idx)) - 2, blockBuffer))) - { - Serial.print("Unable to write to sector "); Serial.println(numOfSector); - return; - } - } - else - { - memset(blockBuffer, 0, sizeof(blockBuffer)); - if (!(nfc.mifareclassic_WriteDataBlock((BLOCK_NUMBER_OF_SECTOR_TRAILER(idx)) - 3, blockBuffer))) - { - Serial.print("Unable to write to sector "); Serial.println(numOfSector); - return; - } - if (!(nfc.mifareclassic_WriteDataBlock((BLOCK_NUMBER_OF_SECTOR_TRAILER(idx)) - 2, blockBuffer))) - { - Serial.print("Unable to write to sector "); Serial.println(numOfSector); - return; - } - } - memset(blockBuffer, 0, sizeof(blockBuffer)); - if (!(nfc.mifareclassic_WriteDataBlock((BLOCK_NUMBER_OF_SECTOR_TRAILER(idx)) - 1, blockBuffer))) - { - Serial.print("Unable to write to sector "); Serial.println(numOfSector); - return; - } - - // Step 3: Reset both keys to 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF - memcpy(blockBuffer, KEY_DEFAULT_KEYAB, sizeof(KEY_DEFAULT_KEYAB)); - memcpy(blockBuffer + 6, blankAccessBits, sizeof(blankAccessBits)); - blockBuffer[9] = 0x69; - memcpy(blockBuffer + 10, KEY_DEFAULT_KEYAB, sizeof(KEY_DEFAULT_KEYAB)); - - // Step 4: Write the trailer block - if (!(nfc.mifareclassic_WriteDataBlock((BLOCK_NUMBER_OF_SECTOR_TRAILER(idx)), blockBuffer))) - { - Serial.print("Unable to write trailer block of sector "); Serial.println(numOfSector); - return; - } - } - } - - // Wait a bit before trying again - Serial.println("\n\nDone!"); - delay(1000); - Serial.flush(); - while(Serial.available()) Serial.read(); -} \ No newline at end of file
diff -r db8030e71f55 -r b5922b3b3257 PN532/examples/mifareclassic_updatendef/mifareclassic_updatendef.pde --- a/PN532/examples/mifareclassic_updatendef/mifareclassic_updatendef.pde Tue Sep 13 06:01:19 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,145 +0,0 @@ -/**************************************************************************/ -/*! - Updates a sector that is already formatted for NDEF (using - mifareclassic_formatndef.pde for example), inserting a new url - - To enable debug message, define DEBUG in PN532/PN532_debug.h -*/ -/**************************************************************************/ - -#include <SPI.h> -#include <PN532_SPI.h> -#include "PN532.h" - -PN532_SPI pn532spi(SPI, 10); -PN532 nfc(pn532spi); - - - -/* - We can encode many different kinds of pointers to the card, - from a URL, to an Email address, to a phone number, and many more - check the library header .h file to see the large # of supported - prefixes! -*/ -// For a http://www. url: -const char * url = "seeedstudio.com"; -uint8_t ndefprefix = NDEF_URIPREFIX_HTTP_WWWDOT; - -// for an email address -//const char * url = "mail@example.com"; -//uint8_t ndefprefix = NDEF_URIPREFIX_MAILTO; - -// for a phone number -//const char * url = "+1 212 555 1212"; -//uint8_t ndefprefix = NDEF_URIPREFIX_TEL; - - -void setup(void) { - Serial.begin(115200); - Serial.println("Looking for PN532..."); - - 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); - - // configure board to read RFID tags - nfc.SAMConfig(); -} - -void loop(void) { - uint8_t success; // Flag to check if there was an error with the PN532 - 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) - bool authenticated = false; // Flag to indicate if the sector is authenticated - - // Use the default NDEF keys (these would have have set by mifareclassic_formatndef.pde!) - uint8_t keya[6] = { 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5 }; - uint8_t keyb[6] = { 0xD3, 0xF7, 0xD3, 0xF7, 0xD3, 0xF7 }; - - Serial.println("Place your NDEF formatted Mifare Classic card on the reader to update the"); - Serial.println("NDEF record and press any key to continue ..."); - // Wait for user input before proceeding - while (!Serial.available()); - // a key was pressed1 - while (Serial.available()) Serial.read(); - - // Wait for an ISO14443A type card (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 - Serial.println("Found an ISO14443A card"); - Serial.print(" UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes"); - Serial.print(" UID Value: "); - nfc.PrintHex(uid, uidLength); - Serial.println(""); - - // Make sure this is a Mifare Classic card - if (uidLength != 4) - { - Serial.println("Ooops ... this doesn't seem to be a Mifare Classic card!"); - return; - } - - // We probably have a Mifare Classic card ... - Serial.println("Seems to be a Mifare Classic card (4 byte UID)"); - - // Check if this is an NDEF card (using first block of sector 1 from mifareclassic_formatndef.pde) - // Must authenticate on the first key using 0xD3 0xF7 0xD3 0xF7 0xD3 0xF7 - success = nfc.mifareclassic_AuthenticateBlock (uid, uidLength, 4, 0, keyb); - if (!success) - { - Serial.println("Unable to authenticate block 4 ... is this card NDEF formatted?"); - return; - } - - Serial.println("Authentication succeeded (seems to be an NDEF/NFC Forum tag) ..."); - - // Authenticated seems to have worked - // Try to write an NDEF record to sector 1 - // Use 0x01 for the URI Identifier Code to prepend "http://www." - // to the url (and save some space). For information on URI ID Codes - // see http://www.ladyada.net/wiki/private/articlestaging/nfc/ndef - if (strlen(url) > 38) - { - // The length is also checked in the WriteNDEFURI function, but lets - // warn users here just in case they change the value and it's bigger - // than it should be - Serial.println("URI is too long ... must be less than 38 characters!"); - return; - } - - Serial.println("Updating sector 1 with URI as NDEF Message"); - - // URI is within size limits ... write it to the card and report success/failure - success = nfc.mifareclassic_WriteNDEFURI(1, ndefprefix, url); - if (success) - { - Serial.println("NDEF URI Record written to sector 1"); - Serial.println(""); - } - else - { - Serial.println("NDEF Record creation failed! :("); - } - } - - // Wait a bit before trying again - Serial.println("\n\nDone!"); - delay(1000); - Serial.flush(); - while(Serial.available()) Serial.read(); -}
diff -r db8030e71f55 -r b5922b3b3257 PN532/examples/p2p_raw/p2p_raw.ino --- a/PN532/examples/p2p_raw/p2p_raw.ino Tue Sep 13 06:01:19 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,51 +0,0 @@ -// snep_test.ino -// send a SNEP message to adnroid and get a message from android - -#include "SPI.h" -#include "PN532_SPI.h" -#include "llcp.h" -#include "snep.h" - -PN532_SPI pn532spi(SPI, 10); -SNEP nfc(pn532spi); - -void setup() -{ - Serial.begin(115200); - Serial.println("-------Peer to Peer--------"); -} - -uint8_t message[] = { -0xD2, 0xA, 0xB, 0x74,0x65, 0x78, 0x74, 0x2F, 0x70, 0x6C, -0x61, 0x69, 0x6E, 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x77, -0x6F, 0x72, 0x6C, 0x64}; - -uint8_t buf[128]; - -void loop() -{ - - nfc.write(message, sizeof(message)); - delay(3000); - - int16_t len = nfc.read(buf, sizeof(buf)); - if (len > 0) { - Serial.println("get a SNEP message:"); - for (uint8_t i = 0; i < len; i++) { - Serial.print(buf[i], HEX); - Serial.print(' '); - } - Serial.print('\n'); - for (uint8_t i = 0; i < len; i++) { - char c = buf[i]; - if (c <= 0x1f || c > 0x7f) { - Serial.print('.'); - } else { - Serial.print(c); - } - } - Serial.print('\n'); - } - delay(3000); -} -
diff -r db8030e71f55 -r b5922b3b3257 PN532/examples/p2p_with_ndef_library/p2p_with_ndef_library.ino --- a/PN532/examples/p2p_with_ndef_library/p2p_with_ndef_library.ino Tue Sep 13 06:01:19 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,55 +0,0 @@ -// send a NDEF message to adnroid or get a NDEF message -// -// note: [NDEF library](https://github.com/Don/NDEF) is needed. - -#include "SPI.h" -#include "PN532_SPI.h" -#include "snep.h" -#include "NdefMessage.h" - -PN532_SPI pn532spi(SPI, 10); -SNEP nfc(pn532spi); -uint8_t ndefBuf[128]; - -void setup() -{ - Serial.begin(115200); - Serial.println("-------Peer to Peer--------"); -} - -void loop() -{ -#if 1 - Serial.println("Send a message to Android"); - NdefMessage message = NdefMessage(); - message.addUriRecord("http://www.seeedstudio.com"); - int messageSize = message.getEncodedSize(); - if (messageSize > sizeof(ndefBuf)) { - Serial.println("ndefBuf is too small"); - while (1) { - } - - } - - message.encode(ndefBuf); - if (0 >= nfc.write(ndefBuf, messageSize)) { - Serial.println("Failed"); - } else { - Serial.println("Success"); - } - - delay(3000); -#else - Serial.println("Get a message from Android"); - int msgSize = nfc.read(ndefBuf, sizeof(ndefBuf)); - if (msgSize > 0) { - NdefMessage msg = NdefMessage(ndefBuf, msgSize); - msg.print(); - Serial.println("\nSuccess"); - } else { - Serial.println("failed"); - } - delay(3000); -#endif -} -
diff -r db8030e71f55 -r b5922b3b3257 PN532/examples/readMifare/readMifare.pde --- a/PN532/examples/readMifare/readMifare.pde Tue Sep 13 06:01:19 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,145 +0,0 @@ -/**************************************************************************/ -/*! - 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. - - To enable debug message, define DEBUG in PN532/PN532_debug.h -*/ -/**************************************************************************/ - -#include <SPI.h> -#include <PN532_SPI.h> -#include "PN532.h" - -PN532_SPI pn532spi(SPI, 10); -PN532 nfc(pn532spi); - -void setup(void) { - Serial.begin(115200); - 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); - - // configure board to read RFID tags - nfc.SAMConfig(); - - Serial.println("Waiting for an ISO14443A Card ..."); -} - - -void loop(void) { - 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) - - // 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 - Serial.println("Found an ISO14443A card"); - Serial.print(" UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes"); - Serial.print(" UID Value: "); - nfc.PrintHex(uid, uidLength); - Serial.println(""); - - if (uidLength == 4) - { - // We probably have a Mifare Classic card ... - Serial.println("Seems to be a Mifare Classic card (4 byte UID)"); - - // Now we need to try to authenticate it for read/write access - // Try with the factory default KeyA: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF - Serial.println("Trying to authenticate block 4 with default KEYA value"); - uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; - - // 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, 4, 0, keya); - - if (success) - { - Serial.println("Sector 1 (Blocks 4..7) has been authenticated"); - uint8_t data[16]; - - // If you want to write something to block 4 to test with, uncomment - // the following line and this text should be read back in a minute - // data = { 'a', 'd', 'a', 'f', 'r', 'u', 'i', 't', '.', 'c', 'o', 'm', 0, 0, 0, 0}; - // success = nfc.mifareclassic_WriteDataBlock (4, data); - - // Try to read the contents of block 4 - success = nfc.mifareclassic_ReadDataBlock(4, data); - - if (success) - { - // Data seems to have been read ... spit it out - Serial.println("Reading Block 4:"); - nfc.PrintHexChar(data, 16); - Serial.println(""); - - // Wait a bit before reading the card again - delay(1000); - } - else - { - Serial.println("Ooops ... unable to read the requested block. Try another key?"); - } - } - else - { - Serial.println("Ooops ... authentication failed: Try another key?"); - } - } - - if (uidLength == 7) - { - // We probably have a Mifare Ultralight card ... - Serial.println("Seems to be a Mifare Ultralight tag (7 byte UID)"); - - // Try to read the first general-purpose user page (#4) - Serial.println("Reading page 4"); - 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); - Serial.println(""); - - // Wait a bit before reading the card again - delay(1000); - } - else - { - Serial.println("Ooops ... unable to read the requested page!?"); - } - } - } -} -
diff -r db8030e71f55 -r b5922b3b3257 SPIClass.cpp --- a/SPIClass.cpp Tue Sep 13 06:01:19 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,124 +0,0 @@ -/* - * Copyright (c) 2010 by Cristian Maglie <c.maglie@bug.st> - * SPI Master library for arduino. - * - * This file is free software; you can redistribute it and/or modify - * it under the terms of either the GNU General Public License version 2 - * or the GNU Lesser General Public License version 2.1, both as - * published by the Free Software Foundation. - */ - -#include "arduino.h" -#include "SPIClass.h" - -SPIClass SPI; - -void SPIClass::begin() { - - // When the SS pin is set as OUTPUT, it can be used as - // a general purpose output port (it doesn't influence - // SPI operations). - pinMode(SS, OUTPUT); - - // Set SS to high so a connected chip will be "deselected" by default - digitalWrite(SS, HIGH); - - pinMode(SCK, ALTERNATE); - pinMode(MOSI, ALTERNATE); - pinMode(MISO, ALTERNATE); - - spi = SPI1; - RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); - - SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; - SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; - SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; - SPI_InitStructure.SPI_BaudRatePrescaler = SPI_CLOCK_DIV4; - SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; - SPI_InitStructure.SPI_Mode = SPI_Mode_Master; - SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; - SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; - SPI_InitStructure.SPI_CRCPolynomial = 7; - - SPI_Init(spi,&SPI_InitStructure); - SPI_RxFIFOThresholdConfig(spi,SPI_RxFIFOThreshold_QF); - SPI_Cmd(spi,ENABLE); -} - - -void SPIClass::end() { - SPI_Cmd(spi,DISABLE); -} - -void SPIClass::setBitOrder(uint8_t bitOrder) -{ - SPI_Cmd(spi,DISABLE); - if(bitOrder == LSBFIRST) - { - SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB; - } - else - { - SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; - } - SPI_Init(spi,&SPI_InitStructure); - SPI_Cmd(spi,ENABLE); -} - -void SPIClass::setDataMode(uint8_t mode) -{ - SPI_Cmd(spi,DISABLE); - if (mode == SPI_MODE0) - { - SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; - SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; - } - else if(mode == SPI_MODE1) - { - SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; - SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; - } - else if(mode == SPI_MODE2) - { - SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; - SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; - } - else if(mode == SPI_MODE3) - { - SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; - SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; - } - - SPI_Init(spi,&SPI_InitStructure); - SPI_Cmd(spi,ENABLE); -} - -void SPIClass::setClockDivider(uint8_t rate) -{ - SPI_Cmd(spi,DISABLE); - SPI_InitStructure.SPI_BaudRatePrescaler = rate; - - SPI_Init(spi,&SPI_InitStructure); - SPI_Cmd(spi,ENABLE); -} - -byte SPIClass::transfer(byte _data) { - uint8_t d = _data; - - while (SPI_I2S_GetFlagStatus(spi,SPI_I2S_FLAG_TXE) == RESET); - SPI_SendData8(spi,d); - - while (SPI_I2S_GetFlagStatus(spi,SPI_I2S_FLAG_RXNE) == RESET); - d = SPI_ReceiveData8(spi); - return d ; -} - -void SPIClass::attachInterrupt(void) { - // Should be enableInterrupt() -} - -void SPIClass::detachInterrupt(void) { - // Should be disableInterrupt() -} - -
diff -r db8030e71f55 -r b5922b3b3257 SPIClass.h --- a/SPIClass.h Tue Sep 13 06:01:19 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,58 +0,0 @@ -/* - * Copyright (c) 2010 by Cristian Maglie <c.maglie@bug.st> - * SPI Master library for arduino. - * - * This file is free software; you can redistribute it and/or modify - * it under the terms of either the GNU General Public License version 2 - * or the GNU Lesser General Public License version 2.1, both as - * published by the Free Software Foundation. - */ - -#ifndef _SPI_H_INCLUDED -#define _SPI_H_INCLUDED - -#include <stdio.h> -#include <Arduino.h> - - -#define SPI_CLOCK_DIV2 SPI_BaudRatePrescaler_2 -#define SPI_CLOCK_DIV4 SPI_BaudRatePrescaler_4 -#define SPI_CLOCK_DIV8 SPI_BaudRatePrescaler_8 -#define SPI_CLOCK_DIV16 SPI_BaudRatePrescaler_16 -#define SPI_CLOCK_DIV32 SPI_BaudRatePrescaler_32 -#define SPI_CLOCK_DIV64 SPI_BaudRatePrescaler_64 -#define SPI_CLOCK_DIV128 SPI_BaudRatePrescaler_128 -#define SPI_CLOCK_DIV256 SPI_BaudRatePrescaler_256 - -#define SPI_MODE0 0x00 -#define SPI_MODE1 0x04 -#define SPI_MODE2 0x08 -#define SPI_MODE3 0x0C - - -class SPIClass { -public: - uint8_t transfer(uint8_t _data); - - // SPI Configuration methods - - inline static void attachInterrupt(); - inline static void detachInterrupt(); // Default - - void begin(); // Default - void end(); - - void setBitOrder(uint8_t); - void setDataMode(uint8_t); - void setClockDivider(uint8_t); -private: - SPI_TypeDef *spi; - SPI_InitTypeDef SPI_InitStructure; - uint16_t bitOrder; -}; - -extern SPIClass SPI; - - - -#endif
diff -r db8030e71f55 -r b5922b3b3257 Wire.cpp --- a/Wire.cpp Tue Sep 13 06:01:19 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,391 +0,0 @@ -/* - * TwoWire.h - * Copyright (c) 2011 Cristian Maglie <c.maglie@bug.st>. - * All rights reserved. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - */ - -extern "C" { -#include <string.h> -} - -#include "Wire.h" - - - -#define EVENT_TIMEOUT 100 - - -TwoWire::TwoWire(I2C_TypeDef *_twi) : - twi(_twi), rxBufferIndex(0), rxBufferLength(0), txAddress(0), - txBufferLength(0), srvBufferIndex(0), srvBufferLength(0), status( - UNINITIALIZED){ - // Empty -} - -void TwoWire::begin(void) { - if (onBeginCallback) - onBeginCallback(); - - if (twi==I2C1) - { - RCC_I2CCLKConfig(RCC_I2C1CLK_HSI); - RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); - - pinMode(SDA, ALTERNATE); - pinMode(SCL, ALTERNATE); - } - else if (twi == I2C2) - { - RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE); - - pinMode(SDA1, ALTERNATE); - pinMode(SCL1, ALTERNATE); - } - - - I2C_DeInit(twi); - - I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; - I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable; - I2C_InitStructure.I2C_DigitalFilter = 0x00; - if (status == UNINITIALIZED) - { - I2C_InitStructure.I2C_OwnAddress1 = 0x00; - status = MASTER_IDLE; - } - I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; - I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; - I2C_InitStructure.I2C_Timing = 0x00E0D3FF; - I2C_Init(twi, &I2C_InitStructure); - - I2C_Cmd(twi, ENABLE); - - -} - -void TwoWire::begin(uint8_t address) { - if (onBeginCallback) - onBeginCallback(); - status = SLAVE_IDLE; - if (twi==I2C1) - { - RCC_I2CCLKConfig(RCC_I2C1CLK_HSI); - RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); - - pinMode(SDA, ALTERNATE); - pinMode(SCL, ALTERNATE); - } - - - I2C_DeInit(twi); - - I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; - I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable; - I2C_InitStructure.I2C_DigitalFilter = 0x00; - I2C_InitStructure.I2C_OwnAddress1 = address; - I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; - I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; - I2C_InitStructure.I2C_Timing = 0x00E0D3FF; - I2C_Init(twi, &I2C_InitStructure); - - NVIC_SetPriority(I2C1_IRQn, 0); - NVIC_EnableIRQ(I2C1_IRQn); - I2C_Cmd(twi, ENABLE); - I2C_ITConfig(twi, I2C_IT_ADDRI, ENABLE); -} - -void TwoWire::begin(int address) { - address <<= 1; - begin((uint8_t) address); -} - -uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop) { - address <<= 1; - if (quantity > BUFFER_LENGTH) - quantity = BUFFER_LENGTH; - uint32_t _millis; - - _millis = millis(); - while(I2C_GetFlagStatus(twi, I2C_FLAG_BUSY) != RESET) - { - if(EVENT_TIMEOUT < (millis() - _millis)) return 0; - } - - I2C_TransferHandling(twi, address, quantity, I2C_AutoEnd_Mode, I2C_Generate_Start_Read); - - - uint8_t *pBuffer = rxBuffer; - uint8_t numByteToRead = quantity; - uint8_t bytesRead = 0; - /* While there is data to be read */ - while(numByteToRead) - { - _millis = millis(); - while(I2C_GetFlagStatus(twi, I2C_FLAG_RXNE) == RESET) - { - if(EVENT_TIMEOUT < (millis() - _millis)) return 0; - } - /* Read a byte from the Slave */ - *pBuffer = I2C_ReceiveData(twi); - - bytesRead++; - - /* Point to the next location where the byte read will be saved */ - pBuffer++; - - /* Decrement the read bytes counter */ - numByteToRead--; - } - _millis = millis(); - while(I2C_GetFlagStatus(twi, I2C_FLAG_STOPF) == RESET) - { - if(EVENT_TIMEOUT < (millis() - _millis)) return 0; - } - // set rx buffer iterator vars - rxBufferIndex = 0; - rxBufferLength = bytesRead; - - return bytesRead; -} - -uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity) { - return requestFrom((uint8_t) address, (uint8_t) quantity, (uint8_t) true); -} - -uint8_t TwoWire::requestFrom(int address, int quantity) { - return requestFrom((uint8_t) address, (uint8_t) quantity, (uint8_t) true); -} - -uint8_t TwoWire::requestFrom(int address, int quantity, int sendStop) { - return requestFrom((uint8_t) address, (uint8_t) quantity, (uint8_t) sendStop); -} - -void TwoWire::beginTransmission(uint8_t address) { - status = MASTER_SEND; - - // save address of target and empty buffer - txAddress = address; - txBufferLength = 0; -} - -void TwoWire::beginTransmission(int address) { - address <<= 1; - beginTransmission((uint8_t) address); -} - -// -// Originally, 'endTransmission' was an f(void) function. -// It has been modified to take one parameter indicating -// whether or not a STOP should be performed on the bus. -// Calling endTransmission(false) allows a sketch to -// perform a repeated start. -// -// WARNING: Nothing in the library keeps track of whether -// the bus tenure has been properly ended with a STOP. It -// is very possible to leave the bus in a hung state if -// no call to endTransmission(true) is made. Some I2C -// devices will behave oddly if they do not see a STOP. -// -uint8_t TwoWire::endTransmission(uint8_t sendStop) { - uint32_t _millis; - - _millis = millis(); - while(I2C_GetFlagStatus(twi, I2C_FLAG_BUSY) != RESET) - { - if(EVENT_TIMEOUT < (millis() - _millis)) return 0; - } - if (sendStop == true) - { - I2C_TransferHandling(twi, txAddress, txBufferLength, I2C_AutoEnd_Mode, I2C_Generate_Start_Write); - } - else - { - I2C_TransferHandling(twi, txAddress, txBufferLength, I2C_SoftEnd_Mode, I2C_Generate_Start_Write); - } - uint8_t *pBuffer = txBuffer; - uint8_t NumByteToWrite = txBufferLength; - /* While there is data to be read */ - while(NumByteToWrite--) - { - _millis = millis(); - while(I2C_GetFlagStatus(twi, I2C_FLAG_TXIS) == RESET) - { - if(EVENT_TIMEOUT < (millis() - _millis)) return 0; - } - /* Send the current byte to slave */ - I2C_SendData(twi, *pBuffer++); - } - _millis = millis(); - if (sendStop == true) - { - while(I2C_GetFlagStatus(twi, I2C_FLAG_STOPF) == RESET) - { - if(EVENT_TIMEOUT < (millis() - _millis)) return 0; - } - } - else - { - while(I2C_GetFlagStatus(twi, I2C_FLAG_STOPF) == RESET) - { - if(EVENT_TIMEOUT < (millis() - _millis)) return 0; - } - } - // reset tx buffer iterator vars - txBufferLength = 0; - - status = MASTER_IDLE; - return 0; -} - -// This provides backwards compatibility with the original -// definition, and expected behaviour, of endTransmission -// -uint8_t TwoWire::endTransmission(void) -{ - return endTransmission(true); -} - -size_t TwoWire::write(uint8_t data) { - if (status == MASTER_SEND) { - if (txBufferLength >= BUFFER_LENGTH) - return 0; - txBuffer[txBufferLength++] = data; - return 1; - } else { - if (srvBufferLength >= BUFFER_LENGTH) - return 0; - srvBuffer[srvBufferLength++] = data; - return 1; - } -} - -size_t TwoWire::write(const uint8_t *data, size_t quantity) { - if (status == MASTER_SEND) { - for (size_t i = 0; i < quantity; ++i) { - if (txBufferLength >= BUFFER_LENGTH) - return i; - txBuffer[txBufferLength++] = data[i]; - } - } else { - for (size_t i = 0; i < quantity; ++i) { - if (srvBufferLength >= BUFFER_LENGTH) - return i; - srvBuffer[srvBufferLength++] = data[i]; - } - } - return quantity; -} - -int TwoWire::available(void) { - return rxBufferLength - rxBufferIndex; -} - -int TwoWire::read(void) { - if (rxBufferIndex < rxBufferLength) - return rxBuffer[rxBufferIndex++]; - return -1; -} - -int TwoWire::peek(void) { - if (rxBufferIndex < rxBufferLength) - return rxBuffer[rxBufferIndex]; - return -1; -} - -void TwoWire::flush(void) { - // Do nothing, use endTransmission(..) to force - // data transfer. -} - -void TwoWire::onReceive(void(*function)(int)) { - onReceiveCallback = function; -} - -void TwoWire::onRequest(void(*function)(void)) { - onRequestCallback = function; -} - -void TwoWire::onService(void) -{ - if (I2C_GetITStatus(twi, I2C_IT_ADDR) == SET) - { - I2C_ITConfig(twi, I2C_IT_RXI | I2C_IT_TXI | I2C_IT_STOPI, ENABLE); - srvBufferLength = 0; - srvBufferIndex = 0; - if (twi->ISR & (1 << 16)) { - status = SLAVE_SEND; - if (onRequestCallback) - onRequestCallback(); - } else { - status = SLAVE_RECV; - } - - I2C_ClearITPendingBit(twi, I2C_IT_ADDR); - } - if (I2C_GetITStatus(twi, I2C_IT_TXIS) == SET) - { - uint8_t c = 'x'; - if (srvBufferIndex < srvBufferLength) - c = srvBuffer[srvBufferIndex++]; - I2C_SendData(twi, c); - } - if (I2C_GetITStatus(twi, I2C_IT_RXNE) == SET) - { - if (srvBufferLength < BUFFER_LENGTH) - srvBuffer[srvBufferLength++] = I2C_ReceiveData(twi); - } - if (I2C_GetITStatus(twi, I2C_IT_STOPF) == SET) - { - if (status == SLAVE_RECV && onReceiveCallback) { - // Copy data into rxBuffer - // (allows to receive another packet while the - // user program reads actual data) - for (uint8_t i = 0; i < srvBufferLength; ++i) - rxBuffer[i] = srvBuffer[i]; - rxBufferIndex = 0; - rxBufferLength = srvBufferLength; - // Alert calling program - onReceiveCallback( rxBufferLength); - } - I2C_ITConfig(twi, I2C_IT_ADDRI, ENABLE); - I2C_ITConfig(twi, I2C_IT_RXI | I2C_IT_TXI | I2C_IT_STOPI, DISABLE); - I2C_ClearITPendingBit(twi, I2C_IT_STOPF); - } -} - - - - -TwoWire Wire = TwoWire(I2C1); -TwoWire Wire1 = TwoWire(I2C2); - -#ifdef __cplusplus -extern "C" { -#endif -void WIRE_ISR_HANDLER(void) { - Wire.onService(); -} - -void WIRE1_ISR_HANDLER(void) { - Wire1.onService(); -} -#ifdef __cplusplus -} -#endif - - -
diff -r db8030e71f55 -r b5922b3b3257 Wire.h --- a/Wire.h Tue Sep 13 06:01:19 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,115 +0,0 @@ -/* - * TwoWire.h - * Copyright (c) 2011 Cristian Maglie <c.maglie@bug.st>. - * All rights reserved. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#ifndef TwoWire_h -#define TwoWire_h - -// Include Atmel CMSIS driver -//#include <include/twi.h> - -#include "Stream.h" -#include "variant.h" - -#define BUFFER_LENGTH 32 - -class TwoWire : public Stream { -public: - TwoWire(I2C_TypeDef *twi); - void begin(); - void begin(uint8_t); - void begin(int); - void beginTransmission(uint8_t); - void beginTransmission(int); - uint8_t endTransmission(void); - uint8_t endTransmission(uint8_t); - uint8_t requestFrom(uint8_t, uint8_t); - uint8_t requestFrom(uint8_t, uint8_t, uint8_t); - uint8_t requestFrom(int, int); - uint8_t requestFrom(int, int, int); - virtual size_t write(uint8_t); - virtual size_t write(const uint8_t *, size_t); - virtual int available(void); - virtual int read(void); - virtual int peek(void); - virtual void flush(void); - void onReceive(void(*)(int)); - void onRequest(void(*)(void)); - - inline size_t write(unsigned long n) { return write((uint8_t)n); } - inline size_t write(long n) { return write((uint8_t)n); } - inline size_t write(unsigned int n) { return write((uint8_t)n); } - inline size_t write(int n) { return write((uint8_t)n); } - using Print::write; - - void onService(void); - -private: - // RX Buffer - uint8_t rxBuffer[BUFFER_LENGTH]; - uint8_t rxBufferIndex; - uint8_t rxBufferLength; - - // TX Buffer - uint8_t txAddress; - uint8_t txBuffer[BUFFER_LENGTH]; - uint8_t txBufferLength; - - // Service buffer - uint8_t srvBuffer[BUFFER_LENGTH]; - uint8_t srvBufferIndex; - uint8_t srvBufferLength; - - // Callback user functions - void (*onRequestCallback)(void); - void (*onReceiveCallback)(int); - - // Called before initialization - void (*onBeginCallback)(void); - - // TWI instance - I2C_TypeDef *twi; - I2C_InitTypeDef I2C_InitStructure; - - // TWI state - enum TwoWireStatus { - UNINITIALIZED, - MASTER_IDLE, - MASTER_SEND, - MASTER_RECV, - SLAVE_IDLE, - SLAVE_RECV, - SLAVE_SEND - }; - TwoWireStatus status; - - // TWI clock frequency - static const uint32_t TWI_CLOCK = 100000; - - // Timeouts ( - static const uint32_t RECV_TIMEOUT = 100000; - static const uint32_t XMIT_TIMEOUT = 100000; -}; - -extern TwoWire Wire; -extern TwoWire Wire1; - - -#endif -
diff -r db8030e71f55 -r b5922b3b3257 wiring_constants.h --- a/wiring_constants.h Tue Sep 13 06:01:19 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,106 +0,0 @@ -/* - Copyright (c) 2014 BestU. All right reserved. - Copyright (c) 2011 Arduino. All right reserved. - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - See the GNU Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -*/ - -#ifndef _WIRING_CONSTANTS_ -#define _WIRING_CONSTANTS_ - -#ifdef __cplusplus -extern "C"{ -#endif // __cplusplus - -#define HIGH 0x1 -#define LOW 0x0 - -#define INPUT 0x0 -#define OUTPUT 0x1 -#define ALTERNATE 0x2 -#define ANALOG 0x3 - -#define true 0x1 -#define false 0x0 - -#define PI 3.1415926535897932384626433832795 -#define HALF_PI 1.5707963267948966192313216916398 -#define TWO_PI 6.283185307179586476925286766559 -#define DEG_TO_RAD 0.017453292519943295769236907684886 -#define RAD_TO_DEG 57.295779513082320876798154814105 -#define EULER 2.718281828459045235360287471352 - -#define SERIAL 0x0 -#define DISPLAY 0x1 - -enum BitOrder { - LSBFIRST = 0, - MSBFIRST = 1 -}; - -// LOW 0 -// HIGH 1 -#define CHANGE 2 -#define FALLING 3 -#define RISING 4 - -#define DEFAULT 1 -#define EXTERNAL 0 - -// undefine stdlib's abs if encountered -#ifdef abs -#undef abs -#endif // abs - -#ifndef min -#define min(a,b) ((a)<(b)?(a):(b)) -#endif // min - -#ifndef max -#define max(a,b) ((a)>(b)?(a):(b)) -#endif // max - -#define abs(x) ((x)>0?(x):-(x)) -#define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt))) -#define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5)) -#define radians(deg) ((deg)*DEG_TO_RAD) -#define degrees(rad) ((rad)*RAD_TO_DEG) -#define sq(x) ((x)*(x)) - -#define interrupts() __enable_irq() -#define noInterrupts() __disable_irq() - -#define lowByte(w) ((uint8_t) ((w) & 0xff)) -#define highByte(w) ((uint8_t) ((w) >> 8)) - -#define bitRead(value, bit) (((value) >> (bit)) & 0x01) -#define bitSet(value, bit) ((value) |= (1UL << (bit))) -#define bitClear(value, bit) ((value) &= ~(1UL << (bit))) -#define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit)) - -typedef unsigned int word; - -#define bit(b) (1UL << (b)) - -// TODO: to be checked -typedef uint8_t boolean ; -typedef uint8_t byte ; - - -#ifdef __cplusplus -} // extern "C" -#endif // __cplusplus - -#endif /* _WIRING_CONSTANTS_ */