update edpt_stream to support non-buffered (no fifo) mode

This commit is contained in:
hathach
2024-09-10 16:15:51 +07:00
parent 867f17acea
commit c0030810dd
2 changed files with 72 additions and 44 deletions

View File

@@ -129,11 +129,9 @@ uint32_t tu_edpt_stream_write_xfer(tu_edpt_stream_t* s);
// Start an zero-length packet if needed // Start an zero-length packet if needed
bool tu_edpt_stream_write_zlp_if_needed(tu_edpt_stream_t* s, uint32_t last_xferred_bytes); bool tu_edpt_stream_write_zlp_if_needed(tu_edpt_stream_t* s, uint32_t last_xferred_bytes);
// Get the number of bytes available for writing // Get the number of bytes available for writing to FIFO
TU_ATTR_ALWAYS_INLINE static inline // Note: if no fifo, return endpoint size if not busy, 0 otherwise
uint32_t tu_edpt_stream_write_available(tu_edpt_stream_t* s) { uint32_t tu_edpt_stream_write_available(tu_edpt_stream_t* s);
return (uint32_t) tu_fifo_remaining(&s->ff);
}
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// Stream Read // Stream Read
@@ -148,13 +146,15 @@ uint32_t tu_edpt_stream_read_xfer(tu_edpt_stream_t* s);
// Must be called in the transfer complete callback // Must be called in the transfer complete callback
TU_ATTR_ALWAYS_INLINE static inline TU_ATTR_ALWAYS_INLINE static inline
void tu_edpt_stream_read_xfer_complete(tu_edpt_stream_t* s, uint32_t xferred_bytes) { void tu_edpt_stream_read_xfer_complete(tu_edpt_stream_t* s, uint32_t xferred_bytes) {
tu_fifo_write_n(&s->ff, s->ep_buf, (uint16_t) xferred_bytes); if (tu_fifo_depth(&s->ff)) {
tu_fifo_write_n(&s->ff, s->ep_buf, (uint16_t) xferred_bytes);
}
} }
// Same as tu_edpt_stream_read_xfer_complete but skip the first n bytes // Same as tu_edpt_stream_read_xfer_complete but skip the first n bytes
TU_ATTR_ALWAYS_INLINE static inline TU_ATTR_ALWAYS_INLINE static inline
void tu_edpt_stream_read_xfer_complete_offset(tu_edpt_stream_t* s, uint32_t xferred_bytes, uint32_t skip_offset) { void tu_edpt_stream_read_xfer_complete_offset(tu_edpt_stream_t* s, uint32_t xferred_bytes, uint32_t skip_offset) {
if (skip_offset < xferred_bytes) { if (tu_fifo_depth(&s->ff) && (skip_offset < xferred_bytes)) {
tu_fifo_write_n(&s->ff, s->ep_buf + skip_offset, (uint16_t) (xferred_bytes - skip_offset)); tu_fifo_write_n(&s->ff, s->ep_buf + skip_offset, (uint16_t) (xferred_bytes - skip_offset));
} }
} }

View File

