From d962d63e2da8fa5067d121e2436410c939f71354 Mon Sep 17 00:00:00 2001 From: kopecdav Date: Mon, 10 Feb 2025 15:42:32 +0100 Subject: [PATCH] chore(core): Redo NFC driver to event based structure --- core/embed/io/nfc/inc/io/nfc.h | 1 + core/embed/io/nfc/st25r3916b/nfc.c | 42 ++++++++++++++++++++++++++---- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/core/embed/io/nfc/inc/io/nfc.h b/core/embed/io/nfc/inc/io/nfc.h index 66004d1976..2bb0178fe8 100644 --- a/core/embed/io/nfc/inc/io/nfc.h +++ b/core/embed/io/nfc/inc/io/nfc.h @@ -46,6 +46,7 @@ typedef enum { typedef enum { NFC_NO_EVENT, + NFC_EVENT_DEACTIVATED, NFC_EVENT_ACTIVATED, } nfc_event_t; diff --git a/core/embed/io/nfc/st25r3916b/nfc.c b/core/embed/io/nfc/st25r3916b/nfc.c index 657369fdb4..dd20375b30 100644 --- a/core/embed/io/nfc/st25r3916b/nfc.c +++ b/core/embed/io/nfc/st25r3916b/nfc.c @@ -51,16 +51,20 @@ // NFC-F PAD0 #define LM_PAD0 0x00U +typedef enum{ + NFC_STATE_ACTIVE, + NFC_STATE_NOT_ACTIVE +} nfc_state_t; + typedef struct { bool initialized; // SPI driver SPI_HandleTypeDef hspi; // NFC IRQ pin callback void (*nfc_irq_callback)(void); - EXTI_HandleTypeDef hEXTI; - rfalNfcDiscoverParam disc_params; + nfc_state_t last_nfc_state; } st25r3916b_driver_t; static st25r3916b_driver_t g_st25r3916b_driver = { @@ -211,6 +215,7 @@ nfc_status_t nfc_init() { } drv->initialized = true; + drv->last_nfc_state = NFC_STATE_NOT_ACTIVE; return NFC_OK; } @@ -375,14 +380,40 @@ nfc_status_t nfc_get_event(nfc_event_t *event) { return NFC_NOT_INITIALIZED; } - static rfalNfcDevice *nfcDevice; + rfalNfcDevice *nfcDevice; // Run RFAL worker periodically rfalNfcWorker(); - if(rfalNfcIsDevActivated(rfalNfcGetState())) { + rfalNfcState rfal_state = rfalNfcGetState(); - *event = NFC_EVENT_ACTIVATED; + nfc_state_t cur_nfc_state = NFC_STATE_NOT_ACTIVE; + + if(rfalNfcIsDevActivated(rfal_state)) { + cur_nfc_state = NFC_STATE_ACTIVE; + } + + if(cur_nfc_state != drv->last_nfc_state){ + + switch(cur_nfc_state){ + + case NFC_STATE_ACTIVE: + *event = NFC_EVENT_ACTIVATED; + break; + + case NFC_STATE_NOT_ACTIVE: + *event = NFC_EVENT_DEACTIVATED; + break; + + default: + *event = NFC_NO_EVENT; + } + + drv->last_nfc_state = cur_nfc_state; + + } + + if(cur_nfc_state == NFC_STATE_ACTIVE) { rfalNfcGetActiveDevice(&nfcDevice); @@ -434,6 +465,7 @@ nfc_status_t nfc_get_event(nfc_event_t *event) { rfalNfcDeactivate(RFAL_NFC_DEACTIVATE_DISCOVERY); // Automatically deactivate } + // No event in CE mode, activation/deactivation handled automatically *event = NFC_NO_EVENT; break;