RPICSIO

The InterruptPortMM Class

 

The InterruptPortMM Class

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;

 

Source Code

The source code for this class is available online for download and also in browseable form.

 

Example Code

Example code which demonstrates the use of this class can be found on the Interrupt Port Example page.

 

Syntax

public class InterruptPortMM : PortMM, IComparable, IDisposable

Provides interrupt port functionality for a Raspberry Pi (Memory Mapped Version).

 

Public Constructors

public InterruptPortMM (GpioEnum gpioIn, InterruptMode eventInterruptModeIn) : base(gpioIn)

Constructor.

Parameters
gpioIDIn - The GPIO the port will access.
InterruptMode - The Interrupt Mode the port will trigger on.

 

public InterruptPortMM (GpioEnum gpioIn, InterruptMode eventInterruptModeIn, int evPriorityIn, int evCodeIn) : base(gpioIn)

Constructor.

Parameters
gpioIDIn - The GPIO the port will access.
InterruptMode - The Interrupt Mode the port will trigger on.
evPriorityIn - The event priority relative to other InterruptPort objects on the same GPIO bank. Higher numbers are processed first.
evCodeIn - A user specified code which appears in the event output.

 

Public Fields

public event InterruptEventHandlerMM OnInterrupt;

The event through which the InterruptPort class transmits the interrupt data to other objects.

 

Public Methods

public void ClearInterrupt()

Clears the interrupt. Must be called after every interrupt before a new interrupt can be received.

 

public override void ClosePort()

Closes the port. Dispose() should subsequently be called to release resources. It is not possible to re-open a closed InterruptPort - a new instance must be created.

 

public void DisableInterrupt()

Disables the interrupt. Interrupts can be disabled when not needed and re-enabled without re-creating the interrupt.

 

public void Dispose()

Implements IDisposable. Should be called after the class is closed to release resources.

 

public void EnableInterrupt()

Enables the interrupt. Interrupts can be disabled when not needed and re-enabled without re-creating the interrupt.

 

public override PortDirectionEnum PortDirection()

Returns the port direction. This value is always PortDirectionEnum.PORTDIR_INPUT.

Returns
A PortDirectionEnum value.

 

Public Properties

public int InterruptEventPriority { get; set; }

Gets/Sets the event priority. For InterruptPorts on the same bank, the InterruptPort with the higher priority will be activated in preference to the others if there simultaneous events.

value
The event priority relative to other InterruptPort objects on the same GPIO bank. Higher numbers are processed first.

 

public int EvCode { get; }

Gets/Sets the event code. This is a userdefined value specified when the interrupt port was created and will be present in the generated interrupt event.

value
A user defined value specified when the interrupt port was created.

 

public InterruptMode EventInterruptMode { get; set; }

Gets/Sets the events interrupt mode.

value
The events interrupt mode

 

public GpioConfig GpioCfgObject { get; }

Gets the objects gpioCfgObject. There is no set accessor. This object is built in the constructor

value
The working gpioID.

 

public GpioEnum GpioID { get; }

Gets the objects gpioID. There is no set accessor. This value is set in the constructor

value
The working gpioID.

 

public int InterruptEventsReceived { get; set; }

Gets/Sets the count of events received. Events triggered and lost because the processing of another event took too long are not included in this count.

value
The count of events received.

 

public int InterruptEventsSent { get; set; }

Gets/Sets the cound of events sent. It is possible to receive an event and not send it if the port is not enabled when the event is received.

value
The count of events sent.