diff --git a/source/app/main.c b/source/app/main.c index dd894ef..75d8b82 100644 --- a/source/app/main.c +++ b/source/app/main.c @@ -86,6 +86,7 @@ void L0_main_init(void) //外设初始化 bsp_key_init(); + bsp_rfid_init(); //串口初始化 L0_uart0_buf_init(); diff --git a/source/app/main.h b/source/app/main.h index dbe37e2..687fdee 100644 --- a/source/app/main.h +++ b/source/app/main.h @@ -44,6 +44,7 @@ #include "../bsp/bsp_config.h" #include "../bsp/bsp_config_delay.h" #include "../bsp/bsp_key.h" +#include "../bsp/bsp_rfid.h" #include "../bsp/chipid.h" #include "../bsp/cs1232.h" #include "../bsp/rs485.h" diff --git a/source/bsp/bsp_config.h b/source/bsp/bsp_config.h index 08d3757..8c13f32 100644 --- a/source/bsp/bsp_config.h +++ b/source/bsp/bsp_config.h @@ -136,6 +136,12 @@ #define KC2 P07 //NET6 #define KC3 P13 //NET7 #define KC4 P20 //NET8 + + //RFID EN -- 暂未定义 + #define RFID_ENABLE 1 + #define RFID_DISABLE 0 + #define RFID_EN_PIN P32 //RFID-EN >1.1v使能 + //EEP存储地址定义 #define D_EEP_SECTOR_SIZE 0x200 //每个扇区0x200==512bytes diff --git a/source/bsp/bsp_rfid.c b/source/bsp/bsp_rfid.c new file mode 100644 index 0000000..e816fd3 --- /dev/null +++ b/source/bsp/bsp_rfid.c @@ -0,0 +1,14 @@ +#include "bsp_rfid.h" +#include "bsp_config_delay.h" + +U8 bsp_rfid_init(void) +{ + D_stdIO_P3(BITN2); + bsp_rfid_en(RFID_ENABLE); + return 0; +} + +void bsp_rfid_en(U8 status) +{ + RFID_EN_PIN = status; +} \ No newline at end of file diff --git a/source/bsp/bsp_rfid.h b/source/bsp/bsp_rfid.h new file mode 100644 index 0000000..afe1c95 --- /dev/null +++ b/source/bsp/bsp_rfid.h @@ -0,0 +1,9 @@ +#ifndef _BSP_RFID_H +#define _BSP_RFID_H + +#include "bsp_config.h" + +extern U8 bsp_rfid_init(void); +extern void bsp_rfid_en(U8 status); + +#endif \ No newline at end of file diff --git a/source/msp/UART2.c b/source/msp/UART2.c index 4c7f0ee..90fd8b0 100644 --- a/source/msp/UART2.c +++ b/source/msp/UART2.c @@ -55,13 +55,15 @@ msp/uartx.c 底层代码 和cpu相关 缓存发送也放在里面 ----------------------------------------------------------------------------------------- ********************************************************************************/ #include "uart2.h" -#include "../app/common.h" +#include "../tpc/debug.h" //发送缓冲区 static volatile Ts_uart_send_buf idata ts_uart_send_buf; //接收缓冲区最多存放16个字符,这个值尽量小,但要大于实际处理协议的缓冲区大小 -static U8 uart2_recv_buf[16]; +#define RECV_BUF_SIZE 16 +static U8 uart2_recv_buf[RECV_BUF_SIZE]; Ts_uart_recv_buf ts_uart2_recv_buf; +int g_uart2_recv_num = 0; void L0_uart2_init(void) { @@ -72,29 +74,38 @@ void L0_uart2_init(void) T2H = 0xFF; //设定定时初值 AUXR |= 0x10; //启动定时器2 -#elif (MainFre_22M == D_sys_MainFre) //115200bps@22.1184MHz +#elif (MainFre_22M == D_sys_MainFre) //9600bps@22.1184MHz S2CON = 0x50; //8位数据,可变波特率 - AUXR |= 0x04; //定时器2时钟为Fosc,即1T - T2L = 0xD0; //设定定时初值 - T2H = 0xFF; //设定定时初值 - AUXR |= 0x10; //启动定时器2 + AUXR &= 0xFB; //定时器时钟12T模式 + T2L = 0xD0; //设置定时初始值 + T2H = 0xFF; //设置定时初始值 + AUXR |= 0x10; //定时器2开始计时 #endif } void L0_uart2_buf_init(void) { + //初始化协议发送缓冲区 ts_uart[uNum2].p = &ts_uart_send_buf; ts_uart[uNum2].p->now = 0; ts_uart[uNum2].p->ok = D_ready; - ts_uart[uNum2].t = &ts_uart_recv_buf; + + //初始化协议接收缓冲区 + ts_uart[uNum2].t = &ts_uart2_recv_buf; ts_uart[uNum2].t->buf = uart2_recv_buf; - ts_uart[uNum2].t->head_0 = D_CMD_Filter1_ff; - ts_uart[uNum2].t->head_1 = D_CMD_Filter2_fe; ts_uart[uNum2].t->head = 0; ts_uart[uNum2].t->ok = 0; - ts_uart[uNum2].tp_handler = L1_s2b_PH3; + + //串口协议解析专用字段 + ts_uart[uNum2].t->head_0 = 0x60; + ts_uart[uNum2].t->maxnum = RECV_BUF_SIZE; + ts_uart[uNum2].tp_handler = L1_s2b_nfc; + + //串口初始化 L0_uart2_init(); - D_uart2_ES_INT(1); //打开串口中断 + + //打开串口中断 + D_uart2_ES_INT(1); #if (D_UART2_485_TYPE != TYPE_485_NONE) D_UART2_485_RX() //默认处于接收状态 @@ -115,12 +126,14 @@ UART 中断 #define D_SERVE_uart2 interrupt 8 void INTERRUPT_uart2(void) D_SERVE_uart2// using 2 { - //NOP(); NOP(); NOP(); + NOP(); NOP(); NOP(); if(L0_uart2_IntRI()) //如果是U2接收中断 { L0_uart2_IntRIClear(); //清除接收中断标志 ts_uart[uNum2].t->reg = L0_uartN_get(uNum2); - ts_uart[uNum2].tp_handler(ts_uart[uNum2].t)); + if(ts_uart[uNum2].tp_handler != NULL){ + ts_uart[uNum2].tp_handler(ts_uart[uNum2].t); + } } if(L0_uart2_IntTI()) //如果是U0发送中断 { @@ -138,10 +151,9 @@ void INTERRUPT_uart2(void) D_SERVE_uart2// using 2 #if (D_UART2_485_TYPE != TYPE_485_NONE) D_UART2_485_RX() //切换到接收状态 #endif - } } - //NOP(); NOP(); NOP(); + NOP(); NOP(); NOP(); } diff --git a/source/msp/UART2.h b/source/msp/UART2.h index b916b67..7dcaead 100644 --- a/source/msp/UART2.h +++ b/source/msp/UART2.h @@ -54,8 +54,7 @@ msp/uartx.c 底层代码 和cpu相关 缓存发送也放在里面 #define _uart2_H #include "../bsp/bsp_config.h" -#include "../tpc/tpc_ccmodbus.h" -#include "uartN.h" +#include "uart_x.h" #define uNum2 1 @@ -78,8 +77,10 @@ void L0_uart2_sendArray(U8 * buf, U16 len); #define L0_uart2_ushex(X) L0_uartN_ushex(uNum2,X) #define L0_uart2_ulhex(X) L0_uartN_ulhex(uNum2,X) #define L0_uart2_sendstr(buf) L0_uartN_sendstr(uNum2,buf) +#define L1_uart2_uchexArray(buf) L1_uartN_uchexArray(uNum2,buf) extern Ts_uart_recv_buf ts_uart2_recv_buf; +extern int g_uart2_recv_num; #endif //#ifndef _uart2_H diff --git a/source/tpc/debug.c b/source/tpc/debug.c index 38da3e3..37b0efc 100644 --- a/source/tpc/debug.c +++ b/source/tpc/debug.c @@ -110,6 +110,62 @@ void L1_s2b_nfc (Ts_uart_recv_buf *p) //reentrant } +//RFID协议:BB 02 22 00 11 D5 30 00 E2 00 10 71 00 00 52 9B 09 40 B4 02 EB 98 0C 7E +//帧头:BB +//Type:00命令帧 /01响应帧 /02通知帧 +//Command: 07指令桢 /22单次读写 /27多次轮询 /28停止轮询 /0C选择 / B6设置功率 +//数据长度:00 07 +//数据: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 +void L1_s2b_rfid (Ts_uart_recv_buf *p) //reentrant +{ + if (0 == p->head) + { + if (p->head_0 == (p->reg & 0xFF)) + { + p->head = 1; + p->buf[0] = p->reg; + p->index = 0; + p->num = p->maxnum; + p->ocr = p->reg; + } + } + else + { + p->buf[++p->index] = p->reg; + if(p->index == 3) + { + //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) + { + //命令结束,必须有个地方清0,否则无法再次接受报文 + p->ok = 1; + } + } + p->head = 0; + } + } +} + + /****************************************************************************** ** End Of File