Skip to content

Commit 623b8e7

Browse files
committed
WIP: tinyusb: Support Python-defined runtime USB devices.
This work was funded through GitHub Sponsors.
1 parent 813d559 commit 623b8e7

File tree

10 files changed

+443
-2
lines changed

10 files changed

+443
-2
lines changed

ports/rp2/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ set(MICROPY_SOURCE_QSTR
148148
${MICROPY_DIR}/shared/readline/readline.c
149149
${MICROPY_DIR}/shared/runtime/mpirq.c
150150
${MICROPY_DIR}/shared/runtime/sys_stdio_mphal.c
151+
${MICROPY_DIR}/shared/tinyusb/mp_usbd.c
151152
${MICROPY_PORT_DIR}/machine_adc.c
152153
${MICROPY_PORT_DIR}/machine_i2c.c
153154
${MICROPY_PORT_DIR}/machine_i2s.c

ports/rp2/modmachine.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,9 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
266266
{ MP_ROM_QSTR(MP_QSTR_SoftSPI), MP_ROM_PTR(&mp_machine_soft_spi_type) },
267267
{ MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&machine_timer_type) },
268268
{ MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&machine_uart_type) },
269+
#if MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE
270+
{ MP_ROM_QSTR(MP_QSTR_USBD), MP_ROM_PTR(&mp_type_usbd) },
271+
#endif
269272
{ MP_ROM_QSTR(MP_QSTR_WDT), MP_ROM_PTR(&machine_wdt_type) },
270273

271274
{ MP_ROM_QSTR(MP_QSTR_PWRON_RESET), MP_ROM_INT(RP2_RESET_PWRON) },

ports/rp2/modmachine.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ extern const mp_obj_type_t machine_spi_type;
1212
extern const mp_obj_type_t machine_timer_type;
1313
extern const mp_obj_type_t machine_uart_type;
1414
extern const mp_obj_type_t machine_wdt_type;
15+
extern const mp_obj_type_t mp_type_usbd;
1516

1617
void machine_pin_init(void);
1718
void machine_pin_deinit(void);

ports/rp2/mpconfigport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
#define MICROPY_HW_ENABLE_USBDEV (1)
4444
#endif
4545

46+
#define MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE (1) // TODO: should probably disable this by default
47+
4648
#if MICROPY_HW_ENABLE_USBDEV
4749
// Enable USB-CDC serial port
4850
#ifndef MICROPY_HW_USB_CDC

py/objstr.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ const byte *str_index_to_ptr(const mp_obj_type_t *type, const byte *self_data, s
100100
mp_obj_t index, bool is_slice);
101101
const byte *find_subbytes(const byte *haystack, size_t hlen, const byte *needle, size_t nlen, int direction);
102102

103+
#define MP_DEFINE_BYTES_OBJ(obj_name, target, len) mp_obj_str_t obj_name = {{&mp_type_bytes}, 0, (len), (const byte *)(target)}
104+
103105
mp_obj_t mp_obj_bytes_hex(size_t n_args, const mp_obj_t *args, const mp_obj_type_t *type);
104106
mp_obj_t mp_obj_bytes_fromhex(mp_obj_t type_in, mp_obj_t data);
105107

py/runtime.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ void mp_init(void) {
171171
MP_STATE_VM(bluetooth) = MP_OBJ_NULL;
172172
#endif
173173

174+
#if MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE
175+
MP_STATE_VM(usbd) = MP_OBJ_NULL;
176+
#endif
177+
174178
#if MICROPY_PY_THREAD_GIL
175179
mp_thread_mutex_init(&MP_STATE_VM(gil_mutex));
176180
#endif

0 commit comments

Comments
 (0)