Skip to content

Commit

Permalink
Add comments to make it easier to understand
Browse files Browse the repository at this point in the history
  • Loading branch information
ljos committed Oct 7, 2013
1 parent 45a84cb commit 2dc010b
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions examples/auth_read_write/authenticate.ino
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ MFRC522 nfc(SAD, RST);

void setup() {
SPI.begin();
// Read a fast as possible. There is a limit for how long we are
// allowed to read from the tags.
Serial.begin(115200);

Serial.println("Looking for MFRC522.");
nfc.begin();

// Get the firmware version of the RFID chip
byte version = nfc.getFirmwareVersion();
if (! version) {
Serial.print("Didn't find MFRC522 board.");
Expand All @@ -34,6 +37,8 @@ void loop() {
byte serial[5];
int i, j, pos;

// Send a general request out into the aether. If there is a tag in
// the area it will respond and the status will be MI_OK.
status = nfc.requestTag(MF1_REQIDL, data);

if (status == MI_OK) {
Expand All @@ -43,6 +48,8 @@ void loop() {
Serial.print(", ");
Serial.println(data[1], HEX);

// calculate the anti-collision value for the currently detected
// tag and write the serial into the data array.
status = nfc.antiCollision(data);
memcpy(serial, data, 5);

Expand All @@ -53,15 +60,24 @@ void loop() {
}
Serial.println(serial[3], HEX);

// Select the tag that we want to talk to. If we don't do this the
// chip does not know which tag it should talk if there should be
// any other tags in the area..
nfc.selectTag(serial);

// Assuming that there are only 64 blocks of memory in this chip.
for (i = 0; i < 64; i++) {
// Try to authenticate each block first with the A key.
status = nfc.authenticate(MF1_AUTHENT1A, i, keyA, serial);
if (status == MI_OK) {
Serial.print("Authenticated block nb. 0x");
Serial.print(i, HEX);
Serial.println(" with key A.");
// Reading block i from the tag into data.
status = nfc.readFromTag(i, data);
if (status == MI_OK) {
// If there was no error when reading; print all the hex
// values in the data.
for (j = 0; j < 15; j++) {
Serial.print(data[j], HEX);
Serial.print(", ");
Expand All @@ -71,6 +87,8 @@ void loop() {
Serial.println("Read failed.");
}
} else {
// If we could not authenticate with the A key, we will try
// the B key.
status = nfc.authenticate(MF1_AUTHENT1B, i, keyB, serial);
if (status == MI_OK) {
Serial.print("Authenticated block nb. 0x");
Expand All @@ -93,6 +111,7 @@ void loop() {
}
}

// Stop the tag and get ready for reading a new tag.
nfc.haltTag();
}
delay(2000);
Expand Down

0 comments on commit 2dc010b

Please sign in to comment.