当有busoff进去重新初始化的时候,需要把接收偏移寄存器地址,重置到初始化的位置。
当出现busoff的时候需要将g_u16ReceiveOffset重新设置为0.
Can_Return_Type CAN_Receive_Message(volatile Can_Pdu_TypeDef *Pdu)
{
Can_Return_Type ret;
uint32_t tmpaddr = 0;
uint32_t tmpreg = 0;
uint32_t tmpreg1 = 0;
uint32_t i = 0;
uint8_t sdataoffset[] = {1,0,7,6,5,4,11,10};
uint8_t edataoffset[] = {7,6,5,4,11,10,9,8};
Pdu->Frame_length = CAN_Get_Receive_Message_Counter(CAN4_SFR);
for (uint8_t receive_count = 0; receive_count < Pdu->Frame_length; receive_count++)
{
tmpaddr = CAN4_RECEIVE_ADDR;
tmpaddr += g_u16ReceiveOffset;
tmpreg = *(volatile uint32_t *)tmpaddr;
/* CAN帧格式 */
tmpreg1 = (tmpreg & CAN_INFR_IDE) >> CAN_INFR_IDE_POS;
Pdu->CAN_Message[receive_count].m_FrameFormat = tmpreg1;
/* CAN远程发送请求 */
tmpreg1 = (tmpreg & CAN_INFR_RTR) >> CAN_INFR_RTR_POS;
Pdu->CAN_Message[receive_count].m_RemoteTransmit = tmpreg1;
/* 数据长度 */
tmpreg1 = (tmpreg & CAN_INFR_DLC) >> CAN_INFR_DLC0_POS;
Pdu->CAN_Message[receive_count].m_DataLength = tmpreg1;
if(Pdu->CAN_Message[receive_count].m_RemoteTransmit == 0x01)
{
g_u16ReceiveOffset += 0x10;
if (g_u16ReceiveOffset > 1008)
{
g_u16ReceiveOffset = 0;
}
continue;
}else
{
Pdu->CAN_Message[receive_count].m_RemoteTransmit = CAN_DATA_FRAME;
}
/* 获取RXDATA1*/
tmpaddr += 4;
tmpreg = *(volatile uint32_t *)tmpaddr;
if(Pdu->CAN_Message[receive_count].m_FrameFormat == 0x00)
{
/* Standard Frame Format */
/* 标准帧ID */
tmpreg1 = (tmpreg & CAN_TX0R_SFF_ID) >> CAN_TX0R_ID18_POS;
Pdu->CAN_Message[receive_count].m_Can_ID = tmpreg1;
/* 获取数据区,数据长度为m_DataLength,最大为8字节 */
for (i = 0; i < Pdu->CAN_Message[receive_count].m_DataLength; i++)
{
Pdu->CAN_Message[receive_count].m_Data[i] = *(volatile uint8_t *)(tmpaddr + sdataoffset[i]);
}
}else
{
/* Extended Frame Format */
/* 扩展帧ID */
tmpreg1 = (tmpreg & CAN_TX0R_EFF_ID) >> CAN_TX0R_ID0_POS;
Pdu->CAN_Message[receive_count].m_Can_ID = tmpreg1;
/* 获取数据区,数据长度为m_DataLength,最大为8字节 */
for (i = 0; i < Pdu->CAN_Message[receive_count].m_DataLength; i++)
{
Pdu->CAN_Message[receive_count].m_Data[i] = *(volatile uint8_t *)(tmpaddr + edataoffset[i]);
}
}
g_u16ReceiveOffset += 0x10;
if (g_u16ReceiveOffset > 1008)
{
g_u16ReceiveOffset = 0;
}
}
CAN_Release_Receive_Buffer(CAN4_SFR, Pdu->Frame_length);
ret = Can_OK;
return ret;
}
|