Skip to content

Add USBX Device HID Mouse RTOS and Standalone Demo #204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
314 changes: 314 additions & 0 deletions samples/device_hid_mouse_rtos/demo_device_hid_descriptors.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,314 @@
/***************************************************************************
* Copyright (c) 2025-present Eclipse ThreadX Contributors
*
* This program and the accompanying materials are made available under the
* terms of the MIT License which is available at
* https://opensource.org/licenses/MIT.
*
*
* SPDX-License-Identifier: MIT
**************************************************************************/

/**************************************************************************/
/**************************************************************************/
/** */
/** AUTHOR */
/** */
/** Mohamed AYED */
/** */
/**************************************************************************/
/**************************************************************************/

#include "demo_device_hid_descriptors.h"

UCHAR hid_report[] = {
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x02, // USAGE (Mouse)
0xa1, 0x01, // COLLECTION (Application)

/* Pointer and Physical are required by Apple Recovery */
0x09, 0x01, // USAGE (Pointer)
0xa1, 0x00, // COLLECTION (Physical)

/* 3 Buttons */
0x05, 0x09, // USAGE_PAGE (Button)
0x19, 0x01, // USAGE_MINIMUM (Button 1)
0x29, 0x03, // USAGE_MAXIMUM (Button 3)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x01, // LOGICAL_MAXIMUM (1)
0x75, 0x01, // REPORT_SIZE (1)
0x95, 0x03, // REPORT_COUNT (3) -> 3 buttons
0x81, 0x02, // INPUT (Data, Variable, Absolute) -> Buttons

0x75, 0x05, // REPORT_SIZE (5)
0x95, 0x01, // REPORT_COUNT (1)
0x81, 0x03, // INPUT (Constant, Variable, Absolute) -> Padding bits

/* X, Y */
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x30, // USAGE (X)
0x09, 0x31, // USAGE (Y)
#ifdef UX_DEMO_MOUSE_ABSOLUTE
0x16, 0x00, 0x00, // LOGICAL_MINIMUM (0)
0x26, 0xFF, 0x7F, // LOGICAL_MAXIMUM (32767) (0x7FFF)
0x75, 0x10, // REPORT_SIZE (16) (2 bytes per axis)
0x95, 0x02, // REPORT_COUNT (2) -> X, Y position
0x81, 0x02, // INPUT (Data, Variable, Absolute) -> Absolute X, Y position
#else /* UX_DEMO_MOUSE_ABSOLUTE */
0x15, 0x81, // LOGICAL_MINIMUM (-127)
0x25, 0x7F, // LOGICAL_MAXIMUM (127)
0x75, 0x08, // REPORT_SIZE (8) (1 bytes per axis)
0x95, 0x02, // REPORT_COUNT (2) -> X, Y movement
0x81, 0x06, // INPUT (Data, Variable, Relative) -> X, Y are relative
#endif /* UX_DEMO_MOUSE_ABSOLUTE */

/* Wheel */
0x09, 0x38, // USAGE (Mouse Wheel)
0x15, 0x81, // LOGICAL_MINIMUM (-127)
0x25, 0x7F, // LOGICAL_MAXIMUM (127)
0x75, 0x08, // REPORT_SIZE (8)
0x95, 0x01, // REPORT_COUNT (1) -> Wheel movement
0x81, 0x06, // INPUT (Data, Variable, Relative) -> Wheel

/* End */
0xC0, // END_COLLECTION
0xC0 // END_COLLECTION
};

#define UX_HID_MOUSE_REPORT_LENGTH (sizeof(hid_report)/sizeof(hid_report[0]))

