USB Human Interface Device (HID) Configuration

USB Human Interface Device (HID) Configuration

General HID Configuration

There are two options for using a USB mouse or a USB keyboard - the standalone Boot Protocol (HIDBP) way and the full featured HID driver way. The Boot Protocol way is generally inferior, and this document describes the full featured way. The Boot Protocol way may be appropriate for embedded systems and other systems with resource constraints and no real need for the full keyboard and mouse capabilities.

It is important to remember that the HID driver handles those devices (or actually those interfaces on each device) that claim to comply with the Human Interface Device (HID) specification. However the HID specification doesn't say anything about what the HID driver should do with information received from a HID device, or where the information that is sent to a device comes from, since this is obviously dependent on what the device is supposed to be doing, and what the operating system is. Linux (at the operating system kernel level) supports four interfaces to a HID device - keyboard, mouse, joystick and a generic interface, known as the event interface. These are implemented by the Input device level.

HID Mouse Configuration

In the kernel configuration stage, you need to turn on USB Human Interface Device (HID) support in the USB support and Mouse Support in the Input core support. You don't need to worry about the screen resolution entries for a normal mouse - these are for mouse-like devices such as a graphics tablet. Do not turn on USB HIDBP Mouse support. Perform the normal kernel rebuild and installation steps. If you are installing as modules, you need to load the input.o, hid.o and mousedev.o modules.

Plug in a USB mouse and check that your mouse has been correctly sensed by the kernel. If you don't have a kernel message, look for the changes to /proc/bus/usb/devices.

Since USB supports multiple identical devices, you can have multiple mice plugged in. You can get each mouse seperately, or you can get them all mixed together. You almost always want the mixed version, and that is what will be used in this example. You need to set up a device node entry for the mixed mice. It is customary to create the entries for this device in the /dev/input/ directory. Use the following commands:

mkdir /dev/input
mknod /dev/input/mice c 13 63

If you cat /dev/input/mice you should see some bizarre looking characters as you move the mouse or click any of the buttons.

If you want to use the mouse under X, you have various options. Which one you select is dependent on what version of XFree86 you are using and whether you are using only USB for your mouse (or mice), or whether you want to use a USB mouse and some other kind of pointer device.

 

 

  • You need to edit the XF86Config file (usually /usr/X11R6/lib/X11/XF86Config or /etc/X11/XF86Config).

  • If you are using XFree86 version 4.0 or later, add a InputDevice section that looks like the following:

    Section "InputDevice"
         Identifier  "USB Mice"
         Driver      "mouse"
         Option      "Protocol"   "IMPS/2"
         Option      "Device"     "/dev/input/mice"
    EndSection
    or, if you want to use a wheel mouse, something like:
    Section "InputDevice"
         Identifier  "USB Mice"
         Driver      "mouse"
         Option      "Protocol"   "IMPS/2"
         Option      "Device"     "/dev/input/mice"
         Option      "ZAxisMapping"   "4 5"
         Option      "Buttons"        "5"
    EndSection
    may be more useful. Consult the XFree86 documentation for a detailed explaination and more examples.

    You also need to add an entry to each applicable ServerLayout Section. These are normally at the end of the configuration file. If you only have a USB mouse (or USB mice), then replace the line with the "CorePointer" entry with the following line:

         InputDevice "USB Mice" "CorePointer"
    If you want to use both a USB mouse (or USB mice) and some other kind of pointer device, then add (do not replace) the following line to the applicable ServerLayout sections:
         InputDevice "USB Mice" "SendCoreEvents"

  • If you are using only a USB mouse (or USB mice) with XFree86 3.3, edit the Pointer section so that it looks like the following:

    Section "Pointer"
        Protocol    "IMPS/2"
        Device      "/dev/input/mice"
    EndSection

  • If you are trying to use a USB mouse (or USB mice) in addition to another pointer type device with XFree86 3.3, then you need to use the XInput extensions. Keep the existing Pointer (or modify it as required for the other device if you are doing an initial installation), and add the following entry (anywhere sensible, ideally in the Input devices area):

    Section "Xinput"
       SubSection "Mouse"
            DeviceName   "USB Mice"
            Protocol     "IMPS/2"
            Port         "/dev/input/mice"
            AlwaysCore
       EndSubSection
    EndSection

  • Restart the X server. If you don't have any mouse support at this point, remember that Ctrl-Alt-F1 will get you a virtual terminal that you can use to kill the xserver and start debugging from the error messages.

 

If you want to use the mouse under gpm, run (or kill and restart if it is already running) gpm with the following options. gpm -m /dev/input/mice -t imps2 (as superuser remember). You can make this the default if you edit the initialisation files. These are typically named something like rc.d and are in /etc/rc.d/ on RedHat distributions.

If you have both a USB mouse (or USB mice) and some other kind of pointer device, you may wish to use gpm in repeater mode. If you have a PS/2 mouse on /dev/psaux and a USB mouse (or USB mice) on /dev/input/mice, then the following gpm command would probably be appropriate: gpm -m /dev/input/mice -t imps2 -M -m /dev/psaux -t ps2 -R imps2. Note that this will make the output appear on /dev/gpmdata, which is a FIFO and does not need to be created in advance. You can use this as the mouse "device" to non-X programs, and both mice will work together.

Keyboard Configuration

