Subversion Repositories Code-Repo

Rev

Rev 268 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
255 Kevin 1
#ifndef ETHERNET_H
2
#define	ETHERNET_H
3
 
4
#define ETH_TX_DESCRIPTOR_COUNT 2
268 Kevin 5
#define ETH_RX_DESCRIPTOR_COUNT 10
255 Kevin 6
#define ETH_TX_ED_BUFFER_SIZE   2032
7
#define ETH_RX_ED_BUFFER_SIZE   2032
8
 
9
#ifdef CEREBOT_MX7CK
10
#define PHY_RESET_TRIS  TRISAbits.TRISA6
11
#define PHY_RESET_LAT   LATAbits.LATA6
12
#endif
13
#ifdef CEREBOT_32MX7
14
#define PHY_RESET_TRIS  TRISEbits.TRISE9
15
#define PHY_RESET_LAT   LATEbits.LATE9
16
#endif
17
#define ETH_MDC_TRIS    TRISDbits.TRISD11
18
#define ETH_MDIO_TRIS   TRISDbits.TRISD8
19
#define ETH_TXEN_TRIS   TRISDbits.TRISD6
20
#define ETH_TXD0_TRIS   TRISFbits.TRISF1
21
#define ETH_TXD1_TRIS   TRISFbits.TRISF0
22
#define ETH_RXCLK_TRIS  TRISGbits.TRISG9
23
#define ETH_RXDV_TRIS   TRISGbits.TRISG8
24
#define ETH_RXD0_TRIS   TRISBbits.TRISB12
25
#define ETH_RXD1_TRIS   TRISBbits.TRISB13
26
#define ETH_RXERR_TRIS  TRISBbits.TRISB11
27
 
28
#define PHY_ADDRESS     0x0
29
 
30
typedef union {
31
    struct {
32
        uint8_t BYTE_0;
33
        uint8_t BYTE_1;
34
        uint8_t BYTE_2;
35
        uint8_t BYTE_3;
36
        uint8_t BYTE_4;
37
        uint8_t BYTE_5;
38
    };
39
    uint8_t bytes[6];
40
} ETH_MAC_ADDRESS;
41
 
42
typedef union {
43
    struct {
44
        // Bits 31:0
45
        unsigned BYTE_COUNT             : 16;   // Total bytes in frame not counting collided bytes
46
        unsigned COLLISION_COUNT        : 4;    // Number of collisions current packet incurred durrent transmit attempts
47
        unsigned CRC_ERROR              : 1;    // Attached CRC did not match the internal generated CRC
48
        unsigned LENGTH_CHECK_ERROR     : 1;    // Frame length field value in packet does not match actual data byte length and is not a Type field
49
        unsigned LENGTH_OUT_OF_RANGE    : 1;    // Frame type/length field was larger than 1500 bytes
50
        unsigned DONE                   : 1;    // Transmit of packet was completed
51
        unsigned MULTICASE              : 1;    // Destination address was a multicast address
52
        unsigned BROADCAST              : 1;    // Destination address was a broadcast address
53
        unsigned PACKET_DEFER           : 1;    // Packet was deferred for at least one attempt
54
        unsigned EXCESSIVE_DEFER        : 1;    // Packet was defered in excess of 6071/24287 nibble(100Mbps)/bit(10Mbps) times
55
        unsigned MAXIMUM_COLLISION      : 1;    // Packet aborted, number of collisions exceeded RETX
56
        unsigned LATE_COLLISION         : 1;    // Collision occurred beyond the collision window (512 bit times)
57
        unsigned GIANT                  : 1;    // Frame byte count greater than MACMAXF
58
        unsigned UNDERRUN               : 1;    // Failed to transfer complete packet to the transmit MAC module
59
        // Bits 63:32
60
        unsigned BYTES_TRANSMITTED      : 16;   // Total bytes transmitted on wire (including collisions)
61
        unsigned CONTROL_FRAME          : 1;    // Frame transmitted was a control frame
62
        unsigned PAUSE_CONTROL_FRAME    : 1;    // Frame transmitted was a control frame with a valid PAUSE Op code
63
        unsigned BACKPRESSURE           : 1;    // Carrier-sense method backpressure was previously applied
64
        unsigned VLAN_TAGGED            : 1;    // Frame length/type field contained 0x8100 (VLAN protocol identifier)
65
        unsigned                        : 12;
66
    };
67
    uint32_t registers[2];
68
} ETH_TRANSMIT_STATUS_VECTOR;
69
 
