Skip to content

Commit 4c6bb16

Browse files
authored
Merge pull request hathach#1289 from kasjer/kasjer/vendor-update
vendor: Write improvements
2 parents f2c276d + 4ca2215 commit 4c6bb16

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/class/vendor/vendor_device.c

Lines changed: 14 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

@@ -247,6 +257,7 @@ bool vendord_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint
247257
}
248258
else if ( ep_addr == p_itf->ep_in )
249259
{
260+
if (tud_vendor_tx_cb) tud_vendor_tx_cb(itf, xferred_bytes);
250261
// Send complete, try to send more if possible
251262
maybe_transmit(p_itf);
252263
}

src/class/vendor/vendor_device.h

Lines changed: 9 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,13 +65,16 @@ 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)
7072
//--------------------------------------------------------------------+
7173

7274
// Invoked when received new data
7375
TU_ATTR_WEAK void tud_vendor_rx_cb(uint8_t itf);
76+
// Invoked when last rx transfer finished
77+
TU_ATTR_WEAK void tud_vendor_tx_cb(uint8_t itf, uint32_t sent_bytes);
7478

7579
//--------------------------------------------------------------------+
7680
// Inline Functions
@@ -121,6 +125,11 @@ static inline uint32_t tud_vendor_write_available (void)
121125
return tud_vendor_n_write_available(0);
122126
}
123127

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

0 commit comments

Comments
 (0)