ENC28J60 Ethernet library with examples and full source code in MikroBasic language, available here :
http://www.microelemente.ro/MikroBasic/ ... amples.zip
Compiled for BICPIC4 board, MCU PIC18F8722.
Default Ip : 192.168.1.253
Code: Select all
'Ethernet library V3.3 and example for ENC28J60.
' ARP, ICMP, UDP, NTP, TCP, HTTP
' Author of this project : Florin Andrei Medrea
' Copyright (c) 2008 - YO2LIO - All Rights Reserved
' 29-Jan-2008
User Functions and Procedures:
(AddrPacket is address of received packet, TXSTART is start address of transmited packet)
- Sub Procedure Eth_SetParameters
' Set your IP, Mac, Mask, Gateway ... etc... here.
' Str2Ip("192.193.194.14",eth_ip_addr)
' Str2Ip("85.120.220.254",eth_ntp_addr)
' Str2Ip("192.193.194.1",eth_gateway)
' Str2Ip("255.255.255.0",eth_mask)
' Str2Mac("0004A300809A",eth_mac)
' Str2Ip("192.193.194.4",user_ip_addr)
' eth_port = 10001
' dest_port = 10001
- Sub Procedure Eth_Init_(bim byref ENC_Port as byte,dim CS,Reset as byte);
' Init MCU Ethernet.
- Sub Procedure Eth_Reset
' Reset MCU Ethernet.
- Sub Procedure Wait_For_Lan
' Wait fo Lan Link.
- Sub Procedure Eth_DoPacket
' Process incoming packets.
- Sub Function Eth_UserTCP(dim byref dest_ip_addr_T as byte[4], dim byref source_port_T, dest_port_T, len_T as word) as word
' This function is called by library. Put your TCP response for WEB server parameters here. See example.
- Sub Function Eth_UserUDP(dim byref dest_ip_addr_U as byte[4], dim byref dest_port_U, source_port_U, len_U as word) as word
' This function is called by library. Put your UDP response here. See example. ( ECHO example )
- Sub Function CopyFlashToEthMem_CP(dim start_Flash_address as longint) as 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)
- Sub Function Eth_SendARP(dim byref ip_dest as byte[4]) as Boolean
' Get ARP request. Return True if Mac exist.
' Return dest. mac address in dim dest_mac as byte[6].
' Return dest. ip address in dim dest_ip_addr as byte[4].
' Ex. bol = Eth_SendARP(user_ip_addr)
' First function search in ArpCache (size of 9) ...
- Sub Procedure SendUDP(dim dest_port_S, source_port_S, len_data as word, dim byref data_udp as 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 ')
- Sub Procedure Eth_SendPing(dim SlotState as boolean, dim PingSlot as 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 as
' PingCache[Slot].IpP as byte[4]
' PingCache[Slot].MacP as byte[6]
' PingCache[Slot].Time as word
' PingCache[Slot].TTL as byte.
- Sub Function Send_Ping(dim byref ip_address as byte[4]) as word
' Send ping at specified ip_address and return response time.
' Ex. PingTimeResponse = Send_Ping(user_ip_address)
- Sub Function Send_UDP(dim byref ip_address as byte[4], dim dest_port_S, source_port_S, len_data as word, dim byref data_udp as array[1472] of byte) as 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 ")
- Sub Function Send_ARP(dim byref ip_address as byte[4], dim byref mac_address as byte[6]) as 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)
- Sub Procedure Firewall(dim ICMP, TCP, UDP as 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)
- Sub Function Ntp_query as 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.
- Sub Procedure Get_Time
' Transform NTP time in day/month/year ...
' Data will be stored in as
' 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')
- Sub Procedure Eth_PutByte(dim value as byte)
' Put one byte in ETH memory.
- Sub Function Eth_GetByte as byte
' Get one byte from ETH memory.
- Sub Procedure CopyEthMemToRam(dim start_eth_address, dest_ram_address, length_w as word)
' Ex. CopyEthMemToRam(AddrPacket+6,@dest_mac_addr,6)
- Sub Procedure CopyEthMemToRam_Inv(dim start_eth_address, dest_ram_address, length_w as word)
' Ex. CopyEthMemToRam(AddrPacket+6,@data_dWord,4)
- Sub Procedure CopyEthMemToEthMem(dim start_eth_address, dest_eth_address, length_w as word, dim where as 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)
- Sub Procedure CopyRamToEthMem(dim start_ram_address, dest_eth_address, length_w as word)
' Ex. CopyRamToEthMem(@eth_mac,TXSTART+22,6)
- Sub Procedure CopyRamToEthMem_Inv(dim start_ram_address, dest_eth_address, length_w as word)
' Ex. CopyRamToEthMem(@data_dWord,TXSTART+22,4)
- Sub Procedure CopyRamToEthMem_CP(dim start_ram_address, length_w as word)
' Copy var data to Eth Memory.
' Ex. CopyRamToEthMem_CP(@data,46)
- Sub Procedure CopyRamToEthMem_CP_Inv(dim start_ram_address, length_w as word)
' Ex. CopyRamToEthMem_CP(@data_dWord,4)
- Sub Function CopyRamStringToEthMem_CP(dim byref s as string[1472]) as 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')
- Sub Procedure CopyFlashToEthMem(dim start_Flash_address as longint, dim dest_eth_address, length_w as word)
' Ex. CopyFlashToEthMem(@httpHeader,TXSTART+54,30)
- Sub Procedure CopyFlashToEthMem_CP(dim start_Flash_address as longint, dim length_w as word)
' Ex. CopyFlashToEthMem(@httpHeader,30)
- Sub Function CopyFlashStringToEthMem(dim start_Flash_address as longint, dim dest_eth_address as word) as 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)
- Sub Function CopyFlashStringToEthMem_CP(dim start_Flash_address as longint) as 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)
- Sub Procedure WriteToEthMem(dim dest_eth_address as word, dim value as byte)
' Ex. WriteToEthMem(TXSTART+12,$08)
- Sub Function ReadFromEthMem(dim start_eth_address as word) as byte
' Ex. data = ReadFromEthMem(AddrPacket+38)
- Sub Function EthMemCompareWithRam(dim start_eth_address, start_ram_address, length_w as word) as boolean
' Ex. bol = EthMemCompareWithRam(AddrPacket+30,@eth_ip_addr,4)
- Sub Function EthMemCompareWithFlash(dim start_eth_address as word, dim start_Flash_address as longint, dim length_w as word) as boolean
' Ex. bol = EthMemCompareWithFlash(AddrPacket+54, @httpHeader, 30)