mirror of
https://github.com/esphome/esphome.git
synced 2026-09-21 00:55:38 +08:00
Pn532 upgrades (#1302)
* Move pn532 -> pn532_spi Add pn532_i2c * Update i2c address * Always wait for ready byte before reading * Generalise the pn532 a bit more so less code in i2c and spi implementations * clang * Add pn532_i2c to test1 * Try to get setup working * Fixes * More updates * Command consts * A few upgrades * Change text back to include 'new' * Fix data reading
This commit is contained in:
+3
-1
@@ -43,7 +43,9 @@ esphome/components/network/* @esphome/core
|
||||
esphome/components/ota/* @esphome/core
|
||||
esphome/components/output/* @esphome/core
|
||||
esphome/components/pid/* @OttoWinter
|
||||
esphome/components/pn532/* @OttoWinter
|
||||
esphome/components/pn532/* @OttoWinter @jesserockz
|
||||
esphome/components/pn532_i2c/* @OttoWinter @jesserockz
|
||||
esphome/components/pn532_spi/* @OttoWinter @jesserockz
|
||||
esphome/components/power_supply/* @esphome/core
|
||||
esphome/components/rc522_spi/* @glmnet
|
||||
esphome/components/restart/* @esphome/core
|
||||
|
||||
@@ -1,30 +1,37 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome import automation
|
||||
from esphome.components import spi
|
||||
from esphome.const import CONF_ID, CONF_ON_TAG, CONF_TRIGGER_ID
|
||||
from esphome.const import CONF_ON_TAG, CONF_TRIGGER_ID
|
||||
from esphome.core import coroutine
|
||||
|
||||
CODEOWNERS = ['@OttoWinter']
|
||||
DEPENDENCIES = ['spi']
|
||||
CODEOWNERS = ['@OttoWinter', '@jesserockz']
|
||||
AUTO_LOAD = ['binary_sensor']
|
||||
MULTI_CONF = True
|
||||
|
||||
CONF_PN532_ID = 'pn532_id'
|
||||
|
||||
pn532_ns = cg.esphome_ns.namespace('pn532')
|
||||
PN532 = pn532_ns.class_('PN532', cg.PollingComponent, spi.SPIDevice)
|
||||
PN532 = pn532_ns.class_('PN532', cg.PollingComponent)
|
||||
|
||||
PN532Trigger = pn532_ns.class_('PN532Trigger', automation.Trigger.template(cg.std_string))
|
||||
|
||||
CONFIG_SCHEMA = cv.Schema({
|
||||
PN532_SCHEMA = cv.Schema({
|
||||
cv.GenerateID(): cv.declare_id(PN532),
|
||||
cv.Optional(CONF_ON_TAG): automation.validate_automation({
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(PN532Trigger),
|
||||
}),
|
||||
}).extend(cv.polling_component_schema('1s')).extend(spi.spi_device_schema())
|
||||
}).extend(cv.polling_component_schema('1s'))
|
||||
|
||||
|
||||
def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
def CONFIG_SCHEMA(conf):
|
||||
if conf:
|
||||
raise cv.Invalid("This component has been moved in 1.16, please see the docs for updated "
|
||||
"instructions. https://esphome.io/components/binary_sensor/pn532.html")
|
||||
|
||||
|
||||
@coroutine
|
||||
def setup_pn532(var, config):
|
||||
yield cg.register_component(var, config)
|
||||
yield spi.register_spi_device(var, config)
|
||||
|
||||
for conf in config.get(CONF_ON_TAG, []):
|
||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID])
|
||||
|
||||
@@ -3,12 +3,10 @@ import esphome.config_validation as cv
|
||||
from esphome.components import binary_sensor
|
||||
from esphome.const import CONF_UID, CONF_ID
|
||||
from esphome.core import HexInt
|
||||
from . import pn532_ns, PN532
|
||||
from . import pn532_ns, PN532, CONF_PN532_ID
|
||||
|
||||
DEPENDENCIES = ['pn532']
|
||||
|
||||
CONF_PN532_ID = 'pn532_id'
|
||||
|
||||
|
||||
def validate_uid(value):
|
||||
value = cv.string_strict(value)
|
||||
|
||||
+174
-213
File diff suppressed because it is too large
Load Diff
@@ -3,17 +3,20 @@
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/automation.h"
|
||||
#include "esphome/components/binary_sensor/binary_sensor.h"
|
||||
#include "esphome/components/spi/spi.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace pn532 {
|
||||
|
||||
static const uint8_t PN532_COMMAND_VERSION_DATA = 0x02;
|
||||
static const uint8_t PN532_COMMAND_SAMCONFIGURATION = 0x14;
|
||||
static const uint8_t PN532_COMMAND_RFCONFIGURATION = 0x32;
|
||||
static const uint8_t PN532_COMMAND_INDATAEXCHANGE = 0x40;
|
||||
static const uint8_t PN532_COMMAND_INLISTPASSIVETARGET = 0x4A;
|
||||
|
||||
class PN532BinarySensor;
|
||||
class PN532Trigger;
|
||||
|
||||
class PN532 : public PollingComponent,
|
||||
public spi::SPIDevice<spi::BIT_ORDER_LSB_FIRST, spi::CLOCK_POLARITY_LOW, spi::CLOCK_PHASE_LEADING,
|
||||
spi::DATA_RATE_1MHZ> {
|
||||
class PN532 : public PollingComponent {
|
||||
public:
|
||||
void setup() override;
|
||||
|
||||
@@ -28,38 +31,19 @@ class PN532 : public PollingComponent,
|
||||
void register_trigger(PN532Trigger *trig) { this->triggers_.push_back(trig); }
|
||||
|
||||
protected:
|
||||
/// Write the full command given in data to the PN532
|
||||
void pn532_write_command_(const std::vector<uint8_t> &data);
|
||||
bool pn532_write_command_check_ack_(const std::vector<uint8_t> &data);
|
||||
|
||||
/** Read a data frame from the PN532 and return the result as a vector.
|
||||
*
|
||||
* Note that is_ready needs to be checked first before requesting this method.
|
||||
*
|
||||
* On failure, an empty vector is returned.
|
||||
*/
|
||||
std::vector<uint8_t> pn532_read_data_();
|
||||
|
||||
/** Checks if the PN532 has set its ready status flag.
|
||||
*
|
||||
* Procedure goes as follows:
|
||||
* - Host sends command to PN532 "write data"
|
||||
* - Wait for readiness (until PN532 has processed command) by polling "read status"/is_ready_
|
||||
* - Parse ACK/NACK frame with "read data" byte
|
||||
*
|
||||
* - If data required, wait until device reports readiness again
|
||||
* - Then call "read data" and read certain number of bytes (length is given at offset 4 of frame)
|
||||
*/
|
||||
bool is_ready_();
|
||||
bool wait_ready_();
|
||||
|
||||
bool read_ack_();
|
||||
|
||||
void turn_off_rf_();
|
||||
bool write_command_(const std::vector<uint8_t> &data);
|
||||
bool read_response_(uint8_t command, std::vector<uint8_t> &data);
|
||||
bool read_ack_();
|
||||
uint8_t read_response_length_();
|
||||
|
||||
virtual bool write_data(const std::vector<uint8_t> &data) = 0;
|
||||
virtual bool read_data(std::vector<uint8_t> &data, uint8_t len) = 0;
|
||||
|
||||
bool requested_read_{false};
|
||||
std::vector<PN532BinarySensor *> binary_sensors_;
|
||||
std::vector<PN532Trigger *> triggers_;
|
||||
std::vector<uint8_t> current_uid_;
|
||||
enum PN532Error {
|
||||
NONE = 0,
|
||||
WAKEUP_FAILED,
|
||||
@@ -71,7 +55,7 @@ class PN532BinarySensor : public binary_sensor::BinarySensor {
|
||||
public:
|
||||
void set_uid(const std::vector<uint8_t> &uid) { uid_ = uid; }
|
||||
|
||||
bool process(const uint8_t *data, uint8_t len);
|
||||
bool process(std::vector<uint8_t> &data);
|
||||
|
||||
void on_scan_end() {
|
||||
if (!this->found_) {
|
||||
@@ -87,7 +71,7 @@ class PN532BinarySensor : public binary_sensor::BinarySensor {
|
||||
|
||||
class PN532Trigger : public Trigger<std::string> {
|
||||
public:
|
||||
void process(const uint8_t *uid, uint8_t uid_length);
|
||||
void process(std::vector<uint8_t> &data);
|
||||
};
|
||||
|
||||
} // namespace pn532
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import i2c, pn532
|
||||
from esphome.const import CONF_ID
|
||||
|
||||
AUTO_LOAD = ['pn532']
|
||||
CODEOWNERS = ['@OttoWinter', '@jesserockz']
|
||||
DEPENDENCIES = ['i2c']
|
||||
|
||||
pn532_i2c_ns = cg.esphome_ns.namespace('pn532_i2c')
|
||||
PN532I2C = pn532_i2c_ns.class_('PN532I2C', pn532.PN532, i2c.I2CDevice)
|
||||
|
||||
CONFIG_SCHEMA = cv.All(pn532.PN532_SCHEMA.extend({
|
||||
cv.GenerateID(): cv.declare_id(PN532I2C),
|
||||
}).extend(i2c.i2c_device_schema(0x24)))
|
||||
|
||||
|
||||
def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
yield pn532.setup_pn532(var, config)
|
||||
yield i2c.register_i2c_device(var, config)
|
||||
@@ -0,0 +1,45 @@
|
||||
#include "pn532_i2c.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
// Based on:
|
||||
// - https://cdn-shop.adafruit.com/datasheets/PN532C106_Application+Note_v1.2.pdf
|
||||
// - https://www.nxp.com/docs/en/nxp/application-notes/AN133910.pdf
|
||||
// - https://www.nxp.com/docs/en/nxp/application-notes/153710.pdf
|
||||
|
||||
namespace esphome {
|
||||
namespace pn532_i2c {
|
||||
|
||||
static const char *TAG = "pn532_i2c";
|
||||
|
||||
bool PN532I2C::write_data(const std::vector<uint8_t> &data) { return this->write_bytes_raw(data.data(), data.size()); }
|
||||
|
||||
bool PN532I2C::read_data(std::vector<uint8_t> &data, uint8_t len) {
|
||||
delay(5);
|
||||
|
||||
std::vector<uint8_t> ready;
|
||||
ready.resize(1);
|
||||
uint32_t start_time = millis();
|
||||
while (true) {
|
||||
if (this->read_bytes_raw(ready.data(), 1)) {
|
||||
if (ready[0] == 0x01)
|
||||
break;
|
||||
}
|
||||
|
||||
if (millis() - start_time > 100) {
|
||||
ESP_LOGV(TAG, "Timed out waiting for readiness from PN532!");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
data.resize(len + 1);
|
||||
this->read_bytes_raw(data.data(), len + 1);
|
||||
return true;
|
||||
}
|
||||
|
||||
void PN532I2C::dump_config() {
|
||||
PN532::dump_config();
|
||||
LOG_I2C_DEVICE(this);
|
||||
}
|
||||
|
||||
} // namespace pn532_i2c
|
||||
} // namespace esphome
|
||||
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/pn532/pn532.h"
|
||||
#include "esphome/components/i2c/i2c.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace pn532_i2c {
|
||||
|
||||
class PN532I2C : public pn532::PN532, public i2c::I2CDevice {
|
||||
public:
|
||||
void dump_config() override;
|
||||
|
||||
protected:
|
||||
bool write_data(const std::vector<uint8_t> &data) override;
|
||||
bool read_data(std::vector<uint8_t> &data, uint8_t len) override;
|
||||
};
|
||||
|
||||
} // namespace pn532_i2c
|
||||
} // namespace esphome
|
||||
@@ -0,0 +1,21 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import spi, pn532
|
||||
from esphome.const import CONF_ID
|
||||
|
||||
AUTO_LOAD = ['pn532']
|
||||
CODEOWNERS = ['@OttoWinter', '@jesserockz']
|
||||
DEPENDENCIES = ['spi']
|
||||
|
||||
pn532_spi_ns = cg.esphome_ns.namespace('pn532_spi')
|
||||
PN532Spi = pn532_spi_ns.class_('PN532Spi', pn532.PN532, spi.SPIDevice)
|
||||
|
||||
CONFIG_SCHEMA = cv.All(pn532.PN532_SCHEMA.extend({
|
||||
cv.GenerateID(): cv.declare_id(PN532Spi),
|
||||
}).extend(spi.spi_device_schema(cs_pin_required=True)))
|
||||
|
||||
|
||||
def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
yield pn532.setup_pn532(var, config)
|
||||
yield spi.register_spi_device(var, config)
|
||||
@@ -0,0 +1,69 @@
|
||||
#include "pn532_spi.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
// Based on:
|
||||
// - https://cdn-shop.adafruit.com/datasheets/PN532C106_Application+Note_v1.2.pdf
|
||||
// - https://www.nxp.com/docs/en/nxp/application-notes/AN133910.pdf
|
||||
// - https://www.nxp.com/docs/en/nxp/application-notes/153710.pdf
|
||||
|
||||
namespace esphome {
|
||||
namespace pn532_spi {
|
||||
|
||||
static const char *TAG = "pn532_spi";
|
||||
|
||||
void PN532Spi::setup() {
|
||||
ESP_LOGI(TAG, "PN532Spi setup started!");
|
||||
this->spi_setup();
|
||||
|
||||
this->cs_->digital_write(false);
|
||||
delay(10);
|
||||
ESP_LOGI(TAG, "SPI setup finished!");
|
||||
PN532::setup();
|
||||
}
|
||||
|
||||
bool PN532Spi::write_data(const std::vector<uint8_t> &data) {
|
||||
this->enable();
|
||||
delay(2);
|
||||
// First byte, communication mode: Write data
|
||||
this->write_byte(0x01);
|
||||
|
||||
this->write_array(data.data(), data.size());
|
||||
this->disable();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool PN532Spi::read_data(std::vector<uint8_t> &data, uint8_t len) {
|
||||
this->enable();
|
||||
// First byte, communication mode: Read state
|
||||
this->write_byte(0x02);
|
||||
|
||||
uint32_t start_time = millis();
|
||||
while (true) {
|
||||
if (this->read_byte() & 0x01)
|
||||
break;
|
||||
|
||||
if (millis() - start_time > 100) {
|
||||
this->disable();
|
||||
ESP_LOGV(TAG, "Timed out waiting for readiness from PN532!");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Read data (transmission from the PN532 to the host)
|
||||
this->write_byte(0x03);
|
||||
|
||||
data.resize(len);
|
||||
this->read_array(data.data(), len);
|
||||
this->disable();
|
||||
data.insert(data.begin(), 0x01);
|
||||
return true;
|
||||
};
|
||||
|
||||
void PN532Spi::dump_config() {
|
||||
PN532::dump_config();
|
||||
LOG_PIN(" CS Pin: ", this->cs_);
|
||||
}
|
||||
|
||||
} // namespace pn532_spi
|
||||
} // namespace esphome
|
||||
@@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/pn532/pn532.h"
|
||||
#include "esphome/components/spi/spi.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace pn532_spi {
|
||||
|
||||
class PN532Spi : public pn532::PN532,
|
||||
public spi::SPIDevice<spi::BIT_ORDER_LSB_FIRST, spi::CLOCK_POLARITY_LOW, spi::CLOCK_PHASE_LEADING,
|
||||
spi::DATA_RATE_1MHZ> {
|
||||
public:
|
||||
void setup() override;
|
||||
|
||||
void dump_config() override;
|
||||
|
||||
protected:
|
||||
bool write_data(const std::vector<uint8_t> &data) override;
|
||||
bool read_data(std::vector<uint8_t> &data, uint8_t len) override;
|
||||
};
|
||||
|
||||
} // namespace pn532_spi
|
||||
} // namespace esphome
|
||||
+3
-1
@@ -1645,7 +1645,7 @@ remote_receiver:
|
||||
status_led:
|
||||
pin: GPIO2
|
||||
|
||||
pn532:
|
||||
pn532_spi:
|
||||
cs_pin: GPIO23
|
||||
update_interval: 1s
|
||||
on_tag:
|
||||
@@ -1655,6 +1655,8 @@ pn532:
|
||||
topic: the/topic
|
||||
payload: !lambda 'return x;'
|
||||
|
||||
pn532_i2c:
|
||||
|
||||
rdm6300:
|
||||
|
||||
rc522_spi:
|
||||
|
||||
Reference in New Issue
Block a user