From 2ef0a614009ee7ac8af6f6762a0b118dcdb888b3 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 7 Feb 2023 20:53:19 +0700 Subject: [PATCH] add tud_cdc_write_auto_flush_level() api --- src/class/cdc/cdc_device.c | 19 +++++++++++--- src/class/cdc/cdc_device.h | 54 ++++++++++++++++++++------------------ 2 files changed, 43 insertions(+), 30 deletions(-) diff --git a/src/class/cdc/cdc_device.c b/src/class/cdc/cdc_device.c index 8d10a416c..29325d81f 100644 --- a/src/class/cdc/cdc_device.c +++ b/src/class/cdc/cdc_device.c @@ -52,7 +52,8 @@ typedef struct uint8_t line_state; /*------------- From this point, data is not cleared by bus reset -------------*/ - char wanted_char; + char wanted_char; + uint16_t write_flush_level; cdc_line_coding_t line_coding; // FIFO @@ -168,9 +169,12 @@ uint32_t tud_cdc_n_write(uint8_t itf, void const* buffer, uint32_t bufsize) cdcd_interface_t* p_cdc = &_cdcd_itf[itf]; uint16_t ret = tu_fifo_write_n(&p_cdc->tx_ff, buffer, (uint16_t) bufsize); - // flush if queue more than packet size - // may need to suppress -Wunreachable-code since most of the time CFG_TUD_CDC_TX_BUFSIZE < BULK_PACKET_SIZE - if ( (tu_fifo_count(&p_cdc->tx_ff) >= BULK_PACKET_SIZE) || ((CFG_TUD_CDC_TX_BUFSIZE < BULK_PACKET_SIZE) && tu_fifo_full(&p_cdc->tx_ff)) ) + // auto flush threshold is bulk packet size if not set by write_auto_flush_level()) + // Note: in rare case where CFG_TUD_CDC_TX_BUFSIZE < BULK_PACKET_SIZE, it would be the TX BUFSIZE + uint16_t const threshold = p_cdc->write_flush_level ? p_cdc->write_flush_level : TU_MIN(BULK_PACKET_SIZE, CFG_TUD_CDC_TX_BUFSIZE); + + // flush if queue more than above threshold + if ( tu_fifo_count(&p_cdc->tx_ff) >= threshold ) { tud_cdc_n_write_flush(itf); } @@ -178,6 +182,13 @@ uint32_t tud_cdc_n_write(uint8_t itf, void const* buffer, uint32_t bufsize) return ret; } +bool tud_cdc_n_write_auto_flush_level(uint8_t itf, uint16_t count) +{ + cdcd_interface_t* p_cdc = &_cdcd_itf[itf]; + p_cdc->write_flush_level = count; + return true; +} + uint32_t tud_cdc_n_write_flush (uint8_t itf) { cdcd_interface_t* p_cdc = &_cdcd_itf[itf]; diff --git a/src/class/cdc/cdc_device.h b/src/class/cdc/cdc_device.h index f8a004df4..34ca8a50a 100644 --- a/src/class/cdc/cdc_device.h +++ b/src/class/cdc/cdc_device.h @@ -45,11 +45,6 @@ extern "C" { #endif -/** \addtogroup CDC_Serial Serial - * @{ - * \defgroup CDC_Serial_Device Device - * @{ */ - //--------------------------------------------------------------------+ // Application API (Multiple Ports) // CFG_TUD_CDC > 1 @@ -87,16 +82,20 @@ bool tud_cdc_n_peek (uint8_t itf, uint8_t* ui8); uint32_t tud_cdc_n_write (uint8_t itf, void const* buffer, uint32_t bufsize); // Write a byte -static inline +TU_ATTR_ALWAYS_INLINE static inline uint32_t tud_cdc_n_write_char (uint8_t itf, char ch); // Write a null-terminated string -static inline +TU_ATTR_ALWAYS_INLINE static inline uint32_t tud_cdc_n_write_str (uint8_t itf, char const* str); -// Force sending data if possible, return number of forced bytes +// Manually force sending data if possible, return number of forced bytes uint32_t tud_cdc_n_write_flush (uint8_t itf); +// Set bytes count for TX FIFO that will be auto flushed when reached. +// Default value is the endpoint packet size +bool tud_cdc_n_write_auto_flush_level(uint8_t itf, uint16_t count); + // Return the number of bytes (characters) available for writing to TX FIFO buffer in a single n_write operation. uint32_t tud_cdc_n_write_available (uint8_t itf); @@ -106,23 +105,24 @@ bool tud_cdc_n_write_clear (uint8_t itf); //--------------------------------------------------------------------+ // Application API (Single Port) //--------------------------------------------------------------------+ -static inline bool tud_cdc_connected (void); -static inline uint8_t tud_cdc_get_line_state (void); -static inline void tud_cdc_get_line_coding (cdc_line_coding_t* coding); -static inline void tud_cdc_set_wanted_char (char wanted); +TU_ATTR_ALWAYS_INLINE static inline bool tud_cdc_connected (void); +TU_ATTR_ALWAYS_INLINE static inline uint8_t tud_cdc_get_line_state (void); +TU_ATTR_ALWAYS_INLINE static inline void tud_cdc_get_line_coding (cdc_line_coding_t* coding); +TU_ATTR_ALWAYS_INLINE static inline void tud_cdc_set_wanted_char (char wanted); -static inline uint32_t tud_cdc_available (void); -static inline int32_t tud_cdc_read_char (void); -static inline uint32_t tud_cdc_read (void* buffer, uint32_t bufsize); -static inline void tud_cdc_read_flush (void); -static inline bool tud_cdc_peek (uint8_t* ui8); +TU_ATTR_ALWAYS_INLINE static inline uint32_t tud_cdc_available (void); +TU_ATTR_ALWAYS_INLINE static inline int32_t tud_cdc_read_char (void); +TU_ATTR_ALWAYS_INLINE static inline uint32_t tud_cdc_read (void* buffer, uint32_t bufsize); +TU_ATTR_ALWAYS_INLINE static inline void tud_cdc_read_flush (void); +TU_ATTR_ALWAYS_INLINE static inline bool tud_cdc_peek (uint8_t* ui8); -static inline uint32_t tud_cdc_write_char (char ch); -static inline uint32_t tud_cdc_write (void const* buffer, uint32_t bufsize); -static inline uint32_t tud_cdc_write_str (char const* str); -static inline uint32_t tud_cdc_write_flush (void); -static inline uint32_t tud_cdc_write_available (void); -static inline bool tud_cdc_write_clear (void); +TU_ATTR_ALWAYS_INLINE static inline uint32_t tud_cdc_write_char (char ch); +TU_ATTR_ALWAYS_INLINE static inline uint32_t tud_cdc_write (void const* buffer, uint32_t bufsize); +TU_ATTR_ALWAYS_INLINE static inline uint32_t tud_cdc_write_str (char const* str); +TU_ATTR_ALWAYS_INLINE static inline uint32_t tud_cdc_write_flush (void); +TU_ATTR_ALWAYS_INLINE static inline bool tud_cdc_write_auto_flush_level(uint16_t count); +TU_ATTR_ALWAYS_INLINE static inline uint32_t tud_cdc_write_available (void); +TU_ATTR_ALWAYS_INLINE static inline bool tud_cdc_write_clear (void); //--------------------------------------------------------------------+ // Application Callback API (weak is optional) @@ -230,6 +230,11 @@ static inline uint32_t tud_cdc_write_flush (void) return tud_cdc_n_write_flush(0); } +static inline bool tud_cdc_write_auto_flush_level(uint16_t count) +{ + return tud_cdc_n_write_auto_flush_level(0, count); +} + static inline uint32_t tud_cdc_write_available(void) { return tud_cdc_n_write_available(0); @@ -240,9 +245,6 @@ static inline bool tud_cdc_write_clear(void) return tud_cdc_n_write_clear(0); } -/** @} */ -/** @} */ - //--------------------------------------------------------------------+ // INTERNAL USBD-CLASS DRIVER API //--------------------------------------------------------------------+