mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-06-13 16:13:32 +02:00
Fix PN532 SPI communication (#1511)
This commit is contained in:
@@ -26,7 +26,7 @@ bool PN532Spi::write_data(const std::vector<uint8_t> &data) {
|
||||
delay(2);
|
||||
// First byte, communication mode: Write data
|
||||
this->write_byte(0x01);
|
||||
|
||||
ESP_LOGV(TAG, "Writing data: %s", hexencode(data).c_str());
|
||||
this->write_array(data.data(), data.size());
|
||||
this->disable();
|
||||
|
||||
@@ -34,31 +34,122 @@ bool PN532Spi::write_data(const std::vector<uint8_t> &data) {
|
||||
}
|
||||
|
||||
bool PN532Spi::read_data(std::vector<uint8_t> &data, uint8_t len) {
|
||||
this->enable();
|
||||
// First byte, communication mode: Read state
|
||||
this->write_byte(0x02);
|
||||
ESP_LOGV(TAG, "Waiting for ready byte...");
|
||||
|
||||
uint32_t start_time = millis();
|
||||
while (true) {
|
||||
if (this->read_byte() & 0x01)
|
||||
this->enable();
|
||||
// First byte, communication mode: Read state
|
||||
this->write_byte(0x02);
|
||||
bool ready = this->read_byte() == 0x01;
|
||||
this->disable();
|
||||
if (ready)
|
||||
break;
|
||||
ESP_LOGV(TAG, "Not ready yet...");
|
||||
|
||||
if (millis() - start_time > 100) {
|
||||
this->disable();
|
||||
ESP_LOGV(TAG, "Timed out waiting for readiness from PN532!");
|
||||
return false;
|
||||
}
|
||||
yield();
|
||||
}
|
||||
|
||||
// Read data (transmission from the PN532 to the host)
|
||||
this->enable();
|
||||
delay(2);
|
||||
this->write_byte(0x03);
|
||||
|
||||
ESP_LOGV(TAG, "Reading data...");
|
||||
|
||||
data.resize(len);
|
||||
this->read_array(data.data(), len);
|
||||
this->disable();
|
||||
data.insert(data.begin(), 0x01);
|
||||
ESP_LOGV(TAG, "Read data: %s", hexencode(data).c_str());
|
||||
return true;
|
||||
};
|
||||
}
|
||||
|
||||
bool PN532Spi::read_response(uint8_t command, std::vector<uint8_t> &data) {
|
||||
ESP_LOGV(TAG, "Reading response");
|
||||
|
||||
uint32_t start_time = millis();
|
||||
while (true) {
|
||||
this->enable();
|
||||
// First byte, communication mode: Read state
|
||||
this->write_byte(0x02);
|
||||
bool ready = this->read_byte() == 0x01;
|
||||
this->disable();
|
||||
if (ready)
|
||||
break;
|
||||
ESP_LOGV(TAG, "Not ready yet...");
|
||||
|
||||
if (millis() - start_time > 100) {
|
||||
ESP_LOGV(TAG, "Timed out waiting for readiness from PN532!");
|
||||
return false;
|
||||
}
|
||||
yield();
|
||||
}
|
||||
|
||||
this->enable();
|
||||
delay(2);
|
||||
this->write_byte(0x03);
|
||||
|
||||
std::vector<uint8_t> header(7);
|
||||
this->read_array(header.data(), 7);
|
||||
|
||||
ESP_LOGV(TAG, "Header data: %s", hexencode(header).c_str());
|
||||
|
||||
if (header[0] != 0x00 && header[1] != 0x00 && header[2] != 0xFF) {
|
||||
// invalid packet
|
||||
ESP_LOGV(TAG, "read data invalid preamble!");
|
||||
return false;
|
||||
}
|
||||
|
||||
bool valid_header = (static_cast<uint8_t>(header[3] + header[4]) == 0 && // LCS, len + lcs = 0
|
||||
header[5] == 0xD5 && // TFI - frame from PN532 to system controller
|
||||
header[6] == command + 1); // Correct command response
|
||||
|
||||
if (!valid_header) {
|
||||
ESP_LOGV(TAG, "read data invalid header!");
|
||||
return false;
|
||||
}
|
||||
|
||||
// full length of message, including command response
|
||||
uint8_t full_len = header[3];
|
||||
// length of data, excluding command response
|
||||
uint8_t len = full_len - 1;
|
||||
if (full_len == 0)
|
||||
len = 0;
|
||||
|
||||
ESP_LOGV(TAG, "Reading response of length %d", len);
|
||||
|
||||
data.resize(len + 1);
|
||||
this->read_array(data.data(), len + 1);
|
||||
this->disable();
|
||||
|
||||
ESP_LOGV(TAG, "Response data: %s", hexencode(data).c_str());
|
||||
|
||||
uint8_t checksum = header[5] + header[6]; // TFI + Command response code
|
||||
for (int i = 0; i < len - 1; i++) {
|
||||
uint8_t dat = data[i];
|
||||
checksum += dat;
|
||||
}
|
||||
checksum = ~checksum + 1;
|
||||
|
||||
if (data[len - 1] != checksum) {
|
||||
ESP_LOGV(TAG, "read data invalid checksum! %02X != %02X", data[len - 1], checksum);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (data[len] != 0x00) {
|
||||
ESP_LOGV(TAG, "read data invalid postamble!");
|
||||
return false;
|
||||
}
|
||||
|
||||
data.erase(data.end() - 2, data.end()); // Remove checksum and postamble
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void PN532Spi::dump_config() {
|
||||
PN532::dump_config();
|
||||
|
||||
@@ -18,6 +18,7 @@ class PN532Spi : public pn532::PN532,
|
||||
protected:
|
||||
bool write_data(const std::vector<uint8_t> &data) override;
|
||||
bool read_data(std::vector<uint8_t> &data, uint8_t len) override;
|
||||
bool read_response(uint8_t command, std::vector<uint8_t> &data) override;
|
||||
};
|
||||
|
||||
} // namespace pn532_spi
|
||||
|
||||
Reference in New Issue
Block a user