srsLTE/lib/examples/usrp_txrx.c

186 lines
5.0 KiB
C
Raw Normal View History

/**
2015-06-05 05:36:46 -07:00
*
* \section COPYRIGHT
2015-06-05 05:36:46 -07:00
*
2021-03-19 03:45:56 -07:00
* Copyright 2013-2021 Software Radio Systems Limited
2015-06-05 05:36:46 -07:00
*
* By using this file, you agree to the terms and conditions set
* forth in the LICENSE file which can be found at the top level of
* the distribution.
2015-06-05 05:36:46 -07:00
*
*/
#include <complex.h>
#include <math.h>
2015-06-05 05:36:46 -07:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
2015-06-05 05:36:46 -07:00
2021-03-19 03:45:56 -07:00
#include "srsran/phy/rf/rf.h"
#include "srsran/srsran.h"
2015-06-05 05:36:46 -07:00
2015-08-28 02:08:19 -07:00
uint32_t nof_prb = 25;
uint32_t nof_frames = 20;
2015-06-05 05:36:46 -07:00
int time_adv_samples = 0;
float tone_offset_hz = 1e6;
2021-03-19 03:45:56 -07:00
float rf_rx_gain = 40, srsran_rf_tx_gain = 40, rf_freq = 2.4e9;
char* rf_args = "";
char* output_filename = NULL;
char* input_filename = NULL;
2015-06-05 05:36:46 -07:00
void usage(char* prog)
{
2015-11-10 03:31:53 -08:00
printf("Usage: %s -o [rx_signal_file]\n", prog);
printf("\t-a RF args [Default %s]\n", rf_args);
printf("\t-f RF TX/RX frequency [Default %.2f MHz]\n", rf_freq / 1e6);
printf("\t-g RF RX gain [Default %.1f dB]\n", rf_rx_gain);
2021-03-19 03:45:56 -07:00
printf("\t-G RF TX gain [Default %.1f dB]\n", srsran_rf_tx_gain);
2015-12-16 08:38:04 -08:00
printf("\t-t Single tone offset (Hz) [Default %f]\n", tone_offset_hz);
printf("\t-T Time advance samples [Default %d]\n", time_adv_samples);
2015-11-10 03:31:53 -08:00
printf("\t-i File name to read signal from [Default single tone]\n");
printf("\t-p Number of UL RB [Default %d]\n", nof_prb);
2015-06-05 05:36:46 -07:00
}
void parse_args(int argc, char** argv)
{
2015-06-05 05:36:46 -07:00
int opt;
2015-12-16 08:38:04 -08:00
while ((opt = getopt(argc, argv, "ioafgGptT")) != -1) {
2015-06-05 05:36:46 -07:00
switch (opt) {
case 'a':
rf_args = argv[optind];
break;
case 'o':
output_filename = argv[optind];
break;
case 'i':
input_filename = argv[optind];
break;
case 't':
tone_offset_hz = strtof(argv[optind], NULL);
break;
case 'T':
time_adv_samples = (int)strtol(argv[optind], NULL, 10);
break;
case 'f':
rf_freq = strtof(argv[optind], NULL);
break;
case 'g':
rf_rx_gain = strtof(argv[optind], NULL);
break;
case 'G':
2021-03-19 03:45:56 -07:00
srsran_rf_tx_gain = strtof(argv[optind], NULL);
break;
case 'p':
nof_prb = (uint32_t)strtol(argv[optind], NULL, 10);
2021-03-19 03:45:56 -07:00
if (!srsran_nofprb_isvalid(nof_prb)) {
ERROR("Invalid number of UL RB %d", nof_prb);
exit(-1);
}
break;
default:
usage(argv[0]);
2015-06-05 05:36:46 -07:00
exit(-1);
}
}
2015-11-10 03:31:53 -08:00
if (!output_filename) {
2015-06-05 05:36:46 -07:00
usage(argv[0]);
exit(-1);
}
2015-12-16 08:38:04 -08:00
if (time_adv_samples < 0) {
printf("Time advance must be positive\n");
usage(argv[0]);
exit(-1);
}
2015-06-05 05:36:46 -07:00
}
int main(int argc, char** argv)
{
2015-06-05 05:36:46 -07:00
parse_args(argc, argv);
2021-03-19 03:45:56 -07:00
uint32_t flen = srsran_sampling_freq_hz(nof_prb) / 1000;
2021-03-19 03:45:56 -07:00
cf_t* rx_buffer = srsran_vec_cf_malloc(flen * nof_frames);
2015-06-05 05:36:46 -07:00
if (!rx_buffer) {
perror("malloc");
exit(-1);
}
2021-03-19 03:45:56 -07:00
cf_t* tx_buffer = srsran_vec_cf_malloc((flen + time_adv_samples));
2015-06-05 05:36:46 -07:00
if (!tx_buffer) {
perror("malloc");
exit(-1);
}
2021-03-19 03:45:56 -07:00
srsran_vec_cf_zero(tx_buffer, flen + time_adv_samples);
2021-03-19 03:45:56 -07:00
cf_t* zeros = srsran_vec_cf_malloc(flen);
2015-06-05 05:36:46 -07:00
if (!zeros) {
perror("calloc");
exit(-1);
}
2021-03-19 03:45:56 -07:00
srsran_vec_cf_zero(zeros, flen);
2015-12-16 08:38:04 -08:00
2021-03-19 03:45:56 -07:00
float time_adv_sec = (float)time_adv_samples / srsran_sampling_freq_hz(nof_prb);
// Send through RF
2021-03-19 03:45:56 -07:00
srsran_rf_t rf;
printf("Opening RF device...\n");
2021-03-19 03:45:56 -07:00
if (srsran_rf_open(&rf, rf_args)) {
ERROR("Error opening rf");
2015-06-05 05:36:46 -07:00
exit(-1);
}
2021-03-19 03:45:56 -07:00
int srate = srsran_sampling_freq_hz(nof_prb);
srsran_rf_set_rx_srate(&rf, (double)srate);
srsran_rf_set_tx_srate(&rf, (double)srate);
2021-03-19 03:45:56 -07:00
srsran_rf_set_rx_gain(&rf, rf_rx_gain);
srsran_rf_set_tx_gain(&rf, srsran_rf_tx_gain);
srsran_rf_set_rx_freq(&rf, 0, rf_freq);
2015-06-05 05:36:46 -07:00
printf("Subframe len: %d samples\n", flen);
printf("Time advance: %f us\n", time_adv_sec * 1e6);
printf("Set TX/RX rate: %.2f MHz\n", (float)srate / 1000000);
printf("Set RX gain: %.1f dB\n", rf_rx_gain);
2021-03-19 03:45:56 -07:00
printf("Set TX gain: %.1f dB\n", srsran_rf_tx_gain);
printf("Set TX/RX freq: %.2f MHz\n", rf_freq / 1000000);
2021-03-19 03:45:56 -07:00
srsran_rf_set_tx_freq(&rf, 0, rf_freq);
2019-04-23 01:53:11 -07:00
2015-06-05 05:36:46 -07:00
sleep(1);
2015-11-10 03:31:53 -08:00
if (input_filename) {
2021-03-19 03:45:56 -07:00
srsran_vec_load_file(input_filename, &tx_buffer[time_adv_samples], flen * sizeof(cf_t));
2015-11-10 03:31:53 -08:00
} else {
for (int i = 0; i < flen - time_adv_samples; i++) {
tx_buffer[i + time_adv_samples] = 0.3 * cexpf(_Complex_I * 2 * M_PI * tone_offset_hz * ((float)i / (float)srate));
2015-11-10 03:31:53 -08:00
}
2021-03-19 03:45:56 -07:00
srsran_vec_save_file("srsran_rf_txrx_tone", tx_buffer, flen * sizeof(cf_t));
2015-11-10 03:31:53 -08:00
}
2015-06-05 05:36:46 -07:00
2021-03-19 03:45:56 -07:00
srsran_timestamp_t tstamp;
2021-03-19 03:45:56 -07:00
srsran_rf_start_rx_stream(&rf, false);
uint32_t nframe = 0;
2015-06-05 13:31:28 -07:00
while (nframe < nof_frames) {
2015-06-05 05:36:46 -07:00
printf("Rx subframe %d\n", nframe);
2021-03-19 03:45:56 -07:00
srsran_rf_recv_with_time(&rf, &rx_buffer[flen * nframe], flen, true, &tstamp.full_secs, &tstamp.frac_secs);
2015-06-05 05:36:46 -07:00
nframe++;
if (nframe == 9) {
2021-03-19 03:45:56 -07:00
srsran_timestamp_add(&tstamp, 0, 2e-3 - time_adv_sec);
srsran_rf_send_timed2(&rf, tx_buffer, flen + time_adv_samples, tstamp.full_secs, tstamp.frac_secs, true, true);
printf("Transmitting Signal\n");
2015-06-05 05:36:46 -07:00
}
}
2021-03-19 03:45:56 -07:00
srsran_vec_save_file(output_filename, &rx_buffer[10 * flen], flen * sizeof(cf_t));
2015-06-05 05:36:46 -07:00
free(tx_buffer);
free(rx_buffer);
printf("Done\n");
exit(0);
}