Skip to content

Commit 1618c58

Browse files
authored
Merge pull request #3373 from hathach/vendor-dedicated-hwfifo
update Vendor device to omit ep buffer for port with dedicated hwfifo
2 parents 5ef55bf + 6e9e9ce commit 1618c58

File tree

10 files changed

+414
-374
lines changed

10 files changed

+414
-374
lines changed

examples/device/cdc_msc/src/main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ void cdc_task(void) {
124124

125125
if ((btn_prev == 0u) && (btn != 0u)) {
126126
uart_state.dsr ^= 1;
127+
uart_state.dcd ^= 1;
127128
tud_cdc_notify_uart_state(&uart_state);
128129
}
129130
btn_prev = btn;

examples/device/webusb_serial/src/main.c

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ int main(void) {
101101

102102
while (1) {
103103
tud_task(); // tinyusb device task
104-
cdc_task();
104+
tud_cdc_write_flush();
105105
led_blinking_task();
106106
}
107107
}
@@ -116,13 +116,7 @@ static void echo_all(const uint8_t buf[], uint32_t count) {
116116

117117
// echo to cdc
118118
if (tud_cdc_connected()) {
119-
for (uint32_t i = 0; i < count; i++) {
120-
tud_cdc_write_char(buf[i]);
121-
if (buf[i] == '\r') {
122-
tud_cdc_write_char('\n');
123-
}
124-
}
125-
tud_cdc_write_flush();
119+
tud_cdc_write(buf, count);
126120
}
127121
}
128122

@@ -162,7 +156,9 @@ void tud_resume_cb(void) {
162156
// return false to stall control endpoint (e.g unsupported request)
163157
bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const* request) {
164158
// nothing to with DATA & ACK stage
165-
if (stage != CONTROL_STAGE_SETUP) return true;
159+
if (stage != CONTROL_STAGE_SETUP) {
160+
return true;
161+
}
166162

167163
switch (request->bmRequestType_bit.type) {
168164
case TUSB_REQ_TYPE_VENDOR:
@@ -215,33 +211,21 @@ bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_requ
215211
return false;
216212
}
217213

218-
void tud_vendor_rx_cb(uint8_t itf, uint8_t const* buffer, uint16_t bufsize) {
219-
(void) itf;
214+
void tud_vendor_rx_cb(uint8_t idx, const uint8_t *buffer, uint32_t bufsize) {
215+
(void)idx;
216+
(void)buffer;
217+
(void)bufsize;
220218

221-
echo_all(buffer, bufsize);
222-
223-
// if using RX buffered is enabled, we need to flush the buffer to make room for new data
224-
#if CFG_TUD_VENDOR_RX_BUFSIZE > 0
225-
tud_vendor_read_flush();
226-
#endif
219+
while (tud_vendor_available()) {
220+
uint8_t buf[64];
221+
const uint32_t count = tud_vendor_read(buf, sizeof(buf));
222+
echo_all(buf, count);
223+
}
227224
}
228225

229226
//--------------------------------------------------------------------+
230227
// USB CDC
231228
//--------------------------------------------------------------------+
232-
void cdc_task(void) {
233-
if (tud_cdc_connected()) {
234-
// connected and there are data available
235-
if (tud_cdc_available()) {
236-
uint8_t buf[64];
237-
238-
uint32_t count = tud_cdc_read(buf, sizeof(buf));
239-
240-
// echo back to both web serial and cdc
241-
echo_all(buf, count);
242-
}
243-
}
244-
}
245229

246230
// Invoked when cdc when line state changed e.g connected/disconnected
247231
void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) {
@@ -255,8 +239,13 @@ void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) {
255239
}
256240

257241
// Invoked when CDC interface received data from host
258-
void tud_cdc_rx_cb(uint8_t itf) {
259-
(void)itf;
242+
void tud_cdc_rx_cb(uint8_t idx) {
243+
(void)idx;
244+
while (tud_cdc_available()) {
245+
uint8_t buf[64];
246+
const uint32_t count = tud_cdc_read(buf, sizeof(buf));
247+
echo_all(buf, count); // echo back to both web serial and cdc
248+
}
260249
}
261250

262251
//--------------------------------------------------------------------+
@@ -267,7 +256,9 @@ void led_blinking_task(void) {
267256
static bool led_state = false;
268257

269258
// Blink every interval ms
270-
if (board_millis() - start_ms < blink_interval_ms) return; // not enough time
259+
if (board_millis() - start_ms < blink_interval_ms) {
260+
return; // not enough time
261+
}
271262
start_ms += blink_interval_ms;
272263

273264
board_led_write(led_state);

src/class/cdc/cdc_device.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,8 @@ void cdcd_init(void) {
266266
uint8_t *epout_buf = NULL;
267267
uint8_t *epin_buf = NULL;
268268
#else
269-
cdcd_epbuf_t *p_epbuf = &_cdcd_epbuf[i];
270-
uint8_t *epout_buf = p_epbuf->epout;
271-
uint8_t *epin_buf = p_epbuf->epin;
269+
uint8_t *epout_buf = _cdcd_epbuf[i].epout;
270+
uint8_t *epin_buf = _cdcd_epbuf[i].epin;
272271
#endif
273272

274273
tu_edpt_stream_init(&p_cdc->stream.rx, false, false, false, p_cdc->stream.rx_ff_buf, CFG_TUD_CDC_RX_BUFSIZE,
@@ -516,11 +515,9 @@ bool cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_
516515
}
517516

518517
// Data sent to host, we continue to fetch from tx fifo to send.
519-
// Note: This will cause incorrect baudrate set in line coding.
520-
// Though maybe the baudrate is not really important !!!
518+
// Note: This will cause incorrect baudrate set in line coding. Though maybe the baudrate is not really important !
521519
if (ep_addr == stream_tx->ep_addr) {
522-
// invoke transmit callback to possibly refill tx fifo
523-
tud_cdc_tx_complete_cb(itf);
520+
tud_cdc_tx_complete_cb(itf); // invoke callback to possibly refill tx fifo
524521

525522
if (0 == tu_edpt_stream_write_xfer(rhport, stream_tx)) {
526523
// If there is no data left, a ZLP should be sent if needed

0 commit comments

Comments
 (0)