首页 > 其他分享 >3.USB移植

3.USB移植

时间:2022-11-11 13:33:52浏览次数:39  
标签:status USB DEV dev pdev 移植 usb

项目要求USB设备为打印机设备,bulk传输,所以将官方的CDC例程进行修改

1.将\HC32F4A0_DDL_Rev2.0.0\projects\ev_hc32f4a0_lqfp176\applications\usb\usb_dev_cdc\source中的文件和
HC32F4A0_DDL_Rev2.0.0\midwares\hc32\usb\usb_device_lib拷贝到自己的工程中

2.修改usb_dev_desc.c中的内容
#define DEV_VID (0x0000)
#define DEV_PID (0x0000)

#define DEV_LANGID_STRING              (0x409)
#define DEV_MANUFACTURER_STRING        ("demo")
#define DEV_PRODUCT_FS_STRING          ("demo")
#define DEV_SERIALNUMBER_FS_STRING     ("ffffffffffff")
#define DEV_CONFIGURATION_FS_STRING    ("demo Config")
#define DEV_INTERFACE_FS_STRING        ("demo Interface")

3.修改usb_dev_cdc_class.c中的内容
1.修改配置描述符

        __USB_ALIGN_BEGIN static uint8_t usb_dev_cdc_cfgdesc[USB_CDC_CONFIG_DESC_SIZ]  =
        {
            /*Configuration Descriptor*/
            0x09,	/* bLength: Configuration Descriptor size */
            USB_CFG_DESCRIPTOR_TYPE, /* bDescriptorType: Configuration */
            USB_CDC_CONFIG_DESC_SIZ, /* wTotalLength:no of returned bytes */
            0x00,
            0x01,   /* bNumInterfaces: 1 interface */
            0x01,   /* bConfigurationValue: Configuration value */
            0x00,   /* iConfiguration: Index of string descriptor describing the configuration */
            0xC0,   /* bmAttributes: self powered */
            0x32,   /* MaxPower 0 mA */

            /*Interface Descriptor*/
            /* 09 */
            0x09,   /* bLength: Interface Descriptor size */
            USB_INTERFACE_DESCRIPTOR_TYPE,	/* bDescriptorType: Interface */
            /* Interface descriptor type */
            0x00,   /* bInterfaceNumber: Number of Interface */
            0x00,   /* bAlternateSetting: Alternate setting */
            0x02,   /* bNumEndpoints: One endpoints used */
            0x07,   /* bInterfaceClass: Printers Interface Class */
            0x01,   /* bInterfaceSubClass: Printers */
            0x02,   /* bInterfaceProtocol: Bi-directional interface */
            0x00,   /* iInterface: */


            /*Endpoint 1 Descriptor*/
            0x07,   /* bLength: Endpoint Descriptor size */
            USB_ENDPOINT_DESCRIPTOR_TYPE,   /* bDescriptorType: Endpoint */
            CDC_OUT_EP,   /* bEndpointAddress: (OUT1) */
            0x02,   /* bmAttributes: Bulk */
            LOBYTE(MAX_CDC_PACKET_SIZE),
            HIBYTE(MAX_CDC_PACKET_SIZE),
            0x00,   /* bInterval: */
            /*Endpoint 2 Descriptor*/
            /* 25 */
            0x07,   /* bLength: Endpoint Descriptor size */
            USB_ENDPOINT_DESCRIPTOR_TYPE,   /* bDescriptorType: Endpoint */
            CDC_IN_EP,   /* bEndpointAddress: (IN2) */
            0x02,   /* bmAttributes: Bulk */
            LOBYTE(MAX_CDC_PACKET_SIZE),
            HIBYTE(MAX_CDC_PACKET_SIZE),
            0x00,   /* bInterval: ignore for Bulk transfer */

        } ;
2.修改设置请求返回值,设备将枚举成打印机,并会回复device id请求

    case USB_REQ_TYPE_CLASS : //0x20
        if (req->wLength != 0U)
        {
			__ALIGN_BEGIN const uint8_t buffer[]={
					  0x01, 0x5C,             //92 0x5C
					  0x4D, 0x46, 0x47, 0x3A, //pMFG:             
					  'D' , 'e' , 'm' , 'o',' ', ';', 				
					  0x43, 0x4D, 0x44, 0x3A, //CMD: 
					  0x53, 0x63, 0x61, 0x6E, 0x6E, 0x65, 0x72, 0x5F, 0x49, 0x6D, 0x61, 0x67, 0x65, 0x3B, ;
					  0x4D, 0x4F, 0x44, 0x45, 0x4C, 0x3A, //MODEL:
					  0x53, 0x63, 0x61 ,0x6E, 0x6E, 0x65, 0x72, 0x3B, ;
					  0x63, 0x6C, 0x61, 0x73, 0x73, 0x3A,       //class:
					  0x53, 0x43, 0x41, 0x4E, 0x4E, 0x45, 0x52, 0x3B,  ;
					  0x44, 0x45, 0x53, 0x3A, //DES:
					  0x53, 0x63, 0x61, 0x6E, 0x6E, 0x65, 0x72, 0x5F, 0x74, 0x65, 0x73, 0x74, 0x3B, ;
					  0x43, 0x4D, 0x54, 0x3A, //CMT:			
					  0x44, 0x45, 0x4D ,0x4F, 0x20, 0x00, 0x43, 0x6F, 0x2E, 0x2C, 0x4C, 0x74, 0x64						
					  };
			/* Send the data to the host */						

			cdcCmd = req->bRequest;
			cdcLen = req->wLength;
			usb_ctrldatatx(pdev, (uint8_t *)buffer, sizeof(buffer));					  
					        
        }
        break;

4.修改usb_dev_stdreq.c中的内容

void usb_getstatus(usb_core_instance  *pdev, const USB_SETUP_REQ *req)
{
    (void)(req);
    uint8_t device_cur_status=0;
    switch (pdev->dev.device_cur_status)
    {
        case USB_DEV_ADDRESSED:
        case USB_DEV_CONFIGURED:
            #ifdef SELF_POWER
            dev_cfg_status = USB_CONFIG_SELF_POWERED;
            #else
            dev_cfg_status = 0x00U;
            #endif

            if (0U != pdev->dev.device_remote_wakeup)
            {
                dev_cfg_status |= USB_CONFIG_REMOTE_WAKEUP;
            }
            
            device_cur_status=pdev->dev.device_cur_status;
            if (  (device_cur_status== USB_DEV_DEFAULT)   //0x01
                ||(device_cur_status==USB_DEV_ADDRESSED)  //0x02
                ||(device_cur_status==USB_DEV_CONFIGURED))//0x03
            {
                if (LOBYTE(req->wIndex) <= USBD_ITF_MAX_NUM) //0x01
                {
                    pdev->dev.class_callback->ep0_setup(pdev, (void*)req);

                    if (req->wLength == 0U)
                    {
                        usb_ctrlstatustx(pdev);
                    }
                }
                else
                {
                    usb_ctrlerr(pdev);
                }
            }			
            
            
//            usb_ctrldatatx(pdev, (uint8_t *)&dev_cfg_status, 0x5c);
            break;

        default :
            usb_ctrlerr(pdev);
            break;
    }
}

5.修改usb_dev_cdc_class.c中的内容

标签:status,USB,DEV,dev,pdev,移植,usb
From: https://www.cnblogs.com/zy257/p/16880220.html

相关文章