/** * @brief CAN MSP Initialization * This function configures the hardware resources used in this example: * - Peripheral's clock enable * - Peripheral's GPIO Configuration * @param hcan: CAN handle pointer * @retval None */ void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan) { GPIO_InitTypeDef GPIO_InitStruct; /*##-1- Enable peripherals and GPIO Clocks #################################*/ /* CAN1 Periph clock enable */ CANx_CLK_ENABLE(); /* Enable GPIO clock ****************************************/ CANx_GPIO_CLK_ENABLE(); /*##-2- Configure peripheral GPIO ##########################################*/ /* CAN1 TX GPIO pin configuration */ GPIO_InitStruct.Pin = CANx_TX_PIN; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Speed = GPIO_SPEED_FAST; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Alternate = CANx_TX_AF; HAL_GPIO_Init(CANx_TX_GPIO_PORT, &GPIO_InitStruct); /* CAN1 RX GPIO pin configuration */ GPIO_InitStruct.Pin = CANx_RX_PIN; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Speed = GPIO_SPEED_FAST; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Alternate = CANx_RX_AF; HAL_GPIO_Init(CANx_TX_GPIO_PORT, &GPIO_InitStruct); }
/** * @brief CAN MSP Initialization * This function configures the hardware resources used in this example: * - Peripheral's clock enable * - Peripheral's GPIO Configuration * - NVIC configuration for DMA interrupt request enable * @param hcan: CAN handle pointer * @retval None */ void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan) { GPIO_InitTypeDef GPIO_InitStruct; /*##-1- Enable peripherals and GPIO Clocks #################################*/ /* CAN1 Periph clock enable */ CANx_CLK_ENABLE(); /* Enable GPIO clock ****************************************/ CANx_GPIO_CLK_ENABLE(); /*##-2- Configure peripheral GPIO ##########################################*/ /* CAN1 TX GPIO pin configuration */ GPIO_InitStruct.Pin = CANx_TX_PIN; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Speed = GPIO_SPEED_FAST; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Alternate = CANx_TX_AF; HAL_GPIO_Init(CANx_TX_GPIO_PORT, &GPIO_InitStruct); /* CAN1 RX GPIO pin configuration */ GPIO_InitStruct.Pin = CANx_RX_PIN; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Speed = GPIO_SPEED_FAST; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Alternate = CANx_RX_AF; HAL_GPIO_Init(CANx_TX_GPIO_PORT, &GPIO_InitStruct); /*##-3- Configure the NVIC #################################################*/ /* NVIC configuration for CAN1 Reception complete interrupt */ HAL_NVIC_SetPriority(CANx_RX_IRQn, 1, 0); HAL_NVIC_EnableIRQ(CANx_RX_IRQn); }
/** * @brief Enables the CAN2 interface with the current settings * @param None * @retval None */ static ErrorStatus prvEnableCan2Interface() { /*##-1- Enable peripheral Clocks ###########################################*/ /* CAN2 Peripheral clock enable */ CANx_CLK_ENABLE(); /*##-2- Configure the NVIC #################################################*/ /* NVIC configuration for CAN2 Reception complete interrupt */ HAL_NVIC_SetPriority(CANx_RX_IRQn, configLIBRARY_LOWEST_INTERRUPT_PRIORITY, 0); HAL_NVIC_EnableIRQ(CANx_RX_IRQn); /*##-3- Configure the CAN peripheral #######################################*/ if (HAL_CAN_Init(&CAN_Handle) != HAL_OK) { /* Initialization Error */ goto error; } /*##-4- Configure the CAN Filter ###########################################*/ if (HAL_CAN_ConfigFilter(&CAN_Handle, &CAN_Filter) != HAL_OK) { /* Filter configuration Error */ goto error; } /*##-5- Configure Transmission process #####################################*/ CAN_Handle.pTxMsg->StdId = 0x321; CAN_Handle.pTxMsg->ExtId = 0x01; CAN_Handle.pTxMsg->RTR = CAN_RTR_DATA; CAN_Handle.pTxMsg->IDE = CAN_ID_STD; CAN_Handle.pTxMsg->DLC = 2; /*##-6- Start the Reception process and enable reception interrupt #########*/ if (HAL_CAN_Receive_IT(&CAN_Handle, CAN_FIFO0) != HAL_OK) { /* Reception Error */ goto error; } return SUCCESS; error: /* Something went wrong so disable */ prvDisableCan2Interface(); return ERROR; }
void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan) { (void)hcan; __HAL_RCC_CAN1_CLK_ENABLE(); // Must always enable CAN1 clk even if we only use CAN2 { __HAL_RCC_CAN1_CLK_ENABLE(); __HAL_RCC_CAN2_CLK_ENABLE(); } CANx_CLK_ENABLE(); CANx_GPIO_CLK_ENABLE(); HAL_GPIO_Init(CANx_TX_GPIO_PORT, &(GPIO_InitTypeDef){ .Pin = CANx_TX_PIN, .Mode = GPIO_MODE_AF_PP, .Speed = GPIO_SPEED_FAST, .Pull = GPIO_NOPULL, //GPIO_PULLUP, .Alternate = CANx_TX_AF, });