本文介绍了STM32CubeMX USB CDC VCP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了大量的例子,但没有关于如何从 STM32MXCube 中正确"做到这一点.

I've found large number of examples, but nothing on how to do it "properly" from STM32MXCube.

如何从 STM32CubeMX 为 USB CDC 虚拟 COM 端口通信创建骨架代码(如果可能,STM32F4 Discovery)?

How do I create skeleton code from STM32CubeMX for USB CDC virtual COM port communications (if possible STM32F4 Discovery)?

推荐答案

以 CDC 作为 USB 设备的 Discovery F4 STM32CubeMX 项目应该是开箱即用的.假设您使用最新的 STM32CubeMX 和库:

A STM32CubeMX project for Discovery F4 with CDC as USB device should work out of the box. Assuming you use an up-to-date STM32CubeMX and library:

  • 启动 STM32CubeMX
  • 选择板 Discovery F4
  • 仅启用外围 UBS_OTG_FS 设备(不选中多余的东西)
  • 启用中间件 USB_Device 通信 .. .aka CDC

在时钟选项卡中检查时钟源是 HSE HCLK.它应在 48 MHz (USB) 中提供 168 MHz HLCK 和 48 MHz.检查任何地方都没有红色.

In the clock tab check the clock source is HSE HCLK. It shall give 168 MHz HLCK and 48 MHz in the 48 MHz (USB). Check there is no red anywhere.

保存项目

生成代码(我使用了 SW4STM32 工具链)

Generate code (I used SW4STM32 toolchains)

构建(您可能需要切换到内部 CDT 构建器与 GNU make).

Build (you may need to switch to internal CDT builder vs. GNU make).

现在添加一些代码来通过 COM 端口发送数据,瞧它应该可以工作了.

Now add some code to send data over the COM port and voila it should work.

实际上,棘手的部分是在主机 USB 连接之前不要尝试进行任何CDC"访问(尚未设置 CDC)

Actually, the tricky part is not try to make any "CDC" access until the host USB connects (no CDC setup yet)

这是我如何进行快速发射测试:

Here is how I did it for quick emit test:

uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len)
{
    uint8_t result = USBD_OK;

    /* USER CODE BEGIN 7 */
    if (hUsbDevice_0 == NULL)
        return -1;

    USBD_CDC_SetTxBuffer(hUsbDevice_0, Buf, Len);
    result = USBD_CDC_TransmitPacket(hUsbDevice_0);
    /* USER CODE END 7 */

    return result;
}

static int8_t CDC_DeInit_FS(void)
{
    /* USER CODE BEGIN 4 */
    hUsbDevice_0 = NULL;
    return (USBD_OK);
    /* USER CODE END 4 */
}

在文件 main.c 中

/* USER CODE BEGIN Includes */
#include "usbd_cdc_if.h"
/* USER CODE END Includes */
....

/* USER CODE BEGIN WHILE */
while (1)
{
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
    uint8_t HiMsg[] = "hello\r\n";
    CDC_Transmit_FS(HiMsg, strlen(HiMsg));
    HAL_Delay(200);
}

只要您插入 micro USB (CN5) CDC 数据就会开始显示在主机终端上.

As soon you plug the micro USB (CN5) CDC data will start to show on the host terminal.

那行得通.我可以在终端上看到hello"(您可能需要安装驱动程序,http://www.st.com/web/en/catalog/tools/PF257938).

That works. I can see "hello" on the terminal (you may need to install a driver, http://www.st.com/web/en/catalog/tools/PF257938).

对于接收,它需要首先准备好,比如说,在一个合适的地方通过第一次调用 USBD_CDC_ReceivePacket() 来启动.为此,它可以是 CDC_Init_FS.

For reception, it needs to be first armed, say, started by a first call to USBD_CDC_ReceivePacket() in a good place. For that it can be CDC_Init_FS.

然后您可以在数据到达 CDC_Receive_FS 时对其进行处理,并从这里重新重新接收.

Then you can handle data as it arrives in CDC_Receive_FS and rearming reception again from here.

这对我有用.

static int8_t CDC_Receive_FS (uint8_t* Buf, uint32_t *Len)
{
    /* USER CODE BEGIN 6 */
    USBD_CDC_ReceivePacket(hUsbDevice_0);
    return (USBD_OK);
    /* USER CODE END 6 */
}

static int8_t CDC_Init_FS(void)
{
    hUsbDevice_0 = &hUsbDeviceFS;

    /* USER CODE BEGIN 3 */
    /* Set Application Buffers */
    USBD_CDC_SetTxBuffer(hUsbDevice_0, UserTxBufferFS, 0);
    USBD_CDC_SetRxBuffer(hUsbDevice_0, UserRxBufferFS);
    USBD_CDC_ReceivePacket(hUsbDevice_0);

    return (USBD_OK);
    /* USER CODE END 3 */
}

这篇关于STM32CubeMX USB CDC VCP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-14 11:50