rc522.c
rc522.c
如下:
#include <string.h>
#include "sys.h"
#include "delay.h"
#include "rc522.h"
#define MF522_RST PCout(0)
#define MF522_SI PCout(1)
#define MF522_SCK PCout(2)
#define MF522_NSS PCout(3)
#define MF522_SO PCin(4)
void rc522_init ( void ) {
/* rc522模块的初始化 */
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd ( RCC_APB2Periph_GPIOC, ENABLE );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; /* 推挽输出 */
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init ( GPIOC, &GPIO_InitStructure );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init ( GPIOC, &GPIO_InitStructure );
}
/*--------------------------------------------------------------------------------------
* 功能:寻卡,成功则返回MI_OK
* 参数:req_code:寻卡方式,0x52为寻感应区内所有符合14443A标准的卡,0x26为寻未进入休眠状态的卡
* pTagType:卡片类型代码。0x4400为Mifare_UltraLight,0x0400为Mifare_One(S50),
* 0x0200为Mifare_One(S70),0x0800为Mifare_Pro(X),0x4403为Mifare_DESFire
*-------------------------------------=-----------------------------------------------*/
char PcdRequest ( unsigned char req_code, unsigned char *pTagType ) {
char status;
unsigned int unLen;
unsigned char ucComMF522Buf[MAXRLEN];
ClearBitMask ( Status2Reg, 0x08 );
WriteRawRC ( BitFramingReg, 0x07 );
SetBitMask ( TxControlReg, 0x03 );
ucComMF522Buf[0] = req_code;
status = PcdComMF522 ( PCD_TRANSCEIVE, ucComMF522Buf, 1, ucComMF522Buf, &unLen );
if ( ( status == MI_OK ) && ( unLen == 0x10 ) ) {
*pTagType = ucComMF522Buf[0];
* ( pTagType + 1 ) = ucComMF522Buf[1];
} else {
status = MI_ERR;
}
return status;
}
char PcdAnticoll ( unsigned char *pSnr ) {
/* 防冲突。参数pSnr是卡片序列号(4字节) */
char status;
unsigned char i, snr_check = 0;
unsigned int unLen;
unsigned char ucComMF522Buf[MAXRLEN];
ClearBitMask ( Status2Reg, 0x08 );
WriteRawRC ( BitFramingReg, 0x00 );
ClearBitMask ( CollReg, 0x80 );
ucComMF522Buf[0] = PICC_ANTICOLL1;
ucComMF522Buf[1] = 0x20;
status = PcdComMF522 ( PCD_TRANSCEIVE, ucComMF522Buf, 2, ucComMF522Buf, &unLen );
if ( status == MI_OK ) {
for ( i = 0; i < 4; i++ ) {
* ( pSnr + i ) = ucComMF522Buf[i];
snr_check ^= ucComMF522Buf[i];
}
if ( snr_check != ucComMF522Buf[i] ) {
status = MI_ERR;
}
}
SetBitMask ( CollReg, 0x80 );
return status;
}
char PcdSelect ( unsigned char *pSnr ) {
/* 选定卡片。参数pSnr是卡片序列号(4字节) */
char status;
unsigned char i;
unsigned int unLen;
unsigned char ucComMF522Buf[MAXRLEN];
ucComMF522Buf[0] = PICC_ANTICOLL1;
ucComMF522Buf[1] = 0x70;
ucComMF522Buf[6] = 0;
for ( i = 0; i < 4; i++ ) {
ucComMF522Buf[i + 2] = * ( pSnr + i );
ucComMF522Buf[6] ^= * ( pSnr + i );
}
CalulateCRC ( ucComMF522Buf, 7, &ucComMF522Buf[7] );
ClearBitMask ( Status2Reg, 0x08 );
status = PcdComMF522 ( PCD_TRANSCEIVE, ucComMF522Buf, 9, ucComMF522Buf, &unLen );
if ( ( status == MI_OK ) && ( unLen == 0x18 ) ) {
status = MI_OK;
} else {
status = MI_ERR;
}
return status;
}
/*-----------------------------------------------------------------------------------------------------------
* 功能:验证卡片密码。成功则返回MI_OK
* 参数:auth_mode:密码验证模式,0x60为验证A密钥,0x61为验证B密钥;addr:块地址;pKey:密码;pSnr:卡片序列号(4字节)
* ---------------------------------------------------------------------------------------------------------*/
char PcdAuthState ( unsigned char auth_mode, unsigned char addr, unsigned char *pKey, unsigned char *pSnr ) {
char status;
unsigned int unLen;
unsigned char i, ucComMF522Buf[MAXRLEN];
ucComMF522Buf[0] = auth_mode;
ucComMF522Buf[1] = addr;
for ( i = 0; i < 6; i++ ) {
ucComMF522Buf[i + 2] = * ( pKey + i );
}
for ( i = 0; i < 6; i++ ) {
ucComMF522Buf[i + 8] = * ( pSnr + i );
}
status = PcdComMF522 ( PCD_AUTHENT, ucComMF522Buf, 12, ucComMF522Buf, &unLen );
if ( ( status != MI_OK ) || ( ! ( ReadRawRC ( Status2Reg ) & 0x08 ) ) ) {
status = MI_ERR;
}
return status;
}
char PcdRead ( unsigned char addr, unsigned char *pData ) {
/* 读取M1卡数据。参数addr是块地址,pData是读出的数据(16字节) */
char status;
unsigned int unLen;
unsigned char i, ucComMF522Buf[MAXRLEN];
ucComMF522Buf[0] = PICC_READ;
ucComMF522Buf[1] = addr;
CalulateCRC ( ucComMF522Buf, 2, &ucComMF522Buf[2] );
status = PcdComMF522 ( PCD_TRANSCEIVE