Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vendor: Write improvements #1289

Merged
merged 2 commits into from
Mar 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/class/vendor/vendor_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) );
Expand All @@ -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;
}

Expand Down Expand Up @@ -247,6 +257,7 @@ bool vendord_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint
}
else if ( ep_addr == p_itf->ep_in )
{
if (tud_vendor_tx_cb) tud_vendor_tx_cb(itf, xferred_bytes);
// Send complete, try to send more if possible
maybe_transmit(p_itf);
}
Expand Down
9 changes: 9 additions & 0 deletions src/class/vendor/vendor_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -64,13 +65,16 @@ 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)
//--------------------------------------------------------------------+

// Invoked when received new data
TU_ATTR_WEAK void tud_vendor_rx_cb(uint8_t itf);
// Invoked when last rx transfer finished
TU_ATTR_WEAK void tud_vendor_tx_cb(uint8_t itf, uint32_t sent_bytes);

//--------------------------------------------------------------------+
// Inline Functions
Expand Down Expand Up @@ -121,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
//--------------------------------------------------------------------+
Expand Down