通过cubemx生成的HAL库,CAN模块配置如下:
CAN_HandleTypeDef hcan;
CanTxMsgTypeDef TxMessage;
CanRxMsgTypeDef RxMessage;
/* CAN init function */
void MX_CAN_Init(void)
{
hcan.Instance = CAN;
hcan.pTxMsg = &TxMessage;
hcan.pRxMsg = &RxMessage;
//250K
hcan.Init.Prescaler = 12;
hcan.Init.Mode = CAN_MODE_NORMAL;
hcan.Init.SJW = CAN_SJW_1TQ;
hcan.Init.BS1 = CAN_BS1_7TQ;
hcan.Init.BS2 = CAN_BS2_8TQ;
hcan.Init.TTCM = DISABLE;
hcan.Init.ABOM = ENABLE;
hcan.Init.AWUM = DISABLE;
hcan.Init.NART = DISABLE;
hcan.Init.RFLM = DISABLE;
hcan.Init.TXFP = DISABLE;
if (HAL_CAN_Init(&hcan) != HAL_OK)
{
Error_Handler();
}
CAN_FilterConfTypeDef sFilterConfig;
// /*##-2- Configure the CAN Filter ###########################################*/
// 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_FILTER_FIFO0;
// sFilterConfig.FilterActivation = ENABLE;
// sFilterConfig.BankNumber = 14;
// if (HAL_CAN_ConfigFilter(&hcan, &sFilterConfig) != HAL_OK)
// {
// /* Filter configuration Error */
// Error_Handler();
// }
// HAL_CAN_Receive_IT(&hcan, CAN_FIFO0); //使能CAN接收中断
/*##-2- Configure the CAN Filter ###########################################*/
sFilterConfig.FilterNumber = 1;
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_FILTER_FIFO1;
sFilterConfig.FilterActivation = ENABLE;
sFilterConfig.BankNumber = 14;
if (HAL_CAN_ConfigFilter(&hcan, &sFilterConfig) != HAL_OK)
{
/* Filter configuration Error */
Error_Handler();
}
HAL_CAN_Receive_IT(&hcan, CAN_FIFO1); //使能CAN接收中断
}
上面代码注释掉了CAN_FIFO0的那部分,可以正常接收到数据(进入CAN_FIFO0接收中断);反之,注释掉CAN_FIFO1的那部分也是可以正常接收到数据(进入CAN_FIFO1接收中断)。但如果两个都配置的话(不注释掉),一直只能进入CAN_FIFO0接收中断。我想要交替触发的效果,可以跟USB双缓冲一样吗? |