vendor: Add tx flush functionality

So far tud_vendor_n_write() always flushed data.
It requires to have whole vendor packed constructed
before in one buffer.

With this change data do get flushed when endpoint size
is filled on write, when there is no enough data to
fill endpoint data is not sent and subsequent calls to
write functions can add more bytes.

Vendor code needs to call tud_vendor_n_flush() when packet is
ready to be sent.
This commit is contained in:
Jerzy Kasenberg 2021-08-25 10:57:35 +02:00
parent d069ea1421
commit 4ca2215684
2 changed files with 20 additions and 3 deletions

View File

@ -113,7 +113,7 @@ void tud_vendor_n_read_flush (uint8_t itf)
//--------------------------------------------------------------------+
// Write API
//--------------------------------------------------------------------+
static bool maybe_transmit(vendord_interface_t* p_itf)
static uint16_t maybe_transmit(vendord_interface_t* p_itf)
{
// skip if previous transfer not complete
TU_VERIFY( !usbd_edpt_busy(TUD_OPT_RHPORT, p_itf->ep_in) );
@ -123,14 +123,24 @@ static bool maybe_transmit(vendord_interface_t* p_itf)
{
TU_ASSERT( usbd_edpt_xfer(TUD_OPT_RHPORT, p_itf->ep_in, p_itf->epin_buf, count) );
}
return true;
return count;
}
uint32_t tud_vendor_n_write (uint8_t itf, void const* buffer, uint32_t bufsize)
{
vendord_interface_t* p_itf = &_vendord_itf[itf];
uint16_t ret = tu_fifo_write_n(&p_itf->tx_ff, buffer, bufsize);
maybe_transmit(p_itf);
if (tu_fifo_count(&p_itf->tx_ff) >= CFG_TUD_VENDOR_EPSIZE) {
maybe_transmit(p_itf);
}
return ret;
}
uint32_t tud_vendor_n_flush (uint8_t itf)
{
vendord_interface_t* p_itf = &_vendord_itf[itf];
uint32_t ret = maybe_transmit(p_itf);
return ret;
}

View File

@ -52,6 +52,7 @@ uint32_t tud_vendor_n_write_available (uint8_t itf);
static inline
uint32_t tud_vendor_n_write_str (uint8_t itf, char const* str);
uint32_t tud_vendor_n_flush (uint8_t itf);
//--------------------------------------------------------------------+
// Application API (Single Port)
@ -64,6 +65,7 @@ static inline void tud_vendor_read_flush (void);
static inline uint32_t tud_vendor_write (void const* buffer, uint32_t bufsize);
static inline uint32_t tud_vendor_write_str (char const* str);
static inline uint32_t tud_vendor_write_available (void);
static inline uint32_t tud_vendor_flush (void);
//--------------------------------------------------------------------+
// Application Callback API (weak is optional)
@ -123,6 +125,11 @@ static inline uint32_t tud_vendor_write_available (void)
return tud_vendor_n_write_available(0);
}
static inline uint32_t tud_vendor_flush (void)
{
return tud_vendor_n_flush(0);
}
//--------------------------------------------------------------------+
// Internal Class Driver API
//--------------------------------------------------------------------+