You may not need any operating system support at all to use a USB keyboard if you have a PC architecture. There are several BIOS available where the BIOS can provide USB support from a keyboard plugged into the root hub on the motherboard. This may or may not work through other hubs and does not normally work with add-in boards, so you might want to add in support anyway. You definately want to add keyboard support if you add any operating system support, as the Linux USB support will disable the BIOS support. You also need to use Linux USB keyboard support if you want to use any of the "multimedia" types keys that are provided with some USB keybords.

In the kernel configuration stage, you need to turn on USB Human Interface Device (HID) support in USB support and Keyboard support in Input core support. Do not turn on USB HIDBP Keyboard support. Perform the normal kernel rebuild and installation steps. If you are installing as modules, you need to load the hid.o, input.o and keybdev.o modules.

Check the kernel logs to ensure that your keyboard is being correctly sensed by the kernel.

At this point, you should be able to use your USB keyboard as a normal keyboard. Be aware that LILO is not USB aware, and that unless your BIOS supports a USB keyboard, you may not be able to select a non-default boot image using the USB keyboard. I have personally used only a USB keyboard (and USB mouse) and have experienced no problems.

USB Joystick and Gamepad support

In the kernel configuration stage, you need to turn on USB Human Interface Device (HID) support in USB support and Joystick support in Input core support. Perform the normal kernel rebuild and installation steps. If you are installing as modules, you need to load the hid.o, input.o and joydev.o modules.

You need to set up a device node entry for the joystick. It is customary to create the entries for USB device in the /dev/input/ directory. You can use the following commands to create four device nodes, although there is no reason why you can not use more:

mknod /dev/input/js0 c 13 0
mknod /dev/input/js1 c 13 1
mknod /dev/input/js2 c 13 2
mknod /dev/input/js3 c 13 3

If you plug in a gamepad or joystick and cat /dev/input/js0 you should see some bizarre looking characters as you move the stick or click any of the buttons.

You should now be able to use the USB joystick or gamepad with any of the normal games or other joystick compatible applications.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是基于STM32CubeMX和HAL库的USB复合设备HID和Audio的例子,可以参考实现自己的USB复合设备。 首先,在STM32CubeMX中进行如下配置: 1. 配置USB引脚 在Pinout选项卡中,选择需要使用的USB引脚,并进行配置。 2. 配置USB Device 在中间的Configuration选项卡中,选择USB Device,并进行配置。 - Class For FS IP: CDC and HID - Class For HS IP: CDC and HID - Configuration Parameters: Default 3. 配置USB CDC 在右侧的Connectivity选项卡中,选择USB_DEVICE,并进行配置。 - Class/Interface: Communication Device Class (CDC) - SubClass: None - Protocol: None 4. 配置USB HID 在右侧的Connectivity选项卡中,选择USB_DEVICE,并进行配置。 - Class/Interface: Human Interface Device (HID) - SubClass: None - Protocol: None 5. 配置USB Audio 在右侧的Connectivity选项卡中,选择USB_DEVICE,并进行配置。 - Class/Interface: Audio - SubClass: Audio Control - Protocol: None 接下来,根据以上配置生成项目代码,并根据需要进行修改。 1. HID设备实现 在main.c文件中,可以使用以下代码配置HID设备: ```c #include "usbd_hid.h" USBD_HandleTypeDef hUsbDeviceFS; uint8_t HID_Buffer[4]; static void MX_USB_DEVICE_Init(void) { /* Init Device Library,Add Supported Class and Start the library*/ USBD_Init(&hUsbDeviceFS, &FS_Desc, DEVICE_FS); USBD_RegisterClass(&hUsbDeviceFS, &USBD_HID); USBD_Start(&hUsbDeviceFS); } void USB_HID_Send(uint8_t *data, uint16_t len) { USBD_HID_SendReport(&hUsbDeviceFS, data, len); } void USB_HID_Init(void) { MX_USB_DEVICE_Init(); } void USB_HID_Task(void) { // 读取HID_Buffer的数据并处理 } int main(void) { USB_HID_Init(); while (1) { USB_HID_Task(); } } ``` 2. Audio设备实现 在main.c文件中,可以使用以下代码配置Audio设备: ```c #include "usbd_audio.h" USBD_HandleTypeDef hUsbDeviceFS; static void MX_USB_DEVICE_Init(void) { /* Init Device Library,Add Supported Class and Start the library*/ USBD_Init(&hUsbDeviceFS, &FS_Desc, DEVICE_FS); USBD_RegisterClass(&hUsbDeviceFS, &USBD_AUDIO); USBD_Start(&hUsbDeviceFS); } void USB_AUDIO_Send(uint8_t *data, uint16_t len) { USBD_AUDIO_Sync(&hUsbDeviceFS, data, len); } void USB_AUDIO_Init(void) { MX_USB_DEVICE_Init(); } void USB_AUDIO_Task(void) { // 处理音频数据并写入USB_AUDIO_Send中 } int main(void) { USB_AUDIO_Init(); while (1) { USB_AUDIO_Task(); } } ``` 需要注意的是,以上代码中的USB_HID_Send和USB_AUDIO_Send需要根据具体的设备进行实现。同时,需要根据设备类型配置对应的HID描述符和Audio描述符,这些描述符可以在STM32CubeMX中进行配置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值