Power down PN532 before deep sleep (#4707)

This commit is contained in:
tracestep 2023-04-30 18:24:15 -03:00 committed by GitHub
parent 2d56b70a36
commit f4b98f5e32
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 0 deletions

View file

@ -81,7 +81,32 @@ void PN532::setup() {
this->turn_off_rf_(); this->turn_off_rf_();
} }
bool PN532::powerdown() {
updates_enabled_ = false;
requested_read_ = false;
ESP_LOGI(TAG, "Powering down PN532");
if (!this->write_command_({PN532_COMMAND_POWERDOWN, 0b10100000})) { // enable i2c,spi wakeup
ESP_LOGE(TAG, "Error writing powerdown command to PN532");
return false;
}
std::vector<uint8_t> response;
if (!this->read_response(PN532_COMMAND_POWERDOWN, response)) {
ESP_LOGE(TAG, "Error reading PN532 powerdown response");
return false;
}
if (response[0] != 0x00) {
ESP_LOGE(TAG, "Error on PN532 powerdown: %02x", response[0]);
return false;
}
ESP_LOGV(TAG, "Powerdown successful");
delay(1);
return true;
}
void PN532::update() { void PN532::update() {
if (!updates_enabled_)
return;
for (auto *obj : this->binary_sensors_) for (auto *obj : this->binary_sensors_)
obj->on_scan_end(); obj->on_scan_end();

View file

@ -17,6 +17,7 @@ static const uint8_t PN532_COMMAND_SAMCONFIGURATION = 0x14;
static const uint8_t PN532_COMMAND_RFCONFIGURATION = 0x32; static const uint8_t PN532_COMMAND_RFCONFIGURATION = 0x32;
static const uint8_t PN532_COMMAND_INDATAEXCHANGE = 0x40; static const uint8_t PN532_COMMAND_INDATAEXCHANGE = 0x40;
static const uint8_t PN532_COMMAND_INLISTPASSIVETARGET = 0x4A; static const uint8_t PN532_COMMAND_INLISTPASSIVETARGET = 0x4A;
static const uint8_t PN532_COMMAND_POWERDOWN = 0x16;
class PN532BinarySensor; class PN532BinarySensor;
@ -30,6 +31,7 @@ class PN532 : public PollingComponent {
float get_setup_priority() const override; float get_setup_priority() const override;
void loop() override; void loop() override;
void on_shutdown() override { powerdown(); }
void register_tag(PN532BinarySensor *tag) { this->binary_sensors_.push_back(tag); } void register_tag(PN532BinarySensor *tag) { this->binary_sensors_.push_back(tag); }
void register_ontag_trigger(nfc::NfcOnTagTrigger *trig) { this->triggers_ontag_.push_back(trig); } void register_ontag_trigger(nfc::NfcOnTagTrigger *trig) { this->triggers_ontag_.push_back(trig); }
@ -45,6 +47,7 @@ class PN532 : public PollingComponent {
void clean_mode(); void clean_mode();
void format_mode(); void format_mode();
void write_mode(nfc::NdefMessage *message); void write_mode(nfc::NdefMessage *message);
bool powerdown();
protected: protected:
void turn_off_rf_(); void turn_off_rf_();
@ -79,6 +82,7 @@ class PN532 : public PollingComponent {
bool write_mifare_ultralight_tag_(std::vector<uint8_t> &uid, nfc::NdefMessage *message); bool write_mifare_ultralight_tag_(std::vector<uint8_t> &uid, nfc::NdefMessage *message);
bool clean_mifare_ultralight_(); bool clean_mifare_ultralight_();
bool updates_enabled_{true};
bool requested_read_{false}; bool requested_read_{false};
std::vector<PN532BinarySensor *> binary_sensors_; std::vector<PN532BinarySensor *> binary_sensors_;
std::vector<nfc::NfcOnTagTrigger *> triggers_ontag_; std::vector<nfc::NfcOnTagTrigger *> triggers_ontag_;