Device driver for the NXP MFRC522 RFID controller. More...
Device driver for the NXP MFRC522 RFID controller.
With this driver MIFARE and NTAG tags/keys can be read and written contactless.
Supported features:
Partially supported features:
Data sheet available here: https://www.nxp.com/docs/en/data-sheet/MFRC522.pdf
This driver is a port of a driver originally written for Arduino, you can find the original here: https://github.com/miguelbalboa/rfid (currently based on commit 0a568b45baf1852883630e90ea125786e88f5322)
Files | |
file | mfrc522.h |
file | mfrc522_regs.h |
Register definitions for the MFRC522 controller. | |
Data Structures | |
struct | mfrc522_params_t |
MFRC522 device initialization parameters. More... | |
struct | mfrc522_t |
MFRC522 device data structure type. More... | |
struct | mfrc522_uid_t |
A struct used for passing the UID of a PICC. More... | |
struct | mfrc522_mifare_key_t |
A struct used for passing a MIFARE Crypto1 key. More... | |
Macros | |
#define | MFRC522_UID_MAX_SIZE 10 |
Maximum size for uid byte array. | |
#define | MFRC522_MF_ACK 0xA |
The MIFARE Classic uses a 4 bit ACK/NAK. | |
#define | MFRC522_MF_KEY_SIZE 6 |
A Mifare Crypto1 key is 6 bytes. | |
Functions | |
void | mfrc522_pcd_set_register_bitmask (mfrc522_t *dev, mfrc522_pcd_register_t reg, uint8_t mask) |
Sets the bits given in mask in register reg. | |
void | mfrc522_pcd_clear_register_bitmask (mfrc522_t *dev, mfrc522_pcd_register_t reg, uint8_t mask) |
Clears the bits given in mask from register reg. | |
int | mfrc522_pcd_calculate_crc (mfrc522_t *dev, const uint8_t *data, uint8_t length, uint8_t *result) |
Use the CRC coprocessor in the MFRC522 to calculate a CRC_A. | |
int | mfrc522_pcd_init (mfrc522_t *dev, const mfrc522_params_t *params) |
Initialization. | |
void | mfrc522_pcd_reset (mfrc522_t *dev) |
Performs a soft reset on the MFRC522 chip and waits for it to be ready again. | |
void | mfrc522_pcd_antenna_on (mfrc522_t *dev) |
Turns the antenna on by enabling pins TX1 and TX2. | |
void | mfrc522_pcd_antenna_off (mfrc522_t *dev) |
Turns the antenna off by disabling pins TX1 and TX2. | |
mfrc522_pcd_rx_gain_t | mfrc522_pcd_get_antenna_gain (mfrc522_t *dev) |
Get the current MFRC522 receiver gain. | |
void | mfrc522_pcd_set_antenna_gain (mfrc522_t *dev, mfrc522_pcd_rx_gain_t rx_gain) |
Set the MFRC522 receiver gain. | |
void | mfrc522_pcd_soft_power_down (mfrc522_t *dev) |
Set the MFRC522 to soft power-down mode. | |
void | mfrc522_pcd_soft_power_up (mfrc522_t *dev) |
Set the MFRC522 to leave soft power-down mode. | |
int | mfrc522_pcd_transceive_data (mfrc522_t *dev, const uint8_t *send_data, uint8_t send_len, uint8_t *back_data, uint8_t *back_len, uint8_t *valid_bits, uint8_t rx_align, bool check_crc) |
Executes MFRC522_CMD_TRANSCEIVE. | |
int | mfrc522_pcd_communicate_with_picc (mfrc522_t *dev, mfrc522_pcd_command_t command, uint8_t wait_irq, const uint8_t *send_data, uint8_t send_len, uint8_t *back_data, uint8_t *back_len, uint8_t *valid_bits, uint8_t rx_align, bool check_crc) |
Transfers data to MFRC522's FIFO, executes a command, waits for completion and transfers data back from the FIFO. | |
int | mfrc522_picc_request_a (mfrc522_t *dev, uint8_t *buffer_atqa, uint8_t *buffer_size) |
Transmits REQA, Type A. | |
int | mfrc522_picc_wakeup_a (mfrc522_t *dev, uint8_t *buffer_atqa, uint8_t *buffer_size) |
Transmits WUPA, Type A. | |
int | mfrc522_picc_reqa_or_wupa (mfrc522_t *dev, mfrc522_picc_command_t command, uint8_t *buffer_atqa, uint8_t *buffer_size) |
Transmits REQA (Type A) or WUPA (Type A) commands. | |
int | mfrc522_picc_select (mfrc522_t *dev, mfrc522_uid_t *uid, uint8_t valid_bits) |
Transmits SELECT/ANTICOLLISION commands to select a single PICC. | |
int | mfrc522_picc_halt_a (mfrc522_t *dev) |
Instructs a PICC in state ACTIVE(*) to go to state HALT. | |
int | mfrc522_pcd_authenticate (mfrc522_t *dev, mfrc522_picc_command_t command, uint8_t block_addr, const mfrc522_mifare_key_t *key, const mfrc522_uid_t *uid) |
Executes the MFRC522 MFAuthent command. | |
void | mfrc522_pcd_stop_crypto1 (mfrc522_t *dev) |
Used to exit the PCD from its authenticated state. | |
int | mfrc522_mifare_read (mfrc522_t *dev, uint8_t block_addr, uint8_t *buffer, uint8_t *buffer_size) |
Read 16 bytes (+ 2 bytes CRC_A) from the active PICC. | |
int | mfrc522_mifare_write (mfrc522_t *dev, uint8_t block_addr, const uint8_t *buffer, uint8_t buffer_size) |
Write 16 bytes to the active PICC. | |
int | mfrc522_mifare_ultralight_write (mfrc522_t *dev, uint8_t page, const uint8_t *buffer) |
Write a 4 byte page to the active MIFARE Ultralight PICC. | |
int | mfrc522_mifare_decrement (mfrc522_t *dev, uint8_t block_addr, int32_t delta) |
Subtract the operand from the value of the addressed block, and store the result in the Transfer Buffer. | |
int | mfrc522_mifare_increment (mfrc522_t *dev, uint8_t block_addr, int32_t delta) |
Add the operand to the value of the addressed block, and store the result in the Transfer Buffer. | |
int | mfrc522_mifare_restore (mfrc522_t *dev, uint8_t block_addr) |
Copies the value of the addressed block into the Transfer Buffer. | |
int | mfrc522_mifare_transfer (mfrc522_t *dev, uint8_t block_addr) |
Write the value from the Transfer Buffer into destination block. | |
int | mfrc522_mifare_get_value (mfrc522_t *dev, uint8_t block_addr, int32_t *value) |
Helper routine to read the current value from a Value Block. | |
int | mfrc522_mifare_set_value (mfrc522_t *dev, uint8_t block_addr, int32_t value) |
Helper routine to write a specific value into a Value Block. | |
int | mfrc522_pcd_ntag216_auth (mfrc522_t *dev, const uint8_t *password, uint8_t p_ack[]) |
Authenticate with a NTAG216. | |
int | mfrc522_pcd_mifare_transceive (mfrc522_t *dev, const uint8_t *send_data, uint8_t send_len, bool accept_timeout) |
Wrapper for MIFARE protocol communication. | |
mfrc522_picc_type_t | mfrc522_picc_get_type (uint8_t sak) |
Translates the SAK (Select Acknowledge) to a PICC type. | |
const char * | mfrc522_picc_get_type_string (mfrc522_picc_type_t picc_type) |
Returns the name for the PICC type. | |
void | mfrc522_mifare_set_access_bits (uint8_t *access_bit_buffer, uint8_t g0, uint8_t g1, uint8_t g2, uint8_t g3) |
Calculates the bit pattern needed for the specified access bits. | |
int | mfrc522_mifare_open_uid_backdoor (mfrc522_t *dev) |
Performs the "magic sequence" needed to get Chinese UID changeable Mifare cards to allow writing to sector 0, where the card UID is stored. | |
int | mfrc522_mifare_set_uid (mfrc522_t *dev, mfrc522_uid_t *uid, const uint8_t *new_uid, uint8_t new_uid_size) |
Read entire block 0, including all manufacturer data, and overwrites that block with the new UID, a freshly calculated BCC, and the original manufacturer data. | |
int | mfrc522_mifare_unbrick_uid_sector (mfrc522_t *dev) |
Reset entire sector 0 to zeroes, so the card can be read again by readers. | |
bool | mfrc522_picc_is_new_card_present (mfrc522_t *dev) |
Checks whether a new card could be detected. | |
int | mfrc522_picc_read_card_serial (mfrc522_t *dev, mfrc522_uid_t *uid) |
Simple wrapper around mfrc522_picc_select() | |
void | mfrc522_pcd_dump_version_to_serial (mfrc522_t *dev) |
Dump debug info about the connected PCD to stdout. | |
void | mfrc522_picc_dump_to_serial (mfrc522_t *dev, mfrc522_uid_t *uid) |
Dump debug info about the selected PICC to stdout. | |
void | mfrc522_picc_dump_details_to_serial (mfrc522_uid_t *uid) |
Dump card info (UID, SAK, Type) about the selected PICC to stdout. | |
void | mfrc522_picc_dump_mifare_classic_to_serial (mfrc522_t *dev, mfrc522_uid_t *uid, mfrc522_picc_type_t picc_type, mfrc522_mifare_key_t *key) |
Dump memory contents of a MIFARE Classic PICC to stdout. | |
void | mfrc522_picc_dump_mifare_classic_sector_to_serial (mfrc522_t *dev, mfrc522_uid_t *uid, mfrc522_mifare_key_t *key, uint8_t sector) |
Dump memory contents of a sector of a MIFARE Classic PICC to stdout. | |
void | mfrc522_picc_dump_mifare_ultralight_to_serial (mfrc522_t *dev) |
Dump memory contents of a MIFARE Ultralight PICC to stdout. | |
bool | mfrc522_pcd_perform_self_test (mfrc522_t *dev) |
Perform a self-test of the MFRC522. | |
#define MFRC522_MF_ACK 0xA |
#define MFRC522_MF_KEY_SIZE 6 |
#define MFRC522_UID_MAX_SIZE 10 |
enum mfrc522_picc_type_t |
PICC types.
int mfrc522_mifare_decrement | ( | mfrc522_t * | dev, |
uint8_t | block_addr, | ||
int32_t | delta | ||
) |
Subtract the operand from the value of the addressed block, and store the result in the Transfer Buffer.
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.
[in] | dev | Device descriptor of the MFRC522 |
[in] | block_addr | Block (0-0xff) number |
[in] | delta | Number to be subtracted from the value of block block_addr |
0 | on success |
-ECANCELED | on internal error |
int mfrc522_mifare_get_value | ( | mfrc522_t * | dev, |
uint8_t | block_addr, | ||
int32_t * | value | ||
) |
Helper routine to read the current value from a Value Block.
[in] | dev | Device descriptor of the MFRC522 |
[in] | block_addr | Block (0x00-0xff) number |
[out] | value | Current value of the Value Block |
0 | on success |
-ECONNABORTED | when a collision occurred |
-EIO | on communication error |
-ENOBUFS | when a buffer is too small to receive data |
-ETIMEDOUT | on timeout |
int mfrc522_mifare_increment | ( | mfrc522_t * | dev, |
uint8_t | block_addr, | ||
int32_t | delta | ||
) |
Add the operand to the value of the addressed block, and store the result in the Transfer Buffer.
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.
[in] | dev | Device descriptor of the MFRC522 |
[in] | block_addr | Block (0-0xff) number |
[in] | delta | Number to be added to the value of block block_addr |
0 | on success |
-ECANCELED | on internal error |
int mfrc522_mifare_open_uid_backdoor | ( | mfrc522_t * | dev | ) |
Performs the "magic sequence" needed to get Chinese UID changeable Mifare cards to allow writing to sector 0, where the card UID is stored.
[in] | dev | Device descriptor of the MFRC522 |
0 | on success |
-ECONNABORTED | when a collision occurred |
-EIO | on communication error |
-ENOBUFS | when a buffer is too small to receive data |
-ETIMEDOUT | on timeout |
int mfrc522_mifare_read | ( | mfrc522_t * | dev, |
uint8_t | block_addr, | ||
uint8_t * | buffer, | ||
uint8_t * | buffer_size | ||
) |
Read 16 bytes (+ 2 bytes CRC_A) from the active PICC.
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 block_addr is 03h then pages 03h, 04h, 05h, 06h are returned. A roll-back is implemented: If block_addr 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.
[in] | dev | Device descriptor of the MFRC522 |
[in] | block_addr | MIFARE Classic: The block (0-0xff) number. MIFARE Ultralight: The first page to return data from. |
[out] | buffer | Buffer to store the data in |
[in,out] | buffer_size | Buffer size, at least 18 bytes. Also number of bytes returned on success. |
0 | on success |
-ECONNABORTED | when a collision occurred |
-EIO | on communication error |
-ENOBUFS | when a buffer is too small to receive data |
-ETIMEDOUT | on timeout |
int mfrc522_mifare_restore | ( | mfrc522_t * | dev, |
uint8_t | block_addr | ||
) |
Copies the value of the addressed block into the Transfer Buffer.
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, i.e. with access bits [C1 C2 C3] = [110] or [001]. Use mfrc522_mifare_transfer() to store the result in a block.
[in] | dev | Device descriptor of the MFRC522 |
[in] | block_addr | Block (0-0xff) number |
0 | on success |
-ECANCELED | on internal error |
void mfrc522_mifare_set_access_bits | ( | uint8_t * | access_bit_buffer, |
uint8_t | g0, | ||
uint8_t | g1, | ||
uint8_t | g2, | ||
uint8_t | g3 | ||
) |
Calculates the bit pattern needed for the specified access bits.
In the [C1 C2 C3] tuples C1 is MSB (=4) and C3 is LSB (=1).
[out] | access_bit_buffer | Pointer to byte 6, 7 and 8 in the sector trailer. Bytes [0..2] will be set. |
[in] | g0 | Access bits [C1 C2 C3] for block 0 (for sectors 0-31) or blocks 0-4 (for sectors 32-39) |
[in] | g1 | Access bits [C1 C2 C3] for block 1 (for sectors 0-31) or blocks 5-9 (for sectors 32-39) |
[in] | g2 | Access bits [C1 C2 C3] for block 2 (for sectors 0-31) or blocks 10-14 (for sectors 32-39) |
[in] | g3 | Access bits [C1 C2 C3] for the sector trailer, block 3 (for sectors 0-31) or block 15 (for sectors 32-39) |
int mfrc522_mifare_set_uid | ( | mfrc522_t * | dev, |
mfrc522_uid_t * | uid, | ||
const uint8_t * | new_uid, | ||
uint8_t | new_uid_size | ||
) |
Read entire block 0, including all manufacturer data, and overwrites that block with the new UID, a freshly calculated BCC, and the original manufacturer data.
[in] | dev | Device descriptor of the MFRC522 |
[in] | uid | PICC's UID |
[in] | new_uid | New UID to set on PICC |
[in] | new_uid_size | Size of new UID |
0 | on success |
-ECANCELED | on internal error |
-ECONNABORTED | when a collision occurred |
-EINVAL | on invalid argument |
-EIO | on communication error |
-ENOBUFS | when a buffer is too small to receive data |
-ETIMEDOUT | on timeout |
int mfrc522_mifare_set_value | ( | mfrc522_t * | dev, |
uint8_t | block_addr, | ||
int32_t | value | ||
) |
Helper routine to write a specific value into a Value Block.
[in] | dev | Device descriptor of the MFRC522 |
[in] | block_addr | Block (0x00-0xff) number |
[in] | value | New value of the Value Block |
0 | on success |
-ECONNABORTED | when a collision occurred |
-EINVAL | on invalid argument |
-EIO | on communication error |
-ENOBUFS | when a buffer is too small to receive data |
-ETIMEDOUT | on timeout |
int mfrc522_mifare_transfer | ( | mfrc522_t * | dev, |
uint8_t | block_addr | ||
) |
Write the value from the Transfer Buffer into destination block.
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].
[in] | dev | Device descriptor of the MFRC522 |
[in] | block_addr | Block (0-0xff) number |
0 | on success |
-ECONNABORTED | when a collision occurred |
-EINVAL | on invalid argument |
-EIO | on communication error |
-ENOBUFS | when a buffer is too small to receive data |
-ETIMEDOUT | on timeout |
int mfrc522_mifare_ultralight_write | ( | mfrc522_t * | dev, |
uint8_t | page, | ||
const uint8_t * | buffer | ||
) |
Write a 4 byte page to the active MIFARE Ultralight PICC.
[in] | dev | Device descriptor of the MFRC522 |
[in] | page | The page (2-15) to write to |
[in] | buffer | The 4 bytes to write to the PICC |
0 | on success |
-ECONNABORTED | when a collision occurred |
-EINVAL | on invalid argument |
-EIO | on communication error |
-ENOBUFS | when a buffer is too small to receive data |
-ETIMEDOUT | on timeout |
int mfrc522_mifare_unbrick_uid_sector | ( | mfrc522_t * | dev | ) |
Reset entire sector 0 to zeroes, so the card can be read again by readers.
[in] | dev | Device descriptor of the MFRC522 |
0 | on success |
-ECONNABORTED | when a collision occurred |
-EINVAL | on invalid argument |
-EIO | on communication error |
-ENOBUFS | when a buffer is too small to receive data |
-ETIMEDOUT | on timeout |
int mfrc522_mifare_write | ( | mfrc522_t * | dev, |
uint8_t | block_addr, | ||
const uint8_t * | buffer, | ||
uint8_t | buffer_size | ||
) |
Write 16 bytes to the active PICC.
For MIFARE Ultralight the operation 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.
[in] | dev | Device descriptor of the MFRC522 |
[in] | block_addr | MIFARE Classic: The block (0-0xff) number. MIFARE Ultralight: The page (2-15) to write to. |
[in] | buffer | The 16 bytes to write to the PICC |
[in] | buffer_size | Buffer size, must be at least 16 bytes. Exactly 16 bytes are written. |
0 | on success |
-ECONNABORTED | when a collision occurred |
-EINVAL | on invalid argument |
-EIO | on communication error |
-ENOBUFS | when a buffer is too small to receive data |
-ETIMEDOUT | on timeout |
void mfrc522_pcd_antenna_off | ( | mfrc522_t * | dev | ) |
Turns the antenna off by disabling pins TX1 and TX2.
[in] | dev | Device descriptor of the MFRC522 |
void mfrc522_pcd_antenna_on | ( | mfrc522_t * | dev | ) |
Turns the antenna on by enabling pins TX1 and TX2.
After a reset these pins are disabled.
[in] | dev | Device descriptor of the MFRC522 |
int mfrc522_pcd_authenticate | ( | mfrc522_t * | dev, |
mfrc522_picc_command_t | command, | ||
uint8_t | block_addr, | ||
const mfrc522_mifare_key_t * | key, | ||
const mfrc522_uid_t * | uid | ||
) |
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 - i.e. in state ACTIVE(*) - before calling this function. Remember to call mfrc522_pcd_stop_crypto1() after communicating with the authenticated PICC - otherwise no new communications can start.
All keys are set to FFFFFFFFFFFFh at chip delivery.
[in] | dev | Device descriptor of the MFRC522 |
[in] | command | MFRC522_PICC_CMD_MF_AUTH_KEY_A or PICC_CMD_MF_AUTH_KEY_B |
[in] | block_addr | Block number. See numbering in the comments in the .h file. |
[in] | key | Crypteo1 key to use (6 bytes) |
[in] | uid | PICC's UID |
0 | on success |
-ECONNABORTED | when a collision occurred |
-EIO | on communication error |
-ENOBUFS | when a buffer is too small to receive data |
-ETIMEDOUT | on timeout |
int mfrc522_pcd_calculate_crc | ( | mfrc522_t * | dev, |
const uint8_t * | data, | ||
uint8_t | length, | ||
uint8_t * | result | ||
) |
Use the CRC coprocessor in the MFRC522 to calculate a CRC_A.
[in] | dev | Device descriptor of the MFRC522 |
[in] | data | Data to transfer to the FIFO for CRC calculation |
[in] | length | Number of bytes to transfer |
[out] | result | Result buffer. Result is written to result[0..1], low byte first. |
0 | on success |
-ETIMEDOUT | on timeout |
void mfrc522_pcd_clear_register_bitmask | ( | mfrc522_t * | dev, |
mfrc522_pcd_register_t | reg, | ||
uint8_t | mask | ||
) |
Clears the bits given in mask from register reg.
[in] | dev | Device descriptor of the MFRC522 |
[in] | reg | Register to write to |
[in] | mask | Bitmask with the bits to clear |
int mfrc522_pcd_communicate_with_picc | ( | mfrc522_t * | dev, |
mfrc522_pcd_command_t | command, | ||
uint8_t | wait_irq, | ||
const uint8_t * | send_data, | ||
uint8_t | send_len, | ||
uint8_t * | back_data, | ||
uint8_t * | back_len, | ||
uint8_t * | valid_bits, | ||
uint8_t | rx_align, | ||
bool | check_crc | ||
) |
Transfers data to MFRC522's FIFO, executes a command, waits for completion and transfers data back from the FIFO.
[in] | dev | Device descriptor of the MFRC522 |
[in] | command | The command to execute |
[in] | wait_irq | The bits in the ComIrqReg register that signals successful completion of the command |
[in] | send_data | Data to transfer to the FIFO |
[in] | send_len | Number of bytes to transfer to the FIFO |
[out] | back_data | Buffer if data should be read back after executing the command, otherwise NULL |
[in,out] | back_len | Max number of bytes to write to *back_data. Returns number of bytes returned. |
[in,out] | valid_bits | Number of valid bits in the last byte. 0 for 8 valid bits. |
[in] | rx_align | Defines the bit position in back_data[0] for the first bit received |
[in] | check_crc | True => The last two bytes of the response is assumed to be a CRC_A that must be validated |
0 | on success |
-ECONNABORTED | when a collision occurred |
-EIO | on communication error |
-ENOBUFS | when a buffer is too small to receive data |
-ETIMEDOUT | on timeout |
void mfrc522_pcd_dump_version_to_serial | ( | mfrc522_t * | dev | ) |
Dump debug info about the connected PCD to stdout.
[in] | dev | Device descriptor of the MFRC522 |
mfrc522_pcd_rx_gain_t mfrc522_pcd_get_antenna_gain | ( | mfrc522_t * | dev | ) |
Get the current MFRC522 receiver gain.
[in] | dev | Device descriptor of the MFRC522 |
int mfrc522_pcd_init | ( | mfrc522_t * | dev, |
const mfrc522_params_t * | params | ||
) |
Initialization.
[out] | dev | Device descriptor of the MFRC522 |
[in] | params | Parameters for device initialization |
0 | Success |
-EINVAL | Invalid CS pin/line |
-ENXIO | Invalid device |
int mfrc522_pcd_mifare_transceive | ( | mfrc522_t * | dev, |
const uint8_t * | send_data, | ||
uint8_t | send_len, | ||
bool | accept_timeout | ||
) |
Wrapper for MIFARE protocol communication.
Adds CRC_A, executes the Transceive command and checks that the response is MFRC522_MF_ACK or a timeout.
[in] | dev | Device descriptor of the MFRC522 |
[in] | send_data | Data to transfer to the FIFO. Do NOT include the CRC_A. |
[in] | send_len | Number of bytes in send_data |
[in] | accept_timeout | If true, then a timeout is also a success |
0 | on success |
-ECONNABORTED | when a collision occurred |
-EINVAL | on invalid argument |
-EIO | on communication error |
-ENOBUFS | when a buffer is too small to receive data |
-ETIMEDOUT | on timeout |
int mfrc522_pcd_ntag216_auth | ( | mfrc522_t * | dev, |
const uint8_t * | password, | ||
uint8_t | p_ack[] | ||
) |
Authenticate with a NTAG216.
[in] | dev | Device descriptor of the MFRC522 |
[in] | password | Password (must have a size of exactly 4 bytes) |
[out] | p_ack | result (must have a size of exactly 2 bytes) |
0 | on success |
-ECONNABORTED | when a collision occurred |
-EIO | on communication error |
-ENOBUFS | when a buffer is too small to receive data |
-ETIMEDOUT | on timeout |
bool mfrc522_pcd_perform_self_test | ( | mfrc522_t * | dev | ) |
Perform a self-test of the MFRC522.
(See section 16.1.1)
[in] | dev | Device descriptor of the MFRC522 |
void mfrc522_pcd_reset | ( | mfrc522_t * | dev | ) |
Performs a soft reset on the MFRC522 chip and waits for it to be ready again.
[in] | dev | Device descriptor of the MFRC522 |
void mfrc522_pcd_set_antenna_gain | ( | mfrc522_t * | dev, |
mfrc522_pcd_rx_gain_t | rx_gain | ||
) |
Set the MFRC522 receiver gain.
[in] | dev | Device descriptor of the MFRC522 |
[in] | rx_gain | Antenna gain |
void mfrc522_pcd_set_register_bitmask | ( | mfrc522_t * | dev, |
mfrc522_pcd_register_t | reg, | ||
uint8_t | mask | ||
) |
Sets the bits given in mask in register reg.
[in] | dev | Device descriptor of the MFRC522 |
[in] | reg | Register to write to |
[in] | mask | Bitmask with the bits to set |
void mfrc522_pcd_soft_power_down | ( | mfrc522_t * | dev | ) |
Set the MFRC522 to soft power-down mode.
[in] | dev | Device descriptor of the MFRC522 |
void mfrc522_pcd_soft_power_up | ( | mfrc522_t * | dev | ) |
Set the MFRC522 to leave soft power-down mode.
[in] | dev | Device descriptor of the MFRC522 |
void mfrc522_pcd_stop_crypto1 | ( | mfrc522_t * | dev | ) |
Used to exit the PCD from its authenticated state.
[in] | dev | Device descriptor of the MFRC522 |
int mfrc522_pcd_transceive_data | ( | mfrc522_t * | dev, |
const uint8_t * | send_data, | ||
uint8_t | send_len, | ||
uint8_t * | back_data, | ||
uint8_t * | back_len, | ||
uint8_t * | valid_bits, | ||
uint8_t | rx_align, | ||
bool | check_crc | ||
) |
Executes MFRC522_CMD_TRANSCEIVE.
[in] | dev | Device descriptor of the MFRC522 |
[in] | send_data | Data to transfer to the FIFO |
[in] | send_len | Number of bytes to transfer to the FIFO |
[out] | back_data | Buffer if data should be read back after executing the command, otherwise NULL |
[in,out] | back_len | Max number of bytes to write to *back_data. Returns number of bytes returned. |
[in,out] | valid_bits | Number of valid bits in the last byte. 0 for 8 valid bits |
[in] | rx_align | Defines the bit position in back_data[0] for the first bit received |
[in] | check_crc | If true, the last two bytes of the response are assumed to be a CRC_A that must be validated |
0 | on success |
-ECONNABORTED | when a collision occurred |
-EIO | on communication error |
-ENOBUFS | when a buffer is too small to receive data |
-ETIMEDOUT | on timeout |
void mfrc522_picc_dump_details_to_serial | ( | mfrc522_uid_t * | uid | ) |
Dump card info (UID, SAK, Type) about the selected PICC to stdout.
[in] | uid | PICC's UID |
void mfrc522_picc_dump_mifare_classic_sector_to_serial | ( | mfrc522_t * | dev, |
mfrc522_uid_t * | uid, | ||
mfrc522_mifare_key_t * | key, | ||
uint8_t | sector | ||
) |
Dump memory contents of a sector of a MIFARE Classic PICC to stdout.
Uses mfrc522_pcd_authenticate(), mfrc522_mifare_read() and mfrc522_pcd_stop_crypto1(). Always uses MFRC522_PICC_CMD_MF_AUTH_KEY_A because only Key A can always read the sector trailer access bits.
[in] | dev | Device descriptor of the MFRC522 |
[in] | uid | PICC's UID |
[in] | key | Key A for the sector. |
[in] | sector | The sector to dump, 0..39 |
void mfrc522_picc_dump_mifare_classic_to_serial | ( | mfrc522_t * | dev, |
mfrc522_uid_t * | uid, | ||
mfrc522_picc_type_t | picc_type, | ||
mfrc522_mifare_key_t * | key | ||
) |
Dump memory contents of a MIFARE Classic PICC to stdout.
[in] | dev | Device descriptor of the MFRC522 |
[in] | uid | PICC's UID |
[in] | picc_type | PICC type enum |
[in] | key | Key A used for all sectors. |
void mfrc522_picc_dump_mifare_ultralight_to_serial | ( | mfrc522_t * | dev | ) |
Dump memory contents of a MIFARE Ultralight PICC to stdout.
[in] | dev | Device descriptor of the MFRC522 |
void mfrc522_picc_dump_to_serial | ( | mfrc522_t * | dev, |
mfrc522_uid_t * | uid | ||
) |
Dump debug info about the selected PICC to stdout.
[in] | dev | Device descriptor of the MFRC522 |
[in] | uid | PICC's UID |
mfrc522_picc_type_t mfrc522_picc_get_type | ( | uint8_t | sak | ) |
Translates the SAK (Select Acknowledge) to a PICC type.
[in] | sak | The SAK byte returned from mfrc522_picc_select() |
const char * mfrc522_picc_get_type_string | ( | mfrc522_picc_type_t | picc_type | ) |
Returns the name for the PICC type.
[in] | picc_type | PICC type enum |
int mfrc522_picc_halt_a | ( | mfrc522_t * | dev | ) |
Instructs a PICC in state ACTIVE(*) to go to state HALT.
[in] | dev | Device descriptor of the MFRC522 |
0 | on success |
-EIO | on communication error |
-ETIMEDOUT | on timeout |
bool mfrc522_picc_is_new_card_present | ( | mfrc522_t * | dev | ) |
Checks whether a new card could be detected.
[in] | dev | Device descriptor of the MFRC522 |
int mfrc522_picc_read_card_serial | ( | mfrc522_t * | dev, |
mfrc522_uid_t * | uid | ||
) |
Simple wrapper around mfrc522_picc_select()
[in] | dev | Device descriptor of the MFRC522 |
[in] | uid | PICC's UID |
0 | on success |
-ECANCELED | on internal error |
-ECONNABORTED | when a collision occurred |
-EINVAL | on invalid argument |
-EIO | on communication error |
-ENOBUFS | when a buffer is too small to receive data |
-ETIMEDOUT | on timeout |
int mfrc522_picc_reqa_or_wupa | ( | mfrc522_t * | dev, |
mfrc522_picc_command_t | command, | ||
uint8_t * | buffer_atqa, | ||
uint8_t * | buffer_size | ||
) |
Transmits REQA (Type A) or WUPA (Type A) commands.
[in] | dev | Device descriptor of the MFRC522 |
[in] | command | Command to send - MFRC522_PICC_CMD_ISO_14443_REQA or MFRC522_PICC_CMD_ISO_14443_WUPA |
[out] | buffer_atqa | Buffer to store the ATQA in |
[in,out] | buffer_size | Buffer size, at least two bytes. Also number of bytes returned on success. |
0 | on success |
-ECONNABORTED | when a collision occurred |
-EINVAL | if command was neither MFRC522_PICC_CMD_ISO_14443_REQA nor MFRC522_PICC_CMD_ISO_14443_WUPA |
-EIO | on communication error |
-ENOBUFS | when a buffer is too small to receive data |
-ETIMEDOUT | on timeout |
int mfrc522_picc_request_a | ( | mfrc522_t * | dev, |
uint8_t * | buffer_atqa, | ||
uint8_t * | buffer_size | ||
) |
Transmits REQA, Type A.
Invites PICCs in state IDLE to go to READY and prepare for anti-collision or selection. 7 bit frame.
[in] | dev | Device descriptor of the MFRC522 |
[out] | buffer_atqa | Buffer to store the ATQA in |
[in,out] | buffer_size | Buffer size, at least two bytes. Also number of bytes returned on success. |
0 | on success |
-ECONNABORTED | when a collision occurred |
-EINVAL | on invalid argument |
-EIO | on communication error |
-ENOBUFS | when a buffer is too small to receive data |
-ETIMEDOUT | on timeout |
int mfrc522_picc_select | ( | mfrc522_t * | dev, |
mfrc522_uid_t * | uid, | ||
uint8_t | valid_bits | ||
) |
Transmits SELECT/ANTICOLLISION commands to select a single PICC.
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?
[in] | dev | Device descriptor of the MFRC522 |
[in,out] | uid | Normally output, but can also be used to supply a known UID |
[in] | valid_bits | Number of known UID bits supplied in *uid. Normally 0. If set you must also supply uid->size. |
0 | on success |
-ECANCELED | on internal error |
-ECONNABORTED | when a collision occurred |
-EINVAL | on invalid argument |
-EIO | on communication error |
-ENOBUFS | when a buffer is too small to receive data |
-ETIMEDOUT | on timeout |
int mfrc522_picc_wakeup_a | ( | mfrc522_t * | dev, |
uint8_t * | buffer_atqa, | ||
uint8_t * | buffer_size | ||
) |
Transmits WUPA, Type A.
Invites PICCs in state IDLE and HALT to go to READY(*) and prepare for anti-collision or selection. 7 bit frame.
[in] | dev | Device descriptor of the MFRC522 |
[out] | buffer_atqa | Buffer to store the ATQA in |
[in,out] | buffer_size | Buffer size, at least two bytes. Also number of bytes returned on success. |
0 | on success |
-ECONNABORTED | when a collision occurred |
-EINVAL | on invalid argument |
-EIO | on communication error |
-ENOBUFS | when a buffer is too small to receive data |
-ETIMEDOUT | on timeout |