2 * Copyright 2007 Free Software Foundation, Inc.
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.
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.
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/>.
22 #include "memory_map.h"
25 #include "buffer_pool.h"
30 #include "usrp2_eth_packet.h"
31 #include "memcpy_wa.h"
32 #include "print_rmon_regs.h"
38 // ----------------------------------------------------------------
40 static u2_mac_addr_t dst_mac_addr =
41 {{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }};
43 // ----------------------------------------------------------------
45 // #define PACKET_SIZE 1500 // bytes
46 // #define ETH_DATA_RATE 1000000 // 1MB/s
47 // #define ETH_PACKET_RATE (ETH_DATA_RATE/PACKET_SIZE) // 13,3333 pkts/s
49 // static int timer_delta = MASTER_CLK_RATE/ETH_PACKET_RATE; // ticks between interrupts
51 static int timer_delta = (int)(MASTER_CLK_RATE * 1e-3); // tick at 1 kHz
52 static int sim_timer_delta = (int)(MASTER_CLK_RATE * 100e-6); // tick at 10 kHz
54 static volatile bool send_packet_now = false; // timer handler sets this
55 static volatile bool link_is_up = false; // eth handler sets this
57 int packet_number = 0;
60 #define CPU_TX_BUF 0 // cpu xmits ethernet frames from here
61 #define CPU_RX_BUF 1 // receive ethernet frames here
63 // ----------------------------------------------------------------
66 * Called when eth phy state changes (w/ interrupts disabled)
69 link_changed_callback(int speed)
71 link_is_up = speed == 0 ? false : true;
72 hal_set_leds(link_is_up ? 0x2 : 0x0, 0x2);
73 printf("\neth link changed: speed = %d\n", speed);
77 timer_irq_handler(unsigned irq)
79 hal_set_timeout(timer_delta); // schedule next timeout
85 init_packet(int *buf, const u2_eth_packet_t *pkt, int bufnum)
88 int mark = ((bufnum & 0xff) << 24) | 0x005A0000;
90 for (i = 0; i < BP_NLINES; i++){
95 // copy header into buffer
96 memcpy_wa(buf, pkt, sizeof(*pkt));
102 u2_eth_packet_t pkt __attribute__((aligned (4)));
104 memset(&pkt, 0, sizeof(pkt));
106 pkt.ehdr.dst = dst_mac_addr;
107 // src address filled in by mac
109 pkt.ehdr.ethertype = U2_ETHERTYPE;
110 pkt.fixed.word0 = 0x01234567;
111 pkt.fixed.timestamp = 0xffffffff;
113 // init just the one we're using
114 init_packet((void *)buffer_ram(CPU_TX_BUF), &pkt, CPU_TX_BUF);
120 int npackets_sent = 0;
124 // setup tx gpio bits for GPIOM_FPGA_1 -- fpga debug output
125 //hal_gpio_set_sels(GPIO_TX_BANK, "1111111111111111");
126 //hal_gpio_set_sels(GPIO_RX_BANK, "1111111111111111");
128 putstr("\ngen_eth_packets\n");
130 hal_set_leds(0x0, 0x3);
134 pic_register_handler(IRQ_TIMER, timer_irq_handler);
136 if (hwconfig_simulation_p())
137 timer_delta = sim_timer_delta;
139 hal_set_timeout(timer_delta);
141 ethernet_register_link_changed_callback(link_changed_callback);
145 if (hwconfig_simulation_p()){
146 eth_mac->speed = 4; // hardcode mac speed to 1000
151 // fire off a receive from the ethernet
152 bp_receive_to_buf(CPU_RX_BUF, PORT_ETH, 1, 0, BP_LAST_LINE);
155 uint32_t status = buffer_pool_status->status;
157 if (status & (BPS_DONE(CPU_RX_BUF) | BPS_ERROR(CPU_RX_BUF))){
158 bp_clear_buf(CPU_RX_BUF);
159 // ignore incoming ethernet packets; they were looped back in sim
160 bp_receive_to_buf(CPU_RX_BUF, PORT_ETH, 1, 0, BP_LAST_LINE);
163 if (status & (BPS_DONE(CPU_TX_BUF) | BPS_ERROR(CPU_TX_BUF))){
164 if (status & BPS_ERROR(CPU_TX_BUF)){
167 bp_clear_buf(CPU_TX_BUF);
169 if ((npackets_sent & 0xF) == 0){ // print after every 16 packets
175 if (link_is_up && send_packet_now && (status & BPS_IDLE(CPU_TX_BUF))){
176 send_packet_now = false;
178 // kick off the next packet
179 // FIXME set packet number in packet
181 bp_send_from_buf(CPU_TX_BUF, PORT_ETH, 1, 0, 255); // 1KB total
182 hal_toggle_leds(0x1);