stm32制作CAN适配器4--WinUsb的使用

前面使用STM32G474芯片的USB模块做了一个CANFD程序,当时用的是HID模式,在实际使用时发现HID模块的通讯速率太慢了,只能1ms传输一帧,就会造成有些掉帧现象。 现在就把HID模块改为在Window下同样免驱的WinUsb来实现CANFD数据的传输。 首先使用STM32CubeMX配置端口与时钟

采用外部晶振12M

USB第三方库选CDC模式
到此配置完成,点击生成代码,在生成的代码中更改以下内容实现WinUSB驱动 。

在usbd_desc.c中增加Winusb的特有字符描述

c 复制代码
#if (USBD_SUPPORT_WINUSB==1)
const uint8_t USBD_OS_STRING[8] = { 
		'M',
		'S',
		'F',
		'T',
		'1',
		'0',
		'0',
		0xA0U,//USB_REQ_MS_VENDOR_CODE,
}; 

uint8_t *USBD_WinUSBOSStrDescriptor(uint16_t *length)
{
	USBD_GetString((uint8_t *)USBD_OS_STRING, USBD_StrDesc, length);
	return USBD_StrDesc;
}

#define USB_LEN_OS_FEATURE_DESC 0x28
__ALIGN_BEGIN uint8_t USBD_WINUSB_OSFeatureDesc[USB_LEN_OS_FEATURE_DESC] __ALIGN_END =
{
		0x28, 0, 0, 0, // length
		0, 1,          // bcd version 1.0
		4, 0,          // windex: extended compat ID descritor
		1,             // no of function
		0, 0, 0, 0, 0, 0, 0, // reserve 7 bytes
		// function
		0,             // interface no
		0,             // reserved
		'W', 'I', 'N', 'U', 'S', 'B', 0, 0, //  first ID
		0,   0,   0,   0,   0,   0, 0, 0,  // second ID
		0,   0,   0,   0,   0,   0 // reserved 6 bytes
};

uint8_t *USBD_WinUSBOSFeatureDescriptor(uint16_t *length)
{
	*length = USB_LEN_OS_FEATURE_DESC;
	return USBD_WINUSB_OSFeatureDesc;
}

#define USB_LEN_OS_PROPERTY_DESC 0x8E
__ALIGN_BEGIN uint8_t USBD_WINUSB_OSPropertyDesc[USB_LEN_OS_PROPERTY_DESC] __ALIGN_END =
{
		0x8E, 0, 0, 0,  // length 246 byte
		0x00, 0x01,   // BCD version 1.0
		0x05, 0x00,   // Extended Property Descriptor Index(5)
		0x01, 0x00,   // number of section (1)
		//; property section
		0x84, 0x00, 0x00, 0x00,   // size of property section
		0x1, 0, 0, 0,   //; property data type (1)
		0x28, 0,        //; property name length (42)
		'D', 0,
		'e', 0,
		'v', 0,
		'i', 0,
		'c', 0,
		'e', 0,
		'I', 0,
		'n', 0,
		't', 0,
		'e', 0,
		'r', 0,
		'f', 0,
		'a', 0,
		'c', 0,
		'e', 0,
		'G', 0,
		'U', 0,
		'I', 0,
		'D', 0,
		0, 0,
		// 13eb360b-bc1e-46cb-ac8b-ef3da47
		0x4E, 0, 0, 0,  // ; property data length
		'{', 0,
		'1', 0,
		'3', 0,
		'E', 0,
		'B', 0,
		'3', 0,
		'6', 0,
		'0', 0,
		'B', 0,
		'-', 0,
		'B', 0,
		'C', 0,
		'1', 0,
		'E', 0,
		'-', 0,
		'4', 0,
		'6', 0,
		'C', 0,
		'B', 0,
		'-', 0,
		'A', 0,
		'C', 0,
		'8', 0,
		'B', 0,
		'-', 0,
		'E', 0,
		'F', 0,
		'3', 0,
		'D', 0,
		'A', 0,
		'4', 0,
		'7', 0,
		'B', 0,
		'4', 0,
		'0', 0,
		'6', 0,
		'2', 0,
		'}', 0,
		0, 0,

};
uint8_t *USBD_WinUSBOSPropertyDescriptor(uint16_t *length)
{
	*length = USB_LEN_OS_PROPERTY_DESC;
	return USBD_WINUSB_OSPropertyDesc;
}
#endif // (USBD_SUPPORT_WINUSB==1)

在usbd_ctlreq.c中static void USBD_GetDescriptor(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req)增加case 0xEE。

