无法确定如何通过USB将数据从stm32f103c8发送到PC的帽子开关

问题描述 投票:0回答:1

我最近重新开始使用微控制器,最后有点卡住了。因此,我要构建的是自定义游戏手柄。我可以正确模拟按钮的数据,但是带上帽子开关后,任何操作均无效。我假设我发送了错误的数据包,但无法确定正确的结构。在测试代​​码中,我只是尝试发送一些“按钮按下”,还试图按下帽子开关上的某个按键,但是看来该PC无法识别我的数据包。我确实经历了the hid documentation(尤其是第64、65页),但仍然不知道为什么它不起作用。

我的HID描述符:

0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
0x09, 0x05,                    // USAGE (Game Pad)
0xa1, 0x01,                    // COLLECTION (Application)
0xa1, 0x00,                    //   COLLECTION (Physical)
0x05, 0x09,                    //     USAGE_PAGE (Button)
0x19, 0x01,                    //     USAGE_MINIMUM (Button 1)
0x29, 0x0e,                    //     USAGE_MAXIMUM (Button 14)
0x15, 0x00,                    //     LOGICAL_MINIMUM (0)
0x25, 0x01,                    //     LOGICAL_MAXIMUM (1)
0x95, 0x0e,                    //     REPORT_COUNT (14)
0x75, 0x01,                    //     REPORT_SIZE (1)
0x81, 0x02,                    //   INPUT (Data,Var,Abs)
0x95, 0x01,                    //   REPORT_COUNT (1)
0x75, 0x02,                    //   REPORT_SIZE (2)
0x81, 0x03,                    //   INPUT (Cnst,Var,Abs)
0x05, 0x01,                    //   USAGE_PAGE (Generic Desktop)
0x09, 0x39,                    //   USAGE (Hat switch)
0x15, 0x00,                    //   LOGICAL_MINIMUM (0)
0x25, 0x03,                    //   LOGICAL_MAXIMUM (3)
0x35, 0x00,                    //   PHYSICAL_MINIMUM (0)
0x46, 0x0e, 0x01,              //   PHYSICAL_MAXIMUM (270)
0x95, 0x01,                    //   REPORT_COUNT (1)
0x75, 0x04,                    //   REPORT_SIZE (4)
0x81, 0x42,                    //   INPUT (Data,Var,Abs,Null)
0x95, 0x01,                    //   REPORT_COUNT (1)
0x75, 0x04,                    //   REPORT_SIZE (4)
0x81, 0x03,                    //   INPUT (Cnst,Var,Abs)
0xc0,                          //   END_COLLECTION
0xc0                           // END_COLLECTION

基本测试代码:

struct gamepad_report_t
{
    uint16_t buttons;
    uint8_t hs0 : 1;
    uint8_t hs1 : 1;
    uint8_t hs2 : 1;
    uint8_t hs3 : 1;
};

struct gamepad_report_t gamepad_report;

gamepad_report.buttons = 0x0001;
gamepad_report.hs0 = 1;
gamepad_report.hs1 = 0;
gamepad_report.hs2 = 0;
gamepad_report.hs3 = 0;

int main()
{
    while (1)
    {
      gamepad_report.buttons = 0x0010;
      HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_4);
      USBD_CUSTOM_HID_SendReport(&hUsbDeviceFS, &gamepad_report, sizeof(struct gamepad_report_t));
      HAL_Delay(500);
    }
}

Data packet structure I have imaganed

What I see in the device properties when the uC is connected

c usb stm32 hid cubemx
1个回答
0
投票

我在这里遇到的问题是我想发送到PC的struct变量。如果我要发送数组,那么一切都会按预期进行...通常是一个简单的问题,会使人们发疯。

© www.soinside.com 2019 - 2024. All rights reserved.