summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHa Thach <[email protected]>2025-11-14 14:23:46 +0700
committerGitHub <[email protected]>2025-11-14 14:23:46 +0700
commita3a5a41be8e22f529a8941c107ec32d48faa6ff2 (patch)
treebc8a4c397b957cd0d86b35e0d0f9ba7eacefcd30
parent8b8f1f80b4f01fc0fc56ad58af9b784e161636b0 (diff)
parentc9a9e94ae554c3d545b5eb795f81fa9216a93e4f (diff)
Merge pull request #3339 from hathach/cdc-edpt-stream
migrate cdc device to edpt stream API
-rw-r--r--.clang-format8
-rw-r--r--examples/device/net_lwip_webserver/src/usb_descriptors.c2
-rw-r--r--examples/dual/host_info_to_device_cdc/src/main.c26
-rw-r--r--examples/dual/host_info_to_device_cdc/src/tusb_config.h2
-rw-r--r--src/class/cdc/cdc_device.c392
-rw-r--r--src/class/cdc/cdc_host.c50
-rw-r--r--src/class/midi/midi_device.c12
-rw-r--r--src/class/midi/midi_host.c48
-rw-r--r--src/class/vendor/vendor_device.c16
-rw-r--r--src/common/tusb_common.h2
-rw-r--r--src/common/tusb_private.h6
-rw-r--r--src/tusb.c10
12 files changed, 264 insertions, 310 deletions
diff --git a/.clang-format b/.clang-format
index c278fec37..f15c26c9e 100644
--- a/.clang-format
+++ b/.clang-format
@@ -1,7 +1,7 @@
---
Language: Cpp
BasedOnStyle: LLVM
-AlignAfterOpenBracket: AlwaysBreak
+AlignAfterOpenBracket: Align
AlignConsecutiveAssignments:
Enabled: true
AcrossEmptyLines: false
@@ -38,6 +38,7 @@ AllowShortEnumsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: Never
AlwaysBreakTemplateDeclarations: Yes
+BinPackArguments: true
BreakBeforeBraces: Custom
BraceWrapping:
AfterCaseLabel: false
@@ -57,11 +58,14 @@ BraceWrapping:
SplitEmptyRecord: true
SplitEmptyNamespace: true
BracedInitializerIndentWidth: 2
+BreakBeforeBinaryOperators: None
BreakConstructorInitializers: AfterColon
BreakConstructorInitializersBeforeComma: false
+ContinuationIndentWidth: 2
ColumnLimit: 120
ConstructorInitializerAllOnOneLineOrOnePerLine: false
Cpp11BracedListStyle: true
+IncludeBlocks: Preserve
IncludeCategories:
- Regex: '^<.*'
Priority: 1
@@ -78,6 +82,8 @@ MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 2
NamespaceIndentation: All
+PenaltyBreakBeforeFirstCallParameter: 1000000
+PenaltyBreakOpenParenthesis: 1000000
QualifierAlignment: Custom
QualifierOrder: ['static', 'const', 'volatile', 'restrict', 'type']
ReflowComments: false
diff --git a/examples/device/net_lwip_webserver/src/usb_descriptors.c b/examples/device/net_lwip_webserver/src/usb_descriptors.c
index b49962d65..c976cb62b 100644
--- a/examples/device/net_lwip_webserver/src/usb_descriptors.c
+++ b/examples/device/net_lwip_webserver/src/usb_descriptors.c
@@ -270,7 +270,7 @@ bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, const tusb_contro
switch (request->bmRequestType_bit.type) {
case TUSB_REQ_TYPE_VENDOR:
- switch (request->bRequest) { //-V2520 //-V2659
+ switch (request->bRequest) {
case 1:
if (request->wIndex == 7) {
// Get Microsoft OS 2.0 compatible descriptor
diff --git a/examples/dual/host_info_to_device_cdc/src/main.c b/examples/dual/host_info_to_device_cdc/src/main.c
index 03a1ac3d8..00d059b66 100644
--- a/examples/dual/host_info_to_device_cdc/src/main.c
+++ b/examples/dual/host_info_to_device_cdc/src/main.c
@@ -69,7 +69,7 @@ enum {
static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED;
-static bool is_print[CFG_TUH_DEVICE_MAX+1] = { 0 };
+static bool is_printable[CFG_TUH_DEVICE_MAX + 1] = {0};
static tusb_desc_device_t descriptor_device[CFG_TUH_DEVICE_MAX+1];
static void print_utf16(uint16_t *temp_buf, size_t buf_len);
@@ -106,6 +106,10 @@ static void usb_device_init(void) {
.speed = TUSB_SPEED_AUTO
};
tusb_init(BOARD_TUD_RHPORT, &dev_init);
+ tud_cdc_configure_t cdc_cfg = TUD_CDC_CONFIGURE_DEFAULT();
+ cdc_cfg.tx_persistent = true;
+ cdc_cfg.tx_overwritabe_if_not_connected = false;
+ tud_cdc_configure(&cdc_cfg);
board_init_after_tusb();
}
@@ -206,17 +210,23 @@ void tud_resume_cb(void) {
}
void cdc_task(void) {
+ static uint32_t connected_ms = 0;
+
if (!tud_cdc_connected()) {
- // delay a bit otherwise we can outpace host's terminal. Linux will set LineState (DTR) then Line Coding.
- // If we send data before Linux's terminal set Line Coding, it can be ignored --> missing data with hardware test loop
- tusb_time_delay_ms_api(20);
+ connected_ms = board_millis();
return;
}
+ // delay a bit otherwise we can outpace host's terminal. Linux will set LineState (DTR) then Line Coding.
+ // If we send data before Linux's terminal set Line Coding, it can be ignored --> missing data with hardware test loop
+ if (board_millis() - connected_ms < 100) {
+ return; // wait for stable connection
+ }
+
for (uint8_t daddr = 1; daddr <= CFG_TUH_DEVICE_MAX; daddr++) {
if (tuh_mounted(daddr)) {
- if (is_print[daddr]) {
- is_print[daddr] = false;
+ if (is_printable[daddr]) {
+ is_printable[daddr] = false;
print_device_info(daddr, &descriptor_device[daddr]);
tud_cdc_write_flush();
}
@@ -283,13 +293,13 @@ void tuh_enum_descriptor_device_cb(uint8_t daddr, tusb_desc_device_t const* desc
void tuh_mount_cb(uint8_t daddr) {
cdc_printf("mounted device %u\r\n", daddr);
tud_cdc_write_flush();
- is_print[daddr] = true;
+ is_printable[daddr] = true;
}
void tuh_umount_cb(uint8_t daddr) {
cdc_printf("unmounted device %u\r\n", daddr);
tud_cdc_write_flush();
- is_print[daddr] = false;
+ is_printable[daddr] = false;
}
//--------------------------------------------------------------------+
diff --git a/examples/dual/host_info_to_device_cdc/src/tusb_config.h b/examples/dual/host_info_to_device_cdc/src/tusb_config.h
index bb47fbf4a..601c27dae 100644
--- a/examples/dual/host_info_to_device_cdc/src/tusb_config.h
+++ b/examples/dual/host_info_to_device_cdc/src/tusb_config.h
@@ -112,7 +112,7 @@
// CDC FIFO size of TX and RX
#define CFG_TUD_CDC_RX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
-#define CFG_TUD_CDC_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
+#define CFG_TUD_CDC_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 256)
// CDC Endpoint transfer buffer size, more is faster
#define CFG_TUD_CDC_EP_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
diff --git a/src/class/cdc/cdc_device.c b/src/class/cdc/cdc_device.c
index b3253b141..babb89952 100644
--- a/src/class/cdc/cdc_device.c
+++ b/src/class/cdc/cdc_device.c
@@ -48,28 +48,23 @@
typedef struct {
uint8_t rhport;
uint8_t itf_num;
- uint8_t ep_in;
- uint8_t ep_out;
-
uint8_t ep_notify;
uint8_t line_state; // Bit 0: DTR, Bit 1: RTS
/*------------- From this point, data is not cleared by bus reset -------------*/
- char wanted_char;
TU_ATTR_ALIGNED(4) cdc_line_coding_t line_coding;
+ char wanted_char;
- // FIFO
- tu_fifo_t rx_ff;
- tu_fifo_t tx_ff;
-
- uint8_t rx_ff_buf[CFG_TUD_CDC_RX_BUFSIZE];
- uint8_t tx_ff_buf[CFG_TUD_CDC_TX_BUFSIZE];
+ struct {
+ tu_edpt_stream_t tx;
+ tu_edpt_stream_t rx;
- OSAL_MUTEX_DEF(rx_ff_mutex);
- OSAL_MUTEX_DEF(tx_ff_mutex);
+ uint8_t tx_ff_buf[CFG_TUD_CDC_TX_BUFSIZE];
+ uint8_t rx_ff_buf[CFG_TUD_CDC_RX_BUFSIZE];
+ } stream;
} cdcd_interface_t;
-#define ITF_MEM_RESET_SIZE offsetof(cdcd_interface_t, wanted_char)
+#define ITF_MEM_RESET_SIZE offsetof(cdcd_interface_t, line_coding)
typedef struct {
TUD_EPBUF_DEF(epout, CFG_TUD_CDC_EP_BUFSIZE);
@@ -81,110 +76,100 @@ typedef struct {
} cdcd_epbuf_t;
//--------------------------------------------------------------------+
-// INTERNAL OBJECT & FUNCTION DECLARATION
-//--------------------------------------------------------------------+
-static cdcd_interface_t _cdcd_itf[CFG_TUD_CDC];
-CFG_TUD_MEM_SECTION static cdcd_epbuf_t _cdcd_epbuf[CFG_TUD_CDC];
-
-static tud_cdc_configure_t _cdcd_cfg = TUD_CDC_CONFIGURE_DEFAULT();
-
-static bool _prep_out_transaction(uint8_t itf) {
- const uint8_t rhport = 0;
- cdcd_interface_t* p_cdc = &_cdcd_itf[itf];
- cdcd_epbuf_t* p_epbuf = &_cdcd_epbuf[itf];
-
- // Skip if usb is not ready yet
- TU_VERIFY(tud_ready() && p_cdc->ep_out);
-
- uint16_t available = tu_fifo_remaining(&p_cdc->rx_ff);
-
- // Prepare for incoming data but only allow what we can store in the ring buffer.
- // TODO Actually we can still carry out the transfer, keeping count of received bytes
- // and slowly move it to the FIFO when read().
- // This pre-check reduces endpoint claiming
- TU_VERIFY(available >= CFG_TUD_CDC_EP_BUFSIZE);
-
- // claim endpoint
- TU_VERIFY(usbd_edpt_claim(p_cdc->rhport, p_cdc->ep_out));
-
- // fifo can be changed before endpoint is claimed
- available = tu_fifo_remaining(&p_cdc->rx_ff);
-
- if (available >= CFG_TUD_CDC_EP_BUFSIZE) {
- return usbd_edpt_xfer(rhport, p_cdc->ep_out, p_epbuf->epout, CFG_TUD_CDC_EP_BUFSIZE);
- } else {
- // Release endpoint since we don't make any transfer
- usbd_edpt_release(p_cdc->rhport, p_cdc->ep_out);
- return false;
- }
-}
-
-//--------------------------------------------------------------------+
// Weak stubs: invoked if no strong implementation is available
//--------------------------------------------------------------------+
TU_ATTR_WEAK void tud_cdc_rx_cb(uint8_t itf) {
- (void) itf;
+ (void)itf;
}
TU_ATTR_WEAK void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char) {
- (void) itf;
- (void) wanted_char;
+ (void)itf;
+ (void)wanted_char;
}
TU_ATTR_WEAK void tud_cdc_tx_complete_cb(uint8_t itf) {
- (void) itf;
+ (void)itf;
}
TU_ATTR_WEAK void tud_cdc_notify_complete_cb(uint8_t itf) {
- (void) itf;
+ (void)itf;
}
TU_ATTR_WEAK void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) {
- (void) itf;
- (void) dtr;
- (void) rts;
+ (void)itf;
+ (void)dtr;
+ (void)rts;
}
-TU_ATTR_WEAK void tud_cdc_line_coding_cb(uint8_t itf, cdc_line_coding_t const* p_line_coding) {
- (void) itf;
- (void) p_line_coding;
+TU_ATTR_WEAK void tud_cdc_line_coding_cb(uint8_t itf, const cdc_line_coding_t *p_line_coding) {
+ (void)itf;
+ (void)p_line_coding;
}
TU_ATTR_WEAK void tud_cdc_send_break_cb(uint8_t itf, uint16_t duration_ms) {
- (void) itf;
- (void) duration_ms;
+ (void)itf;
+ (void)duration_ms;
+}
+
+//--------------------------------------------------------------------+
+// INTERNAL OBJECT & FUNCTION DECLARATION
+//--------------------------------------------------------------------+
+static cdcd_interface_t _cdcd_itf[CFG_TUD_CDC];
+CFG_TUD_MEM_SECTION static cdcd_epbuf_t _cdcd_epbuf[CFG_TUD_CDC];
+static tud_cdc_configure_t _cdcd_cfg = TUD_CDC_CONFIGURE_DEFAULT();
+
+TU_ATTR_ALWAYS_INLINE static inline uint8_t find_cdc_itf(uint8_t ep_addr) {
+ for (uint8_t idx = 0; idx < CFG_TUD_CDC; idx++) {
+ const cdcd_interface_t *p_cdc = &_cdcd_itf[idx];
+ if (ep_addr == p_cdc->stream.rx.ep_addr || ep_addr == p_cdc->stream.tx.ep_addr ||
+ (ep_addr == p_cdc->ep_notify && ep_addr != 0)) {
+ return idx;
+ }
+ }
+ return TUSB_INDEX_INVALID_8;
}
//--------------------------------------------------------------------+
// APPLICATION API
//--------------------------------------------------------------------+
bool tud_cdc_configure(const tud_cdc_configure_t* driver_cfg) {
- TU_VERIFY(driver_cfg);
+ TU_VERIFY(driver_cfg != NULL);
_cdcd_cfg = *driver_cfg;
return true;
}
bool tud_cdc_n_ready(uint8_t itf) {
- return tud_ready() && _cdcd_itf[itf].ep_in != 0 && _cdcd_itf[itf].ep_out != 0;
+ TU_VERIFY(itf < CFG_TUD_CDC);
+ TU_VERIFY(tud_ready());
+ const cdcd_interface_t *p_cdc = &_cdcd_itf[itf];
+
+ const bool in_opened = tu_edpt_stream_is_opened(&p_cdc->stream.tx);
+ const bool out_opened = tu_edpt_stream_is_opened(&p_cdc->stream.rx);
+ return in_opened && out_opened;
}
bool tud_cdc_n_connected(uint8_t itf) {
+ TU_VERIFY(itf < CFG_TUD_CDC);
+ TU_VERIFY(tud_ready());
// DTR (bit 0) active is considered as connected
- return tud_ready() && tu_bit_test(_cdcd_itf[itf].line_state, 0);
+ return tu_bit_test(_cdcd_itf[itf].line_state, 0);
}
uint8_t tud_cdc_n_get_line_state(uint8_t itf) {
+ TU_VERIFY(itf < CFG_TUD_CDC, 0);
return _cdcd_itf[itf].line_state;
}
-void tud_cdc_n_get_line_coding(uint8_t itf, cdc_line_coding_t* coding) {
+void tud_cdc_n_get_line_coding(uint8_t itf, cdc_line_coding_t *coding) {
+ TU_VERIFY(itf < CFG_TUD_CDC, );
(*coding) = _cdcd_itf[itf].line_coding;
}
#if CFG_TUD_CDC_NOTIFY
bool tud_cdc_n_notify_uart_state (uint8_t itf, const cdc_notify_uart_state_t *state) {
- cdcd_interface_t* p_cdc = &_cdcd_itf[itf];
- cdcd_epbuf_t* p_epbuf = &_cdcd_epbuf[itf];
+ TU_VERIFY(itf < CFG_TUD_CDC);
+ cdcd_interface_t *p_cdc = &_cdcd_itf[itf];
+ cdcd_epbuf_t *p_epbuf = &_cdcd_epbuf[itf];
TU_VERIFY(tud_ready() && p_cdc->ep_notify != 0);
TU_VERIFY(usbd_edpt_claim(p_cdc->rhport, p_cdc->ep_notify));
@@ -200,8 +185,9 @@ bool tud_cdc_n_notify_uart_state (uint8_t itf, const cdc_notify_uart_state_t *st
}
bool tud_cdc_n_notify_conn_speed_change(uint8_t itf, const cdc_notify_conn_speed_change_t* conn_speed_change) {
- cdcd_interface_t* p_cdc = &_cdcd_itf[itf];
- cdcd_epbuf_t* p_epbuf = &_cdcd_epbuf[itf];
+ TU_VERIFY(itf < CFG_TUD_CDC);
+ cdcd_interface_t *p_cdc = &_cdcd_itf[itf];
+ cdcd_epbuf_t *p_epbuf = &_cdcd_epbuf[itf];
TU_VERIFY(tud_ready() && p_cdc->ep_notify != 0);
TU_VERIFY(usbd_edpt_claim(p_cdc->rhport, p_cdc->ep_notify));
@@ -218,6 +204,7 @@ bool tud_cdc_n_notify_conn_speed_change(uint8_t itf, const cdc_notify_conn_speed
#endif
void tud_cdc_n_set_wanted_char(uint8_t itf, char wanted) {
+ TU_VERIFY(itf < CFG_TUD_CDC, );
_cdcd_itf[itf].wanted_char = wanted;
}
@@ -225,77 +212,53 @@ void tud_cdc_n_set_wanted_char(uint8_t itf, char wanted) {
// READ API
//--------------------------------------------------------------------+
uint32_t tud_cdc_n_available(uint8_t itf) {
- return tu_fifo_count(&_cdcd_itf[itf].rx_ff);
+ TU_VERIFY(itf < CFG_TUD_CDC, 0);
+ return tu_edpt_stream_read_available(&_cdcd_itf[itf].stream.rx);
}
uint32_t tud_cdc_n_read(uint8_t itf, void* buffer, uint32_t bufsize) {
- cdcd_interface_t* p_cdc = &_cdcd_itf[itf];
- uint32_t num_read = tu_fifo_read_n(&p_cdc->rx_ff, buffer, (uint16_t) TU_MIN(bufsize, UINT16_MAX));
- _prep_out_transaction(itf);
- return num_read;
+ TU_VERIFY(itf < CFG_TUD_CDC, 0);
+ cdcd_interface_t *p_cdc = &_cdcd_itf[itf];
+ return tu_edpt_stream_read(p_cdc->rhport, &p_cdc->stream.rx, buffer, bufsize);
}
-bool tud_cdc_n_peek(uint8_t itf, uint8_t* chr) {
- return tu_fifo_peek(&_cdcd_itf[itf].rx_ff, chr);
+bool tud_cdc_n_peek(uint8_t itf, uint8_t *chr) {
+ TU_VERIFY(itf < CFG_TUD_CDC);
+ return tu_edpt_stream_peek(&_cdcd_itf[itf].stream.rx, chr);
}
void tud_cdc_n_read_flush(uint8_t itf) {
- cdcd_interface_t* p_cdc = &_cdcd_itf[itf];
- tu_fifo_clear(&p_cdc->rx_ff);
- _prep_out_transaction(itf);
+ TU_VERIFY(itf < CFG_TUD_CDC, );
+ cdcd_interface_t *p_cdc = &_cdcd_itf[itf];
+ tu_edpt_stream_clear(&p_cdc->stream.rx);
+ tu_edpt_stream_read_xfer(p_cdc->rhport, &p_cdc->stream.rx);
}
//--------------------------------------------------------------------+
// WRITE API
//--------------------------------------------------------------------+
uint32_t tud_cdc_n_write(uint8_t itf, const void* buffer, uint32_t bufsize) {
- cdcd_interface_t* p_cdc = &_cdcd_itf[itf];
- uint16_t wr_count = tu_fifo_write_n(&p_cdc->tx_ff, buffer, (uint16_t) TU_MIN(bufsize, UINT16_MAX));
-
- // flush if queue more than packet size
- if (tu_fifo_count(&p_cdc->tx_ff) >= BULK_PACKET_SIZE
- #if CFG_TUD_CDC_TX_BUFSIZE < BULK_PACKET_SIZE
- || tu_fifo_full(&p_cdc->tx_ff) // check full if fifo size is less than packet size
- #endif
- ) {
- tud_cdc_n_write_flush(itf);
- }
-
- return wr_count;
+ TU_VERIFY(itf < CFG_TUD_CDC, 0);
+ cdcd_interface_t *p_cdc = &_cdcd_itf[itf];
+ return tu_edpt_stream_write(p_cdc->rhport, &p_cdc->stream.tx, buffer, bufsize);
}
uint32_t tud_cdc_n_write_flush(uint8_t itf) {
- cdcd_interface_t* p_cdc = &_cdcd_itf[itf];
- cdcd_epbuf_t* p_epbuf = &_cdcd_epbuf[itf];
- TU_VERIFY(tud_ready(), 0); // Skip if usb is not ready yet
-
- // No data to send
- if (0 == tu_fifo_count(&p_cdc->tx_ff)) {
- return 0;
- }
-
- TU_VERIFY(usbd_edpt_claim(p_cdc->rhport, p_cdc->ep_in), 0); // Claim the endpoint
-
- // Pull data from FIFO
- const uint16_t count = tu_fifo_read_n(&p_cdc->tx_ff, p_epbuf->epin, CFG_TUD_CDC_EP_BUFSIZE);
-
- if (count > 0) {
- TU_ASSERT(usbd_edpt_xfer(p_cdc->rhport, p_cdc->ep_in, p_epbuf->epin, count), 0);
- return count;
- } else {
- // Release endpoint since we don't make any transfer
- // Note: data is dropped if terminal is not connected
- usbd_edpt_release(p_cdc->rhport, p_cdc->ep_in);
- return 0;
- }
+ TU_VERIFY(itf < CFG_TUD_CDC, 0);
+ cdcd_interface_t *p_cdc = &_cdcd_itf[itf];
+ return tu_edpt_stream_write_xfer(p_cdc->rhport, &p_cdc->stream.tx);
}
uint32_t tud_cdc_n_write_available(uint8_t itf) {
- return tu_fifo_remaining(&_cdcd_itf[itf].tx_ff);
+ TU_VERIFY(itf < CFG_TUD_CDC, 0);
+ cdcd_interface_t *p_cdc = &_cdcd_itf[itf];
+ return tu_edpt_stream_write_available(p_cdc->rhport, &p_cdc->stream.tx);
}
bool tud_cdc_n_write_clear(uint8_t itf) {
- return tu_fifo_clear(&_cdcd_itf[itf].tx_ff);
+ TU_VERIFY(itf < CFG_TUD_CDC);
+ cdcd_interface_t *p_cdc = &_cdcd_itf[itf];
+ return tu_edpt_stream_clear(&p_cdc->stream.tx);
}
//--------------------------------------------------------------------+
@@ -304,7 +267,8 @@ bool tud_cdc_n_write_clear(uint8_t itf) {
void cdcd_init(void) {
tu_memclr(_cdcd_itf, sizeof(_cdcd_itf));
for (uint8_t i = 0; i < CFG_TUD_CDC; i++) {
- cdcd_interface_t* p_cdc = &_cdcd_itf[i];
+ cdcd_interface_t *p_cdc = &_cdcd_itf[i];
+ cdcd_epbuf_t *p_epbuf = &_cdcd_epbuf[i];
p_cdc->wanted_char = (char) -1;
@@ -314,44 +278,23 @@ void cdcd_init(void) {
p_cdc->line_coding.parity = 0;
p_cdc->line_coding.data_bits = 8;
- // Config RX fifo
- tu_fifo_config(&p_cdc->rx_ff, p_cdc->rx_ff_buf, TU_ARRAY_SIZE(p_cdc->rx_ff_buf), 1, false);
+ tu_edpt_stream_init(&p_cdc->stream.rx, false, false, false, p_cdc->stream.rx_ff_buf, CFG_TUD_CDC_RX_BUFSIZE,
+ p_epbuf->epout, CFG_TUD_CDC_EP_BUFSIZE);
// TX fifo can be configured to change to overwritable if not connected (DTR bit not set). Without DTR we do not
// know if data is actually polled by terminal. This way the most current data is prioritized.
// Default: is overwritable
- tu_fifo_config(&p_cdc->tx_ff, p_cdc->tx_ff_buf, TU_ARRAY_SIZE(p_cdc->tx_ff_buf), 1, _cdcd_cfg.tx_overwritabe_if_not_connected);
-
- #if OSAL_MUTEX_REQUIRED
- osal_mutex_t mutex_rd = osal_mutex_create(&p_cdc->rx_ff_mutex);
- osal_mutex_t mutex_wr = osal_mutex_create(&p_cdc->tx_ff_mutex);
- TU_ASSERT(mutex_rd != NULL && mutex_wr != NULL, );
-
- tu_fifo_config_mutex(&p_cdc->rx_ff, NULL, mutex_rd);
- tu_fifo_config_mutex(&p_cdc->tx_ff, mutex_wr, NULL);
- #endif
+ tu_edpt_stream_init(&p_cdc->stream.tx, false, true, _cdcd_cfg.tx_overwritabe_if_not_connected,
+ p_cdc->stream.tx_ff_buf, CFG_TUD_CDC_TX_BUFSIZE, p_epbuf->epin, CFG_TUD_CDC_EP_BUFSIZE);
}
}
bool cdcd_deinit(void) {
- #if OSAL_MUTEX_REQUIRED
- for(uint8_t i=0; i<CFG_TUD_CDC; i++) {
+ for (uint8_t i = 0; i < CFG_TUD_CDC; i++) {
cdcd_interface_t* p_cdc = &_cdcd_itf[i];
- const osal_mutex_t mutex_rd = p_cdc->rx_ff.mutex_rd;
- const osal_mutex_t mutex_wr = p_cdc->tx_ff.mutex_wr;
-
- if (mutex_rd != NULL) {
- osal_mutex_delete(mutex_rd);
- tu_fifo_config_mutex(&p_cdc->rx_ff, NULL, NULL);
- }
-
- if (mutex_wr != NULL) {
- osal_mutex_delete(mutex_wr);
- tu_fifo_config_mutex(&p_cdc->tx_ff, NULL, NULL);
- }
+ tu_edpt_stream_deinit(&p_cdc->stream.rx);
+ tu_edpt_stream_deinit(&p_cdc->stream.tx);
}
- #endif
-
return true;
}
@@ -360,74 +303,85 @@ void cdcd_reset(uint8_t rhport) {
for (uint8_t i = 0; i < CFG_TUD_CDC; i++) {
cdcd_interface_t* p_cdc = &_cdcd_itf[i];
-
tu_memclr(p_cdc, ITF_MEM_RESET_SIZE);
- if (!_cdcd_cfg.rx_persistent) {
- tu_fifo_clear(&p_cdc->rx_ff);
- }
- if (!_cdcd_cfg.tx_persistent) {
- tu_fifo_clear(&p_cdc->tx_ff);
- }
- tu_fifo_set_overwritable(&p_cdc->tx_ff, _cdcd_cfg.tx_overwritabe_if_not_connected);
+
+ tu_fifo_set_overwritable(&p_cdc->stream.tx.ff, _cdcd_cfg.tx_overwritabe_if_not_connected); // back to default
+ tu_edpt_stream_close(&p_cdc->stream.rx);
+ tu_edpt_stream_close(&p_cdc->stream.tx);
}
}
uint16_t cdcd_open(uint8_t rhport, const tusb_desc_interface_t* itf_desc, uint16_t max_len) {
// Only support ACM subclass
- TU_VERIFY( TUSB_CLASS_CDC == itf_desc->bInterfaceClass &&
- CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL == itf_desc->bInterfaceSubClass, 0);
+ TU_VERIFY(TUSB_CLASS_CDC == itf_desc->bInterfaceClass &&
+ CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL == itf_desc->bInterfaceSubClass,
+ 0);
- // Find available interface
- cdcd_interface_t* p_cdc;
- uint8_t cdc_id;
- for (cdc_id = 0; cdc_id < CFG_TUD_CDC; cdc_id++) {
- p_cdc = &_cdcd_itf[cdc_id];
- if (p_cdc->ep_in == 0) {
- break;
- }
- }
+ const uint8_t cdc_id = find_cdc_itf(0); // Find available interface
TU_ASSERT(cdc_id < CFG_TUD_CDC, 0);
+ cdcd_interface_t *p_cdc = &_cdcd_itf[cdc_id];
//------------- Control Interface -------------//
p_cdc->rhport = rhport;
p_cdc->itf_num = itf_desc->bInterfaceNumber;
- uint16_t drv_len = sizeof(tusb_desc_interface_t);
- const uint8_t* p_desc = tu_desc_next(itf_desc);
+ const uint8_t *p_desc = (const uint8_t *)itf_desc;
+ const uint8_t *desc_end = p_desc + max_len;
- // Communication Functional Descriptors
- while (TUSB_DESC_CS_INTERFACE == tu_desc_type(p_desc) && drv_len <= max_len) {
- drv_len += tu_desc_len(p_desc);
+ // Skip all class-specific descriptor
+ p_desc = tu_desc_next(itf_desc);
+ while (tu_desc_in_bounds(p_desc, desc_end) && TUSB_DESC_CS_INTERFACE == tu_desc_type(p_desc)) {
p_desc = tu_desc_next(p_desc);
}
+ // notification endpoint (optional)
if (TUSB_DESC_ENDPOINT == tu_desc_type(p_desc)) {
- // notification endpoint
const tusb_desc_endpoint_t* desc_ep = (const tusb_desc_endpoint_t*) p_desc;
TU_ASSERT(usbd_edpt_open(rhport, desc_ep), 0);
p_cdc->ep_notify = desc_ep->bEndpointAddress;
- drv_len += tu_desc_len(p_desc);
p_desc = tu_desc_next(p_desc);
}
- //------------- Data Interface (if any) -------------//
- if ((TUSB_DESC_INTERFACE == tu_desc_type(p_desc)) &&
- (TUSB_CLASS_CDC_DATA == ((const tusb_desc_interface_t*) p_desc)->bInterfaceClass)) {
- // next to endpoint descriptor
- drv_len += tu_desc_len(p_desc);
- p_desc = tu_desc_next(p_desc);
+ //------------- Data Interface (optional) -------------//
+ if (TUSB_DESC_INTERFACE == tu_desc_type(p_desc)) {
+ const tusb_desc_interface_t *data_itf_desc = (const tusb_desc_interface_t *)p_desc;
+ if (TUSB_CLASS_CDC_DATA == data_itf_desc->bInterfaceClass) {
+ for (uint8_t e = 0; e < data_itf_desc->bNumEndpoints; e++) {
+ if (!tu_desc_in_bounds(p_desc, desc_end)) {
+ break;
+ }
+ p_desc = tu_desc_next(p_desc);
- // Open endpoint pair
- TU_ASSERT(usbd_open_edpt_pair(rhport, p_desc, 2, TUSB_XFER_BULK, &p_cdc->ep_out, &p_cdc->ep_in), 0);
+ const tusb_desc_endpoint_t *desc_ep = (const tusb_desc_endpoint_t *)p_desc;
+ TU_ASSERT(TUSB_DESC_ENDPOINT == desc_ep->bDescriptorType && TUSB_XFER_BULK == desc_ep->bmAttributes.xfer, 0);
- drv_len += 2 * sizeof(tusb_desc_endpoint_t);
- }
+ TU_ASSERT(usbd_edpt_open(rhport, desc_ep), 0);
+ if (tu_edpt_dir(desc_ep->bEndpointAddress) == TUSB_DIR_IN) {
+ tu_edpt_stream_t *stream_tx = &p_cdc->stream.tx;
+
+ tu_edpt_stream_open(stream_tx, desc_ep);
+ if (_cdcd_cfg.tx_persistent) {
+ tu_edpt_stream_write_xfer(rhport, stream_tx); // flush pending data
+ } else {
+ tu_edpt_stream_clear(stream_tx);
+ }
+ } else {
+ tu_edpt_stream_t *stream_rx = &p_cdc->stream.rx;
- // Prepare for incoming data
- _prep_out_transaction(cdc_id);
+ tu_edpt_stream_open(stream_rx, desc_ep);
+ if (!_cdcd_cfg.rx_persistent) {
+ tu_edpt_stream_clear(stream_rx);
+ }
+ TU_ASSERT(tu_edpt_stream_read_xfer(rhport, stream_rx) > 0, 0); // prepare for incoming data
+ }
+ }
- return drv_len;
+ p_desc = tu_desc_next(p_desc);
+ }
+ }
+
+ return (uint16_t)(p_desc - (const uint8_t *)itf_desc);
}
// Invoked when a control transfer occurred on an interface of this class
@@ -449,7 +403,7 @@ bool cdcd_control_xfer_cb(uint8_t rhport, uint8_t stage, const tusb_control_requ
}
TU_VERIFY(itf < CFG_TUD_CDC);
- switch (request->bRequest) { //-V2520 //-V2659
+ switch (request->bRequest) {
case CDC_REQUEST_SET_LINE_CODING:
if (stage == CONTROL_STAGE_SETUP) {
TU_LOG_DRV(" Set Line Coding\r\n");
@@ -484,15 +438,13 @@ bool cdcd_control_xfer_cb(uint8_t rhport, uint8_t stage, const tusb_control_requ
// If enabled: fifo overwriting is disabled if DTR bit is set and vice versa
if (_cdcd_cfg.tx_overwritabe_if_not_connected) {
- tu_fifo_set_overwritable(&p_cdc->tx_ff, !dtr);
+ tu_fifo_set_overwritable(&p_cdc->stream.tx.ff, !dtr);
} else {
- tu_fifo_set_overwritable(&p_cdc->tx_ff, false);
+ tu_fifo_set_overwritable(&p_cdc->stream.tx.ff, false);
}
TU_LOG_DRV(" Set Control Line State: DTR = %d, RTS = %d\r\n", dtr, rts);
-
- // Invoke callback
- tud_cdc_line_state_cb(itf, dtr, rts);
+ tud_cdc_line_state_cb(itf, dtr, rts); // invoke callback
} else {
// nothing to do
}
@@ -507,7 +459,6 @@ bool cdcd_control_xfer_cb(uint8_t rhport, uint8_t stage, const tusb_control_requ
} else {
// nothing to do
}
-
break;
default:
@@ -518,58 +469,45 @@ bool cdcd_control_xfer_cb(uint8_t rhport, uint8_t stage, const tusb_control_requ
}
bool cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) {
- (void) result;
+ (void)result;
- uint8_t itf;
- cdcd_interface_t* p_cdc;
-
- // Identify which interface to use
- for (itf = 0; itf < CFG_TUD_CDC; itf++) {
- p_cdc = &_cdcd_itf[itf];
- if ((ep_addr == p_cdc->ep_out) || (ep_addr == p_cdc->ep_in) || (ep_addr == p_cdc->ep_notify)) {
- break;
- }
- }
+ uint8_t itf = find_cdc_itf(ep_addr);
TU_ASSERT(itf < CFG_TUD_CDC);
- cdcd_epbuf_t* p_epbuf = &_cdcd_epbuf[itf];
+ cdcd_interface_t *p_cdc = &_cdcd_itf[itf];
+ tu_edpt_stream_t *stream_rx = &p_cdc->stream.rx;
+ tu_edpt_stream_t *stream_tx = &p_cdc->stream.tx;
- // Received new data
- if (ep_addr == p_cdc->ep_out) {
- tu_fifo_write_n(&p_cdc->rx_ff, p_epbuf->epout, (uint16_t) xferred_bytes);
+ // Received new data, move to fifo
+ if (ep_addr == stream_rx->ep_addr) {
+ tu_edpt_stream_read_xfer_complete(stream_rx, xferred_bytes);
- // Check for wanted char and invoke callback if needed
- if (((signed char) p_cdc->wanted_char) != -1) {
+ // Check for wanted char and invoke wanted callback (multiple times if multiple wanted received)
+ if (((signed char)p_cdc->wanted_char) != -1) {
for (uint32_t i = 0; i < xferred_bytes; i++) {
- if ((p_cdc->wanted_char == (char) p_epbuf->epout[i]) && !tu_fifo_empty(&p_cdc->rx_ff)) {
+ if ((p_cdc->wanted_char == (char)stream_rx->ep_buf[i]) && !tu_edpt_stream_empty(stream_rx)) {
tud_cdc_rx_wanted_cb(itf, p_cdc->wanted_char);
}
}
}
- // invoke receive callback (if there is still data)
- if (!tu_fifo_empty(&p_cdc->rx_ff)) {
+ // invoke receive callback if there is still data
+ if (!tu_edpt_stream_empty(stream_rx)) {
tud_cdc_rx_cb(itf);
}
- // prepare for OUT transaction
- _prep_out_transaction(itf);
+ tu_edpt_stream_read_xfer(rhport, stream_rx); // prepare for more data
}
// Data sent to host, we continue to fetch from tx fifo to send.
// Note: This will cause incorrect baudrate set in line coding.
// Though maybe the baudrate is not really important !!!
- if (ep_addr == p_cdc->ep_in) {
+ if (ep_addr == stream_tx->ep_addr) {
// invoke transmit callback to possibly refill tx fifo
tud_cdc_tx_complete_cb(itf);
- if (0 == tud_cdc_n_write_flush(itf)) {
- // If there is no data left, a ZLP should be sent if
- // xferred_bytes is multiple of EP Packet size and not zero
- if (0 == tu_fifo_count(&p_cdc->tx_ff) && xferred_bytes > 0 && (0 == (xferred_bytes & (BULK_PACKET_SIZE - 1)))) {
- if (usbd_edpt_claim(rhport, p_cdc->ep_in)) {
- TU_ASSERT(usbd_edpt_xfer(rhport, p_cdc->ep_in, NULL, 0));
- }
- }
+ if (0 == tu_edpt_stream_write_xfer(rhport, stream_tx)) {
+ // If there is no data left, a ZLP should be sent if needed
+ tu_edpt_stream_write_zlp_if_needed(rhport, stream_tx, xferred_bytes);
}
}
diff --git a/src/class/cdc/cdc_host.c b/src/class/cdc/cdc_host.c
index 7fdf0a7b9..35717ddf6 100644
--- a/src/class/cdc/cdc_host.c
+++ b/src/class/cdc/cdc_host.c
@@ -298,6 +298,7 @@ TU_VERIFY_STATIC(TU_ARRAY_SIZE(serial_drivers) == SERIAL_DRIVER_COUNT, "Serial d
//--------------------------------------------------------------------+
// INTERNAL OBJECT & FUNCTION DECLARATION
//--------------------------------------------------------------------+
+static bool open_ep_stream_pair(cdch_interface_t *p_cdc, const tusb_desc_endpoint_t *desc_ep);
TU_ATTR_ALWAYS_INLINE static inline cdch_interface_t * get_itf(uint8_t idx) {
TU_ASSERT(idx < CFG_TUH_CDC, NULL);
@@ -364,7 +365,7 @@ static cdch_interface_t* get_itf_by_xfer(const tuh_xfer_t * xfer) {
#endif
default:
- break;
+ break; // unknown driver
}
}
}
@@ -389,8 +390,6 @@ static cdch_interface_t * make_new_itf(uint8_t daddr, tusb_desc_interface_t cons
return NULL;
}
-static bool open_ep_stream_pair(cdch_interface_t * p_cdc , tusb_desc_endpoint_t const *desc_ep);
-
//--------------------------------------------------------------------+
// Weak stubs: invoked if no strong implementation is available
//--------------------------------------------------------------------+
@@ -519,7 +518,7 @@ bool tuh_cdc_read_clear (uint8_t idx) {
TU_VERIFY(p_cdc);
bool ret = tu_edpt_stream_clear(&p_cdc->stream.rx);
- tu_edpt_stream_read_xfer(p_cdc->daddr, &p_cdc->stream.rx);
+ (void)tu_edpt_stream_read_xfer(p_cdc->daddr, &p_cdc->stream.rx);
return ret;
}
@@ -648,13 +647,10 @@ bool cdch_init(void) {
for (size_t i = 0; i < CFG_TUH_CDC; i++) {
cdch_interface_t *p_cdc = &cdch_data[i];
cdch_epbuf_t *epbuf = &cdch_epbuf[i];
- tu_edpt_stream_init(&p_cdc->stream.tx, true, true, false,
- p_cdc->stream.tx_ff_buf, CFG_TUH_CDC_TX_BUFSIZE,
- epbuf->tx, CFG_TUH_CDC_TX_EPSIZE);
-
- tu_edpt_stream_init(&p_cdc->stream.rx, true, false, false,
- p_cdc->stream.rx_ff_buf, CFG_TUH_CDC_RX_BUFSIZE,
- epbuf->rx, CFG_TUH_CDC_RX_EPSIZE);
+ TU_ASSERT(tu_edpt_stream_init(&p_cdc->stream.tx, true, true, false, p_cdc->stream.tx_ff_buf, CFG_TUH_CDC_TX_BUFSIZE,
+ epbuf->tx, CFG_TUH_CDC_TX_EPSIZE));
+ TU_ASSERT(tu_edpt_stream_init(&p_cdc->stream.rx, true, false, false, p_cdc->stream.rx_ff_buf,
+ CFG_TUH_CDC_RX_BUFSIZE, epbuf->rx, CFG_TUH_CDC_RX_EPSIZE));
}
return true;
@@ -663,8 +659,8 @@ bool cdch_init(void) {
bool cdch_deinit(void) {
for (size_t i = 0; i < CFG_TUH_CDC; i++) {
cdch_interface_t *p_cdc = &cdch_data[i];
- tu_edpt_stream_deinit(&p_cdc->stream.tx);
- tu_edpt_stream_deinit(&p_cdc->stream.rx);
+ (void)tu_edpt_stream_deinit(&p_cdc->stream.tx);
+ (void)tu_edpt_stream_deinit(&p_cdc->stream.rx);
}
return true;
}
@@ -674,11 +670,9 @@ void cdch_close(uint8_t daddr) {
cdch_interface_t *p_cdc = &cdch_data[idx];
if (p_cdc->daddr == daddr) {
TU_LOG_CDC(p_cdc, "close");
+ tuh_cdc_umount_cb(idx); // invoke callback
- // Invoke application callback
- tuh_cdc_umount_cb(idx);
-
- p_cdc->daddr = 0;
+ p_cdc->daddr = 0;
p_cdc->bInterfaceNumber = 0;
p_cdc->mounted = false;
tu_edpt_stream_close(&p_cdc->stream.tx);
@@ -696,13 +690,12 @@ bool cdch_xfer_cb(uint8_t daddr, uint8_t ep_addr, xfer_result_t event, uint32_t
TU_ASSERT(p_cdc);
if (ep_addr == p_cdc->stream.tx.ep_addr) {
- // invoke tx complete callback to possibly refill tx fifo
- tuh_cdc_tx_complete_cb(idx);
+ tuh_cdc_tx_complete_cb(idx); // invoke transmit complete callback
if (0 == tu_edpt_stream_write_xfer(daddr, &p_cdc->stream.tx)) {
// If there is no data left, a ZLP should be sent if:
// - xferred_bytes is multiple of EP Packet size and not zero
- tu_edpt_stream_write_zlp_if_needed(daddr, &p_cdc->stream.tx, xferred_bytes);
+ (void)tu_edpt_stream_write_zlp_if_needed(daddr, &p_cdc->stream.tx, xferred_bytes);
}
} else if (ep_addr == p_cdc->stream.rx.ep_addr) {
#if CFG_TUH_CDC_FTDI
@@ -718,7 +711,6 @@ bool cdch_xfer_cb(uint8_t daddr, uint8_t ep_addr, xfer_result_t event, uint32_t
#endif
{
tu_edpt_stream_read_xfer_complete(&p_cdc->stream.rx, xferred_bytes);
-
tuh_cdc_rx_cb(idx); // invoke receive callback
}
@@ -727,7 +719,7 @@ bool cdch_xfer_cb(uint8_t daddr, uint8_t ep_addr, xfer_result_t event, uint32_t
} else if (ep_addr == p_cdc->ep_notif) {
// TODO handle notification endpoint
} else {
- TU_ASSERT(false);
+ return false;
}
return true;
@@ -736,20 +728,17 @@ bool cdch_xfer_cb(uint8_t daddr, uint8_t ep_addr, xfer_result_t event, uint32_t
//--------------------------------------------------------------------+
// Enumeration
//--------------------------------------------------------------------+
-
static bool open_ep_stream_pair(cdch_interface_t *p_cdc, tusb_desc_endpoint_t const *desc_ep) {
for (size_t i = 0; i < 2; i++) {
TU_ASSERT(TUSB_DESC_ENDPOINT == desc_ep->bDescriptorType &&
TUSB_XFER_BULK == desc_ep->bmAttributes.xfer);
TU_ASSERT(tuh_edpt_open(p_cdc->daddr, desc_ep));
+ tu_edpt_stream_t *stream =
+ (tu_edpt_dir(desc_ep->bEndpointAddress) == TUSB_DIR_IN) ? &p_cdc->stream.rx : &p_cdc->stream.tx;
+ tu_edpt_stream_open(stream, desc_ep);
+ tu_edpt_stream_clear(stream);
- if (tu_edpt_dir(desc_ep->bEndpointAddress) == TUSB_DIR_IN) {
- tu_edpt_stream_open(&p_cdc->stream.rx, desc_ep);
- } else {
- tu_edpt_stream_open(&p_cdc->stream.tx, desc_ep);
- }
-
- desc_ep = (tusb_desc_endpoint_t const *) tu_desc_next(desc_ep);
+ desc_ep = (const tusb_desc_endpoint_t *)tu_desc_next(desc_ep);
}
return true;
@@ -832,6 +821,7 @@ static void cdch_process_set_config(tuh_xfer_t *xfer) {
}
}
+// return false if there is no active transfer
static bool set_line_state_on_enum(cdch_interface_t *p_cdc, tuh_xfer_t *xfer) {
enum {
ENUM_SET_LINE_CODING = 0,
diff --git a/src/class/midi/midi_device.c b/src/class/midi/midi_device.c
index f065e486a..b20903d68 100644
--- a/src/class/midi/midi_device.c
+++ b/src/class/midi/midi_device.c
@@ -404,13 +404,17 @@ uint16_t midid_open(uint8_t rhport, const tusb_desc_interface_t *desc_itf, uint1
const uint8_t ep_addr = ((const tusb_desc_endpoint_t *)p_desc)->bEndpointAddress;
if (tu_edpt_dir(ep_addr) == TUSB_DIR_IN) {
- tu_edpt_stream_open(&p_midi->ep_stream.tx, desc_ep);
+ tu_edpt_stream_t *stream_tx = &p_midi->ep_stream.tx;
+ tu_edpt_stream_open(stream_tx, desc_ep);
+ tu_edpt_stream_clear(stream_tx);
} else {
- tu_edpt_stream_open(&p_midi->ep_stream.rx, desc_ep);
- TU_ASSERT(tu_edpt_stream_read_xfer(rhport, &p_midi->ep_stream.rx) > 0, 0); // prepare to receive data
+ tu_edpt_stream_t *stream_rx = &p_midi->ep_stream.rx;
+ tu_edpt_stream_open(stream_rx, desc_ep);
+ tu_edpt_stream_clear(stream_rx);
+ TU_ASSERT(tu_edpt_stream_read_xfer(rhport, stream_rx) > 0, 0); // prepare to receive data
}
- p_desc = tu_desc_next(p_desc); // skip CS Endpoint descriptor
+ p_desc = tu_desc_next(p_desc); // skip CS Endpoint descriptor
found_ep++;
}
diff --git a/src/class/midi/midi_host.c b/src/class/midi/midi_host.c
index 8b78fe945..07062875c 100644
--- a/src/class/midi/midi_host.c
+++ b/src/class/midi/midi_host.c
@@ -59,9 +59,6 @@ typedef struct {
uint8_t iInterface;
uint8_t itf_count; // number of interface including Audio Control + MIDI streaming
- uint8_t ep_in; // IN endpoint address
- uint8_t ep_out; // OUT endpoint address
-
uint8_t rx_cable_count; // IN endpoint CS descriptor bNumEmbMIDIJack value
uint8_t tx_cable_count; // OUT endpoint CS descriptor bNumEmbMIDIJack value
@@ -147,8 +144,6 @@ void midih_close(uint8_t daddr) {
TU_LOG_DRV(" MIDI close addr = %u index = %u\r\n", daddr, idx);
tuh_midi_umount_cb(idx);
- p_midi->ep_in = 0;
- p_midi->ep_out = 0;
p_midi->bInterfaceNumber = 0;
p_midi->rx_cable_count = 0;
p_midi->tx_cable_count = 0;
@@ -169,23 +164,25 @@ bool midih_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint
const uint8_t idx = get_idx_by_ep_addr(dev_addr, ep_addr);
TU_VERIFY(idx < CFG_TUH_MIDI);
midih_interface_t *p_midi = &_midi_host[idx];
+ tu_edpt_stream_t *ep_str_rx = &p_midi->ep_stream.rx;
+ tu_edpt_stream_t *ep_str_tx = &p_midi->ep_stream.tx;
- if (ep_addr == p_midi->ep_stream.rx.ep_addr) {
+ if (ep_addr == ep_str_rx->ep_addr) {
// receive new data, put it into FIFO and invoke callback if available
// Note: some devices send back all zero packets even if there is no data ready
- if (xferred_bytes && !tu_mem_is_zero(p_midi->ep_stream.rx.ep_buf, xferred_bytes)) {
- tu_edpt_stream_read_xfer_complete(&p_midi->ep_stream.rx, xferred_bytes);
+ if (xferred_bytes && !tu_mem_is_zero(ep_str_rx->ep_buf, xferred_bytes)) {
+ tu_edpt_stream_read_xfer_complete(ep_str_rx, xferred_bytes);
tuh_midi_rx_cb(idx, xferred_bytes);
}
- tu_edpt_stream_read_xfer(dev_addr, &p_midi->ep_stream.rx); // prepare for next transfer
- } else if (ep_addr == p_midi->ep_stream.tx.ep_addr) {
+ tu_edpt_stream_read_xfer(dev_addr, ep_str_rx); // prepare for next transfer
+ } else if (ep_addr == ep_str_tx->ep_addr) {
tuh_midi_tx_cb(idx, xferred_bytes);
- if (0 == tu_edpt_stream_write_xfer(dev_addr, &p_midi->ep_stream.tx)) {
+ if (0 == tu_edpt_stream_write_xfer(dev_addr, ep_str_tx)) {
// If there is no data left, a ZLP should be sent if
// xferred_bytes is multiple of EP size and not zero
- tu_edpt_stream_write_zlp_if_needed(dev_addr, &p_midi->ep_stream.tx, xferred_bytes);
+ tu_edpt_stream_write_zlp_if_needed(dev_addr, ep_str_tx, xferred_bytes);
}
}
@@ -295,21 +292,20 @@ bool midih_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *d
const midi_desc_cs_endpoint_t *p_csep = (const midi_desc_cs_endpoint_t *) p_desc;
TU_LOG_DRV(" Endpoint and CS_Endpoint descriptor %02x\r\n", p_ep->bEndpointAddress);
+ tu_edpt_stream_t *ep_stream;
if (tu_edpt_dir(p_ep->bEndpointAddress) == TUSB_DIR_OUT) {
- p_midi->ep_out = p_ep->bEndpointAddress;
p_midi->tx_cable_count = p_csep->bNumEmbMIDIJack;
desc_cb.desc_epout = p_ep;
-
- TU_ASSERT(tuh_edpt_open(dev_addr, p_ep));
- tu_edpt_stream_open(&p_midi->ep_stream.tx, p_ep);
+ ep_stream = &p_midi->ep_stream.tx;
} else {
- p_midi->ep_in = p_ep->bEndpointAddress;
p_midi->rx_cable_count = p_csep->bNumEmbMIDIJack;
- desc_cb.desc_epin = p_ep;
-
- TU_ASSERT(tuh_edpt_open(dev_addr, p_ep));
- tu_edpt_stream_open(&p_midi->ep_stream.rx, p_ep);
+ desc_cb.desc_epin = p_ep;
+ ep_stream = &p_midi->ep_stream.rx;
}
+ TU_ASSERT(tuh_edpt_open(dev_addr, p_ep));
+ tu_edpt_stream_open(ep_stream, p_ep);
+ tu_edpt_stream_clear(ep_stream);
+
break;
}
@@ -379,8 +375,14 @@ bool tuh_midi_itf_get_info(uint8_t idx, tuh_itf_info_t* info) {
desc->bDescriptorType = TUSB_DESC_INTERFACE;
desc->bInterfaceNumber = p_midi->bInterfaceNumber;
- desc->bAlternateSetting = 0;
- desc->bNumEndpoints = (uint8_t)((p_midi->ep_in != 0 ? 1:0) + (p_midi->ep_out != 0 ? 1:0));
+ desc->bAlternateSetting = 0;
+ desc->bNumEndpoints = 0;
+ if (tu_edpt_stream_is_opened(&p_midi->ep_stream.tx)) {
+ desc->bNumEndpoints++;
+ }
+ if (tu_edpt_stream_is_opened(&p_midi->ep_stream.rx)) {
+ desc->bNumEndpoints++;
+ }
desc->bInterfaceClass = TUSB_CLASS_AUDIO;
desc->bInterfaceSubClass = AUDIO_SUBCLASS_MIDI_STREAMING;
desc->bInterfaceProtocol = 0;
diff --git a/src/class/vendor/vendor_device.c b/src/class/vendor/vendor_device.c
index c916ebe47..7da4d2239 100644
--- a/src/class/vendor/vendor_device.c
+++ b/src/class/vendor/vendor_device.c
@@ -234,16 +234,18 @@ uint16_t vendord_open(uint8_t rhport, const tusb_desc_interface_t* desc_itf, uin
const tusb_desc_endpoint_t* desc_ep = (const tusb_desc_endpoint_t*) p_desc;
TU_ASSERT(usbd_edpt_open(rhport, desc_ep));
- // open endpoint stream, skip if already opened
+ // open endpoint stream, skip if already opened (multiple IN/OUT endpoints)
if (tu_edpt_dir(desc_ep->bEndpointAddress) == TUSB_DIR_IN) {
- if (p_vendor->tx.stream.ep_addr == 0) {
- tu_edpt_stream_open(&p_vendor->tx.stream, desc_ep);
- tud_vendor_n_write_flush(itf);
+ tu_edpt_stream_t *stream_tx = &p_vendor->tx.stream;
+ if (stream_tx->ep_addr == 0) {
+ tu_edpt_stream_open(stream_tx, desc_ep);
+ tu_edpt_stream_write_xfer(rhport, stream_tx); // flush pending data
}
} else {
- if (p_vendor->rx.stream.ep_addr == 0) {
- tu_edpt_stream_open(&p_vendor->rx.stream, desc_ep);
- TU_ASSERT(tu_edpt_stream_read_xfer(rhport, &p_vendor->rx.stream) > 0, 0); // prepare for incoming data
+ tu_edpt_stream_t *stream_rx = &p_vendor->rx.stream;
+ if (stream_rx->ep_addr == 0) {
+ tu_edpt_stream_open(stream_rx, desc_ep);
+ TU_ASSERT(tu_edpt_stream_read_xfer(rhport, stream_rx) > 0, 0); // prepare for incoming data
}
}
}
diff --git a/src/common/tusb_common.h b/src/common/tusb_common.h
index 7aa42a2d7..f377d5272 100644
--- a/src/common/tusb_common.h
+++ b/src/common/tusb_common.h
@@ -354,7 +354,7 @@ TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_desc_subtype(void const* desc) {
return ((uint8_t const*) desc)[DESC_OFFSET_SUBTYPE];
}
-TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_desc_in_bounds(uint8_t const* p_desc, uint8_t const* desc_end) {
+TU_ATTR_ALWAYS_INLINE static inline bool tu_desc_in_bounds(const uint8_t *p_desc, const uint8_t *desc_end) {
if (p_desc >= desc_end) {
return false;
}
diff --git a/src/common/tusb_private.h b/src/common/tusb_private.h
index 367209e57..be1264a71 100644
--- a/src/common/tusb_private.h
+++ b/src/common/tusb_private.h
@@ -100,7 +100,6 @@ bool tu_edpt_stream_deinit(tu_edpt_stream_t* s);
// Open an stream for an endpoint
TU_ATTR_ALWAYS_INLINE static inline void tu_edpt_stream_open(tu_edpt_stream_t* s, tusb_desc_endpoint_t const *desc_ep) {
- tu_fifo_clear(&s->ff);
s->ep_addr = desc_ep->bEndpointAddress;
s->is_mps512 = tu_edpt_packet_size(desc_ep) == 512;
}
@@ -113,11 +112,14 @@ TU_ATTR_ALWAYS_INLINE static inline void tu_edpt_stream_close(tu_edpt_stream_t*
s->ep_addr = 0;
}
-// Clear fifo
TU_ATTR_ALWAYS_INLINE static inline bool tu_edpt_stream_clear(tu_edpt_stream_t* s) {
return tu_fifo_clear(&s->ff);
}
+TU_ATTR_ALWAYS_INLINE static inline bool tu_edpt_stream_empty(tu_edpt_stream_t *s) {
+ return tu_fifo_empty(&s->ff);
+}
+
//--------------------------------------------------------------------+
// Stream Write
//--------------------------------------------------------------------+
diff --git a/src/tusb.c b/src/tusb.c
index b308e2915..6fb5309ab 100644
--- a/src/tusb.c
+++ b/src/tusb.c
@@ -354,8 +354,8 @@ bool tu_edpt_stream_init(tu_edpt_stream_t* s, bool is_host, bool is_tx, bool ove
return true;
}
-bool tu_edpt_stream_deinit(tu_edpt_stream_t* s) {
- (void) s;
+bool tu_edpt_stream_deinit(tu_edpt_stream_t *s) {
+ (void)s;
#if OSAL_MUTEX_REQUIRED
if (s->ff.mutex_wr) {
osal_mutex_delete(s->ff.mutex_wr);
@@ -363,7 +363,7 @@ bool tu_edpt_stream_deinit(tu_edpt_stream_t* s) {
if (s->ff.mutex_rd) {
osal_mutex_delete(s->ff.mutex_rd);
}
-#endif
+ #endif
return true;
}
@@ -412,7 +412,7 @@ TU_ATTR_ALWAYS_INLINE static inline bool stream_release(uint8_t hwid, tu_edpt_st
bool tu_edpt_stream_write_zlp_if_needed(uint8_t hwid, tu_edpt_stream_t* s, uint32_t last_xferred_bytes) {
// ZLP condition: no pending data, last transferred bytes is multiple of packet size
const uint16_t mps = s->is_mps512 ? TUSB_EPSIZE_BULK_HS : TUSB_EPSIZE_BULK_FS;
- TU_VERIFY(!tu_fifo_count(&s->ff) && last_xferred_bytes > 0 && (0 == (last_xferred_bytes & (mps - 1))));
+ TU_VERIFY(tu_fifo_empty(&s->ff) && last_xferred_bytes > 0 && (0 == (last_xferred_bytes & (mps - 1))));
TU_VERIFY(stream_claim(hwid, s));
TU_ASSERT(stream_xfer(hwid, s, 0));
return true;
@@ -517,7 +517,7 @@ uint32_t tu_edpt_stream_read_xfer(uint8_t hwid, tu_edpt_stream_t* s) {
}
uint32_t tu_edpt_stream_read(uint8_t hwid, tu_edpt_stream_t* s, void* buffer, uint32_t bufsize) {
- uint32_t num_read = tu_fifo_read_n(&s->ff, buffer, (uint16_t) bufsize);
+ const uint32_t num_read = tu_fifo_read_n(&s->ff, buffer, (uint16_t)bufsize);
tu_edpt_stream_read_xfer(hwid, s);
return num_read;
}