Browse Source

nfc协议解析完毕

bfysop-v2
mr.zhangsan 1 year ago
parent
commit
edc08e9c1d
  1. 9
      source/.vscode/settings.json
  2. 35
      source/app/main.c
  3. 2
      source/app/main.h
  4. 71
      source/app/task_nfc.c
  5. 33
      source/app/task_nfc.h
  6. 2
      source/bsp/bsp_config.c
  7. 35
      source/cpu/stc_stc8h.h
  8. 7
      source/msp/UART0.C
  9. 4
      source/msp/UART0.h
  10. 40
      source/msp/UART3.c
  11. 5
      source/msp/UART3.h
  12. 5
      source/msp/uart_x.c
  13. 11
      source/msp/uart_x.h
  14. 50
      source/tpc/debug.c
  15. 14
      source/tpc/debug.h
  16. 4
      source/tpc/modbus.c

9
source/.vscode/settings.json

@ -10,6 +10,13 @@
"stc_macro.H": "cpp",
"uart_x.h": "c",
"UART4.C": "cpp",
"tpc_x.h": "c"
"tpc_x.h": "c",
"uart3.h": "c",
"time.h": "c",
"debug.h": "c",
"app_config.h": "c",
"task_nfc.h": "c",
"task_debug.h": "c",
"clib.h": "c"
}
}

35
source/app/main.c

@ -83,10 +83,18 @@ void L0_main_init(void)
{
//Lc_delay_ms(200);
L0_board_config();
//外设初始化
bsp_key_init();
L0_uart0_buf_init();//串口初始化
//串口初始化
L0_uart0_buf_init();
L0_uart3_buf_init();
//打开系统总中断
EA = 1;
//Lc_delay_ms(100);
//系统定时器初始化
L1_tick_init();
L0_timer0_Init();
@ -95,6 +103,9 @@ void L0_main_init(void)
// L2_485_init();
// L0_ADS1213_INIT();
//任务初始化
L3_task_nfc_init();
// L3_task_adc_init(0);
// L3_task_adc_init(1);
// L3_task_adc_init(2);
@ -110,6 +121,8 @@ void L0_main_init(void)
//===============================================
void main(void)
{
U8 key = 0;
U8 count = 0;
#if 0
L0_board_config();
while(1)
@ -120,7 +133,6 @@ void main(void)
delayInMs(500);
}
#endif
U8 key = 0;
//初始化
L0_main_init();
//验证加密信息
@ -133,10 +145,12 @@ 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('.');
L0_uart0_uc('.');
// L0_uart0_uchex(g_uart3_recv_num);
// L0_uart0_uchex(count);
// L0_uart0_sendArray("v2.7",4);
//L0_uartN_sendstr(0,"v2.8111");
}
@ -148,11 +162,12 @@ void main(void)
}
//串口调试
if(ts_uart0_recv_buf.ok == 1){
NOP(10);
ts_uart0_recv_buf.ok = 0;
L0_uart0_uc(ts_uart0_recv_buf.reg);
}
// if(ts_uart3_recv_buf.ok == 1){
// // count++;
// ts_uart3_recv_buf.ok = 0;
// L0_uart0_uchex(ts_uart3_recv_buf.reg);
// }
L3_task_nfc_handler(&_s_task_nfc);
//L1_ADS1213_main();

2
source/app/main.h

@ -38,6 +38,7 @@
#define _MAIN_H
#include "../msp/uart0.h"
#include "../msp/uart3.h"
#include "../msp/eeprom.h"
#include "../bsp/bsp_config.h"
@ -53,6 +54,7 @@
#include "../app/task_debug.h"
#include "../app/task_register.h"
#include "../app/task_encrypt.h"
#include "../app/task_nfc.h"
//////////////////////////////////////////////////////////////////

71
source/app/task_nfc.c

