使用定时器来精确控制数据发送的时间间隙,保证和其他小板不冲突
This commit is contained in:
@@ -11,7 +11,8 @@
|
||||
#include "debug.h"
|
||||
#include "crc.h"
|
||||
#include "dev_flash.h"
|
||||
|
||||
#include "mymisc.h"
|
||||
#include "elec_det.h"
|
||||
|
||||
|
||||
|
||||
@@ -259,7 +260,8 @@ protu_def *protu_creat(uart_def *uart)
|
||||
p->uart->init(p->uart,0);
|
||||
//p->uart->set_irq(p->uart,recv_irq,p);
|
||||
p->uart->set_end_irq(p->uart,p->buff,RECV_BUFF_SIZE,recv_end_irq,p);
|
||||
|
||||
p->timer=dev_get("timer");
|
||||
p->timer->init(p->timer);
|
||||
count++;
|
||||
return p;
|
||||
}
|
||||
@@ -275,33 +277,63 @@ int protu_send(protu_def *p,array_def *data)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
typedef struct{
|
||||
protu_def *p;
|
||||
array_def *t;
|
||||
}send_pkt_def;
|
||||
|
||||
static void protu_send_later(void *ptr)
|
||||
{
|
||||
send_pkt_def *s=ptr;
|
||||
protu_send(s->p,s->t);
|
||||
arr_delete(s->t);
|
||||
}
|
||||
|
||||
static send_pkt_def g_send_pkt;
|
||||
|
||||
|
||||
|
||||
// 在指定时间间隙中发送
|
||||
static void protu_send_ontime(protu_def *p,send_pkt_def *s)
|
||||
{
|
||||
uint32_t tick=p->timer->read(p->timer);
|
||||
int delay=tick%100;
|
||||
int gap=5*(elec_local_addr()-1);
|
||||
if(delay<=gap){
|
||||
delay=gap-delay;
|
||||
}else{
|
||||
delay=gap+100-delay;
|
||||
}
|
||||
later_execute(protu_send_later,s,delay);
|
||||
}
|
||||
|
||||
|
||||
// 槽函数,回复上位机
|
||||
void protu_reply_call(protu_def *p,array_def *data)
|
||||
{
|
||||
param_check(p);
|
||||
array_def *t=0;
|
||||
// if(p->silent)
|
||||
// return;
|
||||
if(p->codec)
|
||||
t=p->codec->encode(p,data);
|
||||
// DBG_LOG("send encode:%s",arr_string(t));
|
||||
if(t){
|
||||
protu_send(p,t);
|
||||
arr_delete(t);
|
||||
g_send_pkt.p=p;
|
||||
g_send_pkt.t=t;
|
||||
protu_send_ontime(p,&g_send_pkt);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// 槽函数,主动上发
|
||||
void protu_send_call(protu_def *p,uint8_t cmd,array_def *data)
|
||||
{
|
||||
array_def *t=0;
|
||||
// if(p->silent)
|
||||
// return;
|
||||
if(cmd!=0){
|
||||
p->cmd=cmd;
|
||||
// 上报数据不增加流水号 2023.7.14
|
||||
//p->cmd_no++;
|
||||
if(p->codec)
|
||||
t=p->codec->encode(p,data);
|
||||
}else if(p->cmd==0)
|
||||
@@ -310,8 +342,9 @@ void protu_send_call(protu_def *p,uint8_t cmd,array_def *data)
|
||||
t=protu_encode_str(p,data);
|
||||
}
|
||||
if(t){
|
||||
protu_send(p,t);
|
||||
arr_delete(t);
|
||||
g_send_pkt.p=p;
|
||||
g_send_pkt.t=t;
|
||||
protu_send_ontime(p,&g_send_pkt);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user