FDCAN输出数据(STM32H7)

STM32配置FDCAN

总线上有的设备可能不支持FDCAN,FDCAN配置为传统CAN。

配置外部时钟

 

 

配置FDCAN1

 

 GPIO配置

 定义变量

FDCAN_RxHeaderTypeDef RxHeader;
FDCAN_TxHeaderTypeDef TxHeader;
uint8_t RxData[8];
uint8_t TxData[64] = {0x24,0x47,0x4E,0x47,0x53,0x41,0x2C,0x41,0x2C,0x31,0x2C,0x2C,0x2C,0x2C,0x2C,0x2C,0x2C,0x2C,0x2C,0x2C,0x2C,0x2C,0x2C,0x39,0x39,0x2E,0x30,0x2C,0x39,0x39,0x2E,0x30,0x2C,0x39,0x39,0x2E,0x30,0x2A,0x31,0x45,0x0A};
uint8_t TxData1[8] = {0x03,0x41,0x0D,0x32,0x00,0x00,0x00,0x00};

配置CAN发送参数

void FDCAN1_Config(void)
{
  FDCAN_FilterTypeDef sFilterConfig;


  sFilterConfig.IdType = FDCAN_STANDARD_ID;   //  配置为过滤标准帧
  sFilterConfig.FilterIndex = 0;              //  过滤器的索引号
  sFilterConfig.FilterType = FDCAN_FILTER_MASK;  //  过滤方式为范围,即从FilterID1~FilterID2之间的值
  sFilterConfig.FilterConfig = FDCAN_FILTER_TO_RXFIFO0;
  sFilterConfig.FilterID1 = 0x0000;
  sFilterConfig.FilterID2 = 0x07ff;           //  标准帧为11位ID,即0x7ff,本例配置为接收所有帧
  if(HAL_FDCAN_ConfigFilter(&hfdcan1, &sFilterConfig) != HAL_OK)
  {
    Error_Handler();
  }

  if (HAL_FDCAN_ConfigGlobalFilter(&hfdcan1, FDCAN_REJECT, FDCAN_REJECT, FDCAN_FILTER_REMOTE, FDCAN_FILTER_REMOTE) != HAL_OK)
  {
    Error_Handler();
  }

//  sFilterConfig.IdType = FDCAN_EXTENDED_ID;   //  配置为过滤扩展帧
//  sFilterConfig.FilterIndex = 0;              //  过滤器的索引号
//  sFilterConfig.FilterType = FDCAN_FILTER_RANGE_NO_EIDM;  //  过滤方式为范围,即从FilterID1~FilterID2之间的值
//  sFilterConfig.FilterConfig = FDCAN_FILTER_TO_RXFIFO1;
//  sFilterConfig.FilterID1 = 0x00000000;
//  sFilterConfig.FilterID2 = 0x01ffffff;       //  扩展帧为29位ID,即0x1fffffff,本例配置为接收所有帧
//  HAL_FDCAN_ConfigFilter(&hfdcan1, &sFilterConfig);

  HAL_FDCAN_ActivateNotification(&hfdcan1,FDCAN_IT_RX_FIFO0_NEW_MESSAGE,0);

  if (HAL_FDCAN_Start(&hfdcan1) != HAL_OK)
  {
    Error_Handler();
  }
}

CAN发送数据包

uint8_t can1_SendPacket(uint32_t id, uint8_t *_DataBuf, uint8_t _Len)
{
    FDCAN_TxHeaderTypeDef TxHeader = {0};

    /* 配置发送参数 */
    TxHeader.Identifier = id;                      /* 设置发送帧消息的ID */
    TxHeader.IdType = FDCAN_STANDARD_ID;              /* 标准ID */
    TxHeader.TxFrameType = FDCAN_DATA_FRAME;         /* 数据帧 */
    TxHeader.ErrorStateIndicator = FDCAN_ESI_ACTIVE; /* 设置错误状态指 */
    TxHeader.BitRateSwitch = FDCAN_BRS_OFF;           /* 关闭可变波特率 */
    TxHeader.FDFormat = FDCAN_CLASSIC_CAN;                /* FDCAN格式 */
    TxHeader.TxEventFifoControl = FDCAN_NO_TX_EVENTS;/* 用于发送事件FIFO控制, 无发送事件*/
    TxHeader.MessageMarker = 0;     /* 用于复制到TX EVENT FIFO的消息Maker来识别消息状态,范围0-0xFF */

    /* 添加数据到TX FIFO */
    int i = 0;
    int len = 8U;
    for( ; _Len; )
    {
        if(_Len>=8)
        {
            _Len -=8;
        }
        else
        {
            len = _Len;
            _Len = 0;
        }
        TxHeader.DataLength = len<<16;      /* 发送数据长度 */

        while((hfdcan1.Instance->TXFQS)&(1U<<21));   //发送缓冲满了就阻塞
        if(HAL_FDCAN_AddMessageToTxFifoQ(&hfdcan1, &TxHeader, _DataBuf+i)) return 1;
        i +=len;
    }

    return 0;
}

 

测试

int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MPU Configuration--------------------------------------------------------*/
  MPU_Config();

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_FDCAN1_Init();
  /* USER CODE BEGIN 2 */
  FDCAN1_Config();
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
        can1_SendPacket(0x0001, TxData, 41);
        HAL_Delay(1000);
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

 

 

posted @ 2023-04-19 16:59  妖岭  阅读(1534)  评论(0编辑  收藏  举报