@ -0,0 +1,71 @@
////////////////////////////////////////////////////////////////////////////
///@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_nfc.h"
#include "../app/app_config.h"
#include "../bsp/bsp_config.h"
#include "../msp/uart0.h"
#include "../msp/uart3.h"
S_TASK_NFC _s_task_nfc;
TPC_NFC tpc_nfc;
//=============================================
void L3_task_nfc_init(void)
{
L1_task_init(&_s_task_nfc.task);
L3_task_s_go(_s_task_nfc,D_task_init);
}
#define D_task_NFC_READ 0x50
#define D_task_NFC_PRINT 0x51
void L3_task_nfc_handler(S_TASK_NFC *s)
{
TTSS_Task_init()
L2_task_go(D_task_NFC_READ);
TTSS_Task_step(D_task_NFC_READ)
if(ts_uart3_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);
tpc_nfc.ocr = tpc_nfc.buf[tpc_nfc.num[0] << 8 | tpc_nfc.num[1]];
L2_task_go_Tdelay(D_task_NFC_PRINT,0);
}
TTSS_Task_step(D_task_NFC_PRINT)
L0_uart0_sendstr("\r\n--------- Recv NFC --------\r\n");
L0_uart0_sendstr("id : ");
L0_uart0_uchex(tpc_nfc.slaveId);
L0_uart0_0d0a();
L0_uart0_sendstr("num: ");
L0_uart0_uchex(tpc_nfc.num[0]);
L0_uart0_uchex(tpc_nfc.num[1]);
L0_uart0_0d0a();
L0_uart0_sendstr("cmd: ");
L0_uart0_uchex(tpc_nfc.cmd);
L0_uart0_0d0a();
L0_uart0_sendstr("no : ");
L0_uart0_sendArrayHex(tpc_nfc.buf, tpc_nfc.num[0] << 8 | tpc_nfc.num[1]);
L0_uart0_0d0a();
L0_uart0_sendstr("ocr: ");
L0_uart0_uchex(tpc_nfc.ocr);
L0_uart0_0d0a();
L2_task_go_Tdelay(D_task_NFC_READ,0);
TTSS_Task_end();
}

33
source/app/task_nfc.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_NFC_H
#define _APP_TASK_NFC_H
#include "../ctask/task.h"
#include "../clib/clib.h"
#include "../tpc/debug.h"
typedef struct _s_task_nfc
{
TS_task task;
}S_TASK_NFC;
extern S_TASK_NFC _s_task_nfc;
extern void L3_task_nfc_init(void);
extern void L3_task_nfc_handler(S_TASK_NFC *s);
#endif // #ifndef _APP_TASK_NFC_H

2
source/bsp/bsp_config.c

@ -62,7 +62,7 @@ void L0_board_config(void)
BITN_0(P_SW2 ,BITN0);// rxd2 p1.0 txd2 p1.1
#endif
//uart4
//uart3
#if (PIN_UART3_Rxd_00_Txd_01 == D_PIN_UART3)
BITN_0(P_SW2 ,BITN1);// rxd2 p1.0 txd2 p1.1
#elif (PIN_UART3_Rxd_50_Txd_51 == D_PIN_UART3)

35
source/cpu/stc_stc8h.h

@ -1089,7 +1089,42 @@ sfr RSTCFG = 0xff;
#define NOP_N(N) NOP##N()
/////////////////////////////////////////////////
#define ET4 0x40
#define ET3 0x20
#define ES4 0x10
#define ES3 0x08
#define ET2 0x04
#define ESPI 0x02
#define ES2 0x01
#define S2SM0 BITN7
#define S2ST2 BITN6
#define S2SM2 BITN5
#define S2REN BITN4
#define S2TB8 BITN3
#define S2RB8 BITN2
#define S2TI BITN1
#define S2RI BITN0
#define S3SM0 BITN7
#define S3ST3 BITN6
#define S3SM2 BITN5
#define S3REN BITN4
#define S3TB8 BITN3
#define S3RB8 BITN2
#define S3TI BITN1
#define S3RI BITN0
#define S4SM0 BITN7
#define S4ST4 BITN6
#define S4SM2 BITN5
#define S4REN BITN4
#define S4TB8 BITN3
#define S4RB8 BITN2
#define S4TI BITN1
#define S4RI BITN0
#endif

7
source/msp/UART0.C

