diff --git a/dependencies.lock b/dependencies.lock index 416ca2b..d7d4113 100644 --- a/dependencies.lock +++ b/dependencies.lock @@ -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 diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index dad2e43..9f313d3 100755 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -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/" diff --git a/main/idf_component.yml b/main/idf_component.yml index 5fc5359..bde3f72 100644 --- a/main/idf_component.yml +++ b/main/idf_component.yml @@ -1,5 +1,6 @@ ## IDF Component Manager Manifest File dependencies: + abobija/rc522: "^3.3.1" lvgl/lvgl: "^8.3.0" ## Required IDF version idf: diff --git a/main/main.c b/main/main.c index 16c9848..e790719 100755 --- a/main/main.c +++ b/main/main.c @@ -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); diff --git a/main/rfid_rc522/memory_dump.c b/main/rfid_rc522/memory_dump.c new file mode 100644 index 0000000..744a706 --- /dev/null +++ b/main/rfid_rc522/memory_dump.c @@ -0,0 +1,194 @@ +#include +#include +#include +#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 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_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, §or), TAG, ""); + ESP_RETURN_ON_ERROR(rc522_mifare_auth_sector(scanner, picc, §or, &key), TAG, ""); + + rc522_mifare_sector_block_t trailer; + ESP_RETURN_ON_ERROR(rc522_mifare_read_sector_trailer_block(scanner, picc, §or, &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, §or, &trailer, block_offset, &block), + TAG, + ""); + + dump_block(&block, sector_index); + } + while (block_offset--); + } + while (sector_index--); + + DUMP("\n"); + + 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 (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); + } + +}