-
Notifications
You must be signed in to change notification settings - Fork 609
Add libgpiodv2 support #2184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add libgpiodv2 support #2184
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
d4d09ad
libgpiodv2: introduce namespace for libgpiod v1 for differentation to v2
huesla fa70b4c
libgpiodv2: add binding and proxy classes for libgpiod v2
huesla 1802a7e
libgpiodv2: add libgpiodv2 driver and event observer
huesla 8ddb83b
libgpiodv2: add libgpiod driver factory to select driver based on ins…
huesla cfc5764
libgpiodv2: add tests
huesla 7aca23d
libgpiodv2: add documentation on how to use libgpiodv2 driver
huesla 576896c
libgpiodv2: documentation improvement
huesla a9c1bb5
libgpiodv2: name namespaces consistent
huesla 489fabe
libgpiodv2: keep LibGpiodDriver name to not break client code, make n…
huesla a22a430
libgpiodv2: remove unnecessary global::
huesla cb78456
libgpiodv2: do not block operations when waiting for edge events
huesla 81e24a3
libgpiodv2: adjust documentation
huesla 7d60b3f
libgpiodv2: introduce factory for libgpiod proxy objects
huesla 9c126eb
libgpiodv2: locking reads is not necessary
huesla edae721
libgpiodv2: adjust creation of versioned driver instances
huesla 943f81c
libgpiodv2: fix typo
huesla 118e97b
libgpiodv2: add GetAvailableVersions
huesla 21dd0c3
libgpiodv2: remove obsolete test
huesla 428a708
libgpiodv2: adjust docs
huesla 53a39ca
libgpiodv2: refactoring
huesla ea2952e
libgpiodv2: let GpioException derive from IOException
huesla 8e1e578
libgpiodv2: adjust documentation
huesla 15640eb
libgpiodv2: adjust .md style
huesla c519265
libgpiodv2: apply disposable pattern to all proxies, assign enum memb…
huesla 2ae7ed6
libgpiodv2: minor fix in binding
huesla 00af9b4
libgpiodv2: free all filedescriptors
huesla a42db33
libgpiodv2: make LibGpiodDriverFactory a singleton and use Lazy
huesla 58ec654
libgpiodv2: replace getter, improve exception message
huesla dd372bb
libgpiodv2: use alternative init in LibGpiodDriverFactory
huesla 0caa2dd
Add required suppressions
pgrawehr 34e7ea3
Disable LibGpiod2 for Helix until runners are updated
pgrawehr d2d618f
libgpiodv2: recursively search for libgpiod
huesla cd717eb
Fix exclusions
pgrawehr 1a22986
libgpiodv2: improve XML doc
huesla File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| # Using libgpiod to control GPIOs | ||
|
|
||
| ## Quick usage: blink LED example | ||
|
|
||
| This example targets a RaspberryPi 3/4, see comments for more information: | ||
|
|
||
| ```c# | ||
| // side note: on the Raspberry Pi the GPIO chip line offsets are the same numbers as the usual BCM GPIO numbering, which is convenient | ||
| const int ledGpio = 15; | ||
|
|
||
| // on the Pi3,4 you most likely want 0, on the Pi5 number 4, see 'gpioinfo' tool | ||
| const int chipNumber = 0; | ||
| // 'using' will dispose the controller when it falls out of scope, which will un-claim lines | ||
|
|
||
| // alternatively be more explicit: 'new GpioController(chipNumber, new LibGpiodDriver())' | ||
| using var gpioController = new GpioController(chipNumber); | ||
|
|
||
| gpioController.OpenPin(ledGpio); | ||
|
|
||
| for (int i = 0; i < 5; i++) | ||
| { | ||
| controller.Write(ledGpio, PinValue.High); | ||
| await Task.Delay(1000); | ||
| controller.Write(ledGpio, PinValue.Low); | ||
| await Task.Delay(1000); | ||
| } | ||
| ``` | ||
|
|
||
| ## libgpiod versions | ||
|
|
||
| **Note**: The documented version of libgpiod is not the same as the library so name, see the following table: | ||
|
|
||
| | Documented version | Library so name | Comment | | ||
| | ------------------ | ----------------- | -------------------------------------------------------- | | ||
| | 1.0.2 | libgpiod.so.1.0.2 | last .so.1.x library | | ||
| | 1.1 | libgpiod.so.2.0.0 | first occurrence of inconsistency, first .so.2.x library | | ||
| | 1.6.4 | libgpiod.so.2.2.2 | last .so.2.x library | | ||
| | 2.0 | libgpiod.so.3.0.0 | first .so.3.x library | | ||
| | 2.1 | libgpiod.so.3.1.0 | latest .so.3.x library (currently) | | ||
|
|
||
| ## libgpiod version support | ||
|
|
||
| Currently (12/23) dotnet-iot supports v0, v1 and v2 of libgpiod. | ||
|
|
||
| The following table shows which driver supports which library version | ||
|
|
||
| | LibGpiodDriverVersion | Libgpiod version (documented) | | ||
| | --------------------- | ----------------------------- | | ||
| | V1 | 0.x to 1.x | | ||
| | V2 | 2.x | | ||
|
|
||
| ## Choose LibGpiodDriver Version | ||
|
|
||
| If you want to explicitly select the version of the libgpiod driver, to target a specific library version, there are following options: | ||
|
|
||
| 1. constructor of LibGpiodDriver: | ||
|
|
||
| ```c# | ||
| new LibGpiodDriver(chipNumber, LibGpiodDriverVersion.V1) | ||
| ``` | ||
|
|
||
| 2. Environment variable: | ||
|
|
||
| ```shell | ||
| export DOTNET_IOT_LIBGPIOD_DRIVER_VERSION=V1 // or V2... | ||
| ``` | ||
|
|
||
| When not explicitly specified, dotnet iot automatically tries to find a driver compatible to what library version is installed. | ||
|
|
||
| ## Install libgpiod | ||
|
|
||
| If you want to control GPIOs using libgpiod, the library must be installed. | ||
|
|
||
| Many package managers provide a libgpiod package, for example: | ||
|
|
||
| ```shell | ||
| apt install libgpiod2 | ||
| ``` | ||
|
|
||
| ## Install libgpiod manually | ||
|
|
||
| The installation should be the same on all Pi's, or boards whose distro uses the APT package manager. | ||
|
|
||
| 1. Install build dependencies | ||
|
|
||
| ```shell | ||
| sudo apt update && sudo apt install -y autogen autoconf autoconf-archive libtool libtool-bin pkg-config build-essential | ||
| ``` | ||
|
|
||
| 2. Download the tarball and unpack it, see [releases](https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/refs/), e.g. | ||
|
|
||
| ```shell | ||
| wget https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/snapshot/libgpiod-2.1.tar.gz | ||
| tar -xzf libgpiod-2.1.tar.gz | ||
| ``` | ||
|
|
||
| 3. Compile and install (see [docs](https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/about/)) | ||
|
|
||
| ```shell | ||
| cd libgpiod-2.1/ | ||
| ./autogen.sh | ||
| make | ||
| sudo make install | ||
| sudo ldconfig | ||
| ``` | ||
|
|
||
| This will install the library .so files to `/usr/lib/local` | ||
|
|
||
| If you want to also build command line utilities `gpioinfo, gpiodetect` etc., specify `./autogen.sh --enable-tools=yes` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Device.Gpio.Drivers; | ||
| using System.Diagnostics; | ||
| using System.Threading.Tasks; | ||
| using Xunit; | ||
| using Xunit.Abstractions; | ||
|
|
||
| namespace System.Device.Gpio.Tests; | ||
|
|
||
| [Trait("feature", "gpio")] | ||
| [Trait("feature", "gpio-libgpiod2")] | ||
| [Trait("SkipOnTestRun", "Windows_NT")] | ||
| public class LibGpiodV2DriverTests : GpioControllerTestBase | ||
| { | ||
| private const int ChipNumber = 0; | ||
|
|
||
| public LibGpiodV2DriverTests(ITestOutputHelper testOutputHelper) | ||
| : base(testOutputHelper) | ||
| { | ||
| } | ||
|
|
||
| protected override GpioDriver GetTestDriver() => new LibGpiodDriver(ChipNumber, LibGpiodDriverVersion.V2); | ||
|
|
||
| protected override PinNumberingScheme GetTestNumberingScheme() => PinNumberingScheme.Logical; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| // Disable these StyleCop rules for this file, as we are using native names here. | ||
| #pragma warning disable SA1300 // Element should begin with upper-case letter | ||
|
|
||
| using System.Runtime.InteropServices; | ||
|
|
||
| internal partial class Interop | ||
| { | ||
| [DllImport(LibcLibrary, SetLastError = true)] | ||
| internal static extern int pipe(int[] pipefd); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/System.Device.Gpio/Interop/Unix/libgpiod/V2/Binding/Enums/GpiodEdgeEventType.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace System.Device.Gpio.Libgpiod.V2; | ||
|
|
||
| /// <seealso href="https://libgpiod.readthedocs.io/en/latest/group__edge__event.html"/> | ||
| internal enum GpiodEdgeEventType | ||
| { | ||
| RisingEdge = 1, | ||
| FallingEdge = 2 | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.