新手入门
- 积分
- 17
- 金钱
- 17
- 注册时间
- 2020-8-22
- 在线时间
- 3 小时
|
10金钱
配置和一些关键代码段:
/* USB Standard Device Descriptor */
const uint8_t CustomHID_DeviceDescriptor[CUSTOMHID_SIZ_DEVICE_DESC] =
{
0x12, /*bLength Ãèêö·û3¤¶è 0x12=18×Ö½ú*/
USB_DEVICE_DESCRIPTOR_TYPE, /*bDescriptorType Ãèêö·ûààDí*/
0x00, /*bcdUSB ±¾é豸ê1óÃμÄUSBD-òé°æ±¾*/
0x02,
0x00, /*bDeviceClass àà′úÂë*/
0x00, /*bDeviceSubClass ×óàà′úÂë*/
0x00, /*bDeviceProtocol D-òéÂë*/
0x40, /*bMaxPacketSize40 ¶Ëμã0×î′ó°ü3¤*/
0x48, /*idVendor (0x484C) 3§éìID*/
0x4c,
0x00, /*idProduct = 0x1000 2úÆ·ID*/
0x10,
0x00, /*bcdDevice rel. 2.00 é豸°æ±¾oÅ*/
0x02,
1, /*Index of string descriptor describing
manufacturer Ãèêö3§éìμÄ×Ö·û′®Ë÷òy */
2, /*Index of string descriptor describing
product Ãèêö2úÆ·μÄ×Ö·û′®Ë÷òy*/
3, /*Index of string descriptor describing the
device serial number 2úÆ·DòáDoÅμÄ×Ö·û′®Ë÷òy*/
0x01 /*bNumConfigurations ¿éÄüμÄÅäÖÃêy*/
}
; /* CustomHID_DeviceDescriptor */
/* USB Configuration Descriptor */
/* All Descriptors (Configuration, Interface, Endpoint, Class, Vendor */
const uint8_t CustomHID_ConfigDescriptor[CUSTOMHID_SIZ_CONFIG_DESC] =
{
//òÔÏÂÎaÅäÖÃÃèêö·û
0x09, /* bLength: Configuration Descriptor size ¸ÃÃèêö·ûμÄ3¤¶è*/
USB_CONFIGURATION_DESCRIPTOR_TYPE, /* bDescriptorType: Configuration Ãèêö·ûààDí*/
CUSTOMHID_SIZ_CONFIG_DESC, /* wTotalLength: Bytes returned ÅäÖᢽó¿ú¡¢¶ËμãoíààÃèêö×Ö½ú×üoí*/
0x00,
0x01, /* bNumInterfaces: 1 interface Ö§3Ö½ó¿úêy*/
0x01, /* bConfigurationValue: Configuration value ±¾ÅäÖÃÃèêö·û±êê¶ */
0x00, /* iConfiguration: Index of string descriptor describing
the configurationÅäÖÃÃèêö·ûËμÃ÷×Ö·û′®Ë÷òy */
0xC0, /* bmAttributes: Self powered μçÔ′¼°»½DÑ*/
0x96, /* MaxPower 300 mA: this current is used for detecting Vbus é豸oÄμçμçá÷*/
//òÔÏÂÎa½ó¿úÃèêö·û
/************** Descriptor of Custom HID interface ****************/
/* 09 */
0x09, /* bLength: Interface Descriptor size ¸ÃÃèêö·ûμÄ3¤¶è*/
USB_INTERFACE_DESCRIPTOR_TYPE,/* bDescriptorType: Interface descriptor type Ãèêö·ûààDí*/
0x00, /* bInterfaceNumber: Number of Interface ±¾½ó¿úÃèêö·û±êê¶ */
0x00, /* bAlternateSetting: Alternate setting ¿é¸Ä±äμÄéèÖÃ*/
0x02, /* bNumEndpoints ½ó¿ú¶Ëμãê£3yá˶Ëμã0»1óD2¸öμÄòa˼*/
0x03, /* bInterfaceClass: HID ½ó¿úààDí′úÂë*/
0x00, /* bInterfaceSubClass : 1=BOOT, 0=no boot Æô¶ˉààDí*/
0x00, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */
0, /* iInterface: Index of string descriptor ½ó¿úÃèêö·ûËμÃ÷×Ö·û′®Ë÷òy*/
//òÔÏÂÎaHIDÃèêö·û
/******************** Descriptor of Custom HID HID ********************/
/* 18 */
0x09, /* bLength: HID Descriptor size 3¤¶è*/
HID_DESCRIPTOR_TYPE, /* bDescriptorType: HID Ãèêö·ûààDí*/
0x10,//0x10, /* bcdHID: HID Class Spec release number Hid1æ·¶°æ±¾*/
0x01,
0x00, /* bCountryCode: Hardware target country 1ú¼ò′úÂë*/
0x01, /* bNumDescriptors: Number of HID class descriptors to follow Ö§3ÖμÄÃèêö·û¸öêy*/
0x22, /* bDescriptorType Ö§3ÖμÄÃèêöààDí 0x22êDZ¨±í*/
CUSTOMHID_SIZ_REPORT_DESC,/* wItemLength: Total length of Report descriptor ReportÃèêö·û3¤¶è*/
0x00,
//òÔÏÂÎaêäèë¶Ëμã1Ãèêö·û £¬ÆäêμêÇé豸ÏòÖ÷»ú·¢Ëí
/******************* Descriptor of Custom HID endpoints ******************/
/* 27 */
0x07, /* bLength: Endpoint Descriptor size */
USB_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType: Ãèêö·ûμÄààDí*/
//ÏÂÃæÕa¸öμ½μ×óÃ81»1êÇ82£¬oÃÃÔoy°¡=>8Îa·¢Ëí£¬2Îa·¢ËíμĶ˿ú£¬ËùòÔêÇ82£¬μ«êÇ»1êÇ2»DD°¡
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 ¶ËμãààDí 0x03ÖD¶Ï¶Ëμã*/
0x40,//0x40, /* wMaxPacketSize: 64 Bytes max ¶Ëμã·¢Ëí½óêÕ×î′ó°ü3¤*/
0x00,
0x01, /* bInterval: Polling Interval (32 ms) ÖD¶Ï¶ËμãÂÖÑμê±¼ä¼ä¸ô*/
//òÔÏÂÎaêä3ö¶Ëμã2Ãèêö·û £¬ÆäêμêÇÖ÷»úÏòé豸·¢»Øêy¾Y
/* 34 */
0x07, /* bLength: Endpoint Descriptor size */
USB_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType: */
/* Endpoint descriptor type */
0x01, /* bEndpointAddress: */
/* Endpoint Address (OUT) ½óêÕμÄ*/
0x03, /* bmAttributes: Interrupt endpoint */
0x40,//0x40, /* wMaxPacketSize: 64 Bytes max */
0x00,
0x01, /* bInterval: Polling Interval (32 ms) */
/* 41 */
}
; /* CustomHID_ConfigDescriptor */
/* hid±¨¸æÃèêö·û Õa¸ö1|ÄüÕæêÇ쫸′Ôó áË*/
const uint8_t CustomHID_ReportDescriptor[CUSTOMHID_SIZ_REPORT_DESC] =
{
0x06, 0xff,0xff,//0x06,0xFF,0x00,//0x8c, /* USAGE_PAGE (ST Page) ¶¨òåé豸1|Äü*/
//0x05Îaǰ×o£¬0x01Îaêy¾Y2¿·Ö£¬0x05 0000 0101
//bit0-bit1′ú±íÕa¸öǰ×ooóÃæ¸úμÄêy¾Y3¤¶èbsize 01±íê¾oóÃæ¸úò»¸ö×Ö½ú
//bit2-bit3′ú±íǰ×oààDí 0 main 1 global 2 local 3 reserved 01諾Ö
//bit4-bit7′ú±íǰ×oμÄtag 0000±íê¾Îausage page tag
//0x01±íê¾general desktop page í¨óÃ×àÃæ¿ØÖÆ
0x09, 0x00,//0xA5,//0x06,//0x01, /* USAGE (Demo Kit) ¶¨òåó÷¨*/
0xa1, 0x01, /* COLLECTION (Application) ¿aò»¸ö¼ˉoÏ*/
// The Input report êäè뱨±í
0x09,0x01,//0x03, // USAGE ID - Vendor defined ¶¨òåó÷¨
0x15,0x00, // LOGICAL_MINIMUM (0) ¶¨òåêäèë×îD¡Öμ
0x26,0x00, 0xFF, // LOGICAL_MAXIMUM (255) ¶¨òåêäèë×î′óÖμ
0x75,0x08, // REPORT_SIZE (8bit) ¶¨ò屨±íêy¾YÏî′óD¡
0x95,0x40, // REPORT_COUNT (64Byte) ¶¨ò屨±íêy¾YÏò¸öêy
0x81,0x02, // INPUT (Data,Var,Abs) êäèëÏîÄ¿
// The Output report êä3ö±¨±í
0x09,0x02,//0x04, // USAGE ID - Vendor defined ¶¨òåó÷¨
0x15,0x00, // LOGICAL_MINIMUM (0) ¶¨òåêä3ö×îD¡Öμ
0x26,0x00,0xFF, // LOGICAL_MAXIMUM (255) ¶¨òåêä3ö×î′óÖμ
0x75,0x08, // REPORT_SIZE (8bit) ¶¨ò屨±íêy¾YÏî′óD¡
0x95,0x40, // REPORT_COUNT (64Byte) ¶¨ò屨±íêy¾YÏò¸öêy
0x91,0x02, // OUTPUT (Data,Var,Abs) êä3öÏîÄ¿
0xc0 /* END_COLLECTION */ //1رռˉoÏ
}; /* CustomHID_ReportDescriptor */
/* USB String Descriptors (optional) */
//êμÏÖ¶Ô¶ËμãμÄéèÖÃ
void CustomHID_Reset(void)
{
/* Set CustomHID_DEVICE as not configured */
pInformation->Current_Configuration = 0;
pInformation->Current_Interface = 0;/*the default Interface*/
/* Current Feature initialization */
pInformation->Current_Feature = CustomHID_ConfigDescriptor[7];//1©μçÄ£ê½
SetBTABLE(BTABLE_ADDRESS);//·Ö×é»o3åÇøÃèêö±íμØÖ·éèÖÃ
/* Initialize Endpoint 0 */
SetEPType(ENDP0, EP_CONTROL);//3õê¼»ˉÎa¿ØÖƶËμãààDí
SetEPTxStatus(ENDP0, EP_TX_STALL);//¶ËμãòÔSTALL·Ö×éÏìó|ËùóDμÄ·¢ËíÇëÇó
//ò2¾íêǶËμã×′ì¬éèÖÃ3é·¢ËíÎTD§£¬ò2¾íêÇÖ÷»úμÄINáîÅÆ°üà′μÄê±oò£¬»ØËíò»¸öSTALL
SetEPRxAddr(ENDP0, ENDP0_RXADDR);//éèÖöËμã0Ãèêö·ûμĽóêü
SetEPTxAddr(ENDP0, ENDP0_TXADDR);//éèÖöËμã0Ãèêö·ûμÄ·¢ËíμØÖ·
Clear_Status_Out(ENDP0);
//½öóÃóú¿ØÖƶËμ㣬èç1ûSTATUS_OUTλ±»Çå3y£¬OUT·Ö×é¿éòÔ°üo¬èÎòa3¤¶èμÄêy¾Y
SetEPRxCount(ENDP0, Device_Property.MaxPacketSize);
//é豸¶Ëμã0μĽóêü×Ö½ú¼Ä′æÆ÷μÄ×î′óÖμêÇ64
SetEPRxValid(ENDP0);//éèÖýóêü¶ËμãóDD§
/* Initialize Endpoint 1 Õa¸öêǽóêÕμÄ*/
SetEPType(ENDP1, EP_INTERRUPT);//3õê¼»ˉÎaÖD¶Ï¶ËμãààDí ¶Ëμã1½óêÕ
//¢SetEPTxAddr(ENDP1, ENDP1_TXADDR);//éèÖ÷¢Ëíêy¾YμÄμØÖ·
SetEPRxAddr(ENDP1, ENDP1_RXADDR);//éèÖýóêÕμØÖ·
//SetEPTxCount(ENDP1, 64);//éèÖ÷¢ËíμÄ3¤¶è
SetEPRxCount(ENDP1, 64);//éèÖýóêÕμÄ3¤¶è
SetEPRxStatus(ENDP1, EP_RX_VALID);//éèÖýóêü¶ËμãóDD§
//SetEPTxStatus(ENDP1, EP_TX_NAK);//éèÖ÷¢Ëí¶Ëμã¶Ëμã·Çó|′e
/* Initialize Endpoint 2 Õa¸öêÇ·¢ËíμÄ*/
SetEPType(ENDP2, EP_INTERRUPT);//3õê¼»ˉÎaÖD¶Ï¶ËμãààDí ¶Ëμã2·¢Ëí
SetEPTxAddr(ENDP2, ENDP2_TXADDR);//éèÖ÷¢Ëíêy¾YμÄμØÖ·
//SetEPRxAddr(ENDP2, ENDP2_RXADDR);//
SetEPTxCount(ENDP2, 64);//éèÖ÷¢ËíμÄ3¤¶è
SetEPRxCount(ENDP2, 64);//
SetEPRxStatus(ENDP2, EP_RX_VALID);//éèÖýóêü¶Ëμã1رÕ
SetEPTxStatus(ENDP2, EP_TX_VALID);//éèÖ÷¢Ëí¶ËμãóDD§
SetEPTxStatus(ENDP2, EP_TX_NAK);//éèÖ÷¢Ëí¶Ëμã¶Ëμã·Çó|′e
SetEPTxStatus(ENDP2, EP_TX_STALL);//¶ËμãòÔSTALL·Ö×éÏìó|ËùóDμÄ·¢ËíÇëÇó
SetEPTxValid(ENDP2);//éèÖ÷¢Ëí¶ËμãóDD§
/* Set this device to response on default address */
SetDeviceAddress(0);//éèÖÃé豸óÃè±ê¡μØÖ·Ïàó|
bDeviceState = ATTACHED;//μ±Ç°×′ì¬á¬½ó
}
/* Private functions ---------------------------------------------------------*/
/**
* @brief í¨1yUSB·¢Ëíêy¾Y
* @param data êy¾Y′æ′¢ê×μØÖ·
* @param dataNum ·¢ËíμÄêy¾Y×Ö½úêy
* @retval ·¢ËíμÄ×Ö½úêy
*/
uint32_t USB_SendData(uint8_t *data,uint32_t dataNum)
{
#ifndef STM32F10X_CL
//½«êy¾Yí¨1yUSB·¢Ëí3öè¥
UserToPMABufferCopy(data, ENDP2_TXADDR, dataNum);
SetEPTxCount(ENDP2, REPORT_COUNT);
SetEPTxValid(ENDP2);
#else
USB_SIL_Write(EP2_IN, data, dataNum);
#endif
return dataNum;
}
/**
* @brief ½óêÕ′óUSB»ñè¡μÄêy¾Y
* @param data êy¾Y′æ′¢ê×μØÖ·
* @param dataNum ×¼±¸¶áè¡μÄêy¾Y×Ö½úêy
* @retval ¶áè¡μÄ×Ö½úêy
*/
uint32_t USB_GetData(uint8_t *data,uint32_t dataNum)
{
uint32_t len=0;
if(dataNum>sizeof(USB_Receive_Buffer)){
dataNum = sizeof(USB_Receive_Buffer);
}
for(len=0;len<dataNum;len++){
*data=USB_Receive_Buffer[len];
data++;
}
return dataNum;
}
/* -------------- Buffer Description Table -----------------*/
/*-------------------------------------------------------------*/
/* buffer table base address */
/* buffer table base address */
#define BTABLE_ADDRESS (0x00)
/* EP0 */
/* rx/tx buffer base address */
#define ENDP0_RXADDR (0x18)
#define ENDP0_TXADDR (0x58)
/* EP1 */
/* tx buffer base address */
#define ENDP1_TXADDR (0x98)
#define ENDP1_RXADDR (0xD8)
//μØÖ·Îa32λ¶ÔÆä,λ4μı¶êy£¬2»Äü3¬1y bMaxPacketSize
/* EP2 */
/* tx buffer base address */
#define ENDP2_TXADDR (0x118)
//#define ENDP2_RXADDR (0x118)
|
|