Update incorrectly checked in Makefile.am
[debian/gnuradio] / usrp2 / firmware / apps / mimo_tx.c
1 /*
2  * Copyright 2007,2008,2009 Free Software Foundation, Inc.
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
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.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 /*
19  * This is a down and dirty test program that confirms that the we can
20  * coherently transmit different signals to two USRP2s connected via a
21  * mimo cable.  This code runs in the USRP2 connected to the ethernet.
22  * The other USRP runs mimo_tx_slave.  The host runs test_mimo_tx.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include "u2_init.h"
30 #include "memory_map.h"
31 #include "spi.h"
32 #include "hal_io.h"
33 #include "buffer_pool.h"
34 #include "pic.h"
35 #include "bool.h"
36 #include "ethernet.h"
37 #include "nonstdio.h"
38 #include "usrp2_eth_packet.h"
39 #include "bsm12.h"
40 #include "mimo_app_common_v2.h"
41 #include "memcpy_wa.h"
42 #include <stddef.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include "clocks.h"
46
47 #define FW_SETS_SEQNO   1       // define to 0 or 1 (FIXME must be 1 for now)
48
49 #if (FW_SETS_SEQNO)
50 static int fw_seqno;    // used when f/w is filling in sequence numbers
51 #endif
52
53
54 /*
55  * Experimental code to transmit packets to DSP Tx and SERDES
56  *
57  * Hard wire the Tx config so we don't have to deal with control stuff yet.
58  */
59
60 #define BUF_BSM12_0     4
61 #define BUF_BSM12_1     5
62 #define BUF_BSM12_2     6
63
64 //#define CPU_TX_BUF    7       // cpu -> eth
65
66 // 4 lines of ethernet hdr + 1 line transport hdr + 2 lines (word0 + timestamp)
67 // DSP Tx reads word0 (flags) + timestamp followed by samples
68
69 #define DSP_TX_FIRST_LINE ((sizeof(u2_eth_hdr_t) + sizeof(u2_transport_hdr_t))/4)
70
71 // Receive from ethernet
72 buf_cmd_args_t bsm12_recv_args = {
73   PORT_ETH,
74   0,
75   BP_LAST_LINE
76 };
77
78 // send to DSP Tx
79 buf_cmd_args_t bsm12_send0_args = {
80   PORT_DSP,
81   DSP_TX_FIRST_LINE,    // starts just past transport header
82   0                     // filled in from last_line register
83 };
84
85 // send to SERDES
86 buf_cmd_args_t bsm12_send1_args = {
87   PORT_SERDES,
88   0,                    // starts just past transport header
89   0                     // filled in from last_line register
90 };
91
92 bsm12_t bsm12_sm;       // the state machine
93
94 #if 0
95 /*
96  * ================================================================
97  *   configure DSP RX double buffering state machine (dsp -> eth)
98  * ================================================================
99  */
100
101 // 4 lines of ethernet hdr + 1 line transport hdr + 1 line (word0)
102 // DSP Rx writes timestamp followed by nlines_per_frame of samples
103 #define DSP_RX_FIRST_LINE ((sizeof(u2_eth_hdr_t) + sizeof(u2_transport_hdr_t))/4 + 1)
104
105 // receive from DSP
106 buf_cmd_args_t dsp_rx_recv_args = {
107   PORT_DSP,
108   DSP_RX_FIRST_LINE,
109   BP_LAST_LINE
110 };
111
112 // send to ETH
113 buf_cmd_args_t dsp_rx_send_args = {
114   PORT_ETH,
115   0,            // starts with ethernet header in line 0
116   0,            // filled in from list_line register
117 };
118
119 dbsm_t dsp_rx_sm;       // the state machine
120 #endif
121
122
123 // The mac address of the host we're sending to.
124 u2_mac_addr_t host_mac_addr;
125
126
127 // variables for streaming mode
128
129 static bool         streaming_p = false;
130 static unsigned int streaming_items_per_frame = 0;
131 static int          streaming_frame_count = 0;
132 #define FRAMES_PER_CMD  1000
133
134 bool is_streaming(void){ return streaming_p; }
135
136
137 // ----------------------------------------------------------------
138
139
140 void
141 restart_streaming(void)
142 {
143 #if 0
144   // setup RX DSP regs
145   dsp_rx_regs->clear_state = 1;                 // reset
146
147   streaming_p = true;
148   streaming_frame_count = FRAMES_PER_CMD;
149
150   dsp_rx_regs->rx_command =
151     MK_RX_CMD(FRAMES_PER_CMD * streaming_items_per_frame,
152               streaming_items_per_frame,
153               1, 1);                    // set "chain" bit
154
155   // kick off the state machine
156   dbsm_start(&dsp_rx_sm);
157
158   dsp_rx_regs->rx_time = 0;             // enqueue first of two commands
159
160   // make sure this one and the rest have the "now" and "chain" bits set.
161   dsp_rx_regs->rx_command =
162     MK_RX_CMD(FRAMES_PER_CMD * streaming_items_per_frame,
163               streaming_items_per_frame,
164               1, 1);                            
165
166   dsp_rx_regs->rx_time = 0;             // enqueue second command
167 #endif
168 }
169
170 void
171 start_rx_streaming_cmd(const u2_mac_addr_t *host, op_start_rx_streaming_t *p)
172 {
173 #if 0
174   host_mac_addr = *host;        // remember who we're sending to
175
176   /*
177    * Construct  ethernet header and word0 and preload into two buffers
178    */
179   u2_eth_packet_t       pkt;
180   memset(&pkt, 0, sizeof(pkt));
181   pkt.ehdr.dst = *host;
182   pkt.ehdr.src = *ethernet_mac_addr();
183   pkt.ehdr.ethertype = U2_ETHERTYPE;
184   u2p_set_word0(&pkt.fixed, 0, 0);
185   // DSP RX will fill in timestamp
186
187   memcpy_wa(buffer_ram(DSP_RX_BUF_0), &pkt, sizeof(pkt));
188   memcpy_wa(buffer_ram(DSP_RX_BUF_1), &pkt, sizeof(pkt));
189
190
191   if (FW_SETS_SEQNO)
192     fw_seqno = 0;
193
194   streaming_items_per_frame = p->items_per_frame;
195   restart_streaming();
196 #endif
197 }
198
199
200 void
201 stop_rx_cmd(void)
202 {
203 #if 0
204   streaming_p = false;
205   dsp_rx_regs->clear_state = 1; // flush cmd queue
206   bp_clear_buf(DSP_RX_BUF_0);
207   bp_clear_buf(DSP_RX_BUF_1);
208 #endif
209 }
210
211
212 static void
213 setup_tx()
214 {
215   dsp_tx_regs->clear_state = 1;
216
217   int tx_scale = 2500;
218   int interp = 8;               // * 4
219
220   // setup some defaults
221
222   dsp_tx_regs->freq = 429496730;        // 10MHz
223   dsp_tx_regs->scale_iq = (tx_scale << 16) | tx_scale;
224   dsp_tx_regs->interp_rate = (1 << 9) | (1 << 8) | interp;
225 }
226
227
228 #if 0
229 #if (FW_SETS_SEQNO)
230 /*
231  * Debugging ONLY.  This will be handled by the tx_protocol_engine.
232  *
233  * This is called when the DSP Rx chain has filled in a packet.
234  * We set and increment the seqno, then return false, indicating
235  * that we didn't handle the packet.  A bit of a kludge
236  * but it should work.
237  */
238 int
239 fw_sets_seqno_inspector(bsm12_t *sm, int buf_this)
240 {
241   uint32_t *p = buffer_ram(buf_this);
242   uint32_t seqno = fw_seqno++;
243
244   // KLUDGE all kinds of nasty magic numbers and embedded knowledge
245   uint32_t t = p[4];
246   t = (t & 0xffff00ff) | ((seqno & 0xff) << 8);
247   p[4] = t;
248
249   // queue up another rx command when required
250   if (streaming_p && --streaming_frame_count == 0){
251     streaming_frame_count = FRAMES_PER_CMD;
252     dsp_rx_regs->rx_time = 0;
253   }
254
255   return false;         // we didn't handle the packet
256 }
257 #endif
258 #endif
259
260
261 inline static void
262 buffer_irq_handler(unsigned irq)
263 {
264   uint32_t  status = buffer_pool_status->status;
265
266   bsm12_process_status(&bsm12_sm, status);
267 }
268
269 int
270 main(void)
271 {
272   u2_init();
273
274   putstr("\nMIMO Tx\n");
275   print_mac_addr(ethernet_mac_addr()->addr);
276   newline();
277
278   ethernet_register_link_changed_callback(link_changed_callback);
279   ethernet_init();
280
281   clocks_mimo_config(MC_WE_DONT_LOCK | MC_PROVIDE_CLK_TO_MIMO);
282
283 #if 0
284   // make bit 15 of Tx gpio's be a s/w output
285   hal_gpio_set_sel(GPIO_TX_BANK, 15, 's');
286   hal_gpio_set_ddr(GPIO_TX_BANK, 0x8000, 0x8000);
287 #endif
288
289   output_regs->debug_mux_ctrl = 1;
290 #if 0
291   hal_gpio_set_sels(GPIO_TX_BANK, "1111111111111111");
292   hal_gpio_set_sels(GPIO_RX_BANK, "1111111111111111");
293   hal_gpio_set_ddr(GPIO_TX_BANK, 0xffff, 0xffff);
294   hal_gpio_set_ddr(GPIO_RX_BANK, 0xffff, 0xffff);
295 #endif
296
297
298   // initialize double buffering state machine for ethernet -> DSP Tx
299
300   bsm12_init(&bsm12_sm, BUF_BSM12_0,
301              &bsm12_recv_args, &bsm12_send0_args, &bsm12_send1_args,
302              eth_pkt_inspector);
303
304
305 #if 0
306   // initialize double buffering state machine for DSP RX -> Ethernet
307
308   if (FW_SETS_SEQNO){
309     dbsm_init(&dsp_rx_sm, DSP_RX_BUF_0,
310               &dsp_rx_recv_args, &dsp_rx_send_args,
311               fw_sets_seqno_inspector);
312   }
313   else {
314     dbsm_init(&dsp_rx_sm, DSP_RX_BUF_0,
315               &dsp_rx_recv_args, &dsp_rx_send_args,
316               dbsm_nop_inspector);
317   }
318
319   // tell app_common that this dbsm could be sending to the ethernet
320   ac_could_be_sending_to_eth = &dsp_rx_sm;
321 #endif
322
323
324   // program tx registers
325   setup_tx();
326
327   // kick off the state machine
328   bsm12_start(&bsm12_sm);
329
330   //int which = 0;
331
332   while(1){
333     // hal_gpio_write(GPIO_TX_BANK, which, 0x8000);
334     // which ^= 0x8000;
335
336     buffer_irq_handler(0);
337
338     int pending = pic_regs->pending;            // poll for under or overrun
339
340     if (pending & PIC_UNDERRUN_INT){
341       // dbsm_handle_tx_underrun(&dsp_tx_sm);
342       pic_regs->pending = PIC_UNDERRUN_INT;     // clear interrupt
343       putchar('U');
344     }
345
346     if (pending & PIC_OVERRUN_INT){
347       // dbsm_handle_rx_overrun(&dsp_rx_sm);
348       pic_regs->pending = PIC_OVERRUN_INT;      // clear pending interrupt
349
350       // FIXME Figure out how to handle this robustly.
351       // Any buffers that are emptying should be allowed to drain...
352
353       if (streaming_p){
354         // restart_streaming();
355         // FIXME report error
356       }
357       else {
358         // FIXME report error
359       }
360       putchar('O');
361     }
362   }
363 }