RC522(RFID模块)实践总结
此次使用RC522模块和S50卡实现近场通讯功能(开发板与RC522通讯方式为硬件SPI),就实践过程中的一些知识点进行总结:
- RC522模块和M1卡要点介绍;
- 驱动代码;
- 出现问题及解决方法;
1. RC522模块和M1卡要点介绍:
- MFRC522简化功能框图;
- MFRC522与主机SPI通讯引脚配置;
- MFRC522与M1卡的通讯原理和通讯流程;
- M1卡存储结构;
MFRC522简化功能框图:
先从RC522功能框图入手,可以从大方向上理解通讯原理。
上述主机一般指的就是手上的开发板,通信接口对应天线,MFRC522与主机通讯支持UART、SPI以及IIC,本人采用SPI通讯方式。
MFRC522与主机SPI通讯引脚配置:
MFRC522与M1卡的通讯原理和通讯流程:
工作原理:
读写器向M1卡发一组固定频率的电磁波,卡片内有一个 LC串联谐振电路,其频率与读写器发射的频率相同,在电磁波的激励下,LC谐振电路产生共振,从而使电容内有了电荷,在这个电容的另一端,接有一个单向导通的电子泵,将电容内的电荷送到另一个电容内储存,当所积累的电荷达到2V时,此电容可做为电源为其它电路提供工作电压,将卡内数据发射出去或接取读写器的数据。
通讯流程:
M1卡存储结构
存储结构:
- 其中第0扇区的块0是用于存放厂商代码的,已经固化,不可更改,为32位(4Bytes);
- 每个扇区的块0、块1和块2位数据块,可用于存储数据,每块16个字节(只有S50卡是这样);
- 每个扇区的块3位控制块,包含了密码A、存取控制、密码B,具体结构如下图所示;
2. 驱动代码
RC522.h
#ifndef __RC522_H
#define __RC522_H
#include "stm32f10x.h"
#include "stm32f10x_spi.h"
#include <string.h>
#include <stdio.h>
/*******************************
*连线说明:
*1--SDA <----->PA4
*2--SCK <----->PA5
*3--MOSI <----->PA7
*4--MISO <----->PA6
*5--悬空
*6--GND <----->GND
*7--RST <----->PB0
*8--VCC <----->VCC
************************************/
//MF522命令代码
#define PCD_IDLE 0x00 //取消当前命令
#define PCD_AUTHENT 0x0E //验证密钥
#define PCD_RECEIVE 0x08 //接收数据
#define PCD_TRANSMIT 0x04 //发送数据
#define PCD_TRANSCEIVE 0x0C //发送并接收数据
#define PCD_RESETPHASE 0x0F //复位
#define PCD_CALCCRC 0x03 //CRC计算
//Mifare_One卡片命令代码
#define PICC_REQIDL 0x26 //寻天线区内未进入休眠状态
#define PICC_REQALL 0x52 //寻天线区内全部卡
#define PICC_ANTICOLL1 0x93 //防冲撞
#define PICC_ANTICOLL2 0x95 //防冲撞
#define PICC_AUTHENT1A 0x60 //验证A密钥
#define PICC_AUTHENT1B 0x61 //验证B密钥
#define PICC_READ 0x30 //读块
#define PICC_WRITE 0xA0 //写块
#define PICC_DECREMENT 0xC0 //扣款
#define PICC_INCREMENT 0xC1 //充值
#define PICC_RESTORE 0xC2 //调块数据到缓冲区
#define PICC_TRANSFER 0xB0 //保存缓冲区中数据
#define PICC_HALT 0x50 //休眠
#define DEF_FIFO_LENGTH 64 //FIFO size=64byte
#define MAXRLEN 18
//MF522寄存器定义
// 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 TxAutoReg 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
//和RC522通讯时返回的M1卡状态
#define MI_OK 0x26
#define MI_NOTAGERR 0xcc
#define MI_ERR 0xbb
//和MF522通讯时返回的错误代码
#define SHAQU1 0X01
#define KUAI4 0X04
#define KUAI7 0X07
#define REGCARD 0xa1
#define CONSUME 0xa2
#define READCARD 0xa3
#define ADDMONEY 0xa4
#define SPI_RC522_ReadByte() SPI_RC522_SendByte(0)
#define SET_SPI_CS (GPIOF->BSRR=0X01)
#define CLR_SPI_CS (GPIOF->BRR=0X01)
#define SET_RC522RST GPIOF->BSRR=0X02
#define CLR_RC522RST GPIOF->BRR=0X02
/***********************RC522 函数宏定义**********************/
#define RC522_CS_Enable() GPIO_ResetBits ( GPIOA, GPIO_Pin_4 )
#define RC522_CS_Disable() GPIO_SetBits ( GPIOA, GPIO_Pin_4 )
#define RC522_Reset_Enable() GPIO_ResetBits( GPIOB, GPIO_Pin_0 )
#define RC522_Reset_Disable() GPIO_SetBits ( GPIOB, GPIO_Pin_0 )
#define RC522_SCK_0() GPIO_ResetBits( GPIOA, GPIO_Pin_5 )
#define RC522_SCK_1() GPIO_SetBits ( GPIOA, GPIO_Pin_5 )
#define RC522_MOSI_0() GPIO_ResetBits( GPIOA, GPIO_Pin_7 )
#define RC522_MOSI_1() GPIO_SetBits ( GPIOA, GPIO_Pin_7 )
#define RC522_MISO_GET() GPIO_ReadInputDataBit ( GPIOA, GPIO_Pin_6 )
u8 SPI_RC522_SendByte ( u8 byte);
u8 ReadRawRC ( u8 ucAddress );
void WriteRawRC ( u8 ucAddress, u8 ucValue );
void SPI1_Init ( void );
void RC522_Handel ( void );
void RC522_Init ( void ); //初始化
void PcdReset ( void ); //复位
void M500PcdConfigISOType ( u8 type ); //工作方式
char PcdRequest ( u8 req_code, u8 pTagType ); //寻卡
char PcdAnticoll ( u8 pSnr); //防冲撞
void PcdAntennaOn ( void ); //开启天线
void PcdAntennaOff ( void ); //关闭天线
void SetBitMask ( u8 ucReg, u8 ucMask );
void ClearBitMask ( u8 ucReg, u8 ucMask );
char PcdSelect ( u8 pSnr ); //选择卡片
char PcdAuthState ( u8 ucAuth_mode, u8 ucAddr, u8 pKey, u8 pSnr ); //验证密码
char PcdWrite ( u8 ucAddr, u8 pData );
char PcdRead ( u8 ucAddr, u8 pData );
void ShowID ( u16 x,u16 y, u8 p, u16 charColor, u16 bkColor); //显示卡的卡号,以十六进制显示
char PcdHalt ( void ); //命令卡片进入休眠状态
void CalulateCRC ( u8 pIndata, u8 ucLen, u8 pOutData );
#endif
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
RC522.c
#include "rc522.h"
#include "./SysTick/bsp_SysTick.h"
#include "./usart/bsp_usart.h"
#include "stm32f10x_spi.h"
// M1卡分为16个扇区,每个扇区由四个块(块0、块1、块2、块3)组成
// 将16个扇区的64个块按绝对地址编号为:0~63
// 第0个扇区的块0(即绝对地址0块),用于存放厂商代码,已经固化不可更改
// 每个扇区的块0、块1、块2为数据块,可用于存放数据
// 每个扇区的块3为控制块(绝对地址为:块3、块7、块11.....)包括密码A,存取控制、密码B等
/*******************************
*连线说明:
*1--SDA <----->PA4
*2--SCK <----->PA5
*3--MOSI <----->PA7
*4--MISO <----->PA6
*5--悬空
*6--GND <----->GND
*7--RST <----->PB0
*8--VCC <----->VCC
************************************/
#define RC522_DELAY() delay_us( 2 )
/全局变量/
unsigned char CT[2]; //卡类型
unsigned char SN[4]; //卡号
unsigned char RFID[16]; //存放RFID
unsigned char lxl_bit=0;
unsigned char card1_bit=0;
unsigned char card2_bit=0;
unsigned char card3_bit=0;
unsigned char card4_bit=0;
unsigned char total=0;
unsigned char lxl[4]={196,58,104,217};
unsigned char card_1[4]={83,106,11,1};
unsigned char card_2[4]={208,121,31,57};
unsigned char card_3[4]={176,177,143,165};
unsigned char card_4[4]={5,158,10,136};
u8 KEY[6]={0xff,0xff,0xff,0xff,0xff,0xff};
u8 AUDIO_OPEN[6] = {0xAA, 0x07, 0x02, 0x00, 0x09, 0xBC};
unsigned char RFID1[16]={0x00,0x00,0x00,0x00,0x00,0x00,0xff,0x07,0x80,0x29,0xff,0xff,0xff,0xff,0xff,0xff};
/函数声明/
unsigned char status;
unsigned char s=0x08;
/* 函数名:RC522_Init
- 描述 :初始化RC522配置
- 输入 :无
- 返回 : 无
- 调用 :外部调用 */
void RC522_Init ( void )
{
SPI1_Init();
RC522_Reset_Disable(); //将RST置高,启动内部复位阶段;
PcdReset (); //复位RC522
PcdAntennaOff(); //关闭天线
RC522_DELAY(); //delay 1ms
PcdAntennaOn(); //打开天线
M500PcdConfigISOType ( 'A' ); //设置工作方式
}
/* 函数名:SPI1_Init
-
描述 :初始化SPI1的配置
-
输入 :无
-
返回 : 无
-
调用 :外部调用 */
void SPI1_Init (void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE );//PORTB时钟使能
// CS
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure); //根据设定参数初始化PF0、PF1
// SCK
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// MISO
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// MOSI
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// RST
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_Init(GPIOB, &GPIO_InitStructure);
<span class="token comment">//置高CS口</span>
RC522_CS_Disable();
//其他SPI1配置
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //全双工;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //主机模式;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //传输数据为8位;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //时钟极性CPOL为空闲时低电平;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //时钟采样点为时钟奇数沿(上升沿);
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSS引脚由软件改变;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64; //预分频系数64;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //MSB先行模式;
SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC校验;
//初始化SPI1
SPI_Init(SPI1 , &SPI_InitStructure);
//使能SPI1
SPI_Cmd(SPI1 , ENABLE);
}
/* 函数名:PcdRese
-
描述 :复位RC522
-
输入 :无
-
返回 : 无
-
调用 :外部调用 */
void PcdReset ( void )
{
RC522_Reset_Disable();
delay_us ( 1 );
RC522_Reset_Enable();
delay_us ( 1 );
RC522_Reset_Disable();
delay_us ( 1 );
WriteRawRC ( CommandReg, 0x0f );
while ( ReadRawRC ( CommandReg ) & 0x10 );
delay_us ( 1 );
WriteRawRC ( ModeReg, 0x3D ); //定义发送和接收常用模式 和Mifare卡通讯,CRC初始值0x6363
WriteRawRC ( TReloadRegL, 30 ); //16位定时器低位
WriteRawRC ( TReloadRegH, 0 ); //16位定时器高位
WriteRawRC ( TModeReg, 0x8D ); //定义内部定时器的设置
WriteRawRC ( TPrescalerReg, 0x3E ); //设置定时器分频系数
WriteRawRC ( TxAutoReg, 0x40 ); //调制发送信号为100%ASK
}
/* 函数名:SPI_RC522_SendByte
- 描述 :向RC522发送1 Byte 数据
- 输入 :byte,要发送的数据
- 返回 : RC522返回的数据
- 调用 :内部调用 */
u8 SPI_RC522_SendByte ( u8 byte )
{
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) RESET);
SPI_I2S_SendData(SPI1, byte);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) RESET);
return SPI_I2S_ReceiveData(SPI1);
}
/* 函数名:ReadRawRC
-
描述 :读RC522寄存器
-
输入 :ucAddress,寄存器地址
-
返回 : 寄存器的当前值
-
调用 :内部调用 */
u8 ReadRawRC ( u8 ucAddress )
{
u8 ucAddr, ucReturn;
ucAddr = ( ( ucAddress << 1 ) & 0x7E ) | 0x80;
RC522_CS_Enable();
SPI_RC522_SendByte ( ucAddr );
ucReturn = SPI_RC522_ReadByte ();
RC522_CS_Disable();
return ucReturn;
}
/* 函数名:WriteRawRC
-
描述 :写RC522寄存器
-
输入 :ucAddress,寄存器地址 、 ucValue,写入寄存器的值
-
返回 : 无
-
调用 :内部调用 */
void WriteRawRC ( u8 ucAddress, u8 ucValue )
{
u8 ucAddr;
ucAddr = ( ucAddress << 1 ) & 0x7E;
RC522_CS_Enable();
SPI_RC522_SendByte ( ucAddr );
SPI_RC522_SendByte ( ucValue );
RC522_CS_Disable();
}
/* 函数名:M500PcdConfigISOType
-
描述 :设置RC522的工作方式
-
输入 :ucType,工作方式
-
返回 : 无
-
调用 :外部调用 */
void M500PcdConfigISOType ( u8 ucType )
{
if ( ucType == 'A') //ISO14443_A
{
ClearBitMask ( Status2Reg, 0x08 );
WriteRawRC ( ModeReg, 0x3D );//3F
WriteRawRC ( RxSelReg, 0x86 );//84
WriteRawRC ( RFCfgReg, 0x7F ); //4F
WriteRawRC ( TReloadRegL, 30 );//tmoLength);// TReloadVal = 'h6a =tmoLength(dec)
WriteRawRC ( TReloadRegH, 0 );
WriteRawRC ( TModeReg, 0x8D );
WriteRawRC ( TPrescalerReg, 0x3E );
delay_us ( 2 );
PcdAntennaOn <span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//开天线</span>
}
}
/*
-
函数名:SetBitMask
-
描述 :对RC522寄存器置位
-
输入 :ucReg,寄存器地址
-
ucMask,置位值
-
返回 : 无
-
调用 :内部调用
*/
void SetBitMask ( u8 ucReg, u8 ucMask )
{
u8 ucTemp;
ucTemp = ReadRawRC ( ucReg );
WriteRawRC ( ucReg, ucTemp | ucMask ); // set bit mask
}
/* 函数名:ClearBitMask
-
描述 :对RC522寄存器清位
-
输入 :ucReg,寄存器地址
-
ucMask,清位值
-
返回 : 无
-
调用 :内部调用 */
void ClearBitMask ( u8 ucReg, u8 ucMask )
{
u8 ucTemp;
ucTemp = ReadRawRC ( ucReg );
WriteRawRC ( ucReg, ucTemp & ( ~ ucMask) ); // clear bit mask
}
/* 函数名:PcdAntennaOn
-
描述 :开启天线
-
输入 :无
-
返回 : 无
-
调用 :内部调用 */
void PcdAntennaOn ( void )
{
u8 uc;
uc = ReadRawRC ( TxControlReg );
if ( ! ( uc & 0x03 ) )
SetBitMask(TxControlReg, 0x03);
}
/* 函数名:PcdAntennaOff
- 描述 :开启天线
- 输入 :无
- 返回 : 无
- 调用 :内部调用 */
void PcdAntennaOff ( void )
{
ClearBitMask ( TxControlReg, 0x03 );
}
void ShowID(u16 x,u16 y, u8 *p, u16 charColor, u16 bkColor) //显示卡的卡号,以十六进制显示
{
u8 num[9];
<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"ID>>>%s\r\n"</span><span class="token punctuation">,</span> num<span class="token punctuation">)</span><span class="token punctuation">;</span>
}
/* 函数名:PcdComMF522
-
描述 :通过RC522和ISO14443卡通讯
-
输入 :ucCommand,RC522命令字
-
pInData,通过RC522发送到卡片的数据
-
ucInLenByte,发送数据的字节长度
-
pOutData,接收到的卡片返回数据
-
pOutLenBit,返回数据的位长度
-
返回 : 状态值
-
= MI_OK,成功
-
调用 :内部调用 /
char PcdComMF522 ( u8 ucCommand, u8 pInData, u8 ucInLenByte, u8 pOutData, u32 pOutLenBit )
{
char cStatus = MI_ERR;
u8 ucIrqEn = 0x00;
u8 ucWaitFor = 0x00;
u8 ucLastBits;
u8 ucN;
u32 ul;
switch ( ucCommand )
{
case PCD_AUTHENT: //Mifare认证
ucIrqEn = 0x12; //允许错误中断请求ErrIEn 允许空闲中断IdleIEn
ucWaitFor = 0x10; //认证寻卡等待时候 查询空闲中断标志位
break;
<span class="token keyword">case</span> PCD_TRANSCEIVE<span class="token punctuation">:</span> <span class="token comment">//接收发送 发送接收</span>
ucIrqEn <span class="token operator">=</span> <span class="token number">0x77</span><span class="token punctuation">;</span> <span class="token comment">//允许TxIEn RxIEn IdleIEn LoAlertIEn ErrIEn TimerIEn</span>
ucWaitFor <span class="token operator">=</span> <span class="token number">0x30</span><span class="token punctuation">;</span> <span class="token comment">//寻卡等待时候 查询接收中断标志位与 空闲中断标志位</span>
<span class="token keyword">break</span><span class="token punctuation">;</span>
<span class="token keyword">default</span><span class="token punctuation">:</span>
<span class="token keyword">break</span><span class="token punctuation">;</span>
}
WriteRawRC ( ComIEnReg, ucIrqEn | 0x80 ); //IRqInv置位管脚IRQ与Status1Reg的IRq位的值相反
ClearBitMask ( ComIrqReg, 0x80 ); //Set1该位清零时,CommIRqReg的屏蔽位清零
WriteRawRC ( CommandReg, PCD_IDLE ); //写空闲命令
SetBitMask ( FIFOLevelReg, 0x80 ); //置位FlushBuffer清除内部FIFO的读和写指针以及ErrReg的BufferOvfl标志位被清除
for ( ul = 0; ul < ucInLenByte; ul ++ )
WriteRawRC ( FIFODataReg, pInData [ ul ] ); //写数据进FIFOdata
WriteRawRC ( CommandReg, ucCommand ); //写命令
if ( ucCommand == PCD_TRANSCEIVE )
SetBitMask(BitFramingReg,0x80); //StartSend置位启动数据发送 该位与收发命令使用时才有效
ul = 1000;//根据时钟频率调整,操作M1卡最大等待时间25ms
do //认证 与寻卡等待时间
{
ucN = ReadRawRC ( ComIrqReg ); //查询事件中断
ul --;
} while ( ( ul != 0 ) && ( ! ( ucN & 0x01 ) ) && ( ! ( ucN & ucWaitFor ) ) ); //退出条件i=0,定时器中断,与写空闲命令
ClearBitMask ( BitFramingReg, 0x80 ); //清理允许StartSend位
if ( ul != 0 )
{
if ( ! (( ReadRawRC ( ErrorReg ) & 0x1B )) ) //读错误标志寄存器BufferOfI CollErr ParityErr ProtocolErr
{
cStatus = MI_OK;
<span class="token keyword">if</span> <span class="token punctuation">(</span> ucN <span class="token operator">&</span> ucIrqEn <span class="token operator">&</span> <span class="token number">0x01</span> <span class="token punctuation">)</span> <span class="token comment">//是否发生定时器中断</span>
cStatus <span class="token operator">=</span> MI_NOTAGERR<span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span> ucCommand <span class="token operator">==</span> PCD_TRANSCEIVE <span class="token punctuation">)</span>
<span class="token punctuation">{<!-- --></span>
ucN <span class="token operator">=</span> ReadRawRC <span class="token punctuation">(</span> FIFOLevelReg <span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//读FIFO中保存的字节数</span>
ucLastBits <span class="token operator">=</span> ReadRawRC <span class="token punctuation">(</span> ControlReg <span class="token punctuation">)</span> <span class="token operator">&</span> <span class="token number">0x07</span><span class="token punctuation">;</span> <span class="token comment">//最后接收到得字节的有效位数</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span> ucLastBits <span class="token punctuation">)</span>
<span class="token operator">*</span> pOutLenBit <span class="token operator">=</span> <span class="token punctuation">(</span> ucN <span class="token operator">-</span> <span class="token number">1</span> <span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token number">8</span> <span class="token operator">+</span> ucLastBits<span class="token punctuation">;</span> <span class="token comment">//N个字节数减去1(最后一个字节)+最后一位的位数 读取到的数据总位数</span>
<span class="token keyword">else</span>
<span class="token operator">*</span> pOutLenBit <span class="token operator">=</span> ucN <span class="token operator">*</span> <span class="token number">8</span><span class="token punctuation">;</span> <span class="token comment">//最后接收到的字节整个字节有效</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span> ucN <span class="token operator">==</span> <span class="token number">0</span> <span class="token punctuation">)</span>
ucN <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span> ucN <span class="token operator">></span> MAXRLEN <span class="token punctuation">)</span>
ucN <span class="token operator">=</span> MAXRLEN<span class="token punctuation">;</span>
<span class="token keyword">for</span> <span class="token punctuation">(</span> ul <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> ul <span class="token operator"><</span> ucN<span class="token punctuation">;</span> ul <span class="token operator">++</span> <span class="token punctuation">)</span>
pOutData <span class="token punctuation">[</span> ul <span class="token punctuation">]</span> <span class="token operator">=</span> ReadRawRC <span class="token punctuation">(</span> FIFODataReg <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token keyword">else</span>
cStatus <span class="token operator">=</span> MI_ERR<span class="token punctuation">;</span>
}
SetBitMask ( ControlReg, 0x80 ); // stop timer now
WriteRawRC ( CommandReg, PCD_IDLE );
return cStatus;
}
/* 函数名:PcdRequest
-
描述 :寻卡
-
输入 :ucReq_code,寻卡方式
-
= 0x52,寻感应区内所有符合14443A标准的卡
-
= 0x26,寻未进入休眠状态的卡
-
pTagType,卡片类型代码
-
= 0x4400,Mifare_UltraLight
-
= 0x0400,Mifare_One(S50)
-
= 0x0200,Mifare_One(S70)
-
= 0x0800,Mifare_Pro(X))
-
= 0x4403,Mifare_DESFire
-
返回 : 状态值
-
= MI_OK,成功
-
调用 :外部调用 /
char PcdRequest ( u8 ucReq_code, u8 pTagType )
{
char cStatus;
u8 ucComMF522Buf [ MAXRLEN ];
u32 ulLen;
ClearBitMask ( Status2Reg, 0x08 ); //清理指示MIFARECyptol单元接通以及所有卡的数据通信被加密的情况
WriteRawRC ( BitFramingReg, 0x07 ); // 发送的最后一个字节的 七位
SetBitMask ( TxControlReg, 0x03 ); //TX1,TX2管脚的输出信号传递经发送调制的13.56的能量载波信号
ucComMF522Buf [ 0 ] = ucReq_code; //存入 卡片命令字
cStatus = PcdComMF522 ( PCD_TRANSCEIVE, ucComMF522Buf, 1, ucComMF522Buf, & ulLen ); //寻卡
if ( ( cStatus MI_OK ) && ( ulLen 0x10 ) ) //寻卡成功返回卡类型
{
pTagType = ucComMF522Buf [ 0 ];
( pTagType + 1 ) = ucComMF522Buf [ 1 ];
}
else
cStatus = MI_ERR;
return cStatus;
}
/* 函数名:PcdAnticoll
-
描述 :防冲撞
-
输入 :pSnr,卡片序列号,4字节
-
返回 : 状态值
-
= MI_OK,成功
-
调用 :外部调用 /
char PcdAnticoll ( u8 pSnr )
{
char cStatus;
u8 uc, ucSnr_check = 0;
u8 ucComMF522Buf [ MAXRLEN ];
u32 ulLen;
ClearBitMask ( Status2Reg, 0x08 ); //清MFCryptol On位 只有成功执行MFAuthent命令后,该位才能置位
WriteRawRC ( BitFramingReg, 0x00); //清理寄存器 停止收发
ClearBitMask ( CollReg, 0x80 ); //清ValuesAfterColl所有接收的位在冲突后被清除
ucComMF522Buf [ 0 ] = 0x93; //卡片防冲突命令
ucComMF522Buf [ 1 ] = 0x20;
cStatus = PcdComMF522 ( PCD_TRANSCEIVE, ucComMF522Buf, 2, ucComMF522Buf, & ulLen);//与卡片通信
if ( cStatus == MI_OK) //通信成功
{
for ( uc = 0; uc < 4; uc ++ )
{
* ( pSnr + uc ) = ucComMF522Buf [ uc ]; //读出UID
ucSnr_check ^= ucComMF522Buf [ uc ];
}
<span class="token keyword">if</span> <span class="token punctuation">(</span> ucSnr_check <span class="token operator">!=</span> ucComMF522Buf <span class="token punctuation">[</span> uc <span class="token punctuation">]</span> <span class="token punctuation">)</span>
cStatus <span class="token operator">=</span> MI_ERR<span class="token punctuation">;</span>
}
SetBitMask ( CollReg, 0x80 );
return cStatus;
}
/* 函数名:PcdSelect
-
描述 :选定卡片
-
输入 :pSnr,卡片序列号,4字节
-
返回 : 状态值
-
= MI_OK,成功
-
调用 :外部调用 /
char PcdSelect ( u8 pSnr )
{
char ucN;
u8 uc;
u8 ucComMF522Buf [ MAXRLEN ];
u32 ulLen;
ucComMF522Buf [ 0 ] = PICC_ANTICOLL1;
ucComMF522Buf [ 1 ] = 0x70;
ucComMF522Buf [ 6 ] = 0;
for ( uc = 0; uc < 4; uc ++ )
{
ucComMF522Buf [ uc + 2 ] = ( pSnr + uc );
ucComMF522Buf [ 6 ] ^= ( pSnr + uc );
}
CalulateCRC ( ucComMF522Buf, 7, & ucComMF522Buf [ 7 ] );
ClearBitMask ( Status2Reg, 0x08 );
ucN = PcdComMF522 ( PCD_TRANSCEIVE, ucComMF522Buf, 9, ucComMF522Buf, & ulLen );
if ( ( ucN MI_OK ) && ( ulLen 0x18 ) )
ucN = MI_OK;
else
ucN = MI_ERR;
return ucN;
}
/* 函数名:CalulateCRC
-
描述 :用RC522计算CRC16
-
输入 :pIndata,计算CRC16的数组
-
ucLen,计算CRC16的数组字节长度
-
pOutData,存放计算结果存放的首地址
-
返回 : 无
-
调用 :内部调用 /
void CalulateCRC ( u8 pIndata, u8 ucLen, u8 * pOutData )
{
u8 uc, ucN;
ClearBitMask(DivIrqReg,0x04);
WriteRawRC(CommandReg,PCD_IDLE);
SetBitMask(FIFOLevelReg,0x80);
for ( uc = 0; uc < ucLen; uc ++)
WriteRawRC ( FIFODataReg, * ( pIndata + uc ) );
WriteRawRC ( CommandReg, PCD_CALCCRC );
uc = 0xFF;
do {
ucN = ReadRawRC ( DivIrqReg );
uc --;}
while ( ( uc != 0 ) && ! ( ucN & 0x04 ) );
pOutData [ 0 ] = ReadRawRC ( CRCResultRegL );
pOutData [ 1 ] = ReadRawRC ( CRCResultRegM );
}
/* 函数名:PcdAuthState
-
描述 :验证卡片密码
-
输入 :ucAuth_mode,密码验证模式
-
= 0x60,验证A密钥
-
= 0x61,验证B密钥
-
u8 ucAddr,块地址
-
pKey,密码
-
pSnr,卡片序列号,4字节
-
返回 : 状态值
-
= MI_OK,成功
-
调用 :外部调用 /
char PcdAuthState ( u8 ucAuth_mode, u8 ucAddr, u8 pKey, u8 * pSnr )
{
char cStatus;
u8 uc, ucComMF522Buf [ MAXRLEN ];
u32 ulLen;
ucComMF522Buf [ 0 ] = ucAuth_mode;
ucComMF522Buf [ 1 ] = ucAddr;
for ( uc = 0; uc < 6; uc ++ )
ucComMF522Buf [ uc + 2 ] = * ( pKey + uc );
for ( uc = 0; uc < 6; uc ++ )
ucComMF522Buf [ uc + 8 ] = * ( pSnr + uc );
cStatus = PcdComMF522 ( PCD_AUTHENT, ucComMF522Buf, 12, ucComMF522Buf, & ulLen );
if ( ( cStatus != MI_OK ) || ( ! ( ReadRawRC ( Status2Reg ) & 0x08 ) ) ){
cStatus = MI_ERR;
}
return cStatus;
}
/* 函数名:PcdWrite
-
描述 :写数据到M1卡一块
-
输入 :u8 ucAddr,块地址
-
pData,写入的数据,16字节
-
返回 : 状态值
-
= MI_OK,成功
-
调用 :外部调用 /
char PcdWrite ( u8 ucAddr, u8 pData )
{
char cStatus;
u8 uc, ucComMF522Buf [ MAXRLEN ];
u32 ulLen;
ucComMF522Buf [ 0 ] = PICC_WRITE;
ucComMF522Buf [ 1 ] = ucAddr;
CalulateCRC ( ucComMF522Buf, 2, & ucComMF522Buf [ 2 ] );
cStatus = PcdComMF522 ( PCD_TRANSCEIVE, ucComMF522Buf, 4, ucComMF522Buf, & ulLen );
if ( ( cStatus != MI_OK ) || ( ulLen != 4 ) || ( ( ucComMF522Buf [ 0 ] & 0x0F ) != 0x0A ) )
cStatus = MI_ERR;
if ( cStatus == MI_OK )
{
memcpy(ucComMF522Buf, pData, 16);
for ( uc = 0; uc < 16; uc ++ )
ucComMF522Buf [ uc ] = * ( pData + uc );
CalulateCRC ( ucComMF522Buf, 16, & ucComMF522Buf [ 16 ] );
cStatus = PcdComMF522 ( PCD_TRANSCEIVE, ucComMF522Buf, 18, ucComMF522Buf, & ulLen );
<span class="token keyword">if</span> <span class="token punctuation">(</span> <span class="token punctuation">(</span> cStatus <span class="token operator">!=</span> MI_OK <span class="token punctuation">)</span> <span class="token operator">||</span> <span class="token punctuation">(</span> ulLen <span class="token operator">!=</span> <span class="token number">4</span> <span class="token punctuation">)</span> <span class="token operator">||</span> <span class="token punctuation">(</span> <span class="token punctuation">(</span> ucComMF522Buf <span class="token punctuation">[</span> <span class="token number">0</span> <span class="token punctuation">]</span> <span class="token operator">&</span> <span class="token number">0x0F</span> <span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token number">0x0A</span> <span class="token punctuation">)</span> <span class="token punctuation">)</span>
cStatus <span class="token operator">=</span> MI_ERR<span class="token punctuation">;</span>
}
return cStatus;
}
/* 函数名:PcdRead
-
描述 :读取M1卡一块数据
-
输入 :u8 ucAddr,块地址
-
pData,读出的数据,16字节
-
返回 : 状态值
-
= MI_OK,成功
-
调用 :外部调用 /
char PcdRead ( u8 ucAddr, u8 pData )
{
char cStatus;
u8 uc, ucComMF522Buf [ MAXRLEN ];
u32 ulLen;
ucComMF522Buf [ 0 ] = PICC_READ;
ucComMF522Buf [ 1 ] = ucAddr;
CalulateCRC ( ucComMF522Buf, 2, & ucComMF522Buf [ 2 ] );
cStatus = PcdComMF522 ( PCD_TRANSCEIVE, ucComMF522Buf, 4, ucComMF522Buf, & ulLen );
if ( ( cStatus MI_OK ) && ( ulLen 0x90 ) )
{
for ( uc = 0; uc < 16; uc ++ )
* ( pData + uc ) = ucComMF522Buf [ uc ];
}
else
cStatus = MI_ERR;
return cStatus;
}
/* 函数名:PcdHalt
-
描述 :命令卡片进入休眠状态
-
输入 :无
-
返回 : 状态值
-
= MI_OK,成功
-
调用 :外部调用 */
char PcdHalt( void )
{
u8 ucComMF522Buf [ MAXRLEN ];
u32 ulLen;
ucComMF522Buf [ 0 ] = PICC_HALT;
ucComMF522Buf [ 1 ] = 0;
CalulateCRC ( ucComMF522Buf, 2, & ucComMF522Buf [ 2 ] );
PcdComMF522 ( PCD_TRANSCEIVE, ucComMF522Buf, 4, ucComMF522Buf, & ulLen );
return MI_OK;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
- 324
- 325
- 326
- 327
- 328
- 329
- 330
- 331
- 332
- 333
- 334
- 335
- 336
- 337
- 338
- 339
- 340
- 341
- 342
- 343
- 344
- 345
- 346
- 347
- 348
- 349
- 350
- 351
- 352
- 353
- 354
- 355
- 356
- 357
- 358
- 359
- 360
- 361
- 362
- 363
- 364
- 365
- 366
- 367
- 368
- 369
- 370
- 371
- 372
- 373
- 374
- 375
- 376
- 377
- 378
- 379
- 380
- 381
- 382
- 383
- 384
- 385
- 386
- 387
- 388
- 389
- 390
- 391
- 392
- 393
- 394
- 395
- 396
- 397
- 398
- 399
- 400
- 401
- 402
- 403
- 404
- 405
- 406
- 407
- 408
- 409
- 410
- 411
- 412
- 413
- 414
- 415
- 416
- 417
- 418
- 419
- 420
- 421
- 422
- 423
- 424
- 425
- 426
- 427
- 428
- 429
- 430
- 431
- 432
- 433
- 434
- 435
- 436
- 437
- 438
- 439
- 440
- 441
- 442
- 443
- 444
- 445
- 446
- 447
- 448
- 449
- 450
- 451
- 452
- 453
- 454
- 455
- 456
- 457
- 458
- 459
- 460
- 461
- 462
- 463
- 464
- 465
- 466
- 467
- 468
- 469
- 470
- 471
- 472
- 473
- 474
- 475
- 476
- 477
- 478
- 479
- 480
- 481
- 482
- 483
- 484
- 485
- 486
- 487
- 488
- 489
- 490
- 491
- 492
- 493
- 494
- 495
- 496
- 497
- 498
- 499
- 500
- 501
- 502
- 503
- 504
- 505
- 506
- 507
- 508
- 509
- 510
- 511
- 512
- 513
- 514
- 515
- 516
- 517
- 518
- 519
- 520
- 521
- 522
- 523
- 524
- 525
- 526
- 527
- 528
- 529
- 530
- 531
- 532
- 533
- 534
- 535
- 536
- 537
- 538
- 539
- 540
- 541
- 542
- 543
- 544
- 545
- 546
- 547
- 548
- 549
- 550
- 551
- 552
- 553
- 554
- 555
- 556
- 557
- 558
- 559
- 560
- 561
- 562
- 563
- 564
- 565
- 566
- 567
- 568
- 569
- 570
- 571
- 572
- 573
- 574
- 575
- 576
- 577
- 578
- 579
- 580
- 581
- 582
- 583
- 584
- 585
- 586
- 587
- 588
- 589
- 590
- 591
- 592
- 593
- 594
- 595
- 596
- 597
- 598
- 599
- 600
- 601
- 602
- 603
- 604
- 605
- 606
- 607
- 608
- 609
- 610
- 611
- 612
- 613
- 614
- 615
- 616
- 617
- 618
- 619
- 620
- 621
- 622
- 623
- 624
- 625
- 626
- 627
- 628
- 629
- 630
- 631
- 632
- 633
- 634
- 635
- 636
- 637
- 638
- 639
- 640
- 641
- 642
- 643
- 644
- 645
- 646
- 647
- 648
- 649
- 650
- 651
- 652
- 653
- 654
- 655
- 656
3. 出现问题及解决方法:
本人在尝试读取卡片的时候也遇到了一个问题,调试过好几天硬件和软件后,还是不能寻到卡,keil单步调试一直表示寻卡返回状态参数为:MI_ERR,最后终于调试成功,错误原因在于开发板坏了……
不过在找错误的过程中也寻找了一些其他人调试失败的原因,引以为鉴:
- 硬件问题:这种情况一般出现在自己设计PCB的童鞋身上,那么这时候就应该先购买现成模块,在调试完代码并成功的基础上再调试硬件;
- SPI传输速率设置问题:SPI口例程中的预分频默认为4,而RC522中的SPI最高速率为10MHz/S,计算可知,预分频指数至少为8,所以适当升高预分频数,据反馈,预分频为8也容易出错,所以建议32或者64甚至为256;
- SPI时序问题:根据文档中的时序图,仔细设置SPI_InitStructure.SPI_CPOL和SPI_InitStructure.SPI_CPHA这两个参数;
- SPI口的GPIO模式设置:我以前在设计TM1638芯片为核心的灯、按键模组时也出现过这个问题,后来一般全部设置为推挽输出就基本不在出现这个问题;
- 天线在复位时需要先关闭再开启;