Skip to content

Commit

Permalink
Add new Pybricks device class
Browse files Browse the repository at this point in the history
Adds a new Pybricks device class and provides the correct
descriptors so that Windows will use the WinUSB driver.
  • Loading branch information
nkarstens committed Nov 1, 2023
1 parent 2c5d111 commit 77ebf82
Show file tree
Hide file tree
Showing 6 changed files with 989 additions and 2 deletions.
1 change: 1 addition & 0 deletions bricks/_common/arm_none_eabi.mk
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ endif
SRC_STM32_USB_DEV += $(addprefix lib/pbio/drv/usb/stm32_usbd/,\
usbd_conf.c \
usbd_desc.c \
usbd_pybricks.c \
)

NXOS_SRC_C = $(addprefix lib/pbio/platform/nxt/nxos/,\
Expand Down
1 change: 1 addition & 0 deletions lib/pbio/drv/usb/stm32_usbd/usbd_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
#define USBD_MAX_NUM_CONFIGURATION 1
#define USBD_MAX_STR_DESC_SIZ 0x100
#define USBD_SELF_POWERED 1
#define USBD_CLASS_BOS_ENABLED 1

#endif /* _USBD_CONF_H_ */
149 changes: 149 additions & 0 deletions lib/pbio/drv/usb/stm32_usbd/usbd_desc.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
/* Includes ------------------------------------------------------------------*/
#include "usbd_core.h"
#include "usbd_conf.h"
#include "usbd_pybricks.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
Expand All @@ -61,12 +62,18 @@
#define DEVICE_ID3 (0x1FFF7A18)

#define USB_SIZ_STRING_SERIAL 0x1A
#define USB_SIZ_BOS_DESC 33

/* USB Standard Device Descriptor */
__ALIGN_BEGIN uint8_t USBD_DeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END = {
0x12, /* bLength */
USB_DESC_TYPE_DEVICE, /* bDescriptorType */
#if ((USBD_LPM_ENABLED == 1) || (USBD_CLASS_BOS_ENABLED == 1))
0x01, /* bcdUSB */ /* changed to USB version 2.01
in order to support BOS Desc */
#else
0x00, /* bcdUSB */
#endif /* (USBD_LPM_ENABLED == 1) || (USBD_CLASS_BOS_ENABLED == 1) */
0x02,
0x02, /* bDeviceClass */
0x02, /* bDeviceSubClass */
Expand All @@ -84,6 +91,135 @@ __ALIGN_BEGIN uint8_t USBD_DeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END = {
USBD_MAX_NUM_CONFIGURATION /* bNumConfigurations */
}; /* USB_DeviceDescriptor */

/** BOS descriptor. */
#if ((USBD_LPM_ENABLED == 1) || (USBD_CLASS_BOS_ENABLED == 1))
#if defined ( __ICCARM__ ) /* IAR Compiler */
#pragma data_alignment=4
#endif /* defined ( __ICCARM__ ) */
__ALIGN_BEGIN uint8_t USBD_BOSDesc[USB_SIZ_BOS_DESC] __ALIGN_END =
{
5, /* bLength */
USB_DESC_TYPE_BOS, /* bDescriptorType = BOS */
LOBYTE(USB_SIZ_BOS_DESC), /* wTotalLength */
HIBYTE(USB_SIZ_BOS_DESC), /* wTotalLength */
0x01, /* bNumDeviceCaps = 1 */

28, /* bLength */
USB_DEVICE_CAPABITY_TYPE, /* bDescriptorType = Device Capability */
0x05, /* bDevCapabilityType = Platform */
0x00, /* bReserved */

/*
* PlatformCapabilityUUID
* Microsoft OS 2.0 descriptor platform capability ID
* D8DD60DF-4589-4CC7-9CD2-659D9E648A9F
*/
0xDF, 0x60, 0xDD, 0xD8,
0x89, 0x45,
0xC7, 0x4C,
0x9C, 0xD2,
0x65, 0x9D, 0x9E, 0x64, 0x8A, 0x9F,

0x00, 0x00, 0x03, 0x06, /* dwWindowsVersion = 0x06030000 for Windows 8.1 Build */
LOBYTE(USB_SIZ_MS_OS_DSCRPTR_SET), /* wMSOSDescriptorSetTotalLength */
HIBYTE(USB_SIZ_MS_OS_DSCRPTR_SET), /* wMSOSDescriptorSetTotalLength */
USB_MS_VENDOR_CODE, /* bMS_VendorCode */
0x00 /* bAltEnumCode = Does not support alternate enumeration */
};
#endif /* (USBD_LPM_ENABLED == 1) || (USBD_CLASS_BOS_ENABLED == 1) */

#if ((USBD_LPM_ENABLED == 1) || (USBD_CLASS_BOS_ENABLED == 1))
#if defined ( __ICCARM__ ) /* IAR Compiler */
#pragma data_alignment=4
#endif /* defined ( __ICCARM__ ) */
__ALIGN_BEGIN uint8_t USBD_OSDescSet[USB_SIZ_MS_OS_DSCRPTR_SET] __ALIGN_END =
{
0x0A, 0x00, /* wLength = 10 */
0x00, 0x00, /* wDescriptorType = MS_OS_20_SET_HEADER_DESCRIPTOR */
0x00, 0x00, 0x03, 0x06, /* dwWindowsVersion = 0x06030000 for Windows 8.1 Build */
LOBYTE(USB_SIZ_MS_OS_DSCRPTR_SET), /* wTotalLength */
HIBYTE(USB_SIZ_MS_OS_DSCRPTR_SET), /* wTotalLength (cont.) */

0x14, 0x00, /* wLength = 20 */
0x03, 0x00, /* wDescriptorType = MS_OS_20_FEATURE_COMPATBLE_ID */
'W', 'I', 'N', 'U', 'S', 'B', /* CompatibleID */
0x00, 0x00, /* CompatibleID (cont.) */
0x00, 0x00, 0x00, 0x00, /* SubCompatibleID */
0x00, 0x00, 0x00, 0x00, /* SubCompatibleID (cont.) */

0x82, 0x00, /* wLength = 130 */
0x04, 0x00, /* wDescriptorType = MS_OS_20_FEATURE_REG_PROPERTY */
0x07, 0x00, /* wStringType = REG_MULTI_SZ */
/* wPropertyNameLength = 42 */
0x2A, 0x00,
/* PropertyName = DeviceInterfaceGUIDs */
'D', '\0',
'e', '\0',
'v', '\0',
'i', '\0',
'c', '\0',
'e', '\0',
'I', '\0',
'n', '\0',
't', '\0',
'e', '\0',
'r', '\0',
'f', '\0',
'a', '\0',
'c', '\0',
'e', '\0',
'G', '\0',
'U', '\0',
'I', '\0',
'D', '\0',
's', '\0',
'\0', '\0',

/* wPropertyDataLength = 78 */
0x4E, 0x00,
/* PropertyData = {A5C44A4C-53D4-4389-9821-AE95051908A1} */
'{', '\0',
'A', '\0',
'5', '\0',
'C', '\0',
'4', '\0',
'4', '\0',
'A', '\0',
'4', '\0',
'C', '\0',
'-', '\0',
'5', '\0',
'3', '\0',
'D', '\0',
'4', '\0',
'-', '\0',
'4', '\0',
'3', '\0',
'8', '\0',
'9', '\0',
'-', '\0',
'9', '\0',
'8', '\0',
'2', '\0',
'1', '\0',
'-', '\0',
'A', '\0',
'E', '\0',
'9', '\0',
'5', '\0',
'0', '\0',
'5', '\0',
'1', '\0',
'9', '\0',
'0', '\0',
'8', '\0',
'A', '\0',
'1', '\0',
'}', '\0',
'\0', '\0'
};
#endif /* (USBD_LPM_ENABLED == 1) || (USBD_CLASS_BOS_ENABLED == 1) */

/* USB Standard Device Descriptor */
__ALIGN_BEGIN uint8_t USBD_LangIDDesc[USB_LEN_LANGID_STR_DESC] __ALIGN_END = {
USB_LEN_LANGID_STR_DESC,
Expand Down Expand Up @@ -236,6 +372,16 @@ static uint8_t *USBD_Pybricks_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, ui
return USBD_StrDesc;
}

#if ((USBD_LPM_ENABLED == 1) || (USBD_CLASS_BOS_ENABLED == 1))
static uint8_t *USBD_Pybricks_BOSDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) {
/* Prevent unused argument(s) compilation warning */
UNUSED(speed);

*length = sizeof(USBD_BOSDesc);
return (uint8_t *)USBD_BOSDesc;
}
#endif /* (USBD_LPM_ENABLED == 1) || (USBD_CLASS_BOS_ENABLED == 1) */

USBD_DescriptorsTypeDef Pybricks_Desc = {
.GetDeviceDescriptor = USBD_Pybricks_DeviceDescriptor,
.GetLangIDStrDescriptor = USBD_Pybricks_LangIDStrDescriptor,
Expand All @@ -244,4 +390,7 @@ USBD_DescriptorsTypeDef Pybricks_Desc = {
.GetSerialStrDescriptor = USBD_Pybricks_SerialStrDescriptor,
.GetConfigurationStrDescriptor = USBD_Pybricks_ConfigStrDescriptor,
.GetInterfaceStrDescriptor = USBD_Pybricks_InterfaceStrDescriptor,
#if ((USBD_LPM_ENABLED == 1) || (USBD_CLASS_BOS_ENABLED == 1))
.GetBOSDescriptor = USBD_Pybricks_BOSDescriptor,
#endif
};
Loading

0 comments on commit 77ebf82

Please sign in to comment.