NRF52832学习笔记(40)——RFID RC522使用

Posted Leung_ManWah

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NRF52832学习笔记(40)——RFID RC522使用相关的知识,希望对你有一定的参考价值。

一、简介

MF RC522 是应用于 13.56MHz 非接触式通信中高集成度读写卡系列芯片中的一员。是 NXP 公司针对“三表”应用推出的一款低电压、低成本、体积小的非接触式读写卡芯片,是智能仪表和便携式手持设备研发的较好选择。

MFRC522数据手册: https://pan.baidu.com/s/10v68Z7sCFFSwPgrZ2eHtXw?pwd=d4fw 提取码:d4fw

二、硬件连接

功能口引脚
MISO25
MOSI24
CLK23
CSN22
RST26

三、添加SPI驱动

查看 NRF52832学习笔记(5)——SPI接口使用
不勾选EasyDMA

四、工程代码

百度网盘:https://pan.baidu.com/s/1vIkWuXDgTIYb8vAT8yfUMA?pwd=xprr 提取码:xprr

board_gpio.cboard_gpio.hboard_mfrc522.cboard_mfrc522.hboard_spi.cboard_spi.h 两个文件加入工程的Application文件夹下

4.1 board_gpio.c

/*********************************************************************
 * INCLUDES
 */
#include "nrf_gpio.h"
#include "app_error.h"

#include "board_gpio.h"	

/*********************************************************************
 * PUBLIC FUNCTIONS
 */
/**
 @brief NFC复位引脚初始化
 @param 无
 @return 无
*/
void NFC_GPIO_Init(void)

    nrf_gpio_cfg_output(NFC_RST_GPIO_PIN);
    NFC_GPIO_Write(NFC_RST_HIGH);


/**
 @brief 配置NFC复位引脚工作模式
 @param mode -[in] 工作模式
 @return 无
*/
void NFC_GPIO_Write(uint8_t mode)

    nrf_gpio_pin_write(NFC_RST_GPIO_PIN, mode);


/****************************************************END OF FILE****************************************************/

4.2 board_gpio.h

#ifndef _BOARD_GPIO_H_
#define _BOARD_GPIO_H_

/*********************************************************************
 * INCLUDES
 */
#include "nrf_gpio.h"

/*********************************************************************
 * DEFINITIONS
 */
/*=========================================================================*/
/*                                   输出                                  */
/*=========================================================================*/
#define NFC_RST_GPIO_PIN                26
#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 "nrf_drv_spi.h"
#include "nrf_gpio.h"
#include "app_error.h"

#include "board_spi.h"	

static void spiEventCallback(nrf_drv_spi_evt_t const *pEvent, void *arg);

/*********************************************************************
 * LOCAL VARIABLES
 */
static volatile bool s_transferOk = true;  										// SPI数据传输完成标志
static const nrf_drv_spi_t s_spiHandle = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE);	// SPI instance

/*********************************************************************
 * PUBLIC FUNCTIONS
 */
/**
 @brief NFC SPI驱动初始化
 @param 无
 @return 无
*/
void NFC_SPI_Init(void)

	ret_code_t errCode;
	
	nrf_drv_spi_config_t spiConfig = NRF_DRV_SPI_DEFAULT_CONFIG;				// 使用SPI默认配置
	// 配置SPI端口,注意CSN不要在这设置,另外用GPIO口控制
	spiConfig.miso_pin = NFC_SPI_MISO_PIN;
	spiConfig.mosi_pin = NFC_SPI_MOSI_PIN;
	spiConfig.sck_pin = NFC_SPI_SCLK_PIN;
	spiConfig.mode = NRF_DRV_SPI_MODE_0;
	spiConfig.frequency = SPI_FREQUENCY_FREQUENCY_K250;	
	spiConfig.irq_priority = 4;													// 在定时器中使用优先级需小于6
	
	errCode = nrf_drv_spi_init(&s_spiHandle, &spiConfig, spiEventCallback, NULL);
	APP_ERROR_CHECK(errCode);
	
	nrf_gpio_cfg_output(NFC_SPI_CS_PIN);