@@ -239,8 +239,7 @@ bool tu_edpt_stream_deinit(tu_edpt_stream_t* s) {
return true; return true;
} }
TU_ATTR_ALWAYS_INLINE static inline TU_ATTR_ALWAYS_INLINE static inline bool stream_claim(tu_edpt_stream_t* s) {
bool stream_claim(tu_edpt_stream_t* s) {
if (s->is_host) { if (s->is_host) {
#if CFG_TUH_ENABLED #if CFG_TUH_ENABLED
return usbh_edpt_claim(s->daddr, s->ep_addr); return usbh_edpt_claim(s->daddr, s->ep_addr);
@@ -253,8 +252,7 @@ bool stream_claim(tu_edpt_stream_t* s) {
return false; return false;
} }
TU_ATTR_ALWAYS_INLINE static inline TU_ATTR_ALWAYS_INLINE static inline bool stream_xfer(tu_edpt_stream_t* s, uint16_t count) {
bool stream_xfer(tu_edpt_stream_t* s, uint16_t count) {
if (s->is_host) { if (s->is_host) {
#if CFG_TUH_ENABLED #if CFG_TUH_ENABLED
return usbh_edpt_xfer(s->daddr, s->ep_addr, count ? s->ep_buf : NULL, count); return usbh_edpt_xfer(s->daddr, s->ep_addr, count ? s->ep_buf : NULL, count);
@@ -267,8 +265,7 @@ bool stream_xfer(tu_edpt_stream_t* s, uint16_t count) {
return false; return false;
} }
TU_ATTR_ALWAYS_INLINE static inline TU_ATTR_ALWAYS_INLINE static inline bool stream_release(tu_edpt_stream_t* s) {
bool stream_release(tu_edpt_stream_t* s) {
if (s->is_host) { if (s->is_host) {
#if CFG_TUH_ENABLED #if CFG_TUH_ENABLED
return usbh_edpt_release(s->daddr, s->ep_addr); return usbh_edpt_release(s->daddr, s->ep_addr);
@@ -296,7 +293,6 @@ uint32_t tu_edpt_stream_write_xfer(tu_edpt_stream_t* s) {
// skip if no data // skip if no data
TU_VERIFY(tu_fifo_count(&s->ff), 0); TU_VERIFY(tu_fifo_count(&s->ff), 0);
// Claim the endpoint
TU_VERIFY(stream_claim(s), 0); TU_VERIFY(stream_claim(s), 0);
// Pull data from FIFO -> EP buf // Pull data from FIFO -> EP buf
@@ -315,46 +311,78 @@ uint32_t tu_edpt_stream_write_xfer(tu_edpt_stream_t* s) {
uint32_t tu_edpt_stream_write(tu_edpt_stream_t* s, void const* buffer, uint32_t bufsize) { uint32_t tu_edpt_stream_write(tu_edpt_stream_t* s, void const* buffer, uint32_t bufsize) {
TU_VERIFY(bufsize); // TODO support ZLP TU_VERIFY(bufsize); // TODO support ZLP
const uint16_t ret = tu_fifo_write_n(&s->ff, buffer, (uint16_t) bufsize);
// flush if fifo has more than packet size or if (0 == tu_fifo_depth(&s->ff)) {
// in rare case: fifo depth is configured too small (which never reach packet size) // no fifo for buffered
if ((tu_fifo_count(&s->ff) >= s->ep_packetsize) || (tu_fifo_depth(&s->ff) < s->ep_packetsize)) { TU_VERIFY(stream_claim(s), 0);
tu_edpt_stream_write_xfer(s); const uint32_t xact_len = tu_min32(bufsize, s->ep_bufsize);
memcpy(s->ep_buf, buffer, xact_len);
TU_ASSERT(stream_xfer(s, xact_len), 0);
return xact_len;
} else {
const uint16_t ret = tu_fifo_write_n(&s->ff, buffer, (uint16_t) bufsize);
// flush if fifo has more than packet size or
// in rare case: fifo depth is configured too small (which never reach packet size)
if ((tu_fifo_count(&s->ff) >= s->ep_packetsize) || (tu_fifo_depth(&s->ff) < s->ep_packetsize)) {
tu_edpt_stream_write_xfer(s);
}
return ret;
} }
}
return ret; uint32_t tu_edpt_stream_write_available(tu_edpt_stream_t* s) {
if (tu_fifo_depth(&s->ff)) {
return (uint32_t) tu_fifo_remaining(&s->ff);
} else {
bool is_busy = true;
if (s->is_host) {
#if CFG_TUH_ENABLED
is_busy = usbh_edpt_busy(s->daddr, s->ep_addr);
#endif
} else {
#if CFG_TUD_ENABLED
is_busy = usbd_edpt_busy(s->rhport, s->ep_addr);
#endif
}
return is_busy ? 0 : s->ep_bufsize;
}
} }
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// Stream Read // Stream Read
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
uint32_t tu_edpt_stream_read_xfer(tu_edpt_stream_t* s) { uint32_t tu_edpt_stream_read_xfer(tu_edpt_stream_t* s) {
uint16_t available = tu_fifo_remaining(&s->ff); if (0 == tu_fifo_depth(&s->ff)) {
// no fifo for buffered
// Prepare for incoming data but only allow what we can store in the ring buffer. TU_VERIFY(stream_claim(s), 0);
// TODO Actually we can still carry out the transfer, keeping count of received bytes TU_ASSERT(stream_xfer(s, s->ep_bufsize), 0);
// and slowly move it to the FIFO when read(). return s->ep_bufsize;
// This pre-check reduces endpoint claiming
TU_VERIFY(available >= s->ep_packetsize);
// claim endpoint
TU_VERIFY(stream_claim(s), 0);
// get available again since fifo can be changed before endpoint is claimed
available = tu_fifo_remaining(&s->ff);
if (available >= s->ep_packetsize) {
// multiple of packet size limit by ep bufsize
uint16_t count = (uint16_t) (available & ~(s->ep_packetsize - 1));
count = tu_min16(count, s->ep_bufsize);
TU_ASSERT(stream_xfer(s, count), 0);
return count;
} else { } else {
// Release endpoint since we don't make any transfer uint16_t available = tu_fifo_remaining(&s->ff);
stream_release(s);
return 0; // 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 >= s->ep_packetsize);
TU_VERIFY(stream_claim(s), 0);
// get available again since fifo can be changed before endpoint is claimed
available = tu_fifo_remaining(&s->ff);
if (available >= s->ep_packetsize) {
// multiple of packet size limit by ep bufsize
uint16_t count = (uint16_t) (available & ~(s->ep_packetsize - 1));
count = tu_min16(count, s->ep_bufsize);
TU_ASSERT(stream_xfer(s, count), 0);
return count;
} else {
// Release endpoint since we don't make any transfer
stream_release(s);
return 0;
}
} }
} }