USB的Device开发:还是ST的STM32CubeMX好用。

2020-01-04 19:00发布

用STM32CubeMX设置"mode"为Device_Only,"class for FS IP"为Communication Device class.
生成代码只在几个地方修改一点代码,自己写的不超过50行代码,PC就可以识别为串口了。

1.Heap Size 要加大。

2.usbd_cdc_if.c 中要加上:
1)
USBD_CDC_LineCodingTypeDef LineCoding;

2)
#define APP_RX_DATA_SIZE  64
#define APP_TX_DATA_SIZE  64


3)
  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;

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

3.  stmcdc.inf文件
;
; STMicroelectronics Comunication Device Class driver instalation file
; (C)2006 Copyright STMicroelectronics
;

[Version]
Signature="$Windows NT$"
Class=Ports
ClassGuid={4D36E978-E325-11CE-BFC1-08002BE10318}
Provider=%STM%
LayoutFile=layout.inf
DriverVer=10/02/06

[Manufacturer]
%STM%=DeviceList

[DestinationDirs]
DefaultDestDir=12

[SourceDisksFiles]

[SourceDisksNames]

[DeviceList]
%DESCRIPTION%=STMUSB, USBVID_0483&PID_5740

;------------------------------------------------------------------------------
;  Windows 2000/XP Sections
;------------------------------------------------------------------------------

[STMUSB.nt]
include=mdmcpq.inf
CopyFiles=DriverCopyFiles
AddReg=STMUSB.nt.AddReg

[DriverCopyFiles]
usbser.sys,,,0x20

[STMUSB.nt.AddReg]
HKR,,DevLoader,,*ntkern
HKR,,NTMPDriver,,usbser.sys
HKR,,EnumPropPages32,,"MsPorts.dll,SerialPortPropPageProvider"

[STMUSB.nt.Services]
AddService=usbser, 0x00000002, DriverService


[STMUSB.nt.HW]
include=mdmcpq.inf

[DriverService]
DisplayName=%DESCRIPTION%
ServiceType=1
StartType=3
ErrorControl=1
ServiceBinary=%12%usbser.sys

;------------------------------------------------------------------------------
;  String Definitions
;------------------------------------------------------------------------------

[Strings]
STM="STMicroelectronics"
DESCRIPTION="STM32 Virtual COM Port"
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
27条回答
huangqi412
1楼-- · 2020-01-06 01:39
还没用过这个图形配置代码
yixuanyuxiao
2楼-- · 2020-01-06 07:32
usb 串口。mark
dulala
3楼-- · 2020-01-06 12:18
这个不错,加到工程中试一下
kaomantou
4楼-- · 2020-01-06 15:35
这个usb模拟串口后,PC端还要写驱动程序么?
bbs2009
5楼-- · 2020-01-06 20:35
 精彩回答 2  元偷偷看……
lyl520719
6楼-- · 2020-01-07 02:34
谢谢分享,

一周热门 更多>