ESPHome  2024.12.2
pn7160_spi.cpp
Go to the documentation of this file.
1 #include "pn7160_spi.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace pn7160_spi {
6 
7 static const char *const TAG = "pn7160_spi";
8 
10  this->spi_setup();
11  this->cs_->digital_write(false);
12  PN7160::setup();
13 }
14 
15 uint8_t PN7160Spi::read_nfcc(nfc::NciMessage &rx, const uint16_t timeout) {
16  if (this->wait_for_irq_(timeout) != nfc::STATUS_OK) {
17  ESP_LOGW(TAG, "read_nfcc_() timeout waiting for IRQ");
18  return nfc::STATUS_FAILED;
19  }
20 
21  rx.get_message().resize(nfc::NCI_PKT_HEADER_SIZE);
22  this->enable();
23  this->write_byte(TDD_SPI_READ); // send "transfer direction detector"
24  this->read_array(rx.get_message().data(), nfc::NCI_PKT_HEADER_SIZE);
25 
26  uint8_t length = rx.get_payload_size();
27  if (length > 0) {
28  rx.get_message().resize(length + nfc::NCI_PKT_HEADER_SIZE);
29  this->read_array(rx.get_message().data() + nfc::NCI_PKT_HEADER_SIZE, length);
30  }
31  this->disable();
32  // semaphore to ensure transaction is complete before returning
33  if (this->wait_for_irq_(pn7160::NFCC_DEFAULT_TIMEOUT, false) != nfc::STATUS_OK) {
34  ESP_LOGW(TAG, "read_nfcc_() post-read timeout waiting for IRQ line to clear");
35  return nfc::STATUS_FAILED;
36  }
37  return nfc::STATUS_OK;
38 }
39 
41  this->enable();
42  this->write_byte(TDD_SPI_WRITE); // send "transfer direction detector"
43  this->write_array(tx.encode().data(), tx.encode().size());
44  this->disable();
45  return nfc::STATUS_OK;
46 }
47 
49  PN7160::dump_config();
50  LOG_PIN(" CS Pin: ", this->cs_);
51 }
52 
53 } // namespace pn7160_spi
54 } // namespace esphome
void setup()
virtual void digital_write(bool value)=0
uint8_t write_nfcc(nfc::NciMessage &tx) override
Definition: pn7160_spi.cpp:40
uint8_t wait_for_irq_(uint16_t timeout=NFCC_DEFAULT_TIMEOUT, bool pin_state=true)
Definition: pn7160.cpp:1154
GPIOPin * cs_
Definition: spi.h:378
std::vector< uint8_t > & get_message()
Definition: nci_message.cpp:67
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
uint8_t read_nfcc(nfc::NciMessage &rx, uint16_t timeout) override
Definition: pn7160_spi.cpp:15