简体   繁体   中英

Arduino NFC PN532 library error

I am using this NFC Shield http://www.elecfreaks.com/wiki/index.php?title=RFID_/_NFC_Shield and by using the provided library I am not able to run the examples.
This is the example code.

#include <PN532.h>

#define SCK 13
#define MOSI 11
#define SS 10
#define MISO 12

PN532 nfc(SCK, MISO, MOSI, SS);

void setup(void) {
Serial.begin(9600);
Serial.println("Hello!");

nfc.begin();

uint32_t versiondata = nfc.getFirmwareVersion();
if (! versiondata) {
    Serial.print("Didn't find PN53x board");
    while (1); // halt
}
// Got ok data, print it out!
Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);
Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);
Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
Serial.print("Supports "); Serial.println(versiondata & 0xFF, HEX);

// configure board to read RFID tags and cards
nfc.SAMConfig();
}


void loop(void) {
uint32_t id;
// look for MiFare type cards
id = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A);

if (id != 0) {
    Serial.print("Read card #"); Serial.println(id);
}
}

and it gives this error http://i.stack.imgur.com/MNvl9.jpg

And these are the detailed errors

    In file included from readMifareTargetID.pde:1:
    C:\Users\saqib\Documents\Arduino\libraries\PN532_SPI/PN532.h:44: error: expected `)' before 'cs'
C:\Users\saqib\Documents\Arduino\libraries\PN532_SPI/PN532.h:48: error: 'boolean' does not name a type
C:\Users\saqib\Documents\Arduino\libraries\PN532_SPI/PN532.h:49: error: 'uint32_t' does not name a type
C:\Users\saqib\Documents\Arduino\libraries\PN532_SPI/PN532.h:50: error: 'uint32_t' does not name a type
C:\Users\saqib\Documents\Arduino\libraries\PN532_SPI/PN532.h:51: error: 'uint32_t' does not name a type
C:\Users\saqib\Documents\Arduino\libraries\PN532_SPI/PN532.h:57: error: 'uint32_t' does not name a type
C:\Users\saqib\Documents\Arduino\libraries\PN532_SPI/PN532.h:58: error: 'uint32_t' does not name a type
C:\Users\saqib\Documents\Arduino\libraries\PN532_SPI/PN532.h:60: error: 'boolean' does not name a type
C:\Users\saqib\Documents\Arduino\libraries\PN532_SPI/PN532.h:65: error: 'uint8_t' does not name a type
C:\Users\saqib\Documents\Arduino\libraries\PN532_SPI/PN532.h:67: error: 'boolean' does not name a type
C:\Users\saqib\Documents\Arduino\libraries\PN532_SPI/PN532.h:68: error: 'uint8_t' does not name a type
C:\Users\saqib\Documents\Arduino\libraries\PN532_SPI/PN532.h:69: error: 'uint8_t' has not been declared
C:\Users\saqib\Documents\Arduino\libraries\PN532_SPI/PN532.h:69: error: 'uint8_t' has not been declared
C:\Users\saqib\Documents\Arduino\libraries\PN532_SPI/PN532.h:70: error: 'uint8_t' has not been declared
C:\Users\saqib\Documents\Arduino\libraries\PN532_SPI/PN532.h:70: error: 'uint8_t' has not been declared
C:\Users\saqib\Documents\Arduino\libraries\PN532_SPI/PN532.h:71: error: 'uint8_t' has not been declared
C:\Users\saqib\Documents\Arduino\libraries\PN532_SPI/PN532.h:72: error: 'uint8_t' does not name a type
In file included from C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino/Arduino.h:213,
                 from readMifareTargetID.pde:8:
C:\Program Files (x86)\Arduino\hardware\arduino\variants\mega/pins_arduino.h:35: error: expected unqualified-id before numeric constant
C:\Program Files (x86)\Arduino\hardware\arduino\variants\mega/pins_arduino.h:36: error: expected unqualified-id before numeric constant
C:\Program Files (x86)\Arduino\hardware\arduino\variants\mega/pins_arduino.h:37: error: expected unqualified-id before numeric constant
C:\Program Files (x86)\Arduino\hardware\arduino\variants\mega/pins_arduino.h:38: error: expected unqualified-id before numeric constant
readMifareTargetID:8: error: no matching function for call to 'PN532::PN532(int, int, int, int)'
C:\Users\saqib\Documents\Arduino\libraries\PN532_SPI/PN532.h:42: note: candidates are: PN532::PN532()
C:\Users\saqib\Documents\Arduino\libraries\PN532_SPI/PN532.h:42: note:                 PN532::PN532(const PN532&)
readMifareTargetID.pde: In function 'void setup()':
readMifareTargetID:16: error: 'class PN532' has no member named 'getFirmwareVersion'
readMifareTargetID:28: error: 'class PN532' has no member named 'SAMConfig'
readMifareTargetID.pde: In function 'void loop()':
readMifareTargetID:35: error: 'class PN532' has no member named 'readPassiveTargetID'

This is the .h file with the library

 // PN532 library by adafruit/ladyada
// MIT license

// authenticateBlock, readMemoryBlock, writeMemoryBlock contributed
// by Seeed Technology Inc (www.seeedstudio.com)


#include <Arduino.h>

#define PN532_PREAMBLE 0x00
#define PN532_STARTCODE1 0x00
#define PN532_STARTCODE2 0xFF
#define PN532_POSTAMBLE 0x00

#define PN532_HOSTTOPN532 0xD4

#define PN532_FIRMWAREVERSION 0x02
#define PN532_GETGENERALSTATUS 0x04
#define PN532_SAMCONFIGURATION  0x14
#define PN532_INLISTPASSIVETARGET 0x4A
#define PN532_INDATAEXCHANGE 0x40
#define PN532_MIFARE_READ 0x30
#define PN532_MIFARE_WRITE 0xA0

#define PN532_AUTH_WITH_KEYA 0x60
#define PN532_AUTH_WITH_KEYB 0x61


#define PN532_WAKEUP 0x55

#define  PN532_SPI_STATREAD 0x02
#define  PN532_SPI_DATAWRITE 0x01
#define  PN532_SPI_DATAREAD 0x03
#define  PN532_SPI_READY 0x01

#define PN532_MIFARE_ISO14443A 0x0

#define KEY_A   1
#define KEY_B   2


class PN532{
public:
    PN532(uint8_t cs, uint8_t clk, uint8_t mosi, uint8_t miso);

    void begin(void);

    boolean SAMConfig(void);
    uint32_t getFirmwareVersion(void);
    uint32_t readPassiveTargetID(uint8_t cardbaudrate);
    uint32_t authenticateBlock( uint8_t cardnumber /*1 or 2*/,
                uint32_t cid /*Card NUID*/,
                uint8_t blockaddress /*0 to 63*/,
                uint8_t authtype /*Either KEY_A or KEY_B */,
                uint8_t * keys);

    uint32_t readMemoryBlock(uint8_t cardnumber /*1 or 2*/,uint8_t blockaddress /*0 to 63*/, uint8_t * block);
    uint32_t writeMemoryBlock(uint8_t cardnumber /*1 or 2*/,uint8_t blockaddress /*0 to 63*/, uint8_t * block);

    boolean sendCommandCheckAck(uint8_t *cmd, uint8_t cmdlen, uint16_t timeout = 1000);

    //

private:
    uint8_t _ss, _clk, _mosi, _miso;

    boolean spi_readack();
    uint8_t readspistatus(void);
    void readspidata(uint8_t* buff, uint8_t n);
    void spiwritecommand(uint8_t* cmd, uint8_t cmdlen);
    void spiwrite(uint8_t c);
    uint8_t spiread(void);
};

and this the .cpp file

// PN532 library by adafruit/ladyada
// MIT license

// authenticateBlock, readMemoryBlock, writeMemoryBlock contributed
// by Seeed Technology Inc (www.seeedstudio.com)

#include <Arduino.h>
#include "PN532.h"

//#define PN532DEBUG 1

byte pn532ack[] = {0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00};
byte pn532response_firmwarevers[] = {0x00, 0xFF, 0x06, 0xFA, 0xD5, 0x03};

#define PN532_PACKBUFFSIZ 64
byte pn532_packetbuffer[PN532_PACKBUFFSIZ];

PN532::PN532(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t ss) {
    _clk = clk;
    _miso = miso;
    _mosi = mosi;
    _ss = ss;

    pinMode(_ss, OUTPUT);
    pinMode(_clk, OUTPUT);
    pinMode(_mosi, OUTPUT);
    pinMode(_miso, INPUT);
}

void PN532::begin() {
    digitalWrite(_ss, LOW);

    delay(1000);

    // not exactly sure why but we have to send a dummy command to get synced up
    pn532_packetbuffer[0] = PN532_FIRMWAREVERSION;
    sendCommandCheckAck(pn532_packetbuffer, 1);

    // ignore response!
}

uint32_t PN532::getFirmwareVersion(void) {
    uint32_t response;

    pn532_packetbuffer[0] = PN532_FIRMWAREVERSION;

    if (! sendCommandCheckAck(pn532_packetbuffer, 1))
        return 0;

    // read data packet
    readspidata(pn532_packetbuffer, 12);
    // check some basic stuff
    if (0 != strncmp((char *)pn532_packetbuffer, (char *)pn532response_firmwarevers, 6)) {
        return 0;
    }

    response = pn532_packetbuffer[6];
    response <<= 8;
    response |= pn532_packetbuffer[7];
    response <<= 8;
    response |= pn532_packetbuffer[8];
    response <<= 8;
    response |= pn532_packetbuffer[9];

    return response;
}


// default timeout of one second
boolean PN532::sendCommandCheckAck(uint8_t *cmd, uint8_t cmdlen, uint16_t timeout) {
    uint16_t timer = 0;

    // write the command
    spiwritecommand(cmd, cmdlen);

    // Wait for chip to say its ready!
    while (readspistatus() != PN532_SPI_READY) {
        if (timeout != 0) {
            timer+=10;
            if (timer > timeout)
                return false;
        }
        delay(10);
    }

    // read acknowledgement
    if (!spi_readack()) {
        return false;
    }

    timer = 0;
    // Wait for chip to say its ready!
    while (readspistatus() != PN532_SPI_READY) {
        if (timeout != 0) {
            timer+=10;
            if (timer > timeout)
                return false;
        }
        delay(10);
    }

    return true; // ack'd command
}

boolean PN532::SAMConfig(void) {
    pn532_packetbuffer[0] = PN532_SAMCONFIGURATION;
    pn532_packetbuffer[1] = 0x01; // normal mode;
    pn532_packetbuffer[2] = 0x14; // timeout 50ms * 20 = 1 second
    pn532_packetbuffer[3] = 0x01; // use IRQ pin!

    if (! sendCommandCheckAck(pn532_packetbuffer, 4))
        return false;

    // read data packet
    readspidata(pn532_packetbuffer, 8);

    return  (pn532_packetbuffer[5] == 0x15);
}

uint32_t PN532::authenticateBlock(uint8_t cardnumber /*1 or 2*/,uint32_t cid /*Card NUID*/, uint8_t blockaddress /*0 to 63*/,uint8_t authtype/*Either KEY_A or KEY_B */, uint8_t * keys) {
    pn532_packetbuffer[0] = PN532_INDATAEXCHANGE;
    pn532_packetbuffer[1] = cardnumber;  // either card 1 or 2 (tested for card 1)
    if(authtype == KEY_A)
    {
        pn532_packetbuffer[2] = PN532_AUTH_WITH_KEYA;
    }
    else
    {
        pn532_packetbuffer[2] = PN532_AUTH_WITH_KEYB;
    }
    pn532_packetbuffer[3] = blockaddress; //This address can be 0-63 for MIFARE 1K card

    pn532_packetbuffer[4] = keys[0];
    pn532_packetbuffer[5] = keys[1];
    pn532_packetbuffer[6] = keys[2];
    pn532_packetbuffer[7] = keys[3];
    pn532_packetbuffer[8] = keys[4];
    pn532_packetbuffer[9] = keys[5];

    pn532_packetbuffer[10] = ((cid >> 24) & 0xFF);
    pn532_packetbuffer[11] = ((cid >> 16) & 0xFF);
    pn532_packetbuffer[12] = ((cid >> 8) & 0xFF);
    pn532_packetbuffer[13] = ((cid >> 0) & 0xFF);

    if (! sendCommandCheckAck(pn532_packetbuffer, 14))
        return false;

    // read data packet
    readspidata(pn532_packetbuffer, 2+6);

#ifdef PN532DEBUG
    for(int iter=0;iter<14;iter++)
    {
        Serial.print(pn532_packetbuffer[iter], HEX);
        Serial.print(" ");
    }
    Serial.println();
    // check some basic stuff

    Serial.println("AUTH");
    for(uint8_t i=0;i<2+6;i++)
    {
        Serial.print(pn532_packetbuffer[i], HEX); Serial.println(" ");
    }
#endif

    if((pn532_packetbuffer[6] == 0x41) && (pn532_packetbuffer[7] == 0x00))
    {
    return true;
    }
    else
    {
    return false;
    }

}

uint32_t PN532::readMemoryBlock(uint8_t cardnumber /*1 or 2*/,uint8_t blockaddress /*0 to 63*/, uint8_t * block) {
    pn532_packetbuffer[0] = PN532_INDATAEXCHANGE;
    pn532_packetbuffer[1] = cardnumber;  // either card 1 or 2 (tested for card 1)
    pn532_packetbuffer[2] = PN532_MIFARE_READ;
    pn532_packetbuffer[3] = blockaddress; //This address can be 0-63 for MIFARE 1K card

    if (! sendCommandCheckAck(pn532_packetbuffer, 4))
        return false;

    // read data packet
    readspidata(pn532_packetbuffer, 18+6);
    // check some basic stuff
#ifdef PN532DEBUG
    Serial.println("READ");
#endif
    for(uint8_t i=8;i<18+6;i++)
    {
        block[i-8] = pn532_packetbuffer[i];
#ifdef PN532DEBUG
        Serial.print(pn532_packetbuffer[i], HEX); Serial.print(" ");
#endif
    }
    if((pn532_packetbuffer[6] == 0x41) && (pn532_packetbuffer[7] == 0x00))
    {
    return true; //read successful
    }
    else
    {
    return false;
    }

}

//Do not write to Sector Trailer Block unless you know what you are doing.
uint32_t PN532::writeMemoryBlock(uint8_t cardnumber /*1 or 2*/,uint8_t blockaddress /*0 to 63*/, uint8_t * block) {
    pn532_packetbuffer[0] = PN532_INDATAEXCHANGE;
    pn532_packetbuffer[1] = cardnumber;  // either card 1 or 2 (tested for card 1)
    pn532_packetbuffer[2] = PN532_MIFARE_WRITE;
    pn532_packetbuffer[3] = blockaddress;

    for(uint8_t byte=0; byte <16; byte++)
    {
        pn532_packetbuffer[4+byte] = block[byte];
    }

    if (! sendCommandCheckAck(pn532_packetbuffer, 20))
        return false;
    // read data packet
    readspidata(pn532_packetbuffer, 2+6);

#ifdef PN532DEBUG
    // check some basic stuff
    Serial.println("WRITE");
    for(uint8_t i=0;i<2+6;i++)
    {
        Serial.print(pn532_packetbuffer[i], HEX); Serial.println(" ");
    }
#endif

    if((pn532_packetbuffer[6] == 0x41) && (pn532_packetbuffer[7] == 0x00))
    {
    return true; //write successful
    }
    else
    {
    return false;
    }
}

uint32_t PN532::readPassiveTargetID(uint8_t cardbaudrate) {
    uint32_t cid;

    pn532_packetbuffer[0] = PN532_INLISTPASSIVETARGET;
    pn532_packetbuffer[1] = 1;  // max 1 cards at once (we can set this to 2 later)
    pn532_packetbuffer[2] = cardbaudrate;

    if (! sendCommandCheckAck(pn532_packetbuffer, 3))
        return 0x0;  // no cards read

    // read data packet
    readspidata(pn532_packetbuffer, 20);
    // check some basic stuff

    Serial.print("Found "); Serial.print(pn532_packetbuffer[7], DEC); Serial.println(" tags");
    if (pn532_packetbuffer[7] != 1)
        return 0;

    uint16_t sens_res = pn532_packetbuffer[9];
    sens_res <<= 8;
    sens_res |= pn532_packetbuffer[10];
    Serial.print("Sens Response: 0x");  Serial.println(sens_res, HEX);
    Serial.print("Sel Response: 0x");  Serial.println(pn532_packetbuffer[11], HEX);
    cid = 0;
    for (uint8_t i=0; i< pn532_packetbuffer[12]; i++) {
        cid <<= 8;
        cid |= pn532_packetbuffer[13+i];
        Serial.print(" 0x"); Serial.print(pn532_packetbuffer[13+i], HEX);
    }

#ifdef PN532DEBUG
    Serial.println("TargetID");
    for(uint8_t i=0;i<20;i++)
    {
        Serial.print(pn532_packetbuffer[i], HEX); Serial.println(" ");
    }
#endif  
    return cid;
}


/************** high level SPI */


boolean PN532::spi_readack() {
    uint8_t ackbuff[6];

    readspidata(ackbuff, 6);

    return (0 == strncmp((char *)ackbuff, (char *)pn532ack, 6));
}

/************** mid level SPI */

uint8_t PN532::readspistatus(void) {
    digitalWrite(_ss, LOW);
    delay(2);
    spiwrite(PN532_SPI_STATREAD);
    // read byte
    uint8_t x = spiread();



    digitalWrite(_ss, HIGH);
    return x;
}

void PN532::readspidata(uint8_t* buff, uint8_t n) {
    digitalWrite(_ss, LOW);
    delay(2);
    spiwrite(PN532_SPI_DATAREAD);

#ifdef PN532DEBUG
    Serial.print("Reading: ");
#endif
    for (uint8_t i=0; i<n; i++) {
        delay(1);
        buff[i] = spiread();
#ifdef PN532DEBUG
        Serial.print(" 0x");
        Serial.print(buff[i], HEX);
#endif
    }

#ifdef PN532DEBUG
    Serial.println();
#endif

    digitalWrite(_ss, HIGH);
}

void PN532::spiwritecommand(uint8_t* cmd, uint8_t cmdlen) {
    uint8_t checksum;

    cmdlen++;

#ifdef PN532DEBUG
    Serial.print("\nSending: ");
#endif

    digitalWrite(_ss, LOW);
    delay(2);     // or whatever the delay is for waking up the board
    spiwrite(PN532_SPI_DATAWRITE);

    checksum = PN532_PREAMBLE + PN532_PREAMBLE + PN532_STARTCODE2;
    spiwrite(PN532_PREAMBLE);
    spiwrite(PN532_PREAMBLE);
    spiwrite(PN532_STARTCODE2);

    spiwrite(cmdlen);
    uint8_t cmdlen_1=~cmdlen + 1;
    spiwrite(cmdlen_1);

    spiwrite(PN532_HOSTTOPN532);
    checksum += PN532_HOSTTOPN532;

#ifdef PN532DEBUG
    Serial.print(" 0x"); Serial.print(PN532_PREAMBLE, HEX);
    Serial.print(" 0x"); Serial.print(PN532_PREAMBLE, HEX);
    Serial.print(" 0x"); Serial.print(PN532_STARTCODE2, HEX);
    Serial.print(" 0x"); Serial.print(cmdlen, HEX);
    Serial.print(" 0x"); Serial.print(cmdlen_1, HEX);
    Serial.print(" 0x"); Serial.print(PN532_HOSTTOPN532, HEX);
#endif

    for (uint8_t i=0; i<cmdlen-1; i++) {
        spiwrite(cmd[i]);
        checksum += cmd[i];
#ifdef PN532DEBUG
        Serial.print(" 0x"); Serial.print(cmd[i], HEX);
#endif
    }
    uint8_t checksum_1=~checksum;
    spiwrite(checksum_1);
    spiwrite(PN532_POSTAMBLE);
    digitalWrite(_ss, HIGH);

#ifdef PN532DEBUG
    Serial.print(" 0x"); Serial.print(checksum_1, HEX);
    Serial.print(" 0x"); Serial.print(PN532_POSTAMBLE, HEX);
    Serial.println();
#endif
} 
/************** low level SPI */

void PN532::spiwrite(uint8_t c) {
    int8_t i;
    digitalWrite(_clk, HIGH);

    for (i=0; i<8; i++) {
        digitalWrite(_clk, LOW);
        if (c & _BV(i)) {
            digitalWrite(_mosi, HIGH);
        } else {
            digitalWrite(_mosi, LOW);
        }
        digitalWrite(_clk, HIGH);
    }
}

uint8_t PN532::spiread(void) {
    int8_t i, x;
    x = 0;
    digitalWrite(_clk, HIGH);

    for (i=0; i<8; i++) {
        if (digitalRead(_miso)) {
            x |= _BV(i);
        }
        digitalWrite(_clk, LOW);
        digitalWrite(_clk, HIGH);
    }
    return x;
}

I am a beginner and any help will be greatly appreciated.Thanks

you're not including the stdlib and stdbool headers that give you respectively the uint8_t / uint32_t type and boolean type, please consider adding this at the top of your files:

#include <stdint.h>
#include <stdbool.h>

Dont exactly know how it happened but I did try some things that I would like to mention which may help somebody else with this problem. I deleted all my libraries of NFC(PN532) and added the original library using Arduino's own add library option. Other than that I just changed my board to Uno and then changed it back to 2560 mega and compiled the sketch and it works now.

  • Note:(FOR ANYBODY NEW WHO READS THIS) Anybody using Arduino 1.0 or greater for such libraries should edit the .h and .cpp files by removing WProgram.h with Arduino.h.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM