Skip to content

Commit c48d2eb

Browse files
authored
Merge pull request #2630 from HiFiPhile/dcd_race
2 parents ad3c49c + 6fb6602 commit c48d2eb

File tree

5 files changed

+30
-7
lines changed

5 files changed

+30
-7
lines changed

hw/bsp/stm32f7/family.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ ifeq ($(PORT), 1)
2424
$(info "Using OTG_HS in FullSpeed mode")
2525
endif
2626
else
27+
CFLAGS += -DBOARD_TUD_MAX_SPEED=OPT_MODE_FULL_SPEED
2728
$(info "Using OTG_FS")
2829
endif
2930

src/class/cdc/cdc_device.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ static tud_cdc_configure_fifo_t _cdcd_fifo_cfg;
8383

8484
static bool _prep_out_transaction (cdcd_interface_t* p_cdc) {
8585
uint8_t const rhport = 0;
86+
87+
// Skip if usb is not ready yet
88+
TU_VERIFY(tud_ready() && p_cdc->ep_out);
89+
8690
uint16_t available = tu_fifo_remaining(&p_cdc->rx_ff);
8791

8892
// Prepare for incoming data but only allow what we can store in the ring buffer.
@@ -116,6 +120,10 @@ bool tud_cdc_configure_fifo(tud_cdc_configure_fifo_t const* cfg) {
116120
return true;
117121
}
118122

123+
bool tud_cdc_n_ready(uint8_t itf) {
124+
return tud_ready() && _cdcd_itf[itf].ep_in != 0 && _cdcd_itf[itf].ep_out != 0;
125+
}
126+
119127
bool tud_cdc_n_connected(uint8_t itf) {
120128
// DTR (bit 0) active is considered as connected
121129
return tud_ready() && tu_bit_test(_cdcd_itf[itf].line_state, 0);

src/class/cdc/cdc_device.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ bool tud_cdc_configure_fifo(tud_cdc_configure_fifo_t const* cfg);
6161
// Application API (Multiple Ports) i.e. CFG_TUD_CDC > 1
6262
//--------------------------------------------------------------------+
6363

64+
// Check if interface is ready
65+
bool tud_cdc_n_ready(uint8_t itf);
66+
6467
// Check if terminal is connected to this port
6568
bool tud_cdc_n_connected(uint8_t itf);
6669

@@ -116,6 +119,11 @@ bool tud_cdc_n_write_clear(uint8_t itf);
116119
//--------------------------------------------------------------------+
117120
// Application API (Single Port)
118121
//--------------------------------------------------------------------+
122+
123+
TU_ATTR_ALWAYS_INLINE static inline bool tud_cdc_ready(void) {
124+
return tud_cdc_n_ready(0);
125+
}
126+
119127
TU_ATTR_ALWAYS_INLINE static inline bool tud_cdc_connected(void) {
120128
return tud_cdc_n_connected(0);
121129
}

src/common/tusb_verify.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@
5656
* #define TU_VERIFY(cond) if(cond) return false;
5757
* #define TU_VERIFY(cond,ret) if(cond) return ret;
5858
*
59-
* #define TU_ASSERT(cond) if(cond) {_MESS_FAILED(); TU_BREAKPOINT(), return false;}
60-
* #define TU_ASSERT(cond,ret) if(cond) {_MESS_FAILED(); TU_BREAKPOINT(), return ret;}
59+
* #define TU_ASSERT(cond) if(cond) {TU_MESS_FAILED(); TU_BREAKPOINT(), return false;}
60+
* #define TU_ASSERT(cond,ret) if(cond) {TU_MESS_FAILED(); TU_BREAKPOINT(), return ret;}
6161
*------------------------------------------------------------------*/
6262

6363
#ifdef __cplusplus
@@ -70,9 +70,9 @@
7070

7171
#if CFG_TUSB_DEBUG
7272
#include <stdio.h>
73-
#define _MESS_FAILED() tu_printf("%s %d: ASSERT FAILED\r\n", __func__, __LINE__)
73+
#define TU_MESS_FAILED() tu_printf("%s %d: ASSERT FAILED\r\n", __func__, __LINE__)
7474
#else
75-
#define _MESS_FAILED() do {} while (0)
75+
#define TU_MESS_FAILED() do {} while (0)
7676
#endif
7777

7878
// Halt CPU (breakpoint) when hitting error, only apply for Cortex M3, M4, M7, M33. M55
@@ -119,7 +119,7 @@
119119
*------------------------------------------------------------------*/
120120
#define TU_ASSERT_DEFINE(_cond, _ret) \
121121
do { \
122-
if ( !(_cond) ) { _MESS_FAILED(); TU_BREAKPOINT(); return _ret; } \
122+
if ( !(_cond) ) { TU_MESS_FAILED(); TU_BREAKPOINT(); return _ret; } \
123123
} while(0)
124124

125125
#define TU_ASSERT_1ARGS(_cond) TU_ASSERT_DEFINE(_cond, false)

src/device/usbd.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -747,17 +747,23 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const
747747
_usbd_dev.speed = speed; // restore speed
748748
}
749749

750+
_usbd_dev.cfg_num = cfg_num;
751+
750752
// Handle the new configuration and execute the corresponding callback
751753
if ( cfg_num ) {
752754
// switch to new configuration if not zero
753-
TU_ASSERT( process_set_config(rhport, cfg_num) );
755+
if (!process_set_config(rhport, cfg_num)) {
756+
TU_MESS_FAILED();
757+
TU_BREAKPOINT();
758+
_usbd_dev.cfg_num = 0;
759+
return false;
760+
}
754761
if ( tud_mount_cb ) tud_mount_cb();
755762
} else {
756763
if ( tud_umount_cb ) tud_umount_cb();
757764
}
758765
}
759766

760-
_usbd_dev.cfg_num = cfg_num;
761767
tud_control_status(rhport, p_request);
762768
}
763769
break;

0 commit comments

Comments
 (0)