70
typedef union {
71
    struct {
72
        // Bits 31:0
73
        unsigned BYTE_COUNT             : 16;   // Length of received frame
74
        unsigned LONG_DROP_EVENT        : 1;    // Packet over 50000 bit times occured or packet since last RSV was dropped
75
        unsigned RXDV_EVENT             : 1;    // Last receive event seen not long enough to be a valid packet
76
        unsigned CARRIER_EVENT          : 1;    // Carrier event detected, noted, and reported
77
        unsigned CODE_VIOLATION         : 1;    // MII data does not represent a valid data code when MRXER asserted
78
        unsigned CRC_ERROR              : 1;    // Frame CRC does not match the CRC calculated by the receiver MAC
79
        unsigned LENGTH_CHECK_ERROR     : 1;    // Frame length field value doe snot match the actual data byte length
80
        unsigned LENGTH_OUT_OF_RANGE    : 1;    // Frame type/length field was larger than 1500 bytes
81
        unsigned RECEIVE_OK             : 1;    // Packet has a valid CRC and no symbol errors
82
        unsigned MULTICAST              : 1;    // Packet had a valid multicast address
83
        unsigned BROADCAST              : 1;    // Packet had a valid broadcast address
84
        unsigned DRIBBLE_NIBBLE         : 1;    // An additional 1-7 bits received after packet
85
        unsigned CONTROL_FRAME          : 1;    // Frame recognized as a control frame
86
        unsigned PAUSE_CONTROL_FRAME    : 1;    // Frame recognized as a control frame with a valid PAUSE Op code
87
        unsigned UNKNOWN_OP_CODE        : 1;    // Frame recognized as a control frame but with an unknown Op code
88
        unsigned VLAN_TAGGED            : 1;    // Frame recognized as a VLAN tagged frame
89
        unsigned                        : 1;
90
        // Bits 63:32;
91
        unsigned PKT_CHECKSUM           : 16;   // RX packet payload checksum of this descriptor's packet
92
        unsigned                        : 8;
93
        unsigned RUNT_PACKET            : 1;    // Runt packet
94
        unsigned BROADCAST_OR_NOT_DEST  : 1;    // NOT unicast match AND NOT multicast match
95
        unsigned HASH_TABLE_MATCH       : 1;    // Hash table match
96
        unsigned MAGIC_PACKET_MATCH     : 1;    // Magic packet match
97
        unsigned PATTERN_MATCH          : 1;    // Pattern match
98
        unsigned UNICAST_MATCH          : 1;    // Unicast match
99
        unsigned BROADCAST_MATCH        : 1;    // Broadcast match
100
        unsigned MULTICAST_MATCH        : 1;    // Multicast match
101
    };
102
    uint32_t registers[2];
103
} ETH_RECEIVE_STATUS_VECTOR;
104
 
105
typedef struct {
106
    unsigned        : 7;
107
    unsigned EOWN   : 1;            // Ethernet controller own bit (1 = ED owned by controller, do not modify)
108
    unsigned NPV    : 1;            // Next ED pointer valid enable bit (1 = NEXT_ED field exists)
109
    unsigned        : 7;
110
    unsigned BYTE_COUNT : 11;       // Number of bytes to be transmited for this descriptor (1-2047)
111
    unsigned        : 3;
112
    unsigned EOP    : 1;            // End of packet enable bit (1 = transmit end of packet delimiter)
113
    unsigned SOP    : 1;            // Start of packet enable bit (1 = transmit start of packet delimiter)
114
    uint32_t BUFFER_ADDR;       // Starting point address of the data buffer
115
    ETH_TRANSMIT_STATUS_VECTOR TSV; // Transmit status vector bits
116
} ETH_TX_ETHERNET_DESCRIPTOR;
117
 
118
typedef struct {
119
    unsigned        : 7;
120
    unsigned EOWN   : 1;            // Ethernet controller own bit (1 = ED owned by controller, do not modify)
121
    unsigned NPV    : 1;            // Next ED pointer valid enable bit (1 = NEXT_ED field exists)
122
    unsigned        : 7;
123
    unsigned BYTE_COUNT : 11;       // Number of bytes to be transmited for this descriptor (1-2047)
124
    unsigned        : 3;
125
    unsigned EOP    : 1;            // End of packet enable bit (1 = transmit end of packet delimiter)
126
    unsigned SOP    : 1;            // Start of packet enable bit (1 = transmit start of packet delimiter)
127
    uint32_t BUFFER_ADDR;       // Starting point address of the data buffer
128
    ETH_RECEIVE_STATUS_VECTOR RSV;  // Transmit status vector bits
129
} ETH_RX_ETHERNET_DESCRIPTOR;
130
 
131
typedef struct {
132
    ETH_TX_ETHERNET_DESCRIPTOR descriptor[ETH_TX_DESCRIPTOR_COUNT];
133
    uint32_t next_ED;
134
} ETH_TX_DESCRIPTOR_TABLE;
135
 
136
typedef struct {
137
    ETH_RX_ETHERNET_DESCRIPTOR descriptor[ETH_RX_DESCRIPTOR_COUNT];
138
    uint32_t next_ED;
139
} ETH_RX_DESCRIPTOR_TABLE;
140
 
141
typedef struct {
142
    ETH_TX_DESCRIPTOR_TABLE TX_ED_table;
143
    ETH_RX_DESCRIPTOR_TABLE RX_ED_table;
144
    uint8_t TX_ED_buffer[ETH_TX_DESCRIPTOR_COUNT][ETH_TX_ED_BUFFER_SIZE];
145
    uint8_t RX_ED_buffer[ETH_RX_DESCRIPTOR_COUNT][ETH_RX_ED_BUFFER_SIZE];
146
    uint8_t TX_descriptor_index;
147
    uint8_t RX_descriptor_index;
148
    void (*tx_callback)(void);
149
    void (*rx_callback)(void);
150
} ETH_DATA;
151
 
152
void ETH_Init(ETH_DATA *data, void(*tx_callback)(void), void(*rx_callback)(void));
153
 
154
uint16_t ETH_PHY_Read(uint8_t address, uint8_t reg);
155
void ETH_PHY_Write(uint8_t address, uint8_t reg, uint16_t value);
156
 
157
uint8_t ETH_Recv_Queue(void);
158
uint8_t ETH_Read_Packet(uint8_t *buffer, uint16_t *length);
159
uint8_t ETH_Write_Packet(ETH_MAC_ADDRESS dest, ETH_MAC_ADDRESS src, uint16_t length, uint8_t *buffer);
160
 
161
#endif	/* ETHERNET_H */
162