/**
 @brief NFC SPI读出写入数据
 @param pWriteData -[in] 写入数据
 @param pReadData -[out] 读出数据
 @param writeDataLen -[in] 写入数据长度
 @return 无
*/
void NFC_SPI_Transfer(uint8_t *pWriteData, uint8_t *pReadData, uint8_t writeDataLen)

	s_transferOk = false;

	APP_ERROR_CHECK(nrf_drv_spi_transfer(&s_spiHandle, pWriteData, writeDataLen, pReadData, writeDataLen));

	while(!s_transferOk)
	
		__WFE();
																				// Error in SPI or transfer already in progress.


/**
 @brief 开启SPI,与初始化区别:没有初始化CS引脚
 @param 无
 @return 无
*/
void NFC_SPI_Enable(void)

	ret_code_t errCode;
	
	nrf_drv_spi_config_t spiConfig = NRF_DRV_SPI_DEFAULT_CONFIG;				// 使用SPI默认配置
	// 配置SPI端口,注意CSN不要在这设置,另外用GPIO口控制
	spiConfig.miso_pin = NFC_SPI_MISO_PIN;
	spiConfig.mosi_pin = NFC_SPI_MOSI_PIN;
	spiConfig.sck_pin = NFC_SPI_SCLK_PIN;
	spiConfig.mode = NRF_DRV_SPI_MODE_0;
	spiConfig.frequency = SPI_FREQUENCY_FREQUENCY_K250;	
	spiConfig.irq_priority = 4;													// 在定时器中使用优先级需小于6
	
	errCode = nrf_drv_spi_init(&s_spiHandle, &spiConfig, spiEventCallback, NULL);
	APP_ERROR_CHECK(errCode);


/**
 @brief 禁用SPI
 @param 无
 @return 无
*/
void NFC_SPI_Disable(void)

	nrf_drv_spi_uninit(&s_spiHandle);



/*********************************************************************
 * LOCAL FUNCTIONS
 */
/**
 @brief SPI事件处理回调函数
 @param 无
 @return 无
*/
static void spiEventCallback(nrf_drv_spi_evt_t const *pEvent, void *arg)

	s_transferOk = true;


/****************************************************END OF FILE****************************************************/

4.4 board_spi.h

#ifndef _BOARD_SPI_H_
#define _BOARD_SPI_H_

/*********************************************************************
 * INCLUDES
 */
#include "nrf_gpio.h"

/*********************************************************************
 * DEFINITIONS
 */
#define NFC_SPI_MISO_PIN            25
#define NFC_SPI_MOSI_PIN            24
#define NFC_SPI_SCLK_PIN            23
#define NFC_SPI_CS_PIN              22

#define SPI_CS_HIGH                 nrf_gpio_pin_write(NFC_SPI_CS_PIN, 1)
#define SPI_CS_LOW                  nrf_gpio_pin_write(NFC_SPI_CS_PIN, 0)

#define SPI_INSTANCE  				0			// SPI instance index 

/*********************************************************************
 * API FUNCTIONS
 */
void NFC_SPI_Init(void);
void NFC_SPI_Transfer(uint8_t *pWriteData, uint8_t *pReadData, uint8_t writeDataLen);
void NFC_SPI_Enable(void);
void NFC_SPI_Disable(void);

#endif /* _BOARD_SPI_H_ */

4.5 board_mfrc522.c

/*********************************************************************
 * INCLUDES
 */
#include "nrf_delay.h"
#include "nrf_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];                                               // 卡序列号
static uint8_t s_defaultKeyA[6] = 0x00, 0x00, 0x00, 0x00, 0x00, 0x00;         // 默认密码A

/*********************************************************************
 * PUBLIC FUNCTIONS
 */
/**
 @brief MFRC522的初始化函数
 @param 无
 @return 无
*/
void MFRC522_Init(void)

    pcdReset();                                 // 复位
    delayMs(5);
