[STC32G application] RC522 card reader module operates CPU card and RATS protocol

Series Article Directory

提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加
For example: the use of pandas in Chapter 1 Introduction to Python Machine Learning


提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档


foreword

提示:这里可以添加本文要记录的大概内容:

A recent small project, read the CPU card through the card reader IC, of ​​course, the CPU encryption card is impossible to be cracked, so there is no good way, only to read the characteristics of some CPU cards through RATS, and make some judgments .
Regarding the RATS protocol, we refer to the information on the Internet, which is more detailed: https://www.lmlphp.com/user/83786/article/item/880503/
Let me share some operations related to STC32G.


提示:以下是本篇文章正文内容,下面案例可供参考

1. STC32G driver for RC522

The first is the definition and operation of IO.

//
// IO定义
//
sbit  MCU_SDA   = P0^6;
sbit  MCU_SCK   = P0^7;
sbit  MCU_MOSI  = P1^0;
sbit  MCU_MISO  = P1^1;
sbit  MCU_ISR   = P2^3;
sbit  MCU_RST   = P2^4;

#define NSS522_1        MCU_SDA = 1
#define NSS522_0        MCU_SDA = 0

#define SCK522_1        MCU_SCK = 1
#define SCK522_0        MCU_SCK = 0

#define SI522_1         MCU_MOSI = 1
#define SI522_0         MCU_MOSI = 0

#define SO522           MCU_MISO

#define RST522_1        MCU_RST = 1
#define RST522_0        MCU_RST = 0

After the I/O is defined, the mode of the I/O port can be initialized through the program.

    P1_MODE_IN_HIZ(GPIO_Pin_1);         //P4.1设置为高阻输入
    P1_PULL_UP_ENABLE(GPIO_Pin_1);      //P4.1 开启内部上拉

    P2_MODE_IN_HIZ(GPIO_Pin_3);         //P2.3设置为高阻输入
    P2_PULL_UP_ENABLE(GPIO_Pin_3);      //P2.3 开启内部上拉

    P0_MODE_OUT_PP(GPIO_Pin_6|GPIO_Pin_7);  //P0.6 P0.7 设置推挽模式
    P1_MODE_OUT_PP(GPIO_Pin_0);         //P1.0 设置为准双向口
    P2_MODE_OUT_PP(GPIO_Pin_4);   //P2.3 P2.4 设置推挽模式

    MCU_SDA = 1;
    MCU_SCK = 1;
    MCU_MOSI = 1;
    MCU_RST = 1;

In this way, the definition of the I/O port is completed, and the SPI part of the entire RC522 is simulated by the I/O port, which is more convenient and has strong portability.

2. RATS operation

1. Operation sequence

The card reading operation is the same as the previous method, firstly configure the RC522 chip, then power on the coil, and then PcdRequest, PcdAnticoll, PcdSelect. After getting the card ID, call PcdRats to get the data of RATS response.

2. PcdRats code

The code is as follows (example):

//*************************************************************************
// 函数名	:PcdRats
// 描述		:转入APDU命令格式
// 入口		: 无
// 出口		: DataOut 输出的数据,	Len 输出数据的长度
// 返回		:成功返回MI_OK
//*************************************************************************

char PcdRats(unsigned char * DataOut,unsigned char * Len)
{
    
    
    char status = MI_ERR;
    unsigned int unLen;
    unsigned char ucComMF522Buf[MAXRLEN];

    ClearBitMask(Status2Reg,0x08);	// 清空校验成功标志,清除MFCrypto1On位

    memset(ucComMF522Buf, 0x00, MAXRLEN);

    ucComMF522Buf[0] = 0xE0;
    ucComMF522Buf[1] = 0x51;

    CalulateCRC(ucComMF522Buf,2,&ucComMF522Buf[2]);	// 生成发送内容的CRC校验,保存到最后两个字节

    status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,4,DataOut,&unLen);// 将收到的卡片类型号保存

    if (status == MI_OK)
    {
    
    
        Len[0]= unLen/8-2;
        status = MI_OK;
    }
    else
        status = MI_ERR;
    return status;          //返回结果
}

3. Rats response data

insert image description here

The feedback data TL refers to the data length, T0 indicates the format of the following data, and TA-TC includes some card attributes, such as bit rate bit capability and some characteristics of communication. The subsequent history bytes contain the previously transmitted card IDs.

If you cannot read the information of the CPU card, you can also do a simple screening through the data responded by RATS.


Summarize

提示:这里对文章进行总结:

Guess you like

Origin blog.csdn.net/lunzilx/article/details/131393244