STM32 HID通信长度设置——64byte改成32byte

1、usb_desc.c 修改 3个描述中的两个描述

USB Standard Device Descriptor :不修改

USB Configuration Descriptor:修改,红色标识处

CustomHID_ConfigDescriptor:修改,红色标识处


/* USB Configuration Descriptor */
const uint8_t CustomHID_ConfigDescriptor[CUSTOMHID_SIZ_CONFIG_DESC] =
{
    0x09, /* bLength: Configuation Descriptor size */
    USB_CONFIGURATION_DESCRIPTOR_TYPE, /* bDescriptorType: Configuration */
    CUSTOMHID_SIZ_CONFIG_DESC,
    /* wTotalLength: Bytes returned */
    0x00,
    0x01,         /* bNumInterfaces: 1 interface */
    0x01,         /* bConfigurationValue: Configuration value */
    0x00,         /* iConfiguration: Index of string descriptor describing
                                 the configuration*/
    0xC0,         /* bmAttributes: Bus powered */
                  /*Bus powered: 7th bit, Self Powered: 6th bit, Remote wakeup: 5th bit, reserved: 4..0 bits */
    0x96,         /* MaxPower 300 mA: this current is used for detecting Vbus */
    /************** Descriptor of Custom HID interface ****************/
    /* 09 */
    0x09,         /* bLength: Interface Descriptor size */
    USB_INTERFACE_DESCRIPTOR_TYPE,/* bDescriptorType: Interface descriptor type */
    0x00,         /* bInterfaceNumber: Number of Interface */
    0x00,         /* bAlternateSetting: Alternate setting */
    0x02,         /* bNumEndpoints */
    0x03,         /* bInterfaceClass: HID */
    0x00,         /* bInterfaceSubClass : 1=BOOT, 0=no boot */
    0x00,         /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */
    0,            /* iInterface: Index of string descriptor */
    /******************** Descriptor of Custom HID HID ********************/
    /* 18 */
    0x09,         /* bLength: HID Descriptor size */
    HID_DESCRIPTOR_TYPE, /* bDescriptorType: HID */
    0x10,         /* bcdHID: HID Class Spec release number */
    0x01,
    0x00,         /* bCountryCode: Hardware target country */
    0x01,         /* bNumDescriptors: Number of HID class descriptors to follow */
    0x22,         /* bDescriptorType */
    CUSTOMHID_SIZ_REPORT_DESC,/* wItemLength: Total length of Report descriptor */
    0x00,
    /******************** Descriptor of Custom HID endpoints ******************/
    /* 27 */
    0x07,          /* bLength: Endpoint Descriptor size */
    USB_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType: */

    0x82,          /* bEndpointAddress: Endpoint Address (IN) */               
                   // bit 3...0 : the endpoint number
                   // bit 6...4 : reserved
                    // bit 7     : 0(OUT), 1(IN)
    0x03,          /* bmAttributes: Interrupt endpoint */
    0x20,          /* wMaxPacketSize: 64 Bytes max */
    0x00,
    0x02,          /* bInterval: Polling Interval (2 ms) */
    /* 34 */
        
    0x07,    /* bLength: Endpoint Descriptor size */
    USB_ENDPOINT_DESCRIPTOR_TYPE,    /* bDescriptorType: */
            /*    Endpoint descriptor type */
    0x01,    /* bEndpointAddress: */
            /*    Endpoint Address (OUT) */
    0x03,    /* bmAttributes: Interrupt endpoint */
    0x20,    /* wMaxPacketSize: 64 Bytes max  */
    0x00,
    0x02,    /* bInterval: Polling Interval (2 ms) */
    /* 41 */
};

/* CustomHID_ConfigDescriptor */
const uint8_t CustomHID_ReportDescriptor[CUSTOMHID_SIZ_REPORT_DESC] = 

    0x05, 0x8c, /* USAGE_PAGE (ST Page) */ 
    0x09, 0x01, /* USAGE (Demo Kit) */ 
    0xa1, 0x01, /* COLLECTION (Application) */ 
    
    // The Input report 
    0x09,0x03, // USAGE ID - Vendor defined 
    0x15,0x00, // LOGICAL_MINIMUM (0) 
    0x26,0x00, 0xFF, // LOGICAL_MAXIMUM (255) 
    0x75,0x08, // REPORT_SIZE (8bit) 
    0x95,0x20, // REPORT_COUNT (64Byte) 
    0x81,0x02, // INPUT (Data,Var,Abs) 

    // The Output report 
    0x09,0x04, // USAGE ID - Vendor defined 
    0x15,0x00, // LOGICAL_MINIMUM (0) 
    0x26,0x00,0xFF, // LOGICAL_MAXIMUM (255) 
    0x75,0x08, // REPORT_SIZE (8bit) 
    0x95,0x20, // REPORT_COUNT (64Byte) 
    0x91,0x02, // OUTPUT (Data,Var,Abs) 

    0xc0 /* END_COLLECTION */ 
}; /* CustomHID_ReportDescriptor */ 

 

2、#define    REPORT_COUNT        32//端点长度

接收和发送的端点字节,修改接收和发送函数

发送:

uint32_t USB_SendData(uint8_t *data,uint32_t dataNum)
{
    //将数据通过USB发送出去
    UserToPMABufferCopy(data, ENDP2_TXADDR, dataNum);
    SetEPTxCount(ENDP2, REPORT_COUNT);
    SetEPTxValid(ENDP2);
    return dataNum;  
}

接收:

void EP1_OUT_Callback(void)
{
    //接收
    u8 len=0;
    //启动定时器接收
    memset(USB_Receive_Buffer,0,sizeof(USB_Receive_Buffer));
    PMAToUserBufferCopy(USB_Receive_Buffer, ENDP1_RXADDR, REPORT_COUNT);
      SetEPRxStatus(ENDP1, EP_RX_VALID);
     USB_Received_Flag=1;
    
    //将数据重新组包
    if(USB_Received_Flag_END == 0)
    {
        USB_Received_Flag_END = 1;
        memset(USB_Receive_Buffer_END,0,sizeof(USB_Receive_Buffer_END));
        memcpy(USB_Receive_Buffer_END,&USB_Receive_Buffer[0],REPORT_COUNT);
        USB_Receive_Buffer_NOW_count=REPORT_COUNT;
        
    }else if(USB_Received_Flag_END > 0)
    {        
        if(USB_Receive_Buffer_NOW_count <(256-REPORT_COUNT))
        {
            memcpy(&USB_Receive_Buffer_END[USB_Receive_Buffer_NOW_count],&USB_Receive_Buffer[0],REPORT_COUNT);
            USB_Receive_Buffer_NOW_count += REPORT_COUNT;
            //USB_Receive_Buffer_END_count = USB_Receive_Buffer[0];
        }else
        {
            USB_Receive_Buffer_NOW_count = 0;
            USB_Received_Flag_END = 0;
        }
    }
}

 

3、usb_config.h

64改成32即可

#define RX_FIFO_SIZE                           32  //64
#define TX0_FIFO_SIZE                          32  //64
#define TX1_FIFO_SIZE                          32  //64
#define TX2_FIFO_SIZE                          32  //64
#define TX3_FIFO_SIZE                          16

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值