Skip to content

Commit

Permalink
rtl-sdr: gain setting: better error messages, add some delay
Browse files Browse the repository at this point in the history
fix some corner cases
  • Loading branch information
wiedehopf committed Apr 30, 2024
1 parent 17f089c commit 346e6c3
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 9 deletions.
2 changes: 2 additions & 0 deletions readsb.c
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ static void modesInit(void) {

pthread_mutex_init(&Modes.traceDebugMutex, NULL);
pthread_mutex_init(&Modes.hungTimerMutex, NULL);
pthread_mutex_init(&Modes.sdrControlMutex, NULL);

threadInit(&Threads.reader, "reader");
threadInit(&Threads.upkeep, "upkeep");
Expand Down Expand Up @@ -2887,6 +2888,7 @@ int main(int argc, char **argv) {

pthread_mutex_destroy(&Modes.traceDebugMutex);
pthread_mutex_destroy(&Modes.hungTimerMutex);
pthread_mutex_destroy(&Modes.sdrControlMutex);

if (Modes.debug_bogus) {
display_total_short_range_stats();
Expand Down
2 changes: 2 additions & 0 deletions readsb.h
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,8 @@ struct _Modes
iq_convert_fn converter_function;
char * dev_name;
int gain;
int sdrInitialized;
pthread_mutex_t sdrControlMutex;
int dc_filter; // should we apply a DC filter?
int enable_agc;
sdr_type_t sdr_type; // where are we getting data from?
Expand Down
26 changes: 24 additions & 2 deletions sdr.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,13 @@ static sdr_handler *current_handler() {
//

bool sdrOpen() {
return current_handler()->open();
pthread_mutex_lock(&Modes.sdrControlMutex);
bool success = current_handler()->open();
pthread_mutex_unlock(&Modes.sdrControlMutex);
if (success) {
Modes.sdrInitialized = 1;
}
return success;
}

bool sdrHasRun() {
Expand All @@ -188,15 +194,31 @@ void sdrRun() {
}

void sdrCancel() {
pthread_mutex_lock(&Modes.sdrControlMutex);
Modes.sdrInitialized = 0;

current_handler()->cancel();

pthread_mutex_unlock(&Modes.sdrControlMutex);
}

void sdrClose() {
pthread_mutex_lock(&Modes.sdrControlMutex);

Modes.sdrInitialized = 0;
current_handler()->close();

pthread_mutex_unlock(&Modes.sdrControlMutex);
}

void sdrSetGain(){
current_handler()->setGain();
pthread_mutex_lock(&Modes.sdrControlMutex);

if (Modes.sdrInitialized) {
current_handler()->setGain();
}

pthread_mutex_unlock(&Modes.sdrControlMutex);
}

void lockReader() {
Expand Down
32 changes: 25 additions & 7 deletions sdr_rtlsdr.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ static struct {
int numgains;
int *gains;
int curGain;
int tunerAgcEnabled;
} RTLSDR;

//
Expand All @@ -87,14 +88,20 @@ void rtlsdrInitConfig() {
RTLSDR.bounce_buffer = NULL;
RTLSDR.numgains = 0;
RTLSDR.gains = NULL;
RTLSDR.tunerAgcEnabled = 0;
}

void rtlsdrSetGain() {
if (Modes.gain == MODES_AUTO_GAIN || Modes.gain >= 520) {
Modes.gain = 590;

RTLSDR.tunerAgcEnabled = 1;

fprintf(stderr, "rtlsdr: enabling tuner AGC\n");
rtlsdr_set_tuner_gain_mode(RTLSDR.dev, 0);
if (rtlsdr_set_tuner_gain_mode(RTLSDR.dev, 0)) {
fprintf(stderr, "rtlsdr: enabling tuner AGC failed\n");
return;
}
} else {

int target = (Modes.gain == MODES_MAX_GAIN ? 9999 : Modes.gain);
Expand All @@ -104,12 +111,23 @@ void rtlsdrSetGain() {
if (closest == -1 || abs(RTLSDR.gains[i] - target) < abs(RTLSDR.gains[closest] - target))
closest = i;
}

Modes.gain = RTLSDR.gains[closest];
rtlsdr_set_tuner_gain_mode(RTLSDR.dev, 1); // disable autogain
rtlsdr_set_tuner_gain(RTLSDR.dev, RTLSDR.gains[closest]);
fprintf(stderr, "rtlsdr: tuner gain set to %.1f dB\n",
rtlsdr_get_tuner_gain(RTLSDR.dev) / 10.0);
int newGain = RTLSDR.gains[closest];

if (RTLSDR.tunerAgcEnabled) {
if (rtlsdr_set_tuner_gain_mode(RTLSDR.dev, 1)) {
fprintf(stderr, "rtlsdr: disabling tuner AGC failed\n");
return;
}
RTLSDR.tunerAgcEnabled = 0;
usleep(1000);
}
if (rtlsdr_set_tuner_gain(RTLSDR.dev, newGain)) {
fprintf(stderr, "rtlsdr: setting tuner gain failed\n");
return;
} else {
fprintf(stderr, "rtlsdr: tuner gain set to %.1f dB\n", newGain / 10.0);
Modes.gain = newGain;
}
}
}

Expand Down

0 comments on commit 346e6c3

Please sign in to comment.