收到广播命令时重新初始化定时器,can总线添加发送时长超时判断

This commit is contained in:
ranchuan
2024-01-06 09:54:54 +08:00
parent 78e0ebd0c2
commit b9224f2293
11 changed files with 70 additions and 49 deletions

View File

@@ -270,10 +270,10 @@ protu_def *protu_creat(uart_def *uart)
int protu_send(protu_def *p,array_def *data)
int protu_send(protu_def *p,array_def *data,int timeout_ms)
{
//DBG_LOG("send:%s",str_temp(arr_string(data)));
return p->uart->write(p->uart,arr_data(data),arr_length(data));
return p->uart->write(p->uart,arr_data(data),arr_length(data),timeout_ms);
}
@@ -287,7 +287,7 @@ array_def *t;
static void protu_send_later(void *ptr)
{
send_pkt_def *s=ptr;
protu_send(s->p,s->t);
protu_send(s->p,s->t,50);
arr_delete(s->t);
}
@@ -296,12 +296,16 @@ static send_pkt_def g_send_pkt;
// 在指定时间间隙中发送
static void protu_send_ontime(protu_def *p,send_pkt_def *s)
// 返回0失败
static int protu_send_ontime(protu_def *p,send_pkt_def *s)
{
uint32_t tick=p->timer->read(p->timer);
// 根据返回的数据长度计算发送需要的时间,添加1ms的余量
// 根据协议,每个指令从机的返回数据长度等长,所以需要的时间窗口也相等
int wnd_tick=((arr_length(s->t)+12)/13+2);
// 253字节需要23ms
// 每ms传输的字节数为253/23=11,
// 考虑can总线自动重传保留1/3的余量
int wnd_tick=((arr_length(s->t)+4)/5+2);
int delay=tick%(wnd_tick*(p->num));
int gap=p->rank*wnd_tick;
if(delay<=gap){
@@ -313,14 +317,26 @@ static void protu_send_ontime(protu_def *p,send_pkt_def *s)
// 广播命令在指定时间窗口发送
//later_execute(protu_send_later,s,delay);
while(p->timer->read(p->timer)<(tick+delay));
protu_send_later(s);
// protu_send_later(s);
return protu_send(s->p,s->t,wnd_tick-2);
}else{
// 单播命令直接发送
protu_send_later(s);
return arr_length(s->t);
}
}
static int protu_send_ontime_loop(protu_def *p,send_pkt_def *s)
{
while(protu_send_ontime(p,s)==0);
return 0;
}
// 槽函数,回复上位机
void protu_reply_call(protu_def *p,array_def *data)
{
@@ -332,7 +348,7 @@ void protu_reply_call(protu_def *p,array_def *data)
if(t){
g_send_pkt.p=p;
g_send_pkt.t=t;
protu_send_ontime(p,&g_send_pkt);
protu_send_ontime_loop(p,&g_send_pkt);
}
}
@@ -355,7 +371,7 @@ void protu_send_call(protu_def *p,uint8_t cmd,array_def *data)
if(t){
g_send_pkt.p=p;
g_send_pkt.t=t;
protu_send_ontime(p,&g_send_pkt);
protu_send_ontime_loop(p,&g_send_pkt);
}
}