Skip to content

Commit 4ca2215

Browse files
committed
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.
1 parent d069ea1 commit 4ca2215

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/class/vendor/vendor_device.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ void tud_vendor_n_read_flush (uint8_t itf)
113113
//--------------------------------------------------------------------+
114114
// Write API
115115
//--------------------------------------------------------------------+
116-
static bool maybe_transmit(vendord_interface_t* p_itf)
116+
static uint16_t maybe_transmit(vendord_interface_t* p_itf)
117117
{
118118
// skip if previous transfer not complete
119119
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)
123123
{
124124
TU_ASSERT( usbd_edpt_xfer(TUD_OPT_RHPORT, p_itf->ep_in, p_itf->epin_buf, count) );
125125
}
126-
return true;
126+
return count;
127127
}
128128

129129
uint32_t tud_vendor_n_write (uint8_t itf, void const* buffer, uint32_t bufsize)
130130
{
131131
vendord_interface_t* p_itf = &_vendord_itf[itf];
132132
uint16_t ret = tu_fifo_write_n(&p_itf->tx_ff, buffer, bufsize);
133-
maybe_transmit(p_itf);
133+
if (tu_fifo_count(&p_itf->tx_ff) >= CFG_TUD_VENDOR_EPSIZE) {
134+
maybe_transmit(p_itf);
135+
}
136+
return ret;
137+
}
138+
139+
uint32_t tud_vendor_n_flush (uint8_t itf)
140+
{
141+
vendord_interface_t* p_itf = &_vendord_itf[itf];
142+
uint32_t ret = maybe_transmit(p_itf);
143+
134144
return ret;
135145
}
136146

src/class/vendor/vendor_device.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ uint32_t tud_vendor_n_write_available (uint8_t itf);
5252

5353
static inline
5454
uint32_t tud_vendor_n_write_str (uint8_t itf, char const* str);
55+
uint32_t tud_vendor_n_flush (uint8_t itf);
5556

5657
//--------------------------------------------------------------------+
5758
// Application API (Single Port)
@@ -64,6 +65,7 @@ static inline void tud_vendor_read_flush (void);
6465
static inline uint32_t tud_vendor_write (void const* buffer, uint32_t bufsize);
6566
static inline uint32_t tud_vendor_write_str (char const* str);
6667
static inline uint32_t tud_vendor_write_available (void);
68+
static inline uint32_t tud_vendor_flush (void);
6769

6870
//--------------------------------------------------------------------+
6971
// Application Callback API (weak is optional)
@@ -123,6 +125,11 @@ static inline uint32_t tud_vendor_write_available (void)
123125
return tud_vendor_n_write_available(0);
124126
}
125127

128+
static inline uint32_t tud_vendor_flush (void)
129+
{
130+
return tud_vendor_n_flush(0);
131+
}
132+
126133
//--------------------------------------------------------------------+
127134
// Internal Class Driver API
128135
//--------------------------------------------------------------------+

0 commit comments

Comments
 (0)