Due to high demand expect some shipping delays at this time, orders may not ship for 1-2 business days.
0

webUSB on RP2040 with TinyUSB
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

webUSB on RP2040 with TinyUSB

by leSuedois on Thu Oct 21, 2021 8:14 am

Hello,
I tried the WebUSB Serial example on a Raspberry PI Pico. All software is up to date, Arduino1.8.16, Board core 1.9.5 and TinyUSB Version 1.5 .
I read and added all changes in the README.md and tried to comment/uncomment TinyUSB_Device_Task() and TinyUSB_Device_FlushCDC() without success.
It works almost but fails at the end. The Chrome Browser says there is no compatible device after telling me in a pop up that a device is avaliable.

My code is the example code for webUSB Serial with the modifications suggested in README.md :
Code: Select all | TOGGLE FULL SIZE
/*********************************************************************
 Adafruit invests time and resources providing this open source code,
 please support Adafruit and open-source hardware by purchasing
 products from Adafruit!

 MIT license, check LICENSE for more information
 Copyright (c) 2019 Ha Thach for Adafruit Industries
 All text above, and the splash screen below must be included in
 any redistribution
*********************************************************************/

/* This sketch demonstrates WebUSB as web serial with browser with WebUSB support (e.g Chrome).
 * After enumerated successfully, Browser will pop-up notification
 * with URL to landing page, click on it to test
 *  - Click "Connect" and select device, When connected the on-board LED will litted up.
 *  - Any charters received from either webusb/Serial will be echo back to webusb and Serial
 * 
 * Note:
 * - The WebUSB landing page notification is currently disabled in Chrome
 * on Windows due to Chromium issue 656702 (https://crbug.com/656702). You have to
 * go to landing page (below) to test
 *
 * - On Windows 7 and prior: You need to use Zadig tool to manually bind the
 * WebUSB interface with the WinUSB driver for Chrome to access. From windows 8 and 10, this
 * is done automatically by firmware.
 */

#include "Adafruit_TinyUSB.h"

// USB WebUSB object
Adafruit_USBD_WebUSB usb_web;

// Landing Page: scheme (0: http, 1: https), url
WEBUSB_URL_DEF(landingPage, 1 /*https*/, "adafruit.github.io/Adafruit_TinyUSB_Arduino/examples/webusb-serial/index.html");

int led_pin = 25;

// the setup function runs once when you press reset or power the board
void setup()
{
#if defined(ARDUINO_ARCH_MBED) && defined(ARDUINO_ARCH_RP2040)
  // Manual begin() is required on core without built-in support for TinyUSB such as mbed rp2040
  TinyUSB_Device_Init(0);
#endif

  pinMode(led_pin, OUTPUT);
  digitalWrite(led_pin, LOW);
 
  usb_web.setLandingPage(&landingPage);
  usb_web.setLineStateCallback(line_state_callback);
  //usb_web.setStringDescriptor("TinyUSB WebUSB");
  usb_web.begin();

  SerialTinyUSB.begin(115200);

  // wait until device mounted
  while( !TinyUSBDevice.mounted() ) delay(1);
  delay(2000);
  SerialTinyUSB.println("TinyUSB WebUSB Serial example");
}

// function to echo to both Serial and WebUSB
void echo_all(char chr)
{
  SerialTinyUSB.write(chr);
  // print extra newline for Serial
  if ( chr == '\r' ) SerialTinyUSB.write('\n');
 
  usb_web.write(chr);
}

void loop()
{
  TinyUSB_Device_Task();
  TinyUSB_Device_FlushCDC();
  // from WebUSB to both Serial & webUSB
  if (usb_web.available()) echo_all(usb_web.read());

  // From Serial to both Serial & webUSB
  if (SerialTinyUSB.available())   echo_all(SerialTinyUSB.read()); 
}

void line_state_callback(bool connected)
{
  digitalWrite(led_pin, connected);

  if ( connected ) usb_web.println("TinyUSB WebUSB Serial example");
}


I know tinyUSB is experimental for this core. But hey, I really like the RP2040 and I want to update my old Project with the Arduino Leonardo!
Any help is appreciated!
Thanks, Simon

leSuedois
 
Posts: 8
Joined: Fri Oct 04, 2019 6:56 am

Re: webUSB on RP2040 with TinyUSB

by mikeysklar on Sat Oct 23, 2021 5:53 pm

Based on issue #75 (closed) from the Adafruit_TinyUSB_Arduino github repo thre is another tinyUSB library that does work with RP2040 based board.

https://github.com/adafruit/Adafruit_Ti ... /issues/75

Give this one a shot and let us know how it goes.

https://github.com/hathach/ti

mikeysklar
 
Posts: 6246
Joined: Mon Aug 01, 2016 8:10 pm

Re: webUSB on RP2040 with TinyUSB

by hathach on Mon Oct 25, 2021 7:09 am

Which core you are using ? The mbed core has its limit, it is best to run this one https://github.com/earlephilhower/arduino-pico

hathach
 
Posts: 1172
Joined: Tue Apr 23, 2013 1:02 am

Re: webUSB on RP2040 with TinyUSB

by leSuedois on Mon Oct 25, 2021 11:54 am