UCHAR ux_demo_device_framework_full_speed[] = {
/* Device descriptor */
0x12, /* bLength */
0x01, /* bDescriptorType */
UX_W0(UX_DEMO_BCD_USB), UX_W1(UX_DEMO_BCD_USB), /* bcdUSB */
0x00, /* bDeviceClass : 0x00 : Interface-defined */
0x00, /* bDeviceSubClass : 0x00 : Reset */
0x00, /* bDeviceProtocol : 0x00 : Reset */
UX_DEMO_MAX_EP0_SIZE, /* bMaxPacketSize0 */
UX_W0(UX_DEMO_HID_DEVICE_VID), UX_W1(UX_DEMO_HID_DEVICE_VID), /* idVendor : ... */
UX_W0(UX_DEMO_HID_DEVICE_PID), UX_W1(UX_DEMO_HID_DEVICE_PID), /* idProduct */
0x00, 0x00, /* bcdDevice */
0x01, /* iManufacturer */
0x02, /* iProduct */
0x03, /* iSerialNumber */
0x01, /* bNumConfigurations */

/* Configuration Descriptor, total 34 */
0x09, /* bLength */
0x02, /* bDescriptorType */
UX_W0(UX_DEMO_HID_CONFIG_DESC_SIZE), UX_W1(UX_DEMO_HID_CONFIG_DESC_SIZE), /* wTotalLength */
0x01, /* bNumInterfaces */
0x01, /* bConfigurationValue */
0x04, /* iConfiguration */
0xC0, /* bmAttributes */
/* D6 : 0x1 : Self-powered */
/* D5, Remote Wakeup : 0x0 : Not supported */
0x32, /* bMaxPower : 50 : 100mA */

/* Interface descriptor */
0x09, /* bLength */
0x04, /* bDescriptorType */
0x00, /* bInterfaceNumber */
0x00, /* bAlternateSetting */
0x01, /* bNumEndpoints */
0x03, /* bInterfaceClass : 0x03 : HID */
UX_DEMO_HID_SUBCLASS, /* bInterfaceSubClass : ... : Boot/non-boot Subclass */
0x02, /* bInterfaceProtocol : 0x00 : Undefined */
0x06, /* iInterface */

/* HID Descriptor */
0x09, /* bLength : 9 */
0x21, /* bDescriptorType : 0x21 : HID descriptor */
0x10, 0x01, /* bcdHID : 0x0110 */
0x21, /* bCountryCode : 33 : US */
0x01, /* bNumDescriptors */
0x22, /* bReportDescriptorType1 : 0x22 : Report descriptor */
UX_W0(UX_HID_MOUSE_REPORT_LENGTH), UX_W1(UX_HID_MOUSE_REPORT_LENGTH), /* wDescriptorLength1 */

/* Endpoint Descriptor */
0x07, /* bLength */
0x05, /* bDescriptorType */
UX_DEMO_HID_ENDPOINT_ADDRESS, /* bEndpointAddress */
/* D7, Direction : 0x01 */
/* D3..0, Endpoint number : 2 */
0x03, /* bmAttributes */
/* D1..0, Transfer Type : 0x3 : Interrupt */
/* D3..2, Synchronization Type : 0x0 : No Synchronization */
/* D5..4, Usage Type : 0x0 : Data endpoint */
UX_W0(UX_DEMO_HID_ENDPOINT_SIZE), UX_W1(UX_DEMO_HID_ENDPOINT_SIZE), /* wMaxPacketSize */
/* D10..0, Max Packet Size */
/* D12..11, Additional transactions : 0x00 */
UX_DEMO_HID_ENDPOINT_BINTERVAL, /* bInterval : 8 : 8ms / x128 (FS 128ms/HS 16ms) */
};


