stm32 dfu升级一定要配置成虚拟串口吗?可不可以配置成HID

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了stm32 dfu升级一定要配置成虚拟串口吗?可不可以配置成HID相关的知识,希望对你有一定的参考价值。

参考技术A

可以的,官方有支持通过 DfuSe 工具控制程序跳进 DFU 模式

或者自己写上个hid上位机,网上有例子。在用户程序里实现HID

参考技术B HID、虚拟串口(CDC)和DFU是不同的

APM/STM32F072RB基于HAL库配置USB CDC虚拟串口功能

APM/STM32F072RB基于HAL库配置USB CDC虚拟串口功能


  • 📢采用的自制开发板,开源PCB工程详情放在《极海APM32F072RB开发环境测试
  • ✨本案例基于STM32CubeMX工具配置。
  • 📺使用STM32CubeMX工具配置工程改为APMF072RB型号过程如下:

⛳注意事项

  • ⚡配置的时候将Stack_SizeHeap_Size修改大一点值,避免在使用USB CDC虚拟串口功能不够,导致初始化失败,电脑端加载不出虚拟串口。
  • 🔰在调用CDC_Transmit_FS()打印调试信息的地方包含#include "usbd_cdc_if.h"
  • 如果是使用在极海APM32072RB上,在STM32CubeMX配置完工程后,在Keil设置里,将型号修改为极海APM32072RB型号,否则烧录会报错。不支持直接选择默认的STM32F072RB型号进行烧录。
  • 🍁使用USB CDC虚拟串口功能,在没有USB转串口工具的情况下,可以很方便调试。

📝主程序main.c代码

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @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 "main.h"
#include "usb_device.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "usbd_cdc_if.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 */
unsigned char buff[12] = "hello world";//添加的输出内容
  /* 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 */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USB_DEVICE_Init();
  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
		HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_9);
		  HAL_Delay(1000);//加个ms单位延时
		CDC_Transmit_FS(buff,sizeof(buff));//添加的输出内容
		
  
  /* USER CODE END 3 */


/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)

  RCC_OscInitTypeDef RCC_OscInitStruct = 0;
  RCC_ClkInitTypeDef RCC_ClkInitStruct = 0;
  RCC_PeriphCLKInitTypeDef PeriphClkInit = 0;

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48;
  RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  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_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI48;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  
    Error_Handler();
  
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB;
  PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_HSI48;

  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != 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 */

  • 📜串口监视器调试信息输出

📚工程案例源码

  • 🌼Keil工程里已经将型号修改为APM32F072RB了。
链接:https://pan.baidu.com/s/1xJlYoSlwqCsuXfx2pRadng 
提取码:doxo

以上是关于stm32 dfu升级一定要配置成虚拟串口吗?可不可以配置成HID的主要内容,如果未能解决你的问题,请参考以下文章

STM32F407单片机UART串口gpio口复用功能配置问题

STM32 DFU模式烧录代码

STM32CubeMX学习笔记(50)——USB接口使用(DFU固件升级)

STM32CubeMX学习笔记(50)——USB接口使用(DFU固件升级)

STM32CubeMX学习笔记(50)——USB接口使用(DFU固件升级)

stm32 串口发送数组 cpu可以工作吗