Ethernet PIC18FxxJ60 Library
PIC18FxxJ60
family of microcontrollers feature an embedded Ethernet controller module.
This is a complete connectivity solution, including full implementations of both
Media Access Control (MAC) and Physical Layer transceiver (PHY) modules. Two pulse
transformers and a few passive components are all that are required to connect the
microcontroller directly to an Ethernet network.
The Ethernet module meets all of the IEEE 802.3 specifications for 10-BaseT connectivity to a twisted-pair network. It incorporates a number of packet filtering schemes to limit incoming packets. It also provides an internal DMA module for fast data throughput and hardware assisted IP checksum calculations. Provisions are also made for two LED outputs to indicate link and network activity
This library provides the posibility to easily utilize ethernet feature of the above mentioned MCUs.
Ethernet PIC18FxxJ60 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_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 header files (
"eth_j60LibDef.h"
and"eth_j60LibPrivate.h"
) in Uses\P18 folder of the compiler with description of all routines and global variables, relevant to the user, implemented in the Ethernet PIC18FxxJ60 Library.
Library Routines
- Ethernet_Init
- 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 |
void Ethernet_Init(unsigned char *mac, unsigned char *ip, unsigned char fullDuplex); |
---|---|
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:
Note :
If a DHCP server is to be used, IP address should be set to
0.0.0.0 .
|
Requires | Nothing. |
Example |
#define Ethernet_HALFDUPLEX 0 #define Ethernet_FULLDUPLEX 1 unsigned char myMacAddr[6] = {0x00, 0x14, 0xA5, 0x76, 0x19, 0x3f}; // my MAC address unsigned char myIpAddr = {192, 168, 1, 60 }; // my IP addr Ethernet_Init(myMacAddr, myIpAddr, Ethernet_FULLDUPLEX); |
Ethernet_Enable
Prototype |
void Ethernet_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:
Note :
|
||||||||||||||||||||||||||||||||||||
Requires |
Ethernet module has to be initialized. See Ethernet_Init. |
||||||||||||||||||||||||||||||||||||
Example |
Ethernet_Enable(_Ethernet_CRC | _Ethernet_UNICAST); // enable CRC checking and Unicast traffic |
Ethernet_Disable
Prototype |
void Ethernet_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:
Note :
|
||||||||||||||||||||||||||||||||||||
Requires |
Ethernet module has to be initialized. See Ethernet_Init. |
||||||||||||||||||||||||||||||||||||
Example |
Ethernet_Disable(_Ethernet_CRC | _Ethernet_UNICAST); // disable CRC checking and Unicast traffic |
Ethernet_doPacket
Prototype |
unsigned char Ethernet_doPacket(); |
---|---|
Returns |
|
Description |
This is MAC module routine. It processes next received packet if such exists. Packets are processed in the following manner:
Note :
Ethernet_doPacket must be called as often as possible in user's code.
|
Requires |
Ethernet module has to be initialized. See Ethernet_Init. |
Example |
if (Ethernet_doPacket() == 0) { // process received packets ... } |
Ethernet_putByte
Prototype |
void Ethernet_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:
|
Requires |
Ethernet module has to be initialized. See Ethernet_Init. |
Example |
char data_; ... Ethernet_putByte(data_); // put an byte into Ethernet controller's buffer |
Ethernet_putBytes
Prototype |
void Ethernet_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 ( Parameters:
|
Requires |
Ethernet module has to be initialized. See Ethernet_Init. |
Example |
char *buffer = "mikroElektronika"; ... Ethernet_putBytes(buffer, 16); // put an RAM array into Ethernet controller's buffer |
Ethernet_putConstBytes
Prototype |
void Ethernet_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 ( Parameters:
|
Requires |
Ethernet module has to be initialized. See Ethernet_Init. |
Example |
const char *buffer = "mikroElektronika"; ... Ethernet_putConstBytes(buffer, 16); // put a const array into Ethernet controller's buffer |
Ethernet_putString
Prototype |
unsigned int Ethernet_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 ( Parameters:
|
Requires |
Ethernet module has to be initialized. See Ethernet_Init. |
Example |
char *buffer = "mikroElektronika"; ... Ethernet_putString(buffer); // put a RAM string into Ethernet controller's buffer |
Ethernet_putConstString
Prototype |
unsigned int Ethernet_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 ( Parameters:
|
Requires |
Ethernet module has to be initialized. See Ethernet_Init. |
Example |
const char *buffer = "mikroElektronika"; ... Ethernet_putConstString(buffer); // put a const string into Ethernet controller's buffer |
Ethernet_getByte
Prototype |
unsigned char Ethernet_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 ( |
Requires |
Ethernet module has to be initialized. See Ethernet_Init. |
Example |
char buffer; ... buffer = Ethernet_getByte(); // read a byte from Ethernet controller's buffer |
Ethernet_getBytes
Prototype |
void Ethernet_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:
|
Requires |
Ethernet module has to be initialized. See Ethernet_Init. |
Example |
char buffer[16]; ... Ethernet_getBytes(buffer, 0x100, 16); // read 16 bytes, starting from address 0x100 |
Ethernet_UserTCP
Prototype |
unsigned int Ethernet_UserTCP(unsigned char *remoteHost, unsigned int remotePort, unsigned int localPort, unsigned int reqLength, TEthPktFlags *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_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:
Note :
The function source code is provided with appropriate example projects. The code should be adjusted by the user to achieve desired reply.
|
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 |
unsigned int Ethernet_UserUDP(unsigned char *remoteHost, unsigned int remotePort, unsigned int destPort, unsigned int reqLength, TEthPktFlags *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_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 |
unsigned char * Ethernet_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.
Note :
User should always copy the IP address from the RAM location returned by this routine into it's own IP address buffer. These locations should not be altered by the user in any case!
|
Requires |
Ethernet module has to be initialized. See Ethernet_Init. |
Example |
unsigned char ipAddr[4]; // user IP address buffer ... memcpy(ipAddr, Ethernet_getIpAddress(), 4); // fetch IP address |
Ethernet_getGwIpAddress
Prototype |
unsigned char * Ethernet_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.
Note :
User should always copy the IP address from the RAM location returned by this routine into it's own gateway IP address buffer. These locations should not be altered by the user in any case!
|
Requires |
Ethernet module has to be initialized. See Ethernet_Init. |
Example |
unsigned char gwIpAddr[4]; // user gateway IP address buffer ... memcpy(gwIpAddr, Ethernet_getGwIpAddress(), 4); // fetch gateway IP address |
Ethernet_getDnsIpAddress();
Prototype |
unsigned char * Ethernet_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.
Note :
User should always copy the IP address from the RAM location returned by this routine into it's own DNS IP address buffer. These locations should not be altered by the user in any case!
|
Requires |
Ethernet module has to be initialized. See Ethernet_Init. |
Example |
unsigned char dnsIpAddr[4]; // user DNS IP address buffer ... memcpy(dnsIpAddr, Ethernet_getDnsIpAddress(), 4); // fetch DNS server address |
Ethernet_getIpMask
Prototype |
unsigned char * Ethernet_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.
Note :
User should always copy the IP address from the RAM location returned by this routine into it's own IP subnet mask buffer. These locations should not be altered by the user in any case!
|
Requires |
Ethernet module has to be initialized. See Ethernet_Init. |
Example |
unsigned char IpMask[4]; // user IP subnet mask buffer ... memcpy(IpMask, Ethernet_getIpMask(), 4); // fetch IP subnet mask |
Ethernet_confNetwork
Prototype |
void Ethernet_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:
Note :
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 |
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_confNetwork(ipMask, gwIpAddr, dnsIpAddr); // set network configuration parameters |
Ethernet_arpResolve
Prototype |
unsigned char *Ethernet_arpResolve(unsigned char *ip, unsigned char tmax); |
---|---|
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:
Note :
The Ethernet services are not stopped while this routine waits for ARP reply. The incoming packets will be processed normaly during this time.
|
Requires |
Ethernet module has to be initialized. See Ethernet_Init. |
Example |
unsigned char IpAddr[4] = {192, 168, 1, 1 }; // IP address ... Ethernet_arpResolve(IpAddr, 5); // get MAC address behind the above IP address, wait 5 secs for the response |
Ethernet_sendUDP
Prototype |
unsigned char Ethernet_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_Init. |
Example |
unsigned char IpAddr[4] = {192, 168, 1, 1 }; // remote IP address ... 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 |
unsigned char *Ethernet_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.
Note :
|
Requires |
Ethernet module has to be initialized. See Ethernet_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_dnsResolve("swisstime.ethz.ch", 5), 4); |
Ethernet_initDHCP
Prototype |
unsigned char Ethernet_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 successfuly, their values are stored into the library global variables. These parameters can be fetched by using appropriate library IP get routines:
UDP port Parameters:
Note :
|
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 |
unsigned char Ethernet_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_Init. |
Example |
while(1) { ... if(Ethernet_doDHCPLeaseTime()) ... // it's time to renew the IP address lease } |
Ethernet_renewDHCP
Prototype |
unsigned char Ethernet_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_Init. |
Example |
while(1) { ... if(Ethernet_doDHCPLeaseTime()) Ethernet_renewDHCP(5); // it's time to renew the IP address lease, with 5 secs for a reply ... } |
Library Example
This code shows how to use the PIC18FxxJ60 Ethernet library :
- the board will reply to ARP & ICMP echo requests
- the board will reply to UDP requests on any port :
- returns the request in upper char with a header made of remote host IP & port number
- the board will reply to HTTP requests on port 80, GET method with pathnames :
- / will return the HTML main page
- /s will return board status as text string
- /t0 ... /t7 will toggle RD0 to RD7 bit and return HTML main page
- all other requests return also HTML main page.
#include "__EthJ60.h" #define Ethernet_HALFDUPLEX 0 #define Ethernet_FULLDUPLEX 1 /************************************************************ * ROM constant strings */ const unsigned char httpHeader[] = "HTTP/1.1 200 OKnContent-type: " ; // HTTP header const unsigned char httpMimeTypeHTML[] = "text/htmlnn" ; // HTML MIME type const unsigned char httpMimeTypeScript[] = "text/plainnn" ; // TEXT MIME type unsigned char httpMethod[] = "GET /"; /* * web page, splited into 2 parts : * when coming short of ROM, fragmented data is handled more efficiently by linker * * this HTML page calls the boards to get its status, and builds itself with javascript */ const char *indexPage = // Change the IP address of the page to be refreshed "<meta http-equiv="refresh" content="3;url=http://192.168.20.60"> <HTML><HEAD></HEAD><BODY> <h1>PIC18FxxJ60 Mini Web Server</h1> <a href=/>Reload</a> <script src=/s></script> <table><tr><td valign=top><table border=1 style="font-size:20px ;font-family: terminal ;"> <tr><th colspan=2>ADC</th></tr> <tr><td>AN2</td><td><script>document.write(AN2)</script></td></tr> <tr><td>AN3</td><td><script>document.write(AN3)</script></td></tr> </table></td><td><table border=1 style="font-size:20px ;font-family: terminal ;"> <tr><th colspan=2>PORTB</th></tr> <script> var str,i; str=""; for(i=0;i<8;i++) {str+="<tr><td bgcolor=pink>BUTTON #"+i+"</td>"; if(PORTB&(1<<i)){str+="<td bgcolor=red>ON";} else {str+="<td bgcolor=#cccccc>OFF";} str+="</td></tr>";} document.write(str) ; </script> " ; const char *indexPage2 = "</table></td><td> <table border=1 style="font-size:20px ;font-family: terminal ;"> <tr><th colspan=3>PORTD</th></tr> <script> var str,i; str=""; for(i=0;i<3;i++) {str+="<tr><td bgcolor=yellow>LED #"+i+"</td>"; if(PORTD&(1<<i)){str+="<td bgcolor=red>ON";} else {str+="<td bgcolor=#cccccc>OFF";} str+="</td><td><a href=/t"+i+">Toggle</a></td></tr>";} document.write(str) ; </script> </table></td></tr></table> This is HTTP request #<script>document.write(REQ)</script></BODY></HTML> " ; /*********************************** * RAM variables */ unsigned char myMacAddr[6] = {0x00, 0x14, 0xA5, 0x76, 0x19, 0x3f} ; // my MAC address unsigned char myIpAddr[4] = {192, 168, 20, 60 } ; // my IP address unsigned char gwIpAddr[4] = {192, 168, 20, 6 } ; // gateway (router) IP address unsigned char ipMask[4] = {255, 255, 255, 0 } ; // network mask (for example : 255.255.255.0) unsigned char dnsIpAddr[4] = {192, 168, 20, 1 } ; // DNS server IP address unsigned char getRequest[15] ; // HTTP request buffer unsigned char dyna[30] ; // buffer for dynamic response unsigned long httpCounter = 0 ; // counter of HTTP requests /******************************************* * functions */ /* * put the constant string pointed to by s to the Ethernet controller's transmit buffer. */ /*unsigned int putConstString(const char *s) { unsigned int ctr = 0 ; while(*s) { Ethernet_putByte(*s++) ; ctr++ ; } return(ctr) ; }*/ /* * it will be much faster to use library Ethernet_putConstString routine * instead of putConstString routine above. However, the code will be a little * bit bigger. User should choose between size and speed and pick the implementation that * suites him best. If you choose to go with the putConstString definition above * the #define line below should be commented out. * */ #define putConstString Ethernet_putConstString /* * put the string pointed to by s to the Ethernet controller's transmit buffer */ /*unsigned int putString(char *s) { unsigned int ctr = 0 ; while(*s) { Ethernet_putByte(*s++) ; ctr++ ; } return(ctr) ; }*/ /* * it will be much faster to use library Ethernet_putString routine * instead of putString routine above. However, the code will be a little * bit bigger. User should choose between size and speed and pick the implementation that * suites him best. If you choose to go with the putString definition above * the #define line below should be commented out. * */ #define putString Ethernet_putString /* * this function is called by the library * the user accesses to the HTTP request by successive calls to Ethernet_getByte() * the user puts data in the transmit buffer by successive calls to Ethernet_putByte() * the function must return the length in bytes of the HTTP reply, or 0 if nothing to transmit * * if you don't need to reply to HTTP requests, * just define this function with a return(0) as single statement * */ unsigned int Ethernet_UserTCP(unsigned char *remoteHost, unsigned int remotePort, unsigned int localPort, unsigned int reqLength, TEthPktFlags *flags) { unsigned int len = 0 ; // my reply length unsigned char i ; // general purpose char // should we close tcp socket after response is sent? // library closes tcp socket by default if canClose flag is not reset here // flags->canCloseTCP = 0; // 0 - do not close socket // otherwise - close socket if(localPort != 80) // I listen only to web request on port 80 { return(0) ; } // get 10 first bytes only of the request, the rest does not matter here for(i = 0 ; i < 10 ; i++) { getRequest[i] = Ethernet_getByte() ; } getRequest[10] = 0 ; if(memcmp(getRequest, httpMethod, 5)) // only GET method is supported here { return(0) ; } httpCounter++ ; // one more request done if(getRequest[5] == 's') // if request path name starts with s, store dynamic data in transmit buffer { // the text string replied by this request can be interpreted as javascript statements // by browsers len = putConstString(httpHeader) ; // HTTP header len += putConstString(httpMimeTypeScript) ; // with text MIME type // add AN2 value to reply IntToStr(ADC_Read(2), dyna) ; len += putConstString("var AN2=") ; len += putString(dyna) ; len += putConstString(";") ; // add AN3 value to reply IntToStr(ADC_Read(3), dyna) ; len += putConstString("var AN3=") ; len += putString(dyna) ; len += putConstString(";") ; // add PORTB value (buttons) to reply len += putConstString("var PORTB=") ; IntToStr(PORTB, dyna) ; len += putString(dyna) ; len += putConstString(";") ; // add PORTD value (LEDs) to reply len += putConstString("var PORTD=") ; IntToStr(PORTD, dyna) ; len += putString(dyna) ; len += putConstString(";") ; // add HTTP requests counter to reply IntToStr(httpCounter, dyna) ; len += putConstString("var REQ=") ; len += putString(dyna) ; len += putConstString(";") ; } else if(getRequest[5] == 't') // if request path name starts with t, toggle PORTD (LED) bit number that comes after { unsigned char bitMask = 0 ; // for bit mask if(isdigit(getRequest[6])) // if 0 <= bit number <= 9, bits 8 & 9 does not exist but does not matter { bitMask = getRequest[6] - '0' ; // convert ASCII to integer bitMask = 1 << bitMask ; // create bit mask PORTD ^= bitMask ; // toggle PORTD with xor operator } } if(len == 0) // what do to by default { len = putConstString(httpHeader) ; // HTTP header len += putConstString(httpMimeTypeHTML) ; // with HTML MIME type len += putConstString(indexPage) ; // HTML page first part len += putConstString(indexPage2) ; // HTML page second part } return(len) ; // return to the library with the number of bytes to transmit } /* * this function is called by the library * the user accesses to the UDP request by successive calls to Ethernet_getByte() * the user puts data in the transmit buffer by successive calls to Ethernet_putByte() * 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 UDP requests, * just define this function with a return(0) as single statement * */ unsigned int Ethernet_UserUDP(unsigned char *remoteHost, unsigned int remotePort, unsigned int destPort, unsigned int reqLength, TEthPktFlags *flags) { unsigned int len ; // my reply length // reply is made of the remote host IP address in human readable format ByteToStr(remoteHost[0], dyna) ; // first IP address byte dyna[3] = '.' ; ByteToStr(remoteHost[1], dyna + 4) ; // second dyna[7] = '.' ; ByteToStr(remoteHost[2], dyna + 8) ; // third dyna[11] = '.' ; ByteToStr(remoteHost[3], dyna + 12) ; // fourth dyna[15] = ':' ; // add separator // then remote host port number WordToStr(remotePort, dyna + 16) ; dyna[21] = '[' ; WordToStr(destPort, dyna + 22) ; dyna[27] = ']' ; dyna[28] = 0 ; // the total length of the request is the length of the dynamic string plus the text of the request len = 28 + reqLength; // puts the dynamic string into the transmit buffer Ethernet_putBytes(dyna, 28) ; // then puts the request string converted into upper char into the transmit buffer while(reqLength--) { Ethernet_putByte(toupper(Ethernet_getByte())) ; } return(len) ; // back to the library with the length of the UDP reply } /* * main entry */ void main() { ADCON1 = 0x0B ; // ADC convertors will be used with AN2 and AN3 CMCON = 0x07 ; // turn off comparators PORTA = 0 ; TRISA = 0xfc ; // set PORTA as input for ADC // except RA0 and RA1 which will be used as // ethernet's LEDA and LEDB PORTB = 0 ; TRISB = 0xff ; // set PORTB as input for buttons PORTD = 0 ; TRISD = 0 ; // set PORTD as output /* * Initialize Ethernet controller */ Ethernet_Init(myMacAddr, myIpAddr, Ethernet_FULLDUPLEX) ; // dhcp will not be used here, so use preconfigured addresses Ethernet_confNetwork(ipMask, gwIpAddr, dnsIpAddr) ; while(1) // do forever { /* * if necessary, test the return value to get error code */ Ethernet_doPacket() ; // process incoming Ethernet packets /* * add your stuff here if needed * Ethernet_doPacket() must be called as often as possible * otherwise packets could be lost */ } }
What do you think about this topic ? Send us feedback!