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

同步更改,更新说明

This commit is contained in:
Sucareto 2024-04-18 20:48:28 +08:00
parent d4c136377a
commit fd7ed887ac
4 changed files with 65 additions and 2036 deletions

View File

@ -48,7 +48,7 @@ PN532_SPI pn532(SPI, PN532_SPI_SS);
#include "PN532.h" #include "PN532.h"
PN532 nfc(pn532); PN532 nfc(pn532);
uint8_t KeyA[6], KeyB[6]; uint8_t KeyA[6], KeyB[6];
uint8_t DefaultKey[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}; uint8_t DefaultKey[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
#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);
@ -98,23 +98,18 @@ enum {
CMD_MIFARE_KEY_SET_A = 0x50, CMD_MIFARE_KEY_SET_A = 0x50,
CMD_MIFARE_AUTHORIZE_A = 0x51, CMD_MIFARE_AUTHORIZE_A = 0x51,
CMD_MIFARE_READ = 0x52, CMD_MIFARE_READ = 0x52,
CMD_MIFARE_WRITE = 0x53, // CMD_MIFARE_WRITE = 0x53,
CMD_MIFARE_KEY_SET_B = 0x54, CMD_MIFARE_KEY_SET_B = 0x54,
CMD_MIFARE_AUTHORIZE_B = 0x55, CMD_MIFARE_AUTHORIZE_B = 0x55,
// Boot,update // Boot,update
CMD_TO_UPDATER_MODE = 0x60, CMD_TO_UPDATER_MODE = 0x60,
CMD_SEND_HEX_DATA = 0x61, CMD_SEND_HEX_DATA = 0x61,
CMD_TO_NORMAL_MODE = 0x62, CMD_TO_NORMAL_MODE = 0x62,
CMD_SEND_BINDATA_INIT = 0x63, // CMD_SEND_BINDATA_INIT = 0x63,
CMD_SEND_BINDATA_EXEC = 0x64, // CMD_SEND_BINDATA_EXEC = 0x64,
// FeliCa // FeliCa
CMD_FELICA_PUSH = 0x70, // CMD_FELICA_PUSH = 0x70,
CMD_FELICA_THROUGH = 0x71, CMD_FELICA_THROUGH = 0x71,
CMD_FELICA_THROUGH_POLL = 0x00,
CMD_FELICA_THROUGH_READ = 0x06,
CMD_FELICA_THROUGH_WRITE = 0x08,
CMD_FELICA_THROUGH_GET_SYSTEM_CODE = 0x0C,
CMD_FELICA_THROUGH_NDA_A4 = 0xA4,
// LED board // LED board
CMD_EXT_BOARD_LED = 0x80, CMD_EXT_BOARD_LED = 0x80,
CMD_EXT_BOARD_LED_RGB = 0x81, CMD_EXT_BOARD_LED_RGB = 0x81,
@ -126,14 +121,27 @@ enum {
CMD_EXT_TO_NORMAL_MODE = 0xf5, CMD_EXT_TO_NORMAL_MODE = 0xf5,
}; };
enum { // 未确认效果 enum {
ERROR_NONE = 0, FelicaPolling = 0x00,
ERROR_NFCRW_INIT_ERROR = 1, FelicaReqResponce = 0x04,
ERROR_NFCRW_FIRMWARE_UP_TO_DATE = 3, FelicaReadWithoutEncryptData = 0x06,
ERROR_NFCRW_ACCESS_ERROR = 4, FelicaWriteWithoutEncryptData = 0x08,
ERROR_CARD_DETECT_TIMEOUT = 5, FelicaReqSysCode = 0x0C,
ERROR_CARD_DETECT_ERROR = 32, FelicaActive2 = 0xA4,
ERROR_FELICA_ERROR = 33, };
enum {
STATUS_OK = 0x00,
STATUS_CARD_ERROR = 0x01,
STATUS_NOT_ACCEPT = 0x02,
STATUS_INVALID_COMMAND = 0x03,
STATUS_INVALID_DATA = 0x04,
STATUS_SUM_ERROR = 0x05,
STATUS_INTERNAL_ERROR = 0x06,
STATUS_INVALID_FIRM_DATA = 0x07,
STATUS_FIRM_UPDATE_SUCCESS = 0x08,
STATUS_COMP_DUMMY_2ND = 0x10,
STATUS_COMP_DUMMY_3RD = 0x20,
}; };
typedef union { typedef union {
@ -246,8 +254,8 @@ uint8_t packet_read() {
escape = false; escape = false;
} }
req.bytes[++len] = r; req.bytes[++len] = r;
if (len == req.frame_len && checksum == r) { if (len == req.frame_len) {
return req.cmd; return checksum == r ? req.cmd : STATUS_SUM_ERROR;
} }
checksum += r; checksum += r;
} }
@ -284,18 +292,18 @@ void res_init(uint8_t payload_len = 0) {
res.addr = req.addr; res.addr = req.addr;
res.seq_no = req.seq_no; res.seq_no = req.seq_no;
res.cmd = req.cmd; res.cmd = req.cmd;
res.status = ERROR_NONE; res.status = STATUS_OK;
res.payload_len = payload_len; res.payload_len = payload_len;
} }
void sys_to_normal_mode() { void sys_to_normal_mode() {
res_init(); res_init();
if (nfc.getFirmwareVersion()) { if (nfc.getFirmwareVersion()) {
res.status = ERROR_NFCRW_FIRMWARE_UP_TO_DATE; res.status = STATUS_INVALID_COMMAND;
u8g2.drawXBM(95, 0, 16, 16, blank); u8g2.drawXBM(95, 0, 16, 16, blank);
u8g2.drawXBM(113, 0, 16, 16, blank); u8g2.drawXBM(113, 0, 16, 16, blank);
} else { } else {
res.status = ERROR_NFCRW_INIT_ERROR; res.status = STATUS_INTERNAL_ERROR;
u8g2.drawXBM(95, 0, 16, 16, rf_off); u8g2.drawXBM(95, 0, 16, 16, rf_off);
FastLED.showColor(0xFF0000); FastLED.showColor(0xFF0000);
} }
@ -316,7 +324,7 @@ void sys_get_hw_version() {
res_init(sizeof(old_hw_version) - 1); res_init(sizeof(old_hw_version) - 1);
memcpy(res.version, old_hw_version, res.payload_len); memcpy(res.version, old_hw_version, res.payload_len);
} else { } else {
res_init(sizeof(new_hw_version)); res_init(sizeof(new_hw_version) - 1);
memcpy(res.version, new_hw_version, res.payload_len); memcpy(res.version, new_hw_version, res.payload_len);
} }
} }
@ -331,7 +339,6 @@ void sys_get_led_info() {
} }
} }
void nfc_start_polling() { void nfc_start_polling() {
res_init(); res_init();
nfc.setRFField(0x00, 0x01); nfc.setRFField(0x00, 0x01);
@ -365,7 +372,6 @@ void nfc_card_detect() {
} else { } else {
res_init(1); res_init(1);
res.count = 0; res.count = 0;
res.status = ERROR_NONE;
u8g2.drawXBM(113, 0, 16, 16, blank); u8g2.drawXBM(113, 0, 16, 16, blank);
return; return;
} }
@ -375,14 +381,14 @@ void nfc_card_detect() {
void nfc_mifare_authorize_a() { void nfc_mifare_authorize_a() {
res_init(); res_init();
if (!nfc.mifareclassic_AuthenticateBlock(req.uid, 4, req.block_no, 0, KeyA)) { if (!nfc.mifareclassic_AuthenticateBlock(req.uid, 4, req.block_no, 0, KeyA)) {
res.status = ERROR_NFCRW_ACCESS_ERROR; res.status = STATUS_CARD_ERROR;
} }
} }
void nfc_mifare_authorize_b() { void nfc_mifare_authorize_b() {
res_init(); res_init();
if (!nfc.mifareclassic_AuthenticateBlock(req.uid, 4, req.block_no, 1, KeyB)) { if (!nfc.mifareclassic_AuthenticateBlock(req.uid, 4, req.block_no, 1, KeyB)) {
res.status = ERROR_NFCRW_ACCESS_ERROR; res.status = STATUS_CARD_ERROR;
} }
} }
@ -394,7 +400,7 @@ void nfc_mifare_read() {
return; return;
} else if (!nfc.mifareclassic_ReadDataBlock(req.block_no, res.block)) { } else if (!nfc.mifareclassic_ReadDataBlock(req.block_no, res.block)) {
res_init(); res_init();
res.status = ERROR_CARD_DETECT_TIMEOUT; // TODO res.status = STATUS_CARD_ERROR;
} }
} }
@ -404,20 +410,20 @@ void nfc_felica_through() {
SystemCode = SystemCode >> 8 | SystemCode << 8; SystemCode = SystemCode >> 8 | SystemCode << 8;
} else { } else {
res_init(); res_init();
res.status = ERROR_FELICA_ERROR; res.status = STATUS_CARD_ERROR;
return; return;
} }
uint8_t code = req.encap_code; uint8_t code = req.encap_code;
res.encap_code = code + 1; res.encap_code = code + 1;
switch (code) { switch (code) {
case CMD_FELICA_THROUGH_POLL: case FelicaPolling:
{ {
res_init(0x14); res_init(0x14);
res.poll_systemCode[0] = SystemCode; res.poll_systemCode[0] = SystemCode;
res.poll_systemCode[1] = SystemCode >> 8; res.poll_systemCode[1] = SystemCode >> 8;
} }
break; break;
case CMD_FELICA_THROUGH_GET_SYSTEM_CODE: case FelicaReqSysCode:
{ {
res_init(0x0D); res_init(0x0D);
res.felica_payload[0] = 0x01; res.felica_payload[0] = 0x01;
@ -425,13 +431,13 @@ void nfc_felica_through() {
res.felica_payload[2] = SystemCode >> 8; res.felica_payload[2] = SystemCode >> 8;
} }
break; break;
case CMD_FELICA_THROUGH_NDA_A4: case FelicaActive2:
{ {
res_init(0x0B); res_init(0x0B);
res.felica_payload[0] = 0x00; res.felica_payload[0] = 0x00;
} }
break; break;
case CMD_FELICA_THROUGH_READ: case FelicaReadWithoutEncryptData:
{ {
uint16_t serviceCodeList[1] = { (uint16_t)(req.serviceCodeList[1] << 8 | req.serviceCodeList[0]) }; uint16_t serviceCodeList[1] = { (uint16_t)(req.serviceCodeList[1] << 8 | req.serviceCodeList[0]) };
for (uint8_t i = 0; i < req.numBlock; i++) { for (uint8_t i = 0; i < req.numBlock; i++) {
@ -446,7 +452,7 @@ void nfc_felica_through() {
res_init(0x0D + req.numBlock * 16); res_init(0x0D + req.numBlock * 16);
} }
break; break;
case CMD_FELICA_THROUGH_WRITE: case FelicaWriteWithoutEncryptData:
{ {
res_init(0x0C); // WriteWithoutEncryption,ignore res_init(0x0C); // WriteWithoutEncryption,ignore
res.RW_status[0] = 0; res.RW_status[0] = 0;
@ -455,7 +461,7 @@ void nfc_felica_through() {
break; break;
default: default:
res_init(); res_init();
res.status = ERROR_FELICA_ERROR; res.status = STATUS_INVALID_COMMAND;
} }
res.encap_len = res.payload_len; res.encap_len = res.payload_len;
} }

View File

@ -65,7 +65,7 @@ void setup() {
SerialDevice.begin(FWSW ? 38400 : 115200); SerialDevice.begin(FWSW ? 38400 : 115200);
u8g2.drawStr(0, 16, "Aime Reader"); u8g2.drawStr(0, 16, "Aime Reader");
u8g2.drawStr(105, 64, FWSW ? "LOW" : "HIGH"); u8g2.drawStr(105, 64, FWSW ? "LOW" : "HIGH");
u8g2.drawStr(0, 64, FWSW ? "TN32MSEC003S" : "837-15396"); u8g2.drawStr(0, 64, FWSW ? "TN32MSEC003S" : new_hw_version);
FastLED.showColor(FWSW ? CRGB::Green : CRGB::Blue); FastLED.showColor(FWSW ? CRGB::Green : CRGB::Blue);
ReaderMain = AimeCardReader; ReaderMain = AimeCardReader;
} }
@ -181,9 +181,9 @@ void AimeCardReader() { // Aime mode
nfc_mifare_read(); nfc_mifare_read();
break; break;
// FeliCa // FeliCa
case CMD_FELICA_THROUGH: // case CMD_FELICA_THROUGH:
nfc_felica_through(); // nfc_felica_through();
break; // break;
// LED // LED
case CMD_EXT_BOARD_LED_RGB: case CMD_EXT_BOARD_LED_RGB:
FastLED.showColor(CRGB(req.color_payload[0], req.color_payload[1], req.color_payload[2])); FastLED.showColor(CRGB(req.color_payload[0], req.color_payload[1], req.color_payload[2]));
@ -193,8 +193,22 @@ void AimeCardReader() { // Aime mode
break; break;
case CMD_EXT_BOARD_LED_RGB_UNKNOWN: case CMD_EXT_BOARD_LED_RGB_UNKNOWN:
break; break;
case CMD_CARD_SELECT:
case CMD_CARD_HALT:
case CMD_EXT_TO_NORMAL_MODE:
case CMD_TO_UPDATER_MODE:
case CMD_SEND_HEX_DATA:
res_init();
break;
case STATUS_SUM_ERROR:
res_init();
res.status = STATUS_SUM_ERROR;
break;
default: default:
res_init(); res_init();
res.status = STATUS_INVALID_COMMAND;
} }
u8g2.sendBuffer(); u8g2.sendBuffer();
ConnectTime = millis(); ConnectTime = millis();

File diff suppressed because one or more lines are too long

View File

@ -27,6 +27,7 @@ https://user-images.githubusercontent.com/28331534/170975661-137f3474-f61a-4a4d-
### PCB 设计: ### PCB 设计:
- 该 PCB 方案仅用于我自己快速测试,并非最佳实现方案 - 该 PCB 方案仅用于我自己快速测试,并非最佳实现方案
- 如果需要使用此 PCB 方案,请务必把 logo 删掉,然后根据需求认真审查 PCB 设计并作出修改后再进行制作 - 如果需要使用此 PCB 方案,请务必把 logo 删掉,然后根据需求认真审查 PCB 设计并作出修改后再进行制作
- 建议使用 PN532 HSU 模式,可以去掉 TTL 拨码开关,需要直通 PN532 时,在代码里实现 Serial 数据转发
### 拨码开关: ### 拨码开关:
@ -67,15 +68,13 @@ https://user-images.githubusercontent.com/28331534/170975661-137f3474-f61a-4a4d-
[index.php](OTA/index.php) 默认是判断文件 MD5 是否一致,不一致就会发送更新。 [index.php](OTA/index.php) 默认是判断文件 MD5 是否一致,不一致就会发送更新。
## 感谢 ## 参考
- 原项目:[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) - Spice 通信参考:[PN5180-cardio SpiceAPI branch](https://github.com/CrazyRedMachine/PN5180-cardio/tree/SpiceAPI)
- Spice 通信(已复制到本仓库并根据需要修改了部分代码):[SpiceAPI](https://github.com/spicetools/spicetools/tree/master/api/resources/arduino)
- 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)