Files
99_7018_lmx/apps/earphone/remote_control/RFID/rfid_event.c

301 lines
7.5 KiB
C
Raw Normal View History

2025-11-24 16:33:33 +08:00
/********************************************************************************************************
* @file rfid_main.c
* @brief RFID
********************************************************************************************************/
#include "./include/rfid_main.h"
#include "./include/READER.h"
#include "./include/READER_REG.h"
#include "./include/MIFARE.h"
#include "./include/NTAG.h"
#include "./include/CPU_CARD.h"
#include "./rfid_hal.h"
2025-11-24 18:58:32 +08:00
#define FUN_ENABLE_XLOG 1
2025-11-24 16:33:33 +08:00
#ifdef xlog
#undef xlog
#endif
2025-11-24 18:58:32 +08:00
#if FUN_ENABLE_XLOG
2025-11-24 16:33:33 +08:00
#define xlog(format, ...) printf("[XT:%s] " format, __func__, ##__VA_ARGS__)
#else
#define xlog(format, ...) ((void)0)
#endif
/**
* @brief Type A卡片事件
* @details
* ISO/IEC 14443 Type A卡片的完整激活流程
* 1. Type A协议
* 2. RF场
* 3. RequestAnticollision
* 4. SAKSelect AcknowledgeMifare, NTAG, CPU卡
* 5. RF场
* @return
*/
void TYPE_A_EVENT(void)
{
unsigned char result;
int i;
xlog("TYPE_A_EVENT begin\n");
// 初始化读卡器为Type A模式
result = ReaderA_Initial();
if (result != SUCCESS)
{
xlog("INIT_ERROR\r\n");
2025-11-24 18:58:32 +08:00
SetCW(FUN_DISABLE);
2025-11-24 16:33:33 +08:00
return;
}
// 打开RF场载波
2025-11-24 18:58:32 +08:00
result = SetCW(FUN_ENABLE);
2025-11-24 16:33:33 +08:00
if (result != SUCCESS)
{
xlog("CW_ERROR\r\n");
2025-11-24 18:58:32 +08:00
SetCW(FUN_DISABLE);
2025-11-24 16:33:33 +08:00
return;
}
// 激活Type A卡片
result = ReaderA_CardActivate(&PICC_A);
if (result != SUCCESS)
{
// xlog("ReaderA_CardActivate_ERROR\r\n");
2025-11-24 18:58:32 +08:00
SetCW(FUN_DISABLE);
2025-11-24 16:33:33 +08:00
return;
}
xlog("************* TYPE A CARD ************* \r\n");
xlog("-> ATQA = %02X%02X\r\n", PICC_A.ATQA[0], PICC_A.ATQA[1]);
if (PICC_A.UID_Length > 0)
{
xlog("-> UID = ");
for (i = 0; i < PICC_A.UID_Length; i++)
{
xlog("%02X", PICC_A.UID[i]);
}
xlog("\r\n");
}
xlog("-> SAK = %02X\r\n", PICC_A.SAK[0]);
// 根据SAK值判断卡片类型
if (PICC_A.SAK[0] == 0x08)
{
xlog("************* Mifare CARD ************* \r\n");
result = MIFARE_CARD_EVENT();
}
else if ((PICC_A.SAK[0] == 0x28) || (PICC_A.SAK[0] == 0x20))
{
xlog("************* CPU CARD ************* \r\n");
result = CPU_CARD_EVENT();
}
else if (PICC_A.SAK[0] == 0x04)
{
xlog("************* NTAG CARD ************* \r\n");
result = NTAG_EVENT();
}
2025-11-24 18:58:32 +08:00
SetCW(FUN_DISABLE); // 关闭RF场
2025-11-24 16:33:33 +08:00
}
/**
* @brief Type B卡片事件
* @details
* ISO/IEC 14443 Type B卡片的激活流程
* 1. Type B协议
* 2. RF场
* 3. REQB/WUPB命令寻卡
* 4. ATTRIB命令选卡
* 5. SN
* 6. RF场
* @return
*/
void TYPE_B_EVENT(void)
{
unsigned char result;
int i;
xlog("TYPE_B_EVENT begin\n");
ReaderB_Initial();
2025-11-24 18:58:32 +08:00
SetCW(FUN_ENABLE);
2025-11-24 16:33:33 +08:00
result = ReaderB_Request(&PICC_B);
if (result != SUCCESS)
{
2025-11-24 18:58:32 +08:00
SetCW(FUN_DISABLE);
2025-11-24 16:33:33 +08:00
return;
}
xlog("************* TYPE B CARD ************* \r\n");
// 打印ATQB信息
xlog("-> ATQB = ");
for(i=0; i<12; i++) xlog("%02X", PICC_B.ATQB[i]);
xlog("\r\n");
result = ReaderB_Attrib(&PICC_B);
if (result != SUCCESS)
{
2025-11-24 18:58:32 +08:00
SetCW(FUN_DISABLE);
2025-11-24 16:33:33 +08:00
return;
}
xlog("-> ATTRIB = %02X\r\n", PICC_B.CID);
result = ReaderB_Get_SN(&PICC_B);
if (result != SUCCESS)
{
2025-11-24 18:58:32 +08:00
SetCW(FUN_DISABLE);
2025-11-24 16:33:33 +08:00
return;
}
xlog("-> SN = ");
for(i=0; i<8; i++) xlog("%02X", PICC_B.SN[i]);
xlog("\r\n");
2025-11-24 18:58:32 +08:00
SetCW(FUN_DISABLE);
2025-11-24 16:33:33 +08:00
}
/**
* @brief Type V (ISO/IEC 15693)
* @details
* ISO/IEC 15693 Vicinity卡片的交互流程
* 1. 15693
* 2. RF场
* 3. Inventory命令寻卡并获取UID
* 4. Select命令选择卡片
* 5. 4
* 6. RF场
* @return
*/
void TYPE_V_EVENT(void)
{
unsigned char result, i;
xlog("TYPE_V_EVENT begin\n");
ReaderV_Initial();
2025-11-24 18:58:32 +08:00
SetCW(FUN_ENABLE);
2025-11-24 16:33:33 +08:00
result = ReaderV_Inventory(&PICC_V);
if (result != SUCCESS)
{
2025-11-24 18:58:32 +08:00
SetCW(FUN_DISABLE);
2025-11-24 16:33:33 +08:00
xlog("-> ReaderV Inventory ERROR!\r\n");
return;
}
xlog("************* TYPE V CARD ************* \r\n");
xlog("UID=");
for (i = 0; i < 8; i++)
{
xlog("%02X", PICC_V.UID[i]);
}
xlog("\r\n");
result = ReaderV_Select(&PICC_V);
if (result != SUCCESS)
{
2025-11-24 18:58:32 +08:00
SetCW(FUN_DISABLE);
2025-11-24 16:33:33 +08:00
xlog("-> ReaderV Select ERROR!\r\n");
return;
}
// 示例:写单个块
memcpy(PICC_V.BLOCK_DATA, "\x11\x22\x33\x44", 4);
result = ReaderV_WriteSingleBlock(4, &PICC_V);
if (result != SUCCESS)
{
2025-11-24 18:58:32 +08:00
SetCW(FUN_DISABLE);
2025-11-24 16:33:33 +08:00
xlog("-> ReaderV WriteSingleBlock ERROR!\r\n");
return;
}
xlog("WriteSingleBlock SUCCESS\r\n");
// 示例:读单个块
result = ReaderV_ReadSingleBlock(4, &PICC_V);
if (result != SUCCESS)
{
2025-11-24 18:58:32 +08:00
SetCW(FUN_DISABLE);
2025-11-24 16:33:33 +08:00
xlog("-> ReaderV ReadSingleBlock ERROR!\r\n");
return;
}
xlog("BLOCK DATA = %02X%02X%02X%02X \r\n", PICC_V.BLOCK_DATA[0], PICC_V.BLOCK_DATA[1], PICC_V.BLOCK_DATA[2], PICC_V.BLOCK_DATA[3]);
2025-11-24 18:58:32 +08:00
SetCW(FUN_DISABLE);
2025-11-24 16:33:33 +08:00
}
/**
* @brief Type F (FeliCa)
* @details
* FeliCa卡片的交互流程
* 1. FeliCa协议
* 2. RF场
* 3. Inventory命令寻卡并获取UID
* 4. FeliCa卡的数据交换命令
* 5. RF场
* @note
* @return
*/
void TYPE_F_EVENT(void)
{
unsigned char result, i;
xlog("TYPE_F_EVENT begin\n");
ReaderF_Initial();
2025-11-24 18:58:32 +08:00
SetCW(FUN_ENABLE);
2025-11-24 16:33:33 +08:00
result = ReaderF_Inventory(&PICC_F);
if (result != SUCCESS)
{
2025-11-24 18:58:32 +08:00
SetCW(FUN_DISABLE);
2025-11-24 16:33:33 +08:00
return;
}
xlog("************* TYPE F CARD ************* \r\n");
xlog("->TYPE F UID = ");
for(i=0; i<8; i++) xlog("%02X", PICC_F.UID[i]);
xlog("\r\n");
2025-11-24 18:58:32 +08:00
SetCW(FUN_DISABLE);
2025-11-24 16:33:33 +08:00
}
/**
* @brief RFID模块的主任务函数
* @details
*
* @return
*/
void rfid_task_fuc(void)
{
unsigned char result, reg_data;
static u8 first_init = 0;
if(first_init == 0){
first_init = 1;
2025-11-24 18:58:32 +08:00
// rfid_hal_init();
FM176XX_HardInit();
2025-11-24 16:33:33 +08:00
// 2. 复位 FM176XX 芯片
2025-11-25 14:12:44 +08:00
result = FM176XX_SoftReset();
if (result != SUCCESS)
2025-11-24 16:33:33 +08:00
{
2025-11-25 14:12:44 +08:00
xlog("FM176XX HardReset FAIL\r\n");
2025-11-24 16:33:33 +08:00
}
2025-11-25 14:12:44 +08:00
else
{
xlog("FM176XX HardReset SUCCESS\r\n");
}
2025-11-24 16:33:33 +08:00
}
// 3. 读取芯片版本号,确认通信是否正常
GetReg(REG_VERSION, &reg_data);
xlog("REG_VERSION = %02X\r\n", reg_data);
2025-11-25 14:12:44 +08:00
// TYPE_A_EVENT();
// TYPE_B_EVENT();
2025-11-24 16:33:33 +08:00
TYPE_V_EVENT();
2025-11-25 14:12:44 +08:00
// TYPE_F_EVENT();
2025-11-24 16:33:33 +08:00
}