@ -55,10 +55,9 @@ msp/uartx.c 底层代码 和cpu相关 缓存发送也放在里面
-----------------------------------------------------------------------------------------
********************************************************************************/
#include "uart0.h"
#include "../msp/time.h"
#include "../tpc/debug.h"
int g_uart0_recv_num = 0;
int g_uart0_send_num = 0;
//发送缓冲区
static volatile Ts_uart_send_buf idata ts_uart_send_buf;
@ -93,7 +92,8 @@ void L0_uart0_buf_init(void)
ts_uart[uNum0].t->ok = 0;
//定义接收处理协议
ts_uart[uNum0].tp_handler = L1_s2b_debug; //使用函数指针导致错误
// ts_uart[uNum0].tp_handler = L1_s2b_debug; //使用函数指针导致错误
ts_uart[uNum0].tp_handler = NULL; //使用函数指针导致错误
//串口定时器及中断初始化
L0_uart0_init();
@ -123,7 +123,6 @@ void INTERRUPT_UART(void) D_SERVE_UART
L0_uart0_IntRIClear(); //清除接收中断标志
ts_uart[uNum0].t->reg = L0_uartN_get(uNum0);
if(ts_uart[uNum0].tp_handler != NULL){
g_uart0_recv_num++;
ts_uart[uNum0].tp_handler(ts_uart[uNum0].t);
}
}

4
source/msp/UART0.h

@ -79,10 +79,10 @@ void L0_uart0_sendArray(U8 * buf, U16 len);
#define L0_uart0_ushex(X) L0_uartN_ushex(uNum0,X)
#define L0_uart0_ulhex(X) L0_uartN_ulhex(uNum0,X)
#define L0_uart0_sendstr(buf) L0_uartN_sendstr(uNum0,buf)
#define L1_uart0_uchexArray(buf) L1_uartN_uchexArray(uNum0,buf)
#define L0_uart0_sendArrayHex(buf, n) L0_uartN_sendArrayHex(uNum0,buf,n)
extern Ts_uart_recv_buf ts_uart0_recv_buf;
extern int g_uart0_recv_num;
extern int g_uart0_send_num;
#endif //#ifndef _uart0_H

40
source/msp/UART3.c

@ -55,14 +55,15 @@ msp/uartx.c 底层代码 和cpu相关 缓存发送也放在里面
-----------------------------------------------------------------------------------------
********************************************************************************/
#include "uart3.h"
#include "time.h"
#include <stdio.h>
#include "../tpc/debug.h"
//发送缓冲区
static volatile Ts_uart_send_buf idata ts_uart_send_buf;
//接收缓冲区最多存放16个字符,这个值尽量小,但要大于实际处理协议的缓冲区大小
static U8 uart3_recv_buf[16];
#define RECV_BUF_SIZE 16
static U8 uart3_recv_buf[RECV_BUF_SIZE];
Ts_uart_recv_buf ts_uart3_recv_buf;
int g_uart3_recv_num = 0;
void L0_uart3_init(void)
{
@ -74,29 +75,40 @@ void L0_uart3_init(void)
T3H = 0xFF; //设定定时初值
T4T3M |= 0x08; //启动定时器3
#elif (MainFre_22M == D_sys_MainFre) //115200bps@22.1184MHz
#elif (MainFre_22M == D_sys_MainFre) //9600bps@22.1184MHz
S3CON = 0x10; //8位数据,可变波特率
S3CON |= 0x40; //串口3选择定时器3为波特率发生器
T4T3M |= 0x02; //定时器3时钟为Fosc,即1T
T3L = 0xD0; //设定定时初
T3H = 0xFF; //设定定时初
T4T3M |= 0x08; //启动定时器3
T4T3M &= 0xFD; //定时器时钟12T模式
T3L = 0xD0; //设置定时初始
T3H = 0xFF; //设置定时初始
T4T3M |= 0x08; //定时器3开始计时
#endif
}
void L0_uart3_buf_init(void)
{
//初始化协议发送缓冲区
ts_uart[uNum3].p = &ts_uart_send_buf;
ts_uart[uNum3].p->now = 0;
ts_uart[uNum3].p->ok = D_ready;
//初始化协议接收缓冲区
ts_uart[uNum3].t = &ts_uart3_recv_buf;
ts_uart[uNum3].t->buf = uart3_recv_buf;
ts_uart[uNum3].t->head = 0;
ts_uart[uNum3].t->ok = 0;
ts_uart[uNum3].tp_handler = L1_s2b_PH1;
//串口协议解析专用字段
ts_uart[uNum3].t->head_0 = 0x60;
ts_uart[uNum3].t->maxnum = RECV_BUF_SIZE;
ts_uart[uNum3].tp_handler = L1_s2b_nfc;
//串口初始化
L0_uart3_init();
D_uart3_ES_INT(1); //打开串口中断
//打开串口中断
D_uart3_ES_INT(1);
#if (D_UART3_485_TYPE != TYPE_485_NONE)
D_UART3_485_RX() //默认处于接收状态
@ -118,14 +130,15 @@ UART 中断
void INTERRUPT_UART3(void) D_SERVE_UART3// using 3
{
NOP(); NOP(); NOP();
if(L0_uart3_IntRI()) //如果是U0接收中断
if(L0_uart3_IntRI()) //如果是U3接收中断
{
L0_uart3_IntRIClear(); //清除接收中断标志
ts_uart[uNum3].t->reg = L0_uartN_get(uNum3);
g_uart3_recv_num++;
if(ts_uart[uNum3].tp_handler != NULL){
ts_uart[uNum3].tp_handler(ts_uart[uNum3].t);
}
if(L0_uart3_IntTI())
{
}
if(L0_uart3_IntTI()) //如果是U0发送中断
{
L0_uart3_IntTIClear(); //清除发送中断标志
@ -144,7 +157,6 @@ void INTERRUPT_UART3(void) D_SERVE_UART3// using 3
#endif
}
}
}
NOP(); NOP(); NOP();
}

