@@ -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)
163157bool 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
247231void 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 );
0 commit comments