sop板
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

158 lines
4.3 KiB

////////////////////////////////////////////////////////////////////////////
///@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_rs485.h"
#include "../app/app_config.h"
#include "../bsp/bsp_config.h"
#include "../msp/uart0.h"
#include "../msp/uart4.h"
S_TASK_RS485 _s_task_rs485;
TPC_RS485 tpc_rs485;
TPC_RS485_ACK tpc_rs485_ack;
static U8 acklen = 0;
static U8 i = 0;
U16 setLedStatus = 0;
//=============================================
void L3_task_rs485_init(void)
{
L1_task_init(&_s_task_rs485.task);
L3_task_s_go(_s_task_rs485,D_task_init);
}
#define D_task_RS485_READ 0x50
#define D_task_RS485_PRINT 0x51
#define D_task_RS485_ACK 0x52
#define D_task_RS485_MODE 0x53
void L3_task_rs485_handler(S_TASK_RS485 *s)
{
TTSS_Task_init()
L2_task_go(D_task_RS485_READ);
TTSS_Task_step(D_task_RS485_READ)
if(ts_uart[uNum4].r.ok == 1)
{
ts_uart[uNum4].r.ok = 0;
parse_rs485_pkg();
}
//read next
L2_task_go_Tdelay(D_task_RS485_READ,0);
TTSS_Task_end();
}
//485协议:AA 00 04 01 10 00 00 15
//FILTER:固定1个字节 AA
//SLAVEID:固定1个字节,代表从机ID
//CMDER: 固定1个字节,代表命令,每个从机自己定义。
//NUM: 固定2个字节,高字节在前,代表后续DATA的长度(不包括ocr),比如0x0010,代表后续 16个字节
//DATA:(Num 个字节)数据域
//OCR:1个字节,代表校验和, = 从SlaveId开始 - DATA结束的所有字节之和。
void parse_rs485_pkg()
{
TPC_RS485 *p = (TPC_RS485 *) ts_uart[uNum4].r.buf;
p->ocr = p->buf[p->num[0] << 8 | p->num[1]];
if(p->head[0] == 0xAA && 1 /*count_ocr() */)
{
if(p->slaveId == R.slave_id && p->cmd == 0x10)
{
//1.buf[0]高4位是否需要授权标志
R.auth_flag &= 0x0F;
R.auth_flag |= p->buf[0] & 0xF0;
//2.buf[0]低四位是否进入低功耗模式
// R.low_power_mode = p->buf[0] & 0x0F;
L3_set_power_mode(p->buf[0] & 0x0F);
//3.buf[1-2], rfid映射
//TODO 上位机发过来的rfid编号如何处理
setLedStatus = (U16)p->buf[1] << 8 | p->buf[2];
if(setLedStatus != 0x00)
{
//0位是功能按键,常亮,不提供设置功能
for(i=1;i<LED_BTN_NUM;i++)
{
//不处理0,只处理1的情况(代表其他端产生了sop中的节点,比如平板上触发谈话等)
if((setLedStatus >> i) & 0x0001)
{
R.led_status[i] = LED0_ON;
}
}
}
//构造响应包,并且返回
acklen = constructor_rs485_ack();
//写出
L0_uart4_sendArray((U8*)&tpc_rs485_ack, acklen);
//日志
print_rs485_pkg(p);
}
}
}
void print_rs485_pkg(TPC_RS485 *p)
{
L0_uart0_sendstr("\r\n--------- Recv RS485 --------\r\n");
L0_uart0_sendstr("slaveId : ");
L0_uart0_uchex(p->slaveId);
L0_uart0_0d0a();
L0_uart0_sendstr("cmd: ");
L0_uart0_uchex(p->cmd);
L0_uart0_0d0a();
L0_uart0_sendstr("num: ");
L0_uart0_uchex(p->num[0]);
L0_uart0_uchex(p->num[1]);
L0_uart0_0d0a();
L0_uart0_sendstr("dat : ");
L0_uart0_sendArrayHex(p->buf, p->num[0] << 8 | p->num[1]);
L0_uart0_0d0a();
L0_uart0_sendstr("ocr: ");
L0_uart0_uchex(p->ocr);
L0_uart0_0d0a();
}
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.slaveId = R.slave_id;
tpc_rs485_ack.cmd = 0x10;
tpc_rs485_ack.num[0] = (num >> 8) & 0xFF;
tpc_rs485_ack.num[1] = num & 0xFF;
tpc_rs485_ack.buf[0] = R.func_btn;
Lc_buf_copy_uc((U8*)&tpc_rs485_ack.buf[1], (U8*)&R.nfc, sizeof(R.nfc));
Lc_buf_copy_uc((U8*)&tpc_rs485_ack.buf[1+sizeof(R.nfc)], (U8*)R.rfids, sizeof(R.rfids));
tpc_rs485_ack.ocr = 0;
tpc_rs485_ack.ocr += tpc_rs485_ack.slaveId;
tpc_rs485_ack.ocr += tpc_rs485_ack.cmd;
tpc_rs485_ack.ocr += tpc_rs485_ack.num[0];
tpc_rs485_ack.ocr += tpc_rs485_ack.num[1];
for(i=0;i<num;i++)
{
tpc_rs485_ack.ocr += tpc_rs485_ack.buf[i];
}
tpc_rs485_ack.buf[num] = tpc_rs485_ack.ocr;
//清0资源
R.func_btn = 0;
L3_nfc_clear();
L3_rfid_clear();
return 5 + num + 1;
}