W5100
The modules allow the use of the modules WIZnet / W5100 in Swordfish. The W5100 has within it all the logic related to the TCP client / server responds to the ping without special drivers, and it is for this reason that lends itself to be used quickly and without a great deal of memory.
You can download modules and examples from the following link: WIZ811MJ/W5100
In particular, I have used through a module sold by the same WIZnet called WIZ811MJ that mounts over chip and ethernet connector with all necessary circuitry. There are several modules with W5100 chip but only the WIZ811MJ has stripline connectors at 2.54 mm step (one model has stripline 2.0 mm step ) and two holes for the mounting screws so look for hw size and description because they are very similar but differ in some important details.
Works with 3.3V only although would be driven with signals up to 5V tolerance. In a prototype I used a PIC at 5V and a 3,3V translator levels but do not recommend it because then you must go down a lot with the speed of the SPI and you lose in performance. Now I used it with 3.3V, a PIC18F2682, a CAN transceiver from Texas Instruments, the SN65HVD230 who also works with 3.3V.
I used the SPI interface even if WIZ811MJ module also has a parallel interface. It has two connectors strips with 2x10 pin of which have been connected just a dozen including power and ground. To enable the SPI interface and disable the parallel need to put a resistor pull-up 10KOhm on pin named /CS of the module (in case of WIZ811MJ is J2/7) that puts in three-state the parallel bus and the module use SPI communication enabled via pin named /SCS (in the WIZ811MJ module is the pin J2/4).
Pin Module | Function | Connected to.. |
---|---|---|
J1/1 | MOSI | SDO MCU line |
J1/2 | MISO | SDI MCU line |
J1/11 | GND | Vss |
J1/12 | 3V3 | 3,3V Vcc |
Pin Module | Function | Connected to.. |
---|---|---|
J2/1 | 3V3 | 3,3V Vcc |
J2/2 | /RESET | Reset line in my case PORTC.1 |
J2/3 | SCLK | SCK MCU line |
J2/4 | /SCS | SPI Chip select in my case PORTC.0 |
J2/7 | /CS | Pullup to 3,3V via 10KOhm resistor |
J2/9 | GND | Vss |
J2/10 | GND | Vss |
J2/19 | GND | Vss |
J2/20 | GND | Vss |
The module in question is well-known by many Arduino fans and the only flaw it has is the lack of natives DHCP and DNS for which you must write the missing driver .. actually the WIZnet provides extensive documentation about it and they should make porting from C ++.
Porting I did include the initialization and all that allows you to use the W5100 as a server because I intend to use it as a web service even if all the routines implemented allow its use as a server / client TCP and UDP as it is, I translated the entire set of C ++ functions. In the future I intend to implement NTP and sending e-mails since there are many examples on the net by Arduino libraries.
The W5100 has a total memory of 8K transmission and 8K reception RAM and has 4 independent sockets on which you can make simultaneous connections. The memories are adaptable to the needs, and you can predefine the amount of memory allocation for the TX and RX for every socket in steps of 1K. The routine that sets the memory is the W5100_Regs.SysMemInit and have to call after a reset of the device and so you need to reset the parameters of the network (refer to W5100_DHCP.SetDHCPNetwork and the datasheet of the device).
Modules in zip
Module | Description |
---|---|
W5100.bas | Main module with public aliases to other module |
W5100-Consts.bas | Device constants like regs and memory definitions |
W5100-DHCP.bas | DHCP module with DHCHTask main sub |
W5100-DNS.bas | DNS module with DNSResolve main sub, the DNS uses global Dns_Addr for DNS server IP address |
W5100-IPConfig.bas | Network default configuration like MAC address and IP,GW,DNS when USE_DHCP=False |
W5100-Regs.bas | Module responsible of device regs setting |
W5100-RX.bas | All sync and async RX functions |
W5100-RXBuffer.bas | RX buffer definition. Is the module that defines a memory area to be used as the buffer .. W5100 uses for each of its memories two pointers in overwrite mode, then in some cases, as in the reception of an HTTP request, it is necessary a buffer memory to extract it. The sockets are 4 but This memory must be used for the socket where this can happen |
W5100-Socket.bas | Routines for socket management |
W5100-TX.bas | All sync and async TX functions |
W5100-Utils.bas | SW transposition of C++ arrays management |
SOAP.bas | Routines for the creation of response and parameters extraction from SOAP request |
You can download modules and examples from the following link: WIZ811MJ/W5100
The zip contains two folders named \W5100 and \VS2010. Copy folder W5100 directly into your \UserLibrary folder. Folder VS2010 contains a VisualStudio 2010 project used with the SOAP example found below.
If you do not use DHCP the settings affecting fixed ip address, subnet mask and gw are set in the module "W5100-IPConfig.bas" that however can be overridden and this module is not part of the zip and must be created into project folder. The default tcp port cTCP_PORT is set in "W5100-Consts.bas" and as default is the port 80. This port is used only and exclusively by the examples which I reported below and is not used in another way by the modules contained in the zip.
W5100-IPConfig.bas
Create this module in a file called W5100-IPConfig.bas in the folder where have created the two sample programs.
{ ***************************************************************************** * Name : W5100-IPConfig.BAS * * Author : Coccoliso * * Notice : Copyright (c) 2014 [select VIEW...EDITOR OPTIONS] * * : All Rights Reserved * * Date : 02/11/2014 * * Version : 1.0 * * Notes : * * : * ***************************************************************************** } Module W5100_IPConfig Public Const cMac_Addr(6) As Byte = ($00,$0E,$35,$73,$9F,$EF), // Customizable MAC Address cIp_Addr(4) As Byte = (192,168,1,9), // Static IP address cSub_Mask(4) As Byte = (255,255,255,0), // Subnet mask of your network cGtw_Addr(4) As Byte = (192,168,1,200), // Gateway address cDns_Addr(4) As Byte = (192,168,1,200) // Primary DNS. Your gateway. Public Const cHOST_NAME = "WIZ811" // At this time unused.
A note on the size of the RX buffer. I set the buffer to 600 bytes but in reality this size is due to the size that is used in the receiving SOAP envelope that I have used. This dimension will be adjusted as needed in "W5100-RXBuffer.bas" module.
... Public Dim RXBuff As String (600), RXBuffPointer As Word ...
HTTP Server Sample
Here's an example of use with DHCP request, DNS resolution and a simple web server that responds to the "/index.html" and is able to recognize the HTTP verb and act as a result.
{ ***************************************************************************** * Name : Test W5100.BAS * * Author : Coccoliso * * Notice : Copyright (c) 2014 * * : All Rights Reserved * * Date : 1/11/2014 * * Version : 1.0 * * Notes : * * : * ***************************************************************************** } Device = 18f2682 Clock = 40 #option WIZ_DHCP_DEBUG = false #option WIZ_DNS_DEBUG = false #option USE_DHCP = true #option USE_DNS = true #option WIZ_SCS_PIN = PORTC.0 #option WIZ_RST_PIN = PORTC.1 Include "W5100-Consts" Include "W5100-Utils" Include "W5100" Include "usart" Include "convert" Include "string" #if USE_DNS Include "W5100-DNSConsts" Dim dnsRes As DNS_INFO #endif Private Sub ConfigDisplay() USART.Write(#13,#10) USART.Write("MAC Address ",SARtoString(),#13,#10) USART.Write("IP Address ",SIPRtoString(),#13,#10) USART.Write("Subnet Mask ",SUBRtoString(),#13,#10) USART.Write("Gateway ",GARtoString(),#13,#10) USART.Write("DNS Address ",AddrToString(@Dns_Addr),#13,#10) USART.Write(#13,#10) End Sub #if USE_DHCP Sub OnUpdate() ConfigDisplay() End Sub Sub OnConflict() End Sub #endif // ################################################################################## // ################################################################################## Dim sockstat As Byte Dim sockreg As Byte Dim rsize As Word Dim charcount As Word Dim writecount As Word Dim iGet As Integer Dim iEnd As Integer Dim iPost As Integer Dim iInc As Integer Dim sResp As String USART.SetBaudrate(br115200) #if USE_DHCP = true DHCPSetEvents(@OnUpdate, @OnConflict) DHCPTask(3) #else ConfigDisplay() #endif DelayMS(1000) #if USE_DNS USART.Write("DNSResolving for www.google.com ... ") dnsRes = DNSResolve("www.google.com") If dnsRes.Status = cRC_NO_ERROR Then USART.Write(AddrToString(@dnsRes.IPAddr.Byt),13,10) Else USART.Write("unable to resolve (ERR=",DecToStr(dnsRes.Status),")",13,10) End If #endif USART.Write(13,10) USART.Write("Open a browser on http://" + SIPRtoString() + " for test it.",13,10) sockstat = 0 sockreg = 1 writecount = 0 charcount = 0 While true sockstat=ReadReg(sockreg, rSx_SR) Select sockstat Case SOCK_CLOSED If Socket(sockreg, MR_TCP, TCP_PORT, 0) Then If Not Listen(sockreg) Then DelayMS(1) End If End If Case SOCK_ESTABLISHED rsize = W5100.DataAvailable(sockreg) If rsize > 0 Then SockTimeoutEnable() GetRXBuffer(sockreg, @RXBuff, rsize, RXBuffPointer) Inc(RXBuffPointer,rsize) { A GET REQUEST Header is like this .. GET /index.html HTTP/1.1 Host: xxx.xxx.xxx.xxx Keep-Alive: timeout=15 Connection: Keep-Alive X-Forwarded-For: 192.168.10.1 .. then I search for "Connection:" Other types are xml tagged. } If isSubStrInRXBuff(0, RXBuffPointer, "Connection:")<>-1 Then charcount = RXBuffPointer writecount = 0 While charcount>0 Dec(charcount) USART.Write(RXBuff(writecount)) Inc(writecount) End While iGet = isSubStrInRXBuff(0, W5100.RXBuffPointer, "GET /") iPost = isSubStrInRXBuff(0, W5100.RXBuffPointer, "POST /") If iGet<>-1 Then iEnd = isSubStrInRXBuff(iGet, W5100.RXBuffPointer, " HTTP/") ElseIf iPost<>-1 Then iEnd = isSubStrInRXBuff(iPost, W5100.RXBuffPointer, " HTTP/") End If ASyncTXBufferInit(sockreg) PutInTXBuffer(sockreg,("HTTP/1.1 200 OK" + #13 + #10),false) PutInTXBuffer(sockreg,("Content-Type: text/html" + #13 + #10 + #13 + #10),false) PutInTXBuffer(sockreg,"<html><body>",false) If iGet<>-1 Then If (iEnd - iGet)<>5 Then sResp = "" ExtractRXBufferString(sockreg,@sResp, iGet + 5, iEnd + 1) If sResp<>"index.html" Then PutInTXBuffer(sockreg,"<h1>Sorry</h1>",false) PutInTXBuffer(sockreg,("<h2>Before you can download the page " +#34) ,false) PutInTXBuffer(sockreg, sResp,false) PutInTXBuffer(sockreg,(#34 + " I think that you have to create it ..</h2>"),false) PutInTXBuffer(sockreg,"<h2>Please specify ",false) PutInTXBuffer(sockreg,(#34 + "index.html" + #34 + " to get it!</h2>"),false) Else PutInTXBuffer(sockreg,("<form name=" + #34 + "myform" + #34),false) PutInTXBuffer(sockreg,(" action=" + #34 + "index.html" + #34),false) PutInTXBuffer(sockreg,(" method=" + #34 + "POST" + #34 + ">"),false) PutInTXBuffer(sockreg,"<br><br>",false) PutInTXBuffer(sockreg,("<input type=" + #34 + "text" + #34),false) PutInTXBuffer(sockreg,(" size=" + #34 + "25" + #34),false) PutInTXBuffer(sockreg,(" name=incoming value=" + #34 + #34 + ">"),false) PutInTXBuffer(sockreg,("<br><input type=" + #34 + "submit" + #34),false) PutInTXBuffer(sockreg,(" value=" + #34 + " SEND TEXT " + #34 + "><br>"),false) PutInTXBuffer(sockreg,"</form>",false) End If Else PutInTXBuffer(sockreg,("<h2>Please specify " ),false) PutInTXBuffer(sockreg,(#34 + "index.html" + #34 ),false) PutInTXBuffer(sockreg,(" to get it!</h2>"),false) End If EndIf If iPost<>-1 Then iInc = isSubStrInRXBuff(iPost, W5100.RXBuffPointer, "incoming=") If iInc <>-1 Then sResp = "" ExtractRXBufferString(sockreg,@sResp, iInc + 9, W5100.RXBuffPointer + 1) PutInTXBuffer(sockreg,"<h2>Here you can take value posted ... ",false) PutInTXBuffer(sockreg, sResp + " </h2>",false) End If End If PutInTXBuffer(sockreg,"</body></html>",false) ASyncTXBufferSend(sockreg) Disconnect(sockreg) RXBuffClean() SockTimeoutDisable() End If End If Case SOCK_FIN_WAIT, SOCK_CLOSING, SOCK_TIME_WAIT, SOCK_CLOSE_WAIT, SOCK_LAST_ACK Close(sockreg) USART.Write(13,10,"SOCKET(",DecToStr(sockreg),") Closed",13,10) End Select If SockTimeout() Then Disconnect(sockreg) RXBuffClean() SockTimeoutDisable() End If End While
I want to point out the use of the send buffer:
ASyncTXBufferInit(sockreg) PutInTXBuffer(sockreg,("HTTP/1.1 200 OK" + #13 + #10),false) ... ASyncTXBufferSend(sockreg)
ASyncTXBufferInit storing the pointer of transmission buffer and after a series of entries is communicated to the device to forward the content included from initial pointer to current pointer, all internal memory overwrites and memory pointer movements are under control of W5100. The third "false" PutInTXBuffer parameter tells the device to not process the buffer and do not close the transmission.
SOAP 1.1 Web Service Sample
Use as a web service instead imposes many standards and the amount of memory MCU does not allow the use of a wsdl downloaded at the time and so you need to create it in another way .. I have implemented a SOAP ws 1.1 in VS2010 then re-imported into the client program during development allows me to do without it when using the embedded web server. An example of the result of this import/generation make by VisualStudio tool you can find directly in the project folder of the client \VS2010 \WindowsApplication1\Service References\ServiceReference1 that stores a copy of the generated WSDL named "Service1.wsdl", open it, this is how the web service should return as result of a "?WSDL" client request .. 10K characters are too many for every PIC. Use of this generation method is simple. Emulate the web service response with a custom "as like" web service then the client application, when a wsdl is request for generate the recv class, you point to in-solution web service.. you're done and do not need new reload it until wsdl updates. One thing you must remember is to change the url in the client app before using the application because end point is the original ws windows url.
The purpose of the application is to show in the first case how to send a request to the web service in which it took a command and a parameter and obtain a Boolean value, and in a second case which the application call the web service for message state consisting of a class ( an object ) of composite type.
Simple test application is "WindowsApplication1.exe" from \VS2010\WindowsApplication1\bin\Debug folder. You can open the source with VisualStudio loading "WebService1.sln" solution file from \VS2010 folder that opens in the solution also the client project.
This is VS2010 VB.NET class named Service1.asmx used in example:
Imports System.Web.Services Imports System.Web.Services.Protocols Imports System.ComponentModel <System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _ <System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _ <ToolboxItem(False)> _ Public Class Service1 Inherits System.Web.Services.WebService <WebMethod()> _ Public Function Status() As Response Dim a As New Response() a.ListaAttese.Add(10) a.ListaAttese.Add(1) a.ListaAttese.Add(2) Return a End Function <WebMethod()> _ Public Function Command(cCommand As Byte, cParm As Byte) As Boolean Return True End Function End Class
The response class ( composite object ) than contains returned values: four integers two boolean and two variable size arrays of integer.
Imports System.Collections.Generic Imports System.Runtime.Serialization <Serializable()> _ Public Class Response Public Property PointsNum As Integer Public Property Chiamate As Integer Public Property InAttesa As Integer Public Property Visore As Integer Public Property Buzzer As Boolean Public Property Flash As Boolean Public Property ListaChiamate As New List(Of Integer) Public Property ListaAttese As New List(Of Integer) Public Sub New() End Sub End Class
This is the "PIC side" source that receives, interprets and returns to the windows client app the class above:
{ ***************************************************************************** * Name : W5100 WS.BAS * * Author : Coccoliso * * Notice : Copyright (c) 2014 * * : All Rights Reserved * * Date : 14/10/2014 * * Version : 1.0 * * Notes : * * : * ***************************************************************************** } Device = 18f2682 Clock = 40 #option WIZ_DHCP_DEBUG = false #option WIZ_DNS_DEBUG = false #option USE_DHCP = true #option USE_DNS = false #option WIZ_SCS_PIN = PORTC.0 #option WIZ_RST_PIN = PORTC.1 Include "usart" Include "convert" Include "string" Include "W5100-Consts" Include "W5100-Utils" Include "W5100" Include "SOAP" Private Sub ConfigDisplay() USART.Write(#13,#10) USART.Write("MAC Address ",SARtoString(),#13,#10) USART.Write("IP Address ",SIPRtoString(),#13,#10) USART.Write("Subnet Mask ",SUBRtoString(),#13,#10) USART.Write("Gateway ",GARtoString(),#13,#10) USART.Write("DNS Address ",AddrToString(@Dns_Addr),#13,#10) USART.Write(#13,#10) End Sub #if USE_DHCP Sub OnUpdate() ConfigDisplay() End Sub Sub OnConflict() End Sub #endif // ################################################################## // ################################################################## Dim sockstat As Byte Dim sockreg As Byte Dim rsize As Word Dim iMethod As Integer Dim cCommand As Integer Dim cParm As Integer USART.SetBaudrate(br115200) MemoryDisplay #if USE_DHCP = true DHCPSetEvents(@OnUpdate, @OnConflict) DHCPTask(3) #else ConfigDisplay() #endif DelayMS(1000) USART.Write("WS Testing.. ",#13,#10) sockreg = 1 sockstat = 0 rsize = 0 iMethod = 0 cCommand = 0 cParm = 0 While true sockstat=ReadReg(sockreg, rSx_SR) Select sockstat Case SOCK_CLOSED If Socket(sockreg, MR_TCP, TCP_PORT, 0) Then If Not Listen(sockreg) Then DelayMS(1) End If End If Case SOCK_ESTABLISHED rsize = W5100.DataAvailable(sockreg) If rsize > 0 Then SockTimeoutEnable() GetRXBuffer(sockreg, @RXBuff, rsize, RXBuffPointer) Inc(RXBuffPointer,rsize) If SOAP.CheckEnvelope() Then If SOAP.CheckMethod("Command", iMethod) Then If SOAP.GetValue(iMethod,"cCommand",cCommand) Then // Now know cCommand value, the first parameter USART.Write("cCommand=",DecToStr(cCommand),#13,#10) Else USART.Write("cCommand=","INVALID",#13,#10) End If If SOAP.GetValue(iMethod, "cParm",cParm) Then // Now know cParm value, the second parameter USART.Write("cParm=",DecToStr(cParm),#13,#10) Else USART.Write("cParm=","INVALID",#13,#10) End If // Incoming method is a Command request // so prepare a Command SOAP response SOAP.Initialize(sockreg,"Command") // This method require a single boolean value SOAP.WriteResult(true) // The SOAP message is complete // then write a response finalize SOAP.Finalize() // Free the receiver app with // a socket close Disconnect(sockreg) // Clean rx buffer RXBuffClean() // Suspend timeout calculation SockTimeoutDisable() End If If CheckMethod("Status", iMethod) Then // Incoming method is a Status request // so prepare a Status SOAP response SOAP.Initialize(sockreg,"Status") // Write a single integer element named PointsNum SOAP.WriteElement("PointsNum",0) // Write a single integer element named Chiamate SOAP.WriteElement("Chiamate",0) // Write a single integer element named InAttesa SOAP.WriteElement("InAttesa",0) // Write a single integer element named Visore SOAP.WriteElement("Visore",0) // Write a single boolean element named Buzzer SOAP.WriteElement("Buzzer", false) // Write a single boolean element named Flash SOAP.WriteElement("Flash", false) // Now must write an array of integer named ListaChiamate // so prepare a multiple element with an open SOAP.WriteElementOpen("ListaChiamate") // Now write some integer values SOAP.Write(0,1,2,3) // Now close the multiple element SOAP.WriteElementClose("ListaChiamate") // Now must write an array of integer named ListaAttese // but for now is empty so prepare an empty element SOAP.WriteEmptyElement("ListaAttese") // The SOAP message is complete // write a response finalize SOAP.Finalize() // Free the receiver app with // a socket close Disconnect(sockreg) // Clean rx buffer RXBuffClean() // Suspend timeout calculation SockTimeoutDisable() End If End If End If Case SOCK_FIN_WAIT, SOCK_CLOSING, SOCK_TIME_WAIT, SOCK_CLOSE_WAIT, SOCK_LAST_ACK Close(sockreg) USART.Write("SOCKET(",DecToStr(sockreg),") Closed",13,10) End Select If SockTimeout() Then Disconnect(sockreg) RXBuffClean() SockTimeoutDisable() End If End While
This is the request that the client sends to the web service when requesting the Command method:
<?xml version="1.0" encoding="utf-16"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <soap:Body> <Command xmlns="http://tempuri.org/"> <cCommand>0</cCommand> <cParm>0</cParm> </Command> </soap:Body> </soap:Envelope>
This is the PIC response at the Command method request:
ResponseCode: 200 (OK) Content-Type:text/xml; charset=utf-8 <?xml version="1.0" encoding="utf-16"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <soap:Body> <CommandResponse xmlns="http://tempuri.org/"> <CommandResult>true</CommandResult> </CommandResponse> </soap:Body> </soap:Envelope>
This is the request that the client sends to the web service when requesting the Status method:
<?xml version="1.0" encoding="utf-16"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <soap:Body> <Status xmlns="http://tempuri.org/" /> </soap:Body> </soap:Envelope>
This is the PIC response at the Status method request:
ResponseCode: 200 (OK) Content-Type:text/xml; charset=utf-8 <?xml version="1.0" encoding="utf-16"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <soap:Body> <StatusResponse xmlns="http://tempuri.org/"> <StatusResult> <PointsNum>0</PointsNum> <Chiamate>0</Chiamate> <InAttesa>0</InAttesa> <Visore>0</Visore> <Buzzer>false</Buzzer> <Flash>false</Flash> <ListaChiamate /> <ListaAttese> <int>10</int> <int>1</int> <int>2</int> </ListaAttese> </StatusResult> </StatusResponse> </soap:Body> </soap:Envelope>
You can download modules and examples from the following link: WIZ811MJ/W5100