public class MFRC522 extends Object implements Closeable
Datasheet
Example Python code
MiFare Classic Universal toolKit (MFCUK)
Work-in-progress!
Wiring:
Java port from MFRC522 Library to use ARDUINO RFID MODULE KIT 13.56 MHZ WITH TAGS SPI W AND R BY COOQROBOT that was created by Miguel Balboa (circuitito.com).
There are three hardware components involved:
The microcontroller and card reader uses SPI for communication. The protocol is described in the MFRC522 datasheet.
The card reader and the tags communicate using a 13.56MHz electromagnetic field. The protocol is defined in http://wg8.de/wg8n1496_17n3613_Ballot_FCD14443-3.pdf:
If only the PICC UID is wanted, the above documents have all the information needed.
To read and write from MIFARE PICCs, the MIFARE protocol is used after the PICC has been selected.
The MIFARE Classic and Ultralight chips and protocols are described in the datasheets:
Has 16 sectors * 4 blocks/sector * 16 bytes/block = 1024 bytes.
The blocks are numbered 0-63.
Block 3 in each sector is the Sector Trailer. See sections 8.6 and 8.7:
Bytes 0-5: Key A Bytes 6-8: Access Bits Bytes 9: User data Bytes 10-15: Key B (or user data)
Block 0 is read-only manufacturer data, sometimes as follows:
0 1 2 3 |4 | 5 |6 |7 8 9 A B C D E F UID |BCC|SAK|ATAQ|Manufacturer data
To access a block, an authentication using a key from the block's sector must be performed first.
Example: To read from block 10, first authenticate using a key from sector 3 (blocks 8-11).
All keys are set to FFFFFFFFFFFFh at chip delivery.
Warning: Please read section 8.7 "Memory Access". It includes this text: if the PICC detects a format violation the whole sector is irreversibly blocked.
To use a block in "value block" mode (for Increment/Decrement operations) you need to change the sector trailer. Use setAccessBits() to calculate the bit patterns.
Has (32 sectors * 4 blocks/sector + 8 sectors * 16 blocks/sector) * 16 bytes/block = 4096 bytes.
The blocks are numbered 0-255.
The last block in each sector is the Sector Trailer like above.
Has 5 sectors * 4 blocks/sector * 16 bytes/block = 320 bytes.
The blocks are numbered 0-19.
The last block in each sector is the Sector Trailer like above.
Has 16 pages of 4 bytes = 64 bytes.
Pages 0 + 1 is used for the 7-byte UID.
Page 2 contains the last check digit for the UID, one byte manufacturer internal data, and the lock bytes (see http://www.nxp.com/documents/data_sheet/MF0ICU1.pdf section 8.5.2).
Page 3 is OTP, One Time Programmable bits. Once set to 1 they cannot revert to 0.
Pages 4-15 are read/write unless blocked by the lock bytes in page 2.
Has 48 pages of 4 bytes = 192 bytes.
Pages 0 + 1 is used for the 7-byte UID.
Page 2 contains the last check digit for the UID, one byte manufacturer internal data, and the lock bytes (see http://www.nxp.com/documents/data_sheet/MF0ICU1.pdf section 8.5.2).
Page 3 is OTP, One Time Programmable bits. Once set to 1 they cannot revert to 0.
Pages 4-39 are read/write unless blocked by the lock bytes in page 2.
Page 40 Lock bytes
Page 41 16 bit one way counter
Pages 42-43 Authentication configuration
Pages 44-47 Authentication key
KeyA Bits KeyB R W R W R W 0 - A A - A A 1 - A A A A A Default 2 - - A - A - 3 - B AB B - B 4 - B AB - - B 5 - - AB B - - 6 - - AB - - - 7 - - AB - - -
R W + -X 0 AB AB AB AB Data Default 1 AB -- -- AB Value 2 AB -- -- -- Data 3 B B -- -- Data 4 AB B -- -- Data 5 B -- -- -- Data 6 AB B B AB Value 7 -- -- -- -- Data
Modifier and Type | Class and Description |
---|---|
static class |
MFRC522.AntennaGain |
static class |
MFRC522.PiccType |
static class |
MFRC522.Response |
static class |
MFRC522.StatusCode |
static class |
MFRC522.UID |
Modifier and Type | Field and Description |
---|---|
static byte[] |
DEFAULT_KEY |
static byte[] |
FM17522_firmware_reference |
static byte |
MF_ACK |
static byte |
MF_KEY_SIZE |
static byte[] |
MFRC522_firmware_referenceV0_0 |
static byte[] |
MFRC522_firmware_referenceV1_0 |
static byte[] |
MFRC522_firmware_referenceV2_0 |
static byte |
MI_ERR |
static byte |
MI_NOTAGERR |
static byte |
MI_OK |
Constructor and Description |
---|
MFRC522(int chipSelect,
int resetGpio) |
MFRC522(int controller,
int chipSelect,
DigitalOutputDevice resetPin) |
MFRC522(int controller,
int chipSelect,
int resetGpio) |
Modifier and Type | Method and Description |
---|---|
MFRC522.StatusCode |
authenticate(boolean authKeyA,
byte blockAddr,
byte[] key,
MFRC522.UID uid)
Executes the MFRC522 MFAuthent command.
|
void |
close() |
static void |
dumpDetailsToConsole(MFRC522.UID uid)
Deprecated.
kept for backward compatibility
|
void |
dumpMifareClassicSectorToConsole(MFRC522.UID uid,
byte[] key,
byte sector)
Dumps memory contents of a sector of a MIFARE Classic PICC.
|
void |
dumpMifareClassicToConsole(MFRC522.UID uid,
byte[] key)
Dumps memory contents of a MIFARE Classic PICC.
|
void |
dumpMifareUltralightToConsole()
Dumps memory contents of a MIFARE Ultralight PICC.
|
void |
dumpToConsole(MFRC522.UID uid)
Deprecated.
Kept for backward compatibility
|
void |
dumpToConsole(MFRC522.UID uid,
byte[] key) |
void |
dumpVersionToConsole()
Dumps debug info about the connected PCD to Serial.
|
MFRC522.AntennaGain |
getAntennaGain()
Get the current MFRC522 Receiver Gain (RxGain[2:0]) value.
|
static MFRC522.PiccType |
getPiccType(byte sak)
Translates the SAK (Select Acknowledge) to a PICC type.
|
int |
getVersion() |
MFRC522.StatusCode |
haltA()
Instructs a PICC in state ACTIVE(*) to go to state HALT.
|
void |
init()
Initializes the MFRC522 chip.
|
boolean |
isNewCardPresent()
Returns true if a PICC responds to PICC_CMD_REQA.
|
MFRC522.StatusCode |
mifareDecrement(byte blockAddr,
int delta)
MIFARE Decrement subtracts the delta from the value of the addressed block, and stores the result in a volatile memory.
|
Integer |
mifareGetValue(byte blockAddr)
Helper routine to read the current value from a Value Block.
|
MFRC522.StatusCode |
mifareIncrement(byte blockAddr,
int delta)
MIFARE Increment adds the delta to the value of the addressed block, and stores the result in a volatile memory.
|
boolean |
mifareOpenUidBackdoor()
Performs the "magic sequence" needed to get Chinese UID changeable
Mifare cards to allow writing to sector 0, where the card UID is stored.
|
byte[] |
mifareRead(byte blockAddr)
Reads 16 bytes (+ 2 bytes CRC_A) from the active PICC.
|
MFRC522.StatusCode |
mifareRestore(byte blockAddr)
MIFARE Restore copies the value of the addressed block into a volatile memory.
|
boolean |
mifareSetUid(byte[] newUid,
MFRC522.UID uid,
byte[] authKey)
Reads entire block 0, including all manufacturer data, and overwrites
that block with the new UID, a freshly calculated BCC, and the original
manufacturer data.
|
MFRC522.StatusCode |
mifareSetValue(byte blockAddr,
int value)
Helper routine to write a specific value into a Value Block.
|
MFRC522.StatusCode |
mifareTransceive(byte[] sendData) |
MFRC522.StatusCode |
mifareTransceive(byte[] sendData,
boolean acceptTimeout)
Wrapper for MIFARE protocol communication.
|
MFRC522.StatusCode |
mifareTransfer(byte blockAddr)
MIFARE Transfer writes the value stored in the volatile memory into one MIFARE Classic block.
|
MFRC522.StatusCode |
mifareTwoStepHelper(com.diozero.MFRC522.PiccCommand command,
byte blockAddr,
int data)
Helper function for the two-step MIFARE Classic protocol operations Decrement, Increment and Restore.
|
MFRC522.StatusCode |
mifareUltralightWrite(byte page,
byte[] buffer)
Writes a 4 byte page to the active MIFARE Ultralight PICC.
|
boolean |
mifareUnbrickUidSector()
Resets entire sector 0 to zeroes, so the card can be read again by readers.
|
MFRC522.StatusCode |
mifareWrite(byte blockAddr,
byte[] buffer)
Writes 16 bytes to the active PICC.
|
boolean |
performSelfTest()
Performs a self-test of the MFRC522
See 16.1.1 in http://www.nxp.com/documents/data_sheet/MFRC522.pdf
|
MFRC522.UID |
readCardSerial()
Simple wrapper around PICC_Select.
|
MFRC522.StatusCode |
requestA(byte[] bufferATQA)
Transmits a REQuest command, Type A.
|
MFRC522.StatusCode |
requestAOrWakeUpA(com.diozero.MFRC522.PiccCommand command,
byte[] bufferATQA)
Transmits REQA or WUPA commands.
|
MFRC522.UID |
select() |
MFRC522.UID |
select(byte validBits)
Transmits SELECT/ANTICOLLISION commands to select a single PICC.
|
void |
setAntennaGain(MFRC522.AntennaGain gain)
Set the MFRC522 Receiver Gain (RxGain) to value specified by given mask.
|
void |
setAntennaOn(boolean on)
Open antennas, each time you start or shut down the natural barrier
between the transmitter should be at least 1ms interval
|
void |
setLogReadsAndWrites(boolean logReadsAndWrites) |
void |
stopCrypto1()
Used to exit the PCD from its authenticated state.
|
MFRC522.StatusCode |
wakeupA(byte[] bufferATQA)
Transmits a Wake-UP command, Type A.
|
public static final byte[] DEFAULT_KEY
public static final byte[] MFRC522_firmware_referenceV0_0
public static final byte[] MFRC522_firmware_referenceV1_0
public static final byte[] MFRC522_firmware_referenceV2_0
public static final byte[] FM17522_firmware_reference
public static final byte MF_ACK
public static final byte MF_KEY_SIZE
public static final byte MI_OK
public static final byte MI_NOTAGERR
public static final byte MI_ERR
public MFRC522(int chipSelect, int resetGpio)
public MFRC522(int controller, int chipSelect, int resetGpio)
public MFRC522(int controller, int chipSelect, DigitalOutputDevice resetPin)
public void close()
close
in interface Closeable
close
in interface AutoCloseable
public void setLogReadsAndWrites(boolean logReadsAndWrites)
public void init()
public void setAntennaOn(boolean on)
on
- on/off valuepublic MFRC522.AntennaGain getAntennaGain()
public void setAntennaGain(MFRC522.AntennaGain gain)
gain
- New antenna gain valuepublic boolean performSelfTest()
public int getVersion()
public MFRC522.StatusCode requestA(byte[] bufferATQA)
bufferATQA
- The buffer to store the ATQA (Answer to request) inpublic MFRC522.StatusCode wakeupA(byte[] bufferATQA)
bufferATQA
- The buffer to store the ATQA (Answer to request) inpublic MFRC522.StatusCode requestAOrWakeUpA(com.diozero.MFRC522.PiccCommand command, byte[] bufferATQA)
command
- The command to send - PICC_CMD_REQA or PICC_CMD_WUPAbufferATQA
- The buffer to store the ATQA (Answer to request) inpublic MFRC522.UID select()
public MFRC522.UID select(byte validBits)
validBits
- The number of known UID bits supplied in *uid. Normally 0. If set you must also supply uid->size.public MFRC522.StatusCode haltA()
public MFRC522.StatusCode authenticate(boolean authKeyA, byte blockAddr, byte[] key, MFRC522.UID uid)
authKeyA
- PICC_CMD_MF_AUTH_KEY_A or PICC_CMD_MF_AUTH_KEY_BblockAddr
- The block number. See numbering in the comments in the .h file.key
- Crypto1 key to use (6 bytes)uid
- Pointer to Uid struct. The first 4 bytes of the UID is used.public void stopCrypto1()
public byte[] mifareRead(byte blockAddr)
blockAddr
- MIFARE Classic: The block (0-0xff) number. MIFARE Ultralight: The first page to return data from.public MFRC522.StatusCode mifareWrite(byte blockAddr, byte[] buffer)
blockAddr
- MIFARE Classic: The block (0-0xff) number. MIFARE Ultralight: The page (2-15) to write to.buffer
- The 16 bytes to write to the PICCpublic MFRC522.StatusCode mifareUltralightWrite(byte page, byte[] buffer)
page
- The page (2-15) to write to.buffer
- The 4 bytes to write to the PICCpublic MFRC522.StatusCode mifareDecrement(byte blockAddr, int delta)
blockAddr
- The block (0-0xff) number.delta
- This number is subtracted from the value of block blockAddr.public MFRC522.StatusCode mifareIncrement(byte blockAddr, int delta)
blockAddr
- The block (0-0xff) number.delta
- This number is added to the value of block blockAddr.public MFRC522.StatusCode mifareRestore(byte blockAddr)
blockAddr
- The block (0-0xff) number.public MFRC522.StatusCode mifareTwoStepHelper(com.diozero.MFRC522.PiccCommand command, byte blockAddr, int data)
command
- The command to useblockAddr
- The block (0-0xff) number.data
- The data to transfer in step 2public MFRC522.StatusCode mifareTransfer(byte blockAddr)
blockAddr
- The block (0-0xff) number.public Integer mifareGetValue(byte blockAddr)
blockAddr
- The block (0x00-0xff) number.public MFRC522.StatusCode mifareSetValue(byte blockAddr, int value)
blockAddr
- The block (0x00-0xff) number.value
- New value of the Value Block.public MFRC522.StatusCode mifareTransceive(byte[] sendData)
public MFRC522.StatusCode mifareTransceive(byte[] sendData, boolean acceptTimeout)
sendData
- Data to transfer to the FIFO. Do NOT include the CRC_A.acceptTimeout
- A timeout is also successpublic static MFRC522.PiccType getPiccType(byte sak)
sak
- The SAK byte returned from PICC_Select().public boolean mifareOpenUidBackdoor()
public boolean mifareSetUid(byte[] newUid, MFRC522.UID uid, byte[] authKey)
newUid
- New UID.uid
- Current UIDauthKey
- Authentication keypublic boolean mifareUnbrickUidSector()
public boolean isNewCardPresent()
public MFRC522.UID readCardSerial()
public void dumpVersionToConsole()
public void dumpToConsole(MFRC522.UID uid)
uid
- UID returned from a successful PICC_Select().public void dumpToConsole(MFRC522.UID uid, byte[] key)
public static void dumpDetailsToConsole(MFRC522.UID uid)
uid
- UID struct returned from a successful PICC_Select().public void dumpMifareClassicToConsole(MFRC522.UID uid, byte[] key)
uid
- UID returned from a successful PICC_Select().key
- Key A used for all sectors.public void dumpMifareClassicSectorToConsole(MFRC522.UID uid, byte[] key, byte sector)
uid
- UID returned from a successful select().key
- Key A for the sector.sector
- The sector to dump, 0..39.public void dumpMifareUltralightToConsole()
Copyright © 2016–2017 mattjlewis. All rights reserved.