change edpt stream api to take hwid from API to reduce memory footprint
This commit is contained in:
		| @@ -341,14 +341,14 @@ uint32_t tuh_cdc_write(uint8_t idx, void const* buffer, uint32_t bufsize) { | ||||
|   cdch_interface_t* p_cdc = get_itf(idx); | ||||
|   TU_VERIFY(p_cdc); | ||||
|  | ||||
|   return tu_edpt_stream_write(&p_cdc->stream.tx, buffer, bufsize); | ||||
|   return tu_edpt_stream_write(p_cdc->daddr, &p_cdc->stream.tx, buffer, bufsize); | ||||
| } | ||||
|  | ||||
| uint32_t tuh_cdc_write_flush(uint8_t idx) { | ||||
|   cdch_interface_t* p_cdc = get_itf(idx); | ||||
|   TU_VERIFY(p_cdc); | ||||
|  | ||||
|   return tu_edpt_stream_write_xfer(&p_cdc->stream.tx); | ||||
|   return tu_edpt_stream_write_xfer(p_cdc->daddr, &p_cdc->stream.tx); | ||||
| } | ||||
|  | ||||
| bool tuh_cdc_write_clear(uint8_t idx) { | ||||
| @@ -362,7 +362,7 @@ uint32_t tuh_cdc_write_available(uint8_t idx) { | ||||
|   cdch_interface_t* p_cdc = get_itf(idx); | ||||
|   TU_VERIFY(p_cdc); | ||||
|  | ||||
|   return tu_edpt_stream_write_available(&p_cdc->stream.tx); | ||||
|   return tu_edpt_stream_write_available(p_cdc->daddr, &p_cdc->stream.tx); | ||||
| } | ||||
|  | ||||
| //--------------------------------------------------------------------+ | ||||
| @@ -373,7 +373,7 @@ uint32_t tuh_cdc_read (uint8_t idx, void* buffer, uint32_t bufsize) { | ||||
|   cdch_interface_t* p_cdc = get_itf(idx); | ||||
|   TU_VERIFY(p_cdc); | ||||
|  | ||||
|   return tu_edpt_stream_read(&p_cdc->stream.rx, buffer, bufsize); | ||||
|   return tu_edpt_stream_read(p_cdc->daddr, &p_cdc->stream.rx, buffer, bufsize); | ||||
| } | ||||
|  | ||||
| uint32_t tuh_cdc_read_available(uint8_t idx) { | ||||
| @@ -395,7 +395,7 @@ bool tuh_cdc_read_clear (uint8_t idx) { | ||||
|   TU_VERIFY(p_cdc); | ||||
|  | ||||
|   bool ret = tu_edpt_stream_clear(&p_cdc->stream.rx); | ||||
|   tu_edpt_stream_read_xfer(&p_cdc->stream.rx); | ||||
|   tu_edpt_stream_read_xfer(p_cdc->daddr, &p_cdc->stream.rx); | ||||
|   return ret; | ||||
| } | ||||
|  | ||||
| @@ -677,10 +677,10 @@ bool cdch_xfer_cb(uint8_t daddr, uint8_t ep_addr, xfer_result_t event, uint32_t | ||||
|     // invoke tx complete callback to possibly refill tx fifo | ||||
|     if (tuh_cdc_tx_complete_cb) tuh_cdc_tx_complete_cb(idx); | ||||
|  | ||||
|     if ( 0 == tu_edpt_stream_write_xfer(&p_cdc->stream.tx) ) { | ||||
|     if ( 0 == tu_edpt_stream_write_xfer(daddr, &p_cdc->stream.tx) ) { | ||||
|       // If there is no data left, a ZLP should be sent if: | ||||
|       // - xferred_bytes is multiple of EP Packet size and not zero | ||||
|       tu_edpt_stream_write_zlp_if_needed(&p_cdc->stream.tx, xferred_bytes); | ||||
|       tu_edpt_stream_write_zlp_if_needed(daddr, &p_cdc->stream.tx, xferred_bytes); | ||||
|     } | ||||
|   } else if ( ep_addr == p_cdc->stream.rx.ep_addr ) { | ||||
|     #if CFG_TUH_CDC_FTDI | ||||
| @@ -698,7 +698,7 @@ bool cdch_xfer_cb(uint8_t daddr, uint8_t ep_addr, xfer_result_t event, uint32_t | ||||
|     if (tuh_cdc_rx_cb) tuh_cdc_rx_cb(idx); | ||||
|  | ||||
|     // prepare for next transfer if needed | ||||
|     tu_edpt_stream_read_xfer(&p_cdc->stream.rx); | ||||
|     tu_edpt_stream_read_xfer(daddr, &p_cdc->stream.rx); | ||||
|   }else if ( ep_addr == p_cdc->ep_notif ) { | ||||
|     // TODO handle notification endpoint | ||||
|   }else { | ||||
| @@ -719,9 +719,9 @@ static bool open_ep_stream_pair(cdch_interface_t* p_cdc, tusb_desc_endpoint_t co | ||||
|     TU_ASSERT(tuh_edpt_open(p_cdc->daddr, desc_ep)); | ||||
|  | ||||
|     if (tu_edpt_dir(desc_ep->bEndpointAddress) == TUSB_DIR_IN) { | ||||
|       tu_edpt_stream_open(&p_cdc->stream.rx, p_cdc->daddr, desc_ep); | ||||
|       tu_edpt_stream_open(&p_cdc->stream.rx, desc_ep); | ||||
|     } else { | ||||
|       tu_edpt_stream_open(&p_cdc->stream.tx, p_cdc->daddr, desc_ep); | ||||
|       tu_edpt_stream_open(&p_cdc->stream.tx, desc_ep); | ||||
|     } | ||||
|  | ||||
|     desc_ep = (tusb_desc_endpoint_t const*) tu_desc_next(desc_ep); | ||||
| @@ -763,7 +763,7 @@ static void set_config_complete(cdch_interface_t * p_cdc, uint8_t idx, uint8_t i | ||||
|   if (tuh_cdc_mount_cb) tuh_cdc_mount_cb(idx); | ||||
|  | ||||
|   // Prepare for incoming data | ||||
|   tu_edpt_stream_read_xfer(&p_cdc->stream.rx); | ||||
|   tu_edpt_stream_read_xfer(p_cdc->daddr, &p_cdc->stream.rx); | ||||
|  | ||||
|   // notify usbh that driver enumeration is complete | ||||
|   usbh_driver_set_config_complete(p_cdc->daddr, itf_num); | ||||
|   | ||||
| @@ -42,14 +42,11 @@ typedef struct TU_ATTR_PACKED | ||||
| }tu_edpt_state_t; | ||||
|  | ||||
| typedef struct { | ||||
|   bool is_host;       // host or device | ||||
|   union { | ||||
|       uint8_t daddr;  // host mode | ||||
|       uint8_t rhport; // device mode | ||||
|       uint8_t hwid; | ||||
|   struct { | ||||
|       uint8_t is_host : 1;       // host or device | ||||
|       uint8_t is_highspeed: 1;       // is highspeed | ||||
|   }; | ||||
|   uint8_t ep_addr; | ||||
|   uint8_t ep_speed; | ||||
|  | ||||
|   uint16_t ep_packetsize; | ||||
|   uint16_t ep_bufsize; | ||||
| @@ -97,16 +94,14 @@ bool tu_edpt_stream_deinit(tu_edpt_stream_t* s); | ||||
| // Open an stream for an endpoint | ||||
| // hwid is either device address (host mode) or rhport (device mode) | ||||
| TU_ATTR_ALWAYS_INLINE static inline | ||||
| void tu_edpt_stream_open(tu_edpt_stream_t* s, uint8_t hwid, tusb_desc_endpoint_t const *desc_ep) { | ||||
| void tu_edpt_stream_open(tu_edpt_stream_t* s, tusb_desc_endpoint_t const *desc_ep) { | ||||
|   tu_fifo_clear(&s->ff); | ||||
|   s->hwid = hwid; | ||||
|   s->ep_addr = desc_ep->bEndpointAddress; | ||||
|   s->ep_packetsize = tu_edpt_packet_size(desc_ep); | ||||
| } | ||||
|  | ||||
| TU_ATTR_ALWAYS_INLINE static inline | ||||
| void tu_edpt_stream_close(tu_edpt_stream_t* s) { | ||||
|   s->hwid = 0; | ||||
|   s->ep_addr = 0; | ||||
| } | ||||
|  | ||||
| @@ -121,27 +116,27 @@ bool tu_edpt_stream_clear(tu_edpt_stream_t* s) { | ||||
| //--------------------------------------------------------------------+ | ||||
|  | ||||
| // Write to stream | ||||
| 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); | ||||
|  | ||||
| // Start an usb transfer if endpoint is not busy | ||||
| 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); | ||||
|  | ||||
| // 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(uint8_t hwid, tu_edpt_stream_t* s, uint32_t last_xferred_bytes); | ||||
|  | ||||
| // Get the number of bytes available for writing to FIFO | ||||
| // 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(uint8_t hwid, tu_edpt_stream_t* s); | ||||
|  | ||||
| //--------------------------------------------------------------------+ | ||||
| // Stream Read | ||||
| //--------------------------------------------------------------------+ | ||||
|  | ||||
| // Read from stream | ||||
| 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); | ||||
|  | ||||
| // Start an usb transfer if endpoint is not busy | ||||
| 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); | ||||
|  | ||||
| // Must be called in the transfer complete callback | ||||
| TU_ATTR_ALWAYS_INLINE static inline | ||||
|   | ||||
							
								
								
									
										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
	 hathach
					hathach