ESPHome  2022.8.0
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 
15 bool PN532I2C::write_data(const std::vector<uint8_t> &data) {
16  return this->write(data.data(), data.size()) == i2c::ERROR_OK;
17 }
18 
19 bool PN532I2C::read_data(std::vector<uint8_t> &data, uint8_t len) {
20  delay(1);
21 
22  std::vector<uint8_t> ready;
23  ready.resize(1);
24  uint32_t start_time = millis();
25  while (true) {
26  if (this->read_bytes_raw(ready.data(), 1)) {
27  if (ready[0] == 0x01)
28  break;
29  }
30 
31  if (millis() - start_time > 100) {
32  ESP_LOGV(TAG, "Timed out waiting for readiness from PN532!");
33  return false;
34  }
35  }
36 
37  data.resize(len + 1);
38  this->read_bytes_raw(data.data(), len + 1);
39  return true;
40 }
41 
42 bool PN532I2C::read_response(uint8_t command, std::vector<uint8_t> &data) {
43  ESP_LOGV(TAG, "Reading response");
44  uint8_t len = this->read_response_length_();
45  if (len == 0) {
46  return false;
47  }
48 
49  ESP_LOGV(TAG, "Reading response of length %d", len);
50  if (!this->read_data(data, 6 + len + 2)) {
51  ESP_LOGD(TAG, "No response data");
52  return false;
53  }
54 
55  if (data[1] != 0x00 && data[2] != 0x00 && data[3] != 0xFF) {
56  // invalid packet
57  ESP_LOGV(TAG, "read data invalid preamble!");
58  return false;
59  }
60 
61  bool valid_header = (static_cast<uint8_t>(data[4] + data[5]) == 0 && // LCS, len + lcs = 0
62  data[6] == 0xD5 && // TFI - frame from PN532 to system controller
63  data[7] == command + 1); // Correct command response
64 
65  if (!valid_header) {
66  ESP_LOGV(TAG, "read data invalid header!");
67  return false;
68  }
69 
70  data.erase(data.begin(), data.begin() + 6); // Remove headers
71 
72  uint8_t checksum = 0;
73  for (int i = 0; i < len + 1; i++) {
74  uint8_t dat = data[i];
75  checksum += dat;
76  }
77  checksum = ~checksum + 1;
78 
79  if (data[len + 1] != checksum) {
80  ESP_LOGV(TAG, "read data invalid checksum! %02X != %02X", data[len], checksum);
81  return false;
82  }
83 
84  if (data[len + 2] != 0x00) {
85  ESP_LOGV(TAG, "read data invalid postamble!");
86  return false;
87  }
88 
89  data.erase(data.begin(), data.begin() + 2); // Remove TFI and command code
90  data.erase(data.end() - 2, data.end()); // Remove checksum and postamble
91 
92  return true;
93 }
94 
96  std::vector<uint8_t> data;
97  if (!this->read_data(data, 6)) {
98  return 0;
99  }
100 
101  if (data[1] != 0x00 && data[2] != 0x00 && data[3] != 0xFF) {
102  // invalid packet
103  ESP_LOGV(TAG, "read data invalid preamble!");
104  return 0;
105  }
106 
107  bool valid_header = (static_cast<uint8_t>(data[4] + data[5]) == 0 && // LCS, len + lcs = 0
108  data[6] == 0xD5); // TFI - frame from PN532 to system controller
109 
110  if (!valid_header) {
111  ESP_LOGV(TAG, "read data invalid header!");
112  return 0;
113  }
114 
115  this->send_nack_();
116 
117  // full length of message, including TFI
118  uint8_t full_len = data[4];
119  // length of data, excluding TFI
120  uint8_t len = full_len - 1;
121  if (full_len == 0)
122  len = 0;
123  return len;
124 }
125 
127  PN532::dump_config();
128  LOG_I2C_DEVICE(this);
129 }
130 
131 } // namespace pn532_i2c
132 } // namespace esphome
bool read_data(std::vector< uint8_t > &data, uint8_t len) override
Definition: pn532_i2c.cpp:19
bool write_data(const std::vector< uint8_t > &data) override
Definition: pn532_i2c.cpp:15
optional< std::array< uint8_t, N > > read_bytes_raw()
Definition: i2c.h:80
uint32_t IRAM_ATTR HOT millis()
Definition: core.cpp:26
ErrorCode write(const uint8_t *data, uint8_t len, bool stop=true)
Definition: i2c.h:56
uint8_t checksum
Definition: bl0939.h:35
std::string size_t len
Definition: helpers.h:278
Definition: a4988.cpp:4
void dump_config() override
Definition: pn532_i2c.cpp:126
bool read_response(uint8_t command, std::vector< uint8_t > &data) override
Definition: pn532_i2c.cpp:42
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:27