收到广播命令时重新初始化定时器,can总线添加发送时长超时判断
This commit is contained in:
@@ -813,7 +813,7 @@
|
|||||||
|
|
||||||
<Group>
|
<Group>
|
||||||
<GroupName>stm32lib</GroupName>
|
<GroupName>stm32lib</GroupName>
|
||||||
<tvExp>0</tvExp>
|
<tvExp>1</tvExp>
|
||||||
<tvExpOptDlg>0</tvExpOptDlg>
|
<tvExpOptDlg>0</tvExpOptDlg>
|
||||||
<cbSel>0</cbSel>
|
<cbSel>0</cbSel>
|
||||||
<RteFlg>0</RteFlg>
|
<RteFlg>0</RteFlg>
|
||||||
@@ -2097,7 +2097,7 @@
|
|||||||
|
|
||||||
<Group>
|
<Group>
|
||||||
<GroupName>coder</GroupName>
|
<GroupName>coder</GroupName>
|
||||||
<tvExp>1</tvExp>
|
<tvExp>0</tvExp>
|
||||||
<tvExpOptDlg>0</tvExpOptDlg>
|
<tvExpOptDlg>0</tvExpOptDlg>
|
||||||
<cbSel>0</cbSel>
|
<cbSel>0</cbSel>
|
||||||
<RteFlg>0</RteFlg>
|
<RteFlg>0</RteFlg>
|
||||||
|
@@ -324,3 +324,5 @@
|
|||||||
V2.11 读流水号时如果目标值为0则不报错
|
V2.11 读流水号时如果目标值为0则不报错
|
||||||
2021.01.04
|
2021.01.04
|
||||||
V2.12 sn为0时如果未写流水号则报错
|
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);
|
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){
|
if((dst_addrs&(1<<(self_addr-1)))!=0){
|
||||||
p->silent=1;
|
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->rank=calc_bit_num(dst_addrs,self_addr-1);
|
||||||
p->num=calc_bit_num(dst_addrs,32);
|
p->num=calc_bit_num(dst_addrs,32);
|
||||||
arr_remove(r,0,3);
|
arr_remove(r,0,3);
|
||||||
|
@@ -135,13 +135,20 @@ static uint32_t GetCanFilter(YeCanID_un yeid)
|
|||||||
@param 发送数据长度
|
@param 发送数据长度
|
||||||
@rtv 0 成功 1 失败
|
@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;
|
CanTxMsg transmit_message;
|
||||||
uint8_t mailbox_number;
|
uint8_t mailbox_number;
|
||||||
YeCanID_un yeid_un;
|
YeCanID_un yeid_un;
|
||||||
uint32_t time_out = 0;
|
int time_out = delay_ms*1000;
|
||||||
uint8_t uc_rtv = 0;
|
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);
|
//can_transmission_stop(CAN1,0);
|
||||||
/* initialize transmit message */
|
/* initialize transmit message */
|
||||||
transmit_message.ExtId = 0x00;
|
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.FunClass = 1;
|
||||||
yeid_un.yecanid.SegFlag = 0;
|
yeid_un.yecanid.SegFlag = 0;
|
||||||
yeid_un.yecanid.SegNum = 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(can_tx_mutex != RT_NULL)
|
||||||
{
|
{
|
||||||
if(RT_EOK != rt_mutex_take (can_tx_mutex,500))
|
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);
|
memcpy(transmit_message.Data,txdata,8);
|
||||||
|
|
||||||
mailbox_number = CAN_Transmit(CAN1, &transmit_message);
|
mailbox_number = CAN_Transmit(CAN1, &transmit_message);
|
||||||
time_out = 5000;
|
|
||||||
while( CAN_TxStatus_Ok != CAN_TransmitStatus(CAN1,mailbox_number) && time_out > 0)
|
while( CAN_TxStatus_Ok != CAN_TransmitStatus(CAN1,mailbox_number) && time_out > 0)
|
||||||
{
|
{
|
||||||
time_out --;
|
time_out -=20;
|
||||||
rt_hw_us_delay(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;
|
uc_rtv = 1;
|
||||||
goto can0_tx_end;
|
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;
|
time_out = 5000;
|
||||||
while( CAN_TxStatus_Ok != CAN_TransmitStatus(CAN1,mailbox_number) && time_out > 0)
|
while( CAN_TxStatus_Ok != CAN_TransmitStatus(CAN1,mailbox_number) && time_out > 0)
|
||||||
{
|
{
|
||||||
time_out --;
|
time_out -=20;
|
||||||
rt_hw_us_delay(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;
|
uc_rtv = 1;
|
||||||
goto can0_tx_end;
|
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.DLC = len;
|
||||||
transmit_message.ExtId = yeid_un.Exide;
|
transmit_message.ExtId = yeid_un.Exide;
|
||||||
mailbox_number = CAN_Transmit(CAN1, &transmit_message);
|
mailbox_number = CAN_Transmit(CAN1, &transmit_message);
|
||||||
time_out = 5000;
|
|
||||||
while(CAN_TxStatus_Ok != CAN_TransmitStatus(CAN1,mailbox_number) && time_out > 0)
|
while(CAN_TxStatus_Ok != CAN_TransmitStatus(CAN1,mailbox_number) && time_out > 0)
|
||||||
{
|
{
|
||||||
|
|
||||||
time_out --;
|
time_out -=20;
|
||||||
rt_hw_us_delay(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;
|
uc_rtv = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -345,12 +339,12 @@ static int read(uart_def *u,uint8_t *b,int len)
|
|||||||
{
|
{
|
||||||
return 0;
|
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 src=elec_local_addr();
|
||||||
uint8_t dst=0;
|
uint8_t dst=0;
|
||||||
uint8_t ret;
|
uint8_t ret;
|
||||||
ret=YeCan_SendFrame(src,dst,b,len);
|
ret=YeCan_SendFrame(src,dst,b,len,timeout_ms);
|
||||||
if(ret==0) return len;
|
if(ret==0) return len;
|
||||||
else return 0;
|
else return 0;
|
||||||
}
|
}
|
||||||
@@ -427,11 +421,11 @@ static int h_read(uart_def *u,uint8_t *b,int len)
|
|||||||
{
|
{
|
||||||
return 0;
|
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;
|
h_self_def *h=&g_hself;
|
||||||
uart_def *s=h->dev[h->dev_index];
|
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 发送数据长度
|
@param 发送数据长度
|
||||||
@rtv 0 成功 1 失败
|
@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
|
#endif
|
||||||
|
@@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
static int init(timer_def *t){
|
static int init(timer_def *t){
|
||||||
static int inited=0;
|
static int inited=0;
|
||||||
if(inited!=0) return 0;
|
// if(inited!=0) return 0;
|
||||||
inited=1;
|
inited=1;
|
||||||
TIM_TimeBaseInitTypeDef timer;
|
TIM_TimeBaseInitTypeDef timer;
|
||||||
|
|
||||||
@@ -58,10 +58,20 @@ static uint32_t read(timer_def *t)
|
|||||||
}
|
}
|
||||||
static int write(timer_def *t,const uint32_t value)
|
static int write(timer_def *t,const uint32_t value)
|
||||||
{
|
{
|
||||||
|
uint32_t v;
|
||||||
TIM_Cmd(TIM4,DISABLE);
|
TIM_Cmd(TIM4,DISABLE);
|
||||||
TIM_SetCounter(TIM4,(value%1000)*10);
|
TIM_Cmd(TIM5,DISABLE);
|
||||||
TIM_SetCounter(TIM5,value/1000);
|
|
||||||
|
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(TIM4,ENABLE);
|
||||||
|
TIM_Cmd(TIM5,ENABLE);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -361,7 +361,7 @@ static int dma_tx_reset(self_data *self,uint8_t *tx_buff,uint32_t tx_buff_size)
|
|||||||
|
|
||||||
|
|
||||||
// dma后台发送需要保证数据在发送期间有效
|
// 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);
|
||||||
param_check(u->private_data);
|
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);
|
||||||
param_check(u->private_data);
|
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"
|
#define SOFT_VERSION "2.12"
|
||||||
|
|
||||||
|
|
||||||
|
@@ -58,8 +58,6 @@ int main()
|
|||||||
connect(protu2,protu_recv_signal,0,cmd,cmd_recv_slot);
|
connect(protu2,protu_recv_signal,0,cmd,cmd_recv_slot);
|
||||||
connect(cmd,cmd_reply_signal,0,protu2,protu_send_call);
|
connect(cmd,cmd_reply_signal,0,protu2,protu_send_call);
|
||||||
|
|
||||||
timer_def *timer=dev_get("timer");
|
|
||||||
timer->init(timer);
|
|
||||||
// rt_thread_mdelay(5000);
|
// rt_thread_mdelay(5000);
|
||||||
// timer->write(timer,0);
|
// timer->write(timer,0);
|
||||||
while(1)
|
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,
|
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);
|
void (*irq)(void *t,uint32_t len),void *t);
|
||||||
int (*read)(struct __uart_def *u,uint8_t *b,int len);
|
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;
|
void *private_data;
|
||||||
}uart_def;
|
}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)));
|
//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)
|
static void protu_send_later(void *ptr)
|
||||||
{
|
{
|
||||||
send_pkt_def *s=ptr;
|
send_pkt_def *s=ptr;
|
||||||
protu_send(s->p,s->t);
|
protu_send(s->p,s->t,50);
|
||||||
arr_delete(s->t);
|
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);
|
uint32_t tick=p->timer->read(p->timer);
|
||||||
// 根据返回的数据长度计算发送需要的时间,添加1ms的余量
|
// 根据返回的数据长度计算发送需要的时间,添加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 delay=tick%(wnd_tick*(p->num));
|
||||||
int gap=p->rank*wnd_tick;
|
int gap=p->rank*wnd_tick;
|
||||||
if(delay<=gap){
|
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);
|
//later_execute(protu_send_later,s,delay);
|
||||||
while(p->timer->read(p->timer)<(tick+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{
|
}else{
|
||||||
// 单播命令直接发送
|
// 单播命令直接发送
|
||||||
protu_send_later(s);
|
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)
|
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){
|
if(t){
|
||||||
g_send_pkt.p=p;
|
g_send_pkt.p=p;
|
||||||
g_send_pkt.t=t;
|
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){
|
if(t){
|
||||||
g_send_pkt.p=p;
|
g_send_pkt.p=p;
|
||||||
g_send_pkt.t=t;
|
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