blob: 2716825f8927703522734ba75893d0fba46346c5 [file] [log] [blame]
Austin Schuh41baf202022-01-01 14:33:40 -08001/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2019 Ha Thach (tinyusb.org)
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 *
24 */
25
26/* This example demonstrates WebUSB as web serial with browser with WebUSB support (e.g Chrome).
27 * After enumerated successfully, browser will pop-up notification
28 * with URL to landing page, click on it to test
29 * - Click "Connect" and select device, When connected the on-board LED will litted up.
30 * - Any charters received from either webusb/Serial will be echo back to webusb and Serial
31 *
32 * Note:
33 * - The WebUSB landing page notification is currently disabled in Chrome
34 * on Windows due to Chromium issue 656702 (https://crbug.com/656702). You have to
35 * go to landing page (below) to test
36 *
37 * - On Windows 7 and prior: You need to use Zadig tool to manually bind the
38 * WebUSB interface with the WinUSB driver for Chrome to access. From windows 8 and 10, this
39 * is done automatically by firmware.
40 *
41 * - On Linux/macOS, udev permission may need to be updated by
42 * - copying '/examples/device/99-tinyusb.rules' file to /etc/udev/rules.d/ then
43 * - run 'sudo udevadm control --reload-rules && sudo udevadm trigger'
44 */
45
46#include <stdlib.h>
47#include <stdio.h>
48#include <string.h>
49
50#include "bsp/board.h"
51#include "tusb.h"
52#include "usb_descriptors.h"
53
54//--------------------------------------------------------------------+
55// MACRO CONSTANT TYPEDEF PROTYPES
56//--------------------------------------------------------------------+
57
58/* Blink pattern
59 * - 250 ms : device not mounted
60 * - 1000 ms : device mounted
61 * - 2500 ms : device is suspended
62 */
63enum {
64 BLINK_NOT_MOUNTED = 250,
65 BLINK_MOUNTED = 1000,
66 BLINK_SUSPENDED = 2500,
67
68 BLINK_ALWAYS_ON = UINT32_MAX,
69 BLINK_ALWAYS_OFF = 0
70};
71
72static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED;
73
74#define URL "example.tinyusb.org/webusb-serial/"
75
76const tusb_desc_webusb_url_t desc_url =
77{
78 .bLength = 3 + sizeof(URL) - 1,
79 .bDescriptorType = 3, // WEBUSB URL type
80 .bScheme = 1, // 0: http, 1: https
81 .url = URL
82};
83
84static bool web_serial_connected = false;
85
86//------------- prototypes -------------//
87void led_blinking_task(void);
88void cdc_task(void);
89void webserial_task(void);
90
91/*------------- MAIN -------------*/
92int main(void)
93{
94 board_init();
95
96 tusb_init();
97
98 while (1)
99 {
100 tud_task(); // tinyusb device task
101 cdc_task();
102 webserial_task();
103 led_blinking_task();
104 }
105
106 return 0;
107}
108
109// send characters to both CDC and WebUSB
110void echo_all(uint8_t buf[], uint32_t count)
111{
112 // echo to web serial
113 if ( web_serial_connected )
114 {
115 tud_vendor_write(buf, count);
116 }
117
118 // echo to cdc
119 if ( tud_cdc_connected() )
120 {
121 for(uint32_t i=0; i<count; i++)
122 {
123 tud_cdc_write_char(buf[i]);
124
125 if ( buf[i] == '\r' ) tud_cdc_write_char('\n');
126 }
127 tud_cdc_write_flush();
128 }
129}
130
131//--------------------------------------------------------------------+
132// Device callbacks
133//--------------------------------------------------------------------+
134
135// Invoked when device is mounted
136void tud_mount_cb(void)
137{
138 blink_interval_ms = BLINK_MOUNTED;
139}
140
141// Invoked when device is unmounted
142void tud_umount_cb(void)
143{
144 blink_interval_ms = BLINK_NOT_MOUNTED;
145}
146
147// Invoked when usb bus is suspended
148// remote_wakeup_en : if host allow us to perform remote wakeup
149// Within 7ms, device must draw an average of current less than 2.5 mA from bus
150void tud_suspend_cb(bool remote_wakeup_en)
151{
152 (void) remote_wakeup_en;
153 blink_interval_ms = BLINK_SUSPENDED;
154}
155
156// Invoked when usb bus is resumed
157void tud_resume_cb(void)
158{
159 blink_interval_ms = BLINK_MOUNTED;
160}
161
162//--------------------------------------------------------------------+
163// WebUSB use vendor class
164//--------------------------------------------------------------------+
165
166// Invoked when a control transfer occurred on an interface of this class
167// Driver response accordingly to the request and the transfer stage (setup/data/ack)
168// return false to stall control endpoint (e.g unsupported request)
169bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request)
170{
171 // nothing to with DATA & ACK stage
172 if (stage != CONTROL_STAGE_SETUP) return true;
173
174 switch (request->bmRequestType_bit.type)
175 {
176 case TUSB_REQ_TYPE_VENDOR:
177 switch (request->bRequest)
178 {
179 case VENDOR_REQUEST_WEBUSB:
180 // match vendor request in BOS descriptor
181 // Get landing page url
182 return tud_control_xfer(rhport, request, (void*)(uintptr_t) &desc_url, desc_url.bLength);
183
184 case VENDOR_REQUEST_MICROSOFT:
185 if ( request->wIndex == 7 )
186 {
187 // Get Microsoft OS 2.0 compatible descriptor
188 uint16_t total_len;
189 memcpy(&total_len, desc_ms_os_20+8, 2);
190
191 return tud_control_xfer(rhport, request, (void*)(uintptr_t) desc_ms_os_20, total_len);
192 }else
193 {
194 return false;
195 }
196
197 default: break;
198 }
199 break;
200
201 case TUSB_REQ_TYPE_CLASS:
202 if (request->bRequest == 0x22)
203 {
204 // Webserial simulate the CDC_REQUEST_SET_CONTROL_LINE_STATE (0x22) to connect and disconnect.
205 web_serial_connected = (request->wValue != 0);
206
207 // Always lit LED if connected
208 if ( web_serial_connected )
209 {
210 board_led_write(true);
211 blink_interval_ms = BLINK_ALWAYS_ON;
212
213 tud_vendor_write_str("\r\nTinyUSB WebUSB device example\r\n");
214 }else
215 {
216 blink_interval_ms = BLINK_MOUNTED;
217 }
218
219 // response with status OK
220 return tud_control_status(rhport, request);
221 }
222 break;
223
224 default: break;
225 }
226
227 // stall unknown request
228 return false;
229}
230
231void webserial_task(void)
232{
233 if ( web_serial_connected )
234 {
235 if ( tud_vendor_available() )
236 {
237 uint8_t buf[64];
238 uint32_t count = tud_vendor_read(buf, sizeof(buf));
239
240 // echo back to both web serial and cdc
241 echo_all(buf, count);
242 }
243 }
244}
245
246
247//--------------------------------------------------------------------+
248// USB CDC
249//--------------------------------------------------------------------+
250void cdc_task(void)
251{
252 if ( tud_cdc_connected() )
253 {
254 // connected and there are data available
255 if ( tud_cdc_available() )
256 {
257 uint8_t buf[64];
258
259 uint32_t count = tud_cdc_read(buf, sizeof(buf));
260
261 // echo back to both web serial and cdc
262 echo_all(buf, count);
263 }
264 }
265}
266
267// Invoked when cdc when line state changed e.g connected/disconnected
268void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts)
269{
270 (void) itf;
271
272 // connected
273 if ( dtr && rts )
274 {
275 // print initial message when connected
276 tud_cdc_write_str("\r\nTinyUSB WebUSB device example\r\n");
277 }
278}
279
280// Invoked when CDC interface received data from host
281void tud_cdc_rx_cb(uint8_t itf)
282{
283 (void) itf;
284}
285
286//--------------------------------------------------------------------+
287// BLINKING TASK
288//--------------------------------------------------------------------+
289void led_blinking_task(void)
290{
291 static uint32_t start_ms = 0;
292 static bool led_state = false;
293
294 // Blink every interval ms
295 if ( board_millis() - start_ms < blink_interval_ms) return; // not enough time
296 start_ms += blink_interval_ms;
297
298 board_led_write(led_state);
299 led_state = 1 - led_state; // toggle
300}