Skip to content

Commit

Permalink
Merge pull request #84 from fventuri/master
Browse files Browse the repository at this point in the history
Add support for SDRplay RSP receivers
  • Loading branch information
g0orx authored Nov 20, 2020
2 parents 1668438 + 43c4f18 commit 4e0b4b7
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 18 deletions.
1 change: 1 addition & 0 deletions discovered.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ struct _DISCOVERED {
#ifdef SOAPYSDR
struct soapy {
int rtlsdr_count;
int sdrplay_count;
int sample_rate;
size_t rx_channels;
size_t rx_gains;
Expand Down
27 changes: 23 additions & 4 deletions radio.c
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ void delete_receiver(RECEIVER *rx) {
if(radio->discovered->protocol==PROTOCOL_1) {
protocol1_stop();
}
if(radio->transmitter->rx==rx) {
if(radio->transmitter!=NULL && radio->transmitter->rx==rx) {
radio->transmitter->rx=NULL;
}
radio->receiver[i]=NULL;
Expand All @@ -605,7 +605,7 @@ g_print("delete_receiver: receivers now %d\n",radio->receivers);
}
}

if(radio->transmitter->rx==NULL) {
if(radio->transmitter!=NULL && radio->transmitter->rx==NULL) {
if(radio->receivers>0) {
for(i=0;i<radio->discovered->supported_receivers;i++) {
if(radio->receiver[i]!=NULL) {
Expand Down Expand Up @@ -789,8 +789,27 @@ g_print("add_receiver: using receiver %d\n",i);
r->receiver[i]=create_receiver(i,r->sample_rate);
r->receivers++;
g_print("add_receiver: receivers now %d\n",r->receivers);
if(r->discovered->protocol==PROTOCOL_2) {
protocol2_start_receiver(r->receiver[i]);
switch(r->discovered->protocol) {
case PROTOCOL_2:
protocol2_start_receiver(r->receiver[i]);
break;
#ifdef SOAPYSDR
case PROTOCOL_SOAPYSDR:
soapy_protocol_create_receiver(r->receiver[i]);
RECEIVER *rx=r->receiver[i];
int adc=rx->adc;
soapy_protocol_set_rx_antenna(radio->receiver[i],radio->adc[adc].antenna);
double f=(double)(rx->frequency_a-rx->lo_a+rx->error_a);
soapy_protocol_set_rx_frequency(radio->receiver[i]);
soapy_protocol_set_automatic_gain(radio->receiver[i],radio->adc[adc].agc);
for(int i=0;i<radio->discovered->info.soapy.rx_gains;i++) {
soapy_protocol_set_gain(&radio->adc[adc]);
}
soapy_protocol_start_receiver(rx);
break;
#endif
default:
break;
}
} else {
g_print("add_receiver: no receivers available\n");
Expand Down
4 changes: 3 additions & 1 deletion radio_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ void update_radio_info(RECEIVER *rx) {
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(info->temp_b),radio->transmitter->temperature>radio->temperature_alarm_value);
}

gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(info->swr_b),radio->transmitter->swr>radio->swr_alarm_value);
if (radio->transmitter!=NULL) {
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(info->swr_b),radio->transmitter->swr>radio->swr_alarm_value);
}

#ifdef MIDI
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(info->midi_b),radio->midi_enabled && (radio->receiver[midi_rx]->channel==rx->channel));
Expand Down
4 changes: 3 additions & 1 deletion receiver.c
Original file line number Diff line number Diff line change
Expand Up @@ -1570,7 +1570,9 @@ g_print("create_receiver: channel=%d frequency_min=%ld frequency_max=%ld\n", cha
break;
#ifdef SOAPYSDR
case PROTOCOL_SOAPYSDR:
if(radio->discovered->supported_receivers>1) {
if(radio->discovered->supported_receivers>1 &&
// fv - need to figure how to deal with the Lime Suite
strcmp(radio->discovered->name,"lms7")==0) {
rx->adc=2;
} else {
rx->adc=1;
Expand Down
2 changes: 1 addition & 1 deletion receiver_dialog.c
Original file line number Diff line number Diff line change
Expand Up @@ -1308,7 +1308,7 @@ GtkWidget *create_receiver_dialog(RECEIVER *rx) {
row++;

rx->tx_control_b=gtk_check_button_new_with_label("Use This Receivers Frequency");
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rx->tx_control_b), radio->transmitter->rx==rx);
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rx->tx_control_b), radio->transmitter!=NULL && radio->transmitter->rx==rx);
gtk_grid_attach(GTK_GRID(tx_grid),rx->tx_control_b,0,0,1,1);
rx->tx_control_signal_id=g_signal_connect(rx->tx_control_b,"toggled",G_CALLBACK(tx_cb),rx);
}
Expand Down
23 changes: 23 additions & 0 deletions soapy_discovery.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "soapy_discovery.h"

static int rtlsdr_count=0;
static int sdrplay_count=0;

static void get_info(char *driver) {
size_t rx_rates_length, tx_rates_length, rx_gains_length, tx_gains_length, ranges_length, rx_antennas_length, tx_antennas_length, rx_bandwidth_length, tx_bandwidth_length;
Expand All @@ -35,6 +36,7 @@ static void get_info(char *driver) {
char *version;
char *address=NULL;
int rtlsdr_val=0;
int sdrplay_val=0;

fprintf(stderr,"soapy_discovery: get_info: %s\n", driver);

Expand All @@ -45,6 +47,12 @@ static void get_info(char *driver) {
SoapySDRKwargs_set(&args, "rtl", count);
rtlsdr_val=rtlsdr_count;
rtlsdr_count++;
} else if(strcmp(driver,"sdrplay")==0) {
char label[16];
sprintf(label,"SDRplay Dev%d",sdrplay_count);
SoapySDRKwargs_set(&args, "label", label);
sdrplay_val=sdrplay_count;
sdrplay_count++;
}
SoapySDRDevice *sdr = SoapySDRDevice_make(&args);
SoapySDRKwargs_clear(&args);
Expand All @@ -54,6 +62,9 @@ static void get_info(char *driver) {

char *hardwarekey=SoapySDRDevice_getHardwareKey(sdr);
fprintf(stderr,"HardwareKey=%s\n",hardwarekey);
if(strcmp(driver,"sdrplay")==0) {
address=hardwarekey;
}

SoapySDRKwargs info=SoapySDRDevice_getHardwareInfo(sdr);
version="";
Expand All @@ -65,6 +76,11 @@ static void get_info(char *driver) {
if(strcmp(info.keys[i],"fw_version")==0) {
version=info.vals[i];
}
if(strcmp(info.keys[i],"sdrplay_api_api_version")==0) {
/* take just the first 4 characters here */
info.vals[i][4]='\0';
version=info.vals[i];
}
if(strcmp(info.keys[i],"ip,ip-addr")==0) {
address=info.vals[i];
}
Expand Down Expand Up @@ -197,8 +213,13 @@ static void get_info(char *driver) {
discovered[devices].info.soapy.sample_rate=sample_rate;
if(strcmp(driver,"rtlsdr")==0) {
discovered[devices].info.soapy.rtlsdr_count=rtlsdr_val;
discovered[devices].info.soapy.sdrplay_count=0;
} else if(strcmp(driver,"sdrplay")==0) {
discovered[devices].info.soapy.rtlsdr_count=0;
discovered[devices].info.soapy.sdrplay_count=sdrplay_val;
} else {
discovered[devices].info.soapy.rtlsdr_count=0;
discovered[devices].info.soapy.sdrplay_count=0;
}
discovered[devices].info.soapy.rx_channels=rx_channels;
discovered[devices].info.soapy.rx_gains=rx_gains_length;
Expand Down Expand Up @@ -241,6 +262,8 @@ fprintf(stderr,"Tx gains: \n");
devices++;
}

// fv
SoapySDRDevice_unmake(sdr);

free(ranges);

Expand Down
25 changes: 16 additions & 9 deletions soapy_protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@

static double bandwidth=1000000.0;

#define MAX_CHANNELS 2
static SoapySDRDevice *soapy_device;
static SoapySDRStream *rx_stream;
static SoapySDRStream *rx_stream[MAX_CHANNELS];
static SoapySDRStream *tx_stream;
static int soapy_rx_sample_rate;
static int soapy_tx_sample_rate;
Expand Down Expand Up @@ -104,21 +105,21 @@ g_print("%s: setting samplerate=%f\n",__FUNCTION__,(double)soapy_rx_sample_rate)
size_t channel=rx->adc;
g_print("%s: SoapySDRDevice_setupStream: channel=%ld\n",__FUNCTION__,channel);
#if defined(SOAPY_SDR_API_VERSION) && (SOAPY_SDR_API_VERSION < 0x00080000)
rc=SoapySDRDevice_setupStream(soapy_device,&rx_stream,SOAPY_SDR_RX,SOAPY_SDR_CF32,&channel,1,NULL);
rc=SoapySDRDevice_setupStream(soapy_device,&rx_stream[channel],SOAPY_SDR_RX,SOAPY_SDR_CF32,&channel,1,NULL);
if(rc!=0) {
g_print("%s: SoapySDRDevice_setupStream (RX) failed: %s\n",__FUNCTION__,SoapySDR_errToStr(rc));
_exit(-1);
}
#else
rx_stream=SoapySDRDevice_setupStream(soapy_device,SOAPY_SDR_RX,SOAPY_SDR_CF32,&channel,1,NULL);
if(rx_stream==NULL) {
rx_stream[channel]=SoapySDRDevice_setupStream(soapy_device,SOAPY_SDR_RX,SOAPY_SDR_CF32,&channel,1,NULL);
if(rx_stream[channel]==NULL) {
g_print("%s: SoapySDRDevice_setupStream (RX) failed: %s\n",__FUNCTION__,SoapySDR_errToStr(rc));
_exit(-1);
}
#endif


max_samples=SoapySDRDevice_getStreamMTU(soapy_device,rx_stream);
max_samples=SoapySDRDevice_getStreamMTU(soapy_device,rx_stream[channel]);
if(max_samples>(2*rx->fft_size)) {
max_samples=2*rx->fft_size;
}
Expand All @@ -130,7 +131,8 @@ void soapy_protocol_start_receiver(RECEIVER *rx) {
int rc;

g_print("%s: activate_stream\n",__FUNCTION__);
rc=SoapySDRDevice_activateStream(soapy_device, rx_stream, 0, 0LL, 0);
size_t channel=rx->adc;
rc=SoapySDRDevice_activateStream(soapy_device, rx_stream[channel], 0, 0LL, 0);
if(rc!=0) {
g_print("%s: SoapySDRDevice_activateStream failed: %s\n",__FUNCTION__,SoapySDR_errToStr(rc));
_exit(-1);
Expand Down Expand Up @@ -234,6 +236,10 @@ g_print("soapy_protocol_init: SoapySDRDevice_make\n");
char id[16];
sprintf(id,"%d",radio->discovered->info.soapy.rtlsdr_count);
SoapySDRKwargs_set(&args, "rtl", id);
} else if(strcmp(radio->discovered->name,"sdrplay")==0) {
char label[16];
sprintf(label,"SDRplay Dev%d",radio->discovered->info.soapy.sdrplay_count);
SoapySDRKwargs_set(&args, "label", label);
}
soapy_device=SoapySDRDevice_make(&args);
if(soapy_device==NULL) {
Expand All @@ -257,8 +263,9 @@ static void *receive_thread(void *arg) {
void *buffs[]={buffer};
running=TRUE;
g_print("%s: receive_thread\n",__FUNCTION__);
size_t channel=rx->adc;
while(running) {
elements=SoapySDRDevice_readStream(soapy_device,rx_stream,buffs,max_samples,&flags,&timeNs,timeoutUs);
elements=SoapySDRDevice_readStream(soapy_device,rx_stream[channel],buffs,max_samples,&flags,&timeNs,timeoutUs);
if(elements<0) {
g_print("%s: elements=%d max_samples=%d\n",__FUNCTION__,elements,max_samples);
running=FALSE;
Expand Down Expand Up @@ -320,9 +327,9 @@ g_print("resampler: elements in=%d out=%d\n",elements,out_elements);
}

g_print("%s: receive_thread: SoapySDRDevice_deactivateStream\n",__FUNCTION__);
SoapySDRDevice_deactivateStream(soapy_device,rx_stream,0,0LL);
SoapySDRDevice_deactivateStream(soapy_device,rx_stream[channel],0,0LL);
g_print("%s: receive_thread: SoapySDRDevice_closeStream\n",__FUNCTION__);
SoapySDRDevice_closeStream(soapy_device,rx_stream);
SoapySDRDevice_closeStream(soapy_device,rx_stream[channel]);
//g_print("%s: receive_thread: SoapySDRDevice_unmake\n",__FUNCTION__);
// SoapySDRDevice_unmake(soapy_device);
//_exit(0);
Expand Down
4 changes: 2 additions & 2 deletions vfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ void vfo_aswapb(RECEIVER *rx) {

frequency_changed(rx);
receiver_mode_changed(rx,rx->mode_a);
if(radio->transmitter->rx==rx) {
if(radio->transmitter!=NULL && radio->transmitter->rx==rx) {
if(rx->split!=SPLIT_OFF) {
transmitter_set_mode(radio->transmitter,rx->mode_b);
} else {
Expand Down Expand Up @@ -442,7 +442,7 @@ void mode_cb(GtkWidget *menu_item,gpointer data) {
if(choice->rx->split!=SPLIT_OFF) {
choice->rx->mode_b=choice->selection;
}
if(radio->transmitter->rx==choice->rx) {
if(radio->transmitter!=NULL && radio->transmitter->rx==choice->rx) {
if(choice->rx->split!=SPLIT_OFF) {
transmitter_set_mode(radio->transmitter,choice->rx->mode_b);
} else {
Expand Down

0 comments on commit 4e0b4b7

Please sign in to comment.