3 * Copyright 2007 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
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)
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.
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.
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>
41 #include <gmac_symbols.h>
43 static bool verbose = true;
45 class test_gmac_tx : public mb_mblock
49 pmt_t d_tx_chan; // returned tx channel handle
57 long d_nsamples_to_send;
58 long d_nsamples_xmitted;
59 long d_nframes_xmitted;
60 long d_samples_per_frame;
63 // for generating sine wave output
64 ui_nco<float,float> d_nco;
68 test_gmac_tx(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg);
70 void handle_message(mb_message_sptr msg);
75 void allocate_channel();
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();
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),
88 d_samples_per_frame((long)(126 * 4)), // full packet
89 d_done_sending(false),
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);
97 connect("self", "tx0", "GMAC", "tx0");
98 connect("self", "cs", "GMAC", "cs");
102 int interp = 32; // 32 -> 4MS/s
103 double sample_rate = 128e6 / interp;
104 d_nco.set_freq(2*M_PI * freq/sample_rate);
108 test_gmac_tx::~test_gmac_tx()
113 test_gmac_tx::handle_message(mb_message_sptr msg)
115 pmt_t event = msg->signal();
116 pmt_t data = msg->data();
117 pmt_t port_id = msg->port_id();
119 pmt_t handle = PMT_F;
120 pmt_t status = PMT_F;
121 pmt_t dict = PMT_NIL;
122 std::string error_msg;
124 // Dispatch based on state
127 //------------------------------ INIT ---------------------------------//
128 // When GMAC is done initializing, it will send a response
131 if(pmt_eq(event, s_response_gmac_initialized)) {
132 handle = pmt_nth(0, data);
133 status = pmt_nth(1, data);
135 if(pmt_eq(status, PMT_T)) {
136 enter_transmitting();
140 error_msg = "error initializing gmac:";
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)
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);
156 if (pmt_eq(status, PMT_T)){
157 handle_xmit_response(handle);
161 error_msg = "bad response-tx-pkt:";
170 // An error occured, print it, and shutdown all m-blocks
172 std::cerr << error_msg << data
173 << "status = " << status << std::endl;
177 // Received an unhandled message for a specific state
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;
185 test_gmac_tx::enter_transmitting()
187 d_state = TRANSMITTING;
188 d_nsamples_xmitted = 0;
190 d_cs->send(s_cmd_carrier_sense_deadline,
192 pmt_from_long(50000000)));
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();
201 test_gmac_tx::build_and_send_next_frame()
203 // allocate the uniform vector for the samples
204 // FIXME perhaps hold on to this between calls
207 long nsamples_this_frame =
208 std::min(d_nsamples_to_send - d_nsamples_xmitted,
209 d_samples_per_frame);
211 long nsamples_this_frame = d_samples_per_frame;
214 if (nsamples_this_frame == 0){
215 d_done_sending = true;
220 size_t nshorts = 2 * nsamples_this_frame; // 16-bit I & Q
221 pmt_t uvec = pmt_make_s16vector(nshorts, 0);
223 int16_t *samples = pmt_s16vector_writeable_elements(uvec, ignore);
225 // fill in the complex sinusoid
226 for (int i = 0; i < nsamples_this_frame; i++){
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();
236 gr_complex s(d_amplitude, d_amplitude);
238 // write 16-bit i & q
239 samples[2*i] = (int16_t) s.real();
240 samples[2*i+1] = (int16_t) s.imag();
244 // Per packet properties
245 pmt_t tx_properties = pmt_make_dict();
247 if(d_nframes_xmitted > 25000) {
248 pmt_dict_set(tx_properties,
249 pmt_intern("carrier-sense"),
253 if(d_nframes_xmitted > 35000) {
254 pmt_dict_set(tx_properties,
255 pmt_intern("carrier-sense"),
259 if(d_nframes_xmitted == 45000) {
260 d_cs->send(s_cmd_carrier_sense_threshold,
262 pmt_from_long(100)));
265 if(d_nframes_xmitted == 60000) {
266 d_cs->send(s_cmd_carrier_sense_threshold,
271 if(d_nframes_xmitted == 75000) {
272 d_cs->send(s_cmd_carrier_sense_disable,
276 if(d_nframes_xmitted > 90000 && d_nframes_xmitted < 110000) {
277 pmt_dict_set(tx_properties,
278 pmt_intern("carrier-sense"),
282 if(d_nframes_xmitted > 110000) {
284 if(d_nframes_xmitted % 100 == 0)
285 pmt_dict_set(tx_properties,
286 pmt_intern("carrier-sense"),
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
295 tx_properties)); // per pkt properties
297 d_nsamples_xmitted += nsamples_this_frame;
301 std::cout << "[TEST_GMAC_TX] Transmitted frame\n";
306 test_gmac_tx::handle_xmit_response(pmt_t handle)
308 if (d_done_sending &&
309 pmt_to_long(handle) == (d_nframes_xmitted - 1)){
310 // We're done sending and have received all responses
313 build_and_send_next_frame();
316 REGISTER_MBLOCK_CLASS(test_gmac_tx);
319 // ----------------------------------------------------------------
322 main (int argc, char **argv)
324 // handle any command line args here
326 mb_runtime_sptr rt = mb_make_runtime();
327 pmt_t result = PMT_NIL;
329 rt->run("test_gmac_tx", "test_gmac_tx", PMT_F, &result);