void Library_spot_hardware_native_Microsoft_SPOT_Hardware_Port::IsrProcedure( GPIO_PIN pin, BOOL pinState, void* context )
{
    { 
        ASSERT_IRQ_MUST_BE_OFF();

        // If context parameter is NULL, then there is no way to dispatch data managed thread.
        CLR_RT_HeapBlock_NativeEventDispatcher* port = (CLR_RT_HeapBlock_NativeEventDispatcher*)context;
        if ( port == NULL )
        {    
            return;
        }
        
        // Retrieve managed object correspoinding to instance of CLR_RT_HeapBlock.
        // Managed object keeps all the data associated with port.
        CLR_RT_HeapBlock* pManagedPortObj = NULL;
        port->RecoverManagedObject( pManagedPortObj );
        if ( pManagedPortObj == NULL )
        {
            return;
        }

        CLR_INT32 &flags              = pManagedPortObj[ Library_spot_hardware_native_Microsoft_SPOT_Hardware_Port::FIELD__m_flags             ].NumericByRef().s4;
        CLR_UINT32  portID            = pManagedPortObj[ Library_spot_hardware_native_Microsoft_SPOT_Hardware_Port::FIELD__m_portId            ].NumericByRefConst().u4;
        GPIO_INT_EDGE  interruptMode  = (GPIO_INT_EDGE)pManagedPortObj[ Library_spot_hardware_native_Microsoft_SPOT_Hardware_Port::FIELD__m_interruptMode ].NumericByRefConst().s4;
        GPIO_RESISTOR  resistorMode   = (GPIO_RESISTOR)pManagedPortObj[ Library_spot_hardware_native_Microsoft_SPOT_Hardware_Port::FIELD__m_resistorMode  ].NumericByRefConst().s4;

        
        // Discard the call if flags show that interrupt is disabled for this port. 
        if ( flags & GPIO_PortParams::c_InterruptDisabled )
        {
            return;
        }

        // For the level interrupt we disable it once the level was reached.
        // Application needs to enable it back. It is used to protect agains multiple callbacks as result of reaching the level. 
        if ( interruptMode == GPIO_INT_LEVEL_HIGH || interruptMode == GPIO_INT_LEVEL_LOW )
        {   
           
            if ( flags & GPIO_PortParams::c_Disposed )
            {
                return;
            }

            // Disable interrupts for port if not disabled. 
            if(( flags & GPIO_PortParams::c_InterruptDisabled ) == 0)
            {
                // Disable level interrupt.
                flags |= GPIO_PortParams::c_InterruptDisabled;
                ::CPU_GPIO_EnableInputPin2(
                                             portID, // data1 is portId for GPIO case.
                                             false, 
                                             NULL,
                                             0,
                                             GPIO_INT_NONE,
                                             resistorMode
                                          );
            }
        }

        // To calling SaveToHALQueue saves data to 128 slot HAL queue and finally causes dispatch to managed callback. 
        port->SaveToHALQueue( pin, pinState );
    }
}