c 复制代码
#if (USBD_SUPPORT_WINUSB==1)
	case 0xEE: // OS String 
	if (pdev->pClass->GetWinUSBOSDescriptor != NULL)
	{
		pbuf = pdev->pClass->GetWinUSBOSDescriptor(&len);
	}
	else
	{
		USBD_CtlError(pdev, req);
		err++;
	}  
	break;
#endif // (USBD_SUPPORT_WINUSB==1)

USBD_DescriptorsTypeDef CDC_Desc =
{
  USBD_CDC_DeviceDescriptor,
  USBD_CDC_LangIDStrDescriptor,
  USBD_CDC_ManufacturerStrDescriptor,
  USBD_CDC_ProductStrDescriptor,
  USBD_CDC_SerialStrDescriptor,
  USBD_CDC_ConfigStrDescriptor,
  USBD_CDC_InterfaceStrDescriptor,
 USBD_WinUSBOSFeatureDescriptor,
USBD_WinUSBOSPropertyDescriptor,
};

在usbd_def.h中USBD_ClassTypeDef和USBD_DescriptorsTypeDef增加winusb描述

c 复制代码
typedef struct _Device_cb
{
  uint8_t (*Init)(struct _USBD_HandleTypeDef *pdev, uint8_t cfgidx);
  uint8_t (*DeInit)(struct _USBD_HandleTypeDef *pdev, uint8_t cfgidx);
  /* Control Endpoints*/
  uint8_t (*Setup)(struct _USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef  *req);
  uint8_t (*EP0_TxSent)(struct _USBD_HandleTypeDef *pdev);
  uint8_t (*EP0_RxReady)(struct _USBD_HandleTypeDef *pdev);
  /* Class Specific Endpoints*/
  uint8_t (*DataIn)(struct _USBD_HandleTypeDef *pdev, uint8_t epnum);
  uint8_t (*DataOut)(struct _USBD_HandleTypeDef *pdev, uint8_t epnum);
  uint8_t (*SOF)(struct _USBD_HandleTypeDef *pdev);
  uint8_t (*IsoINIncomplete)(struct _USBD_HandleTypeDef *pdev, uint8_t epnum);
  uint8_t (*IsoOUTIncomplete)(struct _USBD_HandleTypeDef *pdev, uint8_t epnum);

  uint8_t  *(*GetHSConfigDescriptor)(uint16_t *length);
  uint8_t  *(*GetFSConfigDescriptor)(uint16_t *length);
  uint8_t  *(*GetOtherSpeedConfigDescriptor)(uint16_t *length);
  uint8_t  *(*GetDeviceQualifierDescriptor)(uint16_t *length);
#if (USBD_SUPPORT_USER_STRING_DESC == 1U)
  uint8_t  *(*GetUsrStrDescriptor)(struct _USBD_HandleTypeDef *pdev, uint8_t index,  uint16_t *length);
#endif
#if (USBD_SUPPORT_WINUSB == 1) 
  uint8_t  *(*GetWinUSBOSDescriptor)(uint16_t *length); 
#endif  

} USBD_ClassTypeDef;

/* USB Device descriptors structure */
typedef struct
{
  uint8_t *(*GetDeviceDescriptor)(USBD_SpeedTypeDef speed, uint16_t *length);
  uint8_t *(*GetLangIDStrDescriptor)(USBD_SpeedTypeDef speed, uint16_t *length);
  uint8_t *(*GetManufacturerStrDescriptor)(USBD_SpeedTypeDef speed, uint16_t *length);
  uint8_t *(*GetProductStrDescriptor)(USBD_SpeedTypeDef speed, uint16_t *length);
  uint8_t *(*GetSerialStrDescriptor)(USBD_SpeedTypeDef speed, uint16_t *length);
  uint8_t *(*GetConfigurationStrDescriptor)(USBD_SpeedTypeDef speed, uint16_t *length);
  uint8_t *(*GetInterfaceStrDescriptor)(USBD_SpeedTypeDef speed, uint16_t *length);
#if (USBD_CLASS_USER_STRING_DESC == 1)
  uint8_t *(*GetUserStrDescriptor)(USBD_SpeedTypeDef speed, uint8_t idx, uint16_t *length);
#endif
#if ((USBD_LPM_ENABLED == 1U) || (USBD_CLASS_BOS_ENABLED == 1))
  uint8_t *(*GetBOSDescriptor)(USBD_SpeedTypeDef speed, uint16_t *length);
#endif
#if (USBD_SUPPORT_WINUSB==1)
   uint8_t *(*GetWinUSBOSFeatureDescriptor)(uint16_t *length);
   uint8_t *(*GetWinUSBOSPropertyDescriptor)(uint16_t *length);
#endif
} USBD_DescriptorsTypeDef;

