From e0502ca2172cdc78e3cc32f95b44391f03d8c60b Mon Sep 17 00:00:00 2001 From: Georgii Surkov Date: Wed, 27 Sep 2023 14:02:23 +0300 Subject: [PATCH 1/4] Implement per-technology delay compensation --- firmware/targets/f7/furi_hal/furi_hal_nfc.c | 2 +- .../targets/f7/furi_hal/furi_hal_nfc_felica.c | 6 +- firmware/targets/f7/furi_hal/furi_hal_nfc_i.h | 95 +++++++++++-------- .../f7/furi_hal/furi_hal_nfc_iso14443a.c | 8 ++ .../f7/furi_hal/furi_hal_nfc_iso14443b.c | 6 +- .../f7/furi_hal/furi_hal_nfc_iso15693.c | 14 +++ .../targets/f7/furi_hal/furi_hal_nfc_timer.c | 30 +++++- lib/nfc/nfc.c | 6 ++ 8 files changed, 122 insertions(+), 45 deletions(-) diff --git a/firmware/targets/f7/furi_hal/furi_hal_nfc.c b/firmware/targets/f7/furi_hal/furi_hal_nfc.c index e93e9701c7b..93ae9632185 100644 --- a/firmware/targets/f7/furi_hal/furi_hal_nfc.c +++ b/firmware/targets/f7/furi_hal/furi_hal_nfc.c @@ -7,7 +7,7 @@ #define TAG "FuriHalNfc" -static const FuriHalNfcTechBase* furi_hal_nfc_tech[FuriHalNfcTechNum] = { +const FuriHalNfcTechBase* furi_hal_nfc_tech[FuriHalNfcTechNum] = { [FuriHalNfcTechIso14443a] = &furi_hal_nfc_iso14443a, [FuriHalNfcTechIso14443b] = &furi_hal_nfc_iso14443b, [FuriHalNfcTechIso15693] = &furi_hal_nfc_iso15693, diff --git a/firmware/targets/f7/furi_hal/furi_hal_nfc_felica.c b/firmware/targets/f7/furi_hal/furi_hal_nfc_felica.c index 251956e3c31..619b1e9eea0 100644 --- a/firmware/targets/f7/furi_hal/furi_hal_nfc_felica.c +++ b/firmware/targets/f7/furi_hal/furi_hal_nfc_felica.c @@ -52,6 +52,10 @@ static FuriHalNfcError furi_hal_nfc_felica_poller_deinit(FuriHalSpiBusHandle* ha const FuriHalNfcTechBase furi_hal_nfc_felica = { .poller = { + .compensation = + { + .fwt = 0, + }, .init = furi_hal_nfc_felica_poller_init, .deinit = furi_hal_nfc_felica_poller_deinit, .wait_event = furi_hal_nfc_wait_event_common, @@ -59,5 +63,5 @@ const FuriHalNfcTechBase furi_hal_nfc_felica = { .rx = furi_hal_nfc_common_fifo_rx, }, - .listener = {0}, + .listener = {}, }; diff --git a/firmware/targets/f7/furi_hal/furi_hal_nfc_i.h b/firmware/targets/f7/furi_hal/furi_hal_nfc_i.h index 677fee5180e..7faa85e8dd0 100644 --- a/firmware/targets/f7/furi_hal/furi_hal_nfc_i.h +++ b/firmware/targets/f7/furi_hal/furi_hal_nfc_i.h @@ -3,6 +3,7 @@ #include #include #include + #include #include @@ -35,45 +36,6 @@ typedef struct { FuriHalNfcTech tech; } FuriHalNfc; -extern FuriHalNfc furi_hal_nfc; - -void furi_hal_nfc_event_init(); - -void furi_hal_nfc_event_set(FuriHalNfcEventInternalType event); - -void furi_hal_nfc_init_gpio_isr(); - -void furi_hal_nfc_deinit_gpio_isr(); - -void furi_hal_nfc_timers_init(); - -void furi_hal_nfc_timers_deinit(); - -uint32_t furi_hal_nfc_get_irq(FuriHalSpiBusHandle* handle); - -bool furi_hal_nfc_event_wait_for_specific_irq( - FuriHalSpiBusHandle* handle, - uint32_t mask, - uint32_t timeout_ms); - -// Common technology methods -FuriHalNfcEvent furi_hal_nfc_wait_event_common(uint32_t timeout_ms); -FuriHalNfcError furi_hal_nfc_common_listener_rx_start(FuriHalSpiBusHandle* handle); -FuriHalNfcError furi_hal_nfc_common_fifo_tx( - FuriHalSpiBusHandle* handle, - const uint8_t* tx_data, - size_t tx_bits); -FuriHalNfcError furi_hal_nfc_common_fifo_rx( - FuriHalSpiBusHandle* handle, - uint8_t* rx_data, - size_t rx_data_size, - size_t* rx_bits); - -FuriHalNfcError furi_hal_nfc_poller_tx_common( - FuriHalSpiBusHandle* handle, - const uint8_t* tx_data, - size_t tx_bits); - // Technology specific API typedef FuriHalNfcError (*FuriHalNfcChipConfig)(FuriHalSpiBusHandle* handle); typedef FuriHalNfcError ( @@ -88,6 +50,12 @@ typedef FuriHalNfcError (*FuriHalNfcSleep)(FuriHalSpiBusHandle* handle); typedef FuriHalNfcError (*FuriHalNfcIdle)(FuriHalSpiBusHandle* handle); typedef struct { + uint32_t fdt; + uint32_t fwt; +} FuriHalNfcPollerCompensation; + +typedef struct { + FuriHalNfcPollerCompensation compensation; FuriHalNfcChipConfig init; FuriHalNfcChipConfig deinit; FuriHalNfcWaitEvent wait_event; @@ -96,6 +64,11 @@ typedef struct { } FuriHalNfcTechPollerBase; typedef struct { + uint32_t fdt; +} FuriHalNfcListenerCompensation; + +typedef struct { + FuriHalNfcListenerCompensation compensation; FuriHalNfcChipConfig init; FuriHalNfcChipConfig deinit; FuriHalNfcWaitEvent wait_event; @@ -115,6 +88,50 @@ extern const FuriHalNfcTechBase furi_hal_nfc_iso14443b; extern const FuriHalNfcTechBase furi_hal_nfc_iso15693; extern const FuriHalNfcTechBase furi_hal_nfc_felica; +extern const FuriHalNfcTechBase* furi_hal_nfc_tech[]; + +extern FuriHalNfc furi_hal_nfc; + +void furi_hal_nfc_event_init(); + +void furi_hal_nfc_event_set(FuriHalNfcEventInternalType event); + +void furi_hal_nfc_init_gpio_isr(); + +void furi_hal_nfc_deinit_gpio_isr(); + +void furi_hal_nfc_timers_init(); + +void furi_hal_nfc_timers_deinit(); + +uint32_t furi_hal_nfc_get_irq(FuriHalSpiBusHandle* handle); + +bool furi_hal_nfc_event_wait_for_specific_irq( + FuriHalSpiBusHandle* handle, + uint32_t mask, + uint32_t timeout_ms); + +// Common technology methods +FuriHalNfcEvent furi_hal_nfc_wait_event_common(uint32_t timeout_ms); + +FuriHalNfcError furi_hal_nfc_common_listener_rx_start(FuriHalSpiBusHandle* handle); + +FuriHalNfcError furi_hal_nfc_common_fifo_tx( + FuriHalSpiBusHandle* handle, + const uint8_t* tx_data, + size_t tx_bits); + +FuriHalNfcError furi_hal_nfc_common_fifo_rx( + FuriHalSpiBusHandle* handle, + uint8_t* rx_data, + size_t rx_data_size, + size_t* rx_bits); + +FuriHalNfcError furi_hal_nfc_poller_tx_common( + FuriHalSpiBusHandle* handle, + const uint8_t* tx_data, + size_t tx_bits); + #ifdef __cplusplus } #endif diff --git a/firmware/targets/f7/furi_hal/furi_hal_nfc_iso14443a.c b/firmware/targets/f7/furi_hal/furi_hal_nfc_iso14443a.c index 4f7b94bd846..139619660de 100644 --- a/firmware/targets/f7/furi_hal/furi_hal_nfc_iso14443a.c +++ b/firmware/targets/f7/furi_hal/furi_hal_nfc_iso14443a.c @@ -326,6 +326,10 @@ FuriHalNfcError furi_hal_nfc_iso14443_3a_listener_idle(FuriHalSpiBusHandle* hand const FuriHalNfcTechBase furi_hal_nfc_iso14443a = { .poller = { + .compensation = + { + .fwt = 0, + }, .init = furi_hal_nfc_iso14443a_poller_init, .deinit = furi_hal_nfc_iso14443a_poller_deinit, .wait_event = furi_hal_nfc_wait_event_common, @@ -335,6 +339,10 @@ const FuriHalNfcTechBase furi_hal_nfc_iso14443a = { .listener = { + .compensation = + { + .fdt = 0, + }, .init = furi_hal_nfc_iso14443a_listener_init, .deinit = furi_hal_nfc_iso14443a_listener_deinit, .wait_event = furi_hal_nfc_iso14443_3a_listener_wait_event, diff --git a/firmware/targets/f7/furi_hal/furi_hal_nfc_iso14443b.c b/firmware/targets/f7/furi_hal/furi_hal_nfc_iso14443b.c index 5d11a42b072..75061dc70a3 100644 --- a/firmware/targets/f7/furi_hal/furi_hal_nfc_iso14443b.c +++ b/firmware/targets/f7/furi_hal/furi_hal_nfc_iso14443b.c @@ -91,6 +91,10 @@ static FuriHalNfcError furi_hal_nfc_iso14443b_poller_deinit(FuriHalSpiBusHandle* const FuriHalNfcTechBase furi_hal_nfc_iso14443b = { .poller = { + .compensation = + { + .fwt = 0, + }, .init = furi_hal_nfc_iso14443b_poller_init, .deinit = furi_hal_nfc_iso14443b_poller_deinit, .wait_event = furi_hal_nfc_wait_event_common, @@ -98,5 +102,5 @@ const FuriHalNfcTechBase furi_hal_nfc_iso14443b = { .rx = furi_hal_nfc_common_fifo_rx, }, - .listener = {0}, + .listener = {}, }; diff --git a/firmware/targets/f7/furi_hal/furi_hal_nfc_iso15693.c b/firmware/targets/f7/furi_hal/furi_hal_nfc_iso15693.c index 1c65c26f955..0f0c4a54a7b 100644 --- a/firmware/targets/f7/furi_hal/furi_hal_nfc_iso15693.c +++ b/firmware/targets/f7/furi_hal/furi_hal_nfc_iso15693.c @@ -18,6 +18,11 @@ #define FURI_HAL_NFC_ISO15693_RESP_PATTERN_0 (0x01U) #define FURI_HAL_NFC_ISO15693_RESP_PATTERN_1 (0x02U) +// Derived experimentally +#define FURI_HAL_NFC_ISO15693_POLLER_FDT_COMP_FC (0) +#define FURI_HAL_NFC_ISO15693_POLLER_FWT_COMP_FC (0) +#define FURI_HAL_NFC_ISO15693_LISTENER_FDT_COMP_FC (2735) + #define BITS_IN_BYTE (8U) #define TAG "FuriHalIso15693" @@ -419,6 +424,11 @@ FuriHalNfcError furi_hal_nfc_iso15693_listener_sleep(FuriHalSpiBusHandle* handle const FuriHalNfcTechBase furi_hal_nfc_iso15693 = { .poller = { + .compensation = + { + .fdt = FURI_HAL_NFC_ISO15693_POLLER_FDT_COMP_FC, + .fwt = FURI_HAL_NFC_ISO15693_POLLER_FWT_COMP_FC, + }, .init = furi_hal_nfc_iso15693_poller_init, .deinit = furi_hal_nfc_iso15693_poller_deinit, .wait_event = furi_hal_nfc_wait_event_common, @@ -428,6 +438,10 @@ const FuriHalNfcTechBase furi_hal_nfc_iso15693 = { .listener = { + .compensation = + { + .fdt = FURI_HAL_NFC_ISO15693_LISTENER_FDT_COMP_FC, + }, .init = furi_hal_nfc_iso15693_listener_init, .deinit = furi_hal_nfc_iso15693_listener_deinit, .wait_event = furi_hal_nfc_iso15693_wait_event, diff --git a/firmware/targets/f7/furi_hal/furi_hal_nfc_timer.c b/firmware/targets/f7/furi_hal/furi_hal_nfc_timer.c index f68f83b38cf..8dadba0f53f 100644 --- a/firmware/targets/f7/furi_hal/furi_hal_nfc_timer.c +++ b/firmware/targets/f7/furi_hal/furi_hal_nfc_timer.c @@ -1,9 +1,9 @@ #include "furi_hal_nfc_i.h" -#include #include + +#include #include -#include #include #define FURI_HAL_NFC_FREQ_KHZ (13560U) @@ -100,8 +100,32 @@ static void furi_hal_nfc_timer_deinit(FuriHalNfcTimer timer) { } } +static uint32_t furi_hal_nfc_timer_get_compensation(FuriHalNfcTimer timer) { + const FuriHalNfcTechBase* current_tech = furi_hal_nfc_tech[furi_hal_nfc.tech]; + + if(furi_hal_nfc.mode == FuriHalNfcModePoller) { + const FuriHalNfcPollerCompensation* comp = ¤t_tech->poller.compensation; + if(timer == FuriHalNfcTimerFwt) + return comp->fwt; + else if(timer == FuriHalNfcTimerBlockTx) + return comp->fdt; + + } else if(furi_hal_nfc.mode == FuriHalNfcModeListener) { + const FuriHalNfcListenerCompensation* comp = ¤t_tech->listener.compensation; + if(timer == FuriHalNfcTimerBlockTx) return comp->fdt; + } + + return 0; +} + static void furi_hal_nfc_timer_start(FuriHalNfcTimer timer, uint32_t time_fc) { - uint32_t arr_reg = furi_hal_nfc_timers[timer].freq_khz * time_fc / FURI_HAL_NFC_FREQ_KHZ; + const uint32_t comp_fc = furi_hal_nfc_timer_get_compensation(timer); + + // Not starting the timer if the compensation value is greater than the requested delay + if(comp_fc >= time_fc) return; + + const uint32_t arr_reg = + furi_hal_nfc_timers[timer].freq_khz * (time_fc - comp_fc) / FURI_HAL_NFC_FREQ_KHZ; furi_check(arr_reg < UINT16_MAX); LL_TIM_SetAutoReload(furi_hal_nfc_timers[timer].timer, arr_reg); diff --git a/lib/nfc/nfc.c b/lib/nfc/nfc.c index 35c8f60c585..2177564ba77 100644 --- a/lib/nfc/nfc.c +++ b/lib/nfc/nfc.c @@ -129,6 +129,8 @@ static int32_t nfc_worker_listener(void* context) { instance->callback(nfc_event, instance->context); } if(event & FuriHalNfcEventRxEnd) { + furi_hal_nfc_timer_block_tx_start(instance->fdt_listen_fc); + nfc_event.type = NfcEventTypeRxEnd; furi_hal_nfc_listener_rx( instance->rx_buffer, sizeof(instance->rx_buffer), &instance->rx_bits); @@ -329,6 +331,10 @@ NfcError nfc_listener_tx(Nfc* instance, const BitBuffer* tx_buffer) { furi_assert(tx_buffer); NfcError ret = NfcErrorNone; + + while(furi_hal_nfc_timer_block_tx_is_running()) { + } + FuriHalNfcError error = furi_hal_nfc_listener_tx(bit_buffer_get_data(tx_buffer), bit_buffer_get_size(tx_buffer)); if(error != FuriHalNfcErrorNone) { From 0df347f51285978dd6f64f518f4bb920fe0570a8 Mon Sep 17 00:00:00 2001 From: Georgii Surkov Date: Wed, 27 Sep 2023 14:09:56 +0300 Subject: [PATCH 2/4] Add conditional compilation for NFC timer debug output --- .../targets/f7/furi_hal/furi_hal_nfc_timer.c | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/firmware/targets/f7/furi_hal/furi_hal_nfc_timer.c b/firmware/targets/f7/furi_hal/furi_hal_nfc_timer.c index 8dadba0f53f..d8a3a04d82b 100644 --- a/firmware/targets/f7/furi_hal/furi_hal_nfc_timer.c +++ b/firmware/targets/f7/furi_hal/furi_hal_nfc_timer.c @@ -8,6 +8,11 @@ #define FURI_HAL_NFC_FREQ_KHZ (13560U) +/** + * To enable timer debug output on GPIO, define the FURI_HAL_NFC_TIMER_DEBUG macro + * Example: ./fbt --extra-define=FURI_HAL_NFC_TIMER_DEBUG + */ + typedef enum { FuriHalNfcTimerFwt, FuriHalNfcTimerBlockTx, @@ -23,13 +28,17 @@ typedef struct { FuriHalInterruptId irq_id; IRQn_Type irq_type; bool is_configured; +#ifdef FURI_HAL_NFC_TIMER_DEBUG const GpioPin* pin; +#endif } FuriHalNfcTimerConfig; static FuriHalNfcTimerConfig furi_hal_nfc_timers[FuriHalNfcTimerCount] = { [FuriHalNfcTimerFwt] = { +#ifdef FURI_HAL_NFC_TIMER_DEBUG .pin = &gpio_ext_pa7, +#endif .timer = TIM1, .bus = FuriHalBusTIM1, .prescaler = 15, @@ -41,7 +50,9 @@ static FuriHalNfcTimerConfig furi_hal_nfc_timers[FuriHalNfcTimerCount] = { }, [FuriHalNfcTimerBlockTx] = { +#ifdef FURI_HAL_NFC_TIMER_DEBUG .pin = &gpio_ext_pa6, +#endif .timer = TIM17, .bus = FuriHalBusTIM17, .prescaler = 31, @@ -58,7 +69,9 @@ static void furi_hal_nfc_timer_irq_callback(void* context) { if(LL_TIM_IsActiveFlag_UPDATE(timer_config->timer)) { LL_TIM_ClearFlag_UPDATE(timer_config->timer); furi_hal_nfc_event_set(timer_config->event); +#ifdef FURI_HAL_NFC_TIMER_DEBUG furi_hal_gpio_write(timer_config->pin, false); +#endif } } @@ -84,9 +97,11 @@ static void furi_hal_nfc_timer_init(FuriHalNfcTimer timer) { NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 5, 0)); NVIC_EnableIRQ(furi_hal_nfc_timers[timer].irq_type); furi_hal_nfc_timers[timer].is_configured = true; +#ifdef FURI_HAL_NFC_TIMER_DEBUG furi_hal_gpio_init( furi_hal_nfc_timers[timer].pin, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh); furi_hal_gpio_write(furi_hal_nfc_timers[timer].pin, false); +#endif } static void furi_hal_nfc_timer_deinit(FuriHalNfcTimer timer) { @@ -130,7 +145,9 @@ static void furi_hal_nfc_timer_start(FuriHalNfcTimer timer, uint32_t time_fc) { LL_TIM_SetAutoReload(furi_hal_nfc_timers[timer].timer, arr_reg); LL_TIM_EnableCounter(furi_hal_nfc_timers[timer].timer); +#ifdef FURI_HAL_NFC_TIMER_DEBUG furi_hal_gpio_write(furi_hal_nfc_timers[timer].pin, true); +#endif } static void furi_hal_nfc_timer_stop(FuriHalNfcTimer timer) { @@ -140,7 +157,9 @@ static void furi_hal_nfc_timer_stop(FuriHalNfcTimer timer) { if(LL_TIM_IsActiveFlag_UPDATE(furi_hal_nfc_timers[timer].timer)) { LL_TIM_ClearFlag_UPDATE(furi_hal_nfc_timers[timer].timer); } +#ifdef FURI_HAL_NFC_TIMER_DEBUG furi_hal_gpio_write(furi_hal_nfc_timers[timer].pin, false); +#endif } void furi_hal_nfc_timers_init() { @@ -181,7 +200,9 @@ void furi_hal_nfc_timer_block_tx_start_us(uint32_t time_us) { LL_TIM_SetAutoReload(furi_hal_nfc_timers[FuriHalNfcTimerBlockTx].timer, arr_reg); LL_TIM_EnableCounter(furi_hal_nfc_timers[FuriHalNfcTimerBlockTx].timer); +#ifdef FURI_HAL_NFC_TIMER_DEBUG furi_hal_gpio_write(furi_hal_nfc_timers[FuriHalNfcTimerBlockTx].pin, true); +#endif } void furi_hal_nfc_timer_block_tx_stop() { From 84fadf103154cf5c119bf482b679966e21868e42 Mon Sep 17 00:00:00 2001 From: Georgii Surkov Date: Wed, 27 Sep 2023 16:54:38 +0300 Subject: [PATCH 3/4] Use signed numbers for delay compensation --- firmware/targets/f7/furi_hal/furi_hal_nfc_felica.c | 3 ++- firmware/targets/f7/furi_hal/furi_hal_nfc_i.h | 9 ++++++--- firmware/targets/f7/furi_hal/furi_hal_nfc_iso14443a.c | 8 ++++++-- firmware/targets/f7/furi_hal/furi_hal_nfc_iso14443b.c | 3 ++- firmware/targets/f7/furi_hal/furi_hal_nfc_timer.c | 6 +++--- firmware/targets/furi_hal_include/furi_hal_nfc.h | 1 - lib/nfc/nfc.c | 2 +- 7 files changed, 20 insertions(+), 12 deletions(-) diff --git a/firmware/targets/f7/furi_hal/furi_hal_nfc_felica.c b/firmware/targets/f7/furi_hal/furi_hal_nfc_felica.c index 619b1e9eea0..27758ad489a 100644 --- a/firmware/targets/f7/furi_hal/furi_hal_nfc_felica.c +++ b/firmware/targets/f7/furi_hal/furi_hal_nfc_felica.c @@ -54,7 +54,8 @@ const FuriHalNfcTechBase furi_hal_nfc_felica = { { .compensation = { - .fwt = 0, + .fdt = FURI_HAL_NFC_POLLER_FDT_COMP_FC, + .fwt = FURI_HAL_NFC_POLLER_FWT_COMP_FC, }, .init = furi_hal_nfc_felica_poller_init, .deinit = furi_hal_nfc_felica_poller_deinit, diff --git a/firmware/targets/f7/furi_hal/furi_hal_nfc_i.h b/firmware/targets/f7/furi_hal/furi_hal_nfc_i.h index 7faa85e8dd0..052bc7265cf 100644 --- a/firmware/targets/f7/furi_hal/furi_hal_nfc_i.h +++ b/firmware/targets/f7/furi_hal/furi_hal_nfc_i.h @@ -11,6 +11,9 @@ extern "C" { #endif +#define FURI_HAL_NFC_POLLER_FDT_COMP_FC (-500) +#define FURI_HAL_NFC_POLLER_FWT_COMP_FC (FURI_HAL_NFC_POLLER_FDT_COMP_FC) + typedef enum { FuriHalNfcEventInternalTypeAbort = (1U << 0), FuriHalNfcEventInternalTypeIrq = (1U << 1), @@ -50,8 +53,8 @@ typedef FuriHalNfcError (*FuriHalNfcSleep)(FuriHalSpiBusHandle* handle); typedef FuriHalNfcError (*FuriHalNfcIdle)(FuriHalSpiBusHandle* handle); typedef struct { - uint32_t fdt; - uint32_t fwt; + int32_t fdt; + int32_t fwt; } FuriHalNfcPollerCompensation; typedef struct { @@ -64,7 +67,7 @@ typedef struct { } FuriHalNfcTechPollerBase; typedef struct { - uint32_t fdt; + int32_t fdt; } FuriHalNfcListenerCompensation; typedef struct { diff --git a/firmware/targets/f7/furi_hal/furi_hal_nfc_iso14443a.c b/firmware/targets/f7/furi_hal/furi_hal_nfc_iso14443a.c index 139619660de..4e488e281c3 100644 --- a/firmware/targets/f7/furi_hal/furi_hal_nfc_iso14443a.c +++ b/firmware/targets/f7/furi_hal/furi_hal_nfc_iso14443a.c @@ -7,6 +7,9 @@ #define TAG "FuriHalIso14443a" +// Prevent FDT timer from starting +#define FURI_HAL_NFC_ISO14443A_LISTENER_FDT_COMP_FC (INT32_MAX) + static Iso14443_3aSignal* iso14443_3a_signal = NULL; static FuriHalNfcError furi_hal_nfc_iso14443a_common_init(FuriHalSpiBusHandle* handle) { @@ -328,7 +331,8 @@ const FuriHalNfcTechBase furi_hal_nfc_iso14443a = { { .compensation = { - .fwt = 0, + .fdt = FURI_HAL_NFC_POLLER_FDT_COMP_FC, + .fwt = FURI_HAL_NFC_POLLER_FWT_COMP_FC, }, .init = furi_hal_nfc_iso14443a_poller_init, .deinit = furi_hal_nfc_iso14443a_poller_deinit, @@ -341,7 +345,7 @@ const FuriHalNfcTechBase furi_hal_nfc_iso14443a = { { .compensation = { - .fdt = 0, + .fdt = FURI_HAL_NFC_ISO14443A_LISTENER_FDT_COMP_FC, }, .init = furi_hal_nfc_iso14443a_listener_init, .deinit = furi_hal_nfc_iso14443a_listener_deinit, diff --git a/firmware/targets/f7/furi_hal/furi_hal_nfc_iso14443b.c b/firmware/targets/f7/furi_hal/furi_hal_nfc_iso14443b.c index 75061dc70a3..bb1f9f59ec0 100644 --- a/firmware/targets/f7/furi_hal/furi_hal_nfc_iso14443b.c +++ b/firmware/targets/f7/furi_hal/furi_hal_nfc_iso14443b.c @@ -93,7 +93,8 @@ const FuriHalNfcTechBase furi_hal_nfc_iso14443b = { { .compensation = { - .fwt = 0, + .fdt = FURI_HAL_NFC_POLLER_FDT_COMP_FC, + .fwt = FURI_HAL_NFC_POLLER_FWT_COMP_FC, }, .init = furi_hal_nfc_iso14443b_poller_init, .deinit = furi_hal_nfc_iso14443b_poller_deinit, diff --git a/firmware/targets/f7/furi_hal/furi_hal_nfc_timer.c b/firmware/targets/f7/furi_hal/furi_hal_nfc_timer.c index d8a3a04d82b..ca9953e0411 100644 --- a/firmware/targets/f7/furi_hal/furi_hal_nfc_timer.c +++ b/firmware/targets/f7/furi_hal/furi_hal_nfc_timer.c @@ -115,7 +115,7 @@ static void furi_hal_nfc_timer_deinit(FuriHalNfcTimer timer) { } } -static uint32_t furi_hal_nfc_timer_get_compensation(FuriHalNfcTimer timer) { +static int32_t furi_hal_nfc_timer_get_compensation(FuriHalNfcTimer timer) { const FuriHalNfcTechBase* current_tech = furi_hal_nfc_tech[furi_hal_nfc.tech]; if(furi_hal_nfc.mode == FuriHalNfcModePoller) { @@ -134,10 +134,10 @@ static uint32_t furi_hal_nfc_timer_get_compensation(FuriHalNfcTimer timer) { } static void furi_hal_nfc_timer_start(FuriHalNfcTimer timer, uint32_t time_fc) { - const uint32_t comp_fc = furi_hal_nfc_timer_get_compensation(timer); + const int32_t comp_fc = furi_hal_nfc_timer_get_compensation(timer); // Not starting the timer if the compensation value is greater than the requested delay - if(comp_fc >= time_fc) return; + if(comp_fc >= (int32_t)time_fc) return; const uint32_t arr_reg = furi_hal_nfc_timers[timer].freq_khz * (time_fc - comp_fc) / FURI_HAL_NFC_FREQ_KHZ; diff --git a/firmware/targets/furi_hal_include/furi_hal_nfc.h b/firmware/targets/furi_hal_include/furi_hal_nfc.h index d5ba3b8a73f..26c2d452b7b 100644 --- a/firmware/targets/furi_hal_include/furi_hal_nfc.h +++ b/firmware/targets/furi_hal_include/furi_hal_nfc.h @@ -8,7 +8,6 @@ extern "C" { #endif -#define FURI_HAL_NFC_TIMER_COMPENSATION_FC (500) #define FURI_HAL_NFC_EVENT_WAIT_FOREVER (0xFFFFFFFFU) typedef enum { diff --git a/lib/nfc/nfc.c b/lib/nfc/nfc.c index 2177564ba77..24f6ad71f79 100644 --- a/lib/nfc/nfc.c +++ b/lib/nfc/nfc.c @@ -360,7 +360,7 @@ static NfcError nfc_poller_trx_state_machine(Nfc* instance, uint32_t fwt_fc) { if(event & FuriHalNfcEventTxEnd) { if(instance->comm_state == NfcCommStateWaitTxEnd) { if(fwt_fc) { - furi_hal_nfc_timer_fwt_start(fwt_fc + FURI_HAL_NFC_TIMER_COMPENSATION_FC); + furi_hal_nfc_timer_fwt_start(fwt_fc); } furi_hal_nfc_timer_block_tx_start_us(instance->fdt_poll_poll_us); instance->comm_state = NfcCommStateWaitRxStart; From 8358582b7646569e7f7d62f88ceea51879d01e43 Mon Sep 17 00:00:00 2001 From: Georgii Surkov Date: Wed, 27 Sep 2023 17:23:30 +0300 Subject: [PATCH 4/4] Fix ISO15693 poller compensation times --- firmware/targets/f7/furi_hal/furi_hal_nfc_iso15693.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/firmware/targets/f7/furi_hal/furi_hal_nfc_iso15693.c b/firmware/targets/f7/furi_hal/furi_hal_nfc_iso15693.c index 0f0c4a54a7b..513bf5eb3c4 100644 --- a/firmware/targets/f7/furi_hal/furi_hal_nfc_iso15693.c +++ b/firmware/targets/f7/furi_hal/furi_hal_nfc_iso15693.c @@ -19,8 +19,6 @@ #define FURI_HAL_NFC_ISO15693_RESP_PATTERN_1 (0x02U) // Derived experimentally -#define FURI_HAL_NFC_ISO15693_POLLER_FDT_COMP_FC (0) -#define FURI_HAL_NFC_ISO15693_POLLER_FWT_COMP_FC (0) #define FURI_HAL_NFC_ISO15693_LISTENER_FDT_COMP_FC (2735) #define BITS_IN_BYTE (8U) @@ -426,8 +424,8 @@ const FuriHalNfcTechBase furi_hal_nfc_iso15693 = { { .compensation = { - .fdt = FURI_HAL_NFC_ISO15693_POLLER_FDT_COMP_FC, - .fwt = FURI_HAL_NFC_ISO15693_POLLER_FWT_COMP_FC, + .fdt = FURI_HAL_NFC_POLLER_FDT_COMP_FC, + .fwt = FURI_HAL_NFC_POLLER_FWT_COMP_FC, }, .init = furi_hal_nfc_iso15693_poller_init, .deinit = furi_hal_nfc_iso15693_poller_deinit,