2025-10-28 17:50:43 +09:00
|
|
|
/*
|
|
|
|
|
* SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
|
*/
|
|
|
|
|
/*
|
2026-01-20 17:53:27 +09:00
|
|
|
Example using M5UnitUnified for M5Unit-NFC/RFID
|
2025-11-21 16:23:37 +09:00
|
|
|
Detect NFC-A PICC
|
2026-01-06 14:32:49 +09:00
|
|
|
This example is shared with M5Unit-RFID
|
2025-10-28 17:50:43 +09:00
|
|
|
*/
|
|
|
|
|
#include <M5Unified.h>
|
|
|
|
|
#include <M5UnitUnified.h>
|
|
|
|
|
#include <M5UnitUnifiedNFC.h>
|
|
|
|
|
#include <M5Utility.h>
|
2026-04-06 19:48:03 +09:00
|
|
|
#include <Wire.h>
|
2025-10-28 17:50:43 +09:00
|
|
|
#include <vector>
|
|
|
|
|
|
2025-12-10 15:59:17 +09:00
|
|
|
// *************************************************************
|
2026-05-14 13:42:43 +09:00
|
|
|
// Choose ONE define symbol to match the unit/board you are using
|
2025-12-10 15:59:17 +09:00
|
|
|
// *************************************************************
|
2026-05-14 13:42:43 +09:00
|
|
|
#if !defined(USING_UNIT_NFC) && !defined(USING_CAP_CC1101) && !defined(USING_UNIT_RFID2) && \
|
|
|
|
|
!defined(USING_M5DIAL_BUILTIN_WS1850S)
|
|
|
|
|
// For UnitNFC (ST25R3916, I2C)
|
2025-12-10 15:59:17 +09:00
|
|
|
// #define USING_UNIT_NFC
|
2026-05-14 13:42:43 +09:00
|
|
|
// For CapCC1101NFC (ST25R3916, SPI)
|
2026-04-13 13:37:59 +09:00
|
|
|
// #define USING_CAP_CC1101
|
2026-05-14 13:42:43 +09:00
|
|
|
// For UnitRFID2 (WS1850S external, I2C GROVE)
|
2026-01-06 14:32:49 +09:00
|
|
|
// #define USING_UNIT_RFID2
|
2026-05-14 13:42:43 +09:00
|
|
|
// For M5Dial Builtin WS1850S (internal I2C)
|
|
|
|
|
// #define USING_M5DIAL_BUILTIN_WS1850S
|
2026-01-06 14:32:49 +09:00
|
|
|
#endif
|
|
|
|
|
|
2026-05-14 13:42:43 +09:00
|
|
|
#if defined(USING_UNIT_RFID2) || defined(USING_M5DIAL_BUILTIN_WS1850S)
|
2026-01-06 14:32:49 +09:00
|
|
|
#include <M5UnitUnifiedRFID.h>
|
2025-12-10 15:59:17 +09:00
|
|
|
#endif
|
2025-12-03 19:13:30 +09:00
|
|
|
|
2025-10-28 17:50:43 +09:00
|
|
|
using namespace m5::nfc::a;
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
auto& lcd = M5.Display;
|
|
|
|
|
m5::unit::UnitUnified Units;
|
2025-12-03 19:13:30 +09:00
|
|
|
|
2025-12-10 15:59:17 +09:00
|
|
|
#if defined(USING_UNIT_NFC)
|
|
|
|
|
#pragma message "Choose UnitNFC"
|
|
|
|
|
m5::unit::UnitNFC unit{}; // I2C
|
2026-04-13 13:37:59 +09:00
|
|
|
#elif defined(USING_CAP_CC1101)
|
|
|
|
|
#pragma message "Choose CapCC1101NFC"
|
|
|
|
|
m5::unit::CapCC1101NFC unit{}; // CapCC1101 (SPI)
|
2026-01-06 14:32:49 +09:00
|
|
|
#elif defined(USING_UNIT_RFID2)
|
|
|
|
|
#pragma message "Choose UnitRFID2"
|
2026-05-14 13:42:43 +09:00
|
|
|
m5::unit::UnitRFID2 unit{}; // UnitRFID2 external (M5Unit-RFID, GROVE)
|
|
|
|
|
#elif defined(USING_M5DIAL_BUILTIN_WS1850S)
|
|
|
|
|
#pragma message "Choose UnitRFID2 (M5Dial Builtin)"
|
|
|
|
|
m5::unit::UnitRFID2 unit{}; // M5Dial builtin WS1850S (internal I2C)
|
2025-12-03 19:13:30 +09:00
|
|
|
#else
|
2026-05-14 13:42:43 +09:00
|
|
|
#error Choose ONE: USING_UNIT_NFC / USING_CAP_CC1101 / USING_UNIT_RFID2 / USING_M5DIAL_BUILTIN_WS1850S
|
2025-12-03 19:13:30 +09:00
|
|
|
#endif
|
2025-12-17 00:31:16 +09:00
|
|
|
m5::nfc::NFCLayerA nfc_a{unit};
|
2025-10-28 17:50:43 +09:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
void setup()
|
|
|
|
|
{
|
|
|
|
|
M5.begin();
|
2026-04-06 19:48:03 +09:00
|
|
|
M5.setTouchButtonHeightByRatio(100);
|
|
|
|
|
|
|
|
|
|
// The screen shall be in landscape mode
|
|
|
|
|
if (lcd.height() > lcd.width()) {
|
|
|
|
|
lcd.setRotation(1);
|
|
|
|
|
}
|
2025-10-28 17:50:43 +09:00
|
|
|
|
2026-04-06 19:48:03 +09:00
|
|
|
bool unit_ready{};
|
2026-05-14 13:42:43 +09:00
|
|
|
|
2026-04-06 19:48:03 +09:00
|
|
|
#if defined(USING_M5DIAL_BUILTIN_WS1850S)
|
2026-05-14 13:42:43 +09:00
|
|
|
// M5Dial builtin WS1850S: small loop antenna; reduce RxGain to 33dB to mitigate
|
|
|
|
|
// reflection interference (default 48dB causes unstable WUPA on Builtin).
|
|
|
|
|
{
|
|
|
|
|
auto cfg = unit.config();
|
|
|
|
|
cfg.receiver_gain = m5::unit::mfrc522::ReceiverGain::dB33;
|
|
|
|
|
unit.config(cfg);
|
|
|
|
|
}
|
2026-04-06 19:48:03 +09:00
|
|
|
// M5Dial builtin WS1850S on In_I2C (G12/G11, shared with RTC8563)
|
|
|
|
|
M5_LOGI("Using M5.In_I2C for builtin WS1850S");
|
|
|
|
|
unit_ready = Units.add(unit, M5.In_I2C) && Units.begin();
|
2026-05-14 13:42:43 +09:00
|
|
|
|
|
|
|
|
#elif defined(USING_UNIT_NFC) || defined(USING_UNIT_RFID2)
|
|
|
|
|
// External I2C unit (GROVE port).
|
|
|
|
|
// NessoN1: Arduino Wire (I2C_NUM_0) cannot be used for GROVE port (used by In_I2C internals).
|
|
|
|
|
// Use QWIIC (port_a) with Wire. (Requires QWIIC-GROVE conversion cable)
|
|
|
|
|
// NanoC6: Wire.begin() on GROVE pins conflicts with Ex_I2C on I2C_NUM_0; use M5.Ex_I2C directly.
|
|
|
|
|
if (M5.getBoard() == m5::board_t::board_M5NanoC6) {
|
2026-04-06 19:48:03 +09:00
|
|
|
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();
|
|
|
|
|
}
|
2026-05-14 13:42:43 +09:00
|
|
|
|
2026-04-13 13:37:59 +09:00
|
|
|
#elif defined(USING_CAP_CC1101)
|
2025-10-28 17:50:43 +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 */);
|
|
|
|
|
}
|
2025-10-31 16:38:11 +09:00
|
|
|
SPISettings settings = {10000000, MSBFIRST, SPI_MODE1};
|
2026-05-14 13:42:43 +09:00
|
|
|
unit_ready = Units.add(unit, SPI, settings) && Units.begin();
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
if (!unit_ready) {
|
2025-10-28 17:50:43 +09:00
|
|
|
M5_LOGE("Failed to begin");
|
|
|
|
|
lcd.fillScreen(TFT_RED);
|
|
|
|
|
while (true) {
|
|
|
|
|
m5::utility::delay(10000);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-06 19:48:03 +09:00
|
|
|
M5_LOGI("M5UnitUnified initialized");
|
2025-10-28 17:50:43 +09:00
|
|
|
M5_LOGI("%s", Units.debugInfo().c_str());
|
|
|
|
|
|
|
|
|
|
lcd.setFont(&fonts::Font0);
|
|
|
|
|
lcd.fillScreen(0);
|
2026-04-06 19:48:03 +09:00
|
|
|
lcd.setCursor(0, lcd.height() / 2);
|
2025-10-28 17:50:43 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void loop()
|
|
|
|
|
{
|
|
|
|
|
M5.update();
|
|
|
|
|
Units.update();
|
|
|
|
|
|
2025-12-01 18:32:06 +09:00
|
|
|
std::vector<PICC> piccs;
|
|
|
|
|
if (nfc_a.detect(piccs)) {
|
2025-11-10 18:48:42 +09:00
|
|
|
lcd.fillScreen(0);
|
2026-04-06 19:48:03 +09:00
|
|
|
lcd.setCursor(0, lcd.height() / 2);
|
2026-01-07 20:19:51 +09:00
|
|
|
uint16_t idx{};
|
2025-12-01 18:32:06 +09:00
|
|
|
for (auto&& u : piccs) {
|
2025-12-21 18:30:53 +09:00
|
|
|
M5.Speaker.tone(6000, 5);
|
2026-01-06 14:32:49 +09:00
|
|
|
// detect only performs a provisional classification based on sak, so further identification is required
|
2025-12-21 18:30:53 +09:00
|
|
|
if (nfc_a.identify(u)) {
|
|
|
|
|
M5.Log.printf("PICC:%s %s %04X/%02X %u/%u\n", u.uidAsString().c_str(), u.typeAsString().c_str(), u.atqa,
|
|
|
|
|
u.sak, u.userAreaSize(), u.totalSize());
|
2026-04-13 16:08:21 +09:00
|
|
|
lcd.printf("[%2u]:PICC:<%s> %s\n", static_cast<unsigned>(idx), u.uidAsString().c_str(),
|
|
|
|
|
u.typeAsString().c_str());
|
2025-12-21 18:30:53 +09:00
|
|
|
++idx;
|
|
|
|
|
} else {
|
|
|
|
|
M5_LOGW("Failed to identify %s %s %04X/%02X %u/%u", u.uidAsString().c_str(), u.typeAsString().c_str(),
|
|
|
|
|
u.atqa, u.sak, u.userAreaSize(), u.totalSize());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (idx) {
|
|
|
|
|
M5.Speaker.tone(3000, 10);
|
|
|
|
|
lcd.printf("==> %u PICC\n", idx);
|
|
|
|
|
M5.Log.printf("==> %u PICC\n", idx);
|
2025-10-28 17:50:43 +09:00
|
|
|
}
|
2025-10-31 16:38:11 +09:00
|
|
|
nfc_a.deactivate();
|
2025-10-28 17:50:43 +09:00
|
|
|
}
|
|
|
|
|
}
|