This commit is contained in:
ignasj
2016-08-17 11:43:05 +03:00
parent e1cdc46c67
commit d19b10ae06
68 changed files with 141 additions and 23871 deletions
+139 -196
View File
@@ -40,30 +40,21 @@
#include <unistd.h>
#include <errno.h>
#include "common_lib.h"
#include "LMS_SDR.h"
#include "LMS7002M.h"
#include "Si5351C.h"
#include "LMS_StreamBoard.h"
#include "LMS7002M_RegistersMap.h"
#include <cmath>
/** @addtogroup _LMSSDR_PHY_RF_INTERFACE_
* @{
*/
#include <lime/LimeSuite.h>
#include "common_lib.h"
///define for parameter enumeration if prefix might be needed
#define LMS7param(id) id
LMScomms* usbport;
LMScomms* comport;
LMS7002M* lms7;
Si5351C* Si;
LMS_StreamBoard *lmsStream;
lms_device_t* lms_device;
lms_stream_t rx_stream;
lms_stream_t tx_stream;
#define RXDCLENGTH 4096
#define NUMBUFF 40
#define NUMBUFF 32
int16_t cos_fsover8[8] = {2047, 1447, 0, -1448, -2047, -1448, 0, 1447};
int16_t cos_3fsover8[8] = {2047, -1448, 0, 1447, -2047, 1447, 0, -1448};
@@ -74,18 +65,23 @@ int write_output(const char *fname,const char *vname,void *data,int length,int d
/*! \brief Called to send samples to the LMSSDR RF target
\param device pointer to the device structure specific to the RF hardware target
\param timestamp The timestamp at whicch the first sample MUST be sent
\param timestamp The timestamp at whicch the first sample MUST be sent
\param buff Buffer which holds the samples
\param nsamps number of samples to be sent
\param antenna_id index of the antenna
\param flags Ignored for the moment
\returns 0 on success
*/
*/
int trx_lms_write(openair0_device *device, openair0_timestamp timestamp, void **buff, int nsamps, int antenna_id, int flags) {
LMS_TRxWrite((int16_t*)buff[0], nsamps,0, timestamp);
return nsamps;
lms_stream_meta_t meta;
meta.waitForTimestamp = true;
meta.flushPartialPacket = false;
meta.timestamp = timestamp;
return LMS_SendStream(&tx_stream,(const void*)buff[0],nsamps,&meta,30);
}
/*! \brief Receive samples from hardware.
@@ -100,16 +96,18 @@ int trx_lms_write(openair0_device *device, openair0_timestamp timestamp, void **
* \returns number of samples read
*/
int trx_lms_read(openair0_device *device, openair0_timestamp *ptimestamp, void **buff, int nsamps, int antenna_id) {
uint64_t timestamp;
int16_t *dst_ptr = (int16_t*) buff[0];
int ret;
ret = LMS_TRxRead(dst_ptr, nsamps,0,&timestamp, 10);
*ptimestamp=timestamp;
return ret;
lms_stream_meta_t meta;
meta.waitForTimestamp = false;
meta.flushPartialPacket = false;
int ret;
ret = LMS_RecvStream(&rx_stream,buff[0],nsamps,&meta,50);
*ptimestamp = meta.timestamp;
return ret;
}
/*! \brief set RX gain offset from calibration table
* \param openair0_cfg RF frontend parameters set by application
* \param chain_index RF chain ID
@@ -132,18 +130,17 @@ void set_rx_gain_offset(openair0_config_t *openair0_cfg, int chain_index) {
}
i++;
}
}
/*! \brief Set Gains (TX/RX) on LMSSDR
* \param device the hardware to use
* \param openair0_cfg openair0 Config structure
* \returns 0 in success
* \returns 0 in success
*/
int trx_lms_set_gains(openair0_device* device, openair0_config_t *openair0_cfg) {
double gv = openair0_cfg[0].rx_gain[0] - openair0_cfg[0].rx_gain_offset[0];
/* double gv = openair0_cfg[0].rx_gain[0] - openair0_cfg[0].rx_gain_offset[0];
if (gv > 31) {
printf("RX Gain 0 too high, reduce by %f dB\n",gv-31);
@@ -153,201 +150,145 @@ int trx_lms_set_gains(openair0_device* device, openair0_config_t *openair0_cfg)
printf("RX Gain 0 too low, increase by %f dB\n",-gv);
gv = 0;
}
printf("[LMS] Setting 7002M G_PGA_RBB to %d\n", (int16_t)gv);
lms7->Modify_SPI_Reg_bits(LMS7param(G_PGA_RBB),(int16_t)gv);
printf("[LMS] Setting 7002M G_PGA_RBB to %d\n", (uint16_t)gv);
LMS_WriteParam(lms_device,LMS7param(G_PGA_RBB),(uint16_t)gv);*/
return(0);
}
/*! \brief Start LMSSDR
* \param device the hardware to use
* \param device the hardware to use
* \returns 0 on success
*/
int trx_lms_start(openair0_device *device){
LMS_Init(0, 128*1024);
usbport = LMS_GetUSBPort();
//connect data stream port
LMS_UpdateDeviceList(usbport);
const char *name = LMS_GetDeviceName(usbport, 0);
printf("Connecting to device: %s\n",name);
lms_info_str_t list[16]={0};
int n= LMS_GetDeviceList(list);
if (LMS_DeviceOpen(usbport, 0)==0)
{
Si = new Si5351C();
lms7 = new LMS7002M(usbport);
liblms7_status opStatus;
printf("Configuring Si5351C\n");
Si->Initialize(usbport);
Si->SetPLL(0, 25000000, 0);
Si->SetPLL(1, 25000000, 0);
Si->SetClock(0, 27000000, true, false);
Si->SetClock(1, 27000000, true, false);
for (int i = 2; i < 8; ++i)
Si->SetClock(i, 27000000, false, false);
Si5351C::Status status = Si->ConfigureClocks();
if (status != Si5351C::SUCCESS)
{
printf("Failed to configure Si5351C");
exit(-1);
}
status = Si->UploadConfiguration();
if (status != Si5351C::SUCCESS)
printf("Failed to upload Si5351C configuration");
lms7->ResetChip();
opStatus = lms7->LoadConfig(device->openair0_cfg[0].configFilename);
if (opStatus != LIBLMS7_SUCCESS) {
printf("Failed to load configuration file %s\n",device->openair0_cfg[0].configFilename);
exit(-1);
if (n <= 0) {
fprintf(stderr, "No LimeSDR board found: %s\n", n < 0?LMS_GetLastErrorMessage():"");
return -1;
}
opStatus = lms7->UploadAll();
if (opStatus != LIBLMS7_SUCCESS) {
printf("Failed to upload configuration file\n");
printf("Connecting to device: %s\n",list[0]);
if (LMS_Open(&lms_device,list[0],NULL)<0) {
fprintf(stderr, "Can't open device port: %s\n",LMS_GetLastErrorMessage());
return -1;
}
LMS_Init(lms_device);
LMS_EnableCalibCache(lms_device,false);
if (LMS_LoadConfig(lms_device,device->openair0_cfg[0].configFilename) != 0)
{
printf("Failed to load configuration file %s\n%s",device->openair0_cfg[0].configFilename,LMS_GetLastErrorMessage());
exit(-1);
}
// Set TX filter
printf("Tuning TX filter\n");
opStatus = lms7->TuneTxFilter(LMS7002M::TxFilter::TX_HIGHBAND,device->openair0_cfg[0].tx_bw/1e6);
/* LMS_EnableChannel(lms_device,LMS_CH_RX,0,true);
LMS_EnableChannel(lms_device,LMS_CH_TX,0,true);
if (device->openair0_cfg->rx_num_channels == 2)
{
LMS_EnableChannel(lms_device,LMS_CH_RX,1,true);
LMS_EnableChannel(lms_device,LMS_CH_TX,1,true);
} */
LMS_VCTCXOWrite(lms_device,125);
if (opStatus != LIBLMS7_SUCCESS) {
printf("Warning: Could not tune TX filter to %f MHz\n",device->openair0_cfg[0].tx_bw/1e6);
if (LMS_SetSampleRate(lms_device,device->openair0_cfg->sample_rate,2)!=0)
{
fprintf(stderr, "Failed to set sample rate %s\n",LMS_GetLastErrorMessage());
return -1;
}
printf("Tuning RX filter\n");
opStatus = lms7->TuneRxFilter(LMS7002M::RxFilter::RX_LPF_LOWBAND,device->openair0_cfg[0].rx_bw/1e6);
printf("Set sample rate %f MHz\n",device->openair0_cfg->sample_rate/1e6);
if (opStatus != LIBLMS7_SUCCESS) {
printf("Warning: Could not tune RX filter to %f MHz\n",device->openair0_cfg[0].rx_bw/1e6);
if (LMS_SetLOFrequency(lms_device,LMS_CH_RX, 0, device->openair0_cfg[0].rx_freq[0])!=0)
{
fprintf(stderr, "Failed to Set Rx frequency: %s\n", LMS_GetLastErrorMessage());
return -1;
}
if (LMS_SetLOFrequency(lms_device,LMS_CH_TX, 0,device->openair0_cfg[0].tx_freq[0])!=0)
{
fprintf(stderr, "Failed to Set Tx frequency: %s\n", LMS_GetLastErrorMessage());
return -1;
}
printf("Set TX frequency %f MHz\n",device->openair0_cfg[0].tx_freq[0]/1e6);
/* printf("Tuning TIA filter\n");
opStatus = lms7->TuneRxFilter(LMS7002M::RxFilter::RX_TIA,7.0);
if (opStatus != LIBLMS7_SUCCESS) {
printf("Warning: Could not tune RX TIA filter\n");
}*/
opStatus = lms7->SetInterfaceFrequency(lms7->GetFrequencyCGEN_MHz(),
lms7->Get_SPI_Reg_bits(HBI_OVR_TXTSP),
lms7->Get_SPI_Reg_bits(HBD_OVR_RXTSP));
if (opStatus != LIBLMS7_SUCCESS) {
printf("SetInterfaceFrequency failed: %f,%d,%d\n",
lms7->GetFrequencyCGEN_MHz(),
lms7->Get_SPI_Reg_bits(HBI_OVR_TXTSP),
lms7->Get_SPI_Reg_bits(HBD_OVR_RXTSP));
}
else {
printf("SetInterfaceFrequency as %f,%d,%d\n",
lms7->GetFrequencyCGEN_MHz(),
lms7->Get_SPI_Reg_bits(HBI_OVR_TXTSP),
lms7->Get_SPI_Reg_bits(HBD_OVR_RXTSP));
}
lmsStream = new LMS_StreamBoard(usbport);
LMS_StreamBoard::Status opStreamStatus;
// this will configure that sampling rate at output of FPGA
opStreamStatus = lmsStream->ConfigurePLL(usbport,
device->openair0_cfg[0].sample_rate,
device->openair0_cfg[0].sample_rate,90);
if (opStatus != LIBLMS7_SUCCESS){
printf("Sample rate programming failed\n");
exit(-1);
}
opStatus = lms7->SetFrequencySX(LMS7002M::Tx, device->openair0_cfg[0].tx_freq[0]/1e6,30.72);
if (opStatus != LIBLMS7_SUCCESS) {
printf("Cannot set TX frequency %f MHz\n",device->openair0_cfg[0].tx_freq[0]/1e6);
exit(-1);
}
else {
printf("Set TX frequency %f MHz\n",device->openair0_cfg[0].tx_freq[0]/1e6);
}
opStatus = lms7->SetFrequencySX(LMS7002M::Rx, device->openair0_cfg[0].rx_freq[0]/1e6,30.72);
if (opStatus != LIBLMS7_SUCCESS) {
printf("Cannot set RX frequency %f MHz\n",device->openair0_cfg[0].rx_freq[0]/1e6);
exit(-1);
}
else {
printf("Set RX frequency %f MHz\n",device->openair0_cfg[0].rx_freq[0]/1e6);
}
printf("Override antenna settings to: RX1_W, TXA_2");
LMS_SetAntenna(lms_device, LMS_CH_RX, 0, 3);
LMS_SetAntenna(lms_device, LMS_CH_TX, 0, 2);
trx_lms_set_gains(device, device->openair0_cfg);
// Run calibration procedure
// calibrate_rf(device);
//lms7->CalibrateTx(5.0);
LMS_RxStart();
}
else
{
return(-1);
}
//connect control port
/*LMS_SetNormalizedGain(lms_device, LMS_CH_TX, 0, 0.175);
LMS_SetNormalizedGain(lms_device, LMS_CH_RX, 0, 0.65);*/
/* comport = LMS_GetCOMPort();
LMS_UpdateDeviceList(comport);
name = LMS_GetDeviceName(comport, 0);
if (*name == 0)
comport = usbport; //attempt to use data port
else
{
printf("Connecting to device: %s\n",name);
if (LMS_DeviceOpen(comport, 0)!=0)
return (-1);
}
lms7 = new LMS7002M(comport);
if( access( "./config.ini", F_OK ) != -1 ) //load config file
lms7->LoadConfig("./config.ini");
//calibration takes too long
//lms7->CalibrateRx(5.0);
//lms7->CalibrateTx(5.0);
*/
for (int i = 0; i< device->openair0_cfg->rx_num_channels; i++)
{
if (LMS_SetLPFBW(lms_device,LMS_CH_RX,i,device->openair0_cfg->rx_bw)!=0)
printf("RX ch:%d filter calibration failed, bw:%fMHz\n%s\n",i,device->openair0_cfg->rx_bw/1e6,LMS_GetLastErrorMessage());
if (LMS_SetLPFBW(lms_device,LMS_CH_TX,i,device->openair0_cfg->tx_bw)!=0)
printf("TX ch:%d filter calibration failed, bw:%fMHz\n%s\n",i,device->openair0_cfg->tx_bw/1e6,LMS_GetLastErrorMessage());
return 0;
if (LMS_Calibrate(lms_device,LMS_CH_RX,i,device->openair0_cfg->rx_bw,0)!=0)
printf("RX ch:%d calibration failed, bw:%f MHz\n%s\n",i,device->openair0_cfg->rx_bw/1e6,LMS_GetLastErrorMessage());
if (LMS_Calibrate(lms_device,LMS_CH_TX,i,device->openair0_cfg->tx_bw,0)!=0)
printf("TX ch:%d calibration failed, bw:%fMHz\n%s\n",i,device->openair0_cfg->tx_bw/1e6,LMS_GetLastErrorMessage());
}
rx_stream.channel = 0;
rx_stream.fifoSize = 256*1024;
rx_stream.throughputVsLatency = 0.1;
rx_stream.dataFmt = lms_stream_t::LMS_FMT_I16;
rx_stream.isTx = false;
if (LMS_SetupStream(lms_device, &rx_stream)!=0)
printf("RX stream setup failed %s\n",LMS_GetLastErrorMessage());
tx_stream.channel = 0;
tx_stream.fifoSize = 256*1024;
tx_stream.throughputVsLatency = 0.1;
tx_stream.dataFmt = lms_stream_t::LMS_FMT_I16;
tx_stream.isTx = true;
if (LMS_SetupStream(lms_device, &tx_stream)!=0)
printf("TX stream setup failed %s\n",LMS_GetLastErrorMessage());
printf("SR: %.3f MHz\n", (float)device->openair0_cfg->sample_rate / 1e6);
printf("SR: %.3f MHz\n", (float)device->openair0_cfg->sample_rate / 1e6);
if (LMS_StartStream(&rx_stream)!=0)
printf("Failed to start TX stream %s\n",LMS_GetLastErrorMessage());
if (LMS_StartStream(&tx_stream)!=0)
printf("Failed to start Rx stream %s\n",LMS_GetLastErrorMessage());
return 0;
}
/*! \brief Stop LMSSDR
* \param card Index of the RF card to use
* \param card Index of the RF card to use
* \returns 0 on success
*/
int trx_lms_stop(int card) {
/*
LMS_DeviceClose(usbport);
LMS_DeviceClose(comport);
delete lms7;
return LMS_Destroy();
*/
LMS_StopStream(&rx_stream);
LMS_StopStream(&tx_stream);
LMS_DestroyStream(lms_device,&rx_stream);
LMS_DestroyStream(lms_device,&tx_stream);
LMS_Close(lms_device);
}
/*! \brief Set frequencies (TX/RX)
* \param device the hardware to use
* \param openair0_cfg openair0 Config structure (ignored. It is there to comply with RF common API)
* \param exmimo_dump_config (ignored)
* \returns 0 in success
* \returns 0 in success
*/
int trx_lms_set_freq(openair0_device* device, openair0_config_t *openair0_cfg,int exmimo_dump_config) {
//Control port must be connected
lms7->SetFrequencySX(LMS7002M::Tx,openair0_cfg->tx_freq[0]/1e6,30.72);
lms7->SetFrequencySX(LMS7002M::Rx,openair0_cfg->rx_freq[0]/1e6,30.72);
//Control port must be connected
LMS_SetLOFrequency(lms_device,LMS_CH_TX,0,openair0_cfg->tx_freq[0]);
LMS_SetLOFrequency(lms_device,LMS_CH_RX,0,openair0_cfg->rx_freq[0]);
printf ("[LMS] rx frequency:%f;\n",openair0_cfg->rx_freq[0]/1e6);
set_rx_gain_offset(openair0_cfg,0);
return(0);
}
// 31 = 19 dB => 105 dB total gain @ 2.6 GHz
@@ -364,10 +305,12 @@ rx_gain_calib_table_t calib_table_sodera[] = {
/*! \brief Get LMSSDR Statistics
* \param device the hardware to use
* \returns 0 in success
* \returns 0 in success
*/
int trx_lms_get_stats(openair0_device* device) {
return(0);
@@ -376,7 +319,7 @@ int trx_lms_get_stats(openair0_device* device) {
/*! \brief Reset LMSSDR Statistics
* \param device the hardware to use
* \returns 0 in success
* \returns 0 in success
*/
int trx_lms_reset_stats(openair0_device* device) {
@@ -385,7 +328,7 @@ int trx_lms_reset_stats(openair0_device* device) {
}
/*! \brief Terminate operation of the LMSSDR transceiver -- free all associated resources
/*! \brief Terminate operation of the LMSSDR transceiver -- free all associated resources
* \param device the hardware to use
*/
void trx_lms_end(openair0_device *device) {
@@ -407,28 +350,28 @@ int device_init(openair0_device *device, openair0_config_t *openair0_cfg){
case 30720000:
// from usrp_time_offset
openair0_cfg[0].samples_per_packet = 2048;
openair0_cfg[0].tx_sample_advance = 15;
openair0_cfg[0].tx_sample_advance = 40;
openair0_cfg[0].tx_bw = 30.72e6;
openair0_cfg[0].rx_bw = 30.72e6;
openair0_cfg[0].tx_scheduling_advance = 8*openair0_cfg[0].samples_per_packet;
break;
case 15360000:
openair0_cfg[0].samples_per_packet = 2048;
openair0_cfg[0].tx_sample_advance = 45;
openair0_cfg[0].tx_bw = 28e6;
openair0_cfg[0].tx_sample_advance = 70;
openair0_cfg[0].tx_bw = 10e6;
openair0_cfg[0].rx_bw = 10e6;
openair0_cfg[0].tx_scheduling_advance = 8*openair0_cfg[0].samples_per_packet;
break;
case 7680000:
openair0_cfg[0].samples_per_packet = 1024;
openair0_cfg[0].tx_sample_advance = 70;
openair0_cfg[0].tx_bw = 28e6;
openair0_cfg[0].tx_sample_advance = 50;
openair0_cfg[0].tx_bw = 5.0e6;
openair0_cfg[0].rx_bw = 5.0e6;
openair0_cfg[0].tx_scheduling_advance = 8*openair0_cfg[0].samples_per_packet;
break;
case 1920000:
openair0_cfg[0].samples_per_packet = 256;
openair0_cfg[0].tx_sample_advance = 50;
openair0_cfg[0].tx_sample_advance = 10;
openair0_cfg[0].tx_bw = 1.25e6;
openair0_cfg[0].rx_bw = 1.25e6;
openair0_cfg[0].tx_scheduling_advance = 8*openair0_cfg[0].samples_per_packet;
@@ -445,14 +388,14 @@ int device_init(openair0_device *device, openair0_config_t *openair0_cfg){
device->Mod_id = 1;
device->trx_start_func = trx_lms_start;
device->trx_write_func = trx_lms_write;
device->trx_read_func = trx_lms_read;
device->trx_read_func = trx_lms_read;
device->trx_get_stats_func = trx_lms_get_stats;
device->trx_reset_stats_func = trx_lms_reset_stats;
device->trx_end_func = trx_lms_end;
device->trx_stop_func = trx_lms_stop;
device->trx_set_freq_func = trx_lms_set_freq;
device->trx_set_gains_func = trx_lms_set_gains;
device->openair0_cfg = openair0_cfg;
return 0;