diff options
author | Marti Bolivar <mbolivar@leaflabs.com> | 2013-01-17 18:08:39 -0500 |
---|---|---|
committer | Marti Bolivar <mbolivar@leaflabs.com> | 2013-01-17 18:08:39 -0500 |
commit | afb4199070e464c317b86741e107692120e05097 (patch) | |
tree | c57eda4542c0bb85f9354e877c0f159dd34fce6f | |
parent | 419a6796c2d444762e2ae5b7ae8b846690add839 (diff) | |
download | librambutan-afb4199070e464c317b86741e107692120e05097.tar.gz librambutan-afb4199070e464c317b86741e107692120e05097.zip |
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 <mbolivar@leaflabs.com>
-rw-r--r-- | libmaple/usb/stm32f1/usb_cdcacm.c | 19 |
1 files changed, 8 insertions, 11 deletions
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; } |