一、簡介
MF RC522 是應(yīng)用于 13.56MHz 非接觸式通信中高集成度讀寫卡系列芯片中的一員损敷。是 NXP 公司針對“三表”應(yīng)用推出的一款低電壓秉版、低成本袒餐、體積小的非接觸式讀寫卡芯片,是智能儀表和便攜式手持設(shè)備研發(fā)的較好選擇漾肮。
MFRC522數(shù)據(jù)手冊: https://pan.baidu.com/s/10v68Z7sCFFSwPgrZ2eHtXw?pwd=d4fw 提取碼:d4fw
二厂抖、硬件連接
功能口 | 引腳 |
---|---|
MISO | 19 |
MOSI | 23 |
SCLK | 18 |
CS | 5 |
RST | 25 |
三、添加SPI驅(qū)動(dòng)
查看 ESP32學(xué)習(xí)筆記(19)——SPI(主機(jī))接口使用
四初橘、工程代碼
百度網(wǎng)盤:https://pan.baidu.com/s/1gCtsGrn28ZfV1OinTolJPw?pwd=d3po 提取碼:d3po
將文件解壓到 esp-idf/examples 目錄下:
4.1 board_gpio.c
/*********************************************************************
* INCLUDES
*/
#include "driver/gpio.h"
#include "board_gpio.h"
/*********************************************************************
* PUBLIC FUNCTIONS
*/
/**
@brief NFC復(fù)位引腳初始化
@param 無
@return 無
*/
void NFC_GPIO_Init(void)
{
gpio_pad_select_gpio(NFC_RST_GPIO_PIN); // 選擇一個(gè)GPIO
gpio_set_direction(NFC_RST_GPIO_PIN, GPIO_MODE_OUTPUT); // 把這個(gè)GPIO作為輸出
NFC_GPIO_Write(NFC_RST_HIGH);
}
/**
@brief 配置NFC復(fù)位引腳工作模式
@param mode -[in] 工作模式
@return 無
*/
void NFC_GPIO_Write(uint8_t mode)
{
gpio_set_level(NFC_RST_GPIO_PIN, mode);
}
4.2 board_gpio.h
#ifndef _BOARD_GPIO_H_
#define _BOARD_GPIO_H_
/*********************************************************************
* INCLUDES
*/
#include "driver/gpio.h"
/*********************************************************************
* DEFINITIONS
*/
#define NFC_RST_GPIO_PIN GPIO_NUM_25 // NFC復(fù)位
#define NFC_RST_LOW 0x00
#define NFC_RST_HIGH 0x01
/*********************************************************************
* API FUNCTIONS
*/
void NFC_GPIO_Init(void);
void NFC_GPIO_Write(uint8_t mode);
#endif /* _BOARD_GPIO_H_ */
4.3 board_spi.c
/*********************************************************************
* INCLUDES
*/
#include <string.h>
#include "driver/spi_master.h"
#include "board_spi.h"
/*********************************************************************
* LOCAL VARIABLES
*/
static spi_device_handle_t s_spiHandle;
/*********************************************************************
* PUBLIC FUNCTIONS
*/
/**
@brief NFC SPI驅(qū)動(dòng)初始化
@param 無
@return 無
*/
void NFC_SPI_Init(void)
{
esp_err_t ret;
spi_bus_config_t spiBusConfig =
{
.miso_io_num = NFC_SPI_MISO_PIN, // MISO信號(hào)線
.mosi_io_num = NFC_SPI_MOSI_PIN, // MOSI信號(hào)線
.sclk_io_num = NFC_SPI_SCLK_PIN, // SCLK信號(hào)線
.quadwp_io_num = -1, // WP信號(hào)線,專用于QSPI的D2
.quadhd_io_num = -1, // HD信號(hào)線充岛,專用于QSPI的D3
.max_transfer_sz = 64 * 8, // 最大傳輸數(shù)據(jù)大小
};
spi_device_interface_config_t spiDeviceConfig =
{
.clock_speed_hz = SPI_MASTER_FREQ_10M, // Clock out at 10 MHz,
.mode = 0, // SPI mode 0
/*
* The timing requirements to read the busy signal from the EEPROM cannot be easily emulated
* by SPI transactions. We need to control CS pin by SW to check the busy signal manually.
*/
.spics_io_num = -1,
.queue_size = 7, // 傳輸隊(duì)列大小保檐,決定了等待傳輸數(shù)據(jù)的數(shù)量
};
//Initialize the SPI bus
ret = spi_bus_initialize(SPI3_HOST, &spiBusConfig, DMA_CHAN);
ESP_ERROR_CHECK(ret);
ret = spi_bus_add_device(SPI3_HOST, &spiDeviceConfig, &s_spiHandle);
ESP_ERROR_CHECK(ret);
// 配置CS引腳
gpio_pad_select_gpio(NFC_SPI_CS_PIN); // 選擇一個(gè)GPIO
gpio_set_direction(NFC_SPI_CS_PIN, GPIO_MODE_OUTPUT); // 把這個(gè)GPIO作為輸出
}
/**
@brief NFC SPI寫入數(shù)據(jù)
@param pData -[in] 寫入數(shù)據(jù)
@param dataLen -[in] 寫入數(shù)據(jù)長度
@return 無
*/
void NFC_SPI_Write(uint8_t *pData, uint32_t dataLen)
{
esp_err_t ret;
spi_transaction_t t;
if(0 == dataLen) // no need to send anything
{
return;
}
memset(&t, 0, sizeof(t)); // Zero out the transaction
t.length = dataLen * 8; // Len is in bytes, transaction length is in bits.
t.tx_buffer = pData; // Data
ret = spi_device_polling_transmit(s_spiHandle, &t); // Transmit!
assert(ret == ESP_OK); // Should have had no issues.
}
/**
@brief NFC SPI讀取數(shù)據(jù)
@param pData -[out] 讀取數(shù)據(jù)
@param dataLen -[in] 讀取數(shù)據(jù)長度
@return 無
*/
void NFC_SPI_Read(uint8_t *pData, uint32_t dataLen)
{
spi_transaction_t t;
if(0 == dataLen) // no need to receivce anything
{
return;
}
memset(&t, 0, sizeof(t)); // Zero out the transaction
t.length = dataLen * 8; // Len is in bytes, transaction length is in bits.
t.rx_buffer = pData;
esp_err_t ret = spi_device_polling_transmit(s_spiHandle, &t);
assert(ret == ESP_OK);
}
/****************************************************END OF FILE****************************************************/
4.4 board_spi.h
#ifndef _BOARD_SPI_H_
#define _BOARD_SPI_H_
/*********************************************************************
* INCLUDES
*/
#include "driver/gpio.h"
/*********************************************************************
* DEFINITIONS
*/
#define NFC_SPI_MISO_PIN GPIO_NUM_19
#define NFC_SPI_MOSI_PIN GPIO_NUM_23
#define NFC_SPI_SCLK_PIN GPIO_NUM_18
#define NFC_SPI_CS_PIN GPIO_NUM_5
#define DMA_CHAN 2
#define SPI_CS_LOW gpio_set_level(NFC_SPI_CS_PIN, 0)
#define SPI_CS_HIGH gpio_set_level(NFC_SPI_CS_PIN, 1)
/*********************************************************************
* API FUNCTIONS
*/
void NFC_SPI_Init(void);
void NFC_SPI_Write(uint8_t *pData, uint32_t dataLen);
void NFC_SPI_Read(uint8_t *pData, uint32_t dataLen);
#endif /* _BOARD_SPI_H_ */
4.5 board_mfrc522.c
/*********************************************************************
* INCLUDES
*/
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "board_gpio.h"
#include "board_spi.h"
#include "board_mfrc522.h"
static char pcdRequest(uint8_t reqCode, uint8_t *pTagType);
static char pcdAnticoll(uint8_t *pSnr);
static char pcdSelect(uint8_t *pSnr);
static char pcdAuthState(uint8_t authMode, uint8_t addr, uint8_t *pKey, uint8_t *pSnr);
static char pcdRead(uint8_t addr, uint8_t *pData);
static char pcdWrite(uint8_t addr, uint8_t *pData);
static void pcdReset(void);
static void calulateCRC(uint8_t *pInData, uint8_t len, uint8_t *pOutData);
static char pcdComMF522(uint8_t command, uint8_t *pInData, uint8_t inLenByte, uint8_t *pOutData, uint32_t *pOutLenBit);
static void pcdAntennaOn(void);
static void pcdAntennaOff(void);
static void setBitMask(uint8_t reg, uint8_t mask);
static void clearBitMask(uint8_t reg, uint8_t mask);
static uint8_t readRawRc(uint8_t addr);
static void writeRawRc(uint8_t addr, uint8_t writeData);
static void delayMs(uint8_t time);
/*********************************************************************
* LOCAL VARIABLES
*/
static uint8_t s_cardType[2]; // 卡類型
static uint8_t s_cardSerialNo[4]; // 卡序列號(hào)
static uint8_t s_defaultKeyA[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; // 默認(rèn)密碼A
static const char *TAG = "MFRC522";
/*********************************************************************
* PUBLIC FUNCTIONS
*/
/**
@brief MFRC522的初始化函數(shù)
@param 無
@return 無
*/
void MFRC522_Init(void)
{
pcdReset(); // 復(fù)位
delayMs(5);
ESP_LOGI(TAG, "reg: %02x" ,readRawRc(Status1Reg));
ESP_LOGI(TAG, "reg: %02x" ,readRawRc(Status2Reg));
ESP_LOGI(TAG, "reg: %02x" ,readRawRc(WaterLevelReg));
pcdAntennaOn(); // 開啟天線發(fā)射
}
/**
@brief MFRC522讀取卡片塊數(shù)據(jù)
@param addr -[in] 塊地址
@return 狀態(tài)值,0 - 成功崔梗;2 - 無卡夜只;3 - 防沖撞失敗蒜魄;4 - 選卡失斎雍ァ;5 - 密碼錯(cuò)誤
*/
uint8_t MFRC522_ReadCardDataBlock(uint8_t addr)
{
memset(s_cardSerialNo, 0, 4);
if(pcdRequest(PICC_REQALL, s_cardType) == MI_OK)
{
}
else
{
ESP_LOGI(TAG, "ERR: 2");
return 2; // 無卡
}
if(pcdAnticoll(s_cardSerialNo) == MI_OK)
{
}
else
{
ESP_LOGI(TAG, "ERR: 3");
return 3; // 防沖撞失敗
}
if(pcdSelect(s_cardSerialNo) == MI_OK)
{
}
else
{
ESP_LOGI(TAG, "ERR: 4");
return 4; // 選卡失敗
}
if(pcdAuthState(0x60, addr, s_defaultKeyA, s_cardSerialNo) == MI_OK)
{
ESP_LOGI(TAG, "ERR: 0");
return 0;
}
else
{
ESP_LOGI(TAG, "ERR: 5");
return 5; // 密碼錯(cuò)誤
}
}
/**
@brief 讀取卡片序列號(hào)
@param pCardSerialNo -[out] 卡片序列號(hào)
@return 0 - 讀卡成功谈为;2 - 無卡
*/
uint8_t MFRC522_ReadCardSerialNo(uint8_t *pCardSerialNo)
{
uint8_t status = MFRC522_ReadCardDataBlock(4);
memcpy(pCardSerialNo, s_cardSerialNo, 4);
return status;
}
/*********************************************************************
* LOCAL FUNCTIONS
*/
/**
@brief 尋卡
@param reqCode -[in] 尋卡方式旅挤,0x52 尋感應(yīng)區(qū)內(nèi)所有符合1443A標(biāo)準(zhǔn)的卡,0x26 尋未進(jìn)入休眠狀態(tài)的卡
@param pTagType -[out] 卡片類型代碼
0x4400 = Mifare_UltraLight
0x0400 = Mifare_One(S50)
0x0200 = Mifare_One(S70)
0x0800 = Mifare_Pro(X)
0x4403 = Mifare_DESFire
@return 狀態(tài)值伞鲫,MI OK - 成功粘茄;MI_ERR - 失敗
*/
static char pcdRequest(uint8_t reqCode, uint8_t *pTagType)
{
char status;
uint32_t len;
uint8_t comMF522Buf[MAXRLEN];
clearBitMask(Status2Reg, 0x08);
writeRawRc(BitFramingReg, 0x07);
setBitMask(TxControlReg, 0x03);
comMF522Buf[0] = reqCode;
status = pcdComMF522(PCD_TRANSCEIVE, comMF522Buf, 1, comMF522Buf, &len); // 發(fā)送并接收數(shù)據(jù)
if((status == MI_OK) && (len == 0x10))
{
ESP_LOGI(TAG, "mi_ok");
*pTagType = comMF522Buf[0];
*(pTagType+1) = comMF522Buf[1];
}
else
{
ESP_LOGI(TAG, "mi_err");
status = MI_ERR;
}
return status;
}
/**
@brief 防沖撞
@param pSnr -[out] 卡片序列號(hào),4字節(jié)
@return 狀態(tài)值秕脓,MI OK - 成功柒瓣;MI_ERR - 失敗
*/
static char pcdAnticoll(uint8_t *pSnr)
{
char status;
uint8_t i, snrCheck = 0;
uint32_t len;
uint8_t comMF522Buf[MAXRLEN];
clearBitMask(Status2Reg, 0x08); // 寄存器包含接收器和發(fā)送器和數(shù)據(jù)模式檢測器的狀態(tài)標(biāo)志
writeRawRc(BitFramingReg, 0x00); // 不啟動(dòng)數(shù)據(jù)發(fā)送,接收的LSB位存放在位0吠架,接收到的第二位放在位1芙贫,定義發(fā)送的最后一個(gè)字節(jié)位數(shù)為8
clearBitMask(CollReg, 0x80); // 所有接收的位在沖突后將被清除
comMF522Buf[0] = PICC_ANTICOLL1;
comMF522Buf[1] = 0x20;
status = pcdComMF522(PCD_TRANSCEIVE, comMF522Buf, 2, comMF522Buf, &len);
if(status == MI_OK)
{
for(i = 0; i < 4; i++)
{
*(pSnr + i) = comMF522Buf[i];
snrCheck ^= comMF522Buf[i];
}
if(snrCheck != comMF522Buf[i]) // 返回四個(gè)字節(jié),最后一個(gè)字節(jié)為校驗(yàn)位
{
status = MI_ERR;
}
}
setBitMask(CollReg, 0x80);
return status;
}
/**
@brief 選定卡片
@param pSnr -[in] 卡片序列號(hào)傍药,4字節(jié)
@return 狀態(tài)值磺平,MI OK - 成功;MI_ERR - 失敗
*/
static char pcdSelect(uint8_t *pSnr)
{
char status;
uint8_t i;
uint8_t comMF522Buf[MAXRLEN];
uint32_t len;
comMF522Buf[0] = PICC_ANTICOLL1;
comMF522Buf[1] = 0x70;
comMF522Buf[6] = 0;
for(i = 0; i < 4; i++)
{
comMF522Buf[i + 2] = *(pSnr + i);
comMF522Buf[6] ^= *(pSnr + i);
}
calulateCRC(comMF522Buf, 7, &comMF522Buf[7]);
clearBitMask(Status2Reg, 0x08);
status = pcdComMF522(PCD_TRANSCEIVE, comMF522Buf, 9, comMF522Buf, &len);
if((status == MI_OK ) && (len == 0x18))
{
status = MI_OK;
}
else
{
status = MI_ERR;
}
return status;
}
/**
@brief 驗(yàn)證卡片密碼
@param authMode -[in] 密碼驗(yàn)證模式拐辽,0x60 驗(yàn)證A密鑰褪秀,0x61 驗(yàn)證B密鑰
@param addr -[in] 塊地址
@param pKey -[in] 密碼
@param pSnr -[in] 卡片序列號(hào),4字節(jié)
@return 狀態(tài)值薛训,MI OK - 成功媒吗;MI_ERR - 失敗
*/
static char pcdAuthState(uint8_t authMode, uint8_t addr, uint8_t *pKey, uint8_t *pSnr)
{
char status;
uint8_t i, comMF522Buf[MAXRLEN];
uint32_t len;
comMF522Buf[0] = authMode;
comMF522Buf[1] = addr;
for(i = 0; i < 6; i++)
{
comMF522Buf[i + 2] = *(pKey + i);
}
for(i = 0; i < 6; i++)
{
comMF522Buf[i + 8] = *(pSnr + i);
}
status = pcdComMF522(PCD_AUTHENT, comMF522Buf, 12, comMF522Buf, &len);
if((status != MI_OK ) || ( ! (readRawRc(Status2Reg) & 0x08)))
{
status = MI_ERR;
}
return status;
}
/**
@brief 讀取M1卡一塊數(shù)據(jù)
@param addr -[in] 塊地址
@param pData -[out] 讀出的數(shù)據(jù),16字節(jié)
@return 狀態(tài)值乙埃,MI OK - 成功闸英;MI_ERR - 失敗
*/
static char pcdRead(uint8_t addr, uint8_t *pData)
{
char status;
uint8_t i, comMF522Buf[MAXRLEN];
uint32_t len;
comMF522Buf[0] = PICC_READ;
comMF522Buf[1] = addr;
calulateCRC(comMF522Buf, 2, &comMF522Buf[2]);
status = pcdComMF522(PCD_TRANSCEIVE, comMF522Buf, 4, comMF522Buf, &len);
if((status == MI_OK) && (len == 0x90))
{
for(i = 0; i < 16; i++)
{
*(pData + i) = comMF522Buf[i];
}
}
else
{
status = MI_ERR;
}
return status;
}
/**
@brief 寫入M1卡一塊數(shù)據(jù)
@param addr -[in] 塊地址
@param pData -[out] 寫入的數(shù)據(jù)锯岖,16字節(jié)
@return 狀態(tài)值,MI OK - 成功甫何;MI_ERR - 失敗
*/
static char pcdWrite(uint8_t addr, uint8_t *pData)
{
char status;
uint8_t i, comMF522Buf[MAXRLEN];
uint32_t len;
comMF522Buf[0] = PICC_WRITE;
comMF522Buf[1] = addr;
calulateCRC(comMF522Buf, 2, &comMF522Buf[2]);
status = pcdComMF522(PCD_TRANSCEIVE, comMF522Buf, 4, comMF522Buf, &len);
if((status != MI_OK) || (len != 4) || ((comMF522Buf[0] & 0x0F) != 0x0A))
{
status = MI_ERR;
}
if(status == MI_OK)
{
for(i = 0; i < 16; i++)
{
comMF522Buf[i] = *(pData + i);
}
calulateCRC(comMF522Buf, 16, &comMF522Buf[16]);
status = pcdComMF522(PCD_TRANSCEIVE, comMF522Buf, 18, comMF522Buf, &len);
if((status != MI_OK) || (len != 4) || ((comMF522Buf[0] & 0x0F) != 0x0A))
{
status = MI_ERR;
}
}
return status;
}
/**
@brief 復(fù)位RC522
@return 無
*/
static void pcdReset(void)
{
// 需先保持高電平出吹,后給個(gè)下降沿
NFC_GPIO_Write(NFC_RST_LOW);
delayMs(5);
NFC_GPIO_Write(NFC_RST_HIGH);
delayMs(10);
writeRawRc(CommandReg, PCD_RESETPHASE); // 和MI卡通訊,CRC初始值0x6363
delayMs(1);
writeRawRc(ModeReg, 0x3D);
writeRawRc(TReloadRegL, 30);
writeRawRc(TReloadRegH, 0);
writeRawRc(TModeReg, 0x8D);
writeRawRc(TPrescalerReg, 0x3E);
writeRawRc(TxASKReg, 0x40);
}
/**
@brief 用MF522計(jì)算CRC16
@param pInData -[in] 計(jì)算CRC16的數(shù)組
@param len -[in] 計(jì)算CRC16的數(shù)組字節(jié)長度
@param pOutData -[out] 存放計(jì)算結(jié)果存放的首地址
@return 無
*/
static void calulateCRC(uint8_t *pInData, uint8_t len, uint8_t *pOutData)
{
uint8_t i, n;
clearBitMask(DivIrqReg, 0x04);
writeRawRc(CommandReg, PCD_IDLE);
setBitMask(FIFOLevelReg, 0x80);
for(i = 0; i < len; i++)
{
writeRawRc(FIFODataReg, *(pInData + i));
}
writeRawRc(CommandReg, PCD_CALCCRC);
i = 0xFF;
do
{
n = readRawRc(DivIrqReg);
i--;
}
while((i != 0) && ! (n & 0x04));
pOutData[0] = readRawRc(CRCResultRegL);
pOutData[1] = readRawRc(CRCResultRegM);
}
/**
@brief 通過MFRC522和ISO14443卡通訊
@param command -[in] RC522命令字
@param pInData -[in] 通過RC522發(fā)送到卡片的數(shù)據(jù)
@param inLenByte -[in] 發(fā)送數(shù)據(jù)的字節(jié)長度
@param pOutData -[out] 接收到的卡片返回?cái)?shù)據(jù)
@param pOutLenBit -[out] 返回?cái)?shù)據(jù)的位長度
@return 狀態(tài)值辙喂,MI OK - 成功捶牢;MI_ERR - 失敗
*/
static char pcdComMF522(uint8_t command, uint8_t *pInData, uint8_t inLenByte, uint8_t *pOutData, uint32_t *pOutLenBit)
{
char status = MI_ERR;
uint8_t irqEn = 0x00;
uint8_t waitFor = 0x00;
uint8_t lastBits;
uint8_t n;
uint32_t i;
uint8_t j;
switch(command)
{
case PCD_AUTHENT:
irqEn = 0x12;
waitFor = 0x10;
break;
case PCD_TRANSCEIVE:
irqEn = 0x77;
waitFor = 0x30;
break;
default:
break;
}
writeRawRc(ComIEnReg, irqEn | 0x80);
clearBitMask(ComIrqReg, 0x80);
writeRawRc(CommandReg, PCD_IDLE);
setBitMask(FIFOLevelReg, 0x80); // 清空FIFO
for(i = 0; i < inLenByte; i++)
{
writeRawRc(FIFODataReg, pInData[i]); // 數(shù)據(jù)寫入FIFO
}
writeRawRc(CommandReg, command); // 命令寫入命令寄存器
if(command == PCD_TRANSCEIVE)
{
setBitMask(BitFramingReg, 0x80); // 開始發(fā)送
}
i = 6000; // 根據(jù)時(shí)鐘頻率調(diào)整,操作M1卡最大等待時(shí)間25ms 2000?
do
{
n = readRawRc(ComIrqReg);
i--;
}
while((i != 0) && !(n & 0x01) && !(n & waitFor));
clearBitMask(BitFramingReg, 0x80);
if(i != 0)
{
j = readRawRc(ErrorReg);
if(!(j & 0x1B))
{
status = MI_OK;
if(n & irqEn & 0x01)
{
status = MI_NOTAGERR;
}
if(command == PCD_TRANSCEIVE)
{
n = readRawRc(FIFOLevelReg);
lastBits = readRawRc(ControlReg) & 0x07;
if(lastBits)
{
*pOutLenBit = (n - 1) * 8 + lastBits;
}
else
{
*pOutLenBit = n * 8;
}
if(n == 0)
{
n = 1;
}
if(n > MAXRLEN)
{
n = MAXRLEN;
}
for(i = 0; i < n; i++)
{
pOutData[i] = readRawRc(FIFODataReg);
}
}
}
else
{
status = MI_ERR;
}
}
setBitMask(ControlReg, 0x80); // stop timer now
writeRawRc(CommandReg, PCD_IDLE);
return status;
}
/**
@brief 開啟天線【每次啟動(dòng)或關(guān)閉天線發(fā)射之間至少有1ms的間隔】
@return 無
*/
static void pcdAntennaOn(void)
{
uint8_t temp;
temp = readRawRc(TxControlReg);
if(!(temp & 0x03))
{
setBitMask(TxControlReg, 0x03);
}
}
/**
@brief 關(guān)閉天線
@return 無
*/
static void pcdAntennaOff(void)
{
clearBitMask(TxControlReg, 0x03);
}
/**
@brief 置RC522寄存器位
@param reg -[in] 寄存器地址
@param mask -[in] 置位值
@return 無
*/
static void setBitMask(uint8_t reg, uint8_t mask)
{
char temp = 0x00;
temp = readRawRc(reg) | mask;
writeRawRc(reg, temp | mask); // set bit mask
}
/**
@brief 清RC522寄存器位
@param reg -[in] 寄存器地址
@param mask -[in] 清位值
@return 無
*/
static void clearBitMask(uint8_t reg, uint8_t mask)
{
char temp = 0x00;
temp = readRawRc(reg) & (~mask);
writeRawRc(reg, temp); // clear bit mask
}
/**
@brief 寫RC522寄存器
@param addr -[in] 寄存器地址
@param writeData -[in] 寫入數(shù)據(jù)
@return 無
*/
static void writeRawRc(uint8_t addr, uint8_t writeData)
{
SPI_CS_LOW;
addr <<= 1;
addr &= 0x7e;
NFC_SPI_Write(&addr, 1);
NFC_SPI_Write(&writeData, 1);
SPI_CS_HIGH;
}
/**
@brief 讀RC522寄存器
@param addr -[in] 寄存器地址
@return 讀出一字節(jié)數(shù)據(jù)
*/
static uint8_t readRawRc(uint8_t addr)
{
uint8_t readData;
SPI_CS_LOW;
addr <<= 1;
addr |= 0x80;
NFC_SPI_Write(&addr, 1);
NFC_SPI_Read(&readData, 1);
SPI_CS_HIGH;
return readData;
}
/**
@brief 毫秒級(jí)延時(shí)函數(shù)
@param time -[in] 延時(shí)時(shí)間(毫秒)
@return 無
*/
static void delayMs(uint8_t time)
{
vTaskDelay(time / portTICK_PERIOD_MS);
}
/****************************************************END OF FILE****************************************************/
4.6 board_mfrc522.h
#ifndef _BOARD_MFRC522_H_
#define _BOARD_MFRC522_H_
/*********************************************************************
* INCLUDES
*/
/*********************************************************************
* DEFINITIONS
*/
#define MAXRLEN 18
//******************************************************************/
// MFRC522命令字
//******************************************************************/
#define PCD_IDLE 0x00 // 取消當(dāng)前命令
#define PCD_AUTHENT 0x0E // 驗(yàn)證密鑰
#define PCD_RECEIVE 0x08 // 接收數(shù)據(jù)
#define PCD_TRANSMIT 0x04 // 發(fā)送數(shù)據(jù)
#define PCD_TRANSCEIVE 0x0C // 發(fā)送并接收數(shù)據(jù)
#define PCD_RESETPHASE 0x0F // 復(fù)位
#define PCD_CALCCRC 0x03 // CRC計(jì)算
//******************************************************************/
// Mifare_One卡片命令字
//******************************************************************/
#define PICC_REQIDL 0x26 // 尋天線區(qū)內(nèi)未進(jìn)入休眠狀態(tài)
#define PICC_REQALL 0x52 // 尋天線區(qū)內(nèi)全部卡
#define PICC_ANTICOLL1 0x93 // 防沖撞
#define PICC_ANTICOLL2 0x95 // 防沖撞
#define PICC_AUTHENT1A 0x60 // 驗(yàn)證A密鑰
#define PICC_AUTHENT1B 0x61 // 驗(yàn)證B密鑰
#define PICC_READ 0x30 // 讀塊
#define PICC_WRITE 0xA0 // 寫塊
#define PICC_DECREMENT 0xC0 // 扣款
#define PICC_INCREMENT 0xC1 // 充值
#define PICC_RESTORE 0xC2 // 調(diào)塊數(shù)據(jù)到緩沖區(qū)
#define PICC_TRANSFER 0xB0 // 保存緩沖區(qū)中數(shù)據(jù)
#define PICC_HALT 0x50 // 休眠
//******************************************************************/
// MFRC522 FIFO長度定義
//******************************************************************/
#define DEF_FIFO_LENGTH 64 // FIFO size=64byte
//******************************************************************/
// MFRC522寄存器定義
//******************************************************************/
// PAGE 0
#define RFU00 0x00
#define CommandReg 0x01
#define ComIEnReg 0x02
#define DivlEnReg 0x03
#define ComIrqReg 0x04
#define DivIrqReg 0x05
#define ErrorReg 0x06
#define Status1Reg 0x07
#define Status2Reg 0x08
#define FIFODataReg 0x09
#define FIFOLevelReg 0x0A
#define WaterLevelReg 0x0B
#define ControlReg 0x0C
#define BitFramingReg 0x0D
#define CollReg 0x0E
#define RFU0F 0x0F
// PAGE 1
#define RFU10 0x10
#define ModeReg 0x11
#define TxModeReg 0x12
#define RxModeReg 0x13
#define TxControlReg 0x14
#define TxASKReg 0x15
#define TxSelReg 0x16
#define RxSelReg 0x17
#define RxThresholdReg 0x18
#define DemodReg 0x19
#define RFU1A 0x1A
#define RFU1B 0x1B
#define MifareReg 0x1C
#define RFU1D 0x1D
#define RFU1E 0x1E
#define SerialSpeedReg 0x1F
// PAGE 2
#define RFU20 0x20
#define CRCResultRegM 0x21
#define CRCResultRegL 0x22
#define RFU23 0x23
#define ModWidthReg 0x24
#define RFU25 0x25
#define RFCfgReg 0x26
#define GsNReg 0x27
#define CWGsCfgReg 0x28
#define ModGsCfgReg 0x29
#define TModeReg 0x2A
#define TPrescalerReg 0x2B
#define TReloadRegH 0x2C
#define TReloadRegL 0x2D
#define TCounterValueRegH 0x2E
#define TCounterValueRegL 0x2F
// PAGE 3
#define RFU30 0x30
#define TestSel1Reg 0x31
#define TestSel2Reg 0x32
#define TestPinEnReg 0x33
#define TestPinValueReg 0x34
#define TestBusReg 0x35
#define AutoTestReg 0x36
#define VersionReg 0x37
#define AnalogTestReg 0x38
#define TestDAC1Reg 0x39
#define TestDAC2Reg 0x3A
#define TestADCReg 0x3B
#define RFU3C 0x3C
#define RFU3D 0x3D
#define RFU3E 0x3E
#define RFU3F 0x3F
//******************************************************************/
// MFRC522通訊返回錯(cuò)誤代碼
//******************************************************************/
#define MI_OK (char)0
#define MI_NOTAGERR (char)(-1)
#define MI_ERR (char)(-2)
/*********************************************************************
* API FUNCTIONS
*/
void MFRC522_Init(void);
uint8_t MFRC522_ReadCardDataBlock(uint8_t addr);
uint8_t MFRC522_ReadCardSerialNo(uint8_t *pCardSerialNo);
#endif /* _BOARD_MFRC522_H_ */
五、API調(diào)用
需包含頭文件 board_mfrc522.h
5.1 MFRC522_Init()
功能 | MFRC522初始化函數(shù) |
---|---|
函數(shù)定義 | void MFRC522_Init(void) |
參數(shù) | 無 |
返回 | 無 |
5.2 MFRC522_ReadCardDataBlock()
功能 | MFRC522讀取卡片塊數(shù)據(jù) |
---|---|
函數(shù)定義 | uint8_t MFRC522_ReadCardDataBlock(uint8_t blockAddr) |
參數(shù) | blockAddr:塊地址 |
返回 | 狀態(tài)值,0 - 成功搭幻;2 - 無卡昌犹;3 - 防沖撞失敗;4 - 選卡失敗;5 - 密碼錯(cuò)誤 |
5.3 MFRC522_ReadCardSerialNo()
功能 | 讀取卡片序列號(hào) |
---|---|
函數(shù)定義 | uint8_t MFRC522_ReadCardSerialNo(uint8_t *pCardSerialNo) |
參數(shù) | pCardSerialNo:卡片序列號(hào) |
返回 | 狀態(tài)值炒考,0 - 讀卡成功;2 - 無卡 |
六霎迫、使用例子
1)添加頭文件
#include "board_gpio.h"
#include "board_mfrc522.h"
#include "board_spi.h"
2)添加初始化代碼(main.c的main函數(shù)中)
首先調(diào)用 NFC_GPIO_Init()
初始化 RFID RC522 模塊的 RST 引腳斋枢,然后調(diào)用 NFC_SPI_Init()
初始化 SPI 通信,最后調(diào)用 MFRC522_Init()
初始化 RC522 模塊知给。
/**@brief Application main function.
*/
void app_main(void)
{
ESP_ERROR_CHECK(nvs_flash_init());
NFC_GPIO_Init();
NFC_SPI_Init();
MFRC522_Init();
/*-------------------------- 創(chuàng)建線程 ---------------------------*/
xTaskCreate(monitor_task, "monitor_task", 2048, NULL, 4, NULL);
}
3)添加任務(wù)杏慰,定時(shí)讀取數(shù)據(jù)塊4
static void monitor_task(void *arg)
{
while(1) // 任務(wù)都是一個(gè)無限循環(huán),不能返回
{
uint8_t card[4];
MFRC522_ReadCardSerialNo(card);
ESP_LOGI(TAG, "card: %02x%02x%02x%02x", card[0], card[1], card[2], card[3]);
vTaskDelay(500 / portTICK_PERIOD_MS); // 100ms
}
}
4)復(fù)位后炼鞠,可通過讀取以下三個(gè)寄存器缘滥,判斷SPI是否通信成功
ESP_LOGI(TAG, "reg: %02x" ,readRawRc(Status1Reg));
ESP_LOGI(TAG, "reg: %02x" ,readRawRc(Status2Reg));
ESP_LOGI(TAG, "reg: %02x" ,readRawRc(WaterLevelReg));
查看打印,返回值與數(shù)據(jù)手冊中描述一致
5)讀取卡號(hào)
? 由 Leung 寫于 2022 年 5 月 30 日