如题,我设置三个CAN独立收发工作,只有CAN1能发送和接收,CAN2和CAN3只能发送,但是接收不到,进不了接收中断,CAN配置代码如下,求大神帮忙看看。
int CAN_Config(void)
{
HAL_CAN_DeInit(&CanHandleToBMS1);
HAL_CAN_DeInit(&CanHandleToBMS2);
HAL_CAN_DeInit(&CanHandleToDC);
CAN_FilterConfTypeDef sFilterConfig;
static CanTxMsgTypeDef TxMessage2;
static CanRxMsgTypeDef RxMessage2;
static CanTxMsgTypeDef TxMessage1;
static CanRxMsgTypeDef RxMessage1;
static CanTxMsgTypeDef TxMessage;
static CanRxMsgTypeDef RxMessage;
//CAN1 初始化 25MHz 250kbps
CanHandleToBMS1.Instance = CAN1;
CanHandleToBMS1.pTxMsg = &TxMessage1;
CanHandleToBMS1.pRxMsg = &RxMessage1;
CanHandleToBMS1.Init.TTCM = DISABLE; //非时间触发通信模式
CanHandleToBMS1.Init.ABOM = ENABLE; //DISABLE; // ENABLE; //软件自动离线管理
CanHandleToBMS1.Init.AWUM = DISABLE;
CanHandleToBMS1.Init.NART = DISABLE; //ENABLE; // DISABLE; //禁止报文自动传送
CanHandleToBMS1.Init.RFLM = ENABLE; //DISABLE; // ENABLE; //报文不锁定,新的覆盖旧的
CanHandleToBMS1.Init.TXFP = DISABLE;
CanHandleToBMS1.Init.Mode = CAN_MODE_NORMAL; //CAN_MODE_LOOPBACK; //CAN_MODE_NORMAL;
CanHandleToBMS1.Init.SJW = CAN_SJW_1TQ;
CanHandleToBMS1.Init.BS1 = CAN_BS1_4TQ;
CanHandleToBMS1.Init.BS2 = CAN_BS2_5TQ;
CanHandleToBMS1.Init.Prescaler = 20;
if(HAL_CAN_Init(&CanHandleToBMS1)!=HAL_OK)
return 1;
//CAN1过滤器 使能FIFO0
sFilterConfig.FilterNumber = 0;
sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
sFilterConfig.FilterIdHigh = 0x0000;
sFilterConfig.FilterIdLow = 0x0000;
sFilterConfig.FilterMaskIdHigh = 0x0000;
sFilterConfig.FilterMaskIdLow = 0x0000;
sFilterConfig.FilterFIFOAssignment = CAN_FIFO0;
sFilterConfig.FilterActivation = ENABLE;
sFilterConfig.BankNumber = 14;
if(HAL_CAN_ConfigFilter(&CanHandleToBMS1, &sFilterConfig)!=HAL_OK)
return 2;
//
//CAN2 初始化 25MHz 250kbps
CanHandleToBMS2.Instance = CAN2;
CanHandleToBMS2.pTxMsg = &TxMessage;
CanHandleToBMS2.pRxMsg = &RxMessage;
CanHandleToBMS2.Init.TTCM = DISABLE;
CanHandleToBMS2.Init.ABOM = ENABLE;
CanHandleToBMS2.Init.AWUM = DISABLE;
CanHandleToBMS2.Init.NART = DISABLE;
CanHandleToBMS2.Init.RFLM = ENABLE;
CanHandleToBMS2.Init.TXFP = DISABLE;
CanHandleToBMS2.Init.Mode = CAN_MODE_NORMAL;
CanHandleToBMS2.Init.SJW = CAN_SJW_1TQ;
CanHandleToBMS2.Init.BS1 = CAN_BS1_4TQ;
CanHandleToBMS2.Init.BS2 = CAN_BS2_5TQ;
CanHandleToBMS2.Init.Prescaler =20;
if(HAL_CAN_Init(&CanHandleToBMS2)!=HAL_OK)
return 3;
//CAN2过滤器 使能FIFO0
sFilterConfig.FilterNumber = 14;//14;
sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
sFilterConfig.FilterIdHigh = 0x0000;
sFilterConfig.FilterIdLow = 0x0000;
sFilterConfig.FilterMaskIdHigh = 0x0000;
sFilterConfig.FilterMaskIdLow = 0x0000;
sFilterConfig.FilterFIFOAssignment = CAN_FIFO1;
sFilterConfig.FilterActivation = ENABLE;
sFilterConfig.BankNumber = 14;
if(HAL_CAN_ConfigFilter(&CanHandleToBMS2, &sFilterConfig)!=HAL_OK)
return 4;
//
/*##-CAN3初始化 #######################################*/
CanHandleToDC.Instance = CAN3;
CanHandleToDC.pTxMsg = &TxMessage2;
CanHandleToDC.pRxMsg = &RxMessage2;
//CAN3 初始化 30MHz 125kbps
CanHandleToDC.Init.TTCM = DISABLE;
CanHandleToDC.Init.ABOM = ENABLE;
CanHandleToDC.Init.AWUM = DISABLE;
CanHandleToDC.Init.NART = DISABLE;
CanHandleToDC.Init.RFLM = ENABLE;
CanHandleToDC.Init.TXFP = DISABLE;
CanHandleToDC.Init.Mode = CAN_MODE_NORMAL;
CanHandleToDC.Init.SJW = CAN_SJW_1TQ;
CanHandleToDC.Init.BS1 = CAN_BS1_4TQ;
CanHandleToDC.Init.BS2 = CAN_BS2_5TQ;
CanHandleToDC.Init.Prescaler = 20;
HAL_CAN_Init(&CanHandleToDC);
//CAN3过滤器 使能FIFO0
sFilterConfig.FilterNumber = 0;
sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
sFilterConfig.FilterIdHigh = 0x0000;//(((uint32_t)0x1207C080<<3)&0xFFFF0000)>>16;
sFilterConfig.FilterIdLow = 0x0000;// (((uint32_t)0x1207C080<<3)|CAN_RTR_DATA|CAN_ID_EXT)&0xFFFF;
sFilterConfig.FilterMaskIdHigh = 0x0000;//0xFFFF;
sFilterConfig.FilterMaskIdLow = 0x0000;//0xFF00;
sFilterConfig.FilterFIFOAssignment = CAN_FIFO0;
sFilterConfig.FilterActivation = ENABLE;
sFilterConfig.BankNumber = 14;
HAL_CAN_ConfigFilter(&CanHandleToDC, &sFilterConfig);
return 0;
}
//CAN接收中断回调函数:
void HAL_CAN_RxCpltCallback(CAN_HandleTypeDef *CanHandle)
{
uint8_t i;
if(CanHandle->pRxMsg->IDE == CAN_ID_EXT) //
{
if(CanHandle->Instance == CAN1) //
{
// __HAL_CAN_ENABLE_IT(&CanHandleToBMS1,CAN_IT_FMP0);//重新开启FIF00消息挂号中断
CANBMS.IDE = CanHandle->pRxMsg->ExtId;
CANBMS.DLC = CanHandle->pRxMsg->DLC;
for(i=0;i<8;i++)
{
CANBMS.Data[i] = CanHandle->pRxMsg->Data[i];
}
LeftCANqueue_push(CANBMS);
HAL_CAN_Receive_IT(CanHandle, CAN_FIFO0);
__HAL_CAN_ENABLE_IT(&CanHandleToBMS1,CAN_IT_FMP0);//重新开启FIF00消息挂号中断
}
if(CanHandle->Instance == CAN2)
{
CANBMS.IDE = CanHandle->pRxMsg->ExtId;
CANBMS.DLC = CanHandle->pRxMsg->DLC;
for(i=0;i<8;i++)
{
CANBMS.Data[i] = CanHandle->pRxMsg->Data[i];
}
RightCANqueue_push(CANBMS);
HAL_CAN_Receive_IT(CanHandle, CAN_FIFO1);
__HAL_CAN_ENABLE_IT(&CanHandleToBMS2,CAN_IT_FMP1);
}
if(CanHandle->Instance == CAN3)
{
CANBMS.IDE = CanHandle->pRxMsg->ExtId;
CANBMS.DLC = CanHandle->pRxMsg->DLC;
for(i=0;i<8;i++)
{
CANBMS.Data[i] = CanHandle->pRxMsg->Data[i];
}
HAL_CAN_Receive_IT(CanHandle, CAN_FIFO0);
__HAL_CAN_ENABLE_IT(&CanHandleToDC,CAN_IT_FMP0);
}
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
extern "C" void CAN1_RX0_IRQHandler(void)//CAN1 FIFO0
{
//STM32有2个3级深度的接收缓冲区:FIFO0和FIFO1
//如果是来自FIFO0的接收中断,则用CAN1_RX0_IRQn中断来处理。
//YXCAN1_EXT_ReceiveMsg(&YXRcvCAN_Msg,0);
zCNWb.RcvCANMsg(1,0);
}
#if(CANY1Y2==2)
extern "C" void CAN2_RX1_IRQHandler(void)//CAN2 FIFO1
{
//YXCAN2_EXT_ReceiveMsg(&YXRcvCAN_Msg,0);
zCNWb.RcvCANMsg(2,0);
};
#endif
--------------------------------------------------------------------------------
冗余双CAN总线高层协议CANWeb开发资料STM32F105开发板KeilC++源程序
http://www.openedv.com/forum.php?mod=viewthread&tid=109854
一周热门 更多>