Skip to content

Commit

Permalink
WIP: tinyusb: Support Python-defined runtime USB devices.
Browse files Browse the repository at this point in the history
This work was funded through GitHub Sponsors.
  • Loading branch information
projectgus committed Jul 10, 2023
1 parent 813d559 commit 623b8e7
Show file tree
Hide file tree
Showing 10 changed files with 443 additions and 2 deletions.
1 change: 1 addition & 0 deletions ports/rp2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ set(MICROPY_SOURCE_QSTR
${MICROPY_DIR}/shared/readline/readline.c
${MICROPY_DIR}/shared/runtime/mpirq.c
${MICROPY_DIR}/shared/runtime/sys_stdio_mphal.c
${MICROPY_DIR}/shared/tinyusb/mp_usbd.c
${MICROPY_PORT_DIR}/machine_adc.c
${MICROPY_PORT_DIR}/machine_i2c.c
${MICROPY_PORT_DIR}/machine_i2s.c
Expand Down
3 changes: 3 additions & 0 deletions ports/rp2/modmachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,9 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_SoftSPI), MP_ROM_PTR(&mp_machine_soft_spi_type) },
{ MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&machine_timer_type) },
{ MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&machine_uart_type) },
#if MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE
{ MP_ROM_QSTR(MP_QSTR_USBD), MP_ROM_PTR(&mp_type_usbd) },
#endif
{ MP_ROM_QSTR(MP_QSTR_WDT), MP_ROM_PTR(&machine_wdt_type) },

{ MP_ROM_QSTR(MP_QSTR_PWRON_RESET), MP_ROM_INT(RP2_RESET_PWRON) },
Expand Down
1 change: 1 addition & 0 deletions ports/rp2/modmachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ extern const mp_obj_type_t machine_spi_type;
extern const mp_obj_type_t machine_timer_type;
extern const mp_obj_type_t machine_uart_type;
extern const mp_obj_type_t machine_wdt_type;
extern const mp_obj_type_t mp_type_usbd;

void machine_pin_init(void);
void machine_pin_deinit(void);
Expand Down
2 changes: 2 additions & 0 deletions ports/rp2/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
#define MICROPY_HW_ENABLE_USBDEV (1)
#endif

#define MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE (1) // TODO: should probably disable this by default

#if MICROPY_HW_ENABLE_USBDEV
// Enable USB-CDC serial port
#ifndef MICROPY_HW_USB_CDC
Expand Down
2 changes: 2 additions & 0 deletions py/objstr.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ const byte *str_index_to_ptr(const mp_obj_type_t *type, const byte *self_data, s
mp_obj_t index, bool is_slice);
const byte *find_subbytes(const byte *haystack, size_t hlen, const byte *needle, size_t nlen, int direction);

#define MP_DEFINE_BYTES_OBJ(obj_name, target, len) mp_obj_str_t obj_name = {{&mp_type_bytes}, 0, (len), (const byte *)(target)}

mp_obj_t mp_obj_bytes_hex(size_t n_args, const mp_obj_t *args, const mp_obj_type_t *type);
mp_obj_t mp_obj_bytes_fromhex(mp_obj_t type_in, mp_obj_t data);

Expand Down
4 changes: 4 additions & 0 deletions py/runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ void mp_init(void) {
MP_STATE_VM(bluetooth) = MP_OBJ_NULL;
#endif

#if MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE
MP_STATE_VM(usbd) = MP_OBJ_NULL;
#endif

#if MICROPY_PY_THREAD_GIL
mp_thread_mutex_init(&MP_STATE_VM(gil_mutex));
#endif
Expand Down

0 comments on commit 623b8e7

Please sign in to comment.