在usbd_cdc.c中的static uint8_t USBD_CDC_Setup(USBD_HandleTypeDef *pdev,

USBD_SetupReqTypedef *req)更改为下面

c 复制代码
#define WINUSB_CONFIG_DESC_SIZE 32
__ALIGN_BEGIN uint8_t USBD_CDC_CfgHSDesc[WINUSB_CONFIG_DESC_SIZE] __ALIGN_END =
{
  /*Configuration Descriptor*/
  0x09,   /* bLength: Configuration Descriptor size */
  USB_DESC_TYPE_CONFIGURATION,      /* bDescriptorType: Configuration */
  WINUSB_CONFIG_DESC_SIZE,                /* wTotalLength:no of returned bytes */
  0x00,
  0x01,   /* bNumInterfaces: 1 interface for WINUSB */
  0x01,   /* bConfigurationValue: Configuration value */
  USBD_IDX_CONFIG_STR,   /* iConfiguration: Index of string descriptor describing the configuration */
  0xC0,   /* bmAttributes: self powered */
  0x32,   /* MaxPower 50*2 mA */
  
  /*---------------------------------------------------------------------------*/
  /*Data class interface descriptor*/
  0x09,   /* bLength: Endpoint Descriptor size */
  USB_DESC_TYPE_INTERFACE,  /* bDescriptorType: */
  0x00,   /* bInterfaceNumber: Number of Interface, zero based index of this interface */
  0x00,   /* bAlternateSetting: Alternate setting */
  0x02,   /* bNumEndpoints: Two endpoints used */
  0xff,   /* bInterfaceClass: vendor */
//  0x00,   /* bInterfaceClass: vendor */
  0x00,   /* bInterfaceSubClass: */
  0x00,   /* bInterfaceProtocol: */
  0x00,   /* iInterface: */
  
  /*Endpoint OUT Descriptor*/
  0x07,   /* bLength: Endpoint Descriptor size */
  USB_DESC_TYPE_ENDPOINT,      /* bDescriptorType: Endpoint */
  CDC_OUT_EP,                        /* bEndpointAddress */
  0x02,                              /* bmAttributes: Bulk */
  LOBYTE(CDC_DATA_HS_MAX_PACKET_SIZE),  /* wMaxPacketSize: */
  HIBYTE(CDC_DATA_HS_MAX_PACKET_SIZE),
  0x00,                              /* bInterval: ignore for Bulk transfer */
  
  /*Endpoint IN Descriptor*/
  0x07,   /* bLength: Endpoint Descriptor size */
  USB_DESC_TYPE_ENDPOINT,      /* bDescriptorType: Endpoint */
  CDC_IN_EP,                         /* bEndpointAddress */
  0x02,                              /* bmAttributes: Bulk */
  LOBYTE(CDC_DATA_HS_MAX_PACKET_SIZE),  /* wMaxPacketSize: */
  HIBYTE(CDC_DATA_HS_MAX_PACKET_SIZE),
  0x00                               /* bInterval: ignore for Bulk transfer */
} ;