UCHAR ux_demo_device_framework_high_speed[] = {
/* Device descriptor */
0x12, /* bLength */
0x01, /* bDescriptorType */
UX_W0(UX_DEMO_BCD_USB), UX_W1(UX_DEMO_BCD_USB), /* bcdUSB */
0x00, /* bDeviceClass : 0x00 : Interface-defined */
0x00, /* bDeviceSubClass : 0x00 : Reset */
0x00, /* bDeviceProtocol : 0x00 : Reset */
UX_DEMO_MAX_EP0_SIZE, /* bMaxPacketSize0 */
UX_W0(UX_DEMO_HID_DEVICE_VID), UX_W1(UX_DEMO_HID_DEVICE_VID), /* idVendor : ... */
UX_W0(UX_DEMO_HID_DEVICE_PID), UX_W1(UX_DEMO_HID_DEVICE_PID), /* idProduct */
0x01, 0x00, /* bcdDevice */
0x01, /* iManufacturer */
0x02, /* iProduct */
0x03, /* iSerialNumber */
0x01, /* bNumConfigurations */

/* Device qualifier descriptor */
0x0A, /* bLength */
0x06, /* bDescriptorType */
UX_W0(UX_DEMO_BCD_USB), UX_W1(UX_DEMO_BCD_USB), /* bcdUSB */
0x00, /* bDeviceClass : 0x00 : Interface-defined */
0x00, /* bDeviceSubClass : 0x00 : Reset */
0x00, /* bDeviceProtocol : 0x00 : Reset */
UX_DEMO_MAX_EP0_SIZE, /* bMaxPacketSize0 */
0x01, /* bNumConfigurations */
0x00, /* bReserved */

/* Configuration descriptor */
0x09, /* bLength */
0x02, /* bDescriptorType */
UX_W0(UX_DEMO_HID_CONFIG_DESC_SIZE), UX_W1(UX_DEMO_HID_CONFIG_DESC_SIZE), /* wTotalLength */
0x01, /* bNumInterfaces */
0x01, /* bConfigurationValue */
0x05, /* iConfiguration */
0xC0, /* bmAttributes */
/* D6 : 0x1 : Self-powered */
/* D5, Remote Wakeup : 0x0 : Not supported */
0x19, /* bMaxPower : 50 : 100mA */

/* Interface descriptor */
0x09, /* bLength */
0x04, /* bDescriptorType */
0x00, /* bInterfaceNumber */
0x00, /* bAlternateSetting */
0x01, /* bNumEndpoints */
0x03, /* bInterfaceClass : 0x03 : HID */
UX_DEMO_HID_SUBCLASS, /* bInterfaceSubClass : ... : Boot/non-boot Subclass */
0x02, /* bInterfaceProtocol : 0x00 : Undefined */
0x06, /* iInterface */

/* HID Descriptor */
0x09, /* bLength : 9 */
0x21, /* bDescriptorType : 0x21 : HID descriptor */
UX_W0(UX_DEMO_BCD_HID), UX_W1(UX_DEMO_BCD_HID), /* bcdHID : 0x0110 */
0x21, /* bCountryCode : 33 : US */
0x01, /* bNumDescriptors */
0x22, /* bReportDescriptorType1 : 0x22 : Report descriptor */
UX_W0(UX_HID_MOUSE_REPORT_LENGTH), UX_W1(UX_HID_MOUSE_REPORT_LENGTH), /* wDescriptorLength1 */

/* Endpoint Descriptor (Interrupt In) */
0x07, /* bLength */
0x05, /* bDescriptorType */
UX_DEMO_HID_ENDPOINT_ADDRESS, /* bEndpointAddress */
/* D7, Direction : 0x01 */
/* D3..0, Endpoint number : 2 */
0x03, /* bmAttributes */
/* D1..0, Transfer Type : 0x3 : Interrupt */
/* D3..2, Synchronization Type : 0x0 : No Synchronization */
/* D5..4, Usage Type : 0x0 : Data endpoint */
UX_W0(UX_DEMO_HID_ENDPOINT_SIZE), UX_W1(UX_DEMO_HID_ENDPOINT_SIZE), /* wMaxPacketSize */
/* D10..0, Max Packet Size */
/* D12..11, Additional transactions : 0x00 */
UX_DEMO_HID_ENDPOINT_BINTERVAL, /* bInterval : 8 : 8ms / x128 (FS 128ms/HS 16ms) */
};


/* String Device Framework :
Byte 0 and 1 : Word containing the language ID : 0x0904 for US
Byte 2 : Byte containing the index of the descriptor
Byte 3 : Byte containing the length of the descriptor string
*/
UCHAR ux_demo_string_framework[] = {

/* iManufacturer string descriptor : Index 1 */
0x09, 0x04, 0x01, 12,
'U', 'S', 'B', 'X', ' ', 'e', 'c', 'l', 'i', 'p', 's', 'e',

/* iProduct string descriptor : Index 2 */
0x09, 0x04, 0x02, 14,
'H', 'I', 'D', ' ', 'M', 'o', 'u', 's', 'e', ' ', 'D', 'e', 'm', 'o',

/* iSerialNumber Number string descriptor : Index 3 */
0x09, 0x04, 0x03, 13,
'0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1',

/* iConfiguration string descriptor : Index 4 */
0x09, 0x04, 0x04, 10,
'F', 'U', 'L', 'L', ' ', 'S', 'P', 'E', 'E', 'D',

/* iConfiguration string descriptor : Index 5 */
0x09, 0x04, 0x05, 10,
'H', 'I', 'G', 'H', ' ', 'S', 'P', 'E', 'E', 'D',

/* iInterface string descriptor : Index 6 */
0x09, 0x04, 0x06, 5,
'M', 'o', 'u', 's', 'e'
};


