ESPHome 2026.11.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#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
14ESPHOME_LOG_TAG(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 ESP_LOGV_HEX(TAG, data.data(), data.size(), PN532_MAX_LOG_BYTES, "Writing data: %s");
42 this->write_array(data.data(), data.size());
43 this->disable();
44
45 return true;
46}
47
49 if (len + 1 > pn532::PN532_FRAME_MAX_SIZE || 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 // lead with a status byte so callers see the same layout as on the I2C bus
61 data.resize(len + 1);
62 data[0] = 0x01;
63 this->read_array(data.data() + 1, len);
64 this->disable();
65 ESP_LOGV_HEX(TAG, data.data(), data.size(), PN532_MAX_LOG_BYTES, "Read data: %s");
66 return true;
67}
68
69bool PN532Spi::read_response(uint8_t command, pn532::PN532Frame &data) {
70 ESP_LOGV(TAG, "Reading response");
71
72 if (this->read_ready_(true) != pn532::PN532ReadReady::READY) {
73 return false;
74 }
75
76 this->enable();
77 delay(2);
78 this->write_byte(0x03);
79
80 std::array<uint8_t, 7> header;
81 this->read_array(header.data(), header.size());
82
83#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
84 char hex_buf[format_hex_pretty_size(PN532_MAX_LOG_BYTES)];
85#endif
86 ESP_LOGV(TAG, "Header data: %s", format_hex_pretty_to(hex_buf, sizeof(hex_buf), header.data(), header.size()));
87
88 if (header[0] != 0x00 || header[1] != 0x00 || header[2] != 0xFF) {
89 // invalid packet
90 ESP_LOGV(TAG, "read data invalid preamble!");
91 this->disable();
92 return false;
93 }
94
95 bool valid_header = (static_cast<uint8_t>(header[3] + header[4]) == 0 && // LCS, len + lcs = 0
96 header[5] == 0xD5 && // TFI - frame from PN532 to system controller
97 header[6] == command + 1); // Correct command response
98
99 if (!valid_header) {
100 ESP_LOGV(TAG, "read data invalid header!");
101 this->disable();
102 return false;
103 }
104
105 // full length of message, including command response (minimum 2: TFI + command response)
106 uint8_t full_len = header[3];
107 if (full_len < 2) {
108 ESP_LOGV(TAG, "read data has no payload");
109 this->disable();
110 return false;
111 }
112
113 // length of data, excluding command response
114 uint8_t len = full_len - 1;
115
116 ESP_LOGV(TAG, "Reading response of length %d", len);
117
118 data.resize(len + 1);
119 this->read_array(data.data(), len + 1);
120 this->disable();
121
122 ESP_LOGV(TAG, "Response data: %s", format_hex_pretty_to(hex_buf, sizeof(hex_buf), data.data(), data.size()));
123
124 // TFI + Command response code + data
125 const uint8_t checksum = -sum_bytes(data.data(), len - 1, header[5] + header[6]);
126
127 if (data[len - 1] != checksum) {
128 ESP_LOGV(TAG, "read data invalid checksum! %02X != %02X", data[len - 1], checksum);
129 return false;
130 }
131
132 if (data[len] != 0x00) {
133 ESP_LOGV(TAG, "read data invalid postamble!");
134 return false;
135 }
136
137 data.resize(len - 1); // Remove checksum and postamble
138
139 return true;
140}
141
143 PN532::dump_config();
144 LOG_PIN(" CS Pin: ", this->cs_);
145}
146
147} // 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:286
enum PN532ReadReady read_ready_(bool block)
Definition pn532.cpp:326
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:48
bool read_response(uint8_t command, pn532::PN532Frame &data) override
Definition pn532_spi.cpp:69
ESPHOME_LOG_TAG(TAG, "pn532_spi")
constexpr uint8_t sum_bytes(const uint8_t *data, size_t len, uint8_t sum=0)
Add up the bytes of data with size len, truncated to 8 bits: the checksum many serial and IR protocol...
Definition helpers.h:894
const void size_t len
Definition hal.h:68
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:498
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:1609
void HOT delay(uint32_t ms)
Definition hal.cpp:85