stm32笔记[16]-使用usb-cdc串口.md

摘要

在stm32f103cbt6核心板使用usb cdc虚拟串口,回环发送的字符串.

关键信息

  • STM32CubeIDE
  • JLINK
  • stm32f103cbt6
  • 外部晶振:8MHz

原理简介

usb-cdc简介

[https://blog.csdn.net/weixin_52296952/article/details/135776171]
[https://www.usbzh.com/article/detail-842.html]
[https://usb.org/sites/default/files/CDC1.2_WMC1.1_012011.zip]
USB通信设备类CDC分类编号为0x0a,且必须指定为接口设备类。

USB 通信设备类(或USB CDC类)是一个复合通用串行总线 设备类。
通信设备类用于类似于网卡的计算机网络设备,提供用于将以太网或ATM帧传输到某些物理介质的接口。它还用于调制解调器、ISDN、传真机和电话应用程序以执行常规语音呼叫。
通信设备具有三个基本任务:

设备管理(控制配置特定设备并通知 USB 主机某些事件)
呼叫管理(建立和终止电话呼叫或其他连接)
数据传输(发送和接收应用数据)

USB 组件中的 CDC 实现具有:

使用CDC的ACM(抽象控制模型)子类模拟虚拟 COM 端口。
使用CDC的ACM(抽象控制模型)子类使用RDNIS协议模拟网络连接。这支持Windows 主机 PC 和嵌入式设备之间的 USB 网络连接,以及USB 设备 RNDIS 到以太网桥应用程序。
使用 CDC 的NCM(网络控制模型)子类模拟以太网适配器(仅适用于USB 设备)。使用 CDC (NCM),您可以在基于 Linux 的主机系统上创建Ethernet-over-USB(适用于 Linux 主机)应用程序。

实现

  1. Connective->USB开启USB Device(FS)
  2. Middleware->USB_DEVICE->开启Communication Device Class
  3. 配置时钟(参数如下)
    USB部分时钟必须是48MHz
/* ########################## Oscillator Values adaptation ####################*/
/**
  * @brief Adjust the value of External High Speed oscillator (HSE) used in your application.
  *        This value is used by the RCC HAL module to compute the system frequency
  *        (when HSE is used as system clock source, directly or through the PLL).
  */
#if !defined  (HSE_VALUE)
  #define HSE_VALUE    8000000U /*!< Value of the External oscillator in Hz */
#endif /* HSE_VALUE */

#if !defined  (HSE_STARTUP_TIMEOUT)
  #define HSE_STARTUP_TIMEOUT    100U   /*!< Time out for HSE start up, in ms */
#endif /* HSE_STARTUP_TIMEOUT */

/**
  * @brief Internal High Speed oscillator (HSI) value.
  *        This value is used by the RCC HAL module to compute the system frequency
  *        (when HSI is used as system clock source, directly or through the PLL).
  */
#if !defined  (HSI_VALUE)
  #define HSI_VALUE    8000000U /*!< Value of the Internal oscillator in Hz*/
#endif /* HSI_VALUE */

/**
  * @brief Internal Low Speed oscillator (LSI) value.
  */
#if !defined  (LSI_VALUE)
 #define LSI_VALUE               40000U    /*!< LSI Typical Value in Hz */
#endif /* LSI_VALUE */                     /*!< Value of the Internal Low Speed oscillator in Hz
                                                The real value may vary depending on the variations
                                                in voltage and temperature. */

/**
  * @brief External Low Speed oscillator (LSE) value.
  *        This value is used by the UART, RTC HAL module to compute the system frequency
  */
#if !defined  (LSE_VALUE)
  #define LSE_VALUE    32768U /*!< Value of the External oscillator in Hz*/
#endif /* LSE_VALUE */

#if !defined  (LSE_STARTUP_TIMEOUT)
  #define LSE_STARTUP_TIMEOUT    5000U   /*!< Time out for LSE start up, in ms */
#endif /* LSE_STARTUP_TIMEOUT */

/* Tip: To avoid modifying this file each time you need to use different HSE,
   ===  you can define the HSE value in your toolchain compiler preprocessor. */
时钟图
  1. 修改代码
    USB_DEVICE/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);
  CDC_Transmit_FS(Buf, *Len); // 回传
  return (USBD_OK);
  /* USER CODE END 6 */
}
  1. 编译&烧录
    生成hex和bin文件方法:C/C++ Build->Settings->MCU Post Build outputs->convert to binary file
使用jflash烧录二进制

烧录完成后重新上电.

效果

虚拟串口输出
posted @ 2024-06-15 01:53  qsBye  阅读(21)  评论(0编辑  收藏  举报