fun_ofdm  1.0
802.11a Physical Layer for USRP
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Macros Pages
usrp.cpp
Go to the documentation of this file.
1 
11 #include "usrp.h"
12 
13 namespace fun
14 {
21  m_params(params)
22  {
23  // Instantiate the multi_usrp
24  m_usrp = uhd::usrp::multi_usrp::make(uhd::device_addr_t(m_params.device_addr));
25  m_device = m_usrp->get_device();
26 
27  // Set the center frequency
28  m_usrp->set_tx_freq(uhd::tune_request_t(m_params.freq));
29  m_usrp->set_rx_freq(uhd::tune_request_t(m_params.freq));
30 
31  // Set the sample rate
32  m_usrp->set_tx_rate(m_params.rate);
33  m_usrp->set_rx_rate(m_params.rate);
34 
35  // Set the gains
36  m_usrp->set_tx_gain(m_params.tx_gain);
37  m_usrp->set_rx_gain(m_params.rx_gain);
38 
39  // Set the RX antenna
40  //m_usrp->set_rx_antenna("RX2");
41 
42  // Get the TX and RX stream handles
43  m_tx_streamer = m_usrp->get_tx_stream(uhd::stream_args_t("fc64"));
44  m_rx_streamer = m_usrp->get_rx_stream(uhd::stream_args_t("fc64"));
45 
46  // Start the RX stream
47  uhd::stream_cmd_t stream_cmd(uhd::stream_cmd_t::STREAM_MODE_START_CONTINUOUS);
48  stream_cmd.stream_now = true;
49  m_usrp->issue_stream_cmd(stream_cmd);
50 
51  sem_init(&m_tx_sem, 0, 0);
52  sem_post(&m_tx_sem);
53  }
54 
66  void usrp::send_burst(std::vector<std::complex<double> > samples)
67  {
68  sem_wait(&m_tx_sem);
69 
70  uhd::tx_metadata_t tx_metadata;
71  tx_metadata.start_of_burst = true;
72  tx_metadata.end_of_burst = true;
73  tx_metadata.has_time_spec = false;
74  m_tx_streamer->send(&samples[0], samples.size(), tx_metadata);
75 
76  sem_post(&m_tx_sem);
77  }
78 
79 
91  void usrp::send_burst_sync(std::vector<std::complex<double> > samples)
92  {
93  // Scale the samples by m_amp
94  if(m_params.tx_amp != 1.0)
95  for(int x = 0; x < samples.size(); x++)
96  samples[x] *= m_params.tx_amp;
97 
98  // Send the samples
99  uhd::tx_metadata_t tx_metadata;
100  tx_metadata.start_of_burst = true;
101  tx_metadata.end_of_burst = true;
102  tx_metadata.has_time_spec = false;
103  m_tx_streamer->send(&samples[0], samples.size(), tx_metadata);
104 
105  // Wait for the end of burst ACK followed by an underflow
106  bool got_ack = false;
107  bool got_underflow = false;
108  uhd::async_metadata_t async_metadata;
109  while(!got_ack && !got_underflow && m_device->recv_async_msg(async_metadata, 1))
110  {
111  got_ack = (async_metadata.event_code == uhd::async_metadata_t::EVENT_CODE_BURST_ACK);
112  got_underflow = (got_ack && async_metadata.event_code == uhd::async_metadata_t::EVENT_CODE_UNDERFLOW);
113  }
114  }
115 
125  void usrp::get_samples(int num_samples, std::vector<std::complex<double> > & buffer)
126  {
127  // Get some samples
128  uhd::rx_metadata_t rx_meta;
129  m_rx_streamer->recv(&buffer[0], num_samples, rx_meta);
130  }
131 
132 }
133