-
Notifications
You must be signed in to change notification settings - Fork 630
Implement GpioDriver on Pca95x4 for GpioController access #2581
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
Open
Copilot
wants to merge
4
commits into
main
Choose a base branch
from
copilot/implement-gpiodriver-for-pca95x4
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a917117
Initial plan
Copilot db05b2b
Implement GpioDriver on Pca95x4 with tests, sample and docs
Copilot 60a37f9
Address review: add exception messages and refine test mock
Copilot 2bd5938
Merge remote-tracking branch 'origin/main' into copilot/implement-gpi…
Copilot 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
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,10 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <TargetFramework>$(DefaultTestTfms)</TargetFramework> | ||
| <IsPackable>false</IsPackable> | ||
| <GenerateDocumentationFile>false</GenerateDocumentationFile> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <ProjectReference Include="..\Pca95x4.csproj" /> | ||
| </ItemGroup> | ||
| </Project> |
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,174 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Device.Gpio; | ||
| using System.Device.I2c; | ||
| using Xunit; | ||
|
|
||
| namespace Iot.Device.Pca95x4.Tests | ||
| { | ||
| public class Pca95x4Test | ||
| { | ||
| [Fact] | ||
| public void PinCountIsEight() | ||
| { | ||
| using Pca95x4Chip chip = new Pca95x4Chip(); | ||
| using Pca95x4 device = new Pca95x4(chip); | ||
| using GpioController controller = new GpioController(device); | ||
| Assert.Equal(8, controller.PinCount); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(PinMode.Input)] | ||
| [InlineData(PinMode.Output)] | ||
| public void SupportedPinModes(PinMode mode) | ||
| { | ||
| using Pca95x4Chip chip = new Pca95x4Chip(); | ||
| using Pca95x4 device = new Pca95x4(chip); | ||
| using GpioController controller = new GpioController(device); | ||
| Assert.True(controller.IsPinModeSupported(0, mode)); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(PinMode.InputPullUp)] | ||
| [InlineData(PinMode.InputPullDown)] | ||
| public void UnsupportedPinModes(PinMode mode) | ||
| { | ||
| using Pca95x4Chip chip = new Pca95x4Chip(); | ||
| using Pca95x4 device = new Pca95x4(chip); | ||
| using GpioController controller = new GpioController(device); | ||
| Assert.False(controller.IsPinModeSupported(0, mode)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SetPinModeUpdatesConfigurationRegister() | ||
| { | ||
| using Pca95x4Chip chip = new Pca95x4Chip(); | ||
| using Pca95x4 device = new Pca95x4(chip); | ||
| using GpioController controller = new GpioController(device); | ||
|
|
||
| controller.OpenPin(2, PinMode.Output); | ||
| // A cleared configuration bit designates an output. | ||
| Assert.Equal(0, chip.Registers[(int)Register.Configuration] & (1 << 2)); | ||
| Assert.Equal(PinMode.Output, controller.GetPinMode(2)); | ||
|
|
||
| controller.SetPinMode(2, PinMode.Input); | ||
| // A set configuration bit designates an input. | ||
| Assert.NotEqual(0, chip.Registers[(int)Register.Configuration] & (1 << 2)); | ||
| Assert.Equal(PinMode.Input, controller.GetPinMode(2)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void WriteUpdatesOutputRegister() | ||
| { | ||
| using Pca95x4Chip chip = new Pca95x4Chip(); | ||
| using Pca95x4 device = new Pca95x4(chip); | ||
| using GpioController controller = new GpioController(device); | ||
|
|
||
| controller.OpenPin(5, PinMode.Output); | ||
| controller.Write(5, PinValue.High); | ||
| Assert.NotEqual(0, chip.Registers[(int)Register.OutputPort] & (1 << 5)); | ||
|
|
||
| controller.Write(5, PinValue.Low); | ||
| Assert.Equal(0, chip.Registers[(int)Register.OutputPort] & (1 << 5)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ReadReflectsInputRegister() | ||
| { | ||
| using Pca95x4Chip chip = new Pca95x4Chip(); | ||
| using Pca95x4 device = new Pca95x4(chip); | ||
| using GpioController controller = new GpioController(device); | ||
|
|
||
| controller.OpenPin(3, PinMode.Input); | ||
|
|
||
| chip.Registers[(int)Register.InputPort] = 1 << 3; | ||
| Assert.Equal(PinValue.High, controller.Read(3)); | ||
|
|
||
| chip.Registers[(int)Register.InputPort] = 0x00; | ||
| Assert.Equal(PinValue.Low, controller.Read(3)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TogglingFlipsOutput() | ||
| { | ||
| using Pca95x4Chip chip = new Pca95x4Chip(); | ||
| using Pca95x4 device = new Pca95x4(chip); | ||
| using GpioController controller = new GpioController(device); | ||
|
|
||
| controller.OpenPin(1, PinMode.Output); | ||
| controller.Write(1, PinValue.Low); | ||
| controller.Toggle(1); | ||
| Assert.NotEqual(0, chip.Registers[(int)Register.OutputPort] & (1 << 1)); | ||
| controller.Toggle(1); | ||
| Assert.Equal(0, chip.Registers[(int)Register.OutputPort] & (1 << 1)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void RegisterApiStillWorks() | ||
| { | ||
| using Pca95x4Chip chip = new Pca95x4Chip(); | ||
| using Pca95x4 device = new Pca95x4(chip); | ||
|
|
||
| device.Write(Register.OutputPort, 0xAA); | ||
| Assert.Equal(0xAA, device.Read(Register.OutputPort)); | ||
| Assert.True(device.ReadBit(Register.OutputPort, 1)); | ||
| Assert.False(device.ReadBit(Register.OutputPort, 0)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Simple in-memory mock that mimics the four PCA95x4 registers. | ||
| /// </summary> | ||
| private sealed class Pca95x4Chip : I2cDevice | ||
| { | ||
| private readonly byte[] _registers = new byte[4]; | ||
| private int _address; | ||
|
|
||
| public Pca95x4Chip() | ||
| { | ||
| // At reset the I/Os are configured as inputs. | ||
| _registers[(int)Register.Configuration] = 0xFF; | ||
| } | ||
|
|
||
| public byte[] Registers => _registers; | ||
|
|
||
| public override I2cConnectionSettings ConnectionSettings => new I2cConnectionSettings(1, 0x38); | ||
|
|
||
| public override void WriteByte(byte value) => _address = value; | ||
|
|
||
| public override byte ReadByte() => _registers[_address]; | ||
|
|
||
| public override void Write(ReadOnlySpan<byte> buffer) | ||
| { | ||
| _address = buffer[0]; | ||
| if (buffer.Length > 1) | ||
| { | ||
| _registers[_address] = buffer[1]; | ||
|
|
||
| // On real hardware the Input Port reflects the actual pin levels. Pins configured | ||
| // as outputs (a cleared Configuration bit) are driven by the Output Port, while | ||
| // pins configured as inputs (a set Configuration bit) keep their external level. | ||
| if (_address == (int)Register.OutputPort) | ||
| { | ||
| byte configuration = _registers[(int)Register.Configuration]; | ||
| byte input = _registers[(int)Register.InputPort]; | ||
| _registers[(int)Register.InputPort] = | ||
| (byte)((input & configuration) | (buffer[1] & ~configuration)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public override void Read(Span<byte> buffer) | ||
| { | ||
| for (int i = 0; i < buffer.Length; i++) | ||
| { | ||
| buffer[i] = _registers[_address]; | ||
| } | ||
| } | ||
|
|
||
| public override void WriteRead(ReadOnlySpan<byte> writeBuffer, Span<byte> readBuffer) => | ||
| throw new NotImplementedException(); | ||
| } | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
local Copilot comment:
🟠
Togglemisbehaves when input polarity inversion is enabled (major, new bug)The PR doesn't override
Toggle, soGpioDriver's defaultToggle(pin) = Write(pin, !Read(pin))runs.Readgoes toInputPort, which the PCA95x4 datasheet defines to be affected by thePolarityInversionregister. If a user has enabled polarity inversion (via the existing publicInvertInputRegisterPolarityAPI) and then callscontroller.Toggle(pin)on an output, the driver reads the inverted value and writes the same latch value back — Toggle silently becomes a no-op. OverrideToggleto read/flip theOutputPortlatch directly (mirrors whatPcx857xdoes with its cached_pinValueBits).