Luke Beach
/
Nfctest2
Uses PN532 to get the UID of an nfc card and (try to) compare it with a default value
main.cpp@0:9d2afe43dcf1, 2018-10-20 (annotated)
- Committer:
- co657_lb580
- Date:
- Sat Oct 20 13:39:41 2018 +0000
- Revision:
- 0:9d2afe43dcf1
Initial commit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
co657_lb580 | 0:9d2afe43dcf1 | 1 | #include "PN532_I2C.h" |
co657_lb580 | 0:9d2afe43dcf1 | 2 | #include "PN532.h" |
co657_lb580 | 0:9d2afe43dcf1 | 3 | #include <inttypes.h> |
co657_lb580 | 0:9d2afe43dcf1 | 4 | #include "mbed.h" |
co657_lb580 | 0:9d2afe43dcf1 | 5 | |
co657_lb580 | 0:9d2afe43dcf1 | 6 | |
co657_lb580 | 0:9d2afe43dcf1 | 7 | DigitalOut ledBrd (LED1); // arducleo onboard led |
co657_lb580 | 0:9d2afe43dcf1 | 8 | |
co657_lb580 | 0:9d2afe43dcf1 | 9 | DigitalOut ledNFC (LED2); // status led |
co657_lb580 | 0:9d2afe43dcf1 | 10 | |
co657_lb580 | 0:9d2afe43dcf1 | 11 | DigitalOut rstNFC (D4); // pn532 chip reset control |
co657_lb580 | 0:9d2afe43dcf1 | 12 | |
co657_lb580 | 0:9d2afe43dcf1 | 13 | Serial pc(USBTX, USBRX); |
co657_lb580 | 0:9d2afe43dcf1 | 14 | |
co657_lb580 | 0:9d2afe43dcf1 | 15 | |
co657_lb580 | 0:9d2afe43dcf1 | 16 | // ----------------------------------------- I2C |
co657_lb580 | 0:9d2afe43dcf1 | 17 | I2C pn532_i2c (I2C_SDA, I2C_SCL); |
co657_lb580 | 0:9d2afe43dcf1 | 18 | PN532_I2C pn532_if (pn532_i2c); |
co657_lb580 | 0:9d2afe43dcf1 | 19 | |
co657_lb580 | 0:9d2afe43dcf1 | 20 | int acceptedCard = 1232695128; |
co657_lb580 | 0:9d2afe43dcf1 | 21 | |
co657_lb580 | 0:9d2afe43dcf1 | 22 | PN532 nfc(pn532_if); |
co657_lb580 | 0:9d2afe43dcf1 | 23 | |
co657_lb580 | 0:9d2afe43dcf1 | 24 | /*============================================================================== |
co657_lb580 | 0:9d2afe43dcf1 | 25 | * \brief reset the pn532 chip |
co657_lb580 | 0:9d2afe43dcf1 | 26 | */ |
co657_lb580 | 0:9d2afe43dcf1 | 27 | void reset_chip (void) |
co657_lb580 | 0:9d2afe43dcf1 | 28 | { |
co657_lb580 | 0:9d2afe43dcf1 | 29 | rstNFC = 0; |
co657_lb580 | 0:9d2afe43dcf1 | 30 | wait_ms (100); |
co657_lb580 | 0:9d2afe43dcf1 | 31 | rstNFC = 1; |
co657_lb580 | 0:9d2afe43dcf1 | 32 | } |
co657_lb580 | 0:9d2afe43dcf1 | 33 | |
co657_lb580 | 0:9d2afe43dcf1 | 34 | |
co657_lb580 | 0:9d2afe43dcf1 | 35 | /*============================================================================== |
co657_lb580 | 0:9d2afe43dcf1 | 36 | * \brief init the peripheral |
co657_lb580 | 0:9d2afe43dcf1 | 37 | */ |
co657_lb580 | 0:9d2afe43dcf1 | 38 | void setup(void) |
co657_lb580 | 0:9d2afe43dcf1 | 39 | { |
co657_lb580 | 0:9d2afe43dcf1 | 40 | ledBrd = 0; |
co657_lb580 | 0:9d2afe43dcf1 | 41 | ledNFC = 0; |
co657_lb580 | 0:9d2afe43dcf1 | 42 | reset_chip (); |
co657_lb580 | 0:9d2afe43dcf1 | 43 | |
co657_lb580 | 0:9d2afe43dcf1 | 44 | uint32_t versiondata = 0; |
co657_lb580 | 0:9d2afe43dcf1 | 45 | pc.baud(115200); |
co657_lb580 | 0:9d2afe43dcf1 | 46 | pc.printf ("Hello!\n"); |
co657_lb580 | 0:9d2afe43dcf1 | 47 | |
co657_lb580 | 0:9d2afe43dcf1 | 48 | while (1) { |
co657_lb580 | 0:9d2afe43dcf1 | 49 | nfc.begin(); |
co657_lb580 | 0:9d2afe43dcf1 | 50 | nfc.SAMConfig(); |
co657_lb580 | 0:9d2afe43dcf1 | 51 | versiondata = nfc.getFirmwareVersion(); |
co657_lb580 | 0:9d2afe43dcf1 | 52 | if (! versiondata) { |
co657_lb580 | 0:9d2afe43dcf1 | 53 | pc.printf("Didn't find PN53x board\n\n"); |
co657_lb580 | 0:9d2afe43dcf1 | 54 | wait_ms(500); |
co657_lb580 | 0:9d2afe43dcf1 | 55 | } else { |
co657_lb580 | 0:9d2afe43dcf1 | 56 | break; |
co657_lb580 | 0:9d2afe43dcf1 | 57 | } |
co657_lb580 | 0:9d2afe43dcf1 | 58 | } |
co657_lb580 | 0:9d2afe43dcf1 | 59 | |
co657_lb580 | 0:9d2afe43dcf1 | 60 | // Got ok data, print it out! |
co657_lb580 | 0:9d2afe43dcf1 | 61 | pc.printf ("Found chip PN5%02X , Firmware ver. %d.%d\n", |
co657_lb580 | 0:9d2afe43dcf1 | 62 | (versiondata>>24) & 0xFF, |
co657_lb580 | 0:9d2afe43dcf1 | 63 | (versiondata>>16) & 0xFF, |
co657_lb580 | 0:9d2afe43dcf1 | 64 | (versiondata>>8) & 0xFF); |
co657_lb580 | 0:9d2afe43dcf1 | 65 | |
co657_lb580 | 0:9d2afe43dcf1 | 66 | // Set the max number of retry attempts to read from a card |
co657_lb580 | 0:9d2afe43dcf1 | 67 | // This prevents us from waiting forever for a card, which is |
co657_lb580 | 0:9d2afe43dcf1 | 68 | // the default behaviour of the PN532. |
co657_lb580 | 0:9d2afe43dcf1 | 69 | nfc.setPassiveActivationRetries(0xFF); |
co657_lb580 | 0:9d2afe43dcf1 | 70 | |
co657_lb580 | 0:9d2afe43dcf1 | 71 | // configure board to read RFID tags |
co657_lb580 | 0:9d2afe43dcf1 | 72 | nfc.SAMConfig(); |
co657_lb580 | 0:9d2afe43dcf1 | 73 | |
co657_lb580 | 0:9d2afe43dcf1 | 74 | pc.printf ("\nWaiting for an ISO14443A card\n"); |
co657_lb580 | 0:9d2afe43dcf1 | 75 | } |
co657_lb580 | 0:9d2afe43dcf1 | 76 | |
co657_lb580 | 0:9d2afe43dcf1 | 77 | |
co657_lb580 | 0:9d2afe43dcf1 | 78 | /*============================================================================== |
co657_lb580 | 0:9d2afe43dcf1 | 79 | * \brief find a tag |
co657_lb580 | 0:9d2afe43dcf1 | 80 | */ |
co657_lb580 | 0:9d2afe43dcf1 | 81 | void loop(void) |
co657_lb580 | 0:9d2afe43dcf1 | 82 | { |
co657_lb580 | 0:9d2afe43dcf1 | 83 | bool success; |
co657_lb580 | 0:9d2afe43dcf1 | 84 | uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID |
co657_lb580 | 0:9d2afe43dcf1 | 85 | uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type) |
co657_lb580 | 0:9d2afe43dcf1 | 86 | |
co657_lb580 | 0:9d2afe43dcf1 | 87 | // configure board to read RFID tags |
co657_lb580 | 0:9d2afe43dcf1 | 88 | nfc.SAMConfig(); |
co657_lb580 | 0:9d2afe43dcf1 | 89 | |
co657_lb580 | 0:9d2afe43dcf1 | 90 | // Wait for an ISO14443A type cards (Mifare, etc.). When one is found |
co657_lb580 | 0:9d2afe43dcf1 | 91 | // 'uid' will be populated with the UID, and uidLength will indicate |
co657_lb580 | 0:9d2afe43dcf1 | 92 | // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight) |
co657_lb580 | 0:9d2afe43dcf1 | 93 | success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength); |
co657_lb580 | 0:9d2afe43dcf1 | 94 | |
co657_lb580 | 0:9d2afe43dcf1 | 95 | printf ("\n"); |
co657_lb580 | 0:9d2afe43dcf1 | 96 | |
co657_lb580 | 0:9d2afe43dcf1 | 97 | if (success) { |
co657_lb580 | 0:9d2afe43dcf1 | 98 | ledNFC = 1; // led on |
co657_lb580 | 0:9d2afe43dcf1 | 99 | |
co657_lb580 | 0:9d2afe43dcf1 | 100 | pc.printf("Found a card!\n"); |
co657_lb580 | 0:9d2afe43dcf1 | 101 | |
co657_lb580 | 0:9d2afe43dcf1 | 102 | pc.printf("UID Length: %d bytes\n", uidLength); |
co657_lb580 | 0:9d2afe43dcf1 | 103 | pc.printf("UID Value: "); |
co657_lb580 | 0:9d2afe43dcf1 | 104 | //int id = 0; //create a variable to build the user id into |
co657_lb580 | 0:9d2afe43dcf1 | 105 | for (uint8_t i=0; i < uidLength; i++) |
co657_lb580 | 0:9d2afe43dcf1 | 106 | { |
co657_lb580 | 0:9d2afe43dcf1 | 107 | pc.printf("%d",uid[i]); |
co657_lb580 | 0:9d2afe43dcf1 | 108 | } |
co657_lb580 | 0:9d2afe43dcf1 | 109 | pc.printf("\r\n"); |
co657_lb580 | 0:9d2afe43dcf1 | 110 | |
co657_lb580 | 0:9d2afe43dcf1 | 111 | |
co657_lb580 | 0:9d2afe43dcf1 | 112 | /* COMPUTER SCIENCE GROUP CHAT OPTION |
co657_lb580 | 0:9d2afe43dcf1 | 113 | int result = 0; |
co657_lb580 | 0:9d2afe43dcf1 | 114 | for (int i = 0; i < uidLength; i++) { |
co657_lb580 | 0:9d2afe43dcf1 | 115 | result = result + (uid[i] * pow(10.0, (uidLength-i)-1)); |
co657_lb580 | 0:9d2afe43dcf1 | 116 | } |
co657_lb580 | 0:9d2afe43dcf1 | 117 | pc.printf("Result: %d \r\n",result); |
co657_lb580 | 0:9d2afe43dcf1 | 118 | */ |
co657_lb580 | 0:9d2afe43dcf1 | 119 | |
co657_lb580 | 0:9d2afe43dcf1 | 120 | // DAVID BARNES OPTION |
co657_lb580 | 0:9d2afe43dcf1 | 121 | unsigned long sum = 0; |
co657_lb580 | 0:9d2afe43dcf1 | 122 | for(int i = 0; i < sizeof(uid) / sizeof(*uid); i++) { |
co657_lb580 | 0:9d2afe43dcf1 | 123 | sum = (sum * 10) + uid[i]; |
co657_lb580 | 0:9d2afe43dcf1 | 124 | } |
co657_lb580 | 0:9d2afe43dcf1 | 125 | printf("Sum: %lu", sum); |
co657_lb580 | 0:9d2afe43dcf1 | 126 | |
co657_lb580 | 0:9d2afe43dcf1 | 127 | /* ONLINE FORUM OPTION |
co657_lb580 | 0:9d2afe43dcf1 | 128 | uint32_t cardidentifier = 0; |
co657_lb580 | 0:9d2afe43dcf1 | 129 | cardidentifier = uid[0]; |
co657_lb580 | 0:9d2afe43dcf1 | 130 | cardidentifier >>= 8; cardidentifier |= uid[1]; |
co657_lb580 | 0:9d2afe43dcf1 | 131 | cardidentifier >>= 8; cardidentifier |= uid[2]; |
co657_lb580 | 0:9d2afe43dcf1 | 132 | cardidentifier >>= 8; cardidentifier |= uid[3]; |
co657_lb580 | 0:9d2afe43dcf1 | 133 | pc.printf("My number: %u \r\n",cardidentifier); |
co657_lb580 | 0:9d2afe43dcf1 | 134 | */ |
co657_lb580 | 0:9d2afe43dcf1 | 135 | pc.printf("Correct number %u \r\n", acceptedCard); |
co657_lb580 | 0:9d2afe43dcf1 | 136 | |
co657_lb580 | 0:9d2afe43dcf1 | 137 | wait_ms (100); |
co657_lb580 | 0:9d2afe43dcf1 | 138 | ledNFC = 0; //turn off red and turn on green |
co657_lb580 | 0:9d2afe43dcf1 | 139 | //green led to show it is the right card |
co657_lb580 | 0:9d2afe43dcf1 | 140 | // wait until the card is taken away |
co657_lb580 | 0:9d2afe43dcf1 | 141 | while (nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength, 500)) {} |
co657_lb580 | 0:9d2afe43dcf1 | 142 | |
co657_lb580 | 0:9d2afe43dcf1 | 143 | ledNFC = 0; // led off |
co657_lb580 | 0:9d2afe43dcf1 | 144 | } else { |
co657_lb580 | 0:9d2afe43dcf1 | 145 | // PN532 probably timed out waiting for a card |
co657_lb580 | 0:9d2afe43dcf1 | 146 | pc.printf("\nTimed out waiting for a card\n"); |
co657_lb580 | 0:9d2afe43dcf1 | 147 | ledNFC = 0; |
co657_lb580 | 0:9d2afe43dcf1 | 148 | wait_ms (200); |
co657_lb580 | 0:9d2afe43dcf1 | 149 | } |
co657_lb580 | 0:9d2afe43dcf1 | 150 | } |
co657_lb580 | 0:9d2afe43dcf1 | 151 | |
co657_lb580 | 0:9d2afe43dcf1 | 152 | |
co657_lb580 | 0:9d2afe43dcf1 | 153 | /*============================================================================== |
co657_lb580 | 0:9d2afe43dcf1 | 154 | * \brief main entry |
co657_lb580 | 0:9d2afe43dcf1 | 155 | */ |
co657_lb580 | 0:9d2afe43dcf1 | 156 | int main() |
co657_lb580 | 0:9d2afe43dcf1 | 157 | { |
co657_lb580 | 0:9d2afe43dcf1 | 158 | setup(); |
co657_lb580 | 0:9d2afe43dcf1 | 159 | |
co657_lb580 | 0:9d2afe43dcf1 | 160 | while (1) |
co657_lb580 | 0:9d2afe43dcf1 | 161 | loop (); |
co657_lb580 | 0:9d2afe43dcf1 | 162 | } |