Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
ports: Rename USBD_VID/PID config macros to MICROPY_HW_USB_VID/PID.
For consistency with other board-level config macros that begin with
MICROPY_HW_USB.

Also allow boards in the mimxrt, nrf and samd ports to configure these
values.

Signed-off-by: Damien George <damien@micropython.org>
  • Loading branch information
dpgeorge committed Aug 7, 2021
1 parent 45f9a38 commit 96c6b8c
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 47 deletions.
10 changes: 6 additions & 4 deletions ports/mimxrt/tusb_port.c
Expand Up @@ -26,8 +26,10 @@

#include "tusb.h"

#define USBD_VID (0xf055)
#define USBD_PID (0x9802)
#ifndef MICROPY_HW_USB_VID
#define MICROPY_HW_USB_VID (0xf055)
#define MICROPY_HW_USB_PID (0x9802)
#endif

#define USBD_DESC_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN)
#define USBD_MAX_POWER_MA (250)
Expand Down Expand Up @@ -57,8 +59,8 @@ static const tusb_desc_device_t usbd_desc_device = {
.bDeviceSubClass = MISC_SUBCLASS_COMMON,
.bDeviceProtocol = MISC_PROTOCOL_IAD,
.bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
.idVendor = USBD_VID,
.idProduct = USBD_PID,
.idVendor = MICROPY_HW_USB_VID,
.idProduct = MICROPY_HW_USB_PID,
.bcdDevice = 0x0100,
.iManufacturer = USBD_STR_MANUF,
.iProduct = USBD_STR_PRODUCT,
Expand Down
10 changes: 6 additions & 4 deletions ports/nrf/drivers/usb/usb_descriptors.c
Expand Up @@ -26,8 +26,10 @@

#include "tusb.h"

#define USBD_VID (0xf055)
#define USBD_PID (0x9802)
#ifndef MICROPY_HW_USB_VID
#define MICROPY_HW_USB_VID (0xf055)
#define MICROPY_HW_USB_PID (0x9802)
#endif

#define USBD_DESC_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN)
#define USBD_MAX_POWER_MA (250)
Expand Down Expand Up @@ -57,8 +59,8 @@ static const tusb_desc_device_t usbd_desc_device = {
.bDeviceSubClass = MISC_SUBCLASS_COMMON,
.bDeviceProtocol = MISC_PROTOCOL_IAD,
.bMaxPacketSize0 = CFG_TUD_ENDOINT0_SIZE,
.idVendor = USBD_VID,
.idProduct = USBD_PID,
.idVendor = MICROPY_HW_USB_VID,
.idProduct = MICROPY_HW_USB_PID,
.bcdDevice = 0x0100,
.iManufacturer = USBD_STR_MANUF,
.iProduct = USBD_STR_PRODUCT,
Expand Down
10 changes: 6 additions & 4 deletions ports/samd/tusb_port.c
Expand Up @@ -27,8 +27,10 @@
#include "samd_soc.h"
#include "tusb.h"

#define USBD_VID (0xf055)
#define USBD_PID (0x9802)
#ifndef MICROPY_HW_USB_VID
#define MICROPY_HW_USB_VID (0xf055)
#define MICROPY_HW_USB_PID (0x9802)
#endif

#define USBD_DESC_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN)
#define USBD_MAX_POWER_MA (250)
Expand Down Expand Up @@ -58,8 +60,8 @@ static const tusb_desc_device_t usbd_desc_device = {
.bDeviceSubClass = MISC_SUBCLASS_COMMON,
.bDeviceProtocol = MISC_PROTOCOL_IAD,
.bMaxPacketSize0 = CFG_TUD_ENDOINT0_SIZE,
.idVendor = USBD_VID,
.idProduct = USBD_PID,
.idVendor = MICROPY_HW_USB_VID,
.idProduct = MICROPY_HW_USB_PID,
.bcdDevice = 0x0100,
.iManufacturer = USBD_STR_MANUF,
.iProduct = USBD_STR_PRODUCT,
Expand Down
2 changes: 1 addition & 1 deletion ports/stm32/Makefile
Expand Up @@ -725,7 +725,7 @@ GEN_PINS_AF_PY = $(BUILD)/pins_af.py
INSERT_USB_IDS = $(TOP)/tools/insert-usb-ids.py
FILE2H = $(TOP)/tools/file2h.py

USB_IDS_FILE = usb.h
USB_IDS_FILE = mpconfigboard_common.h
CDCINF_TEMPLATE = pybcdc.inf_template
GEN_CDCINF_FILE = $(HEADER_BUILD)/pybcdc.inf
GEN_CDCINF_HEADER = $(HEADER_BUILD)/pybcdc_inf.h
Expand Down
6 changes: 3 additions & 3 deletions ports/stm32/main.c
Expand Up @@ -582,13 +582,13 @@ void stm32_main(uint32_t reset_mode) {
// init USB device to default setting if it was not already configured
if (!(pyb_usb_flags & PYB_USB_FLAG_USB_MODE_CALLED)) {
#if MICROPY_HW_USB_MSC
const uint16_t pid = USBD_PID_CDC_MSC;
const uint16_t pid = MICROPY_HW_USB_PID_CDC_MSC;
const uint8_t mode = USBD_MODE_CDC_MSC;
#else
const uint16_t pid = USBD_PID_CDC;
const uint16_t pid = MICROPY_HW_USB_PID_CDC;
const uint8_t mode = USBD_MODE_CDC;
#endif
pyb_usb_dev_init(pyb_usb_dev_detect(), USBD_VID, pid, mode, 0, NULL, NULL);
pyb_usb_dev_init(pyb_usb_dev_detect(), MICROPY_HW_USB_VID, pid, mode, 0, NULL, NULL);
}
#endif

Expand Down
27 changes: 27 additions & 0 deletions ports/stm32/mpconfigboard_common.h
Expand Up @@ -177,6 +177,33 @@
#define MICROPY_HW_UART_IS_RESERVED(uart_id) (false)
#endif

/*****************************************************************************/
// USB configuration

// The USBD_xxx VID/PID macros have been renamed to MICROPY_HW_USB_xxx.
#ifdef USBD_VID
#error "Old USBD_VID configuration option used"
#endif

// Default VID and PID values to use for the USB device. If MICROPY_HW_USB_VID
// is defined by a board then all needed PID options must also be defined. The
// VID and PID can also be set dynamically in pyb.usb_mode().
// Windows needs a different PID to distinguish different device configurations.
#ifndef MICROPY_HW_USB_VID
#define MICROPY_HW_USB_VID (0xf055)
#define MICROPY_HW_USB_PID_CDC_MSC (0x9800)
#define MICROPY_HW_USB_PID_CDC_HID (0x9801)
#define MICROPY_HW_USB_PID_CDC (0x9802)
#define MICROPY_HW_USB_PID_MSC (0x9803)
#define MICROPY_HW_USB_PID_CDC2_MSC (0x9804)
#define MICROPY_HW_USB_PID_CDC2 (0x9805)
#define MICROPY_HW_USB_PID_CDC3 (0x9806)
#define MICROPY_HW_USB_PID_CDC3_MSC (0x9807)
#define MICROPY_HW_USB_PID_CDC_MSC_HID (0x9808)
#define MICROPY_HW_USB_PID_CDC2_MSC_HID (0x9809)
#define MICROPY_HW_USB_PID_CDC3_MSC_HID (0x980a)
#endif

/*****************************************************************************/
// General configuration

Expand Down
24 changes: 12 additions & 12 deletions ports/stm32/usb.c
Expand Up @@ -412,7 +412,7 @@ STATIC mp_obj_t pyb_usb_mode(size_t n_args, const mp_obj_t *pos_args, mp_map_t *
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_mode, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
{ MP_QSTR_port, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
{ MP_QSTR_vid, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = USBD_VID} },
{ MP_QSTR_vid, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = MICROPY_HW_USB_VID} },
{ MP_QSTR_pid, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
#if MICROPY_HW_USB_MSC
{ MP_QSTR_msc, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_PTR(&mp_const_empty_tuple_obj)} },
Expand Down Expand Up @@ -489,61 +489,61 @@ STATIC mp_obj_t pyb_usb_mode(size_t n_args, const mp_obj_t *pos_args, mp_map_t *
uint8_t mode;
if (strcmp(mode_str, "CDC+MSC") == 0 || strcmp(mode_str, "VCP+MSC") == 0) {
if (pid == -1) {
pid = USBD_PID_CDC_MSC;
pid = MICROPY_HW_USB_PID_CDC_MSC;
}
mode = USBD_MODE_CDC_MSC;
} else if (strcmp(mode_str, "VCP+MSC+HID") == 0) {
if (pid == -1) {
pid = USBD_PID_CDC_MSC_HID;
pid = MICROPY_HW_USB_PID_CDC_MSC_HID;
}
mode = USBD_MODE_CDC_MSC_HID;
#if MICROPY_HW_USB_CDC_NUM >= 2
} else if (strcmp(mode_str, "VCP+VCP") == 0) {
if (pid == -1) {
pid = USBD_PID_CDC2;
pid = MICROPY_HW_USB_PID_CDC2;
}
mode = USBD_MODE_CDC2;
} else if (strcmp(mode_str, "VCP+VCP+MSC") == 0) {
if (pid == -1) {
pid = USBD_PID_CDC2_MSC;
pid = MICROPY_HW_USB_PID_CDC2_MSC;
}
mode = USBD_MODE_CDC2_MSC;
} else if (strcmp(mode_str, "2xVCP+MSC+HID") == 0) {
if (pid == -1) {
pid = USBD_PID_CDC2_MSC_HID;
pid = MICROPY_HW_USB_PID_CDC2_MSC_HID;
}
mode = USBD_MODE_CDC2_MSC_HID;
#endif
#if MICROPY_HW_USB_CDC_NUM >= 3
} else if (strcmp(mode_str, "3xVCP") == 0) {
if (pid == -1) {
pid = USBD_PID_CDC3;
pid = MICROPY_HW_USB_PID_CDC3;
}
mode = USBD_MODE_CDC3;
} else if (strcmp(mode_str, "3xVCP+MSC") == 0) {
if (pid == -1) {
pid = USBD_PID_CDC3_MSC;
pid = MICROPY_HW_USB_PID_CDC3_MSC;
}
mode = USBD_MODE_CDC3_MSC;
} else if (strcmp(mode_str, "3xVCP+MSC+HID") == 0) {
if (pid == -1) {
pid = USBD_PID_CDC3_MSC_HID;
pid = MICROPY_HW_USB_PID_CDC3_MSC_HID;
}
mode = USBD_MODE_CDC3_MSC_HID;
#endif
} else if (strcmp(mode_str, "CDC+HID") == 0 || strcmp(mode_str, "VCP+HID") == 0) {
if (pid == -1) {
pid = USBD_PID_CDC_HID;
pid = MICROPY_HW_USB_PID_CDC_HID;
}
mode = USBD_MODE_CDC_HID;
} else if (strcmp(mode_str, "CDC") == 0 || strcmp(mode_str, "VCP") == 0) {
if (pid == -1) {
pid = USBD_PID_CDC;
pid = MICROPY_HW_USB_PID_CDC;
}
mode = USBD_MODE_CDC;
} else if (strcmp(mode_str, "MSC") == 0) {
if (pid == -1) {
pid = USBD_PID_MSC;
pid = MICROPY_HW_USB_PID_MSC;
}
mode = USBD_MODE_MSC;
} else {
Expand Down
16 changes: 0 additions & 16 deletions ports/stm32/usb.h
Expand Up @@ -30,22 +30,6 @@

#define PYB_USB_FLAG_USB_MODE_CALLED (0x0002)

#ifndef USBD_VID
// Windows needs a different PID to distinguish different device configurations
#define USBD_VID (0xf055)
#define USBD_PID_CDC_MSC (0x9800)
#define USBD_PID_CDC_HID (0x9801)
#define USBD_PID_CDC (0x9802)
#define USBD_PID_MSC (0x9803)
#define USBD_PID_CDC2_MSC (0x9804)
#define USBD_PID_CDC2 (0x9805)
#define USBD_PID_CDC3 (0x9806)
#define USBD_PID_CDC3_MSC (0x9807)
#define USBD_PID_CDC_MSC_HID (0x9808)
#define USBD_PID_CDC2_MSC_HID (0x9809)
#define USBD_PID_CDC3_MSC_HID (0x980a)
#endif

typedef enum {
PYB_USB_STORAGE_MEDIUM_NONE = 0,
PYB_USB_STORAGE_MEDIUM_FLASH,
Expand Down
7 changes: 4 additions & 3 deletions tools/insert-usb-ids.py
Expand Up @@ -8,6 +8,7 @@
import re
import string

config_prefix = "MICROPY_HW_USB_"
needed_keys = ("USB_PID_CDC_MSC", "USB_PID_CDC_HID", "USB_PID_CDC", "USB_VID")


Expand All @@ -16,10 +17,10 @@ def parse_usb_ids(filename):
for line in open(filename).readlines():
line = line.rstrip("\r\n")
match = re.match("^#define\s+(\w+)\s+\(0x([0-9A-Fa-f]+)\)$", line)
if match and match.group(1).startswith("USBD_"):
key = match.group(1).replace("USBD", "USB")
if match and match.group(1).startswith(config_prefix):
key = match.group(1).replace(config_prefix, "USB_")
val = match.group(2)
print("key =", key, "val =", val)
# print("key =", key, "val =", val)
if key in needed_keys:
rv[key] = val
for k in needed_keys:
Expand Down

0 comments on commit 96c6b8c

Please sign in to comment.