From eeecc887be1b077cb8493fa3084fae6c12d86317 Mon Sep 17 00:00:00 2001 From: rodrigobengoechea <53696937+rodrigobengoechea@users.noreply.github.com> Date: Thu, 4 Feb 2021 08:23:40 -0300 Subject: [PATCH] Fixes #1423 (#1434) * fixing issue #1423 Co-authored-by: Rodrigo Bengoechea --- src/devices/Pn532/Pn532.cs | 58 ++++++++++++++++++++++++---- src/devices/Pn532/samples/Program.cs | 33 +++++++++++++++- 2 files changed, 83 insertions(+), 8 deletions(-) diff --git a/src/devices/Pn532/Pn532.cs b/src/devices/Pn532/Pn532.cs index d1346f19..b6881d3e 100644 --- a/src/devices/Pn532/Pn532.cs +++ b/src/devices/Pn532/Pn532.cs @@ -1191,11 +1191,11 @@ namespace Iot.Device.Pn532 /// /// Read the PN532 GPIO /// - /// The P7 GPIO /// The P3 GPIO + /// The P7 GPIO /// The specific operation mode register /// True if success - public bool ReadGpio(out Port7 p7, out Port3 p3, out OperatingMode l0L1) + public bool ReadGpio(out Port3 p3, out Port7 p7, out OperatingMode l0L1) { // No flag as default p7 = 0; @@ -1209,14 +1209,14 @@ namespace Iot.Device.Pn532 Span retGPIO = stackalloc byte[3]; ret = ReadResponse(CommandSet.ReadGPIO, retGPIO); - p7 = (Port7)retGPIO[0]; - p3 = (Port3)retGPIO[1]; + p3 = (Port3)retGPIO[0]; + p7 = (Port7)retGPIO[1]; l0L1 = (OperatingMode)retGPIO[2]; return ret >= 0; } /// - /// Write the PN532 GPIO + /// Write the PN532 GPIO ports 3 and 7 /// /// The P7 GPIO /// The P3 GPIO @@ -1225,8 +1225,52 @@ namespace Iot.Device.Pn532 { Span toWrite = stackalloc byte[2] { - (byte)p7, - (byte)p3 + (byte)(0x80 | (byte)p3), + (byte)(0x80 | (byte)p7) + }; + var ret = WriteCommand(CommandSet.WriteGPIO, toWrite); + if (ret < 0) + { + return false; + } + + ret = ReadResponse(CommandSet.WriteGPIO, Span.Empty); + return ret >= 0; + } + + /// + /// Write the PN532 GPIO port 3 leaving port 7 in it's current state + /// + /// The P3 GPIO + /// True if success + public bool WriteGpio(Port3 p3) + { + Span toWrite = stackalloc byte[2] + { + (byte)(0x80 | (byte)p3), + (byte)(0x00) + }; + var ret = WriteCommand(CommandSet.WriteGPIO, toWrite); + if (ret < 0) + { + return false; + } + + ret = ReadResponse(CommandSet.WriteGPIO, Span.Empty); + return ret >= 0; + } + + /// + /// Write the PN532 GPIO port 7 leaving port 3 in it's current state + /// + /// The P7 GPIO + /// True if success + public bool WriteGpio(Port7 p7) + { + Span toWrite = stackalloc byte[2] + { + (byte)(0x00), + (byte)(0x80 | (byte)p7) }; var ret = WriteCommand(CommandSet.WriteGPIO, toWrite); if (ret < 0) diff --git a/src/devices/Pn532/samples/Program.cs b/src/devices/Pn532/samples/Program.cs index e3b7af04..1e0f0032 100644 --- a/src/devices/Pn532/samples/Program.cs +++ b/src/devices/Pn532/samples/Program.cs @@ -8,6 +8,7 @@ using System.Device.I2c; using System.Device.Spi; using System.Linq; using System.Threading; +using System.Threading.Tasks; using Iot.Device.Card; using Iot.Device.Card.CreditCardProcessing; using Iot.Device.Card.Mifare; @@ -76,6 +77,7 @@ if (pn532.FirmwareVersion is FirmwareVersion version) // To run tests, uncomment the next line // RunTests(pn532); ReadMiFare(pn532); + // TestGPIO(pn532); // To read Credit Cards, uncomment the next line // ReadCreditCard(pn532); @@ -209,6 +211,35 @@ void ReadMiFare(Pn532 pn532) } } +void TestGPIO(Pn532 pn532) +{ + Console.WriteLine("Turning Off Port 7!"); + var ret = pn532.WriteGpio((Port7)0); + + // Access GPIO + ret = pn532.ReadGpio(out Port3 p3, out Port7 p7, out OperatingMode l0L1); + Console.WriteLine($"P7: {p7}"); + Console.WriteLine($"P3: {p3}"); + Console.WriteLine($"L0L1: {l0L1} "); + + var on = true; + for (var i = 0; i < 10; i++) + { + if (on) + { + p7 = Port7.P71; + } + else + { + p7 = 0; + } + + ret = pn532.WriteGpio(p7); + Task.Delay(150).Wait(); + on = !on; + } +} + void RunTests(Pn532 pn532) { Console.WriteLine( @@ -240,7 +271,7 @@ void RunTests(Pn532 pn532) Console.WriteLine($"Are results same: {redSfrus.SequenceEqual(redSfrs)}"); // Access GPIO - ret = pn532.ReadGpio(out Port7 p7, out Port3 p3, out OperatingMode l0L1); + ret = pn532.ReadGpio(out Port3 p3, out Port7 p7, out OperatingMode l0L1); Console.WriteLine($"P7: {p7}"); Console.WriteLine($"P3: {p3}"); Console.WriteLine($"L0L1: {l0L1} "); -- GitLab