//    NRF_LOG_INFO("reg: %02x" ,readRawRc(Status1Reg));
//    NRF_LOG_INFO("reg: %02x" ,readRawRc(Status2Reg));
//    NRF_LOG_INFO("reg: %02x" ,readRawRc(WaterLevelReg));
    pcdAntennaOn();                             // 开启天线发射


/**
 @brief MFRC522读取卡片块数据
 @param addr -[in] 块地址
 @return 状态值,0 - 成功;2 - 无卡;3 - 防冲撞失败;4 - 选卡失败;5 - 密码错误
*/
uint8_t MFRC522_ReadCardDataBlock(uint8_t addr)

    memset(s_cardSerialNo, 0, 4);

    if(pcdRequest(PICC_REQALL, s_cardType) == MI_OK)
    
    
    else
    
        NRF_LOG_INFO("ERR: 2");
        return 2;                               // 无卡
    

    if(pcdAnticoll(s_cardSerialNo) == MI_OK)
    
    
    else
    
        NRF_LOG_INFO("ERR: 3");
        return 3;                               // 防冲撞失败
    

    if(pcdSelect(s_cardSerialNo) == MI_OK)
    
    
    else
    
        NRF_LOG_INFO("ERR: 4");
        return 4;                               // 选卡失败
    

    if(pcdAuthState(PICC_AUTHENT1A, addr, s_defaultKeyA, s_cardSerialNo) == MI_OK)
    
        NRF_LOG_INFO("ERR: 0");
        return 0;
    
    else
    
        NRF_LOG_INFO("ERR: 5");
        return 5;                               // 密码错误
    


/**
 @brief 读取卡片序列号
 @param pCardSerialNo -[out] 卡片序列号
 @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 寻感应区内所有符合1443A标准的卡,0x26 寻未进入休眠状态的卡
 @param pTagType -[out] 卡片类型代码
                        0x4400 = Mifare_UltraLight
                        0x0400 = Mifare_One(S50)
                        0x0200 = Mifare_One(S70)
                        0x0800 = Mifare_Pro(X)
                        0x4403 = Mifare_DESFire
 @return 状态值,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);    // 发送并接收数据
    if((status == MI_OK) && (len == 0x10))
    
        NRF_LOG_INFO("mi_ok");
        *pTagType = comMF522Buf[0];
        *(pTagType+1) = comMF522Buf[1];
    
    else
    
        NRF_LOG_INFO("mi_err");
        status = MI_ERR;
    

    return status;


/**
 @brief 防冲撞
 @param pSnr -[out] 卡片序列号,4字节
 @return 状态值,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);             // 寄存器包含接收器和发送器和数据模式检测器的状态标志
    writeRawRc(BitFramingReg, 0x00);            // 不启动数据发送,接收的LSB位存放在位0,接收到的第二位放在位1,定义发送的最后一个字节位数为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])          // 返回四个字节,最后一个字节为校验位
        
            status = MI_ERR;
        
    

    setBitMask(CollReg, 0x80);

    return status;


/**
 @brief 选定卡片
 @param pSnr -[in] 卡片序列号,4字节
 @return 状态值,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 验证卡片密码
 @param authMode -[in] 密码验证模式,0x60 验证A密钥,0x61 验证B密钥
 @param addr -[in] 块地址
 @param pKey -[in] 密码
 @param pSnr -[in] 卡片序列号,4字节
 @return 状态值,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卡一块数据
 @param addr -[in] 块地址
 @param pData -[out] 读出的数据,16字节
 @return 状态值,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])nrf52832 学习笔记配对和绑定

nrf52832 学习笔记蓝牙主从机连接和连接参数更新

nrf52832 学习笔记蓝牙主机发现服务

nrf52832 学习笔记蓝牙主机扫描

nrf52832 学习笔记蓝牙主机扫描

nrf52832 学习笔记SDK框架分析