Skip to content

Commit

Permalink
drv/usb/stm32_usbd: use pbdrvconfig for USB info
Browse files Browse the repository at this point in the history
This moves the USB VID/PID and strings into pbdrvconfig.h, so that each
hub can set the appropriate values. Additionally, since SPIKE Prime and
Robot Inventor use the same firmware, we have to dynamically set the
PID at runtime based on the hub variant.
  • Loading branch information
dlech committed Dec 23, 2023
1 parent 84e3e3d commit 92f2cac
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 11 deletions.
33 changes: 33 additions & 0 deletions lib/lego/lego_usb.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2023 The Pybricks Authors

// LEGO USB stuff
// https://github.com/pybricks/technical-info/blob/master/assigned-numbers.md#usb


#ifndef _LEGO_USB_H_
#define _LEGO_USB_H_

/** Official LEGO USB Vendor ID. */
#define LEGO_USB_VID 0x0694
/** Official LEGO USB Product ID for SPIKE Prime in DFU mode. */
#define LEGO_USB_PID_SPIKE_PRIME_DFU 0x0008
/** Official LEGO USB Product ID for SPIKE Prime. */
#define LEGO_USB_PID_SPIKE_PRIME 0x0009
/** Official LEGO USB Product ID for SPIKE Essential in DFU mode. */
#define LEGO_USB_PID_SPIKE_ESSENTIAL_DFU 0x000C
/** Official LEGO USB Product ID for SPIKE Essential. */
#define LEGO_USB_PID_SPIKE_ESSENTIAL 0x000D
/** Official LEGO USB Product ID for MINDSTORMS Robot Inventor in DFU mode. */
#define LEGO_USB_PID_ROBOT_INVENTOR_DFU 0x0010
/** Official LEGO USB Product ID for MINDSTORMS Robot Inventor. */
#define LEGO_USB_PID_ROBOT_INVENTOR 0x0011

/** Official LEGO USB Manufacturer String. */
#define LEGO_USB_MFG_STR "LEGO System A/S"
/** Official LEGO USB Product String for SPIKE Prime and MINDSTORMS Robot Inventor. */
#define LEGO_USB_PROD_STR_TECHNIC_LARGE_HUB "LEGO Technic Large Hub"
/** Official LEGO USB Product String for SPIKE Essential. */
#define LEGO_USB_PROD_STR_TECHNIC_SMALL_HUB "LEGO Technic Small Hub"

#endif // _LEGO_USB_H_
42 changes: 31 additions & 11 deletions lib/pbio/drv/usb/stm32_usbd/usbd_desc.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,17 @@
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/

#include <lego_usb.h>

#include <pbdrv/config.h>

#include "usbd_core.h"
#include "usbd_conf.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define USBD_VID 0x0483
#define USBD_PID 0x5740
#define USBD_LANGID_STRING 0x409
#define USBD_MANUFACTURER_STRING "STMicroelectronics"
#define USBD_PRODUCT_FS_STRING "Pybricks Hub"
#define USBD_CONFIGURATION_FS_STRING "Pybricks Config"
#define USBD_INTERFACE_FS_STRING "Pybricks Interface"

Expand All @@ -63,7 +64,11 @@
#define USB_SIZ_STRING_SERIAL 0x1A

/* USB Standard Device Descriptor */
__ALIGN_BEGIN static const uint8_t USBD_DeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END = {
__ALIGN_BEGIN static
#if !defined(PBDRV_CONFIG_USB_STM32F4_HUB_VARIANT_ADDR)
const
#endif
uint8_t USBD_DeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END = {
0x12, /* bLength */
USB_DESC_TYPE_DEVICE, /* bDescriptorType */
0x00, /* bcdUSB */
Expand All @@ -72,10 +77,10 @@ __ALIGN_BEGIN static const uint8_t USBD_DeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END
0x02, /* bDeviceSubClass */
0x00, /* bDeviceProtocol */
USB_MAX_EP0_SIZE, /* bMaxPacketSize */
LOBYTE(USBD_VID), /* idVendor */
HIBYTE(USBD_VID), /* idVendor */
LOBYTE(USBD_PID), /* idVendor */
HIBYTE(USBD_PID), /* idVendor */
LOBYTE(PBDRV_CONFIG_USB_VID), /* idVendor */
HIBYTE(PBDRV_CONFIG_USB_VID), /* idVendor */
LOBYTE(PBDRV_CONFIG_USB_PID), /* idProduct */
HIBYTE(PBDRV_CONFIG_USB_PID), /* idProduct */
0x00, /* bcdDevice rel. 2.00 */
0x02,
USBD_IDX_MFC_STR, /* Index of manufacturer string */
Expand Down Expand Up @@ -178,7 +183,7 @@ static uint8_t *USBD_Pybricks_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint1
* @retval Pointer to descriptor buffer
*/
static uint8_t *USBD_Pybricks_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) {
USBD_GetString((uint8_t *)USBD_PRODUCT_FS_STRING, USBD_StrDesc, length);
USBD_GetString((uint8_t *)PBDRV_CONFIG_USB_PROD_STR, USBD_StrDesc, length);
return USBD_StrDesc;
}

Expand All @@ -192,7 +197,7 @@ static uint8_t *USBD_Pybricks_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed,
/* Prevent unused argument(s) compilation warning */
UNUSED(speed);

USBD_GetString((uint8_t *)USBD_MANUFACTURER_STRING, USBD_StrDesc, length);
USBD_GetString((uint8_t *)PBDRV_CONFIG_USB_MFG_STR, USBD_StrDesc, length);
return USBD_StrDesc;
}

Expand Down Expand Up @@ -245,3 +250,18 @@ USBD_DescriptorsTypeDef USBD_Pybricks_Desc = {
.GetConfigurationStrDescriptor = USBD_Pybricks_ConfigStrDescriptor,
.GetInterfaceStrDescriptor = USBD_Pybricks_InterfaceStrDescriptor,
};

void USBD_Pybricks_Desc_Init(void) {
// the same firmware runs on both SPIKE Prime and Robot Inventor so we need
// to dynamically set the PID based on the variant
#ifdef PBDRV_CONFIG_USB_STM32F4_HUB_VARIANT_ADDR
#define VARIANT (*(uint32_t *)PBDRV_CONFIG_USB_STM32F4_HUB_VARIANT_ADDR)
if (VARIANT == 0) {
USBD_DeviceDesc[10] = LOBYTE(PBDRV_CONFIG_USB_PID_0);
USBD_DeviceDesc[11] = HIBYTE(PBDRV_CONFIG_USB_PID_0);
} else if (VARIANT == 1) {
USBD_DeviceDesc[10] = LOBYTE(PBDRV_CONFIG_USB_PID_1);
USBD_DeviceDesc[11] = HIBYTE(PBDRV_CONFIG_USB_PID_1);
}
#endif
}
1 change: 1 addition & 0 deletions lib/pbio/drv/usb/stm32_usbd/usbd_desc.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
#include "usbd_def.h"

extern USBD_DescriptorsTypeDef USBD_Pybricks_Desc;
void USBD_Pybricks_Desc_Init(void);

#endif /* _USBD_DESC_H_ */
4 changes: 4 additions & 0 deletions lib/pbio/platform/essential_hub/pbdrvconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@
#define PBDRV_CONFIG_UART_STM32F4_LL_IRQ_NUM_UART (2)

#define PBDRV_CONFIG_USB (1)
#define PBDRV_CONFIG_USB_VID LEGO_USB_VID
#define PBDRV_CONFIG_USB_PID LEGO_USB_PID_SPIKE_ESSENTIAL
#define PBDRV_CONFIG_USB_MFG_STR LEGO_USB_MFG_STR
#define PBDRV_CONFIG_USB_PROD_STR LEGO_USB_PROD_STR_TECHNIC_SMALL_HUB " + Pybricks"
#define PBDRV_CONFIG_USB_STM32F4 (1)

#define PBDRV_CONFIG_WATCHDOG (1)
Expand Down
7 changes: 7 additions & 0 deletions lib/pbio/platform/prime_hub/pbdrvconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,14 @@
#define PBDRV_CONFIG_UART_STM32F4_LL_IRQ_NUM_UART (6)

#define PBDRV_CONFIG_USB (1)
#define PBDRV_CONFIG_USB_VID LEGO_USB_VID
#define PBDRV_CONFIG_USB_PID 0xFFFF
#define PBDRV_CONFIG_USB_PID_0 LEGO_USB_PID_SPIKE_PRIME
#define PBDRV_CONFIG_USB_PID_1 LEGO_USB_PID_ROBOT_INVENTOR
#define PBDRV_CONFIG_USB_MFG_STR LEGO_USB_MFG_STR
#define PBDRV_CONFIG_USB_PROD_STR LEGO_USB_PROD_STR_TECHNIC_LARGE_HUB " + Pybricks"
#define PBDRV_CONFIG_USB_STM32F4 (1)
#define PBDRV_CONFIG_USB_STM32F4_HUB_VARIANT_ADDR 0x08007d80

#define PBDRV_CONFIG_WATCHDOG (1)
#define PBDRV_CONFIG_WATCHDOG_STM32 (1)
Expand Down

0 comments on commit 92f2cac

Please sign in to comment.