diff --git a/applications/main/nfc/nfc_cli.c b/applications/main/nfc/nfc_cli.c index 2acb8cb416c..b5a40b122da 100644 --- a/applications/main/nfc/nfc_cli.c +++ b/applications/main/nfc/nfc_cli.c @@ -4,13 +4,7 @@ #include #include -#include -#include #include -#include - -#include -#include #define FLAG_EVENT (1 << 10) @@ -18,80 +12,32 @@ static void nfc_cli_print_usage() { printf("Usage:\r\n"); printf("nfc \r\n"); printf("Cmd list:\r\n"); - printf("\tdetect\t - detect nfc device\r\n"); - printf("\temulate\t - emulate predefined nfca card\r\n"); - printf("\tapdu\t - Send APDU and print response \r\n"); if(furi_hal_rtc_is_flag_set(FuriHalRtcFlagDebug)) { printf("\tfield\t - turn field on\r\n"); } } -static void furi_hal_nfc_iso15693_listener_transparent_mode_enter(FuriHalSpiBusHandle* handle) { - st25r3916_direct_cmd(handle, ST25R3916_CMD_TRANSPARENT_MODE); - - furi_hal_spi_bus_handle_deinit(handle); - furi_hal_nfc_deinit_gpio_isr(); -} - -static void furi_hal_nfc_iso15693_listener_transparent_mode_exit(FuriHalSpiBusHandle* handle) { - // Configure gpio back to SPI and exit transparent mode - furi_hal_nfc_init_gpio_isr(); - furi_hal_spi_bus_handle_init(handle); - - st25r3916_direct_cmd(handle, ST25R3916_CMD_UNMASK_RECEIVE_DATA); -} - -static void furi_hal_nfc_iso15693_parser_callback(Iso15693ParserEvent event, void* context) { - furi_assert(context); - - if(event == Iso15693ParserEventDataReceived) { - FuriThreadId thread_id = context; - furi_thread_flags_set(thread_id, FLAG_EVENT); - } -} - -// TODO remove this test command -static void nfc_cli_check(Cli* cli, FuriString* args) { +static void nfc_cli_field(Cli* cli, FuriString* args) { UNUSED(args); - UNUSED(cli); - FuriHalSpiBusHandle* handle = &furi_hal_spi_bus_handle_nfc; - uint8_t data[100] = {}; - size_t bits = 0; + // Check if nfc worker is not busy + if(furi_hal_nfc_is_hal_ready() != FuriHalNfcErrorNone) { + printf("NFC chip failed to start\r\n"); + return; + } - Nfc* nfc = nfc_alloc(); + furi_hal_nfc_acquire(); furi_hal_nfc_low_power_mode_stop(); - furi_hal_nfc_set_mode(FuriHalNfcModeListener, FuriHalNfcTechIso15693); - furi_hal_nfc_iso15693_listener_transparent_mode_enter(handle); - Iso15693Parser* instance = iso15693_parser_alloc(&gpio_spi_r_miso, 1024); - - FuriThreadId thread_id = furi_thread_get_current_id(); - furi_thread_set_current_priority(FuriThreadPriorityHighest); - iso15693_parser_start(instance, furi_hal_nfc_iso15693_parser_callback, thread_id); + furi_hal_nfc_poller_field_on(); - while(true) { - uint32_t flag = furi_thread_flags_wait(FLAG_EVENT, FuriFlagWaitAny, FuriWaitForever); - furi_thread_flags_clear(flag); + printf("Field is on. Don't leave device in this mode for too long.\r\n"); + printf("Press Ctrl+C to abort\r\n"); - if(flag & FLAG_EVENT) { - if(iso15693_parser_run(instance)) { - iso15693_parser_get_data(instance, data, sizeof(data), &bits); - break; - } - } + while(!cli_cmd_interrupt_received(cli)) { + furi_delay_ms(50); } - for(size_t i = 0; i < bits / 8; i++) { - printf("%02X ", data[i]); - } - printf("\r\n"); - - iso15693_parser_stop(instance); - furi_hal_nfc_iso15693_listener_transparent_mode_exit(handle); - - iso15693_parser_free(instance); - furi_hal_nfc_reset_mode(); furi_hal_nfc_low_power_mode_start(); - nfc_free(nfc); + furi_hal_nfc_release(); } static void nfc_cli(Cli* cli, FuriString* args, void* context) { @@ -104,9 +50,11 @@ static void nfc_cli(Cli* cli, FuriString* args, void* context) { nfc_cli_print_usage(); break; } - if(furi_string_cmp_str(cmd, "c") == 0) { - nfc_cli_check(cli, args); - break; + if(furi_hal_rtc_is_flag_set(FuriHalRtcFlagDebug)) { + if(furi_string_cmp_str(cmd, "field") == 0) { + nfc_cli_field(cli, args); + break; + } } nfc_cli_print_usage();