Code
program Ethernet_TwentyTen;
{$NOSHADOW}
{ $WG} {global Warnings on}
{$DEFINE Enable_W5100}
{$DEFINE Enable_TCP}
{ $DEFINE Enable_UDP}
Device = mega328p, VCC=5;
{ $BOOTRST $03800} {Reset Jump to $03800}
Define_Fuses
Override_Fuses;
NoteBook = A;
COMport = USB;
LockBits0 = [];
FuseBits0 = [CKSEL0];
FuseBits1 = [];
FuseBits2 = [];
ProgMode = SPI;
ProgFuses = true;
ProgLock = false;
ProgFlash = true;
ProgEEprom = false;
Import SysTick, WatchDog, SerPort, SPIdriver, TWImaster;
From System Import ;
From System Import LongWord, LongInt;
Define
ProcClock = 16000000; {Hertz}
SysTick = 10; {msec}
WatchDog = msec125; {presc = 3}
StackSize = $0064, iData;
FrameSize = $0064, iData;
SerPort = 38400, Stop2; {Baud, StopBits|Parity}
RxBuffer = 8, iData;
TxBuffer = 8, iData;
TWIpresc = TWI_BR100;
SPIMode = 0 ;
SPIpresc = 0;
SPIOrder = MSB;
SPI_SS = true;
{
// FAT16 = SPI_C, PortA, 6, iData; // PortX defines the SS-Port and n (6) the SS-Pin
FAT16 = MMC_SPI, iData;
F16_FileHandles= 1;
F16_DirLevels = 1;
}
{$IFDEF Enable_W5100}
Uses
uIPGLOBAL,
uWEB,
uW5100;
// UFAT16;
// uxAP_Definitions,
// uxAP_UDP;
{$EndIf}
Implementation
{$IDATA}
{--------------------------------------------------------------}
{ Type Declarations }
type
tTCPServerBuffer = Array[0..999] of Byte;
{--------------------------------------------------------------}
{ Const Declarations }
structconst
dummy : word = $1234; // Negate possible data corruption at start of eeprom
Firmware_Version : String = '1.0.0.0' ; // From Header at the top of this file
CRLF : String = #13+#10;
WebPageContent : String = 'HTTP/1.0 200 OK'+#13+#10+'Content-type: text/plain'+#13+#10+#13+#10+'Hello World. I''m a Mega328P & W5100 running AVRCo. Hits=';
WebPageCRLF : String = #13+#10;
{--------------------------------------------------------------}
{ Var Declarations }
Var
{$IDATA}
WEBServer : tIPSocket ;
pWEBServer : pIPSocketHandle;
WEBServerBuf : tTCPServerBuffer ; // Shared for Tx & Rx
Result : Boolean ;
RedLED[@portd, 5] : bit;
St_Tx,St_Rx : String[50];
WebPageCounter : Word ;
WebPage : String[200];
{--------------------------------------------------------------}
{ functions }
procedure InitPorts;
begin
DDRB.5:= 1; // redled
end InitPorts;
Procedure StartupBlinks;
Var
I : Byte;
Begin
// WatchDogLED := OFF;
For I := 1 to 3 do
RedLED := 0;
mdelay(200);
RedLED := 1;
mdelay(200);
Endfor;
End;
{--------------------------------------------------------------}
{ Main Program }
{$IDATA}
begin
InitPorts;
EnableInts;
Writeln(Serout,'Start.');
StartupBlinks;
With IP_Core do
StrToIP('10.0.0.250', IP);
StrToIP('255.255.255.0', Mask);
StrToIP('10.0.0.1', GateWay);
StrToMac('00-A0-A1-A2-A3-A4', MAC);
ResponsePing := True;
RXBroadcast := True;
RXCheckSumCheck := True;
SendICMPCtrlMessages := False;
Endwith;
IF NOT W5100_Init(1500) then
Repeat
Writeln(Serout,'W5100 failed to respond?');
// Beepout(200,100); // W5100 failed to respond.
mdelay(1000);
Until W5100_Init(1500);
endif;
// =======================================================
// Define TCP Server Socket connection parameters
pWEBServer := @WEBServer;
With pWEBServer^ do
SocketNo := 0 ; // 0 or 1 or 2 or 3
Protocol := pTCP;
DelayAck := NoDelayedAck; // TCP Only
LocalPort := 8080;
// StrToIP('10.0.0.101', RemoteHost); // Only required for TCP Client Mode. NOT TCP Server Mode
// RemotePort := 270; // Only required for TCP Client Mode. NOT TCP Server Mode
PacketInfo.TxBufferPtr := @WEBServerBuf;
PacketInfo.RxBufferPtr := @WEBServerBuf;
PacketInfo.BufferLen := SizeOf(tTCPServerBuffer);
endwith;
//OpenSocket(pWEBServer);
//TCP_Listen(pWEBServer);
Process_TCP_State_Machine(pWEBServer) ;
// =======================================================
// WatchDogStart;
loop
If Process_TCP_State_Machine(pWEBServer) then
// We have a connection established and received data
Writeln(Serout,'Rx Data');
If TCP_Recv(pWebServer) then
Inc(WebPageCounter);
WebPage := WebPageContent + InttoStr(WebPageCounter:5) + #13+#10;
copyblock(@WebServerBuf,@St_Rx+1,Word(IP_DataAv(pWebServer)));
St_Rx[0] := Char(IP_DataAv(pWebServer));
Writeln(Serout,'Count = '+InttoStr(WebPageCounter:5));
// writeln(serout,'WEB Server Rx ='+St_Rx+'.');
// writeln(serout,'WEB Server Rx ='+inttostr(WebServer.RxPacketSize));
if compareblock(@WebServerBuf,@WEBGetCommand+1,5) then // 'GET /' received
copyblock(@WebPage+1,@WebServerBuf,Word(WebPage[0]));
TCP_Send(pWebServer,Word(WebPage[0]));
Endif;
TCP_DisConnect(pWebServer);
EndIf;
EndIf;
// WatchDogTrig;
mdelay(100); // Don't need to hammer the W5100
endloop;
end Ethernet_TwentyTen.