OpenCores
URL https://opencores.org/ocsvn/minsoc/minsoc/trunk

Subversion Repositories minsoc

[/] [minsoc/] [trunk/] [sw/] [eth/] [eth.c] - Blame information for rev 17

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 rfajardo
#include "../support/support.h"
2
#include "../support/board.h"
3
#include "../support/uart.h"
4
 
5
#include "../support/spr_defs.h"
6
 
7
#include "eth.h"
8
 
9
void uart_print_str(char *);
10
void uart_print_long(unsigned long);
11
 
12
// Dummy or32 except vectors
13
void buserr_except(){}
14
void dpf_except(){}
15
void ipf_except(){}
16
void lpint_except(){}
17
void align_except(){}
18
void illegal_except(){}
19
/*void hpint_except(){
20
 
21
}*/
22
void dtlbmiss_except(){}
23
void itlbmiss_except(){}
24
void range_except(){}
25
void syscall_except(){}
26
void res1_except(){}
27
void trap_except(){}
28
void res2_except(){}
29
 
30
 
31
void uart_interrupt()
32
{
33
    char lala;
34
    unsigned char interrupt_id;
35
    interrupt_id = REG8(UART_BASE + UART_IIR);
36
    if ( interrupt_id & UART_IIR_RDI )
37
    {
38
        lala = uart_getc();
39
        uart_putc(lala+1);
40
    }
41
 
42
}
43
 
44
 
45
void uart_print_str(char *p)
46
{
47
        while(*p != 0) {
48
                uart_putc(*p);
49
                p++;
50
        }
51
}
52
 
53
void uart_print_long(unsigned long ul)
54
{
55
  int i;
56
  char c;
57
 
58
 
59
  uart_print_str("0x");
60
  for(i=0; i<8; i++) {
61
 
62
  c = (char) (ul>>((7-i)*4)) & 0xf;
63
  if(c >= 0x0 && c<=0x9)
64
    c += '0';
65
  else
66
    c += 'a' - 10;
67
  uart_putc(c);
68
  }
69
 
70
}
71
 
72
void uart_print_short(unsigned long ul)
73
{
74
  int i;
75
  char c;
76
  char flag=0;
77
 
78
 
79
  uart_print_str("0x");
80
  for(i=0; i<8; i++) {
81
 
82
  c = (char) (ul>>((7-i)*4)) & 0xf;
83
  if(c >= 0x0 && c<=0x9)
84
    c += '0';
85
  else
86
    c += 'a' - 10;
87
  if ((c != '0') || (i==7))
88
    flag=1;
89
  if(flag)
90
    uart_putc(c);
91
  }
92
 
93
}
94
 
95
int tx_done;
96
int rx_done;
97
int rx_len;
98
char tx_data[1536];     //max length
99
char rx_data[1536];
100
 
101
void eth_init()
102
{
103
        //TXEN & RXEN = 1; PAD & CRC = 1; FULLD = 1
104
        REG32(ETH_BASE + ETH_MODER) = ETH_TXEN | ETH_RXEN | ETH_PAD | ETH_CRCEN | ETH_FULLD;
105
        //PHY Address = 0x001
106
        REG32(ETH_BASE + ETH_MIIADDRESS) = 0x00000001;
107
 
108
        //enable all interrupts
109
        REG32(ETH_BASE + ETH_INT_MASK) = ETH_RXC | ETH_TXC | ETH_BUSY | ETH_RXE | ETH_RXB | ETH_TXE | ETH_TXB;
110
 
111
        //set MAC ADDRESS
112
        REG32(ETH_BASE + ETH_MAC_ADDR1) = OWN_MAC_ADDRESS >> 32;        //low word = mac address high word
113
        REG32(ETH_BASE + ETH_MAC_ADDR0) = OWN_MAC_ADDRESS;              //mac address rest
114
 
115
        //configure TXBD0
116
        REG32(ETH_BASE + ETH_TXBD0H) = tx_data;         //address used for tx_data
117
        REG32(ETH_BASE + ETH_TXBD0L) = TX_READY;        //length = 0 | PAD & CRC = 1 | IRQ & WR = 1
118
 
119
        //configure RXBD0
120
        REG32(ETH_BASE + ETH_RXBD0H) = rx_data;         //address used for tx_data
121
        REG32(ETH_BASE + ETH_RXBD0L) = RX_READY;        //len = 0 | IRQ & WR = 1 | EMPTY = 1
122
 
123
        //set txdata
124
        tx_data[0] = BROADCAST_ADDRESS >> 40;
125
        tx_data[1] = BROADCAST_ADDRESS >> 32;
126
        tx_data[2] = BROADCAST_ADDRESS >> 24;
127
        tx_data[3] = BROADCAST_ADDRESS >> 16;
128
        tx_data[4] = BROADCAST_ADDRESS >> 8;
129
        tx_data[5] = BROADCAST_ADDRESS;
130
 
131
        tx_data[6] = OWN_MAC_ADDRESS >> 40;
132
        tx_data[7] = OWN_MAC_ADDRESS >> 32;
133
        tx_data[8] = OWN_MAC_ADDRESS >> 24;
134
        tx_data[9] = OWN_MAC_ADDRESS >> 16;
135
        tx_data[10] = OWN_MAC_ADDRESS >> 8;
136
        tx_data[11] = OWN_MAC_ADDRESS;
137
 
138
        //erase interrupts
139
        REG32(ETH_BASE + ETH_INT_SOURCE) = ETH_RXC | ETH_TXC | ETH_BUSY | ETH_RXE | ETH_RXB | ETH_TXE | ETH_TXB;
140
 
141
    tx_done = 1;
142
    rx_done = 0;
143
    rx_len = 0;
144
}
145
 
