Use Seeeduino Arch (or Arch Pro) + NFC Shield to write a Mifare Classic tag
Dependencies: PN532 USBDevice mbed
main.cpp@0:b81d113f3a83, 2013-10-08 (annotated)
- Committer:
- yihui
- Date:
- Tue Oct 08 08:58:32 2013 +0000
- Revision:
- 0:b81d113f3a83
- Child:
- 1:c5b5be0d6c5d
Inital, read an ISO14443A card's UID
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yihui | 0:b81d113f3a83 | 1 | #include "mbed.h" |
yihui | 0:b81d113f3a83 | 2 | #include "PN532.h" |
yihui | 0:b81d113f3a83 | 3 | #include "USBSerial.h" |
yihui | 0:b81d113f3a83 | 4 | |
yihui | 0:b81d113f3a83 | 5 | // PN532(mosi, miso, clk, cs) |
yihui | 0:b81d113f3a83 | 6 | PN532 nfc(P1_22, P1_21, P1_20, P0_2); |
yihui | 0:b81d113f3a83 | 7 | |
yihui | 0:b81d113f3a83 | 8 | DigitalOut myled(LED1); |
yihui | 0:b81d113f3a83 | 9 | |
yihui | 0:b81d113f3a83 | 10 | USBSerial pc; |
yihui | 0:b81d113f3a83 | 11 | |
yihui | 0:b81d113f3a83 | 12 | int main() { |
yihui | 0:b81d113f3a83 | 13 | |
yihui | 0:b81d113f3a83 | 14 | nfc.begin(); |
yihui | 0:b81d113f3a83 | 15 | uint32_t versiondata = nfc.getFirmwareVersion(); |
yihui | 0:b81d113f3a83 | 16 | if (! versiondata) { |
yihui | 0:b81d113f3a83 | 17 | pc.printf("Didn't find PN532\r\n"); |
yihui | 0:b81d113f3a83 | 18 | while (1) { |
yihui | 0:b81d113f3a83 | 19 | myled = !myled; |
yihui | 0:b81d113f3a83 | 20 | wait(0.1); |
yihui | 0:b81d113f3a83 | 21 | } |
yihui | 0:b81d113f3a83 | 22 | } |
yihui | 0:b81d113f3a83 | 23 | |
yihui | 0:b81d113f3a83 | 24 | pc.printf("Found chip PN5%2X\r\n", versiondata >> 24); |
yihui | 0:b81d113f3a83 | 25 | pc.printf("Firmware V%d.%d\r\n", (versiondata >> 16) & 0xFF, (versiondata >> 8) & 0xFF); |
yihui | 0:b81d113f3a83 | 26 | |
yihui | 0:b81d113f3a83 | 27 | nfc.SAMConfig(); |
yihui | 0:b81d113f3a83 | 28 | |
yihui | 0:b81d113f3a83 | 29 | |
yihui | 0:b81d113f3a83 | 30 | while(1) { |
yihui | 0:b81d113f3a83 | 31 | bool success; |
yihui | 0:b81d113f3a83 | 32 | uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID |
yihui | 0:b81d113f3a83 | 33 | uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type) |
yihui | 0:b81d113f3a83 | 34 | |
yihui | 0:b81d113f3a83 | 35 | // Wait for an ISO14443A type cards (Mifare, etc.). When one is found |
yihui | 0:b81d113f3a83 | 36 | // 'uid' will be populated with the UID, and uidLength will indicate |
yihui | 0:b81d113f3a83 | 37 | // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight) |
yihui | 0:b81d113f3a83 | 38 | success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength); |
yihui | 0:b81d113f3a83 | 39 | |
yihui | 0:b81d113f3a83 | 40 | if (success) { |
yihui | 0:b81d113f3a83 | 41 | myled = 1; |
yihui | 0:b81d113f3a83 | 42 | pc.printf("Found a card\r\n"); |
yihui | 0:b81d113f3a83 | 43 | pc.printf("UID length: %d\r\nUID Value: ", uidLength); |
yihui | 0:b81d113f3a83 | 44 | for (uint8_t i=0; i < uidLength; i++) { |
yihui | 0:b81d113f3a83 | 45 | pc.printf("0x%X ", uid[i]); |
yihui | 0:b81d113f3a83 | 46 | } |
yihui | 0:b81d113f3a83 | 47 | |
yihui | 0:b81d113f3a83 | 48 | wait(1); |
yihui | 0:b81d113f3a83 | 49 | myled = 0; |
yihui | 0:b81d113f3a83 | 50 | } else { |
yihui | 0:b81d113f3a83 | 51 | pc.printf("Time out\r\n"); |
yihui | 0:b81d113f3a83 | 52 | wait(1); |
yihui | 0:b81d113f3a83 | 53 | } |
yihui | 0:b81d113f3a83 | 54 | } |
yihui | 0:b81d113f3a83 | 55 | } |