ESPHome 2026.1.0-dev
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// Based on:
6// - https://cdn-shop.adafruit.com/datasheets/PN532C106_Application+Note_v1.2.pdf
7// - https://www.nxp.com/docs/en/nxp/application-notes/AN133910.pdf
8// - https://www.nxp.com/docs/en/nxp/application-notes/153710.pdf
9
10namespace esphome {
11namespace pn532_spi {
12
13static const char *const TAG = "pn532_spi";
14
15// Maximum bytes to log in verbose hex output
16static constexpr size_t PN532_MAX_LOG_BYTES = 64;
17
19 this->spi_setup();
20
21 this->cs_->digital_write(false);
22 delay(10);
23 PN532::setup();
24}
25
27 this->enable();
28 this->write_byte(0x02);
29 bool ready = this->read_byte() == 0x01;
30 this->disable();
31 return ready;
32}
33
34bool PN532Spi::write_data(const std::vector<uint8_t> &data) {
35 this->enable();
36 delay(2);
37 // First byte, communication mode: Write data
38 this->write_byte(0x01);
39#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
40 char hex_buf[format_hex_pretty_size(PN532_MAX_LOG_BYTES)];
41#endif
42 ESP_LOGV(TAG, "Writing data: %s", format_hex_pretty_to(hex_buf, sizeof(hex_buf), data.data(), data.size()));
43 this->write_array(data.data(), data.size());
44 this->disable();
45
46 return true;
47}
48
49bool PN532Spi::read_data(std::vector<uint8_t> &data, uint8_t len) {
50 if (this->read_ready_(true) != pn532::PN532ReadReady::READY) {
51 return false;
52 }
53
54 // Read data (transmission from the PN532 to the host)
55 this->enable();
56 delay(2);
57 this->write_byte(0x03);
58
59 ESP_LOGV(TAG, "Reading data");
60
61 data.resize(len);
62 this->read_array(data.data(), len);
63 this->disable();
64 data.insert(data.begin(), 0x01);
65#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
66 char hex_buf[format_hex_pretty_size(PN532_MAX_LOG_BYTES)];
67#endif
68 ESP_LOGV(TAG, "Read data: %s", format_hex_pretty_to(hex_buf, sizeof(hex_buf), data.data(), data.size()));
69 return true;
70}
71
72bool PN532Spi::read_response(uint8_t command, std::vector<uint8_t> &data) {
73 ESP_LOGV(TAG, "Reading response");
74
75 if (this->read_ready_(true) != pn532::PN532ReadReady::READY) {
76 return false;
77 }
78
79 this->enable();
80 delay(2);
81 this->write_byte(0x03);
82
83 std::vector<uint8_t> header(7);
84 this->read_array(header.data(), 7);
85
86#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
87 char hex_buf[format_hex_pretty_size(PN532_MAX_LOG_BYTES)];
88#endif
89 ESP_LOGV(TAG, "Header data: %s", format_hex_pretty_to(hex_buf, sizeof(hex_buf), header.data(), header.size()));
90
91 if (header[0] != 0x00 && header[1] != 0x00 && header[2] != 0xFF) {
92 // invalid packet
93 ESP_LOGV(TAG, "read data invalid preamble!");
94 return false;
95 }
96
97 bool valid_header = (static_cast<uint8_t>(header[3] + header[4]) == 0 && // LCS, len + lcs = 0
98 header[5] == 0xD5 && // TFI - frame from PN532 to system controller
99 header[6] == command + 1); // Correct command response
100
101 if (!valid_header) {
102 ESP_LOGV(TAG, "read data invalid header!");
103 return false;
104 }
105
106 // full length of message, including command response
107 uint8_t full_len = header[3];
108 // length of data, excluding command response
109 uint8_t len = full_len - 1;
110 if (full_len == 0)
111 len = 0;
112
113 ESP_LOGV(TAG, "Reading response of length %d", len);
114
115 data.resize(len + 1);
116 this->read_array(data.data(), len + 1);
117 this->disable();
118
119 ESP_LOGV(TAG, "Response data: %s", format_hex_pretty_to(hex_buf, sizeof(hex_buf), data.data(), data.size()));
120
121 uint8_t checksum = header[5] + header[6]; // TFI + Command response code
122 for (int i = 0; i < len - 1; i++) {
123 uint8_t dat = data[i];
124 checksum += dat;
125 }
126 checksum = ~checksum + 1;
127
128 if (data[len - 1] != checksum) {
129 ESP_LOGV(TAG, "read data invalid checksum! %02X != %02X", data[len - 1], checksum);
130 return false;
131 }
132
133 if (data[len] != 0x00) {
134 ESP_LOGV(TAG, "read data invalid postamble!");
135 return false;
136 }
137
138 data.erase(data.end() - 2, data.end()); // Remove checksum and postamble
139
140 return true;
141}
142
144 PN532::dump_config();
145 LOG_PIN(" CS Pin: ", this->cs_);
146}
147
148} // namespace pn532_spi
149} // namespace esphome
uint8_t checksum
Definition bl0906.h:3
virtual void digital_write(bool value)=0
enum PN532ReadReady read_ready_(bool block)
Definition pn532.cpp:308
bool write_data(const std::vector< uint8_t > &data) override
Definition pn532_spi.cpp:34
bool read_data(std::vector< uint8_t > &data, uint8_t len) override
Definition pn532_spi.cpp:49
bool is_read_ready() override
Definition pn532_spi.cpp:26
bool read_response(uint8_t command, std::vector< uint8_t > &data) override
Definition pn532_spi.cpp:72
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:533
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:331
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:735
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:26