The InterruptPortMM class is designed to monitor the state (high or low) of a GPIO pin on the Raspberry Pi and send an event through a designated .NET delegate when the state changes to a pre-defined trigger level. All access is via a Memory Mapped access method. There is no version of this class which uses SYSFS access.
These are not true interrupts. They have much more in common with a polled event although the event will "interrupt" the Main() thread and execute code in an object listening on them. All GPIOs are associated with one of two banks. The act of creating an InterruptPort on a GPIO will start an event monitoring thread for that bank. This GPIO bank event monitoring thread is continuously computable and there is only one thread per bank.
The InterruptPort registers with the GPIO bank event monitoring thread and when a pin state change of significance is detected the SendInterruptEvent function in this class will be called. The SendInterruptEvent function will place the relevant information into an EventData object and then send it on to anybody listening on the InterruptPorts OnInterrupt delegate
Be aware that the OnInterrupt handler method which accepts the data from the C# event is executing in the Gpio banks event monitoring thread - not the main() thread. You have to be careful what you do in there to avoid the many issues associated with multi-threaded applications. Also, it is not advisable to take too long to process the incoming event data - you cannot receive another interrupt while processing that one.
PRIORITYS: The GPIO bank event monitoring thread will detect changes and then run down its list of registered InterruptPort until it finds the correct one for that pin state change. The order of these registered InterruptPorts is the Priority - highest first. Thus, if two pins change state simultaneously then only one will get processed. If on a subsequent pass the second interrupts pin state is still changed, it will get processed then. If it has changed back it will not be processed and the interrupt event will be lost.
CLOSING DOWN: Be aware that a call to DisableInterrupt() does not stop the worker thread. You need to do an explicit ClosePort() call to stop the thread and release any resources the InterruptPort is using. Always explicitly Dispose() all ports when you are done with them.
IMPORTANT NOTE: Before creating any RPICSIO Port class you will need to ensure that the RPICSIO Library knows which type of Raspberry Pi you are dealing with. This is done by executing a call (only once is necessary) like the one below somewhere in your code before you create any port.
RPICSIOConfig.Instance.RPIType = RPITypeEnum.RPITYPE_RPI2;
public class InterruptPortMM : PortMM, IComparable, IDisposable
public InterruptPortMM (GpioEnum gpioIn, InterruptMode eventInterruptModeIn) : base(gpioIn)
public InterruptPortMM (GpioEnum gpioIn, InterruptMode eventInterruptModeIn, int evPriorityIn, int evCodeIn) : base(gpioIn)
public event InterruptEventHandlerMM OnInterrupt;
public void ClearInterrupt()
public override void ClosePort()
public void DisableInterrupt()
public void Dispose()
public void EnableInterrupt()
public override PortDirectionEnum PortDirection()
public int InterruptEventPriority { get; set; }
public int EvCode { get; }
public InterruptMode EventInterruptMode { get; set; }
public GpioConfig GpioCfgObject { get; }
public GpioEnum GpioID { get; }
public int InterruptEventsReceived { get; set; }
public int InterruptEventsSent { get; set; }