Skip to content

Commit 1e50aaa

Browse files
jfischer-nocfriedt
authored andcommitted
samples: usb: move the legacy code out of the HID mouse sample
Move the legacy code from the HID mouse sample to a separate sample in the legacy directory. Signed-off-by: Johann Fischer <[email protected]>
1 parent a7b0f71 commit 1e50aaa

File tree

11 files changed

+364
-101
lines changed

11 files changed

+364
-101
lines changed

samples/subsys/usb/hid-mouse/README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.. zephyr:code-sample:: usb-hid-mouse
22
:name: USB HID mouse
3-
:relevant-api: _usb_device_core_api usb_hid_device_api input_interface
3+
:relevant-api: usbd_api usbd_hid_device input_interface
44

55
Implement a basic HID mouse device.
66

samples/subsys/usb/hid-mouse/prj.conf

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
CONFIG_USB_DEVICE_STACK=y
2-
CONFIG_USB_DEVICE_HID=y
3-
CONFIG_USB_DEVICE_PRODUCT="Zephyr HID mouse sample"
4-
CONFIG_USB_DEVICE_PID=0x0007
5-
CONFIG_USB_DEVICE_INITIALIZE_AT_BOOT=n
1+
CONFIG_USB_DEVICE_STACK_NEXT=y
2+
CONFIG_USBD_HID_SUPPORT=y
63

74
CONFIG_LOG=y
8-
CONFIG_USB_DRIVER_LOG_LEVEL_ERR=y
9-
CONFIG_USB_DEVICE_LOG_LEVEL_ERR=y
5+
CONFIG_USBD_LOG_LEVEL_WRN=y
6+
CONFIG_USBD_HID_LOG_LEVEL_WRN=y
7+
CONFIG_UDC_DRIVER_LOG_LEVEL_WRN=y
8+
CONFIG_SAMPLE_USBD_PID=0x0007
109

1110
CONFIG_GPIO=y
1211
CONFIG_INPUT=y
13-
CONFIG_INPUT_MODE_SYNCHRONOUS=y

samples/subsys/usb/hid-mouse/sample.yaml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@ common:
66
depends_on:
77
- gpio
88
tests:
9-
sample.usb.hid-mouse:
10-
depends_on:
11-
- usb_device
12-
platform_exclude:
13-
- frdm_mcxn947/mcxn947/cpu0
14-
tags: usb
159
sample.usb_device_next.hid-mouse:
1610
depends_on:
1711
- usbd
@@ -23,7 +17,4 @@ tests:
2317
- nucleo_f413zh
2418
- mimxrt1060_evk/mimxrt1062/qspi
2519
- max32690evkit/max32690/m4
26-
extra_args:
27-
- CONF_FILE="usbd_next_prj.conf"
28-
- EXTRA_DTC_OVERLAY_FILE="usbd_next.overlay"
2920
tags: usb

samples/subsys/usb/hid-mouse/src/main.c

Lines changed: 48 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,14 @@
1515
#include <zephyr/input/input.h>
1616
#include <zephyr/sys/util.h>
1717

18-
#include <zephyr/usb/usb_device.h>
1918
#include <zephyr/usb/usbd.h>
20-
#include <zephyr/usb/class/usb_hid.h>
19+
#include <zephyr/usb/class/usbd_hid.h>
2120

2221
#include <zephyr/logging/log.h>
2322
LOG_MODULE_REGISTER(main, LOG_LEVEL_INF);
2423

2524
static const struct gpio_dt_spec led0 = GPIO_DT_SPEC_GET(DT_ALIAS(led0), gpios);
2625
static const uint8_t hid_report_desc[] = HID_MOUSE_REPORT_DESC(2);
27-
static enum usb_dc_status_code usb_status;
2826

2927
#define MOUSE_BTN_LEFT 0
3028
#define MOUSE_BTN_RIGHT 1
@@ -38,22 +36,7 @@ enum mouse_report_idx {
3836
};
3937

4038
K_MSGQ_DEFINE(mouse_msgq, MOUSE_REPORT_COUNT, 2, 1);
41-
static K_SEM_DEFINE(ep_write_sem, 0, 1);
42-
43-
static inline void status_cb(enum usb_dc_status_code status, const uint8_t *param)
44-
{
45-
usb_status = status;
46-
}
47-
48-
static ALWAYS_INLINE void rwup_if_suspended(void)
49-
{
50-
if (IS_ENABLED(CONFIG_USB_DEVICE_REMOTE_WAKEUP)) {
51-
if (usb_status == USB_DC_SUSPEND) {
52-
usb_wakeup_request();
53-
return;
54-
}
55-
}
56-
}
39+
static bool mouse_ready;
5740

