OpenCores
URL https://opencores.org/ocsvn/1g_ethernet_dpi/1g_ethernet_dpi/trunk

Subversion Repositories 1g_ethernet_dpi

[/] [1g_ethernet_dpi/] [trunk/] [sw/] [dev/] [test_main/] [src/] [net/] [net.c] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 kuzmi4
 
2
#include <stdio.h>
3
 
4
#include "xil_types.h"
5
#include "xil_io.h"
6
 
7
#include "xparameters.h" // XPAR_TMEMAC_0_BASEADDR, XPAR_AXIDMA_0_DEVICE_ID, XPAR_BRAM_0_BASEADDR
8
#include "xaxidma.h" // XAxiDma_LookupConfig, ..
9
 
10
#include "net.h"
11
#include "arp.h"
12
#include "ip.h"
13
#include "icmp.h"
14
 
15
// NET-IF
16
net_if_t net_if;
17
u8 mac_addr[8];
18
 
19
// DMA 
20
XAxiDma AxiDma;
21
XAxiDma_Config *CfgPtr;
22
 
23
// BUFF
24
UINTPTR tx_buff;
25
UINTPTR rx_buff;
26
int rx_len;
27
 
28
// ??
29
int mac_raw_send(char *data_i, int data_bytes);    // net_init
30
void low_level_input(char *buff);             // net_input
31
 
32
 
33
// net-stack INIT
34
int net_init(tmemac_cfg_t *iv_tmemac_cfg, u32 pkt_tx, u32 pkt_rx)
35
{
36
    int result;
37
 
38
    // DMA init
39
    CfgPtr = XAxiDma_LookupConfig(XPAR_AXIDMA_0_DEVICE_ID);
40
    if (!CfgPtr) { return -1; }
41
 
42
    result = XAxiDma_CfgInitialize(&AxiDma, CfgPtr);
43
    if (result != XST_SUCCESS) { return -2; }
44
 
45
    XAxiDma_IntrDisable(&AxiDma, XAXIDMA_IRQ_ALL_MASK, XAXIDMA_DEVICE_TO_DMA); // Disable interrupts, we use polling mode
46
    XAxiDma_IntrDisable(&AxiDma, XAXIDMA_IRQ_ALL_MASK, XAXIDMA_DMA_TO_DEVICE); // ..
47
 
48
    tx_buff = (UINTPTR)pkt_tx;
49
    rx_buff = (UINTPTR)pkt_rx;
50
    rx_len = 0;
51
 
52
    result = XAxiDma_SimpleTransfer(&AxiDma,(UINTPTR)rx_buff, 1024, XAXIDMA_DEVICE_TO_DMA);
53
    if (result) {
54
        return -3;
55
    }
56
 
57
    // ETH init
58
    result = tri_mode_emac_init(iv_tmemac_cfg);
59
    if (result) {
60
        return -4;
61
    }
62
 
63
    // NET-IF init
64
    //  ip
65
    net_if.ip_addr = htonl(iv_tmemac_cfg->ip_addr);
66
    //  mac
67
    mac_addr[0] = ((iv_tmemac_cfg->mac_high) >>  8) & 0xFF;
68
    mac_addr[1] = ((iv_tmemac_cfg->mac_high) >>  0) & 0xFF;
69
    mac_addr[2] = ((iv_tmemac_cfg->mac_low ) >> 24) & 0xFF;
70
    mac_addr[3] = ((iv_tmemac_cfg->mac_low ) >> 16) & 0xFF;
71
    mac_addr[4] = ((iv_tmemac_cfg->mac_low ) >>  8) & 0xFF;
72
    mac_addr[5] = ((iv_tmemac_cfg->mac_low ) >>  0) & 0xFF;
73
    net_if.mac_addr = &mac_addr[0];
74
    //  raw_send
75
    net_if.net_raw_send = mac_raw_send;
76
 
77
    // ARP
78
    eth_arp_init(&net_if);
79
    // IP
80
    eth_ip_init(&net_if);
81
    // ICMP
82
    eth_icmp_init(&net_if);
83
 
84
    // Final
85
    return 0;
86
}
87
// poll income-pkt
88
int net_input(char *buff)
89
{
90
    // rx
91
    low_level_input(buff);
92
    // prep-len
93
    int len = rx_len;
94
    rx_len = 0;
95
    // Final
96
    return len;
97
}
98
 
99
 
100
// tbd
101
void low_level_input(char *buff)
102
{
103
    // dec vars
104
    int i, result;
105
 
106
    // check Dma_Busy
107
    if (XAxiDma_Busy(&AxiDma, XAXIDMA_DEVICE_TO_DMA)) {
108
        return;
109
    }
110
 
111
    // if we are here -> check dma-status
112
    result = XAxiDma_IntrGetIrq(&AxiDma, XAXIDMA_DEVICE_TO_DMA);
113
    if (result & XAXIDMA_IRQ_ERROR_MASK) {
114
        rx_len = -1;
115
        return;
116
    }
117
    if (result & XAXIDMA_IRQ_IOC_MASK){
118
        // if we are here  we have rxd / cp 1st 512B to USER-buff
119
        u32 *tmp = (u32 *)buff;
120
        for (i = 0; i < 512/4; i++){
121
            *tmp = Xil_In32(rx_buff+(i*4));
122
            tmp++;
123
        }
124
        rx_len = 512/4;
125
    }
126
 
127
    // irq-ack
128
    XAxiDma_IntrAckIrq(&AxiDma, XAXIDMA_IRQ_ALL_MASK, XAXIDMA_DEVICE_TO_DMA);
129
    // restart dma
130
    result = XAxiDma_SimpleTransfer(&AxiDma,(UINTPTR)rx_buff, 1024, XAXIDMA_DEVICE_TO_DMA);
131
    // Final
132
    rx_len = (result == XST_SUCCESS)? rx_len : -2;
133
}
134
// eth-tx data via {DMA+MAC}
135
int mac_raw_send(char *data_i, int data_bytes)
136
{
137
    // dec vars
138
    int i, len_dw, len_b;
139
    u32 *odata_dw, *idata_dw;
140
    u8  *odata_b,  *idata_b;
141
 
142
    // chk
143
    if ((long)data_i & 0x03) { // DW-align
144
        xil_printf("ERR: tx-buff addr\n\r");
145
        return -1;
146
    }
147
    // prep
148
    len_dw = data_bytes / 4;
149
    len_b  = data_bytes & (4-1);
150
    // #0 - copy main DW-body
151
    odata_dw = (u32 *)tx_buff;
152
    idata_dw = (u32 *)data_i;
153
    for (i = 0; i < len_dw; i++) {
154
        Xil_Out32((long)odata_dw, *idata_dw);
155
        odata_dw++; idata_dw++;
156
    }
157
    // #1 - copy BYTE-tail
158
    if (len_b) { // always DW-aligned
159
        Xil_Out32((long)odata_dw, *idata_dw);
160
    }
161
/*
162
    odata_b = (u8 *)odata_dw;
163
    idata_b = (u8 *)idata_dw;
164
    for (i = 0; i < len_b; i++) {
165
        Xil_Out8((long)odata_b, *idata_b);
166
        odata_b++; idata_b++;
167
    }
168
*/
169
 
170
    // tx
171
    i = XAxiDma_SimpleTransfer(&AxiDma, tx_buff, data_bytes, XAXIDMA_DMA_TO_DEVICE);
172
    // Final
173
    return i;
174
}

powered by: WebSVN 2.1.0

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