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