1、为何会导致漏包?
can发送到速度过快。中断接收到数据,但是数据被覆盖,应用层处理不过来,
1个can包16Byte(8Byte数据,8Byte其他参数)
假设can 速率250kbps。则1s中大能最传输多少包?
250000bps / (16*8) = 1953个包。
2、解决方法,
使用ringbuff增加缓存数据区,增加一段buffer保存数据。
mcan_rx_buffer_frame_t rxFrame[64];
volatile uint16_t rdIndex=0; /* Index of the data to send out. */
volatile uint16_t wrIndex=0; /* Index of the memory to save new arrived data. */
void CAN0_IRQ0_IRQHandler(void)
{
MCAN_ClearStatusFlag(EXAMPLE_MCAN, CAN_IR_RF0N_MASK);
MCAN_ReadRxFifo(EXAMPLE_MCAN, 0, &rxFrame[wrIndex]);
wrIndex = ((++wrIndex) & (64-1));
if (wrIndex == rdIndex) {
rdIndex = ((++rdIndex) & (64-1));
}
/* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
exception return operation might vector to incorrect interrupt */
#if defined __CORTEX_M && (__CORTEX_M == 4U)
__DSB();
#endif
}
read_data()
{
... ...
while (rdIndex == wrIndex) {
}
memcpy(rx_buf, rxFrame[rdIndex].data, rxFrame[rdIndex].size);
can_id = rxFrame[rdIndex].id;
//rdIndex = ((rdIndex++) & (64-1));
rdIndex = ((++rdIndex) & (64-1));
return (can_id/* >> STDID_OFFSET*/);
}