G120E Gpio to Cpu.Pins map

How do you map between G120E Gpio ports and Cpu.Pins? The reason I am asking is the following code crashes when I try to use interrupts on G120E.Gpio.Pn_xx ports, but work when I use Cpu.Pin.GPIO_Pinxx? Is there a function I can call to do the job or do I need to look at the board schematic?

Here is the failing code:


using Microsoft.SPOT.Hardware;
using System;
using GHI.Pins;

public class Program
{
   public static void Main()
   {
     // Code crashes right here:
      var interrupt = new InterruptPort(G120E.Gpio.P4_28, false, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeHigh);
      interrupt.OnInterrupt += interrupt_OnInterrupt;
      PowerState.Sleep(SleepLevel.DeepSleep, HardwareEvent.OEMReserved1);
      ///Continue on with your program here
   }
   private static void interrupt_OnInterrupt(uint data1, uint data2, DateTime time)
   {
      //Interrupted
   }
}

When I modify the code and replace the G120E.Gpio.P4_28 with a Cpu pin number it works:

Here is the working code:


using Microsoft.SPOT.Hardware;
using System;
using GHI.Pins;

public class Program
{
   public static void Main()
   {
      var interrupt = new InterruptPort(Cpu.Pin.GPIO_Pin0, false, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeHigh);
      interrupt.OnInterrupt += interrupt_OnInterrupt;
      PowerState.Sleep(SleepLevel.DeepSleep, HardwareEvent.OEMReserved1);
      ///Continue on with your program here
   }
   private static void interrupt_OnInterrupt(uint data1, uint data2, DateTime time)
   {
      //Interrupted
   }
}

Thanks.

Point well taken.
As a newbie, I am learning the hard way!
Technically, I should be getting an answer to my question soon.

Thanks.

Technically, you haven’t told us what “crashes” means. To help us you’d really need to understand what happens, when it happens, and what the result is.

Also, as a general rule, not all pins will be interrupt capable in all devices. That means that at some point, you will need to explore the datasheet to find what ones are and what are not.

When I step over the following line using the debugger, I get an exception:


      interrupt.OnInterrupt += interrupt_OnInterrupt;

Error:

The thread ‘’ (0x2) has exited with code 0 (0x0).
#### Exception System.ArgumentException - 0xfd000000 (1) ####
#### Message:
#### Microsoft.SPOT.Hardware.InterruptPort::EnableInterrupt [IP: 0000] ####
#### Microsoft.SPOT.Hardware.NativeEventDispatcher::add_OnInterrupt [IP: 0027] ####
#### Program::Main [IP: 0019] ####
A first chance exception of type ‘System.ArgumentException’ occurred in Microsoft.SPOT.Hardware.dll
A first chance exception of type ‘System.ArgumentException’ occurred in Microsoft.SPOT.Hardware.dll
An unhandled exception of type ‘System.ArgumentException’ occurred in Microsoft.SPOT.Hardware.dll

As far as G120E is concerned, where do I find the information on Interruptible pins? I am assuming interruptible pins have to be directly associated with the Cpu pins and there are only 16 of them. What is the one-to-one map of G120E GPIO’s to Cpu pins?

Thanks.

https://www.ghielectronics.com/downloads/man/G120_and_G120E_Datasheet.pdf

See section 8.4, which says “Only pins on ports 0 and 2 support interrupts”.

The other thing I thought about your code that I thought I’d point out is the GHI.Pins definition technically is a Cpu.Pin definition. You can check in Object Browser and see that. So that reinforces the fact that the cause of this was using a non-interrupt capable pin, it had nothing to do with the difference in Cpu.Pin vs GHI.Pins, it was just that you selected a pin that was interrupt capable in your second example…

You are right! After I change the port number to the ones on P0 and P2, it started working.
Thanks a bunch.

1 Like