Loading...
Searching...
No Matches
mfrc522.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2021 Freie Universität Berlin
3 *
4 * This file is subject to the terms and conditions of the GNU Lesser
5 * General Public License v2.1. See the file LICENSE in the top level
6 * directory for more details.
7 */
8
38#ifndef MFRC522_H
39#define MFRC522_H
40
41#ifdef __cplusplus
42extern "C"
43{
44#endif
45
46#include "stdbool.h"
47
48#include "periph/spi.h"
49
50#include "mfrc522_regs.h"
51
55#define MFRC522_UID_MAX_SIZE 10
56
60#define MFRC522_MF_ACK 0xA
61
65#define MFRC522_MF_KEY_SIZE 6
66
70typedef struct {
71 spi_t spi_dev;
73 gpio_t sck_pin;
74 gpio_t miso_pin;
75 gpio_t mosi_pin;
76 gpio_t cs_pin;
77 gpio_t rst_pin;
79
83typedef struct {
85} mfrc522_t;
86
90typedef struct {
91 uint8_t size;
92 uint8_t uid_byte[MFRC522_UID_MAX_SIZE];
93 uint8_t sak;
96
100typedef struct {
101 uint8_t key_byte[MFRC522_MF_KEY_SIZE];
103
121
130
139
152int mfrc522_pcd_calculate_crc(mfrc522_t *dev, const uint8_t *data, uint8_t length, uint8_t *result);
153
165
173
181
188
197
205
218
225
252 const uint8_t *send_data, uint8_t send_len,
253 uint8_t *back_data, uint8_t *back_len,
254 uint8_t *valid_bits, uint8_t rx_align, bool check_crc);
255
286 uint8_t wait_irq,
287 const uint8_t *send_data, uint8_t send_len,
288 uint8_t *back_data, uint8_t *back_len,
289 uint8_t *valid_bits, uint8_t rx_align, bool check_crc);
290
310int mfrc522_picc_request_a(mfrc522_t *dev, uint8_t *buffer_atqa, uint8_t *buffer_size);
311
331int mfrc522_picc_wakeup_a(mfrc522_t *dev, uint8_t *buffer_atqa, uint8_t *buffer_size);
332
355 uint8_t *buffer_atqa, uint8_t *buffer_size);
356
392int mfrc522_picc_select(mfrc522_t *dev, mfrc522_uid_t *uid, uint8_t valid_bits);
393
404
432 uint8_t block_addr, const mfrc522_mifare_key_t *key,
433 const mfrc522_uid_t *uid);
434
444
474int mfrc522_mifare_read(mfrc522_t *dev, uint8_t block_addr, uint8_t *buffer, uint8_t *buffer_size);
475
501int mfrc522_mifare_write(mfrc522_t *dev, uint8_t block_addr,
502 const uint8_t *buffer, uint8_t buffer_size);
503
518int mfrc522_mifare_ultralight_write(mfrc522_t *dev, uint8_t page, const uint8_t *buffer);
519
538int mfrc522_mifare_decrement(mfrc522_t *dev, uint8_t block_addr, int32_t delta);
539
557int mfrc522_mifare_increment(mfrc522_t *dev, uint8_t block_addr, int32_t delta);
558
574int mfrc522_mifare_restore(mfrc522_t *dev, uint8_t block_addr);
575
594int mfrc522_mifare_transfer(mfrc522_t *dev, uint8_t block_addr);
595
613int mfrc522_mifare_get_value(mfrc522_t *dev, uint8_t block_addr, int32_t *value);
614
633int mfrc522_mifare_set_value(mfrc522_t *dev, uint8_t block_addr, int32_t value);
634
650int mfrc522_pcd_ntag216_auth(mfrc522_t *dev, const uint8_t *password, uint8_t p_ack[]);
651
671 const uint8_t *send_data, uint8_t send_len,
672 bool accept_timeout);
673
682
691
707void mfrc522_mifare_set_access_bits(uint8_t *access_bit_buffer,
708 uint8_t g0, uint8_t g1, uint8_t g2, uint8_t g3);
709
730
754 const uint8_t *new_uid, uint8_t new_uid_size);
755
769
781
800
807
818
825
837 mfrc522_picc_type_t picc_type,
839
853 mfrc522_mifare_key_t *key, uint8_t sector);
854
861
872
873#ifdef __cplusplus
874}
875#endif
876
877#endif /* MFRC522_H */
spi_clk_t
Definition periph_cpu.h:352
#define MFRC522_UID_MAX_SIZE
Maximum size for uid byte array.
Definition mfrc522.h:55
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.
void mfrc522_pcd_antenna_off(mfrc522_t *dev)
Turns the antenna off by disabling pins TX1 and TX2.
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_mifare_open_uid_backdoor(mfrc522_t *dev)
Performs the "magic sequence" needed to get Chinese UID changeable Mifare cards to allow writing to s...
int mfrc522_pcd_init(mfrc522_t *dev, const mfrc522_params_t *params)
Initialization.
void mfrc522_pcd_soft_power_up(mfrc522_t *dev)
Set the MFRC522 to leave soft power-down mode.
mfrc522_picc_type_t
PICC types.
Definition mfrc522.h:107
int mfrc522_mifare_transfer(mfrc522_t *dev, uint8_t block_addr)
Write the value from the Transfer Buffer into destination block.
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.
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.
mfrc522_pcd_rx_gain_t mfrc522_pcd_get_antenna_gain(mfrc522_t *dev)
Get the current MFRC522 receiver gain.
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_mifare_unbrick_uid_sector(mfrc522_t *dev)
Reset entire sector 0 to zeroes, so the card can be read again by readers.
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 fr...
const char * mfrc522_picc_get_type_string(mfrc522_picc_type_t picc_type)
Returns the name for the PICC type.
int mfrc522_picc_read_card_serial(mfrc522_t *dev, mfrc522_uid_t *uid)
Simple wrapper around mfrc522_picc_select()
void mfrc522_pcd_reset(mfrc522_t *dev)
Performs a soft reset on the MFRC522 chip and waits for it to be ready again.
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_picc_wakeup_a(mfrc522_t *dev, uint8_t *buffer_atqa, uint8_t *buffer_size)
Transmits WUPA, Type A.
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.
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.
#define MFRC522_MF_KEY_SIZE
A Mifare Crypto1 key is 6 bytes.
Definition mfrc522.h:65
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.
void mfrc522_pcd_soft_power_down(mfrc522_t *dev)
Set the MFRC522 to soft power-down mode.
int mfrc522_mifare_restore(mfrc522_t *dev, uint8_t block_addr)
Copies the value of the addressed block into the Transfer Buffer.
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_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 Buff...
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.
void mfrc522_picc_dump_to_serial(mfrc522_t *dev, mfrc522_uid_t *uid)
Dump debug info about the selected PICC to stdout.
int mfrc522_picc_halt_a(mfrc522_t *dev)
Instructs a PICC in state ACTIVE(*) to go to state HALT.
void mfrc522_picc_dump_mifare_ultralight_to_serial(mfrc522_t *dev)
Dump memory contents of a MIFARE Ultralight PICC to stdout.
void mfrc522_pcd_dump_version_to_serial(mfrc522_t *dev)
Dump debug info about the connected PCD to stdout.
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,...
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.
bool mfrc522_pcd_perform_self_test(mfrc522_t *dev)
Perform a self-test of the MFRC522.
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.
mfrc522_picc_type_t mfrc522_picc_get_type(uint8_t sak)
Translates the SAK (Select Acknowledge) to a PICC type.
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.
bool mfrc522_picc_is_new_card_present(mfrc522_t *dev)
Checks whether a new card could be detected.
void mfrc522_pcd_antenna_on(mfrc522_t *dev)
Turns the antenna on by enabling pins TX1 and TX2.
int mfrc522_pcd_ntag216_auth(mfrc522_t *dev, const uint8_t *password, uint8_t p_ack[])
Authenticate with a NTAG216.
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_pcd_set_antenna_gain(mfrc522_t *dev, mfrc522_pcd_rx_gain_t rx_gain)
Set the MFRC522 receiver gain.
int mfrc522_picc_request_a(mfrc522_t *dev, uint8_t *buffer_atqa, uint8_t *buffer_size)
Transmits REQA, Type A.
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.
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_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.
@ MFRC522_PICC_TYPE_ISO_14443_4
PICC compliant with ISO/IEC 14443-4.
Definition mfrc522.h:108
@ MFRC522_PICC_TYPE_MIFARE_UL
MIFARE Ultralight or Ultralight C.
Definition mfrc522.h:113
@ MFRC522_PICC_TYPE_MIFARE_4K
MIFARE Classic protocol, 4KB.
Definition mfrc522.h:112
@ MFRC522_PICC_TYPE_MIFARE_PLUS
MIFARE Plus.
Definition mfrc522.h:114
@ MFRC522_PICC_TYPE_MIFARE_DESFIRE
MIFARE DESFire.
Definition mfrc522.h:115
@ MFRC522_PICC_TYPE_MIFARE_MINI
MIFARE Classic protocol, 320 bytes.
Definition mfrc522.h:110
@ MFRC522_PICC_TYPE_TNP3XXX
Only mentioned in NXP AN 10833 MIFARE Type Identification Procedure.
Definition mfrc522.h:116
@ MFRC522_PICC_TYPE_UNKNOWN
unknown type
Definition mfrc522.h:119
@ MFRC522_PICC_TYPE_MIFARE_1K
MIFARE Classic protocol, 1KB.
Definition mfrc522.h:111
@ MFRC522_PICC_TYPE_ISO_18092
PICC compliant with ISO/IEC 18092 (NFC)
Definition mfrc522.h:109
@ MFRC522_PICC_TYPE_NOT_COMPLETE
SAK indicates UID is not complete.
Definition mfrc522.h:118
Register definitions for the MFRC522 controller.
mfrc522_pcd_command_t
Command definitions.
mfrc522_picc_command_t
PICC command definitions.
mfrc522_pcd_register_t
Register definitions.
mfrc522_pcd_rx_gain_t
Receiver gain definitions.
Low-level SPI peripheral driver interface definition.
A struct used for passing a MIFARE Crypto1 key.
Definition mfrc522.h:100
MFRC522 device initialization parameters.
Definition mfrc522.h:70
spi_t spi_dev
SPI bus the controller is connected to.
Definition mfrc522.h:71
gpio_t miso_pin
MISO pin.
Definition mfrc522.h:74
gpio_t sck_pin
SCK pin.
Definition mfrc522.h:73
spi_clk_t spi_clk
SPI clock speed.
Definition mfrc522.h:72
gpio_t mosi_pin
MOSI pin.
Definition mfrc522.h:75
gpio_t rst_pin
Reset pin.
Definition mfrc522.h:77
gpio_t cs_pin
Slave select pin.
Definition mfrc522.h:76
MFRC522 device data structure type.
Definition mfrc522.h:83
mfrc522_params_t params
Device initialization parameters.
Definition mfrc522.h:84
A struct used for passing the UID of a PICC.
Definition mfrc522.h:90
uint8_t sak
The SAK (Select acknowledge) byte returned from the PICC after successful selection.
Definition mfrc522.h:93
uint8_t size
Number of bytes in the UID.
Definition mfrc522.h:91