Skip to content
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

usb: Refactor some TinyUSB code from rp2 port to shared/tinyusb. #9816

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion ports/nrf/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ SRC_SHARED_C += $(addprefix shared/,\
runtime/pyexec.c \
runtime/sys_stdio_mphal.c \
runtime/interrupt_char.c \
runtime/tinyusb_helpers.c \
tinyusb/mp_cdc_common.c \
timeutils/timeutils.c \
)

Expand Down
11 changes: 9 additions & 2 deletions ports/rp2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,10 @@ set(MICROPY_SOURCE_LIB
${MICROPY_DIR}/shared/runtime/pyexec.c
${MICROPY_DIR}/shared/runtime/stdout_helpers.c
${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/mp_cdc_common.c
${MICROPY_DIR}/shared/tinyusb/mp_usbd.c
${MICROPY_DIR}/shared/tinyusb/mp_usbd_descriptor.c
)

set(MICROPY_SOURCE_DRIVERS
Expand Down Expand Up @@ -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
)
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 0 additions & 3 deletions ports/rp2/boards/PICO/mpconfigboard.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// Board and hardware specific configuration
#define MICROPY_HW_BOARD_NAME "Raspberry Pi Pico"
#define MICROPY_HW_FLASH_STORAGE_BYTES (1408 * 1024)

// Enable USB Mass Storage with FatFS filesystem.
// #define MICROPY_HW_USB_MSC (1)
3 changes: 3 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 "mp_usbd.h"

#include "pico/stdlib.h"
#include "pico/binary_info.h"
Expand Down Expand Up @@ -85,7 +86,9 @@ int main(int argc, char **argv) {
#endif

#if MICROPY_HW_ENABLE_USBDEV
#if MICROPY_HW_USB_CDC
bi_decl(bi_program_feature("USB REPL"))
#endif
tusb_init();
#endif

Expand Down
28 changes: 22 additions & 6 deletions ports/rp2/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,28 @@
// Options controlling how MicroPython is built, overriding defaults in py/mpconfig.h

#include <stdint.h>
#include "hardware/flash.h"
#include "hardware/spi.h"
#include "hardware/sync.h"
#include "pico/binary_info.h"
#include "pico/multicore.h"
#include "mpconfigboard.h"
#if MICROPY_HW_USB_MSC
#include "hardware/flash.h"
#endif

// Board and hardware specific configuration
#define MICROPY_HW_MCU_NAME "RP2040"
#define MICROPY_HW_ENABLE_UART_REPL (0) // useful if there is no USB
#define MICROPY_HW_ENABLE_USBDEV (1)
#define MICROPY_HW_ENABLE_UART_REPL (1) // useful if there is no USB
#define MICROPY_HW_ENABLE_USBDEV (1)

#if MICROPY_HW_ENABLE_USBDEV
// Enable USB-CDC serial port
#ifndef MICROPY_HW_USB_CDC
#define MICROPY_HW_USB_CDC (1)
#endif
// Enable USB Mass Storage with FatFS filesystem.
#ifndef MICROPY_HW_USB_MSC
#define MICROPY_HW_USB_MSC (0)
#endif
#endif

#ifndef MICROPY_CONFIG_ROM_LEVEL
#define MICROPY_CONFIG_ROM_LEVEL (MICROPY_CONFIG_ROM_LEVEL_EXTRA_FEATURES)
Expand Down Expand Up @@ -198,6 +207,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 All @@ -210,7 +226,7 @@ extern void mp_thread_end_atomic_section(uint32_t);
#define MICROPY_PY_LWIP_EXIT lwip_lock_release();

#if MICROPY_HW_ENABLE_USBDEV
#define MICROPY_HW_USBDEV_TASK_HOOK extern void tud_task_ext(uint32_t, bool); tud_task_ext(0, false);
#define MICROPY_HW_USBDEV_TASK_HOOK extern void usbd_task(void); usbd_task();
#define MICROPY_VM_HOOK_COUNT (10)
#define MICROPY_VM_HOOK_INIT static uint vm_hook_divisor = MICROPY_VM_HOOK_COUNT;
#define MICROPY_VM_HOOK_POLL if (get_core_num() == 0 && --vm_hook_divisor == 0) { \
Expand Down
12 changes: 6 additions & 6 deletions ports/rp2/mphalport.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
#include "lib/cyw43-driver/src/cyw43.h"
#endif

#if MICROPY_HW_ENABLE_UART_REPL || MICROPY_HW_ENABLE_USBDEV
#if MICROPY_HW_ENABLE_UART_REPL || MICROPY_HW_USB_CDC

#ifndef MICROPY_HW_STDIN_BUFFER_LEN
#define MICROPY_HW_STDIN_BUFFER_LEN 512
Expand All @@ -50,7 +50,7 @@ ringbuf_t stdin_ringbuf = { stdin_ringbuf_array, sizeof(stdin_ringbuf_array) };

#endif

#if MICROPY_HW_ENABLE_USBDEV
#if MICROPY_HW_USB_CDC

uint8_t cdc_itf_pending; // keep track of cdc interfaces which need attention to poll

Expand Down Expand Up @@ -91,10 +91,10 @@ void tud_cdc_rx_cb(uint8_t itf) {

uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
uintptr_t ret = 0;
#if MICROPY_HW_ENABLE_USBDEV
#if MICROPY_HW_USB_CDC
poll_cdc_interfaces();
#endif
#if MICROPY_HW_ENABLE_UART_REPL || MICROPY_HW_ENABLE_USBDEV
#if MICROPY_HW_ENABLE_UART_REPL || MICROPY_HW_USB_CDC
if ((poll_flags & MP_STREAM_POLL_RD) && ringbuf_peek(&stdin_ringbuf) != -1) {
ret |= MP_STREAM_POLL_RD;
}
Expand All @@ -108,7 +108,7 @@ uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
// Receive single character
int mp_hal_stdin_rx_chr(void) {
for (;;) {
#if MICROPY_HW_ENABLE_USBDEV
#if MICROPY_HW_USB_CDC
poll_cdc_interfaces();
#endif

Expand All @@ -132,7 +132,7 @@ void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
mp_uart_write_strn(str, len);
#endif

#if MICROPY_HW_ENABLE_USBDEV
#if MICROPY_HW_USB_CDC
if (tud_cdc_connected()) {
for (size_t i = 0; i < len;) {
uint32_t n = len - i;
Expand Down
151 changes: 0 additions & 151 deletions ports/rp2/tusb_port.c

This file was deleted.

44 changes: 44 additions & 0 deletions ports/rp2/usbd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2022 Blake W. Felt & Angus Gratton
*
* 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 "mp_usbd.h"
#include "string.h"
#include "tusb.h"
#include "pico/unique_id.h"

void mp_usbd_port_get_serial_number(char *serial_buf) {
pico_unique_board_id_t id;
pico_get_unique_board_id(&id);
// convert to hex
int hexlen = sizeof(id.id) * 2;
MP_STATIC_ASSERT(hexlen <= USBD_DESC_STR_MAX);
for (int i = 0; i < hexlen; i += 2) {
static const char *hexdig = "0123456789abcdef";
serial_buf[i] = hexdig[id.id[i / 2] >> 4];
serial_buf[i + 1] = hexdig[id.id[i / 2] & 0x0f];
}
serial_buf[hexlen] = 0;
}
File renamed without changes.