当前位置: 首页 > article >正文

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

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

采用外部晶振12M
在这里插入图片描述

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

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


#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。

#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描述

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)更改为下面


#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


http://www.kler.cn/a/447781.html

相关文章:

  • druid与pgsql结合踩坑记
  • mac iterm2 使用 lrzsz
  • LeetCode题练习与总结:预测赢家--486
  • 基于LabVIEW的USRP信道测量开发
  • Elasticsearch-DSL高级查询操作
  • RabbitMQ 路由(Routing)通讯方式详解
  • 植物大战僵尸杂交版v3.0.2最新版本(附下载链接)
  • 云图库平台(二)前端项目初始化
  • 二进制分析的新兴趋势:塑造安全的移动应用
  • Kubernates
  • Ubuntu24版 最新安装CUDA驱动方式
  • dify工作流+github actions实现翻译并创建PR
  • 智能体实战(需求分析助手)二、需求分析助手第一版实现(支持需求提取、整理、痛点分析、需求分类、优先级分析、需求文档生成等功能)
  • Spring(二)AOP、切入点表达式、AspecJ常用通知的类型、Spring中的事务管理
  • 【期末复习】JavaEE(上)
  • powershell美化
  • 使用 Docker 打包和运行 Vue 应用
  • Suno Api V4模型无水印开发「人声伴奏分离」 —— 「Suno Api系列」第7篇
  • 成方金融科技后端部分笔试题 - 解析
  • Pytorch | 利用VMI-FGSM针对CIFAR10上的ResNet分类器进行对抗攻击
  • Pytorch | 从零构建MobileNet对CIFAR10进行分类
  • 电子应用设计方案66:智能打印机系统设计
  • Vulkan 学习(11)---- Vulkan RenderPass 创建
  • Liunx下MySQL:表的约束
  • OpenTK 中帧缓存的深度解析与应用实践
  • Gemini 2.0:面向智能体时代的全新 AI 模型