时钟频率
FDCAN1
设置250kbit/s
FDCAN2
设置500kbit/s
FDCAN3
设置500kbit/s
fdcan.c
/* USER CODE BEGIN Header */
/********************************************************************************* @file fdcan.c* @brief This file provides code for the configuration* of the FDCAN instances.******************************************************************************* @attention** Copyright (c) 2023 STMicroelectronics.* All rights reserved.** This software is licensed under terms that can be found in the LICENSE file* in the root directory of this software component.* If no LICENSE file comes with this software, it is provided AS-IS.********************************************************************************/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "fdcan.h"/* USER CODE BEGIN 0 */
#include "SEGGER_RTT.h"
#include "can_comm.h"
/* USER CODE END 0 */FDCAN_HandleTypeDef hfdcan1;
FDCAN_HandleTypeDef hfdcan2;
FDCAN_HandleTypeDef hfdcan3;/* FDCAN1 init function */
void MX_FDCAN1_Init(void)
{/* USER CODE BEGIN FDCAN1_Init 0 */FDCAN_FilterTypeDef FDCAN1_RXFilter;/* USER CODE END FDCAN1_Init 0 *//* USER CODE BEGIN FDCAN1_Init 1 *//* USER CODE END FDCAN1_Init 1 */hfdcan1.Instance = FDCAN1;hfdcan1.Init.FrameFormat = FDCAN_FRAME_CLASSIC;hfdcan1.Init.Mode = FDCAN_MODE_NORMAL;hfdcan1.Init.AutoRetransmission = DISABLE;hfdcan1.Init.TransmitPause = DISABLE;hfdcan1.Init.ProtocolException = DISABLE;hfdcan1.Init.NominalPrescaler = 10;hfdcan1.Init.NominalSyncJumpWidth = 8;hfdcan1.Init.NominalTimeSeg1 = 31;hfdcan1.Init.NominalTimeSeg2 = 8;hfdcan1.Init.DataPrescaler = 1;hfdcan1.Init.DataSyncJumpWidth = 1;hfdcan1.Init.DataTimeSeg1 = 0xF;hfdcan1.Init.DataTimeSeg2 = 0x4;hfdcan1.Init.MessageRAMOffset = 0;hfdcan1.Init.StdFiltersNbr = 1;hfdcan1.Init.ExtFiltersNbr = 0;hfdcan1.Init.RxFifo0ElmtsNbr = 32;hfdcan1.Init.RxFifo0ElmtSize = FDCAN_DATA_BYTES_8;hfdcan1.Init.RxFifo1ElmtsNbr = 0;hfdcan1.Init.RxFifo1ElmtSize = FDCAN_DATA_BYTES_8;hfdcan1.Init.RxBuffersNbr = 0;hfdcan1.Init.RxBufferSize = FDCAN_DATA_BYTES_8;hfdcan1.Init.TxEventsNbr = 0;hfdcan1.Init.TxBuffersNbr = 0;hfdcan1.Init.TxFifoQueueElmtsNbr = 6;hfdcan1.Init.TxFifoQueueMode = FDCAN_TX_FIFO_OPERATION;hfdcan1.Init.TxElmtSize = FDCAN_DATA_BYTES_8;if (HAL_FDCAN_Init(&hfdcan1) != HAL_OK){Error_Handler();}/* USER CODE BEGIN FDCAN1_Init 2 *///配置RX滤波器 FDCAN1_RXFilter.IdType=FDCAN_STANDARD_ID; //标准IDFDCAN1_RXFilter.FilterIndex=0; //滤波器索引 FDCAN1_RXFilter.FilterType=FDCAN_FILTER_MASK; //滤波器类型FDCAN1_RXFilter.FilterConfig=FDCAN_FILTER_TO_RXFIFO0; //过滤器0关联到FIFO0 FDCAN1_RXFilter.FilterID1=0x0000; //32位IDFDCAN1_RXFilter.FilterID2=0x0000; //如果FDCAN配置为传统模式的话,这里是32位掩码HAL_FDCAN_ConfigFilter(&hfdcan1,&FDCAN1_RXFilter);HAL_FDCAN_Start(&hfdcan1); //开启FDCAN/* HAL_FDCAN_ConfigGlobalFilter()* 参数2:设置标准帧ID,接收的报文ID没有匹配上滤波器时,选择拒绝接收(没有匹配上时,可以选择放入FIFO0或者FIFO1)。* 参数3:设置拓展帧ID,接收的报文ID没有匹配上滤波器时,选择拒绝接收。* 参数4:设置是否拒绝远程标准帧,ENABLE代表拒绝接收。* 参数5:设置是否拒绝远程拓展帧,ENABLE代表拒绝接收。*/HAL_FDCAN_ConfigGlobalFilter(&hfdcan1,FDCAN_REJECT,FDCAN_REJECT,DISABLE,ENABLE); /* 设置FDCAN1滤波器0全局配置 */HAL_FDCAN_ActivateNotification(&hfdcan1,FDCAN_IT_RX_FIFO0_NEW_MESSAGE,0);/* USER CODE END FDCAN1_Init 2 */}
/* FDCAN2 init function */
void MX_FDCAN2_Init(void)
{/* USER CODE BEGIN FDCAN2_Init 0 */FDCAN_FilterTypeDef FDCAN2_RXFilter;/* USER CODE END FDCAN2_Init 0 *//* USER CODE BEGIN FDCAN2_Init 1 *//* USER CODE END FDCAN2_Init 1 */hfdcan2.Instance = FDCAN2;hfdcan2.Init.FrameFormat = FDCAN_FRAME_CLASSIC;hfdcan2.Init.Mode = FDCAN_MODE_NORMAL;hfdcan2.Init.AutoRetransmission = DISABLE;hfdcan2.Init.TransmitPause = DISABLE;hfdcan2.Init.ProtocolException = DISABLE;hfdcan2.Init.NominalPrescaler = 5;hfdcan2.Init.NominalSyncJumpWidth = 8;hfdcan2.Init.NominalTimeSeg1 = 31;hfdcan2.Init.NominalTimeSeg2 = 8;hfdcan2.Init.DataPrescaler = 1;hfdcan2.Init.DataSyncJumpWidth = 1;hfdcan2.Init.DataTimeSeg1 = 15;hfdcan2.Init.DataTimeSeg2 = 4;hfdcan2.Init.MessageRAMOffset = 0x406;hfdcan2.Init.StdFiltersNbr = 1;hfdcan2.Init.ExtFiltersNbr = 0;hfdcan2.Init.RxFifo0ElmtsNbr = 0;hfdcan2.Init.RxFifo0ElmtSize = FDCAN_DATA_BYTES_8;hfdcan2.Init.RxFifo1ElmtsNbr = 32;hfdcan2.Init.RxFifo1ElmtSize = FDCAN_DATA_BYTES_8;hfdcan2.Init.RxBuffersNbr = 0;hfdcan2.Init.RxBufferSize = FDCAN_DATA_BYTES_8;hfdcan2.Init.TxEventsNbr = 0;hfdcan2.Init.TxBuffersNbr = 0;hfdcan2.Init.TxFifoQueueElmtsNbr = 6;hfdcan2.Init.TxFifoQueueMode = FDCAN_TX_FIFO_OPERATION;hfdcan2.Init.TxElmtSize = FDCAN_DATA_BYTES_8;if (HAL_FDCAN_Init(&hfdcan2) != HAL_OK){Error_Handler();}/* USER CODE BEGIN FDCAN2_Init 2 *///配置RX滤波器 FDCAN2_RXFilter.IdType=FDCAN_STANDARD_ID; //标准IDFDCAN2_RXFilter.FilterIndex=0; //滤波器索引 FDCAN2_RXFilter.FilterType=FDCAN_FILTER_MASK; //滤波器类型FDCAN2_RXFilter.FilterConfig=FDCAN_FILTER_TO_RXFIFO1; //过滤器0关联到FIFO1 FDCAN2_RXFilter.FilterID1=0x0000; //32位IDFDCAN2_RXFilter.FilterID2=0x0000; //如果FDCAN配置为传统模式的话,这里是32位掩码HAL_FDCAN_ConfigFilter(&hfdcan2,&FDCAN2_RXFilter);HAL_FDCAN_Start(&hfdcan2); //开启FDCAN/* HAL_FDCAN_ConfigGlobalFilter()* 参数2:设置标准帧ID,接收的报文ID没有匹配上滤波器时,选择拒绝接收(没有匹配上时,可以选择放入FIFO0或者FIFO1)。* 参数3:设置拓展帧ID,接收的报文ID没有匹配上滤波器时,选择拒绝接收。* 参数4:设置是否拒绝远程标准帧,ENABLE代表拒绝接收。* 参数5:设置是否拒绝远程拓展帧,ENABLE代表拒绝接收。*/HAL_FDCAN_ConfigGlobalFilter(&hfdcan2,FDCAN_REJECT,FDCAN_REJECT,DISABLE,ENABLE); /* 设置FDCAN1滤波器1全局配置 */HAL_FDCAN_ActivateNotification(&hfdcan2,FDCAN_IT_RX_FIFO1_NEW_MESSAGE,0);/* USER CODE END FDCAN2_Init 2 */}
/* FDCAN3 init function */
void MX_FDCAN3_Init(void)
{/* USER CODE BEGIN FDCAN3_Init 0 */FDCAN_FilterTypeDef FDCAN3_RXFilter;/* USER CODE END FDCAN3_Init 0 *//* USER CODE BEGIN FDCAN3_Init 1 *//* USER CODE END FDCAN3_Init 1 */hfdcan3.Instance = FDCAN3;hfdcan3.Init.FrameFormat = FDCAN_FRAME_CLASSIC;hfdcan3.Init.Mode = FDCAN_MODE_NORMAL;hfdcan3.Init.AutoRetransmission = DISABLE;hfdcan3.Init.TransmitPause = DISABLE;hfdcan3.Init.ProtocolException = DISABLE;hfdcan3.Init.NominalPrescaler = 5;hfdcan3.Init.NominalSyncJumpWidth = 8;hfdcan3.Init.NominalTimeSeg1 = 31;hfdcan3.Init.NominalTimeSeg2 = 8;hfdcan3.Init.DataPrescaler = 1;hfdcan3.Init.DataSyncJumpWidth = 1;hfdcan3.Init.DataTimeSeg1 = 15;hfdcan3.Init.DataTimeSeg2 = 4;hfdcan3.Init.MessageRAMOffset = 0x800;hfdcan3.Init.StdFiltersNbr = 1;hfdcan3.Init.ExtFiltersNbr = 0;hfdcan3.Init.RxFifo0ElmtsNbr = 0;hfdcan3.Init.RxFifo0ElmtSize = FDCAN_DATA_BYTES_8;hfdcan3.Init.RxFifo1ElmtsNbr = 32;hfdcan3.Init.RxFifo1ElmtSize = FDCAN_DATA_BYTES_8;hfdcan3.Init.RxBuffersNbr = 0;hfdcan3.Init.RxBufferSize = FDCAN_DATA_BYTES_8;hfdcan3.Init.TxEventsNbr = 0;hfdcan3.Init.TxBuffersNbr = 0;hfdcan3.Init.TxFifoQueueElmtsNbr = 6;hfdcan3.Init.TxFifoQueueMode = FDCAN_TX_FIFO_OPERATION;hfdcan3.Init.TxElmtSize = FDCAN_DATA_BYTES_8;if (HAL_FDCAN_Init(&hfdcan3) != HAL_OK){Error_Handler();}/* USER CODE BEGIN FDCAN3_Init 2 *///配置RX滤波器 FDCAN3_RXFilter.IdType=FDCAN_STANDARD_ID; //标准IDFDCAN3_RXFilter.FilterIndex=0; //滤波器索引 FDCAN3_RXFilter.FilterType=FDCAN_FILTER_MASK; //滤波器类型FDCAN3_RXFilter.FilterConfig=FDCAN_FILTER_TO_RXFIFO1; //过滤器0关联到FIFO1 FDCAN3_RXFilter.FilterID1=0x0000; //32位IDFDCAN3_RXFilter.FilterID2=0x0000; //如果FDCAN配置为传统模式的话,这里是32位掩码HAL_FDCAN_ConfigFilter(&hfdcan3,&FDCAN3_RXFilter);HAL_FDCAN_Start(&hfdcan3); //开启FDCAN/* HAL_FDCAN_ConfigGlobalFilter()* 参数2:设置标准帧ID,接收的报文ID没有匹配上滤波器时,选择拒绝接收(没有匹配上时,可以选择放入FIFO0或者FIFO1)。* 参数3:设置拓展帧ID,接收的报文ID没有匹配上滤波器时,选择拒绝接收。* 参数4:设置是否拒绝远程标准帧,ENABLE代表拒绝接收。* 参数5:设置是否拒绝远程拓展帧,ENABLE代表拒绝接收。*/HAL_FDCAN_ConfigGlobalFilter(&hfdcan3,FDCAN_REJECT,FDCAN_REJECT,DISABLE,ENABLE); /* 设置FDCAN1滤波器1全局配置 */HAL_FDCAN_ActivateNotification(&hfdcan3,FDCAN_IT_RX_FIFO1_NEW_MESSAGE,0);/* USER CODE END FDCAN3_Init 2 */}static uint32_t HAL_RCC_FDCAN_CLK_ENABLED=0;void HAL_FDCAN_MspInit(FDCAN_HandleTypeDef* fdcanHandle)
{GPIO_InitTypeDef GPIO_InitStruct = {0};RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};if(fdcanHandle->Instance==FDCAN1){/* USER CODE BEGIN FDCAN1_MspInit 0 *//* USER CODE END FDCAN1_MspInit 0 *//** Initializes the peripherals clock*/PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_FDCAN;PeriphClkInitStruct.FdcanClockSelection = RCC_FDCANCLKSOURCE_PLL;if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK){Error_Handler();}/* FDCAN1 clock enable */HAL_RCC_FDCAN_CLK_ENABLED++;if(HAL_RCC_FDCAN_CLK_ENABLED==1){__HAL_RCC_FDCAN_CLK_ENABLE();}__HAL_RCC_GPIOB_CLK_ENABLE();/**FDCAN1 GPIO ConfigurationPB8 ------> FDCAN1_RXPB9 ------> FDCAN1_TX*/GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9;GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;GPIO_InitStruct.Pull = GPIO_NOPULL;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;GPIO_InitStruct.Alternate = GPIO_AF9_FDCAN1;HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);/* FDCAN1 interrupt Init */HAL_NVIC_SetPriority(FDCAN1_IT0_IRQn, 11, 0);HAL_NVIC_EnableIRQ(FDCAN1_IT0_IRQn);/* USER CODE BEGIN FDCAN1_MspInit 1 *//* USER CODE END FDCAN1_MspInit 1 */}else if(fdcanHandle->Instance==FDCAN2){/* USER CODE BEGIN FDCAN2_MspInit 0 *//* USER CODE END FDCAN2_MspInit 0 *//** Initializes the peripherals clock*/PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_FDCAN;PeriphClkInitStruct.FdcanClockSelection = RCC_FDCANCLKSOURCE_PLL;if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK){Error_Handler();}/* FDCAN2 clock enable */HAL_RCC_FDCAN_CLK_ENABLED++;if(HAL_RCC_FDCAN_CLK_ENABLED==1){__HAL_RCC_FDCAN_CLK_ENABLE();}__HAL_RCC_GPIOB_CLK_ENABLE();/**FDCAN2 GPIO ConfigurationPB12 ------> FDCAN2_RXPB13 ------> FDCAN2_TX*/GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13;GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;GPIO_InitStruct.Pull = GPIO_NOPULL;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;GPIO_InitStruct.Alternate = GPIO_AF9_FDCAN2;HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);/* FDCAN2 interrupt Init */HAL_NVIC_SetPriority(FDCAN2_IT0_IRQn, 12, 0);HAL_NVIC_EnableIRQ(FDCAN2_IT0_IRQn);/* USER CODE BEGIN FDCAN2_MspInit 1 *//* USER CODE END FDCAN2_MspInit 1 */}else if(fdcanHandle->Instance==FDCAN3){/* USER CODE BEGIN FDCAN3_MspInit 0 *//* USER CODE END FDCAN3_MspInit 0 *//** Initializes the peripherals clock*/PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_FDCAN;PeriphClkInitStruct.FdcanClockSelection = RCC_FDCANCLKSOURCE_PLL;if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK){Error_Handler();}/* FDCAN3 clock enable */HAL_RCC_FDCAN_CLK_ENABLED++;if(HAL_RCC_FDCAN_CLK_ENABLED==1){__HAL_RCC_FDCAN_CLK_ENABLE();}__HAL_RCC_GPIOD_CLK_ENABLE();/**FDCAN3 GPIO ConfigurationPD12 ------> FDCAN3_RXPD13 ------> FDCAN3_TX*/GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13;GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;GPIO_InitStruct.Pull = GPIO_NOPULL;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;GPIO_InitStruct.Alternate = GPIO_AF5_FDCAN3;HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);/* FDCAN3 interrupt Init */HAL_NVIC_SetPriority(FDCAN3_IT0_IRQn, 13, 0);HAL_NVIC_EnableIRQ(FDCAN3_IT0_IRQn);/* USER CODE BEGIN FDCAN3_MspInit 1 *//* USER CODE END FDCAN3_MspInit 1 */}
}void HAL_FDCAN_MspDeInit(FDCAN_HandleTypeDef* fdcanHandle)
{if(fdcanHandle->Instance==FDCAN1){/* USER CODE BEGIN FDCAN1_MspDeInit 0 *//* USER CODE END FDCAN1_MspDeInit 0 *//* Peripheral clock disable */HAL_RCC_FDCAN_CLK_ENABLED--;if(HAL_RCC_FDCAN_CLK_ENABLED==0){__HAL_RCC_FDCAN_CLK_DISABLE();}/**FDCAN1 GPIO ConfigurationPB8 ------> FDCAN1_RXPB9 ------> FDCAN1_TX*/HAL_GPIO_DeInit(GPIOB, GPIO_PIN_8|GPIO_PIN_9);/* FDCAN1 interrupt Deinit */HAL_NVIC_DisableIRQ(FDCAN1_IT0_IRQn);/* USER CODE BEGIN FDCAN1_MspDeInit 1 *//* USER CODE END FDCAN1_MspDeInit 1 */}else if(fdcanHandle->Instance==FDCAN2){/* USER CODE BEGIN FDCAN2_MspDeInit 0 *//* USER CODE END FDCAN2_MspDeInit 0 *//* Peripheral clock disable */HAL_RCC_FDCAN_CLK_ENABLED--;if(HAL_RCC_FDCAN_CLK_ENABLED==0){__HAL_RCC_FDCAN_CLK_DISABLE();}/**FDCAN2 GPIO ConfigurationPB12 ------> FDCAN2_RXPB13 ------> FDCAN2_TX*/HAL_GPIO_DeInit(GPIOB, GPIO_PIN_12|GPIO_PIN_13);/* FDCAN2 interrupt Deinit */HAL_NVIC_DisableIRQ(FDCAN2_IT0_IRQn);/* USER CODE BEGIN FDCAN2_MspDeInit 1 *//* USER CODE END FDCAN2_MspDeInit 1 */}else if(fdcanHandle->Instance==FDCAN3){/* USER CODE BEGIN FDCAN3_MspDeInit 0 *//* USER CODE END FDCAN3_MspDeInit 0 *//* Peripheral clock disable */HAL_RCC_FDCAN_CLK_ENABLED--;if(HAL_RCC_FDCAN_CLK_ENABLED==0){__HAL_RCC_FDCAN_CLK_DISABLE();}/**FDCAN3 GPIO ConfigurationPD12 ------> FDCAN3_RXPD13 ------> FDCAN3_TX*/HAL_GPIO_DeInit(GPIOD, GPIO_PIN_12|GPIO_PIN_13);/* FDCAN3 interrupt Deinit */HAL_NVIC_DisableIRQ(FDCAN3_IT0_IRQn);/* USER CODE BEGIN FDCAN3_MspDeInit 1 *//* USER CODE END FDCAN3_MspDeInit 1 */}
}/* USER CODE BEGIN 1 */
/*** 函数功能: FIFO0的接收中断回调函数* 输入参数: RxFifo0ITs:返回标志位* 返回值: void* 说明:* 1.FDCAN1使用RXFIFO0*/
void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo0ITs)
{uint8_t i=0;uint8_t rxdata[8];FDCAN_RxHeaderTypeDef FDCAN1_RxHeader;
// SEGGER_RTT_printf(0,"HAL_FDCAN_RxFifo0Callback\n");if((RxFifo0ITs&FDCAN_IT_RX_FIFO0_NEW_MESSAGE)!=RESET) //FIFO0新数据中断{//提取FIFO0中接收到的数据HAL_FDCAN_GetRxMessage(hfdcan,FDCAN_RX_FIFO0,&FDCAN1_RxHeader,rxdata);// SEGGER_RTT_printf(0,"FDCAN1_RxHeader id:%x\r\n",FDCAN1_RxHeader.Identifier);
// SEGGER_RTT_printf(0,"FDCAN1_RxHeader len:%d\r\n",FDCAN1_RxHeader.DataLength>>16);
// for(i=0;i<8;i++)
// SEGGER_RTT_printf(0,"FDCAN1_RxHeader rxdata[%d]:%d\r\n",i,rxdata[i]);}
}/*** 函数功能: FIFO1的接收中断回调函数* 输入参数: RxFifo0ITs:返回标志位* 返回值: void* 说明:* 1、FDCAN2使用RXFIFO1*/
void HAL_FDCAN_RxFifo1Callback(FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo1ITs)
{uint8_t i=0;uint8_t rxdata[8];FDCAN_RxHeaderTypeDef FDCAN2_RxHeader;FDCAN_RxHeaderTypeDef FDCAN3_RxHeader;if((RxFifo1ITs&FDCAN_IT_RX_FIFO1_NEW_MESSAGE)!=RESET) //FIFO1新数据中断{//提取FIFO0中接收到的数据HAL_FDCAN_GetRxMessage(hfdcan,FDCAN_RX_FIFO1,&FDCAN2_RxHeader,rxdata);SEGGER_RTT_printf(0,"FDCAN2_RxHeader id:%x\r\n",FDCAN2_RxHeader.Identifier);SEGGER_RTT_printf(0,"FDCAN2_RxHeader len:%d\r\n",FDCAN2_RxHeader.DataLength>>16);for(i=0;i<8;i++)SEGGER_RTT_printf(0,"FDCAN2_RxHeader rxdata[%d]:%d\r\n",i,rxdata[i]);}if((RxFifo1ITs&FDCAN_IT_RX_FIFO1_NEW_MESSAGE)!=RESET) //FIFO1新数据中断{//提取FIFO0中接收到的数据HAL_FDCAN_GetRxMessage(hfdcan,FDCAN_RX_FIFO1,&FDCAN3_RxHeader,rxdata);SEGGER_RTT_printf(0,"FDCAN3_RxHeader id:%#x\r\n",FDCAN3_RxHeader.Identifier);SEGGER_RTT_printf(0,"FDCAN3_RxHeader len:%d\r\n",FDCAN3_RxHeader.DataLength>>16);for(i=0;i<8;i++)SEGGER_RTT_printf(0,"FDCAN3_RxHeader rxdata[%d]:%d\r\n",i,rxdata[i]);}}
/* USER CODE END 1 */
新增文件 can_comm.c
#include "can_comm.h"//CAN_TxHeaderTypeDef TxHeader; // 发送
//CAN_RxHeaderTypeDef RxHeader; // 接收FDCAN_TxHeaderTypeDef FDCAN1_TxHeader; // 发送
FDCAN_RxHeaderTypeDef FDCAN1_RxHeader; // 接收FDCAN_TxHeaderTypeDef FDCAN2_TxHeader; // 发送
FDCAN_RxHeaderTypeDef FDCAN2_RxHeader; // 接收FDCAN_TxHeaderTypeDef FDCAN3_TxHeader; // 发送
FDCAN_RxHeaderTypeDef FDCAN3_RxHeader; // 接收#if 1
//调试好的//注意:数据长度必须用 FDCAN_DLC_BYTES_8
uint8_t FDCAN1_Send_Msg(uint8_t* msg,uint32_t len)
{ FDCAN1_TxHeader.Identifier=0x12; //32位IDFDCAN1_TxHeader.IdType=FDCAN_STANDARD_ID; //标准IDFDCAN1_TxHeader.TxFrameType=FDCAN_DATA_FRAME; //数据帧FDCAN1_TxHeader.DataLength=len; //数据长度FDCAN1_TxHeader.ErrorStateIndicator=FDCAN_ESI_ACTIVE; FDCAN1_TxHeader.BitRateSwitch=FDCAN_BRS_OFF; //关闭速率切换FDCAN1_TxHeader.FDFormat=FDCAN_CLASSIC_CAN; //传统的CAN模式FDCAN1_TxHeader.TxEventFifoControl=FDCAN_NO_TX_EVENTS; //无发送事件FDCAN1_TxHeader.MessageMarker=0; if(HAL_FDCAN_AddMessageToTxFifoQ(&hfdcan1,&FDCAN1_TxHeader,msg)!=HAL_OK) {SEGGER_RTT_printf(0, "FDCAN1_Send_Msg false\n"); //调试通用return 1;//发送}SEGGER_RTT_printf(0, "FDCAN1_Send_Msg true\n"); //调试通用return 0;
}//注意:数据长度必须用 FDCAN_DLC_BYTES_8
uint8_t FDCAN2_Send_Msg(uint8_t* msg,uint32_t len)
{ FDCAN2_TxHeader.Identifier=0x12; //32位IDFDCAN2_TxHeader.IdType=FDCAN_STANDARD_ID; //标准IDFDCAN2_TxHeader.TxFrameType=FDCAN_DATA_FRAME; //数据帧FDCAN2_TxHeader.DataLength=len; //数据长度FDCAN2_TxHeader.ErrorStateIndicator=FDCAN_ESI_ACTIVE; FDCAN2_TxHeader.BitRateSwitch=FDCAN_BRS_OFF; //关闭速率切换FDCAN2_TxHeader.FDFormat=FDCAN_CLASSIC_CAN; //传统的CAN模式FDCAN2_TxHeader.TxEventFifoControl=FDCAN_NO_TX_EVENTS; //无发送事件FDCAN2_TxHeader.MessageMarker=0; if(HAL_FDCAN_AddMessageToTxFifoQ(&hfdcan2,&FDCAN2_TxHeader,msg)!=HAL_OK) {SEGGER_RTT_printf(0, "FDCAN2_Send_Msg false\n"); //调试通用return 1;//发送}SEGGER_RTT_printf(0, "FDCAN2_Send_Msg true\n"); //调试通用return 0;
}//注意:数据长度必须用 FDCAN_DLC_BYTES_8
uint8_t FDCAN3_Send_Msg(uint8_t* msg,uint32_t len)
{ FDCAN3_TxHeader.Identifier=0x12; //32位IDFDCAN3_TxHeader.IdType=FDCAN_STANDARD_ID; //标准IDFDCAN3_TxHeader.TxFrameType=FDCAN_DATA_FRAME; //数据帧FDCAN3_TxHeader.DataLength=len; //数据长度FDCAN3_TxHeader.ErrorStateIndicator=FDCAN_ESI_ACTIVE; FDCAN3_TxHeader.BitRateSwitch=FDCAN_BRS_OFF; //关闭速率切换FDCAN3_TxHeader.FDFormat=FDCAN_CLASSIC_CAN; //传统的CAN模式FDCAN3_TxHeader.TxEventFifoControl=FDCAN_NO_TX_EVENTS; //无发送事件FDCAN3_TxHeader.MessageMarker=0; if(HAL_FDCAN_AddMessageToTxFifoQ(&hfdcan3,&FDCAN3_TxHeader,msg)!=HAL_OK) {SEGGER_RTT_printf(0, "FDCAN3_Send_Msg false\n"); //调试通用return 1;//发送}SEGGER_RTT_printf(0, "FDCAN3_Send_Msg true\n"); //调试通用return 0;
}//注意:数据长度必须用 FDCAN_DLC_BYTES_8
uint8_t FDCAN1_Send_Msg_TWTY_TPSLS(uint8_t* msg,uint32_t len)
{ FDCAN1_TxHeader.Identifier=0x18FFA017; //32位IDFDCAN1_TxHeader.IdType=FDCAN_EXTENDED_ID; //扩展IDFDCAN1_TxHeader.TxFrameType=FDCAN_DATA_FRAME; //数据帧FDCAN1_TxHeader.DataLength=len; //数据长度 FDCAN_DLC_BYTES_64FDCAN1_TxHeader.ErrorStateIndicator=FDCAN_ESI_ACTIVE; FDCAN1_TxHeader.BitRateSwitch=FDCAN_BRS_OFF; //关闭速率切换 FDCAN_BRS_ONFDCAN1_TxHeader.FDFormat=FDCAN_CLASSIC_CAN; //传统的CAN模式 FDCAN_FD_CANFDCAN1_TxHeader.TxEventFifoControl=FDCAN_NO_TX_EVENTS; //无发送事件FDCAN1_TxHeader.MessageMarker=0; if(HAL_FDCAN_AddMessageToTxFifoQ(&hfdcan1,&FDCAN1_TxHeader,msg)!=HAL_OK) {SEGGER_RTT_printf(0, "FDCAN1_Send_Msg false\n"); //调试通用return 1;//发送}SEGGER_RTT_printf(0, "FDCAN1_Send_Msg true\n"); //调试通用return 0;
}
#endif
main.c
uint8_t canbuf[8] = {0x55,0x57,0x02,0x03,0x04,0x05,0x06,0x07};
FDCAN1_Send_Msg(canbuf,FDCAN_DLC_BYTES_8);
FDCAN2_Send_Msg(canbuf,FDCAN_DLC_BYTES_8);
FDCAN3_Send_Msg(canbuf,FDCAN_DLC_BYTES_8);