Rev 268 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
#ifndef ETHERNET_H#define ETHERNET_H#define ETH_TX_DESCRIPTOR_COUNT 2#define ETH_RX_DESCRIPTOR_COUNT 10#define ETH_TX_ED_BUFFER_SIZE 2032#define ETH_RX_ED_BUFFER_SIZE 2032#ifdef CEREBOT_MX7CK#define PHY_RESET_TRIS TRISAbits.TRISA6#define PHY_RESET_LAT LATAbits.LATA6#endif#ifdef CEREBOT_32MX7#define PHY_RESET_TRIS TRISEbits.TRISE9#define PHY_RESET_LAT LATEbits.LATE9#endif#define ETH_MDC_TRIS TRISDbits.TRISD11#define ETH_MDIO_TRIS TRISDbits.TRISD8#define ETH_TXEN_TRIS TRISDbits.TRISD6#define ETH_TXD0_TRIS TRISFbits.TRISF1#define ETH_TXD1_TRIS TRISFbits.TRISF0#define ETH_RXCLK_TRIS TRISGbits.TRISG9#define ETH_RXDV_TRIS TRISGbits.TRISG8#define ETH_RXD0_TRIS TRISBbits.TRISB12#define ETH_RXD1_TRIS TRISBbits.TRISB13#define ETH_RXERR_TRIS TRISBbits.TRISB11#define PHY_ADDRESS 0x0typedef union {struct {uint8_t BYTE_0;uint8_t BYTE_1;uint8_t BYTE_2;uint8_t BYTE_3;uint8_t BYTE_4;uint8_t BYTE_5;};uint8_t bytes[6];} ETH_MAC_ADDRESS;typedef union {struct {// Bits 31:0unsigned BYTE_COUNT : 16; // Total bytes in frame not counting collided bytesunsigned COLLISION_COUNT : 4; // Number of collisions current packet incurred durrent transmit attemptsunsigned CRC_ERROR : 1; // Attached CRC did not match the internal generated CRCunsigned LENGTH_CHECK_ERROR : 1; // Frame length field value in packet does not match actual data byte length and is not a Type fieldunsigned LENGTH_OUT_OF_RANGE : 1; // Frame type/length field was larger than 1500 bytesunsigned DONE : 1; // Transmit of packet was completedunsigned MULTICASE : 1; // Destination address was a multicast addressunsigned BROADCAST : 1; // Destination address was a broadcast addressunsigned PACKET_DEFER : 1; // Packet was deferred for at least one attemptunsigned EXCESSIVE_DEFER : 1; // Packet was defered in excess of 6071/24287 nibble(100Mbps)/bit(10Mbps) timesunsigned MAXIMUM_COLLISION : 1; // Packet aborted, number of collisions exceeded RETXunsigned LATE_COLLISION : 1; // Collision occurred beyond the collision window (512 bit times)unsigned GIANT : 1; // Frame byte count greater than MACMAXFunsigned UNDERRUN : 1; // Failed to transfer complete packet to the transmit MAC module// Bits 63:32unsigned BYTES_TRANSMITTED : 16; // Total bytes transmitted on wire (including collisions)unsigned CONTROL_FRAME : 1; // Frame transmitted was a control frameunsigned PAUSE_CONTROL_FRAME : 1; // Frame transmitted was a control frame with a valid PAUSE Op codeunsigned BACKPRESSURE : 1; // Carrier-sense method backpressure was previously appliedunsigned VLAN_TAGGED : 1; // Frame length/type field contained 0x8100 (VLAN protocol identifier)unsigned : 12;};uint32_t registers[2];} ETH_TRANSMIT_STATUS_VECTOR;typedef union {struct {// Bits 31:0unsigned BYTE_COUNT : 16; // Length of received frameunsigned LONG_DROP_EVENT : 1; // Packet over 50000 bit times occured or packet since last RSV was droppedunsigned RXDV_EVENT : 1; // Last receive event seen not long enough to be a valid packetunsigned CARRIER_EVENT : 1; // Carrier event detected, noted, and reportedunsigned CODE_VIOLATION : 1; // MII data does not represent a valid data code when MRXER assertedunsigned CRC_ERROR : 1; // Frame CRC does not match the CRC calculated by the receiver MACunsigned LENGTH_CHECK_ERROR : 1; // Frame length field value doe snot match the actual data byte lengthunsigned LENGTH_OUT_OF_RANGE : 1; // Frame type/length field was larger than 1500 bytesunsigned RECEIVE_OK : 1; // Packet has a valid CRC and no symbol errorsunsigned MULTICAST : 1; // Packet had a valid multicast addressunsigned BROADCAST : 1; // Packet had a valid broadcast addressunsigned DRIBBLE_NIBBLE : 1; // An additional 1-7 bits received after packetunsigned CONTROL_FRAME : 1; // Frame recognized as a control frameunsigned PAUSE_CONTROL_FRAME : 1; // Frame recognized as a control frame with a valid PAUSE Op codeunsigned UNKNOWN_OP_CODE : 1; // Frame recognized as a control frame but with an unknown Op codeunsigned VLAN_TAGGED : 1; // Frame recognized as a VLAN tagged frameunsigned : 1;// Bits 63:32;unsigned PKT_CHECKSUM : 16; // RX packet payload checksum of this descriptor's packetunsigned : 8;unsigned RUNT_PACKET : 1; // Runt packetunsigned BROADCAST_OR_NOT_DEST : 1; // NOT unicast match AND NOT multicast matchunsigned HASH_TABLE_MATCH : 1; // Hash table matchunsigned MAGIC_PACKET_MATCH : 1; // Magic packet matchunsigned PATTERN_MATCH : 1; // Pattern matchunsigned UNICAST_MATCH : 1; // Unicast matchunsigned BROADCAST_MATCH : 1; // Broadcast matchunsigned MULTICAST_MATCH : 1; // Multicast match};uint32_t registers[2];} ETH_RECEIVE_STATUS_VECTOR;typedef struct {unsigned : 7;unsigned EOWN : 1; // Ethernet controller own bit (1 = ED owned by controller, do not modify)unsigned NPV : 1; // Next ED pointer valid enable bit (1 = NEXT_ED field exists)unsigned : 7;unsigned BYTE_COUNT : 11; // Number of bytes to be transmited for this descriptor (1-2047)unsigned : 3;unsigned EOP : 1; // End of packet enable bit (1 = transmit end of packet delimiter)unsigned SOP : 1; // Start of packet enable bit (1 = transmit start of packet delimiter)uint32_t BUFFER_ADDR; // Starting point address of the data bufferETH_TRANSMIT_STATUS_VECTOR TSV; // Transmit status vector bits} ETH_TX_ETHERNET_DESCRIPTOR;typedef struct {unsigned : 7;unsigned EOWN : 1; // Ethernet controller own bit (1 = ED owned by controller, do not modify)unsigned NPV : 1; // Next ED pointer valid enable bit (1 = NEXT_ED field exists)unsigned : 7;unsigned BYTE_COUNT : 11; // Number of bytes to be transmited for this descriptor (1-2047)unsigned : 3;unsigned EOP : 1; // End of packet enable bit (1 = transmit end of packet delimiter)unsigned SOP : 1; // Start of packet enable bit (1 = transmit start of packet delimiter)uint32_t BUFFER_ADDR; // Starting point address of the data bufferETH_RECEIVE_STATUS_VECTOR RSV; // Transmit status vector bits} ETH_RX_ETHERNET_DESCRIPTOR;typedef struct {ETH_TX_ETHERNET_DESCRIPTOR descriptor[ETH_TX_DESCRIPTOR_COUNT];uint32_t next_ED;} ETH_TX_DESCRIPTOR_TABLE;typedef struct {ETH_RX_ETHERNET_DESCRIPTOR descriptor[ETH_RX_DESCRIPTOR_COUNT];uint32_t next_ED;} ETH_RX_DESCRIPTOR_TABLE;typedef struct {ETH_TX_DESCRIPTOR_TABLE TX_ED_table;ETH_RX_DESCRIPTOR_TABLE RX_ED_table;uint8_t TX_ED_buffer[ETH_TX_DESCRIPTOR_COUNT][ETH_TX_ED_BUFFER_SIZE];uint8_t RX_ED_buffer[ETH_RX_DESCRIPTOR_COUNT][ETH_RX_ED_BUFFER_SIZE];uint8_t TX_descriptor_index;uint8_t RX_descriptor_index;void (*tx_callback)(void);void (*rx_callback)(void);} ETH_DATA;void ETH_Init(ETH_DATA *data, void(*tx_callback)(void), void(*rx_callback)(void));uint16_t ETH_PHY_Read(uint8_t address, uint8_t reg);void ETH_PHY_Write(uint8_t address, uint8_t reg, uint16_t value);uint8_t ETH_Recv_Queue(void);uint8_t ETH_Read_Packet(uint8_t *buffer, uint16_t *length);uint8_t ETH_Write_Packet(ETH_MAC_ADDRESS dest, ETH_MAC_ADDRESS src, uint16_t length, uint8_t *buffer);#endif /* ETHERNET_H */