/* 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 */ /* USER CODE END 0 */ FDCAN_HandleTypeDef hfdcan2; /* FDCAN2 init function */ void MX_FDCAN2_Init(void) { /* USER CODE BEGIN FDCAN2_Init 0 */ /* 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_FD_NO_BRS; hfdcan2.Init.Mode = FDCAN_MODE_NORMAL; hfdcan2.Init.AutoRetransmission = DISABLE; hfdcan2.Init.TransmitPause = DISABLE; hfdcan2.Init.ProtocolException = DISABLE; hfdcan2.Init.NominalPrescaler = 10; hfdcan2.Init.NominalSyncJumpWidth = 1; hfdcan2.Init.NominalTimeSeg1 = 5; hfdcan2.Init.NominalTimeSeg2 = 2; hfdcan2.Init.DataPrescaler = 10; hfdcan2.Init.DataSyncJumpWidth = 1; hfdcan2.Init.DataTimeSeg1 = 5; hfdcan2.Init.DataTimeSeg2 = 2; hfdcan2.Init.MessageRAMOffset = 0; hfdcan2.Init.StdFiltersNbr = 0; hfdcan2.Init.ExtFiltersNbr = 0; hfdcan2.Init.RxFifo0ElmtsNbr = 32; hfdcan2.Init.RxFifo0ElmtSize = FDCAN_DATA_BYTES_64; hfdcan2.Init.RxFifo1ElmtsNbr = 0; hfdcan2.Init.RxFifo1ElmtSize = FDCAN_DATA_BYTES_64; hfdcan2.Init.RxBuffersNbr = 0; hfdcan2.Init.RxBufferSize = FDCAN_DATA_BYTES_64; hfdcan2.Init.TxEventsNbr = 0; hfdcan2.Init.TxBuffersNbr = 0; hfdcan2.Init.TxFifoQueueElmtsNbr = 32; hfdcan2.Init.TxFifoQueueMode = FDCAN_TX_FIFO_OPERATION; hfdcan2.Init.TxElmtSize = FDCAN_DATA_BYTES_64; if (HAL_FDCAN_Init(&hfdcan2) != HAL_OK) { Error_Handler(); } /* USER CODE BEGIN FDCAN2_Init 2 */ if (HAL_FDCAN_ActivateNotification(&hfdcan2, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0) != HAL_OK) { Error_Handler(); } if (HAL_FDCAN_ActivateNotification(&hfdcan2, FDCAN_IT_BUS_OFF, 0) != HAL_OK) { Error_Handler(); } HAL_FDCAN_Start(&hfdcan2); /* USER CODE END FDCAN2_Init 2 */ } void HAL_FDCAN_MspInit(FDCAN_HandleTypeDef* fdcanHandle) { GPIO_InitTypeDef GPIO_InitStruct = {0}; RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0}; 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_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); /**FDCAN2 GPIO Configuration PB5 ------> FDCAN2_RX PB6 ------> FDCAN2_TX */ GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6; 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, 0, 0); HAL_NVIC_EnableIRQ(FDCAN2_IT0_IRQn); /* USER CODE BEGIN FDCAN2_MspInit 1 */ /* USER CODE END FDCAN2_MspInit 1 */ } } void HAL_FDCAN_MspDeInit(FDCAN_HandleTypeDef* fdcanHandle) { if(fdcanHandle->Instance==FDCAN2) { /* USER CODE BEGIN FDCAN2_MspDeInit 0 */ /* USER CODE END FDCAN2_MspDeInit 0 */ /* Peripheral clock disable */ __HAL_RCC_FDCAN_CLK_DISABLE(); /**FDCAN2 GPIO Configuration PB5 ------> FDCAN2_RX PB6 ------> FDCAN2_TX */ HAL_GPIO_DeInit(GPIOB, GPIO_PIN_5|GPIO_PIN_6); /* FDCAN2 interrupt Deinit */ HAL_NVIC_DisableIRQ(FDCAN2_IT0_IRQn); /* USER CODE BEGIN FDCAN2_MspDeInit 1 */ /* USER CODE END FDCAN2_MspDeInit 1 */ } } /* USER CODE BEGIN 1 */ /* USER CODE END 1 */