Update incorrectly checked in Makefile.am
[debian/gnuradio] / usrp2 / firmware / apps / gen_pause_frames.c
1 /*
2  * Copyright 2007 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 #include "u2_init.h"
19 #include "memory_map.h"
20 #include "spi.h"
21 #include "hal_io.h"
22 #include "buffer_pool.h"
23 #include "pic.h"
24 #include "bool.h"
25 #include "ethernet.h"
26 #include "nonstdio.h"
27 #include "u2_eth_packet.h"
28 #include "memcpy_wa.h"
29 #include <stddef.h>
30 #include <stdlib.h>
31
32
33 // ----------------------------------------------------------------
34
35 unsigned char dst_mac_addr[6] = {
36   0xff, 0xff, 0xff, 0xff, 0xff, 0xff
37 };
38
39 // ----------------------------------------------------------------
40
41 // #define      PACKET_SIZE 1500                // bytes
42 // #define ETH_DATA_RATE 1000000                // 1MB/s
43 // #define      ETH_PACKET_RATE (ETH_DATA_RATE/PACKET_SIZE)     // 13,3333 pkts/s
44
45 // static int timer_delta = MASTER_CLK_RATE/ETH_PACKET_RATE;    // ticks between interrupts
46
47 static int timer_delta = MASTER_CLK_RATE/1000;  // tick at 1kHz
48
49 static volatile bool send_packet_now = false;   // timer handler sets this
50 static volatile bool link_is_up = false;        // eth handler sets this
51
52 int packet_number = 0;
53
54 // ----------------------------------------------------------------
55
56 // debugging output on tx pins
57 #define LS_MASK  0xE0000
58 #define LS_1000  0x80000
59 #define LS_100   0x40000
60 #define LS_10    0x20000
61
62
63 /*
64  * Called when eth phy state changes (w/ interrupts disabled)
65  */
66 void
67 link_changed_callback(int speed)
68 {
69   int v = 0;
70   switch(speed){
71   case 10:
72     v = LS_10;
73     link_is_up = true;
74     break;
75     
76   case 100:
77     v = LS_100;
78     link_is_up = true;
79     break;
80     
81   case 1000:
82     v = LS_100;
83     link_is_up = true;
84     break;
85
86   default:
87     v = 0;
88     link_is_up = false;
89     break;
90   }
91
92   hal_gpio_set_tx(v, LS_MASK);  /* set debug bits on d'board */
93
94   putstr("\neth link changed: speed = ");
95   puthex16_nl(speed);
96 }
97
98 void
99 timer_irq_handler(unsigned irq)
100 {
101   hal_set_timeout(timer_delta); // schedule next timeout
102   send_packet_now = 1;
103 }
104
105
106 void
107 buffer_irq_handler(unsigned irq)
108 {
109   // FIXME
110 }
111
112 static void
113 init_packet(int *buf, const u2_eth_packet_t *pkt, int bufnum)
114 {
115   int i = 0;
116   int mark = ((bufnum & 0xff) << 24) | 0x005A0000;
117
118   for (i = 0; i < BP_NLINES; i++){
119     buf[i] = mark | i;
120     mark ^= 0x00FF0000;
121   }
122
123   // copy header into buffer
124   memcpy_wa(buf, pkt, sizeof(*pkt));
125 }
126
127 static void
128 init_packets(void)
129 {
130   int   i;
131   
132   u2_eth_packet_t       pkt __attribute__((aligned (4)));
133
134   for (i = 0; i < 6; i++){
135     pkt.ehdr.dst.addr[i] = dst_mac_addr[i];
136   }
137   pkt.ehdr.src = *ethernet_mac_addr();
138   pkt.ehdr.ethertype = U2_ETHERTYPE;
139
140   // fill ALL buffers for debugging
141   for (i = 0; i < 8; i++)
142     init_packet((void *)buffer_ram(i), &pkt, i);
143 }
144
145 static int led_counter = 0;
146
147 int
148 main(void)
149 {
150   int send_pause = 1;
151   
152   u2_init();
153
154   // setup tx gpio bits for GPIOM_FPGA_1 -- fpga debug output
155   //hal_gpio_set_sels(GPIO_TX_BANK, "1111111111111111");
156   //hal_gpio_set_sels(GPIO_RX_BANK, "1111111111111111");
157
158   putstr("\ngen_eth_packets\n");
159   
160   // Control LEDs
161   output_regs->leds = 0x00;
162
163   init_packets();
164
165   // pic_register_handler(IRQ_BUFFER, buffer_irq_handler);  // poll for now
166   pic_register_handler(IRQ_TIMER, timer_irq_handler);
167   hal_set_timeout(timer_delta);
168
169   ethernet_register_link_changed_callback(link_changed_callback);
170
171   ethernet_init();
172
173   eth_mac->pause_frame_send_en = 1;
174   eth_mac->pause_quanta_set = 16384 / 512;
175
176   // eth_mac->speed = 4;        // FIXME hardcode mac speed to 1000
177
178   while(1){
179     if (link_is_up && send_packet_now){
180       send_packet_now = false;
181
182
183       if (send_pause)
184         eth_mac->xon_cpu = 1;
185       else
186         eth_mac->xon_cpu = 0;
187
188       send_pause ^= 1;
189
190       // kick off the next packet
191       // FIXME set packet number in packet
192
193 #if 0
194       bp_send_from_buf(0, PORT_ETH, 1, 0, 255); // 1KB total
195
196       while ((buffer_pool_status->status & (BPS_DONE_0|BPS_ERROR_0)) == 0)
197         ;
198       bp_clear_buf(0);
199 #endif
200
201       output_regs->leds = ((++led_counter) & 0x1) | (link_is_up ? 0x2 : 0x0);
202     }
203   }
204
205   hal_finish();
206   return 1;
207 }