ESPHome  2024.7.0
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 
5 namespace esphome {
6 namespace pn7160_i2c {
7 
8 static const char *const TAG = "pn7160_i2c";
9 
10 uint8_t PN7160I2C::read_nfcc(nfc::NciMessage &rx, const uint16_t timeout) {
11  if (this->wait_for_irq_(timeout) != nfc::STATUS_OK) {
12  ESP_LOGW(TAG, "read_nfcc_() timeout waiting for IRQ");
13  return nfc::STATUS_FAILED;
14  }
15 
16  rx.get_message().resize(nfc::NCI_PKT_HEADER_SIZE);
17  if (!this->read_bytes_raw(rx.get_message().data(), nfc::NCI_PKT_HEADER_SIZE)) {
18  return nfc::STATUS_FAILED;
19  }
20 
21  uint8_t length = rx.get_payload_size();
22  if (length > 0) {
23  rx.get_message().resize(length + nfc::NCI_PKT_HEADER_SIZE);
24  if (!this->read_bytes_raw(rx.get_message().data() + nfc::NCI_PKT_HEADER_SIZE, length)) {
25  return nfc::STATUS_FAILED;
26  }
27  }
28  // semaphore to ensure transaction is complete before returning
29  if (this->wait_for_irq_(pn7160::NFCC_DEFAULT_TIMEOUT, false) != nfc::STATUS_OK) {
30  ESP_LOGW(TAG, "read_nfcc_() post-read timeout waiting for IRQ line to clear");
31  return nfc::STATUS_FAILED;
32  }
33  return nfc::STATUS_OK;
34 }
35 
37  if (this->write(tx.encode().data(), tx.encode().size()) == i2c::ERROR_OK) {
38  return nfc::STATUS_OK;
39  }
40  return nfc::STATUS_FAILED;
41 }
42 
44  PN7160::dump_config();
45  LOG_I2C_DEVICE(this);
46 }
47 
48 } // namespace pn7160_i2c
49 } // namespace esphome
uint8_t read_nfcc(nfc::NciMessage &rx, uint16_t timeout) override
Definition: pn7160_i2c.cpp:10
uint8_t wait_for_irq_(uint16_t timeout=NFCC_DEFAULT_TIMEOUT, bool pin_state=true)
Definition: pn7160.cpp:1154
uint8_t write_nfcc(nfc::NciMessage &tx) override
Definition: pn7160_i2c.cpp:36
optional< std::array< uint8_t, N > > read_bytes_raw()
Definition: i2c.h:225
ErrorCode write(const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a device using an I2CBus
Definition: i2c.h:186
std::vector< uint8_t > & get_message()
Definition: nci_message.cpp:67
No error found during execution of method.
Definition: i2c_bus.h:13
uint8_t get_payload_size(bool recompute=false)
Definition: nci_message.cpp:43
uint16_t length
Definition: tt21100.cpp:12
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
std::vector< uint8_t > encode()
Definition: nci_message.cpp:27