LibPN532 for mbed sample - Read Tag Uid

Dependencies:   LibPN532 mbed

Fork of PN532_ReadUid by dotnfc Tang

Committer:
dotnfc
Date:
Tue Sep 13 06:18:59 2016 +0000
Revision:
0:54a12c4b19c2
Child:
1:18f5ba9fb718
LibPN532 sample - Read ISO14443A UID

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dotnfc 0:54a12c4b19c2 1 // Basic example show how to read a tag uid.
dotnfc 0:54a12c4b19c2 2 // you can use i2c, spi, hsu interface.
dotnfc 0:54a12c4b19c2 3 //
dotnfc 0:54a12c4b19c2 4 // Assembled by dotnfc as Arducleo Sample
dotnfc 0:54a12c4b19c2 5 // 2016/09/10
dotnfc 0:54a12c4b19c2 6
dotnfc 0:54a12c4b19c2 7
dotnfc 0:54a12c4b19c2 8
dotnfc 0:54a12c4b19c2 9 #include "mbed.h"
dotnfc 0:54a12c4b19c2 10 #include "PN532.h"
dotnfc 0:54a12c4b19c2 11 #include "PN532_HSU.h"
dotnfc 0:54a12c4b19c2 12 #include "PN532_SPI.h"
dotnfc 0:54a12c4b19c2 13 #include "PN532_I2C.h"
dotnfc 0:54a12c4b19c2 14
dotnfc 0:54a12c4b19c2 15 DigitalOut ledBrd (LED1); // arducleo onboard led
dotnfc 0:54a12c4b19c2 16
dotnfc 0:54a12c4b19c2 17 DigitalOut ledNFC (D9); // status led
dotnfc 0:54a12c4b19c2 18
dotnfc 0:54a12c4b19c2 19 DigitalOut rstNFC (D4); // pn532 chip reset control
dotnfc 0:54a12c4b19c2 20
dotnfc 0:54a12c4b19c2 21 PwmOut buz(PB_1);
dotnfc 0:54a12c4b19c2 22
dotnfc 0:54a12c4b19c2 23 Serial pc(SERIAL_TX, SERIAL_RX);
dotnfc 0:54a12c4b19c2 24
dotnfc 0:54a12c4b19c2 25 // ----------------------------------------- HSU
dotnfc 0:54a12c4b19c2 26 //HardwareSerial pn532_hsu (PC_10, PC_11);
dotnfc 0:54a12c4b19c2 27 //PN532_HSU pn532_if (pn532_hsu);
dotnfc 0:54a12c4b19c2 28
dotnfc 0:54a12c4b19c2 29 // ----------------------------------------- SPI
dotnfc 0:54a12c4b19c2 30 SPI pn532_spi (SPI_MOSI, SPI_MISO, SPI_SCK);
dotnfc 0:54a12c4b19c2 31 PN532_SPI pn532_if (pn532_spi, SPI_CS);
dotnfc 0:54a12c4b19c2 32
dotnfc 0:54a12c4b19c2 33 // ----------------------------------------- I2C
dotnfc 0:54a12c4b19c2 34 //I2C pn532_i2c (I2C_SDA, I2C_SCL);
dotnfc 0:54a12c4b19c2 35 //PN532_I2C pn532_if (pn532_i2c);
dotnfc 0:54a12c4b19c2 36
dotnfc 0:54a12c4b19c2 37
dotnfc 0:54a12c4b19c2 38
dotnfc 0:54a12c4b19c2 39 PN532 nfc(pn532_if);
dotnfc 0:54a12c4b19c2 40
dotnfc 0:54a12c4b19c2 41 /*==============================================================================
dotnfc 0:54a12c4b19c2 42 * \brief reset the pn532 chip
dotnfc 0:54a12c4b19c2 43 */
dotnfc 0:54a12c4b19c2 44 void reset_chip (void)
dotnfc 0:54a12c4b19c2 45 {
dotnfc 0:54a12c4b19c2 46 rstNFC = 0;
dotnfc 0:54a12c4b19c2 47 wait_ms (100);
dotnfc 0:54a12c4b19c2 48 rstNFC = 1;
dotnfc 0:54a12c4b19c2 49 }
dotnfc 0:54a12c4b19c2 50
dotnfc 0:54a12c4b19c2 51
dotnfc 0:54a12c4b19c2 52 /*==============================================================================
dotnfc 0:54a12c4b19c2 53 * \brief init the peripheral
dotnfc 0:54a12c4b19c2 54 */
dotnfc 0:54a12c4b19c2 55 void setup(void)
dotnfc 0:54a12c4b19c2 56 {
dotnfc 0:54a12c4b19c2 57 ledBrd = 0;
dotnfc 0:54a12c4b19c2 58 ledNFC = 0;
dotnfc 0:54a12c4b19c2 59 reset_chip ();
dotnfc 0:54a12c4b19c2 60
dotnfc 0:54a12c4b19c2 61 uint32_t versiondata = 0;
dotnfc 0:54a12c4b19c2 62 pc.baud(115200);
dotnfc 0:54a12c4b19c2 63 pc.printf ("Hello!\n");
dotnfc 0:54a12c4b19c2 64
dotnfc 0:54a12c4b19c2 65 while (1) {
dotnfc 0:54a12c4b19c2 66 nfc.begin();
dotnfc 0:54a12c4b19c2 67 //nfc.SAMConfig();
dotnfc 0:54a12c4b19c2 68 versiondata = nfc.getFirmwareVersion();
dotnfc 0:54a12c4b19c2 69 if (! versiondata) {
dotnfc 0:54a12c4b19c2 70 pc.printf("Didn't find PN53x board\n\n");
dotnfc 0:54a12c4b19c2 71 wait_ms(500);
dotnfc 0:54a12c4b19c2 72 } else {
dotnfc 0:54a12c4b19c2 73 break;
dotnfc 0:54a12c4b19c2 74 }
dotnfc 0:54a12c4b19c2 75 }
dotnfc 0:54a12c4b19c2 76
dotnfc 0:54a12c4b19c2 77 // Got ok data, print it out!
dotnfc 0:54a12c4b19c2 78 pc.printf ("Found chip PN5%02X , Firmware ver. %d.%d\n",
dotnfc 0:54a12c4b19c2 79 (versiondata>>24) & 0xFF,
dotnfc 0:54a12c4b19c2 80 (versiondata>>16) & 0xFF,
dotnfc 0:54a12c4b19c2 81 (versiondata>>8) & 0xFF);
dotnfc 0:54a12c4b19c2 82
dotnfc 0:54a12c4b19c2 83 // Set the max number of retry attempts to read from a card
dotnfc 0:54a12c4b19c2 84 // This prevents us from waiting forever for a card, which is
dotnfc 0:54a12c4b19c2 85 // the default behaviour of the PN532.
dotnfc 0:54a12c4b19c2 86 nfc.setPassiveActivationRetries(0xFF);
dotnfc 0:54a12c4b19c2 87
dotnfc 0:54a12c4b19c2 88 // configure board to read RFID tags
dotnfc 0:54a12c4b19c2 89 nfc.SAMConfig();
dotnfc 0:54a12c4b19c2 90
dotnfc 0:54a12c4b19c2 91 pc.printf ("\nWaiting for an ISO14443A card\n");
dotnfc 0:54a12c4b19c2 92 }
dotnfc 0:54a12c4b19c2 93
dotnfc 0:54a12c4b19c2 94
dotnfc 0:54a12c4b19c2 95 /*==============================================================================
dotnfc 0:54a12c4b19c2 96 * \brief find a tag
dotnfc 0:54a12c4b19c2 97 */
dotnfc 0:54a12c4b19c2 98 void loop(void)
dotnfc 0:54a12c4b19c2 99 {
dotnfc 0:54a12c4b19c2 100 bool success;
dotnfc 0:54a12c4b19c2 101 uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
dotnfc 0:54a12c4b19c2 102 uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
dotnfc 0:54a12c4b19c2 103
dotnfc 0:54a12c4b19c2 104 // configure board to read RFID tags
dotnfc 0:54a12c4b19c2 105 nfc.SAMConfig();
dotnfc 0:54a12c4b19c2 106
dotnfc 0:54a12c4b19c2 107 // Wait for an ISO14443A type cards (Mifare, etc.). When one is found
dotnfc 0:54a12c4b19c2 108 // 'uid' will be populated with the UID, and uidLength will indicate
dotnfc 0:54a12c4b19c2 109 // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
dotnfc 0:54a12c4b19c2 110 success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength);
dotnfc 0:54a12c4b19c2 111
dotnfc 0:54a12c4b19c2 112 printf ("\n");
dotnfc 0:54a12c4b19c2 113
dotnfc 0:54a12c4b19c2 114 if (success) {
dotnfc 0:54a12c4b19c2 115 tone (buz, 800); // turn on the buzzer
dotnfc 0:54a12c4b19c2 116 ledNFC = 1; // led on
dotnfc 0:54a12c4b19c2 117
dotnfc 0:54a12c4b19c2 118 pc.printf("Found a card!\n");
dotnfc 0:54a12c4b19c2 119
dotnfc 0:54a12c4b19c2 120 pc.printf("UID Length: %d bytes\n", uidLength);
dotnfc 0:54a12c4b19c2 121 pc.printf("UID Value: ");
dotnfc 0:54a12c4b19c2 122
dotnfc 0:54a12c4b19c2 123 for (uint8_t i=0; i < uidLength; i++)
dotnfc 0:54a12c4b19c2 124 pc.printf(" 0x%02X", uid[i]);
dotnfc 0:54a12c4b19c2 125
dotnfc 0:54a12c4b19c2 126 pc.printf("\n");
dotnfc 0:54a12c4b19c2 127
dotnfc 0:54a12c4b19c2 128 wait_ms (100);
dotnfc 0:54a12c4b19c2 129 tone (buz, 0); // turn off the buzzer
dotnfc 0:54a12c4b19c2 130
dotnfc 0:54a12c4b19c2 131 // wait until the card is taken away
dotnfc 0:54a12c4b19c2 132 while (nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength, 500)) {}
dotnfc 0:54a12c4b19c2 133
dotnfc 0:54a12c4b19c2 134 ledNFC = 0; // led off
dotnfc 0:54a12c4b19c2 135 } else {
dotnfc 0:54a12c4b19c2 136 // PN532 probably timed out waiting for a card
dotnfc 0:54a12c4b19c2 137 pc.printf("\nTimed out waiting for a card\n");
dotnfc 0:54a12c4b19c2 138 ledNFC = 0;
dotnfc 0:54a12c4b19c2 139 wait_ms (200);
dotnfc 0:54a12c4b19c2 140 }
dotnfc 0:54a12c4b19c2 141 }
dotnfc 0:54a12c4b19c2 142
dotnfc 0:54a12c4b19c2 143
dotnfc 0:54a12c4b19c2 144 /*==============================================================================
dotnfc 0:54a12c4b19c2 145 * \brief main entry
dotnfc 0:54a12c4b19c2 146 */
dotnfc 0:54a12c4b19c2 147 int main()
dotnfc 0:54a12c4b19c2 148 {
dotnfc 0:54a12c4b19c2 149 setup();
dotnfc 0:54a12c4b19c2 150
dotnfc 0:54a12c4b19c2 151 while (1)
dotnfc 0:54a12c4b19c2 152 loop ();
dotnfc 0:54a12c4b19c2 153 }
dotnfc 0:54a12c4b19c2 154