Example #1
0
 internal void DeviceDisablePort()
 {
     _isr           = IntPtr.Zero;
     _isrParam      = IntPtr.Zero;
     _interruptMode = GpioInterruptMode.None;
     _resistorMode  = GpioResistorMode.Disabled;
     _mode          = GpioPortMode.None;
 }
        /// <summary>Registers a GPIO pin in the system</summary>
        /// <param name="Name">Name for the pin</param>
        /// <param name="Pin">Pin number</param>
        /// <param name="ExpectedMode">Expected mode for the pin</param>
        /// <param name="AllowedMode">Allowed modes for the pin</param>
        /// <remarks>
        /// <para>Useful method for registering a pin</para>
        /// </para>NOTE: This should only be called from an overload of the
        /// <see cref="LoadDefaultComponents()"/> Method</para>
        /// </remarks>
        protected GpioPort RegisterPin(string Name, Cpu.Pin Pin, GpioPortMode ExpectedMode, GpioPortMode AllowedMode)
        {
            if (this.State != EmulatorState.Configuration)
            {
                throw new InvalidOperationException(
                          "Cannot register GPIO Pins unless emulator is in the Configuration state");
            }

            GpioPort port = new GpioPort();

            port.Pin           = Pin;
            port.ModesAllowed  = AllowedMode;
            port.ModesExpected = ExpectedMode;
            port.ComponentId   = Name;
            RegisterComponent(port);
            return(port);
        }
Example #3
0
        internal void DeviceEnableAsOutputPort(bool initialState)
        {
            if ((_modesExpected & GpioPortMode.OutputPort) == 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 output 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();
            }

            _mode             = GpioPortMode.OutputPort;
            _state            = initialState;
            _stateInitialized = true;
        }
Example #4
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;
        }
Example #5
0
 public GpioPortNull(Cpu.Pin pin, GpioPortMode portCapabilities)
     : base(pin, GpioPortMode.None, portCapabilities)
 {
 }
Example #6
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;
            }
        }
Example #7
0
 protected GpioPort(Cpu.Pin pin, GpioPortMode expected, GpioPortMode allowed)
 {
     _pin           = pin;
     _modesExpected = expected;
     _modesAllowed  = allowed;
 }
Example #8
0
 public GpioPort()
 {
     _pin           = Cpu.Pin.GPIO_NONE;
     _modesExpected = GpioPortMode.None;
     _modesAllowed  = GpioPortMode.None;
 }
Example #9
0
		/// <summary>Registers a GPIO pin in the system</summary>
		/// <param name="name">Name for the pin</param>
		/// <param name="pin">Pin number</param>
		/// <param name="expectedMode">Expected mode for the pin</param>
		/// <param name="allowedMode">Allowed modes for the pin</param>
		/// <remarks>
		/// <para>Useful method for registering a pin</para>
		/// <para>NOTE: This should only be called from an overload of the
        /// <see cref="EmulatorService.LoadDefaultComponents()"/> Method</para>
		/// </remarks>
		protected GpioPort RegisterPin(string name, Cpu.Pin pin, GpioPortMode expectedMode, GpioPortMode allowedMode)
		{
			if (State != EmulatorState.Configuration)
				throw new InvalidOperationException(
							 "Cannot register GPIO Pins unless emulator is in the Configuration state");

			var port = new GpioPort {Pin = pin, ModesAllowed = allowedMode, ModesExpected = expectedMode, ComponentId = name};
		    RegisterComponent(port);
			return port;
		}
Example #10
0
		/// <summary>Registers a GPIO pin in the system</summary>
		/// <param name="Name">Name for the pin</param>
		/// <param name="Pin">Pin number</param>
		/// <param name="ExpectedMode">Expected mode for the pin</param>
		/// <param name="AllowedMode">Allowed modes for the pin</param>
		/// <remarks>
		/// <para>Useful method for registering a pin</para>
		/// </para>NOTE: This should only be called from an overload of the
		/// <see cref="LoadDefaultComponents()"/> Method</para>
		/// </remarks>
		protected GpioPort RegisterPin(string Name, Cpu.Pin Pin, GpioPortMode ExpectedMode, GpioPortMode AllowedMode)
		{
			if (this.State != EmulatorState.Configuration)
				throw new InvalidOperationException(
							 "Cannot register GPIO Pins unless emulator is in the Configuration state");

			GpioPort port = new GpioPort();
			port.Pin = Pin;
			port.ModesAllowed = AllowedMode;
			port.ModesExpected = ExpectedMode;
			port.ComponentId = Name;
			RegisterComponent(port);
			return port;
		}