From 96c6b8cae3ad5037c41f4e029974fb17d727d012 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 1 Aug 2021 11:04:15 +1000 Subject: [PATCH] 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 --- ports/mimxrt/tusb_port.c | 10 +++++---- ports/nrf/drivers/usb/usb_descriptors.c | 10 +++++---- ports/samd/tusb_port.c | 10 +++++---- ports/stm32/Makefile | 2 +- ports/stm32/main.c | 6 +++--- ports/stm32/mpconfigboard_common.h | 27 +++++++++++++++++++++++++ ports/stm32/usb.c | 24 +++++++++++----------- ports/stm32/usb.h | 16 --------------- tools/insert-usb-ids.py | 7 ++++--- 9 files changed, 65 insertions(+), 47 deletions(-) diff --git a/ports/mimxrt/tusb_port.c b/ports/mimxrt/tusb_port.c index 2b1ccabbe7..128861a49a 100644 --- a/ports/mimxrt/tusb_port.c +++ b/ports/mimxrt/tusb_port.c @@ -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) @@ -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, diff --git a/ports/nrf/drivers/usb/usb_descriptors.c b/ports/nrf/drivers/usb/usb_descriptors.c index e9436ded54..b1edfcc18f 100644 --- a/ports/nrf/drivers/usb/usb_descriptors.c +++ b/ports/nrf/drivers/usb/usb_descriptors.c @@ -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) @@ -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, diff --git a/ports/samd/tusb_port.c b/ports/samd/tusb_port.c index e96f332464..2098334fba 100644 --- a/ports/samd/tusb_port.c +++ b/ports/samd/tusb_port.c @@ -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) @@ -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, diff --git a/ports/stm32/Makefile b/ports/stm32/Makefile index d78e2b4851..fd76f20a6d 100644 --- a/ports/stm32/Makefile +++ b/ports/stm32/Makefile @@ -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 diff --git a/ports/stm32/main.c b/ports/stm32/main.c index 3ff7077ac2..d55f1a2c30 100644 --- a/ports/stm32/main.c +++ b/ports/stm32/main.c @@ -582,13 +582,13 @@ soft_reset: // 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 diff --git a/ports/stm32/mpconfigboard_common.h b/ports/stm32/mpconfigboard_common.h index ce9dbdb0af..46456420da 100644 --- a/ports/stm32/mpconfigboard_common.h +++ b/ports/stm32/mpconfigboard_common.h @@ -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 diff --git a/ports/stm32/usb.c b/ports/stm32/usb.c index 838613f283..1ed45592e9 100644 --- a/ports/stm32/usb.c +++ b/ports/stm32/usb.c @@ -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)} }, @@ -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 { diff --git a/ports/stm32/usb.h b/ports/stm32/usb.h index 295038ebd4..3c382887ad 100644 --- a/ports/stm32/usb.h +++ b/ports/stm32/usb.h @@ -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, diff --git a/tools/insert-usb-ids.py b/tools/insert-usb-ids.py index b1d848ad4e..9b53f3f68a 100644 --- a/tools/insert-usb-ids.py +++ b/tools/insert-usb-ids.py @@ -8,6 +8,7 @@ import sys import re import string +config_prefix = "MICROPY_HW_USB_" needed_keys = ("USB_PID_CDC_MSC", "USB_PID_CDC_HID", "USB_PID_CDC", "USB_VID") @@ -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: