Merged r7478:7608 from michaelld/t186 into trunk. Adds ability to compile GNU Radio...
[debian/gnuradio] / usrp / host / apps-inband / test_gmac_tx.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2007 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio 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.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <mb_mblock.h>
27 #include <mb_runtime.h>
28 #include <mb_protocol_class.h>
29 #include <mb_exception.h>
30 #include <mb_msg_queue.h>
31 #include <mb_message.h>
32 #include <mb_msg_accepter.h>
33 #include <mb_class_registry.h>
34 #include <pmt.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <iostream>
38
39 #include <ui_nco.h>
40 #include <gmac.h>
41 #include <gmac_symbols.h>
42
43 static bool verbose = true;
44
45 class test_gmac_tx : public mb_mblock
46 {
47   mb_port_sptr  d_tx;
48   mb_port_sptr  d_cs;
49   pmt_t         d_tx_chan;      // returned tx channel handle
50
51   enum state_t {
52     INIT,
53     TRANSMITTING,
54   };
55
56   state_t       d_state;
57   long          d_nsamples_to_send;
58   long          d_nsamples_xmitted;
59   long          d_nframes_xmitted;
60   long          d_samples_per_frame;
61   bool          d_done_sending;
62
63   // for generating sine wave output
64   ui_nco<float,float>   d_nco;
65   double                d_amplitude;
66
67  public:
68   test_gmac_tx(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg);
69   ~test_gmac_tx();
70   void handle_message(mb_message_sptr msg);
71
72  protected:
73   void open_usrp();
74   void close_usrp();
75   void allocate_channel();
76   void send_packets();
77   void enter_transmitting();
78   void build_and_send_next_frame();
79   void handle_xmit_response(pmt_t invocation_handle);
80   void enter_closing_channel();
81 };
82
83 test_gmac_tx::test_gmac_tx(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg)
84   : mb_mblock(runtime, instance_name, user_arg),
85     d_state(INIT), d_nsamples_to_send((long) 40e6),
86     d_nsamples_xmitted(0),
87     d_nframes_xmitted(0),
88     d_samples_per_frame((long)(126 * 4)),       // full packet
89     d_done_sending(false),
90     d_amplitude(16384)
91
92   
93   define_component("GMAC", "gmac", PMT_NIL);
94   d_tx = define_port("tx0", "gmac-tx", false, mb_port::INTERNAL);
95   d_cs = define_port("cs", "gmac-cs", false, mb_port::INTERNAL);
96
97   connect("self", "tx0", "GMAC", "tx0");
98   connect("self", "cs", "GMAC", "cs");
99
100   // initialize NCO
101   double freq = 100e3;
102   int interp = 32;                          // 32 -> 4MS/s
103   double sample_rate = 128e6 / interp;  
104   d_nco.set_freq(2*M_PI * freq/sample_rate);
105
106 }
107
108 test_gmac_tx::~test_gmac_tx()
109 {
110 }
111
112 void
113 test_gmac_tx::handle_message(mb_message_sptr msg)
114 {
115   pmt_t event = msg->signal();
116   pmt_t data = msg->data();
117   pmt_t port_id = msg->port_id();
118
119   pmt_t handle = PMT_F;
120   pmt_t status = PMT_F;
121   pmt_t dict = PMT_NIL;
122   std::string error_msg;
123
124   // Dispatch based on state
125   switch(d_state) {
126     
127     //------------------------------ INIT ---------------------------------//
128     // When GMAC is done initializing, it will send a response
129     case INIT:
130       
131       if(pmt_eq(event, s_response_gmac_initialized)) {
132         handle = pmt_nth(0, data);
133         status = pmt_nth(1, data);
134
135         if(pmt_eq(status, PMT_T)) {
136           enter_transmitting();
137           return;
138         }
139         else {
140           error_msg = "error initializing gmac:";
141           goto bail;
142         }
143       }
144       goto unhandled;
145
146     //-------------------------- TRANSMITTING ----------------------------//
147     // In the transmit state we count the number of underruns received and
148     // ballpark the number with an expected count (something >1 for starters)
149     case TRANSMITTING:
150       
151       // Check that the transmits are OK
152       if (pmt_eq(event, s_response_tx_pkt)){
153         handle = pmt_nth(0, data);
154         status = pmt_nth(1, data);
155
156         if (pmt_eq(status, PMT_T)){
157           handle_xmit_response(handle);
158           return;
159         }
160         else {
161           error_msg = "bad response-tx-pkt:";
162           goto bail;
163         }
164       }
165
166       goto unhandled;
167
168   }
169
170  // An error occured, print it, and shutdown all m-blocks
171  bail:
172   std::cerr << error_msg << data
173             << "status = " << status << std::endl;
174   shutdown_all(PMT_F);
175   return;
176
177  // Received an unhandled message for a specific state
178  unhandled:
179   if(verbose && !pmt_eq(event, pmt_intern("%shutdown")))
180     std::cout << "[TEST_GMAC_TX] unhandled msg: " << msg
181               << "in state "<< d_state << std::endl;
182 }
183
184 void
185 test_gmac_tx::enter_transmitting()
186 {
187   d_state = TRANSMITTING;
188   d_nsamples_xmitted = 0;
189
190   d_cs->send(s_cmd_carrier_sense_deadline,
191              pmt_list2(PMT_NIL,
192                        pmt_from_long(50000000)));
193
194   build_and_send_next_frame();  // fire off 4 to start pipeline
195   build_and_send_next_frame();
196   build_and_send_next_frame();
197   build_and_send_next_frame();
198 }
199
200 void
201 test_gmac_tx::build_and_send_next_frame()
202 {
203   // allocate the uniform vector for the samples
204   // FIXME perhaps hold on to this between calls
205
206 #if 0
207   long nsamples_this_frame =
208     std::min(d_nsamples_to_send - d_nsamples_xmitted,
209              d_samples_per_frame);
210 #else
211   long nsamples_this_frame = d_samples_per_frame;
212 #endif
213
214   if (nsamples_this_frame == 0){
215     d_done_sending = true;
216     return;
217   }
218     
219
220   size_t nshorts = 2 * nsamples_this_frame;     // 16-bit I & Q
221   pmt_t uvec = pmt_make_s16vector(nshorts, 0);
222   size_t ignore;
223   int16_t *samples = pmt_s16vector_writeable_elements(uvec, ignore);
224
225   // fill in the complex sinusoid
226   for (int i = 0; i < nsamples_this_frame; i++){
227
228     if (1){
229       gr_complex s;
230       d_nco.sincos(&s, 1, d_amplitude);
231       // write 16-bit i & q
232       samples[2*i] =   (int16_t) s.real();
233       samples[2*i+1] = (int16_t) s.imag();
234     }
235     else {
236       gr_complex s(d_amplitude, d_amplitude);
237
238       // write 16-bit i & q
239       samples[2*i] =   (int16_t) s.real();
240       samples[2*i+1] = (int16_t) s.imag();
241     }
242   }
243     
244   // Per packet properties
245   pmt_t tx_properties = pmt_make_dict();
246
247   if(d_nframes_xmitted > 25000) {
248     pmt_dict_set(tx_properties,
249                  pmt_intern("carrier-sense"),
250                  PMT_F);
251   }
252
253   if(d_nframes_xmitted > 35000) {
254     pmt_dict_set(tx_properties,
255                  pmt_intern("carrier-sense"),
256                  PMT_NIL);
257   }
258
259   if(d_nframes_xmitted == 45000) {
260     d_cs->send(s_cmd_carrier_sense_threshold, 
261                pmt_list2(PMT_NIL,
262                          pmt_from_long(100)));
263   }
264   
265   if(d_nframes_xmitted == 60000) {
266     d_cs->send(s_cmd_carrier_sense_threshold, 
267                pmt_list2(PMT_NIL,
268                          pmt_from_long(25)));
269   }
270   
271   if(d_nframes_xmitted == 75000) {
272     d_cs->send(s_cmd_carrier_sense_disable, 
273                pmt_list1(PMT_NIL));
274   }
275   
276   if(d_nframes_xmitted > 90000 && d_nframes_xmitted < 110000) {
277     pmt_dict_set(tx_properties,
278                  pmt_intern("carrier-sense"),
279                  PMT_T);
280   }
281   
282   if(d_nframes_xmitted > 110000) {
283
284     if(d_nframes_xmitted % 100 == 0)
285       pmt_dict_set(tx_properties,
286                    pmt_intern("carrier-sense"),
287                    PMT_T);
288 }
289
290   pmt_t timestamp = pmt_from_long(0xffffffff);  // NOW
291   d_tx->send(s_cmd_tx_pkt,
292              pmt_list4(PMT_NIL,   // invocation-handle
293            PMT_NIL,         // destination
294                        uvec,                                // the samples
295            tx_properties)); // per pkt properties
296
297   d_nsamples_xmitted += nsamples_this_frame;
298   d_nframes_xmitted++;
299
300   if(verbose && 0)
301     std::cout << "[TEST_GMAC_TX] Transmitted frame\n";
302 }
303
304
305 void
306 test_gmac_tx::handle_xmit_response(pmt_t handle)
307 {
308   if (d_done_sending &&
309       pmt_to_long(handle) == (d_nframes_xmitted - 1)){
310     // We're done sending and have received all responses
311   }
312
313   build_and_send_next_frame();
314 }
315
316 REGISTER_MBLOCK_CLASS(test_gmac_tx);
317
318
319 // ----------------------------------------------------------------
320
321 int
322 main (int argc, char **argv)
323 {
324   // handle any command line args here
325
326   mb_runtime_sptr rt = mb_make_runtime();
327   pmt_t result = PMT_NIL;
328
329   rt->run("test_gmac_tx", "test_gmac_tx", PMT_F, &result);
330 }