ESPHome 2026.6.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
11
12static const char *const TAG = "pn532_spi";
13
14// Maximum bytes to log in verbose hex output
15static constexpr size_t PN532_MAX_LOG_BYTES = 64;
16
18 this->spi_setup();
19
20 this->cs_->digital_write(false);
21 delay(10);
22 PN532::setup();
23}
24
26 this->enable();
27 this->write_byte(0x02);
28 bool ready = this->read_byte() == 0x01;
29 this->disable();
30 return ready;
31}
32
33bool PN532Spi::write_data(const std::vector<uint8_t> &data) {
34 this->enable();
35 delay(2);
36 // First byte, communication mode: Write data
37 this->write_byte(0x01);
38#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
39 char hex_buf[format_hex_pretty_size(PN532_MAX_LOG_BYTES)];
40#endif
41 ESP_LOGV(TAG, "Writing data: %s", format_hex_pretty_to(hex_buf, sizeof(hex_buf), data.data(), data.size()));
42 this->write_array(data.data(), data.size());
43 this->disable();
44
45 return true;
46}
47
48bool PN532Spi::read_data(std::vector<uint8_t> &data, uint8_t len) {
49 if (this->read_ready_(true) != pn532::PN532ReadReady::READY) {
50 return false;
51 }
52
53 // Read data (transmission from the PN532 to the host)
54 this->enable();
55 delay(2);
56 this->write_byte(0x03);
57
58 ESP_LOGV(TAG, "Reading data");
59
60 data.resize(len);
61 this->read_array(data.data(), len);
62 this->disable();
63 data.insert(data.begin(), 0x01);
64#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
65 char hex_buf[format_hex_pretty_size(PN532_MAX_LOG_BYTES)];
66#endif
67 ESP_LOGV(TAG, "Read data: %s", format_hex_pretty_to(hex_buf, sizeof(hex_buf), data.data(), data.size()));
68 return true;
69}
70
71bool PN532Spi::read_response(uint8_t command, std::vector<uint8_t> &data) {
72 ESP_LOGV(TAG, "Reading response");
73
74 if (this->read_ready_(true) != pn532::PN532ReadReady::READY) {
75 return false;
76 }
77
78 this->enable();
79 delay(2);
80 this->write_byte(0x03);
81
82 std::vector<uint8_t> header(7);
83 this->read_array(header.data(), 7);
84
85#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
86 char hex_buf[format_hex_pretty_size(PN532_MAX_LOG_BYTES)];
87#endif
88 ESP_LOGV(TAG, "Header data: %s", format_hex_pretty_to(hex_buf, sizeof(hex_buf), header.data(), header.size()));
89
90 if (header[0] != 0x00 || header[1] != 0x00 || header[2] != 0xFF) {
91 // invalid packet
92 ESP_LOGV(TAG, "read data invalid preamble!");
93 this->disable();
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 this->disable();
104 return false;
105 }
106
107 // full length of message, including command response (minimum 2: TFI + command response)
108 uint8_t full_len = header[3];
109 if (full_len < 2) {
110 ESP_LOGV(TAG, "read data has no payload");
111 this->disable();
112 return false;
113 }
114
115 // length of data, excluding command response
116 uint8_t len = full_len - 1;
117
118 ESP_LOGV(TAG, "Reading response of length %d", len);
119
120 data.resize(len + 1);
121 this->read_array(data.data(), len + 1);
122 this->disable();
123
124 ESP_LOGV(TAG, "Response data: %s", format_hex_pretty_to(hex_buf, sizeof(hex_buf), data.data(), data.size()));
125
126 uint8_t checksum = header[5] + header[6]; // TFI + Command response code
127 for (int i = 0; i < len - 1; i++) {
128 uint8_t dat = data[i];
129 checksum += dat;
130 }
131 checksum = ~checksum + 1;
132
133 if (data[len - 1] != checksum) {
134 ESP_LOGV(TAG, "read data invalid checksum! %02X != %02X", data[len - 1], checksum);
135 return false;
136 }
137
138 if (data[len] != 0x00) {
139 ESP_LOGV(TAG, "read data invalid postamble!");
140 return false;
141 }
142
143 data.erase(data.end() - 2, data.end()); // Remove checksum and postamble
144
145 return true;
146}
147
149 PN532::dump_config();
150 LOG_PIN(" CS Pin: ", this->cs_);
151}
152
153} // namespace esphome::pn532_spi
uint8_t checksum
Definition bl0906.h:3
virtual void digital_write(bool value)=0
enum PN532ReadReady read_ready_(bool block)
Definition pn532.cpp:307
bool write_data(const std::vector< uint8_t > &data) override
Definition pn532_spi.cpp:33
bool read_data(std::vector< uint8_t > &data, uint8_t len) override
Definition pn532_spi.cpp:48
bool is_read_ready() override
Definition pn532_spi.cpp:25
bool read_response(uint8_t command, std::vector< uint8_t > &data) override
Definition pn532_spi.cpp:71
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:340
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:1386
void HOT delay(uint32_t ms)
Definition hal.cpp:85