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"
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。