Skip to content

Commit d518a1b

Browse files
committed
tinyusb: More refactoring of static USB device implementation.
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>
1 parent 3765f88 commit d518a1b

File tree

12 files changed

+179
-116
lines changed

12 files changed

+179
-116
lines changed

ports/nrf/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ SRC_SHARED_C += $(addprefix shared/,\
242242
runtime/pyexec.c \
243243
runtime/sys_stdio_mphal.c \
244244
runtime/interrupt_char.c \
245-
runtime/tinyusb_helpers.c \
245+
tinyusb/cdc_common.c \
246246
timeutils/timeutils.c \
247247
)
248248

ports/rp2/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ set(MICROPY_SOURCE_LIB
9494
${MICROPY_DIR}/shared/runtime/pyexec.c
9595
${MICROPY_DIR}/shared/runtime/stdout_helpers.c
9696
${MICROPY_DIR}/shared/runtime/sys_stdio_mphal.c
97-
${MICROPY_DIR}/shared/runtime/tinyusb_helpers.c
9897
${MICROPY_DIR}/shared/timeutils/timeutils.c
98+
${MICROPY_DIR}/shared/tinyusb/cdc_common.c
9999
${MICROPY_DIR}/shared/tinyusb/usbd.c
100100
${MICROPY_DIR}/shared/tinyusb/usbd_descriptor.c
101101
)

