こちらのプログラムを改造して、RFID の sector を表示してみました。
M5Stack Core2: RFID-RC522 の使い方
次のファイルには変更はありません。
MFRC522_I2C.cpp
フォルダー構造
$ tree RFID_sector/
RFID_sector/
├── MFRC522_I2C.cpp
├── MFRC522_I2C.h
├── RFID_sector.ino
└── block01.cpp
RFID_sector.ino
// ---------------------------------------------------------------
// RFID_sector.ino
//
// Jun/12/2022
// ---------------------------------------------------------------
#include <M5Core2.h>
#include "MFRC522_I2C.h"
#include <Wire.h>
MFRC522 mfrc522(0x28); // Create MFRC522 instance.
int nocard = 0;
// ---------------------------------------------------------------
void setup() {
M5.begin();
M5.lcd.setTextSize(2);
M5.Lcd.println("MFRC522 Sector Jun/12/2022");
Wire.begin();
mfrc522.PCD_Init();
M5.Lcd.println("Please put the card\n\nUID:");
}
// ---------------------------------------------------------------
void loop() {
M5.Lcd.setCursor(50,47);
if (!mfrc522.PICC_IsNewCardPresent() || ! mfrc522.PICC_ReadCardSerial()) {
nocard++;
if (2 < nocard)
{
M5.Lcd.println(" No Card ");
// Serial.println("*** No Card ***");
}
}
else
{
nocard = 0;
uid_display_proc();
}
delay(200);
}
// ---------------------------------------------------------------
void uid_display_proc()
{
byte buffer[72];
short aa[] = {1,0,0,0,0,0,0,0,0,0};
char buf[60];
sprintf(buf,"mfrc522.uid.size = %d",mfrc522.uid.size);
Serial.println(buf);
for (short it = 0; it < mfrc522.uid.size; it++) {
aa[it] = mfrc522.uid.uidByte[it];
}
sprintf(buf, "%02x %02x %02x %02x",aa[0],aa[1],aa[2],aa[3]);
M5.Lcd.println(buf);
M5.Lcd.println("");
Serial.println(buf);
byte piccType = mfrc522.PICC_GetType((&mfrc522.uid)->sak);
Serial.print(F("PICC type: "));
Serial.println(mfrc522.PICC_GetTypeName(piccType));
byte sector = 1;
mfrc522.PICC_Dump_1K(&mfrc522.uid,sector,buffer);
for (short it=0; it<4; it++)
{
serial_dump_block_proc(&buffer[it*18]);
}
for (short it=0; it<4; it++)
{
short jt = it * 18;
sprintf(buf, "%02x %02x %02x %02x ",buffer[jt],buffer[jt+1],buffer[jt+2],buffer[jt+3]);
M5.Lcd.print(buf);
sprintf(buf, "%02x %02x %02x %02x ",buffer[jt+4],buffer[jt+5],buffer[jt+6],buffer[jt+7]);
M5.Lcd.print(buf);
M5.Lcd.println("");
}
}
// ---------------------------------------------------------------
void serial_dump_block_proc(byte buffer[])
{
for (byte index = 0; index < 16; index++) {
if(buffer[index] < 0x10)
Serial.print(F(" 0"));
else
Serial.print(F(" "));
Serial.print(buffer[index], HEX);
if ((index % 4) == 3) {
Serial.print(F(" "));
}
}
Serial.println();
}
// ---------------------------------------------------------------
block01.cpp
// ---------------------------------------------------------------
// block01.cpp
//
// Jun/12/2022
// ---------------------------------------------------------------
#include <Arduino.h>
#include "MFRC522_I2C.h"
#include <Wire.h>
// ---------------------------------------------------------------
void MFRC522::PICC_Dump_1K(Uid *uid,byte sector,byte buffer[])
{
// byte buffer[72];
MIFARE_Key key;
byte piccType = PICC_GetType(uid->sak);
if (piccType == PICC_TYPE_MIFARE_1K)
{
for (byte it = 0; it < 6; it++) {
key.keyByte[it] = 0xFF;
}
}
PICC_DumpMifareClassicSector_1K(uid,&key,sector,buffer);
PICC_HaltA(); // Halt the PICC before stopping the encrypted session.
PCD_StopCrypto1();
Serial.println();
}
// ---------------------------------------------------------------
void MFRC522::PICC_DumpMifareClassicSector_1K
(Uid *uid,MIFARE_Key *key,byte sector,byte buffer[])
{
byte firstBlock;
byte no_of_blocks;
if (sector < 40)
{
no_of_blocks = 16;
firstBlock = 128 + (sector - 32) * no_of_blocks;
if (sector < 32)
{
no_of_blocks = 4;
firstBlock = sector * no_of_blocks;
}
MFRC522::dump_block_proc(uid,key,sector,no_of_blocks,firstBlock,buffer);
/*
for (short it=0; it<4; it++)
{
serial_dump_block_proc(&buffer[it*18]);
}
*/
}
}
// ---------------------------------------------------------------
void MFRC522::dump_block_proc(Uid *uid,MIFARE_Key *key,byte sector, byte no_of_blocks,byte firstBlock,byte buffer[])
{
byte status;
byte blockAddr;
int it = 0;
for (int8_t blockOffset = no_of_blocks - 1; blockOffset >= 0; blockOffset--)
{
blockAddr = firstBlock + blockOffset;
// Serial.printf("blockAddr = %x\r\n",blockAddr);
// Serial.printf("firstBlock = %x\r\n",firstBlock);
status = PCD_Authenticate(PICC_CMD_MF_AUTH_KEY_A, firstBlock, key, uid);
if (status != STATUS_OK) {
Serial.print(F("PCD_Authenticate() failed: "));
Serial.println(GetStatusCodeName(status));
return;
}
dump_read_block_proc(blockAddr,&buffer[it*18]);
it++;
}
}
// ---------------------------------------------------------------
void MFRC522::dump_read_block_proc(byte blockAddr,byte buffer_out[])
{
byte buffer[18];
byte byteCount = sizeof(buffer);
// Serial.printf("byteCount = %d\r\n", byteCount);
byte status = MIFARE_Read(blockAddr, buffer, &byteCount);
if (status != STATUS_OK) {
Serial.print(F("MIFARE_Read() failed: "));
Serial.println(GetStatusCodeName(status));
}
for (short it=0; it<18; it++)
{
buffer_out[it] = buffer[it];
}
}
// ---------------------------------------------------------------
Support functions に追加
MFRC522_I2C.h
(省略)
void PICC_Dump_1K(Uid *uid, byte sector,byte buffer[]);
void PICC_DumpMifareClassicSector_1K(Uid *uid, MIFARE_Key *key, byte sector,byte buffer[]);
void dump_block_proc(Uid *uid,MIFARE_Key *key,byte sector, byte no_of_blocks,byte firstBlock,byte buffer[]);
void dump_read_block_proc(byte blockAddr,byte buffer[]);
(省略)
