blob: 95ef2b2a6c6407ce66cb555df223c6124929b24f [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
16
17#include "sigProcLib.h"
18#include "GSMCommon.h"
19#include "LinkedLists.h"
20#include "radioDevice.h"
kurtis.heimerl8aea56e2011-11-26 03:18:30 +000021#include "radioVector.h"
22#include "radioClock.h"
Thomas Tsoue90a42b2013-11-13 23:38:09 -050023#include "Resampler.h"
dburgessb3a0ca42011-10-12 07:44:40 +000024
Alexander Chemerisd734e2d2013-06-16 14:30:58 +040025static const unsigned gSlotLen = 148; ///< number of symbols per slot, not counting guard periods
26
dburgessb3a0ca42011-10-12 07:44:40 +000027/** class to interface the transceiver with the USRP */
28class RadioInterface {
29
Thomas Tsoucb69f082013-04-08 14:18:26 -040030protected:
dburgessb3a0ca42011-10-12 07:44:40 +000031
32 Thread mAlignRadioServiceLoopThread; ///< thread that synchronizes transmit and receive sections
33
Thomas Tsou204a9f12013-10-29 18:34:16 -040034 std::vector<VectorFIFO> mReceiveFIFO; ///< FIFO that holds receive bursts
dburgessb3a0ca42011-10-12 07:44:40 +000035
36 RadioDevice *mRadio; ///< the USRP object
Thomas Tsouc1f7c422013-10-11 13:49:55 -040037
Thomas Tsou204a9f12013-10-29 18:34:16 -040038 size_t mSPSTx;
39 size_t mSPSRx;
40 size_t mChans;
Thomas Tsoue90a42b2013-11-13 23:38:09 -050041 size_t mMIMO;
42
Thomas Tsou204a9f12013-10-29 18:34:16 -040043 std::vector<signalVector *> sendBuffer;
44 std::vector<signalVector *> recvBuffer;
dburgessb3a0ca42011-10-12 07:44:40 +000045 unsigned sendCursor;
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040046 unsigned recvCursor;
dburgessb3a0ca42011-10-12 07:44:40 +000047
Thomas Tsou204a9f12013-10-29 18:34:16 -040048 std::vector<short *> convertRecvBuffer;
49 std::vector<short *> convertSendBuffer;
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040050
dburgessb3a0ca42011-10-12 07:44:40 +000051 bool underrun; ///< indicates writes to USRP are too slow
52 bool overrun; ///< indicates reads from USRP are too slow
53 TIMESTAMP writeTimestamp; ///< sample timestamp of next packet written to USRP
54 TIMESTAMP readTimestamp; ///< sample timestamp of next packet read from USRP
55
56 RadioClock mClock; ///< the basestation clock!
57
dburgessb3a0ca42011-10-12 07:44:40 +000058 int receiveOffset; ///< offset b/w transmit and receive GSM timestamps, in timeslots
dburgessb3a0ca42011-10-12 07:44:40 +000059
60 bool mOn; ///< indicates radio is on
61
62 double powerScaling;
63
64 bool loadTest;
65 int mNumARFCNs;
66 signalVector *finalVec, *finalVec9;
67
Thomas Tsoucb69f082013-04-08 14:18:26 -040068private:
69
dburgessb3a0ca42011-10-12 07:44:40 +000070 /** format samples to USRP */
kurtis.heimerl9b557832011-11-26 03:18:34 +000071 int radioifyVector(signalVector &wVector,
72 float *floatVector,
kurtis.heimerl9b557832011-11-26 03:18:34 +000073 bool zero);
dburgessb3a0ca42011-10-12 07:44:40 +000074
75 /** format samples from USRP */
kurtis.heimerl9b557832011-11-26 03:18:34 +000076 int unRadioifyVector(float *floatVector, signalVector &wVector);
dburgessb3a0ca42011-10-12 07:44:40 +000077
78 /** push GSM bursts into the transmit buffer */
Thomas Tsoucb69f082013-04-08 14:18:26 -040079 virtual void pushBuffer(void);
dburgessb3a0ca42011-10-12 07:44:40 +000080
81 /** pull GSM bursts from the receive buffer */
Thomas Tsoucb69f082013-04-08 14:18:26 -040082 virtual void pullBuffer(void);
dburgessb3a0ca42011-10-12 07:44:40 +000083
84public:
85
86 /** start the interface */
87 void start();
88
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040089 /** intialization */
Thomas Tsoufe269fe2013-10-14 23:56:51 -040090 virtual bool init(int type);
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040091 virtual void close();
92
dburgessb3a0ca42011-10-12 07:44:40 +000093 /** constructor */
94 RadioInterface(RadioDevice* wRadio = NULL,
Thomas Tsoue90a42b2013-11-13 23:38:09 -050095 size_t sps = 4, size_t chans = 1, size_t diversity = 1,
96 int receiveOffset = 3, GSM::Time wStartTime = GSM::Time(0));
Thomas Tsou204a9f12013-10-29 18:34:16 -040097
dburgessb3a0ca42011-10-12 07:44:40 +000098 /** destructor */
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040099 virtual ~RadioInterface();
dburgessb3a0ca42011-10-12 07:44:40 +0000100
dburgessb3a0ca42011-10-12 07:44:40 +0000101 /** check for underrun, resets underrun value */
kurtis.heimerle724d6d2011-11-26 03:18:46 +0000102 bool isUnderrun();
dburgessb3a0ca42011-10-12 07:44:40 +0000103
104 /** return the receive FIFO */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400105 VectorFIFO* receiveFIFO(size_t chan = 0);
dburgessb3a0ca42011-10-12 07:44:40 +0000106
107 /** return the basestation clock */
108 RadioClock* getClock(void) { return &mClock;};
109
dburgessb3a0ca42011-10-12 07:44:40 +0000110 /** set transmit frequency */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400111 bool tuneTx(double freq, size_t chan = 0);
dburgessb3a0ca42011-10-12 07:44:40 +0000112
113 /** set receive frequency */
Thomas Tsoue90a42b2013-11-13 23:38:09 -0500114 virtual bool tuneRx(double freq, size_t chan = 0);
dburgessb3a0ca42011-10-12 07:44:40 +0000115
kurtis.heimerle724d6d2011-11-26 03:18:46 +0000116 /** set receive gain */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400117 double setRxGain(double dB, size_t chan = 0);
kurtis.heimerle724d6d2011-11-26 03:18:46 +0000118
119 /** get receive gain */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400120 double getRxGain(size_t chan = 0);
kurtis.heimerle724d6d2011-11-26 03:18:46 +0000121
dburgessb3a0ca42011-10-12 07:44:40 +0000122 /** drive transmission of GSM bursts */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400123 void driveTransmitRadio(std::vector<signalVector *> &bursts,
124 std::vector<bool> &zeros);
dburgessb3a0ca42011-10-12 07:44:40 +0000125
126 /** drive reception of GSM bursts */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400127 bool driveReceiveRadio();
dburgessb3a0ca42011-10-12 07:44:40 +0000128
Thomas Tsou204a9f12013-10-29 18:34:16 -0400129 void setPowerAttenuation(double atten, size_t chan = 0);
dburgessb3a0ca42011-10-12 07:44:40 +0000130
131 /** returns the full-scale transmit amplitude **/
132 double fullScaleInputValue();
133
134 /** returns the full-scale receive amplitude **/
135 double fullScaleOutputValue();
136
kurtis.heimerl6b495a52011-11-26 03:17:21 +0000137 /** set thread priority on current thread */
Thomas Tsou7553aa92013-11-08 12:50:03 -0500138 void setPriority(float prio = 0.5) { mRadio->setPriority(prio); }
dburgessb3a0ca42011-10-12 07:44:40 +0000139
Thomas Tsou02d88d12013-04-05 15:36:30 -0400140 /** get transport window type of attached device */
141 enum RadioDevice::TxWindowType getWindowType() { return mRadio->getWindowType(); }
kurtis.heimerle380af32011-11-26 03:18:55 +0000142
Thomas Tsouf2293b82013-04-08 13:35:36 -0400143#if USRP1
dburgessb3a0ca42011-10-12 07:44:40 +0000144protected:
145
146 /** drive synchronization of Tx/Rx of USRP */
147 void alignRadio();
148
dburgessb3a0ca42011-10-12 07:44:40 +0000149 friend void *AlignRadioServiceLoopAdapter(RadioInterface*);
Thomas Tsouf2293b82013-04-08 13:35:36 -0400150#endif
dburgessb3a0ca42011-10-12 07:44:40 +0000151};
152
Thomas Tsouf2293b82013-04-08 13:35:36 -0400153#if USRP1
dburgessb3a0ca42011-10-12 07:44:40 +0000154/** synchronization thread loop */
155void *AlignRadioServiceLoopAdapter(RadioInterface*);
Thomas Tsouf2293b82013-04-08 13:35:36 -0400156#endif
Thomas Tsoucb69f082013-04-08 14:18:26 -0400157
158class RadioInterfaceResamp : public RadioInterface {
159
160private:
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400161 signalVector *innerSendBuffer;
162 signalVector *outerSendBuffer;
163 signalVector *innerRecvBuffer;
164 signalVector *outerRecvBuffer;
Thomas Tsoucb69f082013-04-08 14:18:26 -0400165
166 void pushBuffer();
167 void pullBuffer();
168
169public:
170
Thomas Tsoue90a42b2013-11-13 23:38:09 -0500171 RadioInterfaceResamp(RadioDevice* wRadio, size_t wSPS = 4, size_t chans = 1);
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400172
173 ~RadioInterfaceResamp();
174
Thomas Tsoufe269fe2013-10-14 23:56:51 -0400175 bool init(int type);
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400176 void close();
Thomas Tsoucb69f082013-04-08 14:18:26 -0400177};
Thomas Tsoue90a42b2013-11-13 23:38:09 -0500178
179class RadioInterfaceDiversity : public RadioInterface {
180public:
181 RadioInterfaceDiversity(RadioDevice* wRadio,
182 size_t sps = 4, size_t chans = 2);
183
184 ~RadioInterfaceDiversity();
185
186 bool init(int type);
187 void close();
188 bool tuneRx(double freq, size_t chan);
189
190private:
191 std::vector<Resampler *> dnsamplers;
192 std::vector<float> phases;
193 signalVector *outerRecvBuffer;
194
195 bool mDiversity;
196 double mFreqSpacing;
197
198 bool setupDiversityChannels();
199 void pullBuffer();
200};