Skip to content

shared/tinyusb: (just rp2 for now) change USB device settings and add HID #9356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
13 changes: 12 additions & 1 deletion ports/rp2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,23 @@ 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
${MICROPY_DIR}/shared/tinyusb/usbd_descriptor.c
${MICROPY_DIR}/shared/tinyusb/usbd_hid.c
${MICROPY_DIR}/shared/tinyusb/usbd.c
)

set(MICROPY_SOURCE_QSTR
${MICROPY_SOURCE_PY}
${MICROPY_DIR}/shared/readline/readline.c
${MICROPY_DIR}/shared/runtime/mpirq.c
${MICROPY_DIR}/shared/runtime/sys_stdio_mphal.c
${MICROPY_DIR}/shared/tinyusb/usbd.c
${MICROPY_DIR}/shared/tinyusb/usbd_descriptor.c
${MICROPY_DIR}/shared/tinyusb/usbd_hid.c
${PROJECT_SOURCE_DIR}/machine_adc.c
${PROJECT_SOURCE_DIR}/machine_i2c.c
${PROJECT_SOURCE_DIR}/machine_i2s.c
Expand Down Expand Up @@ -233,6 +239,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)
Expand Down
5 changes: 5 additions & 0 deletions ports/rp2/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down
8 changes: 8 additions & 0 deletions ports/rp2/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
#define MICROPY_VFS_FAT (1)
#define MICROPY_SSL_MBEDTLS (1)
#define MICROPY_PY_LWIP_SOCK_RAW (MICROPY_PY_LWIP)
#define MICROPY_HW_USB_HID (1)

// fatfs configuration
#define MICROPY_FATFS_ENABLE_LFN (1)
Expand Down Expand Up @@ -198,6 +199,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);
Expand Down
151 changes: 0 additions & 151 deletions ports/rp2/tusb_port.c

This file was deleted.

14 changes: 14 additions & 0 deletions ports/rp2/usbd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

#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;
}
18 changes: 15 additions & 3 deletions ports/rp2/tusb_config.h → shared/tinyusb/tusb_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
* THE SOFTWARE.
*
*/
#ifndef MICROPY_INCLUDED_RP2_TUSB_CONFIG_H
#define MICROPY_INCLUDED_RP2_TUSB_CONFIG_H

#ifndef MICROPY_INCLUDED_EXTMOD_TUSB_CONFIG_H
#define MICROPY_INCLUDED_EXTMOD_TUSB_CONFIG_H

#include <py/mpconfig.h>
#include "mpconfigport.h"

#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE)
Expand All @@ -41,4 +43,14 @@
#define CFG_TUD_MSC_BUFSIZE (MICROPY_FATFS_MAX_SS)
#endif

#endif // MICROPY_INCLUDED_RP2_TUSB_CONFIG_H
#if MICROPY_HW_USB_HID
#define CFG_TUD_HID (1)
#endif

// HID buffer size Should be sufficient to hold ID (if any) + Data
#define CFG_TUD_HID_EP_BUFSIZE (64)

#define MICROPY_HW_USB_MAX_DESCRIPTORS (10)
#define MICROPY_HW_USB_MAX_HID (3)

#endif // MICROPY_INCLUDED_EXTMOD_TUSB_CONFIG_H
50 changes: 50 additions & 0 deletions shared/tinyusb/usbd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

#include "py/runtime.h"
#include "usbd.h"
#include "tusb_config.h"

// tusb.h is not available when running the string preprocessor
#ifndef NO_QSTR
#include "tusb.h"
#endif

void usbd_reset_all(void) {
usbd_reset_descriptor();
usbd_reset_hid();
}

STATIC mp_obj_t usbd_ready(void) {
return mp_obj_new_bool(tud_ready());
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(usbd_ready_obj, usbd_ready);

STATIC mp_obj_t usbd_connected(void) {
return mp_obj_new_bool(tud_connected());
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(usbd_connected_obj, usbd_connected);

STATIC mp_obj_t usbd_remote_wakeup(void) {
return mp_obj_new_bool(tud_remote_wakeup());
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(usbd_remote_wakeup_obj, usbd_remote_wakeup);

STATIC const mp_rom_map_elem_t usb_device_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_usb_device) },

{ MP_ROM_QSTR(MP_QSTR_ready), (mp_obj_t)&usbd_ready_obj},
{ MP_ROM_QSTR(MP_QSTR_connected), (mp_obj_t)&usbd_connected_obj},
{ MP_ROM_QSTR(MP_QSTR_remote_wakeup), (mp_obj_t)&usbd_remote_wakeup_obj},

{ MP_ROM_QSTR(MP_QSTR_Descriptor), MP_ROM_PTR(&usbd_descriptor_type) },
#if MICROPY_HW_USB_HID
{ MP_ROM_QSTR(MP_QSTR_HID), MP_ROM_PTR(&usbd_hid_type) },
#endif
};
STATIC MP_DEFINE_CONST_DICT(usb_device_module_globals, usb_device_module_globals_table);

const mp_obj_module_t mp_module_usb_device = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t *)&usb_device_module_globals,
};

MP_REGISTER_MODULE(MP_QSTR_usb_device, mp_module_usb_device);
24 changes: 24 additions & 0 deletions shared/tinyusb/usbd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

#ifndef MICROPY_INCLUDED_USBD_SHARED_H_
#define MICROPY_INCLUDED_USBD_SHARED_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);
void usbd_reset_hid(void);

// internal

extern const mp_obj_type_t usbd_descriptor_type;
extern const mp_obj_type_t usbd_hid_type;

int usbd_desc_add_descriptor(uint8_t *desc, int len, char *name);

#endif
Loading
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