10 static const uint8_t WAIT_I_RQ = 0x30;
12 static const char *
const TAG =
"rc522";
14 static const uint8_t RESET_COUNT = 5;
19 for (uint8_t i = 0; i <
len; i++) {
20 const char *format =
"%02X";
23 offset += sprintf(buf + offset, format, b[i]);
25 return std::string(buf);
31 for (
size_t i = 0; i < uid.size(); i++) {
32 const char *format =
"%02X";
33 if (i + 1 < uid.size())
35 offset += sprintf(buf + offset, format, uid[i]);
37 return std::string(buf);
49 ESP_LOGV(TAG,
"Power down mode detected. Hard resetting...");
72 ESP_LOGV(TAG,
"Initialize");
99 ESP_LOGCONFIG(TAG,
"RC522:");
100 switch (this->error_code_) {
104 ESP_LOGE(TAG,
"Reset command failed!");
110 LOG_UPDATE_INTERVAL(
this);
113 LOG_BINARY_SENSOR(
" ",
"Tag", child);
125 ESP_LOGW(TAG,
"Communication takes longer than update interval: %d", state_);
152 ESP_LOGV(TAG,
"finished communication status: %d, state: %d", status, state_);
160 ESP_LOGV(TAG,
"CMD_REQA -> TIMEOUT (no tag present) %d", status);
163 ESP_LOGW(TAG,
"CMD_REQA -> Not OK %d", status);
166 ESP_LOGW(TAG,
"CMD_REQA -> OK, but unexpected back_length_ of %d",
back_length_);
178 ESP_LOGV(TAG,
"STATE_READ_SERIAL (%d)", status);
190 ESP_LOGE(TAG,
"uid_idx_ invalid, uid_idx_ = %d",
uid_idx_);
215 ESP_LOGV(TAG,
"STATE_READ_SERIAL_DONE -> TIMEOUT (no tag present) %d", status);
217 ESP_LOGW(TAG,
"Unexpected response. Read status is %d. Read bytes: %d (%s)", status,
back_length_,
230 for (uint8_t i = 2 + cascade; i < 6; i++)
248 if (tag->process(rfid_uid)) {
260 trigger->process(rfid_uid);
263 ESP_LOGD(TAG,
"Found new tag '%s'",
format_uid(rfid_uid).c_str());
295 ESP_LOGI(TAG,
"Soft reset...");
303 ESP_LOGI(TAG,
"Device online.");
310 ESP_LOGE(TAG,
"Unable to reset RC522.");
322 if ((value & 0x03) != 0x03) {
332 if ((value & 0x03) != 0x00) {
393 if (!(n & WAIT_I_RQ)) {
398 ESP_LOGW(TAG,
"Communication with the MFRC522 might be down, reset in %d",
410 if (error_reg_value & 0x13) {
422 uint8_t valid_bits_local =
427 if (error_reg_value & 0x08) {
428 ESP_LOGW(TAG,
"collision error, received %d bytes + %d bits (but anticollision not implemented)",
429 back_length_ - (valid_bits_local > 0), valid_bits_local);
433 if (valid_bits_local) {
434 ESP_LOGW(TAG,
"only %d valid bits received, tag distance to high? Error code is 0x%x", valid_bits_local,
452 ESP_LOGVV(TAG,
"pcd_calculate_crc_(..., %d, ...)", length);
475 ESP_LOGVV(TAG,
"pcd_calculate_crc_() STATUS_OK");
481 ESP_LOGD(TAG,
"pcd_calculate_crc_() TIMEOUT");
488 if (data.size() != this->uid_.size()) {
491 for (
size_t i = 0; i < data.size(); i++) {
492 if (data[i] != this->uid_[i]) {
498 this->publish_state(result);
499 this->found_ = result;
virtual void digital_write(bool value)=0
void pcd_reset_()
Performs a soft reset on the MFRC522 chip and waits for it to be ready again.
void pcd_set_register_bit_mask_(PcdRegister reg, uint8_t mask)
Sets the bits given in mask in register reg.
uint8_t buffer_[9]
buffer for communication, the first bits [0..back_idx-1] are for tx , [back_idx..back_idx+back_len] f...
std::vector< RC522Trigger * > triggers_ontag_
void pcd_transceive_data_(uint8_t send_len)
Transfers data to the MFRC522 FIFO, executes a command, waits for completion and transfers data back ...
virtual void pcd_write_register(PcdRegister reg, uint8_t value)=0
virtual void pin_mode(gpio::Flags flags)=0
void dump_config() override
uint8_t back_length_
In: Max number of uint8_ts to write to *backData. Out: The number of uint8_ts returned.
uint32_t IRAM_ATTR HOT millis()
void pcd_calculate_crc_(uint8_t *data, uint8_t length)
Use the CRC coprocessor in the MFRC522 to calculate a CRC_A.
std::vector< RC522Trigger * > triggers_ontagremoved_
uint32_t awaiting_comm_time_
bool process(std::vector< uint8_t > &data)
std::string format_uid(std::vector< uint8_t > &uid)
void process(std::vector< uint8_t > &data)
virtual bool digital_read()=0
enum esphome::rc522::RC522::RC522Error NONE
void pcd_antenna_off_()
Turns the antenna off by disabling pins TX1 and TX2.
virtual void mark_failed()
Mark this component as failed.
Implementation of SPI Controller mode.
std::vector< uint8_t > current_uid_
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
StatusCode await_transceive_()
virtual uint8_t pcd_read_register(PcdRegister reg)=0
void pcd_antenna_on_()
Turns the antenna on by enabling pins TX1 and TX2.
void pcd_clear_register_bit_mask_(PcdRegister reg, uint8_t mask)
Clears the bits given in mask from register reg.
std::string format_buffer(uint8_t *b, uint8_t len)
std::vector< RC522BinarySensor * > binary_sensors_