Files

217 lines
6.5 KiB
C++
Raw Permalink Normal View History

2025-12-02 12:43:39 +09:00
/*
* SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
*
* SPDX-License-Identifier: MIT
*/
/*
2025-12-10 15:59:17 +09:00
Example using M5UnitUnified for ST25R3916
Read/write NDEF NFC-F PICC
2025-12-02 12:43:39 +09:00
*/
#include <M5Unified.h>
#include <M5UnitUnified.h>
#include <M5UnitUnifiedNFC.h>
#include <M5Utility.h>
#include <vector>
2025-12-10 15:59:17 +09:00
// *************************************************************
// Choose one define symbol to match the unit you are using
// *************************************************************
#if !defined(USING_UNIT_NFC) && !defined(USING_CAP_CC1101)
2025-12-10 15:59:17 +09:00
// For UnitNFC
// #define USING_UNIT_NFC
// For CapNFC
// #define USING_CAP_CC1101
2025-12-10 15:59:17 +09:00
#endif
2025-12-02 12:43:39 +09:00
using namespace m5::nfc;
using namespace m5::nfc::f;
using namespace m5::nfc::ndef;
namespace {
auto& lcd = M5.Display;
m5::unit::UnitUnified Units;
2025-12-10 15:59:17 +09:00
#if defined(USING_UNIT_NFC)
#pragma message "Choose UnitNFC"
m5::unit::UnitNFC unit{}; // I2C
#elif defined(USING_CAP_CC1101)
#pragma message "Choose CapCC1101NFC"
m5::unit::CapCC1101NFC unit{}; // CapCC1101 (SPI)
2025-12-10 15:59:17 +09:00
#else
#error Choose unit please!
#endif
2025-12-17 00:31:16 +09:00
m5::nfc::NFCLayerF nfc_f{unit};
2025-12-02 12:43:39 +09:00
2025-12-02 17:43:53 +09:00
void read_ndef()
2025-12-02 12:43:39 +09:00
{
2025-12-02 17:43:53 +09:00
TLV msg{};
2025-12-02 12:43:39 +09:00
// Read NDEF message TLV
if (!nfc_f.ndefRead(msg)) {
M5_LOGE("Failed to read");
2026-01-30 17:46:07 +09:00
lcd.fillScreen(TFT_RED);
2025-12-02 12:43:39 +09:00
return;
}
// If it does not exist, a Null TLV is returned
if (msg.isMessageTLV()) {
lcd.setCursor(0, lcd.fontHeight());
2026-01-30 17:46:07 +09:00
M5.Log.printf("==== NDEF Message %zu records ====\n", msg.records().size());
2025-12-02 12:43:39 +09:00
for (auto&& r : msg.records()) {
switch (r.tnf()) {
case TNF::Wellknown: {
const auto payload = r.payloadAsString();
M5.Log.printf("SZ:%3u TNF:%u T:%s [%s]\n", r.payloadSize(), r.tnf(), r.type(), payload.c_str());
lcd.printf("T:%s [%s]\n", r.type(), payload.c_str());
2025-12-02 12:43:39 +09:00
} break;
default:
M5.Log.printf("SZ:%3u TNF:%u T:%s\n", r.payloadSize(), r.tnf(), r.type());
break;
}
}
} else {
M5.Log.printf("NDEF Message TLV is NOT exists\n");
}
}
2025-12-02 17:43:53 +09:00
void write_ndef()
2025-12-02 12:43:39 +09:00
{
2025-12-02 17:43:53 +09:00
TLV msg{Tag::Message}; // NDEF Message TLV
Record r[4] = {}; // Wellknown as default
2025-12-02 12:43:39 +09:00
2026-01-30 17:46:07 +09:00
// *********************************************************
2025-12-02 12:43:39 +09:00
// Change format to support NDEF
2026-01-30 17:46:07 +09:00
// *********************************************************
2025-12-02 12:43:39 +09:00
if (!nfc_f.writeSupportNDEF(true)) {
M5_LOGE("Failed to writeSupportNDEF");
return;
}
// URI record
r[0].setURIPayload("m5stack.com/", URIProtocol::HTTPS);
// Text record with language type
2025-12-02 12:43:39 +09:00
const char* en_data = "Hello M5Stack";
r[1].setTextPayload(en_data, "en");
const char* ja_data = "こんにちは M5Stack";
r[2].setTextPayload(ja_data, "ja");
const char* zh_data = "你好 M5Stack";
r[3].setTextPayload(zh_data, "zh");
2025-12-02 17:43:53 +09:00
uint32_t max_user_size = nfc_f.activatedPICC().userAreaSize() - 1 /* terminator TLV */;
2025-12-02 12:43:39 +09:00
for (auto&& rr : r) {
msg.push_back(rr);
if (msg.required() > max_user_size) {
msg.pop_back();
break;
}
}
if (!nfc_f.ndefWrite(msg)) {
M5_LOGE("Failed to write");
return;
}
nfc_f.dump();
}
} // namespace
void setup()
{
M5.begin();
2025-12-10 15:59:17 +09:00
M5.setTouchButtonHeightByRatio(100);
2025-12-02 12:43:39 +09:00
2025-12-10 15:59:17 +09:00
auto cfg = unit.config();
2025-12-02 12:43:39 +09:00
cfg.mode = NFC::F;
2025-12-10 15:59:17 +09:00
unit.config(cfg);
2025-12-02 12:43:39 +09:00
2025-12-10 15:59:17 +09:00
#if defined(USING_UNIT_NFC)
auto board = M5.getBoard();
bool unit_ready{};
// NessoN1: SoftwareI2C too slow for NFC RF timing -> use port_a (Wire) via else branch
if (board == m5::board_t::board_M5NanoC6) {
M5_LOGI("Using M5.Ex_I2C");
unit_ready = Units.add(unit, M5.Ex_I2C) && Units.begin();
} else {
auto pin_num_sda = M5.getPin(m5::pin_name_t::port_a_sda);
auto pin_num_scl = M5.getPin(m5::pin_name_t::port_a_scl);
M5_LOGI("getPin: SDA:%u SCL:%u", pin_num_sda, pin_num_scl);
Wire.end();
Wire.begin(pin_num_sda, pin_num_scl, 400 * 1000U);
unit_ready = Units.add(unit, Wire) && Units.begin();
}
if (!unit_ready) {
2025-12-10 15:59:17 +09:00
M5_LOGE("Failed to begin");
lcd.fillScreen(TFT_RED);
2025-12-10 15:59:17 +09:00
while (true) {
m5::utility::delay(10000);
}
}
#elif defined(USING_CAP_CC1101)
2025-12-02 12:43:39 +09:00
if (!SPI.bus()) {
auto spi_sclk = M5.getPin(m5::pin_name_t::sd_spi_sclk);
auto spi_mosi = M5.getPin(m5::pin_name_t::sd_spi_mosi);
auto spi_miso = M5.getPin(m5::pin_name_t::sd_spi_miso);
M5_LOGI("getPin: %d,%d,%d", spi_sclk, spi_mosi, spi_miso);
SPI.begin(spi_sclk, spi_miso, spi_mosi /* SS is shared SD, CC1101, ST25R3916 */);
}
SPISettings settings = {10000000, MSBFIRST, SPI_MODE1};
2025-12-10 15:59:17 +09:00
if (!Units.add(unit, SPI, settings) || !Units.begin()) {
2025-12-02 12:43:39 +09:00
M5_LOGE("Failed to begin");
lcd.fillScreen(TFT_RED);
while (true) {
m5::utility::delay(10000);
}
}
2025-12-10 15:59:17 +09:00
#endif
M5_LOGI("M5UnitUnified initialized");
2025-12-02 12:43:39 +09:00
M5_LOGI("%s", Units.debugInfo().c_str());
if (lcd.height() > lcd.width()) {
2025-12-02 12:43:39 +09:00
lcd.setRotation(1);
}
lcd.setFont(&fonts::Font0);
lcd.fillScreen(0);
lcd.setCursor(0, 0);
2026-01-05 11:16:56 +09:00
lcd.printf("Please put the PICC and click BtnA");
M5.Log.printf("Please put the PICC and click BtnA\n");
2025-12-02 12:43:39 +09:00
}
void loop()
{
M5.update();
Units.update();
bool clicked = M5.BtnA.wasClicked(); // For read
bool held = M5.BtnA.wasHold(); // For write
if (clicked || held) {
PICC picc{};
if (nfc_f.detect(picc)) {
if (nfc_f.activate(picc)) {
2025-12-02 17:43:53 +09:00
M5.Log.printf("%s:%s %s F:%02X DF:%04X\n", picc.idmAsString().c_str(), picc.pmmAsString().c_str(),
2025-12-02 12:43:39 +09:00
picc.typeAsString().c_str(), picc.format, picc.dfc_format);
if (clicked) {
M5.Speaker.tone(2000, 30);
lcd.fillScreen(TFT_BLUE);
2026-01-30 17:46:07 +09:00
// nfc_f.dump();
2025-12-02 17:43:53 +09:00
read_ndef();
2025-12-02 12:43:39 +09:00
} else if (held) {
M5.Speaker.tone(4000, 30);
lcd.fillScreen(TFT_YELLOW);
2025-12-02 17:43:53 +09:00
write_ndef();
2026-01-30 17:46:07 +09:00
lcd.fillScreen(0);
2025-12-02 12:43:39 +09:00
}
M5.Log.printf("Please remove the PICC from the reader\n");
nfc_f.deactivate();
}
lcd.setCursor(0, 0);
2026-01-05 11:16:56 +09:00
lcd.printf("Please put the PICC and click/hold BtnA");
M5.Log.printf("Please put the PICC and click/hold BtnA\n");
2025-12-02 12:43:39 +09:00
} else {
M5.Log.printf("PICC NOT exists\n");
}
}
}