ESPHome 2026.10.2
Loading...
Searching...
No Matches
pn532_spi.cpp
Go to the documentation of this file.
1#include "pn532_spi.h"
3#include "esphome/core/log.h"
4
5#include <array>
6
7// Based on:
8// - https://cdn-shop.adafruit.com/datasheets/PN532C106_Application+Note_v1.2.pdf
9// - https://www.nxp.com/docs/en/nxp/application-notes/AN133910.pdf
10// - https://www.nxp.com/docs/en/nxp/application-notes/153710.pdf
11
13
14static const char *const TAG = "pn532_spi";
15
16// Maximum bytes to log in verbose hex output
17static constexpr size_t PN532_MAX_LOG_BYTES = 64;
18
20 this->spi_setup();
21
22 this->cs_->digital_write(false);
23 delay(10);
24 PN532::setup();
25}
26
28 this->enable();
29 this->write_byte(0x02);
30 // only bit 0 (RDY) of the status byte is defined (UM0701-02, 6.2.5)
31 const bool ready = this->read_byte() & 0x01;
32 this->disable();
33 return ready;
34}
35
36bool PN532Spi::write_data(const std::span<const uint8_t> data) {
37 this->enable();
38 delay(2);
39 // First byte, communication mode: Write data
40 this->write_byte(0x01);
41#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
42 char hex_buf[format_hex_pretty_size(PN532_MAX_LOG_BYTES)];
43#endif
44 ESP_LOGV(TAG, "Writing data: %s", format_hex_pretty_to(hex_buf, sizeof(hex_buf), data.data(), data.size()));
45 this->write_array(data.data(), data.size());
46 this->disable();
47
48 return true;
49}
50
52 if (len + 1 > pn532::PN532_FRAME_MAX_SIZE || this->read_ready_(true) != pn532::PN532ReadReady::READY) {
53 return false;
54 }
55
56 // Read data (transmission from the PN532 to the host)
57 this->enable();
58 delay(2);
59 this->write_byte(0x03);
60
61 ESP_LOGV(TAG, "Reading data");
62
63 // lead with a status byte so callers see the same layout as on the I2C bus
64 data.resize(len + 1);
65 data[0] = 0x01;
66 this->read_array(data.data() + 1, len);
67 this->disable();
68#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
69 char hex_buf[format_hex_pretty_size(PN532_MAX_LOG_BYTES)];
70#endif
71 ESP_LOGV(TAG, "Read data: %s", format_hex_pretty_to(hex_buf, sizeof(hex_buf), data.data(), data.size()));
72 return true;
73}
74
75bool PN532Spi::read_response(uint8_t command, pn532::PN532Frame &data) {
76 ESP_LOGV(TAG, "Reading response");
77
78 if (this->read_ready_(true) != pn532::PN532ReadReady::READY) {
79 return false;
80 }
81
82 this->enable();
83 delay(2);
84 this->write_byte(0x03);
85
86 std::array<uint8_t, 7> header;
87 this->read_array(header.data(), header.size());
88
89#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
90 char hex_buf[format_hex_pretty_size(PN532_MAX_LOG_BYTES)];
91#endif
92 ESP_LOGV(TAG, "Header data: %s", format_hex_pretty_to(hex_buf, sizeof(hex_buf), header.data(), header.size()));
93
94 if (header[0] != 0x00 || header[1] != 0x00 || header[2] != 0xFF) {
95 // invalid packet
96 ESP_LOGV(TAG, "read data invalid preamble!");
97 this->disable();
98 return false;
99 }
100
101 bool valid_header = (static_cast<uint8_t>(header[3] + header[4]) == 0 && // LCS, len + lcs = 0
102 header[5] == 0xD5 && // TFI - frame from PN532 to system controller
103 header[6] == command + 1); // Correct command response
104
105 if (!valid_header) {
106 ESP_LOGV(TAG, "read data invalid header!");
107 this->disable();
108 return false;
109 }
110
111 // full length of message, including command response (minimum 2: TFI + command response)
112 uint8_t full_len = header[3];
113 if (full_len < 2) {
114 ESP_LOGV(TAG, "read data has no payload");
115 this->disable();
116 return false;
117 }
118
119 // length of data, excluding command response
120 uint8_t len = full_len - 1;
121
122 ESP_LOGV(TAG, "Reading response of length %d", len);
123
124 data.resize(len + 1);
125 this->read_array(data.data(), len + 1);
126 this->disable();
127
128 ESP_LOGV(TAG, "Response data: %s", format_hex_pretty_to(hex_buf, sizeof(hex_buf), data.data(), data.size()));
129
130 uint8_t checksum = header[5] + header[6]; // TFI + Command response code
131 for (int i = 0; i < len - 1; i++) {
132 uint8_t dat = data[i];
133 checksum += dat;
134 }
135 checksum = ~checksum + 1;
136
137 if (data[len - 1] != checksum) {
138 ESP_LOGV(TAG, "read data invalid checksum! %02X != %02X", data[len - 1], checksum);
139 return false;
140 }
141
142 if (data[len] != 0x00) {
143 ESP_LOGV(TAG, "read data invalid postamble!");
144 return false;
145 }
146
147 data.resize(len - 1); // Remove checksum and postamble
148
149 return true;
150}
151
153 PN532::dump_config();
154 LOG_PIN(" CS Pin: ", this->cs_);
155}
156
157} // namespace esphome::pn532_spi
uint8_t checksum
Definition bl0906.h:3
virtual void digital_write(bool value)=0
Minimal static vector - saves memory by avoiding std::vector overhead.
Definition helpers.h:291
enum PN532ReadReady read_ready_(bool block)
Definition pn532.cpp:333
bool write_data(std::span< const uint8_t > data) override
Definition pn532_spi.cpp:36
bool is_read_ready() override
Definition pn532_spi.cpp:27
bool read_data(pn532::PN532Frame &data, size_t len) override
Definition pn532_spi.cpp:51
bool read_response(uint8_t command, pn532::PN532Frame &data) override
Definition pn532_spi.cpp:75
const void size_t len
Definition hal.h:64
char * format_hex_pretty_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length, char separator)
Format byte array as uppercase hex to buffer (base implementation).
Definition helpers.cpp:433
constexpr size_t format_hex_pretty_size(size_t byte_count)
Calculate buffer size needed for format_hex_pretty_to with separator: "XX:XX:...:XX\0".
Definition helpers.h:1532
void HOT delay(uint32_t ms)
Definition hal.cpp:85