滤波器配置 void FDCAN1_Config(uint8_t SelectDevice,uint32_t AcceptCode,uint32_t AcceptMask) { FDCAN_FilterTypeDef sFilterConfig; if(AcceptCode>0x7FF) { /* Configure Rx filter */ sFilterConfig.IdType = FDCAN_EXTENDED_ID; sFilterConfig.FilterIndex = 0; sFilterConfig.FilterType = FDCAN_FILTER_MASK; sFilterConfig.FilterConfig = FDCAN_FILTER_TO_RXFIFO0; sFilterConfig.FilterID1 = AcceptCode; sFilterConfig.FilterID2 = AcceptMask; if (HAL_FDCAN_ConfigFilter(&hfdcan1, &sFilterConfig) != HAL_OK) { //Error_Handler(); } } else { /* Configure Rx filter */ sFilterConfig.IdType = FDCAN_STANDARD_ID; sFilterConfig.FilterIndex = 0; sFilterConfig.FilterType = FDCAN_FILTER_MASK; sFilterConfig.FilterConfig = FDCAN_FILTER_TO_RXFIFO0; sFilterConfig.FilterID1 = AcceptCode; sFilterConfig.FilterID2 = AcceptMask; if (HAL_FDCAN_ConfigFilter(&hfdcan1, &sFilterConfig) != HAL_OK) { //Error_Handler(); } } /* Configure global filter: Filter all remote frames with STD and EXT ID Reject non matching frames with STD ID and EXT ID */ if (HAL_FDCAN_ConfigGlobalFilter(&hfdcan1, FDCAN_REJECT, FDCAN_REJECT, FDCAN_FILTER_REMOTE, FDCAN_FILTER_REMOTE) != HAL_OK) { //Error_Handler(); } if (HAL_FDCAN_ActivateNotification(&hfdcan1, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0) != HAL_OK) { //Error_Handler(); } if (HAL_FDCAN_ConfigTxDelayCompensation(&hfdcan1, 5, 0) != HAL_OK) { //Error_Handler(); } if (HAL_FDCAN_EnableTxDelayCompensation(&hfdcan1) != HAL_OK) { //Error_Handler(); } /* Start the FDCAN module */ if (HAL_FDCAN_Start(&hfdcan1) != HAL_OK) { //Error_Handler(); } if(SelectDevice) //CANFD_BUS { TxHeader.BitRateSwitch = FDCAN_BRS_ON; TxHeader.FDFormat = FDCAN_FD_CAN; } else //CAN_BUS { TxHeader.BitRateSwitch = FDCAN_BRS_OFF; TxHeader.FDFormat = FDCAN_CLASSIC_CAN; } TxHeader.TxEventFifoControl = FDCAN_NO_TX_EVENTS; TxHeader.MessageMarker = 0; // // HAL_NVIC_SetPriority(FDCAN1_IT0_IRQn, 5, 0); // // HAL_NVIC_EnableIRQ(FDCAN1_IT0_IRQn); } 初始化 void MX_FDCAN1_Init(uint8_t CAN_Filter) { hfdcan1.Instance = FDCAN1; hfdcan1.Init.ClockDivider = FDCAN_CLOCK_DIV8; //系统时钟160MHz,FDCAN内核时钟20MHz hfdcan1.Init.FrameFormat = FDCAN_FRAME_FD_BRS; hfdcan1.Init.Mode = FDCAN_MODE_NORMAL; hfdcan1.Init.AutoRetransmission = DISABLE; hfdcan1.Init.TransmitPause = DISABLE; hfdcan1.Init.ProtocolException = DISABLE; hfdcan1.Init.NominalPrescaler = 1; hfdcan1.Init.NominalSyncJumpWidth = 8; hfdcan1.Init.NominalTimeSeg1 = 0x1F; hfdcan1.Init.NominalTimeSeg2 = 8; hfdcan1.Init.DataPrescaler = 1; hfdcan1.Init.DataSyncJumpWidth = 4; hfdcan1.Init.DataTimeSeg1 = 5; hfdcan1.Init.DataTimeSeg2 = 4; if(CAN_Filter) { hfdcan1.Init.StdFiltersNbr = 0;//顺序0 hfdcan1.Init.ExtFiltersNbr = 1;//顺序1 } else { hfdcan1.Init.StdFiltersNbr = 1;//顺序0 hfdcan1.Init.ExtFiltersNbr = 0;//顺序1 } hfdcan1.Init.TxFifoQueueMode = FDCAN_TX_FIFO_OPERATION; if (HAL_FDCAN_Init(&hfdcan1) != HAL_OK) { //Error_Handler(); } } 中断处理 void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo0ITs) { uint16_t numBytes; uint8_t dataPayload[64] = {0}; // Used to store the received data uint8_t i; if((RxFifo0ITs & FDCAN_IT_RX_FIFO0_NEW_MESSAGE) != RESET) { // test ^= 1; // CAN1_LED(test); /* Retrieve Rx messages from RX FIFO0 */ if (HAL_FDCAN_GetRxMessage(hfdcan, FDCAN_RX_FIFO0, &RxHeader, dataPayload) != HAL_OK) { //Error_Handler(); } if (hfdcan->Instance == FDCAN1) { numBytes = RxHeader.DataLength>>16; if (RxHeader.FDFormat)//CANFD_BUS { switch(numBytes) { case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: memcpy(RxCANFDData, dataPayload, numBytes); break; case 9: memcpy(RxCANFDData, dataPayload, 12); break; case 10: memcpy(RxCANFDData, dataPayload, 16); break; case 11: memcpy(RxCANFDData, dataPayload, 20); break; case 12: memcpy(RxCANFDData, dataPayload, 24); break; case 13: memcpy(RxCANFDData, dataPayload, 32); break; case 14: memcpy(RxCANFDData, dataPayload, 48); break; case 15: memcpy(RxCANFDData, dataPayload, 64); break; } canfd_receive_flag = 1; CANFDLED_Memory = 1; } else//CANFD_BUS { memcpy(RxCANData, dataPayload, numBytes); can_receive_flag = 1; CANLED_Memory = 1; } //FDFormat HAL_FDCAN_ActivateNotification(&hfdcan1, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0); }//FDCAN1 } } 有那位大神知道是什么问题 |
STM32Cube_FW_G4_V1.2.0\Projects\STM32G474E-EVAL\Examples\FDCAN\FDCAN_Com_IT