未验证 提交 eeecc887 编写于 作者: R rodrigobengoechea 提交者: GitHub

Fixes #1423 (#1434)

* fixing issue #1423
Co-authored-by: NRodrigo Bengoechea <rbengoechea@serbiznet.com>
上级 61a5aa31
...@@ -1191,11 +1191,11 @@ namespace Iot.Device.Pn532 ...@@ -1191,11 +1191,11 @@ namespace Iot.Device.Pn532
/// <summary> /// <summary>
/// Read the PN532 GPIO /// Read the PN532 GPIO
/// </summary> /// </summary>
/// <param name="p7">The P7 GPIO</param>
/// <param name="p3">The P3 GPIO</param> /// <param name="p3">The P3 GPIO</param>
/// <param name="p7">The P7 GPIO</param>
/// <param name="l0L1">The specific operation mode register</param> /// <param name="l0L1">The specific operation mode register</param>
/// <returns>True if success</returns> /// <returns>True if success</returns>
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 // No flag as default
p7 = 0; p7 = 0;
...@@ -1209,14 +1209,14 @@ namespace Iot.Device.Pn532 ...@@ -1209,14 +1209,14 @@ namespace Iot.Device.Pn532
Span<byte> retGPIO = stackalloc byte[3]; Span<byte> retGPIO = stackalloc byte[3];
ret = ReadResponse(CommandSet.ReadGPIO, retGPIO); ret = ReadResponse(CommandSet.ReadGPIO, retGPIO);
p7 = (Port7)retGPIO[0]; p3 = (Port3)retGPIO[0];
p3 = (Port3)retGPIO[1]; p7 = (Port7)retGPIO[1];
l0L1 = (OperatingMode)retGPIO[2]; l0L1 = (OperatingMode)retGPIO[2];
return ret >= 0; return ret >= 0;
} }
/// <summary> /// <summary>
/// Write the PN532 GPIO /// Write the PN532 GPIO ports 3 and 7
/// </summary> /// </summary>
/// <param name="p7">The P7 GPIO</param> /// <param name="p7">The P7 GPIO</param>
/// <param name="p3">The P3 GPIO</param> /// <param name="p3">The P3 GPIO</param>
...@@ -1225,8 +1225,52 @@ namespace Iot.Device.Pn532 ...@@ -1225,8 +1225,52 @@ namespace Iot.Device.Pn532
{ {
Span<byte> toWrite = stackalloc byte[2] Span<byte> toWrite = stackalloc byte[2]
{ {
(byte)p7, (byte)(0x80 | (byte)p3),
(byte)p3 (byte)(0x80 | (byte)p7)
};
var ret = WriteCommand(CommandSet.WriteGPIO, toWrite);
if (ret < 0)
{
return false;
}
ret = ReadResponse(CommandSet.WriteGPIO, Span<byte>.Empty);
return ret >= 0;
}
/// <summary>
/// Write the PN532 GPIO port 3 leaving port 7 in it's current state
/// </summary>
/// <param name="p3">The P3 GPIO</param>
/// <returns>True if success</returns>
public bool WriteGpio(Port3 p3)
{
Span<byte> 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<byte>.Empty);
return ret >= 0;
}
/// <summary>
/// Write the PN532 GPIO port 7 leaving port 3 in it's current state
/// </summary>
/// <param name="p7">The P7 GPIO</param>
/// <returns>True if success</returns>
public bool WriteGpio(Port7 p7)
{
Span<byte> toWrite = stackalloc byte[2]
{
(byte)(0x00),
(byte)(0x80 | (byte)p7)
}; };
var ret = WriteCommand(CommandSet.WriteGPIO, toWrite); var ret = WriteCommand(CommandSet.WriteGPIO, toWrite);
if (ret < 0) if (ret < 0)
......
...@@ -8,6 +8,7 @@ using System.Device.I2c; ...@@ -8,6 +8,7 @@ using System.Device.I2c;
using System.Device.Spi; using System.Device.Spi;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks;
using Iot.Device.Card; using Iot.Device.Card;
using Iot.Device.Card.CreditCardProcessing; using Iot.Device.Card.CreditCardProcessing;
using Iot.Device.Card.Mifare; using Iot.Device.Card.Mifare;
...@@ -76,6 +77,7 @@ if (pn532.FirmwareVersion is FirmwareVersion version) ...@@ -76,6 +77,7 @@ if (pn532.FirmwareVersion is FirmwareVersion version)
// To run tests, uncomment the next line // To run tests, uncomment the next line
// RunTests(pn532); // RunTests(pn532);
ReadMiFare(pn532); ReadMiFare(pn532);
// TestGPIO(pn532);
// To read Credit Cards, uncomment the next line // To read Credit Cards, uncomment the next line
// ReadCreditCard(pn532); // ReadCreditCard(pn532);
...@@ -209,6 +211,35 @@ void ReadMiFare(Pn532 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) void RunTests(Pn532 pn532)
{ {
Console.WriteLine( Console.WriteLine(
...@@ -240,7 +271,7 @@ void RunTests(Pn532 pn532) ...@@ -240,7 +271,7 @@ void RunTests(Pn532 pn532)
Console.WriteLine($"Are results same: {redSfrus.SequenceEqual(redSfrs)}"); Console.WriteLine($"Are results same: {redSfrus.SequenceEqual(redSfrs)}");
// Access GPIO // 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($"P7: {p7}");
Console.WriteLine($"P3: {p3}"); Console.WriteLine($"P3: {p3}");
Console.WriteLine($"L0L1: {l0L1} "); Console.WriteLine($"L0L1: {l0L1} ");
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册