如题,我设置三个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);
}
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
谢谢,已经解决了!
一周热门 更多>