Skip to content

Commit

Permalink
Increment a sequence number when transceiver mode changes.
Browse files Browse the repository at this point in the history
This fixes bug #1042, which occured when an RX->OFF->RX sequence
happened quickly enough that the loop in rx_mode() did not see the
change. As a result, the enable_baseband_streaming() call at the start
of that function was not repeated for the new RX operation, so RX
progress stalled.

To solve this, the vendor request handler now increments a sequence
number when it changes the transceiver mode. Instead of the RX loop
checking whether the transceiver mode is still RX, it now checks whether
the current sequence number is the same as when it was started. If not,
there must have been at least one mode change, so the loop exits, and
the main loop starts the necessary loop for the new mode. The same
behaviour is implemented for the TX and sweep loops.

For this approach to be reliable, we must ensure that when deciding
which mode and sequence number to use, we take both values from the same
set_transceiver_mode request.

To achieve this, we briefly disable the USB0 interrupt to stop the
vendor request handler from running whilst reading the mode and sequence
number together. Then the loop dispatch proceeds using those pre-read
values.
  • Loading branch information
martinling committed Feb 3, 2022
1 parent 4c9fcf8 commit 7057235
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 13 deletions.
22 changes: 18 additions & 4 deletions firmware/hackrf_usb/hackrf_usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -263,15 +263,29 @@ int main(void) {
operacake_init(operacake_allow_gpio);

while(true) {
switch (transceiver_mode()) {

// Briefly disable USB interrupt so that we can
// atomically retrieve both the transceiver mode
// and the mode change sequence number. They are
// changed together by the set_transceiver_mode
// vendor request handler.

nvic_disable_irq(NVIC_USB0_IRQ);

transceiver_mode_t mode = transceiver_mode();
uint32_t seq = transceiver_mode_seq();

nvic_enable_irq(NVIC_USB0_IRQ);

switch (mode) {
case TRANSCEIVER_MODE_RX:
rx_mode();
rx_mode(seq);
break;
case TRANSCEIVER_MODE_TX:
tx_mode();
tx_mode(seq);
break;
case TRANSCEIVER_MODE_RX_SWEEP:
sweep_mode();
sweep_mode(seq);
break;
case TRANSCEIVER_MODE_CPLD_UPDATE:
cpld_update();
Expand Down
4 changes: 2 additions & 2 deletions firmware/hackrf_usb/usb_api_sweep.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ usb_request_status_t usb_vendor_request_init_sweep(
return USB_REQUEST_STATUS_OK;
}

void sweep_mode(void) {
void sweep_mode(uint32_t seq) {
unsigned int blocks_queued = 0;
unsigned int phase = 1;
bool odd = true;
Expand All @@ -98,7 +98,7 @@ void sweep_mode(void) {

baseband_streaming_enable(&sgpio_config);

while (TRANSCEIVER_MODE_RX_SWEEP == transceiver_mode()) {
while (transceiver_mode_seq() == seq) {
// Set up IN transfer of buffer 0.
if ( m0_state.offset >= 16384 && phase == 1) {
transfer = true;
Expand Down
2 changes: 1 addition & 1 deletion firmware/hackrf_usb/usb_api_sweep.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ enum sweep_style {
usb_request_status_t usb_vendor_request_init_sweep(
usb_endpoint_t* const endpoint, const usb_transfer_stage_t stage);

void sweep_mode(void);
void sweep_mode(uint32_t seq);

#endif /* __USB_API_SWEEP_H__ */
15 changes: 11 additions & 4 deletions firmware/hackrf_usb/usb_api_transceiver.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ usb_request_status_t usb_vendor_request_set_freq_explicit(
}

static volatile transceiver_mode_t _transceiver_mode = TRANSCEIVER_MODE_OFF;
static volatile uint32_t _transceiver_mode_seq = 0;
static volatile hw_sync_mode_t _hw_sync_mode = HW_SYNC_MODE_OFF;

void set_hw_sync_mode(const hw_sync_mode_t new_hw_sync_mode) {
Expand All @@ -248,6 +249,10 @@ transceiver_mode_t transceiver_mode(void) {
return _transceiver_mode;
}

uint32_t transceiver_mode_seq(void) {
return _transceiver_mode_seq;
}

void set_transceiver_mode(const transceiver_mode_t new_transceiver_mode) {
baseband_streaming_disable(&sgpio_config);
operacake_sctimer_reset_state();
Expand Down Expand Up @@ -287,6 +292,8 @@ void set_transceiver_mode(const transceiver_mode_t new_transceiver_mode) {

m0_state.offset = 0;
}

_transceiver_mode_seq++;
}

usb_request_status_t usb_vendor_request_set_transceiver_mode(
Expand Down Expand Up @@ -324,12 +331,12 @@ usb_request_status_t usb_vendor_request_set_hw_sync_mode(
}
}

void rx_mode(void) {
void rx_mode(uint32_t seq) {
unsigned int phase = 1;

baseband_streaming_enable(&sgpio_config);

while (TRANSCEIVER_MODE_RX == _transceiver_mode) {
while (_transceiver_mode_seq == seq) {
// Set up IN transfer of buffer 0.
if (16384 <= m0_state.offset && 1 == phase) {
usb_transfer_schedule_block(
Expand All @@ -353,7 +360,7 @@ void rx_mode(void) {
}
}

void tx_mode(void) {
void tx_mode(uint32_t seq) {
unsigned int phase = 1;

memset(&usb_bulk_buffer[0x0000], 0, 0x8000);
Expand All @@ -367,7 +374,7 @@ void tx_mode(void) {
// Start transmitting zeros while the host fills buffer 1.
baseband_streaming_enable(&sgpio_config);

while (TRANSCEIVER_MODE_TX == _transceiver_mode) {
while (_transceiver_mode_seq == seq) {
// Set up OUT transfer of buffer 0.
if (16384 <= m0_state.offset && 1 == phase) {
usb_transfer_schedule_block(
Expand Down
5 changes: 3 additions & 2 deletions firmware/hackrf_usb/usb_api_transceiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ usb_request_status_t usb_vendor_request_set_hw_sync_mode(
usb_endpoint_t* const endpoint, const usb_transfer_stage_t stage);

transceiver_mode_t transceiver_mode(void);
uint32_t transceiver_mode_seq(void);
void set_transceiver_mode(const transceiver_mode_t new_transceiver_mode);
void start_streaming_on_hw_sync();
void rx_mode(void);
void tx_mode(void);
void rx_mode(uint32_t seq);
void tx_mode(uint32_t seq);

#endif/*__USB_API_TRANSCEIVER_H__*/

0 comments on commit 7057235

Please sign in to comment.