1
0
mirror of https://github.com/Sucareto/ESP32-CardReader.git synced 2024-11-23 23:00:56 +01:00

更新内容:

添加 SpiceTools 模式;
改为使用已修改的 PN532 和 SpiceAPI 库;
移除实现不正确的 USB 连接状态显示;
重新排布 OLED 的显示文字和图标位置;
修改 SW 开关定义,移除没能实现的 BLE 刷卡功能;
删除冗余代码。
This commit is contained in:
Sucareto 2023-02-04 21:32:28 +08:00
parent 7a279737d3
commit a2c939a3b0
3 changed files with 539 additions and 482 deletions

View File

@ -1,55 +1,54 @@
#if defined(ARDUINO_NodeMCU_32S) #ifdef ARDUINO_NodeMCU_32S
#pragma message "当前的开发板是 NodeMCU_32S" #pragma message "当前的开发板是 NodeMCU_32S"
#define SerialDevice Serial #define SerialDevice Serial
#define LED_PIN 13 #define LED_PIN 13
#define PN532_SPI_SS 5 #define PN532_SPI_SS 5
// #define OTA_Enable
#define SW1 33 #define SW1 33
#define SW2 25 #define SW2 25
#define SW3 26 #define SW3 26
#define SW4 27 #define SW4 27
bool ReaderMode, FWSW;
#ifdef OTA_Enable
#pragma message "已启用OTA更新" #pragma message "已启用OTA更新"
#define STASSID "ALL.Net Wi-Fi" #define STASSID "ALL.Net Wi-Fi"
#define STAPASS "SEGASEGASEGA" #define STAPASS "SEGASEGASEGA"
#define OTA_URL "http://esp-update.local/Sucareto/ESP32-Reader:2333/" #define OTA_URL "http://esp-update.local/Sucareto/ESP32-Reader:2333/"
#include <WiFi.h> #include <WiFi.h>
#include <HTTPUpdate.h> #include <HTTPUpdate.h>
#endif
#else #else
#error "开发板选错了!!!" #error "开发板选错了!!!"
#endif #endif
#include "Aime-Reader.h" #include "ReaderCmd.h"
void (*ReaderMain)();
void setup() { void setup() {
pinMode(SW1, INPUT_PULLUP); pinMode(SW1, INPUT_PULLUP); // Switch to Spice mode
pinMode(SW2, INPUT_PULLUP); pinMode(SW2, INPUT_PULLUP); // Enable OTA
pinMode(SW3, INPUT_PULLUP); pinMode(SW3, INPUT_PULLUP); // LED brightness
pinMode(SW4, INPUT_PULLUP); pinMode(SW4, INPUT_PULLUP); // (Aime) Baudrate & fw/hw | (Spice) 1P 2P
bool high_baudrate = !digitalRead(SW1);
FWSW = !digitalRead(SW2);
u8g2.begin(); u8g2.begin();
u8g2.setFont(u8g2_font_6x12_mf); u8g2.setFont(u8g2_font_6x12_mf);
u8g2.drawXBM(0, 0, 16, 16, usb_disconnect);
u8g2.drawStr(48, 10, high_baudrate ? "HIGH" : "LOW");
u8g2.sendBuffer();
SerialDevice.begin(high_baudrate ? 115200 : 38400);
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, 8); FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, 8);
if (!digitalRead(SW3)) { if (digitalRead(SW3)) {
FastLED.setBrightness(20); FastLED.setBrightness(20);
} }
FastLED.showColor(0); FastLED.showColor(0);
if (!digitalRead(SW4)) { #ifdef OTA_Enable
if (!digitalRead(SW1)) { // OTA check
WiFi.begin(STASSID, STAPASS); WiFi.begin(STASSID, STAPASS);
u8g2.drawStr(0, 28, "WiFi Connecting..."); u8g2.drawStr(0, 28, "WiFi Connecting...");
u8g2.sendBuffer(); u8g2.sendBuffer();
while (WiFi.status() != WL_CONNECTED); while (WiFi.status() != WL_CONNECTED) {
delay(100);
};
u8g2.drawStr(0, 28, "WiFi connected. "); u8g2.drawStr(0, 28, "WiFi connected. ");
u8g2.drawStr(0, 41, "Check update... "); u8g2.drawStr(0, 41, "Check update... ");
u8g2.sendBuffer(); u8g2.sendBuffer();
@ -65,105 +64,92 @@ void setup() {
u8g2.drawStr(0, 41, "Already up to date. "); u8g2.drawStr(0, 41, "Already up to date. ");
break; break;
} }
u8g2.sendBuffer();
} }
#endif
nfc.begin(); nfc.begin();
while (!nfc.getFirmwareVersion()) { while (!nfc.getFirmwareVersion()) {
u8g2.drawXBM(20, 0, 16, 16, rf_off); u8g2.drawXBM(95, 0, 16, 16, rf_off);
u8g2.sendBuffer(); u8g2.sendBuffer();
FastLED.showColor(0xFF0000); FastLED.showColor(0xFF0000);
delay(500); delay(500);
FastLED.showColor(0); FastLED.showColor(0);
delay(500); delay(500);
} }
nfc.setPassiveActivationRetries(0x10);//设定等待次数 nfc.setPassiveActivationRetries(0x10);
nfc.SAMConfig(); nfc.SAMConfig();
u8g2.clearBuffer();
ReaderMode = !digitalRead(SW1);
FWSW = digitalRead(SW4);
if (ReaderMode) { // BEMANI mode
SerialDevice.begin(115200);
u8g2.drawStr(0, 14, "SpiceTools");
u8g2.drawStr(117, 64, FWSW ? "1P" : "2P");
FastLED.showColor(CRGB::Yellow);
ReaderMain = SpiceToolsReader;
} else { // Aime mode
SerialDevice.begin(FWSW ? 38400 : 115200);
u8g2.drawStr(0, 16, "Aime Reader");
u8g2.drawStr(105, 64, FWSW ? "LOW" : "HIGH");
u8g2.drawStr(0, 64, FWSW ? "TN32MSEC003S" : "837-15396");
FastLED.showColor(FWSW ? CRGB::Green : CRGB::Blue);
ReaderMain = AimeCardReader;
}
memset(&req, 0, sizeof(req.bytes)); memset(&req, 0, sizeof(req.bytes));
memset(&res, 0, sizeof(res.bytes)); memset(&res, 0, sizeof(res.bytes));
u8g2.drawXBM(20, 0, 16, 16, blank);
u8g2.drawStr(0, 64, FWSW ? "TN32MSEC003S" : "837-15396");
u8g2.sendBuffer(); u8g2.sendBuffer();
FastLED.showColor(high_baudrate ? 0x0000FF : 0x00FF00);
} }
unsigned long ConnectTime = 0;
bool ConnectStatus = false;
void loop() { void loop() {
SerialCheck(); ReaderMain();
packet_write();
if (ConnectStatus & millis() - ConnectTime > 5000) {
u8g2.drawXBM(0, 0, 16, 16, usb_disconnect);
u8g2.sendBuffer();
ConnectStatus = false;
}
} }
static uint8_t len, r, checksum;
static bool escape = false;
static uint8_t packet_read() { void SpiceToolsReader() { // Spice mode
while (SerialDevice.available()) { uint16_t SystemCode;
r = SerialDevice.read(); char card_id[17];
if (r == 0xE0) { if (nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, res.mifare_uid, &res.id_len)
req.frame_len = 0xFF; && nfc.mifareclassic_AuthenticateBlock(res.mifare_uid, res.id_len, 1, 0, MifareKey)
continue; && nfc.mifareclassic_ReadDataBlock(1, res.block)) {
} sprintf(card_id, "%02X%02X%02X%02X%02X%02X%02X%02X",
if (req.frame_len == 0xFF) { res.block[0], res.block[1], res.block[2], res.block[3],
req.frame_len = r; res.block[4], res.block[5], res.block[6], res.block[7]);
len = 0;
checksum = r;
continue;
}
if (r == 0xD0) {
escape = true;
continue;
}
if (escape) {
r++;
escape = false;
}
req.bytes[++len] = r;
if (len == req.frame_len && checksum == r) {
ConnectStatus = true;
ConnectTime = millis();
return req.cmd;
}
checksum += r;
}
return 0;
}
static void packet_write() { } else if (nfc.felica_Polling(0xFFFF, 0x00, res.IDm, res.PMm, &SystemCode, 200) == 1) {
uint8_t checksum = 0, len = 0; sprintf(card_id, "%02X%02X%02X%02X%02X%02X%02X%02X",
if (res.cmd == 0) { res.IDm[0], res.IDm[1], res.IDm[2], res.IDm[3],
res.IDm[4], res.IDm[5], res.IDm[6], res.IDm[7]);
} else {
return; return;
} }
SerialDevice.write(0xE0); u8g2.drawXBM(113, 0, 16, 16, card);
while (len <= res.frame_len) { u8g2.sendBuffer();
uint8_t w; spiceapi::InfoAvs avs_info{};
if (len == res.frame_len) { if (spiceapi::info_avs(CON, avs_info)) {
w = checksum; FWSW = digitalRead(SW4);
} else { spiceapi::card_insert(CON, !FWSW, card_id);
w = res.bytes[len]; u8g2.drawStr(0, 30, card_id);
checksum += w; u8g2.drawStr(0, 64, (avs_info.model + ":" + avs_info.dest + avs_info.spec + avs_info.rev + ":" + avs_info.ext).c_str());
u8g2.drawStr(117, 64, FWSW ? "1P" : "2P");
u8g2.sendBuffer();
for (int i = 0; i < 8; i++) {
leds[i] = CRGB::Red;
leds[7 - i] = CRGB::Blue;
FastLED.delay(50);
leds[i] = CRGB::Black;
leds[7 - i] = CRGB::Black;
} }
if (w == 0xE0 || w == 0xD0) { FastLED.show();
SerialDevice.write(0xD0);
SerialDevice.write(--w);
} else {
SerialDevice.write(w);
}
len++;
} }
res.cmd = 0; u8g2.drawXBM(113, 0, 16, 16, blank);
u8g2.drawStr(0, 30, " ");
u8g2.sendBuffer();
} }
void SerialCheck() {
void AimeCardReader() { // Aime mode
switch (packet_read()) { switch (packet_read()) {
case SG_NFC_CMD_RESET: case SG_NFC_CMD_RESET:
sg_nfc_cmd_reset(); sg_nfc_cmd_reset();
@ -190,7 +176,7 @@ void SerialCheck() {
sg_nfc_cmd_bana_authenticate(); sg_nfc_cmd_bana_authenticate();
break; break;
case SG_NFC_CMD_MIFARE_SELECT_TAG: case SG_NFC_CMD_MIFARE_SELECT_TAG:
sg_nfc_cmd_mifare_select_tag(); sg_res_init();
break; break;
case SG_NFC_CMD_MIFARE_SET_KEY_AIME: case SG_NFC_CMD_MIFARE_SET_KEY_AIME:
sg_nfc_cmd_mifare_set_key_aime(); sg_nfc_cmd_mifare_set_key_aime();
@ -214,8 +200,9 @@ void SerialCheck() {
sg_led_cmd_set_color(); sg_led_cmd_set_color();
break; break;
case 0: case 0:
break; return;
default: default:
sg_res_init(); sg_res_init();
} }
packet_write();
} }

View File

@ -1,9 +1,16 @@
# ESP32-CardReader # ESP32-CardReader
- 此项目为 [Arduino-Aime-Reader](https://github.com/Sucareto/Arduino-Aime-Reader) 的使用示例。 - 此项目为 [Arduino-Aime-Reader](https://github.com/Sucareto/Arduino-Aime-Reader) 的使用示例,使用的主控模块是 NodeMCU-32S
- 主控模块是 NodeMCU-32S添加了 SSD1306 模块显示信息。 - 添加了 SSD1306 模块展示状态
- 可以使用拨码开关切换连接波特率和修改其他设置,也可以切换为 HSU 直通模式。 - 添加了拨码开关用于切换读卡器模式和波特率,也可以切换为 HSU 直通模式
- 直通模式可以给 Android 和 PC 连接作为 USB PN532 读卡器使用,例如使用 [MIFARE Classic Tool (MCT)](https://github.com/ikarus23/MifareClassicTool) 或 [MifareOneTool](https://github.com/xcicode/MifareOneTool) 之类的软件。 - 直通模式可以给 Android 或 PC 连接作为 USB PN532 读卡器使用,例如使用 [MIFARE Classic Tool (MCT)](https://github.com/ikarus23/MifareClassicTool) 或 [MifareOneTool](https://github.com/xcicode/MifareOneTool) 等读卡软件
- 在原版 Aime 读卡器的基础上添加了 SpiceTools 模式,可以读取自定义的 MIFARE 卡和 FeliCa 卡
- MIFARE 读取 sector 0 block 1 的前半部分作为卡号发送(可以通过代码定义)
- FeliCa 读取 idm 作为卡号发送
- **仅用于分辨卡片和账号,读卡逻辑和卡号计算未正确实现**
**本项目仅用于展示原项目 [Arduino-Aime-Reader](https://github.com/Sucareto/Arduino-Aime-Reader) 的使用示例,未曾开展或参与成品售卖,也不会向商业化制作提供任何支持。**
## 实物图: ## 实物图:
<details><summary>点击展开</summary> <details><summary>点击展开</summary>
@ -18,18 +25,32 @@ https://user-images.githubusercontent.com/28331534/170975661-137f3474-f61a-4a4d-
## 使用说明: ## 使用说明:
### PCB 设计: ### PCB 设计:
因为不舍得花时间去画 PCB所以 ESP32 和 PN532 就使用了模块直插的方式,或许以后有机会的话... - 该 PCB 方案仅用于我自己快速测试,并非最佳实现方案
- 如果需要使用此 PCB 方案,请务必把 logo 删掉,然后根据需求认真审查 PCB 设计并作出修改后再进行制作
### 拨码开关: ### 拨码开关:
在 PCB 放置了两个 4P 拨码开关 在 PCB 放置了两个 4P 拨码开关
#### SW 1-4可修改代码自定义 #### SW 1-4用于更改读卡器功能切换后需要重启
- SW1切换波特率对应`high_baudrate` - SW1切换读卡器模式
- SW2切换固件显示版本`TN32MSEC003S` 或 `837-15396` - ONSpiceTools 模式,需要在 SpiceTools 添加启动参数 `-apiserial COM1 -apiserialbaud 115200`"COM1" 改为实际的端口号
- SW3切换 BLE 连接(未实现) - OFFAime 模式,和 [Arduino-Aime-Reader](https://github.com/Sucareto/Arduino-Aime-Reader) 使用方法一致
- SW4启用 OTA 更新 - SW2切换启动时的 OTA 开关
- ON连接 WiFi 获取更新,如未能连接到 WiFi 则会持续到连接成功后才能启动
- OFF跳过检查更新直接启动
- SW3切换 LED 亮度
- ON全亮度
- OFF亮度降低到指定值
- SW4不同模式的功能切换
- Spice 模式(可在运行中切换)
- ON卡号发送到 2P 槽位(需要游戏支持)
- OFF卡号发送到 1P 槽位
- Aime 模式
- ON使用 38400 波特率初始化,固件版本是 TN32MSEC003S
- OFF使用 115200 波特率初始化,固件版本是 837-15396
#### TTL用于切换至 “USB PN532 读卡器”模式) #### TTL用于切换至 “USB PN532 读卡器”模式,需要在断电情况下切换
- SW1连接到 ESP32 的 `EN` 引脚,调为 `ON` 后会停用 ESP32只使用 CH340 串口通信芯片 - SW1连接到 ESP32 的 `EN` 引脚,调为 `ON` 后会停用 ESP32只使用 CH340 串口通信芯片
- SW2在目前版本v2为空置 - SW2在目前版本v2为空置
- SW3 & SW4连接 CH340 和 PN532 的 `RX TX`引脚 - SW3 & SW4连接 CH340 和 PN532 的 `RX TX`引脚
@ -48,8 +69,12 @@ https://user-images.githubusercontent.com/28331534/170975661-137f3474-f61a-4a4d-
## 感谢: ## 感谢:
- 原项目:[Arduino-Aime-Reader](https://github.com/Sucareto/Arduino-Aime-Reader) - 原项目:[Arduino-Aime-Reader](https://github.com/Sucareto/Arduino-Aime-Reader)
- 驱动 WS2812B[FastLED](https://github.com/FastLED/FastLED)
- 操作 SSD1306[u8g2](https://github.com/olikraus/u8g2) - 操作 SSD1306[u8g2](https://github.com/olikraus/u8g2)
- Spice 通信(已复制到本仓库并根据需要修改了部分代码):[SpiceAPI](https://github.com/spicetools/spicetools/tree/master/api/resources/arduino)
- 驱动 PN532已复制到本仓库并根据需要修改了部分代码[PN532](https://github.com/elechouse/PN532)
- OLED 显示图案设计PCtoLCD2002 - OLED 显示图案设计PCtoLCD2002
- AimePad 盖板设计:[CoolBreezeArcanine](https://github.com/CoolBreezeArcanine) - AimePad 盖板设计:[CoolBreezeArcanine](https://github.com/CoolBreezeArcanine)
- PCB 图案:[妖夢 - ぷりん](https://www.pixiv.net/artworks/87578487) - PCB 图案:[妖夢 - ぷりん](https://www.pixiv.net/artworks/87578487)
- OTA 代码参考:[OTA Updates](https://arduino-esp8266.readthedocs.io/en/latest/ota_updates/readme.html#http-server) - OTA 代码参考:[OTA Updates](https://arduino-esp8266.readthedocs.io/en/latest/ota_updates/readme.html#http-server)
- Spice 通信参考:[PN5180-cardio SpiceAPI branch](https://github.com/CrazyRedMachine/PN5180-cardio/tree/SpiceAPI)

View File

@ -1,380 +1,425 @@
#include <SPI.h> #include "src/PN532_SPI.h"
#include <PN532_SPI.h> PN532_SPI pn532(SPI, PN532_SPI_SS);
PN532_SPI pn532(SPI, PN532_SPI_SS);
#include "src/PN532.h"
#include "PN532.h" PN532 nfc(pn532);
PN532 nfc(pn532);
#include "FastLED.h"
#include "FastLED.h" CRGB leds[8];
CRGB leds[8];
#include <U8g2lib.h>
#include <U8g2lib.h> U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0);
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0);
#define SPICEAPI_INTERFACE Serial
static unsigned char usb_disconnect[] = { #include "src/wrappers.h"
0x00, 0x40, 0x00, 0xAE, 0x00, 0x51, 0x80, 0x20, 0x00, 0x41, 0x80, 0x42, 0x40, 0x44, 0x08, 0x28, spiceapi::Connection CON(512);
0x14, 0x14, 0x22, 0x02, 0x42, 0x00, 0x82, 0x00, 0x04, 0x01, 0x8A, 0x00, 0x75, 0x00, 0x02, 0x00
};
// static unsigned char usb_disconnect[] = {
static unsigned char usb_connect[] = { // 0x00, 0x40, 0x00, 0xAE, 0x00, 0x51, 0x80, 0x20, 0x00, 0x41, 0x80, 0x42, 0x40, 0x44, 0x08, 0x28,
0x00, 0x40, 0x00, 0xA0, 0x00, 0x51, 0x80, 0x2A, 0x60, 0x14, 0x50, 0x08, 0xB0, 0x10, 0x48, 0x21, // 0x14, 0x14, 0x22, 0x02, 0x42, 0x00, 0x82, 0x00, 0x04, 0x01, 0x8A, 0x00, 0x75, 0x00, 0x02, 0x00
0x84, 0x12, 0x08, 0x0D, 0x10, 0x0A, 0x28, 0x06, 0x54, 0x01, 0x8A, 0x00, 0x05, 0x00, 0x02, 0x00 // };
}; // static unsigned char usb_connect[] = {
static unsigned char rf_open[] = { // 0x00, 0x40, 0x00, 0xA0, 0x00, 0x51, 0x80, 0x2A, 0x60, 0x14, 0x50, 0x08, 0xB0, 0x10, 0x48, 0x21,
0x00, 0x00, 0x00, 0x00, 0x04, 0x20, 0x02, 0x40, 0x02, 0x40, 0x11, 0x88, 0x91, 0x89, 0x49, 0x92, // 0x84, 0x12, 0x08, 0x0D, 0x10, 0x0A, 0x28, 0x06, 0x54, 0x01, 0x8A, 0x00, 0x05, 0x00, 0x02, 0x00
0x49, 0x92, 0x91, 0x89, 0x11, 0x88, 0x02, 0x40, 0x02, 0x40, 0x04, 0x20, 0x00, 0x00, 0x00, 0x00 // };
}; static unsigned char rf_open[] = {
0x00, 0x00, 0x00, 0x00, 0x04, 0x20, 0x02, 0x40, 0x02, 0x40, 0x11, 0x88, 0x91, 0x89, 0x49, 0x92,
static unsigned char rf_off[] = { 0x49, 0x92, 0x91, 0x89, 0x11, 0x88, 0x02, 0x40, 0x02, 0x40, 0x04, 0x20, 0x00, 0x00, 0x00, 0x00
0x01, 0x00, 0x02, 0x00, 0x04, 0x20, 0x0A, 0x40, 0x12, 0x40, 0x21, 0x88, 0x51, 0x89, 0x89, 0x92, };
0x49, 0x93, 0x91, 0x8A, 0x11, 0x8C, 0x02, 0x48, 0x02, 0x50, 0x04, 0x20, 0x00, 0x40, 0x00, 0x80
}; static unsigned char rf_off[] = {
static unsigned char card [] = { 0x01, 0x00, 0x02, 0x00, 0x04, 0x20, 0x0A, 0x40, 0x12, 0x40, 0x21, 0x88, 0x51, 0x89, 0x89, 0x92,
0x00, 0x00, 0xFC, 0x3F, 0x02, 0x40, 0x32, 0x48, 0x52, 0x48, 0x92, 0x48, 0x12, 0x49, 0x12, 0x4A, 0x49, 0x93, 0x91, 0x8A, 0x11, 0x8C, 0x02, 0x48, 0x02, 0x50, 0x04, 0x20, 0x00, 0x40, 0x00, 0x80
0x52, 0x48, 0x92, 0x48, 0x12, 0x49, 0x12, 0x4A, 0x12, 0x4C, 0x02, 0x40, 0xFC, 0x3F, 0x00, 0x00 };
}; static unsigned char card[] = {
0x00, 0x00, 0xFC, 0x3F, 0x02, 0x40, 0x32, 0x48, 0x52, 0x48, 0x92, 0x48, 0x12, 0x49, 0x12, 0x4A,
static unsigned char blank [] = { 0x52, 0x48, 0x92, 0x48, 0x12, 0x49, 0x12, 0x4A, 0x12, 0x4C, 0x02, 0x40, 0xFC, 0x3F, 0x00, 0x00
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, };
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
}; static unsigned char blank[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
uint8_t AimeKey[6], BanaKey[6]; 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
//TN32MSEC003S
char FW_TN32[] = {'T', 'N', '3', '2', 'M', 'S', 'E', 'C', '0', '0', '3', 'S', ' ', 'F', '/', 'W', ' ', 'V', 'e', 'r', '1', '.', '2'}; uint8_t AimeKey[6], BanaKey[6], MifareKey[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
char HW_TN32[] = {'T', 'N', '3', '2', 'M', 'S', 'E', 'C', '0', '0', '3', 'S', ' ', 'H', '/', 'W', ' ', 'V', 'e', 'r', '3', '.', '0'};
char BOARD_TN32[] = {'1', '5', '0', '8', '4', 0xFF, 0x10, 0x00, 0x12}; //TN32MSEC003S
char FW_TN32[] = { 'T', 'N', '3', '2', 'M', 'S', 'E', 'C', '0', '0', '3', 'S', ' ', 'F', '/', 'W', ' ', 'V', 'e', 'r', '1', '.', '2' };
//837-15396 char HW_TN32[] = { 'T', 'N', '3', '2', 'M', 'S', 'E', 'C', '0', '0', '3', 'S', ' ', 'H', '/', 'W', ' ', 'V', 'e', 'r', '3', '.', '0' };
char FW_837[] = {0x94}; char BOARD_TN32[] = { '1', '5', '0', '8', '4', 0xFF, 0x10, 0x00, 0x12 };
char HW_837[] = {'8', '3', '7', '-', '1', '5', '3', '9', '6'};
char BOARD_837[] = {'0', '0', '0', '-', '0', '0', '0', '0', '0', 0xFF, 0x11, 0x40}; //837-15396
char FW_837[] = { 0x94 };
bool FWSW; char HW_837[] = { '8', '3', '7', '-', '1', '5', '3', '9', '6' };
enum { char BOARD_837[] = { '0', '0', '0', '-', '0', '0', '0', '0', '0', 0xFF, 0x11, 0x40 };
SG_NFC_CMD_GET_FW_VERSION = 0x30,
SG_NFC_CMD_GET_HW_VERSION = 0x32, enum {
SG_NFC_CMD_RADIO_ON = 0x40, SG_NFC_CMD_GET_FW_VERSION = 0x30,
SG_NFC_CMD_RADIO_OFF = 0x41, SG_NFC_CMD_GET_HW_VERSION = 0x32,
SG_NFC_CMD_POLL = 0x42, SG_NFC_CMD_RADIO_ON = 0x40,
SG_NFC_CMD_MIFARE_SELECT_TAG = 0x43, SG_NFC_CMD_RADIO_OFF = 0x41,
SG_NFC_CMD_MIFARE_SET_KEY_BANA = 0x50, SG_NFC_CMD_POLL = 0x42,
SG_NFC_CMD_BANA_AUTHENTICATE = 0x51, SG_NFC_CMD_MIFARE_SELECT_TAG = 0x43,
SG_NFC_CMD_MIFARE_READ_BLOCK = 0x52, SG_NFC_CMD_MIFARE_SET_KEY_BANA = 0x50,
SG_NFC_CMD_MIFARE_SET_KEY_AIME = 0x54, SG_NFC_CMD_BANA_AUTHENTICATE = 0x51,
SG_NFC_CMD_AIME_AUTHENTICATE = 0x55, SG_NFC_CMD_MIFARE_READ_BLOCK = 0x52,
SG_NFC_CMD_TO_UPDATER_MODE = 0x60, SG_NFC_CMD_MIFARE_SET_KEY_AIME = 0x54,
SG_NFC_CMD_SEND_HEX_DATA = 0x61, SG_NFC_CMD_AIME_AUTHENTICATE = 0x55,
SG_NFC_CMD_RESET = 0x62, SG_NFC_CMD_TO_UPDATER_MODE = 0x60,
SG_NFC_CMD_FELICA_ENCAP = 0x71, SG_NFC_CMD_SEND_HEX_DATA = 0x61,
SG_RGB_CMD_SET_COLOR = 0x81, SG_NFC_CMD_RESET = 0x62,
SG_RGB_CMD_GET_INFO = 0xF0, SG_NFC_CMD_FELICA_ENCAP = 0x71,
SG_RGB_CMD_RESET = 0xF5, SG_RGB_CMD_SET_COLOR = 0x81,
SG_RGB_CMD_GET_INFO = 0xF0,
//FELICA_ENCAP SG_RGB_CMD_RESET = 0xF5,
FELICA_CMD_POLL = 0x00,
FELICA_CMD_NDA_06 = 0x06, //FELICA_ENCAP
FELICA_CMD_NDA_08 = 0x08, FELICA_CMD_POLL = 0x00,
FELICA_CMD_GET_SYSTEM_CODE = 0x0C, FELICA_CMD_NDA_06 = 0x06,
FELICA_CMD_NDA_A4 = 0xA4, FELICA_CMD_NDA_08 = 0x08,
}; FELICA_CMD_GET_SYSTEM_CODE = 0x0C,
FELICA_CMD_NDA_A4 = 0xA4,
typedef union packet_req { };
uint8_t bytes[128];
struct { typedef union packet_req {
uint8_t frame_len; uint8_t bytes[128];
uint8_t addr; struct {
uint8_t seq_no; uint8_t frame_len;
uint8_t cmd; uint8_t addr;
uint8_t payload_len; uint8_t seq_no;
union { uint8_t cmd;
uint8_t key[6]; //sg_nfc_req_mifare_set_key(bana or aime) uint8_t payload_len;
uint8_t color_payload[3];//sg_led_req_set_color union {
struct { //sg_nfc_cmd_mifare_select_tag,sg_nfc_cmd_mifare_authenticate,sg_nfc_cmd_mifare_read_block uint8_t key[6]; //sg_nfc_req_mifare_set_key(bana or aime)
uint8_t uid[4]; uint8_t color_payload[3]; //sg_led_req_set_color
uint8_t block_no; struct { //sg_nfc_cmd_mifare_select_tag,sg_nfc_cmd_mifare_authenticate,sg_nfc_cmd_mifare_read_block
}; uint8_t uid[4];
struct { //sg_nfc_req_felica_encap uint8_t block_no;
uint8_t encap_IDm[8]; };
uint8_t encap_len; struct { //sg_nfc_req_felica_encap
uint8_t encap_code; uint8_t encap_IDm[8];
union { uint8_t encap_len;
struct { uint8_t encap_code;
uint8_t poll_systemCode[2]; union {
uint8_t poll_requestCode; struct {
uint8_t poll_timeout; uint8_t poll_systemCode[2];
}; uint8_t poll_requestCode;
struct { //NDA_06,NDA_08,NDA_A4 uint8_t poll_timeout;
uint8_t RW_IDm[8]; };
uint8_t numService;//and NDA_A4 unknown byte struct { //NDA_06,NDA_08,NDA_A4
uint8_t serviceCodeList[2]; uint8_t RW_IDm[8];
uint8_t numBlock; uint8_t numService; //and NDA_A4 unknown byte
uint8_t blockList[1][2]; uint8_t serviceCodeList[2];
uint8_t blockData[16];//WriteWithoutEncryption,ignore uint8_t numBlock;
}; uint8_t blockList[1][2];
uint8_t felica_payload[1]; uint8_t blockData[16]; //WriteWithoutEncryption,ignore
}; };
}; uint8_t felica_payload[1];
}; };
}; };
} packet_req_t; };
};
typedef union packet_res { } packet_req_t;
uint8_t bytes[128];
struct { typedef union packet_res {
uint8_t frame_len; uint8_t bytes[128];
uint8_t addr; struct {
uint8_t seq_no; uint8_t frame_len;
uint8_t cmd; uint8_t addr;
uint8_t status; uint8_t seq_no;
uint8_t payload_len; uint8_t cmd;
union { uint8_t status;
char version[1]; //sg_nfc_res_get_fw_version,sg_nfc_res_get_hw_version,sg_led_res_get_info uint8_t payload_len;
uint8_t block[16]; //sg_nfc_res_mifare_read_block union {
struct { //sg_nfc_res_poll char version[1]; //sg_nfc_res_get_fw_version,sg_nfc_res_get_hw_version,sg_led_res_get_info
uint8_t count; uint8_t block[16]; //sg_nfc_res_mifare_read_block
uint8_t type; struct { //sg_nfc_res_poll
uint8_t id_len; uint8_t count;
union { uint8_t type;
uint8_t mifare_uid[4]; uint8_t id_len;
struct { union {
uint8_t IDm[8]; uint8_t mifare_uid[4];
uint8_t PMm[8]; struct {
}; uint8_t IDm[8];
}; uint8_t PMm[8];
}; };
struct { //sg_nfc_res_felica_encap };
uint8_t encap_len; };
uint8_t encap_code; struct { //sg_nfc_res_felica_encap
uint8_t encap_IDm[8]; uint8_t encap_len;
union { uint8_t encap_code;
struct {//FELICA_CMD_POLL uint8_t encap_IDm[8];
uint8_t poll_PMm[8]; union {
uint8_t poll_systemCode[2]; struct { //FELICA_CMD_POLL
}; uint8_t poll_PMm[8];
struct { uint8_t poll_systemCode[2];
uint8_t RW_status[2];//猜测,NDA_06,NDA_08 };
uint8_t numBlock;//NDA_06 struct {
uint8_t blockData[1][1][16];//NDA_06 uint8_t RW_status[2]; //猜测,NDA_06,NDA_08
}; uint8_t numBlock; //NDA_06
uint8_t felica_payload[1]; uint8_t blockData[1][1][16]; //NDA_06
}; };
}; uint8_t felica_payload[1];
}; };
}; };
} packet_res_t; };
};
static packet_req_t req; } packet_res_t;
static packet_res_t res;
static packet_req_t req;
static void sg_res_init(uint8_t payload_len = 0) { //初始化模板 static packet_res_t res;
res.frame_len = 6 + payload_len; static uint8_t len, r, checksum;
res.addr = req.addr; static bool escape = false;
res.seq_no = req.seq_no;
res.cmd = req.cmd; static uint8_t packet_read() {
res.status = 0; while (SerialDevice.available()) {
res.payload_len = payload_len; r = SerialDevice.read();
} if (r == 0xE0) {
req.frame_len = 0xFF;
static void sg_nfc_cmd_reset() { //重置读卡器 continue;
FWSW = !digitalRead(SW2); }
nfc.begin(); if (req.frame_len == 0xFF) {
nfc.setPassiveActivationRetries(0x10); //设定等待次数,0xFF永远等待 req.frame_len = r;
nfc.SAMConfig(); len = 0;
if (nfc.getFirmwareVersion()) { checksum = r;
nfc.SAMConfig(); continue;
sg_res_init(); }
res.status = 3; if (r == 0xD0) {
u8g2.drawXBM(0, 0, 16, 16, usb_connect); escape = true;
u8g2.drawXBM(20, 0, 16, 16, blank); continue;
u8g2.drawXBM(111, 0, 16, 16, blank); }
u8g2.sendBuffer(); if (escape) {
return; r++;
} escape = false;
u8g2.drawXBM(20, 0, 16, 16, rf_off); }
u8g2.sendBuffer(); req.bytes[++len] = r;
FastLED.showColor(0xFF0000); if (len == req.frame_len && checksum == r) {
} return req.cmd;
}
static void sg_nfc_cmd_get_fw_version() { checksum += r;
if (FWSW) { }
sg_res_init(sizeof(FW_TN32)); return 0;
memcpy(res.version, FW_TN32, res.payload_len); }
} else {
sg_res_init(sizeof(FW_837)); static void packet_write() {
memcpy(res.version, FW_837, res.payload_len); uint8_t checksum = 0, len = 0;
} if (res.cmd == 0) {
} return;
}
static void sg_nfc_cmd_get_hw_version() { SerialDevice.write(0xE0);
if (FWSW) { while (len <= res.frame_len) {
sg_res_init(sizeof(HW_TN32)); uint8_t w;
memcpy(res.version, HW_TN32, res.payload_len); if (len == res.frame_len) {
} else { w = checksum;
sg_res_init(sizeof(HW_837)); } else {
memcpy(res.version, HW_837, res.payload_len); w = res.bytes[len];
} checksum += w;
} }
if (w == 0xE0 || w == 0xD0) {
static void sg_led_cmd_get_info() { SerialDevice.write(0xD0);
if (FWSW) { SerialDevice.write(--w);
sg_res_init(sizeof(BOARD_TN32)); } else {
memcpy(res.version, BOARD_TN32, res.payload_len); SerialDevice.write(w);
} else { }
sg_res_init(sizeof(BOARD_837)); len++;
memcpy(res.version, BOARD_837, res.payload_len); }
} res.cmd = 0;
} }
static void sg_nfc_cmd_mifare_set_key_aime() { static void sg_res_init(uint8_t payload_len = 0) { //初始化模板
sg_res_init(); res.frame_len = 6 + payload_len;
memcpy(AimeKey, req.key, 6); res.addr = req.addr;
} res.seq_no = req.seq_no;
res.cmd = req.cmd;
static void sg_nfc_cmd_mifare_set_key_bana() { res.status = 0;
sg_res_init(); res.payload_len = payload_len;
memcpy(BanaKey, req.key, 6); }
}
static void sg_nfc_cmd_reset() {
static void sg_led_cmd_reset() { if (nfc.getFirmwareVersion()) {
sg_res_init(); sg_res_init();
} res.status = 3;
u8g2.drawXBM(95, 0, 16, 16, blank);
static void sg_led_cmd_set_color() { u8g2.drawXBM(113, 0, 16, 16, blank);
FastLED.showColor(CRGB(req.color_payload[0], req.color_payload[1], req.color_payload[2])); u8g2.sendBuffer();
} return;
}
static void sg_nfc_cmd_radio_on() { u8g2.drawXBM(95, 0, 16, 16, rf_off);
sg_res_init(); u8g2.sendBuffer();
nfc.setRFField(0x00, 0x01); FastLED.showColor(CRGB::Red);
u8g2.drawXBM(20, 0, 16, 16, rf_open); while (true) {};
u8g2.sendBuffer(); }
}
static void sg_nfc_cmd_get_fw_version() {
static void sg_nfc_cmd_radio_off() { if (FWSW) {
sg_res_init(); sg_res_init(sizeof(FW_TN32));
nfc.setRFField(0x00, 0x00); memcpy(res.version, FW_TN32, res.payload_len);
u8g2.drawXBM(20, 0, 16, 16, blank); } else {
u8g2.sendBuffer(); sg_res_init(sizeof(FW_837));
} memcpy(res.version, FW_837, res.payload_len);
}
static void sg_nfc_cmd_poll() { //卡号发送 }
uint16_t SystemCode;
if (nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, res.mifare_uid, &res.id_len)) { static void sg_nfc_cmd_get_hw_version() {
sg_res_init(0x07); if (FWSW) {
res.count = 1; sg_res_init(sizeof(HW_TN32));
res.type = 0x10; memcpy(res.version, HW_TN32, res.payload_len);
u8g2.drawXBM(111, 0, 16, 16, card); } else {
u8g2.sendBuffer(); sg_res_init(sizeof(HW_837));
return; memcpy(res.version, HW_837, res.payload_len);
} }
else if (nfc.felica_Polling(0xFFFF, 0x00, res.IDm, res.PMm, &SystemCode, 200) == 1) {//< 0: error }
sg_res_init(0x13);
res.count = 1; static void sg_led_cmd_get_info() {
res.type = 0x20; if (FWSW) {
res.id_len = 0x10; sg_res_init(sizeof(BOARD_TN32));
u8g2.drawXBM(111, 0, 16, 16, card); memcpy(res.version, BOARD_TN32, res.payload_len);
u8g2.sendBuffer(); } else {
return; sg_res_init(sizeof(BOARD_837));
} else { memcpy(res.version, BOARD_837, res.payload_len);
sg_res_init(1); }
res.count = 0; }
u8g2.drawXBM(111, 0, 16, 16, blank);
u8g2.sendBuffer(); static void sg_nfc_cmd_mifare_set_key_aime() {
} sg_res_init();
} memcpy(AimeKey, req.key, 6);
}
static void sg_nfc_cmd_mifare_select_tag() {
sg_res_init(); static void sg_nfc_cmd_mifare_set_key_bana() {
} sg_res_init();
memcpy(BanaKey, req.key, 6);
static void sg_nfc_cmd_aime_authenticate() { }
sg_res_init();
if (nfc.mifareclassic_AuthenticateBlock(req.uid, 4, req.block_no, 1, AimeKey)) { static void sg_led_cmd_reset() {
return; sg_res_init();
} else { }
res.status = 1;
} static void sg_led_cmd_set_color() {
} FastLED.showColor(CRGB(req.color_payload[0], req.color_payload[1], req.color_payload[2]));
}
static void sg_nfc_cmd_bana_authenticate() {
sg_res_init(); static void sg_nfc_cmd_radio_on() {
if (nfc.mifareclassic_AuthenticateBlock(req.uid, 4, req.block_no, 0, BanaKey)) { sg_res_init();
return; nfc.setRFField(0x00, 0x01);
} else { u8g2.drawXBM(95, 0, 16, 16, rf_open);
res.status = 1; u8g2.sendBuffer();
} }
}
static void sg_nfc_cmd_radio_off() {
static void sg_nfc_cmd_mifare_read_block() {//读取卡扇区数据 sg_res_init();
if (nfc.mifareclassic_ReadDataBlock(req.block_no, res.block)) { nfc.setRFField(0x00, 0x00);
sg_res_init(0x10); u8g2.drawXBM(95, 0, 16, 16, blank);
return; u8g2.sendBuffer();
} }
sg_res_init();
res.status = 1; static void sg_nfc_cmd_poll() { //卡号发送
} uint16_t SystemCode;
if (nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, res.mifare_uid, &res.id_len)) {
static void sg_nfc_cmd_felica_encap() { sg_res_init(0x07);
uint16_t SystemCode; res.count = 1;
if (nfc.felica_Polling(0xFFFF, 0x01, res.encap_IDm, res.poll_PMm, &SystemCode, 200) == 1) { res.type = 0x10;
SystemCode = SystemCode >> 8 | SystemCode << 8;//SystemCode大小端反转注意 } else if (nfc.felica_Polling(0xFFFF, 0x00, res.IDm, res.PMm, &SystemCode, 200) == 1) { //< 0: error
} sg_res_init(0x13);
else { res.count = 1;
sg_res_init(); res.type = 0x20;
res.status = 1; res.id_len = 0x10;
return; } else {
} sg_res_init(1);
uint8_t code = req.encap_code; res.count = 0;
res.encap_code = code + 1; u8g2.drawXBM(113, 0, 16, 16, blank);
switch (code) { u8g2.sendBuffer();
case FELICA_CMD_POLL: return;
{ }
sg_res_init(0x14); u8g2.drawXBM(113, 0, 16, 16, card);
res.poll_systemCode[0] = SystemCode; u8g2.sendBuffer();
res.poll_systemCode[1] = SystemCode >> 8; }
}
break; static void sg_nfc_cmd_aime_authenticate() {
case FELICA_CMD_GET_SYSTEM_CODE: sg_res_init();
{ if (nfc.mifareclassic_AuthenticateBlock(req.uid, 4, req.block_no, 1, AimeKey)) {
sg_res_init(0x0D); return;
res.felica_payload[0] = 0x01;//未知 } else {
res.felica_payload[1] = SystemCode;//SystemCode res.status = 1;
res.felica_payload[2] = SystemCode >> 8; }
} }
break;
case FELICA_CMD_NDA_A4: static void sg_nfc_cmd_bana_authenticate() {
{ sg_res_init();
sg_res_init(0x0B); if (nfc.mifareclassic_AuthenticateBlock(req.uid, 4, req.block_no, 0, BanaKey)) {
res.felica_payload[0] = 0x00; return;
} } else {
break; res.status = 1;
case FELICA_CMD_NDA_06: }
{ }
uint16_t serviceCodeList[1] = {(uint16_t)(req.serviceCodeList[1] << 8 | req.serviceCodeList[0])};//大小端反转注意
for (uint8_t i = 0; i < req.numBlock; i++) { static void sg_nfc_cmd_mifare_read_block() { //读取卡扇区数据
uint16_t blockList[1] = {(uint16_t)(req.blockList[i][0] << 8 | req.blockList[i][1])}; if (nfc.mifareclassic_ReadDataBlock(req.block_no, res.block)) {
if (nfc.felica_ReadWithoutEncryption(1, serviceCodeList, 1, blockList, res.blockData[i]) != 1) { sg_res_init(0x10);
memset(res.blockData[i], 0, 16);//dummy data return;
} }
} sg_res_init();
res.RW_status[0] = 0; res.status = 1;
res.RW_status[1] = 0; }
res.numBlock = req.numBlock;
sg_res_init(0x0D + req.numBlock * 16); static void sg_nfc_cmd_felica_encap() {
} uint16_t SystemCode;
break; if (nfc.felica_Polling(0xFFFF, 0x01, res.encap_IDm, res.poll_PMm, &SystemCode, 200) == 1) {
case FELICA_CMD_NDA_08: SystemCode = SystemCode >> 8 | SystemCode << 8; //SystemCode大小端反转注意
{ } else {
sg_res_init(0x0C);//此处应有写入卡,但是不打算实现 sg_res_init();
res.RW_status[0] = 0; res.status = 1;
res.RW_status[1] = 0; return;
} }
break; uint8_t code = req.encap_code;
default: res.encap_code = code + 1;
sg_res_init(); switch (code) {
res.status = 1; case FELICA_CMD_POLL:
} {
res.encap_len = res.payload_len; sg_res_init(0x14);
} res.poll_systemCode[0] = SystemCode;
res.poll_systemCode[1] = SystemCode >> 8;
}
break;
case FELICA_CMD_GET_SYSTEM_CODE:
{
sg_res_init(0x0D);
res.felica_payload[0] = 0x01; //未知
res.felica_payload[1] = SystemCode; //SystemCode
res.felica_payload[2] = SystemCode >> 8;
}
break;
case FELICA_CMD_NDA_A4:
{
sg_res_init(0x0B);
res.felica_payload[0] = 0x00;
}
break;
case FELICA_CMD_NDA_06:
{
uint16_t serviceCodeList[1] = { (uint16_t)(req.serviceCodeList[1] << 8 | req.serviceCodeList[0]) }; //大小端反转注意
for (uint8_t i = 0; i < req.numBlock; i++) {
uint16_t blockList[1] = { (uint16_t)(req.blockList[i][0] << 8 | req.blockList[i][1]) };
if (nfc.felica_ReadWithoutEncryption(1, serviceCodeList, 1, blockList, res.blockData[i]) != 1) {
memset(res.blockData[i], 0, 16); //dummy data
}
}
res.RW_status[0] = 0;
res.RW_status[1] = 0;
res.numBlock = req.numBlock;
sg_res_init(0x0D + req.numBlock * 16);
}
break;
case FELICA_CMD_NDA_08:
{
sg_res_init(0x0C); //此处应有写入卡,但是不打算实现
res.RW_status[0] = 0;
res.RW_status[1] = 0;
}
break;
default:
sg_res_init();
res.status = 1;
}
res.encap_len = res.payload_len;
}