Merge commit 'v3.3.0' into upstream
[debian/gnuradio] / usrp2 / firmware / apps / bitrot / tx_drop_rate_limited.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 "eth_mac.h"
36 #include <stddef.h>
37 #include <stdlib.h>
38 #include <string.h>
39
40
41 /*
42  * receive packets from ethernet at a fixed rate and discard them
43  */
44
45 int total_rx_pkts = 0;
46 int total_rx_bytes = 0;
47
48
49 static int timer_delta = (int)(MASTER_CLK_RATE * 10e-6); // 10us / tick
50
51
52 /*
53  * This program can respond to queries from the host
54  * and stream rx samples.
55  *
56  * Buffer 1 is used by the cpu to send frames to the host.
57  * Buffers 2 and 3 are used to double-buffer the DSP Rx to eth flow
58  * Buffers 4 and 5 are used to double-buffer the eth to DSP Tx  eth flow
59  */
60 //#define CPU_RX_BUF    0       // eth -> cpu
61 //#define CPU_TX_BUF    1       // cpu -> eth
62
63 #define DSP_RX_BUF_0    2       // dsp rx -> eth (double buffer)
64 #define DSP_RX_BUF_1    3       // dsp rx -> eth
65 #define DSP_TX_BUF_0    4       // eth -> dsp tx (double buffer)
66 #define DSP_TX_BUF_1    5       // eth -> dsp tx
67
68
69
70 // ----------------------------------------------------------------
71
72
73 // The mac address of the host we're sending to.
74 u2_mac_addr_t host_mac_addr;
75
76
77 static volatile bool receive_packet_now = false;
78
79 void
80 timer_irq_handler(unsigned irq)
81 {
82   hal_set_timeout(timer_delta); // schedule next timeout
83   receive_packet_now = true;
84 }
85
86
87 // Tx DSP underrun
88 void
89 underrun_irq_handler(unsigned irq)
90 {
91   putchar('U');
92 }
93
94
95 void
96 start_rx_cmd(const u2_mac_addr_t *host, op_start_rx_t *p)
97 {
98 }
99
100 void
101 stop_rx_cmd(void)
102 {
103 }
104
105 static void
106 setup_tx()
107 {
108   dsp_tx_regs->clear_state = 1;
109   bp_clear_buf(DSP_TX_BUF_0);
110   bp_clear_buf(DSP_TX_BUF_1);
111
112   int tx_scale = 256;
113   int interp = 32;
114
115   op_config_tx_t def_config;
116   memset(&def_config, 0, sizeof(def_config));
117   def_config.phase_inc  = 408021893;                    // 9.5 MHz [2**32 * fc/fsample]
118   def_config.scale_iq = (tx_scale << 16) | tx_scale;
119   def_config.interp = interp;
120
121   // setup Tx DSP regs
122   config_tx_cmd(&def_config);
123 }
124
125
126 /*
127  * Called when an ethernet packet is received.
128  *
129  * Claim that we handled all the packets,
130  * dropping those destined for the TX DSP chain
131  * on the ground.
132  */
133 bool
134 nop_eth_pkt_inspector(dbsm_t *sm, int bufno)
135 {
136   hal_toggle_leds(0x1);
137
138   u2_eth_packet_t *pkt = (u2_eth_packet_t *) buffer_ram(bufno);
139   size_t byte_len = (buffer_pool_status->last_line[bufno] - 1) * 4;
140
141   total_rx_pkts++;
142   total_rx_bytes += byte_len;
143
144   // inspect rcvd frame and figure out what do do.
145
146   if (pkt->ehdr.ethertype != U2_ETHERTYPE)
147     return true;        // ignore, probably bogus PAUSE frame from MAC
148
149   int chan = u2p_chan(&pkt->fixed);
150
151   switch (chan){
152   case CONTROL_CHAN:
153     handle_control_chan_frame(pkt, byte_len);
154     return true;        // we handled the packet
155     break;
156
157   case 0:
158   default:
159     return true;        // We handled the data by dropping it :)
160     break;
161   }
162 }
163
164
165 inline static void
166 buffer_irq_handler(unsigned irq)
167 {
168   uint32_t  status = buffer_pool_status->status;
169
170   if (status & (BPS_DONE(CPU_TX_BUF) | BPS_ERROR(CPU_TX_BUF)))
171     bp_clear_buf(CPU_TX_BUF);
172
173   if (status & (BPS_DONE(DSP_TX_BUF_0) | BPS_ERROR(DSP_TX_BUF_0))){
174     bp_clear_buf(DSP_TX_BUF_0);
175
176     if (status & BPS_ERROR(DSP_TX_BUF_0)){
177       int crc = eth_mac_read_rmon(0x05);
178       int fifo_full = eth_mac_read_rmon(0x06);
179       int too_short_too_long = eth_mac_read_rmon(0x07);
180       putstr("Errors! status = ");
181       puthex32_nl(status);
182
183       printf("crc_err\t\t= %d\n", crc);
184       printf("fifo_full\t\t= %d\n", fifo_full);
185       printf("too_short_too_long\t= %d\n", too_short_too_long);
186
187       printf("total_rx_pkts  = %d\n", total_rx_pkts);
188       printf("total_rx_bytes = %d\n", total_rx_bytes);
189     }
190     else
191       nop_eth_pkt_inspector(0, DSP_TX_BUF_0);
192   }
193
194   if (receive_packet_now && (status & BPS_IDLE(DSP_TX_BUF_0))){
195     receive_packet_now = false;
196     bp_receive_to_buf(DSP_TX_BUF_0, PORT_ETH, 1, 0, BP_LAST_LINE);
197   }
198 }
199
200
201 int
202 main(void)
203 {
204   u2_init();
205
206   // setup tx gpio bits for GPIOM_FPGA_1 -- fpga debug output
207   hal_gpio_set_tx_mode(15, 0, GPIOM_FPGA_1);
208   hal_gpio_set_rx_mode(15, 0, GPIOM_FPGA_1);
209
210   putstr("\ntx_drop_rate_limited\n");
211   
212   // Control LEDs
213   hal_set_leds(0x0, 0x3);
214
215   pic_register_handler(IRQ_UNDERRUN, underrun_irq_handler);
216
217   pic_register_handler(IRQ_TIMER, timer_irq_handler);
218   hal_set_timeout(timer_delta);
219
220   ethernet_register_link_changed_callback(link_changed_callback);
221   ethernet_init();
222
223   // program tx registers
224   setup_tx();
225
226   // start a receive from ethernet
227   bp_receive_to_buf(DSP_TX_BUF_0, PORT_ETH, 1, 0, BP_LAST_LINE);
228
229   while(1){
230     buffer_irq_handler(0);
231   }
232 }
233