예제 #1
0
 public NXPGPIOPort(Machine machine, int numberOfPins) : base(machine, numberOfPins)
 {
     IRQ = new GPIO();
     interruptManager = new GPIOInterruptManager(IRQ, State);
     DefineGPIORegisters();
     DefinePortRegisters();
 }
예제 #2
0
        public CC2538_GPIO(Machine machine) : base(machine, NumberOfGPIOs)
        {
            locker     = new object();
            IRQ        = new GPIO();
            irqManager = new GPIOInterruptManager(IRQ, State);

            PrepareRegisters();
        }
예제 #3
0
        public PSE_GPIO(Machine machine) : base(machine, 32)
        {
            locker     = new object();
            IRQ        = new GPIO();
            irqManager = new GPIOInterruptManager(IRQ, State);

            var registersMap = new Dictionary <long, DoubleWordRegister>
            {
                { (long)Registers.InterruptRegister, new DoubleWordRegister(this)
                  .WithValueField(0, 32,
                                  writeCallback: (_, val) =>
                    {
                        foreach (var i in BitHelper.GetSetBits(val))
                        {
                            irqManager.ClearInterrupt(i);
                            if ((irqManager.PinDirection[i] & GPIOInterruptManager.Direction.Input) != 0)
                            {
                                Connections[i].Set(false);
                            }
                        }
                    },
                                  valueProviderCallback: _ => BitHelper.GetValueFromBitsArray(irqManager.ActiveInterrupts), name: "INTR") },

                { (long)Registers.InputRegister, new DoubleWordRegister(this)
                  .WithValueField(0, 32, out inputReg, FieldMode.Read,
                                  valueProviderCallback: val =>
                    {
                        var pins   = irqManager.PinDirection.Select(x => (x & GPIOInterruptManager.Direction.Input) != 0);
                        var result = pins.Zip(State, (pin, state) => pin && state);
                        return(BitHelper.GetValueFromBitsArray(result));
                    }, name: "GPIN") },

                { (long)Registers.OutputRegister, new DoubleWordRegister(this)
                  .WithValueField(0, 32,
                                  valueProviderCallback: val =>
                    {
                        var pins   = irqManager.PinDirection.Select(x => (x & GPIOInterruptManager.Direction.Output) != 0);
                        var result = pins.Zip(Connections.Values, (pin, state) => pin && state.IsSet);
                        return(BitHelper.GetValueFromBitsArray(result));
                    },
                                  writeCallback: (_, val) =>
                    {
                        // Potentially we should raise an exception, as GPIO is bidirectional,
                        // but we do not have such infrastructure.
                        var bits = BitHelper.GetBits(val);
                        for (var i = 0; i < bits.Length; i++)
                        {
                            if ((irqManager.PinDirection[i] & GPIOInterruptManager.Direction.Output) != 0)
                            {
                                Connections[i].Set(bits[i]);
                            }
                        }
                    }, name: "GPOUT") },

                { (long)Registers.ClearRegister, new DoubleWordRegister(this)
                  .WithValueField(0, 32, writeCallback: (_, val) => SetRegisterBits(val, false), name: "CLEAR_BITS") },

                { (long)Registers.SetRegister, new DoubleWordRegister(this)
                  .WithValueField(0, 32, writeCallback: (_, val) => SetRegisterBits(val, true), name: "SET_BITS") },
            };

            var intTypeToVal = new TwoWayDictionary <GPIOInterruptManager.InterruptTrigger, uint>();

            intTypeToVal.Add(GPIOInterruptManager.InterruptTrigger.ActiveHigh, 0);
            intTypeToVal.Add(GPIOInterruptManager.InterruptTrigger.ActiveLow, 1);
            intTypeToVal.Add(GPIOInterruptManager.InterruptTrigger.RisingEdge, 2);
            intTypeToVal.Add(GPIOInterruptManager.InterruptTrigger.FallingEdge, 3);
            intTypeToVal.Add(GPIOInterruptManager.InterruptTrigger.BothEdges, 4);

            for (var i = 0; i < RegisterLength; i++)
            {
                var j = i;
                registersMap.Add(i * RegisterOffset, new DoubleWordRegister(this)
                                 .WithFlag(0,
                                           writeCallback: (_, val) =>
                {
                    if (val)
                    {
                        irqManager.PinDirection[j] |= GPIOInterruptManager.Direction.Output;
                    }
                    else
                    {
                        irqManager.PinDirection[j] &= ~GPIOInterruptManager.Direction.Output;
                    }
                },
                                           valueProviderCallback: _ => (irqManager.PinDirection[j] & GPIOInterruptManager.Direction.Output) != 0, name: "OutputRegEnable")
                                 .WithFlag(1,
                                           writeCallback: (_, value) =>
                {
                    if (value)
                    {
                        irqManager.PinDirection[j] |= GPIOInterruptManager.Direction.Input;
                    }
                    else
                    {
                        irqManager.PinDirection[j] &= ~GPIOInterruptManager.Direction.Input;
                    }
                },
                                           valueProviderCallback: _ => (irqManager.PinDirection[j] & GPIOInterruptManager.Direction.Input) != 0, name: "InputRegEnable")
                                 .WithTag("OutputBufferEnable", 2, 1)
                                 .WithFlag(3, writeCallback: (_, v) => { irqManager.InterruptEnable[j] = v; }, valueProviderCallback: _ => irqManager.InterruptEnable[j], name: "InterruptEnable")
                                 .WithReservedBits(4, 1)
                                 .WithValueField(5, 3,
                                                 writeCallback: (_, value) =>
                {
                    if (!intTypeToVal.TryGetValue(value, out var type))
                    {
                        this.Log(LogLevel.Warning, "Invalid interrupt type for pin #{0}: {1}", j, value);
                        return;
                    }
                    irqManager.InterruptType[j] = type;
                },
                                                 valueProviderCallback: _ => intTypeToVal[irqManager.InterruptType[j]], name: "InterruptType"));
            }
            registers = new DoubleWordRegisterCollection(this, registersMap);
        }
