Cannot reset the ENC28 using the RESET pin

On the Raptor board, using a G400 module, how do I reset the ENC28J60 Ethernet module? I tried this, but calling new OutputPort(ResetPin, true) throws an exception. The ENC28 is plugged in to socket #3.

Code Language: C#

private void ResetENC28()
{
try
{
Cpu.Pin ResetPin = G400.PC23;
OutputPort ResetPort = new OutputPort(ResetPin, true );
ResetPort.Write(false);
Thread.Sleep(10);
ResetPort.Write(true);
}
catch ( Exception ex )
{
}
}

I would try this

private GTI.DigitalOutput reset;
Socket socket = Socket.GetSocket(3, true, this, null);
reset = GTI.DigitalOutputFactory.Create(socket, Socket.Pin.Four, false, this);

It throws an exception. This is my version:


 private void ResetENC28()
            {
                try
                {
                    GTI.DigitalOutput reset;
                    Gadgeteer.Socket socket = Gadgeteer.Socket.GetSocket(3, true, ethernet_ENC28, null);
                    reset = GTI.DigitalOutputFactory.Create(socket, Gadgeteer.Socket.Pin.Four, false, ethernet_ENC28);
                    Thread.Sleep(20);
                    reset.Write(true);
                }
                catch ( Exception ex )
                {
                }
             }

The exception is thrown when GTI.DigitalOutputFactory.Create is called. I get a System.Exception.

Perhaps it’s because the pin is already occupied by the instance of the EthernetENC28J60 class

see link:

https://www.ghielectronics.com/community/forum/topic?id=12342&page=21#msg164363

at post #201

just a housekeeping request. Please don’t double post. Your other post was in a G120 thread, so was misplaced because your question was about G400 / Raptor. Plus, that thread was months old; and not related to your issue at all. A single new thread is always a much better idea unless you have a 100% match of the situation

Sorry, I put my answer in the wrong forum thread.

The Gadgeteer Designer in Visual Studio creates the file Program.Generated.cs, and this code is written for me:


this.ethernetENC28 = new GTM.GHIElectronics.EthernetENC28(3);

The parameter 3 is the socket number the ENC28 module is plugged into on the Raptor board. There is no way to pass Cpu.Pin.GPIO_NONE. Or I am missing something.

I saw an example where the module was not added with the designer.
It used the namespace GHI.Networking;

 
using GHI.Networking;

private static readonly EthernetENC28J60 netIf
             = new EthernetENC28J60(SPI.SPI_module.SPI1, (Cpu.Pin)28, (Cpu.Pin)33, Cpu.Pin.GPIO_NONE);

@ dspacek - As RoSchmi said, If you use the Gadgeteer designer, there is no way to not pass the reset pin since we manage it all in the Gadgeteer driver. If you create the GHI.Networking.EthernetENC28 object yourself you can pass GPIO_None for the reset pin. https://www.ghielectronics.com/docs/30/networking might be helpful.

I commented out the ethernetENC28 construction in the program.generated.cs:


//this.ethernetENC28 = new GTM.GHIElectronics.EthernetENC28(3);

In my application thread, I created my new EthernetENC28J60 object for the Raptor board with a ENC28 module plugged into socket #3:


          //eth = _ethernet_ENC28.NetworkInterface;  //Normally use the Gadgeteer module
            //This object needed so I can control the reset pin to the ENC28
            eth = new GHINET.EthernetENC28J60(SPI.SPI_module.SPI1, G400.PA28, G400.PB1, Cpu.Pin.GPIO_NONE);  //Used by Raptor ENC28 ethernet module in socket 3
            ni = null;

Then I tried calling my function to reset the ENC28, and it throws an exception.


          private void ResetENC28()
            {
                try
                {
                    OutputPort reset = new OutputPort(G400.PC23, true);
                    reset.Write(false);
                    Thread.Sleep(20);
                    reset.Write(true);
                }
                catch ( Exception ex )
                {
                    Debug.Print("ResetENC28 Exception: " + ex.ToString());
                }
             }


