shared/tinyusb: Further refactor static USB device implementation.

App the mp_ prefix to usbd_ symbols and files which are defined here and
not in TinyUSB.

rp2 only for now. This includes some groundwork for dynamic USB devices
(defined in Python).

This work was funded through GitHub Sponsors.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
This commit is contained in:
Angus Gratton 2022-10-04 10:25:56 +11:00 committed by Damien George
parent eed4eb2645
commit 2e6e53057b
13 changed files with 258 additions and 192 deletions

View File

@ -242,7 +242,7 @@ SRC_SHARED_C += $(addprefix shared/,\
runtime/pyexec.c \
runtime/sys_stdio_mphal.c \
runtime/interrupt_char.c \
runtime/tinyusb_helpers.c \
tinyusb/mp_cdc_common.c \
timeutils/timeutils.c \
)

View File

@ -94,10 +94,10 @@ set(MICROPY_SOURCE_LIB
${MICROPY_DIR}/shared/runtime/pyexec.c
${MICROPY_DIR}/shared/runtime/stdout_helpers.c
${MICROPY_DIR}/shared/runtime/sys_stdio_mphal.c
${MICROPY_DIR}/shared/runtime/tinyusb_helpers.c
${MICROPY_DIR}/shared/timeutils/timeutils.c
${MICROPY_DIR}/shared/tinyusb/usbd.c
${MICROPY_DIR}/shared/tinyusb/usbd_descriptor.c
${MICROPY_DIR}/shared/tinyusb/mp_cdc_common.c
${MICROPY_DIR}/shared/tinyusb/mp_usbd.c
${MICROPY_DIR}/shared/tinyusb/mp_usbd_descriptor.c
)
set(MICROPY_SOURCE_DRIVERS

View File

@ -43,7 +43,7 @@
#include "modrp2.h"
#include "mpbthciport.h"
#include "genhdr/mpversion.h"
#include "usbd.h"
#include "mp_usbd.h"
#include "pico/stdlib.h"
#include "pico/binary_info.h"
@ -88,7 +88,6 @@ int main(int argc, char **argv) {
#if MICROPY_HW_ENABLE_USBDEV
bi_decl(bi_program_feature("USB REPL"))
tusb_init();
usbd_reset_all(); // run now just in case usb initialization occurs early
#endif
#if MICROPY_PY_THREAD
@ -161,9 +160,6 @@ int main(int argc, char **argv) {
machine_pin_init();
rp2_pio_init();
machine_i2s_init0();
#if MICROPY_HW_ENABLE_USBDEV
usbd_reset_all();
#endif
#if MICROPY_PY_BLUETOOTH
mp_bluetooth_hci_init();

View File

@ -217,7 +217,7 @@ extern void mp_thread_end_atomic_section(uint32_t);
#define MICROPY_PY_LWIP_EXIT lwip_lock_release();
#if MICROPY_HW_ENABLE_USBDEV
#define MICROPY_HW_USBDEV_TASK_HOOK extern void tud_task_ext(uint32_t, bool); tud_task_ext(0, false);
#define MICROPY_HW_USBDEV_TASK_HOOK extern void usbd_task(void); usbd_task();
#define MICROPY_VM_HOOK_COUNT (10)
#define MICROPY_VM_HOOK_INIT static uint vm_hook_divisor = MICROPY_VM_HOOK_COUNT;
#define MICROPY_VM_HOOK_POLL if (get_core_num() == 0 && --vm_hook_divisor == 0) { \

View File

@ -3,7 +3,7 @@
*
* The MIT License (MIT)
*
* Copyright (c) 2022 Blake W. Felt
* Copyright (c) 2022 Blake W. Felt & Angus Gratton
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@ -24,16 +24,21 @@
* THE SOFTWARE.
*/
#include "usbd.h"
#include "mp_usbd.h"
#include "string.h"
#include "tusb.h"
#include "pico/unique_id.h"
int usbd_serialnumber(uint8_t *buf) {
void mp_usbd_port_get_serial_number(char *serial_buf) {
pico_unique_board_id_t id;
const int len = 8;
pico_get_unique_board_id(&id);
memcpy(buf, id.id, len);
return len;
// convert to hex
int hexlen = sizeof(id.id) * 2;
MP_STATIC_ASSERT(hexlen <= USBD_DESC_STR_MAX);
for (int i = 0; i < hexlen; i += 2) {
static const char *hexdig = "0123456789abcdef";
serial_buf[i] = hexdig[id.id[i / 2] >> 4];
serial_buf[i + 1] = hexdig[id.id[i / 2] & 0x0f];
}
serial_buf[hexlen] = 0;
}

View File

@ -3,7 +3,7 @@
*
* The MIT License (MIT)
*
* Copyright (c) 2022 Blake W. Felt
* Copyright (c) 2022 Blake W. Felt & Angus Gratton
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@ -24,9 +24,14 @@
* THE SOFTWARE.
*/
#include "py/runtime.h"
#include "usbd.h"
#include <stdlib.h>
void usbd_reset_all(void) {
usbd_reset_descriptor();
#ifndef NO_QSTR
#include "tusb.h" // TinyUSB is not avaiable when running the string preprocessor
#include "device/usbd.h"
#include "device/usbd_pvt.h"
#endif
void usbd_task(void) {
tud_task_ext(0, false);
}

View File

@ -3,7 +3,7 @@
*
* The MIT License (MIT)
*
* Copyright (c) 2022 Blake W. Felt
* Copyright (c) 2022 Blake W. Felt & Angus Gratton
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@ -24,18 +24,16 @@
* THE SOFTWARE.
*/
#ifndef MICROPY_INCLUDED_SHARED_TINYUSB_USBD_H
#define MICROPY_INCLUDED_SHARED_TINYUSB_USBD_H
#ifndef MICROPY_INCLUDED_SHARED_TINYUSB_MP_USBD_H
#define MICROPY_INCLUDED_SHARED_TINYUSB_MP_USBD_H
#include "py/obj.h"
// defined externally (needed per port)
// Call instead of tud_task()
void mp_usbd_task(void);
int usbd_serialnumber(uint8_t *buf);
// external use
void usbd_reset_all(void);
void usbd_reset_descriptor(void);
// Function to be implemented in port code.
// Can write a string up to USBD_DESC_STR_MAX characters long, plus terminating byte.
extern void mp_usbd_port_get_serial_number(char *buf);
#endif // MICROPY_INCLUDED_SHARED_TINYUSB_USBD_H

View File

@ -0,0 +1,124 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2019 Damien P. George
* Copyright (c) 2022 Blake W. Felt & Angus Gratton
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "mpconfigport.h"
#include "tusb.h"
#include "mp_usbd.h"
#include "mp_usbd_internal.h"
#define USBD_CDC_CMD_MAX_SIZE (8)
#define USBD_CDC_IN_OUT_MAX_SIZE (64)
const tusb_desc_device_t mp_usbd_desc_device_static = {
.bLength = sizeof(tusb_desc_device_t),
.bDescriptorType = TUSB_DESC_DEVICE,
.bcdUSB = 0x0200,
.bDeviceClass = TUSB_CLASS_MISC,
.bDeviceSubClass = MISC_SUBCLASS_COMMON,
.bDeviceProtocol = MISC_PROTOCOL_IAD,
.bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
.idVendor = MICROPY_HW_USB_VID,
.idProduct = MICROPY_HW_USB_PID,
.bcdDevice = 0x0100,
.iManufacturer = USBD_STR_MANUF,
.iProduct = USBD_STR_PRODUCT,
.iSerialNumber = USBD_STR_SERIAL,
.bNumConfigurations = 1,
};
const uint8_t mp_usbd_desc_cfg_static[USBD_STATIC_DESC_LEN] = {
TUD_CONFIG_DESCRIPTOR(1, USBD_ITF_STATIC_MAX, USBD_STR_0, USBD_STATIC_DESC_LEN,
0, USBD_MAX_POWER_MA),
TUD_CDC_DESCRIPTOR(USBD_ITF_CDC, USBD_STR_CDC, USBD_CDC_EP_CMD,
USBD_CDC_CMD_MAX_SIZE, USBD_CDC_EP_OUT, USBD_CDC_EP_IN, USBD_CDC_IN_OUT_MAX_SIZE),
#if CFG_TUD_MSC
TUD_MSC_DESCRIPTOR(USBD_ITF_MSC, 5, EPNUM_MSC_OUT, EPNUM_MSC_IN, 64),
#endif
};
const uint16_t *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
char serial_buf[USBD_DESC_STR_MAX + 1]; // Includes terminating NUL byte
static uint16_t desc_wstr[USBD_DESC_STR_MAX + 1]; // Includes prefix uint16_t
const char *desc_str;
uint16_t desc_len;
switch (index) {
case 0:
desc_wstr[1] = 0x0409; // supported language is English
desc_len = 4;
break;
case USBD_STR_SERIAL:
// TODO: make a port-specific serial number callback
mp_usbd_port_get_serial_number(serial_buf);
desc_str = serial_buf;
break;
case USBD_STR_MANUF:
desc_str = MICROPY_HW_USB_MANUFACTURER_STRING;
break;
case USBD_STR_PRODUCT:
desc_str = MICROPY_HW_USB_PRODUCT_FS_STRING;
break;
case USBD_STR_CDC:
desc_str = MICROPY_HW_USB_CDC_INTERFACE_STRING;
break;
#if CFG_TUD_MSC
case USBD_STR_MSC:
desc_str = MICROPY_HW_USB_MSC_INTERFACE_STRING;
break;
#endif
default:
desc_str = NULL;
}
if (index != 0) {
if (desc_str == NULL) {
return NULL; // Will STALL the endpoint
}
// Convert from narrow string to wide string
desc_len = 2;
for (int i = 0; i < USBD_DESC_STR_MAX && desc_str[i] != 0; i++) {
desc_wstr[1 + i] = desc_str[i];
desc_len += 2;
}
}
// first byte is length (including header), second byte is string type
desc_wstr[0] = (TUSB_DESC_STRING << 8) | desc_len;
return desc_wstr;
}
const uint8_t *tud_descriptor_device_cb(void) {
return (const void *)&mp_usbd_desc_device_static;
}
const uint8_t *tud_descriptor_configuration_cb(uint8_t index) {
(void)index;
return mp_usbd_desc_cfg_static;
}

View File

@ -0,0 +1,34 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2022 Angus Gratton
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef MICROPY_INCLUDED_SHARED_TINYUSB_MP_USBD_INTERNAL_H
#define MICROPY_INCLUDED_SHARED_TINYUSB_MP_USBD_INTERNAL_H
#include "tusb.h"
// Static USB device descriptor values
extern const tusb_desc_device_t mp_usbd_desc_device_static;
extern const uint8_t mp_usbd_desc_cfg_static[USBD_STATIC_DESC_LEN];
#endif // MICROPY_INCLUDED_SHARED_TINYUSB_MP_USBD_INTERNAL_H

View File

@ -2,6 +2,7 @@
* The MIT License (MIT)
*
* Copyright (c) 2020-2021 Damien P. George
* Copyright (c) 2022 Angus Gratton
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@ -29,6 +30,20 @@
#include <py/mpconfig.h>
#include "mpconfigport.h"
#if MICROPY_HW_ENABLE_USBDEV
#ifndef MICROPY_HW_USB_MANUFACTURER_STRING
#define MICROPY_HW_USB_MANUFACTURER_STRING "MicroPython"
#endif
#ifndef MICROPY_HW_USB_PRODUCT_FS_STRING
#define MICROPY_HW_USB_PRODUCT_FS_STRING "Board in FS mode"
#endif
#ifndef MICROPY_HW_USB_CDC_INTERFACE_STRING
#define MICROPY_HW_USB_CDC_INTERFACE_STRING "Board CDC"
#endif
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE)
#define CFG_TUD_CDC (1)
@ -36,11 +51,56 @@
#define CFG_TUD_CDC_RX_BUFSIZE (256)
#define CFG_TUD_CDC_TX_BUFSIZE (256)
#if MICROPY_HW_USB_MSC
// Board and hardware specific configuration
// MSC Configuration
#if CFG_TUD_MSC
#ifndef MICROPY_HW_USB_MSC_INTERFACE_STRING
#define MICROPY_HW_USB_MSC_INTERFACE_STRING "Board MSC"
#endif
#define CFG_TUD_MSC (1)
// Set MSC EP buffer size to FatFS block size to avoid partial read/writes (offset arg).
#define CFG_TUD_MSC_BUFSIZE (MICROPY_FATFS_MAX_SS)
#endif
// Define static descriptor size and interface count based on the above config
#if CFG_TUD_MSC
#define USBD_STATIC_DESC_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN + TUD_MSC_DESC_LEN)
#else
#define USBD_STATIC_DESC_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN)
#endif
#define USBD_STR_0 (0x00)
#define USBD_STR_MANUF (0x01)
#define USBD_STR_PRODUCT (0x02)
#define USBD_STR_SERIAL (0x03)
#define USBD_STR_CDC (0x04)
#define USBD_STR_MSC (0x05)
#define USBD_MAX_POWER_MA (250)
#define USBD_DESC_STR_MAX (20)
#define USBD_ITF_CDC (0) // needs 2 interfaces
#define USBD_CDC_EP_CMD (0x81)
#define USBD_CDC_EP_OUT (0x02)
#define USBD_CDC_EP_IN (0x82)
#if CFG_TUD_MSC
#define USBD_ITF_MSC (2)
#define EPNUM_MSC_OUT (0x03)
#define EPNUM_MSC_IN (0x83)
#endif
/* Limits of statically defined USB interfaces, endpoints, strings */
#if CFG_TUD_MSC
#define USBD_ITF_STATIC_MAX (3)
#define USBD_STR_STATIC_MAX (USBD_STR_MSC + 1)
#define USBD_EP_STATIC_MAX (0x04) // ignoring the IN EP flag 0x80
#else
#define USBD_ITF_STATIC_MAX (2)
#define USBD_STR_STATIC_MAX (USBD_STR_CDC + 1)
#define USBD_EP_STATIC_MAX (0x03) // ignoring the IN EP flag 0x80
#endif
#endif // MICROPY_INCLUDED_SHARED_TINYUSB_TUSB_CONFIG_H

View File

@ -1,157 +0,0 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2019 Damien P. George
* Copyright (c) 2022 Blake W. Felt
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "mpconfigport.h"
#include "tusb.h"
#include "usbd.h"
#if CFG_TUD_MSC
#define USBD_DESC_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN + TUD_MSC_DESC_LEN)
#else
#define USBD_DESC_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN)
#endif
#define USBD_MAX_POWER_MA (250)
#define USBD_ITF_CDC (0) // needs 2 interfaces
#define USBD_ITF_MSC (2)
#if CFG_TUD_MSC
#define USBD_ITF_MAX (3)
#else
#define USBD_ITF_MAX (2)
#endif
#define USBD_CDC_EP_CMD (0x81)
#define USBD_CDC_EP_OUT (0x02)
#define USBD_CDC_EP_IN (0x82)
#define USBD_CDC_CMD_MAX_SIZE (8)
#define USBD_CDC_IN_OUT_MAX_SIZE (64)
#define EPNUM_MSC_OUT (0x03)
#define EPNUM_MSC_IN (0x83)
#define USBD_STR_0 (0x00)
#define USBD_STR_MANUF (0x01)
#define USBD_STR_PRODUCT (0x02)
#define USBD_STR_SERIAL (0x03)
#define USBD_STR_CDC (0x04)
#define USBD_STR_MSC (0x05)
#define USBD_DESC_STR_MAX (20)
#define USBD_DESC_SERIAL_MAX (32)
// Note: descriptors returned from callbacks must exist long enough for transfer to complete
static const tusb_desc_device_t usbd_desc_device = {
.bLength = sizeof(tusb_desc_device_t),
.bDescriptorType = TUSB_DESC_DEVICE,
.bcdUSB = 0x0200,
.bDeviceClass = TUSB_CLASS_MISC,
.bDeviceSubClass = MISC_SUBCLASS_COMMON,
.bDeviceProtocol = MISC_PROTOCOL_IAD,
.bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
.idVendor = MICROPY_HW_USB_VID,
.idProduct = MICROPY_HW_USB_PID,
.bcdDevice = 0x0100,
.iManufacturer = USBD_STR_MANUF,
.iProduct = USBD_STR_PRODUCT,
.iSerialNumber = USBD_STR_SERIAL,
.bNumConfigurations = 1,
};
static const uint8_t usbd_desc_cfg[USBD_DESC_LEN] = {
TUD_CONFIG_DESCRIPTOR(1, USBD_ITF_MAX, USBD_STR_0, USBD_DESC_LEN,
0, USBD_MAX_POWER_MA),
TUD_CDC_DESCRIPTOR(USBD_ITF_CDC, USBD_STR_CDC, USBD_CDC_EP_CMD,
USBD_CDC_CMD_MAX_SIZE, USBD_CDC_EP_OUT, USBD_CDC_EP_IN, USBD_CDC_IN_OUT_MAX_SIZE),
#if CFG_TUD_MSC
TUD_MSC_DESCRIPTOR(USBD_ITF_MSC, 5, EPNUM_MSC_OUT, EPNUM_MSC_IN, 64),
#endif
};
static const char *const usbd_desc_str[] = {
[USBD_STR_MANUF] = "MicroPython",
[USBD_STR_PRODUCT] = "Board in FS mode",
[USBD_STR_SERIAL] = NULL, // generated dynamically
[USBD_STR_CDC] = "Board CDC",
#if CFG_TUD_MSC
[USBD_STR_MSC] = "Board MSC",
#endif
};
const uint8_t *tud_descriptor_device_cb(void) {
return (const uint8_t *)&usbd_desc_device;
}
const uint8_t *tud_descriptor_configuration_cb(uint8_t index) {
(void)index;
return usbd_desc_cfg;
}
const uint16_t *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
static uint16_t desc_str[USBD_DESC_STR_MAX];
uint8_t len;
if (index == 0) {
desc_str[1] = 0x0409; // supported language is English
len = 1;
} else {
if (index >= sizeof(usbd_desc_str) / sizeof(usbd_desc_str[0])) {
return NULL;
}
// check, if serial is requested
if (index == USBD_STR_SERIAL) {
uint8_t buffer[USBD_DESC_SERIAL_MAX] = {0};
int buflen;
const char *hexdig = "0123456789abcdef";
buflen = usbd_serialnumber(buffer);
// byte by byte conversion
len = 0;
for (int i=0; i<buflen; i++) {
uint8_t val = buffer[i];
desc_str[1 + len] = hexdig[val >> 4];
desc_str[2 + len] = hexdig[val & 0x0F];
len += 2;
}
} else {
const char *str = usbd_desc_str[index];
for (len = 0; len < USBD_DESC_STR_MAX - 1 && str[len]; ++len) {
desc_str[1 + len] = str[len];
}
}
}
// first byte is length (including header), second byte is string type
desc_str[0] = (TUSB_DESC_STRING << 8) | (2 * len + 2);
return desc_str;
}
void usbd_reset_descriptor(void) {
// not used yet
}

View File

@ -43,6 +43,7 @@ PATHS = [
"shared/netutils/*.[ch]",
"shared/timeutils/*.[ch]",
"shared/runtime/*.[ch]",
"shared/tinyusb/*.[ch]",
"mpy-cross/*.[ch]",
"ports/**/*.[ch]",
"py/*.[ch]",