Skip to content
Merged
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
1 change: 0 additions & 1 deletion bricks/_common/arm_none_eabi.mk
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,6 @@ NXOS_SRC_C = $(addprefix lib/pbio/platform/nxt/nxos/,\
drivers/radar.c \
drivers/rs485.c \
drivers/sensors.c \
drivers/usb.c \
interrupts.c \
lock.c \
util.c \
Expand Down
1 change: 1 addition & 0 deletions bricks/_common/sources.mk
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ PBIO_SRC_C = $(addprefix lib/pbio/,\
drv/uart/uart_stm32f4_ll_irq.c \
drv/uart/uart_stm32l4_ll_dma.c \
drv/usb/usb_ev3.c \
drv/usb/usb_nxt.c \
drv/usb/usb_stm32.c \
drv/watchdog/watchdog_ev3.c \
drv/watchdog/watchdog_stm32.c \
Expand Down
12 changes: 0 additions & 12 deletions lib/contiki-core/sys/cc.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +134,6 @@
#define NULL 0
#endif /* NULL */

#ifndef MAX
#define MAX(n, m) (((n) < (m)) ? (m) : (n))
#endif

#ifndef MIN
#define MIN(n, m) (((n) < (m)) ? (n) : (m))
#endif

#ifndef ABS
#define ABS(n) (((n) < 0) ? -(n) : (n))
#endif


#define CC_CONCAT2(s1, s2) s1##s2
/**
Expand Down
5 changes: 3 additions & 2 deletions lib/pbio/drv/reset/reset_nxt.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
#include <at91sam7s256.h>
#include <nxos/drivers/_avr.h>
#include <nxos/drivers/_lcd.h>
#include <nxos/drivers/_usb.h>
#include <nxos/drivers/bt.h>

#include <pbdrv/reset.h>

#include "../usb/usb_nxt.h"

void pbdrv_reset_init(void) {
}

Expand Down Expand Up @@ -70,7 +71,7 @@ void pbdrv_reset_power_off(void) {
}

nx__lcd_shutdown();
nx__usb_disable();
pbdrv_usb_nxt_deinit();
nx__avr_power_down();
}

Expand Down
206 changes: 45 additions & 161 deletions lib/pbio/drv/usb/stm32_usbd/usbd_desc.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,8 @@

#include <lego/usb.h>

#include <pbdrv/bluetooth.h>
#include <pbdrv/config.h>
#include <pbio/protocol.h>
#include <pbio/version.h>
#include <pbsys/storage.h>

#include "usbd_core.h"
#include "usbd_conf.h"
Expand All @@ -63,37 +60,28 @@
#define USBD_CONFIGURATION_FS_STRING "Pybricks Config"
#define USBD_INTERFACE_FS_STRING "Pybricks Interface"

static const char firmware_version[] = PBIO_VERSION_STR;
static const char software_version[] = PBIO_PROTOCOL_VERSION_STR;

// STM32 MCU Device ID register addresses
// REVISIT: make pbdrv_xxx_get_serial_number() and use that instead
#define DEVICE_ID1 (0x1FFF7A10)
#define DEVICE_ID2 (0x1FFF7A14)
#define DEVICE_ID3 (0x1FFF7A18)

#define USB_DEV_CAP_TYPE_PLATFORM (5)
// bDevCapabilityType for USB_DEVICE_CAPABITY_TYPE
#define USB_DEV_CAP_TYPE_PLATFORM (5)

#define USB_SIZ_STRING_SERIAL 0x1A
#define USB_SIZ_BOS_DESC_CONST (5 + 28 + 24)
#define USB_SIZ_UUID (128 / 8)
#define USB_SIZ_PLATFORM_HDR (4 + USB_SIZ_UUID)
#define USB_SIZ_HUB_NAME_MAX (16)
#define USB_SIZ_BOS_DESC (USB_SIZ_BOS_DESC_CONST + \
USB_SIZ_PLATFORM_HDR + USB_SIZ_HUB_NAME_MAX + \
USB_SIZ_PLATFORM_HDR + sizeof(firmware_version) - 1 + \
USB_SIZ_PLATFORM_HDR + sizeof(software_version) - 1 + \
USB_SIZ_PLATFORM_HDR + PBIO_PYBRICKS_HUB_CAPABILITIES_VALUE_SIZE)
// descriptor sizes
#define USB_SIZ_STRING_SERIAL 26
#define USB_SIZ_BOS_DESC (5 + 28 + 24)

/* USB Standard Device Descriptor */
__ALIGN_BEGIN static
#if !defined(PBDRV_CONFIG_USB_STM32F4_HUB_VARIANT_ADDR)
const
#endif
uint8_t USBD_DeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END = {
uint8_t USBD_DeviceDesc[] __ALIGN_END = {
0x12, /* bLength */
USB_DESC_TYPE_DEVICE, /* bDescriptorType */
0x01, /* bcdUSB */ /* changed to USB version 2.01
in order to support BOS Desc */
0x02,
0x10, 0x02, /* bcdUSB = 2.1.0 (for BOS support) */
PBIO_PYBRICKS_USB_DEVICE_CLASS, /* bDeviceClass */
PBIO_PYBRICKS_USB_DEVICE_SUBCLASS, /* bDeviceSubClass */
PBIO_PYBRICKS_USB_DEVICE_PROTOCOL, /* bDeviceProtocol */
Expand All @@ -102,72 +90,75 @@ uint8_t USBD_DeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END = {
HIBYTE(PBDRV_CONFIG_USB_VID), /* idVendor */
LOBYTE(PBDRV_CONFIG_USB_PID), /* idProduct */
HIBYTE(PBDRV_CONFIG_USB_PID), /* idProduct */
0x00, /* bcdDevice rel. 2.00 */
0x02,
0x00, 0x02, /* bcdDevice rel. 2.0.0 */
USBD_IDX_MFC_STR, /* Index of manufacturer string */
USBD_IDX_PRODUCT_STR, /* Index of product string */
USBD_IDX_SERIAL_STR, /* Index of serial number string */
USBD_MAX_NUM_CONFIGURATION /* bNumConfigurations */
}; /* USB_DeviceDescriptor */
_Static_assert(USB_LEN_DEV_DESC == sizeof(USBD_DeviceDesc));

/** BOS descriptor. */
__ALIGN_BEGIN static uint8_t USBD_BOSDesc[USB_SIZ_BOS_DESC] __ALIGN_END =
__ALIGN_BEGIN static const uint8_t USBD_BOSDesc[] __ALIGN_END =
{
5, /* bLength */
USB_DESC_TYPE_BOS, /* bDescriptorType = BOS */
LOBYTE(USB_SIZ_BOS_DESC), /* wTotalLength */
HIBYTE(USB_SIZ_BOS_DESC), /* wTotalLength */
2, /* bNumDeviceCaps */

28, /* bLength */
// IMPORTANT: The WebUSB descriptor must be first to make Chromium happy.

24, /* bLength */
USB_DEVICE_CAPABITY_TYPE, /* bDescriptorType = Device Capability */
USB_DEV_CAP_TYPE_PLATFORM, /* bDevCapabilityType */
0x00, /* bReserved */

/*
* PlatformCapabilityUUID
* Microsoft OS 2.0 descriptor platform capability ID
* D8DD60DF-4589-4CC7-9CD2-659D9E648A9F
* WebUSB Platform Capability descriptor
* 3408B638-09A9-47A0-8BFD-A0768815B665
* RFC 4122 explains the correct byte ordering
*/
0xDF, 0x60, 0xDD, 0xD8, /* 32-bit value */
0x89, 0x45, /* 16-bit value */
0xC7, 0x4C, /* 16-bit value */
0x9C, 0xD2,
0x65, 0x9D, 0x9E, 0x64, 0x8A, 0x9F,
0x38, 0xB6, 0x08, 0x34, /* 32-bit value */
0xA9, 0x09, /* 16-bit value */
0xA0, 0x47, /* 16-bit value */
0x8B, 0xFD,
0xA0, 0x76, 0x88, 0x15, 0xB6, 0x65,

0x00, 0x00, 0x03, 0x06, /* dwWindowsVersion = 0x06030000 for Windows 8.1 Build */
LOBYTE(USBD_SIZ_MS_OS_DSCRPTR_SET), /* wMSOSDescriptorSetTotalLength */
HIBYTE(USBD_SIZ_MS_OS_DSCRPTR_SET), /* wMSOSDescriptorSetTotalLength */
USBD_MS_VENDOR_CODE, /* bMS_VendorCode */
0x00, /* bAltEnumCode = Does not support alternate enumeration */
LOBYTE(0x0100), /* bcdVersion */
HIBYTE(0x0100), /* bcdVersion */
USBD_VENDOR_CODE_WEBUSB, /* bVendorCode */
USBD_WEBUSB_LANDING_PAGE_IDX, /* iLandingPage */

24, /* bLength */
// IMPORTANT: The MS OS 2.0 descriptor must be last to make Chromium happy.

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

/*
* PlatformCapabilityUUID
* WebUSB Platform Capability descriptor
* 3408B638-09A9-47A0-8BFD-A0768815B665
* Microsoft OS 2.0 descriptor platform capability ID
* D8DD60DF-4589-4CC7-9CD2-659D9E648A9F
* RFC 4122 explains the correct byte ordering
*/
0x38, 0xB6, 0x08, 0x34, /* 32-bit value */
0xA9, 0x09, /* 16-bit value */
0xA0, 0x47, /* 16-bit value */
0x8B, 0xFD,
0xA0, 0x76, 0x88, 0x15, 0xB6, 0x65,
0xDF, 0x60, 0xDD, 0xD8, /* 32-bit value */
0x89, 0x45, /* 16-bit value */
0xC7, 0x4C, /* 16-bit value */
0x9C, 0xD2,
0x65, 0x9D, 0x9E, 0x64, 0x8A, 0x9F,

LOBYTE(0x0100), /* bcdVersion */
HIBYTE(0x0100), /* bcdVersion */
USBD_WEBUSB_VENDOR_CODE, /* bVendorCode */
USBD_WEBUSB_LANDING_PAGE_IDX /* iLandingPage */
0x00, 0x00, 0x03, 0x06, /* dwWindowsVersion = 0x06030000 for Windows 8.1 Build */
LOBYTE(USBD_SIZ_MS_OS_DSCRPTR_SET), /* wMSOSDescriptorSetTotalLength */
HIBYTE(USBD_SIZ_MS_OS_DSCRPTR_SET), /* wMSOSDescriptorSetTotalLength */
USBD_VENDOR_CODE_MS, /* bMS_VendorCode */
0x00, /* bAltEnumCode = Does not support alternate enumeration */
};
_Static_assert(USB_SIZ_BOS_DESC == sizeof(USBD_BOSDesc));

static uint16_t USBD_BOSDesc_Len;

__ALIGN_BEGIN const uint8_t USBD_OSDescSet[USBD_SIZ_MS_OS_DSCRPTR_SET] __ALIGN_END =
__ALIGN_BEGIN const uint8_t USBD_OSDescSet[] __ALIGN_END =
{
0x0A, 0x00, /* wLength = 10 */
0x00, 0x00, /* wDescriptorType = MS_OS_20_SET_HEADER_DESCRIPTOR */
Expand Down Expand Up @@ -254,6 +245,7 @@ __ALIGN_BEGIN const uint8_t USBD_OSDescSet[USBD_SIZ_MS_OS_DSCRPTR_SET] __ALIGN_E
'\0', '\0',
'\0', '\0'
};
_Static_assert(USBD_SIZ_MS_OS_DSCRPTR_SET == sizeof(USBD_OSDescSet));

/* USB Standard Device Descriptor */
__ALIGN_BEGIN static const uint8_t USBD_LangIDDesc[USB_LEN_LANGID_STR_DESC] __ALIGN_END = {
Expand Down Expand Up @@ -314,38 +306,6 @@ static void Get_SerialNum(void) {
}
}

/**
* @brief Add the short BLE UUID to the buffer in little-endian format.
* @param dst The destination buffer
* @param dst The short BLE UUID to add
* @retval None
*/
static void add_ble_short_uuid_le(uint8_t *dst, uint16_t short_uuid) {
/* 32-bit */
dst[0] = LOBYTE(short_uuid);
dst[1] = HIBYTE(short_uuid);
dst[2] = 0x00;
dst[3] = 0x00;

/* 16-bit */
dst[4] = 0x00;
dst[5] = 0x00;

/* 16-bit */
dst[6] = 0x00;
dst[7] = 0x10;

/* 8-byte buffer */
dst[8] = 0x80;
dst[9] = 0x00;
dst[10] = 0x00;
dst[11] = 0x80;
dst[12] = 0x5F;
dst[13] = 0x9B;
dst[14] = 0x34;
dst[15] = 0xFB;
}

/**
* @brief Returns the device descriptor.
* @param speed: Current device speed
Expand Down Expand Up @@ -443,7 +403,7 @@ static uint8_t *USBD_Pybricks_BOSDescriptor(USBD_SpeedTypeDef speed, uint16_t *l
/* Prevent unused argument(s) compilation warning */
UNUSED(speed);

*length = USBD_BOSDesc_Len;
*length = USB_SIZ_BOS_DESC;
return (uint8_t *)USBD_BOSDesc;
}

Expand Down Expand Up @@ -471,80 +431,4 @@ void USBD_Pybricks_Desc_Init(void) {
USBD_DeviceDesc[11] = HIBYTE(PBDRV_CONFIG_USB_PID_1);
}
#endif

const char *str;
size_t len;

uint8_t *ptr = &USBD_BOSDesc[USB_SIZ_BOS_DESC_CONST];

/* Add device name */
str = pbdrv_bluetooth_get_hub_name();
len = MIN(strlen(str), USB_SIZ_HUB_NAME_MAX);

*ptr++ = USB_SIZ_PLATFORM_HDR + len; // bLength
*ptr++ = USB_DEVICE_CAPABITY_TYPE; // bDescriptorType
*ptr++ = USB_DEV_CAP_TYPE_PLATFORM; // bDevCapabilityType
*ptr++ = 0x00; // bReserved

// PlatformCapabilityUUID
add_ble_short_uuid_le(ptr, pbio_gatt_device_name_char_uuid);
ptr += USB_SIZ_UUID;

// CapabilityData: Device Name
memcpy(ptr, str, len);
ptr += len;

/* Add firmware version */
*ptr++ = USB_SIZ_PLATFORM_HDR + sizeof(firmware_version) - 1; // bLength
*ptr++ = USB_DEVICE_CAPABITY_TYPE; // bDescriptorType
*ptr++ = USB_DEV_CAP_TYPE_PLATFORM; // bDevCapabilityType
*ptr++ = 0x00; // bReserved

// PlatformCapabilityUUID
add_ble_short_uuid_le(ptr, pbio_gatt_firmware_version_char_uuid);
ptr += USB_SIZ_UUID;

// CapabilityData: Firmware Version
memcpy(ptr, firmware_version, sizeof(firmware_version) - 1);
ptr += sizeof(firmware_version) - 1;

/* Add software (protocol) version */
*ptr++ = USB_SIZ_PLATFORM_HDR + sizeof(software_version) - 1; // bLength
*ptr++ = USB_DEVICE_CAPABITY_TYPE; // bDescriptorType
*ptr++ = USB_DEV_CAP_TYPE_PLATFORM; // bDevCapabilityType
*ptr++ = 0x00; // bReserved

// PlatformCapabilityUUID
add_ble_short_uuid_le(ptr, pbio_gatt_software_version_char_uuid);
ptr += USB_SIZ_UUID;

// CapabilityData: Software Version
memcpy(ptr, software_version, sizeof(software_version) - 1);
ptr += sizeof(software_version) - 1;

/* Add hub capabilities */
*ptr++ = USB_SIZ_PLATFORM_HDR + PBIO_PYBRICKS_HUB_CAPABILITIES_VALUE_SIZE; // bLength
*ptr++ = USB_DEVICE_CAPABITY_TYPE; // bDescriptorType
*ptr++ = USB_DEV_CAP_TYPE_PLATFORM; // bDevCapabilityType
*ptr++ = 0x00; // bReserved

// PlatformCapabilityUUID
pbio_uuid128_le_copy(ptr, pbio_pybricks_hub_capabilities_char_uuid);
ptr += USB_SIZ_UUID;

// CapabilityData: Hub Capabilities
pbio_pybricks_hub_capabilities(ptr,
USBD_PYBRICKS_MAX_PACKET_SIZE - 1,
PBSYS_CONFIG_APP_FEATURE_FLAGS,
pbsys_storage_get_maximum_program_size(),
PBSYS_CONFIG_HMI_NUM_SLOTS);
ptr += PBIO_PYBRICKS_HUB_CAPABILITIES_VALUE_SIZE;

/* Update wTotalLength field in BOS Descriptor */
USBD_BOSDesc_Len = ptr - USBD_BOSDesc;
USBD_BOSDesc[2] = LOBYTE(USBD_BOSDesc_Len);
USBD_BOSDesc[3] = HIBYTE(USBD_BOSDesc_Len);

/* Update bNumDeviceCaps field in BOS Descriptor */
USBD_BOSDesc[4] += 4;
}
Loading