ENC28J60 library for 16 bit MCU family, with full source code, available here : http://www.microelemente.ro/MikroPascal ... source.zip
Tested on Explorer16 board, MCU PIC24FJ128GA010, home made ENC28J60 ethernet board, MCU clock 32 MHz(16 Mips), Spi bus speed 16 Mbps.
Default Ip is 192.193.194.12 , you can change this from unit enc_lib_user.
Code: Select all
{Ethernet library V3.6 and example for ENC28J60.
ARP, ICMP, UDP, NTP, TCP, HTTP
Author of this project : Florin Andrei Medrea
Copyright (c) 2008 - YO2LIO - All Rights Reserved
26-Jan-2008
User Functions and Procedures:
Available types :
IpHeader : array[4] of byte;
MacHeader : array[6] of byte;
(AddrPacket is address of received packet, TXSTART is start address of transmited packet)
- Procedure Eth_SetParameters;
// Set your IP, Mac, Mask, Gateway ... etc... here.
// Str2Ip('192.168.1.253',eth_ip_addr);
// Str2Ip('85.120.220.254',eth_ntp_addr);
// Str2Ip('192.168.1.1',eth_gateway);
// Str2Ip('255.255.255.0',eth_mask);
// Str2Mac('0004A300809A',eth_mac);
// Str2Ip('192.168.1.4',user_ip_addr);
// eth_port := 10001;
// dest_port := 10001;
- Procedure Eth_Init_(ENC_Port,CS,Reset);
// Init Ethernet.
- Procedure Eth_Reset;
// Reset Ethernet.
- Procedure Eth_DoPacket;
// Process incoming packets.
- Function Eth_UserTCP(var dest_ip_addr_T : IpHeader; var source_port_T, dest_port_T, len_T : word) : word;
// This function is called by library. Put your TCP response for WEB server parameters here. See example.
- Function Eth_UserUDP(var dest_ip_addr_U : IpHeader; var dest_port_U, source_port_U, len_U : word) : word;
// This function is called by library. Put your UDP response here. See example. ( ECHO example )
- Function CopyFlashToEthMem_CP(start_Flash_address : longint): word;
// Copy Const from flash to Eth Memory and return length of Const data.
// Const data must be defined as STRING. (and must be zero terminated)
// Ex. len := CopyFlashToEthMem_CP(@httpHeader);
- Function Eth_SendARP(var ip_dest : IpHeader) : Boolean;
// Get ARP request. Return True if Mac exist.
// Return dest. mac address in var dest_mac : MacHeader;.
// Return dest. ip address in var dest_ip_addr : IpHeader;.
// Ex. bol := Eth_SendARP(user_ip_addr);
// First function search in ArpCache (size of 9) ...
- Procedure SendUDP(dest_port_S, source_port_S, len_data : word; var data_udp : array[1472] of byte);
// Send UDP mesage. Max 1472 bytes.
// Ex. If Eth_SendARP(user_ip_addr) then SendUDP(dest_port, eth_port, 10, 'Test Test ');
- Procedure Eth_SendPing(SlotState : boolean; PingSlot : Byte);
// Have 8 Slots available 0 .. 7 and PingCache size of 8.
// Ex. Eth_SendPing(Eth_SendARP(user_ip_addr),0); Send Ping at user_ip_addr and put result in Slot 0.
// PingCache format :
// PingCache_IpP[Slot] : IpHeader;
// PingCache_MacP[Slot] : MacHeader;
// PingCache_Time[Slot] : word;
// PingCache_TTL[Slot] : byte.
- Function Send_Ping(var ip_address : IpHeader) : word;
// Send ping at specified ip_address and return response time.
// Ex. PingTimeResponse := Send_Ping(user_ip_address);
- Function Send_UDP(var ip_address : IpHeader; dest_port_S, source_port_S, len_data : word; var data_udp : array[1472] of byte) : boolean;
// Send UDP mesage, max 1472 bytes, at specified ip_address and return true if success.
// Ex. Success := Send_UDP(user_ip_addr, 10001, 9999, 10, 'data data ');
- Function Send_ARP(var ip_address : IpHeader; var mac_address : MacHeader) : boolean;
// Get MAC for specified ip_address, put result in mac_address variable and return true if success.
// Ex. Success := Send_ARP(user_ip_addr,reply_mac_addr);
- Procedure Firewall(ICMP, TCP, UDP : boolean);
// Default all false, allow all type of packets.
// If ICMP = true, ignore ICMP request. Ex. Firewall(true, false, false);
// If TCP = true, ignore TCP packets. Ex. Firewall(false, true, false);
// If UDP = true, ignore UDP packets. Ex. Firewall(false, false, true);
- Function Ntp_query : Boolean;
// Syncronize time.
// Ntp address must me stored in eth_ntp_addr (see Eth_SetParameters)
// If syncronization was successfully Ntp_sync flag will be true.
- Procedure Get_Time;
// Transform NTP time in day/month/year ...
// Data will be stored in :
// TTime_Rfc = Time in RFC format
// TTime_Unix = Time in UNIX format
// TTime_Year = Year (ex. 2007)
// TTime_Month = Month (ex. 07)
// TTime_Day = Day (ex. 31)
// TTime_Hour = Hour(ex. 12)
// TTime_Min = Min (ex. 59)
// TTime_Sec = Sec (ex. 32)
// TTime_Str = String format max. 32 ('2007-11-24 14:25:00')
- Procedure Eth_PutByte(value : byte);
// Put one byte in ETH memory.
- Function Eth_GetByte : byte;
// Get one byte from ETH memory.
- Procedure CopyEthMemToRam(start_eth_address, dest_ram_address, length_w : word);
// Ex. CopyEthMemToRam(AddrPacket+6,@dest_mac_addr,6);
- Procedure CopyEthMemToRam_Inv(start_eth_address, dest_ram_address, length_w : word);
// Ex. CopyEthMemToRam(AddrPacket+6,@data_dWord,4);
- Procedure CopyEthMemToEthMem(start_eth_address, dest_eth_address, length_w : word; where : byte);
// where = 0 copy from Eth RxBuf to Eth TxBuf
// where = 1 copy from Eth TxBuf to Eth TxBuf
// Ex. CopyEthMemToEthMem(AddrPacket+38,TXSTART+28,4,0);
- Procedure CopyRamToEthMem(start_ram_address, dest_eth_address, length_w : word);
// Ex. CopyRamToEthMem(@eth_mac,TXSTART+22,6);
- Procedure CopyRamToEthMem_Inv(start_ram_address, dest_eth_address, length_w : word);
// Ex. CopyRamToEthMem(@data_dWord,TXSTART+22,4);
- Procedure CopyRamToEthMem_CP(start_ram_address, length_w : word);
// Copy var data to Eth Memory.
// Ex. CopyRamToEthMem_CP(@data,46);
- Procedure CopyRamToEthMem_CP_Inv(start_ram_address, length_w : word);
// Ex. CopyRamToEthMem_CP(@data_dWord,4);
- Function CopyRamStringToEthMem_CP(var s : string[255]): word;
// Copy var data to Eth Memory and return length of var data.
// Var data must be defined as String. (must be zero terminated)
// Ex. len := CopyRamStringToEthMem_CP('data data data');
- Procedure CopyFlashToEthMem(start_Flash_address : longint; dest_eth_address, length_w : word);
// Ex. CopyFlashToEthMem(@httpHeader,TXSTART+54,30);
- Procedure CopyFlashToEthMem_CP(start_Flash_address : longint; length_w : word);
// Ex. CopyFlashToEthMem(@httpHeader,30);
- Function CopyFlashStringToEthMem(start_Flash_address : longint; dest_eth_address : word): word;
// Copy Const from flash to Eth Memory and return length of Const data.
// Const data must be defined as STRING. (must be zero terminated)
// Ex. len := CopyFlashToEthMem_CP(@httpHeader,TXSTART+54);
- Function CopyFlashStringToEthMem_CP(start_Flash_address : longint): word;
// Copy Const from flash to Eth Memory and return length of Const data.
// Const data must be defined as STRING. (must be zero terminated)
// Ex. len := CopyFlashToEthMem_CP(@httpHeader);
- Procedure WriteToEthMem(dest_eth_address : word; value : byte);
// Ex. WriteToEthMem(TXSTART+12,$08);
- Function ReadFromEthMem(start_eth_address : word) : byte;
// Ex. data := ReadFromEthMem(AddrPacket+38);
- Function EthMemCompareWithRam(start_eth_address, start_ram_address, length_w : word) : boolean;
// Ex. bol := EthMemCompareWithRam(AddrPacket+30,@eth_ip_addr,4);
- Function EthMemCompareWithFlash(start_eth_address : word; start_Flash_address : longint; length_w : word) : boolean;
// Ex. bol := EthMemCompareWithFlash(AddrPacket+54, @httpHeader, 30);
}