Ethernet Library
The internal Ethernet Controller consists of a fully integrated media access controller (MAC) and network physical (PHY) interface (Stellaris M3 family only). The Ethernet Controller conforms to IEEE 802.3 specifications and fully supports 10BASE-T and 100BASE-TX standards.
The internal Ethernet library supports :
- IPv4 protocol.
- ARP requests.
- ICMP echo requests.
- UDP requests.
- TCP requests (no stack, no packet reconstruction).
- ARP client with cache.
- DNS client.
- UDP client.
- DHCP client.
- packet fragmentation is NOT supported.

- Global library variable
Ethernet_Intern_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 are
__Ethernet_Intern_Private.h
and__Ethernet_Intern.h
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_Intern_Init
- Ethernet_Intern_Enable
- Ethernet_Intern_Disable
- Ethernet_Intern_confFilter
- Ethernet_Intern_doPacket
- Ethernet_Intern_writePayloadByte
- Ethernet_Intern_writePayloadBytes
- Ethernet_Intern_writePayloadString
- Ethernet_Intern_readPayloadByte
- Ethernet_Intern_readPayloadBytes
- Ethernet_Intern_putByte
- Ethernet_Intern_putBytes
- Ethernet_Intern_putString
- Ethernet_Intern_putConstString
- Ethernet_Intern_putConstBytes
- Ethernet_Intern_getByte
- Ethernet_Intern_getBytes
- Ethernet_Intern_UserTCP
- Ethernet_Intern_UserUDP
- Ethernet_Intern_getIpAddress
- Ethernet_Intern_getGwIpAddress
- Ethernet_Intern_getDnsIpAddress
- Ethernet_Intern_getIpMask
- Ethernet_Intern_confNetwork
- Ethernet_Intern_arpResolve
- Ethernet_Intern_sendUDP
- Ethernet_Intern_dnsResolve
- Ethernet_Intern_initDHCP
- Ethernet_Intern_doDHCPLeaseTime
- Ethernet_Intern_renewDHCP
Ethernet_Intern_Init
Prototype |
// for Stellaris MCUs
void Ethernet_Intern_Init(unsigned char *mac, unsigned char *ip, unsigned char config); // for ST MCUsunsigned char Ethernet_Intern_Init(unsigned char *mac, unsigned char *ip, unsigned char config, const Module_Struct *module); // for Kinetis MCUsunsigned char Ethernet_Intern_Init(unsigned char *mac, unsigned char *ip, const Module_Struct *module); |
---|---|
Returns |
|
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 |
#define Ethernet_Intern_HALFDUPLEX 0 #define Ethernet_Intern_FULLDUPLEX 1 unsigned char myMacAddr[6] = {0x00, 0x14, 0xA5, 0x76, 0x19, 0x3f}; // my MAC address unsigned char myIpAddr[4] = {192, 168, 1, 60 }; // my IP addr Ethernet_Intern_Init(myMacAddr, myIpAddr, _ETHERNET_AUTO_NEGOTIATION); |
Ethernet_Intern_Enable
Prototype |
void Ethernet_Intern_Enable(unsigned char enFlt); |
||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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_Intern_Init. |
||||||||||||||||||||||||||||||||||||
Example |
Ethernet_Intern_Enable(_Ethernet_Intern_CRC | _Ethernet_Intern_MULTICAST); // enable CRC checking and Multicast traffic |
Ethernet_Intern_Disable
Prototype |
void Ethernet_Intern_Disable(unsigned char disFlt); |
||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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_Intern_Init. |
||||||||||||||||||||||||||||||||||||
Example |
Ethernet_Intern_Disable(_Ethernet_Intern_CRC | _Ethernet_Intern_MULTICAST); // disable CRC checking and Multicast traffic |
Ethernet_Intern_confFilter
Prototype |
void Ethernet_Intern_confFilter(unsigned long cfg) |
---|---|
Returns |
Nothing. |
Description |
This routine enables/disables appropriate network traffic on the MCU's internal Ethernet module by the means of it's receive filters (unicast, multicast, broadcast, crc).
Parameters:
![]() |
Requires |
Ethernet module has to be initialized. See Ethernet_Intern_Init. |
Example |
Ethernet_Intern_confFilter(_ETHERNET_FILTER_RECEIVE_ALL_EN | _ETHERNET_FILTER_MULTICAST_RECEIVE_ALL); // enable CRC checking and Multicast traffic |
Ethernet_Intern_doPacket
Prototype |
unsigned char Ethernet_Intern_doPacket(); |
---|---|
Returns |
|
Description |
This is MAC module routine. It processes next received packet if such exists. Packets are processed in the following manner:
![]() Ethernet_Intern_doPacket must be called as often as possible in user's code.
|
Requires |
Ethernet module has to be initialized. See Ethernet_Intern_Init. |
Example |
if (Ethernet_Intern_doPacket() == 0) { // process received packets ... } |
Ethernet_Intern_writePayloadByte
Prototype |
void Ethernet_Intern_writePayloadByte(char dat, unsigned int offset); |
---|---|
Returns |
Nothing. |
Description |
It writes one byte in the TCP payload. Parameters:
This routine is called either from the Ethernet_Intern_UserTCP or Ethernet_Intern_UserUDP. This routine is valid only for Stellaris devices. |
Requires |
Ethernet module has to be initialized. See Ethernet_Intern_Init. |
Example |
char data_; ... Ethernet_Intern_writePayloadByte(data_,0); // put a byte at the beginning of the TCP payload |
Ethernet_Intern_writePayloadBytes
Prototype |
void Ethernet_Intern_writePayloadBytes(char *dat, unsigned int offset, unsigned int numOfBytes); |
---|---|
Returns |
Nothing. |
Description |
It writes a requested number of bytes into TCP payload. Parameters:
This routine is called either from the Ethernet_Intern_UserTCP or Ethernet_Intern_UserUDP. This routine is valid only for Stellaris devices. |
Requires |
Ethernet module has to be initialized. See Ethernet_Intern_Init. |
Example |
char *buffer = "mikroElektronika"; ... Ethernet_Intern_writePayloadBytes(buffer, 0, 16); // put an RAM array into TCP payload |
Ethernet_Intern_writePayloadString
Prototype |
unsigned int Ethernet_Intern_writePayloadString(char *dat, unsigned int offset); |
---|---|
Returns |
Number of bytes written into the TCP payload. |
Description |
This is MAC module routine. It stores whole string (excluding null termination) into RAM at current write location. Parameters:
This routine is called either from the Ethernet_Intern_UserTCP or Ethernet_Intern_UserUDP. This routine is valid only for Stellaris devices. |
Requires |
Ethernet module has to be initialized. See Ethernet_Intern_Init. |
Example |
char *buffer = "mikroElektronika"; ... Ethernet_Intern_writePayloadString(buffer, 0); // put a RAM string into TCP payload |
Ethernet_Intern_readPayloadByte
Prototype |
unsigned char Ethernet_Intern_readPayloadByte(unsigned int offset); |
---|---|
Returns |
Byte read from TCP payload. |
Description |
It fetches a byte from the TCP payload, from the position detemined by the Parameters:
This routine is called either from the Ethernet_Intern_UserTCP or Ethernet_Intern_UserUDP. This routine is valid only for Stellaris devices. |
Requires |
Ethernet module has to be initialized. See Ethernet_Intern_Init. |
Example |
char buffer; ... buffer = Ethernet_Intern_readPayloadByte(); // read a byte from TCP payload |
Ethernet_Intern_readPayloadBytes
Prototype |
void Ethernet_Intern_readPayloadBytes(char *dat, unsigned int offset, unsigned int numOfBytes); |
---|---|
Returns |
Nothing. |
Description |
It fetches requested number of bytes from the TCP payload. Parameters:
This routine is called either from the Ethernet_Intern_UserTCP or Ethernet_Intern_UserUDP. This routine is valid only for Stellaris devices. |
Requires |
Ethernet module has to be initialized. See Ethernet_Intern_Init. |
Example |
char buffer[16]; ... Ethernet_Intern_readPayloadBytes(buffer, 0, 16); // read 16 bytes, starting from the beginning of the TCP payload |
Ethernet_Intern_putByte
Prototype |
void Ethernet_Intern_putByte(unsigned char v); |
---|---|
Returns |
Nothing. |
Description |
This is MAC module routine. It stores one byte to address pointed by the current Ethernet controller's write pointer. Parameters:
This routine is valid for ST and Kinetis devices. |
Requires |
Ethernet module has to be initialized. See Ethernet_Intern_Init. |
Example |
char data_; ... Ethernet_Intern_putByte(data_); // put an byte into Ethernet controller's buffer |
Ethernet_Intern_putBytes
Prototype |
void Ethernet_Intern_putBytes(unsigned char *ptr, unsigned int n); |
---|---|
Returns |
Nothing. |
Description |
This is MAC module routine. It stores requested number of bytes into Ethernet controller's RAM starting from current Ethernet controller's write pointer location. Parameters:
This routine is valid for ST and Kinetis devices. |
Requires |
Ethernet module has to be initialized. See Ethernet_Intern_Init. |
Example |
char *buffer = "mikroElektronika"; ... Ethernet_Intern_putBytes(buffer, 16); // put an RAM array into Ethernet controller's buffer |
Ethernet_Intern_putConstBytes
Prototype |
void Ethernet_Intern_putConstBytes(const unsigned char *ptr, unsigned int n); |
---|---|
Returns |
Nothing. |
Description |
This is MAC module routine. It stores requested number of const bytes into Ethernet controller's RAM starting from current Ethernet controller's write pointer location. Parameters:
This routine is valid for ST and Kinetis devices. |
Requires |
Ethernet module has to be initialized. See Ethernet_Intern_Init. |
Example |
const char *buffer = "mikroElektronika"; ... Ethernet_Intern_putConstBytes(buffer, 16); // put a const array into Ethernet controller's buffer |
Ethernet_Intern_putString
Prototype |
unsigned int Ethernet_Intern_putString(unsigned char *ptr); |
---|---|
Returns |
Number of bytes written into Ethernet controller's RAM. |
Description |
This is MAC module routine. It stores whole string (excluding null termination) into Ethernet controller's RAM starting from current Ethernet controller's write pointer location. Parameters:
This routine is valid for ST and Kinetis devices. |
Requires |
Ethernet module has to be initialized. See Ethernet_Intern_Init. |
Example |
char *buffer = "mikroElektronika"; ... Ethernet_Intern_putString(buffer); // put a RAM string into Ethernet controller's buffer |
Ethernet_Intern_putConstString
Prototype |
unsigned int Ethernet_Intern_putConstString(const unsigned char *ptr); |
---|---|
Returns |
Number of bytes written into Ethernet controller's RAM. |
Description |
This is MAC module routine. It stores whole const string (excluding null termination) into Ethernet controller's RAM starting from current Ethernet controller's write pointer location. Parameters:
This routine is valid for ST and Kinetis devices. |
Requires |
Ethernet module has to be initialized. See Ethernet_Intern_Init. |
Example |
const char *buffer = "mikroElektronika"; ... Ethernet_Intern_putConstString(buffer); // put a const string into Ethernet controller's buffer |
Ethernet_Intern_getByte
Prototype |
unsigned char Ethernet_Intern_getByte(); |
---|---|
Returns |
Byte read from Ethernet controller's RAM. |
Description |
This is MAC module routine. It fetches a byte from address pointed to by current Ethernet controller's read pointer. This routine is valid for ST and Kinetis devices. |
Requires |
Ethernet module has to be initialized. See Ethernet_Intern_Init. |
Example |
char buffer; ... buffer = Ethernet_Intern_getByte(); // read a byte from Ethernet controller's buffer |
Ethernet_Intern_getBytes
Prototype |
void Ethernet_Intern_getBytes(unsigned char *ptr, unsigned int addr, unsigned int n); |
---|---|
Returns |
Nothing. |
Description |
This is MAC module routine. It fetches equested number of bytes from Ethernet controller's RAM starting from given address. If value of Parameters:
This routine is valid for ST and Kinetis devices. |
Requires |
Ethernet module has to be initialized. See Ethernet_Intern_Init. |
Example |
char buffer[16]; ... Ethernet_Intern_getBytes(buffer, 0x100, 16); // read 16 bytes, starting from address 0x100 |
Ethernet_Intern_UserTCP
Prototype |
unsigned int Ethernet_Intern_UserTCP(unsigned char *remoteHost, unsigned int remotePort, unsigned int localPort, unsigned int reqLength, TEthInternPktFlags *flags); |
---|---|
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_Intern_get routines. The user puts data in the transmit buffer by using some of the Ethernet_Intern_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_Intern_Init. |
Example |
This function is internally called by the library and should not be called by the user's code. |
Ethernet_Intern_UserUDP
Prototype |
unsigned int Ethernet_Intern_UserUDP(unsigned char *remoteHost, unsigned int remotePort, unsigned int destPort, unsigned int reqLength, TEthInternPktFlags *flags); |
---|---|
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_Intern_get routines. The user puts data in the transmit buffer by using some of the Ethernet_Intern_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_Intern_Init. |
Example |
This function is internally called by the library and should not be called by the user's code. |
Ethernet_Intern_getIpAddress
Prototype |
unsigned char * Ethernet_Intern_getIpAddress();
|
---|---|
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_Intern_Init. |
Example |
unsigned char ipAddr[4]; // user IP address buffer ... memcpy(ipAddr, Ethernet_Intern_getIpAddress(), 4); // fetch IP address |
Ethernet_Intern_getGwIpAddress
Prototype |
unsigned char * Ethernet_Intern_getGwIpAddress();
|
---|---|
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_Intern_Init. |
Example |
unsigned char gwIpAddr[4]; // user gateway IP address buffer ... memcpy(gwIpAddr, Ethernet_Intern_getGwIpAddress(), 4); // fetch gateway IP address |
Ethernet_Intern_getDnsIpAddress
Prototype |
unsigned char * Ethernet_Intern_getDnsIpAddress();
|
---|---|
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_Intern_Init. |
Example |
unsigned char dnsIpAddr[4]; // user DNS IP address buffer ... memcpy(dnsIpAddr, Ethernet_Intern_getDnsIpAddress(), 4); // fetch DNS server address |
Ethernet_Intern_getIpMask
Prototype |
unsigned char * Ethernet_Intern_getIpMask();
|
---|---|
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_Intern_Init. |
Example |
unsigned char IpMask[4]; // user IP subnet mask buffer ... memcpy(IpMask, Ethernet_Intern_getIpMask(), 4); // fetch IP subnet mask |
Ethernet_Intern_confNetwork
Prototype |
void Ethernet_Intern_confNetwork(char *ipMask, char *gwIpAddr, char *dnsIpAddr);
|
---|---|
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_Intern_Init. |
Example |
unsigned char ipMask[4] = {255, 255, 255, 0 }; // network mask (for example : 255.255.255.0) unsigned char gwIpAddr[4] = {192, 168, 1, 1 }; // gateway (router) IP address unsigned char dnsIpAddr[4] = {192, 168, 1, 1 }; // DNS server IP address ... Ethernet_Intern_confNetwork(ipMask, gwIpAddr, dnsIpAddr); // set network configuration parameters |
Ethernet_Intern_arpResolve
Prototype |
unsigned char *Ethernet_Intern_arpResolve(unsigned char *ip, unsigned char tmax); |
---|---|
Returns |
If successful, returns MAC address, NULL otherwise. |
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_Intern_Init. |
Example |
unsigned char IpAddr[4] = {192, 168, 1, 1 }; // IP address ... Ethernet_Intern_arpResolve(IpAddr, 5); // get MAC address behind the above IP address, wait 5 secs for the response |
Ethernet_Intern_sendUDP
Prototype |
unsigned int Ethernet_Intern_sendUDP(unsigned char *destIP, unsigned int sourcePort, unsigned int destPort, unsigned char *pkt, unsigned int pktLen); |
---|---|
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_Intern_Init. |
Example |
unsigned char IpAddr[4] = {192, 168, 1, 1 }; // remote IP address ... Ethernet_Intern_sendUDP(IpAddr, 10001, 10001, "Hello", 5); // send Hello message to the above IP address, from UDP port 10001 to UDP port 10001 |
Ethernet_Intern_dnsResolve
Prototype |
unsigned char *Ethernet_Intern_dnsResolve(unsigned char *host, unsigned char tmax); |
---|---|
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_Intern_Init. |
Example |
unsigned char * remoteHostIpAddr[4]; // 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_Intern_dnsResolve("swisstime.ethz.ch", 5), 4); |
Ethernet_Intern_initDHCP
Prototype |
unsigned char Ethernet_Intern_initDHCP(unsigned char tmax); |
---|---|
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_Intern_Init. |
Example |
... Ethernet_Intern_initDHCP(5); // get network configuration from DHCP server, wait 5 sec for the response ... |
Ethernet_Intern_doDHCPLeaseTime
Prototype |
unsigned int Ethernet_Intern_doDHCPLeaseTime(); |
---|---|
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_Intern_Init. |
Example |
while(1) { ... if(Ethernet_Intern_doDHCPLeaseTime()) ... // it's time to renew the IP address lease } |
Ethernet_Intern_renewDHCP
Prototype |
unsigned int Ethernet_Intern_renewDHCP(unsigned char tmax); |
---|---|
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_Intern_Init. |
Example |
while(1) { ... if(Ethernet_Intern_doDHCPLeaseTime()) Ethernet_Intern_renewDHCP(5); // it's time to renew the IP address lease, with 5 secs for a reply ... } |
What do you think about this topic ? Send us feedback!