/* Multiple languages are supported on the device, to add
a language besides english, the unicode language code must
be appended to the language_id_framework array and the length
adjusted accordingly. */
UCHAR ux_demo_language_id_framework[] = {
/* English. */
0x09, 0x04
};


UCHAR* ux_demo_get_high_speed_framework(VOID)
{
return ux_demo_device_framework_high_speed;
}

ULONG ux_demo_get_high_speed_framework_length(VOID)
{
return sizeof(ux_demo_device_framework_high_speed);
}

UCHAR* ux_demo_get_full_speed_framework(VOID)
{
return ux_demo_device_framework_full_speed;
}

ULONG ux_demo_get_full_speed_framework_length(VOID)
{
return sizeof(ux_demo_device_framework_full_speed);
}

UCHAR* ux_demo_get_string_framework(VOID)
{
return ux_demo_string_framework;
}

ULONG ux_demo_get_string_framework_length(VOID)
{
return sizeof(ux_demo_string_framework);
}

UCHAR* ux_demo_get_language_framework(VOID)
{
return ux_demo_language_id_framework;
}

ULONG ux_demo_get_language_framework_length(VOID)
{
return sizeof(ux_demo_language_id_framework);
}

UCHAR* ux_demo_device_hid_get_report(VOID)
{
return hid_report;
}

ULONG ux_demo_device_hid_get_report_length(VOID)
{
return (sizeof(hid_report)/sizeof(hid_report[0]));
}
70 changes: 70 additions & 0 deletions samples/device_hid_mouse_rtos/demo_device_hid_descriptors.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/***************************************************************************
* Copyright (c) 2025-present Eclipse ThreadX Contributors
*
* This program and the accompanying materials are made available under the
* terms of the MIT License which is available at
* https://opensource.org/licenses/MIT.
*
*
* SPDX-License-Identifier: MIT
**************************************************************************/

/**************************************************************************/
/**************************************************************************/
/** */
/** AUTHOR */
/** */
/** Mohamed AYED */
/** */
/**************************************************************************/
/**************************************************************************/

#ifndef UX_DEMO_DEVICE_DESCRIPTORS_H
#define UX_DEMO_DEVICE_DESCRIPTORS_H

#ifdef __cplusplus
extern "C" {
#endif

#include "ux_api.h"

//#define UX_DEMO_MOUSE_ABSOLUTE

#define DEMO_HID_BOOT_DEVICE
#define UX_DEMO_HID_DEVICE_VID 0x070A
#define UX_DEMO_HID_DEVICE_PID 0x4036

#define UX_DEMO_MAX_EP0_SIZE 0x40U
#define UX_DEMO_HID_CONFIG_DESC_SIZE 0x22U

#define UX_DEMO_BCD_USB 0x0200

#define UX_DEMO_BCD_HID 0x0110

#define UX_DEMO_HID_ENDPOINT_SIZE 0x08
#define UX_DEMO_HID_ENDPOINT_ADDRESS 0x81
#define UX_DEMO_HID_ENDPOINT_BINTERVAL 0x08

#ifdef DEMO_HID_BOOT_DEVICE
#define UX_DEMO_HID_SUBCLASS 0x01
#else /* DEMO_HID_BOOT_DEVICE */
#define UX_DEMO_HID_SUBCLASS 0x00
#endif


UCHAR* ux_demo_get_high_speed_framework(VOID);
ULONG ux_demo_get_high_speed_framework_length(VOID);
UCHAR* ux_demo_get_full_speed_framework(VOID);
ULONG ux_demo_get_full_speed_framework_length(VOID);
UCHAR* ux_demo_get_string_framework(VOID);
ULONG ux_demo_get_string_framework_length(VOID);
UCHAR* ux_demo_get_language_framework(VOID);
ULONG ux_demo_get_language_framework_length(VOID);

UCHAR* ux_demo_device_hid_get_report(VOID);
ULONG ux_demo_device_hid_get_report_length(VOID);

#ifdef __cplusplus
}
#endif
#endif /* UX_DEMO_DEVICE_DESCRIPTORS_H */
Loading