Skip to content

Commit 2ef7894

Browse files
committed
Merge branch 'master' into fork/verylowfreq/pr-ch32v-usbfs-host
2 parents 21120eb + 4b107a2 commit 2ef7894

File tree

9 files changed

+360
-421
lines changed

9 files changed

+360
-421
lines changed

.idea/cmake.xml

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/device/webusb_serial/src/main.c

Lines changed: 46 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED;
7373

7474
#define URL "example.tinyusb.org/webusb-serial/index.html"
7575

76-
const tusb_desc_webusb_url_t desc_url =
77-
{
76+
const tusb_desc_webusb_url_t desc_url = {
7877
.bLength = 3 + sizeof(URL) - 1,
7978
.bDescriptorType = 3, // WEBUSB URL type
8079
.bScheme = 1, // 0: http, 1: https
@@ -86,11 +85,9 @@ static bool web_serial_connected = false;
8685
//------------- prototypes -------------//
8786
void led_blinking_task(void);
8887
void cdc_task(void);
89-
void webserial_task(void);
9088

9189
/*------------- MAIN -------------*/
92-
int main(void)
93-
{
90+
int main(void) {
9491
board_init();
9592

9693
// init device stack on configured roothub port
@@ -100,33 +97,28 @@ int main(void)
10097
board_init_after_tusb();
10198
}
10299

103-
while (1)
104-
{
100+
while (1) {
105101
tud_task(); // tinyusb device task
106102
cdc_task();
107-
webserial_task();
108103
led_blinking_task();
109104
}
110105
}
111106

112107
// 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) {
115109
// echo to web serial
116-
if ( web_serial_connected )
117-
{
110+
if (web_serial_connected) {
118111
tud_vendor_write(buf, count);
119112
tud_vendor_write_flush();
120113
}
121114

122115
// 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++) {
127118
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+
}
130122
}
131123
tud_cdc_write_flush();
132124
}
@@ -137,29 +129,25 @@ void echo_all(uint8_t buf[], uint32_t count)
137129
//--------------------------------------------------------------------+
138130

139131
// Invoked when device is mounted
140-
void tud_mount_cb(void)
141-
{
132+
void tud_mount_cb(void) {
142133
blink_interval_ms = BLINK_MOUNTED;
143134
}
144135

145136
// Invoked when device is unmounted
146-
void tud_umount_cb(void)
147-
{
137+
void tud_umount_cb(void) {
148138
blink_interval_ms = BLINK_NOT_MOUNTED;
149139
}
150140

151141
// Invoked when usb bus is suspended
152142
// remote_wakeup_en : if host allow us to perform remote wakeup
153143
// 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;
157146
blink_interval_ms = BLINK_SUSPENDED;
158147
}
159148

160149
// Invoked when usb bus is resumed
161-
void tud_resume_cb(void)
162-
{
150+
void tud_resume_cb(void) {
163151
blink_interval_ms = tud_mounted() ? BLINK_MOUNTED : BLINK_NOT_MOUNTED;
164152
}
165153

@@ -170,61 +158,53 @@ void tud_resume_cb(void)
170158
// Invoked when a control transfer occurred on an interface of this class
171159
// Driver response accordingly to the request and the transfer stage (setup/data/ack)
172160
// 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) {
175162
// nothing to with DATA & ACK stage
176163
if (stage != CONTROL_STAGE_SETUP) return true;
177164

178-
switch (request->bmRequestType_bit.type)
179-
{
165+
switch (request->bmRequestType_bit.type) {
180166
case TUSB_REQ_TYPE_VENDOR:
181-
switch (request->bRequest)
182-
{
167+
switch (request->bRequest) {
183168
case VENDOR_REQUEST_WEBUSB:
184169
// match vendor request in BOS descriptor
185170
// 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);
187172

188173
case VENDOR_REQUEST_MICROSOFT:
189-
if ( request->wIndex == 7 )
190-
{
174+
if (request->wIndex == 7) {
191175
// Get Microsoft OS 2.0 compatible descriptor
192176
uint16_t total_len;
193-
memcpy(&total_len, desc_ms_os_20+8, 2);
177+
memcpy(&total_len, desc_ms_os_20 + 8, 2);
194178

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 {
198181
return false;
199182
}
200183

201184
default: break;
202185
}
203-
break;
186+
break;
204187

205188
case TUSB_REQ_TYPE_CLASS:
206-
if (request->bRequest == 0x22)
207-
{
189+
if (request->bRequest == 0x22) {
208190
// Webserial simulate the CDC_REQUEST_SET_CONTROL_LINE_STATE (0x22) to connect and disconnect.
209191
web_serial_connected = (request->wValue != 0);
210192

211193
// Always lit LED if connected
212-
if ( web_serial_connected )
213-
{
194+
if (web_serial_connected) {
214195
board_led_write(true);
215196
blink_interval_ms = BLINK_ALWAYS_ON;
216197

217198
tud_vendor_write_str("\r\nWebUSB interface connected\r\n");
218199
tud_vendor_write_flush();
219-
}else
220-
{
200+
} else {
221201
blink_interval_ms = BLINK_MOUNTED;
222202
}
223203

224204
// response with status OK
225205
return tud_control_status(rhport, request);
226206
}
227-
break;
207+
break;
228208

229209
default: break;
230210
}
@@ -233,32 +213,24 @@ bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_requ
233213
return false;
234214
}
235215

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;
244218

245-
// echo back to both web serial and cdc
246-
echo_all(buf, count);
247-
}
248-
}
249-
}
219+
echo_all(buffer, bufsize);
250220

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+
}
251226

252227
//--------------------------------------------------------------------+
253228
// USB CDC
254229
//--------------------------------------------------------------------+
255-
void cdc_task(void)
256-
{
257-
if ( tud_cdc_connected() )
258-
{
230+
void cdc_task(void) {
231+
if (tud_cdc_connected()) {
259232
// connected and there are data available
260-
if ( tud_cdc_available() )
261-
{
233+
if (tud_cdc_available()) {
262234
uint8_t buf[64];
263235

264236
uint32_t count = tud_cdc_read(buf, sizeof(buf));
@@ -270,34 +242,30 @@ void cdc_task(void)
270242
}
271243

272244
// 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;
276247

277248
// connected
278-
if ( dtr && rts )
279-
{
249+
if (dtr && rts) {
280250
// print initial message when connected
281251
tud_cdc_write_str("\r\nTinyUSB WebUSB device example\r\n");
282252
}
283253
}
284254

285255
// 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;
289258
}
290259

291260
//--------------------------------------------------------------------+
292261
// BLINKING TASK
293262
//--------------------------------------------------------------------+
294-
void led_blinking_task(void)
295-
{
263+
void led_blinking_task(void) {
296264
static uint32_t start_ms = 0;
297265
static bool led_state = false;
298266

299267
// 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
301269
start_ms += blink_interval_ms;
302270

303271
board_led_write(led_state);

examples/device/webusb_serial/src/tusb_config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@
102102
#define CFG_TUD_CDC_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
103103

104104
// Vendor FIFO size of TX and RX
105-
// If not configured vendor endpoints will not be buffered
105+
// If zero: vendor endpoints will not be buffered
106106
#define CFG_TUD_VENDOR_RX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
107107
#define CFG_TUD_VENDOR_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
108108

src/class/cdc/cdc_host.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -341,14 +341,14 @@ uint32_t tuh_cdc_write(uint8_t idx, void const* buffer, uint32_t bufsize) {
341341
cdch_interface_t* p_cdc = get_itf(idx);
342342
TU_VERIFY(p_cdc);
343343

344-
return tu_edpt_stream_write(&p_cdc->stream.tx, buffer, bufsize);
344+
return tu_edpt_stream_write(p_cdc->daddr, &p_cdc->stream.tx, buffer, bufsize);
345345
}
346346

347347
uint32_t tuh_cdc_write_flush(uint8_t idx) {
348348
cdch_interface_t* p_cdc = get_itf(idx);
349349
TU_VERIFY(p_cdc);
350350

351-
return tu_edpt_stream_write_xfer(&p_cdc->stream.tx);
351+
return tu_edpt_stream_write_xfer(p_cdc->daddr, &p_cdc->stream.tx);
352352
}
353353

354354
bool tuh_cdc_write_clear(uint8_t idx) {
@@ -362,7 +362,7 @@ uint32_t tuh_cdc_write_available(uint8_t idx) {
362362
cdch_interface_t* p_cdc = get_itf(idx);
363363
TU_VERIFY(p_cdc);
364364

365-
return tu_edpt_stream_write_available(&p_cdc->stream.tx);
365+
return tu_edpt_stream_write_available(p_cdc->daddr, &p_cdc->stream.tx);
366366
}
367367

368368
//--------------------------------------------------------------------+
@@ -373,7 +373,7 @@ uint32_t tuh_cdc_read (uint8_t idx, void* buffer, uint32_t bufsize) {
373373
cdch_interface_t* p_cdc = get_itf(idx);
374374
TU_VERIFY(p_cdc);
375375

376-
return tu_edpt_stream_read(&p_cdc->stream.rx, buffer, bufsize);
376+
return tu_edpt_stream_read(p_cdc->daddr, &p_cdc->stream.rx, buffer, bufsize);
377377
}
378378

379379
uint32_t tuh_cdc_read_available(uint8_t idx) {
@@ -395,7 +395,7 @@ bool tuh_cdc_read_clear (uint8_t idx) {
395395
TU_VERIFY(p_cdc);
396396

397397
bool ret = tu_edpt_stream_clear(&p_cdc->stream.rx);
398-
tu_edpt_stream_read_xfer(&p_cdc->stream.rx);
398+
tu_edpt_stream_read_xfer(p_cdc->daddr, &p_cdc->stream.rx);
399399
return ret;
400400
}
401401

@@ -677,10 +677,10 @@ bool cdch_xfer_cb(uint8_t daddr, uint8_t ep_addr, xfer_result_t event, uint32_t
677677
// invoke tx complete callback to possibly refill tx fifo
678678
if (tuh_cdc_tx_complete_cb) tuh_cdc_tx_complete_cb(idx);
679679

680-
if ( 0 == tu_edpt_stream_write_xfer(&p_cdc->stream.tx) ) {
680+
if ( 0 == tu_edpt_stream_write_xfer(daddr, &p_cdc->stream.tx) ) {
681681
// If there is no data left, a ZLP should be sent if:
682682
// - xferred_bytes is multiple of EP Packet size and not zero
683-
tu_edpt_stream_write_zlp_if_needed(&p_cdc->stream.tx, xferred_bytes);
683+
tu_edpt_stream_write_zlp_if_needed(daddr, &p_cdc->stream.tx, xferred_bytes);
684684
}
685685
} else if ( ep_addr == p_cdc->stream.rx.ep_addr ) {
686686
#if CFG_TUH_CDC_FTDI
@@ -698,7 +698,7 @@ bool cdch_xfer_cb(uint8_t daddr, uint8_t ep_addr, xfer_result_t event, uint32_t
698698
if (tuh_cdc_rx_cb) tuh_cdc_rx_cb(idx);
699699

700700
// prepare for next transfer if needed
701-
tu_edpt_stream_read_xfer(&p_cdc->stream.rx);
701+
tu_edpt_stream_read_xfer(daddr, &p_cdc->stream.rx);
702702
}else if ( ep_addr == p_cdc->ep_notif ) {
703703
// TODO handle notification endpoint
704704
}else {
@@ -719,9 +719,9 @@ static bool open_ep_stream_pair(cdch_interface_t* p_cdc, tusb_desc_endpoint_t co
719719
TU_ASSERT(tuh_edpt_open(p_cdc->daddr, desc_ep));
720720

721721
if (tu_edpt_dir(desc_ep->bEndpointAddress) == TUSB_DIR_IN) {
722-
tu_edpt_stream_open(&p_cdc->stream.rx, p_cdc->daddr, desc_ep);
722+
tu_edpt_stream_open(&p_cdc->stream.rx, desc_ep);
723723
} else {
724-
tu_edpt_stream_open(&p_cdc->stream.tx, p_cdc->daddr, desc_ep);
724+
tu_edpt_stream_open(&p_cdc->stream.tx, desc_ep);
725725
}
726726

727727
desc_ep = (tusb_desc_endpoint_t const*) tu_desc_next(desc_ep);
@@ -763,7 +763,7 @@ static void set_config_complete(cdch_interface_t * p_cdc, uint8_t idx, uint8_t i
763763
if (tuh_cdc_mount_cb) tuh_cdc_mount_cb(idx);
764764

765765
// Prepare for incoming data
766-
tu_edpt_stream_read_xfer(&p_cdc->stream.rx);
766+
tu_edpt_stream_read_xfer(p_cdc->daddr, &p_cdc->stream.rx);
767767

768768
// notify usbh that driver enumeration is complete
769769
usbh_driver_set_config_complete(p_cdc->daddr, itf_num);

0 commit comments

Comments
 (0)