This page will show you how to use advanced mikroC ENC28J60 ethernet library calls :
DISCLAIMER |
|
The reader is supposed :
|
If you have any question, comment, or bug report, you are welcome : please post it to my forum. |
Page index :
LOW LEVEL FUNCTIONS
Here are SPI Ethernet mikroC functions, as they are supposed to be.
They should be the same for mikroPascal (see here) and mikroBasic.
PROTOTYPE | unsigned char Spi_Ethernet_readReg(unsigned char addr) ; |
PARAMETERS |
address of ENC28J60 register to read |
RETURNS | value of register @ addr in current bank selection |
DESCRIPTION |
read an ENC28J60 register |
REQUIRES |
Spi_Ethernet_init must have been called |
EXAMPLE | b = Spi_Ethernet_readReg(0x10) ; |
PROTOTYPE | void Spi_Ethernet_writeReg(unsigned char addr, unsigned short v) ; |
PARAMETERS |
addr : address of ENC28J60 register to write in current bank selection |
RETURNS | nothing |
DESCRIPTION |
writes a byte to an ENC28J60 register |
REQUIRES |
Spi_Ethernet_init must have been called |
EXAMPLE | Spi_Ethernet_writeReg(0x10, 0xff) ; |
WRITE ENC28J20 ADDRESS REGISTER
PROTOTYPE | void Spi_Ethernet_writeAddr(unsigned char addr, unsigned int v) ; |
PARAMETERS |
addr : address of ENC28J60 register to write in current bank selection |
RETURNS | nothing |
DESCRIPTION |
writes an ENC28J60 register with high and low part, usefull to write address registers |
REQUIRES |
Spi_Ethernet_init must have been called |
EXAMPLE | Spi_Ethernet_writeReg(0x10, 0x1234) ; |
PROTOTYPE | void Spi_Ethernet_setBitReg(unsigned char addr, unsigned char mask) ; |
PARAMETERS |
addr : address of ENC28J60 register |
RETURNS | nothing |
DESCRIPTION |
ENC28J60 register is ORed with mask |
REQUIRES |
Spi_Ethernet_init must have been called |
EXAMPLE | Spi_Ethernet_setBitReg(0x12, 0b00010010) ; |
CLEAR BITS OF ENC28J60 REGISTER
PROTOTYPE | void Spi_Ethernet_clearBitReg(unsigned char addr, unsigned char mask) ; |
PARAMETERS |
addr : address of ENC28J60 register |
RETURNS | nothing |
DESCRIPTION |
ENC28J60 register is ANDed with complement of mask |
REQUIRES |
Spi_Ethernet_init must have been called |
EXAMPLE | Spi_Ethernet_clearBitReg(0x12, 0b00010010) ; |
PROTOTYPE | void Spi_Ethernet_writePHY(unsigned char reg, unsigned short *h, unsigned short *l) ; |
PARAMETERS |
reg : address of ENC28J60 PHY |
RETURNS | nothing |
DESCRIPTION |
reads high and low bytes from PHY register |
REQUIRES |
Spi_Ethernet_init must have been called |
EXAMPLE | Spi_Ethernet_writePHY(3, &h, &l) ; |
PROTOTYPE | void Spi_Ethernet_writePHY(unsigned char reg, unsigned short h, unsigned short l) ; |
PARAMETERS |
reg : address of ENC28J60 PHY |
RETURNS | nothing |
DESCRIPTION |
writes high and low bytes to PHY register |
REQUIRES |
Spi_Ethernet_init must have been called |
EXAMPLE | Spi_Ethernet_writePHY(3, 0, 0) ; |
READ ONE BYTE FROM ENC28J60 INTERNAL MEMORY
PROTOTYPE | unsigned char Spi_Ethernet_readMem(unsigned int addr) ; |
PARAMETERS |
addr : address of byte to read |
RETURNS | value of byte @ addr |
DESCRIPTION |
read one byte from ENC28J60 RAM |
REQUIRES |
Spi_Ethernet_init must have been called |
EXAMPLE | b = Spi_Ethernet_readMem(0x0250) ; |
STORE BYTES TO ENC28J60 INTERNAL MEMORY
PROTOTYPE | void Spi_Ethernet_writeMemory(unsigned int addr, unsigned char v1, unsigned char v2, unsigned char bis) ; |
PARAMETERS |
addr : destination address of first byte in ENC28J60 RAM |
RETURNS | nothing |
DESCRIPTION |
store one or two bytes to ENC28J60 RAM |
REQUIRES |
Spi_Ethernet_init must have been called |
EXAMPLE | Spi_Ethernet_writeMemory(0x100, 12, 0, 0) ; |
COPY PIC MEMORY TO ENC28J60 INTERNAL MEMORY
PROTOTYPE | void Spi_Ethernet_memcpy(unsigned int addr, unsigned char *s, unsigned char l) ; |
PARAMETERS |
addr : destination address of first byte in ENC28J60 RAM |
RETURNS | nothing |
DESCRIPTION |
copy PIC memory to ENC28J60 RAM |
REQUIRES |
Spi_Ethernet_init must have been called |
EXAMPLE | Spi_Ethernet_memcpy(0x0600, "TEST", 4) ; |
COMPARE PIC MEMORY TO ENC28J60 INTERNAL MEMORY
PROTOTYPE | unsigned char Spi_Ethernet_memcmp(unsigned int addr, unsigned char *s, unsigned char l) ; |
PARAMETERS |
addr : address of first byte in ENC28J60 RAM to compare |
RETURNS | 0 if memories match, > 0 otherwise |
DESCRIPTION |
compares ENC28J60 RAM from address addr to PIC RAM pointed to by s, on l bytes length |
REQUIRES |
Spi_Ethernet_init must have been called |
EXAMPLE | Spi_Ethernet_memcmp(0x0600, "TEST", 4) ; |
CHECKSUM CALCULATION (DMA OPERATION)
PROTOTYPE | void Spi_Ethernet_checksum(unsigned int start, unsigned int l) ; |
PARAMETERS |
start : address of first byte in ENC28J60 memory |
RETURNS | nothing, checksum is in ENC28J60 EDMACS[H:L] registers |
DESCRIPTION | ENC28J60 performs DMA checksum of l bytes starting from start addr, using ENC28J60 built-in DMA operation |
REQUIRES | Spi_Ethernet_init must have been called |
EXAMPLE | Spi_Ethernet_checksum(0x1000, 60) ; |
ENC28J60 INTERNAL RAM COPY (DMA OPERATION)
PROTOTYPE | void Spi_Ethernet_RAMcopy(unsigned int start, unsigned int stop, unsigned int dest, unsigned char w) ; |
PARAMETERS |
start address of first source byte (included) in ENC memory to copy |
RETURNS | nothing |
DESCRIPTION | ENC28J60 performs DMA memory copy from start to dest, using ENC28J60 built-in DMA operation. if wrap is allowed, performs correct operation to avoid ENC28J60 hang on circular buffer |
REQUIRES | Spi_Ethernet_init must have been called |
EXAMPLE | Spi_Ethernet_RAMcopy(0x500, 0x600, 0x1400, 1) ; |
PROTOTYPE | void Spi_Ethernet_TXpacket(unsigned int l) ; |
PARAMETERS |
l : length in bytes of packet to transmit |
RETURNS | nothing |
DESCRIPTION |
send packet over wires |
REQUIRES |
Spi_Ethernet_init must have been called |
EXAMPLE | Spi_Ethernet_TXpacket(60) ; |
LIBRARY OVERVIEW
Here is what you have to know, and to keep in mind, when using the functions above.
Please respect this few rules if you want to mix successfully mikroC user's functions with your own library calls :
PROGRAM EXAMPLE
This example is an UDP push server :
You can test it with the built-in UDP terminal of your MikroElektronika compiler (Tools -> UDP Terminal).
Here is a screen capture :
Type here the IP address of your board, the port number 33000, then click "Connect"
Type here your requests, end with carriage return (or click "Send") :
b : begin push
e : end push
History of your commands are here
UDP packets received from ENC28J60 bord are here (I played with PORTB buttons during test)
The hardware is the one described in the mikroC manual, the software has been tested with the mikroElektronika Serial Ethernet Board, and with my own board too.
MIKROC SOURCE CODE EXAMPLE
Create a new mikroC project for your PIC18 target, copy/paste this source code into editor, change your MAC and IP addresses in macAddr and IpAddr variables and just build the project.
/* #define NULL 0 #define Spi_Ethernet_FULLDUPLEX 1 #define TRANSMIT_START 0x19AE // some ENC28J60 registers, see datasheet /*********************************** unsigned char ipAddr[4] = {192, 168, 1, 101} ; // my IP address unsigned char destIpAddr[4] ; // IP address unsigned char transmit = 0 ; #define ARPCACHELEN 3 /******************************************* // step in ARP cache // is it the address I want in a valid slot ? // find older slot, will be needed further // select ENC register bank 0 /*************************** /**************************** /* Spi_Ethernet_memcpy(REPLY_START + 22, macAddr, 6) ; // mac addr source /******************************************************************* tmr = 0 ; // select ENC register bank 0, may have been changed by library /* /* // restore ERDPT before exiting // restore ERDPT before doing a new call to the library // failed, no response : MAC addr is null /* // is destination port valid ? // does destination IP address exist ? // select ENC register bank 0 /* /************************** /*************************** Spi_Ethernet_memcpy(REPLY_START + 42, pkt, pktLen) ; // payload /***************** /***************** // build pseudo header // ready for checksum /**************************
/* // end command // error /* /* while(1) // do forever if(transmit && (tmr > 15)) // transmit one UDP packet per second
|
mikroPascal Source Code EXAMPLE
Philippe Celma did the translation from the original mikroC source code into mikroPascal, it is fully documented and works exactly the same.
You can download its mikroPascal project from here :
ENC28J60-LIBRARY-Spi_Ethernet_TxPacket_mikroPascal.ZIP (Zip file, 5.56 Ko).
Like the mikroC example, just set your own MAC and IP addresses in the source code before building the project.
Thank you Philippe !