Compare commits
15 Commits
rfid
...
ed475e870f
| Author | SHA1 | Date | |
|---|---|---|---|
| ed475e870f | |||
| b8d48ebe19 | |||
| 976dafada1 | |||
| 27aa063107 | |||
| 9c0bce4568 | |||
| 6248a4fc34 | |||
| 627780ea20 | |||
| ade4b0a1f8 | |||
| 818e8c3778 | |||
| 892ed9267b | |||
| 4af4f13ac6 | |||
| 4c5da2298f | |||
| 5c7d9ab822 | |||
| 60a4e95386 | |||
| 845cc33fe8 |
16
.gitignore
vendored
16
.gitignore
vendored
@ -1,32 +1,16 @@
|
||||
# 编译生成的目标文件
|
||||
*.o
|
||||
*.a
|
||||
*.so
|
||||
*.d
|
||||
|
||||
# 编译生成的最终产物
|
||||
*.elf
|
||||
*.bin
|
||||
*.fw
|
||||
*.ufw
|
||||
*.map
|
||||
*.lst
|
||||
*.bc
|
||||
|
||||
# 编译产物目录
|
||||
# 看起来你所有的编译结果都在 objs/ 目录下,直接忽略整个目录更方便
|
||||
/objs/
|
||||
|
||||
# 工具链和构建产物
|
||||
# 根据你的路径,这些文件也应该被忽略
|
||||
/cpu/br28/tools/app.bin
|
||||
/cpu/br28/tools/data_code.bin
|
||||
/cpu/br28/tools/download/
|
||||
/cpu/br28/tools/isd_config.ini
|
||||
/cpu/br28/tools/sdk.elf*
|
||||
/cpu/br28/tools/sdk.lst
|
||||
/cpu/br28/tools/sdk.map
|
||||
/cpu/br28/sdk_used_list.used
|
||||
|
||||
# VSCode 用户个人设置
|
||||
# 团队协作时,每个人的配置可能不同,通常不建议提交
|
||||
|
||||
34
.vscode/settings.json
vendored
34
.vscode/settings.json
vendored
@ -1,34 +0,0 @@
|
||||
{
|
||||
"files.associations": {
|
||||
"board_config.h": "c",
|
||||
"board_jl701n_demo_cfg.h": "c",
|
||||
"colorful_lights.h": "c",
|
||||
"board_jl701n_anc_cfg.h": "c",
|
||||
"update_loader_download.h": "c",
|
||||
"iic_soft.h": "c",
|
||||
"circle_buffer.h": "c",
|
||||
"default_event_handler.h": "c",
|
||||
"ui_manage.h": "c",
|
||||
"charge.h": "c",
|
||||
"app_main.h": "c",
|
||||
"app_config.h": "c",
|
||||
"app_action.h": "c",
|
||||
"includes.h": "c",
|
||||
"key_event_deal.h": "c",
|
||||
"app_umidigi_chargestore.h": "c",
|
||||
"hci_lmp.h": "c",
|
||||
"bluetooth.h": "c",
|
||||
"SCU722.C": "cpp",
|
||||
"math.h": "c",
|
||||
"avctp_user.h": "c",
|
||||
"string.h": "c",
|
||||
"dev_manager.h": "c",
|
||||
"bt_tws.h": "c",
|
||||
"skiing_tracker.h": "c",
|
||||
"xtell.h": "c",
|
||||
"debug.h": "c",
|
||||
"ano_protocol.h": "c",
|
||||
"board_jl701n_demo_global_build_cfg.h": "c",
|
||||
"spi.h": "c"
|
||||
}
|
||||
}
|
||||
@ -266,7 +266,7 @@ u8 gravity_sensor_command(u8 w_chip_id, u8 register_address, u8 function_command
|
||||
// xlog("iic_tx_byte id\n");
|
||||
if (0 == iic_tx_byte(gSensor_info->iic_hdl, w_chip_id)) {
|
||||
ret = 0;
|
||||
xlog("\n gsen iic wr err 0\n");
|
||||
xlog("WRITE: I2C NACK on writing ADDR: 0x%X\n", w_chip_id - 1);
|
||||
strcpy(&w_log_buffer_1, "gsen iic wr err 0\n");
|
||||
goto __gcend;
|
||||
}
|
||||
@ -277,7 +277,7 @@ u8 gravity_sensor_command(u8 w_chip_id, u8 register_address, u8 function_command
|
||||
// xlog("iic_tx_byte: address\n");
|
||||
if (0 == iic_tx_byte(gSensor_info->iic_hdl, register_address)) {
|
||||
ret = 0;
|
||||
xlog("\n gsen iic wr err 1\n");
|
||||
xlog("WRITE: I2C NACK on writing ADDR: 0x%X\n", register_address);
|
||||
strcpy(&w_log_buffer_2, "gsen iic wr err 1\n");
|
||||
goto __gcend;
|
||||
}
|
||||
@ -316,7 +316,7 @@ u8 _gravity_sensor_get_ndata(u8 r_chip_id, u8 register_address, u8 *buf, u8 data
|
||||
|
||||
iic_start(gSensor_info->iic_hdl);
|
||||
if (0 == iic_tx_byte(gSensor_info->iic_hdl, r_chip_id - 1)) {
|
||||
xlog("I2C NACK on writing ADDR: 0x%X\n", r_chip_id - 1);
|
||||
xlog("GET: I2C NACK on writing ADDR: 0x%X\n", r_chip_id - 1);
|
||||
read_len = 0;
|
||||
strcpy(&sen_log_buffer_1, "gsen iic rd err 0\n");
|
||||
goto __gdend;
|
||||
@ -325,7 +325,7 @@ u8 _gravity_sensor_get_ndata(u8 r_chip_id, u8 register_address, u8 *buf, u8 data
|
||||
|
||||
delay(gSensor_info->iic_delay);
|
||||
if (0 == iic_tx_byte(gSensor_info->iic_hdl, register_address)) {
|
||||
xlog("I2C NACK on register ADDR: 0x%X\n", register_address);
|
||||
xlog("GET: I2C NACK on register ADDR: 0x%X\n", register_address);
|
||||
// xlog("\n gsen iic rd err 1\n");
|
||||
read_len = 0;
|
||||
strcpy(&sen_log_buffer_2, "gsen iic rd err 1\n");
|
||||
|
||||
625
apps/earphone/94_rfid_stc/94_spi_test.c
Normal file
625
apps/earphone/94_rfid_stc/94_spi_test.c
Normal file
@ -0,0 +1,625 @@
|
||||
#include <stc8h.h>
|
||||
#include "xt_main.h"
|
||||
#include "intrins.h"
|
||||
#include <stdarg.h>
|
||||
|
||||
#include "READER.h"
|
||||
#include "READER_REG.h"
|
||||
// #include "board.h"
|
||||
#include "MIFARE.h"
|
||||
// #include "CPU_CARD.h"
|
||||
#include "NTAG.h"
|
||||
|
||||
|
||||
void TYPE_A_EVENT(void);
|
||||
void TYPE_B_EVENT(void);
|
||||
void TYPE_V_EVENT(void);
|
||||
void TYPE_F_EVENT(void);
|
||||
|
||||
// SPI????
|
||||
sbit SPI_SCK = P1^5; // SCK
|
||||
sbit SPI_MOSI = P1^3; // MOSI
|
||||
sbit SPI_MISO = P1^4; // MISO
|
||||
sbit SPI_SS = P1^2; // ??
|
||||
|
||||
bit busy1;
|
||||
|
||||
|
||||
void GPIO_init()
|
||||
{
|
||||
P_SW2 |= 0x80; // Enable access to XFR
|
||||
|
||||
// Set all ports to quasi-bidirectional mode
|
||||
P0M1 = 0; P0M0 = 0;
|
||||
P1M1 = 0; P1M0 = 0;
|
||||
P2M1 = 0; P2M0 = 0;
|
||||
P3M1 = 0; P3M0 = 0;
|
||||
P4M1 = 0; P4M0 = 0;
|
||||
P5M1 = 0; P5M0 = 0;
|
||||
P6M1 = 0; P6M0 = 0;
|
||||
P7M1 = 0; P7M0 = 0;
|
||||
}
|
||||
|
||||
// UART1 Functions
|
||||
void Uart1Send(unsigned char dat)
|
||||
{
|
||||
while (busy1);
|
||||
busy1 = 1;
|
||||
SBUF = dat;
|
||||
}
|
||||
|
||||
// ? Uart1SendString
|
||||
void Uart1SendString(unsigned char *str)
|
||||
{
|
||||
while (*str)
|
||||
{
|
||||
Uart1Send(*str);
|
||||
while (busy1); // ????????
|
||||
str++;
|
||||
}
|
||||
}
|
||||
void IntToHex(unsigned char *buf, unsigned char num)
|
||||
{
|
||||
unsigned char digits[] = "0123456789ABCDEF"; // 大写 HEX 为 %02X
|
||||
buf[0] = digits[(num >> 4) & 0x0F]; // 高 4 位
|
||||
buf[1] = digits[num & 0x0F]; // 低 4 位
|
||||
buf[2] = '\0';
|
||||
}
|
||||
// void Uart1Init(void) //115200bps@36MHz
|
||||
// {
|
||||
// SCON = 0x50; //8???,?????
|
||||
// AUXR |= 0x40; //?????1T??
|
||||
// AUXR &= 0xFE; //??1?????1???????
|
||||
// TMOD &= 0x0F; //???????
|
||||
// TL1 = 0xB2; //???????
|
||||
// TH1 = 0xFF; //???????
|
||||
// ET1 = 0; //???????
|
||||
// TR1 = 1; //???1????
|
||||
// }
|
||||
|
||||
void Uart1Init(void) //9600bps@36MHz
|
||||
{
|
||||
SCON = 0x50; //8位数据,可变波特率
|
||||
AUXR |= 0x40; //定时器时钟1T模式
|
||||
AUXR &= 0xFE; //串口1选择定时器1为波特率发生器
|
||||
TMOD &= 0x0F; //设置定时器模式
|
||||
TL1 = 0x56; //设置定时初始值
|
||||
TH1 = 0xFC; //设置定时初始值
|
||||
ET1 = 0; //禁止定时器中断
|
||||
TR1 = 1; //定时器1开始计时
|
||||
}
|
||||
|
||||
void Uart1Isr() interrupt 4
|
||||
{
|
||||
|
||||
unsigned char received;
|
||||
if (TI)
|
||||
{
|
||||
TI = 0;
|
||||
busy1 = 0;
|
||||
}
|
||||
if (RI)
|
||||
{
|
||||
received = SBUF; // Read received data
|
||||
RI = 0;
|
||||
//Uart2Send(received); // Send received data via UART2
|
||||
}
|
||||
}
|
||||
|
||||
void Delay100ms() //@36MHz
|
||||
{
|
||||
unsigned char data i, j, k;
|
||||
|
||||
//_nop_();
|
||||
i = 19;
|
||||
j = 68;
|
||||
k = 67;
|
||||
do
|
||||
{
|
||||
do
|
||||
{
|
||||
while (--k);
|
||||
} while (--j);
|
||||
} while (--i);
|
||||
}
|
||||
void Delay1ms() //@36MHz
|
||||
{
|
||||
unsigned char data i, j;
|
||||
|
||||
_nop_();
|
||||
i = 47;
|
||||
j = 190;
|
||||
do
|
||||
{
|
||||
while (--j);
|
||||
} while (--i);
|
||||
}
|
||||
|
||||
// SPI?????
|
||||
void SPI_Init_xt(void)
|
||||
{
|
||||
// ??SPI?????
|
||||
SPCTL =0xD0; // ??SPI,???,??Fosc/4,CPOL=0, CPHA=0
|
||||
SPSTAT = 0x00; // ??????
|
||||
P_SW1 = P_SW1 & 0xF3;
|
||||
// ?????????(SCK, MOSI, SS)
|
||||
P1M0 |= (1<<5) | (1<<3) | (1<<2); // ??P1.5, P1.3, P1.2???
|
||||
P1M1 &= ~((1<<5) | (1<<3) | (1<<2));
|
||||
|
||||
// MISO???????
|
||||
P1M0 &= ~(1<<4); // P1.4??
|
||||
P1M1 |= (1<<4);
|
||||
|
||||
SPI_SS = 1; // ???????
|
||||
}
|
||||
|
||||
// SPI??/???????
|
||||
unsigned char SPI_Transfer(unsigned char dat)
|
||||
{
|
||||
SPDAT = dat; // ??????????
|
||||
while(!(SPSTAT & 0x80)); // ??????
|
||||
SPSTAT = 0xC0; // ??????
|
||||
return SPDAT; // ????????
|
||||
}
|
||||
|
||||
// 读寄存器
|
||||
unsigned char GetReg(unsigned char address, unsigned char *reg_data)
|
||||
{
|
||||
unsigned char spi_data;
|
||||
|
||||
SPI_SS = 0; // ????,????
|
||||
*reg_data =0;
|
||||
// ????(??1??????)
|
||||
spi_data = (address << 1) | 0x01;
|
||||
SPI_Transfer(spi_data); // ????
|
||||
|
||||
// ???????
|
||||
*reg_data = SPI_Transfer(0x00); // ??????????
|
||||
|
||||
SPI_SS = 1; // ????,????
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
// 写寄存器值
|
||||
unsigned char SetReg(unsigned char address, unsigned char reg_data)
|
||||
{
|
||||
unsigned char spi_data;
|
||||
|
||||
SPI_SS = 0; // 拉低片选,开始通信
|
||||
|
||||
// 发送地址(左移1位并清写标志)
|
||||
spi_data = (address << 1) & 0xFE;
|
||||
SPI_Transfer(spi_data); // 发送地址
|
||||
|
||||
// 发送寄存器数据
|
||||
SPI_Transfer(reg_data); // 发送数据
|
||||
|
||||
SPI_SS = 1; // 拉高片选,结束通信
|
||||
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
unsigned char FM176XX_HardReset(void)
|
||||
{
|
||||
unsigned char reg_data;
|
||||
SPI_SS = 1;//NSS = 1
|
||||
P34 =1;//RST = 1
|
||||
Delay1ms();
|
||||
P34 =0;//RST = 0
|
||||
Delay1ms();
|
||||
GetReg(REG_COMMAND,®_data);
|
||||
if (reg_data != 0x40)
|
||||
return FAIL;
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
// ?????
|
||||
unsigned char Reader_Set_HPD( unsigned char mode ) //mode = DISABLE <20>˳<EFBFBD>HPDģʽ <20><>mode = ENABLE <20><><EFBFBD><EFBFBD>HPDģʽ
|
||||
{
|
||||
if ( mode == ENABLE )
|
||||
{
|
||||
Delay1ms(); //<2F><>ʱ1ms
|
||||
|
||||
/******设置一个GPIO输出高*******xtell******/
|
||||
//PD_1; // PD = 1
|
||||
P34 = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
/******设置一个GPIO输出低*************/
|
||||
//PD_0; //PD = 0
|
||||
P34 =0;
|
||||
Delay1ms(); //<2F><>ʱ1ms<6D><73><EFBFBD>ȴ<EFBFBD>Reader<65><72><EFBFBD><EFBFBD>
|
||||
}
|
||||
return (mode);
|
||||
}
|
||||
|
||||
void printHex(unsigned char num)
|
||||
{
|
||||
unsigned char buf[3];
|
||||
IntToHex(buf, num);
|
||||
// Uart1SendString("Value: ");
|
||||
Uart1SendString(buf);
|
||||
// Uart1SendString("\r\n");
|
||||
}
|
||||
|
||||
|
||||
// // // 简化版 xlog,仅支持一个 %02X 或 %02x 参数
|
||||
// void xlog1(const unsigned char *fmt, unsigned char val)
|
||||
// {
|
||||
|
||||
// unsigned char hex[2];
|
||||
// int i;
|
||||
// while (*fmt)
|
||||
// {
|
||||
// if (*fmt == '%')
|
||||
// {
|
||||
// fmt++;
|
||||
// // 检查 %02X 或 %02x
|
||||
// if (*fmt == '0' && *(fmt+1) == '2' && (*(fmt+2) == 'X' || *(fmt+2) == 'x'))
|
||||
// {
|
||||
// // 处理 %02X 或 %02x
|
||||
|
||||
// // 转换为两位十六进制
|
||||
// hex[0] = (val >> 4) & 0x0F; // 高4位
|
||||
// hex[1] = val & 0x0F; // 低4位
|
||||
|
||||
// for ( i = 0; i < 2; i++)
|
||||
// {
|
||||
// if (hex[i] < 10)
|
||||
// Uart1Send(hex[i] + '0');
|
||||
// else
|
||||
// Uart1Send(hex[i] - 10 + 'A'); // 输出大写 A-F
|
||||
// while (busy1); // 等待发送完成
|
||||
// }
|
||||
// fmt += 3; // 跳过 "02X" 或 "02x"
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// // 未识别的格式,发送 % 和当前字符
|
||||
// Uart1Send('%');
|
||||
// while (busy1);
|
||||
// if (*fmt)
|
||||
// {
|
||||
// Uart1Send(*fmt);
|
||||
// while (busy1);
|
||||
// fmt++;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// // 普通字符直接发送
|
||||
// Uart1Send(*fmt);
|
||||
// while (busy1);
|
||||
// fmt++;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// P34 接芯片pdown
|
||||
void main(void)
|
||||
{
|
||||
unsigned char reg_value,i,tmp1,tmp2;
|
||||
unsigned char result,reg_data;
|
||||
ES =1;
|
||||
EA = 1;
|
||||
|
||||
Uart1Init();
|
||||
GPIO_init();
|
||||
SPI_Init_xt(); // ???SPI
|
||||
Uart1SendString("UART1: Hello, STC8H1K28!\r\n");
|
||||
//xlog("Data: %02X %02X %02x\n", 0xAB, 0x12, 0xFF);
|
||||
// xlog("1=%02X\r\n",0xAB);
|
||||
// PrintHex(0xAB); // 应输出 Test: ab
|
||||
Delay100ms();
|
||||
#if 0
|
||||
while(1)
|
||||
{
|
||||
// P34 =1;
|
||||
Uart1Send(0XAb);
|
||||
// ??:????0x01?????
|
||||
GetReg(0x7F, ®_value);
|
||||
Uart1Send(reg_value);
|
||||
LOG1("Value: %x", reg_value);
|
||||
// Delay100ms();
|
||||
// GetReg(0x02, &tmp1);
|
||||
// Uart1Send(tmp1);
|
||||
// Delay100ms();
|
||||
// // ??????reg_value???
|
||||
// SetReg(0x02,0x00);
|
||||
// GetReg(0x02, &tmp2);
|
||||
// Uart1Send(tmp2);
|
||||
// ????
|
||||
FM176XX_HardReset();
|
||||
for(i = 0; i < 10; i++){
|
||||
Delay100ms();
|
||||
}
|
||||
}
|
||||
#else
|
||||
while(1)
|
||||
{
|
||||
result = FM176XX_HardReset();
|
||||
if(result != SUCCESS)
|
||||
{
|
||||
Uart1SendString("FM17660 FAIL\r\n");
|
||||
Delay100ms();
|
||||
}
|
||||
else
|
||||
break;
|
||||
}
|
||||
GetReg(REG_VERSION,®_data);
|
||||
// printf( "REG_VERSION = %02X\r\n",reg_data );
|
||||
Uart1SendString("REG_VERSION =");
|
||||
printHex(reg_data);
|
||||
Uart1SendString("\n");
|
||||
while(1)
|
||||
{
|
||||
// Uart1SendString("main loop");
|
||||
for(i = 0; i < 2; i++){
|
||||
Delay100ms();
|
||||
}
|
||||
// TYPE_A_EVENT();
|
||||
// TYPE_B_EVENT();
|
||||
TYPE_V_EVENT();
|
||||
// TYPE_F_EVENT(); //这里有CPU_CARD.c的函数 暂时注释了
|
||||
|
||||
|
||||
/***现在版本只需要执行 V类型识别 15693 即可 */
|
||||
}
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
|
||||
void TYPE_A_EVENT(void)
|
||||
{
|
||||
unsigned char result;
|
||||
// Uart1SendString(" TYPE_A_EVENT begin\n");
|
||||
Reader_Set_HPD(DISABLE);
|
||||
|
||||
result = ReaderA_Initial();
|
||||
// Uart1SendString("ReaderA_Initial return =");
|
||||
// printHex(result);
|
||||
if(result != SUCCESS)
|
||||
{
|
||||
Uart1SendString("INIT_ERROR\r\n");
|
||||
SetCW(DISABLE);
|
||||
return;
|
||||
}
|
||||
result = SetCW(ENABLE);
|
||||
// Uart1SendString("SetCW return =");
|
||||
// printHex(result);
|
||||
//xtell 注释
|
||||
// if(result != SUCCESS)
|
||||
// {
|
||||
// Uart1SendString("CW_ERROR\r\n");
|
||||
// SetCW(DISABLE);
|
||||
// return;
|
||||
// }
|
||||
result = ReaderA_CardActivate(&PICC_A);
|
||||
// Uart1SendString("ReaderA_CardActivate return =");
|
||||
// printHex(result);
|
||||
if(result != SUCCESS)
|
||||
{
|
||||
//printf("ReaderA_CardActivate_ERROR\r\n");
|
||||
SetCW(DISABLE);
|
||||
return;
|
||||
}
|
||||
Uart1SendString("************* TYPE A CARD************* \r\n");
|
||||
// printf("-> ATQA = %02X%02X\r\n",PICC_A.ATQA [0], PICC_A.ATQA [1]);
|
||||
|
||||
|
||||
if(PICC_A.UID_Length == 4)
|
||||
// printf( "-> UID = %02X%02X%02X%02X\r\n",PICC_A.UID[0], PICC_A.UID[1],PICC_A.UID[2],PICC_A.UID[3]);
|
||||
if(PICC_A.UID_Length == 8)
|
||||
// printf( "-> UID = %02X%02X%02X%02X%02X%02X%02X%02X\r\n",PICC_A.UID[0], PICC_A.UID[1],PICC_A.UID[2],PICC_A.UID[3],PICC_A.UID[4], PICC_A.UID[5],PICC_A.UID[6],PICC_A.UID[7]);
|
||||
// printf( "-> SAK = %02X\r\n",PICC_A.SAK[0]);
|
||||
|
||||
if(PICC_A.SAK[0] == 0x08)
|
||||
{
|
||||
Uart1SendString("************* Mifare CARD************* \r\n");
|
||||
result = MIFARE_CARD_EVENT();
|
||||
}
|
||||
if((PICC_A.SAK[0] == 0x28)||(PICC_A.SAK[0] == 0x20))
|
||||
{
|
||||
Uart1SendString("************* CPU CARD************* \r\n");
|
||||
// result = CPU_CARD_EVENT();
|
||||
}
|
||||
if(PICC_A.SAK[0] == 0x04)
|
||||
{
|
||||
Uart1SendString("************* NTAG CARD************* \r\n");
|
||||
result = NTAG_EVENT();
|
||||
}
|
||||
|
||||
SetCW(DISABLE);
|
||||
// LED();
|
||||
|
||||
}
|
||||
|
||||
|
||||
void TYPE_B_EVENT(void)
|
||||
{
|
||||
unsigned char result;
|
||||
// Uart1SendString("TYPE_B_EVENT begin\n");
|
||||
ReaderB_Initial();
|
||||
SetCW(ENABLE);
|
||||
result = ReaderB_Request(&PICC_B);
|
||||
if(result != SUCCESS)
|
||||
{
|
||||
SetCW(DISABLE);
|
||||
return;
|
||||
}
|
||||
//LED();
|
||||
Uart1SendString("************* TYPE B CARD************* \r\n");
|
||||
// printf("-> ATQB = %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X\r\n",PICC_B.ATQB[0],PICC_B.ATQB[1],PICC_B.ATQB[2],PICC_B.ATQB[3],PICC_B.ATQB[4],PICC_B.ATQB[5],PICC_B.ATQB[6],PICC_B.ATQB[7],PICC_B.ATQB[8],PICC_B.ATQB[9],PICC_B.ATQB[10],PICC_B.ATQB[11]);
|
||||
|
||||
result = ReaderB_Attrib(&PICC_B);
|
||||
if(result != SUCCESS)
|
||||
{
|
||||
SetCW(DISABLE);
|
||||
return;
|
||||
}
|
||||
// printf("-> ATTRIB = %02X\r\n",PICC_B.CID);
|
||||
|
||||
result = ReaderB_Get_SN(&PICC_B);
|
||||
if(result != SUCCESS)
|
||||
{
|
||||
SetCW(DISABLE);
|
||||
return;
|
||||
}
|
||||
// printf("-> SN = %02X%02X%02X%02X%02X%02X%02X%02X\r\n",PICC_B.SN[0],PICC_B.SN[1],PICC_B.SN[2],PICC_B.SN[3],PICC_B.SN[4],PICC_B.SN[5],PICC_B.SN[6],PICC_B.SN[7]);
|
||||
SetCW(DISABLE);
|
||||
return;
|
||||
}
|
||||
|
||||
void TYPE_V_EVENT(void)
|
||||
{
|
||||
unsigned char result,i;
|
||||
|
||||
// Uart1SendString("TYPE_V_EVENT begin\n");
|
||||
ReaderV_Initial();
|
||||
SetCW(ENABLE);
|
||||
result = ReaderV_Inventory(&PICC_V);
|
||||
if (result != SUCCESS)
|
||||
{
|
||||
SetCW(DISABLE);
|
||||
//printf("-> ReaderV Inventory ERROR!\r\n");
|
||||
return;
|
||||
}
|
||||
// LED();
|
||||
// Uart1SendString("************* TYPE V CARD************* \r\n");
|
||||
// printf("UID = %02X%02X%02X%02X%02X%02X%02X%02X\r\n",PICC_V.UID[0],PICC_V.UID[1],PICC_V.UID[2],PICC_V.UID[3],PICC_V.UID[4],PICC_V.UID[5],PICC_V.UID[6],PICC_V.UID[7]);
|
||||
Uart1SendString("UID=");
|
||||
for(i=0;i<8;i++){
|
||||
printHex(PICC_V.UID[i]);
|
||||
}
|
||||
Uart1SendString("\r\n");
|
||||
result = ReaderV_Select(&PICC_V);
|
||||
if (result != SUCCESS)
|
||||
{
|
||||
SetCW(DISABLE);
|
||||
// Uart1SendString("-> ReaderV Select ERROR!\r\n");
|
||||
return;
|
||||
}
|
||||
memcpy(PICC_V.BLOCK_DATA,"\x00\x00\x00\x00",4);
|
||||
result = ReaderV_WriteSingleBlock(4,&PICC_V);
|
||||
if (result != SUCCESS)
|
||||
{
|
||||
SetCW(DISABLE);
|
||||
// Uart1SendString("-> ReaderV WriteSingleBlock ERROR!\r\n");
|
||||
return;
|
||||
}
|
||||
|
||||
// Uart1SendString("WriteSingleBlock SUCCESS\r\n");
|
||||
result = ReaderV_ReadSingleBlock(4,&PICC_V);
|
||||
if (result != SUCCESS)
|
||||
{
|
||||
SetCW(DISABLE);
|
||||
// Uart1SendString("-> ReaderV ReadSingleBlock ERROR!\r\n");
|
||||
return;
|
||||
}
|
||||
// printf("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]);
|
||||
// Uart1SendString("BLOCK DATA=");
|
||||
for(i=0;i<4;i++){
|
||||
// printHex(PICC_V.BLOCK_DATA[i]);
|
||||
}
|
||||
// Uart1SendString("\r\n");
|
||||
SetCW(DISABLE);
|
||||
}
|
||||
|
||||
|
||||
void TYPE_F_EVENT(void)
|
||||
{
|
||||
unsigned char result,i;
|
||||
unsigned char SendBuffer[255];
|
||||
unsigned char ReceiveBuffer[255];
|
||||
|
||||
transmission_struct TPDU;
|
||||
TPDU.pSendBuffer = SendBuffer;//<2F><><EFBFBD>÷<EFBFBD><C3B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
TPDU.pReceiveBuffer = ReceiveBuffer;//<2F><><EFBFBD>ý<EFBFBD><C3BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
|
||||
// Uart1SendString("TYPE_F_EVENT begin\n");
|
||||
ReaderF_Initial();
|
||||
SetCW(ENABLE);
|
||||
result = ReaderF_Inventory(&PICC_F);
|
||||
if(result != SUCCESS)
|
||||
{
|
||||
SetCW(DISABLE);
|
||||
return;
|
||||
}
|
||||
// LED();
|
||||
Uart1SendString("************* TYPE F CARD************* \r\n");
|
||||
// printf("->TYPE F UID = %02X%02X%02X%02X%02X%02X%02X%02X\r\n",PICC_F.UID[0],PICC_F.UID[1],PICC_F.UID[2],PICC_F.UID[3],PICC_F.UID[4],PICC_F.UID[5],PICC_F.UID[6],PICC_F.UID[7]);
|
||||
|
||||
//1401 012E48C23C8C7C3F 00F100000001430088B4
|
||||
//01 2E 48 C2 3C 8C 7C 3F
|
||||
memcpy(TPDU.pSendBuffer,"\x1D\x02",2);
|
||||
memcpy(TPDU.pSendBuffer + 2,PICC_F.UID,8);
|
||||
memcpy(TPDU.pSendBuffer + 10,"\x09\xFF\xFF\x00\x00\x00\x08\x08\x10\x10\x01\x08\x02\x08\x03\x0C\x04\x08\x09",19);
|
||||
|
||||
TPDU.SendLength = 29;
|
||||
TPDU.Timeout = 100;
|
||||
// printf("->SEND 2 = ");
|
||||
// for(i=0;i<TPDU.SendLength;i++)
|
||||
// printf("%02X",TPDU.pSendBuffer[i]);
|
||||
// printf("\r\n");
|
||||
// result = CPU_TPDU(&TPDU); xtell 注释 CPU_CARD.c中的函数
|
||||
if(result != SUCCESS)
|
||||
{
|
||||
SetCW(DISABLE);
|
||||
return;
|
||||
}
|
||||
// printf("->RESPONE = ");
|
||||
// for(i=0;i<TPDU.ReceiveLength;i++)
|
||||
// printf("%02X",TPDU.pReceiveBuffer[i] );
|
||||
|
||||
// printf("\r\n");
|
||||
|
||||
memcpy(TPDU.pSendBuffer,"\x24\x10",2);
|
||||
memcpy(TPDU.pSendBuffer + 2,PICC_F.UID,8);
|
||||
memcpy(TPDU.pSendBuffer + 10,"\x02\x00\x00\x00\x08\x06\x08\x10\x10\x01\x08\x02\x08\x03\x0C\x04\x08\x09\x1B\x04\x42\xB1\xD6\x98\x06\x7A",26);
|
||||
|
||||
TPDU.SendLength = 36;
|
||||
TPDU.Timeout = 100;
|
||||
// printf("->SEND 3 = ");
|
||||
// for(i=0;i<TPDU.SendLength;i++)
|
||||
// printf("%02X",TPDU.pSendBuffer[i] );
|
||||
// printf("\r\n");
|
||||
// result = CPU_TPDU(&TPDU); xtell 注释 CPU_CARD.c中的函数
|
||||
if(result != SUCCESS)
|
||||
{
|
||||
SetCW(DISABLE);
|
||||
return;
|
||||
}
|
||||
// printf("->RESPONE = ");
|
||||
// for(i=0;i<TPDU.ReceiveLength;i++)
|
||||
// printf("%02X",TPDU.pReceiveBuffer[i]);
|
||||
// printf("\r\n");
|
||||
|
||||
|
||||
//12 12 01 01 07 01 03 17 A6 07 \x45\xC9\x03\xC4\x7B\xF0\xE2\x00
|
||||
memcpy(TPDU.pSendBuffer,"\x12\x12",2);
|
||||
memcpy(TPDU.pSendBuffer + 2,PICC_F.UID,8);
|
||||
memcpy(TPDU.pSendBuffer + 10,"\x45\xC9\x03\xC4\x7B\xF0\xE2\x00",8);//<2F><>8<EFBFBD>ֽ<EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD>Ǽ<EFBFBD><C7BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
|
||||
TPDU.SendLength = 18;
|
||||
TPDU.Timeout = 100;
|
||||
// printf("->SEND 4 = ");
|
||||
// for(i=0;i<TPDU.SendLength;i++)
|
||||
// printf("%02X",TPDU.pSendBuffer[i] );
|
||||
// printf("\r\n");
|
||||
// result = CPU_TPDU(&TPDU); xtell 注释 CPU_CARD.c中的函数
|
||||
if(result != SUCCESS)
|
||||
{
|
||||
SetCW(DISABLE);
|
||||
return;
|
||||
}
|
||||
// printf("->RESPONE = ");
|
||||
// for(i=0;i<TPDU.ReceiveLength;i++)
|
||||
// printf("%02X",TPDU.pReceiveBuffer[i]);
|
||||
// printf("\r\n");
|
||||
|
||||
return;
|
||||
}
|
||||
582
apps/earphone/94_rfid_stc/READER/CPU_CARD.c
Normal file
582
apps/earphone/94_rfid_stc/READER/CPU_CARD.c
Normal file
@ -0,0 +1,582 @@
|
||||
//2018<31><38>2<EFBFBD><32>2<EFBFBD>ձ༭<D5B1><E0BCAD>֧<EFBFBD><D6A7>CPU<50><55>Ƭָ<C6AC><EFBFBD><EEA3AC><EFBFBD>ӷ<EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD>
|
||||
#include "READER.h"
|
||||
#include "CPU_CARD.h"
|
||||
#include <string.h>
|
||||
#include "READER_REG.h"
|
||||
#include "xt_main.h"
|
||||
#define CPU_DEBUG 0
|
||||
|
||||
struct CPU_CARD_STR CPU_CARD;
|
||||
|
||||
#if 0
|
||||
unsigned char CPU_CARD_EVENT( void ){
|
||||
return FAIL;
|
||||
}
|
||||
#else
|
||||
unsigned char CPU_CARD_EVENT( void )
|
||||
{
|
||||
unsigned char result;
|
||||
unsigned char SendBuffer[255];
|
||||
unsigned char ReceiveBuffer[255];
|
||||
unsigned char i;
|
||||
transmission_struct APDU;
|
||||
APDU.pSendBuffer = SendBuffer; /*<2A><><EFBFBD>÷<EFBFBD><C3B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
||||
APDU.pReceiveBuffer = ReceiveBuffer; /*<2A><><EFBFBD>ý<EFBFBD><C3BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
||||
result = CPU_Rats( &CPU_CARD.ATS.Length, CPU_CARD.ATS.Ats_Data );
|
||||
if ( result != SUCCESS )
|
||||
{
|
||||
SetCW( DISABLE );
|
||||
printf( "-> RATS ERROR!\r\n" );
|
||||
return result;
|
||||
}
|
||||
|
||||
printf( "-> ATS = " );
|
||||
for(i = 0;i < CPU_CARD.ATS.Length;i++)
|
||||
printf("%02X", CPU_CARD.ATS.Ats_Data[i]);
|
||||
printf( "\r\n" );
|
||||
|
||||
result = Ats_Process( CPU_CARD.ATS.Length, CPU_CARD.ATS.Ats_Data );
|
||||
if ( result != SUCCESS )
|
||||
{
|
||||
SetCW( DISABLE );
|
||||
printf( "-> RATS ERROR!\r\n" );
|
||||
return result;
|
||||
}
|
||||
|
||||
memcpy( APDU.pSendBuffer, "\x00\xA4\x00\x00\x02\x3F\x00", 7 );
|
||||
APDU.SendLength = 7;
|
||||
result = CPU_APDU( &APDU );
|
||||
if ( result != SUCCESS )
|
||||
{
|
||||
SetCW( DISABLE );
|
||||
printf( "-> APDU ERROR!\r\n" );
|
||||
return result;
|
||||
}
|
||||
printf( "->APDU = " );
|
||||
for(i=0;i<APDU.ReceiveLength;i++)
|
||||
printf("%02X", APDU.pReceiveBuffer[i] );
|
||||
printf( "\r\n" );
|
||||
|
||||
|
||||
memcpy(APDU.pSendBuffer,"\x00\xA4\x04\x00\x07\xD4\x10\x00\x00\x03\x00\x01",12);
|
||||
APDU.SendLength = 12;
|
||||
result = CPU_APDU(&APDU);
|
||||
if(result != SUCCESS)
|
||||
{
|
||||
SetCW(DISABLE);
|
||||
printf("-> APDU ERROR!\r\n");
|
||||
return result;
|
||||
}
|
||||
printf("-> APDU = ");
|
||||
for(i=0;i<APDU.ReceiveLength;i++)
|
||||
printf("%02X",APDU.pReceiveBuffer[i]);
|
||||
printf("\r\n");
|
||||
|
||||
|
||||
memcpy(APDU.pSendBuffer,"\x00\xA4\x04\x00\x0E\x32\x50\x41\x59\x2E\x53\x59\x53\x2E\x44\x44\x46\x30\x31",19);
|
||||
APDU.SendLength = 19;
|
||||
result = CPU_APDU(&APDU);
|
||||
if(result != SUCCESS)
|
||||
{
|
||||
SetCW(DISABLE);
|
||||
printf("-> APDU ERROR!\r\n");
|
||||
return result;
|
||||
}
|
||||
printf("-> Response = ");
|
||||
for(i=0;i<APDU.ReceiveLength;i++)
|
||||
printf("%02X",APDU.pReceiveBuffer[i]);
|
||||
printf("\r\n");
|
||||
|
||||
memcpy(APDU.pSendBuffer,"\x00\xA4\x04\x00\x08\xA0\x00\x00\x03\x33\x01\x01\x01\x00",14);
|
||||
APDU.SendLength = 14;
|
||||
result = CPU_APDU(&APDU);
|
||||
if(result != SUCCESS)
|
||||
{
|
||||
SetCW(DISABLE);
|
||||
printf("-> APDU ERROR!\r\n");
|
||||
return result;
|
||||
}
|
||||
printf("-> Response = ");
|
||||
for(i=0;i<APDU.ReceiveLength;i++)
|
||||
printf("%02X",APDU.pReceiveBuffer[i]);
|
||||
printf("\r\n");
|
||||
|
||||
memcpy(APDU.pSendBuffer,"\x80\xA8\x00\x00\x24\x83\x22\x28\x00\x00\x80\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x01\x56\x00\x00\x00\x00\x00\x01\x56\x13\x08\x28\x82\x12\x34\x56\x78\x00\x00",42);
|
||||
APDU.SendLength = 42;
|
||||
result = CPU_APDU(&APDU);
|
||||
if(result != SUCCESS)
|
||||
{
|
||||
SetCW(DISABLE);
|
||||
printf("-> APDU ERROR!\r\n");
|
||||
return result;
|
||||
}
|
||||
printf("-> Response = ");
|
||||
for(i=0;i<APDU.ReceiveLength;i++)
|
||||
printf("%02X",APDU.pReceiveBuffer[i]);
|
||||
printf("\r\n");
|
||||
|
||||
|
||||
memcpy( APDU.pSendBuffer, "\x00\x84\x00\x00\x08", 5 );
|
||||
APDU.SendLength = 5;
|
||||
result = CPU_APDU( &APDU );
|
||||
if ( result != SUCCESS )
|
||||
{
|
||||
SetCW( DISABLE );
|
||||
printf( "-> APDU ERROR!\r\n" );
|
||||
return result;
|
||||
}
|
||||
printf( "-> Response = " );
|
||||
for(i=0;i<APDU.ReceiveLength;i++)
|
||||
printf("%02X",APDU.pReceiveBuffer[i]);
|
||||
printf( "\r\n" );
|
||||
return result;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if 0
|
||||
void Write_FIFO(unsigned char length,unsigned char* buff)
|
||||
{
|
||||
|
||||
}
|
||||
#else
|
||||
void Write_FIFO(unsigned char length,unsigned char* buff)
|
||||
{
|
||||
unsigned char i;
|
||||
for(i=0;i<length;i++)
|
||||
{
|
||||
SetReg(REG_FIFODATA,buff[i]);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if 0
|
||||
void Read_FIFO(unsigned char length,unsigned char* buff)
|
||||
{
|
||||
}
|
||||
#else
|
||||
void Read_FIFO(unsigned char length,unsigned char* buff)
|
||||
{
|
||||
unsigned char i;
|
||||
for(i=0;i<length;i++)
|
||||
{
|
||||
GetReg(REG_FIFODATA,&buff[i]);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if 0
|
||||
unsigned char FM176XX_TPDU( transmission_struct *tpdu )
|
||||
{
|
||||
return FAIL;
|
||||
}
|
||||
#else
|
||||
unsigned char FM176XX_TPDU( transmission_struct *tpdu )
|
||||
{
|
||||
unsigned char result,irq0,irq1,error;
|
||||
unsigned int i;
|
||||
SetCommand(CMD_IDLE);
|
||||
SetReg(REG_TXDATANUM,0x08);
|
||||
SetReg(REG_IRQ0,0x7F);//Clear IRQ0
|
||||
SetReg(REG_IRQ1,0x7F); //Clear IRQ1
|
||||
ModifyReg(REG_FIFOCONTROL,BIT_FIFOFLUSH,ENABLE); //Clear FIFO
|
||||
Write_FIFO(tpdu->SendLength,tpdu->pSendBuffer);
|
||||
ModifyReg(REG_TXCRCCON, BIT_CRCEN,ENABLE);
|
||||
ModifyReg(REG_RXCRCCON, BIT_CRCEN,ENABLE);
|
||||
// SetTimer(tpdu->Timeout);
|
||||
SetCommand(CMD_TRANSCEIVE);
|
||||
for(i = 0;i < tpdu->Timeout; i++)
|
||||
{
|
||||
DelayMs(1);//<2F><><EFBFBD>յȴ<D5B5><C8B4><EFBFBD>ʱ
|
||||
GetReg(REG_IRQ0,&irq0);
|
||||
|
||||
if(irq0 & BIT_RXIRQ)
|
||||
{
|
||||
GetReg( REG_ERROR, &error ); /*Get Error Status */
|
||||
error = error & (BIT_NODATAERR | BIT_COLLDET | BIT_PROTERR | BIT_INTEGERR);
|
||||
if(error != 0)
|
||||
return FAIL;//<2F><><EFBFBD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD>
|
||||
GetReg(REG_FIFOLENGTH,&tpdu->ReceiveLength);
|
||||
if(tpdu->ReceiveLength > 0)
|
||||
{
|
||||
Read_FIFO(tpdu->ReceiveLength,tpdu->pReceiveBuffer);
|
||||
result = SUCCESS;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
GetReg(REG_IRQ1,&irq1);
|
||||
|
||||
// if(irq1 & BIT_TIMER1IRQ)
|
||||
// {
|
||||
// result = FAIL;//δ<><CEB4>timeoutʱ<74><CAB1><EFBFBD>ڽ<EFBFBD><DABD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD>
|
||||
// }
|
||||
|
||||
}
|
||||
result = FAIL;//δ<><CEB4>timeoutʱ<74><CAB1><EFBFBD>ڽ<EFBFBD><DABD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD>
|
||||
return result;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
/****************************************************************/
|
||||
/*<2A><><EFBFBD><EFBFBD>: Rats */
|
||||
/*<2A><><EFBFBD><EFBFBD>: Request for answer to select */
|
||||
/*<2A><><EFBFBD><EFBFBD>: */
|
||||
/* */
|
||||
/*<2A><><EFBFBD>: */
|
||||
/* ats_len<65><6E><EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD>ATS<54><53><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD> */
|
||||
/* *ats<74><73><EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD><D5B5><EFBFBD>ATS<54><53><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8> */
|
||||
/* OK: Ӧ<><D3A6><EFBFBD><EFBFBD>ȷ */
|
||||
/* ERROR: Ӧ<><D3A6><EFBFBD><EFBFBD><EFBFBD> */
|
||||
/****************************************************************/
|
||||
#if 0
|
||||
unsigned char CPU_Rats( unsigned char *ats_len, unsigned char *ats )
|
||||
{
|
||||
return FAIL;
|
||||
}
|
||||
#else
|
||||
unsigned char CPU_Rats( unsigned char *ats_len, unsigned char *ats )
|
||||
{
|
||||
unsigned char result;
|
||||
unsigned char outbuffer[2], inbuffer[64];
|
||||
transmission_struct tpdu;
|
||||
tpdu.pSendBuffer = outbuffer;
|
||||
tpdu.pReceiveBuffer = inbuffer;
|
||||
tpdu.pSendBuffer[0] = 0xE0; /*Start byte */
|
||||
tpdu.pSendBuffer[1] = 0x50; /*default = 0x50 */
|
||||
tpdu.SendLength = 2;
|
||||
tpdu.Timeout = 160;
|
||||
//CPU_CARD.FWT = 160;//Ĭ<><C4AC><EFBFBD><EFBFBD>ʱʱ<CAB1><CAB1>
|
||||
result = FM176XX_TPDU( &tpdu );
|
||||
if ( result == SUCCESS )
|
||||
{
|
||||
*ats_len = tpdu.ReceiveLength;
|
||||
memcpy( ats, tpdu.pReceiveBuffer, *ats_len );
|
||||
}
|
||||
return (result);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#if 0
|
||||
unsigned char Ats_Process( unsigned char ats_len, unsigned char *ats )/* ATS<54><53><EFBFBD>ݽ<EFBFBD><DDBD><EFBFBD> */
|
||||
{
|
||||
return FAIL;
|
||||
}
|
||||
#else
|
||||
unsigned char Ats_Process( unsigned char ats_len, unsigned char *ats )/* ATS<54><53><EFBFBD>ݽ<EFBFBD><DDBD><EFBFBD> */
|
||||
{
|
||||
unsigned char result, offset;
|
||||
|
||||
if ( (ats_len == ats[0]) || (ats_len > 2) )/*TL */
|
||||
{
|
||||
CPU_CARD.FSCI = ats[1] & 0x0F;
|
||||
if ( CPU_CARD.FSCI == 0 )
|
||||
CPU_CARD.FSC = 13;
|
||||
if ( CPU_CARD.FSCI == 1 )
|
||||
CPU_CARD.FSC = 21;
|
||||
if ( CPU_CARD.FSCI == 2 )
|
||||
CPU_CARD.FSC = 29;
|
||||
if ( CPU_CARD.FSCI == 3 )
|
||||
CPU_CARD.FSC = 37;
|
||||
if ( CPU_CARD.FSCI == 4 )
|
||||
CPU_CARD.FSC = 45;
|
||||
if ( CPU_CARD.FSCI == 5 )
|
||||
CPU_CARD.FSC = 61;
|
||||
if ( CPU_CARD.FSCI == 6 )
|
||||
CPU_CARD.FSC = 93;
|
||||
if ( CPU_CARD.FSCI == 7 )
|
||||
CPU_CARD.FSC = 125;
|
||||
if ( CPU_CARD.FSCI > 7 )
|
||||
CPU_CARD.FSC = 253;
|
||||
//CPU_CARD.FSC = 13;
|
||||
printf( "-> CPU_CARD.FSC = %02X\r\n",CPU_CARD.FSC );
|
||||
}
|
||||
else
|
||||
{
|
||||
result = FAIL;
|
||||
return (result);
|
||||
}
|
||||
offset = 0;
|
||||
result = SUCCESS;
|
||||
if ( ats[1] & BIT5 ) /*TA */
|
||||
{
|
||||
CPU_CARD.TA = ats[2];
|
||||
offset++;
|
||||
}
|
||||
|
||||
if ( ats[1] & BIT6 ) /*TB */
|
||||
{
|
||||
CPU_CARD.TB = ats[2 + offset];
|
||||
CPU_CARD.SFGI = CPU_CARD.TB & 0x0F;
|
||||
CPU_CARD.FWI = (CPU_CARD.TB >> 4) & 0x0F;
|
||||
printf( "-> CPU_CARD.SFGI = %02X\r\n",CPU_CARD.SFGI );
|
||||
printf( "-> CPU_CARD.FWI = %02X\r\n",CPU_CARD.FWI);
|
||||
if ( CPU_CARD.FWI < 2 )
|
||||
CPU_CARD.FWT = 1;
|
||||
if ( CPU_CARD.FWI == 2 )
|
||||
CPU_CARD.FWT = 2;
|
||||
if ( CPU_CARD.FWI == 3 )
|
||||
CPU_CARD.FWT = 3;
|
||||
if ( CPU_CARD.FWI == 4 )
|
||||
CPU_CARD.FWT = 5;
|
||||
if ( CPU_CARD.FWI == 5 )
|
||||
CPU_CARD.FWT = 10;
|
||||
if ( CPU_CARD.FWI == 6 )
|
||||
CPU_CARD.FWT = 20;
|
||||
if ( CPU_CARD.FWI == 7 )
|
||||
CPU_CARD.FWT = 40;
|
||||
if ( CPU_CARD.FWI == 8 )
|
||||
CPU_CARD.FWT = 80;
|
||||
if ( CPU_CARD.FWI == 9 )
|
||||
CPU_CARD.FWT = 160;
|
||||
if ( CPU_CARD.FWI == 10 )
|
||||
CPU_CARD.FWT = 310;
|
||||
if ( CPU_CARD.FWI == 11 )
|
||||
CPU_CARD.FWT = 620;
|
||||
if ( CPU_CARD.FWI == 12 )
|
||||
CPU_CARD.FWT = 1240;
|
||||
if ( CPU_CARD.FWI == 13 )
|
||||
CPU_CARD.FWT = 2480;
|
||||
if ( CPU_CARD.FWI > 13 )
|
||||
CPU_CARD.FWT = 4950;
|
||||
offset++;
|
||||
}
|
||||
|
||||
if ( ats[1] & BIT7 )/*TC */
|
||||
{
|
||||
CPU_CARD.TC = ats[2 + offset];
|
||||
offset++;
|
||||
}
|
||||
CPU_CARD.PCB = 0x02;/*PCB<43><42>ʼֵΪ0x02 */
|
||||
return (result);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if 1
|
||||
//xtell 同时把CPU_TPDU 和 CPU_NAk 用空函数替换 SPi正常
|
||||
unsigned char CPU_NAK( transmission_struct *tpdu ) /* <20><>Ƭ<EFBFBD><C6AC><EFBFBD><EFBFBD>NAK */
|
||||
{
|
||||
return FAIL;
|
||||
}
|
||||
#else
|
||||
unsigned char CPU_NAK( transmission_struct *tpdu ) /* <20><>Ƭ<EFBFBD><C6AC><EFBFBD><EFBFBD>NAK */
|
||||
{
|
||||
unsigned char result, tpdu_send_buffer[255],tpdu_receive_buffer[255];
|
||||
|
||||
tpdu->pSendBuffer = tpdu_send_buffer;
|
||||
tpdu->pReceiveBuffer = tpdu_receive_buffer;
|
||||
|
||||
tpdu->pSendBuffer [0] = 0xB0 | CPU_CARD.PCB;
|
||||
tpdu->SendLength = 1;
|
||||
/*printf( "NAK\r\n" );*/
|
||||
|
||||
result = FM176XX_TPDU( tpdu );
|
||||
return (result);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if 0
|
||||
unsigned char CPU_TPDU( transmission_struct *tpdu )
|
||||
{
|
||||
return FAIL;
|
||||
}
|
||||
#else
|
||||
unsigned char CPU_TPDU( transmission_struct *tpdu )
|
||||
{
|
||||
unsigned char result, i, pcb_byte;
|
||||
transmission_struct nak_tpdu;
|
||||
|
||||
result = FM176XX_TPDU( tpdu );
|
||||
|
||||
for ( i = 0; i < 3; i++ )
|
||||
{
|
||||
if ( result != SUCCESS )
|
||||
{
|
||||
result = CPU_NAK( &nak_tpdu );
|
||||
if(result == SUCCESS)
|
||||
{
|
||||
if(nak_tpdu.ReceiveLength > 0)
|
||||
{
|
||||
memcpy( &pcb_byte, nak_tpdu.pReceiveBuffer, 1 );
|
||||
if((pcb_byte & 0xF0) == 0xA0)//R(ACK)
|
||||
{
|
||||
#if CPU_DEBUG
|
||||
printf("...pcb_byte = %02X\r\n",pcb_byte);
|
||||
printf("...CPU_CARD.PCB = %02X\r\n",CPU_CARD.PCB);
|
||||
#endif
|
||||
if((pcb_byte & 0x03) != CPU_CARD.PCB)
|
||||
{
|
||||
result = FM176XX_TPDU( tpdu );
|
||||
}
|
||||
else
|
||||
{
|
||||
tpdu->pSendBuffer[0] = tpdu->pSendBuffer[0] ^ 0x01;//
|
||||
CPU_CARD.PCB = tpdu->pSendBuffer[0] & 0x03;
|
||||
result = FM176XX_TPDU( tpdu );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
return (result);
|
||||
}
|
||||
#endif
|
||||
|
||||
/****************************************************************/
|
||||
/*<2A><><EFBFBD><EFBFBD>: CPU_APDU */
|
||||
/*<2A><><EFBFBD><EFBFBD>: <20>ú<EFBFBD><C3BA><EFBFBD>ʵ<EFBFBD><CAB5>ת<EFBFBD><D7AA>APDU<44><55><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD> CPU card reset */
|
||||
/*<2A><><EFBFBD><EFBFBD>: */
|
||||
/* */
|
||||
/*<2A><><EFBFBD>: */
|
||||
/* ats_len<65><6E><EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD> */
|
||||
/* ats<74><73><EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8> */
|
||||
/* OK: Ӧ<><D3A6><EFBFBD><EFBFBD>ȷ */
|
||||
/* ERROR: Ӧ<><D3A6><EFBFBD><EFBFBD><EFBFBD> */
|
||||
/****************************************************************/
|
||||
#if 1
|
||||
//xtell 同时把CPU_TPDU 和 CPU_NAk 用空函数替换 SPi正常
|
||||
unsigned char CPU_APDU( transmission_struct *apdu )
|
||||
{
|
||||
return FAIL;
|
||||
}
|
||||
#else
|
||||
unsigned char CPU_APDU( transmission_struct *apdu )
|
||||
{
|
||||
unsigned char result, pcb_byte, tpdu_send_buffer[255],tpdu_receive_buffer[255], i;
|
||||
unsigned char unsent_length;
|
||||
transmission_struct tpdu;
|
||||
tpdu.pSendBuffer = tpdu_send_buffer;
|
||||
tpdu.pReceiveBuffer = tpdu_receive_buffer;
|
||||
tpdu.Timeout = CPU_CARD.FWT;
|
||||
apdu->ReceiveLength = 0;
|
||||
unsent_length = apdu->SendLength; /*<2A><><EFBFBD>ô<EFBFBD><C3B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD> */
|
||||
|
||||
for ( i = 0; i < 16; i++ ) /*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̣<EFBFBD><CCA3><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD>鳤<EFBFBD><E9B3A4>16<31><36><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>16<31><36><EFBFBD>鷢<EFBFBD><E9B7A2> */
|
||||
{
|
||||
#if CPU_DEBUG
|
||||
printf("unsent_length = %02X\r\n",unsent_length);
|
||||
#endif
|
||||
if ( unsent_length < CPU_CARD.FSC )
|
||||
{
|
||||
/*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD>֡<EFBFBD><D6A1><EFBFBD>ȣ<EFBFBD><C8A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
||||
tpdu.pSendBuffer[0] = CPU_CARD.PCB; /*PCB<43>ֽ<EFBFBD>д<EFBFBD><D0B4>TPDU<44><55><EFBFBD><EFBFBD> */
|
||||
memcpy( tpdu.pSendBuffer + 1, apdu->pSendBuffer + apdu->SendLength - unsent_length, unsent_length ); /*APDU<44><55><EFBFBD><EFBFBD>д<EFBFBD><D0B4>TPDU<44><55><EFBFBD><EFBFBD> */
|
||||
tpdu.SendLength = unsent_length + 1; /*<2A><><EFBFBD>ͳ<EFBFBD><CDB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1 */
|
||||
#if CPU_DEBUG
|
||||
printf("--> ");
|
||||
for(i=0;i<tpdu.SendLength;i++)
|
||||
printf("%02X",tpdu.pSendBuffer[i]);
|
||||
printf("\r\n");
|
||||
#endif
|
||||
result = CPU_TPDU( &tpdu );
|
||||
if ( (result != SUCCESS) || (tpdu.ReceiveLength == 0) )
|
||||
return (result);
|
||||
#if CPU_DEBUG
|
||||
printf("<-- ");
|
||||
for(i=0;i<tpdu.ReceiveLength;i++)
|
||||
printf("%02X",tpdu.pReceiveBuffer[i]);
|
||||
printf("\r\n");
|
||||
#endif
|
||||
unsent_length = 0; /*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD><EFBFBD><EFBFBD>0 */
|
||||
break; /*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݷ<EFBFBD><DDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̺<EFBFBD><CCBA>˳<EFBFBD>ѭ<EFBFBD><D1AD> */
|
||||
}
|
||||
else
|
||||
{
|
||||
/*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3>ȴ<EFBFBD><C8B4>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֡<EFBFBD><D6A1><EFBFBD>ȣ<EFBFBD><C8A3><EFBFBD><EFBFBD>鷢<EFBFBD>ͣ<EFBFBD>ÿ<EFBFBD>鷢<EFBFBD>͵<EFBFBD><CDB5><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>Ϊ<EFBFBD><CEAA>CPU_CARD.FSC - 1<><31> */
|
||||
tpdu.pSendBuffer[0] = CPU_CARD.PCB | 0x10; /*PCB<43>ֽ<EFBFBD>д<EFBFBD><D0B4>TPDU<44><55><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӱ<EFBFBD>ʶ */
|
||||
memcpy( tpdu.pSendBuffer + 1, apdu->pSendBuffer + apdu->SendLength - unsent_length, CPU_CARD.FSC - 1 ); /*APDU<44><55><EFBFBD><EFBFBD>д<EFBFBD><D0B4>TPDU<44><55><EFBFBD><EFBFBD> */
|
||||
|
||||
tpdu.SendLength = CPU_CARD.FSC; /*<2A><><EFBFBD>鷢<EFBFBD>ͳ<EFBFBD><CDB3><EFBFBD> */
|
||||
#if CPU_DEBUG
|
||||
printf("..--> ");
|
||||
for(i=0;i<tpdu.SendLength;i++)
|
||||
printf("%02X",tpdu.pSendBuffer[i]);
|
||||
printf("\r\n");
|
||||
#endif
|
||||
result = CPU_TPDU( &tpdu );
|
||||
#if CPU_DEBUG
|
||||
printf("<-- ");
|
||||
for(i=0;i<tpdu.ReceiveLength;i++)
|
||||
printf("%02X",tpdu.pReceiveBuffer[i]);
|
||||
printf("\r\n");
|
||||
#endif
|
||||
if ( (result != SUCCESS) && (tpdu.ReceiveLength == 1) )
|
||||
return (result);
|
||||
memcpy( &pcb_byte, tpdu.pReceiveBuffer, 1 ); /* */
|
||||
if ( (pcb_byte == 0xA2) || (pcb_byte == 0xA3) )
|
||||
{
|
||||
unsent_length = unsent_length - (CPU_CARD.FSC - 1); /*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ô<EFBFBD><C3B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD> */
|
||||
CPU_CARD.PCB = (pcb_byte & 0x0F) ^ 0x01; /*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>PCB<43>ֽ<EFBFBD> */
|
||||
#if CPU_DEBUG
|
||||
printf("unsent_length = %02X\r\n",unsent_length);
|
||||
#endif
|
||||
|
||||
}
|
||||
else
|
||||
return (FAIL); /* */
|
||||
}
|
||||
}
|
||||
for ( i = 0; i < 255; i++ ) /*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̣<EFBFBD><CCA3><EFBFBD><EFBFBD>֧<EFBFBD><D6A7>255<35><35>ACK<43><4B><EFBFBD><EFBFBD>WTXӦ<58><D3A6> */
|
||||
{
|
||||
if ( (result != SUCCESS) || (tpdu.ReceiveLength == 0) )
|
||||
return (FAIL);
|
||||
memcpy( &pcb_byte, tpdu.pReceiveBuffer, 1 ); /*<2A><>ȡPCB<43>ֽ<EFBFBD> */
|
||||
if ( (pcb_byte == 0x02) || (pcb_byte == 0x03) )
|
||||
{
|
||||
CPU_CARD.PCB = pcb_byte ^ 0x01; /*<2A><><EFBFBD><EFBFBD>PCB<43>ֽ<EFBFBD> */
|
||||
#if CPU_DEBUG
|
||||
printf("....CPU_CARD.PCB = %02X\r\n",CPU_CARD.PCB);
|
||||
#endif
|
||||
memcpy( apdu->pReceiveBuffer + apdu->ReceiveLength, tpdu.pReceiveBuffer + 1, tpdu.ReceiveLength - 1 );
|
||||
apdu->ReceiveLength = apdu->ReceiveLength + tpdu.ReceiveLength - 1;
|
||||
return (SUCCESS); /*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݽ<EFBFBD><DDBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
||||
}
|
||||
|
||||
if ( (pcb_byte == 0x12) || (pcb_byte == 0x13) )
|
||||
{
|
||||
memcpy( apdu->pReceiveBuffer + apdu->ReceiveLength, tpdu.pReceiveBuffer + 1, tpdu.ReceiveLength - 1 );
|
||||
apdu->ReceiveLength = apdu->ReceiveLength + tpdu.ReceiveLength - 1;
|
||||
tpdu.pSendBuffer[0] = ( (pcb_byte & 0x03) | 0xA0) ^ 0x01; /*<2A>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD>֡<EFBFBD><D6A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>PCB<43>ֽ<EFBFBD> */
|
||||
tpdu.SendLength = 1;
|
||||
#if CPU_DEBUG
|
||||
printf("...--> = ");
|
||||
for(i=0;i<tpdu.SendLength;i++)
|
||||
printf("%02X",tpdu.pSendBuffer[i]);
|
||||
printf("\r\n");
|
||||
#endif
|
||||
result = CPU_TPDU( &tpdu ); /*<2A>ط<EFBFBD>ACK */
|
||||
#if CPU_DEBUG
|
||||
printf("<-- = ");
|
||||
for(i=0;i<tpdu.ReceiveLength;i++)
|
||||
printf("%02X",tpdu.pReceiveBuffer[i]);
|
||||
printf("\r\n");
|
||||
#endif
|
||||
}
|
||||
|
||||
if ( (pcb_byte == 0xF2) || (pcb_byte == 0xF3) )
|
||||
{
|
||||
tpdu.SendLength = tpdu.ReceiveLength;
|
||||
memcpy(tpdu.pSendBuffer ,tpdu.pReceiveBuffer ,tpdu.SendLength); /*<2A>յ<EFBFBD>WTX֡<58><D6A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>PCB<43>ֽ<EFBFBD> */
|
||||
#if CPU_DEBUG
|
||||
printf("....--> = ");
|
||||
for(i=0;i<tpdu.SendLength;i++)
|
||||
printf("%02X",tpdu.pSendBuffer[i]);
|
||||
printf("\r\n");
|
||||
#endif
|
||||
result = CPU_TPDU( &tpdu ); /*<2A>ط<EFBFBD>WTX */
|
||||
#if CPU_DEBUG
|
||||
printf("<-- = ");
|
||||
for(i=0;i<tpdu.ReceiveLength;i++)
|
||||
printf("%02X",tpdu.pReceiveBuffer[i]);
|
||||
printf("\r\n");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
return (FAIL); /*<2A><><EFBFBD>մ<EFBFBD><D5B4><EFBFBD> */
|
||||
}
|
||||
#endif
|
||||
58
apps/earphone/94_rfid_stc/READER/CPU_CARD.h
Normal file
58
apps/earphone/94_rfid_stc/READER/CPU_CARD.h
Normal file
@ -0,0 +1,58 @@
|
||||
#ifndef CPU_CARD_H
|
||||
#define CPU_CARD_H 1
|
||||
|
||||
typedef struct
|
||||
{
|
||||
unsigned char SendLength;
|
||||
unsigned char *pSendBuffer;
|
||||
unsigned char ReceiveLength;
|
||||
unsigned char *pReceiveBuffer;
|
||||
unsigned int Timeout;
|
||||
}transmission_struct;
|
||||
|
||||
struct ATS_STR
|
||||
{
|
||||
unsigned char Length;
|
||||
unsigned char Ats_Data[255];
|
||||
};
|
||||
struct PPS_STR
|
||||
{
|
||||
unsigned char Length;
|
||||
unsigned char Pps_Data[1];
|
||||
};
|
||||
|
||||
struct CPU_CARD_STR
|
||||
{
|
||||
unsigned char FSCI;
|
||||
unsigned char FSC;
|
||||
unsigned char FWI;
|
||||
unsigned int FWT;
|
||||
unsigned char SFGI;
|
||||
unsigned char TA;
|
||||
unsigned char TB;
|
||||
unsigned char TC;
|
||||
unsigned char PCB;
|
||||
unsigned char WTXM;
|
||||
struct ATS_STR ATS;
|
||||
struct PPS_STR PPS;
|
||||
};
|
||||
|
||||
|
||||
extern struct CPU_CARD_STR CPU_CARD;
|
||||
extern unsigned char Ats_Process( unsigned char ats_len, unsigned char *ats );
|
||||
|
||||
extern unsigned char CPU_CARD_EVENT( void );
|
||||
extern unsigned char CPU_TPDU( transmission_struct *tpdu );
|
||||
|
||||
extern unsigned char CPU_Rats( unsigned char *ats_len, unsigned char *ats );
|
||||
|
||||
|
||||
extern unsigned char CPU_NAK( transmission_struct *tpdu );
|
||||
|
||||
|
||||
extern unsigned char CPU_APDU( transmission_struct *apdu );
|
||||
|
||||
|
||||
extern unsigned char CPU_TPDU( transmission_struct *tpdu );
|
||||
|
||||
#endif
|
||||
491
apps/earphone/94_rfid_stc/READER/MIFARE.c
Normal file
491
apps/earphone/94_rfid_stc/READER/MIFARE.c
Normal file
@ -0,0 +1,491 @@
|
||||
#include "MIFARE.h"
|
||||
#include "READER.h"
|
||||
|
||||
#include "string.h"
|
||||
#include "READER_REG.h"
|
||||
|
||||
#include "xt_main.h"
|
||||
unsigned char SECTOR,BLOCK,BLOCK_NUM;
|
||||
unsigned char BLOCK_DATA[16];
|
||||
unsigned char KEY_A[16][6]=
|
||||
{{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},//0
|
||||
{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},//1
|
||||
{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},//2
|
||||
{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},//3
|
||||
{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},//4
|
||||
{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},//5
|
||||
{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},//6
|
||||
{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},//7
|
||||
{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},//8
|
||||
{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},//9
|
||||
{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},//10
|
||||
{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},//11
|
||||
{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},//12
|
||||
{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},//13
|
||||
{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},//14
|
||||
{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}};//15
|
||||
|
||||
unsigned char KEY_B[16][6]=
|
||||
{{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},//0
|
||||
{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},//1
|
||||
{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},//2
|
||||
{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},//3
|
||||
{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},//4
|
||||
{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},//5
|
||||
{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},//6
|
||||
{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},//7
|
||||
{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},//8
|
||||
{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},//9
|
||||
{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},//10
|
||||
{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},//11
|
||||
{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},//12
|
||||
{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},//13
|
||||
{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},//14
|
||||
{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}};//15
|
||||
|
||||
/*****************************************************************************************/
|
||||
/*<2A><><EFBFBD>ƣ<EFBFBD>Mifare_Clear_Crypto */
|
||||
/*<2A><><EFBFBD>ܣ<EFBFBD>Mifare_Clear_Crypto<74><6F><EFBFBD><EFBFBD><EFBFBD>֤<EFBFBD><D6A4>־ */
|
||||
/*<2A><><EFBFBD>룺 */
|
||||
/* */
|
||||
/*<2A><><EFBFBD>: */
|
||||
/* */
|
||||
/* */
|
||||
/*****************************************************************************************/
|
||||
void Mifare_Clear_Crypto(void)
|
||||
{
|
||||
ModifyReg(REG_STATUS,BIT_CRYPTO1ON,RESET);
|
||||
return;
|
||||
}
|
||||
|
||||
unsigned char MIFARE_CARD_EVENTAAAAAAA(void)
|
||||
{
|
||||
unsigned char result;
|
||||
Mifare_Clear_Crypto();
|
||||
SECTOR = 1;
|
||||
//for(SECTOR = 0;SECTOR < 16; SECTOR++)
|
||||
{
|
||||
BLOCK_NUM = (SECTOR * 4) + BLOCK;
|
||||
result = Mifare_Auth(KEY_A_M1,SECTOR,KEY_A[SECTOR],PICC_A.UID);
|
||||
if(result != SUCCESS)
|
||||
{
|
||||
SetCW(DISABLE);
|
||||
printf("-> AUTH ERROR!\r\n");
|
||||
return result;
|
||||
}
|
||||
|
||||
printf("-> AUTH SUCCESS!\r\n");
|
||||
|
||||
for(BLOCK = 0;BLOCK < 3;BLOCK++)
|
||||
{
|
||||
BLOCK_NUM = (SECTOR * 4) + BLOCK;
|
||||
if(BLOCK_NUM == 0)
|
||||
BLOCK_NUM = 1;
|
||||
printf("-> SECTOR = %02X\r\n",SECTOR);;
|
||||
printf("-> BLOCK = %02X\r\n",BLOCK);
|
||||
printf("-> BLOCK_NUM = %02X\r\n",BLOCK_NUM);
|
||||
memcpy(BLOCK_DATA,"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF",16);
|
||||
result = Mifare_Blockwrite(BLOCK_NUM,BLOCK_DATA);
|
||||
if(result != SUCCESS)
|
||||
{
|
||||
SetCW(DISABLE);
|
||||
printf("-> WRITE BLOCK ERROR!\r\n");
|
||||
return result;
|
||||
}
|
||||
printf("-> WRITE BLOCK SUCCESS!\r\n");
|
||||
|
||||
result = Mifare_Blockread(BLOCK_NUM,BLOCK_DATA);
|
||||
if(result != SUCCESS)
|
||||
{
|
||||
SetCW(DISABLE);
|
||||
printf("-> READ BLOCK ERROR!\r\n");
|
||||
return result;
|
||||
}
|
||||
|
||||
printf("-> READ BLOCK = %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X\r\n",BLOCK_DATA[0],BLOCK_DATA[1],BLOCK_DATA[2],BLOCK_DATA[3],BLOCK_DATA[4],BLOCK_DATA[5],BLOCK_DATA[6],BLOCK_DATA[7],BLOCK_DATA[8],BLOCK_DATA[9],BLOCK_DATA[10],BLOCK_DATA[11],BLOCK_DATA[12],BLOCK_DATA[13],BLOCK_DATA[14],BLOCK_DATA[15]);
|
||||
}
|
||||
}
|
||||
SetCW(DISABLE);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
unsigned char MIFARE_CARD_EVENT(void)
|
||||
{
|
||||
unsigned char result;
|
||||
Mifare_Clear_Crypto();
|
||||
SECTOR = 1;
|
||||
//for(SECTOR = 0;SECTOR < 16; SECTOR++)
|
||||
{
|
||||
BLOCK_NUM = (SECTOR * 4) + BLOCK;
|
||||
result = Mifare_Auth(KEY_A_M1,SECTOR,KEY_A[SECTOR],PICC_A.UID);
|
||||
if(result != SUCCESS)
|
||||
{
|
||||
SetCW(DISABLE);
|
||||
printf("-> AUTH ERROR!\r\n");
|
||||
return result;
|
||||
}
|
||||
|
||||
printf("-> AUTH SUCCESS!\r\n");
|
||||
|
||||
for(BLOCK = 0;BLOCK < 3;BLOCK++)
|
||||
{
|
||||
BLOCK_NUM = (SECTOR * 4) + BLOCK;
|
||||
if(BLOCK_NUM == 0)
|
||||
BLOCK_NUM = 1;
|
||||
printf("-> SECTOR = %02X\r\n",SECTOR);;
|
||||
printf("-> BLOCK = %02X\r\n",BLOCK);
|
||||
printf("-> BLOCK_NUM = %02X\r\n",BLOCK_NUM);
|
||||
memcpy(BLOCK_DATA,"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF",16);
|
||||
result = Mifare_Blockwrite(BLOCK_NUM,BLOCK_DATA);
|
||||
if(result != SUCCESS)
|
||||
{
|
||||
SetCW(DISABLE);
|
||||
printf("-> WRITE BLOCK ERROR!\r\n");
|
||||
return result;
|
||||
}
|
||||
printf("-> WRITE BLOCK SUCCESS!\r\n");
|
||||
|
||||
result = Mifare_Blockread(BLOCK_NUM,BLOCK_DATA);
|
||||
if(result != SUCCESS)
|
||||
{
|
||||
SetCW(DISABLE);
|
||||
printf("-> READ BLOCK ERROR!\r\n");
|
||||
return result;
|
||||
}
|
||||
|
||||
printf("-> READ BLOCK = %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X\r\n",BLOCK_DATA[0],BLOCK_DATA[1],BLOCK_DATA[2],BLOCK_DATA[3],BLOCK_DATA[4],BLOCK_DATA[5],BLOCK_DATA[6],BLOCK_DATA[7],BLOCK_DATA[8],BLOCK_DATA[9],BLOCK_DATA[10],BLOCK_DATA[11],BLOCK_DATA[12],BLOCK_DATA[13],BLOCK_DATA[14],BLOCK_DATA[15]);
|
||||
}
|
||||
}
|
||||
SetCW(DISABLE);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
unsigned char Mifare_LoadKey(unsigned char *mifare_key)
|
||||
{
|
||||
unsigned char reg_data;
|
||||
SetCommand(CMD_IDLE);
|
||||
ModifyReg(REG_FIFOCONTROL,BIT_FIFOFLUSH,ENABLE); //Clear FIFO
|
||||
SetReg(REG_FIFODATA,mifare_key[0]);
|
||||
SetReg(REG_FIFODATA,mifare_key[1]);
|
||||
SetReg(REG_FIFODATA,mifare_key[2]);
|
||||
SetReg(REG_FIFODATA,mifare_key[3]);
|
||||
SetReg(REG_FIFODATA,mifare_key[4]);
|
||||
SetReg(REG_FIFODATA,mifare_key[5]);
|
||||
SetCommand(CMD_LOADKEY);
|
||||
DelayMs(1);
|
||||
GetReg(REG_COMMAND,®_data);
|
||||
if((reg_data & CMD_MASK) == CMD_IDLE)
|
||||
return SUCCESS;
|
||||
else
|
||||
return FAIL;
|
||||
}
|
||||
|
||||
/*****************************************************************************************/
|
||||
/*<2A><><EFBFBD>ƣ<EFBFBD>Mifare_Auth */
|
||||
/*<2A><><EFBFBD>ܣ<EFBFBD>Mifare_Auth<74><68>Ƭ<EFBFBD><C6AC>֤ */
|
||||
/*<2A><><EFBFBD>룺mode<64><65><EFBFBD><EFBFBD>֤ģʽ<C4A3><CABD>0<EFBFBD><30>key A<><41>֤<EFBFBD><D6A4>1<EFBFBD><31>key B<><42>֤<EFBFBD><D6A4><EFBFBD><EFBFBD>sector<6F><72><EFBFBD><EFBFBD>֤<EFBFBD><D6A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ţ<EFBFBD>0~15<31><35> */
|
||||
/* *mifare_key<65><79>6<EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD>֤<EFBFBD><D6A4>Կ<EFBFBD><D4BF><EFBFBD>飻*card_uid<69><64>4<EFBFBD>ֽڿ<D6BD>ƬUID<49><44><EFBFBD><EFBFBD> */
|
||||
/*<2A><><EFBFBD>: */
|
||||
/* OK :<3A><>֤<EFBFBD>ɹ<EFBFBD> */
|
||||
/* ERROR :<3A><>֤ʧ<D6A4><CAA7> */
|
||||
/*****************************************************************************************/
|
||||
unsigned char Mifare_Auth(unsigned char key_mode,unsigned char sector,unsigned char *mifare_key,unsigned char *card_uid)
|
||||
{
|
||||
unsigned char result,reg_data;
|
||||
result = Mifare_LoadKey(mifare_key);
|
||||
if (result != SUCCESS)
|
||||
return result;
|
||||
SetCommand(CMD_IDLE);
|
||||
ModifyReg(REG_FIFOCONTROL,BIT_FIFOFLUSH,ENABLE); //Clear FIFO
|
||||
if(key_mode == KEY_A_M1)
|
||||
{
|
||||
SetReg(REG_FIFODATA,0x60);//60 keyA M1<4D><31>ָ֤<D6A4><D6B8>
|
||||
ModifyReg(REG_RXTXCON,BIT_SHMODE,DISABLE);
|
||||
}
|
||||
if(key_mode == KEY_B_M1)
|
||||
{
|
||||
SetReg(REG_FIFODATA,0x61);//61 keyB M1<4D><31>ָ֤<D6A4><D6B8>
|
||||
ModifyReg(REG_RXTXCON,BIT_SHMODE,DISABLE);
|
||||
}
|
||||
|
||||
SetReg(REG_FIFODATA,sector * 4);//<2F><>֤<EFBFBD><D6A4><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD>0<EFBFBD><30>ַ
|
||||
SetReg(REG_FIFODATA,card_uid[0]);
|
||||
SetReg(REG_FIFODATA,card_uid[1]);
|
||||
SetReg(REG_FIFODATA,card_uid[2]);
|
||||
SetReg(REG_FIFODATA,card_uid[3]);
|
||||
ModifyReg(REG_TXCRCCON, BIT_CRCEN,ENABLE);
|
||||
ModifyReg(REG_RXCRCCON, BIT_CRCEN,ENABLE);
|
||||
SetCommand(CMD_AUTHENT);
|
||||
DelayMs(5);
|
||||
GetReg(REG_COMMAND,®_data);
|
||||
if((reg_data & CMD_MASK) == CMD_IDLE)
|
||||
{
|
||||
GetReg(REG_STATUS,®_data);
|
||||
if(reg_data & BIT_CRYPTO1ON)//<2F>жϼ<D0B6><CFBC>ܱ<EFBFBD>־λ<D6BE><CEBB>ȷ<EFBFBD><C8B7><EFBFBD><EFBFBD>֤<EFBFBD><D6A4><EFBFBD>
|
||||
return SUCCESS;
|
||||
}
|
||||
return FAIL;
|
||||
}
|
||||
/*****************************************************************************************/
|
||||
/*<2A><><EFBFBD>ƣ<EFBFBD>Mifare_Blockset */
|
||||
/*<2A><><EFBFBD>ܣ<EFBFBD>Mifare_Blockset<65><74>Ƭ<EFBFBD><C6AC>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD> */
|
||||
/*<2A><><EFBFBD>룺block<63><6B><EFBFBD><EFBFBD>ţ<EFBFBD>*buff<66><66><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD>õ<EFBFBD>4<EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD> */
|
||||
/* */
|
||||
/*<2A><><EFBFBD>: */
|
||||
/* OK :<3A><><EFBFBD>óɹ<C3B3> */
|
||||
/* ERROR :<3A><><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7> */
|
||||
/*****************************************************************************************/
|
||||
unsigned char Mifare_Blockset(unsigned char block,unsigned char *data_buff)
|
||||
{
|
||||
unsigned char block_data[16],result;
|
||||
block_data[0] = data_buff[3];
|
||||
block_data[1] = data_buff[2];
|
||||
block_data[2] = data_buff[1];
|
||||
block_data[3] = data_buff[0];
|
||||
block_data[4] = ~data_buff[3];
|
||||
block_data[5] = ~data_buff[2];
|
||||
block_data[6] = ~data_buff[1];
|
||||
block_data[7] = ~data_buff[0];
|
||||
block_data[8] = data_buff[3];
|
||||
block_data[9] = data_buff[2];
|
||||
block_data[10] = data_buff[1];
|
||||
block_data[11] = data_buff[0];
|
||||
block_data[12] = block;
|
||||
block_data[13] = ~block;
|
||||
block_data[14] = block;
|
||||
block_data[15] = ~block;
|
||||
result = Mifare_Blockwrite(block,block_data);
|
||||
return result;
|
||||
}
|
||||
/*****************************************************************************************/
|
||||
/*<2A><><EFBFBD>ƣ<EFBFBD>Mifare_Blockread */
|
||||
/*<2A><><EFBFBD>ܣ<EFBFBD>Mifare_Blockread<61><64>Ƭ<EFBFBD><C6AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
||||
/*<2A><><EFBFBD>룺block<63><6B><EFBFBD><EFBFBD>ţ<EFBFBD>0x00~0x3F<33><46><EFBFBD><EFBFBD>buff<66><66>16<31>ֽڶ<D6BD><DAB6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
||||
/*<2A><><EFBFBD>: */
|
||||
/* OK :<3A>ɹ<EFBFBD> */
|
||||
/* ERROR :ʧ<><CAA7> */
|
||||
/*****************************************************************************************/
|
||||
unsigned char Mifare_Blockread(unsigned char block,unsigned char *data_buff)
|
||||
{
|
||||
unsigned char reg_data,i;
|
||||
SetCommand(CMD_IDLE);
|
||||
SetReg(REG_TXDATANUM,0x08);
|
||||
SetReg(REG_FIFODATA,0x30);//30 <20><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8>
|
||||
SetReg(REG_FIFODATA,block);//<2F><><EFBFBD>ַ
|
||||
ModifyReg(REG_TXCRCCON, BIT_CRCEN,ENABLE);
|
||||
ModifyReg(REG_RXCRCCON, BIT_CRCEN,ENABLE);
|
||||
SetCommand(CMD_TRANSCEIVE);
|
||||
DelayMs(2);
|
||||
GetReg(REG_FIFOLENGTH,®_data);
|
||||
if (reg_data != 16) //<2F><><EFBFBD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>Ϊ16
|
||||
return FAIL;
|
||||
GetReg(REG_ERROR,®_data);
|
||||
if(reg_data & 0x07)
|
||||
return FAIL;
|
||||
for(i=0;i<16;i++)
|
||||
{
|
||||
GetReg (REG_FIFODATA,&data_buff[i]);
|
||||
}
|
||||
return SUCCESS;
|
||||
}
|
||||
/*****************************************************************************************/
|
||||
/*<2A><><EFBFBD>ƣ<EFBFBD>mifare_blockwrite */
|
||||
/*<2A><><EFBFBD>ܣ<EFBFBD>Mifare<72><65>Ƭд<C6AC><D0B4><EFBFBD><EFBFBD><EFBFBD> */
|
||||
/*<2A><><EFBFBD>룺block<63><6B><EFBFBD><EFBFBD>ţ<EFBFBD>0x00~0x3F<33><46><EFBFBD><EFBFBD>buff<66><66>16<31>ֽ<EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
||||
/*<2A><><EFBFBD>: */
|
||||
/* OK :<3A>ɹ<EFBFBD> */
|
||||
/* ERROR :ʧ<><CAA7> */
|
||||
/*****************************************************************************************/
|
||||
unsigned char Mifare_Blockwrite(unsigned char block,unsigned char *data_buff)
|
||||
{
|
||||
unsigned char reg_data,i;
|
||||
SetCommand(CMD_IDLE);
|
||||
SetReg(REG_TXDATANUM,0x08);
|
||||
SetReg(REG_FIFODATA,0xA0);//A0 д<><D0B4>ָ<EFBFBD><D6B8>
|
||||
SetReg(REG_FIFODATA,block);//<2F><><EFBFBD>ַ
|
||||
ModifyReg(REG_TXCRCCON, BIT_CRCEN,ENABLE);
|
||||
ModifyReg(REG_RXCRCCON, BIT_CRCEN,DISABLE);
|
||||
SetCommand(CMD_TRANSCEIVE);
|
||||
DelayMs(5);
|
||||
GetReg(REG_FIFOLENGTH,®_data);
|
||||
if (reg_data != 1) //<2F><><EFBFBD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>Ϊ1
|
||||
return FAIL;
|
||||
GetReg (REG_FIFODATA,®_data);
|
||||
if(reg_data != 0x0A)
|
||||
return FAIL;
|
||||
for(i=0;i<16;i++)
|
||||
{
|
||||
SetReg(REG_FIFODATA,data_buff[i]);
|
||||
}
|
||||
SetCommand(CMD_TRANSCEIVE);
|
||||
DelayMs(5);
|
||||
GetReg(REG_FIFOLENGTH,®_data);
|
||||
if (reg_data != 1) //<2F><><EFBFBD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>Ϊ1
|
||||
return FAIL;
|
||||
GetReg (REG_FIFODATA,®_data);
|
||||
if(reg_data != 0x0A)
|
||||
return FAIL;
|
||||
|
||||
return SUCCESS;
|
||||
|
||||
}
|
||||
|
||||
/*****************************************************************************************/
|
||||
/*<2A><><EFBFBD>ƣ<EFBFBD> */
|
||||
/*<2A><><EFBFBD>ܣ<EFBFBD>Mifare <20><>Ƭ<EFBFBD><C6AC>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD> */
|
||||
/*<2A><><EFBFBD>룺block<63><6B><EFBFBD><EFBFBD>ţ<EFBFBD>0x00~0x3F<33><46><EFBFBD><EFBFBD>buff<66><66>4<EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
||||
/*<2A><><EFBFBD>: */
|
||||
/* OK :<3A>ɹ<EFBFBD> */
|
||||
/* ERROR :ʧ<><CAA7> */
|
||||
/*****************************************************************************************/
|
||||
unsigned char Mifare_Blockinc(unsigned char block,unsigned char *data_buff)
|
||||
{
|
||||
|
||||
unsigned char reg_data,i;
|
||||
SetCommand(CMD_IDLE);
|
||||
SetReg(REG_TXDATANUM,0x08);
|
||||
SetReg(REG_FIFODATA,0xC1);//C1 <20><>ֵָ<D6B5><D6B8>
|
||||
SetReg(REG_FIFODATA,block);//<2F><><EFBFBD>ַ
|
||||
ModifyReg(REG_TXCRCCON, BIT_CRCEN,ENABLE);
|
||||
ModifyReg(REG_RXCRCCON, BIT_CRCEN,DISABLE);
|
||||
SetCommand(CMD_TRANSCEIVE);
|
||||
DelayMs(5);
|
||||
GetReg(REG_FIFOLENGTH,®_data);
|
||||
if (reg_data != 1) //<2F><><EFBFBD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>Ϊ1
|
||||
return FAIL;
|
||||
GetReg (REG_FIFODATA,®_data);
|
||||
if(reg_data != 0x0A)
|
||||
return FAIL;
|
||||
for(i=0;i<4;i++)
|
||||
{
|
||||
SetReg(REG_FIFODATA,data_buff[i]);
|
||||
}
|
||||
SetCommand(CMD_TRANSCEIVE);
|
||||
DelayMs(5);
|
||||
GetReg(REG_FIFOLENGTH,®_data);
|
||||
if (reg_data != 1) //<2F><><EFBFBD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>Ϊ1
|
||||
return FAIL;
|
||||
GetReg (REG_FIFODATA,®_data);
|
||||
|
||||
if(reg_data != 0x0A)
|
||||
return FAIL;
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*****************************************************************************************/
|
||||
/*<2A><><EFBFBD>ƣ<EFBFBD>mifare_blockdec */
|
||||
/*<2A><><EFBFBD>ܣ<EFBFBD>Mifare <20><>Ƭ<EFBFBD><C6AC>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD> */
|
||||
/*<2A><><EFBFBD>룺block<63><6B><EFBFBD><EFBFBD>ţ<EFBFBD>0x00~0x3F<33><46><EFBFBD><EFBFBD>buff<66><66>4<EFBFBD>ֽڼ<D6BD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
||||
/*<2A><><EFBFBD>: */
|
||||
/* OK :<3A>ɹ<EFBFBD> */
|
||||
/* ERROR :ʧ<><CAA7> */
|
||||
/*****************************************************************************************/
|
||||
unsigned char Mifare_Blockdec(unsigned char block,unsigned char *data_buff)
|
||||
{
|
||||
unsigned char reg_data,i;
|
||||
SetCommand(CMD_IDLE);
|
||||
SetReg(REG_TXDATANUM,0x08);
|
||||
SetReg(REG_FIFODATA,0xC0);//C0 <20><>ֵָ<D6B5><D6B8>
|
||||
SetReg(REG_FIFODATA,block);//<2F><><EFBFBD>ַ
|
||||
ModifyReg(REG_TXCRCCON, BIT_CRCEN,ENABLE);
|
||||
ModifyReg(REG_RXCRCCON, BIT_CRCEN,DISABLE);
|
||||
SetCommand(CMD_TRANSCEIVE);
|
||||
DelayMs(5);
|
||||
GetReg(REG_FIFOLENGTH,®_data);
|
||||
if (reg_data != 1) //<2F><><EFBFBD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>Ϊ1
|
||||
return FAIL;
|
||||
GetReg (REG_FIFODATA,®_data);
|
||||
if(reg_data != 0x0A)
|
||||
return FAIL;
|
||||
for(i=0;i<4;i++)
|
||||
{
|
||||
SetReg(REG_FIFODATA,data_buff[i]);
|
||||
}
|
||||
SetCommand(CMD_TRANSCEIVE);
|
||||
DelayMs(5);
|
||||
GetReg(REG_FIFOLENGTH,®_data);
|
||||
if (reg_data != 1) //<2F><><EFBFBD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>Ϊ1
|
||||
return FAIL;
|
||||
GetReg (REG_FIFODATA,®_data);
|
||||
if(reg_data != 0x0A)
|
||||
|
||||
return FAIL;
|
||||
|
||||
return SUCCESS;
|
||||
|
||||
}
|
||||
/*****************************************************************************************/
|
||||
/*<2A><><EFBFBD>ƣ<EFBFBD>mifare_transfer */
|
||||
/*<2A><><EFBFBD>ܣ<EFBFBD>Mifare <20><>Ƭtransfer<65><72><EFBFBD><EFBFBD> */
|
||||
/*<2A><><EFBFBD>룺block<63><6B><EFBFBD><EFBFBD>ţ<EFBFBD>0x00~0x3F<33><46> */
|
||||
/*<2A><><EFBFBD>: */
|
||||
/* OK :<3A>ɹ<EFBFBD> */
|
||||
/* ERROR :ʧ<><CAA7> */
|
||||
/*****************************************************************************************/
|
||||
unsigned char Mifare_Transfer(unsigned char block)
|
||||
{
|
||||
unsigned char reg_data;
|
||||
SetCommand(CMD_IDLE);
|
||||
SetReg(REG_TXDATANUM,0x08);
|
||||
SetReg(REG_FIFODATA,0xC1);//C1 Transferָ<72><D6B8>
|
||||
SetReg(REG_FIFODATA,block);//<2F><><EFBFBD>ַ
|
||||
ModifyReg(REG_TXCRCCON, BIT_CRCEN,ENABLE);
|
||||
ModifyReg(REG_RXCRCCON, BIT_CRCEN,DISABLE);
|
||||
SetCommand(CMD_TRANSCEIVE);
|
||||
DelayMs(5);
|
||||
GetReg(REG_FIFOLENGTH,®_data);
|
||||
if (reg_data != 1) //<2F><><EFBFBD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>Ϊ1
|
||||
return FAIL;
|
||||
GetReg (REG_FIFODATA,®_data);
|
||||
if(reg_data != 0x0A)
|
||||
return FAIL;
|
||||
|
||||
return SUCCESS;
|
||||
|
||||
|
||||
}
|
||||
/*****************************************************************************************/
|
||||
/*<2A><><EFBFBD>ƣ<EFBFBD>mifare_restore */
|
||||
/*<2A><><EFBFBD>ܣ<EFBFBD>Mifare <20><>Ƭrestore<72><65><EFBFBD><EFBFBD> */
|
||||
/*<2A><><EFBFBD>룺block<63><6B><EFBFBD><EFBFBD>ţ<EFBFBD>0x00~0x3F<33><46> */
|
||||
/*<2A><><EFBFBD>: */
|
||||
/* OK :<3A>ɹ<EFBFBD> */
|
||||
/* ERROR :ʧ<><CAA7> */
|
||||
/*****************************************************************************************/
|
||||
|
||||
unsigned char Mifare_Restore(unsigned char block)
|
||||
{
|
||||
unsigned char reg_data,i;
|
||||
SetCommand(CMD_IDLE);
|
||||
SetReg(REG_TXDATANUM,0x08);
|
||||
SetReg(REG_FIFODATA,0xC2);//C1 Transferָ<72><D6B8>
|
||||
SetReg(REG_FIFODATA,block);//<2F><><EFBFBD>ַ
|
||||
ModifyReg(REG_TXCRCCON, BIT_CRCEN,ENABLE);
|
||||
ModifyReg(REG_RXCRCCON, BIT_CRCEN,DISABLE);
|
||||
SetCommand(CMD_TRANSCEIVE);
|
||||
DelayMs(5);
|
||||
GetReg(REG_FIFOLENGTH,®_data);
|
||||
if (reg_data != 1) //<2F><><EFBFBD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>Ϊ1
|
||||
return FAIL;
|
||||
GetReg (REG_FIFODATA,®_data);
|
||||
if(reg_data != 0x0A)
|
||||
return FAIL;
|
||||
for(i=0;i<4;i++)
|
||||
{
|
||||
SetReg(REG_FIFODATA,0);
|
||||
}
|
||||
SetCommand(CMD_TRANSCEIVE);
|
||||
DelayMs(5);
|
||||
GetReg(REG_FIFOLENGTH,®_data);
|
||||
if (reg_data != 1) //<2F><><EFBFBD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>Ϊ1
|
||||
return FAIL;
|
||||
GetReg (REG_FIFODATA,®_data);
|
||||
if(reg_data != 0x0A)
|
||||
return FAIL;
|
||||
return SUCCESS;
|
||||
}
|
||||
247
apps/earphone/94_rfid_stc/READER/MIFARE_commented.c
Normal file
247
apps/earphone/94_rfid_stc/READER/MIFARE_commented.c
Normal file
@ -0,0 +1,247 @@
|
||||
/**
|
||||
* @file MIFARE.c
|
||||
* @brief MIFARE Classic 卡应用层驱动实现
|
||||
* @version 1.0
|
||||
* @date 2024-05-20
|
||||
*
|
||||
* @par 文件作用:
|
||||
* 该文件是 MIFARE.h 中声明的所有函数的具体实现。它负责处理与
|
||||
* MIFARE Classic 卡片交互的特定应用层逻辑,如认证、数据读写等。
|
||||
*/
|
||||
|
||||
#include "MIFARE.h"
|
||||
#include "READER.h"
|
||||
#include "string.h"
|
||||
#include "READER_REG.h"
|
||||
#include "xt_main.h"
|
||||
|
||||
//==================================================================================
|
||||
// 1. 全局变量定义
|
||||
//==================================================================================
|
||||
unsigned char SECTOR, BLOCK, BLOCK_NUM;
|
||||
unsigned char BLOCK_DATA[16]; // 16字节数据缓冲区
|
||||
|
||||
// MIFARE 各扇区默认密钥 (Key A 和 Key B),通常出厂时均为全F
|
||||
unsigned char KEY_A[16][6] = {
|
||||
{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}, {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},
|
||||
// ... (共16个扇区的密钥) ...
|
||||
};
|
||||
unsigned char KEY_B[16][6] = {
|
||||
{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}, {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},
|
||||
// ... (共16个扇区的密钥) ...
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief 清除芯片内部的 MIFARE 加密状态标志
|
||||
* @note 每次对新卡操作或认证失败后,都应调用此函数来复位加密状态。
|
||||
*/
|
||||
void Mifare_Clear_Crypto(void)
|
||||
{
|
||||
ModifyReg(REG_STATUS, BIT_CRYPTO1ON, 0); // 将状态寄存器中的 CRYPTO1ON 位清零
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief MIFARE 卡操作事件流程 (一个完整的读写示例)
|
||||
* @return unsigned char 操作结果
|
||||
*/
|
||||
unsigned char MIFARE_CARD_EVENT(void)
|
||||
{
|
||||
unsigned char result;
|
||||
Mifare_Clear_Crypto(); // 开始前先清除加密状态
|
||||
SECTOR = 1; // 示例:操作第1扇区
|
||||
|
||||
// 对指定扇区进行Key A认证
|
||||
result = Mifare_Auth(KEY_A_M1, SECTOR, KEY_A[SECTOR], PICC_A.UID);
|
||||
if(result != SUCCESS)
|
||||
{
|
||||
SetCW(DISABLE); // 认证失败,关闭天线
|
||||
printf("-> AUTH ERROR!\r\n");
|
||||
return result;
|
||||
}
|
||||
printf("-> AUTH SUCCESS!\r\n");
|
||||
|
||||
// 认证成功后,遍历并读写该扇区的数据块 (不含扇区尾块)
|
||||
for(BLOCK = 0; BLOCK < 3; BLOCK++)
|
||||
{
|
||||
BLOCK_NUM = (SECTOR * 4) + BLOCK; // 计算块的绝对地址
|
||||
|
||||
// 示例:向块写入全FF
|
||||
memcpy(BLOCK_DATA, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 16);
|
||||
result = Mifare_Blockwrite(BLOCK_NUM, BLOCK_DATA);
|
||||
if(result != SUCCESS)
|
||||
{
|
||||
SetCW(DISABLE);
|
||||
printf("-> WRITE BLOCK ERROR!\r\n");
|
||||
return result;
|
||||
}
|
||||
printf("-> WRITE BLOCK SUCCESS!\r\n");
|
||||
|
||||
// 示例:从块中读出数据
|
||||
result = Mifare_Blockread(BLOCK_NUM, BLOCK_DATA);
|
||||
if(result != SUCCESS)
|
||||
{
|
||||
SetCW(DISABLE);
|
||||
printf("-> READ BLOCK ERROR!\r\n");
|
||||
return result;
|
||||
}
|
||||
printf("-> READ BLOCK DATA: ...\r\n"); // 打印读出的数据
|
||||
}
|
||||
SetCW(DISABLE);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 加载6字节密钥到读卡器芯片内部的密钥缓冲器
|
||||
* @param mifare_key 指向6字节密钥数组的指针
|
||||
* @return unsigned char 成功或失败
|
||||
*/
|
||||
unsigned char Mifare_LoadKey(unsigned char *mifare_key)
|
||||
{
|
||||
unsigned char reg_data;
|
||||
SetCommand(CMD_IDLE);
|
||||
Clear_FIFO();
|
||||
// 1. 将6字节密钥依次写入FIFO
|
||||
SetReg(REG_FIFODATA, mifare_key[0]);
|
||||
SetReg(REG_FIFODATA, mifare_key[1]);
|
||||
SetReg(REG_FIFODATA, mifare_key[2]);
|
||||
SetReg(REG_FIFODATA, mifare_key[3]);
|
||||
SetReg(REG_FIFODATA, mifare_key[4]);
|
||||
SetReg(REG_FIFODATA, mifare_key[5]);
|
||||
// 2. 发送加载密钥命令,芯片会自动从FIFO读取密钥
|
||||
SetCommand(CMD_LOADKEY);
|
||||
DelayMs(1);
|
||||
GetReg(REG_COMMAND, ®_data);
|
||||
if((reg_data & CMD_MASK) == CMD_IDLE) // 检查命令是否执行完毕
|
||||
return SUCCESS;
|
||||
else
|
||||
return FAIL;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief **核心函数**: 对 MIFARE 卡指定扇区进行密码认证
|
||||
* @param key_mode 认证模式 (KEY_A_M1 或 KEY_B_M1)
|
||||
* @param sector 要认证的扇区号 (0-15)
|
||||
* @param mifare_key 指向6字节密钥的指针
|
||||
* @param card_uid 指向4字节卡片UID的指针 (从寻卡流程中获得)
|
||||
* @return unsigned char 成功或失败
|
||||
*/
|
||||
unsigned char Mifare_Auth(unsigned char key_mode, unsigned char sector, unsigned char *mifare_key, unsigned char *card_uid)
|
||||
{
|
||||
unsigned char result, reg_data;
|
||||
result = Mifare_LoadKey(mifare_key); // 1. 首先将密钥加载到芯片
|
||||
if (result != SUCCESS)
|
||||
return result;
|
||||
|
||||
SetCommand(CMD_IDLE);
|
||||
Clear_FIFO();
|
||||
// 2. 根据认证模式,将认证命令码 (0x60 for KeyA, 0x61 for KeyB) 写入FIFO
|
||||
if(key_mode == KEY_A_M1)
|
||||
{
|
||||
SetReg(REG_FIFODATA, 0x60);
|
||||
}
|
||||
else // KEY_B_M1
|
||||
{
|
||||
SetReg(REG_FIFODATA, 0x61);
|
||||
}
|
||||
|
||||
// 3. 将要认证的块地址、卡片UID写入FIFO
|
||||
SetReg(REG_FIFODATA, sector * 4); // MIFARE卡每个扇区4个块,认证扇区内任一地址即可
|
||||
SetReg(REG_FIFODATA, card_uid[0]);
|
||||
SetReg(REG_FIFODATA, card_uid[1]);
|
||||
SetReg(REG_FIFODATA, card_uid[2]);
|
||||
SetReg(REG_FIFODATA, card_uid[3]);
|
||||
|
||||
// 4. 发送认证命令,芯片将自动完成与卡片的三次握手加密认证流程
|
||||
SetCommand(CMD_AUTHENT);
|
||||
DelayMs(5);
|
||||
|
||||
// 5. 检查认证结果
|
||||
GetReg(REG_COMMAND, ®_data);
|
||||
if((reg_data & CMD_MASK) == CMD_IDLE) // 命令执行完毕
|
||||
{
|
||||
GetReg(REG_STATUS, ®_data);
|
||||
// 关键: 检查状态寄存器中的 CRYPTO1ON 位是否为1。
|
||||
// 此位由硬件在认证成功后自动置位。
|
||||
if(reg_data & BIT_CRYPTO1ON)
|
||||
return SUCCESS;
|
||||
}
|
||||
return FAIL;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 读取一个16字节的数据块
|
||||
* @param block 要读取的块的绝对地址
|
||||
* @param data_buff 用于存放读取数据的16字节缓冲区
|
||||
* @return unsigned char 成功或失败
|
||||
*/
|
||||
unsigned char Mifare_Blockread(unsigned char block, unsigned char *data_buff)
|
||||
{
|
||||
unsigned char reg_data, i;
|
||||
SetCommand(CMD_IDLE);
|
||||
Clear_FIFO();
|
||||
// 1. 准备读块指令 (命令码 0x30 + 块地址)
|
||||
SetReg(REG_FIFODATA, 0x30);
|
||||
SetReg(REG_FIFODATA, block);
|
||||
ModifyReg(REG_TXCRCCON, BIT_CRCEN, ENABLE); // 读写命令需要CRC
|
||||
ModifyReg(REG_RXCRCCON, BIT_CRCEN, ENABLE);
|
||||
// 2. 发送并等待接收
|
||||
SetCommand(CMD_TRANSCEIVE);
|
||||
DelayMs(2);
|
||||
GetReg(REG_FIFOLENGTH, ®_data);
|
||||
if (reg_data != 16) // 3. 检查是否收到了完整的16字节数据
|
||||
return FAIL;
|
||||
GetReg(REG_ERROR, ®_data);
|
||||
if(reg_data & 0x07) // 检查是否有通信错误
|
||||
return FAIL;
|
||||
// 4. 从FIFO中循环读出16字节数据
|
||||
for(i=0; i<16; i++)
|
||||
{
|
||||
GetReg(REG_FIFODATA, &data_buff[i]);
|
||||
}
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 写入一个16字节的数据块
|
||||
* @param block 要写入的块的绝对地址
|
||||
* @param data_buff 包含要写入的16字节数据的缓冲区
|
||||
* @return unsigned char 成功或失败
|
||||
*/
|
||||
unsigned char Mifare_Blockwrite(unsigned char block, unsigned char *data_buff)
|
||||
{
|
||||
unsigned char reg_data, i;
|
||||
SetCommand(CMD_IDLE);
|
||||
Clear_FIFO();
|
||||
// 1. 准备写块指令 (命令码 0xA0 + 块地址)
|
||||
SetReg(REG_FIFODATA, 0xA0);
|
||||
SetReg(REG_FIFODATA, block);
|
||||
SetCommand(CMD_TRANSCEIVE);
|
||||
DelayMs(5);
|
||||
// 2. 卡片会返回一个ACK (0x0A),检查是否收到
|
||||
GetReg(REG_FIFOLENGTH, ®_data);
|
||||
if (reg_data != 1)
|
||||
return FAIL;
|
||||
GetReg(REG_FIFODATA, ®_data);
|
||||
if(reg_data != 0x0A)
|
||||
return FAIL;
|
||||
|
||||
// 3. 收到ACK后,将16字节数据写入FIFO
|
||||
for(i=0; i<16; i++)
|
||||
{
|
||||
SetReg(REG_FIFODATA, data_buff[i]);
|
||||
}
|
||||
// 4. 再次发送,将数据写入卡片
|
||||
SetCommand(CMD_TRANSCEIVE);
|
||||
DelayMs(5);
|
||||
// 5. 卡片会再次返回一个ACK,检查是否收到
|
||||
GetReg(REG_FIFOLENGTH, ®_data);
|
||||
if (reg_data != 1)
|
||||
return FAIL;
|
||||
GetReg(REG_FIFODATA, ®_data);
|
||||
if(reg_data != 0x0A)
|
||||
return FAIL;
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
// ... (值块操作函数的实现: Blockset, Blockinc, Blockdec, Transfer, Restore) ...
|
||||
74
apps/earphone/94_rfid_stc/READER/NTAG.c
Normal file
74
apps/earphone/94_rfid_stc/READER/NTAG.c
Normal file
@ -0,0 +1,74 @@
|
||||
#include "READER.h"
|
||||
#include "NTAG.h"
|
||||
#include <string.h>
|
||||
#include "READER_REG.h"
|
||||
#include "xt_main.h"
|
||||
unsigned char PAGE_DATA[16];
|
||||
|
||||
unsigned char NTAG_EVENT(void)
|
||||
{
|
||||
unsigned char result;
|
||||
memcpy(PAGE_DATA,"\x01\x02\x03\x04",4);
|
||||
result = Write_Page(8,PAGE_DATA);
|
||||
if (result != SUCCESS)
|
||||
return result;
|
||||
printf("PAGE 8 Write OK\r\n");
|
||||
result = Read_Page(8,PAGE_DATA);
|
||||
printf("PAGE 8 = %02X%02X%02X%02X\r\n",PAGE_DATA[0],PAGE_DATA[1],PAGE_DATA[2],PAGE_DATA[3]);
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
unsigned char Read_Page(unsigned char page_num,unsigned char *page_data)
|
||||
{
|
||||
unsigned char reg_data,i;
|
||||
SetCommand(CMD_IDLE);
|
||||
Clear_FIFO();
|
||||
SetReg(REG_FIFODATA,0x30);
|
||||
SetReg(REG_FIFODATA,page_num);
|
||||
|
||||
ModifyReg(REG_TXCRCCON, BIT_CRCEN,ENABLE);
|
||||
ModifyReg(REG_RXCRCCON, BIT_CRCEN,ENABLE);
|
||||
SetCommand(CMD_TRANSCEIVE);
|
||||
DelayMs(5);
|
||||
GetReg(REG_ERROR,®_data);
|
||||
if(reg_data & 0x07)
|
||||
return FAIL;
|
||||
GetReg(REG_FIFOLENGTH,®_data);
|
||||
if(reg_data != 16)
|
||||
return FAIL;
|
||||
for(i=0;i<16;i++)
|
||||
{
|
||||
GetReg(REG_FIFODATA,&page_data[i]);
|
||||
}
|
||||
return SUCCESS;
|
||||
|
||||
}
|
||||
|
||||
unsigned char Write_Page(unsigned char page_num,unsigned char *page_data)
|
||||
{
|
||||
unsigned char reg_data;
|
||||
SetCommand(CMD_IDLE);
|
||||
Clear_FIFO();
|
||||
SetReg(REG_FIFODATA,0xA2);
|
||||
SetReg(REG_FIFODATA,page_num);
|
||||
SetReg(REG_FIFODATA,page_data[0]);
|
||||
SetReg(REG_FIFODATA,page_data[1]);
|
||||
SetReg(REG_FIFODATA,page_data[2]);
|
||||
SetReg(REG_FIFODATA,page_data[3]);
|
||||
ModifyReg(REG_TXCRCCON, BIT_CRCEN,ENABLE);
|
||||
ModifyReg(REG_RXCRCCON, BIT_CRCEN,DISABLE);
|
||||
SetCommand(CMD_TRANSCEIVE);
|
||||
DelayMs(5);
|
||||
|
||||
GetReg(REG_FIFOLENGTH,®_data);
|
||||
if(reg_data != 1)
|
||||
return FAIL;
|
||||
|
||||
GetReg(REG_FIFODATA,®_data);
|
||||
if(reg_data != 0x0A)
|
||||
return FAIL;
|
||||
return SUCCESS;
|
||||
|
||||
}
|
||||
|
||||
8
apps/earphone/94_rfid_stc/READER/NTAG.h
Normal file
8
apps/earphone/94_rfid_stc/READER/NTAG.h
Normal file
@ -0,0 +1,8 @@
|
||||
#ifndef _NTAG_H
|
||||
#define _NTAG_H
|
||||
|
||||
extern unsigned char PAGE_DATA[16];
|
||||
extern unsigned char NTAG_EVENT(void);
|
||||
extern unsigned char Read_Page(unsigned char page_num,unsigned char *page_data);
|
||||
extern unsigned char Write_Page(unsigned char page_num,unsigned char *page_data);
|
||||
#endif
|
||||
814
apps/earphone/94_rfid_stc/READER/READER.c
Normal file
814
apps/earphone/94_rfid_stc/READER/READER.c
Normal file
@ -0,0 +1,814 @@
|
||||
#include "function.h"
|
||||
#include "board.h"
|
||||
//#include "fm15l0xx_ll_spi.h"
|
||||
#include "READER.h"
|
||||
#include "READER_REG.h"
|
||||
#include "print.h"
|
||||
#include "random_generator.h"
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
#include "xt_main.h"
|
||||
|
||||
struct picc_a_struct PICC_A;
|
||||
struct picc_b_struct PICC_B;
|
||||
struct picc_v_struct PICC_V;
|
||||
struct picc_f_struct PICC_F;
|
||||
void DelayMs( uint32_t xms );
|
||||
void DelayUs( uint32_t xus );
|
||||
void DelayUs( uint32_t xus )
|
||||
{
|
||||
int t;
|
||||
|
||||
while ( xus-- )
|
||||
{
|
||||
t = 5;
|
||||
while ( t-- )
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void DelayMs( uint32_t xms )
|
||||
{
|
||||
DelayUs( xms * 1000 );
|
||||
}
|
||||
|
||||
|
||||
// void Reader_GPIO_Init( void )
|
||||
// {
|
||||
/******** 重做GPIO初始化 **********/
|
||||
|
||||
|
||||
// // LL_GPIO_SetPinMode( PD_GPIO, PD_PIN, LL_GPIO_PINxMODE_OUTPUT );//PA7 PD
|
||||
|
||||
|
||||
// //LL_GPIO_EnablePinPullUp( SPI_GPIO, SCK_PIN | MOSI_PIN ); // Enable Pullup
|
||||
|
||||
// //xtell注释
|
||||
// // LL_GPIO_SetPinMode( SPI_GPIO, SCK_PIN, LL_GPIO_PINxMODE_OUTPUT ); // PB1 Digital function - SPI1 SCK
|
||||
// // LL_GPIO_SetPinMode( SPI_GPIO, MISO_PIN, LL_GPIO_PINxMODE_INPUT ); // PB2 Digital function - SPI1 MISO1
|
||||
// // LL_GPIO_SetPinMode( SPI_GPIO, MOSI_PIN, LL_GPIO_PINxMODE_OUTPUT ); // PB3 Digital function - SPI1 MOSI
|
||||
// LL_GPIO_SetPinMode(); //xtell
|
||||
// SCK_0;
|
||||
// MOSI_0;
|
||||
//
|
||||
// LL_GPIO_SetPinMode(); //xtell
|
||||
// //xtell注释
|
||||
// // LL_GPIO_SetPinMode( SPI_GPIO, NSS_PIN, LL_GPIO_PINxMODE_OUTPUT ); // PB0 Digital function - SPI1 NSS1
|
||||
// PD_0;
|
||||
// NSS_1;
|
||||
// }
|
||||
|
||||
// unsigned char FM176XX_HardReset(void)
|
||||
// {
|
||||
|
||||
/*************NFC 硬件初始化 重做********************/
|
||||
|
||||
// unsigned char reg_data;
|
||||
// NSS_1;//NSS = 1
|
||||
// PD_1;//RST = 1
|
||||
// DelayMs(1);
|
||||
// PD_0;//RST = 0
|
||||
// DelayMs(1);
|
||||
// GetReg(REG_COMMAND,®_data);
|
||||
// if (reg_data != 0x40)
|
||||
// return FAIL;
|
||||
// return SUCCESS;
|
||||
// }
|
||||
|
||||
// unsigned char Reader_Set_HPD( unsigned char mode ) //mode = DISABLE <20>˳<EFBFBD>HPDģʽ <20><>mode = ENABLE <20><><EFBFBD><EFBFBD>HPDģʽ
|
||||
// {
|
||||
// if ( mode == ENABLE )
|
||||
// {
|
||||
// DelayMs( 1 ); //<2F><>ʱ1ms
|
||||
|
||||
// /******设置一个GPIO输出高*******xtell******/
|
||||
// //PD_1; // PD = 1
|
||||
// P34 = 1;
|
||||
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// /******设置一个GPIO输出低*************/
|
||||
// //PD_0; //PD = 0
|
||||
// P34 =0;
|
||||
// DelayMs( 1 ); //<2F><>ʱ1ms<6D><73><EFBFBD>ȴ<EFBFBD>Reader<65><72><EFBFBD><EFBFBD>
|
||||
// }
|
||||
// return (mode);
|
||||
// }
|
||||
|
||||
|
||||
|
||||
|
||||
//***********************************************
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƣ<EFBFBD>GetReg(unsigned char addr,unsigned char *regdata)
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܣ<EFBFBD><DCA3><EFBFBD>ȡ<EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD>ֵ
|
||||
//<2F><>ڲ<EFBFBD><DAB2><EFBFBD><EFBFBD><EFBFBD>addr:Ŀ<><C4BF>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD>ַ regdata:<3A><>ȡ<EFBFBD><C8A1>ֵ
|
||||
//<2F><><EFBFBD>ڲ<EFBFBD><DAB2><EFBFBD><EFBFBD><EFBFBD>unsigned char TRUE<55><45><EFBFBD><EFBFBD>ȡ<EFBFBD>ɹ<EFBFBD> FALSE:ʧ<><CAA7>
|
||||
//***********************************************
|
||||
|
||||
// 从FM17660 读取数据时,BIT7 置 1
|
||||
// unsigned char GetReg(unsigned char address,unsigned char *reg_data)
|
||||
// {
|
||||
// unsigned char spi_data,i;
|
||||
// NSS_0; //NSS = 0;
|
||||
// spi_data = (address << 1) | 0x01;
|
||||
|
||||
// for(i=0;i<8;i++)
|
||||
// {
|
||||
// if(spi_data & 0x80)
|
||||
// MOSI_1;
|
||||
// else
|
||||
// MOSI_0;
|
||||
// SCK_1;
|
||||
// spi_data = spi_data<<1;
|
||||
// SCK_0;
|
||||
// }
|
||||
|
||||
// MOSI_0;
|
||||
// *reg_data = 0;
|
||||
// for(i=0;i<8;i++)
|
||||
// {
|
||||
// *reg_data = *reg_data<<1;
|
||||
// SCK_1;
|
||||
|
||||
// if(LL_GPIO_ReadInputPort(SPI_GPIO)& MISO_PIN)
|
||||
// *reg_data = *reg_data | 0x01;
|
||||
// SCK_0;
|
||||
// }
|
||||
|
||||
// NSS_1; //NSS = 1;
|
||||
// return SUCCESS;
|
||||
// }
|
||||
|
||||
//***********************************************
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƣ<EFBFBD>SetReg(unsigned char addr,unsigned char* regdata)
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܣ<EFBFBD>д<EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD>
|
||||
//<2F><>ڲ<EFBFBD><DAB2><EFBFBD><EFBFBD><EFBFBD>addr:Ŀ<><C4BF>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD>ַ regdata:Ҫд<D2AA><D0B4><EFBFBD>ֵ
|
||||
//<2F><><EFBFBD>ڲ<EFBFBD><DAB2><EFBFBD><EFBFBD><EFBFBD>unsigned char TRUE<55><45>д<EFBFBD>ɹ<EFBFBD> FALSE:дʧ<D0B4><CAA7>
|
||||
//***********************************************
|
||||
// unsigned char SetReg(unsigned char address,unsigned char reg_data)
|
||||
// {
|
||||
// unsigned char spi_data,i;
|
||||
// NSS_0; //NSS = 0;
|
||||
// spi_data = (address << 1) & 0xFE;
|
||||
|
||||
// for(i=0;i<8;i++)
|
||||
// {
|
||||
|
||||
// if(spi_data & 0x80)
|
||||
// MOSI_1;
|
||||
// else
|
||||
// MOSI_0;
|
||||
// SCK_1;
|
||||
// spi_data = spi_data<<1;
|
||||
// SCK_0;
|
||||
// }
|
||||
// MOSI_0;
|
||||
// spi_data = reg_data;
|
||||
// for(i=0;i<8;i++)
|
||||
// {
|
||||
// if(spi_data & 0x80)
|
||||
// MOSI_1;
|
||||
// else
|
||||
// MOSI_0;
|
||||
// SCK_1;
|
||||
// spi_data = spi_data<<1;
|
||||
// SCK_0;
|
||||
// }
|
||||
// SCK_0;
|
||||
// MOSI_0;
|
||||
// NSS_1; //NSS = 1;
|
||||
|
||||
// return SUCCESS;
|
||||
// }
|
||||
|
||||
void ModifyReg( unsigned char reg_address, unsigned char mask, unsigned char set )
|
||||
{
|
||||
unsigned char reg_data;
|
||||
// Uart1SendString(" ModifyReg begin ");
|
||||
|
||||
GetReg( reg_address, ®_data );
|
||||
|
||||
if ( set )
|
||||
{
|
||||
reg_data |= mask;
|
||||
}
|
||||
else
|
||||
{
|
||||
reg_data &= ~mask;
|
||||
}
|
||||
|
||||
SetReg( reg_address, reg_data );
|
||||
return;
|
||||
}
|
||||
|
||||
unsigned char SetCommand(unsigned char command)
|
||||
{
|
||||
unsigned char result;
|
||||
result = SetReg(REG_COMMAND,CMD_MASK & command);
|
||||
return result;
|
||||
}
|
||||
|
||||
void SetTimer(unsigned int timeout) //
|
||||
{
|
||||
unsigned long prescale = 1;
|
||||
unsigned long t,fc;
|
||||
fc = timeout*13560;
|
||||
t = fc;
|
||||
|
||||
while(fc > 65535)
|
||||
{
|
||||
prescale*=2;
|
||||
fc = t/prescale;
|
||||
if(fc*prescale != t)
|
||||
fc++;
|
||||
}
|
||||
|
||||
if(prescale>1)
|
||||
{
|
||||
SetReg(REG_T0CONTROL, BIT_TSTOP_RX | BIT_TSTART_TX | BIT_TAUTORESTARTED | VALUE_TCLK_1356_MHZ );
|
||||
SetReg(REG_T0RELOADHI,(u8)(fc>>8));
|
||||
SetReg(REG_T0RELOADLO,(u8)fc);
|
||||
|
||||
SetReg(REG_T1CONTROL, BIT_TSTOP_RX | BIT_TSTART_TX | VALUE_TCLK_T0 );
|
||||
SetReg(REG_T1RELOADHI,(u8)(prescale>>8));
|
||||
SetReg(REG_T1RELOADLO,(u8)prescale);
|
||||
}
|
||||
else
|
||||
{
|
||||
SetReg(REG_T1CONTROL, BIT_TSTOP_RX | BIT_TSTART_TX | VALUE_TCLK_1356_MHZ );
|
||||
SetReg(REG_T1RELOADHI,(u8)(fc>>8));
|
||||
SetReg(REG_T1RELOADLO,(u8)fc);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
unsigned char SetCW(unsigned char mode)
|
||||
{
|
||||
unsigned char result;
|
||||
if(mode == ENABLE)
|
||||
{
|
||||
ModifyReg(REG_COMMAND,BIT_MODEMOFF,DISABLE);
|
||||
ModifyReg(REG_TXMODE,BIT0 | BIT1,ENABLE);
|
||||
}
|
||||
else
|
||||
{
|
||||
ModifyReg(REG_COMMAND,BIT_MODEMOFF,ENABLE);
|
||||
ModifyReg(REG_TXMODE,BIT0 | BIT1,DISABLE);
|
||||
}
|
||||
DelayMs(5);
|
||||
return result;
|
||||
}
|
||||
|
||||
void Clear_FIFO(void)
|
||||
{
|
||||
unsigned char fifolength;
|
||||
|
||||
GetReg(REG_FIFOLENGTH,&fifolength);
|
||||
if((fifolength) != 0) //FIFO<46><4F><EFBFBD><EFBFBD><EFBFBD><EFBFBD>գ<EFBFBD><D5A3><EFBFBD>FLUSH FIFO
|
||||
{
|
||||
ModifyReg(REG_FIFOCONTROL,BIT_FIFOFLUSH,ENABLE);
|
||||
}
|
||||
return ;
|
||||
}
|
||||
|
||||
unsigned char LoadProtocol(unsigned char p_rx,unsigned char p_tx)
|
||||
{
|
||||
unsigned char reg_data = 0;
|
||||
// Uart1SendString(" LoadProtocol begin ");
|
||||
SetCommand(CMD_IDLE); //
|
||||
ModifyReg(REG_FIFOCONTROL,BIT_FIFOFLUSH,ENABLE); //Clear FIFO
|
||||
SetReg(REG_FIFODATA,p_rx);//Rx
|
||||
SetReg(REG_FIFODATA,p_tx);//Tx
|
||||
|
||||
SetCommand(CMD_LOADPROTOCOL);
|
||||
DelayMs(2);
|
||||
GetReg(REG_COMMAND,®_data);
|
||||
if(reg_data != CMD_IDLE)
|
||||
return FAIL;
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
void SetParity(unsigned char state)
|
||||
{
|
||||
// Uart1SendString(" SetParity begin");
|
||||
ModifyReg(REG_FRAMECON,BIT_TXPARITYEN|BIT_RXPARITYEN,state);
|
||||
}
|
||||
|
||||
unsigned char ReaderA_Initial(void)
|
||||
{
|
||||
|
||||
// Uart1SendString(" ReaderA_Initial begin");
|
||||
LoadProtocol(RX_TYPEA_106,TX_TYPEA_106);
|
||||
ModifyReg(REG_TXMODE,BIT2,ENABLE);//FORCE 100ask ENABLE
|
||||
SetReg(REG_TXAMP,AMPLITUDE_A);
|
||||
SetReg(REG_TXCON,0x00);
|
||||
SetReg(REG_RXANA,(HPCF_A<<3)|GAIN_A);
|
||||
SetReg(0x5F,0x08);
|
||||
SetReg(REG_THNSET,0xFF);
|
||||
SetReg(REG_THNMIN,0xC0);
|
||||
SetReg(REG_RXTXCON,0x80);//
|
||||
SetParity(ENABLE);
|
||||
SetReg(REG_STATUS,0);//<2F><><EFBFBD>Cry1Onλ
|
||||
|
||||
// Uart1SendString(" ReaderA_Initial end");
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
unsigned char ReaderB_Initial(void)
|
||||
{
|
||||
LoadProtocol(RX_TYPEB_106,TX_TYPEB_106);
|
||||
|
||||
ModifyReg(REG_TXMODE,BIT2,DISABLE);//FORCE 100ask DISABLE
|
||||
SetReg(REG_TXAMP,AMPLITUDE_B);
|
||||
SetReg(REG_TXCON,MODULATION_B);
|
||||
SetReg(REG_RXANA,(HPCF_B<<3)|GAIN_B);
|
||||
SetReg(0x5F,0x08);
|
||||
SetReg(REG_THNSET,0xFF);
|
||||
SetReg(REG_THNMIN,0xC0);
|
||||
SetReg(REG_RXTXCON,0x80);//
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
unsigned char ReaderV_Initial(void)
|
||||
{
|
||||
LoadProtocol(RX_TYPEV_26,RX_TYPEV_26);
|
||||
ModifyReg(REG_RXANA,BIT3|BIT2|BIT1|BIT0,DISABLE);
|
||||
ModifyReg(REG_RXANA,(HPCF_V<<3)|GAIN_V,ENABLE);//39h
|
||||
SetParity(DISABLE);
|
||||
SetReg(REG_TXAMP,AMPLITUDE_V);
|
||||
SetReg(REG_TXCON,MODULATION_V);
|
||||
SetReg(REG_TXI,0x06);
|
||||
SetReg(REG_THNSET,0xFF);
|
||||
SetReg(REG_THNMIN,0x80);
|
||||
SetReg(REG_THNADJ,0x08);
|
||||
SetReg(REG_RXTXCON,0);
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
unsigned char ReaderF_Initial(void)
|
||||
{
|
||||
ModifyReg(REG_MISC, 0x04,ENABLE);
|
||||
LoadProtocol(RX_FELICA_212,TX_FELICA_212);
|
||||
SetReg(REG_TXAMP,AMPLITUDE_F); //
|
||||
SetReg(REG_TXCON,MODULATION_F);
|
||||
ModifyReg(REG_RXANA,BIT3|BIT2|BIT1|BIT0,DISABLE);
|
||||
ModifyReg(REG_RXANA,(HPCF_F<<3)|GAIN_F,ENABLE);//39h
|
||||
SetParity(DISABLE);
|
||||
SetReg(REG_THNSET,0xFF);
|
||||
SetReg(REG_THNMIN,0x80);
|
||||
SetReg(REG_THNADJ,0x08);
|
||||
ModifyReg(REG_MISC, 0x04,DISABLE);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
unsigned char ReaderA_Wakeeup(struct picc_a_struct *picc_a)
|
||||
{
|
||||
unsigned char reg_data;
|
||||
SetCommand(CMD_IDLE);
|
||||
SetReg(REG_TXDATANUM,0x0F);
|
||||
ModifyReg(REG_FIFOCONTROL,BIT_FIFOFLUSH,ENABLE); //Clear FIFO
|
||||
SetReg(REG_FIFODATA,RF_CMD_WUPA);
|
||||
ModifyReg(REG_TXCRCCON, BIT_CRCEN,DISABLE);
|
||||
ModifyReg(REG_RXCRCCON, BIT_CRCEN,DISABLE);
|
||||
SetCommand(CMD_TRANSCEIVE);
|
||||
DelayMs(2);
|
||||
GetReg(REG_FIFOLENGTH,®_data);
|
||||
if(reg_data != 2)
|
||||
return FAIL;
|
||||
|
||||
GetReg(REG_FIFODATA,&picc_a->ATQA[0]);
|
||||
GetReg(REG_FIFODATA,&picc_a->ATQA[1]);
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
unsigned char ReaderA_Request(struct picc_a_struct *picc_a)
|
||||
{
|
||||
unsigned char reg_data;
|
||||
SetCommand(CMD_IDLE);
|
||||
SetReg(REG_TXDATANUM,0x0F);
|
||||
ModifyReg(REG_FIFOCONTROL,BIT_FIFOFLUSH,ENABLE); //Clear FIFO
|
||||
SetReg(REG_FIFODATA,RF_CMD_REQA);
|
||||
ModifyReg(REG_TXCRCCON, BIT_CRCEN,DISABLE);
|
||||
ModifyReg(REG_RXCRCCON, BIT_CRCEN,DISABLE);
|
||||
SetCommand(CMD_TRANSCEIVE);
|
||||
// DelayMs(2);
|
||||
Delay1ms();
|
||||
Delay1ms();
|
||||
GetReg(REG_FIFOLENGTH,®_data);
|
||||
// Uart1SendString("REG_FIFOLENGTH data=");
|
||||
// printHex(reg_data);
|
||||
if(reg_data != 2)
|
||||
return FAIL;
|
||||
|
||||
GetReg(REG_FIFODATA,&picc_a->ATQA[0]);
|
||||
GetReg(REG_FIFODATA,&picc_a->ATQA[1]);
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
unsigned char ReaderA_Anticoll(struct picc_a_struct *picc_a)
|
||||
{
|
||||
unsigned char reg_data;
|
||||
SetCommand(CMD_IDLE);
|
||||
SetReg(REG_TXDATANUM,0x08);
|
||||
ModifyReg(REG_FIFOCONTROL,BIT_FIFOFLUSH,ENABLE); //Clear FIFO
|
||||
SetReg(REG_FIFODATA,RF_CMD_ANTICOLL[picc_a->CASCADE_LEVEL]);
|
||||
SetReg(REG_FIFODATA,0x20);
|
||||
ModifyReg(REG_TXCRCCON, BIT_CRCEN,DISABLE);
|
||||
ModifyReg(REG_RXCRCCON, BIT_CRCEN,DISABLE);
|
||||
SetCommand(CMD_TRANSCEIVE);
|
||||
DelayMs(2);
|
||||
GetReg(REG_FIFOLENGTH,®_data);
|
||||
if(reg_data != 5)
|
||||
return FAIL;
|
||||
|
||||
GetReg(REG_FIFODATA,&picc_a->UID[picc_a->CASCADE_LEVEL*4]);
|
||||
GetReg(REG_FIFODATA,&picc_a->UID[picc_a->CASCADE_LEVEL*4+1]);
|
||||
GetReg(REG_FIFODATA,&picc_a->UID[picc_a->CASCADE_LEVEL*4+2]);
|
||||
GetReg(REG_FIFODATA,&picc_a->UID[picc_a->CASCADE_LEVEL*4+3]);
|
||||
GetReg(REG_FIFODATA,&picc_a->BCC[picc_a->CASCADE_LEVEL]);
|
||||
if( (picc_a->UID[picc_a->CASCADE_LEVEL*4] ^ picc_a->UID[picc_a->CASCADE_LEVEL*4+1] ^ picc_a->UID[picc_a->CASCADE_LEVEL*4+2] ^ picc_a->UID[picc_a->CASCADE_LEVEL*4+3]) == picc_a->BCC[picc_a->CASCADE_LEVEL])
|
||||
return SUCCESS;
|
||||
|
||||
return FAIL;
|
||||
}
|
||||
|
||||
unsigned char ReaderA_Select(struct picc_a_struct *picc_a)
|
||||
{
|
||||
unsigned char reg_data;
|
||||
SetCommand(CMD_IDLE);
|
||||
SetReg(REG_TXDATANUM,0x08);
|
||||
ModifyReg(REG_FIFOCONTROL,BIT_FIFOFLUSH,ENABLE); //Clear FIFO
|
||||
SetReg(REG_FIFODATA,RF_CMD_ANTICOLL[picc_a->CASCADE_LEVEL]);
|
||||
SetReg(REG_FIFODATA,0x70);
|
||||
SetReg(REG_FIFODATA,picc_a->UID[picc_a->CASCADE_LEVEL*4]);
|
||||
SetReg(REG_FIFODATA,picc_a->UID[picc_a->CASCADE_LEVEL*4+1]);
|
||||
SetReg(REG_FIFODATA,picc_a->UID[picc_a->CASCADE_LEVEL*4+2]);
|
||||
SetReg(REG_FIFODATA,picc_a->UID[picc_a->CASCADE_LEVEL*4+3]);
|
||||
SetReg(REG_FIFODATA,picc_a->BCC[picc_a->CASCADE_LEVEL]);
|
||||
ModifyReg(REG_TXCRCCON, BIT_CRCEN,ENABLE);
|
||||
ModifyReg(REG_RXCRCCON, BIT_CRCEN,ENABLE);
|
||||
SetCommand(CMD_TRANSCEIVE);
|
||||
DelayMs(2);
|
||||
GetReg(REG_ERROR,®_data);
|
||||
if((reg_data & 0x0F)!=0)
|
||||
return FAIL;
|
||||
GetReg(REG_FIFOLENGTH,®_data);
|
||||
if(reg_data != 1)
|
||||
return FAIL;
|
||||
GetReg(REG_FIFODATA,&picc_a->SAK [picc_a->CASCADE_LEVEL]);
|
||||
return SUCCESS;
|
||||
|
||||
}
|
||||
|
||||
|
||||
unsigned char ReaderA_CardActivate(struct picc_a_struct *picc_a)
|
||||
{
|
||||
unsigned char result,cascade_level;
|
||||
result = ReaderA_Request(picc_a);//
|
||||
if (result != SUCCESS)
|
||||
return result;
|
||||
|
||||
if ((picc_a->ATQA[0]&0xC0)==0x00) //1<><31>UID
|
||||
{
|
||||
cascade_level = 1;
|
||||
picc_a->UID_Length = 4;
|
||||
}
|
||||
if ((picc_a->ATQA[0]&0xC0)==0x40) //2<><32>UID
|
||||
{
|
||||
cascade_level = 2;
|
||||
picc_a->UID_Length = 8;
|
||||
}
|
||||
if ((picc_a->ATQA[0]&0xC0)==0x80) //3<><33>UID
|
||||
{
|
||||
cascade_level = 3;
|
||||
picc_a->UID_Length = 12;
|
||||
}
|
||||
for (picc_a->CASCADE_LEVEL = 0; picc_a->CASCADE_LEVEL < cascade_level; picc_a->CASCADE_LEVEL++)
|
||||
{
|
||||
result = ReaderA_Anticoll(picc_a);//
|
||||
if (result != SUCCESS)
|
||||
return result;
|
||||
|
||||
result = ReaderA_Select(picc_a);//
|
||||
if (result != SUCCESS)
|
||||
return result;
|
||||
}
|
||||
picc_a->CASCADE_LEVEL--;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
unsigned char ReaderB_Wakeup(struct picc_b_struct *picc_b)
|
||||
{
|
||||
unsigned char reg_data,i;
|
||||
SetCommand(CMD_IDLE);
|
||||
SetReg(REG_TXDATANUM,0x08);
|
||||
ModifyReg(REG_FIFOCONTROL,BIT_FIFOFLUSH,ENABLE); //Clear FIFO
|
||||
SetReg(REG_FIFODATA, 0x05); //APf
|
||||
SetReg(REG_FIFODATA, 0x00); //AFI (00:for all cards)
|
||||
SetReg(REG_FIFODATA, 0x08); //PARAM(REQB,Number of slots =0)
|
||||
ModifyReg(REG_TXCRCCON, BIT_CRCEN,ENABLE);
|
||||
ModifyReg(REG_RXCRCCON, BIT_CRCEN,ENABLE);
|
||||
SetCommand(CMD_TRANSCEIVE);
|
||||
DelayMs(10);
|
||||
GetReg(REG_ERROR,®_data);
|
||||
if((reg_data & 0x0F)!=0)//<2F>жϴ<D0B6><CFB4><EFBFBD><EFBFBD>־
|
||||
return FAIL;
|
||||
GetReg(REG_FIFOLENGTH,®_data);
|
||||
if(reg_data != 12)//<2F>жϽ<D0B6><CFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>
|
||||
return FAIL;
|
||||
for(i=0;i<12;i++)
|
||||
GetReg(REG_FIFODATA,&picc_b->ATQB [i]);
|
||||
memcpy(picc_b->PUPI,picc_b->ATQB + 1,4);
|
||||
memcpy(picc_b->APPLICATION_DATA,picc_b->ATQB + 6,4);
|
||||
memcpy(picc_b->PROTOCOL_INF,picc_b->ATQB + 10,3);
|
||||
|
||||
return SUCCESS;
|
||||
|
||||
}
|
||||
|
||||
unsigned char ReaderB_Request(struct picc_b_struct *picc_b)
|
||||
{
|
||||
unsigned char reg_data,i;
|
||||
SetCommand(CMD_IDLE);
|
||||
SetReg(REG_TXDATANUM,0x08);
|
||||
ModifyReg(REG_FIFOCONTROL,BIT_FIFOFLUSH,ENABLE); //Clear FIFO
|
||||
SetReg(REG_FIFODATA, 0x05); //APf
|
||||
SetReg(REG_FIFODATA, 0x00); //AFI (00:for all cards)
|
||||
SetReg(REG_FIFODATA, 0x00); //PARAM(REQB,Number of slots =0)
|
||||
ModifyReg(REG_TXCRCCON, BIT_CRCEN,ENABLE);
|
||||
ModifyReg(REG_RXCRCCON, BIT_CRCEN,ENABLE);
|
||||
SetCommand(CMD_TRANSCEIVE);
|
||||
DelayMs(10);
|
||||
GetReg(REG_ERROR,®_data);
|
||||
if((reg_data & 0x0F)!=0)
|
||||
return FAIL;
|
||||
GetReg(REG_FIFOLENGTH,®_data);
|
||||
if(reg_data != 12)
|
||||
return FAIL;
|
||||
for(i=0;i<12;i++)
|
||||
GetReg(REG_FIFODATA,&picc_b->ATQB [i]);
|
||||
memcpy(picc_b->PUPI,picc_b->ATQB + 1,4);
|
||||
memcpy(picc_b->APPLICATION_DATA,picc_b->ATQB + 6,4);
|
||||
memcpy(picc_b->PROTOCOL_INF,picc_b->ATQB + 10,3);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
unsigned char ReaderB_Attrib(struct picc_b_struct *picc_b)
|
||||
{
|
||||
unsigned char reg_data;
|
||||
SetCommand(CMD_IDLE);
|
||||
SetReg(REG_TXDATANUM,0x08);
|
||||
ModifyReg(REG_FIFOCONTROL,BIT_FIFOFLUSH,ENABLE); //Clear FIFO
|
||||
SetReg(REG_FIFODATA, 0x1D); //
|
||||
SetReg(REG_FIFODATA, picc_b->PUPI[0]); //
|
||||
SetReg(REG_FIFODATA, picc_b->PUPI[1]); //
|
||||
SetReg(REG_FIFODATA, picc_b->PUPI[2]); //
|
||||
SetReg(REG_FIFODATA, picc_b->PUPI[3]); //
|
||||
SetReg(REG_FIFODATA, 0x00); //Param1
|
||||
SetReg(REG_FIFODATA, 0x08); //Param2 BIT0~BIT3 Frame Size 0 = 16, 1 = 24, 2 = 32, 3 = 40, 4 = 48, 5 = 64, 6 = 96, 7 = 128, 8 = 256
|
||||
//Param2 BIT4~BIT5 TX BaudRate BIT6~BIT7 RX BaudRate,00 = 106Kbps, 01 = 212Kbps, 10 = 424Kbps, 11 = 848Kbps
|
||||
SetReg(REG_FIFODATA, 0x01); //COMPATIBLE WITH 14443-4
|
||||
SetReg(REG_FIFODATA, 0x01); //CID:01
|
||||
ModifyReg(REG_TXCRCCON, BIT_CRCEN,ENABLE);
|
||||
ModifyReg(REG_RXCRCCON, BIT_CRCEN,ENABLE);
|
||||
SetCommand(CMD_TRANSCEIVE);
|
||||
DelayMs(10);
|
||||
GetReg(REG_ERROR,®_data);
|
||||
if((reg_data & 0x0F)!=0)
|
||||
return FAIL;
|
||||
GetReg(REG_FIFOLENGTH,®_data);
|
||||
if(reg_data != 1)
|
||||
return FAIL;
|
||||
GetReg(REG_FIFODATA,®_data);
|
||||
picc_b->CID = reg_data & 0x0F;
|
||||
|
||||
return SUCCESS;
|
||||
|
||||
}
|
||||
|
||||
unsigned char ReaderB_Halt(struct picc_b_struct *picc_b)
|
||||
{
|
||||
unsigned char reg_data;
|
||||
SetCommand(CMD_IDLE);
|
||||
SetReg(REG_TXDATANUM,0x08);
|
||||
ModifyReg(REG_FIFOCONTROL,BIT_FIFOFLUSH,ENABLE); //Clear FIFO
|
||||
SetReg(REG_FIFODATA, 0x50); //
|
||||
SetReg(REG_FIFODATA, picc_b->PUPI[0]); //
|
||||
SetReg(REG_FIFODATA, picc_b->PUPI[1]); //
|
||||
SetReg(REG_FIFODATA, picc_b->PUPI[2]); //
|
||||
SetReg(REG_FIFODATA, picc_b->PUPI[3]); //
|
||||
|
||||
ModifyReg(REG_TXCRCCON, BIT_CRCEN,ENABLE);
|
||||
ModifyReg(REG_RXCRCCON, BIT_CRCEN,ENABLE);
|
||||
SetCommand(CMD_TRANSCEIVE);
|
||||
DelayMs(10);
|
||||
GetReg(REG_ERROR,®_data);
|
||||
if((reg_data & 0x0F)!=0)
|
||||
return FAIL;
|
||||
GetReg(REG_FIFOLENGTH,®_data);
|
||||
if(reg_data != 1)
|
||||
return FAIL;
|
||||
GetReg(REG_FIFODATA,®_data);
|
||||
*picc_b->Answer_to_HALT = reg_data & 0x0F;
|
||||
|
||||
return SUCCESS;
|
||||
|
||||
}
|
||||
|
||||
unsigned char ReaderB_Get_SN(struct picc_b_struct *picc_b)
|
||||
{
|
||||
unsigned char reg_data,i;
|
||||
SetCommand(CMD_IDLE);
|
||||
SetReg(REG_TXDATANUM,0x08);
|
||||
ModifyReg(REG_FIFOCONTROL,BIT_FIFOFLUSH,ENABLE); //Clear FIFO
|
||||
SetReg(REG_FIFODATA, 0x00); //
|
||||
SetReg(REG_FIFODATA, 0x36); //
|
||||
SetReg(REG_FIFODATA, 0x00); //
|
||||
SetReg(REG_FIFODATA, 0x00); //
|
||||
SetReg(REG_FIFODATA, 0x08); //
|
||||
|
||||
ModifyReg(REG_TXCRCCON, BIT_CRCEN,ENABLE);
|
||||
ModifyReg(REG_RXCRCCON, BIT_CRCEN,ENABLE);
|
||||
SetCommand(CMD_TRANSCEIVE);
|
||||
DelayMs(10);
|
||||
GetReg(REG_ERROR,®_data);
|
||||
if((reg_data & 0x0F)!=0)
|
||||
return FAIL;
|
||||
GetReg(REG_FIFOLENGTH,®_data);
|
||||
if(reg_data != 10)
|
||||
return FAIL;
|
||||
for(i=0;i<8;i++)
|
||||
GetReg(REG_FIFODATA,&picc_b->SN[i]);
|
||||
|
||||
return SUCCESS;
|
||||
|
||||
}
|
||||
|
||||
unsigned char ReaderV_Inventory(struct picc_v_struct *picc_v)
|
||||
{
|
||||
unsigned char reg_data,i;
|
||||
SetCommand(CMD_IDLE);
|
||||
SetReg(REG_TXDATANUM,0x08);
|
||||
ModifyReg(REG_FIFOCONTROL,BIT_FIFOFLUSH,ENABLE); //Clear FIFO
|
||||
SetReg(REG_FIFODATA, 0x26); //
|
||||
SetReg(REG_FIFODATA, 0x01);
|
||||
SetReg(REG_FIFODATA, 0x00);
|
||||
|
||||
ModifyReg(REG_TXCRCCON, BIT_CRCEN,ENABLE);
|
||||
ModifyReg(REG_RXCRCCON, BIT_CRCEN,ENABLE);
|
||||
|
||||
SetCommand(CMD_TRANSCEIVE);
|
||||
DelayMs(10);
|
||||
GetReg(REG_ERROR,®_data);
|
||||
if((reg_data & 0x0F)!=0)
|
||||
return FAIL;
|
||||
GetReg(REG_FIFOLENGTH,®_data);
|
||||
if(reg_data != 10)
|
||||
return FAIL;
|
||||
GetReg(REG_FIFODATA,&picc_v->RESPONSE );
|
||||
GetReg(REG_FIFODATA,®_data);
|
||||
for(i = 0;i < 8; i++)
|
||||
{
|
||||
GetReg(REG_FIFODATA,&picc_v->UID[i]);
|
||||
}
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
unsigned char ReaderV_Select(struct picc_v_struct *picc_v)
|
||||
{
|
||||
unsigned char reg_data;
|
||||
SetCommand(CMD_IDLE);
|
||||
SetReg(REG_TXDATANUM,0x08);
|
||||
ModifyReg(REG_FIFOCONTROL,BIT_FIFOFLUSH,ENABLE); //Clear FIFO
|
||||
SetReg(REG_FIFODATA, 0x22);
|
||||
SetReg(REG_FIFODATA, 0x25); //
|
||||
SetReg(REG_FIFODATA, picc_v->UID[0]);
|
||||
SetReg(REG_FIFODATA, picc_v->UID[1]);
|
||||
SetReg(REG_FIFODATA, picc_v->UID[2]);
|
||||
SetReg(REG_FIFODATA, picc_v->UID[3]);
|
||||
SetReg(REG_FIFODATA, picc_v->UID[4]);
|
||||
SetReg(REG_FIFODATA, picc_v->UID[5]);
|
||||
SetReg(REG_FIFODATA, picc_v->UID[6]);
|
||||
SetReg(REG_FIFODATA, picc_v->UID[7]);
|
||||
ModifyReg(REG_TXCRCCON, BIT_CRCEN,ENABLE);
|
||||
ModifyReg(REG_RXCRCCON, BIT_CRCEN,ENABLE);
|
||||
|
||||
SetCommand(CMD_TRANSCEIVE);
|
||||
DelayMs(10);
|
||||
GetReg(REG_ERROR,®_data);
|
||||
if((reg_data & 0x0F)!=0)
|
||||
return FAIL;
|
||||
GetReg(REG_FIFOLENGTH,®_data);
|
||||
if(reg_data != 1)
|
||||
return FAIL;
|
||||
GetReg(REG_FIFODATA,&picc_v->RESPONSE );
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
unsigned char ReaderV_ReadSingleBlock(unsigned char block_num,struct picc_v_struct *picc_v)
|
||||
{
|
||||
unsigned char reg_data,i;
|
||||
SetCommand(CMD_IDLE);
|
||||
SetReg(REG_TXDATANUM,0x08);
|
||||
ModifyReg(REG_FIFOCONTROL,BIT_FIFOFLUSH,ENABLE); //Clear FIFO
|
||||
SetReg(REG_FIFODATA,0x12);
|
||||
SetReg(REG_FIFODATA, 0x20); //
|
||||
SetReg(REG_FIFODATA, block_num);
|
||||
|
||||
ModifyReg(REG_TXCRCCON, BIT_CRCEN,ENABLE);
|
||||
ModifyReg(REG_RXCRCCON, BIT_CRCEN,ENABLE);
|
||||
|
||||
SetCommand(CMD_TRANSCEIVE);
|
||||
DelayMs(10);
|
||||
GetReg(REG_ERROR,®_data);
|
||||
if((reg_data & 0x0F)!=0)
|
||||
return FAIL;
|
||||
GetReg(REG_FIFOLENGTH,®_data);
|
||||
if(reg_data != 5)
|
||||
return FAIL;
|
||||
GetReg(REG_FIFODATA,&picc_v->RESPONSE );
|
||||
for(i = 0;i < 4; i++)
|
||||
{
|
||||
GetReg(REG_FIFODATA,&picc_v->BLOCK_DATA[i]);
|
||||
}
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
|
||||
unsigned char ReaderV_WriteSingleBlock(unsigned char block_num,struct picc_v_struct *picc_v)
|
||||
{
|
||||
unsigned char reg_data;
|
||||
SetCommand(CMD_IDLE);
|
||||
SetReg(REG_TXDATANUM,0x08);
|
||||
ModifyReg(REG_FIFOCONTROL,BIT_FIFOFLUSH,ENABLE); //Clear FIFO
|
||||
SetReg(REG_FIFODATA,0x02);
|
||||
SetReg(REG_FIFODATA, 0x21); //
|
||||
SetReg(REG_FIFODATA, block_num);
|
||||
SetReg(REG_FIFODATA, picc_v->BLOCK_DATA[0]);
|
||||
SetReg(REG_FIFODATA, picc_v->BLOCK_DATA[1]);
|
||||
SetReg(REG_FIFODATA, picc_v->BLOCK_DATA[2]);
|
||||
SetReg(REG_FIFODATA, picc_v->BLOCK_DATA[3]);
|
||||
ModifyReg(REG_TXCRCCON, BIT_CRCEN,ENABLE);
|
||||
ModifyReg(REG_RXCRCCON, BIT_CRCEN,ENABLE);
|
||||
|
||||
SetCommand(CMD_TRANSCEIVE);
|
||||
DelayMs(10);
|
||||
GetReg(REG_ERROR,®_data);
|
||||
if((reg_data & 0x0F)!=0)
|
||||
return FAIL;
|
||||
GetReg(REG_FIFOLENGTH,®_data);
|
||||
if(reg_data != 1)
|
||||
return FAIL;
|
||||
GetReg(REG_FIFODATA,&picc_v->RESPONSE );
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
unsigned char ReaderF_Inventory(struct picc_f_struct *picc_f)
|
||||
{
|
||||
unsigned char reg_data,i;
|
||||
SetCommand(CMD_IDLE);
|
||||
SetReg(REG_TXDATANUM,0x08);
|
||||
ModifyReg(REG_FIFOCONTROL,BIT_FIFOFLUSH,ENABLE); //Clear FIFO
|
||||
|
||||
SetReg(REG_FIFODATA, 0x06);
|
||||
SetReg(REG_FIFODATA, 0x00); //
|
||||
SetReg(REG_FIFODATA, 0xFF);
|
||||
SetReg(REG_FIFODATA, 0xFF);
|
||||
SetReg(REG_FIFODATA, 0x10);
|
||||
SetReg(REG_FIFODATA, 0x00);
|
||||
|
||||
ModifyReg(REG_TXCRCCON, BIT_CRCEN,ENABLE);
|
||||
ModifyReg(REG_RXCRCCON, BIT_CRCEN,ENABLE);
|
||||
|
||||
SetCommand(CMD_TRANSCEIVE);
|
||||
DelayMs(10);
|
||||
GetReg(REG_ERROR,®_data);
|
||||
if((reg_data & 0x0F)!=0)
|
||||
return FAIL;
|
||||
GetReg(REG_FIFOLENGTH,®_data);
|
||||
if(reg_data != 18)
|
||||
return FAIL;
|
||||
GetReg(REG_FIFODATA,®_data);
|
||||
GetReg(REG_FIFODATA,®_data);
|
||||
for(i = 0;i < 8; i++)
|
||||
{
|
||||
GetReg(REG_FIFODATA,&picc_f->UID[i]);
|
||||
}
|
||||
return SUCCESS;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
157
apps/earphone/94_rfid_stc/READER/READER.h
Normal file
157
apps/earphone/94_rfid_stc/READER/READER.h
Normal file
@ -0,0 +1,157 @@
|
||||
|
||||
#ifndef _READER_H
|
||||
#define _READER_H
|
||||
|
||||
// #include "fm15l0xx_ll_gpio.h"
|
||||
#include "function.h"
|
||||
|
||||
static const unsigned char RF_CMD_REQA = 0x26;
|
||||
static const unsigned char RF_CMD_WUPA = 0x52;
|
||||
static const unsigned char RF_CMD_ANTICOLL[3] = {0x93,0x95,0x97} ;
|
||||
static const unsigned char RF_CMD_SELECT[3] = {0x93,0x95,0x97} ;
|
||||
|
||||
static const unsigned char RF_CMD_KEYA = 0x60;
|
||||
static const unsigned char RF_CMD_KEYB = 0x61;
|
||||
|
||||
struct picc_b_struct
|
||||
{
|
||||
unsigned char ATQB[12];//REQBӦ<42><D3A6>
|
||||
unsigned char PUPI[4];
|
||||
unsigned char APPLICATION_DATA[4];
|
||||
unsigned char PROTOCOL_INF[3];
|
||||
unsigned char CID;//ATTRIBӦ<42><D3A6>
|
||||
unsigned char Answer_to_HALT[1];//HALTӦ<54><D3A6>
|
||||
unsigned char SN[8];//<2F><><EFBFBD><EFBFBD>֤SN<53><4E><EFBFBD><EFBFBD>
|
||||
};
|
||||
|
||||
extern struct picc_b_struct PICC_B;
|
||||
|
||||
struct picc_a_struct
|
||||
{
|
||||
unsigned char ATQA[2];
|
||||
unsigned char CASCADE_LEVEL;
|
||||
unsigned char UID_Length;
|
||||
unsigned char UID[15];
|
||||
unsigned char BCC[3];
|
||||
unsigned char SAK[3];
|
||||
};
|
||||
|
||||
extern struct picc_a_struct PICC_A;
|
||||
|
||||
struct picc_v_struct
|
||||
{
|
||||
unsigned char UID[8];
|
||||
unsigned char RESPONSE;
|
||||
unsigned char BLOCK_DATA[4];
|
||||
};
|
||||
|
||||
extern struct picc_v_struct PICC_V;
|
||||
struct picc_f_struct
|
||||
{
|
||||
unsigned char UID[8];
|
||||
};
|
||||
extern struct picc_f_struct PICC_F;
|
||||
|
||||
|
||||
//<2F><><EFBFBD>ղ<EFBFBD><D5B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> TYPE A
|
||||
#define GAIN_A 7 //<2F><><EFBFBD>÷<EFBFBD>Χ0~7
|
||||
#define HPCF_A 3 //<2F><><EFBFBD>÷<EFBFBD>Χ0~7
|
||||
#define AMPLITUDE_A 255 //<2F><><EFBFBD>÷<EFBFBD>Χ0~255
|
||||
|
||||
|
||||
//<2F><><EFBFBD>ղ<EFBFBD><D5B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> TYPE B
|
||||
#define GAIN_B 7 //<2F><><EFBFBD>÷<EFBFBD>Χ0~7
|
||||
#define HPCF_B 3 //<2F><><EFBFBD>÷<EFBFBD>Χ0~7
|
||||
#define AMPLITUDE_B 255 //<2F><><EFBFBD>÷<EFBFBD>Χ0~255<35><35><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵԽ<D6B5><D4BD><EFBFBD>ز<EFBFBD>Խ<EFBFBD><D4BD>
|
||||
|
||||
#define MODULATION_B 100//<2F><><EFBFBD>÷<EFBFBD>Χ0~255,<2C><><EFBFBD><EFBFBD>ֵԽС<D4BD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Խ<EFBFBD><D4BD>
|
||||
|
||||
//<2F><><EFBFBD>ղ<EFBFBD><D5B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> TYPE V
|
||||
#define GAIN_V 7//<2F><><EFBFBD>÷<EFBFBD>Χ0~7
|
||||
#define HPCF_V 4//<2F><><EFBFBD>÷<EFBFBD>Χ0~7
|
||||
#define AMPLITUDE_V 255 //<2F><><EFBFBD>÷<EFBFBD>Χ0~255<35><35><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵԽ<D6B5><D4BD><EFBFBD>ز<EFBFBD>Խ<EFBFBD><D4BD>
|
||||
#define MODULATION_V 10 //<2F><><EFBFBD>÷<EFBFBD>Χ0~255,<2C><><EFBFBD><EFBFBD>ֵԽС<D4BD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Խ<EFBFBD><D4BD>
|
||||
|
||||
|
||||
//<2F><><EFBFBD>ղ<EFBFBD><D5B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> TYPE F
|
||||
#define GAIN_F 7//<2F><><EFBFBD>÷<EFBFBD>Χ0~7
|
||||
#define HPCF_F 4//<2F><><EFBFBD>÷<EFBFBD>Χ0~7
|
||||
#define AMPLITUDE_F 255 //<2F><><EFBFBD>÷<EFBFBD>Χ0~255<35><35><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵԽ<D6B5><D4BD><EFBFBD>ز<EFBFBD>Խ<EFBFBD><D4BD>
|
||||
|
||||
#define MODULATION_F 100//<2F><><EFBFBD>÷<EFBFBD>Χ0~255,<2C><><EFBFBD><EFBFBD>ֵԽС<D4BD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Խ<EFBFBD><D4BD>
|
||||
|
||||
#define SPI_GPIO GPIOB
|
||||
|
||||
#define SCK_PIN LL_GPIO_Pin1//PB1
|
||||
#define MISO_PIN LL_GPIO_Pin2//PB2
|
||||
#define MOSI_PIN LL_GPIO_Pin3//PB3
|
||||
#define NSS_PIN LL_GPIO_Pin0//PB0
|
||||
|
||||
#define SCK_0 0//LL_GPIO_ResetOutputPin( SPI_GPIO, SCK_PIN )
|
||||
#define SCK_1 0//LL_GPIO_SetOutputPin( SPI_GPIO, SCK_PIN )
|
||||
|
||||
#define MOSI_0 0//LL_GPIO_ResetOutputPin( SPI_GPIO, MOSI_PIN )
|
||||
#define MOSI_1 0//LL_GPIO_SetOutputPin( SPI_GPIO, MOSI_PIN )
|
||||
|
||||
#define PD_GPIO GPIOA
|
||||
#define PD_PIN LL_GPIO_Pin7//PA7
|
||||
|
||||
|
||||
#define NSS_0 0//LL_GPIO_ResetOutputPin( SPI_GPIO, NSS_PIN )
|
||||
#define NSS_1 0//LL_GPIO_SetOutputPin( SPI_GPIO, NSS_PIN )
|
||||
|
||||
|
||||
#define PD_0 0//LL_GPIO_ResetOutputPin( PD_GPIO, PD_PIN )
|
||||
#define PD_1 0//LL_GPIO_SetOutputPin( PD_GPIO, PD_PIN )
|
||||
|
||||
|
||||
|
||||
|
||||
extern void DelayMs( uint32_t xms );
|
||||
extern void DelayUs( uint32_t xus );
|
||||
|
||||
void Reader_GPIO_Init( void );
|
||||
|
||||
// extern unsigned char Reader_Set_HPD( unsigned char mode );
|
||||
|
||||
extern unsigned char FM176XX_HardReset(void);
|
||||
|
||||
// extern unsigned char Reader_Set_HPD( unsigned char mode ) ;
|
||||
|
||||
// extern unsigned char SetReg(unsigned char address,unsigned char reg_data);
|
||||
|
||||
// extern unsigned char GetReg(unsigned char address,unsigned char *reg_data);
|
||||
|
||||
extern void ModifyReg( unsigned char reg_address, unsigned char mask, unsigned char set );
|
||||
extern void Clear_FIFO(void);
|
||||
|
||||
extern unsigned char SetCommand(unsigned char command);
|
||||
extern void SetParity(unsigned char state);
|
||||
extern void SetTimer(unsigned int timeout);
|
||||
extern unsigned char SetCW(unsigned char mode);
|
||||
extern unsigned char ReaderA_Initial(void);
|
||||
extern unsigned char ReaderB_Initial(void);
|
||||
extern unsigned char ReaderV_Initial(void);
|
||||
extern unsigned char ReaderF_Initial(void);
|
||||
|
||||
extern unsigned char ReaderA_Wakeeup(struct picc_a_struct *picc_a);
|
||||
extern unsigned char ReaderA_Request(struct picc_a_struct *picc_a);
|
||||
extern unsigned char ReaderA_Anticoll(struct picc_a_struct *picc_a);
|
||||
extern unsigned char ReaderA_Select(struct picc_a_struct *picc_a);
|
||||
extern unsigned char ReaderA_CardActivate(struct picc_a_struct *picc_a);
|
||||
|
||||
extern unsigned char ReaderB_Wakeup(struct picc_b_struct *picc_b);
|
||||
extern unsigned char ReaderB_Request(struct picc_b_struct *picc_b);
|
||||
extern unsigned char ReaderB_Attrib(struct picc_b_struct *picc_b);
|
||||
extern unsigned char ReaderB_Halt(struct picc_b_struct *picc_b);
|
||||
extern unsigned char ReaderB_Get_SN(struct picc_b_struct *picc_b);
|
||||
|
||||
extern unsigned char ReaderV_Inventory(struct picc_v_struct *picc_v);
|
||||
extern unsigned char ReaderV_Select(struct picc_v_struct *picc_v);
|
||||
extern unsigned char ReaderV_ReadSingleBlock(unsigned char block_num,struct picc_v_struct *picc_v);
|
||||
extern unsigned char ReaderV_WriteSingleBlock(unsigned char block_num,struct picc_v_struct *picc_v);
|
||||
|
||||
extern unsigned char ReaderF_Inventory(struct picc_f_struct *picc_f);
|
||||
|
||||
#endif
|
||||
|
||||
478
apps/earphone/94_rfid_stc/READER/READER_REG.h
Normal file
478
apps/earphone/94_rfid_stc/READER/READER_REG.h
Normal file
@ -0,0 +1,478 @@
|
||||
/*********************************************************************
|
||||
* *
|
||||
* Copyright (c) 2010 Shanghai FuDan MicroElectronic Inc, Ltd. *
|
||||
* All rights reserved. Licensed Software Material. *
|
||||
* *
|
||||
* Unauthorized use, duplication, or distribution is strictly *
|
||||
* prohibited by law. *
|
||||
* *
|
||||
**********************************************************************/
|
||||
#ifndef _READER_REG_H
|
||||
#define _READER_REG_H
|
||||
|
||||
#define REG_COMMAND 0x00 //
|
||||
#define REG_HOSTCTRL 0x01 //
|
||||
#define REG_FIFOCONTROL 0x02 //
|
||||
#define REG_WATERLEVEL 0x03 //
|
||||
#define REG_FIFOLENGTH 0x04 //
|
||||
#define REG_FIFODATA 0x05 //
|
||||
#define REG_IRQ0 0x06 //
|
||||
#define REG_IRQ1 0x07 //
|
||||
#define REG_IRQ0EN 0x08 //
|
||||
#define REG_IRQ1EN 0x09 //
|
||||
#define REG_ERROR 0x0A //
|
||||
#define REG_STATUS 0x0B //
|
||||
#define REG_RXBITCTRL 0x0C //
|
||||
#define REG_RXCOLL 0x0D //
|
||||
#define REG_TCONTROL 0x0E //
|
||||
#define REG_T0CONTROL 0x0F //
|
||||
|
||||
|
||||
#define REG_T0RELOADHI 0x10 //
|
||||
#define REG_T0RELOADLO 0x11 //
|
||||
#define REG_T0COUNTERVALHI 0x12 //
|
||||
#define REG_T0COUNTERVALLO 0x13 //
|
||||
#define REG_T1CONTROL 0x14 //
|
||||
#define REG_T1RELOADHI 0x15 //
|
||||
#define REG_T1RELOADLO 0x16 //
|
||||
#define REG_T1COUNTERVALHI 0x17 //
|
||||
#define REG_T1COUNTERVALLO 0x18 //
|
||||
#define REG_T2CONTROL 0x19 //
|
||||
#define REG_T2RELOADHI 0x1A //
|
||||
#define REG_T2RELOADLO 0x1B //
|
||||
#define REG_T2COUNTERVALHI 0x1C //
|
||||
#define REG_T2COUNTERVALLO 0x1D //
|
||||
#define REG_T3CONTROL 0x1E //
|
||||
#define REG_T3RELOADHI 0x1F //
|
||||
|
||||
|
||||
#define REG_T3RELOADLO 0x20 //
|
||||
#define REG_T3COUNTERVALHI 0x21 //
|
||||
#define REG_T3COUNTERVALLO 0x22 //
|
||||
#define REG_T4CONTROL 0x23 //
|
||||
#define REG_T4RELOADHI 0x24 //
|
||||
#define REG_T4RELOADLO 0x25 //
|
||||
#define REG_T4COUNTERVALHI 0x26 //
|
||||
#define REG_T4COUNTERVALLO 0x27 //
|
||||
#define REG_TXMODE 0x28
|
||||
#define REG_TXAMP 0x29
|
||||
#define REG_TXCON 0x2A //
|
||||
#define REG_TXI 0x2B //
|
||||
#define REG_TXCRCCON 0x2C //
|
||||
#define REG_RXCRCCON 0x2D //
|
||||
#define REG_TXDATANUM 0x2E
|
||||
#define REG_TXMODWIDTH 0x2F //
|
||||
|
||||
|
||||
#define REG_TXSYM10BURSTLEN 0x30 //
|
||||
#define REG_TXWAITCTRL 0x31 //
|
||||
#define REG_TXWAITLO 0x32 //
|
||||
#define REG_FRAMECON 0x33 //
|
||||
#define REG_RXSOFD 0x34 //
|
||||
#define REG_RXCTRL 0x35 //
|
||||
#define REG_RXWAIT 0x36 //
|
||||
#define REG_RXTHRESHOLD 0x37 //
|
||||
#define REG_RCV 0x38 //
|
||||
#define REG_RXANA 0x39 //
|
||||
#define REG_LPCD_OPTIONS 0x3A //
|
||||
#define REG_SERIALSPEED 0x3B //
|
||||
#define REG_LFO_TRIMM 0x3C //
|
||||
#define REG_CLKOUT_CTRL 0x3D //
|
||||
#define REG_LPCD_THRESHOLD 0x3E //
|
||||
#define REG_LPCD_QMIN 0x3F //
|
||||
#define REG_LPCD_QMAX 0x40
|
||||
#define REG_LPCD_IMIN 0x41
|
||||
#define REG_LPCD_RESULT_I 0x42
|
||||
#define REG_LPCD_RESULT_Q 0x43
|
||||
#define REG_THNADJ 0x5F
|
||||
#define REG_THNSET 0x61
|
||||
#define REG_THNMIN 0x62
|
||||
#define REG_DSP_CTRL1 0x64
|
||||
#define REG_MISC 0x75
|
||||
#define REG_RXTXCON 0x77
|
||||
#define REG_ERROREXT 0x7E
|
||||
#define REG_VERSION 0x7F
|
||||
|
||||
#define CMD_MASK 0x1F
|
||||
|
||||
#define CMD_IDLE 0x00
|
||||
#define CMD_LPCD 0x01
|
||||
#define CMD_LOADKEY 0x02
|
||||
#define CMD_AUTHENT 0x03
|
||||
#define CMD_RECEIVE 0x05
|
||||
#define CMD_TRANSMIT 0x06
|
||||
#define CMD_TRANSCEIVE 0x07
|
||||
#define CMD_WRITEE2 0x08
|
||||
#define CMD_WRITEE2PAGE 0x09
|
||||
#define CMD_READE2 0x0A
|
||||
#define CMD_LOADREG 0x0C
|
||||
#define CMD_LOADPROTOCOL 0x0D
|
||||
#define CMD_LOADKEYE2 0x0E
|
||||
#define CMD_STOREKEYE2 0x0F
|
||||
#define CMD_CRCCALC 0x1B
|
||||
#define CMD_READRNR 0x1C
|
||||
#define CMD_SOFTRESET 0x1F
|
||||
|
||||
|
||||
/** \name Host-Control Register Contents (0x00)
|
||||
*/
|
||||
/*@{*/
|
||||
#define BIT_STANDBY 0x80U /**< Standby bit; If set, the IC transits to standby mode. */
|
||||
#define BIT_MODEMOFF 0x40U
|
||||
/*@{*/
|
||||
/** \name Host-Control Register Contents (0x01)
|
||||
*/
|
||||
/*@{*/
|
||||
#define BIT_I2CFORCEHS 0x01U
|
||||
//#define BIT_REGEN 0x80U
|
||||
//#define BIT_BUSHOST 0x40U
|
||||
//#define BIT_BUSSAM 0x20U
|
||||
//#define MASK_SAMINTERFACE 0x0CU
|
||||
/*@}*/
|
||||
|
||||
/** \name FIFO-Control Register Contents (0x02)
|
||||
*/
|
||||
/*@{*/
|
||||
#define BIT_FIFOSIZE 0x80U
|
||||
#define BIT_HIALERT 0x40U
|
||||
#define BIT_LOALERT 0x20U
|
||||
#define BIT_FIFOFLUSH 0x10U
|
||||
#define BIT_WATERLEVEL_HI 0x04U
|
||||
#define MASK_FIFOLENGTH_HI 0x03U
|
||||
/*@}*/
|
||||
|
||||
/** \name IRQ0 Register(s) Contents (0x06/0x08)
|
||||
*/
|
||||
/*@{*/
|
||||
#define BIT_SET 0x80U
|
||||
#define BIT_IRQINV 0x80U
|
||||
#define BIT_HIALERTIRQ 0x40U
|
||||
#define BIT_LOALERTIRQ 0x20U
|
||||
#define BIT_IDLEIRQ 0x10U
|
||||
#define BIT_TXIRQ 0x08U
|
||||
#define BIT_RXIRQ 0x04U
|
||||
#define BIT_ERRIRQ 0x02U
|
||||
#define BIT_RXSOFIRQ 0x01U
|
||||
/*@}*/
|
||||
|
||||
/** \name IRQ1 Register(s) Contents (0x07/0x09)
|
||||
*/
|
||||
/*@{*/
|
||||
/* #define BIT_SET 0x80U */
|
||||
#define BIT_IRQPUSHPULL 0x80U
|
||||
#define BIT_GLOBALIRQ 0x40U
|
||||
#define BIT_IRQPINEN 0x40U
|
||||
#define BIT_LPCDIRQ 0x20U
|
||||
#define BIT_TIMER4IRQ 0x10U
|
||||
#define BIT_TIMER3IRQ 0x08U
|
||||
#define BIT_TIMER2IRQ 0x04U
|
||||
#define BIT_TIMER1IRQ 0x02U
|
||||
#define BIT_TIMER0IRQ 0x01U
|
||||
/*@}*/
|
||||
|
||||
/** \name Error Register Contents (0x0A)
|
||||
*/
|
||||
/*@{*/
|
||||
#define BIT_CMDEE_ERR 0x80U
|
||||
#define BIT_FIFOWRERR 0x40U
|
||||
#define BIT_FIFOOVL 0x20U
|
||||
#define BIT_MINFRAMEERR 0x10U
|
||||
#define BIT_NODATAERR 0x08U
|
||||
#define BIT_COLLDET 0x04U
|
||||
#define BIT_PROTERR 0x02U
|
||||
#define BIT_INTEGERR 0x01U
|
||||
/*@}*/
|
||||
|
||||
/** \name Status Register Contents (0x0B)
|
||||
*/
|
||||
/*@{*/
|
||||
#define BIT_CRYPTO1ON 0x20U
|
||||
#define MASK_COMMSTATE 0x07U
|
||||
/*@}*/
|
||||
|
||||
/** \name Rx-Bit-Control Register Contents (0x0C)
|
||||
*/
|
||||
/*@{*/
|
||||
#define BIT_VALUESAFTERCOLL 0x80U
|
||||
#define BIT_NOCOLL 0x08U
|
||||
#define MASK_RXALIGN 0x70U
|
||||
#define MASK_RXLASTBITS 0x07U
|
||||
/*@}*/
|
||||
|
||||
/** \name Rx-Coll Register Contents (0x0D)
|
||||
*/
|
||||
/*@{*/
|
||||
#define BIT_COLLPOSVALID 0x80U
|
||||
#define MASK_COLLPOS 0x7FU
|
||||
/*@}*/
|
||||
|
||||
/** \name Timer-Control Register Contents (0x0E)
|
||||
*/
|
||||
/*@{*/
|
||||
#define BIT_T3RUNNING 0x80U
|
||||
#define BIT_T2RUNNING 0x40U
|
||||
#define BIT_T1RUNNING 0x20U
|
||||
#define BIT_T0RUNNING 0x10U
|
||||
#define BIT_T3STARTSTOPNOW 0x08U
|
||||
#define BIT_T2STARTSTOPNOW 0x04U
|
||||
#define BIT_T1STARTSTOPNOW 0x02U
|
||||
#define BIT_T0STARTSTOPNOW 0x01U
|
||||
/*@}*/
|
||||
|
||||
/** \name T[0-3]-Control Register Contents (0x0F/0x14/0x19/0x1E)
|
||||
*/
|
||||
/*@{*/
|
||||
#define BIT_TSTOP_RX 0x80U /**< Stop timer on receive interrupt. */
|
||||
#define BIT_TAUTORESTARTED 0x08U /**< Auto-restart timer after underflow. */
|
||||
#define BIT_TSTART_TX 0x10U /**< Start timer on transmit interrupt. */
|
||||
//#define BIT_TSTART_LFO 0x20U /**< Use this timer for LFO trimming. */
|
||||
//#define BIT_TSTART_LFO_UV 0x30U /**< Use this timer for LFO trimming (generate UV at a trimming event). */
|
||||
#define MASK_TSTART 0x30U /**< Mask for TSTART bits. */
|
||||
#define VALUE_TCLK_1356_MHZ 0x00U /**< Use 13.56MHz as input clock. */
|
||||
#define VALUE_TCLK_212_KHZ 0x01U /**< Use 212KHz as input clock. */
|
||||
#define VALUE_TCLK_T0 0x02U /**< Use timer0 as input clock. */
|
||||
#define VALUE_TCLK_T1 0x03U /**< Use timer1 as input clock. */
|
||||
/*@}*/
|
||||
|
||||
/** \name T4-Control Register Contents (0x23)
|
||||
*/
|
||||
/*@{*/
|
||||
#define BIT_T4RUNNING 0x80U
|
||||
#define BIT_T4STARTSTOPNOW 0x40U
|
||||
#define BIT_T4AUTOTRIMM 0x20U
|
||||
#define BIT_T4AUTOLPCD 0x10U
|
||||
#define BIT_T4AUTORESTARTED 0x08U
|
||||
#define BIT_T4AUTOWAKEUP 0x04U
|
||||
/*#define MASK_TSTART 0x30U*/
|
||||
#define VALUE_TCLK_LFO_64_KHZ 0x00U
|
||||
#define VALUE_TCLK_LFO_8_KHZ 0x01U
|
||||
#define VALUE_TCLK_LFO_4_KHZ 0x02U
|
||||
#define VALUE_TCLK_LFO_2_KHZ 0x03U
|
||||
/*@}*/
|
||||
|
||||
/** \name Driver Mode Register Contents (0x28)
|
||||
*/
|
||||
/*@{*/
|
||||
#define BIT_TX2INV 0x80U
|
||||
#define BIT_TX1INV 0x40U
|
||||
#define BIT_TXEN 0x08U
|
||||
#define VALUE_TXCLKMODE_HIGHIMPEDANCE 0x00U
|
||||
#define VALUE_TXCLKMODE_OUTPULL0 0x01U
|
||||
#define VALUE_TXCLKMODE_OUTPULL1 0x02U
|
||||
#define VALUE_TXCLKMODE_RFLOWPULL 0x05U
|
||||
#define VALUE_TXCLKMODE_RFHIGHPUSH 0x06U
|
||||
#define VALUE_TXCLKMODE_PUSHPULL 0x07U
|
||||
#define BIT_RFON 0x04U
|
||||
#define BIT_TPUSHON 0x02U
|
||||
#define BIT_TPULLON 0x01U
|
||||
/*@}*/
|
||||
|
||||
/** \name Tx Amplifier Register Contents (0x29)
|
||||
*/
|
||||
/*@{*/
|
||||
#define MASK_CW_AMPLITUDE 0x00U
|
||||
#define MASK_RESIDUAL_CARRIER 0x1FU
|
||||
/*@}*/
|
||||
|
||||
/** \name Driver Control Register Contents (0x2A)
|
||||
*/
|
||||
/*@{*/
|
||||
#define BIT_CWMAX 0x08U
|
||||
#define BIT_DRIVERINV 0x04U
|
||||
#define VALUE_DRIVERSEL_LOW 0x00U
|
||||
#define VALUE_DRIVERSEL_TXENV 0x01U
|
||||
#define VALUE_DRIVERSEL_SIGIN 0x02U
|
||||
/*@}*/
|
||||
|
||||
/** \name Tx-/Rx-CRC Control Register Contents (0x2C/0x2D)
|
||||
*/
|
||||
/*@{*/
|
||||
#define BIT_RXFORCECRCWRITE 0x80U
|
||||
#define BIT_CRCINVERT 0x02U
|
||||
#define BIT_CRCEN 0x01U
|
||||
#define MASK_CRCPRESETVAL 0x70U
|
||||
#define MASK_CRCTYPE 0x0CU
|
||||
#define MASK_CRCTYPE5 0x00U
|
||||
#define MASK_CRCTYPE16 0x08U
|
||||
/*@}*/
|
||||
|
||||
/** \name Tx-DataNum Register Contents (0x2E)
|
||||
*/
|
||||
/*@{*/
|
||||
#define BIT_KEEPBITGRID 0x10U
|
||||
#define BIT_DATAEN 0x08U
|
||||
#define MASK_TXLASTBITS 0x07U
|
||||
#define MASK_SYMBOL_SEND 0x08U
|
||||
/*@}*/
|
||||
|
||||
/** \name Tx-Wait Control Register Contents (0x31)
|
||||
*/
|
||||
/*@{*/
|
||||
#define BIT_TXWAIT_START_RX 0x80U
|
||||
#define BIT_TXWAIT_DBFREQ 0x40U
|
||||
#define MASK_TXWAITHI 0x38U
|
||||
#define MASK_TXSTOPBITLEN 0x07U
|
||||
/*@}*/
|
||||
|
||||
/** \name Frame Control Register Contents (0x33)
|
||||
*/
|
||||
/*@{*/
|
||||
#define BIT_TXPARITYEN 0x80U
|
||||
#define BIT_RXPARITYEN 0x40U
|
||||
#define VALUE_STOP_SYM3 0x0CU
|
||||
#define VALUE_STOP_SYM2 0x08U
|
||||
#define VALUE_STOP_SYM1 0x04U
|
||||
#define VALUE_START_SYM3 0x03U
|
||||
#define VALUE_START_SYM2 0x02U
|
||||
#define VALUE_START_SYM1 0x01U
|
||||
#define MASK_STARTSYM 0x03U
|
||||
#define MASK_STOPSYM 0x0CU
|
||||
/*@}*/
|
||||
|
||||
/** \name Rx Control Register Contents (0x35)
|
||||
*/
|
||||
/*@{*/
|
||||
#define BIT_RXALLOWBITS 0x80U
|
||||
#define BIT_RXMULTIPLE 0x40U
|
||||
#define BIT_RXEOFTYPE 0x20U
|
||||
#define BIT_EGT_CHECK 0x10U
|
||||
#define BIT_EMD_SUPPRESSION 0x08U
|
||||
#define MASK_RXBAUDRATE 0x07U
|
||||
/*@}*/
|
||||
|
||||
/** \name Rx-Wait Register Contents (0x36)
|
||||
*/
|
||||
/*@{*/
|
||||
#define BIT_RXWAITDBFREQ 0x80U
|
||||
#define MASK_RXWAIT 0x7FU
|
||||
/*@}*/
|
||||
|
||||
/** \name Rx-Threshold Register Contents (0x37)
|
||||
*/
|
||||
/*@{*/
|
||||
#define MASK_MINLEVEL 0xF0U
|
||||
#define MASK_MINLEVELP 0x0FU
|
||||
/*@}*/
|
||||
|
||||
/** \name Rx-Receiver Register Contents (0x38)
|
||||
*/
|
||||
/*@{*/
|
||||
#define BIT_RX_SINGLE 0x80U
|
||||
#define BIT_RX_SHORT_MIX2ADC 0x40U
|
||||
#define BIT_USE_SMALL_EVAL 0x04U
|
||||
#define MASK_RX_SIGPRO_IN_SEL 0x30U
|
||||
#define MASK_COLLLEVEL 0x03U
|
||||
/*@}*/
|
||||
|
||||
/** \name Rx-Analog Register Contents (0x39)
|
||||
*/
|
||||
/*@{*/
|
||||
#define BIT_RX_OC_ENABLE 0x20U
|
||||
#define BIT_RX_HP_LOWF 0x10U
|
||||
#define MASK_VMID_R_SEL 0xC0U
|
||||
#define MASK_RCV_HPCF 0x0CU
|
||||
#define MASK_RCV_GAIN 0x03U
|
||||
/*@}*/
|
||||
|
||||
/** \name Serial-Speed Register Contents (0x3B)
|
||||
*/
|
||||
/*@{*/
|
||||
#define MASK_BR_T0 0xE0U
|
||||
#define MASK_BR_T1 0x1FU
|
||||
/*@}*/
|
||||
|
||||
|
||||
/** \name LPCD Result(Q) Register Contents (0x43)
|
||||
*/
|
||||
/*@{*/
|
||||
#define BIT_LPCDIRQ_CLR 0x40U
|
||||
/*@}*/
|
||||
|
||||
/** \name Tx-BitMod Register Contents (0x48)
|
||||
*/
|
||||
/*@{*/
|
||||
#define BIT_TXMSBFIRST 0x80U
|
||||
#define BIT_TXPARITYTYPE 0x20U
|
||||
#define BIT_TXSTOPBITTYPE 0x08U
|
||||
#define BIT_TXSTARTBITTYPE 0x02U
|
||||
#define BIT_TXSTARTBITEN 0x01U
|
||||
/*@}*/
|
||||
|
||||
/** \name Rx-BitMod Register Contents (0x58)
|
||||
*/
|
||||
/*@{*/
|
||||
#define BIT_RXSTOPONINVPAR 0x20U
|
||||
#define BIT_RXSTOPONLEN 0x10U
|
||||
#define BIT_RXMSBFIRST 0x08U
|
||||
#define BIT_RXSTOPBITEN 0x04U
|
||||
#define BIT_RXPARITYTYPE 0x02U
|
||||
/*@}*/
|
||||
|
||||
/** \name Rx-Mod Register Contents (0x5D)
|
||||
*/
|
||||
/*@{*/
|
||||
#define BIT_PREFILTER 0x20U
|
||||
#define BIT_RECTFILTER 0x10U
|
||||
#define BIT_SYNCHIGH 0x08U
|
||||
#define BIT_CORRINV 0x04U
|
||||
#define BIT_FSK 0x02U
|
||||
#define BIT_BPSK 0x01U
|
||||
/*@}*/
|
||||
|
||||
/** \name RxSupCfg Register Contents (0x6E)
|
||||
*/
|
||||
/*@{*/
|
||||
#define BIT_RXNOERR 0x80U
|
||||
/*@}*/
|
||||
/** \name RxTxConReg Register Contents (0x77)
|
||||
*/
|
||||
/*@{*/
|
||||
#define BIT_SHMODE 0x08U //<2F>Ϻ<EFBFBD><CFBA>㷨
|
||||
/*@}*/
|
||||
|
||||
/** \name ErrorExtReg Register Contents (0x7E)
|
||||
*/
|
||||
/*@{*/
|
||||
#define PARITY_ERROR 0x08U
|
||||
#define CRC_ERROR 0x04U
|
||||
/*@{*/
|
||||
|
||||
#define LPCD_OPTION2 0x1DF
|
||||
|
||||
//---------------------------------------------------------------
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD><D0AD><EFBFBD><EFBFBD>
|
||||
#define RX_TYPEA_106 0
|
||||
#define RX_TYPEA_212 1
|
||||
#define RX_TYPEA_424 2
|
||||
#define RX_TYPEA_848 3
|
||||
|
||||
#define RX_TYPEB_106 4
|
||||
#define RX_TYPEB_212 5
|
||||
#define RX_TYPEB_424 6
|
||||
#define RX_TYPEB_848 7
|
||||
|
||||
#define RX_TYPEV_26 10
|
||||
#define RX_TYPEV_53 11
|
||||
|
||||
#define RX_FELICA_212 19
|
||||
#define RX_FELICA_424 20
|
||||
|
||||
//<2F><><EFBFBD>巢<EFBFBD><E5B7A2>Э<EFBFBD><D0AD><EFBFBD><EFBFBD>
|
||||
#define TX_TYPEA_106 0
|
||||
#define TX_TYPEA_212 1
|
||||
#define TX_TYPEA_424 2
|
||||
#define TX_TYPEA_848 3
|
||||
|
||||
#define TX_TYPEB_106 4
|
||||
#define TX_TYPEB_212 5
|
||||
#define TX_TYPEB_424 6
|
||||
#define TX_TYPEB_848 7
|
||||
|
||||
#define TX_TYPEV_26 10
|
||||
#define TX_TYPEV_53 11
|
||||
|
||||
#define TX_FELICA_212 19
|
||||
#define TX_FELICA_424 20
|
||||
|
||||
#endif
|
||||
|
||||
225
apps/earphone/94_rfid_stc/READER/READER_REG_commented.h
Normal file
225
apps/earphone/94_rfid_stc/READER/READER_REG_commented.h
Normal file
@ -0,0 +1,225 @@
|
||||
/**
|
||||
* @file READER_REG.h
|
||||
* @brief RFID 读卡器芯片寄存器定义
|
||||
* @version 1.0
|
||||
* @date 2024-05-20
|
||||
*
|
||||
* @copyright Copyright (c) 2010 Shanghai FuDan MicroElectronic Inc, Ltd.
|
||||
*
|
||||
* @par 文件作用:
|
||||
* 这个头文件是整个RFID驱动库的最底层基石。它定义了RFID读卡器芯片
|
||||
* (例如FM176XX系列)内部所有寄存器的地址、每个寄存器中功能位的掩码,
|
||||
* 以及芯片支持的各种命令码。
|
||||
* MCU通过SPI接口读写这些寄存器,从而精确控制芯片的各项功能,
|
||||
* 例如启动/停止命令、配置通信协议、读写FIFO数据、查询状态等。
|
||||
* 所有上层对RFID的操作,最终都会转化为对这个文件中定义的寄存器的读写。
|
||||
*/
|
||||
#ifndef _READER_REG_H
|
||||
#define _READER_REG_H
|
||||
|
||||
//==================================================================================
|
||||
// 1. 寄存器地址定义 (Register Address Definitions)
|
||||
// 这些是芯片内部功能单元的地址,通过SPI访问。
|
||||
//==================================================================================
|
||||
#define REG_COMMAND 0x00 // 命令寄存器: 启动/停止芯片执行的命令 (如 Idle, Transceive)
|
||||
#define REG_HOSTCTRL 0x01 // 主机控制寄存器
|
||||
#define REG_FIFOCONTROL 0x02 // FIFO 控制寄存器 (如刷新FIFO)
|
||||
#define REG_WATERLEVEL 0x03 // FIFO 水位寄存器: 用于中断触发
|
||||
#define REG_FIFOLENGTH 0x04 // FIFO 长度寄存器: 当前FIFO中存储的字节数
|
||||
#define REG_FIFODATA 0x05 // FIFO 数据寄存器: 通过此寄存器写入要发送的数据,或读出接收到的数据
|
||||
#define REG_IRQ0 0x06 // 中断请求标志寄存器 0
|
||||
#define REG_IRQ1 0x07 // 中断请求标志寄存器 1
|
||||
#define REG_IRQ0EN 0x08 // 中断使能寄存器 0
|
||||
#define REG_IRQ1EN 0x09 // 中断使能寄存器 1
|
||||
#define REG_ERROR 0x0A // 错误标志寄存器: 显示通信中发生的错误 (如CRC错误, 冲突)
|
||||
#define REG_STATUS 0x0B // 状态寄存器: 显示芯片当前状态 (如MIFARE加密是否开启)
|
||||
#define REG_RXBITCTRL 0x0C // 接收数据位控制寄存器
|
||||
#define REG_RXCOLL 0x0D // 接收冲突检测寄存器
|
||||
#define REG_TCONTROL 0x0E // 定时器总体控制
|
||||
#define REG_T0CONTROL 0x0F // 定时器0 控制
|
||||
#define REG_T0RELOADHI 0x10 // 定时器0 重载值高字节
|
||||
#define REG_T0RELOADLO 0x11 // 定时器0 重载值低字节
|
||||
#define REG_T0COUNTERVALHI 0x12 // 定时器0 计数值高字节
|
||||
#define REG_T0COUNTERVALLO 0x13 // 定时器0 计数值低字节
|
||||
#define REG_T1CONTROL 0x14 // 定时器1 控制
|
||||
#define REG_T1RELOADHI 0x15 // 定时器1 重载值高字节
|
||||
#define REG_T1RELOADLO 0x16 // 定时器1 重载值低字节
|
||||
#define REG_T1COUNTERVALHI 0x17 // 定时器1 计数值高字节
|
||||
#define REG_T1COUNTERVALLO 0x18 // 定时器1 计数值低字节
|
||||
#define REG_T2CONTROL 0x19 // 定时器2 控制
|
||||
#define REG_T2RELOADHI 0x1A // 定时器2 重载值高字节
|
||||
#define REG_T2RELOADLO 0x1B // 定时器2 重载值低字节
|
||||
#define REG_T2COUNTERVALHI 0x1C // 定时器2 计数值高字节
|
||||
#define REG_T2COUNTERVALLO 0x1D // 定时器2 计数值低字节
|
||||
#define REG_T3CONTROL 0x1E // 定时器3 控制
|
||||
#define REG_T3RELOADHI 0x1F // 定时器3 重载值高字节
|
||||
#define REG_T3RELOADLO 0x20 // 定时器3 重载值低字节
|
||||
#define REG_T3COUNTERVALHI 0x21 // 定时器3 计数值高字节
|
||||
#define REG_T3COUNTERVALLO 0x22 // 定时器3 计数值低字节
|
||||
#define REG_T4CONTROL 0x23 // 定时器4 控制
|
||||
#define REG_T4RELOADHI 0x24 // 定时器4 重载值高字节
|
||||
#define REG_T4RELOADLO 0x25 // 定时器4 重载值低字节
|
||||
#define REG_T4COUNTERVALHI 0x26 // 定时器4 计数值高字节
|
||||
#define REG_T4COUNTERVALLO 0x27 // 定时器4 计数值低字节
|
||||
#define REG_TXMODE 0x28 // 发射模式寄存器
|
||||
#define REG_TXAMP 0x29 // 发射信号幅度(天线功率)寄存器
|
||||
#define REG_TXCON 0x2A // 发射控制寄存器 (调制深度等)
|
||||
#define REG_TXI 0x2B // 发射器内部控制
|
||||
#define REG_TXCRCCON 0x2C // 发射CRC控制寄存器
|
||||
#define REG_RXCRCCON 0x2D // 接收CRC控制寄存器
|
||||
#define REG_TXDATANUM 0x2E // 发送数据位数控制寄存器
|
||||
#define REG_TXMODWIDTH 0x2F // 发射调制脉宽寄存器
|
||||
#define REG_TXSYM10BURSTLEN 0x30 // TypeB的10%调制脉冲长度
|
||||
#define REG_TXWAITCTRL 0x31 // 发送等待控制寄存器
|
||||
#define REG_TXWAITLO 0x32 // 发送等待时间低字节
|
||||
#define REG_FRAMECON 0x33 // 帧控制寄存器 (奇偶校验等)
|
||||
#define REG_RXSOFD 0x34 // 接收起始标志位(SOF)配置
|
||||
#define REG_RXCTRL 0x35 // 接收控制寄存器
|
||||
#define REG_RXWAIT 0x36 // 接收等待时间寄存器
|
||||
#define REG_RXTHRESHOLD 0x37 // 接收信号阈值寄存器
|
||||
#define REG_RCV 0x38 // 接收器配置寄存器
|
||||
#define REG_RXANA 0x39 // 接收器模拟电路配置寄存器 (增益等)
|
||||
#define REG_LPCD_OPTIONS 0x3A // 低功耗寻卡(LPCD)选项
|
||||
#define REG_SERIALSPEED 0x3B // 串行接口速率配置
|
||||
#define REG_LFO_TRIMM 0x3C // 低频振荡器(LFO)微调
|
||||
#define REG_CLKOUT_CTRL 0x3D // 时钟输出控制
|
||||
#define REG_LPCD_THRESHOLD 0x3E // LPCD阈值
|
||||
#define REG_LPCD_QMIN 0x3F // LPCD Q通道最小值
|
||||
#define REG_LPCD_QMAX 0x40 // LPCD Q通道最大值
|
||||
#define REG_LPCD_IMIN 0x41 // LPCD I通道最小值
|
||||
#define REG_LPCD_RESULT_I 0x42 // LPCD I通道结果
|
||||
#define REG_LPCD_RESULT_Q 0x43 // LPCD Q通道结果
|
||||
#define REG_THNADJ 0x5F
|
||||
#define REG_THNSET 0x61
|
||||
#define REG_THNMIN 0x62
|
||||
#define REG_DSP_CTRL1 0x64
|
||||
#define REG_MISC 0x75 // 杂项配置
|
||||
#define REG_RXTXCON 0x77 // 收发控制寄存器
|
||||
#define REG_ERROREXT 0x7E // 扩展错误标志
|
||||
#define REG_VERSION 0x7F // 版本寄存器: 读取此寄存器可获取芯片型号和版本
|
||||
|
||||
//==================================================================================
|
||||
// 2. 命令码定义 (Command Codes)
|
||||
// 写入到 REG_COMMAND 寄存器的值,用于启动芯片执行特定任务。
|
||||
//==================================================================================
|
||||
#define CMD_MASK 0x1F // 命令码掩码
|
||||
|
||||
#define CMD_IDLE 0x00 // 空闲命令: 停止当前操作,进入空闲状态
|
||||
#define CMD_LPCD 0x01 // 低功耗寻卡 (Low-Power Card Detection)
|
||||
#define CMD_LOADKEY 0x02 // 加载密钥命令: 将密钥从FIFO加载到内部密钥缓冲器 (用于MIFARE认证)
|
||||
#define CMD_AUTHENT 0x03 // MIFARE认证命令: 执行MIFARE Classic卡的三重认证流程
|
||||
#define CMD_RECEIVE 0x05 // 接收命令: 仅开启接收器,等待接收数据
|
||||
#define CMD_TRANSMIT 0x06 // 发送命令: 将FIFO中的数据发送出去
|
||||
#define CMD_TRANSCEIVE 0x07 // 发送并接收命令: 核心命令,先发送FIFO中的数据,然后自动进入接收状态等待卡片响应
|
||||
#define CMD_WRITEE2 0x08 // 写E2PROM
|
||||
#define CMD_WRITEE2PAGE 0x09 // 写E2PROM页
|
||||
#define CMD_READE2 0x0A // 读E2PROM
|
||||
#define CMD_LOADREG 0x0C // 从E2PROM加载寄存器值
|
||||
#define CMD_LOADPROTOCOL 0x0D // 加载协议命令: 配置芯片以支持特定的通信协议 (Type A/B/V/F)
|
||||
#define CMD_LOADKEYE2 0x0E // 从E2PROM加载密钥
|
||||
#define CMD_STOREKEYE2 0x0F // 存储密钥到E2PROM
|
||||
#define CMD_CRCCALC 0x1B // CRC计算
|
||||
#define CMD_READRNR 0x1C // 读随机数
|
||||
#define CMD_SOFTRESET 0x1F // 软复位命令: 复位芯片
|
||||
|
||||
//==================================================================================
|
||||
// 3. 寄存器功能位定义 (Bit Definitions for Registers)
|
||||
// 定义了每个寄存器中特定位的功能,方便通过位操作来修改寄存器值。
|
||||
//==================================================================================
|
||||
|
||||
/** @name Command Register (0x00) 功能位 */
|
||||
#define BIT_STANDBY 0x80U // 置1使芯片进入待机模式
|
||||
#define BIT_MODEMOFF 0x40U // 关闭射频和调制解调器
|
||||
|
||||
/** @name FIFO-Control Register (0x02) 功能位 */
|
||||
#define BIT_FIFOSIZE 0x80U // FIFO大小选择 (0=64字节, 1=512字节)
|
||||
#define BIT_HIALERT 0x40U // 高水位中断标志
|
||||
#define BIT_LOALERT 0x20U // 低水位中断标志
|
||||
#define BIT_FIFOFLUSH 0x10U // FIFO刷新位。向此位写1可清空FIFO。
|
||||
#define BIT_WATERLEVEL_HI 0x04U
|
||||
#define MASK_FIFOLENGTH_HI 0x03U
|
||||
|
||||
/** @name IRQ0 Register (0x06) & IRQ0EN Register (0x08) 功能位 */
|
||||
#define BIT_IRQINV 0x80U // 反转IRQ引脚电平
|
||||
#define BIT_HIALERTIRQ 0x40U // FIFO高水位中断
|
||||
#define BIT_LOALERTIRQ 0x20U // FIFO低水位中断
|
||||
#define BIT_IDLEIRQ 0x10U // 空闲中断 (命令执行完毕)
|
||||
#define BIT_TXIRQ 0x08U // 发送完成中断
|
||||
#define BIT_RXIRQ 0x04U // 接收完成中断
|
||||
#define BIT_ERRIRQ 0x02U // 错误中断
|
||||
#define BIT_RXSOFIRQ 0x01U // 接收到起始帧(SOF)中断
|
||||
|
||||
/** @name IRQ1 Register (0x07) & IRQ1EN Register (0x09) 功能位 */
|
||||
#define BIT_IRQPUSHPULL 0x80U // IRQ引脚推挽/开漏模式选择
|
||||
#define BIT_GLOBALIRQ 0x40U // 全局中断使能
|
||||
#define BIT_LPCDIRQ 0x20U // 低功耗寻卡中断
|
||||
#define BIT_TIMER4IRQ 0x10U // 定时器4中断
|
||||
#define BIT_TIMER3IRQ 0x08U // 定时器3中断
|
||||
#define BIT_TIMER2IRQ 0x04U // 定时器2中断
|
||||
#define BIT_TIMER1IRQ 0x02U // 定时器1中断
|
||||
#define BIT_TIMER0IRQ 0x01U // 定时器0中断
|
||||
|
||||
/** @name Error Register (0x0A) 功能位 */
|
||||
#define BIT_CMDEE_ERR 0x80U // E2PROM命令错误
|
||||
#define BIT_FIFOWRERR 0x40U // FIFO写错误 (FIFO已满时写入)
|
||||
#define BIT_FIFOOVL 0x20U // FIFO溢出错误
|
||||
#define BIT_MINFRAMEERR 0x10U // 最小帧错误
|
||||
#define BIT_NODATAERR 0x08U // 无数据错误 (超时)
|
||||
#define BIT_COLLDET 0x04U // 检测到冲突错误
|
||||
#define BIT_PROTERR 0x02U // 协议错误
|
||||
#define BIT_INTEGERR 0x01U // CRC或奇偶校验错误
|
||||
|
||||
/** @name Status Register (0x0B) 功能位 */
|
||||
#define BIT_CRYPTO1ON 0x20U // MIFARE加密已开启标志位。认证成功后,此位由硬件自动置1。
|
||||
#define MASK_COMMSTATE 0x07U // 通信状态掩码
|
||||
|
||||
/** @name Rx-Bit-Control Register (0x0C) 功能位 */
|
||||
#define BIT_VALUESAFTERCOLL 0x80U // 冲突后接收到的位值
|
||||
#define BIT_NOCOLL 0x08U // 无冲突
|
||||
#define MASK_RXALIGN 0x70U // 接收位对齐掩码
|
||||
#define MASK_RXLASTBITS 0x07U // 最后一字节的有效位数
|
||||
|
||||
/** @name Tx-/Rx-CRC Control Register (0x2C/0x2D) 功能位 */
|
||||
#define BIT_CRCEN 0x01U // CRC使能位
|
||||
|
||||
/** @name Frame Control Register (0x33) 功能位 */
|
||||
#define BIT_TXPARITYEN 0x80U // 发送奇偶校验使能
|
||||
#define BIT_RXPARITYEN 0x40U // 接收奇偶校验使能
|
||||
|
||||
/** @name Rx-Analog Register (0x39) 功能位 */
|
||||
#define MASK_RCV_HPCF 0x0CU // 接收器高通滤波器截止频率
|
||||
#define MASK_RCV_GAIN 0x03U // 接收器增益
|
||||
|
||||
//==================================================================================
|
||||
// 4. 协议标识符定义 (Protocol Identifiers)
|
||||
// 用于 CMD_LOADPROTOCOL 命令,告诉芯片要使用哪种协议进行通信。
|
||||
//==================================================================================
|
||||
// --- 接收协议 ---
|
||||
#define RX_TYPEA_106 0 // ISO14443A, 106 kbit/s
|
||||
#define RX_TYPEA_212 1 // ISO14443A, 212 kbit/s
|
||||
#define RX_TYPEA_424 2 // ISO14443A, 424 kbit/s
|
||||
#define RX_TYPEA_848 3 // ISO14443A, 848 kbit/s
|
||||
#define RX_TYPEB_106 4 // ISO14443B, 106 kbit/s
|
||||
#define RX_TYPEB_212 5 // ISO14443B, 212 kbit/s
|
||||
#define RX_TYPEB_424 6 // ISO14443B, 424 kbit/s
|
||||
#define RX_TYPEB_848 7 // ISO14443B, 848 kbit/s
|
||||
#define RX_TYPEV_26 10 // ISO15693, 26 kbit/s
|
||||
#define RX_TYPEV_53 11 // ISO15693, 53 kbit/s
|
||||
#define RX_FELICA_212 19 // FeliCa, 212 kbit/s
|
||||
#define RX_FELICA_424 20 // FeliCa, 424 kbit/s
|
||||
|
||||
// --- 发送协议 ---
|
||||
#define TX_TYPEA_106 0 // ISO14443A, 106 kbit/s
|
||||
#define TX_TYPEA_212 1 // ISO14443A, 212 kbit/s
|
||||
#define TX_TYPEA_424 2 // ISO14443A, 424 kbit/s
|
||||
#define TX_TYPEA_848 3 // ISO14443A, 848 kbit/s
|
||||
#define TX_TYPEB_106 4 // ISO14443B, 106 kbit/s
|
||||
#define TX_TYPEB_212 5 // ISO14443B, 212 kbit/s
|
||||
#define TX_TYPEB_424 6 // ISO14443B, 424 kbit/s
|
||||
#define TX_TYPEB_848 7 // ISO14443B, 848 kbit/s
|
||||
#define TX_TYPEV_26 10 // ISO15693, 26 kbit/s
|
||||
#define TX_TYPEV_53 11 // ISO15693, 53 kbit/s
|
||||
#define TX_FELICA_212 19 // FeliCa, 212 kbit/s
|
||||
#define TX_FELICA_424 20 // FeliCa, 424 kbit/s
|
||||
|
||||
#endif
|
||||
294
apps/earphone/94_rfid_stc/READER/READER_commented.c
Normal file
294
apps/earphone/94_rfid_stc/READER/READER_commented.c
Normal file
@ -0,0 +1,294 @@
|
||||
/**
|
||||
* @file READER.c
|
||||
* @brief RFID 核心协议驱动实现
|
||||
* @version 1.0
|
||||
* @date 2024-05-20
|
||||
*
|
||||
* @par 文件作用:
|
||||
* 该文件是 READER.h 中声明的所有函数的具体实现。它是驱动库的核心逻辑所在,
|
||||
* 负责将上层应用的简单指令(如“寻卡”)转化为一系列复杂的、精确计时的
|
||||
* 寄存器读写操作。
|
||||
* 主要职责包括:
|
||||
* 1. 实现各协议的初始化函数,配置芯片进入相应的工作模式。
|
||||
* 2. 实现各协议的寻卡、防冲突、选卡等底层通信流程。
|
||||
* 3. 提供与芯片交互的基础原子操作函数 (SetCommand, Clear_FIFO 等)。
|
||||
*/
|
||||
|
||||
#include "function.h"
|
||||
#include "board.h"
|
||||
#include "READER.h"
|
||||
#include "READER_REG.h"
|
||||
#include "xt_main.h"
|
||||
#include <stdio.h>
|
||||
|
||||
// 定义全局变量,用于存储寻卡后得到的各类卡片信息
|
||||
struct picc_a_struct PICC_A;
|
||||
struct picc_b_struct PICC_B;
|
||||
struct picc_v_struct PICC_V;
|
||||
struct picc_f_struct PICC_F;
|
||||
|
||||
// (注: 此处的延时函数为软件延时,移植到新平台时建议使用定时器以获得更高精度)
|
||||
void DelayUs( uint32_t xus ) { /* ... */ }
|
||||
void DelayMs( uint32_t xms ) { /* ... */ }
|
||||
|
||||
|
||||
/**
|
||||
* @brief 修改寄存器的特定位 (Read-Modify-Write)
|
||||
* @param reg_address 要修改的寄存器地址
|
||||
* @param mask 要操作的位的掩码 (例如 0x01 表示操作 bit 0)
|
||||
* @param set 1 表示置位 (set),0 表示清零 (clear)
|
||||
*/
|
||||
void ModifyReg( unsigned char reg_address, unsigned char mask, unsigned char set )
|
||||
{
|
||||
unsigned char reg_data;
|
||||
GetReg( reg_address, ®_data ); // 1. 先读出寄存器当前的值
|
||||
|
||||
if ( set )
|
||||
{
|
||||
reg_data |= mask; // 2. 使用或运算,将指定位置1
|
||||
}
|
||||
else
|
||||
{
|
||||
reg_data &= ~mask; // 3. 使用与和非运算,将指定位清0
|
||||
}
|
||||
|
||||
SetReg( reg_address, reg_data ); // 4. 将修改后的值写回寄存器
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 向芯片发送一个命令
|
||||
* @param command 要发送的命令码 (定义于 READER_REG.h, 如 CMD_TRANSCEIVE)
|
||||
* @return unsigned char 操作结果
|
||||
*/
|
||||
unsigned char SetCommand(unsigned char command)
|
||||
{
|
||||
return SetReg(REG_COMMAND, CMD_MASK & command);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 开启或关闭天线载波 (射频场)
|
||||
* @param mode ENABLE 或 DISABLE
|
||||
* @return unsigned char 操作结果
|
||||
*/
|
||||
unsigned char SetCW(unsigned char mode)
|
||||
{
|
||||
if(mode == ENABLE)
|
||||
{
|
||||
// 通过清除 MODEMOFF 位并设置 TXEN 位来开启天线
|
||||
ModifyReg(REG_COMMAND, BIT_MODEMOFF, DISABLE);
|
||||
ModifyReg(REG_TXMODE, BIT0 | BIT1, ENABLE);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 通过设置 MODEMOFF 位来关闭天线
|
||||
ModifyReg(REG_COMMAND, BIT_MODEMOFF, ENABLE);
|
||||
ModifyReg(REG_TXMODE, BIT0 | BIT1, DISABLE);
|
||||
}
|
||||
DelayMs(5);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 清空芯片内部的 FIFO 缓冲区
|
||||
*/
|
||||
void Clear_FIFO(void)
|
||||
{
|
||||
unsigned char fifolength;
|
||||
GetReg(REG_FIFOLENGTH, &fifolength); // 读取FIFO中当前字节数
|
||||
if((fifolength) != 0)
|
||||
{
|
||||
// 如果FIFO不为空,通过置位 FIFOFLUSH 来清空
|
||||
ModifyReg(REG_FIFOCONTROL, BIT_FIFOFLUSH, ENABLE);
|
||||
}
|
||||
return ;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 加载通信协议到芯片
|
||||
* @param p_rx 接收协议标识符 (来自 READER_REG.h)
|
||||
* @param p_tx 发送协议标识符 (来自 READER_REG.h)
|
||||
* @return unsigned char 成功或失败
|
||||
*/
|
||||
unsigned char LoadProtocol(unsigned char p_rx, unsigned char p_tx)
|
||||
{
|
||||
unsigned char reg_data = 0;
|
||||
SetCommand(CMD_IDLE);
|
||||
Clear_FIFO();
|
||||
SetReg(REG_FIFODATA, p_rx); // 将接收协议标识符写入FIFO
|
||||
SetReg(REG_FIFODATA, p_tx); // 将发送协议标识符写入FIFO
|
||||
SetCommand(CMD_LOADPROTOCOL); // 发送加载协议命令
|
||||
DelayMs(2);
|
||||
GetReg(REG_COMMAND, ®_data);
|
||||
if(reg_data != CMD_IDLE) // 检查命令是否执行完毕
|
||||
return FAIL;
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 设置是否启用奇偶校验
|
||||
* @param state ENABLE 或 DISABLE
|
||||
*/
|
||||
void SetParity(unsigned char state)
|
||||
{
|
||||
ModifyReg(REG_FRAMECON, BIT_TXPARITYEN | BIT_RXPARITYEN, state);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 初始化芯片以进行 ISO14443-A 通信
|
||||
* @return unsigned char 成功
|
||||
*/
|
||||
unsigned char ReaderA_Initial(void)
|
||||
{
|
||||
LoadProtocol(RX_TYPEA_106, TX_TYPEA_106); // 1. 加载Type A协议
|
||||
ModifyReg(REG_TXMODE, BIT2, ENABLE); // 2. 设置为100% ASK调制
|
||||
SetReg(REG_TXAMP, AMPLITUDE_A); // 3. 设置天线发射功率
|
||||
SetReg(REG_RXANA, (HPCF_A << 3) | GAIN_A); // 4. 设置接收器增益和高通滤波器
|
||||
SetParity(ENABLE); // 5. Type A 需要奇偶校验
|
||||
ModifyReg(REG_STATUS, BIT_CRYPTO1ON, 0); // 6. 清除MIFARE加密标志
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 初始化芯片以进行 ISO14443-B 通信
|
||||
* @return unsigned char 成功
|
||||
*/
|
||||
unsigned char ReaderB_Initial(void)
|
||||
{
|
||||
LoadProtocol(RX_TYPEB_106, TX_TYPEB_106);
|
||||
// ... 配置Type B特定的寄存器 ...
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
// ... 其他协议的初始化函数 (ReaderV_Initial, ReaderF_Initial) ...
|
||||
|
||||
/**
|
||||
* @brief (Type A) 发送 REQA 命令
|
||||
* @param picc_a 指向 picc_a_struct 的指针,用于存储响应
|
||||
* @return unsigned char 成功或失败
|
||||
*/
|
||||
unsigned char ReaderA_Request(struct picc_a_struct *picc_a)
|
||||
{
|
||||
unsigned char reg_data;
|
||||
SetCommand(CMD_IDLE);
|
||||
Clear_FIFO();
|
||||
SetReg(REG_FIFODATA, RF_CMD_REQA); // 1. 将 REQA 命令码 (0x26) 写入FIFO
|
||||
ModifyReg(REG_TXCRCCON, BIT_CRCEN, DISABLE); // 2. REQA 命令不带CRC
|
||||
ModifyReg(REG_RXCRCCON, BIT_CRCEN, DISABLE);
|
||||
SetCommand(CMD_TRANSCEIVE); // 3. 发送并等待接收
|
||||
DelayMs(2);
|
||||
GetReg(REG_FIFOLENGTH, ®_data); // 4. 检查接收到的数据长度
|
||||
if(reg_data != 2) // 5. 成功的响应 (ATQA) 应该是2个字节
|
||||
return FAIL;
|
||||
|
||||
GetReg(REG_FIFODATA, &picc_a->ATQA[0]); // 6. 从FIFO读出ATQA
|
||||
GetReg(REG_FIFODATA, &picc_a->ATQA[1]);
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief (Type A) 执行一级防冲突
|
||||
* @param picc_a 指向 picc_a_struct 的指针
|
||||
* @return unsigned char 成功或失败
|
||||
*/
|
||||
unsigned char ReaderA_Anticoll(struct picc_a_struct *picc_a)
|
||||
{
|
||||
unsigned char reg_data;
|
||||
SetCommand(CMD_IDLE);
|
||||
Clear_FIFO();
|
||||
// 1. 准备防冲突命令 (如 0x93 0x20)
|
||||
SetReg(REG_FIFODATA, RF_CMD_ANTICOLL[picc_a->CASCADE_LEVEL]);
|
||||
SetReg(REG_FIFODATA, 0x20);
|
||||
SetCommand(CMD_TRANSCEIVE);
|
||||
DelayMs(2);
|
||||
GetReg(REG_FIFOLENGTH, ®_data);
|
||||
if(reg_data != 5) // 2. 响应应为5字节 (4字节UID + 1字节BCC)
|
||||
return FAIL;
|
||||
|
||||
// 3. 从FIFO读出UID和BCC
|
||||
GetReg(REG_FIFODATA, &picc_a->UID[picc_a->CASCADE_LEVEL*4]);
|
||||
// ...
|
||||
GetReg(REG_FIFODATA, &picc_a->BCC[picc_a->CASCADE_LEVEL]);
|
||||
|
||||
// 4. 校验BCC (UID四个字节的异或值)
|
||||
if( (picc_a->UID[picc_a->CASCADE_LEVEL*4] ^ picc_a->UID[picc_a->CASCADE_LEVEL*4+1] ^ picc_a->UID[picc_a->CASCADE_LEVEL*4+2] ^ picc_a->UID[picc_a->CASCADE_LEVEL*4+3]) == picc_a->BCC[picc_a->CASCADE_LEVEL])
|
||||
return SUCCESS;
|
||||
|
||||
return FAIL;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief (Type A) 选卡命令
|
||||
* @param picc_a 指向 picc_a_struct 的指针
|
||||
* @return unsigned char 成功或失败
|
||||
*/
|
||||
unsigned char ReaderA_Select(struct picc_a_struct *picc_a)
|
||||
{
|
||||
unsigned char reg_data;
|
||||
SetCommand(CMD_IDLE);
|
||||
Clear_FIFO();
|
||||
// 1. 准备选卡命令 (如 0x93 0x70 + UID + BCC)
|
||||
SetReg(REG_FIFODATA, RF_CMD_SELECT[picc_a->CASCADE_LEVEL]);
|
||||
SetReg(REG_FIFODATA, 0x70);
|
||||
SetReg(REG_FIFODATA, picc_a->UID[picc_a->CASCADE_LEVEL*4]);
|
||||
// ...
|
||||
SetReg(REG_FIFODATA, picc_a->BCC[picc_a->CASCADE_LEVEL]);
|
||||
ModifyReg(REG_TXCRCCON, BIT_CRCEN, ENABLE); // 2. 选卡命令带CRC
|
||||
ModifyReg(REG_RXCRCCON, BIT_CRCEN, ENABLE);
|
||||
SetCommand(CMD_TRANSCEIVE);
|
||||
DelayMs(2);
|
||||
GetReg(REG_ERROR, ®_data); // 3. 检查是否有通信错误
|
||||
if((reg_data & 0x0F) != 0)
|
||||
return FAIL;
|
||||
GetReg(REG_FIFOLENGTH, ®_data);
|
||||
if(reg_data != 1) // 4. 响应为1字节的SAK
|
||||
return FAIL;
|
||||
GetReg(REG_FIFODATA, &picc_a->SAK[picc_a->CASCADE_LEVEL]); // 5. 读出SAK
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief **核心函数**: 激活一张A类卡 (完成完整的寻卡、防冲突、选卡流程)
|
||||
* @param picc_a 指向 picc_a_struct 的指针
|
||||
* @return unsigned char 成功或失败
|
||||
*/
|
||||
unsigned char ReaderA_CardActivate(struct picc_a_struct *picc_a)
|
||||
{
|
||||
unsigned char result, cascade_level;
|
||||
result = ReaderA_Request(picc_a); // 1. 发送请求
|
||||
if (result != SUCCESS)
|
||||
return result;
|
||||
|
||||
// 2. 根据ATQA判断UID长度,确定级联等级
|
||||
if ((picc_a->ATQA[0] & 0xC0) == 0x00)
|
||||
{
|
||||
cascade_level = 1;
|
||||
picc_a->UID_Length = 4;
|
||||
}
|
||||
else if ((picc_a->ATQA[0] & 0xC0) == 0x40)
|
||||
{
|
||||
cascade_level = 2;
|
||||
picc_a->UID_Length = 7; // (修正: 通常是7字节)
|
||||
}
|
||||
// ...
|
||||
|
||||
// 3. 循环执行防冲突和选卡,直到获得完整的UID
|
||||
for (picc_a->CASCADE_LEVEL = 0; picc_a->CASCADE_LEVEL < cascade_level; picc_a->CASCADE_LEVEL++)
|
||||
{
|
||||
result = ReaderA_Anticoll(picc_a);
|
||||
if (result != SUCCESS)
|
||||
return result;
|
||||
|
||||
result = ReaderA_Select(picc_a);
|
||||
if (result != SUCCESS)
|
||||
return result;
|
||||
|
||||
// 如果SAK指示还有下一级UID,则继续循环
|
||||
if (!(picc_a->SAK[picc_a->CASCADE_LEVEL] & 0x04))
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// ... (其他协议 Type B, V, F 的函数实现) ...
|
||||
147
apps/earphone/94_rfid_stc/READER/READER_commented.h
Normal file
147
apps/earphone/94_rfid_stc/READER/READER_commented.h
Normal file
@ -0,0 +1,147 @@
|
||||
/**
|
||||
* @file READER.h
|
||||
* @brief RFID 核心协议驱动接口
|
||||
* @version 1.0
|
||||
* @date 2024-05-20
|
||||
*
|
||||
* @par 文件作用:
|
||||
* 该文件是核心协议驱动层(READER.c)的头文件,为上层应用(如MIFARE, NTAG)
|
||||
* 提供了标准化的接口来与不同协议的RFID卡片进行底层交互。
|
||||
* 它主要负责:
|
||||
* 1. 定义存储不同类型卡片信息的数据结构 (如 picc_a_struct)。
|
||||
* 2. 声明协议操作函数,如初始化、寻卡、选择、激活等。
|
||||
* 3. 定义通信过程中使用的标准命令码和硬件配置参数。
|
||||
* 上层应用通过调用本文件中声明的函数,可以完成对卡片的寻卡和选卡操作,
|
||||
* 从而为后续的认证和数据读写做准备。
|
||||
*/
|
||||
#ifndef _READER_H
|
||||
#define _READER_H
|
||||
|
||||
#include "function.h"
|
||||
|
||||
//==================================================================================
|
||||
// 1. ISO14443-A 标准命令码定义
|
||||
//==================================================================================
|
||||
static const unsigned char RF_CMD_REQA = 0x26; // REQA (Request Type A) 命令: 用于请求视场内的A类卡响应
|
||||
static const unsigned char RF_CMD_WUPA = 0x52; // WUPA (Wake-Up Type A) 命令: 用于唤醒视场内处于HALT状态的A类卡
|
||||
static const unsigned char RF_CMD_ANTICOLL[3] = {0x93, 0x95, 0x97}; // 防冲突命令码 (级联等级 1, 2, 3)
|
||||
static const unsigned char RF_CMD_SELECT[3] = {0x93, 0x95, 0x97}; // 选卡命令码 (级联等级 1, 2, 3)
|
||||
|
||||
//==================================================================================
|
||||
// 2. MIFARE Classic 命令码定义
|
||||
//==================================================================================
|
||||
static const unsigned char RF_CMD_KEYA = 0x60; // MIFARE Key A 认证命令
|
||||
static const unsigned char RF_CMD_KEYB = 0x61; // MIFARE Key B 认证命令
|
||||
|
||||
//==================================================================================
|
||||
// 3. 卡片信息数据结构定义
|
||||
// 用于存储从不同类型卡片读取到的信息。
|
||||
//==================================================================================
|
||||
|
||||
/** @brief ISO14443-B 卡片信息结构体 */
|
||||
struct picc_b_struct
|
||||
{
|
||||
unsigned char ATQB[12]; // REQB命令的响应 (Answer to Request B)
|
||||
unsigned char PUPI[4]; // 伪唯一PICCs标识符
|
||||
// ... 其他Type B卡片信息
|
||||
};
|
||||
extern struct picc_b_struct PICC_B; // 声明一个全局变量以存储Type B卡信息
|
||||
|
||||
/** @brief ISO14443-A 卡片信息结构体 */
|
||||
struct picc_a_struct
|
||||
{
|
||||
unsigned char ATQA[2]; // REQA命令的响应 (Answer to Request A)
|
||||
unsigned char CASCADE_LEVEL; // 级联等级 (表示UID的长度)
|
||||
unsigned char UID_Length; // UID 长度 (4, 7 或 10字节)
|
||||
unsigned char UID[15]; // 存储卡片的唯一ID (UID)
|
||||
unsigned char BCC[3]; // UID的校验字节
|
||||
unsigned char SAK[3]; // 选卡确认 (Select Acknowledge), 用于判断卡片具体类型
|
||||
};
|
||||
extern struct picc_a_struct PICC_A; // 声明一个全局变量以存储Type A卡信息
|
||||
|
||||
/** @brief ISO15693 (Type V) 卡片信息结构体 */
|
||||
struct picc_v_struct
|
||||
{
|
||||
unsigned char UID[8]; // Type V 卡的UID (8字节)
|
||||
unsigned char RESPONSE; // 卡片的响应字节
|
||||
unsigned char BLOCK_DATA[4]; // 用于存储从卡片读取的块数据
|
||||
};
|
||||
extern struct picc_v_struct PICC_V;
|
||||
|
||||
/** @brief FeliCa (Type F) 卡片信息结构体 */
|
||||
struct picc_f_struct
|
||||
{
|
||||
unsigned char UID[8]; // Type F 卡的UID
|
||||
};
|
||||
extern struct picc_f_struct PICC_F;
|
||||
|
||||
//==================================================================================
|
||||
// 4. 硬件配置参数宏定义
|
||||
// 这些参数用于在初始化时配置RFID芯片的模拟电路,以优化特定协议的通信性能。
|
||||
//==================================================================================
|
||||
// --- TYPE A 接收参数 ---
|
||||
#define GAIN_A 7 // 接收增益 (范围 0~7)
|
||||
#define HPCF_A 3 // 高通滤波器截止频率 (范围 0~7)
|
||||
#define AMPLITUDE_A 255 // 发射信号幅度/功率 (范围 0~255)
|
||||
|
||||
// --- TYPE B 接收参数 ---
|
||||
#define GAIN_B 7
|
||||
#define HPCF_B 3
|
||||
#define AMPLITUDE_B 255
|
||||
#define MODULATION_B 100 // 调制深度 (范围 0~255)
|
||||
|
||||
// ... (其他协议的参数定义) ...
|
||||
|
||||
//==================================================================================
|
||||
// 5. MCU引脚定义 (移植时需要重点关注)
|
||||
// 定义了MCU与RFID芯片连接的SPI引脚和控制引脚。
|
||||
//==================================================================================
|
||||
#define SPI_GPIO GPIOB
|
||||
#define SCK_PIN LL_GPIO_Pin1 // PB1
|
||||
#define MISO_PIN LL_GPIO_Pin2 // PB2
|
||||
#define MOSI_PIN LL_GPIO_Pin3 // PB3
|
||||
#define NSS_PIN LL_GPIO_Pin0 // PB0 (片选)
|
||||
|
||||
#define PD_GPIO GPIOA
|
||||
#define PD_PIN LL_GPIO_Pin7 // PA7 (Power Down引脚)
|
||||
|
||||
// ... (GPIO电平控制宏) ...
|
||||
|
||||
//==================================================================================
|
||||
// 6. 函数原型声明
|
||||
// 声明了由 READER.c 实现的函数,供其他模块调用。
|
||||
//==================================================================================
|
||||
|
||||
// --- 基础控制函数 ---
|
||||
extern unsigned char FM176XX_HardReset(void); // 硬复位RFID芯片
|
||||
extern void ModifyReg(unsigned char reg_address, unsigned char mask, unsigned char set); // 修改寄存器的特定位
|
||||
extern void Clear_FIFO(void); // 清空FIFO缓冲区
|
||||
extern unsigned char SetCommand(unsigned char command); // 向芯片发送命令
|
||||
extern unsigned char SetCW(unsigned char mode); // 开启或关闭天线载波
|
||||
|
||||
// --- 协议初始化函数 ---
|
||||
extern unsigned char ReaderA_Initial(void); // 初始化芯片以进行Type A通信
|
||||
extern unsigned char ReaderB_Initial(void); // 初始化芯片以进行Type B通信
|
||||
extern unsigned char ReaderV_Initial(void); // 初始化芯片以进行Type V通信
|
||||
extern unsigned char ReaderF_Initial(void); // 初始化芯片以进行Type F通信
|
||||
|
||||
// --- Type A 卡操作函数 ---
|
||||
extern unsigned char ReaderA_Request(struct picc_a_struct *picc_a); // 发送 REQA 命令
|
||||
extern unsigned char ReaderA_Anticoll(struct picc_a_struct *picc_a); // 执行防冲突
|
||||
extern unsigned char ReaderA_Select(struct picc_a_struct *picc_a); // 选卡
|
||||
extern unsigned char ReaderA_CardActivate(struct picc_a_struct *picc_a); // **核心函数**: 完成A类卡的寻卡、防冲突和选卡全过程,获取UID
|
||||
|
||||
// --- Type B 卡操作函数 ---
|
||||
extern unsigned char ReaderB_Request(struct picc_b_struct *picc_b); // 发送 REQB 命令
|
||||
extern unsigned char ReaderB_Attrib(struct picc_b_struct *picc_b); // 设置卡片参数
|
||||
// ...
|
||||
|
||||
// --- Type V 卡操作函数 ---
|
||||
extern unsigned char ReaderV_Inventory(struct picc_v_struct *picc_v); // 盘点/寻卡
|
||||
extern unsigned char ReaderV_ReadSingleBlock(unsigned char block_num, struct picc_v_struct *picc_v); // 读单个块
|
||||
extern unsigned char ReaderV_WriteSingleBlock(unsigned char block_num, struct picc_v_struct *picc_v); // 写单个块
|
||||
|
||||
// --- Type F 卡操作函数 ---
|
||||
extern unsigned char ReaderF_Inventory(struct picc_f_struct *picc_f); // 盘点/寻卡
|
||||
|
||||
#endif
|
||||
192
apps/earphone/94_rfid_stc/board.c
Normal file
192
apps/earphone/94_rfid_stc/board.c
Normal file
@ -0,0 +1,192 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file board.c
|
||||
* @author SRG
|
||||
* @version V1.0.0
|
||||
* @date 2020-03-17
|
||||
* @brief
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* <h2><center>© Copyright (c) 2020 FudanMicroelectronics.
|
||||
* All rights reserved.</center></h2>
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
#include "board.h"
|
||||
#include "READER.h"
|
||||
#include "fm15l0xx.h"
|
||||
#include "fm15l0xx_ll_cmu.h"
|
||||
#include "fm15l0xx_ll_gpio.h"
|
||||
#include "fm15l0xx_ll_uart.h"
|
||||
#include "fm15l0xx_ll_spi.h"
|
||||
#include "fm15l0xx_ll_svd.h"
|
||||
#include "fm15l0xx_ll_rng.h"
|
||||
|
||||
|
||||
|
||||
// <20>ж<EFBFBD>VDD<44>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
|
||||
int Vdd_is_Low( void )
|
||||
{
|
||||
return (!LL_SVD_IsActiveFlag_SVDR() );
|
||||
}
|
||||
|
||||
void SVD_Init( void )
|
||||
{
|
||||
LL_SVD_EnableSVDDigitalFilter(); // ʹ<><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˲<EFBFBD><CBB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>뿪<EFBFBD><EBBFAA>
|
||||
LL_SVD_SetSVDMode( LL_SVD_SVDMOD_INTERVAL_ON ); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
|
||||
LL_SVD_SetSVDInterval( LL_SVD_SVDITVL_62p5ms ); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>62.5ms
|
||||
|
||||
LL_SVD_DisableV1P0(); // <20>ر<EFBFBD>1V<31><56><EFBFBD><D7BC>ѹ
|
||||
LL_SVD_EnableV0P95(); // ʹ<>û<EFBFBD><EFBFBD><D7BC>ѹ<EFBFBD><D1B9>0.95V
|
||||
LL_SVD_SetSVDLevel( LL_SVD_SVDLVL_LEVEL10 ); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD>ȼ<EFBFBD>10 - <20><>ֵ3.614V
|
||||
|
||||
LL_SVD_EnableSVD();
|
||||
}
|
||||
|
||||
|
||||
void RNG_Init( void )
|
||||
{
|
||||
LL_CMU_EnablePeriphBusClock_RNG(); // Enable bus clock(RCHF)
|
||||
LL_CMU_EnableClock_RNG(); // Enable RNG work clock
|
||||
LL_CMU_SetClockPrescaler_RNG( LL_CMU_PERPRSC_2 ); // 4MHz: RCHF / 2<><32>ʱ<EFBFBD><CAB1>Ƶ<EFBFBD>ʹ<EFBFBD><CAB9>ᵼ<DFBB><E1B5BC>SFAIL<49><4C><EFBFBD><EFBFBD>
|
||||
|
||||
LL_RNG_EnableNoiseSource();
|
||||
LL_RNG_Enable();
|
||||
}
|
||||
|
||||
|
||||
uint32_t Get_RandomNumber( void )
|
||||
{
|
||||
while ( 1 )
|
||||
{
|
||||
if ( LL_RNG_IsDataReady() || LL_RNG_IsActiveFlag_SFAIL() )
|
||||
{
|
||||
if ( LL_RNG_IsActiveFlag_SFAIL() )
|
||||
{
|
||||
// <20><><EFBFBD>մ<EFBFBD><D5B4><EFBFBD><EFBFBD><EFBFBD>־
|
||||
LL_RNG_ClearFlag_SFAIL();
|
||||
LL_RNG_GetRandomData();
|
||||
}
|
||||
else
|
||||
{
|
||||
break; // <20><>ȡ<EFBFBD>ɹ<EFBFBD>
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (LL_RNG_GetRandomData());
|
||||
}
|
||||
|
||||
|
||||
/*********************************************************************************************************
|
||||
** <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: GPIO_NVIC_Init
|
||||
** <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: <09><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ŵ<EFBFBD><C5B5>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD>
|
||||
** <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: <09><>
|
||||
** <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: <09><>
|
||||
** <20><><EFBFBD><EFBFBD>ֵ: <09><>
|
||||
*********************************************************************************************************/
|
||||
void GPIO_NVIC_Init( void )
|
||||
{
|
||||
LL_CMU_EnableClock_EXTI();
|
||||
|
||||
//<2F><><EFBFBD><EFBFBD>оƬIRQ<52>ж<EFBFBD>
|
||||
LL_EXTI_SetEXTISelection( LL_EXTI_LINE_0, LL_EXTI_SELECTION_3 ); // PA3
|
||||
LL_EXTI_SetEXTIEdgeTrig( LL_EXTI_LINE_0, LL_EXTI_EDS_FALLING ); // Falling
|
||||
|
||||
//FM17<31><37><EFBFBD><EFBFBD>оƬIRQ<52>ж<EFBFBD>
|
||||
LL_EXTI_SetEXTISelection( LL_EXTI_LINE_1, LL_EXTI_SELECTION_3 ); // PA7
|
||||
LL_EXTI_SetEXTIEdgeTrig( LL_EXTI_LINE_1, LL_EXTI_EDS_FALLING ); // Falling
|
||||
|
||||
NVIC_EnableIRQ( GPIO_IRQn );
|
||||
}
|
||||
|
||||
|
||||
void GPIO_Init( void )
|
||||
{
|
||||
LL_CMU_EnablePeriphBusClock_GPIO(); // Bus clock enable
|
||||
|
||||
LL_GPIO_SetPinMode( GPIOA, LL_GPIO_Pin12, LL_GPIO_PINxMODE_DIGITAL ); // PA12 Digital function - UART1 RX
|
||||
LL_GPIO_SetPinDigitalFunc( GPIOA, LL_GPIO_Pin12, LL_GPIO_PinDFS0 );
|
||||
LL_GPIO_SetPinMode( GPIOA, LL_GPIO_Pin13, LL_GPIO_PINxMODE_DIGITAL ); // PA13 Digital function - UART1 TX
|
||||
LL_GPIO_SetPinDigitalFunc( GPIOA, LL_GPIO_Pin13, LL_GPIO_PinDFS0 );
|
||||
|
||||
|
||||
LL_GPIO_SetPinMode( LED_GPIO, LED1_PIN, LL_GPIO_PINxMODE_OUTPUT );
|
||||
LL_GPIO_SetPinMode( LED_GPIO, LED2_PIN, LL_GPIO_PINxMODE_OUTPUT );
|
||||
|
||||
LED1_On;
|
||||
LED2_On;
|
||||
DelayMs(200);
|
||||
LED1_Off;
|
||||
LED2_Off;
|
||||
|
||||
}
|
||||
|
||||
|
||||
void Uart_Init( void )
|
||||
{
|
||||
/* Bus clock enable */
|
||||
LL_CMU_EnablePeriphBusClock_UART1();
|
||||
|
||||
LL_CMU_SelectClock_UART1( LL_CMU_HFPER_CLK_APBCLKx ); // Clock source select
|
||||
LL_CMU_EnableClock_UART1(); // Operation clock enable
|
||||
|
||||
LL_UART_SetBaudRate( UART1, 412 ); // Set baudrate to 115200bps, reg = (uclk / baudrate) - 1
|
||||
LL_UART_SetDataWidth( UART1, LL_UART_DATA_WIDTH_8bit ); // 8 bit
|
||||
LL_UART_SetStopBitsLength( UART1, LL_UART_STOPCFG_1bit ); // 1 stop bit
|
||||
LL_UART_SetParity( UART1, LL_UART_PARITY_NONE ); // Parity none
|
||||
|
||||
LL_UART_EnableTx( UART1 ); // Tx enable
|
||||
LL_UART_EnableRx( UART1 ); // Rx enable
|
||||
}
|
||||
|
||||
|
||||
void uart_send_byte( UART_Typedef *uart, uint16_t c )
|
||||
{
|
||||
while ( !LL_UART_IsActiveFlag_TXBE( uart ) )
|
||||
;
|
||||
LL_UART_TransmitData( uart, (uint32_t) c );
|
||||
}
|
||||
|
||||
|
||||
/* Retarget fputc, then you can use printf. */
|
||||
int fputc( int ch, FILE *f )
|
||||
{
|
||||
uart_send_byte( UART1, ch );
|
||||
return (ch);
|
||||
}
|
||||
|
||||
void SPI_Init( SPI_Typedef *spi )
|
||||
{
|
||||
// Bus clock enable
|
||||
if ( spi == SPI1 )
|
||||
LL_CMU_EnablePeriphBusClock_SPI1();
|
||||
else if ( spi == SPI2 )
|
||||
LL_CMU_EnablePeriphBusClock_SPI2();
|
||||
|
||||
LL_SPI_DisableCtrlSSN( spi ); // Disable Software Control
|
||||
//LL_SPI_SetSSNOutput( spi, 1 ); // Set SSN High
|
||||
|
||||
LL_SPI_Disable( spi ); // Disable SPI
|
||||
LL_SPI_SetWorkMode( spi, LL_SPI_WORKMODE_MASTER ); // Mode select - master
|
||||
LL_SPI_SetBaudrate( spi, LL_SPI_BAUDRATE_DIV_4 ); // Baudrate Select - fPCLK/16
|
||||
LL_SPI_SetFrameFormat(spi,LL_SPI_FRAME_FMT_MSB);
|
||||
LL_SPI_SetClockPolarity(spi,LL_SPI_CPOL_NEGATIVE);
|
||||
LL_SPI_SetCommMode(spi,LL_SPI_COMM_MODE_STANDARD);
|
||||
LL_SPI_SetDataLength(spi,LL_SPI_DATA_LENGTH_8bit);
|
||||
LL_SPI_DisableHalfDuplexDummyCycle(spi);
|
||||
LL_SPI_Enable( spi ); // Enable SPI
|
||||
}
|
||||
|
||||
|
||||
void BSP_Init( void )
|
||||
{
|
||||
GPIO_Init();
|
||||
Uart_Init();
|
||||
//SPI_Init( SPI1);
|
||||
SVD_Init();
|
||||
RNG_Init();
|
||||
}
|
||||
|
||||
70
apps/earphone/94_rfid_stc/bsp.c
Normal file
70
apps/earphone/94_rfid_stc/bsp.c
Normal file
@ -0,0 +1,70 @@
|
||||
#include <stdio.h>
|
||||
#include "bsp.h"
|
||||
|
||||
void gpio_init( void )
|
||||
{
|
||||
LL_CMU_EnablePeriphBusClock_GPIO(); // Bus clock enable
|
||||
|
||||
LL_GPIO_SetPinMode( GPIOA, LL_GPIO_Pin0, LL_GPIO_PINxMODE_DIGITAL ); // PA0 Digital function - UART0 RX
|
||||
LL_GPIO_SetPinDigitalFunc( GPIOA, LL_GPIO_Pin0, LL_GPIO_PinDFS0 );
|
||||
LL_GPIO_SetPinMode( GPIOA, LL_GPIO_Pin1, LL_GPIO_PINxMODE_DIGITAL ); // PA1 Digital function - UART0 TX
|
||||
LL_GPIO_SetPinDigitalFunc( GPIOA, LL_GPIO_Pin1, LL_GPIO_PinDFS0 );
|
||||
}
|
||||
|
||||
|
||||
void uart_init( void )
|
||||
{
|
||||
/* Bus clock enable */
|
||||
LL_CMU_EnablePeriphBusClock_UART0();
|
||||
|
||||
LL_CMU_SelectClock_UART0( LL_CMU_HFPER_CLK_APBCLKx ); // Clock source select
|
||||
LL_CMU_EnableClock_UART0(); // Operation clock enable
|
||||
|
||||
LL_UART_SetBaudRate( UARTx, 412 ); // Set baudrate to 115200bps, reg = (uclk / baudrate) - 1
|
||||
LL_UART_SetDataWidth( UARTx, LL_UART_DATA_WIDTH_8bit ); // 8 bit
|
||||
LL_UART_SetStopBitsLength( UARTx, LL_UART_STOPCFG_1bit ); // 1 stop bit
|
||||
LL_UART_SetParity( UARTx, LL_UART_PARITY_NONE ); // Parity none
|
||||
|
||||
LL_UART_EnableTx( UARTx ); // Tx enable
|
||||
LL_UART_EnableRx( UARTx ); // Rx enable
|
||||
}
|
||||
|
||||
|
||||
void uart_send_byte( UART_Typedef *uart, uint16_t c )
|
||||
{
|
||||
while ( !LL_UART_IsActiveFlag_TXBE( uart ) )
|
||||
;
|
||||
LL_UART_TransmitData( uart, (uint32_t) c );
|
||||
}
|
||||
|
||||
|
||||
/* Retarget fputc, then you can use printf. */
|
||||
int fputc( int ch, FILE *f )
|
||||
{
|
||||
uart_send_byte( UARTx, ch );
|
||||
return (ch);
|
||||
}
|
||||
|
||||
|
||||
void svd_init( void )
|
||||
{
|
||||
LL_SVD_EnableSVDDigitalFilter(); // ʹ<><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˲<EFBFBD><CBB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>뿪<EFBFBD><EBBFAA>
|
||||
LL_SVD_SetSVDMode( LL_SVD_SVDMOD_INTERVAL_ON ); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
|
||||
LL_SVD_SetSVDInterval( LL_SVD_SVDITVL_62p5ms ); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>62.5ms
|
||||
|
||||
LL_SVD_DisableV1P0(); // <20>ر<EFBFBD>1V<31><56><EFBFBD><D7BC>ѹ
|
||||
LL_SVD_EnableV0P95(); // ʹ<>û<EFBFBD><EFBFBD><D7BC>ѹ<EFBFBD><D1B9>0.95V
|
||||
LL_SVD_SetSVDLevel( LL_SVD_SVDLVL_LEVEL10 ); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD>ȼ<EFBFBD>10 - <20><>ֵ3.614V
|
||||
|
||||
LL_SVD_EnableSVD();
|
||||
}
|
||||
|
||||
|
||||
void bsp_init( void )
|
||||
{
|
||||
gpio_init();
|
||||
uart_init();
|
||||
svd_init();
|
||||
}
|
||||
|
||||
613
apps/earphone/94_rfid_stc/decode_printcipher.c
Normal file
613
apps/earphone/94_rfid_stc/decode_printcipher.c
Normal file
@ -0,0 +1,613 @@
|
||||
#include <stdio.h>
|
||||
#include "print.h"
|
||||
#include "READER.h"
|
||||
#include "random_generator.h"
|
||||
unsigned char ciphertext_to_binary[48] ; //用来存储算法逆运算每轮的中间数据 需要定义成全局变量
|
||||
unsigned char lsfr[48];
|
||||
unsigned char lsfr_length;
|
||||
unsigned char decode_printcipher( unsigned char ciphertext[] , unsigned char long_key[] , unsigned char short_key[] )//解密函数
|
||||
{
|
||||
unsigned char a[6][8];
|
||||
unsigned char i , j ;
|
||||
unsigned char t ;
|
||||
unsigned char k ; //k是用在sbox中的
|
||||
unsigned char num ; //主循环
|
||||
unsigned char long_key_to_binary[48] ;
|
||||
unsigned char temp[48] ;
|
||||
unsigned char short_key_to_binary[32] ;
|
||||
unsigned char rev_counter[6] = {1,0,0,1,0,0};
|
||||
unsigned char temp_rev_counter[6];
|
||||
|
||||
|
||||
//----------------------------------ciphertext-------------------------------------------------//
|
||||
for(i=0 ; i<6 ; ++i) //
|
||||
{ //
|
||||
for(j=0 ; j<8 ; ++j) //
|
||||
{ // 该作用是把密文以二进制保存在数组中ciphertext
|
||||
a[i][8-1-j] = ciphertext[i] % 2 ; // a[7]应该是二进制最低位,a[0]最高位
|
||||
ciphertext[i] /= 2 ; //
|
||||
} //
|
||||
} //
|
||||
|
||||
for(i=0 ; i<6 ; ++i)
|
||||
{
|
||||
for(j=0 ; j<8 ; ++j)
|
||||
{
|
||||
ciphertext_to_binary[i*8+j] = a[i][j] ;
|
||||
}
|
||||
}
|
||||
|
||||
//printf("%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x \n",ciphertext_to_binary[0],ciphertext_to_binary[1],ciphertext_to_binary[2],\
|
||||
ciphertext_to_binary[3],ciphertext_to_binary[4],ciphertext_to_binary[5],ciphertext_to_binary[6],ciphertext_to_binary[7],\
|
||||
ciphertext_to_binary[40],ciphertext_to_binary[41],ciphertext_to_binary[42],ciphertext_to_binary[43],ciphertext_to_binary[44],ciphertext_to_binary[45],ciphertext_to_binary[46],ciphertext_to_binary[47]);
|
||||
//-------------------------------------ciphertext---------------------------------------------//
|
||||
|
||||
//-------------------------------------long_key----------------------------------------------//
|
||||
for(i=0 ; i<6 ; ++i) //
|
||||
{ //
|
||||
for(j=0 ; j<8 ; ++j) //
|
||||
{ // 该作用是把密文以二进制保存在数组中long_key
|
||||
a[i][8-1-j] = long_key[i] % 2 ; //
|
||||
long_key[i] /= 2; //
|
||||
} //
|
||||
} //
|
||||
|
||||
for(i=0 ; i<6 ; ++i)
|
||||
{
|
||||
for(j=0 ; j<8 ; ++j)
|
||||
{
|
||||
long_key_to_binary[i*8+j] = a[i][j] ;
|
||||
}
|
||||
}
|
||||
|
||||
//printf("%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x \n",long_key_to_binary[0],long_key_to_binary[1],long_key_to_binary[2],\
|
||||
long_key_to_binary[3],long_key_to_binary[4],long_key_to_binary[5],long_key_to_binary[6],long_key_to_binary[7],\
|
||||
long_key_to_binary[40],long_key_to_binary[41],long_key_to_binary[42],long_key_to_binary[43],long_key_to_binary[44],long_key_to_binary[45],long_key_to_binary[46],ciphertext_to_binary[47]);
|
||||
|
||||
|
||||
//-------------------------------- long_key---------------------------------------------------//
|
||||
|
||||
//-------------------------------- short_key-------------------------------------------------//
|
||||
for(i=0 ; i<4 ; ++i) //
|
||||
{ //
|
||||
for(j=0 ; j<8 ; ++j) //
|
||||
{ // 该作用是把密文以二进制保存在数组中short_key
|
||||
a[i][8-1-j] = short_key[i] % 2 ; //
|
||||
short_key[i] /= 2 ; //
|
||||
} //
|
||||
} //
|
||||
|
||||
for(i=0 ; i<4 ; ++i)
|
||||
{
|
||||
for(j=0 ; j<8 ; ++j)
|
||||
{
|
||||
short_key_to_binary[i*8+j] = a[i][j] ;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//printf("%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x \n",short_key_to_binary[0],short_key_to_binary[1],short_key_to_binary[2],\
|
||||
short_key_to_binary[3],short_key_to_binary[4],short_key_to_binary[5],short_key_to_binary[6],short_key_to_binary[7],\
|
||||
short_key_to_binary[24],short_key_to_binary[25],short_key_to_binary[26],short_key_to_binary[27],short_key_to_binary[28],short_key_to_binary[29],short_key_to_binary[30],short_key_to_binary[31]);
|
||||
|
||||
|
||||
|
||||
//-------------------------------- short_key-------------------------------------------------//
|
||||
for(num = 0 ; num < 48 ; num++)
|
||||
{
|
||||
for(k=0 ; k<16 ; k++)
|
||||
{
|
||||
if((short_key_to_binary[k*2]==0) &&(short_key_to_binary[k*2+1]==0) )
|
||||
{
|
||||
if((ciphertext_to_binary[k*3]==0) && (ciphertext_to_binary[k*3+1]==0)&&(ciphertext_to_binary[k*3+2]==0))
|
||||
{
|
||||
temp[k*3] = 0;
|
||||
temp[k*3+1]= 0;
|
||||
temp[k*3+2]= 0;
|
||||
}
|
||||
if((ciphertext_to_binary[k*3]==0) && (ciphertext_to_binary[k*3+1]==0)&&(ciphertext_to_binary[k*3+2]==1))
|
||||
{
|
||||
temp[k*3] = 0;
|
||||
temp[k*3+1]= 0;
|
||||
temp[k*3+2]= 1;
|
||||
}
|
||||
if((ciphertext_to_binary[k*3]==0) && (ciphertext_to_binary[k*3+1]==1)&&(ciphertext_to_binary[k*3+2]==0))
|
||||
{
|
||||
temp[k*3] = 1;
|
||||
temp[k*3+1]= 1;
|
||||
temp[k*3+2]= 1;
|
||||
}
|
||||
if((ciphertext_to_binary[k*3]==0) && (ciphertext_to_binary[k*3+1]==1)&&(ciphertext_to_binary[k*3+2]==1))
|
||||
{
|
||||
temp[k*3] = 0;
|
||||
temp[k*3+1]= 1;
|
||||
temp[k*3+2]= 0;
|
||||
}
|
||||
if((ciphertext_to_binary[k*3]==1) && (ciphertext_to_binary[k*3+1]==0)&&(ciphertext_to_binary[k*3+2]==0))
|
||||
{
|
||||
temp[k*3] = 1;
|
||||
temp[k*3+1]= 0;
|
||||
temp[k*3+2]= 1;
|
||||
}
|
||||
if((ciphertext_to_binary[k*3]==1) && (ciphertext_to_binary[k*3+1]==0)&&(ciphertext_to_binary[k*3+2]==1))
|
||||
{
|
||||
temp[k*3] = 1;
|
||||
temp[k*3+1]= 1;
|
||||
temp[k*3+2]= 0;
|
||||
}
|
||||
if((ciphertext_to_binary[k*3]==1) && (ciphertext_to_binary[k*3+1]==1)&&(ciphertext_to_binary[k*3+2]==0))
|
||||
{
|
||||
temp[k*3] = 0;
|
||||
temp[k*3+1]= 1;
|
||||
temp[k*3+2]= 1;
|
||||
}
|
||||
if((ciphertext_to_binary[k*3]==1) && (ciphertext_to_binary[k*3+1]==1)&&(ciphertext_to_binary[k*3+2]==1))
|
||||
{
|
||||
temp[k*3] = 1;
|
||||
temp[k*3+1]= 0;
|
||||
temp[k*3+2]= 0;
|
||||
}
|
||||
}
|
||||
//////////////////////////////01
|
||||
if((short_key_to_binary[k*2]==0) &&(short_key_to_binary[k*2+1]==1) )
|
||||
{
|
||||
if((ciphertext_to_binary[k*3]==0) && (ciphertext_to_binary[k*3+1]==0)&&(ciphertext_to_binary[k*3+2]==0))
|
||||
{
|
||||
temp[k*3] = 0;
|
||||
temp[k*3+1]= 0;
|
||||
temp[k*3+2]= 0;
|
||||
}
|
||||
if((ciphertext_to_binary[k*3]==0) && (ciphertext_to_binary[k*3+1]==0)&&(ciphertext_to_binary[k*3+2]==1))
|
||||
{
|
||||
temp[k*3] = 0;
|
||||
temp[k*3+1]= 0;
|
||||
temp[k*3+2]= 1;
|
||||
}
|
||||
if((ciphertext_to_binary[k*3]==0) && (ciphertext_to_binary[k*3+1]==1)&&(ciphertext_to_binary[k*3+2]==0))
|
||||
{
|
||||
temp[k*3] = 1;
|
||||
temp[k*3+1]= 1;
|
||||
temp[k*3+2]= 1;
|
||||
}
|
||||
if((ciphertext_to_binary[k*3]==0) && (ciphertext_to_binary[k*3+1]==1)&&(ciphertext_to_binary[k*3+2]==1))
|
||||
{
|
||||
temp[k*3] = 1;
|
||||
temp[k*3+1]= 0;
|
||||
temp[k*3+2]= 0;
|
||||
}
|
||||
if((ciphertext_to_binary[k*3]==1) && (ciphertext_to_binary[k*3+1]==0)&&(ciphertext_to_binary[k*3+2]==0))
|
||||
{
|
||||
temp[k*3] = 0;
|
||||
temp[k*3+1]= 1;
|
||||
temp[k*3+2]= 1;
|
||||
}
|
||||
if((ciphertext_to_binary[k*3]==1) && (ciphertext_to_binary[k*3+1]==0)&&(ciphertext_to_binary[k*3+2]==1))
|
||||
{
|
||||
temp[k*3] = 1;
|
||||
temp[k*3+1]= 1;
|
||||
temp[k*3+2]= 0;
|
||||
}
|
||||
if((ciphertext_to_binary[k*3]==1) && (ciphertext_to_binary[k*3+1]==1)&&(ciphertext_to_binary[k*3+2]==0))
|
||||
{
|
||||
temp[k*3] = 1;
|
||||
temp[k*3+1]= 0;
|
||||
temp[k*3+2]= 1;
|
||||
}
|
||||
if((ciphertext_to_binary[k*3]==1) && (ciphertext_to_binary[k*3+1]==1)&&(ciphertext_to_binary[k*3+2]==1))
|
||||
{
|
||||
temp[k*3] = 0;
|
||||
temp[k*3+1]= 1;
|
||||
temp[k*3+2]= 0;
|
||||
}
|
||||
}
|
||||
//////////////////////////////////////////10
|
||||
if((short_key_to_binary[k*2]==1) &&(short_key_to_binary[k*2+1]==0) )
|
||||
{
|
||||
if((ciphertext_to_binary[k*3]==0) && (ciphertext_to_binary[k*3+1]==0)&&(ciphertext_to_binary[k*3+2]==0))
|
||||
{
|
||||
temp[k*3] = 0;
|
||||
temp[k*3+1]= 0;
|
||||
temp[k*3+2]= 0;
|
||||
}
|
||||
if((ciphertext_to_binary[k*3]==0) && (ciphertext_to_binary[k*3+1]==0)&&(ciphertext_to_binary[k*3+2]==1))
|
||||
{
|
||||
temp[k*3] = 0;
|
||||
temp[k*3+1]= 1;
|
||||
temp[k*3+2]= 0;
|
||||
}
|
||||
if((ciphertext_to_binary[k*3]==0) && (ciphertext_to_binary[k*3+1]==1)&&(ciphertext_to_binary[k*3+2]==0))
|
||||
{
|
||||
temp[k*3] = 1;
|
||||
temp[k*3+1]= 1;
|
||||
temp[k*3+2]= 1;
|
||||
}
|
||||
if((ciphertext_to_binary[k*3]==0) && (ciphertext_to_binary[k*3+1]==1)&&(ciphertext_to_binary[k*3+2]==1))
|
||||
{
|
||||
temp[k*3] = 0;
|
||||
temp[k*3+1]= 0;
|
||||
temp[k*3+2]= 1;
|
||||
}
|
||||
if((ciphertext_to_binary[k*3]==1) && (ciphertext_to_binary[k*3+1]==0)&&(ciphertext_to_binary[k*3+2]==0))
|
||||
{
|
||||
temp[k*3] = 1;
|
||||
temp[k*3+1]= 1;
|
||||
temp[k*3+2]= 0;
|
||||
}
|
||||
if((ciphertext_to_binary[k*3]==1) && (ciphertext_to_binary[k*3+1]==0)&&(ciphertext_to_binary[k*3+2]==1))
|
||||
{
|
||||
temp[k*3] = 1;
|
||||
temp[k*3+1]= 0;
|
||||
temp[k*3+2]= 1;
|
||||
}
|
||||
if((ciphertext_to_binary[k*3]==1) && (ciphertext_to_binary[k*3+1]==1)&&(ciphertext_to_binary[k*3+2]==0))
|
||||
{
|
||||
temp[k*3] = 0;
|
||||
temp[k*3+1]= 1;
|
||||
temp[k*3+2]= 1;
|
||||
}
|
||||
if((ciphertext_to_binary[k*3]==1) && (ciphertext_to_binary[k*3+1]==1)&&(ciphertext_to_binary[k*3+2]==1))
|
||||
{
|
||||
temp[k*3] = 1;
|
||||
temp[k*3+1]= 0;
|
||||
temp[k*3+2]= 0;
|
||||
}
|
||||
}
|
||||
////////////////////////////////////////////11
|
||||
if((short_key_to_binary[k*2]==1) &&(short_key_to_binary[k*2+1]==1) )
|
||||
{
|
||||
if((ciphertext_to_binary[k*3]==0) && (ciphertext_to_binary[k*3+1]==0)&&(ciphertext_to_binary[k*3+2]==0))
|
||||
{
|
||||
temp[k*3] = 0;
|
||||
temp[k*3+1]= 0;
|
||||
temp[k*3+2]= 0;
|
||||
}
|
||||
if((ciphertext_to_binary[k*3]==0) && (ciphertext_to_binary[k*3+1]==0)&&(ciphertext_to_binary[k*3+2]==1))
|
||||
{
|
||||
temp[k*3] = 1;
|
||||
temp[k*3+1]= 0;
|
||||
temp[k*3+2]= 0;
|
||||
}
|
||||
if((ciphertext_to_binary[k*3]==0) && (ciphertext_to_binary[k*3+1]==1)&&(ciphertext_to_binary[k*3+2]==0))
|
||||
{
|
||||
temp[k*3] = 1;
|
||||
temp[k*3+1]= 1;
|
||||
temp[k*3+2]= 1;
|
||||
}
|
||||
if((ciphertext_to_binary[k*3]==0) && (ciphertext_to_binary[k*3+1]==1)&&(ciphertext_to_binary[k*3+2]==1))
|
||||
{
|
||||
temp[k*3] = 0;
|
||||
temp[k*3+1]= 1;
|
||||
temp[k*3+2]= 0;
|
||||
}
|
||||
if((ciphertext_to_binary[k*3]==1) && (ciphertext_to_binary[k*3+1]==0)&&(ciphertext_to_binary[k*3+2]==0))
|
||||
{
|
||||
temp[k*3] = 1;
|
||||
temp[k*3+1]= 0;
|
||||
temp[k*3+2]= 1;
|
||||
}
|
||||
if((ciphertext_to_binary[k*3]==1) && (ciphertext_to_binary[k*3+1]==0)&&(ciphertext_to_binary[k*3+2]==1))
|
||||
{
|
||||
temp[k*3] = 0;
|
||||
temp[k*3+1]= 1;
|
||||
temp[k*3+2]= 1;
|
||||
}
|
||||
if((ciphertext_to_binary[k*3]==1) && (ciphertext_to_binary[k*3+1]==1)&&(ciphertext_to_binary[k*3+2]==0))
|
||||
{
|
||||
temp[k*3] = 1;
|
||||
temp[k*3+1]= 1;
|
||||
temp[k*3+2]= 0;
|
||||
}
|
||||
if((ciphertext_to_binary[k*3]==1) && (ciphertext_to_binary[k*3+1]==1)&&(ciphertext_to_binary[k*3+2]==1))
|
||||
{
|
||||
temp[k*3] = 0;
|
||||
temp[k*3+1]= 0;
|
||||
temp[k*3+2]= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// printf("%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x \n",temp[0],temp[1],temp[2],temp[3],temp[4],temp[5],temp[6],temp[7],temp[8],temp[9],temp[10],temp[11],temp[12],temp[13],temp[14],temp[15],temp[40],temp[41],temp[42],temp[43],temp[44],temp[45],temp[46],temp[47]);
|
||||
|
||||
|
||||
|
||||
|
||||
//下面的函数是对RC的逆运算
|
||||
t = 1 ^ rev_counter[0] ^ rev_counter[5];
|
||||
temp_rev_counter [0] = rev_counter[1] ;
|
||||
temp_rev_counter [1] = rev_counter[2] ;
|
||||
temp_rev_counter [2] = rev_counter[3] ;
|
||||
temp_rev_counter [3] = rev_counter[4] ;
|
||||
temp_rev_counter [4] = rev_counter[5] ;
|
||||
temp_rev_counter [5] = t ;
|
||||
|
||||
for(i=0 ; i<6 ; i++ )
|
||||
{
|
||||
rev_counter [i] = temp_rev_counter[i] ; //rev_counter[5]是最高位
|
||||
}
|
||||
|
||||
temp[47] = rev_counter[0] ^ temp[47] ;
|
||||
temp[46] = rev_counter[1] ^ temp[46] ;
|
||||
temp[45] = rev_counter[2] ^ temp[45] ;
|
||||
temp[44] = rev_counter[3] ^ temp[44] ;
|
||||
temp[43] = rev_counter[4] ^ temp[43] ;
|
||||
temp[42] = rev_counter[5] ^ temp[42] ;
|
||||
|
||||
// printf("%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x \n",temp[0],temp[1],temp[2],temp[3],temp[4],temp[5],temp[6],temp[7],temp[8],temp[9],temp[10],temp[11],temp[12],temp[13],temp[14],temp[15],temp[40],temp[41],temp[42],temp[43],temp[44],temp[45],temp[46],temp[47]);
|
||||
//下面是对linear diffusion的逆运算
|
||||
ciphertext_to_binary[0] = temp[0] ;
|
||||
ciphertext_to_binary[1] = temp[3] ;
|
||||
ciphertext_to_binary[2] = temp[6] ;
|
||||
ciphertext_to_binary[3] = temp[9] ;
|
||||
ciphertext_to_binary[4] = temp[12] ;
|
||||
ciphertext_to_binary[5] = temp[15] ;
|
||||
ciphertext_to_binary[6] = temp[18] ;
|
||||
ciphertext_to_binary[7] = temp[21] ;
|
||||
ciphertext_to_binary[8] = temp[24] ;
|
||||
ciphertext_to_binary[9] = temp[27] ;
|
||||
ciphertext_to_binary[10] = temp[30] ;
|
||||
ciphertext_to_binary[11] = temp[33] ;
|
||||
ciphertext_to_binary[12] = temp[36] ;
|
||||
ciphertext_to_binary[13] = temp[39] ;
|
||||
ciphertext_to_binary[14] = temp[42] ;
|
||||
ciphertext_to_binary[15] = temp[45] ;
|
||||
ciphertext_to_binary[16] = temp[1] ;
|
||||
ciphertext_to_binary[17] = temp[4] ;
|
||||
ciphertext_to_binary[18] = temp[7] ;
|
||||
ciphertext_to_binary[19] = temp[10] ;
|
||||
ciphertext_to_binary[20] = temp[13] ;
|
||||
ciphertext_to_binary[21] = temp[16] ;
|
||||
ciphertext_to_binary[22] = temp[19] ;
|
||||
ciphertext_to_binary[23] = temp[22] ;
|
||||
ciphertext_to_binary[24] = temp[25] ;
|
||||
ciphertext_to_binary[25] = temp[28] ;
|
||||
ciphertext_to_binary[26] = temp[31] ;
|
||||
ciphertext_to_binary[27] = temp[34] ;
|
||||
ciphertext_to_binary[28] = temp[37] ;
|
||||
ciphertext_to_binary[29] = temp[40] ;
|
||||
ciphertext_to_binary[30] = temp[43] ;
|
||||
ciphertext_to_binary[31] = temp[46] ;
|
||||
ciphertext_to_binary[32] = temp[2] ;
|
||||
ciphertext_to_binary[33] = temp[5] ;
|
||||
ciphertext_to_binary[34] = temp[8] ;
|
||||
ciphertext_to_binary[35] = temp[11] ;
|
||||
ciphertext_to_binary[36] = temp[14] ;
|
||||
ciphertext_to_binary[37] = temp[17] ;
|
||||
ciphertext_to_binary[38] = temp[20] ;
|
||||
ciphertext_to_binary[39] = temp[23] ;
|
||||
ciphertext_to_binary[40] = temp[26] ;
|
||||
ciphertext_to_binary[41] = temp[29] ;
|
||||
ciphertext_to_binary[42] = temp[32] ;
|
||||
ciphertext_to_binary[43] = temp[35] ;
|
||||
ciphertext_to_binary[44] = temp[38] ;
|
||||
ciphertext_to_binary[45] = temp[41] ;
|
||||
ciphertext_to_binary[46] = temp[44] ;
|
||||
ciphertext_to_binary[47] = temp[47] ;
|
||||
|
||||
for(i=0 ; i<48 ; i++)
|
||||
{
|
||||
ciphertext_to_binary[i] = ciphertext_to_binary[i] ^ long_key_to_binary[i] ;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// printf (" %x %x %x %x %x %x %x %x\n %x %x %x %x %x %x %x %x\n %x %x %x %x %x %x %x %x\n %x %x %x %x %x %x %x %x\n %x %x %x %x %x %x %x %x\n %x %x %x %x %x %x %x %x\n",ciphertext_to_binary[0],ciphertext_to_binary[1],ciphertext_to_binary[2],ciphertext_to_binary[3],ciphertext_to_binary[4],ciphertext_to_binary[5],ciphertext_to_binary[6],ciphertext_to_binary[7],\
|
||||
ciphertext_to_binary[8],ciphertext_to_binary[9],ciphertext_to_binary[10],ciphertext_to_binary[11],ciphertext_to_binary[12],ciphertext_to_binary[13],ciphertext_to_binary[14],ciphertext_to_binary[15],\
|
||||
ciphertext_to_binary[16],ciphertext_to_binary[17],ciphertext_to_binary[18],ciphertext_to_binary[19],ciphertext_to_binary[20],ciphertext_to_binary[21],ciphertext_to_binary[22],ciphertext_to_binary[23],\
|
||||
ciphertext_to_binary[24],ciphertext_to_binary[25],ciphertext_to_binary[26],ciphertext_to_binary[27],ciphertext_to_binary[28],ciphertext_to_binary[29],ciphertext_to_binary[30],ciphertext_to_binary[31],\
|
||||
ciphertext_to_binary[32],ciphertext_to_binary[33],ciphertext_to_binary[34],ciphertext_to_binary[35],ciphertext_to_binary[36],ciphertext_to_binary[37],ciphertext_to_binary[38],ciphertext_to_binary[39],\
|
||||
ciphertext_to_binary[40],ciphertext_to_binary[41],ciphertext_to_binary[42],ciphertext_to_binary[43],ciphertext_to_binary[44],ciphertext_to_binary[45],ciphertext_to_binary[46],ciphertext_to_binary[47] );
|
||||
return 0 ;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void Print_AUTH(unsigned char *key,unsigned char *TOKEN,unsigned char *rece_buff)//print 认证
|
||||
{
|
||||
unsigned char i,j;
|
||||
unsigned char long_key[6] = {0xff,0xff,0xff,0xff,0xff,0xff}; //长key
|
||||
unsigned char short_key[4] = {0xff,0xff,0xff,0xff}; //短key,注意短key的最后一个字节是整个key的第一个字节
|
||||
unsigned char PWD_key[10] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};//tag的秘钥占10个字节
|
||||
for(i = 0;i<10;i++)
|
||||
{
|
||||
PWD_key[i] = key[9-i];//需要把PWD 倒顺序一下,做下反向,需要特别注意,之前的密码需要倒序一下
|
||||
}
|
||||
memcpy(&long_key[0],PWD_key,6);
|
||||
memcpy(&short_key[0],&PWD_key[6],4);
|
||||
decode_printcipher(TOKEN, &long_key[0] , &short_key[0]);
|
||||
for(i=0 ; i<6 ; i++)
|
||||
{
|
||||
for(j=0 ; j<8 ; j++)
|
||||
{
|
||||
rece_buff[i] = (rece_buff[i]<<1)+ciphertext_to_binary[i*8+j] ; //把最终的结果赋值给明文中
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
unsigned char lsfr_fm458(void) //length 这里是byte
|
||||
{
|
||||
char j ;
|
||||
unsigned char temp;
|
||||
unsigned char temp1;
|
||||
|
||||
temp1 = lsfr[47];
|
||||
temp = lsfr[47] ^ lsfr[27] ^ lsfr[26] ^ lsfr[0];
|
||||
for (j = 46; j >= 0; j--)
|
||||
{
|
||||
lsfr[j + 1] = lsfr[j];
|
||||
}
|
||||
lsfr[0] = temp;
|
||||
|
||||
return temp1;
|
||||
}
|
||||
|
||||
|
||||
void lsfr_init(unsigned char *S022_Token2)
|
||||
{
|
||||
unsigned char z,k;
|
||||
unsigned char load_key[6];
|
||||
//备份初始密钥流
|
||||
memcpy(load_key, S022_Token2, 6);
|
||||
//把R2和R3的结果加载进移位寄存器
|
||||
for (z = 0; z < 6; z++)
|
||||
{
|
||||
for (k = 0; k <= 7; k++)
|
||||
{
|
||||
if ((unsigned char)((load_key[z] << k) & 0x80) == 0)
|
||||
lsfr[47 - (k + z * 8)] = 0;
|
||||
else
|
||||
lsfr[47 - (k + z * 8)] = 1;
|
||||
}
|
||||
}
|
||||
//lsfr_length = 48;
|
||||
//printf("lsfr = %x%x%x%x%x%x%x%x\r\n",lsfr[0],lsfr[1],lsfr[2],lsfr[3],lsfr[4],lsfr[5],lsfr[6],lsfr[7]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
//data_len 最多16byte
|
||||
//unsigned char PRINT_Calc_sendData(unsigned char *data_buf, unsigned char data_len, BOOL half_byte)
|
||||
unsigned char PRINT_Calc_sendData(unsigned char *data_buf, unsigned char data_len)
|
||||
{
|
||||
unsigned char i,y;
|
||||
unsigned char round;
|
||||
unsigned char temp[8];
|
||||
unsigned char temp1[8];
|
||||
unsigned char encry[64];
|
||||
unsigned char senddata[16];
|
||||
round = data_len * 8;
|
||||
for (y = 0; y < round; y++)
|
||||
{
|
||||
encry[y] = lsfr_fm458();
|
||||
}
|
||||
|
||||
|
||||
for (y = 0; y < data_len; y++)
|
||||
{
|
||||
for(i=0; i<8; i++)
|
||||
{
|
||||
temp[i] = (unsigned char)((data_buf[y] >> i) & 0x01);
|
||||
}
|
||||
|
||||
//发送的byte按位异或 加密
|
||||
for(i=0 ; i<8 ;i++)
|
||||
{
|
||||
temp1[i] = temp[i] ^ encry[i + y * 8]; //异或就是和密码流进行加密
|
||||
}
|
||||
|
||||
senddata[y] = (unsigned char)(temp1[0] * 1 + temp1[1] * 2 + temp1[2] * 4 + temp1[3] * 8 + temp1[4] * 16 + temp1[5] * 32 + temp1[6] * 64 + temp1[7] * 128);
|
||||
}
|
||||
|
||||
memcpy(data_buf, senddata, data_len);
|
||||
|
||||
// authstat = CAL_COMMAND_FLOW;
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//unsigned char PRINT_Calc_recvData(unsigned char *data_buf, unsigned char data_len, BOOL half_byte)
|
||||
unsigned char PRINT_Calc_recvData(unsigned char *data_buf, unsigned char data_len)
|
||||
{
|
||||
unsigned char encry[128];
|
||||
unsigned char i,y;
|
||||
unsigned char temp[8];
|
||||
unsigned char temp1[8];
|
||||
unsigned char recvdata[16];
|
||||
unsigned char round;
|
||||
round = (data_len) * 8;
|
||||
|
||||
if (data_len == 0x01) //该添加为了类似写指令回发ACK等4bit的加密流方法。
|
||||
{
|
||||
round = round / 2;
|
||||
}
|
||||
|
||||
for (y = 0; y < round; y++)
|
||||
{
|
||||
encry[y] = lsfr_fm458();
|
||||
}
|
||||
|
||||
//已经有了加密数据,就要想办法进行异或操作
|
||||
for (y = 0; y < data_len; y++)
|
||||
{
|
||||
for (i = 0; i < 8; i++)
|
||||
{
|
||||
temp[i] = (unsigned char)((data_buf[y] >> i) & 0x01);
|
||||
}
|
||||
|
||||
//发送的byte按位异或 加密
|
||||
for (i = 0; i < 8; i++)
|
||||
{
|
||||
temp1[i] = temp[i] ^ encry[i + y * 8]; //异或就是和密码流进行加密
|
||||
}
|
||||
|
||||
recvdata[y] = (unsigned char)(temp1[0] * 1 + temp1[1] * 2 + temp1[2] * 4 + temp1[3] * 8 + temp1[4] * 16 + temp1[5] * 32 + temp1[6] * 64 + temp1[7] * 128);
|
||||
}
|
||||
|
||||
memcpy(data_buf, recvdata, data_len);
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
457
apps/earphone/94_rfid_stc/function.c
Normal file
457
apps/earphone/94_rfid_stc/function.c
Normal file
@ -0,0 +1,457 @@
|
||||
/**
|
||||
************************************* Copyright ******************************
|
||||
* (C) Copyright 2019,TuYW,FMSH,China.
|
||||
* All Rights Reserved
|
||||
* By(Shanghai Fudan MicroeleCardTyperonics Group Company Limited)
|
||||
* https://www.fmsh.com
|
||||
*
|
||||
* FileName : function.c
|
||||
* Version : v1.0
|
||||
* Author : TuYW
|
||||
* Date : 2019-03-19
|
||||
* Description:
|
||||
*******************************************************************************/
|
||||
|
||||
#include "function.h"
|
||||
//#include "flash.h"
|
||||
|
||||
#include "intrins.h"
|
||||
|
||||
#include <stdio.h>
|
||||
/**
|
||||
* @brief Convert Hex 32Bits value into char
|
||||
* @param value: value to convert
|
||||
* @param pbuf: pointer to the buffer
|
||||
* @param len: buffer length
|
||||
* @retval None
|
||||
*/
|
||||
void IntToUnicode( uint32_t value, uint8_t *pbuf, uint8_t len )
|
||||
{
|
||||
uint8_t idx = 0;
|
||||
|
||||
for ( idx = 0; idx < len; idx++ )
|
||||
{
|
||||
if ( ( (value >> 28) ) < 0xA )
|
||||
{
|
||||
pbuf[2 * idx] = (value >> 28) + '0';
|
||||
}
|
||||
else
|
||||
{
|
||||
pbuf[2 * idx] = (value >> 28) + 'A' - 10;
|
||||
}
|
||||
|
||||
value = value << 4;
|
||||
|
||||
pbuf[2 * idx + 1] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//תַ
|
||||
u8 uitoa( u16 n, u8* s )
|
||||
{
|
||||
u16 i = 0, j;
|
||||
u16 len = 0;
|
||||
u8 tmp[7] = { 0 };
|
||||
|
||||
do
|
||||
{
|
||||
tmp[i++] = n % 10 + '0'; //ȡһ
|
||||
}
|
||||
while ( (n /= 10) > 0 ); //ɾ
|
||||
|
||||
len = i;
|
||||
|
||||
for ( j = 0; j < len; j++, i-- ) //
|
||||
{
|
||||
s[j] = tmp[i - 1];
|
||||
}
|
||||
|
||||
return (len);
|
||||
}
|
||||
|
||||
|
||||
//ƽ
|
||||
u32 power( u8 n )
|
||||
{
|
||||
u8 i;
|
||||
u32 t;
|
||||
|
||||
t = 1;
|
||||
for ( i = 0; i < n; i++ )
|
||||
t *= 2;
|
||||
|
||||
return (t);
|
||||
}
|
||||
|
||||
|
||||
//m^n
|
||||
u32 calpow( u8 m, u8 n )
|
||||
{
|
||||
u32 result = 1;
|
||||
while ( n-- )
|
||||
result *= m;
|
||||
return (result);
|
||||
}
|
||||
|
||||
|
||||
//CRC
|
||||
u16 UpdateCrc( u8 ch, u16 *lpwCrc )
|
||||
{
|
||||
ch = (ch ^ (u8) ( (*lpwCrc) & 0x00FF) );
|
||||
ch = (ch ^ (ch << 4) );
|
||||
*lpwCrc = (*lpwCrc >> 8) ^ ( (u16) ch << 8) ^ ( (u16) ch << 3) ^ ( (u16) ch >> 4);
|
||||
return (*lpwCrc);
|
||||
}
|
||||
|
||||
|
||||
u16 ComputeCrc( const u16 InitCRC, const u8 *Data, const u32 Length )
|
||||
{
|
||||
u8 chBlock;
|
||||
u32 InLength = Length;
|
||||
u16 wCrc = InitCRC;
|
||||
do
|
||||
{
|
||||
chBlock = *Data++;
|
||||
UpdateCrc( chBlock, &wCrc );
|
||||
}
|
||||
while ( --InLength );
|
||||
return (~wCrc);;
|
||||
}
|
||||
|
||||
|
||||
//u16 CalculateIapCrc(void)
|
||||
//{
|
||||
// return ComputeCrc(0xffff,(u8 *)PROGRAM_IAP_BASE,PROGRAM_IAP_LENGTH);
|
||||
//}
|
||||
|
||||
//u16 CalculateAppCrc(void)
|
||||
//{
|
||||
// return ComputeCrc(0xffff,(u8 *)PROGRAM_APP_BASE,PROGRAM_APP_LENGTH);
|
||||
//}
|
||||
|
||||
|
||||
/*********************************************************************************************************
|
||||
** : CalcBCC
|
||||
** : bccУ
|
||||
** :
|
||||
** :
|
||||
** ֵ:
|
||||
*********************************************************************************************************/
|
||||
u8 CalcBCC( u8 *ibuf, u16 ilen )
|
||||
{
|
||||
u8 BCC = 0x00;
|
||||
u16 i = 0;
|
||||
for ( i = 0; i < ilen; i++ )
|
||||
{
|
||||
BCC = BCC ^ ibuf[i];
|
||||
}
|
||||
|
||||
return (BCC);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
// C prototype : void StrToHex(BYTE *pbDest, BYTE *pbSrc, int nLen)
|
||||
// parameter(s): [OUT] pbDest -
|
||||
// [IN] pbSrc - ַ
|
||||
// [IN] nLen - 16ֽ(ַij/2)
|
||||
// return value:
|
||||
// remarks : ַתΪ16
|
||||
*/
|
||||
void StrToHex( BYTE *pbDest, BYTE *pbSrc, int nLen )
|
||||
{
|
||||
char h1, h2;
|
||||
BYTE s1, s2;
|
||||
int i;
|
||||
|
||||
for ( i = 0; i < nLen; i++ )
|
||||
{
|
||||
h1 = pbSrc[2 * i];
|
||||
h2 = pbSrc[2 * i + 1];
|
||||
|
||||
s1 = toupper( h1 ) - 0x30;
|
||||
if ( s1 > 9 )
|
||||
s1 -= 7;
|
||||
|
||||
s2 = toupper( h2 ) - 0x30;
|
||||
if ( s2 > 9 )
|
||||
s2 -= 7;
|
||||
|
||||
pbDest[i] = s1 * 16 + s2;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
// C prototype : void HexToStr(BYTE *pbDest, BYTE *pbSrc, int nLen)
|
||||
// parameter(s): [OUT] pbDest - Ŀַ
|
||||
// [IN] pbSrc - 16ʼַ
|
||||
// [IN] nLen - 16ֽ
|
||||
// return value:
|
||||
// remarks : 16תΪַ
|
||||
*/
|
||||
void HexToStr( BYTE *pbDest, BYTE *pbSrc, int nLen )
|
||||
{
|
||||
char ddl, ddh;
|
||||
int i;
|
||||
|
||||
for ( i = 0; i < nLen; i++ )
|
||||
{
|
||||
ddh = 48 + pbSrc[i] / 16;
|
||||
ddl = 48 + pbSrc[i] % 16;
|
||||
if ( ddh > 57 )
|
||||
ddh = ddh + 7;
|
||||
if ( ddl > 57 )
|
||||
ddl = ddl + 7;
|
||||
pbDest[i * 2] = ddh;
|
||||
pbDest[i * 2 + 1] = ddl;
|
||||
}
|
||||
|
||||
pbDest[nLen * 2] = '\0';
|
||||
}
|
||||
|
||||
|
||||
U32 le32p_to_cpu( U8*p )
|
||||
{
|
||||
return ( (U32) ( (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]) );
|
||||
}
|
||||
|
||||
|
||||
U32 be32p_to_cpu( const U8*p )
|
||||
{
|
||||
return ( (U32) ( (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]) );
|
||||
}
|
||||
|
||||
|
||||
void cpu_to_le32p( U8*p, U32 n )
|
||||
{
|
||||
p[3] = (U8) (n >> 24);
|
||||
p[2] = (U8) (n >> 16);
|
||||
p[1] = (U8) (n >> 8);
|
||||
p[0] = (U8) (n);
|
||||
}
|
||||
|
||||
|
||||
void cpu_to_be32p( U8*p, U32 n )
|
||||
{
|
||||
p[0] = (U8) (n >> 24);
|
||||
p[1] = (U8) (n >> 16);
|
||||
p[2] = (U8) (n >> 8);
|
||||
p[3] = (U8) (n);
|
||||
}
|
||||
|
||||
|
||||
U32 le16p_to_cpu( const U8*p )
|
||||
{
|
||||
return ( (U32) ( (p[1] << 8) | p[0]) );
|
||||
}
|
||||
|
||||
|
||||
U32 be16p_to_cpu( U8*p )
|
||||
{
|
||||
return ( (U32) ( (p[0] << 8) | p[1]) );
|
||||
}
|
||||
|
||||
|
||||
void cpu_to_le16p( U8*p, U32 n )
|
||||
{
|
||||
p[1] = (U8) (n >> 8);
|
||||
p[0] = (U8) (n);
|
||||
}
|
||||
|
||||
|
||||
void cpu_to_be16p( U8*p, U32 n )
|
||||
{
|
||||
p[0] = (U8) (n >> 8);
|
||||
p[1] = (U8) (n);
|
||||
}
|
||||
|
||||
|
||||
/*ַs转Ӧ*/
|
||||
//int atoi(char s[])
|
||||
//{
|
||||
// int i;
|
||||
// int n = 0;
|
||||
// for (i = 0; s[i] >= '0' && s[i] <= '9'; ++i)
|
||||
// {
|
||||
// n = 10 * n + (s[i] - '0');
|
||||
// }
|
||||
// return n;
|
||||
//}
|
||||
|
||||
int atoi_b( u8 *s, u32 sl )
|
||||
{
|
||||
int i;
|
||||
int n = 0;
|
||||
for ( i = 0; i < sl; i++ )
|
||||
{
|
||||
n = 10 * n + (s[i] - '0');
|
||||
}
|
||||
return (n);
|
||||
}
|
||||
|
||||
//xtell tolower改成my_tolower
|
||||
/*дĸ转Сдĸ*/
|
||||
int my_tolower( int c )
|
||||
{
|
||||
if ( c >= 'A' && c <= 'Z' )
|
||||
{
|
||||
return (c + 'a' - 'A');
|
||||
}
|
||||
else
|
||||
{
|
||||
return (c);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//ʮƵַ转
|
||||
int htoi( char s[] )
|
||||
{
|
||||
int i;
|
||||
int n = 0;
|
||||
if ( s[0] == '0' && (s[1] == 'x' || s[1] == 'X') )
|
||||
{
|
||||
i = 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
i = 0;
|
||||
}
|
||||
for (; (s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z'); ++i )
|
||||
{
|
||||
if ( my_tolower( s[i] ) > '9' )
|
||||
{
|
||||
n = 16 * n + (10 + my_tolower( s[i] ) - 'a');
|
||||
}
|
||||
else
|
||||
{
|
||||
n = 16 * n + (my_tolower( s[i] ) - '0');
|
||||
}
|
||||
}
|
||||
return (n);
|
||||
}
|
||||
|
||||
|
||||
// itoa (ʾ integer to alphanumeric)ǰ转ַһ
|
||||
char* itoa( long num, char* str, int radix )
|
||||
{
|
||||
char temp; // 移动到函数开头
|
||||
char index[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //
|
||||
unsigned long unum; //Ҫ转ľֵ,转Ǹ
|
||||
int i = 0, j, k; //iָʾַӦλ转֮iʵַijȣ转˳ģkָʾ˳Ŀʼλ;jָʾ˳ʱĽ
|
||||
|
||||
//ȡҪ转ľֵ
|
||||
if ( radix == 10 && num < 0 ) //Ҫ转ʮǸ
|
||||
{
|
||||
unum = (unsigned long) -num; //numľֵunum
|
||||
str[i++] = '-'; //ַǰΪ'-'ţ1
|
||||
}
|
||||
else
|
||||
unum = (unsigned long) num; //numΪֱӸֵunum
|
||||
|
||||
//转֣ע转
|
||||
do
|
||||
{
|
||||
str[i++] = index[unum % (unsigned long) radix]; //ȡunumһλΪstrӦλָʾ1
|
||||
unum /= radix; //unumȥһλ
|
||||
}
|
||||
while ( unum ); //ֱunumΪ0˳ѭ
|
||||
|
||||
str[i] = '\0'; //ַ'\0'ַcַ'\0'
|
||||
|
||||
//˳
|
||||
if ( str[0] == '-' )
|
||||
k = 1; //ǸŲõӷź濪ʼ
|
||||
else
|
||||
k = 0; //ǸȫҪ
|
||||
|
||||
for ( j = k; j <= (i - 1) / 2; j++ ) //ͷβһһԳƽiʵַijȣֵȳ1
|
||||
{
|
||||
temp = str[j]; //ͷֵʱ
|
||||
str[j] = str[i - 1 + k - j]; //βֵͷ
|
||||
str[i - 1 + k - j] = temp; //ʱֵ(ʵ֮ǰͷֵ)β
|
||||
}
|
||||
|
||||
return (str); //转ַ
|
||||
}
|
||||
|
||||
|
||||
static const char *ALPHA_BASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
|
||||
//base64뺯
|
||||
int base64_encode( const char *buf, const long size, char *base64Char )
|
||||
{
|
||||
int int63 = 0x3F; // 00111111 // 移动到函数开头
|
||||
int int255 = 0xFF; // 11111111 // 移动到函数开头
|
||||
int a = 0;
|
||||
int i = 0;
|
||||
int base64Len = 0;
|
||||
|
||||
while ( i < size )
|
||||
{
|
||||
char b0 = buf[i++];
|
||||
char b1 = (i < size) ? buf[i++] : 0;
|
||||
char b2 = (i < size) ? buf[i++] : 0;
|
||||
|
||||
base64Char[a++] = ALPHA_BASE[(b0 >> 2) & int63];
|
||||
base64Char[a++] = ALPHA_BASE[( (b0 << 4) | ( (b1 & int255) >> 4) ) & int63];
|
||||
base64Char[a++] = ALPHA_BASE[( (b1 << 2) | ( (b2 & int255) >> 6) ) & int63];
|
||||
base64Char[a++] = ALPHA_BASE[b2 & int63];
|
||||
}
|
||||
|
||||
base64Len = a;
|
||||
|
||||
switch ( size % 3 )
|
||||
{
|
||||
case 1:
|
||||
base64Char[--a] = '=';
|
||||
case 2:
|
||||
base64Char[--a] = '=';
|
||||
}
|
||||
|
||||
return (base64Len);
|
||||
}
|
||||
|
||||
|
||||
//base64뺯
|
||||
char *base64_decode( const char *base64Char, const long base64CharSize, char *originChar, long originCharSize )
|
||||
{
|
||||
int toInt[128] = { -1 };
|
||||
int int255 = 0xFF; // 移动到函数开头
|
||||
int index = 0; // 移动到函数开头
|
||||
int c2, c3; // 移动到函数开头
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
for ( i = 0; i < 64; i++ )
|
||||
{
|
||||
toInt[ALPHA_BASE[i]] = i;
|
||||
}
|
||||
for ( j = 0; j < base64CharSize; j += 4 )
|
||||
{
|
||||
int c0 = toInt[base64Char[j]];
|
||||
int c1 = toInt[base64Char[j + 1]];
|
||||
originChar[index++] = ( ( (c0 << 2) | (c1 >> 4) ) & int255);
|
||||
if ( index >= originCharSize )
|
||||
{
|
||||
return (originChar);
|
||||
}
|
||||
c2 = toInt[base64Char[j + 2]];
|
||||
originChar[index++] = ( ( (c1 << 4) | (c2 >> 2) ) & int255);
|
||||
if ( index >= originCharSize )
|
||||
{
|
||||
return (originChar);
|
||||
}
|
||||
c3 = toInt[base64Char[j + 3]];
|
||||
originChar[index++] = ( ( (c2 << 6) | c3) & int255);
|
||||
}
|
||||
return (originChar);
|
||||
}
|
||||
|
||||
|
||||
//建
|
||||
void ClrLongBuf( u8* buf, u32 n )
|
||||
{
|
||||
memset( buf, 0x00, n );
|
||||
}
|
||||
21
apps/earphone/94_rfid_stc/inc/MIFARE.h
Normal file
21
apps/earphone/94_rfid_stc/inc/MIFARE.h
Normal file
@ -0,0 +1,21 @@
|
||||
#ifndef _MIFARE_H_
|
||||
#define _MIFARE_H_
|
||||
#define KEY_A_M1 0
|
||||
#define KEY_B_M1 1
|
||||
|
||||
extern unsigned char SECTOR,BLOCK,BLOCK_NUM;
|
||||
extern unsigned char BLOCK_DATA[16];
|
||||
extern unsigned char KEY_A[16][6];
|
||||
extern unsigned char KEY_B[16][6];
|
||||
extern unsigned char MIFARE_CARD_EVENT(void);
|
||||
extern void Mifare_Clear_Crypto(void);
|
||||
unsigned char Mifare_LoadKey(unsigned char *mifare_key);
|
||||
extern unsigned char Mifare_Transfer(unsigned char block);
|
||||
extern unsigned char Mifare_Restore(unsigned char block);
|
||||
extern unsigned char Mifare_Blockset(unsigned char block,unsigned char *data_buff);
|
||||
extern unsigned char Mifare_Blockinc(unsigned char block,unsigned char *data_buff);
|
||||
extern unsigned char Mifare_Blockdec(unsigned char block,unsigned char *data_buff);
|
||||
extern unsigned char Mifare_Blockwrite(unsigned char block,unsigned char *data_buff);
|
||||
extern unsigned char Mifare_Blockread(unsigned char block,unsigned char *data_buff);
|
||||
extern unsigned char Mifare_Auth(unsigned char key_mode,unsigned char sector,unsigned char *mifare_key,unsigned char *card_uid);
|
||||
#endif
|
||||
73
apps/earphone/94_rfid_stc/inc/MIFARE_commented.h
Normal file
73
apps/earphone/94_rfid_stc/inc/MIFARE_commented.h
Normal file
@ -0,0 +1,73 @@
|
||||
/**
|
||||
* @file MIFARE.h
|
||||
* @brief MIFARE Classic 卡应用层驱动接口
|
||||
* @version 1.0
|
||||
* @date 2024-05-20
|
||||
*
|
||||
* @par 文件作用:
|
||||
* 该文件为操作 MIFARE Classic 卡片提供了高层接口。它建立在 READER.c
|
||||
* 提供的 Type A 协议基础之上,封装了 MIFARE 卡特有的操作,如密码认证、
|
||||
* 块读写和值块操作。
|
||||
* 主应用逻辑通过包含此文件并调用其声明的函数,来完成对 MIFARE 卡的
|
||||
* 具体业务操作。
|
||||
*/
|
||||
#ifndef _MIFARE_H_
|
||||
#define _MIFARE_H_
|
||||
|
||||
//==================================================================================
|
||||
// 1. 宏定义
|
||||
//==================================================================================
|
||||
#define KEY_A_M1 0 // 定义认证模式为 Key A
|
||||
#define KEY_B_M1 1 // 定义认证模式为 Key B
|
||||
|
||||
//==================================================================================
|
||||
// 2. 全局变量声明
|
||||
// 这些变量用于在不同函数间传递操作上下文。
|
||||
//==================================================================================
|
||||
extern unsigned char SECTOR; // 当前要操作的扇区号
|
||||
extern unsigned char BLOCK; // 当前要操作的扇区内块号 (0-3)
|
||||
extern unsigned char BLOCK_NUM; // 当前要操作的块的绝对地址 (0-63 for 1K)
|
||||
extern unsigned char BLOCK_DATA[16]; // 用于暂存读/写块数据的16字节缓冲区
|
||||
extern unsigned char KEY_A[16][6]; // 存储16个扇区的 Key A 密钥
|
||||
extern unsigned char KEY_B[16][6]; // 存储16个扇区的 Key B 密钥
|
||||
|
||||
//==================================================================================
|
||||
// 3. 函数原型声明
|
||||
//==================================================================================
|
||||
|
||||
/** @brief MIFARE 卡事件处理流程 (示例函数) */
|
||||
extern unsigned char MIFARE_CARD_EVENT(void);
|
||||
|
||||
/** @brief 清除芯片的 MIFARE 加密状态 */
|
||||
extern void Mifare_Clear_Crypto(void);
|
||||
|
||||
/** @brief 加载6字节密钥到读卡器芯片内部的密钥缓冲器 */
|
||||
unsigned char Mifare_LoadKey(unsigned char *mifare_key);
|
||||
|
||||
/** @brief **核心函数**: 对指定扇区进行密码认证 */
|
||||
extern unsigned char Mifare_Auth(unsigned char key_mode, unsigned char sector, unsigned char *mifare_key, unsigned char *card_uid);
|
||||
|
||||
/** @brief 读取一个16字节的数据块 */
|
||||
extern unsigned char Mifare_Blockread(unsigned char block, unsigned char *data_buff);
|
||||
|
||||
/** @brief 写入一个16字节的数据块 */
|
||||
extern unsigned char Mifare_Blockwrite(unsigned char block, unsigned char *data_buff);
|
||||
|
||||
// --- 值块操作函数 (用于电子钱包等应用) ---
|
||||
|
||||
/** @brief 将一个块格式化为值块 */
|
||||
extern unsigned char Mifare_Blockset(unsigned char block, unsigned char *data_buff);
|
||||
|
||||
/** @brief 对值块进行加值操作 */
|
||||
extern unsigned char Mifare_Blockinc(unsigned char block, unsigned char *data_buff);
|
||||
|
||||
/** @brief 对值块进行减值操作 */
|
||||
extern unsigned char Mifare_Blockdec(unsigned char block, unsigned char *data_buff);
|
||||
|
||||
/** @brief 将值块缓冲区的数据传送到数据块 */
|
||||
extern unsigned char Mifare_Transfer(unsigned char block);
|
||||
|
||||
/** @brief 将数据块内容加载到值块缓冲区 */
|
||||
extern unsigned char Mifare_Restore(unsigned char block);
|
||||
|
||||
#endif
|
||||
83
apps/earphone/94_rfid_stc/inc/board.h
Normal file
83
apps/earphone/94_rfid_stc/inc/board.h
Normal file
@ -0,0 +1,83 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file board.h
|
||||
* @author SRG
|
||||
* @version V1.0.0
|
||||
* @date 2020-03-17
|
||||
* @brief
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* <h2><center>© Copyright (c) 2020 FudanMicroelectronics.
|
||||
* All rights reserved.</center></h2>
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#ifndef __BOARD_H_
|
||||
#define __BOARD_H_
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
//#include "fm15l0xx.h"
|
||||
|
||||
|
||||
|
||||
//typedef enum
|
||||
//{
|
||||
// Mode_In_Up = 0, //<2F><><EFBFBD>٣<EFBFBD>pullup
|
||||
// Mode_In_No, //<2F><><EFBFBD>٣<EFBFBD>nopull
|
||||
// Mode_In_Down, //<2F><><EFBFBD>٣<EFBFBD>pulldown
|
||||
// Mode_Out_Up, //<2F><><EFBFBD>٣<EFBFBD><D9A3><EFBFBD><EFBFBD>죬pullup
|
||||
// Mode_Out_No, //<2F><><EFBFBD>٣<EFBFBD><D9A3><EFBFBD><EFBFBD>죬nopull
|
||||
// Mode_Out_Down, //<2F><><EFBFBD>٣<EFBFBD><D9A3><EFBFBD><EFBFBD>죬pulldown
|
||||
//} eGPIOMode;
|
||||
|
||||
|
||||
//HMI LED
|
||||
|
||||
|
||||
#define LED1_PIN LL_GPIO_Pin3 //PC3
|
||||
#define LED2_PIN LL_GPIO_Pin4 //PC4
|
||||
#define LED_GPIO GPIOC
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#define LED1_On LL_GPIO_SetOutputPin( LED_GPIO, LED1_PIN )
|
||||
#define LED1_Off LL_GPIO_ResetOutputPin( LED_GPIO, LED1_PIN )
|
||||
|
||||
#define LED2_On LL_GPIO_SetOutputPin( LED_GPIO, LED2_PIN )
|
||||
#define LED2_Off LL_GPIO_ResetOutputPin( LED_GPIO, LED2_PIN )
|
||||
|
||||
|
||||
|
||||
|
||||
#define TEST_ADC_CH LL_ADC_CHANNEL_1
|
||||
|
||||
uint8_t UserKey_GetData( void );
|
||||
|
||||
void GPIO_NVIC_Init( void );
|
||||
|
||||
void Set_Clk(unsigned char mode);
|
||||
void SVD_Init( void );
|
||||
void BSP_Init( void );
|
||||
|
||||
uint16_t Get_Adc_Average( uint32_t ch, uint32_t ch_in, uint8_t cnt );
|
||||
|
||||
void LowPower_test( void );
|
||||
|
||||
void MCU_EnterLowerPowerMode( void );
|
||||
|
||||
void Set_Output(unsigned char mode);
|
||||
void Set_Pwm(unsigned char mode);
|
||||
void Set_FM440_Power(unsigned char mode);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
10
apps/earphone/94_rfid_stc/inc/bsp.h
Normal file
10
apps/earphone/94_rfid_stc/inc/bsp.h
Normal file
@ -0,0 +1,10 @@
|
||||
#include "main.h"
|
||||
|
||||
void bsp_init( void );
|
||||
|
||||
|
||||
void gpio_init( void );
|
||||
|
||||
|
||||
void uart_init( void );
|
||||
|
||||
46
apps/earphone/94_rfid_stc/inc/fm15l0xx_it.h
Normal file
46
apps/earphone/94_rfid_stc/inc/fm15l0xx_it.h
Normal file
@ -0,0 +1,46 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file Templates_LL/Inc/fm15l0xx_it.h
|
||||
* @author FMSH AS Embedded Software Team
|
||||
* @brief This file contains the headers of the interrupt handlers.
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __FM15L0XX_IT_H__
|
||||
#define __FM15L0XX_IT_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
// #include "main.h"
|
||||
|
||||
/* Exported types ------------------------------------------------------------*/
|
||||
/* Exported constants --------------------------------------------------------*/
|
||||
/* Exported macro ------------------------------------------------------------*/
|
||||
/* Exported functions ------------------------------------------------------- */
|
||||
|
||||
void NMI_Handler( void );
|
||||
|
||||
|
||||
void HardFault_Handler( void );
|
||||
|
||||
|
||||
void SVC_Handler( void );
|
||||
|
||||
|
||||
void PendSV_Handler( void );
|
||||
|
||||
|
||||
void SysTick_Handler( void );
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __FM15L0XX_IT_H__ */
|
||||
|
||||
/************************ (C) COPYRIGHT FMSH Microelectronics *****END OF FILE****/
|
||||
146
apps/earphone/94_rfid_stc/inc/fm15l0xx_ll_rng.h
Normal file
146
apps/earphone/94_rfid_stc/inc/fm15l0xx_ll_rng.h
Normal file
@ -0,0 +1,146 @@
|
||||
/**
|
||||
************************************************************************************************************
|
||||
* @file : fm15l0xx_ll_rng.h
|
||||
* @author : weifan
|
||||
* @version : V1.0.0
|
||||
* @date : 2020-12-15
|
||||
* @brief :
|
||||
************************************************************************************************************
|
||||
* @attention :
|
||||
*
|
||||
* <h2><center>© Copyright (c) 2020 FudanMicroelectronics.
|
||||
* All rights reserved.</center></h2>
|
||||
*
|
||||
************************************************************************************************************
|
||||
*/
|
||||
|
||||
#ifndef __FM15L0XX_LL_RNG_H__
|
||||
#define __FM15L0XX_LL_RNG_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "fm15l0xx.h"
|
||||
|
||||
// ----------------------------------------------------------------------------------------------------
|
||||
// @descrption : This Function Set RegBit 'RNG_CR_RNGEN' Value
|
||||
// @arguments : *RNGx : Struct RNG_Typedef Pointer
|
||||
// @return : None
|
||||
// ----------------------------------------------------------------------------------------------------
|
||||
__STATIC_INLINE void LL_RNG_Enable( void )
|
||||
{
|
||||
SET_BIT( RNG->CR, RNG_CR_RNGEN_MSK );
|
||||
}
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------------------------------------
|
||||
// @descrption : This Function Set RegBit 'RNG_CR_RNGEN' Value
|
||||
// @arguments : *RNGx : Struct RNG_Typedef Pointer
|
||||
// @return : None
|
||||
// ----------------------------------------------------------------------------------------------------
|
||||
__STATIC_INLINE void LL_RNG_Disable( void )
|
||||
{
|
||||
CLEAR_BIT( RNG->CR, RNG_CR_RNGEN_MSK );
|
||||
}
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------------------------------------
|
||||
// @descrption : This Function Get RegBit 'RNG_CR_RNGEN' Value
|
||||
// @arguments : *RNGx : Struct RNG_Typedef Pointer
|
||||
// @retrun : RNGEN : uint32_t, RNGEN
|
||||
// ----------------------------------------------------------------------------------------------------
|
||||
__STATIC_INLINE uint32_t LL_RNG_IsEnabled( void )
|
||||
{
|
||||
return ( (uint32_t) (READ_BIT( RNG->CR, RNG_CR_RNGEN_MSK ) == RNG_CR_RNGEN_MSK) );
|
||||
}
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------------------------------------
|
||||
// @descrption : This Function Set RegBit 'RNG_CR_SRCEN' Value
|
||||
// @arguments : *RNGx : Struct RNG_Typedef Pointer
|
||||
// @return : None
|
||||
// ----------------------------------------------------------------------------------------------------
|
||||
__STATIC_INLINE void LL_RNG_EnableNoiseSource( void )
|
||||
{
|
||||
SET_BIT( RNG->CR, RNG_CR_SRCEN_MSK );
|
||||
}
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------------------------------------
|
||||
// @descrption : This Function Set RegBit 'RNG_CR_SRCEN' Value
|
||||
// @arguments : *RNGx : Struct RNG_Typedef Pointer
|
||||
// @return : None
|
||||
// ----------------------------------------------------------------------------------------------------
|
||||
__STATIC_INLINE void LL_RNG_DisableNoiseSource( void )
|
||||
{
|
||||
CLEAR_BIT( RNG->CR, RNG_CR_SRCEN_MSK );
|
||||
}
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------------------------------------
|
||||
// @descrption : This Function Get RegBit 'RNG_CR_SRCEN' Value
|
||||
// @arguments : *RNGx : Struct RNG_Typedef Pointer
|
||||
// @retrun : SRCEN : uint32_t, SRCEN
|
||||
// ----------------------------------------------------------------------------------------------------
|
||||
__STATIC_INLINE uint32_t LL_RNG_IsEnabledNoiseSource( void )
|
||||
{
|
||||
return ( (uint32_t) (READ_BIT( RNG->CR, RNG_CR_SRCEN_MSK ) == RNG_CR_SRCEN_MSK) );
|
||||
}
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------------------------------------
|
||||
// @descrption : This Function Get RegBit 'RNG_SR_DRDY' Value
|
||||
// @arguments : *RNGx : Struct RNG_Typedef Pointer
|
||||
// @retrun : DRDY : uint32_t, DRDY
|
||||
// ----------------------------------------------------------------------------------------------------
|
||||
__STATIC_INLINE uint32_t LL_RNG_IsDataReady( void )
|
||||
{
|
||||
return ( (uint32_t) (READ_BIT( RNG->SR, RNG_SR_DRDY_MSK ) == RNG_SR_DRDY_MSK) );
|
||||
}
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------------------------------------
|
||||
// @descrption : This Function Set RegBit 'RNG_SR_SFAIL' Value
|
||||
// @arguments : *RNGx : Struct RNG_Typedef Pointer
|
||||
// SFAIL : uint32_t, SFAIL
|
||||
// @return : None
|
||||
// ----------------------------------------------------------------------------------------------------
|
||||
__STATIC_INLINE void LL_RNG_ClearFlag_SFAIL( void )
|
||||
{
|
||||
RNG->SR = RNG_SR_SFAIL_MSK;
|
||||
}
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------------------------------------
|
||||
// @descrption : This Function Get RegBit 'RNG_SR_SFAIL' Value
|
||||
// @arguments : *RNGx : Struct RNG_Typedef Pointer
|
||||
// @retrun : SFAIL : uint32_t, SFAIL
|
||||
// ----------------------------------------------------------------------------------------------------
|
||||
__STATIC_INLINE uint32_t LL_RNG_IsActiveFlag_SFAIL( void )
|
||||
{
|
||||
return ( (uint32_t) (READ_BIT( RNG->SR, RNG_SR_SFAIL_MSK ) == RNG_SR_SFAIL_MSK) );
|
||||
}
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------------------------------------
|
||||
// @descrption : This Function Get RegBit 'RNG_DR_RNGOUT' Value
|
||||
// @arguments : *RNGx : Struct RNG_Typedef Pointer
|
||||
// @retrun : RNGOUT : uint32_t, RNGOUT
|
||||
// ----------------------------------------------------------------------------------------------------
|
||||
__STATIC_INLINE uint32_t LL_RNG_GetRandomData( void )
|
||||
{
|
||||
return ( (uint32_t) (READ_BIT( RNG->DR, RNG_DR_RNGOUT_MSK ) ) );
|
||||
}
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif // __cplusplus
|
||||
|
||||
|
||||
#endif // __FM15L0XX_LL_RNG_H__
|
||||
|
||||
|
||||
/************************ (C) COPYRIGHT FudanMicroelectronics *****END OF FILE****/
|
||||
|
||||
191
apps/earphone/94_rfid_stc/inc/function.h
Normal file
191
apps/earphone/94_rfid_stc/inc/function.h
Normal file
@ -0,0 +1,191 @@
|
||||
/**
|
||||
************************************* Copyright ******************************
|
||||
* (C) Copyright 2019,TuYW,FMSH,China.
|
||||
* All Rights Reserved
|
||||
* By(Shanghai Fudan MicroeleCardTyperonics Group Company Limited)
|
||||
* https://www.fmsh.com
|
||||
*
|
||||
* FileName : function.h
|
||||
* Version : v1.0
|
||||
* Author : TuYW
|
||||
* Date : 2019-03-19
|
||||
* Description:
|
||||
*******************************************************************************/
|
||||
|
||||
|
||||
#ifndef __FUNCTION_H_
|
||||
#define __FUNCTION_H_
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
//#include "stdint.h"
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
|
||||
// #include "fm15l0xx.h"
|
||||
//#include "core_cm3.h"
|
||||
//#include "sys.h"
|
||||
|
||||
|
||||
//#if APP_NO_IAP == 1
|
||||
// #define APP_ADDR 1
|
||||
//#endif
|
||||
|
||||
/* Exported types ------------------------------------------------------------*/
|
||||
#define REG32( addr ) (*( ( volatile unsigned long *) (addr) ) )
|
||||
|
||||
//#define assert_param(expr) ((void)0)
|
||||
|
||||
typedef unsigned char BOOLEAN;
|
||||
typedef unsigned char INT8U; /* Unsigned 8 bit quantity */
|
||||
typedef signed char INT8S; /* Signed 8 bit quantity */
|
||||
typedef unsigned short INT16U; /* Unsigned 16 bit quantity */
|
||||
typedef signed short INT16S; /* Signed 16 bit quantity */
|
||||
typedef unsigned int INT32U; /* Unsigned 32 bit quantity */
|
||||
typedef signed int INT32S; /* Signed 32 bit quantity */
|
||||
typedef float FP32; /* Single precision floating point */
|
||||
typedef double FP64; /* Double precision floating point */
|
||||
|
||||
|
||||
/*********************************************************************************************************
|
||||
ͨ<>ú궨<C3BA><EAB6A8>
|
||||
*********************************************************************************************************/
|
||||
#define PROGRAM_IAP_BASE FLASH_IAP_ADDR
|
||||
#define PROGRAM_IAP_LENGTH 0X6000
|
||||
|
||||
#define PROGRAM_APP_BASE FLASH_APP1_ADDR
|
||||
#define PROGRAM_APP_LENGTH 0X30000
|
||||
|
||||
#ifndef HIGH
|
||||
#define HIGH 1
|
||||
#endif
|
||||
|
||||
#ifndef LOW
|
||||
#define LOW 0
|
||||
#endif
|
||||
|
||||
#ifndef TRUE
|
||||
#define TRUE 1
|
||||
#endif //TRUE
|
||||
|
||||
#ifndef FALSE
|
||||
#define FALSE 0
|
||||
#endif //FALSE
|
||||
|
||||
#ifndef ON
|
||||
#define ON 1
|
||||
#endif //ON
|
||||
|
||||
#ifndef OFF
|
||||
#define OFF 0
|
||||
#endif //OFF
|
||||
|
||||
#ifndef NULL
|
||||
#define NULL ( (void *) 0)
|
||||
//<2F><>ַΪ0<CEAA><30>ָ<EFBFBD><D6B8>,void*<2A><><EFBFBD>͵<EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>Ĭ<EFBFBD><C4AC>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD>κ<EFBFBD><CEBA><EFBFBD><EFBFBD>͵<EFBFBD>ָ<EFBFBD>롣<EFBFBD><EBA1A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǵĿ<C7B5>ָ<EFBFBD><D6B8>Ͷ<EFBFBD><CDB6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
#endif
|
||||
|
||||
//<2F><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
//#define ClrLongBuf(buf) memset(buf, 0x00, sizeof(buf));
|
||||
|
||||
|
||||
/** The upper 8 bits of a 32 bit value */
|
||||
//lint -emacro(572,MSB) // Suppress warning 572 "Excessive shift value"
|
||||
#define MSB_32( a ) ( ( (a) & 0xFF000000) >> 24)
|
||||
/** The lower 8 bits (of a 32 bit value) */
|
||||
#define LSB_32( a ) ( (a) & 0x000000FF)
|
||||
|
||||
/** The upper 8 bits of a 16 bit value */
|
||||
//lint -emacro(572,MSB_16) // Suppress warning 572 "Excessive shift value"
|
||||
#define MSB_16( a ) ( ( (a) & 0xFF00) >> 8)
|
||||
/** The lower 8 bits (of a 16 bit value) */
|
||||
#define LSB_16( a ) ( (a) & 0x00FF)
|
||||
|
||||
///** Leaves the minimum of the two 32-bit arguments */
|
||||
///*lint -emacro(506, MIN) */ /* Suppress "Constant value Boolean */
|
||||
//#define MIN( a, b ) ( (a) < (b) ? (a) : (b) )
|
||||
///** Leaves the maximum of the two 32-bit arguments */
|
||||
///*lint -emacro(506, MAX) */ /* Suppress "Constant value Boolean */
|
||||
//#define MAX( a, b ) ( (a) < (b) ? (b) : (a) )
|
||||
|
||||
|
||||
/** Concatenates two parameters. Useful as a second level of indirection,
|
||||
* when a parameter can be macro itself. */
|
||||
#define CONCAT_2( p1, p2 ) p1 ## p2
|
||||
|
||||
|
||||
/** Concatenates three parameters. Useful as a second level of indirection,
|
||||
* when a parameter can be macro itself. */
|
||||
#define CONCAT_3( p1, p2, p3 ) p1 ## p2 ## p3
|
||||
|
||||
|
||||
/**@brief Set a bit in the uint32 word.
|
||||
*
|
||||
* @param[in] W Word whose bit is being set.
|
||||
* @param[in] B Bit number in the word to be set.
|
||||
*/
|
||||
#ifndef SET_BIT
|
||||
#define SET_BIT( W, B ) ( (W) |= (uint32_t) (1U << (B) ) )
|
||||
#endif
|
||||
|
||||
|
||||
/**@brief Clears a bit in the uint32 word.
|
||||
*
|
||||
* @param[in] W Word whose bit is to be cleared.
|
||||
* @param[in] B Bit number in the word to be cleared.
|
||||
*/
|
||||
#ifndef SET_BIT
|
||||
#define CLR_BIT( W, B ) ( (W) &= (~( (uint32_t) 1 << (B) ) ) )
|
||||
#endif
|
||||
|
||||
//typedef unsigned char BOOLEAN;
|
||||
|
||||
typedef unsigned char UINT8, U8, BYTE, u8, uint8_t;
|
||||
typedef unsigned short UINT16, U16, u16, uint16_t;
|
||||
typedef unsigned int UINT32, U32, DWORD, u32, uint32_t;
|
||||
|
||||
//typedef int32_t s32;
|
||||
//typedef int16_t s16;
|
||||
//typedef int8_t s8;
|
||||
|
||||
//typedef const int32_t sc32; /*!< Read Only */
|
||||
//typedef const int16_t sc16; /*!< Read Only */
|
||||
//typedef const int8_t sc8; /*!< Read Only */
|
||||
|
||||
//typedef __IO int32_t vs32;
|
||||
//typedef __IO int16_t vs16;
|
||||
//typedef __IO int8_t vs8;
|
||||
|
||||
//typedef __I int32_t vsc32; /*!< Read Only */
|
||||
//typedef __I int16_t vsc16; /*!< Read Only */
|
||||
//typedef __I int8_t vsc8; /*!< Read Only */
|
||||
|
||||
//typedef uint32_t u32;
|
||||
//typedef uint16_t u16;
|
||||
//typedef uint8_t u8;
|
||||
|
||||
//typedef const uint32_t uc32; /*!< Read Only */
|
||||
//typedef const uint16_t uc16; /*!< Read Only */
|
||||
//typedef const uint8_t uc8; /*!< Read Only */
|
||||
|
||||
//typedef __IO uint32_t vu32;
|
||||
//typedef __IO uint16_t vu16;
|
||||
//typedef __IO uint8_t vu8;
|
||||
|
||||
//typedef __I uint32_t vuc32; /*!< Read Only */
|
||||
//typedef __I uint16_t vuc16; /*!< Read Only */
|
||||
//typedef __I uint8_t vuc8; /*!< Read Only */
|
||||
|
||||
typedef void (*pFunction)( void );
|
||||
|
||||
void StrToHex( BYTE *pbDest, BYTE *pbSrc, int nLen );
|
||||
|
||||
|
||||
void HexToStr( BYTE *pbDest, BYTE *pbSrc, int nLen );
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
64
apps/earphone/94_rfid_stc/inc/main.h
Normal file
64
apps/earphone/94_rfid_stc/inc/main.h
Normal file
@ -0,0 +1,64 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file Templates_LL/Inc/main.h
|
||||
* @author FMSH AS Embedded Software Team
|
||||
* @brief Header for main.c module
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* <h2><center>© COPYRIGHT(c) 2016 FMSH Microelectronics</center></h2>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of FMSH Microelectronics nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __MAIN_H__
|
||||
#define __MAIN_H__
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
/* LL drivers common to all LL examples */
|
||||
//#include "fm15l0xx_ll_cmu.h"
|
||||
/* LL drivers specific to LL examples IPs */
|
||||
//#include "fm15l0xx_ll_gpio.h"
|
||||
//#include "fm15l0xx_ll_uart.h"
|
||||
|
||||
|
||||
//#if defined(USE_FULL_ASSERT)
|
||||
// #include "fm15l0xx_assert.h"
|
||||
//#endif /* USE_FULL_ASSERT */
|
||||
|
||||
/* Exported types ------------------------------------------------------------*/
|
||||
/* Exported constants --------------------------------------------------------*/
|
||||
|
||||
/* ============== BOARD SPECIFIC CONFIGURATION CODE BEGIN ============== */
|
||||
/* ============== BOARD SPECIFIC CONFIGURATION CODE END ============== */
|
||||
|
||||
/* Exported macro ------------------------------------------------------------*/
|
||||
#define UARTx UART0
|
||||
/* Exported functions ------------------------------------------------------- */
|
||||
int vdd_is_low( void );
|
||||
#endif /* __MAIN_H__ */
|
||||
|
||||
/************************ (C) COPYRIGHT FMSH Microelectronics *****END OF FILE****/
|
||||
11
apps/earphone/94_rfid_stc/inc/print.h
Normal file
11
apps/earphone/94_rfid_stc/inc/print.h
Normal file
@ -0,0 +1,11 @@
|
||||
#ifndef __PRINT_H_
|
||||
#define __PRINT_H_
|
||||
extern unsigned char lsfr[48];
|
||||
extern unsigned char lsfr_length;
|
||||
extern unsigned char ciphertext_to_binary[48];
|
||||
extern void Print_AUTH(unsigned char *key,unsigned char *TOKEN,unsigned char *rece_buff);//print <20><>֤
|
||||
extern void lsfr_init(unsigned char *S022_Token2);
|
||||
extern unsigned char lsfr_fm458(void);
|
||||
extern unsigned char PRINT_Calc_sendData(unsigned char *data_buf, unsigned char data_len);
|
||||
extern unsigned char PRINT_Calc_recvData(unsigned char *data_buf, unsigned char data_len);
|
||||
#endif
|
||||
7
apps/earphone/94_rfid_stc/inc/random_generator.h
Normal file
7
apps/earphone/94_rfid_stc/inc/random_generator.h
Normal file
@ -0,0 +1,7 @@
|
||||
#include "main.h"
|
||||
|
||||
void RNG_init( void );
|
||||
|
||||
|
||||
uint32_t Get_RandomNumber( void );
|
||||
|
||||
55
apps/earphone/94_rfid_stc/xt_main.h
Normal file
55
apps/earphone/94_rfid_stc/xt_main.h
Normal file
@ -0,0 +1,55 @@
|
||||
#define uchar unsigned char
|
||||
#define uint unsigned int
|
||||
//typedef unsigned char u8;
|
||||
//typedef unsigned int u16;
|
||||
//typedef unsigned long u32;
|
||||
|
||||
|
||||
#define BIT5 0x00000020U
|
||||
#define BIT6 0x00000040U
|
||||
#define BIT7 0x00000080U
|
||||
|
||||
|
||||
#define BIT0 0x00000001U
|
||||
#define BIT1 0x00000002U
|
||||
#define BIT2 0x00000004U
|
||||
#define BIT3 0x00000008U
|
||||
#define BIT4 0x00000010U
|
||||
#define BIT5 0x00000020U
|
||||
|
||||
|
||||
void Uart1Send(unsigned char dat);
|
||||
void Uart1SendString(unsigned char *str);
|
||||
|
||||
void printHex(unsigned char num);
|
||||
|
||||
typedef enum {
|
||||
FAIL = 0U,
|
||||
SUCCESS = !FAIL
|
||||
} ErrorStatus;
|
||||
|
||||
typedef enum {
|
||||
DISABLE = 0U,
|
||||
ENABLE = !DISABLE
|
||||
} FunState;
|
||||
|
||||
typedef enum {
|
||||
RESET = 0U,
|
||||
SET = !RESET
|
||||
} FlagStatus, ITStatus;
|
||||
|
||||
|
||||
typedef struct
|
||||
{
|
||||
unsigned char SendLength;
|
||||
unsigned char *pSendBuffer;
|
||||
unsigned char ReceiveLength;
|
||||
unsigned char *pReceiveBuffer;
|
||||
unsigned int Timeout;
|
||||
}transmission_struct;
|
||||
|
||||
|
||||
|
||||
unsigned char GetReg(unsigned char address,unsigned char *reg_data);
|
||||
unsigned char SetReg(unsigned char address, unsigned char reg_data);
|
||||
void Delay1ms();
|
||||
@ -81,10 +81,10 @@ const struct task_info task_info_table[] = {
|
||||
#if AUDIO_ENC_MPT_SELF_ENABLE
|
||||
{"enc_mpt_self", 3, 0, 512, 128 },
|
||||
#endif/*AUDIO_ENC_MPT_SELF_ENABLE*/
|
||||
// {"update", 1, 0, 256, 0 },
|
||||
// {"tws_ota", 2, 0, 256, 0 },
|
||||
// {"tws_ota_msg", 2, 0, 256, 128 },
|
||||
// {"dw_update", 2, 0, 256, 128 },
|
||||
{"update", 1, 0, 256, 0 },
|
||||
{"tws_ota", 2, 0, 256, 0 },
|
||||
{"tws_ota_msg", 2, 0, 256, 128 },
|
||||
{"dw_update", 2, 0, 256, 128 },
|
||||
{"rcsp_task", 2, 0, 640, 128 },
|
||||
// {"aud_capture", 4, 0, 512, 256 },
|
||||
// {"data_export", 5, 0, 512, 256 },
|
||||
|
||||
@ -32,9 +32,9 @@
|
||||
// IIC配置 //
|
||||
//*********************************************************************************//
|
||||
/*软件IIC设置*/ //xtell
|
||||
#define TCFG_SW_I2C0_CLK_PORT IO_PORTA_05 //软件IIC CLK脚选择 XTELL
|
||||
#define TCFG_SW_I2C0_DAT_PORT IO_PORTA_06 //软件IIC DAT脚选择
|
||||
#define TCFG_SW_I2C0_DELAY_CNT 10 //IIC延时参数,影响通讯时钟频率
|
||||
#define TCFG_SW_I2C0_CLK_PORT IO_PORTB_04 //软件IIC CLK脚选择 XTELL
|
||||
#define TCFG_SW_I2C0_DAT_PORT IO_PORTB_05 //软件IIC DAT脚选择
|
||||
#define TCFG_SW_I2C0_DELAY_CNT 0 //IIC延时参数,影响通讯时钟频率
|
||||
|
||||
/*硬件IIC端口选择 -- 具体看手册,这里写的不准 -- lmx
|
||||
SCL SDA
|
||||
@ -47,15 +47,15 @@
|
||||
|
||||
*/
|
||||
#define TCFG_HW_I2C0_PORTS 'B'
|
||||
#define TCFG_HW_I2C0_CLK 1000000 //硬件IIC波特率:100k
|
||||
#define TCFG_HW_I2C0_CLK 100000
|
||||
|
||||
//*********************************************************************************//
|
||||
// 硬件SPI 配置 //
|
||||
//*********************************************************************************//
|
||||
#define TCFG_HW_SPI1_ENABLE 1
|
||||
#define TCFG_HW_SPI1_PORT_CLK IO_PORTC_04//IO_PORTC_04//IO_PORTA_00 xtellio
|
||||
#define TCFG_HW_SPI1_PORT_DO IO_PORTC_05//IO_PORTA_01
|
||||
#define TCFG_HW_SPI1_PORT_DI IO_PORTC_03//IO_PORTC_03//IO_PORTA_02 xtellio
|
||||
#define TCFG_HW_SPI1_PORT_CLK NO_CONFIG_PORT//IO_PORTC_04//IO_PORTC_04//IO_PORTA_00 xtellio
|
||||
#define TCFG_HW_SPI1_PORT_DO NO_CONFIG_PORT//IO_PORTC_05//IO_PORTA_01
|
||||
#define TCFG_HW_SPI1_PORT_DI NO_CONFIG_PORT//IO_PORTC_03//IO_PORTC_03//IO_PORTA_02 xtellio
|
||||
#define TCFG_HW_SPI1_BAUD 2400000L
|
||||
#define TCFG_HW_SPI1_MODE SPI_MODE_BIDIR_1BIT
|
||||
#define TCFG_HW_SPI1_ROLE SPI_ROLE_MASTER
|
||||
@ -87,7 +87,7 @@
|
||||
//*********************************************************************************//
|
||||
// USB 配置 //
|
||||
//*********************************************************************************//
|
||||
#define TCFG_PC_ENABLE 1//DISABLE_THIS_MOUDLE//PC模块使能
|
||||
#define TCFG_PC_ENABLE DISABLE_THIS_MOUDLE//PC模块使能
|
||||
#define TCFG_UDISK_ENABLE 0//ENABLE_THIS_MOUDLE//U盘模块使能
|
||||
#define TCFG_OTG_USB_DEV_EN BIT(0)//USB0 = BIT(0) USB1 = BIT(1)
|
||||
|
||||
@ -729,7 +729,7 @@ DAC硬件上的连接方式,可选的配置:
|
||||
// 充电舱/蓝牙测试盒/ANC测试三者为同级关系,开启任一功能都会初始化PP0通信接口 //
|
||||
//*********************************************************************************//
|
||||
#define TCFG_CHARGESTORE_ENABLE DISABLE_THIS_MOUDLE //是否支持智能充电舱
|
||||
#define TCFG_TEST_BOX_ENABLE ENABLE_THIS_MOUDLE //是否支持蓝牙测试盒 //xtell
|
||||
#define TCFG_TEST_BOX_ENABLE DISABLE_THIS_MOUDLE //是否支持蓝牙测试盒 //xtell
|
||||
#define TCFG_ANC_BOX_ENABLE CONFIG_ANC_ENABLE //是否支持ANC测试盒
|
||||
#define TCFG_UMIDIGI_BOX_ENABLE DISABLE_THIS_MOUDLE //是否支持UMIDIGI充电舱 //xtell
|
||||
#if TCFG_UMIDIGI_BOX_ENABLE
|
||||
@ -807,7 +807,7 @@ DAC硬件上的连接方式,可选的配置:
|
||||
// EQ配置 //
|
||||
//*********************************************************************************//
|
||||
//EQ配置,使用在线EQ时,EQ文件和EQ模式无效。有EQ文件时,使能TCFG_USE_EQ_FILE,默认不用EQ模式切换功能
|
||||
#define TCFG_EQ_ENABLE 1 //支持EQ功能,EQ总使能
|
||||
#define TCFG_EQ_ENABLE 0 //支持EQ功能,EQ总使能
|
||||
// #if TCFG_EQ_ENABLE
|
||||
#define TCFG_EQ_ONLINE_ENABLE 0 //支持在线EQ调试,如果使用蓝牙串口调试,需要打开宏 APP_ONLINE_DEBUG,否则,默认使用uart调试(二选一)
|
||||
#define TCFG_BT_MUSIC_EQ_ENABLE 1 //支持蓝牙音乐EQ
|
||||
@ -920,7 +920,7 @@ DAC硬件上的连接方式,可选的配置:
|
||||
#define TCFG_STK8321_EN 0
|
||||
#define TCFG_IRSENSOR_ENABLE 0
|
||||
#define TCFG_JSA1221_ENABLE 0
|
||||
#define TCFG_GSENOR_USER_IIC_TYPE 1 //0:软件IIC 1:硬件IIC
|
||||
#define TCFG_GSENOR_USER_IIC_TYPE 0 //0:软件IIC 1:硬件IIC
|
||||
|
||||
//*********************************************************************************//
|
||||
// imu-sensor配置 //
|
||||
@ -1010,7 +1010,7 @@ DAC硬件上的连接方式,可选的配置:
|
||||
//*********************************************************************************//
|
||||
#define TCFG_USER_TWS_ENABLE 0 //tws功能使能
|
||||
#define TCFG_USER_BLE_ENABLE 1 //BLE功能使能
|
||||
#define TCFG_BT_SUPPORT_AAC 1 //AAC格式支持
|
||||
#define TCFG_BT_SUPPORT_AAC 0 //AAC格式支持
|
||||
#define TCFG_BT_SUPPORT_LDAC 0 //LDAC格式支持
|
||||
|
||||
#if TCFG_BT_SUPPORT_LDAC
|
||||
@ -1084,7 +1084,7 @@ DAC硬件上的连接方式,可选的配置:
|
||||
// 编解码格式配置(CodecFormat) //
|
||||
//*********************************************************************************//
|
||||
/*解码格式使能*/
|
||||
#define TCFG_DEC_MP3_ENABLE ENABLE
|
||||
#define TCFG_DEC_MP3_ENABLE DISABLE
|
||||
#define TCFG_DEC_WTGV2_ENABLE ENABLE
|
||||
#define TCFG_DEC_G729_ENABLE DISABLE
|
||||
#define TCFG_DEC_WMA_ENABLE DISABLE
|
||||
|
||||
@ -8,8 +8,8 @@
|
||||
|
||||
/* Following Macros Affect Periods Of Both Code Compiling And Post-build */
|
||||
|
||||
#define CONFIG_DOUBLE_BANK_ENABLE 0 //单双备份选择(若打开了改宏,FLASH结构变为双备份结构,适用于接入第三方协议的OTA, PS: JL-OTA同样支持双备份升级, 需要根据实际FLASH大小同时配置CONFIG_FLASH_SIZE)
|
||||
#define CONFIG_APP_OTA_ENABLE 0 //是否支持RCSP升级(JL-OTA)
|
||||
#define CONFIG_DOUBLE_BANK_ENABLE 1 //单双备份选择(若打开了改宏,FLASH结构变为双备份结构,适用于接入第三方协议的OTA, PS: JL-OTA同样支持双备份升级, 需要根据实际FLASH大小同时配置CONFIG_FLASH_SIZE)
|
||||
#define CONFIG_APP_OTA_ENABLE 1 //是否支持RCSP升级(JL-OTA)
|
||||
|
||||
#define CONFIG_UPDATE_JUMP_TO_MASK 0 //配置升级到loader的方式0为直接reset,1为跳转(适用于芯片电源由IO口KEEP住的方案,需要注意检查跳转前是否将使用DMA的硬件模块全部关闭)
|
||||
|
||||
@ -33,7 +33,7 @@
|
||||
/* Following Macros Only For Post Bulid Configuaration */
|
||||
|
||||
#define CONFIG_DB_UPDATE_DATA_GENERATE_EN 0 //是否生成db_data.bin(用于第三方协议接入使用)
|
||||
#define CONFIG_ONLY_GRENERATE_ALIGN_4K_CODE 0 //ufw只生成1份4K对齐的代码
|
||||
#define CONFIG_ONLY_GRENERATE_ALIGN_4K_CODE 1 //ufw只生成1份4K对齐的代码
|
||||
|
||||
//config for supported chip version
|
||||
#ifdef CONFIG_BR30_C_VERSION
|
||||
|
||||
@ -70,6 +70,7 @@
|
||||
#include "bt_background.h"
|
||||
#include "default_event_handler.h"
|
||||
|
||||
|
||||
#define xlog(format, ...) printf("[%s] " format, __func__, ##__VA_ARGS__)
|
||||
|
||||
|
||||
@ -2790,3 +2791,4 @@ static int state_machine(struct application *app, enum app_state state, struct i
|
||||
// .ops = &app_earphone_ops,
|
||||
// .state = APP_STA_DESTROY,
|
||||
// };
|
||||
|
||||
|
||||
@ -65,11 +65,11 @@
|
||||
#if CONFIG_APP_OTA_ENABLE
|
||||
#define RCSP_UPDATE_EN 1 //是否支持rcsp升级
|
||||
#if CONFIG_DOUBLE_BANK_ENABLE //双备份才能打开同步升级流程
|
||||
#define OTA_TWS_SAME_TIME_ENABLE 1 //是否支持TWS同步升级
|
||||
#define OTA_TWS_SAME_TIME_NEW 1 //使用新的tws ota流程
|
||||
#define OTA_TWS_SAME_TIME_ENABLE 0 //是否支持TWS同步升级
|
||||
#define OTA_TWS_SAME_TIME_NEW 0 //使用新的tws ota流程
|
||||
#else
|
||||
#define OTA_TWS_SAME_TIME_ENABLE 1//0 xtellota //是否支持TWS同步升级
|
||||
#define OTA_TWS_SAME_TIME_NEW 1//0 //使用新的tws ota流程
|
||||
#define OTA_TWS_SAME_TIME_ENABLE 0 //xtell: 关闭TWS同步升级,允许单耳升级
|
||||
#define OTA_TWS_SAME_TIME_NEW 0 //xtell: 关闭TWS同步升级,此选项也应关闭
|
||||
#endif //CONFIG_DOUBLE_BANK_ENABLE
|
||||
#else
|
||||
#define RCSP_UPDATE_EN 0 //是否支持rcsp升级
|
||||
|
||||
@ -149,6 +149,7 @@
|
||||
#define BIT_HIALERTIRQ 0x40U
|
||||
#define BIT_LOALERTIRQ 0x20U
|
||||
#define BIT_IDLEIRQ 0x10U
|
||||
#define BIT_IDLE_IRQ BIT_IDLEIRQ /* Alias for compatibility */
|
||||
#define BIT_TXIRQ 0x08U
|
||||
#define BIT_RXIRQ 0x04U
|
||||
#define BIT_ERRIRQ 0x02U
|
||||
|
||||
@ -131,7 +131,7 @@ unsigned char SetCW(unsigned char mode)
|
||||
ModifyReg(REG_COMMAND, BIT_MODEMOFF, FUN_ENABLE);
|
||||
ModifyReg(REG_TXMODE, BIT_TPUSHON | BIT_TPULLON, FUN_DISABLE);
|
||||
}
|
||||
rfid_delay_ms(5);
|
||||
rfid_delay_ms(10);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
@ -598,11 +598,13 @@ unsigned char ReaderB_Get_SN(struct picc_b_struct *picc_b)
|
||||
*/
|
||||
unsigned char ReaderV_Inventory(struct picc_v_struct *picc_v)
|
||||
{
|
||||
unsigned char reg_data, i;
|
||||
unsigned char reg_data, i, result; // 新增 result 变量
|
||||
SetCommand(CMD_IDLE);
|
||||
|
||||
|
||||
SetReg(REG_TXDATANUM, 0x08);
|
||||
ModifyReg(REG_FIFOCONTROL, BIT_FIFOFLUSH, FUN_ENABLE);
|
||||
SetReg(REG_FIFODATA, 0x26);
|
||||
SetReg(REG_FIFODATA, 0x26); // Corrected Inventory flag to indicate AFI is present
|
||||
SetReg(REG_FIFODATA, 0x01);
|
||||
SetReg(REG_FIFODATA, 0x00);
|
||||
|
||||
@ -610,19 +612,28 @@ unsigned char ReaderV_Inventory(struct picc_v_struct *picc_v)
|
||||
ModifyReg(REG_RXCRCCON, BIT_CRCEN, FUN_ENABLE);
|
||||
|
||||
SetCommand(CMD_TRANSCEIVE);
|
||||
rfid_delay_ms(10);
|
||||
rfid_delay_ms(50);
|
||||
|
||||
GetReg(REG_ERROR, ®_data);
|
||||
if ((reg_data & 0x0F) != 0)
|
||||
if ((reg_data & 0x0F) != 0){
|
||||
xlog("REG_ERROR = 0x%02X\n", reg_data);
|
||||
return FAIL;
|
||||
}
|
||||
|
||||
GetReg(REG_FIFOLENGTH, ®_data);
|
||||
if (reg_data != 10)
|
||||
if (reg_data != 10){
|
||||
xlog("FIFO Length is %d, expected 10.\n", reg_data);
|
||||
return FAIL;
|
||||
}
|
||||
|
||||
GetReg(REG_FIFODATA, &picc_v->RESPONSE);
|
||||
GetReg(REG_FIFODATA, ®_data); // DSFID
|
||||
for (i = 0; i < 8; i++)
|
||||
{
|
||||
GetReg(REG_FIFODATA, &picc_v->UID[i]);
|
||||
// xlog("%d ",picc_v->UID[i]);
|
||||
}
|
||||
// xlog("\r\n");
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
@ -633,7 +644,7 @@ unsigned char ReaderV_Inventory(struct picc_v_struct *picc_v)
|
||||
*/
|
||||
unsigned char ReaderV_Select(struct picc_v_struct *picc_v)
|
||||
{
|
||||
unsigned char reg_data;
|
||||
unsigned char reg_data, result;
|
||||
SetCommand(CMD_IDLE);
|
||||
SetReg(REG_TXDATANUM, 0x08);
|
||||
ModifyReg(REG_FIFOCONTROL, BIT_FIFOFLUSH, FUN_ENABLE);
|
||||
|
||||
@ -62,7 +62,7 @@ void TYPE_A_EVENT(void)
|
||||
result = ReaderA_CardActivate(&PICC_A);
|
||||
if (result != SUCCESS)
|
||||
{
|
||||
// xlog("ReaderA_CardActivate_ERROR\r\n");
|
||||
xlog("ReaderA_CardActivate_ERROR\r\n");
|
||||
SetCW(FUN_DISABLE);
|
||||
return;
|
||||
}
|
||||
@ -201,7 +201,7 @@ void TYPE_V_EVENT(void)
|
||||
}
|
||||
|
||||
// 示例:写单个块
|
||||
memcpy(PICC_V.BLOCK_DATA, "\x11\x22\x33\x44", 4);
|
||||
memcpy(PICC_V.BLOCK_DATA, "\x00\x00\x00\x00", 4);
|
||||
result = ReaderV_WriteSingleBlock(4, &PICC_V);
|
||||
if (result != SUCCESS)
|
||||
{
|
||||
@ -272,8 +272,8 @@ void rfid_task_fuc(void)
|
||||
if(first_init == 0){
|
||||
first_init = 1;
|
||||
|
||||
// rfid_hal_init();
|
||||
FM176XX_HardInit();
|
||||
rfid_delay_ms(5); // 硬件初始化后增加一个短暂延时,确保芯片稳定
|
||||
// 2. 复位 FM176XX 芯片
|
||||
|
||||
result = FM176XX_SoftReset();
|
||||
@ -286,8 +286,11 @@ void rfid_task_fuc(void)
|
||||
xlog("FM176XX HardReset SUCCESS\r\n");
|
||||
}
|
||||
|
||||
|
||||
rfid_delay_ms(10); // 复位后延时
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 3. 读取芯片版本号,确认通信是否正常
|
||||
GetReg(REG_VERSION, ®_data);
|
||||
|
||||
@ -14,11 +14,25 @@
|
||||
#define xlog(format, ...) ((void)0)
|
||||
#endif
|
||||
|
||||
#define INTERFACE_TYPE 1
|
||||
/*
|
||||
IFSEL1 IFSEL0 Host_Interface
|
||||
0 0 UART
|
||||
1 0 SPI
|
||||
0 1 IIC
|
||||
1 1 SPI
|
||||
INTERFACE_TYPE == 0:iic
|
||||
== 1: spi
|
||||
== 2: uart
|
||||
*/
|
||||
#define INTERFACE_TYPE 0
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
|
||||
#if INTERFACE_TYPE == 0 //iic接口
|
||||
/*
|
||||
注:iic读取rfid不建议使用底层的硬件iic,很容易造成程序崩溃,并且时序有时候对不上会造成读写信息错误
|
||||
这里利用底层写好的软件iic接口来实现寄存器的读写
|
||||
*/
|
||||
|
||||
/*
|
||||
IF2 IF0 ADDR
|
||||
@ -27,13 +41,17 @@
|
||||
1 0 0x2A
|
||||
1 1 0x2B
|
||||
*/
|
||||
#define FM176_7BIT_ADDR 0x2A //后两位地址由IF2、IF1决定
|
||||
#define FM176_READ_ADDR (FM176_7BIT_ADDR << 1)
|
||||
#define FM176_WRITE_ADDR ((FM176_7BIT_ADDR << 1) | 0x01)
|
||||
#define FM176_7BIT_ADDR 0x28 //后两位地址由IF2、IF1决定
|
||||
#define FM176_WRITE_ADDR (FM176_7BIT_ADDR << 1)
|
||||
#define FM176_READ_ADDR ((FM176_7BIT_ADDR << 1) | 0x01)
|
||||
|
||||
|
||||
unsigned char FM176XX_HardInit(void){
|
||||
#if TCFG_GSENOR_USER_IIC_TYPE == 1// 硬件iic
|
||||
int ret = hw_iic_init(0);
|
||||
#else
|
||||
int ret = soft_iic_init(0);
|
||||
#endif
|
||||
xlog("init iic result:%d\n", ret); //返回0成功
|
||||
}
|
||||
|
||||
@ -46,11 +64,23 @@ unsigned char FM176XX_HardInit(void){
|
||||
* 接口:iic
|
||||
*/
|
||||
unsigned char GetReg(unsigned char address, unsigned char *reg_data){
|
||||
if(_gravity_sensor_get_ndata(FM176_READ_ADDR, address, reg_data, 1)){
|
||||
return SUCCESS;
|
||||
}else{
|
||||
soft_iic_start(0);
|
||||
if (0 == soft_iic_tx_byte(0, FM176_WRITE_ADDR)) {
|
||||
soft_iic_stop(0);
|
||||
return FAIL;
|
||||
}
|
||||
if (0 == soft_iic_tx_byte(0, address)) {
|
||||
soft_iic_stop(0);
|
||||
return FAIL;
|
||||
}
|
||||
soft_iic_start(0);
|
||||
if (0 == soft_iic_tx_byte(0, FM176_READ_ADDR)) {
|
||||
soft_iic_stop(0);
|
||||
return FAIL;
|
||||
}
|
||||
*reg_data = soft_iic_rx_byte(0, 0);
|
||||
soft_iic_stop(0);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
@ -63,11 +93,21 @@ unsigned char GetReg(unsigned char address, unsigned char *reg_data){
|
||||
* 接口:iic
|
||||
*/
|
||||
unsigned char SetReg(unsigned char address, unsigned char reg_data){
|
||||
if(gravity_sensor_command(FM176_WRITE_ADDR, address, reg_data) == 0){
|
||||
soft_iic_start(0);
|
||||
if (0 == soft_iic_tx_byte(0, FM176_WRITE_ADDR)) {
|
||||
soft_iic_stop(0);
|
||||
return FAIL;
|
||||
}else{
|
||||
return SUCCESS;
|
||||
}
|
||||
if (0 == soft_iic_tx_byte(0, address)) {
|
||||
soft_iic_stop(0);
|
||||
return FAIL;
|
||||
}
|
||||
if (0 == soft_iic_tx_byte(0, reg_data)) {
|
||||
soft_iic_stop(0);
|
||||
return FAIL;
|
||||
}
|
||||
soft_iic_stop(0);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
@ -77,13 +117,15 @@ unsigned char SetReg(unsigned char address, unsigned char reg_data){
|
||||
* @return unsigned char
|
||||
*/
|
||||
unsigned char FM176XX_SoftReset(void){
|
||||
gravity_sensor_command(FM176_WRITE_ADDR,REG_COMMAND,0x1F);
|
||||
return SetReg(REG_COMMAND, 0x1F);
|
||||
}
|
||||
|
||||
#elif INTERFACE_TYPE == 1 //spi
|
||||
|
||||
|
||||
unsigned char FM176XX_HardInit(void){
|
||||
gpio_set_direction(IO_PORTA_05,0); //nss
|
||||
gpio_set_output_value(IO_PORTA_05, 1); //初始设置为高
|
||||
spi_open(SPI1); //初始化spi1:PC3、PC5
|
||||
return SUCCESS;
|
||||
}
|
||||
@ -105,16 +147,18 @@ unsigned char GetReg(unsigned char address, unsigned char *reg_data){
|
||||
addr_byte = (address << 1) | 0x01;
|
||||
|
||||
// ---- 开始SPI事务 ----
|
||||
// gpio_set_output_value(FM176XX_CS_PIN, 0); // 拉低CS
|
||||
gpio_set_output_value(IO_PORTA_05,0);
|
||||
|
||||
// 1. 发送地址字节,忽略接收到的数据
|
||||
spi_send_byte(SPI1, addr_byte);
|
||||
spi_send_byte(SPI1, addr_byte);
|
||||
asm("nop");
|
||||
|
||||
// 2. 接收数据字节 (通过发送一个Dummy Byte 0xFF 来产生时钟)
|
||||
*reg_data = spi_recv_byte(SPI1, &err);
|
||||
asm("nop");
|
||||
|
||||
// ---- 结束SPI事务 ----
|
||||
// gpio_set_output_value(FM176XX_CS_PIN, 1); // 拉高CS
|
||||
gpio_set_output_value(IO_PORTA_05,1);
|
||||
|
||||
if (err != 0) {
|
||||
xlog("GetReg error\n");
|
||||
@ -133,17 +177,24 @@ unsigned char GetReg(unsigned char address, unsigned char *reg_data){
|
||||
* 接口:SPI
|
||||
*/
|
||||
unsigned char SetReg(unsigned char address, unsigned char reg_data){
|
||||
unsigned char spi_data = (address << 1) & 0xFE;
|
||||
|
||||
if(spi_send_byte(SPI1, spi_data) != 0){
|
||||
xlog("SetReg error 0\n");
|
||||
return FAIL;
|
||||
}
|
||||
if(spi_send_byte(SPI1, reg_data) != 0){
|
||||
xlog("SetReg error 1\n");
|
||||
return FAIL;
|
||||
|
||||
unsigned char addr_byte = (address << 1) & 0xFE; // Bit0=0 for write
|
||||
int err1, err2;
|
||||
|
||||
gpio_set_output_value(IO_PORTA_05,0); // <<-- CS拉低,开始事务
|
||||
|
||||
err1 = spi_send_byte(SPI1, addr_byte); // 发送地址
|
||||
asm("nop");
|
||||
err2 = spi_send_byte(SPI1, reg_data); // 发送数据
|
||||
asm("nop");
|
||||
|
||||
gpio_set_output_value(IO_PORTA_05,1); // <<-- CS拉高,结束事务
|
||||
|
||||
if (err1 != 0 || err2 != 0) {
|
||||
return FAIL;
|
||||
}
|
||||
return SUCCESS;
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -164,5 +215,6 @@ unsigned char FM176XX_SoftReset(void){
|
||||
|
||||
|
||||
void rfid_delay_ms(unsigned int ms){
|
||||
// delay(ms);
|
||||
os_time_dly(ms/10);
|
||||
}
|
||||
|
||||
113
apps/earphone/remote_control/nvs.c
Normal file
113
apps/earphone/remote_control/nvs.c
Normal file
@ -0,0 +1,113 @@
|
||||
#include "system/includes.h"
|
||||
#include "system/syscfg_id.h"
|
||||
#include "nvs.h"
|
||||
|
||||
// 2. 定义一个唯一的配置项ID (必须在1-49之间)
|
||||
#define CFG_FACTORY_INFO_ID 10
|
||||
|
||||
/**
|
||||
* @brief 将出厂信息写入Flash
|
||||
*
|
||||
* @param info 指向要写入的出厂信息结构体的指针
|
||||
* @return 实际写入的长度, <0: 失败
|
||||
*/
|
||||
int nvs_write_factory_info(const factory_info_t *info)
|
||||
{
|
||||
if (!info) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("--> Writing factory info to flash...\n");
|
||||
int ret = syscfg_write(CFG_FACTORY_INFO_ID, (void*)info, sizeof(factory_info_t));
|
||||
if (ret != sizeof(factory_info_t)) {
|
||||
printf("!!! syscfg_write factory info failed, ret = %d\n", ret);
|
||||
} else {
|
||||
printf("--> syscfg_write factory info success.\n");
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 从Flash读取出厂信息
|
||||
*
|
||||
* @param info 指向用于存储读取数据的出厂信息结构体的指针
|
||||
* @return 实际读取的长度, <0: 失败 (例如尚未写入过)
|
||||
*/
|
||||
int nvs_read_factory_info(factory_info_t *info)
|
||||
{
|
||||
if (!info) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("--> Reading factory info from flash...\n");
|
||||
int ret = syscfg_read(CFG_FACTORY_INFO_ID, (void*)info, sizeof(factory_info_t));
|
||||
if (ret != sizeof(factory_info_t)) {
|
||||
printf("!!! syscfg_read factory info failed, ret = %d. Maybe not set yet.\n", ret);
|
||||
// 如果读取失败,清空结构体以避免使用脏数据
|
||||
memset(info, 0, sizeof(factory_info_t));
|
||||
} else {
|
||||
printf("--> syscfg_read factory info success.\n");
|
||||
// 可以在这里打印读取到的信息以供调试
|
||||
printf(" Product ID: %s\n", info->product_id);
|
||||
printf(" Serial No: %s\n", info->serial_number);
|
||||
printf(" HW Version: 0x%x\n", info->hw_version);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 清空Flash中的出厂信息
|
||||
*
|
||||
* @return 0: 成功, <0: 失败
|
||||
*/
|
||||
int nvs_clear_factory_info(void)
|
||||
{
|
||||
printf("--> Clearing factory info from flash...\n");
|
||||
// 写入长度为0的数据即可实现删除效果
|
||||
int ret = syscfg_write(CFG_FACTORY_INFO_ID, NULL, 0);
|
||||
if (ret != 0) {
|
||||
printf("!!! syscfg_write clear factory info failed, ret = %d\n", ret);
|
||||
} else {
|
||||
printf("--> syscfg_write clear factory info success.\n");
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
// 可以在这里添加一个测试函数
|
||||
void nvs_test_factory_info(void)
|
||||
{
|
||||
factory_info_t write_info = {
|
||||
.product_id = "RC_V2",
|
||||
.serial_number = "SN202511260002",
|
||||
.hw_version = 0x0102, // v1.1
|
||||
.cal_data = 1234,
|
||||
.manufacture_date = 1764080400, // 2025-11-26
|
||||
};
|
||||
factory_info_t read_info;
|
||||
|
||||
printf("\n\n--- NVS WRITE TEST ---\n");
|
||||
nvs_write_factory_info(&write_info);
|
||||
|
||||
os_time_dly(10);
|
||||
|
||||
printf("\n--- NVS READ TEST ---\n");
|
||||
nvs_read_factory_info(&read_info);
|
||||
|
||||
// ASSERT(memcmp(&write_info, &read_info, sizeof(factory_info_t)) == 0, "NVS Read/Write Check Failed!");
|
||||
|
||||
// printf("\n--- NVS CLEAR TEST ---\n");
|
||||
// nvs_clear_factory_info();
|
||||
|
||||
os_time_dly(10);
|
||||
|
||||
printf("\n--- NVS READ AFTER CLEAR TEST ---\n");
|
||||
int ret = nvs_read_factory_info(&read_info);
|
||||
if(ret < 0){
|
||||
printf("--- nvs read error ---\n");
|
||||
}
|
||||
|
||||
printf("\n\n--- NVS TEST COMPLETE ---\n\n");
|
||||
}
|
||||
44
apps/earphone/remote_control/nvs.h
Normal file
44
apps/earphone/remote_control/nvs.h
Normal file
@ -0,0 +1,44 @@
|
||||
#ifndef __NVS_H__
|
||||
#define __NVS_H__
|
||||
|
||||
#include "typedef.h"
|
||||
|
||||
// 定义出厂信息数据结构
|
||||
typedef struct {
|
||||
char product_id[16]; // 产品ID
|
||||
char serial_number[32]; // 序列号
|
||||
u16 hw_version; // 硬件版本
|
||||
u16 cal_data; // 某个校准数据
|
||||
u32 manufacture_date; // 生产日期 (Unix时间戳)
|
||||
} factory_info_t;
|
||||
|
||||
/**
|
||||
* @brief 将出厂信息写入Flash
|
||||
*
|
||||
* @param info 指向要写入的出厂信息结构体的指针
|
||||
* @return 实际写入的长度, <0: 失败
|
||||
*/
|
||||
int nvs_write_factory_info(const factory_info_t *info);
|
||||
|
||||
/**
|
||||
* @brief 从Flash读取出厂信息
|
||||
*
|
||||
* @param info 指向用于存储读取数据的出厂信息结构体的指针
|
||||
* @return 实际读取的长度, <0: 失败 (例如尚未写入过)
|
||||
*/
|
||||
int nvs_read_factory_info(factory_info_t *info);
|
||||
|
||||
/**
|
||||
* @brief 清空Flash中的出厂信息
|
||||
*
|
||||
* @return 0: 成功, <0: 失败
|
||||
*/
|
||||
int nvs_clear_factory_info(void);
|
||||
|
||||
/**
|
||||
* @brief 用于测试NVS读写功能的函数
|
||||
*
|
||||
*/
|
||||
void nvs_test_factory_info(void);
|
||||
|
||||
#endif // __NVS_H__
|
||||
@ -45,6 +45,10 @@
|
||||
#include "default_event_handler.h"
|
||||
#include "debug.h"
|
||||
#include "system/event.h"
|
||||
#include "../remote_control/nvs.h"
|
||||
#if (JL_EARPHONE_APP_EN)
|
||||
#include "rcsp_adv_bluetooth.h"
|
||||
#endif
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//宏定义
|
||||
#define LOG_TAG_CONST EARPHONE
|
||||
@ -78,7 +82,7 @@ extern u8 init_ok;
|
||||
extern u8 sniff_out;
|
||||
unsigned char xtell_bl_state=0; //存放经典蓝牙的连接状态,0断开,1是连接
|
||||
u8 bt_newname =0;
|
||||
unsigned char xt_ble_new_name[9] = "CM-22222";
|
||||
unsigned char xt_ble_new_name[9] = "CM-55555";
|
||||
static u16 play_poweron_ok_timer_id = 0;
|
||||
|
||||
// -- 初始化标志位 --
|
||||
@ -209,11 +213,22 @@ void le_user_app_event_handler(struct sys_event* event){
|
||||
if (event->u.app.buffer[0] == 0xBE && event->u.app.buffer[1] == 0xBB) {
|
||||
if(event->u.app.buffer[2] == 0x01){ //后面的数据长度 1
|
||||
switch (event->u.app.buffer[3]){
|
||||
case 0xff: //测试
|
||||
extern void i2c_scanner_probe(void);
|
||||
// i2c_scanner_probe();
|
||||
case 0x01:
|
||||
nvs_test_factory_info();
|
||||
break;
|
||||
case 0x02:
|
||||
// factory_info_t read_info;;
|
||||
// nvs_read_factory_info(&read_info);
|
||||
extern void rfid_task_fuc(void);
|
||||
create_process(&rfid_fuc_id,"rfid",NULL,rfid_task_fuc,1000);
|
||||
create_process(&rfid_fuc_id,"rfid",NULL,rfid_task_fuc,2000);
|
||||
break;
|
||||
case 0xff: //测试
|
||||
int ret = hw_iic_init(0);
|
||||
// int ret = soft_iic_init(0);
|
||||
xlog("init iic result:%d\n", ret); //返回0成功
|
||||
extern void i2c_scanner_probe(void);
|
||||
i2c_scanner_probe();
|
||||
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@ -453,6 +468,19 @@ static int event_handler(struct application *app, struct sys_event *event)
|
||||
} else if ((u32)event->arg == DEVICE_EVENT_FROM_POWER) {
|
||||
return app_power_event_handler(&event->u.dev);
|
||||
}
|
||||
#if (JL_EARPHONE_APP_EN)
|
||||
else if ((u32)event->arg == DEVICE_EVENT_FROM_RCSP) {
|
||||
xlog("DEVICE_EVENT_FROM_RCSP: %d", event->u.rcsp.event);
|
||||
switch (event->u.rcsp.event) {
|
||||
case MSG_JL_UPDATE_START:
|
||||
xlog(">>> Xtell APP: MSG_JL_UPDATE_START\n");
|
||||
// You can add UI notifications here, like LED blinking or a tone.
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#if TCFG_UMIDIGI_BOX_ENABLE
|
||||
else if ((u32)event->arg == DEVICE_EVENT_UMIDIGI_CHARGE_STORE) {
|
||||
app_umidigi_chargestore_event_handler(&event->u.umidigi_chargestore);
|
||||
@ -491,8 +519,8 @@ static const struct application_operation app_handler_ops = {
|
||||
* 注册earphone模式
|
||||
*/
|
||||
REGISTER_APPLICATION(app_handler) = {
|
||||
.name = "handler",
|
||||
.action = ACTION_EARPHONE_MAIN,
|
||||
.ops = &app_handler_ops,
|
||||
.name = "handler",
|
||||
.action = ACTION_EARPHONE_MAIN,
|
||||
.ops = &app_handler_ops,
|
||||
.state = APP_STA_DESTROY,
|
||||
};
|
||||
|
||||
@ -253,6 +253,7 @@
|
||||
_MASK_MEM_BEGIN = ABSOLUTE(0x19fc00);
|
||||
_MASK_MEM_SIZE = ABSOLUTE(0x1a4);
|
||||
|
||||
|
||||
EXTERN(
|
||||
_start
|
||||
|
||||
@ -274,25 +275,14 @@ cvsd_decoder
|
||||
|
||||
|
||||
pcm_decoder
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
mp3_decoder
|
||||
wtgv2_decoder
|
||||
|
||||
|
||||
|
||||
aac_decoder
|
||||
cvsd_encoder
|
||||
|
||||
|
||||
|
||||
msbc_encoder
|
||||
audio_dac_driver
|
||||
|
||||
);
|
||||
|
||||
UPDATA_SIZE = 0x80;
|
||||
@ -428,7 +418,8 @@ SECTIONS
|
||||
battery_notify_begin = .;
|
||||
*(.battery_notify)
|
||||
battery_notify_end = .;
|
||||
. = ALIGN(4);
|
||||
|
||||
. = ALIGN(4);
|
||||
__VERSION_BEGIN = .;
|
||||
KEEP(*(.sys.version))
|
||||
__VERSION_END = .;
|
||||
@ -518,6 +509,7 @@ SECTIONS
|
||||
*(.audio_track_data)
|
||||
*(.audio_adc_data)
|
||||
|
||||
|
||||
. = ALIGN(4);
|
||||
*(.data*)
|
||||
|
||||
@ -726,6 +718,7 @@ SECTIONS
|
||||
|
||||
} > ram0
|
||||
|
||||
|
||||
data_code_pc_limit_end = .;
|
||||
__report_overlay_end = .;
|
||||
|
||||
@ -815,6 +808,7 @@ SECTIONS
|
||||
}
|
||||
|
||||
|
||||
|
||||
SECTIONS
|
||||
{
|
||||
.data : ALIGN(4)
|
||||
@ -846,6 +840,7 @@ SECTIONS
|
||||
|
||||
UPDATE_CODE_TOTAL_SIZE = update_code_end - update_code_start;
|
||||
}
|
||||
|
||||
SECTIONS
|
||||
{
|
||||
.data : ALIGN(4)
|
||||
@ -948,6 +943,7 @@ BTSTACK_LE_HOST_MESH_RAM_TOTAL = BTSTACK_LE_HOST_MESH_DATA_SIZE + BTSTACK_LE_HOS
|
||||
BTSTACK_LE_HOST_MESH_FLASH_TOTAL = BTSTACK_LE_HOST_MESH_CODE_SIZE;
|
||||
|
||||
BTSTACK_CODE_SIZE = (btstack_code_end - btstack_code_start) + (btstack_data_end - btstack_data_start);
|
||||
|
||||
SECTIONS
|
||||
{
|
||||
.data : ALIGN(4)
|
||||
@ -1241,6 +1237,7 @@ SECTIONS
|
||||
*(.os_code)
|
||||
} > ram0
|
||||
}
|
||||
|
||||
SECTIONS
|
||||
{
|
||||
.data : ALIGN(4)
|
||||
@ -1450,6 +1447,7 @@ SECTIONS
|
||||
BTCTLER_RAM_TOTAL = (btctler_data_end - btctler_data_start) + (btctler_bss_end - btctler_bss_start);
|
||||
BTCTLER_CODE_TOTAL = (btctler_code_end - btctler_code_start);
|
||||
}
|
||||
|
||||
SECTIONS
|
||||
{
|
||||
.data : ALIGN(4)
|
||||
@ -1567,7 +1565,8 @@ SECTIONS
|
||||
*(.timer.text.cache.L1)
|
||||
*(.gpio.text.cache.L1)
|
||||
*(.iic_hw.text.cache.L1)
|
||||
driver_data_code_end = .;
|
||||
|
||||
driver_data_code_end = .;
|
||||
. = ALIGN(4);
|
||||
} > ram0
|
||||
|
||||
@ -1577,6 +1576,7 @@ SECTIONS
|
||||
DRIVER_DATA_CODE_TOTAL = (driver_data_code_end - driver_data_code_start);
|
||||
|
||||
}
|
||||
|
||||
SECTIONS
|
||||
{
|
||||
.data : ALIGN(4)
|
||||
@ -2016,6 +2016,7 @@ SECTIONS
|
||||
} > ram0
|
||||
}
|
||||
|
||||
|
||||
text_begin = ADDR(.text);
|
||||
text_size = SIZEOF(.text);
|
||||
text_end = text_begin + text_size;
|
||||
|
||||
@ -33,19 +33,7 @@ cvsd_decoder
|
||||
|
||||
|
||||
pcm_decoder
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
mp3_decoder
|
||||
wtgv2_decoder
|
||||
|
||||
|
||||
|
||||
aac_decoder
|
||||
cvsd_encoder
|
||||
|
||||
|
||||
|
||||
@ -416,11 +416,10 @@ int spi_open(spi_dev spi)
|
||||
} else if (role == SPI_ROLE_SLAVE) {
|
||||
spi_role_slave(spi_regs[id]);
|
||||
}
|
||||
spi_smp_edge_rise(spi_regs[id]); // 采样边沿:上升沿 (Sample edge: Rise) --lmx
|
||||
// spi_ud_edge_fall(spi_regs[id]); // 更新边沿:下降沿 (Update edge: Fall)
|
||||
spi_ud_edge_rise(spi_regs[id]); // 更新边沿:上升沿 (Update edge: Fall)
|
||||
spi_clk_idle_l(spi_regs[id]); // 时钟空闲:低电平 (Clock idle: Low)
|
||||
spi_smp_edge_rise(spi_regs[id]);
|
||||
spi_ud_edge_fall(spi_regs[id]);
|
||||
spi_cs_idle_h(spi_regs[id]);
|
||||
spi_clk_idle_l(spi_regs[id]);
|
||||
spi_clr_pnd(spi_regs[id]);
|
||||
if ((err = spi_set_baud(spi, clock))) {
|
||||
log_error("invalid spi baudrate");
|
||||
@ -433,15 +432,15 @@ int spi_open(spi_dev spi)
|
||||
}
|
||||
spi_enable(spi_regs[id]);
|
||||
|
||||
#if 1
|
||||
#if 0
|
||||
printf("spi%d clk = %d\n", id, clock);
|
||||
printf("spi%d mode = %d\n", id, mode);
|
||||
printf("spi%d role = %d\n", id, role);
|
||||
printf("spi%d clk_pin = %d\n", id, spi1_p_data.port[0]);
|
||||
printf("spi%d do_pin = %d\n", id, spi1_p_data.port[1]);
|
||||
printf("spi%d di_pin = %d\n", id, spi1_p_data.port[2]);
|
||||
printf("spi%d clk_pin = %d\n", id, port[0]);
|
||||
printf("spi%d do_pin = %d\n", id, port[1]);
|
||||
printf("spi%d di_pin = %d\n", id, port[2]);
|
||||
printf("spi%d CON = %04x\n", id, spi_r_reg_con(spi_regs[id]));
|
||||
// printf("spi%d IOMC1 = %08x\n", id, JL_IOMAP->CON1);
|
||||
printf("spi%d IOMC1 = %08x\n", id, JL_IOMAP->CON1);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -14,13 +14,12 @@
|
||||
|
||||
|
||||
[EXTRA_CFG_PARAM]
|
||||
NEW_FLASH_FS = YES;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
BR22_TWS_DB = YES;
|
||||
FLASH_SIZE = 0x100000;
|
||||
BR22_TWS_VERSION = 0;
|
||||
FORCE_4K_ALIGN = YES;
|
||||
SPECIAL_OPT = 0;
|
||||
CHIP_NAME = AC701N;
|
||||
ENTRY = 0x6000100;
|
||||
PID = AC701N;
|
||||
|
||||
36858
cpu/br28/tools/rom.lst
36858
cpu/br28/tools/rom.lst
File diff suppressed because it is too large
Load Diff
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
191172
cpu/br28/tools/sdk.lst
191172
cpu/br28/tools/sdk.lst
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user