5
source/msp/UART3.h

@ -54,9 +54,7 @@ msp/uartx.c 底层代码 和cpu相关 缓存发送也放在里面
#define _uart3_H
#include "../bsp/bsp_config.h"
#include "../tpc/tpc_modbus.h"
#include "../tpc/tpc_modbus.h"
#include "uartN.h"
#include "uart_x.h"
#define uNum3 2
@ -88,6 +86,7 @@ void L0_uart3_sendArray(U8 * buf, U16 len);
#define L1_uart3_uchexArray(buf) L1_uartN_uchexArray(uNum3,buf)
extern Ts_uart_recv_buf ts_uart3_recv_buf;
extern int g_uart3_recv_num;
#endif //#ifndef _uart3_H

5
source/msp/uart_x.c

@ -197,15 +197,14 @@ void L0_uartN_sendstr(U8 uartx,U8 *str)
L0_uartN_sendArray(uartx,str,Lc_strlen(str));
}
void L0_uartN_uchexArray(U8 uartx,vU8 *buf,U16 n)
void L0_uartN_sendArrayHex(U8 uartx,vU8 *buf,U16 n)
{
int i;
U16 i;
for(i=0;i<n;i++)
{
L0_uartN_uchex(uartx,buf[i]);
L0_uartN_uc(uartx,' ');
}
L0_uartN_0d0a(uartx);
}

11
source/msp/uart_x.h

