ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
pn7160_spi.cpp
Go to the documentation of this file.
1#include "pn7160_spi.h"
2#include "esphome/core/log.h"
3
5
6ESPHOME_LOG_TAG(TAG, "pn7160_spi");
7
9 this->spi_setup();
10 PN7160::setup();
11}
12
13uint8_t PN7160Spi::read_nfcc(nfc::NciMessage &rx, const uint16_t timeout) {
14 if (this->wait_for_irq_(timeout) != nfc::STATUS_OK) {
15 ESP_LOGW(TAG, "read_nfcc_() timeout waiting for IRQ");
16 return nfc::STATUS_FAILED;
17 }
18
19 rx.reset();
20 this->enable();
21 this->write_byte(TDD_SPI_READ); // send "transfer direction detector"
22 this->read_array(rx.get_message().data(), nfc::NCI_PKT_HEADER_SIZE);
23
24 const uint8_t length = rx.get_payload_size();
26 if (length > 0) {
27 this->read_array(rx.get_message().data() + nfc::NCI_PKT_HEADER_SIZE, length);
28 }
29 this->disable();
30 // IRQ normally drops at the end of the read. If another message is queued it rises again at once, and the short
31 // low pulse may be missed; that means more data is waiting, not that this read failed (UM11495, 6.3.4).
32 if (this->wait_for_irq_(pn71xx::NFCC_IRQ_CLEAR_TIMEOUT, false) != nfc::STATUS_OK) {
33 ESP_LOGVV(TAG, "IRQ still active after read; another message is pending");
34 }
35 return nfc::STATUS_OK;
36}
37
39 const auto encoded = tx.encode();
40 this->enable();
41 // send "transfer direction detector"; the NFCC answers 0xFF when it is ready to receive (UM11495, 6.3.3)
42 const uint8_t status = this->transfer_byte(TDD_SPI_WRITE);
43 const bool ready = status == 0xFF;
44 if (ready) {
45 this->write_array(encoded.data(), encoded.size());
46 }
47 this->disable();
48 if (!ready) {
49 ESP_LOGV(TAG, "NFCC not ready for write (0x%02X)", status);
50 }
51 return ready ? nfc::STATUS_OK : nfc::STATUS_FAILED;
52}
53
55 PN7160::dump_config();
56 LOG_PIN(" CS Pin: ", this->cs_);
57}
58
59} // namespace esphome::pn7160_spi
uint8_t status
Definition bl0942.h:8
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 read_nfcc(nfc::NciMessage &rx, uint16_t timeout) override
uint8_t write_nfcc(nfc::NciMessage &tx) override
uint8_t wait_for_irq_(uint16_t timeout=NFCC_DEFAULT_TIMEOUT, bool pin_state=true)
Definition pn71xx.cpp:1177
ESPHOME_LOG_TAG(TAG, "pn7160_spi")