ports/rp2/main.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ int main(int argc, char **argv) {
8888
#if MICROPY_HW_ENABLE_USBDEV
8989
bi_decl(bi_program_feature("USB REPL"))
9090
tusb_init();
91-
usbd_reset_all(); // run now just in case usb initialization occurs early
9291
#endif
9392

9493
#if MICROPY_PY_THREAD
@@ -161,9 +160,6 @@ int main(int argc, char **argv) {
161160
machine_pin_init();
162161
rp2_pio_init();
163162
machine_i2s_init0();
164-
#if MICROPY_HW_ENABLE_USBDEV
165-
usbd_reset_all();
166-
#endif
167163

168164
#if MICROPY_PY_BLUETOOTH
169165
mp_bluetooth_hci_init();

ports/rp2/mpconfigport.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ extern void mp_thread_end_atomic_section(uint32_t);
217217
#define MICROPY_PY_LWIP_EXIT lwip_lock_release();
218218

219219
#if MICROPY_HW_ENABLE_USBDEV
220-
#define MICROPY_HW_USBDEV_TASK_HOOK extern void tud_task_ext(uint32_t, bool); tud_task_ext(0, false);
220+
#define MICROPY_HW_USBDEV_TASK_HOOK extern void usbd_task(void); usbd_task();
221221
#define MICROPY_VM_HOOK_COUNT (10)
222222
#define MICROPY_VM_HOOK_INIT static uint vm_hook_divisor = MICROPY_VM_HOOK_COUNT;
223223
#define MICROPY_VM_HOOK_POLL if (get_core_num() == 0 && --vm_hook_divisor == 0) { \

ports/rp2/usbd.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* The MIT License (MIT)
55
*
6-
* Copyright (c) 2022 Blake W. Felt
6+
* Copyright (c) 2022 Blake W. Felt & Angus Gratton
77
*
88
* Permission is hereby granted, free of charge, to any person obtaining a copy
99
* of this software and associated documentation files (the "Software"), to deal
@@ -26,14 +26,19 @@
2626

2727
#include "usbd.h"
2828
#include "string.h"
29+
#include "tusb.h"
2930
#include "pico/unique_id.h"
3031

31-
int usbd_serialnumber(uint8_t *buf) {
32+
void usbd_port_get_serial_number(char *serial_buf) {
3233
pico_unique_board_id_t id;
33-
const int len = 8;
34-
3534
pico_get_unique_board_id(&id);
36-
memcpy(buf, id.id, len);
37-
38-
return len;
35+
// convert to hex
36+
int hexlen = sizeof(id.id) * 2;
37+
MP_STATIC_ASSERT(hexlen <= USBD_DESC_STR_MAX);
38+
for (int i = 0; i < hexlen; i += 2) {
39+
static const char *hexdig = "0123456789abcdef";
40+
serial_buf[i] = hexdig[id.id[i / 2] >> 4];
41+
serial_buf[i + 1] = hexdig[id.id[i / 2] & 0x0f];
42+
}
43+
serial_buf[hexlen] = 0;
3944
}
File renamed without changes.

shared/tinyusb/tusb_config.h

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* The MIT License (MIT)
33
*
44
* Copyright (c) 2020-2021 Damien P. George
5-
* Copyright (c) 2022 Blake W. Felt
5+
* Copyright (c) 2022 Blake W. Felt & Angus Gratton
66
*
77
* Permission is hereby granted, free of charge, to any person obtaining a copy
88
* of this software and associated documentation files (the "Software"), to deal
@@ -30,6 +30,18 @@
3030
#include <py/mpconfig.h>
3131
#include "mpconfigport.h"
3232

33+
#ifndef MICROPY_HW_USB_STR_MANUF
34+
#define MICROPY_HW_USB_STR_MANUF "MicroPython"
35+
#endif
36+
37+
#ifndef MICROPY_HW_USB_STR_PRODUCT
38+
#define MICROPY_HW_USB_STR_PRODUCT "Board in FS mode"
39+
#endif
40+
41+
#ifndef MICROPY_HW_USB_STR_CDC
42+
#define MICROPY_HW_USB_STR_CDC "Board CDC"
43+
#endif
44+
3345
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE)
3446

3547
#define CFG_TUD_CDC (1)
@@ -39,9 +51,54 @@
3951

4052
#if MICROPY_HW_USB_MSC
4153
// Board and hardware specific configuration
54+
#ifndef MICROPY_HW_USB_STR_MSC
55+
#define MICROPY_HW_USB_STR_MSC "Board MSC"
56+
#endif
4257
#define CFG_TUD_MSC (1)
4358
// Set MSC EP buffer size to FatFS block size to avoid partial read/writes (offset arg).
4459
#define CFG_TUD_MSC_BUFSIZE (MICROPY_FATFS_MAX_SS)
4560
#endif
4661

62+
// Define static descriptor size and interface count based on the above config
63+
64+
#if CFG_TUD_MSC
65+
#define USBD_STATIC_DESC_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN + TUD_MSC_DESC_LEN)
66+
#else
67+
#define USBD_STATIC_DESC_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN)
68+
#endif
69+
70+
#define USBD_STR_0 (0x00)
71+
#define USBD_STR_MANUF (0x01)
72+
#define USBD_STR_PRODUCT (0x02)
73+
#define USBD_STR_SERIAL (0x03)
74+
#define USBD_STR_CDC (0x04)
75+
#define USBD_STR_MSC (0x05)
76+
77+
#define USBD_MAX_POWER_MA (250)
78+
79+
#define USBD_DESC_STR_MAX (20)
80+
81+
#define USBD_ITF_CDC (0) // needs 2 interfaces
82+
83+
#define USBD_CDC_EP_CMD (0x81)
84+
#define USBD_CDC_EP_OUT (0x02)
85+
#define USBD_CDC_EP_IN (0x82)
86+
87+
#if CFG_TUD_MSC
88+
#define USBD_ITF_MSC (2)
89+
#define EPNUM_MSC_OUT (0x03)
90+
#define EPNUM_MSC_IN (0x83)
91+
#endif
92+
93+
/* Limits of statically defined USB interfaces, endpoints, strings */
94+
#if CFG_TUD_MSC
95+
#define USBD_ITF_STATIC_MAX (3)
96+
#define USBD_STR_STATIC_MAX (USBD_STR_MSC + 1)
97+
#define USBD_EP_STATIC_MAX (0x04) // ignoring the IN EP flag 0x80
98+
#else
99+
#define USBD_ITF_STATIC_MAX (2)
100+
#define USBD_STR_STATIC_MAX (USBD_STR_CDC + 1)
101+
#define USBD_EP_STATIC_MAX (0x03) // ignoring the IN EP flag 0x80
102+
#endif
103+
47104
#endif // MICROPY_INCLUDED_LIB_UTILS_TUSB_CONFIG_H

shared/tinyusb/usbd.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* The MIT License (MIT)
55
*
6-
* Copyright (c) 2022 Blake W. Felt
6+
* Copyright (c) 2022 Blake W. Felt & Angus Gratton
77
*
88
* Permission is hereby granted, free of charge, to any person obtaining a copy
99
* of this software and associated documentation files (the "Software"), to deal
@@ -24,9 +24,14 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#include "py/runtime.h"
28-
#include "usbd.h"
27+
#include <stdlib.h>
2928

30-
void usbd_reset_all(void) {
31-
usbd_reset_descriptor();
29+
#ifndef NO_QSTR
30+
#include "tusb.h" // TinyUSB is not avaiable when running the string preprocessor
31+
#include "device/usbd.h"
32+
#include "device/usbd_pvt.h"
33+
#endif
34+
35+
void usbd_task(void) {
36+
tud_task_ext(0, false);
3237
}

shared/tinyusb/usbd.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* The MIT License (MIT)
55
*
6-
* Copyright (c) 2022 Blake W. Felt
6+
* Copyright (c) 2022 Blake W. Felt & Angus Gratton
77
*
88
* Permission is hereby granted, free of charge, to any person obtaining a copy
99
* of this software and associated documentation files (the "Software"), to deal
@@ -29,13 +29,11 @@
2929

3030
#include "py/obj.h"
3131

32-
// defined externally (needed per port)
32+
// Call instead of tud_task()
33+
void usbd_task(void);
3334

34-
int usbd_serialnumber(uint8_t *buf);
35-
36-
// external use
37-
38-
void usbd_reset_all(void);
39-
void usbd_reset_descriptor(void);
35+
// Function to be implemented in port code.
36+
// Can write a string up to USBD_DESC_STR_MAX characters long, plus terminating byte.
37+
extern void usbd_port_get_serial_number(char *buf);
4038

4139
#endif // MICROPY_INCLUDED_LIB_UTILS_USBD_H

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy