summaryrefslogtreecommitdiff
path: root/examples/device/webusb_serial
diff options
context:
space:
mode:
Diffstat (limited to 'examples/device/webusb_serial')
-rw-r--r--examples/device/webusb_serial/CMakeLists.txt3
-rw-r--r--examples/device/webusb_serial/src/main.c101
2 files changed, 64 insertions, 40 deletions
diff --git a/examples/device/webusb_serial/CMakeLists.txt b/examples/device/webusb_serial/CMakeLists.txt
index f4bf75c26..5e3ec0909 100644
--- a/examples/device/webusb_serial/CMakeLists.txt
+++ b/examples/device/webusb_serial/CMakeLists.txt
@@ -33,9 +33,6 @@ if(FAMILY STREQUAL "rp2040")
CFG_TUSB_OS=OPT_OS_PICO
)
- target_link_libraries(${PROJECT} pico_stdlib pico_fix_rp2040_usb_device_enumeration)
- pico_add_extra_outputs(${PROJECT})
-
else()
message(FATAL_ERROR "Invalid FAMILY specified")
endif()
diff --git a/examples/device/webusb_serial/src/main.c b/examples/device/webusb_serial/src/main.c
index 143a73374..c85b3cc9a 100644
--- a/examples/device/webusb_serial/src/main.c
+++ b/examples/device/webusb_serial/src/main.c
@@ -23,6 +23,26 @@
*
*/
+/* This example 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.
+ *
+ * - On Linux/macOS, udev permission may need to be updated by
+ * - copying '/examples/device/99-tinyusb.rules' file to /etc/udev/rules.d/ then
+ * - run 'sudo udevadm control --reload-rules && sudo udevadm trigger'
+ */
+
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
@@ -149,52 +169,59 @@ void tud_resume_cb(void)
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;
+ if (stage != CONTROL_STAGE_SETUP) return true;
- if (request->bmRequestType_bit.type == 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*) &desc_url, desc_url.bLength);
+ 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*) &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);
- case VENDOR_REQUEST_MICROSOFT:
- if ( request->wIndex == 7 )
+ return tud_control_xfer(rhport, request, (void*) desc_ms_os_20, total_len);
+ }else
+ {
+ return false;
+ }
+
+ default: break;
+ }
+ break;
+
+ case TUSB_REQ_TYPE_CLASS:
+ if (request->bRequest == 0x22)
+ {
+ // Webserial simulate the CDC_REQUEST_SET_CONTROL_LINE_STATE (0x22) to connect and disconnect.
+ web_serial_connected = (request->wValue != 0);
+
+ // Always lit LED if connected
+ if ( web_serial_connected )
{
- // Get Microsoft OS 2.0 compatible descriptor
- uint16_t total_len;
- memcpy(&total_len, desc_ms_os_20+8, 2);
+ board_led_write(true);
+ blink_interval_ms = BLINK_ALWAYS_ON;
- return tud_control_xfer(rhport, request, (void*) desc_ms_os_20, total_len);
+ tud_vendor_write_str("\r\nTinyUSB WebUSB device example\r\n");
}else
{
- return false;
+ blink_interval_ms = BLINK_MOUNTED;
}
- default:
- return false;
- }
- } else if (
- request->bmRequestType_bit.type == TUSB_REQ_TYPE_CLASS &&
- request->bRequest == 0x22) {
- // Webserial simulate the CDC_REQUEST_SET_CONTROL_LINE_STATE (0x22) to
- // connect and disconnect.
- web_serial_connected = (request->wValue != 0);
-
- // Always lit LED if connected
- if ( web_serial_connected ) {
- board_led_write(true);
- blink_interval_ms = BLINK_ALWAYS_ON;
-
- tud_vendor_write_str("\r\nTinyUSB WebUSB device example\r\n");
- }else
- {
- blink_interval_ms = BLINK_MOUNTED;
- }
+ // response with status OK
+ return tud_control_status(rhport, request);
+ }
+ break;
- // response with status OK
- return tud_control_status(rhport, request);
+ default: break;
}
// stall unknown request