/* * Copyright (c) 2022 Talkweb Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include #include #include "stm32f4xx_hal.h" CAN_HandleTypeDef hcan1; void MX_CAN1_Init(void) { hcan1.Instance = CAN1; hcan1.Init.Prescaler = 12; hcan1.Init.Mode = CAN_MODE_NORMAL; hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ; hcan1.Init.TimeSeg1 = CAN_BS1_6TQ; hcan1.Init.TimeSeg2 = CAN_BS2_7TQ; hcan1.Init.TimeTriggeredMode = DISABLE; hcan1.Init.AutoBusOff = DISABLE; hcan1.Init.AutoWakeUp = DISABLE; hcan1.Init.AutoRetransmission = DISABLE; hcan1.Init.ReceiveFifoLocked = DISABLE; hcan1.Init.TransmitFifoPriority = DISABLE; if (HAL_CAN_Init(&hcan1) != HAL_OK) { Error_Handler(); } } static void can_receive_irq(void) { printf("can irq\n"); HAL_CAN_IRQHandler(&hcan1); } /** * @brief CAN MSP Initialization * This function configures the hardware resources used in this example * @param hcan: CAN handle pointer * @retval None */ void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan) { GPIO_InitTypeDef GPIO_InitStruct = {0}; if (hcan->Instance==CAN1) { /* Peripheral clock enable */ __HAL_RCC_CAN1_CLK_ENABLE(); __HAL_RCC_GPIOI_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); /**CAN1 GPIO Configuration PI9 ------> CAN1_RX PB9 ------> CAN1_TX */ GPIO_InitStruct.Pin = GPIO_PIN_9; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; GPIO_InitStruct.Alternate = GPIO_AF9_CAN1; HAL_GPIO_Init(GPIOI, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_PIN_9; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; GPIO_InitStruct.Alternate = GPIO_AF9_CAN1; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); LOS_HwiCreate(CAN1_RX0_IRQn, 0, 0, (HWI_PROC_FUNC)can_receive_irq, 0); } } /** * @brief CAN MSP De-Initialization * This function freeze the hardware resources used in this example * @param hcan: CAN handle pointer * @retval None */ void HAL_CAN_MspDeInit(CAN_HandleTypeDef* hcan) { if (hcan->Instance==CAN1) { /* Peripheral clock disable */ __HAL_RCC_CAN1_CLK_DISABLE(); /**CAN1 GPIO Configuration PI9 ------> CAN1_RX PB9 ------> CAN1_TX */ HAL_GPIO_DeInit(GPIOI, GPIO_PIN_9); HAL_GPIO_DeInit(GPIOB, GPIO_PIN_9); } }