MFRC522
Classes
Type
Name
struct
struct
Public Types
Type
Name
enum
enum
enum
enum
enum
enum
enum
Public Attributes
Type
Name
Public Static Attributes
Type
Name
static const uint8_t
Public Functions
Type
Name
int
int
int
int
int
uint8_t
void
void
void
uint8_t
uint8_t
uint8_t
uint8_t
uint8_t
uint8_t
uint8_t
uint8_t
uint8_t
void
uint8_t
uint8_t
uint8_t
uint8_t
uint8_t
uint8_t
uint8_t
uint8_t
uint8_t
char *
char *
void
bool
bool
Detailed Description
MFRC522 example
Public Types Documentation
enum PCD_Register
MFRC522 registers (described in chapter 9 of the datasheet). When using SPI all addresses areed one bit left in the "SPI address byte" (section 8.1.2.3)
enum PCD_Command
enum PCD_RxGain
enum PICC_Command
enum MIFARE_Misc
enum PICC_Type
enum StatusCode
Public Attributes Documentation
variable uid
Public Static Attributes Documentation
variable FIFO_SIZE
Public Functions Documentation
function MFRC522
MFRC522 constructor
Parameters:
sda I2C sda pin
scl I2C scl pin
Constructor. Prepares the output pins.
function MFRC522
MFRC522 constructor
Parameters:
sda I2C sda pin
scl I2C scl pin
reset Reset pin
Constructor. Prepares the output pins.
function ~MFRC522
MFRC522 destructor Destructor.
function PCD_Init
Initializes the MFRC522 chip.
function PCD_Reset
Performs a soft reset on the MFRC522 chip and waits for it to be ready again.
function PCD_AntennaOn
Turns the antenna on by enabling pins TX1 and TX2. After a reset these pins disabled.
function PCD_WriteRegister
Writes a byte to the specified register in the MFRC522 chip. The interface is described in the datasheet section 8.1.2.
Parameters:
reg The register to write to. One of the PCD_Register enums.
value The value to write.
Writes a byte to the specified register in the MFRC522 chip. The interface is described in the datasheet section 8.1.2.
function PCD_WriteRegister
Writes a number of bytes to the specified register in the MFRC522 chip. The interface is described in the datasheet section 8.1.2.
Parameters:
reg The register to write to. One of the PCD_Register enums.
count The number of bytes to write to the register
values The values to write. Byte array.
Writes a number of bytes to the specified register in the MFRC522 chip. The interface is described in the datasheet section 8.1.2.
function PCD_ReadRegister
Reads a byte from the specified register in the MFRC522 chip. The interface is described in the datasheet section 8.1.2.
Parameters:
reg The register to read from. One of the PCD_Register enums.
Returns:
Register value
Reads a byte from the specified register in the MFRC522 chip. The interface is described in the datasheet section 8.1.2.
function PCD_ReadRegister
Reads a number of bytes from the specified register in the MFRC522 chip. The interface is described in the datasheet section 8.1.2.
Parameters:
reg The register to read from. One of the PCD_Register enums.
count The number of bytes to read.
values Byte array to store the values in.
rxAlign Only bit positions rxAlign..7 in values[0] are updated.
Reads a number of bytes from the specified register in the MFRC522 chip. The interface is described in the datasheet section 8.1.2.
function PCD_SetRegisterBits
Sets the bits given in mask in register reg.
Parameters:
reg The register to update. One of the PCD_Register enums.
mask The bits to set.
Sets the bits given in mask in register reg.
function PCD_ClrRegisterBits
Clears the bits given in mask from register reg.
Parameters:
reg The register to update. One of the PCD_Register enums.
mask The bits to clear.
Clears the bits given in mask from register reg.
function PCD_CalculateCRC
Use the CRC coprocessor in the MFRC522 to calculate a CRC_A.
Parameters:
data Pointer to the data to transfer to the FIFO for CRC calculation.
length The number of bytes to transfer.
result Pointer to result buffer. Result is written to result[0..1], low byte first.
Returns:
STATUS_OK on success, STATUS_??? otherwise.
Use the CRC coprocessor in the MFRC522 to calculate a CRC_A.
function PCD_TransceiveData
Executes the Transceive command. CRC validation can only be done if backData and backLen are specified.
Parameters:
sendData Pointer to the data to transfer to the FIFO.
sendLen Number of bytes to transfer to the FIFO.
backData NULL or pointer to buffer if data should be read back after executing the command.
backLen Max number of bytes to write to *backData. Out: The number of bytes returned.
validBits The number of valid bits in the last byte. 0 for 8 valid bits. Default NULL.
rxAlign Defines the bit position in backData[0] for the first bit received. Default 0.
checkCRC True => The last two bytes of the response is assumed to be a CRC_A that must be validated.
Returns:
STATUS_OK on success, STATUS_??? otherwise.
Executes the Transceive command. CRC validation can only be done if backData and backLen are specified.
function PCD_CommunicateWithPICC
Transfers data to the MFRC522 FIFO, executes a commend, waits for completion and transfers data back from the FIFO. CRC validation can only be done if backData and backLen are specified.
Parameters:
command The command to execute. One of the PCD_Command enums.
waitIRq The bits in the ComIrqReg register that signals successful completion of the command.
sendData Pointer to the data to transfer to the FIFO.
sendLen Number of bytes to transfer to the FIFO.
backData NULL or pointer to buffer if data should be read back after executing the command.
backLen In: Max number of bytes to write to *backData. Out: The number of bytes returned.
validBits In/Out: The number of valid bits in the last byte. 0 for 8 valid bits.
rxAlign In: Defines the bit position in backData[0] for the first bit received. Default 0.
checkCRC In: True => The last two bytes of the response is assumed to be a CRC_A that must be validated.
Returns:
STATUS_OK on success, STATUS_??? otherwise.
Transfers data to the MFRC522 FIFO, executes a commend, waits for completion and transfers data back from the FIFO. CRC validation can only be done if backData and backLen are specified.
function PICC_RequestA
Transmits a REQuest command, Type A. Invites PICCs in state IDLE to go to READY and prepare for anticollision or selection. 7 bit frame. Beware: When two PICCs are in the field at the same time I often get STATUS_TIMEOUT - probably due do bad antenna design.
Parameters:
bufferATQA The buffer to store the ATQA (Answer to request) in
bufferSize Buffer size, at least two bytes. Also number of bytes returned if STATUS_OK.
Returns:
STATUS_OK on success, STATUS_??? otherwise.
function PICC_WakeupA
Transmits a Wake-UP command, Type A. Invites PICCs in state IDLE and HALT to go to READY(*) and prepare for anticollision or selection. 7 bit frame. Beware: When two PICCs are in the field at the same time I often get STATUS_TIMEOUT - probably due do bad antenna design.
Parameters:
bufferATQA The buffer to store the ATQA (Answer to request) in
bufferSize Buffer size, at least two bytes. Also number of bytes returned if STATUS_OK.
Returns:
STATUS_OK on success, STATUS_??? otherwise.
Transmits a Wake-UP command, Type A. Invites PICCs in state IDLE and HALT to go to READY(*) and prepare for anticollision or selection. 7 bit frame. Beware: When two PICCs are in the field at the same time I often get STATUS_TIMEOUT - probably due do bad antenna design.
function PICC_REQA_or_WUPA
Transmits REQA or WUPA commands. Beware: When two PICCs are in the field at the same time I often get STATUS_TIMEOUT - probably due do bad antenna design.
Parameters:
command The command to send - PICC_CMD_REQA or PICC_CMD_WUPA
bufferATQA The buffer to store the ATQA (Answer to request) in
bufferSize Buffer size, at least two bytes. Also number of bytes returned if STATUS_OK.
Returns:
STATUS_OK on success, STATUS_??? otherwise.
function PICC_Select
Transmits SELECT/ANTICOLLISION commands to select a single PICC. Before calling this function the PICCs must be placed in the READY(*) state by calling PICC_RequestA() or PICC_WakeupA(). On success:
The chosen PICC is in state ACTIVE(*) and all other PICCs have returned to state IDLE/HALT. (Figure 7 of the ISO/IEC 14443-3 draft.)
The UID size and value of the chosen PICC is returned in *uid along with the SAK.
A PICC UID consists of 4, 7 or 10 bytes. Only 4 bytes can be specified in a SELECT command, so for the longer UIDs two or three iterations are used: UID size Number of UID bytes Cascade levels Example of PICC ======== =================== ============== =============== single 4 1 MIFARE Classic double 7 2 MIFARE Ultralight triple 10 3 Not currently in use?
Parameters:
uid Pointer to Uid struct. Normally output, but can also be used to supply a known UID.
validBits The number of known UID bits supplied in *uid. Normally 0. If set you must also supply uid->size.
Returns:
STATUS_OK on success, STATUS_??? otherwise.
function PICC_HaltA
Instructs a PICC in state ACTIVE(*) to go to state HALT.
Returns:
STATUS_OK on success, STATUS_??? otherwise.
function PCD_Authenticate
Executes the MFRC522 MFAuthent command. This command manages MIFARE authentication to enable a secure communication to any MIFARE Mini, MIFARE 1K and MIFARE 4K card. The authentication is described in the MFRC522 datasheet section 10.3.1.9 and http://www.nxp.com/documents/data_sheet/MF1S503x.pdf section 10.1. For use with MIFARE Classic PICCs. The PICC must be selected - ie in state ACTIVE(*) - before calling this function. Remember to call PCD_StopCrypto1() after communicating with the authenticated PICC - otherwise no new communications can start. All keys are set to FFFFFFFFFFFFh at chip delivery.
Parameters:
command PICC_CMD_MF_AUTH_KEY_A or PICC_CMD_MF_AUTH_KEY_B
blockAddr The block number. See numbering in the comments in the .h file.
key Pointer to the Crypteo1 key to use (6 bytes)
uid Pointer to Uid struct. The first 4 bytes of the UID is used.
Returns:
STATUS_OK on success, STATUS_??? otherwise. Probably STATUS_TIMEOUT if you supply the wrong key.
function PCD_StopCrypto1
Used to exit the PCD from its authenticated state. Remember to call this function after communicating with an authenticated PICC - otherwise no new communications can start.
function MIFARE_Read
Reads 16 bytes (+ 2 bytes CRC_A) from the active PICC. For MIFARE Classic the sector containing the block must be authenticated before calling this function. For MIFARE Ultralight only addresses 00h to 0Fh are decoded. The MF0ICU1 returns a NAK for higher addresses. The MF0ICU1 responds to the READ command by sending 16 bytes starting from the page address defined by the command argument. For example; if blockAddr is 03h then pages 03h, 04h, 05h, 06h are returned. A roll-back is implemented: If blockAddr is 0Eh, then the contents of pages 0Eh, 0Fh, 00h and 01h are returned. The buffer must be at least 18 bytes because a CRC_A is also returned. Checks the CRC_A before returning STATUS_OK.
Parameters:
blockAddr MIFARE Classic: The block (0-0xff) number. MIFARE Ultralight: The first page to return data from.
buffer The buffer to store the data in
bufferSize Buffer size, at least 18 bytes. Also number of bytes returned if STATUS_OK.
Returns:
STATUS_OK on success, STATUS_??? otherwise.
function MIFARE_Write
Writes 16 bytes to the active PICC. For MIFARE Classic the sector containing the block must be authenticated before calling this function. For MIFARE Ultralight the opretaion is called "COMPATIBILITY WRITE". Even though 16 bytes are transferred to the Ultralight PICC, only the least significant 4 bytes (bytes 0 to 3) are written to the specified address. It is recommended to set the remaining bytes 04h to 0Fh to all logic 0.
Parameters:
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 PICC
bufferSize Buffer size, must be at least 16 bytes. Exactly 16 bytes are written.
Returns:
STATUS_OK on success, STATUS_??? otherwise.
function MIFARE_UltralightWrite
Writes a 4 byte page to the active MIFARE Ultralight PICC.
Parameters:
page The page (2-15) to write to.
buffer The 4 bytes to write to the PICC
bufferSize Buffer size, must be at least 4 bytes. Exactly 4 bytes are written.
Returns:
STATUS_OK on success, STATUS_??? otherwise.
function MIFARE_Decrement
MIFARE Decrement subtracts the delta from the value of the addressed block, and stores the result in a volatile memory. For MIFARE Classic only. The sector containing the block must be authenticated before calling this function. Only for blocks in "value block" mode, ie with access bits [C1 C2 C3] = [110] or [001]. Use MIFARE_Transfer() to store the result in a block.
Parameters:
blockAddr The block (0-0xff) number.
delta This number is subtracted from the value of block blockAddr.
Returns:
STATUS_OK on success, STATUS_??? otherwise.
function MIFARE_Increment
MIFARE Increment adds the delta to the value of the addressed block, and stores the result in a volatile memory. For MIFARE Classic only. The sector containing the block must be authenticated before calling this function. Only for blocks in "value block" mode, ie with access bits [C1 C2 C3] = [110] or [001]. Use MIFARE_Transfer() to store the result in a block.
Parameters:
blockAddr The block (0-0xff) number.
delta This number is added to the value of block blockAddr.
Returns:
STATUS_OK on success, STATUS_??? otherwise.
function MIFARE_Restore
MIFARE Restore copies the value of the addressed block into a volatile memory. For MIFARE Classic only. The sector containing the block must be authenticated before calling this function. Only for blocks in "value block" mode, ie with access bits [C1 C2 C3] = [110] or [001]. Use MIFARE_Transfer() to store the result in a block.
Parameters:
blockAddr The block (0-0xff) number.
Returns:
STATUS_OK on success, STATUS_??? otherwise.
MIFARE Restore copies the value of the addressed block into a volatile memory.
function MIFARE_Transfer
MIFARE Transfer writes the value stored in the volatile memory into one MIFARE Classic block. For MIFARE Classic only. The sector containing the block must be authenticated before calling this function. Only for blocks in "value block" mode, ie with access bits [C1 C2 C3] = [110] or [001].
Parameters:
blockAddr The block (0-0xff) number.
Returns:
STATUS_OK on success, STATUS_??? otherwise.
function PCD_MIFARE_Transceive
Wrapper for MIFARE protocol communication. Adds CRC_A, executes the Transceive command and checks that the response is MF_ACK or a timeout.
Parameters:
sendData Pointer to the data to transfer to the FIFO. Do NOT include the CRC_A.
sendLen Number of bytes in sendData.
acceptTimeout True => A timeout is also success
Returns:
STATUS_OK on success, STATUS_??? otherwise.
function PICC_GetType
Translates the SAK (Select Acknowledge) to a PICC type.
Parameters:
sak The SAK byte returned from PICC_Select().
Returns:
PICC_Type
function PICC_GetTypeName
Returns a string pointer to the PICC type name.
Parameters:
type One of the PICC_Type enums.
Returns:
A string pointer to the PICC type name.
function GetStatusCodeName
Returns a string pointer to a status code name.
Parameters:
code One of the StatusCode enums.
Returns:
A string pointer to a status code name.
function MIFARE_SetAccessBits
Calculates the bit pattern needed for the specified access bits. In the [C1 C2 C3] tupples C1 is MSB (=4) and C3 is LSB (=1).
Parameters:
accessBitBuffer Pointer to byte 6, 7 and 8 in the sector trailer. Bytes [0..2] will be set.
g0 Access bits [C1 C2 C3] for block 0 (for sectors 0-31) or blocks 0-4 (for sectors 32-39)
g1 Access bits [C1 C2 C3] for block 1 (for sectors 0-31) or blocks 5-9 (for sectors 32-39)
g2 Access bits [C1 C2 C3] for block 2 (for sectors 0-31) or blocks 10-14 (for sectors 32-39)
g3 Access bits [C1 C2 C3] for the sector trailer, block 3 (for sectors 0-31) or block 15 (for sectors 32-39)
function PICC_IsNewCardPresent
Returns true if a PICC responds to PICC_CMD_REQA. Only "new" cards in state IDLE are invited. Sleeping cards in state HALT are ignored.
Returns:
bool
function PICC_ReadCardSerial
Simple wrapper around PICC_Select. Returns true if a UID could be read. Remember to call PICC_IsNewCardPresent(), PICC_RequestA() or PICC_WakeupA() first. The read UID is available in the class variable uid.
Returns:
bool
The documentation for this class was generated from the following file: D:/w/hw-libs/\_libs\_/libraries/MFRC522.h
Last updated