STM32CubeMXhal库CAN2配置
在开启can1时钟的基础上,进行如下配置:
1 /** 2 * @brief CAN2 Initialization Function 3 * @param None 4 * @retval None 5 */ 6 void MX_CAN2_Init(void) 7 { 8 9 /* USER CODE BEGIN CAN2_Init 0 */ 10 CAN_FilterTypeDef CAN2_FilerConf; 11 /* USER CODE END CAN2_Init 0 */ 12 13 /* USER CODE BEGIN CAN2_Init 1 */ 14 15 /* USER CODE END CAN2_Init 1 */ 16 hcan2.Instance = CAN2; 17 hcan2.Init.Prescaler = 3; 18 hcan2.Init.Mode = CAN_MODE_NORMAL; 19 hcan2.Init.SyncJumpWidth = CAN_SJW_1TQ; 20 hcan2.Init.TimeSeg1 = CAN_BS1_9TQ; 21 hcan2.Init.TimeSeg2 = CAN_BS2_4TQ; 22 hcan2.Init.TimeTriggeredMode = DISABLE;//这几个DISABLE暂时还用不上,我试着改了几个为ENABLE也能正常运行(小弟才疏学浅,要是有错误轻喷) 23 hcan2.Init.AutoBusOff = DISABLE; 24 hcan2.Init.AutoWakeUp = DISABLE; 25 hcan2.Init.AutoRetransmission = DISABLE; 26 hcan2.Init.ReceiveFifoLocked = DISABLE; 27 hcan2.Init.TransmitFifoPriority = DISABLE; 28 if (HAL_CAN_Init(&hcan2) != HAL_OK) 29 { 30 Error_Handler(); 31 } 32 33 /* USER CODE BEGIN CAN2_Init 2 */ 34 CAN2_FilerConf.FilterIdHigh = 0X0000; 35 CAN2_FilerConf.FilterIdLow = 0X0000; 36 CAN2_FilerConf.FilterMaskIdHigh = 0X0000; 37 CAN2_FilerConf.FilterMaskIdLow = 0X0000; 38 CAN2_FilerConf.FilterFIFOAssignment = CAN_FILTER_FIFO1;//此处选择fifo号要与其他地方一致 39 CAN2_FilerConf.FilterBank = 14; 40 CAN2_FilerConf.FilterMode = CAN_FILTERMODE_IDMASK; 41 CAN2_FilerConf.FilterScale = CAN_FILTERSCALE_32BIT; 42 CAN2_FilerConf.FilterActivation = ENABLE; 43 CAN2_FilerConf.SlaveStartFilterBank = 14; 44 if (HAL_CAN_ConfigFilter(&hcan2, &CAN2_FilerConf) != HAL_OK) 45 { 46 /* Filter configuration Error */ 47 Error_Handler(); 48 } 49 50 /* Start the CAN peripheral */ 51 if (HAL_CAN_Start(&hcan2) != HAL_OK)//这个函数和下面的函数是cubemx没有给出的,需要手动添加 52 { 53 /* Start Error */ 54 Error_Handler(); 55 } 56 57 /* Activate CAN RX notification */ 58 if (HAL_CAN_ActivateNotification(&hcan2, CAN_IT_RX_FIFO1_MSG_PENDING) != HAL_OK)//选择fifo1中断 59 { 60 /* Notification Error */ 61 Error_Handler(); 62 } 63 64 /* USER CODE END CAN2_Init 2 */ 65 }
回调函数:
1 /** 2 *@brief CAN2回调函数 3 */ 4 void HAL_CAN_RxFifo1MsgPendingCallback(CAN_HandleTypeDef *hcan) 5 { 6 CAN_RxHeaderTypeDef RxMsg; 7 8 uint8_t Rx_BUF[8]; 9 RxMsg.DLC=8; 10 RxMsg.IDE=CAN_ID_STD; 11 RxMsg.RTR=CAN_RTR_DATA; 12 13 if(hcan->Instance == CAN2) 14 { 15 HAL_CAN_GetRxMessage(&hcan2, CAN_RX_FIFO1, &RxMsg, Rx_BUF);//此处的fifo1也要注意 16 } 17 }
有几个需要注意的点已在代码后面注释。
另外,回调函数的选择、msp初始化函数中的接收中断使能名称,stm32f4xx_it.c中的中断函数名称一定要与所选择的fifo号一致,我就是因为中断函数名称选成了RX0而被坑了好几天。