基于MSP430 USB API的USB-HID游戏手柄:描述符配置及模拟输入扩展问题
Alright, let's work through getting your 14-channel 16-bit analog input gamepad up and running on the MSP430. I'll break down exactly what you need to adjust in the descriptor settings and code, step by step.
Calculating the HID Report Descriptor Size (report_desc_size_HID0)
To get the correct descriptor size, we need to count the total bytes of every item in your modified HID report descriptor. Each HID descriptor item has a 1-byte prefix (encoding tag, type, and data size) plus the data bytes specified by the size field.
For your adjusted descriptor, let's tally up the bytes:
- Global items (like
UsagePage,ReportSize,LogicalMinimum): Each takes 2 bytes (1-byte prefix + 1-byte data, since all values fit in 1 byte) - Local items (like
Usage,UsageMinimum): Each takes 2 bytes - Main items (like
Collection,Input,Output): Each takes 2 bytes, exceptEndCollectionwhich only takes 1 byte (no data field)
Adding up all items in your modified descriptor gives a total of 88 bytes. So you'll need to update:
#define report_desc_size_HID0 88
in descriptors.h.
Calculating the Input Report Length (report_len_input)
The input report length is the total number of bytes sent to the host, calculated from the Input sections of your descriptor:
- 14 channels of 16-bit analog data:
16 bits × 14 = 224 bits = 28 bytes - 32 bits of button data (even though you only use 6 buttons, the descriptor specifies 32 bits):
32 bits = 4 bytes
Adding these gives a total input report length of 32 bytes. Update this value in descriptors.c:
const uint16_t report_len_input = 32;
Additional Required Code Changes
Beyond the descriptor values, you'll need to adjust a few other parts of the code to match your new setup:
- Update report structs in
USB_gamepad.h:
Modify the input report struct to hold 14 analog channels and your 6 buttons:
For the output report (indicators), keep the 16-bit structure but ensure only bits 0-5 are used for your 6 indicators:typedef struct { uint16_t analog[14]; // Stores 14 16-bit ADC readings uint32_t buttons; // Bits 0-5 map to your 6 buttons; remaining bits unused } USB_Gamepad_InputReport_t;typedef struct { uint16_t indicators; // Bits 0-5 control your 6 LEDs; remaining bits unused } USB_Gamepad_OutputReport_t; - Adjust macro definitions:
UpdateNUM_BUTTONSandNUM_INDICATORS(indescriptors.horUSB_gamepad.h) to 6:#define NUM_BUTTONS 6 #define NUM_INDICATORS 6 - Update ADC sampling logic:
Inmain.c, modify the code that reads ADC values to sample 14 channels instead of 8, and populate theanalogarray in the input report correctly. - Update button handling:
Ensure only your 6 physical buttons are mapped to the first 6 bits of thebuttonsfield in the input report.
Validation Steps
Once you've made these changes:
- Compile the modified code and flash it to your MSP430 development kit.
- Connect the board to a Windows PC.
- Open Settings > Bluetooth & devices > Game controllers – your device should appear as a recognized gamepad.
- Open the controller's properties to test:
- Verify all 14 analog axes show changing values when you adjust your inputs.
- Confirm each button press registers correctly.
- Check that indicators (LEDs) respond to output commands from the host.
内容的提问来源于stack exchange,提问作者ihish52

