From Swordfish Wiki

SwordfishUser: Ethernet

The Swordfish modules used for Ethernet support comprises of a partial port of the Microchip TCP/IP framework version 4.11. Note the use of the word partial. Some modules are not currently implemented (for example, HTTP), but I may get around to doing this in the future - time permitting. The following modules have been implemented.

7ApplicationDHCP, DNS, SMTP, SNTP
6Presentation 
5SessionNetBIOS (NBNS)
4TransportTCP, UDP
3NetworkIP, ICMP, ARP
2Data LinkMAC
1PhysicalENC28J60

The modules have had limited testing. However, all the samples shown here work fine and it's probably better these modules are in the public domain, rather than just sitting on my machine!

Hardware Requirements

Before you start using the Ethernet library, you will need some hardware. The code is based around the Microchip ENC28J60, a 3.3v device which has a SPI interface and an 8KB buffer. The board I used is for the MikroStak system (shown to the left) but any development or breakout board that supports this device should suffice. For example, the mikroElectronika serial interface board or Sparkfun Ethernet interface board respectively. The schematic for the MikroStak board can be found here. Note the buffer interface chip. If your MCU is running at 5v you will need some level conversion as shown in the schematic.

Some PIC microcontrollers have ethernet support built in. For example, the 18F97J60. The current Swordfish implementation does not support this device*. However, the Microchip firmware is well designed at it should be a fairly trivial task to port the 18F97J60 physical layer "C" code. Like the ENC28J60.bas file, this would sit beneath the MAC.bas file and so no other changes to the library source code should be required.

Swordfish Library Code

The first thing you need to do is make sure you are running version 2.0.2.0 or higher of the Swordfish IDE. IF not, run the online update to patch the latest version. Next, download the Swordfish Ethernet module code.

Download Ethernet Library »

Download Ethernet Library V2 (with 18F97J60 and ENC424J600 support)

You should unzip the above and install in your UserLibrary folder. For example,

 C:\Program Files\Swordfish\UserLibrary\Ethernet
 C:\Program Files\Swordfish\UserLibrary\Ethernet\Microchip 4.11

The folder called 'Microchip 4.11' contains the core modules ported from the Microchip TCP/IP libraries. It is not recommended you edit these file unless you really understand the Microchip TCP/IP framework. However, feel free to take a look at the code - it will give you some idea of why a typical TCP/IP application uses substantial amounts of ROM when compiled!

The Ethernet folder itself has a number of support files. "NETApp.bas" may need to be edited if not using DHCP, as you will need to manually edit IP addresses. "NETAppDisplay.bas" and "UTCDateTime.bas" are helper modules for the sample programs.

Testing the Library Code

The following program can be used to test your setup. I used an 18F4680 but an 18F452 will work OK. The 18F4680 was chosen because some of the samples require more than 32K of ROM.

// device and clock - code will work on an 18F452 as well...
Device = 18F4680
Clock = 20

// important configurations...
#option ENC28J60_CS = PORTD.6
#option NET_ICMP = true   // enable ping 
#option NET_DHCP = true   // enable Dynamic Host Configuration Protocol (DHCP)
Include "NETApp.bas"      // always first!

// program helpers...
Include "NETAppDisplay.bas"
Include "usart.bas"

// program start...
SetBaudrate(br115200)

#if NET_DHCP
Write("Waiting for DHCP...",13,10)
#endif

// loop forever...
While true

   // This task performs normal stack task including checking for incoming 
   // packet, type of packet and calling appropriate stack entity to process it.
   NETApp.Task

   // If DHCP is enabled, display configuration when a DHCP event
   // has occurred...
   #if NET_DHCP
   If NETApp.DHCPEvent() Then
      DisplayConfig()
   EndIf

   // read USART value - if equal to "d" then display config info...
   If DataAvailable Then
      If USART.ReadByte = Byte("d") Then
         DisplayConfig()
      EndIf
   EndIf
   #endif               
Wend

The above program uses DHCP to automatically configure your IP addresses. You should then be able to ping your board over the network. Here's what you need to do.

If all goes well, the following should be displayed in the Serial Communicator window

 Waiting for DHCP...
 MAC Address   : $00 $04 $A3 $00 $00 $00 
 IP address    : 192.123.223.1
 Mask          : 255.255.255.0
 Gateway       : 192.123.223.0

OK, you now have the IP address which (in the above example) is 192.123.223.1. If you now open a "command prompt" window, you can ping your device. For example,

 ping 192.123.223.1
 Pinging 192.123.223.1 with 32 bytes of data:

 Reply from 192.123.223.1: bytes=32 time=28ms TTL=100
 Reply from 192.123.223.1: bytes=32 time=2ms TTL=100
 Reply from 192.123.223.1: bytes=32 time=2ms TTL=100
 Reply from 192.123.223.1: bytes=32 time=2ms TTL=100

 Ping statistics for 192.123.223.1:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
 Approximate round trip times in milli-seconds:
    Minimum = 2ms, Maximum = 28ms, Average = 8ms

Usage Notes

To use this module with boards that have RESET pins that are not tied high, a modification should be made to ENC28J60.bas. It does not appear to set the RESET pin high. In the MACInit sub-routine (about line 1263, add the following code:

Output(ENC_RST_IO)
ENC_RST_IO = 1

And then define the RESET pin in your main application near where you set your CS pin option:

#option ENC28J60_RST = PORTC.0

Then things work much better.

Additional Samples

The following sample programs assume you have followed all of the above installation requirements and tested the library code - again, see the code sample above.

Please note that I have posted the Ethernet library and sample code because it may be of some use or interest to other Swordfish users. It represents a large body of work and as such I am unable to commit resources in providing support for the time being.

However, comments and suggestions are welcome on the forum

Forum Support Links

Retrieved from https://www.sfcompiler.co.uk/wiki/pmwiki.php?n=SwordfishUser.Ethernet
Page last modified on September 27, 2015, at 05:05 PM