예제 #1
0
 internal void DeviceDisablePort()
 {
     _isr           = IntPtr.Zero;
     _isrParam      = IntPtr.Zero;
     _interruptMode = GpioInterruptMode.None;
     _resistorMode  = GpioResistorMode.Disabled;
     _mode          = GpioPortMode.None;
 }
예제 #2
0
        public override void SetupComponent()
        {
            if (_pin == Cpu.Pin.GPIO_NONE || _pin < 0)
            {
                throw new Exception("The GpioPort has an invalid pin.");
            }

            if ((_modesExpected & _modesAllowed) != _modesExpected)
            {
                throw new InvalidOperationException("All modes expected have to be allowed: expected = " +
                                                    _modesExpected.ToString() + "; allowed = " + _modesAllowed.ToString());
            }

            _isReserved       = false;
            _mode             = GpioPortMode.None;
            _state            = false;
            _stateInitialized = false;

            _interruptMode = GpioInterruptMode.None;
            _resistorMode  = GpioResistorMode.Disabled;
            _debounceMode  = DebounceMode.None;
        }
예제 #3
0
        internal void DeviceEnableAsInputPort(bool glitchFilter, GpioResistorMode resistorMode, GpioInterruptMode interruptMode, IntPtr isr, IntPtr isrParam)
        {
            if ((_modesExpected & GpioPortMode.InputPort) == 0)
            {
                if ((this is GpioPortNull) == false) // We put an warning about accessing a non-existing port in DeviceGet already
                {
                    Trace.WriteLine("Attempt to initialize GPIO Port " + Pin + " as an input port when it's expecting " + _modesExpected.ToString());
                }
                return;
            }

            if (_mode != GpioPortMode.None)
            {
                // return the port to a Disabled state first if it wasn't previously disabled.
                DeviceDisablePort();
            }

            _interruptMode = interruptMode;
            _isr           = isr;
            _isrParam      = isrParam;
            _resistorMode  = resistorMode;

            if (glitchFilter)
            {
                _debounceMode = (this.Emulator.GpioPorts.HardwareDebounceSupported) ? DebounceMode.HardwareDebounce : DebounceMode.SoftwareDebounce;
            }
            else
            {
                _debounceMode = DebounceMode.None;
            }

            _mode = GpioPortMode.InputPort;

            if (_stateInitialized == false)
            {
                // Since the pullup/pulldown are meant for as a way to avoid floating values, we shouldn't count the state as initialized.
                // We just simply set the state appropriately.
                if (_resistorMode == GpioResistorMode.PullUp)
                {
                    _state = true;
                }
                else if (_resistorMode == GpioResistorMode.PullDown)
                {
                    _state = false;
                }
            }

            if (_interruptMode != GpioInterruptMode.None)
            {
                if (isr.Equals(IntPtr.Zero))
                {
                    Debug.Assert(false, "Invalid ISR.");
                    Trace.WriteLine("Error: Interrupt port " + _pin + " has an invalid ISR.");
                    return;
                }

                if ((interruptMode == GpioInterruptMode.LevelHigh && _state == true) ||
                    (interruptMode == GpioInterruptMode.LevelLow && _state == false))
                {
                    HandleDebounce(true);

                    // Note that HandleDebounce will make this function re-entrant if it's a level interrupt.
                    return;
                }
            }
            else
            {
                _isr      = IntPtr.Zero;
                _isrParam = IntPtr.Zero;
            }
        }