ESPHome  2024.9.2
pn532_i2c.cpp
Go to the documentation of this file.
1 #include "pn532_i2c.h"
2 #include "esphome/core/log.h"
3 #include "esphome/core/hal.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 
10 namespace esphome {
11 namespace pn532_i2c {
12 
13 static const char *const TAG = "pn532_i2c";
14 
16  uint8_t ready;
17  if (!this->read_bytes_raw(&ready, 1)) {
18  return false;
19  }
20  return ready == 0x01;
21 }
22 
23 bool PN532I2C::write_data(const std::vector<uint8_t> &data) {
24  return this->write(data.data(), data.size()) == i2c::ERROR_OK;
25 }
26 
27 bool PN532I2C::read_data(std::vector<uint8_t> &data, uint8_t len) {
28  delay(1);
29 
30  if (this->read_ready_(true) != pn532::PN532ReadReady::READY) {
31  return false;
32  }
33 
34  data.resize(len + 1);
35  this->read_bytes_raw(data.data(), len + 1);
36  return true;
37 }
38 
39 bool PN532I2C::read_response(uint8_t command, std::vector<uint8_t> &data) {
40  ESP_LOGV(TAG, "Reading response");
41  uint8_t len = this->read_response_length_();
42  if (len == 0) {
43  return false;
44  }
45 
46  ESP_LOGV(TAG, "Reading response of length %d", len);
47  if (!this->read_data(data, 6 + len + 2)) {
48  ESP_LOGD(TAG, "No response data");
49  return false;
50  }
51 
52  if (data[1] != 0x00 && data[2] != 0x00 && data[3] != 0xFF) {
53  // invalid packet
54  ESP_LOGV(TAG, "read data invalid preamble!");
55  return false;
56  }
57 
58  bool valid_header = (static_cast<uint8_t>(data[4] + data[5]) == 0 && // LCS, len + lcs = 0
59  data[6] == 0xD5 && // TFI - frame from PN532 to system controller
60  data[7] == command + 1); // Correct command response
61 
62  if (!valid_header) {
63  ESP_LOGV(TAG, "read data invalid header!");
64  return false;
65  }
66 
67  data.erase(data.begin(), data.begin() + 6); // Remove headers
68 
69  uint8_t checksum = 0;
70  for (int i = 0; i < len + 1; i++) {
71  uint8_t dat = data[i];
72  checksum += dat;
73  }
74  checksum = ~checksum + 1;
75 
76  if (data[len + 1] != checksum) {
77  ESP_LOGV(TAG, "read data invalid checksum! %02X != %02X", data[len], checksum);
78  return false;
79  }
80 
81  if (data[len + 2] != 0x00) {
82  ESP_LOGV(TAG, "read data invalid postamble!");
83  return false;
84  }
85 
86  data.erase(data.begin(), data.begin() + 2); // Remove TFI and command code
87  data.erase(data.end() - 2, data.end()); // Remove checksum and postamble
88 
89  return true;
90 }
91 
93  std::vector<uint8_t> data;
94  if (!this->read_data(data, 6)) {
95  return 0;
96  }
97 
98  if (data[1] != 0x00 && data[2] != 0x00 && data[3] != 0xFF) {
99  // invalid packet
100  ESP_LOGV(TAG, "read data invalid preamble!");
101  return 0;
102  }
103 
104  bool valid_header = (static_cast<uint8_t>(data[4] + data[5]) == 0 && // LCS, len + lcs = 0
105  data[6] == 0xD5); // TFI - frame from PN532 to system controller
106 
107  if (!valid_header) {
108  ESP_LOGV(TAG, "read data invalid header!");
109  return 0;
110  }
111 
112  this->send_nack_();
113 
114  // full length of message, including TFI
115  uint8_t full_len = data[4];
116  // length of data, excluding TFI
117  uint8_t len = full_len - 1;
118  if (full_len == 0)
119  len = 0;
120  return len;
121 }
122 
124  PN532::dump_config();
125  LOG_I2C_DEVICE(this);
126 }
127 
128 } // namespace pn532_i2c
129 } // namespace esphome
bool read_data(std::vector< uint8_t > &data, uint8_t len) override
Definition: pn532_i2c.cpp:27
bool write_data(const std::vector< uint8_t > &data) override
Definition: pn532_i2c.cpp:23
uint8_t checksum
Definition: bl0906.h:210
optional< std::array< uint8_t, N > > read_bytes_raw()
Definition: i2c.h:225
enum PN532ReadReady read_ready_(bool block)
Definition: pn532.cpp:310
ErrorCode write(const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a device using an I2CBus
Definition: i2c.h:186
No error found during execution of method.
Definition: i2c_bus.h:13
bool is_read_ready() override
Definition: pn532_i2c.cpp:15
std::string size_t len
Definition: helpers.h:292
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void dump_config() override
Definition: pn532_i2c.cpp:123
bool read_response(uint8_t command, std::vector< uint8_t > &data) override
Definition: pn532_i2c.cpp:39
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26