Hi,
Trying to test ENC28 with G120HDR V2.0
When i make blue led blink, i can deploy and run the project witout any problem.
Adding some code to test the ENC28 1.1 module, i am not sure about the socket but it seems that SPI socket is wired to SPI2 (it will perfet to have a detailed spec of the HDR V2 pinout).
When i try to run the code the debbuger is going to Eth1.Open(); and then hangs. Futher i can reset the G120HDR but it seems to jump into an infinite loop, and the only way to make it alive again is to reload the firmware.
It seems that i have missed something.
Here is the code, running with .Net framework 4.2 , G120 with 02/14/13 Firmware
regards
Loïc
using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.Net;
using System.Threading;
using GHI.Premium.Net;
using System.Text;
using System.Net;
using GHI.Premium.Hardware;
namespace Test_Application1
{
public class Program
{
static OutputPort output;
static Timer stateTimer;
static EthernetENC28J60 Eth1;
// if you were to use ENC28J60-based Ethernet connection16.
//static EthernetENC28J60 Eth1 = new EthernetENC28J60(add code here to configure it);
// wifi is same thing18.
// static WiFiRS9110 wifi = new WiFiRS9110(......);
static byte[] outBuffer;
static public ManualResetEvent NetworkAvailablityBlocking = null;
static public ManualResetEvent IPAddressSetResetEvent = null;
static string myIP = "192.168.240.166";
public static void Main()
{
Debug.Print("Test");
output = new OutputPort(GHI.Premium.Hardware.G120.Pin.P1_5, false);
//---- timer tick
TimerCallback timerDelegate = new TimerCallback(TimerTick);
AutoResetEvent autoEvent = new AutoResetEvent(false);
stateTimer = new Timer(timerDelegate, autoEvent, 400, 171);
//---- timer tick1 for a nice effect frequency blinking blue led
TimerCallback timerDelegate1 = new TimerCallback(TimerTick1);
AutoResetEvent autoEvent1 = new AutoResetEvent(false);
stateTimer = new Timer(timerDelegate1, autoEvent1, 500, 175);
//--- Some sample grabed on the net, hope the GPIO pin are ok
Eth1 = new EthernetENC28J60(SPI.SPI_module.SPI2, G120.Pin.P1_9, G120.Pin.P2_4, G120.Pin.GPIO_NONE);
InitHttp();
while (true)
{
Thread.Sleep(12500);
}
}
static public void TimerTick(Object stateInfo)
{
// TODO: Do Timer Tick stuff here...
output.Write(!output.Read());
}
static public void TimerTick1(Object stateInfo)
{
// TODO: Do Timer Tick stuff here...
output.Write(!output.Read());
}
public static void InitHttp()
{
Eth1.Open(); // its hanging here ...
NetworkInterfaceExtension.AssignNetworkingStackTo(Eth1);
Eth1.NetworkInterface.EnableStaticIP(myIP, "255.255.255.0", "192.168.240.2");
//Eth1.NetworkInterface.EnableDhcp();
IPAddressSetResetEvent = new ManualResetEvent(false);
Eth1.CableConnectivityChanged += new EthernetENC28J60.CableConnectivityChangedEventHandler(Eth1_CableConnectivityChanged);
Eth1.NetworkAddressChanged += new NetworkInterfaceExtension.NetworkAddressChangedEventHandler(Eth1_NetworkAddressChanged);
if (!Eth1.IsCableConnected)
{
NetworkAvailablityBlocking = new ManualResetEvent(false);
do
{
if (!Eth1.IsCableConnected)
{
Debug.Print("Ethernet cable is not connected yet.");
}
else
break;
} while (!NetworkAvailablityBlocking.WaitOne(5000, false));
}
while (!IPAddressSetResetEvent.WaitOne(500, false))
{
Debug.Print("IP address is not set yet.");
}
Debug.Print("IP address is set");
string str = Resources.GetString(Resources.StringResources.String1);
outBuffer = Encoding.UTF8.GetBytes(str);
try
{
Debug.Print("Running HttpServer");
Debug.Print("Type this IP address to access the webpage: " + Eth1.NetworkInterface.IPAddress);
RunServer();
}
catch (Exception e)
{
Debug.Print(e.Message);
}
}
static void Eth1_CableConnectivityChanged(object sender, EthernetENC28J60.CableConnectivityEventArgs e)
{
Debug.Print("Built-in Ethernet Cable is " + (e.IsConnected ? "Connected!" : "Disconneced!"));
if (e.IsConnected)
NetworkAvailablityBlocking.Set();
}
static void Eth1_NetworkAddressChanged(object sender, EventArgs e)
{
Debug.Print("New address for The built-in Ethernet Network Interface ");
Debug.Print("Is DhCp enabled: " + Eth1.NetworkInterface.IsDhcpEnabled);
Debug.Print("Is DynamicDnsEnabled enabled: " + Eth1.NetworkInterface.IsDynamicDnsEnabled);
Debug.Print("NetworkInterfaceType " + Eth1.NetworkInterface.NetworkInterfaceType);
Debug.Print("Network settings:");
Debug.Print("IP Address: " + Eth1.NetworkInterface.IPAddress);
Debug.Print("Subnet Mask: " + Eth1.NetworkInterface.SubnetMask);
Debug.Print("Default Getway: " + Eth1.NetworkInterface.GatewayAddress);
Debug.Print("Number of DNS servers:" + Eth1.NetworkInterface.DnsAddresses.Length);
for (int i = 0; i < Eth1.NetworkInterface.DnsAddresses.Length; i++)
Debug.Print("DNS Server " + i.ToString() + ":" + Eth1.NetworkInterface.DnsAddresses[i]);
Debug.Print("------------------------------------------------------");
if (Eth1.NetworkInterface.IPAddress == myIP || Eth1.NetworkInterface.IPAddress != "0.0.0.0")
IPAddressSetResetEvent.Set();
}
internal static void RunServer()
{
HttpListener listener = new HttpListener("http");
listener.Start();
while (true)
{
HttpListenerResponse response = null;
HttpListenerContext context = null;
try
{
context = listener.GetContext();
response = context.Response;
HttpListenerRequest request = context.Request;
switch (request.HttpMethod.ToUpper())
{
case "GET": ProcessClientGetRequest(context);
break;
}
if (response != null)
{
response.Close();
}
}
catch
{
if (context != null)
{
context.Close();
}
}
}
}
private static void ProcessClientGetRequest(HttpListenerContext context)
{
HttpListenerRequest request = context.Request;
HttpListenerResponse response = context.Response;
Debug.Print(request.RawUrl);
response.OutputStream.Write(outBuffer, 0, outBuffer.Length);
}
}
}