From 12d58bbb625395cc276e04e99a59ccfde94c24c4 Mon Sep 17 00:00:00 2001 From: yagoda Date: Thu, 13 Jul 2017 15:14:22 +0100 Subject: [PATCH 1/2] fixing soapy Tx for lime and RTL-SDR --- lib/src/phy/rf/rf_soapy_imp.c | 95 ++++++++++++++++++++++++----------- 1 file changed, 66 insertions(+), 29 deletions(-) diff --git a/lib/src/phy/rf/rf_soapy_imp.c b/lib/src/phy/rf/rf_soapy_imp.c index 8fcf57c24..760b9b880 100644 --- a/lib/src/phy/rf/rf_soapy_imp.c +++ b/lib/src/phy/rf/rf_soapy_imp.c @@ -43,9 +43,14 @@ typedef struct { SoapySDRRange *ranges; SoapySDRStream *rxStream; SoapySDRStream *txStream; + bool tx_stream_active; } rf_soapy_handler_t; +cf_t zero_mem[64*1024]; + + + int soapy_error(void *h) { return 0; @@ -82,6 +87,7 @@ void rf_soapy_register_error_handler(void *notused, srslte_rf_error_handler_t ne } + char* rf_soapy_devname(void* h) { return "soapy"; @@ -120,15 +126,12 @@ int rf_soapy_start_rx_stream(void *h) int rf_soapy_start_tx_stream(void *h) { - rf_soapy_handler_t *handler = (rf_soapy_handler_t*) h; - if (SoapySDRDevice_setupStream(handler->device, &(handler->txStream), SOAPY_SDR_TX, SOAPY_SDR_CF32, NULL, 0, NULL) != 0) { - printf("setupStream fail: %s\n", SoapySDRDevice_lastError()); - return SRSLTE_ERROR; - } - +rf_soapy_handler_t *handler = (rf_soapy_handler_t*) h; if(SoapySDRDevice_activateStream(handler->device, handler->txStream, 0, 0, 0) != 0) return SRSLTE_ERROR; + handler->tx_stream_active = true; + return SRSLTE_SUCCESS; } @@ -210,12 +213,16 @@ int rf_soapy_open_multi(char *args, void **h, uint32_t nof_rx_antennas) bzero(handler, sizeof(rf_soapy_handler_t)); *h = handler; handler->device = sdr; - + handler->tx_stream_active = false; if (SoapySDRDevice_setupStream(handler->device, &(handler->rxStream), SOAPY_SDR_RX, SOAPY_SDR_CF32, NULL, 0, NULL) != 0) { printf("setupStream fail: %s\n", SoapySDRDevice_lastError()); return SRSLTE_ERROR; } - + + if (SoapySDRDevice_setupStream(handler->device, &(handler->txStream), SOAPY_SDR_TX, SOAPY_SDR_CF32, NULL, 0, NULL) != 0) { + printf("setupStream fail: %s\n", SoapySDRDevice_lastError()); + return SRSLTE_ERROR; + } return SRSLTE_SUCCESS; } @@ -300,7 +307,7 @@ double rf_soapy_set_tx_gain(void *h, double gain) printf("setGain fail: %s\n", SoapySDRDevice_lastError()); return SRSLTE_ERROR; } - return rf_soapy_get_rx_gain(h); + return rf_soapy_get_tx_gain(h); } @@ -338,7 +345,7 @@ double rf_soapy_set_tx_freq(void *h, double freq) printf("setFrequency fail: %s\n", SoapySDRDevice_lastError()); return SRSLTE_ERROR; } - return SoapySDRDevice_getFrequency(handler->device, SOAPY_SDR_RX, 0); + return SoapySDRDevice_getFrequency(handler->device, SOAPY_SDR_TX, 0); } @@ -413,23 +420,53 @@ int rf_soapy_recv_with_time(void *h, int rf_soapy_send_timed(void *h, - void *data, - int nsamples, - time_t secs, - double frac_secs, - bool has_time_spec, - bool blocking, - bool is_start_of_burst, - bool is_end_of_burst) -{ - int flags; - long long timeNs; - rf_soapy_handler_t *handler = (rf_soapy_handler_t*) h; - timeNs = secs * 1000000000; - timeNs = timeNs + (frac_secs * 1000000000); - int ret = SoapySDRDevice_writeStream(handler->device, handler->txStream, data, nsamples, &flags, timeNs, 100000); - if(ret != nsamples) - return SRSLTE_ERROR; - - return ret; + void *data, + int nsamples, + time_t secs, + double frac_secs, + bool has_time_spec, + bool blocking, + bool is_start_of_burst, + bool is_end_of_burst) +{ + + int flags; + long long timeNs; + int trials = 0; + int ret = 0; + rf_soapy_handler_t *handler = (rf_soapy_handler_t*) h; + timeNs = secs * 1000000000; + timeNs = timeNs + (frac_secs * 1000000000); + int num_channels = 1; + int n = 0; + + if(!handler->tx_stream_active){ + rf_soapy_start_tx_stream(h); + } + + + cf_t *data_c = (cf_t*) data; + do{ + size_t tx_samples = nsamples; + if (tx_samples > nsamples - n) + { + tx_samples = nsamples - n; + } + void *buff = (void*) &data_c[n]; + const void *buffs_ptr[1] = {buff}; + ret = SoapySDRDevice_writeStream(handler->device, handler->txStream, buffs_ptr, tx_samples, &flags, timeNs, 10000); + if(ret < 0) + return SRSLTE_ERROR; + n += ret; + trials++; + }while (n < nsamples && trials < 100); + + + if(ret != nsamples) + return SRSLTE_ERROR; + + + + return ret; + } From 8c049b2981ec3a53e4ef218a42c8dafc729e68f6 Mon Sep 17 00:00:00 2001 From: yagoda Date: Fri, 14 Jul 2017 09:41:00 +0100 Subject: [PATCH 2/2] fix for both lime and rtl --- lib/src/phy/rf/rf_soapy_imp.c | 42 +++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/lib/src/phy/rf/rf_soapy_imp.c b/lib/src/phy/rf/rf_soapy_imp.c index 760b9b880..ff57870ed 100644 --- a/lib/src/phy/rf/rf_soapy_imp.c +++ b/lib/src/phy/rf/rf_soapy_imp.c @@ -44,6 +44,7 @@ typedef struct { SoapySDRStream *rxStream; SoapySDRStream *txStream; bool tx_stream_active; + bool rx_stream_active; } rf_soapy_handler_t; @@ -116,7 +117,16 @@ void rf_soapy_set_rx_cal(void *h, srslte_rf_cal_t *cal) int rf_soapy_start_rx_stream(void *h) { rf_soapy_handler_t *handler = (rf_soapy_handler_t*) h; - + if(!handler->rxStream){ + if (SoapySDRDevice_setupStream(handler->device, &(handler->rxStream), SOAPY_SDR_RX, SOAPY_SDR_CF32, NULL, 0, NULL) != 0) { + printf("setupStream fail: %s\n", SoapySDRDevice_lastError()); + return SRSLTE_ERROR; + + + usleep(100000); + handler->rx_stream_active = true; + } + } if (SoapySDRDevice_activateStream(handler->device, handler->rxStream, 0, 0, 0) != 0) return SRSLTE_ERROR; @@ -126,11 +136,22 @@ int rf_soapy_start_rx_stream(void *h) int rf_soapy_start_tx_stream(void *h) { -rf_soapy_handler_t *handler = (rf_soapy_handler_t*) h; + rf_soapy_handler_t *handler = (rf_soapy_handler_t*) h; + if(!handler->txStream){ + if (SoapySDRDevice_setupStream(handler->device, &(handler->txStream), SOAPY_SDR_TX, SOAPY_SDR_CF32, NULL, 0, NULL) != 0) { + printf("setupStream fail: %s\n", SoapySDRDevice_lastError()); + return SRSLTE_ERROR; + } + + usleep(100000); + handler->tx_stream_active = true; + } + + if(SoapySDRDevice_activateStream(handler->device, handler->txStream, 0, 0, 0) != 0) return SRSLTE_ERROR; - handler->tx_stream_active = true; + return SRSLTE_SUCCESS; } @@ -214,15 +235,8 @@ int rf_soapy_open_multi(char *args, void **h, uint32_t nof_rx_antennas) *h = handler; handler->device = sdr; handler->tx_stream_active = false; - if (SoapySDRDevice_setupStream(handler->device, &(handler->rxStream), SOAPY_SDR_RX, SOAPY_SDR_CF32, NULL, 0, NULL) != 0) { - printf("setupStream fail: %s\n", SoapySDRDevice_lastError()); - return SRSLTE_ERROR; - } - if (SoapySDRDevice_setupStream(handler->device, &(handler->txStream), SOAPY_SDR_TX, SOAPY_SDR_CF32, NULL, 0, NULL) != 0) { - printf("setupStream fail: %s\n", SoapySDRDevice_lastError()); - return SRSLTE_ERROR; - } + return SRSLTE_SUCCESS; } @@ -372,6 +386,10 @@ int rf_soapy_recv_with_time_multi(void *h, int ret = 0; long long timeNs; //timestamp for receive buffer int n = 0; + + if(!handler->rxStream){ + rf_soapy_start_tx_stream(h); + } do { size_t rx_samples = nsamples; @@ -440,7 +458,7 @@ int rf_soapy_send_timed(void *h, int num_channels = 1; int n = 0; - if(!handler->tx_stream_active){ + if(!handler->txStream){ rf_soapy_start_tx_stream(h); }