samples: hid-mouse: add support for the new HID implementation

Add support for the new HID implementation.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
This commit is contained in:
Johann Fischer 2023-11-10 18:52:26 +01:00 committed by Anas Nashif
commit fce6b20f61
6 changed files with 87 additions and 7 deletions

View file

@ -4,5 +4,6 @@ cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(hid-mouse)
include(${ZEPHYR_BASE}/samples/subsys/usb/common/common.cmake)
FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})

View file

@ -0,0 +1,9 @@
# Copyright (c) 2023 Nordic Semiconductor ASA
# SPDX-License-Identifier: Apache-2.0
# Source common USB sample options used to initialize new experimental USB
# device stack. The scope of these options is limited to USB samples in project
# tree, you cannot use them in your own application.
source "samples/subsys/usb/common/Kconfig.sample_usbd"
source "Kconfig.zephyr"

View file

@ -1,10 +1,19 @@
sample:
name: USB HID mouse sample
common:
harness: button
filter: dt_alias_exists("sw0") and dt_alias_exists("led0")
depends_on:
- usb_device
- gpio
tests:
sample.usb.hid-mouse:
depends_on:
- usb_device
- gpio
harness: button
filter: dt_alias_exists("sw0") and dt_alias_exists("led0")
tags: usb
sample.usb_device_next.hid-mouse:
platform_allow:
- nrf52840dk/nrf52840
- frdm_k64f
extra_args:
- CONF_FILE="usbd_next_prj.conf"
- EXTRA_DTC_OVERLAY_FILE="usbd_next.overlay"
tags: usb

View file

@ -5,6 +5,8 @@
* SPDX-License-Identifier: Apache-2.0
*/
#include <sample_usbd.h>
#include <string.h>
#include <zephyr/kernel.h>
@ -14,6 +16,7 @@
#include <zephyr/sys/util.h>
#include <zephyr/usb/usb_device.h>
#include <zephyr/usb/usbd.h>
#include <zephyr/usb/class/usb_hid.h>
#include <zephyr/logging/log.h>
@ -34,10 +37,10 @@ enum mouse_report_idx {
MOUSE_REPORT_COUNT = 4,
};
static uint8_t report[MOUSE_REPORT_COUNT];
static uint8_t __aligned(sizeof(void *)) report[MOUSE_REPORT_COUNT];
static K_SEM_DEFINE(report_sem, 0, 1);
static void status_cb(enum usb_dc_status_code status, const uint8_t *param)
static inline void status_cb(enum usb_dc_status_code status, const uint8_t *param)
{
usb_status = status;
}
@ -93,6 +96,30 @@ static void input_cb(struct input_event *evt)
INPUT_CALLBACK_DEFINE(NULL, input_cb);
#if defined(CONFIG_USB_DEVICE_STACK_NEXT)
static int enable_usb_device_next(void)
{
struct usbd_contex *sample_usbd;
int err;
sample_usbd = sample_usbd_init_device(NULL);
if (sample_usbd == NULL) {
LOG_ERR("Failed to initialize USB device");
return -ENODEV;
}
err = usbd_enable(sample_usbd);
if (err) {
LOG_ERR("Failed to enable device support");
return err;
}
LOG_DBG("USB device support enabled");
return 0;
}
#endif /* IS_ENABLED(CONFIG_USB_DEVICE_STACK_NEXT) */
int main(void)
{
const struct device *hid_dev;
@ -103,7 +130,11 @@ int main(void)
return 0;
}
#if defined(CONFIG_USB_DEVICE_STACK_NEXT)
hid_dev = DEVICE_DT_GET_ONE(zephyr_hid_device);
#else
hid_dev = device_get_binding("HID_0");
#endif
if (hid_dev == NULL) {
LOG_ERR("Cannot get USB HID Device");
return 0;
@ -121,7 +152,11 @@ int main(void)
usb_hid_init(hid_dev);
#if defined(CONFIG_USB_DEVICE_STACK_NEXT)
ret = enable_usb_device_next();
#else
ret = usb_enable(status_cb);
#endif
if (ret != 0) {
LOG_ERR("Failed to enable USB");
return 0;

View file

@ -0,0 +1,15 @@
/*
* Copyright (c) 2023 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
/ {
hid_dev_0: hid_dev_0 {
compatible = "zephyr,hid-device";
interface-name = "HID0";
protocol-code = "none";
in-polling-rate = <1000>;
in-report-size = <64>;
};
};

View file

@ -0,0 +1,11 @@
CONFIG_USB_DEVICE_STACK_NEXT=y
CONFIG_USBD_HID_SUPPORT=y
CONFIG_LOG=y
CONFIG_USBD_LOG_LEVEL_WRN=y
CONFIG_USBD_HID_LOG_LEVEL_WRN=y
CONFIG_UDC_DRIVER_LOG_LEVEL_WRN=y
CONFIG_SAMPLE_USBD_PID=0x0007
CONFIG_GPIO=y
CONFIG_INPUT=y