__ALIGN_BEGIN uint8_t USBD_CDC_CfgFSDesc[WINUSB_CONFIG_DESC_SIZE] __ALIGN_END =
{
  /*Configuration Descriptor*/
  0x09,   /* bLength: Configuration Descriptor size */
  USB_DESC_TYPE_CONFIGURATION,      /* bDescriptorType: Configuration */
  WINUSB_CONFIG_DESC_SIZE,                /* wTotalLength:no of returned bytes */
  0x00,
  0x01,   /* bNumInterfaces: 1 interface for Game IO */
  0x01,   /* bConfigurationValue: Configuration value */
  USBD_IDX_CONFIG_STR,   /* iConfiguration: Index of string descriptor describing the configuration */
  0xC0,   /* bmAttributes: self powered */
  0x32,   /* MaxPower 0 mA */
  
  /*---------------------------------------------------------------------------*/
  /*Data class interface descriptor*/
  0x09,   /* bLength: Endpoint Descriptor size */
  USB_DESC_TYPE_INTERFACE,  /* bDescriptorType: */
  0x00,   /* bInterfaceNumber: Number of Interface, zero based index of this interface */
  0x00,   /* bAlternateSetting: Alternate setting */
  0x02,   /* bNumEndpoints: Two endpoints used */
  0xff,   /* bInterfaceClass: vendor */
//  0x00,   /* bInterfaceClass: vendor */
  0x00,   /* bInterfaceSubClass: */
  0x00,   /* bInterfaceProtocol: */
  0x00,   /* iInterface: */
  
  /*Endpoint OUT Descriptor*/
  0x07,   /* bLength: Endpoint Descriptor size */
  USB_DESC_TYPE_ENDPOINT,      /* bDescriptorType: Endpoint */
  CDC_OUT_EP,                        /* bEndpointAddress */
  0x02,                              /* bmAttributes: Bulk */
  LOBYTE(CDC_DATA_FS_MAX_PACKET_SIZE),  /* wMaxPacketSize: */
  HIBYTE(CDC_DATA_FS_MAX_PACKET_SIZE),
  0x00,                              /* bInterval: ignore for Bulk transfer */
  
  /*Endpoint IN Descriptor*/
  0x07,   /* bLength: Endpoint Descriptor size */
  USB_DESC_TYPE_ENDPOINT,      /* bDescriptorType: Endpoint */
  CDC_IN_EP,                         /* bEndpointAddress */
  0x02,                              /* bmAttributes: Bulk */
  LOBYTE(CDC_DATA_FS_MAX_PACKET_SIZE),  /* wMaxPacketSize: */
  HIBYTE(CDC_DATA_FS_MAX_PACKET_SIZE),
  0x00                               /* bInterval: ignore for Bulk transfer */
} ;    

void USBD_WinUSBGetDescriptor(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req)
{
  uint16_t len;
  uint8_t *pbuf;
  
    
  switch (req->wIndex)
  { 
  case 0x04: // compat ID
    pbuf = pdev->pDesc->GetWinUSBOSFeatureDescriptor(&len);
    break;
  case 0x05:
    pbuf = pdev->pDesc->GetWinUSBOSPropertyDescriptor(&len);
    break;
     
  default: 
     USBD_CtlError(pdev , req);
    return;
  }
  if((len != 0)&& (req->wLength != 0))
  {
    
    len = MIN(len , req->wLength);
    
    USBD_CtlSendData (pdev, 
                      pbuf,
                      len);
  }
} 

static uint8_t USBD_CDC_Setup(USBD_HandleTypeDef *pdev,
                              USBD_SetupReqTypedef *req)
{
  USBD_StatusTypeDef ret = USBD_OK;

  if(req->bRequest == 0xA0)
  {
    USBD_WinUSBGetDescriptor( pdev, req ); 
    return USBD_OK;
  }

  if(req->bmRequest == 0xC1) 
  {
  	USBD_WinUSBGetDescriptor( pdev, req ); 
  	return USBD_OK;
  }
  
  return (uint8_t)ret;
}

编译驱动成功!

网盘:https://pan.baidu.com/s/1nIK4ig2TW4pdCfUxiYL6VA?pwd=1234

相关推荐
羽获飞28 分钟前
从零开始学嵌入式之STM32——7.STM32的系统架构
stm32·单片机·嵌入式硬件
范纹杉想快点毕业29 分钟前
嵌入式系统架构之道:告别“意大利面条”,拥抱状态机与事件驱动
java·开发语言·c++·嵌入式硬件·算法·架构·mfc
-Springer-37 分钟前
STM32 学习 —— 个人学习笔记2-1(软件安装)
笔记·stm32·学习
来自晴朗的明天1 小时前
6、AD7683 单通道 AD 采样电路
单片机·嵌入式硬件·硬件工程
TEL136997627501 小时前
方寸微PT153s千兆网口方案 替代RTL8153b方案
单片机·嵌入式硬件
克莱斯勒ya1 小时前
硬件支持包下载地址
嵌入式硬件
恒锐丰小吕1 小时前
屹晶微 EG2122 中压250V半桥驱动芯片技术解析
嵌入式硬件·硬件工程
qq_25814297-npl1 小时前
在单片机串口接收程序中,通常每接收完一条报文就添加一条接收时间,而不是每接收一个字节。这是因为报文是逻辑单元,添加时间戳到完整报文更合理和高效。
单片机·嵌入式硬件
星马梦缘1 小时前
STM32主控最小电路绘制
stm32·单片机·嵌入式硬件
蓬荜生灰1 小时前
STM32(10)-- 创建库函数版工程
stm32·单片机·嵌入式硬件