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