|
56 | 56 |
|
57 | 57 | #define BUF_HALF_MASK (USB_SAMP_BUFFER_SIZE >> 1) |
58 | 58 |
|
| 59 | +// Unless we know the host knows our buffer size, we'll avoid leaving TX |
| 60 | +// until we've transmitted all bytes sent by the host. This flag is cleared |
| 61 | +// when the host requests our buffer size. |
| 62 | +bool auto_tx_flush = true; |
| 63 | + |
59 | 64 | volatile uint32_t dma_started, dma_pending, usb_started, usb_completed; |
60 | 65 |
|
61 | 66 | typedef struct { |
@@ -345,7 +350,7 @@ volatile transceiver_request_t transceiver_request = { |
345 | 350 | void transceiver_usb_setup_complete(usb_endpoint_t* const endpoint) |
346 | 351 | { |
347 | 352 | if (transceiver_request.mode == TRANSCEIVER_MODE_TX && |
348 | | - endpoint->setup.request == 1) { |
| 353 | + endpoint->setup.request == 1 && auto_tx_flush) { |
349 | 354 | // This is a request to leave TX mode. Do so but NAK for now. |
350 | 355 | request_transceiver_mode(endpoint->setup.value); |
351 | 356 | } else { |
@@ -493,6 +498,11 @@ usb_request_status_t usb_vendor_request_get_buffer_size( |
493 | 498 | NULL, |
494 | 499 | NULL); |
495 | 500 | usb_transfer_schedule_ack(endpoint->out); |
| 501 | + |
| 502 | + // We now know the host is aware of our buffer size, so it |
| 503 | + // can make its own decisions about flushing the buffer. |
| 504 | + auto_tx_flush = false; |
| 505 | + |
496 | 506 | return USB_REQUEST_STATUS_OK; |
497 | 507 | } |
498 | 508 | return USB_REQUEST_STATUS_OK; |
@@ -643,9 +653,17 @@ void tx_mode(uint32_t seq) |
643 | 653 | } |
644 | 654 | } |
645 | 655 |
|
646 | | - // Host has now requested to stop TX. We won't initiate any further USB |
647 | | - // transfers into the bulk buffer. However, we should make sure all |
648 | | - // data currently in the USB bulk buffer reaches the sample buffer. |
| 656 | + // Host has now requested to stop TX. If we're not auto-flushing, we |
| 657 | + // should now stop TX immediately. |
| 658 | + |
| 659 | + if (!auto_tx_flush) { |
| 660 | + transceiver_shutdown(); |
| 661 | + return; |
| 662 | + } |
| 663 | + |
| 664 | + // Otherwise, we should now ensure all bytes sent by the host are |
| 665 | + // transmitted before we leave TX. First, we should make sure all data |
| 666 | + // currently in the USB bulk buffer reaches the sample buffer. |
649 | 667 |
|
650 | 668 | if ((usb_started - usb_completed) > 0) { |
651 | 669 | // We were part way through a 16KB firmware-side transfer when |
|
0 commit comments