基于STM32F103的USB学习笔记27 - CustomHID
基于JoyStickMouse例程修改即可。
1. 調(diào)整描述符
設(shè)備描述符可以不用改。
配置描述符長度改為41,主要是增加一個端點。
#define USB_CONFIG_DESC_LEN ? ? ? ? ? ? ? ? ? ? 41
配置描述符集合中的Interfae描述符修改如下,
? ? 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 CustomHID endpoint ********************/
? ? /* 27 */
? ? 0x07, ? ? ? ? ?/*bLength: Endpoint Descriptor size*/
? ? USB_ENDPOINT_DESCRIPTOR_TYPE, /*bDescriptorType:*/
? ? 0x81, ? ? ? ? ?/*bEndpointAddress: Endpoint Address (IN)*/
? ? 0x03, ? ? ? ? ?/*bmAttributes: Interrupt endpoint*/
? ? 0x40, ? ? ? ? ?/*wMaxPacketSize: 64 Byte max */
? ? 0x00,
? ? 0x0a,//0x20, ? ? ? ? ?/*bInterval: Polling Interval (32 ms)*/
? ? /* 34 */
? ? 0x07, ? ? ? ? ?/*bLength: Endpoint Descriptor size*/
? ? USB_ENDPOINT_DESCRIPTOR_TYPE, /*bDescriptorType:*/
? ? 0x01, ? ? ? ? ?/*bEndpointAddress: Endpoint Address (OUT)*/
? ? 0x03, ? ? ? ? ?/*bmAttributes: Interrupt endpoint*/
? ? 0x40, ? ? ? ? ?/*wMaxPacketSize: 64 Byte max */
? ? 0x00,
? ? 0x0a,//0x20, ? ? ? ? ?/*bInterval: Polling Interval (32 ms)*/
配置描述符集合中的HID描述符需要和Report描述符一起配合改。
const uint8_t hidReportDescriptor[HID_REPORT_DESC_LEN] =
{
? ? /* USER CODE BEGIN 0 */
? ? 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,0x40, // 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,0x40, // REPORT_COUNT (64Byte)
? ? 0x91,0x02, // OUTPUT (Data,Var,Abs)
?
? ? /* USER CODE END 0 */
? ? 0xC0 ? ?/* ? ? END_COLLECTION?? ? ? ? ? ? ? ? */
};?
修改HID描述符中描述Report描述符的長度
/* 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*/
? ? HID_REPORT_DESCRIPTOR_TYPE, ? ? ? ? /*bDescriptorType*/
? ? HID_REPORT_DESC_LEN,/*wItemLength: Total length of Report descriptor*/
? ? 0x00,
即#define HID_REPORT_DESC_LEN ? ? ? ? ? ? ? ? ? ? 33
字符串描述符可以根據(jù)需要修改。
2. 調(diào)整PMA地址分配,RX和TX都分配64字節(jié)。
#define ENDP1_TXADDR ? ? ? ?(0x100)
#define ENDP1_RXADDR ? ? ? ?(0x140)
然后在Reset里面修改端點的初始化參數(shù)
? ? SetEPType(ENDP1, EP_INTERRUPT);
? ? SetEPTxAddr(ENDP1, ENDP1_TXADDR);
? ? SetEPRxAddr(ENDP1, ENDP1_RXADDR);
? ? SetEPTxCount(ENDP1, 64);
? ? SetEPRxCount(ENDP1, 64);
? ? SetEPRxStatus(ENDP1, EP_RX_VALID);
? ? SetEPTxStatus(ENDP1, EP_TX_NAK);
3. 根據(jù)項目需求修改EP1的讀寫函數(shù),這里只是讀取64字節(jié)數(shù)據(jù),然后打印第1個字節(jié)和第64個字節(jié)
void EP1_OUT_Callback(void)
{
? ? uint8_t buf[64];
? ? buf[0] = 0;
? ? buf[63] = 0;
? ? USB_SIL_Read(EP1_OUT, buf);
? ? SetEPRxStatus(ENDP1, EP_RX_VALID);
? ? Printf("buf[0] = 0x%x, buf[63] = 0x%x\r\n", buf[0], buf[63]);
}
發(fā)送數(shù)據(jù)改為發(fā)送64字節(jié)
/* Copy mouse position info in ENDP1 Tx Packet Memory Area*/
USB_SIL_Write(EP1_IN, txBuffer, 64);
/* Enable endpoint for transmission */
SetEPTxValid(ENDP1);
4. 使用單片機多功能調(diào)試助手調(diào)試
發(fā)送實驗:
接收實驗:
總結(jié)
以上是生活随笔為你收集整理的基于STM32F103的USB学习笔记27 - CustomHID的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C语言学习完后,C++与Java我应该怎
- 下一篇: Vim简介以及常用命令