Compare commits

..

2 Commits

Author SHA1 Message Date
74a31f1b40 调试对KEY A和KEY B的修改 2025-04-25 17:56:01 +08:00
9e8bcbe608 原生库代码,支持对IC卡的读取 2025-04-25 13:14:21 +08:00
5 changed files with 283 additions and 4 deletions

View File

@ -1,4 +1,14 @@
dependencies:
abobija/rc522:
component_hash: 45869b7a266329f19dbc84a878c97acdd20535ff7cdc4ed9c8cdbff7f8420d70
dependencies:
- name: idf
require: private
version: ^5.0
source:
registry_url: https://components.espressif.com/
type: service
version: 3.3.1
espressif/button:
component_hash: ea6e01641b8dcf196ecc48be2961b1f20af397b61a752ab041a19282e41bafcb
dependencies:
@ -44,7 +54,7 @@ dependencies:
type: service
version: 1.7.2
espressif/tinyusb:
component_hash: 0f96faa5e29d5a04553ba7050b9051c1f6b549b9340681eb7afdda419a8dff2e
component_hash: 10703da2c3cd39a944711ee3d320c6dda54debabf13b4b933a7c90daf102372b
dependencies:
- name: idf
require: private
@ -56,7 +66,7 @@ dependencies:
- esp32s2
- esp32s3
- esp32p4
version: 0.18.0~1
version: 0.18.0~2
idf:
source:
type: idf
@ -69,11 +79,12 @@ dependencies:
type: service
version: 8.4.0
direct_dependencies:
- abobija/rc522
- espressif/button
- espressif/esp_lvgl_port
- espressif/esp_tinyusb
- idf
- lvgl/lvgl
manifest_hash: 0776e45d0542df1d6474383d6a345709a73282d68dc13bdb93bd6007fcae3e92
manifest_hash: e6ca60012c0c9387fb28df631decc4f926df80be4182266e68f1cf3aec3385cd
target: esp32s2
version: 2.0.0

View File

