Hi,
In todays post(s) I thought I'd try demonstrate how to set up one of the W5100's four sockets in TCP mode and how to read data that is received. In the end we'll be able to see the data that a web browser sends when it tries to reach the W5100.
The W5100 can have four connections, or sockets as it called, open simultanously. What this means is that you can have one socket setup to listen for TCP trafic on port 80 (which is the standard port for HTTP) thus acting as a webserver. At the same time you can have another socket connect TO a server and send an email while a third socket connects to a NTP server and retrieves the correct time.
For now we'll concentrate on a single socket though.
First order of business is to initialize the W5100, like before this is done by performing a soft reset and then write to its SHAR, SIPR, SUBR and GAR registers to set the MAC-adress, IP-adress, net-mask and gateway adress. Setting the gateway adress isn't strictly needed for this to work but I did that anyway. For now I kept all the settings "hardcoded" but in a real device these would obviously have to be changable thru some kind of userinterface and stored in EEPROM or whatever.
Code:
'******************************************************************************************
Init_W5100:
'Perform a soft reset of the W5100
WizAdress = W5100_Mode : WizData = 128 : Gosub Write_W5100
' Set W5100 MAC adress
WizAdress = W5100_SHAR0 : WizData = $90 : GOSUB Write_W5100
WizAdress = W5100_SHAR1 : WizData = $A2 : GOSUB Write_W5100
WizAdress = W5100_SHAR2 : WizData = $DA : GOSUB Write_W5100
WizAdress = W5100_SHAR3 : WizData = $00 : GOSUB Write_W5100
WizAdress = W5100_SHAR4 : WizData = $48 : GOSUB Write_W5100
WizAdress = W5100_SHAR5 : WizData = $03 : GOSUB Write_W5100
' Set W5100 IP adress
WizAdress = W5100_SIPR0 : WizData = 192 : Gosub Write_W5100
WizAdress = W5100_SIPR1 : WizData = 168 : Gosub Write_W5100
WizAdress = W5100_SIPR2 : WizData = 1 : Gosub Write_W5100
WizAdress = W5100_SIPR3 : WizData = 50 : Gosub Write_W5100
' Set W5100 Net mask
WizAdress = W5100_SUBR0 : WizData = 255 : GOSUB Write_W5100
WizAdress = W5100_SUBR1 : WizData = 255 : GOSUB Write_W5100
WizAdress = W5100_SUBR2 : WizData = 255 : GOSUB Write_W5100
WizAdress = W5100_SUBR3 : WizData = 0 : GOSUB Write_W5100
' Set W5100 gateway adress
WizAdress = W5100_GAR0 : WizData = 192 : GOSUB Write_W5100
WizAdress = W5100_GAR1 : WizData = 168 : GOSUB Write_W5100
WizAdress = W5100_GAR2 : WizData = 1 : GOSUB Write_W5100
WizAdress = W5100_GAR3 : WizData = 254 : GOSUB Write_W5100
RETURN
'******************************************************************************************
Next task is to setup socket 0 to listen for TCP trafic on port 80. To do this we first write the value 1 to the sockets Mode register, this sets the protocol for the socket to TCP. Then we write the value 80 to the sockets Source Port register and then tell the W5100 to open the socket. Once the socket is open we can finally switch it into Listen mode which is what we want when acting as a server.
Code:
'******************************************************************************************
Init_Socket_0:
' Set socket 0 protocol to TCP by assigning the sockets mode register the value 1.
WizAdress = W5100_S0_MR : WizData = 1 : GOSUB Write_W5100
' Set the port register of socket 0.
WizAdress = W5100_S0_PORT0 : WizData = 0 : Gosub Write_W5100
WizAdress = W5100_S0_PORT1 : WizData = 80 : GOSUB Write_W5100
Open_Socket_0:
' Set the Command Register of socket 0 to OPEN - this inits the socket.
WizAdress = W5100_S0_CR : WizData = W5100_Sn_OPEN : GOSUB Write_W5100
' Now switch Socket 0 into Listen Mode.
WizAdress = W5100_S0_CR : WizData = W5100_Sn_LISTEN : GOSUB Write_W5100
RETURN
'******************************************************************************************
Once the socket is in Listen mode it will automatically switch to Established mode when it receives a connection request from a client. We can check in which mode the socket is by reading the sockets Status register.
Code:
HSEROUT ["Program Start", 13]
Main:
WizAdress = W5100_S0_SR : GOSUB Read_W5100
HSEROUT["Socket 0 status = $", HEX WizData,13]
Pause 500
Goto Main
If you run this code with a serial terminal open and type in the W5100's IP-adress in a browser you should see the sockets status register changing from $14 (Listen) to $17 (Established):
...to be continued in the next post.
/Henrik.
Bookmarks