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>
This commit is contained in:
Damien George 2021-08-01 11:04:15 +10:00
parent 45f9a38451
commit 96c6b8cae3
9 changed files with 65 additions and 47 deletions

View File

@ -26,8 +26,10 @@
#include "tusb.h" #include "tusb.h"
#define USBD_VID (0xf055) #ifndef MICROPY_HW_USB_VID
#define USBD_PID (0x9802) #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_DESC_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN)
#define USBD_MAX_POWER_MA (250) #define USBD_MAX_POWER_MA (250)
@ -57,8 +59,8 @@ static const tusb_desc_device_t usbd_desc_device = {
.bDeviceSubClass = MISC_SUBCLASS_COMMON, .bDeviceSubClass = MISC_SUBCLASS_COMMON,
.bDeviceProtocol = MISC_PROTOCOL_IAD, .bDeviceProtocol = MISC_PROTOCOL_IAD,
.bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE, .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
.idVendor = USBD_VID, .idVendor = MICROPY_HW_USB_VID,
.idProduct = USBD_PID, .idProduct = MICROPY_HW_USB_PID,
.bcdDevice = 0x0100, .bcdDevice = 0x0100,
.iManufacturer = USBD_STR_MANUF, .iManufacturer = USBD_STR_MANUF,
.iProduct = USBD_STR_PRODUCT, .iProduct = USBD_STR_PRODUCT,

View File

@ -26,8 +26,10 @@
#include "tusb.h" #include "tusb.h"
#define USBD_VID (0xf055) #ifndef MICROPY_HW_USB_VID
#define USBD_PID (0x9802) #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_DESC_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN)
#define USBD_MAX_POWER_MA (250) #define USBD_MAX_POWER_MA (250)
@ -57,8 +59,8 @@ static const tusb_desc_device_t usbd_desc_device = {
.bDeviceSubClass = MISC_SUBCLASS_COMMON, .bDeviceSubClass = MISC_SUBCLASS_COMMON,
.bDeviceProtocol = MISC_PROTOCOL_IAD, .bDeviceProtocol = MISC_PROTOCOL_IAD,
.bMaxPacketSize0 = CFG_TUD_ENDOINT0_SIZE, .bMaxPacketSize0 = CFG_TUD_ENDOINT0_SIZE,
.idVendor = USBD_VID, .idVendor = MICROPY_HW_USB_VID,
.idProduct = USBD_PID, .idProduct = MICROPY_HW_USB_PID,
.bcdDevice = 0x0100, .bcdDevice = 0x0100,
.iManufacturer = USBD_STR_MANUF, .iManufacturer = USBD_STR_MANUF,
.iProduct = USBD_STR_PRODUCT, .iProduct = USBD_STR_PRODUCT,

View File

@ -27,8 +27,10 @@
#include "samd_soc.h" #include "samd_soc.h"
#include "tusb.h" #include "tusb.h"
#define USBD_VID (0xf055) #ifndef MICROPY_HW_USB_VID
#define USBD_PID (0x9802) #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_DESC_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN)
#define USBD_MAX_POWER_MA (250) #define USBD_MAX_POWER_MA (250)
@ -58,8 +60,8 @@ static const tusb_desc_device_t usbd_desc_device = {
.bDeviceSubClass = MISC_SUBCLASS_COMMON, .bDeviceSubClass = MISC_SUBCLASS_COMMON,
.bDeviceProtocol = MISC_PROTOCOL_IAD, .bDeviceProtocol = MISC_PROTOCOL_IAD,
.bMaxPacketSize0 = CFG_TUD_ENDOINT0_SIZE, .bMaxPacketSize0 = CFG_TUD_ENDOINT0_SIZE,
.idVendor = USBD_VID, .idVendor = MICROPY_HW_USB_VID,
.idProduct = USBD_PID, .idProduct = MICROPY_HW_USB_PID,
.bcdDevice = 0x0100, .bcdDevice = 0x0100,
.iManufacturer = USBD_STR_MANUF, .iManufacturer = USBD_STR_MANUF,
.iProduct = USBD_STR_PRODUCT, .iProduct = USBD_STR_PRODUCT,

View File

@ -725,7 +725,7 @@ GEN_PINS_AF_PY = $(BUILD)/pins_af.py
INSERT_USB_IDS = $(TOP)/tools/insert-usb-ids.py INSERT_USB_IDS = $(TOP)/tools/insert-usb-ids.py
FILE2H = $(TOP)/tools/file2h.py FILE2H = $(TOP)/tools/file2h.py
USB_IDS_FILE = usb.h USB_IDS_FILE = mpconfigboard_common.h
CDCINF_TEMPLATE = pybcdc.inf_template CDCINF_TEMPLATE = pybcdc.inf_template
GEN_CDCINF_FILE = $(HEADER_BUILD)/pybcdc.inf GEN_CDCINF_FILE = $(HEADER_BUILD)/pybcdc.inf
GEN_CDCINF_HEADER = $(HEADER_BUILD)/pybcdc_inf.h GEN_CDCINF_HEADER = $(HEADER_BUILD)/pybcdc_inf.h

View File

@ -582,13 +582,13 @@ soft_reset:
// init USB device to default setting if it was not already configured // init USB device to default setting if it was not already configured
if (!(pyb_usb_flags & PYB_USB_FLAG_USB_MODE_CALLED)) { if (!(pyb_usb_flags & PYB_USB_FLAG_USB_MODE_CALLED)) {
#if MICROPY_HW_USB_MSC #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; const uint8_t mode = USBD_MODE_CDC_MSC;
#else #else
const uint16_t pid = USBD_PID_CDC; const uint16_t pid = MICROPY_HW_USB_PID_CDC;
const uint8_t mode = USBD_MODE_CDC; const uint8_t mode = USBD_MODE_CDC;
#endif #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 #endif

View File

@ -177,6 +177,33 @@
#define MICROPY_HW_UART_IS_RESERVED(uart_id) (false) #define MICROPY_HW_UART_IS_RESERVED(uart_id) (false)
#endif #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 // General configuration

View File

@ -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[] = { static const mp_arg_t allowed_args[] = {
{ MP_QSTR_mode, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, { 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_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} }, { MP_QSTR_pid, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
#if MICROPY_HW_USB_MSC #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)} }, { 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; uint8_t mode;
if (strcmp(mode_str, "CDC+MSC") == 0 || strcmp(mode_str, "VCP+MSC") == 0) { if (strcmp(mode_str, "CDC+MSC") == 0 || strcmp(mode_str, "VCP+MSC") == 0) {
if (pid == -1) { if (pid == -1) {
pid = USBD_PID_CDC_MSC; pid = MICROPY_HW_USB_PID_CDC_MSC;
} }
mode = USBD_MODE_CDC_MSC; mode = USBD_MODE_CDC_MSC;
} else if (strcmp(mode_str, "VCP+MSC+HID") == 0) { } else if (strcmp(mode_str, "VCP+MSC+HID") == 0) {
if (pid == -1) { if (pid == -1) {
pid = USBD_PID_CDC_MSC_HID; pid = MICROPY_HW_USB_PID_CDC_MSC_HID;
} }
mode = USBD_MODE_CDC_MSC_HID; mode = USBD_MODE_CDC_MSC_HID;
#if MICROPY_HW_USB_CDC_NUM >= 2 #if MICROPY_HW_USB_CDC_NUM >= 2
} else if (strcmp(mode_str, "VCP+VCP") == 0) { } else if (strcmp(mode_str, "VCP+VCP") == 0) {
if (pid == -1) { if (pid == -1) {
pid = USBD_PID_CDC2; pid = MICROPY_HW_USB_PID_CDC2;
} }
mode = USBD_MODE_CDC2; mode = USBD_MODE_CDC2;
} else if (strcmp(mode_str, "VCP+VCP+MSC") == 0) { } else if (strcmp(mode_str, "VCP+VCP+MSC") == 0) {
if (pid == -1) { if (pid == -1) {
pid = USBD_PID_CDC2_MSC; pid = MICROPY_HW_USB_PID_CDC2_MSC;
} }
mode = USBD_MODE_CDC2_MSC; mode = USBD_MODE_CDC2_MSC;
} else if (strcmp(mode_str, "2xVCP+MSC+HID") == 0) { } else if (strcmp(mode_str, "2xVCP+MSC+HID") == 0) {
if (pid == -1) { if (pid == -1) {
pid = USBD_PID_CDC2_MSC_HID; pid = MICROPY_HW_USB_PID_CDC2_MSC_HID;
} }
mode = USBD_MODE_CDC2_MSC_HID; mode = USBD_MODE_CDC2_MSC_HID;
#endif #endif
#if MICROPY_HW_USB_CDC_NUM >= 3 #if MICROPY_HW_USB_CDC_NUM >= 3
} else if (strcmp(mode_str, "3xVCP") == 0) { } else if (strcmp(mode_str, "3xVCP") == 0) {
if (pid == -1) { if (pid == -1) {
pid = USBD_PID_CDC3; pid = MICROPY_HW_USB_PID_CDC3;
} }
mode = USBD_MODE_CDC3; mode = USBD_MODE_CDC3;
} else if (strcmp(mode_str, "3xVCP+MSC") == 0) { } else if (strcmp(mode_str, "3xVCP+MSC") == 0) {
if (pid == -1) { if (pid == -1) {
pid = USBD_PID_CDC3_MSC; pid = MICROPY_HW_USB_PID_CDC3_MSC;
} }
mode = USBD_MODE_CDC3_MSC; mode = USBD_MODE_CDC3_MSC;
} else if (strcmp(mode_str, "3xVCP+MSC+HID") == 0) { } else if (strcmp(mode_str, "3xVCP+MSC+HID") == 0) {
if (pid == -1) { if (pid == -1) {
pid = USBD_PID_CDC3_MSC_HID; pid = MICROPY_HW_USB_PID_CDC3_MSC_HID;
} }
mode = USBD_MODE_CDC3_MSC_HID; mode = USBD_MODE_CDC3_MSC_HID;
#endif #endif
} else if (strcmp(mode_str, "CDC+HID") == 0 || strcmp(mode_str, "VCP+HID") == 0) { } else if (strcmp(mode_str, "CDC+HID") == 0 || strcmp(mode_str, "VCP+HID") == 0) {
if (pid == -1) { if (pid == -1) {
pid = USBD_PID_CDC_HID; pid = MICROPY_HW_USB_PID_CDC_HID;
} }
mode = USBD_MODE_CDC_HID; mode = USBD_MODE_CDC_HID;
} else if (strcmp(mode_str, "CDC") == 0 || strcmp(mode_str, "VCP") == 0) { } else if (strcmp(mode_str, "CDC") == 0 || strcmp(mode_str, "VCP") == 0) {
if (pid == -1) { if (pid == -1) {
pid = USBD_PID_CDC; pid = MICROPY_HW_USB_PID_CDC;
} }
mode = USBD_MODE_CDC; mode = USBD_MODE_CDC;
} else if (strcmp(mode_str, "MSC") == 0) { } else if (strcmp(mode_str, "MSC") == 0) {
if (pid == -1) { if (pid == -1) {
pid = USBD_PID_MSC; pid = MICROPY_HW_USB_PID_MSC;
} }
mode = USBD_MODE_MSC; mode = USBD_MODE_MSC;
} else { } else {

View File

@ -30,22 +30,6 @@
#define PYB_USB_FLAG_USB_MODE_CALLED (0x0002) #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 { typedef enum {
PYB_USB_STORAGE_MEDIUM_NONE = 0, PYB_USB_STORAGE_MEDIUM_NONE = 0,
PYB_USB_STORAGE_MEDIUM_FLASH, PYB_USB_STORAGE_MEDIUM_FLASH,

View File

@ -8,6 +8,7 @@ import sys
import re import re
import string import string
config_prefix = "MICROPY_HW_USB_"
needed_keys = ("USB_PID_CDC_MSC", "USB_PID_CDC_HID", "USB_PID_CDC", "USB_VID") 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(): for line in open(filename).readlines():
line = line.rstrip("\r\n") line = line.rstrip("\r\n")
match = re.match("^#define\s+(\w+)\s+\(0x([0-9A-Fa-f]+)\)$", line) match = re.match("^#define\s+(\w+)\s+\(0x([0-9A-Fa-f]+)\)$", line)
if match and match.group(1).startswith("USBD_"): if match and match.group(1).startswith(config_prefix):
key = match.group(1).replace("USBD", "USB") key = match.group(1).replace(config_prefix, "USB_")
val = match.group(2) val = match.group(2)
print("key =", key, "val =", val) # print("key =", key, "val =", val)
if key in needed_keys: if key in needed_keys:
rv[key] = val rv[key] = val
for k in needed_keys: for k in needed_keys: