mirror of
https://github.com/hathach/tinyusb.git
synced 2026-02-05 10:45:45 +00:00
@ -53,15 +53,13 @@ typedef struct TU_ATTR_PACKED {
|
||||
}tu_edpt_state_t;
|
||||
|
||||
typedef struct {
|
||||
struct TU_ATTR_PACKED {
|
||||
bool is_host : 1; // 1: host, 0: device
|
||||
bool is_mps512 : 1; // 1: 512, 0: 64 since stream is used for Bulk only
|
||||
};
|
||||
bool is_host; // 1: host, 0: device
|
||||
uint8_t ep_addr;
|
||||
uint16_t ep_bufsize;
|
||||
|
||||
uint8_t *ep_buf; // set to NULL to use xfer_fifo when CFG_TUD_EDPT_DEDICATED_HWFIFO = 1
|
||||
tu_fifo_t ff;
|
||||
uint16_t mps;
|
||||
|
||||
// mutex: read if rx, otherwise write
|
||||
OSAL_MUTEX_DEF(ff_mutexdef);
|
||||
@ -101,7 +99,7 @@ bool tu_edpt_stream_deinit(tu_edpt_stream_t* s);
|
||||
// Open an endpoint stream
|
||||
TU_ATTR_ALWAYS_INLINE static inline void tu_edpt_stream_open(tu_edpt_stream_t* s, tusb_desc_endpoint_t const *desc_ep) {
|
||||
s->ep_addr = desc_ep->bEndpointAddress;
|
||||
s->is_mps512 = tu_edpt_packet_size(desc_ep) == 512;
|
||||
s->mps = tu_edpt_packet_size(desc_ep);
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool tu_edpt_stream_is_opened(const tu_edpt_stream_t *s) {
|
||||
|
||||
17
src/tusb.c
17
src/tusb.c
@ -415,8 +415,7 @@ TU_ATTR_ALWAYS_INLINE static inline bool stream_release(uint8_t hwid, tu_edpt_st
|
||||
//--------------------------------------------------------------------+
|
||||
bool tu_edpt_stream_write_zlp_if_needed(uint8_t hwid, tu_edpt_stream_t* s, uint32_t last_xferred_bytes) {
|
||||
// ZLP condition: no pending data, last transferred bytes is multiple of packet size
|
||||
const uint16_t mps = s->is_mps512 ? TUSB_EPSIZE_BULK_HS : TUSB_EPSIZE_BULK_FS;
|
||||
TU_VERIFY(tu_fifo_empty(&s->ff) && last_xferred_bytes > 0 && (0 == (last_xferred_bytes & (mps - 1))));
|
||||
TU_VERIFY(tu_fifo_empty(&s->ff) && last_xferred_bytes > 0 && (0 == (last_xferred_bytes & (s->mps - 1))));
|
||||
TU_VERIFY(stream_claim(hwid, s));
|
||||
TU_ASSERT(stream_xfer(hwid, s, 0));
|
||||
return true;
|
||||
@ -469,8 +468,7 @@ uint32_t tu_edpt_stream_write(uint8_t hwid, tu_edpt_stream_t *s, const void *buf
|
||||
|
||||
// flush if fifo has more than packet size or
|
||||
// in rare case: fifo depth is configured too small (which never reach packet size)
|
||||
const uint16_t mps = s->is_mps512 ? TUSB_EPSIZE_BULK_HS : TUSB_EPSIZE_BULK_FS;
|
||||
if ((tu_fifo_count(&s->ff) >= mps) || (tu_fifo_depth(&s->ff) < mps)) {
|
||||
if ((tu_fifo_count(&s->ff) >= s->mps) || (tu_fifo_depth(&s->ff) < s->mps)) {
|
||||
tu_edpt_stream_write_xfer(hwid, s);
|
||||
}
|
||||
return ret;
|
||||
@ -507,21 +505,22 @@ uint32_t tu_edpt_stream_read_xfer(uint8_t hwid, tu_edpt_stream_t* s) {
|
||||
TU_ASSERT(stream_xfer(hwid, s, s->ep_bufsize), 0);
|
||||
return s->ep_bufsize;
|
||||
} else {
|
||||
const uint16_t mps = s->is_mps512 ? TUSB_EPSIZE_BULK_HS : TUSB_EPSIZE_BULK_FS;
|
||||
uint16_t available = tu_fifo_remaining(&s->ff);
|
||||
|
||||
// Prepare for incoming data but only allow what we can store in the ring buffer.
|
||||
// TODO Actually we can still carry out the transfer, keeping count of received bytes
|
||||
// and slowly move it to the FIFO when read().
|
||||
// This pre-check reduces endpoint claiming
|
||||
TU_VERIFY(available >= mps);
|
||||
TU_VERIFY(available >= s->mps);
|
||||
TU_VERIFY(stream_claim(hwid, s), 0);
|
||||
available = tu_fifo_remaining(&s->ff); // re-get available since fifo can be changed
|
||||
|
||||
if (available >= mps) {
|
||||
if (available >= s->mps) {
|
||||
// multiple of packet size limit by ep bufsize
|
||||
uint16_t count = (uint16_t) (available & ~(mps - 1));
|
||||
count = tu_min16(count, s->ep_bufsize);
|
||||
uint16_t count = (uint16_t) (available & ~(s->mps - 1));
|
||||
if (s->ep_buf != NULL) {
|
||||
count = tu_min16(count, s->ep_bufsize);
|
||||
}
|
||||
TU_ASSERT(stream_xfer(hwid, s, count), 0);
|
||||
return count;
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user