From 0175c2e84e7e04b3f2d6f27f4ac725bdee03fbbf Mon Sep 17 00:00:00 2001 From: Martin Ling Date: Mon, 25 Jul 2022 10:25:56 +0100 Subject: [PATCH 1/3] Simplify rx_mode loop and prevent it stalling. On rad1o, the UI update could block this loop from running for long enough that it could stall in a state where neither of the conditions was met. Fix this by removing the 'phase' variable, in favour of a counter tracking the number of bytes that have been scheduled for USB transfer. Whenever there are enough bytes to schedule the next transfer, do so. Meanwhile, the M0 count is prevented from wrapping around and clobbering data not yet sent, because the M0 code monitors the m4_count variable which is updated as each transfer completes. --- firmware/hackrf_usb/usb_api_transceiver.c | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/firmware/hackrf_usb/usb_api_transceiver.c b/firmware/hackrf_usb/usb_api_transceiver.c index 7d2d7864..4882e890 100644 --- a/firmware/hackrf_usb/usb_api_transceiver.c +++ b/firmware/hackrf_usb/usb_api_transceiver.c @@ -368,35 +368,22 @@ void transceiver_bulk_transfer_complete(void *user_data, unsigned int bytes_tran } void rx_mode(uint32_t seq) { - unsigned int phase = 1; + uint32_t usb_count = 0; transceiver_startup(TRANSCEIVER_MODE_RX); baseband_streaming_enable(&sgpio_config); while (transceiver_request.seq == seq) { - uint32_t m0_offset = m0_state.m0_count & USB_BULK_BUFFER_MASK; - // Set up IN transfer of buffer 0. - if (16384 <= m0_offset && 1 == phase) { + if ((m0_state.m0_count - usb_count) >= 0x4000) { usb_transfer_schedule_block( &usb_endpoint_bulk_in, - &usb_bulk_buffer[0x0000], + &usb_bulk_buffer[usb_count & USB_BULK_BUFFER_MASK], 0x4000, transceiver_bulk_transfer_complete, NULL ); - phase = 0; - } - // Set up IN transfer of buffer 1. - if (16384 > m0_offset && 0 == phase) { - usb_transfer_schedule_block( - &usb_endpoint_bulk_in, - &usb_bulk_buffer[0x4000], - 0x4000, - transceiver_bulk_transfer_complete, - NULL - ); - phase = 1; + usb_count += 0x4000; } } From a943610cd42225b97286a34ecc2e40cc0842c80e Mon Sep 17 00:00:00 2001 From: Martin Ling Date: Mon, 25 Jul 2022 10:40:07 +0100 Subject: [PATCH 2/3] Simpify TX loop in the same way as RX loop. --- firmware/hackrf_usb/usb_api_transceiver.c | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/firmware/hackrf_usb/usb_api_transceiver.c b/firmware/hackrf_usb/usb_api_transceiver.c index 4882e890..1783e337 100644 --- a/firmware/hackrf_usb/usb_api_transceiver.c +++ b/firmware/hackrf_usb/usb_api_transceiver.c @@ -391,7 +391,7 @@ void rx_mode(uint32_t seq) { } void tx_mode(uint32_t seq) { - unsigned int phase = 0; + unsigned int usb_count = 0; transceiver_startup(TRANSCEIVER_MODE_TX); @@ -403,6 +403,7 @@ void tx_mode(uint32_t seq) { transceiver_bulk_transfer_complete, NULL ); + usb_count += 0x4000; // Enable streaming. The M0 is in TX_START mode, and will automatically // send zeroes until the host fills buffer 0. Once that buffer is filled, @@ -411,28 +412,15 @@ void tx_mode(uint32_t seq) { baseband_streaming_enable(&sgpio_config); while (transceiver_request.seq == seq) { - uint32_t m0_offset = m0_state.m0_count & USB_BULK_BUFFER_MASK; - // Set up OUT transfer of buffer 0. - if (16384 <= m0_offset && 1 == phase) { + if ((usb_count - m0_state.m0_count) <= 0x4000) { usb_transfer_schedule_block( &usb_endpoint_bulk_out, - &usb_bulk_buffer[0x0000], + &usb_bulk_buffer[usb_count & USB_BULK_BUFFER_MASK], 0x4000, transceiver_bulk_transfer_complete, NULL ); - phase = 0; - } - // Set up OUT transfer of buffer 1. - if (16384 > m0_offset && 0 == phase) { - usb_transfer_schedule_block( - &usb_endpoint_bulk_out, - &usb_bulk_buffer[0x4000], - 0x4000, - transceiver_bulk_transfer_complete, - NULL - ); - phase = 1; + usb_count += 0x4000; } } From 3418f6e249e650481eaa93c76dce5c0cfc381246 Mon Sep 17 00:00:00 2001 From: Martin Ling Date: Mon, 25 Jul 2022 10:54:07 +0100 Subject: [PATCH 3/3] Use a #define for USB transfer size. --- firmware/hackrf_usb/usb_api_transceiver.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/firmware/hackrf_usb/usb_api_transceiver.c b/firmware/hackrf_usb/usb_api_transceiver.c index 1783e337..273c98de 100644 --- a/firmware/hackrf_usb/usb_api_transceiver.c +++ b/firmware/hackrf_usb/usb_api_transceiver.c @@ -44,6 +44,8 @@ #include "usb_endpoint.h" #include "usb_api_sweep.h" +#define USB_TRANSFER_SIZE 0x4000 + typedef struct { uint32_t freq_mhz; uint32_t freq_hz; @@ -375,15 +377,15 @@ void rx_mode(uint32_t seq) { baseband_streaming_enable(&sgpio_config); while (transceiver_request.seq == seq) { - if ((m0_state.m0_count - usb_count) >= 0x4000) { + if ((m0_state.m0_count - usb_count) >= USB_TRANSFER_SIZE) { usb_transfer_schedule_block( &usb_endpoint_bulk_in, &usb_bulk_buffer[usb_count & USB_BULK_BUFFER_MASK], - 0x4000, + USB_TRANSFER_SIZE, transceiver_bulk_transfer_complete, NULL ); - usb_count += 0x4000; + usb_count += USB_TRANSFER_SIZE; } } @@ -399,11 +401,11 @@ void tx_mode(uint32_t seq) { usb_transfer_schedule_block( &usb_endpoint_bulk_out, &usb_bulk_buffer[0x0000], - 0x4000, + USB_TRANSFER_SIZE, transceiver_bulk_transfer_complete, NULL ); - usb_count += 0x4000; + usb_count += USB_TRANSFER_SIZE; // Enable streaming. The M0 is in TX_START mode, and will automatically // send zeroes until the host fills buffer 0. Once that buffer is filled, @@ -412,15 +414,15 @@ void tx_mode(uint32_t seq) { baseband_streaming_enable(&sgpio_config); while (transceiver_request.seq == seq) { - if ((usb_count - m0_state.m0_count) <= 0x4000) { + if ((usb_count - m0_state.m0_count) <= USB_TRANSFER_SIZE) { usb_transfer_schedule_block( &usb_endpoint_bulk_out, &usb_bulk_buffer[usb_count & USB_BULK_BUFFER_MASK], - 0x4000, + USB_TRANSFER_SIZE, transceiver_bulk_transfer_complete, NULL ); - usb_count += 0x4000; + usb_count += USB_TRANSFER_SIZE; } }