Skip to content

Commit 5a4256a

Browse files
committed
fixed crash bug
added compute nearest freq for bandwidth filter
1 parent 7256d30 commit 5a4256a

2 files changed

Lines changed: 50 additions & 52 deletions

File tree

ExtIO_Hackrf/ExtIO_HackRF.cpp

Lines changed: 49 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,25 @@
1515
//---------------------------------------------------------------------------
1616
#define EXTIO_EXPORTS 1
1717
#define HWNAME "ExtIO HackRF"
18-
typedef long clock_t;
19-
static hackrf_device *device;
20-
HWND h_dialog = NULL;
21-
int result;
22-
short *short_buf = NULL;
23-
static int buffer_len;
2418
#define BUF_LEN 262144 /* must be multiple of 512 */
2519
#define BYTES_PER_SAMPLE 2 /* HackRF device produces 8 bit signed IQ data */
26-
2720
#define EXT_HWTYPE exthwUSBdata16
28-
2921
#define FREQ_MIN_HZ 1000000 /* 1 MHz */
3022
#define FREQ_MAX_HZ 7250000000 /* 7250MHz */
3123

24+
25+
3226
typedef struct sr {
3327
uint32_t value;
3428
TCHAR *name;
3529
} sr_t;
3630

31+
uint8_t board_id = BOARD_ID_INVALID;
32+
wchar_t str[10];
33+
static char SDR_progname[32 + 1] = "\0";
34+
static int SDR_ver_major = -1;
35+
static int SDR_ver_minor = -1;
36+
3737
static sr_t samplerates[] = {
3838
{ 2000000, TEXT("2 MSPS") },
3939
{ 4000000, TEXT("4 MSPS") },
@@ -46,41 +46,37 @@ static sr_t samplerates[] = {
4646

4747
static int samplerate_default = 3; // 10 MSPS
4848

49-
pfnExtIOCallback Callback = NULL;
50-
volatile long gExtSampleRate = 10000000;//Default 10MSPS
51-
volatile int64_t glLOfreq = 101700000L;//Default 101.7Mhz
52-
volatile bool gbStartHW = false;
53-
volatile bool gbExit = false;
49+
pfnExtIOCallback Callback = nullptr;
50+
51+
uint32_t gBandwidth;
52+
uint32_t gExtSampleRate = 10000000;//Default 10MSPS
53+
int64_t glLOfreq = 101700000L;//Default 101.7Mhz
54+
bool gbExit = false;
55+
5456
int amp = 0;
57+
unsigned int lna_gain = 16, vga_gain = 16;
5558
HANDLE bandwidth_thread;
5659
clock_t time_start, time_now;
57-
volatile uint32_t byte_count = 0;
58-
unsigned int lna_gain = 16, vga_gain = 8;
59-
60-
uint8_t board_id = BOARD_ID_INVALID;
60+
uint32_t byte_count = 0;
6161

62-
63-
wchar_t str[10];
64-
static char SDR_progname[32 + 1] = "\0";
65-
static int SDR_ver_major = -1;
66-
static int SDR_ver_minor = -1;
62+
typedef long clock_t;
63+
static hackrf_device *device;
64+
HWND h_dialog = NULL;
65+
int result;
66+
short *short_buf = nullptr;
6767

6868

6969

7070
int hackrf_rx_callback(hackrf_transfer* transfer){
7171

7272
byte_count += transfer->valid_length;
7373

74-
if (gbStartHW){
75-
for (int i = 0; i < transfer->valid_length; i++)
74+
for (int i = 0; i < transfer->valid_length; i++)
7675
{
7776
short_buf[i] = (short)(transfer->buffer[i] << 8);
7877

7978
}
80-
Callback(buffer_len, 0, 0, (void*)short_buf);
81-
82-
83-
}
79+
Callback(BUF_LEN, 0, 0, (void*)short_buf);
8480

8581
return 0;
8682
}
@@ -228,7 +224,8 @@ static INT_PTR CALLBACK MainDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPAR
228224
{
229225
gExtSampleRate = samplerates[ComboBox_GetCurSel(GET_WM_COMMAND_HWND(wParam, lParam))].value;
230226
hackrf_set_sample_rate(device, gExtSampleRate);
231-
hackrf_set_baseband_filter_bandwidth(device, gExtSampleRate);
227+
gBandwidth = hackrf_compute_baseband_filter_bw_round_down_lt(gExtSampleRate);
228+
hackrf_set_baseband_filter_bandwidth(device, gBandwidth);
232229
Callback(-1, extHw_Changed_SampleRate, 0, NULL);
233230
}
234231
return TRUE;
@@ -307,7 +304,8 @@ bool EXTIO_API OpenHW(void)
307304

308305
gExtSampleRate = samplerates[samplerate_default].value;
309306
result = hackrf_set_sample_rate(device, gExtSampleRate);
310-
result |= hackrf_set_baseband_filter_bandwidth(device, gExtSampleRate);
307+
gBandwidth = hackrf_compute_baseband_filter_bw_round_down_lt(gExtSampleRate);
308+
result |= hackrf_set_baseband_filter_bandwidth(device, gBandwidth);
311309
if (result != HACKRF_SUCCESS) {
312310
MessageBox(NULL, TEXT("hackrf_set_sample_rate_manual Failed"), NULL, MB_OK);
313311
return FALSE;
@@ -316,20 +314,10 @@ bool EXTIO_API OpenHW(void)
316314
h_dialog = CreateDialog(hInst, MAKEINTRESOURCE(IDD_HACKRF_SETTINGS), NULL, (DLGPROC)MainDlgProc);
317315
ShowWindow(h_dialog, SW_HIDE);
318316

319-
buffer_len = BUF_LEN;
320-
short_buf = new (std::nothrow) short[buffer_len];
317+
short_buf = new (std::nothrow) short[BUF_LEN];
318+
319+
321320

322-
result = hackrf_set_lna_gain(device, lna_gain);
323-
result |= hackrf_set_vga_gain(device, vga_gain);
324-
result |= hackrf_set_amp_enable(device, amp);
325-
result |= hackrf_start_rx(device, hackrf_rx_callback, NULL);
326-
if (result != HACKRF_SUCCESS) {
327-
MessageBox(NULL, TEXT("hackrf_start_rx Failed"), NULL, MB_OK);
328-
delete short_buf;
329-
return FALSE;
330-
}
331-
while (!hackrf_is_streaming(device));
332-
bandwidth_thread=CreateThread(NULL, 0, usb_bandwidth, NULL, 0, 0);
333321
return TRUE;
334322
}
335323

@@ -386,21 +374,30 @@ int64_t EXTIO_API StartHW64(int64_t LOfreq)
386374
glLOfreq = LOfreq;
387375
SetHWLO64(glLOfreq);
388376

389-
gbStartHW = TRUE;
390-
391-
377+
hackrf_set_lna_gain(device, lna_gain);
378+
hackrf_set_vga_gain(device, vga_gain);
379+
hackrf_set_amp_enable(device, amp);
380+
result = hackrf_start_rx(device, hackrf_rx_callback, NULL);
381+
if (result != HACKRF_SUCCESS) {
382+
MessageBox(NULL, TEXT("hackrf_start_rx Failed"), NULL, MB_OK);
383+
delete short_buf;
384+
return FALSE;
385+
}
386+
while (!hackrf_is_streaming(device));
387+
bandwidth_thread = CreateThread(NULL, 0, usb_bandwidth, NULL, 0, 0);
392388

393389
// number of complex elements returned each
394390
// invocation of the callback routine
395-
return buffer_len / BYTES_PER_SAMPLE;
391+
return BUF_LEN / BYTES_PER_SAMPLE;
396392
}
397393

398394

399395
//---------------------------------------------------------------------------
400396
extern "C"
401397
void EXTIO_API StopHW(void)
402398
{
403-
gbStartHW = FALSE;
399+
//gbStartHW = FALSE;
400+
hackrf_stop_rx(device);
404401

405402
}
406403

@@ -611,10 +608,11 @@ extern "C"
611608
int EXTIO_API ExtIoSetSrate(int srate_idx)
612609
{
613610
if (srate_idx >= 0 && srate_idx < (sizeof(samplerates) / sizeof(samplerates[0])))
614-
{
611+
{
615612
gExtSampleRate = samplerates[srate_idx].value;
616613
hackrf_set_sample_rate(device, gExtSampleRate);
617-
hackrf_set_baseband_filter_bandwidth(device, gExtSampleRate);
614+
gBandwidth = hackrf_compute_baseband_filter_bw_round_down_lt(gExtSampleRate);
615+
hackrf_set_baseband_filter_bandwidth(device, gBandwidth);
618616
ComboBox_SetCurSel(GetDlgItem(h_dialog, IDC_SAMPLERATE), srate_idx);
619617
Callback(-1, extHw_Changed_SampleRate, 0, NULL);// Signal application
620618
return 0;
@@ -695,7 +693,7 @@ int EXTIO_API ActivateTx(int magicA, int magicB){
695693

696694
extern "C"
697695
void ModeChanged(char mode){
698-
696+
699697
}
700698
extern "C"
701699
int EXTIO_API SetModeRxTx(int modeRxTx){

ExtIO_Hackrf/ExtIO_HackRF.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,5 @@ StopHW
3737
GetHWLO64
3838
;TX Support
3939
ActivateTx
40-
ModeChanged
40+
; ModeChanged
4141
SetModeRxTx

0 commit comments

Comments
 (0)