change edpt stream api to take hwid from API to reduce memory footprint
This commit is contained in:
62
src/tusb.c
62
src/tusb.c
@@ -239,40 +239,40 @@ bool tu_edpt_stream_deinit(tu_edpt_stream_t* s) {
|
||||
return true;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool stream_claim(tu_edpt_stream_t* s) {
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool stream_claim(uint8_t hwid, tu_edpt_stream_t* s) {
|
||||
if (s->is_host) {
|
||||
#if CFG_TUH_ENABLED
|
||||
return usbh_edpt_claim(s->daddr, s->ep_addr);
|
||||
return usbh_edpt_claim(hwid, s->ep_addr);
|
||||
#endif
|
||||
} else {
|
||||
#if CFG_TUD_ENABLED
|
||||
return usbd_edpt_claim(s->rhport, s->ep_addr);
|
||||
return usbd_edpt_claim(hwid, s->ep_addr);
|
||||
#endif
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool stream_xfer(tu_edpt_stream_t* s, uint16_t count) {
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool stream_xfer(uint8_t hwid, tu_edpt_stream_t* s, uint16_t count) {
|
||||
if (s->is_host) {
|
||||
#if CFG_TUH_ENABLED
|
||||
return usbh_edpt_xfer(s->daddr, s->ep_addr, count ? s->ep_buf : NULL, count);
|
||||
return usbh_edpt_xfer(hwid, s->ep_addr, count ? s->ep_buf : NULL, count);
|
||||
#endif
|
||||
} else {
|
||||
#if CFG_TUD_ENABLED
|
||||
return usbd_edpt_xfer(s->rhport, s->ep_addr, count ? s->ep_buf : NULL, count);
|
||||
return usbd_edpt_xfer(hwid, s->ep_addr, count ? s->ep_buf : NULL, count);
|
||||
#endif
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool stream_release(tu_edpt_stream_t* s) {
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool stream_release(uint8_t hwid, tu_edpt_stream_t* s) {
|
||||
if (s->is_host) {
|
||||
#if CFG_TUH_ENABLED
|
||||
return usbh_edpt_release(s->daddr, s->ep_addr);
|
||||
return usbh_edpt_release(hwid, s->ep_addr);
|
||||
#endif
|
||||
} else {
|
||||
#if CFG_TUD_ENABLED
|
||||
return usbd_edpt_release(s->rhport, s->ep_addr);
|
||||
return usbd_edpt_release(hwid, s->ep_addr);
|
||||
#endif
|
||||
}
|
||||
return false;
|
||||
@@ -281,43 +281,43 @@ TU_ATTR_ALWAYS_INLINE static inline bool stream_release(tu_edpt_stream_t* s) {
|
||||
//--------------------------------------------------------------------+
|
||||
// Stream Write
|
||||
//--------------------------------------------------------------------+
|
||||
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(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
|
||||
TU_VERIFY(!tu_fifo_count(&s->ff) && last_xferred_bytes && (0 == (last_xferred_bytes & (s->ep_packetsize - 1))));
|
||||
TU_VERIFY(stream_claim(s));
|
||||
TU_ASSERT(stream_xfer(s, 0));
|
||||
TU_VERIFY(stream_claim(hwid, s));
|
||||
TU_ASSERT(stream_xfer(hwid, s, 0));
|
||||
return true;
|
||||
}
|
||||
|
||||
uint32_t tu_edpt_stream_write_xfer(tu_edpt_stream_t* s) {
|
||||
uint32_t tu_edpt_stream_write_xfer(uint8_t hwid, tu_edpt_stream_t* s) {
|
||||
// skip if no data
|
||||
TU_VERIFY(tu_fifo_count(&s->ff), 0);
|
||||
|
||||
TU_VERIFY(stream_claim(s), 0);
|
||||
TU_VERIFY(stream_claim(hwid, s), 0);
|
||||
|
||||
// Pull data from FIFO -> EP buf
|
||||
uint16_t const count = tu_fifo_read_n(&s->ff, s->ep_buf, s->ep_bufsize);
|
||||
|
||||
if (count) {
|
||||
TU_ASSERT(stream_xfer(s, count), 0);
|
||||
TU_ASSERT(stream_xfer(hwid, s, count), 0);
|
||||
return count;
|
||||
} else {
|
||||
// Release endpoint since we don't make any transfer
|
||||
// Note: data is dropped if terminal is not connected
|
||||
stream_release(s);
|
||||
stream_release(hwid, s);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t tu_edpt_stream_write(tu_edpt_stream_t* s, void const* buffer, uint32_t bufsize) {
|
||||
uint32_t tu_edpt_stream_write(uint8_t hwid, tu_edpt_stream_t* s, void const* buffer, uint32_t bufsize) {
|
||||
TU_VERIFY(bufsize); // TODO support ZLP
|
||||
|
||||
if (0 == tu_fifo_depth(&s->ff)) {
|
||||
// no fifo for buffered
|
||||
TU_VERIFY(stream_claim(s), 0);
|
||||
TU_VERIFY(stream_claim(hwid, s), 0);
|
||||
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);
|
||||
TU_ASSERT(stream_xfer(hwid, s, xact_len), 0);
|
||||
return xact_len;
|
||||
} else {
|
||||
const uint16_t ret = tu_fifo_write_n(&s->ff, buffer, (uint16_t) bufsize);
|
||||
@@ -325,24 +325,24 @@ uint32_t tu_edpt_stream_write(tu_edpt_stream_t* s, void const* buffer, uint32_t
|
||||
// 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);
|
||||
tu_edpt_stream_write_xfer(hwid, s);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t tu_edpt_stream_write_available(tu_edpt_stream_t* s) {
|
||||
uint32_t tu_edpt_stream_write_available(uint8_t hwid, 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);
|
||||
is_busy = usbh_edpt_busy(hwid, s->ep_addr);
|
||||
#endif
|
||||
} else {
|
||||
#if CFG_TUD_ENABLED
|
||||
is_busy = usbd_edpt_busy(s->rhport, s->ep_addr);
|
||||
is_busy = usbd_edpt_busy(hwid, s->ep_addr);
|
||||
#endif
|
||||
}
|
||||
return is_busy ? 0 : s->ep_bufsize;
|
||||
@@ -352,11 +352,11 @@ uint32_t tu_edpt_stream_write_available(tu_edpt_stream_t* s) {
|
||||
//--------------------------------------------------------------------+
|
||||
// Stream Read
|
||||
//--------------------------------------------------------------------+
|
||||
uint32_t tu_edpt_stream_read_xfer(tu_edpt_stream_t* s) {
|
||||
uint32_t tu_edpt_stream_read_xfer(uint8_t hwid, tu_edpt_stream_t* s) {
|
||||
if (0 == tu_fifo_depth(&s->ff)) {
|
||||
// no fifo for buffered
|
||||
TU_VERIFY(stream_claim(s), 0);
|
||||
TU_ASSERT(stream_xfer(s, s->ep_bufsize), 0);
|
||||
TU_VERIFY(stream_claim(hwid, s), 0);
|
||||
TU_ASSERT(stream_xfer(hwid, s, s->ep_bufsize), 0);
|
||||
return s->ep_bufsize;
|
||||
} else {
|
||||
uint16_t available = tu_fifo_remaining(&s->ff);
|
||||
@@ -367,7 +367,7 @@ uint32_t tu_edpt_stream_read_xfer(tu_edpt_stream_t* s) {
|
||||
// This pre-check reduces endpoint claiming
|
||||
TU_VERIFY(available >= s->ep_packetsize);
|
||||
|
||||
TU_VERIFY(stream_claim(s), 0);
|
||||
TU_VERIFY(stream_claim(hwid, s), 0);
|
||||
|
||||
// get available again since fifo can be changed before endpoint is claimed
|
||||
available = tu_fifo_remaining(&s->ff);
|
||||
@@ -376,19 +376,19 @@ uint32_t tu_edpt_stream_read_xfer(tu_edpt_stream_t* s) {
|
||||
// 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);
|
||||
TU_ASSERT(stream_xfer(hwid, s, count), 0);
|
||||
return count;
|
||||
} else {
|
||||
// Release endpoint since we don't make any transfer
|
||||
stream_release(s);
|
||||
stream_release(hwid, s);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t tu_edpt_stream_read(tu_edpt_stream_t* s, void* buffer, uint32_t bufsize) {
|
||||
uint32_t tu_edpt_stream_read(uint8_t hwid, tu_edpt_stream_t* s, void* buffer, uint32_t bufsize) {
|
||||
uint32_t num_read = tu_fifo_read_n(&s->ff, buffer, (uint16_t) bufsize);
|
||||
tu_edpt_stream_read_xfer(s);
|
||||
tu_edpt_stream_read_xfer(hwid, s);
|
||||
return num_read;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user