@ -76,13 +76,14 @@ typedef struct _ts_uart_send_buf_
typedef struct _ts_uart_recv_buf_
{//8byte
vU8 reg;
vU8 head; //接收标志头标志
vU8 ok; //接收协议ok标志,串口初始化设置为0
vU8 head; //接收标志头标志,串口初始化设置0
vU8 maxnum; //接收到的数目的最大值,串口初始化设置
vU8 cashe[2];
vU8 head_0;
vU8 head_1;
vU8 num; //接收到的数据的计数, 随着数据接收而变化注意数据长度的范围
vU8 ok; //接收协议ok标志
vU8 max; //接收到的数目的最大值
vU8 index; //当前下标
vU8 num; //协议实际长度
vU8 *buf; ////协议缓冲,由每个串口根据需要的缓冲区大小自己定义
vU8 *sp;
vU8 *sp2; //备份值,如果协议需要备份,单独提供备份缓冲区
@ -111,7 +112,7 @@ extern void L0_uartN_uchex(U8 uartx, U8 ww);
extern void L0_uartN_ushex(U8 uartx, U16 ww);
extern void L0_uartN_ulhex(U8 uartx, U32 ww);
extern void L0_uartN_sendstr(U8 uartx,U8 *buf);
extern void L1_uartN_uchexArray(U8 uartx,vU8 *buf,U16 n);
extern void L0_uartN_sendArrayHex(U8 uartx,vU8 *buf,U16 n);
#endif //#ifndef _uartN_H

50
source/tpc/debug.c

@ -52,41 +52,59 @@ msp/uartx.c 底层代码 和cpu相关
void L1_s2b_debug (Ts_uart_recv_buf *p) //reentrant
{
p->ok = 1;
LED0 ^= 1;
return;
}
void L1_s2b_PH1 (Ts_uart_recv_buf *p) //reentrant
//NFC协议:60 20 00 07 10 02 04 00 99 83 33 4E 36
//起始帧:60
//设备ID:20
//数据长度:00 07
//命令:10
//数据:02 04 00 99 83 33 4E
//校验:36 从起始帧到数据字段 [60 20 00 07 10 02 04 00 99 83 33 4E] 的所有字节的依次异或值
void L1_s2b_nfc (Ts_uart_recv_buf *p) //reentrant
{
p->ok = 1;
return;
if (0 == p->head)
{
if (D_HETU_FX_fi == (p->reg & p->head_0))
if (p->head_0 == (p->reg & 0xFF))
{
p->head = 1;
p->num = 1;
p->buf[0] = p->reg;
//p->ok = 1;
p->index = 0;
p->num = p->maxnum;
p->ocr = p->reg;
}
}
else
{
p->buf[p->num++] = p->reg;
if(p->num >= D_HETU_FX_buf_max) // [D_HETU_FX_buf_max == 5]
p->buf[++p->index] = p->reg;
if(p->index == 3)
{
p->ocr = p->buf[1];
p->ocr += p->buf[2];
p->ocr += p->buf[3];
if(p->ocr == p->buf[D_HETU_FX_buf_max-1])
//5个字节协议头 + 1校验 + 数据(长度由2、3字节计算)
p->num = 5 + 1 + ((p->buf[2] << 8) | (p->buf[3]));
if(p->num > p->maxnum){
//error
p->head = 0;
p->ok = 0;
return;
}
}
if(p->index < p->num - 1)
{
//计算OCR
p->ocr ^= p->reg;
}
else
{
if(p->ocr == p->buf[p->num-1])
{
if (p->ok != 1)
{
p->ok = 1;//收到 命令结束,必须有个地方清0,否则无法再次接受报文
//命令结束,必须有个地方清0,否则无法再次接受报文
p->ok = 1;
}
}
p->head = 0; //放在if (p->ok != 1) 外
p->head = 0;
}
}
}

14
source/tpc/debug.h

@ -38,10 +38,22 @@ typedef struct
vU8 ocr;
}TS_P_debug;
#define TPC_NFC_DAT_MAX 16
typedef struct _tpc_nfc_
{
U8 head;
U8 slaveId;
U8 num[2];
U8 cmd;
U8 buf[TPC_NFC_DAT_MAX];
U8 ocr;
}TPC_NFC;
#define D_HETU_FX_buf_max 5 //定长协议 长度为5
#define D_HETU_FX_fi 0x0F
#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_PH1 (Ts_uart_recv_buf *p);// reentrant;
#endif /* end __TPC_DEBUG_H_ */

4
source/tpc/modbus.c

@ -44,14 +44,14 @@ void L1_s2b_PH4(Ts_uart_recv_buf *p)
if(p->head == 0)
{
p->head = 1;
p->max = D_TPC_HANDLER_X_LEN;
p->maxnum = D_TPC_HANDLER_X_LEN;
p->sp = p->buf;
p->num = 0;
p->sp[p->num++] = p->reg;
}
else
{
if(p->num < p->max)
if(p->num < p->maxnum)
{
p->sp[p->num++] = p->reg;
}

Loading…
Cancel
Save