Skip to content

Commit 2f38a99

Browse files
committed
Added the function of specifying serial port pins
1 parent 49a3418 commit 2f38a99

File tree

3 files changed

+88
-26
lines changed

3 files changed

+88
-26
lines changed

Diff for: README.md

+12
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,18 @@ HSU is short for High Speed Uart. HSU interface needs only 4 wires to connect PN
9595
To use the `Serial1` control PN532, refer to the code below.
9696

9797
```c++
98+
/* If you need to specify the pin: For ESP32, you can use PN532_HSU pn532hsu(Serial1, 36, 4) to specify the pin;
99+
For other series that support soft serial port, you can use the soft serial port to specify the pin:
100+
#define USE_SOFT_SERIAL_PIN
101+
#define NFC_INTERFACE_HSU
102+
#include <PN532_HSU.h>
103+
#include <PN532.h>
104+
105+
SoftwareSerial mysoft_serial(D7,D6);
106+
PN532_HSU pn532hsu(mysoft_serial);
107+
PN532 nfc(pn532hsu);
108+
*/
109+
98110
#define NFC_INTERFACE_HSU
99111

100112
#include <PN532_HSU.h>

Diff for: src/PN532_HSU.cpp

+53-24
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,62 @@
33
#include "PN532_HSU.h"
44
#include "PN532_debug.h"
55

6+
7+
8+
#ifdef ESP32
9+
10+
PN532_HSU::PN532_HSU(HardwareSerial &serial, int8_t rxPin, int8_t txPin)
11+
{
12+
hsu_serial = &serial;
13+
command = 0;
14+
_rxPin = rxPin;
15+
_txPin = txPin;
16+
}
17+
#else
18+
#ifdef USE_SOFT_SERIAL_PIN
19+
PN532_HSU::PN532_HSU(SoftwareSerial &serial){
20+
hsu_serial = &serial;
21+
command = 0;
22+
}
23+
#else
624
PN532_HSU::PN532_HSU(HardwareSerial &serial)
725
{
8-
_serial = &serial;
26+
hsu_serial = &serial;
927
command = 0;
1028
}
29+
#endif
30+
#endif
1131

1232
void PN532_HSU::begin()
1333
{
14-
_serial->begin(115200);
34+
35+
#ifdef ESP32
36+
get_serial(hsu_serial)->begin(115200, SERIAL_8N1, _rxPin, _txPin);
37+
#else
38+
#ifdef USE_SOFT_SERIAL_PIN
39+
get_serial(hsu_serial)->begin(115200);
40+
#else
41+
get_serial(hsu_serial)->begin(115200);
42+
#endif
43+
#endif
1544
}
1645

1746
void PN532_HSU::wakeup()
1847
{
19-
_serial->write(0x55);
20-
_serial->write(0x55);
21-
_serial->write(uint8_t(0x00));
22-
_serial->write(uint8_t(0x00));
23-
_serial->write(uint8_t(0x00));
48+
get_serial(hsu_serial)->write(0x55);
49+
get_serial(hsu_serial)->write(0x55);
50+
get_serial(hsu_serial)->write(uint8_t(0x00));
51+
get_serial(hsu_serial)->write(uint8_t(0x00));
52+
get_serial(hsu_serial)->write(uint8_t(0x00));
2453

2554
/** dump serial buffer */
26-
if (_serial->available())
55+
if (get_serial(hsu_serial)->available())
2756
{
2857
DMSG("Dump serial buffer: ");
2958
}
30-
while (_serial->available())
59+
while (get_serial(hsu_serial)->available())
3160
{
32-
uint8_t ret = _serial->read();
61+
uint8_t ret = get_serial(hsu_serial)->read();
3362
DMSG_HEX(ret);
3463
}
3564
}
@@ -38,40 +67,40 @@ int8_t PN532_HSU::writeCommand(const uint8_t *header, uint8_t hlen, const uint8_
3867
{
3968

4069
/** dump serial buffer */
41-
if (_serial->available())
70+
if (get_serial(hsu_serial)->available())
4271
{
4372
DMSG("Dump serial buffer: ");
4473
}
45-
while (_serial->available())
74+
while (get_serial(hsu_serial)->available())
4675
{
47-
uint8_t ret = _serial->read();
76+
uint8_t ret = get_serial(hsu_serial)->read();
4877
DMSG_HEX(ret);
4978
}
5079

5180
command = header[0];
5281

53-
_serial->write(uint8_t(PN532_PREAMBLE));
54-
_serial->write(uint8_t(PN532_STARTCODE1));
55-
_serial->write(uint8_t(PN532_STARTCODE2));
82+
get_serial(hsu_serial)->write(uint8_t(PN532_PREAMBLE));
83+
get_serial(hsu_serial)->write(uint8_t(PN532_STARTCODE1));
84+
get_serial(hsu_serial)->write(uint8_t(PN532_STARTCODE2));
5685

5786
uint8_t length = hlen + blen + 1; // length of data field: TFI + DATA
58-
_serial->write(length);
59-
_serial->write(~length + 1); // checksum of length
87+
get_serial(hsu_serial)->write(length);
88+
get_serial(hsu_serial)->write(~length + 1); // checksum of length
6089

61-
_serial->write(PN532_HOSTTOPN532);
90+
get_serial(hsu_serial)->write(PN532_HOSTTOPN532);
6291
uint8_t sum = PN532_HOSTTOPN532; // sum of TFI + DATA
6392

6493
DMSG("\nWrite: ");
6594

66-
_serial->write(header, hlen);
95+
get_serial(hsu_serial)->write(header, hlen);
6796
for (uint8_t i = 0; i < hlen; i++)
6897
{
6998
sum += header[i];
7099

71100
DMSG_HEX(header[i]);
72101
}
73102

74-
_serial->write(body, blen);
103+
get_serial(hsu_serial)->write(body, blen);
75104
for (uint8_t i = 0; i < blen; i++)
76105
{
77106
sum += body[i];
@@ -80,8 +109,8 @@ int8_t PN532_HSU::writeCommand(const uint8_t *header, uint8_t hlen, const uint8_
80109
}
81110

82111
uint8_t checksum = ~sum + 1; // checksum of TFI + DATA
83-
_serial->write(checksum);
84-
_serial->write(uint8_t(PN532_POSTAMBLE));
112+
get_serial(hsu_serial)->write(checksum);
113+
get_serial(hsu_serial)->write(uint8_t(PN532_POSTAMBLE));
85114

86115
return readAckFrame();
87116
}
@@ -195,7 +224,7 @@ int8_t PN532_HSU::receive(uint8_t *buf, int len, uint16_t timeout)
195224
start_millis = millis();
196225
do
197226
{
198-
ret = _serial->read();
227+
ret = get_serial(hsu_serial)->read();
199228
if (ret >= 0)
200229
{
201230
break;

Diff for: src/PN532_HSU.h

+23-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22
#ifndef __PN532_HSU_H__
33
#define __PN532_HSU_H__
44

5+
6+
#ifdef USE_SOFT_SERIAL_PIN
7+
#include <SoftwareSerial.h>
8+
#define get_serial(x) (static_cast<SoftwareSerial*>(x))
9+
#else
10+
#define get_serial(x) (static_cast<HardwareSerial*>(x))
11+
#endif
12+
13+
514
#include "PN532Interface.h"
615
#include "Arduino.h"
716

@@ -12,17 +21,29 @@
1221
class PN532_HSU : public PN532Interface
1322
{
1423
public:
24+
#ifdef ESP32
25+
PN532_HSU(HardwareSerial &serial, int8_t rxPin = -1, int8_t txPin = -1);
26+
#else
27+
#ifdef USE_SOFT_SERIAL_PIN
28+
PN532_HSU(SoftwareSerial &serial);
29+
#else
1530
PN532_HSU(HardwareSerial &serial);
31+
#endif
32+
#endif
1633

1734
void begin();
1835
void wakeup();
1936
virtual int8_t writeCommand(const uint8_t *header, uint8_t hlen, const uint8_t *body = 0, uint8_t blen = 0);
2037
int16_t readResponse(uint8_t buf[], uint8_t len, uint16_t timeout);
2138

2239
private:
23-
HardwareSerial *_serial;
40+
// HardwareSerial *_serial;
41+
void *hsu_serial;
2442
uint8_t command;
25-
43+
#ifdef ESP32
44+
int8_t _rxPin;
45+
int8_t _txPin;
46+
#endif
2647
int8_t readAckFrame();
2748

2849
int8_t receive(uint8_t *buf, int len, uint16_t timeout = PN532_HSU_READ_TIMEOUT);

0 commit comments

Comments
 (0)