简体   繁体   English

STM32F4 的 USB CDC 设置和库出现问题

[英]Problem with USB CDC settings and libraries for STM32F4

I'm working in an embedded aplication for STM32F401RBT6 and I'm trying to establish a connection with the PC (Windows 10) but the device is not recognized by the system.我正在为STM32F401RBT6嵌入式应用程序,我正在尝试与 PC(Windows 10)建立连接,但系统无法识别该设备。 The code that I generated by STMCubeMX and debugged by Atollic not works.我由STMCubeMX生成并由Atollic调试的代码不起作用。 I saw and try reproduce several examples, but anything works.我看到并尝试重现几个示例,但任何方法都有效。 In the code, I have all libraries that i think necessary.在代码中,我有我认为必要的所有库。

档案 档案2

I'm have this archives generated by STMCubeMX for the CDC comunication, but I'm newbie and I don't know what I have to modify on the code for the USB be recognized by the system.我有这个由STMCubeMXCDC通信生成的档案,但我是新手,我不知道我必须对系统识别的 USB 的代码进行哪些修改。 Someone can help me?有人可以帮助我吗?

Inside USBD_CDC_Init(..) function there is a malloc function which allocates about 540 Bytes memory on the Heap. Inside USBD_CDC_Init(..) function there is a malloc function which allocates about 540 Bytes memory on the Heap.

This was not taken into account from CubeMX when code generated.生成代码时,CubeMX 没有考虑到这一点。

So, at least you must define the Heap size taking into account theese extra Bytes, to have the USB CDC port working.因此,至少您必须在考虑这些额外字节的情况下定义堆大小,以使 USB CDC 端口正常工作。

Beside the point from Soup ( the failing malloc caused by a heap that is only 0x200 by default) some Windows version have a problem with the line coding in the example.除了 Soup(默认情况下只有 0x200 的堆导致的失败 malloc)之外,某些 Windows 版本在示例中的行编码有问题。

In the usbd_cdc_if.c you should add:在 usbd_cdc_if.c 中,您应该添加:

/* USER CODE BEGIN PRIVATE_VARIABLES */
USBD_CDC_LineCodingTypeDef LineCoding =
    {
    115200, /* baud rate*/
    0x00,   /* stop bits-1*/
    0x00,   /* parity - none*/
    0x08    /* nb. of bits 8*/
    };

And a little bit below下面一点

static int8_t CDC_Control_FS(uint8_t cmd, uint8_t* pbuf, uint16_t length)
.
.
.
case CDC_SET_LINE_CODING:
    LineCoding.bitrate    = (uint32_t)(pbuf[0] | (pbuf[1] << 8) |\
                            (pbuf[2] << 16) | (pbuf[3] << 24));
    LineCoding.format     = pbuf[4];
    LineCoding.paritytype = pbuf[5];
    LineCoding.datatype   = pbuf[6];

break;

case CDC_GET_LINE_CODING:
    pbuf[0] = (uint8_t)(LineCoding.bitrate);
    pbuf[1] = (uint8_t)(LineCoding.bitrate >> 8);
    pbuf[2] = (uint8_t)(LineCoding.bitrate >> 16);
    pbuf[3] = (uint8_t)(LineCoding.bitrate >> 24);
    pbuf[4] = LineCoding.format;
    pbuf[5] = LineCoding.paritytype;
    pbuf[6] = LineCoding.datatype;
break;

So the host won't get undefined data if he tries to set the line coding.因此,如果主机尝试设置线路编码,他将不会得到未定义的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM