7f9b7a563ef12c832c9d47c3068ed895dbeef33e
[debian/gnuradio] / usrp2 / firmware / apps / bitrot / tx_drop2.c
1 /*
2  * Copyright 2007,2008 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.h"
34 #include "print_rmon_regs.h"
35 #include <stddef.h>
36 #include <stdlib.h>
37 #include <string.h>
38
39
40 /*
41  * Like tx_only.c, but we discard data packets instead of sending them to the
42  * DSP TX pipeline.
43  */
44
45 int total_rx_pkts = 0;
46 int total_rx_bytes = 0;
47
48
49 static int timer_delta = MASTER_CLK_RATE/1000;  // tick at 1kHz
50
51 /*
52  * This program can respond to queries from the host
53  * and stream rx samples.
54  *
55  * Buffer 1 is used by the cpu to send frames to the host.
56  * Buffers 2 and 3 are used to double-buffer the DSP Rx to eth flow
57  * Buffers 4 and 5 are used to double-buffer the eth to DSP Tx  eth flow
58  */
59 //#define CPU_RX_BUF    0       // eth -> cpu
60 //#define CPU_TX_BUF    1       // cpu -> eth
61
62 #define DSP_RX_BUF_0    2       // dsp rx -> eth (double buffer)
63 #define DSP_RX_BUF_1    3       // dsp rx -> eth
64 #define DSP_TX_BUF_0    4       // eth -> dsp tx (double buffer)
65 #define DSP_TX_BUF_1    5       // eth -> dsp tx
66
67
68 /*
69  * ================================================================
70  *      configure DSP RX double buffering state machine
71  * ================================================================
72  */
73
74 // 4 lines of ethernet hdr + 1 line (word0)
75 // DSP Rx writes timestamp followed by nlines_per_frame of samples
76 #define DSP_RX_FIRST_LINE                 5
77 #define DSP_RX_SAMPLES_PER_FRAME        128
78 #define DSP_RX_EXTRA_LINES                1     // writes timestamp
79
80 // Receive from DSP Rx
81 buf_cmd_args_t dsp_rx_recv_args = {
82   PORT_DSP,
83   DSP_RX_FIRST_LINE,
84   BP_LAST_LINE
85 };
86
87 // send to ethernet
88 buf_cmd_args_t dsp_rx_send_args = {
89   PORT_ETH,
90   0,            // starts with ethernet header in line 0
91   0,            // filled in from last_line register
92 };
93
94 dbsm_t dsp_rx_sm;       // the state machine
95
96 /*
97  * ================================================================
98  *      configure DSP TX double buffering state machine
99  * ================================================================
100  */
101
102 // 4 lines of ethernet hdr + 2 lines (word0 + timestamp)
103 // DSP Tx reads word0 (flags) + timestamp followed by samples
104
105 #define DSP_TX_FIRST_LINE                 4
106 #define DSP_TX_SAMPLES_PER_FRAME        250     // not used except w/ debugging
107 #define DSP_TX_EXTRA_LINES                2     // reads word0 + timestamp
108
109 // Receive from ethernet
110 buf_cmd_args_t dsp_tx_recv_args = {
111   PORT_ETH,
112   0,
113   BP_LAST_LINE
114 };
115
116 // send to DSP Tx
117 buf_cmd_args_t dsp_tx_send_args = {
118   PORT_DSP,
119   DSP_TX_FIRST_LINE,    // starts just past ethernet header
120   0                     // filled in from last_line register
121 };
122
123 dbsm_t dsp_tx_sm;       // the state machine
124
125
126 // ----------------------------------------------------------------
127
128
129 // The mac address of the host we're sending to.
130 u2_mac_addr_t host_mac_addr;
131
132
133 void
134 timer_irq_handler(unsigned irq)
135 {
136   hal_set_timeout(timer_delta); // schedule next timeout
137 }
138
139 // Tx DSP underrun
140 void
141 underrun_irq_handler(unsigned irq)
142 {
143   putchar('U');
144
145   dbsm_stop(&dsp_tx_sm);
146   dsp_tx_regs->clear_state = 1;
147   dbsm_start(&dsp_tx_sm);  // restart sm so we're listening to ethernet again
148
149   // putstr("\nirq: underrun\n");
150 }
151
152
153 void
154 start_rx_cmd(const u2_mac_addr_t *host, op_start_rx_t *p)
155 {
156 }
157
158 void
159 stop_rx_cmd(void)
160 {
161 }
162
163 static void
164 setup_tx()
165 {
166   dsp_tx_regs->clear_state = 1;
167   bp_clear_buf(DSP_TX_BUF_0);
168   bp_clear_buf(DSP_TX_BUF_1);
169
170 #if 1
171   int tx_scale = 256;
172   int interp = 32;
173
174   op_config_tx_t def_config;
175   memset(&def_config, 0, sizeof(def_config));
176   def_config.phase_inc  = 408021893;                    // 9.5 MHz [2**32 * fc/fsample]
177   def_config.scale_iq = (tx_scale << 16) | tx_scale;
178   def_config.interp = interp;
179
180   // setup Tx DSP regs
181   config_tx_cmd(&def_config);
182 #endif
183 }
184
185
186 inline static void
187 buffer_irq_handler(unsigned irq)
188 {
189   uint32_t  status = buffer_pool_status->status;
190
191   if (status & BPS_ERROR_ALL){
192     // FIXME rare path, handle error conditions
193     putstr("Errors! status = ");
194     puthex32_nl(status);
195
196     printf("total_rx_pkts  = %d\n", total_rx_pkts);
197     printf("total_rx_bytes = %d\n", total_rx_bytes);
198
199     print_rmon_regs();
200
201     if (status & (BPS_ERROR(DSP_TX_BUF_0) | BPS_ERROR(DSP_TX_BUF_1))){
202       dbsm_stop(&dsp_tx_sm);            
203       dsp_tx_regs->clear_state = 1; // try to restart
204       dbsm_start(&dsp_tx_sm);
205       return;
206     }
207   }
208
209   dbsm_process_status(&dsp_tx_sm, status);
210
211   if (status & BPS_DONE(CPU_TX_BUF)){
212     bp_clear_buf(CPU_TX_BUF);
213   }
214 }
215
216 /*
217  * Called when an ethernet packet is received.
218  * Return true if we handled it here (always!)
219  */
220 bool
221 nop_eth_pkt_inspector(dbsm_t *sm, int bufno)
222 {
223   hal_toggle_leds(0x1);
224
225   u2_eth_packet_t *pkt = (u2_eth_packet_t *) buffer_ram(bufno);
226   size_t byte_len = (buffer_pool_status->last_line[bufno] - 1) * 4;
227
228   total_rx_pkts++;
229   total_rx_bytes += byte_len;
230
231   // inspect rcvd frame and figure out what do do.
232
233   if (pkt->ehdr.ethertype != U2_ETHERTYPE)
234     return true;        // ignore, probably bogus PAUSE frame from MAC
235
236   int chan = u2p_chan(&pkt->fixed);
237
238   switch (chan){
239   case CONTROL_CHAN:
240     handle_control_chan_frame(pkt, byte_len);
241     return true;        // we handled the packet
242     break;
243
244   case 0:
245   default:
246     return true;        // say we handled it
247     break;
248   }
249 }
250
251
252 int
253 main(void)
254 {
255   u2_init();
256
257   // setup tx gpio bits for GPIOM_FPGA_1 -- fpga debug output
258   hal_gpio_set_tx_mode(15, 0, GPIOM_FPGA_1);
259   hal_gpio_set_rx_mode(15, 0, GPIOM_FPGA_1);    // no printing...
260
261   putstr("\ntx_drop2\n");
262   
263   // Control LEDs
264   hal_set_leds(0x0, 0x3);
265
266   // pic_register_handler(IRQ_OVERRUN,  overrun_irq_handler);
267   pic_register_handler(IRQ_UNDERRUN, underrun_irq_handler);
268
269   //pic_register_handler(IRQ_TIMER, timer_irq_handler);
270   //hal_set_timeout(timer_delta);
271
272   ethernet_register_link_changed_callback(link_changed_callback);
273
274   ethernet_init();
275
276   // initialize double buffering state machine for ethernet -> DSP Tx
277
278   dbsm_init(&dsp_tx_sm, DSP_TX_BUF_0,
279             &dsp_tx_recv_args, &dsp_tx_send_args,
280             nop_eth_pkt_inspector);
281
282   // program tx registers
283   setup_tx();
284
285   // kick off the state machine
286   dbsm_start(&dsp_tx_sm);
287
288   while(1){
289     buffer_irq_handler(0);
290   }
291 }
292