STM32学习记录-USB_CDC(虚拟串口)

文章目录

  • 1、CUBE配置
  • 2、代码验证
  • 3、重定向


1、CUBE配置

STM32学习记录-USB_CDC(虚拟串口)_第1张图片
STM32学习记录-USB_CDC(虚拟串口)_第2张图片
STM32学习记录-USB_CDC(虚拟串口)_第3张图片

2、代码验证

在usbd_cdc_if.c文件添加

static int8_t CDC_Receive_FS(uint8_t* Buf, uint32_t *Len)
{
  /* USER CODE BEGIN 6 */
  USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &Buf[0]);
  USBD_CDC_ReceivePacket(&hUsbDeviceFS);
  //把收到的返回出去
  USBD_CDC_SetTxBuffer(&hUsbDeviceFS, Buf, *Len);
  CDC_Transmit_FS(Buf, *Len);
  return (USBD_OK);
  /* USER CODE END 6 */
}

STM32学习记录-USB_CDC(虚拟串口)_第4张图片

3、重定向

在usbd_cdc_if.c文件最后添加

/* USER CODE BEGIN PRIVATE_FUNCTIONS_IMPLEMENTATION */

#include 
 
void usb_printf(const char *format, ...)
{
    va_list args;
    uint32_t length;
 
    va_start(args, format);
    length = vsnprintf((char *)UserTxBufferFS, APP_TX_DATA_SIZE, (char *)format, args);
    va_end(args);
    CDC_Transmit_FS(UserTxBufferFS, length);
}
/* USER CODE END PRIVATE_FUNCTIONS_IMPLEMENTATION */

在usbd_cdc_if.h文件添加声明

void usb_printf(const char *format, ...);

主函数

#include "main.h"
#include "usb_device.h"
#include "gpio.h"
#include "usbd_cdc_if.h"
extern USBD_HandleTypeDef hUsbDeviceFS;
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* 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 */

  /* 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)
  {
    HAL_Delay(10000);
    usb_printf("测试:%f\n",0.1);
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

STM32学习记录-USB_CDC(虚拟串口)_第5张图片

你可能感兴趣的:(STM32)