• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..--

src/29-Dec-2025-170127

CMakeLists.txtD29-Dec-2025289 107

KconfigD29-Dec-2025373 107

README.rstD29-Dec-20254.1 KiB11085

app.overlayD29-Dec-2025270 1614

prj.confD29-Dec-2025268 1310

sample.yamlD29-Dec-2025463 2120

README.rst

1.. zephyr:code-sample:: usb-hid-mouse
2   :name: USB HID mouse
3   :relevant-api: usbd_api usbd_hid_device input_interface
4
5   Implement a basic HID mouse device.
6
7Overview
8********
9
10This sample app demonstrates use of a USB Human Interface Device (HID) driver
11by the Zephyr project. This very simple driver enumerates a board with a button
12into a mouse that has a left mouse button and optionally (depending on
13the number of buttons on the board) a right mouse button, X-axis movement,
14and Y-axis movement.
15If the USB peripheral driver supports remote wakeup feature, wakeup request
16will be performed on every button click if the bus is in suspended state.
17
18Requirements
19************
20
21This project requires an USB device driver and uses the :ref:`input` API.
22There must be a :dtcompatible:`gpio-keys` group of buttons or keys defined at
23the board level that can generate input events, otherwise the example will build
24but not work as expected.
25
26The key mapping in the sample is as follows:
27
28- ``INPUT_KEY_0``: left button
29- ``INPUT_KEY_1``: right button
30- ``INPUT_KEY_2``: move the mouse along the x-axis
31- ``INPUT_KEY_3``: move the mouse along the y-axis
32
33An LED must also be configured via the ``led0`` devicetree alias. You may also
34see this error if you try to build this sample for an unsupported board:
35
36.. code-block:: none
37
38   Unsupported board: led0 devicetree alias is not defined
39
40Building and Running
41********************
42
43This sample can be built for multiple boards, in this example we will build it
44for the nucleo_f070rb board:
45
46.. zephyr-app-commands::
47   :zephyr-app: samples/subsys/usb/hid-mouse
48   :board: nucleo_f070rb
49   :goals: build
50   :compact:
51
52After you have built and flashed the sample app image to your board, plug the
53board into a host device, for example, a PC running Linux.
54The board will be detected as shown by the Linux dmesg command:
55
56.. code-block:: console
57
58    dmesg | tail -10
59    usb 2-2: new full-speed USB device number 2 using at91_ohci
60    usb 2-2: New USB device found, idVendor=2fe3, idProduct=0007, bcdDevice= 2.03
61    usb 2-2: New USB device strings: Mfr=1, Product=2, SerialNumber=3
62    usb 2-2: Product: Zephyr HID mouse sample
63    usb 2-2: Manufacturer: ZEPHYR
64    usb 2-2: SerialNumber: 86FE679A598AC47A
65    input: ZEPHYR Zephyr HID mouse sample as /devices/soc0/ahb/600000.ohci/usb2/2-2/2-2:1.0/0003:2FE3:0100.0001/input/input0
66    hid-generic 0003:2FE3:0100.0001: input: USB HID v1.10 Mouse [ZEPHYR Zephyr HID mouse sample] on usb-at91-2/input0
67
68You can also monitor mouse events by using the standard Linux ``evtest`` command
69(see the `Ubuntu evtest man page`_ for more information about this tool):
70
71.. _Ubuntu evtest man page:
72   http://manpages.ubuntu.com/manpages/trusty/man1/evtest.1.html
73
74.. code-block:: console
75
76    sudo evtest /dev/input/event0
77    Input driver version is 1.0.1
78    Input device ID: bus 0x3 vendor 0x2fe3 product 0x7 version 0x110
79    Input device name: "ZEPHYR Zephyr HID mouse sample"
80    Supported events:
81      Event type 0 (EV_SYN)
82      Event type 1 (EV_KEY)
83        Event code 272 (BTN_LEFT)
84        Event code 273 (BTN_RIGHT)
85        Event code 274 (BTN_MIDDLE)
86      Event type 2 (EV_REL)
87        Event code 0 (REL_X)
88        Event code 1 (REL_Y)
89        Event code 8 (REL_WHEEL)
90      Event type 4 (EV_MSC)
91        Event code 4 (MSC_SCAN)
92    Properties:
93    Testing ... (interrupt to exit)
94
95When you press the button on your board, it will act as if the left
96mouse button was pressed, and this information will be displayed
97by ``evtest``:
98
99.. code-block:: console
100
101    Event: time 1167609663.618515, type 4 (EV_MSC), code 4 (MSC_SCAN), value 90001
102    Event: time 1167609663.618515, type 1 (EV_KEY), code 272 (BTN_LEFT), value 1
103    Event: time 1167609663.618515, -------------- SYN_REPORT ------------
104    Event: time 1167609663.730510, type 4 (EV_MSC), code 4 (MSC_SCAN), value 90001
105    Event: time 1167609663.730510, type 1 (EV_KEY), code 272 (BTN_LEFT), value 0
106    Event: time 1167609663.730510, -------------- SYN_REPORT ------------
107
108If your board has more than one button, they will act as right mouse button,
109X-axis movement, and Y-axis movement.
110