From 1d7966f74ef1b7ba04b536619b3b7d48804d3bd8 Mon Sep 17 00:00:00 2001 From: gornekich Date: Thu, 1 Jun 2023 16:37:47 +0400 Subject: [PATCH] NFC: fix MFC timings (#2719) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * digital signal: add optimization * nfc test: more restrict tests * digital signal: build as separate library * digital signal: remove unused flags, format sources * digital signal: fix cflag name * target: fix build for f18 target Co-authored-by: あく --- applications/debug/unit_tests/nfc/nfc_test.c | 61 +++++++++++++++++--- firmware/targets/f18/api_symbols.csv | 21 ++++++- firmware/targets/f7/api_symbols.csv | 3 +- firmware/targets/f7/target.json | 1 + lib/ReadMe.md | 1 + lib/SConscript | 2 +- lib/digital_signal/SConscript | 20 +++++++ lib/digital_signal/digital_signal.c | 3 +- lib/misc.scons | 2 - 9 files changed, 98 insertions(+), 16 deletions(-) create mode 100644 lib/digital_signal/SConscript diff --git a/applications/debug/unit_tests/nfc/nfc_test.c b/applications/debug/unit_tests/nfc/nfc_test.c index 54bdd5909..bc2f7887b 100644 --- a/applications/debug/unit_tests/nfc/nfc_test.c +++ b/applications/debug/unit_tests/nfc/nfc_test.c @@ -27,6 +27,12 @@ static const uint32_t nfc_test_file_version = 1; #define NFC_TEST_DATA_MAX_LEN 18 #define NFC_TETS_TIMINGS_MAX_LEN 1350 +// Maximum allowed time for buffer preparation to fit 500us nt message timeout +#define NFC_TEST_4_BYTE_BUILD_BUFFER_TIM_MAX (150) +#define NFC_TEST_16_BYTE_BUILD_BUFFER_TIM_MAX (640) +#define NFC_TEST_4_BYTE_BUILD_SIGNAL_TIM_MAX (110) +#define NFC_TEST_16_BYTE_BUILD_SIGNAL_TIM_MAX (440) + typedef struct { Storage* storage; NfcaSignal* signal; @@ -89,13 +95,13 @@ static bool nfc_test_read_signal_from_file(const char* file_name) { static bool nfc_test_digital_signal_test_encode( const char* file_name, - uint32_t encode_max_time, + uint32_t build_signal_max_time_us, + uint32_t build_buffer_max_time_us, uint32_t timing_tolerance, uint32_t timings_sum_tolerance) { furi_assert(nfc_test); bool success = false; - uint32_t time = 0; uint32_t dut_timings_sum = 0; uint32_t ref_timings_sum = 0; uint8_t parity[10] = {}; @@ -109,17 +115,37 @@ static bool nfc_test_digital_signal_test_encode( // Encode signal FURI_CRITICAL_ENTER(); - time = DWT->CYCCNT; + uint32_t time_start = DWT->CYCCNT; + nfca_signal_encode( nfc_test->signal, nfc_test->test_data, nfc_test->test_data_len * 8, parity); + + uint32_t time_signal = + (DWT->CYCCNT - time_start) / furi_hal_cortex_instructions_per_microsecond(); + + time_start = DWT->CYCCNT; + digital_signal_prepare_arr(nfc_test->signal->tx_signal); - time = (DWT->CYCCNT - time) / furi_hal_cortex_instructions_per_microsecond(); + + uint32_t time_buffer = + (DWT->CYCCNT - time_start) / furi_hal_cortex_instructions_per_microsecond(); FURI_CRITICAL_EXIT(); // Check timings - if(time > encode_max_time) { + if(time_signal > build_signal_max_time_us) { FURI_LOG_E( - TAG, "Encoding time: %ld us while accepted value: %ld us", time, encode_max_time); + TAG, + "Build signal time: %ld us while accepted value: %ld us", + time_signal, + build_signal_max_time_us); + break; + } + if(time_buffer > build_buffer_max_time_us) { + FURI_LOG_E( + TAG, + "Build buffer time: %ld us while accepted value: %ld us", + time_buffer, + build_buffer_max_time_us); break; } @@ -156,7 +182,16 @@ static bool nfc_test_digital_signal_test_encode( break; } - FURI_LOG_I(TAG, "Encoding time: %ld us. Acceptable time: %ld us", time, encode_max_time); + FURI_LOG_I( + TAG, + "Build signal time: %ld us. Acceptable time: %ld us", + time_signal, + build_signal_max_time_us); + FURI_LOG_I( + TAG, + "Build buffer time: %ld us. Acceptable time: %ld us", + time_buffer, + build_buffer_max_time_us); FURI_LOG_I( TAG, "Timings sum difference: %ld [1/64MHZ]. Acceptable difference: %ld [1/64MHz]", @@ -171,11 +206,19 @@ static bool nfc_test_digital_signal_test_encode( MU_TEST(nfc_digital_signal_test) { mu_assert( nfc_test_digital_signal_test_encode( - NFC_TEST_RESOURCES_DIR NFC_TEST_SIGNAL_SHORT_FILE, 500, 1, 37), + NFC_TEST_RESOURCES_DIR NFC_TEST_SIGNAL_SHORT_FILE, + NFC_TEST_4_BYTE_BUILD_SIGNAL_TIM_MAX, + NFC_TEST_4_BYTE_BUILD_BUFFER_TIM_MAX, + 1, + 37), "NFC short digital signal test failed\r\n"); mu_assert( nfc_test_digital_signal_test_encode( - NFC_TEST_RESOURCES_DIR NFC_TEST_SIGNAL_LONG_FILE, 2000, 1, 37), + NFC_TEST_RESOURCES_DIR NFC_TEST_SIGNAL_LONG_FILE, + NFC_TEST_16_BYTE_BUILD_SIGNAL_TIM_MAX, + NFC_TEST_16_BYTE_BUILD_BUFFER_TIM_MAX, + 1, + 37), "NFC long digital signal test failed\r\n"); } diff --git a/firmware/targets/f18/api_symbols.csv b/firmware/targets/f18/api_symbols.csv index 7f0dcebd5..3c075e0d1 100644 --- a/firmware/targets/f18/api_symbols.csv +++ b/firmware/targets/f18/api_symbols.csv @@ -1,5 +1,5 @@ entry,status,name,type,params -Version,+,28.1,, +Version,+,28.2,, Header,+,applications/services/bt/bt_service/bt.h,, Header,+,applications/services/cli/cli.h,, Header,+,applications/services/cli/cli_vcp.h,, @@ -80,6 +80,7 @@ Header,+,firmware/targets/furi_hal_include/furi_hal_usb_hid.h,, Header,+,firmware/targets/furi_hal_include/furi_hal_usb_hid_u2f.h,, Header,+,firmware/targets/furi_hal_include/furi_hal_version.h,, Header,+,firmware/targets/furi_hal_include/furi_hal_vibro.h,, +Header,+,lib/digital_signal/digital_signal.h,, Header,+,lib/flipper_application/api_hashtable/api_hashtable.h,, Header,+,lib/flipper_application/api_hashtable/compilesort.hpp,, Header,+,lib/flipper_application/flipper_application.h,, @@ -617,6 +618,24 @@ Function,+,dialog_message_set_text,void,"DialogMessage*, const char*, uint8_t, u Function,+,dialog_message_show,DialogMessageButton,"DialogsApp*, const DialogMessage*" Function,+,dialog_message_show_storage_error,void,"DialogsApp*, const char*" Function,-,difftime,double,"time_t, time_t" +Function,-,digital_sequence_add,void,"DigitalSequence*, uint8_t" +Function,-,digital_sequence_alloc,DigitalSequence*,"uint32_t, const GpioPin*" +Function,-,digital_sequence_clear,void,DigitalSequence* +Function,-,digital_sequence_free,void,DigitalSequence* +Function,-,digital_sequence_send,_Bool,DigitalSequence* +Function,-,digital_sequence_set_sendtime,void,"DigitalSequence*, uint32_t" +Function,-,digital_sequence_set_signal,void,"DigitalSequence*, uint8_t, DigitalSignal*" +Function,-,digital_sequence_timebase_correction,void,"DigitalSequence*, float" +Function,-,digital_signal_add,void,"DigitalSignal*, uint32_t" +Function,-,digital_signal_add_pulse,void,"DigitalSignal*, uint32_t, _Bool" +Function,-,digital_signal_alloc,DigitalSignal*,uint32_t +Function,-,digital_signal_append,_Bool,"DigitalSignal*, DigitalSignal*" +Function,-,digital_signal_free,void,DigitalSignal* +Function,-,digital_signal_get_edge,uint32_t,"DigitalSignal*, uint32_t" +Function,-,digital_signal_get_edges_cnt,uint32_t,DigitalSignal* +Function,-,digital_signal_get_start_level,_Bool,DigitalSignal* +Function,-,digital_signal_prepare_arr,void,DigitalSignal* +Function,-,digital_signal_send,void,"DigitalSignal*, const GpioPin*" Function,-,diprintf,int,"int, const char*, ..." Function,+,dir_walk_alloc,DirWalk*,Storage* Function,+,dir_walk_close,void,DirWalk* diff --git a/firmware/targets/f7/api_symbols.csv b/firmware/targets/f7/api_symbols.csv index 1259f0fce..a9ce0c26a 100644 --- a/firmware/targets/f7/api_symbols.csv +++ b/firmware/targets/f7/api_symbols.csv @@ -1,5 +1,5 @@ entry,status,name,type,params -Version,+,28.1,, +Version,+,28.2,, Header,+,applications/services/bt/bt_service/bt.h,, Header,+,applications/services/cli/cli.h,, Header,+,applications/services/cli/cli_vcp.h,, @@ -86,6 +86,7 @@ Header,+,firmware/targets/furi_hal_include/furi_hal_usb_hid.h,, Header,+,firmware/targets/furi_hal_include/furi_hal_usb_hid_u2f.h,, Header,+,firmware/targets/furi_hal_include/furi_hal_version.h,, Header,+,firmware/targets/furi_hal_include/furi_hal_vibro.h,, +Header,+,lib/digital_signal/digital_signal.h,, Header,+,lib/flipper_application/api_hashtable/api_hashtable.h,, Header,+,lib/flipper_application/api_hashtable/compilesort.hpp,, Header,+,lib/flipper_application/flipper_application.h,, diff --git a/firmware/targets/f7/target.json b/firmware/targets/f7/target.json index c50364453..e3dc78325 100644 --- a/firmware/targets/f7/target.json +++ b/firmware/targets/f7/target.json @@ -28,6 +28,7 @@ "flipperformat", "toolbox", "nfc", + "digital_signal", "pulse_reader", "microtar", "usb_stm32", diff --git a/lib/ReadMe.md b/lib/ReadMe.md index 93236b267..138bef2b3 100644 --- a/lib/ReadMe.md +++ b/lib/ReadMe.md @@ -27,6 +27,7 @@ - `nfc` - NFC library, used by NFC application - `one_wire` - OneWire library, used by iButton application - `print` - Tiny printf implementation +- `digital_signal` - Digital Signal library used by NFC for software implemented protocols - `pulse_reader` - Pulse Reader library used by NFC for software implemented protocols - `qrcode` - QR-Code library - `stm32wb_cmsis` - STM32WB series CMSIS headers, extends CMSIS Core diff --git a/lib/SConscript b/lib/SConscript index 8727746d8..495ba4bfe 100644 --- a/lib/SConscript +++ b/lib/SConscript @@ -15,7 +15,6 @@ env.Append( Dir("u8g2"), Dir("update_util"), Dir("print"), - Dir("pulse_reader"), ], ) @@ -95,6 +94,7 @@ libs = env.BuildModules( "mbedtls", "subghz", "nfc", + "digital_signal", "pulse_reader", "appframe", "misc", diff --git a/lib/digital_signal/SConscript b/lib/digital_signal/SConscript new file mode 100644 index 000000000..2ddf7a58b --- /dev/null +++ b/lib/digital_signal/SConscript @@ -0,0 +1,20 @@ +Import("env") + +env.Append( + CPPPATH=[ + "#/lib/digital_signal", + ], + SDK_HEADERS=[ + File("digital_signal.h"), + ], +) + +libenv = env.Clone(FW_LIB_NAME="digital_signal") +libenv.ApplyLibFlags() +libenv.Append(CCFLAGS=["-O3", "-funroll-loops", "-Ofast"]) + +sources = libenv.GlobRecursive("*.c*") + +lib = libenv.StaticLibrary("${FW_LIB_NAME}", sources) +libenv.Install("${LIB_DIST_DIR}", lib) +Return("lib") diff --git a/lib/digital_signal/digital_signal.c b/lib/digital_signal/digital_signal.c index ab25458b5..94dbeeab9 100644 --- a/lib/digital_signal/digital_signal.c +++ b/lib/digital_signal/digital_signal.c @@ -212,8 +212,7 @@ void digital_signal_prepare_arr(DigitalSignal* signal) { internals->reload_reg_entries = 0; for(size_t pos = 0; pos < signal->edge_cnt; pos++) { - uint32_t edge_scaled = (internals->factor * signal->edge_timings[pos]) / (1024 * 1024); - uint32_t pulse_duration = edge_scaled + internals->reload_reg_remainder; + uint32_t pulse_duration = signal->edge_timings[pos] + internals->reload_reg_remainder; if(pulse_duration < 10 || pulse_duration > 10000000) { FURI_LOG_D( TAG, diff --git a/lib/misc.scons b/lib/misc.scons index 1ff6e2fb0..10aa3f9d5 100644 --- a/lib/misc.scons +++ b/lib/misc.scons @@ -4,7 +4,6 @@ Import("env") env.Append( CPPPATH=[ - "#/lib/digital_signal", "#/lib/fnv1a_hash", "#/lib/heatshrink", "#/lib/micro-ecc", @@ -26,7 +25,6 @@ libenv.ApplyLibFlags() sources = [] libs_recurse = [ - "digital_signal", "micro-ecc", "u8g2", "update_util",