diff --git a/srslte/examples/cell_measurement.c b/srslte/examples/cell_measurement.c index 6a071fdf3..debb268a8 100644 --- a/srslte/examples/cell_measurement.c +++ b/srslte/examples/cell_measurement.c @@ -158,20 +158,20 @@ int main(int argc, char **argv) { exit(-1); } + printf("Opening RF device...\n"); + if (srslte_rf_open(&rf, prog_args.rf_args)) { + fprintf(stderr, "Error opening rf\n"); + exit(-1); + } if (prog_args.rf_gain > 0) { - printf("Opening RF device...\n"); - if (srslte_rf_open(&rf, prog_args.rf_args)) { - fprintf(stderr, "Error opening rf\n"); - exit(-1); - } srslte_rf_set_rx_gain(&rf, prog_args.rf_gain); } else { - printf("Opening RF device with threaded RX Gain control ...\n"); - if (srslte_rf_open_th(&rf, prog_args.rf_args, false)) { + printf("Starting AGC thread...\n"); + if (srslte_rf_start_gain_thread(&rf, false)) { fprintf(stderr, "Error opening rf\n"); exit(-1); } - srslte_rf_set_rx_gain(&rf, 50); + srslte_rf_set_rx_gain(&rf, 50); } sigset_t sigset; diff --git a/srslte/examples/cell_search.c b/srslte/examples/cell_search.c index e7cc2bd21..6096b5c74 100644 --- a/srslte/examples/cell_search.c +++ b/srslte/examples/cell_search.c @@ -154,16 +154,16 @@ int main(int argc, char **argv) { parse_args(argc, argv); + printf("Opening RF device...\n"); + if (srslte_rf_open(&rf, rf_args)) { + fprintf(stderr, "Error opening rf\n"); + exit(-1); + } if (!config.init_agc) { - printf("Opening RF device...\n"); - if (srslte_rf_open(&rf, rf_args)) { - fprintf(stderr, "Error opening rf\n"); - exit(-1); - } srslte_rf_set_rx_gain(&rf, rf_gain); } else { - printf("Opening RF device with threaded RX Gain control ...\n"); - if (srslte_rf_open_th(&rf, rf_args, false)) { + printf("Starting AGC thread...\n"); + if (srslte_rf_start_gain_thread(&rf, false)) { fprintf(stderr, "Error opening rf\n"); exit(-1); } diff --git a/srslte/examples/pdsch_ue.c b/srslte/examples/pdsch_ue.c index f6e42ed85..f0ca0c7aa 100644 --- a/srslte/examples/pdsch_ue.c +++ b/srslte/examples/pdsch_ue.c @@ -298,17 +298,17 @@ int main(int argc, char **argv) { #ifndef DISABLE_RF if (!prog_args.input_file_name) { + printf("Opening RF device...\n"); + if (srslte_rf_open(&rf, prog_args.rf_args)) { + fprintf(stderr, "Error opening rf\n"); + exit(-1); + } /* Set receiver gain */ if (prog_args.rf_gain > 0) { - printf("Opening RF device...\n"); - if (srslte_rf_open(&rf, prog_args.rf_args)) { - fprintf(stderr, "Error opening rf\n"); - exit(-1); - } srslte_rf_set_rx_gain(&rf, prog_args.rf_gain); } else { - printf("Opening RF device with threaded RX Gain control ...\n"); - if (srslte_rf_open_th(&rf, prog_args.rf_args, false)) { + printf("Starting AGC thread...\n"); + if (srslte_rf_start_gain_thread(&rf, false)) { fprintf(stderr, "Error opening rf\n"); exit(-1); } diff --git a/srslte/include/srslte/rf/rf.h b/srslte/include/srslte/rf/rf.h index 770b44bb3..185bafbe0 100644 --- a/srslte/include/srslte/rf/rf.h +++ b/srslte/include/srslte/rf/rf.h @@ -30,11 +30,22 @@ #include #include #include +#include + #include "srslte/config.h" typedef struct { void *handler; void *dev; + + // The following variables are for threaded RX gain control + pthread_t thread_gain; + pthread_cond_t cond; + pthread_mutex_t mutex; + double cur_rx_gain; + double new_rx_gain; + bool tx_gain_same_rx; + float tx_rx_gain_offset; } srslte_rf_t; typedef void (*srslte_rf_msg_handler_t)(const char*); @@ -42,15 +53,12 @@ typedef void (*srslte_rf_msg_handler_t)(const char*); SRSLTE_API int srslte_rf_open(srslte_rf_t *h, char *args); -SRSLTE_API int srslte_rf_open_th(srslte_rf_t *h, - char *args, - bool tx_gain_same_rx); - SRSLTE_API int srslte_rf_open_devname(srslte_rf_t *h, char *devname, - char *args, - bool agc_thread, - bool tx_gain_same_rx); + char *args); + +SRSLTE_API int srslte_rf_start_gain_thread(srslte_rf_t *rf, + bool tx_gain_same_rx); SRSLTE_API int srslte_rf_close(srslte_rf_t *h); diff --git a/srslte/lib/agc/src/agc.c b/srslte/lib/agc/src/agc.c index 94357c8e3..3d971427a 100644 --- a/srslte/lib/agc/src/agc.c +++ b/srslte/lib/agc/src/agc.c @@ -124,11 +124,11 @@ void srslte_agc_process(srslte_agc_t *q, cf_t *signal, uint32_t len) { if (!q->uhd_handler) { srslte_vec_sc_prod_cfc(signal, q->gain, signal, len); } else { - if (q->gain < 1) { - q->gain = 1.0; + if (gain_db < 0) { + gain_db = 0.0; } if (isinf(gain_db) || isnan(gain_db)) { - q->gain = 10.0; + gain_db = 10.0; } else { gain_uhd_db = q->set_gain_callback(q->uhd_handler, gain_db); q->gain = pow(10, gain_uhd_db/10); diff --git a/srslte/lib/rf/src/rf_blade_imp.c b/srslte/lib/rf/src/rf_blade_imp.c index f8d0db817..4afe2a2c9 100644 --- a/srslte/lib/rf/src/rf_blade_imp.c +++ b/srslte/lib/rf/src/rf_blade_imp.c @@ -172,16 +172,7 @@ float rf_blade_get_rssi(void *h) return 0; } -double rf_blade_set_rx_gain_th(void *h, double gain) -{ - return rf_blade_set_rx_gain(h, gain); -} - -void rf_blade_set_tx_rx_gain_offset(void *h, double offset) -{ -} - -int rf_blade_open(char *args, void **h, bool create_thread_gain, bool tx_gain_same_rx) +int rf_blade_open(char *args, void **h) { *h = NULL; diff --git a/srslte/lib/rf/src/rf_blade_imp.h b/srslte/lib/rf/src/rf_blade_imp.h index 3ea6104b7..f94342bb9 100644 --- a/srslte/lib/rf/src/rf_blade_imp.h +++ b/srslte/lib/rf/src/rf_blade_imp.h @@ -29,9 +29,7 @@ #include "srslte/rf/rf.h" SRSLTE_API int rf_blade_open(char *args, - void **handler, - bool agc_thread, - bool tx_gain_same_rx); + void **handler); SRSLTE_API int rf_blade_close(void *h); @@ -61,15 +59,6 @@ SRSLTE_API double rf_blade_set_rx_srate(void *h, SRSLTE_API double rf_blade_set_rx_gain(void *h, double gain); -SRSLTE_API void rf_blade_set_tx_rx_gain_offset(void *h, - double offset); - -SRSLTE_API double rf_blade_set_rx_gain_th(void *h, - double gain); - -SRSLTE_API double rf_blade_set_tx_gain_th(void *h, - double gain); - SRSLTE_API float rf_blade_get_rx_gain_offset(void *h); SRSLTE_API double rf_blade_get_rx_gain(void *h); diff --git a/srslte/lib/rf/src/rf_dev.h b/srslte/lib/rf/src/rf_dev.h index 9436655a6..c59d76d34 100644 --- a/srslte/lib/rf/src/rf_dev.h +++ b/srslte/lib/rf/src/rf_dev.h @@ -34,11 +34,9 @@ typedef struct { void (*srslte_rf_flush_buffer)(void *h); bool (*srslte_rf_has_rssi)(void *h); float (*srslte_rf_get_rssi)(void *h); - double (*srslte_rf_set_rx_gain_th)(void *h, double gain); - void (*srslte_rf_set_tx_rx_gain_offset)(void *h, double offset); void (*srslte_rf_suppress_stdout)(void *h); void (*srslte_rf_register_msg_handler)(void *h, srslte_rf_msg_handler_t msg_handler); - int (*srslte_rf_open)(char *args, void **h, bool agc_thread, bool tx_gain_same_rx); + int (*srslte_rf_open)(char *args, void **h); int (*srslte_rf_close)(void *h); void (*srslte_rf_set_master_clock_rate)(void *h, double rate); bool (*srslte_rf_is_master_clock_dynamic)(void *h); @@ -71,8 +69,6 @@ static rf_dev_t dev_uhd = { rf_uhd_flush_buffer, rf_uhd_has_rssi, rf_uhd_get_rssi, - rf_uhd_set_rx_gain_th, - rf_uhd_set_tx_rx_gain_offset, rf_uhd_suppress_stdout, rf_uhd_register_msg_handler, rf_uhd_open, @@ -106,8 +102,6 @@ static rf_dev_t dev_blade = { rf_blade_flush_buffer, rf_blade_has_rssi, rf_blade_get_rssi, - rf_blade_set_rx_gain_th, - rf_blade_set_tx_rx_gain_offset, rf_blade_suppress_stdout, rf_blade_register_msg_handler, rf_blade_open, diff --git a/srslte/lib/rf/src/rf_imp.c b/srslte/lib/rf/src/rf_imp.c index e7ed4b9c2..830e1cea3 100644 --- a/srslte/lib/rf/src/rf_imp.c +++ b/srslte/lib/rf/src/rf_imp.c @@ -39,15 +39,69 @@ int rf_get_available_devices(char **devnames, int max_strlen) { return i; } +double srslte_rf_set_rx_gain_th(srslte_rf_t *rf, double gain) +{ + if (gain > rf->new_rx_gain + 0.5 || gain < rf->new_rx_gain - 0.5) { + pthread_mutex_lock(&rf->mutex); + rf->new_rx_gain = gain; + pthread_cond_signal(&rf->cond); + pthread_mutex_unlock(&rf->mutex); + } + return gain; +} -int srslte_rf_open_devname(srslte_rf_t *rf, char *devname, char *args, bool agc_thread, bool tx_gain_same_rx) { +void srslte_rf_set_tx_rx_gain_offset(srslte_rf_t *rf, double offset) { + rf->tx_rx_gain_offset = offset; +} + +/* This thread listens for set_rx_gain commands to the USRP */ +static void* thread_gain_fcn(void *h) { + srslte_rf_t* rf = (srslte_rf_t*) h; + + while(1) { + pthread_mutex_lock(&rf->mutex); + while(rf->cur_rx_gain == rf->new_rx_gain) + { + pthread_cond_wait(&rf->cond, &rf->mutex); + } + if (rf->new_rx_gain != rf->cur_rx_gain) { + rf->cur_rx_gain = rf->new_rx_gain; + srslte_rf_set_rx_gain(h, rf->cur_rx_gain); + } + if (rf->tx_gain_same_rx) { + srslte_rf_set_tx_gain(h, rf->cur_rx_gain+rf->tx_rx_gain_offset); + } + pthread_mutex_unlock(&rf->mutex); + } + return NULL; +} + + +/* Create auxiliary thread and mutexes for AGC */ +int srslte_rf_start_gain_thread(srslte_rf_t *rf, bool tx_gain_same_rx) { + rf->tx_gain_same_rx = tx_gain_same_rx; + rf->tx_rx_gain_offset = 0.0; + if (pthread_mutex_init(&rf->mutex, NULL)) { + return -1; + } + if (pthread_cond_init(&rf->cond, NULL)) { + return -1; + } + if (pthread_create(&rf->thread_gain, NULL, thread_gain_fcn, rf)) { + perror("pthread_create"); + return -1; + } + return 0; +} + +int srslte_rf_open_devname(srslte_rf_t *rf, char *devname, char *args) { /* Try to open the device if name is provided */ if (devname) { int i=0; while(available_devices[i] != NULL) { if (!strcmp(available_devices[i]->name, devname)) { rf->dev = available_devices[i]; - return available_devices[i]->srslte_rf_open(args, &rf->handler, agc_thread, tx_gain_same_rx); + return available_devices[i]->srslte_rf_open(args, &rf->handler); } i++; } @@ -57,7 +111,7 @@ int srslte_rf_open_devname(srslte_rf_t *rf, char *devname, char *args, bool agc_ /* If in auto mode or provided device not found, try to open in order of apperance in available_devices[] array */ int i=0; while(available_devices[i] != NULL) { - if (!available_devices[i]->srslte_rf_open(args, &rf->handler, agc_thread, tx_gain_same_rx)) { + if (!available_devices[i]->srslte_rf_open(args, &rf->handler)) { rf->dev = available_devices[i]; return 0; } @@ -98,16 +152,6 @@ float srslte_rf_get_rssi(srslte_rf_t *rf) return ((rf_dev_t*) rf->dev)->srslte_rf_get_rssi(rf->handler); } -double srslte_rf_set_rx_gain_th(srslte_rf_t *rf, double gain) -{ - return ((rf_dev_t*) rf->dev)->srslte_rf_set_rx_gain_th(rf->handler, gain); -} - -void srslte_rf_set_tx_rx_gain_offset(srslte_rf_t *rf, double offset) -{ - ((rf_dev_t*) rf->dev)->srslte_rf_set_tx_rx_gain_offset(rf->handler, offset); -} - void srslte_rf_suppress_stdout(srslte_rf_t *rf) { ((rf_dev_t*) rf->dev)->srslte_rf_suppress_stdout(rf->handler); @@ -120,12 +164,7 @@ void srslte_rf_register_msg_handler(srslte_rf_t *rf, srslte_rf_msg_handler_t msg int srslte_rf_open(srslte_rf_t *h, char *args) { - return srslte_rf_open_devname(h, NULL, args, false, false); -} - -int srslte_rf_open_th(srslte_rf_t *h, char *args, bool tx_gain_same_rx) -{ - return srslte_rf_open_devname(h, NULL, args, true, tx_gain_same_rx); + return srslte_rf_open_devname(h, NULL, args); } int srslte_rf_close(srslte_rf_t *rf) diff --git a/srslte/lib/rf/src/rf_uhd_imp.c b/srslte/lib/rf/src/rf_uhd_imp.c index 3f15e7304..3432ab15e 100644 --- a/srslte/lib/rf/src/rf_uhd_imp.c +++ b/srslte/lib/rf/src/rf_uhd_imp.c @@ -43,15 +43,7 @@ typedef struct { uhd_rx_metadata_handle rx_md, rx_md_first; uhd_tx_metadata_handle tx_md; - // The following variables are for threaded RX gain control uhd_meta_range_handle rx_gain_range; - pthread_t thread_gain; - pthread_cond_t cond; - pthread_mutex_t mutex; - double cur_rx_gain; - double new_rx_gain; - bool tx_gain_same_rx; - float tx_rx_gain_offset; size_t rx_nof_samples; size_t tx_nof_samples; double tx_rate; @@ -190,47 +182,7 @@ float rf_uhd_get_rssi(void *h) { return val_out; } -double rf_uhd_set_rx_gain_th(void *h, double gain) -{ - rf_uhd_handler_t *handler = (rf_uhd_handler_t*) h; - double gain_clipped; - uhd_meta_range_clip(handler->rx_gain_range, gain, true, &gain_clipped); - if (gain_clipped > handler->new_rx_gain + 0.5 || gain_clipped < handler->new_rx_gain - 0.5) { - pthread_mutex_lock(&handler->mutex); - handler->new_rx_gain = gain_clipped; - pthread_cond_signal(&handler->cond); - pthread_mutex_unlock(&handler->mutex); - } - return gain_clipped; -} - -void rf_uhd_set_tx_rx_gain_offset(void *h, double offset) { - rf_uhd_handler_t *handler = (rf_uhd_handler_t*) h; - handler->tx_rx_gain_offset = offset; -} - -/* This thread listens for set_rx_gain commands to the USRP */ -static void* thread_gain_fcn(void *h) { - rf_uhd_handler_t *handler = (rf_uhd_handler_t*) h; - while(1) { - pthread_mutex_lock(&handler->mutex); - while(handler->cur_rx_gain == handler->new_rx_gain) - { - pthread_cond_wait(&handler->cond, &handler->mutex); - } - if (handler->new_rx_gain != handler->cur_rx_gain) { - handler->cur_rx_gain = handler->new_rx_gain; - rf_uhd_set_rx_gain(h, handler->cur_rx_gain); - } - if (handler->tx_gain_same_rx) { - rf_uhd_set_tx_gain(h, handler->cur_rx_gain+handler->tx_rx_gain_offset); - } - pthread_mutex_unlock(&handler->mutex); - } - return NULL; -} - -int rf_uhd_open(char *args, void **h, bool create_thread_gain, bool tx_gain_same_rx) +int rf_uhd_open(char *args, void **h) { *h = NULL; @@ -291,8 +243,6 @@ int rf_uhd_open(char *args, void **h, bool create_thread_gain, bool tx_gain_same uhd_rx_streamer_max_num_samps(handler->rx_stream, &handler->rx_nof_samples); uhd_tx_streamer_max_num_samps(handler->tx_stream, &handler->tx_nof_samples); - handler->tx_gain_same_rx = tx_gain_same_rx; - handler->tx_rx_gain_offset = 0.0; uhd_meta_range_make(&handler->rx_gain_range); uhd_usrp_get_rx_gain_range(handler->usrp, "", 0, handler->rx_gain_range); @@ -300,20 +250,6 @@ int rf_uhd_open(char *args, void **h, bool create_thread_gain, bool tx_gain_same uhd_rx_metadata_make(&handler->rx_md); uhd_rx_metadata_make(&handler->rx_md_first); uhd_tx_metadata_make(&handler->tx_md, false, 0, 0, false, false); - - /* Create auxiliary thread and mutexes for AGC */ - if (create_thread_gain) { - if (pthread_mutex_init(&handler->mutex, NULL)) { - return -1; - } - if (pthread_cond_init(&handler->cond, NULL)) { - return -1; - } - if (pthread_create(&handler->thread_gain, NULL, thread_gain_fcn, handler)) { - perror("pthread_create"); - return -1; - } - } /* Find out if the master clock rate is configurable */ double cur_clock, new_clock; diff --git a/srslte/lib/rf/src/rf_uhd_imp.h b/srslte/lib/rf/src/rf_uhd_imp.h index 666050899..390f0f57e 100644 --- a/srslte/lib/rf/src/rf_uhd_imp.h +++ b/srslte/lib/rf/src/rf_uhd_imp.h @@ -31,9 +31,7 @@ #include "srslte/rf/rf.h" SRSLTE_API int rf_uhd_open(char *args, - void **handler, - bool agc_thread, - bool tx_gain_same_rx); + void **handler); SRSLTE_API int rf_uhd_close(void *h); @@ -63,17 +61,6 @@ SRSLTE_API double rf_uhd_set_rx_srate(void *h, SRSLTE_API double rf_uhd_set_rx_gain(void *h, double gain); -SRSLTE_API void rf_uhd_set_tx_rx_gain_offset(void *h, - double offset); - -SRSLTE_API double rf_uhd_set_rx_gain_th(void *h, - double gain); - -SRSLTE_API double rf_uhd_set_tx_gain_th(void *h, - double gain); - -SRSLTE_API float rf_uhd_get_rx_gain_offset(void *h); - SRSLTE_API double rf_uhd_get_rx_gain(void *h); SRSLTE_API double rf_uhd_get_tx_gain(void *h);