InterruptPort Event in NETMF

Hardware: FEZ Cerbuino Bee
Software: NETMF 4.2
Project: Plain NETMF Project.

I use a OutputPort to simulate the button press and use InterruptPort to test the InterruptPort Event.
Below are two cases.

Case1 works but Case2 not.

But if I remove the

 byte[] info = new byte[10 * 1024 - 128];

Case1 and Case2 all work.

Why?

Case1:

    public class Program
    {
        public static void Main()
        {

            InterruptPort IntButton = new InterruptPort(FEZCerbuino.Pin.Digital.D2, false,
                                                        Port.ResistorMode.PullDown,
                                                        Port.InterruptMode.InterruptEdgeHigh);

            IntButton.OnInterrupt += new NativeEventHandler(IntButton_OnInterrupt);
            //Init();
            Debug.Print("start");

            OutputPort DOut;
            DOut = new OutputPort(FEZCerbuino.Pin.Digital.D1, true);

            while (true)
            {


                byte[] info = new byte[10 * 1024 - 128];
                DOut.Write(true);
                Thread.Sleep(500);

                DOut.Write(false);
                Thread.Sleep(500);
                Debug.Print("output");
            }
        }

        static void IntButton_OnInterrupt(uint data1, uint data2, DateTime time)
        {
            Debug.Print("Button Pressed");
        }

        static void Init()
        {
            InterruptPort IntButton = new InterruptPort(FEZCerbuino.Pin.Digital.D2, false,
                                                        Port.ResistorMode.PullDown,
                                                        Port.InterruptMode.InterruptEdgeHigh);

            IntButton.OnInterrupt += new NativeEventHandler(IntButton_OnInterrupt);
        }

    }

Case2:

 public class Program
    {
        public static void Main()
        {

            //InterruptPort IntButton = new InterruptPort(FEZCerbuino.Pin.Digital.D2, false,
            //                                            Port.ResistorMode.PullDown,
            //                                            Port.InterruptMode.InterruptEdgeHigh);

            //IntButton.OnInterrupt += new NativeEventHandler(IntButton_OnInterrupt);
            Init();
            Debug.Print("start");

            OutputPort LED;
            LED = new OutputPort(FEZCerbuino.Pin.Digital.D1, true);

            while (true)
            {


                byte[] info = new byte[10 * 1024 - 128];
                LED.Write(true);
                Thread.Sleep(500);

                LED.Write(false);
                Thread.Sleep(500);
                Debug.Print("output");
            }
        }

        static void IntButton_OnInterrupt(uint data1, uint data2, DateTime time)
        {
            Debug.Print("Button Pressed");
        }

        static void Init()
        {
            InterruptPort IntButton = new InterruptPort(FEZCerbuino.Pin.Digital.D2, false,
                                                        Port.ResistorMode.PullDown,
                                                        Port.InterruptMode.InterruptEdgeHigh);

            IntButton.OnInterrupt += new NativeEventHandler(IntButton_OnInterrupt);
        }

    }

Define “not working”. And move allocation of the byte array out of the loop.

“Not working” mean IntButton_OnInterrupt do not raise.
why can not put allocation of the byte array in the loop?

I would guess your getting an out of memory exception error on the byte array line thats causing it to fall out, this would cause it all to stop.

Does the LED blink for a short while and then stop or does it never work?

Out of curiosity change the line in the function init to say

IntButton.OnInterrupt += IntButton_OnInterrupt;

Does it work now?

Paul

you’re allocating too much memory each time around. How much free memory do you have to start with? Each loop is allocating the better part of ~10k and you’re doing it every second.

The LED is no a LED, sorry, I change the name.
Actually,I wire the D1 and D2 in FEZ Cerbuino Bee.

@ Bodwad
The same.

@ Brett
After I change the array to small [1024], it works in fist several runs but stop after later.

Debug.output

start
Button Pressed
output
Button Pressed
output
Button Pressed
output
Button Pressed
output
Button Pressed
output
Button Pressed
output
Button Pressed
output
Button Pressed
output
Button Pressed
output
Button Pressed
output
Button Pressed
output
Button Pressed
output
Button Pressed
output
Button Pressed
output
Button Pressed
output
Button Pressed
output
Button Pressed
output
Button Pressed
output
Button Pressed
output
Button Pressed
output
output
output
output
output
output
output
output
output
output
output
output
output
output
output
output
output
output
output
output
output
output
output
output

Why are you allocating it inside the loop?

Actually, it is not my application.
My friend meet this problem in his application.
His board is not GHI’s, So he want to make sure the problem is the hardware or the NETMF.
So he ask me to check in GHI’s Board.

OK, I remove the array allocation.
interesting, the result is the same,
it works in fist several runs but stop after later.

OK
I add IntButton.OnInterrupt -= new NativeEventHandler(IntButton_OnInterrupt);in the Init()

Now, it works.
My friend guess the Problem is the GC?

static void Init()
        {
            InterruptPort IntButton = new InterruptPort(FEZCerbuino.Pin.Digital.D2, false,
                                                        Port.ResistorMode.PullDown,
                                                        Port.InterruptMode.InterruptEdgeHigh);
            IntButton.OnInterrupt -= new NativeEventHandler(IntButton_OnInterrupt);

            IntButton.OnInterrupt += new NativeEventHandler(IntButton_OnInterrupt);
        }

Irrespective of the device, you’ll deplete memory quickly. If you force GC to run in the loop you might get it to run more successfully. But its very clearly the framework getting to a memory depletion point that means it can’t run the handler.

The problem is that in Case 2 your IntBitton is local to Init method. As soon as Init is done the IntButton is due for garbage collection and eventually will be collected.

Am also meet the same problem…when am run inside the while loop, Interrupt handler doesn’t work but if the while loop breaked means the interrupt getting triggered…what is the problem…?

Regards YuvaRaja

Can you post a short example of your code so that we can see what your trying to do?

Thanks,

Paul

This is my example code…this code fine but in a large code interrupt not triggered… i think the GC remove the reference…
port.OnInterrupt += new NativeEventHandler(Port_OnInterrupt);
//LIS302DL Accelerometer = new LIS302DL((Cpu.Pin)67);
while (true)
{
led1.Write(true);
Thread.Sleep(1000);
led1.Write(false);
Thread.Sleep(1000);
}

private static void Port_OnInterrupt(uint data1, uint data2, DateTime time)
{
led.Write(true);
Thread.Sleep(500);
led.Write(false);
}

Regards YuvaRaja

It is hard to say from this example. How and where the port is allocated?

Architect, the above example work well but interrupt doesn’t trigger in a large amount of code…

Am overcome that problem using this code.

port.OnInterrupt += new NativeEventHandler(Port_OnInterrupt);
port.OnInterrupt -= new NativeEventHandler(Port_OnInterrupt);
port.OnInterrupt += new NativeEventHandler(Port_OnInterrupt);

this code initialize and un initialize the event handler in specific time

Regards YuvaRaja

By itself adding and removing handlers is harmless. The problem must be somewhere else in the “large amount of code”.