Explorar o código

USB HID 64字节透传

qpalz555 hai 1 ano
pai
achega
bddb37867e

+ 8 - 16
Core/Src/main.c

@@ -61,12 +61,12 @@ void SendKeyboardReport(uint8_t modifiers, uint8_t key1, uint8_t key2, uint8_t k
   uint8_t report[8] = {0};
 
   report[0] = modifiers; // 修饰键(如Ctrl�Shift等)
-  report[2] = key1;      // 第一个按键
-  report[3] = key2;      // 第二个按键
-  report[4] = key3;      // 第三个按键
-  report[5] = key4;      // 第四个按键
-  report[6] = key5;      // 第五个按键
-  report[7] = key6;      // 第六个按键
+  report[2] = key1;      // 第一个按�
+  report[3] = key2;      // 第二个按�
+  report[4] = key3;      // 第三个按�
+  report[5] = key4;      // 第四个按�
+  report[6] = key5;      // 第五个按�
+  report[7] = key6;      // 第六个按�
 
   USBD_CUSTOM_HID_SendReport(&hUsbDeviceFS, report, sizeof(report));
 }
@@ -111,10 +111,7 @@ int main(void)
   while (1)
   {
     /* USER CODE END WHILE */
-    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET);
-    HAL_Delay(500);
-    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);  
-    HAL_Delay(500);
+
     /* USER CODE BEGIN 3 */
   }
   /* USER CODE END 3 */
@@ -183,16 +180,11 @@ static void MX_GPIO_Init(void)
 
   /*Configure GPIO pins : PA0 PA1 PA2 PA3
                            PA4 PA5 PA6 PA7 */
-  GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_2|GPIO_PIN_3
+  GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3
                           |GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
   GPIO_InitStruct.Pull = GPIO_NOPULL;
   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
-	
-  GPIO_InitStruct.Pin = GPIO_PIN_1;
-  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
-  GPIO_InitStruct.Pull = GPIO_NOPULL;
-  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);	
 
   /*Configure GPIO pin : PB1 */
   GPIO_InitStruct.Pin = GPIO_PIN_1;

+ 3 - 2
Middlewares/ST/STM32_USB_Device_Library/Class/CustomHID/Inc/usbd_customhid.h

@@ -17,6 +17,7 @@
   ******************************************************************************
   */
 
+
 /* Define to prevent recursive inclusion -------------------------------------*/
 #ifndef __USB_CUSTOMHID_H
 #define __USB_CUSTOMHID_H
