移植与电子模块通信相关代码
This commit is contained in:
@@ -1,470 +0,0 @@
|
||||
#include "stdio.h"
|
||||
#include "rtthread.h"
|
||||
#include "board.h"
|
||||
#include "mystdlib.h"
|
||||
#include "list.h"
|
||||
#include "mystring.h"
|
||||
#include "signal.h"
|
||||
#include "prot_mcu.h"
|
||||
#include "buff.h"
|
||||
#include "bytearray.h"
|
||||
#include "debug.h"
|
||||
#include "crc.h"
|
||||
|
||||
|
||||
|
||||
typedef struct{
|
||||
uint16_t no;
|
||||
list_def *cmds;//int
|
||||
uint8_t addr;
|
||||
uint8_t cmd;
|
||||
array_def *data;
|
||||
int retry;
|
||||
int timeout_ms;
|
||||
}protm_slave;
|
||||
|
||||
|
||||
|
||||
static int protm_slave_sub(void *a,void *b)
|
||||
{
|
||||
protm_slave *a_=a;
|
||||
protm_slave *b_=b;
|
||||
return a_->addr-b_->addr;
|
||||
}
|
||||
|
||||
static int protm_slave_del(void *t)
|
||||
{
|
||||
protm_slave *p=t;
|
||||
CHECK_DO(p->cmds,list_delete);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static protm_slave *protm_slave_creat(void)
|
||||
{
|
||||
protm_slave *p=calloc(1,sizeof(protm_slave));
|
||||
param_check(p);
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
array_def *protm_decode(protm_def *p,array_def *data);
|
||||
protm_slave *protm_get_slave(protm_def *p,uint8_t addr);
|
||||
|
||||
|
||||
|
||||
typedef struct{
|
||||
array_def *data;
|
||||
int addr;
|
||||
}send_data_def;
|
||||
|
||||
|
||||
// 定义事件
|
||||
#define EVENT_RECV 0x1
|
||||
#define EVENT_SEND 0x2
|
||||
#define EVENT_TIMEOUT 0x4
|
||||
|
||||
|
||||
|
||||
struct _protm_def{
|
||||
uart_def *uart;
|
||||
array_def *buff;
|
||||
int num_to_recv;
|
||||
rt_event_t event;
|
||||
rt_timer_t timer;
|
||||
int run;
|
||||
char *str_err;
|
||||
list_def *slaves;//protm_slave
|
||||
list_def *slaves_addr;//int
|
||||
list_def *send_data;//send_data_def
|
||||
int in_send;//在发送状态时为1
|
||||
uint8_t recv_cmd;
|
||||
uint8_t recv_src;
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
static void recv_irq(void *t,uint8_t d)
|
||||
{
|
||||
protm_def *p=t;
|
||||
arr_append(p->buff,d);
|
||||
switch(arr_length(p->buff)){
|
||||
case 2:
|
||||
if(arr_get(p->buff,0)=='Y'&&arr_get(p->buff,1)=='e')
|
||||
{
|
||||
p->num_to_recv=4;
|
||||
}else{
|
||||
arr_remove(p->buff,0,1);
|
||||
p->num_to_recv=0;
|
||||
}
|
||||
break;
|
||||
case 4:
|
||||
{
|
||||
int len=arr_get(p->buff,2)|(arr_get(p->buff,3)<<8);
|
||||
p->num_to_recv=len;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
// 此时一帧数据已完成
|
||||
if(p->num_to_recv>0&&p->num_to_recv==arr_length(p->buff))
|
||||
{
|
||||
rt_event_send(p->event,EVENT_RECV);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* 与从机通信的串口是串行的,一个通信事务完成之后才能开始下一个通信
|
||||
* 协议层接收的通信事务是并行的,应用层来的通信任务会被放入队列
|
||||
*/
|
||||
|
||||
static void protm_send_next(void *t);
|
||||
int protm_send(protm_def *p,send_data_def *d);
|
||||
static void protm_send_timeout(void *t);
|
||||
static void protm_run(void *t)
|
||||
{
|
||||
protm_def *p=t;
|
||||
array_def *data;
|
||||
array_def *decode_data;
|
||||
uint32_t ev;
|
||||
while(p->run)
|
||||
{
|
||||
rt_event_recv(p->event,0xffffffff,RT_EVENT_FLAG_OR|RT_EVENT_FLAG_CLEAR,RT_WAITING_FOREVER,&ev);
|
||||
if(ev&EVENT_RECV)
|
||||
{
|
||||
// DBG_LOG("protmcu:recv event.");
|
||||
data=arr_duplicate(p->buff);
|
||||
arr_clear(p->buff);
|
||||
//DBG_LOG("recv:%s",str_temp(arr_string(data)));
|
||||
decode_data=protm_decode(p,data);
|
||||
arr_delete(data);
|
||||
emit protm_recv_signal(p,p->recv_src,p->recv_cmd,arr_temp(decode_data),str_temp(str_duplicate(p->str_err)));
|
||||
irq_disable();
|
||||
p->num_to_recv=0;
|
||||
irq_enable();
|
||||
protm_send_next(p);
|
||||
}
|
||||
else if(ev&EVENT_TIMEOUT)
|
||||
{
|
||||
protm_send_timeout(p);
|
||||
}
|
||||
if (ev&EVENT_SEND)
|
||||
{
|
||||
// DBG_LOG("protmcu:send event.");
|
||||
if(list_length(p->send_data)>0)
|
||||
{
|
||||
send_data_def *a=(send_data_def *)list_get(p->send_data,0);
|
||||
protm_send(p,a);
|
||||
}
|
||||
}
|
||||
ev=0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 发送下一个
|
||||
static void protm_send_next(void *t)
|
||||
{
|
||||
protm_def *p=t;
|
||||
rt_timer_stop(p->timer);
|
||||
list_remove(p->send_data,0);
|
||||
// 如果发送列表中有数据,此时发送
|
||||
if (list_length(p->send_data)>0)
|
||||
{
|
||||
// DBG_LOG("protmcu:EVENT_SEND.");
|
||||
rt_event_send(p->event,EVENT_SEND);
|
||||
}
|
||||
else{
|
||||
irq_disable();
|
||||
p->in_send=0;
|
||||
irq_enable();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void protm_send_timeout_cb(void *t)
|
||||
{
|
||||
protm_def *p=t;
|
||||
rt_event_send(p->event,EVENT_TIMEOUT);
|
||||
}
|
||||
|
||||
// 接收超时
|
||||
static void protm_send_timeout(void *t)
|
||||
{
|
||||
protm_def *p=t;
|
||||
send_data_def *s=list_get(p->send_data,0);
|
||||
protm_slave *sl=protm_get_slave(p,s->addr);
|
||||
if(sl->retry>0){
|
||||
sl->retry--;
|
||||
DBG_WARN("slave:%d retry left ",s->addr,sl->retry);
|
||||
list_shift(p->send_data);
|
||||
}else{
|
||||
DBG_WARN("slave:%d retry timeout,remove the send data.",s->addr);
|
||||
list_remove(p->send_data,0);
|
||||
}
|
||||
// 如果发送列表中有数据,此时发送
|
||||
if (list_length(p->send_data)>0)
|
||||
{
|
||||
// DBG_LOG("protmcu:EVENT_SEND.");
|
||||
rt_event_send(p->event,EVENT_SEND);
|
||||
}
|
||||
else{
|
||||
irq_disable();
|
||||
p->in_send=0;
|
||||
irq_enable();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// 发送队列删除条目
|
||||
static int _list_send_data_del(void *t)
|
||||
{
|
||||
send_data_def *a=t;
|
||||
arr_delete(a->data);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
protm_def *protm_creat(uart_def *uart,int *addrs,int num)
|
||||
{
|
||||
static int count=0;
|
||||
char name[20]={0};
|
||||
protm_def *p=calloc(1,sizeof(protm_def));
|
||||
param_check(p);
|
||||
p->uart=uart;
|
||||
sprintf(name,"protm_event#%d",count);
|
||||
p->event=rt_event_create(name,RT_IPC_FLAG_FIFO);
|
||||
p->buff=arr_creat();
|
||||
p->run=1;
|
||||
p->slaves=list_creat(sizeof(protm_slave),protm_slave_sub,protm_slave_del,0);
|
||||
p->slaves_addr=list_creat_int();
|
||||
p->send_data=list_creat(sizeof(send_data_def),0,_list_send_data_del,0);
|
||||
// 创建超时定时器,超过指定时间则不再等待从机返回
|
||||
p->timer=rt_timer_create("port_timer",protm_send_timeout_cb,p,
|
||||
rt_tick_from_millisecond(55),
|
||||
RT_TIMER_FLAG_ONE_SHOT|RT_TIMER_FLAG_SOFT_TIMER);
|
||||
list_appends(p->slaves_addr,addrs,num);
|
||||
sprintf(name,"protm_t#%d",count);
|
||||
rt_thread_t rt_t=rt_thread_create(name,protm_run,p,512,5,20);
|
||||
rt_thread_startup(rt_t);
|
||||
p->uart->init(p->uart);
|
||||
p->uart->set_irq(p->uart,recv_irq,p);
|
||||
|
||||
count++;
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
// 得到指定地址的从机,找不到则添加
|
||||
protm_slave *protm_get_slave(protm_def *p,uint8_t addr)
|
||||
{
|
||||
protm_slave *r=0;
|
||||
for(int i=0;i<list_length(p->slaves);i++)
|
||||
{
|
||||
r=list_get(p->slaves,i);
|
||||
if(r->addr==addr)
|
||||
return r;
|
||||
}
|
||||
r=protm_slave_creat();
|
||||
r->addr=addr;
|
||||
r->cmds=list_creat_int();
|
||||
r->no=0;
|
||||
list_append(p->slaves,r);
|
||||
free(r);
|
||||
return list_get(p->slaves,-1);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 设置所有从机流水号
|
||||
void protm_set_no_all(protm_def *p,uint16_t no,list_def *cmd/*int*/)
|
||||
{
|
||||
for(int i=0;i<list_length(p->slaves);i++)
|
||||
{
|
||||
protm_slave *s=list_get(p->slaves,i);
|
||||
s->no=no;
|
||||
list_clear(s->cmds);
|
||||
list_append_from(s->cmds,cmd);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 设置单个从机流水号
|
||||
void protm_set_no(protm_def *p,uint8_t addr,uint16_t no,list_def *cmd/*int*/)
|
||||
{
|
||||
protm_slave *s=protm_get_slave(p,addr);
|
||||
s->no=no;
|
||||
list_clear(s->cmds);
|
||||
list_append_from(s->cmds,cmd);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// 解码
|
||||
array_def *protm_decode(protm_def *p,array_def *data)
|
||||
{
|
||||
array_def *r=arr_creat();
|
||||
param_check(r);
|
||||
str_set(p->str_err,"ok");
|
||||
if(arr_length(data)<10)
|
||||
{
|
||||
DBG_WARN("recv data len too less.");
|
||||
str_set(p->str_err,"recv data len too less.");
|
||||
return r;
|
||||
}
|
||||
uint8_t src=arr_get(data,4);
|
||||
p->recv_src=src;
|
||||
uint16_t len=arr_get(data,2)|(arr_get(data,3)<<8);
|
||||
uint8_t crc=crc_crc8(arr_data(data),arr_length(data)-1);
|
||||
if(len!=arr_length(data))
|
||||
{
|
||||
// 如果长度不相等则产生了数据丢失
|
||||
DBG_WARN("recv data have lossed.");
|
||||
str_set(p->str_err,"recv data have lossed.");
|
||||
return r;
|
||||
}
|
||||
uint16_t no=arr_get(data,7)|(arr_get(data,8)<<8);
|
||||
uint16_t h_no=protm_get_slave(p,src)->no;
|
||||
if(no!=h_no)
|
||||
{
|
||||
// 发送一条指令等待其返回,此时流水号应相同
|
||||
//DBG_WARN("slave_addr=%d cmd_no error:h_no=%d,no=%d.",src,h_no,no);
|
||||
//str_set(p->str_err,"cmd no err.");
|
||||
//return r;
|
||||
}
|
||||
if(crc!=arr_get(data,-1))
|
||||
{
|
||||
DBG_WARN("recv data crc check error:%02x,%02x.",crc,arr_get(data,-1));
|
||||
str_set(p->str_err,"crc check err.");
|
||||
}
|
||||
p->recv_cmd=arr_get(data,6);
|
||||
list_def *cmds=protm_get_slave(p,src)->cmds;
|
||||
if(list_contains(cmds,(int []){p->recv_cmd})==0){
|
||||
// 命令号校验不对
|
||||
DBG_WARN("cmd check err.cmds=%s,recv_cmd=%d",tappend(list_string(cmds),0),p->recv_cmd);
|
||||
str_set(p->str_err,"cmd check err.");
|
||||
}
|
||||
arr_delete(r);
|
||||
return arr_mid(data,9,len-10);
|
||||
}
|
||||
|
||||
|
||||
// 编码
|
||||
array_def *protm_encode(protm_def *p,uint8_t dst,uint8_t cmd,list_def *comp_cmd/*int*/,array_def *data)
|
||||
{
|
||||
array_def *t=arr_creat();
|
||||
param_check(t);
|
||||
uint16_t len=arr_length(data)+10;
|
||||
protm_slave *slave=protm_get_slave(p,dst);
|
||||
slave->no++;
|
||||
protm_set_no(p,dst,slave->no,comp_cmd);
|
||||
arr_append(t,'Y');
|
||||
arr_append(t,'e');
|
||||
arr_append(t,len&0xff);
|
||||
arr_append(t,len>>8);
|
||||
arr_append(t,0);// 源地址
|
||||
arr_append(t,dst);// 目标地址
|
||||
arr_append(t,cmd);// 命令码
|
||||
arr_append(t,slave->no&0xff);
|
||||
arr_append(t,slave->no>>8);
|
||||
arr_appends_from(t,data);
|
||||
arr_append(t,crc_crc8(arr_data(t),arr_length(t)));
|
||||
return t;
|
||||
}
|
||||
|
||||
// 发送数据,发送前先开启超时定时器
|
||||
int protm_send(protm_def *p,send_data_def *d)
|
||||
{
|
||||
rt_tick_t tick=0;
|
||||
protm_slave *s=protm_get_slave(p,d->addr);
|
||||
//DBG_LOG("send:%s",str_temp(arr_string(d->data)));
|
||||
tick=rt_tick_from_millisecond(s->timeout_ms);
|
||||
rt_timer_control(p->timer,RT_TIMER_CTRL_SET_TIME,&tick);
|
||||
rt_timer_start(p->timer);
|
||||
if(d->data==0)
|
||||
{
|
||||
DBG_WARN("addr=%d,d->data=0",d->addr);
|
||||
return 0;
|
||||
}
|
||||
return p->uart->write(p->uart,arr_data(d->data),arr_length(d->data));
|
||||
}
|
||||
|
||||
|
||||
//// 槽函数,发送数据到指定地址
|
||||
//void protm_send_call(protm_def *p,list_def *addrs/*int*/,uint8_t cmd,list_def *comp_cmd/*int*/,array_def *data)
|
||||
//{
|
||||
// param_check(p);
|
||||
// for(int i=0;i<list_length(addrs);i++)
|
||||
// {
|
||||
// uint8_t dst=(uint8_t)list_get_int(addrs,i);
|
||||
// if(list_contains(p->slaves_addr,(int []){dst})==1)
|
||||
// {
|
||||
// array_def *t=0;
|
||||
// t=protm_encode(p,dst,cmd,comp_cmd,data);
|
||||
// protm_send(p,t);
|
||||
// arr_delete(t);
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
||||
|
||||
|
||||
// 槽函数,发送数据到指定地址
|
||||
void protm_send_call(protm_def *p,uint8_t addr,uint8_t cmd,list_def *comp_cmd/*int*/,array_def *data,int timeout_ms,int retry)
|
||||
{
|
||||
param_check(p);
|
||||
if(list_contains(p->slaves_addr,(int []){addr})==1)
|
||||
{
|
||||
array_def *t=0;
|
||||
t=protm_encode(p,addr,cmd,comp_cmd,data);
|
||||
//protm_send(p,t);
|
||||
//arr_delete(t);
|
||||
// 添加到发送队列,发送必须等到回应或超时才能发送下一个
|
||||
DBG_LOG("send to:%d",addr);
|
||||
send_data_def sd={0};
|
||||
sd.addr=addr;sd.data=t;
|
||||
protm_slave *s=protm_get_slave(p,addr);
|
||||
s->retry=retry;
|
||||
s->timeout_ms=timeout_ms;
|
||||
list_append(p->send_data,&sd);
|
||||
if(p->in_send==0){
|
||||
irq_disable();
|
||||
p->in_send=1;
|
||||
irq_enable();
|
||||
//DBG_LOG("send call");
|
||||
rt_event_send(p->event,EVENT_SEND);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// 返回1表示此地址可以发送到这个端口
|
||||
int protm_contains(protm_def *p,uint8_t addr)
|
||||
{
|
||||
param_check(p);
|
||||
return list_contains(p->slaves_addr,(int []){addr});
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@@ -1,22 +1,11 @@
|
||||
#include "stdlib.h"
|
||||
#include "signal.h"
|
||||
#include "commend.h"
|
||||
#include "handle.h"
|
||||
#include "handle_for_checker.h"
|
||||
#include "handle_for_coder.h"
|
||||
#include "input.h"
|
||||
#include "log.h"
|
||||
#include "moter.h"
|
||||
#include "process.h"
|
||||
#include "prot_mcu.h"
|
||||
#include "prot_uc.h"
|
||||
#include "tcp.h"
|
||||
#include "transmit.h"
|
||||
#include "tran_for_checker.h"
|
||||
#include "tran_for_coder.h"
|
||||
#include "tran_for_coder2.h"
|
||||
#include "tran_for_coder2ch.h"
|
||||
#include "udp.h"
|
||||
#include "tran_for_slave.h"
|
||||
|
||||
|
||||
|
||||
@@ -34,53 +23,6 @@ signal_export(cmd_reply_signal);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void port_send_signal(port_mcu *obj,uint8_t addr,uint8_t cmd,list_def *comp_cmd,array_def *data,int timeout_ms,int retry)
|
||||
{
|
||||
uint32_t param[6];
|
||||
param[0]=(uint32_t)addr;
|
||||
param[1]=(uint32_t)cmd;
|
||||
param[2]=(uint32_t)comp_cmd;
|
||||
param[3]=(uint32_t)data;
|
||||
param[4]=(uint32_t)timeout_ms;
|
||||
param[5]=(uint32_t)retry;
|
||||
_signal_emit(obj,port_send_signal,param,6);
|
||||
}
|
||||
signal_export(port_send_signal);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void port_end_signal(port_mcu *obj,port_mcu *src,void *data,int ack,char *err_str)
|
||||
{
|
||||
uint32_t param[4];
|
||||
param[0]=(uint32_t)src;
|
||||
param[1]=(uint32_t)data;
|
||||
param[2]=(uint32_t)ack;
|
||||
param[3]=(uint32_t)err_str;
|
||||
_signal_emit(obj,port_end_signal,param,4);
|
||||
}
|
||||
signal_export(port_end_signal);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void moter_end_signal(void *m)
|
||||
{
|
||||
_signal_emit(m,moter_end_signal,0,0);
|
||||
}
|
||||
signal_export(moter_end_signal);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -107,24 +49,6 @@ signal_export(protu_recv_signal);
|
||||
uint32_t param[4];
|
||||
param[0]=(uint32_t)codec_name;
|
||||
param[1]=(uint32_t)cmd;
|
||||
param[3]=(uint32_t)err_str;
|
||||
_signal_emit(obj,protm_recv_signal,param,4);
|
||||
}
|
||||
signal_export(protm_recv_signal);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void protu_recv_signal(void *obj,const char *codec_name,uint8_t cmd,array_def *data,char *err_str)
|
||||
{
|
||||
uint32_t param[4];
|
||||
param[0]=(uint32_t)codec_name;
|
||||
param[1]=(uint32_t)cmd;
|
||||
param[2]=(uint32_t)data;
|
||||
param[3]=(uint32_t)err_str;
|
||||
param[2]=(uint32_t)data;
|
||||
param[3]=(uint32_t)err_str;
|
||||
_signal_emit(obj,protu_recv_signal,param,4);
|
||||
@@ -146,28 +70,6 @@ signal_export(tran_send_signal);
|
||||
signal_export(tran_reply_signal);
|
||||
|
||||
|
||||
|
||||
void tcp_recv_signal(void *obj,array_def *data)
|
||||
{
|
||||
uint32_t param[1];
|
||||
param[0]=(uint32_t)data;
|
||||
_signal_emit(obj,tcp_recv_signal,param,1);
|
||||
}
|
||||
signal_export(tcp_recv_signal);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void tran_reply_signal(tran_def *p,array_def *data)
|
||||
{
|
||||
uint32_t param[1];
|
||||
param[0]=(uint32_t)data;
|
||||
_signal_emit(p,tran_reply_signal,param,1);
|
||||
}
|
||||
signal_export(tran_reply_signal);
|
||||
|
||||
|
||||
|
||||
@@ -187,16 +89,3 @@ signal_export(code2_start_signal);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void coder_live_send_signal(void *obj,uint8_t cmd,array_def *data)
|
||||
{
|
||||
uint32_t param[2];
|
||||
param[0]=(uint32_t)cmd;
|
||||
param[1]=(uint32_t)data;
|
||||
_signal_emit(obj,coder_live_send_signal,param,2);
|
||||
}
|
||||
signal_export(coder_live_send_signal);
|
||||
|
||||
|
||||
|
@@ -1,20 +1,16 @@
|
||||
#include "tran_for_coder2.h"
|
||||
#include "tran_for_coder2ch.h"
|
||||
#include "debug.h"
|
||||
#include "mymisc.h"
|
||||
#include "mystdlib.h"
|
||||
#include "board.h"
|
||||
#include "dev_flash.h"
|
||||
#include "tcp.h"
|
||||
#include "prot_uc.h"
|
||||
#include "handle_for_coder.h"
|
||||
#include "handle_for_checker.h"
|
||||
#include "coder_lib.h"
|
||||
#include "JQ_PSDGenerate.h"
|
||||
#include "mystring.h"
|
||||
#include "coder_judge.h"
|
||||
#include "JQ_UIDGenerate.h"
|
||||
|
||||
#include "transmit.h"
|
||||
|
||||
|
||||
|
||||
@@ -154,10 +150,10 @@ static uint32_t in_range_err(const uint8_t *src_data,const uint8_t *errbit)
|
||||
|
||||
|
||||
// 注码完成
|
||||
static void write_uid_end(ucport_def *u,port_mcu *src,void *data,int ack,char *err_str)
|
||||
static void write_uid_end(ucport_def *u,void *src,void *data,int ack,char *err_str)
|
||||
{
|
||||
write_uid_def *w=(write_uid_def *)u;
|
||||
uint8_t addr=port_get_addr(src);
|
||||
uint8_t addr=0;
|
||||
if(addr<=0||addr>w->addrs_num){
|
||||
DBG_WARN("addr err:%d",addr);
|
||||
return;
|
||||
@@ -263,17 +259,14 @@ static ucport_def *write_uid(tran_def *t, uint8_t cmd,array_def *data)
|
||||
u->ack[i*38+0]=i+slave_addr_off();
|
||||
u->ack[i*38+1]=1;
|
||||
if(1){
|
||||
port_mcu *mcu=tran_get_portm(u->u.p,i);
|
||||
// 这里打开赋码操作
|
||||
if(mcu){
|
||||
if(check_shell_code(u->item[i].shell_code)){
|
||||
port_start(mcu,code2_creat(8,4,u->item[i].uid_pw_hex));
|
||||
}else{
|
||||
// 管壳码无效,不注码,此时默认已ack
|
||||
// 管壳码无效不视为失败
|
||||
ret=0;
|
||||
write_uid_fillret(u,i+1,0xff);
|
||||
}
|
||||
if(check_shell_code(u->item[i].shell_code)){
|
||||
// 开始注码
|
||||
//port_start(mcu,code2_creat(8,4,u->item[i].uid_pw_hex));
|
||||
}else{
|
||||
// 管壳码无效,不注码,此时默认已ack
|
||||
// 管壳码无效不视为失败
|
||||
ret=0;
|
||||
write_uid_fillret(u,i+1,0xff);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -316,89 +309,6 @@ transmit_export(ym_checker,0x02,write_uid)
|
||||
|
||||
|
||||
|
||||
typedef struct{
|
||||
rt_timer_t timer;
|
||||
void *protu;
|
||||
}live_keeper_def;
|
||||
|
||||
|
||||
static void live_start(live_keeper_def *t)
|
||||
{
|
||||
rt_tick_t tick=0;
|
||||
tick=rt_tick_from_millisecond(1000);
|
||||
rt_timer_control(t->timer,RT_TIMER_CTRL_SET_TIME,&tick);
|
||||
rt_timer_start(t->timer);
|
||||
}
|
||||
static void live_recv(live_keeper_def *t)
|
||||
{
|
||||
rt_tick_t tick=0;
|
||||
tick=rt_tick_from_millisecond(5000);
|
||||
rt_timer_control(t->timer,RT_TIMER_CTRL_SET_TIME,&tick);
|
||||
rt_timer_start(t->timer);
|
||||
//rt_timer_stop(t->timer);
|
||||
}
|
||||
|
||||
|
||||
// cmd=0x8a
|
||||
static void live_send(void *p)
|
||||
{
|
||||
live_keeper_def *live=p;
|
||||
const sys_param_def *par=sys_param();
|
||||
tran_def *t=app_variable("tran",0,0);
|
||||
uint16_t slave_online=0;
|
||||
if(t){
|
||||
slave_online=tran_get_slave_online(t);
|
||||
}
|
||||
//protu_codec_set(live->protu,protu_find_codec("ym_checker"));
|
||||
|
||||
array_def *d=arr_creat();
|
||||
arr_append(d,coder_extract_chip(0));// 模块类型
|
||||
arr_append(d,0xff);
|
||||
arr_append(d,0x03);
|
||||
arr_append(d,par->local_id&0xff);
|
||||
arr_append(d,slave_online&0xff);// 在线的小板
|
||||
arr_append(d,(slave_online>>8)&0xff);
|
||||
emit coder2_live_send_signal(p,0x8a,arr_temp(d));
|
||||
//DBG_LOG("send liver data.");
|
||||
|
||||
}
|
||||
|
||||
|
||||
static void init_for_coder2ch(void *t)
|
||||
{
|
||||
void *protu=app_variable("protu",0,0);
|
||||
if(protu){
|
||||
live_keeper_def *live=calloc(1,sizeof(live_keeper_def));
|
||||
live->protu=protu;
|
||||
live->timer=rt_timer_create("live_t",live_send,live,
|
||||
rt_tick_from_millisecond(1000),
|
||||
RT_TIMER_FLAG_PERIODIC|RT_TIMER_FLAG_SOFT_TIMER);
|
||||
protu_codec_set(protu,protu_find_codec("ym_checker"));
|
||||
connect(protu,protu_recv_signal,0,live,live_recv);
|
||||
connect(live,coder2_live_send_signal,0,protu,protu_send_call);
|
||||
DBG_LOG("coder2ch liver created");
|
||||
live_start(live);
|
||||
}else{
|
||||
DBG_WARN("can not fond variable \"protu\"");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// 如果本机为赋码仪并且连接类型为tcp
|
||||
// 初始化心跳
|
||||
static int init_live_keeper(void)
|
||||
{
|
||||
const sys_param_def *par=sys_param();
|
||||
app_valid_call("protu",init_for_coder2ch,0);
|
||||
return 0;
|
||||
}
|
||||
app_init_export(init_live_keeper);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
34
source/task/tran_for_slave.c
Normal file
34
source/task/tran_for_slave.c
Normal file
@@ -0,0 +1,34 @@
|
||||
#include "tran_for_slave.h"
|
||||
#include "debug.h"
|
||||
#include "mymisc.h"
|
||||
#include "mystdlib.h"
|
||||
#include "board.h"
|
||||
#include "dev_flash.h"
|
||||
#include "prot_uc.h"
|
||||
#include "coder_lib.h"
|
||||
#include "JQ_PSDGenerate.h"
|
||||
#include "mystring.h"
|
||||
#include "coder_judge.h"
|
||||
#include "JQ_UIDGenerate.h"
|
||||
#include "transmit.h"
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
14
source/task/tran_for_slave.h
Normal file
14
source/task/tran_for_slave.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#ifndef tran_for_slave_h__
|
||||
#define tran_for_slave_h__
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#endif
|
@@ -20,49 +20,15 @@
|
||||
|
||||
|
||||
|
||||
#define PORT_NUM 2
|
||||
|
||||
|
||||
typedef struct _tran_def{
|
||||
ucport_def *u;
|
||||
port_mcu *port[PORT_NUM];
|
||||
uint32_t slave_online;// 对应位为1则从机在线
|
||||
int is_busy;
|
||||
}tran_def;
|
||||
|
||||
|
||||
|
||||
// 打印从机状态
|
||||
static void tran_print_slave_stat(void *t)
|
||||
{
|
||||
list_def *l=list_creat_int();
|
||||
uint32_t a=tran_get_slave_online(t);
|
||||
for(int i=0;i<PORT_NUM;i++)
|
||||
{
|
||||
if(a&(1<<i)){
|
||||
list_append_int(l,i+1);
|
||||
}
|
||||
}
|
||||
DBG_LOG("slave online stat:%s",str_temp(list_string(l)));
|
||||
//DBG_LOG("slave online stat:%08x",a);
|
||||
list_delete(l);
|
||||
}
|
||||
|
||||
|
||||
// 扫描从机
|
||||
static void tran_scan_slave(void *t)
|
||||
{
|
||||
for(int i=0;i<PORT_NUM;i++)
|
||||
{
|
||||
// 只给在线设备升级方案
|
||||
port_mcu *mcu=tran_get_portm(t,i);
|
||||
if(mcu&&(tran_get_slave_online(t)&(1<<i))){
|
||||
// f9用于获取运行状态,失败则设备不在线
|
||||
port_start(mcu,usercmd_creat(0xf9,arr_temp(arr_creat())));
|
||||
}
|
||||
}
|
||||
later_execute(tran_print_slave_stat,t,5500);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -71,27 +37,18 @@ static int tran_init(void)
|
||||
{
|
||||
sig_thread t=thread_creat(10);
|
||||
tran_def *tran= tran_creat(t);
|
||||
tran->slave_online=0xfffff;
|
||||
app_variable("tran",tran,0);
|
||||
later_execute(tran_scan_slave,tran,500);
|
||||
return 0;
|
||||
}
|
||||
app_init_export(tran_init);
|
||||
|
||||
|
||||
void tran_end_slot(tran_def *obj,port_mcu *src,void *data,int ack,char *err_str);
|
||||
void tran_end_slot(tran_def *obj,void *src,void *data,int ack,char *err_str);
|
||||
|
||||
tran_def *tran_creat(sig_thread t)
|
||||
{
|
||||
tran_def *tr=calloc(1,sizeof(tran_def));
|
||||
param_check(tr);
|
||||
port_init();
|
||||
for(int i=0;i<PORT_NUM;i++)
|
||||
{
|
||||
tr->port[i]=port_creat(i+1,t);
|
||||
// 连接操作结束信号
|
||||
connect(tr->port[i],port_end_signal,0,tr,tran_end_slot);
|
||||
}
|
||||
return tr;
|
||||
}
|
||||
|
||||
@@ -133,33 +90,16 @@ void tran_recv_slot(tran_def *t,const char *codec_name,uint8_t cmd,array_def *da
|
||||
}
|
||||
}
|
||||
|
||||
void tran_set_slave_online(tran_def *t,uint8_t addr,int stat)
|
||||
{
|
||||
param_check(t);
|
||||
uint32_t a=0;
|
||||
a=1<<(addr-1);
|
||||
if(stat)
|
||||
t->slave_online|=a;
|
||||
else{
|
||||
t->slave_online&=(~a);
|
||||
//DBG_LOG("a=%08x",a);
|
||||
}
|
||||
}
|
||||
uint32_t tran_get_slave_online(tran_def *t)
|
||||
{
|
||||
param_check(t);
|
||||
return t->slave_online;
|
||||
}
|
||||
|
||||
void tran_end_slot(tran_def *obj,port_mcu *src,void *data,int ack,char *err_str)
|
||||
|
||||
|
||||
void tran_end_slot(tran_def *obj,void *src,void *data,int ack,char *err_str)
|
||||
{
|
||||
//DBG_LOG("tran end slot");
|
||||
param_check(obj);
|
||||
if(strcmp(err_str,"timeout")==0)
|
||||
{
|
||||
tran_set_slave_online(obj,port_get_addr(src),0);
|
||||
}else{
|
||||
tran_set_slave_online(obj,port_get_addr(src),1);
|
||||
}
|
||||
if((obj->u)&&obj->u->doend)
|
||||
obj->u->doend(obj->u,src,data,ack,err_str);
|
||||
@@ -175,13 +115,7 @@ int tran_get_busy(tran_def *t)
|
||||
}
|
||||
|
||||
|
||||
// 获取指定通道
|
||||
port_mcu *tran_get_portm(tran_def *t,int index)
|
||||
{
|
||||
if(index<0||index>=PORT_NUM)
|
||||
return 0;
|
||||
return t->port[index];
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -374,13 +308,6 @@ static void when_scheme_done(ucport_def *u)
|
||||
DBG_LOG("memused:%d",mem_perused());
|
||||
rt_thread_mdelay(100);
|
||||
DBG_LOG("memused:%d",mem_perused());
|
||||
for(int i=0;i<PORT_NUM;i++)
|
||||
{
|
||||
// 只给在线设备升级方案
|
||||
port_mcu *mcu=tran_get_portm(tran,i);
|
||||
if(mcu&&(tran_get_slave_online(tran)&(1<<i)))
|
||||
port_start(mcu,updata_scheme_creat(check_scheme()->slave_data,2048));
|
||||
}
|
||||
}
|
||||
static ucport_def *tran_scheme(tran_def *t, uint8_t cmd,array_def *data)
|
||||
{
|
||||
|
@@ -2,9 +2,9 @@
|
||||
#define transmit_h__
|
||||
|
||||
#include "rtthread.h"
|
||||
#include "handle.h"
|
||||
#include "signal.h"
|
||||
|
||||
#include "stdint.h"
|
||||
#include "bytearray.h"
|
||||
|
||||
|
||||
// 错误码定义
|
||||
@@ -25,7 +25,7 @@ typedef struct _ucport_def{
|
||||
tran_def *p;
|
||||
int (*dolater)(struct _ucport_def *u,uint8_t cmd,array_def *data,char *err_str);
|
||||
void (*del)(struct _ucport_def *h);
|
||||
void (*doend)(struct _ucport_def *h,port_mcu *src,void *data,int ack,char *err_str);
|
||||
void (*doend)(struct _ucport_def *h,void *src,void *data,int ack,char *err_str);
|
||||
}ucport_def;
|
||||
|
||||
|
||||
@@ -57,8 +57,6 @@ void tran_recv_slot(tran_def *t,const char *codec_name,uint8_t cmd,array_def *da
|
||||
|
||||
void tran_set_busy(tran_def *t,int busy);
|
||||
int tran_get_busy(tran_def *t);
|
||||
port_mcu *tran_get_portm(tran_def *t,int index);
|
||||
uint32_t tran_get_slave_online(tran_def *t);
|
||||
|
||||
|
||||
signal tran_reply_signal(tran_def *p,array_def *data);
|
||||
|
Reference in New Issue
Block a user