aboutsummaryrefslogtreecommitdiffstats
path: root/firmware/microblaze/apps/cruft/gen_pause_frames.c
blob: 0f81dafff2fbef38ad7a1f837cdfb87e8cadbd75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
 * Copyright 2007 Free Software Foundation, Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "u2_init.h"
#include "memory_map.h"
#include "spi.h"
#include "hal_io.h"
#include "buffer_pool.h"
#include "pic.h"
#include <stdbool.h>
#include "ethernet.h"
#include "nonstdio.h"
#include "u2_eth_packet.h"
#include "memcpy_wa.h"
#include <stddef.h>
#include <stdlib.h>


// ----------------------------------------------------------------

unsigned char dst_mac_addr[6] = {
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff
};

// ----------------------------------------------------------------

// #define	PACKET_SIZE 1500		// bytes
// #define ETH_DATA_RATE 1000000		// 1MB/s
// #define	ETH_PACKET_RATE (ETH_DATA_RATE/PACKET_SIZE)	// 13,3333 pkts/s

// static int timer_delta = MASTER_CLK_RATE/ETH_PACKET_RATE;	// ticks between interrupts

static int timer_delta = MASTER_CLK_RATE/1000;	// tick at 1kHz

static volatile bool send_packet_now = false;   // timer handler sets this
static volatile bool link_is_up = false;	// eth handler sets this

int packet_number = 0;

// ----------------------------------------------------------------

// debugging output on tx pins
#define LS_MASK  0xE0000
#define LS_1000  0x80000
#define LS_100   0x40000
#define LS_10    0x20000


/*
 * Called when eth phy state changes (w/ interrupts disabled)
 */
void
link_changed_callback(int speed)
{
  int v = 0;
  switch(speed){
  case 10:
    v = LS_10;
    link_is_up = true;
    break;
    
  case 100:
    v = LS_100;
    link_is_up = true;
    break;
    
  case 1000:
    v = LS_100;
    link_is_up = true;
    break;

  default:
    v = 0;
    link_is_up = false;
    break;
  }

  hal_gpio_set_tx(v, LS_MASK);	/* set debug bits on d'board */

  putstr("\neth link changed: speed = ");
  puthex16_nl(speed);
}

void
timer_irq_handler(unsigned irq)
{
  hal_set_timeout(timer_delta);	// schedule next timeout
  send_packet_now = 1;
}


void
buffer_irq_handler(unsigned irq)
{
  // FIXME
}

static void
init_packet(int *buf, const u2_eth_packet_t *pkt, int bufnum)
{
  int i = 0;
  int mark = ((bufnum & 0xff) << 24) | 0x005A0000;

  for (i = 0; i < BP_NLINES; i++){
    buf[i] = mark | i;
    mark ^= 0x00FF0000;
  }

  // copy header into buffer
  memcpy_wa(buf, pkt, sizeof(*pkt));
}

static void
init_packets(void)
{
  int	i;
  
  u2_eth_packet_t	pkt __attribute__((aligned (4)));

  for (i = 0; i < 6; i++){
    pkt.ehdr.dst.addr[i] = dst_mac_addr[i];
  }
  pkt.ehdr.src = *ethernet_mac_addr();
  pkt.ehdr.ethertype = U2_ETHERTYPE;

  // fill ALL buffers for debugging
  for (i = 0; i < 8; i++)
    init_packet((void *)buffer_ram(i), &pkt, i);
}

static int led_counter = 0;

int
main(void)
{
  int send_pause = 1;
  
  u2_init();

  // setup tx gpio bits for GPIOM_FPGA_1 -- fpga debug output
  //hal_gpio_set_sels(GPIO_TX_BANK, "1111111111111111");
  //hal_gpio_set_sels(GPIO_RX_BANK, "1111111111111111");

  putstr("\ngen_eth_packets\n");
  
  // Control LEDs
  output_regs->leds = 0x00;

  init_packets();

  // pic_register_handler(IRQ_BUFFER, buffer_irq_handler);  // poll for now
  pic_register_handler(IRQ_TIMER, timer_irq_handler);
  hal_set_timeout(timer_delta);

  ethernet_register_link_changed_callback(link_changed_callback);

  ethernet_init();

  eth_mac->pause_frame_send_en = 1;
  eth_mac->pause_quanta_set = 16384 / 512;

  // eth_mac->speed = 4;	// FIXME hardcode mac speed to 1000

  while(1){
    if (link_is_up && send_packet_now){
      send_packet_now = false;


      if (send_pause)
	eth_mac->xon_cpu = 1;
      else
	eth_mac->xon_cpu = 0;

      send_pause ^= 1;

      // kick off the next packet
      // FIXME set packet number in packet

#if 0
      bp_send_from_buf(0, PORT_ETH, 1, 0, 255);	// 1KB total

      while ((buffer_pool_status->status & (BPS_DONE_0|BPS_ERROR_0)) == 0)
	;
      bp_clear_buf(0);
#endif

      output_regs->leds = ((++led_counter) & 0x1) | (link_is_up ? 0x2 : 0x0);
    }
  }

  hal_finish();
  return 1;
}