ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
pn7160_i2c.cpp
Go to the documentation of this file.
1#include "pn7160_i2c.h"
2#include "esphome/core/log.h"
3#include "esphome/core/hal.h"
4
6
7ESPHOME_LOG_TAG(TAG, "pn7160_i2c");
8
9uint8_t PN7160I2C::read_nfcc(nfc::NciMessage &rx, const uint16_t timeout) {
10 if (this->wait_for_irq_(timeout) != nfc::STATUS_OK) {
11 ESP_LOGW(TAG, "read_nfcc_() timeout waiting for IRQ");
12 return nfc::STATUS_FAILED;
13 }
14
15 rx.reset();
16 if (!this->read_bytes_raw(rx.get_message().data(), nfc::NCI_PKT_HEADER_SIZE)) {
17 return nfc::STATUS_FAILED;
18 }
19
20 const uint8_t length = rx.get_payload_size();
22 if (length > 0 && !this->read_bytes_raw(rx.get_message().data() + nfc::NCI_PKT_HEADER_SIZE, length)) {
23 return nfc::STATUS_FAILED;
24 }
25 // IRQ normally drops at the end of the read. If another message is queued it rises again at once, and the short
26 // low pulse may be missed; that means more data is waiting, not that this read failed (UM11495, 6.2.4).
27 if (this->wait_for_irq_(pn71xx::NFCC_IRQ_CLEAR_TIMEOUT, false) != nfc::STATUS_OK) {
28 ESP_LOGVV(TAG, "IRQ still active after read; another message is pending");
29 }
30 return nfc::STATUS_OK;
31}
32
34 const auto encoded = tx.encode();
35 if (this->write(encoded.data(), encoded.size()) == i2c::ERROR_OK) {
36 return nfc::STATUS_OK;
37 }
38 return nfc::STATUS_FAILED;
39}
40
42 PN7160::dump_config();
43 LOG_I2C_DEVICE(this);
44}
45
46} // namespace esphome::pn7160_i2c
ErrorCode write(const uint8_t *data, size_t len) const
writes an array of bytes to a device using an I2CBus
Definition i2c.h:195
optional< std::array< uint8_t, N > > read_bytes_raw()
Definition i2c.h:262
One NCI packet, held in a fixed buffer so building and reading messages never allocates.
Definition nci_message.h:16
uint8_t get_payload_size(bool recompute=false)
std::span< const uint8_t > encode()
Stamps the payload length into the header and returns the packet ready to send.
void set_payload_size(uint8_t size)
Sets the packet size to the header plus size payload bytes, for a bus driver filling the buffer direc...
uint8_t write_nfcc(nfc::NciMessage &tx) override
uint8_t read_nfcc(nfc::NciMessage &rx, uint16_t timeout) override
Definition pn7160_i2c.cpp:9
uint8_t wait_for_irq_(uint16_t timeout=NFCC_DEFAULT_TIMEOUT, bool pin_state=true)
Definition pn71xx.cpp:1177
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:15
ESPHOME_LOG_TAG(TAG, "pn7160_i2c")