Ethernet Library
PIC32MX
family of microcontrollers feature an embedded Ethernet controller module.
The Ethernet controller is a bus master module that interfaces with an off-chip Physical Layer (PHY) to implement a complete Ethernet node in a system.
It provides the modules needed to implement a 10/100 Mbps Ethernet node using an external PHY chip. In order to offload the CPU from moving packet data to and from the module, internal descriptor-based DMA engines are included in the controller.
The Ethernet Controller consists of the following modules:
- Media Access Control (MAC) block:
Responsible for implementing the MAC functions of the Ethernet specification. - Flow Control (FC) block :
Responsible for control of the transmission of PAUSE frames. Reception of PAUSE frames is handled within the MAC. - RX Filter (RXF) block :
This module performs filtering on every receive packet to determine whether each packet should be accepted or rejected. - TX DMA/TX BM Engine :
The TX DMA and TX Buffer Management engines perform data transfers from the memory (using descriptor tables) to the MAC Transmit Interface. - RX DMA/RX BM Engine :
The RX DMA and RX Buffer Management engines transfer receive packets from the MAC to the memory (using descriptor tables).
Ethernet library supports :
- Supports 10/100 Mbps data transfer rates.
- Supports full-duplex and half-duplex operation.
- Supports RMII and MII PHY interface.
- Supports MIIM PHY management interface.
- Supports both manual and automatic flow control.
- RAM descriptor-based DMA operation for both receive and transmit path.
- Fully configurable interrupts.
- Configurable receive packet filtering :
- CRC check.
- 64-byte pattern match.
- Broadcast, multicast and unicast packets.
- Magic Packet.
- 64-bit hash table.
- Runt packet.
- Supports packet payload checksum calculation
- Supports various hardware statistics counters