ResetENC28 Exception: System.Exception
#### Exception System.Exception - CLR_E_PIN_UNAVAILABLE (65) ####
#### Message:
#### Microsoft.SPOT.Hardware.Port::.ctor [IP: 0000] ####
#### Microsoft.SPOT.Hardware.OutputPort::.ctor [IP: 0006] ####
#### XGadgeteer.Networking.WebServerManager+Server::ResetENC28 [IP: 0008] ####
#### XGadgeteer.Networking.WebServerManager+Server::PolledListen [IP: 003c] ####
#### XGadgeteer.Networking.WebServerManager+Server::ProcessRequest [IP: 0014] ####
A first chance exception of type ‘System.Exception’ occurred in Microsoft.SPOT.Hardware.dll

@ dspacek - If you are passing GPIO_NONE for the reset pin to the ENC28 constructor, that pin is likely used somewhere else in your program.

Is it possible to reset the module without exception before you create the instance of the EthernetENC28J60 Class?

The EthernetENC28J60 Class has a dispose method.
Perhaps it works if you first call the dispose method and then try to reset.

Yes, it Is possible to reset the module without exception before I create the instance of the EthernetENC28J60 Class. But after I create the EthernetENC28J60 class, and dispose of it, and do a Garbage Collection, when I run OutputPort reset = new OutputPort(G400.PC23, true); an exception ex2 is thrown:

Exception System.Exception - CLR_E_PIN_UNAVAILABLE (5)

#### Message: 
#### Microsoft.SPOT.Hardware.Port::.ctor [IP: 0000] ####
#### Microsoft.SPOT.Hardware.OutputPort::.ctor [IP: 0006] ####
#### SmartTruckRaptor.PrototypeDevice::.ctor [IP: 00c7] ####
#### SmartTruckRaptor.Program::ProcessCommandsLoop [IP: 003f] ####

A first chance exception of type ‘System.Exception’ occurred in Microsoft.SPOT.Hardware.dll

I commented out the Gadgeteer Designer ENC28 object in program.generated.cs before calling this:



            try
            {
                OutputPort reset = new OutputPort(G400.PC23, true);
                reset.Write(false);
                Thread.Sleep(20);
                reset.Write(true);
            }
            catch (Exception ex1)
            {
                Debug.Print("ResetENC28 Exception: " + ex1.ToString());
            }

            eth = new GHINET.EthernetENC28J60(SPI.SPI_module.SPI1, G400.PA28, G400.PB1, Cpu.Pin.GPIO_NONE);  //Used by Raptor ENC28 ethernet module in socket 3
            Thread.Sleep(100);
            eth.Dispose();
            Debug.GC(true);
            Thread.Sleep(100);

            try
            {
                OutputPort reset = new OutputPort(G400.PC23, true);
                reset.Write(false);
                Thread.Sleep(20);
                reset.Write(true);
            }
            catch (Exception ex2)
            {   //Exception here always
                Debug.Print("ResetENC28 Exception: " + ex2.ToString());
            }



and if you create OutputPort reset = new …
outside the try block?

Why would I do that? I will just get an unhandled exception.

to have only one OutputPort reset “connected” to G400.PC23

So you want me to try this code? If not, please show me the code you want me to try.


                 OutputPort reset = new OutputPort(G400.PC23, true);
                 reset.Write(false);
                 Thread.Sleep(20);
                 reset.Write(true);

             eth = new GHINET.EthernetENC28J60(SPI.SPI_module.SPI1, G400.PA28, G400.PB1, Cpu.Pin.GPIO_NONE);  //Used by Raptor ENC28 ethernet module in socket 3
             Thread.Sleep(100);
             eth.Dispose();
             Debug.GC(true);
             Thread.Sleep(100);

                  reset = new OutputPort(G400.PC23, true);
                 reset.Write(false);
                 Thread.Sleep(20);
                 reset.Write(true);

I tried this code, I got the same exception as before:


           try
            {
                OutputPort reset = new OutputPort(G400.PC23, true);
                reset.Write(false);
                Thread.Sleep(20);
                reset.Write(true);

                eth = new GHINET.EthernetENC28J60(SPI.SPI_module.SPI1, G400.PA28, G400.PB1, Cpu.Pin.GPIO_NONE);  //Used by Raptor ENC28 ethernet module in socket 3
                Thread.Sleep(100);
                eth.Dispose();
                Debug.GC(true);
                Thread.Sleep(100);

                reset = new OutputPort(G400.PC23, true);
                reset.Write(false);
                Thread.Sleep(20);
                reset.Write(true);
            }
            catch (Exception ex)
            {
                string msg = ex.Message;
            }


:think: