blob: b560339140d817c20184855d9cab0021a9997fea [file] [log] [blame]
dburgessb3a0ca42011-10-12 07:44:40 +00001/*
2* Copyright 2008 Free Software Foundation, Inc.
3*
4* This software is distributed under multiple licenses; see the COPYING file in the main directory for licensing information for this specific distribuion.
5*
6* This use of this software may be subject to additional restrictions.
7* See the LEGAL file in the main directory for details.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13*/
14
15#ifndef _USRP_DEVICE_H_
16#define _USRP_DEVICE_H_
17
18#ifdef HAVE_CONFIG_H
19#include "config.h"
20#endif
21
22#include "radioDevice.h"
23
Thomas Tsouc0641242013-10-11 14:55:31 -040024#include <usrp/usrp_standard.h>
25#include <usrp/usrp_bytesex.h>
26#include <usrp/usrp_prims.h>
dburgessb3a0ca42011-10-12 07:44:40 +000027#include <sys/time.h>
28#include <math.h>
29#include <string>
30#include <iostream>
31
dburgessb3a0ca42011-10-12 07:44:40 +000032#include <boost/shared_ptr.hpp>
33typedef boost::shared_ptr<usrp_standard_tx> usrp_standard_tx_sptr;
34typedef boost::shared_ptr<usrp_standard_rx> usrp_standard_rx_sptr;
dburgessb3a0ca42011-10-12 07:44:40 +000035
36/** A class to handle a USRP rev 4, with a two RFX900 daughterboards */
37class USRPDevice: public RadioDevice {
38
39private:
40
41 static const double masterClockRate; ///< the USRP clock rate
42 double desiredSampleRate; ///< the desired sampling rate
43 usrp_standard_rx_sptr m_uRx; ///< the USRP receiver
44 usrp_standard_tx_sptr m_uTx; ///< the USRP transmitter
kurtis.heimerl79e71c92011-11-26 03:16:48 +000045
46 db_base_sptr m_dbRx; ///< rx daughterboard
47 db_base_sptr m_dbTx; ///< tx daughterboard
48 usrp_subdev_spec rxSubdevSpec;
49 usrp_subdev_spec txSubdevSpec;
50
Thomas Tsouc1f7c422013-10-11 13:49:55 -040051 int sps;
dburgessb3a0ca42011-10-12 07:44:40 +000052 double actualSampleRate; ///< the actual USRP sampling rate
53 unsigned int decimRate; ///< the USRP decimation rate
54
55 unsigned long long samplesRead; ///< number of samples read from USRP
56 unsigned long long samplesWritten; ///< number of samples sent to USRP
57
58 bool started; ///< flag indicates USRP has started
59 bool skipRx; ///< set if USRP is transmit-only.
60
61 static const unsigned int currDataSize_log2 = 21;
62 static const unsigned long currDataSize = (1 << currDataSize_log2);
63 short *data;
64 unsigned long dataStart;
65 unsigned long dataEnd;
66 TIMESTAMP timeStart;
67 TIMESTAMP timeEnd;
68 bool isAligned;
69
70 Mutex writeLock;
71
72 short *currData; ///< internal data buffer when reading from USRP
73 TIMESTAMP currTimestamp; ///< timestamp of internal data buffer
74 unsigned currLen; ///< size of internal data buffer
75
76 TIMESTAMP timestampOffset; ///< timestamp offset b/w Tx and Rx blocks
77 TIMESTAMP latestWriteTimestamp; ///< timestamp of most recent ping command
78 TIMESTAMP pingTimestamp; ///< timestamp of most recent ping response
Thomas Tsouc1f7c422013-10-11 13:49:55 -040079
80 long long pingOffset;
dburgessb3a0ca42011-10-12 07:44:40 +000081 unsigned long hi32Timestamp;
82 unsigned long lastPktTimestamp;
83
84 double rxGain;
85
86#ifdef SWLOOPBACK
87 short loopbackBuffer[1000000];
88 int loopbackBufferSize;
89 double samplePeriod;
90
91 struct timeval startTime;
92 struct timeval lastReadTime;
93 bool firstRead;
94#endif
95
dburgessb3a0ca42011-10-12 07:44:40 +000096 public:
97
98 /** Object constructor */
Tom Tsou5cd70dc2016-03-06 01:28:40 -080099 USRPDevice(size_t sps);
dburgessb3a0ca42011-10-12 07:44:40 +0000100
101 /** Instantiate the USRP */
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700102 int open(const std::string &, int, bool);
dburgessb3a0ca42011-10-12 07:44:40 +0000103
104 /** Start the USRP */
105 bool start();
106
107 /** Stop the USRP */
108 bool stop();
109
kurtis.heimerld9364002011-11-26 03:17:15 +0000110 /** Set priority not supported */
Thomas Tsou7553aa92013-11-08 12:50:03 -0500111 void setPriority(float prio = 0.5) { }
kurtis.heimerld9364002011-11-26 03:17:15 +0000112
Thomas Tsou02d88d12013-04-05 15:36:30 -0400113 enum TxWindowType getWindowType() { return TX_WINDOW_USRP1; }
kurtis.heimerle380af32011-11-26 03:18:55 +0000114
dburgessb3a0ca42011-10-12 07:44:40 +0000115 /**
116 Read samples from the USRP.
117 @param buf preallocated buf to contain read result
118 @param len number of samples desired
119 @param overrun Set if read buffer has been overrun, e.g. data not being read fast enough
120 @param timestamp The timestamp of the first samples to be read
121 @param underrun Set if USRP does not have data to transmit, e.g. data not being sent fast enough
122 @param RSSI The received signal strength of the read result
123 @return The number of samples actually read
124 */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400125 int readSamples(std::vector<short *> &buf, int len, bool *overrun,
126 TIMESTAMP timestamp = 0xffffffff, bool *underrun = NULL,
127 unsigned *RSSI = NULL);
dburgessb3a0ca42011-10-12 07:44:40 +0000128 /**
129 Write samples to the USRP.
130 @param buf Contains the data to be written.
131 @param len number of samples to write.
132 @param underrun Set if USRP does not have data to transmit, e.g. data not being sent fast enough
133 @param timestamp The timestamp of the first sample of the data buffer.
134 @param isControl Set if data is a control packet, e.g. a ping command
135 @return The number of samples actually written
136 */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400137 int writeSamples(std::vector<short *> &bufs, int len, bool *underrun,
138 TIMESTAMP timestamp = 0xffffffff, bool isControl = false);
139
dburgessb3a0ca42011-10-12 07:44:40 +0000140 /** Update the alignment between the read and write timestamps */
141 bool updateAlignment(TIMESTAMP timestamp);
Thomas Tsou204a9f12013-10-29 18:34:16 -0400142
dburgessb3a0ca42011-10-12 07:44:40 +0000143 /** Set the transmitter frequency */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400144 bool setTxFreq(double wFreq, size_t chan = 0);
dburgessb3a0ca42011-10-12 07:44:40 +0000145
146 /** Set the receiver frequency */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400147 bool setRxFreq(double wFreq, size_t chan = 0);
dburgessb3a0ca42011-10-12 07:44:40 +0000148
149 /** Returns the starting write Timestamp*/
kurtis.heimerlc8739b82011-11-02 00:06:34 +0000150 TIMESTAMP initialWriteTimestamp(void) { return 20000;}
dburgessb3a0ca42011-10-12 07:44:40 +0000151
152 /** Returns the starting read Timestamp*/
kurtis.heimerlc8739b82011-11-02 00:06:34 +0000153 TIMESTAMP initialReadTimestamp(void) { return 20000;}
dburgessb3a0ca42011-10-12 07:44:40 +0000154
155 /** returns the full-scale transmit amplitude **/
156 double fullScaleInputValue() {return 13500.0;}
157
158 /** returns the full-scale receive amplitude **/
159 double fullScaleOutputValue() {return 9450.0;}
160
161 /** sets the receive chan gain, returns the gain setting **/
Thomas Tsou204a9f12013-10-29 18:34:16 -0400162 double setRxGain(double dB, size_t chan = 0);
dburgessb3a0ca42011-10-12 07:44:40 +0000163
164 /** get the current receive gain */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400165 double getRxGain(size_t chan = 0) { return rxGain; }
dburgessb3a0ca42011-10-12 07:44:40 +0000166
167 /** return maximum Rx Gain **/
kurtis.heimerl79e71c92011-11-26 03:16:48 +0000168 double maxRxGain(void);
dburgessb3a0ca42011-10-12 07:44:40 +0000169
170 /** return minimum Rx Gain **/
kurtis.heimerl79e71c92011-11-26 03:16:48 +0000171 double minRxGain(void);
dburgessb3a0ca42011-10-12 07:44:40 +0000172
173 /** sets the transmit chan gain, returns the gain setting **/
Thomas Tsou204a9f12013-10-29 18:34:16 -0400174 double setTxGain(double dB, size_t chan = 0);
dburgessb3a0ca42011-10-12 07:44:40 +0000175
176 /** return maximum Tx Gain **/
kurtis.heimerl79e71c92011-11-26 03:16:48 +0000177 double maxTxGain(void);
dburgessb3a0ca42011-10-12 07:44:40 +0000178
179 /** return minimum Rx Gain **/
kurtis.heimerl79e71c92011-11-26 03:16:48 +0000180 double minTxGain(void);
dburgessb3a0ca42011-10-12 07:44:40 +0000181
dburgessb3a0ca42011-10-12 07:44:40 +0000182 /** Return internal status values */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400183 inline double getTxFreq(size_t chan = 0) { return 0; }
184 inline double getRxFreq(size_t chan = 0) { return 0; }
185 inline double getSampleRate() { return actualSampleRate; }
dburgessb3a0ca42011-10-12 07:44:40 +0000186 inline double numberRead() { return samplesRead; }
Thomas Tsou204a9f12013-10-29 18:34:16 -0400187 inline double numberWritten() { return samplesWritten; }
dburgessb3a0ca42011-10-12 07:44:40 +0000188
189};
190
191#endif // _USRP_DEVICE_H_
192