初中生
最后登录1970-1-1
在线时间 小时
注册时间2025-2-7
|
开发工具:CUBEIDE
开发板芯片:F407ZGT6
开发板:野火 霸天虎V1

问题如下: CAN回环模式测试,调试过程中监视寄存器发现数据已进入发送邮箱,TSR寄存器的RQCP0和TXOK0值为1,说明发送成功,但是寄存器FIFO0的RF0R寄存器值为0,数据未进入FIFO 已用KEIL下载官方回环例程,官方例程回环模式能收发数据,说明CAN设备正常,有大佬帮忙看看这个是什么问题吗?或者需要怎样调试才能发现问题根源? 求指点
工程附件已上传
已设置筛选器为不过滤,激活FIFO0的接收寄存器,具体代码见后文代码块或附件
寄存器值如下:
 原理图如下:
 通过CUBEIDE设置CAN2如下:
 代码如下 main.c: - /* USER CODE BEGIN Header */
- /**
- ******************************************************************************
- * [url=home.php?mod=space&uid=90986]@file[/url] : main.c
- * [url=home.php?mod=space&uid=41770]@brief[/url] : Main program body
- ******************************************************************************
- * @attention
- *
- * Copyright (c) 2025 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 "main.h"
- #include "can.h"
- #include "usart.h"
- #include "gpio.h"
-
- /* Private includes ----------------------------------------------------------*/
- /* USER CODE BEGIN Includes */
- #include "../../BSP/LED/led.h"
- #include "../../SYSTEM/delay/delay.h"
- #include "../../BSP/BEEF/beef.h"
- #include "../../BSP/KEY/key.h"
- #include "string.h"
- /* USER CODE END Includes */
-
- /* Private typedef -----------------------------------------------------------*/
- /* USER CODE BEGIN PTD */
-
- /* USER CODE END PTD */
-
- /* Private define ------------------------------------------------------------*/
- /* USER CODE BEGIN PD */
-
- /* USER CODE END PD */
-
- /* Private macro -------------------------------------------------------------*/
- /* USER CODE BEGIN PM */
-
- /* USER CODE END PM */
-
- /* Private variables ---------------------------------------------------------*/
-
- /* USER CODE BEGIN PV */
-
- /* USER CODE END PV */
-
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- /* USER CODE BEGIN PFP */
-
- /* USER CODE END PFP */
-
- /* Private user code ---------------------------------------------------------*/
- /* USER CODE BEGIN 0 */
-
- /* USER CODE END 0 */
-
- /**
- * @brief The application entry point.
- * @retval int
- */
- int main(void)
- {
-
- /* USER CODE BEGIN 1 */
- uint8_t key;
- uint8_t i = 0;
- uint8_t cnt = 0;
- uint8_t canbuf[8];
- uint8_t rxlen = 0;
- uint8_t res;
- uint16_t times = 0;
- /* USER CODE END 1 */
-
- /* 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 */
- delay_init(84);
- /* USER CODE END SysInit */
-
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_USART1_UART_Init();
- MX_CAN2_Init();
- /* USER CODE BEGIN 2 */
-
- /* USER CODE END 2 */
-
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- key = key_scan(0);
-
- if(key == KEY2_PRESS)
- {
- printf("Send: ");
- for(i = 0; i<8; i++)
- {
- canbuf[i] = cnt+i;
- printf("%d ", canbuf[i]);
- }
- printf("\r\n");
-
- res = can_send_msg(0x12, canbuf, 8);
-
- if(res)
- {
- printf("Send Failed!\r\n");
- }
- else
- {
- printf("Send Successful!\r\n");
- }
- }
-
- rxlen = can_receive_msg(0x12, canbuf);
- if(rxlen)
- {
- printf("Receive:");
- for(i = 0; i<rxlen; i++)
- {
- printf("%d ", canbuf[i]);
- }
- printf("\r\n");
- }
-
- times++;
- if(times %200 == 0)
- printf("Please Press KEY2 To Send Message!\r\n");
-
-
- if(times % 30 == 0)
- {
- cnt++;
- LED_R_TOGGLE();
- }
-
- delay_ms(10);
-
-
- /* USER CODE END WHILE */
-
- /* USER CODE BEGIN 3 */
- }
- /* USER CODE END 3 */
- }
-
- /**
- * @brief System Clock Configuration
- * @retval None
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct = {0};
- RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
-
- /** Configure the main internal regulator output voltage
- */
- __HAL_RCC_PWR_CLK_ENABLE();
- __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
-
- /** Initializes the RCC Oscillators according to the specified parameters
- * in the RCC_OscInitTypeDef structure.
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
- RCC_OscInitStruct.HSEState = RCC_HSE_ON;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
- RCC_OscInitStruct.PLL.PLLM = 25;
- RCC_OscInitStruct.PLL.PLLN = 336;
- RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
- RCC_OscInitStruct.PLL.PLLQ = 4;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- Error_Handler();
- }
-
- /** Initializes the CPU, AHB and APB buses clocks
- */
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
- |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
-
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
- {
- Error_Handler();
- }
- }
-
- /* USER CODE BEGIN 4 */
-
- /* USER CODE END 4 */
-
- /**
- * @brief This function is executed in case of error occurrence.
- * @retval None
- */
- void Error_Handler(void)
- {
- /* USER CODE BEGIN Error_Handler_Debug */
- /* User can add his own implementation to report the HAL error return state */
- __disable_irq();
- while (1)
- {
- }
- /* USER CODE END Error_Handler_Debug */
- }
-
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t *file, uint32_t line)
- {
- /* USER CODE BEGIN 6 */
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* USER CODE END 6 */
- }
- #endif /* USE_FULL_ASSERT */
复制代码
can.c - /* USER CODE BEGIN Header */
- /**
- ******************************************************************************
- * @file can.c
- * @brief This file provides code for the configuration
- * of the CAN instances.
- ******************************************************************************
- * @attention
- *
- * Copyright (c) 2025 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 */
- CAN_TxHeaderTypeDef g_canx_txheader; /* 发送参数句柄 */
- CAN_RxHeaderTypeDef g_canx_rxheader; /* 接收参数句柄 */
- CAN_FilterTypeDef sFilterConfig;
- /* USER CODE END 0 */
-
- CAN_HandleTypeDef hcan2;
-
- /* CAN2 init function */
- void MX_CAN2_Init(void)
- {
-
- /* USER CODE BEGIN CAN2_Init 0 */
- CAN_FilterTypeDef sFilterConfig; /*配置 CAN 过滤器*/
- /* USER CODE END CAN2_Init 0 */
-
- /* USER CODE BEGIN CAN2_Init 1 */
-
- /* USER CODE END CAN2_Init 1 */
- hcan2.Instance = CAN2;
- hcan2.Init.Prescaler = 4;
- hcan2.Init.Mode = CAN_MODE_LOOPBACK;
- hcan2.Init.SyncJumpWidth = CAN_SJW_1TQ;
- hcan2.Init.TimeSeg1 = CAN_BS1_12TQ;
- hcan2.Init.TimeSeg2 = CAN_BS2_8TQ;
- hcan2.Init.TimeTriggeredMode = DISABLE;
- hcan2.Init.AutoBusOff = DISABLE;
- hcan2.Init.AutoWakeUp = DISABLE;
- hcan2.Init.AutoRetransmission = ENABLE;
- hcan2.Init.ReceiveFifoLocked = DISABLE;
- hcan2.Init.TransmitFifoPriority = DISABLE;
- if (HAL_CAN_Init(&hcan2) != HAL_OK)
- {
- Error_Handler();
- }
- /* USER CODE BEGIN CAN2_Init 2 */
- //配置过滤器
- sFilterConfig.FilterBank = 0; // 过滤器组编号
- sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK; // 过滤器模式
- sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT; // 过滤器尺度
- sFilterConfig.FilterIdHigh = 0x0000; // 过滤器 ID 高 16 位
- sFilterConfig.FilterIdLow = 0x0000; // 过滤器 ID 低 16 位
- sFilterConfig.FilterMaskIdHigh = 0x0000; // 过滤器掩码高 16 位
- sFilterConfig.FilterMaskIdLow = 0x0000; // 过滤器掩码低 16 位
- sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0; // 过滤器分配给 FIFO 0
- sFilterConfig.FilterActivation = ENABLE; // 使能过滤器
- sFilterConfig.SlaveStartFilterBank = 14; // CAN2 的过滤器起始位置
-
- if(HAL_CAN_ConfigFilter(&hcan2, &sFilterConfig) != HAL_OK)
- { /*过滤器配置*/
- Error_Handler();
- }
-
- if(HAL_CAN_Start(&hcan2) != HAL_OK)
- { /*启动CAN外围设备*/
- Error_Handler();
- }
-
- /*使能中断接收,FIFO0消息挂号中断允许,选则CAN外设中断源为“ CAN 接收 FIFO 0 消息挂起中断”,
- * 同时需要使能打开CAN的中断响应,此处未写是因在可视化界面已配置,
- * 会在HAL_CAN_Init初始化时调用的HAL_CAN_MspInit函数中进行使能*/
- //__HAL_CAN_ENABLE_IT(&hcan2, CAN_IT_RX_FIFO0_MSG_PENDING);
- HAL_CAN_ActivateNotification(&hcan2, CAN_IT_RX_FIFO0_MSG_PENDING);
-
-
-
-
- /* USER CODE END CAN2_Init 2 */
-
- }
-
- void HAL_CAN_MspInit(CAN_HandleTypeDef* canHandle)
- {
-
- GPIO_InitTypeDef GPIO_InitStruct = {0};
- if(canHandle->Instance==CAN2)
- {
- /* USER CODE BEGIN CAN2_MspInit 0 */
-
- /* USER CODE END CAN2_MspInit 0 */
- /* CAN2 clock enable */
- __HAL_RCC_CAN2_CLK_ENABLE();
- __HAL_RCC_CAN1_CLK_ENABLE();
-
- __HAL_RCC_GPIOB_CLK_ENABLE();
- /**CAN2 GPIO Configuration
- PB12 ------> CAN2_RX
- PB13 ------> CAN2_TX
- */
- GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13;
- 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_CAN2;
- HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
-
- /* CAN2 interrupt Init */
- HAL_NVIC_SetPriority(CAN2_RX0_IRQn, 1, 0);
- HAL_NVIC_EnableIRQ(CAN2_RX0_IRQn);
- /* USER CODE BEGIN CAN2_MspInit 1 */
-
-
- /* USER CODE END CAN2_MspInit 1 */
- }
- }
-
- void HAL_CAN_MspDeInit(CAN_HandleTypeDef* canHandle)
- {
-
- if(canHandle->Instance==CAN2)
- {
- /* USER CODE BEGIN CAN2_MspDeInit 0 */
-
- /* USER CODE END CAN2_MspDeInit 0 */
- /* Peripheral clock disable */
- __HAL_RCC_CAN2_CLK_DISABLE();
- __HAL_RCC_CAN1_CLK_DISABLE();
-
- /**CAN2 GPIO Configuration
- PB12 ------> CAN2_RX
- PB13 ------> CAN2_TX
- */
- HAL_GPIO_DeInit(GPIOB, GPIO_PIN_12|GPIO_PIN_13);
-
- /* CAN2 interrupt Deinit */
- HAL_NVIC_DisableIRQ(CAN2_RX0_IRQn);
- /* USER CODE BEGIN CAN2_MspDeInit 1 */
-
- /* USER CODE END CAN2_MspDeInit 1 */
- }
- }
-
- /* USER CODE BEGIN 1 */
- /**
- * * @brief CAN 发送一组数据
- * * [url=home.php?mod=space&uid=87825]@note[/url] 发送格式固定为: 标准 ID, 数据帧
- * * @param id : 标准 ID(11 位)
- * * @retval 发送状态 0, 成功; 1, 失败;
- * */
- uint8_t can_send_msg(uint32_t id, uint8_t *msg, uint8_t len)
- {
- uint32_t TxMailBox = CAN_TX_MAILBOX0;
-
- g_canx_txheader.StdId = id; /*标准标识符*/
- g_canx_txheader.ExtId = id; /*扩展标识符(29位)*/
- g_canx_txheader.IDE = CAN_ID_STD; /* 使用标准帧 */
- g_canx_txheader.RTR = CAN_RTR_DATA; /* 数据帧 */
- g_canx_txheader.DLC = len;
-
- if(HAL_CAN_AddTxMessage(&hcan2, &g_canx_txheader, msg, &TxMailBox) != HAL_OK)
- {
- return 1;
- }
-
- /*等待发送完成,所有邮箱为空(3个邮箱)*/
- while(HAL_CAN_GetTxMailboxesFreeLevel(&hcan2) !=3);
-
- return 0;
-
- }
-
- /** * @brief CAN 接收数据查询
- * * @note 接收数据格式固定为: 标准 ID, 数据帧
- * * @param id : 要查询的 标准 ID(11 位)
- * * @param buf : 数据缓存区
- * * @retval 接收结果
- * * @arg 0 , 无数据被接收到;
- * * @arg 其他, 接收的数据长度
- * */
- uint8_t can_receive_msg(uint32_t id, uint8_t *buf)
- {
- if(HAL_CAN_GetRxFifoFillLevel(&hcan2, CAN_RX_FIFO0) == 0)
- {
- return 0;
- }
- if(HAL_CAN_GetRxMessage(&hcan2, CAN_RX_FIFO0, &g_canx_rxheader, buf) != HAL_OK)
- {
- return 0;
- }
-
- /*接收到的ID不对/不是标准帧/不是数据帧*/
- if(g_canx_rxheader.StdId != id || g_canx_rxheader.IDE != CAN_ID_STD || g_canx_rxheader.RTR != CAN_RTR_DATA)
- {
- return 0;
- }
-
- return g_canx_rxheader.DLC;
- }
- /* USER CODE END 1 */
复制代码
|
|