Ft232HGpio.cs 6.7 KB
Newer Older
L
Laurent Ellerbach 已提交
1 2 3 4
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
L
Laurent Ellerbach 已提交
5
using System.Collections.Generic;
L
Laurent Ellerbach 已提交
6 7 8 9 10 11 12 13 14 15 16
using System.Device.Gpio;
using System.Linq;
using System.Threading;

namespace Iot.Device.Ft232H
{
    /// <summary>
    /// GPIO driver for the FT232H
    /// </summary>
    public class Ft232HGpio : GpioDriver
    {
L
Laurent Ellerbach 已提交
17 18
        private readonly Dictionary<int, PinValue> _pinValues = new Dictionary<int, PinValue>();

L
Laurent Ellerbach 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
        /// <summary>
        /// Store the FTDI Device Information
        /// </summary>
        public Ft232HDevice DeviceInformation { get; private set; }

        /// <inheritdoc/>
        protected override int PinCount => Ft232HDevice.PinCountConst;

        /// <summary>
        /// Creates a GPIO Driver
        /// </summary>
        /// <param name="deviceInformation">The FT232H device</param>
        internal Ft232HGpio(Ft232HDevice deviceInformation)
        {
            DeviceInformation = deviceInformation;
            // Open device
            DeviceInformation.GetHandle();
            DeviceInformation.InitializeGpio();
        }

        /// <inheritdoc/>
        protected override int ConvertPinNumberToLogicalNumberingScheme(int pinNumber) => pinNumber;

        /// <inheritdoc/>
        protected override void OpenPin(int pinNumber)
        {
            if ((pinNumber < 0) || (pinNumber >= PinCount))
            {
                throw new ArgumentException($"Pin number can only be between 0 and {PinCount - 1}");
            }

            // Pin 0, 1 and 2 are used by I2C
            if (DeviceInformation.IsI2cModeEnabled && (
                (pinNumber >= 0) && (pinNumber <= 2)))
            {
                throw new ArgumentException($"Can't open pin 0, 1 or 2 while I2C mode is on");
            }

            // Pin 0, 1 and 2 are used by SPI
            if (DeviceInformation.IsSpiModeEnabled && (
                (pinNumber >= 0) && (pinNumber <= 2)))
            {
                throw new ArgumentException($"Can't open pin 0, 1 or 2 while SPI mode is on");
            }

            if (DeviceInformation.ConnectionSettings.Where(m => m.ChipSelectLine == pinNumber).Any())
            {
                throw new ArgumentException($"Pin already open or used as Chip Select");
            }

            DeviceInformation.PinOpen[pinNumber] = true;
L
Laurent Ellerbach 已提交
70
            _pinValues.TryAdd(pinNumber, PinValue.Low);
L
Laurent Ellerbach 已提交
71 72 73 74 75 76
        }

        /// <inheritdoc/>
        protected override void ClosePin(int pinNumber)
        {
            DeviceInformation.PinOpen[pinNumber] = false;
L
Laurent Ellerbach 已提交
77
            _pinValues.Remove(pinNumber);
L
Laurent Ellerbach 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
        }

        /// <inheritdoc/>
        protected override void SetPinMode(int pinNumber, PinMode mode)
        {
            if (pinNumber < 8)
            {
                if (mode != PinMode.Output)
                {
                    byte mask = 0xFF;
                    mask &= (byte)(~(1 << pinNumber));
                    DeviceInformation.GpioLowDir &= mask;
                }
                else
                {
                    DeviceInformation.GpioLowDir |= (byte)(1 << pinNumber);
                }

                DeviceInformation.SetGpioValuesLow();
            }
            else
            {
                if (mode != PinMode.Output)
                {
                    byte mask = 0xFF;
                    mask &= (byte)(~(1 << (pinNumber - 8)));
                    DeviceInformation.GpioHighDir &= mask;
                }
                else
                {
                    DeviceInformation.GpioHighDir |= (byte)(1 << (pinNumber - 8));
                }

                DeviceInformation.SetGpioValuesHigh();
            }
        }

        /// <inheritdoc/>
        protected override PinMode GetPinMode(int pinNumber)
        {
            if (pinNumber < 8)
            {
                return ((DeviceInformation.GpioLowDir >> pinNumber) & 0x01) == 0x01 ? PinMode.Output : PinMode.Input;
            }

            return ((DeviceInformation.GpioHighDir >> (pinNumber - 8)) & 0x01) == 0x01 ? PinMode.Output : PinMode.Input;
        }

        /// <inheritdoc/>
        protected override bool IsPinModeSupported(int pinNumber, PinMode mode)
        {
            return mode == PinMode.Input || mode == PinMode.Output;
        }

        /// <inheritdoc/>
        protected override PinValue Read(int pinNumber)
        {
            if (pinNumber < 8)
            {
                var val = DeviceInformation.GetGpioValuesLow();
L
Laurent Ellerbach 已提交
138
                _pinValues[pinNumber] = (((val >> pinNumber) & 0x01) == 0x01) ? PinValue.High : PinValue.Low;
L
Laurent Ellerbach 已提交
139 140 141 142
            }
            else
            {
                var valhigh = DeviceInformation.GetGpioValuesHigh();
L
Laurent Ellerbach 已提交
143
                _pinValues[pinNumber] = (((valhigh >> (pinNumber - 8)) & 0x01) == 0x01) ? PinValue.High : PinValue.Low;
L
Laurent Ellerbach 已提交
144
            }
L
Laurent Ellerbach 已提交
145 146

            return _pinValues[pinNumber];
L
Laurent Ellerbach 已提交
147 148
        }

L
Laurent Ellerbach 已提交
149 150 151
        /// <inheritdoc/>
        protected override void Toggle(int pinNumber) => Write(pinNumber, !_pinValues[pinNumber]);

L
Laurent Ellerbach 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
        /// <inheritdoc/>
        protected override void Write(int pinNumber, PinValue value)
        {
            if (pinNumber < 8)
            {
                if (value == PinValue.High)
                {
                    DeviceInformation.GpioLowData |= (byte)(1 << pinNumber);
                }
                else
                {
                    byte mask = 0xFF;
                    mask &= (byte)(~(1 << pinNumber));
                    DeviceInformation.GpioLowData &= mask;
                }

                DeviceInformation.SetGpioValuesLow();
            }
            else
            {
                if (value == PinValue.High)
                {
                    DeviceInformation.GpioHighData |= (byte)(1 << (pinNumber - 8));
                }
                else
                {
                    byte mask = 0xFF;
                    mask &= (byte)(~(1 << (pinNumber - 8)));
                    DeviceInformation.GpioHighData &= mask;
                }

                DeviceInformation.SetGpioValuesHigh();
            }
L
Laurent Ellerbach 已提交
185 186

            _pinValues[pinNumber] = value;
L
Laurent Ellerbach 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
        }

        /// <inheritdoc/>
        protected override WaitForEventResult WaitForEvent(int pinNumber, PinEventTypes eventTypes, CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }

        /// <inheritdoc/>
        protected override void AddCallbackForPinValueChangedEvent(int pinNumber, PinEventTypes eventTypes, PinChangeEventHandler callback)
        {
            throw new NotImplementedException();
        }

        /// <inheritdoc/>
        protected override void RemoveCallbackForPinValueChangedEvent(int pinNumber, PinChangeEventHandler callback)
        {
            throw new NotImplementedException();
        }
    }
}