From 185757ed413fc7bdc65cf494a6079cfed049f98e Mon Sep 17 00:00:00 2001 From: James Smith <{ID}+{username}@users.noreply.github.com> Date: Sat, 20 Dec 2025 13:27:29 -0700 Subject: [PATCH 1/3] Added tud_vendor_write_clear() which forcefully clears TX buffer --- examples/device/webusb_serial/src/main.c | 1 + src/class/vendor/vendor_device.c | 6 ++++++ src/class/vendor/vendor_device.h | 7 +++++++ src/common/tusb_fifo.c | 16 ++++++++++++++++ src/common/tusb_fifo.h | 1 + src/common/tusb_private.h | 4 ++++ 6 files changed, 35 insertions(+) diff --git a/examples/device/webusb_serial/src/main.c b/examples/device/webusb_serial/src/main.c index 50794bdba..155768b76 100644 --- a/examples/device/webusb_serial/src/main.c +++ b/examples/device/webusb_serial/src/main.c @@ -196,6 +196,7 @@ bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_requ tud_vendor_write_str("\r\nWebUSB interface connected\r\n"); tud_vendor_write_flush(); } else { + tud_vendor_write_clear(); // anything left in the buffer is now thrown out blink_interval_ms = BLINK_MOUNTED; } diff --git a/src/class/vendor/vendor_device.c b/src/class/vendor/vendor_device.c index 9972911e0..ced3c0d71 100644 --- a/src/class/vendor/vendor_device.c +++ b/src/class/vendor/vendor_device.c @@ -160,6 +160,12 @@ uint32_t tud_vendor_n_write_available(uint8_t idx) { vendord_interface_t *p_itf = &_vendord_itf[idx]; return tu_edpt_stream_write_available(&p_itf->stream.tx); } + +uint32_t tud_vendor_n_write_clear(uint8_t idx) { + TU_VERIFY(idx < CFG_TUD_VENDOR, 0); + vendord_interface_t *p_itf = &_vendord_itf[idx]; + return tu_edpt_stream_count_and_clear(&p_itf->stream.tx); +} #endif //--------------------------------------------------------------------+ diff --git a/src/class/vendor/vendor_device.h b/src/class/vendor/vendor_device.h index d59c885d2..878d7aee0 100644 --- a/src/class/vendor/vendor_device.h +++ b/src/class/vendor/vendor_device.h @@ -91,6 +91,9 @@ uint32_t tud_vendor_n_write_flush(uint8_t idx); // Return number of bytes available for writing in TX FIFO uint32_t tud_vendor_n_write_available(uint8_t idx); + +// Clear the write buffer and return the number of elements cleared +uint32_t tud_vendor_n_write_clear(uint8_t idx); #endif // Write a null-terminated string to TX FIFO @@ -148,6 +151,10 @@ TU_ATTR_ALWAYS_INLINE static inline uint32_t tud_vendor_write_flush(void) { TU_ATTR_ALWAYS_INLINE static inline uint32_t tud_vendor_write_available(void) { return tud_vendor_n_write_available(0); } + +TU_ATTR_ALWAYS_INLINE static inline uint32_t tud_vendor_write_clear(void) { + return tud_vendor_n_write_clear(0); +} #endif // backward compatible diff --git a/src/common/tusb_fifo.c b/src/common/tusb_fifo.c index e97b6ccce..28086be0b 100644 --- a/src/common/tusb_fifo.c +++ b/src/common/tusb_fifo.c @@ -95,6 +95,22 @@ void tu_fifo_clear(tu_fifo_t *f) { ff_unlock(f->mutex_rd); } +// synchronously count then clear fifo, returning the count +uint16_t tu_fifo_count_and_clear(tu_fifo_t *f) { + ff_lock(f->mutex_wr); + ff_lock(f->mutex_rd); + + uint16_t cnt = tu_fifo_count(f); + + f->rd_idx = 0; + f->wr_idx = 0; + + ff_unlock(f->mutex_wr); + ff_unlock(f->mutex_rd); + + return cnt; +} + // Change the fifo overwritable mode void tu_fifo_set_overwritable(tu_fifo_t *f, bool overwritable) { if (f->overwritable == overwritable) { diff --git a/src/common/tusb_fifo.h b/src/common/tusb_fifo.h index 2e2a0db6f..75eeded01 100644 --- a/src/common/tusb_fifo.h +++ b/src/common/tusb_fifo.h @@ -159,6 +159,7 @@ typedef enum { bool tu_fifo_config(tu_fifo_t *f, void *buffer, uint16_t depth, uint16_t item_size, bool overwritable); void tu_fifo_set_overwritable(tu_fifo_t *f, bool overwritable); void tu_fifo_clear(tu_fifo_t *f); +uint16_t tu_fifo_count_and_clear(tu_fifo_t *f); #if OSAL_MUTEX_REQUIRED TU_ATTR_ALWAYS_INLINE static inline diff --git a/src/common/tusb_private.h b/src/common/tusb_private.h index 10e12c2af..0c1709d49 100644 --- a/src/common/tusb_private.h +++ b/src/common/tusb_private.h @@ -144,6 +144,10 @@ TU_ATTR_ALWAYS_INLINE static inline void tu_edpt_stream_clear(tu_edpt_stream_t * tu_fifo_clear(&s->ff); } +TU_ATTR_ALWAYS_INLINE static inline uint16_t tu_edpt_stream_count_and_clear(tu_edpt_stream_t *s) { + return tu_fifo_count_and_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); } From b7463bad87456303c0b68bda120a300b343f45fa Mon Sep 17 00:00:00 2001 From: James Smith <{ID}+{username}@users.noreply.github.com> Date: Sun, 21 Dec 2025 06:41:57 -0700 Subject: [PATCH 2/3] Removed tu_fifo_count_and_clear --- src/class/vendor/vendor_device.c | 4 +++- src/common/tusb_fifo.c | 16 ---------------- src/common/tusb_fifo.h | 1 - src/common/tusb_private.h | 4 ---- 4 files changed, 3 insertions(+), 22 deletions(-) diff --git a/src/class/vendor/vendor_device.c b/src/class/vendor/vendor_device.c index ced3c0d71..762cbeebc 100644 --- a/src/class/vendor/vendor_device.c +++ b/src/class/vendor/vendor_device.c @@ -164,7 +164,9 @@ uint32_t tud_vendor_n_write_available(uint8_t idx) { uint32_t tud_vendor_n_write_clear(uint8_t idx) { TU_VERIFY(idx < CFG_TUD_VENDOR, 0); vendord_interface_t *p_itf = &_vendord_itf[idx]; - return tu_edpt_stream_count_and_clear(&p_itf->stream.tx); + uint32_t cnt = tu_edpt_stream_read_available(&p_itf->stream.tx); + tu_edpt_stream_clear(&p_itf->stream.tx); + return cnt; } #endif diff --git a/src/common/tusb_fifo.c b/src/common/tusb_fifo.c index 28086be0b..e97b6ccce 100644 --- a/src/common/tusb_fifo.c +++ b/src/common/tusb_fifo.c @@ -95,22 +95,6 @@ void tu_fifo_clear(tu_fifo_t *f) { ff_unlock(f->mutex_rd); } -// synchronously count then clear fifo, returning the count -uint16_t tu_fifo_count_and_clear(tu_fifo_t *f) { - ff_lock(f->mutex_wr); - ff_lock(f->mutex_rd); - - uint16_t cnt = tu_fifo_count(f); - - f->rd_idx = 0; - f->wr_idx = 0; - - ff_unlock(f->mutex_wr); - ff_unlock(f->mutex_rd); - - return cnt; -} - // Change the fifo overwritable mode void tu_fifo_set_overwritable(tu_fifo_t *f, bool overwritable) { if (f->overwritable == overwritable) { diff --git a/src/common/tusb_fifo.h b/src/common/tusb_fifo.h index 75eeded01..2e2a0db6f 100644 --- a/src/common/tusb_fifo.h +++ b/src/common/tusb_fifo.h @@ -159,7 +159,6 @@ typedef enum { bool tu_fifo_config(tu_fifo_t *f, void *buffer, uint16_t depth, uint16_t item_size, bool overwritable); void tu_fifo_set_overwritable(tu_fifo_t *f, bool overwritable); void tu_fifo_clear(tu_fifo_t *f); -uint16_t tu_fifo_count_and_clear(tu_fifo_t *f); #if OSAL_MUTEX_REQUIRED TU_ATTR_ALWAYS_INLINE static inline diff --git a/src/common/tusb_private.h b/src/common/tusb_private.h index 0c1709d49..10e12c2af 100644 --- a/src/common/tusb_private.h +++ b/src/common/tusb_private.h @@ -144,10 +144,6 @@ TU_ATTR_ALWAYS_INLINE static inline void tu_edpt_stream_clear(tu_edpt_stream_t * tu_fifo_clear(&s->ff); } -TU_ATTR_ALWAYS_INLINE static inline uint16_t tu_edpt_stream_count_and_clear(tu_edpt_stream_t *s) { - return tu_fifo_count_and_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); } From b53739dee47ae21a21eaa74e9ae6d17d0def32c8 Mon Sep 17 00:00:00 2001 From: James Smith <{ID}+{username}@users.noreply.github.com> Date: Sun, 21 Dec 2025 09:06:05 -0700 Subject: [PATCH 3/3] Change return of tud_vendor_n_write_clear from uint32_t to bool --- src/class/vendor/vendor_device.c | 5 ++--- src/class/vendor/vendor_device.h | 6 +++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/class/vendor/vendor_device.c b/src/class/vendor/vendor_device.c index 762cbeebc..b8e6fec6f 100644 --- a/src/class/vendor/vendor_device.c +++ b/src/class/vendor/vendor_device.c @@ -161,12 +161,11 @@ uint32_t tud_vendor_n_write_available(uint8_t idx) { return tu_edpt_stream_write_available(&p_itf->stream.tx); } -uint32_t tud_vendor_n_write_clear(uint8_t idx) { +bool tud_vendor_n_write_clear(uint8_t idx) { TU_VERIFY(idx < CFG_TUD_VENDOR, 0); vendord_interface_t *p_itf = &_vendord_itf[idx]; - uint32_t cnt = tu_edpt_stream_read_available(&p_itf->stream.tx); tu_edpt_stream_clear(&p_itf->stream.tx); - return cnt; + return true; } #endif diff --git a/src/class/vendor/vendor_device.h b/src/class/vendor/vendor_device.h index 878d7aee0..c3de4c49d 100644 --- a/src/class/vendor/vendor_device.h +++ b/src/class/vendor/vendor_device.h @@ -92,8 +92,8 @@ uint32_t tud_vendor_n_write_flush(uint8_t idx); // Return number of bytes available for writing in TX FIFO uint32_t tud_vendor_n_write_available(uint8_t idx); -// Clear the write buffer and return the number of elements cleared -uint32_t tud_vendor_n_write_clear(uint8_t idx); +// Clear the transmit FIFO +bool tud_vendor_n_write_clear(uint8_t idx); #endif // Write a null-terminated string to TX FIFO @@ -152,7 +152,7 @@ TU_ATTR_ALWAYS_INLINE static inline uint32_t tud_vendor_write_available(void) { return tud_vendor_n_write_available(0); } -TU_ATTR_ALWAYS_INLINE static inline uint32_t tud_vendor_write_clear(void) { +TU_ATTR_ALWAYS_INLINE static inline bool tud_vendor_write_clear(void) { return tud_vendor_n_write_clear(0); } #endif