add tud_cdc_write_auto_flush_level() api

This commit is contained in:
hathach 2023-02-07 20:53:19 +07:00
parent c0d79457f6
commit 2ef0a61400
2 changed files with 43 additions and 30 deletions

View File

@ -52,7 +52,8 @@ typedef struct
uint8_t line_state; uint8_t line_state;
/*------------- From this point, data is not cleared by bus reset -------------*/ /*------------- 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; cdc_line_coding_t line_coding;
// FIFO // 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]; cdcd_interface_t* p_cdc = &_cdcd_itf[itf];
uint16_t ret = tu_fifo_write_n(&p_cdc->tx_ff, buffer, (uint16_t) bufsize); uint16_t ret = tu_fifo_write_n(&p_cdc->tx_ff, buffer, (uint16_t) bufsize);
// flush if queue more than packet size // auto flush threshold is bulk packet size if not set by write_auto_flush_level())
// may need to suppress -Wunreachable-code since most of the time CFG_TUD_CDC_TX_BUFSIZE < BULK_PACKET_SIZE // Note: in rare case where CFG_TUD_CDC_TX_BUFSIZE < BULK_PACKET_SIZE, it would be the TX BUFSIZE
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)) ) 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); 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; 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) uint32_t tud_cdc_n_write_flush (uint8_t itf)
{ {
cdcd_interface_t* p_cdc = &_cdcd_itf[itf]; cdcd_interface_t* p_cdc = &_cdcd_itf[itf];

View File

@ -45,11 +45,6 @@
extern "C" { extern "C" {
#endif #endif
/** \addtogroup CDC_Serial Serial
* @{
* \defgroup CDC_Serial_Device Device
* @{ */
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// Application API (Multiple Ports) // Application API (Multiple Ports)
// CFG_TUD_CDC > 1 // 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); uint32_t tud_cdc_n_write (uint8_t itf, void const* buffer, uint32_t bufsize);
// Write a byte // Write a byte
static inline TU_ATTR_ALWAYS_INLINE static inline
uint32_t tud_cdc_n_write_char (uint8_t itf, char ch); uint32_t tud_cdc_n_write_char (uint8_t itf, char ch);
// Write a null-terminated string // 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); 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); 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. // 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); 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) // Application API (Single Port)
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
static inline bool tud_cdc_connected (void); TU_ATTR_ALWAYS_INLINE static inline bool tud_cdc_connected (void);
static inline uint8_t tud_cdc_get_line_state (void); TU_ATTR_ALWAYS_INLINE static inline uint8_t tud_cdc_get_line_state (void);
static inline void tud_cdc_get_line_coding (cdc_line_coding_t* coding); TU_ATTR_ALWAYS_INLINE 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 void tud_cdc_set_wanted_char (char wanted);
static inline uint32_t tud_cdc_available (void); TU_ATTR_ALWAYS_INLINE static inline uint32_t tud_cdc_available (void);
static inline int32_t tud_cdc_read_char (void); TU_ATTR_ALWAYS_INLINE static inline int32_t tud_cdc_read_char (void);
static inline uint32_t tud_cdc_read (void* buffer, uint32_t bufsize); TU_ATTR_ALWAYS_INLINE static inline uint32_t tud_cdc_read (void* buffer, uint32_t bufsize);
static inline void tud_cdc_read_flush (void); TU_ATTR_ALWAYS_INLINE static inline void tud_cdc_read_flush (void);
static inline bool tud_cdc_peek (uint8_t* ui8); TU_ATTR_ALWAYS_INLINE static inline bool tud_cdc_peek (uint8_t* ui8);
static inline uint32_t tud_cdc_write_char (char ch); TU_ATTR_ALWAYS_INLINE static inline uint32_t tud_cdc_write_char (char ch);
static inline uint32_t tud_cdc_write (void const* buffer, uint32_t bufsize); TU_ATTR_ALWAYS_INLINE static inline uint32_t tud_cdc_write (void const* buffer, uint32_t bufsize);
static inline uint32_t tud_cdc_write_str (char const* str); TU_ATTR_ALWAYS_INLINE static inline uint32_t tud_cdc_write_str (char const* str);
static inline uint32_t tud_cdc_write_flush (void); TU_ATTR_ALWAYS_INLINE static inline uint32_t tud_cdc_write_flush (void);
static inline uint32_t tud_cdc_write_available (void); TU_ATTR_ALWAYS_INLINE static inline bool tud_cdc_write_auto_flush_level(uint16_t count);
static inline bool tud_cdc_write_clear (void); 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) // 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); 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) static inline uint32_t tud_cdc_write_available(void)
{ {
return tud_cdc_n_write_available(0); 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); return tud_cdc_n_write_clear(0);
} }
/** @} */
/** @} */
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// INTERNAL USBD-CLASS DRIVER API // INTERNAL USBD-CLASS DRIVER API
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+