ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
pn7160.cpp
Go to the documentation of this file.
1#include "pn7160.h"
2
3#include "esphome/core/hal.h"
4#include "esphome/core/log.h"
5
6namespace esphome::pn7160 {
7
8ESPHOME_LOG_TAG(TAG, "pn7160");
9
11 if (this->dwl_req_pin_ != nullptr) {
12 this->dwl_req_pin_->setup();
13 }
14 if (this->wkup_req_pin_ != nullptr) {
15 this->wkup_req_pin_->setup();
16 }
17 PN71xx::setup();
18}
19
21 ESP_LOGCONFIG(TAG, "PN7160:");
22 PN71xx::dump_config();
23 if (this->dwl_req_pin_ != nullptr) {
24 LOG_PIN(" DWL_REQ pin: ", this->dwl_req_pin_);
25 }
26 if (this->wkup_req_pin_ != nullptr) {
27 LOG_PIN(" WKUP_REQ pin: ", this->wkup_req_pin_);
28 }
29}
30
32 // DWL_REQ must be low when VEN rises, or the chip starts in firmware download mode (UM11495)
33 if (this->dwl_req_pin_ != nullptr) {
34 this->dwl_req_pin_->digital_write(false);
35 delay(pn71xx::NFCC_RESET_DELAY);
36 }
37}
38
39uint8_t PN7160::verify_reset(nfc::NciMessage &rx, const bool reset_config) {
40 // PN7160 always sends CORE_RESET_NTF after CORE_RESET_RSP (UM11495, 8.2)
41 if (this->read_nfcc(rx, pn71xx::NFCC_INIT_TIMEOUT) != nfc::STATUS_OK) {
42 ESP_LOGE(TAG, "Reset notification was not received");
43 return nfc::STATUS_FAILED;
44 }
45 if ((!rx.message_type_is(nfc::NCI_PKT_MT_CTRL_NOTIFICATION)) || (!rx.message_length_is(9)) ||
46 (rx.get_message()[nfc::NCI_PKT_PAYLOAD_OFFSET] != 0x02) ||
47 (rx.get_message()[nfc::NCI_PKT_PAYLOAD_OFFSET + 1] != (uint8_t) reset_config)) {
48 char buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
49 ESP_LOGE(TAG, "Reset notification was malformed: %s", nfc::format_bytes_to(buf, rx.get_message()));
50 return nfc::STATUS_FAILED;
51 }
52
53 // payload: trigger, config status, NCI version, manufacturer ID, info length (4), then the manufacturer specific
54 // information: hardware, ROM code, FLASH major and FLASH minor versions (UM11495, 8.2 and 8.3)
55 const auto &ntf = rx.get_message();
56 ESP_LOGD(TAG,
57 "Configuration %s, NCI version: 0x%02X, Manufacturer ID: 0x%02X\n"
58 " Hardware version: %u\n"
59 " ROM code version: %u\n"
60 " FLASH major version: %u\n"
61 " FLASH minor version: %u",
62 ntf[4] ? LOG_STR_LITERAL("reset") : LOG_STR_LITERAL("retained"), ntf[5], ntf[6], ntf[8], ntf[9], ntf[10],
63 ntf[11]);
64
65 return nfc::STATUS_OK;
66}
67
69 // the chip's version information is logged from CORE_RESET_NTF in verify_reset()
70 if (rx.get_message().size() >= 8) {
71 char feat_buf[nfc::FORMAT_BYTES_BUFFER_SIZE];
72 ESP_LOGD(TAG, "NFCC features: %s", nfc::format_bytes_to(feat_buf, rx.get_payload().subspan(1, 4)));
73 }
74
76}
77
78} // namespace esphome::pn7160
virtual void setup()=0
virtual void digital_write(bool value)=0
size_t size() const
Definition helpers.h:355
One NCI packet, held in a fixed buffer so building and reading messages never allocates.
Definition nci_message.h:16
bool message_type_is(uint8_t message_type) const
bool message_length_is(uint8_t message_length, bool recompute=false)
std::span< const uint8_t > get_payload() const
The payload bytes that follow the header.
uint8_t get_simple_status_response() const
void dump_config() override
Definition pn7160.cpp:20
void prepare_reset() override
Definition pn7160.cpp:31
void setup() override
Definition pn7160.cpp:10
GPIOPin * wkup_req_pin_
Definition pn7160.h:56
uint8_t verify_reset(nfc::NciMessage &rx, bool reset_config) override
Definition pn7160.cpp:39
GPIOPin * dwl_req_pin_
Definition pn7160.h:55
uint8_t process_init_response(nfc::NciMessage &rx) override
Definition pn7160.cpp:68
virtual uint8_t read_nfcc(nfc::NciMessage &rx, uint16_t timeout)=0
char * format_bytes_to(char *buffer, std::span< const uint8_t > bytes)
Format bytes to buffer with ' ' separator (e.g., "04 11 22 33"). Returns buffer for inline use.
Definition nfc.cpp:14
ESPHOME_LOG_TAG(TAG, "pn7160")
void HOT delay(uint32_t ms)
Definition hal.cpp:85