5841
static void input_cb(struct input_event *evt, void *user_data)
5942
{
@@ -63,11 +46,9 @@ static void input_cb(struct input_event *evt, void *user_data)
6346

6447
switch (evt->code) {
6548
case INPUT_KEY_0:
66-
rwup_if_suspended();
6749
WRITE_BIT(tmp[MOUSE_BTN_REPORT_IDX], MOUSE_BTN_LEFT, evt->value);
6850
break;
6951
case INPUT_KEY_1:
70-
rwup_if_suspended();
7152
WRITE_BIT(tmp[MOUSE_BTN_REPORT_IDX], MOUSE_BTN_RIGHT, evt->value);
7253
break;
7354
case INPUT_KEY_2:
@@ -99,42 +80,30 @@ static void input_cb(struct input_event *evt, void *user_data)
9980

10081
INPUT_CALLBACK_DEFINE(NULL, input_cb, NULL);
10182

102-
#if defined(CONFIG_USB_DEVICE_STACK_NEXT)
103-
static int enable_usb_device_next(void)
83+
static void mouse_iface_ready(const struct device *dev, const bool ready)
10484
{
105-
struct usbd_context *sample_usbd;
106-
int err;
107-
108-
sample_usbd = sample_usbd_init_device(NULL);
109-
if (sample_usbd == NULL) {
110-
LOG_ERR("Failed to initialize USB device");
111-
return -ENODEV;
112-
}
113-
114-
err = usbd_enable(sample_usbd);
115-
if (err) {
116-
LOG_ERR("Failed to enable device support");
117-
return err;
118-
}
119-
120-
LOG_DBG("USB device support enabled");
121-
122-
return 0;
85+
LOG_INF("HID device %s interface is %s",
86+
dev->name, ready ? "ready" : "not ready");
87+
mouse_ready = ready;
12388
}
124-
#endif /* defined(CONFIG_USB_DEVICE_STACK_NEXT) */
12589

126-
static void int_in_ready_cb(const struct device *dev)
90+
static int mouse_get_report(const struct device *dev,
91+
const uint8_t type, const uint8_t id, const uint16_t len,
92+
uint8_t *const buf)
12793
{
128-
ARG_UNUSED(dev);
129-
k_sem_give(&ep_write_sem);
94+
LOG_WRN("Get Report not implemented, Type %u ID %u", type, id);
95+
96+
return 0;
13097
}
13198

132-
static const struct hid_ops ops = {
133-
.int_in_ready = int_in_ready_cb,
99+
struct hid_device_ops mouse_ops = {
100+
.iface_ready = mouse_iface_ready,
101+
.get_report = mouse_get_report,
134102
};
135103

136104
int main(void)
137105
{
106+
struct usbd_context *sample_usbd;
138107
const struct device *hid_dev;
139108
int ret;
140109

@@ -143,51 +112,58 @@ int main(void)
143112
return 0;
144113
}
145114

146-
#if defined(CONFIG_USB_DEVICE_STACK_NEXT)
147-
hid_dev = DEVICE_DT_GET_ONE(zephyr_hid_device);
148-
#else
149-
hid_dev = device_get_binding("HID_0");
150-
#endif
151-
if (hid_dev == NULL) {
152-
LOG_ERR("Cannot get USB HID Device");
153-
return 0;
154-
}
155-
156115
ret = gpio_pin_configure_dt(&led0, GPIO_OUTPUT);
157-
if (ret < 0) {
116+
if (ret != 0) {
158117
LOG_ERR("Failed to configure the LED pin, error: %d", ret);
159118
return 0;
160119
}
161120

162-
usb_hid_register_device(hid_dev,
163-
hid_report_desc, sizeof(hid_report_desc),
164-
&ops);
121+
hid_dev = DEVICE_DT_GET_ONE(zephyr_hid_device);
122+
if (!device_is_ready(hid_dev)) {
123+
LOG_ERR("HID Device is not ready");
124+
return -EIO;
125+
}
165126

166-
usb_hid_init(hid_dev);
127+
ret = hid_device_register(hid_dev,
128+
hid_report_desc, sizeof(hid_report_desc),
129+
&mouse_ops);
130+
if (ret != 0) {
131+
LOG_ERR("Failed to register HID Device, %d", ret);
132+
return ret;
133+
}
167134

168-
#if defined(CONFIG_USB_DEVICE_STACK_NEXT)
169-
ret = enable_usb_device_next();
170-
#else
171-
ret = usb_enable(status_cb);
172-
#endif
135+
sample_usbd = sample_usbd_init_device(NULL);
136+
if (sample_usbd == NULL) {
137+
LOG_ERR("Failed to initialize USB device");
138+
return -ENODEV;
139+
}
140+
141+
ret = usbd_enable(sample_usbd);
173142
if (ret != 0) {
174-
LOG_ERR("Failed to enable USB");
175-
return 0;
143+
LOG_ERR("Failed to enable device support");
144+
return ret;
176145
}
177146

147+
LOG_DBG("USB device support enabled");
148+
178149
while (true) {
179150
UDC_STATIC_BUF_DEFINE(report, MOUSE_REPORT_COUNT);
180151

181152
k_msgq_get(&mouse_msgq, &report, K_FOREVER);
182153

183-
ret = hid_int_ep_write(hid_dev, report, MOUSE_REPORT_COUNT, NULL);
154+
if (!mouse_ready) {
155+
LOG_INF("USB HID device is not ready");
156+
continue;
157+
}
158+
159+
ret = hid_device_submit_report(hid_dev, MOUSE_REPORT_COUNT, report);
184160
if (ret) {
185-
LOG_ERR("HID write error, %d", ret);
161+
LOG_ERR("HID submit report error, %d", ret);
186162
} else {
187-
k_sem_take(&ep_write_sem, K_FOREVER);
188163
/* Toggle LED on sent report */
189164
(void)gpio_pin_toggle(led0.port, led0.pin);
190165
}
191166
}
167+
192168
return 0;
193169
}

samples/subsys/usb/hid-mouse/usbd_next_prj.conf

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

0 commit comments

Comments
 (0)