X-Git-Url: https://git.rohieb.name/hackover2013-badge-firmware.git/blobdiff_plain/9d18e10afb2439a6a9ba6978a799259746a837b7..10e466530d58c52705b957688f3c62f3b60f2bdf:/tools/examples/sensors/pn532/ISO14443A_ID/main.c diff --git a/tools/examples/sensors/pn532/ISO14443A_ID/main.c b/tools/examples/sensors/pn532/ISO14443A_ID/main.c index fb6066e..09dadc1 100644 --- a/tools/examples/sensors/pn532/ISO14443A_ID/main.c +++ b/tools/examples/sensors/pn532/ISO14443A_ID/main.c @@ -38,8 +38,8 @@ #include "projectconfig.h" #include "sysinit.h" -#include "drivers/sensors/pn532/pn532.h" -#include "drivers/sensors/pn532/pn532_drvr.h" +#include "drivers/rf/pn532/pn532.h" +#include "drivers/rf/pn532/pn532_bus.h" /**************************************************************************/ /*! @@ -55,7 +55,7 @@ int main (void) { #ifdef CFG_INTERFACE - #error "CFG_INTERFACE must be disabled in projectconfig.h for this demo" + //#error "CFG_INTERFACE must be disabled in projectconfig.h for this demo" #endif #if !defined CFG_PRINTF_USBCDC #error "CFG_PRINTF_USBCDC must be enabled in projectconfig.h for this demo" @@ -64,75 +64,125 @@ int main (void) // Configure cpu and mandatory peripherals systemInit(); - // Wait 5 second for someone to open the USB connection for printf + // Wait a bit for someone to open the USB connection for printf systickDelay(5000); // Initialise the PN532 pn532Init(); - byte_t response[256]; - size_t responseLen; pn532_error_t error; + byte_t response[64]; + size_t responseLen; - // Setup command to initialise a single ISO14443A target at 106kbps + // Setup command to initialise a single ISO14443A target at 106kbps (Mifare Classic, Ultralight, etc.) byte_t abtCommand[] = { PN532_COMMAND_INLISTPASSIVETARGET, 0x01, PN532_MODULATION_ISO14443A_106KBPS }; while (1) { printf("%s", CFG_PRINTF_NEWLINE); - printf("Wait for an ISO14443A card (Mifare Classic, etc.)%s", CFG_PRINTF_NEWLINE); + printf("Waiting for an ISO14443A card (Mifare Classic, etc.)%s", CFG_PRINTF_NEWLINE); - // Send the command + // Send the command (and handle the most common errors) error = pn532Write(abtCommand, sizeof(abtCommand)); - - // Wait until we get a response or an unexpected error message - do + if (error) { - error = pn532Read(response, &responseLen); - systickDelay(25); + // Something went wrong sending the command (probably the bus selection or wiring) + switch(error) + { + case (PN532_ERROR_NOACK): + case (PN532_ERROR_INVALIDACK): + // No ACK response frame received from the PN532 + printf("Ooops ... No valid ACK frame received!%s", CFG_PRINTF_NEWLINE); + break; + case (PN532_ERROR_I2C_NACK): + // No ACK bit received to I2C start ... not same as PN532 ACK frame (bus pins not set correctly?) + printf("Ooops ... No I2C ACK received! Are the bus select pins sets to I2C?%s", CFG_PRINTF_NEWLINE); + break; + case (PN532_ERROR_READYSTATUSTIMEOUT): + // Timed out waiting for the ready bit to clear ... this can be intentional, though, in the + // case of PN532_COMMAND_INLISTPASSIVETARGET since it will only clear when a card + // enters the magnetic field! Handle with caution because it isn't always an error + // Note: Only valid for I2C and SPI + printf("Timed out waiting for Ready/IRQ%s", CFG_PRINTF_NEWLINE); + break; + default: + printf("Ooops ... something went wrong! [PN532 Error Code: 0x%02X]%s", error, CFG_PRINTF_NEWLINE); + break; + } } - #ifdef PN532_UART - while (error == PN532_ERROR_RESPONSEBUFFEREMPTY); - #endif - #ifdef PN532_SPI - while ((error == PN532_ERROR_RESPONSEBUFFEREMPTY) || (error = PN532_ERROR_SPIREADYSTATUSTIMEOUT)); - #endif - - // Print the card details if possible - if (!error) + else { - /* Response for ISO14443A 106KBPS (Mifare Classic, etc.) - See UM0701-02 section 7.3.5 for more information - - byte Description - ------------- ------------------------------------------ - b7 Tags Found - b8 Tag Number (only one used in this example) - b9..10 SENS_RES - b11 SEL_RES - b12 NFCID Length - b13..NFCIDLen NFCID - - SENS_RES SEL_RES Manufacturer/Card Type NFCID Len - -------- ------- ----------------------- --------- - 00 04 08 NXP Mifare Classic 1K 4 bytes */ + // Commmand seems to have gone through ... + do + { + // Keep reading until we get a response or an unexpected error condition + error = pn532Read(response, &responseLen); + systickDelay(25); + } + while (error == PN532_ERROR_RESPONSEBUFFEREMPTY); printf("%s", CFG_PRINTF_NEWLINE); - printf("%-12s: %d %s", "Tags Found", response[7], CFG_PRINTF_NEWLINE); - printf("%-12s: %02X %02X %s", "SENS_RES", response[9], response[10], CFG_PRINTF_NEWLINE); - printf("%-12s: %02X %s", "SEL_RES", response[11], CFG_PRINTF_NEWLINE); - printf("%-12s: ", "NFCID"); - size_t pos; - for (pos=0; pos < response[12]; pos++) + printf("%-12s: ", "Received"); + pn532PrintHex(response, responseLen); + + // Try to handle some potential frame errors + // Unhandled errors are caught further down + switch (error) { - printf("%02x ", response[13 + pos]); + case (PN532_ERROR_PREAMBLEMISMATCH): + // Frame should start with 0x00 0x00 0xFF! + printf("Response frame doesn't start with expected preamble (00 00 FF)%s", CFG_PRINTF_NEWLINE); + break; + case (PN532_ERROR_APPLEVELERROR): + printf("Application level error reported by PN532%s", CFG_PRINTF_NEWLINE); + break; + case (PN532_ERROR_LENCHECKSUMMISMATCH): + printf("Frame length check/checksum mismatch%s", CFG_PRINTF_NEWLINE); + break; + default: + // Other errors handled below + break; + } + + // Print the card details if possible + if (!error) + { + /* Response for ISO14443A 106KBPS (Mifare Classic, etc.) + See UM0701-02 section 7.3.5 for more information + + byte Description + ------------- ------------------------------------------ + b7 Tags Found + b8 Tag Number (only one used in this example) + b9..10 SENS_RES + b11 SEL_RES + b12 NFCID Length + b13..NFCIDLen NFCID + + SENS_RES SEL_RES Manufacturer/Card Type NFCID Len + -------- ------- ----------------------- --------- + 00 04 08 NXP Mifare Classic 1K 4 bytes */ + + printf("%-12s: %d %s", "Tags Found", response[7], CFG_PRINTF_NEWLINE); + printf("%-12s: %02X %02X %s", "SENS_RES", response[9], response[10], CFG_PRINTF_NEWLINE); + printf("%-12s: %02X %s", "SEL_RES", response[11], CFG_PRINTF_NEWLINE); + printf("%-12s: ", "NFCID"); + size_t pos; + for (pos=0; pos < response[12]; pos++) + { + printf("%02x ", response[13 + pos]); + } + printf(CFG_PRINTF_NEWLINE); + if ((response[9] == 0x00) && (response[10] == 0x04) && (response[11] == 0x08)) + { + printf("Seems to be a Mifare Classic 1K Card%s", CFG_PRINTF_NEWLINE); + } + } + else + { + // Oops .... something bad happened. Check 'error' + printf("Ooops! Error %02X %s", error, CFG_PRINTF_NEWLINE); } - printf(CFG_PRINTF_NEWLINE); - } - else - { - // Oops .... something bad happened. Check 'error' - printf("Ooops! Error %02X %s", error, CFG_PRINTF_NEWLINE); } // Wait at least one second before trying again