From 14d5437923c40950cf4923da3062e4295ed2ba7a Mon Sep 17 00:00:00 2001 From: Christophe Gerbier Date: Thu, 21 Dec 2017 23:28:13 +0100 Subject: [PATCH] Add drivemode for alternate function This permits to set a pin to an alternate function (e.g. PWM, ADC) Signed-off-by: Christophe Gerbier --- source/GpioPinDriveMode.cs | 6 +++++- "source/Gpio\342\200\213Pin.cs" | 15 +++++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/source/GpioPinDriveMode.cs b/source/GpioPinDriveMode.cs index a64583b..5d1ecd0 100644 --- a/source/GpioPinDriveMode.cs +++ b/source/GpioPinDriveMode.cs @@ -41,6 +41,10 @@ public enum GpioPinDriveMode /// /// Configures the GPIO pin in open collector mode with resistive pull-down mode. /// - OutputOpenSourcePullDown + OutputOpenSourcePullDown, + /// + /// Configures the GPIO pin for an alternate function + /// + Alternate } } diff --git "a/source/Gpio\342\200\213Pin.cs" "b/source/Gpio\342\200\213Pin.cs" index da514f7..d6f6562 100644 --- "a/source/Gpio\342\200\213Pin.cs" +++ "b/source/Gpio\342\200\213Pin.cs" @@ -27,6 +27,7 @@ public sealed class Gpio​Pin : IDisposable private object _syncLock = new object(); private readonly int _pinNumber; + private int _alternateFunction = 0; private GpioPinDriveMode _driveMode = GpioPinDriveMode.Input; private TimeSpan _debounceTimeout = TimeSpan.Zero; private GpioPinValueChangedEventHandler _callbacks = null; @@ -148,26 +149,28 @@ public bool IsDriveModeSupported(GpioPinDriveMode driveMode) /// /// An enumeration value that specifies drive mode to use for the GPIO pin. /// The drive mode specifies whether the pin is configured as an input or an output, and determines how values are driven onto the pin. + /// An integer value that specifies the alternate function assigned to the GPIO pin. /// The following exceptions can be thrown by this method: /// /// E_INVALIDARG : The GPIO pin does not support the specified drive mode. /// E_ACCESSDENIED : The pin is open in shared read-only mode.Close the pin and reopen it in exclusive mode to change the drive mode of the pin. /// /// - public void SetDriveMode(GpioPinDriveMode value) + public void SetDriveMode(GpioPinDriveMode value, int alternateFunction = 0) { lock (_syncLock) { // check if pin has been disposed if (_disposedValue) { throw new ObjectDisposedException(); } - if (_driveMode == value) return; + if (_driveMode == value && alternateFunction == _alternateFunction) return; // check if the request drive mode is supported // need to call the native method directly because we are already inside a lock if (NativeIsDriveModeSupported(value)) { - NativeSetDriveMode(value); + NativeSetDriveMode(value, alternateFunction); + _alternateFunction = alternateFunction; _driveMode = value; } } @@ -246,7 +249,7 @@ public event GpioPinValueChangedEventHandler ValueChanged try { _callbacks = callbacksNew; - NativeSetDriveMode(_driveMode); + NativeSetDriveMode(_driveMode, _alternateFunction); } catch { @@ -271,7 +274,7 @@ public event GpioPinValueChangedEventHandler ValueChanged try { _callbacks = callbacksNew; - NativeSetDriveMode(_driveMode); + NativeSetDriveMode(_driveMode, _alternateFunction); } catch { @@ -351,7 +354,7 @@ public void Dispose() private extern bool NativeIsDriveModeSupported(GpioPinDriveMode driveMode); [MethodImpl(MethodImplOptions.InternalCall)] - private extern void NativeSetDriveMode(GpioPinDriveMode driveMode); + private extern void NativeSetDriveMode(GpioPinDriveMode driveMode, int alternateFunction); [MethodImpl(MethodImplOptions.InternalCall)] private extern bool NativeInit(int pinNumber);