Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8bd99bd

Browse files
authored
Merge pull request greatscottgadgets#707 from miek/transceiver
Clean up transceiver modes & fix invalid data on start of TX/RX
2 parents 40d7a07 + 1c091a1 commit 8bd99bd

11 files changed

Lines changed: 128 additions & 62 deletions

File tree

firmware/common/hackrf_core.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,8 @@ typedef enum {
256256
TRANSCEIVER_MODE_RX = 1,
257257
TRANSCEIVER_MODE_TX = 2,
258258
TRANSCEIVER_MODE_SS = 3,
259-
TRANSCEIVER_MODE_CPLD_UPDATE = 4
259+
TRANSCEIVER_MODE_CPLD_UPDATE = 4,
260+
TRANSCEIVER_MODE_RX_SWEEP = 5,
260261
} transceiver_mode_t;
261262

262263
typedef enum {

firmware/hackrf_usb/hackrf_usb.c

Lines changed: 14 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -252,46 +252,22 @@ int main(void) {
252252
}
253253
operacake_init(operacake_allow_gpio);
254254

255-
unsigned int phase = 0;
256-
usb_bulk_buffer_offset = 0;
257-
258255
while(true) {
259-
// Check whether we need to initiate a CPLD update
260-
if (start_cpld_update)
261-
cpld_update();
262-
263-
// Check whether we need to initiate sweep mode
264-
if (start_sweep_mode) {
265-
start_sweep_mode = false;
256+
switch (transceiver_mode()) {
257+
case TRANSCEIVER_MODE_RX:
258+
rx_mode();
259+
break;
260+
case TRANSCEIVER_MODE_TX:
261+
tx_mode();
262+
break;
263+
case TRANSCEIVER_MODE_RX_SWEEP:
266264
sweep_mode();
267-
}
268-
269-
// Set up IN transfer of buffer 0.
270-
if ( usb_bulk_buffer_offset >= 16384
271-
&& phase == 1
272-
&& transceiver_mode() != TRANSCEIVER_MODE_OFF) {
273-
usb_transfer_schedule_block(
274-
(transceiver_mode() == TRANSCEIVER_MODE_RX)
275-
? &usb_endpoint_bulk_in : &usb_endpoint_bulk_out,
276-
&usb_bulk_buffer[0x0000],
277-
0x4000,
278-
NULL, NULL
279-
);
280-
phase = 0;
281-
}
282-
283-
// Set up IN transfer of buffer 1.
284-
if ( usb_bulk_buffer_offset < 16384
285-
&& phase == 0
286-
&& transceiver_mode() != TRANSCEIVER_MODE_OFF) {
287-
usb_transfer_schedule_block(
288-
(transceiver_mode() == TRANSCEIVER_MODE_RX)
289-
? &usb_endpoint_bulk_in : &usb_endpoint_bulk_out,
290-
&usb_bulk_buffer[0x4000],
291-
0x4000,
292-
NULL, NULL
293-
);
294-
phase = 1;
265+
break;
266+
case TRANSCEIVER_MODE_CPLD_UPDATE:
267+
cpld_update();
268+
break;
269+
default:
270+
break;
295271
}
296272
}
297273

firmware/hackrf_usb/usb_api_cpld.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
#include <stdint.h>
3434
#include <stdbool.h>
3535

36-
volatile bool start_cpld_update = false;
3736
uint8_t cpld_xsvf_buffer[512];
3837
volatile bool cpld_wait = false;
3938

firmware/hackrf_usb/usb_api_cpld.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
#include <usb_type.h>
2929
#include <usb_request.h>
3030

31-
extern volatile bool start_cpld_update;
32-
3331
void cpld_update(void);
3432

3533
usb_request_status_t usb_vendor_request_cpld_checksum(

firmware/hackrf_usb/usb_api_sweep.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@
2727
#include "usb_bulk_buffer.h"
2828
#include "tuning.h"
2929
#include "usb_endpoint.h"
30+
#include "streaming.h"
3031

3132
#define MIN(x,y) ((x)<(y)?(x):(y))
3233
#define MAX(x,y) ((x)>(y)?(x):(y))
3334
#define FREQ_GRANULARITY 1000000
3435
#define MAX_RANGES 10
3536
#define THROWAWAY_BUFFERS 2
3637

37-
volatile bool start_sweep_mode = false;
3838
static uint64_t sweep_freq;
3939
static uint16_t frequencies[MAX_RANGES * 2];
4040
static unsigned char data[9 + MAX_RANGES * 2 * sizeof(frequencies[0])];
@@ -44,6 +44,7 @@ static uint32_t step_width = 0;
4444
static uint32_t offset = 0;
4545
static enum sweep_style style = LINEAR;
4646

47+
/* Do this before starting sweep mode with set_transceiver_mode(). */
4748
usb_request_status_t usb_vendor_request_init_sweep(
4849
usb_endpoint_t* const endpoint, const usb_transfer_stage_t stage)
4950
{
@@ -78,7 +79,6 @@ usb_request_status_t usb_vendor_request_init_sweep(
7879
}
7980
sweep_freq = (uint64_t)frequencies[0] * FREQ_GRANULARITY;
8081
set_freq(sweep_freq + offset);
81-
start_sweep_mode = true;
8282
usb_transfer_schedule_ack(endpoint->in);
8383
}
8484
return USB_REQUEST_STATUS_OK;
@@ -93,7 +93,9 @@ void sweep_mode(void) {
9393
uint8_t *buffer;
9494
bool transfer = false;
9595

96-
while(transceiver_mode() != TRANSCEIVER_MODE_OFF) {
96+
baseband_streaming_enable(&sgpio_config);
97+
98+
while (TRANSCEIVER_MODE_RX_SWEEP == transceiver_mode()) {
9799
// Set up IN transfer of buffer 0.
98100
if ( usb_bulk_buffer_offset >= 16384 && phase == 1) {
99101
transfer = true;

firmware/hackrf_usb/usb_api_sweep.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
#include <usb_type.h>
2727
#include <usb_request.h>
2828

29-
extern volatile bool start_sweep_mode;
30-
3129
enum sweep_style {
3230
LINEAR = 0,
3331
INTERLEAVED = 1,

firmware/hackrf_usb/usb_api_transceiver.c

Lines changed: 80 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@
3636
#include <usb_queue.h>
3737

3838
#include <stddef.h>
39+
#include <string.h>
3940

4041
#include "usb_endpoint.h"
42+
#include "usb_api_sweep.h"
4143

4244
typedef struct {
4345
uint32_t freq_mhz;
@@ -251,17 +253,22 @@ void set_transceiver_mode(const transceiver_mode_t new_transceiver_mode) {
251253

252254
_transceiver_mode = new_transceiver_mode;
253255

254-
if( _transceiver_mode == TRANSCEIVER_MODE_RX ) {
256+
switch (_transceiver_mode) {
257+
case TRANSCEIVER_MODE_RX_SWEEP:
258+
case TRANSCEIVER_MODE_RX:
255259
led_off(LED3);
256260
led_on(LED2);
257261
rf_path_set_direction(&rf_path, RF_PATH_DIRECTION_RX);
258262
usb_bulk_buffer_tx = false;
259-
} else if (_transceiver_mode == TRANSCEIVER_MODE_TX) {
263+
break;
264+
case TRANSCEIVER_MODE_TX:
260265
led_off(LED2);
261266
led_on(LED3);
262267
rf_path_set_direction(&rf_path, RF_PATH_DIRECTION_TX);
263268
usb_bulk_buffer_tx = true;
264-
} else {
269+
break;
270+
case TRANSCEIVER_MODE_OFF:
271+
default:
265272
led_off(LED2);
266273
led_off(LED3);
267274
rf_path_set_direction(&rf_path, RF_PATH_DIRECTION_OFF);
@@ -274,7 +281,7 @@ void set_transceiver_mode(const transceiver_mode_t new_transceiver_mode) {
274281

275282
hw_sync_enable(_hw_sync_mode);
276283

277-
baseband_streaming_enable(&sgpio_config);
284+
usb_bulk_buffer_offset = 0;
278285
}
279286
}
280287

@@ -287,11 +294,9 @@ usb_request_status_t usb_vendor_request_set_transceiver_mode(
287294
case TRANSCEIVER_MODE_OFF:
288295
case TRANSCEIVER_MODE_RX:
289296
case TRANSCEIVER_MODE_TX:
290-
set_transceiver_mode(endpoint->setup.value);
291-
usb_transfer_schedule_ack(endpoint->in);
292-
return USB_REQUEST_STATUS_OK;
297+
case TRANSCEIVER_MODE_RX_SWEEP:
293298
case TRANSCEIVER_MODE_CPLD_UPDATE:
294-
start_cpld_update = true;
299+
set_transceiver_mode(endpoint->setup.value);
295300
usb_transfer_schedule_ack(endpoint->in);
296301
return USB_REQUEST_STATUS_OK;
297302
default:
@@ -314,3 +319,70 @@ usb_request_status_t usb_vendor_request_set_hw_sync_mode(
314319
return USB_REQUEST_STATUS_OK;
315320
}
316321
}
322+
323+
void rx_mode(void) {
324+
unsigned int phase = 1;
325+
326+
baseband_streaming_enable(&sgpio_config);
327+
328+
while (TRANSCEIVER_MODE_RX == _transceiver_mode) {
329+
// Set up IN transfer of buffer 0.
330+
if (16384 <= usb_bulk_buffer_offset && 1 == phase) {
331+
usb_transfer_schedule_block(
332+
&usb_endpoint_bulk_in,
333+
&usb_bulk_buffer[0x0000],
334+
0x4000,
335+
NULL, NULL
336+
);
337+
phase = 0;
338+
}
339+
// Set up IN transfer of buffer 1.
340+
if (16384 > usb_bulk_buffer_offset && 0 == phase) {
341+
usb_transfer_schedule_block(
342+
&usb_endpoint_bulk_in,
343+
&usb_bulk_buffer[0x4000],
344+
0x4000,
345+
NULL, NULL
346+
);
347+
phase = 1;
348+
}
349+
}
350+
}
351+
352+
void tx_mode(void) {
353+
unsigned int phase = 1;
354+
355+
memset(&usb_bulk_buffer[0x0000], 0, 0x8000);
356+
// Set up OUT transfer of buffer 1.
357+
usb_transfer_schedule_block(
358+
&usb_endpoint_bulk_out,
359+
&usb_bulk_buffer[0x4000],
360+
0x4000,
361+
NULL, NULL
362+
);
363+
// Start transmitting zeros while the host fills buffer 1.
364+
baseband_streaming_enable(&sgpio_config);
365+
366+
while (TRANSCEIVER_MODE_TX == _transceiver_mode) {
367+
// Set up OUT transfer of buffer 0.
368+
if (16384 <= usb_bulk_buffer_offset && 1 == phase) {
369+
usb_transfer_schedule_block(
370+
&usb_endpoint_bulk_out,
371+
&usb_bulk_buffer[0x0000],
372+
0x4000,
373+
NULL, NULL
374+
);
375+
phase = 0;
376+
}
377+
// Set up OUT transfer of buffer 1.
378+
if (16384 > usb_bulk_buffer_offset && 0 == phase) {
379+
usb_transfer_schedule_block(
380+
&usb_endpoint_bulk_out,
381+
&usb_bulk_buffer[0x4000],
382+
0x4000,
383+
NULL, NULL
384+
);
385+
phase = 1;
386+
}
387+
}
388+
}

firmware/hackrf_usb/usb_api_transceiver.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,7 @@ usb_request_status_t usb_vendor_request_set_hw_sync_mode(
5959
transceiver_mode_t transceiver_mode(void);
6060
void set_transceiver_mode(const transceiver_mode_t new_transceiver_mode);
6161
void start_streaming_on_hw_sync();
62+
void rx_mode(void);
63+
void tx_mode(void);
6264

6365
#endif/*__USB_API_TRANSCEIVER_H__*/

host/hackrf-tools/src/hackrf_sweep.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -664,13 +664,6 @@ int main(int argc, char** argv) {
664664
ifftwPlan = fftwf_plan_dft_1d(fftSize * step_count, ifftwIn, ifftwOut, FFTW_BACKWARD, FFTW_MEASURE);
665665
}
666666

667-
result |= hackrf_start_rx(device, rx_callback, NULL);
668-
if (result != HACKRF_SUCCESS) {
669-
fprintf(stderr, "hackrf_start_rx() failed: %s (%d)\n", hackrf_error_name(result), result);
670-
usage();
671-
return EXIT_FAILURE;
672-
}
673-
674667
result = hackrf_init_sweep(device, frequencies, num_ranges, num_samples * 2,
675668
TUNE_STEP * FREQ_ONE_MHZ, OFFSET, INTERLEAVED);
676669
if( result != HACKRF_SUCCESS ) {
@@ -679,6 +672,13 @@ int main(int argc, char** argv) {
679672
return EXIT_FAILURE;
680673
}
681674

675+
result |= hackrf_start_rx_sweep(device, rx_callback, NULL);
676+
if (result != HACKRF_SUCCESS) {
677+
fprintf(stderr, "hackrf_start_rx_sweep() failed: %s (%d)\n", hackrf_error_name(result), result);
678+
usage();
679+
return EXIT_FAILURE;
680+
}
681+
682682
if (amp) {
683683
fprintf(stderr, "call hackrf_set_amp_enable(%u)\n", amp_enable);
684684
result = hackrf_set_amp_enable(device, (uint8_t)amp_enable);

host/libhackrf/src/hackrf.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ typedef enum {
9696
HACKRF_TRANSCEIVER_MODE_TRANSMIT = 2,
9797
HACKRF_TRANSCEIVER_MODE_SS = 3,
9898
TRANSCEIVER_MODE_CPLD_UPDATE = 4,
99+
TRANSCEIVER_MODE_RX_SWEEP = 5,
99100
} hackrf_transceiver_mode;
100101

101102
typedef enum {
@@ -215,6 +216,8 @@ static int allocate_transfers(hackrf_device* const device)
215216
return HACKRF_ERROR_NO_MEM;
216217
}
217218

219+
memset(device->buffer, 0, TRANSFER_COUNT * TRANSFER_BUFFER_SIZE);
220+
218221
for(transfer_index=0; transfer_index<TRANSFER_COUNT; transfer_index++)
219222
{
220223
device->transfers[transfer_index] = libusb_alloc_transfer(0);
@@ -2162,6 +2165,20 @@ int ADDCALL hackrf_set_ui_enable(hackrf_device* device, const uint8_t value)
21622165
}
21632166
}
21642167

2168+
int ADDCALL hackrf_start_rx_sweep(hackrf_device* device, hackrf_sample_block_cb_fn callback, void* rx_ctx)
2169+
{
2170+
USB_API_REQUIRED(device, 0x0104)
2171+
int result;
2172+
const uint8_t endpoint_address = LIBUSB_ENDPOINT_IN | 1;
2173+
result = hackrf_set_transceiver_mode(device, TRANSCEIVER_MODE_RX_SWEEP);
2174+
if (HACKRF_SUCCESS == result)
2175+
{
2176+
device->rx_ctx = rx_ctx;
2177+
result = create_transfer_thread(device, endpoint_address, callback);
2178+
}
2179+
return result;
2180+
}
2181+
21652182
#ifdef __cplusplus
21662183
} // __cplusplus defined.
21672184
#endif

0 commit comments

Comments
 (0)