Krolyxon
Commit 3120783

Branches

Tags

Refs →

orion-rf.piolibdepsesp32-s3-n16r8Adafruit PN532examplesiso14443as_targetiso14443as_target.ino

106 lines · 3.9 KB

1/**************************************************************************/
2/*!
3 @file iso14443as_target.pde
4 @original Adafruit Industries
5 @modified Salvador Mendoza(@Netxing)
6 @license BSD (see license.txt)
7
8 This example will attempt to mimic an ISO14443A smart card
9 and retrieve some basic information from a PoS or terminal,
10 this can be used to establish a communication process.
11
12 Note that you need the baud rate to be 115200 because we need to print
13 out the data and read from the card at the same time!
14
15This is an example sketch for the Adafruit PN532 NFC/RFID breakout boards
16This library works with the Adafruit NFC breakout
17 ----> https://www.adafruit.com/products/364
18
19Check out the links above for our tutorials and wiring diagrams
20These chips use SPI or I2C to communicate.
21
22Adafruit invests time and resources providing this open source code,
23please support Adafruit and open-source hardware by purchasing
24products from Adafruit!
25
26*/
27/**************************************************************************/
28#include <Wire.h>
29#include <SPI.h>
30#include <Adafruit_PN532.h>
31
32// If using the breakout with SPI, define the pins for SPI communication.
33#define PN532_SCK (2)
34#define PN532_MOSI (3)
35#define PN532_SS (4)
36#define PN532_MISO (5)
37
38// If using the breakout or shield with I2C, define just the pins connected
39// to the IRQ and reset lines. Use the values below (2, 3) for the shield!
40#define PN532_IRQ (2)
41#define PN532_RESET (3) // Not connected by default on the NFC Shield
42
43// Uncomment just _one_ line below depending on how your breakout or shield
44// is connected to the Arduino:
45
46// Use this line for a breakout with a SPI connection:
47Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);
48
49// Use this line for a breakout with a hardware SPI connection. Note that
50// the PN532 SCK, MOSI, and MISO pins need to be connected to the Arduino's
51// hardware SPI SCK, MOSI, and MISO pins. On an Arduino Uno these are
52// SCK = 13, MOSI = 11, MISO = 12. The SS line can be any digital IO pin.
53//Adafruit_PN532 nfc(PN532_SS);
54
55// Or use this line for a breakout or shield with an I2C connection:
56//Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);
57
58void setup(void) {
59 Serial.begin(115200);
60 while (!Serial) delay(10); // for Leonardo/Micro/Zero
61
62 Serial.println("Hello!");
63
64 nfc.begin();
65
66 uint32_t versiondata = nfc.getFirmwareVersion();
67 if (! versiondata) {
68 Serial.print("Didn't find PN53x board");
69 while (1); // halt
70 }
71
72 // Got ok data, print it out!
73 Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);
74 Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);
75 Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
76
77 // Set the max number of retry attempts to read from a card
78 // This prevents us from waiting forever for a card, which is
79 // the default behaviour of the PN532.
80 nfc.setPassiveActivationRetries(0xFF);
81
82 Serial.println("As Target... Approach the NFC PN532 Board to a PoS or terminal!");
83 delay(200);
84}
85
86void loop(void) {
87 uint8_t apdubuffer[255] = {}, apdulen = 0;
88 uint8_t ppse[] = {0x8E, 0x6F, 0x23, 0x84, 0x0E, 0x32, 0x50, 0x41, 0x59, 0x2E, 0x53, 0x59, 0x53, 0x2E, 0x44, 0x44, 0x46, 0x30, 0x31, 0xA5, 0x11, 0xBF, 0x0C, 0x0E, 0x61, 0x0C, 0x4F, 0x07, 0xA0, 0x00, 0x00, 0x00, 0x03, 0x10, 0x10, 0x87, 0x01, 0x01, 0x90, 0x00};
89 nfc.AsTarget();
90 (void)nfc.getDataTarget(apdubuffer, &apdulen); //Read initial APDU
91 if (apdulen>0){
92 for (uint8_t i = 0; i < apdulen; i++){
93 Serial.print(" 0x"); Serial.print(apdubuffer[i], HEX);
94 }
95 Serial.println("");
96 }
97 nfc.setDataTarget(ppse, sizeof(ppse)); //Mimic a smart card response with a PPSE APDU
98 nfc.getDataTarget(apdubuffer, &apdulen); //Read respond from the PoS or Terminal
99 if (apdulen>0){
100 for (uint8_t i = 0; i < apdulen; i++){
101 Serial.print(" 0x"); Serial.print(apdubuffer[i], HEX);
102 }
103 Serial.println("");
104 }
105 delay(1000);
106}