@@ -42,10 +43,10 @@ extern "C" {
   * @{
   */
 #define CUSTOM_HID_EPIN_ADDR                 0x81U
-#define CUSTOM_HID_EPIN_SIZE                 0x40U
+#define CUSTOM_HID_EPIN_SIZE                 0x02U
 
 #define CUSTOM_HID_EPOUT_ADDR                0x01U
-#define CUSTOM_HID_EPOUT_SIZE                0x40U
+#define CUSTOM_HID_EPOUT_SIZE                0x02U
 
 #define USB_CUSTOM_HID_CONFIG_DESC_SIZ       41U
 #define USB_CUSTOM_HID_DESC_SIZ              9U

+ 19 - 41
USB_DEVICE/App/usbd_custom_hid_if.c

@@ -105,39 +105,24 @@ uint8_t HID_Buffer[64];
 /** Usb HID report descriptor. */
 __ALIGN_BEGIN static uint8_t CUSTOM_HID_ReportDesc_FS[USBD_CUSTOM_HID_REPORT_DESC_SIZE] __ALIGN_END =
 {
-  0x05, 0x01,        // USAGE_PAGE (Generic Desktop)
-  0x09, 0x06,        // USAGE (Keyboard)
-  0xA1, 0x01,        // COLLECTION (Application)
-  0x05, 0x07,        // USAGE_PAGE (Keyboard)
-  0x19, 0xE0,        // USAGE_MINIMUM (Keyboard LeftControl)
-  0x29, 0xE7,        // USAGE_MAXIMUM (Keyboard Right GUI)
-  0x15, 0x00,        // LOGICAL_MINIMUM (0)
-  0x25, 0x01,        // LOGICAL_MAXIMUM (1)
-  0x75, 0x01,        // REPORT_SIZE (1)
-  0x95, 0x08,        // REPORT_COUNT (8)
-  0x81, 0x02,        // INPUT (Data,Var,Abs)
-  0x95, 0x01,        // REPORT_COUNT (1)
-  0x75, 0x08,        // REPORT_SIZE (8)
-  0x81, 0x03,        // INPUT (Cnst,Var,Abs)
-  0x95, 0x05,        // REPORT_COUNT (5)
-  0x75, 0x01,        // REPORT_SIZE (1)
-  0x05, 0x08,        // USAGE_PAGE (LEDs)
-  0x19, 0x01,        // USAGE_MINIMUM (Num Lock)
-  0x29, 0x05,        // USAGE_MAXIMUM (Kana)
-  0x91, 0x02,        // OUTPUT (Data,Var,Abs)
-  0x95, 0x01,        // REPORT_COUNT (1)
-  0x75, 0x03,        // REPORT_SIZE (3)
-  0x91, 0x03,        // OUTPUT (Cnst,Var,Abs)
-  0x95, 0x06,        // REPORT_COUNT (6)
-  0x75, 0x08,        // REPORT_SIZE (8)
-  0x15, 0x00,        // LOGICAL_MINIMUM (0)
-  0x25, 0x65,        // LOGICAL_MAXIMUM (101)
-  0x05, 0x07,        // USAGE_PAGE (Keyboard)
-  0x19, 0x00,        // USAGE_MINIMUM (Reserved (no event indicated))
-  0x29, 0x65,        // USAGE_MAXIMUM (Keyboard Application)
-  0x81, 0x00,        // INPUT (Data,Ary,Abs)
-  0xC0               // END_COLLECTION
-}; 
+  0x06, 0x00, 0xFF,  // Usage Page (Vendor Defined 0xFF00)
+  0x09, 0x00,        // Usage (0x00)
+  0xA1, 0x01,        // Collection (Application)
+  0x09, 0x01,        //   Usage (0x01)
+  0x15, 0x00,        //   Logical Minimum (0)
+  0x26, 0xFF, 0x00,  //   Logical Maximum (255)
+  0x75, 0x08,        //   Report Size (8)
+  0x95, 0x40,        //   Report Count (64)
+  0x81, 0x00,        //   Input (Data,Array,Abs,No Wrap,Linear,Preferred State,No Null Position)
+  0x09, 0x02,        //   Usage (0x02)
+  0x15, 0x00,        //   Logical Minimum (0)
+  0x26, 0xFF, 0x00,  //   Logical Maximum (255)
+  0x75, 0x08,        //   Report Size (8)
+  0x95, 0x40,        //   Report Count (64)
+  0x91, 0x00,        //   Output (Data,Array,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile)
+  0xC0,              // End Collection
+  // 34 bytes
+};
 
 /* USER CODE BEGIN PRIVATE_VARIABLES */
 
@@ -209,7 +194,6 @@ static int8_t CUSTOM_HID_DeInit_FS(void)
   return (USBD_OK);
   /* USER CODE END 5 */
 }
-
 uint32_t size = 0;
 uint8_t buff[64];
 /**
@@ -232,13 +216,7 @@ static int8_t CUSTOM_HID_OutEvent_FS(uint8_t event_idx, uint8_t state)
   }
   
   USBD_CUSTOM_HID_SendReport(&hUsbDeviceFS, buff, size);
-  
-  // 开启下一次接收
-//  if (USBD_CUSTOM_HID_ReceivePacket(&hUsbDeviceFS) != (uint8_t)USBD_OK)
-//  {
-//	  return -1;
-//  }
-
+  /* USER CODE BEGIN 6 */
   return (USBD_OK);
   /* USER CODE END 6 */
 }

+ 1 - 1
USB_DEVICE/Target/usbd_conf.h

@@ -74,7 +74,7 @@
 /*---------- -----------*/
 #define USBD_CUSTOMHID_OUTREPORT_BUF_SIZE     64
 /*---------- -----------*/
-#define USBD_CUSTOM_HID_REPORT_DESC_SIZE     63
+#define USBD_CUSTOM_HID_REPORT_DESC_SIZE     34
 /*---------- -----------*/
 #define CUSTOM_HID_FS_BINTERVAL     0x5