- By default, the Ethernet library implementation initializes the PHY SMSC 8720A chip (present on the mikroElektronika's MultiMedia Board for PIC32MX7).
- Global library variable
Ethernet_userTimerSec
is used to keep track of time for all client implementations (ARP, DNS, UDP and DHCP). It is user responsibility to increment this variable each second in it's code if any of the clients is used. - For advanced users there is
__EthernetPrivate.mpas
unit in Uses folder of the compiler with description of all routines and global variables, relevant to the user, implemented in the Ethernet Library.
Library Routines
- Ethernet_Init
- Ethernet_SetPHYInit
- Ethernet_Set_Default_PHY
- Ethernet_Enable
- Ethernet_Disable
- Ethernet_doPacket
- Ethernet_putByte
- Ethernet_putBytes
- Ethernet_putString
- Ethernet_putConstString
- Ethernet_putConstBytes
- Ethernet_getByte
- Ethernet_getBytes
- Ethernet_UserTCP
- Ethernet_UserUDP
- Ethernet_getIpAddress
- Ethernet_getGwIpAddress
- Ethernet_getDnsIpAddress
- Ethernet_getIpMask
- Ethernet_confNetwork
- Ethernet_arpResolve
- Ethernet_sendUDP
- Ethernet_dnsResolve
- Ethernet_initDHCP
- Ethernet_doDHCPLeaseTime
- Ethernet_renewDHCP
Ethernet_Init
Prototype |
procedure Ethernet_Init(var mac : array[6] of byte; var ip : array[4] of byte; configuration : dword); |
---|---|
Returns |
Nothing. |
Description |
This is MAC module routine. It initializes Ethernet controller. This function is internaly splited into 2 parts to help linker when coming short of memory. Ethernet controller settings (parameters not mentioned here are set to default):
Parameters:
![]() 0.0.0.0 .
|
Requires | Nothing. |
Example |
var myMacAddr : array[6] of byte; // my MAC address myIpAddr : array[4] of byte; // my IP addr ... myMacAddr[0] := 0x00; myMacAddr[1] := 0x14; myMacAddr[2] := 0xA5; myMacAddr[3] := 0x76; myMacAddr[4] := 0x19; myMacAddr[5] := 0x3F; myIpAddr[0] := 192; myIpAddr[1] := 168; myIpAddr[2] := 20; myIpAddr[3] := 60; Ethernet_Init(myMacAddr, myIpAddr, _ETHERNET_AUTO_NEGOTIATION); |
Ethernet_SetPHYInit
Prototype |
procedure Ethernet_SetPHYInit(PHYInit_ptr : ^TEthernet_PHYInit_Ptr); |
---|---|
Returns |
Nothing. |
Description |
By default, the Ethernet library implementation initializes the PHY SMSC 8720A chip (present on the mikroElektronika's MultiMedia Board for PIC32MX7).
Parameters:
|
Requires | Nothing. |
Example |
Ethernet_SetPHYInit(@Phy_Init); |
Ethernet_Set_Default_PHY
Prototype |
procedure Ethernet_Set_Default_PHY(); |
---|---|
Returns |
Nothing. |
Description |
initializes the PHY SMSC 8720A chip (present on the mikroElektronika's MultiMedia Board for PIC32MX7) to the library default. Parameters:
|
Requires | Nothing. |
Example |
Ethernet_Set_Default_PHY(); |
Ethernet_Enable
Prototype |
procedure Ethernet_Enable(enFlt : word); |
||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
Nothing. |
||||||||||||||||||||||||||||||||||||
Description |
This is MAC module routine. This routine enables appropriate network traffic on the MCU's internal Ethernet module by the means of it's receive filters (unicast, multicast, broadcast, crc). Specific type of network traffic will be enabled if a corresponding bit of this routine's input parameter is set. Therefore, more than one type of network traffic can be enabled at the same time. For this purpose, predefined library constants (see the table below) can be ORed to form appropriate input value. Parameters:
![]() |
||||||||||||||||||||||||||||||||||||
Requires |
Ethernet module has to be initialized. See Ethernet_Init. |
||||||||||||||||||||||||||||||||||||
Example |
Ethernet_Enable(_Ethernet_CRC or _Ethernet_UNICAST); // enable CRC checking and Unicast traffic |
Ethernet_Disable
Prototype |
procedure Ethernet_Disable(disFlt : word); |
||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
Nothing. |
||||||||||||||||||||||||||||||||||||
Description |
This is MAC module routine. This routine disables appropriate network traffic on the MCU's internal Ethernet module by the means of it's receive filters (unicast, multicast, broadcast, crc). Specific type of network traffic will be disabled if a corresponding bit of this routine's input parameter is set. Therefore, more than one type of network traffic can be disabled at the same time. For this purpose, predefined library constants (see the table below) can be ORed to form appropriate input value. Parameters:
![]() |
||||||||||||||||||||||||||||||||||||
Requires |
Ethernet module has to be initialized. See Ethernet_Init. |
||||||||||||||||||||||||||||||||||||
Example |
Ethernet_Disable(_Ethernet_CRC or _Ethernet_UNICAST); // disable CRC checking and Unicast traffic |
Ethernet_doPacket
Prototype |
function Ethernet_doPacket() : byte; |
---|---|
Returns |
|
Description |
This is MAC module routine. It processes next received packet if such exists. Packets are processed in the following manner:
![]() Ethernet_doPacket must be called as often as possible in user's code.
|
Requires |
Ethernet module has to be initialized. See Ethernet_Init. |
Example |
while true do begin ... Ethernet_doPacket(); // process received packets ... end; |
Ethernet_putByte
Prototype |
procedure Ethernet_putByte(v : byte); |
---|---|
Returns |
Nothing. |
Description |
This is MAC module routine. It stores one byte at current write location. Parameters:
|
Requires |
Ethernet module has to be initialized. See Ethernet_Init. |
Example |
var data : byte; ... Ethernet_putByte(data); // put an byte into buffer |
Ethernet_putBytes
Prototype |
procedure Ethernet_putBytes(ptr : ^byte; n : word); |
---|---|
Returns |
Nothing. |
Description |
This is MAC module routine. It stores requested number of bytes into RAM at current write location. Parameters:
|
Requires |
Ethernet module has to be initialized. See Ethernet_Init. |
Example |
var buffer : array[17] of byte; ... buffer := 'mikroElektronika'; ... Ethernet_putBytes(buffer, 16); // put an RAM array into buffer |
Ethernet_putConstBytes
Prototype |
procedure Ethernet_putConstBytes(const ptr : ^byte; n : word); |
---|---|
Returns |
Nothing. |
Description |
This is MAC module routine. It stores requested number of const bytes into RAM at current write location. Parameters:
|
Requires |
Ethernet module has to be initialized. See Ethernet_Init. |
Example |
const buffer : array[17] of byte; ... buffer := 'mikroElektronika'; ... Ethernet_putConstBytes(buffer, 16); // put a const array into buffer |
Ethernet_putString
Prototype |
function Ethernet_putString(ptr : ^byte) : word; |
---|---|
Returns |
Number of bytes written into RAM. |
Description |
This is MAC module routine. It stores whole string (excluding null termination) into RAM at current write location. Parameters:
|
Requires |
Ethernet module has to be initialized. See Ethernet_Init. |
Example |
var buffer : string[16]; ... buffer := 'mikroElektronika'; ... Ethernet_putString(buffer); // put a RAM string into buffer |
Ethernet_putConstString
Prototype |
function Ethernet_putConstString(const ptr : ^byte) : word; |
---|---|
Returns |
Number of bytes written into RAM. |
Description |
This is MAC module routine. It stores whole const string (excluding null termination) into RAM at current write location. Parameters:
|
Requires |
Ethernet module has to be initialized. See Ethernet_Init. |
Example |
const buffer : string[16]; ... buffer := 'mikroElektronika'; ... Ethernet_putConstString(buffer); // put a const string into buffer |
Ethernet_getByte
Prototype |
function Ethernet_getByte() : byte; |
---|---|
Returns |
Byte read from RAM. |
Description |
This is MAC module routine. It fetches a byte from address pointed to by current read location. |
Requires |
Ethernet module has to be initialized. See Ethernet_Init. |
Example |
var buffer : byte; ... buffer := Ethernet_getByte(); // read a byte from buffer |
Ethernet_getBytes
Prototype |
procedure Ethernet_getBytes(ptr : ^byte; addr : dword; n : word); |
---|---|
Returns |
Nothing. |
Description |
This is MAC module routine. It fetches requested number of bytes from RAM from address pointed to by current read location. If value of Parameters:
|
Requires |
Ethernet module has to be initialized. See Ethernet_Init. |
Example |
var buffer: array[16] of byte; ... Ethernet_getBytes(buffer, 0x100, 16); // read 16 bytes, starting from address 0x100 |
Ethernet_UserTCP
Prototype |
function Ethernet_UserTCP(var remoteHost : array[4] of byte; remotePort, localPort, reqLength : word; var flags: TEthPktFlags) : word; |
---|---|
Returns |
|
Description |
This is TCP module routine. It is internally called by the library. The user accesses to the TCP/HTTP request by using some of the Ethernet_get routines. The user puts data in the transmit buffer by using some of the Ethernet_put routines. The function must return the length in bytes of the TCP/HTTP reply, or 0 if there is nothing to transmit. If there is no need to reply to the TCP/HTTP requests, just define this function with return(0) as a single statement. Parameters:
![]() |
Requires |
Ethernet module has to be initialized. See Ethernet_Init. |
Example |
This function is internally called by the library and should not be called by the user's code. |
Ethernet_UserUDP
Prototype |
function Ethernet_UserUDP(var remoteHost : array[4] of byte; remotePort, destPort, reqLength : word; var flags: TEthPktFlags) : word; |
---|---|
Returns |
|
Description |
This is UDP module routine. It is internally called by the library. The user accesses to the UDP request by using some of the Ethernet_get routines. The user puts data in the transmit buffer by using some of the Ethernet_put routines. The function must return the length in bytes of the UDP reply, or 0 if nothing to transmit. If you don't need to reply to the UDP requests, just define this function with a return(0) as single statement. Parameters:
|
Requires |
Ethernet module has to be initialized. See Ethernet_Init. |
Example |
This function is internally called by the library and should not be called by the user's code. |
Ethernet_getIpAddress
Prototype |
function Ethernet_getIpAddress() : word;
|
---|---|
Returns | Pointer to the global variable holding IP address. |
Description |
This routine should be used when DHCP server is present on the network to fetch assigned IP address. ![]() |
Requires |
Ethernet module has to be initialized. See Ethernet_Init. |
Example |
var ipAddr : array[4] of byte; // user IP address buffer ... memcpy(ipAddr, Ethernet_getIpAddress(), 4); // fetch IP address |
Ethernet_getGwIpAddress
Prototype |
function Ethernet_getGwIpAddress() : word;
|
---|---|
Returns | Pointer to the global variable holding gateway IP address. |
Description |
This routine should be used when DHCP server is present on the network to fetch assigned gateway IP address. ![]() |
Requires |
Ethernet module has to be initialized. See Ethernet_Init. |
Example |
var gwIpAddr : array[4] of byte; // user gateway IP address buffer ... memcpy(gwIpAddr, Ethernet_getGwIpAddress(), 4); // fetch gateway IP address |
Ethernet_getDnsIpAddress
Prototype |
function Ethernet_getDnsIpAddress() : word;
|
---|---|
Returns | Pointer to the global variable holding DNS IP address. |
Description |
This routine should be used when DHCP server is present on the network to fetch assigned DNS IP address. ![]() |
Requires |
Ethernet module has to be initialized. See Ethernet_Init. |
Example |
var dnsIpAddr : array[4] of byte; // user DNS IP address buffer ... memcpy(dnsIpAddr, Ethernet_getDnsIpAddress(), 4); // fetch DNS server address |
Ethernet_getIpMask
Prototype |
function Ethernet_getIpMask() : word;
|
---|---|
Returns | Pointer to the global variable holding IP subnet mask. |
Description |
This routine should be used when DHCP server is present on the network to fetch assigned IP subnet mask. ![]() |
Requires |
Ethernet module has to be initialized. See Ethernet_Init. |
Example |
var IpMask : array[4] of byte; // user IP subnet mask buffer ... memcpy(IpMask, Ethernet_getIpMask(), 4); // fetch IP subnet mask |
Ethernet_confNetwork
Prototype |
procedure Ethernet_confNetwork(var ipMask, gwIpAddr, dnsIpAddr : array[4] of byte);
|
---|---|
Returns | Nothing. |
Description |
Configures network parameters (IP subnet mask, gateway IP address, DNS IP address) when DHCP is not used. Parameters:
![]() |
Requires |
Ethernet module has to be initialized. See Ethernet_Init. |
Example |
var ipMask : array[4] of byte; // network mask (for example : 255.255.255.0) gwIpAddr : array[4] of byte; // gateway (router) IP address dnsIpAddr : array[4] of byte; // DNS server IP address ... gwIpAddr[0] := 192; gwIpAddr[1] := 168; gwIpAddr[2] := 20; gwIpAddr[3] := 6; dnsIpAddr[0] := 192; dnsIpAddr[1] := 168; dnsIpAddr[2] := 20; dnsIpAddr[3] := 100; ipMask[0] := 255; ipMask[1] := 255; ipMask[2] := 255; ipMask[3] := 0; ... Ethernet_confNetwork(ipMask, gwIpAddr, dnsIpAddr); // set network configuration parameters |
Ethernet_arpResolve
Prototype |
function Ethernet_arpResolve(var ip : array[4] of byte; tmax : byte) : word; |
---|---|
Returns |
|
Description |
This is ARP module routine. It sends an ARP request for given IP address and waits for ARP reply.
If the requested IP address was resolved, an ARP cash entry is used for storing the configuration.
ARP cash can store up to 3 entries. For ARP cash structure refer to Parameters:
![]() |
Requires |
Ethernet module has to be initialized. See Ethernet_Init. |
Example |
var IpAddr : array[4] of byte; // IP address ... IpAddr[0] := 192; IpAddr[0] := 168; IpAddr[0] := 1; IpAddr[0] := 1; ... Ethernet_arpResolve(IpAddr, 5); // get MAC address behind the above IP address, wait 5 secs for the response |
Ethernet_sendUDP
Prototype |
function Ethernet_sendUDP(var destIP : array[4] of byte; sourcePort, destPort : word; pkt : ^byte; pktLen : word) : byte; |
---|---|
Returns |
|
Description |
This is UDP module routine. It sends an UDP packet on the network. Parameters:
|
Requires |
Ethernet module has to be initialized. See Ethernet_Init. |
Example |
var IpAddr : array[4] of byte; // remote IP address ... IpAddr[0] := 192; IpAddr[0] := 168; IpAddr[0] := 1; IpAddr[0] := 1; ... Ethernet_sendUDP(IpAddr, 10001, 10001, 'Hello', 5); // send Hello message to the above IP address, from UDP port 10001 to UDP port 10001 |
Ethernet_dnsResolve
Prototype |
function Ethernet_dnsResolve(var host : array[4] of byte; tmax : byte) : word; |
---|---|
Returns |
|
Description |
This is DNS module routine. It sends an DNS request for given host name and waits for DNS reply.
If the requested host name was resolved, it's IP address is stored in library global variable
and a pointer containing this address is returned by the routine. UDP port Parameters:
The above mentioned network parameters should be set by this routine only if DHCP module is not used. Otherwise DHCP will override these settings. ![]()
|
Requires |
Ethernet module has to be initialized. See Ethernet_Init. |
Example |
var remoteHostIpAddr : array[4] of byte; // user host IP address buffer ... // SNTP server: // Zurich, Switzerland: Integrated Systems Lab, Swiss Fed. Inst. of Technology // 129.132.2.21: swisstime.ethz.ch // Service Area: Switzerland and Europe memcpy(remoteHostIpAddr, Ethernet_dnsResolve('swisstime.ethz.ch', 5), 4); |
Ethernet_initDHCP
Prototype |
function Ethernet_initDHCP(tmax : byte) : byte; |
---|---|
Returns |
|
Description |
This is DHCP module routine. It sends an DHCP request for network parameters (IP, gateway, DNS addresses and IP subnet mask) and waits for DHCP reply. If the requested parameters were obtained successfully, their values are stored into the library global variables. These parameters can be fetched by using appropriate library IP get routines:
UDP port Parameters:
![]()
|
Requires |
Ethernet module has to be initialized. See Ethernet_Init. |
Example |
... Ethernet_initDHCP(5); // get network configuration from DHCP server, wait 5 sec for the response ... |
Ethernet_doDHCPLeaseTime
Prototype |
function Ethernet_doDHCPLeaseTime() : byte; |
---|---|
Returns |
|
Description |
This is DHCP module routine. It takes care of IP address lease time by decrementing the global lease time library counter. When this time expires, it's time to contact DHCP server and renew the lease. |
Requires |
Ethernet module has to be initialized. See Ethernet_Init. |
Example |
while true do begin ... if(Ethernet_doDHCPLeaseTime() <> 0) then begin ... // it's time to renew the IP address lease end; end; |
Ethernet_renewDHCP
Prototype |
function Ethernet_renewDHCP(tmax : byte) : byte; |
---|---|
Returns |
|
Description |
This is DHCP module routine. It sends IP address lease time renewal request to DHCP server. Parameters:
|
Requires |
Ethernet module has to be initialized. See Ethernet_Init. |
Example |
while true do begin ... if(Ethernet_doDHCPLeaseTime() <> 0) then begin Ethernet_renewDHCP(5); // it's time to renew the IP address lease, with 5 secs for a reply end; ... end; |
What do you think about this topic ? Send us feedback!