From 92c6edf0bdcc556e5fb67c7fabf5c2515ad16785 Mon Sep 17 00:00:00 2001 From: "mr.zhangsan" Date: Mon, 29 Apr 2024 20:14:59 +0800 Subject: [PATCH] =?UTF-8?q?rfid=E8=A7=A3=E6=9E=90=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/.vscode/settings.json | 3 +- source/app/app_config.h | 3 ++ source/app/main.c | 7 ++- source/app/main.h | 2 + source/app/task_nfc.c | 8 +-- source/app/task_rfid.c | 99 ++++++++++++++++++++++++++++++++++++ source/app/task_rfid.h | 33 ++++++++++++ source/bsp/bsp_config.c | 1 + source/msp/UART3.c | 22 ++++++-- source/msp/uart_x.c | 5 +- source/msp/uart_x.h | 1 + source/tpc/debug.c | 19 +++---- source/tpc/debug.h | 14 +++++ 13 files changed, 196 insertions(+), 21 deletions(-) create mode 100644 source/app/task_rfid.c create mode 100644 source/app/task_rfid.h diff --git a/source/.vscode/settings.json b/source/.vscode/settings.json index 1eecaab..eabf88c 100644 --- a/source/.vscode/settings.json +++ b/source/.vscode/settings.json @@ -17,6 +17,7 @@ "app_config.h": "c", "task_nfc.h": "c", "task_debug.h": "c", - "clib.h": "c" + "clib.h": "c", + "uart2.h": "c" } } \ No newline at end of file diff --git a/source/app/app_config.h b/source/app/app_config.h index 897ee39..aa48b1e 100644 --- a/source/app/app_config.h +++ b/source/app/app_config.h @@ -25,6 +25,9 @@ #define D_ADC_CHANNEL_NUM 4 typedef struct global_register { + //低功耗模式 + U8 low_power_mode; + //RO Register U16 reserved1; U32 total_weight; //净重(显示重量)*100 diff --git a/source/app/main.c b/source/app/main.c index 75d8b82..a76ed86 100644 --- a/source/app/main.c +++ b/source/app/main.c @@ -90,6 +90,7 @@ void L0_main_init(void) //串口初始化 L0_uart0_buf_init(); + L0_uart2_buf_init(); L0_uart3_buf_init(); //打开系统总中断 @@ -106,6 +107,7 @@ void L0_main_init(void) //任务初始化 L3_task_nfc_init(); + L3_task_rfid_init(); // L3_task_adc_init(0); // L3_task_adc_init(1); @@ -150,7 +152,7 @@ void main(void) if(1 == s_nos_tick.t1s_heatbeart) { s_nos_tick.t1s_heatbeart = 0; - LED0 ^= 1; + // LED0 ^= 1; // KR1 ^= 1; // KC1 ^= 1; L0_uart0_uc('.'); @@ -169,6 +171,9 @@ void main(void) //NFC协议处理 L3_task_nfc_handler(&_s_task_nfc); + //RFID协议处理 + L3_task_rfid_handler(&_s_task_rfid); + //串口调试 // if(ts_uart3_recv_buf.ok == 1){ // // count++; diff --git a/source/app/main.h b/source/app/main.h index 687fdee..9f44870 100644 --- a/source/app/main.h +++ b/source/app/main.h @@ -38,6 +38,7 @@ #define _MAIN_H #include "../msp/uart0.h" +#include "../msp/uart2.h" #include "../msp/uart3.h" #include "../msp/eeprom.h" @@ -56,6 +57,7 @@ #include "../app/task_register.h" #include "../app/task_encrypt.h" #include "../app/task_nfc.h" +#include "../app/task_rfid.h" ////////////////////////////////////////////////////////////////// diff --git a/source/app/task_nfc.c b/source/app/task_nfc.c index b0ccee1..69e2a61 100644 --- a/source/app/task_nfc.c +++ b/source/app/task_nfc.c @@ -14,7 +14,7 @@ #include "../app/app_config.h" #include "../bsp/bsp_config.h" #include "../msp/uart0.h" -#include "../msp/uart3.h" +#include "../msp/uart2.h" S_TASK_NFC _s_task_nfc; TPC_NFC tpc_nfc; @@ -35,10 +35,10 @@ void L3_task_nfc_handler(S_TASK_NFC *s) L2_task_go(D_task_NFC_READ); TTSS_Task_step(D_task_NFC_READ) - if(ts_uart3_recv_buf.ok == 1) + if(ts_uart2_recv_buf.ok == 1) { - ts_uart3_recv_buf.ok = 0; - Lc_buf_copy_uc((U8*)&tpc_nfc, ts_uart3_recv_buf.buf, ts_uart3_recv_buf.num); + ts_uart2_recv_buf.ok = 0; + Lc_buf_copy_uc((U8*)&tpc_nfc, ts_uart2_recv_buf.buf, ts_uart2_recv_buf.num); tpc_nfc.ocr = tpc_nfc.buf[tpc_nfc.num[0] << 8 | tpc_nfc.num[1]]; L2_task_go_Tdelay(D_task_NFC_PRINT,0); } diff --git a/source/app/task_rfid.c b/source/app/task_rfid.c new file mode 100644 index 0000000..806302d --- /dev/null +++ b/source/app/task_rfid.c @@ -0,0 +1,99 @@ +//////////////////////////////////////////////////////////////////////////// +///@copyright Copyright (c) 2018, 传控科技 All rights reserved. +///------------------------------------------------------------------------- +/// @file bsp_drv.c +/// @brief bsp @ driver config +///------------------------------------------------------------------------- +/// @version 1.0 +/// @author CC +/// @date 20180331 +/// @note cc_AS_stc02 +////////////////////////////////////////////////////////////////////////////// + +#include "task_rfid.h" +#include "../app/app_config.h" +#include "../bsp/bsp_config.h" +#include "../msp/uart0.h" +#include "../msp/uart3.h" + +S_TASK_RFID _s_task_rfid; +TPC_RFID tpc_rfid; + +//4通道,读取次数 3 3 3 3 +U8 rfidSetChannelCmd[] = {0xBB,0x00,0x1B,0x00,0x05,0x02,0x03,0x03,0x03,0x03,0x2E,0x7E}; +//设置功率 26dBm +U8 rfidSetWCmd[] = {0xBB,0x00,0xB6,0x00,0x02,0x0A,0x28,0xEA,0x7E}; +//选择卡片 +U8 rfidSelCmd[] = {0x00}; +//轮询读取(FFFF代表永久轮询) +U8 rfidLoopRead[] = {0xBB,0x00,0x27,0x00,0x03,0x22,0xFF,0xFF,0x4A,0x7E}; + +//============================================= +void L3_task_rfid_init(void) +{ + L1_task_init(&_s_task_rfid.task); + L3_task_s_go(_s_task_rfid,D_task_init); +} + +#define D_task_RFID_READ 0x50 +#define D_task_RFID_PRINT 0x51 +#define D_task_RFID_MODE 0x52 + +void L3_task_rfid_handler(S_TASK_RFID *s) +{ + TTSS_Task_init() + L0_uart0_uchex(sizeof(rfidSetChannelCmd) / sizeof(U8)); + L0_uart0_uchex(sizeof(rfidSetWCmd) / sizeof(U8)); + L0_uart0_uchex(sizeof(rfidSelCmd) / sizeof(U8)); + L0_uart0_uchex(sizeof(rfidLoopRead) / sizeof(U8)); + + //1.设置天线 + L0_uart3_sendArray(rfidSetChannelCmd, sizeof(rfidSetChannelCmd) / sizeof(U8)); + NOP10(); + //2.设置功率 + L0_uart3_sendArray(rfidSetWCmd, sizeof(rfidSetWCmd) / sizeof(U8)); + NOP10(); + //3.选择掩码 + L0_uart3_sendArray(rfidSelCmd, sizeof(rfidSelCmd) / sizeof(U8)); + NOP10(); + //4.启动轮询 + L0_uart3_sendArray(rfidLoopRead, sizeof(rfidLoopRead) / sizeof(U8)); + NOP10(); + + L2_task_go(D_task_RFID_READ); + + TTSS_Task_step(D_task_RFID_READ) + if(ts_uart3_recv_buf.ok == 1) + { + ts_uart3_recv_buf.ok = 0; + Lc_buf_copy_uc((U8*)&tpc_rfid, ts_uart3_recv_buf.buf, ts_uart3_recv_buf.num); + tpc_rfid.ocr = tpc_rfid.buf[tpc_rfid.num[0] << 8 | tpc_rfid.num[1]]; + L2_task_go_Tdelay(D_task_RFID_PRINT,0); + } + + TTSS_Task_step(D_task_RFID_PRINT) + L0_uart0_sendstr("\r\n--------- Recv RFID --------\r\n"); + L0_uart0_sendstr("type : "); + L0_uart0_uchex(tpc_rfid.type); + L0_uart0_0d0a(); + L0_uart0_sendstr("cmd: "); + L0_uart0_uchex(tpc_rfid.cmd); + L0_uart0_0d0a(); + L0_uart0_sendstr("num: "); + L0_uart0_uchex(tpc_rfid.num[0]); + L0_uart0_uchex(tpc_rfid.num[1]); + L0_uart0_0d0a(); + L0_uart0_sendstr("dat : "); + L0_uart0_sendArrayHex(tpc_rfid.buf, tpc_rfid.num[0] << 8 | tpc_rfid.num[1]); + L0_uart0_0d0a(); + L0_uart0_sendstr("ocr: "); + L0_uart0_uchex(tpc_rfid.ocr); + L0_uart0_0d0a(); + L2_task_go_Tdelay(D_task_RFID_READ,0); + + TTSS_Task_end(); +} + + + + diff --git a/source/app/task_rfid.h b/source/app/task_rfid.h new file mode 100644 index 0000000..807bc42 --- /dev/null +++ b/source/app/task_rfid.h @@ -0,0 +1,33 @@ +//////////////////////////////////////////////////////////////////////////// +///@copyright Copyright (c) 2018, 传控科技 All rights reserved. +///------------------------------------------------------------------------- +/// @file bsp_drv.h +/// @brief bsp @ driver config +///------------------------------------------------------------------------- +/// @version 1.0 +/// @author CC +/// @date 20180331 +/// @note cc_AS_stc02 + +////////////////////////////////////////////////////////////////////////////// + +#ifndef _APP_TASK_RFID_H +#define _APP_TASK_RFID_H + +#include "../ctask/task.h" +#include "../clib/clib.h" +#include "../tpc/debug.h" + +typedef struct _s_task_rfid +{ + TS_task task; +}S_TASK_RFID; + +extern S_TASK_RFID _s_task_rfid; + +extern void L3_task_rfid_init(void); +extern void L3_task_rfid_handler(S_TASK_RFID *s); + + +#endif // #ifndef _APP_TASK_RFID_H + diff --git a/source/bsp/bsp_config.c b/source/bsp/bsp_config.c index 80e4a65..523f88b 100644 --- a/source/bsp/bsp_config.c +++ b/source/bsp/bsp_config.c @@ -30,6 +30,7 @@ void L0_board_config(void) D_stdIO_P2_ALL(); D_stdIO_P3_ALL(); D_stdIO_P4_ALL(); + D_stdIO_P5_ALL(); //IO BLED0 = BLED1 = BLED2 = BLED3 = BLED4 = BLED5 = BLED6 = BLED0_OFF; BLED7 = BLED8 = BLED9 = BLED10 = BLED11 = BLED12 = BLED13 = BLED0_OFF; diff --git a/source/msp/UART3.c b/source/msp/UART3.c index e8794a8..3872d8e 100644 --- a/source/msp/UART3.c +++ b/source/msp/UART3.c @@ -60,7 +60,7 @@ msp/uartx.c 底层代码 和cpu相关 缓存发送也放在里面 //发送缓冲区 static volatile Ts_uart_send_buf idata ts_uart_send_buf; //接收缓冲区最多存放16个字符,这个值尽量小,但要大于实际处理协议的缓冲区大小 -#define RECV_BUF_SIZE 16 +#define RECV_BUF_SIZE 32 static U8 uart3_recv_buf[RECV_BUF_SIZE]; Ts_uart_recv_buf ts_uart3_recv_buf; int g_uart3_recv_num = 0; @@ -75,13 +75,22 @@ void L0_uart3_init(void) T3H = 0xFF; //设定定时初值 T4T3M |= 0x08; //启动定时器3 -#elif (MainFre_22M == D_sys_MainFre) //9600bps@22.1184MHz +// #elif (MainFre_22M == D_sys_MainFre) //9600bps@22.1184MHz +// S3CON = 0x10; //8位数据,可变波特率 +// S3CON |= 0x40; //串口3选择定时器3为波特率发生器 +// T4T3M &= 0xFD; //定时器时钟12T模式 +// T3L = 0xD0; //设置定时初始值 +// T3H = 0xFF; //设置定时初始值 +// T4T3M |= 0x08; //定时器3开始计时 + +#elif (MainFre_22M == D_sys_MainFre) //115200bps@22.1184MHz S3CON = 0x10; //8位数据,可变波特率 S3CON |= 0x40; //串口3选择定时器3为波特率发生器 T4T3M &= 0xFD; //定时器时钟12T模式 - T3L = 0xD0; //设置定时初始值 + T3L = 0xFC; //设置定时初始值 T3H = 0xFF; //设置定时初始值 T4T3M |= 0x08; //定时器3开始计时 + #endif } @@ -100,9 +109,10 @@ void L0_uart3_buf_init(void) ts_uart[uNum3].t->ok = 0; //串口协议解析专用字段 - ts_uart[uNum3].t->head_0 = 0x60; + ts_uart[uNum3].t->head_0 = 0xBB; + ts_uart[uNum3].t->tail_0 = 0x7E; ts_uart[uNum3].t->maxnum = RECV_BUF_SIZE; - ts_uart[uNum3].tp_handler = L1_s2b_nfc; + ts_uart[uNum3].tp_handler = L1_s2b_rfid; //串口初始化 L0_uart3_init(); @@ -132,6 +142,7 @@ void INTERRUPT_UART3(void) D_SERVE_UART3// using 3 NOP(); NOP(); NOP(); if(L0_uart3_IntRI()) //如果是U3接收中断 { + // LED0 ^= 1; L0_uart3_IntRIClear(); //清除接收中断标志 ts_uart[uNum3].t->reg = L0_uartN_get(uNum3); g_uart3_recv_num++; @@ -141,6 +152,7 @@ void INTERRUPT_UART3(void) D_SERVE_UART3// using 3 } if(L0_uart3_IntTI()) //如果是U0发送中断 { + // LED0 ^= 1; L0_uart3_IntTIClear(); //清除发送中断标志 if(ts_uart[uNum3].p->max != ts_uart[uNum3].p->now) { diff --git a/source/msp/uart_x.c b/source/msp/uart_x.c index 1675083..330a235 100644 --- a/source/msp/uart_x.c +++ b/source/msp/uart_x.c @@ -89,7 +89,7 @@ void L0_waitFree_uartN(U8 uartx) ts_uart[uartx].p->over = 0; while(ts_uart[uartx].p->ok != D_ready) { - #if 0 //发送数据特别快时,某些情况下会导致数据发送出错 + #if 1 //发送数据特别快时,某些情况下会导致数据发送出错 if(ts_uart[uartx].p->over ++ > 600000) { break; @@ -101,6 +101,9 @@ void L0_waitFree_uartN(U8 uartx) void L0_uartN_sendArray(U8 uartx,void *buf,U16 len) { L0_waitFree_uartN(uartx); + if(len == 0){ + return; + } ts_uart[uartx].p->ok = D_clear; ts_uart[uartx].p->over = 0; ts_uart[uartx].p->max = len; diff --git a/source/msp/uart_x.h b/source/msp/uart_x.h index 5cade81..16abc3f 100644 --- a/source/msp/uart_x.h +++ b/source/msp/uart_x.h @@ -82,6 +82,7 @@ typedef struct _ts_uart_recv_buf_ vU8 cashe[2]; vU8 head_0; vU8 head_1; + vU8 tail_0; vU8 index; //当前下标 vU8 num; //协议实际长度 vU8 *buf; ////协议缓冲,由每个串口根据需要的缓冲区大小自己定义 diff --git a/source/tpc/debug.c b/source/tpc/debug.c index 37b0efc..d37ba08 100644 --- a/source/tpc/debug.c +++ b/source/tpc/debug.c @@ -114,7 +114,7 @@ void L1_s2b_nfc (Ts_uart_recv_buf *p) //reentrant //帧头:BB //Type:00命令帧 /01响应帧 /02通知帧 //Command: 07指令桢 /22单次读写 /27多次轮询 /28停止轮询 /0C选择 / B6设置功率 -//数据长度:00 07 +//数据长度:00 11 //数据:D5 30 00 E2 00 10 71 00 00 52 9B 09 40 B4 02 EB 98 //校验:0C 从Type到数据字段 [02 22 00 11 D5 30 00 E2 00 10 71 00 00 52 9B 09 40 B4 02 EB 98] 的所有字节的累加和 //帧尾:7E @@ -128,16 +128,17 @@ void L1_s2b_rfid (Ts_uart_recv_buf *p) //reentrant p->buf[0] = p->reg; p->index = 0; p->num = p->maxnum; - p->ocr = p->reg; + //header字节不算累加和 + p->ocr = 0; } } else { p->buf[++p->index] = p->reg; - if(p->index == 3) + if(p->index == 4) { - //5个字节协议头 + 1校验 + 数据(长度由2、3字节计算) - p->num = 5 + 1 + ((p->buf[2] << 8) | (p->buf[3])); + //5个字节协议头 + 1校验 + 1尾字节 + 数据(长度由2、3字节计算) + p->num = 5 + 1 + 1 + ((p->buf[3] << 8) | (p->buf[4])); if(p->num > p->maxnum){ //error p->head = 0; @@ -145,14 +146,14 @@ void L1_s2b_rfid (Ts_uart_recv_buf *p) //reentrant return; } } - if(p->index < p->num - 1) + if(p->index < p->num - 2) { //计算OCR - p->ocr ^= p->reg; + p->ocr += p->reg; } - else + else if(p->index == p->num - 1) { - if(p->ocr == p->buf[p->num-1]) + if((p->buf[p->num - 1] == p->tail_0) && (p->ocr == p->buf[p->num-2])) { if (p->ok != 1) { diff --git a/source/tpc/debug.h b/source/tpc/debug.h index 81ebab5..309ece1 100644 --- a/source/tpc/debug.h +++ b/source/tpc/debug.h @@ -49,11 +49,25 @@ typedef struct _tpc_nfc_ U8 ocr; }TPC_NFC; +#define TPC_RFID_DAT_MAX 32 +typedef struct _tpc_rfid_ +{ + U8 head; + U8 type; + U8 cmd; + U8 num[2]; + U8 buf[TPC_RFID_DAT_MAX]; + U8 ocr; + U8 tail; +}TPC_RFID; + + #define D_HETU_FX_buf_max 5 //定长协议 长度为5 #define D_HETU_FX_fi 0x60 extern void L1_s2b_debug(Ts_uart_recv_buf *p); extern void L1_s2b_nfc (Ts_uart_recv_buf *p); +extern void L1_s2b_rfid (Ts_uart_recv_buf *p); extern void L1_s2b_PH1 (Ts_uart_recv_buf *p);// reentrant; #endif /* end __TPC_DEBUG_H_ */