146
void eth_send(int length)
147
{
148
    if (!tx_done)
149
        return;
150
 
151
    tx_done = 0;
152
        tx_data[12] = length >> 8;
153
        tx_data[13] = length;
154
 
155
        REG32(ETH_BASE + ETH_TXBD0L) = (( 0x0000FFFF & ( length + HDR_LEN ) ) << 16) | BD_SND;
156
}
157
 
158
void eth_receive()
159
{
160
    int i;
161
    uart_print_str("Length: \n");
162
    uart_print_long(rx_len - HDR_LEN - CRC_LEN);
163
    uart_print_str("\n");
164
    uart_print_str("Data: \n");
165
    for ( i = 0; i < rx_len - HDR_LEN - CRC_LEN; i++ )
166
    {
167
        uart_print_short(rx_data[i+HDR_LEN]);
168
//        uart_print_str("\n");
169
    }
170
 
171
    rx_done = 0;
172
    rx_len = 0;
173
}
174
 
175
void eth_interrupt()
176
{
177
    unsigned long source = REG32(ETH_BASE + ETH_INT_SOURCE);
178
    switch (source)
179
    {
180
        case ETH_TXB:
181
            tx_done = 1;
182
            //erase interrupt
183
            REG32(ETH_BASE + ETH_INT_SOURCE) |= ETH_TXB;
184
            break;
185
 
186
        case ETH_RXB:
187
            rx_done = 1;
188
            //erase interrupt
189
            REG32(ETH_BASE + ETH_INT_SOURCE) |= ETH_RXB;
190
            //accept further data (reset RXBD to empty)
191
            rx_len = (REG32(ETH_BASE + ETH_RXBD0L) >> 16);
192
            REG32(ETH_BASE + ETH_RXBD0L) = RX_READY;    //len = 0 | IRQ & WR = 1 | EMPTY = 1
193
            break;
194
 
195
        default:
196
            break;
197
    }
198
}
199
 
200
int main()
201
{
202
    unsigned long lalala;
203
        uart_init();
204
 
205
        int_init();
206
        int_add(UART_IRQ, &uart_interrupt);
207
        int_add(ETH_IRQ, &eth_interrupt);
208
 
209
        /* We can't use printf because in this simple example
210
           we don't link C library. */
211
        uart_print_str("Hello World.\n\r");
212
 
213
        eth_init();
214
 
215
        tx_data[14] = 0xFF;
216
        tx_data[15] = 0x2B;
217
        tx_data[16] = 0x40;
218
        tx_data[17] = 0x50;
219
 
220
        eth_send(4);
221
 
222
    while(1)
223
    {
224
        if (rx_done)
225
        {
226
            eth_receive();
227
        }
228
    }
229
 
230
        report(0xdeaddead);
231
        or32_exit(0);
232
}
233
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.