- #include "RC522.h"
- //三目运算符true取前面那个
- #define RS522_RST(N) HAL_GPIO_WritePin(RC522_RST_GPIO_Port, RC522_RST_Pin, N==1?GPIO_PIN_SET:GPIO_PIN_RESET)
- #define RS522_NSS(N) HAL_GPIO_WritePin(RC522_CS_GPIO_Port, RC522_CS_Pin, N==1?GPIO_PIN_SET:GPIO_PIN_RESET)
- #define osDelay HAL_Delay
- /**************************************************************************************
- * 函数名称:MFRC_Init
- * 功能描述:MFRC初始化
- * 入口参数:无
- * 出口参数:无
- * 返 回 值:无
- * 说 明:MFRC的SPI接口速率为0~10Mbps
- ***************************************************************************************/
- void MFRC_Init(void)
- {
- RS522_NSS(1);
- RS522_RST(1);
- }
- /**************************************************************************************
- * 函数名称: SPI_RW_Byte
- * 功能描述: 模拟SPI读写一个字节
- * 入口参数: -byte:要发送的数据
- * 出口参数: -byte:接收到的数据
- ***************************************************************************************/
- static uint8_t ret; //些函数是HAL与标准库不同和地方,【读写函数】
- uint8_t SPI2_RW_Byte(uint8_t byte)
- {
- HAL_SPI_TransmitReceive(&hspi2, &byte, &ret, 1, 10);//把byte写入,并读出一个值 存入ret
- return ret; //入口是byte的地址,读取时用的也是ret的地址;1:一次只写入一个值 10:timeout
- }
- /**************************************************************************************
- * 函数名称:MFRC_WriteReg
- * 功能描述:写一个寄存器
- * 入口参数:-addr:待写的寄存器地址
- * -data:待写的寄存器数据
- * 出口参数:无
- * 返 回 值:无
- * 说 明:无
- ***************************************************************************************/
- void MFRC_WriteReg(uint8_t addr, uint8_t data)
- {
- uint8_t AddrByte;
- AddrByte = (addr << 1 ) & 0x7E; //求出地址字节
- RS522_NSS(0); //NSS拉低
- SPI2_RW_Byte(AddrByte); //写地址字节
- SPI2_RW_Byte(data); //写数据
- RS522_NSS(1); //NSS拉高
- }
- /**************************************************************************************
- * 函数名称:MFRC_ReadReg
- * 功能描述:读一个寄存器
- * 入口参数:-addr:待读的寄存器地址
- * 出口参数:无
- * 返 回 值:-data:读到寄存器的数据
- * 说 明:无
- ***************************************************************************************/
- uint8_t MFRC_ReadReg(uint8_t addr)
- {
- uint8_t AddrByte, data;
- AddrByte = ((addr << 1 ) & 0x7E ) | 0x80; //求出地址字节
- RS522_NSS(0); //NSS拉低
- SPI2_RW_Byte(AddrByte); //写地址字节
- data = SPI2_RW_Byte(0x00); //读数据
- RS522_NSS(1); //NSS拉高
- return data;
- }
其他接口保持不变,我们来看一下RC522提供的接口和指令有哪些。
- #ifndef _RC522_H
- #define _RC522_H
- //头文件
- //************************************************
- #include "gpio.h"//要一些引脚上的宏定义
- #include "spi.h"//硬件SPI的定义
- #include "printf.h"
- #include "main.h"//Laber User上的宏定义
- //************************************************
- //MFRC522驱动程序
- //************************************************
- /*MFRC522寄存器定义*/
- //PAGE0
- #define MFRC_RFU00 0x00
- #define MFRC_CommandReg 0x01
- #define MFRC_ComIEnReg 0x02
- #define MFRC_DivlEnReg 0x03
- #define MFRC_ComIrqReg 0x04
- #define MFRC_DivIrqReg 0x05
- #define MFRC_ErrorReg 0x06
- #define MFRC_Status1Reg 0x07
- #define MFRC_Status2Reg 0x08
- #define MFRC_FIFODataReg 0x09
- #define MFRC_FIFOLevelReg 0x0A
- #define MFRC_WaterLevelReg 0x0B
- #define MFRC_ControlReg 0x0C
- #define MFRC_BitFramingReg 0x0D
- #define MFRC_CollReg 0x0E
- #define MFRC_RFU0F 0x0F
- //PAGE1
- #define MFRC_RFU10 0x10
- #define MFRC_ModeReg 0x11
- #define MFRC_TxModeReg 0x12
- #define MFRC_RxModeReg 0x13
- #define MFRC_TxControlReg 0x14
- #define MFRC_TxAutoReg 0x15 //中文手册有误
- #define MFRC_TxSelReg 0x16
- #define MFRC_RxSelReg 0x17
- #define MFRC_RxThresholdReg 0x18
- #define MFRC_DemodReg 0x19
- #define MFRC_RFU1A 0x1A
- #define MFRC_RFU1B 0x1B
- #define MFRC_MifareReg 0x1C
- #define MFRC_RFU1D 0x1D
- #define MFRC_RFU1E 0x1E
- #define MFRC_SerialSpeedReg 0x1F
- //PAGE2
- #define MFRC_RFU20 0x20
- #define MFRC_CRCResultRegM 0x21
- #define MFRC_CRCResultRegL 0x22
- #define MFRC_RFU23 0x23
- #define MFRC_ModWidthReg 0x24
- #define MFRC_RFU25 0x25
- #define MFRC_RFCfgReg 0x26
- #define MFRC_GsNReg 0x27
- #define MFRC_CWGsCfgReg 0x28
- #define MFRC_ModGsCfgReg 0x29
- #define MFRC_TModeReg 0x2A
- #define MFRC_TPrescalerReg 0x2B
- #define MFRC_TReloadRegH 0x2C
- #define MFRC_TReloadRegL 0x2D
- #define MFRC_TCounterValueRegH 0x2E
- #define MFRC_TCounterValueRegL 0x2F
- //PAGE3
- #define MFRC_RFU30 0x30
- #define MFRC_TestSel1Reg 0x31
- #define MFRC_TestSel2Reg 0x32
- #define MFRC_TestPinEnReg 0x33
- #define MFRC_TestPinValueReg 0x34
- #define MFRC_TestBusReg 0x35
- #define MFRC_AutoTestReg 0x36
- #define MFRC_VersionReg 0x37
- #define MFRC_AnalogTestReg 0x38
- #define MFRC_TestDAC1Reg 0x39
- #define MFRC_TestDAC2Reg 0x3A
- #define MFRC_TestADCReg 0x3B
- #define MFRC_RFU3C 0x3C
- #define MFRC_RFU3D 0x3D
- #define MFRC_RFU3E 0x3E
- #define MFRC_RFU3F 0x3F
- /*MFRC522的FIFO长度定义*/
- #define MFRC_FIFO_LENGTH 64
- /*MFRC522传输的帧长定义*/
- #define MFRC_MAXRLEN 18
- /*MFRC522命令集,中文手册P59*/
- #define MFRC_IDLE 0x00 //取消当前命令的执行
- #define MFRC_CALCCRC 0x03 //激活CRC计算
- #define MFRC_TRANSMIT 0x04 //发送FIFO缓冲区内容
- #define MFRC_NOCMDCHANGE 0x07 //无命令改变
- #define MFRC_RECEIVE 0x08 //激活接收器接收数据
- #define MFRC_TRANSCEIVE 0x0C //发送并接收数据
- #define MFRC_AUTHENT 0x0E //执行Mifare认证(验证密钥)
- #define MFRC_RESETPHASE 0x0F //复位MFRC522
- /*MFRC522通讯时返回的错误代码*/
- #define MFRC_OK (char)(0)
- #define MFRC_NOTAGERR (char)(-1)
- #define MFRC_ERR (char)(-2)
- /*MFRC522函数声明*/
- void MFRC_Init(void);
- void MFRC_WriteReg(uint8_t addr, uint8_t data);
- uint8_t MFRC_ReadReg(uint8_t addr);
- void MFRC_SetBitMask(uint8_t addr, uint8_t mask);
- void MFRC_ClrBitMask(uint8_t addr, uint8_t mask);
- void MFRC_CalulateCRC(uint8_t *pInData, uint8_t len, uint8_t *pOutData);
- char MFRC_CmdFrame(uint8_t cmd, uint8_t *pInData, uint8_t InLenByte, uint8_t *pOutData, uint16_t *pOutLenBit);
- //********************************************************************
- //MFRC552与MF1卡通讯接口程序
- //*********************************************************************
- /*Mifare1卡片命令字*/
- #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_TRANSFER 0xB0 //转存(传送)
- #define PICC_RESTORE 0xC2 //恢复(重储)
- #define PICC_HALT 0x50 //休眠
- /*PCD通讯时返回的错误代码*/
- #define PCD_OK (char)0 //成功
- #define PCD_NOTAGERR (char)(-1) //无卡
- #define PCD_ERR (char)(-2) //出错
- /*PCD函数声明*/
- void PCD_Init(void);//读写器初始化
- void PCD_Reset(void);
- void PCD_AntennaOn(void);
- void PCD_AntennaOff(void);
- char PCD_Request(uint8_t RequestMode, uint8_t *pCardType); //寻卡,并返回卡的类型
- char PCD_Anticoll(uint8_t *pSnr); //防冲突,返回卡号
- char PCD_Select(uint8_t *pSnr); //选卡
- char PCD_AuthState(uint8_t AuthMode, uint8_t BlockAddr, uint8_t *pKey, uint8_t *pSnr); //验证密码(密码A和密码B)
- char PCD_WriteBlock(uint8_t BlockAddr, uint8_t *pData); //写数据
- char PCD_ReadBlock(uint8_t BlockAddr, uint8_t *pData); //读数据
- char PCD_Value(uint8_t mode, uint8_t BlockAddr, uint8_t *pValue);
- char PCD_BakValue(uint8_t sourceBlockAddr, uint8_t goalBlockAddr);
- char PCD_Halt(void);
- //******************************************************************************************
- #endif
不过接下来我们需要测试一下,SPI是否正常,接上便携示波器,最近出门在外,不方便带示波器,所以带了一个LOTO的便携示波器,不过他刚好有逻辑分析仪的功能,刚好测试一下它的性能,接线图如下,需要将ChA口接到时钟线上,这样才能执行触发功能。
图9
然后将代码进入调试,进入读寄存器函数,在进入前打个断点,然后开启LOTO示波器的触发功能,然后运行到读取结束,可以看到读取到了【0x83】这个值。
图10
再来看看逻辑分析仪读取到的值,可以看到也是【0x83】,说明这个逻辑分析仪性能还行。
图11
SPI功能测试完了,接下来就要进行读写卡了。首先科普一下读写卡的整个过程【寻卡-》放冲撞-》选卡-》解密卡-》读/写卡】。
按照上面的流程,调用相关的函数,整体代码如下。
- /* USER CODE BEGIN Header */
- /**
- ******************************************************************************
- * [url=home.php?mod=space&uid=288409]@file[/url] : main.c
- * [url=home.php?mod=space&uid=247401]@brief[/url] : Main program body
- ******************************************************************************
- * @attention
- *
- * <h2><center>© Copyright (c) 2021 STMicroelectronics.
- * All rights reserved.</center></h2>
- *
- * This software component is licensed by ST under BSD 3-Clause license,
- * the "License"; You may not use this file except in compliance with the
- * License. You may obtain a copy of the License at:
- * opensource.org/licenses/BSD-3-Clause
- *
- ******************************************************************************
- */
- /* USER CODE END Header */
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include "spi.h"
- #include "usart.h"
- #include "gpio.h"
- /* Private includes ----------------------------------------------------------*/
- /* USER CODE BEGIN Includes */
- #include "delay.h"
- #include "printf.h"
- #include "rc522.h"
- #include "string.h"
- /* USER CODE END Includes */
- /* Private typedef -----------------------------------------------------------*/
- /* USER CODE BEGIN PTD */
- /* USER CODE END PTD */
- /* Private define ------------------------------------------------------------*/
- /* USER CODE BEGIN PD */
- /* USER CODE END PD */
- /* Private macro -------------------------------------------------------------*/
- /* USER CODE BEGIN PM */
- /* USER CODE END PM */
- /* Private variables ---------------------------------------------------------*/
- /* USER CODE BEGIN PV */
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- /* USER CODE BEGIN PFP */
- /* USER CODE END PFP */
- /* Private user code ---------------------------------------------------------*/
- /* USER CODE BEGIN 0 */
- uint8_t key_A[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
- char *WriteData = {"1234567890ABCDEF"};
- char ReadData[16] = {0};
- /* USER CODE END 0 */
- /**
- * @brief The application entry point.
- * @retval int
- */
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- char pcd_err = 0;
- /* USER CODE END 1 */
- /* MCU Configuration--------------------------------------------------------*/
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- /* USER CODE BEGIN Init */
- /* USER CODE END Init */
- /* Configure the system clock */
- SystemClock_Config();
- /* USER CODE BEGIN SysInit */
- /* USER CODE END SysInit */
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_USART1_UART_Init();
- MX_SPI2_Init();
- /* USER CODE BEGIN 2 */
- //初始化
- //*************************
- PCD_Init();//RC522初始化
- //*************************
- //全局变量
- //*************************
- uint8_t RxBuffer[4];
- char Card_ID[8];
- //*************************
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- pcd_err = PCD_Request(PICC_REQIDL, RxBuffer);//返回值为0,代表寻卡成功;并把卡类型存入RxBuffer中
- if(pcd_err == PCD_OK)
- {
- uint16_t cardType = (RxBuffer[0] << 8) | RxBuffer[1];
-
- printf("卡类型:0x%04X\r\n", cardType);
-
- pcd_err = PCD_Anticoll(RxBuffer); //防冲撞,完成这部就可以简单地 读取卡号,本次不涉及更高层次应用
- if(pcd_err == PCD_OK)
- {
- sprintf(Card_ID,"%x%x%x%x",RxBuffer[0],RxBuffer[1],RxBuffer[2],RxBuffer[3]);
- printf("ID=%s\r\n",Card_ID);
- }
-
- pcd_err = PCD_Select((uint8_t *)RxBuffer); //选卡
- if(pcd_err == PCD_OK)
- {
- printf("Select Card OK\r\n");
- }
- else
- {
- printf("Select Card Error\r\n");
- }
-
- pcd_err = PCD_AuthState(PICC_AUTHENT1A, 5, key_A, RxBuffer); //解密
- if(pcd_err == PCD_OK)
- {
- printf("Auth Card OK\r\n");
- }
- else
- {
- printf("Auth Card Error\r\n");
- }
-
- pcd_err = PCD_WriteBlock(6, (uint8_t *)WriteData); //写卡
- if(pcd_err == PCD_OK)
- {
- printf("写卡成功\r\n");
- }
- else
- {
- printf("写卡失败:%d\r\n",pcd_err);
- }
-
- HAL_Delay(1);
- pcd_err = PCD_ReadBlock(6, (uint8_t *)ReadData); //读卡
- if(pcd_err == PCD_OK)
- {
- printf("读卡成功:%s\r\n", ReadData);
- }
- else
- {
- printf("读卡失败:%d\r\n",pcd_err);
- }
-
- PCD_Halt();
-
- memset(RxBuffer, 0, sizeof(RxBuffer));//清空字符串,这里要清除RxBuffer才行
- HAL_Delay(1000);
- }
- HAL_Delay(100);
- }
- /* USER CODE END 3 */
- }
- /**
- * @brief System Clock Configuration
- * @retval None
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct = {0};
- RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
- /** Initializes the RCC Oscillators according to the specified parameters
- * in the RCC_OscInitTypeDef structure.
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
- RCC_OscInitStruct.HSEState = RCC_HSE_ON;
- RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
- RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- Error_Handler();
- }
- /** Initializes the CPU, AHB and APB buses clocks
- */
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
- |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
- {
- Error_Handler();
- }
- }
- /* USER CODE BEGIN 4 */
- /* USER CODE END 4 */
- /**
- * @brief This function is executed in case of error occurrence.
- * @retval None
- */
- void Error_Handler(void)
- {
- /* USER CODE BEGIN Error_Handler_Debug */
- /* User can add his own implementation to report the HAL error return state */
- __disable_irq();
- printf("error\r\n");
- while (1)
- {
- }
- /* USER CODE END Error_Handler_Debug */
- }
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t *file, uint32_t line)
- {
- /* USER CODE BEGIN 6 */
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* USER CODE END 6 */
- }
- #endif /* USE_FULL_ASSERT */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
代码是先进**,然后进**,再进**。关于解密,卡默认的密码是【FFFFFFFFFFFF】,一共是6个【0xFF】。
最终的输出结果如下图12所示,写入【“1234567890ABCDEF”】内容,读出的也是【“1234567890ABCDEF”】内容。
图12
5.总结
SPI配置下来还是比较简单的,这个工程最主要的还是得读懂RC522的工作流程,如果能对IC卡进行读写,项目的基本功能就实现了,后续只要调用相关的接口接可以了,这次的分享就到这里了!
项目工程(由于分卷原因,需要重新命名,把.00x放在.zip后面):
RC522.001.zip
(9 MB, 下载次数: 32)
RC522.002.zip
(7.62 MB, 下载次数: 27)