예제 #4
0
        public MiV_CoreGPIO(Machine machine) : base(machine, NumberOfInterrupts)
        {
            innerLock = new object();
            IRQ       = new GPIO();

            irqManager = new GPIOInterruptManager(IRQ, State);

            var registersMap = new Dictionary <long, DoubleWordRegister>
            {
                { (long)Registers.OutputRegister, new DoubleWordRegister(this).WithValueField(0, 32, FieldMode.Write, writeCallback: (_, value) =>
                    {
                        lock (innerLock)
                        {
                            var bits = BitHelper.GetBits(value);
                            for (var i = 0; i < bits.Length; i++)
                            {
                                Connections[i].Set(bits[i]);
                            }
                        }
                    }) },

                { (long)Registers.InputRegister, new DoubleWordRegister(this).WithValueField(0, 32, FieldMode.Read, valueProviderCallback: _ =>
                    {
                        lock (innerLock)
                        {
                            return(BitHelper.GetValueFromBitsArray(State));
                        }
                    }) },

                { (long)Registers.InterruptClearRegister, new DoubleWordRegister(this).WithValueField(0, 32, writeCallback: (_, value) =>
                    {
                        lock (innerLock)
                        {
                            foreach (var i in BitHelper.GetSetBits(value))
                            {
                                irqManager.ClearInterrupt((uint)i);
                            }
                        }
                    }, valueProviderCallback: _ => {
                        lock (innerLock)
                        {
                            return(BitHelper.GetValueFromBitsArray(irqManager.ActiveInterrupts));
                        }
                    }) }
            };

            var intTypeToVal = new TwoWayDictionary <GPIOInterruptManager.InterruptTrigger, uint>();

            intTypeToVal.Add(GPIOInterruptManager.InterruptTrigger.ActiveHigh, 0);
            intTypeToVal.Add(GPIOInterruptManager.InterruptTrigger.ActiveLow, 1);
            intTypeToVal.Add(GPIOInterruptManager.InterruptTrigger.RisingEdge, 2);
            intTypeToVal.Add(GPIOInterruptManager.InterruptTrigger.FallingEdge, 3);
            intTypeToVal.Add(GPIOInterruptManager.InterruptTrigger.BothEdges, 4);

            for (var i = 0; i < NumberOfInterrupts; i++)
            {
                var j = i;
                registersMap.Add((long)Registers.ConfigurationRegisterBase + i * 0x4, new DoubleWordRegister(this)
                                 .WithFlag(0, writeCallback: (_, v) =>
                {
                    if (v)
                    {
                        irqManager.PinDirection[j] |= GPIOInterruptManager.Direction.Output;
                    }
                    else
                    {
                        irqManager.PinDirection[j] &= ~GPIOInterruptManager.Direction.Output;
                    }
                },
                                           valueProviderCallback: _ => (irqManager.PinDirection[j] & GPIOInterruptManager.Direction.Output) != 0, name: "OUTREG")
                                 .WithFlag(1, writeCallback: (_, value) =>
                {
                    if (value)
                    {
                        irqManager.PinDirection[j] |= GPIOInterruptManager.Direction.Input;
                    }
                    else
                    {
                        irqManager.PinDirection[j] &= ~GPIOInterruptManager.Direction.Input;
                    }
                },
                                           valueProviderCallback: _ => (irqManager.PinDirection[j] & GPIOInterruptManager.Direction.Input) != 0, name: "INREG")
                                 .WithTag("OUTBUFF", 2, 1)
                                 .WithFlag(3, writeCallback: (_, v) => { irqManager.InterruptEnable[j] = v; }, valueProviderCallback: _ => irqManager.InterruptEnable[j], name: "INTENABLE")
                                 //bit 4 unused
                                 .WithValueField(5, 3, writeCallback: (_, value) =>
                {
                    if (!intTypeToVal.TryGetValue(value, out var type))
                    {
                        this.Log(LogLevel.Warning, "Invalid interrupt type for pin #{0}: {1}", j, value);
                        return;
                    }
                    irqManager.InterruptType[j] = type;
                }, valueProviderCallback: _ =>
                {
                    if (!intTypeToVal.TryGetValue(irqManager.InterruptType[j], out var value))
                    {
                        throw new ArgumentOutOfRangeException($"Unknown interrupt trigger type: {irqManager.InterruptType[j]}");
                    }
                    return(value);
                }, name: "INTTYPE"));
            }

            registers = new DoubleWordRegisterCollection(this, registersMap);
        }