USB state module

The USB state module is responsible for tracking the USB connection. It is also responsible for transmitting data through USB on the application’s device.

Module events

Source Module

Input Event

This Module

Output Event

Sink Module

Source modules for config_event

config_event

usb_state

HID forward module

hid_report_event

HID state module

HID Service module

USB state module

Source modules for module_state_event

module_state_event

Bluetooth LE advertising module

wake_up_event

Buttons module

HID forward module

Motion module

Power manager module

Wheel module

config_event

Sink modules for config_event

hid_report_event

Sink modules for hid_report_event

hid_report_sent_event

HID forward module

HID state module

Motion module

hid_report_subscriber_event

HID state module

hid_report_subscription_event

HID forward module

HID state module

Motion module

module_state_event

Sink modules for module_state_event

usb_state_event

Battery charger module

Bluetooth LE connection parameters module

Motion module

USB state power manager module

Note

See the Overview: Firmware architecture for more information about the event-based communication in the nRF Desktop application and about how to read this table.

Configuration

The module is enabled by selecting CONFIG_DESKTOP_USB_ENABLE option. The option selects CONFIG_USB_DEVICE_STACK and CONFIG_USB_DEVICE_HID Kconfig options.

The CONFIG_DESKTOP_USB_ENABLE option is implied by the CONFIG_DESKTOP_ROLE_HID_DONGLE option. The module is enabled by default for the nRF Desktop dongles because the dongles forward the HID data to the host connected over USB. See the HID configuration documentation for details.

Additionally, you can also configure the options described in the following sections.

USB device identifiers

When the USB support is enabled for the device, the following options are by default set to common HID device identifiers defined for the nRF Desktop application:

Low latency device configuration

The module sets default value of CONFIG_USB_HID_POLL_INTERVAL_MS to 1. For low latency of HID reports, the device requests a polling rate of 1 ms.

Boot protocol configuration

The CONFIG_DESKTOP_HID_BOOT_INTERFACE_MOUSE and CONFIG_DESKTOP_HID_BOOT_INTERFACE_KEYBOARD Kconfig options are used to control support of HID boot reports in the nRF Desktop application. The module aligns the USB HID boot protocol configuration with the application configuration. The module sets the default value of the CONFIG_USB_HID_BOOT_PROTOCOL Kconfig option and calls the usb_hid_set_proto_code() function during initialization to set the USB HID Boot Interface protocol code.

USB device instance configuration

The nRF Desktop device can provide multiple instances of a HID-class USB device. The number of instances is controlled by CONFIG_USB_HID_DEVICE_COUNT. By default, the option is set to:

See the subsections below for details.

nRF Desktop Peripheral

The nRF Desktop Peripheral devices by default use only a single HID-class USB instance. In that case, this instance is used for all the HID reports.

Enable CONFIG_DESKTOP_USB_SELECTIVE_REPORT_SUBSCRIPTION to use more than one HID-class USB instance on nRF Desktop Peripheral. Make sure to set a greater value in the CONFIG_USB_HID_DEVICE_COUNT option and create an additional usb_state_def.h header in the configuration. The header assigns HID reports to the HID-class USB instances. A given HID report can be handled only by a single HID-class USB instance. For example, the file contents can look as follows:

#include "hid_report_desc.h"

/* This configuration file is included only once from usb_state module and holds
 * information about HID report subscriptions of USB HID instances.
 */

/* This structure enforces the header file is included only once in the build.
 * Violating this requirement triggers a multiple definition error at link time.
 */
const struct {} usb_state_def_include_once;

static const uint32_t usb_hid_report_bm[] = {
       BIT(REPORT_ID_MOUSE),
       BIT(REPORT_ID_KEYBOARD_KEYS),
};

The usb_hid_report_bm defines HID reports handled by a HID-class USB instance, in a bitmask format. In this example, the HID mouse input report is handled by the first HID-class USB instance and the HID keyboard input report is handled by the second HID-class USB instance.

nRF Desktop Central

The nRF Desktop Central device can use either a single HID-class USB instance or a number of instances equal to that specified in the CONFIG_DESKTOP_HID_DONGLE_BOND_COUNT option. If only one instance is used, reports from all Peripherals connected to the Central are forwarded to the same instance. In other cases, reports from each of the bonded peripherals are forwarded to a dedicated HID-class USB instance. The same instance is used after reconnection.

USB wakeup configuration

The nRF Desktop device can work as a source of wakeup events for the host device if connected through the USB. To use the feature, enable the CONFIG_DESKTOP_USB_REMOTE_WAKEUP option. This option selects the CONFIG_USB_DEVICE_REMOTE_WAKEUP to enable required dependencies in the USB stack.

When host enters the suspended state, the USB will be suspended as well. With this feature enabled, this state change is used to suspend the nRF Desktop device (see Power manager module). When the nRF Desktop device wakes up from standby, the USB state module will issue a wakeup request on the USB.

Note

The USB wakeup request is transmitted to the host only if the host enables this request before suspending the USB.

Implementation details

The USB state module registers the CONFIG_USB_HID_DEVICE_COUNT instances of HID-class USB device and initializes the USB subsystem.

The necessary callbacks are connected to the module to ensure that the state of the USB connection is tracked. From the application’s viewpoint, USB can be in the following states:

These states are broadcast by the USB state module with a usb_state_event. When the device is connected to the host and configured for the communication, the module will broadcast the USB_STATE_ACTIVE state. The module will also subscribe to all HID reports available in the application for the selected protocol.

When the device is disconnected from the host, the module will unsubscribe from receiving the HID reports.

When the HID report data is transmitted through hid_report_event, the module will pass it to the associated endpoint. Upon data delivery, hid_report_sent_event is submitted by the module.

Note

Only one report can be transmitted by the module to a single instance of HID-class USB device at any given time. Different instances can transmit reports in parallel.

The USB state module is a transport for Configuration channel when the channel is enabled.

The module also handles a HID keyboard LED output report received through USB from the connected host. The module sends the report using hid_report_event, that is handled either by HID state module (for peripheral) or by the HID forward module (for dongle).