diff --git a/source/.vscode/settings.json b/source/.vscode/settings.json index fcdb329..3e25e41 100644 --- a/source/.vscode/settings.json +++ b/source/.vscode/settings.json @@ -21,6 +21,8 @@ "uart2.h": "c", "task_rfid.h": "c", "task_rs485.h": "c", - "rs485.h": "c" + "rs485.h": "c", + "bsp_led.h": "c", + "task_nfc_auth.h": "c" } } \ No newline at end of file diff --git a/source/app/app_config.c b/source/app/app_config.c index f7857f9..36dbd79 100644 --- a/source/app/app_config.c +++ b/source/app/app_config.c @@ -76,12 +76,32 @@ int i; void L3_reg_reset(void) { + //设置RFID对照表 + for(i=0; i> 8 & 0xFF; + R.rfids[i].rfid_no[1] = rfidno >> 0 & 0xFF; + R.rfids[i].rssi = rssi; + R.rfids[i].ant = ant; + return 1; + } + if(R.rfids[i].rfid_no[0] == (rfidno >> 8 & 0xFF) && R.rfids[i].rfid_no[1] == (rfidno >> 0 & 0xFF)) + { + //该rfid已经存在 + break; + } + } + return 0; +} + +U8 L3_find_rfid_table(U16 rfidno) +{ + for(i=0; i0代表需要授权 + //低4位 > 0代表已授权,低4位==0代表未授权 + //0x01 代表按键不需要授权、已授权 + //0x00 代表按键不需要授权、未授权 + //0x11 代表按键需要授权、已授权 + //0x10 代表按键需要授权、未授权 + U8 auth_flag; + //功能按键 U8 func_btn; @@ -47,8 +64,8 @@ typedef struct global_register U8 rfid_no[2]; //rfid信号强度 U8 rssi; - //rfid天线编号 - U8 ant; + //rfid天线编号1,2,3,4 FF代表按键 + U8 ant; }rfids[4]; }GlobalRegister; @@ -81,6 +98,8 @@ extern void L3_reg_2_iap(void); extern void L3_iap_2_reg(void); extern void L3_reg_reset(void); extern void L3_reg_init(void); +extern U8 L3_find_rfid_table(U16 rfidno); +extern U8 L3_new_rfid(U16 rfidno, U8 rssi, U8 ant); #endif diff --git a/source/app/main.c b/source/app/main.c index b8a59b0..9932d6c 100644 --- a/source/app/main.c +++ b/source/app/main.c @@ -85,6 +85,7 @@ void L0_main_init(void) L0_board_config(); //外设初始化 + bsp_led_init(); bsp_key_init(); bsp_rfid_init(); @@ -107,6 +108,9 @@ void L0_main_init(void) L3_task_nfc_init(); L3_task_rfid_init(); L3_task_rs485_init(); + L3_task_bled_init(); + L3_task_btn_init(); + L3_task_auth_init(); // L3_task_adc_init(0); // L3_task_adc_init(1); @@ -156,8 +160,6 @@ void main(void) { s_nos_tick.t1s_heatbeart = 0; LED0 ^= 1; - // KR1 ^= 1; - // KC1 ^= 1; L0_uart0_uc('.'); // L0_uart0_uchex(g_uart3_recv_num); // L0_uart0_uchex(count); @@ -165,16 +167,18 @@ void main(void) //L0_uartN_sendstr(0,"v2.8111"); } - // 扫描键盘 - key = bsp_key_scan(); - if(key != BSP_KEY_NUM_MAX){ - L0_uart0_uc('#'); - L0_uart0_uchex(key); - } + //设置bled任务 + L3_task_bled_handler(&_s_task_bled); + + //扫描键盘任务 + L3_task_btn_handler(&_s_task_btn); //NFC协议处理 L3_task_nfc_handler(&_s_task_nfc); + //10s认证过期 + L3_task_auth_handler(&_s_task_auth); + //RFID协议处理 L3_task_rfid_handler(&_s_task_rfid); diff --git a/source/app/main.h b/source/app/main.h index 44a97f8..266c996 100644 --- a/source/app/main.h +++ b/source/app/main.h @@ -40,6 +40,7 @@ #include "../bsp/bsp_config.h" #include "../bsp/bsp_config_delay.h" +#include "../bsp/bsp_led.h" #include "../bsp/bsp_key.h" #include "../bsp/bsp_rfid.h" #include "../bsp/chipid.h" @@ -57,7 +58,9 @@ #include "../app/task_nfc.h" #include "../app/task_rfid.h" #include "../app/task_rs485.h" - +#include "../app/task_bled.h" +#include "../app/task_btn.h" +#include "../app/task_nfc_auth.h" ////////////////////////////////////////////////////////////////// diff --git a/source/app/task_bled.c b/source/app/task_bled.c new file mode 100644 index 0000000..94c679b --- /dev/null +++ b/source/app/task_bled.c @@ -0,0 +1,50 @@ +//////////////////////////////////////////////////////////////////////////// +///@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_bled.h" +#include "../app/app_config.h" +#include "../bsp/bsp_config.h" +#include "../msp/uart0.h" +#include "../bsp/bsp_led.h" + +S_TASK_BLED _s_task_bled; +static int i; + +void L3_task_bled_init(void) +{ + L1_task_init(&_s_task_bled.task); + L3_task_s_go(_s_task_bled,D_task_init); +} + +#define D_task_BLED_SET 0x50 + +void L3_task_bled_handler(S_TASK_BLED *s) +{ + TTSS_Task_init() + L2_task_go(D_task_BLED_SET); + + TTSS_Task_step(D_task_BLED_SET) + //L0_uart0_sendArrayHex(R.led_status, 14); + for(i=0;iauth_flag != R.auth_flag){ + s->auth_flag = R.auth_flag; + //授权中 + if(R.auth_flag & 0x0F != 0){ + L2_task_go_Tdelay(D_task_AUTH_SETZERO,D_Tdelay_10s); + } + } + + TTSS_Task_step(D_task_AUTH_SETZERO) + //清除授权标志 + R.auth_flag &= 0xF0; + L0_uart0_sendstr("Auth Expired."); + L2_task_go_Tdelay(D_task_AUTH_READ,0); + + TTSS_Task_end(); +} + + + + diff --git a/source/app/task_nfc_auth.h b/source/app/task_nfc_auth.h new file mode 100644 index 0000000..818bfb6 --- /dev/null +++ b/source/app/task_nfc_auth.h @@ -0,0 +1,34 @@ +//////////////////////////////////////////////////////////////////////////// +///@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_AUTH_H +#define _APP_TASK_NFC_AUTH_H + +#include "../ctask/task.h" +#include "../clib/clib.h" +#include "../tpc/debug.h" + +typedef struct _s_task_auth +{ + TS_task task; + U8 auth_flag; +}S_TASK_AUTH; + +extern S_TASK_AUTH _s_task_auth; + +extern void L3_task_auth_init(void); +extern void L3_task_auth_handler(S_TASK_AUTH *s); + + +#endif // #ifndef _APP_TASK_NFC_AUTH_H + diff --git a/source/app/task_rfid.c b/source/app/task_rfid.c index 806302d..bd5240c 100644 --- a/source/app/task_rfid.c +++ b/source/app/task_rfid.c @@ -37,7 +37,7 @@ void L3_task_rfid_init(void) #define D_task_RFID_READ 0x50 #define D_task_RFID_PRINT 0x51 -#define D_task_RFID_MODE 0x52 +#define D_task_RFID_SET 0x52 void L3_task_rfid_handler(S_TASK_RFID *s) { @@ -68,9 +68,30 @@ void L3_task_rfid_handler(S_TASK_RFID *s) 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); + if(tpc_rfid.type == 0x02 && tpc_rfid.cmd == 0x22){ + // L2_task_go_Tdelay(D_task_RFID_PRINT,0); + L2_task_go_Tdelay(D_task_RFID_SET,0); + } } + TTSS_Task_step(D_task_RFID_SET) + U8 index = 0; + U8 num = (U16)tpc_rfid.num[0] << 8 | tpc_rfid.num[1]; + U16 rfidno = (U16)tpc_rfid.buf[num-5] << 8 | tpc_rfid.buf[num-4]; + U8 rssi = tpc_rfid.buf[0]; + U8 ant = tpc_rfid.buf[num-1]; + //1.判定RFID卡号是否符合规则 + //2.保存RFID卡号到寄存器 + L3_new_rfid(rfidno, rssi, ant); + //3.查找编号是否在rfidtable中 + index = L3_find_rfid_table(rfidno); + //4.亮灯 + //TODO 如果缓冲区满了,没放进去,是否亮灯 + if(index < LED_BTN_NUM){ + R.led_status[index] = BLED0_ON; + } + L2_task_go_Tdelay(D_task_RFID_READ,0); + TTSS_Task_step(D_task_RFID_PRINT) L0_uart0_sendstr("\r\n--------- Recv RFID --------\r\n"); L0_uart0_sendstr("type : "); diff --git a/source/app/task_rs485.c b/source/app/task_rs485.c index 7c4472c..d79778e 100644 --- a/source/app/task_rs485.c +++ b/source/app/task_rs485.c @@ -44,10 +44,12 @@ void L3_task_rs485_handler(S_TASK_RS485 *s) TTSS_Task_step(D_task_RS485_READ) if(ts_uart4_recv_buf.ok == 1) { + //L0_uart0_uc('%'); + // LED0 ^= 1; ts_uart4_recv_buf.ok = 0; Lc_buf_copy_uc((U8*)&tpc_rs485, ts_uart4_recv_buf.buf, ts_uart4_recv_buf.num); tpc_rs485.ocr = tpc_rs485.buf[tpc_rs485.num[0] << 8 | tpc_rs485.num[1]]; - // L2_task_go_Tdelay(D_task_RS485_PRINT,0); + //L2_task_go_Tdelay(D_task_RS485_PRINT,0); L2_task_go_Tdelay(D_task_RS485_ACK,0); } @@ -57,10 +59,12 @@ void L3_task_rs485_handler(S_TASK_RS485 *s) //读取 if(tpc_rs485.cmd == 0x10) { - //buf[0] 低功耗模式 - R.low_power_mode = tpc_rs485.buf[0]; + //buf[0] 是否需要授权标志、低功耗模式 + R.auth_flag &= 0x0F; + R.auth_flag |= tpc_rs485.buf[0] & 0xF0; + R.low_power_mode = tpc_rs485.buf[0] & 0x0F; //buf[1-2], rfid映射 - //TODO + //TODO 上位机发过来的rfid编号如何处理 //构造响应包,并且返回 @@ -98,7 +102,7 @@ U8 constructor_rs485_ack() { U8 num = (1 + sizeof(R.nfc) + sizeof(R.rfids)); tpc_rs485_ack.head[0] = 0xAA; - tpc_rs485_ack.head[1] = 0x55; + // tpc_rs485_ack.head[1] = 0x55; tpc_rs485_ack.slaveId = R.slave_id; tpc_rs485_ack.cmd = 0x10; tpc_rs485_ack.num[0] = (num >> 8) & 0xFF; @@ -130,7 +134,7 @@ U8 constructor_rs485_ack() R.rfids[3].rfid_no[0] = 0x00; R.rfids[3].rfid_no[1] = 0x00; - return 6 + num + 1; + return 5 + num + 1; } diff --git a/source/bsp/bsp_config.c b/source/bsp/bsp_config.c index 523f88b..99a8720 100644 --- a/source/bsp/bsp_config.c +++ b/source/bsp/bsp_config.c @@ -31,10 +31,6 @@ void L0_board_config(void) 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; - LED0 = LED0_ON; //uart0 #if (PIN_UART0_Rxd_30_Txd_31 == D_PIN_UART0) diff --git a/source/bsp/bsp_led.c b/source/bsp/bsp_led.c new file mode 100644 index 0000000..852ff8b --- /dev/null +++ b/source/bsp/bsp_led.c @@ -0,0 +1,55 @@ +#include "bsp_led.h" + +void bsp_led_init(void) +{ + //LED0 + D_stdIO_P2(BITN3); + + //BLED0-13 + D_stdIO_P4(BITN2); + D_stdIO_P4(BITN1); + D_stdIO_P3(BITN7); + D_stdIO_P3(BITN5); + D_stdIO_P1(BITN6); + D_stdIO_P1(BITN5); + D_stdIO_P1(BITN4); + D_stdIO_P4(BITN7); + D_stdIO_P1(BITN1); + D_stdIO_P2(BITN6); + D_stdIO_P2(BITN5); + D_stdIO_P2(BITN1); + D_stdIO_P4(BITN4); + D_stdIO_P2(BITN2); + + //初始化 + BLED0 = BLED1 = BLED2 = BLED3 = BLED4 = BLED5 = BLED6 = BLED0_OFF; + BLED7 = BLED8 = BLED9 = BLED10 = BLED11 = BLED12 = BLED13 = BLED0_OFF; + LED0 = LED0_OFF; +} + +void bsp_led_set(U8 status) +{ + LED0 = status; +} + +void bsp_bled_set(U8 index, U8 status) +{ + switch (index) + { + case 0: BLED0 = status; break; + case 1: BLED1 = status; break; + case 2: BLED2 = status; break; + case 3: BLED3 = status; break; + case 4: BLED4 = status; break; + case 5: BLED5 = status; break; + case 6: BLED6 = status; break; + case 7: BLED7 = status; break; + case 8: BLED8 = status; break; + case 9: BLED9 = status; break; + case 10:BLED10 = status; break; + case 11:BLED11 = status; break; + case 12:BLED12 = status; break; + case 13:BLED13 = status; break; + default: break; + } +} \ No newline at end of file diff --git a/source/bsp/bsp_led.h b/source/bsp/bsp_led.h new file mode 100644 index 0000000..e6bb12d --- /dev/null +++ b/source/bsp/bsp_led.h @@ -0,0 +1,14 @@ +#ifndef _BSP_LED_H +#define _BSP_LED_H + +#include "bsp_config.h" + +extern void bsp_led_init(void); +extern void bsp_bled_set(U8 index, U8 status); +extern void bsp_led_set(U8 status); + +#define BLED_SET_STATUS(N, status); do { \ + BLED##N = status; \ + }while(0); + +#endif //_BSP_LED_H \ No newline at end of file diff --git a/source/msp/UART4.C b/source/msp/UART4.C index a73d011..7ef7b59 100644 --- a/source/msp/UART4.C +++ b/source/msp/UART4.C @@ -142,7 +142,6 @@ void INTERRUPT_uart4(void) D_SERVE_uart4// using 2 //NOP(); NOP(); NOP(); if(L0_uart4_IntRI()) //如果是U0接收中断 { - LED0 ^= 1; L0_uart4_IntRIClear(); //清除接收中断标志 // ts_uart[uNum4].t->reg = L0_uartN_get(uNum4); ts_uart[uNum4].t->reg = S4BUF; diff --git a/source/tpc/debug.c b/source/tpc/debug.c index 330e36c..fdd2ec2 100644 --- a/source/tpc/debug.c +++ b/source/tpc/debug.c @@ -115,7 +115,7 @@ void L1_s2b_nfc (Ts_uart_recv_buf *p) //reentrant //Type:00命令帧 /01响应帧 /02通知帧 //Command: 07指令桢 /22单次读写 /27多次轮询 /28停止轮询 /0C选择 / B6设置功率 //数据长度:00 11 -//数据:D5 30 00 E2 00 10 71 00 00 52 9B 09 40 B4 02 EB 98 +//数据: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 @@ -173,39 +173,30 @@ void L1_s2b_rfid (Ts_uart_recv_buf *p) //reentrant //NUM: 固定2个字节,高字节在前,代表后续DATA的长度(不包括ocr),比如0x0010,代表后续 16个字节 //DATA:(Num 个字节)数据域 //OCR:1个字节,代表校验和, = 从SlaveId开始 - DATA结束的所有字节之和。 + void L1_s2b_rs485 (Ts_uart_recv_buf *p) //reentrant { - // LED0 ^= 1; // p->ok = 1; // return; if (0 == p->head) { - p->buf[p->index++] = p->reg; - if(p->index == 2){ - if (p->head_0 == p->buf[0] && p->head_1 == p->buf[1]) - { - p->head = 1; - p->index = 1; - p->num = p->maxnum; - p->ocr = 0; - } - else - { - p->head = 0; - p->ok = 0; - p->index = 0; - //往前移动一位 - p->buf[p->index++] = p->buf[1]; - } + if (p->head_0 == (p->reg & 0xFF)) + { + p->head = 1; + p->buf[0] = p->reg; + p->index = 0; + p->num = p->maxnum; + //header字节不算累加和 + p->ocr = 0; } } else { p->buf[++p->index] = p->reg; - if(p->index == 5) + if(p->index == 4) { - //2个字节协议头 + 1SlaveId + 1Cmder + 2字节数据长度 + 数据 + 1ocr - p->num = 6 + 1 + ((p->buf[4] << 8) | (p->buf[5])); + //1个字节协议头 + 1SlaveId + 1Cmder + 2字节数据长度 + 数据 + 1ocr + p->num = 5 + 1 + ((p->buf[3] << 8) | (p->buf[4])); if(p->num > p->maxnum){ //error p->head = 0; @@ -226,17 +217,81 @@ void L1_s2b_rs485 (Ts_uart_recv_buf *p) //reentrant if (p->ok != 1) { //命令结束,必须有个地方清0,否则无法再次接受报文 + // LED0 ^= 1; p->ok = 1; } } p->head = 0; - p->index = 0; } } } +// void L1_s2b_rs485 (Ts_uart_recv_buf *p) //reentrant +// { +// // LED0 ^= 1; +// // p->ok = 1; +// // return; +// if (0 == p->head) +// { +// p->buf[p->index++] = p->reg; +// if(p->index == 2){ +// if (p->head_0 == p->buf[0] && p->head_1 == p->buf[1]) +// { +// p->head = 1; +// p->index = 1; +// p->num = p->maxnum; +// p->ocr = 0; +// } +// else +// { +// p->head = 0; +// p->ok = 0; +// p->index = 0; +// //往前移动一位 +// p->buf[p->index++] = p->buf[1]; +// } +// } +// } +// else +// { +// p->buf[++p->index] = p->reg; +// if(p->index == 5) +// { +// //2个字节协议头 + 1SlaveId + 1Cmder + 2字节数据长度 + 数据 + 1ocr +// p->num = 6 + 1 + ((p->buf[4] << 8) | (p->buf[5])); +// if(p->num > p->maxnum){ +// //error +// p->head = 0; +// p->ok = 0; +// p->index = 0; +// return; +// } +// } +// if(p->index < p->num - 1) +// { +// //计算OCR +// p->ocr += p->reg; +// } +// else if(p->index == p->num - 1) +// { +// if(p->ocr == p->buf[p->num-1]) +// { +// if (p->ok != 1) +// { +// //命令结束,必须有个地方清0,否则无法再次接受报文 +// p->ok = 1; +// } +// } +// p->head = 0; +// p->index = 0; +// } +// } +// } + + + /****************************************************************************** ** End Of File diff --git a/source/tpc/debug.h b/source/tpc/debug.h index 20e3a79..c35ba9b 100644 --- a/source/tpc/debug.h +++ b/source/tpc/debug.h @@ -64,7 +64,7 @@ typedef struct _tpc_rfid_ #define TPC_RS485_DAT_MAX 32 typedef struct _tpc_rs485_ { - U8 head[2]; + U8 head[1]; U8 slaveId; U8 cmd; U8 num[2]; @@ -72,10 +72,21 @@ typedef struct _tpc_rs485_ U8 ocr; }TPC_RS485; +// #define TPC_RS485_DAT_MAX 32 +// typedef struct _tpc_rs485_ +// { +// U8 head[2]; +// U8 slaveId; +// U8 cmd; +// U8 num[2]; +// U8 buf[TPC_RS485_DAT_MAX]; +// U8 ocr; +// }TPC_RS485; + #define TPC_RS485_ACK_DAT_MAX 32 typedef struct _tpc_rs485_ack_ { - U8 head[2]; + U8 head[1]; U8 slaveId; U8 cmd; U8 num[2];