[ '-----------------------------------------------------------------------------------------
'name : dhcp_W5500.bas
'copyright : (c) 1995-2017, MCS Electronics. Based on dhcp.bas from Atilio Mosca
'purpose : demo: DHCP
'micro : Mega88
'suited for demo : no
'commercial addon needed : only hardware
'-----------------------------------------------------------------------------------------
$regfile = "M2560def.dat" 'ATMEGA2560 (TQFP)
$crystal = 7372800 'oscillator speed (MHz)
Config Clockdiv = 1 'clock divider
Config Clock = User 'use compiler time functions w/ user set time & date
$loadersize = 4096
$hwstack = 200 'hardware stack size (30)
$swstack = 200 'software stack size (51)
$framesize = 2000 'frame size (1419)
'$lib "stackcheck.lib"
$baud = 19200 ' use baud rate
$hwstack = 128 ' default use 32 for the hardware stack
$swstack = 128 ' default use 10 for the SW stack
$framesize = 128 ' default use 40 for the frame space
Declare Function Xidcookie_ok() As Byte
Declare Function Parse_dhcp_msg(byval Doption As Byte) As Byte
Declare Function Dhcp_ok() As Byte
Declare Sub Print_parse()
Dim Mac_add(6) As Byte
' result
Tcp_cs Alias Porth.3 : Set Tcp_cs 'Ethernet chip select
status Alias portd.6 : Set status
reset status
'This number should match the one in the 'Config Tcpip...' line
'Do not use first byte
Mac_add(1) = 0
Mac_add(2) = 128
Mac_add(3) = 12
Mac_add(4) = 34
Mac_add(5) = 56
Mac_add(6) = 78
Config Spi = Hard , Interrupt = Off , Data Order = Msb , Master = Yes , Polarity = High , Phase = 1 , Clockrate = 4 , Noss = 0
Spsr = 1
Spiinit
'Print "Init , set IP to 192.168.1.70" ' display a message
Config Tcpip = Noint , Mac = Mac_add(1) , Ip = 10.0.16.56 , Submask = 255.255.255.0 , Gateway = 10.0.16.254 , Localport = 1000 , Chip = W5500 , Spi = 1 , Cs = Tcp_cs
Dim Bclient As Byte ' socket number
Dim Idx As Byte
Dim Result As Word , Result2 As Word , Result3 As Word ' result
Dim S As String * 80
Dim Flags As Byte
Dim Peer As Long
Dim L As Long
Do
Waitms 1000
For Idx = 0 To 3
Result = Socketstat(idx , 0) ' get status
Select Case Result
Case Sock_established
If Flags.idx = 0 Then ' if we did not send a welcome message yet
Flags.idx = 1
Result = Tcpwrite(idx , "Hello from W5200A{013}{010}") ' send welcome
End If
Result = Socketstat(idx , Sel_recv) ' get number of bytes waiting
If Result > 0 Then
Result3 = Tcpwrite(idx , "Hello")
Do
Result3 = Tcpwrite(idx , "here")
set status
Result = Tcpread(idx , S)
reset status
Result3 = Tcpwrite(idx , "Pass")
If Lcase(s) = "exit" Then
Result2 = Tcpwrite(idx , "exit" )
Elseif Lcase(s) = "time" Then
Result2 = Tcpwrite(idx , "12:00:00{013}{010}") ' you should send date$ or time$
End If
Loop Until Result = 0
End If
Case Sock_close_wait
Closesocket Idx
Case Sock_closed
Bclient = Getsocket(idx , Sock_stream , 5000 , 0) ' get socket for server mode, specify port 5000
Socketlisten Idx
' Print "Result " ; Result
Flags.idx = 0 ' reset the hello message flag
End Select
Next Idx
Loop
End] |