|
46 | 46 |
|
47 | 47 | #define BUF_HALF_MASK (USB_SAMP_BUFFER_SIZE >> 1) |
48 | 48 |
|
| 49 | +// Unless we know the host knows our buffer size, we'll avoid leaving TX |
| 50 | +// until we've transmitted all bytes sent by the host. This flag is cleared |
| 51 | +// when the host requests our buffer size. |
| 52 | +bool auto_tx_flush = true; |
| 53 | + |
49 | 54 | volatile uint32_t dma_started, dma_pending, usb_started, usb_completed; |
50 | 55 |
|
51 | 56 | typedef struct { |
@@ -303,7 +308,7 @@ volatile transceiver_request_t transceiver_request = { |
303 | 308 | void transceiver_usb_setup_complete(usb_endpoint_t* const endpoint) |
304 | 309 | { |
305 | 310 | if (transceiver_request.mode == TRANSCEIVER_MODE_TX && |
306 | | - endpoint->setup.request == 1) { |
| 311 | + endpoint->setup.request == 1 && auto_tx_flush) { |
307 | 312 | // This is a request to leave TX mode. Do so but NAK for now. |
308 | 313 | request_transceiver_mode(endpoint->setup.value); |
309 | 314 | } else { |
@@ -445,6 +450,11 @@ usb_request_status_t usb_vendor_request_get_buffer_size( |
445 | 450 | NULL, |
446 | 451 | NULL); |
447 | 452 | usb_transfer_schedule_ack(endpoint->out); |
| 453 | + |
| 454 | + // We now know the host is aware of our buffer size, so it |
| 455 | + // can make its own decisions about flushing the buffer. |
| 456 | + auto_tx_flush = false; |
| 457 | + |
448 | 458 | return USB_REQUEST_STATUS_OK; |
449 | 459 | } |
450 | 460 | return USB_REQUEST_STATUS_OK; |
@@ -606,9 +616,17 @@ void tx_mode(uint32_t seq) |
606 | 616 | radio_update(&radio); |
607 | 617 | } |
608 | 618 |
|
609 | | - // Host has now requested to stop TX. We won't initiate any further USB |
610 | | - // transfers into the bulk buffer. However, we should make sure all |
611 | | - // data currently in the USB bulk buffer reaches the sample buffer. |
| 619 | + // Host has now requested to stop TX. If we're not auto-flushing, we |
| 620 | + // should now stop TX immediately. |
| 621 | + |
| 622 | + if (!auto_tx_flush) { |
| 623 | + transceiver_shutdown(); |
| 624 | + return; |
| 625 | + } |
| 626 | + |
| 627 | + // Otherwise, we should now ensure all bytes sent by the host are |
| 628 | + // transmitted before we leave TX. First, we should make sure all data |
| 629 | + // currently in the USB bulk buffer reaches the sample buffer. |
612 | 630 |
|
613 | 631 | if ((usb_started - usb_completed) > 0) { |
614 | 632 | // We were part way through a 16KB firmware-side transfer when |
|
0 commit comments