@ -10,12 +10,15 @@ file(GLOB PORT_GUI "GUI/lvgl_task.c" "GUI/custom/*.c" "GUI/generated/*.c" "GUI/g
file(GLOB PORT_USB "usb_serial_task.c" )
endif()
file(GLOB RFID_SRCS "rfid_rc522/memory_dump.c")
idf_component_register(SRCS "main.c" "Receiver.c" "flasher_tool.c"
${PORT_SRCS}
${FILE_SYS_SRCS}
${PORT_USB}
${PORT_GUI}
${BSP_SRCS}
${RFID_SRCS}
INCLUDE_DIRS "GUI/custom/"
INCLUDE_DIRS "GUI/generated/"
INCLUDE_DIRS "GUI/generated/guider_fonts/"

View File

@ -1,5 +1,6 @@
## IDF Component Manager Manifest File
dependencies:
abobija/rc522: "^3.3.1"
lvgl/lvgl: "^8.3.0"
## Required IDF version
idf:

View File

@ -129,6 +129,7 @@ EventGroupHandle_t System_eventGroup;
Sys_config_t System_Config;
extern void Axp202_task(void *pvParameter);
extern void rfid_rc522_task(void *pvParameter);
extern QueueHandle_t gpio_evt_queue;
@ -168,6 +169,7 @@ void app_main(void)
xTaskCreate(&usb_serial_task,"usb_serial_task",1024*10,NULL,22,NULL);
xTaskCreate(&Flasher_tool_task, "Flasher_tool_task", 1024*20, NULL, 10, NULL);
xTaskCreate(&Axp202_task,"Axp202_task",1024*5,NULL,8,NULL);
xTaskCreate(&rfid_rc522_task,"rfid_rc522_task",1024*5,NULL,15,NULL);
#if 0
home_html.html_data = load_html2Ram(home_html);
wifi_config_html.html_data = load_html2Ram(wifi_config_html);
@ -227,7 +229,7 @@ void app_main(void)
bits = xEventGroupWaitBits(System_eventGroup, EVENT_GET_BATTPERCENTAGE ,pdFALSE,pdFALSE,portMAX_DELAY);
if(bits&EVENT_GET_BATTPERCENTAGE)
{
ESP_LOGI(TAG,"EVENT_GET_BATTPERCENTAGE");
// ESP_LOGI(TAG,"EVENT_GET_BATTPERCENTAGE");
lv_event_send(guider_ui.screen_label_1, LV_EVENT_CUSTOM_UPDATE, (void *)getBattPercentage());
xEventGroupClearBits(System_eventGroup,EVENT_GET_BATTPERCENTAGE);
}

View File

@ -0,0 +1,262 @@
#include <esp_log.h>
#include <esp_check.h>
#include <string.h>
#include "rc522.h"
#include "driver/rc522_spi.h"
#include "picc/rc522_mifare.h"
static const char *TAG = "rc522-memory-dump-example";
#define RC522_SPI_BUS_GPIO_MISO (7)
#define RC522_SPI_BUS_GPIO_MOSI (6)
#define RC522_SPI_BUS_GPIO_SCLK (4)
#define RC522_SPI_SCANNER_GPIO_SDA (2)
#define RC522_SCANNER_GPIO_RST (12) // soft-reset
static rc522_spi_config_t driver_config = {
.host_id = SPI3_HOST,
.bus_config = &(spi_bus_config_t){
.miso_io_num = RC522_SPI_BUS_GPIO_MISO,
.mosi_io_num = RC522_SPI_BUS_GPIO_MOSI,
.sclk_io_num = RC522_SPI_BUS_GPIO_SCLK,
},
.dev_config = {
.spics_io_num = RC522_SPI_SCANNER_GPIO_SDA,
},
.rst_io_num = RC522_SCANNER_GPIO_RST,
};
static rc522_driver_handle_t driver;
static rc522_handle_t scanner;
#define DUMP(format, ...) esp_log_write(ESP_LOG_INFO, TAG, format, ##__VA_ARGS__)
static void dump_header()
{
DUMP("Sector Block Bytes AccessBits\n");
DUMP(" 0 1 2 3 4 5 6 7 8 9 11 12 15 c1 c2 c3\n");
}
static void dump_block(rc522_mifare_sector_block_t *block, uint8_t sector_index)
{
if (block->type == RC522_MIFARE_BLOCK_TRAILER) {
DUMP("%*d", 6, sector_index);
}
else {
DUMP("%*s", 6, "");
}
// Block address
DUMP(" %*d", 5, block->address);
// Data
DUMP(" ");
for (uint8_t i = 0; i < 16; i++) {
DUMP("%02" RC522_X "", block->bytes[i]);
if ((i % 4) == 3) {
DUMP(" ");
}
}
// Access bits
DUMP(" %d %d %d", block->access_bits.c1, block->access_bits.c2, block->access_bits.c3);
// String representation (if it's data block)
if (block->type == RC522_MIFARE_BLOCK_DATA) {
DUMP(" |");
for (uint8_t i = 0; i < 16; i++) {
if (block->bytes[i] >= 32 && block->bytes[i] <= 126) { // standard ascii codes
DUMP(LOG_COLOR(LOG_COLOR_GREEN) "%c" LOG_RESET_COLOR, block->bytes[i]);
}
else {
DUMP("%c", '.');
}
}
DUMP("|");
}
// Value (if it's value block)
else if (block->type == RC522_MIFARE_BLOCK_VALUE) {
DUMP(" (val=%ld (0x%04l" RC522_X "), adr=0x%02" RC522_X ")",
block->value_info.value,
block->value_info.value,
block->value_info.addr);
}
// Errors and warnings
if (block->type == RC522_MIFARE_BLOCK_TRAILER && block->error == RC522_ERR_MIFARE_ACCESS_BITS_INTEGRITY_VIOLATION) {
DUMP(" ABITS_ERR");
}
if (block->type == RC522_MIFARE_BLOCK_VALUE && block->error == RC522_ERR_MIFARE_VALUE_BLOCK_INTEGRITY_VIOLATION) {
DUMP(" VAL_ERR");
}
// Termination
DUMP("\n");
}
static void dump_block_buffer(uint8_t buffer[RC522_MIFARE_BLOCK_SIZE])
{
for (uint8_t i = 0; i < RC522_MIFARE_BLOCK_SIZE; i++) {
esp_log_write(ESP_LOG_INFO, TAG, "%02" RC522_X " ", buffer[i]);
}
esp_log_write(ESP_LOG_INFO, TAG, "\n");
}
static esp_err_t dump_memory(rc522_handle_t scanner, rc522_picc_t *picc)
{
rc522_mifare_key_t key = {
.value = { RC522_MIFARE_KEY_VALUE_DEFAULT },
};
rc522_mifare_key_t key_b = {
.type = RC522_MIFARE_KEY_B,
.value = { 0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xF0 },
};
rc522_mifare_desc_t mifare;
ESP_RETURN_ON_ERROR(rc522_mifare_get_desc(picc, &mifare), TAG, "");
DUMP("\n");
dump_header();
// Start from the highest sector
uint8_t sector_index = mifare.number_of_sectors - 1;
do {
rc522_mifare_sector_desc_t sector;
ESP_RETURN_ON_ERROR(rc522_mifare_get_sector_desc(sector_index, &sector), TAG, "");
if (sector_index == 0 || sector_index == 1 || sector_index == 2) {
ESP_RETURN_ON_ERROR(rc522_mifare_auth_sector(scanner, picc, &sector, &key_b), TAG, "");
} else
{
ESP_RETURN_ON_ERROR(rc522_mifare_auth_sector(scanner, picc, &sector, &key), TAG, "");
}
rc522_mifare_sector_block_t trailer;
ESP_RETURN_ON_ERROR(rc522_mifare_read_sector_trailer_block(scanner, picc, &sector, &trailer), TAG, "");
dump_block(&trailer, sector_index);
// Start from the highest (non-trailer) block
uint8_t block_offset = sector.number_of_blocks - 2;
do {
rc522_mifare_sector_block_t block;
ESP_RETURN_ON_ERROR(rc522_mifare_read_sector_block(scanner, picc, &sector, &trailer, block_offset, &block),
TAG,
"");
dump_block(&block, sector_index);
}
while (block_offset--);
}
while (sector_index--);
DUMP("\n");
return ESP_OK;
}
static esp_err_t read_write(rc522_handle_t scanner, rc522_picc_t *picc)
{
uint8_t read_buffer[RC522_MIFARE_BLOCK_SIZE];
// uint8_t write_buffer[RC522_MIFARE_BLOCK_SIZE] = {0x01 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0xFF ,0x07 ,0x80 ,0x69 ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xF0};
uint8_t write_buffer[RC522_MIFARE_BLOCK_SIZE] = {0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0xFF ,0x07 ,0x80 ,0x69 ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xF0};
rc522_mifare_key_t key_a = {
.type = RC522_MIFARE_KEY_A,
.value = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
// .value = { RC522_MIFARE_KEY_VALUE_DEFAULT },
};
rc522_mifare_key_t key_b = {
.type = RC522_MIFARE_KEY_B,
.value = { 0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xF0 },
// .value = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
};
rc522_mifare_sector_desc_t sector;
sector.index = 3;
// rc522_mifare_get_sector_desc(7, &sector); // 获取第5扇区信息
// 鉴权
ESP_RETURN_ON_ERROR(rc522_mifare_auth(scanner, picc, sector.index, &key_a), TAG, "auth fail");
ESP_RETURN_ON_ERROR(rc522_mifare_read(scanner, picc, sector.index, read_buffer), TAG, "read fail");
dump_block_buffer(read_buffer);
// 鉴权
ESP_RETURN_ON_ERROR(rc522_mifare_auth(scanner, picc, sector.index, &key_b), TAG, "auth fail");
ESP_RETURN_ON_ERROR(rc522_mifare_read(scanner, picc, sector.index, read_buffer), TAG, "read fail");
dump_block_buffer(read_buffer);
// ESP_RETURN_ON_ERROR(rc522_mifare_write(scanner, picc, sector.index, write_buffer), TAG, "write fail");
// ESP_RETURN_ON_ERROR(rc522_mifare_read(scanner, picc, sector.index, read_buffer), TAG, "read fail");
// dump_block_buffer(read_buffer);
return ESP_OK;
}
static void on_picc_state_changed(void *arg, esp_event_base_t base, int32_t event_id, void *data)
{
rc522_picc_state_changed_event_t *event = (rc522_picc_state_changed_event_t *)data;
rc522_picc_t *picc = event->picc;
if (picc->state != RC522_PICC_STATE_ACTIVE) {
return;
}
rc522_picc_print(picc);
picc->type = RC522_PICC_TYPE_MIFARE_1K;
if (!rc522_mifare_type_is_classic_compatible(picc->type)) {
ESP_LOGW(TAG, "Card is not supported by this example");
return;
}
if (dump_memory(scanner, picc) == ESP_OK) {
ESP_LOGI(TAG, "Memory dump success");
}
else {
ESP_LOGE(TAG, "Memory dump failed");
}
if (read_write(scanner, picc) == ESP_OK) {
ESP_LOGI(TAG, "Read/Write success");
}
else {
ESP_LOGE(TAG, "Read/Write failed");
}
if (rc522_mifare_deauth(scanner, picc) != ESP_OK) {
ESP_LOGW(TAG, "Deauth failed");
}
}
void rfid_rc522_task(void *pvParameter)
{
rc522_spi_create(&driver_config, &driver);
rc522_driver_install(driver);
rc522_config_t scanner_config = {
.driver = driver,
};
rc522_create(&scanner_config, &scanner);
rc522_register_events(scanner, RC522_EVENT_PICC_STATE_CHANGED, on_picc_state_changed, NULL);
rc522_start(scanner);
while (1)
{
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}