@@ -73,8 +73,7 @@ static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED;
73
73
74
74
#define URL "example.tinyusb.org/webusb-serial/index.html"
75
75
76
- const tusb_desc_webusb_url_t desc_url =
77
- {
76
+ const tusb_desc_webusb_url_t desc_url = {
78
77
.bLength = 3 + sizeof (URL ) - 1 ,
79
78
.bDescriptorType = 3 , // WEBUSB URL type
80
79
.bScheme = 1 , // 0: http, 1: https
@@ -86,11 +85,9 @@ static bool web_serial_connected = false;
86
85
//------------- prototypes -------------//
87
86
void led_blinking_task (void );
88
87
void cdc_task (void );
89
- void webserial_task (void );
90
88
91
89
/*------------- MAIN -------------*/
92
- int main (void )
93
- {
90
+ int main (void ) {
94
91
board_init ();
95
92
96
93
// init device stack on configured roothub port
@@ -100,33 +97,28 @@ int main(void)
100
97
board_init_after_tusb ();
101
98
}
102
99
103
- while (1 )
104
- {
100
+ while (1 ) {
105
101
tud_task (); // tinyusb device task
106
102
cdc_task ();
107
- webserial_task ();
108
103
led_blinking_task ();
109
104
}
110
105
}
111
106
112
107
// send characters to both CDC and WebUSB
113
- void echo_all (uint8_t buf [], uint32_t count )
114
- {
108
+ void echo_all (const uint8_t buf [], uint32_t count ) {
115
109
// echo to web serial
116
- if ( web_serial_connected )
117
- {
110
+ if (web_serial_connected ) {
118
111
tud_vendor_write (buf , count );
119
112
tud_vendor_write_flush ();
120
113
}
121
114
122
115
// echo to cdc
123
- if ( tud_cdc_connected () )
124
- {
125
- for (uint32_t i = 0 ; i < count ; i ++ )
126
- {
116
+ if (tud_cdc_connected ()) {
117
+ for (uint32_t i = 0 ; i < count ; i ++ ) {
127
118
tud_cdc_write_char (buf [i ]);
128
-
129
- if ( buf [i ] == '\r' ) tud_cdc_write_char ('\n' );
119
+ if (buf [i ] == '\r' ) {
120
+ tud_cdc_write_char ('\n' );
121
+ }
130
122
}
131
123
tud_cdc_write_flush ();
132
124
}
@@ -137,29 +129,25 @@ void echo_all(uint8_t buf[], uint32_t count)
137
129
//--------------------------------------------------------------------+
138
130
139
131
// Invoked when device is mounted
140
- void tud_mount_cb (void )
141
- {
132
+ void tud_mount_cb (void ) {
142
133
blink_interval_ms = BLINK_MOUNTED ;
143
134
}
144
135
145
136
// Invoked when device is unmounted
146
- void tud_umount_cb (void )
147
- {
137
+ void tud_umount_cb (void ) {
148
138
blink_interval_ms = BLINK_NOT_MOUNTED ;
149
139
}
150
140
151
141
// Invoked when usb bus is suspended
152
142
// remote_wakeup_en : if host allow us to perform remote wakeup
153
143
// Within 7ms, device must draw an average of current less than 2.5 mA from bus
154
- void tud_suspend_cb (bool remote_wakeup_en )
155
- {
156
- (void ) remote_wakeup_en ;
144
+ void tud_suspend_cb (bool remote_wakeup_en ) {
145
+ (void )remote_wakeup_en ;
157
146
blink_interval_ms = BLINK_SUSPENDED ;
158
147
}
159
148
160
149
// Invoked when usb bus is resumed
161
- void tud_resume_cb (void )
162
- {
150
+ void tud_resume_cb (void ) {
163
151
blink_interval_ms = tud_mounted () ? BLINK_MOUNTED : BLINK_NOT_MOUNTED ;
164
152
}
165
153
@@ -170,61 +158,53 @@ void tud_resume_cb(void)
170
158
// Invoked when a control transfer occurred on an interface of this class
171
159
// Driver response accordingly to the request and the transfer stage (setup/data/ack)
172
160
// return false to stall control endpoint (e.g unsupported request)
173
- bool tud_vendor_control_xfer_cb (uint8_t rhport , uint8_t stage , tusb_control_request_t const * request )
174
- {
161
+ bool tud_vendor_control_xfer_cb (uint8_t rhport , uint8_t stage , tusb_control_request_t const * request ) {
175
162
// nothing to with DATA & ACK stage
176
163
if (stage != CONTROL_STAGE_SETUP ) return true;
177
164
178
- switch (request -> bmRequestType_bit .type )
179
- {
165
+ switch (request -> bmRequestType_bit .type ) {
180
166
case TUSB_REQ_TYPE_VENDOR :
181
- switch (request -> bRequest )
182
- {
167
+ switch (request -> bRequest ) {
183
168
case VENDOR_REQUEST_WEBUSB :
184
169
// match vendor request in BOS descriptor
185
170
// Get landing page url
186
- return tud_control_xfer (rhport , request , (void * )(uintptr_t ) & desc_url , desc_url .bLength );
171
+ return tud_control_xfer (rhport , request , (void * )(uintptr_t )& desc_url , desc_url .bLength );
187
172
188
173
case VENDOR_REQUEST_MICROSOFT :
189
- if ( request -> wIndex == 7 )
190
- {
174
+ if (request -> wIndex == 7 ) {
191
175
// Get Microsoft OS 2.0 compatible descriptor
192
176
uint16_t total_len ;
193
- memcpy (& total_len , desc_ms_os_20 + 8 , 2 );
177
+ memcpy (& total_len , desc_ms_os_20 + 8 , 2 );
194
178
195
- return tud_control_xfer (rhport , request , (void * )(uintptr_t ) desc_ms_os_20 , total_len );
196
- }else
197
- {
179
+ return tud_control_xfer (rhport , request , (void * )(uintptr_t )desc_ms_os_20 , total_len );
180
+ } else {
198
181
return false;
199
182
}
200
183
201
184
default : break ;
202
185
}
203
- break ;
186
+ break ;
204
187
205
188
case TUSB_REQ_TYPE_CLASS :
206
- if (request -> bRequest == 0x22 )
207
- {
189
+ if (request -> bRequest == 0x22 ) {
208
190
// Webserial simulate the CDC_REQUEST_SET_CONTROL_LINE_STATE (0x22) to connect and disconnect.
209
191
web_serial_connected = (request -> wValue != 0 );
210
192
211
193
// Always lit LED if connected
212
- if ( web_serial_connected )
213
- {
194
+ if (web_serial_connected ) {
214
195
board_led_write (true);
215
196
blink_interval_ms = BLINK_ALWAYS_ON ;
216
197
217
198
tud_vendor_write_str ("\r\nWebUSB interface connected\r\n" );
218
199
tud_vendor_write_flush ();
219
- }else
220
- {
200
+ } else {
221
201
blink_interval_ms = BLINK_MOUNTED ;
222
202
}
223
203
224
204
// response with status OK
225
205
return tud_control_status (rhport , request );
226
206
}
227
- break ;
207
+ break ;
228
208
229
209
default : break ;
230
210
}
@@ -233,32 +213,24 @@ bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_requ
233
213
return false;
234
214
}
235
215
236
- void webserial_task (void )
237
- {
238
- if ( web_serial_connected )
239
- {
240
- if ( tud_vendor_available () )
241
- {
242
- uint8_t buf [64 ];
243
- uint32_t count = tud_vendor_read (buf , sizeof (buf ));
216
+ void tud_vendor_rx_cb (uint8_t itf , uint8_t const * buffer , uint16_t bufsize ) {
217
+ (void ) itf ;
244
218
245
- // echo back to both web serial and cdc
246
- echo_all (buf , count );
247
- }
248
- }
249
- }
219
+ echo_all (buffer , bufsize );
250
220
221
+ // if using RX buffered is enabled, we need to flush the buffer to make room for new data
222
+ #if CFG_TUD_VENDOR_RX_BUFSIZE > 0
223
+ tud_vendor_read_flush ();
224
+ #endif
225
+ }
251
226
252
227
//--------------------------------------------------------------------+
253
228
// USB CDC
254
229
//--------------------------------------------------------------------+
255
- void cdc_task (void )
256
- {
257
- if ( tud_cdc_connected () )
258
- {
230
+ void cdc_task (void ) {
231
+ if (tud_cdc_connected ()) {
259
232
// connected and there are data available
260
- if ( tud_cdc_available () )
261
- {
233
+ if (tud_cdc_available ()) {
262
234
uint8_t buf [64 ];
263
235
264
236
uint32_t count = tud_cdc_read (buf , sizeof (buf ));
@@ -270,34 +242,30 @@ void cdc_task(void)
270
242
}
271
243
272
244
// Invoked when cdc when line state changed e.g connected/disconnected
273
- void tud_cdc_line_state_cb (uint8_t itf , bool dtr , bool rts )
274
- {
275
- (void ) itf ;
245
+ void tud_cdc_line_state_cb (uint8_t itf , bool dtr , bool rts ) {
246
+ (void )itf ;
276
247
277
248
// connected
278
- if ( dtr && rts )
279
- {
249
+ if (dtr && rts ) {
280
250
// print initial message when connected
281
251
tud_cdc_write_str ("\r\nTinyUSB WebUSB device example\r\n" );
282
252
}
283
253
}
284
254
285
255
// Invoked when CDC interface received data from host
286
- void tud_cdc_rx_cb (uint8_t itf )
287
- {
288
- (void ) itf ;
256
+ void tud_cdc_rx_cb (uint8_t itf ) {
257
+ (void )itf ;
289
258
}
290
259
291
260
//--------------------------------------------------------------------+
292
261
// BLINKING TASK
293
262
//--------------------------------------------------------------------+
294
- void led_blinking_task (void )
295
- {
263
+ void led_blinking_task (void ) {
296
264
static uint32_t start_ms = 0 ;
297
265
static bool led_state = false;
298
266
299
267
// Blink every interval ms
300
- if ( board_millis () - start_ms < blink_interval_ms ) return ; // not enough time
268
+ if (board_millis () - start_ms < blink_interval_ms ) return ; // not enough time
301
269
start_ms += blink_interval_ms ;
302
270
303
271
board_led_write (led_state );
0 commit comments