123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- /* USER CODE BEGIN Header */
- /**
- ******************************************************************************
- * @file can.c
- * @brief This file provides code for the configuration
- * of the CAN instances.
- ******************************************************************************
- * @attention
- *
- * Copyright (c) 2024 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 "can.h"
- /* USER CODE BEGIN 0 */
- void MX_CAN1_Filer(void)
- {
- CAN_FilterTypeDef CAN1_FilerConf;
- CAN1_FilerConf.FilterIdHigh = 0; // 过滤掉不是发送给本机的数据帧//32位ID
- CAN1_FilerConf.FilterIdLow = 0; // 确保收到的是标准数据帧
- CAN1_FilerConf.FilterMaskIdHigh = 0; // 32位MASK
- CAN1_FilerConf.FilterMaskIdLow = 0; // 0xFFFF;
- CAN1_FilerConf.FilterFIFOAssignment = CAN_FILTER_FIFO0; // 过滤器0关联到FIFO0
- CAN1_FilerConf.FilterBank = 0; // 过滤器0
- CAN1_FilerConf.FilterMode = CAN_FILTERMODE_IDMASK; // 屏蔽位模式
- CAN1_FilerConf.FilterScale = CAN_FILTERSCALE_32BIT;
- CAN1_FilerConf.FilterActivation = ENABLE;
- CAN1_FilerConf.SlaveStartFilterBank = 14;
- if (HAL_CAN_ConfigFilter(&hcan, &CAN1_FilerConf) != HAL_OK)
- {
- Error_Handler();
- }
- }
- /* USER CODE END 0 */
- CAN_HandleTypeDef hcan;
- /* CAN init function */
- void MX_CAN_Init(void)
- {
- /* USER CODE BEGIN CAN_Init 0 */
- /* USER CODE END CAN_Init 0 */
- /* USER CODE BEGIN CAN_Init 1 */
- /* USER CODE END CAN_Init 1 */
- hcan.Instance = CAN1;
- hcan.Init.Prescaler = 18;
- hcan.Init.Mode = CAN_MODE_NORMAL;
- hcan.Init.SyncJumpWidth = CAN_SJW_1TQ;
- hcan.Init.TimeSeg1 = CAN_BS1_13TQ;
- hcan.Init.TimeSeg2 = CAN_BS2_2TQ;
- hcan.Init.TimeTriggeredMode = DISABLE;
- hcan.Init.AutoBusOff = ENABLE;
- hcan.Init.AutoWakeUp = DISABLE;
- hcan.Init.AutoRetransmission = DISABLE;
- hcan.Init.ReceiveFifoLocked = DISABLE;
- hcan.Init.TransmitFifoPriority = DISABLE;
- if (HAL_CAN_Init(&hcan) != HAL_OK)
- {
- Error_Handler();
- }
- /* USER CODE BEGIN CAN_Init 2 */
- MX_CAN1_Filer();
- if (HAL_CAN_Start(&hcan) != HAL_OK)
- {
- Error_Handler();
- }
- if (HAL_CAN_ActivateNotification(&hcan, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK)
- {
- Error_Handler();
- }
- /* USER CODE END CAN_Init 2 */
- }
- void HAL_CAN_MspInit(CAN_HandleTypeDef *canHandle)
- {
- GPIO_InitTypeDef GPIO_InitStruct = {0};
- if (canHandle->Instance == CAN1)
- {
- /* USER CODE BEGIN CAN1_MspInit 0 */
- /* USER CODE END CAN1_MspInit 0 */
- /* CAN1 clock enable */
- __HAL_RCC_CAN1_CLK_ENABLE();
- __HAL_RCC_GPIOB_CLK_ENABLE();
- /**CAN GPIO Configuration
- PB8 ------> CAN_RX
- PB9 ------> CAN_TX
- */
- GPIO_InitStruct.Pin = GPIO_PIN_8;
- GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
- GPIO_InitStruct.Pull = GPIO_PULLUP;
- HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
- GPIO_InitStruct.Pin = GPIO_PIN_9;
- GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
- HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
- __HAL_AFIO_REMAP_CAN1_2();
- /* CAN1 interrupt Init */
- HAL_NVIC_SetPriority(USB_HP_CAN1_TX_IRQn, 0, 0);
- HAL_NVIC_EnableIRQ(USB_HP_CAN1_TX_IRQn);
- HAL_NVIC_SetPriority(USB_LP_CAN1_RX0_IRQn, 0, 0);
- HAL_NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn);
- /* USER CODE BEGIN CAN1_MspInit 1 */
- /* USER CODE END CAN1_MspInit 1 */
- }
- }
- void HAL_CAN_MspDeInit(CAN_HandleTypeDef *canHandle)
- {
- if (canHandle->Instance == CAN1)
- {
- /* USER CODE BEGIN CAN1_MspDeInit 0 */
- /* USER CODE END CAN1_MspDeInit 0 */
- /* Peripheral clock disable */
- __HAL_RCC_CAN1_CLK_DISABLE();
- /**CAN GPIO Configuration
- PB8 ------> CAN_RX
- PB9 ------> CAN_TX
- */
- HAL_GPIO_DeInit(GPIOB, GPIO_PIN_8 | GPIO_PIN_9);
- /* CAN1 interrupt Deinit */
- /* USER CODE BEGIN CAN1:USB_HP_CAN1_TX_IRQn disable */
- /**
- * Uncomment the line below to disable the "USB_HP_CAN1_TX_IRQn" interrupt
- * Be aware, disabling shared interrupt may affect other IPs
- */
- /* HAL_NVIC_DisableIRQ(USB_HP_CAN1_TX_IRQn); */
- /* USER CODE END CAN1:USB_HP_CAN1_TX_IRQn disable */
- /* USER CODE BEGIN CAN1:USB_LP_CAN1_RX0_IRQn disable */
- /**
- * Uncomment the line below to disable the "USB_LP_CAN1_RX0_IRQn" interrupt
- * Be aware, disabling shared interrupt may affect other IPs
- */
- /* HAL_NVIC_DisableIRQ(USB_LP_CAN1_RX0_IRQn); */
- /* USER CODE END CAN1:USB_LP_CAN1_RX0_IRQn disable */
- /* USER CODE BEGIN CAN1_MspDeInit 1 */
- /* USER CODE END CAN1_MspDeInit 1 */
- }
- }
- /* USER CODE BEGIN 1 */
- /* USER CODE END 1 */
|