Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 sysmocom - s.f.m.c. GmbH |
| 3 | * |
Pau Espin Pedrol | 21d03d3 | 2019-07-22 12:05:52 +0200 | [diff] [blame] | 4 | * SPDX-License-Identifier: AGPL-3.0+ |
| 5 | * |
| 6 | * This software is distributed under multiple licenses; see the COPYING file in |
Martin Hauke | 066fd04 | 2019-10-13 19:08:00 +0200 | [diff] [blame] | 7 | * the main directory for licensing information for this specific distribution. |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 8 | * |
| 9 | * This use of this software may be subject to additional restrictions. |
| 10 | * See the LEGAL file in the main directory for details. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 15 | |
| 16 | */ |
| 17 | |
| 18 | #ifndef _LMS_DEVICE_H_ |
| 19 | #define _LMS_DEVICE_H_ |
| 20 | |
Eric | c0f78a3 | 2023-05-12 13:00:14 +0200 | [diff] [blame^] | 21 | #include <map> |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 22 | #ifdef HAVE_CONFIG_H |
| 23 | #include "config.h" |
| 24 | #endif |
| 25 | |
| 26 | #include "radioDevice.h" |
Eric | c0f78a3 | 2023-05-12 13:00:14 +0200 | [diff] [blame^] | 27 | #include "bandmanager.h" |
Pau Espin Pedrol | dcbcfa5 | 2019-05-03 16:15:06 +0200 | [diff] [blame] | 28 | #include "smpl_buf.h" |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 29 | |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 30 | #include <sys/time.h> |
| 31 | #include <math.h> |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 32 | #include <limits.h> |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 33 | #include <string> |
| 34 | #include <iostream> |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 35 | #include <lime/LimeSuite.h> |
| 36 | |
Pau Espin Pedrol | f68f19b | 2020-06-19 14:48:09 +0200 | [diff] [blame] | 37 | extern "C" { |
| 38 | #include <osmocom/gsm/gsm_utils.h> |
| 39 | } |
| 40 | |
Harald Welte | 0277b58 | 2018-11-26 19:17:00 +0100 | [diff] [blame] | 41 | /* Definition of LIMESDR_TX_AMPL limits maximum amplitude of I and Q |
| 42 | * channels separately. Hence LIMESDR_TX_AMPL value must be 1/sqrt(2) = |
| 43 | * 0.7071.... to get an amplitude of 1 of the complex signal: |
| 44 | * A^2 = I^2 + Q^2 |
| 45 | * A^2 = (1/sqrt(2))^2 + (1/sqrt(2))^2 |
| 46 | * A^2 = 1/2 + 1/2 |
| 47 | * A^2 = 1 */ |
| 48 | #define LIMESDR_TX_AMPL 0.707 |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 49 | |
Pau Espin Pedrol | a7bf6cd | 2020-01-14 17:52:15 +0100 | [diff] [blame] | 50 | enum lms_dev_type { |
| 51 | LMS_DEV_SDR_USB, /* LimeSDR-USB */ |
| 52 | LMS_DEV_SDR_MINI, /* LimeSDR-Mini */ |
| 53 | LMS_DEV_NET_MICRO, /* LimeNet-micro */ |
| 54 | LMS_DEV_UNKNOWN, |
| 55 | }; |
| 56 | |
Pau Espin Pedrol | e91544d | 2020-10-13 17:03:37 +0200 | [diff] [blame] | 57 | struct dev_band_desc { |
| 58 | /* Maximum LimeSuite Tx Gain which can be set/used without distorting |
| 59 | the output * signal, and the resulting real output power measured |
| 60 | when that gain is used. |
| 61 | */ |
| 62 | double nom_lms_tx_gain; /* dB */ |
| 63 | double nom_out_tx_power; /* dBm */ |
| 64 | /* Factor used to infer base real RSSI offset on the Rx path based on current |
| 65 | configured RxGain. The resulting rssiOffset is added to the per burst |
| 66 | calculated energy in upper layers. These values were empirically |
| 67 | found and may change based on multiple factors, see OS#4468. |
| 68 | Correct measured values only provided for LimeSDR-USB so far. |
| 69 | rssiOffset = rxGain + rxgain2rssioffset_rel; |
| 70 | */ |
| 71 | double rxgain2rssioffset_rel; /* dB */ |
| 72 | }; |
Pau Espin Pedrol | f68f19b | 2020-06-19 14:48:09 +0200 | [diff] [blame] | 73 | |
Eric | c0f78a3 | 2023-05-12 13:00:14 +0200 | [diff] [blame^] | 74 | /* Device parameter descriptor */ |
| 75 | struct dev_desc { |
| 76 | /* Does LimeSuite allow switching the clock source for this device? |
| 77 | * LimeSDR-Mini does not have switches but needs soldering to select |
| 78 | * external/internal clock. Any call to LMS_SetClockFreq() will fail. |
| 79 | */ |
| 80 | bool clock_src_switchable; |
| 81 | /* Does LimeSuite allow using REF_INTERNAL for this device? |
| 82 | * LimeNET-Micro does not like selecting internal clock |
| 83 | */ |
| 84 | bool clock_src_int_usable; |
| 85 | /* Sample rate coef (without having TX/RX samples per symbol into account) */ |
| 86 | double rate; |
| 87 | /* Sample rate coef (without having TX/RX samples per symbol into account), if multi-arfcn is enabled */ |
| 88 | double rate_multiarfcn; |
| 89 | /* Coefficient multiplied by TX sample rate in order to shift Tx time */ |
| 90 | double ts_offset_coef; |
| 91 | /* Coefficient multiplied by TX sample rate in order to shift Tx time, if multi-arfcn is enabled */ |
| 92 | double ts_offset_coef_multiarfcn; |
| 93 | /* Device Name Prefix as presented by LimeSuite API LMS_GetDeviceInfo() */ |
| 94 | std::string desc_str; |
| 95 | }; |
| 96 | |
| 97 | using dev_band_key_t = std::tuple<lms_dev_type, gsm_band>; |
| 98 | using power_map_t = std::map<dev_band_key_t, dev_band_desc>; |
| 99 | using dev_map_t = std::map<lms_dev_type, struct dev_desc>; |
| 100 | |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 101 | /** A class to handle a LimeSuite supported device */ |
Eric | c0f78a3 | 2023-05-12 13:00:14 +0200 | [diff] [blame^] | 102 | class LMSDevice:public RadioDevice, public band_manager<power_map_t, dev_map_t> { |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 103 | |
| 104 | private: |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 105 | lms_device_t *m_lms_dev; |
| 106 | std::vector<lms_stream_t> m_lms_stream_rx; |
| 107 | std::vector<lms_stream_t> m_lms_stream_tx; |
| 108 | |
Pau Espin Pedrol | dcbcfa5 | 2019-05-03 16:15:06 +0200 | [diff] [blame] | 109 | std::vector<smpl_buf *> rx_buffers; |
| 110 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 111 | double actualSampleRate; ///< the actual USRP sampling rate |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 112 | |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 113 | bool started; ///< flag indicates LMS has started |
| 114 | bool skipRx; ///< set if LMS is transmit-only. |
| 115 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 116 | TIMESTAMP ts_initial, ts_offset; |
| 117 | |
Pau Espin Pedrol | 705a348 | 2019-09-13 16:51:48 +0200 | [diff] [blame] | 118 | std::vector<double> tx_gains, rx_gains; |
Pau Espin Pedrol | a7bf6cd | 2020-01-14 17:52:15 +0100 | [diff] [blame] | 119 | |
| 120 | enum lms_dev_type m_dev_type; |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 121 | |
Joachim Steiger | 4ce4555 | 2019-04-16 16:35:53 +0200 | [diff] [blame] | 122 | bool do_calib(size_t chan); |
| 123 | bool do_filters(size_t chan); |
Pau Espin Pedrol | e0010fa | 2019-08-26 17:10:27 +0200 | [diff] [blame] | 124 | void log_ant_list(bool dir_tx, size_t chan, std::ostringstream& os); |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 125 | int get_ant_idx(const std::string & name, bool dir_tx, size_t chan); |
| 126 | bool flush_recv(size_t num_pkts); |
Pau Espin Pedrol | 68a7809 | 2019-07-29 20:11:25 +0200 | [diff] [blame] | 127 | void update_stream_stats_rx(size_t chan, bool *overrun); |
| 128 | void update_stream_stats_tx(size_t chan, bool *underrun); |
Pau Espin Pedrol | a7bf6cd | 2020-01-14 17:52:15 +0100 | [diff] [blame] | 129 | bool do_clock_src_freq(enum ReferenceType ref, double freq); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 130 | public: |
| 131 | |
| 132 | /** Object constructor */ |
Eric | 19e134a | 2023-05-10 23:50:38 +0200 | [diff] [blame] | 133 | LMSDevice(InterfaceType iface, const struct trx_cfg *cfg); |
| 134 | ~LMSDevice(); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 135 | |
Eric | 19e134a | 2023-05-10 23:50:38 +0200 | [diff] [blame] | 136 | /** Instantiate the LMS */ |
| 137 | int open(); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 138 | |
Eric | 19e134a | 2023-05-10 23:50:38 +0200 | [diff] [blame] | 139 | /** Start the LMS */ |
| 140 | bool start(); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 141 | |
Eric | 19e134a | 2023-05-10 23:50:38 +0200 | [diff] [blame] | 142 | /** Stop the LMS */ |
| 143 | bool stop(); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 144 | |
Eric | 19e134a | 2023-05-10 23:50:38 +0200 | [diff] [blame] | 145 | enum TxWindowType getWindowType() |
| 146 | { |
| 147 | return TX_WINDOW_LMS1; |
| 148 | } |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 149 | |
| 150 | /** |
| 151 | Read samples from the LMS. |
| 152 | @param buf preallocated buf to contain read result |
| 153 | @param len number of samples desired |
| 154 | @param overrun Set if read buffer has been overrun, e.g. data not being read fast enough |
| 155 | @param timestamp The timestamp of the first samples to be read |
| 156 | @param underrun Set if LMS does not have data to transmit, e.g. data not being sent fast enough |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 157 | @return The number of samples actually read |
| 158 | */ |
| 159 | int readSamples(std::vector < short *>&buf, int len, bool * overrun, |
| 160 | TIMESTAMP timestamp = 0xffffffff, bool * underrun = |
Pau Espin Pedrol | f8c0c46 | 2020-03-12 19:08:46 +0100 | [diff] [blame] | 161 | NULL); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 162 | /** |
| 163 | Write samples to the LMS. |
| 164 | @param buf Contains the data to be written. |
| 165 | @param len number of samples to write. |
| 166 | @param underrun Set if LMS does not have data to transmit, e.g. data not being sent fast enough |
| 167 | @param timestamp The timestamp of the first sample of the data buffer. |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 168 | @return The number of samples actually written |
| 169 | */ |
| 170 | int writeSamples(std::vector < short *>&bufs, int len, bool * underrun, |
Pau Espin Pedrol | dfc6e5f | 2020-03-12 19:35:33 +0100 | [diff] [blame] | 171 | TIMESTAMP timestamp = 0xffffffff); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 172 | |
| 173 | /** Update the alignment between the read and write timestamps */ |
| 174 | bool updateAlignment(TIMESTAMP timestamp); |
| 175 | |
| 176 | /** Set the transmitter frequency */ |
| 177 | bool setTxFreq(double wFreq, size_t chan = 0); |
| 178 | |
| 179 | /** Set the receiver frequency */ |
| 180 | bool setRxFreq(double wFreq, size_t chan = 0); |
| 181 | |
| 182 | /** Returns the starting write Timestamp*/ |
| 183 | TIMESTAMP initialWriteTimestamp(void) { |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 184 | return ts_initial; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | /** Returns the starting read Timestamp*/ |
| 188 | TIMESTAMP initialReadTimestamp(void) { |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 189 | return ts_initial; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | /** returns the full-scale transmit amplitude **/ |
| 193 | double fullScaleInputValue() { |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 194 | return(double) SHRT_MAX * LIMESDR_TX_AMPL; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /** returns the full-scale receive amplitude **/ |
| 198 | double fullScaleOutputValue() { |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 199 | return (double) SHRT_MAX; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /** sets the receive chan gain, returns the gain setting **/ |
| 203 | double setRxGain(double dB, size_t chan = 0); |
| 204 | |
| 205 | /** get the current receive gain */ |
| 206 | double getRxGain(size_t chan = 0) { |
Pau Espin Pedrol | 705a348 | 2019-09-13 16:51:48 +0200 | [diff] [blame] | 207 | return rx_gains[chan]; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /** return maximum Rx Gain **/ |
| 211 | double maxRxGain(void); |
| 212 | |
| 213 | /** return minimum Rx Gain **/ |
| 214 | double minRxGain(void); |
| 215 | |
Pau Espin Pedrol | e91544d | 2020-10-13 17:03:37 +0200 | [diff] [blame] | 216 | double rssiOffset(size_t chan); |
Pau Espin Pedrol | f68f19b | 2020-06-19 14:48:09 +0200 | [diff] [blame] | 217 | |
| 218 | double setPowerAttenuation(int atten, size_t chan); |
| 219 | double getPowerAttenuation(size_t chan = 0); |
| 220 | |
Pau Espin Pedrol | 0e09e7c | 2020-05-29 16:39:07 +0200 | [diff] [blame] | 221 | int getNominalTxPower(size_t chan = 0); |
| 222 | |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 223 | /** sets the RX path to use, returns true if successful and false otherwise */ |
| 224 | bool setRxAntenna(const std::string & ant, size_t chan = 0); |
| 225 | |
| 226 | /* return the used RX path */ |
| 227 | std::string getRxAntenna(size_t chan = 0); |
| 228 | |
| 229 | /** sets the RX path to use, returns true if successful and false otherwise */ |
| 230 | bool setTxAntenna(const std::string & ant, size_t chan = 0); |
| 231 | |
| 232 | /* return the used RX path */ |
| 233 | std::string getTxAntenna(size_t chan = 0); |
| 234 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 235 | /** return whether user drives synchronization of Tx/Rx of USRP */ |
| 236 | bool requiresRadioAlign(); |
| 237 | |
| 238 | /** return whether user drives synchronization of Tx/Rx of USRP */ |
| 239 | virtual GSM::Time minLatency(); |
| 240 | |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 241 | /** Return internal status values */ |
| 242 | inline double getTxFreq(size_t chan = 0) { |
| 243 | return 0; |
| 244 | } |
| 245 | inline double getRxFreq(size_t chan = 0) { |
| 246 | return 0; |
| 247 | } |
| 248 | inline double getSampleRate() { |
| 249 | return actualSampleRate; |
| 250 | } |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 251 | }; |
| 252 | |
| 253 | #endif // _LMS_DEVICE_H_ |