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