Skip to content

port/rp2: Added WebUSB to RP2040. #11301

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 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ports/rp2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ string(CONCAT GIT_SUBMODULES "${GIT_SUBMODULES} " lib/tinyusb)
include(${MICROPY_DIR}/py/py.cmake)
include(${MICROPY_DIR}/extmod/extmod.cmake)
include(${PICO_SDK_PATH}/pico_sdk_init.cmake)
include(${PICO_TINYUSB_PATH}/hw/bsp/rp2040/family.cmake)

# Define the top-level project
project(${MICROPY_TARGET})
Expand Down Expand Up @@ -98,6 +99,7 @@ set(MICROPY_SOURCE_LIB
${MICROPY_DIR}/shared/tinyusb/mp_cdc_common.c
${MICROPY_DIR}/shared/tinyusb/mp_usbd.c
${MICROPY_DIR}/shared/tinyusb/mp_usbd_descriptor.c
${PICO_TINYUSB_PATH}/hw/bsp/rp2040/family.c
)

set(MICROPY_SOURCE_DRIVERS
Expand Down Expand Up @@ -127,6 +129,7 @@ set(MICROPY_SOURCE_PORT
pendsv.c
rp2_flash.c
rp2_pio.c
tusb_port.c
uart.c
usbd.c
msc_disk.c
Expand Down Expand Up @@ -356,6 +359,7 @@ target_include_directories(${MICROPY_TARGET} PRIVATE
${MICROPY_INC_CORE}
${MICROPY_INC_USERMOD}
${MICROPY_BOARD_DIR}
${PICO_TINYUSB_PATH}/src
"${PROJECT_SOURCE_DIR}"
"${CMAKE_BINARY_DIR}"
)
Expand Down
123 changes: 123 additions & 0 deletions ports/rp2/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@
#include "lib/cyw43-driver/src/cyw43.h"
#endif

#if MICROPY_HW_USB_VENDOR
#include "bsp/board.h"
#include "tusb_port.h"

#include "hardware/watchdog.h"

#include "main.h"
#endif

extern uint8_t __StackTop, __StackBottom;
extern uint8_t __GcHeapStart, __GcHeapEnd;

Expand All @@ -71,6 +80,25 @@ bi_decl(bi_program_feature_group_with_flags(BINARY_INFO_TAG_MICROPYTHON,
BINARY_INFO_ID_MP_FROZEN, "frozen modules",
BI_NAMED_GROUP_SEPARATE_COMMAS | BI_NAMED_GROUP_SORT_ALPHA));

#if MICROPY_HW_USB_VENDOR
// WebUSB URL Descriptor
#define URL "creepier-dragonfly-8429.dataplicity.io"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does the end-user override this? Is there just one blessed front-end for development via WebUSB?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The user cannot override this. It is a temorary URL we're using to test the implementation. It would idealy be replaced with a link to something like a MicroPython page about WebUSB support with a list of supported WebUSB enabled editors.


const tusb_desc_webusb_url_t desc_url = {
.bLength = 3 + sizeof(URL) - 1,
.bDescriptorType = 3,
.bScheme = 1,
.url = URL
};

// WebUSB Connection Status
static bool web_serial_connected = false;

bool check_web_serial_connected(void) {
return web_serial_connected;
}
#endif

int main(int argc, char **argv) {
#if MICROPY_HW_ENABLE_UART_REPL
bi_decl(bi_program_feature("UART REPL"))
Expand All @@ -87,6 +115,11 @@ int main(int argc, char **argv) {
bi_decl(bi_program_feature("USB REPL"))
#endif
tusb_init();
#if MICROPY_HW_USB_VENDOR
// Setup board for WebUSB
board_init();
tud_init(BOARD_TUD_RHPORT);
#endif
#endif

#if MICROPY_PY_THREAD
Expand Down Expand Up @@ -296,3 +329,93 @@ const char rp2_help_text[] =
"For further help on a specific object, type help(obj)\n"
"For a list of available modules, type help('modules')\n"
;


#if MICROPY_HW_USB_VENDOR
// Invoked when device is mounted
void tud_mount_cb(void) { }

// Invoked when device is unmounted
void tud_umount_cb(void) { }

// Invoked when usb bus is suspended
// remote_wakeup_en : if host allow us to perform remote wakeup
// Within 7ms, device must draw an average of current less than 2.5 mA from bus
void tud_suspend_cb(bool remote_wakeup_en) {
(void) remote_wakeup_en;
}

// Invoked when usb bus is resumed
void tud_resume_cb(void) { }

// Invoked when a control transfer occurred on an interface of this class
// Driver response accordingly to the request and the transfer stage (setup/data/ack)
// return false to stall control endpoint (e.g unsupported request)
bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request) {
// Nothing to with DATA & ACK stage
if (stage != CONTROL_STAGE_SETUP) return true;

switch (request->bmRequestType_bit.type)
{
case TUSB_REQ_TYPE_VENDOR:
switch (request->bRequest)
{
case VENDOR_REQUEST_WEBUSB:
// Match vendor request in BOS descriptor
// Get landing page url
return tud_control_xfer(rhport, request, (void*)(uintptr_t) &desc_url, desc_url.bLength);

case VENDOR_REQUEST_MICROSOFT:
if (request->wIndex == 7)
{
// Get Microsoft OS 2.0 compatible descriptor
uint16_t total_len;
memcpy(&total_len, desc_ms_os_20 + 8, 2);

return tud_control_xfer(rhport, request, (void*)(uintptr_t) desc_ms_os_20, total_len);
} else {

return false;
}

default: break;
}
break;

case TUSB_REQ_TYPE_CLASS:
if (request->bRequest == VENDOR_REQUEST_WEBUSB_CONNECTION) {

int status = true;

if (request->wValue == 0x01) {

web_serial_connected = true;
// Response with status OK
status = tud_control_status(rhport, request);

if (mp_interrupt_char == CHAR_CTRL_C) {
mp_sched_keyboard_interrupt(); // Interrupt file running in REPL
}
ringbuf_put(&stdin_ringbuf, CHAR_CTRL_D); // Interrupt friendly REPL
} else {

web_serial_connected = false;
// Response with status OK
// Done before resetting RP2040 to speed up response
status = tud_control_status(rhport, request);

// Reset the RP2040 using the watchdog after 10 ms
watchdog_enable(10, 1);
}

return status;
}
break;

default: break;
}

// Stall unknown request
return false;
}
#endif
8 changes: 8 additions & 0 deletions ports/rp2/main.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef MAIN_H_
#define MAIN_H_

#if MICROPY_HW_USB_VENDOR
bool check_web_serial_connected(void);
#endif

#endif /* MAIN_H_ */
4 changes: 4 additions & 0 deletions ports/rp2/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@
#ifndef MICROPY_HW_USB_MSC
#define MICROPY_HW_USB_MSC (0)
#endif
// Enable WebUSB-Vendor serial port
#ifndef MICROPY_HW_USB_VENDOR
#define MICROPY_HW_USB_VENDOR (1)
#endif
#endif

#ifndef MICROPY_CONFIG_ROM_LEVEL
Expand Down
59 changes: 57 additions & 2 deletions ports/rp2/mphalport.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@
#include "lib/cyw43-driver/src/cyw43.h"
#endif

#if MICROPY_HW_ENABLE_UART_REPL || MICROPY_HW_USB_CDC
#if MICROPY_HW_USB_VENDOR
#include "main.h"
#endif

#if MICROPY_HW_ENABLE_UART_REPL || MICROPY_HW_USB_CDC || MICROPY_HW_USB_VENDOR

#ifndef MICROPY_HW_STDIN_BUFFER_LEN
#define MICROPY_HW_STDIN_BUFFER_LEN 512
Expand Down Expand Up @@ -89,12 +93,40 @@ void tud_cdc_rx_cb(uint8_t itf) {

#endif

#if MICROPY_HW_USB_VENDOR

void poll_vendor_interfaces(void) {
if (check_web_serial_connected() && ringbuf_free(&stdin_ringbuf)) {
for (uint8_t itf = 0; itf < CFG_TUD_VENDOR; itf++) {
tud_vendor_rx_cb(itf);
}
}
}

void tud_vendor_rx_cb(uint8_t itf) {
uint32_t available_bytes = tud_vendor_n_available(itf);
for (; available_bytes > 0; available_bytes--) {
uint8_t buf[1];
tud_vendor_n_read(itf, buf, sizeof(uint8_t));
if (buf[0] == mp_interrupt_char) {
mp_sched_keyboard_interrupt();
} else {
ringbuf_put(&stdin_ringbuf, buf[0]);
}
}
}

#endif

uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
uintptr_t ret = 0;
#if MICROPY_HW_USB_CDC
poll_cdc_interfaces();
#endif
#if MICROPY_HW_ENABLE_UART_REPL || MICROPY_HW_USB_CDC
#if MICROPY_HW_USB_VENDOR
poll_vendor_interfaces();
#endif
#if MICROPY_HW_ENABLE_UART_REPL || MICROPY_HW_USB_CDC || MICROPY_HW_USB_VENDOR
if ((poll_flags & MP_STREAM_POLL_RD) && ringbuf_peek(&stdin_ringbuf) != -1) {
ret |= MP_STREAM_POLL_RD;
}
Expand All @@ -105,6 +137,9 @@ uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
if (tud_cdc_connected() && tud_cdc_write_available() > 0) {
ret |= MP_STREAM_POLL_WR;
}
if (check_web_serial_connected() && tud_vendor_write_available() > 0) {
ret |= MP_STREAM_POLL_WR;
}
#endif
}
#endif
Expand All @@ -120,6 +155,9 @@ int mp_hal_stdin_rx_chr(void) {
#if MICROPY_HW_USB_CDC
poll_cdc_interfaces();
#endif
#if MICROPY_HW_USB_VENDOR
poll_vendor_interfaces();
#endif

int c = ringbuf_get(&stdin_ringbuf);
if (c != -1) {
Expand Down Expand Up @@ -163,6 +201,23 @@ void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
}
#endif

#if MICROPY_HW_USB_VENDOR
if (check_web_serial_connected()) {
for (uint32_t i = 0; i < len;) {
uint32_t n = len - i;
if (n > 128) {
n = 128;
}
while (n > tud_vendor_write_available()) {
MICROPY_EVENT_POLL_HOOK
}
uint32_t n2 = tud_vendor_write(str + i, n);
tud_vendor_flush();
i += n2;
}
}
#endif

#if MICROPY_PY_OS_DUPTERM
mp_uos_dupterm_tx_strn(str, len);
#endif
Expand Down
78 changes: 78 additions & 0 deletions ports/rp2/tusb_port.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020-2021 Damien P. George
*
* 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 "tusb.h"
#include "pico/unique_id.h"
#include "tusb_port.h"

#define BOS_TOTAL_LEN (TUD_BOS_DESC_LEN + TUD_BOS_WEBUSB_DESC_LEN + TUD_BOS_MICROSOFT_OS_DESC_LEN)

#define MS_OS_20_DESC_LEN 0xB2

// BOS Descriptor is required for webUSB
uint8_t const desc_bos[] = {
// total length, number of device caps
TUD_BOS_DESCRIPTOR(BOS_TOTAL_LEN, 2),

// Vendor Code, iLandingPage
TUD_BOS_WEBUSB_DESCRIPTOR(VENDOR_REQUEST_WEBUSB, 1),

// Microsoft OS 2.0 descriptor
TUD_BOS_MS_OS_20_DESCRIPTOR(MS_OS_20_DESC_LEN, VENDOR_REQUEST_MICROSOFT)
};

uint8_t const * tud_descriptor_bos_cb(void) {
return desc_bos;
}

uint8_t const desc_ms_os_20[] = {
// Set header: length, type, windows version, total length
U16_TO_U8S_LE(0x000A), U16_TO_U8S_LE(MS_OS_20_SET_HEADER_DESCRIPTOR), U32_TO_U8S_LE(0x06030000), U16_TO_U8S_LE(MS_OS_20_DESC_LEN),

// Configuration subset header: length, type, configuration index, reserved, configuration total length
U16_TO_U8S_LE(0x0008), U16_TO_U8S_LE(MS_OS_20_SUBSET_HEADER_CONFIGURATION), 0, 0, U16_TO_U8S_LE(MS_OS_20_DESC_LEN-0x0A),

// Function Subset header: length, type, first interface, reserved, subset length
U16_TO_U8S_LE(0x0008), U16_TO_U8S_LE(MS_OS_20_SUBSET_HEADER_FUNCTION), USBD_ITF_VENDOR, 0, U16_TO_U8S_LE(MS_OS_20_DESC_LEN-0x0A-0x08),

// MS OS 2.0 Compatible ID descriptor: length, type, compatible ID, sub compatible ID
U16_TO_U8S_LE(0x0014), U16_TO_U8S_LE(MS_OS_20_FEATURE_COMPATBLE_ID), 'W', 'I', 'N', 'U', 'S', 'B', 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // sub-compatible

// MS OS 2.0 Registry property descriptor: length, type
U16_TO_U8S_LE(MS_OS_20_DESC_LEN-0x0A-0x08-0x08-0x14), U16_TO_U8S_LE(MS_OS_20_FEATURE_REG_PROPERTY),
U16_TO_U8S_LE(0x0007), U16_TO_U8S_LE(0x002A), // wPropertyDataType, wPropertyNameLength and PropertyName "DeviceInterfaceGUIDs\0" in UTF-16
'D', 0x00, 'e', 0x00, 'v', 0x00, 'i', 0x00, 'c', 0x00, 'e', 0x00, 'I', 0x00, 'n', 0x00, 't', 0x00, 'e', 0x00,
'r', 0x00, 'f', 0x00, 'a', 0x00, 'c', 0x00, 'e', 0x00, 'G', 0x00, 'U', 0x00, 'I', 0x00, 'D', 0x00, 's', 0x00, 0x00, 0x00,
U16_TO_U8S_LE(0x0050), // wPropertyDataLength
//bPropertyData: “{975F44D9-0D08-43FD-8B3E-127CA8AFFF9D}”.
'{', 0x00, '9', 0x00, '7', 0x00, '5', 0x00, 'F', 0x00, '4', 0x00, '4', 0x00, 'D', 0x00, '9', 0x00, '-', 0x00,
'0', 0x00, 'D', 0x00, '0', 0x00, '8', 0x00, '-', 0x00, '4', 0x00, '3', 0x00, 'F', 0x00, 'D', 0x00, '-', 0x00,
'8', 0x00, 'B', 0x00, '3', 0x00, 'E', 0x00, '-', 0x00, '1', 0x00, '2', 0x00, '7', 0x00, 'C', 0x00, 'A', 0x00,
'8', 0x00, 'A', 0x00, 'F', 0x00, 'F', 0x00, 'F', 0x00, '9', 0x00, 'D', 0x00, '}', 0x00, 0x00, 0x00, 0x00, 0x00
};

TU_VERIFY_STATIC(sizeof(desc_ms_os_20) == MS_OS_20_DESC_LEN, "Incorrect size");
12 changes: 12 additions & 0 deletions ports/rp2/tusb_port.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef TUSB_PORT_H_
#define TUSB_PORT_H_

enum {
VENDOR_REQUEST_WEBUSB = 1,
VENDOR_REQUEST_MICROSOFT = 2,
VENDOR_REQUEST_WEBUSB_CONNECTION = 34
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have a link for where this vendor request number is from? I can find the other two but seemingly not this one.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got this number from the tinyusb repo in the webusb example.
This number is then used in the browser code to request to connect and disconnect with the device

};

extern const uint8_t desc_ms_os_20[];

#endif /* TUSB_PORT_H_ */
Loading