Merge commit 'v3.3.0' into upstream
[debian/gnuradio] / usrp2 / firmware / apps / bitrot / tx_drop.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 TX double buffering state machine
71  * ================================================================
72  */
73
74 // 4 lines of ethernet hdr + 2 lines (word0 + timestamp)
75 // DSP Tx reads word0 (flags) + timestamp followed by samples
76
77 #define DSP_TX_FIRST_LINE                 4
78 #define DSP_TX_SAMPLES_PER_FRAME        250     // not used except w/ debugging
79 #define DSP_TX_EXTRA_LINES                2     // reads word0 + timestamp
80
81 // Receive from ethernet
82 buf_cmd_args_t dsp_tx_recv_args = {
83   PORT_ETH,
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 ethernet header
92   0                     // filled in from last_line register
93 };
94
95 dbsm_t dsp_tx_sm;       // the state machine
96
97
98 // ----------------------------------------------------------------
99
100
101 // The mac address of the host we're sending to.
102 u2_mac_addr_t host_mac_addr;
103
104
105 void
106 timer_irq_handler(unsigned irq)
107 {
108   hal_set_timeout(timer_delta); // schedule next timeout
109 }
110
111 // Tx DSP underrun
112 void
113 underrun_irq_handler(unsigned irq)
114 {
115   putchar('U');
116
117   dbsm_stop(&dsp_tx_sm);
118   dsp_tx_regs->clear_state = 1;
119   dbsm_start(&dsp_tx_sm);  // restart sm so we're listening to ethernet again
120
121   // putstr("\nirq: underrun\n");
122 }
123
124
125 void
126 start_rx_cmd(const u2_mac_addr_t *host, op_start_rx_t *p)
127 {
128 }
129
130 void
131 stop_rx_cmd(void)
132 {
133 }
134
135 static void
136 setup_tx()
137 {
138   dsp_tx_regs->clear_state = 1;
139   bp_clear_buf(DSP_TX_BUF_0);
140   bp_clear_buf(DSP_TX_BUF_1);
141
142   int tx_scale = 256;
143   int interp = 32;
144
145   op_config_tx_t def_config;
146   memset(&def_config, 0, sizeof(def_config));
147   def_config.phase_inc  = 408021893;                    // 9.5 MHz [2**32 * fc/fsample]
148   def_config.scale_iq = (tx_scale << 16) | tx_scale;
149   def_config.interp = interp;
150
151   // setup Tx DSP regs
152   config_tx_cmd(&def_config);
153 }
154
155
156 inline static void
157 buffer_irq_handler(unsigned irq)
158 {
159   uint32_t  status = buffer_pool_status->status;
160
161   if (status & BPS_ERROR_ALL){
162     // FIXME rare path, handle error conditions
163     putstr("Errors! status = ");
164     puthex32_nl(status);
165
166     printf("total_rx_pkts  = %d\n", total_rx_pkts);
167     printf("total_rx_bytes = %d\n", total_rx_bytes);
168
169     print_rmon_regs();
170
171     if (status & (BPS_ERROR(DSP_TX_BUF_0) | BPS_ERROR(DSP_TX_BUF_1))){
172       dbsm_stop(&dsp_tx_sm);            
173       dsp_tx_regs->clear_state = 1; // try to restart
174       dbsm_start(&dsp_tx_sm);
175       return;
176     }
177   }
178
179   dbsm_process_status(&dsp_tx_sm, status);
180
181   if (status & BPS_DONE(CPU_TX_BUF)){
182     bp_clear_buf(CPU_TX_BUF);
183   }
184 }
185
186
187 /*
188  * Called when an ethernet packet is received.
189  *
190  * Claim that we handled all the packets,
191  * dropping those destined for the TX DSP chain
192  * on the ground.
193  */
194 bool
195 nop_eth_pkt_inspector(dbsm_t *sm, int bufno)
196 {
197   hal_toggle_leds(0x1);
198
199   u2_eth_packet_t *pkt = (u2_eth_packet_t *) buffer_ram(bufno);
200   size_t byte_len = (buffer_pool_status->last_line[bufno] - 1) * 4;
201
202   total_rx_pkts++;
203   total_rx_bytes += byte_len;
204
205   // inspect rcvd frame and figure out what do do.
206
207   if (pkt->ehdr.ethertype != U2_ETHERTYPE)
208     return true;        // ignore, probably bogus PAUSE frame from MAC
209
210   int chan = u2p_chan(&pkt->fixed);
211
212   switch (chan){
213   case CONTROL_CHAN:
214     handle_control_chan_frame(pkt, byte_len);
215     return true;        // we handled the packet
216     break;
217
218   case 0:
219   default:
220     return true;        // We handled the data by dropping it :)
221     break;
222   }
223 }
224
225
226 int
227 main(void)
228 {
229   u2_init();
230
231   // setup tx gpio bits for GPIOM_FPGA_1 -- fpga debug output
232   hal_gpio_set_tx_mode(15, 0, GPIOM_FPGA_1);
233   hal_gpio_set_rx_mode(15, 0, GPIOM_FPGA_1);
234
235   putstr("\ntx_drop\n");
236   
237   // Control LEDs
238   hal_set_leds(0x0, 0x3);
239
240   pic_register_handler(IRQ_UNDERRUN, underrun_irq_handler);
241
242   ethernet_register_link_changed_callback(link_changed_callback);
243   ethernet_init();
244
245   // initialize double buffering state machine for ethernet -> DSP Tx
246
247   dbsm_init(&dsp_tx_sm, DSP_TX_BUF_0,
248             &dsp_tx_recv_args, &dsp_tx_send_args,
249             nop_eth_pkt_inspector);
250
251   // program tx registers
252   setup_tx();
253
254   // kick off the state machine
255   dbsm_start(&dsp_tx_sm);
256
257   while(1){
258     buffer_irq_handler(0);
259   }
260 }
261