Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 39 additions & 28 deletions components/drivers/can/dev_can.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ rt_inline int _can_int_tx(struct rt_can_device *can, const struct rt_can_msg *da
}
else
{
err_ret:
err_ret:
level = rt_hw_local_irq_disable();
can->status.dropedsndpkg++;
rt_hw_local_irq_enable(level);
Expand Down Expand Up @@ -345,34 +345,47 @@ static rt_ssize_t _can_nonblocking_tx(struct rt_can_device *can, const struct rt
{
return -RT_EINVAL;
}

level = rt_hw_local_irq_disable();
Comment thread
meng-plus marked this conversation as resolved.
Resolved
while (sent_size < size)
{
if (can->ops->sendmsg_nonblocking(can, pmsg) == RT_EOK)
if (rt_ringbuffer_data_len(&can->nb_tx_rb) >= sizeof(struct rt_can_msg))
{
pmsg++;
sent_size += sizeof(struct rt_can_msg);
continue;
if (rt_ringbuffer_space_len(&can->nb_tx_rb) >= sizeof(struct rt_can_msg))
{
rt_ringbuffer_put(&can->nb_tx_rb, (rt_uint8_t *)pmsg, sizeof(struct rt_can_msg));
pmsg++;
sent_size += sizeof(struct rt_can_msg);
continue;
}
else
{
/* Buffer is full, cannot process this message or subsequent ones. */
can->status.dropedsndpkg += (size - sent_size) / sizeof(struct rt_can_msg);
break;
}
}

level = rt_hw_local_irq_disable();
if (rt_ringbuffer_space_len(&can->nb_tx_rb) >= sizeof(struct rt_can_msg))
else
{
rt_ringbuffer_put(&can->nb_tx_rb, (rt_uint8_t *)pmsg, sizeof(struct rt_can_msg));
rt_hw_local_irq_enable(level);
if (can->ops->sendmsg_nonblocking(can, pmsg) != RT_EOK)
{
level = rt_hw_local_irq_disable();
if (rt_ringbuffer_put(&can->nb_tx_rb, (rt_uint8_t *)pmsg,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

发送刚返回忙、帧还没入队时,最后一个 TX 完成中断可能先到。ISR 看到空队列就退出,后续 write 看到队列非空也只会入队,这帧就没人启动发送了。请把忙后入队和 TX 完成处理同步起来,或在入队后检查一次是否需要启动发送。

sizeof(struct rt_can_msg)) != sizeof(struct rt_can_msg))
{
can->status.dropedsndpkg += (size - sent_size) / sizeof(struct rt_can_msg);
rt_hw_local_irq_enable(level);
break;
}
rt_hw_local_irq_enable(level);
}

pmsg++;
sent_size += sizeof(struct rt_can_msg);
}
else
{
/* Buffer is full, cannot process this message or subsequent ones. */
can->status.dropedsndpkg += (size - sent_size) / sizeof(struct rt_can_msg);
rt_hw_local_irq_enable(level);
break;
level = rt_hw_local_irq_disable();
}
}

rt_hw_local_irq_enable(level);
return sent_size;
}

Expand Down Expand Up @@ -409,7 +422,7 @@ static rt_err_t rt_can_open(struct rt_device *dev, rt_uint16_t oflag)
struct rt_can_rx_fifo *rx_fifo;

rx_fifo = (struct rt_can_rx_fifo *) rt_malloc(sizeof(struct rt_can_rx_fifo) +
can->config.msgboxsz * sizeof(struct rt_can_msg_list));
can->config.msgboxsz * sizeof(struct rt_can_msg_list));
RT_ASSERT(rx_fifo != RT_NULL);

rx_fifo->buffer = (struct rt_can_msg_list *)(rx_fifo + 1);
Expand Down Expand Up @@ -441,12 +454,12 @@ static rt_err_t rt_can_open(struct rt_device *dev, rt_uint16_t oflag)
struct rt_can_tx_fifo *tx_fifo;

tx_fifo = (struct rt_can_tx_fifo *) rt_malloc(sizeof(struct rt_can_tx_fifo) +
can->config.sndboxnumber * sizeof(struct rt_can_sndbxinx_list));
can->config.sndboxnumber * sizeof(struct rt_can_sndbxinx_list));
RT_ASSERT(tx_fifo != RT_NULL);

tx_fifo->buffer = (struct rt_can_sndbxinx_list *)(tx_fifo + 1);
rt_memset(tx_fifo->buffer, 0,
can->config.sndboxnumber * sizeof(struct rt_can_sndbxinx_list));
can->config.sndboxnumber * sizeof(struct rt_can_sndbxinx_list));
rt_list_init(&tx_fifo->freelist);
for (i = 0; i < can->config.sndboxnumber; i++)
{
Expand Down Expand Up @@ -725,7 +738,6 @@ static rt_err_t rt_can_control(struct rt_device *dev,
}
rt_hw_local_irq_enable(level);
}

}
else
{
Expand Down Expand Up @@ -1057,7 +1069,6 @@ void rt_hw_can_isr(struct rt_can_device *can, int event)
listmsg->owner = &can->hdr[hdr];
can->hdr[hdr].msgs++;
}

}
#endif
rt_hw_local_irq_enable(level);
Expand Down Expand Up @@ -1132,7 +1143,8 @@ void rt_hw_can_isr(struct rt_can_device *can, int event)
level = rt_hw_local_irq_disable();
if (rt_ringbuffer_data_len(&can->nb_tx_rb) >= sizeof(struct rt_can_msg))
{
rt_ringbuffer_get(&can->nb_tx_rb, (rt_uint8_t *)&msg_to_send, sizeof(struct rt_can_msg));
struct rt_ringbuffer rb_shadow = can->nb_tx_rb;
rt_ringbuffer_get(&rb_shadow, (rt_uint8_t *)&msg_to_send, sizeof(struct rt_can_msg));
msg_was_present = RT_TRUE;
}
rt_hw_local_irq_enable(level);
Expand All @@ -1141,14 +1153,13 @@ void rt_hw_can_isr(struct rt_can_device *can, int event)
{
break;
}

if (can->ops->sendmsg_nonblocking(can, &msg_to_send) != RT_EOK)
{
level = rt_hw_local_irq_disable();
rt_ringbuffer_put_force(&can->nb_tx_rb, (rt_uint8_t *)&msg_to_send, sizeof(struct rt_can_msg));
rt_hw_local_irq_enable(level);
break;
}
level = rt_hw_local_irq_disable();
rt_ringbuffer_get(&can->nb_tx_rb, (rt_uint8_t *)&msg_to_send, sizeof(struct rt_can_msg));
rt_hw_local_irq_enable(level);
}
}
break;
Expand Down
Loading