Skip to content

Commit 84edff4

Browse files
committed
add CFG_TUD_VENDOR_RX_MANUAL_XFER per suggestion
1 parent 1465a9b commit 84edff4

File tree

2 files changed

+54
-10
lines changed

2 files changed

+54
-10
lines changed

src/class/vendor/vendor_device.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,15 @@ void tud_vendor_n_read_flush(uint8_t idx) {
136136
}
137137
#endif
138138

139+
#if CFG_TUD_VENDOR_RX_MANUAL_XFER
140+
bool tud_vendor_n_read_xfer(uint8_t idx) {
141+
TU_VERIFY(idx < CFG_TUD_VENDOR);
142+
vendord_interface_t *p_itf = &_vendord_itf[idx];
143+
return tu_edpt_stream_read_xfer(p_itf->rhport, &p_itf->stream.rx);
144+
}
145+
#endif
146+
147+
139148
//--------------------------------------------------------------------+
140149
// Write API
141150
//--------------------------------------------------------------------+
@@ -274,7 +283,9 @@ uint16_t vendord_open(uint8_t rhport, const tusb_desc_interface_t *desc_itf, uin
274283
tu_edpt_stream_t *stream_rx = &p_vendor->stream.rx;
275284
if (stream_rx->ep_addr == 0) {
276285
tu_edpt_stream_open(stream_rx, desc_ep);
286+
#if CFG_TUD_VENDOR_RX_MANUAL_XFER
277287
TU_ASSERT(tu_edpt_stream_read_xfer(rhport, stream_rx) > 0, 0); // prepare for incoming data
288+
#endif
278289
}
279290
}
280291
}
@@ -302,7 +313,9 @@ bool vendord_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint
302313
tud_vendor_rx_cb(idx, NULL, 0);
303314
#endif
304315

316+
#if CFG_TUD_VENDOR_RX_MANUAL_XFER
305317
tu_edpt_stream_read_xfer(rhport, &p_vendor->stream.rx); // prepare next data
318+
#endif
306319
} else if (ep_addr == p_vendor->stream.tx.ep_addr) {
307320
// Send complete
308321
tud_vendor_tx_cb(idx, (uint16_t)xferred_bytes);

src/class/vendor/vendor_device.h

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,15 @@
2727
#ifndef TUSB_VENDOR_DEVICE_H_
2828
#define TUSB_VENDOR_DEVICE_H_
2929

30+
#ifdef __cplusplus
31+
extern "C" {
32+
#endif
33+
3034
#include "common/tusb_common.h"
3135

36+
//--------------------------------------------------------------------+
37+
// Configuration
38+
//--------------------------------------------------------------------+
3239
#ifndef CFG_TUD_VENDOR_EPSIZE
3340
#define CFG_TUD_VENDOR_EPSIZE 64
3441
#endif
@@ -43,30 +50,53 @@
4350
#define CFG_TUD_VENDOR_TX_BUFSIZE 64
4451
#endif
4552

46-
#ifdef __cplusplus
47-
extern "C" {
53+
// Application will manually schedule RX transfer. This can be useful when using with non-fifo (buffered) mode
54+
// i.e. CFG_TUD_VENDOR_RX_BUFSIZE = 0
55+
#ifndef CFG_TUD_VENDOR_RX_MANUAL_XFER
56+
#define CFG_TUD_VENDOR_RX_MANUAL_XFER 0
4857
#endif
4958

5059
//--------------------------------------------------------------------+
5160
// Application API (Multiple Interfaces) i.e CFG_TUD_VENDOR > 1
5261
//--------------------------------------------------------------------+
53-
bool tud_vendor_n_mounted(uint8_t idx);
62+
63+
// Return whether the vendor interface is mounted
64+
bool tud_vendor_n_mounted(uint8_t idx);
5465

5566
#if CFG_TUD_VENDOR_RX_BUFSIZE > 0
67+
// Return number of available bytes for reading
5668
uint32_t tud_vendor_n_available(uint8_t idx);
57-
bool tud_vendor_n_peek(uint8_t idx, uint8_t *ui8);
69+
70+
// Peek a byte from RX buffer
71+
bool tud_vendor_n_peek(uint8_t idx, uint8_t *ui8);
72+
73+
// Read from RX FIFO
5874
uint32_t tud_vendor_n_read(uint8_t idx, void *buffer, uint32_t bufsize);
75+
76+
// Discard count bytes in RX FIFO
5977
uint32_t tud_vendor_n_read_discard(uint8_t idx, uint32_t count);
60-
void tud_vendor_n_read_flush(uint8_t idx);
78+
79+
// Flush (clear) RX FIFO
80+
void tud_vendor_n_read_flush(uint8_t idx);
81+
#endif
82+
83+
#if CFG_TUD_VENDOR_RX_MANUAL_XFER
84+
// Start a new RX transfer to fill the RX FIFO, return false if previous transfer is still ongoing
85+
bool tud_vendor_n_read_xfer(uint8_t idx);
6186
#endif
6287

88+
// Write to TX FIFO. This can be buffered and not sent immediately unless buffered bytes >= USB endpoint size
6389
uint32_t tud_vendor_n_write(uint8_t idx, const void *buffer, uint32_t bufsize);
6490

6591
#if CFG_TUD_VENDOR_TX_BUFSIZE > 0
92+
// Force sending buffered data, return number of bytes sent
6693
uint32_t tud_vendor_n_write_flush(uint8_t idx);
94+
95+
// Return number of bytes available for writing in TX FIFO
6796
uint32_t tud_vendor_n_write_available(uint8_t idx);
6897
#endif
6998

99+
// Write a null-terminated string to TX FIFO
70100
TU_ATTR_ALWAYS_INLINE static inline uint32_t tud_vendor_n_write_str(uint8_t idx, const char *str) {
71101
return tud_vendor_n_write(idx, str, strlen(str));
72102
}
@@ -103,6 +133,12 @@ TU_ATTR_ALWAYS_INLINE static inline void tud_vendor_read_flush(void) {
103133
}
104134
#endif
105135

136+
#if CFG_TUD_VENDOR_RX_MANUAL_XFER
137+
TU_ATTR_ALWAYS_INLINE static inline bool tud_vendor_read_xfer(void) {
138+
return tud_vendor_n_read_xfer(0);
139+
}
140+
#endif
141+
106142
TU_ATTR_ALWAYS_INLINE static inline uint32_t tud_vendor_write(const void *buffer, uint32_t bufsize) {
107143
return tud_vendor_n_write(0, buffer, bufsize);
108144
}
@@ -136,11 +172,6 @@ void tud_vendor_rx_cb(uint8_t idx, const uint8_t *buffer, uint32_t bufsize);
136172
// Invoked when tx transfer is finished
137173
void tud_vendor_tx_cb(uint8_t idx, uint32_t sent_bytes);
138174

139-
//--------------------------------------------------------------------+
140-
// Inline Functions
141-
//--------------------------------------------------------------------+
142-
143-
144175
//--------------------------------------------------------------------+
145176
// Internal Class Driver API
146177
//--------------------------------------------------------------------+

0 commit comments

Comments
 (0)