shared/tinyusb: Create common TinyUSB code for reuse by ports.

This code originates from the rp2 port, and the rp2 port has been updated
to use this common code.
This commit is contained in:
Blake Felt 2022-09-17 20:34:09 -06:00 committed by Damien George
parent a513558e3a
commit eed4eb2645
8 changed files with 160 additions and 21 deletions

View File

@ -96,6 +96,8 @@ set(MICROPY_SOURCE_LIB
${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
)
set(MICROPY_SOURCE_DRIVERS
@ -125,8 +127,8 @@ set(MICROPY_SOURCE_PORT
pendsv.c
rp2_flash.c
rp2_pio.c
tusb_port.c
uart.c
usbd.c
msc_disk.c
mbedtls/mbedtls_port.c
)
@ -233,6 +235,11 @@ if(MICROPY_BLUETOOTH_NIMBLE)
list(APPEND MICROPY_INC_CORE ${NIMBLE_INCLUDE})
endif()
# tinyusb helper
target_include_directories(${MICROPY_TARGET} PRIVATE
${MICROPY_DIR}/shared/tinyusb/
)
if (MICROPY_PY_NETWORK_CYW43)
string(CONCAT GIT_SUBMODULES "${GIT_SUBMODULES} " lib/cyw43-driver)
if((NOT (${ECHO_SUBMODULES})) AND NOT EXISTS ${MICROPY_DIR}/lib/cyw43-driver/src/cyw43.h)

View File

@ -43,6 +43,7 @@
#include "modrp2.h"
#include "mpbthciport.h"
#include "genhdr/mpversion.h"
#include "usbd.h"
#include "pico/stdlib.h"
#include "pico/binary_info.h"
@ -87,6 +88,7 @@ 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
@ -159,6 +161,9 @@ 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

@ -198,6 +198,13 @@ extern const struct _mod_network_nic_type_t mod_network_nic_type_wiznet5k;
// Miscellaneous settings
#ifndef MICROPY_HW_USB_VID
#define MICROPY_HW_USB_VID (0x2E8A) // Raspberry Pi
#endif
#ifndef MICROPY_HW_USB_PID
#define MICROPY_HW_USB_PID (0x0005) // RP2 MicroPython
#endif
// Entering a critical section.
extern uint32_t mp_thread_begin_atomic_section(void);
extern void mp_thread_end_atomic_section(uint32_t);

39
ports/rp2/usbd.c Normal file
View File

@ -0,0 +1,39 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* 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 "usbd.h"
#include "string.h"
#include "pico/unique_id.h"
int usbd_serialnumber(uint8_t *buf) {
pico_unique_board_id_t id;
const int len = 8;
pico_get_unique_board_id(&id);
memcpy(buf, id.id, len);
return len;
}

View File

@ -22,9 +22,11 @@
* THE SOFTWARE.
*
*/
#ifndef MICROPY_INCLUDED_RP2_TUSB_CONFIG_H
#define MICROPY_INCLUDED_RP2_TUSB_CONFIG_H
#ifndef MICROPY_INCLUDED_SHARED_TINYUSB_TUSB_CONFIG_H
#define MICROPY_INCLUDED_SHARED_TINYUSB_TUSB_CONFIG_H
#include <py/mpconfig.h>
#include "mpconfigport.h"
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE)
@ -41,4 +43,4 @@
#define CFG_TUD_MSC_BUFSIZE (MICROPY_FATFS_MAX_SS)
#endif
#endif // MICROPY_INCLUDED_RP2_TUSB_CONFIG_H
#endif // MICROPY_INCLUDED_SHARED_TINYUSB_TUSB_CONFIG_H

32
shared/tinyusb/usbd.c Normal file
View File

@ -0,0 +1,32 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* 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 "py/runtime.h"
#include "usbd.h"
void usbd_reset_all(void) {
usbd_reset_descriptor();
}

41
shared/tinyusb/usbd.h Normal file
View File

@ -0,0 +1,41 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* 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.
*/
#ifndef MICROPY_INCLUDED_SHARED_TINYUSB_USBD_H
#define MICROPY_INCLUDED_SHARED_TINYUSB_USBD_H
#include "py/obj.h"
// defined externally (needed per port)
int usbd_serialnumber(uint8_t *buf);
// external use
void usbd_reset_all(void);
void usbd_reset_descriptor(void);
#endif // MICROPY_INCLUDED_SHARED_TINYUSB_USBD_H

View File

@ -4,6 +4,7 @@
* 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
@ -24,15 +25,9 @@
* THE SOFTWARE.
*/
#include "mpconfigport.h"
#include "tusb.h"
#include "pico/unique_id.h"
#ifndef MICROPY_HW_USB_VID
#define MICROPY_HW_USB_VID (0x2E8A) // Raspberry Pi
#endif
#ifndef MICROPY_HW_USB_PID
#define MICROPY_HW_USB_PID (0x0005) // RP2 MicroPython
#endif
#include "usbd.h"
#if CFG_TUD_MSC
#define USBD_DESC_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN + TUD_MSC_DESC_LEN)
@ -65,6 +60,9 @@
#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 = {
@ -115,8 +113,7 @@ const uint8_t *tud_descriptor_configuration_cb(uint8_t index) {
}
const uint16_t *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
#define DESC_STR_MAX (20)
static uint16_t desc_str[DESC_STR_MAX];
static uint16_t desc_str[USBD_DESC_STR_MAX];
uint8_t len;
if (index == 0) {
@ -128,17 +125,22 @@ const uint16_t *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
}
// check, if serial is requested
if (index == USBD_STR_SERIAL) {
pico_unique_board_id_t id;
pico_get_unique_board_id(&id);
uint8_t buffer[USBD_DESC_SERIAL_MAX] = {0};
int buflen;
const char *hexdig = "0123456789abcdef";
buflen = usbd_serialnumber(buffer);
// byte by byte conversion
for (len = 0; len < 16; len += 2) {
const char *hexdig = "0123456789abcdef";
desc_str[1 + len] = hexdig[id.id[len >> 1] >> 4];
desc_str[1 + len + 1] = hexdig[id.id[len >> 1] & 0x0f];
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 < DESC_STR_MAX - 1 && str[len]; ++len) {
for (len = 0; len < USBD_DESC_STR_MAX - 1 && str[len]; ++len) {
desc_str[1 + len] = str[len];
}
}
@ -149,3 +151,7 @@ const uint16_t *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
return desc_str;
}
void usbd_reset_descriptor(void) {
// not used yet
}