From afb4199070e464c317b86741e107692120e05097 Mon Sep 17 00:00:00 2001 From: Marti Bolivar Date: Thu, 17 Jan 2013 18:08:39 -0500 Subject: usb_cdcacm.c: Fixups for usb_cdcacm_tx(). - Check if we're transmitting (instead of how many bytes are left untransmitted) before bailing, in case we're transmitting a zero-length packet. - Set transmitting=1 before setting the endpoint valid to avoid races with the USB interrupt. - Eliminate some duplicated code. Signed-off-by: Marti Bolivar --- libmaple/usb/stm32f1/usb_cdcacm.c | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) (limited to 'libmaple/usb/stm32f1') diff --git a/libmaple/usb/stm32f1/usb_cdcacm.c b/libmaple/usb/stm32f1/usb_cdcacm.c index 3f3a493..ecb9397 100644 --- a/libmaple/usb/stm32f1/usb_cdcacm.c +++ b/libmaple/usb/stm32f1/usb_cdcacm.c @@ -394,7 +394,7 @@ void usb_cdcacm_putc(char ch) { * buffer, and returns the number of bytes copied. */ uint32 usb_cdcacm_tx(const uint8* buf, uint32 len) { /* Last transmission hasn't finished, so abort. */ - if (n_unsent_bytes) { + if (usb_cdcacm_is_transmitting()) { return 0; } @@ -406,17 +406,14 @@ uint32 usb_cdcacm_tx(const uint8* buf, uint32 len) { /* Queue bytes for sending. */ if (len) { usb_copy_to_pma(buf, len, USB_CDCACM_TX_ADDR); - usb_set_ep_tx_count(USB_CDCACM_TX_ENDP, len); - n_unsent_bytes = len; - usb_set_ep_tx_stat(USB_CDCACM_TX_ENDP, USB_EP_STAT_TX_VALID); - transmitting = 1; - } else { - usb_set_ep_tx_count(USB_CDCACM_TX_ENDP, 0); - usb_set_ep_tx_stat(USB_CDCACM_TX_ENDP, USB_EP_STAT_TX_VALID); - // actually block here waiting for interrupt, even though we sent 0 bytes - n_unsent_bytes = 0; - transmitting = 1; } + // We still need to wait for the interrupt, even if we're sending + // zero bytes. (Sending zero-size packets is useful for flushing + // host-side buffers.) + usb_set_ep_tx_count(USB_CDCACM_TX_ENDP, len); + n_unsent_bytes = len; + transmitting = 1; + usb_set_ep_tx_stat(USB_CDCACM_TX_ENDP, USB_EP_STAT_TX_VALID); return len; } -- cgit v1.2.3