收到广播命令时重新初始化定时器,can总线添加发送时长超时判断
This commit is contained in:
@@ -324,3 +324,5 @@
|
||||
V2.11 读流水号时如果目标值为0则不报错
|
||||
2021.01.04
|
||||
V2.12 sn为0时如果未写流水号则报错
|
||||
2024.01.05
|
||||
收到广播命令时重新初始化定时器,can总线添加发送时长超时判断,超时后自动重发
|
||||
|
@@ -265,7 +265,8 @@ array_def *protm_decode(protu_def *p,array_def *data)
|
||||
int dst_addrs=arr_get(r,0)|(arr_get(r,1)<<8)|(arr_get(r,2)<<16);
|
||||
if((dst_addrs&(1<<(self_addr-1)))!=0){
|
||||
p->silent=1;
|
||||
p->timer->write(p->timer,0);
|
||||
// p->timer->write(p->timer,0);
|
||||
p->timer->init(p->timer);
|
||||
p->rank=calc_bit_num(dst_addrs,self_addr-1);
|
||||
p->num=calc_bit_num(dst_addrs,32);
|
||||
arr_remove(r,0,3);
|
||||
|
@@ -135,13 +135,20 @@ static uint32_t GetCanFilter(YeCanID_un yeid)
|
||||
@param 发送数据长度
|
||||
@rtv 0 成功 1 失败
|
||||
*/
|
||||
uint8_t YeCan_SendFrame(uint8_t srcaddr,uint8_t dstaddr,const uint8_t* txdata, uint16_t len)
|
||||
timer_def *timer=0;
|
||||
uint8_t YeCan_SendFrame(uint8_t srcaddr,uint8_t dstaddr,const uint8_t* txdata, uint16_t len,int delay_ms)
|
||||
{
|
||||
CanTxMsg transmit_message;
|
||||
uint8_t mailbox_number;
|
||||
YeCanID_un yeid_un;
|
||||
uint32_t time_out = 0;
|
||||
int time_out = delay_ms*1000;
|
||||
uint8_t uc_rtv = 0;
|
||||
uint32_t tick_start=0;
|
||||
if(timer==0){
|
||||
timer=dev_get("timer");
|
||||
}
|
||||
tick_start=timer->read(timer);
|
||||
|
||||
//can_transmission_stop(CAN1,0);
|
||||
/* initialize transmit message */
|
||||
transmit_message.ExtId = 0x00;
|
||||
@@ -155,19 +162,6 @@ uint8_t YeCan_SendFrame(uint8_t srcaddr,uint8_t dstaddr,const uint8_t* txdata, u
|
||||
yeid_un.yecanid.FunClass = 1;
|
||||
yeid_un.yecanid.SegFlag = 0;
|
||||
yeid_un.yecanid.SegNum = 0;
|
||||
if(srcaddr == 0x00 || srcaddr == 0x1f)
|
||||
{
|
||||
time_out = 0;
|
||||
while(time_out < 5)
|
||||
{
|
||||
elec_led1_power(0);
|
||||
rt_thread_mdelay(50);
|
||||
elec_led1_power(1);
|
||||
rt_thread_mdelay(50);
|
||||
time_out++;
|
||||
|
||||
}
|
||||
}
|
||||
if(can_tx_mutex != RT_NULL)
|
||||
{
|
||||
if(RT_EOK != rt_mutex_take (can_tx_mutex,500))
|
||||
@@ -182,13 +176,13 @@ uint8_t YeCan_SendFrame(uint8_t srcaddr,uint8_t dstaddr,const uint8_t* txdata, u
|
||||
memcpy(transmit_message.Data,txdata,8);
|
||||
|
||||
mailbox_number = CAN_Transmit(CAN1, &transmit_message);
|
||||
time_out = 5000;
|
||||
while( CAN_TxStatus_Ok != CAN_TransmitStatus(CAN1,mailbox_number) && time_out > 0)
|
||||
{
|
||||
time_out --;
|
||||
time_out -=20;
|
||||
rt_hw_us_delay(20);
|
||||
}
|
||||
if(time_out == 0)
|
||||
// if(time_out <= 0)
|
||||
if((timer->read(timer)-tick_start)>=delay_ms)
|
||||
{
|
||||
uc_rtv = 1;
|
||||
goto can0_tx_end;
|
||||
@@ -214,10 +208,11 @@ uint8_t YeCan_SendFrame(uint8_t srcaddr,uint8_t dstaddr,const uint8_t* txdata, u
|
||||
time_out = 5000;
|
||||
while( CAN_TxStatus_Ok != CAN_TransmitStatus(CAN1,mailbox_number) && time_out > 0)
|
||||
{
|
||||
time_out --;
|
||||
time_out -=20;
|
||||
rt_hw_us_delay(20);
|
||||
}
|
||||
if(time_out == 0)
|
||||
// if(time_out <= 0)
|
||||
if((timer->read(timer)-tick_start)>=delay_ms)
|
||||
{
|
||||
uc_rtv = 1;
|
||||
goto can0_tx_end;
|
||||
@@ -229,16 +224,15 @@ uint8_t YeCan_SendFrame(uint8_t srcaddr,uint8_t dstaddr,const uint8_t* txdata, u
|
||||
transmit_message.DLC = len;
|
||||
transmit_message.ExtId = yeid_un.Exide;
|
||||
mailbox_number = CAN_Transmit(CAN1, &transmit_message);
|
||||
time_out = 5000;
|
||||
while(CAN_TxStatus_Ok != CAN_TransmitStatus(CAN1,mailbox_number) && time_out > 0)
|
||||
{
|
||||
|
||||
time_out --;
|
||||
time_out -=20;
|
||||
rt_hw_us_delay(20);
|
||||
}
|
||||
if(time_out == 0)
|
||||
// if(time_out <= 0)
|
||||
if((timer->read(timer)-tick_start)>=delay_ms)
|
||||
{
|
||||
|
||||
uc_rtv = 1;
|
||||
}
|
||||
|
||||
@@ -345,12 +339,12 @@ static int read(uart_def *u,uint8_t *b,int len)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
static int write(uart_def *u,const uint8_t *b,int len)
|
||||
static int write(uart_def *u,const uint8_t *b,int len,int timeout_ms)
|
||||
{
|
||||
uint8_t src=elec_local_addr();
|
||||
uint8_t dst=0;
|
||||
uint8_t ret;
|
||||
ret=YeCan_SendFrame(src,dst,b,len);
|
||||
ret=YeCan_SendFrame(src,dst,b,len,timeout_ms);
|
||||
if(ret==0) return len;
|
||||
else return 0;
|
||||
}
|
||||
@@ -427,11 +421,11 @@ static int h_read(uart_def *u,uint8_t *b,int len)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
static int h_write(uart_def *u,const uint8_t *b,int len)
|
||||
static int h_write(uart_def *u,const uint8_t *b,int len,int timeout_ms)
|
||||
{
|
||||
h_self_def *h=&g_hself;
|
||||
uart_def *s=h->dev[h->dev_index];
|
||||
return s->write(s,b,len);
|
||||
return s->write(s,b,len,timeout_ms);
|
||||
}
|
||||
|
||||
|
||||
|
@@ -45,6 +45,6 @@ void YeCanInit(void);
|
||||
@param 发送数据长度
|
||||
@rtv 0 成功 1 失败
|
||||
*/
|
||||
uint8_t YeCan_SendFrame(uint8_t srcaddr,uint8_t dstaddr,const uint8_t* txdata, uint16_t len);
|
||||
uint8_t YeCan_SendFrame(uint8_t srcaddr,uint8_t dstaddr,const uint8_t* txdata, uint16_t len,int delay_ms);
|
||||
|
||||
#endif
|
||||
|
@@ -8,7 +8,7 @@
|
||||
|
||||
static int init(timer_def *t){
|
||||
static int inited=0;
|
||||
if(inited!=0) return 0;
|
||||
// if(inited!=0) return 0;
|
||||
inited=1;
|
||||
TIM_TimeBaseInitTypeDef timer;
|
||||
|
||||
@@ -58,10 +58,20 @@ static uint32_t read(timer_def *t)
|
||||
}
|
||||
static int write(timer_def *t,const uint32_t value)
|
||||
{
|
||||
uint32_t v;
|
||||
TIM_Cmd(TIM4,DISABLE);
|
||||
TIM_SetCounter(TIM4,(value%1000)*10);
|
||||
TIM_SetCounter(TIM5,value/1000);
|
||||
TIM_Cmd(TIM5,DISABLE);
|
||||
|
||||
v=(value%1000)*10;
|
||||
TIM4->CNT = v;
|
||||
TIM4->EGR |= TIM_EventSource_Update;
|
||||
|
||||
v=value/1000;
|
||||
TIM5->CNT = v;
|
||||
TIM5->EGR |= TIM_EventSource_Update;
|
||||
|
||||
TIM_Cmd(TIM4,ENABLE);
|
||||
TIM_Cmd(TIM5,ENABLE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@@ -361,7 +361,7 @@ static int dma_tx_reset(self_data *self,uint8_t *tx_buff,uint32_t tx_buff_size)
|
||||
|
||||
|
||||
// dma后台发送需要保证数据在发送期间有效
|
||||
static int write(uart_def *u,const uint8_t *b,int len)
|
||||
static int write(uart_def *u,const uint8_t *b,int len,int timeout_ms)
|
||||
{
|
||||
param_check(u);
|
||||
param_check(u->private_data);
|
||||
@@ -380,7 +380,7 @@ static int write(uart_def *u,const uint8_t *b,int len)
|
||||
}
|
||||
|
||||
// 阻塞发送,此函数结束即可释放内存
|
||||
static int write_block(uart_def *u,const uint8_t *b,int len)
|
||||
static int write_block(uart_def *u,const uint8_t *b,int len,int timeout_ms)
|
||||
{
|
||||
param_check(u);
|
||||
param_check(u->private_data);
|
||||
|
@@ -6,7 +6,7 @@
|
||||
|
||||
|
||||
|
||||
#define BUILD_DATE "2024-01-04 14:39:32"
|
||||
#define BUILD_DATE "2024-01-05 18:59:06"
|
||||
#define SOFT_VERSION "2.12"
|
||||
|
||||
|
||||
|
@@ -58,8 +58,6 @@ int main()
|
||||
connect(protu2,protu_recv_signal,0,cmd,cmd_recv_slot);
|
||||
connect(cmd,cmd_reply_signal,0,protu2,protu_send_call);
|
||||
|
||||
timer_def *timer=dev_get("timer");
|
||||
timer->init(timer);
|
||||
// rt_thread_mdelay(5000);
|
||||
// timer->write(timer,0);
|
||||
while(1)
|
||||
|
@@ -22,7 +22,7 @@ typedef struct __uart_def{
|
||||
int (*set_end_irq)(struct __uart_def *u,uint8_t *rx_buff,int rx_buff_size,
|
||||
void (*irq)(void *t,uint32_t len),void *t);
|
||||
int (*read)(struct __uart_def *u,uint8_t *b,int len);
|
||||
int (*write)(struct __uart_def *u,const uint8_t *b,int len);
|
||||
int (*write)(struct __uart_def *u,const uint8_t *b,int len,int timeout_ms);
|
||||
void *private_data;
|
||||
}uart_def;
|
||||
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user