Hello,
I use the latest erlephilhowder pico core and it's included Adafruit TinyUSB library.
Tomorrow I will buy a adafruit board to see if there is a difference with their ROM bootloader.
@mikeysklar I will give a try with the 'original' TinyUSB library as soon as I can!
Since I am a weekend dev it will take some time to get the original lib to work with Arduino..
Thank you for your answers and the stuff you do!
Simon

leSuedois
 
Posts: 8
Joined: Fri Oct 04, 2019 6:56 am

Re: webUSB on RP2040 with TinyUSB

by hathach on Mon Oct 25, 2021 12:13 pm

Can you tell me more about your pc set up
- PC OS (win, mac, linux) and its version
- Your browser and its version. If it is chrome, make sure yo enable the webusb feature.
- what is your dev board

hathach
 
Posts: 1172
Joined: Tue Apr 23, 2013 1:02 am

Re: webUSB on RP2040 with TinyUSB

by leSuedois on Tue Oct 26, 2021 9:52 am

Hello hathach,
I have a 2019 Macbook Air under OSX Mojave, my board is a standard Raspberry PI Pico from a official Reseller.
Arduino, Chrome, etc are all latest versions. The libraries are the latest versions in Arduino Board and Library Manager.
The WebUSB Feature is activated and example works with an old Arduino Leonardo.

Today I bought a Adafruit Feather and I tried it again with the Serial example and it worked!
I don't know but I think something magic happens somewhere...

leSuedois
 
Posts: 8
Joined: Fri Oct 04, 2019 6:56 am

Re: webUSB on RP2040 with TinyUSB

by mikeysklar on Tue Oct 26, 2021 3:07 pm

Your Mac setup works fine with the Adafruit Feather and old Arduino Leonardo but not with the Pico RP2040?

Is the Adafruit Feather the RP2040 based model?

mikeysklar
 
Posts: 6246
Joined: Mon Aug 01, 2016 8:10 pm

Re: webUSB on RP2040 with TinyUSB

by leSuedois on Wed Oct 27, 2021 10:07 am

Sorry I forgot to mention, yes it's the Feather RP2040.
I can't wrap my head around this problem, I tried it once again today. I took a brand new rpi pico, uninstalled and re-installed the RP2040 core in Arduino.
The rpi pico does give the little popup on the screen but still can't connect in Chrome, the Adafruit Feather RP2040 works fine.
Is there a difference between the rpi bootloader and adafruit bootloader?

leSuedois
 
Posts: 8
Joined: Fri Oct 04, 2019 6:56 am

Re: webUSB on RP2040 with TinyUSB

by mikeysklar on Wed Oct 27, 2021 6:26 pm

@leSuedois,

Both the Pico and Feather RP2040's have a bootloader ROM which is permanently installed. We don't have an upgrade or override. As to your question are they the same? I'm not 100% sure, but you could verify at least the version number by using the BOOTSEL+ RESET button and looking for a TXT file that has version info. That would be at least one helpful datapoint.

mikeysklar
 
Posts: 6246
Joined: Mon Aug 01, 2016 8:10 pm

Re: webUSB on RP2040 with TinyUSB

by leSuedois on Thu Oct 28, 2021 2:46 am

Hello everyone,
bad hypothesis from my side, here is whats in the .txt file in both boards:
Code: Select all | TOGGLE FULL SIZE
UF2 Bootloader v2.0
Model: Raspberry Pi RP2
Board-ID: RPI-RP2

Have a nice day!

leSuedois
 
Posts: 8
Joined: Fri Oct 04, 2019 6:56 am

Re: webUSB on RP2040 with TinyUSB

by leSuedois on Thu Oct 28, 2021 3:13 am

OK, my bad. The "Problem" is on the web and not at all on the embedded side.
While in the subway I came across this page (https://wicg.github.io/webusb/) that mentions clearly (point 5 Device Enumeration) that on the web side devices are filtered with PID and VID. Look at line 12-20 in the serial.js file in the example.
Code: Select all | TOGGLE FULL SIZE
serial.requestPort = function() {
    const filters = [
      { 'vendorId': 0x239A }, // Adafruit boards
      { 'vendorId': 0xcafe }, // TinyUSB example
    ];
    return navigator.usb.requestDevice({ 'filters': filters }).then(
      device => new serial.Port(device)
    );
}

RPi Pico Vendor and Product ID are 0x2e8a and 0x000a. If I would be a little line of code on the internet I won't accept either.
Voila!
I give it a try this evening but I think that's it. It explaines the strange behaviour to pop up on the screen but not be seen in the webUSB Serial example.

leSuedois
 
Posts: 8
Joined: Fri Oct 04, 2019 6:56 am

Re: webUSB on RP2040 with TinyUSB

by mikeysklar on Thu Oct 28, 2021 12:18 pm

Wahoo! Congratulations. Good find noticing the vendor ID check that was causing the RPi Pico to not be detected. Well done.

mikeysklar
 
Posts: 6246
Joined: Mon Aug 01, 2016 8:10 pm

Re: webUSB on RP2040 with TinyUSB

by leSuedois on Fri Oct 29, 2021 3:13 am

I confirm that with some brutal vid and pid modification the webUSB Serial example works like a charm on the RPi Pico!
Thanks everybody!

leSuedois
 
Posts: 8
Joined: Fri Oct 04, 2019 6:56 am

Please be positive and constructive with your questions and comments.