diff --git a/core/SConscript.prodtest b/core/SConscript.prodtest index d245a544c8..8c5e45170f 100644 --- a/core/SConscript.prodtest +++ b/core/SConscript.prodtest @@ -9,7 +9,7 @@ PRODUCTION = ARGUMENTS.get('PRODUCTION', '0') == '1' BOOTLOADER_DEVEL = ARGUMENTS.get('BOOTLOADER_DEVEL', '0') == '1' HW_REVISION = ARGUMENTS.get('HW_REVISION', None) -FEATURES_WANTED = ["input", "sbu", "sd_card", "rgb_led", "usb", "consumption_mask", "optiga", "haptic", "ble", "tropic"] +FEATURES_WANTED = ["input", "sbu", "nfc", "sd_card", "rgb_led", "usb", "consumption_mask", "optiga", "haptic", "ble", "tropic"] CCFLAGS_MOD = '' CPPPATH_MOD = [] diff --git a/core/embed/io/nfc/inc/io/nfc.h b/core/embed/io/nfc/inc/io/nfc.h new file mode 100644 index 0000000000..34aaf2d3f0 --- /dev/null +++ b/core/embed/io/nfc/inc/io/nfc.h @@ -0,0 +1,40 @@ +/* + * This file is part of the Trezor project, https://trezor.io/ + * + * Copyright (c) SatoshiLabs + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef TREZORHAL_NFC_H +#define TREZORHAL_NFC_H + +#include +#include + +typedef enum { + NFC_OK, + NFC_ERROR, + NFC_NOT_INITIALIZED, + NFC_SPI_BUS_ERROR, + NFC_INITIALIZATION_FAILED, +} nfc_status_t; + +nfc_status_t nfc_init(); + +nfc_status_t nfc_deinit(); + +void nfc_poll_type_A(); + +#endif // TREZORHAL_NFC_H diff --git a/core/embed/io/nfc/rfal/doc/Release_Notes.html b/core/embed/io/nfc/rfal/doc/Release_Notes.html new file mode 100644 index 0000000000..607e52711f --- /dev/null +++ b/core/embed/io/nfc/rfal/doc/Release_Notes.html @@ -0,0 +1,458 @@ + + + + + + + + + + + + + Release Notes for RFAL Library + + + + + + + + + +
+


+

+
+ + + + + + +
+ + + + + + + + + +

+
+

Release +Notes for RFAL software Library

+

Copyright +2019 STMicroelectronics

+

+
+

 

+ + + + + + +

+
The RFAL Library +(RF Abstraction Layer) provides several functionalities required to perform RF/NFC communications. +The RFAL encapsulates the different RF ICs (ST25R3911, ST25R3916, ST25R200, ST25R95 and future ST25R devices) into a common and easy to use interface.
+
+ The technologies currently supported by RFAL are: +
    +
  • NFC-A \ ISO14443A (T1T, T2T, T4TA)
  • +
  • NFC-B \ ISO14443B (T4TB)
  • +
  • NFC-F \ FeliCa (T3T)
  • +
  • NFC-V \ ISO15693 (T5T)
  • +
  • P2P \ ISO18092 (NFCIP1, Passive-Active P2P)
  • +
  • ST25TB (ISO14443-2 Type B with Proprietary Protocol)
  • +
  • PicoPass \ iClass
  • +
  • B' \ Calypso
  • +
  • CTS \ CTM
  • +
+

+ The protocols provided by RFAL are: +
    +
  • ISO-DEP (ISO14443-4)
  • +
  • NFC-DEP (ISO18092)
  • +
+
+
+
    +
+ +

Update History

+
+ +

V3.0.1 / 12-Sep-2024

+

+

Main Changes

+
    +
  • Hotfix to address condition that prevents usage of WU default parameters on ST25R3916 (not ST25R3916B) if AAT enabled
  • +
+
+ +

V3.0.0 / 10-Jun-2024

+

+

Main Changes

+
    +
  • ST25R200/ST25R100 support added
  • +
  • Changed rfalDeinitialize procedure to ensure ST25R is set in PD mode
  • +
  • Added new interface to check whether Wake-up mode is enabled
  • +
  • Extended NFC/HL to allow to configure whether to poll ahead of going to wake-up mode
  • +
  • Improved ISO-DEP buffer handling during Listen mode activation
  • +
  • Improved RFAL NFC/HL layer handling during discovery, particularly for AP2P
  • +
  • DPO extended to allow adjustment without a communication mode previously set
  • +
  • Updated ST25R3916 to ensure MOSI state during read operation
  • +
  • Extended MISRA C / CERT C coverage to DPO and CD modules
  • +
  • Introduced WLC WPT API declarations
  • +
  • Several driver improvements and fixes
  • +
+
+ +

V2.10.0 / 16-Fev-2024

+

+

Main Changes

+
    +
  • Feature switches no longer disabled by omission. User configs at rfal_platform.h or by default by rfal_defConfig.h
  • +
  • Ensure ST25R3916B AWS is disabled during regulator adjustment
  • +
  • Extended WU mode to allow the reference measurement to be obtained from WU/PD mode (ST25R3911 and ST25R3916)
  • +
  • Extended NFC/HL to allow to configure whether to poll ahead of going to wake-up mode
  • +
  • Changed NFC/HL to only initialize Analog Configuration if not yet performed
  • +
  • Added new interface to check if Wake-up mode is enabled
  • +
  • Extended LED support for debug purposes
  • +
  • ISO-DEP extended to ensure NFC-B TR2 / FDT Poll
  • +
  • Updated DID handling in ISO-DEP
  • +
  • Added Listen mode modulation Get and Set interfaces
  • +
  • Introduced Listen mode External Field On callback
  • +
  • ST25TB UID integrity checked during Pcall16 anticollision
  • +
  • Several driver improvements and fixes
  • +
+
+ +

V2.8.0 / 14-Dez-2022

+

+

Main Changes

+
    +
  • Incorporated RFAL dependecies (utils, errno) into library to make library more self-contained
  • +
  • Prepended RFAL prefix into user configuration file platform.h --> rfal_platform.h (breaks compatibility with previous versions, see example in rfal.chm))
  • +
  • Introduced default configuration rfal_defConfig.h
  • +
  • Improved ISO-DEP Deselect sequence
  • +
  • Changed the RFAL NFC/HL layer to make use of non blocking ISO-DEP Deselect procedure
  • +
  • Several improvements and fixes on RFAL NFC/HL layer, including deactivation procedure and compliance mode used for ISO-DEP
  • +
  • Extended ISO15693 anticollsion APIs to allow larger receptions
  • +
  • Improved library error checking and portability
  • +
  • New APIs introduced in rfalChip interface
  • +
  • Extended rfalLowPowerModeStart to receive a mode to be set
  • +
  • Added RFAL Card Detection/Protection module
  • +
  • Several driver improvements and fixes
  • +
+
+ +

V2.6.0 / 19-Jan-2022

+

+

Main Changes

+
    +
  • Introduced support for ST25R3916B
  • +
  • Extended NFC technology modules with non-blocking APIs for card activation (Technology detection, Collision Resolution and Activation)
  • +
  • Extended NFC/HL module with option to bail out, deactivation types, and usage of new non-blocking APIs
  • +
  • Added new transceive flag for CRC check on Rx
  • +
  • Better aligned NFC-V special frame timings to NFC Forum Digital
  • +
  • Further improved compliance to CERT C
  • +
  • Corrected PAD length in SENSF_RES
  • +
  • Modified Field Detector to manual setting for RF Collision avoidance / Field On (ST25R3916) due to high temperature limitation
  • +
  • Changed default Field On mechanism from RF Collision avoidance to transmitter enable (ST25R3911) due to high temperature limitation
  • +
  • Several driver improvements and fixes
  • +
+
+ +

V2.4.0 / 14-Jun-2021

+

+

Main Changes

+
    +
  • Aligned to NFC Forum CR12
  • +
  • EMD error handling simplified/renamed
  • +
  • SW license disclaimer updated
  • +
  • Added ST25R device selection check
  • +
  • Improved SPI initialization sequence
  • +
  • Modified certain chip specific features/definitions
  • +
  • Added API for retrieving Wake-up status
  • +
  • Extended available parameters on NFC/HL module
  • +
  • Introduced support for proprietary NFC technology on NFC/HL module
  • +
  • Introduced Transceive start synchronization callback
  • +
  • Extended support for non-blocking activation
  • +
  • Extended ST25R3916 SW Tag Detection with fractional delta option
  • +
  • Improved compliance to CERT C
  • +
  • Added handling for ST25R3916 PPON2 timer limitation
  • +
  • Several driver improvements and fixes
  • +
+
+ +

V2.2.0 / 22-May-2020

+

+

Main Changes

+
    +
  • Better alignment to NFC Forum latest requirements (CR12)
  • +
  • Extended NFC-V module with non-addressed mode support and improved aticollision
  • +
  • Feature Switches changed to be not mandatory. Modules disabled by default
  • +
  • Aligned APIs on platform.h (breaks compatibility with previous versions, see example in rfal.chm)
  • +
  • Added API for release/deletion of timers
  • +
  • ST25R3916 default analog table modified to X-NUCLEO-NFC06A1 board
  • +
  • Improved AP2P operation
  • +
  • Fixed issues introduced on previous release linked to SFGT and anticollision retries
  • +
  • Introduced Low-Power mode
  • +
  • Several driver improvements
  • +
+
+ +

V2.1.2 / 27-Jan-2020

+

+

Main Changes

+
    +
  • Extended ISO-DEP and NFC-A module to support non-blocking activation interfaces
  • +
  • Extended NFC/HL module to make use of the new APIs further splitting the execution of the worker during the different activities
  • +
  • Modified NFC-A anticollision to strictly comply to NFC Forum DP. A separate proprietary method is now available.
  • +
  • NFC-V changed to use OOK (100% AM) by default
  • +
  • Fixed FWT used by NFC-V Sleep
  • +
  • Fixed NFC-F FDT Poll value
  • +
  • Fixed incorrect register access on ST25R3911B RFO Get/Set method
  • +
  • SPI driver modified to clear Rx buffer prior to operation
  • +
  • Added further code size optimizations based on enabled features
  • +
  • Updated ST25R3916 driver to DS Rev2
  • +
  • Updated SW Tag Detection as describded in AN Rev3
  • +
  • Several driver improvements
  • +
+
+ +

V2.1.0 / 30-Sep-2019

+

+

Main Changes

+
    +
  • Extended RFAL NFC Higher Layer for increased functionality and configurations
  • +
  • Several improvements on the ISO-DEP protocol layer
  • +
  • Protocol buffer sizes made fully configurable for increased memory management
  • +
  • Introduced option for Collision Avoidance with Automatic Gain Control
  • +
  • Several driver improvements
  • +
  • ST25R3916 overheat protection disabled
  • +
  • RF Transceive modified for transmission errors to precede other errors
  • +
  • Analog Configs extended to support different DPO power levels
  • +
+
+ +

V2.0.10 / 25-Jun-2019

+

+

Main Changes

+
    +
  • Various improvements on RFAL NFC Higher layer
  • +
  • Added alternative NFC-V anticollision method (non NFC Forum compliant)
  • +
  • Several minor improvements and fixes
  • +
+
+ +

V2.0.6 / 10-Apr-2019

+

+

Main Changes

+
    +
  • Several NFC-V interoperability improvements
  • +
  • Extended support for specific features of ST's ISO15693 Tags. New ST25Dx module added
  • +
  • Interrupt handling changed and further protection added
  • +
  • RFAL feature switches have been modified and features are now disabled if omitted
  • +
  • ST25R3916 AAT (Automatic Antenna Tunning) module added
  • +
  • RFAL NFC Higher layer added
  • +
  • Several driver improvements
  • +
+
+ +

V2.0.4 / 06-Fev-2019

+

Provided with ST25R3916 DISCO v1.0.0 / EMVCo v1.2.0

+

Main Changes

+
    +
  • Minor improvements on NFC-F module
  • +
  • Several improvements on NFC-V module including support for ST proprietary features
  • +
  • Fixed issue with Delta RWT calculation
  • +
  • Fixed incorrect usage of NFCB dTbPoll / DdFWT
  • +
  • Added compile switch for Listen Mode
  • +
  • Low power Listen Mode support added
  • +
  • Listen Mode aligned to NFC Forum Digital 2.1
  • +
  • Added handling for EMVCo 3.0 static FDTListen
  • +
  • Introduced SW Tag Detection
  • +
+
+ +

V2.0.2 / 31-Oct-2018

+

Provided with ST25R3916 DISCO v0.9.4 (binary only)

+

Main Changes

+
    +
  • New T4T module added
  • +
  • Added support for T3T Check and Update commands
  • +
  • Improved NFC-V module and added Write Multiple Blocks support
  • +
  • New rfalWorker protection added for improved control in multi-thread environments
  • +
  • Added support for user defined Analog Config tables
  • +
  • Several driver improvements and protections added
  • +
+
+ +

V2.0.0 / 28-Aug-2018

+ +

Main Changes

+
    +
  • MISRA C 2012 compliant
  • +
  • ST25R3916 support added
  • +
  • ST25R95 support added
  • +
  • Fix unwanted Field Detector disable when entering Wake-up mode
  • +
  • Extended Analog Config to have specific events
  • +
  • Fixed NFC-DEP potential issue if DID used
  • +
  • Extended NFC-V commands
  • +
  • T2T module added
  • +
  • Improved initial Listen mode handling
  • +
  • Extended Wake-Up mode to support Capacitive measurement
  • +
+
+ +

V1.3.6 / 08-May-2018

+

Provided with ST25R3911B DISCO v1.2.0

+

Main Changes

+
    +
  • Added ISO15693 x4 and x8 mode support
  • +
  • Added S(PARAMETERS) support
  • +
  • Interface changes for measurement, Wake-Up and DPO methods
  • +
  • Added further feature switches to enable/disable individual modules
  • +
  • Changed communication protection
  • +
  • Improved NFC-A anti-collision
  • +
  • Several driver improvements
  • +
+
+

V1.3.4 / 07-May-2018

+ +

Main Changes

+
    +
  • Fixed NFC-V Read operation in addressed mode
  • +
+
+

V1.3.2 / 31-January-2018

+ +

Main Changes

+
    +
  • Modified Wake-Up mode interface
  • +
  • Fixed SFGI calculation in ISO-DEP
  • +
+
+

V1.3.0 / 22-January-2018

+ +

Main Changes

+
    +
  • Introduced a new IRQ status handling to read the registers only once
  • +
  • Several changes for supporting Linux platform
  • +
  • SPI Select/Deselect moved to platform.h
  • +
  • Aditional protection of the IRQ status reading, new macros available: platformProtectST25R391xIrqStatus / platformUnprotectST25R391xIrqStatus
  • +
  • Renamed the IRQ Enable/Disable macros to platformProtectST25R391xComm / platformUnprotectST25R391xComm
  • +
  • Renamed SPI pins from chip specific to ST25R391X
  • +
  • Introduced a new option ST25R391X_COM_SINGLETXRX which executes SPI in one single exchange (additional buffer required)
  • +
  • Updated and added errata handlings to latest ST25R3911 Errata version
  • +
  • Fixed inconsitency on Analog settings for NFC-V
  • +
  • Fixed issue on NFC-V 1of256 decoding
  • +
  • Changed the default NFC-A FDT Listen to be more strict
  • +
  • Added Wake-Up mode support
  • +
  • Added RFAL version definition
  • +
+
+

V1.2.0 / 17-August-2017

+

Provided with ST25R3911B Disco v1.1.16

+

Main Changes

+
    +
  • Aligned Technology modules to NFC Activity v1.1 and EMVCo v2.6
  • +
  • Extended NFC-B Collision Resolution allowing user define Slots
  • +
  • Added feature switches to enable/disable individual modules
  • +
  • ISO-DEP Interface changes allowing more user configurations and further EMVCo alignment
  • +
  • Changed ST25TB detection to always perform Anti Collision loop regardeless of the result of the Poll
  • +
  • Fixed FIFO WL handling
  • +
  • Modified FDT Poll handling
  • +
  • changed rfalCalibrate() to not overwrite dynamic configs
  • +
  • Added adjustment for TR1PUTMIN
  • +
+ +
+

V1.1.0 / 30-June-2017

+

Provided with ST25R3911B Disco v1.1.12

+

Main Changes

+
    +
  • EMD supression enabled for ST25R3911B
  • +
+ +
+

V1.0.0 / 16-May-2017

+

Provided with X-NUCLEO-NFC05A1 v1.0.0

+

Main Changes

+
    +
  • Added support for B', CTS and PicoPass/iClass mode
  • +
  • Several impromvements for NFC-V mode
  • +
  • Improved error detection during NFC-B collision resolution
  • +
  • Extended T1T module
  • +
+ +
+

V0.9.0 / 02-March-2017

+

Provided with ST25R3911B Discovery Kit on Embedded World Conference (binary only)

+

Main Changes

+ +
+ +
+

+
+
+

 

+
+ diff --git a/core/embed/io/nfc/rfal/doc/ST25R3916_CERTComplianceReport.html b/core/embed/io/nfc/rfal/doc/ST25R3916_CERTComplianceReport.html new file mode 100644 index 0000000000..0f9e126028 --- /dev/null +++ b/core/embed/io/nfc/rfal/doc/ST25R3916_CERTComplianceReport.html @@ -0,0 +1,11568 @@ + + + + + +Helix QAC GEP/GCS/GRP Report + + + + +
+
+
+
+
+ +This report documents the coverage of CERT conformance. Its notation is derived from MISRA, please refer for details of the following reports (GEP, GRP, GCS, etc.) to this standard.
+This section targets to provide an overview of Guidelines Enforcement Plan (GEP).
+This document will only focus on STMicroelectronics NFC RF Abstraction Layer (RFAL).
+The project has been designed to comply with the standard ISO/IEC 9899:1999 ([C99]). +
+
+

1. Tools version

+
+ + + + + + + + + + + + + + + + + +
ComponentVersionTargetOptions
certccm1.5.0C
rcma2.3.0C_CPP
qac9.9.0C
    -d : __schedule_barrier=_ignore_semi
+
    -namelength : 63
+
    -prodoption : df::function_timeout=20000
+
+

2. Configuration

+This section targets to provide the main configuration options used for CERT compliance.
+The project complies to [C99],
+the variables length has been consequently set to a dedicated value (cf 'namelength' option in table above). +
+
+Repository/components:
+
    +
  • MCU target:
  • +
      RFAL-ST25R3916

    +
  • RFAL informations:
  • +
      Path: rfal
    +
      Version: v3.0.0
    +
  • Project repositories SHA1:
  • +
      common: bd63699
    +
      nucleo: afd8cc1
    +
      rfal: 5b33cb9
    +
    +
+

3. Assistance/Enforcement

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
GuidelineCategoryDescriptionAssistance/Enforcement Sub Rules
ARR02RecommendationExplicitly specify array bounds, even if implicitly defined by an initializer
+ + + + + + + + + + + + + + + + + +
QacDescription
qac-0678[u] Array element is array of unknown size. Arrays cannot be constructed from incomplete types.
qac-0688Array size determined by number of initializers which include concatenated string literals.
qac-3674Array size defined implicitly by the number of initializers.
qac-3684Array declared with unknown size.
+
ARR30RuleDo not form or use out-of-bounds pointers or array subscripts
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-2820Constant: Arithmetic operation on NULL pointer.
qac-2821Definite: Arithmetic operation on NULL pointer.
qac-2822Apparent: Arithmetic operation on NULL pointer.
qac-2823Suspicious: Arithmetic operation on NULL pointer.
qac-2840Constant: Dereference of an invalid pointer value.
qac-2841Definite: Dereference of an invalid pointer value.
qac-2842Apparent: Dereference of an invalid pointer value.
qac-2843Suspicious: Dereference of an invalid pointer value.
qac-2930Constant: Computing an invalid pointer value.
qac-2931Definite: Computing an invalid pointer value.
qac-2932Apparent: Computing an invalid pointer value.
qac-2933Suspicious: Computing an invalid pointer value.
qac-2935Constant: Dereference of an invalid char pointer value.
qac-2936Definite: Dereference of an invalid char pointer value.
qac-2937Apparent: Dereference of an invalid char pointer value.
qac-2938Suspicious: Dereference of an invalid char pointer value.
qac-2950Constant: Negative value used in array subscript or pointer arithmetic operation.
qac-2951Definite: Negative value used in array subscript or pointer arithmetic operation.
qac-2952Apparent: Negative value used in array subscript or pointer arithmetic operation.
qac-2953Suspicious: Negative value used in array subscript or pointer arithmetic operation.
+
ARR32RuleEnsure size arguments for variable length arrays are in a valid range
+ + + + + +
QacDescription
qac-1051[C99] A variable length array has been declared.
+
ARR36RuleDo not subtract or compare two pointers that do not refer to the same array
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-0487[C] If two pointers are subtracted, they must be pointers that address compatible types.
qac-0513[C] Relational operator used to compare pointers to incompatible types.
qac-2668Subtraction of a pointer to an array and a pointer to a non-array.
qac-2669Comparison of a pointer to an array and a pointer to a non-array.
qac-2761Definite: Subtracting pointers that address different objects.
qac-2762Apparent: Subtracting pointers that address different objects.
qac-2763Suspicious: Subtracting pointers that address different objects.
qac-2766Definite: Subtracting pointers that address different members of the same object.
qac-2767Apparent: Subtracting pointers that address different members of the same object.
qac-2768Suspicious: Subtracting pointers that address different members of the same object.
qac-2771Definite: Comparing pointers that address different objects.
qac-2772Apparent: Comparing pointers that address different objects.
qac-2773Suspicious: Comparing pointers that address different objects.
+
ARR37RuleDo not add or subtract an integer to a pointer to a non-array object
+ + + + + + + + + + + + + + + + + +
QacDescription
qac-2930Constant: Computing an invalid pointer value.
qac-2931Definite: Computing an invalid pointer value.
qac-2932Apparent: Computing an invalid pointer value.
qac-2933Suspicious: Computing an invalid pointer value.
+
ARR38RuleGuarantee that library functions do not form invalid pointers
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-2840Constant: Dereference of an invalid pointer value.
qac-2841Definite: Dereference of an invalid pointer value.
qac-2842Apparent: Dereference of an invalid pointer value.
qac-2843Suspicious: Dereference of an invalid pointer value.
qac-2845Constant: Maximum number of characters to be read/written is larger than the target buffer size.
qac-2846Definite: Maximum number of characters to be read/written is larger than the target buffer size.
qac-2847Apparent: Maximum number of characters to be read/written is larger than the target buffer size.
qac-2848Suspicious: Maximum number of characters to be read/written is larger than the target buffer size.
qac-2935Constant: Dereference of an invalid char pointer value.
qac-2936Definite: Dereference of an invalid char pointer value.
qac-2937Apparent: Dereference of an invalid char pointer value.
qac-2938Suspicious: Dereference of an invalid char pointer value.
qac-4880Constant: Pointed to object has smaller size than the size_t argument.
qac-4881Definite: Pointed to object has smaller size than the size_t argument.
qac-4882Apparent: Pointed to object has smaller size than the size_t argument.
qac-4883Suspicious: Pointed to object has smaller size than the size_t argument.
+
ARR39RuleDo not add or subtract a scaled integer to a pointer
+ + + + + + + + + + + + + +
QacDescription
qac-4955Constant: Adding/subtracting a scaled integer to a pointer.
qac-4956Definite: Adding/subtracting a scaled integer to a pointer.
qac-4957Apparent: Adding/subtracting a scaled integer to a pointer.
+
CON30RuleClean up thread-specific storage
+ + + + + + + + + + + + + + + + + + + + + +
QacDescription
mta-1780Memory associated with the TLS key '%1s' is deallocated more than once.
mta-1781Memory associated with the TLS key '%1s' may be deallocated more than once.
mta-1782Memory associated with the TLS key '%1s' is deallocated in a way that does not match its allocation.
mta-1783Memory associated with the TLS key '%1s' is not deallocated.
mta-1784The origin of the TLS key '%1s' cannot be identified. Perhaps it should have been passed as a thread parameter?
+
CON31RuleDo not destroy a mutex while it is locked
+ + + + + + + + + +
QacDescription
qac-4961Definite: Attempt to destroy a mutex which is still locked
qac-4962Apparent: Attempt to destroy a mutex which is still locked
+
CON32RulePrevent data races when accessing bit-fields from multiple threads
+ + + + + + + + + +
QacDescription
mta-1774Definite: data race for an object '%1s' which shares its physical storage location with one or more others.
mta-1775Apparent: data race for an object '%1s' which shares its physical storage location with one or more others.
+
CON33RuleAvoid race conditions when using library functions
+ + + + + + + + + + + + + +
QacDescription
qac-4976Definite: Call to a non-reentrant function outside of a critical section.
qac-4977Apparent: Call to a non-reentrant function outside of a critical section.
certccm-5037Use of '%1s'.
+
CON34RuleDeclare objects shared between threads with appropriate storage durations
+ + + + + + + + + + + + + +
QacDescription
qac-4926Definite: The lifetime of the variable passed to the thread creation function is shorter than the lifetime of the thread.
qac-4927Apparent: The lifetime of the variable passed to the thread creation function is shorter than the lifetime of the thread.
qac-4928Suspicious: The lifetime of the variable passed to the thread creation function is shorter than the lifetime of the thread.
+
CON35RuleAvoid deadlock by locking in a predefined order
+ + + + + + + + + +
QacDescription
mta-1772Mutex '%1s' violates the lock hierarchy, as it is acquired before '%2s' elsewhere %3s% of the time.
mta-1773Mutexes '%1s' and '%2s' are not ordered in the lock hierarchy.
+
CON36RuleWrap functions that can spuriously wake up in a loop
+ + + + + +
QacDescription
qac-2027This function can wake spuriously, but does not appear to be wrapped in a loop.
+
CON37RuleDo not call signal() in a multithreaded program
+ + + + + +
QacDescription
certccm-5021'%s' is being used.
+
CON38RulePreserve thread safety and liveness when using condition variables
+ + + + + + + + + +
QacDescription
mta-1778Condition variable '%1s' is signaled and used with multiple wait operations.
mta-1779Condition variable '%1s' is signaled and used with a re-entered wait operation.
+
CON39RuleDo not join or detach a thread that was previously joined or detached
+ + + + + +
QacDescription
mta-1776Thread '%1s' is made unjoinable multiple times.
+
CON40RuleDo not refer to an atomic variable twice in an expression
+ + + + + + + + + + + + + +
QacDescription
qac-1114This atomic variable is referenced directly twice in the same expression.
qac-1115This atomic lvalue appears to be referenced twice in the same expression.
qac-1116This atomic variable appears to be updated non-atomically.
+
CON41RuleWrap functions that can fail spuriously in a loop
+ + + + + +
QacDescription
qac-2026This function can fail spuriously, but does not appear to be wrapped in a loop.
+
CON43RuleDo not allow data races in multithreaded code
+ + + + + + + + + + + + + + + + + +
QacDescription
mta-1765Definite: data race for object '%1s'.
mta-1766Apparent: data race for object '%1s'.
mta-1770Definite: data race for a volatile object '%1s'.
mta-1771Definite: data race for a mutable object '%1s'.
+
DCL00RecommendationConst-qualify immutable objects
+ + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-3204The variable '%s' is only set once and so it could be declared with the 'const' qualifier.
qac-3227The parameter '%s' is never modified and so it could be declared with the 'const' qualifier.
qac-3232File scope static, '%s', is never modified. It could be declared const.
qac-3673The object addressed by the pointer parameter '%1s' is not modified and so the pointer could be of type 'pointer to const'.
qac-3677The elements of the array parameter '%1s' are not modified and so they could be qualified as 'const'.
+
DCL01RecommendationDo not reuse variable names in subscopes
+ + + + + + + + + + + + + + + + + +
QacDescription
qac-0795Identifier matches other identifier(s) (e.g. '%1s') in an outer scope within the specified number of significant characters.
qac-0796Identifier matches other identifier(s) (e.g. '%1s') in an outer scope within the ISO:C99 limit of 63 significant characters
qac-2547This declaration of tag '%s' hides a more global declaration.
qac-3334This declaration of '%1s' hides a more global declaration.
+
DCL05RecommendationUse typedefs of non-pointer types only
+ + + + + +
QacDescription
certccm-5004This typedef is applied to a pointer type.
+
DCL06RecommendationUse meaningful symbolic constants to represent literal values
+ + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-3120Hard-coded 'magic' integer constant, '%1s'.
qac-3121Hard-coded 'magic' floating constant, '%1s'.
qac-3122Hard-coded 'magic' string literal, %1s.
qac-3123Hard coded 'magic' character constant, %1s.
qac-3131Hard coded 'magic' number, '%1s', used to define the size of a bit-field.
qac-3132Hard coded 'magic' number, '%1s', used to define the size of an array.
+
DCL07RecommendationInclude the appropriate type information in function declarators
+ + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-1304Old style definition of function '%1s()' is not portable to C++.
qac-2050The 'int' type specifier has been omitted from a function declaration.
qac-3331The definition for identifier '%s' with external linkage conflicts with a previous declaration in the same scope.
qac-3335No function declaration. Implicit declaration inserted: 'extern int %s();'.
qac-3408'%1s' has external linkage and is being defined without any previous declaration.
qac-3450Function '%s', with internal linkage, is being defined without a previous declaration.
+
DCL10RecommendationMaintain the contract between the writer and caller of variadic functions
+ + + + + + + + + +
QacDescription
qac-0184[U] Insufficient arguments to satisfy conversion specifier, number %1s.
qac-0185[U] Call contains more arguments than conversion specifiers.
+
DCL11RecommendationUnderstand the type issues associated with variadic functions
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-0179[U] Argument type does not match conversion specifier number %1s.
qac-0184[U] Insufficient arguments to satisfy conversion specifier, number %1s.
qac-0185[U] Call contains more arguments than conversion specifiers.
qac-0186[U] A call to this function must include at least one argument.
qac-0190[U] Using unsupported conversion specifier number %1s.
qac-0191[U] Unknown length modifier used with 'd/i/n' conversion specifier, number %1s.
qac-0192[U] Unknown length modifier used with 'o' conversion specifier, number %1s.
qac-0193[U] Unknown length modifier used with 'u' conversion specifier, number %1s.
qac-0194[U] Unknown length modifier used with 'x/X' conversion specifier, number %1s.
qac-0195[U] Unknown length modifier used with 'e/E/f/F/g/G' conversion specifier, number %1s.
qac-0196[U] Unknown length modifier used with 's' conversion specifier, number %1s.
qac-0197[U] Unknown length modifier used with 'p' conversion specifier, number %1s.
qac-0198[U] Unknown length modifier used with '%%' conversion specifier, number %1s.
qac-0199[U] Unknown length modifier used with '[' conversion specifier, number %1s.
qac-0200[U] Unknown length modifier used with 'c' conversion specifier, number %1s.
qac-0201[U] Incomplete conversion specifier, number %1s.
qac-0206[U] Argument type does not match conversion specifier number %1s.
qac-0207[U] 'scanf' expects address of objects being stored into.
qac-0208[U] Same character occurs in scanset more than once.
+
DCL13RecommendationDeclare function parameters that are pointers to values not changed by the function as const
+ + + + + + + + + + + + + +
QacDescription
qac-0431[C] Function argument points to a more heavily qualified type.
qac-3673The object addressed by the pointer parameter '%1s' is not modified and so the pointer could be of type 'pointer to const'.
qac-3677The elements of the array parameter '%1s' are not modified and so they could be qualified as 'const'.
+
DCL15RecommendationDeclare file-scope objects or functions that do not need external linkage as static
+ + + + + + + + + +
QacDescription
rcma-1504The object '%1s' is only referenced in the translation unit where it is defined.
rcma-1531The object '%1s' is referenced in only one translation unit - but not the one in which it is defined.
+
DCL16RecommendationUse 'L', not 'l', to indicate a long value
+ + + + + +
QacDescription
qac-1280A lowercase letter L (l) has been used in an integer or floating suffix.
+
DCL18RecommendationDo not begin integer constants with 0 when specifying a decimal value
+ + + + + + + + + +
QacDescription
qac-0339Octal constant used.
qac-1272Redundant leading zeroes on a numeric constant.
+
DCL19RecommendationMinimize the scope of variables and functions
+ + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
rcma-1504The object '%1s' is only referenced in the translation unit where it is defined.
rcma-1505The function '%1s' is only referenced in the translation unit where it is defined.
rcma-1531The object '%1s' is referenced in only one translation unit - but not the one in which it is defined.
rcma-1532The function '%1s' is only referenced in one translation unit - but not the one in which it is defined.
qac-3210The global identifier '%1s' is declared here but is not used in this translation unit.
qac-3218File scope static, '%1s', is only accessed in one function.
+
DCL20RecommendationExplicitly specify void when a function accepts no arguments
+ + + + + + + + + +
QacDescription
qac-3001Function has been declared with an empty parameter list.
qac-3007"void" has been omitted when defining a function with no parameters.
+
DCL21RecommendationUnderstand the storage of compound literals
+ + + + + + + + + +
QacDescription
qac-1054[C99] A compound literal has been used.
qac-3217Address of automatic object exported to a pointer with linkage or wider scope.
+
DCL23RecommendationGuarantee that mutually visible identifiers are unique
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-0627[C] '%s' has different type to previous declaration in the same scope.
qac-0776[L] External identifier matches other external identifier(s) (e.g. '%1s') in first 6 characters - program does not conform strictly to ISO:C90.
qac-0777[U] External identifier does not differ from other identifier(s) (e.g. '%1s') within the specified number of significant characters.
qac-0778[L] Identifier matches other identifier(s) (e.g. '%1s') in first 31 characters - program does not conform strictly to ISO:C90.
qac-0779[U] Identifier does not differ from other identifier(s) (e.g. '%1s') within the specified number of significant characters.
qac-0789[L] Identifier matches other identifier(s) (e.g. '%1s') in first 63 characters - program does not conform strictly to ISO:C99.
qac-0791[U] Macro identifier does not differ from other macro identifier(s) (e.g. '%1s') within the specified number of significant characters.
qac-0793[L] Macro identifier matches other macro identifier(s) (e.g. '%1s') in first 63 characters - program does not conform strictly to ISO:C99.
+
DCL30RuleDeclare objects with appropriate storage durations
+ + + + + + + + + + + + + + + + + +
QacDescription
qac-3217Address of automatic object exported to a pointer with linkage or wider scope.
qac-3225Address of automatic object exported using a function parameter.
qac-3230Address of automatic object assigned to local pointer with static storage duration.
qac-4140Address of automatic object exported in function return value.
+
DCL31RuleDeclare identifiers before using them
+ + + + + + + + + + + + + + + + + +
QacDescription
qac-0434[S] The identifier '%s' has not been declared.
qac-2050The 'int' type specifier has been omitted from a function declaration.
qac-2051The 'int' type specifier has been omitted from an object declaration.
qac-3335No function declaration. Implicit declaration inserted: 'extern int %s();'.
+
DCL36RuleDo not declare an identifier with conflicting linkage classifications
+ + + + + +
QacDescription
qac-0625[U] '%s' has been declared with both internal and external linkage - the behaviour is undefined.
+
DCL37RuleDo not declare or define a reserved identifier
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-0602[U] The identifier '%s' is reserved for use by the library.
qac-0603[U] The macro identifier '%s' is reserved.
qac-4600The macro '%1s' is also defined in '<%2s>'.
qac-4601The macro '%1s' is the name of an identifier in '<%2s>'.
qac-4602The identifier '%1s' is declared as a macro in '<%2s>'.
qac-4603The object/function '%1s'is being defined with the same name as an ordinary identifier defined in '<%2s>'.
qac-4604The object/function '%1s' is being declared with the same name as an ordinary identifier defined in '<%2s>'.
qac-4605The typedef '%1s' is also defined in '<%2s>'.
qac-4606The typedef '%1s' has the same name as another ordinary identifier in '<%2s>'.
qac-4607The enum constant '%1s' has the same name as another ordinary identifier in '<%2s>'.
qac-4608The tag '%1s' is also defined in '<%2s>'.
qac-4620The macro '%1s' may also be defined as a macro in '<%2s>'.
qac-4621The macro '%1s' may also be defined as a typedef in '<%2s>'.
qac-4622The identifier '%1s' may be defined as a macro in '<%2s>'.
qac-4623The typedef '%1s' may also be defined in '<%2s>'.
qac-4624The ordinary identifier '%1s' may be defined as a typedef in '<%2s>'.
qac-4640The macro '%1s' could conflict in the future with the name of a macro in '<%2s>'.
qac-4641The identifier '%1s' could conflict in the future with the name of a macro in '<%2s>'.
qac-4642The macro '%1s' could conflict in the future with the name of a function in '<%2s>'.
qac-4643The identifier '%1s' could conflict in the future with the name of a function in '<%2s>'.
qac-4644The macro '%1s' could conflict in the future with the name of a typedef in '<%2s>'.
qac-4645The identifier '%1s' could conflict in the future with the name of a typedef in '<%2s>'.
+
DCL38RuleUse the correct syntax when declaring a flexible array member
+ + + + + + + + + +
QacDescription
qac-1037[E] Arrays of length zero are a language extension.
qac-1039[E] Treating array of length one as potentially flexible member.
+
DCL39RuleAvoid information leakage when passing a structure across a trust boundary
+ + + + + + + + + + + + + +
QacDescription
qac-4941Definite: Passing a padded structure from a trusted boundary to another domain.
qac-4942Apparent: Passing a padded structure from a trusted boundary to another domain.
qac-4943Suspicious: Passing a padded structure from a trusted boundary to another domain.
+
DCL40RuleDo not create incompatible declarations of the same function or object
+ + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-0776[L] External identifier matches other external identifier(s) (e.g. '%1s') in first 6 characters - program does not conform strictly to ISO:C90.
qac-0778[L] Identifier matches other identifier(s) (e.g. '%1s') in first 31 characters - program does not conform strictly to ISO:C90.
qac-0779[U] Identifier does not differ from other identifier(s) (e.g. '%1s') within the specified number of significant characters.
qac-0789[L] Identifier matches other identifier(s) (e.g. '%1s') in first 63 characters - program does not conform strictly to ISO:C99.
rcma-1510'%1s' has external linkage and has incompatible declarations.
+
DCL41RuleDo not declare variables inside a switch statement before the first case label
+ + + + + + + + + + + + + +
QacDescription
qac-2008Code statements precede the first label in this 'switch' construct.
qac-2882This 'switch' statement will bypass the initialization of local variables.
qac-3234Declarations precede the first label in this 'switch' construct.
+
ENV03RecommendationSanitize the environment when invoking external programs
+ + + + + +
QacDescription
certccm-5017'%s' is being used.
+
ENV30RuleDo not modify the object referenced by the return value of certain functions
+ + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-1492The result of library function '%1s' is used to modify the referenced object.
qac-1493The result of library function '%1s' is used as a pointer to a modifiable object.
qac-1494The result of library function '%1s' might be modified.
qac-4751Definite: Modifying an object that is referenced by the return value of a library function.
qac-4752Apparent: Modifying an object that is referenced by the return value of a library function.
qac-4753Suspicious: Modifying an object that is referenced by the return value of a library function.
+
ENV31RuleDo not rely on an environment pointer following an operation that may invalidate it
+ + + + + + + + + + + + + +
QacDescription
qac-4991Definite: Using an invalidated value '%s' of the environment pointer passed as the third argument to main.
qac-4992Apparent: Using an invalidated value '%s' of the environment pointer passed as the third argument to main.
qac-4993Suspicious: Using an invalidated value '%s' of the environment pointer passed as the third argument to main.
+
ENV32RuleAll exit handlers must return normally
+ + + + + + + + + + + + + +
QacDescription
qac-4856Definite: Atexit handler does not return normally.
qac-4857Apparent: Atexit handler does not return normally.
qac-4858Suspicious: Atexit handler does not return normally.
+
ENV33RuleDo not call system()
+ + + + + +
QacDescription
certccm-5018'%s' is being used.
+
ENV34RuleDo not store pointers returned by certain functions
+ + + + + + + + + + + + + +
QacDescription
qac-2681Definite: Using an invalidated value '%s' returned from a Standard Library function.
qac-2682Apparent: Using an invalidated value '%s' returned from a Standard Library function.
qac-2683Suspicious: Using an invalidated value '%s' returned from a Standard Library function.
+
ERR30RuleSet errno to zero before calling a library function known to set errno, and check errno only after the function returns a value indicating failure
+ + + + + + + + + + + + + + + + + +
QacDescription
qac-2500Call to '%s' is not immediately preceded by the zero-ing of 'errno'.
qac-2501Call to '%s' is not immediately followed by the testing of 'errno'.
qac-2502Zero-ing of 'errno' is not immediately followed by a call to an 'errno' setting function.
qac-2503Testing of 'errno' is not immediately preceded by a call to an 'errno' setting function.
+
ERR32RuleDo not rely on indeterminate values of errno
+ + + + + + + + + + + + + + + + + +
QacDescription
qac-2031Signal handler '%1s' accesses errno.
qac-4781Definite: Signal handler causes the value of errno to be indeterminate.
qac-4782Apparent: Signal handler causes the value of errno to be indeterminate.
qac-4783Suspicious: Signal handler causes the value of errno to be indeterminate.
+
ERR33RuleDetect and handle standard library errors
+ + + + + +
QacDescription
qac-3200'%s' returns a value which is not being used.
+
ERR34RuleDetect errors when converting a string to a number
+ + + + + +
QacDescription
certccm-5030'%s' is being used.
+
EXP00RecommendationUse parentheses for precedence of operation
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-3389Extra parentheses recommended to clarify the ordering of a % operator and another arithmetic operator (* / % + -).
qac-3390Expression is equivalent to: "%s"
qac-3391Extra parentheses recommended. A conditional operation is the operand of another conditional operator.
qac-3392Extra parentheses recommended. A shift, relational or equality operation is the operand of a second identical operator.
qac-3393Extra parentheses recommended. An arithmetic operation (* / + -) is the operand of a different operator with the same precedence.
qac-3394Extra parentheses recommended. A shift, relational or equality operation is the operand of a different operator with the same precedence.
qac-3395Extra parentheses recommended. A * or / operation is the operand of a + or - operator.
qac-3396Extra parentheses recommended. A binary operation is the operand of a conditional operator.
qac-3397Extra parentheses recommended. A binary operation is the operand of a binary operator with different precedence.
qac-3398Extra parentheses recommended. A function call, array subscript, or member operation is the operand of a logical && or ||.
qac-3399Extra parentheses recommended. A unary operation is the operand of a logical && or ||.
qac-3400Extra parentheses recommended. A binary operation is the operand of a logical && or ||.
+
EXP02RecommendationBe aware of the short-circuit behavior of the logical AND and OR operators
+ + + + + +
QacDescription
qac-3415Right hand operand of '&&' or '||' is an expression with persistent side effects.
+
EXP03RecommendationDo not assume the size of a structure is the sum of the sizes of its members
+ + + + + +
QacDescription
qac-0697The size of the allocated memory block is not an integral multiple of the size of the object type addressed by the pointer cast.
+
EXP05RecommendationDo not cast away a const qualification
+ + + + + + + + + +
QacDescription
qac-0311Dangerous pointer cast results in loss of const qualification.
qac-0431[C] Function argument points to a more heavily qualified type.
+
EXP07RecommendationDo not diminish the benefits of constants by assuming their values in expressions
+ + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-3120Hard-coded 'magic' integer constant, '%1s'.
qac-3121Hard-coded 'magic' floating constant, '%1s'.
qac-3122Hard-coded 'magic' string literal, %1s.
qac-3123Hard coded 'magic' character constant, %1s.
qac-3131Hard coded 'magic' number, '%1s', used to define the size of a bit-field.
qac-3132Hard coded 'magic' number, '%1s', used to define the size of an array.
+
EXP08RecommendationEnsure pointer arithmetic is used correctly
+ + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-0488Performing pointer arithmetic.
qac-2930Constant: Computing an invalid pointer value.
qac-2931Definite: Computing an invalid pointer value.
qac-2932Apparent: Computing an invalid pointer value.
qac-2933Suspicious: Computing an invalid pointer value.
+
EXP10RecommendationDo not depend on the order of evaluation of subexpressions or the order in which side effects take place
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-0400[U] '%1s' is modified more than once between sequence points - evaluation order unspecified.
qac-0401[U] '%1s' may be modified more than once between sequence points - evaluation order unspecified.
qac-0402[U] '%1s' is modified and accessed between sequence points - evaluation order unspecified.
qac-0403[U] '%1s' may be modified and accessed between sequence points - evaluation order unspecified.
qac-0404[U] More than one read access to volatile objects between sequence points.
qac-0405[U] More than one modification of volatile objects between sequence points.
qac-3226The result of an assignment is being used in an arithmetic operation or another assigning operation.
qac-3326The result of an assignment is being used in a logical operation.
+
EXP11RecommendationDo not make assumptions regarding the layout of structures with bit-fields
+ + + + + + + + + +
QacDescription
qac-0310Casting to different object pointer type.
qac-0751Casting to char pointer type.
+
EXP12RecommendationDo not ignore values returned by functions
+ + + + + +
QacDescription
qac-3200'%s' returns a value which is not being used.
+
EXP13RecommendationTreat relational and equality operators as if they were nonassociative
+ + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-3392Extra parentheses recommended. A shift, relational or equality operation is the operand of a second identical operator.
qac-3401Possible precedence confusion: extra parentheses are recommended here.
qac-4111Right hand operand of relational operator is a 'Boolean' expression.
qac-4112Left hand operand of relational operator is a 'Boolean' expression.
qac-4113Both operands of relational operator are 'Boolean' expressions.
+
EXP15RecommendationDo not place a semicolon on the same line as an if, for, or while statement
+ + + + + +
QacDescription
qac-3109Null statement follows other code on the same line.
+
EXP16RecommendationDo not compare function pointers to constant values
+ + + + + + + + + + + + + +
QacDescription
qac-0428Function identifier is not followed by () but a function call may be intended.
qac-3004This integral constant expression is being interpreted as a null pointer constant.
qac-3344Controlling expression is not an 'essentially Boolean' expression.
+
EXP19RecommendationUse braces for the body of an if, for, or while statement
+ + + + + +
QacDescription
qac-2212Body of control statement is not enclosed within braces.
+
EXP20RecommendationPerform explicit tests to determine success, true and false, and equality
+ + + + + + + + + +
QacDescription
qac-3344Controlling expression is not an 'essentially Boolean' expression.
qac-4116Operand of logical ! operator is not an 'essentially Boolean' expression.
+
EXP30RuleDo not depend on the order of evaluation for side effects
+ + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-0400[U] '%1s' is modified more than once between sequence points - evaluation order unspecified.
qac-0401[U] '%1s' may be modified more than once between sequence points - evaluation order unspecified.
qac-0402[U] '%1s' is modified and accessed between sequence points - evaluation order unspecified.
qac-0403[U] '%1s' may be modified and accessed between sequence points - evaluation order unspecified.
qac-0404[U] More than one read access to volatile objects between sequence points.
qac-0405[U] More than one modification of volatile objects between sequence points.
+
EXP32RuleDo not access a volatile object through a nonvolatile reference
+ + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-0312Dangerous pointer cast results in loss of volatile qualification.
qac-0562[C] Right operand of assignment points to a more heavily qualified type.
qac-0563[C] Right operand of assignment is not of compatible pointer type.
qac-0673[C] Initializer points to a more heavily qualified type.
qac-0674[C] Initializer for pointer is of incompatible type.
+
EXP33RuleDo not read uninitialized memory
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-2726Definite: Use of uninitialized resource.
qac-2727Apparent: Use of uninitialized resource.
qac-2728Suspicious: Use of uninitialized resource.
qac-2961Definite: Using value of uninitialized automatic object '%s'.
qac-2962Apparent: Using value of uninitialized automatic object '%s'.
qac-2963Suspicious: Using value of uninitialized automatic object '%s'.
qac-2966Definite: Some members of object '%s' are uninitialized.
qac-2967Apparent: Some members of object '%s' are uninitialized.
qac-2968Suspicious: Some members of object '%s' are uninitialized.
qac-2971Definite: Passing address of uninitialized object '%s' to a function parameter declared as a pointer to const.
qac-2972Apparent: Passing address of uninitialized object '%s' to a function parameter declared as a pointer to const.
qac-2973Suspicious: Passing address of uninitialized object '%s' to a function parameter declared as a pointer to const.
qac-2976Definite: Passing address of partially initialized object '%s' to a function parameter declared as a pointer to const.
qac-2977Apparent: Passing address of partially initialized object '%s' to a function parameter declared as a pointer to const.
qac-2978Suspicious: Passing address of partially initialized object '%s' to a function parameter declared as a pointer to const.
+
EXP34RuleDo not dereference null pointers
+ + + + + + + + + + + + + + + + + +
QacDescription
qac-2810Constant: Dereference of NULL pointer.
qac-2811Definite: Dereference of NULL pointer.
qac-2812Apparent: Dereference of NULL pointer.
qac-2813Suspicious: Dereference of NULL pointer.
+
EXP35RuleDo not modify objects with temporary lifetime
+ + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-0450[U] Passing an array with temporary lifetime as a function parameter.
qac-0455[U] Passing an array with temporary lifetime as a constant function parameter.
qac-0459[U] Modifying the contents of an array with temporary lifetime.
qac-0464[U] Storing a pointer to an array with temporary lifetime.
qac-0465[U] Returning a pointer to an array with temporary lifetime.
+
EXP36RuleDo not cast pointers into more strictly aligned pointer types
+ + + + + + + + + +
QacDescription
qac-0326[I] Cast between a pointer to void and an integral type.
qac-3305Pointer cast to stricter alignment.
+
EXP37RuleCall functions with the correct number and type of arguments
+ + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-1331Type or number of arguments doesn't match previous use of the function.
qac-1332Type or number of arguments doesn't match prototype found later.
qac-1333Type or number of arguments doesn't match function definition found later.
qac-3002Defining '%1s()' with an identifier list and separate parameter declarations is an obsolescent feature.
qac-3320Type of argument no. %s differs from its type in definition of function.
qac-3335No function declaration. Implicit declaration inserted: 'extern int %s();'.
+
EXP39RuleDo not access a variable through a pointer of an incompatible type
+ + + + + + + + + + + + + +
QacDescription
qac-0310Casting to different object pointer type.
qac-0751Casting to char pointer type.
qac-3305Pointer cast to stricter alignment.
+
EXP40RuleDo not modify constant objects
+ + + + + +
QacDescription
qac-0563[C] Right operand of assignment is not of compatible pointer type.
+
EXP42RuleDo not compare padding data
+ + + + + + + + + + + + + + + + + +
QacDescription
qac-4726Definite: Comparison of padding bits that are not part of the object representation.
qac-4727Apparent: Comparison of padding bits that are not part of the object representation.
qac-4728Suspicious: Comparison of padding bits that are not part of the object representation.
qac-4729Possible: Comparison of padding bits that are not part of the object representation.
+
EXP43RuleAvoid undefined behavior when using restrict-qualified pointers
+ + + + + +
QacDescription
qac-1057[C99] The keyword 'restrict' has been used.
+
EXP44RuleDo not rely on side effects in operands to sizeof, _Alignof, or _Generic
+ + + + + +
QacDescription
qac-3307The operand of 'sizeof' is an expression with implied side effects, but they will not be evaluated.
+
EXP45RuleDo not perform assignments in selection statements
+ + + + + + + + + + + + + + + + + +
QacDescription
qac-3314This controlling expression is an assignment.
qac-3326The result of an assignment is being used in a logical operation.
qac-3344Controlling expression is not an 'essentially Boolean' expression.
qac-3416Logical operation performed on expression with persistent side effects.
+
EXP46RuleDo not use a bitwise operator with a Boolean-like operand
+ + + + + + + + + +
QacDescription
qac-3344Controlling expression is not an 'essentially Boolean' expression.
qac-4502An expression of 'essentially Boolean' type (%1s) is being used as the %2s operand of this bitwise operator (%3s).
+
EXP47RuleDo not call va_arg with an argument of the incorrect type
+ + + + + + + + + + + + + + + + + +
QacDescription
qac-4901Definite: Invalid call to va_arg.
qac-4902Apparent: Invalid call to va_arg.
qac-4903Suspicious: Invalid call to va_arg.
qac-4904Possible: Invalid call to va_arg.
+
FIO01RecommendationBe careful using functions that use file names for identification
+ + + + + +
QacDescription
certccm-5011'%s' is being used.
+
FIO03RecommendationDo not make assumptions about fopen() and file creation
+ + + + + +
QacDescription
certccm-5012'%s' is being used.
+
FIO06RecommendationCreate files with appropriate access permissions
+ + + + + +
QacDescription
certccm-5013'%s' is being used.
+
FIO08RecommendationTake care when calling remove() on an open file
+ + + + + +
QacDescription
certccm-5014'%s' is being used.
+
FIO10RecommendationTake care when using the rename() function
+ + + + + +
QacDescription
certccm-5015'%s' is being used.
+
FIO21RecommendationDo not create temporary files in shared directories
+ + + + + +
QacDescription
certccm-5016'%s' is being used.
+
FIO30RuleExclude user input from format strings
+ + + + + + + + + + + + + +
QacDescription
qac-4916Definite: Using a tainted variable as format string.
qac-4917Apparent: Using a tainted variable as format string.
qac-4918Suspicious: Using a tainted variable as format string.
+
FIO32RuleDo not perform operations on devices that are only appropriate for files
+ + + + + + + + + + + + + +
QacDescription
qac-4921Definite: Using a tainted variable as a device name when opening a device that could be a file.
qac-4922Apparent: Using a tainted variable as a device name when opening a device that could be a file.
qac-4923Suspicious: Using a tainted variable as a device name when opening a device that could be a file.
+
FIO34RuleDistinguish between characters read from a file and EOF or WEOF
+ + + + + + + + + +
QacDescription
qac-2676Definite: The value originating from an EOF returning function was modified before being compared with macro EOF.
qac-2678Suspicious: The value originating from an EOF returning function was modified before being compared with macro EOF.
+
FIO37RuleDo not assume that fgets() or fgetws() returns a nonempty string when successful
+ + + + + + + + + + + + + +
QacDescription
qac-4911Definite: Assuming that fgets or fgetws returned a non empty string.
qac-4912Apparent: Assuming that fgets or fgetws returned a non empty string.
qac-4913Suspicious: Assuming that fgets or fgetws returned a non empty string.
+
FIO38RuleDo not copy a FILE object
+ + + + + + + + + +
QacDescription
qac-1485A pointer to a FILE object is dereferenced.
certccm-5028Copy of a FILE object.
+
FIO39RuleDo not alternately input and output from a stream without an intervening flush or positioning call
+ + + + + + + + + + + + + +
QacDescription
qac-4711Definite: Alternating I/O operations on a file stream found without an intervening positioning call or EOF.
qac-4712Apparent: Alternating I/O operations on a file stream found without an intervening positioning call or EOF.
qac-4713Suspicious: Alternating I/O operations on a file stream found without an intervening positioning call or EOF.
+
FIO40RuleReset strings on fgets() or fgetws() failure
+ + + + + + + + + + + + + +
QacDescription
qac-4861Definite: Using an array containing indeterminate values after a call to fgets or fgetws.
qac-4862Apparent: Using an array containing indeterminate values after a call to fgets or fgetws.
qac-4863Suspicious: Using an array containing indeterminate values after a call to fgets or fgetws.
+
FIO41RuleDo not call getc(), putc(), getwc(), or putwc() with a stream argument that has side effects
+ + + + + +
QacDescription
certccm-5036'%s' is being used.
+
FIO42RuleClose files when they are no longer needed
+ + + + + + + + + + + + + +
QacDescription
qac-2701Definite: Opened file is not closed.
qac-2702Apparent: Opened file is not closed.
qac-2703Suspicious: Opened file is not closed.
+
FIO44RuleOnly use values for fsetpos() that are returned from fgetpos()
+ + + + + + + + + + + + + +
QacDescription
qac-4841Definite: Passing a file position argument to fsetpos() not obtained from fgetpos().
qac-4842Apparent: Passing a file position argument to fsetpos() not obtained from fgetpos().
qac-4843Suspicious: Passing a file position argument to fsetpos() not obtained from fgetpos().
+
FIO45RuleAvoid TOCTOU race conditions while accessing files
+ + + + + + + + + + + + + +
QacDescription
qac-4851Definite: File access time-of-check time-of-use race condition.
qac-4852Apparent: File access time-of-check time-of-use race condition.
qac-4853Suspicious: File access time-of-check time-of-use race condition.
+
FIO46RuleDo not access a closed file
+ + + + + + + + + + + + + +
QacDescription
qac-2696Definite: Attempt to access a file which has been closed.
qac-2697Apparent: Attempt to access a file which has been closed.
qac-2698Suspicious: Attempt to access a file which has been closed.
+
FIO47RuleUse valid format strings
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-0161[U] Unknown length modifier used with 'i' or 'd' conversion specifier, number %1s.
qac-0162[U] Unknown length modifier used with 'o' conversion specifier, number %1s.
qac-0163[U] Unknown length modifier used with 'u' conversion specifier, number %1s.
qac-0164[U] Unknown length modifier used with 'x' conversion specifier, number %1s.
qac-0165[U] Unknown length modifier used with 'X' conversion specifier, number %1s.
qac-0166[U] Unknown length modifier used with 'f' conversion specifier, number %1s.
qac-0167[U] Unknown length modifier used with 'e' conversion specifier, number %1s.
qac-0168[U] Unknown length modifier used with 'E' conversion specifier, number %1s.
qac-0169[U] Unknown length modifier used with 'g' conversion specifier, number %1s.
qac-0170[U] Unknown length modifier used with 'G' conversion specifier, number %1s.
qac-0171[U] Unknown length modifier used with 'c' conversion specifier, number %1s.
qac-0172[U] Unknown length modifier used with '%%' conversion specifier, number %1s.
qac-0173[U] Unknown length modifier used with 's' conversion specifier, number %1s.
qac-0174[U] Unknown length modifier used with 'n' conversion specifier, number %1s.
qac-0175[U] Unknown length modifier used with 'p' conversion specifier, number %1s.
qac-0176[U] Incomplete conversion specifier, number %1s.
qac-0177[U] Field width of format conversion specifier exceeds 509 characters.
qac-0178[U] Precision of format conversion specifier exceeds 509 characters.
qac-0179[U] Argument type does not match conversion specifier number %1s.
qac-0180[C99] Use of ll for conversion specifier.
qac-0184[U] Insufficient arguments to satisfy conversion specifier, number %1s.
qac-0185[U] Call contains more arguments than conversion specifiers.
qac-0190[U] Using unsupported conversion specifier number %1s.
qac-0191[U] Unknown length modifier used with 'd/i/n' conversion specifier, number %1s.
qac-0192[U] Unknown length modifier used with 'o' conversion specifier, number %1s.
qac-0193[U] Unknown length modifier used with 'u' conversion specifier, number %1s.
qac-0194[U] Unknown length modifier used with 'x/X' conversion specifier, number %1s.
qac-0195[U] Unknown length modifier used with 'e/E/f/F/g/G' conversion specifier, number %1s.
qac-0196[U] Unknown length modifier used with 's' conversion specifier, number %1s.
qac-0197[U] Unknown length modifier used with 'p' conversion specifier, number %1s.
qac-0198[U] Unknown length modifier used with '%%' conversion specifier, number %1s.
qac-0199[U] Unknown length modifier used with '[' conversion specifier, number %1s.
qac-0200[U] Unknown length modifier used with 'c' conversion specifier, number %1s.
qac-0201[U] Incomplete conversion specifier, number %1s.
qac-0202[I] '-' character in '[]' conversion specification is implementation defined.
qac-0204[U] Field width of format conversion specifier exceeds 509 characters.
qac-0206[U] Argument type does not match conversion specifier number %1s.
qac-0209[U] Using a non-constant format string.
+
FLP02RecommendationAvoid using floating-point numbers when precise computation is needed
+ + + + + +
QacDescription
qac-0790This translation unit makes use of floating types.
+
FLP06RecommendationConvert integers to floating point for floating point operations
+ + + + + + + + + +
QacDescription
qac-4117Result of integer division operation implicitly converted to a floating type.
qac-4118Result of integer division operation cast to a floating type.
+
FLP30RuleDo not use floating-point variables as loop counters
+ + + + + + + + + + + + + +
QacDescription
qac-3339Floating point variable used as 'while' loop control variable.
qac-3340Floating point variable used as 'for' loop control variable.
qac-3342Controlling expression of 'for' loop is a floating point comparison.
+
FLP32RulePrevent or detect domain and range errors in math functions
+ + + + + +
QacDescription
certccm-5025'%s' is being used.
+
FLP34RuleEnsure that floating-point conversions are within range of the new type
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-4450An expression of 'essentially floating' type (%1s) is being converted to Boolean type, '%2s' on assignment.
qac-4451An expression of 'essentially floating' type (%1s) is being converted to character type, '%2s' on assignment.
qac-4452An expression of 'essentially floating' type (%1s) is being converted to enum type, '%2s' on assignment.
qac-4453An expression of 'essentially floating' type (%1s) is being converted to signed type, '%2s' on assignment.
qac-4454An expression of 'essentially floating' type (%1s) is being converted to unsigned type, '%2s' on assignment.
qac-4462A non-constant expression of 'essentially floating' type (%1s) is being converted to narrower floating type, '%2s' on assignment.
qac-4465A constant expression of 'essentially floating' type (%1s) is being converted to narrower floating type, '%2s' on assignment.
+
FLP36RulePreserve precision when converting integral values to floating-point type
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-1260Integer constant implicitly converted to a floating type.
qac-1263Floating constant causes implicit conversion of other (integral) operand.
qac-1298An integer constant of 'essentially signed' type is being converted to floating type on assignment.
qac-1299An integer constant of 'essentially unsigned' type is being converted to floating type on assignment.
qac-1800The %1s operand (essential type: '%2s') will be implicitly converted to a floating type, '%3s', in this arithmetic operation.
qac-1802The %1s operand (essential type: '%2s') will be implicitly converted to a floating type, '%3s', in this relational operation.
qac-1803The %1s operand (essential type: '%2s') will be implicitly converted to a floating type, '%3s', in this equality operation.
qac-1804The %1s operand (essential type: '%2s') will be implicitly converted to a floating type, '%3s', in this conditional operation.
qac-4117Result of integer division operation implicitly converted to a floating type.
qac-4435A non-constant expression of 'essentially signed' type (%1s) is being converted to floating type, '%2s' on assignment.
qac-4437A constant expression of 'essentially signed' type (%1s) is being converted to floating type, '%2s' on assignment.
qac-4445An expression of 'essentially unsigned' type (%1s) is being converted to floating type, '%2s' on assignment.
+
FLP37RuleDo not use object representations to compare floating-point values
+ + + + + +
QacDescription
certccm-5026Incorrect floating point value comparison.
+
INT02RecommendationUnderstand integer conversion rules
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-1250Unsuffixed integer constant causes implicit conversion of other operand.
qac-1251Suffixed integer constant causes implicit conversion of other operand.
qac-1252Suffixed integer constant implicitly converted to different integer type.
qac-1253Unsuffixed integer constant implicitly converted to different integer type.
qac-1256An integer constant suffixed with L is being converted to type signed or unsigned long long on assignment.
qac-1257An integer constant suffixed with L or LL is being converted to a type of lower rank on assignment.
qac-1260Integer constant implicitly converted to a floating type.
qac-1263Floating constant causes implicit conversion of other (integral) operand.
qac-1266A floating constant is being converted to integral type on assignment.
qac-1274Unsuffixed floating constant causes implicit conversion of other (floating) operand.
qac-1290An integer constant of 'essentially signed' type is being converted to unsigned type on assignment.
qac-1291An integer constant of 'essentially unsigned' type is being converted to signed type on assignment.
qac-1292An integer constant of 'essentially signed' type is being converted to type char on assignment.
qac-1293An integer constant of 'essentially unsigned' type is being converted to type char on assignment.
qac-1294An integer constant of 'essentially signed' type is being converted to type _Bool on assignment.
qac-1295An integer constant of 'essentially unsigned' type is being converted to type _Bool on assignment.
qac-1296An integer constant of 'essentially signed' type is being converted to enum type on assignment.
qac-1297An integer constant of 'essentially unsigned' type is being converted to enum type on assignment.
qac-1298An integer constant of 'essentially signed' type is being converted to floating type on assignment.
qac-1299An integer constant of 'essentially unsigned' type is being converted to floating type on assignment.
qac-1800The %1s operand (essential type: '%2s') will be implicitly converted to a floating type, '%3s', in this arithmetic operation.
qac-1802The %1s operand (essential type: '%2s') will be implicitly converted to a floating type, '%3s', in this relational operation.
qac-1803The %1s operand (essential type: '%2s') will be implicitly converted to a floating type, '%3s', in this equality operation.
qac-1804The %1s operand (essential type: '%2s') will be implicitly converted to a floating type, '%3s', in this conditional operation.
qac-1810An operand of 'essentially character' type is being added to another operand of 'essentially character' type.
qac-1811An operand of 'essentially character' type is being subtracted from an operand of 'essentially signed' type.
qac-1812An operand of 'essentially character' type is being subtracted from an operand of 'essentially unsigned' type.
qac-1813An operand of 'essentially character' type is being balanced with an operand of 'essentially floating' type in this arithmetic operation.
qac-1820The %1s operand is non-constant and 'essentially signed' (%2s) but will be implicitly converted to an unsigned type (%3s) in this arithmetic operation.
qac-1821The %1s operand is non-constant and 'essentially signed' (%2s) but will be implicitly converted to an unsigned type (%3s) in this bitwise operation.
qac-1822The %1s operand is non-constant and 'essentially signed' (%2s) but will be implicitly converted to an unsigned type (%3s) in this relational operation.
qac-1823The %1s operand is non-constant and 'essentially signed' (%2s) but will be implicitly converted to an unsigned type (%3s) in this equality operation.
qac-1824The %1s operand is non-constant and 'essentially signed' (%2s) but will be implicitly converted to an unsigned type (%3s) in this conditional operation.
qac-1830The %1s operand is constant, 'essentially signed' (%2s) and negative but will be implicitly converted to an unsigned type (%3s) in this arithmetic operation.
qac-1831The %1s operand is constant, 'essentially signed' (%2s) and negative but will be implicitly converted to an unsigned type (%3s) in this bitwise operation.
qac-1832The %1s operand is constant, 'essentially signed' (%2s) and negative but will be implicitly converted to an unsigned type (%3s) in this relational operation.
qac-1833The %1s operand is constant, 'essentially signed' (%2s) and negative but will be implicitly converted to an unsigned type (%3s) in this equality operation.
qac-1834The %1s operand is constant, 'essentially signed' (%2s) and negative but will be implicitly converted to an unsigned type (%3s) in this conditional operation.
qac-1840The %1s operand is constant, 'essentially signed' (%2s) and non-negative but will be implicitly converted to an unsigned type (%3s) in this arithmetic operation.
qac-1841The %1s operand is constant, 'essentially signed' (%2s) and non-negative but will be implicitly converted to an unsigned type (%3s) in this bitwise operation.
qac-1842The %1s operand is constant, 'essentially signed' (%2s) and non-negative but will be implicitly converted to an unsigned type (%3s) in this relational operation.
qac-1843The %1s operand is constant, 'essentially signed' (%2s) and non-negative but will be implicitly converted to an unsigned type (%3s) in this equality operation.
qac-1844The %1s operand is constant, 'essentially signed' (%2s) and non-negative but will be implicitly converted to an unsigned type (%3s) in this conditional operation.
qac-1850The %1s operand is 'essentially unsigned' (%2s) but will be implicitly converted to a signed type (%3s) in this arithmetic operation.
qac-1851The %1s operand is 'essentially unsigned' (%2s) but will be implicitly converted to a signed type (%3s) in this bitwise operation.
qac-1852The %1s operand is 'essentially unsigned' (%2s) but will be implicitly converted to a signed type (%3s) in this relational operation.
qac-1853The %1s operand is 'essentially unsigned' (%2s) but will be implicitly converted to a signed type (%3s) in this equality operation.
qac-1854The %1s operand is 'essentially unsigned' (%2s) but will be implicitly converted to a signed type (%3s) in this conditional operation.
qac-1860The operands of this arithmetic operator are of different 'essential signedness' but will generate a result of type 'signed int'.
qac-1861The operands of this bitwise operator are of different 'essential signedness' but will generate a result of type 'signed int'.
qac-1862The operands of this relational operator are of different 'essential signedness' but will both be promoted to 'signed int' for comparison.
qac-1863The operands of this equality operator are of different 'essential signedness' but will both be promoted to 'signed int' for comparison.
qac-1864The 2nd and 3rd operands of this conditional operator are of different 'essential signedness'. The result will be in the promoted type 'signed int'.
qac-1880The operands of this relational operator are expressions of different 'essential type' categories (%1s and %2s).
qac-1881The operands of this equality operator are expressions of different 'essential type' categories (%1s and %2s).
qac-1882The 2nd and 3rd operands of this conditional operator are expressions of different 'essential type' categories (%1s and %2s).
qac-2100Integral promotion : unsigned char promoted to signed int.
qac-2101Integral promotion : unsigned short promoted to signed int.
qac-2102Integral promotion : unsigned char promoted to unsigned int.
qac-2103Integral promotion : unsigned short promoted to unsigned int.
qac-2104Integral promotion : signed char promoted to signed int.
qac-2105Integral promotion : signed short promoted to signed int.
qac-2106Integral promotion : plain char promoted to signed int.
qac-2107Integral promotion : plain char promoted to unsigned int.
qac-2109Integral promotion : _Bool promoted to signed int.
qac-2110Default argument promotion : unsigned char promoted to signed int.
qac-2111Default argument promotion : unsigned short promoted to signed int.
qac-2112Default argument promotion : unsigned char promoted to unsigned int.
qac-2113Default argument promotion : unsigned short promoted to unsigned int.
qac-2114Default argument promotion : signed char promoted to signed int.
qac-2115Default argument promotion : signed short promoted to signed int.
qac-2116Default argument promotion : plain char promoted to signed int.
qac-2117Default argument promotion : plain char promoted to unsigned int.
qac-2118Default argument promotion : float promoted to double.
qac-2119Default argument promotion : _Bool promoted to signed int.
qac-2120Integral promotion : unsigned bit-field promoted to signed int.
qac-2122Integral promotion : unsigned bit-field promoted to unsigned int.
qac-2124Integral promotion : signed bit-field promoted to signed int.
qac-2130Default argument promotion : unsigned bit-field promoted to signed int.
qac-2132Default argument promotion : unsigned bit-field promoted to unsigned int.
qac-2134Default argument promotion : signed bit-field promoted to signed int.
qac-4401An expression of 'essentially Boolean' type (%1s) is being converted to character type, '%2s' on assignment.
qac-4402An expression of 'essentially Boolean' type (%1s) is being converted to enum type, '%2s' on assignment.
qac-4403An expression of 'essentially Boolean' type (%1s) is being converted to signed type, '%2s' on assignment.
qac-4404An expression of 'essentially Boolean' type (%1s) is being converted to unsigned type, '%2s' on assignment.
qac-4405An expression of 'essentially Boolean' type (%1s) is being converted to floating type, '%2s' on assignment.
qac-4410An expression of 'essentially character' type (%1s) is being converted to Boolean type, '%2s' on assignment.
qac-4412An expression of 'essentially character' type (%1s) is being converted to enum type, '%2s' on assignment.
qac-4413An expression of 'essentially character' type (%1s) is being converted to signed type, '%2s' on assignment.
qac-4414An expression of 'essentially character' type (%1s) is being converted to unsigned type, '%2s' on assignment.
qac-4415An expression of 'essentially character' type (%1s) is being converted to floating type, '%2s' on assignment.
qac-4420An expression of 'essentially enum' type (%1s) is being converted to Boolean type, '%2s' on assignment.
qac-4421An expression of 'essentially enum' type (%1s) is being converted to character type, '%2s' on assignment.
qac-4422An expression of 'essentially enum' type (%1s) is being converted to a different enum type, '%2s' on assignment.
qac-4423An expression of 'essentially enum' type (%1s) is being converted to signed type, '%2s' on assignment.
qac-4424An expression of 'essentially enum' type (%1s) is being converted to unsigned type, '%2s' on assignment.
qac-4425An expression of 'essentially enum' type (%1s) is being converted to floating type, '%2s' on assignment.
qac-4430An expression of 'essentially signed' type (%1s) is being converted to Boolean type, '%2s' on assignment.
qac-4431An expression of 'essentially signed' type (%1s) is being converted to character type, '%2s' on assignment.
qac-4432An expression of 'essentially signed' type (%1s) is being converted to enum type, '%2s' on assignment.
qac-4434A non-constant expression of 'essentially signed' type (%1s) is being converted to unsigned type, '%2s' on assignment.
qac-4435A non-constant expression of 'essentially signed' type (%1s) is being converted to floating type, '%2s' on assignment.
qac-4436A constant expression of 'essentially signed' type (%1s) is being converted to unsigned type, '%2s' on assignment.
qac-4437A constant expression of 'essentially signed' type (%1s) is being converted to floating type, '%2s' on assignment.
qac-4440An expression of 'essentially unsigned' type (%1s) is being converted to Boolean type, '%2s' on assignment.
qac-4441An expression of 'essentially unsigned' type (%1s) is being converted to character type, '%2s' on assignment.
qac-4442An expression of 'essentially unsigned' type (%1s) is being converted to enum type, '%2s' on assignment.
qac-4443A non-constant expression of 'essentially unsigned' type (%1s) is being converted to a wider signed type, '%2s' on assignment.
qac-4445An expression of 'essentially unsigned' type (%1s) is being converted to floating type, '%2s' on assignment.
qac-4446A non-constant expression of 'essentially unsigned' type (%1s) is being converted to signed type, '%2s' on assignment.
qac-4447A constant expression of 'essentially unsigned' type (%1s) is being converted to signed type, '%2s' on assignment.
qac-4460A non-constant expression of 'essentially signed' type (%1s) is being converted to narrower signed type, '%2s' on assignment.
qac-4461A non-constant expression of 'essentially unsigned' type (%1s) is being converted to narrower unsigned type, '%2s' on assignment.
qac-4463A constant expression of 'essentially signed' type (%1s) is being converted to narrower signed type, '%2s' on assignment.
qac-4464A constant expression of 'essentially unsigned' type (%1s) is being converted to narrower unsigned type, '%2s' on assignment.
qac-4470A non-constant expression of 'essentially signed' type (%1s) is being passed to a function parameter of wider signed type, '%2s'.
qac-4471A non-constant expression of 'essentially unsigned' type (%1s) is being passed to a function parameter of wider unsigned type, '%2s'.
qac-4480A non-constant expression of 'essentially signed' type (%1s) is being returned from a function defined with a wider signed return type, '%2s'.
qac-4481A non-constant expression of 'essentially unsigned' type (%1s) is being returned from a function defined with a wider unsigned return type, '%2s'.
+
INT04RecommendationEnforce limits on integer values originating from tainted sources
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-2794Possible: Tainted right hand operand of shift operator is negative or too large.
qac-2804Possible: Overflow in signed arithmetic tainted operation.
qac-2854Possible: Implicit conversion of a tainted expression to a signed integer type of insufficient size.
qac-2859Possible: Casting a tainted expression to a signed integer type of insufficient size.
qac-2864Possible: Implementation-defined value resulting from left shift operation on tainted expression of signed type.
qac-2894Possible: Negative tainted value implicitly converted to an unsigned type.
qac-2899Possible: Negative tainted value cast to an unsigned type.
qac-2904Possible: Positive integer tainted value truncated by implicit conversion to a smaller unsigned type.
qac-2909Possible: Positive integer tainted value truncated by cast to a smaller unsigned type.
qac-2914Possible: Wraparound in unsigned arithmetic tainted operation.
qac-2924Possible: Left shift operation on tainted expression of unsigned type results in loss of high order bits.
qac-2944Possible: Result of implicit conversion of a tainted expression is only representable in a two's complement implementation.
qac-2949Possible: Result of cast of a tainted expression is only representable in a two's complement implementation.
qac-2954Possible: Negative tainted value used in array subscript or pointer arithmetic operation.
qac-2956Definite: Using object '%s' with tainted value.
qac-2959Possible: Using object '%s' with tainted value.
+
INT05RecommendationDo not use input functions to convert character data if they cannot handle all possible inputs
+ + + + + +
QacDescription
certccm-5005'%s' is being used.
+
INT07RecommendationUse only explicitly signed or unsigned char type for numeric values
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-1292An integer constant of 'essentially signed' type is being converted to type char on assignment.
qac-1293An integer constant of 'essentially unsigned' type is being converted to type char on assignment.
qac-4401An expression of 'essentially Boolean' type (%1s) is being converted to character type, '%2s' on assignment.
qac-4421An expression of 'essentially enum' type (%1s) is being converted to character type, '%2s' on assignment.
qac-4431An expression of 'essentially signed' type (%1s) is being converted to character type, '%2s' on assignment.
qac-4441An expression of 'essentially unsigned' type (%1s) is being converted to character type, '%2s' on assignment.
qac-4451An expression of 'essentially floating' type (%1s) is being converted to character type, '%2s' on assignment.
+
INT08RecommendationVerify that all integer values are in range
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-2800Constant: Overflow in signed arithmetic operation.
qac-2801Definite: Overflow in signed arithmetic operation.
qac-2802Apparent: Overflow in signed arithmetic operation.
qac-2803Suspicious: Overflow in signed arithmetic operation.
qac-2910Constant: Wraparound in unsigned arithmetic operation.
qac-2911Definite: Wraparound in unsigned arithmetic operation.
qac-2912Apparent: Wraparound in unsigned arithmetic operation.
qac-2913Suspicious: Wraparound in unsigned arithmetic operation.
+
INT09RecommendationEnsure enumeration constants map to unique values
+ + + + + +
QacDescription
qac-0724The value of this implicitly-specified enumeration constant is not unique.
+
INT10RecommendationDo not assume a positive remainder when using the % operator
+ + + + + +
QacDescription
qac-3103Result of signed division or remainder operation may be implementation defined.
+
INT12RecommendationDo not make assumptions about the type of a plain int bit-field when used in an expression
+ + + + + + + + + +
QacDescription
qac-0634[I] Bit-field %1s in %2s has not been declared explicitly as unsigned or signed.
qac-0635[C99] Bit-field %1s in %2s has been declared with a type not explicitly supported.
+
INT13RecommendationUse bitwise operators only on unsigned operands
+ + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-4532An expression of 'essentially signed' type (%1s) is being used as the %2s operand of this bitwise operator (%3s).
qac-4533An expression of 'essentially signed' type (%1s) is being used as the left-hand operand of this shift operator (%2s).
qac-4534An expression of 'essentially signed' type (%1s) is being used as the right-hand operand of this shift operator (%2s).
qac-4543A non-negative constant expression of 'essentially signed' type (%1s) is being used as the left-hand operand of this shift operator (%2s).
qac-4544A non-negative constant expression of 'essentially signed' type (%1s) is being used as the right-hand operand of this shift operator (%2s).
+
INT16RecommendationDo not make assumptions about representation of signed integers
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-2940Constant: Result of implicit conversion is only representable in a two's complement implementation.
qac-2941Definite: Result of implicit conversion is only representable in a two's complement implementation.
qac-2942Apparent: Result of implicit conversion is only representable in a two's complement implementation.
qac-2943Suspicious: Result of implicit conversion is only representable in a two's complement implementation.
qac-2945Constant: Result of cast is only representable in a two's complement implementation.
qac-2946Definite: Result of cast is only representable in a two's complement implementation.
qac-2947Apparent: Result of cast is only representable in a two's complement implementation.
qac-2948Suspicious: Result of cast is only representable in a two's complement implementation.
+
INT18RecommendationEvaluate integer expressions in a larger size before comparing or assigning to that size
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-1890A composite expression of 'essentially signed' type (%1s) is being implicitly converted to a wider signed type, '%2s'.
qac-1891A composite expression of 'essentially unsigned' type (%1s) is being implicitly converted to a wider unsigned type, '%2s'.
qac-1892A composite expression of 'essentially floating' type (%1s) is being implicitly converted to a wider floating type, '%2s'.
qac-1893The 2nd and 3rd operands of this conditional operator are both 'essentially signed' ('%1s' and '%2s') but one is a composite expression of a narrower type than the other.
qac-1894The 2nd and 3rd operands of this conditional operator are both 'essentially unsigned' ('%1s' and '%2s') but one is a composite expression of a narrower type than the other.
qac-1895The 2nd and 3rd operands of this conditional operator are both 'essentially floating' ('%1s' and '%2s') but one is a composite expression of a narrower type than the other.
qac-4490A composite expression of 'essentially signed' type (%1s) is being converted to wider signed type, '%2s' on assignment.
qac-4491A composite expression of 'essentially unsigned' type (%1s) is being converted to wider unsigned type, '%2s' on assignment.
qac-4492A composite expression of 'essentially floating' type (%1s) is being converted to wider floating type, '%2s' on assignment.
+
INT30RuleEnsure that unsigned integer operations do not wrap
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-2910Constant: Wraparound in unsigned arithmetic operation.
qac-2911Definite: Wraparound in unsigned arithmetic operation.
qac-2912Apparent: Wraparound in unsigned arithmetic operation.
qac-2913Suspicious: Wraparound in unsigned arithmetic operation.
qac-3383Cannot identify wraparound guard for unsigned arithmetic expression.
qac-3384Cannot identify wraparound guard for dependent unsigned arithmetic expression.
qac-3385Cannot identify wraparound guard for sizeof-VLA expression.
qac-3386Cannot identify wraparound guard for dependent sizeof-VLA expression.
+
INT31RuleEnsure that integer conversions do not result in lost or misinterpreted data
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-2850Constant: Implicit conversion to a signed integer type of insufficient size.
qac-2851Definite: Implicit conversion to a signed integer type of insufficient size.
qac-2852Apparent: Implicit conversion to a signed integer type of insufficient size.
qac-2853Suspicious: Implicit conversion to a signed integer type of insufficient size.
qac-2855Constant: Casting to a signed integer type of insufficient size.
qac-2856Definite: Casting to a signed integer type of insufficient size.
qac-2857Apparent: Casting to a signed integer type of insufficient size.
qac-2858Suspicious: Casting to a signed integer type of insufficient size.
qac-2890Constant: Negative value implicitly converted to an unsigned type.
qac-2891Definite: Negative value implicitly converted to an unsigned type.
qac-2892Apparent: Negative value implicitly converted to an unsigned type.
qac-2893Suspicious: Negative value implicitly converted to an unsigned type.
qac-2895Constant: Negative value cast to an unsigned type.
qac-2896Definite: Negative value cast to an unsigned type.
qac-2897Apparent: Negative value cast to an unsigned type.
qac-2898Suspicious: Negative value cast to an unsigned type.
qac-2900Constant: Positive integer value truncated by implicit conversion to a smaller unsigned type.
qac-2901Definite: Positive integer value truncated by implicit conversion to a smaller unsigned type.
qac-2902Apparent: Positive integer value truncated by implicit conversion to a smaller unsigned type.
qac-2903Suspicious: Positive integer value truncated by implicit conversion to a smaller unsigned type.
qac-2905Constant: Positive integer value truncated by cast to a smaller unsigned type.
qac-2906Definite: Positive integer value truncated by cast to a smaller unsigned type.
qac-2907Apparent: Positive integer value truncated by cast to a smaller unsigned type.
qac-2908Suspicious: Positive integer value truncated by cast to a smaller unsigned type.
+
INT32RuleEnsure that operations on signed integers do not result in overflow
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-2800Constant: Overflow in signed arithmetic operation.
qac-2801Definite: Overflow in signed arithmetic operation.
qac-2802Apparent: Overflow in signed arithmetic operation.
qac-2803Suspicious: Overflow in signed arithmetic operation.
qac-2860Constant: Implementation-defined value resulting from left shift operation on expression of signed type.
qac-2861Definite: Implementation-defined value resulting from left shift operation on expression of signed type.
qac-2862Apparent: Implementation-defined value resulting from left shift operation on expression of signed type.
qac-2863Suspicious: Implementation-defined value resulting from left shift operation on expression of signed type.
+
INT33RuleEnsure that division and remainder operations do not result in divide-by-zero errors
+ + + + + + + + + + + + + + + + + +
QacDescription
qac-2830Constant: Division by zero.
qac-2831Definite: Division by zero.
qac-2832Apparent: Division by zero.
qac-2833Suspicious: Division by zero.
+
INT34RuleDo not shift an expression by a negative number of bits or by greater than or equal to the number of bits that exist in the operand
+ + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-0499Right operand of shift operator is greater than or equal to the width of the essential type of the left operand.
qac-2790Constant: Right hand operand of shift operator is negative or too large.
qac-2791Definite: Right hand operand of shift operator is negative or too large.
qac-2792Apparent: Right hand operand of shift operator is negative or too large.
qac-2793Suspicious: Right hand operand of shift operator is negative or too large.
+
INT35RuleUse correct integer precisions
+ + + + + +
QacDescription
qac-0582[I] Multiplying the size of an integer by CHAR_BIT will include padding bits in the result.
+
INT36RuleConverting a pointer to integer or integer to pointer
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-0303[I] Cast between a pointer to volatile object and an integral type.
qac-0305[I] Cast between a pointer to function and an integral type.
qac-0306[I] Cast between a pointer to object and an integral type.
qac-0309[U] Integral type is not large enough to hold a pointer value.
qac-0324[u] Cast between a pointer to incomplete type and an integral type.
qac-0326[I] Cast between a pointer to void and an integral type.
qac-0360An expression of pointer type is being converted to type _Bool on assignment.
qac-0361An expression of pointer type is being cast to type _Bool.
qac-0362An expression of essentially Boolean type is being cast to a pointer.
+
MEM02RecommendationImmediately cast the result of a memory allocation function call into a pointer to the allocated type
+ + + + + +
QacDescription
qac-0695Type given in sizeof is not compatible with the pointed to type used to cast malloc.
+
MEM03RecommendationClear sensitive information stored in reusable resources
+ + + + + +
QacDescription
certccm-5010'%s' is being used.
+
MEM05RecommendationAvoid large stack allocations
+ + + + + + + + + + + + + +
QacDescription
qac-1051[C99] A variable length array has been declared.
rcma-1520Functions are indirectly recursive.
qac-3670Recursive call to function containing this call.
+
MEM30RuleDo not access freed memory
+ + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-4866Definite: Memory is used after free.
qac-4867Apparent: Memory is used after free.
qac-4868Suspicious: Memory is used after free.
qac-4871Definite: Zero size has been passed to realloc, malloc or calloc.
qac-4872Apparent: Zero size has been passed to realloc, malloc or calloc.
qac-4873Suspicious: Zero size has been passed to realloc, malloc or calloc.
+
MEM31RuleFree dynamically allocated memory when no longer needed
+ + + + + + + + + + + + + +
QacDescription
qac-2706Definite: Allocated memory is not deallocated.
qac-2707Apparent: Allocated memory is not deallocated.
qac-2708Suspicious: Allocated memory is not deallocated.
+
MEM33RuleAllocate and copy structures containing a flexible array member dynamically
+ + + + + + + + + + + + + + + + + +
QacDescription
qac-1061[C] Structure '%1s' with flexible array member '%2s' cannot be used in the declaration of structure member '%3s'.
qac-1062[C] Structure '%1s' with flexible array member '%2s' cannot be used in the declaration of array elements.
qac-1063[C99] '%s' is an automatically-allocated object of a type declared with a flexible array member.
qac-1064[C99] '%s' is a statically-allocated object of a type declared with a flexible array member.
+
MEM34RuleOnly free memory allocated dynamically
+ + + + + + + + + + + + + +
QacDescription
qac-2721Definite: Deallocation of non dynamic memory.
qac-2722Apparent: Deallocation of non dynamic memory.
qac-2723Suspicious: Deallocation of non dynamic memory.
+
MEM35RuleAllocate sufficient memory for an object
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-0696The size of the allocated memory block is smaller than the size of the object type addressed by the pointer cast.
qac-0701Argument for memory size does not refer to 'sizeof (%1s)'.
qac-1069[C99] Taking sizeof a struct with a flexible array member without adding the array size.
qac-1071[C99] Taking sizeof a struct with a flexible array member without explicitly adding the array size in terms of element sizes.
qac-1073[C99] Taking sizeof a struct with a flexible array member while adding an array size based on the wrong element type.
qac-2840Constant: Dereference of an invalid pointer value.
qac-2841Definite: Dereference of an invalid pointer value.
qac-2842Apparent: Dereference of an invalid pointer value.
qac-2843Suspicious: Dereference of an invalid pointer value.
qac-2935Constant: Dereference of an invalid char pointer value.
qac-2936Definite: Dereference of an invalid char pointer value.
qac-2937Apparent: Dereference of an invalid char pointer value.
qac-2938Suspicious: Dereference of an invalid char pointer value.
+
MEM36RuleDo not modify the alignment of objects by calling realloc()
+ + + + + +
QacDescription
certccm-5027'%s' is being used.
+
MSC01RecommendationStrive for logical completeness
+ + + + + + + + + + + + + +
QacDescription
qac-2000No 'else' clause exists for this 'if' statement.
qac-2002No 'default' label found in this 'switch' statement.
qac-2004No concluding 'else' exists in this 'if'-'else'-'if' statement.
+
MSC04RecommendationUse comments consistently and in a readable fashion
+ + + + + +
QacDescription
qac-3108Nested comments are not recognized in the ISO standard.
+
MSC07RecommendationDetect and remove dead code
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
rcma-1501The function '%1s' is declared but is not used within this project.
rcma-1503The function '%1s' is defined but is not used within this project.
qac-2008Code statements precede the first label in this 'switch' construct.
qac-2877This loop will never be executed more than once.
qac-2880This code is unreachable.
qac-2881The code in this 'default' clause is unreachable.
qac-2882This 'switch' statement will bypass the initialization of local variables.
qac-2883This 'goto' statement will always bypass the initialization of local variables.
qac-2980The value of this function parameter is never used before being modified.
qac-2981This initialization is redundant. The value of this object is never used before being modified.
qac-2982This assignment is redundant. The value of this object is never used before being modified.
qac-2983This assignment is redundant. The value of this object is never subsequently used.
qac-2984This operation is redundant. The value of the result is always '%1s'.
qac-2985This operation is redundant. The value of the result is always that of the left-hand operand.
qac-2986This operation is redundant. The value of the result is always that of the right-hand operand.
qac-3202The label '%s:' is not used in this function and could be removed.
qac-3203The variable '%s' is set but never used.
qac-3205The identifier '%1s' is not used and could be removed.
qac-3206The parameter '%s' is not used in this function.
qac-3207File scope static, '%1s', is not used, and could be removed.
qac-3210The global identifier '%1s' is declared here but is not used in this translation unit.
qac-3219Static function '%1s()' is not used within this translation unit.
qac-3229File scope static, '%1s', is written but never used.
qac-3404Statement contains a redundant * operator at top level. *p++ means *(p++) not (*p)++.
qac-3422Statement contains a redundant operator at top level.
qac-3423Statement contains a redundant cast at top level.
qac-3425One branch of this conditional operation is a redundant expression.
qac-3470The operand of 'sizeof' is not an expression which designates either an object or a type.
+
MSC09RecommendationCharacter encoding: Use subset of ASCII for safety
+ + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-0285[I] Character constant contains character which is not a member of the basic source character set.
qac-0286[I] String literal contains character which is not a member of the basic source character set.
qac-0287[I] Header name contains character which is not a member of the basic source character set.
qac-0288[I] Source file '%s' has comments containing characters which are not members of the basic source character set.
qac-0289[I] Source file '%s' has preprocessing tokens containing characters which are not members of the basic source character set.
qac-0299[I] Source file '%s' includes #pragma directives containing characters which are not members of the basic source character set.
+
MSC12RecommendationDetect and remove code that has no effect or is never executed
+ + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-3110The left-hand operand of this ',' has no side effects.
qac-3112This statement has no side-effect - it can be removed.
qac-3307The operand of 'sizeof' is an expression with implied side effects, but they will not be evaluated.
qac-3404Statement contains a redundant * operator at top level. *p++ means *(p++) not (*p)++.
qac-3426Right hand side of comma expression has no side effect and its value is not used.
qac-3427Right hand side of logical operator has no side effect and its value is not used.
+
MSC13RecommendationDetect and remove unused values
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
rcma-1500The object '%1s' is declared but is not used within this project.
rcma-1502The object '%1s' is defined but is not used within this project.
qac-2980The value of this function parameter is never used before being modified.
qac-2981This initialization is redundant. The value of this object is never used before being modified.
qac-2982This assignment is redundant. The value of this object is never used before being modified.
qac-2983This assignment is redundant. The value of this object is never subsequently used.
qac-2984This operation is redundant. The value of the result is always '%1s'.
qac-2985This operation is redundant. The value of the result is always that of the left-hand operand.
qac-2986This operation is redundant. The value of the result is always that of the right-hand operand.
qac-3203The variable '%s' is set but never used.
qac-3205The identifier '%1s' is not used and could be removed.
qac-3206The parameter '%s' is not used in this function.
qac-3207File scope static, '%1s', is not used, and could be removed.
qac-3229File scope static, '%1s', is written but never used.
+
MSC14RecommendationDo not introduce unnecessary platform dependencies
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-0202[I] '-' character in '[]' conversion specification is implementation defined.
qac-0240[I] This file contains the control-M character at the end of a line.
qac-0241[I] This file contains the control-Z character - was this transferred from a PC?
qac-0242[I] This file contains the control-M character in the middle of a line.
qac-0243[I] Treating an invalid character as whitespace.
qac-0246[E] Binary integer constants are a language extension.
qac-0284[I] Multiple character constants have implementation defined values.
qac-0551[E] Cast may not operate on the left operand of the assignment operator.
qac-0581[I] Floating-point constant may be too small to be representable.
qac-0601[E] Function 'main()' is not of type 'int (void)' or 'int (int, char *[])'.
qac-0633[E] Empty structures and unions are a language extension.
qac-0634[I] Bit-field %1s in %2s has not been declared explicitly as unsigned or signed.
qac-0635[C99] Bit-field %1s in %2s has been declared with a type not explicitly supported.
qac-0660[C11] Defining an unnamed member in a struct or union.
qac-0662[C11] Accessing a member of an unnamed struct or union member.
qac-0830[E] Unrecognized text encountered after a preprocessing directive.
qac-0831[E] Use of '\\' in this '#include' line is a PC extension - this usage is non-portable.
qac-0840[E] Extra tokens at end of #include directive.
qac-0899[E] Unrecognized preprocessing directive has been ignored - assumed to be a language extension.
qac-1001[E] '#include %s' is a VMS extension.
qac-1002[E] '%s' is not a legal identifier in ISO C.
qac-1003[E] '#%s' is a language extension for in-line assembler. All statements located between #asm and #endasm will be ignored.
qac-1006[E] This in-line assembler construct is a language extension. The code has been ignored.
qac-1008[E] '#%s' is not a legal ISO C preprocessing directive.
qac-1012[E] Use of a C++ reference type ('type &') will be treated as a language extension.
qac-1014[E] Non-standard type specifier - this will be treated as a language extension.
qac-1015[E] '%s' is not a legal keyword in ISO C - this will be treated as a language extension.
qac-1019[E] '@ address' is not supported in ISO C - this will be treated as a language extension.
qac-1020[E] '__typeof__' is not supported in ISO C, and is treated as a language extension.
qac-1021[E] A statement expression is not supported in ISO C, and is treated as a language extension.
qac-1022[E] '__alignof__' is a language extension. It is mapped to the standard '_Alignof' operator.
qac-1026[E] The indicated @word construct has been ignored.
qac-1028[E] Use of the sizeof operator in a preprocessing directive is a language extension.
qac-1029[E] Whitespace encountered between backslash and new-line has been ignored.
qac-1034[E] Macro defined with named variable argument list. This is a language extension.
qac-1035[E] No macro arguments supplied for variable argument list. This is a language extension.
qac-1036[E] Comma before ## ignored in expansion of variadic macro. This is a language extension.
qac-1037[E] Arrays of length zero are a language extension.
qac-1038[E] The sequence ", ##__VA_ARGS__" is a language extension.
qac-1041[E] Empty aggregate initializers are a language extension.
qac-1042[E] Using I64 or UI64 as an integer constant suffix. This is a language extension.
qac-1043[E] Defining an anonymous union object. This is a language extension.
qac-1044[E] Defining an anonymous struct object. This is a language extension.
qac-1045[E] Use of the #include_next preprocessing directive is a language extension.
qac-1046[E] Function is being declared with default argument syntax. This is a language extension.
qac-1434[I] This enum constant is not representable in a 16 bit integer type.
qac-3664[E] Using a dot operator to access an individual bit is a language extension.
+
MSC15RecommendationDo not depend on undefined behavior
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-0160[U] Using unsupported conversion specifier number %1s.
qac-0161[U] Unknown length modifier used with 'i' or 'd' conversion specifier, number %1s.
qac-0162[U] Unknown length modifier used with 'o' conversion specifier, number %1s.
qac-0163[U] Unknown length modifier used with 'u' conversion specifier, number %1s.
qac-0164[U] Unknown length modifier used with 'x' conversion specifier, number %1s.
qac-0165[U] Unknown length modifier used with 'X' conversion specifier, number %1s.
qac-0166[U] Unknown length modifier used with 'f' conversion specifier, number %1s.
qac-0167[U] Unknown length modifier used with 'e' conversion specifier, number %1s.
qac-0168[U] Unknown length modifier used with 'E' conversion specifier, number %1s.
qac-0169[U] Unknown length modifier used with 'g' conversion specifier, number %1s.
qac-0170[U] Unknown length modifier used with 'G' conversion specifier, number %1s.
qac-0171[U] Unknown length modifier used with 'c' conversion specifier, number %1s.
qac-0172[U] Unknown length modifier used with '%%' conversion specifier, number %1s.
qac-0173[U] Unknown length modifier used with 's' conversion specifier, number %1s.
qac-0174[U] Unknown length modifier used with 'n' conversion specifier, number %1s.
qac-0175[U] Unknown length modifier used with 'p' conversion specifier, number %1s.
qac-0176[U] Incomplete conversion specifier, number %1s.
qac-0177[U] Field width of format conversion specifier exceeds 509 characters.
qac-0178[U] Precision of format conversion specifier exceeds 509 characters.
qac-0179[U] Argument type does not match conversion specifier number %1s.
qac-0184[U] Insufficient arguments to satisfy conversion specifier, number %1s.
qac-0185[U] Call contains more arguments than conversion specifiers.
qac-0186[U] A call to this function must include at least one argument.
qac-0190[U] Using unsupported conversion specifier number %1s.
qac-0191[U] Unknown length modifier used with 'd/i/n' conversion specifier, number %1s.
qac-0192[U] Unknown length modifier used with 'o' conversion specifier, number %1s.
qac-0193[U] Unknown length modifier used with 'u' conversion specifier, number %1s.
qac-0194[U] Unknown length modifier used with 'x/X' conversion specifier, number %1s.
qac-0195[U] Unknown length modifier used with 'e/E/f/F/g/G' conversion specifier, number %1s.
qac-0196[U] Unknown length modifier used with 's' conversion specifier, number %1s.
qac-0197[U] Unknown length modifier used with 'p' conversion specifier, number %1s.
qac-0198[U] Unknown length modifier used with '%%' conversion specifier, number %1s.
qac-0199[U] Unknown length modifier used with '[' conversion specifier, number %1s.
qac-0200[U] Unknown length modifier used with 'c' conversion specifier, number %1s.
qac-0201[U] Incomplete conversion specifier, number %1s.
qac-0203[U] Value of character prior to '-' in '[]' is greater than following character.
qac-0204[U] Field width of format conversion specifier exceeds 509 characters.
qac-0206[U] Argument type does not match conversion specifier number %1s.
qac-0207[U] 'scanf' expects address of objects being stored into.
qac-0208[U] Same character occurs in scanset more than once.
qac-0235[U] Unknown escape sequence.
qac-0275[U] Floating value is out of range for conversion to destination type.
qac-0301[u] Cast between a pointer to object and a floating type.
qac-0302[u] Cast between a pointer to function and a floating type.
qac-0304[U] The address of an array declared 'register' may not be computed.
qac-0307[u] Cast between a pointer to object and a pointer to function.
qac-0309[U] Integral type is not large enough to hold a pointer value.
qac-0323[C] Cast between a pointer to incomplete type and a floating type.
qac-0327[C] Cast between a pointer to void and a floating type.
qac-0337[U] String literal has undefined value. This may be a result of using '#' on \\.
qac-0400[U] '%1s' is modified more than once between sequence points - evaluation order unspecified.
qac-0401[U] '%1s' may be modified more than once between sequence points - evaluation order unspecified.
qac-0402[U] '%1s' is modified and accessed between sequence points - evaluation order unspecified.
qac-0403[U] '%1s' may be modified and accessed between sequence points - evaluation order unspecified.
qac-0475[u] Operand of 'sizeof' is an expression designating a bit-field.
qac-0543[U] 'void' expressions have no value and may not be used in expressions.
qac-0544[U] The value of an incomplete 'union' may not be used.
qac-0545[U] The value of an incomplete 'struct' may not be used.
qac-0602[U] The identifier '%s' is reserved for use by the library.
qac-0603[U] The macro identifier '%s' is reserved.
qac-0623[U] '%s' has incomplete type and no linkage - this is undefined.
qac-0625[U] '%s' has been declared with both internal and external linkage - the behaviour is undefined.
qac-0626[U] '%s' has different type to previous declaration (which is no longer in scope).
qac-0630[U] More than one definition of '%s' (with external linkage).
qac-0632[U] Tentative definition of '%s' with internal linkage cannot have unknown size.
qac-0636[U] There are no named members in this 'struct' or 'union'.
qac-0654[U] Using 'const' or 'volatile' in a function return type is undefined.
qac-0658[U] Parameter cannot have 'void' type.
qac-0661[U] '%1s()' may not have a storage class specifier of '%2s' when declared at block scope.
qac-0667[U] '%s' is declared as a typedef and may not be redeclared as an object at an inner scope without an explicit type specifier.
qac-0668[U] '%s' is declared as a typedef and may not be redeclared as a member of a 'struct' or 'union' without an explicit type specifier.
qac-0672[U] The initializer for a 'struct', 'union' or array is not enclosed in braces.
qac-0676[u] Array element is of function type. Arrays cannot be constructed from function types.
qac-0678[u] Array element is array of unknown size. Arrays cannot be constructed from incomplete types.
qac-0680[u] Array element is 'void' or an incomplete 'struct' or 'union'. Arrays cannot be constructed from incomplete types.
qac-0706[U] Label '%s' is not unique within this function.
qac-0745[U] 'return;' found in '%s()', which has been defined with a non-'void' return type.
qac-0777[U] External identifier does not differ from other identifier(s) (e.g. '%1s') within the specified number of significant characters.
qac-0779[U] Identifier does not differ from other identifier(s) (e.g. '%1s') within the specified number of significant characters.
qac-0813[U] Using any of the characters ' " or /* in '#include <%s>' gives undefined behaviour.
qac-0814[U] Using the characters ' or /* in '#include "%s"' gives undefined behaviour.
qac-0821[C] '#include' does not identify a header or source file that can be processed.
qac-0836[U] Definition of macro named 'defined'.
qac-0837[U] Use of '#undef' to remove the operator 'defined'.
qac-0848[U] Attempting to #undef '%s', which is a predefined macro name.
qac-0853[U] Macro arguments contain a sequence of tokens that has the form of a preprocessing directive.
qac-0854[U] Attempting to #define '%s', which is a predefined macro name.
qac-0864[U] '#line' directive specifies line number which is not in the range 1 to 32767.
qac-0865[U] '#line' directive is badly formed.
qac-0867[U] '#line' has not been followed by a line number.
qac-0872[U] Result of '##' operator is not a legal preprocessing token.
qac-0874[U] Character string literal and wide character string literal are adjacent.
qac-0885[U] The token 'defined' is generated in the expansion of this macro.
qac-0887[U] Use of 'defined' must match either 'defined(identifier)' or 'defined identifier'.
qac-0888[U] 'defined' requires an identifier as an argument.
qac-0914[U] Source file does not end with a newline character.
qac-0915[U] Source file ends with a backslash character followed by a newline.
qac-0942[U] A * can only be used to specify array size within function prototype scope.
rcma-1509'%1s' has external linkage and has multiple definitions.
rcma-1510'%1s' has external linkage and has incompatible declarations.
qac-3113[U] 'return' statement includes no expression but function '%s()' is implicitly of type 'int'.
qac-3114[U] Function '%s()' is implicitly of type 'int' but ends without returning a value.
qac-3239[U] inline function '%1s' has external linkage, but is not defined within this translation unit.
qac-3311[u] An earlier jump to this statement will bypass the initialization of local variables.
qac-3312[u] This goto statement will jump into a previous block and bypass the initialization of local variables.
qac-3319[U] Function called with number of arguments which differs from number of parameters in definition.
qac-3437[u] '%1s' is only provided by the standard library as a macro, but is being used as a value here.
qac-3438[U] #undef'ing the assert macro to call a function of that name causes undefined behaviour.
+
MSC17RecommendationFinish every set of statements associated with a case label with a break statement
+ + + + + +
QacDescription
qac-2003The preceding 'switch' clause is not empty and does not end with a 'jump' statement. Execution will fall through.
+
MSC20RecommendationDo not use a switch statement to transfer control into a complex block
+ + + + + +
QacDescription
qac-2019'Switch' label is located within a nested code block.
+
MSC30RuleDo not use the rand() function for generating pseudorandom numbers
+ + + + + +
QacDescription
certccm-5022'%s' is being used.
+
MSC32RuleProperly seed pseudorandom number generators
+ + + + + +
QacDescription
certccm-5031PRNG is not seeded before usage.
+
MSC33RuleDo not pass invalid data to the asctime() function
+ + + + + +
QacDescription
certccm-5032'%s' is being used.
+
MSC37RuleEnsure that control never reaches the end of a non-void function
+ + + + + +
QacDescription
qac-2888This function has been declared with a non-void 'return' type but ends with an implicit 'return ;' statement.
+
MSC38RuleDo not treat a predefined identifier as an object if it might only be implemented as a macro
+ + + + + + + + + +
QacDescription
qac-3437[u] '%1s' is only provided by the standard library as a macro, but is being used as a value here.
qac-3475[u] '%1s' is only provided by the standard library as a macro, but is being declared as a value here.
+
MSC39RuleDo not call va_arg() on a va_list that has an indeterminate value
+ + + + + +
QacDescription
qac-3497[U] The va_list object '%1s' is both passed as an argument and accessed with va_arg.
+
MSC40RuleDo not violate constraints
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-0232[C] Value of hex escape sequence is not representable in type 'unsigned char'.
qac-0233[C] Value of octal escape sequence is not representable in type 'unsigned char'.
qac-0244[C] Value of character constant is not representable in type 'int'.
qac-0268[S] Comment open at end of translation unit.
qac-0278[C] Overflow in signed arithmetic operation on constant operands.
qac-0321[C] Declaration within 'for' statement defines an identifier '%1s' which is not an object.
qac-0322[C] Illegal storage class specifier used in 'for' statement declaration.
qac-0338[C] Octal or hex escape sequence value is too large for 'unsigned char' or 'wchar_t' type.
qac-0422[C] Function call contains fewer arguments than prototype specifies.
qac-0423[C] Function call contains more arguments than prototype specifies.
qac-0426[C] Called function has incomplete return type.
qac-0427[C] Object identifier used as if it were a function or a function pointer identifier.
qac-0429[C] Function argument is not of arithmetic type.
qac-0430[C] Function argument is not of compatible 'struct'/'union' type.
qac-0431[C] Function argument points to a more heavily qualified type.
qac-0432[C] Function argument is not of compatible pointer type.
qac-0435[C] The 'struct'/'union' member '%s' does not exist.
qac-0436[C] Left operand of '.' must be a 'struct' or 'union' object.
qac-0437[C] Left operand of '->' must be a pointer to a 'struct' or 'union' object.
qac-0446[C] Operand of ++/-- must have scalar (arithmetic or pointer) type.
qac-0447[C] Operand of ++/-- must be a modifiable object.
qac-0448[C] Operand of ++/-- must not be a pointer to an object of unknown size.
qac-0449[C] Operand of ++/-- must not be a pointer to a function.
qac-0451[C] Subscripting requires a pointer (or array lvalue).
qac-0452[C] Cannot subscript a pointer to an object of unknown size.
qac-0453[C] An array subscript must have integral type.
qac-0454[C] The address-of operator '&' cannot be applied to an object declared with 'register'.
qac-0456[C] This expression does not have an address - '&' may only be applied to an lvalue or a function designator.
qac-0457[C] The address-of operator '&' cannot be applied to a bit-field.
qac-0458[C] Indirection operator '*' requires operand of pointer type.
qac-0460[C] The keyword static is used in the declaration of the index of an array which is not a function parameter.
qac-0461[C] The keyword static is used in the declaration of an inner index of a multi-dimensional array.
qac-0462[C] A type qualifier (const, volatile or restrict) is used in the declaration of the index of an array which is not a function parameter.
qac-0463[C] A type qualifier (const, volatile or restrict) is used in the declaration of an inner index of a multi-dimensional array.
qac-0466[C] Unary '+' requires arithmetic operand.
qac-0467[C] Operand of '!' must have scalar (arithmetic or pointer) type.
qac-0468[C] Unary '-' requires arithmetic operand.
qac-0469[C] Bitwise not '~' requires integral operand.
qac-0476[C] 'sizeof' cannot be applied to a bit-field.
qac-0477[C] 'sizeof' cannot be applied to a function.
qac-0478[C] 'sizeof' cannot be applied to an object of unknown size.
qac-0481[C] Only scalar expressions may be cast to other types.
qac-0482[C] Expressions may only be cast to 'void' or scalar types.
qac-0483[C] A pointer to an object of unknown size cannot be the operand of an addition operator.
qac-0484[C] A pointer to an object of unknown size cannot be the operand of a subtraction operator.
qac-0485[C] Only integral expressions may be added to pointers.
qac-0486[C] Only integral expressions and compatible pointers may be subtracted from pointers.
qac-0487[C] If two pointers are subtracted, they must be pointers that address compatible types.
qac-0493[C] Type of left operand is not compatible with this operator.
qac-0494[C] Type of right operand is not compatible with this operator.
qac-0495[C] Left operand of '%', '<<', '>>', '&', '^' or '|' must have integral type.
qac-0496[C] Right operand of '%', '<<', '>>', '&', '^' or '|' must have integral type.
qac-0513[C] Relational operator used to compare pointers to incompatible types.
qac-0514[C] Relational operator used to compare a pointer with an incompatible operand.
qac-0515[C] Equality operator used to compare a pointer with an incompatible operand.
qac-0536[C] First operand of '&&', '||' or '?' must have scalar (arithmetic or pointer) type.
qac-0537[C] Second operand of '&&' or '||' must have scalar (arithmetic or pointer) type.
qac-0540[C] 2nd and 3rd operands of conditional operator '?' must have compatible types.
qac-0541[C] Argument no. %s does not have object type.
qac-0542[C] Controlling expression must have scalar (arithmetic or pointer) type.
qac-0546[C] 'enum %s' has unknown content. Use of an enum tag with undefined content is not permitted.
qac-0547[C] This declaration of tag '%s' conflicts with a previous declaration.
qac-0550[C] Left operand of '+=' or '-=' is a pointer to an object of unknown size.
qac-0554[C] 'static %s()' has been declared and called but no definition has been given.
qac-0555[C] Invalid assignment to object of void type or array type.
qac-0556[C] Left operand of assignment must be a modifiable object.
qac-0557[C] Right operand of assignment is not of arithmetic type.
qac-0558[C] Right operand of '+=' or '-=' must have integral type when left operand is a pointer.
qac-0559[C] Right operand of '<<=', '>>=', '&=', '|=', '^=' or '%=' must have integral type.
qac-0560[C] Left operand of '<<=', '>>=', '&=', '|=', '^=' or '%=' must have integral type.
qac-0561[C] Right operand of assignment is not of compatible 'struct'/'union' type.
qac-0562[C] Right operand of assignment points to a more heavily qualified type.
qac-0563[C] Right operand of assignment is not of compatible pointer type.
qac-0564[C] Left operand of assignment must be an lvalue (it must designate an object).
qac-0565[C] Left operand of '+=' or '-=' must be of arithmetic or pointer to object type.
qac-0580[C] Constant is too large to be representable.
qac-0588[C] Width of bit-field must be an integral constant expression.
qac-0589[C] Enumeration constant must be an integral constant expression.
qac-0590[C] Array bound must be an integral constant expression.
qac-0591[C] A 'case' label must be an integral constant expression.
qac-0605[C] A declaration must declare a tag or an identifier.
qac-0616[C] Illegal combination of type specifiers or storage class specifiers.
qac-0619[C] The identifier '%1s' has already been defined in the current scope within the ordinary identifier namespace.
qac-0620[C] Cannot initialize '%s' because it has unknown size.
qac-0621[C] The struct/union '%s' cannot be initialized because it has unknown size.
qac-0622[C] The identifier '%s' has been declared both with and without linkage in the same scope.
qac-0627[C] '%s' has different type to previous declaration in the same scope.
qac-0628[C] '%s' has different type to previous declaration at wider scope.
qac-0629[C] More than one definition of '%s' (with internal linkage).
qac-0631[C] More than one declaration of '%s' (with no linkage).
qac-0638[C] Duplicate member name '%s' in 'struct' or 'union'.
qac-0640[C] '%s' in 'struct' or 'union' type may not have 'void' type.
qac-0641[C] '%s' in 'struct' or 'union' type may not have function type.
qac-0642[C] '%s' in 'struct' or 'union' type may not be an array of unknown size.
qac-0643[C] '%s' in 'struct' or 'union' type may not be a 'struct' or 'union' with unknown content.
qac-0644[C] Width of bit-field must be no bigger than the width of an 'int'.
qac-0645[C] A zero width bit-field cannot be given a name.
qac-0646[C] Enumeration constants must have values representable as 'int's.
qac-0649[C] K&R style declaration of parameters is not legal after a function header that includes a parameter list.
qac-0650[C] Illegal storage class specifier on named function parameter.
qac-0651[C] Missing type specifiers in function declaration.
qac-0653[C] Duplicate definition of 'struct', 'union' or 'enum' tag '%s'.
qac-0655[C] Illegal storage class specifier on unnamed function parameter.
qac-0656[C] Function return type cannot be function or array type, or an incomplete struct/union (for function definition).
qac-0657[C] Unnamed parameter specified in function definition.
qac-0659[C] The identifier '%s' was not given in the parameter list.
qac-0664[C] Parameter specified with type 'void'.
qac-0665[C] Two parameters have been declared with the same name '%1s'.
qac-0669[C] The restrict qualifier can only be applied to pointer types derived from object or incomplete types.
qac-0671[C] Initializer for object of arithmetic type is not of arithmetic type.
qac-0673[C] Initializer points to a more heavily qualified type.
qac-0674[C] Initializer for pointer is of incompatible type.
qac-0675[C] Initializer is not of compatible 'struct'/'union' type.
qac-0677[C] Array size is negative, or unrepresentable.
qac-0682[C] Initializer for object of a character type is a string literal.
qac-0683[C] Initializer for object of a character type is a wide string literal.
qac-0684[C] Too many initializers.
qac-0685[C] Initializer for any object with static storage duration must be a constant expression.
qac-0690[C] String literal contains too many characters to initialize object.
qac-0698[C] String literal used to initialize an object of incompatible type.
qac-0699[C] String literal used to initialize a pointer of incompatible type.
qac-0708[C] No definition found for the label '%s' in this function.
qac-0709[C] Initialization of locally declared 'extern %s' is illegal.
qac-0736[C] 'case' label does not have unique value within this 'switch' statement.
qac-0737[C] More than one 'default' label found in 'switch' statement.
qac-0738[C] Controlling expression in a 'switch' statement must have integral type.
qac-0746[C] 'return exp;' found in '%s()' whose return type is 'void'.
qac-0747[C] 'return exp;' found in '%s()' whose return type is qualified 'void'.
qac-0755[C] 'return' expression is not of arithmetic type.
qac-0756[C] 'return' expression is not of compatible 'struct'/'union' type.
qac-0757[C] 'return' expression points to a more heavily qualified type.
qac-0758[C] 'return' expression is not of compatible pointer type.
qac-0766[C] 'continue' statement found outside an iteration statement.
qac-0767[C] 'break' statement found outside a 'switch' or iteration statement.
qac-0768[C] 'case' or 'default' found outside a 'switch' statement.
qac-0774[C] 'auto' may not be specified on global declaration of '%s'.
qac-0775[C] 'register' may not be specified on global declaration of '%s'.
qac-0801[C] The '##' operator may not be the first token in a macro replacement list.
qac-0802[C] The '##' operator may not be the last token in a macro replacement list.
qac-0803[C] The '#' operator may only appear before a macro parameter.
qac-0804[C] Macro parameter '%s' is not unique.
qac-0811[C] The glue operator '##' may only appear in a '#define' preprocessing directive.
qac-0821[C] '#include' does not identify a header or source file that can be processed.
qac-0834[C] Function-like macro '%s()' is being redefined as an object-like macro.
qac-0835[C] Macro '%s' is being redefined with different parameter names.
qac-0844[C] Macro '%s' is being redefined with a different replacement list.
qac-0845[C] Object-like macro '%s' is being redefined as a function-like macro.
qac-0851[C] More arguments in macro call than specified in definition.
qac-0852[S] Unable to find the ')' that marks the end of the macro call.
qac-0866[C] The string literal in a '#line' directive cannot be a 'wide string literal'.
qac-0873[C] Preprocessing token cannot be converted to an actual token.
qac-0877[C] '#if' and '#elif' expressions may contain only integral constants.
qac-0940[C] Illegal usage of a variably modified type.
qac-0941[C] A variable length array may not be initialized.
qac-0943[C] Jump to label '%s' is a jump into the scope of an identifier with variably modified type.
qac-0944[C] The label '%s' is inside the scope of an identifier with variably modified type.
qac-1023[C] Using '_Alignof' on function types is illegal.
qac-1024[C] Using '_Alignof' on incomplete types is illegal.
qac-1025[C] Using '_Alignof' on bit-fields is illegal.
qac-1033[C] The identifier __VA_ARGS__ may only be used in the replacement list of a variadic macro.
qac-1047[EE] Function is being declared with default argument syntax after a previous call to the function. This is not allowed.
qac-1048[EE] Default argument values are missing for some parameters in this function declaration. This is not allowed.
qac-1050[EE] Nested functions cannot be 'extern' or 'static'.
qac-1061[C] Structure '%1s' with flexible array member '%2s' cannot be used in the declaration of structure member '%3s'.
qac-1062[C] Structure '%1s' with flexible array member '%2s' cannot be used in the declaration of array elements.
qac-3236[C] 'inline' may not be applied to function 'main'.
qac-3237[C] inline function '%1s' has external linkage and is defining an object, '%2s', with static storage duration.
qac-3238[C] inline function '%1s' has external linkage and is referring to an object, '%2s', with internal linkage.
qac-3244[C] 'inline' may only be used in the declaration of a function identifier.
+
MSC41RuleNever hard code sensitive information
+ + + + + +
QacDescription
qac-3122Hard-coded 'magic' string literal, %1s.
+
POS30Rule_OptionalUse the readlink() function properly
+ + + + + +
QacDescription
certccm-5033'%s' is being used.
+
POS33Rule_OptionalDo not use vfork()
+ + + + + +
QacDescription
certccm-5023'%s' is being used.
+
POS34Rule_OptionalDo not call putenv() with a pointer to an automatic variable as the argument
+ + + + + +
QacDescription
certccm-5024'%s' is being used.
+
POS35Rule_OptionalAvoid race conditions while checking for the existence of a symbolic link
+ + + + + + + + + + + + + +
QacDescription
qac-4886Definite: Time-of-check time-of-use race condition when checking for the existence of a symbolic link.
qac-4887Apparent: Time-of-check time-of-use race condition when checking for the existence of a symbolic link.
qac-4888Suspicious: Time-of-check time-of-use race condition when checking for the existence of a symbolic link.
+
POS36Rule_OptionalObserve correct revocation order while relinquishing privileges
+ + + + + + + + + + + + + +
QacDescription
qac-4891Definite: Revocation order was not observed while relinquishing privileges.
qac-4892Apparent: Revocation order was not observed while relinquishing privileges.
qac-4893Suspicious: Revocation order was not observed while relinquishing privileges.
+
POS37Rule_OptionalEnsure that privilege relinquishment is successful
+ + + + + + + + + + + + + +
QacDescription
qac-4876Definite: Permanent user rights drop success is not checked.
qac-4877Apparent: Permanent user rights drop success is not checked.
qac-4878Suspicious: Permanent user rights drop success is not checked.
+
POS38Rule_OptionalBeware of race conditions when using fork and file descriptors
+ + + + + + + + + +
QacDescription
qac-4951Definite: Beware of race conditions when using fork and file descriptors.
qac-4952Apparent: Beware of race conditions when using fork and file descriptors.
+
POS39Rule_OptionalUse the correct byte ordering when transferring data between systems
+ + + + + + + + + + + + + +
QacDescription
qac-4906Definite: Data may not be correctly converted between host and network byte order.
qac-4907Apparent: Data may not be correctly converted between host and network byte order.
qac-4908Suspicious: Data may not be correctly converted between host and network byte order.
+
POS44Rule_OptionalDo not use signals to terminate threads
+ + + + + +
QacDescription
certccm-5034'%s' is being used.
+
POS47Rule_OptionalDo not use threads that can be canceled asynchronously
+ + + + + +
QacDescription
certccm-5035Asynchronous thread cancellation is used.
+
POS48Rule_OptionalDo not unlock or destroy another POSIX thread's mutex
+ + + + + + + + + + + + + + + + + +
QacDescription
qac-4971Definite: Attempt to destroy a mutex which has not been created by the current thread
qac-4972Apparent: Attempt to destroy a mutex which has not been created by the current thread
qac-4981Definite: Attempt to unlock a mutex which has not been locked by the current thread
qac-4982Apparent: Attempt to unlock a mutex which has not been locked by the current thread
+
POS49Rule_OptionalWhen data must be accessed by multiple threads, provide a mutex and guarantee no adjacent data is also accessed
+ + + + + + + + + +
QacDescription
mta-1774Definite: data race for an object '%1s' which shares its physical storage location with one or more others.
mta-1775Apparent: data race for an object '%1s' which shares its physical storage location with one or more others.
+
POS50Rule_OptionalDeclare objects shared between POSIX threads with appropriate storage durations
+ + + + + + + + + + + + + +
QacDescription
qac-4926Definite: The lifetime of the variable passed to the thread creation function is shorter than the lifetime of the thread.
qac-4927Apparent: The lifetime of the variable passed to the thread creation function is shorter than the lifetime of the thread.
qac-4928Suspicious: The lifetime of the variable passed to the thread creation function is shorter than the lifetime of the thread.
+
POS51Rule_OptionalAvoid deadlock with POSIX threads by locking in predefined order
+ + + + + + + + + +
QacDescription
mta-1772Mutex '%1s' violates the lock hierarchy, as it is acquired before '%2s' elsewhere %3s% of the time.
mta-1773Mutexes '%1s' and '%2s' are not ordered in the lock hierarchy.
+
POS52Rule_OptionalDo not perform operations that can block while holding a POSIX lock
+ + + + + + + + + +
QacDescription
qac-4966Definite: Performing a blocking action while holding a POSIX lock.
qac-4967Apparent: Performing a blocking action while holding a POSIX lock.
+
POS53Rule_OptionalDo not use more than one mutex for concurrent waiting operations on a condition variable
+ + + + + +
QacDescription
mta-1769Condition variable '%1s' used with multiple mutexes.
+
POS54Rule_OptionalDetect and handle POSIX library errors
+ + + + + +
QacDescription
qac-3200'%s' returns a value which is not being used.
+
PRE00RecommendationPrefer inline or static functions to function-like macros
+ + + + + +
QacDescription
qac-3453A function could probably be used instead of this function-like macro.
+
PRE01RecommendationUse parentheses within macros around parameter names
+ + + + + +
QacDescription
qac-3410Macro parameter not enclosed in ().
+
PRE02RecommendationMacro replacement lists should be parenthesized
+ + + + + +
QacDescription
qac-3409The replacement list of function-like macro '%1s' is not enclosed in ().
+
PRE03RecommendationPrefer typedefs to defines for encoding non-pointer types
+ + + + + +
QacDescription
qac-3413Macro definition could be replaced by a typedef.
+
PRE04RecommendationDo not reuse a standard header file name
+ + + + + +
QacDescription
certccm-5001The header '%s' is a standard header file name.
+
PRE05RecommendationUnderstand macro replacement when concatenating tokens or performing stringification
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-0341Using the stringify operator '#'.
qac-0342Using the glue operator '##'.
qac-0801[C] The '##' operator may not be the first token in a macro replacement list.
qac-0802[C] The '##' operator may not be the last token in a macro replacement list.
qac-0803[C] The '#' operator may only appear before a macro parameter.
qac-0811[C] The glue operator '##' may only appear in a '#define' preprocessing directive.
qac-0872[U] Result of '##' operator is not a legal preprocessing token.
qac-0880Using # and ## operators in the same macro definition.
qac-0881Using multiple ## operators in the same macro definition.
qac-0884Using multiple # operators in the same macro definition.
+
PRE06RecommendationEnclose header files in an inclusion guard
+ + + + + +
QacDescription
qac-0883Include file code is not protected against repeated inclusion
+
PRE07RecommendationAvoid using repeated question marks
+ + + + + +
QacDescription
qac-3601Trigraphs (??x) are an ISO feature.
+
PRE08RecommendationGuarantee that header file names are unique
+ + + + + +
QacDescription
certccm-5002The header '%s' file name shadows a previously included header.
+
PRE09RecommendationDo not replace secure functions with deprecated or obsolescent functions
+ + + + + +
QacDescription
certccm-5003The obsolete function '%s' will be used as a result of this macro expansion
+
PRE10RecommendationWrap multi-statement macros in a do-while loop
+ + + + + + + + + +
QacDescription
qac-3412Macro defines an unrecognized code-fragment.
qac-3458Macro defines a braced code statement block.
+
PRE11RecommendationDo not conclude macro definitions with a semicolon
+ + + + + +
QacDescription
qac-3412Macro defines an unrecognized code-fragment.
+
PRE12RecommendationDo not define unsafe macros
+ + + + + +
QacDescription
qac-3456Parameter '%1s' will be evaluated more than once when this macro is used.
+
PRE30RuleDo not create a universal character name through concatenation
+ + + + + +
QacDescription
qac-0905[U] Producing a universal character name through token concatenation is undefined.
+
PRE31RuleAvoid side effects in arguments to unsafe macros
+ + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-3462Argument to macro '%s' appears to contain a side effect, but it is not used.
qac-3463Argument to macro '%s' contains a side effect that will not be evaluated at runtime.
qac-3464Argument to macro '%s' contains a side effect that will be evaluated more than once.
qac-3465Argument to macro '%s' contains a side effect that might not be evaluated at runtime.
qac-3466Variable argument list to macro '%s' appears to contain an expression with side effects.
qac-3467Variable argument list to macro '%s' appears to contain an expression with side effects that will be used more than once.
+
PRE32RuleDo not use preprocessor directives in invocations of function-like macros
+ + + + + +
QacDescription
qac-0853[U] Macro arguments contain a sequence of tokens that has the form of a preprocessing directive.
+
SIG00RecommendationMask signals handled by noninterruptible signal handlers
+ + + + + +
QacDescription
certccm-5019'%s' is being used.
+
SIG01RecommendationUnderstand implementation-specific details regarding signal handler persistence
+ + + + + +
QacDescription
certccm-5020'%s' is being used.
+
SIG30RuleCall only asynchronous-safe functions within signal handlers
+ + + + + + + + + +
QacDescription
qac-2028Signal handler '%1s' calls async-unsafe functions.
qac-2030The signal handler passed here might not be async-safe.
+
SIG31RuleDo not access shared objects in signal handlers
+ + + + + + + + + +
QacDescription
qac-2029Signal handler '%1s' accesses shared objects.
qac-2030The signal handler passed here might not be async-safe.
+
SIG34RuleDo not call signal() from within interruptible signal handlers
+ + + + + +
QacDescription
certccm-5021'%s' is being used.
+
SIG35RuleDo not return from a computational exception signal handler
+ + + + + + + + + + + + + +
QacDescription
qac-4846Definite: Signal handler returns from a computational exception.
qac-4847Apparent: Signal handler returns from a computational exception.
qac-4848Suspicious: Signal handler returns from a computational exception.
+
STR04RecommendationUse plain char for characters in the basic character set
+ + + + + + + + + + + + + +
QacDescription
qac-0432[C] Function argument is not of compatible pointer type.
qac-0674[C] Initializer for pointer is of incompatible type.
qac-0699[C] String literal used to initialize a pointer of incompatible type.
+
STR05RecommendationUse pointers to const when referring to string literals
+ + + + + + + + + +
QacDescription
qac-0752String literal passed as argument to function whose parameter is not a 'pointer to const'.
qac-0753String literal assigned to pointer which is not a 'pointer to const'.
+
STR06RecommendationDo not assume that strtok() leaves the parse string unchanged
+ + + + + +
QacDescription
certccm-5007'%s' is being used.
+
STR07RecommendationUse the bounds-checking interfaces for string manipulation
+ + + + + +
QacDescription
certccm-5008'%s' is being used.
+
STR09RecommendationDon't assume numeric values for expressions with type plain character
+ + + + + + + + + +
QacDescription
qac-2106Integral promotion : plain char promoted to signed int.
qac-2107Integral promotion : plain char promoted to unsigned int.
+
STR10RecommendationDo not concatenate different type of string literals
+ + + + + +
QacDescription
qac-0874[U] Character string literal and wide character string literal are adjacent.
+
STR11RecommendationDo not specify the bound of a character array initialized with a string literal
+ + + + + +
QacDescription
qac-1312The array being initialized is not large enough to hold a terminating null byte for the string initializer.
+
STR30RuleDo not attempt to modify string literals
+ + + + + + + + + + + + + + + + + +
QacDescription
qac-0556[C] Left operand of assignment must be a modifiable object.
qac-0752String literal passed as argument to function whose parameter is not a 'pointer to const'.
qac-0753String literal assigned to pointer which is not a 'pointer to const'.
qac-0754[U] String literal is being modified.
+
STR31RuleGuarantee that storage for strings has sufficient space for character data and the null terminator
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-2840Constant: Dereference of an invalid pointer value.
qac-2841Definite: Dereference of an invalid pointer value.
qac-2842Apparent: Dereference of an invalid pointer value.
qac-2843Suspicious: Dereference of an invalid pointer value.
qac-2845Constant: Maximum number of characters to be read/written is larger than the target buffer size.
qac-2846Definite: Maximum number of characters to be read/written is larger than the target buffer size.
qac-2847Apparent: Maximum number of characters to be read/written is larger than the target buffer size.
qac-2848Suspicious: Maximum number of characters to be read/written is larger than the target buffer size.
qac-2930Constant: Computing an invalid pointer value.
qac-2931Definite: Computing an invalid pointer value.
qac-2932Apparent: Computing an invalid pointer value.
qac-2933Suspicious: Computing an invalid pointer value.
qac-2935Constant: Dereference of an invalid char pointer value.
qac-2936Definite: Dereference of an invalid char pointer value.
qac-2937Apparent: Dereference of an invalid char pointer value.
qac-2938Suspicious: Dereference of an invalid char pointer value.
certccm-5009'%s' is being used.
certccm-5038'%s' is being used.
+
STR32RuleDo not pass a non-null-terminated character sequence to a library function that expects a string
+ + + + + + + + + + + + + +
QacDescription
qac-2835Constant: Non-null terminated string used in a string function.
qac-2836Definite: Non-null terminated string used in a string function.
qac-2839Possible: Non-null terminated string used in a string function.
+
STR34RuleCast characters to unsigned char before converting to larger integer sizes
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-2140Implicit conversion from plain char to wider signed integer type.
qac-2141Implicit conversion from plain char to wider unsigned integer type.
qac-2143Implicit conversion from plain char to enum type.
qac-2144Implicit conversion from signed char to wider signed integer type.
qac-2145Implicit conversion from signed char to wider unsigned integer type.
qac-2147Implicit conversion from signed char to enum type.
qac-2148Explicit conversion from plain char to wider signed integer type.
qac-2149Explicit conversion from plain char to wider unsigned integer type.
qac-2151Explicit conversion from plain char to enum type.
qac-2152Explicit conversion from signed char to wider signed integer type.
qac-2153Explicit conversion from signed char to wider unsigned integer type.
qac-2155Explicit conversion from signed char to enum type.
+
STR37RuleArguments to character-handling functions must be representable as an unsigned char
+ + + + + + + + + +
QacDescription
qac-4413An expression of 'essentially character' type (%1s) is being converted to signed type, '%2s' on assignment.
qac-4414An expression of 'essentially character' type (%1s) is being converted to unsigned type, '%2s' on assignment.
+
STR38RuleDo not confuse narrow and wide character strings and functions
+ + + + + +
QacDescription
qac-0432[C] Function argument is not of compatible pointer type.
+
+
+
+ +This section targets to provide an overview of Guidelines Recategorization Plan. +
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
GuidelineDescriptionCategoryRevised Category
ARR02Explicitly specify array bounds, even if implicitly defined by an initializerRecommendationRecommendation
ARR30Do not form or use out-of-bounds pointers or array subscriptsRuleRule
ARR32Ensure size arguments for variable length arrays are in a valid rangeRuleRule
ARR36Do not subtract or compare two pointers that do not refer to the same arrayRuleRule
ARR37Do not add or subtract an integer to a pointer to a non-array objectRuleRule
ARR38Guarantee that library functions do not form invalid pointersRuleRule
ARR39Do not add or subtract a scaled integer to a pointerRuleRule
CON30Clean up thread-specific storageRuleRule
CON31Do not destroy a mutex while it is lockedRuleRule
CON32Prevent data races when accessing bit-fields from multiple threadsRuleRule
CON33Avoid race conditions when using library functionsRuleRule
CON34Declare objects shared between threads with appropriate storage durationsRuleRule
CON35Avoid deadlock by locking in a predefined orderRuleRule
CON36Wrap functions that can spuriously wake up in a loopRuleRule
CON37Do not call signal() in a multithreaded programRuleRule
CON38Preserve thread safety and liveness when using condition variablesRuleRule
CON39Do not join or detach a thread that was previously joined or detachedRuleRule
CON40Do not refer to an atomic variable twice in an expressionRuleRule
CON41Wrap functions that can fail spuriously in a loopRuleRule
CON43Do not allow data races in multithreaded codeRuleRule
DCL00Const-qualify immutable objectsRecommendationRecommendation
DCL01Do not reuse variable names in subscopesRecommendationRecommendation
DCL05Use typedefs of non-pointer types onlyRecommendationDisapplied
DCL06Use meaningful symbolic constants to represent literal valuesRecommendationRecommendation
DCL07Include the appropriate type information in function declaratorsRecommendationRecommendation
DCL10Maintain the contract between the writer and caller of variadic functionsRecommendationRecommendation
DCL11Understand the type issues associated with variadic functionsRecommendationRecommendation
DCL13Declare function parameters that are pointers to values not changed by the function as constRecommendationRecommendation
DCL15Declare file-scope objects or functions that do not need external linkage as staticRecommendationRecommendation
DCL16Use 'L', not 'l', to indicate a long valueRecommendationRecommendation
DCL18Do not begin integer constants with 0 when specifying a decimal valueRecommendationRecommendation
DCL19Minimize the scope of variables and functionsRecommendationRecommendation
DCL20Explicitly specify void when a function accepts no argumentsRecommendationRecommendation
DCL21Understand the storage of compound literalsRecommendationRecommendation
DCL23Guarantee that mutually visible identifiers are uniqueRecommendationRecommendation
DCL30Declare objects with appropriate storage durationsRuleRule
DCL31Declare identifiers before using themRuleRule
DCL36Do not declare an identifier with conflicting linkage classificationsRuleRule
DCL37Do not declare or define a reserved identifierRuleRule
DCL38Use the correct syntax when declaring a flexible array memberRuleRule
DCL39Avoid information leakage when passing a structure across a trust boundaryRuleRule
DCL40Do not create incompatible declarations of the same function or objectRuleRule
DCL41Do not declare variables inside a switch statement before the first case labelRuleRule
ENV03Sanitize the environment when invoking external programsRecommendationRecommendation
ENV30Do not modify the object referenced by the return value of certain functionsRuleRule
ENV31Do not rely on an environment pointer following an operation that may invalidate itRuleRule
ENV32All exit handlers must return normallyRuleRule
ENV33Do not call system()RuleRule
ENV34Do not store pointers returned by certain functionsRuleRule
ERR30Set errno to zero before calling a library function known to set errno, and check errno only after the function returns a value indicating failureRuleRule
ERR32Do not rely on indeterminate values of errnoRuleRule
ERR33Detect and handle standard library errorsRuleDisapplied
ERR34Detect errors when converting a string to a numberRuleRule
EXP00Use parentheses for precedence of operationRecommendationRecommendation
EXP02Be aware of the short-circuit behavior of the logical AND and OR operatorsRecommendationRecommendation
EXP03Do not assume the size of a structure is the sum of the sizes of its membersRecommendationRecommendation
EXP05Do not cast away a const qualificationRecommendationRecommendation
EXP07Do not diminish the benefits of constants by assuming their values in expressionsRecommendationRecommendation
EXP08Ensure pointer arithmetic is used correctlyRecommendationRecommendation
EXP10Do not depend on the order of evaluation of subexpressions or the order in which side effects take placeRecommendationRecommendation
EXP11Do not make assumptions regarding the layout of structures with bit-fieldsRecommendationRecommendation
EXP12Do not ignore values returned by functionsRecommendationDisapplied
EXP13Treat relational and equality operators as if they were nonassociativeRecommendationRecommendation
EXP15Do not place a semicolon on the same line as an if, for, or while statementRecommendationDisapplied
EXP16Do not compare function pointers to constant valuesRecommendationRecommendation
EXP19Use braces for the body of an if, for, or while statementRecommendationRecommendation
EXP20Perform explicit tests to determine success, true and false, and equalityRecommendationRecommendation
EXP30Do not depend on the order of evaluation for side effectsRuleRule
EXP32Do not access a volatile object through a nonvolatile referenceRuleRule
EXP33Do not read uninitialized memoryRuleRule
EXP34Do not dereference null pointersRuleRule
EXP35Do not modify objects with temporary lifetimeRuleRule
EXP36Do not cast pointers into more strictly aligned pointer typesRuleRule
EXP37Call functions with the correct number and type of argumentsRuleRule
EXP39Do not access a variable through a pointer of an incompatible typeRuleRule
EXP40Do not modify constant objectsRuleRule
EXP42Do not compare padding dataRuleRule
EXP43Avoid undefined behavior when using restrict-qualified pointersRuleRule
EXP44Do not rely on side effects in operands to sizeof, _Alignof, or _GenericRuleRule
EXP45Do not perform assignments in selection statementsRuleRule
EXP46Do not use a bitwise operator with a Boolean-like operandRuleRule
EXP47Do not call va_arg with an argument of the incorrect typeRuleRule
FIO01Be careful using functions that use file names for identificationRecommendationRecommendation
FIO03Do not make assumptions about fopen() and file creationRecommendationRecommendation
FIO06Create files with appropriate access permissionsRecommendationRecommendation
FIO08Take care when calling remove() on an open fileRecommendationRecommendation
FIO10Take care when using the rename() functionRecommendationRecommendation
FIO21Do not create temporary files in shared directoriesRecommendationRecommendation
FIO30Exclude user input from format stringsRuleRule
FIO32Do not perform operations on devices that are only appropriate for filesRuleRule
FIO34Distinguish between characters read from a file and EOF or WEOFRuleRule
FIO37Do not assume that fgets() or fgetws() returns a nonempty string when successfulRuleRule
FIO38Do not copy a FILE objectRuleRule
FIO39Do not alternately input and output from a stream without an intervening flush or positioning callRuleRule
FIO40Reset strings on fgets() or fgetws() failureRuleRule
FIO41Do not call getc(), putc(), getwc(), or putwc() with a stream argument that has side effectsRuleRule
FIO42Close files when they are no longer neededRuleRule
FIO44Only use values for fsetpos() that are returned from fgetpos()RuleRule
FIO45Avoid TOCTOU race conditions while accessing filesRuleRule
FIO46Do not access a closed fileRuleRule
FIO47Use valid format stringsRuleRule
FLP02Avoid using floating-point numbers when precise computation is neededRecommendationRecommendation
FLP06Convert integers to floating point for floating point operationsRecommendationRecommendation
FLP30Do not use floating-point variables as loop countersRuleRule
FLP32Prevent or detect domain and range errors in math functionsRuleRule
FLP34Ensure that floating-point conversions are within range of the new typeRuleRule
FLP36Preserve precision when converting integral values to floating-point typeRuleRule
FLP37Do not use object representations to compare floating-point valuesRuleRule
INT02Understand integer conversion rulesRecommendationRecommendation
INT04Enforce limits on integer values originating from tainted sourcesRecommendationRecommendation
INT05Do not use input functions to convert character data if they cannot handle all possible inputsRecommendationRecommendation
INT07Use only explicitly signed or unsigned char type for numeric valuesRecommendationRecommendation
INT08Verify that all integer values are in rangeRecommendationRecommendation
INT09Ensure enumeration constants map to unique valuesRecommendationRecommendation
INT10Do not assume a positive remainder when using the % operatorRecommendationRecommendation
INT12Do not make assumptions about the type of a plain int bit-field when used in an expressionRecommendationRecommendation
INT13Use bitwise operators only on unsigned operandsRecommendationRecommendation
INT16Do not make assumptions about representation of signed integersRecommendationRecommendation
INT18Evaluate integer expressions in a larger size before comparing or assigning to that sizeRecommendationRecommendation
INT30Ensure that unsigned integer operations do not wrapRuleRule
INT31Ensure that integer conversions do not result in lost or misinterpreted dataRuleRule
INT32Ensure that operations on signed integers do not result in overflowRuleRule
INT33Ensure that division and remainder operations do not result in divide-by-zero errorsRuleRule
INT34Do not shift an expression by a negative number of bits or by greater than or equal to the number of bits that exist in the operandRuleRule
INT35Use correct integer precisionsRuleRule
INT36Converting a pointer to integer or integer to pointerRuleRule
MEM02Immediately cast the result of a memory allocation function call into a pointer to the allocated typeRecommendationRecommendation
MEM03Clear sensitive information stored in reusable resourcesRecommendationRecommendation
MEM05Avoid large stack allocationsRecommendationRecommendation
MEM30Do not access freed memoryRuleRule
MEM31Free dynamically allocated memory when no longer neededRuleRule
MEM33Allocate and copy structures containing a flexible array member dynamicallyRuleRule
MEM34Only free memory allocated dynamicallyRuleRule
MEM35Allocate sufficient memory for an objectRuleRule
MEM36Do not modify the alignment of objects by calling realloc()RuleRule
MSC01Strive for logical completenessRecommendationRecommendation
MSC04Use comments consistently and in a readable fashionRecommendationRecommendation
MSC07Detect and remove dead codeRecommendationRecommendation
MSC09Character encoding: Use subset of ASCII for safetyRecommendationRecommendation
MSC12Detect and remove code that has no effect or is never executedRecommendationRecommendation
MSC13Detect and remove unused valuesRecommendationRecommendation
MSC14Do not introduce unnecessary platform dependenciesRecommendationRecommendation
MSC15Do not depend on undefined behaviorRecommendationRecommendation
MSC17Finish every set of statements associated with a case label with a break statementRecommendationRecommendation
MSC20Do not use a switch statement to transfer control into a complex blockRecommendationRecommendation
MSC30Do not use the rand() function for generating pseudorandom numbersRuleRule
MSC32Properly seed pseudorandom number generatorsRuleRule
MSC33Do not pass invalid data to the asctime() functionRuleRule
MSC37Ensure that control never reaches the end of a non-void functionRuleRule
MSC38Do not treat a predefined identifier as an object if it might only be implemented as a macroRuleRule
MSC39Do not call va_arg() on a va_list that has an indeterminate valueRuleRule
MSC40Do not violate constraintsRuleRule
MSC41Never hard code sensitive informationRuleRule
POS30Use the readlink() function properlyRule_OptionalRule_Optional
POS33Do not use vfork()Rule_OptionalRule_Optional
POS34Do not call putenv() with a pointer to an automatic variable as the argumentRule_OptionalRule_Optional
POS35Avoid race conditions while checking for the existence of a symbolic linkRule_OptionalRule_Optional
POS36Observe correct revocation order while relinquishing privilegesRule_OptionalRule_Optional
POS37Ensure that privilege relinquishment is successfulRule_OptionalRule_Optional
POS38Beware of race conditions when using fork and file descriptorsRule_OptionalRule_Optional
POS39Use the correct byte ordering when transferring data between systemsRule_OptionalRule_Optional
POS44Do not use signals to terminate threadsRule_OptionalRule_Optional
POS47Do not use threads that can be canceled asynchronouslyRule_OptionalRule_Optional
POS48Do not unlock or destroy another POSIX thread's mutexRule_OptionalRule_Optional
POS49When data must be accessed by multiple threads, provide a mutex and guarantee no adjacent data is also accessedRule_OptionalRule_Optional
POS50Declare objects shared between POSIX threads with appropriate storage durationsRule_OptionalRule_Optional
POS51Avoid deadlock with POSIX threads by locking in predefined orderRule_OptionalRule_Optional
POS52Do not perform operations that can block while holding a POSIX lockRule_OptionalRule_Optional
POS53Do not use more than one mutex for concurrent waiting operations on a condition variableRule_OptionalRule_Optional
POS54Detect and handle POSIX library errorsRule_OptionalDisapplied
PRE00Prefer inline or static functions to function-like macrosRecommendationDisapplied
PRE01Use parentheses within macros around parameter namesRecommendationRecommendation
PRE02Macro replacement lists should be parenthesizedRecommendationRecommendation
PRE03Prefer typedefs to defines for encoding non-pointer typesRecommendationRecommendation
PRE04Do not reuse a standard header file nameRecommendationRecommendation
PRE05Understand macro replacement when concatenating tokens or performing stringificationRecommendationRecommendation
PRE06Enclose header files in an inclusion guardRecommendationRecommendation
PRE07Avoid using repeated question marksRecommendationRecommendation
PRE08Guarantee that header file names are uniqueRecommendationDisapplied
PRE09Do not replace secure functions with deprecated or obsolescent functionsRecommendationRecommendation
PRE10Wrap multi-statement macros in a do-while loopRecommendationRecommendation
PRE11Do not conclude macro definitions with a semicolonRecommendationDisapplied
PRE12Do not define unsafe macrosRecommendationDisapplied
PRE30Do not create a universal character name through concatenationRuleRule
PRE31Avoid side effects in arguments to unsafe macrosRuleRule
PRE32Do not use preprocessor directives in invocations of function-like macrosRuleRule
SIG00Mask signals handled by noninterruptible signal handlersRecommendationRecommendation
SIG01Understand implementation-specific details regarding signal handler persistenceRecommendationRecommendation
SIG30Call only asynchronous-safe functions within signal handlersRuleRule
SIG31Do not access shared objects in signal handlersRuleRule
SIG34Do not call signal() from within interruptible signal handlersRuleRule
SIG35Do not return from a computational exception signal handlerRuleRule
STR04Use plain char for characters in the basic character setRecommendationRecommendation
STR05Use pointers to const when referring to string literalsRecommendationRecommendation
STR06Do not assume that strtok() leaves the parse string unchangedRecommendationRecommendation
STR07Use the bounds-checking interfaces for string manipulationRecommendationRecommendation
STR09Don't assume numeric values for expressions with type plain characterRecommendationRecommendation
STR10Do not concatenate different type of string literalsRecommendationRecommendation
STR11Do not specify the bound of a character array initialized with a string literalRecommendationRecommendation
STR30Do not attempt to modify string literalsRuleRule
STR31Guarantee that storage for strings has sufficient space for character data and the null terminatorRuleRule
STR32Do not pass a non-null-terminated character sequence to a library function that expects a stringRuleRule
STR34Cast characters to unsigned char before converting to larger integer sizesRuleRule
STR37Arguments to character-handling functions must be representable as an unsigned charRuleRule
STR38Do not confuse narrow and wide character strings and functionsRuleRule
+
+
+ +This section targets to provide an overview of Guidelines Compliance Summary. +
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
GuidelineCategoryDescriptionCompliance
ARR02RecommendationExplicitly specify array bounds, even if implicitly defined by an initializerCompliant
ARR30RuleDo not form or use out-of-bounds pointers or array subscriptsCompliant
ARR32RuleEnsure size arguments for variable length arrays are in a valid rangeCompliant
ARR36RuleDo not subtract or compare two pointers that do not refer to the same arrayCompliant
ARR37RuleDo not add or subtract an integer to a pointer to a non-array objectCompliant
ARR38RuleGuarantee that library functions do not form invalid pointersCompliant
ARR39RuleDo not add or subtract a scaled integer to a pointerCompliant
CON30RuleClean up thread-specific storageCompliant
CON31RuleDo not destroy a mutex while it is lockedCompliant
CON32RulePrevent data races when accessing bit-fields from multiple threadsCompliant
CON33RuleAvoid race conditions when using library functionsCompliant
CON34RuleDeclare objects shared between threads with appropriate storage durationsCompliant
CON35RuleAvoid deadlock by locking in a predefined orderCompliant
CON36RuleWrap functions that can spuriously wake up in a loopCompliant
CON37RuleDo not call signal() in a multithreaded programCompliant
CON38RulePreserve thread safety and liveness when using condition variablesCompliant
CON39RuleDo not join or detach a thread that was previously joined or detachedCompliant
CON40RuleDo not refer to an atomic variable twice in an expressionCompliant
CON41RuleWrap functions that can fail spuriously in a loopCompliant
CON43RuleDo not allow data races in multithreaded codeCompliant
DCL00RecommendationConst-qualify immutable objectsCompliant with deviations:
+
+ + + + + + + + + +
QacDescription
qac-3204The variable '%s' is only set once and so it could be declared with the 'const' qualifier.
qac-3227The parameter '%s' is never modified and so it could be declared with the 'const' qualifier.
+
DCL01RecommendationDo not reuse variable names in subscopesCompliant
DCL05RecommendationUse typedefs of non-pointer types onlyDisapplied
DCL06RecommendationUse meaningful symbolic constants to represent literal valuesCompliant with deviations:
+
+ + + + + + + + + +
QacDescription
qac-3120Hard-coded 'magic' integer constant, '%1s'.
qac-3132Hard coded 'magic' number, '%1s', used to define the size of an array.
+
DCL07RecommendationInclude the appropriate type information in function declaratorsCompliant with deviations:
+
+ + + + + +
QacDescription
qac-3450Function '%s', with internal linkage, is being defined without a previous declaration.
+
DCL10RecommendationMaintain the contract between the writer and caller of variadic functionsCompliant
DCL11RecommendationUnderstand the type issues associated with variadic functionsCompliant
DCL13RecommendationDeclare function parameters that are pointers to values not changed by the function as constCompliant
DCL15RecommendationDeclare file-scope objects or functions that do not need external linkage as staticCompliant
DCL16RecommendationUse 'L', not 'l', to indicate a long valueCompliant
DCL18RecommendationDo not begin integer constants with 0 when specifying a decimal valueCompliant
DCL19RecommendationMinimize the scope of variables and functionsCompliant with deviations:
+
+ + + + + + + + + + + + + +
QacDescription
qac-3210The global identifier '%1s' is declared here but is not used in this translation unit.
rcma-1505The function '%1s' is only referenced in the translation unit where it is defined.
rcma-1532The function '%1s' is only referenced in one translation unit - but not the one in which it is defined.
+
DCL20RecommendationExplicitly specify void when a function accepts no argumentsCompliant
DCL21RecommendationUnderstand the storage of compound literalsCompliant
DCL23RecommendationGuarantee that mutually visible identifiers are uniqueCompliant with deviations:
+
+ + + + + + + + + +
QacDescription
qac-0776[L] External identifier matches other external identifier(s) (e.g. '%1s') in first 6 characters - program does not conform strictly to ISO:C90.
qac-0778[L] Identifier matches other identifier(s) (e.g. '%1s') in first 31 characters - program does not conform strictly to ISO:C90.
+
DCL30RuleDeclare objects with appropriate storage durationsCompliant
DCL31RuleDeclare identifiers before using themCompliant
DCL36RuleDo not declare an identifier with conflicting linkage classificationsCompliant
DCL37RuleDo not declare or define a reserved identifierCompliant
DCL38RuleUse the correct syntax when declaring a flexible array memberCompliant
DCL39RuleAvoid information leakage when passing a structure across a trust boundaryCompliant
DCL40RuleDo not create incompatible declarations of the same function or objectCompliant with deviations:
+
+ + + + + + + + + +
QacDescription
qac-0776[L] External identifier matches other external identifier(s) (e.g. '%1s') in first 6 characters - program does not conform strictly to ISO:C90.
qac-0778[L] Identifier matches other identifier(s) (e.g. '%1s') in first 31 characters - program does not conform strictly to ISO:C90.
+
DCL41RuleDo not declare variables inside a switch statement before the first case labelCompliant
ENV03RecommendationSanitize the environment when invoking external programsCompliant
ENV30RuleDo not modify the object referenced by the return value of certain functionsCompliant
ENV31RuleDo not rely on an environment pointer following an operation that may invalidate itCompliant
ENV32RuleAll exit handlers must return normallyCompliant
ENV33RuleDo not call system()Compliant
ENV34RuleDo not store pointers returned by certain functionsCompliant
ERR30RuleSet errno to zero before calling a library function known to set errno, and check errno only after the function returns a value indicating failureCompliant
ERR32RuleDo not rely on indeterminate values of errnoCompliant
ERR33RuleDetect and handle standard library errorsDisapplied
ERR34RuleDetect errors when converting a string to a numberCompliant
EXP00RecommendationUse parentheses for precedence of operationCompliant
EXP02RecommendationBe aware of the short-circuit behavior of the logical AND and OR operatorsCompliant
EXP03RecommendationDo not assume the size of a structure is the sum of the sizes of its membersCompliant
EXP05RecommendationDo not cast away a const qualificationCompliant
EXP07RecommendationDo not diminish the benefits of constants by assuming their values in expressionsCompliant with deviations:
+
+ + + + + + + + + +
QacDescription
qac-3120Hard-coded 'magic' integer constant, '%1s'.
qac-3132Hard coded 'magic' number, '%1s', used to define the size of an array.
+
EXP08RecommendationEnsure pointer arithmetic is used correctlyCompliant
EXP10RecommendationDo not depend on the order of evaluation of subexpressions or the order in which side effects take placeCompliant
EXP11RecommendationDo not make assumptions regarding the layout of structures with bit-fieldsCompliant with deviations:
+
+ + + + + +
QacDescription
qac-0751Casting to char pointer type.
+
EXP12RecommendationDo not ignore values returned by functionsDisapplied
EXP13RecommendationTreat relational and equality operators as if they were nonassociativeCompliant
EXP15RecommendationDo not place a semicolon on the same line as an if, for, or while statementDisapplied
EXP16RecommendationDo not compare function pointers to constant valuesCompliant
EXP19RecommendationUse braces for the body of an if, for, or while statementCompliant
EXP20RecommendationPerform explicit tests to determine success, true and false, and equalityCompliant
EXP30RuleDo not depend on the order of evaluation for side effectsCompliant
EXP32RuleDo not access a volatile object through a nonvolatile referenceCompliant
EXP33RuleDo not read uninitialized memoryCompliant
EXP34RuleDo not dereference null pointersCompliant
EXP35RuleDo not modify objects with temporary lifetimeCompliant
EXP36RuleDo not cast pointers into more strictly aligned pointer typesCompliant
EXP37RuleCall functions with the correct number and type of argumentsCompliant
EXP39RuleDo not access a variable through a pointer of an incompatible typeCompliant with deviations:
+
+ + + + + +
QacDescription
qac-0751Casting to char pointer type.
+
EXP40RuleDo not modify constant objectsCompliant
EXP42RuleDo not compare padding dataCompliant
EXP43RuleAvoid undefined behavior when using restrict-qualified pointersCompliant
EXP44RuleDo not rely on side effects in operands to sizeof, _Alignof, or _GenericCompliant
EXP45RuleDo not perform assignments in selection statementsCompliant with deviations:
+
+ + + + + +
QacDescription
qac-3416Logical operation performed on expression with persistent side effects.
+
EXP46RuleDo not use a bitwise operator with a Boolean-like operandCompliant
EXP47RuleDo not call va_arg with an argument of the incorrect typeCompliant
FIO01RecommendationBe careful using functions that use file names for identificationCompliant
FIO03RecommendationDo not make assumptions about fopen() and file creationCompliant
FIO06RecommendationCreate files with appropriate access permissionsCompliant
FIO08RecommendationTake care when calling remove() on an open fileCompliant
FIO10RecommendationTake care when using the rename() functionCompliant
FIO21RecommendationDo not create temporary files in shared directoriesCompliant
FIO30RuleExclude user input from format stringsCompliant
FIO32RuleDo not perform operations on devices that are only appropriate for filesCompliant
FIO34RuleDistinguish between characters read from a file and EOF or WEOFCompliant
FIO37RuleDo not assume that fgets() or fgetws() returns a nonempty string when successfulCompliant
FIO38RuleDo not copy a FILE objectCompliant
FIO39RuleDo not alternately input and output from a stream without an intervening flush or positioning callCompliant
FIO40RuleReset strings on fgets() or fgetws() failureCompliant
FIO41RuleDo not call getc(), putc(), getwc(), or putwc() with a stream argument that has side effectsCompliant
FIO42RuleClose files when they are no longer neededCompliant
FIO44RuleOnly use values for fsetpos() that are returned from fgetpos()Compliant
FIO45RuleAvoid TOCTOU race conditions while accessing filesCompliant
FIO46RuleDo not access a closed fileCompliant
FIO47RuleUse valid format stringsCompliant
FLP02RecommendationAvoid using floating-point numbers when precise computation is neededCompliant
FLP06RecommendationConvert integers to floating point for floating point operationsCompliant
FLP30RuleDo not use floating-point variables as loop countersCompliant
FLP32RulePrevent or detect domain and range errors in math functionsCompliant
FLP34RuleEnsure that floating-point conversions are within range of the new typeCompliant
FLP36RulePreserve precision when converting integral values to floating-point typeCompliant
FLP37RuleDo not use object representations to compare floating-point valuesCompliant
INT02RecommendationUnderstand integer conversion rulesCompliant with deviations:
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-1251Suffixed integer constant causes implicit conversion of other operand.
qac-1290An integer constant of 'essentially signed' type is being converted to unsigned type on assignment.
qac-2100Integral promotion : unsigned char promoted to signed int.
qac-2101Integral promotion : unsigned short promoted to signed int.
qac-2105Integral promotion : signed short promoted to signed int.
qac-2109Integral promotion : _Bool promoted to signed int.
qac-4471A non-constant expression of 'essentially unsigned' type (%1s) is being passed to a function parameter of wider unsigned type, '%2s'.
+
INT04RecommendationEnforce limits on integer values originating from tainted sourcesCompliant
INT05RecommendationDo not use input functions to convert character data if they cannot handle all possible inputsCompliant
INT07RecommendationUse only explicitly signed or unsigned char type for numeric valuesCompliant
INT08RecommendationVerify that all integer values are in rangeCompliant
INT09RecommendationEnsure enumeration constants map to unique valuesCompliant
INT10RecommendationDo not assume a positive remainder when using the % operatorCompliant
INT12RecommendationDo not make assumptions about the type of a plain int bit-field when used in an expressionCompliant
INT13RecommendationUse bitwise operators only on unsigned operandsCompliant with deviations:
+
+ + + + + +
QacDescription
qac-4544A non-negative constant expression of 'essentially signed' type (%1s) is being used as the right-hand operand of this shift operator (%2s).
+
INT16RecommendationDo not make assumptions about representation of signed integersCompliant
INT18RecommendationEvaluate integer expressions in a larger size before comparing or assigning to that sizeCompliant
INT30RuleEnsure that unsigned integer operations do not wrapCompliant with deviations:
+
+ + + + + + + + + +
QacDescription
qac-3383Cannot identify wraparound guard for unsigned arithmetic expression.
qac-3384Cannot identify wraparound guard for dependent unsigned arithmetic expression.
+
INT31RuleEnsure that integer conversions do not result in lost or misinterpreted dataCompliant
INT32RuleEnsure that operations on signed integers do not result in overflowCompliant
INT33RuleEnsure that division and remainder operations do not result in divide-by-zero errorsCompliant
INT34RuleDo not shift an expression by a negative number of bits or by greater than or equal to the number of bits that exist in the operandCompliant
INT35RuleUse correct integer precisionsCompliant
INT36RuleConverting a pointer to integer or integer to pointerCompliant with deviations:
+
+ + + + + +
QacDescription
qac-0306[I] Cast between a pointer to object and an integral type.
+
MEM02RecommendationImmediately cast the result of a memory allocation function call into a pointer to the allocated typeCompliant
MEM03RecommendationClear sensitive information stored in reusable resourcesCompliant
MEM05RecommendationAvoid large stack allocationsCompliant
MEM30RuleDo not access freed memoryCompliant
MEM31RuleFree dynamically allocated memory when no longer neededCompliant
MEM33RuleAllocate and copy structures containing a flexible array member dynamicallyCompliant
MEM34RuleOnly free memory allocated dynamicallyCompliant
MEM35RuleAllocate sufficient memory for an objectCompliant
MEM36RuleDo not modify the alignment of objects by calling realloc()Compliant
MSC01RecommendationStrive for logical completenessCompliant with deviations:
+
+ + + + + +
QacDescription
qac-2000No 'else' clause exists for this 'if' statement.
+
MSC04RecommendationUse comments consistently and in a readable fashionCompliant
MSC07RecommendationDetect and remove dead codeCompliant with deviations:
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-2881The code in this 'default' clause is unreachable.
qac-2982This assignment is redundant. The value of this object is never used before being modified.
qac-2983This assignment is redundant. The value of this object is never subsequently used.
qac-2984This operation is redundant. The value of the result is always '%1s'.
qac-2985This operation is redundant. The value of the result is always that of the left-hand operand.
qac-2986This operation is redundant. The value of the result is always that of the right-hand operand.
qac-3205The identifier '%1s' is not used and could be removed.
qac-3210The global identifier '%1s' is declared here but is not used in this translation unit.
rcma-1503The function '%1s' is defined but is not used within this project.
+
MSC09RecommendationCharacter encoding: Use subset of ASCII for safetyCompliant
MSC12RecommendationDetect and remove code that has no effect or is never executedCompliant
MSC13RecommendationDetect and remove unused valuesCompliant with deviations:
+
+ + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-2982This assignment is redundant. The value of this object is never used before being modified.
qac-2983This assignment is redundant. The value of this object is never subsequently used.
qac-2984This operation is redundant. The value of the result is always '%1s'.
qac-2985This operation is redundant. The value of the result is always that of the left-hand operand.
qac-2986This operation is redundant. The value of the result is always that of the right-hand operand.
qac-3205The identifier '%1s' is not used and could be removed.
+
MSC14RecommendationDo not introduce unnecessary platform dependenciesCompliant
MSC15RecommendationDo not depend on undefined behaviorCompliant
MSC17RecommendationFinish every set of statements associated with a case label with a break statementCompliant
MSC20RecommendationDo not use a switch statement to transfer control into a complex blockCompliant
MSC30RuleDo not use the rand() function for generating pseudorandom numbersCompliant
MSC32RuleProperly seed pseudorandom number generatorsCompliant
MSC33RuleDo not pass invalid data to the asctime() functionCompliant
MSC37RuleEnsure that control never reaches the end of a non-void functionCompliant
MSC38RuleDo not treat a predefined identifier as an object if it might only be implemented as a macroCompliant
MSC39RuleDo not call va_arg() on a va_list that has an indeterminate valueCompliant
MSC40RuleDo not violate constraintsCompliant
MSC41RuleNever hard code sensitive informationCompliant
POS30Rule_OptionalUse the readlink() function properlyCompliant
POS33Rule_OptionalDo not use vfork()Compliant
POS34Rule_OptionalDo not call putenv() with a pointer to an automatic variable as the argumentCompliant
POS35Rule_OptionalAvoid race conditions while checking for the existence of a symbolic linkCompliant
POS36Rule_OptionalObserve correct revocation order while relinquishing privilegesCompliant
POS37Rule_OptionalEnsure that privilege relinquishment is successfulCompliant
POS38Rule_OptionalBeware of race conditions when using fork and file descriptorsCompliant
POS39Rule_OptionalUse the correct byte ordering when transferring data between systemsCompliant
POS44Rule_OptionalDo not use signals to terminate threadsCompliant
POS47Rule_OptionalDo not use threads that can be canceled asynchronouslyCompliant
POS48Rule_OptionalDo not unlock or destroy another POSIX thread's mutexCompliant
POS49Rule_OptionalWhen data must be accessed by multiple threads, provide a mutex and guarantee no adjacent data is also accessedCompliant
POS50Rule_OptionalDeclare objects shared between POSIX threads with appropriate storage durationsCompliant
POS51Rule_OptionalAvoid deadlock with POSIX threads by locking in predefined orderCompliant
POS52Rule_OptionalDo not perform operations that can block while holding a POSIX lockCompliant
POS53Rule_OptionalDo not use more than one mutex for concurrent waiting operations on a condition variableCompliant
POS54Rule_OptionalDetect and handle POSIX library errorsDisapplied
PRE00RecommendationPrefer inline or static functions to function-like macrosDisapplied
PRE01RecommendationUse parentheses within macros around parameter namesCompliant
PRE02RecommendationMacro replacement lists should be parenthesizedCompliant
PRE03RecommendationPrefer typedefs to defines for encoding non-pointer typesCompliant
PRE04RecommendationDo not reuse a standard header file nameCompliant
PRE05RecommendationUnderstand macro replacement when concatenating tokens or performing stringificationCompliant
PRE06RecommendationEnclose header files in an inclusion guardCompliant
PRE07RecommendationAvoid using repeated question marksCompliant
PRE08RecommendationGuarantee that header file names are uniqueDisapplied
PRE09RecommendationDo not replace secure functions with deprecated or obsolescent functionsCompliant
PRE10RecommendationWrap multi-statement macros in a do-while loopCompliant with deviations:
+
+ + + + + +
QacDescription
qac-3412Macro defines an unrecognized code-fragment.
+
PRE11RecommendationDo not conclude macro definitions with a semicolonDisapplied
PRE12RecommendationDo not define unsafe macrosDisapplied
PRE30RuleDo not create a universal character name through concatenationCompliant
PRE31RuleAvoid side effects in arguments to unsafe macrosCompliant
PRE32RuleDo not use preprocessor directives in invocations of function-like macrosCompliant
SIG00RecommendationMask signals handled by noninterruptible signal handlersCompliant
SIG01RecommendationUnderstand implementation-specific details regarding signal handler persistenceCompliant
SIG30RuleCall only asynchronous-safe functions within signal handlersCompliant
SIG31RuleDo not access shared objects in signal handlersCompliant
SIG34RuleDo not call signal() from within interruptible signal handlersCompliant
SIG35RuleDo not return from a computational exception signal handlerCompliant
STR04RecommendationUse plain char for characters in the basic character setCompliant
STR05RecommendationUse pointers to const when referring to string literalsCompliant
STR06RecommendationDo not assume that strtok() leaves the parse string unchangedCompliant
STR07RecommendationUse the bounds-checking interfaces for string manipulationCompliant
STR09RecommendationDon't assume numeric values for expressions with type plain characterCompliant
STR10RecommendationDo not concatenate different type of string literalsCompliant
STR11RecommendationDo not specify the bound of a character array initialized with a string literalCompliant
STR30RuleDo not attempt to modify string literalsCompliant
STR31RuleGuarantee that storage for strings has sufficient space for character data and the null terminatorCompliant
STR32RuleDo not pass a non-null-terminated character sequence to a library function that expects a stringCompliant
STR34RuleCast characters to unsigned char before converting to larger integer sizesCompliant
STR37RuleArguments to character-handling functions must be representable as an unsigned charCompliant
STR38RuleDo not confuse narrow and wide character strings and functionsCompliant
+
+
+ +This section targets to provide an overview of Deviation Permits.
+All the rules corresponding to the deviation permits are disabled inside Helix QAC and will not cause any violation or deviation in the Deviation records section below. +
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
GuidelineCategoryDescriptionRatioSub RulesCharacteristicsReason
DCL00RecommendationConst-qualify immutable objects2/5
+ + + + + + + + + +
QacDescription
qac-3204The variable '%s' is only set once and so it could be declared with the 'const' qualifier.
qac-3227The parameter '%s' is never modified and so it could be declared with the 'const' qualifier.
+
Other / AcceptedRecoBreach
DCL05RecommendationUse typedefs of non-pointer types only1/1 (all)
+ + + + + +
QacDescription
certccm-5004This typedef is applied to a pointer type.
+
Other / AcceptedRecoBreach
DCL06RecommendationUse meaningful symbolic constants to represent literal values2/6
+ + + + + + + + + +
QacDescription
qac-3120Hard-coded 'magic' integer constant, '%1s'.
qac-3132Hard coded 'magic' number, '%1s', used to define the size of an array.
+
Other / AcceptedRecoBreach
DCL07RecommendationInclude the appropriate type information in function declarators1/6
+ + + + + +
QacDescription
qac-3450Function '%s', with internal linkage, is being defined without a previous declaration.
+
Other / AcceptedRecoBreach
DCL19RecommendationMinimize the scope of variables and functions3/6
+ + + + + + + + + + + + + +
QacDescription
qac-3210The global identifier '%1s' is declared here but is not used in this translation unit.
rcma-1505The function '%1s' is only referenced in the translation unit where it is defined.
rcma-1532The function '%1s' is only referenced in one translation unit - but not the one in which it is defined.
+
Maintainability / ModularityRFAL Library contains functions which may be used by the user but are not called in the project used for checking compliance.
DCL23RecommendationGuarantee that mutually visible identifiers are unique2/8
+ + + + + + + + + +
QacDescription
qac-0776[L] External identifier matches other external identifier(s) (e.g. '%1s') in first 6 characters - program does not conform strictly to ISO:C90.
qac-0778[L] Identifier matches other identifier(s) (e.g. '%1s') in first 31 characters - program does not conform strictly to ISO:C90.
+
Maintainability / AnalysabilityProject is following C99 which defines 63 signficant characters.
DCL40RuleDo not create incompatible declarations of the same function or object2/5
+ + + + + + + + + +
QacDescription
qac-0776[L] External identifier matches other external identifier(s) (e.g. '%1s') in first 6 characters - program does not conform strictly to ISO:C90.
qac-0778[L] Identifier matches other identifier(s) (e.g. '%1s') in first 31 characters - program does not conform strictly to ISO:C90.
+
Maintainability / AnalysabilityProject is following C99 which defines 63 signficant characters.
ERR33RuleDetect and handle standard library errors1/1 (all)
+ + + + + +
QacDescription
qac-3200'%s' returns a value which is not being used.
+
Maintainability / AnalysabilityTreating the return codes of functions in all places without exception handling would make the code hard to read and maintain. Error checking has been reduced to the places where needed.
EXP07RecommendationDo not diminish the benefits of constants by assuming their values in expressions2/6
+ + + + + + + + + +
QacDescription
qac-3120Hard-coded 'magic' integer constant, '%1s'.
qac-3132Hard coded 'magic' number, '%1s', used to define the size of an array.
+
Other / AcceptedRecoBreach
EXP11RecommendationDo not make assumptions regarding the layout of structures with bit-fields1/2
+ + + + + +
QacDescription
qac-0751Casting to char pointer type.
+
Other / AcceptedRecoBreach +
0751-See EXP39 +
+
EXP12RecommendationDo not ignore values returned by functions1/1 (all)
+ + + + + +
QacDescription
qac-3200'%s' returns a value which is not being used.
+
Maintainability / AnalysabilitySee ERR33.
EXP15RecommendationDo not place a semicolon on the same line as an if, for, or while statement1/1 (all)
+ + + + + +
QacDescription
qac-3109Null statement follows other code on the same line.
+
Other / AcceptedRecoBreach
EXP39RuleDo not access a variable through a pointer of an incompatible type1/3
+ + + + + +
QacDescription
qac-0751Casting to char pointer type.
+
Other / NonMatchEXP39 states "Modifying a variable through a pointer of an incompatible type (other than unsigned char)". Therefore 0751 is disabled.
EXP45RuleDo not perform assignments in selection statements1/4
+ + + + + +
QacDescription
qac-3416Logical operation performed on expression with persistent side effects.
+
Other / NonMatchEXP45 is only targeting assignment inside expressions. 3416 is a considered an invalid mapping.
INT02RecommendationUnderstand integer conversion rules7/119
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-1251Suffixed integer constant causes implicit conversion of other operand.
qac-1290An integer constant of 'essentially signed' type is being converted to unsigned type on assignment.
qac-2100Integral promotion : unsigned char promoted to signed int.
qac-2101Integral promotion : unsigned short promoted to signed int.
qac-2105Integral promotion : signed short promoted to signed int.
qac-2109Integral promotion : _Bool promoted to signed int.
qac-4471A non-constant expression of 'essentially unsigned' type (%1s) is being passed to a function parameter of wider unsigned type, '%2s'.
+
Other / AcceptedRecoBreachCode complies to MISRA restrictions for types and casts (Dir-1.1, Rule-1.1 ,10.1-10.8, 11.1-8) with documented exceptions. No further restrictions employed.
INT13RecommendationUse bitwise operators only on unsigned operands1/5
+ + + + + +
QacDescription
qac-4544A non-negative constant expression of 'essentially signed' type (%1s) is being used as the right-hand operand of this shift operator (%2s).
+
Other / AcceptedRecoBreach
INT30RuleEnsure that unsigned integer operations do not wrap2/8
+ + + + + + + + + +
QacDescription
qac-3383Cannot identify wraparound guard for unsigned arithmetic expression.
qac-3384Cannot identify wraparound guard for dependent unsigned arithmetic expression.
+
Maintainability / TestabilityNo apparent nor definite wraparound happening, checks for wraparound are omitted.
INT36RuleConverting a pointer to integer or integer to pointer1/9
+ + + + + +
QacDescription
qac-0306[I] Cast between a pointer to object and an integral type.
+
Maintainability / ModifiabilityUsing STM32 HAL already creates many violations. Also needed to do pointer arithmetic, calculating offsets inside a buffer.
MSC01RecommendationStrive for logical completeness1/3
+ + + + + +
QacDescription
qac-2000No 'else' clause exists for this 'if' statement.
+
Other / AcceptedRecoBreach
MSC07RecommendationDetect and remove dead code9/28
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-2881The code in this 'default' clause is unreachable.
qac-2982This assignment is redundant. The value of this object is never used before being modified.
qac-2983This assignment is redundant. The value of this object is never subsequently used.
qac-2984This operation is redundant. The value of the result is always '%1s'.
qac-2985This operation is redundant. The value of the result is always that of the left-hand operand.
qac-2986This operation is redundant. The value of the result is always that of the right-hand operand.
qac-3205The identifier '%1s' is not used and could be removed.
qac-3210The global identifier '%1s' is declared here but is not used in this translation unit.
rcma-1503The function '%1s' is defined but is not used within this project.
+
Usability / User error protectionAll the violations were checked and fixing the violation would deteriorate robustness: Removing checks which are unnecessary at the given position, removing trailing iterator increment, etc. Also suppressed for MISRA Rule-2.2
MSC13RecommendationDetect and remove unused values6/14
+ + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-2982This assignment is redundant. The value of this object is never used before being modified.
qac-2983This assignment is redundant. The value of this object is never subsequently used.
qac-2984This operation is redundant. The value of the result is always '%1s'.
qac-2985This operation is redundant. The value of the result is always that of the left-hand operand.
qac-2986This operation is redundant. The value of the result is always that of the right-hand operand.
qac-3205The identifier '%1s' is not used and could be removed.
+
Usability / User error protectionAll the violations were checked and fixing the violation would deteriorate robustness: Removing checks which are unnecessary at the given position, removing trailing iterator increment, etc. Also suppressed for MISRA Rule-2.2
POS54Rule_OptionalDetect and handle POSIX library errors1/1 (all)
+ + + + + +
QacDescription
qac-3200'%s' returns a value which is not being used.
+
Maintainability / AnalysabilityOnly memcpy, memmove and memset are used which don't return errors. The more general 3200(already suppressed as part of ERR33) would only report other violations.
PRE00RecommendationPrefer inline or static functions to function-like macros1/1 (all)
+ + + + + +
QacDescription
qac-3453A function could probably be used instead of this function-like macro.
+
Performance / Resource utilizationSuppressed due to code optimization and efficiency. Compare MISRA Dir-4.9.
PRE08RecommendationGuarantee that header file names are unique1/1 (all)
+ + + + + +
QacDescription
certccm-5002The header '%s' file name shadows a previously included header.
+
Other / AcceptedRecoBreach
PRE10RecommendationWrap multi-statement macros in a do-while loop1/2
+ + + + + +
QacDescription
qac-3412Macro defines an unrecognized code-fragment.
+
Other / AcceptedRecoBreach
PRE11RecommendationDo not conclude macro definitions with a semicolon1/1 (all)
+ + + + + +
QacDescription
qac-3412Macro defines an unrecognized code-fragment.
+
Other / AcceptedRecoBreach
PRE12RecommendationDo not define unsafe macros1/1 (all)
+ + + + + +
QacDescription
qac-3456Parameter '%1s' will be evaluated more than once when this macro is used.
+
Other / AcceptedRecoBreach
+
+
+ +This section targets to provide an overview of Deviation Records. +
+
+
+ +

File: rfal/include/rfal_utils.h

+
+ + + + + + + + + +
LinesCountSuppressed QacsComment
1471
+ + + + +
certccm-5003The obsolete function '%s' will be used as a result of this macro expansion
+
CERT C 9 - string.h from Cosmic only provides functions with low qualified parameters
+

File: rfal/source/rfal_iso15693_2.c

+
+ + + + + + + + + +
LinesCountSuppressed QacsComment
5241
+ + + + +
qac-2911Definite: Wraparound in unsigned arithmetic operation.
+
CERT INT30 - Intentional underflow, part of the coding
+

File: rfal/source/rfal_isoDep.c

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
LinesCountSuppressed QacsComment
10271
+ + + + +
qac-2003The preceding 'switch' clause is not empty and does not end with a 'jump' statement. Execution will fall through.
+
MISRA 16.3 - Intentional fall through
7961
+ + + + +
qac-2003The preceding 'switch' clause is not empty and does not end with a 'jump' statement. Execution will fall through.
+
MISRA 16.3 - Intentional fall through
27311
+ + + + +
qac-0310Casting to different object pointer type.
+
MISRA 11.3 - Intentional safe cast to avoiding large buffer duplication
27941
+ + + + +
qac-2003The preceding 'switch' clause is not empty and does not end with a 'jump' statement. Execution will fall through.
+
MISRA 16.3 - Intentional fall through
2279-22801
+ + + + +
qac-0310Casting to different object pointer type.
+
MISRA 11.3 - Intentional safe cast to avoiding buffer duplication
13601
+ + + + +
qac-2003The preceding 'switch' clause is not empty and does not end with a 'jump' statement. Execution will fall through.
+
MISRA 16.3 - Intentional fall through
+

File: rfal/source/rfal_nfc.c

+
+ + + + + + + + + + + + + + + + + + + + + +
LinesCountSuppressed QacsComment
19241
+ + + + +
qac-0310Casting to different object pointer type.
+
MISRA 11.3 - Intentional safe cast to avoiding large buffer duplication
2051
+ + + + +
qac-2880This code is unreachable.
+
MISRA 2.1 - Unreachable code due to configuration option being set/unset
21491
+ + + + +
qac-0310Casting to different object pointer type.
+
MISRA 11.3 - Intentional safe cast to avoiding large buffer duplication
+

File: rfal/source/rfal_nfcDep.c

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
LinesCountSuppressed QacsComment
16811
+ + + + +
qac-2003The preceding 'switch' clause is not empty and does not end with a 'jump' statement. Execution will fall through.
+
MISRA 16.3 - Intentional fall through
26891
+ + + + +
qac-2003The preceding 'switch' clause is not empty and does not end with a 'jump' statement. Execution will fall through.
+
MISRA 16.3 - Intentional fall through
16091
+ + + + +
qac-2003The preceding 'switch' clause is not empty and does not end with a 'jump' statement. Execution will fall through.
+
MISRA 16.3 - Intentional fall through
26301
+ + + + +
qac-0310Casting to different object pointer type.
+
MISRA 11.3 - Intentional safe cast to avoiding large buffer duplication
+

File: rfal/source/rfal_nfca.c

+
+ + + + + + + + + + + + + + + +
LinesCountSuppressed QacsComment
7391
+ + + + +
qac-2003The preceding 'switch' clause is not empty and does not end with a 'jump' statement. Execution will fall through.
+
MISRA 16.3 - Intentional fall through
3141
+ + + + +
qac-2003The preceding 'switch' clause is not empty and does not end with a 'jump' statement. Execution will fall through.
+
MISRA 16.3 - Intentional fall through
+

File: rfal/source/rfal_t4t.c

+
+ + + + + + + + + + + + + + + +
LinesCountSuppressed QacsComment
1361
+ + + + +
qac-2880This code is unreachable.
+
MISRA 2.1 - Unreachable code due to configuration option being set/unset
1271
+ + + + +
qac-2880This code is unreachable.
+
MISRA 2.1 - Unreachable code due to configuration option being set/unset
+

File: rfal/source/st25r3916/rfal_analogConfigTbl.h

+
+ + + + + + + + + +
LinesCountSuppressed QacsComment
404-4061
+ + + + +
qac-3674Array size defined implicitly by the number of initializers.
+
CERT ARR02 - Flexible array will be used with sizeof, on adding elements error-prone manual update of size would be required
+

File: rfal/source/st25r3916/rfal_dpoTbl.h

+
+ + + + + + + + + +
LinesCountSuppressed QacsComment
53-551
+ + + + +
qac-3674Array size defined implicitly by the number of initializers.
+
CERT ARR02 - Flexible array will be used with sizeof, on adding elements error-prone manual update of size would be required
+

File: rfal/source/st25r3916/rfal_rfst25r3916.c

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
LinesCountSuppressed QacsComment
20591
+ + + + +
qac-2003The preceding 'switch' clause is not empty and does not end with a 'jump' statement. Execution will fall through.
+
MISRA 16.3 - Intentional fall through
36371
+ + + + +
qac-2003The preceding 'switch' clause is not empty and does not end with a 'jump' statement. Execution will fall through.
+
MISRA 16.3 - Intentional fall through
20281
+ + + + +
qac-2003The preceding 'switch' clause is not empty and does not end with a 'jump' statement. Execution will fall through.
+
MISRA 16.3 - Intentional fall through
24021
+ + + + +
qac-2003The preceding 'switch' clause is not empty and does not end with a 'jump' statement. Execution will fall through.
+
MISRA 16.3 - Intentional fall through
20431
+ + + + +
qac-2003The preceding 'switch' clause is not empty and does not end with a 'jump' statement. Execution will fall through.
+
MISRA 16.3 - Intentional fall through
24771
+ + + + +
qac-2003The preceding 'switch' clause is not empty and does not end with a 'jump' statement. Execution will fall through.
+
MISRA 16.3 - Intentional fall through
21311
+ + + + +
qac-2003The preceding 'switch' clause is not empty and does not end with a 'jump' statement. Execution will fall through.
+
MISRA 16.3 - Intentional fall through
26721
+ + + + +
qac-2003The preceding 'switch' clause is not empty and does not end with a 'jump' statement. Execution will fall through.
+
MISRA 16.3 - Intentional fall through
21831
+ + + + +
qac-2003The preceding 'switch' clause is not empty and does not end with a 'jump' statement. Execution will fall through.
+
MISRA 16.3 - Intentional fall through
22611
+ + + + +
qac-2003The preceding 'switch' clause is not empty and does not end with a 'jump' statement. Execution will fall through.
+
MISRA 16.3 - Intentional fall through
25651
+ + + + +
qac-2003The preceding 'switch' clause is not empty and does not end with a 'jump' statement. Execution will fall through.
+
MISRA 16.3 - Intentional fall through
10551
+ + + + +
qac-2880This code is unreachable.
+
MISRA 2.1 - Inconsistently marked as unreachable code
+
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FileRecommendationRuleTotal
rfal/source/st25r3916/rfal_rfst25r3916.c12012
rfal/source/st25r3916/rfal_dpoTbl.h101
rfal/source/rfal_t4t.c202
rfal/source/st25r3916/rfal_analogConfigTbl.h101
rfal/source/rfal_nfcDep.c415
rfal/source/rfal_nfca.c202
rfal/source/rfal_isoDep.c628
rfal/source/rfal_iso15693_2.c112
rfal/source/rfal_nfc.c325
rfal/include/rfal_utils.h101
Total33639
+ +
+
+Note: Qac messages can be mapped by more than one rule or group. In such case it will be counted several times. +
+
+
+ + +There are no duplicated suppressions. + +

File: common/firmware/STM/utils/Inc/utils.h

+
+ + + + + + + + + + + + +
LineUnused QacsComment
79
+ + + + +
qac-0431[C] Function argument points to a more heavily qualified type.
+
MISRA 1.1 - string.h from Cosmic only provides functions with low qualified parameters
81
+ + + + +
qac-0431[C] Function argument points to a more heavily qualified type.
+
MISRA 1.1 - string.h from Cosmic only provides functions with low qualified parameters
+

File: rfal/include/rfal_utils.h

+
+ + + + + + + + + + + + +
LineUnused QacsComment
148
+ + + + +
qac-0431[C] Function argument points to a more heavily qualified type.
+
MISRA 1.1 - string.h from Cosmic only provides functions with low qualified parameters
150
+ + + + +
qac-0431[C] Function argument points to a more heavily qualified type.
+
MISRA 1.1 - string.h from Cosmic only provides functions with low qualified parameters
+

File: rfal/source/rfal_isoDep.c

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
LineUnused QacsComment
2564
+ + + + +
qac-4342An expression of 'essentially unsigned' type (%1s) is being cast to enum type '%2s'.
+
MISRA 10.5 - Layout of enum rfalBitRate and above clamping of maxTxBR guarantee no invalid enum values to be created
2557
+ + + + +
qac-4342An expression of 'essentially unsigned' type (%1s) is being cast to enum type '%2s'.
+
MISRA 10.5 - Layout of enum rfalBitRate and above clamping of maxTxBR guarantee no invalid enum values to be created
2185
+ + + + +
qac-4342An expression of 'essentially unsigned' type (%1s) is being cast to enum type '%2s'.
+
MISRA 10.5 - Layout of enum rfalIsoDepFSxI is guaranteed whithin 4bit range
2653
+ + + + +
qac-4342An expression of 'essentially unsigned' type (%1s) is being cast to enum type '%2s'.
+
MISRA 10.5 - Layout of enum rfalBitRate and range of loop variable guarantee no invalid enum values to be created
2640
+ + + + +
qac-4342An expression of 'essentially unsigned' type (%1s) is being cast to enum type '%2s'.
+
MISRA 10.5 - Layout of enum rfalBitRate and range of loop variable guarantee no invalid enum values to be created
416
+ + + + +
qac-0750A union type specifier has been defined.
+
MISRA 19.2 - Members of the union will not be used concurrently, only one frame at a time
1400
+ + + + +
qac-4342An expression of 'essentially unsigned' type (%1s) is being cast to enum type '%2s'.
+
MISRA 10.5 - Layout of enum rfalBitRate and above masks guarantee no invalid enum values to be created
+

File: rfal/source/rfal_nfc.c

+
+ + + + + + + + + + + + +
LineUnused QacsComment
77
+ + + + +
qac-0750A union type specifier has been defined.
+
MISRA 19.2 - Members of the union will not be used concurrently, only one interface at a time
219
+ + + + +
qac-2880This code is unreachable.
+
MISRA 2.1 - Unreachable code due to configuration option being set/unset
+

File: rfal/source/rfal_nfcDep.c

+
+ + + + + + + +
LineUnused QacsComment
1930
+ + + + +
qac-4342An expression of 'essentially unsigned' type (%1s) is being cast to enum type '%2s'.
+
MISRA 10.5 - Layout of enum rfalBitRate and definition of rfalNfcDepBRS2DSI guarantee no invalid enum values to be created
+

File: rfal/source/rfal_nfca.c

+
+ + + + + + + +
LineUnused QacsComment
746
+ + + + +
qac-4342An expression of 'essentially unsigned' type (%1s) is being cast to enum type '%2s'.
+
MISRA 10.5 - Guaranteed that no invalid enum values are created: see guard_eq_RFAL_NFCA_T2T, ....
+

File: rfal/source/rfal_nfcb.c

+
+ + + + + + + +
LineUnused QacsComment
545
+ + + + +
qac-4342An expression of 'essentially unsigned' type (%1s) is being cast to enum type '%2s'.
+
MISRA 10.5 - Layout of rfalNfcbSlots and the limited loop guarantee that no invalid enum values are created.
+

File: rfal/source/st25r3916/rfal_analogConfigTbl.h

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
LineUnused QacsComment
717
+ + + + +
qac-3406Object/function '%s', with external linkage, has been defined in a header file.
+
MISRA 8.9 - Externally generated table included by the library
405
+
+
MISRA 8.9 - Externally generated table included by the library
716
+ + + + +
qac-3674Array size defined implicitly by the number of initializers.
+
CERT ARR02 - Flexible array will be used with sizeof, on adding elements error-prone manual update of size would be required
405
+ + + + +
qac-3406Object/function '%s', with external linkage, has been defined in a header file.
+
MISRA 8.6 - Externally generated table included by the library
717
+
+
MISRA 8.6 - Externally generated table included by the library
+

File: rfal/source/st25r3916/rfal_dpoTbl.h

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
LineUnused QacsComment
65
+ + + + +
qac-3674Array size defined implicitly by the number of initializers.
+
CERT ARR02 - Flexible array will be used with sizeof, on adding elements error-prone manual update of size would be required
66
+
+
MISRA 8.9 - Externally generated table included by the library
66
+ + + + +
qac-3406Object/function '%s', with external linkage, has been defined in a header file.
+
MISRA 8.6 - Externally generated table included by the library
54
+
+
MISRA 8.9 - Externally generated table included by the library
54
+ + + + +
qac-3406Object/function '%s', with external linkage, has been defined in a header file.
+
MISRA 8.6 - Externally generated table included by the library
+

File: rfal/source/st25r3916/rfal_rfst25r3916.c

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
LineUnused QacsComment
3657
+ + + + +
qac-4342An expression of 'essentially unsigned' type (%1s) is being cast to enum type '%2s'.
+
MISRA 10.5 - Guaranteed that no invalid enum values may be created. See also equalityGuard_RFAL_BR_106 ff.
3387
+ + + + +
qac-0759An object of union type has been defined.
+
MISRA 19.2 - Allocating Union where members are of the same type, just different names. Thus no problem can occur.
280
+ + + + +
qac-0750A union type specifier has been defined.
+
MISRA 19.2 - Both members are of the same type, just different names. Thus no problem can occur.
1639
+ + + + +
5209No description
+
MISRA 4.9 - External function (sqrt()) requires double
3890
+ + + + +
qac-4342An expression of 'essentially unsigned' type (%1s) is being cast to enum type '%2s'.
+
MISRA 10.5 - Guaranteed that no invalid enum values may be created. See also equalityGuard_RFAL_BR_106 ff.
+
+
+ +There are no continuous suppressions by file. +
+
+ +Active Diagnostics refers to diagnostics that are not suppressed (note: no suppressed diagnostics have been taken into account for the calculation of information in this document). +
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FilesActive DiagnosticsViolated RulesViolation CountCompliance Index
rfal/include/rfal_analogConfig.h000100.00
rfal/include/rfal_cd.h000100.00
rfal/include/rfal_chip.h000100.00
rfal/include/rfal_dpo.h000100.00
rfal/include/rfal_isoDep.h000100.00
rfal/include/rfal_nfc.h000100.00
rfal/include/rfal_nfcDep.h000100.00
rfal/include/rfal_nfca.h000100.00
rfal/include/rfal_nfcb.h000100.00
rfal/include/rfal_nfcf.h000100.00
rfal/include/rfal_nfcv.h000100.00
rfal/include/rfal_rf.h000100.00
rfal/include/rfal_st25tb.h000100.00
rfal/include/rfal_st25xv.h000100.00
rfal/include/rfal_t1t.h000100.00
rfal/include/rfal_t2t.h000100.00
rfal/include/rfal_t4t.h000100.00
rfal/include/rfal_utils.h000100.00
rfal/source/rfal_analogConfig.c000100.00
rfal/source/rfal_cd.c000100.00
rfal/source/rfal_cdHb.c000100.00
rfal/source/rfal_crc.c000100.00
rfal/source/rfal_crc.h000100.00
rfal/source/rfal_dpo.c000100.00
rfal/source/rfal_iso15693_2.c000100.00
rfal/source/rfal_iso15693_2.h000100.00
rfal/source/rfal_isoDep.c000100.00
rfal/source/rfal_nfc.c000100.00
rfal/source/rfal_nfcDep.c000100.00
rfal/source/rfal_nfca.c000100.00
rfal/source/rfal_nfcb.c000100.00
rfal/source/rfal_nfcf.c000100.00
rfal/source/rfal_nfcv.c000100.00
rfal/source/rfal_st25tb.c000100.00
rfal/source/rfal_st25xv.c000100.00
rfal/source/rfal_t1t.c000100.00
rfal/source/rfal_t2t.c000100.00
rfal/source/rfal_t4t.c000100.00
rfal/source/st25r3916/rfal_analogConfigTbl.h000100.00
rfal/source/st25r3916/rfal_dpoTbl.h000100.00
rfal/source/st25r3916/rfal_features.h000100.00
rfal/source/st25r3916/rfal_rfst25r3916.c000100.00
rfal/source/st25r3916/st25r3916.c000100.00
rfal/source/st25r3916/st25r3916.h000100.00
rfal/source/st25r3916/st25r3916_aat.c000100.00
rfal/source/st25r3916/st25r3916_aat.h000100.00
rfal/source/st25r3916/st25r3916_com.c000100.00
rfal/source/st25r3916/st25r3916_com.h000100.00
rfal/source/st25r3916/st25r3916_irq.c000100.00
rfal/source/st25r3916/st25r3916_irq.h000100.00
rfal/source/st25r3916/st25r3916_led.c000100.00
rfal/source/st25r3916/st25r3916_led.h000100.00
Total000100.00
+ +

+Nota: Calculation of Compliance Index
+The Compliance Index is the percentage of groups which have no messages in them.
+For each file it is calculated as follows:
+
+( Ntotal - Nerror ) / Ntotal x 100
+
+Ntotal is the total number of enforced rules (i.e. the number of rules that have at least one message mapped to it directly).
+Nerror is the number of rules for which messages appear in that file.
+The File Compliance Index is the mean of all the individual file compliances.
+ +
+
+
+
+ + diff --git a/core/embed/io/nfc/rfal/doc/ST25R3916_MisraComplianceReport.html b/core/embed/io/nfc/rfal/doc/ST25R3916_MisraComplianceReport.html new file mode 100644 index 0000000000..8e6841ddf0 --- /dev/null +++ b/core/embed/io/nfc/rfal/doc/ST25R3916_MisraComplianceReport.html @@ -0,0 +1,10580 @@ + + + + + +Helix QAC GEP/GCS/GRP Report + + + + +
+
+
+
+
+ +This section targets to provide an overview of Guidelines Enforcement Plan (GEP).
+
+This document will only focus on STMicroelectronics NFC RF Abstraction Layer (RFAL).
+
+The project has been designed to comply with the standard ISO/IEC 9899:1999 ([C99]). +
+
+

1. Tools version

+
+ + + + + + + + + + + + + + + + + +
ComponentVersionTargetOptions
rcma2.3.0C_CPP
m3cm2.6.0C
qac9.9.0C
    -d : __schedule_barrier=_ignore_semi
+
    -namelength : 63
+
    -prodoption : df::function_timeout=20000
+
+

2. Configuration

+This section targets to provide the main configuration options used for MISRA compliance.
+
+The project complies to [C99],
+the variables length has been consequently set to a dedicated value (cf 'namelength' option in table above). +
+
+Repository/components:
+
    +
  • MCU target:
  • +
      RFAL-ST25R3916

    +
  • RFAL informations:
  • +
      Path: rfal
    +
      Version: v3.0.0
    +
  • Project repositories SHA1:
  • +
      common: bd63699
    +
      nucleo: afd8cc1
    +
      rfal: 5b33cb9
    +
    +
+

3. Assistance/Enforcement

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
GuidelineCategoryDescriptionAssistance/Enforcement Sub Rules
Dir-1.1RequiredAny implementation-defined behaviour on which the output of the program depends shall be documented and understood
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-0202[I] '-' character in '[]' conversion specification is implementation defined.
qac-0240[I] This file contains the control-M character at the end of a line.
qac-0241[I] This file contains the control-Z character - was this transferred from a PC?
qac-0242[I] This file contains the control-M character in the middle of a line.
qac-0243[I] Treating an invalid character as whitespace.
qac-0246[E] Binary integer constants are a language extension.
qac-0284[I] Multiple character constants have implementation defined values.
qac-0285[I] Character constant contains character which is not a member of the basic source character set.
qac-0286[I] String literal contains character which is not a member of the basic source character set.
qac-0287[I] Header name contains character which is not a member of the basic source character set.
qac-0288[I] Source file '%s' has comments containing characters which are not members of the basic source character set.
qac-0289[I] Source file '%s' has preprocessing tokens containing characters which are not members of the basic source character set.
qac-0292[I] Source file '%s' has comments containing one of the characters '$', '@' or '`'.
qac-0299[I] Source file '%s' includes #pragma directives containing characters which are not members of the basic source character set.
qac-0314[I] Cast from a pointer to object type to a pointer to void.
qac-0315[I] Implicit conversion from a pointer to object type to a pointer to void.
qac-0497[E] Performing pointer arithmetic on pointer to void.
qac-0551[E] Cast may not operate on the left operand of the assignment operator.
qac-0581[I] Floating-point constant may be too small to be representable.
qac-0601[E] Function 'main()' is not of type 'int (void)' or 'int (int, char *[])'.
qac-0633[E] Empty structures and unions are a language extension.
qac-0634[I] Bit-field %1s in %2s has not been declared explicitly as unsigned or signed.
qac-0635[C99] Bit-field %1s in %2s has been declared with a type not explicitly supported.
qac-0660[C11] Defining an unnamed member in a struct or union.
qac-0662[C11] Accessing a member of an unnamed struct or union member.
qac-0830[E] Unrecognized text encountered after a preprocessing directive.
qac-0831[E] Use of '\\' in this '#include' line is a PC extension - this usage is non-portable.
qac-0840[E] Extra tokens at end of #include directive.
qac-0899[E] Unrecognized preprocessing directive has been ignored - assumed to be a language extension.
qac-0981[E] Redundant semicolon in 'struct' or 'union' member declaration list is a language extension.
qac-1001[E] '#include %s' is a VMS extension.
qac-1002[E] '%s' is not a legal identifier in ISO C.
qac-1003[E] '#%s' is a language extension for in-line assembler. All statements located between #asm and #endasm will be ignored.
qac-1006[E] This in-line assembler construct is a language extension. The code has been ignored.
qac-1008[E] '#%s' is not a legal ISO C preprocessing directive.
qac-1012[E] Use of a C++ reference type ('type &') will be treated as a language extension.
qac-1014[E] Non-standard type specifier - this will be treated as a language extension.
qac-1015[E] '%s' is not a legal keyword in ISO C - this will be treated as a language extension.
qac-1019[E] '@ address' is not supported in ISO C - this will be treated as a language extension.
qac-1020[E] '__typeof__' is not supported in ISO C, and is treated as a language extension.
qac-1021[E] A statement expression is not supported in ISO C, and is treated as a language extension.
qac-1022[E] '__alignof__' is a language extension. It is mapped to the standard '_Alignof' operator.
qac-1026[E] The indicated @word construct has been ignored.
qac-1028[E] Use of the sizeof operator in a preprocessing directive is a language extension.
qac-1029[E] Whitespace encountered between backslash and new-line has been ignored.
qac-1034[E] Macro defined with named variable argument list. This is a language extension.
qac-1035[E] No macro arguments supplied for variable argument list. This is a language extension.
qac-1036[E] Comma before ## ignored in expansion of variadic macro. This is a language extension.
qac-1037[E] Arrays of length zero are a language extension.
qac-1038[E] The sequence ", ##__VA_ARGS__" is a language extension.
qac-1039[E] Treating array of length one as potentially flexible member.
qac-1041[E] Empty aggregate initializers are a language extension.
qac-1042[E] Using I64 or UI64 as an integer constant suffix. This is a language extension.
qac-1043[E] Defining an anonymous union object. This is a language extension.
qac-1044[E] Defining an anonymous struct object. This is a language extension.
qac-1045[E] Use of the #include_next preprocessing directive is a language extension.
qac-1046[E] Function is being declared with default argument syntax. This is a language extension.
qac-1049[E] Nested functions are a language extension.
qac-1086[E] '_Alignof (expression)' is a common non-standard extension. ISO C11 only defines '_Alignof (type)'.
qac-1090[E] '__label__' is not supported in ISO C, and is treated as a language extension.
qac-1094[E] '_Static_assert (expression)' with no message is a common non-standard extension.
qac-1130[E] The '__has_include' operator is a language extension.
qac-1131[E] The '__has_include_next' operator is a language extension.
qac-2070Using [[attribute]] syntax.
qac-2071[E] This attribute syntax is a language extension.
qac-2072[E] This attribute specifier is unexpected in this source position, and will be ignored.
qac-2850Constant: Implicit conversion to a signed integer type of insufficient size.
qac-2851Definite: Implicit conversion to a signed integer type of insufficient size.
qac-2852Apparent: Implicit conversion to a signed integer type of insufficient size.
qac-2853Suspicious: Implicit conversion to a signed integer type of insufficient size.
qac-2855Constant: Casting to a signed integer type of insufficient size.
qac-2856Definite: Casting to a signed integer type of insufficient size.
qac-2857Apparent: Casting to a signed integer type of insufficient size.
qac-2858Suspicious: Casting to a signed integer type of insufficient size.
qac-2860Constant: Implementation-defined value resulting from left shift operation on expression of signed type.
qac-2861Definite: Implementation-defined value resulting from left shift operation on expression of signed type.
qac-2862Apparent: Implementation-defined value resulting from left shift operation on expression of signed type.
qac-2863Suspicious: Implementation-defined value resulting from left shift operation on expression of signed type.
qac-2895Constant: Negative value cast to an unsigned type.
qac-2896Definite: Negative value cast to an unsigned type.
qac-2897Apparent: Negative value cast to an unsigned type.
qac-2898Suspicious: Negative value cast to an unsigned type.
qac-3116Unrecognized #pragma arguments '%s' This #pragma directive has been ignored.
qac-3445[E] Conditional expression with middle operand omitted is a language extension.
qac-3664[E] Using a dot operator to access an individual bit is a language extension.
+
Dir-1.1-C99RequiredAny implementation-defined behaviour on which the output of the program depends shall be documented and understood
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-0371[L] Nesting levels of blocks exceeds 127 - program does not conform strictly to ISO:C99.
qac-0372[L] More than 63 levels of nested conditional inclusion - program does not conform strictly to ISO:C99.
qac-0375[L] Nesting of parenthesized expressions exceeds 63 - program does not conform strictly to ISO:C99.
qac-0380[L] Number of macro definitions exceeds 4095 - program does not conform strictly to ISO:C99.
qac-0388[L] '#include "%s"' causes nesting to exceed 15 levels - program does not conform strictly to ISO:C99.
qac-0390[L] Number of members in 'struct' or 'union' exceeds 1023 - program does not conform strictly to ISO:C99.
qac-0391[L] Number of enumeration constants exceeds 1023 - program does not conform strictly to ISO:C99.
qac-0392[L] Nesting of 'struct' or 'union' types exceeds 63 - program does not conform strictly to ISO:C99.
qac-0613[L] Size of object '%1s' exceeds 65535 bytes - program does not conform strictly to ISO:C99.
qac-0615[L] More than 511 block scope identifiers defined within a block - program does not conform strictly to ISO:C99.
qac-0786[L] Identifier matches other macro name(s) (e.g. '%1s') in first 63 characters.
qac-0789[L] Identifier matches other identifier(s) (e.g. '%1s') in first 63 characters - program does not conform strictly to ISO:C99.
qac-0793[L] Macro identifier matches other macro identifier(s) (e.g. '%1s') in first 63 characters - program does not conform strictly to ISO:C99.
qac-0796Identifier matches other identifier(s) (e.g. '%1s') in an outer scope within the ISO:C99 limit of 63 significant characters
qac-1077[C11] The keyword '_Noreturn' has been used.
qac-1078[C11] The keyword '_Alignas' has been used.
qac-1079[C11] The keyword '_Alignof' has been used.
qac-1081[C11] The keyword '_Atomic' has been used.
qac-1082[C11] The keyword '_Generic' has been used.
qac-1083[C11] The keyword '_Static_assert' has been used.
qac-1084[C11] The keyword '_Thread_local' has been used.
+
Dir-2.1RequiredAll source files shall compile without any compilation errorsUnassisted

+Remarks:
+Dedicated checks deployed in Jenkins.
Dir-3.1RequiredAll code shall be traceable to documented requirementsUnassisted

+Remarks:
+Limited management of requirements.
Dir-4.1RequiredRun-time failures shall be minimized
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-2791Definite: Right hand operand of shift operator is negative or too large.
qac-2792Apparent: Right hand operand of shift operator is negative or too large.
qac-2793Suspicious: Right hand operand of shift operator is negative or too large.
qac-2794Possible: Tainted right hand operand of shift operator is negative or too large.
qac-2801Definite: Overflow in signed arithmetic operation.
qac-2802Apparent: Overflow in signed arithmetic operation.
qac-2803Suspicious: Overflow in signed arithmetic operation.
qac-2804Possible: Overflow in signed arithmetic tainted operation.
qac-2811Definite: Dereference of NULL pointer.
qac-2812Apparent: Dereference of NULL pointer.
qac-2813Suspicious: Dereference of NULL pointer.
qac-2821Definite: Arithmetic operation on NULL pointer.
qac-2822Apparent: Arithmetic operation on NULL pointer.
qac-2823Suspicious: Arithmetic operation on NULL pointer.
qac-2831Definite: Division by zero.
qac-2832Apparent: Division by zero.
qac-2833Suspicious: Division by zero.
qac-2841Definite: Dereference of an invalid pointer value.
qac-2842Apparent: Dereference of an invalid pointer value.
qac-2843Suspicious: Dereference of an invalid pointer value.
qac-2845Constant: Maximum number of characters to be read/written is larger than the target buffer size.
qac-2846Definite: Maximum number of characters to be read/written is larger than the target buffer size.
qac-2847Apparent: Maximum number of characters to be read/written is larger than the target buffer size.
qac-2848Suspicious: Maximum number of characters to be read/written is larger than the target buffer size.
qac-2871Infinite loop identified.
qac-2872This loop, if entered, will never terminate.
qac-2877This loop will never be executed more than once.
qac-2935Constant: Dereference of an invalid char pointer value.
qac-2936Definite: Dereference of an invalid char pointer value.
qac-2937Apparent: Dereference of an invalid char pointer value.
qac-2938Suspicious: Dereference of an invalid char pointer value.
+
Dir-4.10RequiredPrecautions shall be taken in order to prevent the contents of a header file being included more then once
+ + + + + +
QacDescription
qac-0883Include file code is not protected against repeated inclusion
+
Dir-4.11RequiredThe validity of values passed to library functions shall be checkedUnassisted

+Remarks:
+No automated check deployed.
+Manual checks done by developers.
Dir-4.12RequiredDynamic memory allocation shall not be used
+ + + + + +
QacDescription
m3cm-5118Use of memory allocation or deallocation function: calloc, malloc, realloc or free.
+
Dir-4.13AdvisoryFunctions which are designed to provide operations on a resource should be called in an appropriate sequence
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-2726Definite: Use of uninitialized resource.
qac-2727Apparent: Use of uninitialized resource.
qac-2728Suspicious: Use of uninitialized resource.
qac-2731Definite: Use of destroyed resource.
qac-2732Apparent: Use of destroyed resource.
qac-2733Suspicious: Use of destroyed resource.
qac-2746Definite: Use of uninitialized file handle.
qac-2747Apparent: Use of uninitialized file handle.
qac-2748Suspicious: Use of uninitialized file handle.
qac-4866Definite: Memory is used after free.
qac-4867Apparent: Memory is used after free.
qac-4868Suspicious: Memory is used after free.
+
Dir-4.14RequiredThe validity of values received from external sources shall be checked
+ + + + + +
QacDescription
qac-2956Definite: Using object '%s' with tainted value.
+
Dir-4.2AdvisoryAll usage of assembly language should be documented
+ + + + + + + + + +
QacDescription
qac-1003[E] '#%s' is a language extension for in-line assembler. All statements located between #asm and #endasm will be ignored.
qac-1006[E] This in-line assembler construct is a language extension. The code has been ignored.
+
Dir-4.3RequiredAssembly language shall be encapsulated and isolated
+ + + + + + + + + +
QacDescription
qac-3006This function contains a mixture of in-line assembler statements and C statements.
qac-3008This function contains a mixture of in-line assembler statements and C code.
+
Dir-4.4AdvisorySections of code should not be "commented out"
+ + + + + + + + + +
QacDescription
qac-2052This line comment appears to comment out source code.
qac-2053This block comment appears to comment out source code.
+
Dir-4.5AdvisoryIdentifiers in the same name space with overlapping visibility should be typographically unambiguous
+ + + + + +
QacDescription
rcma-1710Identifiers have the same matching pattern '%1s'.
+
Dir-4.6Advisorytypedefs that indicate size and signedness should be used in place of the basic numerical types
+ + + + + +
QacDescription
m3cm-5209Use of basic type '%s'.
+
Dir-4.7RequiredIf a function returns error information, then that error information shall be tested
+ + + + + +
QacDescription
qac-2504Return value of '%s' is not checked for error status.
+
Dir-4.8AdvisoryIf a pointer to a structure or union is never dereferenced within a translation unit, then the implementation of the object should be hidden
+ + + + + +
QacDescription
qac-3630The implementation of this struct/union type should be hidden.
+
Dir-4.9AdvisoryA function should be used in preference to a function-like macro where they are interchangeable
+ + + + + + + + + + + + + + + + + +
QacDescription
qac-3469This usage of a function-like macro looks like it could be replaced by an equivalent function call.
qac-3471Some uses of this function-like macro look like they could be replaced by equivalent function calls.
qac-3472All toplevel uses of this function-like macro look like they could be replaced by equivalent function calls.
qac-3473This usage of a function-like setter macro looks like it could be replaced by a similar function call.
+
Rule-1.1RequiredThe program shall contain no violations of the standard C syntax and constraints, and shall not exceed the implementation's translation limits
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-0232[C] Value of hex escape sequence is not representable in type 'unsigned char'.
qac-0233[C] Value of octal escape sequence is not representable in type 'unsigned char'.
qac-0244[C] Value of character constant is not representable in type 'int'.
qac-0268[S] Comment open at end of translation unit.
qac-0321[C] Declaration within 'for' statement defines an identifier '%1s' which is not an object.
qac-0322[C] Illegal storage class specifier used in 'for' statement declaration.
qac-0323[C] Cast between a pointer to incomplete type and a floating type.
qac-0327[C] Cast between a pointer to void and a floating type.
qac-0338[C] Octal or hex escape sequence value is too large for 'unsigned char' or 'wchar_t' type.
qac-0422[C] Function call contains fewer arguments than prototype specifies.
qac-0423[C] Function call contains more arguments than prototype specifies.
qac-0426[C] Called function has incomplete return type.
qac-0427[C] Object identifier used as if it were a function or a function pointer identifier.
qac-0429[C] Function argument is not of arithmetic type.
qac-0430[C] Function argument is not of compatible 'struct'/'union' type.
qac-0431[C] Function argument points to a more heavily qualified type.
qac-0432[C] Function argument is not of compatible pointer type.
qac-0435[C] The 'struct'/'union' member '%s' does not exist.
qac-0436[C] Left operand of '.' must be a 'struct' or 'union' object.
qac-0437[C] Left operand of '->' must be a pointer to a 'struct' or 'union' object.
qac-0446[C] Operand of ++/-- must have scalar (arithmetic or pointer) type.
qac-0447[C] Operand of ++/-- must be a modifiable object.
qac-0448[C] Operand of ++/-- must not be a pointer to an object of unknown size.
qac-0449[C] Operand of ++/-- must not be a pointer to a function.
qac-0451[C] Subscripting requires a pointer (or array lvalue).
qac-0452[C] Cannot subscript a pointer to an object of unknown size.
qac-0453[C] An array subscript must have integral type.
qac-0454[C] The address-of operator '&' cannot be applied to an object declared with 'register'.
qac-0456[C] This expression does not have an address - '&' may only be applied to an lvalue or a function designator.
qac-0457[C] The address-of operator '&' cannot be applied to a bit-field.
qac-0458[C] Indirection operator '*' requires operand of pointer type.
qac-0460[C] The keyword static is used in the declaration of the index of an array which is not a function parameter.
qac-0461[C] The keyword static is used in the declaration of an inner index of a multi-dimensional array.
qac-0462[C] A type qualifier (const, volatile or restrict) is used in the declaration of the index of an array which is not a function parameter.
qac-0463[C] A type qualifier (const, volatile or restrict) is used in the declaration of an inner index of a multi-dimensional array.
qac-0466[C] Unary '+' requires arithmetic operand.
qac-0467[C] Operand of '!' must have scalar (arithmetic or pointer) type.
qac-0468[C] Unary '-' requires arithmetic operand.
qac-0469[C] Bitwise not '~' requires integral operand.
qac-0476[C] 'sizeof' cannot be applied to a bit-field.
qac-0477[C] 'sizeof' cannot be applied to a function.
qac-0478[C] 'sizeof' cannot be applied to an object of unknown size.
qac-0481[C] Only scalar expressions may be cast to other types.
qac-0482[C] Expressions may only be cast to 'void' or scalar types.
qac-0483[C] A pointer to an object of unknown size cannot be the operand of an addition operator.
qac-0484[C] A pointer to an object of unknown size cannot be the operand of a subtraction operator.
qac-0485[C] Only integral expressions may be added to pointers.
qac-0486[C] Only integral expressions and compatible pointers may be subtracted from pointers.
qac-0487[C] If two pointers are subtracted, they must be pointers that address compatible types.
qac-0493[C] Type of left operand is not compatible with this operator.
qac-0494[C] Type of right operand is not compatible with this operator.
qac-0495[C] Left operand of '%', '<<', '>>', '&', '^' or '|' must have integral type.
qac-0496[C] Right operand of '%', '<<', '>>', '&', '^' or '|' must have integral type.
qac-0513[C] Relational operator used to compare pointers to incompatible types.
qac-0514[C] Relational operator used to compare a pointer with an incompatible operand.
qac-0515[C] Equality operator used to compare a pointer with an incompatible operand.
qac-0536[C] First operand of '&&', '||' or '?' must have scalar (arithmetic or pointer) type.
qac-0537[C] Second operand of '&&' or '||' must have scalar (arithmetic or pointer) type.
qac-0540[C] 2nd and 3rd operands of conditional operator '?' must have compatible types.
qac-0541[C] Argument no. %s does not have object type.
qac-0542[C] Controlling expression must have scalar (arithmetic or pointer) type.
qac-0546[C] 'enum %s' has unknown content. Use of an enum tag with undefined content is not permitted.
qac-0547[C] This declaration of tag '%s' conflicts with a previous declaration.
qac-0550[C] Left operand of '+=' or '-=' is a pointer to an object of unknown size.
qac-0554[C] 'static %s()' has been declared and called but no definition has been given.
qac-0555[C] Invalid assignment to object of void type or array type.
qac-0556[C] Left operand of assignment must be a modifiable object.
qac-0557[C] Right operand of assignment is not of arithmetic type.
qac-0558[C] Right operand of '+=' or '-=' must have integral type when left operand is a pointer.
qac-0559[C] Right operand of '<<=', '>>=', '&=', '|=', '^=' or '%=' must have integral type.
qac-0560[C] Left operand of '<<=', '>>=', '&=', '|=', '^=' or '%=' must have integral type.
qac-0561[C] Right operand of assignment is not of compatible 'struct'/'union' type.
qac-0562[C] Right operand of assignment points to a more heavily qualified type.
qac-0563[C] Right operand of assignment is not of compatible pointer type.
qac-0564[C] Left operand of assignment must be an lvalue (it must designate an object).
qac-0565[C] Left operand of '+=' or '-=' must be of arithmetic or pointer to object type.
qac-0580[C] Constant is too large to be representable.
qac-0588[C] Width of bit-field must be an integral constant expression.
qac-0589[C] Enumeration constant must be an integral constant expression.
qac-0590[C] Array bound must be an integral constant expression.
qac-0591[C] A 'case' label must be an integral constant expression.
qac-0605[C] A declaration must declare a tag or an identifier.
qac-0616[C] Illegal combination of type specifiers or storage class specifiers.
qac-0619[C] The identifier '%1s' has already been defined in the current scope within the ordinary identifier namespace.
qac-0620[C] Cannot initialize '%s' because it has unknown size.
qac-0621[C] The struct/union '%s' cannot be initialized because it has unknown size.
qac-0622[C] The identifier '%s' has been declared both with and without linkage in the same scope.
qac-0627[C] '%s' has different type to previous declaration in the same scope.
qac-0628[C] '%s' has different type to previous declaration at wider scope.
qac-0629[C] More than one definition of '%s' (with internal linkage).
qac-0631[C] More than one declaration of '%s' (with no linkage).
qac-0638[C] Duplicate member name '%s' in 'struct' or 'union'.
qac-0640[C] '%s' in 'struct' or 'union' type may not have 'void' type.
qac-0641[C] '%s' in 'struct' or 'union' type may not have function type.
qac-0642[C] '%s' in 'struct' or 'union' type may not be an array of unknown size.
qac-0643[C] '%s' in 'struct' or 'union' type may not be a 'struct' or 'union' with unknown content.
qac-0644[C] Width of bit-field must be no bigger than the width of an 'int'.
qac-0645[C] A zero width bit-field cannot be given a name.
qac-0646[C] Enumeration constants must have values representable as 'int's.
qac-0649[C] K&R style declaration of parameters is not legal after a function header that includes a parameter list.
qac-0650[C] Illegal storage class specifier on named function parameter.
qac-0651[C] Missing type specifiers in function declaration.
qac-0653[C] Duplicate definition of 'struct', 'union' or 'enum' tag '%s'.
qac-0655[C] Illegal storage class specifier on unnamed function parameter.
qac-0656[C] Function return type cannot be function or array type, or an incomplete struct/union (for function definition).
qac-0657[C] Unnamed parameter specified in function definition.
qac-0659[C] The identifier '%s' was not given in the parameter list.
qac-0664[C] Parameter specified with type 'void'.
qac-0665[C] Two parameters have been declared with the same name '%1s'.
qac-0669[C] The restrict qualifier can only be applied to pointer types derived from object or incomplete types.
qac-0671[C] Initializer for object of arithmetic type is not of arithmetic type.
qac-0673[C] Initializer points to a more heavily qualified type.
qac-0674[C] Initializer for pointer is of incompatible type.
qac-0675[C] Initializer is not of compatible 'struct'/'union' type.
qac-0677[C] Array size is negative, or unrepresentable.
qac-0682[C] Initializer for object of a character type is a string literal.
qac-0683[C] Initializer for object of a character type is a wide string literal.
qac-0684[C] Too many initializers.
qac-0685[C] Initializer for any object with static storage duration must be a constant expression.
qac-0690[C] String literal contains too many characters to initialize object.
qac-0698[C] String literal used to initialize an object of incompatible type.
qac-0699[C] String literal used to initialize a pointer of incompatible type.
qac-0708[C] No definition found for the label '%s' in this function.
qac-0709[C] Initialization of locally declared 'extern %s' is illegal.
qac-0736[C] 'case' label does not have unique value within this 'switch' statement.
qac-0737[C] More than one 'default' label found in 'switch' statement.
qac-0738[C] Controlling expression in a 'switch' statement must have integral type.
qac-0746[C] 'return exp;' found in '%s()' whose return type is 'void'.
qac-0747[C] 'return exp;' found in '%s()' whose return type is qualified 'void'.
qac-0755[C] 'return' expression is not of arithmetic type.
qac-0756[C] 'return' expression is not of compatible 'struct'/'union' type.
qac-0757[C] 'return' expression points to a more heavily qualified type.
qac-0758[C] 'return' expression is not of compatible pointer type.
qac-0766[C] 'continue' statement found outside an iteration statement.
qac-0767[C] 'break' statement found outside a 'switch' or iteration statement.
qac-0768[C] 'case' or 'default' found outside a 'switch' statement.
qac-0774[C] 'auto' may not be specified on global declaration of '%s'.
qac-0775[C] 'register' may not be specified on global declaration of '%s'.
qac-0801[C] The '##' operator may not be the first token in a macro replacement list.
qac-0802[C] The '##' operator may not be the last token in a macro replacement list.
qac-0803[C] The '#' operator may only appear before a macro parameter.
qac-0804[C] Macro parameter '%s' is not unique.
qac-0811[C] The glue operator '##' may only appear in a '#define' preprocessing directive.
qac-0817[S] Closing quote or bracket '>' missing from include filename.
qac-0818[Q] Cannot find '%s' - Perhaps the appropriate search path was not given ?
qac-0821[C] '#include' does not identify a header or source file that can be processed.
qac-0834[C] Function-like macro '%s()' is being redefined as an object-like macro.
qac-0835[C] Macro '%s' is being redefined with different parameter names.
qac-0844[C] Macro '%s' is being redefined with a different replacement list.
qac-0845[C] Object-like macro '%s' is being redefined as a function-like macro.
qac-0851[C] More arguments in macro call than specified in definition.
qac-0852[S] Unable to find the ')' that marks the end of the macro call.
qac-0866[C] The string literal in a '#line' directive cannot be a 'wide string literal'.
qac-0873[C] Preprocessing token cannot be converted to an actual token.
qac-0877[C] '#if' and '#elif' expressions may contain only integral constants.
qac-0940[C] Illegal usage of a variably modified type.
qac-0941[C] A variable length array may not be initialized.
qac-0943[C] Jump to label '%s' is a jump into the scope of an identifier with variably modified type.
qac-0944[C] The label '%s' is inside the scope of an identifier with variably modified type.
qac-1023[C] Using '_Alignof' on function types is illegal.
qac-1024[C] Using '_Alignof' on incomplete types is illegal.
qac-1025[C] Using '_Alignof' on bit-fields is illegal.
qac-1033[C] The identifier __VA_ARGS__ may only be used in the replacement list of a variadic macro.
qac-1047[EE] Function is being declared with default argument syntax after a previous call to the function. This is not allowed.
qac-1048[EE] Default argument values are missing for some parameters in this function declaration. This is not allowed.
qac-1050[EE] Nested functions cannot be 'extern' or 'static'.
qac-1061[C] Structure '%1s' with flexible array member '%2s' cannot be used in the declaration of structure member '%3s'.
qac-1062[C] Structure '%1s' with flexible array member '%2s' cannot be used in the declaration of array elements.
qac-1080[EE] A typedef or pointer to function is being declared with default argument syntax. This is not allowed.
qac-1087[C] Objects declared with '_Thread_local' must have linkage or static storage duration.
qac-1088[C] '_Thread_local' must appear on every declaration of an object, or on none.
qac-1089[C] '_Thread_local' may not form part of a function declaration.
qac-1091[EE] Declaration of a GNU local label must be the first statement in the block.
qac-1092[EE] No definition found for the label '%s' in this scope.
qac-1093[C] Failed static assertion '%s'.
qac-1095[S] The message passed to '_Static_assert' must be a string literal.
qac-1096[C] The expression passed to '_Static_assert' must be an integer constant expression.
qac-1112[C] The _Atomic specifier may not be used to qualify this type.
qac-1113[C] Implicit conversion may not add or remove the _Atomic qualifier.
qac-1117[C] This is not a valid alignment expression.
qac-1118[C] An explicitly specified alignment must be at least as strict as the default.
qac-1120[C] This is not an object which can be declared with an alignment specifier.
qac-1124[C] This '_Generic' selection contains multiple 'default' associations.
qac-1125[C] This association does not describe a unique type in the '_Generic' selection.
qac-1126[C] The controlling expression of this '_Generic' selection does not match any association.
qac-1127[C] This '_Generic' association describes an incomplete or variably-modified type.
qac-1133[EE] Using the '__has_include' or '__has_include_next' operator outside of an #if or #elif directive.
qac-2025This appears to jump across a nested function scope boundary.
qac-3236[C] 'inline' may not be applied to function 'main'.
qac-3237[C] inline function '%1s' has external linkage and is defining an object, '%2s', with static storage duration.
qac-3238[C] inline function '%1s' has external linkage and is referring to an object, '%2s', with internal linkage.
qac-3244[C] 'inline' may only be used in the declaration of a function identifier.
+
Rule-1.1-C99RequiredThe program shall contain no violations of the standard C syntax and constraints, and shall not exceed the implementation's translation limits
+ + + + + + + + + + + + + +
QacDescription
qac-2050The 'int' type specifier has been omitted from a function declaration.
qac-2051The 'int' type specifier has been omitted from an object declaration.
qac-3335No function declaration. Implicit declaration inserted: 'extern int %s();'.
+
Rule-1.2AdvisoryLanguage extensions should not be used
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-0246[E] Binary integer constants are a language extension.
qac-0497[E] Performing pointer arithmetic on pointer to void.
qac-0551[E] Cast may not operate on the left operand of the assignment operator.
qac-0601[E] Function 'main()' is not of type 'int (void)' or 'int (int, char *[])'.
qac-0633[E] Empty structures and unions are a language extension.
qac-0635[C99] Bit-field %1s in %2s has been declared with a type not explicitly supported.
qac-0660[C11] Defining an unnamed member in a struct or union.
qac-0662[C11] Accessing a member of an unnamed struct or union member.
qac-0830[E] Unrecognized text encountered after a preprocessing directive.
qac-0831[E] Use of '\\' in this '#include' line is a PC extension - this usage is non-portable.
qac-0840[E] Extra tokens at end of #include directive.
qac-0899[E] Unrecognized preprocessing directive has been ignored - assumed to be a language extension.
qac-0981[E] Redundant semicolon in 'struct' or 'union' member declaration list is a language extension.
qac-1001[E] '#include %s' is a VMS extension.
qac-1002[E] '%s' is not a legal identifier in ISO C.
qac-1003[E] '#%s' is a language extension for in-line assembler. All statements located between #asm and #endasm will be ignored.
qac-1006[E] This in-line assembler construct is a language extension. The code has been ignored.
qac-1008[E] '#%s' is not a legal ISO C preprocessing directive.
qac-1012[E] Use of a C++ reference type ('type &') will be treated as a language extension.
qac-1014[E] Non-standard type specifier - this will be treated as a language extension.
qac-1015[E] '%s' is not a legal keyword in ISO C - this will be treated as a language extension.
qac-1019[E] '@ address' is not supported in ISO C - this will be treated as a language extension.
qac-1020[E] '__typeof__' is not supported in ISO C, and is treated as a language extension.
qac-1021[E] A statement expression is not supported in ISO C, and is treated as a language extension.
qac-1022[E] '__alignof__' is a language extension. It is mapped to the standard '_Alignof' operator.
qac-1026[E] The indicated @word construct has been ignored.
qac-1028[E] Use of the sizeof operator in a preprocessing directive is a language extension.
qac-1029[E] Whitespace encountered between backslash and new-line has been ignored.
qac-1034[E] Macro defined with named variable argument list. This is a language extension.
qac-1035[E] No macro arguments supplied for variable argument list. This is a language extension.
qac-1036[E] Comma before ## ignored in expansion of variadic macro. This is a language extension.
qac-1037[E] Arrays of length zero are a language extension.
qac-1038[E] The sequence ", ##__VA_ARGS__" is a language extension.
qac-1039[E] Treating array of length one as potentially flexible member.
qac-1041[E] Empty aggregate initializers are a language extension.
qac-1042[E] Using I64 or UI64 as an integer constant suffix. This is a language extension.
qac-1043[E] Defining an anonymous union object. This is a language extension.
qac-1044[E] Defining an anonymous struct object. This is a language extension.
qac-1045[E] Use of the #include_next preprocessing directive is a language extension.
qac-1046[E] Function is being declared with default argument syntax. This is a language extension.
qac-1049[E] Nested functions are a language extension.
qac-1083[C11] The keyword '_Static_assert' has been used.
qac-1086[E] '_Alignof (expression)' is a common non-standard extension. ISO C11 only defines '_Alignof (type)'.
qac-1090[E] '__label__' is not supported in ISO C, and is treated as a language extension.
qac-1094[E] '_Static_assert (expression)' with no message is a common non-standard extension.
qac-1122[EW] This non-standard usage of '_Alignof' retrieves the alignment of an array.
qac-1130[E] The '__has_include' operator is a language extension.
qac-1131[E] The '__has_include_next' operator is a language extension.
qac-2070Using [[attribute]] syntax.
qac-2071[E] This attribute syntax is a language extension.
qac-2072[E] This attribute specifier is unexpected in this source position, and will be ignored.
qac-3445[E] Conditional expression with middle operand omitted is a language extension.
qac-3664[E] Using a dot operator to access an individual bit is a language extension.
+
Rule-1.2-C99AdvisoryLanguage extensions should not be used
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-1077[C11] The keyword '_Noreturn' has been used.
qac-1078[C11] The keyword '_Alignas' has been used.
qac-1079[C11] The keyword '_Alignof' has been used.
qac-1081[C11] The keyword '_Atomic' has been used.
qac-1082[C11] The keyword '_Generic' has been used.
qac-1083[C11] The keyword '_Static_assert' has been used.
qac-1084[C11] The keyword '_Thread_local' has been used.
+
Rule-1.3RequiredThere shall be no occurrence of undefined or critical unspecified behaviour
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-0160[U] Using unsupported conversion specifier number %1s.
qac-0161[U] Unknown length modifier used with 'i' or 'd' conversion specifier, number %1s.
qac-0162[U] Unknown length modifier used with 'o' conversion specifier, number %1s.
qac-0163[U] Unknown length modifier used with 'u' conversion specifier, number %1s.
qac-0164[U] Unknown length modifier used with 'x' conversion specifier, number %1s.
qac-0165[U] Unknown length modifier used with 'X' conversion specifier, number %1s.
qac-0166[U] Unknown length modifier used with 'f' conversion specifier, number %1s.
qac-0167[U] Unknown length modifier used with 'e' conversion specifier, number %1s.
qac-0168[U] Unknown length modifier used with 'E' conversion specifier, number %1s.
qac-0169[U] Unknown length modifier used with 'g' conversion specifier, number %1s.
qac-0170[U] Unknown length modifier used with 'G' conversion specifier, number %1s.
qac-0171[U] Unknown length modifier used with 'c' conversion specifier, number %1s.
qac-0172[U] Unknown length modifier used with '%%' conversion specifier, number %1s.
qac-0173[U] Unknown length modifier used with 's' conversion specifier, number %1s.
qac-0174[U] Unknown length modifier used with 'n' conversion specifier, number %1s.
qac-0175[U] Unknown length modifier used with 'p' conversion specifier, number %1s.
qac-0176[U] Incomplete conversion specifier, number %1s.
qac-0177[U] Field width of format conversion specifier exceeds 509 characters.
qac-0178[U] Precision of format conversion specifier exceeds 509 characters.
qac-0179[U] Argument type does not match conversion specifier number %1s.
qac-0184[U] Insufficient arguments to satisfy conversion specifier, number %1s.
qac-0185[U] Call contains more arguments than conversion specifiers.
qac-0186[U] A call to this function must include at least one argument.
qac-0190[U] Using unsupported conversion specifier number %1s.
qac-0191[U] Unknown length modifier used with 'd/i/n' conversion specifier, number %1s.
qac-0192[U] Unknown length modifier used with 'o' conversion specifier, number %1s.
qac-0193[U] Unknown length modifier used with 'u' conversion specifier, number %1s.
qac-0194[U] Unknown length modifier used with 'x/X' conversion specifier, number %1s.
qac-0195[U] Unknown length modifier used with 'e/E/f/F/g/G' conversion specifier, number %1s.
qac-0196[U] Unknown length modifier used with 's' conversion specifier, number %1s.
qac-0197[U] Unknown length modifier used with 'p' conversion specifier, number %1s.
qac-0198[U] Unknown length modifier used with '%%' conversion specifier, number %1s.
qac-0199[U] Unknown length modifier used with '[' conversion specifier, number %1s.
qac-0200[U] Unknown length modifier used with 'c' conversion specifier, number %1s.
qac-0201[U] Incomplete conversion specifier, number %1s.
qac-0203[U] Value of character prior to '-' in '[]' is greater than following character.
qac-0204[U] Field width of format conversion specifier exceeds 509 characters.
qac-0206[U] Argument type does not match conversion specifier number %1s.
qac-0207[U] 'scanf' expects address of objects being stored into.
qac-0208[U] Same character occurs in scanset more than once.
qac-0235[U] Unknown escape sequence.
qac-0275[U] Floating value is out of range for conversion to destination type.
qac-0301[u] Cast between a pointer to object and a floating type.
qac-0302[u] Cast between a pointer to function and a floating type.
qac-0304[U] The address of an array declared 'register' may not be computed.
qac-0307[u] Cast between a pointer to object and a pointer to function.
qac-0309[U] Integral type is not large enough to hold a pointer value.
qac-0337[U] String literal has undefined value. This may be a result of using '#' on \\.
qac-0400[U] '%1s' is modified more than once between sequence points - evaluation order unspecified.
qac-0401[U] '%1s' may be modified more than once between sequence points - evaluation order unspecified.
qac-0402[U] '%1s' is modified and accessed between sequence points - evaluation order unspecified.
qac-0403[U] '%1s' may be modified and accessed between sequence points - evaluation order unspecified.
qac-0404[U] More than one read access to volatile objects between sequence points.
qac-0405[U] More than one modification of volatile objects between sequence points.
qac-0475[u] Operand of 'sizeof' is an expression designating a bit-field.
qac-0543[U] 'void' expressions have no value and may not be used in expressions.
qac-0544[U] The value of an incomplete 'union' may not be used.
qac-0545[U] The value of an incomplete 'struct' may not be used.
qac-0602[U] The identifier '%s' is reserved for use by the library.
qac-0603[U] The macro identifier '%s' is reserved.
qac-0623[U] '%s' has incomplete type and no linkage - this is undefined.
qac-0625[U] '%s' has been declared with both internal and external linkage - the behaviour is undefined.
qac-0626[U] '%s' has different type to previous declaration (which is no longer in scope).
qac-0630[U] More than one definition of '%s' (with external linkage).
qac-0632[U] Tentative definition of '%s' with internal linkage cannot have unknown size.
qac-0636[U] There are no named members in this 'struct' or 'union'.
qac-0654[U] Using 'const' or 'volatile' in a function return type is undefined.
qac-0658[U] Parameter cannot have 'void' type.
qac-0661[U] '%1s()' may not have a storage class specifier of '%2s' when declared at block scope.
qac-0667[U] '%s' is declared as a typedef and may not be redeclared as an object at an inner scope without an explicit type specifier.
qac-0668[U] '%s' is declared as a typedef and may not be redeclared as a member of a 'struct' or 'union' without an explicit type specifier.
qac-0672[U] The initializer for a 'struct', 'union' or array is not enclosed in braces.
qac-0676[u] Array element is of function type. Arrays cannot be constructed from function types.
qac-0678[u] Array element is array of unknown size. Arrays cannot be constructed from incomplete types.
qac-0680[u] Array element is 'void' or an incomplete 'struct' or 'union'. Arrays cannot be constructed from incomplete types.
qac-0706[U] Label '%s' is not unique within this function.
qac-0745[U] 'return;' found in '%s()', which has been defined with a non-'void' return type.
qac-0813[U] Using any of the characters ' " or /* in '#include <%s>' gives undefined behaviour.
qac-0814[U] Using the characters ' or /* in '#include "%s"' gives undefined behaviour.
qac-0836[U] Definition of macro named 'defined'.
qac-0837[U] Use of '#undef' to remove the operator 'defined'.
qac-0840[E] Extra tokens at end of #include directive.
qac-0848[U] Attempting to #undef '%s', which is a predefined macro name.
qac-0853[U] Macro arguments contain a sequence of tokens that has the form of a preprocessing directive.
qac-0854[U] Attempting to #define '%s', which is a predefined macro name.
qac-0864[U] '#line' directive specifies line number which is not in the range 1 to 32767.
qac-0865[U] '#line' directive is badly formed.
qac-0867[U] '#line' has not been followed by a line number.
qac-0872[U] Result of '##' operator is not a legal preprocessing token.
qac-0874[U] Character string literal and wide character string literal are adjacent.
qac-0885[U] The token 'defined' is generated in the expansion of this macro.
qac-0887[U] Use of 'defined' must match either 'defined(identifier)' or 'defined identifier'.
qac-0888[U] 'defined' requires an identifier as an argument.
qac-0905[U] Producing a universal character name through token concatenation is undefined.
qac-0914[U] Source file does not end with a newline character.
qac-0915[U] Source file ends with a backslash character followed by a newline.
qac-0942[U] A * can only be used to specify array size within function prototype scope.
qac-1119[U] Multiple declarations of '%1s' have different explicit alignments.
qac-1331Type or number of arguments doesn't match previous use of the function.
qac-1332Type or number of arguments doesn't match prototype found later.
qac-1333Type or number of arguments doesn't match function definition found later.
rcma-1509'%1s' has external linkage and has multiple definitions.
rcma-1510'%1s' has external linkage and has incompatible declarations.
qac-2726Definite: Use of uninitialized resource.
qac-2727Apparent: Use of uninitialized resource.
qac-2728Suspicious: Use of uninitialized resource.
qac-2731Definite: Use of destroyed resource.
qac-2732Apparent: Use of destroyed resource.
qac-2733Suspicious: Use of destroyed resource.
qac-2746Definite: Use of uninitialized file handle.
qac-2747Apparent: Use of uninitialized file handle.
qac-2748Suspicious: Use of uninitialized file handle.
qac-2800Constant: Overflow in signed arithmetic operation.
qac-2801Definite: Overflow in signed arithmetic operation.
qac-2802Apparent: Overflow in signed arithmetic operation.
qac-2803Suspicious: Overflow in signed arithmetic operation.
qac-2804Possible: Overflow in signed arithmetic tainted operation.
qac-2806Definite: Calling a standard library wide character handling function with an invalid character value.
qac-2807Apparent: Calling a standard library wide character handling function with an invalid character value.
qac-2808Suspicious: Calling a standard library wide character handling function with an invalid character value.
qac-2810Constant: Dereference of NULL pointer.
qac-2811Definite: Dereference of NULL pointer.
qac-2812Apparent: Dereference of NULL pointer.
qac-2813Suspicious: Dereference of NULL pointer.
qac-2820Constant: Arithmetic operation on NULL pointer.
qac-2821Definite: Arithmetic operation on NULL pointer.
qac-2822Apparent: Arithmetic operation on NULL pointer.
qac-2823Suspicious: Arithmetic operation on NULL pointer.
qac-2830Constant: Division by zero.
qac-2831Definite: Division by zero.
qac-2832Apparent: Division by zero.
qac-2833Suspicious: Division by zero.
qac-2840Constant: Dereference of an invalid pointer value.
qac-2841Definite: Dereference of an invalid pointer value.
qac-2842Apparent: Dereference of an invalid pointer value.
qac-2843Suspicious: Dereference of an invalid pointer value.
qac-2935Constant: Dereference of an invalid char pointer value.
qac-3113[U] 'return' statement includes no expression but function '%s()' is implicitly of type 'int'.
qac-3114[U] Function '%s()' is implicitly of type 'int' but ends without returning a value.
qac-3239[U] inline function '%1s' has external linkage, but is not defined within this translation unit.
qac-3311[u] An earlier jump to this statement will bypass the initialization of local variables.
qac-3312[u] This goto statement will jump into a previous block and bypass the initialization of local variables.
qac-3319[U] Function called with number of arguments which differs from number of parameters in definition.
qac-3320Type of argument no. %s differs from its type in definition of function.
qac-3437[u] '%1s' is only provided by the standard library as a macro, but is being used as a value here.
qac-3438[U] #undef'ing the assert macro to call a function of that name causes undefined behaviour.
qac-4866Definite: Memory is used after free.
qac-4867Apparent: Memory is used after free.
qac-4868Suspicious: Memory is used after free.
+
Rule-1.4RequiredEmergent language features shall not be used
+ + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-1077[C11] The keyword '_Noreturn' has been used.
qac-1078[C11] The keyword '_Alignas' has been used.
qac-1079[C11] The keyword '_Alignof' has been used.
qac-1081[C11] The keyword '_Atomic' has been used.
qac-1082[C11] The keyword '_Generic' has been used.
qac-1084[C11] The keyword '_Thread_local' has been used.
+
Rule-10.1RequiredOperands shall not be of an inappropriate essential type.
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-3101Unary '-' applied to an operand of type unsigned int or unsigned long gives an unsigned result.
qac-3102Unary '-' applied to an operand whose underlying type is unsigned.
qac-4115Operand of logical && or || operator is not an 'essentially Boolean' expression.
qac-4116Operand of logical ! operator is not an 'essentially Boolean' expression.
qac-4500An expression of 'essentially Boolean' type (%1s) is being used as an array subscript.
qac-4501An expression of 'essentially Boolean' type (%1s) is being used as the %2s operand of this arithmetic operator (%3s).
qac-4502An expression of 'essentially Boolean' type (%1s) is being used as the %2s operand of this bitwise operator (%3s).
qac-4503An expression of 'essentially Boolean' type (%1s) is being used as the left-hand operand of this shift operator (%2s).
qac-4504An expression of 'essentially Boolean' type (%1s) is being used as the right-hand operand of this shift operator (%2s).
qac-4505An expression of 'essentially Boolean' type (%1s) is being used as the %2s operand of this relational operator (%3s).
qac-4507An expression of 'essentially Boolean' type (%1s) is being used as the operand of this increment/decrement operator (%2s).
qac-4510An expression of 'essentially character' type (%1s) is being used as an array subscript.
qac-4511An expression of 'essentially character' type (%1s) is being used as the %2s operand of this arithmetic operator (%3s).
qac-4512An expression of 'essentially character' type (%1s) is being used as the %2s operand of this bitwise operator (%3s).
qac-4513An expression of 'essentially character' type (%1s) is being used as the left-hand operand of this shift operator (%2s).
qac-4514An expression of 'essentially character' type (%1s) is being used as the right-hand operand of this shift operator (%2s).
qac-4518An expression of 'essentially character' type (%1s) is being used as the %2s operand of this logical operator (%3s).
qac-4519An expression of 'essentially character' type (%1s) is being used as the first operand of this conditional operator (%2s).
qac-4521An expression of 'essentially enum' type (%1s) is being used as the %2s operand of this arithmetic operator (%3s).
qac-4522An expression of 'essentially enum' type (%1s) is being used as the %2s operand of this bitwise operator (%3s).
qac-4523An expression of 'essentially enum' type (%1s) is being used as the left-hand operand of this shift operator (%2s).
qac-4524An expression of 'essentially enum' type (%1s) is being used as the right-hand operand of this shift operator (%2s).
qac-4527An expression of 'essentially enum' type is being used as the operand of this increment/decrement operator.
qac-4528An expression of 'essentially enum' type (%1s) is being used as the %2s operand of this logical operator (%3s).
qac-4529An expression of 'essentially enum' type (%1s) is being used as the first operand of this conditional operator (%2s).
qac-4532An expression of 'essentially signed' type (%1s) is being used as the %2s operand of this bitwise operator (%3s).
qac-4533An expression of 'essentially signed' type (%1s) is being used as the left-hand operand of this shift operator (%2s).
qac-4534An expression of 'essentially signed' type (%1s) is being used as the right-hand operand of this shift operator (%2s).
qac-4538An expression of 'essentially signed' type (%1s) is being used as the %2s operand of this logical operator (%3s).
qac-4539An expression of 'essentially signed' type (%1s) is being used as the first operand of this conditional operator (%2s).
qac-4542A non-negative constant expression of 'essentially signed' type (%1s) is being used as the %2s operand of this bitwise operator (%3s).
qac-4543A non-negative constant expression of 'essentially signed' type (%1s) is being used as the left-hand operand of this shift operator (%2s).
qac-4548A non-negative constant expression of 'essentially signed' type (%1s) is being used as the %2s operand of this logical operator (%3s).
qac-4549A non-negative constant expression of 'essentially signed' type (%1s) is being used as the first operand of this conditional operator (%2s).
qac-4558An expression of 'essentially unsigned' type (%1s) is being used as the %2s operand of this logical operator (%3s).
qac-4559An expression of 'essentially unsigned' type (%1s) is being used as the first operand of this conditional operator (%2s).
qac-4568An expression of 'essentially floating' type (%1s) is being used as the %2s operand of this logical operator (%3s).
qac-4569An expression of 'essentially floating' type (%1s) is being used as the first operand of this conditional operator (%2s).
+
Rule-10.2RequiredExpressions of essentially character type shall not be used inappropriately in addition and subtraction operations
+ + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-1810An operand of 'essentially character' type is being added to another operand of 'essentially character' type.
qac-1811An operand of 'essentially character' type is being subtracted from an operand of 'essentially signed' type.
qac-1812An operand of 'essentially character' type is being subtracted from an operand of 'essentially unsigned' type.
qac-1813An operand of 'essentially character' type is being balanced with an operand of 'essentially floating' type in this arithmetic operation.
qac-1814An operand of 'essentially enum' type is being added or subtracted from an operand of 'essentially character' type.
+
Rule-10.3RequiredThe value of an expression shall not be assigned to an object with a narrower essential type or of a different essential type category.
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-0570This switch case label of 'essential type' '%1s', is not consistent with a controlling expression of essential type '%2s'.
qac-0572This switch case label of 'essential type' '%1s' is not consistent with a controlling expression which has an essential type of lower rank (%2s).
qac-1257An integer constant suffixed with L or LL is being converted to a type of lower rank on assignment.
qac-1264A suffixed floating constant is being converted to a different floating type on assignment.
qac-1265An unsuffixed floating constant is being converted to a different floating type on assignment.
qac-1266A floating constant is being converted to integral type on assignment.
qac-1291An integer constant of 'essentially unsigned' type is being converted to signed type on assignment.
qac-1292An integer constant of 'essentially signed' type is being converted to type char on assignment.
qac-1293An integer constant of 'essentially unsigned' type is being converted to type char on assignment.
qac-1294An integer constant of 'essentially signed' type is being converted to type _Bool on assignment.
qac-1295An integer constant of 'essentially unsigned' type is being converted to type _Bool on assignment.
qac-1296An integer constant of 'essentially signed' type is being converted to enum type on assignment.
qac-1297An integer constant of 'essentially unsigned' type is being converted to enum type on assignment.
qac-1298An integer constant of 'essentially signed' type is being converted to floating type on assignment.
qac-1299An integer constant of 'essentially unsigned' type is being converted to floating type on assignment.
qac-2850Constant: Implicit conversion to a signed integer type of insufficient size.
qac-2890Constant: Negative value implicitly converted to an unsigned type.
qac-2900Constant: Positive integer value truncated by implicit conversion to a smaller unsigned type.
qac-4401An expression of 'essentially Boolean' type (%1s) is being converted to character type, '%2s' on assignment.
qac-4402An expression of 'essentially Boolean' type (%1s) is being converted to enum type, '%2s' on assignment.
qac-4403An expression of 'essentially Boolean' type (%1s) is being converted to signed type, '%2s' on assignment.
qac-4404An expression of 'essentially Boolean' type (%1s) is being converted to unsigned type, '%2s' on assignment.
qac-4405An expression of 'essentially Boolean' type (%1s) is being converted to floating type, '%2s' on assignment.
qac-4410An expression of 'essentially character' type (%1s) is being converted to Boolean type, '%2s' on assignment.
qac-4412An expression of 'essentially character' type (%1s) is being converted to enum type, '%2s' on assignment.
qac-4413An expression of 'essentially character' type (%1s) is being converted to signed type, '%2s' on assignment.
qac-4414An expression of 'essentially character' type (%1s) is being converted to unsigned type, '%2s' on assignment.
qac-4415An expression of 'essentially character' type (%1s) is being converted to floating type, '%2s' on assignment.
qac-4420An expression of 'essentially enum' type (%1s) is being converted to Boolean type, '%2s' on assignment.
qac-4421An expression of 'essentially enum' type (%1s) is being converted to character type, '%2s' on assignment.
qac-4422An expression of 'essentially enum' type (%1s) is being converted to a different enum type, '%2s' on assignment.
qac-4423An expression of 'essentially enum' type (%1s) is being converted to signed type, '%2s' on assignment.
qac-4424An expression of 'essentially enum' type (%1s) is being converted to unsigned type, '%2s' on assignment.
qac-4425An expression of 'essentially enum' type (%1s) is being converted to floating type, '%2s' on assignment.
qac-4430An expression of 'essentially signed' type (%1s) is being converted to Boolean type, '%2s' on assignment.
qac-4431An expression of 'essentially signed' type (%1s) is being converted to character type, '%2s' on assignment.
qac-4432An expression of 'essentially signed' type (%1s) is being converted to enum type, '%2s' on assignment.
qac-4434A non-constant expression of 'essentially signed' type (%1s) is being converted to unsigned type, '%2s' on assignment.
qac-4435A non-constant expression of 'essentially signed' type (%1s) is being converted to floating type, '%2s' on assignment.
qac-4437A constant expression of 'essentially signed' type (%1s) is being converted to floating type, '%2s' on assignment.
qac-4440An expression of 'essentially unsigned' type (%1s) is being converted to Boolean type, '%2s' on assignment.
qac-4441An expression of 'essentially unsigned' type (%1s) is being converted to character type, '%2s' on assignment.
qac-4442An expression of 'essentially unsigned' type (%1s) is being converted to enum type, '%2s' on assignment.
qac-4443A non-constant expression of 'essentially unsigned' type (%1s) is being converted to a wider signed type, '%2s' on assignment.
qac-4445An expression of 'essentially unsigned' type (%1s) is being converted to floating type, '%2s' on assignment.
qac-4446A non-constant expression of 'essentially unsigned' type (%1s) is being converted to signed type, '%2s' on assignment.
qac-4447A constant expression of 'essentially unsigned' type (%1s) is being converted to signed type, '%2s' on assignment.
qac-4450An expression of 'essentially floating' type (%1s) is being converted to Boolean type, '%2s' on assignment.
qac-4451An expression of 'essentially floating' type (%1s) is being converted to character type, '%2s' on assignment.
qac-4452An expression of 'essentially floating' type (%1s) is being converted to enum type, '%2s' on assignment.
qac-4453An expression of 'essentially floating' type (%1s) is being converted to signed type, '%2s' on assignment.
qac-4454An expression of 'essentially floating' type (%1s) is being converted to unsigned type, '%2s' on assignment.
qac-4460A non-constant expression of 'essentially signed' type (%1s) is being converted to narrower signed type, '%2s' on assignment.
qac-4461A non-constant expression of 'essentially unsigned' type (%1s) is being converted to narrower unsigned type, '%2s' on assignment.
qac-4462A non-constant expression of 'essentially floating' type (%1s) is being converted to narrower floating type, '%2s' on assignment.
qac-4463A constant expression of 'essentially signed' type (%1s) is being converted to narrower signed type, '%2s' on assignment.
qac-4464A constant expression of 'essentially unsigned' type (%1s) is being converted to narrower unsigned type, '%2s' on assignment.
qac-4465A constant expression of 'essentially floating' type (%1s) is being converted to narrower floating type, '%2s' on assignment.
+
Rule-10.4RequiredBoth operands of an operator in which the usual arithmetic conversions are performed shall have the same essential type category
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-1800The %1s operand (essential type: '%2s') will be implicitly converted to a floating type, '%3s', in this arithmetic operation.
qac-1802The %1s operand (essential type: '%2s') will be implicitly converted to a floating type, '%3s', in this relational operation.
qac-1803The %1s operand (essential type: '%2s') will be implicitly converted to a floating type, '%3s', in this equality operation.
qac-1804The %1s operand (essential type: '%2s') will be implicitly converted to a floating type, '%3s', in this conditional operation.
qac-1820The %1s operand is non-constant and 'essentially signed' (%2s) but will be implicitly converted to an unsigned type (%3s) in this arithmetic operation.
qac-1821The %1s operand is non-constant and 'essentially signed' (%2s) but will be implicitly converted to an unsigned type (%3s) in this bitwise operation.
qac-1822The %1s operand is non-constant and 'essentially signed' (%2s) but will be implicitly converted to an unsigned type (%3s) in this relational operation.
qac-1823The %1s operand is non-constant and 'essentially signed' (%2s) but will be implicitly converted to an unsigned type (%3s) in this equality operation.
qac-1824The %1s operand is non-constant and 'essentially signed' (%2s) but will be implicitly converted to an unsigned type (%3s) in this conditional operation.
qac-1830The %1s operand is constant, 'essentially signed' (%2s) and negative but will be implicitly converted to an unsigned type (%3s) in this arithmetic operation.
qac-1831The %1s operand is constant, 'essentially signed' (%2s) and negative but will be implicitly converted to an unsigned type (%3s) in this bitwise operation.
qac-1832The %1s operand is constant, 'essentially signed' (%2s) and negative but will be implicitly converted to an unsigned type (%3s) in this relational operation.
qac-1833The %1s operand is constant, 'essentially signed' (%2s) and negative but will be implicitly converted to an unsigned type (%3s) in this equality operation.
qac-1834The %1s operand is constant, 'essentially signed' (%2s) and negative but will be implicitly converted to an unsigned type (%3s) in this conditional operation.
qac-1840The %1s operand is constant, 'essentially signed' (%2s) and non-negative but will be implicitly converted to an unsigned type (%3s) in this arithmetic operation.
qac-1841The %1s operand is constant, 'essentially signed' (%2s) and non-negative but will be implicitly converted to an unsigned type (%3s) in this bitwise operation.
qac-1842The %1s operand is constant, 'essentially signed' (%2s) and non-negative but will be implicitly converted to an unsigned type (%3s) in this relational operation.
qac-1843The %1s operand is constant, 'essentially signed' (%2s) and non-negative but will be implicitly converted to an unsigned type (%3s) in this equality operation.
qac-1844The %1s operand is constant, 'essentially signed' (%2s) and non-negative but will be implicitly converted to an unsigned type (%3s) in this conditional operation.
qac-1850The %1s operand is 'essentially unsigned' (%2s) but will be implicitly converted to a signed type (%3s) in this arithmetic operation.
qac-1851The %1s operand is 'essentially unsigned' (%2s) but will be implicitly converted to a signed type (%3s) in this bitwise operation.
qac-1852The %1s operand is 'essentially unsigned' (%2s) but will be implicitly converted to a signed type (%3s) in this relational operation.
qac-1853The %1s operand is 'essentially unsigned' (%2s) but will be implicitly converted to a signed type (%3s) in this equality operation.
qac-1854The %1s operand is 'essentially unsigned' (%2s) but will be implicitly converted to a signed type (%3s) in this conditional operation.
qac-1860The operands of this arithmetic operator are of different 'essential signedness' but will generate a result of type 'signed int'.
qac-1861The operands of this bitwise operator are of different 'essential signedness' but will generate a result of type 'signed int'.
qac-1862The operands of this relational operator are of different 'essential signedness' but will both be promoted to 'signed int' for comparison.
qac-1863The operands of this equality operator are of different 'essential signedness' but will both be promoted to 'signed int' for comparison.
qac-1864The 2nd and 3rd operands of this conditional operator are of different 'essential signedness'. The result will be in the promoted type 'signed int'.
qac-1880The operands of this relational operator are expressions of different 'essential type' categories (%1s and %2s).
qac-1881The operands of this equality operator are expressions of different 'essential type' categories (%1s and %2s).
qac-1882The 2nd and 3rd operands of this conditional operator are expressions of different 'essential type' categories (%1s and %2s).
+
Rule-10.5AdvisoryThe value of an expression should not be cast to an inappropriate essential type
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-4301An expression of 'essentially Boolean' type (%1s) is being cast to character type '%2s'.
qac-4302An expression of 'essentially Boolean' type (%1s) is being cast to enum type '%2s'.
qac-4303An expression of 'essentially Boolean' type (%1s) is being cast to signed type '%2s'.
qac-4304An expression of 'essentially Boolean' type (%1s) is being cast to unsigned type '%2s'.
qac-4305An expression of 'essentially Boolean' type (%1s) is being cast to floating type '%2s'.
qac-4310An expression of 'essentially character' type (%1s) is being cast to Boolean type, '%2s'.
qac-4312An expression of 'essentially character' type (%1s) is being cast to enum type, '%2s'.
qac-4315An expression of 'essentially character' type (%1s) is being cast to floating type, '%2s'.
qac-4320An expression of 'essentially enum' type (%1s) is being cast to Boolean type, '%2s'.
qac-4322An expression of 'essentially enum' type (%1s) is being cast to a different enum type, '%2s'.
qac-4330An expression of 'essentially signed' type (%1s) is being cast to Boolean type '%2s'.
qac-4332An expression of 'essentially signed' type (%1s) is being cast to enum type, '%2s'.
qac-4340An expression of 'essentially unsigned' type (%1s) is being cast to Boolean type '%2s'.
qac-4342An expression of 'essentially unsigned' type (%1s) is being cast to enum type '%2s'.
qac-4350An expression of 'essentially floating' type (%1s) is being cast to Boolean type '%2s'.
qac-4351An expression of 'essentially floating' type (%1s) is being cast to character type '%2s'.
qac-4352An expression of 'essentially floating' type (%1s) is being cast to enum type, '%2s'.
+
Rule-10.6RequiredThe value of a composite expression shall not be assigned to an object with wider essential type
+ + + + + + + + + + + + + + + + + +
QacDescription
qac-4490A composite expression of 'essentially signed' type (%1s) is being converted to wider signed type, '%2s' on assignment.
qac-4491A composite expression of 'essentially unsigned' type (%1s) is being converted to wider unsigned type, '%2s' on assignment.
qac-4492A composite expression of 'essentially floating' type (%1s) is being converted to wider floating type, '%2s' on assignment.
qac-4499An expression which is the result of a ~ or << operation has been converted to a wider essential type on assignment.
+
Rule-10.7RequiredIf a composite expression is used as one operand of an operator in which the usual arithmetic conversions are performed then the other operand shall not have wider essential type
+ + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-1890A composite expression of 'essentially signed' type (%1s) is being implicitly converted to a wider signed type, '%2s'.
qac-1891A composite expression of 'essentially unsigned' type (%1s) is being implicitly converted to a wider unsigned type, '%2s'.
qac-1892A composite expression of 'essentially floating' type (%1s) is being implicitly converted to a wider floating type, '%2s'.
qac-1893The 2nd and 3rd operands of this conditional operator are both 'essentially signed' ('%1s' and '%2s') but one is a composite expression of a narrower type than the other.
qac-1894The 2nd and 3rd operands of this conditional operator are both 'essentially unsigned' ('%1s' and '%2s') but one is a composite expression of a narrower type than the other.
qac-1895The 2nd and 3rd operands of this conditional operator are both 'essentially floating' ('%1s' and '%2s') but one is a composite expression of a narrower type than the other.
+
Rule-10.8RequiredThe value of a composite expression shall not be cast to a different essential type category or a wider essential type
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-4389A composite expression of 'essentially char' type (%1s) is being cast to a different type category, '%2s'.
qac-4390A composite expression of 'essentially signed' type (%1s) is being cast to a wider signed type, '%2s'.
qac-4391A composite expression of 'essentially unsigned' type (%1s) is being cast to a wider unsigned type, '%2s'.
qac-4392A composite expression of 'essentially floating' type (%1s) is being cast to a wider floating type, '%2s'.
qac-4393A composite expression of 'essentially signed' type (%1s) is being cast to a different type category, '%2s'.
qac-4394A composite expression of 'essentially unsigned' type (%1s) is being cast to a different type category, '%2s'.
qac-4395A composite expression of 'essentially floating' type (%1s) is being cast to a different type category, '%2s'.
qac-4398An expression which is the result of a ~ or << operation has been cast to a different essential type category.
qac-4399An expression which is the result of a ~ or << operation has been cast to a wider type.
+
Rule-11.1RequiredConversions shall not be performed between a pointer to a function and any other type
+ + + + + + + + + + + + + + + + + +
QacDescription
qac-0302[u] Cast between a pointer to function and a floating type.
qac-0305[I] Cast between a pointer to function and an integral type.
qac-0307[u] Cast between a pointer to object and a pointer to function.
qac-0313Casting to different function pointer type.
+
Rule-11.2RequiredConversions shall not be performed between a pointer to an incomplete type and any other type
+ + + + + + + + + + + + + + + + + +
QacDescription
qac-0308Non-portable cast involving pointer to an incomplete type.
qac-0323[C] Cast between a pointer to incomplete type and a floating type.
qac-0324[u] Cast between a pointer to incomplete type and an integral type.
qac-0325[u] Cast between a pointer to incomplete type and a pointer to function.
+
Rule-11.3RequiredA cast shall not be performed between a pointer to object type and a pointer to a different object type
+ + + + + + + + + +
QacDescription
qac-0310Casting to different object pointer type.
qac-3305Pointer cast to stricter alignment.
+
Rule-11.4AdvisoryA conversion should not be performed between a pointer to object and an integer type
+ + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-0303[I] Cast between a pointer to volatile object and an integral type.
qac-0306[I] Cast between a pointer to object and an integral type.
qac-0360An expression of pointer type is being converted to type _Bool on assignment.
qac-0361An expression of pointer type is being cast to type _Bool.
qac-0362An expression of essentially Boolean type is being cast to a pointer.
+
Rule-11.5AdvisoryA conversion should not be performed from pointer to void into pointer to object
+ + + + + + + + + +
QacDescription
qac-0316[I] Cast from a pointer to void to a pointer to object type.
qac-0317[I] Implicit conversion from a pointer to void to a pointer to object type.
+
Rule-11.6RequiredA cast shall not be performed between pointer to void and an arithmetic type
+ + + + + + + + + +
QacDescription
qac-0326[I] Cast between a pointer to void and an integral type.
qac-0327[C] Cast between a pointer to void and a floating type.
+
Rule-11.7RequiredA cast shall not be performed between pointer to object and a non-integer arithmetic type
+ + + + + + + + + +
QacDescription
qac-0301[u] Cast between a pointer to object and a floating type.
qac-0328[U] Cast between a pointer to object and an essential type other than signed/unsigned.
+
Rule-11.8RequiredA cast shall not remove any const or volatile qualification from the type pointed to by a pointer
+ + + + + + + + + +
QacDescription
qac-0311Dangerous pointer cast results in loss of const qualification.
qac-0312Dangerous pointer cast results in loss of volatile qualification.
+
Rule-11.9RequiredThe macro NULL shall be the only permitted form of integer null pointer constant
+ + + + + + + + + +
QacDescription
qac-3003This character constant is being interpreted as a null pointer constant.
qac-3004This integral constant expression is being interpreted as a null pointer constant.
+
Rule-12.1AdvisoryThe precedence of operators within expressions should be made explicit
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-3389Extra parentheses recommended to clarify the ordering of a % operator and another arithmetic operator (* / % + -).
qac-3391Extra parentheses recommended. A conditional operation is the operand of another conditional operator.
qac-3392Extra parentheses recommended. A shift, relational or equality operation is the operand of a second identical operator.
qac-3394Extra parentheses recommended. A shift, relational or equality operation is the operand of a different operator with the same precedence.
qac-3395Extra parentheses recommended. A * or / operation is the operand of a + or - operator.
qac-3396Extra parentheses recommended. A binary operation is the operand of a conditional operator.
qac-3397Extra parentheses recommended. A binary operation is the operand of a binary operator with different precedence.
qac-3433Expression operand to sizeof is not parenthesized.
+
Rule-12.2RequiredThe right hand operand of a shift operator shall lie in the range zero to one less than the width in bits of the essential type of the left hand operand
+ + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-0499Right operand of shift operator is greater than or equal to the width of the essential type of the left operand.
qac-2790Constant: Right hand operand of shift operator is negative or too large.
qac-2791Definite: Right hand operand of shift operator is negative or too large.
qac-2792Apparent: Right hand operand of shift operator is negative or too large.
qac-2793Suspicious: Right hand operand of shift operator is negative or too large.
qac-2794Possible: Tainted right hand operand of shift operator is negative or too large.
+
Rule-12.3AdvisoryThe comma operator should not be used
+ + + + + + + + + +
QacDescription
qac-3417The comma operator has been used outside a 'for' statement.
qac-3418The comma operator has been used in a 'for' statement.
+
Rule-12.4AdvisoryEvaluation of constant expressions should not lead to unsigned integer wrap-around
+ + + + + +
QacDescription
qac-2910Constant: Wraparound in unsigned arithmetic operation.
+
Rule-12.5MandatoryThe sizeof operator shall not have an operand which is a function parameter declared as 'array of type'
+ + + + + +
QacDescription
qac-1321Operand of sizeof is a function parameter of array type.
+
Rule-13.1RequiredInitializer lists shall not contain persistent side-effects
+ + + + + +
QacDescription
qac-3421Expression with persistent side effects is used in an initializer list.
+
Rule-13.2RequiredThe value of an expression and its persistent side-effects shall be the same under all permitted evaluation orders
+ + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-0400[U] '%1s' is modified more than once between sequence points - evaluation order unspecified.
qac-0401[U] '%1s' may be modified more than once between sequence points - evaluation order unspecified.
qac-0402[U] '%1s' is modified and accessed between sequence points - evaluation order unspecified.
qac-0403[U] '%1s' may be modified and accessed between sequence points - evaluation order unspecified.
qac-0404[U] More than one read access to volatile objects between sequence points.
qac-0405[U] More than one modification of volatile objects between sequence points.
+
Rule-13.3AdvisoryA full expression containing an increment (++) or decrement (--) operator should have no other potential side effects other than that caused by the increment or decrement operator
+ + + + + + + + + +
QacDescription
qac-3387A full expression containing an increment (++) or decrement (--) operator should have no potential side effects other than that caused by the increment or decrement operator.
qac-3440Using the value resulting from a ++ or -- operation.
+
Rule-13.4AdvisoryThe result of an assignment operator should not be used
+ + + + + + + + + +
QacDescription
qac-3226The result of an assignment is being used in an arithmetic operation or another assigning operation.
qac-3326The result of an assignment is being used in a logical operation.
+
Rule-13.5RequiredThe right hand operand of a logical && or || operator shall not contain persistent side effects
+ + + + + +
QacDescription
qac-3415Right hand operand of '&&' or '||' is an expression with persistent side effects.
+
Rule-13.6MandatoryThe operand of the sizeof operator shall not contain any expression which has potential side-effects
+ + + + + + + + + +
QacDescription
qac-0945[C99] Operand of sizeof is an expression of variable length array type with side effects.
qac-3307The operand of 'sizeof' is an expression with implied side effects, but they will not be evaluated.
+
Rule-14.1RequiredA loop counter shall not have essentially floating type
+ + + + + + + + + +
QacDescription
qac-3339Floating point variable used as 'while' loop control variable.
qac-3340Floating point variable used as 'for' loop control variable.
+
Rule-14.2RequiredA for loop shall be well-formed
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-2461Loop control variable in this 'for' statement, %1s, has file scope.
qac-2462The variable initialized in the first expression of this 'for' statement is not the variable identified as the 'loop control variable' (%1s).
qac-2463The variable incremented in the third expression of this 'for' statement is not the variable identified as the 'loop control variable' (%1s).
qac-2464Loop control variable, %1s, modified twice in for-loop header.
qac-2467Loop control variable in this 'for' statement, %1s, is not modified inside loop.
qac-2468Loop control variable in this 'for' statement, %1s, is not modified inside loop but has file scope.
qac-2469Loop control variable in this 'for' statement, %1s, is modified in the body of the loop.
qac-2471Unable to identify a 'for' loop control variable.
qac-2472More than one possible 'for' loop control variable.
qac-2476No variable appears to be initialized in the first expression of this 'for' statement (control variable identified as %1s).
qac-2477No variable appears to be incremented in the third expression of this 'for' statement (control variable identified as %1s).
+
Rule-14.3RequiredControlling expressions shall not be invariant
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-2741This 'if' controlling expression is a constant expression and its value is 'true'.
qac-2742This 'if' controlling expression is a constant expression and its value is 'false'.
qac-2990The value of this loop controlling expression is always 'true'.
qac-2991The value of this 'if' controlling expression is always 'true'.
qac-2992The value of this 'if' controlling expression is always 'false'.
qac-2993The value of this 'do - while' loop controlling expression is always 'false'. The loop will only be executed once.
qac-2994The value of this 'while' or 'for' loop controlling expression is always 'false'. The loop will not be entered.
qac-2997The first operand of this conditional operator is always 'true'.
qac-2998The first operand of this conditional operator is always 'false'.
qac-3493The first operand of this conditional operator is always constant 'true'.
qac-3494The first operand of this conditional operator is always constant 'false'.
+
Rule-14.4RequiredThe controlling expression of an if-statement and the controlling expression of an iteration-statement shall have essentially Boolean type
+ + + + + +
QacDescription
qac-3344Controlling expression is not an 'essentially Boolean' expression.
+
Rule-15.1AdvisoryThe goto statement should not be used
+ + + + + +
QacDescription
qac-2001A 'goto' statement has been used.
+
Rule-15.2RequiredThe goto statement shall jump to a label declared later in the same function
+ + + + + +
QacDescription
qac-3310This 'goto' statement involves a backward jump.
+
Rule-15.3RequiredAny label referenced by a goto statement shall be declared in the same block, or in any block enclosing the goto statement
+ + + + + +
QacDescription
qac-3327This goto statement references a label that is declared in a separate block.
+
Rule-15.4AdvisoryThere should be no more than one break or goto statement used to terminate any iteration statement
+ + + + + + + + + +
QacDescription
qac-0771More than one 'break' statement has been used to terminate this iteration statement.
qac-0772Multiple jump statements are used to terminate this iteration statement, including 'goto'.
+
Rule-15.5AdvisoryA function should have a single point of exit at the end
+ + + + + +
QacDescription
qac-2889This function has more than one 'return' path.
+
Rule-15.6RequiredThe body of an iteration-statement or a selection-statement shall be a compound-statement
+ + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-2212Body of control statement is not enclosed within braces.
qac-2214Body of control statement is on the same line and is not enclosed within braces.
qac-2218Body of switch statement is not enclosed within braces.
qac-2219Body of switch statement is on the same line and is not enclosed within braces.
qac-3402Braces are needed to clarify the structure of this 'if'-'if'-'else' statement.
+
Rule-15.7RequiredAll if ... else if constructs shall be terminated with an else statement
+ + + + + + + + + +
QacDescription
qac-2004No concluding 'else' exists in this 'if'-'else'-'if' statement.
qac-2013This 'if .. else if ' construct 'else' statement is empty.
+
Rule-16.1RequiredAll switch statements shall be well-formed
+ + + + + + + + + +
QacDescription
qac-2008Code statements precede the first label in this 'switch' construct.
qac-3234Declarations precede the first label in this 'switch' construct.
+
Rule-16.2RequiredA switch label shall only be used when the most closely-enclosing compound statement is the body of a switch statement
+ + + + + +
QacDescription
qac-2019'Switch' label is located within a nested code block.
+
Rule-16.3RequiredAn unconditional break statement shall terminate every switch-clause
+ + + + + + + + + + + + + + + + + +
QacDescription
qac-2003The preceding 'switch' clause is not empty and does not end with a 'jump' statement. Execution will fall through.
qac-2020Final 'switch' clause does not end with an explicit 'jump' statement.
qac-2023The preceding 'switch' clause is not empty and ends with a jump statement other than 'break'.
qac-2024Final 'switch' clause ends with a 'jump' statement other than 'break'.
+
Rule-16.4RequiredEvery switch statement shall have a default label
+ + + + + + + + + +
QacDescription
qac-2002No 'default' label found in this 'switch' statement.
qac-2016This 'switch' statement 'default' clause is empty.
+
Rule-16.5RequiredA default label shall appear as either the first or the last switch label of a switch statement
+ + + + + +
QacDescription
qac-2012This 'default' label is neither the first nor the last label within the 'switch' block.
+
Rule-16.6RequiredEvery switch statement shall have at least two switch-clauses
+ + + + + +
QacDescription
qac-3315This 'switch' statement is redundant.
+
Rule-16.7RequiredA switch-expression shall not have essentially Boolean type
+ + + + + +
QacDescription
qac-0735Switch expression is of essentially Boolean type.
+
Rule-17.1RequiredThe features of shall not be used
+ + + + + + + + + + + + + +
QacDescription
qac-1337Function defined with a variable number of parameters.
m3cm-5130Use of standard header file .
m3cm-5140Use of variable arguments handling identifier: va_arg, va_start, va_end, va_copy
+
Rule-17.2RequiredFunctions shall not call themselves, either directly or indirectly
+ + + + + + + + + +
QacDescription
rcma-1520Functions are indirectly recursive.
qac-3670Recursive call to function containing this call.
+
Rule-17.3MandatoryA function shall not be declared implicitly
+ + + + + +
QacDescription
qac-3335No function declaration. Implicit declaration inserted: 'extern int %s();'.
+
Rule-17.4MandatoryAll exit paths from a function with non-void return type shall have an explicit return statement with an expression
+ + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-0745[U] 'return;' found in '%s()', which has been defined with a non-'void' return type.
qac-2887Function 'main' ends with an implicit 'return' statement.
qac-2888This function has been declared with a non-void 'return' type but ends with an implicit 'return ;' statement.
qac-3113[U] 'return' statement includes no expression but function '%s()' is implicitly of type 'int'.
qac-3114[U] Function '%s()' is implicitly of type 'int' but ends without returning a value.
+
Rule-17.5AdvisoryThe function argument corresponding to a parameter declared to have an array type shall have an appropriate number of elements
+ + + + + + + + + + + + + + + + + +
QacDescription
qac-2781Definite: Function argument has fewer elements than the array dimension in the parameter declaration for non-inlined call.
qac-2782Apparent: Function argument has fewer elements than the array dimension in the parameter declaration for non-inlined call.
qac-2783Suspicious: Function argument has fewer elements than the array dimension in the parameter declaration for non-inlined call.
qac-2784Possible: Function argument has fewer elements than the array dimension in the parameter declaration for non-inlined call.
+
Rule-17.6MandatoryThe declaration of an array parameter shall not contain the static keyword between the [ ]
+ + + + + +
QacDescription
qac-1058[C99] The keyword 'static' is used in the declaration of a function parameter of array type.
+
Rule-17.7RequiredThe value returned by a function having non-void return type shall be used
+ + + + + +
QacDescription
qac-3200'%s' returns a value which is not being used.
+
Rule-17.8AdvisoryA function parameter should not be modified
+ + + + + + + + + + + + + +
QacDescription
qac-1338The parameter '%1s' is being modified.
qac-1339Evaluating the address of the parameter '%1s'.
qac-1340Storing the address of the parameter '%1s' in a constant pointer.
+
Rule-18.1RequiredA pointer resulting from arithmetic on a pointer operand shall address an element of the same array as that pointer operand
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-2840Constant: Dereference of an invalid pointer value.
qac-2841Definite: Dereference of an invalid pointer value.
qac-2842Apparent: Dereference of an invalid pointer value.
qac-2843Suspicious: Dereference of an invalid pointer value.
qac-2930Constant: Computing an invalid pointer value.
qac-2931Definite: Computing an invalid pointer value.
qac-2932Apparent: Computing an invalid pointer value.
qac-2933Suspicious: Computing an invalid pointer value.
qac-2935Constant: Dereference of an invalid char pointer value.
qac-2936Definite: Dereference of an invalid char pointer value.
qac-2937Apparent: Dereference of an invalid char pointer value.
qac-2938Suspicious: Dereference of an invalid char pointer value.
+
Rule-18.2RequiredSubtraction between pointers shall only be applied to pointers that address elements of the same array
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-2668Subtraction of a pointer to an array and a pointer to a non-array.
qac-2761Definite: Subtracting pointers that address different objects.
qac-2762Apparent: Subtracting pointers that address different objects.
qac-2763Suspicious: Subtracting pointers that address different objects.
qac-2766Definite: Subtracting pointers that address different members of the same object.
qac-2767Apparent: Subtracting pointers that address different members of the same object.
qac-2768Suspicious: Subtracting pointers that address different members of the same object.
+
Rule-18.3RequiredThe relational operators >, >=, < and <= shall not be applied to objects of pointer type except where they point into the same object
+ + + + + + + + + + + + + + + + + +
QacDescription
qac-2669Comparison of a pointer to an array and a pointer to a non-array.
qac-2771Definite: Comparing pointers that address different objects.
qac-2772Apparent: Comparing pointers that address different objects.
qac-2773Suspicious: Comparing pointers that address different objects.
+
Rule-18.4AdvisoryThe +, -, += and -= operators should not be applied to an expression of pointer type
+ + + + + +
QacDescription
qac-0488Performing pointer arithmetic.
+
Rule-18.5AdvisoryDeclarations should contain no more than two levels of pointer nesting
+ + + + + + + + + + + + + + + + + +
QacDescription
qac-3260Typedef defined with more than 2 levels of indirection.
qac-3261Member of struct/union defined with more than 2 levels of indirection.
qac-3262Object defined or declared with more than 2 levels of indirection.
qac-3263Function defined or declared with a return type which has more than 2 levels of indirection.
+
Rule-18.6RequiredThe address of an object with automatic storage shall not be copied to another object that persists after the first object has ceased to exist
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-2916Definite: Storing the address of an object in a pointer that has greater lifetime.
qac-2917Apparent: Storing the address of an object in a pointer that has greater lifetime.
qac-2918Suspicious: Storing the address of an object in a pointer that has greater lifetime.
qac-3217Address of automatic object exported to a pointer with linkage or wider scope.
qac-3225Address of automatic object exported using a function parameter.
qac-3230Address of automatic object assigned to local pointer with static storage duration.
qac-4140Address of automatic object exported in function return value.
+
Rule-18.7RequiredFlexible array members shall not be declared
+ + + + + +
QacDescription
qac-1060[C99] A flexible array member has been declared.
+
Rule-18.8RequiredVariable-length array types shall not be used
+ + + + + + + + + +
QacDescription
qac-1051[C99] A variable length array has been declared.
qac-1052[C99] A variable length array of unspecified size has been declared.
+
Rule-19.1MandatoryAn object shall not be assigned or copied to an overlapping object
+ + + + + + + + + + + + + + + + + +
QacDescription
qac-0681[U] Assignment between two incompatible members of the same union.
qac-2776Definite: Copy between overlapping objects.
qac-2777Apparent: Copy between overlapping objects.
qac-2778Suspicious: Copy between overlapping objects.
+
Rule-19.2AdvisoryThe union keyword should not be used
+ + + + + + + + + +
QacDescription
qac-0750A union type specifier has been defined.
qac-0759An object of union type has been defined.
+
Rule-2.1RequiredA project shall not contain unreachable code
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-0594Negative 'case' label expression is incompatible with unsigned controlling expression in 'switch' statement.
qac-1460'Switch' label value, %1s, not contained in enum type.
rcma-1503The function '%1s' is defined but is not used within this project.
qac-2744This 'while' or 'for' loop controlling expression is a constant expression and its value is 'false'. The loop will not be entered.
qac-2880This code is unreachable.
qac-2882This 'switch' statement will bypass the initialization of local variables.
qac-3219Static function '%1s()' is not used within this translation unit.
+
Rule-2.2RequiredThere shall be no dead code
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-2980The value of this function parameter is never used before being modified.
qac-2981This initialization is redundant. The value of this object is never used before being modified.
qac-2982This assignment is redundant. The value of this object is never used before being modified.
qac-2983This assignment is redundant. The value of this object is never subsequently used.
qac-2985This operation is redundant. The value of the result is always that of the left-hand operand.
qac-2986This operation is redundant. The value of the result is always that of the right-hand operand.
qac-2987This function call produces no side effects and is redundant.
qac-2995The result of this logical operation is always 'true'.
qac-2996The result of this logical operation is always 'false'.
qac-3110The left-hand operand of this ',' has no side effects.
qac-3112This statement has no side-effect - it can be removed.
qac-3404Statement contains a redundant * operator at top level. *p++ means *(p++) not (*p)++.
qac-3422Statement contains a redundant operator at top level.
qac-3423Statement contains a redundant cast at top level.
qac-3424Statement contains a redundant & or | at top level.
qac-3425One branch of this conditional operation is a redundant expression.
qac-3426Right hand side of comma expression has no side effect and its value is not used.
qac-3427Right hand side of logical operator has no side effect and its value is not used.
+
Rule-2.3AdvisoryA project should not contain unused type declarations
+ + + + + + + + + +
QacDescription
rcma-1535The typedef '%1s' is declared but not used within this project.
qac-3205The identifier '%1s' is not used and could be removed.
+
Rule-2.4AdvisoryA project should not contain unused tag declarations
+ + + + + + + + + + + + + +
QacDescription
rcma-1536The tag '%1s' is declared but not used within this project.
rcma-1755The tag '%1s' is declared but not used within this project.
qac-3213The tag '%1s' is not used and could be removed.
+
Rule-2.5AdvisoryA project should not contain unused macro declarations
+ + + + + +
QacDescription
rcma-1534The macro '%1s' is declared but not used within this project.
+
Rule-2.6AdvisoryA function should not contain unused label declarations
+ + + + + +
QacDescription
qac-3202The label '%s:' is not used in this function and could be removed.
+
Rule-2.7AdvisoryThere should be no unused parameters in functions
+ + + + + +
QacDescription
qac-3206The parameter '%s' is not used in this function.
+
Rule-20.1Advisory#include directives should only be preceded by preprocessor directives or comments
+ + + + + +
QacDescription
m3cm-5087Use of #include directive after code fragment.
+
Rule-20.10AdvisoryThe # and ## preprocessor operators should not be used
+ + + + + + + + + +
QacDescription
qac-0341Using the stringify operator '#'.
qac-0342Using the glue operator '##'.
+
Rule-20.11RequiredA macro parameter immediately following a # operator shall not immediately be followed by a ## operator
+ + + + + +
QacDescription
qac-0892This macro parameter is preceded by '#' and followed by '##'.
+
Rule-20.12RequiredA macro parameter used as an operand to the # or ## operators, which is itself subject to further macro replacement, shall only be used as an operand to these operators
+ + + + + + + + + +
QacDescription
qac-0893Macro argument '%1s' is both expanded as a macro and used with the '%2s' operator.
qac-0895Macro parameter '%s' is inconsistently subject to macro replacement.
+
Rule-20.13RequiredA line whose first token is # shall be a valid preprocessing directive
+ + + + + +
QacDescription
qac-3115Unrecognized preprocessing directive has been ignored because of conditional inclusion directives.
+
Rule-20.14RequiredAll #else, #elif and #endif preprocessor directives shall reside in the same file as the #if, #ifdef or #ifndef directive to which they are related
+ + + + + + + + + +
QacDescription
qac-3317'#if...' not matched by '#endif' in included file. This is probably an error.
qac-3318'#else'/'#elif'/'#endif' in included file matched '#if...' in parent file. This is probably an error.
+
Rule-20.2RequiredThe ', " or \ characters and the /* or // character sequences shall not occur in a header file name
+ + + + + + + + + + + + + +
QacDescription
qac-0813[U] Using any of the characters ' " or /* in '#include <%s>' gives undefined behaviour.
qac-0814[U] Using the characters ' or /* in '#include "%s"' gives undefined behaviour.
qac-0831[E] Use of '\\' in this '#include' line is a PC extension - this usage is non-portable.
+
Rule-20.3RequiredThe #include directive shall be followed by either a or "filename" sequence
+ + + + + + + + + + + + + +
QacDescription
qac-0817[S] Closing quote or bracket '>' missing from include filename.
qac-0821[C] '#include' does not identify a header or source file that can be processed.
qac-0840[E] Extra tokens at end of #include directive.
+
Rule-20.4RequiredA macro shall not be defined with the same name as a keyword
+ + + + + +
QacDescription
qac-3439Macro redefines a keyword.
+
Rule-20.4-C99RequiredA macro shall not be defined with the same name as a keyword
+ + + + + +
QacDescription
qac-3468The name of this macro is a reserved identifier in C90 and a keyword in C99.
+
Rule-20.5Advisory#undef should not be used
+ + + + + +
QacDescription
qac-0841Using '#undef'.
+
Rule-20.6RequiredTokens that look like a preprocessing directive shall not occur within a macro argument
+ + + + + +
QacDescription
qac-0853[U] Macro arguments contain a sequence of tokens that has the form of a preprocessing directive.
+
Rule-20.7RequiredExpressions resulting from the expansion of macro parameters shall be enclosed in parentheses
+ + + + + + + + + +
QacDescription
qac-3430Macro argument expression may require parentheses.
qac-3432Simple macro argument expression is not parenthesized.
+
Rule-20.8RequiredThe controlling expression of a #if or #elif preprocessing directive shall evaluate to 0 or 1
+ + + + + +
QacDescription
qac-0894'#%s' directive controlling expression does not evaluate to zero or one.
+
Rule-20.9RequiredAll identifiers used in the controlling expression of #if or #elif preprocessing directives shall be #define'd before evaluation
+ + + + + +
QacDescription
qac-3332The macro '%s' used in this '#if' or '#elif' expression is not defined.
+
Rule-21.1Required#define and #undef shall not be used on a reserved identifier or reserved macro name
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-0603[U] The macro identifier '%s' is reserved.
qac-0836[U] Definition of macro named 'defined'.
qac-0837[U] Use of '#undef' to remove the operator 'defined'.
qac-0848[U] Attempting to #undef '%s', which is a predefined macro name.
qac-0854[U] Attempting to #define '%s', which is a predefined macro name.
qac-4600The macro '%1s' is also defined in '<%2s>'.
qac-4601The macro '%1s' is the name of an identifier in '<%2s>'.
qac-4620The macro '%1s' may also be defined as a macro in '<%2s>'.
qac-4621The macro '%1s' may also be defined as a typedef in '<%2s>'.
+
Rule-21.10RequiredThe Standard Library time and date functions shall not be used
+ + + + + + + + + +
QacDescription
m3cm-5127Use of standard header file .
m3cm-5139Use of time handling identifier: clock, difftime, mktime, time, timespec_get, asctime, ctime, gmtime, localtime, strftime, wcsftime
+
Rule-21.11RequiredThe standard header file shall not be used
+ + + + + + + + + +
QacDescription
m3cm-5131Use of standard header file .
m3cm-5141Use of type-generic math identifier
+
Rule-21.12AdvisoryThe exception handling features of should not be used
+ + + + + +
QacDescription
m3cm-5136Use of exception handling identifier: feclearexcept, fegetexceptflag, feraiseexcept, fesetexceptflag or fetestexcept.
+
Rule-21.13MandatoryAny value passed to a function in shall be representable as an unsigned char or be the value EOF
+ + + + + + + + + + + + + +
QacDescription
qac-2796Definite: Calling a standard library character handling function with an invalid character value.
qac-2797Apparent: Calling a standard library character handling function with an invalid character value.
qac-2798Suspicious: Calling a standard library character handling function with an invalid character value.
+
Rule-21.14RequiredThe Standard Library function memcmp shall not be used to compare null terminated strings
+ + + + + + + + + +
QacDescription
qac-2785Constant: Null terminated string is being passed as argument to Standard Library function memcmp.
qac-2786Definite: Null terminated string is being passed as argument to Standard Library function memcmp.
+
Rule-21.15RequiredThe pointer arguments to the Standard Library functions memcpy, memmove and memcmp shall be pointers to qualified or unqualified versions of compatible types
+ + + + + + + + + + + + + +
QacDescription
qac-1487[I] Comparing the representations of objects of different types.
qac-1495[I] Destination and source objects have incompatible types.
qac-1496[I] Destination and source objects may have incompatible types.
+
Rule-21.16RequiredThe pointer arguments to the Standard Library function memcpy shall point to either a pointer type, an essentially signed type, an essentially unsigned type, an essentially Boolean type or an essentially enum type
+ + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-1488[I] Comparison of a struct object representation.
qac-1489[I] Comparison of a union object representation.
qac-1490[I] Comparison of a floating point object representation.
qac-1491[I] Comparison of an object representation.
qac-1497Comparison of a string object representation.
+
Rule-21.17MandatoryUse of the string handling functions from shall not result in accesses beyond the bounds of the objects referenced by their pointer parameters
+ + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-2835Constant: Non-null terminated string used in a string function.
qac-2836Definite: Non-null terminated string used in a string function.
qac-2935Constant: Dereference of an invalid char pointer value.
qac-2936Definite: Dereference of an invalid char pointer value.
qac-2937Apparent: Dereference of an invalid char pointer value.
qac-2938Suspicious: Dereference of an invalid char pointer value.
+
Rule-21.18MandatoryThe size_t argument passed to any function in shall have an appropriate value
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-2840Constant: Dereference of an invalid pointer value.
qac-2841Definite: Dereference of an invalid pointer value.
qac-2842Apparent: Dereference of an invalid pointer value.
qac-2843Suspicious: Dereference of an invalid pointer value.
qac-2845Constant: Maximum number of characters to be read/written is larger than the target buffer size.
qac-2846Definite: Maximum number of characters to be read/written is larger than the target buffer size.
qac-2847Apparent: Maximum number of characters to be read/written is larger than the target buffer size.
qac-2848Suspicious: Maximum number of characters to be read/written is larger than the target buffer size.
qac-2865Constant: Using 0 as size parameter of a function call.
qac-2866Definite: Using 0 as size parameter of a function call.
qac-2867Apparent: Using 0 as size parameter of a function call.
qac-2868Suspicious: Using 0 as size parameter of a function call.
qac-2935Constant: Dereference of an invalid char pointer value.
qac-2936Definite: Dereference of an invalid char pointer value.
qac-2937Apparent: Dereference of an invalid char pointer value.
qac-2938Suspicious: Dereference of an invalid char pointer value.
qac-4880Constant: Pointed to object has smaller size than the size_t argument.
qac-4881Definite: Pointed to object has smaller size than the size_t argument.
qac-4882Apparent: Pointed to object has smaller size than the size_t argument.
qac-4883Suspicious: Pointed to object has smaller size than the size_t argument.
+
Rule-21.19MandatoryThe pointers returned by the Standard Library functions lovaleconv, getenv, setlocale or strerror shall only be used as if they have pointer to const-qualified type
+ + + + + + + + + + + + + + + + + +
QacDescription
qac-1492The result of library function '%1s' is used to modify the referenced object.
qac-1493The result of library function '%1s' is used as a pointer to a modifiable object.
qac-1494The result of library function '%1s' might be modified.
qac-1498The string referenced by type 'struct lconv' member '%1s' is being modified.
+
Rule-21.2RequiredA reserved identifier or macro name shall not be declared
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-0602[U] The identifier '%s' is reserved for use by the library.
qac-4602The identifier '%1s' is declared as a macro in '<%2s>'.
qac-4603The object/function '%1s'is being defined with the same name as an ordinary identifier defined in '<%2s>'.
qac-4604The object/function '%1s' is being declared with the same name as an ordinary identifier defined in '<%2s>'.
qac-4605The typedef '%1s' is also defined in '<%2s>'.
qac-4606The typedef '%1s' has the same name as another ordinary identifier in '<%2s>'.
qac-4607The enum constant '%1s' has the same name as another ordinary identifier in '<%2s>'.
qac-4608The tag '%1s' is also defined in '<%2s>'.
+
Rule-21.20MandatoryThe pointer returned by the Standard Library functions asctime, ctime, gmtime, localtime, localeconv, getenv, setlocale, or strerror shall not be used following a subsequent call to the same function
+ + + + + + + + + + + + + +
QacDescription
qac-2681Definite: Using an invalidated value '%s' returned from a Standard Library function.
qac-2682Apparent: Using an invalidated value '%s' returned from a Standard Library function.
qac-2683Suspicious: Using an invalidated value '%s' returned from a Standard Library function.
+
Rule-21.21RequiredThe Standard Library system of shall not be used
+ + + + + +
QacDescription
m3cm-5150Use of function: system.
+
Rule-21.3RequiredThe memory allocation and deallocation functions of shall not be used
+ + + + + +
QacDescription
m3cm-5118Use of memory allocation or deallocation function: calloc, malloc, realloc or free.
+
Rule-21.4RequiredThe standard header file shall not be used
+ + + + + + + + + +
QacDescription
m3cm-5132Use of standard header file .
m3cm-5137Use of nonlocal jump identifier: setjmp, longjmp
+
Rule-21.5RequiredThe standard header file shall not be used
+ + + + + + + + + +
QacDescription
m3cm-5123Use of standard header file .
m3cm-5138Use of signal handling identifier: signal, raise
+
Rule-21.6RequiredThe Standard Library input/output functions shall not be used
+ + + + + +
QacDescription
m3cm-5124The Standard Library input/output functions shall not be used
+
Rule-21.7RequiredThe atof, atoi, atol and atoll functions of shall not be used
+ + + + + +
QacDescription
m3cm-5125Use of function: atof, atoi, atol or atoll.
+
Rule-21.8RequiredThe Standard Library termination functions of shall not be used
+ + + + + + + + + + + + + + + + + +
QacDescription
m3cm-5128Use of function: getenv.
m3cm-5150Use of function: system.
m3cm-5151Use of function: abort or exit.
m3cm-5152Use of function: _Exit or quick_exit.
+
Rule-21.9RequiredThe library functions bsearch and qsort of shall not be used
+ + + + + +
QacDescription
m3cm-5135Use of function: bsearch or qsort.
+
Rule-22.1RequiredAll resources obtained dynamically by means of Standard Library functions shall be explicitly released
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-2701Definite: Opened file is not closed.
qac-2702Apparent: Opened file is not closed.
qac-2703Suspicious: Opened file is not closed.
qac-2706Definite: Allocated memory is not deallocated.
qac-2707Apparent: Allocated memory is not deallocated.
qac-2708Suspicious: Allocated memory is not deallocated.
qac-2736Definite: Created resource is not destroyed.
qac-2737Apparent: Created resource is not destroyed.
qac-2738Suspicious: Created resource is not destroyed.
+
Rule-22.10RequiredThe value of errno shall only be tested when the last function to be called was an errno-setting-function
+ + + + + +
QacDescription
qac-2503Testing of 'errno' is not immediately preceded by a call to an 'errno' setting function.
+
Rule-22.2MandatoryA block of memory shall only be freed if it was allocated by means of a Standard Library function
+ + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-2716Definite: Memory deallocated multiple times.
qac-2717Apparent: Memory deallocated multiple times.
qac-2718Suspicious: Memory deallocated multiple times.
qac-2721Definite: Deallocation of non dynamic memory.
qac-2722Apparent: Deallocation of non dynamic memory.
qac-2723Suspicious: Deallocation of non dynamic memory.
+
Rule-22.3RequiredThe same file shall not be open for read and write access at the same time on different streams
+ + + + + + + + + + + + + +
QacDescription
qac-2691Definite: The same file will be open with write access and another mode.
qac-2692Apparent: The same file will be open with write access and another mode.
qac-2693Suspicious: The same file will be open with write access and another mode.
+
Rule-22.4MandatoryThere shall be no attempt to write to a stream which has been opened as read-only
+ + + + + + + + + + + + + +
QacDescription
qac-2686Definite: Writing to a file opened for reading.
qac-2687Apparent: Writing to a file opened for reading.
qac-2688Suspicious: Writing to a file opened for reading.
+
Rule-22.5MandatoryA pointer to a FILE object shall not be dereferenced
+ + + + + + + + + +
QacDescription
qac-1485A pointer to a FILE object is dereferenced.
qac-1486A pointer to a FILE object is converted to a different type.
+
Rule-22.6MandatoryThe value of a pointer to a FILE shall not be used after the associated stream has been closed
+ + + + + + + + + + + + + +
QacDescription
qac-2696Definite: Attempt to access a file which has been closed.
qac-2697Apparent: Attempt to access a file which has been closed.
qac-2698Suspicious: Attempt to access a file which has been closed.
+
Rule-22.7RequiredThe macro EOF shall on ly be compared with the unmodified return value from any Standard Library function capable of returning EOF
+ + + + + + + + + + + + + +
QacDescription
qac-2671Definite: The value being compared with macro EOF does not originate from an EOF returning function.
qac-2676Definite: The value originating from an EOF returning function was modified before being compared with macro EOF.
qac-2678Suspicious: The value originating from an EOF returning function was modified before being compared with macro EOF.
+
Rule-22.8RequiredThe value of errno shall be set to zero prior to a call to an errno-setting-function
+ + + + + +
QacDescription
qac-2500Call to '%s' is not immediately preceded by the zero-ing of 'errno'.
+
Rule-22.9RequiredThe value of errno shall be tested against zero after calling an errno-setting-function
+ + + + + +
QacDescription
qac-2501Call to '%s' is not immediately followed by the testing of 'errno'.
+
Rule-3.1RequiredThe character sequences /* and // shall not be used within a comment.
+ + + + + +
QacDescription
qac-3108Nested comments are not recognized in the ISO standard.
+
Rule-3.2RequiredLine-splicing shall not be used in // comments.
+ + + + + +
QacDescription
m3cm-5134C++ style comment uses line splicing.
+
Rule-4.1RequiredOctal and hexadecimal escape sequences shall be terminated
+ + + + + + + + + +
QacDescription
qac-3636Octal escape sequence '%s' is not terminated.
qac-3637Hexadecimal escape sequence '%s' is not terminated.
+
Rule-4.2AdvisoryTrigraphs should not be used
+ + + + + +
QacDescription
qac-3601Trigraphs (??x) are an ISO feature.
+
Rule-5.1RequiredExternal identifiers shall be distinct
+ + + + + +
QacDescription
qac-0777[U] External identifier does not differ from other identifier(s) (e.g. '%1s') within the specified number of significant characters.
+
Rule-5.2RequiredIdentifiers declared in the same scope and name space shall be distinct
+ + + + + +
QacDescription
qac-0779[U] Identifier does not differ from other identifier(s) (e.g. '%1s') within the specified number of significant characters.
+
Rule-5.3RequiredAn identifier declared in an inner scope shall not hide an identifier declared in an outer scope
+ + + + + + + + + + + + + +
QacDescription
qac-0795Identifier matches other identifier(s) (e.g. '%1s') in an outer scope within the specified number of significant characters.
qac-2547This declaration of tag '%s' hides a more global declaration.
qac-3334This declaration of '%1s' hides a more global declaration.
+
Rule-5.4RequiredMacro identifiers shall be distinct
+ + + + + + + + + + + + + + + + + +
QacDescription
qac-0788This identifier, '%1s', is used as both a macro name and a function-like macro parameter name.
qac-0791[U] Macro identifier does not differ from other macro identifier(s) (e.g. '%1s') within the specified number of significant characters.
qac-0797This identifier, '%1s', is used as both a macro name and a function-like macro parameter name.
qac-0798This identifier, '%1s', is used as both a macro name and a function-like macro parameter name.
+
Rule-5.5RequiredIdentifiers shall be distinct from macro names
+ + + + + + + + + +
QacDescription
qac-0784Identifier '%1s' is also used as a macro name.
qac-0787Identifier does not differ from other macro name(s) (e.g. '%1s') within the specified number of significant characters.
+
Rule-5.6RequiredA typedef name shall be a unique identifier
+ + + + + + + + + + + + + +
QacDescription
rcma-1506The identifier '%1s' is declared as a typedef and is used elsewhere for a different kind of declaration.
rcma-1507'%1s' is used as a typedef for different types.
rcma-1508The typedef '%1s' is declared in more than one location.
+
Rule-5.7RequiredA tag name shall be a unique identifier
+ + + + + + + + + +
QacDescription
rcma-1750'%1s' has multiple definitions.
qac-2547This declaration of tag '%s' hides a more global declaration.
+
Rule-5.8RequiredIdentifiers that define objects or functions with external linkage shall be unique
+ + + + + + + + + + + + + +
QacDescription
rcma-1525Object/function '%1s' with external linkage has same identifier as another object/function with internal linkage.
rcma-1526Object '%1s' with no linkage has same identifier as another object/function with external linkage.
rcma-1756External identifier '%1s' shall be unique.
+
Rule-5.9AdvisoryIdentifiers that define objects or functions with internal linkage should be unique
+ + + + + + + + + + + + + +
QacDescription
rcma-1525Object/function '%1s' with external linkage has same identifier as another object/function with internal linkage.
rcma-1527Object/function '%1s' with internal linkage has same identifier as another object/function with internal linkage.
rcma-1528Object '%1s' with no linkage has same identifier as another object/function with internal linkage.
+
Rule-6.1RequiredBit-fields shall only be declared with an appropriate type
+ + + + + + + + + +
QacDescription
qac-0634[I] Bit-field %1s in %2s has not been declared explicitly as unsigned or signed.
qac-0635[C99] Bit-field %1s in %2s has been declared with a type not explicitly supported.
+
Rule-6.2RequiredSingle-bit named bit fields shall not be of a signed type
+ + + + + +
QacDescription
qac-3660Named bit-field consisting of a single bit declared with a signed type.
+
Rule-7.1RequiredOctal constants shall not be used
+ + + + + + + + + +
QacDescription
qac-0336Macro defined as an octal constant.
qac-0339Octal constant used.
+
Rule-7.2RequiredA "u" or "U" suffix shall be applied to all integer constants that are represented in an unsigned type
+ + + + + +
QacDescription
qac-1281Integer literal constant is of an unsigned type but does not include a "U" suffix.
+
Rule-7.3RequiredThe lowercase character "l" shall not be used in a literal suffix
+ + + + + +
QacDescription
qac-1280A lowercase letter L (l) has been used in an integer or floating suffix.
+
Rule-7.4RequiredA string literal shall not be assigned to an object unless the object's type is "pointer to const-qualified char"
+ + + + + + + + + + + + + +
QacDescription
qac-0752String literal passed as argument to function whose parameter is not a 'pointer to const'.
qac-0753String literal assigned to pointer which is not a 'pointer to const'.
qac-0754[U] String literal is being modified.
+
Rule-8.1RequiredTypes shall be explicitly specified
+ + + + + + + + + +
QacDescription
qac-2050The 'int' type specifier has been omitted from a function declaration.
qac-2051The 'int' type specifier has been omitted from an object declaration.
+
Rule-8.10RequiredAn inline function shall be declared with the static storage class
+ + + + + + + + + + + + + + + + + +
QacDescription
qac-3240inline function '%1s' is being defined with external linkage.
qac-3241This inline function declaration for '%1s' is implicitly static.
qac-3242This inline function declaration for '%1s' prevents it from being defined with internal linkage.
qac-3243inline function '%1s' is also an 'external definition'.
+
Rule-8.11AdvisoryWhen an array with external linkage is declared, its size should be explicitly specified
+ + + + + +
QacDescription
qac-3684Array declared with unknown size.
+
Rule-8.12RequiredWithin an enumerator list, the value of an implicitly-specified enumeration constant shall be unique
+ + + + + +
QacDescription
qac-0724The value of this implicitly-specified enumeration constant is not unique.
+
Rule-8.13AdvisoryA pointer should point to a const-qualified type whenever possible
+ + + + + + + + + + + + + + + + + +
QacDescription
qac-3673The object addressed by the pointer parameter '%1s' is not modified and so the pointer could be of type 'pointer to const'.
qac-3677The elements of the array parameter '%1s' are not modified and so they could be qualified as 'const'.
qac-3678The object referenced by '%1s' is not modified through it, so '%1s' could be declared with type '%2s'.
qac-3679The object referenced by '%1s' is not modified through it, so '%1s' could be declared with type '%2s'.
+
Rule-8.14RequiredThe restrict type qualifier shall not be used
+ + + + + +
QacDescription
qac-1057[C99] The keyword 'restrict' has been used.
+
Rule-8.2RequiredFunction types shall be in prototype form with named parameters
+ + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-1335Parameter identifiers missing in function prototype declaration.
qac-1336Parameter identifiers missing in declaration of a function type.
qac-3001Function has been declared with an empty parameter list.
qac-3002Defining '%1s()' with an identifier list and separate parameter declarations is an obsolescent feature.
qac-3007"void" has been omitted when defining a function with no parameters.
+
Rule-8.3RequiredAll declarations of an object or function shall use the same names and type qualifiers
+ + + + + + + + + + + + + + + + + +
QacDescription
qac-0606Object '%s' is declared using typedefs which are different to those in a previous declaration.
qac-0624Function '%s' is declared using typedefs which are different to those in a previous declaration.
qac-1330The parameter identifiers in this function declaration differ from those in a previous declaration.
qac-3675Function parameter declared with type qualification which differs from previous declaration.
+
Rule-8.4RequiredA compatible declaration shall be visible when an object or function with external linkage is defined
+ + + + + + + + + +
QacDescription
qac-3331The definition for identifier '%s' with external linkage conflicts with a previous declaration in the same scope.
qac-3408'%1s' has external linkage and is being defined without any previous declaration.
+
Rule-8.5RequiredAn external object or function shall be declared once in one and only one file
+ + + + + + + + + + + + + +
QacDescription
rcma-1513Identifier '%1s' with external linkage has separate non-defining declarations in more than one location.
qac-3449Multiple declarations of external object or function.
qac-3451The global identifier '%s' has been declared in more than one file.
+
Rule-8.6RequiredAn identifier with external linkage shall have exactly one external definition
+ + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-0630[U] More than one definition of '%s' (with external linkage).
rcma-1509'%1s' has external linkage and has multiple definitions.
rcma-1752The object '%1s' with external linkage is declared but not defined within this project.
rcma-1753The function '%1s' with external linkage is declared but not defined within this project.
qac-3406Object/function '%s', with external linkage, has been defined in a header file.
+
Rule-8.7AdvisoryFunctions and objects should not be defined with external linkage if they are referenced in only one translation unit
+ + + + + + + + + + + + + +
QacDescription
rcma-1504The object '%1s' is only referenced in the translation unit where it is defined.
rcma-1505The function '%1s' is only referenced in the translation unit where it is defined.
rcma-1514The object '%1s' is only referenced by function '%2s', in the translation unit where it is defined
+
Rule-8.8RequiredThe static storage class specifier shall be used in all declarations of objects and functions that have internal linkage
+ + + + + +
QacDescription
qac-3224This identifier has previously been declared with internal linkage but is not declared here with the static storage class specifier.
+
Rule-8.9AdvisoryAn object should be defined at block scope if its identifier only appears in a single function
+ + + + + + + + + + + + + +
QacDescription
rcma-1514The object '%1s' is only referenced by function '%2s', in the translation unit where it is defined
rcma-1533The object '%1s' is only referenced by function '%2s'.
qac-3218File scope static, '%1s', is only accessed in one function.
+
Rule-9.1MandatoryThe value of an object with automatic storage duration shall not be read before it has been set
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-2883This 'goto' statement will always bypass the initialization of local variables.
qac-2961Definite: Using value of uninitialized automatic object '%s'.
qac-2962Apparent: Using value of uninitialized automatic object '%s'.
qac-2963Suspicious: Using value of uninitialized automatic object '%s'.
qac-2971Definite: Passing address of uninitialized object '%s' to a function parameter declared as a pointer to const.
qac-2972Apparent: Passing address of uninitialized object '%s' to a function parameter declared as a pointer to const.
qac-2973Suspicious: Passing address of uninitialized object '%s' to a function parameter declared as a pointer to const.
+
Rule-9.2RequiredThe initializer for an aggregate or union shall be enclosed in braces
+ + + + + + + + + + + + + +
QacDescription
qac-0692Union initializer is missing the optional {.
qac-0693Struct initializer is missing the optional {.
qac-0694Array initializer is missing the optional {.
+
Rule-9.3RequiredArrays shall not be partially initialized
+ + + + + + + + + +
QacDescription
qac-0686Array has fewer initializers than its declared size. Default initialization is applied to the remainder of the array elements.
qac-0691Array initialized with designators has fewer initializers than its declared size. Default initialization is applied to the remainder of the array elements.
+
Rule-9.4RequiredAn element of an object shall not be initialized more than once
+ + + + + + + + + + + + + +
QacDescription
qac-1397Array element '%s' has already been initialized.
qac-1398Structure member '%s' has already been initialized.
qac-1399A union member has already been initialized.
+
Rule-9.5RequiredWhere designated initializers are used to initialize an array object the size of the array shall be specified explicitly
+ + + + + +
QacDescription
qac-3676Designators are used to initialize an array of unspecified size.
+
+
+
+ +This section targets to provide an overview of Guidelines Recategorization Plan. +
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
GuidelineDescriptionCategoryRevised Category
Dir-1.1Any implementation-defined behaviour on which the output of the program depends shall be documented and understoodRequiredRequired
Dir-1.1-C99Any implementation-defined behaviour on which the output of the program depends shall be documented and understoodRequiredRequired
Dir-2.1All source files shall compile without any compilation errorsRequiredDisapplied
Dir-3.1All code shall be traceable to documented requirementsRequiredDisapplied
Dir-4.1Run-time failures shall be minimizedRequiredRequired
Dir-4.10Precautions shall be taken in order to prevent the contents of a header file being included more then onceRequiredRequired
Dir-4.11The validity of values passed to library functions shall be checkedRequiredDisapplied
Dir-4.12Dynamic memory allocation shall not be usedRequiredRequired
Dir-4.13Functions which are designed to provide operations on a resource should be called in an appropriate sequenceAdvisoryAdvisory
Dir-4.14The validity of values received from external sources shall be checkedRequiredRequired
Dir-4.2All usage of assembly language should be documentedAdvisoryAdvisory
Dir-4.3Assembly language shall be encapsulated and isolatedRequiredRequired
Dir-4.4Sections of code should not be "commented out"AdvisoryAdvisory
Dir-4.5Identifiers in the same name space with overlapping visibility should be typographically unambiguousAdvisoryAdvisory
Dir-4.6typedefs that indicate size and signedness should be used in place of the basic numerical typesAdvisoryAdvisory
Dir-4.7If a function returns error information, then that error information shall be testedRequiredDisapplied
Dir-4.8If a pointer to a structure or union is never dereferenced within a translation unit, then the implementation of the object should be hiddenAdvisoryDisapplied
Dir-4.9A function should be used in preference to a function-like macro where they are interchangeableAdvisoryDisapplied
Rule-1.1The program shall contain no violations of the standard C syntax and constraints, and shall not exceed the implementation's translation limitsRequiredRequired
Rule-1.1-C99The program shall contain no violations of the standard C syntax and constraints, and shall not exceed the implementation's translation limitsRequiredRequired
Rule-1.2Language extensions should not be usedAdvisoryAdvisory
Rule-1.2-C99Language extensions should not be usedAdvisoryAdvisory
Rule-1.3There shall be no occurrence of undefined or critical unspecified behaviourRequiredRequired
Rule-1.4Emergent language features shall not be usedRequiredRequired
Rule-10.1Operands shall not be of an inappropriate essential type.RequiredRequired
Rule-10.2Expressions of essentially character type shall not be used inappropriately in addition and subtraction operationsRequiredRequired
Rule-10.3The value of an expression shall not be assigned to an object with a narrower essential type or of a different essential type category.RequiredRequired
Rule-10.4Both operands of an operator in which the usual arithmetic conversions are performed shall have the same essential type categoryRequiredRequired
Rule-10.5The value of an expression should not be cast to an inappropriate essential typeAdvisoryAdvisory
Rule-10.6The value of a composite expression shall not be assigned to an object with wider essential typeRequiredRequired
Rule-10.7If a composite expression is used as one operand of an operator in which the usual arithmetic conversions are performed then the other operand shall not have wider essential typeRequiredRequired
Rule-10.8The value of a composite expression shall not be cast to a different essential type category or a wider essential typeRequiredRequired
Rule-11.1Conversions shall not be performed between a pointer to a function and any other typeRequiredRequired
Rule-11.2Conversions shall not be performed between a pointer to an incomplete type and any other typeRequiredRequired
Rule-11.3A cast shall not be performed between a pointer to object type and a pointer to a different object typeRequiredRequired
Rule-11.4A conversion should not be performed between a pointer to object and an integer typeAdvisoryAdvisory
Rule-11.5A conversion should not be performed from pointer to void into pointer to objectAdvisoryAdvisory
Rule-11.6A cast shall not be performed between pointer to void and an arithmetic typeRequiredRequired
Rule-11.7A cast shall not be performed between pointer to object and a non-integer arithmetic typeRequiredRequired
Rule-11.8A cast shall not remove any const or volatile qualification from the type pointed to by a pointerRequiredRequired
Rule-11.9The macro NULL shall be the only permitted form of integer null pointer constantRequiredRequired
Rule-12.1The precedence of operators within expressions should be made explicitAdvisoryAdvisory
Rule-12.2The right hand operand of a shift operator shall lie in the range zero to one less than the width in bits of the essential type of the left hand operandRequiredRequired
Rule-12.3The comma operator should not be usedAdvisoryAdvisory
Rule-12.4Evaluation of constant expressions should not lead to unsigned integer wrap-aroundAdvisoryAdvisory
Rule-12.5The sizeof operator shall not have an operand which is a function parameter declared as 'array of type'MandatoryMandatory
Rule-13.1Initializer lists shall not contain persistent side-effectsRequiredRequired
Rule-13.2The value of an expression and its persistent side-effects shall be the same under all permitted evaluation ordersRequiredRequired
Rule-13.3A full expression containing an increment (++) or decrement (--) operator should have no other potential side effects other than that caused by the increment or decrement operatorAdvisoryDisapplied
Rule-13.4The result of an assignment operator should not be usedAdvisoryAdvisory
Rule-13.5The right hand operand of a logical && or || operator shall not contain persistent side effectsRequiredRequired
Rule-13.6The operand of the sizeof operator shall not contain any expression which has potential side-effectsMandatoryMandatory
Rule-14.1A loop counter shall not have essentially floating typeRequiredRequired
Rule-14.2A for loop shall be well-formedRequiredRequired
Rule-14.3Controlling expressions shall not be invariantRequiredRequired
Rule-14.4The controlling expression of an if-statement and the controlling expression of an iteration-statement shall have essentially Boolean typeRequiredRequired
Rule-15.1The goto statement should not be usedAdvisoryAdvisory
Rule-15.2The goto statement shall jump to a label declared later in the same functionRequiredRequired
Rule-15.3Any label referenced by a goto statement shall be declared in the same block, or in any block enclosing the goto statementRequiredRequired
Rule-15.4There should be no more than one break or goto statement used to terminate any iteration statementAdvisoryAdvisory
Rule-15.5A function should have a single point of exit at the endAdvisoryDisapplied
Rule-15.6The body of an iteration-statement or a selection-statement shall be a compound-statementRequiredRequired
Rule-15.7All if ... else if constructs shall be terminated with an else statementRequiredRequired
Rule-16.1All switch statements shall be well-formedRequiredRequired
Rule-16.2A switch label shall only be used when the most closely-enclosing compound statement is the body of a switch statementRequiredRequired
Rule-16.3An unconditional break statement shall terminate every switch-clauseRequiredRequired
Rule-16.4Every switch statement shall have a default labelRequiredRequired
Rule-16.5A default label shall appear as either the first or the last switch label of a switch statementRequiredRequired
Rule-16.6Every switch statement shall have at least two switch-clausesRequiredRequired
Rule-16.7A switch-expression shall not have essentially Boolean typeRequiredRequired
Rule-17.1The features of shall not be usedRequiredRequired
Rule-17.2Functions shall not call themselves, either directly or indirectlyRequiredRequired
Rule-17.3A function shall not be declared implicitlyMandatoryMandatory
Rule-17.4All exit paths from a function with non-void return type shall have an explicit return statement with an expressionMandatoryMandatory
Rule-17.5The function argument corresponding to a parameter declared to have an array type shall have an appropriate number of elementsAdvisoryAdvisory
Rule-17.6The declaration of an array parameter shall not contain the static keyword between the [ ]MandatoryMandatory
Rule-17.7The value returned by a function having non-void return type shall be usedRequiredDisapplied
Rule-17.8A function parameter should not be modifiedAdvisoryAdvisory
Rule-18.1A pointer resulting from arithmetic on a pointer operand shall address an element of the same array as that pointer operandRequiredRequired
Rule-18.2Subtraction between pointers shall only be applied to pointers that address elements of the same arrayRequiredRequired
Rule-18.3The relational operators >, >=, < and <= shall not be applied to objects of pointer type except where they point into the same objectRequiredRequired
Rule-18.4The +, -, += and -= operators should not be applied to an expression of pointer typeAdvisoryAdvisory
Rule-18.5Declarations should contain no more than two levels of pointer nestingAdvisoryAdvisory
Rule-18.6The address of an object with automatic storage shall not be copied to another object that persists after the first object has ceased to existRequiredRequired
Rule-18.7Flexible array members shall not be declaredRequiredRequired
Rule-18.8Variable-length array types shall not be usedRequiredRequired
Rule-19.1An object shall not be assigned or copied to an overlapping objectMandatoryMandatory
Rule-19.2The union keyword should not be usedAdvisoryAdvisory
Rule-2.1A project shall not contain unreachable codeRequiredRequired
Rule-2.2There shall be no dead codeRequiredRequired
Rule-2.3A project should not contain unused type declarationsAdvisoryDisapplied
Rule-2.4A project should not contain unused tag declarationsAdvisoryAdvisory
Rule-2.5A project should not contain unused macro declarationsAdvisoryDisapplied
Rule-2.6A function should not contain unused label declarationsAdvisoryAdvisory
Rule-2.7There should be no unused parameters in functionsAdvisoryAdvisory
Rule-20.1#include directives should only be preceded by preprocessor directives or commentsAdvisoryAdvisory
Rule-20.10The # and ## preprocessor operators should not be usedAdvisoryAdvisory
Rule-20.11A macro parameter immediately following a # operator shall not immediately be followed by a ## operatorRequiredRequired
Rule-20.12A macro parameter used as an operand to the # or ## operators, which is itself subject to further macro replacement, shall only be used as an operand to these operatorsRequiredRequired
Rule-20.13A line whose first token is # shall be a valid preprocessing directiveRequiredRequired
Rule-20.14All #else, #elif and #endif preprocessor directives shall reside in the same file as the #if, #ifdef or #ifndef directive to which they are relatedRequiredRequired
Rule-20.2The ', " or \ characters and the /* or // character sequences shall not occur in a header file nameRequiredRequired
Rule-20.3The #include directive shall be followed by either a or "filename" sequenceRequiredRequired
Rule-20.4A macro shall not be defined with the same name as a keywordRequiredRequired
Rule-20.4-C99A macro shall not be defined with the same name as a keywordRequiredRequired
Rule-20.5#undef should not be usedAdvisoryAdvisory
Rule-20.6Tokens that look like a preprocessing directive shall not occur within a macro argumentRequiredRequired
Rule-20.7Expressions resulting from the expansion of macro parameters shall be enclosed in parenthesesRequiredRequired
Rule-20.8The controlling expression of a #if or #elif preprocessing directive shall evaluate to 0 or 1RequiredRequired
Rule-20.9All identifiers used in the controlling expression of #if or #elif preprocessing directives shall be #define'd before evaluationRequiredRequired
Rule-21.1#define and #undef shall not be used on a reserved identifier or reserved macro nameRequiredRequired
Rule-21.10The Standard Library time and date functions shall not be usedRequiredRequired
Rule-21.11The standard header file shall not be usedRequiredRequired
Rule-21.12The exception handling features of should not be usedAdvisoryAdvisory
Rule-21.13Any value passed to a function in shall be representable as an unsigned char or be the value EOFMandatoryMandatory
Rule-21.14The Standard Library function memcmp shall not be used to compare null terminated stringsRequiredRequired
Rule-21.15The pointer arguments to the Standard Library functions memcpy, memmove and memcmp shall be pointers to qualified or unqualified versions of compatible typesRequiredRequired
Rule-21.16The pointer arguments to the Standard Library function memcpy shall point to either a pointer type, an essentially signed type, an essentially unsigned type, an essentially Boolean type or an essentially enum typeRequiredRequired
Rule-21.17Use of the string handling functions from shall not result in accesses beyond the bounds of the objects referenced by their pointer parametersMandatoryMandatory
Rule-21.18The size_t argument passed to any function in shall have an appropriate valueMandatoryMandatory
Rule-21.19The pointers returned by the Standard Library functions lovaleconv, getenv, setlocale or strerror shall only be used as if they have pointer to const-qualified typeMandatoryMandatory
Rule-21.2A reserved identifier or macro name shall not be declaredRequiredRequired
Rule-21.20The pointer returned by the Standard Library functions asctime, ctime, gmtime, localtime, localeconv, getenv, setlocale, or strerror shall not be used following a subsequent call to the same functionMandatoryMandatory
Rule-21.21The Standard Library system of shall not be usedRequiredRequired
Rule-21.3The memory allocation and deallocation functions of shall not be usedRequiredRequired
Rule-21.4The standard header file shall not be usedRequiredRequired
Rule-21.5The standard header file shall not be usedRequiredRequired
Rule-21.6The Standard Library input/output functions shall not be usedRequiredRequired
Rule-21.7The atof, atoi, atol and atoll functions of shall not be usedRequiredRequired
Rule-21.8The Standard Library termination functions of shall not be usedRequiredRequired
Rule-21.9The library functions bsearch and qsort of shall not be usedRequiredRequired
Rule-22.1All resources obtained dynamically by means of Standard Library functions shall be explicitly releasedRequiredRequired
Rule-22.10The value of errno shall only be tested when the last function to be called was an errno-setting-functionRequiredRequired
Rule-22.2A block of memory shall only be freed if it was allocated by means of a Standard Library functionMandatoryMandatory
Rule-22.3The same file shall not be open for read and write access at the same time on different streamsRequiredRequired
Rule-22.4There shall be no attempt to write to a stream which has been opened as read-onlyMandatoryMandatory
Rule-22.5A pointer to a FILE object shall not be dereferencedMandatoryMandatory
Rule-22.6The value of a pointer to a FILE shall not be used after the associated stream has been closedMandatoryMandatory
Rule-22.7The macro EOF shall on ly be compared with the unmodified return value from any Standard Library function capable of returning EOFRequiredRequired
Rule-22.8The value of errno shall be set to zero prior to a call to an errno-setting-functionRequiredRequired
Rule-22.9The value of errno shall be tested against zero after calling an errno-setting-functionRequiredRequired
Rule-3.1The character sequences /* and // shall not be used within a comment.RequiredRequired
Rule-3.2Line-splicing shall not be used in // comments.RequiredRequired
Rule-4.1Octal and hexadecimal escape sequences shall be terminatedRequiredRequired
Rule-4.2Trigraphs should not be usedAdvisoryAdvisory
Rule-5.1External identifiers shall be distinctRequiredRequired
Rule-5.2Identifiers declared in the same scope and name space shall be distinctRequiredRequired
Rule-5.3An identifier declared in an inner scope shall not hide an identifier declared in an outer scopeRequiredRequired
Rule-5.4Macro identifiers shall be distinctRequiredRequired
Rule-5.5Identifiers shall be distinct from macro namesRequiredRequired
Rule-5.6A typedef name shall be a unique identifierRequiredRequired
Rule-5.7A tag name shall be a unique identifierRequiredRequired
Rule-5.8Identifiers that define objects or functions with external linkage shall be uniqueRequiredRequired
Rule-5.9Identifiers that define objects or functions with internal linkage should be uniqueAdvisoryAdvisory
Rule-6.1Bit-fields shall only be declared with an appropriate typeRequiredRequired
Rule-6.2Single-bit named bit fields shall not be of a signed typeRequiredRequired
Rule-7.1Octal constants shall not be usedRequiredRequired
Rule-7.2A "u" or "U" suffix shall be applied to all integer constants that are represented in an unsigned typeRequiredRequired
Rule-7.3The lowercase character "l" shall not be used in a literal suffixRequiredRequired
Rule-7.4A string literal shall not be assigned to an object unless the object's type is "pointer to const-qualified char"RequiredRequired
Rule-8.1Types shall be explicitly specifiedRequiredRequired
Rule-8.10An inline function shall be declared with the static storage classRequiredRequired
Rule-8.11When an array with external linkage is declared, its size should be explicitly specifiedAdvisoryAdvisory
Rule-8.12Within an enumerator list, the value of an implicitly-specified enumeration constant shall be uniqueRequiredRequired
Rule-8.13A pointer should point to a const-qualified type whenever possibleAdvisoryAdvisory
Rule-8.14The restrict type qualifier shall not be usedRequiredRequired
Rule-8.2Function types shall be in prototype form with named parametersRequiredRequired
Rule-8.3All declarations of an object or function shall use the same names and type qualifiersRequiredRequired
Rule-8.4A compatible declaration shall be visible when an object or function with external linkage is definedRequiredRequired
Rule-8.5An external object or function shall be declared once in one and only one fileRequiredRequired
Rule-8.6An identifier with external linkage shall have exactly one external definitionRequiredRequired
Rule-8.7Functions and objects should not be defined with external linkage if they are referenced in only one translation unitAdvisoryDisapplied
Rule-8.8The static storage class specifier shall be used in all declarations of objects and functions that have internal linkageRequiredRequired
Rule-8.9An object should be defined at block scope if its identifier only appears in a single functionAdvisoryAdvisory
Rule-9.1The value of an object with automatic storage duration shall not be read before it has been setMandatoryMandatory
Rule-9.2The initializer for an aggregate or union shall be enclosed in bracesRequiredRequired
Rule-9.3Arrays shall not be partially initializedRequiredRequired
Rule-9.4An element of an object shall not be initialized more than onceRequiredRequired
Rule-9.5Where designated initializers are used to initialize an array object the size of the array shall be specified explicitlyRequiredRequired
+
+
+ +This section targets to provide an overview of Guidelines Compliance Summary. +
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
GuidelineCategoryDescriptionCompliance
Dir-1.1RequiredAny implementation-defined behaviour on which the output of the program depends shall be documented and understoodCompliant with deviations:
+
+ + + + + + + + + +
QacDescription
qac-0292[I] Source file '%s' has comments containing one of the characters '$', '@' or '`'.
qac-0315[I] Implicit conversion from a pointer to object type to a pointer to void.
+
Dir-1.1-C99RequiredAny implementation-defined behaviour on which the output of the program depends shall be documented and understoodCompliant with deviations:
+
+ + + + + +
QacDescription
qac-0380[L] Number of macro definitions exceeds 4095 - program does not conform strictly to ISO:C99.
+
Dir-2.1RequiredAll source files shall compile without any compilation errorsDisapplied
Dir-3.1RequiredAll code shall be traceable to documented requirementsDisapplied
Dir-4.1RequiredRun-time failures shall be minimizedCompliant
Dir-4.10RequiredPrecautions shall be taken in order to prevent the contents of a header file being included more then onceCompliant
Dir-4.11RequiredThe validity of values passed to library functions shall be checkedDisapplied
Dir-4.12RequiredDynamic memory allocation shall not be usedCompliant
Dir-4.13AdvisoryFunctions which are designed to provide operations on a resource should be called in an appropriate sequenceCompliant
Dir-4.14RequiredThe validity of values received from external sources shall be checkedCompliant
Dir-4.2AdvisoryAll usage of assembly language should be documentedCompliant
Dir-4.3RequiredAssembly language shall be encapsulated and isolatedCompliant
Dir-4.4AdvisorySections of code should not be "commented out"Compliant
Dir-4.5AdvisoryIdentifiers in the same name space with overlapping visibility should be typographically unambiguousCompliant
Dir-4.6Advisorytypedefs that indicate size and signedness should be used in place of the basic numerical typesCompliant
Dir-4.7RequiredIf a function returns error information, then that error information shall be testedDisapplied
Dir-4.8AdvisoryIf a pointer to a structure or union is never dereferenced within a translation unit, then the implementation of the object should be hiddenDisapplied
Dir-4.9AdvisoryA function should be used in preference to a function-like macro where they are interchangeableDisapplied
Rule-1.1RequiredThe program shall contain no violations of the standard C syntax and constraints, and shall not exceed the implementation's translation limitsCompliant
Rule-1.1-C99RequiredThe program shall contain no violations of the standard C syntax and constraints, and shall not exceed the implementation's translation limitsCompliant
Rule-1.2AdvisoryLanguage extensions should not be usedCompliant
Rule-1.2-C99AdvisoryLanguage extensions should not be usedCompliant
Rule-1.3RequiredThere shall be no occurrence of undefined or critical unspecified behaviourCompliant
Rule-1.4RequiredEmergent language features shall not be usedCompliant
Rule-10.1RequiredOperands shall not be of an inappropriate essential type.Compliant
Rule-10.2RequiredExpressions of essentially character type shall not be used inappropriately in addition and subtraction operationsCompliant
Rule-10.3RequiredThe value of an expression shall not be assigned to an object with a narrower essential type or of a different essential type category.Compliant
Rule-10.4RequiredBoth operands of an operator in which the usual arithmetic conversions are performed shall have the same essential type categoryCompliant
Rule-10.5AdvisoryThe value of an expression should not be cast to an inappropriate essential typeCompliant
Rule-10.6RequiredThe value of a composite expression shall not be assigned to an object with wider essential typeCompliant
Rule-10.7RequiredIf a composite expression is used as one operand of an operator in which the usual arithmetic conversions are performed then the other operand shall not have wider essential typeCompliant
Rule-10.8RequiredThe value of a composite expression shall not be cast to a different essential type category or a wider essential typeCompliant
Rule-11.1RequiredConversions shall not be performed between a pointer to a function and any other typeCompliant
Rule-11.2RequiredConversions shall not be performed between a pointer to an incomplete type and any other typeCompliant
Rule-11.3RequiredA cast shall not be performed between a pointer to object type and a pointer to a different object typeCompliant
Rule-11.4AdvisoryA conversion should not be performed between a pointer to object and an integer typeCompliant with deviations:
+
+ + + + + +
QacDescription
qac-0306[I] Cast between a pointer to object and an integral type.
+
Rule-11.5AdvisoryA conversion should not be performed from pointer to void into pointer to objectCompliant
Rule-11.6RequiredA cast shall not be performed between pointer to void and an arithmetic typeCompliant
Rule-11.7RequiredA cast shall not be performed between pointer to object and a non-integer arithmetic typeCompliant
Rule-11.8RequiredA cast shall not remove any const or volatile qualification from the type pointed to by a pointerCompliant
Rule-11.9RequiredThe macro NULL shall be the only permitted form of integer null pointer constantCompliant
Rule-12.1AdvisoryThe precedence of operators within expressions should be made explicitCompliant
Rule-12.2RequiredThe right hand operand of a shift operator shall lie in the range zero to one less than the width in bits of the essential type of the left hand operandCompliant
Rule-12.3AdvisoryThe comma operator should not be usedCompliant
Rule-12.4AdvisoryEvaluation of constant expressions should not lead to unsigned integer wrap-aroundCompliant
Rule-12.5MandatoryThe sizeof operator shall not have an operand which is a function parameter declared as 'array of type'Compliant
Rule-13.1RequiredInitializer lists shall not contain persistent side-effectsCompliant
Rule-13.2RequiredThe value of an expression and its persistent side-effects shall be the same under all permitted evaluation ordersCompliant
Rule-13.3AdvisoryA full expression containing an increment (++) or decrement (--) operator should have no other potential side effects other than that caused by the increment or decrement operatorDisapplied
Rule-13.4AdvisoryThe result of an assignment operator should not be usedCompliant
Rule-13.5RequiredThe right hand operand of a logical && or || operator shall not contain persistent side effectsCompliant
Rule-13.6MandatoryThe operand of the sizeof operator shall not contain any expression which has potential side-effectsCompliant
Rule-14.1RequiredA loop counter shall not have essentially floating typeCompliant
Rule-14.2RequiredA for loop shall be well-formedCompliant
Rule-14.3RequiredControlling expressions shall not be invariantCompliant with deviations:
+
+ + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-2991The value of this 'if' controlling expression is always 'true'.
qac-2992The value of this 'if' controlling expression is always 'false'.
qac-2998The first operand of this conditional operator is always 'false'.
qac-3493The first operand of this conditional operator is always constant 'true'.
qac-3494The first operand of this conditional operator is always constant 'false'.
+
Rule-14.4RequiredThe controlling expression of an if-statement and the controlling expression of an iteration-statement shall have essentially Boolean typeCompliant
Rule-15.1AdvisoryThe goto statement should not be usedCompliant
Rule-15.2RequiredThe goto statement shall jump to a label declared later in the same functionCompliant
Rule-15.3RequiredAny label referenced by a goto statement shall be declared in the same block, or in any block enclosing the goto statementCompliant
Rule-15.4AdvisoryThere should be no more than one break or goto statement used to terminate any iteration statementCompliant
Rule-15.5AdvisoryA function should have a single point of exit at the endDisapplied
Rule-15.6RequiredThe body of an iteration-statement or a selection-statement shall be a compound-statementCompliant
Rule-15.7RequiredAll if ... else if constructs shall be terminated with an else statementCompliant
Rule-16.1RequiredAll switch statements shall be well-formedCompliant
Rule-16.2RequiredA switch label shall only be used when the most closely-enclosing compound statement is the body of a switch statementCompliant
Rule-16.3RequiredAn unconditional break statement shall terminate every switch-clauseCompliant with deviations:
+
+ + + + + + + + + +
QacDescription
qac-2023The preceding 'switch' clause is not empty and ends with a jump statement other than 'break'.
qac-2024Final 'switch' clause ends with a 'jump' statement other than 'break'.
+
Rule-16.4RequiredEvery switch statement shall have a default labelCompliant
Rule-16.5RequiredA default label shall appear as either the first or the last switch label of a switch statementCompliant
Rule-16.6RequiredEvery switch statement shall have at least two switch-clausesCompliant
Rule-16.7RequiredA switch-expression shall not have essentially Boolean typeCompliant
Rule-17.1RequiredThe features of shall not be usedCompliant
Rule-17.2RequiredFunctions shall not call themselves, either directly or indirectlyCompliant
Rule-17.3MandatoryA function shall not be declared implicitlyCompliant
Rule-17.4MandatoryAll exit paths from a function with non-void return type shall have an explicit return statement with an expressionCompliant
Rule-17.5AdvisoryThe function argument corresponding to a parameter declared to have an array type shall have an appropriate number of elementsCompliant
Rule-17.6MandatoryThe declaration of an array parameter shall not contain the static keyword between the [ ]Compliant
Rule-17.7RequiredThe value returned by a function having non-void return type shall be usedDisapplied
Rule-17.8AdvisoryA function parameter should not be modifiedCompliant
Rule-18.1RequiredA pointer resulting from arithmetic on a pointer operand shall address an element of the same array as that pointer operandCompliant
Rule-18.2RequiredSubtraction between pointers shall only be applied to pointers that address elements of the same arrayCompliant
Rule-18.3RequiredThe relational operators >, >=, < and <= shall not be applied to objects of pointer type except where they point into the same objectCompliant
Rule-18.4AdvisoryThe +, -, += and -= operators should not be applied to an expression of pointer typeCompliant
Rule-18.5AdvisoryDeclarations should contain no more than two levels of pointer nestingCompliant
Rule-18.6RequiredThe address of an object with automatic storage shall not be copied to another object that persists after the first object has ceased to existCompliant
Rule-18.7RequiredFlexible array members shall not be declaredCompliant
Rule-18.8RequiredVariable-length array types shall not be usedCompliant
Rule-19.1MandatoryAn object shall not be assigned or copied to an overlapping objectCompliant
Rule-19.2AdvisoryThe union keyword should not be usedCompliant
Rule-2.1RequiredA project shall not contain unreachable codeCompliant with deviations:
+
+ + + + + +
QacDescription
rcma-1503The function '%1s' is defined but is not used within this project.
+
Rule-2.2RequiredThere shall be no dead codeCompliant with deviations:
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-2982This assignment is redundant. The value of this object is never used before being modified.
qac-2983This assignment is redundant. The value of this object is never subsequently used.
qac-2985This operation is redundant. The value of the result is always that of the left-hand operand.
qac-2986This operation is redundant. The value of the result is always that of the right-hand operand.
qac-2995The result of this logical operation is always 'true'.
qac-2996The result of this logical operation is always 'false'.
qac-3112This statement has no side-effect - it can be removed.
+
Rule-2.3AdvisoryA project should not contain unused type declarationsDisapplied
Rule-2.4AdvisoryA project should not contain unused tag declarationsCompliant
Rule-2.5AdvisoryA project should not contain unused macro declarationsDisapplied
Rule-2.6AdvisoryA function should not contain unused label declarationsCompliant
Rule-2.7AdvisoryThere should be no unused parameters in functionsCompliant
Rule-20.1Advisory#include directives should only be preceded by preprocessor directives or commentsCompliant
Rule-20.10AdvisoryThe # and ## preprocessor operators should not be usedCompliant
Rule-20.11RequiredA macro parameter immediately following a # operator shall not immediately be followed by a ## operatorCompliant
Rule-20.12RequiredA macro parameter used as an operand to the # or ## operators, which is itself subject to further macro replacement, shall only be used as an operand to these operatorsCompliant
Rule-20.13RequiredA line whose first token is # shall be a valid preprocessing directiveCompliant
Rule-20.14RequiredAll #else, #elif and #endif preprocessor directives shall reside in the same file as the #if, #ifdef or #ifndef directive to which they are relatedCompliant
Rule-20.2RequiredThe ', " or \ characters and the /* or // character sequences shall not occur in a header file nameCompliant
Rule-20.3RequiredThe #include directive shall be followed by either a or "filename" sequenceCompliant
Rule-20.4RequiredA macro shall not be defined with the same name as a keywordCompliant
Rule-20.4-C99RequiredA macro shall not be defined with the same name as a keywordCompliant
Rule-20.5Advisory#undef should not be usedCompliant
Rule-20.6RequiredTokens that look like a preprocessing directive shall not occur within a macro argumentCompliant
Rule-20.7RequiredExpressions resulting from the expansion of macro parameters shall be enclosed in parenthesesCompliant
Rule-20.8RequiredThe controlling expression of a #if or #elif preprocessing directive shall evaluate to 0 or 1Compliant
Rule-20.9RequiredAll identifiers used in the controlling expression of #if or #elif preprocessing directives shall be #define'd before evaluationCompliant
Rule-21.1Required#define and #undef shall not be used on a reserved identifier or reserved macro nameCompliant
Rule-21.10RequiredThe Standard Library time and date functions shall not be usedCompliant
Rule-21.11RequiredThe standard header file shall not be usedCompliant
Rule-21.12AdvisoryThe exception handling features of should not be usedCompliant
Rule-21.13MandatoryAny value passed to a function in shall be representable as an unsigned char or be the value EOFCompliant
Rule-21.14RequiredThe Standard Library function memcmp shall not be used to compare null terminated stringsCompliant
Rule-21.15RequiredThe pointer arguments to the Standard Library functions memcpy, memmove and memcmp shall be pointers to qualified or unqualified versions of compatible typesCompliant
Rule-21.16RequiredThe pointer arguments to the Standard Library function memcpy shall point to either a pointer type, an essentially signed type, an essentially unsigned type, an essentially Boolean type or an essentially enum typeCompliant
Rule-21.17MandatoryUse of the string handling functions from shall not result in accesses beyond the bounds of the objects referenced by their pointer parametersCompliant
Rule-21.18MandatoryThe size_t argument passed to any function in shall have an appropriate valueCompliant
Rule-21.19MandatoryThe pointers returned by the Standard Library functions lovaleconv, getenv, setlocale or strerror shall only be used as if they have pointer to const-qualified typeCompliant
Rule-21.2RequiredA reserved identifier or macro name shall not be declaredCompliant
Rule-21.20MandatoryThe pointer returned by the Standard Library functions asctime, ctime, gmtime, localtime, localeconv, getenv, setlocale, or strerror shall not be used following a subsequent call to the same functionCompliant
Rule-21.21RequiredThe Standard Library system of shall not be usedCompliant
Rule-21.3RequiredThe memory allocation and deallocation functions of shall not be usedCompliant
Rule-21.4RequiredThe standard header file shall not be usedCompliant
Rule-21.5RequiredThe standard header file shall not be usedCompliant
Rule-21.6RequiredThe Standard Library input/output functions shall not be usedCompliant
Rule-21.7RequiredThe atof, atoi, atol and atoll functions of shall not be usedCompliant
Rule-21.8RequiredThe Standard Library termination functions of shall not be usedCompliant
Rule-21.9RequiredThe library functions bsearch and qsort of shall not be usedCompliant
Rule-22.1RequiredAll resources obtained dynamically by means of Standard Library functions shall be explicitly releasedCompliant
Rule-22.10RequiredThe value of errno shall only be tested when the last function to be called was an errno-setting-functionCompliant
Rule-22.2MandatoryA block of memory shall only be freed if it was allocated by means of a Standard Library functionCompliant
Rule-22.3RequiredThe same file shall not be open for read and write access at the same time on different streamsCompliant
Rule-22.4MandatoryThere shall be no attempt to write to a stream which has been opened as read-onlyCompliant
Rule-22.5MandatoryA pointer to a FILE object shall not be dereferencedCompliant
Rule-22.6MandatoryThe value of a pointer to a FILE shall not be used after the associated stream has been closedCompliant
Rule-22.7RequiredThe macro EOF shall on ly be compared with the unmodified return value from any Standard Library function capable of returning EOFCompliant
Rule-22.8RequiredThe value of errno shall be set to zero prior to a call to an errno-setting-functionCompliant
Rule-22.9RequiredThe value of errno shall be tested against zero after calling an errno-setting-functionCompliant
Rule-3.1RequiredThe character sequences /* and // shall not be used within a comment.Compliant
Rule-3.2RequiredLine-splicing shall not be used in // comments.Compliant
Rule-4.1RequiredOctal and hexadecimal escape sequences shall be terminatedCompliant
Rule-4.2AdvisoryTrigraphs should not be usedCompliant
Rule-5.1RequiredExternal identifiers shall be distinctCompliant
Rule-5.2RequiredIdentifiers declared in the same scope and name space shall be distinctCompliant
Rule-5.3RequiredAn identifier declared in an inner scope shall not hide an identifier declared in an outer scopeCompliant
Rule-5.4RequiredMacro identifiers shall be distinctCompliant
Rule-5.5RequiredIdentifiers shall be distinct from macro namesCompliant
Rule-5.6RequiredA typedef name shall be a unique identifierCompliant
Rule-5.7RequiredA tag name shall be a unique identifierCompliant
Rule-5.8RequiredIdentifiers that define objects or functions with external linkage shall be uniqueCompliant
Rule-5.9AdvisoryIdentifiers that define objects or functions with internal linkage should be uniqueCompliant
Rule-6.1RequiredBit-fields shall only be declared with an appropriate typeCompliant
Rule-6.2RequiredSingle-bit named bit fields shall not be of a signed typeCompliant
Rule-7.1RequiredOctal constants shall not be usedCompliant
Rule-7.2RequiredA "u" or "U" suffix shall be applied to all integer constants that are represented in an unsigned typeCompliant
Rule-7.3RequiredThe lowercase character "l" shall not be used in a literal suffixCompliant
Rule-7.4RequiredA string literal shall not be assigned to an object unless the object's type is "pointer to const-qualified char"Compliant
Rule-8.1RequiredTypes shall be explicitly specifiedCompliant
Rule-8.10RequiredAn inline function shall be declared with the static storage classCompliant
Rule-8.11AdvisoryWhen an array with external linkage is declared, its size should be explicitly specifiedCompliant
Rule-8.12RequiredWithin an enumerator list, the value of an implicitly-specified enumeration constant shall be uniqueCompliant
Rule-8.13AdvisoryA pointer should point to a const-qualified type whenever possibleCompliant
Rule-8.14RequiredThe restrict type qualifier shall not be usedCompliant
Rule-8.2RequiredFunction types shall be in prototype form with named parametersCompliant
Rule-8.3RequiredAll declarations of an object or function shall use the same names and type qualifiersCompliant
Rule-8.4RequiredA compatible declaration shall be visible when an object or function with external linkage is definedCompliant
Rule-8.5RequiredAn external object or function shall be declared once in one and only one fileCompliant
Rule-8.6RequiredAn identifier with external linkage shall have exactly one external definitionCompliant
Rule-8.7AdvisoryFunctions and objects should not be defined with external linkage if they are referenced in only one translation unitDisapplied
Rule-8.8RequiredThe static storage class specifier shall be used in all declarations of objects and functions that have internal linkageCompliant
Rule-8.9AdvisoryAn object should be defined at block scope if its identifier only appears in a single functionCompliant
Rule-9.1MandatoryThe value of an object with automatic storage duration shall not be read before it has been setCompliant
Rule-9.2RequiredThe initializer for an aggregate or union shall be enclosed in bracesCompliant
Rule-9.3RequiredArrays shall not be partially initializedCompliant
Rule-9.4RequiredAn element of an object shall not be initialized more than onceCompliant
Rule-9.5RequiredWhere designated initializers are used to initialize an array object the size of the array shall be specified explicitlyCompliant
+
+
+ +This section targets to provide an overview of Deviation Permits.
+All the rules corresponding to the deviation permits are disabled inside PRQA and will not cause any violation or deviation in the Deviation records section below. +
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
GuidelineCategoryDescriptionRatioSub RulesCharacteristicsReason
Dir-1.1RequiredAny implementation-defined behaviour on which the output of the program depends shall be documented and understood2/85
+ + + + + + + + + +
QacDescription
qac-0292[I] Source file '%s' has comments containing one of the characters '$', '@' or '`'.
qac-0315[I] Implicit conversion from a pointer to object type to a pointer to void.
+
Maintainability / Analysability +
0292-Invalid characters in comments: Doxygen comments are used. +
0315-Library string.h functions (memcpy, etc.) are used and trigger this implicit conversion. +
+
Dir-1.1-C99RequiredAny implementation-defined behaviour on which the output of the program depends shall be documented and understood1/21
+ + + + + +
QacDescription
qac-0380[L] Number of macro definitions exceeds 4095 - program does not conform strictly to ISO:C99.
+
Maintainability / Analysability +
0380-Already CMSIS and STM32HAL trigger this. +
+
Dir-2.1RequiredAll source files shall compile without any compilation errorsNAUn-Assisted
Dir-3.1RequiredAll code shall be traceable to documented requirementsNAUn-Assisted
Dir-4.11RequiredThe validity of values passed to library functions shall be checkedNAUn-Assisted
Dir-4.7RequiredIf a function returns error information, then that error information shall be tested1/1 (all)
+ + + + + +
QacDescription
qac-2504Return value of '%s' is not checked for error status.
+
Usability / User error protectionSee Rule-17.7
+
2504-This check is also awkward to use - manual specification of all functions using -errorstatusreturningfunction,-erf would be required. +
+
Dir-4.8AdvisoryIf a pointer to a structure or union is never dereferenced within a translation unit, then the implementation of the object should be hidden1/1 (all)
+ + + + + +
QacDescription
qac-3630The implementation of this struct/union type should be hidden.
+
Usability / User error protectionThis rule clashes with our library approach.
+
3630-This rule triggers for declaration in our header files, where we are giving the user the option to deeply inspect received NFC answers using structs. +
+
Dir-4.9AdvisoryA function should be used in preference to a function-like macro where they are interchangeable4/4 (all)
+ + + + + + + + + + + + + + + + + +
QacDescription
qac-3469This usage of a function-like macro looks like it could be replaced by an equivalent function call.
qac-3471Some uses of this function-like macro look like they could be replaced by equivalent function calls.
qac-3472All toplevel uses of this function-like macro look like they could be replaced by equivalent function calls.
qac-3473This usage of a function-like setter macro looks like it could be replaced by a similar function call.
+
Performance / Resource utilizationSuppressed due to code optimization and efficiency.
Rule-11.4AdvisoryA conversion should not be performed between a pointer to object and an integer type1/5
+ + + + + +
QacDescription
qac-0306[I] Cast between a pointer to object and an integral type.
+
Maintainability / ModifiabilityUsing STM32 HAL already creates many violations. Also needed to do pointer arithmetic, calculating offsets inside a buffer.
Rule-13.3AdvisoryA full expression containing an increment (++) or decrement (--) operator should have no other potential side effects other than that caused by the increment or decrement operator2/2 (all)
+ + + + + + + + + +
QacDescription
qac-3387A full expression containing an increment (++) or decrement (--) operator should have no potential side effects other than that caused by the increment or decrement operator.
qac-3440Using the value resulting from a ++ or -- operation.
+
Maintainability / AnalysabilityRFAL uses the increment often for building buffers (array[i++] = 42; ...). Splitting this would decrease readability.
Rule-14.3RequiredControlling expressions shall not be invariant5/11
+ + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-2991The value of this 'if' controlling expression is always 'true'.
qac-2992The value of this 'if' controlling expression is always 'false'.
qac-2998The first operand of this conditional operator is always 'false'.
qac-3493The first operand of this conditional operator is always constant 'true'.
qac-3494The first operand of this conditional operator is always constant 'false'.
+
Portability / AdaptabilityRFAL is configurable through compile time switches. This causes some ifs to have invariant conditions at the used configuration. Suppress 14.3 for if statements.
Rule-15.5AdvisoryA function should have a single point of exit at the end1/1 (all)
+ + + + + +
QacDescription
qac-2889This function has more than one 'return' path.
+
Maintainability / AnalysabilitySuppressed due to readability and simplicity of code logic.
Rule-16.3RequiredAn unconditional break statement shall terminate every switch-clause2/4
+ + + + + + + + + +
QacDescription
qac-2023The preceding 'switch' clause is not empty and ends with a jump statement other than 'break'.
qac-2024Final 'switch' clause ends with a 'jump' statement other than 'break'.
+
Maintainability / AnalysabilitySuppressed due to readability and simplicity of code logic.
Rule-17.7RequiredThe value returned by a function having non-void return type shall be used1/1 (all)
+ + + + + +
QacDescription
qac-3200'%s' returns a value which is not being used.
+
Maintainability / AnalysabilityTreating the return codes of functions in all places without exception handling would makes the code hard to read and maintain. Error checking has been reduced to the places where needed.
Rule-2.1RequiredA project shall not contain unreachable code1/7
+ + + + + +
QacDescription
rcma-1503The function '%1s' is defined but is not used within this project.
+
Maintainability / ModularityRFAL provides many functions - some are not used within the checked project.
Rule-2.2RequiredThere shall be no dead code7/18
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
QacDescription
qac-2982This assignment is redundant. The value of this object is never used before being modified.
qac-2983This assignment is redundant. The value of this object is never subsequently used.
qac-2985This operation is redundant. The value of the result is always that of the left-hand operand.
qac-2986This operation is redundant. The value of the result is always that of the right-hand operand.
qac-2995The result of this logical operation is always 'true'.
qac-2996The result of this logical operation is always 'false'.
qac-3112This statement has no side-effect - it can be removed.
+
Usability / User error protectionAll the violations were checked and fixing the violation would deteriorate robustness: Removing checks which are unnecessary at the given position, removing trailing iterator increment, etc.
Rule-2.3AdvisoryA project should not contain unused type declarations2/2 (all)
+ + + + + + + + + +
QacDescription
qac-3205The identifier '%1s' is not used and could be removed.
rcma-1535The typedef '%1s' is declared but not used within this project.
+
Compatibility / InteroperabilityRFAL defines enums for all identifiers available in NFC Forum - some are unused.
Rule-2.5AdvisoryA project should not contain unused macro declarations1/1 (all)
+ + + + + +
QacDescription
rcma-1534The macro '%1s' is declared but not used within this project.
+
Compatibility / InteroperabilityRFAL defines macros for all identifiers of NFC Forum and RF chip register map - some are not used.
Rule-8.7AdvisoryFunctions and objects should not be defined with external linkage if they are referenced in only one translation unit3/3 (all)
+ + + + + + + + + + + + + +
QacDescription
rcma-1504The object '%1s' is only referenced in the translation unit where it is defined.
rcma-1505The function '%1s' is only referenced in the translation unit where it is defined.
rcma-1514The object '%1s' is only referenced by function '%2s', in the translation unit where it is defined
+
Maintainability / ModularityRFAL defines functions which could be called by the user but are not called in the current project.
+
+
+ +This section targets to provide an overview of Deviation Records. +
+
+
+ +

File: rfal/include/rfal_analogConfig.h

+
+ + + + + + + + + +
LinesCountSuppressed QacsComment
2731
+ + + + +
qac-1060[C99] A flexible array member has been declared.
+
MISRA 18.7 - Flexible Array Members are the only meaningful way of denoting a variable length input buffer which follows a fixed header structure.
+

File: rfal/include/rfal_isoDep.h

+
+ + + + + + + + + +
LinesCountSuppressed QacsComment
2841
+ + + + +
qac-0750A union type specifier has been defined.
+
MISRA 19.2 - Both members of the union will not be used concurrently, device is only of type A or B at a time. Thus no problem can occur.
+

File: rfal/include/rfal_nfc.h

+
+ + + + + + + + + + + + + + + + + + + + + +
LinesCountSuppressed QacsComment
2691
+ + + + +
qac-0750A union type specifier has been defined.
+
MISRA 19.2 - Members of the union will not be used concurrently, only one interface at a time
2121
+ + + + +
qac-0750A union type specifier has been defined.
+
MISRA 19.2 - Members of the union will not be used concurrently, only one protocol at a time
2001
+ + + + +
qac-0750A union type specifier has been defined.
+
MISRA 19.2 - Members of the union will not be used concurrently, only one technology at a time
+

File: rfal/include/rfal_nfcDep.h

+
+ + + + + + + + + +
LinesCountSuppressed QacsComment
3151
+ + + + +
qac-0750A union type specifier has been defined.
+
MISRA 19.2 - Both members of the union will not be used concurrently , device is only initiatior or target a time. No problem can occur.
+

File: rfal/include/rfal_nfcv.h

+
+ + + + + + + + + +
LinesCountSuppressed QacsComment
2211
+ + + + +
qac-0750A union type specifier has been defined.
+
MISRA 19.2 - Both members are of the same type, just different names. Thus no problem can occur.
+

File: rfal/source/rfal_isoDep.c

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
LinesCountSuppressed QacsComment
2185-21861
+ + + + +
qac-4342An expression of 'essentially unsigned' type (%1s) is being cast to enum type '%2s'.
+
MISRA 10.5 - Layout of enum rfalIsoDepFSxI is guaranteed whithin 4bit range
1400-14022
+ + + + +
qac-4342An expression of 'essentially unsigned' type (%1s) is being cast to enum type '%2s'.
+
MISRA 10.5 - Layout of enum rfalBitRate and above masks guarantee no invalid enum values to be created
10271
+ + + + +
qac-2003The preceding 'switch' clause is not empty and does not end with a 'jump' statement. Execution will fall through.
+
MISRA 16.3 - Intentional fall through
7961
+ + + + +
qac-2003The preceding 'switch' clause is not empty and does not end with a 'jump' statement. Execution will fall through.
+
MISRA 16.3 - Intentional fall through
25571
+ + + + +
qac-4342An expression of 'essentially unsigned' type (%1s) is being cast to enum type '%2s'.
+
MISRA 10.5 - Layout of enum rfalBitRate and above clamping of maxTxBR guarantee no invalid enum values to be created
25641
+ + + + +
qac-4342An expression of 'essentially unsigned' type (%1s) is being cast to enum type '%2s'.
+
MISRA 10.5 - Layout of enum rfalBitRate and above clamping of maxTxBR guarantee no invalid enum values to be created
27311
+ + + + +
qac-0310Casting to different object pointer type.
+
MISRA 11.3 - Intentional safe cast to avoiding large buffer duplication
27941
+ + + + +
qac-2003The preceding 'switch' clause is not empty and does not end with a 'jump' statement. Execution will fall through.
+
MISRA 16.3 - Intentional fall through
26531
+ + + + +
qac-4342An expression of 'essentially unsigned' type (%1s) is being cast to enum type '%2s'.
+
MISRA 10.5 - Layout of enum rfalBitRate and range of loop variable guarantee no invalid enum values to be created
2279-22801
+ + + + +
qac-0310Casting to different object pointer type.
+
MISRA 11.3 - Intentional safe cast to avoiding buffer duplication
13601
+ + + + +
qac-2003The preceding 'switch' clause is not empty and does not end with a 'jump' statement. Execution will fall through.
+
MISRA 16.3 - Intentional fall through
4161
+ + + + +
qac-0750A union type specifier has been defined.
+
MISRA 19.2 - Members of the union will not be used concurrently, only one frame at a time
26401
+ + + + +
qac-4342An expression of 'essentially unsigned' type (%1s) is being cast to enum type '%2s'.
+
MISRA 10.5 - Layout of enum rfalBitRate and range of loop variable guarantee no invalid enum values to be created
+

File: rfal/source/rfal_nfc.c

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
LinesCountSuppressed QacsComment
19241
+ + + + +
qac-0310Casting to different object pointer type.
+
MISRA 11.3 - Intentional safe cast to avoiding large buffer duplication
771
+ + + + +
qac-0750A union type specifier has been defined.
+
MISRA 19.2 - Members of the union will not be used concurrently, only one interface at a time
2051
+ + + + +
qac-2880This code is unreachable.
+
MISRA 2.1 - Unreachable code due to configuration option being set/unset
21491
+ + + + +
qac-0310Casting to different object pointer type.
+
MISRA 11.3 - Intentional safe cast to avoiding large buffer duplication
+

File: rfal/source/rfal_nfcDep.c

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
LinesCountSuppressed QacsComment
16811
+ + + + +
qac-2003The preceding 'switch' clause is not empty and does not end with a 'jump' statement. Execution will fall through.
+
MISRA 16.3 - Intentional fall through
26891
+ + + + +
qac-2003The preceding 'switch' clause is not empty and does not end with a 'jump' statement. Execution will fall through.
+
MISRA 16.3 - Intentional fall through
1930-19322
+ + + + +
qac-4342An expression of 'essentially unsigned' type (%1s) is being cast to enum type '%2s'.
+
MISRA 10.5 - Layout of enum rfalBitRate and definition of rfalNfcDepBRS2DSI guarantee no invalid enum values to be created
16091
+ + + + +
qac-2003The preceding 'switch' clause is not empty and does not end with a 'jump' statement. Execution will fall through.
+
MISRA 16.3 - Intentional fall through
26301
+ + + + +
qac-0310Casting to different object pointer type.
+
MISRA 11.3 - Intentional safe cast to avoiding large buffer duplication
+

File: rfal/source/rfal_nfca.c

+
+ + + + + + + + + + + + + + + + + + + + + +
LinesCountSuppressed QacsComment
7391
+ + + + +
qac-2003The preceding 'switch' clause is not empty and does not end with a 'jump' statement. Execution will fall through.
+
MISRA 16.3 - Intentional fall through
746-7471
+ + + + +
qac-4342An expression of 'essentially unsigned' type (%1s) is being cast to enum type '%2s'.
+
MISRA 10.5 - Guaranteed that no invalid enum values are created: see guard_eq_RFAL_NFCA_T2T, ....
3141
+ + + + +
qac-2003The preceding 'switch' clause is not empty and does not end with a 'jump' statement. Execution will fall through.
+
MISRA 16.3 - Intentional fall through
+

File: rfal/source/rfal_nfcb.c

+
+ + + + + + + + + +
LinesCountSuppressed QacsComment
545-5461
+ + + + +
qac-4342An expression of 'essentially unsigned' type (%1s) is being cast to enum type '%2s'.
+
MISRA 10.5 - Layout of rfalNfcbSlots and the limited loop guarantee that no invalid enum values are created.
+

File: rfal/source/rfal_t4t.c

+
+ + + + + + + + + + + + + + + +
LinesCountSuppressed QacsComment
1361
+ + + + +
qac-2880This code is unreachable.
+
MISRA 2.1 - Unreachable code due to configuration option being set/unset
1271
+ + + + +
qac-2880This code is unreachable.
+
MISRA 2.1 - Unreachable code due to configuration option being set/unset
+

File: rfal/source/st25r3916/rfal_analogConfigTbl.h

+
+ + + + + + + + + + + + + + +
LinesCountSuppressed QacsComment
405-4061
+ + + + +
qac-3406Object/function '%s', with external linkage, has been defined in a header file.
+
MISRA 8.6 - Externally generated table included by the library
1
+ + + + +
rcma-1514The object '%1s' is only referenced by function '%2s', in the translation unit where it is defined
+
MISRA 8.6 - Externally generated table included by the library
+

File: rfal/source/st25r3916/rfal_dpoTbl.h

+
+ + + + + + + + + + + + + + +
LinesCountSuppressed QacsComment
54-551
+ + + + +
qac-3406Object/function '%s', with external linkage, has been defined in a header file.
+
MISRA 8.6 - Externally generated table included by the library
1
+ + + + +
rcma-1514The object '%1s' is only referenced by function '%2s', in the translation unit where it is defined
+
MISRA 8.6 - Externally generated table included by the library
+

File: rfal/source/st25r3916/rfal_rfst25r3916.c

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
LinesCountSuppressed QacsComment
16391
+ + + + +
m3cm-5209Use of basic type '%s'.
+
MISRA 4.9 - External function (sqrt()) requires double
20591
+ + + + +
qac-2003The preceding 'switch' clause is not empty and does not end with a 'jump' statement. Execution will fall through.
+
MISRA 16.3 - Intentional fall through
36371
+ + + + +
qac-2003The preceding 'switch' clause is not empty and does not end with a 'jump' statement. Execution will fall through.
+
MISRA 16.3 - Intentional fall through
20281
+ + + + +
qac-2003The preceding 'switch' clause is not empty and does not end with a 'jump' statement. Execution will fall through.
+
MISRA 16.3 - Intentional fall through
38901
+ + + + +
qac-4342An expression of 'essentially unsigned' type (%1s) is being cast to enum type '%2s'.
+
MISRA 10.5 - Guaranteed that no invalid enum values may be created. See also equalityGuard_RFAL_BR_106 ff.
2801
+ + + + +
qac-0750A union type specifier has been defined.
+
MISRA 19.2 - Both members are of the same type, just different names. Thus no problem can occur.
24021
+ + + + +
qac-2003The preceding 'switch' clause is not empty and does not end with a 'jump' statement. Execution will fall through.
+
MISRA 16.3 - Intentional fall through
20431
+ + + + +
qac-2003The preceding 'switch' clause is not empty and does not end with a 'jump' statement. Execution will fall through.
+
MISRA 16.3 - Intentional fall through
36571
+ + + + +
qac-4342An expression of 'essentially unsigned' type (%1s) is being cast to enum type '%2s'.
+
MISRA 10.5 - Guaranteed that no invalid enum values may be created. See also equalityGuard_RFAL_BR_106 ff.
24771
+ + + + +
qac-2003The preceding 'switch' clause is not empty and does not end with a 'jump' statement. Execution will fall through.
+
MISRA 16.3 - Intentional fall through
21311
+ + + + +
qac-2003The preceding 'switch' clause is not empty and does not end with a 'jump' statement. Execution will fall through.
+
MISRA 16.3 - Intentional fall through
26721
+ + + + +
qac-2003The preceding 'switch' clause is not empty and does not end with a 'jump' statement. Execution will fall through.
+
MISRA 16.3 - Intentional fall through
33871
+ + + + +
qac-0759An object of union type has been defined.
+
MISRA 19.2 - Allocating Union where members are of the same type, just different names. Thus no problem can occur.
21831
+ + + + +
qac-2003The preceding 'switch' clause is not empty and does not end with a 'jump' statement. Execution will fall through.
+
MISRA 16.3 - Intentional fall through
22611
+ + + + +
qac-2003The preceding 'switch' clause is not empty and does not end with a 'jump' statement. Execution will fall through.
+
MISRA 16.3 - Intentional fall through
25651
+ + + + +
qac-2003The preceding 'switch' clause is not empty and does not end with a 'jump' statement. Execution will fall through.
+
MISRA 16.3 - Intentional fall through
10551
+ + + + +
qac-2880This code is unreachable.
+
MISRA 2.1 - Inconsistently marked as unreachable code
+
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FileRequiredAdvisoryTotal
rfal/source/st25r3916/rfal_rfst25r3916.c12517
rfal/source/st25r3916/rfal_dpoTbl.h112
rfal/source/st25r3916/rfal_analogConfigTbl.h112
rfal/source/rfal_t4t.c202
rfal/source/rfal_nfcDep.c426
rfal/source/rfal_nfcb.c011
rfal/source/rfal_nfca.c213
rfal/source/rfal_isoDep.c6814
rfal/source/rfal_nfc.c314
rfal/include/rfal_nfcv.h011
rfal/include/rfal_isoDep.h011
rfal/include/rfal_analogConfig.h101
rfal/include/rfal_nfc.h033
rfal/include/rfal_nfcDep.h011
Total322658
+
+
+ + +There are no duplicated suppressions. + +

File: common/firmware/STM/utils/Inc/utils.h

+
+ + + + + + + + + + + + +
LineUnused QacsComment
81
+ + + + +
qac-0431[C] Function argument points to a more heavily qualified type.
+
MISRA 1.1 - string.h from Cosmic only provides functions with low qualified parameters
79
+ + + + +
qac-0431[C] Function argument points to a more heavily qualified type.
+
MISRA 1.1 - string.h from Cosmic only provides functions with low qualified parameters
+

File: rfal/source/rfal_iso15693_2.c

+
+ + + + + + + +
LineUnused QacsComment
524
+ + + + +
qac-2911Definite: Wraparound in unsigned arithmetic operation.
+
CERT INT30 - Intentional underflow, part of the coding
+

File: rfal/source/rfal_nfc.c

+
+ + + + + + + +
LineUnused QacsComment
219
+ + + + +
qac-2880This code is unreachable.
+
MISRA 2.1 - Unreachable code due to configuration option being set/unset
+

File: rfal/source/st25r3916/rfal_analogConfigTbl.h

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
LineUnused QacsComment
717
+ + + + +
qac-3406Object/function '%s', with external linkage, has been defined in a header file.
+
MISRA 8.9 - Externally generated table included by the library
404
+ + + + +
qac-3674Array size defined implicitly by the number of initializers.
+
CERT ARR02 - Flexible array will be used with sizeof, on adding elements error-prone manual update of size would be required
716
+ + + + +
qac-3674Array size defined implicitly by the number of initializers.
+
CERT ARR02 - Flexible array will be used with sizeof, on adding elements error-prone manual update of size would be required
405
+
+
MISRA 8.9 - Externally generated table included by the library
717
+
+
MISRA 8.6 - Externally generated table included by the library
+

File: rfal/source/st25r3916/rfal_dpoTbl.h

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
LineUnused QacsComment
53
+ + + + +
qac-3674Array size defined implicitly by the number of initializers.
+
CERT ARR02 - Flexible array will be used with sizeof, on adding elements error-prone manual update of size would be required
66
+
+
MISRA 8.9 - Externally generated table included by the library
66
+ + + + +
qac-3406Object/function '%s', with external linkage, has been defined in a header file.
+
MISRA 8.6 - Externally generated table included by the library
54
+
+
MISRA 8.9 - Externally generated table included by the library
65
+ + + + +
qac-3674Array size defined implicitly by the number of initializers.
+
CERT ARR02 - Flexible array will be used with sizeof, on adding elements error-prone manual update of size would be required
+
+
+ +There are no continuous suppressions by file. +
+
+ +Active Diagnostics refers to diagnostics that are not suppressed (note: no suppressed diagnostics have been taken into account for the calculation of information in this document). +
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FilesActive DiagnosticsViolated RulesViolation CountCompliance Index
rfal/include/rfal_analogConfig.h000100.00
rfal/include/rfal_cd.h000100.00
rfal/include/rfal_chip.h000100.00
rfal/include/rfal_dpo.h000100.00
rfal/include/rfal_isoDep.h000100.00
rfal/include/rfal_nfc.h000100.00
rfal/include/rfal_nfcDep.h000100.00
rfal/include/rfal_nfca.h000100.00
rfal/include/rfal_nfcb.h000100.00
rfal/include/rfal_nfcf.h000100.00
rfal/include/rfal_nfcv.h000100.00
rfal/include/rfal_rf.h000100.00
rfal/include/rfal_st25tb.h000100.00
rfal/include/rfal_st25xv.h000100.00
rfal/include/rfal_t1t.h000100.00
rfal/include/rfal_t2t.h000100.00
rfal/include/rfal_t4t.h000100.00
rfal/include/rfal_utils.h000100.00
rfal/source/rfal_analogConfig.c000100.00
rfal/source/rfal_cd.c000100.00
rfal/source/rfal_cdHb.c000100.00
rfal/source/rfal_crc.c000100.00
rfal/source/rfal_crc.h000100.00
rfal/source/rfal_dpo.c000100.00
rfal/source/rfal_iso15693_2.c000100.00
rfal/source/rfal_iso15693_2.h000100.00
rfal/source/rfal_isoDep.c000100.00
rfal/source/rfal_nfc.c000100.00
rfal/source/rfal_nfcDep.c000100.00
rfal/source/rfal_nfca.c000100.00
rfal/source/rfal_nfcb.c000100.00
rfal/source/rfal_nfcf.c000100.00
rfal/source/rfal_nfcv.c000100.00
rfal/source/rfal_st25tb.c000100.00
rfal/source/rfal_st25xv.c000100.00
rfal/source/rfal_t1t.c000100.00
rfal/source/rfal_t2t.c000100.00
rfal/source/rfal_t4t.c000100.00
rfal/source/st25r3916/rfal_analogConfigTbl.h000100.00
rfal/source/st25r3916/rfal_dpoTbl.h000100.00
rfal/source/st25r3916/rfal_features.h000100.00
rfal/source/st25r3916/rfal_rfst25r3916.c000100.00
rfal/source/st25r3916/st25r3916.c000100.00
rfal/source/st25r3916/st25r3916.h000100.00
rfal/source/st25r3916/st25r3916_aat.c000100.00
rfal/source/st25r3916/st25r3916_aat.h000100.00
rfal/source/st25r3916/st25r3916_com.c000100.00
rfal/source/st25r3916/st25r3916_com.h000100.00
rfal/source/st25r3916/st25r3916_irq.c000100.00
rfal/source/st25r3916/st25r3916_irq.h000100.00
rfal/source/st25r3916/st25r3916_led.c000100.00
rfal/source/st25r3916/st25r3916_led.h000100.00
Total000100.00
+ +

+Nota: Calculation of Compliance Index
+The Compliance Index is the percentage of groups which have no messages in them.
+For each file it is calculated as follows:
+
+( Ntotal - Nerror ) / Ntotal x 100
+
+Ntotal is the total number of enforced rules (i.e. the number of rules that have at least one message mapped to it directly).
+Nerror is the number of rules for which messages appear in that file.
+The File Compliance Index is the mean of all the individual file compliances.
+ +
+
+
+
+ + diff --git a/core/embed/io/nfc/rfal/doc/_htmresc/st_logo.png b/core/embed/io/nfc/rfal/doc/_htmresc/st_logo.png new file mode 100644 index 0000000000..b6180c7074 Binary files /dev/null and b/core/embed/io/nfc/rfal/doc/_htmresc/st_logo.png differ diff --git a/core/embed/io/nfc/rfal/doc/rfal.chm b/core/embed/io/nfc/rfal/doc/rfal.chm new file mode 100644 index 0000000000..b55da094ad Binary files /dev/null and b/core/embed/io/nfc/rfal/doc/rfal.chm differ diff --git a/core/embed/io/nfc/rfal/include/rfal_analogConfig.h b/core/embed/io/nfc/rfal/include/rfal_analogConfig.h new file mode 100644 index 0000000000..dfb29797b0 --- /dev/null +++ b/core/embed/io/nfc/rfal/include/rfal_analogConfig.h @@ -0,0 +1,560 @@ + +/****************************************************************************** + * @attention + * + * COPYRIGHT 2020 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/* + * PROJECT: ST25R391x firmware + * Revision: + * LANGUAGE: ISO C99 + */ + +/*! \file rfal_AnalogConfig.h + * + * \author bkam + * + * \brief RF Chip Analog Configuration Settings + * + * + * \addtogroup RFAL + * @{ + * + * \addtogroup RFAL-HAL + * \brief RFAL Hardware Abstraction Layer + * @{ + * + * \addtogroup AnalogConfig + * \brief RFAL Analog Config Module + * @{ + * + */ + +#ifndef RFAL_ANALOG_CONFIG_H +#define RFAL_ANALOG_CONFIG_H + +/* + ****************************************************************************** + * INCLUDES + ****************************************************************************** + */ +#include "rfal_platform.h" +#include "rfal_rf.h" +#include "rfal_utils.h" + +/* + ****************************************************************************** + * DEFINES + ****************************************************************************** + */ + +#define RFAL_ANALOG_CONFIG_LUT_SIZE \ + (87U) /*!< Maximum number of Configuration IDs in the Loop Up Table */ +#define RFAL_ANALOG_CONFIG_LUT_NOT_FOUND \ + (0xFFU) /*!< Index value indicating no Configuration IDs found */ + +#define RFAL_ANALOG_CONFIG_TBL_SIZE \ + (1024U) /*!< Maximum number of Register-Mask-Value in the Setting List */ + +/* + ****************************************************************************** + * The Analog Configuration is structured as following + * +---------+-----------------------+-----------------------------+ + * | ModeID | Num RVM configuration | RVM (Register, Value, Mask) | + * | (16bit) | (8bit) | (24bit) | + * +---------+-----------------------+-----------------------------+ + * + * The Mode ID coding for different use cases is described below + * + * 1. ModeID coding for NFC technologies (not chip specific) + * +----------------------------------------------------------------------+ + * | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | + * +----------------------------------------------------------------------+ + * | P/L | TECH != CHIP | BR | DIR | + * +----------------------------------------------------------------------+ + * + * 2. ModeID coding for chip specific modes and events + * +----------------------------------------------------------------------+ + * | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | + * +----------------------------------------------------------------------+ + * | P/L | TECH == CHIP | CHIP_SPECIFIC | + * +----------------------------------------------------------------------+ + * + * 3. Special ModeID coding for Direction == DPO + * +----------------------------------------------------------------------+ + * | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | + * +----------------------------------------------------------------------+ + * | P/L | DPO_LVL | TECH* | BR | DIR == DPO | + * +----------------------------------------------------------------------+ + * ^ + * | + * +----- reuse of TECH_RFU bits as DPO level indicator + ****************************************************************************** + */ + +/* Mask bit */ +#define RFAL_ANALOG_CONFIG_POLL_LISTEN_MODE_MASK \ + (0x8000U) /*!< Mask bit of Poll Mode in Analog Configuration ID */ +#define RFAL_ANALOG_CONFIG_TECH_MASK \ + (0x7F00U) /*!< Mask bits for Technology in Analog Configuration ID */ +#define RFAL_ANALOG_CONFIG_BITRATE_MASK \ + (0x00F0U) /*!< Mask bits for Bit rate in Analog Configuration ID */ +#define RFAL_ANALOG_CONFIG_DIRECTION_MASK \ + (0x000FU) /*!< Mask bits for Direction in Analog Configuration ID */ +#define RFAL_ANALOG_CONFIG_CHIP_SPECIFIC_MASK \ + (0x00FFU) /*!< Mask bits for Chip Specific Technology */ + +/* Shift values */ +#define RFAL_ANALOG_CONFIG_POLL_LISTEN_MODE_SHIFT \ + (15U) /*!< Shift value of Poll Mode in Analog Configuration ID */ +#define RFAL_ANALOG_CONFIG_TECH_SHIFT \ + (8U) /*!< Shift value for Technology in Analog Configuration ID */ +#define RFAL_ANALOG_CONFIG_BITRATE_SHIFT \ + (4U) /*!< Shift value for Technology in Analog Configuration ID */ +#define RFAL_ANALOG_CONFIG_DIRECTION_SHIFT \ + (0U) /*!< Shift value for Direction in Analog Configuration ID */ + +/* P/L: bit 15 */ +#define RFAL_ANALOG_CONFIG_POLL \ + (0x0000U) /*!< Poll Mode bit setting in Analog Configuration ID */ +#define RFAL_ANALOG_CONFIG_LISTEN \ + (0x8000U) /*!< Listen Mode bit setting in Analog Configuration ID */ + +/* TECH: bit 14-8 */ +#define RFAL_ANALOG_CONFIG_TECH_CHIP \ + (0x0000U) /*!< Chip-Specific bit setting in Analog Configuration ID */ +#define RFAL_ANALOG_CONFIG_TECH_NFCA \ + (0x0100U) /*!< NFC-A Technology bits setting in Analog Configuration ID */ +#define RFAL_ANALOG_CONFIG_TECH_NFCB \ + (0x0200U) /*!< NFC-B Technology bits setting in Analog Configuration ID */ +#define RFAL_ANALOG_CONFIG_TECH_NFCF \ + (0x0400U) /*!< NFC-F Technology bits setting in Analog Configuration ID */ +#define RFAL_ANALOG_CONFIG_TECH_AP2P \ + (0x0800U) /*!< AP2P Technology bits setting in Analog Configuration ID */ +#define RFAL_ANALOG_CONFIG_TECH_NFCV \ + (0x1000U) /*!< NFC-V Technology bits setting in Analog Configuration ID */ +#define RFAL_ANALOG_CONFIG_TECH_RFU (0x2000U) /*!< RFU for Technology bits */ +#define RFAL_ANALOG_CONFIG_TECH_RFU2 (0x4000U) /*!< RFU for Technology bits */ + +/* BR: bit 7-4 */ +#define RFAL_ANALOG_CONFIG_BITRATE_COMMON \ + (0x0000U) /*!< Common settings for all bit rates in Analog Configuration ID \ + */ +#define RFAL_ANALOG_CONFIG_BITRATE_106 \ + (0x0010U) /*!< 106kbits/s settings in Analog Configuration ID */ +#define RFAL_ANALOG_CONFIG_BITRATE_212 \ + (0x0020U) /*!< 212kbits/s settings in Analog Configuration ID */ +#define RFAL_ANALOG_CONFIG_BITRATE_424 \ + (0x0030U) /*!< 424kbits/s settings in Analog Configuration ID */ +#define RFAL_ANALOG_CONFIG_BITRATE_848 \ + (0x0040U) /*!< 848kbits/s settings in Analog Configuration ID */ +#define RFAL_ANALOG_CONFIG_BITRATE_1695 \ + (0x0050U) /*!< 1695kbits/s settings in Analog Configuration ID */ +#define RFAL_ANALOG_CONFIG_BITRATE_3390 \ + (0x0060U) /*!< 3390kbits/s settings in Analog Configuration ID */ +#define RFAL_ANALOG_CONFIG_BITRATE_6780 \ + (0x0070U) /*!< 6780kbits/s settings in Analog Configuration ID */ +#define RFAL_ANALOG_CONFIG_BITRATE_211p88 \ + (0x0090U) /*!< 211.88kbits/s (ISO15693 x8) in Analog Configuration ID */ +#define RFAL_ANALOG_CONFIG_BITRATE_105p94 \ + (0x00A0U) /*!< 105.94kbits/s (ISO15693 x4) in Analog Configuration ID */ +#define RFAL_ANALOG_CONFIG_BITRATE_53 \ + (0x00B0U) /*!< 53kbits/s (ISO15693 x2) setting in Analog Configuration ID */ +#define RFAL_ANALOG_CONFIG_BITRATE_26 \ + (0x00C0U) /*!< 26kbit/s (1 out of 4) NFC-V setting Analog Configuration ID \ + */ +#define RFAL_ANALOG_CONFIG_BITRATE_1p6 \ + (0x00D0U) /*!< 1.6kbit/s (1 out of 256) NFC-V setting Analog Config ID */ +#define RFAL_ANALOG_CONFIG_BITRATE_RFU (0x00E0U) /*!< RFU for Bitrate bits */ +#define RFAL_ANALOG_CONFIG_BITRATE_RFU2 (0x00F0U) /*!< RFU for Bitrate bits */ + +/* DIR: bit 3-0 */ +#define RFAL_ANALOG_CONFIG_NO_DIRECTION \ + (0x0000U) /*!< No direction setting in Analog Conf ID (Chip Specific only) \ + */ +#define RFAL_ANALOG_CONFIG_TX \ + (0x0001U) /*!< Transmission bit setting in Analog Configuration ID */ +#define RFAL_ANALOG_CONFIG_RX \ + (0x0002U) /*!< Reception bit setting in Analog Configuration ID */ +#define RFAL_ANALOG_CONFIG_ANTICOL \ + (0x0003U) /*!< Anticollision setting in Analog Configuration ID */ +#define RFAL_ANALOG_CONFIG_DPO \ + (0x0004U) /*!< DPO setting in Analog Configuration ID */ +#define RFAL_ANALOG_CONFIG_DLMA \ + (0x0005U) /*!< DLMA setting in Analog Configuration ID */ +#define RFAL_ANALOG_CONFIG_DIRECTION_RFU2 \ + (0x0006U) /*!< RFU for Direction bits */ +#define RFAL_ANALOG_CONFIG_DIRECTION_RFU3 \ + (0x0007U) /*!< RFU for Direction bits */ +#define RFAL_ANALOG_CONFIG_DIRECTION_RFU4 \ + (0x0008U) /*!< RFU for Direction bits */ +#define RFAL_ANALOG_CONFIG_DIRECTION_RFU5 \ + (0x0009U) /*!< RFU for Direction bits */ +#define RFAL_ANALOG_CONFIG_DIRECTION_RFU6 \ + (0x000AU) /*!< RFU for Direction bits */ +#define RFAL_ANALOG_CONFIG_DIRECTION_RFU7 \ + (0x000BU) /*!< RFU for Direction bits */ +#define RFAL_ANALOG_CONFIG_DIRECTION_RFU8 \ + (0x000CU) /*!< RFU for Direction bits */ +#define RFAL_ANALOG_CONFIG_DIRECTION_RFU9 \ + (0x000DU) /*!< RFU for Direction bits */ +#define RFAL_ANALOG_CONFIG_DIRECTION_RFU10 \ + (0x000EU) /*!< RFU for Direction bits */ +#define RFAL_ANALOG_CONFIG_DIRECTION_RFU11 \ + (0x000FU) /*!< RFU for Direction bits */ + +/* bit 7-0 */ +#define RFAL_ANALOG_CONFIG_CHIP_INIT \ + (0x0000U) /*!< Chip-Specific event: Startup;Reset;Initialize */ +#define RFAL_ANALOG_CONFIG_CHIP_DEINIT \ + (0x0001U) /*!< Chip-Specific event: Deinitialize */ +#define RFAL_ANALOG_CONFIG_CHIP_FIELD_ON \ + (0x0002U) /*!< Chip-Specific event: Field On */ +#define RFAL_ANALOG_CONFIG_CHIP_FIELD_OFF \ + (0x0003U) /*!< Chip-Specific event: Field Off */ +#define RFAL_ANALOG_CONFIG_CHIP_WAKEUP_ON \ + (0x0004U) /*!< Chip-Specific event: Wake-up On */ +#define RFAL_ANALOG_CONFIG_CHIP_WAKEUP_OFF \ + (0x0005U) /*!< Chip-Specific event: Wake-up Off */ +#define RFAL_ANALOG_CONFIG_CHIP_LISTEN_ON \ + (0x0006U) /*!< Chip-Specific event: Listen On */ +#define RFAL_ANALOG_CONFIG_CHIP_LISTEN_OFF \ + (0x0007U) /*!< Chip-Specific event: Listen Off */ +#define RFAL_ANALOG_CONFIG_CHIP_POLL_COMMON \ + (0x0008U) /*!< Chip-Specific event: Poll common */ +#define RFAL_ANALOG_CONFIG_CHIP_LISTEN_COMMON \ + (0x0009U) /*!< Chip-Specific event: Listen common */ +#define RFAL_ANALOG_CONFIG_CHIP_LOWPOWER_ON \ + (0x000AU) /*!< Chip-Specific event: Low Power On */ +#define RFAL_ANALOG_CONFIG_CHIP_LOWPOWER_OFF \ + (0x000BU) /*!< Chip-Specific event: Low Power Off */ + +#define RFAL_ANALOG_CONFIG_CHIP_POWER_LVL_00 \ + (0x0010U) /*!< Chip-Specific event: Power Level 00 (e.g DPO, WLC) */ +#define RFAL_ANALOG_CONFIG_CHIP_POWER_LVL_01 \ + (0x0011U) /*!< Chip-Specific event: Power Level 01 (e.g DPO, WLC) */ +#define RFAL_ANALOG_CONFIG_CHIP_POWER_LVL_02 \ + (0x0012U) /*!< Chip-Specific event: Power Level 02 (e.g DPO, WLC) */ +#define RFAL_ANALOG_CONFIG_CHIP_POWER_LVL_03 \ + (0x0013U) /*!< Chip-Specific event: Power Level 03 (e.g DPO, WLC) */ +#define RFAL_ANALOG_CONFIG_CHIP_POWER_LVL_04 \ + (0x0014U) /*!< Chip-Specific event: Power Level 04 (e.g DPO, WLC) */ +#define RFAL_ANALOG_CONFIG_CHIP_POWER_LVL_05 \ + (0x0015U) /*!< Chip-Specific event: Power Level 05 (e.g DPO, WLC) */ +#define RFAL_ANALOG_CONFIG_CHIP_POWER_LVL_06 \ + (0x0016U) /*!< Chip-Specific event: Power Level 06 (e.g DPO, WLC) */ +#define RFAL_ANALOG_CONFIG_CHIP_POWER_LVL_07 \ + (0x0017U) /*!< Chip-Specific event: Power Level 07 (e.g DPO, WLC) */ +#define RFAL_ANALOG_CONFIG_CHIP_POWER_LVL_08 \ + (0x0018U) /*!< Chip-Specific event: Power Level 08 (e.g DPO, WLC) */ +#define RFAL_ANALOG_CONFIG_CHIP_POWER_LVL_09 \ + (0x0019U) /*!< Chip-Specific event: Power Level 09 (e.g DPO, WLC) */ +#define RFAL_ANALOG_CONFIG_CHIP_POWER_LVL_10 \ + (0x001AU) /*!< Chip-Specific event: Power Level 10 (e.g DPO, WLC) */ +#define RFAL_ANALOG_CONFIG_CHIP_POWER_LVL_11 \ + (0x001BU) /*!< Chip-Specific event: Power Level 11 (e.g DPO, WLC) */ +#define RFAL_ANALOG_CONFIG_CHIP_POWER_LVL_12 \ + (0x001CU) /*!< Chip-Specific event: Power Level 12 (e.g DPO, WLC) */ +#define RFAL_ANALOG_CONFIG_CHIP_POWER_LVL_13 \ + (0x001DU) /*!< Chip-Specific event: Power Level 13 (e.g DPO, WLC) */ +#define RFAL_ANALOG_CONFIG_CHIP_POWER_LVL_14 \ + (0x001EU) /*!< Chip-Specific event: Power Level 14 (e.g DPO, WLC) */ +#define RFAL_ANALOG_CONFIG_CHIP_POWER_LVL_15 \ + (0x001FU) /*!< Chip-Specific event: Power Level 15 (e.g DPO, WLC) */ + +#define RFAL_ANALOG_CONFIG_UPDATE_LAST \ + (0x00U) /*!< Value indicating Last configuration set during update */ +#define RFAL_ANALOG_CONFIG_UPDATE_MORE \ + (0x01U) /*!< Value indicating More configuration set coming during update */ + +/* + ****************************************************************************** + * GLOBAL MACROS + ****************************************************************************** + */ + +#define RFAL_ANALOG_CONFIG_ID_GET_POLL_LISTEN(id) \ + (RFAL_ANALOG_CONFIG_POLL_LISTEN_MODE_MASK & \ + (id)) /*!< Check if id indicates Listen mode */ + +#define RFAL_ANALOG_CONFIG_ID_GET_TECH(id) \ + (RFAL_ANALOG_CONFIG_TECH_MASK & \ + (id)) /*!< Get the technology of Configuration ID */ +#define RFAL_ANALOG_CONFIG_ID_IS_CHIP(id) \ + (RFAL_ANALOG_CONFIG_TECH_MASK & \ + (id)) /*!< Check if ID indicates Chip-specific */ +#define RFAL_ANALOG_CONFIG_ID_IS_NFCA(id) \ + (RFAL_ANALOG_CONFIG_TECH_NFCA & (id)) /*!< Check if ID indicates NFC-A */ +#define RFAL_ANALOG_CONFIG_ID_IS_NFCB(id) \ + (RFAL_ANALOG_CONFIG_TECH_NFCB & (id)) /*!< Check if ID indicates NFC-B */ +#define RFAL_ANALOG_CONFIG_ID_IS_NFCF(id) \ + (RFAL_ANALOG_CONFIG_TECH_NFCF & (id)) /*!< Check if ID indicates NFC-F */ +#define RFAL_ANALOG_CONFIG_ID_IS_AP2P(id) \ + (RFAL_ANALOG_CONFIG_TECH_AP2P & (id)) /*!< Check if ID indicates AP2P */ +#define RFAL_ANALOG_CONFIG_ID_IS_NFCV(id) \ + (RFAL_ANALOG_CONFIG_TECH_NFCV & (id)) /*!< Check if ID indicates NFC-V */ + +#define RFAL_ANALOG_CONFIG_ID_GET_BITRATE(id) \ + (RFAL_ANALOG_CONFIG_BITRATE_MASK & \ + (id)) /*!< Get Bitrate of Configuration ID */ +#define RFAL_ANALOG_CONFIG_ID_IS_COMMON(id) \ + (RFAL_ANALOG_CONFIG_BITRATE_MASK & \ + (id)) /*!< Check if ID indicates common bitrate */ +#define RFAL_ANALOG_CONFIG_ID_IS_106(id) \ + (RFAL_ANALOG_CONFIG_BITRATE_106 & \ + (id)) /*!< Check if ID indicates 106kbits/s */ +#define RFAL_ANALOG_CONFIG_ID_IS_212(id) \ + (RFAL_ANALOG_CONFIG_BITRATE_212 & \ + (id)) /*!< Check if ID indicates 212kbits/s */ +#define RFAL_ANALOG_CONFIG_ID_IS_424(id) \ + (RFAL_ANALOG_CONFIG_BITRATE_424 & \ + (id)) /*!< Check if ID indicates 424kbits/s */ +#define RFAL_ANALOG_CONFIG_ID_IS_848(id) \ + (RFAL_ANALOG_CONFIG_BITRATE_848 & \ + (id)) /*!< Check if ID indicates 848kbits/s */ +#define RFAL_ANALOG_CONFIG_ID_IS_1695(id) \ + (RFAL_ANALOG_CONFIG_BITRATE_1695 & \ + (id)) /*!< Check if ID indicates 1695kbits/s */ +#define RFAL_ANALOG_CONFIG_ID_IS_3390(id) \ + (RFAL_ANALOG_CONFIG_BITRATE_3390 & \ + (id)) /*!< Check if ID indicates 3390kbits/s */ +#define RFAL_ANALOG_CONFIG_ID_IS_6780(id) \ + (RFAL_ANALOG_CONFIG_BITRATE_6780 & \ + (id)) /*!< Check if ID indicates 6780kbits/s */ +#define RFAL_ANALOG_CONFIG_ID_IS_26(id) \ + (RFAL_ANALOG_CONFIG_BITRATE_26 & \ + (id)) /*!< Check if ID indicates 1 out of 4 bitrate */ +#define RFAL_ANALOG_CONFIG_ID_IS_1p6(id) \ + (RFAL_ANALOG_CONFIG_BITRATE_1p6 & \ + (id)) /*!< Check if ID indicates 1 out of 256 bitrate */ + +#define RFAL_ANALOG_CONFIG_ID_GET_DIRECTION(id) \ + (RFAL_ANALOG_CONFIG_DIRECTION_MASK & \ + (id)) /*!< Get Direction of Configuration ID */ +#define RFAL_ANALOG_CONFIG_ID_IS_TX(id) \ + (RFAL_ANALOG_CONFIG_TX & (id)) /*!< Check if id indicates TX */ +#define RFAL_ANALOG_CONFIG_ID_IS_RX(id) \ + (RFAL_ANALOG_CONFIG_RX & (id)) /*!< Check if id indicates RX */ + +#define RFAL_ANALOG_CONFIG_CONFIG_NUM(x) \ + (sizeof(x) / sizeof((x)[0])) /*!< Get Analog Config number */ + +/*! Set Analog Config ID value by: Mode, Technology, Bitrate and Direction */ +#define RFAL_ANALOG_CONFIG_ID_SET(mode, tech, br, direction) \ + (RFAL_ANALOG_CONFIG_ID_GET_POLL_LISTEN(mode) | \ + RFAL_ANALOG_CONFIG_ID_GET_TECH(tech) | \ + RFAL_ANALOG_CONFIG_ID_GET_BITRATE(br) | \ + RFAL_ANALOG_CONFIG_ID_GET_DIRECTION(direction)) + +/* + ****************************************************************************** + * GLOBAL DATA TYPES + ****************************************************************************** + */ + +typedef uint8_t + rfalAnalogConfigMode; /*!< Polling or Listening Mode of Configuration */ +typedef uint8_t rfalAnalogConfigTech; /*!< Technology of Configuration */ +typedef uint8_t rfalAnalogConfigBitrate; /*!< Bitrate of Configuration */ +typedef uint8_t rfalAnalogConfigDirection; /*!< Transmit/Receive direction of + Configuration */ + +typedef uint8_t rfalAnalogConfigRegAddr[2]; /*!< Register Address to ST Chip */ +typedef uint8_t rfalAnalogConfigRegMask; /*!< Register Mask Value */ +typedef uint8_t rfalAnalogConfigRegVal; /*!< Register Value */ + +typedef uint16_t rfalAnalogConfigId; /*!< Analog Configuration ID */ +typedef uint16_t rfalAnalogConfigOffset; /*!< Analog Configuration offset + address in the table */ +typedef uint8_t rfalAnalogConfigNum; /*!< Number of Analog settings for the + respective Configuration ID */ + +/*! Struct that contain the Register-Mask-Value set. Make sure that the whole + * structure size is even and unaligned! */ +typedef struct { + rfalAnalogConfigRegAddr addr; /*!< Register Address */ + rfalAnalogConfigRegMask mask; /*!< Register Mask Value */ + rfalAnalogConfigRegVal val; /*!< Register Value */ +} rfalAnalogConfigRegAddrMaskVal; + +/*! Struct that represents the Analog Configs */ +typedef struct { + uint8_t id[sizeof(rfalAnalogConfigId)]; /*!< Configuration ID */ + rfalAnalogConfigNum num; /*!< Number of Config Sets to follow */ + rfalAnalogConfigRegAddrMaskVal regSet[]; + /*!< Register-Mask-Value sets */ /* PRQA S 1060 # MISRA 18.7 - + Flexible Array Members are + the only meaningful way of + denoting a variable length + input buffer which follows a + fixed header structure. */ +} rfalAnalogConfig; + +/* +****************************************************************************** +* GLOBAL FUNCTION PROTOTYPES +****************************************************************************** +*/ + +/*! + ***************************************************************************** + * \brief Initialize the Analog Configuration + * + * Reset the Analog Configuration LUT pointer to reference to default settings. + * + ***************************************************************************** + */ +void rfalAnalogConfigInitialize(void); + +/*! + ***************************************************************************** + * \brief Indicate if the current Analog Configuration Table is complete and + *ready to be used. + * + * \return true if current Analog Configuration Table is complete and ready to + *be used. \return false if current Analog Configuration Table is incomplete + * + ***************************************************************************** + */ +bool rfalAnalogConfigIsReady(void); + +/*! + ***************************************************************************** + * \brief Write the whole Analog Configuration table in raw format + * + * Writes the Analog Configuration and Look Up Table with the given raw table + * + * NOTE: Function does not check the validity of the given Table contents + * + * \param[in] configTbl : location of config Table to be loaded + * \param[in] configTblSize : size of the config Table to be loaded + * + * \return RFAL_ERR_NONE : if setting is updated + * \return RFAL_ERR_PARAM : if configTbl is invalid + * \return RFAL_ERR_NOMEM : if the given Table is bigger exceeds the max size + * \return RFAL_ERR_REQUEST : if the update Configuration Id is disabled + * + ***************************************************************************** + */ +ReturnCode rfalAnalogConfigListWriteRaw(const uint8_t *configTbl, + uint16_t configTblSize); + +/*! + ***************************************************************************** + * \brief Write the Analog Configuration table with new analog settings. + * + * Writes the Analog Configuration and Look Up Table with the new list of + *register-mask-value and Configuration ID respectively. + * + * NOTE: Function does not check for the validity of the Register Address. + * + * \param[in] more : 0x00 indicates it is last Configuration ID settings; + * 0x01 indicates more Configuration ID setting(s) are + *coming. \param[in] *config : reference to the configuration list of current + *Configuraiton ID. + * + * \return RFAL_ERR_PARAM : if Configuration ID or parameter is invalid + * \return RFAL_ERR_NOMEM : if LUT is full + * \return RFAL_ERR_REQUEST : if the update Configuration Id is disabled + * \return RFAL_ERR_NONE : if setting is updated + * + ***************************************************************************** + */ +ReturnCode rfalAnalogConfigListWrite(uint8_t more, + const rfalAnalogConfig *config); + +/*! + ***************************************************************************** + * \brief Read the whole Analog Configuration table in raw format + * + * Reads the whole Analog Configuration Table in raw format + * + * \param[out] tblBuf : location to the buffer to place the Config + *Table \param[in] tblBufLen : length of the buffer to place the Config + *Table \param[out] configTblSize : Config Table size + * + * \return RFAL_ERR_PARAM : if configTbl or configTblSize is invalid + * \return RFAL_ERR_NOMEM : if configTblSize is not enough for the whole table + * \return RFAL_ERR_NONE : if read is successful + * + ***************************************************************************** + */ +ReturnCode rfalAnalogConfigListReadRaw(uint8_t *tblBuf, uint16_t tblBufLen, + uint16_t *configTblSize); + +/*! + ***************************************************************************** + * \brief Read the Analog Configuration table. + * + * Read the Analog Configuration Table + * + * \param[in] configOffset : offset to the next Configuration ID in the List + *Table to be read. \param[out] more : 0x00 indicates it is last + *Configuration ID settings; 0x01 indicates more Configuration ID setting(s) are + *coming. \param[out] config : configuration id, number of configuration + *sets and register-mask-value sets \param[in] numConfig : the remaining + *configuration settings space available; + * + * \return RFAL_ERR_NOMEM : if number of Configuration for respective + *Configuration ID is greater the the remaining configuration setting space + *available \return RFAL_ERR_NONE : if read is successful + * + ***************************************************************************** + */ +ReturnCode rfalAnalogConfigListRead(rfalAnalogConfigOffset *configOffset, + uint8_t *more, rfalAnalogConfig *config, + rfalAnalogConfigNum numConfig); + +/*! + ***************************************************************************** + * \brief Set the Analog settings of indicated Configuration ID. + * + * Update the chip with indicated analog settings of indicated Configuration ID. + * + * \param[in] configId : configuration ID + * + * \return RFAL_ERR_PARAM : if Configuration ID is invalid + * \return RFAL_ERR_INTERNAL : if error updating setting to chip + * \return RFAL_ERR_NONE : if new settings is applied to chip + * + ***************************************************************************** + */ +ReturnCode rfalSetAnalogConfig(rfalAnalogConfigId configId); + +/*! + ***************************************************************************** + * \brief Generates Analog Config mode ID + * + * Converts RFAL mode and bitrate into Analog Config Mode ID. + * + * Update the chip with indicated analog settings of indicated Configuration ID. + * + * \param[in] md : RFAL mode format + * \param[in] br : RFAL bit rate format + * \param[in] dir : Analog Config communication direction + * + * \return Analog Config Mode ID + * + ***************************************************************************** + */ +uint16_t rfalAnalogConfigGenModeID(rfalMode md, rfalBitRate br, uint16_t dir); + +#endif /* RFAL_ANALOG_CONFIG_H */ + +/** + * @} + * + * @} + * + * @} + */ diff --git a/core/embed/io/nfc/rfal/include/rfal_cd.h b/core/embed/io/nfc/rfal/include/rfal_cd.h new file mode 100644 index 0000000000..3f37c78790 --- /dev/null +++ b/core/embed/io/nfc/rfal/include/rfal_cd.h @@ -0,0 +1,173 @@ + +/****************************************************************************** + * @attention + * + * COPYRIGHT 2019 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/* + * PROJECT: ST25R firmware + * Revision: + * LANGUAGE: ISO C99 + */ + +/*! \file rfal_cd.h + * + * \author + * + * \brief Implementation of a Card Detection Algorithm + * + * + * + * \addtogroup RFAL + * @{ + * + * \addtogroup RFAL-HL + * \brief RFAL Higher Layer + * @{ + * + * \addtogroup CD + * \brief RFAL Card Detection + * @{ + * + */ + +#ifndef RFAL_CD_H +#define RFAL_CD_H + +/* + ****************************************************************************** + * INCLUDES + ****************************************************************************** + */ +#include "rfal_platform.h" +#include "rfal_utils.h" + +/* + ****************************************************************************** + * GLOBAL DEFINES + ****************************************************************************** + */ + +/* + ****************************************************************************** + * GLOBAL MACROS + ****************************************************************************** + */ + +/* +****************************************************************************** +* GLOBAL TYPES +****************************************************************************** +*/ + +/*! Card Detection NFC technology type */ +typedef enum { + RFAL_CD_TECH_NONE = 0x00, /*!< No NFC Technology */ + RFAL_CD_TECH_NFCA = 0x01, /*!< NFC Technology NFCB */ + RFAL_CD_TECH_NFCB = 0x02, /*!< NFC Technology NFCB */ + RFAL_CD_TECH_NFCF = 0x04, /*!< NFC Technology NFCF */ + RFAL_CD_TECH_NFCV = 0x08, /*!< NFC Technology NFCV */ + RFAL_CD_TECH_OTHER = 0x10 /*!< NFC Technology OTHER */ +} rfalCdTech; + +/*! Card Detection result|outcome type */ +typedef enum { + RFAL_CD_NOT_FOUND = 0, /* 1 + * \param[in] values : pointer with content to be written on the register(s) + * \param[in] len : number of consecutive registers to be written + * + * + * \return RFAL_ERR_PARAM : Invalid register or bad request + * \return RFAL_ERR_NOTSUPP : Feature not supported + * \return RFAL_ERR_NONE : Write done with no error + ***************************************************************************** + */ +ReturnCode rfalChipWriteReg(uint16_t reg, const uint8_t* values, uint8_t len); + +/*! + ***************************************************************************** + * \brief Reads a register on the RF Chip + * + * Checks if the given register is valid and if so, reads the value(s) + * of the RF Chip register(s) + * + * \param[in] reg : register address to be read, or the first if len > 1 + * \param[out] values : pointer where the register(s) read content will be + *placed \param[in] len : number of consecutive registers to be read + * + * \return RFAL_ERR_PARAM : Invalid register or bad request + * \return RFAL_ERR_NOTSUPP : Feature not supported + * \return RFAL_ERR_NONE : Read done with no error + ***************************************************************************** + */ +ReturnCode rfalChipReadReg(uint16_t reg, uint8_t* values, uint8_t len); + +/*! + ***************************************************************************** + * \brief Change a register on the RF Chip + * + * Change the value of the register bits on the RF Chip Test set in the + *valueMask. + * + * \param[in] reg : register address to be modified + * \param[in] valueMask : mask value of the register bits to be changed + * \param[in] value : register value to be set + * + * \return RFAL_ERR_PARAM : Invalid register or bad request + * \return RFAL_ERR_NOTSUPP : Feature not supported + * \return RFAL_ERR_OK : Change done with no error + ***************************************************************************** + */ +ReturnCode rfalChipChangeRegBits(uint16_t reg, uint8_t valueMask, + uint8_t value); + +/*! + ***************************************************************************** + * \brief Writes a Test register on the RF Chip + * + * Writes the value on the RF Chip Test register + * + * \param[in] reg : register address to be written + * \param[in] value : value to be written on the register + * + * + * \return RFAL_ERR_PARAM : Invalid register or bad request + * \return RFAL_ERR_NOTSUPP : Feature not supported + * \return RFAL_ERR_NONE : Write done with no error + ***************************************************************************** + */ +ReturnCode rfalChipWriteTestReg(uint16_t reg, uint8_t value); + +/*! + ***************************************************************************** + * \brief Reads a Test register on the RF Chip + * + * Reads the value of the RF Chip Test register + * + * \param[in] reg : register address to be read + * \param[out] value : pointer where the register content will be placed + * + * \return RFAL_ERR_PARAM :Invalid register or bad request + * \return RFAL_ERR_NOTSUPP : Feature not supported + * \return RFAL_ERR_NONE : Read done with no error + ***************************************************************************** + */ +ReturnCode rfalChipReadTestReg(uint16_t reg, uint8_t* value); + +/*! + ***************************************************************************** + * \brief Change a Test register on the RF Chip + * + * Change the value of the register bits on the RF Chip Test set in the + *valueMask. + * + * \param[in] reg : test register address to be modified + * \param[in] valueMask : mask value of the register bits to be changed + * \param[in] value : register value to be set + * + * \return RFAL_ERR_PARAM : Invalid register or bad request + * \return RFAL_ERR_NOTSUPP : Feature not supported + * \return RFAL_ERR_OK : Change done with no error + ***************************************************************************** + */ +ReturnCode rfalChipChangeTestRegBits(uint16_t reg, uint8_t valueMask, + uint8_t value); + +/*! + ***************************************************************************** + * \brief Execute command on the RF Chip + * + * Checks if the given command is valid and if so, executes it on + * the RF Chip + * + * \param[in] cmd : direct command to be executed + * + * \return RFAL_ERR_PARAM : Invalid command or bad request + * \return RFAL_ERR_NOTSUPP : Feature not supported + * \return RFAL_ERR_NONE : Direct command executed with no error + ***************************************************************************** + */ +ReturnCode rfalChipExecCmd(uint16_t cmd); + +/*! + ***************************************************************************** + * \brief Set RFO + * + * Sets the RFO driver resistance value used when the + * field is on (unmodulated/active) + * + * \param[in] rfo : the RFO value to be used + * + * \return RFAL_ERR_IO : Internal error + * \return RFAL_ERR_NOTSUPP : Feature not supported + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalChipSetRFO(uint8_t rfo); + +/*! + ***************************************************************************** + * \brief Get RFO + * + * Gets the RFO driver resistance value used when the + * field is on (unmodulated/active) + * + * \param[out] result : the current RFO value + * + * \return RFAL_ERR_IO : Internal error + * \return RFAL_ERR_NOTSUPP : Feature not supported + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalChipGetRFO(uint8_t* result); + +/*! + ***************************************************************************** + * \brief Get LM Field Indicator + * + * Gets an indicator of the signal on RFI while in Passive Listen Mode + * + * \param[out] result : the current RFI value + * + * \return RFAL_ERR_IO : Internal error + * \return RFAL_ERR_NOTSUPP : Feature not supported + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalChipGetLmFieldInd(uint8_t* result); + +/*! + ***************************************************************************** + * \brief Set Listen Mode Modulation + * + * Sets the modulation (modulated and unmodulated state) when Passive Listen + * Mode is used + * + * \param[in] mod : modulation to be used in modulated state + * \param[in] unmod : modulation to be used in unmodulated state + * + * \return RFAL_ERR_IO : Internal error + * \return RFAL_ERR_NOTSUPP : Feature not supported + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalChipSetLMMod(uint8_t mod, uint8_t unmod); + +/*! + ***************************************************************************** + * \brief Get Listen Mode Modulation + * + * Gets the modulation (modulated and unmodulated state) when Passive Listen + * Mode is used + * + * \param[out] mod : modulation set in modulated state + * \param[out] unmod : modulation set in unmodulated state + * + * \return RFAL_ERR_IO : Internal error + * \return RFAL_ERR_NOTSUPP : Feature not supported + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalChipGetLMMod(uint8_t* mod, uint8_t* unmod); + +/*! + ***************************************************************************** + * \brief Measure Amplitude + * + * Measures the RF Amplitude + * + * \param[out] result : result of RF measurement + * + * \return RFAL_ERR_IO : Internal error + * \return RFAL_ERR_NOTSUPP : Feature not supported + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalChipMeasureAmplitude(uint8_t* result); + +/*! + ***************************************************************************** + * \brief Measure Phase + * + * Measures the Phase + * + * \param[out] result : result of Phase measurement + * + * \return RFAL_ERR_IO : Internal error + * \return RFAL_ERR_NOTSUPP : Feature not supported + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalChipMeasurePhase(uint8_t* result); + +/*! + ***************************************************************************** + * \brief Measure Capacitance + * + * Measures the Capacitance + * + * \param[out] result : result of Capacitance measurement + * + * \return RFAL_ERR_IO : Internal error + * \return RFAL_ERR_NOTSUPP : Feature not supported + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalChipMeasureCapacitance(uint8_t* result); + +/*! + ***************************************************************************** + * \brief Measure Power Supply + * + * Measures the Power Supply + * + * \param[in] param : measurement parameter (chip specific) + * \param[out] result : result of the measurement + * + * \return RFAL_ERR_IO : Internal error + * \return RFAL_ERR_NOTSUPP : Feature not supported + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalChipMeasurePowerSupply(uint8_t param, uint8_t* result); + +/*! + ***************************************************************************** + * \brief Measure I and Q + * + * Measures I and Q channels + * + * \param[out] resI : 8 bit long result of the I channel (signed) + * \param[out] resQ : 8 bit long result of the Q channel (signed) + * + * \return RFAL_ERR_IO : Internal error + * \return RFAL_ERR_NOTSUPP : Feature not supported + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalChipMeasureIQ(int8_t* resI, int8_t* resQ); + +/*! + ***************************************************************************** + * \brief Measure combined I and Q + * + * Measures I and Q channels and combines them + * + * \param[out] result : I and Q combined + * + * \return RFAL_ERR_IO : Internal error + * \return RFAL_ERR_NOTSUPP : Feature not supported + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalChipMeasureCombinedIQ(uint8_t* result); + +/*! + ***************************************************************************** + * \brief Set Antenna mode + * + * Sets the antenna mode. + * Differential or single ended antenna mode (RFO1 or RFO2) + * + * \param[in] single : FALSE differential ; single ended mode + * \param[in] rfiox : FALSE RFI1/RFO1 ; TRUE RFI2/RFO2 + * + * \return RFAL_ERR_IO : Internal error + * \return RFAL_ERR_NOTSUPP : Feature not supported + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalChipSetAntennaMode(bool single, bool rfiox); + +#endif /* RFAL_CHIP_H */ + +/** + * @} + * + * @} + * + * @} + */ diff --git a/core/embed/io/nfc/rfal/include/rfal_defConfig.h b/core/embed/io/nfc/rfal/include/rfal_defConfig.h new file mode 100644 index 0000000000..783ee4686b --- /dev/null +++ b/core/embed/io/nfc/rfal/include/rfal_defConfig.h @@ -0,0 +1,340 @@ + +/****************************************************************************** + * @attention + * + * COPYRIGHT 2016 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/* + * PROJECT: ST25R + * Revision: + * LANGUAGE: ISO C99 + */ + +/*! \file rfal_defConfig.h + * + * \author Gustavo Patricio + * + * \brief RF Abstraction Layer (RFAL) default Config file + * + * This file contains a base/default configuration for the + * RFAL library. + * Users can and shall define their on configuration acording + * to their speficic system needs on rfal_platform.h. + * + * \addtogroup RFAL + * @{ + * + */ + +#ifndef RFAL_CONFIG_H +#define RFAL_CONFIG_H + +/* +****************************************************************************** +* INCLUDES +****************************************************************************** +*/ +#include "rfal_features.h" + +/* +****************************************************************************** +* GLOBAL DEFINES +****************************************************************************** +*/ + +/* +****************************************************************************** +* RFAL FEATURES DEFAULT CONFIGURATION +****************************************************************************** +*/ +#ifndef RFAL_FEATURE_LISTEN_MODE +#if RFAL_SUPPORT_CE || RFAL_SUPPORT_MODE_LISTEN_ACTIVE_P2P +#define RFAL_FEATURE_LISTEN_MODE \ + true /*!< Enable RFAL support for Listen Mode */ +#endif /* SUPPORT LISTEN_MODE */ +#endif /* RFAL_FEATURE_LISTEN_MODE */ + +#ifndef RFAL_FEATURE_WAKEUP_MODE +#define RFAL_FEATURE_WAKEUP_MODE \ + true /*!< Enable RFAL support for the Wake-Up mode */ +#endif /* RFAL_FEATURE_WAKEUP_MODE */ + +#ifndef RFAL_FEATURE_LOWPOWER_MODE +#define RFAL_FEATURE_LOWPOWER_MODE \ + false /*!< RFAL support for the Low Power mode, Disabled by default */ +#endif /* RFAL_FEATURE_LOWPOWER_MODE */ + +#ifndef RFAL_FEATURE_NFCA +#if RFAL_SUPPORT_MODE_POLL_NFCA +#define RFAL_FEATURE_NFCA \ + true /*!< Enable RFAL support for NFC-A (ISO14443A) \ + */ +#endif /* RFAL_SUPPORT_MODE_POLL_NFCA */ +#endif /* RFAL_FEATURE_NFCA */ + +#ifndef RFAL_FEATURE_T1T +#if RFAL_SUPPORT_MODE_POLL_NFCA +#define RFAL_FEATURE_T1T true /*!< Enable RFAL support for T1T (Topaz) */ +#endif /* RFAL_SUPPORT_MODE_POLL_NFCA */ +#endif /* RFAL_FEATURE_T1T */ + +#ifndef RFAL_FEATURE_T2T +#if RFAL_SUPPORT_MODE_POLL_NFCA +#define RFAL_FEATURE_T2T true /*!< Enable RFAL support for T2T */ +#endif /* RFAL_SUPPORT_MODE_POLL_NFCA */ +#endif /* RFAL_FEATURE_T2T */ + +#ifndef RFAL_FEATURE_T4T +#if RFAL_SUPPORT_MODE_POLL_NFCA +#define RFAL_FEATURE_T4T true /*!< Enable RFAL support for T4T */ +#endif /* RFAL_SUPPORT_MODE_POLL_NFCA */ +#endif /* RFAL_FEATURE_T2T */ + +#ifndef RFAL_FEATURE_NFCB +#if RFAL_SUPPORT_MODE_POLL_NFCB +#define RFAL_FEATURE_NFCB \ + true /*!< Enable RFAL support for NFC-B (ISO14443B) \ + */ +#endif /* RFAL_SUPPORT_MODE_POLL_NFCB */ +#endif /* RFAL_FEATURE_NFCB */ + +#ifndef RFAL_FEATURE_ST25TB +#if RFAL_SUPPORT_MODE_POLL_NFCB +#define RFAL_FEATURE_ST25TB true /*!< Enable RFAL support for ST25TB */ +#endif /* RFAL_SUPPORT_MODE_POLL_NFCB */ +#endif /* RFAL_FEATURE_ST25TB */ + +#ifndef RFAL_FEATURE_NFCF +#if RFAL_SUPPORT_MODE_POLL_NFCF +#define RFAL_FEATURE_NFCF true /*!< Enable RFAL support for NFC-F (FeliCa) */ +#endif /* RFAL_SUPPORT_MODE_POLL_NFCF */ +#endif /* RFAL_FEATURE_NFCF */ + +#ifndef RFAL_FEATURE_NFCV +#if RFAL_SUPPORT_MODE_POLL_NFCV +#define RFAL_FEATURE_NFCV \ + true /*!< Enable RFAL support for NFC-V (ISO15693) \ + */ +#endif /* RFAL_SUPPORT_MODE_POLL_NFCV */ +#endif /* RFAL_FEATURE_NFCV */ + +#ifndef RFAL_FEATURE_ISO_DEP +#if RFAL_SUPPORT_MODE_POLL_NFCA || RFAL_SUPPORT_MODE_POLL_NFCB || \ + RFAL_SUPPORT_CE +#define RFAL_FEATURE_ISO_DEP \ + true /*!< Enable RFAL support for ISO-DEP (ISO14443-4) */ +#endif /* RFAL_SUPPORT_MODE_ */ +#endif /* RFAL_FEATURE_ISO_DEP */ + +#ifndef RFAL_FEATURE_ISO_DEP_POLL +#if RFAL_SUPPORT_MODE_POLL_NFCA || RFAL_SUPPORT_MODE_POLL_NFCB +#define RFAL_FEATURE_ISO_DEP_POLL \ + true /*!< Enable RFAL support for Poller mode (PCD) ISO-DEP (ISO14443-4) */ +#endif /* RFAL_SUPPORT_MODE_ */ +#endif /* RFAL_FEATURE_ISO_DEP */ + +#ifndef RFAL_FEATURE_ISO_DEP_LISTEN +#if RFAL_SUPPORT_CE +#define RFAL_FEATURE_ISO_DEP_LISTEN \ + true /*!< Enable RFAL support for Listen mode (PICC) ISO-DEP (ISO14443-4) */ +#endif /* RFAL_SUPPORT_MODE_ */ +#endif /* RFAL_FEATURE_ISO_DEP */ + +#ifndef RFAL_FEATURE_ISO_DEP_IBLOCK_MAX_LEN +#if RFAL_FEATURE_ISO_DEP +#define RFAL_FEATURE_ISO_DEP_IBLOCK_MAX_LEN \ + 256U /*!< ISO-DEP I-Block max length. Please use values as defined by \ + rfalIsoDepFSx */ +#endif /* RFAL_FEATURE_ISO_DEP */ +#endif /* RFAL_FEATURE_ISO_DEP_IBLOCK_MAX_LEN */ + +#ifndef RFAL_FEATURE_ISO_DEP_APDU_MAX_LEN +#if RFAL_FEATURE_ISO_DEP +#define RFAL_FEATURE_ISO_DEP_APDU_MAX_LEN \ + 512U /*!< ISO-DEP APDU max length. \ + */ +#endif /* RFAL_FEATURE_ISO_DEP */ +#endif /* RFAL_FEATURE_ISO_DEP_APDU_MAX_LEN */ + +#ifndef RFAL_FEATURE_NFC_DEP +#if RFAL_SUPPORT_MODE_POLL_NFCA && RFAL_SUPPORT_MODE_POLL_NFCF +#define RFAL_FEATURE_NFC_DEP \ + true /*!< Enable RFAL support for NFC-DEP (NFCIP1/P2P) */ +#endif /* RFAL_SUPPORT_MODE_POLL_NFCA/F */ +#endif /* RFAL_FEATURE_NFC_DEP */ + +#ifndef RFAL_FEATURE_NFC_DEP_BLOCK_MAX_LEN +#if RFAL_FEATURE_NFC_DEP +#define RFAL_FEATURE_NFC_DEP_BLOCK_MAX_LEN \ + 254U /*!< NFC-DEP Block/Payload length. Allowed values: 64, 128, 192, 254 */ +#endif /* RFAL_FEATURE_NFC_DEP */ +#endif /* RFAL_FEATURE_NFC_DEP_BLOCK_MAX_LEN */ + +#ifndef RFAL_FEATURE_NFC_DEP_PDU_MAX_LEN +#if RFAL_FEATURE_NFC_DEP +#define RFAL_FEATURE_NFC_DEP_PDU_MAX_LEN 512U /*!< NFC-DEP PDU max length. */ +#endif /* RFAL_FEATURE_NFC_DEP */ +#endif /* RFAL_FEATURE_NFC_DEP_PDU_MAX_LEN */ + +#ifndef RFAL_FEATURE_NFC_RF_BUF_LEN +#define RFAL_FEATURE_NFC_RF_BUF_LEN \ + 258U /*!< RF buffer length used by RFAL NFC layer */ +#endif /* RFAL_FEATURE_NFC_RF_BUF_LEN */ + +#ifndef RFAL_FEATURE_ST25xV +#define RFAL_FEATURE_ST25xV \ + false /*!< ST25xV Module configuration missing. Disabled by default */ +#endif + +#ifndef RFAL_FEATURE_DYNAMIC_ANALOG_CONFIG +#define RFAL_FEATURE_DYNAMIC_ANALOG_CONFIG \ + false /*!< Dynamic Analog Configs configuration missing. Disabled by default \ + */ +#endif + +#ifndef RFAL_FEATURE_DPO +#define RFAL_FEATURE_DPO \ + false /*!< Dynamic Power Module configuration missing. Disabled by default \ + */ +#endif + +#ifndef RFAL_FEATURE_DLMA +#define RFAL_FEATURE_DLMA \ + false /*!< Dynamic LMA Module configuration missing. Disabled by default */ +#endif + +/* +****************************************************************************** +* RFAL OPTIONAL MACROS +****************************************************************************** +*/ + +#ifndef platformProtectST25RIrqStatus +#define platformProtectST25RIrqStatus() /*!< Protect unique access to IRQ \ + status var - IRQ disable on single \ + thread environment (MCU) ; Mutex \ + lock on a multi thread environment \ + */ +#endif /* platformProtectST25RIrqStatus */ + +#ifndef platformUnprotectST25RIrqStatus +#define platformUnprotectST25RIrqStatus() /*!< Unprotect the IRQ status var - \ + IRQ enable on a single thread \ + environment (MCU) ; Mutex unlock \ + on a multi thread environment */ +#endif /* platformUnprotectST25RIrqStatus */ + +#ifndef platformProtectWorker +#define platformProtectWorker() /*!< Protect RFAL Worker/Task/Process from \ + concurrent execution on multi thread \ + platforms */ +#endif /* platformProtectWorker */ + +#ifndef platformUnprotectWorker +#define platformUnprotectWorker() /*!< Unprotect RFAL Worker/Task/Process from \ + concurrent execution on multi thread \ + platforms */ +#endif /* platformUnprotectWorker */ + +#ifndef platformIrqST25RPinInitialize +#define platformIrqST25RPinInitialize() /*!< Initializes ST25R IRQ pin */ +#endif /* platformIrqST25RPinInitialize */ + +#ifndef platformIrqST25RSetCallback +#define platformIrqST25RSetCallback(cb) /*!< Sets ST25R ISR callback */ +#endif /* platformIrqST25RSetCallback */ + +#ifndef platformLedsInitialize +#define platformLedsInitialize() /*!< Initializes the pins used as LEDs to \ + outputs */ +#endif /* platformLedsInitialize */ + +#ifndef platformLedOff +#define platformLedOff(port, pin) /*!< Turns the given LED Off */ +#endif /* platformLedOff */ + +#ifndef platformLedOn +#define platformLedOn(port, pin) /*!< Turns the given LED On */ +#endif /* platformLedOn */ + +#ifndef platformLedToggle +#define platformLedToggle(port, pin) /*!< Toggles the given LED */ +#endif /* platformLedToggle */ + +#ifndef platformGetSysTick +#define platformGetSysTick() /*!< Get System Tick ( 1 tick = 1 ms) */ +#endif /* platformGetSysTick */ + +#ifndef platformTimerDestroy +#define platformTimerDestroy(timer) /*!< Stops and released the given timer */ +#endif /* platformTimerDestroy */ + +#ifndef platformLog +#define platformLog(...) /*!< Log method */ +#endif /* platformLog */ + +#ifndef platformAssert +#define platformAssert(exp) /*!< Asserts whether the given expression is true \ + */ +#endif /* platformAssert */ + +#ifndef platformErrorHandle +#define platformErrorHandle() /*!< Global error handler or trap */ +#endif /* platformErrorHandle */ + +#ifdef RFAL_USE_I2C + +#ifndef platformSpiTxRx +#define platformSpiTxRx(txBuf, rxBuf, len) /*!< SPI transceive */ +#endif /* platformSpiTxRx */ + +#else /* RFAL_USE_I2C */ + +#ifndef platformI2CTx +#define platformI2CTx(txBuf, len, last, txOnly) /*!< I2C Transmit */ +#endif /* platformI2CTx */ + +#ifndef platformI2CRx +#define platformI2CRx(txBuf, len) /*!< I2C Receive */ +#endif /* platformI2CRx */ + +#ifndef platformI2CStart +#define platformI2CStart() /*!< I2C Start condition */ +#endif /* platformI2CStart */ + +#ifndef platformI2CStop +#define platformI2CStop() /*!< I2C Stop condition */ +#endif /* platformI2CStop */ + +#ifndef platformI2CRepeatStart +#define platformI2CRepeatStart() /*!< I2C Repeat Start */ +#endif /* platformI2CRepeatStart */ + +#ifndef platformI2CSlaveAddrWR +#define platformI2CSlaveAddrWR( \ + add) /*!< I2C Slave address for Write operation */ +#endif /* platformI2CSlaveAddrWR */ + +#ifndef platformI2CSlaveAddrRD +#define platformI2CSlaveAddrRD( \ + add) /*!< I2C Slave address for Read operation */ +#endif /* platformI2CSlaveAddrRD */ + +#endif /* RFAL_USE_I2C */ + +#endif /* RFAL_CONFIG_H */ + +/** + * @} + * + */ diff --git a/core/embed/io/nfc/rfal/include/rfal_dpo.h b/core/embed/io/nfc/rfal/include/rfal_dpo.h new file mode 100644 index 0000000000..6c3f72ab56 --- /dev/null +++ b/core/embed/io/nfc/rfal/include/rfal_dpo.h @@ -0,0 +1,223 @@ + +/****************************************************************************** + * @attention + * + * COPYRIGHT 2016 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/* + * PROJECT: ST25R391x firmware + * $Revision: $ + * LANGUAGE: ISO C99 + */ + +/*! \file rfal_dpo.h + * + * \author Martin Zechleitner + * + * \brief Dynamic Power adjustment + * + * This module provides an interface to perform the power adjustment + * dynamically + * + * + * \addtogroup RFAL + * @{ + * + * \addtogroup RFAL-HAL + * \brief RFAL Hardware Abstraction Layer + * @{ + * + * \addtogroup DPO + * \brief RFAL Dynamic Power Module + * @{ + * + */ + +#ifndef RFAL_DPO_H +#define RFAL_DPO_H + +/* + ****************************************************************************** + * INCLUDES + ****************************************************************************** + */ +#include "rfal_platform.h" +#include "rfal_utils.h" + +/* + ****************************************************************************** + * GLOBAL DEFINES + ****************************************************************************** + */ + +#define RFAL_DPO_TABLE_MAX_ENTRIES 4U /*!< Max DPO table entries */ +#define RFAL_DPO_TABLE_PARAM_LEN \ + sizeof(rfalDpoEntry) /*!< DPO Parameter length */ +#define RFAL_DPO_TABLE_SIZE_MAX \ + (RFAL_DPO_TABLE_MAX_ENTRIES * \ + RFAL_DPO_TABLE_PARAM_LEN) /*!< Max DPO table size */ + +/* +****************************************************************************** +* GLOBAL TYPES +****************************************************************************** +*/ + +/*! DPO table entry struct */ +typedef struct { + uint8_t rfoRes; /*!< Setting for the resistance level of the RFO */ + uint8_t inc; /*!< Threshold for incrementing the output power */ + uint8_t dec; /*!< Threshold for decrementing the output power */ +} rfalDpoEntry; + +/*! Function pointer to methode doing the reference measurement */ +typedef ReturnCode (*rfalDpoMeasureFunc)(uint8_t* res); + +/* +****************************************************************************** +* GLOBAL FUNCTION PROTOTYPES +****************************************************************************** +*/ + +/*! + ***************************************************************************** + * \brief Initialize dynamic power table + * + * This function sets the internal dynamic power table to the default + * values stored in rfal_DpoTbl.h + * + ***************************************************************************** + */ +void rfalDpoInitialize(void); + +/*! + ***************************************************************************** + * \brief Set the measurement methode + * + * This function sets the measurement method used for reference measurement. + * Based on the measurement the power will then be adjusted + * + * \param[in] pFunc: callback of measurement function + * + ***************************************************************************** + */ +void rfalDpoSetMeasureCallback(rfalDpoMeasureFunc pFunc); + +/*! + ***************************************************************************** + * \brief Write dynamic power table + * + * Load the dynamic power table + * + * \param[in] powerTbl : location of power Table to be loaded + * \param[in] powerTblEntries : number of entries of the power Table to be + *loaded + * + * \return RFAL_ERR_NONE : No error + * \return RFAL_ERR_PARAM : if configTbl is invalid + * \return RFAL_ERR_NOMEM : if the given Table is bigger exceeds the max size + ***************************************************************************** + */ +ReturnCode rfalDpoTableWrite(const rfalDpoEntry* powerTbl, + uint8_t powerTblEntries); + +/*! + ***************************************************************************** + * \brief Dynamic power table Read + * + * Read the dynamic power table + * + * \param[out] tblBuf : location to the rfalDpoEntry[] to place the + *Table \param[in] tblBufEntries : number of entries available in tblBuf to + *place the power Table \param[out] tableEntries : returned number of entries + *actually written into tblBuf + * + * \return RFAL_ERR_NONE : No error + * \return RFAL_ERR_PARAM : if configTbl is invalid or parameters are invalid + ***************************************************************************** + */ +ReturnCode rfalDpoTableRead(rfalDpoEntry* tblBuf, uint8_t tblBufEntries, + uint8_t* tableEntries); + +/*! + ***************************************************************************** + * \brief Dynamic power adjust + * + * It measures the current output and adjusts the power accordingly to + * the dynamic power table. + * This method | The adjustment shall be performed when the device + * is already emiting RF field + * + * \return RFAL_ERR_NONE : No error + * \return RFAL_ERR_PARAM : if configTbl is invalid or parameters are + *invalid \return RFAL_ERR_WRONG_STATE : if the current state is valid for DPO + *Adjustment + ***************************************************************************** + */ +ReturnCode rfalDpoAdjust(void); + +/*! + ***************************************************************************** + * \brief Get Current Dynamic power table entry + * + * Return current used DPO power table entry settings + * + * \return RFAL_ERR_NONE : Current DpoEntry. This includes d_res, inc and dec + * + ***************************************************************************** + */ +const rfalDpoEntry* rfalDpoGetCurrentTableEntry(void); + +/*! + ***************************************************************************** + * \brief Get Current Dynamic power table index + * + * \return the index currently used DPO table entry + * + ***************************************************************************** + */ +uint8_t rfalDpoGetCurrentTableIndex(void); + +/*! + ***************************************************************************** + * \brief Dynamic power set enabled state + * + * \param[in] enable : new active state + * + * Set state to enable or disable the Dynamic power adjustment + * + ***************************************************************************** + */ +void rfalDpoSetEnabled(bool enable); + +/*! + ***************************************************************************** + * \brief Get the Dynamic power enabled state + * + * Get state of the Dynamic power adjustment + * + * \return true : DPO is enabled + * \return false : DPO is disabled + ***************************************************************************** + */ +bool rfalDpoIsEnabled(void); + +#endif /* RFAL_DPO_H */ + +/** + * @} + * + * @} + * + * @} + */ diff --git a/core/embed/io/nfc/rfal/include/rfal_isoDep.h b/core/embed/io/nfc/rfal/include/rfal_isoDep.h new file mode 100644 index 0000000000..981e0398d0 --- /dev/null +++ b/core/embed/io/nfc/rfal/include/rfal_isoDep.h @@ -0,0 +1,1108 @@ + +/****************************************************************************** + * @attention + * + * COPYRIGHT 2016 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/* + * PROJECT: ST25R391x firmware + * Revision: + * LANGUAGE: ISO C99 + */ + +/*! \file rfal_isoDep.h + * + * \author Gustavo Patricio + * + * \brief Implementation of ISO-DEP protocol + * + * This implementation was based on the following specs: + * - ISO/IEC 14443-4 2nd Edition 2008-07-15 + * - NFC Forum Digital Protocol 1.1 2014-01-14 + * + * + * \addtogroup RFAL + * @{ + * + * \addtogroup RFAL-AL + * \brief RFAL Abstraction Layer + * @{ + * + * \addtogroup ISO-DEP + * \brief RFAL ISO-DEP Module + * @{ + * + */ + +#ifndef RFAL_ISODEP_H_ +#define RFAL_ISODEP_H_ +/* + ****************************************************************************** + * INCLUDES + ****************************************************************************** + */ +#include "rfal_nfcb.h" +#include "rfal_platform.h" + +/* + ****************************************************************************** + * ENABLE SWITCH + ****************************************************************************** + */ + +/* If module is disabled remove the need for the user to set lengths */ +#if !RFAL_FEATURE_ISO_DEP +#undef RFAL_FEATURE_ISO_DEP_IBLOCK_MAX_LEN +#undef RFAL_FEATURE_ISO_DEP_APDU_MAX_LEN + +#define RFAL_FEATURE_ISO_DEP_IBLOCK_MAX_LEN \ + (1U) /*!< ISO-DEP I-Block max length, set to "none" */ +#define RFAL_FEATURE_ISO_DEP_APDU_MAX_LEN \ + (1U) /*!< ISO-DEP APDU max length, set to "none" */ +#endif /* !RFAL_FEATURE_ISO_DEP */ + +/* + ****************************************************************************** + * DEFINES + ****************************************************************************** + */ + +#define RFAL_ISODEP_PROLOGUE_SIZE \ + (3U) /*!< Length of Prologue Field for I-Block Format */ + +#define RFAL_ISODEP_PCB_LEN (1U) /*!< PCB length */ +#define RFAL_ISODEP_DID_LEN (1U) /*!< DID length */ +#define RFAL_ISODEP_NAD_LEN (1U) /*!< NAD length */ +#define RFAL_ISODEP_NO_DID \ + (0x10U) /*!< DID value indicating the ISO-DEP layer not to use DID */ +#define RFAL_ISODEP_NO_NAD \ + (0xFFU) /*!< NAD value indicating the ISO-DEP layer not to use NAD */ + +#define RFAL_ISODEP_FWI_MASK (0xF0U) /*!< Mask bits of FWI */ +#define RFAL_ISODEP_FWI_SHIFT (4U) /*!< Shift val of FWI */ +#define RFAL_ISODEP_FWI_DEFAULT \ + (4U) /*!< Default value for FWI Digital 1.0 11.6.2.17 */ +#define RFAL_ISODEP_ADV_FEATURE \ + (0x0FU) /*!< Indicate 256 Bytes FSD and Advanc Proto Feature support:NAD & \ + DID */ + +#define RFAL_ISODEP_DID_MAX (14U) /*!< Maximum DID value */ + +#define RFAL_ISODEP_BRI_MASK \ + (0x07U) /*!< Mask bits for Poll to Listen Send bitrate */ +#define RFAL_ISODEP_BSI_MASK \ + (0x70U) /*!< Mask bits for Listen to Poll Send bitrate */ +#define RFAL_ISODEP_SAME_BITRATE_MASK \ + (0x80U) /*!< Mask bit indicate only same bit rate D for both direction \ + support */ +#define RFAL_ISODEP_BITRATE_RFU_MASK (0x08U) /*!< Mask bit for RFU */ + +/*! Maximum Frame Waiting Time = ((256 * 16/fc) * 2^FWImax) = ((256*16/fc)*2^14) + * = (67108864)/fc = 2^26 (1/fc) */ +#define RFAL_ISODEP_MAX_FWT ((uint32_t)1U << 26) + +#define RFAL_ISODEP_FSDI_DEFAULT \ + RFAL_ISODEP_FSXI_256 /*!< Default Frame Size Integer in Poll mode */ +#define RFAL_ISODEP_FSX_KEEP \ + (0xFFU) /*!< Flag to keep FSX from activation */ +#define RFAL_ISODEP_DEFAULT_FSCI \ + RFAL_ISODEP_FSXI_256 /*!< FSCI default value to be used in Listen Mode */ +#define RFAL_ISODEP_DEFAULT_FSC \ + RFAL_ISODEP_FSX_256 /*!< FSC default value (aligned \ + RFAL_ISODEP_DEFAULT_FSCI) */ +#define RFAL_ISODEP_DEFAULT_SFGI \ + (0U) /*!< SFGI Default value to be used in Listen Mode */ +#define RFAL_ISODEP_DEFAULT_FWI \ + (8U) /*!< Default Listener FWI (Max) Digital 2.0 B7 & B3 */ + +#define RFAL_ISODEP_APDU_MAX_LEN RFAL_ISODEP_FSX_1024 /*!< Max APDU length */ + +#define RFAL_ISODEP_ATTRIB_RES_MBLI_NO_INFO \ + (0x00U) /*!< MBLI indicating no information on its internal input buffer \ + size */ +#define RFAL_ISODEP_ATTRIB_REQ_PARAM1_DEFAULT \ + (0x00U) /*!< Default values of Param 1 of ATTRIB_REQ Digital 1.0 12.6.1.3-5 \ + */ +#define RFAL_ISODEP_ATTRIB_HLINFO_LEN \ + (32U) /*!< Maximum Size of Higher Layer Information */ +#define RFAL_ISODEP_ATS_HB_MAX_LEN \ + (15U) /*!< Maximum length of Historical Bytes Digital 1.1 13.6.2.23 */ +#define RFAL_ISODEP_ATTRIB_REQ_MIN_LEN \ + (9U) /*!< Minimum Length of ATTRIB_REQ command */ +#define RFAL_ISODEP_ATTRIB_RES_MIN_LEN \ + (1U) /*!< Minimum Length of ATTRIB_RES response */ + +#define RFAL_ISODEP_SPARAM_VALUES_MAX_LEN \ + (16U) /*!< Maximum Length of the value field on S(PARAMETERS) */ +#define RFAL_ISODEP_SPARAM_TAG_BLOCKINFO \ + (0xA0U) /*!< S(PARAMETERS) tag Block information */ +#define RFAL_ISODEP_SPARAM_TAG_BRREQ \ + (0xA1U) /*!< S(PARAMETERS) tag Bit rates Request */ +#define RFAL_ISODEP_SPARAM_TAG_BRIND \ + (0xA2U) /*!< S(PARAMETERS) tag Bit rates Indication */ +#define RFAL_ISODEP_SPARAM_TAG_BRACT \ + (0xA3U) /*!< S(PARAMETERS) tag Bit rates Activation */ +#define RFAL_ISODEP_SPARAM_TAG_BRACK \ + (0xA4U) /*!< S(PARAMETERS) tag Bit rates Acknowledgement */ + +#define RFAL_ISODEP_SPARAM_TAG_SUP_PCD2PICC \ + (0x80U) /*!< S(PARAMETERS) tag Supported bit rates from PCD to PICC */ +#define RFAL_ISODEP_SPARAM_TAG_SUP_PICC2PCD \ + (0x81U) /*!< S(PARAMETERS) tag Supported bit rates from PICC to PCD */ +#define RFAL_ISODEP_SPARAM_TAG_SUP_FRAME \ + (0x82U) /*!< S(PARAMETERS) tag Supported framing options PICC to PCD */ +#define RFAL_ISODEP_SPARAM_TAG_SEL_PCD2PICC \ + (0x83U) /*!< S(PARAMETERS) tag Selected bit rate from PCD to PICC */ +#define RFAL_ISODEP_SPARAM_TAG_SEL_PICC2PCD \ + (0x84U) /*!< S(PARAMETERS) tag Selected bit rate from PICC to PCD */ +#define RFAL_ISODEP_SPARAM_TAG_SEL_FRAME \ + (0x85U) /*!< S(PARAMETERS) tag Selected framing options PICC to PCD */ + +#define RFAL_ISODEP_SPARAM_TAG_LEN (1) /*!< S(PARAMETERS) Tag Length */ +#define RFAL_ISODEP_SPARAM_TAG_BRREQ_LEN \ + (0U) /*!< S(PARAMETERS) tag Bit rates Request Length */ +#define RFAL_ISODEP_SPARAM_TAG_PICC2PCD_LEN \ + (2U) /*!< S(PARAMETERS) bit rates from PCD to PICC Length */ +#define RFAL_ISODEP_SPARAM_TAG_PCD2PICC_LEN \ + (2U) /*!< S(PARAMETERS) bit rates from PICC to PCD Length */ +#define RFAL_ISODEP_SPARAM_TAG_BRACK_LEN \ + (0U) /*!< S(PARAMETERS) tag Bit rates Acknowledgement Length */ + +#define RFAL_ISODEP_ATS_TA_DPL_212 \ + (0x01U) /*!< ATS TA DSI 212 kbps support bit mask */ +#define RFAL_ISODEP_ATS_TA_DPL_424 \ + (0x02U) /*!< ATS TA DSI 424 kbps support bit mask */ +#define RFAL_ISODEP_ATS_TA_DPL_848 \ + (0x04U) /*!< ATS TA DSI 848 kbps support bit mask */ +#define RFAL_ISODEP_ATS_TA_DLP_212 \ + (0x10U) /*!< ATS TA DSI 212 kbps support bit mask */ +#define RFAL_ISODEP_ATS_TA_DLP_424 \ + (0x20U) /*!< ATS TA DRI 424 kbps support bit mask */ +#define RFAL_ISODEP_ATS_TA_DLP_848 \ + (0x40U) /*!< ATS TA DRI 848 kbps support bit mask */ +#define RFAL_ISODEP_ATS_TA_SAME_D \ + (0x80U) /*!< ATS TA same bit both directions bit mask */ +#define RFAL_ISODEP_ATS_TB_FWI_MASK \ + (0xF0U) /*!< Mask bits for FWI (Frame Waiting Integer) in TB byte */ +#define RFAL_ISODEP_ATS_TB_SFGI_MASK \ + (0x0FU) /*!< Mask bits for SFGI (Start-Up Frame Guard Integer) in TB byte */ + +#define RFAL_ISODEP_ATS_T0_TA_PRESENCE_MASK \ + (0x10U) /*!< Mask bit for TA presence */ +#define RFAL_ISODEP_ATS_T0_TB_PRESENCE_MASK \ + (0x20U) /*!< Mask bit for TB presence */ +#define RFAL_ISODEP_ATS_T0_TC_PRESENCE_MASK \ + (0x40U) /*!< Mask bit for TC presence */ +#define RFAL_ISODEP_ATS_T0_FSCI_MASK \ + (0x0FU) /*!< Mask bit for FSCI presence \ + */ +#define RFAL_ISODEP_ATS_T0_OFFSET (0x01U) /*!< Offset of T0 in ATS Response */ + +#define RFAL_ISODEP_MAX_I_RETRYS \ + (2U) /*!< Number of retries for a I-Block Digital 2.0 16.2.5.4 */ +#define RFAL_ISODEP_MAX_R_RETRYS \ + (3U) /*!< Number of retries for a R-Block Digital 2.0 B9 - nRETRY \ + ACK/NAK: [2,5] */ +#define RFAL_ISODEP_MAX_WTX_NACK_RETRYS \ + (3U) /*!< Number of S(WTX) replied with NACK Digital 2.0 B9 - nRETRY \ + WTX[2,5] */ +#define RFAL_ISODEP_MAX_WTX_RETRYS \ + (20U) /*!< Number of overall S(WTX) retries Digital 2.0 16.2.5.2 */ +#define RFAL_ISODEP_MAX_WTX_RETRYS_ULTD \ + (255U) /*!< Use unlimited number of overall S(WTX) */ +#define RFAL_ISODEP_MAX_DSL_RETRYS \ + (0U) /*!< Number of retries for a S(DESELECT) Digital 2.0 B9 - nRETRY \ + DESELECT: [0,5] */ +#define RFAL_ISODEP_RATS_RETRIES \ + (1U) /*!< RATS retries upon fail Digital 2.0 B7 - nRETRY RATS \ + [0,1] */ + +/*! Frame Size for Proximity Card Integer definitions */ +typedef enum { + RFAL_ISODEP_FSXI_16 = + 0, /*!< Frame Size for Proximity Card Integer with 16 bytes */ + RFAL_ISODEP_FSXI_24 = + 1, /*!< Frame Size for Proximity Card Integer with 24 bytes */ + RFAL_ISODEP_FSXI_32 = + 2, /*!< Frame Size for Proximity Card Integer with 32 bytes */ + RFAL_ISODEP_FSXI_40 = + 3, /*!< Frame Size for Proximity Card Integer with 40 bytes */ + RFAL_ISODEP_FSXI_48 = + 4, /*!< Frame Size for Proximity Card Integer with 48 bytes */ + RFAL_ISODEP_FSXI_64 = + 5, /*!< Frame Size for Proximity Card Integer with 64 bytes */ + RFAL_ISODEP_FSXI_96 = + 6, /*!< Frame Size for Proximity Card Integer with 96 bytes */ + RFAL_ISODEP_FSXI_128 = + 7, /*!< Frame Size for Proximity Card Integer with 128 bytes */ + RFAL_ISODEP_FSXI_256 = + 8, /*!< Frame Size for Proximity Card Integer with 256 bytes */ + RFAL_ISODEP_FSXI_512 = 9, /*!< Frame Size for Proximity Card Integer with 512 + bytes ISO14443-3 Amd2 2012 */ + RFAL_ISODEP_FSXI_1024 = 10, /*!< Frame Size for Proximity Card Integer with + 1024 bytes ISO14443-3 Amd2 2012 */ + RFAL_ISODEP_FSXI_2048 = 11, /*!< Frame Size for Proximity Card Integer with + 2048 bytes ISO14443-3 Amd2 2012 */ + RFAL_ISODEP_FSXI_4096 = 12 /*!< Frame Size for Proximity Card Integer with + 4096 bytes ISO14443-3 Amd2 2012 */ +} rfalIsoDepFSxI; + +/*! Frame Size for Proximity Card definitions */ +typedef enum { + RFAL_ISODEP_FSX_16 = 16, /*!< Frame Size for Proximity Card with 16 bytes */ + RFAL_ISODEP_FSX_24 = 24, /*!< Frame Size for Proximity Card with 24 bytes */ + RFAL_ISODEP_FSX_32 = 32, /*!< Frame Size for Proximity Card with 32 bytes */ + RFAL_ISODEP_FSX_40 = 40, /*!< Frame Size for Proximity Card with 40 bytes */ + RFAL_ISODEP_FSX_48 = 48, /*!< Frame Size for Proximity Card with 48 bytes */ + RFAL_ISODEP_FSX_64 = 64, /*!< Frame Size for Proximity Card with 64 bytes */ + RFAL_ISODEP_FSX_96 = 96, /*!< Frame Size for Proximity Card with 96 bytes */ + RFAL_ISODEP_FSX_128 = + 128, /*!< Frame Size for Proximity Card with 128 bytes */ + RFAL_ISODEP_FSX_256 = + 256, /*!< Frame Size for Proximity Card with 256 bytes */ + RFAL_ISODEP_FSX_512 = 512, /*!< Frame Size for Proximity Card with 512 bytes + ISO14443-3 Amd2 2012 */ + RFAL_ISODEP_FSX_1024 = 1024, /*!< Frame Size for Proximity Card with 1024 + bytes ISO14443-3 Amd2 2012 */ + RFAL_ISODEP_FSX_2048 = 2048, /*!< Frame Size for Proximity Card with 2048 + bytes ISO14443-3 Amd2 2012 */ + RFAL_ISODEP_FSX_4096 = 4096, /*!< Frame Size for Proximity Card with 4096 + bytes ISO14443-3 Amd2 2012 */ +} rfalIsoDepFSx; + +/* + ****************************************************************************** + * GLOBAL MACROS + ****************************************************************************** + */ + +/* + ****************************************************************************** + * GLOBAL DATA TYPES + ****************************************************************************** + */ + +/*! RATS format Digital 1.1 13.6.1 */ +typedef struct { + uint8_t CMD; /*!< RATS command byte: 0xE0 */ + uint8_t PARAM; /*!< Param indicating FSDI and DID */ +} rfalIsoDepRats; + +/*! ATS response format Digital 1.1 13.6.2 */ +typedef struct { + uint8_t TL; /*!< Length Byte, including TL byte itself */ + uint8_t T0; /*!< Format Byte T0 indicating if TA, TB, TC */ + uint8_t TA; /*!< Interface Byte TA(1) */ + uint8_t TB; /*!< Interface Byte TB(1) */ + uint8_t TC; /*!< Interface Byte TC(1) */ + uint8_t HB[RFAL_ISODEP_ATS_HB_MAX_LEN]; /*!< Historical Bytes */ +} rfalIsoDepAts; + +/*! PPS Request format (Protocol and Parameter Selection) ISO14443-4 5.3 */ +typedef struct { + uint8_t PPSS; /*!< Start Byte: [ 1101b | CID[4b] ] */ + uint8_t PPS0; /*!< Parameter 0:[ 000b | PPS1[1n] | 0001b ] */ + uint8_t PPS1; /*!< Parameter 1:[ 0000b | DSI[2b] | DRI[2b] ]*/ +} rfalIsoDepPpsReq; + +/*! PPS Response format (Protocol and Parameter Selection) ISO14443-4 5.4 */ +typedef struct { + uint8_t PPSS; /*!< Start Byte: [ 1101b | CID[4b] ] */ +} rfalIsoDepPpsRes; + +/*! ATTRIB Command Format Digital 1.1 15.6.1 */ +typedef struct { + uint8_t cmd; /*!< ATTRIB_REQ command byte */ + uint8_t + nfcid0[RFAL_NFCB_NFCID0_LEN]; /*!< NFCID0 of the card to be selected */ + struct { + uint8_t PARAM1; /*!< PARAM1 of ATTRIB command */ + uint8_t PARAM2; /*!< PARAM2 of ATTRIB command */ + uint8_t PARAM3; /*!< PARAM3 of ATTRIB command */ + uint8_t PARAM4; /*!< PARAM4 of ATTRIB command */ + } Param; /*!< Parameter of ATTRIB command */ + uint8_t + HLInfo[RFAL_ISODEP_ATTRIB_HLINFO_LEN]; /*!< Higher Layer Information */ +} rfalIsoDepAttribCmd; + +/*! ATTRIB Response Format Digital 1.1 15.6.2 */ +typedef struct { + uint8_t mbliDid; /*!< Contains MBLI and DID */ + uint8_t + HLInfo[RFAL_ISODEP_ATTRIB_HLINFO_LEN]; /*!< Higher Layer Information */ +} rfalIsoDepAttribRes; + +/*! S(Parameters) Command Format ISO14443-4 (2016) Table 4 */ +typedef struct { + uint8_t tag; /*!< S(PARAMETERS) Tag field */ + uint8_t length; /*!< S(PARAMETERS) Length field */ + uint8_t value[RFAL_ISODEP_SPARAM_VALUES_MAX_LEN]; /*!< S(PARAMETERS) Value + field */ +} rfalIsoDepSParameter; + +/*! Activation info as Poller and Listener for NFC-A and NFC-B */ +typedef union { /* PRQA S 0750 # MISRA 19.2 - Both members of the union will + not be used concurrently, device is only of type A or B at a + time. Thus no problem can occur. */ + + /*! NFC-A information */ + union { + struct { + rfalIsoDepAts ATS; /*!< ATS response (Poller mode) */ + uint8_t ATSLen; /*!< ATS response length (Poller mode) */ + } Listener; + struct { + rfalIsoDepRats RATS; /*!< RATS request (Listener mode) */ + } Poller; + } A; + + /*! NFC-B information */ + union { + struct { + rfalIsoDepAttribRes + ATTRIB_RES; /*!< ATTRIB_RES (Poller mode) */ + uint8_t ATTRIB_RESLen; /*!< ATTRIB_RES length (Poller mode) */ + } Listener; + struct { + rfalIsoDepAttribCmd ATTRIB; /*!< ATTRIB request (Listener mode) */ + uint8_t ATTRIBLen; /*!< ATTRIB request length (Listener mode) */ + } Poller; + } B; +} rfalIsoDepActivation; + +/*! ISO-DEP device Info */ +typedef struct { + uint8_t FWI; /*!< Frame Waiting Integer */ + uint32_t FWT; /*!< Frame Waiting Time (1/fc) */ + uint32_t dFWT; /*!< Delta Frame Waiting Time (1/fc) */ + uint32_t SFGI; /*!< Start-up Frame Guard time Integer */ + uint32_t SFGT; /*!< Start-up Frame Guard Time (ms) */ + uint8_t FSxI; /*!< Frame Size Device/Card Integer (FSDI or FSCI) */ + uint16_t FSx; /*!< Frame Size Device/Card (FSD or FSC) */ + uint32_t MBL; /*!< Maximum Buffer Length (optional for NFC-B) */ + rfalBitRate DSI; /*!< Bit Rate coding from Listener (PICC) to Poller (PCD) */ + rfalBitRate DRI; /*!< Bit Rate coding from Poller (PCD) to Listener (PICC) */ + uint8_t DID; /*!< Device ID */ + uint8_t NAD; /*!< Node ADdress */ + bool supDID; /*!< DID supported flag */ + bool supNAD; /*!< NAD supported flag */ + bool supAdFt; /*!< Advanced Features supported flag */ +} rfalIsoDepInfo; + +/*! ISO-DEP Device structure */ +typedef struct { + rfalIsoDepActivation activation; /*!< Activation Info */ + rfalIsoDepInfo info; /*!< ISO-DEP (ISO14443-4) device Info */ +} rfalIsoDepDevice; + +/*! ATTRIB Response parameters */ +typedef struct { + uint8_t mbli; /*!< MBLI */ + uint8_t HLInfo[RFAL_ISODEP_ATTRIB_HLINFO_LEN]; /*!< Hi Layer Information */ + uint8_t HLInfoLen; /*!< Hi Layer Information Length */ +} rfalIsoDepAttribResParam; + +/*! ATS Response parameter */ +typedef struct { + uint8_t fsci; /*!< Frame Size of Proximity Card Integer */ + uint8_t fwi; /*!< Frame Waiting Time Integer */ + uint8_t sfgi; /*!< Start-Up Frame Guard Time Integer */ + bool didSupport; /*!< DID Supported */ + uint8_t ta; /*!< Max supported bitrate both direction */ + uint8_t *hb; /*!< Historical Bytes data */ + uint8_t hbLen; /*!< Historical Bytes Length */ +} rfalIsoDepAtsParam; + +/*! Structure of I-Block Buffer format from caller */ +typedef struct { + uint8_t prologue[RFAL_ISODEP_PROLOGUE_SIZE]; /*!< Prologue/SoD buffer */ + uint8_t inf[RFAL_FEATURE_ISO_DEP_IBLOCK_MAX_LEN]; /*!< INF/Payload buffer */ +} rfalIsoDepBufFormat; + +/*! Structure of APDU Buffer format from caller */ +typedef struct { + uint8_t prologue[RFAL_ISODEP_PROLOGUE_SIZE]; /*!< Prologue/SoD buffer */ + uint8_t apdu[RFAL_FEATURE_ISO_DEP_APDU_MAX_LEN]; /*!< APDU/Payload buffer */ +} rfalIsoDepApduBufFormat; + +/*! Listen Activation Parameters Structure */ +typedef struct { + rfalIsoDepBufFormat *rxBuf; /*!< Receive Buffer struct reference */ + uint16_t *rxLen; /*!< Received INF data length in Bytes */ + bool *isRxChaining; /*!< Received data is not complete */ + rfalIsoDepDevice *isoDepDev; /*!< ISO-DEP device info */ +} rfalIsoDepListenActvParam; + +/*! Structure of parameters used on ISO DEP Transceive */ +typedef struct { + rfalIsoDepBufFormat *txBuf; /*!< Transmit Buffer struct reference */ + uint16_t txBufLen; /*!< Transmit Buffer INF field length in Bytes*/ + bool isTxChaining; /*!< Transmit data is not complete */ + rfalIsoDepBufFormat *rxBuf; /*!< Receive Buffer struct reference in Bytes */ + uint16_t *rxLen; /*!< Received INF data length in Bytes */ + bool *isRxChaining; /*!< Received data is not complete */ + uint32_t FWT; /*!< FWT to be used (ignored in Listen Mode) */ + uint32_t dFWT; /*!< Delta FWT to be used */ + uint16_t ourFSx; /*!< Our device Frame Size (FSD or FSC) */ + uint16_t FSx; /*!< Other device Frame Size (FSD or FSC) */ + uint8_t DID; /*!< Device ID (RFAL_ISODEP_NO_DID if no DID) */ +} rfalIsoDepTxRxParam; + +/*! Structure of parameters used on ISO DEP APDU Transceive */ +typedef struct { + rfalIsoDepApduBufFormat *txBuf; /*!< Transmit Buffer struct reference */ + uint16_t txBufLen; /*!< Transmit Buffer INF field length in Bytes*/ + rfalIsoDepApduBufFormat + *rxBuf; /*!< Receive Buffer struct reference in Bytes */ + uint16_t *rxLen; /*!< Received INF data length in Bytes */ + rfalIsoDepBufFormat *tmpBuf; /*!< Temp buffer for Rx I-Blocks (internal) */ + uint32_t FWT; /*!< FWT to be used (ignored in Listen Mode) */ + uint32_t dFWT; /*!< Delta FWT to be used */ + uint16_t FSx; /*!< Other device Frame Size (FSD or FSC) */ + uint16_t ourFSx; /*!< Our device Frame Size (FSD or FSC) */ + uint8_t DID; /*!< Device ID (RFAL_ISODEP_NO_DID if no DID) */ +} rfalIsoDepApduTxRxParam; + +/* + ****************************************************************************** + * GLOBAL FUNCTION PROTOTYPES + ****************************************************************************** + */ + +/*! + ****************************************************************************** + * \brief Initialize the ISO-DEP protocol + * + * Initialize the ISO-DEP protocol layer with default config + ****************************************************************************** + */ +void rfalIsoDepInitialize(void); + +/*! + ****************************************************************************** + * \brief Initialize the ISO-DEP protocol + * + * Initialize the ISO-DEP protocol layer with additional parameters allowing + * to customise the protocol layer for specific behaviours + * + + * \param[in] compMode : Compliance mode to be performed + * \param[in] maxRetriesR : Number of retries for a R-Block + * Digital 2.0 B9 - nRETRY ACK/NAK: [2,5] + * \param[in] maxRetriesSnWTX : Number of retries for a S(WTX) (only in case + * of NAKs) Digital 2.0 B9 - nRETRY WTX[2,5] + * \param[in] maxRetriesSWTX : Number of overall S(WTX) retries. + * Use RFAL_ISODEP_MAX_WTX_RETRYS_ULTD for + disabling + * this limit check Digital 2.0 16.2.5.2 + * \param[in] maxRetriesSDSL : Number of retries for a S(DESELECT) + * Digital 2.0 B9 - nRETRY DESELECT: [0,5] + * \param[in] maxRetriesI : Number of retries for a I-Block + * Digital 2.0 16.2.5.4 + * \param[in] maxRetriesRATS : Number of retries for RATS + * Digital 2.0 B7 - nRETRY RATS [0,1] + * + ****************************************************************************** + */ +void rfalIsoDepInitializeWithParams(rfalComplianceMode compMode, + uint8_t maxRetriesR, + uint8_t maxRetriesSnWTX, + uint8_t maxRetriesSWTX, + uint8_t maxRetriesSDSL, uint8_t maxRetriesI, + uint8_t maxRetriesRATS); + +/*! + ***************************************************************************** + * \brief FSxI to FSx + * + * Convert Frame Size for proximity coupling Device Integer (FSxI) to + * Frame Size for proximity coupling Device (FSx) + * + * FSD - maximum frame size for NFC Forum Device in Poll Mode + * FSC - maximum frame size for NFC Forum Device in Listen Mode + * + * FSxI = FSDI or FSCI + * FSx = FSD or FSC + * + * The FSD/FSC value includes the header and CRC + * + * \param[in] FSxI : Frame Size for proximity coupling Device Integer + * + * \return fsx : Frame Size for proximity coupling Device (FSD or FSC) + * + ***************************************************************************** + */ +uint16_t rfalIsoDepFSxI2FSx(uint8_t FSxI); + +/*! + ***************************************************************************** + * \brief FWI to FWT + * + * Convert Frame Waiting time Integer (FWI) to Frame Waiting Time (FWT) in + * 1/fc units + * + * \param[in] fwi : Frame Waiting time Integer + * + * \return fwt : Frame Waiting Time in 1/fc units + * + ***************************************************************************** + */ +uint32_t rfalIsoDepFWI2FWT(uint8_t fwi); + +/*! + ***************************************************************************** + * \brief Check if the buffer data contains a valid RATS command + * + * Check if it is a well formed RATS command with 2 bytes + * This function does not check the validity of FSDI and DID + * + * \param[in] buf : reference to buffer containing the data to be checked + * \param[in] bufLen : length of data in the buffer in bytes + * + * \return true if the data indicates a RATS command; false otherwise + ***************************************************************************** + */ +bool rfalIsoDepIsRats(const uint8_t *buf, uint8_t bufLen); + +/*! + ***************************************************************************** + * \brief Check if the buffer data contains a valid ATTRIB command + * + * Check if it is a well formed ATTRIB command, but does not check the + * validity of the information inside + * + * \param[in] buf : reference to buffer containing the data to be checked + * \param[in] bufLen : length of data in the buffer in bytes + * + * \return true if the data indicates a ATTRIB command; false otherwise + ***************************************************************************** + */ +bool rfalIsoDepIsAttrib(const uint8_t *buf, uint8_t bufLen); + +/*! + ***************************************************************************** + * \brief Start Listen Activation Handling + * + * Start Listen Activation Handling and setup to receive first I-block which may + * contain complete or partial APDU after activation is completed + * + * Pass in RATS for T4AT, or ATTRIB for T4BT, to handle ATS or ATTRIB Response + *respectively The Activation Handling handles ATS and ATTRIB Response; and + *additionally PPS Response if a PPS is received for T4AT. The method uses the + *current RFAL state machine to determine if it is expecting RATS or ATTRIB + * + * Activation is completed if PPS Response is sent or if first PDU is received + *in T4T-A Activation is completed if ATTRIB Response is sent in T4T-B + * + * \ref rfalIsoDepListenGetActivationStatus provide status if activation is + *completed. \ref rfalIsoDepStartTransceive shall be called right after + *activation is completed + * + * \param[in] atsParam : reference to ATS parameters + * \param[in] attribResParam : reference to ATTRIB_RES parameters + * \param[in] buf : reference to buffer containing RATS or ATTRIB + * \param[in] bufLen : length in bytes of the given bufffer + * \param[in] actParam : reference to incoming reception information will + *be placed + * + * + * \warning Once the Activation has been completed the method + * rfalIsoDepGetTransceiveStatus() must be called. + * If activation has completed due to reception of a data block (not PPS) the + * buffer owned by the caller and passed on actParam must still contain this + *data. The first data will be processed (I-Block or S-DSL) by + *rfalIsoDepGetTransceiveStatus() inform the caller and then for the next + *transaction use rfalIsoDepStartTransceive() + * + * \return RFAL_ERR_NONE : RATS/ATTRIB is valid and activation has started + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_PROTO : Invalid request + * \return RFAL_ERR_NOTSUPP : Feature not supported + ***************************************************************************** + */ +ReturnCode rfalIsoDepListenStartActivation( + rfalIsoDepAtsParam *atsParam, + const rfalIsoDepAttribResParam *attribResParam, const uint8_t *buf, + uint16_t bufLen, rfalIsoDepListenActvParam actParam); + +/*! + ***************************************************************************** + * \brief Get the current Activation Status + * + * \return RFAL_ERR_NONE if Activation is already completed + * \return RFAL_ERR_BUSY if Activation is ongoing + * \return RFAL_ERR_LINK_LOSS if Remote Field is turned off + ***************************************************************************** + */ +ReturnCode rfalIsoDepListenGetActivationStatus(void); + +/*! + ***************************************************************************** + * \brief Get the ISO-DEP Communication Information + * + * Gets the maximum INF length in bytes based on current Frame Size + * for proximity coupling Device (FSD or FSC) excluding the header and CRC + * + * \return maximum INF length in bytes + ***************************************************************************** + */ +uint16_t rfalIsoDepGetMaxInfLen(void); + +/*! + ***************************************************************************** + * \brief ISO-DEP Start Transceive + * + * This method triggers a ISO-DEP Transceive containing a complete or + * partial APDU + * It transmits the given message and handles all protocol retransmitions, + * error handling and control messages + * + * The txBuf contains a complete or partial APDU (INF) to be transmitted + * The Prologue field will be manipulated by the Transceive + * + * If the buffer contains a partial APDU and is not the last block, + * then isTxChaining must be set to true + * + * \param[in] param: reference parameters to be used for the Transceive + * + * \return RFAL_ERR_PARAM : Bad request + * \return RFAL_ERR_WRONG_STATE : The module is not in a proper state + * \return RFAL_ERR_NONE : The Transceive request has been started + ***************************************************************************** + */ +ReturnCode rfalIsoDepStartTransceive(rfalIsoDepTxRxParam param); + +/*! + ***************************************************************************** + * \brief Get the Transceive status + * + * Returns the status of the ISO-DEP Transceive + * + * \warning When the other device is performing chaining once a chained + * block is received the error RFAL_ERR_AGAIN is sent. At this point + * caller must handle the received data immediately. + * When RFAL_ERR_AGAIN is returned an ACK has already been sent to + * the other device and the next block might be incoming. + * If rfalWorker() is called frequently it will place the next + * block on the given buffer + * + * + * \return RFAL_ERR_NONE : Transceive has been completed successfully + * \return RFAL_ERR_BUSY : Transceive is ongoing + * \return RFAL_ERR_PROTO : Protocol error occurred + * \return RFAL_ERR_TIMEOUT : Timeout error occurred + * \return RFAL_ERR_SLEEP_REQ : Deselect has been received and responded + * \return RFAL_ERR_NOMEM : The received INF does not fit into the + * receive buffer + * \return RFAL_ERR_LINK_LOSS : Communication is lost because Reader/Writer + * has turned off its field + * \return RFAL_ERR_AGAIN : received one chaining block, continue to call + * this method to retrieve the remaining blocks + ***************************************************************************** + */ +ReturnCode rfalIsoDepGetTransceiveStatus(void); + +/*! + ***************************************************************************** + * \brief ISO-DEP Start APDU Transceive + * + * This method triggers a ISO-DEP Transceive containing a complete APDU + * It transmits the given message and handles all protocol retransmitions, + * error handling and control messages + * + * The txBuf contains a complete APDU to be transmitted + * The Prologue field will be manipulated by the Transceive + * + * \warning the txBuf will be modified during the transmission + * \warning the maximum RF frame which can be received is limited by + *param.tmpBuf + * + * \param[in] param: reference parameters to be used for the Transceive + * + * \return RFAL_ERR_PARAM : Bad request + * \return RFAL_ERR_WRONG_STATE : The module is not in a proper state + * \return RFAL_ERR_NONE : The Transceive request has been started + ***************************************************************************** + */ +ReturnCode rfalIsoDepStartApduTransceive(rfalIsoDepApduTxRxParam param); + +/*! + ***************************************************************************** + * \brief Get the APDU Transceive status + * + * \return RFAL_ERR_NONE : if Transceive has been completed successfully + * \return RFAL_ERR_BUSY : if Transceive is ongoing + * \return RFAL_ERR_PROTO : if a protocol error occurred + * \return RFAL_ERR_TIMEOUT : if a timeout error occurred + * \return RFAL_ERR_SLEEP_REQ : if Deselect is received and responded + * \return RFAL_ERR_NOMEM : if the received INF does not fit into the + * receive buffer + * \return RFAL_ERR_LINK_LOSS : if communication is lost because Reader/Writer + * has turned off its field + ***************************************************************************** + */ +ReturnCode rfalIsoDepGetApduTransceiveStatus(void); + +/*! + ***************************************************************************** + * \brief ISO-DEP Send RATS + * + * This sends a RATS to make a NFC-A Listen Device to enter + * ISO-DEP layer (ISO14443-4) and checks if the received ATS is valid + * + * \param[in] FSDI : Frame Size Device Integer to be used + * \param[in] DID : Device ID to be used or RFAL_ISODEP_NO_DID for not use + *DID \param[out] ats : pointer to place the ATS Response \param[out] atsLen + *: pointer to place the ATS length + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_TIMEOUT : Timeout error + * \return RFAL_ERR_PAR : Parity error detected + * \return RFAL_ERR_CRC : CRC error detected + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_NONE : No error, ATS received + ***************************************************************************** + */ +ReturnCode rfalIsoDepRATS(rfalIsoDepFSxI FSDI, uint8_t DID, rfalIsoDepAts *ats, + uint8_t *atsLen); + +/*! + ***************************************************************************** + * \brief ISO-DEP Send PPS + * + * This sends a PPS to make a NFC-A Listen Device change the communications + * bit rate from 106kbps to one of the supported bit rates + * Additionally checks if the received PPS response is valid + * + * \param[in] DID : Device ID + * \param[in] DSI : DSI code the divisor from Listener (PICC) to Poller + *(PCD) \param[in] DRI : DRI code the divisor from Poller (PCD) to Listener + *(PICC) \param[out] ppsRes : pointer to place the PPS Response + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_TIMEOUT : Timeout error + * \return RFAL_ERR_PAR : Parity error detected + * \return RFAL_ERR_CRC : CRC error detected + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_NONE : No error, PPS Response received + ***************************************************************************** + */ +ReturnCode rfalIsoDepPPS(uint8_t DID, rfalBitRate DSI, rfalBitRate DRI, + rfalIsoDepPpsRes *ppsRes); + +/*! + ***************************************************************************** + * \brief ISO-DEP Send ATTRIB + * + * This sends a ATTRIB to make a NFC-B Listen Device to enter + * ISO-DEP layer (ISO14443-4) and checks if the received ATTRIB Response is + *valid + * + * \param[in] nfcid0 : NFCID0 to be used for the ATTRIB + * \param[in] PARAM1 : ATTRIB PARAM1 byte (communication parameters) + * \param[in] DSI : DSI code the divisor from Listener (PICC) to Poller + *(PCD) \param[in] DRI : DRI code the divisor from Poller (PCD) to + *Listener (PICC) \param[in] FSDI : PCD's Frame Size to be announced on + *the ATTRIB \param[in] PARAM3 : ATTRIB PARAM1 byte (protocol type) + * \param[in] DID : Device ID to be used or RFAL_ISODEP_NO_DID for not + *use DID \param[in] HLInfo : pointer to Higher layer INF (NULL if none) + * \param[in] HLInfoLen : Length HLInfo + * \param[in] fwt : Frame Waiting Time to be used (from SENSB_RES) + * \param[out] attribRes : pointer to place the ATTRIB Response + * \param[out] attribResLen : pointer to place the ATTRIB Response length + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_TIMEOUT : Timeout error + * \return RFAL_ERR_CRC : CRC error detected + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_NONE : No error, ATTRIB Response received + ***************************************************************************** + */ +ReturnCode rfalIsoDepATTRIB(const uint8_t *nfcid0, uint8_t PARAM1, + rfalBitRate DSI, rfalBitRate DRI, + rfalIsoDepFSxI FSDI, uint8_t PARAM3, uint8_t DID, + const uint8_t *HLInfo, uint8_t HLInfoLen, + uint32_t fwt, rfalIsoDepAttribRes *attribRes, + uint8_t *attribResLen); + +/*! + ***************************************************************************** + * \brief Deselect PICC + * + * This function sends a deselect command to PICC and waits for its + * responce in a blocking way + * + * \return RFAL_ERR_NONE : Deselect successfully sent and acknowledged by + *PICC \return RFAL_ERR_PROTO : Protocol error occurred \return + *RFAL_ERR_TIMEOUT : No response rcvd from PICC + * + ***************************************************************************** + */ +ReturnCode rfalIsoDepDeselect(void); + +/*! + ***************************************************************************** + * \brief Start Deselect + * + * This function starts the exchange to send deselect command to PICC and + * waits for its response + * + * \return RFAL_ERR_NONE : Deselect successfully sent and acknowledged by + *PICC \return RFAL_ERR_PROTO : Protocol error occurred \return + *RFAL_ERR_TIMEOUT : No response rcvd from PICC + * + ***************************************************************************** + */ +ReturnCode rfalIsoDepStartDeselect(void); + +/*! + ***************************************************************************** + * \brief Deselect Get Status + * + * This function sends a deselect command to PICC and waits for it`s + * responce in a blocking way + * + * \return RFAL_ERR_NONE : Deselect successfully sent and acknowledged by + *PICC \return RFAL_ERR_PROTO : Protocol error occurred \return + *RFAL_ERR_TIMEOUT : No response rcvd from PICC + * + ***************************************************************************** + */ +ReturnCode rfalIsoDepGetDeselectStatus(void); + +/*! + ***************************************************************************** + * \brief ISO-DEP Poller Handle NFC-A Activation + * + * This performs a NFC-A Activation into ISO-DEP layer (ISO14443-4) with the + *given parameters. It sends RATS and if the higher bit rates are supported by + * both devices it additionally sends PPS + * Once Activated all details of the device are provided on isoDepDev + * + * \param[in] FSDI : Frame Size Device Integer to be used + * \param[in] DID : Device ID to be used or RFAL_ISODEP_NO_DID for + *not use DID \param[in] maxBR : Max bit rate supported by the Poller + * \param[out] rfalIsoDepDev : ISO-DEP information of the activated Listen + *device + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_TIMEOUT : Timeout error + * \return RFAL_ERR_PAR : Parity error detected + * \return RFAL_ERR_CRC : CRC error detected + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_NONE : No error, activation successful + ***************************************************************************** + */ +ReturnCode rfalIsoDepPollAHandleActivation(rfalIsoDepFSxI FSDI, uint8_t DID, + rfalBitRate maxBR, + rfalIsoDepDevice *rfalIsoDepDev); + +/*! + ***************************************************************************** + * \brief ISO-DEP Poller Handle NFC-B Activation + * + * This performs a NFC-B Activation into ISO-DEP layer (ISO14443-4) with the + *given parameters. It sends ATTRIB and calculates supported higher bit rates of + *both devices and performs activation. Once Activated all details of the device + *are provided on isoDepDev + * + * \param[in] FSDI : Frame Size Device Integer to be used + * \param[in] DID : Device ID to be used or RFAL_ISODEP_NO_DID for + *not use DID \param[in] maxBR : Max bit rate supported by the Poller + * \param[in] PARAM1 : ATTRIB PARAM1 byte (communication parameters) + * \param[in] nfcbDev : pointer to the NFC-B Device containing the + *SENSB_RES \param[in] HLInfo : pointer to Higher layer INF (NULL if + *none) \param[in] HLInfoLen : Length HLInfo \param[out] rfalIsoDepDev : + *ISO-DEP information of the activated Listen device + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_TIMEOUT : Timeout error + * \return RFAL_ERR_PAR : Parity error detected + * \return RFAL_ERR_CRC : CRC error detected + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_NONE : No error, activation successful + ***************************************************************************** + */ +ReturnCode rfalIsoDepPollBHandleActivation(rfalIsoDepFSxI FSDI, uint8_t DID, + rfalBitRate maxBR, uint8_t PARAM1, + const rfalNfcbListenDevice *nfcbDev, + const uint8_t *HLInfo, + uint8_t HLInfoLen, + rfalIsoDepDevice *rfalIsoDepDev); + +/*! + ***************************************************************************** + * \brief ISO-DEP Poller Handle S(Parameters) + * + * This checks if PICC supports S(PARAMETERS), retieves PICC's + * capabilities and sets the Bit Rate at the highest supported by both + * devices + * + * \param[out] rfalIsoDepDev : ISO-DEP information of the activated Listen + *device \param[in] maxTxBR : Maximum Tx bit rate supported by PCD + * \param[in] maxRxBR : Maximum Rx bit rate supported by PCD + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_TIMEOUT : Timeout error + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_NONE : No error, S(PARAMETERS) selection successful + ***************************************************************************** + */ +ReturnCode rfalIsoDepPollHandleSParameters(rfalIsoDepDevice *rfalIsoDepDev, + rfalBitRate maxTxBR, + rfalBitRate maxRxBR); + +/*! + ***************************************************************************** + * \brief ISO-DEP Poller Start NFC-A Activation + * + * This starts a NFC-A Activation into ISO-DEP layer (ISO14443-4) with the + *given parameters. It sends RATS and if the higher bit rates are supported by + * both devices it additionally sends PPS + * Once Activated all details of the device are provided on isoDepDev + * + * + * \see rfalIsoDepPollAGetActivationStatus + * + * \param[in] FSDI : Frame Size Device Integer to be used + * \param[in] DID : Device ID to be used or RFAL_ISODEP_NO_DID for + *not use DID \param[in] maxBR : Max bit rate supported by the Poller + * \param[out] rfalIsoDepDev : ISO-DEP information of the activated Listen + *device + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_TIMEOUT : Timeout error + * \return RFAL_ERR_PAR : Parity error detected + * \return RFAL_ERR_CRC : CRC error detected + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_NONE : No error, start of asynchronous operation + *successful + ***************************************************************************** + */ +ReturnCode rfalIsoDepPollAStartActivation(rfalIsoDepFSxI FSDI, uint8_t DID, + rfalBitRate maxBR, + rfalIsoDepDevice *rfalIsoDepDev); + +/*! + ***************************************************************************** + * \brief ISO-DEP Poller Get NFC-A Activation Status + * + * Returns the activation status started by rfalIsoDepPollAStartActivation + * + * \see rfalIsoDepPollAStartActivation + * + * \return RFAL_ERR_BUSY : Operation is ongoing + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_TIMEOUT : Timeout error + * \return RFAL_ERR_PAR : Parity error detected + * \return RFAL_ERR_CRC : CRC error detected + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_NONE : No error, activation successful + ***************************************************************************** + */ +ReturnCode rfalIsoDepPollAGetActivationStatus(void); + +/*! + ***************************************************************************** + * \brief ISO-DEP Poller Start NFC-B Activation + * + * This starts a NFC-B Activation into ISO-DEP layer (ISO14443-4) with the + *given parameters. It will send ATTRIB and calculate supported higher bit rates + *of both devices and perform activation. Once Activated all details of the + *device are provided on isoDepDev + * + * \see rfalIsoDepPollBGetActivationStatus + * + * \param[in] FSDI : Frame Size Device Integer to be used + * \param[in] DID : Device ID to be used or RFAL_ISODEP_NO_DID for + *not use DID \param[in] maxBR : Max bit rate supported by the Poller + * \param[in] PARAM1 : ATTRIB PARAM1 byte (communication parameters) + * \param[in] nfcbDev : pointer to the NFC-B Device containing the + *SENSB_RES \param[in] HLInfo : pointer to Higher layer INF (NULL if + *none) \param[in] HLInfoLen : Length HLInfo \param[out] rfalIsoDepDev : + *ISO-DEP information of the activated Listen device + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_TIMEOUT : Timeout error + * \return RFAL_ERR_PAR : Parity error detected + * \return RFAL_ERR_CRC : CRC error detected + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_NONE : No error, start of asynchronous operation + *successful + ***************************************************************************** + */ +ReturnCode rfalIsoDepPollBStartActivation(rfalIsoDepFSxI FSDI, uint8_t DID, + rfalBitRate maxBR, uint8_t PARAM1, + const rfalNfcbListenDevice *nfcbDev, + const uint8_t *HLInfo, + uint8_t HLInfoLen, + rfalIsoDepDevice *rfalIsoDepDev); + +/*! + ***************************************************************************** + * \brief ISO-DEP Poller Get NFC-B Activation Status + * + * Returns the activation status started by rfalIsoDepPollBStartActivation + * + * \see rfalIsoDepPollBStartActivation + * + * \return RFAL_ERR_BUSY : Operation is ongoing + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_TIMEOUT : Timeout error + * \return RFAL_ERR_PAR : Parity error detected + * \return RFAL_ERR_CRC : CRC error detected + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_NONE : No error, activation successful + ***************************************************************************** + */ +ReturnCode rfalIsoDepPollBGetActivationStatus(void); + +#endif /* RFAL_ISODEP_H_ */ + +/** + * @} + * + * @} + * + * @} + */ diff --git a/core/embed/io/nfc/rfal/include/rfal_nfc.h b/core/embed/io/nfc/rfal/include/rfal_nfc.h new file mode 100644 index 0000000000..5613bbeb15 --- /dev/null +++ b/core/embed/io/nfc/rfal/include/rfal_nfc.h @@ -0,0 +1,500 @@ + +/****************************************************************************** + * @attention + * + * COPYRIGHT 2016 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/* + * PROJECT: ST25R391x firmware + * Revision: + * LANGUAGE: ISO C99 + */ + +/*! \file rfal_nfc.h + * + * \brief RFAL NFC device + * + * This module provides the required features to behave as an NFC Poller + * or Listener device. It grants an easy to use interface for the following + * activities: Technology Detection, Collision Resolution, Activation, + * Data Exchange, and Deactivation + * + * This layer is influenced by (but not fully aligned with) the NFC Forum + * specifications, in particular: Activity 2.0 and NCI 2.0 + * + * + * + * \addtogroup RFAL + * @{ + * + * \addtogroup RFAL-HL + * \brief RFAL Higher Layer + * @{ + * + * \addtogroup NFC + * \brief RFAL NFC Device + * @{ + * + */ + +#ifndef RFAL_NFC_H +#define RFAL_NFC_H + +/* +****************************************************************************** +* INCLUDES +****************************************************************************** +*/ +#include "rfal_isoDep.h" +#include "rfal_nfcDep.h" +#include "rfal_nfca.h" +#include "rfal_nfcb.h" +#include "rfal_nfcf.h" +#include "rfal_nfcv.h" +#include "rfal_platform.h" +#include "rfal_rf.h" +#include "rfal_st25tb.h" +#include "rfal_utils.h" + +/* +****************************************************************************** +* GLOBAL DEFINES +****************************************************************************** +*/ + +#define RFAL_NFC_TECH_NONE 0x0000U /*!< No technology */ +#define RFAL_NFC_POLL_TECH_A 0x0001U /*!< Poll NFC-A technology Flag */ +#define RFAL_NFC_POLL_TECH_B 0x0002U /*!< Poll NFC-B technology Flag */ +#define RFAL_NFC_POLL_TECH_F 0x0004U /*!< Poll NFC-F technology Flag */ +#define RFAL_NFC_POLL_TECH_V 0x0008U /*!< Poll NFC-V technology Flag */ +#define RFAL_NFC_POLL_TECH_AP2P \ + 0x0010U /*!< Poll AP2P technology Flag */ +#define RFAL_NFC_POLL_TECH_ST25TB \ + 0x0020U /*!< Poll ST25TB technology Flag */ +#define RFAL_NFC_POLL_TECH_PROP \ + 0x0040U /*!< Poll Proprietary technology Flag */ +#define RFAL_NFC_LISTEN_TECH_A 0x1000U /*!< Listen NFC-A technology Flag */ +#define RFAL_NFC_LISTEN_TECH_B 0x2000U /*!< Listen NFC-B technology Flag */ +#define RFAL_NFC_LISTEN_TECH_F 0x4000U /*!< Listen NFC-F technology Flag */ +#define RFAL_NFC_LISTEN_TECH_AP2P \ + 0x8000U /*!< Listen AP2P technology Flag */ + +/* +****************************************************************************** +* GLOBAL MACROS +****************************************************************************** +*/ + +/*! Checks if a device is currently activated */ +#define rfalNfcIsDevActivated(st) \ + (((st) >= RFAL_NFC_STATE_ACTIVATED) && ((st) < RFAL_NFC_STATE_DEACTIVATION)) + +/*! Checks if a device is in discovery */ +#define rfalNfcIsInDiscovery(st) \ + (((st) >= RFAL_NFC_STATE_START_DISCOVERY) && \ + ((st) < RFAL_NFC_STATE_ACTIVATED)) + +/*! Checks if remote device is in Poll mode */ +#define rfalNfcIsRemDevPoller(tp) \ + (((tp) >= RFAL_NFC_POLL_TYPE_NFCA) && ((tp) <= RFAL_NFC_POLL_TYPE_AP2P)) + +/*! Checks if remote device is in Listen mode */ +#define rfalNfcIsRemDevListener(tp) \ + (((int16_t)(tp) >= (int16_t)RFAL_NFC_LISTEN_TYPE_NFCA) && \ + ((tp) <= RFAL_NFC_LISTEN_TYPE_AP2P)) + +/*! Sets the discover parameters to its default values */ +#define rfalNfcDefaultDiscParams(dp) \ + if ((dp) != NULL) { \ + RFAL_MEMSET((dp), 0x00, sizeof(rfalNfcDiscoverParam)); \ + ((dp))->compMode = RFAL_COMPLIANCE_MODE_NFC; \ + ((dp))->devLimit = 1U; \ + ((dp))->nfcfBR = RFAL_BR_212; \ + ((dp))->ap2pBR = RFAL_BR_424; \ + ((dp))->maxBR = RFAL_BR_KEEP; \ + ((dp))->isoDepFS = RFAL_ISODEP_FSXI_256; \ + ((dp))->nfcDepLR = RFAL_NFCDEP_LR_254; \ + ((dp))->GBLen = 0U; \ + ((dp))->p2pNfcaPrio = false; \ + ((dp))->wakeupEnabled = false; \ + ((dp))->wakeupConfigDefault = true; \ + ((dp))->wakeupPollBefore = false; \ + ((dp))->wakeupNPolls = 1U; \ + ((dp))->totalDuration = 1000U; \ + ((dp))->techs2Find = RFAL_NFC_TECH_NONE; \ + ((dp))->techs2Bail = RFAL_NFC_TECH_NONE; \ + } + +/* +****************************************************************************** +* GLOBAL ENUMS +****************************************************************************** +*/ + +/* +****************************************************************************** +* GLOBAL TYPES +****************************************************************************** +*/ + +/*! Main state */ +typedef enum { + RFAL_NFC_STATE_NOTINIT = 0, /*!< Not Initialized state */ + RFAL_NFC_STATE_IDLE = 1, /*!< Initialize state */ + RFAL_NFC_STATE_START_DISCOVERY = 2, /*!< Start Discovery loop state */ + RFAL_NFC_STATE_WAKEUP_MODE = 3, /*!< Wake-Up state */ + RFAL_NFC_STATE_POLL_TECHDETECT = 10, /*!< Technology Detection state */ + RFAL_NFC_STATE_POLL_COLAVOIDANCE = 11, /*!< Collision Avoidance state */ + RFAL_NFC_STATE_POLL_SELECT = 12, /*!< Wait for Selection state */ + RFAL_NFC_STATE_POLL_ACTIVATION = 13, /*!< Activation state */ + RFAL_NFC_STATE_LISTEN_TECHDETECT = 20, /*!< Listen Tech Detect */ + RFAL_NFC_STATE_LISTEN_COLAVOIDANCE = 21, /*!< Listen Collision Avoidance */ + RFAL_NFC_STATE_LISTEN_ACTIVATION = 22, /*!< Listen Activation state */ + RFAL_NFC_STATE_LISTEN_SLEEP = 23, /*!< Listen Sleep state */ + RFAL_NFC_STATE_ACTIVATED = 30, /*!< Activated state */ + RFAL_NFC_STATE_DATAEXCHANGE = 31, /*!< Data Exchange Start state */ + RFAL_NFC_STATE_DATAEXCHANGE_DONE = 33, /*!< Data Exchange terminated */ + RFAL_NFC_STATE_DEACTIVATION = 34 /*!< Deactivation state */ +} rfalNfcState; + +/*! Device type */ +typedef enum { + RFAL_NFC_LISTEN_TYPE_NFCA = 0, /*!< NFC-A Listener device type */ + RFAL_NFC_LISTEN_TYPE_NFCB = 1, /*!< NFC-B Listener device type */ + RFAL_NFC_LISTEN_TYPE_NFCF = 2, /*!< NFC-F Listener device type */ + RFAL_NFC_LISTEN_TYPE_NFCV = 3, /*!< NFC-V Listener device type */ + RFAL_NFC_LISTEN_TYPE_ST25TB = 4, /*!< ST25TB Listener device type */ + RFAL_NFC_LISTEN_TYPE_AP2P = 5, /*!< AP2P Listener device type */ + RFAL_NFC_LISTEN_TYPE_PROP = 6, /*!< Proprietary Listen dev type */ + RFAL_NFC_POLL_TYPE_NFCA = 10, /*!< NFC-A Poller device type */ + RFAL_NFC_POLL_TYPE_NFCB = 11, /*!< NFC-B Poller device type */ + RFAL_NFC_POLL_TYPE_NFCF = 12, /*!< NFC-F Poller device type */ + RFAL_NFC_POLL_TYPE_NFCV = 13, /*!< NFC-V Poller device type */ + RFAL_NFC_POLL_TYPE_AP2P = 15 /*!< AP2P Poller device type */ +} rfalNfcDevType; + +/*! Device interface */ +typedef enum { + RFAL_NFC_INTERFACE_RF = 0, /*!< RF Frame interface */ + RFAL_NFC_INTERFACE_ISODEP = 1, /*!< ISO-DEP interface */ + RFAL_NFC_INTERFACE_NFCDEP = 2 /*!< NFC-DEP interface */ +} rfalNfcRfInterface; + +/*! Deactivation type */ +typedef enum { + RFAL_NFC_DEACTIVATE_IDLE = 0, /*!< Deactivate and go to IDLE */ + RFAL_NFC_DEACTIVATE_SLEEP = 1, /*!< Deactivate and go to SELECT */ + RFAL_NFC_DEACTIVATE_DISCOVERY = 2 /*!< Deactivate and restart DISCOVERY */ +} rfalNfcDeactivateType; + +/*! Device struct containing all its details */ +typedef struct { + rfalNfcDevType type; /*!< Device's type */ + union { /* PRQA S 0750 # MISRA 19.2 - Members of the union will not be used + concurrently, only one technology at a time */ + rfalNfcaListenDevice nfca; /*!< NFC-A Listen Device instance */ + rfalNfcbListenDevice nfcb; /*!< NFC-B Listen Device instance */ + rfalNfcfListenDevice nfcf; /*!< NFC-F Listen Device instance */ + rfalNfcvListenDevice nfcv; /*!< NFC-V Listen Device instance */ + rfalSt25tbListenDevice st25tb; /*!< ST25TB Listen Device instance*/ + } dev; /*!< Device's instance */ + + uint8_t *nfcid; /*!< Device's NFCID */ + uint8_t nfcidLen; /*!< Device's NFCID length */ + rfalNfcRfInterface rfInterface; /*!< Device's interface */ + + union { /* PRQA S 0750 # MISRA 19.2 - Members of the union will not be used + concurrently, only one protocol at a time */ + rfalIsoDepDevice isoDep; /*!< ISO-DEP instance */ + rfalNfcDepDevice nfcDep; /*!< NFC-DEP instance */ + } proto; /*!< Device's protocol */ +} rfalNfcDevice; + +/*! Callbacks for Proprietary|Other Technology Activity 2.1 & + * EMVCo 3.0 9.2 */ +typedef ReturnCode (*rfalNfcPropCallback)(void); + +/*! Struct that holds the Proprietary NFC callbacks */ +typedef struct { + rfalNfcPropCallback + rfalNfcpPollerInitialize; /*!< Prorietary NFC Initialization callback */ + rfalNfcPropCallback + rfalNfcpPollerTechnologyDetection; /*!< Prorietary NFC Technoly Detection + callback */ + rfalNfcPropCallback + rfalNfcpPollerStartCollisionResolution; /*!< Prorietary NFC Start + Collision Resolution callback + */ + rfalNfcPropCallback + rfalNfcpPollerGetCollisionResolutionStatus; /*!< Prorietary NFC Get + Collision Resolution status + callback */ + rfalNfcPropCallback + rfalNfcpStartActivation; /*!< Prorietary NFC Start Activation callback */ + rfalNfcPropCallback + rfalNfcpGetActivationStatus; /*!< Prorietary NFC Get Activation status + callback */ +} rfalNfcPropCallbacks; + +/*! Discovery parameters */ +typedef struct { + rfalComplianceMode compMode; /*!< Compliancy mode to be used */ + uint16_t techs2Find; /*!< Technologies to search for */ + uint16_t techs2Bail; /*!< Bail-out after certain NFC technologies */ + uint16_t totalDuration; /*!< Duration of a whole Poll + Listen cycle NCI 2.1 + Table 46 */ + uint8_t devLimit; /*!< Max number of devices Activity 2.1 + Table 11 */ + rfalBitRate maxBR; /*!< Max Bit rate to be used NCI 2.1 + Table 28 */ + + rfalBitRate nfcfBR; /*!< Bit rate to poll for NFC-F NCI 2.1 Table 27 */ + uint8_t nfcid3[RFAL_NFCDEP_NFCID3_LEN]; /*!< NFCID3 to be used on the + ATR_REQ/ATR_RES */ + uint8_t GB[RFAL_NFCDEP_GB_MAX_LEN]; /*!< General bytes to be used on the + ATR-REQ NCI 2.1 Table 29 */ + uint8_t GBLen; /*!< Length of the General Bytes NCI 2.1 + Table 29 */ + rfalBitRate ap2pBR; /*!< Bit rate to poll for AP2P NCI 2.1 Table 31 */ + bool p2pNfcaPrio; /*!< NFC-A P2P (true) or ISO14443-4/T4T (false) priority */ + rfalNfcPropCallbacks propNfc; /*!< Proprietary Technlogy callbacks */ + + rfalIsoDepFSxI isoDepFS; /*!< ISO-DEP Poller announced maximum frame size + Digital 2.2 Table 60 */ + uint8_t nfcDepLR; /*!< NFC-DEP Poller & Listener maximum frame size + Digital 2.2 Table 90 */ + + rfalLmConfPA lmConfigPA; /*!< Configuration for Passive Listen mode NFC-A */ + rfalLmConfPF lmConfigPF; /*!< Configuration for Passive Listen mode NFC-A */ + + void (*notifyCb)(rfalNfcState st); /*!< Callback to Notify upper layer */ + + bool wakeupEnabled; /*!< Enable Wake-Up mode before polling */ + bool wakeupConfigDefault; /*!< Wake-Up mode default configuration */ + rfalWakeUpConfig wakeupConfig; /*!< Wake-Up mode configuration */ + bool wakeupPollBefore; /*!< Flag to Poll wakeupNPolls times before entering + Wake-up */ + uint16_t wakeupNPolls; /*!< Number of polling cycles before|after entering + Wake-up */ +} rfalNfcDiscoverParam; + +/*! Buffer union, only one interface is used at a time */ +typedef union { /* PRQA S 0750 # MISRA 19.2 - Members of the union will not be + used concurrently, only one interface at a time */ + uint8_t rfBuf[RFAL_FEATURE_NFC_RF_BUF_LEN]; /*!< RF buffer */ + rfalIsoDepApduBufFormat + isoDepBuf; /*!< ISO-DEP buffer format (with header/prologue) */ + rfalNfcDepPduBufFormat + nfcDepBuf; /*!< NFC-DEP buffer format (with header/prologue) */ +} rfalNfcBuffer; + +/*******************************************************************************/ + +/* +****************************************************************************** +* GLOBAL FUNCTION PROTOTYPES +****************************************************************************** +*/ + +/*! + ***************************************************************************** + * \brief RFAL NFC Worker + * + * It runs the internal state machine and runs the RFAL RF worker. + ***************************************************************************** + */ +void rfalNfcWorker(void); + +/*! + ***************************************************************************** + * \brief RFAL NFC Initialize + * + * It initializes this module and its dependencies + * + * \return RFAL_ERR_WRONG_STATE : Incorrect state for this operation + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalNfcInitialize(void); + +/*! + ***************************************************************************** + * \brief RFAL NFC Discovery + * + * It set the device in Discovery state. + * In discovery it will Poll and/or Listen for the technologies configured, + * and perform Wake-up mode if configured to do so. + * + * The device list passed on disParams must not be empty. + * The number of devices on the list is indicated by the devLimit and shall + * be at >= 1. + * + * \param[in] disParams : discovery configuration parameters + * + * \return RFAL_ERR_WRONG_STATE : Incorrect state for this operation + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalNfcDiscover(const rfalNfcDiscoverParam *disParams); + +/*! + ***************************************************************************** + * \brief RFAL NFC Get State + * + * It returns the current state + * + * \return rfalNfcState : the current state + ***************************************************************************** + */ +rfalNfcState rfalNfcGetState(void); + +/*! + ***************************************************************************** + * \brief RFAL NFC Get Devices Found + * + * It returns the location of the device list and the number of + * devices found. + * + * \param[out] devList : device list location + * \param[out] devCnt : number of devices found + * + * \return RFAL_ERR_WRONG_STATE : Incorrect state for this operation + * Discovery still ongoing + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalNfcGetDevicesFound(rfalNfcDevice **devList, uint8_t *devCnt); + +/*! + ***************************************************************************** + * \brief RFAL NFC Get Active Device + * + * It returns the location of the device current Active device + * + * \param[out] dev : device info location + * + * \return RFAL_ERR_WRONG_STATE : Incorrect state for this operation + * No device activated + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalNfcGetActiveDevice(rfalNfcDevice **dev); + +/*! + ***************************************************************************** + * \brief RFAL NFC Select Device + * + * It selects the device to be activated. + * It shall be called when more than one device has been identified to + * indicate which device shall be actived + * + * \param[in] devIdx : device index to be activated + * + * \return RFAL_ERR_WRONG_STATE : Incorrect state for this operation + * Not in select state + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalNfcSelect(uint8_t devIdx); + +/*! + ***************************************************************************** + * \brief RFAL NFC Start Data Exchange + * + * After a device has been activated, it starts a data exchange. + * It handles automatically which interface/protocol to be used and acts + *accordingly. + * + * In Listen mode the first frame/data shall be sent by the Reader/Initiator + * therefore this method must be called first with txDataLen set to zero + * to retrieve the rxData and rcvLen locations. + * + * + * \param[in] txData : data to be transmitted + * \param[in] txDataLen : size of the data to be transmitted (in bits or + *bytes - see below) \param[out] rxData : location of the received data + *after operation is completed \param[out] rvdLen : location of the length + *of the received data (in bits or bytes - see below) \param[in] fwt : + *FWT to be used in case of RF interface. If ISO-DEP or NFC-DEP interface is + *used, this will be ignored + * + * \warning In order to support a wider range of protocols, when RF interface is + *used the lengths are in number of bits (not bytes). Therefore both input + *txDataLen and output rvdLen refer to bits. If ISO-DEP or NFC-DEP interface is + *used those are expressed in number of bytes. + * + * + * \return RFAL_ERR_WRONG_STATE : Incorrect state for this operation + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalNfcDataExchangeStart(uint8_t *txData, uint16_t txDataLen, + uint8_t **rxData, uint16_t **rvdLen, + uint32_t fwt); + +/*! + ***************************************************************************** + * \brief RFAL NFC Get Data Exchange Status + * + * Gets current Data Exchange status + * + * \return RFAL_ERR_NONE : Transceive done with no error + * \return RFAL_ERR_BUSY : Transceive ongoing + * \return RFAL_ERR_AGAIN : received one chaining block, copy received + *data and continue to call this method to retrieve the remaining blocks \return + *RFAL_ERR_XXXX : Error occurred \return RFAL_ERR_TIMEOUT : No + *response \return RFAL_ERR_FRAMING : Framing error detected \return + *RFAL_ERR_PAR : Parity error detected \return RFAL_ERR_CRC : + *CRC error detected \return RFAL_ERR_LINK_LOSS : Link Loss - External Field + *is Off \return RFAL_ERR_RF_COLLISION : Collision detected \return RFAL_ERR_IO + *: Internal error + ***************************************************************************** + */ +ReturnCode rfalNfcDataExchangeGetStatus(void); + +/*! + ***************************************************************************** + * \brief RFAL NFC Deactivate + * + * It triggers the deactivation procedure to terminate communications with + * remote device. + * In case the deactivation type is RFAL_NFC_DEACTIVATE_SLEEP the field is + * kept On and device selection shall follow. Otherwise the field will + * be turned Off. + * + * \warning In case the deactivation type is RFAL_NFC_DEACTIVATE_IDLE the + * deactivation procedure is executed immediately and in a blocking manner + * + * \param[in] deactType : Type of deactivation to be performed + * + * \return RFAL_ERR_WRONG_STATE : Incorrect state for this operation + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalNfcDeactivate(rfalNfcDeactivateType deactType); + +#endif /* RFAL_NFC_H */ + +/** + * @} + * + * @} + * + * @} + */ diff --git a/core/embed/io/nfc/rfal/include/rfal_nfcDep.h b/core/embed/io/nfc/rfal/include/rfal_nfcDep.h new file mode 100644 index 0000000000..f92ff41d7f --- /dev/null +++ b/core/embed/io/nfc/rfal/include/rfal_nfcDep.h @@ -0,0 +1,843 @@ + +/****************************************************************************** + * @attention + * + * COPYRIGHT 2016 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/* + * PROJECT: ST25R391x firmware + * Revision: + * LANGUAGE: ISO C99 + */ + +/*! \file rfal_nfcDep.h + * + * \author Gustavo Patricio + * + * \brief Implementation of NFC-DEP protocol + * + * NFC-DEP is also known as NFCIP - Near Field Communication + * Interface and Protocol + * + * This implementation was based on the following specs: + * - NFC Forum Digital 1.1 + * - ECMA 340 3rd Edition 2013 + * + * + * \addtogroup RFAL + * @{ + * + * \addtogroup RFAL-AL + * \brief RFAL Abstraction Layer + * @{ + * + * \addtogroup NFC-DEP + * \brief RFAL NFC-DEP Module + * @{ + */ + +#ifndef RFAL_NFCDEP_H_ +#define RFAL_NFCDEP_H_ + +/* + ****************************************************************************** + * INCLUDES + ****************************************************************************** + */ +#include "rfal_platform.h" +#include "rfal_rf.h" +#include "rfal_utils.h" + +/* + ****************************************************************************** + * ENABLE SWITCH + ****************************************************************************** + */ + +/* If module is disabled remove the need for the user to set lengths */ +#if !RFAL_FEATURE_NFC_DEP +#undef RFAL_FEATURE_NFC_DEP_BLOCK_MAX_LEN +#undef RFAL_FEATURE_NFC_DEP_PDU_MAX_LEN + +#define RFAL_FEATURE_NFC_DEP_BLOCK_MAX_LEN \ + 1U /*!< NFC-DEP Block/Payload length, set to "none" */ +#define RFAL_FEATURE_NFC_DEP_PDU_MAX_LEN \ + 1U /*!< NFC-DEP PDU length, set to "none" */ +#endif /* !RFAL_FEATURE_NFC_DEP */ + +/* + ****************************************************************************** + * DEFINES + ****************************************************************************** + */ +#define RFAL_NFCDEP_FRAME_SIZE_MAX_LEN \ + 254U /*!< Maximum Frame Size Digital 2.0 Table 90 */ +#define RFAL_NFCDEP_DEPREQ_HEADER_LEN \ + 5U /*!< DEP_REQ header length: CMD_TYPE + CMD_CMD + PBF + DID + NAD */ + +/*! Length NFCIP DEP REQ or RES header (incl LEN) */ +#define RFAL_NFCDEP_DEP_HEADER \ + (RFAL_NFCDEP_LEN_LEN + RFAL_NFCDEP_CMDTYPE_LEN + RFAL_NFCDEP_CMD_LEN + \ + RFAL_NFCDEP_DEP_PFB_LEN) +#define RFAL_NFCDEP_HEADER \ + (RFAL_NFCDEP_CMDTYPE_LEN + RFAL_NFCDEP_CMD_LEN) /*!< NFCIP header length */ +#define RFAL_NFCDEP_SB_LEN \ + 1U /*!< SB length on NFCIP fram for NFC-A */ +#define RFAL_NFCDEP_LEN_LEN \ + 1U /*!< LEN length on NFCIP frame */ +#define RFAL_NFCDEP_CMDTYPE_LEN \ + 1U /*!< Length of the cmd type (REQ | RES) on NFCIP frame */ +#define RFAL_NFCDEP_CMD_LEN \ + 1U /*!< Length of the cmd on NFCIP frame */ +#define RFAL_NFCDEP_DID_LEN \ + 1U /*!< Length of did on NFCIP frame */ +#define RFAL_NFCDEP_DEP_PFB_LEN \ + 1U /*!< Length of the PFB field on NFCIP frame */ + +#define RFAL_NFCDEP_DSL_RLS_LEN_NO_DID \ + (RFAL_NFCDEP_LEN_LEN + RFAL_NFCDEP_CMDTYPE_LEN + \ + RFAL_NFCDEP_CMD_LEN) /*!< Length of DSL_REQ and RLS_REQ with no DID */ +#define RFAL_NFCDEP_DSL_RLS_LEN_DID \ + (RFAL_NFCDEP_DSL_RLS_LEN_NO_DID + \ + RFAL_NFCDEP_DID_LEN) /*!< Length of DSL_REQ and RLS_REQ with DID */ + +#define RFAL_NFCDEP_FS_VAL_MIN \ + 64U /*!< Minimum LR value */ +#define RFAL_NFCDEP_LR_VAL_MASK \ + 0x03U /*!< Bit mask for a LR value */ +#define RFAL_NFCDEP_PP_LR_MASK \ + 0x30U /*!< Bit mask for LR value in PP byte on a ATR REQ/RES */ +#define RFAL_NFCDEP_PP_LR_SHIFT \ + 4U /*!< Position of LR value in PP byte on a ATR REQ/RES */ + +#define RFAL_NFCDEP_DID_MAX \ + 14U /*!< Max DID value Digital 14.6.2.3 */ +#define RFAL_NFCDEP_DID_KEEP \ + 0xFFU /*!< Keep DID value already configured */ +#define RFAL_NFCDEP_DID_NO \ + 0x00U /*!< No DID shall be used */ +#define RFAL_NFCDEP_NAD_NO \ + 0x00U /*!< No NAD shall be used */ + +#define RFAL_NFCDEP_OPER_RTOX_REQ_DIS \ + 0x01U /*!< Operation config: RTOX REQ disable */ +#define RFAL_NFCDEP_OPER_RTOX_REQ_EN \ + 0x00U /*!< Operation config: RTOX REQ enable */ + +#define RFAL_NFCDEP_OPER_ATN_DIS \ + 0x00U /*!< Operation config: ATN disable */ +#define RFAL_NFCDEP_OPER_ATN_EN \ + 0x02U /*!< Operation config: ATN enable */ + +#define RFAL_NFCDEP_OPER_EMPTY_DEP_DIS \ + 0x04U /*!< Operation config: empty DEPs disable */ +#define RFAL_NFCDEP_OPER_EMPTY_DEP_EN \ + 0x00U /*!< Operation config: empty DEPs enable */ + +#define RFAL_NFCDEP_OPER_FULL_MI_DIS \ + 0x00U /*!< Operation config: full chaining DEPs disable */ +#define RFAL_NFCDEP_OPER_FULL_MI_EN \ + 0x08U /*!< Operation config: full chaining DEPs enable */ + +#define RFAL_NFCDEP_BRS_MAINTAIN \ + 0xC0U /*!< Value signalling that BR is to be maintained (no PSL) */ +#define RFAL_NFCDEP_BRS_Dx_MASK \ + 0x07U /*!< Value signalling that BR is to be maintained (no PSL) */ +#define RFAL_NFCDEP_BRS_DSI_POS \ + 3U /*!< Value signalling that BR is to be maintained (no PSL) */ + +#define RFAL_NFCDEP_WT_DELTA \ + (16U - RFAL_NFCDEP_WT_DELTA_ADJUST) /*!< NFC-DEP dWRT (adjusted) Digital 2.0 \ + B.10 */ +#define RFAL_NFCDEP_WT_DELTA_ADJUST \ + 4U /*!< dWRT value adjustment */ + +#define RFAL_NFCDEP_ATR_REQ_NFCID3_POS \ + 2U /*!< NFCID3 offset in ATR_REQ frame */ +#define RFAL_NFCDEP_NFCID3_LEN \ + 10U /*!< NFCID3 Length */ + +#define RFAL_NFCDEP_LEN_MIN \ + 3U /*!< Minimum length byte LEN value */ +#define RFAL_NFCDEP_LEN_MAX \ + 255U /*!< Maximum length byte LEN value */ + +#define RFAL_NFCDEP_ATRRES_HEADER_LEN \ + 2U /*!< ATR RES Header Len: CmdType: 0xD5 + Cod: 0x01 */ +#define RFAL_NFCDEP_ATRRES_MIN_LEN \ + 17U /*!< Minimum length for an ATR RES */ +#define RFAL_NFCDEP_ATRRES_MAX_LEN \ + 64U /*!< Maximum length for an ATR RES Digital 1.0 14.6.1 */ +#define RFAL_NFCDEP_ATRREQ_MIN_LEN \ + 16U /*!< Minimum length for an ATR REQ */ +#define RFAL_NFCDEP_ATRREQ_MAX_LEN \ + RFAL_NFCDEP_ATRRES_MAX_LEN /*!< Maximum length for an ATR REQ \ + Digital 1.0 14.6.1 */ + +#define RFAL_NFCDEP_GB_MAX_LEN \ + (RFAL_NFCDEP_ATRREQ_MAX_LEN - \ + RFAL_NFCDEP_ATRREQ_MIN_LEN) /*!< Maximum length the General Bytes on ATR \ + Digital 1.1 16.6.3 */ + +#define RFAL_NFCDEP_WT_INI_DEFAULT \ + RFAL_NFCDEP_WT_INI_MAX /*!< WT Initiator default value Digital 1.0 14.6.3.8 \ + */ +#define RFAL_NFCDEP_WT_INI_MIN \ + 0U /*!< WT Initiator minimum value Digital 1.0 14.6.3.8 */ +#define RFAL_NFCDEP_WT_INI_MAX \ + 14U /*!< WT Initiator maximum value Digital 1.0 14.6.3.8 A.10 */ +#define RFAL_NFCDEP_RWT_INI_MAX \ + rfalNfcDepWT2RWT(RFAL_NFCDEP_WT_INI_MAX) /*!< RWT Initiator maximum value */ + +#define RFAL_NFCDEP_WT_TRG_MAX_D10 \ + 8U /*!< WT target max Digital 1.0 14.6.3.8 A.10 */ +#define RFAL_NFCDEP_WT_TRG_MAX_D11 \ + 14U /*!< WT target max Digital 1.1 16.6.3.9 A.9 */ +#define RFAL_NFCDEP_WT_TRG_MAX_L13 \ + 10U /*!< WT target max [LLCP] 1.3 6.2.1 */ +#define RFAL_NFCDEP_WT_TRG_MAX \ + RFAL_NFCDEP_WT_TRG_MAX_D11 /*!< WT target max Digital x.x | LLCP x.x */ +#define RFAL_NFCDEP_RWT_TRG_MAX \ + rfalNfcDepWT2RWT(RFAL_NFCDEP_WT_TRG_MAX) /*!< RWT Initiator maximum value */ + +/*! Maximum Frame Waiting Time = ((256 * 16/fc)*2^FWImax) = ((256*16/fc)*2^14) = + * (1048576 / 64)/fc = (100000h*64)/fc */ +#define RFAL_NFCDEP_MAX_FWT ((uint32_t)1U << 20) + +#define RFAL_NFCDEP_WT_MASK \ + 0x0FU /*!< Bit mask for the Wait Time value */ + +#define RFAL_NFCDEP_BR_MASK_106 \ + 0x01U /*!< Enable mask bit rate 106 */ +#define RFAL_NFCDEP_BR_MASK_212 \ + 0x02U /*!< Enable mask bit rate 242 */ +#define RFAL_NFCDEP_BR_MASK_424 \ + 0x04U /*!< Enable mask bit rate 424 */ + +/* + ****************************************************************************** + * GLOBAL MACROS + ****************************************************************************** + */ + +#define rfalNfcDepWT2RWT(wt) \ + ((uint32_t)1U << (((uint32_t)(wt) & RFAL_NFCDEP_WT_MASK) + \ + 12U)) /*!< Converts WT value to RWT (1/fc) */ + +/*! Returns the BRS value from the given bit rate */ +#define rfalNfcDepDx2BRS(br) \ + ((((uint8_t)(br) & RFAL_NFCDEP_BRS_Dx_MASK) << RFAL_NFCDEP_BRS_DSI_POS) | \ + ((uint8_t)(br) & RFAL_NFCDEP_BRS_Dx_MASK)) + +#define rfalNfcDepBRS2DRI(brs) \ + (uint8_t)((uint8_t)(brs) & \ + RFAL_NFCDEP_BRS_Dx_MASK) /*!< Returns the DRI value from the given \ + BRS byte */ +#define rfalNfcDepBRS2DSI(brs) \ + (uint8_t)(((uint8_t)(brs) >> RFAL_NFCDEP_BRS_DSI_POS) & \ + RFAL_NFCDEP_BRS_Dx_MASK) /*!< Returns the DSI value from the given \ + BRS byte */ + +#define rfalNfcDepPP2LR(PPx) \ + (((uint8_t)(PPx) & RFAL_NFCDEP_PP_LR_MASK) >> \ + RFAL_NFCDEP_PP_LR_SHIFT) /*!< Returns the LR value from the given PPx byte \ + */ +#define rfalNfcDepLR2PP(LRx) \ + (((uint8_t)(LRx) << RFAL_NFCDEP_PP_LR_SHIFT) & \ + RFAL_NFCDEP_PP_LR_MASK) /*!< Returns the PP byte with the given LRx value \ + */ + +/*! Returns the Frame size value from the given LRx value */ +#define rfalNfcDepLR2FS(LRx) \ + (uint16_t)(RFAL_MIN((RFAL_NFCDEP_FS_VAL_MIN * ((uint16_t)(LRx) + 1U)), \ + RFAL_NFCDEP_FRAME_SIZE_MAX_LEN)) + +/*! + * Despite DIGITAL 1.0 14.6.2.1 stating that the last two bytes may filled with + * any value, some devices (Samsung Google Nexus) only accept when these are 0 + */ +#define rfalNfcDepSetNFCID(dst, src, len) \ + RFAL_MEMSET((dst), 0x00, RFAL_NFCDEP_NFCID3_LEN); \ + if ((len) > 0U) { \ + RFAL_MEMCPY((dst), (src), (len)); \ + } + +/* + ****************************************************************************** + * GLOBAL ENUMERATIONS + ****************************************************************************** + */ + +/*! Enumeration of NFC-DEP bit rate in ATR Digital 1.0 Table 93 and 94 */ +enum { + RFAL_NFCDEP_Bx_NO_HIGH_BR = 0x00, /*!< Peer supports no high bit rates */ + RFAL_NFCDEP_Bx_08_848 = 0x01, /*!< Peer also supports 848 */ + RFAL_NFCDEP_Bx_16_1695 = 0x02, /*!< Peer also supports 1695 */ + RFAL_NFCDEP_Bx_32_3390 = 0x04, /*!< Peer also supports 3390 */ + RFAL_NFCDEP_Bx_64_6780 = 0x08 /*!< Peer also supports 6780 */ +}; + +/*! Enumeration of NFC-DEP bit rate Dividor in PSL Digital 1.0 Table 100 */ +enum { + RFAL_NFCDEP_Dx_01_106 = RFAL_BR_106, /*!< Divisor D = 1 : bit rate = 106 */ + RFAL_NFCDEP_Dx_02_212 = RFAL_BR_212, /*!< Divisor D = 2 : bit rate = 212 */ + RFAL_NFCDEP_Dx_04_424 = RFAL_BR_424, /*!< Divisor D = 4 : bit rate = 424 */ + RFAL_NFCDEP_Dx_08_848 = RFAL_BR_848, /*!< Divisor D = 8 : bit rate = 848 */ + RFAL_NFCDEP_Dx_16_1695 = + RFAL_BR_1695, /*!< Divisor D = 16 : bit rate = 1695 */ + RFAL_NFCDEP_Dx_32_3390 = + RFAL_BR_3390, /*!< Divisor D = 32 : bit rate = 3390 */ + RFAL_NFCDEP_Dx_64_6780 = RFAL_BR_6780 /*!< Divisor D = 64 : bit rate = 6780 */ +}; + +/*! Enumeration of NFC-DEP Length Reduction (LR) Digital 1.0 Table 91 */ +enum { + RFAL_NFCDEP_LR_64 = 0x00, /*!< Maximum payload size is 64 bytes */ + RFAL_NFCDEP_LR_128 = 0x01, /*!< Maximum payload size is 128 bytes */ + RFAL_NFCDEP_LR_192 = 0x02, /*!< Maximum payload size is 192 bytes */ + RFAL_NFCDEP_LR_254 = 0x03 /*!< Maximum payload size is 254 bytes */ +}; + +/* + ****************************************************************************** + * GLOBAL DATA TYPES + ****************************************************************************** + */ + +/*! NFC-DEP callback to check if upper layer has deactivation pending */ +typedef bool (*rfalNfcDepDeactCallback)(void); + +/*! Enumeration of the nfcip communication modes */ +typedef enum { + RFAL_NFCDEP_COMM_PASSIVE, /*!< Passive communication mode */ + RFAL_NFCDEP_COMM_ACTIVE /*!< Active communication mode */ +} rfalNfcDepCommMode; + +/*! Enumeration of the nfcip roles */ +typedef enum { + RFAL_NFCDEP_ROLE_INITIATOR, /*!< Perform as Initiator */ + RFAL_NFCDEP_ROLE_TARGET /*!< Perform as Target */ +} rfalNfcDepRole; + +/*! Struct that holds all NFCIP configs */ +typedef struct { + rfalNfcDepRole role; /*!< Current NFCIP role */ + rfalNfcDepCommMode commMode; /*!< Current NFCIP communication mode */ + uint8_t oper; /*!< Operation config similar to NCI 1.0 Table 81 */ + + uint8_t did; /*!< Current Device ID (DID) */ + uint8_t nad; /*!< Current Node Addressing (NAD) */ + uint8_t bs; /*!< Bit rate in Sending Direction */ + uint8_t br; /*!< Bit rate in Receiving Direction */ + uint8_t nfcid[RFAL_NFCDEP_NFCID3_LEN]; /*!< Pointer to the NFCID to be used */ + uint8_t nfcidLen; /*!< Length of the given NFCID in nfcid */ + uint8_t + gb[RFAL_NFCDEP_GB_MAX_LEN]; /*!< Pointer General Bytes (GB) to be used */ + uint8_t gbLen; /*!< Length of the given GB in gb */ + uint8_t lr; /*!< Length Reduction (LR) to be used */ + uint8_t to; /*!< Timeout (TO) to be used */ + uint32_t fwt; /*!< Frame Waiting Time (FWT) to be used */ + uint32_t dFwt; /*!< Delta Frame Waiting Time (dFWT) to be used */ +} rfalNfcDepConfigs; + +/*! ATR_REQ command Digital 1.1 16.6.2 */ +typedef struct { + uint8_t CMD1; /*!< Command format 0xD4 */ + uint8_t CMD2; /*!< Command Value */ + uint8_t NFCID3[RFAL_NFCDEP_NFCID3_LEN]; /*!< NFCID3 value */ + uint8_t DID; /*!< DID */ + uint8_t BSi; /*!< Sending Bitrate for Initiator */ + uint8_t BRi; /*!< Receiving Bitrate for Initiator */ + uint8_t PPi; /*!< Optional Parameters presence indicator */ + uint8_t GBi[RFAL_NFCDEP_GB_MAX_LEN]; /*!< General Bytes */ +} rfalNfcDepAtrReq; + +/*! ATR_RES response Digital 1.1 16.6.3 */ +typedef struct { + uint8_t CMD1; /*!< Response Byte 0xD5 */ + uint8_t CMD2; /*!< Command Value */ + uint8_t NFCID3[RFAL_NFCDEP_NFCID3_LEN]; /*!< NFCID3 value */ + uint8_t DID; /*!< DID */ + uint8_t BSt; /*!< Sending Bitrate for Initiator */ + uint8_t BRt; /*!< Receiving Bitrate for Initiator */ + uint8_t TO; /*!< Timeout */ + uint8_t PPt; /*!< Optional Parameters presence indicator */ + uint8_t GBt[RFAL_NFCDEP_GB_MAX_LEN]; /*!< General Bytes */ +} rfalNfcDepAtrRes; + +/*! Structure of transmit I-PDU Buffer format from caller */ +typedef struct { + uint8_t prologue[RFAL_NFCDEP_DEPREQ_HEADER_LEN]; /*!< Prologue space for + NFC-DEP header*/ + uint8_t inf[RFAL_FEATURE_NFC_DEP_BLOCK_MAX_LEN]; /*!< INF | Data area of the + buffer */ +} rfalNfcDepBufFormat; + +/*! Structure of APDU Buffer format from caller */ +typedef struct { + uint8_t prologue[RFAL_NFCDEP_DEPREQ_HEADER_LEN]; /*!< Prologue/SoD buffer */ + uint8_t + pdu[RFAL_FEATURE_NFC_DEP_PDU_MAX_LEN]; /*!< Complete PDU/Payload buffer */ +} rfalNfcDepPduBufFormat; + +/*! Activation info as Initiator and Target */ +typedef union { /* PRQA S 0750 # MISRA 19.2 - Both members of the union will + not be used concurrently , device is only initiatior or + target a time. No problem can occur. */ + struct { + rfalNfcDepAtrRes ATR_RES; /*!< ATR RES (Initiator mode) */ + uint8_t ATR_RESLen; /*!< ATR RES length (Initiator mode) */ + } Target; /*!< Target */ + struct { + rfalNfcDepAtrReq ATR_REQ; /*!< ATR REQ (Target mode) */ + uint8_t ATR_REQLen; /*!< ATR REQ length (Target mode) */ + } Initiator; /*!< Initiator */ +} rfalNfcDepActivation; + +/*! NFC-DEP device Info */ +typedef struct { + uint8_t GBLen; /*!< General Bytes length */ + uint8_t WT; /*!< WT to be used (ignored in Listen Mode) */ + uint32_t FWT; /*!< FWT to be used (1/fc)(ignored Listen Mode) */ + uint32_t dFWT; /*!< Delta FWT to be used (1/fc) */ + uint8_t LR; /*!< Length Reduction coding the max payload */ + uint16_t FS; /*!< Frame Size */ + rfalBitRate DSI; /*!< Bit Rate coding from Initiator to Target */ + rfalBitRate DRI; /*!< Bit Rate coding from Target to Initiator */ + uint8_t DID; /*!< Device ID (RFAL_NFCDEP_DID_NO if no DID) */ + uint8_t NAD; /*!< Node ADdress (RFAL_NFCDEP_NAD_NO if no NAD)*/ +} rfalNfcDepInfo; + +/*! NFC-DEP Device structure */ +typedef struct { + rfalNfcDepActivation activation; /*!< Activation Info */ + rfalNfcDepInfo info; /*!< NFC-DEP device Info */ +} rfalNfcDepDevice; + +/*! NFCIP Protocol structure for P2P Target + * + * operParam : derives from NFC-Forum NCI NFC-DEP Operation Parameter + * NCI 1.1 Table 86: NFC-DEP Operation Parameter + * and it's a bit mask composed as: + * [ 0000b + * | Chain SHALL use max. Transport Data Byte[1b] + * | I-PDU with no Transport Data SHALL NOT be sent [1b] + * | NFC-DEP Target SHALL NOT send RTOX request [1b] + * ] + * + */ +typedef struct { + rfalNfcDepCommMode commMode; /*!< Initiator in Active P2P or Passive P2P*/ + uint8_t operParam; /*!< NFC-DEP Operation Parameter */ + uint8_t *nfcid; /*!< Initiator's NFCID2 or NFCID3 */ + uint8_t nfcidLen; /*!< Initiator's NFCID length (NFCID2/3) */ + uint8_t DID; /*!< Initiator's Device ID DID */ + uint8_t NAD; /*!< Initiator's Node ID NAD */ + uint8_t BS; /*!< Initiator's Bit Rates supported in Tx */ + uint8_t BR; /*!< Initiator's Bit Rates supported in Rx */ + uint8_t LR; /*!< Initiator's Length reduction */ + uint8_t *GB; /*!< Initiator's General Bytes (Gi) */ + uint8_t GBLen; /*!< Initiator's General Bytes length */ +} rfalNfcDepAtrParam; + +/*! Structure of parameters to be passed in for nfcDepListenStartActivation */ +typedef struct { + rfalNfcDepBufFormat *rxBuf; /*!< Receive Buffer struct reference */ + uint16_t *rxLen; /*!< Receive INF data length in bytes */ + bool *isRxChaining; /*!< Received data is not complete */ + rfalNfcDepDevice *nfcDepDev; /*!< NFC-DEP device info */ +} rfalNfcDepListenActvParam; + +/*! NFCIP Protocol structure for P2P Target + * + * operParam : derives from NFC-Forum NCI NFC-DEP Operation Parameter + * NCI 1.1 Table 86: NFC-DEP Operation Parameter + * and it's a bit mask composed as: + * [ 0000b + * | Chain SHALL use max. Transport Data Byte[1b] + * | I-PDU with no Transport Data SHALL NOT be sent [1b] + * | NFC-DEP Target SHALL NOT send RTOX request [1b] + * ] + * + */ +typedef struct { + rfalNfcDepCommMode commMode; /*!< Target in Active P2P or Passive P2P */ + uint8_t nfcid3[RFAL_NFCDEP_NFCID3_LEN]; /*!< Target's NFCID3 */ + uint8_t bst; /*!< Target's Bit Rates supported in Tx */ + uint8_t brt; /*!< Target's Bit Rates supported in Rx */ + uint8_t to; /*!< Target's timeout (TO) value */ + uint8_t ppt; /*!< Target's Presence optional Params(PPt)*/ + uint8_t GBt[RFAL_NFCDEP_GB_MAX_LEN]; /*!< Target's General Bytes (Gt) */ + uint8_t GBtLen; /*!< Target's General Bytes length */ + uint8_t operParam; /*!< NFC-DEP Operation Parameter */ +} rfalNfcDepTargetParam; + +/*! Structure of parameters to be passed in for nfcDepStartIpduTransceive */ +typedef struct { + rfalNfcDepBufFormat *txBuf; /*!< Transmit Buffer struct reference */ + uint16_t txBufLen; /*!< Transmit Buffer INF field length in bytes */ + bool isTxChaining; /*!< Transmit data is not complete */ + rfalNfcDepBufFormat *rxBuf; /*!< Receive Buffer struct reference */ + uint16_t *rxLen; /*!< Receive INF data length */ + bool *isRxChaining; /*!< Received data is not complete */ + uint32_t FWT; /*!< FWT to be used (ignored in Listen Mode) */ + uint32_t dFWT; /*!< Delta FWT to be used */ + uint16_t FSx; /*!< Other device Frame Size (FSD or FSC) */ + uint8_t DID; /*!< Device ID (RFAL_ISODEP_NO_DID if no DID) */ +} rfalNfcDepTxRxParam; + +/*! Structure of parameters used on NFC DEP PDU Transceive */ +typedef struct { + rfalNfcDepPduBufFormat *txBuf; /*!< Transmit Buffer struct reference */ + uint16_t txBufLen; /*!< Transmit Buffer INF field length in Bytes*/ + rfalNfcDepPduBufFormat + *rxBuf; /*!< Receive Buffer struct reference in Bytes */ + uint16_t *rxLen; /*!< Received INF data length in Bytes */ + rfalNfcDepBufFormat *tmpBuf; /*!< Temp buffer for single PDUs (internal) */ + uint32_t FWT; /*!< FWT to be used (ignored in Listen Mode) */ + uint32_t dFWT; /*!< Delta FWT to be used */ + uint16_t FSx; /*!< Other device Frame Size (FSD or FSC) */ + uint8_t DID; /*!< Device ID (RFAL_ISODEP_NO_DID if no DID) */ +} rfalNfcDepPduTxRxParam; + +/* + * ***************************************************************************** + * GLOBAL VARIABLE DECLARATIONS + ****************************************************************************** + */ + +/* + ****************************************************************************** + * GLOBAL FUNCTION PROTOTYPES + ****************************************************************************** + */ + +/*! + ****************************************************************************** + * \brief NFCIP Initialize + * + * This method resets all NFC-DEP inner states, counters and context and sets + * default values + * + ****************************************************************************** + */ +void rfalNfcDepInitialize(void); + +/*! + ****************************************************************************** + * \brief Set deactivating callback + * + * Sets the deactivating callback so that nfcip layer can check if upper layer + * has a deactivation pending, and not perform error recovery upon specific + * errors + * + * \param[in] pFunc : method pointer to deactivation flag check + ****************************************************************************** + */ +void rfalNfcDepSetDeactivatingCallback(rfalNfcDepDeactCallback pFunc); + +/*! + ****************************************************************************** + * \brief Calculate Response Waiting Time + * + * Calculates the Response Waiting Time (RWT) from the given Waiting Time (WT) + * + * \param[in] wt : the WT value to calculate RWT + * + * \return RWT value in 1/fc + ****************************************************************************** + */ +uint32_t rfalNfcDepCalculateRWT(uint8_t wt); + +/*! + ****************************************************************************** + * \brief NFC-DEP Initiator ATR (Attribute Request) + * + * This method configures the NFC-DEP layer with given parameters and then + * sends an ATR to the Target with and checks for a valid response response + * + * \param[in] param : parameters to initialize and compose the ATR + * \param[out] atrRes : location to store the ATR_RES + * \param[out] atrResLen : length of the ATR_RES received + * + * \return RFAL_ERR_NONE : No error + * \return RFAL_ERR_TIMEOUT : Timeout occurred + * \return RFAL_ERR_PROTO : Protocol error occurred + ****************************************************************************** + */ +ReturnCode rfalNfcDepATR(const rfalNfcDepAtrParam *param, + rfalNfcDepAtrRes *atrRes, uint8_t *atrResLen); + +/*! + ****************************************************************************** + * \brief NFC-DEP Initiator PSL (Parameter Selection) + * + * This method sends a PSL to the Target with the given parameters and checks + * for a valid response response + * + * The parameters must be coded according to Digital 1.1 16.7.1 + * + * \param[in] BRS : the selected Bit Rates for Initiator and Target + * \param[in] FSL : the maximum length of Commands and Responses + * + * \return RFAL_ERR_NONE : No error + * \return RFAL_ERR_TIMEOUT : Timeout occurred + * \return RFAL_ERR_PROTO : Protocol error occurred + ****************************************************************************** + */ +ReturnCode rfalNfcDepPSL(uint8_t BRS, uint8_t FSL); + +/*! + ****************************************************************************** + * \brief NFC-DEP Initiator DSL (Deselect) + * + * This method checks if the NFCIP module is configured as initiator and if + * so sends a DSL REQ, waits the target's response and checks it + * + * In case of performing as target no action is taken + * + * \return RFAL_ERR_NONE : No error + * \return RFAL_ERR_TIMEOUT : Timeout occurred + * \return RFAL_ERR_MAX_RERUNS : Timeout occurred + * \return RFAL_ERR_PROTO : Protocol error occurred + ****************************************************************************** + */ +ReturnCode rfalNfcDepDSL(void); + +/*! + ****************************************************************************** + * \brief NFC-DEP Initiator RLS (Release) + * + * This method checks if the NFCIP module is configured as initiator and if + * so sends a RLS REQ, waits target's response and checks it + * + * In case of performing as target no action is taken + * + * \return RFAL_ERR_NONE : No error + * \return RFAL_ERR_TIMEOUT : Timeout occurred + * \return RFAL_ERR_MAX_RERUNS : Timeout occurred + * \return RFAL_ERR_PROTO : Protocol error occurred + ****************************************************************************** + */ +ReturnCode rfalNfcDepRLS(void); + +/*! + ***************************************************************************** + * \brief NFC-DEP Initiator Handle Activation + * + * This performs a Activation into NFC-DEP layer with the given + * parameters. It sends ATR_REQ and if the higher bit rates are supported by + * both devices it additionally sends PSL + * Once Activated all details of the device are provided on nfcDepDev + * + * \param[in] param : required parameters to initialize and send ATR_REQ + * \param[in] desiredBR : Desired bit rate supported by the Poller + * \param[out] nfcDepDev : NFC-DEP information of the activated Listen device + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_TIMEOUT : Timeout error + * \return RFAL_ERR_PAR : Parity error detected + * \return RFAL_ERR_CRC : CRC error detected + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_NONE : No error, activation successful + ***************************************************************************** + */ +ReturnCode rfalNfcDepInitiatorHandleActivation(rfalNfcDepAtrParam *param, + rfalBitRate desiredBR, + rfalNfcDepDevice *nfcDepDev); + +/*! + ****************************************************************************** + * \brief Check if buffer contains valid ATR_REQ + * + * This method checks if the given ATR_REQ is valid + * + * + * \param[in] buf : buffer holding Initiator's received request + * \param[in] bufLen : size of the msg contained on the buf in Bytes + * \param[out] nfcid3 : pointer to where the NFCID3 may be outputed, + * nfcid3 has NFCF_SENSF_NFCID3_LEN as length + * Pass NULL if output parameter not desired + * + * \return true : Valid ATR_REQ received, the ATR_RES has been computed in + *txBuf \return false : Invalid protocol request + * + ****************************************************************************** + */ +bool rfalNfcDepIsAtrReq(const uint8_t *buf, uint16_t bufLen, uint8_t *nfcid3); + +/*! + ****************************************************************************** + * \brief Check is Target has received ATR + * + * This method checks if the NFCIP module is configured as target and if a + * ATR REQ has been received ( whether is in activation or in data exchange) + * + * \return true : a ATR has already been received + * \return false : no ATR has been received + ****************************************************************************** + */ +bool rfalNfcDepTargetRcvdATR(void); + +/*! + ***************************************************************************** + * \brief NFCDEP Start Listen Activation Handling + * + * Start Activation Handling and setup to receive first frame which may + * contain complete or partial DEP-REQ after activation is completed + * + * Pass in ATR_REQ for NFC-DEP to handle ATR_RES. The Activation Handling + * handles ATR_RES and PSL_RES if a PSL_REQ is received + * + * Activation is completed if PSL_RES is sent or if first I-PDU is received + * + * \ref rfalNfcDepListenGetActivationStatus() provide status of the + * ongoing activation + * + * \warning nfcDepGetTransceiveStatus() shall be called right after activation + * is completed (i.e. rfalNfcDepListenGetActivationStatus() return + *RFAL_ERR_NONE) to check for first received frame. + * + * \param[in] param : Target parameters to be used + * \param[in] atrReq : reference to buffer containing ATR_REQ + * \param[in] atrReqLength: Length of ATR_REQ + * \param[out] rxParam : references to buffer, length and chaining + *indication for first complete LLCP to be received + * + * \return RFAL_ERR_NONE : ATR_REQ is valid and activation ongoing + * \return RFAL_ERR_PARAM : ATR_REQ or other params are invalid + * \return RFAL_ERR_LINK_LOSS : Remote Field is turned off + ***************************************************************************** + */ +ReturnCode rfalNfcDepListenStartActivation(const rfalNfcDepTargetParam *param, + const uint8_t *atrReq, + uint16_t atrReqLength, + rfalNfcDepListenActvParam rxParam); + +/*! + ***************************************************************************** + * \brief Get the current NFC-DEP Activation Status + * + * \return RFAL_ERR_NONE : Activation has completed successfully + * \return RFAL_ERR_BUSY : Activation is ongoing + * \return RFAL_ERR_LINK_LOSS : Remote Field was turned off + ***************************************************************************** + */ +ReturnCode rfalNfcDepListenGetActivationStatus(void); + +/*! + ***************************************************************************** + * \brief Start Transceive + * + * Transceives a complete or partial DEP block + * + * The txBuf contains complete or partial of DEP to be transmitted. + * The Prologue field of the I-PDU is handled internally + * + * If the buffer contains partial LLCP and is not the last block, then + * isTxChaining must be set to true + * + * \param[in] param: reference parameters to be used for the Transceive + * + * \return RFAL_ERR_PARAM : Bad request + * \return RFAL_ERR_WRONG_STATE : The module is not in a proper state + * \return RFAL_ERR_NONE : The Transceive request has been started + ***************************************************************************** + */ +ReturnCode rfalNfcDepStartTransceive(const rfalNfcDepTxRxParam *param); + +/*! + ***************************************************************************** + * \brief Return the Transceive status + * + * Returns the status of the NFC-DEP Transceive + * + * \warning When the other device is performing chaining once a chained + * block is received the error RFAL_ERR_AGAIN is sent. At this point + * caller must handle the received data immediately. + * When RFAL_ERR_AGAIN is returned an ACK has already been sent to + * the other device and the next block might be incoming. + * If rfalWorker() is called frequently it will place the next + * block on the given buffer + * + * \return RFAL_ERR_NONE : Transceive has been completed successfully + * \return RFAL_ERR_BUSY : Transceive is ongoing + * \return RFAL_ERR_PROTO : Protocol error occurred + * \return RFAL_ERR_TIMEOUT : Timeout error occurred + * \return RFAL_ERR_SLEEP_REQ : Deselect has been received and responded + * \return RFAL_ERR_NOMEM : The received I-PDU does not fit into the + * receive buffer + * \return RFAL_ERR_LINK_LOSS : Communication is lost because Reader/Writer + * has turned off its field + * \return RFAL_ERR_AGAIN : received one chaining block, continue to call + * this method to retrieve the remaining blocks + ***************************************************************************** + */ +ReturnCode rfalNfcDepGetTransceiveStatus(void); + +/*! + ***************************************************************************** + * \brief Start PDU Transceive + * + * This method triggers a NFC-DEP Transceive containing a complete PDU + * It transmits the given message and handles all protocol retransmitions, + * error handling and control messages + * + * The txBuf contains a complete PDU to be transmitted + * The Prologue field will be manipulated by the Transceive + * + * \warning the txBuf will be modified during the transmission + * \warning the maximum RF frame which can be received is limited by + *param.tmpBuf + * + * \param[in] param: reference parameters to be used for the Transceive + * + * \return RFAL_ERR_PARAM : Bad request + * \return RFAL_ERR_WRONG_STATE : The module is not in a proper state + * \return RFAL_ERR_NONE : The Transceive request has been started + ***************************************************************************** + */ +ReturnCode rfalNfcDepStartPduTransceive(rfalNfcDepPduTxRxParam param); + +/*! + ***************************************************************************** + * \brief Return the PDU Transceive status + * + * Returns the status of the NFC-DEP PDU Transceive + * + * + * \return RFAL_ERR_NONE : Transceive has been completed successfully + * \return RFAL_ERR_BUSY : Transceive is ongoing + * \return RFAL_ERR_PROTO : Protocol error occurred + * \return RFAL_ERR_TIMEOUT : Timeout error occurred + * \return RFAL_ERR_SLEEP_REQ : Deselect has been received and responded + * \return RFAL_ERR_NOMEM : The received I-PDU does not fit into the + * receive buffer + * \return RFAL_ERR_LINK_LOSS : Communication is lost because Reader/Writer + * has turned off its field + ***************************************************************************** + */ +ReturnCode rfalNfcDepGetPduTransceiveStatus(void); + +#endif /* RFAL_NFCDEP_H_ */ + +/** + * @} + * + * @} + * + * @} + */ diff --git a/core/embed/io/nfc/rfal/include/rfal_nfca.h b/core/embed/io/nfc/rfal/include/rfal_nfca.h new file mode 100644 index 0000000000..10d56c8c5e --- /dev/null +++ b/core/embed/io/nfc/rfal/include/rfal_nfca.h @@ -0,0 +1,591 @@ + +/****************************************************************************** + * @attention + * + * COPYRIGHT 2016 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/* + * PROJECT: ST25R391x firmware + * Revision: + * LANGUAGE: ISO C99 + */ + +/*! \file rfal_nfca.h + * + * \author Gustavo Patricio + * + * \brief Provides several NFC-A convenience methods and definitions + * + * It provides a Poller (ISO14443A PCD) interface as well as + * some NFC-A Listener (ISO14443A PICC) helpers. + * + * The definitions and helper methods provided by this module are only + * up to ISO14443-3 layer + * + * + * An usage example is provided here: \ref exampleRfalNfca.c + * \example exampleRfalNfca.c + * + * + * \addtogroup RFAL + * @{ + * + * \addtogroup RFAL-AL + * \brief RFAL Abstraction Layer + * @{ + * + * \addtogroup NFC-A + * \brief RFAL NFC-A Module + * @{ + * + */ + +#ifndef RFAL_NFCA_H +#define RFAL_NFCA_H + +/* + ****************************************************************************** + * INCLUDES + ****************************************************************************** + */ +#include "rfal_platform.h" +#include "rfal_rf.h" +#include "rfal_t1t.h" +#include "rfal_utils.h" + +/* + ****************************************************************************** + * GLOBAL DEFINES + ****************************************************************************** + */ + +#define RFAL_NFCA_CASCADE_1_UID_LEN \ + 4U /*!< UID length of cascade level 1 only tag */ +#define RFAL_NFCA_CASCADE_2_UID_LEN \ + 7U /*!< UID length of cascade level 2 only tag */ +#define RFAL_NFCA_CASCADE_3_UID_LEN \ + 10U /*!< UID length of cascade level 3 only tag */ + +#define RFAL_NFCA_SENS_RES_PLATFORM_MASK \ + 0x0FU /*!< SENS_RES (ATQA) platform configuration mask Digital 1.1 Table 10 \ + */ +#define RFAL_NFCA_SENS_RES_PLATFORM_T1T \ + 0x0CU /*!< SENS_RES (ATQA) T1T platform configuration Digital 1.1 Table 10 \ + */ + +#define RFAL_NFCA_SEL_RES_CONF_MASK \ + 0x60U /*!< SEL_RES (SAK) platform configuration mask Digital 1.1 Table 19 \ + */ +#define RFAL_NFCA_SEL_RES_CONF_T2T \ + 0x00U /*!< SEL_RES (SAK) T2T configuration Digital 1.1 Table 19 */ +#define RFAL_NFCA_SEL_RES_CONF_T4T \ + 0x20U /*!< SEL_RES (SAK) T4T configuration Digital 1.1 Table 19 */ +#define RFAL_NFCA_SEL_RES_CONF_NFCDEP \ + 0x40U /*!< SEL_RES (SAK) NFC-DEP configuration Digital 1.1 Table 19 */ +#define RFAL_NFCA_SEL_RES_CONF_T4T_NFCDEP \ + 0x60U /*!< SEL_RES (SAK) T4T and NFC-DEP configuration Digital 1.1 Table 19 \ + */ + +/*! NFC-A minimum FDT(listen) = ((n * 128 + (84)) / fc) with n_min = 9 + * Digital 1.1 6.10.1 = (1236)/fc Relax with 3etu: (3*128)/fc as with multiple + * NFC-A cards, response may take longer (JCOP cards) = (1236 + 384)/fc = 1620 / + * fc */ +#define RFAL_NFCA_FDTMIN 1620U +/* + ****************************************************************************** + * GLOBAL MACROS + ****************************************************************************** + */ + +/*! Checks if device is a T1T given its SENS_RES */ +#define rfalNfcaIsSensResT1T(sensRes) \ + ((((rfalNfcaSensRes *)(sensRes))->platformInfo & \ + RFAL_NFCA_SENS_RES_PLATFORM_MASK) == RFAL_NFCA_SENS_RES_PLATFORM_T1T) + +/*! Checks if device is a T2T given its SENS_RES */ +#define rfalNfcaIsSelResT2T(selRes) \ + ((((rfalNfcaSelRes *)(selRes))->sak & RFAL_NFCA_SEL_RES_CONF_MASK) == \ + RFAL_NFCA_SEL_RES_CONF_T2T) + +/*! Checks if device is a T4T given its SENS_RES */ +#define rfalNfcaIsSelResT4T(selRes) \ + ((((rfalNfcaSelRes *)(selRes))->sak & RFAL_NFCA_SEL_RES_CONF_MASK) == \ + RFAL_NFCA_SEL_RES_CONF_T4T) + +/*! Checks if device supports NFC-DEP protocol given its SENS_RES */ +#define rfalNfcaIsSelResNFCDEP(selRes) \ + ((((rfalNfcaSelRes *)(selRes))->sak & RFAL_NFCA_SEL_RES_CONF_MASK) == \ + RFAL_NFCA_SEL_RES_CONF_NFCDEP) + +/*! Checks if device supports ISO-DEP and NFC-DEP protocol given its SENS_RES */ +#define rfalNfcaIsSelResT4TNFCDEP(selRes) \ + ((((rfalNfcaSelRes *)(selRes))->sak & RFAL_NFCA_SEL_RES_CONF_MASK) == \ + RFAL_NFCA_SEL_RES_CONF_T4T_NFCDEP) + +/*! Checks if a NFC-A listener device supports multiple protocols (ISO-DEP and + * NFC-DEP) */ +#define rfalNfcaLisDevIsMultiProto(lisDev) \ + (((rfalNfcaListenDevice *)(lisDev))->type == RFAL_NFCA_T4T_NFCDEP) + +/* +****************************************************************************** +* GLOBAL TYPES +****************************************************************************** +*/ + +/*! NFC-A Listen device types */ +typedef enum { + RFAL_NFCA_T1T = 0x01, /* Device configured for T1T Digital 1.1 Table 9 */ + RFAL_NFCA_T2T = 0x00, /* Device configured for T2T Digital 1.1 Table 19 */ + RFAL_NFCA_T4T = 0x20, /* Device configured for T4T Digital 1.1 Table 19 */ + RFAL_NFCA_NFCDEP = + 0x40, /* Device configured for NFC-DEP Digital 1.1 Table 19 */ + RFAL_NFCA_T4T_NFCDEP = + 0x60 /* Device configured for NFC-DEP and T4T Digital 1.1 Table 19 */ +} rfalNfcaListenDeviceType; + +/*! SENS_RES (ATQA) format Digital 1.1 6.6.3 & Table 7 */ +typedef struct { + uint8_t anticollisionInfo; /*!< SENS_RES Anticollision Information */ + uint8_t platformInfo; /*!< SENS_RES Platform Information */ +} rfalNfcaSensRes; + +/*! SDD_REQ (Anticollision) format Digital 1.1 6.7.1 & Table 11 */ +typedef struct { + uint8_t selCmd; /*!< SDD_REQ SEL_CMD: cascade Level */ + uint8_t selPar; /*!< SDD_REQ SEL_PAR: Byte Count[4b] | Bit Count[4b] (NVB: + Number of Valid Bits)*/ +} rfalNfcaSddReq; + +/*! SDD_RES (UID CLn) format Digital 1.1 6.7.2 & Table 15 */ +typedef struct { + uint8_t + nfcid1[RFAL_NFCA_CASCADE_1_UID_LEN]; /*!< NFCID1 cascade level NFCID */ + uint8_t bcc; /*!< BCC Exclusive-OR over first 4 bytes of SDD_RES */ +} rfalNfcaSddRes; + +/*! SEL_REQ (Select) format Digital 1.1 6.8.1 & Table 17 */ +typedef struct { + uint8_t selCmd; /*!< SDD_REQ SEL_CMD: cascade Level */ + uint8_t selPar; /*!< SDD_REQ SEL_PAR: Byte Count[4b] | Bit Count[4b] (NVB: + Number of Valid Bits)*/ + uint8_t nfcid1[RFAL_NFCA_CASCADE_1_UID_LEN]; /*!< NFCID1 data */ + uint8_t bcc; /*!< Checksum calculated as exclusive-OR over the 4 bytes of + NFCID1 CLn */ +} rfalNfcaSelReq; + +/*! SEL_RES (SAK) format Digital 1.1 6.8.2 & Table 19 */ +typedef struct { + uint8_t sak; /*!< Select Acknowledge */ +} rfalNfcaSelRes; + +/*! NFC-A listener device (PICC) struct */ +typedef struct { + rfalNfcaListenDeviceType type; /*!< NFC-A Listen device type */ + rfalNfcaSensRes sensRes; /*!< SENS_RES (ATQA) */ + rfalNfcaSelRes selRes; /*!< SEL_RES (SAK) */ + uint8_t nfcId1Len; /*!< NFCID1 Length */ + uint8_t nfcId1[RFAL_NFCA_CASCADE_3_UID_LEN]; /*!< NFCID1 (UID) */ +#ifdef RFAL_FEATURE_T1T + rfalT1TRidRes ridRes; /*!< RID_RES */ +#endif /* RFAL_FEATURE_T1T */ + bool isSleep; /*!< Device sleeping flag */ +} rfalNfcaListenDevice; + +/* +****************************************************************************** +* GLOBAL FUNCTION PROTOTYPES +****************************************************************************** +*/ + +/*! + ***************************************************************************** + * \brief Initialize NFC-A Poller mode + * + * This methods configures RFAL RF layer to perform as a + * NFC-A Poller/RW (ISO14443A PCD) including all default timings and bit rate + * to 106 kbps + + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or mode not set + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalNfcaPollerInitialize(void); + +/*! + ***************************************************************************** + * \brief NFC-A Poller Check Presence + * + * This method checks if a NFC-A Listen device (PICC) is present on the field + * by sending an ALL_REQ (WUPA) or SENS_REQ (REQA) + * + * \param[in] cmd : Indicate if to send an ALL_REQ or a SENS_REQ + * \param[out] sensRes : If received, the SENS_RES + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_RF_COLLISION : Collision detected one or more device in the + *field \return RFAL_ERR_PAR : Parity error detected, one or more + *device in the field \return RFAL_ERR_CRC : CRC error detected, one or + *more device in the field \return RFAL_ERR_FRAMING : Framing error + *detected, one or more device in the field \return RFAL_ERR_PROTO : + *Protocol error detected, one or more device in the field \return + *RFAL_ERR_TIMEOUT : Timeout error, no listener device detected \return + *RFAL_ERR_NONE : No error, one or more device in the field + ***************************************************************************** + */ +ReturnCode rfalNfcaPollerCheckPresence(rfal14443AShortFrameCmd cmd, + rfalNfcaSensRes *sensRes); + +/*! + ***************************************************************************** + * \brief NFC-A Poller Select + * + * This method selects a NFC-A Listener device (PICC) + * + * \param[in] nfcid1 : Listener device NFCID1 to be selected + * \param[in] nfcidLen : Length of the NFCID1 to be selected + * \param[out] selRes : pointer to place the SEL_RES + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_TIMEOUT : Timeout error + * \return RFAL_ERR_PAR : Parity error detected + * \return RFAL_ERR_CRC : CRC error detected + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_NONE : No error, SEL_RES received + ***************************************************************************** + */ +ReturnCode rfalNfcaPollerSelect(const uint8_t *nfcid1, uint8_t nfcidLen, + rfalNfcaSelRes *selRes); + +/*! + ***************************************************************************** + * \brief NFC-A Poller Start Select + * + * This method starts the selection of a NFC-A Listener device (PICC) + * + * \param[in] nfcid1 : Listener device NFCID1 to be selected + * \param[in] nfcidLen : Length of the NFCID1 to be selected + * \param[out] selRes : pointer to place the SEL_RES + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_TIMEOUT : Timeout error + * \return RFAL_ERR_PAR : Parity error detected + * \return RFAL_ERR_CRC : CRC error detected + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_NONE : No error, SEL_RES received + ***************************************************************************** + */ +ReturnCode rfalNfcaPollerStartSelect(const uint8_t *nfcid1, uint8_t nfcidLen, + rfalNfcaSelRes *selRes); + +/*! + ***************************************************************************** + * \brief NFC-A Poller Get Select Status + * + * This method gets the selection status + * + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_TIMEOUT : Timeout error + * \return RFAL_ERR_PAR : Parity error detected + * \return RFAL_ERR_CRC : CRC error detected + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_NONE : No error, SEL_RES received + ***************************************************************************** + */ +ReturnCode rfalNfcaPollerGetSelectStatus(void); + +/*! + ***************************************************************************** + * \brief NFC-A Poller Sleep + * + * This method sends a SLP_REQ (HLTA) + * No response is expected afterwards Digital 1.1 6.9.2.1 + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalNfcaPollerSleep(void); + +/*! + ***************************************************************************** + * \brief NFC-A Poller Start Sleep + * + * This method sends a SLP_REQ (HLTA) + * No response is expected afterwards Digital 1.1 6.9.2.1 + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalNfcaPollerStartSleep(void); + +/*! + ***************************************************************************** + * \brief NFC-A Poller Get Sleep Status + * + * Returns the Sleep status + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalNfcaPollerGetSleepStatus(void); + +/*! + ***************************************************************************** + * \brief NFC-A Technology Detection + * + * This method performs NFC-A Technology Detection as defined in the spec + * given in the compliance mode + * + * \param[in] compMode : compliance mode to be performed + * \param[out] sensRes : location to store the SENS_RES, if received + * + * When compMode is set to ISO compliance a SLP_REQ (HLTA) is not sent + * after detection. When set to EMV a ALL_REQ (WUPA) is sent instead of + * a SENS_REQ (REQA) + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_NONE : No error, one or more device in the field + ***************************************************************************** + */ +ReturnCode rfalNfcaPollerTechnologyDetection(rfalComplianceMode compMode, + rfalNfcaSensRes *sensRes); + +/*! + ***************************************************************************** + * \brief NFC-A Start Technology Detection + * + * This method starts NFC-A Technology Detection as defined in the spec + * given in the compliance mode + * + * \param[in] compMode : compliance mode to be performed + * \param[out] sensRes : location to store the SENS_RES, if received + * + * When compMode is set to ISO compliance a SLP_REQ (HLTA) is not sent + * after detection. When set to EMV a ALL_REQ (WUPA) is sent instead of + * a SENS_REQ (REQA) + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_NONE : No error, one or more device in the field + ***************************************************************************** + */ +ReturnCode rfalNfcaPollerStartTechnologyDetection(rfalComplianceMode compMode, + rfalNfcaSensRes *sensRes); + +/*! + ***************************************************************************** + * \brief NFC-A Get Technology Detection Status + * + * Returns the Technology Detection status + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_NONE : No error, one or more device in the field + ***************************************************************************** + */ +ReturnCode rfalNfcaPollerGetTechnologyDetectionStatus(void); + +/*! + ***************************************************************************** + * \brief NFC-A Poller Collision Resolution + * + * Collision resolution for one NFC-A Listener device/card (PICC) as + * defined in Activity 2.1 9.3.4 + * + * This method executes anti collision loop and select the device with higher + *NFCID1 + * + * When devLimit = 0 it is configured to perform collision detection only. Once + *a collision is detected the collision resolution is aborted immidiatly. If + *only one device is found with no collisions, it will properly resolved. + * + * \param[in] devLimit : device limit value (CON_DEVICES_LIMIT) + * \param[out] collPending : pointer to collision pending flag (INT_COLL_PEND) + * \param[out] selRes : location to store the last Select Response from + *listener device (PICC) \param[out] nfcId1 : location to store the NFCID1 + *(UID), ensure RFAL_NFCA_CASCADE_3_UID_LEN \param[out] nfcId1Len : pointer to + *length of NFCID1 (UID) + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or mode not set + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_PROTO : Card length invalid + * \return RFAL_ERR_IGNORE : conDevLimit is 0 and there is a collision + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalNfcaPollerSingleCollisionResolution(uint8_t devLimit, + bool *collPending, + rfalNfcaSelRes *selRes, + uint8_t *nfcId1, + uint8_t *nfcId1Len); + +/*! + ***************************************************************************** + * \brief NFC-A Poller Full Collision Resolution + * + * Performs a full Collision resolution as defined in Activity 2.1 9.3.4 + * + * \param[in] compMode : compliance mode to be performed + * \param[in] devLimit : device limit value, and size nfcaDevList + * \param[out] nfcaDevList : NFC-A listener device info + * \param[out] devCnt : Devices found counter + * + * When compMode is set to ISO compliance it assumes that the device is + * not sleeping and therefore no ALL_REQ (WUPA) is sent at the beginning. + * When compMode is set to NFC compliance an additional ALL_REQ (WUPA) is sent + * at the beginning. + * + * + * When devLimit = 0 it is configured to perform collision detection only. Once + *a collision is detected the collision resolution is aborted immidiatly. If + *only one device is found with no collisions, it will properly resolved. + * + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or mode not set + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalNfcaPollerFullCollisionResolution( + rfalComplianceMode compMode, uint8_t devLimit, + rfalNfcaListenDevice *nfcaDevList, uint8_t *devCnt); + +/*! + ***************************************************************************** + * \brief NFC-A Poller Full Collision Resolution with Sleep + * + * Performs a full Collision resolution similar to + *rfalNfcaPollerFullCollisionResolution but an additional SLP_REQ (HLTA) -> + *SENS_RES (REQA) is sent regardless if there was a collision. This proprietary + *behaviour ensures proper activation of certain devices that suffer from + *influence of Type B commands as foreseen in ISO14443-3 5.2.3 or were somehow + * not detected by the first round of collision resolution + * + * \param[in] devLimit : device limit value, and size nfcaDevList + * \param[out] nfcaDevList : NFC-A listener device info + * \param[out] devCnt : Devices found counter + * + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or mode not set + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalNfcaPollerSleepFullCollisionResolution( + uint8_t devLimit, rfalNfcaListenDevice *nfcaDevList, uint8_t *devCnt); + +/*! + ***************************************************************************** + * \brief NFC-A Poller Start Full Collision Resolution + * + * This method starts the full Collision resolution as defined + * in Activity 1.0 or 1.1 9.3.4 + * + * \param[in] compMode : compliance mode to be performed + * \param[in] devLimit : device limit value, and size nfcaDevList + * \param[out] nfcaDevList : NFC-A listener device info + * \param[out] devCnt : Devices found counter + * + * When compMode is set to ISO compliance it assumes that the device is + * not sleeping and therefore no ALL_REQ (WUPA) is sent at the beginning. + * When compMode is set to NFC compliance an additional ALL_REQ (WUPA) is sent + *at the beginning. + * + * + * When devLimit = 0 it is configured to perform collision detection only. Once + *a collision is detected the collision resolution is aborted immidiatly. If + *only one device is found with no collisions, it will properly resolved. + * + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or mode not set + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalNfcaPollerStartFullCollisionResolution( + rfalComplianceMode compMode, uint8_t devLimit, + rfalNfcaListenDevice *nfcaDevList, uint8_t *devCnt); + +/*! + ***************************************************************************** + * \brief NFC-A Get Full Collision Resolution Status + * + * Returns the Collision Resolution status + * + * \return RFAL_ERR_BUSY : Operation is ongoing + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_TIMEOUT : Timeout error + * \return RFAL_ERR_PAR : Parity error detected + * \return RFAL_ERR_CRC : CRC error detected + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_NONE : No error, activation successful + ***************************************************************************** + */ +ReturnCode rfalNfcaPollerGetFullCollisionResolutionStatus(void); + +/*! + ***************************************************************************** + * \brief NFC-A Listener is SLP_REQ + * + * Checks if the given buffer contains valid NFC-A SLP_REQ (HALT) + * + * \param[in] buf: buffer containing data + * \param[in] bufLen: length of the data in buffer to be checked + * + * \return true if data in buf contains a SLP_REQ ; false otherwise + ***************************************************************************** + */ +bool rfalNfcaListenerIsSleepReq(const uint8_t *buf, uint16_t bufLen); + +#endif /* RFAL_NFCA_H */ + +/** + * @} + * + * @} + * + * @} + */ diff --git a/core/embed/io/nfc/rfal/include/rfal_nfcb.h b/core/embed/io/nfc/rfal/include/rfal_nfcb.h new file mode 100644 index 0000000000..c69925441e --- /dev/null +++ b/core/embed/io/nfc/rfal/include/rfal_nfcb.h @@ -0,0 +1,636 @@ + +/****************************************************************************** + * @attention + * + * COPYRIGHT 2016 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/* + * PROJECT: ST25R391x firmware + * Revision: + * LANGUAGE: ISO C99 + */ + +/*! \file rfal_nfcb.h + * + * \author Gustavo Patricio + * + * \brief Implementation of NFC-B (ISO14443B) helpers + * + * It provides a NFC-B Poller (ISO14443B PCD) interface and + * also provides some NFC-B Listener (ISO14443B PICC) helpers + * + * The definitions and helpers methods provided by this module are only + * up to ISO14443-3 layer (excluding ATTRIB) + * + * + * \addtogroup RFAL + * @{ + * + * \addtogroup RFAL-AL + * \brief RFAL Abstraction Layer + * @{ + * + * \addtogroup NFC-B + * \brief RFAL NFC-B Module + * @{ + * + */ + +#ifndef RFAL_NFCB_H +#define RFAL_NFCB_H + +/* + ****************************************************************************** + * INCLUDES + ****************************************************************************** + */ +#include "rfal_platform.h" +#include "rfal_rf.h" +#include "rfal_utils.h" + +/* + ****************************************************************************** + * GLOBAL DEFINES + ****************************************************************************** + */ + +#define RFAL_NFCB_FWTSENSB \ + 7680U /*!< NFC-B FWT(SENSB) Digital 2.0 B.3 */ +#define RFAL_NFCB_DFWT 49152U /*!< NFC-B dFWT Delta 2.0 7.9.1.3 & B.3 */ +#define RFAL_NFCB_DTPOLL_10 \ + rfalConvMsTo1fc(20) /*!< NFC-B Delta Tb Poll Digital 1.0 A.2 */ +#define RFAL_NFCB_DTPOLL_20 \ + rfalConvMsTo1fc(17) /*!< NFC-B Delta Tb Poll Digital 2.1 B.3 */ + +#define RFAL_NFCB_AFI \ + 0x00U /*!< NFC-B default Application Family Digital 1.1 7.6.1.1 */ +#define RFAL_NFCB_PARAM \ + 0x00U /*!< NFC-B default SENSB_REQ PARAM */ +#define RFAL_NFCB_CRC_LEN \ + 2U /*!< NFC-B CRC length and CRC_B(AID) Digital 1.1 Table 28 */ +#define RFAL_NFCB_NFCID0_LEN \ + 4U /*!< Length of NFC-B NFCID0 */ +#define RFAL_NFCB_CMD_LEN \ + 1U /*!< Length of NFC-B Command */ + +#define RFAL_NFCB_SENSB_RES_LEN \ + 12U /*!< Standard length of SENSB_RES without SFGI byte */ +#define RFAL_NFCB_SENSB_RES_EXT_LEN \ + 13U /*!< Extended length of SENSB_RES with SFGI byte */ + +#define RFAL_NFCB_SENSB_REQ_ADV_FEATURE \ + 0x20U /*!< Bit mask for Advance Feature in SENSB_REQ */ +#define RFAL_NFCB_SENSB_RES_FSCI_MASK \ + 0x0FU /*!< Bit mask for FSCI value in SENSB_RES */ +#define RFAL_NFCB_SENSB_RES_FSCI_SHIFT \ + 4U /*!< Shift for FSCI value in SENSB_RES */ +#define RFAL_NFCB_SENSB_RES_PROTO_RFU_MASK \ + 0x08U /*!< Bit mask for Protocol Type RFU in SENSB_RES */ +#define RFAL_NFCB_SENSB_RES_PROTO_TR2_MASK \ + 0x03U /*!< Bit mask for Protocol Type TR2 in SENSB_RES */ +#define RFAL_NFCB_SENSB_RES_PROTO_TR2_SHIFT \ + 1U /*!< Shift for Protocol Type TR2 in SENSB_RES */ +#define RFAL_NFCB_SENSB_RES_PROTO_ISO_MASK \ + 0x01U /*!< Bit mask Protocol Type ISO14443 Compliant in SENSB_RES */ +#define RFAL_NFCB_SENSB_RES_FWI_MASK \ + 0x0FU /*!< Bit mask for FWI value in SENSB_RES */ +#define RFAL_NFCB_SENSB_RES_FWI_SHIFT \ + 4U /*!< Bit mask for FWI value in SENSB_RES */ +#define RFAL_NFCB_SENSB_RES_ADC_MASK \ + 0x0CU /*!< Bit mask for ADC value in SENSB_RES */ +#define RFAL_NFCB_SENSB_RES_ADC_ADV_FEATURE_MASK \ + 0x08U /*!< Bit mask for ADC.Advanced Proto Features in SENSB_RES */ +#define RFAL_NFCB_SENSB_RES_ADC_PROPRIETARY_MASK \ + 0x04U /*!< Bit mask for ADC.Proprietary Application in SENSB_RES */ +#define RFAL_NFCB_SENSB_RES_FO_DID_MASK \ + 0x01U /*!< Bit mask for DID in SENSB_RES */ +#define RFAL_NFCB_SENSB_RES_FO_NAD_MASK \ + 0x02U /*!< Bit mask for DID in SENSB_RES */ +#define RFAL_NFCB_SENSB_RES_FO_MASK \ + 0x03U /*!< Bit mask for FO value in SENSB_RES (NAD and DID) */ +#define RFAL_NFCB_SENSB_RES_SFGI_MASK \ + 0x0FU /*!< Bit mask for SFGI in SENSB_RES */ +#define RFAL_NFCB_SENSB_RES_SFGI_SHIFT \ + 4U /*!< Shift for SFGI in SENSB_RES */ + +/* +****************************************************************************** +* GLOBAL MACROS +****************************************************************************** +*/ + +/*! Get device's FSCI given its SENSB_RES Digital 1.1 7.6.2 */ +#define rfalNfcbGetFSCI(sensbRes) \ + ((((rfalNfcbSensbRes *)(sensbRes))->protInfo.FsciProType >> \ + RFAL_NFCB_SENSB_RES_FSCI_SHIFT) & \ + RFAL_NFCB_SENSB_RES_FSCI_MASK) + +/*! Checks if the given NFC-B device indicates ISO-DEP support */ +#define rfalNfcbIsIsoDepSupported(dev) \ + ((((rfalNfcbListenDevice *)(dev))->sensbRes.protInfo.FsciProType & \ + RFAL_NFCB_SENSB_RES_PROTO_ISO_MASK) != 0U) + +/* +****************************************************************************** +* GLOBAL TYPES +****************************************************************************** +*/ + +/*! SENSB_REQ and ALLB_REQ param Digital 1.1 7.6.1 */ +typedef enum { + RFAL_NFCB_SENS_CMD_ALLB_REQ = 0x08, /*!< ALLB_REQ (WUPB) */ + RFAL_NFCB_SENS_CMD_SENSB_REQ = 0x00 /*!< SENSB_REQ (REQB) */ +} rfalNfcbSensCmd; + +/*! Number of Slots (NI) codes used for NFC-B anti collision Digital 1.1 Table + * 26 */ +typedef enum { + RFAL_NFCB_SLOT_NUM_1 = 0, /*!< N=0 : 1 slot */ + RFAL_NFCB_SLOT_NUM_2 = 1, /*!< N=1 : 2 slots */ + RFAL_NFCB_SLOT_NUM_4 = 2, /*!< N=2 : 4 slots */ + RFAL_NFCB_SLOT_NUM_8 = 3, /*!< N=3 : 8 slots */ + RFAL_NFCB_SLOT_NUM_16 = 4 /*!< N=4 : 16 slots */ +} rfalNfcbSlots; + +/*! SENSB_RES (ATQB) Application Data Format Digital 1.1 Table 28 */ +typedef struct { + uint8_t AFI; /*!< Application Family Identifier */ + uint8_t CRC_B[RFAL_NFCB_CRC_LEN]; /*!< CRC_B of AID */ + uint8_t numApps; /*!< Number of Applications */ +} rfalNfcbSensbResAppData; + +/*! SENSB_RES Protocol Info format Digital 1.1 Table 29 */ +typedef struct { + uint8_t BRC; /*!< Bit Rate Capability */ + uint8_t + FsciProType; /*!< Frame Size Card Integer [4b] | Protocol Type[4 bits] */ + uint8_t FwiAdcFo; /*!< Frame Waiting Integer [4b] | Application Data Coding + [2b] | Frame Options [2b] */ + uint8_t + SFGI; /*!< Optional: Start-Up Frame Guard Time Integer[4b] | RFU [4b] */ +} rfalNfcbSensbResProtocolInfo; + +/*! SENSB_RES format Digital 1.1 7.6.2 */ +typedef struct { + uint8_t cmd; /*!< SENSB_RES: 50h */ + uint8_t nfcid0[RFAL_NFCB_NFCID0_LEN]; /*!< NFC Identifier (PUPI)*/ + rfalNfcbSensbResAppData appData; /*!< Application Data */ + rfalNfcbSensbResProtocolInfo protInfo; /*!< Protocol Information */ +} rfalNfcbSensbRes; + +/*! NFC-B listener device (PICC) struct */ +typedef struct { + uint8_t sensbResLen; /*!< SENSB_RES length */ + rfalNfcbSensbRes sensbRes; /*!< SENSB_RES */ + bool isSleep; /*!< Device sleeping flag */ +} rfalNfcbListenDevice; + +/* +****************************************************************************** +* GLOBAL FUNCTION PROTOTYPES +****************************************************************************** +*/ + +/*! + ***************************************************************************** + * \brief Initialize NFC-B Poller mode + * + * This methods configures RFAL RF layer to perform as a + * NFC-B Poller/RW (ISO14443B PCD) including all default timings + * + * It sets NFC-B parameters (AFI, PARAM) to default values + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or mode not set + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalNfcbPollerInitialize(void); + +/*! + ***************************************************************************** + * \brief Set NFC-B Poller parameters + * + * This methods configures RFAL RF layer to perform as a + * NFCA Poller/RW (ISO14443A PCD) including all default timings + * + * Additionally configures NFC-B specific parameters to be used on the + * following communications + * + * \param[in] AFI : Application Family Identifier to be used + * \param[in] PARAM : PARAM to be used, it announces whether Advanced + * Features or Extended SENSB_RES is supported + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or mode not set + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalNfcbPollerInitializeWithParams(uint8_t AFI, uint8_t PARAM); + +/*! + ***************************************************************************** + * \brief NFC-B Poller Check Presence + * + * This method checks if a NFC-B Listen device (PICC) is present on the field + * by sending an ALLB_REQ (WUPB) or SENSB_REQ (REQB) + * + * \param[in] cmd : Indicate if to send an ALLB_REQ or a SENSB_REQ + * \param[in] slots : The number of slots to be announced + * \param[out] sensbRes : If received, the SENSB_RES + * \param[out] sensbResLen : If received, the SENSB_RES length + * + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_TIMEOUT : Timeout error, no listener device detected + * \return RFAL_ERR_RF_COLLISION : Collision detected one or more device in the + *field \return RFAL_ERR_PAR : Parity error detected, one or more + *device in the field \return RFAL_ERR_CRC : CRC error detected, one or + *more device in the field \return RFAL_ERR_FRAMING : Framing error + *detected, one or more device in the field \return RFAL_ERR_PROTO : + *Protocol error detected, invalid SENSB_RES received \return RFAL_ERR_NONE : No + *error, SENSB_RES received + ***************************************************************************** + */ +ReturnCode rfalNfcbPollerCheckPresence(rfalNfcbSensCmd cmd, rfalNfcbSlots slots, + rfalNfcbSensbRes *sensbRes, + uint8_t *sensbResLen); + +/*! + ***************************************************************************** + * \brief NFC-B Poller Start Check Presence + * + * This method starts check for a NFC-B Listen device (PICC) presence on the + *field by sending an ALLB_REQ (WUPB) or SENSB_REQ (REQB) + * + * \param[in] cmd : Indicate if to send an ALL_REQ or a SENS_REQ + * \param[in] slots : The number of slots to be announced + * \param[out] sensbRes : If received, the SENSB_RES + * \param[out] sensbResLen : If received, the SENSB_RES length + * + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_NONE : No error, SENSB_RES received + ***************************************************************************** + */ +ReturnCode rfalNfcbPollerStartCheckPresence(rfalNfcbSensCmd cmd, + rfalNfcbSlots slots, + rfalNfcbSensbRes *sensbRes, + uint8_t *sensbResLen); + +/*! + ***************************************************************************** + * \brief NFC-B Poller Get Check Presence Status + * + * This method get the presence check status + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_TIMEOUT : Timeout error, no listener device detected + * \return RFAL_ERR_RF_COLLISION : Collision detected one or more device in the + *field \return RFAL_ERR_PAR : Parity error detected, one or more + *device in the field \return RFAL_ERR_CRC : CRC error detected, one or + *more device in the field \return RFAL_ERR_FRAMING : Framing error + *detected, one or more device in the field \return RFAL_ERR_PROTO : + *Protocol error detected, invalid SENSB_RES received \return RFAL_ERR_NONE : No + *error, SENSB_RES received + ***************************************************************************** + */ +ReturnCode rfalNfcbPollerGetCheckPresenceStatus(void); + +/*! + ***************************************************************************** + * \brief NFC-B Poller Sleep + * + * This function is used to send the SLPB_REQ (HLTB) command to put the PICC + *with the given NFCID0 to state HALT so that they do not reply to further + *SENSB_REQ commands (only to ALLB_REQ) + * + * \param[in] nfcid0 : NFCID of the device to be put to Sleep + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalNfcbPollerSleep(const uint8_t *nfcid0); + +/*! + ***************************************************************************** + * \brief NFC-B Poller Slot Marker + * + * This method sends a NFC-B Slot marker frame + * + * \param[in] slotCode : Slot Code [1-15] + * \param[out] sensbRes : If received, the SENSB_RES + * \param[out] sensbResLen : If received, the SENSB_RES length + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_TIMEOUT : Timeout error + * \return RFAL_ERR_PAR : Parity error detected + * \return RFAL_ERR_CRC : CRC error detected + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_NONE : No error, SEL_RES received + ***************************************************************************** + */ +ReturnCode rfalNfcbPollerSlotMarker(uint8_t slotCode, + rfalNfcbSensbRes *sensbRes, + uint8_t *sensbResLen); + +/*! + ***************************************************************************** + * \brief NFC-B Poller Start Slot Marker + * + * This method starts a NFC-B Slot marker + * + * \param[in] slotCode : Slot Code [1-15] + * \param[out] sensbRes : If received, the SENSB_RES + * \param[out] sensbResLen : If received, the SENSB_RES length + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_NONE : No error, SEL_RES received + ***************************************************************************** + */ +ReturnCode rfalNfcbPollerStartSlotMarker(uint8_t slotCode, + rfalNfcbSensbRes *sensbRes, + uint8_t *sensbResLen); + +/*! + ***************************************************************************** + * \brief NFC-B Poller Get Slot Marker Status + * + * This method gets the status of the NFC-B Slot marker + * + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_TIMEOUT : Timeout error + * \return RFAL_ERR_PAR : Parity error detected + * \return RFAL_ERR_CRC : CRC error detected + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_NONE : No error, SEL_RES received + ***************************************************************************** + */ +ReturnCode rfalNfcbPollerGetSlotMarkerStatus(void); + +/*! + ***************************************************************************** + * \brief NFC-B Technology Detection + * + * This method performs NFC-B Technology Detection as defined in the spec + * given in the compliance mode + * + * \param[in] compMode : compliance mode to be performed + * \param[out] sensbRes : location to store the SENSB_RES, if received + * \param[out] sensbResLen : length of the SENSB_RES, if received + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_NONE : No error, one or more device in the field + ***************************************************************************** + */ +ReturnCode rfalNfcbPollerTechnologyDetection(rfalComplianceMode compMode, + rfalNfcbSensbRes *sensbRes, + uint8_t *sensbResLen); + +/*! + ***************************************************************************** + * \brief NFC-B Start Technology Detection + * + * This method starts the NFC-B Technology Detection as defined in the spec + * given in the compliance mode + * + * \param[in] compMode : compliance mode to be performed + * \param[out] sensbRes : location to store the SENSB_RES, if received + * \param[out] sensbResLen : length of the SENSB_RES, if received + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_NONE : No error, one or more device in the field + ***************************************************************************** + */ +ReturnCode rfalNfcbPollerStartTechnologyDetection(rfalComplianceMode compMode, + rfalNfcbSensbRes *sensbRes, + uint8_t *sensbResLen); + +/*! + ***************************************************************************** + * \brief NFC-B Get Technology Detection Status + * + * This method gets the NFC-B Technology Detection status + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_NONE : No error, one or more device in the field + ***************************************************************************** + */ +ReturnCode rfalNfcbPollerGetTechnologyDetectionStatus(void); + +/*! + ***************************************************************************** + * \brief NFC-B Poller Collision Resolution + * + * NFC-B Collision resolution Listener device/card (PICC) as + * defined in Activity 1.1 9.3.5 + * + * This function is used to perform collision resolution for detection in case + * of multiple NFC Forum Devices with Technology B detected. + * Target with valid SENSB_RES will be stored in nfcbDevList and devCnt + *incremented. + * + * \param[in] compMode : compliance mode to be performed + * \param[in] devLimit : device limit value, and size nfcbDevList + * \param[out] nfcbDevList : NFC-B listener device info + * \param[out] devCnt : devices found counter + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or mode not set + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalNfcbPollerCollisionResolution(rfalComplianceMode compMode, + uint8_t devLimit, + rfalNfcbListenDevice *nfcbDevList, + uint8_t *devCnt); + +/*! + ***************************************************************************** + * \brief NFC-B Poller Collision Resolution Slotted + * + * NFC-B Collision resolution Listener device/card (PICC). The sequence can + * be configured to be according to NFC Forum Activity 1.1 9.3.5, ISO10373 + * or EMVCo + * + * This function is used to perform collision resolution for detection in case + * of multiple NFC Forum Devices with Technology B are detected. + * Target with valid SENSB_RES will be stored in nfcbDevList and devCnt + *incremented. + * + * This method provides the means to perform a collision resolution loop with + *specific initial and end number of slots. This allows to user to start the + *loop already with greater number of slots, and or limit the end number of + *slots. At the end a flag indicating whether there were collisions pending is + *returned. + * + * If RFAL_COMPLIANCE_MODE_ISO is used \a initSlots must be set to + *RFAL_NFCB_SLOT_NUM_1 + * + * + * \param[in] compMode : compliance mode to be performed + * \param[in] devLimit : device limit value, and size nfcbDevList + * \param[in] initSlots : number of slots to open initially + * \param[in] endSlots : number of slots when to stop collision resolution + * \param[out] nfcbDevList : NFC-B listener device info + * \param[out] devCnt : devices found counter + * \param[out] colPending : flag indicating whether collision are still pending + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or mode not set + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalNfcbPollerSlottedCollisionResolution( + rfalComplianceMode compMode, uint8_t devLimit, rfalNfcbSlots initSlots, + rfalNfcbSlots endSlots, rfalNfcbListenDevice *nfcbDevList, uint8_t *devCnt, + bool *colPending); + +/*! + ***************************************************************************** + * \brief NFC-B Poller Start Collision Resolution + * + * It starts the NFC-B Collision resolution Listener device/card (PICC) as + * defined in Activity 1.1 9.3.5 + * + * This function is used to trigger the collision resolution for detection in + *case of multiple NFC Forum Devices with Technology B detected. Target with + *valid SENSB_RES will be stored in nfcbDevList and devCnt incremented. + * + * \param[in] compMode : compliance mode to be performed + * \param[in] devLimit : device limit value, and size nfcbDevList + * \param[out] nfcbDevList : NFC-B listener device info + * \param[out] devCnt : devices found counter + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or mode not set + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalNfcbPollerStartCollisionResolution( + rfalComplianceMode compMode, uint8_t devLimit, + rfalNfcbListenDevice *nfcbDevList, uint8_t *devCnt); + +/*! + ***************************************************************************** + * \brief NFC-B Poller Start Collision Resolution Slotted + * + * Starts NFC-B Collision resolution Listener device/card (PICC). The sequence + *can be configured to be according to NFC Forum Activity 1.1 9.3.5, ISO10373 + * or EMVCo + * + * This function is used to trigger the collision resolution for detection in + *case of multiple NFC Forum Devices with Technology B are detected. Target with + *valid SENSB_RES will be stored in nfcbDevList and devCnt incremented. + * + * This method provides the means to perform a collision resolution loop with + *specific initial and end number of slots. This allows to user to start the + *loop already with greater number of slots, and or limit the end number of + *slots. At the end a flag indicating whether there were collisions pending is + *returned. + * + * If RFAL_COMPLIANCE_MODE_ISO is used \a initSlots must be set to + *RFAL_NFCB_SLOT_NUM_1 + * + * + * \param[in] compMode : compliance mode to be performed + * \param[in] devLimit : device limit value, and size nfcbDevList + * \param[in] initSlots : number of slots to open initially + * \param[in] endSlots : number of slots when to stop collision resolution + * \param[out] nfcbDevList : NFC-B listener device info + * \param[out] devCnt : devices found counter + * \param[out] colPending : flag indicating whether collision are still pending + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or mode not set + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalNfcbPollerStartSlottedCollisionResolution( + rfalComplianceMode compMode, uint8_t devLimit, rfalNfcbSlots initSlots, + rfalNfcbSlots endSlots, rfalNfcbListenDevice *nfcbDevList, uint8_t *devCnt, + bool *colPending); + +/*! + ***************************************************************************** + * \brief NFC-B Get Collision Resolution Status + * + * Returns the Collision Resolution status + * + * \return RFAL_ERR_BUSY : Operation is ongoing + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_TIMEOUT : Timeout error + * \return RFAL_ERR_PAR : Parity error detected + * \return RFAL_ERR_CRC : CRC error detected + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_NONE : No error, activation successful + ***************************************************************************** + */ +ReturnCode rfalNfcbPollerGetCollisionResolutionStatus(void); + +/*! + ***************************************************************************** + * \brief NFC-B TR2 code to FDT + * + * Converts the TR2 code as defined in Digital 1.1 Table 33 Minimum + * TR2 Coding to Frame Delay Time (FDT) in 1/Fc + * + * \param[in] tr2Code : TR2 code as defined in Digital 1.1 Table 33 + * + * \return FDT in 1/Fc + ***************************************************************************** + */ +uint32_t rfalNfcbTR2ToFDT(uint8_t tr2Code); + +#endif /* RFAL_NFCB_H */ + +/** + * @} + * + * @} + * + * @} + */ diff --git a/core/embed/io/nfc/rfal/include/rfal_nfcf.h b/core/embed/io/nfc/rfal/include/rfal_nfcf.h new file mode 100644 index 0000000000..ce1673a402 --- /dev/null +++ b/core/embed/io/nfc/rfal/include/rfal_nfcf.h @@ -0,0 +1,508 @@ + +/****************************************************************************** + * @attention + * + * COPYRIGHT 2016 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/* + * PROJECT: ST25R391x firmware + * Revision: + * LANGUAGE: ISO C99 + */ + +/*! \file rfal_nfcf.h + * + * \author Gustavo Patricio + * + * \brief Implementation of NFC-F Poller (FeliCa PCD) device + * + * The definitions and helpers methods provided by this module are + * aligned with NFC-F (FeliCa - JIS X6319-4) + * + * + * \addtogroup RFAL + * @{ + * + * \addtogroup RFAL-AL + * \brief RFAL Abstraction Layer + * @{ + * + * \addtogroup NFC-F + * \brief RFAL NFC-F Module + * @{ + * + */ + +#ifndef RFAL_NFCF_H +#define RFAL_NFCF_H + +/* + ****************************************************************************** + * INCLUDES + ****************************************************************************** + */ +#include "rfal_platform.h" +#include "rfal_rf.h" +#include "rfal_utils.h" + +/* + ****************************************************************************** + * GLOBAL DEFINES + ****************************************************************************** + */ + +#define RFAL_NFCF_NFCID2_LEN \ + 8U /*!< NFCID2 (FeliCa IDm) length */ +#define RFAL_NFCF_SENSF_RES_LEN_MIN \ + 16U /*!< SENSF_RES minimum length */ +#define RFAL_NFCF_SENSF_RES_LEN_MAX \ + 18U /*!< SENSF_RES maximum length */ +#define RFAL_NFCF_SENSF_RES_PAD0_LEN \ + 2U /*!< SENSF_RES PAD0 length */ +#define RFAL_NFCF_SENSF_RES_PAD1_LEN \ + 3U /*!< SENSF_RES PAD1 length */ +#define RFAL_NFCF_SENSF_RES_RD_LEN \ + 2U /*!< SENSF_RES Request Data length */ +#define RFAL_NFCF_SENSF_RES_BYTE1 \ + 1U /*!< SENSF_RES first byte value */ +#define RFAL_NFCF_SENSF_SC_LEN \ + 2U /*!< Felica SENSF_REQ System Code length */ +#define RFAL_NFCF_SENSF_PARAMS_SC1_POS \ + 0U /*!< System Code byte1 position in the SENSF_REQ */ +#define RFAL_NFCF_SENSF_PARAMS_SC2_POS \ + 1U /*!< System Code byte2 position in the SENSF_REQ */ +#define RFAL_NFCF_SENSF_PARAMS_RC_POS \ + 2U /*!< Request Code position in the SENSF_REQ */ +#define RFAL_NFCF_SENSF_PARAMS_TSN_POS \ + 3U /*!< Time Slot Number position in the SENSF_REQ */ +#define RFAL_NFCF_POLL_MAXCARDS \ + 16U /*!< Max number slots/cards 16 */ + +#define RFAL_NFCF_CMD_POS \ + 0U /*!< Command/Responce code length */ +#define RFAL_NFCF_CMD_LEN \ + 1U /*!< Command/Responce code length */ +#define RFAL_NFCF_LENGTH_LEN \ + 1U /*!< LEN field length */ +#define RFAL_NFCF_HEADER_LEN \ + (RFAL_NFCF_LENGTH_LEN + RFAL_NFCF_CMD_LEN) /*!< Header length */ + +#define RFAL_NFCF_NOS_LEN \ + 1U /*!< Number of Services length */ +#define RFAL_NFCF_NOB_LEN \ + 1U /*!< Number of Blocks length */ + +#define RFAL_NFCF_SENSF_NFCID2_BYTE1_POS \ + 0U /*!< NFCID2 byte1 position */ +#define RFAL_NFCF_SENSF_NFCID2_BYTE2_POS \ + 1U /*!< NFCID2 byte2 position */ + +#define RFAL_NFCF_SENSF_NFCID2_PROT_TYPE_LEN \ + 2U /*!< NFCID2 length for byte 1 and byte 2 indicating NFC-DEP or T3T \ + support */ +#define RFAL_NFCF_SENSF_NFCID2_BYTE1_NFCDEP \ + 0x01U /*!< NFCID2 byte1 NFC-DEP support Digital 1.0 Table 44 */ +#define RFAL_NFCF_SENSF_NFCID2_BYTE2_NFCDEP \ + 0xFEU /*!< NFCID2 byte2 NFC-DEP support Digital 1.0 Table 44 */ + +#define RFAL_NFCF_SYSTEMCODE \ + 0xFFFFU /*!< SENSF_RES Default System Code Digital 2.3 8.6.1.5 */ +#define RFAL_NFCF_SYSTEMCODE_LEN \ + 2U /*!< SENSF_RES System Code length Digital 2.3 8.6.1 */ + +#define RFAL_NFCF_BLOCK_LEN \ + 16U /*!< NFCF T3T Block size T3T 1.0 4.1 */ +#define RFAL_NFCF_CHECKUPDATE_RES_ST1_POS \ + 9U /*!< Check|Update Res Status Flag 1 position T3T 1.0 Table 8 */ +#define RFAL_NFCF_CHECKUPDATE_RES_ST2_POS \ + 10U /*!< Check|Update Res Status Flag 2 position T3T 1.0 Table 8 */ +#define RFAL_NFCF_CHECKUPDATE_RES_NOB_POS \ + 11U /*!< Check|Update Res Number of Blocks position T3T 1.0 Table 8 */ + +#define RFAL_NFCF_STATUS_FLAG_SUCCESS \ + 0x00U /*!< Check response Number of Blocks position T3T 1.0 Table 11 */ +#define RFAL_NFCF_STATUS_FLAG_ERROR \ + 0xFFU /*!< Check response Number of Blocks position T3T 1.0 Table 11 */ + +#define RFAL_NFCF_BLOCKLISTELEM_MAX_LEN \ + 3U /*!< Block List Element max Length (3 bytes) T3T 1.0 5.6.1 */ +#define RFAL_NFCF_BLOCKLISTELEM_LEN_BIT \ + 0x80U /*!< Block List Element Length bit (2|3 bytes) T3T 1.0 5.6.1 */ + +#define RFAL_NFCF_SERVICECODE_RDONLY \ + 0x000BU /*!< NDEF Service Code as Read-Only T3T 1.0 7.2.1 */ +#define RFAL_NFCF_SERVICECODE_RDWR \ + 0x0009U /*!< NDEF Service Code as Read and Write T3T 1.0 7.2.1 */ + +#define RFAL_NFCF_TEST_LB_CMD0 \ + 0xD8U /*!< T3T loopback CMD0 ETSI TS 102 695-1 5.6.4.4.2 */ +#define RFAL_NFCF_TEST_LB_CMD1 \ + 0x00U /*!< T3T loopback CMD1 ETSI TS 102 695-1 5.6.4.4.2 */ + +/*! NFC-F Felica command set JIS X6319-4 9.1 */ +enum { + RFAL_NFCF_CMD_POLLING = + 0x00, /*!< SENSF_REQ (Felica Poll/REQC command to identify a card ) */ + RFAL_NFCF_CMD_POLLING_RES = + 0x01, /*!< SENSF_RES (Felica Poll/REQC command response ) */ + RFAL_NFCF_CMD_REQUEST_SERVICE = + 0x02, /*!< verify the existence of Area and Service */ + RFAL_NFCF_CMD_REQUEST_RESPONSE = 0x04, /*!< verify the existence of a card */ + RFAL_NFCF_CMD_READ_WITHOUT_ENCRYPTION = + 0x06, /*!< read Block Data from a Service that requires no authentication + */ + RFAL_NFCF_CMD_READ_WITHOUT_ENCRYPTION_RES = + 0x07, /*!< read Block Data response from a Service with no authentication + */ + RFAL_NFCF_CMD_WRITE_WITHOUT_ENCRYPTION = + 0x08, /*!< write Block Data to a Service that requires no authentication + */ + RFAL_NFCF_CMD_WRITE_WITHOUT_ENCRYPTION_RES = + 0x09, /*!< write Block Data response to a Service with no authentication + */ + RFAL_NFCF_CMD_REQUEST_SYSTEM_CODE = + 0x0c, /*!< acquire the System Code registered to a card */ + RFAL_NFCF_CMD_AUTHENTICATION1 = 0x10, /*!< authenticate a card */ + RFAL_NFCF_CMD_AUTHENTICATION2 = + 0x12, /*!< allow a card to authenticate a Reader/Writer */ + RFAL_NFCF_CMD_READ = + 0x14, /*!< read Block Data from a Service that requires authentication */ + RFAL_NFCF_CMD_WRITE = + 0x16, /*!< write Block Data to a Service that requires authentication */ +}; + +/* + ****************************************************************************** + * GLOBAL MACROS + ****************************************************************************** + */ + +/*! Checks if the given NFC-F device indicates NFC-DEP support */ +#define rfalNfcfIsNfcDepSupported(dev) \ + ((((rfalNfcfListenDevice *)(dev)) \ + ->sensfRes.NFCID2[RFAL_NFCF_SENSF_NFCID2_BYTE1_POS] == \ + RFAL_NFCF_SENSF_NFCID2_BYTE1_NFCDEP) && \ + (((rfalNfcfListenDevice *)(dev)) \ + ->sensfRes.NFCID2[RFAL_NFCF_SENSF_NFCID2_BYTE2_POS] == \ + RFAL_NFCF_SENSF_NFCID2_BYTE2_NFCDEP)) + +/* +****************************************************************************** +* GLOBAL TYPES +****************************************************************************** +*/ + +/*! NFC-F SENSF_RES format Digital 1.1 8.6.2 */ +typedef struct { + uint8_t CMD; /*!< Command Code: 01h */ + uint8_t NFCID2[RFAL_NFCF_NFCID2_LEN]; /*!< NFCID2 */ + uint8_t PAD0[RFAL_NFCF_SENSF_RES_PAD0_LEN]; /*!< PAD0 */ + uint8_t PAD1[RFAL_NFCF_SENSF_RES_PAD1_LEN]; /*!< PAD1 */ + uint8_t MRTIcheck; /*!< MRTIcheck */ + uint8_t MRTIupdate; /*!< MRTIupdate */ + uint8_t PAD2; /*!< PAD2 */ + uint8_t RD[RFAL_NFCF_SENSF_RES_RD_LEN]; /*!< Request Data */ +} rfalNfcfSensfRes; + +/*! NFC-F poller device (PCD) struct */ +typedef struct { + uint8_t NFCID2[RFAL_NFCF_NFCID2_LEN]; /*!< NFCID2 */ +} rfalNfcfPollDevice; + +/*! NFC-F listener device (PICC) struct */ +typedef struct { + uint8_t sensfResLen; /*!< SENF_RES length */ + rfalNfcfSensfRes sensfRes; /*!< SENF_RES */ +} rfalNfcfListenDevice; + +typedef uint16_t rfalNfcfServ; /*!< NFC-F Service Code */ + +/*! NFC-F Block List Element (2 or 3 bytes element) T3T 1.0 5.6.1 */ +typedef struct { + uint8_t conf; /*!< Access Mode | Serv Code List Order */ + uint16_t blockNum; /*!< Block Number */ +} rfalNfcfBlockListElem; + +/*! Check Update Service list and Block list parameter */ +typedef struct { + uint8_t numServ; /*!< Number of Services */ + rfalNfcfServ *servList; /*!< Service Code List */ + uint8_t numBlock; /*!< Number of Blocks */ + rfalNfcfBlockListElem *blockList; /*!< Block Number List */ +} rfalNfcfServBlockListParam; + +/* +****************************************************************************** +* GLOBAL FUNCTION PROTOTYPES +****************************************************************************** +*/ + +/*! + ***************************************************************************** + * \brief Initialize NFC-F Poller mode + * + * This methods configures RFAL RF layer to perform as a + * NFC-F Poller/RW (FeliCa PCD) including all default timings + * + * \param[in] bitRate : NFC-F bitrate to be initialize (212 or 424) + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or mode not set + * \return RFAL_ERR_PARAM : Incorrect bitrate + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalNfcfPollerInitialize(rfalBitRate bitRate); + +/*! + ***************************************************************************** + * \brief NFC-F Poller Check Presence + * + * This function sends a Poll/SENSF command according to NFC Activity spec + * It detects if a NCF-F device is within range + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_CRC : CRC error detected + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_TIMEOUT : Timeout error, no listener device detected + * \return RFAL_ERR_NONE : No error and some NFC-F device was detected + * + ***************************************************************************** + */ +ReturnCode rfalNfcfPollerCheckPresence(void); + +/*! + ***************************************************************************** + * \brief NFC-F Poller Start Check Presence + * + * This function triggers a Poll/SENSF command according to NFC Activity spec + * It detects if a NCF-F device is within range + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_NONE : No error and some NFC-F device was detected + * + ***************************************************************************** + */ +ReturnCode rfalNfcfPollerStartCheckPresence(void); + +/*! + ***************************************************************************** + * \brief NFC-F Poller Get Check Presence Status + * + * This function gets the status of the Check Presense operation + * triggered by rfalNfcfPollerStartCheckPresence() + * + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_CRC : CRC error detected + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_TIMEOUT : Timeout error, no listener device detected + * \return RFAL_ERR_NONE : No error and some NFC-F device was detected + * + ***************************************************************************** + */ +ReturnCode rfalNfcfPollerGetCheckPresenceStatus(void); + +/*! + ***************************************************************************** + * \brief NFC-F Poller Poll + * + * This function sends to all PICCs in field the POLL command with the given + * number of slots. + * + * \param[in] slots : the number of slots to be performed + * \param[in] sysCode : as given in FeliCa poll command + * \param[in] reqCode : FeliCa communication parameters + * \param[out] cardList : Parameter of type rfalFeliCaPollRes which will hold + *the cards found \param[out] devCnt : actual number of cards found + * \param[out] collisions : number of collisions encountered + * + * \warning the list cardList has to be as big as the number of slots for the + *Poll + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_CRC : CRC error detected + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_TIMEOUT : Timeout error, no listener device detected + * \return RFAL_ERR_NONE : No error and some NFC-F device was detected + * + ***************************************************************************** + */ +ReturnCode rfalNfcfPollerPoll(rfalFeliCaPollSlots slots, uint16_t sysCode, + uint8_t reqCode, rfalFeliCaPollRes *cardList, + uint8_t *devCnt, uint8_t *collisions); + +/*! + ***************************************************************************** + * \brief NFC-F Poller Full Collision Resolution + * + * Performs a full Collision resolution as defined in Activity 1.1 9.3.4 + * + * \param[in] compMode : compliance mode to be performed + * \param[in] devLimit : device limit value, and size nfcaDevList + * \param[out] nfcfDevList : NFC-F listener devices list + * \param[out] devCnt : Devices found counter + * + * NFC-F Collision Resolution uses information collected at Technlology + *dectection (if provided on nfcfDevList). Technology Detection shall be + *performed using 4 slots, therefore at least 4 slots/devLimit shall be + *allocated on nfcfDevList. + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or mode not set + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalNfcfPollerCollisionResolution(rfalComplianceMode compMode, + uint8_t devLimit, + rfalNfcfListenDevice *nfcfDevList, + uint8_t *devCnt); + +/*! + ***************************************************************************** + * \brief NFC-F Start Poller Collision Resolution + * + * Triggers a Collision Resolution as defined in Activity 2.1 9.3.6 + * + * NFC-F Collision Resolution uses information collected at Technlology + *dectection (if provided on nfcfDevList). Technology Detection shall be + *performed using 4 slots, therefore at least 4 slots/devLimit shall be + *allocated on nfcfDevList. + * + * \param[in] compMode : compliance mode to be performed + * \param[in] devLimit : device limit value, and size nfcaDevList + * \param[out] nfcfDevList : NFC-F listener devices list + * \param[out] devCnt : Devices found counter + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or mode not set + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalNfcfPollerStartCollisionResolution( + rfalComplianceMode compMode, uint8_t devLimit, + rfalNfcfListenDevice *nfcfDevList, uint8_t *devCnt); + +/*! + ***************************************************************************** + * \brief NFC-F Poller Get Collision Resolution Status + * + * This function gets the status of the Collision Resolution operation + * triggered by rfalNfcfPollerStartCollisionResolution() + * + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_CRC : CRC error detected + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_TIMEOUT : Timeout error, no listener device detected + * \return RFAL_ERR_NONE : No error and some NFC-F device was detected + * + ***************************************************************************** + */ +ReturnCode rfalNfcfPollerGetCollisionResolutionStatus(void); + +/*! + ***************************************************************************** + * \brief NFC-F Poller Check/Read + * + * It computes a Check / Read command accoring to T3T 1.0 and JIS X6319-4 and + * sends it to PICC. If sucessfully, the rxBuf will contain the the number of + * blocks in the first byte followed by the blocks data. + * + * \param[in] nfcid2 : nfcid2 of the device + * \param[in] servBlock : parameter containing the list of Services and + * Blocks to be addressed by this command + * \param[out] rxBuf : buffer to place check/read data + * \param[in] rxBufLen : size of the rxBuf + * \param[out] rcvdLen : length of data placed in rxBuf + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or mode not set + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_REQUEST : The request was executed with error + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalNfcfPollerCheck(const uint8_t *nfcid2, + const rfalNfcfServBlockListParam *servBlock, + uint8_t *rxBuf, uint16_t rxBufLen, + uint16_t *rcvdLen); + +/*! + ***************************************************************************** + * \brief NFC-F Poller Update/Write + * + * It computes a Update / Write command accoring to T3T 1.0 and JIS X6319-4 and + * sends it to PICC. + * + * \param[in] nfcid2 : nfcid2 of the device + * \param[in] servBlock : parameter containing the list of Services and + * Blocks to be addressed by this command + * \param[in] txBuf : buffer where the request will be composed + * \param[in] txBufLen : size of txBuf + * \param[in] blockData : data to written on the given block(s) + * \param[out] rxBuf : buffer to place check/read data + * \param[in] rxBufLen : size of the rxBuf + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or mode not set + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_REQUEST : The request was executed with error + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalNfcfPollerUpdate(const uint8_t *nfcid2, + const rfalNfcfServBlockListParam *servBlock, + uint8_t *txBuf, uint16_t txBufLen, + const uint8_t *blockData, uint8_t *rxBuf, + uint16_t rxBufLen); + +/*! + ***************************************************************************** + * \brief NFC-F Listener is T3T Request + * + * This method checks if the given data is a valid T3T command (Read or Write) + * and in case a valid request has been received it may output the request's + *NFCID2 + * + * \param[in] buf : buffer holding Initiator's received command + * \param[in] bufLen : length of received command in bytes + * \param[out] nfcid2 : pointer to where the NFCID2 may be outputed, + * nfcid2 has NFCF_SENSF_NFCID2_LEN as length + * Pass NULL if output parameter not desired + * + * \return true : Valid T3T command (Read or Write) received + * \return false : Invalid protocol request + * + ***************************************************************************** + */ +bool rfalNfcfListenerIsT3TReq(const uint8_t *buf, uint16_t bufLen, + uint8_t *nfcid2); + +#endif /* RFAL_NFCF_H */ + +/** + * @} + * + * @} + * + * @} + */ diff --git a/core/embed/io/nfc/rfal/include/rfal_nfcv.h b/core/embed/io/nfc/rfal/include/rfal_nfcv.h new file mode 100644 index 0000000000..7f8a026b11 --- /dev/null +++ b/core/embed/io/nfc/rfal/include/rfal_nfcv.h @@ -0,0 +1,828 @@ + +/****************************************************************************** + * @attention + * + * COPYRIGHT 2016 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/* + * PROJECT: ST25R391x firmware + * Revision: + * LANGUAGE: ISO C99 + */ + +/*! \file rfal_nfcv.h + * + * \author Gustavo Patricio + * + * \brief Implementation of NFC-V Poller (ISO15693) device + * + * The definitions and helpers methods provided by this module + * are aligned with NFC-V Digital 2.1 + * + * + * \addtogroup RFAL + * @{ + * + * \addtogroup RFAL-AL + * \brief RFAL Abstraction Layer + * @{ + * + * \addtogroup NFC-V + * \brief RFAL NFC-V Module + * @{ + * + */ + +#ifndef RFAL_NFCV_H +#define RFAL_NFCV_H + +/* + ****************************************************************************** + * INCLUDES + ****************************************************************************** + */ +#include "rfal_platform.h" +#include "rfal_rf.h" +#include "rfal_utils.h" + +/* + ****************************************************************************** + * GLOBAL DEFINES + ****************************************************************************** + */ + +#define RFAL_NFCV_UID_LEN \ + 8U /*!< NFC-V UID length */ +#define RFAL_NFCV_MAX_BLOCK_LEN \ + 32U /*!< Max Block size: can be of up to 256 bits ISO 15693 2000 5 */ +#define RFAL_NFCV_BNO_LEN \ + 1U /*!< NFC-V Block Number length */ +#define RFAL_NFCV_CRC_LEN \ + 2U /*!< NFC-V CRC length */ +#define RFAL_NFCV_MAX_GEN_DATA_LEN \ + (RFAL_NFCV_MAX_BLOCK_LEN + RFAL_NFCV_BNO_LEN + \ + RFAL_NFCV_UID_LEN) /*!The RFAL encapsulates the different + * RF ICs (ST25R3911, ST25R391x, etc) into a common and easy to use interface. + * + * It provides interfaces to configure the RF IC, set/get timings, modes, bit + * rates, specific handlings, execute listen mode, etc. + * + * Furthermore it provides a common interface to perform a Transceive + * operations. The Transceive can be executed in a blocking or non blocking + * way.
Additionally few specific Transceive methods are available to cope + * with the specifics of these particular operations. + * + * The most common interfaces are: + *
  rfalInitialize() + *
  rfalSetFDTPoll() + *
  rfalSetFDTListen() + *
  rfalSetGT() + *
  rfalSetBitRate() + *
  rfalSetMode() + *
  rfalFieldOnAndStartGT() + *
  rfalFieldOff() + *
  rfalStartTransceive() + *
  rfalGetTransceiveStatus() + *
  rfalTransceiveBlockingTxRx() + * + * An usage example is provided here: \ref exampleRfalPoller.c + * \example exampleRfalPoller.c + * + * \addtogroup RFAL + * @{ + * + * \addtogroup RFAL-HAL + * \brief RFAL Hardware Abstraction Layer + * @{ + * + * \addtogroup RF + * \brief RFAL RF Abstraction Layer + * @{ + * + */ + +#ifndef RFAL_RF_H +#define RFAL_RF_H + +/* +****************************************************************************** +* INCLUDES +****************************************************************************** +*/ +#include "rfal_features.h" +#include "rfal_platform.h" +#include "rfal_utils.h" + +/* +****************************************************************************** +* GLOBAL DEFINES +****************************************************************************** +*/ +#define RFAL_VERSION \ + 0x030001U /*!< RFAL Current Version: v3.0.1 */ + +#define RFAL_FWT_NONE \ + 0xFFFFFFFFU /*!< Disabled FWT: Wait forever for a response */ +#define RFAL_GT_NONE \ + RFAL_TIMING_NONE /*!< Disabled GT: No GT will be applied after Field On */ + +#define RFAL_TIMING_NONE \ + 0x00U /*!< Timing disabled | Don't apply */ + +#define RFAL_1FC_IN_4096FC \ + (uint32_t)4096U /*!< Number of 1/fc cycles in one 4096/fc */ +#define RFAL_1FC_IN_2048FC \ + (uint32_t)2048U /*!< Number of 1/fc cycles in one 2048/fc */ +#define RFAL_1FC_IN_512FC \ + (uint32_t)512U /*!< Number of 1/fc cycles in one 512/fc */ +#define RFAL_1FC_IN_64FC \ + (uint32_t)64U /*!< Number of 1/fc cycles in one 64/fc */ +#define RFAL_1FC_IN_8FC \ + (uint32_t)8U /*!< Number of 1/fc cycles in one 8/fc */ +#define RFAL_US_IN_MS \ + (uint32_t)1000U /*!< Number of us in one ms */ +#define RFAL_1MS_IN_1FC \ + (uint32_t)13560U /*!< Number of 1/fc cycles in 1ms */ +#define RFAL_BITS_IN_BYTE \ + (uint16_t)8U /*!< Number of bits in one byte */ + +#define RFAL_CRC_LEN \ + 2U /*!< RF CRC LEN */ + +/*! Default TxRx flags: Tx CRC automatic, Rx CRC removed, NFCIP1 mode off, AGC + * On, Tx Parity automatic, Rx Parity removed */ +#define RFAL_TXRX_FLAGS_DEFAULT \ + ((uint32_t)RFAL_TXRX_FLAGS_CRC_TX_AUTO | \ + (uint32_t)RFAL_TXRX_FLAGS_CRC_RX_REMV | \ + (uint32_t)RFAL_TXRX_FLAGS_NFCIP1_OFF | (uint32_t)RFAL_TXRX_FLAGS_AGC_ON | \ + (uint32_t)RFAL_TXRX_FLAGS_PAR_RX_REMV | \ + (uint32_t)RFAL_TXRX_FLAGS_PAR_TX_AUTO | \ + (uint32_t)RFAL_TXRX_FLAGS_NFCV_FLAG_AUTO) + +#define RFAL_LM_MASK_NFCA \ + ((uint32_t)1U << (uint8_t) \ + RFAL_MODE_LISTEN_NFCA) /*!< Bitmask for Listen Mode enabling NFCA */ +#define RFAL_LM_MASK_NFCB \ + ((uint32_t)1U << (uint8_t) \ + RFAL_MODE_LISTEN_NFCB) /*!< Bitmask for Listen Mode enabling NFCB */ +#define RFAL_LM_MASK_NFCF \ + ((uint32_t)1U << (uint8_t) \ + RFAL_MODE_LISTEN_NFCF) /*!< Bitmask for Listen Mode enabling NFCF */ +#define RFAL_LM_MASK_ACTIVE_P2P \ + ((uint32_t)1U << (uint8_t) \ + RFAL_MODE_LISTEN_ACTIVE_P2P) /*!< Bitmask for Listen Mode enabling AP2P \ + */ + +#define RFAL_LM_SENS_RES_LEN \ + 2U /*!< NFC-A SENS_RES (ATQA) length */ +#define RFAL_LM_SENSB_RES_LEN \ + 13U /*!< NFC-B SENSB_RES (ATQB) length */ +#define RFAL_LM_SENSF_RES_LEN \ + 19U /*!< NFC-F SENSF_RES length */ +#define RFAL_LM_SENSF_SC_LEN \ + 2U /*!< NFC-F System Code length */ + +#define RFAL_NFCID3_LEN \ + 10U /*!< NFCID3 length */ +#define RFAL_NFCID2_LEN \ + 8U /*!< NFCID2 length */ +#define RFAL_NFCID1_TRIPLE_LEN \ + 10U /*!< NFCID1 length */ +#define RFAL_NFCID1_DOUBLE_LEN \ + 7U /*!< NFCID1 length */ +#define RFAL_NFCID1_SINGLE_LEN \ + 4U /*!< NFCID1 length */ + +/* +****************************************************************************** +* GLOBAL MACROS +****************************************************************************** +*/ + +/*! Returns the maximum supported bit rate for RW mode. Caller must check if + * mode is supported before, as even if mode is not supported will return the + * min */ +#define rfalGetMaxBrRW() \ + (((RFAL_SUPPORT_BR_RW_6780) \ + ? RFAL_BR_6780 \ + : ((RFAL_SUPPORT_BR_RW_3390) \ + ? RFAL_BR_3390 \ + : ((RFAL_SUPPORT_BR_RW_1695) \ + ? RFAL_BR_1695 \ + : ((RFAL_SUPPORT_BR_RW_848) \ + ? RFAL_BR_848 \ + : ((RFAL_SUPPORT_BR_RW_424) \ + ? RFAL_BR_424 \ + : ((RFAL_SUPPORT_BR_RW_212) \ + ? RFAL_BR_212 \ + : RFAL_BR_106))))))) + +/*! Returns the maximum supported bit rate for AP2P mode. Caller must check if + * mode is supported before, as even if mode is not supported will return the + * min */ +#define rfalGetMaxBrAP2P() \ + (((RFAL_SUPPORT_BR_AP2P_848) \ + ? RFAL_BR_848 \ + : ((RFAL_SUPPORT_BR_AP2P_424) \ + ? RFAL_BR_424 \ + : ((RFAL_SUPPORT_BR_AP2P_212) ? RFAL_BR_212 : RFAL_BR_106)))) + +/*! Returns the maximum supported bit rate for CE-A mode. Caller must check if + * mode is supported before, as even if mode is not supported will return the + * min */ +#define rfalGetMaxBrCEA() \ + (((RFAL_SUPPORT_BR_CE_A_848) \ + ? RFAL_BR_848 \ + : ((RFAL_SUPPORT_BR_CE_A_424) \ + ? RFAL_BR_424 \ + : ((RFAL_SUPPORT_BR_CE_A_212) ? RFAL_BR_212 : RFAL_BR_106)))) + +/*! Returns the maximum supported bit rate for CE-B mode. Caller must check if + * mode is supported before, as even if mode is not supported will return the + * min */ +#define rfalGetMaxBrCEB() \ + (((RFAL_SUPPORT_BR_CE_B_848) \ + ? RFAL_BR_848 \ + : ((RFAL_SUPPORT_BR_CE_B_424) \ + ? RFAL_BR_424 \ + : ((RFAL_SUPPORT_BR_CE_B_212) ? RFAL_BR_212 : RFAL_BR_106)))) + +/*! Returns the maximum supported bit rate for CE-F mode. Caller must check if + * mode is supported before, as even if mode is not supported will return the + * min */ +#define rfalGetMaxBrCEF() \ + (((RFAL_SUPPORT_BR_CE_F_424) ? RFAL_BR_424 : RFAL_BR_212)) + +#define rfalIsModeActiveComm(md) \ + (((md) == RFAL_MODE_POLL_ACTIVE_P2P) || \ + ((md) == RFAL_MODE_LISTEN_ACTIVE_P2P)) /*!< Checks if mode md is Active \ + Communication */ +#define rfalIsModePassiveComm(md) \ + (!rfalIsModeActiveComm(md)) /*!< Checks if mode md is Passive Communication \ + */ +#define rfalIsModePassiveListen(md) \ + (((md) == RFAL_MODE_LISTEN_NFCA) || ((md) == RFAL_MODE_LISTEN_NFCB) || \ + ((md) == \ + RFAL_MODE_LISTEN_NFCF)) /*!< Checks if mode md is Passive Listen */ +#define rfalIsModePassivePoll(md) \ + (rfalIsModePassiveComm(md) && \ + (!rfalIsModePassiveListen(md))) /*!< Checks if mode md is Passive Poll */ + +#define rfalConv1fcTo8fc(t) \ + (uint32_t)((uint32_t)(t) / \ + RFAL_1FC_IN_8FC) /*!< Converts the given t from 1/fc to 8/fc */ +#define rfalConv8fcTo1fc(t) \ + (uint32_t)((uint32_t)(t) * \ + RFAL_1FC_IN_8FC) /*!< Converts the given t from 8/fc to 1/fc */ + +#define rfalConv1fcTo64fc(t) \ + (uint32_t)( \ + (uint32_t)(t) / \ + RFAL_1FC_IN_64FC) /*!< Converts the given t from 1/fc to 64/fc */ +#define rfalConv64fcTo1fc(t) \ + (uint32_t)((uint32_t)(t) * \ + RFAL_1FC_IN_64FC) /*!< Converts the given t from 64/fc to 1/fc */ + +#define rfalConv1fcTo512fc(t) \ + (uint32_t)( \ + (uint32_t)(t) / \ + RFAL_1FC_IN_512FC) /*!< Converts the given t from 1/fc to 512/fc */ +#define rfalConv512fcTo1fc(t) \ + (uint32_t)( \ + (uint32_t)(t) * \ + RFAL_1FC_IN_512FC) /*!< Converts the given t from 512/fc to 1/fc */ + +#define rfalConv1fcTo2018fc(t) \ + (uint32_t)( \ + (uint32_t)(t) / \ + RFAL_1FC_IN_2048FC) /*!< Converts the given t from 1/fc to 2048/fc */ +#define rfalConv2048fcTo1fc(t) \ + (uint32_t)( \ + (uint32_t)(t) * \ + RFAL_1FC_IN_2048FC) /*!< Converts the given t from 2048/fc to 1/fc */ + +#define rfalConv1fcTo4096fc(t) \ + (uint32_t)( \ + (uint32_t)(t) / \ + RFAL_1FC_IN_4096FC) /*!< Converts the given t from 1/fc to 4096/fc */ +#define rfalConv4096fcTo1fc(t) \ + (uint32_t)( \ + (uint32_t)(t) * \ + RFAL_1FC_IN_4096FC) /*!< Converts the given t from 4096/fc to 1/fc */ + +#define rfalConv1fcToMs(t) \ + (uint32_t)((uint32_t)(t) / \ + RFAL_1MS_IN_1FC) /*!< Converts the given t from 1/fc to ms */ +#define rfalConvMsTo1fc(t) \ + (uint32_t)((uint32_t)(t) * \ + RFAL_1MS_IN_1FC) /*!< Converts the given t from ms to 1/fc */ + +#define rfalConv1fcToUs(t) \ + (uint32_t)(((uint32_t)(t) * RFAL_US_IN_MS) / \ + RFAL_1MS_IN_1FC) /*!< Converts the given t from 1/fc to us */ +#define rfalConvUsTo1fc(t) \ + (uint32_t)(((uint32_t)(t) * RFAL_1MS_IN_1FC) / \ + RFAL_US_IN_MS) /*!< Converts the given t from us to 1/fc */ + +#define rfalConv64fcToMs(t) \ + (uint32_t)((uint32_t)(t) / \ + (RFAL_1MS_IN_1FC / \ + RFAL_1FC_IN_64FC)) /*!< Converts the given t from 64/fc to ms */ +#define rfalConvMsTo64fc(t) \ + (uint32_t)((uint32_t)(t) * \ + (RFAL_1MS_IN_1FC / \ + RFAL_1FC_IN_64FC)) /*!< Converts the given t from ms to 64/fc */ + +#define rfalConvBitsToBytes(n) \ + (uint16_t)( \ + ((uint16_t)(n) + (RFAL_BITS_IN_BYTE - 1U)) / \ + (RFAL_BITS_IN_BYTE)) /*!< Converts the given n from bits to bytes */ +#define rfalConvBytesToBits(n) \ + (uint32_t)( \ + (uint32_t)(n) * \ + (RFAL_BITS_IN_BYTE)) /*!< Converts the given n from bytes to bits */ + +#define rfalRunBlocking(e, fn) \ + do { \ + (e) = (fn); \ + rfalWorker(); \ + } while ((e) == RFAL_ERR_BUSY) /*!< Macro used for the blocking methods */ + +/*! Computes a Transceive context \a ctx with default flags and the lengths + * in bytes with the given arguments + * \a ctx : Transceive context to be assigned + * \a tB : txBuf the pointer to the buffer to be sent + * \a tBL : txBuf length in bytes + * \a rB : rxBuf the pointer to the buffer to place the received frame + * \a rBL : rxBuf length in bytes + * \a rdL : rxRcvdLen the pointer to place the rx length + * \a t : FWT to be used on this transceive in 1/fc + */ +#define rfalCreateByteTxRxContext(ctx, tB, tBL, rB, rBL, rdL, t) \ + (ctx).txBuf = (uint8_t *)(tB); \ + (ctx).txBufLen = (uint16_t)rfalConvBytesToBits(tBL); \ + (ctx).rxBuf = (uint8_t *)(rB); \ + (ctx).rxBufLen = (uint16_t)rfalConvBytesToBits(rBL); \ + (ctx).rxRcvdLen = (uint16_t *)(rdL); \ + (ctx).flags = (uint32_t)RFAL_TXRX_FLAGS_DEFAULT; \ + (ctx).fwt = (uint32_t)(t); + +/*! Computes a Transceive context \a ctx using lengths in bytes + * with the given flags and arguments + * \a ctx : Transceive context to be assigned + * \a tB : txBuf the pointer to the buffer to be sent + * \a tBL : txBuf length in bytes + * \a rB : rxBuf the pointer to the buffer to place the received frame + * \a rBL : rxBuf length in bytes + * \a rBL : rxBuf length in bytes + * \a t : FWT to be used on this transceive in 1/fc + */ +#define rfalCreateByteFlagsTxRxContext(ctx, tB, tBL, rB, rBL, rdL, fl, t) \ + (ctx).txBuf = (uint8_t *)(tB); \ + (ctx).txBufLen = (uint16_t)rfalConvBytesToBits(tBL); \ + (ctx).rxBuf = (uint8_t *)(rB); \ + (ctx).rxBufLen = (uint16_t)rfalConvBytesToBits(rBL); \ + (ctx).rxRcvdLen = (uint16_t *)(rdL); \ + (ctx).flags = (uint32_t)(fl); \ + (ctx).fwt = (uint32_t)(t); + +#define rfalLogE(...) \ + platformLog(__VA_ARGS__) /*!< Macro for the error log method */ +#define rfalLogW(...) \ + platformLog(__VA_ARGS__) /*!< Macro for the warning log method */ +#define rfalLogI(...) \ + platformLog(__VA_ARGS__) /*!< Macro for the info log method */ +#define rfalLogD(...) \ + platformLog(__VA_ARGS__) /*!< Macro for the debug log method */ + +/* +****************************************************************************** +* GLOBAL ENUMS +****************************************************************************** +*/ + +/* RFAL Guard Time (GT) default values */ +#define RFAL_GT_NFCA \ + rfalConvMsTo1fc(5U) /*!< GTA Digital 2.0 6.10.4.1 & B.2 */ +#define RFAL_GT_NFCB \ + rfalConvMsTo1fc(5U) /*!< GTB Digital 2.0 7.9.4.1 & B.3 */ +#define RFAL_GT_NFCF \ + rfalConvMsTo1fc(20U) /*!< GTF Digital 2.0 8.7.4.1 & B.4 */ +#define RFAL_GT_NFCV \ + rfalConvMsTo1fc(5U) /*!< GTV Digital 2.0 9.7.5.1 & B.5 */ +#define RFAL_GT_PICOPASS rfalConvMsTo1fc(1U) /*!< GT Picopass */ +#define RFAL_GT_AP2P rfalConvMsTo1fc(5U) /*!< TIRFG Ecma 340 11.1.1 */ +#define RFAL_GT_AP2P_ADJUSTED \ + rfalConvMsTo1fc(5U + 25U) /*!< Adjusted GT for greater interoperability \ + (Sony XPERIA P, Nokia N9, Huawei P2) */ + +/* RFAL Frame Delay Time (FDT) Listen default values */ +#define RFAL_FDT_LISTEN_NFCA_POLLER \ + 1172U /*!< FDTA,LISTEN,MIN (n=9) Last bit: Logic "1" - tnn,min/2 Digital 1.1 \ + 6.10 ; EMV CCP Spec Book D v2.01 4.8.1.3 */ +#define RFAL_FDT_LISTEN_NFCB_POLLER \ + 1008U /*!< TR0B,MIN Digital 1.1 7.1.3 & A.3 ; EMV CCP Spec Book D \ + v2.01 4.8.1.3 & Table A.5 */ +#define RFAL_FDT_LISTEN_NFCF_POLLER \ + 2672U /*!< TR0F,LISTEN,MIN Digital 1.1 8.7.1.1 & A.4 */ +#define RFAL_FDT_LISTEN_NFCV_POLLER \ + 4310U /*!< FDTV,LISTEN,MIN t1 min Digital 2.1 B.5 ; ISO15693-3 \ + 2009 9.1 */ +#define RFAL_FDT_LISTEN_PICOPASS_POLLER \ + 3400U /*!< ISO15693 t1 min - observed adjustment */ +#define RFAL_FDT_LISTEN_AP2P_POLLER \ + 64U /*!< FDT AP2P No actual FDTListen is required as fields switch and \ + collision avoidance */ +#define RFAL_FDT_LISTEN_NFCA_LISTENER \ + 1172U /*!< FDTA,LISTEN,MIN Digital 1.1 6.10 */ +#define RFAL_FDT_LISTEN_NFCB_LISTENER \ + 1024U /*!< TR0B,MIN Digital 1.1 7.1.3 & A.3 ; EMV CCP Spec Book D \ + v2.01 4.8.1.3 & Table A.5 */ +#define RFAL_FDT_LISTEN_NFCF_LISTENER \ + 2688U /*!< TR0F,LISTEN,MIN Digital 2.1 8.7.1.1 & B.4 */ +#define RFAL_FDT_LISTEN_AP2P_LISTENER \ + 64U /*!< FDT AP2P No actual FDTListen exists as fields switch and collision \ + avoidance */ + +/* RFAL Frame Delay Time (FDT) Poll default values */ +#define RFAL_FDT_POLL_NFCA_POLLER \ + 6780U /*!< FDTA,POLL,MIN Digital 1.1 6.10.3.1 & A.2 */ +#define RFAL_FDT_POLL_NFCA_T1T_POLLER \ + 384U /*!< RRDDT1T,MIN,B1 Digital 1.1 10.7.1 & A.5 */ +#define RFAL_FDT_POLL_NFCB_POLLER \ + 6780U /*!< FDTB,POLL,MIN = TR2B,MIN,DEFAULT Digital 1.1 7.9.3 & A.3 ; \ + EMVCo 3.0 FDTB,PCD,MIN Table A.5 */ +#define RFAL_FDT_POLL_NFCF_POLLER \ + 6800U /*!< FDTF,POLL,MIN Digital 2.1 8.7.3 & B.4 */ +#define RFAL_FDT_POLL_NFCV_POLLER \ + 4192U /*!< FDTV,POLL Digital 2.1 9.7.3.1 & B.5 */ +#define RFAL_FDT_POLL_PICOPASS_POLLER 1790U /*!< FDT Max */ +#define RFAL_FDT_POLL_AP2P_POLLER \ + 6800U /*!< AP2P inhere FDT from the Technology used (use longest: \ + TR0F,POLL,MIN + TR1F) Digital 2.2 17.11.1 */ + +/* +****************************************************************************** +* GLOBAL TYPES +****************************************************************************** +*/ + +/*! RFAL modes */ +typedef enum { + RFAL_MODE_NONE = 0, /*!< No mode selected/defined */ + RFAL_MODE_POLL_NFCA = + 1, /*!< Mode to perform as NFCA (ISO14443A) Poller (PCD) */ + RFAL_MODE_POLL_NFCA_T1T = + 2, /*!< Mode to perform as NFCA T1T (Topaz) Poller (PCD) */ + RFAL_MODE_POLL_NFCB = + 3, /*!< Mode to perform as NFCB (ISO14443B) Poller (PCD) */ + RFAL_MODE_POLL_B_PRIME = + 4, /*!< Mode to perform as B' Calypso (Innovatron) (PCD) */ + RFAL_MODE_POLL_B_CTS = 5, /*!< Mode to perform as CTS Poller (PCD) */ + RFAL_MODE_POLL_NFCF = 6, /*!< Mode to perform as NFCF (FeliCa) Poller (PCD) */ + RFAL_MODE_POLL_NFCV = + 7, /*!< Mode to perform as NFCV (ISO15963) Poller (PCD) */ + RFAL_MODE_POLL_PICOPASS = + 8, /*!< Mode to perform as PicoPass / iClass Poller (PCD) */ + RFAL_MODE_POLL_ACTIVE_P2P = + 9, /*!< Mode to perform as Active P2P (ISO18092) Initiator */ + RFAL_MODE_LISTEN_NFCA = + 10, /*!< Mode to perform as NFCA (ISO14443A) Listener (PICC) */ + RFAL_MODE_LISTEN_NFCB = + 11, /*!< Mode to perform as NFCA (ISO14443B) Listener (PICC) */ + RFAL_MODE_LISTEN_NFCF = + 12, /*!< Mode to perform as NFCA (ISO15963) Listener (PICC) */ + RFAL_MODE_LISTEN_ACTIVE_P2P = + 13 /*!< Mode to perform as Active P2P (ISO18092) Target */ +} rfalMode; + +/*! RFAL Bit rates */ +typedef enum { + RFAL_BR_106 = 0, /*!< Bit Rate 106 kbit/s (fc/128) */ + RFAL_BR_212 = 1, /*!< Bit Rate 212 kbit/s (fc/64) */ + RFAL_BR_424 = 2, /*!< Bit Rate 424 kbit/s (fc/32) */ + RFAL_BR_848 = 3, /*!< Bit Rate 848 kbit/s (fc/16) */ + RFAL_BR_1695 = 4, /*!< Bit Rate 1695 kbit/s (fc/8) */ + RFAL_BR_3390 = 5, /*!< Bit Rate 3390 kbit/s (fc/4) */ + RFAL_BR_6780 = 6, /*!< Bit Rate 6780 kbit/s (fc/2) */ + RFAL_BR_13560 = 7, /*!< Bit Rate 13560 kbit/s (fc) */ + RFAL_BR_211p88 = + 0xE9, /*!< Bit Rate 211,88 kbit/s (fc/64) Fast Mode VICC->VCD */ + RFAL_BR_105p94 = + 0xEA, /*!< Bit Rate 105,94 kbit/s (fc/128) Fast Mode VICC->VCD */ + RFAL_BR_52p97 = + 0xEB, /*!< Bit Rate 52.97 kbit/s (fc/256) Fast Mode VICC->VCD */ + RFAL_BR_26p48 = 0xEC, /*!< Bit Rate 26,48 kbit/s (fc/512) NFCV VICC->VCD & + VCD->VICC 1of4 */ + RFAL_BR_1p66 = + 0xED, /*!< Bit Rate 1,66 kbit/s (fc/8192) NFCV VCD->VICC 1of256 */ + RFAL_BR_KEEP = + 0xFF /*!< Value indicating to keep the same previous bit rate */ +} rfalBitRate; + +/*! RFAL Compliance modes for upper modules */ +typedef enum { + RFAL_COMPLIANCE_MODE_NFC, /*!< Perform with NFC Forum 1.1 compliance */ + RFAL_COMPLIANCE_MODE_EMV, /*!< Perform with EMVCo compliance */ + RFAL_COMPLIANCE_MODE_ISO /*!< Perform with ISO10373 compliance */ +} rfalComplianceMode; + +/*! RFAL main states flags */ +typedef enum { + RFAL_STATE_IDLE = 0, + RFAL_STATE_INIT = 1, + RFAL_STATE_MODE_SET = 2, + + RFAL_STATE_TXRX = 3, + RFAL_STATE_LM = 4, + RFAL_STATE_WUM = 5 + +} rfalState; + +/*! RFAL transceive states */ +typedef enum { + RFAL_TXRX_STATE_IDLE = 0, + RFAL_TXRX_STATE_INIT = 1, + RFAL_TXRX_STATE_START = 2, + + RFAL_TXRX_STATE_TX_IDLE = 11, + RFAL_TXRX_STATE_TX_WAIT_GT = 12, + RFAL_TXRX_STATE_TX_WAIT_FDT = 13, + RFAL_TXRX_STATE_TX_PREP_TX = 14, + RFAL_TXRX_STATE_TX_TRANSMIT = 15, + RFAL_TXRX_STATE_TX_WAIT_WL = 16, + RFAL_TXRX_STATE_TX_RELOAD_FIFO = 17, + RFAL_TXRX_STATE_TX_WAIT_TXE = 18, + RFAL_TXRX_STATE_TX_DONE = 19, + RFAL_TXRX_STATE_TX_FAIL = 20, + + RFAL_TXRX_STATE_RX_IDLE = 81, + RFAL_TXRX_STATE_RX_WAIT_EON = 82, + RFAL_TXRX_STATE_RX_WAIT_RXS = 83, + RFAL_TXRX_STATE_RX_WAIT_RXE = 84, + RFAL_TXRX_STATE_RX_READ_FIFO = 85, + RFAL_TXRX_STATE_RX_ERR_CHECK = 86, + RFAL_TXRX_STATE_RX_READ_DATA = 87, + RFAL_TXRX_STATE_RX_WAIT_EOF = 88, + RFAL_TXRX_STATE_RX_DONE = 89, + RFAL_TXRX_STATE_RX_FAIL = 90, + +} rfalTransceiveState; + +/*! RFAL transceive flags */ +enum { + RFAL_TXRX_FLAGS_CRC_TX_AUTO = + (0U << 0), /*!< CRC will be generated automatic upon transmission */ + RFAL_TXRX_FLAGS_CRC_TX_MANUAL = + (1U << 0), /*!< CRC was calculated manually, included in txBuffer */ + RFAL_TXRX_FLAGS_CRC_RX_KEEP = + (1U << 1), /*!< Upon Reception keep the CRC in rxBuffer (reflected on rcvd + length) */ + RFAL_TXRX_FLAGS_CRC_RX_REMV = (0U << 1), /*!< Remove the CRC from rxBuffer */ + RFAL_TXRX_FLAGS_NFCIP1_ON = + (1U << 2), /*!< Enable NFCIP1 mode: Add SB(F0) and LEN bytes during Tx and + skip SB(F0) byte during Rx */ + RFAL_TXRX_FLAGS_NFCIP1_OFF = + (0U << 2), /*!< Disable NFCIP1 mode: do not append protocol bytes while Tx + nor skip while Rx */ + RFAL_TXRX_FLAGS_AGC_OFF = + (1U << 3), /*!< Disable Automatic Gain Control, improving multiple devices + collision detection. \b DEPRECATED: flag is depreacted, + usage of Anticollision APIs based on Analog Config table + with RFAL_ANALOG_CONFIG_ANTICOL settings */ + RFAL_TXRX_FLAGS_AGC_ON = + (0U << 3), /*!< Enable Automatic Gain Control, improving single device + reception \b DEPRECATED: flag is deprecated, + usage of Anticollision APIs based on Analog Config table + with RFAL_ANALOG_CONFIG_ANTICOL settings */ + RFAL_TXRX_FLAGS_PAR_RX_KEEP = + (1U << 4), /*!< Disable Parity check and keep the Parity and CRC bits in + the received buffer */ + RFAL_TXRX_FLAGS_PAR_RX_REMV = + (0U << 4), /*!< Enable Parity check and remove the parity bits from the + received buffer */ + RFAL_TXRX_FLAGS_PAR_TX_NONE = + (1U << 5), /*!< Disable automatic Parity generation (ISO14443A) and use + the one provided in the buffer*/ + RFAL_TXRX_FLAGS_PAR_TX_AUTO = + (0U << 5), /*!< Enable automatic Parity generation (ISO14443A) */ + RFAL_TXRX_FLAGS_NFCV_FLAG_MANUAL = + (1U << 6), /*!< Disable automatic adaption of flag byte (ISO15693) + according to current comm params */ + RFAL_TXRX_FLAGS_NFCV_FLAG_AUTO = + (0U << 6), /*!< Enable automatic adaption of flag byte (ISO115693) + according to current comm params */ + RFAL_TXRX_FLAGS_CRC_RX_MANUAL = (1U << 7), /*!< Disable automatic CRC check */ + RFAL_TXRX_FLAGS_CRC_RX_AUTO = (0U << 7), /*!< Enable automatic CRC check */ +}; + +/*! RFAL error handling */ +typedef enum { + RFAL_ERRORHANDLING_NONE = + 0, /*!< No special error handling will be performed */ + RFAL_ERRORHANDLING_EMD = 1 /*!< EMD suppression enabled Digital 2.1 4.1.1.1 + ; EMVCo 3.0 4.9.2 ; ISO 14443-3 8.3 */ +} rfalEHandling; + +/*! Struct that holds all context to be used on a Transceive */ +typedef struct { + uint8_t *txBuf; /*!< (In) Buffer where outgoing message is located */ + uint16_t txBufLen; /*!< (In) Length of the outgoing message in bits */ + + uint8_t *rxBuf; /*!< (Out) Buffer where incoming message will be placed */ + uint16_t + rxBufLen; /*!< (In) Maximum length of the incoming message in bits */ + uint16_t *rxRcvdLen; /*!< (Out) Actual received length in bits */ + + uint32_t flags; /*!< (In) TransceiveFlags indication special handling */ + uint32_t fwt; /*!< (In) Frame Waiting Time in 1/fc */ +} rfalTransceiveContext; + +/*! System callback to indicate an event that requires a system reRun */ +typedef void (*rfalUpperLayerCallback)(void); + +/*! Callback to be executed before a Transceive */ +typedef void (*rfalPreTxRxCallback)(void); + +/*! Callback to be executed after a Transceive */ +typedef void (*rfalPostTxRxCallback)(void); + +/*! Callback to sync actual transmission start */ +typedef bool (*rfalSyncTxRxCallback)(void); + +/*! Callback upon External Field detected while in Listen Mode */ +typedef void (*rfalLmEonCallback)(void); + +/*******************************************************************************/ +/* ISO14443A */ +/*******************************************************************************/ + +/*! RFAL ISO 14443A Short Frame Command */ +typedef enum { + RFAL_14443A_SHORTFRAME_CMD_WUPA = 0x52, /*!< ISO14443A WUPA / NFC-A ALL_REQ */ + RFAL_14443A_SHORTFRAME_CMD_REQA = 0x26 /*!< ISO14443A REQA / NFC-A SENS_REQ */ +} rfal14443AShortFrameCmd; + +/*******************************************************************************/ + +/*******************************************************************************/ +/* FeliCa */ +/*******************************************************************************/ + +#define RFAL_FELICA_LEN_LEN 1U /*!< FeliCa LEN byte length */ +#define RFAL_FELICA_POLL_REQ_LEN \ + (RFAL_FELICA_LEN_LEN + 1U + 2U + 1U + \ + 1U) /*!< FeliCa Poll Request length (LEN + CMD + SC + RC + TSN) */ +#define RFAL_FELICA_POLL_RES_LEN \ + (RFAL_FELICA_LEN_LEN + 1U + 8U + 8U + \ + 2U) /*!< Maximum FeliCa Poll Response length (LEN + CMD + NFCID2 + PAD + \ + RD) */ +#define RFAL_FELICA_POLL_MAX_SLOTS \ + 16U /*!< Maximum number of slots (TSN) on FeliCa Poll */ + +/*! NFC-F RC (Request Code) codes NFC Forum Digital 1.1 Table 42 */ +enum { + RFAL_FELICA_POLL_RC_NO_REQUEST = + 0x00U, /*!< RC: No System Code information requested */ + RFAL_FELICA_POLL_RC_SYSTEM_CODE = + 0x01U, /*!< RC: System Code information requested */ + RFAL_FELICA_POLL_RC_COM_PERFORMANCE = + 0x02U /*!< RC: Advanced protocol features supported */ +}; + +/*! NFC-F TSN (Time Slot Number) codes NFC Forum Digital 1.1 Table 43 */ +typedef enum { + RFAL_FELICA_1_SLOT = 0, /*!< TSN with number of Time Slots: 1 */ + RFAL_FELICA_2_SLOTS = 1, /*!< TSN with number of Time Slots: 2 */ + RFAL_FELICA_4_SLOTS = 3, /*!< TSN with number of Time Slots: 4 */ + RFAL_FELICA_8_SLOTS = 7, /*!< TSN with number of Time Slots: 8 */ + RFAL_FELICA_16_SLOTS = 15 /*!< TSN with number of Time Slots: 16 */ +} rfalFeliCaPollSlots; + +/*! NFCF Poll Response NFC Forum Digital 1.1 Table 44 */ +typedef uint8_t rfalFeliCaPollRes[RFAL_FELICA_POLL_RES_LEN]; + +/*******************************************************************************/ + +/*******************************************************************************/ +/* Listen Mode */ +/*******************************************************************************/ + +/*! RFAL Listen Mode NFCID Length */ +typedef enum { + RFAL_LM_NFCID_LEN_04 = + RFAL_NFCID1_SINGLE_LEN, /*!< Listen mode indicates 4 byte NFCID */ + RFAL_LM_NFCID_LEN_07 = + RFAL_NFCID1_DOUBLE_LEN, /*!< Listen mode indicates 7 byte NFCID */ + RFAL_LM_NFCID_LEN_10 = + RFAL_NFCID1_TRIPLE_LEN, /*!< Listen mode indicates 10 byte NFCID */ +} rfalLmNfcidLen; + +/*! RFAL Listen Mode States */ +typedef enum { + RFAL_LM_STATE_NOT_INIT = 0x00, /*!< Not Initialized state */ + RFAL_LM_STATE_POWER_OFF = 0x01, /*!< Power Off state */ + RFAL_LM_STATE_IDLE = 0x02, /*!< Idle state Activity 1.1 5.2 */ + RFAL_LM_STATE_READY_A = + 0x03, /*!< Ready A state Activity 1.1 5.3 5.4 & 5.5 */ + RFAL_LM_STATE_READY_B = 0x04, /*!< Ready B state Activity 1.1 5.11 5.12 */ + RFAL_LM_STATE_READY_F = 0x05, /*!< Ready F state Activity 1.1 5.15 */ + RFAL_LM_STATE_ACTIVE_A = 0x06, /*!< Active A state Activity 1.1 5.6 */ + RFAL_LM_STATE_CARDEMU_4A = + 0x07, /*!< Card Emulation 4A state Activity 1.1 5.10 */ + RFAL_LM_STATE_CARDEMU_4B = + 0x08, /*!< Card Emulation 4B state Activity 1.1 5.14 */ + RFAL_LM_STATE_CARDEMU_3 = + 0x09, /*!< Card Emulation 3 state Activity 1.1 5.18 */ + RFAL_LM_STATE_TARGET_A = 0x0A, /*!< Target A state Activity 1.1 5.9 */ + RFAL_LM_STATE_TARGET_F = 0x0B, /*!< Target F state Activity 1.1 5.17 */ + RFAL_LM_STATE_SLEEP_A = 0x0C, /*!< Sleep A state Activity 1.1 5.7 */ + RFAL_LM_STATE_SLEEP_B = 0x0D, /*!< Sleep B state Activity 1.1 5.13 */ + RFAL_LM_STATE_READY_Ax = + 0x0E, /*!< Ready A* state Activity 1.1 5.3 5.4 & 5.5 */ + RFAL_LM_STATE_ACTIVE_Ax = 0x0F, /*!< Active A* state Activity 1.1 5.6 */ + RFAL_LM_STATE_SLEEP_AF = 0x10, /*!< Sleep AF state Activity 1.1 5.19 */ +} rfalLmState; + +/*! RFAL Listen Mode Passive A configs */ +typedef struct { + rfalLmNfcidLen nfcidLen; /*!< NFCID Len (4, 7 or 10 bytes) */ + uint8_t nfcid[RFAL_NFCID1_TRIPLE_LEN]; /*!< NFCID */ + uint8_t SENS_RES[RFAL_LM_SENS_RES_LEN]; /*!< NFC-106k; SENS_REQ Response */ + uint8_t SEL_RES; /*!< SEL_RES (SAK) with complete NFCID1 (UID) */ +} rfalLmConfPA; + +/*! RFAL Listen Mode Passive B configs */ +typedef struct { + uint8_t SENSB_RES[RFAL_LM_SENSB_RES_LEN]; /*!< SENSF_RES */ +} rfalLmConfPB; + +/*! RFAL Listen Mode Passive F configs */ +typedef struct { + uint8_t SC[RFAL_LM_SENSF_SC_LEN]; /*!< System Code to listen for */ + uint8_t SENSF_RES[RFAL_LM_SENSF_RES_LEN]; /*!< SENSF_RES */ +} rfalLmConfPF; + +/*! RFAL low power modes */ +typedef enum { + RFAL_LP_MODE_PD = 0, /*!< Set RF Chip in Power Down state */ + RFAL_LP_MODE_HR = + 1 /*!< Set RF Chip in Hold Reset state (available for specific devices) */ +} rfalLpMode; + +/*******************************************************************************/ + +/*******************************************************************************/ +/* Wake-Up Mode */ +/*******************************************************************************/ + +#define RFAL_WUM_REFERENCE_AUTO \ + 0xFFU /*!< Indicates new reference is set by the driver */ + +/*! RFAL Wake-Up Mode States */ +typedef enum { + RFAL_WUM_STATE_NOT_INIT = 0x00, /*!< Not Initialized state */ + RFAL_WUM_STATE_INITIALIZING = 0x01, /*!< Wake-Up mode is starting */ + RFAL_WUM_STATE_ENABLED = 0x02, /*!< Wake-Up mode is enabled */ + RFAL_WUM_STATE_ENABLED_WOKE = + 0x03, /*!< Wake-Up mode enabled and has received IRQ(s)*/ +} rfalWumState; + +/*******************************************************************************/ + +/* +****************************************************************************** +* GLOBAL FUNCTION PROTOTYPES +****************************************************************************** +*/ + +/*! + ***************************************************************************** + * \brief RFAL Initialize + * + * Initializes RFAL layer and the ST25R391x + * Ensures that ST25R is properly connected and returns error if any problem + * is detected + * + * \warning rfalAnalogConfigInitialize() should be called before so that + * the Analog config table has been previously initialized. + * + * \return RFAL_ERR_HW_MISMATCH : Expected HW do not match or communication + *error \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalInitialize(void); + +/*! + ***************************************************************************** + * \brief RFAL Calibrate + * + * Performs necessary calibration of RF chip in case it is indicated by current + * register settings. E.g. antenna calibration and regulator calibration + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized + * \return RFAL_ERR_NONE : No error + * + ***************************************************************************** + */ +ReturnCode rfalCalibrate(void); + +/*! + ***************************************************************************** + * \brief RFAL Adjust Regulators + * + * Adjusts ST25R391x regulators + * + * \param[out] result : the result of the calibrate antenna in mV + * NULL if result not requested + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized + * \return RFAL_ERR_NONE : No error + * + ***************************************************************************** + */ +ReturnCode rfalAdjustRegulators(uint16_t *result); + +/*! + ***************************************************************************** + * \brief RFAL Set System Callback + * + * Sets a callback for the driver to call when an event has occurred that + * may require the system to be notified + * + * \param[in] pFunc : method pointer for the upper layer callback + * + ***************************************************************************** + */ +void rfalSetUpperLayerCallback(rfalUpperLayerCallback pFunc); + +/*! + ***************************************************************************** + * \brief RFAL Set Pre Tx Callback + * + * Sets a callback for the driver to call before a Transceive + * + * \param[in] pFunc : method pointer for the Pre Tx callback + * + ***************************************************************************** + */ +void rfalSetPreTxRxCallback(rfalPreTxRxCallback pFunc); + +/*! + ***************************************************************************** + * \brief RFAL Sync Pre Tx Callback + * + * Sets a callback for the driver to execute in order to Syncronize actual + * transmission start. + * If the callback is set TxRx will hold until Sync callback returns true. + * + * \param[in] pFunc : method pointer for the Sync Tx callback + * + ***************************************************************************** + */ +void rfalSetSyncTxRxCallback(rfalSyncTxRxCallback pFunc); + +/*! + ***************************************************************************** + * \brief RFAL Set Post Tx Callback + * + * Sets a callback for the driver to call after a Transceive + * + * \param[in] pFunc : method pointer for the Post Tx callback + * + ***************************************************************************** + */ +void rfalSetPostTxRxCallback(rfalPostTxRxCallback pFunc); + +/*! + ***************************************************************************** + * \brief RFAL Set LM EON Callback + * + * Sets a callback upon External Field On detected while in Passive Listen Mode + * + * \warning callabck available only on applicable devices, + * supporting Passive Listen Mode + * + * \param[in] pFunc : method pointer for the LM EON callback + * + ***************************************************************************** + */ +void rfalSetLmEonCallback(rfalLmEonCallback pFunc); + +/*! + ***************************************************************************** + * \brief RFAL Deinitialize + * + * Deinitializes RFAL layer and the ST25R + * + * \return RFAL_ERR_NONE : No error + * + ***************************************************************************** + */ +ReturnCode rfalDeinitialize(void); + +/*! + ***************************************************************************** + * \brief RFAL Set Mode + * + * Sets the mode that RFAL will operate on the following communications. + * Proper initializations will be performed on the ST25R + * + * \warning bit rate value RFAL_BR_KEEP is not allowed, only in rfalSetBitRate() + * + * \warning the mode will be applied immediately on the RFchip regardless of + * any ongoing operations like Transceive, ListenMode + * + * \param[in] mode : mode for the RFAL/RFchip to perform + * \param[in] txBR : transmit bit rate + * \param[in] rxBR : receive bit rate + * + * \see rfalIsGTExpired + * \see rfalMode + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_NONE : No error + * + ***************************************************************************** + */ +ReturnCode rfalSetMode(rfalMode mode, rfalBitRate txBR, rfalBitRate rxBR); + +/*! + ***************************************************************************** + * \brief RFAL Get Mode + * + * Gets the mode that RFAL is set to operate + * + * \see rfalMode + * + * \return rfalMode : The current RFAL mode + ***************************************************************************** + */ +rfalMode rfalGetMode(void); + +/*! + ***************************************************************************** + * \brief RFAL Set Bit Rate + * + * Sets the Tx and Rx bit rates with the given values + * The bit rate change is applied on the RF chip remaining in the same + * mode previous defined with rfalSetMode() + * + * If no mode is defined bit rates will not be applied and an error + * is returned + * + * \param[in] txBR : transmit bit rate + * \param[in] rxBR : receive bit rate + * + * \see rfalSetMode + * \see rfalMode + * \see rfalBitRate + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_NOT_IMPLEMENTED : Mode not implemented + * \return RFAL_ERR_NONE : No error + * + ***************************************************************************** + */ +ReturnCode rfalSetBitRate(rfalBitRate txBR, rfalBitRate rxBR); + +/*! + ***************************************************************************** + * \brief RFAL Get Bit Rate + * + * Gets the Tx and Rx current bit rates + * + * If RFAL is not initialized or mode not set the bit rates return will + * be invalid RFAL_BR_KEEP + * + * \param[out] txBR : RFAL's current Tx Bit Rate + * \param[out] rxBR : RFAL's current Rx Bit Rate + * + * \see rfalSetBitRate + * \see rfalBitRate + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or mode not set + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalGetBitRate(rfalBitRate *txBR, rfalBitRate *rxBR); + +/*! + ***************************************************************************** + * \brief Set Error Handling Mode + * + * Sets the error handling mode to be used by the RFAL + * + * \param[in] eHandling : the error handling mode + * + ***************************************************************************** + */ +void rfalSetErrorHandling(rfalEHandling eHandling); + +/*! + ***************************************************************************** + * \brief Get Error Handling Mode + * + * Gets the error handling mode currently used by the RFAL + * + * \return rfalEHandling : Current error handling mode + ***************************************************************************** + */ +rfalEHandling rfalGetErrorHandling(void); + +/*! + ***************************************************************************** + * \brief Set Observation Mode + * + * Sets ST25R391x observation modes for RF debug purposes + * + * \param[in] txMode : the observation mode to be used during transmission + * \param[in] rxMode : the observation mode to be used during reception + * + * \warning The Observation Mode is an advanced feature and should be set + * according to the documentation of the part number in use. + * Please refer to the corresponding Datasheet or Application Note(s) + ***************************************************************************** + */ +void rfalSetObsvMode(uint32_t txMode, uint32_t rxMode); + +/*! + ***************************************************************************** + * \brief Get Observation Mode + * + * Gets ST25R391x the current configured observation modes + * + * \param[in] txMode : the current observation mode configured for transmission + * \param[in] rxMode : the current observation mode configured for reception + * + ***************************************************************************** + */ +void rfalGetObsvMode(uint8_t *txMode, uint8_t *rxMode); + +/*! + ***************************************************************************** + * \brief Disable Observation Mode + * + * Disables the ST25R391x observation mode + ***************************************************************************** + */ +void rfalDisableObsvMode(void); + +/*! + ***************************************************************************** + * \brief RFAL Set FDT Poll + * + * Sets the Frame Delay Time (FDT) to be used on the following + * communications. + * + * FDT Poll is the minimum time following a Poll Frame during + * which no subsequent Poll Frame can be sent (without a response from + * the Listener in between) + * FDTx,PP,MIN - Digital 1.1 6.10.2 & 7.9.2 & 8.7.2 + * + * \param[in] FDTPoll : Frame Delay Time in 1/fc cycles + * + ***************************************************************************** + */ +void rfalSetFDTPoll(uint32_t FDTPoll); + +/*! + ***************************************************************************** + * \brief RFAL Set FDT Poll + * + * Gets the current Frame Delay Time (FDT) + * + * FDT Poll is the minimum time following a Poll Frame during + * which no subsequent Poll Frame can be sent (without a response from + * the Listener in between) + * FDTx,PP,MIN - Digital 1.1 6.10.2 & 7.9.2 & 8.7.2 + * + * \return FDT : current FDT value in 1/fc cycles + * + ***************************************************************************** + */ +uint32_t rfalGetFDTPoll(void); + +/*! + ***************************************************************************** + * \brief RFAL Set FDT Listen + * + * Sets the Frame Delay Time (FDT) Listen minimum to be used on the + * following communications. + * + * FDT Listen is the minimum time between a Poll Frame and a Listen Frame + * FDTx,LISTEN,MIN - Digital 1.1 6.10.1 & 7.9.1 & 8.7.1 + * + * \param[in] FDTListen : Frame Delay Time in 1/fc cycles + * + ***************************************************************************** + */ +void rfalSetFDTListen(uint32_t FDTListen); + +/*! + ***************************************************************************** + * \brief RFAL Set FDT Listen + * + * Gets the Frame Delay Time (FDT) Listen minimum + * + * FDT Listen is the minimum time between a Poll Frame and a Listen Frame + * FDTx,LISTEN,MIN - Digital 1.1 6.10.1 & 7.9.1 & 8.7.1 + * + * \return FDT : current FDT value in 1/fc cycles + * + ***************************************************************************** + */ +uint32_t rfalGetFDTListen(void); + +/*! + ***************************************************************************** + * \brief RFAL Get GT + * + * Gets the current Guard Time (GT) + * + * GT is the minimum time when a device in Listen Mode is exposed to an + * unmodulated carrier + * + * \return GT : Guard Time in 1/fc cycles + * + ***************************************************************************** + */ +uint32_t rfalGetGT(void); + +/*! + ***************************************************************************** + * \brief RFAL Set GT + * + * Sets the Guard Time (GT) to be used on the following communications. + * + * GT is the minimum time when a device in Listen Mode is exposed to an + * unmodulated carrier + * + * \param[in] GT : Guard Time in 1/fc cycles + * RFAL_GT_NONE if no GT should be applied + * + ***************************************************************************** + */ +void rfalSetGT(uint32_t GT); + +/*! + ***************************************************************************** + * \brief RFAL Is GT expired + * + * Checks whether the GT timer has expired + * + * \return true : GT has expired or not running + * \return false : GT is still running + * + ***************************************************************************** + */ +bool rfalIsGTExpired(void); + +/*! + ***************************************************************************** + * \brief RFAL Turn Field On and Start GT + * + * Turns the Field On, performing Initial Collision Avoidance + * + * After Field On, if GT was set before, it starts the GT timer to be + * used on the following communications. + * + * \return RFAL_ERR_RF_COLLISION : External field detected + * \return RFAL_ERR_NONE : Field turned On + * + ***************************************************************************** + */ +ReturnCode rfalFieldOnAndStartGT(void); + +/*! + ***************************************************************************** + * \brief RFAL Turn Field Off + * + * Turns the Field Off + * + * \return RFAL_ERR_NONE : Field turned Off + ***************************************************************************** + */ +ReturnCode rfalFieldOff(void); + +/***************************************************************************** + * Transceive * + *****************************************************************************/ + +/*! + ***************************************************************************** + * \brief RFAL Set transceive context + * + * Set the context that will be used for the following Transceive + * Output and input buffers have to be passed and all other details prior to + * the Transceive itself has been started + * + * This method only sets the context. Once set, rfalWorker has + * to be executed until is done + * + * \param[in] ctx : the context for the following Transceive + * + * \see rfalWorker + * \see rfalGetTransceiveStatus + * + * \return RFAL_ERR_NONE : Done with no error + * \return RFAL_ERR_WRONG_STATE : Not initialized properly + * \return RFAL_ERR_PARAM : Invalid parameter or configuration + ***************************************************************************** + */ +ReturnCode rfalStartTransceive(const rfalTransceiveContext *ctx); + +/*! + ***************************************************************************** + * \brief Get Transceive State + * + * Gets current Transceive internal State + * + * \return rfalTransceiveState : the current Transceive internal State + ***************************************************************************** + */ +rfalTransceiveState rfalGetTransceiveState(void); + +/*! + ***************************************************************************** + * \brief Get Transceive Status + * + * Gets current Transceive status + * + * \return RFAL_ERR_NONE : Transceive done with no error + * \return RFAL_ERR_BUSY : Transceive ongoing + * \return RFAL_ERR_XXXX : Error occurred + * \return RFAL_ERR_TIMEOUT : No response + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PAR : Parity error detected + * \return RFAL_ERR_CRC : CRC error detected + * \return RFAL_ERR_LINK_LOSS : Link Loss - External Field is Off + * \return RFAL_ERR_RF_COLLISION : Collision detected + * \return RFAL_ERR_IO : Internal error + ***************************************************************************** + */ +ReturnCode rfalGetTransceiveStatus(void); + +/*! + ***************************************************************************** + * \brief Is Transceive in Tx + * + * Checks if Transceive is in Transmission state + * + * \return true Transmission ongoing + * \return false Not in transmission state + ***************************************************************************** + */ +bool rfalIsTransceiveInTx(void); + +/*! + ***************************************************************************** + * \brief Is Transceive in Rx + * + * Checks if Transceive is in Reception state + * + * \return true Transmission done/reception ongoing + * \return false Not in reception state + ***************************************************************************** + */ +bool rfalIsTransceiveInRx(void); + +/*! + ***************************************************************************** + * \brief Get Transceive RSSI + * + * Gets the RSSI value of the last executed Transceive in mV + * + * \param[out] rssi : RSSI value + * + * \return RFAL_ERR_NOTSUPP : Feature not supported + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalGetTransceiveRSSI(uint16_t *rssi); + +/*! + ***************************************************************************** + * \brief Is Transceive Subcarrier Detected + * + * Checks on the last executed Transceive a subcarrier was detected + * + * \return true Subcarrier was detected + * \return false No subcarrier detected | Not supported + ***************************************************************************** + */ +bool rfalIsTransceiveSubcDetected(void); + +/*! + ***************************************************************************** + * \brief RFAL Worker + * + * This runs RFAL layer, which drives the actual Transceive procedure + * It MUST be executed frequently in order to execute the RFAL internal + * states and perform the requested operations + * + ***************************************************************************** + */ +void rfalWorker(void); + +/***************************************************************************** + * ISO1443A * + *****************************************************************************/ + +/*! + ***************************************************************************** + * \brief Transceives an ISO14443A ShortFrame + * + * Sends REQA or WUPA to detect if there is any PICC in the field + * + * \param[in] txCmd: Command to be sent: + * 0x52 WUPA / ALL_REQ + * 0x26 REQA / SENS_REQ + * \param[out] rxBuf : buffer to place the response + * \param[in] rxBufLen : length of rxBuf in bytes + * \param[out] rxRcvdLen: received length in bits + * \param[in] fwt : Frame Waiting Time in 1/fc + * + * \warning If fwt is set to RFAL_FWT_NONE it will make endlessly for + * a response, which on a blocking method may not be the + * desired usage + * + * \return RFAL_ERR_NONE : If there is response + * \return RFAL_ERR_TIMEOUT : If there is no response + * \return RFAL_ERR_RF_COLLISION : A collision was detected + * + ***************************************************************************** + */ +ReturnCode rfalISO14443ATransceiveShortFrame(rfal14443AShortFrameCmd txCmd, + uint8_t *rxBuf, uint8_t rxBufLen, + uint16_t *rxRcvdLen, uint32_t fwt); + +/*! + ***************************************************************************** + * \brief Sends an ISO14443A Anticollision Frame + * + * This is used to perform ISO14443A anti-collision. + * \note Anticollision is sent without CRC + * + * + * \param[in,out] buf : reference to ANTICOLLISION command (with known + *UID if any) to be sent (also out param) reception will be place on this buf + *after bytesToSend buffer must be capable of holding a whole Anticollison frame + *(rfalNfcaSelReq) \param[in,out] bytesToSend: reference number of full bytes to + *be sent (including CMD byte and SEL_PAR) if a collision occurs will contain + *the number of clear bytes \param[in,out] bitsToSend : reference to number of + *bits (0-7) to be sent; and received (also out param) if a collision occurs + *will indicate the number of clear bits (also out param) \param[out] rxLength + *: reference to the return the received length in bits \param[in] fwt : + *Frame Waiting Time in 1/fc + * + * \return RFAL_ERR_NONE if there is no error + ***************************************************************************** + */ +ReturnCode rfalISO14443ATransceiveAnticollisionFrame(uint8_t *buf, + uint8_t *bytesToSend, + uint8_t *bitsToSend, + uint16_t *rxLength, + uint32_t fwt); + +/*! + ***************************************************************************** + * \brief Start ISO14443A Anticollision Frame transceive + * + * This starts the transceive of an ISO14443A anti-collision frame. + * \note Anticollision is sent without CRC + * + * + * \param[in,out] buf : reference to ANTICOLLISION command (with known + *UID if any) to be sent (also out param) reception will be place on this buf + *after bytesToSend buffer must be capable of holding a whole Anticollison frame + *(rfalNfcaSelReq) \param[in,out] bytesToSend: reference number of full bytes to + *be sent (including CMD byte and SEL_PAR) if a collision occurs will contain + *the number of clear bytes \param[in,out] bitsToSend : reference to number of + *bits (0-7) to be sent; and received (also out param) if a collision occurs + *will indicate the number of clear bits (also out param) \param[out] rxLength + *: reference to the return the received length in bits \param[in] fwt : + *Frame Waiting Time in 1/fc + * + * \return RFAL_ERR_NONE if there is no error + ***************************************************************************** + */ +ReturnCode rfalISO14443AStartTransceiveAnticollisionFrame(uint8_t *buf, + uint8_t *bytesToSend, + uint8_t *bitsToSend, + uint16_t *rxLength, + uint32_t fwt); + +/*! + ***************************************************************************** + * \brief Get ISO14443A Anticollision Frame Status + * + * This gets the ISO14443A anti-collision frame status. + * + * + * \return RFAL_ERR_NONE if there is no error + ***************************************************************************** + */ +ReturnCode rfalISO14443AGetTransceiveAnticollisionFrameStatus(void); + +/***************************************************************************** + * FeliCa * + *****************************************************************************/ + +/*! + ***************************************************************************** + * \brief FeliCa Poll + * + * Sends a Poll Request and collects all Poll Responses according to the + * given slots + * + * + * \param[in] slots : number of slots for the Poll Request + * \param[in] sysCode : system code (SC) for the Poll Request + * \param[in] reqCode : request code (RC) for the Poll Request + * \param[out] pollResList : list of all responses + * \param[in] pollResListSize : number of responses that can be placed in + *pollResList \param[out] devicesDetected : number of cards found \param[out] + *collisionsDetected: number of collisions detected + * + * \return RFAL_ERR_NONE : If there is no error + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or mode not set + * \return RFAL_ERR_TIMEOUT : If there is no response + ***************************************************************************** + */ +ReturnCode rfalFeliCaPoll(rfalFeliCaPollSlots slots, uint16_t sysCode, + uint8_t reqCode, rfalFeliCaPollRes *pollResList, + uint8_t pollResListSize, uint8_t *devicesDetected, + uint8_t *collisionsDetected); + +/*! + ***************************************************************************** + * \brief Start FeliCa Poll + * + * Triggers a Poll Request and all Poll Responses will be collected according + * to the given nuber of slots + * + * + * \param[in] slots : number of slots for the Poll Request + * \param[in] sysCode : system code (SC) for the Poll Request + * \param[in] reqCode : request code (RC) for the Poll Request + * \param[out] pollResList : list of all responses + * \param[in] pollResListSize : number of responses that can be placed in + *pollResList \param[out] devicesDetected : number of cards found \param[out] + *collisionsDetected: number of collisions detected + * + * \return RFAL_ERR_NONE : If there is no error + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or mode not set + ***************************************************************************** + */ +ReturnCode rfalStartFeliCaPoll(rfalFeliCaPollSlots slots, uint16_t sysCode, + uint8_t reqCode, rfalFeliCaPollRes *pollResList, + uint8_t pollResListSize, + uint8_t *devicesDetected, + uint8_t *collisionsDetected); + +/*! + ***************************************************************************** + * \brief Get FeliCa Poll Status + * + * Gets the current state of the Felica Poll Request triggered before + * by rfalStartFeliCaPoll() + * + * + * + * \return RFAL_ERR_NONE : If there is no error + * \return RFAL_ERR_BUSY : Operation ongoing + * \return RFAL_ERR_TIMEOUT : If there is no response + ***************************************************************************** + */ +ReturnCode rfalGetFeliCaPollStatus(void); + +/***************************************************************************** + * ISO15693 * + *****************************************************************************/ + +/*! + ***************************************************************************** + * \brief Sends an ISO15693 Anticollision Frame + * + * This send the Anticollision|Inventory frame (INVENTORY_REQ) + * + * \warning rxBuf must be able to contain the payload and CRC + * + * \param[in] txBuf : Buffer where outgoing message is located + * \param[in] txBufLen : Length of the outgoing message in bytes + * \param[out] rxBuf : Buffer where incoming message will be placed + * \param[in] rxBufLen : Maximum length of the incoming message in bytes + * \param[out] actLen : Actual received length in bits + * + * \return RFAL_ERR_NONE : Transceive done with no error + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or mode not set + * \return RFAL_ERR_IO : Internal error + ***************************************************************************** + */ +ReturnCode rfalISO15693TransceiveAnticollisionFrame(uint8_t *txBuf, + uint8_t txBufLen, + uint8_t *rxBuf, + uint8_t rxBufLen, + uint16_t *actLen); + +/*! + ***************************************************************************** + * \brief Sends an ISO15693 Anticollision EOF + * + * This sends the Anticollision|Inventory EOF used as a slot marker + * + * \warning rxBuf must be able to contain the payload and CRC + * + * \param[out] rxBuf : Buffer where incoming message will be placed + * \param[in] rxBufLen : Maximum length of the incoming message in bytes + * \param[out] actLen : Actual received length in bits + * + * \return RFAL_ERR_NONE : Transceive done with no error + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or mode not set + * \return RFAL_ERR_IO : Internal error + ***************************************************************************** + */ +ReturnCode rfalISO15693TransceiveEOFAnticollision(uint8_t *rxBuf, + uint8_t rxBufLen, + uint16_t *actLen); + +/*! + ***************************************************************************** + * \brief Sends an ISO15693 EOF + * + * This is method sends an ISO15693 (EoF) used for a Write operation + * + * \warning rxBuf must be able to contain the payload and CRC + * + * \param[out] rxBuf : Buffer where incoming message will be placed + * \param[in] rxBufLen : Maximum length of the incoming message in bytes + * \param[out] actLen : Actual received length in bytes + * + * \return RFAL_ERR_NONE : Transceive done with no error + * \return RFAL_ERR_IO : Internal error + ***************************************************************************** + */ +ReturnCode rfalISO15693TransceiveEOF(uint8_t *rxBuf, uint16_t rxBufLen, + uint16_t *actLen); + +/*! + ***************************************************************************** + * \brief Transceive Blocking Tx + * + * This is method triggers a Transceive and executes it blocking until the + * Tx has been completed + * + * \param[in] txBuf : Buffer where outgoing message is located + * \param[in] txBufLen : Length of the outgoing message in bytes + * \param[out] rxBuf : Buffer where incoming message will be placed + * \param[in] rxBufLen : Maximum length of the incoming message in bytes + * \param[out] actLen : Actual received length in bits + * \param[in] flags : TransceiveFlags indication special handling + * \param[in] fwt : Frame Waiting Time in 1/fc + * + * \return RFAL_ERR_NONE : Transceive done with no error + * \return RFAL_ERR_BUSY : Transceive ongoing + * \return RFAL_ERR_XXXX : Error occurred + * \return RFAL_ERR_LINK_LOSS : Link Loss - External Field is Off + * \return RFAL_ERR_RF_COLLISION : Collision detected + * \return RFAL_ERR_IO : Internal error + ***************************************************************************** + */ +ReturnCode rfalTransceiveBlockingTx(uint8_t *txBuf, uint16_t txBufLen, + uint8_t *rxBuf, uint16_t rxBufLen, + uint16_t *actLen, uint32_t flags, + uint32_t fwt); + +/*! + ***************************************************************************** + * \brief Transceive Blocking Rx + * + * This is method executes the reception of an ongoing Transceive triggered + * before by rfalTransceiveBlockingTx() + * + * \return RFAL_ERR_NONE : Transceive done with no error + * \return RFAL_ERR_BUSY : Transceive ongoing + * \return RFAL_ERR_XXXX : Error occurred + * \return RFAL_ERR_TIMEOUT : No response + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PAR : Parity error detected + * \return RFAL_ERR_CRC : CRC error detected + * \return RFAL_ERR_LINK_LOSS : Link Loss - External Field is Off + * \return RFAL_ERR_RF_COLLISION : Collision detected + * \return RFAL_ERR_IO : Internal error + ***************************************************************************** + */ +ReturnCode rfalTransceiveBlockingRx(void); + +/*! + ***************************************************************************** + * \brief Transceive Blocking + * + * This is method triggers a Transceive and executes it blocking until it + * has been completed + * + * \param[in] txBuf : Buffer where outgoing message is located + * \param[in] txBufLen : Length of the outgoing message in bytes + * \param[out] rxBuf : Buffer where incoming message will be placed + * \param[in] rxBufLen : Maximum length of the incoming message in bytes + * \param[out] actLen : Actual received length in bytes + * \param[in] flags : TransceiveFlags indication special handling + * \param[in] fwt : Frame Waiting Time in 1/fc + * + * \return RFAL_ERR_NONE : Transceive done with no error + * \return RFAL_ERR_BUSY : Transceive ongoing + * \return RFAL_ERR_XXXX : Error occurred + * \return RFAL_ERR_TIMEOUT : No response + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PAR : Parity error detected + * \return RFAL_ERR_CRC : CRC error detected + * \return RFAL_ERR_LINK_LOSS : Link Loss - External Field is Off + * \return RFAL_ERR_RF_COLLISION : Collision detected + * \return RFAL_ERR_IO : Internal error + ***************************************************************************** + */ +ReturnCode rfalTransceiveBlockingTxRx(uint8_t *txBuf, uint16_t txBufLen, + uint8_t *rxBuf, uint16_t rxBufLen, + uint16_t *actLen, uint32_t flags, + uint32_t fwt); + +/***************************************************************************** + * Listen Mode * + *****************************************************************************/ + +/*! + ***************************************************************************** + * \brief Is external Field On + * + * Checks if external field (other peer/device) is on/detected + * + * \return true External field is On + * \return false No external field is detected + * + ***************************************************************************** + */ +bool rfalIsExtFieldOn(void); + +/*! + ***************************************************************************** + * \brief Listen Mode start + * + * Configures RF Chip to go into listen mode enabling the given technologies + * + * + * \param[in] lmMask: mask with the enabled/disabled listen modes + * use: RFAL_LM_MASK_NFCA ; RFAL_LM_MASK_NFCB ; + * RFAL_LM_MASK_NFCF ; RFAL_LM_MASK_ACTIVE_P2P + * \param[in] confA: pointer to Passive A configurations (NULL if disabled) + * \param[in] confB: pointer to Passive B configurations (NULL if disabled) + * \param[in] confF: pointer to Passive F configurations (NULL if disabled) + * \param[in] rxBuf: buffer to place incoming data + * \param[in] rxBufLen: length in bits of rxBuf + * \param[in] rxLen: pointer to write the data length in bits placed into + *rxBuf + * + * + * \return RFAL_ERR_WRONG_STATE : Not initialized properly + * \return RFAL_ERR_PARAM : Invalid parametere mask + * \return RFAL_ERR_NONE : Done with no error + * + ***************************************************************************** + */ +ReturnCode rfalListenStart(uint32_t lmMask, const rfalLmConfPA *confA, + const rfalLmConfPB *confB, const rfalLmConfPF *confF, + uint8_t *rxBuf, uint16_t rxBufLen, uint16_t *rxLen); + +/*! + ***************************************************************************** + * \brief Listen Mode start Sleeping + * + * \param[in] sleepSt : sleep state to be set + * \param[in] rxBuf : buffer to place incoming data + * \param[in] rxBufLen : length in bits of rxBuf + * \param[in] rxLen : pointer to write the data length in bits placed into + *rxBuf + * + * \return RFAL_ERR_WRONG_STATE : Not initialized properly + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_NONE : Done with no error + * + ***************************************************************************** + */ +ReturnCode rfalListenSleepStart(rfalLmState sleepSt, uint8_t *rxBuf, + uint16_t rxBufLen, uint16_t *rxLen); + +/*! + ***************************************************************************** + * \brief Listen Mode Stop + * + * Disables the listen mode on the RF Chip + * + * \warning the listen mode will be disabled immediately on the RFchip + *regardless of any ongoing operations like Transceive + * + * \return RFAL_ERR_NONE : Done with no error + * + ***************************************************************************** + */ +ReturnCode rfalListenStop(void); + +/*! + ***************************************************************************** + * \brief Listen Mode get state + * + * Sets the new state of the Listen Mode and applies the necessary changes + * on the RF Chip + * + * \param[out] dataFlag: indicates that Listen Mode has rcvd data and caller + * must process it. The received message is located + * at the rxBuf passed on rfalListenStart(). + * rfalListenSetState() will clear this flag + * if NULL output parameter will no be written/returned + * \param[out] lastBR: bit rate detected of the last initiator request + * if NULL output parameter will no be written/returned + * + * \return rfalLmState RFAL_LM_STATE_NOT_INIT : LM not initialized properly + * Any Other : LM State + * + ***************************************************************************** + */ +rfalLmState rfalListenGetState(bool *dataFlag, rfalBitRate *lastBR); + +/*! + ***************************************************************************** + * \brief Listen Mode set state + * + * Sets the new state of the Listen Mode and applies the necessary changes + * on the RF Chip + * + * \param[in] newSt : New state to go to + * + * \return RFAL_ERR_WRONG_STATE : Not initialized properly + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_NONE : Done with no error + * + ***************************************************************************** + */ +ReturnCode rfalListenSetState(rfalLmState newSt); + +/***************************************************************************** + * Wake-Up Mode * + *****************************************************************************/ + +/*! + ***************************************************************************** + * \brief Wake-Up Mode Start + * + * Sets the RF Chip in Low Power Wake-Up Mode according to the given + * configuration. + * + * \param[in] config : Generic Wake-Up configuration provided by lower + * layers. If NULL will automatically configure the + * Wake-Up mode + * + * \return RFAL_ERR_WRONG_STATE : Not initialized properly + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_NONE : Done with no error + * + ***************************************************************************** + */ +ReturnCode rfalWakeUpModeStart(const rfalWakeUpConfig *config); + +/*! + ***************************************************************************** + * \brief Wake-Up has Woke + * + * Returns true if the Wake-Up mode is enabled and it has already received + * the indication from the RF Chip that the surrounding environment has changed + * and flagged at least one wake-Up interrupt + * + * \return true : Wake-Up mode enabled and has received a wake-up IRQ + * \return false : no Wake-Up IRQ has been received + * + ***************************************************************************** + */ +bool rfalWakeUpModeHasWoke(void); + +/*! + ***************************************************************************** + * \brief Wake-Up is Enabled + * + * Returns true if the Wake-Up mode is enabled and it has already completed + * its starting up sequence. + * When the option to obtain a reference value from WU is enabled, the startup + * sequence takes longer. Otherwise WU mode is running after + *rfalWakeUpModeStart + * + * \return true : Wake-Up mode enabled + * \return false : Wake-Up mode not enabled + * + ***************************************************************************** + */ +bool rfalWakeUpModeIsEnabled(void); + +/*! + ***************************************************************************** + * \brief Wake-Up Get Info + * + * Returns the current information while Wake-up mode is running + * + * \warning The information returned will only be updated in case force is + * enabled, or if an event IRQ has happen. + * Otherwise the info will be filled with zeros. + * + * \param[in] force : Force info update info by retrieving it from device + * \param[out] info : pointer where WU mode info is to be stored + * + * \return RFAL_ERR_WRONG_STATE : Not initialized properly + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_NONE : Done with no error + ***************************************************************************** + */ +ReturnCode rfalWakeUpModeGetInfo(bool force, rfalWakeUpInfo *info); + +/*! + ***************************************************************************** + * \brief Wake-Up Mode Stop + * + * Stops the Wake-Up Mode + * + * \return RFAL_ERR_WRONG_STATE : Not initialized properly + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_NONE : Done with no error + * + ***************************************************************************** + */ +ReturnCode rfalWakeUpModeStop(void); + +/*! + ***************************************************************************** + * \brief WLC-P WPT Monitor Start + * + * After WLC-P reaches its WPT state it starts the monitoring for Impedance + * change and WPT Stop sequeence. + * + * \param[in] config : Generic Wake-Up configuration provided by lower + * layers. If NULL will automatically configure the + * WLC-P WPT Phase + * + * \warning several parameters held in config will be overwritten by the driver + * with the appropriate settings for WPT monitoring. + * + * \return RFAL_ERR_WRONG_STATE : Not initialized properly + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_NONE : Done with no error + * + ***************************************************************************** + */ +ReturnCode rfalWlcPWptMonitorStart(const rfalWakeUpConfig *config); + +/*! + ***************************************************************************** + * \brief WLC-P WPT Monitor Start Stop + * + * Stops the monitoring of WLC-P WPT Phase + * + * \return RFAL_ERR_WRONG_STATE : Not initialized properly + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_NONE : Done with no error + * + ***************************************************************************** + */ +ReturnCode rfalWlcPWptMonitorStop(void); + +/*! + ***************************************************************************** + * \brief WLC-P WPT FOD is Detected + * + * Returns true if the WLC-P WPT is monitored and it has already received + * the indication from the RF Chip that FOD was detected. + * + * \return true : WLC-P WPT is monitored and has identified a FOD + * \return false : no FOD has been identified + * + ***************************************************************************** + */ +bool rfalWlcPWptIsFodDetected(void); + +/*! + ***************************************************************************** + * \brief WLC-P WPT Stop is Detected + * + * Returns true if the WLC-P WPT is monitored and it has already received + * the indication from the RF Chip that a WPT Stop sequence was detected. + * + * \return true : WLC-P WPT is monitored and has identified a WPT Stop + * \return false : no WPT Stop IRQ has been identified + * + ***************************************************************************** + */ +bool rfalWlcPWptIsStopDetected(void); + +/*! + ***************************************************************************** + * \brief Low Power Mode Start + * + * Sets the RF Chip in Low Power Mode. + * In this mode the RF Chip is placed in Low Power Mode, similar to Wake-up + * mode but no operation nor period measurement is performed. + * Mode must be terminated by rfalLowPowerModeStop() + * + * \param[in] mode : low power mode to be set + * + * \return RFAL_ERR_WRONG_STATE : Not initialized properly + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_NONE : Done with no error + * + ***************************************************************************** + */ +ReturnCode rfalLowPowerModeStart(rfalLpMode mode); + +/*! + ***************************************************************************** + * \brief Low Power Mode Stop + * + * Stops the Low Power Mode re-enabling the device + * + * \return RFAL_ERR_WRONG_STATE : Not initialized properly + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_NONE : Done with no error + * + ***************************************************************************** + */ +ReturnCode rfalLowPowerModeStop(void); + +#endif /* RFAL_RF_H */ + +/** + * @} + * + * @} + * + * @} + */ diff --git a/core/embed/io/nfc/rfal/include/rfal_st25tb.h b/core/embed/io/nfc/rfal/include/rfal_st25tb.h new file mode 100644 index 0000000000..613bab25db --- /dev/null +++ b/core/embed/io/nfc/rfal/include/rfal_st25tb.h @@ -0,0 +1,335 @@ + +/****************************************************************************** + * @attention + * + * COPYRIGHT 2016 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/* + * PROJECT: ST25R391x firmware + * Revision: + * LANGUAGE: ISO C99 + */ + +/*! \file rfal_st25tb.h + * + * \author Gustavo Patricio + * + * \brief Implementation of ST25TB interface + * + * + * \addtogroup RFAL + * @{ + * + * \addtogroup RFAL-AL + * \brief RFAL Abstraction Layer + * @{ + * + * \addtogroup ST25TB + * \brief RFAL ST25TB Module + * @{ + * + */ + +#ifndef RFAL_ST25TB_H +#define RFAL_ST25TB_H + +/* + ****************************************************************************** + * INCLUDES + ****************************************************************************** + */ +#include "rfal_nfcb.h" +#include "rfal_platform.h" +#include "rfal_rf.h" +#include "rfal_utils.h" + +/* + ****************************************************************************** + * GLOBAL DEFINES + ****************************************************************************** + */ + +#define RFAL_ST25TB_CHIP_ID_LEN 1U /*!< ST25TB chip ID length */ +#define RFAL_ST25TB_CRC_LEN 2U /*!< ST25TB CRC length */ +#define RFAL_ST25TB_UID_LEN 8U /*!< ST25TB Unique ID length */ +#define RFAL_ST25TB_BLOCK_LEN 4U /*!< ST25TB Data Block length */ + +/* +****************************************************************************** +* GLOBAL MACROS +****************************************************************************** +*/ + +/* +****************************************************************************** +* GLOBAL TYPES +****************************************************************************** +*/ +typedef uint8_t rfalSt25tbUID[RFAL_ST25TB_UID_LEN]; /*!< ST25TB UID type */ +typedef uint8_t + rfalSt25tbBlock[RFAL_ST25TB_BLOCK_LEN]; /*!< ST25TB Block type */ + +/*! ST25TB listener device (PICC) struct */ +typedef struct { + uint8_t chipID; /*!< Device's session Chip ID */ + rfalSt25tbUID UID; /*!< Device's UID */ + bool isDeselected; /*!< Device deselect flag */ +} rfalSt25tbListenDevice; + +/* +****************************************************************************** +* GLOBAL FUNCTION PROTOTYPES +****************************************************************************** +*/ + +/*! + ***************************************************************************** + * \brief Initialize ST25TB Poller mode + * + * This methods configures RFAL RF layer to perform as a + * ST25TB Poller/RW including all default timings + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or mode not set + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalSt25tbPollerInitialize(void); + +/*! + ***************************************************************************** + * \brief ST25TB Poller Check Presence + * + * This method checks if a ST25TB Listen device (PICC) is present on the field + * by sending an Initiate command + * + * \param[out] chipId : if successfully retrieved, the device's chip ID + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_TIMEOUT : Timeout error, no listener device detected + * \return RFAL_ERR_RF_COLLISION : Collision detected one or more device in the + *field \return RFAL_ERR_PROTO : Protocol error detected \return + *RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalSt25tbPollerCheckPresence(uint8_t *chipId); + +/*! + ***************************************************************************** + * \brief ST25TB Poller Collision Resolution + * + * This method performs ST25TB Collision resolution, selects the each device, + * retrieves its UID and then deselects. + * In case only one device is identified the ST25TB device is left in select + * state. + * + * \param[in] devLimit : device limit value, and size st25tbDevList + * \param[out] st25tbDevList : ST35TB listener device info + * \param[out] devCnt : Devices found counter + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_TIMEOUT : Timeout error, no listener device detected + * \return RFAL_ERR_RF_COLLISION : Collision detected one or more device in the + *field \return RFAL_ERR_PROTO : Protocol error detected \return + *RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalSt25tbPollerCollisionResolution( + uint8_t devLimit, rfalSt25tbListenDevice *st25tbDevList, uint8_t *devCnt); + +/*! + ***************************************************************************** + * \brief ST25TB Poller Initiate + * + * This method sends an Initiate command + * + * If a single device responds the chip ID will be retrieved + * + * \param[out] chipId : chip ID of the device + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_TIMEOUT : Timeout error, no listener device detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalSt25tbPollerInitiate(uint8_t *chipId); + +/*! + ***************************************************************************** + * \brief ST25TB Poller Pcall + * + * This method sends a Pcall command + * If successful the device's chip ID will be retrieved + * + * \param[out] chipId : Chip ID of the device + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_TIMEOUT : Timeout error, no listener device detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalSt25tbPollerPcall(uint8_t *chipId); + +/*! + ***************************************************************************** + * \brief ST25TB Poller Slot Marker + * + * This method sends a Slot Marker + * + * If a single device responds the chip ID will be retrieved + * + * \param[in] slotNum : Slot Number + * \param[out] chipIdRes : Chip ID of the device + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_TIMEOUT : Timeout error, no listener device detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalSt25tbPollerSlotMarker(uint8_t slotNum, uint8_t *chipIdRes); + +/*! + ***************************************************************************** + * \brief ST25TB Poller Select + * + * This method sends a ST25TB Select command with the given chip ID. + * + * If the device is already in Selected state and receives an incorrect chip + * ID, it goes into Deselected state + * + * \param[in] chipId : chip ID of the device to be selected + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_TIMEOUT : Timeout error, no listener device detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalSt25tbPollerSelect(uint8_t chipId); + +/*! + ***************************************************************************** + * \brief ST25TB Get UID + * + * This method sends a Get_UID command + * + * If a single device responds the chip UID will be retrieved + * + * \param[out] UID : UID of the found device + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_TIMEOUT : Timeout error, no listener device detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalSt25tbPollerGetUID(rfalSt25tbUID *UID); + +/*! + ***************************************************************************** + * \brief ST25TB Poller Read Block + * + * This method reads a block of the ST25TB + * + * \param[in] blockAddress : address of the block to be read + * \param[out] blockData : location to place the data read from block + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_TIMEOUT : Timeout error, no listener device detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalSt25tbPollerReadBlock(uint8_t blockAddress, + rfalSt25tbBlock *blockData); + +/*! + ***************************************************************************** + * \brief ST25TB Poller Write Block + * + * This method writes a block of the ST25TB + * + * \param[in] blockAddress : address of the block to be written + * \param[in] blockData : data to be written on the block + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_TIMEOUT : Timeout error, no listener device detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalSt25tbPollerWriteBlock(uint8_t blockAddress, + const rfalSt25tbBlock *blockData); + +/*! + ***************************************************************************** + * \brief ST25TB Poller Completion + * + * This method sends a completion command to the ST25TB. After the + * completion the card no longer will reply to any command. + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_TIMEOUT : Timeout error, no listener device detected + * \return RFAL_ERR_PROTO : Protocol error detected, invalid SENSB_RES + *received \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalSt25tbPollerCompletion(void); + +/*! + ***************************************************************************** + * \brief ST25TB Poller Reset to Inventory + * + * This method sends a Reset to Inventory command to the ST25TB. + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_TIMEOUT : Timeout error, no listener device detected + * \return RFAL_ERR_PROTO : Protocol error detected, invalid SENSB_RES + *received \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalSt25tbPollerResetToInventory(void); + +#endif /* RFAL_ST25TB_H */ + +/** + * @} + * + * @} + * + * @} + */ diff --git a/core/embed/io/nfc/rfal/include/rfal_st25xv.h b/core/embed/io/nfc/rfal/include/rfal_st25xv.h new file mode 100644 index 0000000000..b5a39c897d --- /dev/null +++ b/core/embed/io/nfc/rfal/include/rfal_st25xv.h @@ -0,0 +1,807 @@ + +/****************************************************************************** + * @attention + * + * COPYRIGHT 2016 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/* + * PROJECT: ST25R391x firmware + * Revision: + * LANGUAGE: ISO C99 + */ + +/*! \file rfal_st25xv.h + * + * \author Gustavo Patricio + * + * \brief NFC-V ST25 NFC-V Tag specific features + * + * This module provides support for ST's specific features available on + * NFC-V (ISO15693) tag families: ST25D, ST25TV, M24LR + * + * + * \addtogroup RFAL + * @{ + * + * \addtogroup RFAL-AL + * \brief RFAL Abstraction Layer + * @{ + * + * \addtogroup ST25xV + * \brief RFAL ST25xV Module + * @{ + * + */ + +#ifndef RFAL_ST25xV_H +#define RFAL_ST25xV_H + +/* + ****************************************************************************** + * INCLUDES + ****************************************************************************** + */ +#include "rfal_platform.h" +#include "rfal_rf.h" +#include "rfal_utils.h" + +/* + ****************************************************************************** + * GLOBAL DEFINES + ****************************************************************************** + */ + +#define RFAL_NFCV_BLOCKNUM_M24LR_LEN \ + 2U /*!< Block Number length of MR24LR tags: 16 bits */ + +/*! + ***************************************************************************** + * \brief NFC-V Poller Read Single Block (M24LR) + * + * Reads a Single Block from a M24LR tag which has the number of blocks + * bigger than 256 (M24LR16 ; M24LR64) + * + * \param[in] flags : Flags to be used: Sub-carrier; Data_rate; Option + * default: RFAL_NFCV_REQ_FLAG_DEFAULT + * \param[in] uid : UID of the device to be put to be read + * if not provided Select mode will be used + * \param[in] blockNum : Number of the block to read (16 bits) + * \param[out] rxBuf : buffer to store response (also with RES_FLAGS) + * \param[in] rxBufLen : length of rxBuf + * \param[out] rcvLen : number of bytes received + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_CRC : CRC error detected + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_TIMEOUT : Timeout error + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalST25xVPollerM24LRReadSingleBlock( + uint8_t flags, const uint8_t* uid, uint16_t blockNum, uint8_t* rxBuf, + uint16_t rxBufLen, uint16_t* rcvLen); + +/*! + ***************************************************************************** + * \brief NFC-V Poller Fast Read Single Block (M24LR) + * + * Reads a Single Block from a M24LR tag which has the number of blocks + * bigger than 256 (M24LR16 ; M24LR64) using ST Fast mode + * + * \param[in] flags : Flags to be used: Sub-carrier; Data_rate; Option + * default: RFAL_NFCV_REQ_FLAG_DEFAULT + * \param[in] uid : UID of the device to be put to be read + * if not provided Select mode will be used + * \param[in] blockNum : Number of the block to read (16 bits) + * \param[out] rxBuf : buffer to store response (also with RES_FLAGS) + * \param[in] rxBufLen : length of rxBuf + * \param[out] rcvLen : number of bytes received + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_CRC : CRC error detected + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_TIMEOUT : Timeout error + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalST25xVPollerM24LRFastReadSingleBlock( + uint8_t flags, const uint8_t* uid, uint16_t blockNum, uint8_t* rxBuf, + uint16_t rxBufLen, uint16_t* rcvLen); + +/*! + ***************************************************************************** + * \brief NFC-V Poller Write Single Block (M24LR) + * + * Writes a Single Block from a M24LR tag which has the number of blocks + * bigger than 256 (M24LR16 ; M24LR64) + * + * \param[in] flags : Flags to be used: Sub-carrier; Data_rate; Option + * for NFC-Forum use: RFAL_NFCV_REQ_FLAG_DEFAULT + * \param[in] uid : UID of the device to be put to be written + * if not provided Select mode will be used + * \param[in] blockNum : Number of the block to write (16 bits) + * \param[in] wrData : data to be written on the given block + * \param[in] blockLen : number of bytes of a block + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_CRC : CRC error detected + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_TIMEOUT : Timeout error + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalST25xVPollerM24LRWriteSingleBlock(uint8_t flags, + const uint8_t* uid, + uint16_t blockNum, + const uint8_t* wrData, + uint8_t blockLen); + +/*! + ***************************************************************************** + * \brief NFC-V Poller Read Multiple Blocks (M24LR) + * + * Reads Multiple Blocks from a device from a M24LR tag which has the number of + *blocks bigger than 256 (M24LR16 ; M24LR64) + * + * \param[in] flags : Flags to be used: Sub-carrier; Data_rate; Option + * for NFC-Forum use: RFAL_NFCV_REQ_FLAG_DEFAULT + * \param[in] uid : UID of the device to be put to be read + * if not provided Select mode will be used + * \param[in] firstBlockNum : first block to be read (16 bits) + * \param[in] numOfBlocks : number of block to read + * \param[out] rxBuf : buffer to store response (also with RES_FLAGS) + * \param[in] rxBufLen : length of rxBuf + * \param[out] rcvLen : number of bytes received + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_CRC : CRC error detected + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_TIMEOUT : Timeout error + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalST25xVPollerM24LRReadMultipleBlocks( + uint8_t flags, const uint8_t* uid, uint16_t firstBlockNum, + uint8_t numOfBlocks, uint8_t* rxBuf, uint16_t rxBufLen, uint16_t* rcvLen); + +/*! + ***************************************************************************** + * \brief NFC-V Poller Fast Read Multiple Blocks (M24LR) + * + * Reads Multiple Blocks from a device from a M24LR tag which has the number of + *blocks bigger than 256 (M24LR16 ; M24LR64) using ST Fast mode + * + * \param[in] flags : Flags to be used: Sub-carrier; Data_rate; Option + * for NFC-Forum use: RFAL_NFCV_REQ_FLAG_DEFAULT + * \param[in] uid : UID of the device to be put to be read + * if not provided Select mode will be used + * \param[in] firstBlockNum : first block to be read (16 bits) + * \param[in] numOfBlocks : number of block to read + * \param[out] rxBuf : buffer to store response (also with RES_FLAGS) + * \param[in] rxBufLen : length of rxBuf + * \param[out] rcvLen : number of bytes received + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_CRC : CRC error detected + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_TIMEOUT : Timeout error + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalST25xVPollerM24LRFastReadMultipleBlocks( + uint8_t flags, const uint8_t* uid, uint16_t firstBlockNum, + uint8_t numOfBlocks, uint8_t* rxBuf, uint16_t rxBufLen, uint16_t* rcvLen); + +/*! + ***************************************************************************** + * \brief NFC-V Poller Fast Read Single Block + * + * Reads a Single Block from a device (VICC) using ST Fast mode + * + * \param[in] flags : Flags to be used: Sub-carrier; Data_rate; Option + * for NFC-Forum use: RFAL_NFCV_REQ_FLAG_DEFAULT + * \param[in] uid : UID of the device to be put to be read + * if not provided Select mode will be used + * \param[in] blockNum : Number of the block to read + * \param[out] rxBuf : buffer to store response (also with RES_FLAGS) + * \param[in] rxBufLen : length of rxBuf + * \param[out] rcvLen : number of bytes received + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_CRC : CRC error detected + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_TIMEOUT : Timeout error + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalST25xVPollerFastReadSingleBlock(uint8_t flags, + const uint8_t* uid, + uint8_t blockNum, uint8_t* rxBuf, + uint16_t rxBufLen, + uint16_t* rcvLen); + +/*! + ***************************************************************************** + * \brief NFC-V Poller Fast Read Multiple Blocks + * + * Reads Multiple Blocks from a device (VICC) using ST Fast mode + * + * \param[in] flags : Flags to be used: Sub-carrier; Data_rate; Option + * for NFC-Forum use: RFAL_NFCV_REQ_FLAG_DEFAULT + * \param[in] uid : UID of the device to be put to be read + * if not provided Select mode will be used + * \param[in] firstBlockNum : first block to be read + * \param[in] numOfBlocks : number of block to read + * \param[out] rxBuf : buffer to store response (also with RES_FLAGS) + * \param[in] rxBufLen : length of rxBuf + * \param[out] rcvLen : number of bytes received + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_CRC : CRC error detected + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_TIMEOUT : Timeout error + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalST25xVPollerFastReadMultipleBlocks( + uint8_t flags, const uint8_t* uid, uint8_t firstBlockNum, + uint8_t numOfBlocks, uint8_t* rxBuf, uint16_t rxBufLen, uint16_t* rcvLen); + +/*! + ***************************************************************************** + * \brief NFC-V Poller Fast Extended Read Single Block + * + * Reads a Single Block from a device (VICC) supporting extended commands using + *ST Fast mode + * + * \param[in] flags : Flags to be used: Sub-carrier; Data_rate; Option + * for NFC-Forum use: RFAL_NFCV_REQ_FLAG_DEFAULT + * \param[in] uid : UID of the device to be put to be read + * if not provided Select mode will be used + * \param[in] blockNum : Number of the block to read (16 bits) + * \param[out] rxBuf : buffer to store response (also with RES_FLAGS) + * \param[in] rxBufLen : length of rxBuf + * \param[out] rcvLen : number of bytes received + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_CRC : CRC error detected + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_TIMEOUT : Timeout error + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalST25xVPollerFastExtendedReadSingleBlock( + uint8_t flags, const uint8_t* uid, uint16_t blockNum, uint8_t* rxBuf, + uint16_t rxBufLen, uint16_t* rcvLen); + +/*! + ***************************************************************************** + * \brief NFC-V Poller Fast Extended Read Multiple Blocks + * + * Reads Multiple Blocks from a device (VICC) supporting extended commands using + *ST Fast mode + * + * \param[in] flags : Flags to be used: Sub-carrier; Data_rate; Option + * for NFC-Forum use: RFAL_NFCV_REQ_FLAG_DEFAULT + * \param[in] uid : UID of the device to be put to be read + * if not provided Select mode will be used + * \param[in] firstBlockNum : first block to be read (16 bits) + * \param[in] numOfBlocks : number of consecutive blocks to read (16 bits) + * \param[out] rxBuf : buffer to store response (also with RES_FLAGS) + * \param[in] rxBufLen : length of rxBuf + * \param[out] rcvLen : number of bytes received + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_CRC : CRC error detected + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_TIMEOUT : Timeout error + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalST25xVPollerFastExtReadMultipleBlocks( + uint8_t flags, const uint8_t* uid, uint16_t firstBlockNum, + uint16_t numOfBlocks, uint8_t* rxBuf, uint16_t rxBufLen, uint16_t* rcvLen); + +/*! + ***************************************************************************** + * \brief NFC-V Poller Read Configuration + * + * Reads static configuration registers at the Pointer address + * + * \param[in] flags : Flags to be used: Sub-carrier; Data_rate; Option + * for NFC-Forum use: RFAL_NFCV_REQ_FLAG_DEFAULT + * \param[in] uid : UID of the device to be put to be read + * if not provided Select mode will be used + * \param[in] pointer : Pointer address + * \param[out] regValue : Register value + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_CRC : CRC error detected + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_TIMEOUT : Timeout error + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalST25xVPollerReadConfiguration(uint8_t flags, const uint8_t* uid, + uint8_t pointer, + uint8_t* regValue); + +/*! + ***************************************************************************** + * \brief NFC-V Poller Write Configuration + * + * Writes static configuration registers at the Pointer address + * + * \param[in] flags : Flags to be used: Sub-carrier; Data_rate; Option + * for NFC-Forum use: RFAL_NFCV_REQ_FLAG_DEFAULT + * \param[in] uid : UID of the device to be put to be read + * if not provided Select mode will be used + * \param[in] pointer : Pointer address + * \param[in] regValue : Register value + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_CRC : CRC error detected + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_TIMEOUT : Timeout error + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalST25xVPollerWriteConfiguration(uint8_t flags, const uint8_t* uid, + uint8_t pointer, + uint8_t regValue); + +/*! + ***************************************************************************** + * \brief NFC-V Poller Read Dynamic Configuration + * + * Reads dynamic registers at the Pointer address + * + * \param[in] flags : Flags to be used: Sub-carrier; Data_rate; Option + * for NFC-Forum use: RFAL_NFCV_REQ_FLAG_DEFAULT + * \param[in] uid : UID of the device to be put to be read + * if not provided Select mode will be used + * \param[in] pointer : Pointer address + * \param[out] regValue : Register value + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_CRC : CRC error detected + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_TIMEOUT : Timeout error + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalST25xVPollerReadDynamicConfiguration(uint8_t flags, + const uint8_t* uid, + uint8_t pointer, + uint8_t* regValue); + +/*! + ***************************************************************************** + * \brief NFC-V Poller Write Dynamic Configuration + * + * Writes dynamic registers at the Pointer address + * + * \param[in] flags : Flags to be used: Sub-carrier; Data_rate; Option + * for NFC-Forum use: RFAL_NFCV_REQ_FLAG_DEFAULT + * \param[in] uid : UID of the device to be put to be read + * if not provided Select mode will be used + * \param[in] pointer : Pointer address + * \param[in] regValue : Register value + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_CRC : CRC error detected + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_TIMEOUT : Timeout error + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalST25xVPollerWriteDynamicConfiguration(uint8_t flags, + const uint8_t* uid, + uint8_t pointer, + uint8_t regValue); + +/*! + ***************************************************************************** + * \brief NFC-V Poller Fast Read Dynamic Configuration + * + * Reads dynamic registers at the Pointer address using ST Fast mode + * + * \param[in] flags : Flags to be used: Sub-carrier; Data_rate; Option + * for NFC-Forum use: RFAL_NFCV_REQ_FLAG_DEFAULT + * \param[in] uid : UID of the device to be put to be read + * if not provided Select mode will be used + * \param[in] pointer : Pointer address + * \param[out] regValue : Register value + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_CRC : CRC error detected + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_TIMEOUT : Timeout error + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalST25xVPollerFastReadDynamicConfiguration(uint8_t flags, + const uint8_t* uid, + uint8_t pointer, + uint8_t* regValue); + +/*! + ***************************************************************************** + * \brief NFC-V Poller Fast Write Dynamic Configuration + * + * Writes dynamic registers at the Pointer address using ST Fast mode + * + * \param[in] flags : Flags to be used: Sub-carrier; Data_rate; Option + * for NFC-Forum use: RFAL_NFCV_REQ_FLAG_DEFAULT + * \param[in] uid : UID of the device to be put to be read + * if not provided Select mode will be used + * \param[in] pointer : Pointer address + * \param[in] regValue : Register value + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_CRC : CRC error detected + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_TIMEOUT : Timeout error + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalST25xVPollerFastWriteDynamicConfiguration(uint8_t flags, + const uint8_t* uid, + uint8_t pointer, + uint8_t regValue); + +/*! + ***************************************************************************** + * \brief NFC-V Poller Present Password + * + * Sends the Present Password command + * + * \param[in] flags : Flags to be used: Sub-carrier; Data_rate; Option + * for NFC-Forum use: RFAL_NFCV_REQ_FLAG_DEFAULT + * \param[in] uid : UID of the device to be put to be read + * if not provided Select mode will be used + * \param[in] pwdNum : Password number + * \param[in] pwd : Password + * \param[in] pwdLen : Password length + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_CRC : CRC error detected + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_TIMEOUT : Timeout error + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalST25xVPollerPresentPassword(uint8_t flags, const uint8_t* uid, + uint8_t pwdNum, const uint8_t* pwd, + uint8_t pwdLen); + +/*! + ***************************************************************************** + * \brief NFC-V Poller Write Password + * + * Sends the Write Password command + * + * \param[in] flags : Flags to be used: Sub-carrier; Data_rate; Option + * for NFC-Forum use: RFAL_NFCV_REQ_FLAG_DEFAULT + * \param[in] uid : UID of the device to be put to be read + * if not provided Select mode will be used + * \param[in] pwdNum : Password number + * \param[in] pwd : Password + * \param[in] pwdLen : Password length + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_CRC : CRC error detected + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_TIMEOUT : Timeout error + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalST25xVPollerWritePassword(uint8_t flags, const uint8_t* uid, + uint8_t pwdNum, const uint8_t* pwd, + uint8_t pwdLen); + +/*! + ***************************************************************************** + * \brief NFC-V Poller Get Random Number + * + * Returns a 16 bit random number + * + * \param[in] flags : Flags to be used: Sub-carrier; Data_rate; Option + * for NFC-Forum use: RFAL_NFCV_REQ_FLAG_DEFAULT + * \param[in] uid : UID of the device to be put to be read + * if not provided Select mode will be used + * \param[out] rxBuf : buffer to store response (also with RES_FLAGS) + * \param[in] rxBufLen : length of rxBuf + * \param[out] rcvLen : number of bytes received + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_CRC : CRC error detected + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_TIMEOUT : Timeout error + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalST25xVPollerGetRandomNumber(uint8_t flags, const uint8_t* uid, + uint8_t* rxBuf, uint16_t rxBufLen, + uint16_t* rcvLen); + +/*! + ***************************************************************************** + * \brief NFC-V Poller Read Message length + * + * Sends a Read Message Length message to retrieve the value of MB_LEN_Dyn + * + * \param[in] flags : Flags to be used: Sub-carrier; Data_rate; Option + * for NFC-Forum use: RFAL_NFCV_REQ_FLAG_DEFAULT + * \param[in] uid : UID of the device to be put to be read + * if not provided Select mode will be used + * \param[out] msgLen : Message Length + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_CRC : CRC error detected + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_TIMEOUT : Timeout error + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalST25xVPollerReadMessageLength(uint8_t flags, const uint8_t* uid, + uint8_t* msgLen); + +/*! + ***************************************************************************** + * \brief NFC-V Poller Fast Read Message length + * + * Sends a Fast Read Message Length message to retrieve the value of MB_LEN_Dyn + *using ST Fast mode. + * + * \param[in] flags : Flags to be used: Sub-carrier; Data_rate; Option + * for NFC-Forum use: RFAL_NFCV_REQ_FLAG_DEFAULT + * \param[in] uid : UID of the device to be put to be read + * if not provided Select mode will be used + * \param[out] msgLen : Message Length + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_CRC : CRC error detected + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_TIMEOUT : Timeout error + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalST25xVPollerFastReadMsgLength(uint8_t flags, const uint8_t* uid, + uint8_t* msgLen); + +/*! + ***************************************************************************** + * \brief NFC-V Poller Read Message + * + * Reads up to 256 bytes in the Mailbox from the location + * specified by MBpointer and sends back their value in the rxBuf response. + * First MailBox location is '00'. When Number of bytes is set to 00h + * and MBPointer is equals to 00h, the MB_LEN bytes of the full message + * are returned. Otherwise, Read Message command returns (Number of Bytes + 1) + *bytes (i.e. 01h returns 2 bytes, FFh returns 256 bytes). An error is reported + *if (Pointer + Nb of bytes + 1) is greater than the message length. RF Reading + *of the last byte of the mailbox message automatically clears b1 of MB_CTRL_Dyn + *HOST_PUT_MSG, and allows RF to put a new message. + * + * \param[in] flags : Flags to be used: Sub-carrier; Data_rate; Option + * for NFC-Forum use: RFAL_NFCV_REQ_FLAG_DEFAULT + * \param[in] uid : UID of the device to be put to be read + * if not provided Select mode will be used + * \param[in] mbPointer : MPpointer + * \param[in] numBytes : number of bytes + * \param[out] rxBuf : buffer to store response (also with RES_FLAGS) + * \param[in] rxBufLen : length of rxBuf + * \param[out] rcvLen : number of bytes received + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_CRC : CRC error detected + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_TIMEOUT : Timeout error + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalST25xVPollerReadMessage(uint8_t flags, const uint8_t* uid, + uint8_t mbPointer, uint8_t numBytes, + uint8_t* rxBuf, uint16_t rxBufLen, + uint16_t* rcvLen); + +/*! + ***************************************************************************** + * \brief NFC-V Poller Fast Read Message + * + * Reads up to 256 bytes in the Mailbox from the location + * specified by MBpointer and sends back their value in the rxBuf response using + *ST Fast mode. First MailBox location is '00'. When Number of bytes is set to + *00h and MBPointer is equals to 00h, the MB_LEN bytes of the full message are + *returned. Otherwise, Read Message command returns (Number of Bytes + 1) bytes + * (i.e. 01h returns 2 bytes, FFh returns 256 bytes). + * An error is reported if (Pointer + Nb of bytes + 1) is greater than the + *message length. RF Reading of the last byte of the mailbox message + *automatically clears b1 of MB_CTRL_Dyn HOST_PUT_MSG, and allows RF to put a + *new message. + * + * \param[in] flags : Flags to be used: Sub-carrier; Data_rate; Option + * for NFC-Forum use: RFAL_NFCV_REQ_FLAG_DEFAULT + * \param[in] uid : UID of the device to be put to be read + * if not provided Select mode will be used + * \param[in] mbPointer : MPpointer + * \param[in] numBytes : number of bytes + * \param[out] rxBuf : buffer to store response (also with RES_FLAGS) + * \param[in] rxBufLen : length of rxBuf + * \param[out] rcvLen : number of bytes received + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_CRC : CRC error detected + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_TIMEOUT : Timeout error + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalST25xVPollerFastReadMessage(uint8_t flags, const uint8_t* uid, + uint8_t mbPointer, uint8_t numBytes, + uint8_t* rxBuf, uint16_t rxBufLen, + uint16_t* rcvLen); + +/*! + ***************************************************************************** + * \brief NFC-V Poller Write Message + * + * Sends Write message Command + * + * On receiving the Write Message command, the ST25DVxxx puts the data contained + * in the request into the Mailbox buffer, update the MB_LEN_Dyn register, and + * set bit RF_PUT_MSG in MB_CTRL_Dyn register. It then reports if the write + *operation was successful in the response. The ST25DVxxx Mailbox contains up to + *256 data bytes which are filled from the first location '00'. MSGlength + *parameter of the command is the number of Data bytes minus 1 (00 for 1 byte of + *data, FFh for 256 bytes of data). Write Message could be executed only when + *Mailbox is accessible by RF. + * + * \param[in] flags : Flags to be used: Sub-carrier; Data_rate; Option + * for NFC-Forum use: RFAL_NFCV_REQ_FLAG_DEFAULT + * \param[in] uid : UID of the device to be put to be read + * if not provided Select mode will be used + * \param[in] msgLen : MSGLen number of Data bytes minus 1 + * \param[in] msgData : Message Data + * \param[out] txBuf : buffer to used to build the Write Message + *command \param[in] txBufLen : length of txBuf + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_CRC : CRC error detected + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_TIMEOUT : Timeout error + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalST25xVPollerWriteMessage(uint8_t flags, const uint8_t* uid, + uint8_t msgLen, const uint8_t* msgData, + uint8_t* txBuf, uint16_t txBufLen); + +/*! + ***************************************************************************** + * \brief NFC-V Poller Fast Write Message + * + * Sends Fast Write message Command using ST Fast mode + * + * On receiving the Write Message command, the ST25DVxxx puts the data contained + * in the request into the Mailbox buffer, update the MB_LEN_Dyn register, and + * set bit RF_PUT_MSG in MB_CTRL_Dyn register. It then reports if the write + *operation was successful in the response. The ST25DVxxx Mailbox contains up to + *256 data bytes which are filled from the first location '00'. MSGlength + *parameter of the command is the number of Data bytes minus 1 (00 for 1 byte of + *data, FFh for 256 bytes of data). Write Message could be executed only when + *Mailbox is accessible by RF. + * + * \param[in] flags : Flags to be used: Sub-carrier; Data_rate; Option + * for NFC-Forum use: RFAL_NFCV_REQ_FLAG_DEFAULT + * \param[in] uid : UID of the device to be put to be read + * if not provided Select mode will be used + * \param[in] msgLen : MSGLen number of Data bytes minus 1 + * \param[in] msgData : Message Data + * \param[out] txBuf : buffer to used to build the Write Message + *command \param[in] txBufLen : length of txBuf + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or incorrect mode + * \return RFAL_ERR_PARAM : Invalid parameters + * \return RFAL_ERR_IO : Generic internal error + * \return RFAL_ERR_CRC : CRC error detected + * \return RFAL_ERR_FRAMING : Framing error detected + * \return RFAL_ERR_PROTO : Protocol error detected + * \return RFAL_ERR_TIMEOUT : Timeout error + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalST25xVPollerFastWriteMessage(uint8_t flags, const uint8_t* uid, + uint8_t msgLen, + const uint8_t* msgData, + uint8_t* txBuf, uint16_t txBufLen); + +#endif /* RFAL_ST25xV_H */ + +/** + * @} + * + * @} + * + * @} + */ diff --git a/core/embed/io/nfc/rfal/include/rfal_t1t.h b/core/embed/io/nfc/rfal/include/rfal_t1t.h new file mode 100644 index 0000000000..bbb4256f62 --- /dev/null +++ b/core/embed/io/nfc/rfal/include/rfal_t1t.h @@ -0,0 +1,176 @@ + +/****************************************************************************** + * @attention + * + * COPYRIGHT 2016 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/* + * PROJECT: ST25R391x firmware + * Revision: + * LANGUAGE: ISO C99 + */ + +/*! \file rfal_t1t.h + * + * \author Gustavo Patricio + * + * \brief Provides NFC-A T1T convenience methods and definitions + * + * This module provides an interface to perform as a NFC-A Reader/Writer + * to handle a Type 1 Tag T1T (Topaz) + * + * + * \addtogroup RFAL + * @{ + * + * \addtogroup RFAL-AL + * \brief RFAL Abstraction Layer + * @{ + * + * \addtogroup T1T + * \brief RFAL T1T Module + * @{ + * + */ + +#ifndef RFAL_T1T_H +#define RFAL_T1T_H + +/* + ****************************************************************************** + * INCLUDES + ****************************************************************************** + */ +#include "rfal_platform.h" +#include "rfal_rf.h" +#include "rfal_utils.h" + +/* + ****************************************************************************** + * GLOBAL DEFINES + ****************************************************************************** + */ +#define RFAL_T1T_UID_LEN 4 /*!< T1T UID length of cascade level 1 only tag */ +#define RFAL_T1T_HR_LENGTH 2 /*!< T1T HR(Header ROM) length */ + +#define RFAL_T1T_HR0_NDEF_MASK \ + 0xF0 /*!< T1T HR0 NDEF capability mask T1T 1.2 2.2.2 */ +#define RFAL_T1T_HR0_NDEF_SUPPORT \ + 0x10 /*!< T1T HR0 NDEF capable value T1T 1.2 2.2.2 */ + +/*! NFC-A T1T (Topaz) command set */ +typedef enum { + RFAL_T1T_CMD_RID = 0x78, /*!< T1T Read UID */ + RFAL_T1T_CMD_RALL = 0x00, /*!< T1T Read All */ + RFAL_T1T_CMD_READ = 0x01, /*!< T1T Read */ + RFAL_T1T_CMD_WRITE_E = 0x53, /*!< T1T Write with erase (single byte) */ + RFAL_T1T_CMD_WRITE_NE = 0x1A /*!< T1T Write with no erase (single byte) */ +} rfalT1Tcmds; + +/* +****************************************************************************** +* GLOBAL TYPES +****************************************************************************** +*/ + +/*! NFC-A T1T (Topaz) RID_RES Digital 1.1 10.6.2 & Table 50 */ +typedef struct { + uint8_t hr0; /*!< T1T Header ROM: HR0 */ + uint8_t hr1; /*!< T1T Header ROM: HR1 */ + uint8_t uid[RFAL_T1T_UID_LEN]; /*!< T1T UID */ +} rfalT1TRidRes; + +/* +****************************************************************************** +* GLOBAL FUNCTION PROTOTYPES +****************************************************************************** +*/ + +/*! + ***************************************************************************** + * \brief Initialize NFC-A T1T Poller mode + * + * This methods configures RFAL RF layer to perform as a + * NFC-A T1T Poller/RW (Topaz) including all default timings + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or mode not set + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalT1TPollerInitialize(void); + +/*! + ***************************************************************************** + * \brief NFC-A T1T Poller RID + * + * This method reads the UID of a NFC-A T1T Listener device + * + * + * \param[out] ridRes : pointer to place the RID_RES + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or mode not set + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalT1TPollerRid(rfalT1TRidRes* ridRes); + +/*! + ***************************************************************************** + * \brief NFC-A T1T Poller RALL + * + * This method send a Read All command to a NFC-A T1T Listener device + * + * + * \param[in] uid : the UID of the device to read data + * \param[out] rxBuf : pointer to place the read data + * \param[in] rxBufLen : size of rxBuf + * \param[out] rxRcvdLen : actual received data + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or mode not set + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalT1TPollerRall(const uint8_t* uid, uint8_t* rxBuf, + uint16_t rxBufLen, uint16_t* rxRcvdLen); + +/*! + ***************************************************************************** + * \brief NFC-A T1T Poller Write + * + * This method writes the given data on the address of a NFC-A T1T Listener + *device + * + * + * \param[in] uid : the UID of the device to read data + * \param[in] address : address to write the data + * \param[in] data : the data to be written + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or mode not set + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalT1TPollerWrite(const uint8_t* uid, uint8_t address, + uint8_t data); + +#endif /* RFAL_T1T_H */ + +/** + * @} + * + * @} + * + * @} + */ diff --git a/core/embed/io/nfc/rfal/include/rfal_t2t.h b/core/embed/io/nfc/rfal/include/rfal_t2t.h new file mode 100644 index 0000000000..7dabe8f36e --- /dev/null +++ b/core/embed/io/nfc/rfal/include/rfal_t2t.h @@ -0,0 +1,146 @@ + +/****************************************************************************** + * @attention + * + * COPYRIGHT 2016 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/* + * PROJECT: ST25R391x firmware + * Revision: + * LANGUAGE: ISO C99 + */ + +/*! \file rfal_t2t.h + * + * \author Gustavo Patricio + * + * \brief Provides NFC-A T2T convenience methods and definitions + * + * This module provides an interface to perform as a NFC-A Reader/Writer + * to handle a Type 2 Tag T2T + * + * + * \addtogroup RFAL + * @{ + * + * \addtogroup RFAL-AL + * \brief RFAL Abstraction Layer + * @{ + * + * \addtogroup T2T + * \brief RFAL T2T Module + * @{ + * + */ + +#ifndef RFAL_T2T_H +#define RFAL_T2T_H + +/* + ****************************************************************************** + * INCLUDES + ****************************************************************************** + */ +#include "rfal_platform.h" +#include "rfal_rf.h" +#include "rfal_utils.h" + +/* + ****************************************************************************** + * GLOBAL DEFINES + ****************************************************************************** + */ + +#define RFAL_T2T_BLOCK_LEN 4U /*!< T2T block length */ +#define RFAL_T2T_READ_DATA_LEN \ + (4U * RFAL_T2T_BLOCK_LEN) /*!< T2T READ data length */ +#define RFAL_T2T_WRITE_DATA_LEN \ + RFAL_T2T_BLOCK_LEN /*!< T2T WRITE data length */ + +/* +****************************************************************************** +* GLOBAL TYPES +****************************************************************************** +*/ + +/* +****************************************************************************** +* GLOBAL FUNCTION PROTOTYPES +****************************************************************************** +*/ + +/*! + ***************************************************************************** + * \brief NFC-A T2T Poller Read + * + * This method sends a Read command to a NFC-A T2T Listener device + * + * + * \param[in] blockNum : Number of the block to read + * \param[out] rxBuf : pointer to place the read data + * \param[in] rxBufLen : size of rxBuf (RFAL_T2T_READ_DATA_LEN) + * \param[out] rcvLen : actual received data + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or mode not set + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_PROTO : Protocol error + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalT2TPollerRead(uint8_t blockNum, uint8_t* rxBuf, + uint16_t rxBufLen, uint16_t* rcvLen); + +/*! + ***************************************************************************** + * \brief NFC-A T2T Poller Write + * + * This method sends a Write command to a NFC-A T2T Listener device + * + * + * \param[in] blockNum : Number of the block to write + * \param[in] wrData : data to be written on the given block + * size must be of RFAL_T2T_WRITE_DATA_LEN + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or mode not set + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_PROTO : Protocol error + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalT2TPollerWrite(uint8_t blockNum, const uint8_t* wrData); + +/*! + ***************************************************************************** + * \brief NFC-A T2T Poller Sector Select + * + * This method sends a Sector Select commands to a NFC-A T2T Listener device + * + * \param[in] sectorNum : Sector Number + * + * \return RFAL_ERR_WRONG_STATE : RFAL not initialized or mode not set + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_PROTO : Protocol error + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalT2TPollerSectorSelect(uint8_t sectorNum); + +#endif /* RFAL_T2T_H */ + +/** + * @} + * + * @} + * + * @} + */ diff --git a/core/embed/io/nfc/rfal/include/rfal_t4t.h b/core/embed/io/nfc/rfal/include/rfal_t4t.h new file mode 100644 index 0000000000..f1b3c1764d --- /dev/null +++ b/core/embed/io/nfc/rfal/include/rfal_t4t.h @@ -0,0 +1,378 @@ + +/****************************************************************************** + * @attention + * + * COPYRIGHT 2016 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/* + * PROJECT: ST25R391x firmware + * Revision: + * LANGUAGE: ISO C99 + */ + +/*! \file rfal_t4t.h + * + * \author Gustavo Patricio + * + * \brief Provides convenience methods and definitions for T4T (ISO7816-4) + * + * This module provides an interface to exchange T4T APDUs according to + * NFC Forum T4T and ISO7816-4 + * + * This implementation was based on the following specs: + * - ISO/IEC 7816-4 3rd Edition 2013-04-15 + * - NFC Forum T4T Technical Specification 1.0 2017-08-28 + * + * \addtogroup RFAL + * @{ + * + * \addtogroup RFAL-AL + * \brief RFAL Abstraction Layer + * @{ + * + * \addtogroup T4T + * \brief RFAL T4T Module + * @{ + * + */ + +#ifndef RFAL_T4T_H +#define RFAL_T4T_H + +/* + ****************************************************************************** + * INCLUDES + ****************************************************************************** + */ +#include "rfal_isoDep.h" +#include "rfal_platform.h" +#include "rfal_rf.h" +#include "rfal_utils.h" + +/* + ****************************************************************************** + * GLOBAL DEFINES + ****************************************************************************** + */ + +#define RFAL_T4T_MAX_CAPDU_PROLOGUE_LEN \ + 4U /*!< Command-APDU prologue length (CLA INS P1 P2) */ +#define RFAL_T4T_LE_LEN \ + 1U /*!< Le Expected Response Length (short field coding) */ +#define RFAL_T4T_LC_LEN \ + 1U /*!< Lc Data field length (short field coding) */ +#define RFAL_T4T_MAX_RAPDU_SW1SW2_LEN \ + 2U /*!< SW1 SW2 length */ +#define RFAL_T4T_CLA \ + 0x00U /*!< Class byte (contains 00h because secure message are not used) */ + +#define RFAL_T4T_ISO7816_P1_SELECT_BY_DF_NAME \ + 0x04U /*!< P1 value for Select by name */ +#define RFAL_T4T_ISO7816_P1_SELECT_BY_FILEID \ + 0x00U /*!< P1 value for Select by file identifier */ +#define RFAL_T4T_ISO7816_P2_SELECT_FIRST_OR_ONLY_OCCURENCE \ + 0x00U /*!< b2b1 P2 value for First or only occurence */ +#define RFAL_T4T_ISO7816_P2_SELECT_RETURN_FCI_TEMPLATE \ + 0x00U /*!< b4b3 P2 value for Return FCI template */ +#define RFAL_T4T_ISO7816_P2_SELECT_NO_RESPONSE_DATA \ + 0x0CU /*!< b4b3 P2 value for No responce data */ + +#define RFAL_T4T_ISO7816_STATUS_COMPLETE \ + 0x9000U /*!< Command completed \ Normal processing - No further \ + qualification*/ + +/* +****************************************************************************** +* GLOBAL VARIABLES +****************************************************************************** +*/ + +/* +****************************************************************************** +* GLOBAL TYPES +****************************************************************************** +*/ +/*! NFC-A T4T Command-APDU structure */ +typedef struct { + uint8_t CLA; /*!< Class byte */ + uint8_t INS; /*!< Instruction byte */ + uint8_t P1; /*!< Parameter byte 1 */ + uint8_t P2; /*!< Parameter byte 2 */ + uint8_t Lc; /*!< Data field length */ + bool LcFlag; /*!< Lc flag (append Lc when true) */ + uint8_t Le; /*!< Expected Response Length */ + bool LeFlag; /*!< Le flag (append Le when true) */ + + rfalIsoDepApduBufFormat *cApduBuf; /*!< Command-APDU buffer (Tx) */ + uint16_t *cApduLen; /*!< Command-APDU Length */ +} rfalT4tCApduParam; + +/*! NFC-A T4T Response-APDU structure */ +typedef struct { + rfalIsoDepApduBufFormat *rApduBuf; /*!< Response-APDU buffer (Rx) */ + uint16_t rcvdLen; /*!< Full response length */ + uint16_t rApduBodyLen; /*!< Response body length */ + uint16_t statusWord; /*!< R-APDU Status Word SW1|SW2 */ +} rfalT4tRApduParam; + +/*! NFC-A T4T command set T4T 1.0 & ISO7816-4 2013 Table 4 */ +typedef enum { + RFAL_T4T_INS_SELECT = 0xA4U, /*!< T4T Select */ + RFAL_T4T_INS_READBINARY = 0xB0U, /*!< T4T ReadBinary */ + RFAL_T4T_INS_UPDATEBINARY = 0xD6U, /*!< T4T UpdateBinay */ + RFAL_T4T_INS_READBINARY_ODO = 0xB1U, /*!< T4T ReadBinary using ODO */ + RFAL_T4T_INS_UPDATEBINARY_ODO = 0xD7U /*!< T4T UpdateBinay using ODO */ +} rfalT4tCmds; + +/* +****************************************************************************** +* GLOBAL FUNCTION PROTOTYPES +****************************************************************************** +*/ + +/*! + ***************************************************************************** + * \brief T4T Compose APDU + * + * This method computes a C-APDU according to NFC Forum T4T and ISO7816-4. + * + * If C-APDU contains data to be sent, it must be placed inside the buffer + * rfalT4tTxRxApduParam.txRx.cApduBuf.apdu and signaled by Lc + * + * To transceive the formed APDU the ISO-DEP layer shall be used + * + * \see rfalIsoDepStartApduTransceive() + * \see rfalIsoDepGetApduTransceiveStatus() + * \see rfalT4TPollerParseRAPDU() + * + * \warning The ISO-DEP module is used to perform the tranceive. Usually + * activation has been done via ISO-DEP activatiavtion. If not + * please call rfalIsoDepInitialize() before. + * + * \param[in,out] apduParam : APDU parameters + * apduParam.cApduLen will contain the APDU length + * + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_PROTO : Protocol error + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalT4TPollerComposeCAPDU(const rfalT4tCApduParam *apduParam); + +/*! + ***************************************************************************** + * \brief T4T Parse R-APDU + * + * This method parses a R-APDU according to NFC Forum T4T and ISO7816-4. + * It will extract the data length and check if the Satus word is expected. + * + * \param[in,out] apduParam : APDU parameters + * apduParam.rApduBodyLen will contain the data + *length apduParam.statusWord will contain the SW1 and SW2 + * + * \return RFAL_ERR_REQUEST : Status word (SW1 SW2) different from 9000 + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_PROTO : Protocol error + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalT4TPollerParseRAPDU(rfalT4tRApduParam *apduParam); + +/*! + ***************************************************************************** + * \brief T4T Compose Select Application APDU + * + * This method computes a Select Application APDU according to NFC Forum T4T + * + * To transceive the formed APDU the ISO-DEP layer shall be used + * + * \see rfalIsoDepStartApduTransceive() + * \see rfalIsoDepGetApduTransceiveStatus() + * + * \param[out] cApduBuf : buffer where the C-APDU will be placed + * \param[in] aid : Application ID to be used + * \param[in] aidLen : Application ID length + * \param[out] cApduLen : Composed C-APDU length + * + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_PROTO : Protocol error + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalT4TPollerComposeSelectAppl(rfalIsoDepApduBufFormat *cApduBuf, + const uint8_t *aid, uint8_t aidLen, + uint16_t *cApduLen); + +/*! + ***************************************************************************** + * \brief T4T Compose Select File APDU + * + * This method computes a Select File APDU according to NFC Forum T4T + * + * To transceive the formed APDU the ISO-DEP layer shall be used + * + * \see rfalIsoDepStartApduTransceive() + * \see rfalIsoDepGetApduTransceiveStatus() + * + * \param[out] cApduBuf : buffer where the C-APDU will be placed + * \param[in] fid : File ID to be used + * \param[in] fidLen : File ID length + * \param[out] cApduLen : Composed C-APDU length + * + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_PROTO : Protocol error + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalT4TPollerComposeSelectFile(rfalIsoDepApduBufFormat *cApduBuf, + const uint8_t *fid, uint8_t fidLen, + uint16_t *cApduLen); + +/*! + ***************************************************************************** + * \brief T4T Compose Select File APDU for Mapping Version 1 + * + * This method computes a Select File APDU according to NFC Forum T4TOP_v1.0 + * + * To transceive the formed APDU the ISO-DEP layer shall be used + * + * \see rfalIsoDepStartApduTransceive() + * \see rfalIsoDepGetApduTransceiveStatus() + * + * \param[out] cApduBuf : buffer where the C-APDU will be placed + * \param[in] fid : File ID to be used + * \param[in] fidLen : File ID length + * \param[out] cApduLen : Composed C-APDU length + * + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_PROTO : Protocol error + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalT4TPollerComposeSelectFileV1Mapping( + rfalIsoDepApduBufFormat *cApduBuf, const uint8_t *fid, uint8_t fidLen, + uint16_t *cApduLen); + +/*! + ***************************************************************************** + * \brief T4T Compose Read Data APDU + * + * This method computes a Read Data APDU according to NFC Forum T4T + * + * To transceive the formed APDU the ISO-DEP layer shall be used + * + * \see rfalIsoDepStartApduTransceive() + * \see rfalIsoDepGetApduTransceiveStatus() + * + * \param[out] cApduBuf : buffer where the C-APDU will be placed + * \param[in] offset : File offset + * \param[in] expLen : Expected length (Le) + * \param[out] cApduLen : Composed C-APDU length + * + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_PROTO : Protocol error + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalT4TPollerComposeReadData(rfalIsoDepApduBufFormat *cApduBuf, + uint16_t offset, uint8_t expLen, + uint16_t *cApduLen); + +/*! + ***************************************************************************** + * \brief T4T Compose Read Data ODO APDU + * + * This method computes a Read Data ODO APDU according to NFC Forum T4T + * + * To transceive the formed APDU the ISO-DEP layer shall be used + * + * \see rfalIsoDepStartApduTransceive() + * \see rfalIsoDepGetApduTransceiveStatus() + * + * \param[out] cApduBuf : buffer where the C-APDU will be placed + * \param[in] offset : File offset + * \param[in] expLen : Expected length (Le) + * \param[out] cApduLen : Composed C-APDU length + * + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_PROTO : Protocol error + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalT4TPollerComposeReadDataODO(rfalIsoDepApduBufFormat *cApduBuf, + uint32_t offset, uint8_t expLen, + uint16_t *cApduLen); + +/*! + ***************************************************************************** + * \brief T4T Compose Write Data APDU + * + * This method computes a Write Data APDU according to NFC Forum T4T + * + * To transceive the formed APDU the ISO-DEP layer shall be used + * + * \see rfalIsoDepStartApduTransceive() + * \see rfalIsoDepGetApduTransceiveStatus() + * + * \param[out] cApduBuf : buffer where the C-APDU will be placed + * \param[in] offset : File offset + * \param[in] data : Data to be written + * \param[in] dataLen : Data length to be written (Lc) + * \param[out] cApduLen : Composed C-APDU length + * + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_PROTO : Protocol error + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalT4TPollerComposeWriteData(rfalIsoDepApduBufFormat *cApduBuf, + uint16_t offset, const uint8_t *data, + uint8_t dataLen, uint16_t *cApduLen); + +/*! + ***************************************************************************** + * \brief T4T Compose Write Data ODO APDU + * + * This method computes a Write Data ODO sAPDU according to NFC Forum T4T + * + * To transceive the formed APDU the ISO-DEP layer shall be used + * + * \see rfalIsoDepStartApduTransceive() + * \see rfalIsoDepGetApduTransceiveStatus() + * + * \param[out] cApduBuf : buffer where the C-APDU will be placed + * \param[in] offset : File offset + * \param[in] data : Data to be written + * \param[in] dataLen : Data length to be written (Lc) + * \param[out] cApduLen : Composed C-APDU length + * + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_PROTO : Protocol error + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode rfalT4TPollerComposeWriteDataODO(rfalIsoDepApduBufFormat *cApduBuf, + uint32_t offset, + const uint8_t *data, + uint8_t dataLen, + uint16_t *cApduLen); + +#endif /* RFAL_T4T_H */ + +/** + * @} + * + * @} + * + * @} + */ diff --git a/core/embed/io/nfc/rfal/include/rfal_utils.h b/core/embed/io/nfc/rfal/include/rfal_utils.h new file mode 100644 index 0000000000..ca49f00ef3 --- /dev/null +++ b/core/embed/io/nfc/rfal/include/rfal_utils.h @@ -0,0 +1,218 @@ + +/****************************************************************************** + * @attention + * + * COPYRIGHT 2016 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/* + * PROJECT: ST25R + * Revision: + * LANGUAGE: ISO C99 + */ + +/*! \file rfal_utils.h + * + * \author Gustavo Patricio + * + * \brief RF Abstraction Layer (RFAL) Utils + * + * \addtogroup RFAL + * @{ + * + */ + +#ifndef RFAL_UTILS_H +#define RFAL_UTILS_H + +/* +****************************************************************************** +* INCLUDES +****************************************************************************** +*/ +#include +#include + +/* +****************************************************************************** +* GLOBAL DATA TYPES +****************************************************************************** +*/ + +typedef uint16_t ReturnCode; /*!< Standard Return Code type from function. */ + +/* +****************************************************************************** +* DEFINES +****************************************************************************** +*/ + +/* + * Error codes to be used within the application. + * They are represented by an uint8_t + */ + +#define RFAL_ERR_NONE ((ReturnCode)0U) /*!< no error occurred */ +#define RFAL_ERR_NOMEM \ + ((ReturnCode)1U) /*!< not enough memory to perform the requested operation \ + */ +#define RFAL_ERR_BUSY ((ReturnCode)2U) /*!< device or resource busy */ +#define RFAL_ERR_IO ((ReturnCode)3U) /*!< generic IO error */ +#define RFAL_ERR_TIMEOUT ((ReturnCode)4U) /*!< error due to timeout */ +#define RFAL_ERR_REQUEST \ + ((ReturnCode)5U) /*!< invalid request or requested function can't be \ + executed at the moment */ +#define RFAL_ERR_NOMSG ((ReturnCode)6U) /*!< No message of desired type */ +#define RFAL_ERR_PARAM ((ReturnCode)7U) /*!< Parameter error */ +#define RFAL_ERR_SYSTEM ((ReturnCode)8U) /*!< System error */ +#define RFAL_ERR_FRAMING ((ReturnCode)9U) /*!< Framing error */ +#define RFAL_ERR_OVERRUN \ + ((ReturnCode)10U) /*!< lost one or more received bytes */ +#define RFAL_ERR_PROTO ((ReturnCode)11U) /*!< protocol error */ +#define RFAL_ERR_INTERNAL ((ReturnCode)12U) /*!< Internal Error */ +#define RFAL_ERR_AGAIN ((ReturnCode)13U) /*!< Call again */ +#define RFAL_ERR_MEM_CORRUPT ((ReturnCode)14U) /*!< memory corruption */ +#define RFAL_ERR_NOT_IMPLEMENTED ((ReturnCode)15U) /*!< not implemented */ +#define RFAL_ERR_PC_CORRUPT \ + ((ReturnCode)16U) /*!< Program Counter has been manipulated or spike/noise \ + trigger illegal operation */ +#define RFAL_ERR_SEND ((ReturnCode)17U) /*!< error sending*/ +#define RFAL_ERR_IGNORE \ + ((ReturnCode)18U) /*!< indicates error detected but to be ignored */ +#define RFAL_ERR_SEMANTIC \ + ((ReturnCode)19U) /*!< indicates error in state machine (unexpected cmd) */ +#define RFAL_ERR_SYNTAX \ + ((ReturnCode)20U) /*!< indicates error in state machine (unknown cmd) */ +#define RFAL_ERR_CRC ((ReturnCode)21U) /*!< crc error */ +#define RFAL_ERR_NOTFOUND ((ReturnCode)22U) /*!< transponder not found */ +#define RFAL_ERR_NOTUNIQUE \ + ((ReturnCode)23U) /*!< transponder not unique - more than one transponder in \ + field */ +#define RFAL_ERR_NOTSUPP \ + ((ReturnCode)24U) /*!< requested operation not supported */ +#define RFAL_ERR_WRITE ((ReturnCode)25U) /*!< write error */ +#define RFAL_ERR_FIFO ((ReturnCode)26U) /*!< fifo over or underflow error */ +#define RFAL_ERR_PAR ((ReturnCode)27U) /*!< parity error */ +#define RFAL_ERR_DONE ((ReturnCode)28U) /*!< transfer has already finished */ +#define RFAL_ERR_RF_COLLISION \ + ((ReturnCode)29U) /*!< collision error (Bit Collision or during RF Collision \ + avoidance ) */ +#define RFAL_ERR_HW_OVERRUN \ + ((ReturnCode)30U) /*!< lost one or more received bytes */ +#define RFAL_ERR_RELEASE_REQ \ + ((ReturnCode)31U) /*!< device requested release \ + */ +#define RFAL_ERR_SLEEP_REQ ((ReturnCode)32U) /*!< device requested sleep */ +#define RFAL_ERR_WRONG_STATE \ + ((ReturnCode)33U) /*!< incorrent state for requested operation */ +#define RFAL_ERR_MAX_RERUNS \ + ((ReturnCode)34U) /*!< blocking procedure reached maximum runs */ +#define RFAL_ERR_DISABLED \ + ((ReturnCode)35U) /*!< operation aborted due to disabled configuration */ +#define RFAL_ERR_HW_MISMATCH \ + ((ReturnCode)36U) /*!< expected hw do not match \ + */ +#define RFAL_ERR_LINK_LOSS \ + ((ReturnCode)37U) /*!< Other device's field didn't behave as expected: \ + turned off by Initiator in Passive mode, or AP2P did \ + not turn on field */ +#define RFAL_ERR_INVALID_HANDLE \ + ((ReturnCode)38U) /*!< invalid or not initialized device handle */ + +#define RFAL_ERR_INCOMPLETE_BYTE \ + ((ReturnCode)40U) /*!< Incomplete byte rcvd */ +#define RFAL_ERR_INCOMPLETE_BYTE_01 \ + ((ReturnCode)41U) /*!< Incomplete byte rcvd - 1 bit */ +#define RFAL_ERR_INCOMPLETE_BYTE_02 \ + ((ReturnCode)42U) /*!< Incomplete byte rcvd - 2 bit */ +#define RFAL_ERR_INCOMPLETE_BYTE_03 \ + ((ReturnCode)43U) /*!< Incomplete byte rcvd - 3 bit */ +#define RFAL_ERR_INCOMPLETE_BYTE_04 \ + ((ReturnCode)44U) /*!< Incomplete byte rcvd - 4 bit */ +#define RFAL_ERR_INCOMPLETE_BYTE_05 \ + ((ReturnCode)45U) /*!< Incomplete byte rcvd - 5 bit */ +#define RFAL_ERR_INCOMPLETE_BYTE_06 \ + ((ReturnCode)46U) /*!< Incomplete byte rcvd - 6 bit */ +#define RFAL_ERR_INCOMPLETE_BYTE_07 \ + ((ReturnCode)47U) /*!< Incomplete byte rcvd - 7 bit */ + +/* +****************************************************************************** +* GLOBAL MACROS +****************************************************************************** +*/ +/*! Common code to exit a function with the error if function f return error */ +#define RFAL_EXIT_ON_ERR(r, f) \ + (r) = (f); \ + if (RFAL_ERR_NONE != (r)) { \ + return (r); \ + } + +/*! Common code to exit a function if process/function f has not concluded */ +#define RFAL_EXIT_ON_BUSY(r, f) \ + (r) = (f); \ + if (RFAL_ERR_BUSY == (r)) { \ + return (r); \ + } + +#define RFAL_SIZEOF_ARRAY(a) \ + (sizeof(a) / sizeof((a)[0])) /*!< Compute the size of an array */ +#define RFAL_MAX(a, b) \ + (((a) > (b)) ? (a) : (b)) /*!< Return the maximum of the 2 values */ +#define RFAL_MIN(a, b) \ + (((a) < (b)) ? (a) : (b)) /*!< Return the minimum of the 2 values */ +#define RFAL_GETU16(a) \ + (((uint16_t)(a)[0] << 8) | \ + (uint16_t)(a)[1]) /*!< Cast two Big Endian 8-bits byte array to 16-bits \ + unsigned */ +#define RFAL_GETU32(a) \ + (((uint32_t)(a)[0] << 24) | ((uint32_t)(a)[1] << 16) | \ + ((uint32_t)(a)[2] << 8) | \ + ((uint32_t)(a)[3])) /*!< Cast four Big Endian 8-bit byte array to 32-bit \ + unsigned */ + +#ifdef __CSMC__ +/* STM8 COSMIC */ +#define RFAL_MEMMOVE(s1, s2, n) \ + memmove(s1,s2,n) /* PRQA S 5003 # CERT C 9 - string.h from Cosmic only provides functions with low qualified parameters */ /*!< map memmove to string library code */ +static inline void *RFAL_MEMCPY(void *s1, const void *s2, uint32_t n) { + return memcpy(s1, s2, (uint16_t)n); +} /* PRQA S 0431 # MISRA 1.1 - string.h from Cosmic only provides functions + with low qualified parameters */ +#define RFAL_MEMSET(s1, c, n) \ + memset(s1, (char)(c), n) /*!< map memset to string library code */ +static inline int32_t RFAL_BYTECMP(void *s1, const void *s2, uint32_t n) { + return (int32_t)memcmp(s1, s2, (uint16_t)n); +} /* PRQA S 0431 # MISRA 1.1 - string.h from Cosmic only provides functions + with low qualified parameters */ + +#else /* __CSMC__ */ + +#define RFAL_MEMMOVE memmove /*!< map memmove to string library code */ +#define RFAL_MEMCPY memcpy /*!< map memcpy to string library code */ +#define RFAL_MEMSET memset /*!< map memset to string library code */ +#define RFAL_BYTECMP memcmp /*!< map bytecmp to string library code */ +#endif /* __CSMC__ */ + +#define RFAL_NO_WARNING(v) \ + ((void)(v)) /*!< Macro to suppress compiler warning */ + +#ifndef NULL +#define NULL (void *)0 /*!< represents a NULL pointer */ +#endif /* !NULL */ + +#endif /* RFAL_UTILS_H */ + +/** + * @} + * + */ diff --git a/core/embed/io/nfc/rfal/source/rfal_analogConfig.c b/core/embed/io/nfc/rfal/source/rfal_analogConfig.c new file mode 100644 index 0000000000..870537c079 --- /dev/null +++ b/core/embed/io/nfc/rfal/source/rfal_analogConfig.c @@ -0,0 +1,507 @@ + +/****************************************************************************** + * @attention + * + * COPYRIGHT 2016 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/* + * PROJECT: ST25R391x firmware + * Revision: + * LANGUAGE: ISO C99 + */ + +/*! \file rfal_analogConfig.c + * + * \author bkam + * + * \brief Funcitons to manage and set analog settings. + * + */ + +/* + ****************************************************************************** + * INCLUDES + ****************************************************************************** + */ +#include "rfal_analogConfig.h" +#include "rfal_chip.h" +#include "rfal_platform.h" +#include "rfal_utils.h" + +/* Check whether the Default Analog settings are to be used or custom ones */ +#ifdef RFAL_ANALOG_CONFIG_CUSTOM +extern const uint8_t rfalAnalogConfigCustomSettings[]; +extern const uint16_t rfalAnalogConfigCustomSettingsLength; +#else +#include "rfal_analogConfigTbl.h" +#endif + +/* + ****************************************************************************** + * DEFINES + ****************************************************************************** + */ + +#define RFAL_TEST_REG 0x0080U /*!< Test Register indicator */ + +/* + ****************************************************************************** + * MACROS + ****************************************************************************** + */ + +/* + ****************************************************************************** + * LOCAL DATA TYPES + ****************************************************************************** + */ + +#if RFAL_FEATURE_DYNAMIC_ANALOG_CONFIG +static uint8_t + gRfalAnalogConfig[RFAL_ANALOG_CONFIG_TBL_SIZE]; /*!< Analog Configuration + Settings List */ +#endif /* RFAL_FEATURE_DYNAMIC_ANALOG_CONFIG */ + +/*! Struct for Analog Config Look Up Table Update */ +typedef struct { + const uint8_t *currentAnalogConfigTbl; /*!< Reference to start of current + Analog Configuration */ + uint16_t configTblSize; /*!< Total size of Analog Configuration */ + bool ready; /*!< Indicate if Look Up Table is complete and ready for use */ +} rfalAnalogConfigMgmt; + +static rfalAnalogConfigMgmt + gRfalAnalogConfigMgmt; /*!< Analog Configuration LUT management */ + +/* + ****************************************************************************** + * LOCAL TABLES + ****************************************************************************** + */ + +/* + ****************************************************************************** + * LOCAL FUNCTION PROTOTYPES + ****************************************************************************** + */ +static rfalAnalogConfigNum rfalAnalogConfigSearch(rfalAnalogConfigId configId, + uint16_t *configOffset); + +#if RFAL_FEATURE_DYNAMIC_ANALOG_CONFIG +static void rfalAnalogConfigPtrUpdate(const uint8_t *analogConfigTbl); +#endif /* RFAL_FEATURE_DYNAMIC_ANALOG_CONFIG */ + +/* + ****************************************************************************** + * GLOBAL VARIABLE DEFINITIONS + ****************************************************************************** + */ + +/* + ****************************************************************************** + * GLOBAL FUNCTIONS + ****************************************************************************** + */ + +/*******************************************************************************/ +void rfalAnalogConfigInitialize(void) { + /* Use default Analog configuration settings in Flash by default. */ + +/* Check whether the Default Analog settings are to be used or custom ones */ +#ifdef RFAL_ANALOG_CONFIG_CUSTOM + gRfalAnalogConfigMgmt.currentAnalogConfigTbl = rfalAnalogConfigCustomSettings; + gRfalAnalogConfigMgmt.configTblSize = rfalAnalogConfigCustomSettingsLength; +#else + gRfalAnalogConfigMgmt.currentAnalogConfigTbl = + rfalAnalogConfigDefaultSettings; + gRfalAnalogConfigMgmt.configTblSize = sizeof(rfalAnalogConfigDefaultSettings); +#endif + + gRfalAnalogConfigMgmt.ready = true; +} + +/*******************************************************************************/ +bool rfalAnalogConfigIsReady(void) { return gRfalAnalogConfigMgmt.ready; } + +/*******************************************************************************/ +ReturnCode rfalAnalogConfigListWriteRaw(const uint8_t *configTbl, + uint16_t configTblSize) { +#if RFAL_FEATURE_DYNAMIC_ANALOG_CONFIG + + /* Check if the Configuration Table exceed the Table size */ + if (configTblSize >= RFAL_ANALOG_CONFIG_TBL_SIZE) { + return RFAL_ERR_NOMEM; + } + + /* Check for invalid parameters */ + if ((configTbl == NULL) || (configTblSize == 0U)) { + return RFAL_ERR_PARAM; + } + + /* NOTE: On this API (rfalAnalogConfigListWriteRaw) the current AC Table is + * not reset upon error, as on rfalAnalogConfigListWrite. */ + /* On rfalAnalogConfigListWrite the AC table is written in mutiple + * chunks of data which may lead to an invalid|incomplte AC */ + /* table if an error arises, where here the whole AC Table is written + * all together. */ + + /* NOTE: Function does not check for the validity of the Table contents (conf + * IDs, conf sets, register address) */ + RFAL_MEMCPY(gRfalAnalogConfig, configTbl, configTblSize); + + /* Update the total size of configuration settings */ + gRfalAnalogConfigMgmt.configTblSize = configTblSize; + + rfalAnalogConfigPtrUpdate(gRfalAnalogConfig); + return RFAL_ERR_NONE; + +#else + + // If Analog Configuration Update is to be disabled + RFAL_NO_WARNING(configTbl); + RFAL_NO_WARNING(configTblSize); + return RFAL_ERR_REQUEST; + +#endif /* RFAL_FEATURE_DYNAMIC_ANALOG_CONFIG */ +} + +/*******************************************************************************/ +ReturnCode rfalAnalogConfigListWrite(uint8_t more, + const rfalAnalogConfig *config) { +#if RFAL_FEATURE_DYNAMIC_ANALOG_CONFIG + + rfalAnalogConfigId configId; + rfalAnalogConfigNum numConfig; + uint8_t configSize; + + if (true == gRfalAnalogConfigMgmt + .ready) { /* First Update to the Configuration list. */ + gRfalAnalogConfigMgmt.ready = false; // invalidate the config List + gRfalAnalogConfigMgmt.configTblSize = 0; // Clear the config List + } + + configId = RFAL_GETU16(config->id); + + /* Check validity of the Configuration ID. */ + /* NOTE: Direction DPO uses 2msb of the Technology field as level indicator */ + if (((RFAL_ANALOG_CONFIG_TECH_RFU <= + RFAL_ANALOG_CONFIG_ID_GET_TECH(configId)) && + ((RFAL_ANALOG_CONFIG_ID_GET_DIRECTION(configId) != + RFAL_ANALOG_CONFIG_DPO) && + (RFAL_ANALOG_CONFIG_ID_GET_DIRECTION(configId) != + RFAL_ANALOG_CONFIG_DLMA))) || + ((RFAL_ANALOG_CONFIG_BITRATE_6780 < + RFAL_ANALOG_CONFIG_ID_GET_BITRATE(configId)) && + (RFAL_ANALOG_CONFIG_BITRATE_211p88 > + RFAL_ANALOG_CONFIG_ID_GET_BITRATE(configId))) || + (RFAL_ANALOG_CONFIG_BITRATE_1p6 < + RFAL_ANALOG_CONFIG_ID_GET_BITRATE(configId))) { + rfalAnalogConfigInitialize(); /* Revert to default Analog Configuration */ + return RFAL_ERR_PARAM; + } + + numConfig = config->num; + configSize = + (uint8_t)(sizeof(rfalAnalogConfigId) + sizeof(rfalAnalogConfigNum) + + (numConfig * sizeof(rfalAnalogConfigRegAddrMaskVal))); + + /* Check if the Configuration Set exceed the Table size. */ + if (RFAL_ANALOG_CONFIG_TBL_SIZE <= + (gRfalAnalogConfigMgmt.configTblSize + configSize)) { + rfalAnalogConfigInitialize(); /* Revert to default Analog Configuration */ + return RFAL_ERR_NOMEM; + } + + /* NOTE: Function does not check for the validity of the Register Address. */ + RFAL_MEMCPY(&gRfalAnalogConfig[gRfalAnalogConfigMgmt.configTblSize], + (const uint8_t *)config, configSize); + + /* Increment the total size of configuration settings. */ + gRfalAnalogConfigMgmt.configTblSize += configSize; + + /* Check if it is the last Analog Configuration to load. */ + if (RFAL_ANALOG_CONFIG_UPDATE_LAST == + more) { /* Update the Analog Configuration to the new settings. */ + rfalAnalogConfigPtrUpdate(gRfalAnalogConfig); + } + + return RFAL_ERR_NONE; + +#else + + // If Analog Configuration Update is to be disabled + RFAL_NO_WARNING(config); + RFAL_NO_WARNING(more); + return RFAL_ERR_DISABLED; + +#endif /* RFAL_FEATURE_DYNAMIC_ANALOG_CONFIG */ +} + +/*******************************************************************************/ +ReturnCode rfalAnalogConfigListReadRaw(uint8_t *tblBuf, uint16_t tblBufLen, + uint16_t *configTblSize) { + /* Check if the the current table will fit into the given buffer */ + if (tblBufLen < gRfalAnalogConfigMgmt.configTblSize) { + return RFAL_ERR_NOMEM; + } + + /* Check for invalid parameters */ + if ((configTblSize == NULL) || (tblBuf == NULL)) { + return RFAL_ERR_PARAM; + } + + /* Copy the whole Table to the given buffer */ + if (gRfalAnalogConfigMgmt.configTblSize > 0U) /* MISRA 21.18 */ + { + RFAL_MEMCPY(tblBuf, gRfalAnalogConfigMgmt.currentAnalogConfigTbl, + gRfalAnalogConfigMgmt.configTblSize); + } + *configTblSize = gRfalAnalogConfigMgmt.configTblSize; + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalAnalogConfigListRead(rfalAnalogConfigOffset *configOffset, + uint8_t *more, rfalAnalogConfig *config, + rfalAnalogConfigNum numConfig) { + uint16_t configSize; + const rfalAnalogConfigOffset offset = *configOffset; + rfalAnalogConfigNum numConfigSet; + + /* Check if the number of register-mask-value settings for the respective + * Configuration ID will fit into the buffer passed in. */ + if (gRfalAnalogConfigMgmt + .currentAnalogConfigTbl[offset + sizeof(rfalAnalogConfigId)] > + numConfig) { + return RFAL_ERR_NOMEM; + } + + /* Get the number of Configuration set */ + numConfigSet = + gRfalAnalogConfigMgmt + .currentAnalogConfigTbl[offset + sizeof(rfalAnalogConfigId)]; + + /* Pass Configuration Register-Mask-Value sets */ + configSize = + (sizeof(rfalAnalogConfigId) + sizeof(rfalAnalogConfigNum) + + (uint16_t)(numConfigSet * sizeof(rfalAnalogConfigRegAddrMaskVal))); + RFAL_MEMCPY((uint8_t *)config, + &gRfalAnalogConfigMgmt.currentAnalogConfigTbl[offset], + configSize); + *configOffset = offset + configSize; + + /* Check if it is the last Analog Configuration in the Table.*/ + *more = (uint8_t)((*configOffset >= gRfalAnalogConfigMgmt.configTblSize) + ? RFAL_ANALOG_CONFIG_UPDATE_LAST + : RFAL_ANALOG_CONFIG_UPDATE_MORE); + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalSetAnalogConfig(rfalAnalogConfigId configId) { + rfalAnalogConfigOffset configOffset = 0; + rfalAnalogConfigNum numConfigSet; + const rfalAnalogConfigRegAddrMaskVal *configTbl; + ReturnCode retCode = RFAL_ERR_NONE; + rfalAnalogConfigNum i; + + if (true != gRfalAnalogConfigMgmt.ready) { + return RFAL_ERR_REQUEST; + } + + /* Search LUT for the specific Configuration ID */ + while (true) { + numConfigSet = rfalAnalogConfigSearch(configId, &configOffset); + if (RFAL_ANALOG_CONFIG_LUT_NOT_FOUND == numConfigSet) { + break; + } + + configTbl = + (rfalAnalogConfigRegAddrMaskVal *)((uintptr_t)gRfalAnalogConfigMgmt + .currentAnalogConfigTbl + + (uint32_t)configOffset); + /* Increment the offset to the next index to search from */ + configOffset += + (uint16_t)(numConfigSet * sizeof(rfalAnalogConfigRegAddrMaskVal)); + + if ((gRfalAnalogConfigMgmt.configTblSize + 1U) < + configOffset) { /* Error check make sure that the we do not access + outside the configuration Table Size */ + return RFAL_ERR_NOMEM; + } + + for (i = 0; i < numConfigSet; i++) { + if ((RFAL_GETU16(configTbl[i].addr) & RFAL_TEST_REG) != 0U) { + RFAL_EXIT_ON_ERR(retCode, + rfalChipChangeTestRegBits( + (RFAL_GETU16(configTbl[i].addr) & ~RFAL_TEST_REG), + configTbl[i].mask, configTbl[i].val)); + } else { + RFAL_EXIT_ON_ERR(retCode, rfalChipChangeRegBits( + RFAL_GETU16(configTbl[i].addr), + configTbl[i].mask, configTbl[i].val)); + } + } + + } /* while(found Analog Config Id) */ + + return retCode; +} + +/*******************************************************************************/ +uint16_t rfalAnalogConfigGenModeID(rfalMode md, rfalBitRate br, uint16_t dir) { + uint16_t id; + + /* Assign Poll/Listen Mode */ + id = ((md >= RFAL_MODE_LISTEN_NFCA) ? RFAL_ANALOG_CONFIG_LISTEN + : RFAL_ANALOG_CONFIG_POLL); + + /* Assign Technology */ + switch (md) { + case RFAL_MODE_POLL_NFCA: + case RFAL_MODE_POLL_NFCA_T1T: + case RFAL_MODE_LISTEN_NFCA: + id |= RFAL_ANALOG_CONFIG_TECH_NFCA; + break; + + case RFAL_MODE_POLL_NFCB: + case RFAL_MODE_POLL_B_PRIME: + case RFAL_MODE_POLL_B_CTS: + case RFAL_MODE_LISTEN_NFCB: + id |= RFAL_ANALOG_CONFIG_TECH_NFCB; + break; + + case RFAL_MODE_POLL_NFCF: + case RFAL_MODE_LISTEN_NFCF: + id |= RFAL_ANALOG_CONFIG_TECH_NFCF; + break; + + case RFAL_MODE_POLL_NFCV: + case RFAL_MODE_POLL_PICOPASS: + id |= RFAL_ANALOG_CONFIG_TECH_NFCV; + break; + + case RFAL_MODE_POLL_ACTIVE_P2P: + case RFAL_MODE_LISTEN_ACTIVE_P2P: + id |= RFAL_ANALOG_CONFIG_TECH_AP2P; + break; + + default: + id = RFAL_ANALOG_CONFIG_TECH_CHIP; + break; + } + + /* Assign Bitrate */ + id |= (((((uint16_t)(br) >= (uint16_t)RFAL_BR_52p97) ? (uint16_t)(br) + : ((uint16_t)(br) + 1U)) + << RFAL_ANALOG_CONFIG_BITRATE_SHIFT) & + RFAL_ANALOG_CONFIG_BITRATE_MASK); + + /* Assign Direction */ + id |= ((dir << RFAL_ANALOG_CONFIG_DIRECTION_SHIFT) & + RFAL_ANALOG_CONFIG_DIRECTION_MASK); + + return id; +} + +/* + ****************************************************************************** + * LOCAL FUNCTIONS + ****************************************************************************** + */ + +/*! + ***************************************************************************** + * \brief Update the link to Analog Configuration LUT + * + * Update the link to the Analog Configuration LUT for the subsequent search + * of Analog Settings. + * + * \param[in] analogConfigTbl: reference to the start of the new Analog + *Configuration Table + * + ***************************************************************************** + */ +#if RFAL_FEATURE_DYNAMIC_ANALOG_CONFIG +static void rfalAnalogConfigPtrUpdate(const uint8_t *analogConfigTbl) { + gRfalAnalogConfigMgmt.currentAnalogConfigTbl = analogConfigTbl; + gRfalAnalogConfigMgmt.ready = true; +} +#endif /* RFAL_FEATURE_DYNAMIC_ANALOG_CONFIG */ + +/*! + ***************************************************************************** + * \brief Search the Analog Configuration LUT for a specific Configuration ID. + * + * Search the Analog Configuration LUT for the Configuration ID. + * + * \param[in] configId: Configuration ID to search for. + * \param[in] configOffset: Configuration Offset in Table + * + * \return number of Configuration Sets + * \return #RFAL_ANALOG_CONFIG_LUT_NOT_FOUND in case Configuration ID is not + *found. + ***************************************************************************** + */ +static rfalAnalogConfigNum rfalAnalogConfigSearch(rfalAnalogConfigId configId, + uint16_t *configOffset) { + rfalAnalogConfigId foundConfigId; + rfalAnalogConfigId configIdMaskVal; + const uint8_t *configTbl; + const uint8_t *currentConfigTbl; + uint16_t i; + + currentConfigTbl = gRfalAnalogConfigMgmt.currentAnalogConfigTbl; + configIdMaskVal = ((RFAL_ANALOG_CONFIG_POLL_LISTEN_MODE_MASK | + RFAL_ANALOG_CONFIG_BITRATE_MASK) | + ((RFAL_ANALOG_CONFIG_TECH_CHIP == + RFAL_ANALOG_CONFIG_ID_GET_TECH(configId)) + ? (RFAL_ANALOG_CONFIG_TECH_MASK | + RFAL_ANALOG_CONFIG_CHIP_SPECIFIC_MASK) + : configId) | + ((RFAL_ANALOG_CONFIG_NO_DIRECTION == + RFAL_ANALOG_CONFIG_ID_GET_DIRECTION(configId)) + ? RFAL_ANALOG_CONFIG_DIRECTION_MASK + : configId)); + + /* When specific ConfigIDs are to be used, override search mask */ + if ((RFAL_ANALOG_CONFIG_ID_GET_DIRECTION(configId) == + RFAL_ANALOG_CONFIG_DPO) || + (RFAL_ANALOG_CONFIG_ID_GET_DIRECTION(configId) == + RFAL_ANALOG_CONFIG_DLMA)) { + configIdMaskVal = + (RFAL_ANALOG_CONFIG_POLL_LISTEN_MODE_MASK | + RFAL_ANALOG_CONFIG_TECH_MASK | RFAL_ANALOG_CONFIG_BITRATE_MASK | + RFAL_ANALOG_CONFIG_DIRECTION_MASK); + } + + i = (*configOffset); + while (i < gRfalAnalogConfigMgmt.configTblSize) { + configTbl = ¤tConfigTbl[i]; + foundConfigId = RFAL_GETU16(configTbl); + if (configId == (foundConfigId & configIdMaskVal)) { + *configOffset = (uint16_t)(i + sizeof(rfalAnalogConfigId) + + sizeof(rfalAnalogConfigNum)); + return configTbl[sizeof(rfalAnalogConfigId)]; + } + + /* If Config Id does not match, increment to next Configuration Id */ + i += (uint16_t)(sizeof(rfalAnalogConfigId) + sizeof(rfalAnalogConfigNum) + + (configTbl[sizeof(rfalAnalogConfigId)] * + sizeof(rfalAnalogConfigRegAddrMaskVal))); + } /* for */ + + return RFAL_ANALOG_CONFIG_LUT_NOT_FOUND; +} diff --git a/core/embed/io/nfc/rfal/source/rfal_cd.c b/core/embed/io/nfc/rfal/source/rfal_cd.c new file mode 100644 index 0000000000..033b3fc707 --- /dev/null +++ b/core/embed/io/nfc/rfal/source/rfal_cd.c @@ -0,0 +1,738 @@ +/****************************************************************************** + * @attention + * + * COPYRIGHT 2020 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/*! \file rfal_cd.c + * + * \author Gustavo Patricio + * + * \brief RFAL Card Detection + * + * This module implements the Card Detection Algorithm. + * It may be used for applications that require to identify if a card is on + * the vicinity of the NFC antenna, for example: to protect cards against + * damage by a wireless charger (WPC Qi PTx). + * + * Algorith details + * - The algorithm treats multiple devices as if a card is present + * - The algorithm will identify cards by the following distinguishing features + * - Only cards support NFC-V or other non standard technologies (ST25TB, + * ...) + * - Compliant cards support only a single technology + * - The algorithm will identify phones by the following distinguishing + * features + * - Only phones support P2P (NFC-DEP) + * - Only phones are able to communicate on different NFC technologies + * + */ + +/* + ****************************************************************************** + * INCLUDES + ****************************************************************************** + */ +#include "rfal_cd.h" +#include "rfal_nfca.h" +#include "rfal_nfcb.h" +#include "rfal_nfcf.h" +#include "rfal_nfcv.h" +#include "rfal_rf.h" +#include "rfal_st25tb.h" + +/* +****************************************************************************** +* GLOBAL DEFINES +****************************************************************************** +*/ + +#define RFAL_CD_NFCF_DEVLIMIT 4U /*!< NFC-F device limit (TechDet aligned) */ + +/* +****************************************************************************** +* GLOBAL MACROS +****************************************************************************** +*/ + +/* +****************************************************************************** +* GLOBAL TYPES +****************************************************************************** +*/ + +/*! Card Detection states */ +typedef enum { + RFAL_CD_ST_IDLE, /*!< CD idle */ + RFAL_CD_ST_START, /*!< CD starting */ + RFAL_CD_ST_NFCA_INIT, /*!< NFC-A Initialization */ + RFAL_CD_ST_NFCA_TECHDET, /*!< NFC-A Technology Detection */ + RFAL_CD_ST_NFCA_COLRES_START, /*!< NFC-A Collision Resolution starting */ + RFAL_CD_ST_NFCA_COLRES, /*!< NFC-A Collision Resolution */ + RFAL_CD_ST_NFCB_INIT, /*!< NFC-B Initialization */ + RFAL_CD_ST_NFCB_TECHDET, /*!< NFC-B Technology Detection */ + RFAL_CD_ST_NFCB_COLRES_START, /*!< NFC-B Collision Resolution starting */ + RFAL_CD_ST_NFCB_COLRES, /*!< NFC-B Collision Resolution */ + RFAL_CD_ST_NFCF_INIT, /*!< NFC-F Initialization */ + RFAL_CD_ST_NFCF_TECHDET_START, /*!< NFC-F Technology Detection starting */ + RFAL_CD_ST_NFCF_TECHDET, /*!< NFC-F Technology Detection */ + RFAL_CD_ST_NFCF_COLRES_START, /*!< NFC-F Collision Resolution starting */ + RFAL_CD_ST_NFCF_COLRES, /*!< NFC-F Collision Resolution */ + RFAL_CD_ST_NFCV_INIT, /*!< NFC-V Initialization */ + RFAL_CD_ST_NFCV_TECHDET, /*!< NFC-V Technology Detection */ + RFAL_CD_ST_NFCV_COLRES_START, /*!< NFC-V Collision Resolution starting */ + RFAL_CD_ST_NFCV_COLRES, /*!< NFC-V Collision Resolution */ + RFAL_CD_ST_PROPRIETARY, /*!< Proprietary NFC Technologies starting */ + RFAL_CD_ST_ST25TB_INIT, /*!< ST25TB Initialization */ + RFAL_CD_ST_ST25TB_TECHDET, /*!< ST25TB Technology Detection */ + RFAL_CD_ST_CHECK_PROTO, /*!< Evaluate device(s) found and protocols */ + RFAL_CD_ST_HB_START, /*!< Heartbeat Detection start | Field reset*/ + RFAL_CD_ST_HB, /*!< Heartbeat Detection */ + RFAL_CD_ST_DETECTED, /*!< CD completed: card detected */ + RFAL_CD_ST_NOT_DETECTED, /*!< CD completed: No card detected */ + RFAL_CD_ST_ERROR /*!< Error during card detection */ +} rfalCdState; + +/*! Card Detection context */ +typedef struct { + rfalCdState st; /*!< CD state */ + ReturnCode lastErr; /*!< Last occured error */ + rfalNfcaListenDevice nfcaDev; /*!< NFC-A Device Info */ + rfalNfcbListenDevice nfcbDev; /*!< NFC-B Device Info */ + rfalNfcfListenDevice nfcfDev[RFAL_CD_NFCF_DEVLIMIT]; /*!< NFC-F Device Info */ + uint8_t devCnt; /*!< Tech device counter */ + uint8_t mulDevCnt; /*!< Multi Tech device counter */ + rfalCdTech techFound; /*!< First NFC Technology found */ + bool skipTechFound; /*!< Second round ongoing, skip techFound */ + rfalCdRes *res; /*!< Card Detection output result location */ + uint32_t tmr; /*!< Field reset timer */ +} rfalCdCtx; + +/* + ****************************************************************************** + * LOCAL VARIABLES + ****************************************************************************** + */ + +static rfalCdCtx gCd; + +/* +****************************************************************************** +* LOCAL FUNCTION PROTOTYPES +****************************************************************************** +*/ +#ifdef RFAL_CD_HB +extern bool rfalCdHbDetect(rfalCdTech tech); +#endif /* RFAL_CD_HB */ + +/* +****************************************************************************** +* GLOBAL FUNCTION PROTOTYPES +****************************************************************************** +*/ + +/*******************************************************************************/ +ReturnCode rfalCdDetectCard(rfalCdRes *result) { + ReturnCode err; + + RFAL_EXIT_ON_ERR(err, rfalCdStartDetectCard(result)); + rfalRunBlocking(err, rfalCdGetDetectCardStatus()); + + return err; +} + +/*******************************************************************************/ +ReturnCode rfalCdStartDetectCard(rfalCdRes *result) { + if (result == NULL) { + return RFAL_ERR_PARAM; + } + + gCd.st = RFAL_CD_ST_START; + gCd.res = result; + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalCdGetDetectCardStatus(void) { + ReturnCode err; + rfalNfcaSensRes sensRes; + rfalNfcbSensbRes sensbRes; + rfalNfcvInventoryRes invRes; + + switch (gCd.st) { + /*******************************************************************************/ + case RFAL_CD_ST_START: + + gCd.mulDevCnt = 0; /* Initialize Card Detection context */ + gCd.skipTechFound = false; + gCd.techFound = RFAL_CD_TECH_NONE; + gCd.tmr = RFAL_TIMING_NONE; + + gCd.st = RFAL_CD_ST_NFCA_INIT; + break; + + /*******************************************************************************/ + case RFAL_CD_ST_NFCA_INIT: + + /* Verify if we are performing multi technology check */ + if ((gCd.skipTechFound)) { + /* If staring multi technology check if field has been Off long enough + */ + if ((!platformTimerIsExpired(gCd.tmr))) { + break; + } + + if (gCd.techFound == RFAL_CD_TECH_NFCA) { + gCd.st = RFAL_CD_ST_NFCB_INIT; /* If single card card found before was + NFC-A skip tech now */ + break; + } + } + + rfalNfcaPollerInitialize(); /* Initialize for NFC-A */ + err = rfalFieldOnAndStartGT(); /* Turns the Field On if not already and + start GT timer */ + if (err != RFAL_ERR_NONE) { + gCd.lastErr = err; + gCd.st = RFAL_CD_ST_ERROR; /* Unable to turn the field On, cannot + continue Card Detection */ + break; + } + + gCd.st = RFAL_CD_ST_NFCA_TECHDET; + break; + + /*******************************************************************************/ + case RFAL_CD_ST_NFCA_TECHDET: + + if (!rfalIsGTExpired()) { + break; /* Wait until GT has been fulfilled */ + } + + err = + rfalNfcaPollerTechnologyDetection(RFAL_COMPLIANCE_MODE_ISO, &sensRes); + if (err == RFAL_ERR_NONE) { + if (gCd.skipTechFound) /* Verify if we are performing multi technology + check */ + { + gCd.res->detType = RFAL_CD_SINGLE_MULTI_TECH; + gCd.st = RFAL_CD_ST_NOT_DETECTED; /* Single device was another + technology and now NFC-A */ + break; + } + + gCd.st = RFAL_CD_ST_NFCA_COLRES_START; /* NFC-A detected perform + collision resolution */ + break; + } + + gCd.st = RFAL_CD_ST_NFCB_INIT; /* NFC-A not detected, move to NFC-B */ + break; + + /*******************************************************************************/ + case RFAL_CD_ST_NFCA_COLRES_START: + + err = rfalNfcaPollerStartFullCollisionResolution( + RFAL_COMPLIANCE_MODE_ISO, 0, &gCd.nfcaDev, &gCd.devCnt); + if (err != RFAL_ERR_NONE) { + gCd.lastErr = err; + gCd.st = + RFAL_CD_ST_ERROR; /* Collision resolution could not be performed */ + break; + } + + gCd.st = RFAL_CD_ST_NFCA_COLRES; + break; + + /*******************************************************************************/ + case RFAL_CD_ST_NFCA_COLRES: + + err = rfalNfcaPollerGetFullCollisionResolutionStatus(); + if (err != RFAL_ERR_BUSY) { + if ((err == RFAL_ERR_NONE) && + (gCd.devCnt == + 1U)) /* Collision resolution OK and a single card was found */ + { + gCd.mulDevCnt++; + gCd.techFound = RFAL_CD_TECH_NFCA; + } + + /* Check if multiple cards or technologies have already been identified + */ + if ((err != RFAL_ERR_NONE) || (gCd.devCnt > 1U) || + (gCd.mulDevCnt > 1U)) { + gCd.res->detType = + RFAL_CD_MULTIPLE_DEV; /* Report multiple devices. A T1T will also + fail at ColRes */ + gCd.st = RFAL_CD_ST_DETECTED; + + break; + } + + gCd.st = RFAL_CD_ST_NFCB_INIT; /* Move to NFC-B */ + } + break; + + /*******************************************************************************/ + case RFAL_CD_ST_NFCB_INIT: + + /* Verify if we are performing multi technology check */ + if ((gCd.skipTechFound) && (gCd.techFound == RFAL_CD_TECH_NFCB)) { + gCd.st = RFAL_CD_ST_NFCF_INIT; /* If single card card found before was + NFC-B skip tech now */ + break; + } + + rfalNfcbPollerInitialize(); /* Initialize for NFC-B */ + rfalFieldOnAndStartGT(); /* Turns the Field On if not already and start GT + timer */ + + gCd.st = RFAL_CD_ST_NFCB_TECHDET; + break; + + /*******************************************************************************/ + case RFAL_CD_ST_NFCB_TECHDET: + + if (!rfalIsGTExpired()) { + break; /* Wait until GT has been fulfilled */ + } + + err = rfalNfcbPollerTechnologyDetection(RFAL_COMPLIANCE_MODE_NFC, + &sensbRes, &gCd.devCnt); + if (err == RFAL_ERR_NONE) { + /* Verify if we are performing multi technology check OR already found + * one on the first round */ + if (gCd.skipTechFound) { + gCd.res->detType = RFAL_CD_SINGLE_MULTI_TECH; + gCd.st = RFAL_CD_ST_NOT_DETECTED; /* Single device was another + technology and now NFC-B */ + break; + } else if (gCd.techFound != + RFAL_CD_TECH_NONE) /* If on the first round check if other + Tech was already found */ + { + gCd.res->detType = RFAL_CD_MULTIPLE_TECH; + gCd.st = RFAL_CD_ST_DETECTED; + break; + } else { + /* MISRA 15.7 - Empty else */ + } + + gCd.st = RFAL_CD_ST_NFCB_COLRES_START; /* NFC-B detected perform + collision resolution */ + break; + } + + gCd.st = RFAL_CD_ST_NFCF_INIT; /* NFC-B not detected, move to NFC-B */ + break; + + /*******************************************************************************/ + case RFAL_CD_ST_NFCB_COLRES_START: + + err = rfalNfcbPollerStartCollisionResolution(RFAL_COMPLIANCE_MODE_NFC, 0, + &gCd.nfcbDev, &gCd.devCnt); + if (err != RFAL_ERR_NONE) { + gCd.lastErr = err; + gCd.st = + RFAL_CD_ST_ERROR; /* Collision resolution could not be performed */ + break; + } + + gCd.st = RFAL_CD_ST_NFCB_COLRES; + break; + + /*******************************************************************************/ + case RFAL_CD_ST_NFCB_COLRES: + + err = rfalNfcbPollerGetCollisionResolutionStatus(); + if (err != RFAL_ERR_BUSY) { + if ((err == RFAL_ERR_NONE) && + (gCd.devCnt == + 1U)) /* Collision resolution OK and a single card was found */ + { + gCd.mulDevCnt++; + gCd.techFound = RFAL_CD_TECH_NFCB; + } + + /* Check if multiple cards or technologies have already been identified + */ + if ((err != RFAL_ERR_NONE) || (gCd.devCnt > 1U) || + (gCd.mulDevCnt > 1U)) { + gCd.res->detType = RFAL_CD_MULTIPLE_DEV; + gCd.st = RFAL_CD_ST_DETECTED; + break; + } + + gCd.st = ((RFAL_SUPPORT_MODE_POLL_NFCF) + ? RFAL_CD_ST_NFCF_INIT + : RFAL_CD_ST_NFCV_INIT); /* Move to NFC-F or NFC-V */ + } + break; + +#if RFAL_SUPPORT_MODE_POLL_NFCF + /*******************************************************************************/ + case RFAL_CD_ST_NFCF_INIT: + + /* Verify if we are performing multi technology check */ + if ((gCd.skipTechFound) && (gCd.techFound == RFAL_CD_TECH_NFCF)) { + gCd.st = RFAL_CD_ST_PROPRIETARY; /* If single card card found before was + NFC-F skip tech now */ + break; + } + + rfalNfcfPollerInitialize(RFAL_BR_212); /* Initialize for NFC-F */ + rfalFieldOnAndStartGT(); /* Turns the Field On if not already and start GT + timer */ + + gCd.st = RFAL_CD_ST_NFCF_TECHDET_START; + break; + + /*******************************************************************************/ + case RFAL_CD_ST_NFCF_TECHDET_START: + if (!rfalIsGTExpired()) { + break; /* Wait until GT has been fulfilled */ + } + + err = rfalNfcfPollerStartCheckPresence(); + gCd.st = RFAL_CD_ST_NFCF_TECHDET; + break; + + /*******************************************************************************/ + case RFAL_CD_ST_NFCF_TECHDET: + + err = rfalNfcfPollerGetCheckPresenceStatus(); + if (err == RFAL_ERR_BUSY) { + break; /* Wait until NFC-F Technlogy Detection is completed */ + } + + if (gCd.skipTechFound) /* Verify if we are performing multi technology + check */ + { + gCd.st = RFAL_CD_ST_PROPRIETARY; + + /* If single device was another technology and now NFC-F, otherwise + * conclude*/ + if (err == RFAL_ERR_NONE) { + gCd.res->detType = RFAL_CD_SINGLE_MULTI_TECH; + gCd.st = RFAL_CD_ST_NOT_DETECTED; + } + break; + } + + if (err == RFAL_ERR_NONE) { + if (gCd.techFound != + RFAL_CD_TECH_NONE) /* If on the first round check if other Tech was + already found */ + { + gCd.res->detType = RFAL_CD_MULTIPLE_TECH; + gCd.st = RFAL_CD_ST_DETECTED; + break; + } + + gCd.st = RFAL_CD_ST_NFCF_COLRES_START; /* NFC-F detected, perform + collision resolution */ + break; + } + + gCd.st = RFAL_CD_ST_NFCV_INIT; /* NFC-F not detected, move to NFC-V */ + break; + + /*******************************************************************************/ + case RFAL_CD_ST_NFCF_COLRES_START: + + err = rfalNfcfPollerStartCollisionResolution(RFAL_COMPLIANCE_MODE_NFC, + RFAL_CD_NFCF_DEVLIMIT, + gCd.nfcfDev, &gCd.devCnt); + if (err != RFAL_ERR_NONE) { + gCd.lastErr = err; + gCd.st = + RFAL_CD_ST_ERROR; /* Collision resolution could not be performed */ + break; + } + + gCd.st = RFAL_CD_ST_NFCF_COLRES; + break; + + /*******************************************************************************/ + case RFAL_CD_ST_NFCF_COLRES: + + err = rfalNfcfPollerGetCollisionResolutionStatus(); + if (err != RFAL_ERR_BUSY) { + if ((err == RFAL_ERR_NONE) && + (gCd.devCnt == + 1U)) /* Collision resolution OK and a single card was found */ + { + gCd.mulDevCnt++; + gCd.techFound = RFAL_CD_TECH_NFCF; + } + + /* Check if multiple cards or technologies have already been identified + */ + if ((err != RFAL_ERR_NONE) || (gCd.devCnt > 1U) || + (gCd.mulDevCnt > 1U)) { + gCd.res->detType = RFAL_CD_MULTIPLE_DEV; + gCd.st = RFAL_CD_ST_DETECTED; + break; + } + + gCd.st = RFAL_CD_ST_NFCV_INIT; /* Move to NFC-V */ + } + break; +#endif /* RFAL_SUPPORT_MODE_POLL_NFCF*/ + + /*******************************************************************************/ + case RFAL_CD_ST_NFCV_INIT: + + rfalNfcvPollerInitialize(); /* Initialize for NFC-V */ + rfalFieldOnAndStartGT(); /* Turns the Field On if not already and start GT + timer */ + + gCd.st = RFAL_CD_ST_NFCV_TECHDET; + break; + + /*******************************************************************************/ + case RFAL_CD_ST_NFCV_TECHDET: + + if (!rfalIsGTExpired()) { + break; /* Wait until GT has been fulfilled */ + } + + err = rfalNfcvPollerCheckPresence(&invRes); + if (err == RFAL_ERR_NONE) { + if (gCd.techFound != + RFAL_CD_TECH_NONE) /* If other Tech was already found */ + { + gCd.res->detType = RFAL_CD_MULTIPLE_TECH; + gCd.st = RFAL_CD_ST_DETECTED; + break; + } + + gCd.techFound = + RFAL_CD_TECH_NFCV; /* If NFC-V is regarded as card as CE NFC-V is + currently not supported by active devices */ + gCd.res->detType = RFAL_CD_CARD_TECH; + gCd.st = RFAL_CD_ST_DETECTED; + break; + } + + gCd.st = + RFAL_CD_ST_PROPRIETARY; /* Move to Proprietary NFC Technologies */ + break; + + /*******************************************************************************/ + case RFAL_CD_ST_PROPRIETARY: + + rfalFieldOff(); + platformTimerDestroy(gCd.tmr); + gCd.tmr = platformTimerCreate((uint8_t)rfalConv1fcToMs(RFAL_GT_NFCA)); + + /* If none of the other NFC technologies was not seen on a second round, + * regard as card */ + if (gCd.skipTechFound) { + gCd.res->detType = RFAL_CD_SINGLE_DEV; + gCd.st = RFAL_CD_ST_DETECTED; + + /*******************************************************************************/ + /* Only one device found which does not support NFC-DEP and only * + * answered in one technology, perform heartbeat detection */ + +#ifdef RFAL_CD_HB + gCd.st = RFAL_CD_ST_HB_START; +#endif /* RFAL_CD_HB */ + + /*******************************************************************************/ + + break; + } + + gCd.st = RFAL_CD_ST_ST25TB_INIT; + break; + + /*******************************************************************************/ + case RFAL_CD_ST_ST25TB_INIT: + + if ((!platformTimerIsExpired( + gCd.tmr))) /* Check if field has been Off long enough */ + { + break; + } + + rfalSt25tbPollerInitialize(); /* Initialize for ST25TB */ + err = rfalFieldOnAndStartGT(); /* Turns the Field On if not already and + start GT timer */ + + if (err != RFAL_ERR_NONE) { + gCd.lastErr = err; + gCd.st = RFAL_CD_ST_ERROR; /* Unable to turn the field On, cannot + continue Card Detection */ + break; + } + + gCd.st = RFAL_CD_ST_ST25TB_TECHDET; + break; + + /*******************************************************************************/ + case RFAL_CD_ST_ST25TB_TECHDET: + + if ((!rfalIsGTExpired())) { + break; /* Wait until GT has been fulfilled */ + } + + err = rfalSt25tbPollerCheckPresence(NULL); + if (err == RFAL_ERR_NONE) { + gCd.techFound = + RFAL_CD_TECH_OTHER; /* If ST25TB is regarded as card as CE is not + supported by active devices */ + gCd.res->detType = RFAL_CD_CARD_TECH; + gCd.st = RFAL_CD_ST_DETECTED; + break; + } + + gCd.st = RFAL_CD_ST_CHECK_PROTO; + break; + + /*******************************************************************************/ + case RFAL_CD_ST_CHECK_PROTO: + + if (gCd.mulDevCnt == 0U) /* No NFC listener has been detected */ + { + gCd.res->detType = RFAL_CD_NOT_FOUND; + gCd.st = RFAL_CD_ST_NOT_DETECTED; + break; + } + + if (gCd.mulDevCnt == 1U) /* A single NFC listener has been identified */ + { + /* Check if it supports NFC-DEP protocol */ + if (((gCd.techFound == RFAL_CD_TECH_NFCA) && + ((gCd.nfcaDev.type == RFAL_NFCA_NFCDEP) || + (gCd.nfcaDev.type == RFAL_NFCA_T4T_NFCDEP))) || + ((gCd.techFound == RFAL_CD_TECH_NFCF) && + rfalNfcfIsNfcDepSupported(&gCd.nfcfDev[0]))) { + gCd.res->detType = RFAL_CD_SINGLE_P2P; + gCd.st = RFAL_CD_ST_NOT_DETECTED; /* NFC-DEP supported, regarded as + non passive card */ + break; + } + + /* If a single NFC listener has been detected, and did not announce + * NFC-DEP support, * check if it supports mutiple NFC technologies + * (skip the one it was previous seen) */ + gCd.skipTechFound = true; + gCd.st = RFAL_CD_ST_NFCA_INIT; + + /* Reset Field once again to avoid unwanted effect of Proprietary NFC + * Tech modulation */ + rfalFieldOff(); + platformTimerDestroy(gCd.tmr); + gCd.tmr = platformTimerCreate((uint8_t)rfalConv1fcToMs(RFAL_GT_NFCA)); + break; + } + + gCd.res->detType = RFAL_CD_MULTIPLE_DEV; + gCd.st = RFAL_CD_ST_DETECTED; + break; + +#ifdef RFAL_CD_HB + /*******************************************************************************/ + case RFAL_CD_ST_HB_START: + + if ((!platformTimerIsExpired( + gCd.tmr))) /* Check if field has been Off long enough */ + { + break; + } + + switch (gCd.techFound) { + case RFAL_CD_TECH_NFCF: + rfalNfcfPollerInitialize(RFAL_BR_212); + break; + + case RFAL_CD_TECH_NFCB: + rfalNfcbPollerInitialize(); + break; + + case RFAL_CD_TECH_NFCA: + default: + rfalNfcaPollerInitialize(); + break; + } + + err = rfalFieldOnAndStartGT(); + if (err != RFAL_ERR_NONE) { + gCd.lastErr = err; + gCd.st = RFAL_CD_ST_ERROR; /* Unable to turn the field On, cannot + continue Card Detection */ + break; + } + + gCd.st = RFAL_CD_ST_HB; + break; + + /*******************************************************************************/ + case RFAL_CD_ST_HB: + if (!rfalIsGTExpired()) /* Check if GT has been fulfilled */ + { + break; + } + + if (rfalCdHbDetect( + gCd.techFound)) /* Perform tha heartbeat detection sequence */ + { + gCd.res->detType = RFAL_CD_SINGLE_HB; + gCd.st = RFAL_CD_ST_NOT_DETECTED; /* Single device performing ALM, no + passive card */ + break; + } + + gCd.res->detType = RFAL_CD_SINGLE_DEV; + gCd.st = RFAL_CD_ST_DETECTED; /* ALM not detected on single device, regard + as card */ + break; +#endif /* RFAL_CD_HB */ + + /*******************************************************************************/ + case RFAL_CD_ST_DETECTED: + case RFAL_CD_ST_NOT_DETECTED: + + /* Card Detection completed, return outcome */ + gCd.res->detected = ((gCd.st == RFAL_CD_ST_NOT_DETECTED) ? false : true); + + rfalFieldOff(); + gCd.st = RFAL_CD_ST_IDLE; + + return RFAL_ERR_NONE; + + /*******************************************************************************/ + case RFAL_CD_ST_IDLE: + return RFAL_ERR_WRONG_STATE; + + /*******************************************************************************/ + case RFAL_CD_ST_ERROR: + + gCd.res->detType = RFAL_CD_UNKOWN; + gCd.res->detected = + true; /* Error ocurred, mark as card present to avoid damage */ + + rfalFieldOff(); + gCd.st = RFAL_CD_ST_IDLE; + + return gCd.lastErr; + + /*******************************************************************************/ + default: + return RFAL_ERR_INTERNAL; + } + + return RFAL_ERR_BUSY; +} diff --git a/core/embed/io/nfc/rfal/source/rfal_crc.c b/core/embed/io/nfc/rfal/source/rfal_crc.c new file mode 100644 index 0000000000..27c33f21b4 --- /dev/null +++ b/core/embed/io/nfc/rfal/source/rfal_crc.c @@ -0,0 +1,78 @@ + +/****************************************************************************** + * @attention + * + * COPYRIGHT 2016 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/* + * PROJECT: ST25R391x firmware + * Revision: + * LANGUAGE: ISO C99 + */ + +/*! \file rfal_crc.c + * + * \author Oliver Regenfelder + * + * \brief CRC calculation implementation + * + */ + +/* +****************************************************************************** +* INCLUDES +****************************************************************************** +*/ +#include "rfal_crc.h" + +/* +****************************************************************************** +* LOCAL FUNCTION PROTOTYPES +****************************************************************************** +*/ +static uint16_t rfalCrcUpdateCcitt(uint16_t crcSeed, uint8_t dataByte); + +/* +****************************************************************************** +* GLOBAL FUNCTIONS +****************************************************************************** +*/ +uint16_t rfalCrcCalculateCcitt(uint16_t preloadValue, const uint8_t* buf, + uint16_t length) { + uint16_t crc = preloadValue; + uint16_t index; + + for (index = 0; index < length; index++) { + crc = rfalCrcUpdateCcitt(crc, buf[index]); + } + + return crc; +} + +/* +****************************************************************************** +* LOCAL FUNCTIONS +****************************************************************************** +*/ +static uint16_t rfalCrcUpdateCcitt(uint16_t crcSeed, uint8_t dataByte) { + uint16_t crc = crcSeed; + uint8_t dat = dataByte; + + dat ^= (uint8_t)(crc & 0xFFU); + dat ^= (dat << 4); + + crc = (crc >> 8) ^ (((uint16_t)dat) << 8) ^ (((uint16_t)dat) << 3) ^ + (((uint16_t)dat) >> 4); + + return crc; +} diff --git a/core/embed/io/nfc/rfal/source/rfal_crc.h b/core/embed/io/nfc/rfal/source/rfal_crc.h new file mode 100644 index 0000000000..571541f3f2 --- /dev/null +++ b/core/embed/io/nfc/rfal/source/rfal_crc.h @@ -0,0 +1,69 @@ + +/****************************************************************************** + * @attention + * + * COPYRIGHT 2016 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/* + * PROJECT: ST25R391x firmware + * Revision: + * LANGUAGE: ISO C99 + */ + +/*! \file rfal_crc.h + * + * \author Ulrich Herrmann + * + * \brief CRC calculation module + * + */ +/*! + * + */ + +#ifndef RFAL_CRC_H_ +#define RFAL_CRC_H_ + +/* +****************************************************************************** +* INCLUDES +****************************************************************************** +*/ +#include "rfal_platform.h" + +/* +****************************************************************************** +* GLOBAL FUNCTION PROTOTYPES +****************************************************************************** +*/ +/*! + ***************************************************************************** + * \brief Calculate CRC according to CCITT standard. + * + * This function takes \a length bytes from \a buf and calculates the CRC + * for this data. The result is returned. + * \note This implementation calculates the CRC with LSB first, i.e. all + * bytes are "read" from right to left. + * + * \param[in] preloadValue : Initial value of CRC calculation. + * \param[in] buf : buffer to calculate the CRC for. + * \param[in] length : size of the buffer. + * + * \return 16 bit long crc value. + * + ***************************************************************************** + */ +extern uint16_t rfalCrcCalculateCcitt(uint16_t preloadValue, const uint8_t* buf, + uint16_t length); + +#endif /* RFAL_CRC_H_ */ diff --git a/core/embed/io/nfc/rfal/source/rfal_dpo.c b/core/embed/io/nfc/rfal/source/rfal_dpo.c new file mode 100644 index 0000000000..b8a22ea993 --- /dev/null +++ b/core/embed/io/nfc/rfal/source/rfal_dpo.c @@ -0,0 +1,290 @@ + +/****************************************************************************** + * @attention + * + * COPYRIGHT 2016 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/* + * PROJECT: ST25R391x firmware + * $Revision: $ + * LANGUAGE: ISO C99 + */ + +/*! \file rfal_dpo.c + * + * \author Martin Zechleitner + * + * \brief Functions to manage and set dynamic power settings + * + */ + +/* + ****************************************************************************** + * INCLUDES + ****************************************************************************** + */ +#include "rfal_dpo.h" +#include "rfal_analogConfig.h" +#include "rfal_chip.h" +#include "rfal_dpoTbl.h" +#include "rfal_platform.h" +#include "rfal_rf.h" +#include "rfal_utils.h" + +/* + ****************************************************************************** + * ENABLE SWITCH + ****************************************************************************** + */ + +/* Feature switch may be enabled or disabled by user at rfal_platform.h + * Default configuration (ST25R dependant) also provided at rfal_defConfig.h + * + * RFAL_FEATURE_DPO + */ + +#if RFAL_FEATURE_DPO + +/* + ****************************************************************************** + * DEFINES + ****************************************************************************** + */ +#define RFAL_DPO_ANALOGCONFIG_SHIFT 13U +#define RFAL_DPO_ANALOGCONFIG_MASK 0x6000U + +/* + ****************************************************************************** + * LOCAL DATA TYPES + ****************************************************************************** + */ + +/*! RFAL DPO instance */ +typedef struct { + bool enabled; + const rfalDpoEntry* currentDpo; + uint8_t tableEntries; + rfalDpoEntry table[RFAL_DPO_TABLE_MAX_ENTRIES]; + uint8_t tableEntry; + rfalDpoMeasureFunc measureCallback; + rfalMode curMode; + rfalBitRate curBR; +} rfalDpo; + +/* + ****************************************************************************** + * LOCAL VARIABLES + ****************************************************************************** + */ + +static rfalDpo gRfalDpo; + +/* + ****************************************************************************** + * GLOBAL FUNCTIONS + ****************************************************************************** + */ +void rfalDpoInitialize(void) { + /* By default DPO is disabled */ + rfalDpoSetEnabled(false); + +/* Set default measurement */ +#if defined(ST25R3911) || defined(ST25R3916) || defined(ST25R3916B) + gRfalDpo.measureCallback = rfalChipMeasureAmplitude; +#else + gRfalDpo.measureCallback = rfalChipMeasureCombinedIQ; +#endif /* ST25R */ + + /* Use the default Dynamic Power values */ + gRfalDpo.currentDpo = rfalDpoDefaultSettings; + gRfalDpo.tableEntries = + (sizeof(rfalDpoDefaultSettings) / RFAL_DPO_TABLE_PARAM_LEN); + + RFAL_MEMCPY(gRfalDpo.table, gRfalDpo.currentDpo, + sizeof(rfalDpoDefaultSettings)); +} + +/*******************************************************************************/ +void rfalDpoSetMeasureCallback(rfalDpoMeasureFunc pFunc) { + gRfalDpo.measureCallback = pFunc; +} + +/*******************************************************************************/ +ReturnCode rfalDpoTableWrite(const rfalDpoEntry* powerTbl, + uint8_t powerTblEntries) { + uint8_t entry; + + /* Check if the table size parameter is too big */ + if ((powerTblEntries * RFAL_DPO_TABLE_PARAM_LEN) > RFAL_DPO_TABLE_SIZE_MAX) { + return RFAL_ERR_NOMEM; + } + + /* Check if the first increase entry is 0xFF */ + if ((powerTblEntries == 0U) || (powerTbl == NULL)) { + return RFAL_ERR_PARAM; + } + + /* Check if the entries of the dynamic power table are valid */ + for (entry = 0; entry < powerTblEntries; entry++) { + if (powerTbl[entry].inc < powerTbl[entry].dec) { + return RFAL_ERR_PARAM; + } + } + + /* Copy the data set */ + RFAL_MEMCPY(gRfalDpo.table, powerTbl, + (powerTblEntries * RFAL_DPO_TABLE_PARAM_LEN)); + + gRfalDpo.currentDpo = gRfalDpo.table; + gRfalDpo.tableEntries = powerTblEntries; + + if (gRfalDpo.tableEntry > powerTblEntries) { + /* powerTblEntries is always greater then zero, verified at parameter check + */ + gRfalDpo.tableEntry = (powerTblEntries - 1U); + } + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalDpoTableRead(rfalDpoEntry* tblBuf, uint8_t tblBufEntries, + uint8_t* tableEntries) { + /* Wrong request */ + if ((tblBuf == NULL) || (tblBufEntries < gRfalDpo.tableEntries) || + (tableEntries == NULL)) { + return RFAL_ERR_PARAM; + } + + /* Not properly initialized */ + if (gRfalDpo.currentDpo == NULL) { + return RFAL_ERR_WRONG_STATE; + } + + /* Copy the whole Table to the given buffer */ + RFAL_MEMCPY(tblBuf, gRfalDpo.currentDpo, + (tblBufEntries * RFAL_DPO_TABLE_PARAM_LEN)); + *tableEntries = gRfalDpo.tableEntries; + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalDpoAdjust(void) { + uint8_t refValue; + uint16_t modeID; + rfalBitRate br; + rfalMode mode; + uint8_t tableEntry; + const rfalDpoEntry* dpoTable; + + /* Initialize local vars */ + tableEntry = gRfalDpo.tableEntry; + dpoTable = (const rfalDpoEntry*)gRfalDpo.currentDpo; + refValue = 0; + mode = RFAL_MODE_NONE; + br = RFAL_BR_KEEP; + + /* Obtain RFAL's current mode and bit rate */ + mode = rfalGetMode(); + rfalGetBitRate(&br, NULL); + + /* Check if the Power Adjustment is disabled and * + * if the callback to the measurement method is properly set */ + if ((!gRfalDpo.enabled) || (gRfalDpo.measureCallback == NULL)) { + return RFAL_ERR_PARAM; + } + + /* Ensure that the current mode is Passive Poller and table is initialized*/ + if ((!rfalIsModePassivePoll(mode)) || (gRfalDpo.currentDpo == NULL)) { + return RFAL_ERR_WRONG_STATE; + } + + /* Ensure a proper measure reference value */ + if (RFAL_ERR_NONE != gRfalDpo.measureCallback(&refValue)) { + return RFAL_ERR_IO; + } + + if (refValue >= + dpoTable[gRfalDpo.tableEntry].inc) { /* Increase the output power */ + /* the top of the table represents the highest amplitude value*/ + if (gRfalDpo.tableEntry == 0U) { + /* Maximum driver value has been reached */ + } else { + /* Go up in the table to decrease the driver resistance */ + tableEntry--; + } + } else if (refValue <= dpoTable[gRfalDpo.tableEntry] + .dec) { /* Decrease the output power */ + /* The bottom is the highest possible value */ + if ((gRfalDpo.tableEntry + 1U) >= gRfalDpo.tableEntries) { + /* minimum driver value has been reached */ + } else { + /* Go down in the table to increase the driver resistance */ + tableEntry++; + } + } else { + /* Fall through to evaluate whether to write dpo and its associated analog + * configs */ + } + + /* Apply new configs if there was a change on DPO level or RFAL mode|bitrate + */ + /* Also adjust power in case mode is not yet set and a different table + * entry|setting is applicbale */ + if ((mode != gRfalDpo.curMode) || (br != gRfalDpo.curBR) || + (tableEntry != gRfalDpo.tableEntry) || + ((mode == RFAL_MODE_NONE) && (tableEntry != gRfalDpo.tableEntry))) { + /* Update local context */ + gRfalDpo.curMode = mode; + gRfalDpo.curBR = br; + gRfalDpo.tableEntry = tableEntry; + + /* Get the new value for RFO resistance form the table and apply the new RFO + * resistance setting */ + rfalChipSetRFO(dpoTable[gRfalDpo.tableEntry].rfoRes); + + /* Apply the DPO Analog Config according to this threshold */ + /* Technology field is being extended for DPO: 2msb are used for threshold + * step (only 4 allowed) */ + modeID = rfalAnalogConfigGenModeID( + gRfalDpo.curMode, gRfalDpo.curBR, + RFAL_ANALOG_CONFIG_DPO); /* Generate Analog Config mode ID */ + modeID |= (((uint16_t)gRfalDpo.tableEntry << RFAL_DPO_ANALOGCONFIG_SHIFT) & + RFAL_DPO_ANALOGCONFIG_MASK); /* Add DPO threshold step|level */ + rfalSetAnalogConfig(modeID); /* Apply DPO Analog Config */ + } + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +const rfalDpoEntry* rfalDpoGetCurrentTableEntry(void) { + return &gRfalDpo.currentDpo[gRfalDpo.tableEntry]; +} + +/*******************************************************************************/ +uint8_t rfalDpoGetCurrentTableIndex(void) { return gRfalDpo.tableEntry; } + +/*******************************************************************************/ +void rfalDpoSetEnabled(bool enable) { + gRfalDpo.enabled = enable; + gRfalDpo.curMode = RFAL_MODE_NONE; + gRfalDpo.curBR = RFAL_BR_KEEP; + gRfalDpo.tableEntry = 0; +} + +/*******************************************************************************/ +bool rfalDpoIsEnabled(void) { return gRfalDpo.enabled; } + +#endif /* RFAL_FEATURE_DPO */ diff --git a/core/embed/io/nfc/rfal/source/rfal_iso15693_2.c b/core/embed/io/nfc/rfal/source/rfal_iso15693_2.c new file mode 100644 index 0000000000..c6c099e18d --- /dev/null +++ b/core/embed/io/nfc/rfal/source/rfal_iso15693_2.c @@ -0,0 +1,518 @@ + +/****************************************************************************** + * @attention + * + * COPYRIGHT 2016 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/* + * PROJECT: ST25R391x firmware + * Revision: + * LANGUAGE: ISO C99 + */ + +/*! \file rfal_iso15693_2.c + * + * \author Ulrich Herrmann + * + * \brief Implementation of ISO-15693-2 + * + */ + +/* +****************************************************************************** +* INCLUDES +****************************************************************************** +*/ +#include "rfal_iso15693_2.h" +#include "rfal_crc.h" +#include "rfal_utils.h" + +/* + ****************************************************************************** + * ENABLE SWITCH + ****************************************************************************** + */ + +/* Feature switch may be enabled or disabled by user at rfal_platform.h + * Default configuration (ST25R dependant) also provided at rfal_defConfig.h + * + * RFAL_FEATURE_NFCV + */ + +#if RFAL_FEATURE_NFCV + +/* +****************************************************************************** +* LOCAL MACROS +****************************************************************************** +*/ + +#define ISO_15693_DEBUG(...) /*!< Macro for the log method */ + +/* +****************************************************************************** +* LOCAL DEFINES +****************************************************************************** +*/ +#define ISO15693_DAT_SOF_1_4 0x21 /* LSB constants */ +#define ISO15693_DAT_EOF_1_4 0x04 +#define ISO15693_DAT_00_1_4 0x02 +#define ISO15693_DAT_01_1_4 0x08 +#define ISO15693_DAT_10_1_4 0x20 +#define ISO15693_DAT_11_1_4 0x80 + +#define ISO15693_DAT_SOF_1_256 0x81 +#define ISO15693_DAT_EOF_1_256 0x04 +#define ISO15693_DAT_SLOT0_1_256 0x02 +#define ISO15693_DAT_SLOT1_1_256 0x08 +#define ISO15693_DAT_SLOT2_1_256 0x20 +#define ISO15693_DAT_SLOT3_1_256 0x80 + +#define ISO15693_PHY_DAT_MANCHESTER_1 0xaaaa + +#define ISO15693_PHY_BIT_BUFFER_SIZE \ + 1000 /*!< size of the receiving buffer. Might be adjusted if longer \ + datastreams are expected. */ + +/* +****************************************************************************** +* LOCAL VARIABLES +****************************************************************************** +*/ +static rfalIso15693PhyConfig_t + gIso15693PhyConfig; /*!< current phy configuration */ + +/* +****************************************************************************** +* LOCAL FUNCTION PROTOTYPES +****************************************************************************** +*/ +static ReturnCode rfalIso15693PhyVCDCode1Of4(const uint8_t data, + uint8_t* outbuffer, + uint16_t maxOutBufLen, + uint16_t* outBufLen); +static ReturnCode rfalIso15693PhyVCDCode1Of256(const uint8_t data, + uint8_t* outbuffer, + uint16_t maxOutBufLen, + uint16_t* outBufLen); + +/* +****************************************************************************** +* GLOBAL FUNCTIONS +****************************************************************************** +*/ +ReturnCode rfalIso15693PhyConfigure( + const rfalIso15693PhyConfig_t* config, + const struct iso15693StreamConfig** needed_stream_config) { + static struct iso15693StreamConfig auxConfig = { + /* MISRA 8.9 */ + .useBPSK = 0, /* 0: subcarrier, 1:BPSK */ + .din = + 5, /* 2^5*fc = 423750 Hz: divider for the in subcarrier frequency */ + .dout = + 7, /*!< 2^7*fc = 105937 : divider for the in subcarrier frequency */ + .report_period_length = + 3, /*!< 8=2^3 the length of the reporting period */ + }; + + /* make a copy of the configuration */ + RFAL_MEMCPY((uint8_t*)&gIso15693PhyConfig, (const uint8_t*)config, + sizeof(rfalIso15693PhyConfig_t)); + + if (config->speedMode <= + 3U) { /* If valid speed mode adjust report period accordingly */ + auxConfig.report_period_length = (3U - (uint8_t)config->speedMode); + } else { /* If invalid default to normal (high) speed */ + auxConfig.report_period_length = 3; + } + + *needed_stream_config = &auxConfig; + + return RFAL_ERR_NONE; +} + +ReturnCode rfalIso15693PhyGetConfiguration(rfalIso15693PhyConfig_t* config) { + RFAL_MEMCPY(config, &gIso15693PhyConfig, sizeof(rfalIso15693PhyConfig_t)); + + return RFAL_ERR_NONE; +} + +ReturnCode rfalIso15693VCDCode(uint8_t* buffer, uint16_t length, bool sendCrc, + bool sendFlags, bool picopassMode, + uint16_t* subbit_total_length, uint16_t* offset, + uint8_t* outbuf, uint16_t outBufSize, + uint16_t* actOutBufSize) { + ReturnCode err = RFAL_ERR_NONE; + uint8_t eof, sof; + uint8_t transbuf[2]; + uint16_t crc = 0; + ReturnCode (*txFunc)(const uint8_t data, uint8_t* outbuffer, + uint16_t maxOutBufLen, uint16_t* outBufLen); + uint8_t crc_len; + uint8_t* outputBuf; + uint16_t outputBufSize; + + crc_len = (uint8_t)((sendCrc) ? 2 : 0); + + *actOutBufSize = 0; + + if (ISO15693_VCD_CODING_1_4 == gIso15693PhyConfig.coding) { + sof = ISO15693_DAT_SOF_1_4; + eof = ISO15693_DAT_EOF_1_4; + txFunc = rfalIso15693PhyVCDCode1Of4; + *subbit_total_length = + ((1U /* SOF */ + + ((length + (uint16_t)crc_len) * 4U) + 1U) /* EOF */ + ); + if (outBufSize < + 5U) { /* 5 should be safe: enough for sof + 1byte data in 1of4 */ + return RFAL_ERR_NOMEM; + } + } else { + sof = ISO15693_DAT_SOF_1_256; + eof = ISO15693_DAT_EOF_1_256; + txFunc = rfalIso15693PhyVCDCode1Of256; + *subbit_total_length = + ((1U /* SOF */ + + ((length + (uint16_t)crc_len) * 64U) + 1U) /* EOF */ + ); + + if (*offset != 0U) { + if (outBufSize < + 64U) { /* 64 should be safe: enough a single byte data in 1of256 */ + return RFAL_ERR_NOMEM; + } + } else { + if (outBufSize < + 65U) { /* At beginning of a frame we need at least 65 bytes to start: + enough for sof + 1byte data in 1of256 */ + return RFAL_ERR_NOMEM; + } + } + } + + if (length == 0U) { + *subbit_total_length = 1; + } + + if ((length != 0U) && (0U == *offset) && sendFlags && (!picopassMode)) { + /* set high datarate flag */ + buffer[0] |= (uint8_t)ISO15693_REQ_FLAG_HIGH_DATARATE; + /* clear sub-carrier flag - we only support single sub-carrier */ + buffer[0] = (uint8_t)(buffer[0] & + ~ISO15693_REQ_FLAG_TWO_SUBCARRIERS); /* MISRA 10.3 */ + } + + outputBuf = outbuf; /* MISRA 17.8: Use intermediate variable */ + outputBufSize = outBufSize; /* MISRA 17.8: Use intermediate variable */ + + /* Send SOF if at 0 offset */ + if ((length != 0U) && (0U == *offset)) { + *outputBuf = sof; + (*actOutBufSize)++; + outputBufSize--; + outputBuf++; + } + + while ((*offset < length) && (err == RFAL_ERR_NONE)) { + uint16_t filled_size; + /* send data */ + err = txFunc(buffer[*offset], outputBuf, outputBufSize, &filled_size); + (*actOutBufSize) += filled_size; + outputBuf = + &outputBuf[filled_size]; /* MISRA 18.4: Avoid pointer arithmetic */ + outputBufSize -= filled_size; + if (err == RFAL_ERR_NONE) { + (*offset)++; + } + } + if (err != RFAL_ERR_NONE) { + return RFAL_ERR_AGAIN; + } + + while ((err == RFAL_ERR_NONE) && sendCrc && (*offset < (length + 2U))) { + uint16_t filled_size; + if ((0U == crc) && (length != 0U)) { + crc = rfalCrcCalculateCcitt( + (uint16_t)((picopassMode) ? 0xE012U + : 0xFFFFU), /* In PicoPass Mode a different + Preset Value is used */ + ((picopassMode) ? (buffer + 1U) + : buffer), /* CMD byte is not taken into account in + PicoPass mode */ + ((picopassMode) ? (length - 1U) + : length)); /* CMD byte is not taken into account in + PicoPass mode */ + + crc = (uint16_t)((picopassMode) ? crc : ~crc); + } + /* send crc */ + transbuf[0] = (uint8_t)(crc & 0xffU); + transbuf[1] = (uint8_t)((crc >> 8) & 0xffU); + err = txFunc(transbuf[*offset - length], outputBuf, outputBufSize, + &filled_size); + (*actOutBufSize) += filled_size; + outputBuf = + &outputBuf[filled_size]; /* MISRA 18.4: Avoid pointer arithmetic */ + outputBufSize -= filled_size; + if (err == RFAL_ERR_NONE) { + (*offset)++; + } + } + if (err != RFAL_ERR_NONE) { + return RFAL_ERR_AGAIN; + } + + if (((!sendCrc) && (*offset == length)) || + (sendCrc && (*offset == (length + 2U)))) { + *outputBuf = eof; + (*actOutBufSize)++; + outputBufSize--; + outputBuf++; + } else { + return RFAL_ERR_AGAIN; + } + + return err; +} + +ReturnCode rfalIso15693VICCDecode(const uint8_t* inBuf, uint16_t inBufLen, + uint8_t* outBuf, uint16_t outBufLen, + uint16_t* outBufPos, uint16_t* bitsBeforeCol, + uint16_t ignoreBits, bool picopassMode) { + ReturnCode err = RFAL_ERR_NONE; + uint16_t crc; + uint16_t mp; /* Current bit position in manchester bit inBuf*/ + uint16_t bp; /* Current bit position in outBuf */ + + *bitsBeforeCol = 0; + *outBufPos = 0; + + /* first check for valid SOF. Since it starts with 3 unmodulated pulses it is + * 0x17. */ + if ((inBuf[0] & 0x1fU) != 0x17U) { + ISO_15693_DEBUG("0x%x\n", iso15693PhyBitBuffer[0]); + return RFAL_ERR_FRAMING; + } + ISO_15693_DEBUG("SOF\n"); + + if (outBufLen == 0U) { + return RFAL_ERR_NONE; + } + + mp = 5; /* 5 bits were SOF, now manchester starts: 2 bits per payload bit */ + bp = 0; + + RFAL_MEMSET(outBuf, 0, outBufLen); + + if (inBufLen == 0U) { + return RFAL_ERR_CRC; + } + + for (; mp < ((inBufLen * 8U) - 2U); mp += 2U) { + bool isEOF = false; + + uint8_t man; + man = (inBuf[mp / 8U] >> (mp % 8U)) & 0x1U; + man |= ((inBuf[(mp + 1U) / 8U] >> ((mp + 1U) % 8U)) & 0x1U) << 1; + if (1U == man) { + bp++; + } + if (2U == man) { + outBuf[bp / 8U] = + (uint8_t)(outBuf[bp / 8U] | (1U << (bp % 8U))); /* MISRA 10.3 */ + bp++; + } + if ((bp % 8U) == 0U) { /* Check for EOF */ + ISO_15693_DEBUG("ceof %hhx %hhx\n", inBuf[mp / 8U], inBuf[mp / 8 + 1]); + if (((inBuf[mp / 8U] & 0xe0U) == 0xa0U) && + (inBuf[(mp / 8U) + 1U] == + 0x03U)) { /* Now we know that it was 10111000 = EOF */ + ISO_15693_DEBUG("EOF\n"); + isEOF = true; + } + } + if (((0U == man) || (3U == man)) && (!isEOF)) { + if (bp >= ignoreBits) { + err = RFAL_ERR_RF_COLLISION; + } else { + /* ignored collision: leave as 0 */ + bp++; + } + } + if ((bp >= (outBufLen * 8U)) || (err == RFAL_ERR_RF_COLLISION) || + isEOF) { /* Don't write beyond the end */ + break; + } + } + + *outBufPos = (bp / 8U); + *bitsBeforeCol = bp; + + if (err != RFAL_ERR_NONE) { + return err; + } + + if ((bp % 8U) != 0U) { + return RFAL_ERR_CRC; + } + + if (*outBufPos > 2U) { + /* finally, check crc */ + ISO_15693_DEBUG("Calculate CRC, val: 0x%x, outBufLen: ", *outBuf); + ISO_15693_DEBUG("0x%x ", *outBufPos - 2); + + crc = rfalCrcCalculateCcitt(((picopassMode) ? 0xE012U : 0xFFFFU), outBuf, + *outBufPos - 2U); + crc = (uint16_t)((picopassMode) ? crc : ~crc); + + if (((crc & 0xffU) == outBuf[*outBufPos - 2U]) && + (((crc >> 8U) & 0xffU) == outBuf[*outBufPos - 1U])) { + err = RFAL_ERR_NONE; + ISO_15693_DEBUG("OK\n"); + } else { + ISO_15693_DEBUG("error! Expected: 0x%x, got ", crc); + ISO_15693_DEBUG("0x%hhx 0x%hhx\n", outBuf[*outBufPos - 2], + outBuf[*outBufPos - 1]); + err = RFAL_ERR_CRC; + } + } else { + err = RFAL_ERR_CRC; + } + + return err; +} + +/* +****************************************************************************** +* LOCAL FUNCTIONS +****************************************************************************** +*/ +/*! + ***************************************************************************** + * \brief Perform 1 of 4 coding and send coded data + * + * This function takes \a length bytes from \a buffer, perform 1 of 4 coding + * (see ISO15693-2 specification) and sends the data using stream mode. + * + * \param[in] sendSof : send SOF prior to data. + * \param[in] buffer : data to send. + * \param[in] length : number of bytes to send. + * + * \return RFAL_ERR_IO : Error during communication. + * \return RFAL_ERR_NONE : No error. + * + ***************************************************************************** + */ +static ReturnCode rfalIso15693PhyVCDCode1Of4(const uint8_t data, + uint8_t* outbuffer, + uint16_t maxOutBufLen, + uint16_t* outBufLen) { + uint8_t tmp; + ReturnCode err = RFAL_ERR_NONE; + uint16_t a; + uint8_t* outbuf = outbuffer; + + *outBufLen = 0; + + if (maxOutBufLen < 4U) { + return RFAL_ERR_NOMEM; + } + + tmp = data; + for (a = 0; a < 4U; a++) { + switch (tmp & 0x3U) { + case 0: + *outbuf = ISO15693_DAT_00_1_4; + break; + case 1: + *outbuf = ISO15693_DAT_01_1_4; + break; + case 2: + *outbuf = ISO15693_DAT_10_1_4; + break; + case 3: + *outbuf = ISO15693_DAT_11_1_4; + break; + default: + /* MISRA 16.4: mandatory default statement */ + break; + } + outbuf++; + (*outBufLen)++; + tmp >>= 2; + } + return err; +} + +/*! + ***************************************************************************** + * \brief Perform 1 of 256 coding and send coded data + * + * This function takes \a length bytes from \a buffer, perform 1 of 256 coding + * (see ISO15693-2 specification) and sends the data using stream mode. + * \note This function sends SOF prior to the data. + * + * \param[in] sendSof : send SOF prior to data. + * \param[in] buffer : data to send. + * \param[in] length : number of bytes to send. + * + * \return RFAL_ERR_IO : Error during communication. + * \return RFAL_ERR_NONE : No error. + * + ***************************************************************************** + */ +static ReturnCode rfalIso15693PhyVCDCode1Of256(const uint8_t data, + uint8_t* outbuffer, + uint16_t maxOutBufLen, + uint16_t* outBufLen) { + uint8_t tmp; + ReturnCode err = RFAL_ERR_NONE; + uint16_t a; + uint8_t* outbuf = outbuffer; + + *outBufLen = 0; + + if (maxOutBufLen < 64U) { + return RFAL_ERR_NOMEM; + } + + tmp = data; + for (a = 0; a < 64U; a++) { + switch (tmp) { + case 0: + *outbuf = ISO15693_DAT_SLOT0_1_256; + break; + case 1: + *outbuf = ISO15693_DAT_SLOT1_1_256; + break; + case 2: + *outbuf = ISO15693_DAT_SLOT2_1_256; + break; + case 3: + *outbuf = ISO15693_DAT_SLOT3_1_256; + break; + default: + *outbuf = 0; + break; + } + outbuf++; + (*outBufLen)++; + tmp -= 4U; /* PRQA S 2911 # CERT INT30 - Intentional underflow, part of the + coding */ + } + + return err; +} + +#endif /* RFAL_FEATURE_NFCV */ diff --git a/core/embed/io/nfc/rfal/source/rfal_iso15693_2.h b/core/embed/io/nfc/rfal/source/rfal_iso15693_2.h new file mode 100644 index 0000000000..f1e45385cb --- /dev/null +++ b/core/embed/io/nfc/rfal/source/rfal_iso15693_2.h @@ -0,0 +1,200 @@ + +/****************************************************************************** + * @attention + * + * COPYRIGHT 2016 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/* + * PROJECT: ST25R391x firmware + * Revision: + * LANGUAGE: ISO C99 + */ + +/*! \file rfal_iso15693_2.h + * + * \author Ulrich Herrmann + * + * \brief Implementation of ISO-15693-2 + * + */ +/*! + * + */ + +#ifndef RFAL_ISO_15693_2_H +#define RFAL_ISO_15693_2_H + +/* +****************************************************************************** +* INCLUDES +****************************************************************************** +*/ +#include "rfal_platform.h" +#include "rfal_utils.h" + +/* +****************************************************************************** +* GLOBAL DATATYPES +****************************************************************************** +*/ +/*! Enum holding possible VCD codings */ +typedef enum { + ISO15693_VCD_CODING_1_4, + ISO15693_VCD_CODING_1_256 +} rfalIso15693VcdCoding_t; + +/*! Enum holding possible VICC datarates */ + +/*! Configuration parameter used by rfalIso15693PhyConfigure */ +typedef struct { + rfalIso15693VcdCoding_t coding; /*!< desired VCD coding */ + uint32_t speedMode; /*!< 0: normal mode, 1: 2^1 = x2 Fast mode, 2 : 2^2 = x4 + mode, 3 : 2^3 = x8 mode - all rx pulse numbers and + times are divided by 1,2,4,8 */ +} rfalIso15693PhyConfig_t; + +/*! Parameters how the stream mode should work */ +struct iso15693StreamConfig { + uint8_t useBPSK; /*!< 0: subcarrier, 1:BPSK */ + uint8_t din; /*!< the divider for the in subcarrier frequency: fc/2^din */ + uint8_t dout; /*!< the divider for the in subcarrier frequency fc/2^dout */ + uint8_t report_period_length; /*!< the length of the reporting period + 2^report_period_length*/ +}; +/* +****************************************************************************** +* GLOBAL CONSTANTS +****************************************************************************** +*/ + +#define ISO15693_REQ_FLAG_TWO_SUBCARRIERS \ + 0x01U /*!< Flag indication that communication uses two subcarriers */ +#define ISO15693_REQ_FLAG_HIGH_DATARATE \ + 0x02U /*!< Flag indication that communication uses high bitrate */ +#define ISO15693_MASK_FDT_LISTEN \ + (65) /*!< t1min = 308,2us = 4192/fc = 65.5 * 64/fc */ + +/*! t1max = 323,3us = 4384/fc = 68.5 * 64/fc + * 12 = 768/fc unmodulated time of single subcarrior SoF */ +#define ISO15693_FWT (69 + 12) + +/* +****************************************************************************** +* GLOBAL FUNCTION PROTOTYPES +****************************************************************************** +*/ +/*! + ***************************************************************************** + * \brief Initialize the ISO15693 phy + * + * \param[in] config : ISO15693 phy related configuration (See + *rfalIso15693PhyConfig_t) \param[out] needed_stream_config : return a pointer + *to the stream config needed for this iso15693 config. To be used for configure + *RF chip. + * + * \return RFAL_ERR_IO : Error during communication. + * \return RFAL_ERR_NONE : No error. + * + ***************************************************************************** + */ +extern ReturnCode rfalIso15693PhyConfigure( + const rfalIso15693PhyConfig_t* config, + const struct iso15693StreamConfig** needed_stream_config); + +/*! + ***************************************************************************** + * \brief Return current phy configuration + * + * This function returns current Phy configuration previously + * set by rfalIso15693PhyConfigure + * + * \param[out] config : ISO15693 phy configuration. + * + * \return RFAL_ERR_NONE : No error. + * + ***************************************************************************** + */ +extern ReturnCode rfalIso15693PhyGetConfiguration( + rfalIso15693PhyConfig_t* config); + +/*! + ***************************************************************************** + * \brief Code an ISO15693 compatible frame + * + * This function takes \a length bytes from \a buffer, perform proper + * encoding and sends out the frame to the ST25R391x. + * + * \param[in] buffer : data to send, modified to adapt flags. + * \param[in] length : number of bytes to send. + * \param[in] sendCrc : If set to true, CRC is appended to the frame + * \param[in] sendFlags : If set to true, flag field is sent according to + * ISO15693. + * \param[in] picopassMode : If set to true, the coding will be according to + Picopass + * \param[out] subbit_total_length : Return the complete bytes which need to + * be send for the current coding + * \param[in,out] offset : Set to 0 for first transfer, function will + update it to point to next byte to be coded + * \param[out] outbuf : buffer where the function will store the coded + subbit stream + * \param[out] outBufSize : the size of the output buffer + * \param[out] actOutBufSize : the amount of data stored into the buffer at + this call + * + * \return RFAL_ERR_IO : Error during communication. + * \return RFAL_ERR_AGAIN : Data was not coded all the way. Call function + again with a new/emptied buffer + * \return RFAL_ERR_NO_MEM : In case outBuf is not big enough. Needs to have at + least 5 bytes for 1of4 coding and 65 bytes for + 1of256 coding + * \return RFAL_ERR_NONE : No error + * + ***************************************************************************** + */ +extern ReturnCode rfalIso15693VCDCode( + uint8_t* buffer, uint16_t length, bool sendCrc, bool sendFlags, + bool picopassMode, uint16_t* subbit_total_length, uint16_t* offset, + uint8_t* outbuf, uint16_t outBufSize, uint16_t* actOutBufSize); + +/*! + ***************************************************************************** + * \brief Receive an ISO15693 compatible frame + * + * This function receives an ISO15693 frame from the ST25R391x, decodes the + *frame and writes the raw data to \a buffer. \note Buffer needs to be big + *enough to hold CRC also (+2 bytes) + * + * \param[in] inBuf : buffer with the hamming coded stream to be decoded + * \param[in] inBufLen : number of bytes to decode (=length of buffer). + * \param[out] outBuf : buffer where received data shall be written to. + * \param[in] outBufLen : Length of output buffer, should be approx twice + *the size of inBuf \param[out] outBufPos : The number of decoded bytes. Could + *be used in extended implementation to allow multiple calls \param[out] + *bitsBeforeCol : in case of RFAL_ERR_RF_COLLISION this value holds the number + *of bits in the current byte where the collision happened \param[in] ignoreBits + *: number of bits in the beginning where collisions will be ignored \param[in] + *picopassMode : if set to true, the decoding will be according to Picopass + * + * \return RFAL_ERR_RF_COLLISION : collision occured, data uncorrect + * \return RFAL_ERR_CRC : CRC error, data uncorrect + * \return RFAL_ERR_TIMEOUT : timeout waiting for data. + * \return RFAL_ERR_NONE : No error + * + ***************************************************************************** + */ +extern ReturnCode rfalIso15693VICCDecode( + const uint8_t* inBuf, uint16_t inBufLen, uint8_t* outBuf, + uint16_t outBufLen, uint16_t* outBufPos, uint16_t* bitsBeforeCol, + uint16_t ignoreBits, bool picopassMode); + +#endif /* RFAL_ISO_15693_2_H */ diff --git a/core/embed/io/nfc/rfal/source/rfal_isoDep.c b/core/embed/io/nfc/rfal/source/rfal_isoDep.c new file mode 100644 index 0000000000..0d9c2ee051 --- /dev/null +++ b/core/embed/io/nfc/rfal/source/rfal_isoDep.c @@ -0,0 +1,3188 @@ + +/****************************************************************************** + * @attention + * + * COPYRIGHT 2016 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/* + * PROJECT: NFCC firmware + * LANGUAGE: ISO C99 + */ + +/*! \file rfal_isoDep.c + * + * \author Gustavo Patricio + * + * \brief Implementation of ISO-DEP protocol + * + * This implementation was based on the following specs: + * - ISO/IEC 14443-4 2nd Edition 2008-07-15 + * - NFC Forum Digital Protocol 1.1 2014-01-14 + * + */ + +/* + ****************************************************************************** + * INCLUDES + ****************************************************************************** + */ + +#include "rfal_isoDep.h" +#include "rfal_rf.h" +#include "rfal_utils.h" + +/* + ****************************************************************************** + * ENABLE SWITCH + ****************************************************************************** + */ + +/* Feature switch may be enabled or disabled by user at rfal_platform.h + * Default configuration (ST25R dependant) also provided at rfal_defConfig.h + * + * RFAL_FEATURE_ISO_DEP + */ + +#if RFAL_FEATURE_ISO_DEP + +#if (!RFAL_FEATURE_ISO_DEP_POLL && !RFAL_FEATURE_ISO_DEP_LISTEN) +#error \ + " RFAL: Invalid ISO-DEP Configuration. Please select at least one mode: Poller and/or Listener. " +#endif + +/* Check for valid I-Block length [RFAL_ISODEP_FSX_16 ; RFAL_ISODEP_FSX_4096]*/ +#if ((RFAL_FEATURE_ISO_DEP_IBLOCK_MAX_LEN > 4096) || \ + (RFAL_FEATURE_ISO_DEP_IBLOCK_MAX_LEN < 16)) +#error \ + " RFAL: Invalid ISO-DEP IBlock Max length. Please change RFAL_FEATURE_ISO_DEP_IBLOCK_MAX_LEN. " +#endif + +/* Check for valid APDU length. */ +#if ((RFAL_FEATURE_ISO_DEP_APDU_MAX_LEN < RFAL_FEATURE_ISO_DEP_IBLOCK_MAX_LEN)) +#error \ + " RFAL: Invalid ISO-DEP APDU Max length. Please change RFAL_FEATURE_ISO_DEP_APDU_MAX_LEN. " +#endif + +/* + ****************************************************************************** + * DEFINES + ****************************************************************************** + */ +#define ISODEP_CRC_LEN RFAL_CRC_LEN /*!< ISO1443 CRC Length */ + +#define ISODEP_PCB_POS (0U) /*!< PCB position on message header*/ +#define ISODEP_SWTX_INF_POS (1U) /*!< INF position in a S-WTX */ + +#define ISODEP_DID_POS (1U) /*!< DID position on message header*/ +#define ISODEP_SWTX_PARAM_LEN (1U) /*!< SWTX parameter length */ + +#define ISODEP_DSL_MAX_LEN \ + (RFAL_ISODEP_PCB_LEN + RFAL_ISODEP_DID_LEN) /*!< Deselect Req/Res length */ + +#define ISODEP_PCB_xBLOCK_MASK (0xC0U) /*!< Bit mask for Block type */ +#define ISODEP_PCB_IBLOCK (0x00U) /*!< Bit mask indicating a I-Block */ +#define ISODEP_PCB_RBLOCK (0x80U) /*!< Bit mask indicating a R-Block */ +#define ISODEP_PCB_SBLOCK (0xC0U) /*!< Bit mask indicating a S-Block */ +#define ISODEP_PCB_INVALID (0x40U) /*!< Bit mask of an Invalid PCB */ + +#define ISODEP_HDR_MAX_LEN \ + (RFAL_ISODEP_PCB_LEN + RFAL_ISODEP_DID_LEN + \ + RFAL_ISODEP_NAD_LEN) /*!< Max header length (PCB + DID + NAD) */ + +#define ISODEP_PCB_IB_VALID_MASK \ + (ISODEP_PCB_B6_BIT | \ + ISODEP_PCB_B2_BIT) /*!< Bit mask for the MUST bits on I-Block */ +#define ISODEP_PCB_IB_VALID_VAL \ + (ISODEP_PCB_B2_BIT) /*!< Value for the MUST bits on I-Block */ +#define ISODEP_PCB_RB_VALID_MASK \ + (ISODEP_PCB_B6_BIT | ISODEP_PCB_B3_BIT | \ + ISODEP_PCB_B2_BIT) /*!< Bit mask for the MUST bits on R-Block */ +#define ISODEP_PCB_RB_VALID_VAL \ + (ISODEP_PCB_B6_BIT | \ + ISODEP_PCB_B2_BIT) /*!< Value for the MUST bits on R-Block */ +#define ISODEP_PCB_SB_VALID_MASK \ + (ISODEP_PCB_B3_BIT | ISODEP_PCB_B2_BIT | \ + ISODEP_PCB_B1_BIT) /*!< Bit mask for the MUST bits on I-Block */ +#define ISODEP_PCB_SB_VALID_VAL \ + (ISODEP_PCB_B2_BIT) /*!< Value for the MUST bits on I-Block */ + +#define ISODEP_PCB_B1_BIT (0x01U) /*!< Bit mask for the RFU S Blocks */ +#define ISODEP_PCB_B2_BIT \ + (0x02U) /*!< Bit mask for the RFU bit2 in I,S,R Blocks */ +#define ISODEP_PCB_B3_BIT \ + (0x04U) /*!< Bit mask for the RFU bit3 in R Blocks \ + */ +#define ISODEP_PCB_B6_BIT \ + (0x20U) /*!< Bit mask for the RFU bit2 in R Blocks \ + */ +#define ISODEP_PCB_CHAINING_BIT \ + (0x10U) /*!< Bit mask for the chaining bit of an ISO DEP I-Block in PCB. */ +#define ISODEP_PCB_DID_BIT \ + (0x08U) /*!< Bit mask for the DID presence bit of an ISO DEP I,S,R Blocks \ + PCB. */ +#define ISODEP_PCB_NAD_BIT \ + (0x04U) /*!< Bit mask for the NAD presence bit of an ISO DEP I,S,R Blocks in \ + PCB */ +#define ISODEP_PCB_BN_MASK \ + (0x01U) /*!< Bit mask for the block number of an ISO DEP I,R Block in PCB */ + +#define ISODEP_SWTX_PL_MASK \ + (0xC0U) /*!< Bit mask for the Power Level bits of the inf byte of an WTX \ + request or response */ +#define ISODEP_SWTX_WTXM_MASK \ + (0x3FU) /*!< Bit mask for the WTXM bits of the inf byte of an WTX request or \ + response */ + +#define ISODEP_RBLOCK_INF_LEN \ + (0U) /*!< INF length of R-Block Digital 1.1 15.1.3 */ +#define ISODEP_SDSL_INF_LEN \ + (0U) /*!< INF length of S(DSL) Digital 1.1 15.1.3 */ +#define ISODEP_SWTX_INF_LEN \ + (1U) /*!< INF length of S(WTX) Digital 1.1 15.2.2 */ + +#define ISODEP_WTXM_MIN \ + (1U) /*!< Minimum allowed value for the WTXM, Digital 1.0 13.2.2 */ +#define ISODEP_WTXM_MAX \ + (59U) /*!< Maximum allowed value for the WTXM, Digital 1.0 13.2.2 */ + +#define ISODEP_PCB_Sxx_MASK \ + (0x30U) /*!< Bit mask for the S-Block type */ +#define ISODEP_PCB_DESELECT \ + (0x00U) /*!< Bit mask for S-Block indicating Deselect */ +#define ISODEP_PCB_WTX \ + (0x30U) /*!< Bit mask for S-Block indicating Waiting Time eXtension */ + +#define ISODEP_PCB_Rx_MASK (0x10U) /*!< Bit mask for the R-Block type */ +#define ISODEP_PCB_ACK (0x00U) /*!< Bit mask for R-Block indicating ACK */ +#define ISODEP_PCB_NAK (0x10U) /*!< Bit mask for R-Block indicating NAK */ + +/*! Maximum length of control message (no INF) */ +#define ISODEP_CONTROLMSG_BUF_LEN \ + (RFAL_ISODEP_PCB_LEN + RFAL_ISODEP_DID_LEN + RFAL_ISODEP_NAD_LEN + \ + ISODEP_SWTX_PARAM_LEN) + +#define ISODEP_FWT_DEACTIVATION \ + (71680U) /*!< FWT used for DESELECT Digital 2.2 B10 ISO1444-4 7.2 & 8.1 */ +#define ISODEP_MAX_RERUNS \ + (0x0FFFFFFFU) /*!< Maximum rerun retrys for a blocking protocol run*/ + +#define ISODEP_PCBSBLOCK \ + (0x00U | ISODEP_PCB_SBLOCK | \ + ISODEP_PCB_B2_BIT) /*!< PCB Value of a S-Block */ +#define ISODEP_PCB_SDSL \ + (ISODEP_PCBSBLOCK | \ + ISODEP_PCB_DESELECT) /*!< PCB Value of a S-Block with DESELECT */ +#define ISODEP_PCB_SWTX \ + (ISODEP_PCBSBLOCK | ISODEP_PCB_WTX) /*!< PCB Value of a S-Block with WTX */ +#define ISODEP_PCB_SPARAMETERS \ + (ISODEP_PCB_SBLOCK | \ + ISODEP_PCB_WTX) /*!< PCB Value of a S-Block with PARAMETERS */ + +#define ISODEP_FWI_LIS_MAX_NFC \ + 8U /*!< FWT Listener Max FWIT4ATmax FWIBmax Digital 1.1 A6 & A3 */ +#define ISODEP_FWI_LIS_MAX_EMVCO \ + 7U /*!< FWT Listener Max FWIMAX EMVCo 2.6 A.5 */ +#define ISODEP_FWI_LIS_MAX \ + (uint8_t)( \ + (gIsoDep.compMode == RFAL_COMPLIANCE_MODE_EMV) \ + ? ISODEP_FWI_LIS_MAX_EMVCO \ + : ISODEP_FWI_LIS_MAX_NFC) /*!< FWI Listener Max as NFC / EMVCo */ +#define ISODEP_FWT_LIS_MAX \ + rfalIsoDepFWI2FWT(ISODEP_FWI_LIS_MAX) /*!< FWT Listener Max */ + +#define ISODEP_FWI_MIN_10 \ + (1U) /*!< Minimum value for FWI Digital 1.0 11.6.2.17 */ +#define ISODEP_FWI_MIN_11 \ + (0U) /*!< Default value for FWI Digital 1.1 13.6.2 */ +#define ISODEP_FWI_MAX \ + (14U) /*!< Maximum value for FWI Digital 1.0 11.6.2.17 \ + */ +#define ISODEP_SFGI_MIN \ + (0U) /*!< Default value for FWI Digital 1.1 13.6.2.22 \ + */ +#define ISODEP_SFGI_MAX \ + (14U) /*!< Maximum value for FWI Digital 1.1 13.6.2.22 */ + +#define RFAL_ISODEP_SPARAM_TVL_HDR_LEN \ + (2U) /*!< S(PARAMETERS) TVL header length: Tag + Len */ +#define RFAL_ISODEP_SPARAM_HDR_LEN \ + (RFAL_ISODEP_PCB_LEN + \ + RFAL_ISODEP_SPARAM_TVL_HDR_LEN) /*!< S(PARAMETERS) header length: PCB + Tag \ + + Len */ + +/**********************************************************************************************************************/ +/**********************************************************************************************************************/ +#define RFAL_ISODEP_NO_PARAM \ + (0U) /*!< No parameter flag for rfalIsoDepHandleControlMsg() */ + +#define RFAL_ISODEP_CMD_RATS \ + (0xE0U) /*!< RATS command Digital 1.1 13.6.1 */ + +#define RFAL_ISODEP_ATS_MIN_LEN \ + (1U) /*!< Minimum ATS length Digital 1.1 13.6.2 */ +#define RFAL_ISODEP_ATS_HDR_LEN \ + (5U) /*!< ATS headerlength Digital 1.1 13.6.2 */ +#define RFAL_ISODEP_ATS_MAX_LEN \ + (RFAL_ISODEP_ATS_HDR_LEN + \ + RFAL_ISODEP_ATS_HB_MAX_LEN) /*!< Maximum ATS length Digital 1.1 13.6.2 \ + */ +#define RFAL_ISODEP_ATS_T0_FSCI_MASK \ + (0x0FU) /*!< ATS T0's FSCI mask Digital 1.1 13.6.2 */ +#define RFAL_ISODEP_ATS_TB_FWI_SHIFT \ + (4U) /*!< ATS TB's FWI shift Digital 1.1 13.6.2 */ +#define RFAL_ISODEP_ATS_FWI_MASK \ + (0x0FU) /*!< ATS TB's FWI shift Digital 1.1 13.6.2 */ +#define RFAL_ISODEP_ATS_TL_POS \ + (0x00U) /*!< ATS TL's position Digital 1.1 13.6.2 */ + +#define RFAL_ISODEP_PPS_SB \ + (0xD0U) /*!< PPS REQ PPSS's SB value (no CID) ISO14443-4 5.3 */ +#define RFAL_ISODEP_PPS_MASK \ + (0xF0U) /*!< PPS REQ PPSS's SB mask ISO14443-4 5.3 */ +#define RFAL_ISODEP_PPS_SB_DID_MASK \ + (0x0FU) /*!< PPS REQ PPSS's DID|CID mask ISO14443-4 5.3 */ +#define RFAL_ISODEP_PPS_PPS0_PPS1_PRESENT \ + (0x11U) /*!< PPS REQ PPS0 indicating that PPS1 is present */ +#define RFAL_ISODEP_PPS_PPS1 \ + (0x00U) /*!< PPS REQ PPS1 fixed value ISO14443-4 5.3 */ +#define RFAL_ISODEP_PPS_PPS1_DSI_SHIFT \ + (2U) /*!< PPS REQ PPS1 fixed value ISO14443-4 5.3 */ +#define RFAL_ISODEP_PPS_PPS1_DXI_MASK \ + (0x0FU) /*!< PPS REQ PPS1 fixed value ISO14443-4 5.3 */ +#define RFAL_ISODEP_PPS_RES_LEN \ + (1U) /*!< PPS Response length ISO14443-4 5.4 */ +#define RFAL_ISODEP_PPS_STARTBYTE_POS \ + (0U) /*!< PPS REQ PPSS's byte position ISO14443-4 5.4 */ +#define RFAL_ISODEP_PPS_PPS0_POS \ + (1U) /*!< PPS REQ PPS0's byte position ISO14443-4 5.4 */ +#define RFAL_ISODEP_PPS_PPS1_POS \ + (2U) /*!< PPS REQ PPS1's byte position ISO14443-4 5.4 */ +#define RFAL_ISODEP_PPS0_VALID_MASK \ + (0xEFU) /*!< PPS REQ PPS0 valid coding mask ISO14443-4 5.4 */ + +#define RFAL_ISODEP_CMD_ATTRIB \ + (0x1DU) /*!< ATTRIB command Digital 1.1 14.6.1 */ +#define RFAL_ISODEP_ATTRIB_PARAM2_DSI_SHIFT \ + (6U) /*!< ATTRIB PARAM2 DSI shift Digital 1.1 14.6.1 */ +#define RFAL_ISODEP_ATTRIB_PARAM2_DRI_SHIFT \ + (4U) /*!< ATTRIB PARAM2 DRI shift Digital 1.1 14.6.1 */ +#define RFAL_ISODEP_ATTRIB_PARAM2_DXI_MASK \ + (0xF0U) /*!< ATTRIB PARAM2 DxI mask Digital 1.1 14.6.1 */ +#define RFAL_ISODEP_ATTRIB_PARAM2_FSDI_MASK \ + (0x0FU) /*!< ATTRIB PARAM2 FSDI mask Digital 1.1 14.6.1 */ +#define RFAL_ISODEP_ATTRIB_PARAM4_DID_MASK \ + (0x0FU) /*!< ATTRIB PARAM4 DID mask Digital 1.1 14.6.1 */ +#define RFAL_ISODEP_ATTRIB_HDR_LEN \ + (9U) /*!< ATTRIB REQ header length Digital 1.1 14.6.1 */ + +#define RFAL_ISODEP_ATTRIB_RES_HDR_LEN \ + (1U) /*!< ATTRIB RES header length Digital 1.1 14.6.2 */ +#define RFAL_ISODEP_ATTRIB_RES_MBLIDID_POS \ + (0U) /*!< ATTRIB RES MBLI|DID position Digital 1.1 14.6.2 */ +#define RFAL_ISODEP_ATTRIB_RES_DID_MASK \ + (0x0FU) /*!< ATTRIB RES DID mask Digital 1.1 14.6.2 */ +#define RFAL_ISODEP_ATTRIB_RES_MBLI_MASK \ + (0x0FU) /*!< ATTRIB RES MBLI mask Digital 1.1 14.6.2 */ +#define RFAL_ISODEP_ATTRIB_RES_MBLI_SHIFT \ + (4U) /*!< ATTRIB RES MBLI shift Digital 1.1 14.6.2 */ + +#define RFAL_ISODEP_DID_MASK \ + (0x0FU) /*!< ISODEP's DID mask */ +#define RFAL_ISODEP_DID_00 \ + (0U) /*!< ISODEP's DID value 0 */ + +#define RFAL_ISODEP_FSDI_MAX_NFC \ + (8U) /*!< Max FSDI value Digital 2.0 14.6.1.9 & B7 & B8 */ +#define RFAL_ISODEP_FSDI_MAX_NFC_21 \ + (0x0CU) /*!< Max FSDI value Digital 2.1 14.6.1.9 & Table 72 */ +#define RFAL_ISODEP_FSDI_MAX_EMV \ + (0x0CU) /*!< Max FSDI value EMVCo 3.0 5.7.2.5 */ + +#define RFAL_ISODEP_RATS_PARAM_FSDI_MASK \ + (0xF0U) /*!< Mask bits for FSDI in RATS */ +#define RFAL_ISODEP_RATS_PARAM_FSDI_SHIFT \ + (4U) /*!< Shift for FSDI in RATS */ +#define RFAL_ISODEP_RATS_PARAM_DID_MASK \ + (0x0FU) /*!< Mask bits for DID in RATS */ + +#define RFAL_ISODEP_ATS_TL_OFFSET \ + (0x00U) /*!< Offset of TL on ATS */ +#define RFAL_ISODEP_ATS_TA_OFFSET \ + (0x02U) /*!< Offset of TA if it is present on ATS */ +#define RFAL_ISODEP_ATS_TB_OFFSET \ + (0x03U) /*!< Offset of TB if both TA and TB is present on ATS */ +#define RFAL_ISODEP_ATS_TC_OFFSET \ + (0x04U) /*!< Offset of TC if both TA,TB & TC are present on ATS */ +#define RFAL_ISODEP_ATS_HIST_OFFSET \ + (0x05U) /*!< Offset of Historical Bytes if TA, TB & TC are present on ATS */ +#define RFAL_ISODEP_ATS_TC_ADV_FEAT \ + (0x10U) /*!< Bit mask indicating support for Advanced protocol features: DID \ + & NAD */ +#define RFAL_ISODEP_ATS_TC_DID \ + (0x02U) /*!< Bit mask indicating support for DID */ +#define RFAL_ISODEP_ATS_TC_NAD \ + (0x01U) /*!< Bit mask indicating support for NAD */ + +#define RFAL_ISODEP_PPS0_PPS1_PRESENT \ + (0x11U) /*!< PPS0 byte indicating that PPS1 is present */ +#define RFAL_ISODEP_PPS0_PPS1_NOT_PRESENT \ + (0x01U) /*!< PPS0 byte indicating that PPS1 is NOT present */ +#define RFAL_ISODEP_PPS1_DRI_MASK \ + (0x03U) /*!< PPS1 byte DRI mask bits */ +#define RFAL_ISODEP_PPS1_DSI_MASK \ + (0x0CU) /*!< PPS1 byte DSI mask bits */ +#define RFAL_ISODEP_PPS1_DSI_SHIFT \ + (2U) /*!< PPS1 byte DSI shift */ +#define RFAL_ISODEP_PPS1_DxI_MASK \ + (0x03U) /*!< PPS1 byte DSI/DRS mask bits */ + +/*! Delta Time for polling during Activation (ATS) : 20ms Digital 1.0 11.7.1.1 & + * A.7 */ +#define RFAL_ISODEP_T4T_DTIME_POLL_10 rfalConvMsTo1fc(20) + +/*! Delta Time for polling during Activation (ATS) : 16.4ms Digital 1.1 13.8.1.1 + * & A.6 Use 16 ms as testcase T4AT_BI_10_03 sends a frame exactly at the border + */ +#define RFAL_ISODEP_T4T_DTIME_POLL_11 216960U + +/*! Activation frame waiting time FWT(act) = 71680/fc (~5286us) + * Digital 1.1 13.8.1.1 & A.6 */ +#define RFAL_ISODEP_T4T_FWT_ACTIVATION (71680U + RFAL_ISODEP_T4T_DTIME_POLL_11) + +/*! Delta frame waiting time = 16/fc Digital 1.0 11.7.1.3 & A.7*/ +#define RFAL_ISODEP_DFWT_10 16U + +/*! Delta frame waiting time = 16/fc Digital 2.0 14.8.1.3 & B.7*/ +#define RFAL_ISODEP_DFWT_20 49152U + +/* + ****************************************************************************** + * MACROS + ****************************************************************************** + */ + +#define rfalIsoDep_PCBisIBlock(pcb) \ + (((pcb) & (ISODEP_PCB_xBLOCK_MASK | ISODEP_PCB_IB_VALID_MASK)) == \ + (ISODEP_PCB_IBLOCK | \ + ISODEP_PCB_IB_VALID_VAL)) /*!< Checks if pcb is a I-Block */ +#define rfalIsoDep_PCBisRBlock(pcb) \ + (((pcb) & (ISODEP_PCB_xBLOCK_MASK | ISODEP_PCB_RB_VALID_MASK)) == \ + (ISODEP_PCB_RBLOCK | \ + ISODEP_PCB_RB_VALID_VAL)) /*!< Checks if pcb is a R-Block */ +#define rfalIsoDep_PCBisSBlock(pcb) \ + (((pcb) & (ISODEP_PCB_xBLOCK_MASK | ISODEP_PCB_SB_VALID_MASK)) == \ + (ISODEP_PCB_SBLOCK | \ + ISODEP_PCB_SB_VALID_VAL)) /*!< Checks if pcb is a S-Block */ + +#define rfalIsoDep_PCBisChaining(pcb) \ + (((pcb) & ISODEP_PCB_CHAINING_BIT) == \ + ISODEP_PCB_CHAINING_BIT) /*!< Checks if pcb is indicating chaining */ + +#define rfalIsoDep_PCBisDeselect(pcb) \ + (((pcb) & ISODEP_PCB_Sxx_MASK) == \ + ISODEP_PCB_DESELECT) /*!< Checks if pcb is indicating DESELECT */ +#define rfalIsoDep_PCBisWTX(pcb) \ + (((pcb) & ISODEP_PCB_Sxx_MASK) == \ + ISODEP_PCB_WTX) /*!< Checks if pcb is indicating WTX */ + +#define rfalIsoDep_PCBisACK(pcb) \ + (((pcb) & ISODEP_PCB_Rx_MASK) == \ + ISODEP_PCB_ACK) /*!< Checks if pcb is indicating ACK */ +#define rfalIsoDep_PCBisNAK(pcb) \ + (((pcb) & ISODEP_PCB_Rx_MASK) == \ + ISODEP_PCB_NAK) /*!< Checks if pcb is indicating ACK */ + +#define rfalIsoDep_PCBhasDID(pcb) \ + (((pcb) & ISODEP_PCB_DID_BIT) == \ + ISODEP_PCB_DID_BIT) /*!< Checks if pcb is indicating DID */ +#define rfalIsoDep_PCBhasNAD(pcb) \ + (((pcb) & ISODEP_PCB_NAD_BIT) == \ + ISODEP_PCB_NAD_BIT) /*!< Checks if pcb is indicating NAD */ + +#define rfalIsoDep_PCBisIChaining(pcb) \ + (rfalIsoDep_PCBisIBlock(pcb) && \ + rfalIsoDep_PCBisChaining( \ + pcb)) /*!< Checks if pcb is I-Block indicating chaining */ + +#define rfalIsoDep_PCBisSDeselect(pcb) \ + (rfalIsoDep_PCBisSBlock(pcb) && \ + rfalIsoDep_PCBisDeselect( \ + pcb)) /*!< Checks if pcb is S-Block indicating DESELECT */ +#define rfalIsoDep_PCBisSWTX(pcb) \ + (rfalIsoDep_PCBisSBlock(pcb) && \ + rfalIsoDep_PCBisWTX(pcb)) /*!< Checks if pcb is S-Block indicating WTX */ + +#define rfalIsoDep_PCBisRACK(pcb) \ + (rfalIsoDep_PCBisRBlock(pcb) && \ + rfalIsoDep_PCBisACK(pcb)) /*!< Checks if pcb is R-Block indicating ACK */ +#define rfalIsoDep_PCBisRNAK(pcb) \ + (rfalIsoDep_PCBisRBlock(pcb) && \ + rfalIsoDep_PCBisNAK(pcb)) /*!< Checks if pcb is R-Block indicating NAK */ + +#define rfalIsoDep_PCBIBlock(bn) \ + ((uint8_t)(0x00U | ISODEP_PCB_IBLOCK | ISODEP_PCB_B2_BIT | \ + ((bn) & ISODEP_PCB_BN_MASK))) /*!< Returns an I-Block with the \ + given block number (bn) */ +#define rfalIsoDep_PCBIBlockChaining(bn) \ + ((uint8_t)(rfalIsoDep_PCBIBlock(bn) | \ + ISODEP_PCB_CHAINING_BIT)) /*!< Returns an I-Block with the given \ + block number (bn) indicating \ + chaining */ + +#define rfalIsoDep_PCBRBlock(bn) \ + ((uint8_t)(0x00U | ISODEP_PCB_RBLOCK | ISODEP_PCB_B6_BIT | \ + ISODEP_PCB_B2_BIT | \ + ((bn) & ISODEP_PCB_BN_MASK))) /*!< Returns an R-Block with the \ + given block number (bn) */ +#define rfalIsoDep_PCBRACK(bn) \ + ((uint8_t)(rfalIsoDep_PCBRBlock(bn) | \ + ISODEP_PCB_ACK)) /*!< Returns an R-Block with the given block \ + number (bn) indicating ACK */ +#define rfalIsoDep_PCBRNAK(bn) \ + ((uint8_t)(rfalIsoDep_PCBRBlock(bn) | \ + ISODEP_PCB_NAK)) /*!< Returns an R-Block with the given block \ + number (bn) indicating NAK */ + +#define rfalIsoDep_GetBN(pcb) \ + ((uint8_t)((pcb) & ISODEP_PCB_BN_MASK)) /*!< Returns the block number (bn) \ + from the given pcb */ +#define rfalIsoDep_GetWTXM(inf) \ + ((uint8_t)((inf) & ISODEP_SWTX_WTXM_MASK)) /*!< Returns the WTX value from \ + the given inf byte */ +#define rfalIsoDep_isWTXMValid(wtxm) \ + (((wtxm) >= ISODEP_WTXM_MIN) && \ + ((wtxm) <= ISODEP_WTXM_MAX)) /*!< Checks if the given wtxm is valid */ + +#define rfalIsoDep_WTXMListenerMax(fwt) \ + (RFAL_MIN((uint8_t)(ISODEP_FWT_LIS_MAX / (fwt)), \ + ISODEP_WTXM_MAX)) /*!< Calculates the Max WTXM value for the given \ + fwt as a Listener */ + +#define rfalIsoDepCalcdSGFT(s) \ + (384U * ((uint32_t)1U << (s))) /*!< Calculates the dSFGT with given SFGI \ + Digital 1.1 13.8.2.1 & A.6*/ +#define rfalIsoDepCalcSGFT(s) \ + (4096U * \ + ((uint32_t)1U \ + << (s))) /*!< Calculates the SFGT with given SFGI Digital 1.1 13.8.2 */ + +#define rfalIsoDep_PCBNextBN(bn) \ + (((uint8_t)(bn) ^ 0x01U) & \ + ISODEP_PCB_BN_MASK) /*!< Returns the value of the next block number based \ + on bn */ +#define rfalIsoDep_PCBPrevBN(bn) \ + rfalIsoDep_PCBNextBN( \ + bn) /*!< Returns the value of the previous block number based on bn */ +#define rfalIsoDep_ToggleBN(bn) \ + ((bn) = (((bn) ^ 0x01U) & ISODEP_PCB_BN_MASK)) /*!< Toggles the block number \ + value of the given bn */ + +#define rfalIsoDep_WTXAdjust(v) \ + ((v) - ((v) >> 3)) /*!< Adjust WTX timer value to a percentage of the total, \ + current 88% */ + +/*! ISO 14443-4 7.5.6.2 & Digital 1.1 - 15.2.6.2 The CE SHALL NOT attempt error + * recovery and remains in Rx mode upon Transmission or a Protocol Error */ +#define rfalIsoDepReEnableRx(rxB, rxBL, rxL) \ + rfalTransceiveBlockingTx(NULL, 0, rxB, rxBL, rxL, RFAL_TXRX_FLAGS_DEFAULT, \ + RFAL_FWT_NONE) + +#define rfalIsoDepTimerStart(timer, time_ms) \ + do { \ + platformTimerDestroy(timer); \ + (timer) = platformTimerCreate((uint16_t)(time_ms)); \ + } while (0) /*!< Configures and starts the WTX timer */ +#define rfalIsoDepTimerisExpired(timer) \ + platformTimerIsExpired(timer) /*!< Checks WTX timer has expired */ +#define rfalIsoDepTimerDestroy(timer) \ + platformTimerDestroy(timer) /*!< Destroys WTX timer */ + +/* + ****************************************************************************** + * LOCAL DATA TYPES + ****************************************************************************** + */ + +/*! Internal structure to be used in handling of S(PARAMETRS) only */ +typedef struct { + uint8_t pcb; /*!< PCB byte */ + rfalIsoDepSParameter sParam; /*!< S(PARAMETERS) */ +} rfalIsoDepControlMsgSParam; + +/*! Enumeration of the possible control message types */ +typedef enum { + ISODEP_R_ACK, /*!< R-ACK Acknowledge */ + ISODEP_R_NAK, /*!< R-NACK Negative acknowledge */ + ISODEP_S_WTX, /*!< S-WTX Waiting Time Extension */ + ISODEP_S_DSL /*!< S-DSL Deselect */ +} rfalIsoDepControlMsg; + +/*! Enumeration of the IsoDep roles */ +typedef enum { + ISODEP_ROLE_PCD, /*!< Perform as Reader/PCD */ + ISODEP_ROLE_PICC /*!< Perform as Card/PICC */ +} rfalIsoDepRole; + +/*! ISO-DEP layer states */ +typedef enum { + ISODEP_ST_IDLE, /*!< Idle State */ + ISODEP_ST_PCD_TX, /*!< PCD Transmission State */ + ISODEP_ST_PCD_RX, /*!< PCD Reception State */ + ISODEP_ST_PCD_WAIT_DSL, /*!< PCD Wait for DSL response */ + + ISODEP_ST_PICC_ACT_ATS, /*!< PICC has replied to RATS (ATS) */ + ISODEP_ST_PICC_ACT_ATTRIB, /*!< PICC has replied to ATTRIB */ + ISODEP_ST_PICC_RX, /*!< PICC Reception State */ + ISODEP_ST_PICC_SWTX, /*!< PICC Waiting Time eXtension */ + ISODEP_ST_PICC_SDSL, /*!< PICC S(DSL) response ongoing */ + ISODEP_ST_PICC_TX, /*!< PICC Transmission State */ + + ISODEP_ST_PCD_ACT_RATS, /*!< PCD activation (RATS) */ + ISODEP_ST_PCD_ACT_PPS, /*!< PCD activation (PPS) */ + +} rfalIsoDepState; + +/*! Holds all ISO-DEP data(counters, buffers, ID, timeouts, frame size) */ +typedef struct { + rfalIsoDepState state; /*!< ISO-DEP module state */ + rfalIsoDepRole role; /*!< Current ISO-DEP role */ + + uint8_t blockNumber; /*!< Current block number */ + uint8_t did; /*!< Current DID */ + uint8_t nad; /*!< Current DID */ + uint8_t cntIRetrys; /*!< I-Block retry counter */ + uint8_t cntRRetrys; /*!< R-Block retry counter */ + uint8_t cntSDslRetrys; /*!< S(DESELECT) retry counter */ + uint8_t cntSWtxRetrys; /*!< Overall S(WTX) retry counter */ + uint8_t cntSWtxNack; /*!< R(NACK) answered with S(WTX) counter */ + uint32_t fwt; /*!< Current FWT (Frame Waiting Time) */ + uint32_t dFwt; /*!< Current delta FWT */ + uint16_t fsx; /*!< Current FSx FSC or FSD (max Frame size) */ + bool isTxChaining; /*!< Flag for chaining on Tx */ + bool isRxChaining; /*!< Flag for chaining on Rx */ + uint8_t *txBuf; /*!< Tx buffer pointer */ + uint8_t *rxBuf; /*!< Rx buffer pointer */ + uint16_t txBufLen; /*!< Tx buffer length */ + uint16_t rxBufLen; /*!< Rx buffer length */ + uint8_t txBufInfPos; /*!< Start of payload in txBuf */ + uint8_t rxBufInfPos; /*!< Start of payload in rxBuf */ + + uint16_t ourFsx; /*!< Our current FSx FSC or FSD (Frame size) */ + uint8_t lastPCB; /*!< Last PCB sent */ + uint8_t lastWTXM; /*!< Last WTXM sent */ + uint8_t atsTA; /*!< TA on ATS */ + uint8_t hdrLen; /*!< Current ISO-DEP length */ + rfalBitRate txBR; /*!< Current Tx Bit Rate */ + rfalBitRate rxBR; /*!< Current Rx Bit Rate */ + uint16_t *rxLen; /*!< Output parameter ptr to Rx length */ + bool *rxChaining; /*!< Output parameter ptr to Rx chaining flag */ + uint32_t WTXTimer; /*!< Timer used for WTX */ + bool lastDID00; /*!< Last PCD block had DID flag (for DID = 0) */ + + bool isTxPending; /*!< Flag pending Block while waiting WTX Ack */ + bool isWait4WTX; /*!< Flag for waiting WTX Ack */ + + uint8_t maxRetriesI; /*!< Number of retries for a I-Block */ + uint8_t maxRetriesR; /*!< Number of retries for a R-Block */ + uint8_t maxRetriesSDSL; /*!< Number of retries for S(DESELECT) errors */ + uint8_t maxRetriesSWTX; /*!< Number of retries for S(WTX) errors */ + uint8_t maxRetriesSnWTX; /*!< Number of retries S(WTX) replied w NACK */ + uint8_t maxRetriesRATS; /*!< Number of retries for RATS */ + + rfalComplianceMode compMode; /*!< Compliance mode */ + + uint8_t ctrlBuf[ISODEP_CONTROLMSG_BUF_LEN]; /*!< Control msg buf */ + uint16_t ctrlRxLen; /*!< Control msg rcvd len */ + + union { /* PRQA S 0750 # MISRA 19.2 - Members of the union will not be used + concurrently, only one frame at a time */ +#if RFAL_FEATURE_NFCA + rfalIsoDepRats ratsReq; + rfalIsoDepPpsReq ppsReq; +#endif /* RFAL_FEATURE_NFCA */ + +#if RFAL_FEATURE_NFCB + rfalIsoDepAttribCmd attribReq; +#endif /* RFAL_FEATURE_NFCB */ + } actv; /*!< Activation buffer */ + + uint8_t *rxLen8; /*!< Receive length (8-bit) */ + rfalIsoDepDevice *actvDev; /*!< Activation Device Info */ + rfalIsoDepListenActvParam actvParam; /*!< Listen Activation context */ + + rfalIsoDepApduTxRxParam APDUParam; /*!< APDU TxRx params */ + uint16_t APDUTxPos; /*!< APDU Tx position */ + uint16_t APDURxPos; /*!< APDU Rx position */ + bool isAPDURxChaining; /*!< APDU Transceive chaining flag */ + +} rfalIsoDep; + +/* + ****************************************************************************** + * LOCAL VARIABLES + ****************************************************************************** + */ + +static rfalIsoDep gIsoDep; /*!< ISO-DEP Module instance */ + +/* + ****************************************************************************** + * LOCAL FUNCTION PROTOTYPES + ****************************************************************************** + */ +static void rfalIsoDepClearCounters(void); +static ReturnCode rfalIsoDepTx(uint8_t pcb, const uint8_t *txBuf, + uint8_t *infBuf, uint16_t infLen, uint32_t fwt); +static ReturnCode rfalIsoDepHandleControlMsg(rfalIsoDepControlMsg controlMsg, + uint8_t param); +static void rfalIsoDepApdu2IBLockParam(rfalIsoDepApduTxRxParam apduParam, + rfalIsoDepTxRxParam *iBlockParam, + uint16_t txPos, uint16_t rxPos); + +#if RFAL_FEATURE_ISO_DEP_POLL +static ReturnCode rfalIsoDepDataExchangePCD(uint16_t *outActRxLen, + bool *outIsChaining); +static void rfalIsoDepCalcBitRate(rfalBitRate maxAllowedBR, + uint8_t piccBRCapability, rfalBitRate *dsi, + rfalBitRate *dri); +static uint32_t rfalIsoDepSFGI2SFGT(uint8_t sfgi); + +#if RFAL_FEATURE_NFCA +static ReturnCode rfalIsoDepStartRATS(rfalIsoDepFSxI FSDI, uint8_t DID, + rfalIsoDepAts *ats, uint8_t *atsLen); +static ReturnCode rfalIsoDepGetRATSStatus(void); +static ReturnCode rfalIsoDepStartPPS(uint8_t DID, rfalBitRate DSI, + rfalBitRate DRI, rfalIsoDepPpsRes *ppsRes); +static ReturnCode rfalIsoDepGetPPSSTatus(void); +#endif /* RFAL_FEATURE_NFCA */ + +#if RFAL_FEATURE_NFCB +static ReturnCode rfalIsoDepStartATTRIB(const uint8_t *nfcid0, uint8_t PARAM1, + rfalBitRate DSI, rfalBitRate DRI, + rfalIsoDepFSxI FSDI, uint8_t PARAM3, + uint8_t DID, const uint8_t *HLInfo, + uint8_t HLInfoLen, uint32_t fwt, + rfalIsoDepAttribRes *attribRes, + uint8_t *attribResLen); +static ReturnCode rfalIsoDepGetATTRIBStatus(void); +#endif /* RFAL_FEATURE_NFCB */ + +#endif /* RFAL_FEATURE_ISO_DEP_POLL */ + +#if RFAL_FEATURE_ISO_DEP_LISTEN +static ReturnCode rfalIsoDepDataExchangePICC(void); +static ReturnCode rfalIsoDepReSendControlMsg(void); +#endif + +/* + ****************************************************************************** + * LOCAL FUNCTIONS + ****************************************************************************** + */ + +/*******************************************************************************/ +static void rfalIsoDepClearCounters(void) { + gIsoDep.cntIRetrys = 0; + gIsoDep.cntRRetrys = 0; + gIsoDep.cntSDslRetrys = 0; + gIsoDep.cntSWtxRetrys = 0; + gIsoDep.cntSWtxNack = 0; +} + +/*******************************************************************************/ +static ReturnCode rfalIsoDepTx(uint8_t pcb, const uint8_t *txBuf, + uint8_t *infBuf, uint16_t infLen, uint32_t fwt) { + uint8_t *txBlock; + uint16_t txBufLen; + uint8_t computedPcb; + rfalTransceiveContext ctx; + + txBlock = infBuf; /* Point to beginning of the INF, and go backwards */ + gIsoDep.lastPCB = pcb; /* Store the last PCB sent */ + + if (infLen > 0U) { + if (((uintptr_t)infBuf - (uintptr_t)txBuf) < + gIsoDep + .hdrLen) /* Check that we can fit the header in the given space */ + { + return RFAL_ERR_NOMEM; + } + } + + /*******************************************************************************/ + /* Compute optional PCB bits */ + computedPcb = pcb; + if (((gIsoDep.did != RFAL_ISODEP_NO_DID) && + (gIsoDep.did != RFAL_ISODEP_DID_00)) || + ((gIsoDep.did == RFAL_ISODEP_DID_00) && (gIsoDep.lastDID00))) { + computedPcb |= ISODEP_PCB_DID_BIT; + } + if (gIsoDep.nad != RFAL_ISODEP_NO_NAD) { + computedPcb |= ISODEP_PCB_NAD_BIT; + } + if ((gIsoDep.isTxChaining) && (rfalIsoDep_PCBisIBlock(computedPcb))) { + computedPcb |= ISODEP_PCB_CHAINING_BIT; + } + + /*******************************************************************************/ + /* Compute Payload on the given txBuf, start by the PCB | DID | NAD | before + * INF */ + + if ((ISODEP_PCB_NAD_BIT & computedPcb) != 0U) { + *(--txBlock) = gIsoDep.nad; /* NAD is optional */ + } + + if ((ISODEP_PCB_DID_BIT & computedPcb) != 0U) { + *(--txBlock) = gIsoDep.did; /* DID is optional */ + } + + *(--txBlock) = computedPcb; /* PCB always present */ + + txBufLen = + (infLen + + (uint16_t)((uintptr_t)infBuf - + (uintptr_t)txBlock)); /* Calculate overall buffer size */ + + if (txBufLen > + (gIsoDep.fsx - ISODEP_CRC_LEN)) /* Check if msg length violates the + maximum frame size FSC */ + { + return RFAL_ERR_NOTSUPP; + } + + rfalCreateByteFlagsTxRxContext( + ctx, txBlock, txBufLen, gIsoDep.rxBuf, gIsoDep.rxBufLen, gIsoDep.rxLen, + RFAL_TXRX_FLAGS_DEFAULT, + ((gIsoDep.role == ISODEP_ROLE_PICC) ? RFAL_FWT_NONE : fwt)); + return rfalStartTransceive(&ctx); +} + +/*******************************************************************************/ +static ReturnCode rfalIsoDepHandleControlMsg(rfalIsoDepControlMsg controlMsg, + uint8_t param) { + uint8_t pcb; + uint8_t infLen; + uint32_t fwtTemp; + + infLen = 0; + fwtTemp = (gIsoDep.fwt + gIsoDep.dFwt); + RFAL_MEMSET(gIsoDep.ctrlBuf, 0x00, ISODEP_CONTROLMSG_BUF_LEN); + + switch (controlMsg) { + /*******************************************************************************/ + case ISODEP_R_ACK: + + if (gIsoDep.cntRRetrys++ > gIsoDep.maxRetriesR) { + return RFAL_ERR_TIMEOUT; /* NFC Forum mandates timeout or transmission + error depending on previous errors */ + } + + pcb = rfalIsoDep_PCBRACK(gIsoDep.blockNumber); + break; + + /*******************************************************************************/ + case ISODEP_R_NAK: + + if ((gIsoDep.cntRRetrys++ > + gIsoDep.maxRetriesR) || /* Max R Block retries reached */ + (gIsoDep.cntSWtxNack >= + gIsoDep.maxRetriesSnWTX)) /* Max number PICC is allowed to respond + with S(WTX) to R(NAK) */ + { + return RFAL_ERR_TIMEOUT; + } + + pcb = rfalIsoDep_PCBRNAK(gIsoDep.blockNumber); + break; + + /*******************************************************************************/ + case ISODEP_S_WTX: + + if ((gIsoDep.cntSWtxRetrys++ > gIsoDep.maxRetriesSWTX) && + (gIsoDep.maxRetriesSWTX != RFAL_ISODEP_MAX_WTX_RETRYS_ULTD)) { + return RFAL_ERR_PROTO; + } + + /* Check if WTXM is valid */ + if (!rfalIsoDep_isWTXMValid(param)) { + return RFAL_ERR_PROTO; + } + + if (gIsoDep.role == ISODEP_ROLE_PCD) { + /* Calculate temp Wait Time eXtension */ + fwtTemp = (gIsoDep.fwt * param); + fwtTemp = RFAL_MIN(RFAL_ISODEP_MAX_FWT, fwtTemp); + fwtTemp += gIsoDep.dFwt; + } + + pcb = ISODEP_PCB_SWTX; + gIsoDep.ctrlBuf[RFAL_ISODEP_PCB_LEN + RFAL_ISODEP_DID_LEN + infLen++] = + param; + break; + + /*******************************************************************************/ + case ISODEP_S_DSL: + + if (gIsoDep.cntSDslRetrys++ > gIsoDep.maxRetriesSDSL) { + return RFAL_ERR_TIMEOUT; /* NFC Forum mandates timeout or transmission + error depending on previous errors */ + } + + if (gIsoDep.role == ISODEP_ROLE_PCD) { + /* Digital 1.0 - 13.2.7.3 Poller must wait fwtDEACTIVATION */ + fwtTemp = ISODEP_FWT_DEACTIVATION; + gIsoDep.state = ISODEP_ST_PCD_WAIT_DSL; + } + pcb = ISODEP_PCB_SDSL; + break; + + /*******************************************************************************/ + default: + return RFAL_ERR_INTERNAL; + } + + return rfalIsoDepTx( + pcb, gIsoDep.ctrlBuf, + &gIsoDep.ctrlBuf[RFAL_ISODEP_PCB_LEN + RFAL_ISODEP_DID_LEN], infLen, + fwtTemp); +} + +#if RFAL_FEATURE_ISO_DEP_LISTEN +/*******************************************************************************/ +static ReturnCode rfalIsoDepReSendControlMsg(void) { + if (rfalIsoDep_PCBisRACK(gIsoDep.lastPCB)) { + return rfalIsoDepHandleControlMsg(ISODEP_R_ACK, RFAL_ISODEP_NO_PARAM); + } + + if (rfalIsoDep_PCBisRNAK(gIsoDep.lastPCB)) { + return rfalIsoDepHandleControlMsg(ISODEP_R_NAK, RFAL_ISODEP_NO_PARAM); + } + + if (rfalIsoDep_PCBisSDeselect(gIsoDep.lastPCB)) { + return rfalIsoDepHandleControlMsg(ISODEP_S_DSL, RFAL_ISODEP_NO_PARAM); + } + + if (rfalIsoDep_PCBisSWTX(gIsoDep.lastPCB)) { + return rfalIsoDepHandleControlMsg(ISODEP_S_WTX, gIsoDep.lastWTXM); + } + return RFAL_ERR_WRONG_STATE; +} +#endif /* RFAL_FEATURE_ISO_DEP_LISTEN */ + +/* + ****************************************************************************** + * GLOBAL FUNCTIONS + ****************************************************************************** + */ + +/*******************************************************************************/ +void rfalIsoDepInitialize(void) { + gIsoDep.state = ISODEP_ST_IDLE; + gIsoDep.role = ISODEP_ROLE_PCD; + gIsoDep.did = RFAL_ISODEP_NO_DID; + gIsoDep.nad = RFAL_ISODEP_NO_NAD; + gIsoDep.blockNumber = 0; + gIsoDep.isTxChaining = false; + gIsoDep.isRxChaining = false; + gIsoDep.lastDID00 = false; + gIsoDep.lastPCB = ISODEP_PCB_INVALID; + gIsoDep.fsx = (uint16_t)RFAL_ISODEP_FSX_16; + gIsoDep.ourFsx = (uint16_t)RFAL_ISODEP_FSX_16; + gIsoDep.hdrLen = RFAL_ISODEP_PCB_LEN; + + gIsoDep.rxLen = NULL; + gIsoDep.rxBuf = NULL; + gIsoDep.rxBufInfPos = 0U; + gIsoDep.txBufInfPos = 0U; + + gIsoDep.isTxPending = false; + gIsoDep.isWait4WTX = false; + + gIsoDep.compMode = RFAL_COMPLIANCE_MODE_NFC; + gIsoDep.maxRetriesR = RFAL_ISODEP_MAX_R_RETRYS; + gIsoDep.maxRetriesI = RFAL_ISODEP_MAX_I_RETRYS; + gIsoDep.maxRetriesSDSL = RFAL_ISODEP_MAX_DSL_RETRYS; + gIsoDep.maxRetriesSWTX = RFAL_ISODEP_MAX_WTX_RETRYS; + gIsoDep.maxRetriesSnWTX = RFAL_ISODEP_MAX_WTX_NACK_RETRYS; + gIsoDep.maxRetriesRATS = RFAL_ISODEP_RATS_RETRIES; + + gIsoDep.APDURxPos = 0; + gIsoDep.APDUTxPos = 0; + gIsoDep.APDUParam.rxLen = NULL; + gIsoDep.APDUParam.rxBuf = NULL; + gIsoDep.APDUParam.txBuf = NULL; + + rfalIsoDepClearCounters(); + + /* Destroy any ongoing WTX timer */ + rfalIsoDepTimerDestroy(gIsoDep.WTXTimer); + gIsoDep.WTXTimer = 0U; +} + +/*******************************************************************************/ +void rfalIsoDepInitializeWithParams(rfalComplianceMode compMode, + uint8_t maxRetriesR, + uint8_t maxRetriesSnWTX, + uint8_t maxRetriesSWTX, + uint8_t maxRetriesSDSL, uint8_t maxRetriesI, + uint8_t maxRetriesRATS) { + rfalIsoDepInitialize(); + + gIsoDep.compMode = compMode; + gIsoDep.maxRetriesR = maxRetriesR; + gIsoDep.maxRetriesSnWTX = maxRetriesSnWTX; + gIsoDep.maxRetriesSWTX = maxRetriesSWTX; + gIsoDep.maxRetriesSDSL = maxRetriesSDSL; + gIsoDep.maxRetriesI = maxRetriesI; + gIsoDep.maxRetriesRATS = maxRetriesRATS; +} + +#if RFAL_FEATURE_ISO_DEP_POLL +/*******************************************************************************/ +static ReturnCode rfalIsoDepDataExchangePCD(uint16_t *outActRxLen, + bool *outIsChaining) { + ReturnCode ret; + uint8_t rxPCB; + + /* Check out parameters */ + if ((outActRxLen == NULL) || (outIsChaining == NULL)) { + return RFAL_ERR_PARAM; + } + + *outIsChaining = false; + + /* Calculate header required and check if the buffers InfPositions are + * suitable */ + gIsoDep.hdrLen = RFAL_ISODEP_PCB_LEN; + if ((gIsoDep.did != RFAL_ISODEP_NO_DID) && + (gIsoDep.did != RFAL_ISODEP_DID_00)) { + gIsoDep.hdrLen += RFAL_ISODEP_DID_LEN; + } + if (gIsoDep.nad != RFAL_ISODEP_NO_NAD) { + gIsoDep.hdrLen += RFAL_ISODEP_NAD_LEN; + } + + /* Check if there is enough space before the infPos to append ISO-DEP headers + * on rx and tx */ + if ((gIsoDep.rxBufInfPos < gIsoDep.hdrLen) || + (gIsoDep.txBufInfPos < gIsoDep.hdrLen)) { + return RFAL_ERR_PARAM; + } + + /*******************************************************************************/ + switch (gIsoDep.state) { + /*******************************************************************************/ + case ISODEP_ST_IDLE: + return RFAL_ERR_NONE; + + /*******************************************************************************/ + case ISODEP_ST_PCD_TX: + ret = rfalIsoDepTx(rfalIsoDep_PCBIBlock(gIsoDep.blockNumber), + gIsoDep.txBuf, &gIsoDep.txBuf[gIsoDep.txBufInfPos], + gIsoDep.txBufLen, (gIsoDep.fwt + gIsoDep.dFwt)); + switch (ret) { + case RFAL_ERR_NONE: + gIsoDep.state = ISODEP_ST_PCD_RX; + break; + + default: + return ret; + } + /* fall through */ + + /*******************************************************************************/ + case ISODEP_ST_PCD_WAIT_DSL: /* PRQA S 2003 # MISRA 16.3 - Intentional fall + through */ + case ISODEP_ST_PCD_RX: + + ret = rfalGetTransceiveStatus(); + switch (ret) { + /* Data rcvd with error or timeout -> Send R-NAK */ + case RFAL_ERR_TIMEOUT: + case RFAL_ERR_CRC: + case RFAL_ERR_PAR: + case RFAL_ERR_FRAMING: /* added to handle test cases scenario + TC_POL_NFCB_T4AT_BI_82_x_y & + TC_POL_NFCB_T4BT_BI_82_x_y */ + case RFAL_ERR_INCOMPLETE_BYTE: /* added to handle test cases scenario + TC_POL_NFCB_T4AT_BI_82_x_y & + TC_POL_NFCB_T4BT_BI_82_x_y */ + + if (gIsoDep.isRxChaining) { /* Rule 5 - In PICC chaining when a + invalid/timeout occurs -> R-ACK */ + RFAL_EXIT_ON_ERR(ret, rfalIsoDepHandleControlMsg( + ISODEP_R_ACK, RFAL_ISODEP_NO_PARAM)); + } else if (gIsoDep.state == + ISODEP_ST_PCD_WAIT_DSL) { /* Rule 8 - If s-Deselect + response fails MAY retransmit + */ + RFAL_EXIT_ON_ERR(ret, rfalIsoDepHandleControlMsg( + ISODEP_S_DSL, RFAL_ISODEP_NO_PARAM)); + } else { /* Rule 4 - When a invalid block or timeout occurs -> R-NACK + */ + RFAL_EXIT_ON_ERR(ret, rfalIsoDepHandleControlMsg( + ISODEP_R_NAK, RFAL_ISODEP_NO_PARAM)); + } + return RFAL_ERR_BUSY; + + case RFAL_ERR_NONE: + break; + + case RFAL_ERR_BUSY: + return RFAL_ERR_BUSY; /* Debug purposes */ + + default: + return ret; + } + + /*******************************************************************************/ + /* No error, process incoming msg */ + /*******************************************************************************/ + + (*outActRxLen) = rfalConvBitsToBytes(*outActRxLen); + + /* Check rcvd msg length, cannot be less then the expected header */ + if (((*outActRxLen) < gIsoDep.hdrLen) || + ((*outActRxLen) >= gIsoDep.ourFsx)) { + return RFAL_ERR_PROTO; + } + + /* Grab rcvd PCB */ + rxPCB = gIsoDep.rxBuf[ISODEP_PCB_POS]; + + /* EMVCo doesn't allow usage of for CID or NAD EMVCo 2.6 TAble 10.2 */ + if ((gIsoDep.compMode == RFAL_COMPLIANCE_MODE_EMV) && + (rfalIsoDep_PCBhasDID(rxPCB) || rfalIsoDep_PCBhasNAD(rxPCB))) { + return RFAL_ERR_PROTO; + } + + /* If we are expecting DID, check if PCB signals its presence and if + * device ID match*/ + if ((gIsoDep.did != RFAL_ISODEP_NO_DID) && + (gIsoDep.did != RFAL_ISODEP_DID_00) && + ((!rfalIsoDep_PCBhasDID(rxPCB)) || + (gIsoDep.did != gIsoDep.rxBuf[ISODEP_DID_POS]))) { + return RFAL_ERR_PROTO; + } + + /*******************************************************************************/ + /* Process S-Block */ + /*******************************************************************************/ + if (rfalIsoDep_PCBisSBlock(rxPCB)) { + /* Check if is a Wait Time eXtension */ + if (rfalIsoDep_PCBisSWTX(rxPCB)) { + /* Check if PICC has requested S(WTX) as response to R(NAK) + * EMVCo 3.0 10.3.5.5 / Digital 2.0 16.2.6.5 */ + if (rfalIsoDep_PCBisRNAK(gIsoDep.lastPCB)) { + gIsoDep.cntSWtxNack++; /* Count S(WTX) upon R(NAK) */ + gIsoDep.cntRRetrys = + 0; /* Reset R-Block counter has PICC has responded */ + } else { + gIsoDep.cntSWtxNack = 0; /* Reset R(NACK)->S(WTX) counter */ + } + + /* Rule 3 - respond to S-block: get 1st INF byte S(STW): Power + WTXM + */ + RFAL_EXIT_ON_ERR( + ret, rfalIsoDepHandleControlMsg( + ISODEP_S_WTX, + rfalIsoDep_GetWTXM(gIsoDep.rxBuf[gIsoDep.hdrLen]))); + return RFAL_ERR_BUSY; + } + + /* Check if is a deselect response */ + if (rfalIsoDep_PCBisSDeselect(rxPCB)) { + if (gIsoDep.state == ISODEP_ST_PCD_WAIT_DSL) { + rfalIsoDepInitialize(); /* Session finished reInit vars */ + return RFAL_ERR_NONE; + } + + /* Deselect response not expected */ + /* fall through to PROTO error */ + } + /* Unexpected S-Block */ + return RFAL_ERR_PROTO; + } + + /*******************************************************************************/ + /* Process R-Block */ + /*******************************************************************************/ + else if (rfalIsoDep_PCBisRBlock(rxPCB)) { + if (rfalIsoDep_PCBisRACK(rxPCB)) /* Check if is a R-ACK */ + { + if (rfalIsoDep_GetBN(rxPCB) == + gIsoDep.blockNumber) /* Expected block number */ + { + /* Rule B - ACK with expected bn -> Increment block number */ + gIsoDep.blockNumber = rfalIsoDep_PCBNextBN(gIsoDep.blockNumber); + + /* R-ACK only allowed when PCD chaining */ + if (!gIsoDep.isTxChaining) { + return RFAL_ERR_PROTO; + } + + /* Rule 7 - Chaining transaction done, continue chaining */ + rfalIsoDepClearCounters(); + return RFAL_ERR_NONE; /* This block has been transmitted */ + } else { + /* Rule 6 - R-ACK with wrong block number retransmit */ + /* Digital 2.0 16.2.5.4 - Retransmit maximum two times */ + /* EMVCo 3.0 10.3.4.3 - PCD may re-transmit the last I-Block or + * report error */ + if (gIsoDep.cntIRetrys++ < gIsoDep.maxRetriesI) { + gIsoDep.cntRRetrys = 0; /* Clear R counter only */ + gIsoDep.state = ISODEP_ST_PCD_TX; + return RFAL_ERR_BUSY; + } + return RFAL_ERR_TIMEOUT; /* NFC Forum mandates timeout or + transmission error depending on previous + errors */ + } + } else /* Unexcpected R-Block */ + { + return RFAL_ERR_PROTO; + } + } + + /*******************************************************************************/ + /* Process I-Block */ + /*******************************************************************************/ + else if (rfalIsoDep_PCBisIBlock(rxPCB)) { + /*******************************************************************************/ + /* is PICC performing chaining */ + if (rfalIsoDep_PCBisChaining(rxPCB)) { + gIsoDep.isRxChaining = true; + *outIsChaining = true; + + if (rfalIsoDep_GetBN(rxPCB) == gIsoDep.blockNumber) { + /* Rule B - ACK with correct block number -> Increase Block number + */ + rfalIsoDep_ToggleBN(gIsoDep.blockNumber); + + rfalIsoDepClearCounters(); /* Clear counters in case R counter is + already at max */ + + /* Rule 2 - Send ACK */ + RFAL_EXIT_ON_ERR(ret, rfalIsoDepHandleControlMsg( + ISODEP_R_ACK, RFAL_ISODEP_NO_PARAM)); + + /* Received I-Block with chaining, send current data to DH */ + + /* remove ISO DEP header, check is necessary to move the INF data on + * the buffer */ + *outActRxLen -= gIsoDep.hdrLen; + if ((gIsoDep.hdrLen != gIsoDep.rxBufInfPos) && + (*outActRxLen > 0U)) { + RFAL_MEMMOVE(&gIsoDep.rxBuf[gIsoDep.rxBufInfPos], + &gIsoDep.rxBuf[gIsoDep.hdrLen], *outActRxLen); + } + + rfalIsoDepClearCounters(); + return RFAL_ERR_AGAIN; /* Send Again signalling to run again, but + some chaining data has arrived */ + } else { + /* Rule 5 - PICC chaining invalid I-Block -> R-ACK */ + RFAL_EXIT_ON_ERR(ret, rfalIsoDepHandleControlMsg( + ISODEP_R_ACK, RFAL_ISODEP_NO_PARAM)); + } + return RFAL_ERR_BUSY; + } + + gIsoDep.isRxChaining = false; /* clear PICC chaining flag */ + + if (rfalIsoDep_GetBN(rxPCB) == gIsoDep.blockNumber) { + /* Rule B - I-Block with correct block number -> Increase Block number + */ + rfalIsoDep_ToggleBN(gIsoDep.blockNumber); + + /* I-Block transaction done successfully */ + + /* remove ISO DEP header, check is necessary to move the INF data on + * the buffer */ + *outActRxLen -= gIsoDep.hdrLen; + if ((gIsoDep.hdrLen != gIsoDep.rxBufInfPos) && (*outActRxLen > 0U)) { + RFAL_MEMMOVE(&gIsoDep.rxBuf[gIsoDep.rxBufInfPos], + &gIsoDep.rxBuf[gIsoDep.hdrLen], *outActRxLen); + } + + gIsoDep.state = ISODEP_ST_IDLE; + rfalIsoDepClearCounters(); + return RFAL_ERR_NONE; + } else { + if ((gIsoDep.compMode != RFAL_COMPLIANCE_MODE_ISO)) { + /* Invalid Block (not chaining) -> Raise error Digital 1.1 15.2.6.4 + * EMVCo 2.6 10.3.5.4 */ + return RFAL_ERR_PROTO; + } + + /* Rule 4 - Invalid Block -> R-NAK */ + RFAL_EXIT_ON_ERR(ret, rfalIsoDepHandleControlMsg( + ISODEP_R_NAK, RFAL_ISODEP_NO_PARAM)); + return RFAL_ERR_BUSY; + } + } else /* not S/R/I - Block */ + { + return RFAL_ERR_PROTO; + } + /* fall through */ + + /*******************************************************************************/ + default: /* PRQA S 2003 # MISRA 16.3 - Intentional fall through */ + /* MISRA 16.4: no empty default (comment will suffice) */ + break; + } + + return RFAL_ERR_INTERNAL; +} + +/*******************************************************************************/ +ReturnCode rfalIsoDepDeselect(void) { + ReturnCode ret; + + RFAL_EXIT_ON_ERR(ret, rfalIsoDepStartDeselect()); + rfalRunBlocking(ret, rfalIsoDepGetDeselectStatus()); + + return ret; +} + +/*******************************************************************************/ +ReturnCode rfalIsoDepStartDeselect(void) { + /*******************************************************************************/ + /* Using local static vars and static config to cope with a Deselect after * + * RATS\ATTRIB without any I-Block exchanged */ + gIsoDep.rxLen = &gIsoDep.ctrlRxLen; + gIsoDep.rxBuf = gIsoDep.ctrlBuf; + gIsoDep.rxBufLen = + ISODEP_CONTROLMSG_BUF_LEN - (RFAL_ISODEP_PCB_LEN + RFAL_ISODEP_DID_LEN); + gIsoDep.rxBufInfPos = (RFAL_ISODEP_PCB_LEN + RFAL_ISODEP_DID_LEN); + gIsoDep.txBufInfPos = (RFAL_ISODEP_PCB_LEN + RFAL_ISODEP_DID_LEN); + + /* Send DSL request */ + return rfalIsoDepHandleControlMsg(ISODEP_S_DSL, RFAL_ISODEP_NO_PARAM); +} + +/*******************************************************************************/ +ReturnCode rfalIsoDepGetDeselectStatus(void) { + ReturnCode ret; + bool dummyB; + + RFAL_EXIT_ON_BUSY(ret, rfalIsoDepDataExchangePCD(gIsoDep.rxLen, &dummyB)); + + rfalIsoDepInitialize(); + return ret; +} + +#endif /* RFAL_FEATURE_ISO_DEP_POLL */ + +/*******************************************************************************/ +uint32_t rfalIsoDepFWI2FWT(uint8_t fwi) { + uint32_t result; + uint8_t tmpFWI; + + tmpFWI = fwi; + + /* RFU values -> take the default value + * Digital 1.0 11.6.2.17 FWI[1,14] + * Digital 1.1 7.6.2.22 FWI[0,14] + * EMVCo 2.6 Table A.5 FWI[0,14] */ + if (tmpFWI > ISODEP_FWI_MAX) { + tmpFWI = RFAL_ISODEP_FWI_DEFAULT; + } + + /* FWT = (256 x 16/fC) x 2^FWI => 2^(FWI+12) Digital 1.1 13.8.1 & 7.9.1 */ + + result = ((uint32_t)1U << (tmpFWI + 12U)); + result = RFAL_MIN(RFAL_ISODEP_MAX_FWT, + result); /* Maximum Frame Waiting Time must be fulfilled */ + + return result; +} + +/*******************************************************************************/ +uint16_t rfalIsoDepFSxI2FSx(uint8_t FSxI) { + uint16_t fsx; + uint8_t fsi; + + /* Enforce maximum FSxI/FSx allowed - NFC Forum and EMVCo differ */ + fsi = ((gIsoDep.compMode == RFAL_COMPLIANCE_MODE_EMV) + ? RFAL_MIN(FSxI, RFAL_ISODEP_FSDI_MAX_EMV) + : RFAL_MIN(FSxI, RFAL_ISODEP_FSDI_MAX_NFC)); + + switch (fsi) { + case (uint8_t)RFAL_ISODEP_FSXI_16: + fsx = (uint16_t)RFAL_ISODEP_FSX_16; + break; + case (uint8_t)RFAL_ISODEP_FSXI_24: + fsx = (uint16_t)RFAL_ISODEP_FSX_24; + break; + case (uint8_t)RFAL_ISODEP_FSXI_32: + fsx = (uint16_t)RFAL_ISODEP_FSX_32; + break; + case (uint8_t)RFAL_ISODEP_FSXI_40: + fsx = (uint16_t)RFAL_ISODEP_FSX_40; + break; + case (uint8_t)RFAL_ISODEP_FSXI_48: + fsx = (uint16_t)RFAL_ISODEP_FSX_48; + break; + case (uint8_t)RFAL_ISODEP_FSXI_64: + fsx = (uint16_t)RFAL_ISODEP_FSX_64; + break; + case (uint8_t)RFAL_ISODEP_FSXI_96: + fsx = (uint16_t)RFAL_ISODEP_FSX_96; + break; + case (uint8_t)RFAL_ISODEP_FSXI_128: + fsx = (uint16_t)RFAL_ISODEP_FSX_128; + break; + case (uint8_t)RFAL_ISODEP_FSXI_256: + fsx = (uint16_t)RFAL_ISODEP_FSX_256; + break; + case (uint8_t)RFAL_ISODEP_FSXI_512: + fsx = (uint16_t)RFAL_ISODEP_FSX_512; + break; + case (uint8_t)RFAL_ISODEP_FSXI_1024: + fsx = (uint16_t)RFAL_ISODEP_FSX_1024; + break; + case (uint8_t)RFAL_ISODEP_FSXI_2048: + fsx = (uint16_t)RFAL_ISODEP_FSX_2048; + break; + case (uint8_t)RFAL_ISODEP_FSXI_4096: + fsx = (uint16_t)RFAL_ISODEP_FSX_4096; + break; + default: + fsx = (uint16_t)RFAL_ISODEP_FSX_256; + break; + } + return fsx; +} + +#if RFAL_FEATURE_ISO_DEP_LISTEN + +/*******************************************************************************/ +bool rfalIsoDepIsRats(const uint8_t *buf, uint8_t bufLen) { + if (buf != NULL) { + if ((RFAL_ISODEP_CMD_RATS == (uint8_t)*buf) && + (sizeof(rfalIsoDepRats) == bufLen)) { + return true; + } + } + return false; +} + +/*******************************************************************************/ +bool rfalIsoDepIsAttrib(const uint8_t *buf, uint8_t bufLen) { + if (buf != NULL) { + if ((RFAL_ISODEP_CMD_ATTRIB == (uint8_t)*buf) && + (RFAL_ISODEP_ATTRIB_REQ_MIN_LEN <= bufLen) && + ((RFAL_ISODEP_ATTRIB_REQ_MIN_LEN + RFAL_ISODEP_ATTRIB_HLINFO_LEN) >= + bufLen)) { + return true; + } + } + return false; +} + +/*******************************************************************************/ +ReturnCode rfalIsoDepListenStartActivation( + rfalIsoDepAtsParam *atsParam, + const rfalIsoDepAttribResParam *attribResParam, const uint8_t *buf, + uint16_t bufLen, rfalIsoDepListenActvParam actParam) { + uint8_t *txBuf; + uint8_t bufIt; + const uint8_t *buffer = buf; + + /*******************************************************************************/ + bufIt = 0; + txBuf = (uint8_t *) + actParam.rxBuf; /* Use the rxBuf as TxBuf as well, the struct + enforces a size enough RFAL_MAX( + NFCA_ATS_MAX_LEN, NFCB_ATTRIB_RES_MAX_LEN ) */ + gIsoDep.txBR = RFAL_BR_106; + gIsoDep.rxBR = RFAL_BR_106; + + /* Check for a valid buffer pointer */ + if (buffer == NULL) { + return RFAL_ERR_PARAM; + } + + /*******************************************************************************/ + if (*buffer == RFAL_ISODEP_CMD_RATS) { + /* Check ATS parameters */ + if (atsParam == NULL) { + return RFAL_ERR_PARAM; + } + + /* If requested copy RATS to device info */ + if (actParam.isoDepDev != NULL) { + RFAL_MEMCPY((uint8_t *)&actParam.isoDepDev->activation.A.Poller.RATS, + buffer, sizeof(rfalIsoDepRats)); /* Copy RATS' CMD + PARAM */ + } + + /*******************************************************************************/ + /* Process RATS */ + buffer++; + gIsoDep.fsx = + rfalIsoDepFSxI2FSx((((*buffer) & RFAL_ISODEP_RATS_PARAM_FSDI_MASK) >> + RFAL_ISODEP_RATS_PARAM_FSDI_SHIFT)); + gIsoDep.did = (*buffer & RFAL_ISODEP_DID_MASK); + + /*******************************************************************************/ + /* Digital 1.1 13.6.1.8 - DID has to between 0 and 14 */ + if (gIsoDep.did > RFAL_ISODEP_DID_MAX) { + return RFAL_ERR_PROTO; + } + + /* Check if we are configured to support DID */ + if (!atsParam->didSupport) { + gIsoDep.did = + RFAL_ISODEP_NO_DID; /* Even if requested by PCD to use a certain DID, + the ATS setting prevails */ + } + + /*******************************************************************************/ + /* Check RFAL supported bit rates */ + if (((!RFAL_SUPPORT_BR_CE_A_212) && + (((atsParam->ta & RFAL_ISODEP_ATS_TA_DPL_212) != 0U) || + ((atsParam->ta & RFAL_ISODEP_ATS_TA_DLP_212) != 0U))) || + ((!RFAL_SUPPORT_BR_CE_A_424) && + (((atsParam->ta & RFAL_ISODEP_ATS_TA_DPL_424) != 0U) || + ((atsParam->ta & RFAL_ISODEP_ATS_TA_DLP_424) != 0U))) || + ((!RFAL_SUPPORT_BR_CE_A_848) && + (((atsParam->ta & RFAL_ISODEP_ATS_TA_DPL_848) != 0U) || + ((atsParam->ta & RFAL_ISODEP_ATS_TA_DLP_848) != 0U)))) { + return RFAL_ERR_NOTSUPP; + } + + /* Enforce proper FWI configuration */ + if (atsParam->fwi > ISODEP_FWI_LIS_MAX) { + atsParam->fwi = ISODEP_FWI_LIS_MAX; + } + + gIsoDep.atsTA = atsParam->ta; + gIsoDep.fwt = rfalIsoDepFWI2FWT(atsParam->fwi); + gIsoDep.ourFsx = rfalIsoDepFSxI2FSx(atsParam->fsci); + + /* Ensure proper/maximum Historical Bytes length */ + atsParam->hbLen = RFAL_MIN(RFAL_ISODEP_ATS_HB_MAX_LEN, atsParam->hbLen); + + /*******************************************************************************/ + /* Compute ATS */ + + txBuf[bufIt++] = (RFAL_ISODEP_ATS_HIST_OFFSET + atsParam->hbLen); /* TL */ + txBuf[bufIt++] = ((RFAL_ISODEP_ATS_T0_TA_PRESENCE_MASK | + RFAL_ISODEP_ATS_T0_TB_PRESENCE_MASK | + RFAL_ISODEP_ATS_T0_TC_PRESENCE_MASK) | + atsParam->fsci); /* T0 */ + txBuf[bufIt++] = atsParam->ta; /* TA */ + txBuf[bufIt++] = + ((atsParam->fwi << RFAL_ISODEP_RATS_PARAM_FSDI_SHIFT) | + (atsParam->sfgi & RFAL_ISODEP_RATS_PARAM_FSDI_MASK)); /* TB */ + txBuf[bufIt++] = (uint8_t)((atsParam->didSupport) ? RFAL_ISODEP_ATS_TC_DID + : 0U); /* TC */ + + if (atsParam->hbLen > 0U) /* MISRA 21.18 */ + { + RFAL_MEMCPY(&txBuf[bufIt], atsParam->hb, atsParam->hbLen); /* T1-Tk */ + bufIt += atsParam->hbLen; + } + + gIsoDep.state = ISODEP_ST_PICC_ACT_ATS; + + } + /*******************************************************************************/ + else if (*buffer == RFAL_ISODEP_CMD_ATTRIB) { + /* Check ATTRIB parameters */ + if (attribResParam == NULL) { + return RFAL_ERR_PARAM; + } + + /* REMARK: ATTRIB handling */ + RFAL_NO_WARNING(attribResParam); + RFAL_NO_WARNING(bufLen); + return RFAL_ERR_NOT_IMPLEMENTED; + } else { + return RFAL_ERR_PARAM; + } + + gIsoDep.actvParam = actParam; + + /*******************************************************************************/ + /* If requested copy to ISO-DEP device info */ + if (actParam.isoDepDev != NULL) { + actParam.isoDepDev->info.DID = gIsoDep.did; + actParam.isoDepDev->info.FSx = gIsoDep.fsx; + actParam.isoDepDev->info.FWT = gIsoDep.fwt; + actParam.isoDepDev->info.dFWT = 0; + actParam.isoDepDev->info.DSI = gIsoDep.txBR; + actParam.isoDepDev->info.DRI = gIsoDep.rxBR; + } + + return rfalTransceiveBlockingTx(txBuf, bufIt, (uint8_t *)actParam.rxBuf, + sizeof(rfalIsoDepBufFormat), actParam.rxLen, + RFAL_TXRX_FLAGS_DEFAULT, RFAL_FWT_NONE); +} + +/*******************************************************************************/ +ReturnCode rfalIsoDepListenGetActivationStatus(void) { + ReturnCode err; + uint8_t *txBuf; + uint8_t bufIt; + + rfalBitRate dsi; + rfalBitRate dri; + + /* Check if Activation is running */ + if (gIsoDep.state < ISODEP_ST_PICC_ACT_ATS) { + return RFAL_ERR_WRONG_STATE; + } + + /* Check if Activation has finished already */ + if (gIsoDep.state >= ISODEP_ST_PICC_RX) { + return RFAL_ERR_NONE; + } + + /*******************************************************************************/ + /* Check for incoming msg */ + err = rfalGetTransceiveStatus(); + switch (err) { + /*******************************************************************************/ + case RFAL_ERR_NONE: + break; + + /*******************************************************************************/ + case RFAL_ERR_LINK_LOSS: + case RFAL_ERR_BUSY: + case RFAL_ERR_SLEEP_REQ: /* S(DSL) handled by lower layer should cause an + error reported */ + return err; + + /*******************************************************************************/ + case RFAL_ERR_CRC: + case RFAL_ERR_PAR: + case RFAL_ERR_FRAMING: + + /* ISO14443 4 5.6.2.2 2 If ATS has been replied upon a invalid block, + * PICC disables the PPS responses */ + if (gIsoDep.state == ISODEP_ST_PICC_ACT_ATS) { + gIsoDep.state = ISODEP_ST_PICC_RX; + break; + } + /* fall through */ + + /*******************************************************************************/ + default: /* PRQA S 2003 # MISRA 16.3 - Intentional fall through */ + /* ReEnable the receiver and wait for another frame */ + rfalIsoDepReEnableRx((uint8_t *)gIsoDep.actvParam.rxBuf, + sizeof(rfalIsoDepBufFormat), + gIsoDep.actvParam.rxLen); + + return RFAL_ERR_BUSY; + } + + txBuf = + (uint8_t *)gIsoDep.actvParam + .rxBuf; /* Use the rxBuf as TxBuf as well, the struct enforces a size + enough RFAL_MAX(NFCA_PPS_RES_LEN, ISODEP_DSL_MAX_LEN) */ + dri = RFAL_BR_KEEP; /* The RFAL_BR_KEEP is used to check if PPS with BR change + was requested */ + dsi = RFAL_BR_KEEP; /* MISRA 9.1 */ + bufIt = 0; + + /*******************************************************************************/ + gIsoDep.role = ISODEP_ROLE_PICC; + + /*******************************************************************************/ + if (gIsoDep.state == ISODEP_ST_PICC_ACT_ATS) { + /* Check for a PPS ISO 14443-4 5.3 */ + if ((((uint8_t *)gIsoDep.actvParam.rxBuf)[RFAL_ISODEP_PPS_STARTBYTE_POS] & + RFAL_ISODEP_PPS_MASK) == RFAL_ISODEP_PPS_SB) { + /* ISO 14443-4 5.3.1 Check if the we are the addressed DID/CID */ + /* ISO 14443-4 5.3.2 Check for a valid PPS0 */ + if (((((uint8_t *) + gIsoDep.actvParam.rxBuf)[RFAL_ISODEP_PPS_STARTBYTE_POS] & + RFAL_ISODEP_DID_MASK) != gIsoDep.did) || + ((((uint8_t *)gIsoDep.actvParam.rxBuf)[RFAL_ISODEP_PPS_PPS0_POS] & + RFAL_ISODEP_PPS0_VALID_MASK) != + RFAL_ISODEP_PPS0_PPS1_NOT_PRESENT)) { + /* Invalid DID on PPS request or Invalid PPS0, reEnable the receiver and + * wait another frame */ + rfalIsoDepReEnableRx((uint8_t *)gIsoDep.actvParam.rxBuf, + sizeof(rfalIsoDepBufFormat), + gIsoDep.actvParam.rxLen); + + return RFAL_ERR_BUSY; + } + + /*******************************************************************************/ + /* Check PPS1 presence */ + if (((uint8_t *)gIsoDep.actvParam.rxBuf)[RFAL_ISODEP_PPS_PPS0_POS] == + RFAL_ISODEP_PPS0_PPS1_PRESENT) { + uint8_t newdri = + ((uint8_t *)gIsoDep.actvParam.rxBuf)[RFAL_ISODEP_PPS_PPS1_POS] & + RFAL_ISODEP_PPS1_DxI_MASK; /* MISRA 10.8 */ + uint8_t newdsi = + (((uint8_t *)gIsoDep.actvParam.rxBuf)[RFAL_ISODEP_PPS_PPS1_POS] >> + RFAL_ISODEP_PPS1_DSI_SHIFT) & + RFAL_ISODEP_PPS1_DxI_MASK; /* MISRA 10.8 */ + /* PRQA S 4342 2 # MISRA 10.5 - Layout of enum rfalBitRate and above + * masks guarantee no invalid enum values to be created */ + dri = (rfalBitRate)(newdri); + dsi = (rfalBitRate)(newdsi); + + if (((!(RFAL_SUPPORT_BR_CE_A_106)) && + ((dsi == RFAL_BR_106) || (dri == RFAL_BR_106))) || + ((!(RFAL_SUPPORT_BR_CE_A_212)) && + ((dsi == RFAL_BR_212) || (dri == RFAL_BR_212))) || + ((!(RFAL_SUPPORT_BR_CE_A_424)) && + ((dsi == RFAL_BR_424) || (dri == RFAL_BR_424))) || + ((!(RFAL_SUPPORT_BR_CE_A_848)) && + ((dsi == RFAL_BR_848) || (dri == RFAL_BR_848)))) { + return RFAL_ERR_PROTO; + } + } + + /*******************************************************************************/ + /* Compute and send PPS RES / Ack */ + txBuf[bufIt++] = + ((uint8_t *)gIsoDep.actvParam.rxBuf)[RFAL_ISODEP_PPS_STARTBYTE_POS]; + + rfalTransceiveBlockingTx(txBuf, bufIt, (uint8_t *)gIsoDep.actvParam.rxBuf, + sizeof(rfalIsoDepBufFormat), + gIsoDep.actvParam.rxLen, RFAL_TXRX_FLAGS_DEFAULT, + RFAL_FWT_NONE); + + /*******************************************************************************/ + /* Exchange the bit rates if requested */ + if (dri != RFAL_BR_KEEP) { + rfalSetBitRate(dsi, dri); + + gIsoDep.txBR = dsi; /* DSI codes the divisor from PICC to PCD */ + gIsoDep.rxBR = dri; /* DRI codes the divisor from PCD to PICC */ + + if (gIsoDep.actvParam.isoDepDev != NULL) { + gIsoDep.actvParam.isoDepDev->info.DSI = dsi; + gIsoDep.actvParam.isoDepDev->info.DRI = dri; + } + } + } + /* Check for a S-Deselect is done on Data Exchange Activity */ + } + + /*******************************************************************************/ + gIsoDep.hdrLen = RFAL_ISODEP_PCB_LEN; + gIsoDep.hdrLen += RFAL_ISODEP_DID_LEN; /* Always assume DID to be aligned with + Digital 1.1 15.1.2 and ISO14443 + 4 5.6.3 #454 */ + gIsoDep.hdrLen += + (uint8_t)((gIsoDep.nad != RFAL_ISODEP_NO_NAD) ? RFAL_ISODEP_NAD_LEN : 0U); + + /*******************************************************************************/ + /* Rule C - The PICC block number shall be initialized to 1 at activation */ + gIsoDep.blockNumber = 1; + + /* Activation done, keep the rcvd data in, reMap the activation buffer to the + * global to be retrieved by the DEP method */ + gIsoDep.rxBuf = (uint8_t *)gIsoDep.actvParam.rxBuf; + gIsoDep.rxBufLen = sizeof(rfalIsoDepBufFormat); + gIsoDep.rxBufInfPos = (uint8_t)((uint32_t)gIsoDep.actvParam.rxBuf->inf - + (uint32_t)gIsoDep.actvParam.rxBuf->prologue); + gIsoDep.rxLen = gIsoDep.actvParam.rxLen; + gIsoDep.rxChaining = gIsoDep.actvParam.isRxChaining; + + gIsoDep.state = ISODEP_ST_PICC_RX; + return RFAL_ERR_NONE; +} + +#endif /* RFAL_FEATURE_ISO_DEP_LISTEN */ + +/*******************************************************************************/ +uint16_t rfalIsoDepGetMaxInfLen(void) { + /* Check whether all parameters are valid, otherwise return minimum default + * value */ + if ((gIsoDep.fsx < (uint16_t)RFAL_ISODEP_FSX_16) || + (gIsoDep.fsx > (uint16_t)RFAL_ISODEP_FSX_4096) || + (gIsoDep.hdrLen > ISODEP_HDR_MAX_LEN)) { + const uint16_t aux = (uint16_t)RFAL_ISODEP_FSX_16; /* MISRA 10.1 */ + return (aux - RFAL_ISODEP_PCB_LEN - ISODEP_CRC_LEN); + } + + return (gIsoDep.fsx - gIsoDep.hdrLen - ISODEP_CRC_LEN); +} + +/*******************************************************************************/ +ReturnCode rfalIsoDepStartTransceive(rfalIsoDepTxRxParam param) { + gIsoDep.txBuf = param.txBuf->prologue; + gIsoDep.txBufInfPos = + (uint8_t)((uintptr_t)param.txBuf->inf - (uintptr_t)param.txBuf->prologue); + gIsoDep.txBufLen = param.txBufLen; + gIsoDep.isTxChaining = param.isTxChaining; + + gIsoDep.rxBuf = param.rxBuf->prologue; + gIsoDep.rxBufInfPos = + (uint8_t)((uintptr_t)param.rxBuf->inf - (uintptr_t)param.rxBuf->prologue); + gIsoDep.rxBufLen = sizeof(rfalIsoDepBufFormat); + + gIsoDep.rxLen = param.rxLen; + gIsoDep.rxChaining = param.isRxChaining; + + gIsoDep.fwt = param.FWT; + gIsoDep.dFwt = param.dFWT; + gIsoDep.fsx = param.FSx; + gIsoDep.did = param.DID; + + /* Only change the FSx from activation if no to Keep */ + gIsoDep.ourFsx = + ((param.ourFSx != RFAL_ISODEP_FSX_KEEP) ? param.ourFSx : gIsoDep.ourFsx); + + /* Clear inner control params for next dataExchange */ + gIsoDep.isRxChaining = false; + rfalIsoDepClearCounters(); + + if (gIsoDep.role == ISODEP_ROLE_PICC) { + if (gIsoDep.txBufLen > 0U) { + /* Ensure that an RTOX Ack is not being expected at moment */ + if (!gIsoDep.isWait4WTX) { + gIsoDep.state = ISODEP_ST_PICC_TX; + return RFAL_ERR_NONE; + } else { + /* If RTOX Ack is expected, signal a pending Tx to be transmitted right + * after */ + gIsoDep.isTxPending = true; + } + } + + /* Digital 1.1 15.2.5.1 The first block SHALL be sent by the Reader/Writer + */ + gIsoDep.state = ISODEP_ST_PICC_RX; + return RFAL_ERR_NONE; + } + + gIsoDep.state = ISODEP_ST_PCD_TX; + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalIsoDepGetTransceiveStatus(void) { + if (gIsoDep.role == ISODEP_ROLE_PICC) { +#if RFAL_FEATURE_ISO_DEP_LISTEN + return rfalIsoDepDataExchangePICC(); +#else + return RFAL_ERR_NOTSUPP; +#endif /* RFAL_FEATURE_ISO_DEP_LISTEN */ + } else { +#if RFAL_FEATURE_ISO_DEP_POLL + return rfalIsoDepDataExchangePCD(gIsoDep.rxLen, gIsoDep.rxChaining); +#else + return RFAL_ERR_NOTSUPP; +#endif /* RFAL_FEATURE_ISO_DEP_POLL */ + } +} + +#if RFAL_FEATURE_ISO_DEP_LISTEN + +/*******************************************************************************/ +static ReturnCode rfalIsoDepDataExchangePICC(void) { + uint8_t rxPCB; + ReturnCode ret; + + switch (gIsoDep.state) { + /*******************************************************************************/ + case ISODEP_ST_IDLE: + return RFAL_ERR_NONE; + + /*******************************************************************************/ + case ISODEP_ST_PICC_TX: + + ret = rfalIsoDepTx(rfalIsoDep_PCBIBlock(gIsoDep.blockNumber), + gIsoDep.txBuf, &gIsoDep.txBuf[gIsoDep.txBufInfPos], + gIsoDep.txBufLen, RFAL_FWT_NONE); + + /* Clear pending Tx flag */ + gIsoDep.isTxPending = false; + + switch (ret) { + case RFAL_ERR_NONE: + gIsoDep.state = ISODEP_ST_PICC_RX; + return RFAL_ERR_BUSY; + + default: + /* MISRA 16.4: no empty default statement (a comment being enough) */ + break; + } + return ret; + + /*******************************************************************************/ + case ISODEP_ST_PICC_RX: + + ret = rfalGetTransceiveStatus(); + switch (ret) { + /*******************************************************************************/ + /* Data rcvd with error or timeout -> mute */ + case RFAL_ERR_TIMEOUT: + case RFAL_ERR_CRC: + case RFAL_ERR_PAR: + case RFAL_ERR_FRAMING: + + /* Digital 1.1 - 15.2.6.2 The CE SHALL NOT attempt error recovery and + * remains in Rx mode upon Transmission or a Protocol Error */ + rfalIsoDepReEnableRx((uint8_t *)gIsoDep.rxBuf, + sizeof(rfalIsoDepBufFormat), gIsoDep.rxLen); + + return RFAL_ERR_BUSY; + + /*******************************************************************************/ + case RFAL_ERR_LINK_LOSS: + return ret; /* Debug purposes */ + + case RFAL_ERR_BUSY: + return ret; /* Debug purposes */ + + /*******************************************************************************/ + case RFAL_ERR_NONE: + *gIsoDep.rxLen = rfalConvBitsToBytes(*gIsoDep.rxLen); + break; + + /*******************************************************************************/ + default: + return ret; + } + break; + + /*******************************************************************************/ + case ISODEP_ST_PICC_SWTX: + + if (!rfalIsoDepTimerisExpired( + gIsoDep.WTXTimer)) /* Do nothing until WTX timer has expired */ + { + return RFAL_ERR_BUSY; + } + + /* Set waiting for WTX Ack Flag */ + gIsoDep.isWait4WTX = true; + + /* Digital 1.1 15.2.2.9 - Calculate the WTXM such that FWTtemp <= FWTmax + */ + gIsoDep.lastWTXM = (uint8_t)rfalIsoDep_WTXMListenerMax(gIsoDep.fwt); + RFAL_EXIT_ON_ERR( + ret, rfalIsoDepHandleControlMsg(ISODEP_S_WTX, gIsoDep.lastWTXM)); + + gIsoDep.state = ISODEP_ST_PICC_RX; /* Go back to Rx to process WTX ack */ + return RFAL_ERR_BUSY; + + /*******************************************************************************/ + case ISODEP_ST_PICC_SDSL: + + if (!rfalIsTransceiveInTx()) /* Wait until DSL response has been sent */ + { + rfalIsoDepInitialize(); /* Session finished reInit vars, go back to + ISODEP_ST_IDLE */ + return RFAL_ERR_SLEEP_REQ; /* Notify Deselect request */ + } + return RFAL_ERR_BUSY; + + /*******************************************************************************/ + default: + return RFAL_ERR_INTERNAL; + } + + /* ISO 14443-4 7.5.6.2 CE SHALL NOT attempt error recovery -> clear counters + */ + rfalIsoDepClearCounters(); + + /*******************************************************************************/ + /* No error, process incoming msg */ + /*******************************************************************************/ + + /* Grab rcvd PCB */ + rxPCB = gIsoDep.rxBuf[ISODEP_PCB_POS]; + + /*******************************************************************************/ + /* When DID=0 PCD may or may not use DID, therefore check whether current PCD + * request has DID present to be reflected on max INF length #454 */ + + /* ReCalculate Header Length */ + gIsoDep.hdrLen = RFAL_ISODEP_PCB_LEN; + gIsoDep.hdrLen += + (uint8_t)((rfalIsoDep_PCBhasDID(rxPCB)) ? RFAL_ISODEP_DID_LEN : 0U); + gIsoDep.hdrLen += + (uint8_t)((rfalIsoDep_PCBhasNAD(rxPCB)) ? RFAL_ISODEP_NAD_LEN : 0U); + + /* Store whether last PCD block had DID. for PICC special handling of DID = 0 + */ + if (gIsoDep.did == RFAL_ISODEP_DID_00) { + gIsoDep.lastDID00 = ((rfalIsoDep_PCBhasDID(rxPCB)) ? true : false); + } + + /*******************************************************************************/ + /* Check rcvd msg length, cannot be less then the expected header OR * if + * the rcvd msg exceeds our announced frame size (FSD) */ + if (((*gIsoDep.rxLen) < gIsoDep.hdrLen) || + ((*gIsoDep.rxLen) > (gIsoDep.ourFsx - ISODEP_CRC_LEN))) { + rfalIsoDepReEnableRx((uint8_t *)gIsoDep.rxBuf, sizeof(rfalIsoDepBufFormat), + gIsoDep.rxLen); + return RFAL_ERR_BUSY; /* RFAL_ERR_PROTO Ignore this protocol request */ + } + /* If we are not supporting DID, but we receive one OR + * If we are expecting DID, check if PCB signals its presence and if device ID + * match OR If our DID=0 and DID is sent but with an incorrect value */ + if (((gIsoDep.did == RFAL_ISODEP_NO_DID) && (rfalIsoDep_PCBhasDID(rxPCB))) || + ((gIsoDep.did != RFAL_ISODEP_NO_DID) && + (gIsoDep.did != RFAL_ISODEP_DID_00) && + ((!rfalIsoDep_PCBhasDID(rxPCB)) || + (gIsoDep.did != gIsoDep.rxBuf[ISODEP_DID_POS]))) || + ((gIsoDep.did == RFAL_ISODEP_DID_00) && rfalIsoDep_PCBhasDID(rxPCB) && + (RFAL_ISODEP_DID_00 != gIsoDep.rxBuf[ISODEP_DID_POS]))) { + rfalIsoDepReEnableRx((uint8_t *)gIsoDep.rxBuf, sizeof(rfalIsoDepBufFormat), + gIsoDep.rxLen); + return RFAL_ERR_BUSY; /* Ignore a wrong DID request */ + } + + /* If we aren't expecting NAD and it's received */ + if ((gIsoDep.nad == RFAL_ISODEP_NO_NAD) && rfalIsoDep_PCBhasNAD(rxPCB)) { + rfalIsoDepReEnableRx((uint8_t *)gIsoDep.rxBuf, sizeof(rfalIsoDepBufFormat), + gIsoDep.rxLen); + return RFAL_ERR_BUSY; /* Ignore a unexpected NAD request */ + } + + /*******************************************************************************/ + /* Process S-Block */ + /*******************************************************************************/ + if (rfalIsoDep_PCBisSBlock(rxPCB)) { + /* Check if is a Wait Time eXtension */ + if (rfalIsoDep_PCBisSWTX(rxPCB)) { + /* Check if we're expecting a S-WTX */ + if (rfalIsoDep_PCBisWTX(gIsoDep.lastPCB)) { + /* Digital 1.1 15.2.2.11 S(WTX) Ack with different WTXM -> Protocol + * Error * Power level indication also should be set to 0 */ + if ((gIsoDep.rxBuf[gIsoDep.hdrLen] == gIsoDep.lastWTXM) && + ((*gIsoDep.rxLen - gIsoDep.hdrLen) == ISODEP_SWTX_INF_LEN)) { + /* Clear waiting for RTOX Ack Flag */ + gIsoDep.isWait4WTX = false; + + /* Check if a Tx is already pending */ + if (gIsoDep.isTxPending) { + /* Has a pending Tx, go immediately to TX */ + gIsoDep.state = ISODEP_ST_PICC_TX; + return RFAL_ERR_BUSY; + } + + /* Set WTX timer */ + rfalIsoDepTimerStart( + gIsoDep.WTXTimer, + rfalIsoDep_WTXAdjust( + (gIsoDep.lastWTXM * rfalConv1fcToMs(gIsoDep.fwt)))); + + gIsoDep.state = ISODEP_ST_PICC_SWTX; + return RFAL_ERR_BUSY; + } + } + /* Unexpected/Incorrect S-WTX, fall into reRenable */ + } + + /* Check if is a Deselect request */ + if (rfalIsoDep_PCBisSDeselect(rxPCB) && + ((*gIsoDep.rxLen - gIsoDep.hdrLen) == ISODEP_SDSL_INF_LEN)) { + RFAL_EXIT_ON_ERR( + ret, rfalIsoDepHandleControlMsg(ISODEP_S_DSL, RFAL_ISODEP_NO_PARAM)); + + /* S-DSL transmission ongoing, wait until complete */ + gIsoDep.state = ISODEP_ST_PICC_SDSL; + return RFAL_ERR_BUSY; + } + + /* Unexpected S-Block, fall into reRenable */ + } + + /*******************************************************************************/ + /* Process R-Block */ + /*******************************************************************************/ + else if (rfalIsoDep_PCBisRBlock(rxPCB) && + ((*gIsoDep.rxLen - gIsoDep.hdrLen) == ISODEP_RBLOCK_INF_LEN)) { + if (rfalIsoDep_PCBisRACK(rxPCB)) /* Check if is a R-ACK */ + { + if (rfalIsoDep_GetBN(rxPCB) == + gIsoDep.blockNumber) /* Check block number */ + { + /* Rule 11 - R(ACK) with current bn -> re-transmit */ + if (!rfalIsoDep_PCBisIBlock(gIsoDep.lastPCB)) { + rfalIsoDepReSendControlMsg(); + } else { + gIsoDep.state = ISODEP_ST_PICC_TX; + } + + return RFAL_ERR_BUSY; + } else { + if (!gIsoDep.isTxChaining) { + /* Rule 13 violation R(ACK) without performing chaining */ + rfalIsoDepReEnableRx((uint8_t *)gIsoDep.rxBuf, + sizeof(rfalIsoDepBufFormat), gIsoDep.rxLen); + return RFAL_ERR_BUSY; + } + + /* Rule E - R(ACK) with not current bn -> toggle bn */ + rfalIsoDep_ToggleBN(gIsoDep.blockNumber); + + /* This block has been transmitted and acknowledged, perform WTX until + * next data is provided */ + + /* Rule 9 - PICC is allowed to send an S(WTX) instead of an I-block or + * an R(ACK) */ + rfalIsoDepTimerStart( + gIsoDep.WTXTimer, + rfalIsoDep_WTXAdjust(rfalConv1fcToMs(gIsoDep.fwt))); + gIsoDep.state = ISODEP_ST_PICC_SWTX; + + /* Rule 13 - R(ACK) with not current bn -> continue chaining */ + return RFAL_ERR_NONE; /* This block has been transmitted */ + } + } else if (rfalIsoDep_PCBisRNAK(rxPCB)) /* Check if is a R-NACK */ + { + if (rfalIsoDep_GetBN(rxPCB) == + gIsoDep.blockNumber) /* Check block number */ + { + /* Rule 11 - R(NAK) with current bn -> re-transmit last x-Block */ + if (!rfalIsoDep_PCBisIBlock(gIsoDep.lastPCB)) { + rfalIsoDepReSendControlMsg(); + } else { + gIsoDep.state = ISODEP_ST_PICC_TX; + } + + return RFAL_ERR_BUSY; + } else { + /* Rule 12 - R(NAK) with not current bn -> R(ACK) */ + RFAL_EXIT_ON_ERR(ret, rfalIsoDepHandleControlMsg(ISODEP_R_ACK, + RFAL_ISODEP_NO_PARAM)); + + return RFAL_ERR_BUSY; + } + } else { + /* MISRA 15.7 - Empty else */ + } + + /* Unexpected R-Block, fall into reRenable */ + } + + /*******************************************************************************/ + /* Process I-Block */ + /*******************************************************************************/ + else if (rfalIsoDep_PCBisIBlock(rxPCB)) { + /* Rule D - When an I-block is received, the PICC shall toggle its block + * number before sending a block */ + rfalIsoDep_ToggleBN(gIsoDep.blockNumber); + + /*******************************************************************************/ + /* Check if the block number is the one expected */ + /* Check if PCD sent an I-Block instead ACK/NACK when we are chaining */ + if ((rfalIsoDep_GetBN(rxPCB) != gIsoDep.blockNumber) || + (gIsoDep.isTxChaining)) { + /* Remain in the same Block Number */ + rfalIsoDep_ToggleBN(gIsoDep.blockNumber); + + /* ISO 14443-4 7.5.6.2 & Digital 1.1 - 15.2.6.2 The CE SHALL NOT attempt + * error recovery and remains in Rx mode upon Transmission or a Protocol + * Error */ + rfalIsoDepReEnableRx((uint8_t *)gIsoDep.rxBuf, + sizeof(rfalIsoDepBufFormat), gIsoDep.rxLen); + return RFAL_ERR_BUSY; + } + + /*******************************************************************************/ + /* is PCD performing chaining ? */ + if (rfalIsoDep_PCBisChaining(rxPCB)) { + gIsoDep.isRxChaining = true; + *gIsoDep.rxChaining = true; /* Output Parameter*/ + + RFAL_EXIT_ON_ERR( + ret, rfalIsoDepHandleControlMsg(ISODEP_R_ACK, RFAL_ISODEP_NO_PARAM)); + + /* Received I-Block with chaining, send current data to DH */ + + /* remove ISO DEP header, check is necessary to move the INF data on the + * buffer */ + *gIsoDep.rxLen -= gIsoDep.hdrLen; + if ((gIsoDep.hdrLen != gIsoDep.rxBufInfPos) && (*gIsoDep.rxLen > 0U)) { + RFAL_MEMMOVE(&gIsoDep.rxBuf[gIsoDep.rxBufInfPos], + &gIsoDep.rxBuf[gIsoDep.hdrLen], *gIsoDep.rxLen); + } + return RFAL_ERR_AGAIN; /* Send Again signalling to run again, but some + chaining data has arrived*/ + } + + /*******************************************************************************/ + /* PCD is not performing chaining */ + gIsoDep.isRxChaining = false; /* clear PCD chaining flag */ + *gIsoDep.rxChaining = false; /* Output Parameter */ + + /* remove ISO DEP header, check is necessary to move the INF data on the + * buffer */ + *gIsoDep.rxLen -= gIsoDep.hdrLen; + if ((gIsoDep.hdrLen != gIsoDep.rxBufInfPos) && (*gIsoDep.rxLen > 0U)) { + RFAL_MEMMOVE(&gIsoDep.rxBuf[gIsoDep.rxBufInfPos], + &gIsoDep.rxBuf[gIsoDep.hdrLen], *gIsoDep.rxLen); + } + + /*******************************************************************************/ + /* Reception done, send data back and start WTX timer */ + rfalIsoDepTimerStart(gIsoDep.WTXTimer, + rfalIsoDep_WTXAdjust(rfalConv1fcToMs(gIsoDep.fwt))); + + gIsoDep.state = ISODEP_ST_PICC_SWTX; + return RFAL_ERR_NONE; + } else { + /* MISRA 15.7 - Empty else */ + } + + /* Unexpected/Unknown Block */ + /* ISO 14443-4 7.5.6.2 & Digital 1.1 - 15.2.6.2 The CE SHALL NOT attempt + * error recovery and remains in Rx mode upon Transmission or a Protocol Error + */ + rfalIsoDepReEnableRx((uint8_t *)gIsoDep.rxBuf, sizeof(rfalIsoDepBufFormat), + gIsoDep.rxLen); + + return RFAL_ERR_BUSY; +} +#endif /* RFAL_FEATURE_ISO_DEP_LISTEN */ + +#if RFAL_FEATURE_ISO_DEP_POLL + +#if RFAL_FEATURE_NFCA + +/*******************************************************************************/ +static ReturnCode rfalIsoDepStartRATS(rfalIsoDepFSxI FSDI, uint8_t DID, + rfalIsoDepAts *ats, uint8_t *atsLen) { + rfalTransceiveContext ctx; + + if (ats == NULL) { + return RFAL_ERR_PARAM; + } + + gIsoDep.rxBuf = (uint8_t *)ats; + gIsoDep.rxLen8 = atsLen; + gIsoDep.did = DID; + + /*******************************************************************************/ + /* Compose RATS */ + gIsoDep.actv.ratsReq.CMD = RFAL_ISODEP_CMD_RATS; + gIsoDep.actv.ratsReq.PARAM = + (((uint8_t)FSDI << RFAL_ISODEP_RATS_PARAM_FSDI_SHIFT) & + RFAL_ISODEP_RATS_PARAM_FSDI_MASK) | + (DID & RFAL_ISODEP_RATS_PARAM_DID_MASK); + + rfalCreateByteFlagsTxRxContext( + ctx, (uint8_t *)&gIsoDep.actv.ratsReq, sizeof(rfalIsoDepRats), + (uint8_t *)ats, sizeof(rfalIsoDepAts), &gIsoDep.rxBufLen, + RFAL_TXRX_FLAGS_DEFAULT, RFAL_ISODEP_T4T_FWT_ACTIVATION); + return rfalStartTransceive(&ctx); +} + +/*******************************************************************************/ +static ReturnCode rfalIsoDepGetRATSStatus(void) { + ReturnCode ret; + + ret = rfalGetTransceiveStatus(); + if (ret == RFAL_ERR_NONE) { + gIsoDep.rxBufLen = rfalConvBitsToBytes(gIsoDep.rxBufLen); + + /* Check for valid ATS length Digital 1.1 13.6.2.1 & 13.6.2.3 */ + if ((gIsoDep.rxBufLen < RFAL_ISODEP_ATS_MIN_LEN) || + (gIsoDep.rxBufLen > RFAL_ISODEP_ATS_MAX_LEN) || + (gIsoDep.rxBuf[RFAL_ISODEP_ATS_TL_POS] != gIsoDep.rxBufLen)) { + return RFAL_ERR_PROTO; + } + + /* Assign our FSx, in case the a Deselect is send without Transceive */ + gIsoDep.ourFsx = + rfalIsoDepFSxI2FSx((uint8_t)(gIsoDep.actv.ratsReq.PARAM >> + RFAL_ISODEP_RATS_PARAM_FSDI_SHIFT)); + + /* Check and assign if ATS length was requested (length also available on + * TL) */ + if (gIsoDep.rxLen8 != NULL) { + *gIsoDep.rxLen8 = (uint8_t)gIsoDep.rxBufLen; + } + } + + return ret; +} + +/*******************************************************************************/ +ReturnCode rfalIsoDepRATS(rfalIsoDepFSxI FSDI, uint8_t DID, rfalIsoDepAts *ats, + uint8_t *atsLen) { + ReturnCode ret; + + RFAL_EXIT_ON_ERR(ret, rfalIsoDepStartRATS(FSDI, DID, ats, atsLen)); + rfalRunBlocking(ret, rfalIsoDepGetRATSStatus()); + + return ret; +} + +/*******************************************************************************/ +static ReturnCode rfalIsoDepStartPPS(uint8_t DID, rfalBitRate DSI, + rfalBitRate DRI, + rfalIsoDepPpsRes *ppsRes) { + rfalTransceiveContext ctx; + + if ((ppsRes == NULL) || (DSI > RFAL_BR_848) || (DRI > RFAL_BR_848) || + ((DID > RFAL_ISODEP_DID_MAX) && (DID != RFAL_ISODEP_NO_DID))) { + return RFAL_ERR_PARAM; + } + + gIsoDep.rxBuf = (uint8_t *)ppsRes; + + /*******************************************************************************/ + /* Compose PPS Request */ + gIsoDep.actv.ppsReq.PPSS = + (RFAL_ISODEP_PPS_SB | (DID & RFAL_ISODEP_PPS_SB_DID_MASK)); + gIsoDep.actv.ppsReq.PPS0 = RFAL_ISODEP_PPS_PPS0_PPS1_PRESENT; + gIsoDep.actv.ppsReq.PPS1 = + (RFAL_ISODEP_PPS_PPS1 | + ((((uint8_t)DSI << RFAL_ISODEP_PPS_PPS1_DSI_SHIFT) | (uint8_t)DRI) & + RFAL_ISODEP_PPS_PPS1_DXI_MASK)); + + rfalCreateByteFlagsTxRxContext( + ctx, (uint8_t *)&gIsoDep.actv.ppsReq, sizeof(rfalIsoDepPpsReq), + (uint8_t *)ppsRes, sizeof(rfalIsoDepPpsRes), &gIsoDep.rxBufLen, + RFAL_TXRX_FLAGS_DEFAULT, RFAL_ISODEP_T4T_FWT_ACTIVATION); + return rfalStartTransceive(&ctx); +} + +/*******************************************************************************/ +static ReturnCode rfalIsoDepGetPPSSTatus(void) { + ReturnCode ret; + + ret = rfalGetTransceiveStatus(); + if (ret == RFAL_ERR_NONE) { + gIsoDep.rxBufLen = rfalConvBitsToBytes(gIsoDep.rxBufLen); + + /* Check for valid PPS Response */ + if ((gIsoDep.rxBufLen != RFAL_ISODEP_PPS_RES_LEN) || + (*gIsoDep.rxBuf != gIsoDep.actv.ppsReq.PPSS)) { + return RFAL_ERR_PROTO; + } + } + return ret; +} + +/*******************************************************************************/ +ReturnCode rfalIsoDepPPS(uint8_t DID, rfalBitRate DSI, rfalBitRate DRI, + rfalIsoDepPpsRes *ppsRes) { + ReturnCode ret; + + RFAL_EXIT_ON_ERR(ret, rfalIsoDepStartPPS(DID, DSI, DRI, ppsRes)); + rfalRunBlocking(ret, rfalIsoDepGetPPSSTatus()); + + return ret; +} + +#endif /* RFAL_FEATURE_NFCA */ + +#if RFAL_FEATURE_NFCB + +static ReturnCode rfalIsoDepStartATTRIB(const uint8_t *nfcid0, uint8_t PARAM1, + rfalBitRate DSI, rfalBitRate DRI, + rfalIsoDepFSxI FSDI, uint8_t PARAM3, + uint8_t DID, const uint8_t *HLInfo, + uint8_t HLInfoLen, uint32_t fwt, + rfalIsoDepAttribRes *attribRes, + uint8_t *attribResLen) { + rfalTransceiveContext ctx; + + if ((attribRes == NULL) || (attribResLen == NULL) || (DSI > RFAL_BR_848) || + (DRI > RFAL_BR_848) || + ((DID > RFAL_ISODEP_DID_MAX) && (DID != RFAL_ISODEP_NO_DID))) { + return RFAL_ERR_NONE; + } + + gIsoDep.rxBuf = (uint8_t *)attribRes; + gIsoDep.rxLen8 = attribResLen; + gIsoDep.did = DID; + + /*******************************************************************************/ + /* Compose ATTRIB command */ + gIsoDep.actv.attribReq.cmd = RFAL_ISODEP_CMD_ATTRIB; + gIsoDep.actv.attribReq.Param.PARAM1 = PARAM1; + gIsoDep.actv.attribReq.Param.PARAM2 = + (((((uint8_t)DSI << RFAL_ISODEP_ATTRIB_PARAM2_DSI_SHIFT) | + ((uint8_t)DRI << RFAL_ISODEP_ATTRIB_PARAM2_DRI_SHIFT)) & + RFAL_ISODEP_ATTRIB_PARAM2_DXI_MASK) | + ((uint8_t)FSDI & RFAL_ISODEP_ATTRIB_PARAM2_FSDI_MASK)); + gIsoDep.actv.attribReq.Param.PARAM3 = PARAM3; + gIsoDep.actv.attribReq.Param.PARAM4 = + (DID & RFAL_ISODEP_ATTRIB_PARAM4_DID_MASK); + RFAL_MEMCPY(gIsoDep.actv.attribReq.nfcid0, nfcid0, RFAL_NFCB_NFCID0_LEN); + + /* Append the Higher layer Info if provided */ + if ((HLInfo != NULL) && (HLInfoLen > 0U)) { + RFAL_MEMCPY(gIsoDep.actv.attribReq.HLInfo, HLInfo, + RFAL_MIN(HLInfoLen, RFAL_ISODEP_ATTRIB_HLINFO_LEN)); + } + + rfalCreateByteFlagsTxRxContext( + ctx, (uint8_t *)&gIsoDep.actv.attribReq, + (uint16_t)(RFAL_ISODEP_ATTRIB_HDR_LEN + + RFAL_MIN((uint16_t)HLInfoLen, RFAL_ISODEP_ATTRIB_HLINFO_LEN)), + (uint8_t *)gIsoDep.rxBuf, sizeof(rfalIsoDepAttribRes), &gIsoDep.rxBufLen, + RFAL_TXRX_FLAGS_DEFAULT, fwt); + return rfalStartTransceive(&ctx); +} + +/*******************************************************************************/ +static ReturnCode rfalIsoDepGetATTRIBStatus(void) { + ReturnCode ret; + + ret = rfalGetTransceiveStatus(); + if (ret == RFAL_ERR_NONE) { + gIsoDep.rxBufLen = rfalConvBitsToBytes(gIsoDep.rxBufLen); + + /* Check for valid ATTRIB Response Digital 2.3 15.6.2.1 */ + if ((gIsoDep.rxBufLen < RFAL_ISODEP_ATTRIB_RES_HDR_LEN)) { + return RFAL_ERR_PROTO; + } + + if (gIsoDep.rxLen8 != NULL) { + *gIsoDep.rxLen8 = (uint8_t)gIsoDep.rxBufLen; + } + + gIsoDep.ourFsx = + rfalIsoDepFSxI2FSx((uint8_t)(gIsoDep.actv.attribReq.Param.PARAM2 & + RFAL_ISODEP_ATTRIB_PARAM2_FSDI_MASK)); + } + + return ret; +} + +/*******************************************************************************/ +ReturnCode rfalIsoDepATTRIB(const uint8_t *nfcid0, uint8_t PARAM1, + rfalBitRate DSI, rfalBitRate DRI, + rfalIsoDepFSxI FSDI, uint8_t PARAM3, uint8_t DID, + const uint8_t *HLInfo, uint8_t HLInfoLen, + uint32_t fwt, rfalIsoDepAttribRes *attribRes, + uint8_t *attribResLen) { + ReturnCode ret; + + RFAL_EXIT_ON_ERR(ret, rfalIsoDepStartATTRIB(nfcid0, PARAM1, DSI, DRI, FSDI, + PARAM3, DID, HLInfo, HLInfoLen, + fwt, attribRes, attribResLen)); + rfalRunBlocking(ret, rfalIsoDepGetATTRIBStatus()); + + return ret; +} + +#endif /* RFAL_FEATURE_NFCB */ + +#if RFAL_FEATURE_NFCA + +/*******************************************************************************/ +ReturnCode rfalIsoDepPollAHandleActivation(rfalIsoDepFSxI FSDI, uint8_t DID, + rfalBitRate maxBR, + rfalIsoDepDevice *rfalIsoDepDev) { + ReturnCode ret; + + RFAL_EXIT_ON_ERR( + ret, rfalIsoDepPollAStartActivation(FSDI, DID, maxBR, rfalIsoDepDev)); + rfalRunBlocking(ret, rfalIsoDepPollAGetActivationStatus()); + + return ret; +} + +/*******************************************************************************/ +ReturnCode rfalIsoDepPollAStartActivation(rfalIsoDepFSxI FSDI, uint8_t DID, + rfalBitRate maxBR, + rfalIsoDepDevice *rfalIsoDepDev) { + ReturnCode ret; + + if (rfalIsoDepDev == NULL) { + return RFAL_ERR_PARAM; + } + + /* Enable EMD suppresssion|handling according to Digital 2.1 4.1.1.1 ; + * EMVCo 3.0 4.9.2 ; ISO 14443-3 8.3 */ + rfalSetErrorHandling(RFAL_ERRORHANDLING_EMD); + + /* Start RATS Transceive */ + RFAL_EXIT_ON_ERR( + ret, + rfalIsoDepStartRATS(FSDI, DID, &rfalIsoDepDev->activation.A.Listener.ATS, + &rfalIsoDepDev->activation.A.Listener.ATSLen)); + + rfalIsoDepDev->info.DSI = maxBR; + gIsoDep.actvDev = rfalIsoDepDev; + gIsoDep.cntRRetrys = gIsoDep.maxRetriesRATS; + gIsoDep.state = ISODEP_ST_PCD_ACT_RATS; + + return ret; +} + +/*******************************************************************************/ +ReturnCode rfalIsoDepPollAGetActivationStatus(void) { + ReturnCode ret; + uint8_t msgIt; + rfalBitRate maxBR; + + switch (gIsoDep.state) { + /*******************************************************************************/ + case ISODEP_ST_PCD_ACT_RATS: + + ret = rfalIsoDepGetRATSStatus(); + if (ret != RFAL_ERR_BUSY) { + if (ret != RFAL_ERR_NONE) { + /* EMVCo 2.6 9.6.1.1 & 9.6.1.2 If a timeout error is detected + * retransmit, on transmission error abort */ + if ((gIsoDep.compMode == RFAL_COMPLIANCE_MODE_EMV) && + (ret != RFAL_ERR_TIMEOUT)) { + break; + } + + if (gIsoDep.cntRRetrys != 0U) { + /* Ensure FDT before retransmission (reuse RFAL GT timer) */ + rfalSetGT(rfalGetFDTPoll()); + rfalFieldOnAndStartGT(); + + /* Send RATS retransmission */ /* PRQA S 4342 1 # MISRA 10.5 - + Layout of enum rfalIsoDepFSxI is + guaranteed whithin 4bit range */ + RFAL_EXIT_ON_ERR( + ret, + rfalIsoDepStartRATS( + (rfalIsoDepFSxI)(uint8_t)(gIsoDep.actv.ratsReq.PARAM >> + RFAL_ISODEP_RATS_PARAM_FSDI_SHIFT), + gIsoDep.did, &gIsoDep.actvDev->activation.A.Listener.ATS, + &gIsoDep.actvDev->activation.A.Listener.ATSLen)); + gIsoDep.cntRRetrys--; + ret = RFAL_ERR_BUSY; + } + /* Switch between NFC Forum and ISO14443-4 behaviour #595 + * ISO14443-4 5.6.1 If RATS fails, a Deactivation sequence should + * be performed as defined on clause 8 (ISO10373-6 Scenario H.2.5) + * Activity 1.1 9.6 Device Deactivation Activity is to be only + * performed when there's an active device */ + else if (gIsoDep.compMode == RFAL_COMPLIANCE_MODE_ISO) { + RFAL_EXIT_ON_ERR(ret, rfalIsoDepStartDeselect()); + + /* State ISODEP_ST_PCD_WAIT_DSL already set by + * rfalIsoDepHandleControlMsg DSL */ + return RFAL_ERR_BUSY; + } else { + /* MISRA 15.7 - Empty else */ + } + } else /* ATS received */ + { + maxBR = + gIsoDep.actvDev->info.DSI; /* Retrieve requested max bitrate */ + + /*******************************************************************************/ + /* Process ATS Response */ + gIsoDep.actvDev->info.FWI = + RFAL_ISODEP_FWI_DEFAULT; /* Default value EMVCo 2.6 5.7.2.6 */ + gIsoDep.actvDev->info.SFGI = 0U; + gIsoDep.actvDev->info.MBL = 0U; + gIsoDep.actvDev->info.DSI = RFAL_BR_106; + gIsoDep.actvDev->info.DRI = RFAL_BR_106; + gIsoDep.actvDev->info.FSxI = + (uint8_t)RFAL_ISODEP_FSXI_32; /* FSC default value is 32 bytes + ISO14443-A 5.2.3 */ + + /*******************************************************************************/ + /* Check for ATS optional fields */ + if (gIsoDep.actvDev->activation.A.Listener.ATS.TL > + RFAL_ISODEP_ATS_MIN_LEN) { + msgIt = RFAL_ISODEP_ATS_MIN_LEN; + + /* Format byte T0 is optional, if present assign FSDI */ + gIsoDep.actvDev->info.FSxI = + (gIsoDep.actvDev->activation.A.Listener.ATS.T0 & + RFAL_ISODEP_ATS_T0_FSCI_MASK); + + /* T0 has already been processed, always the same position */ + msgIt++; + + /* Check if TA is present */ + if ((gIsoDep.actvDev->activation.A.Listener.ATS.T0 & + RFAL_ISODEP_ATS_T0_TA_PRESENCE_MASK) != 0U) { + rfalIsoDepCalcBitRate(maxBR, + ((uint8_t *)&gIsoDep.actvDev->activation.A + .Listener.ATS)[msgIt++], + &gIsoDep.actvDev->info.DSI, + &gIsoDep.actvDev->info.DRI); + } + + /* Check if TB is present */ + if ((gIsoDep.actvDev->activation.A.Listener.ATS.T0 & + RFAL_ISODEP_ATS_T0_TB_PRESENCE_MASK) != 0U) { + gIsoDep.actvDev->info.SFGI = + ((uint8_t *)&gIsoDep.actvDev->activation.A.Listener + .ATS)[msgIt++]; + gIsoDep.actvDev->info.FWI = + (uint8_t)((gIsoDep.actvDev->info.SFGI >> + RFAL_ISODEP_ATS_TB_FWI_SHIFT) & + RFAL_ISODEP_ATS_FWI_MASK); + gIsoDep.actvDev->info.SFGI &= RFAL_ISODEP_ATS_TB_SFGI_MASK; + } + + /* Check if TC is present */ + if ((gIsoDep.actvDev->activation.A.Listener.ATS.T0 & + RFAL_ISODEP_ATS_T0_TC_PRESENCE_MASK) != 0U) { + /* Check for Protocol features support */ + /* Advanced protocol features defined on Digital 1.0 Table 69, + * removed after */ + gIsoDep.actvDev->info.supAdFt = + (((((uint8_t *)&gIsoDep.actvDev->activation.A.Listener + .ATS)[msgIt] & + RFAL_ISODEP_ATS_TC_ADV_FEAT) != 0U) + ? true + : false); + gIsoDep.actvDev->info.supDID = + (((((uint8_t *)&gIsoDep.actvDev->activation.A.Listener + .ATS)[msgIt] & + RFAL_ISODEP_ATS_TC_DID) != 0U) + ? true + : false); + gIsoDep.actvDev->info.supNAD = + (((((uint8_t *)&gIsoDep.actvDev->activation.A.Listener + .ATS)[msgIt++] & + RFAL_ISODEP_ATS_TC_NAD) != 0U) + ? true + : false); + } + } + + gIsoDep.actvDev->info.FSx = + rfalIsoDepFSxI2FSx(gIsoDep.actvDev->info.FSxI); + gIsoDep.fsx = gIsoDep.actvDev->info.FSx; + + gIsoDep.actvDev->info.SFGT = + rfalIsoDepSFGI2SFGT((uint8_t)gIsoDep.actvDev->info.SFGI); + + /* Ensure SFGT before following frame (reuse RFAL GT timer) */ + rfalSetGT(rfalConvMsTo1fc(gIsoDep.actvDev->info.SFGT)); + rfalFieldOnAndStartGT(); + + gIsoDep.actvDev->info.FWT = + rfalIsoDepFWI2FWT(gIsoDep.actvDev->info.FWI); + gIsoDep.actvDev->info.dFWT = RFAL_ISODEP_DFWT_20; + + gIsoDep.actvDev->info.DID = + ((gIsoDep.actvDev->info.supDID) ? gIsoDep.did + : RFAL_ISODEP_NO_DID); + gIsoDep.actvDev->info.NAD = RFAL_ISODEP_NO_NAD; + + /*******************************************************************************/ + /* If higher bit rates are supported by both devices, send PPS */ + if ((gIsoDep.actvDev->info.DSI != RFAL_BR_106) || + (gIsoDep.actvDev->info.DRI != RFAL_BR_106)) { + /* Send PPS */ /* PRQA S 0310 1 # MISRA 11.3 - Intentional safe + cast to avoiding buffer duplication */ + RFAL_EXIT_ON_ERR( + ret, rfalIsoDepStartPPS(gIsoDep.actvDev->info.DID, + gIsoDep.actvDev->info.DSI, + gIsoDep.actvDev->info.DRI, + (rfalIsoDepPpsRes *)&gIsoDep.ctrlBuf)); + + gIsoDep.state = ISODEP_ST_PCD_ACT_PPS; + return RFAL_ERR_BUSY; + } + + return RFAL_ERR_NONE; + } + } + break; + + /*******************************************************************************/ + case ISODEP_ST_PCD_ACT_PPS: + + ret = rfalIsoDepGetPPSSTatus(); + if (ret != RFAL_ERR_BUSY) { + /* Check whether PPS has been acknowledge */ + if (ret == RFAL_ERR_NONE) { + /* DSI code the divisor from PICC to PCD */ + /* DRI code the divisor from PCD to PICC */ + rfalSetBitRate(gIsoDep.actvDev->info.DRI, gIsoDep.actvDev->info.DSI); + } else { + /* If PPS has faled keep activation bit rate */ + gIsoDep.actvDev->info.DSI = RFAL_BR_106; + gIsoDep.actvDev->info.DRI = RFAL_BR_106; + + /* Ignore PPS response fail, proceed to data exchange */ + ret = RFAL_ERR_NONE; + } + } + break; + + /*******************************************************************************/ + case ISODEP_ST_PCD_WAIT_DSL: + + ret = rfalIsoDepGetDeselectStatus(); + if (ret != RFAL_ERR_BUSY) { + /* Report activation failed with generic tranmission error */ + ret = RFAL_ERR_FRAMING; + } + break; + + /*******************************************************************************/ + default: + ret = RFAL_ERR_WRONG_STATE; + break; + } + + return ret; +} +#endif /* RFAL_FEATURE_NFCA */ + +#if RFAL_FEATURE_NFCB + +/*******************************************************************************/ +ReturnCode rfalIsoDepPollBHandleActivation(rfalIsoDepFSxI FSDI, uint8_t DID, + rfalBitRate maxBR, uint8_t PARAM1, + const rfalNfcbListenDevice *nfcbDev, + const uint8_t *HLInfo, + uint8_t HLInfoLen, + rfalIsoDepDevice *rfalIsoDepDev) { + ReturnCode ret; + + RFAL_EXIT_ON_ERR( + ret, rfalIsoDepPollBStartActivation(FSDI, DID, maxBR, PARAM1, nfcbDev, + HLInfo, HLInfoLen, rfalIsoDepDev)); + rfalRunBlocking(ret, rfalIsoDepPollBGetActivationStatus()); + + return ret; +} + +/*******************************************************************************/ +ReturnCode rfalIsoDepPollBStartActivation(rfalIsoDepFSxI FSDI, uint8_t DID, + rfalBitRate maxBR, uint8_t PARAM1, + const rfalNfcbListenDevice *nfcbDev, + const uint8_t *HLInfo, + uint8_t HLInfoLen, + rfalIsoDepDevice *rfalIsoDepDev) { + ReturnCode ret; + uint32_t tr2; + + /***************************************************************************/ + /* Initialize ISO-DEP Device with info from SENSB_RES */ + rfalIsoDepDev->info.FWI = + ((nfcbDev->sensbRes.protInfo.FwiAdcFo >> RFAL_NFCB_SENSB_RES_FWI_SHIFT) & + RFAL_NFCB_SENSB_RES_FWI_MASK); + rfalIsoDepDev->info.FWT = rfalIsoDepFWI2FWT(rfalIsoDepDev->info.FWI); + rfalIsoDepDev->info.dFWT = RFAL_NFCB_DFWT; + rfalIsoDepDev->info.SFGI = (((uint32_t)nfcbDev->sensbRes.protInfo.SFGI >> + RFAL_NFCB_SENSB_RES_SFGI_SHIFT) & + RFAL_NFCB_SENSB_RES_SFGI_MASK); + rfalIsoDepDev->info.SFGT = + rfalIsoDepSFGI2SFGT((uint8_t)rfalIsoDepDev->info.SFGI); + rfalIsoDepDev->info.FSxI = ((nfcbDev->sensbRes.protInfo.FsciProType >> + RFAL_NFCB_SENSB_RES_FSCI_SHIFT) & + RFAL_NFCB_SENSB_RES_FSCI_MASK); + rfalIsoDepDev->info.FSx = rfalIsoDepFSxI2FSx(rfalIsoDepDev->info.FSxI); + rfalIsoDepDev->info.DID = DID; + rfalIsoDepDev->info.supDID = (((nfcbDev->sensbRes.protInfo.FwiAdcFo & + RFAL_NFCB_SENSB_RES_FO_DID_MASK) != 0U) + ? true + : false); + rfalIsoDepDev->info.supNAD = (((nfcbDev->sensbRes.protInfo.FwiAdcFo & + RFAL_NFCB_SENSB_RES_FO_NAD_MASK) != 0U) + ? true + : false); + + /* Check if DID requested is supported by PICC */ + if ((DID != RFAL_ISODEP_NO_DID) && (!rfalIsoDepDev->info.supDID)) { + return RFAL_ERR_PARAM; + } + + /* Enable EMD suppresssion|handling according to Digital 2.1 4.1.1.1 ; + * EMVCo 3.0 4.9.2 ; ISO 14443-3 8.3 */ + rfalSetErrorHandling(RFAL_ERRORHANDLING_EMD); + + /***************************************************************************/ + /* Set FDT Poll to be used on upcoming communications */ + if (gIsoDep.compMode == RFAL_COMPLIANCE_MODE_EMV) { + /* Disregard Minimum TR2 returned by PICC, always use FDTb MIN + * EMVCo 3.0 6.3.2.10 */ + rfalSetFDTPoll(RFAL_FDT_POLL_NFCB_POLLER); + } else { + tr2 = rfalNfcbTR2ToFDT(((nfcbDev->sensbRes.protInfo.FsciProType >> + RFAL_NFCB_SENSB_RES_PROTO_TR2_SHIFT) & + RFAL_NFCB_SENSB_RES_PROTO_TR2_MASK)); + if (rfalGetFDTPoll() < tr2) { + /* In case TR2 is longer than the one currently running, ensure it's + * fulfilled (max: 9472/fc => 700us) */ + platformDelay(1); + } + + /* Apply minimum TR2 from SENSB_RES Digital 2.1 7.6.2.23 */ + rfalSetFDTPoll(tr2); + } + + /* Calculate max Bit Rate */ + rfalIsoDepCalcBitRate(maxBR, nfcbDev->sensbRes.protInfo.BRC, + &rfalIsoDepDev->info.DSI, &rfalIsoDepDev->info.DRI); + + /***************************************************************************/ + /* Send ATTRIB Command */ + RFAL_EXIT_ON_ERR( + ret, + rfalIsoDepStartATTRIB( + (const uint8_t *)&nfcbDev->sensbRes.nfcid0, + (((nfcbDev->sensbRes.protInfo.FwiAdcFo & + RFAL_NFCB_SENSB_RES_ADC_ADV_FEATURE_MASK) != 0U) + ? PARAM1 + : RFAL_ISODEP_ATTRIB_REQ_PARAM1_DEFAULT), + rfalIsoDepDev->info.DSI, rfalIsoDepDev->info.DRI, FSDI, + (gIsoDep.compMode == RFAL_COMPLIANCE_MODE_EMV) + ? RFAL_NFCB_SENSB_RES_PROTO_ISO_MASK + : (nfcbDev->sensbRes.protInfo.FsciProType & + ((RFAL_NFCB_SENSB_RES_PROTO_TR2_MASK + << RFAL_NFCB_SENSB_RES_PROTO_TR2_SHIFT) | + RFAL_NFCB_SENSB_RES_PROTO_ISO_MASK)), /* EMVCo 2.6 6.4.1.9 */ + DID, HLInfo, HLInfoLen, + (rfalIsoDepDev->info.FWT + rfalIsoDepDev->info.dFWT), + &rfalIsoDepDev->activation.B.Listener.ATTRIB_RES, + &rfalIsoDepDev->activation.B.Listener.ATTRIB_RESLen)); + + gIsoDep.actvDev = rfalIsoDepDev; + return ret; +} + +/*******************************************************************************/ +ReturnCode rfalIsoDepPollBGetActivationStatus(void) { + ReturnCode ret; + uint8_t mbli; + + /***************************************************************************/ + /* Process ATTRIB Response */ + ret = rfalIsoDepGetATTRIBStatus(); + if (ret != RFAL_ERR_BUSY) { + if (ret == RFAL_ERR_NONE) { + /* Digital 1.1 14.6.2.3 - Check if received DID match */ + uint8_t expected_did = + ((RFAL_ISODEP_NO_DID == gIsoDep.did) ? RFAL_ISODEP_DID_00 + : gIsoDep.did); + if ((gIsoDep.actvDev->activation.B.Listener.ATTRIB_RES.mbliDid & + RFAL_ISODEP_ATTRIB_RES_DID_MASK) != expected_did) { + return RFAL_ERR_PROTO; + } + + /* Retrieve MBLI and calculate new FDS/MBL (Maximum Buffer Length) */ + mbli = ((gIsoDep.actvDev->activation.B.Listener.ATTRIB_RES.mbliDid >> + RFAL_ISODEP_ATTRIB_RES_MBLI_SHIFT) & + RFAL_ISODEP_ATTRIB_RES_MBLI_MASK); + if (mbli > 0U) { + /* Digital 1.1 14.6.2 Calculate Maximum Buffer Length MBL = FSC x + * 2^(MBLI-1) */ + gIsoDep.actvDev->info.MBL = + (gIsoDep.actvDev->info.FSx * ((uint32_t)1U << (mbli - 1U))); + } + + /* DSI code the divisor from PICC to PCD */ + /* DRI code the divisor from PCD to PICC */ + rfalSetBitRate(gIsoDep.actvDev->info.DRI, gIsoDep.actvDev->info.DSI); + + /* REMARK: SoF EoF TR0 and TR1 are not passed on to RF layer */ + + /* Start the SFGT timer (reuse RFAL GT timer) */ + rfalSetGT(rfalConvMsTo1fc(gIsoDep.actvDev->info.SFGT)); + rfalFieldOnAndStartGT(); + } else { + gIsoDep.actvDev->info.DSI = RFAL_BR_106; + gIsoDep.actvDev->info.DRI = RFAL_BR_106; + } + + /*******************************************************************************/ + /* Store already FS info, rfalIsoDepGetMaxInfLen() may be called before + * setting TxRx params */ + gIsoDep.fsx = gIsoDep.actvDev->info.FSx; + } + + return ret; +} + +#endif /* RFAL_FEATURE_NFCB */ + +/*******************************************************************************/ +ReturnCode rfalIsoDepPollHandleSParameters(rfalIsoDepDevice *rfalIsoDepDev, + rfalBitRate maxTxBR, + rfalBitRate maxRxBR) { + uint8_t it; + uint8_t supPCD2PICC; + uint8_t supPICC2PCD; + uint8_t currenttxBR; + uint8_t currentrxBR; + rfalBitRate txBR; + rfalBitRate rxBR; + uint16_t rcvLen; + ReturnCode ret; + rfalIsoDepControlMsgSParam sParam; + + if ((rfalIsoDepDev == NULL) || (maxTxBR > RFAL_BR_13560) || + (maxRxBR > RFAL_BR_13560)) { + return RFAL_ERR_PARAM; + } + + it = 0; + supPICC2PCD = 0x00; + supPCD2PICC = 0x00; + txBR = RFAL_BR_106; + rxBR = RFAL_BR_106; + sParam.pcb = ISODEP_PCB_SPARAMETERS; + + /*******************************************************************************/ + /* Send S(PARAMETERS) - Block Info */ + sParam.sParam.tag = RFAL_ISODEP_SPARAM_TAG_BLOCKINFO; + sParam.sParam.value[it++] = RFAL_ISODEP_SPARAM_TAG_BRREQ; + sParam.sParam.value[it++] = RFAL_ISODEP_SPARAM_TAG_BRREQ_LEN; + sParam.sParam.length = it; + + /* Send S(PARAMETERS). Use a fixed FWI of 4 ISO14443-4 2016 7.2 */ + RFAL_EXIT_ON_ERR( + ret, rfalTransceiveBlockingTxRx( + (uint8_t *)&sParam, (RFAL_ISODEP_SPARAM_HDR_LEN + (uint16_t)it), + (uint8_t *)&sParam, sizeof(rfalIsoDepControlMsgSParam), &rcvLen, + RFAL_TXRX_FLAGS_DEFAULT, ISODEP_FWT_DEACTIVATION)); + + it = 0; + + /*******************************************************************************/ + /* Check S(PARAMETERS) response */ + if ((sParam.pcb != ISODEP_PCB_SPARAMETERS) || + (sParam.sParam.tag != RFAL_ISODEP_SPARAM_TAG_BLOCKINFO) || + (sParam.sParam.value[it] != RFAL_ISODEP_SPARAM_TAG_BRIND) || + (rcvLen < RFAL_ISODEP_SPARAM_HDR_LEN) || + (rcvLen != + ((uint16_t)sParam.sParam.length + RFAL_ISODEP_SPARAM_HDR_LEN))) { + return RFAL_ERR_PROTO; + } + + /* Retrieve PICC's bit rate PICC capabilities */ + for (it = 0; it < (rcvLen - (uint16_t)RFAL_ISODEP_SPARAM_TAG_LEN); it++) { + if ((sParam.sParam.value[it] == RFAL_ISODEP_SPARAM_TAG_SUP_PCD2PICC) && + (sParam.sParam.value[it + (uint16_t)RFAL_ISODEP_SPARAM_TAG_LEN] == + RFAL_ISODEP_SPARAM_TAG_PCD2PICC_LEN)) { + supPCD2PICC = + sParam.sParam.value[it + RFAL_ISODEP_SPARAM_TAG_PCD2PICC_LEN]; + } + + if ((sParam.sParam.value[it] == RFAL_ISODEP_SPARAM_TAG_SUP_PICC2PCD) && + (sParam.sParam.value[it + (uint16_t)RFAL_ISODEP_SPARAM_TAG_LEN] == + RFAL_ISODEP_SPARAM_TAG_PICC2PCD_LEN)) { + supPICC2PCD = + sParam.sParam.value[it + RFAL_ISODEP_SPARAM_TAG_PICC2PCD_LEN]; + } + } + + /*******************************************************************************/ + /* Check if requested bit rates are supported by PICC */ + if ((supPICC2PCD == 0x00U) || (supPCD2PICC == 0x00U)) { + return RFAL_ERR_PROTO; + } + + for (it = 0; it <= (uint8_t)maxTxBR; it++) { + if ((supPCD2PICC & (0x01U << it)) != 0U) { + txBR = + (rfalBitRate)it; /* PRQA S 4342 # MISRA 10.5 - Layout of enum + rfalBitRate and above clamping of maxTxBR + guarantee no invalid enum values to be created */ + } + } + for (it = 0; it <= (uint8_t)maxRxBR; it++) { + if ((supPICC2PCD & (0x01U << it)) != 0U) { + rxBR = + (rfalBitRate)it; /* PRQA S 4342 # MISRA 10.5 - Layout of enum + rfalBitRate and above clamping of maxTxBR + guarantee no invalid enum values to be created */ + } + } + + it = 0; + currenttxBR = (uint8_t)txBR; + currentrxBR = (uint8_t)rxBR; + + /*******************************************************************************/ + /* Send S(PARAMETERS) - Bit rates Activation */ + sParam.sParam.tag = RFAL_ISODEP_SPARAM_TAG_BLOCKINFO; + sParam.sParam.value[it++] = RFAL_ISODEP_SPARAM_TAG_BRACT; + sParam.sParam.value[it++] = + (RFAL_ISODEP_SPARAM_TVL_HDR_LEN + RFAL_ISODEP_SPARAM_TAG_PCD2PICC_LEN + + RFAL_ISODEP_SPARAM_TVL_HDR_LEN + RFAL_ISODEP_SPARAM_TAG_PICC2PCD_LEN); + sParam.sParam.value[it++] = RFAL_ISODEP_SPARAM_TAG_SEL_PCD2PICC; + sParam.sParam.value[it++] = RFAL_ISODEP_SPARAM_TAG_PCD2PICC_LEN; + sParam.sParam.value[it++] = ((uint8_t)0x01U << currenttxBR); + sParam.sParam.value[it++] = 0x00U; + sParam.sParam.value[it++] = RFAL_ISODEP_SPARAM_TAG_SEL_PICC2PCD; + sParam.sParam.value[it++] = RFAL_ISODEP_SPARAM_TAG_PICC2PCD_LEN; + sParam.sParam.value[it++] = ((uint8_t)0x01U << currentrxBR); + sParam.sParam.value[it++] = 0x00U; + sParam.sParam.length = it; + + RFAL_EXIT_ON_ERR( + ret, rfalTransceiveBlockingTxRx( + (uint8_t *)&sParam, (RFAL_ISODEP_SPARAM_HDR_LEN + (uint16_t)it), + (uint8_t *)&sParam, sizeof(rfalIsoDepControlMsgSParam), &rcvLen, + RFAL_TXRX_FLAGS_DEFAULT, + (rfalIsoDepDev->info.FWT + rfalIsoDepDev->info.dFWT))); + + it = 0; + + /*******************************************************************************/ + /* Check S(PARAMETERS) Acknowledge */ + if ((sParam.pcb != ISODEP_PCB_SPARAMETERS) || + (sParam.sParam.tag != RFAL_ISODEP_SPARAM_TAG_BLOCKINFO) || + (sParam.sParam.value[it] != RFAL_ISODEP_SPARAM_TAG_BRACK) || + (rcvLen < RFAL_ISODEP_SPARAM_HDR_LEN)) { + return RFAL_ERR_PROTO; + } + + RFAL_EXIT_ON_ERR(ret, rfalSetBitRate(txBR, rxBR)); + + rfalIsoDepDev->info.DRI = txBR; + rfalIsoDepDev->info.DSI = rxBR; + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +static void rfalIsoDepCalcBitRate(rfalBitRate maxAllowedBR, + uint8_t piccBRCapability, rfalBitRate *dsi, + rfalBitRate *dri) { + uint8_t driMask; + uint8_t dsiMask; + int16_t i; + bool bitrateFound; + rfalBitRate curMaxBR; + + curMaxBR = maxAllowedBR; + + do { + bitrateFound = true; + + (*dsi) = RFAL_BR_106; + (*dri) = RFAL_BR_106; + + /* Digital 1.0 5.6.2.5 & 11.6.2.14: A received RFU value of b4 = 1b MUST be + * interpreted as if b7 to b1 ? 0000000b (only 106 kbits/s in both + * direction) */ + if (((RFAL_ISODEP_BITRATE_RFU_MASK & piccBRCapability) != 0U) || + (curMaxBR > RFAL_BR_848) || (curMaxBR == RFAL_BR_KEEP)) { + return; + } + + /***************************************************************************/ + /* Determine Listen->Poll bit rate */ + dsiMask = (piccBRCapability & RFAL_ISODEP_BSI_MASK); + for (i = 2; i >= 0; i--) /* Check supported bit rate from the highest */ + { + if (((dsiMask & (0x10U << (uint8_t)i)) != 0U) && + (((uint8_t)i + 1U) <= (uint8_t)curMaxBR)) { + const uint8_t newdsi = ((uint8_t)i) + 1U; + (*dsi) = (rfalBitRate) + newdsi; /* PRQA S 4342 # MISRA 10.5 - Layout of enum rfalBitRate and + range of loop variable guarantee no invalid enum values + to be created */ + break; + } + } + + /***************************************************************************/ + /* Determine Poll->Listen bit rate */ + driMask = (piccBRCapability & RFAL_ISODEP_BRI_MASK); + for (i = 2; i >= 0; i--) /* Check supported bit rate from the highest */ + { + if (((driMask & (0x01U << (uint8_t)i)) != 0U) && + (((uint8_t)i + 1U) <= (uint8_t)curMaxBR)) { + const uint8_t newdri = ((uint8_t)i) + 1U; + (*dri) = (rfalBitRate) + newdri; /* PRQA S 4342 # MISRA 10.5 - Layout of enum rfalBitRate and + range of loop variable guarantee no invalid enum values + to be created */ + break; + } + } + + /***************************************************************************/ + /* Check if different bit rate is supported */ + + /* Digital 1.0 Table 67: if b8=1b, then only the same bit rate divisor for + * both directions is supported */ + if ((piccBRCapability & RFAL_ISODEP_SAME_BITRATE_MASK) != 0U) { + (*dsi) = RFAL_MIN((*dsi), (*dri)); + (*dri) = (*dsi); + /* Check that the baudrate is supported */ + if ((RFAL_BR_106 != (*dsi)) && + (!(((dsiMask & (0x10U << ((uint8_t)(*dsi) - 1U))) != 0U) && + ((driMask & (0x01U << ((uint8_t)(*dri) - 1U))) != 0U)))) { + bitrateFound = false; + curMaxBR = (*dsi); /* set allowed bitrate to be lowest and determine bit + rate again */ + } + } + } while (!(bitrateFound)); +} + +/*******************************************************************************/ +static uint32_t rfalIsoDepSFGI2SFGT(uint8_t sfgi) { + uint32_t sfgt; + uint8_t tmpSFGI; + + tmpSFGI = sfgi; + + if (tmpSFGI > ISODEP_SFGI_MAX) { + tmpSFGI = ISODEP_SFGI_MIN; + } + + if (tmpSFGI != ISODEP_SFGI_MIN) { + /* If sfgi != 0 wait SFGT + dSFGT Digital 1.1 13.8.2.1 */ + sfgt = rfalIsoDepCalcSGFT(sfgi) + rfalIsoDepCalcdSGFT(sfgi); + } + /* Otherwise use FDTPoll min Digital 1.1 13.8.2.3*/ + else { + sfgt = RFAL_FDT_POLL_NFCA_POLLER; + } + + /* Convert carrier cycles to milli seconds */ + return (rfalConv1fcToMs(sfgt) + 1U); +} + +#endif /* RFAL_FEATURE_ISO_DEP_POLL */ + +/*******************************************************************************/ +static void rfalIsoDepApdu2IBLockParam(rfalIsoDepApduTxRxParam apduParam, + rfalIsoDepTxRxParam *iBlockParam, + uint16_t txPos, uint16_t rxPos) { + RFAL_NO_WARNING(rxPos); /* Keep this param for future use */ + + iBlockParam->DID = apduParam.DID; + iBlockParam->FSx = apduParam.FSx; + iBlockParam->ourFSx = apduParam.ourFSx; + iBlockParam->FWT = apduParam.FWT; + iBlockParam->dFWT = apduParam.dFWT; + + if ((apduParam.txBufLen - txPos) > rfalIsoDepGetMaxInfLen()) { + iBlockParam->isTxChaining = true; + iBlockParam->txBufLen = rfalIsoDepGetMaxInfLen(); + } else { + iBlockParam->isTxChaining = false; + iBlockParam->txBufLen = (apduParam.txBufLen - txPos); + } + + /* TxBuf is moved to the beginning for every I-Block */ + iBlockParam->txBuf = + (rfalIsoDepBufFormat *) + apduParam.txBuf; /* PRQA S 0310 # MISRA 11.3 - Intentional safe cast + to avoiding large buffer duplication */ + iBlockParam->rxBuf = + apduParam.tmpBuf; /* Simply using the apdu buffer is not possible because + of current ACK handling */ + iBlockParam->isRxChaining = &gIsoDep.isAPDURxChaining; + iBlockParam->rxLen = apduParam.rxLen; +} + +/*******************************************************************************/ +ReturnCode rfalIsoDepStartApduTransceive(rfalIsoDepApduTxRxParam param) { + rfalIsoDepTxRxParam txRxParam; + + /* Initialize and store APDU context */ + gIsoDep.APDUParam = param; + gIsoDep.APDUTxPos = 0; + gIsoDep.APDURxPos = 0; + + /* Assign current FSx to calculate INF length (only change the FSx from + * activation if no to Keep) */ + gIsoDep.ourFsx = + ((param.ourFSx != RFAL_ISODEP_FSX_KEEP) ? param.ourFSx : gIsoDep.ourFsx); + gIsoDep.fsx = param.FSx; + + /* Convert APDU TxRxParams to I-Block TxRxParams */ + rfalIsoDepApdu2IBLockParam(gIsoDep.APDUParam, &txRxParam, gIsoDep.APDUTxPos, + gIsoDep.APDURxPos); + + return rfalIsoDepStartTransceive(txRxParam); +} + +/*******************************************************************************/ +ReturnCode rfalIsoDepGetApduTransceiveStatus(void) { + ReturnCode ret; + rfalIsoDepTxRxParam txRxParam; + + ret = rfalIsoDepGetTransceiveStatus(); + switch (ret) { + /*******************************************************************************/ + case RFAL_ERR_NONE: + + /* Check if we are still doing chaining on Tx */ + if (gIsoDep.isTxChaining) { + /* Add already Tx bytes */ + gIsoDep.APDUTxPos += gIsoDep.txBufLen; + + /* Convert APDU TxRxParams to I-Block TxRxParams */ + rfalIsoDepApdu2IBLockParam(gIsoDep.APDUParam, &txRxParam, + gIsoDep.APDUTxPos, gIsoDep.APDURxPos); + + if (txRxParam.txBufLen > 0U) /* MISRA 21.18 */ + { + /* Move next I-Block to beginning of APDU Tx buffer */ + RFAL_MEMCPY(gIsoDep.APDUParam.txBuf->apdu, + &gIsoDep.APDUParam.txBuf->apdu[gIsoDep.APDUTxPos], + txRxParam.txBufLen); + } + + RFAL_EXIT_ON_ERR(ret, rfalIsoDepStartTransceive(txRxParam)); + return RFAL_ERR_BUSY; + } + + /* APDU TxRx is done */ + /* fall through */ + + /*******************************************************************************/ + case RFAL_ERR_AGAIN: /* PRQA S 2003 # MISRA 16.3 - Intentional fall through + */ + + /* Check if no APDU transceive has been started before (data from + * rfalIsoDepListenStartActivation) */ + if (gIsoDep.APDUParam.rxLen == NULL) { + if (ret == RFAL_ERR_AGAIN) { + /* In Listen mode first chained packet cannot be retrieved via APDU + * interface */ + return RFAL_ERR_NOTSUPP; + } + + /* TxRx is complete and full data is already available */ + return RFAL_ERR_NONE; + } + + if (*gIsoDep.APDUParam.rxLen > 0U) /* MISRA 21.18 */ + { + /* Ensure that data in tmpBuf still fits into APDU buffer */ + if ((gIsoDep.APDURxPos + (*gIsoDep.APDUParam.rxLen)) > + (uint16_t)RFAL_FEATURE_ISO_DEP_APDU_MAX_LEN) { + return RFAL_ERR_NOMEM; + } + + /* Copy chained packet from tmp buffer to APDU buffer */ + RFAL_MEMCPY(&gIsoDep.APDUParam.rxBuf->apdu[gIsoDep.APDURxPos], + gIsoDep.APDUParam.tmpBuf->inf, *gIsoDep.APDUParam.rxLen); + gIsoDep.APDURxPos += *gIsoDep.APDUParam.rxLen; + } + + /* Update output param rxLen */ + *gIsoDep.APDUParam.rxLen = gIsoDep.APDURxPos; + + /* Wait for following I-Block or APDU TxRx has finished */ + return ((ret == RFAL_ERR_AGAIN) ? RFAL_ERR_BUSY : RFAL_ERR_NONE); + + /*******************************************************************************/ + default: + /* MISRA 16.4: no empty default statement (a comment being enough) */ + break; + } + + return ret; +} + +#endif /* RFAL_FEATURE_ISO_DEP */ diff --git a/core/embed/io/nfc/rfal/source/rfal_nfc.c b/core/embed/io/nfc/rfal/source/rfal_nfc.c new file mode 100644 index 0000000000..565eed021c --- /dev/null +++ b/core/embed/io/nfc/rfal/source/rfal_nfc.c @@ -0,0 +1,2451 @@ + +/****************************************************************************** + * @attention + * + * COPYRIGHT 2020 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/*! \file rfal_nfc.c + * + * \author Gustavo Patricio + * + * \brief RFAL NFC device + * + * This module provides the required features to behave as an NFC Poller + * or Listener device. It grants an easy to use interface for the following + * activities: Technology Detection, Collision Resolution, Activation, + * Data Exchange, and Deactivation + * + * This layer is influenced by (but not fully aligned with) the NFC Forum + * specifications, in particular: Activity 2.0 and NCI 2.0 + * + */ + +/* + ****************************************************************************** + * INCLUDES + ****************************************************************************** + */ +#include "rfal_nfc.h" +#include "rfal_analogConfig.h" +#include "rfal_utils.h" + +/* +****************************************************************************** +* GLOBAL DEFINES +****************************************************************************** +*/ +#define RFAL_NFC_MAX_DEVICES 5U /*!< Max number of devices supported */ +#define RFAL_NFC_T_FIELD_OFF \ + 5U /*!< tFIELD_OFF minimal duration Activity 2.2 Table 26 */ + +/* +****************************************************************************** +* GLOBAL MACROS +****************************************************************************** +*/ + +#define rfalNfcNfcNotify(st) \ + if (gNfcDev.disc.notifyCb != NULL) gNfcDev.disc.notifyCb((st)) + +#define rfalNfcpCbPollerInitialize() \ + ((gNfcDev.disc.propNfc.rfalNfcpPollerInitialize != NULL) \ + ? gNfcDev.disc.propNfc.rfalNfcpPollerInitialize() \ + : RFAL_ERR_NOTSUPP) +#define rfalNfcpCbPollerTechnologyDetection() \ + ((gNfcDev.disc.propNfc.rfalNfcpPollerTechnologyDetection != NULL) \ + ? gNfcDev.disc.propNfc.rfalNfcpPollerTechnologyDetection() \ + : RFAL_ERR_TIMEOUT) +#define rfalNfcpCbPollerStartCollisionResolution() \ + ((gNfcDev.disc.propNfc.rfalNfcpPollerStartCollisionResolution != NULL) \ + ? gNfcDev.disc.propNfc.rfalNfcpPollerStartCollisionResolution() \ + : RFAL_ERR_NOTSUPP) +#define rfalNfcpCbPollerGetCollisionResolutionStatus() \ + ((gNfcDev.disc.propNfc.rfalNfcpPollerGetCollisionResolutionStatus != NULL) \ + ? gNfcDev.disc.propNfc.rfalNfcpPollerGetCollisionResolutionStatus() \ + : RFAL_ERR_NOTSUPP) +#define rfalNfcpCbStartActivation() \ + ((gNfcDev.disc.propNfc.rfalNfcpStartActivation != NULL) \ + ? gNfcDev.disc.propNfc.rfalNfcpStartActivation() \ + : RFAL_ERR_NOTSUPP) +#define rfalNfcpCbGetActivationStatus() \ + ((gNfcDev.disc.propNfc.rfalNfcpGetActivationStatus != NULL) \ + ? gNfcDev.disc.propNfc.rfalNfcpGetActivationStatus() \ + : RFAL_ERR_NOTSUPP) + +#define rfalNfcHasPollerTechs() \ + ((gNfcDev.disc.techs2Find & \ + (RFAL_NFC_POLL_TECH_A | RFAL_NFC_POLL_TECH_B | RFAL_NFC_POLL_TECH_F | \ + RFAL_NFC_POLL_TECH_V | RFAL_NFC_POLL_TECH_AP2P | \ + RFAL_NFC_POLL_TECH_ST25TB | RFAL_NFC_POLL_TECH_PROP)) != 0U) + +/* +****************************************************************************** +* GLOBAL TYPES +****************************************************************************** +*/ + +/*! Buffer union, only one interface is used at a time */ +typedef union { /*PRQA S 0750 # MISRA 19.2 - Members of the union will not be + used concurrently, only one interface at a time */ + rfalIsoDepBufFormat + isoDepBuf; /*!< ISO-DEP buffer format (with header/prologue) */ + rfalNfcDepBufFormat + nfcDepBuf; /*!< NFC-DEP buffer format (with header/prologue) */ +} rfalNfcTmpBuffer; + +/*! RFAL NFC instance */ +typedef struct { + rfalNfcState state; /*!< Main state */ + uint16_t techsFound; /*!< Technologies found bitmask */ + uint16_t techs2do; /*!< Technologies still to be performed */ + uint16_t techDctCnt; /*!< Technologies detection counter (before WU) */ + rfalBitRate ap2pBR; /*!< Bit rate to poll for AP2P */ + uint8_t selDevIdx; /*!< Selected device index */ + rfalNfcDevice *activeDev; /*!< Active device pointer */ + rfalNfcDiscoverParam disc; /*!< Discovery parameters */ + rfalNfcDevice devList[RFAL_NFC_MAX_DEVICES]; /*!< Location of device list */ + uint8_t devCnt; /*!< Decices found counter */ + uint32_t discTmr; /*!< Discovery Total duration timer */ + ReturnCode dataExErr; /*!< Last Data Exchange error */ + rfalNfcDeactivateType deactType; /*!< Deactivation type */ + bool isRxChaining; /*!< Flag indicating Other device is chaining */ + uint32_t lmMask; /*!< Listen Mode mask */ + bool isFieldOn; /*!< Flag indicating Fieldon for Passive Poll */ + bool isTechInit; /*!< Flag indicating technology has been set */ + bool isOperOngoing; /*!< Flag indicating operation is ongoing */ + bool isDeactivating; /*!< Flag indicating deactivation is ongoing */ + + rfalNfcaSensRes sensRes; /*!< SENS_RES during card detection and activation */ + rfalNfcbSensbRes + sensbRes; /*!< SENSB_RES during card detection and activation */ + uint8_t sensbResLen; /*!< SENSB_RES length */ + + rfalNfcBuffer txBuf; /*!< Tx buffer for Data Exchange */ + rfalNfcBuffer rxBuf; /*!< Rx buffer for Data Exchange */ + uint16_t rxLen; /*!< Length of received data on Data Exchange */ + +#if RFAL_FEATURE_NFC_DEP || RFAL_FEATURE_ISO_DEP + rfalNfcTmpBuffer tmpBuf; /*!< Tmp buffer for Data Exchange */ +#endif /* RFAL_FEATURE_NFC_DEP || RFAL_FEATURE_ISO_DEP */ + +} rfalNfc; + +/* + ****************************************************************************** + * LOCAL VARIABLES + ****************************************************************************** + */ +#ifdef RFAL_TEST_MODE +rfalNfc gNfcDev; +#else /* RFAL_TEST_MODE */ +static rfalNfc gNfcDev; +#endif /* RFAL_TEST_MODE */ + +/* +****************************************************************************** +* LOCAL FUNCTION PROTOTYPES +****************************************************************************** +*/ +static ReturnCode rfalNfcPollTechDetection(void); +static ReturnCode rfalNfcPollCollResolution(void); +static ReturnCode rfalNfcPollActivation(uint8_t devIt); +static ReturnCode rfalNfcDeactivation(void); + +#if RFAL_FEATURE_NFC_DEP +static ReturnCode rfalNfcNfcDepActivate(rfalNfcDevice *device, + rfalNfcDepCommMode commMode, + const uint8_t *atrReq, + uint16_t atrReqLen); +#endif /* RFAL_FEATURE_NFC_DEP */ + +#if RFAL_FEATURE_LISTEN_MODE +static ReturnCode rfalNfcListenActivation(void); +#endif /* RFAL_FEATURE_LISTEN_MODE*/ + +/*******************************************************************************/ +ReturnCode rfalNfcInitialize(void) { + ReturnCode err; + + gNfcDev.state = RFAL_NFC_STATE_NOTINIT; + +#ifdef RFAL_FEATURE_DYNAMIC_ANALOG_CONFIG + /* Initialize the AC if not already done */ + /* Prevents reseting to default AC table in case another has been loaded */ + /* meanwhile - For use cases where a dynamic AC is loaded at run time */ + if ((!rfalAnalogConfigIsReady())) +#endif /* RFAL_FEATURE_DYNAMIC_ANALOG_CONFIG */ + { + rfalAnalogConfigInitialize(); /* Initialize RFAL's Analog Configs */ + } + + RFAL_EXIT_ON_ERR(err, rfalInitialize()); /* Initialize RFAL */ + + RFAL_MEMSET(&gNfcDev, 0x00, sizeof(gNfcDev)); + + gNfcDev.state = RFAL_NFC_STATE_IDLE; /* Go to initialized */ + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalNfcDiscover(const rfalNfcDiscoverParam *disParams) { + /* Check if initialization has been performed */ + if (gNfcDev.state != RFAL_NFC_STATE_IDLE) { + return RFAL_ERR_WRONG_STATE; + } + + /* Check valid parameters */ + if ((disParams == NULL) || (disParams->devLimit > RFAL_NFC_MAX_DEVICES) || + (disParams->devLimit == 0U) || + ((disParams->maxBR > RFAL_BR_1695) && + (disParams->maxBR != RFAL_BR_KEEP)) || + (((disParams->techs2Find & RFAL_NFC_POLL_TECH_F) != 0U) && + (disParams->nfcfBR != RFAL_BR_212) && + (disParams->nfcfBR != RFAL_BR_424)) || + ((((disParams->techs2Find & RFAL_NFC_POLL_TECH_AP2P) != 0U) && + (disParams->ap2pBR > RFAL_BR_424)) || + (disParams->GBLen > RFAL_NFCDEP_GB_MAX_LEN))) { + return RFAL_ERR_PARAM; + } + + if ((((disParams->techs2Find & RFAL_NFC_POLL_TECH_A) != 0U) && + (!((bool)RFAL_FEATURE_NFCA))) || + (((disParams->techs2Find & RFAL_NFC_POLL_TECH_B) != 0U) && + (!((bool)RFAL_FEATURE_NFCB))) || + (((disParams->techs2Find & RFAL_NFC_POLL_TECH_F) != 0U) && + (!((bool)RFAL_FEATURE_NFCF))) || + (((disParams->techs2Find & RFAL_NFC_POLL_TECH_V) != 0U) && + (!((bool)RFAL_FEATURE_NFCV))) || + (((disParams->techs2Find & RFAL_NFC_POLL_TECH_ST25TB) != 0U) && + (!((bool)RFAL_FEATURE_ST25TB))) || + (((disParams->techs2Find & RFAL_NFC_POLL_TECH_AP2P) != 0U) && + (!((bool)RFAL_FEATURE_NFC_DEP))) || + (((disParams->techs2Find & RFAL_NFC_LISTEN_TECH_A) != 0U) && + (!((bool)RFAL_FEATURE_NFCA))) || + (((disParams->techs2Find & RFAL_NFC_LISTEN_TECH_B) != 0U) && + (!((bool)RFAL_FEATURE_NFCB))) || + (((disParams->techs2Find & RFAL_NFC_LISTEN_TECH_F) != 0U) && + (!((bool)RFAL_FEATURE_NFCF))) || + (((disParams->techs2Find & RFAL_NFC_LISTEN_TECH_AP2P) != 0U) && + (!((bool)RFAL_FEATURE_NFC_DEP)))) { + return RFAL_ERR_DISABLED; /* PRQA S 2880 # MISRA 2.1 - Unreachable code + due to configuration option being set/unset */ + } + + if ((((disParams->techs2Find & RFAL_NFC_POLL_TECH_A) != 0U) && + (!((bool)RFAL_SUPPORT_MODE_POLL_NFCA))) || + (((disParams->techs2Find & RFAL_NFC_POLL_TECH_B) != 0U) && + (!((bool)RFAL_SUPPORT_MODE_POLL_NFCB))) || + (((disParams->techs2Find & RFAL_NFC_POLL_TECH_F) != 0U) && + (!((bool)RFAL_SUPPORT_MODE_POLL_NFCF))) || + (((disParams->techs2Find & RFAL_NFC_POLL_TECH_V) != 0U) && + (!((bool)RFAL_SUPPORT_MODE_POLL_NFCV))) || + (((disParams->techs2Find & RFAL_NFC_POLL_TECH_ST25TB) != 0U) && + (!((bool)RFAL_SUPPORT_MODE_POLL_NFCB))) || + (((disParams->techs2Find & RFAL_NFC_POLL_TECH_AP2P) != 0U) && + (!((bool)RFAL_SUPPORT_MODE_POLL_ACTIVE_P2P))) || + (((disParams->techs2Find & RFAL_NFC_LISTEN_TECH_A) != 0U) && + (!((bool)RFAL_SUPPORT_MODE_LISTEN_NFCA))) || + (((disParams->techs2Find & RFAL_NFC_LISTEN_TECH_B) != 0U) && + (!((bool)RFAL_SUPPORT_MODE_LISTEN_NFCB))) || + (((disParams->techs2Find & RFAL_NFC_LISTEN_TECH_F) != 0U) && + (!((bool)RFAL_SUPPORT_MODE_LISTEN_NFCF))) || + (((disParams->techs2Find & RFAL_NFC_LISTEN_TECH_AP2P) != 0U) && + (!((bool)RFAL_SUPPORT_MODE_LISTEN_ACTIVE_P2P)))) { + return RFAL_ERR_NOTSUPP; /* PRQA S 2880 # MISRA 2.1 - Unreachable code due + to configuration option being set/unset */ + } + + /* Initialize context for discovery */ + gNfcDev.activeDev = NULL; + gNfcDev.techsFound = RFAL_NFC_TECH_NONE; + gNfcDev.techDctCnt = 0; + gNfcDev.devCnt = 0; + gNfcDev.deactType = RFAL_NFC_DEACTIVATE_DISCOVERY; + gNfcDev.isTechInit = false; + gNfcDev.isFieldOn = false; + gNfcDev.isDeactivating = false; + gNfcDev.disc = *disParams; + + /* Calculate Listen Mask */ + gNfcDev.lmMask = 0U; + gNfcDev.lmMask |= (((gNfcDev.disc.techs2Find & RFAL_NFC_LISTEN_TECH_A) != 0U) + ? RFAL_LM_MASK_NFCA + : 0U); + gNfcDev.lmMask |= (((gNfcDev.disc.techs2Find & RFAL_NFC_LISTEN_TECH_B) != 0U) + ? RFAL_LM_MASK_NFCB + : 0U); + gNfcDev.lmMask |= (((gNfcDev.disc.techs2Find & RFAL_NFC_LISTEN_TECH_F) != 0U) + ? RFAL_LM_MASK_NFCF + : 0U); + gNfcDev.lmMask |= + (((gNfcDev.disc.techs2Find & RFAL_NFC_LISTEN_TECH_AP2P) != 0U) + ? RFAL_LM_MASK_ACTIVE_P2P + : 0U); + +#if !RFAL_FEATURE_LISTEN_MODE + /* Check if Listen Mode is supported/Enabled */ + if (gNfcDev.lmMask != 0U) { + return RFAL_ERR_DISABLED; + } +#endif + + gNfcDev.state = RFAL_NFC_STATE_START_DISCOVERY; + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalNfcDeactivate(rfalNfcDeactivateType deactType) { + ReturnCode ret; + + /* Check for valid state */ + if ((gNfcDev.state <= RFAL_NFC_STATE_IDLE) || + ((deactType == RFAL_NFC_DEACTIVATE_SLEEP) && + ((gNfcDev.state < RFAL_NFC_STATE_ACTIVATED) || + (gNfcDev.activeDev == NULL)))) { + return RFAL_ERR_WRONG_STATE; + } + + /* Check valid paramters for the deactivation types */ + if (((deactType == RFAL_NFC_DEACTIVATE_SLEEP) && + rfalNfcIsRemDevPoller(gNfcDev.activeDev->type)) || + ((deactType == RFAL_NFC_DEACTIVATE_DISCOVERY) && + (gNfcDev.disc.techs2Find == RFAL_NFC_TECH_NONE))) { + return RFAL_ERR_PARAM; + } + + gNfcDev.deactType = deactType; + + /* Check if Discovery is to continue afterwards or back to Select */ + if ((deactType == RFAL_NFC_DEACTIVATE_DISCOVERY) || + (deactType == RFAL_NFC_DEACTIVATE_SLEEP)) { + /* If so let the state machine continue*/ + gNfcDev.state = RFAL_NFC_STATE_DEACTIVATION; + } else { + /* Otherwise deactivate immediately and go to IDLE */ + rfalRunBlocking(ret, rfalNfcDeactivation()); + gNfcDev.state = RFAL_NFC_STATE_IDLE; + } + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalNfcSelect(uint8_t devIdx) { + /* Check for valid state */ + if (gNfcDev.state != RFAL_NFC_STATE_POLL_SELECT) { + return RFAL_ERR_WRONG_STATE; + } + + gNfcDev.isTechInit = false; + gNfcDev.selDevIdx = devIdx; + gNfcDev.state = RFAL_NFC_STATE_POLL_ACTIVATION; + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +rfalNfcState rfalNfcGetState(void) { return gNfcDev.state; } + +/*******************************************************************************/ +ReturnCode rfalNfcGetDevicesFound(rfalNfcDevice **devList, uint8_t *devCnt) { + /* Check for valid state */ + if (gNfcDev.state < RFAL_NFC_STATE_POLL_SELECT) { + return RFAL_ERR_WRONG_STATE; + } + + /* Check valid parameters */ + if ((devList == NULL) || (devCnt == NULL)) { + return RFAL_ERR_PARAM; + } + + *devCnt = gNfcDev.devCnt; + *devList = gNfcDev.devList; + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalNfcGetActiveDevice(rfalNfcDevice **dev) { + /* Check for valid state */ + if (gNfcDev.state < RFAL_NFC_STATE_ACTIVATED) { + return RFAL_ERR_WRONG_STATE; + } + + /* Check valid parameter */ + if (dev == NULL) { + return RFAL_ERR_PARAM; + } + + /* Check for valid state */ + if ((gNfcDev.devCnt == 0U) || (gNfcDev.activeDev == NULL)) { + return RFAL_ERR_REQUEST; + } + + *dev = gNfcDev.activeDev; + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +void rfalNfcWorker(void) { + ReturnCode err; + + rfalWorker(); /* Execute RFAL process */ + + switch (gNfcDev.state) { + /*******************************************************************************/ + case RFAL_NFC_STATE_NOTINIT: + case RFAL_NFC_STATE_IDLE: + break; + + /*******************************************************************************/ + case RFAL_NFC_STATE_START_DISCOVERY: + + /* Initialize context for discovery cycle */ + gNfcDev.devCnt = 0; + gNfcDev.selDevIdx = 0; + RFAL_MEMSET(gNfcDev.devList, 0x00, sizeof(gNfcDev.devList)); + gNfcDev.techsFound = RFAL_NFC_TECH_NONE; + gNfcDev.techs2do = gNfcDev.disc.techs2Find; + gNfcDev.state = RFAL_NFC_STATE_POLL_TECHDETECT; + gNfcDev.isDeactivating = false; + + /* Start total duration timer */ + platformTimerDestroy(gNfcDev.discTmr); + gNfcDev.discTmr = + (uint32_t)platformTimerCreate(gNfcDev.disc.totalDuration); + +#if RFAL_FEATURE_WAKEUP_MODE + /* Check if Low power Wake-Up is to be performed */ + if ((gNfcDev.disc.wakeupEnabled) && + (((gNfcDev.techDctCnt == 0U) && + (gNfcDev.disc.wakeupPollBefore == false)) || + (gNfcDev.techDctCnt >= gNfcDev.disc.wakeupNPolls))) { + /* Initialize Low power Wake-up mode and wait */ + err = rfalWakeUpModeStart((gNfcDev.disc.wakeupConfigDefault + ? NULL + : &gNfcDev.disc.wakeupConfig)); + if (err == RFAL_ERR_NONE) { + gNfcDev.state = RFAL_NFC_STATE_WAKEUP_MODE; + } + } + gNfcDev.techDctCnt++; + +#endif /* RFAL_FEATURE_WAKEUP_MODE */ + + rfalNfcNfcNotify(gNfcDev.state); /* Notify caller that WU or Technology + Detection has started */ + break; + + /*******************************************************************************/ + case RFAL_NFC_STATE_WAKEUP_MODE: + +#if RFAL_FEATURE_WAKEUP_MODE + /* Check if the Wake-up mode has woke */ + if (rfalWakeUpModeHasWoke()) { + rfalWakeUpModeStop(); /* Disable Wake-up mode */ + gNfcDev.state = + RFAL_NFC_STATE_POLL_TECHDETECT; /* Go to Technology detection */ + gNfcDev.techDctCnt = 1; /* Tech Detect counter (1 woke) */ + + /* (Re)Start total duration timer upon waking up */ + platformTimerDestroy(gNfcDev.discTmr); + gNfcDev.discTmr = + (uint32_t)platformTimerCreate(gNfcDev.disc.totalDuration); + + rfalNfcNfcNotify(gNfcDev.state); /* Notify caller that WU has woke */ + } +#endif /* RFAL_FEATURE_WAKEUP_MODE */ + + break; + + /*******************************************************************************/ + case RFAL_NFC_STATE_POLL_TECHDETECT: + + err = rfalNfcPollTechDetection(); /* Perform Technology Detection */ + if (err != RFAL_ERR_BUSY) /* Wait until all technologies are performed */ + { + if ((err != RFAL_ERR_NONE) || + (gNfcDev.techsFound == + RFAL_NFC_TECH_NONE)) /* Check if any error occurred or no techs + were found */ + { + rfalFieldOff(); + gNfcDev.isFieldOn = false; + gNfcDev.state = + RFAL_NFC_STATE_LISTEN_TECHDETECT; /* Nothing found as poller, go + to listener */ + break; + } + + gNfcDev.techs2do = gNfcDev.techsFound; /* Store the found technologies + for collision resolution */ + gNfcDev.state = + RFAL_NFC_STATE_POLL_COLAVOIDANCE; /* One or more devices found, go + to Collision Avoidance */ + } + break; + + /*******************************************************************************/ + case RFAL_NFC_STATE_POLL_COLAVOIDANCE: + + err = rfalNfcPollCollResolution(); /* Resolve any eventual collision */ + if (err != RFAL_ERR_BUSY) /* Wait until all technologies are performed */ + { + if ((err != RFAL_ERR_NONE) || + (gNfcDev.devCnt == + 0U)) /* Check if any error occurred or no devices were found */ + { + gNfcDev.deactType = RFAL_NFC_DEACTIVATE_DISCOVERY; + gNfcDev.state = RFAL_NFC_STATE_DEACTIVATION; + break; /* Unable to retrieve any device, restart loop */ + } + + /* Check if more than one device has been found */ + if (gNfcDev.devCnt > 1U) { + /* If more than one device was found inform upper layer to choose + * which one to activate */ + if (gNfcDev.disc.notifyCb != NULL) { + gNfcDev.state = RFAL_NFC_STATE_POLL_SELECT; + gNfcDev.disc.notifyCb(gNfcDev.state); + break; + } + } + + /* If only one device or no callback has been set, activate the first + * device found */ + gNfcDev.selDevIdx = 0U; + gNfcDev.state = RFAL_NFC_STATE_POLL_ACTIVATION; + } + break; + + /*******************************************************************************/ + case RFAL_NFC_STATE_POLL_ACTIVATION: + + err = rfalNfcPollActivation(gNfcDev.selDevIdx); + if (err != RFAL_ERR_BUSY) /* Wait until all Activation is complete */ + { + if (err != RFAL_ERR_NONE) /* Check if activation has failed */ + { + /* Check if more than one device has been found */ + if ((gNfcDev.devCnt > 1U) && (gNfcDev.disc.notifyCb != NULL)) { + gNfcDev.state = RFAL_NFC_STATE_POLL_SELECT; + rfalNfcNfcNotify(gNfcDev.state); + break; + } + + gNfcDev.deactType = + RFAL_NFC_DEACTIVATE_DISCOVERY; /* Ensure deactivation, not Sleep + */ + gNfcDev.state = RFAL_NFC_STATE_DEACTIVATION; /* If Activation failed, + restart loop */ + break; + } + + gNfcDev.state = + RFAL_NFC_STATE_ACTIVATED; /* Device has been properly activated */ + rfalNfcNfcNotify(gNfcDev.state); /* Inform upper layer that a device has + been activated */ + } + break; + + /*******************************************************************************/ + case RFAL_NFC_STATE_DATAEXCHANGE: + + rfalNfcDataExchangeGetStatus(); /* Run the internal state machine */ + + if (gNfcDev.dataExErr != + RFAL_ERR_BUSY) /* If Dataexchange has terminated */ + { + gNfcDev.state = RFAL_NFC_STATE_DATAEXCHANGE_DONE; /* Go to done state */ + rfalNfcNfcNotify(gNfcDev.state); /* And notify caller */ + } + if (gNfcDev.dataExErr == + RFAL_ERR_SLEEP_REQ) /* Check if Listen mode has to go to Sleep */ + { + gNfcDev.state = + RFAL_NFC_STATE_LISTEN_SLEEP; /* Go to Listen Sleep state */ + rfalNfcNfcNotify(gNfcDev.state); /* And notify caller */ + } + break; + + /*******************************************************************************/ + case RFAL_NFC_STATE_DEACTIVATION: + + err = rfalNfcDeactivation(); /* Deactivate current device */ + if (err != RFAL_ERR_BUSY) { + if (gNfcDev.deactType == RFAL_NFC_DEACTIVATE_SLEEP) { + gNfcDev.state = RFAL_NFC_STATE_POLL_SELECT; + } else { + gNfcDev.state = ((gNfcDev.deactType == RFAL_NFC_DEACTIVATE_DISCOVERY) + ? RFAL_NFC_STATE_START_DISCOVERY + : RFAL_NFC_STATE_IDLE); + } + + rfalNfcNfcNotify(gNfcDev.state); /* Notify caller */ + } + break; + + /*******************************************************************************/ + case RFAL_NFC_STATE_LISTEN_TECHDETECT: + + if (platformTimerIsExpired(gNfcDev.discTmr)) { +#if RFAL_FEATURE_LISTEN_MODE + rfalListenStop(); +#else + rfalFieldOff(); +#endif /* RFAL_FEATURE_LISTEN_MODE */ + gNfcDev.isFieldOn = false; + + gNfcDev.state = + RFAL_NFC_STATE_START_DISCOVERY; /* Restart the discovery loop */ + rfalNfcNfcNotify(gNfcDev.state); /* Notify caller */ + break; + } + +#if RFAL_FEATURE_LISTEN_MODE + + if (gNfcDev.lmMask != 0U) /* Check if configured to perform Listen mode */ + { + err = rfalListenStart( + gNfcDev.lmMask, &gNfcDev.disc.lmConfigPA, NULL, + &gNfcDev.disc.lmConfigPF, (uint8_t *)&gNfcDev.rxBuf.rfBuf, + (uint16_t)rfalConvBytesToBits(sizeof(gNfcDev.rxBuf.rfBuf)), + &gNfcDev.rxLen); + if (err == RFAL_ERR_NONE) { + gNfcDev.state = + RFAL_NFC_STATE_LISTEN_COLAVOIDANCE; /* Wait for listen mode to be + activated */ + } + } + break; + + /*******************************************************************************/ + case RFAL_NFC_STATE_LISTEN_COLAVOIDANCE: + + if (platformTimerIsExpired( + gNfcDev + .discTmr)) /* Check if the total duration has been reached */ + { + rfalListenStop(); + gNfcDev.state = + RFAL_NFC_STATE_START_DISCOVERY; /* Restart the discovery loop */ + rfalNfcNfcNotify(gNfcDev.state); /* Notify caller */ + break; + } + + /* Check for external field */ + if (rfalListenGetState(NULL, NULL) >= RFAL_LM_STATE_IDLE) { + gNfcDev.state = + RFAL_NFC_STATE_LISTEN_ACTIVATION; /* Wait for listen mode to be + activated */ + } + break; + + /*******************************************************************************/ + case RFAL_NFC_STATE_LISTEN_ACTIVATION: + case RFAL_NFC_STATE_LISTEN_SLEEP: + + err = rfalNfcListenActivation(); + if (err != RFAL_ERR_BUSY) { + if (err == RFAL_ERR_NONE) { + gNfcDev.activeDev = + gNfcDev + .devList; /* Assign the active device to be used further on */ + gNfcDev.devCnt++; + + gNfcDev.state = + RFAL_NFC_STATE_ACTIVATED; /* Device has been properly activated */ + rfalNfcNfcNotify(gNfcDev.state); /* Inform upper layer that a device + has been activated */ + } else if ((!platformTimerIsExpired(gNfcDev.discTmr)) && + (err == RFAL_ERR_LINK_LOSS) && + (gNfcDev.state == RFAL_NFC_STATE_LISTEN_ACTIVATION)) { + break; /* Field|Link broken during activation, keep in Listen the + remaining total duration */ + } else { + rfalListenStop(); + gNfcDev.state = + RFAL_NFC_STATE_START_DISCOVERY; /* Restart the discovery loop */ + rfalNfcNfcNotify(gNfcDev.state); /* Notify caller */ + } + } +#endif /* RFAL_FEATURE_LISTEN_MODE */ + break; + + /*******************************************************************************/ + case RFAL_NFC_STATE_ACTIVATED: + case RFAL_NFC_STATE_POLL_SELECT: + case RFAL_NFC_STATE_DATAEXCHANGE_DONE: + default: + return; + } +} + +/*******************************************************************************/ +ReturnCode rfalNfcDataExchangeStart(uint8_t *txData, uint16_t txDataLen, + uint8_t **rxData, uint16_t **rvdLen, + uint32_t fwt) { + ReturnCode err; + rfalTransceiveContext ctx; + + /*******************************************************************************/ + /* The Data Exchange is divided in two different moments, the trigger/Start of + * * the transfer followed by the check until its completion */ + if ((gNfcDev.state >= RFAL_NFC_STATE_ACTIVATED) && + (gNfcDev.activeDev != NULL)) { + /*******************************************************************************/ + /* In Listen mode is the Poller that initiates the communicatation */ + /* Assign output parameters and rfalNfcDataExchangeGetStatus will return */ + /* incoming data from Poller/Initiator */ + if ((gNfcDev.state == RFAL_NFC_STATE_ACTIVATED) && + rfalNfcIsRemDevPoller(gNfcDev.activeDev->type)) { + if (txDataLen > 0U) { + return RFAL_ERR_WRONG_STATE; + } + + *rvdLen = (uint16_t *)&gNfcDev.rxLen; + *rxData = (uint8_t *)((gNfcDev.activeDev->rfInterface == + RFAL_NFC_INTERFACE_ISODEP) + ? gNfcDev.rxBuf.isoDepBuf.apdu + : ((gNfcDev.activeDev->rfInterface == + RFAL_NFC_INTERFACE_NFCDEP) + ? gNfcDev.rxBuf.nfcDepBuf.pdu + : gNfcDev.rxBuf.rfBuf)); + return RFAL_ERR_NONE; + } + + /*******************************************************************************/ + switch (gNfcDev.activeDev->rfInterface) /* Check which RF interface shall be + used/has been activated */ + { + /*******************************************************************************/ + case RFAL_NFC_INTERFACE_RF: + + rfalCreateByteFlagsTxRxContext( + ctx, (uint8_t *)txData, txDataLen, gNfcDev.rxBuf.rfBuf, + sizeof(gNfcDev.rxBuf.rfBuf), &gNfcDev.rxLen, + RFAL_TXRX_FLAGS_DEFAULT, fwt); + ctx.txBufLen = txDataLen; /* RF interface uses number of bits */ + + *rxData = (uint8_t *)gNfcDev.rxBuf.rfBuf; + *rvdLen = (uint16_t *)&gNfcDev.rxLen; + err = rfalStartTransceive(&ctx); + break; + +#if RFAL_FEATURE_ISO_DEP + /*******************************************************************************/ + case RFAL_NFC_INTERFACE_ISODEP: { + rfalIsoDepApduTxRxParam rfalIsoDepTxRx; + + if (txDataLen > sizeof(gNfcDev.txBuf.isoDepBuf.apdu)) { + return RFAL_ERR_NOMEM; + } + + if (txDataLen > 0U) { + RFAL_MEMCPY((uint8_t *)gNfcDev.txBuf.isoDepBuf.apdu, txData, + txDataLen); + } + + rfalIsoDepTxRx.DID = RFAL_ISODEP_NO_DID; + rfalIsoDepTxRx.ourFSx = RFAL_ISODEP_FSX_KEEP; + rfalIsoDepTxRx.FSx = gNfcDev.activeDev->proto.isoDep.info.FSx; + rfalIsoDepTxRx.dFWT = gNfcDev.activeDev->proto.isoDep.info.dFWT; + rfalIsoDepTxRx.FWT = gNfcDev.activeDev->proto.isoDep.info.FWT; + rfalIsoDepTxRx.txBuf = &gNfcDev.txBuf.isoDepBuf; + rfalIsoDepTxRx.txBufLen = txDataLen; + rfalIsoDepTxRx.rxBuf = &gNfcDev.rxBuf.isoDepBuf; + rfalIsoDepTxRx.rxLen = &gNfcDev.rxLen; + rfalIsoDepTxRx.tmpBuf = &gNfcDev.tmpBuf.isoDepBuf; + *rxData = (uint8_t *)gNfcDev.rxBuf.isoDepBuf.apdu; + *rvdLen = (uint16_t *)&gNfcDev.rxLen; + + /*******************************************************************************/ + /* Trigger a RFAL ISO-DEP Transceive */ + err = rfalIsoDepStartApduTransceive(rfalIsoDepTxRx); + break; + } +#endif /* RFAL_FEATURE_ISO_DEP */ + +#if RFAL_FEATURE_NFC_DEP + /*******************************************************************************/ + case RFAL_NFC_INTERFACE_NFCDEP: { + rfalNfcDepPduTxRxParam rfalNfcDepTxRx; + + if (txDataLen > sizeof(gNfcDev.txBuf.nfcDepBuf.pdu)) { + return RFAL_ERR_NOMEM; + } + + if (txDataLen > 0U) { + RFAL_MEMCPY((uint8_t *)gNfcDev.txBuf.nfcDepBuf.pdu, txData, + txDataLen); + } + + rfalNfcDepTxRx.DID = RFAL_NFCDEP_DID_KEEP; + rfalNfcDepTxRx.FSx = rfalNfcIsRemDevListener(gNfcDev.activeDev->type) + ? rfalNfcDepLR2FS((uint8_t)rfalNfcDepPP2LR( + gNfcDev.activeDev->proto.nfcDep + .activation.Target.ATR_RES.PPt)) + : rfalNfcDepLR2FS((uint8_t)rfalNfcDepPP2LR( + gNfcDev.activeDev->proto.nfcDep + .activation.Initiator.ATR_REQ.PPi)); + rfalNfcDepTxRx.dFWT = gNfcDev.activeDev->proto.nfcDep.info.dFWT; + rfalNfcDepTxRx.FWT = gNfcDev.activeDev->proto.nfcDep.info.FWT; + rfalNfcDepTxRx.txBuf = &gNfcDev.txBuf.nfcDepBuf; + rfalNfcDepTxRx.txBufLen = txDataLen; + rfalNfcDepTxRx.rxBuf = &gNfcDev.rxBuf.nfcDepBuf; + rfalNfcDepTxRx.rxLen = &gNfcDev.rxLen; + rfalNfcDepTxRx.tmpBuf = &gNfcDev.tmpBuf.nfcDepBuf; + *rxData = (uint8_t *)gNfcDev.rxBuf.nfcDepBuf.pdu; + *rvdLen = (uint16_t *)&gNfcDev.rxLen; + + /*******************************************************************************/ + /* Trigger a RFAL NFC-DEP Transceive */ + err = rfalNfcDepStartPduTransceive(rfalNfcDepTxRx); + break; + } +#endif /* RFAL_FEATURE_NFC_DEP */ + + /*******************************************************************************/ + default: + err = RFAL_ERR_PARAM; + break; + } + + /* If a transceive has succesfully started flag Data Exchange as ongoing */ + if (err == RFAL_ERR_NONE) { + gNfcDev.dataExErr = RFAL_ERR_BUSY; + gNfcDev.state = RFAL_NFC_STATE_DATAEXCHANGE; + } + + return err; + } + + return RFAL_ERR_WRONG_STATE; +} + +/*******************************************************************************/ +ReturnCode rfalNfcDataExchangeGetStatus(void) { + /*******************************************************************************/ + /* Check if it's the first frame received in Listen mode */ + if (gNfcDev.state == RFAL_NFC_STATE_ACTIVATED) { + /* Continue data exchange as normal */ + gNfcDev.dataExErr = RFAL_ERR_BUSY; + gNfcDev.state = RFAL_NFC_STATE_DATAEXCHANGE; + + /* Check if we performing in T3T CE */ + if ((gNfcDev.activeDev->type == RFAL_NFC_POLL_TYPE_NFCF) && + (gNfcDev.activeDev->rfInterface == RFAL_NFC_INTERFACE_RF)) { + /* The first frame has been retrieved by rfalListenMode, flag data + * immediately */ + /* Can only call rfalGetTransceiveStatus() after starting a transceive + * with rfalStartTransceive */ + gNfcDev.dataExErr = RFAL_ERR_NONE; + } + } + + /*******************************************************************************/ + /* Check if we are in we have been placed to sleep, and return last error */ + if (gNfcDev.state == RFAL_NFC_STATE_LISTEN_SLEEP) { + return gNfcDev.dataExErr; /* RFAL_ERR_SLEEP_REQ */ + } + + /*******************************************************************************/ + /* Check if Data exchange has been started */ + if ((gNfcDev.state != RFAL_NFC_STATE_DATAEXCHANGE) && + (gNfcDev.state != RFAL_NFC_STATE_DATAEXCHANGE_DONE)) { + return RFAL_ERR_WRONG_STATE; + } + + /* Check if Data exchange is still ongoing */ + if (gNfcDev.dataExErr == RFAL_ERR_BUSY) { + switch (gNfcDev.activeDev->rfInterface) { + /*******************************************************************************/ + case RFAL_NFC_INTERFACE_RF: + gNfcDev.dataExErr = rfalGetTransceiveStatus(); + break; + +#if RFAL_FEATURE_ISO_DEP + /*******************************************************************************/ + case RFAL_NFC_INTERFACE_ISODEP: + gNfcDev.dataExErr = rfalIsoDepGetApduTransceiveStatus(); + break; +#endif /* RFAL_FEATURE_ISO_DEP */ + + /*******************************************************************************/ +#if RFAL_FEATURE_NFC_DEP + case RFAL_NFC_INTERFACE_NFCDEP: + gNfcDev.dataExErr = rfalNfcDepGetPduTransceiveStatus(); + break; +#endif /* RFAL_FEATURE_NFC_DEP */ + + /*******************************************************************************/ + default: + gNfcDev.dataExErr = RFAL_ERR_PARAM; + break; + } + +#if RFAL_FEATURE_LISTEN_MODE + /*******************************************************************************/ + /* If a Sleep request has been received (Listen Mode) go to sleep + * immediately */ + if (gNfcDev.dataExErr == RFAL_ERR_SLEEP_REQ) { + RFAL_EXIT_ON_ERR( + gNfcDev.dataExErr, + rfalListenSleepStart(RFAL_LM_STATE_SLEEP_A, gNfcDev.rxBuf.rfBuf, + sizeof(gNfcDev.rxBuf.rfBuf), &gNfcDev.rxLen)); + + /* If set Sleep was succesfull keep restore the Sleep request signal */ + gNfcDev.dataExErr = RFAL_ERR_SLEEP_REQ; + } +#endif /* RFAL_FEATURE_LISTEN_MODE */ + } + + return gNfcDev.dataExErr; +} + +/*! + ****************************************************************************** + * \brief Poller Technology Detection + * + * This method implements the Technology Detection / Poll for different + * device technologies. + * + * \return RFAL_ERR_NONE : Operation completed with no error + * \return RFAL_ERR_BUSY : Operation ongoing + * \return RFAL_ERR_XXXX : Error occurred + * + ****************************************************************************** + */ +static ReturnCode rfalNfcPollTechDetection(void) { + ReturnCode err; + + err = RFAL_ERR_NONE; + + /* Suppress warning when specific RFAL features have been disabled */ + RFAL_NO_WARNING(err); + + /*******************************************************************************/ + /* AP2P Technology Detection */ + /*******************************************************************************/ + if (((gNfcDev.disc.techs2Find & RFAL_NFC_POLL_TECH_AP2P) != 0U) && + ((gNfcDev.techs2do & RFAL_NFC_POLL_TECH_AP2P) != 0U)) { +#if RFAL_FEATURE_NFC_DEP + + if (!gNfcDev.isTechInit) { + RFAL_EXIT_ON_ERR( + err, rfalSetMode(RFAL_MODE_POLL_ACTIVE_P2P, gNfcDev.disc.ap2pBR, + gNfcDev.disc.ap2pBR)); + rfalSetErrorHandling(RFAL_ERRORHANDLING_NONE); + rfalSetFDTListen(RFAL_FDT_LISTEN_AP2P_POLLER); + rfalSetFDTPoll(RFAL_FDT_POLL_AP2P_POLLER); + rfalSetGT(RFAL_GT_AP2P_ADJUSTED); + RFAL_EXIT_ON_ERR( + err, + rfalFieldOnAndStartGT()); /* Turns the Field On and starts GT timer */ + gNfcDev.isTechInit = true; + } + + if (rfalIsGTExpired()) /* Wait until Guard Time is fulfilled */ + { + gNfcDev.techs2do &= ~RFAL_NFC_POLL_TECH_AP2P; + + err = rfalNfcNfcDepActivate(gNfcDev.devList, RFAL_NFCDEP_COMM_ACTIVE, + NULL, 0); /* Poll for AP2P devices */ + if (err == RFAL_ERR_NONE) { + gNfcDev.techsFound |= RFAL_NFC_POLL_TECH_AP2P; + + gNfcDev.devList->type = RFAL_NFC_LISTEN_TYPE_AP2P; + gNfcDev.devList->rfInterface = RFAL_NFC_INTERFACE_NFCDEP; + gNfcDev.devCnt++; + + return RFAL_ERR_NONE; + } + + gNfcDev.isTechInit = false; + rfalFieldOff(); + } + return RFAL_ERR_BUSY; + +#endif /* RFAL_FEATURE_NFC_DEP */ + } + + /*******************************************************************************/ + /* Turn Field On if Passive Poll technologies are enabled */ + /*******************************************************************************/ + if ((!gNfcDev.isFieldOn) && + ((gNfcDev.disc.techs2Find & + (RFAL_NFC_POLL_TECH_A | RFAL_NFC_POLL_TECH_B | RFAL_NFC_POLL_TECH_F | + RFAL_NFC_POLL_TECH_V | RFAL_NFC_POLL_TECH_ST25TB | + RFAL_NFC_POLL_TECH_PROP)) != 0U)) { + RFAL_EXIT_ON_ERR(err, rfalFieldOnAndStartGT()); /* Turns the Field On */ + gNfcDev.isFieldOn = true; + return RFAL_ERR_BUSY; + } + + /*******************************************************************************/ + /* Passive NFC-A Technology Detection */ + /*******************************************************************************/ + if (((gNfcDev.disc.techs2Find & RFAL_NFC_POLL_TECH_A) != 0U) && + ((gNfcDev.techs2do & RFAL_NFC_POLL_TECH_A) != 0U)) { +#if RFAL_FEATURE_NFCA + + if (!gNfcDev.isTechInit) { + RFAL_EXIT_ON_ERR( + err, rfalNfcaPollerInitialize()); /* Initialize RFAL for NFC-A */ + RFAL_EXIT_ON_ERR(err, rfalFieldOnAndStartGT()); /* As field is already On + only starts GT timer */ + gNfcDev.isTechInit = true; + gNfcDev.isOperOngoing = false; /* No operation currently ongoing */ + } + + if (rfalIsGTExpired()) /* Wait until Guard Time is fulfilled */ + { + if (!gNfcDev.isOperOngoing) { + rfalNfcaPollerStartTechnologyDetection( + gNfcDev.disc.compMode, + &gNfcDev.sensRes); /* Poll for NFC-A devices */ + + gNfcDev.isOperOngoing = true; + return RFAL_ERR_BUSY; + } + + err = rfalNfcaPollerGetTechnologyDetectionStatus(); + if (err != RFAL_ERR_BUSY) { + if (err == RFAL_ERR_NONE) { + gNfcDev.techsFound |= RFAL_NFC_POLL_TECH_A; + } + + gNfcDev.isTechInit = false; + gNfcDev.techs2do &= ~RFAL_NFC_POLL_TECH_A; + } + + /* Check if bail-out after NFC-A Activity 2.1 9.2.3.21 */ + if (((gNfcDev.disc.techs2Bail & RFAL_NFC_POLL_TECH_A) != 0U) && + (gNfcDev.techsFound != 0U)) { + return RFAL_ERR_NONE; + } + } + + return RFAL_ERR_BUSY; + +#endif /* RFAL_FEATURE_NFCA */ + } + + /*******************************************************************************/ + /* Passive NFC-B Technology Detection */ + /*******************************************************************************/ + if (((gNfcDev.disc.techs2Find & RFAL_NFC_POLL_TECH_B) != 0U) && + ((gNfcDev.techs2do & RFAL_NFC_POLL_TECH_B) != 0U)) { +#if RFAL_FEATURE_NFCB + + if (!gNfcDev.isTechInit) { + RFAL_EXIT_ON_ERR( + err, rfalNfcbPollerInitialize()); /* Initialize RFAL for NFC-B */ + RFAL_EXIT_ON_ERR(err, rfalFieldOnAndStartGT()); /* As field is already On + only starts GT timer */ + gNfcDev.isTechInit = true; + gNfcDev.isOperOngoing = false; /* No operation currently ongoing */ + } + + if (rfalIsGTExpired()) /* Wait until Guard Time is fulfilled */ + { + if (!gNfcDev.isOperOngoing) { + rfalNfcbPollerStartTechnologyDetection( + gNfcDev.disc.compMode, &gNfcDev.sensbRes, + &gNfcDev.sensbResLen); /* Poll for NFC-B devices */ + + gNfcDev.isOperOngoing = true; + return RFAL_ERR_BUSY; + } + + err = rfalNfcbPollerGetTechnologyDetectionStatus(); + if (err != RFAL_ERR_BUSY) { + if (err == RFAL_ERR_NONE) { + gNfcDev.techsFound |= RFAL_NFC_POLL_TECH_B; + } + + gNfcDev.isTechInit = false; + gNfcDev.techs2do &= ~RFAL_NFC_POLL_TECH_B; + } + + /* Check if bail-out after NFC-B Activity 2.1 9.2.3.26 */ + if (((gNfcDev.disc.techs2Bail & RFAL_NFC_POLL_TECH_B) != 0U) && + (gNfcDev.techsFound != 0U)) { + return RFAL_ERR_NONE; + } + } + + return RFAL_ERR_BUSY; + +#endif /* RFAL_FEATURE_NFCB */ + } + + /*******************************************************************************/ + /* Passive NFC-F Technology Detection */ + /*******************************************************************************/ + if (((gNfcDev.disc.techs2Find & RFAL_NFC_POLL_TECH_F) != 0U) && + ((gNfcDev.techs2do & RFAL_NFC_POLL_TECH_F) != 0U)) { +#if RFAL_FEATURE_NFCF + + if (!gNfcDev.isTechInit) { + RFAL_EXIT_ON_ERR( + err, rfalNfcfPollerInitialize( + gNfcDev.disc.nfcfBR)); /* Initialize RFAL for NFC-F */ + RFAL_EXIT_ON_ERR(err, rfalFieldOnAndStartGT()); /* As field is already On + only starts GT timer */ + + gNfcDev.isTechInit = true; + gNfcDev.isOperOngoing = false; /* No operation currently ongoing */ + } + + if (rfalIsGTExpired()) /* Wait until Guard Time is fulfilled */ + { + if (!gNfcDev.isOperOngoing) { + rfalNfcfPollerStartCheckPresence(); + + gNfcDev.isOperOngoing = true; + return RFAL_ERR_BUSY; + } + + err = rfalNfcfPollerGetCheckPresenceStatus(); /* Poll for NFC-F devices */ + if (err != RFAL_ERR_BUSY) { + if (err == RFAL_ERR_NONE) { + gNfcDev.techsFound |= RFAL_NFC_POLL_TECH_F; + } + + gNfcDev.isTechInit = false; + gNfcDev.techs2do &= ~RFAL_NFC_POLL_TECH_F; + } + + /* Check if bail-out after NFC-F Activity 2.1 9.2.3.31 */ + if (((gNfcDev.disc.techs2Bail & RFAL_NFC_POLL_TECH_F) != 0U) && + (gNfcDev.techsFound != 0U)) { + return RFAL_ERR_NONE; + } + } + + return RFAL_ERR_BUSY; + +#endif /* RFAL_FEATURE_NFCF */ + } + + /*******************************************************************************/ + /* Passive NFC-V Technology Detection */ + /*******************************************************************************/ + if (((gNfcDev.disc.techs2Find & RFAL_NFC_POLL_TECH_V) != 0U) && + ((gNfcDev.techs2do & RFAL_NFC_POLL_TECH_V) != 0U)) { +#if RFAL_FEATURE_NFCV + + rfalNfcvInventoryRes invRes; + + if (!gNfcDev.isTechInit) { + RFAL_EXIT_ON_ERR( + err, rfalNfcvPollerInitialize()); /* Initialize RFAL for NFC-V */ + RFAL_EXIT_ON_ERR(err, rfalFieldOnAndStartGT()); /* As field is already On + only starts GT timer */ + gNfcDev.isTechInit = true; + } + + if (rfalIsGTExpired()) /* Wait until Guard Time is fulfilled */ + { + err = rfalNfcvPollerCheckPresence(&invRes); /* Poll for NFC-V devices */ + if (err == RFAL_ERR_NONE) { + gNfcDev.techsFound |= RFAL_NFC_POLL_TECH_V; + } + + gNfcDev.isTechInit = false; + gNfcDev.techs2do &= ~RFAL_NFC_POLL_TECH_V; + } + + return RFAL_ERR_BUSY; + +#endif /* RFAL_FEATURE_NFCV */ + } + + /*******************************************************************************/ + /* Passive Proprietary Technology ST25TB */ + /*******************************************************************************/ + if (((gNfcDev.disc.techs2Find & RFAL_NFC_POLL_TECH_ST25TB) != 0U) && + ((gNfcDev.techs2do & RFAL_NFC_POLL_TECH_ST25TB) != 0U)) { +#if RFAL_FEATURE_ST25TB + + if (!gNfcDev.isTechInit) { + RFAL_EXIT_ON_ERR( + err, rfalSt25tbPollerInitialize()); /* Initialize RFAL for NFC-V */ + RFAL_EXIT_ON_ERR(err, rfalFieldOnAndStartGT()); /* As field is already On + only starts GT timer */ + gNfcDev.isTechInit = true; + } + + if (rfalIsGTExpired()) /* Wait until Guard Time is fulfilled */ + { + err = rfalSt25tbPollerCheckPresence(NULL); /* Poll for ST25TB devices */ + if (err == RFAL_ERR_NONE) { + gNfcDev.techsFound |= RFAL_NFC_POLL_TECH_ST25TB; + } + + gNfcDev.isTechInit = false; + gNfcDev.techs2do &= ~RFAL_NFC_POLL_TECH_ST25TB; + } + + return RFAL_ERR_BUSY; + +#endif /* RFAL_FEATURE_ST25TB */ + } + + /*******************************************************************************/ + /* Passive Proprietary Technology */ + /*******************************************************************************/ + if (((gNfcDev.disc.techs2Find & RFAL_NFC_POLL_TECH_PROP) != 0U) && + ((gNfcDev.techs2do & RFAL_NFC_POLL_TECH_PROP) != 0U)) { + if (!gNfcDev.isTechInit) { + RFAL_EXIT_ON_ERR(err, + rfalNfcpCbPollerInitialize()); /* Initialize RFAL for + Proprietary NFC */ + RFAL_EXIT_ON_ERR(err, + rfalFieldOnAndStartGT()); /* As field may already be On + only starts GT timer */ + gNfcDev.isTechInit = true; + } + + if (rfalIsGTExpired()) /* Wait until Guard Time is fulfilled */ + { + err = rfalNfcpCbPollerTechnologyDetection(); /* Poll for devices */ + if (err == RFAL_ERR_NONE) { + gNfcDev.techsFound |= RFAL_NFC_POLL_TECH_PROP; + } + + gNfcDev.isTechInit = false; + gNfcDev.techs2do &= ~RFAL_NFC_POLL_TECH_PROP; + } + + return RFAL_ERR_BUSY; + } + + return RFAL_ERR_NONE; +} + +/*! + ****************************************************************************** + * \brief Poller Collision Resolution + * + * This method implements the Collision Resolution on all technologies that + * have been detected before. + * + * \return RFAL_ERR_NONE : Operation completed with no error + * \return RFAL_ERR_BUSY : Operation ongoing + * \return RFAL_ERR_XXXX : Error occurred + * + ****************************************************************************** + */ +static ReturnCode rfalNfcPollCollResolution(void) { + uint8_t i; + static uint8_t devCnt; + ReturnCode err; + + err = RFAL_ERR_NONE; + i = 0; + + /* Suppress warning when specific RFAL features have been disabled */ + RFAL_NO_WARNING(err); + RFAL_NO_WARNING(devCnt); + RFAL_NO_WARNING(i); + + /* Check if device limit has been reached */ + if (gNfcDev.devCnt >= gNfcDev.disc.devLimit) { + return RFAL_ERR_NONE; + } + + /*******************************************************************************/ + /* NFC-A Collision Resolution */ + /*******************************************************************************/ +#if RFAL_FEATURE_NFCA + if (((gNfcDev.techsFound & RFAL_NFC_POLL_TECH_A) != 0U) && + ((gNfcDev.techs2do & RFAL_NFC_POLL_TECH_A) != + 0U)) /* If a NFC-A device was found/detected, perform Collision + Resolution */ + { + static rfalNfcaListenDevice nfcaDevList[RFAL_NFC_MAX_DEVICES]; + + if (!gNfcDev.isTechInit) { + RFAL_EXIT_ON_ERR( + err, rfalNfcaPollerInitialize()); /* Initialize RFAL for NFC-A */ + RFAL_EXIT_ON_ERR( + err, + rfalFieldOnAndStartGT()); /* Turns the Field On and starts GT timer */ + + gNfcDev.isTechInit = true; /* Technology has been initialized */ + gNfcDev.isOperOngoing = false; /* No operation currently ongoing */ + } + + if (!rfalIsGTExpired()) { + return RFAL_ERR_BUSY; + } + + if (!gNfcDev.isOperOngoing) { + RFAL_EXIT_ON_ERR(err, rfalNfcaPollerStartFullCollisionResolution( + gNfcDev.disc.compMode, + (gNfcDev.disc.devLimit - gNfcDev.devCnt), + nfcaDevList, &devCnt)); + + gNfcDev.isOperOngoing = true; + return RFAL_ERR_BUSY; + } + + err = rfalNfcaPollerGetFullCollisionResolutionStatus(); + if (err != RFAL_ERR_BUSY) { + gNfcDev.isTechInit = false; + gNfcDev.techs2do &= ~RFAL_NFC_POLL_TECH_A; + + if ((err == RFAL_ERR_NONE) && (devCnt != 0U)) { + for (i = 0; i < devCnt; i++) /* Copy devices found form local Nfca list + into global device list */ + { + gNfcDev.devList[gNfcDev.devCnt].type = RFAL_NFC_LISTEN_TYPE_NFCA; + gNfcDev.devList[gNfcDev.devCnt].dev.nfca = nfcaDevList[i]; + gNfcDev.devCnt++; + } + } + } + + return RFAL_ERR_BUSY; + } +#endif /* RFAL_FEATURE_NFCA */ + + /*******************************************************************************/ + /* NFC-B Collision Resolution */ + /*******************************************************************************/ +#if RFAL_FEATURE_NFCB + if (((gNfcDev.techsFound & RFAL_NFC_POLL_TECH_B) != 0U) && + ((gNfcDev.techs2do & RFAL_NFC_POLL_TECH_B) != + 0U)) /* If a NFC-B device was found/detected, perform Collision + Resolution */ + { + static rfalNfcbListenDevice nfcbDevList[RFAL_NFC_MAX_DEVICES]; + + if (!gNfcDev.isTechInit) { + RFAL_EXIT_ON_ERR( + err, rfalNfcbPollerInitialize()); /* Initialize RFAL for NFC-B */ + RFAL_EXIT_ON_ERR( + err, + rfalFieldOnAndStartGT()); /* Ensure GT again as other technologies + have also been polled */ + + gNfcDev.isTechInit = true; + gNfcDev.isOperOngoing = false; /* No operation currently ongoing */ + } + + if (!rfalIsGTExpired()) { + return RFAL_ERR_BUSY; + } + + if (!gNfcDev.isOperOngoing) { + RFAL_EXIT_ON_ERR(err, rfalNfcbPollerStartCollisionResolution( + gNfcDev.disc.compMode, + (gNfcDev.disc.devLimit - gNfcDev.devCnt), + nfcbDevList, &devCnt)); + + gNfcDev.isOperOngoing = true; + return RFAL_ERR_BUSY; + } + + err = rfalNfcbPollerGetCollisionResolutionStatus(); + if (err != RFAL_ERR_BUSY) { + gNfcDev.isTechInit = false; + gNfcDev.techs2do &= ~RFAL_NFC_POLL_TECH_B; + + if ((err == RFAL_ERR_NONE) && (devCnt != 0U)) { + for (i = 0; i < devCnt; i++) /* Copy devices found form local Nfcb list + into global device list */ + { + gNfcDev.devList[gNfcDev.devCnt].type = RFAL_NFC_LISTEN_TYPE_NFCB; + gNfcDev.devList[gNfcDev.devCnt].dev.nfcb = nfcbDevList[i]; + gNfcDev.devCnt++; + } + } + } + + return RFAL_ERR_BUSY; + } +#endif /* RFAL_FEATURE_NFCB*/ + + /*******************************************************************************/ + /* NFC-F Collision Resolution */ + /*******************************************************************************/ +#if RFAL_FEATURE_NFCF + if (((gNfcDev.techsFound & RFAL_NFC_POLL_TECH_F) != 0U) && + ((gNfcDev.techs2do & RFAL_NFC_POLL_TECH_F) != + 0U)) /* If a NFC-F device was found/detected, perform Collision + Resolution */ + { + static rfalNfcfListenDevice nfcfDevList[RFAL_NFC_MAX_DEVICES]; + + if (!gNfcDev.isTechInit) { + RFAL_EXIT_ON_ERR( + err, rfalNfcfPollerInitialize( + gNfcDev.disc.nfcfBR)); /* Initialize RFAL for NFC-F */ + RFAL_EXIT_ON_ERR( + err, + rfalFieldOnAndStartGT()); /* Ensure GT again as other technologies + have also been polled */ + + gNfcDev.isTechInit = true; + gNfcDev.isOperOngoing = false; /* No operation currently ongoing */ + } + + if (!rfalIsGTExpired()) { + return RFAL_ERR_BUSY; + } + + if (!gNfcDev.isOperOngoing) { + RFAL_EXIT_ON_ERR(err, rfalNfcfPollerStartCollisionResolution( + gNfcDev.disc.compMode, + (gNfcDev.disc.devLimit - gNfcDev.devCnt), + nfcfDevList, &devCnt)); + + gNfcDev.isOperOngoing = true; + return RFAL_ERR_BUSY; + } + + err = rfalNfcfPollerGetCollisionResolutionStatus(); + if (err != RFAL_ERR_BUSY) { + gNfcDev.isTechInit = false; + gNfcDev.techs2do &= ~RFAL_NFC_POLL_TECH_F; + + if ((err == RFAL_ERR_NONE) && (devCnt != 0U)) { + for (i = 0; i < devCnt; i++) /* Copy devices found form local Nfcf list + into global device list */ + { + gNfcDev.devList[gNfcDev.devCnt].type = RFAL_NFC_LISTEN_TYPE_NFCF; + gNfcDev.devList[gNfcDev.devCnt].dev.nfcf = nfcfDevList[i]; + gNfcDev.devCnt++; + } + } + } + + return RFAL_ERR_BUSY; + } +#endif /* RFAL_FEATURE_NFCF */ + + /*******************************************************************************/ + /* NFC-V Collision Resolution */ + /*******************************************************************************/ +#if RFAL_FEATURE_NFCV + if (((gNfcDev.techsFound & RFAL_NFC_POLL_TECH_V) != 0U) && + ((gNfcDev.techs2do & RFAL_NFC_POLL_TECH_V) != + 0U)) /* If a NFC-V device was found/detected, perform Collision + Resolution */ + { + rfalNfcvListenDevice nfcvDevList[RFAL_NFC_MAX_DEVICES]; + + if (!gNfcDev.isTechInit) { + RFAL_EXIT_ON_ERR( + err, rfalNfcvPollerInitialize()); /* Initialize RFAL for NFC-V */ + RFAL_EXIT_ON_ERR( + err, + rfalFieldOnAndStartGT()); /* Ensure GT again as other technologies + have also been polled */ + gNfcDev.isTechInit = true; + } + + if (!rfalIsGTExpired()) { + return RFAL_ERR_BUSY; + } + + devCnt = 0; + gNfcDev.isTechInit = false; + gNfcDev.techs2do &= ~RFAL_NFC_POLL_TECH_V; + + err = rfalNfcvPollerCollisionResolution( + RFAL_COMPLIANCE_MODE_NFC, (gNfcDev.disc.devLimit - gNfcDev.devCnt), + nfcvDevList, &devCnt); + if ((err == RFAL_ERR_NONE) && (devCnt != 0U)) { + for (i = 0; i < devCnt; i++) /* Copy devices found form local Nfcf list + into global device list */ + { + gNfcDev.devList[gNfcDev.devCnt].type = RFAL_NFC_LISTEN_TYPE_NFCV; + gNfcDev.devList[gNfcDev.devCnt].dev.nfcv = nfcvDevList[i]; + gNfcDev.devCnt++; + } + } + + return RFAL_ERR_BUSY; + } +#endif /* RFAL_FEATURE_NFCV */ + + /*******************************************************************************/ + /* ST25TB Collision Resolution */ + /*******************************************************************************/ +#if RFAL_FEATURE_ST25TB + if (((gNfcDev.techsFound & RFAL_NFC_POLL_TECH_ST25TB) != 0U) && + ((gNfcDev.techs2do & RFAL_NFC_POLL_TECH_ST25TB) != + 0U)) /* If a ST25TB device was found/detected, perform Collision + Resolution */ + { + rfalSt25tbListenDevice st25tbDevList[RFAL_NFC_MAX_DEVICES]; + + if (!gNfcDev.isTechInit) { + RFAL_EXIT_ON_ERR( + err, rfalSt25tbPollerInitialize()); /* Initialize RFAL for ST25TB */ + RFAL_EXIT_ON_ERR( + err, + rfalFieldOnAndStartGT()); /* Ensure GT again as other technologies + have also been polled */ + gNfcDev.isTechInit = true; + } + + if (!rfalIsGTExpired()) { + return RFAL_ERR_BUSY; + } + + devCnt = 0; + gNfcDev.isTechInit = false; + gNfcDev.techs2do &= ~RFAL_NFC_POLL_TECH_ST25TB; + + err = rfalSt25tbPollerCollisionResolution( + (gNfcDev.disc.devLimit - gNfcDev.devCnt), st25tbDevList, &devCnt); + if ((err == RFAL_ERR_NONE) && (devCnt != 0U)) { + for (i = 0; i < devCnt; i++) /* Copy devices found form local Nfcf list + into global device list */ + { + gNfcDev.devList[gNfcDev.devCnt].type = RFAL_NFC_LISTEN_TYPE_ST25TB; + gNfcDev.devList[gNfcDev.devCnt].dev.st25tb = st25tbDevList[i]; + gNfcDev.devCnt++; + } + } + + return RFAL_ERR_BUSY; + } +#endif /* RFAL_FEATURE_ST25TB */ + + /*******************************************************************************/ + /* Proprietary NFC Collision Resolution */ + /*******************************************************************************/ + if (((gNfcDev.techsFound & RFAL_NFC_POLL_TECH_PROP) != 0U) && + ((gNfcDev.techs2do & RFAL_NFC_POLL_TECH_PROP) != + 0U)) /* If a device was found/detected, perform Collision Resolution */ + { + if (!gNfcDev.isTechInit) { + RFAL_EXIT_ON_ERR(err, + rfalNfcpCbPollerInitialize()); /* Initialize RFAL for + Proprietary NFC */ + RFAL_EXIT_ON_ERR( + err, + rfalFieldOnAndStartGT()); /* Turns the Field On and starts GT timer */ + + gNfcDev.isTechInit = true; /* Technology has been initialized */ + gNfcDev.isOperOngoing = false; /* No operation currently ongoing */ + } + + if (!rfalIsGTExpired()) { + return RFAL_ERR_BUSY; + } + + if (!gNfcDev.isOperOngoing) { + RFAL_EXIT_ON_ERR(err, rfalNfcpCbPollerStartCollisionResolution()); + + gNfcDev.isOperOngoing = true; + return RFAL_ERR_BUSY; + } + + err = rfalNfcpCbPollerGetCollisionResolutionStatus(); + if (err != RFAL_ERR_BUSY) { + gNfcDev.isTechInit = false; + gNfcDev.techs2do &= ~RFAL_NFC_POLL_TECH_PROP; + + if (err == RFAL_ERR_NONE) { + gNfcDev.devCnt = 1; /* Device list held by caller */ + gNfcDev.devList[0].type = RFAL_NFC_LISTEN_TYPE_PROP; + } + } + return RFAL_ERR_BUSY; + } + + return RFAL_ERR_NONE; /* All technologies have been performed */ +} + +/*! + ****************************************************************************** + * \brief Poller Activation + * + * This method Activates a given device according to it's type and + * protocols supported + * + * \param[in] devIt : device's position on the list to be activated + * + * \return RFAL_ERR_NONE : Operation completed with no error + * \return RFAL_ERR_BUSY : Operation ongoing + * \return RFAL_ERR_XXXX : Error occurred + * + ****************************************************************************** + */ +static ReturnCode rfalNfcPollActivation(uint8_t devIt) { + ReturnCode err; + uint8_t devIdx; + rfalNfcaListenDeviceType nfcaType; + + err = RFAL_ERR_NONE; + devIdx = 0; + nfcaType = RFAL_NFCA_T1T; + + /* Suppress warning when specific RFAL features have been disabled */ + RFAL_NO_WARNING(err); + RFAL_NO_WARNING(devIdx); + RFAL_NO_WARNING(nfcaType); + + if (devIt > gNfcDev.devCnt) { + return RFAL_ERR_WRONG_STATE; + } + + switch (gNfcDev.devList[devIt].type) { + /*******************************************************************************/ + /* AP2P Activation */ + /*******************************************************************************/ +#if RFAL_FEATURE_NFC_DEP + case RFAL_NFC_LISTEN_TYPE_AP2P: + /* Activation has already been (ATR_REQ) */ + + gNfcDev.devList[devIt].nfcid = + gNfcDev.devList[devIt].proto.nfcDep.activation.Target.ATR_RES.NFCID3; + gNfcDev.devList[devIt].nfcidLen = RFAL_NFCDEP_NFCID3_LEN; + break; +#endif /* RFAL_FEATURE_NFC_DEP */ + + /*******************************************************************************/ + /* Passive NFC-A Activation */ + /*******************************************************************************/ +#if RFAL_FEATURE_NFCA + case RFAL_NFC_LISTEN_TYPE_NFCA: + + if (!gNfcDev.isTechInit) { + rfalNfcaPollerInitialize(); + gNfcDev.isTechInit = true; + gNfcDev.isOperOngoing = false; + return RFAL_ERR_BUSY; + } + + if (gNfcDev.devList[devIt] + .dev.nfca.isSleep) /* Check if desired device is in Sleep */ + { + if (!gNfcDev.isOperOngoing) { + /* Wake up all cards */ + RFAL_EXIT_ON_ERR( + err, rfalNfcaPollerCheckPresence(RFAL_14443A_SHORTFRAME_CMD_WUPA, + &gNfcDev.sensRes)); + + /* Select specific device */ + RFAL_EXIT_ON_ERR(err, rfalNfcaPollerStartSelect( + gNfcDev.devList[devIt].dev.nfca.nfcId1, + gNfcDev.devList[devIt].dev.nfca.nfcId1Len, + &gNfcDev.devList[devIt].dev.nfca.selRes)); + + gNfcDev.isOperOngoing = true; + } else { + RFAL_EXIT_ON_ERR(err, rfalNfcaPollerGetSelectStatus()); + + /* In case multiple NFC-A devices are present, when activating/waking + a device that is sleeping (not the last one) will make the active + one to go back to IDLE. Marking it as in sleep (Activity 2.2 9.4.4 + Optional Symbol 2) will ensure that gets correctly activated + afterwards */ + for (devIdx = 0; devIdx < gNfcDev.devCnt; devIdx++) { + if (gNfcDev.devList[devIdx].type == RFAL_NFC_LISTEN_TYPE_NFCA) { + gNfcDev.devList[devIdx].dev.nfca.isSleep = true; + } + } + + gNfcDev.devList[devIt].dev.nfca.isSleep = false; + gNfcDev.isOperOngoing = false; + } + return RFAL_ERR_BUSY; + } + + /* Set NFCID */ + gNfcDev.devList[devIt].nfcid = gNfcDev.devList[devIt].dev.nfca.nfcId1; + gNfcDev.devList[devIt].nfcidLen = + gNfcDev.devList[devIt].dev.nfca.nfcId1Len; + + /* If device supports multiple technologies assign protocol requested */ + nfcaType = gNfcDev.devList[devIt].dev.nfca.type; + if (nfcaType == RFAL_NFCA_T4T_NFCDEP) { + nfcaType = + ((gNfcDev.disc.p2pNfcaPrio) ? RFAL_NFCA_NFCDEP : RFAL_NFCA_T4T); + } + + /*******************************************************************************/ + /* Perform protocol specific activation */ + switch (nfcaType) { + /*******************************************************************************/ + case RFAL_NFCA_T1T: + + /* No further activation needed for T1T (RID already performed) */ + + gNfcDev.devList[devIt].nfcid = + gNfcDev.devList[devIt].dev.nfca.ridRes.uid; + gNfcDev.devList[devIt].nfcidLen = RFAL_T1T_UID_LEN; + + gNfcDev.devList[devIt].rfInterface = RFAL_NFC_INTERFACE_RF; + break; + + case RFAL_NFCA_T2T: + + /* No further activation needed for a T2T */ + + gNfcDev.devList[devIt].rfInterface = RFAL_NFC_INTERFACE_RF; + break; + + /*******************************************************************************/ + case RFAL_NFCA_T4T: /* Device supports ISO-DEP */ + +#if RFAL_FEATURE_ISO_DEP && RFAL_FEATURE_ISO_DEP_POLL + if (!gNfcDev.isOperOngoing) { + /* Perform ISO-DEP (ISO14443-4) activation: RATS and PPS if + * supported */ + rfalIsoDepInitializeWithParams( + gNfcDev.disc.compMode, RFAL_ISODEP_MAX_R_RETRYS, + RFAL_ISODEP_MAX_WTX_NACK_RETRYS, RFAL_ISODEP_MAX_WTX_RETRYS, + RFAL_ISODEP_MAX_DSL_RETRYS, RFAL_ISODEP_MAX_I_RETRYS, + RFAL_ISODEP_RATS_RETRIES); + RFAL_EXIT_ON_ERR(err, rfalIsoDepPollAStartActivation( + gNfcDev.disc.isoDepFS, RFAL_ISODEP_NO_DID, + gNfcDev.disc.maxBR, + &gNfcDev.devList[devIt].proto.isoDep)); + + gNfcDev.isOperOngoing = true; + return RFAL_ERR_BUSY; + } + + err = rfalIsoDepPollAGetActivationStatus(); + if (err != RFAL_ERR_NONE) { + return err; + } + + gNfcDev.devList[devIt].rfInterface = + RFAL_NFC_INTERFACE_ISODEP; /* NFC-A T4T device activated */ +#else + gNfcDev.devList[devIt].rfInterface = + RFAL_NFC_INTERFACE_RF; /* No ISO-DEP supported activate using RF + interface */ +#endif /* RFAL_FEATURE_ISO_DEP_POLL */ + break; + + /*******************************************************************************/ + case RFAL_NFCA_NFCDEP: /* Device supports NFC-DEP */ + +#if RFAL_FEATURE_NFC_DEP + /* Perform NFC-DEP (P2P) activation: ATR and PSL if supported */ + RFAL_EXIT_ON_ERR( + err, rfalNfcNfcDepActivate(&gNfcDev.devList[devIt], + RFAL_NFCDEP_COMM_PASSIVE, NULL, 0)); + + gNfcDev.devList[devIt].nfcid = + gNfcDev.devList[devIt] + .proto.nfcDep.activation.Target.ATR_RES.NFCID3; + gNfcDev.devList[devIt].nfcidLen = RFAL_NFCDEP_NFCID3_LEN; + + gNfcDev.devList[devIt].rfInterface = + RFAL_NFC_INTERFACE_NFCDEP; /* NFC-A P2P device activated */ +#else + gNfcDev.devList[devIt].rfInterface = + RFAL_NFC_INTERFACE_RF; /* No NFC-DEP supported activate using RF + interface */ +#endif /* RFAL_FEATURE_NFC_DEP */ + break; + + /*******************************************************************************/ + case RFAL_NFCA_T4T_NFCDEP: /* Multiple proto resolved based on NFCA P2P + Prio config */ + default: + return RFAL_ERR_WRONG_STATE; + } + break; +#endif /* RFAL_FEATURE_NFCA */ + + /*******************************************************************************/ + /* Passive NFC-B Activation */ + /*******************************************************************************/ +#if RFAL_FEATURE_NFCB + case RFAL_NFC_LISTEN_TYPE_NFCB: + + if (!gNfcDev.isTechInit) { + rfalNfcbPollerInitialize(); + gNfcDev.isTechInit = true; + gNfcDev.isOperOngoing = false; + + if (gNfcDev.devList[devIt] + .dev.nfcb.isSleep) /* Check if desired device is in Sleep */ + { + /* Wake up all cards. SENSB_RES may return collision but the NFCID0 is + * available to explicitly select NFC-B card via ATTRIB; so error will + * be ignored here */ + rfalNfcbPollerStartCheckPresence( + RFAL_NFCB_SENS_CMD_ALLB_REQ, RFAL_NFCB_SLOT_NUM_1, + &gNfcDev.sensbRes, &gNfcDev.sensbResLen); + } + + return RFAL_ERR_BUSY; + } + + if (gNfcDev.devList[devIt] + .dev.nfcb.isSleep) /* Check if desired device is still in Sleep */ + { + /* Wake up all cards. SENSB_RES may return collision but the NFCID0 is + * available to explicitly select NFC-B card via ATTRIB; so error will + * be ignored here */ + RFAL_EXIT_ON_BUSY(err, rfalNfcbPollerGetCheckPresenceStatus()); + + gNfcDev.devList[devIt].dev.nfcb.isSleep = false; + } + + /* Set NFCID */ + gNfcDev.devList[devIt].nfcid = + gNfcDev.devList[devIt].dev.nfcb.sensbRes.nfcid0; + gNfcDev.devList[devIt].nfcidLen = RFAL_NFCB_NFCID0_LEN; + +#if RFAL_FEATURE_ISO_DEP && RFAL_FEATURE_ISO_DEP_POLL + /* Check if device supports ISO-DEP (ISO14443-4) */ + if ((gNfcDev.devList[devIt].dev.nfcb.sensbRes.protInfo.FsciProType & + RFAL_NFCB_SENSB_RES_PROTO_ISO_MASK) != 0U) { + if (!gNfcDev.isOperOngoing) { + rfalIsoDepInitializeWithParams( + gNfcDev.disc.compMode, RFAL_ISODEP_MAX_R_RETRYS, + RFAL_ISODEP_MAX_WTX_NACK_RETRYS, RFAL_ISODEP_MAX_WTX_RETRYS, + RFAL_ISODEP_MAX_DSL_RETRYS, RFAL_ISODEP_MAX_I_RETRYS, + RFAL_ISODEP_RATS_RETRIES); + /* Perform ISO-DEP (ISO14443-4) activation: ATTRIB */ + RFAL_EXIT_ON_ERR(err, rfalIsoDepPollBStartActivation( + gNfcDev.disc.isoDepFS, RFAL_ISODEP_NO_DID, + gNfcDev.disc.maxBR, 0x00, + &gNfcDev.devList[devIt].dev.nfcb, NULL, 0, + &gNfcDev.devList[devIt].proto.isoDep)); + + gNfcDev.isOperOngoing = true; + return RFAL_ERR_BUSY; + } + + err = rfalIsoDepPollBGetActivationStatus(); + if (err != RFAL_ERR_NONE) { + return err; + } + + gNfcDev.devList[devIt].rfInterface = + RFAL_NFC_INTERFACE_ISODEP; /* NFC-B T4T device activated */ + break; + } + +#endif /* RFAL_FEATURE_ISO_DEP_POLL */ + + gNfcDev.devList[devIt].rfInterface = + RFAL_NFC_INTERFACE_RF; /* NFC-B device activated */ + break; + +#endif /* RFAL_FEATURE_NFCB */ + + /*******************************************************************************/ + /* Passive NFC-F Activation */ + /*******************************************************************************/ +#if RFAL_FEATURE_NFCF + case RFAL_NFC_LISTEN_TYPE_NFCF: + + rfalNfcfPollerInitialize(gNfcDev.disc.nfcfBR); + +#if RFAL_FEATURE_NFC_DEP + if (rfalNfcfIsNfcDepSupported(&gNfcDev.devList[devIt].dev.nfcf)) { + /* Perform NFC-DEP (P2P) activation: ATR and PSL if supported */ + RFAL_EXIT_ON_ERR( + err, rfalNfcNfcDepActivate(&gNfcDev.devList[devIt], + RFAL_NFCDEP_COMM_PASSIVE, NULL, 0)); + + /* Set NFCID */ + gNfcDev.devList[devIt].nfcid = + gNfcDev.devList[devIt] + .proto.nfcDep.activation.Target.ATR_RES.NFCID3; + gNfcDev.devList[devIt].nfcidLen = RFAL_NFCDEP_NFCID3_LEN; + + gNfcDev.devList[devIt].rfInterface = + RFAL_NFC_INTERFACE_NFCDEP; /* NFC-F P2P device activated */ + break; + } +#endif /* RFAL_FEATURE_NFC_DEP */ + + /* Set NFCID */ + gNfcDev.devList[devIt].nfcid = + gNfcDev.devList[devIt].dev.nfcf.sensfRes.NFCID2; + gNfcDev.devList[devIt].nfcidLen = RFAL_NFCF_NFCID2_LEN; + + gNfcDev.devList[devIt].rfInterface = + RFAL_NFC_INTERFACE_RF; /* NFC-F T3T device activated */ + break; +#endif /* RFAL_FEATURE_NFCF */ + + /*******************************************************************************/ + /* Passive NFC-V Activation */ + /*******************************************************************************/ +#if RFAL_FEATURE_NFCV + case RFAL_NFC_LISTEN_TYPE_NFCV: + + rfalNfcvPollerInitialize(); + + /* No specific activation needed for a T5T */ + + /* Set NFCID */ + gNfcDev.devList[devIt].nfcid = gNfcDev.devList[devIt].dev.nfcv.InvRes.UID; + gNfcDev.devList[devIt].nfcidLen = RFAL_NFCV_UID_LEN; + + gNfcDev.devList[devIt].rfInterface = + RFAL_NFC_INTERFACE_RF; /* NFC-V T5T device activated */ + break; +#endif /* RFAL_FEATURE_NFCV */ + + /*******************************************************************************/ + /* Passive ST25TB Activation */ + /*******************************************************************************/ +#if RFAL_FEATURE_ST25TB + case RFAL_NFC_LISTEN_TYPE_ST25TB: + + rfalSt25tbPollerInitialize(); + + /* No specific activation needed for a ST25TB */ + + /* Set NFCID */ + gNfcDev.devList[devIt].nfcid = gNfcDev.devList[devIt].dev.st25tb.UID; + gNfcDev.devList[devIt].nfcidLen = RFAL_ST25TB_UID_LEN; + + gNfcDev.devList[devIt].rfInterface = + RFAL_NFC_INTERFACE_RF; /* ST25TB device activated */ + break; +#endif /* RFAL_FEATURE_ST25TB */ + + /*******************************************************************************/ + /* Passive Proprietary NFC Activation */ + /*******************************************************************************/ + case RFAL_NFC_LISTEN_TYPE_PROP: + + if (!gNfcDev.isTechInit) { + RFAL_EXIT_ON_ERR(err, rfalNfcpCbPollerInitialize()); + gNfcDev.isTechInit = true; + gNfcDev.isOperOngoing = false; + return RFAL_ERR_BUSY; + } + + if (!gNfcDev.isOperOngoing) { + /* Start activation */ + RFAL_EXIT_ON_ERR(err, rfalNfcpCbStartActivation()); + + gNfcDev.isOperOngoing = true; + return RFAL_ERR_BUSY; + } + + err = rfalNfcpCbGetActivationStatus(); + if (err != RFAL_ERR_NONE) { + return err; + } + + /* Clear NFCID */ + gNfcDev.devList[devIt].nfcid = NULL; + gNfcDev.devList[devIt].nfcidLen = 0; + + gNfcDev.devList[devIt].rfInterface = RFAL_NFC_INTERFACE_RF; + break; + + /*******************************************************************************/ + default: + return RFAL_ERR_WRONG_STATE; + } + + gNfcDev.activeDev = + &gNfcDev.devList[devIt]; /* Assign active device to be used further on */ + gNfcDev.isOperOngoing = false; + return RFAL_ERR_NONE; +} + +/*! + ****************************************************************************** + * \brief Listener Activation + * + * This method handles the listen mode Activation according to the different + * protocols the Reader/Initiator performs + * + * \return RFAL_ERR_NONE : Operation completed with no error + * \return RFAL_ERR_BUSY : Operation ongoing + * \return RFAL_ERR_PROTO : Unexpected frame received + * \return RFAL_ERR_XXXX : Error occurred + * + ****************************************************************************** + */ +#if RFAL_FEATURE_LISTEN_MODE +static ReturnCode rfalNfcListenActivation(void) { + bool isDataRcvd; + ReturnCode ret; + rfalLmState lmSt; + rfalBitRate bitRate; +#if RFAL_FEATURE_NFC_DEP + uint8_t hdrLen; + + /* Set the header length in NFC-A */ + hdrLen = (RFAL_NFCDEP_SB_LEN + RFAL_NFCDEP_LEN_LEN); +#endif /* RFAL_FEATURE_NFC_DEP */ + + lmSt = rfalListenGetState(&isDataRcvd, &bitRate); + switch (lmSt) { +#if RFAL_FEATURE_NFCA + /*******************************************************************************/ + case RFAL_LM_STATE_ACTIVE_A: /* NFC-A CE activation */ + case RFAL_LM_STATE_ACTIVE_Ax: + + if (isDataRcvd) /* Check if Reader/Initator has sent some data */ + { + /* Check if received data is a Sleep request */ + if (rfalNfcaListenerIsSleepReq( + gNfcDev.rxBuf.rfBuf, + rfalConvBitsToBytes( + gNfcDev.rxLen))) /* Check if received data is a SLP_REQ */ + { + /* Set the Listen Mode in Sleep state */ + RFAL_EXIT_ON_ERR(ret, + rfalListenSleepStart( + RFAL_LM_STATE_SLEEP_A, gNfcDev.rxBuf.rfBuf, + sizeof(gNfcDev.rxBuf.rfBuf), &gNfcDev.rxLen)); + } +#if RFAL_FEATURE_ISO_DEP && RFAL_FEATURE_ISO_DEP_LISTEN + /* Check if received data is a valid RATS */ + else if (rfalIsoDepIsRats( + gNfcDev.rxBuf.rfBuf, + (uint8_t)rfalConvBitsToBytes(gNfcDev.rxLen))) { + rfalIsoDepAtsParam atsParam; + rfalIsoDepListenActvParam rxParam; + + /* Set ATS parameters */ + atsParam.fsci = (uint8_t)RFAL_ISODEP_DEFAULT_FSCI; + atsParam.fwi = RFAL_ISODEP_DEFAULT_FWI; + atsParam.sfgi = RFAL_ISODEP_DEFAULT_SFGI; + atsParam.didSupport = false; + atsParam.ta = RFAL_ISODEP_ATS_TA_SAME_D; + atsParam.hb = NULL; + atsParam.hbLen = 0; + + /* Set Rx parameters */ + rxParam.rxBuf = + (rfalIsoDepBufFormat *)&gNfcDev.rxBuf + .isoDepBuf; /* PRQA S 0310 # MISRA 11.3 - Intentional safe + cast to avoiding large buffer duplication */ + rxParam.rxLen = &gNfcDev.rxLen; + rxParam.isoDepDev = &gNfcDev.devList->proto.isoDep; + rxParam.isRxChaining = &gNfcDev.isRxChaining; + + rfalListenSetState( + RFAL_LM_STATE_CARDEMU_4A); /* Set next state CE T4T */ + rfalIsoDepInitialize(); /* Initialize ISO-DEP layer to handle + ISO14443-a activation / RATS */ + + /* Set ISO-DEP layer to digest RATS and handle activation */ + RFAL_EXIT_ON_ERR(ret, rfalIsoDepListenStartActivation( + &atsParam, NULL, gNfcDev.rxBuf.rfBuf, + gNfcDev.rxLen, rxParam)); + } +#endif /* RFAL_FEATURE_ISO_DEP_LISTEN */ + +#if RFAL_FEATURE_NFC_DEP + + /* Check if received data is a valid ATR_REQ */ + else if (rfalNfcDepIsAtrReq( + &gNfcDev.rxBuf.rfBuf[hdrLen], + (rfalConvBitsToBytes(gNfcDev.rxLen) - hdrLen), + gNfcDev.devList->nfcid)) { + gNfcDev.devList->type = RFAL_NFC_POLL_TYPE_NFCA; + RFAL_EXIT_ON_ERR(ret, + rfalNfcNfcDepActivate( + gNfcDev.devList, RFAL_NFCDEP_COMM_PASSIVE, + &gNfcDev.rxBuf.rfBuf[hdrLen], + (rfalConvBitsToBytes(gNfcDev.rxLen) - hdrLen))); + } +#endif /* RFAL_FEATURE_NFC_DEP */ + + else { + return RFAL_ERR_PROTO; + } + } + return RFAL_ERR_BUSY; + +#endif /* RFAL_FEATURE_NFCA */ + +#if RFAL_FEATURE_ISO_DEP && RFAL_FEATURE_ISO_DEP_LISTEN + /*******************************************************************************/ + case RFAL_LM_STATE_CARDEMU_4A: /* T4T ISO-DEP activation */ + + ret = rfalIsoDepListenGetActivationStatus(); + if (ret == RFAL_ERR_NONE) { + gNfcDev.devList->type = RFAL_NFC_POLL_TYPE_NFCA; + gNfcDev.devList->rfInterface = RFAL_NFC_INTERFACE_ISODEP; + gNfcDev.devList->nfcid = NULL; + gNfcDev.devList->nfcidLen = 0; + } + return ( + (ret == RFAL_ERR_LINK_LOSS) + ? RFAL_ERR_PROTO + : ret); /* Link loss during protocol activation, reMap error */ + +#endif /* RFAL_FEATURE_ISO_DEP_LISTEN */ + + /*******************************************************************************/ + case RFAL_LM_STATE_READY_F: /* NFC-F CE activation */ + + if (isDataRcvd) /* Wait for the first received data */ + { +#if RFAL_FEATURE_NFC_DEP + /* Set the header length in NFC-F */ + hdrLen = RFAL_NFCDEP_LEN_LEN; + + if (rfalNfcDepIsAtrReq(&gNfcDev.rxBuf.rfBuf[hdrLen], + (rfalConvBitsToBytes(gNfcDev.rxLen) - hdrLen), + gNfcDev.devList->nfcid)) { + gNfcDev.devList->type = RFAL_NFC_POLL_TYPE_NFCF; + RFAL_EXIT_ON_ERR(ret, + rfalNfcNfcDepActivate( + gNfcDev.devList, RFAL_NFCDEP_COMM_PASSIVE, + &gNfcDev.rxBuf.rfBuf[hdrLen], + (rfalConvBitsToBytes(gNfcDev.rxLen) - hdrLen))); + } else +#endif /* RFAL_FEATURE_NFC_DEP */ + { + rfalListenSetState( + RFAL_LM_STATE_CARDEMU_3); /* First data already received - set T3T + CE */ + } + } + return RFAL_ERR_BUSY; + + /*******************************************************************************/ + case RFAL_LM_STATE_CARDEMU_3: /* T3T activated */ + + gNfcDev.devList->type = RFAL_NFC_POLL_TYPE_NFCF; + gNfcDev.devList->rfInterface = RFAL_NFC_INTERFACE_RF; + gNfcDev.devList->nfcid = NULL; + gNfcDev.devList->nfcidLen = 0; + + return RFAL_ERR_NONE; + +#if RFAL_FEATURE_NFC_DEP + /*******************************************************************************/ + case RFAL_LM_STATE_TARGET_A: /* NFC-DEP activation */ + case RFAL_LM_STATE_TARGET_F: + + ret = rfalNfcDepListenGetActivationStatus(); + if (ret == RFAL_ERR_NONE) { + gNfcDev.devList->rfInterface = RFAL_NFC_INTERFACE_NFCDEP; + gNfcDev.devList->nfcid = + gNfcDev.devList->proto.nfcDep.activation.Initiator.ATR_REQ.NFCID3; + gNfcDev.devList->nfcidLen = RFAL_NFCDEP_NFCID3_LEN; + } + return ret; +#endif /* RFAL_FEATURE_NFC_DEP */ + + /*******************************************************************************/ + case RFAL_LM_STATE_IDLE: /* AP2P activation */ + if (isDataRcvd) /* Check if Reader/Initator has sent some data */ + { + if ((gNfcDev.lmMask & RFAL_LM_MASK_ACTIVE_P2P) != + 0U) /* Check if AP2P is enabled */ + { +#if RFAL_FEATURE_NFC_DEP + /* Calculate the header length in NFC-A or NFC-F mode*/ + hdrLen = ((bitRate == RFAL_BR_106) + ? (RFAL_NFCDEP_SB_LEN + RFAL_NFCDEP_LEN_LEN) + : RFAL_NFCDEP_LEN_LEN); + + if (rfalNfcDepIsAtrReq(&gNfcDev.rxBuf.rfBuf[hdrLen], + (rfalConvBitsToBytes(gNfcDev.rxLen) - hdrLen), + NULL)) { + gNfcDev.devList->type = RFAL_NFC_POLL_TYPE_AP2P; + rfalSetMode((RFAL_MODE_LISTEN_ACTIVE_P2P), bitRate, bitRate); + rfalSetFDTListen(RFAL_FDT_LISTEN_AP2P_LISTENER); + RFAL_EXIT_ON_ERR( + ret, rfalNfcNfcDepActivate( + gNfcDev.devList, RFAL_NFCDEP_COMM_ACTIVE, + &gNfcDev.rxBuf.rfBuf[hdrLen], + (rfalConvBitsToBytes(gNfcDev.rxLen) - hdrLen))); + } else +#endif /* RFAL_FEATURE_NFC_DEP */ + { + return RFAL_ERR_PROTO; + } + } + } + return RFAL_ERR_BUSY; + + /*******************************************************************************/ + case RFAL_LM_STATE_READY_A: + case RFAL_LM_STATE_READY_Ax: + case RFAL_LM_STATE_SLEEP_A: + case RFAL_LM_STATE_SLEEP_AF: + return RFAL_ERR_BUSY; + + /*******************************************************************************/ + case RFAL_LM_STATE_POWER_OFF: + return RFAL_ERR_LINK_LOSS; + + default: /* Wait for activation */ + break; + } + + return RFAL_ERR_INTERNAL; +} +#endif /* RFAL_FEATURE_LISTEN_MODE */ + +/*! + ****************************************************************************** + * \brief Poller NFC DEP Activate + * + * This method performs NFC-DEP Activation + * + * \param[in] device : device info + * \param[in] commMode : communication mode (Passive/Active) + * \param[in] atrReq : received ATR_REQ + * \param[in] atrReqLen : received ATR_REQ size + * + * \return RFAL_ERR_NONE : Operation completed with no error + * \return RFAL_ERR_BUSY : Operation ongoing + * \return RFAL_ERR_XXXX : Error occurred + * + ****************************************************************************** + */ +#if RFAL_FEATURE_NFC_DEP +static ReturnCode rfalNfcNfcDepActivate(rfalNfcDevice *device, + rfalNfcDepCommMode commMode, + const uint8_t *atrReq, + uint16_t atrReqLen) { + rfalNfcDepAtrParam initParam; + + /* Suppress warnings if Listen mode is disabled */ + RFAL_NO_WARNING(atrReq); + RFAL_NO_WARNING(atrReqLen); + + /* If we are in Poll mode */ + if (gNfcDev.state < RFAL_NFC_STATE_LISTEN_TECHDETECT) { + /*******************************************************************************/ + /* If Passive F use the NFCID2 retrieved from SENSF */ + if (device->type == RFAL_NFC_LISTEN_TYPE_NFCF) { + initParam.nfcid = device->dev.nfcf.sensfRes.NFCID2; + initParam.nfcidLen = RFAL_NFCF_NFCID2_LEN; + } else { + initParam.nfcid = gNfcDev.disc.nfcid3; + initParam.nfcidLen = RFAL_NFCDEP_NFCID3_LEN; + } + + initParam.BS = RFAL_NFCDEP_Bx_NO_HIGH_BR; + initParam.BR = RFAL_NFCDEP_Bx_NO_HIGH_BR; + initParam.DID = RFAL_NFCDEP_DID_NO; + initParam.NAD = RFAL_NFCDEP_NAD_NO; + initParam.LR = gNfcDev.disc.nfcDepLR; + initParam.GB = gNfcDev.disc.GB; + initParam.GBLen = gNfcDev.disc.GBLen; + initParam.commMode = commMode; + initParam.operParam = + (RFAL_NFCDEP_OPER_FULL_MI_EN | RFAL_NFCDEP_OPER_EMPTY_DEP_DIS | + RFAL_NFCDEP_OPER_ATN_EN | RFAL_NFCDEP_OPER_RTOX_REQ_EN); + + rfalNfcDepInitialize(); + /* Perform NFC-DEP (P2P) activation: ATR and PSL if supported */ + return rfalNfcDepInitiatorHandleActivation(&initParam, gNfcDev.disc.maxBR, + &device->proto.nfcDep); + } + /* If we are in Listen mode */ +#if RFAL_FEATURE_LISTEN_MODE + else if (rfalNfcIsRemDevPoller(device->type) && + (gNfcDev.state >= RFAL_NFC_STATE_LISTEN_TECHDETECT)) { + rfalNfcDepListenActvParam actvParams; + rfalNfcDepTargetParam targetParam; + + RFAL_MEMCPY(targetParam.nfcid3, (uint8_t *)gNfcDev.disc.nfcid3, + RFAL_NFCDEP_NFCID3_LEN); + targetParam.bst = RFAL_NFCDEP_Bx_NO_HIGH_BR; + targetParam.brt = RFAL_NFCDEP_Bx_NO_HIGH_BR; + targetParam.to = RFAL_NFCDEP_WT_TRG_MAX_L13; /* [LLCP] 1.3 6.2.1 */ + targetParam.ppt = rfalNfcDepLR2PP(gNfcDev.disc.nfcDepLR); + if (gNfcDev.disc.GBLen >= RFAL_NFCDEP_GB_MAX_LEN) { + return RFAL_ERR_PARAM; + } + targetParam.GBtLen = gNfcDev.disc.GBLen; + if (gNfcDev.disc.GBLen > 0U) { + RFAL_MEMCPY(targetParam.GBt, gNfcDev.disc.GB, gNfcDev.disc.GBLen); + } + targetParam.operParam = + (RFAL_NFCDEP_OPER_FULL_MI_EN | RFAL_NFCDEP_OPER_EMPTY_DEP_DIS | + RFAL_NFCDEP_OPER_ATN_EN | RFAL_NFCDEP_OPER_RTOX_REQ_EN); + targetParam.commMode = commMode; + + /* Set activation buffer (including header) for NFC-DEP */ + actvParams.rxBuf = + (rfalNfcDepBufFormat *)&gNfcDev.rxBuf + .nfcDepBuf; /* PRQA S 0310 # MISRA 11.3 - Intentional safe cast to + avoiding large buffer duplication */ + actvParams.rxLen = &gNfcDev.rxLen; + actvParams.isRxChaining = &gNfcDev.isRxChaining; + actvParams.nfcDepDev = &gNfcDev.devList->proto.nfcDep; + + rfalListenSetState(((device->type == RFAL_NFC_POLL_TYPE_NFCA) + ? RFAL_LM_STATE_TARGET_A + : RFAL_LM_STATE_TARGET_F)); + + rfalNfcDepInitialize(); + /* Perform NFC-DEP (P2P) activation: send ATR_RES and handle activation */ + return rfalNfcDepListenStartActivation(&targetParam, atrReq, atrReqLen, + actvParams); + } +#endif /* RFAL_FEATURE_LISTEN_MODE */ + + else { + return RFAL_ERR_INTERNAL; + } +} +#endif /* RFAL_FEATURE_NFC_DEP */ + +/*! + ****************************************************************************** + * \brief Poller NFC Deactivate + * + * This method Deactivates the device if a deactivation procedure exists + * + * \return RFAL_ERR_NONE : Operation completed with no error + * \return RFAL_ERR_BUSY : Operation ongoing + * \return RFAL_ERR_XXXX : Error occurred + * + ****************************************************************************** + */ +static ReturnCode rfalNfcDeactivation(void) { + bool aux; + ReturnCode ret; + + ret = RFAL_ERR_NONE; + aux = false; + + /* Suppress warning when specific RFAL features have been disabled */ + RFAL_NO_WARNING(ret); + + /* Check if a device has been activated */ + if (gNfcDev.activeDev != NULL) { + if (rfalNfcIsRemDevListener( + gNfcDev.activeDev->type)) /* Listen mode no additional deactivation + to be performed*/ + { + switch (gNfcDev.activeDev->rfInterface) { + /*******************************************************************************/ + case RFAL_NFC_INTERFACE_RF: + break; /* No specific deactivation to be performed */ + + /*******************************************************************************/ +#if RFAL_FEATURE_ISO_DEP && RFAL_FEATURE_ISO_DEP_POLL + case RFAL_NFC_INTERFACE_ISODEP: + + if (!gNfcDev.isOperOngoing) { + ret = rfalIsoDepStartDeselect(); + if (ret == RFAL_ERR_NONE) /* Send a Deselect to device */ + { + gNfcDev.isOperOngoing = true; + return RFAL_ERR_BUSY; + } + } else { + RFAL_EXIT_ON_BUSY( + ret, rfalIsoDepGetDeselectStatus()); /* Check if deselection has + finished */ + + aux = true; /* Mark device as deselected */ + gNfcDev.isOperOngoing = false; + } + break; +#endif /* RFAL_FEATURE_ISO_DEP_POLL */ + + /*******************************************************************************/ +#if RFAL_FEATURE_NFC_DEP + case RFAL_NFC_INTERFACE_NFCDEP: + switch (gNfcDev.activeDev->type) { + case RFAL_NFC_LISTEN_TYPE_AP2P: + rfalNfcDepRLS(); /* Send a Release to device */ + break; + + default: + rfalNfcDepDSL(); /* Send a Deselect to device */ + aux = true; /* Mark device as deselected */ + break; + } + break; +#endif /* RFAL_FEATURE_NFC_DEP */ + + default: + return RFAL_ERR_REQUEST; + } + } + } + + /* If deactivation type is only to Sleep, mark it and keep Field On */ + if ((gNfcDev.deactType == RFAL_NFC_DEACTIVATE_SLEEP) && + (gNfcDev.activeDev != NULL) && (aux)) { + gNfcDev.isOperOngoing = false; + + if (gNfcDev.activeDev->type == RFAL_NFC_LISTEN_TYPE_NFCA) { + gNfcDev.activeDev->dev.nfca.isSleep = true; + } else if (gNfcDev.activeDev->type == RFAL_NFC_LISTEN_TYPE_NFCB) { + gNfcDev.activeDev->dev.nfcb.isSleep = true; + } else { + /* MISRA 15.7 - Empty else */ + } + } else { + if (!gNfcDev.isDeactivating) /* Check if the Field deactivation has not + started */ + { +#if RFAL_FEATURE_WAKEUP_MODE + rfalWakeUpModeStop(); +#endif /* RFAL_FEATURE_WAKEUP_MODE */ + +#if RFAL_FEATURE_LISTEN_MODE + rfalListenStop(); +#else + rfalFieldOff(); +#endif + + if ((gNfcDev.isFieldOn) && + rfalNfcHasPollerTechs()) /* Check if configured to Poll modes and the + Field is On */ + { + aux = platformTimerIsExpired( + gNfcDev + .discTmr); /* Check total duration timer is already expired */ + if (((platformGetSysTick() + RFAL_NFC_T_FIELD_OFF) > gNfcDev.discTmr) || + (aux)) /* In case Total Duration has expired or expring in less than + tFIELD_OFF */ + { + platformTimerDestroy(gNfcDev.discTmr); + gNfcDev.discTmr = (uint32_t)platformTimerCreate( + RFAL_NFC_T_FIELD_OFF); /* Ensure that Operating Field is in Off + condition at least tFIELD_OFF */ + } + + gNfcDev.isDeactivating = true; + return RFAL_ERR_BUSY; + } + } else /* The Field deactivation has started */ + { + if (!platformTimerIsExpired(gNfcDev.discTmr)) { + return RFAL_ERR_BUSY; /* Ensure Operating Field in Off condition for the + time remaining */ + } + } + } + + gNfcDev.activeDev = NULL; /* Clear Active Device info */ + gNfcDev.isDeactivating = false; + gNfcDev.isTechInit = false; + gNfcDev.isFieldOn = false; + return RFAL_ERR_NONE; +} diff --git a/core/embed/io/nfc/rfal/source/rfal_nfcDep.c b/core/embed/io/nfc/rfal/source/rfal_nfcDep.c new file mode 100644 index 0000000000..7094e4b042 --- /dev/null +++ b/core/embed/io/nfc/rfal/source/rfal_nfcDep.c @@ -0,0 +1,2827 @@ + +/****************************************************************************** + * @attention + * + * COPYRIGHT 2016 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/* + * PROJECT: NFCC firmware + * LANGUAGE: ISO C99 + */ + +/*! \file rfal_nfcDep.c + * + * \author Gustavo Patricio + * + * \brief Implementation of NFC-DEP protocol + * + * NFC-DEP is also known as NFCIP - Near Field Communication + * Interface and Protocol + * + * This implementation was based on the following specs: + * - NFC Forum Digital 1.1 + * - ECMA 340 3rd Edition 2013 + * + */ + +/* + ****************************************************************************** + * INCLUDES + ****************************************************************************** + */ +#include "rfal_nfcDep.h" +#include "rfal_nfcf.h" +#include "rfal_utils.h" + +/* + ****************************************************************************** + * ENABLE SWITCH + ****************************************************************************** + */ + +/* Feature switch may be enabled or disabled by user at rfal_platform.h + * Default configuration (ST25R dependant) also provided at rfal_defConfig.h + * + * RFAL_FEATURE_NFC_DEP + */ + +#if RFAL_FEATURE_NFC_DEP + +/* Check for valid Block/Payload length Digital 2.0 Table 90*/ +#if ((RFAL_FEATURE_NFC_DEP_BLOCK_MAX_LEN != 64) && \ + (RFAL_FEATURE_NFC_DEP_BLOCK_MAX_LEN != 128) && \ + (RFAL_FEATURE_NFC_DEP_BLOCK_MAX_LEN != 192) && \ + (RFAL_FEATURE_NFC_DEP_BLOCK_MAX_LEN != 254)) +#error \ + " RFAL: Invalid NFC-DEP Block Max length. Please change RFAL_FEATURE_NFC_DEP_BLOCK_MAX_LEN. " +#endif + +/* Check for valid PDU length */ +#if ((RFAL_FEATURE_NFC_DEP_PDU_MAX_LEN < RFAL_FEATURE_NFC_DEP_BLOCK_MAX_LEN)) +#error \ + " RFAL: Invalid NFC-DEP PDU Max length. Please change RFAL_FEATURE_NFC_DEP_PDU_MAX_LEN. " +#endif + +/* + ****************************************************************************** + * DEFINES + ****************************************************************************** + */ +#define NFCIP_ATR_RETRY_MAX \ + 2U /*!< Max consecutive retrys of an ATR REQ with transm error*/ + +#define NFCIP_PSLPAY_LEN \ + (2U) /*!< PSL Payload length (BRS + FSL) */ +#define NFCIP_PSLREQ_LEN \ + (3U + RFAL_NFCDEP_LEN_LEN) /*!< PSL REQ length (incl LEN) */ +#define NFCIP_PSLRES_LEN \ + (3U + RFAL_NFCDEP_LEN_LEN) /*!< PSL RES length (incl LEN) */ + +#define NFCIP_ATRREQ_BUF_LEN \ + (RFAL_NFCDEP_ATRREQ_MAX_LEN + \ + RFAL_NFCDEP_LEN_LEN) /*!< ATR REQ max length (incl LEN) */ +#define NFCIP_ATRRES_BUF_LEN \ + (RFAL_NFCDEP_ATRRES_MAX_LEN + \ + RFAL_NFCDEP_LEN_LEN) /*!< ATR RES max length (incl LEN) */ + +#define NFCIP_RLSREQ_LEN \ + (3U + RFAL_NFCDEP_LEN_LEN) /*!< RLS REQ length (incl LEN) */ +#define NFCIP_RLSRES_LEN \ + (3U + RFAL_NFCDEP_LEN_LEN) /*!< RSL RES length (incl LEN) */ +#define NFCIP_RLSRES_MIN \ + (2U + RFAL_NFCDEP_LEN_LEN) /*!< Minimum length for a RLS RES (incl LEN) */ + +#define NFCIP_DSLREQ_LEN \ + (3U + RFAL_NFCDEP_LEN_LEN) /*!< DSL REQ length (incl LEN) */ +#define NFCIP_DSLRES_LEN \ + (3U + RFAL_NFCDEP_LEN_LEN) /*!< DSL RES length (incl LEN) */ +#define NFCIP_DSLRES_MIN \ + (2U + RFAL_NFCDEP_LEN_LEN) /*!< Minimum length for a DSL RES (incl LEN) */ + +#define NFCIP_DSLRES_MAX_LEN \ + (3U + RFAL_NFCDEP_LEN_LEN) /*!< Maximum length for a DSL RES (incl LEN) */ +#define NFCIP_RLSRES_MAX_LEN \ + (3U + RFAL_NFCDEP_LEN_LEN) /*!< Minimum length for a RLS RES (incl LEN) */ +#define NFCIP_TARGET_RES_MAX \ + (RFAL_MAX(NFCIP_RLSRES_MAX_LEN, \ + NFCIP_DSLRES_MAX_LEN)) /*!< Max target control res length */ + +#define NFCIP_NO_FWT \ + RFAL_FWT_NONE /*!< No FWT value - Target Mode */ +#define NFCIP_INIT_MIN_RTOX \ + 1U /*!< Minimum RTOX value Digital 1.0 14.8.4.1 */ +#define NFCIP_INIT_MAX_RTOX \ + 59U /*!< Maximum RTOX value Digital 1.0 14.8.4.1 */ + +#define NFCIP_TARG_MIN_RTOX \ + 1U /*!< Minimum target RTOX value Digital 1.0 14.8.4.1 */ +#define NFCIP_TARG_MAX_RTOX \ + 59U /*!< Maximum target RTOX value Digital 1.0 14.8.4.1 */ + +#define NFCIP_TRECOV \ + 1280U /*!< Digital 1.0 A.10 Trecov */ + +#define NFCIP_TIMEOUT_ADJUSTMENT \ + 3072U /*!< Timeout Adjustment to compensate timing from end of Tx to end of \ + frame */ +#define NFCIP_RWT_ACTIVATION \ + (0x1000001U + \ + NFCIP_TIMEOUT_ADJUSTMENT) /*!< Digital 2.2 B.11 RWT ACTIVATION 2^24 + \ + RWT Delta + Adjustment*/ +#define NFCIP_RWT_ACM_ACTIVATION \ + (0x200001U + \ + NFCIP_TIMEOUT_ADJUSTMENT) /*!< Digital 2.2 B.11 RWT ACTIVATION 2^21 + \ + RWT Delta + Adjustment*/ + +#define RFAL_NFCDEP_HEADER_PAD \ + (RFAL_NFCDEP_DEPREQ_HEADER_LEN - \ + RFAL_NFCDEP_LEN_MIN) /*!< Difference between expected rcvd header len and \ + max foreseen */ + +#ifndef RFAL_NFCDEP_MAX_TX_RETRYS +#define RFAL_NFCDEP_MAX_TX_RETRYS \ + (uint8_t)3U /*!< Number of retransmit retyrs */ +#endif /* RFAL_NFCDEP_MAX_TX_RETRYS */ + +#ifndef RFAL_NFCDEP_TO_RETRYS +#define RFAL_NFCDEP_TO_RETRYS \ + (uint8_t)3U /*!< Number of retrys for Timeout */ +#endif /* RFAL_NFCDEP_TO_RETRYS */ + +#ifndef RFAL_NFCDEP_MAX_RTOX_RETRYS +#define RFAL_NFCDEP_MAX_RTOX_RETRYS \ + (uint8_t)10U /*!< Number of retrys for RTOX Digital 2.0 17.12.4.3 */ +#endif /* RFAL_NFCDEP_MAX_RTOX_RETRYS */ + +#ifndef RFAL_NFCDEP_MAX_NACK_RETRYS +#define RFAL_NFCDEP_MAX_NACK_RETRYS \ + (uint8_t)3U /*!< Number of retrys for NACK */ +#endif /* RFAL_NFCDEP_MAX_NACK_RETRYS */ + +#ifndef RFAL_NFCDEP_MAX_ATN_RETRYS +#define RFAL_NFCDEP_MAX_ATN_RETRYS \ + (uint8_t)3U /*!< Number of retrys for ATN */ +#endif /* RFAL_NFCDEP_MAX_ATN_RETRYS */ + +#define NFCIP_MIN_TXERROR_LEN \ + 4U /*!< Minimum frame length with error to be ignored Digital 1.0 14.12.5.4 \ + */ + +#define NFCIP_REQ \ + (uint8_t)0xD4U /*!= NFCIP_ST_INIT_IDLE) && \ + ((st) <= NFCIP_ST_INIT_RLS)) /*!< Checks if module is set as Initiator */ +#define nfcipIsTarget(st) \ + (!nfcipIsInitiator(st)) /*!< Checks if module is set as Target */ + +#define nfcipIsBRAllowed(br, mBR) \ + (((1U << (br)) & (mBR)) != \ + 0U) /*!< Checks bit rate is allowed by given mask */ + +#define nfcipIsEmptyDEPEnabled(op) \ + (!nfcipIsEmptyDEPDisabled(op)) /*!< Checks if empty payload is allowed by \ + operation config NCI 1.0 Table 81 */ +#define nfcipIsEmptyDEPDisabled(op) \ + (((op) & RFAL_NFCDEP_OPER_EMPTY_DEP_DIS) != \ + 0U) /*!< Checks if empty payload is not allowed by operation config NCI 1.0 \ + Table 81 */ + +#define nfcipIsRTOXReqEnabled(op) \ + (!nfcipIsRTOXReqDisabled(op)) /*!< Checks if send a RTOX_REQ is allowed by \ + operation config NCI 1.0 Table 81 */ +#define nfcipIsRTOXReqDisabled(op) \ + (((op) & RFAL_NFCDEP_OPER_RTOX_REQ_DIS) != \ + 0U) /*!< Checks if send a RTOX_REQ is not allowed by operation config \ + NCI 1.0 Table 81 */ + +/*! Checks if isDeactivating callback is set and calls it, otherwise returns + * false */ +#define nfcipIsDeactivationPending() \ + ((gNfcip.isDeactivating == NULL) ? false : gNfcip.isDeactivating()) + +/*! Returns the RWT Activation according to the current communication mode */ +#define nfcipRWTActivation() \ + ((gNfcip.cfg.commMode == RFAL_NFCDEP_COMM_ACTIVE) ? NFCIP_RWT_ACM_ACTIVATION \ + : NFCIP_RWT_ACTIVATION) + +#define nfcipRTOXAdjust(v) \ + ((v) - ((v) >> 3)) /*!< Adjust RTOX timer value to a percentage of the \ + total, current 88% */ + +/*******************************************************************************/ + +// timerPollTimeoutValue is necessary after timerCalculateTimeout so that system +// will wake up upon timer timeout. +#define nfcipTimerStart(timer, time_ms) \ + do { \ + platformTimerDestroy(timer); \ + (timer) = platformTimerCreate((uint16_t)(time_ms)); \ + } while (0) /*!< Configures and starts the RTOX timer */ +#define nfcipTimerisExpired(timer) \ + platformTimerIsExpired(timer) /*!< Checks RTOX timer has expired */ +#define nfcipTimerDestroy(timer) \ + platformTimerDestroy(timer) /*!< Destroys RTOX timer */ + +#define nfcipLogE(...) /*!< Macro for the error log method */ +#define nfcipLogW(...) /*!< Macro for the warning log method */ +#define nfcipLogI(...) /*!< Macro for the info log method */ +#define nfcipLogD(...) /*!< Macro for the debug log method */ + +/*! Digital 1.1 - 16.12.5.2 The Target SHALL NOT attempt any error recovery and + * remains in Rx mode upon Transmission or a Protocol Error */ +#define nfcDepReEnableRx(rxB, rxBL, rxL) \ + rfalTransceiveBlockingTx( \ + NULL, 0, (rxB), (rxBL), (rxL), \ + (RFAL_TXRX_FLAGS_DEFAULT | (uint32_t)RFAL_TXRX_FLAGS_NFCIP1_ON), \ + RFAL_FWT_NONE) + +/* + ****************************************************************************** + * LOCAL DATA TYPES + ****************************************************************************** + */ + +/*! Struct that holds all DEP parameters/configs for the following + * communications */ +typedef struct { + uint8_t did; /*!< Device ID (DID) to be used */ + + uint8_t *txBuf; /*!< Pointer to the Tx buffer to be sent */ + uint16_t txBufLen; /*!< Length of the data in the txBuf */ + uint8_t txBufPaylPos; /*!< Position inside txBuf where data starts */ + bool txChaining; /*!< Flag indicating chaining on transmission */ + + uint8_t *rxBuf; /*!< Pointer to the Rx buffer for incoming data */ + uint16_t rxBufLen; /*!< Length of the data in the rxBuf */ + uint8_t rxBufPaylPos; /*!< Position inside rxBuf where data is to be placed*/ + + uint32_t fwt; /*!< Frame Waiting Time (FWT) to be used */ + uint32_t dFwt; /*!< Delta Frame Waiting Time (dFWT) to be used */ + uint16_t fsc; /*!< Frame Size (FSC) to be used */ + +} rfalNfcDepDEPParams; + +/*! NFCIP module states */ +typedef enum { + NFCIP_ST_IDLE, + NFCIP_ST_INIT_IDLE, + NFCIP_ST_INIT_ATR, + NFCIP_ST_INIT_PSL, + NFCIP_ST_INIT_DEP_IDLE, + NFCIP_ST_INIT_DEP_TX, + NFCIP_ST_INIT_DEP_RX, + NFCIP_ST_INIT_DEP_ATN, + NFCIP_ST_INIT_DSL, + NFCIP_ST_INIT_RLS, + + NFCIP_ST_TARG_WAIT_ATR, + NFCIP_ST_TARG_WAIT_ACTV, + NFCIP_ST_TARG_DEP_IDLE, + NFCIP_ST_TARG_DEP_RX, + NFCIP_ST_TARG_DEP_RTOX, + NFCIP_ST_TARG_DEP_TX, + NFCIP_ST_TARG_DEP_SLEEP +} rfalNfcDepState; + +/*! NFCIP commands (Request, Response) */ +typedef enum { + NFCIP_CMD_ATR_REQ = 0x00, + NFCIP_CMD_ATR_RES = 0x01, + NFCIP_CMD_WUP_REQ = 0x02, + NFCIP_CMD_WUP_RES = 0x03, + NFCIP_CMD_PSL_REQ = 0x04, + NFCIP_CMD_PSL_RES = 0x05, + NFCIP_CMD_DEP_REQ = 0x06, + NFCIP_CMD_DEP_RES = 0x07, + NFCIP_CMD_DSL_REQ = 0x08, + NFCIP_CMD_DSL_RES = 0x09, + NFCIP_CMD_RLS_REQ = 0x0A, + NFCIP_CMD_RLS_RES = 0x0B +} rfalNfcDepCmd; + +/*! Struct that holds all NFCIP data */ +typedef struct { + rfalNfcDepConfigs cfg; /*!< Holds the current configuration to be used */ + + rfalNfcDepState state; /*!< Current state of the NFCIP module */ + uint8_t pni; /*!< Packet Number Information (PNI) counter */ + + uint8_t lastCmd; /*!< Last command sent */ + uint8_t lastPFB; /*!< Last PFB sent */ + uint8_t lastPFBnATN; /*!< Last PFB sent (excluding ATN) */ + uint8_t lastRTOX; /*!< Last RTOX value sent */ + + uint8_t cntTxRetrys; /*!< Retransmissions counter */ + uint8_t cntTORetrys; /*!< Timeouts counter */ + uint8_t cntRTOXRetrys; /*!< RTOX counter */ + uint8_t cntNACKRetrys; /*!< NACK counter */ + uint8_t cntATNRetrys; /*!< Attention (ATN) counter */ + + uint16_t fsc; /*!< Current Frame Size (FSC) to be used */ + bool isTxChaining; /*!< Flag for chaining on Transmission */ + bool isRxChaining; /*!< Flag for chaining on Reception */ + uint8_t *txBuf; /*!< Pointer to the Tx buffer to be sent */ + uint8_t *rxBuf; /*!< Pointer to the Rx buffer for incoming data */ + uint16_t txBufLen; /*!< Length of the data in the txBuf */ + uint16_t rxBufLen; /*!< Length of rxBuf buffer */ + uint16_t *rxRcvdLen; /*!< Length of the data in the rxBuf */ + uint8_t txBufPaylPos; /*!< Position in txBuf where data starts */ + uint8_t rxBufPaylPos; /*!< Position in rxBuf where data is to be placed */ + bool *isChaining; /*!< Flag for chaining on Reception */ + + rfalNfcDepDevice *nfcDepDev; /*!< Pointer to NFC-DEP device info */ + + uint32_t RTOXTimer; /*!< Timer used for RTOX */ + rfalNfcDepDeactCallback + isDeactivating; /*!< Deactivating flag check callback */ + + bool isReqPending; /*!< Flag pending REQ from Target activation */ + bool isTxPending; /*!< Flag pending DEP Block while waiting RTOX Ack */ + bool isWait4RTOX; /*!< Flag for waiting RTOX Ack */ + + rfalNfcDepPduTxRxParam PDUParam; /*!< PDU TxRx params */ + uint16_t PDUTxPos; /*!< PDU Tx position */ + uint16_t PDURxPos; /*!< PDU Rx position */ + bool isPDURxChaining; /*!< PDU Transceive chaining flag */ +} rfalNfcDep; + +/* + ****************************************************************************** + * LOCAL VARIABLES + ****************************************************************************** + */ + +static rfalNfcDep gNfcip; /*!< NFCIP module instance */ + +/* + ****************************************************************************** + * LOCAL FUNCTION PROTOTYPES + ****************************************************************************** + */ + +static ReturnCode nfcipTxRx(rfalNfcDepCmd cmd, uint8_t *txBuf, uint32_t fwt, + uint8_t *paylBuf, uint8_t paylBufLen, + uint8_t *rxBuf, uint16_t rxBufLen, + uint16_t *rxActLen); +static ReturnCode nfcipTx(rfalNfcDepCmd cmd, uint8_t *txBuf, uint8_t *paylBuf, + uint16_t paylLen, uint8_t pfbData, uint32_t fwt); +static ReturnCode nfcipDEPControlMsg(uint8_t pfb, uint8_t RTOX); +static ReturnCode nfcipInitiatorHandleDEP(ReturnCode rxRes, uint16_t rxLen, + uint16_t *outActRxLen, + bool *outIsChaining); +static ReturnCode nfcipTargetHandleRX(ReturnCode rxRes, uint16_t *outActRxLen, + bool *outIsChaining); +static ReturnCode nfcipTargetHandleActivation(rfalNfcDepDevice *nfcDepDev, + uint8_t *outBRS); + +/*! + ****************************************************************************** + * \brief NFCIP Configure + * + * Configures the nfcip layer with the given configurations + * + * \param[in] cfg : nfcip configuration for following communication + ****************************************************************************** + */ +static void nfcipConfig(const rfalNfcDepConfigs *cfg); + +/*! + ****************************************************************************** + * \brief Set DEP parameters + * + * This method sets the parameters/configs for following Data Exchange + * Sets the nfcip module state according to the role it is configured + * + * + * \warning To be used only after proper Initiator/Target activation: + * nfcipTargetHandleActivation() or nfcipInitiatorActivate() has + * returned success + * + * This must be called before nfcipRun() in case of Target to pass + * rxBuffer + * + * Everytime some data needs to be transmitted call this to set it and + * call nfcipRun() until done or error + * + * \param[in] DEPParams : the parameters to be used during Data Exchange + ****************************************************************************** + */ +static void nfcipSetDEPParams(const rfalNfcDepDEPParams *DEPParams); + +/*! + ****************************************************************************** + * \brief NFCIP run protocol + * + * This method handles all the nfcip protocol during Data Exchange (DEP + * requests and responses). + * + * A data exchange cycle is considered a DEP REQ and a DEP RES. + * + * In case of Tx chaining(MI) must signal it with nfcipSetDEPParams() + * In case of Rx chaining(MI) outIsChaining will be set to true and the + * current data returned + * + * \param[out] outActRxLen : data received length + * \param[out] outIsChaining : true if other peer is performing chaining(MI) + * + * \return RFAL_ERR_NONE : Data exchange cycle completed successfully + * \return RFAL_ERR_TIMEOUT : Timeout occurred + * \return RFAL_ERR_PROTO : Protocol error occurred + * \return RFAL_ERR_AGAIN : Other peer is doing chaining(MI), current block + * was received successfully call again until complete + * + ****************************************************************************** + */ +static ReturnCode nfcipRun(uint16_t *outActRxLen, bool *outIsChaining); + +/*! + ****************************************************************************** + * \brief Transmission method + * + * This method checks if the current communication is Active or Passive + * and performs the necessary procedures for each communication type + * + * Transmits the data hold in txBuf + * + * \param[in] txBuf : buffer to transmit + * \param[in] txBufLen : txBuffer capacity + * \param[in] fwt : fwt for current Tx + * + * \return RFAL_ERR_NONE : No error + ****************************************************************************** + */ +static ReturnCode nfcipDataTx(uint8_t *txBuf, uint16_t txBufLen, uint32_t fwt); + +/*! + ****************************************************************************** + * \brief Reception method + * + * This method checks if the current communication is Active or Passive + * and calls the appropriate reception method + * + * Copies incoming data to rxBuf + * + * \param[in] blocking : reception is to be done blocking or non-blocking + * + * \return RFAL_ERR_BUSY : Busy + * \return RFAL_ERR_NONE : No error + ****************************************************************************** + */ +static ReturnCode nfcipDataRx(bool blocking); + +/* + ****************************************************************************** + * LOCAL FUNCTIONS + ****************************************************************************** + */ + +/*******************************************************************************/ + +/*******************************************************************************/ +static bool nfcipDxIsSupported(uint8_t Dx, uint8_t BRx, uint8_t BSx) { + uint8_t Bx; + + /* Take the min of the possible bit rates, we'll use one for both directions + */ + Bx = RFAL_MIN(BRx, BSx); + + /* Lower bit rates must be supported for P2P */ + if ((Dx <= (uint8_t)RFAL_NFCDEP_Dx_04_424)) { + return true; + } + + if ((Dx == (uint8_t)RFAL_NFCDEP_Dx_08_848) && + (Bx >= (uint8_t)RFAL_NFCDEP_Bx_08_848)) { + return true; + } + + return false; +} + +/*******************************************************************************/ +static ReturnCode nfcipTxRx(rfalNfcDepCmd cmd, uint8_t *txBuf, uint32_t fwt, + uint8_t *paylBuf, uint8_t paylBufLen, + uint8_t *rxBuf, uint16_t rxBufLen, + uint16_t *rxActLen) { + ReturnCode ret; + + if ((cmd == NFCIP_CMD_DEP_REQ) || + (cmd == NFCIP_CMD_DEP_RES)) /* this method cannot be used for DEPs */ + { + return RFAL_ERR_PARAM; + } + + /* Assign the global params for this TxRx */ + gNfcip.rxBuf = rxBuf; + gNfcip.rxBufLen = rxBufLen; + gNfcip.rxRcvdLen = rxActLen; + + /*******************************************************************************/ + /* Transmission */ + /*******************************************************************************/ + if (txBuf != NULL) /* if nothing to Tx, just do Rx */ + { + RFAL_EXIT_ON_ERR(ret, nfcipTx(cmd, txBuf, paylBuf, paylBufLen, 0, fwt)); + } + + /*******************************************************************************/ + /* Reception */ + /*******************************************************************************/ + ret = nfcipDataRx(true); + if (ret != RFAL_ERR_NONE) { + return ret; + } + + /*******************************************************************************/ + *rxActLen = *rxBuf; /* Use LEN byte instead due to with/without CRC modes */ + return RFAL_ERR_NONE; /* Tx and Rx completed successfully */ +} + +/*******************************************************************************/ +static ReturnCode nfcipDEPControlMsg(uint8_t pfb, uint8_t RTOX) { + uint8_t ctrlMsg[20]; + uint32_t fwt; + + /*******************************************************************************/ + /* Calculate Cmd and fwt to be used */ + /*******************************************************************************/ + const rfalNfcDepCmd depCmd = + ((gNfcip.cfg.role == RFAL_NFCDEP_ROLE_TARGET) ? NFCIP_CMD_DEP_RES + : NFCIP_CMD_DEP_REQ); + fwt = + ((gNfcip.cfg.role == RFAL_NFCDEP_ROLE_TARGET) + ? NFCIP_NO_FWT + : (nfcip_PFBisSTO(pfb) ? ((RTOX * gNfcip.cfg.fwt) + gNfcip.cfg.dFwt) + : (gNfcip.cfg.fwt + gNfcip.cfg.dFwt))); + + if (nfcip_PFBisSTO(pfb)) { + ctrlMsg[RFAL_NFCDEP_DEPREQ_HEADER_LEN] = RTOX; + return nfcipTx(depCmd, ctrlMsg, &ctrlMsg[RFAL_NFCDEP_DEPREQ_HEADER_LEN], + sizeof(uint8_t), pfb, fwt); + } else { + return nfcipTx(depCmd, ctrlMsg, NULL, 0, pfb, fwt); + } +} + +/*******************************************************************************/ +static void nfcipClearCounters(void) { + gNfcip.cntATNRetrys = 0; + gNfcip.cntNACKRetrys = 0; + gNfcip.cntTORetrys = 0; + gNfcip.cntTxRetrys = 0; + gNfcip.cntRTOXRetrys = 0; +} + +/*******************************************************************************/ +static ReturnCode nfcipInitiatorHandleDEP(ReturnCode rxRes, uint16_t rxLen, + uint16_t *outActRxLen, + bool *outIsChaining) { + ReturnCode ret; + uint8_t nfcDepLen; + uint8_t rxMsgIt; + uint8_t rxPFB; + uint8_t rxRTOX; + uint8_t optHdrLen; + + ret = RFAL_ERR_INTERNAL; + rxMsgIt = 0; + optHdrLen = 0; + + *outActRxLen = 0; + *outIsChaining = false; + + /*******************************************************************************/ + /* Handle reception errors */ + /*******************************************************************************/ + switch (rxRes) { + /*******************************************************************************/ + /* Timeout -> Digital 1.0 14.15.5.6 */ + case RFAL_ERR_TIMEOUT: + + nfcipLogI(" NFCIP(I) TIMEOUT TORetrys:%d \r\n", gNfcip.cntTORetrys); + + /* Digital 1.0 14.15.5.6 - If nTO >= Max raise protocol error */ + if (gNfcip.cntTORetrys++ >= RFAL_NFCDEP_TO_RETRYS) { + return RFAL_ERR_PROTO; + } + + /*******************************************************************************/ + /* Upon Timeout error, if Deactivation is pending, no more error recovery + * will be done #54. + * This is used to address the issue some devices that havea big TO. + * Normally LLCP layer has timeout already, and NFCIP layer is still + * running error handling, retrying ATN/NACKs */ + /*******************************************************************************/ + if (nfcipIsDeactivationPending()) { + nfcipLogI(" skipping error recovery due deactivation pending \r\n"); + return RFAL_ERR_TIMEOUT; + } + + /* Digital 1.0 14.15.5.6 1) If last PDU was NACK */ + if (nfcip_PFBisRNACK(gNfcip.lastPFB)) { + /* Digital 1.0 14.15.5.6 2) if NACKs failed raise protocol error */ + if (gNfcip.cntNACKRetrys++ >= RFAL_NFCDEP_MAX_NACK_RETRYS) { + return RFAL_ERR_PROTO; + } + + /* Send NACK */ + nfcipLogI(" NFCIP(I) Sending NACK retry: %d \r\n", + gNfcip.cntNACKRetrys); + RFAL_EXIT_ON_ERR(ret, + nfcipDEPControlMsg(nfcip_PFBRPDU_NACK(gNfcip.pni), 0)); + return RFAL_ERR_BUSY; + } + + nfcipLogI(" NFCIP(I) Checking if to send ATN ATNRetrys: %d \r\n", + gNfcip.cntATNRetrys); + + /* Digital 1.0 14.15.5.6 3) Otherwise send ATN */ + if (gNfcip.cntATNRetrys++ >= RFAL_NFCDEP_MAX_ATN_RETRYS) { + return RFAL_ERR_PROTO; + } + + /* Send ATN */ + nfcipLogI(" NFCIP(I) Sending ATN \r\n"); + RFAL_EXIT_ON_ERR(ret, nfcipDEPControlMsg(nfcip_PFBSPDU_ATN(), 0)); + return RFAL_ERR_BUSY; + + /*******************************************************************************/ + /* Data rcvd with error -> Digital 1.0 14.12.5.4 */ + case RFAL_ERR_CRC: + case RFAL_ERR_PAR: + case RFAL_ERR_FRAMING: + case RFAL_ERR_RF_COLLISION: + + nfcipLogI(" NFCIP(I) rx Error: %d \r\n", rxRes); + + /* Digital 1.0 14.12.5.4 Tx Error with data, ignore */ + if (rxLen < NFCIP_MIN_TXERROR_LEN) { + nfcipLogI(" NFCIP(I) Transmission error w data \r\n"); +#if 0 + if(gNfcip.cfg.commMode == RFAL_NFCDEP_COMM_PASSIVE) + { + nfcipLogI( " NFCIP(I) Transmission error w data -> reEnabling Rx \r\n" ); + nfcipReEnableRxTout( NFCIP_TRECOV ); + return RFAL_ERR_BUSY; + } +#endif /* 0 */ + } + + /* Digital 1.1 16.12.5.4 if NACKs failed raise Transmission error */ + if (gNfcip.cntNACKRetrys++ >= RFAL_NFCDEP_MAX_NACK_RETRYS) { + return RFAL_ERR_FRAMING; + } + + /* Send NACK */ + nfcipLogI(" NFCIP(I) Sending NACK \r\n"); + RFAL_EXIT_ON_ERR(ret, + nfcipDEPControlMsg(nfcip_PFBRPDU_NACK(gNfcip.pni), 0)); + return RFAL_ERR_BUSY; + + case RFAL_ERR_NONE: + break; + + case RFAL_ERR_BUSY: + return RFAL_ERR_BUSY; /* Debug purposes */ + + default: + nfcipLogW(" NFCIP(I) Error: %d \r\n", rxRes); + return rxRes; + } + + /*******************************************************************************/ + /* Rx OK check if valid DEP PDU */ + /*******************************************************************************/ + + if (gNfcip.rxBuf == NULL) { + return RFAL_ERR_IO; + } + + /* Due to different modes on ST25R391x (with/without CRC) use NFC-DEP LEN + * instead of bytes retrieved */ + nfcDepLen = gNfcip.rxBuf[rxMsgIt++]; + + nfcipLogD(" NFCIP(I) rx OK: %d bytes \r\n", nfcDepLen); + + /* Digital 1.0 14.15.5.5 Protocol Error */ + if (gNfcip.rxBuf[rxMsgIt++] != NFCIP_RES) { + nfcipLogW(" NFCIP(I) error %02X instead of %02X \r\n", + gNfcip.rxBuf[(rxMsgIt - 1U)], NFCIP_RES); + return RFAL_ERR_PROTO; + } + + /* Digital 1.0 14.15.5.5 Protocol Error */ + if (gNfcip.rxBuf[rxMsgIt++] != (uint8_t)NFCIP_CMD_DEP_RES) { + nfcipLogW(" NFCIP(I) error %02X instead of %02X \r\n", + gNfcip.rxBuf[(rxMsgIt - 1U)], NFCIP_CMD_DEP_RES); + return RFAL_ERR_PROTO; + } + + rxPFB = gNfcip.rxBuf[rxMsgIt++]; + + /*******************************************************************************/ + /* Check for valid PFB type */ + if (!(nfcip_PFBisSPDU(rxPFB) || nfcip_PFBisRPDU(rxPFB) || + nfcip_PFBisIPDU(rxPFB))) { + return RFAL_ERR_PROTO; + } + + /*******************************************************************************/ + /* Digital 1.0 14.8.2.1 check if DID is expected and match -> Protocol Error + */ + if (gNfcip.cfg.did != RFAL_NFCDEP_DID_NO) { + if ((gNfcip.rxBuf[rxMsgIt++] != gNfcip.cfg.did) || + (!nfcip_PFBhasDID(rxPFB))) { + return RFAL_ERR_PROTO; + } + optHdrLen++; /* Inc header optional field cnt*/ + } else if (nfcip_PFBhasDID(rxPFB)) /* DID not expected but rcv */ + { + return RFAL_ERR_PROTO; + } else { + /* MISRA 15.7 - Empty else */ + } + + /*******************************************************************************/ + /* Digital 1.0 14.6.2.8 & 14.6.3.11 NAD must not be used */ + if (gNfcip.cfg.nad != RFAL_NFCDEP_NAD_NO) { + if ((gNfcip.rxBuf[rxMsgIt++] != gNfcip.cfg.nad) || + (!nfcip_PFBhasNAD(rxPFB))) { + return RFAL_ERR_PROTO; + } + optHdrLen++; /* Inc header optional field cnt*/ + } else if (nfcip_PFBhasNAD(rxPFB)) /* NAD not expected but rcv */ + { + return RFAL_ERR_PROTO; + } else { + /* MISRA 15.7 - Empty else */ + } + + /*******************************************************************************/ + /* Process R-PDU */ + /*******************************************************************************/ + if (nfcip_PFBisRPDU(rxPFB)) { + /*******************************************************************************/ + /* R ACK */ + /*******************************************************************************/ + if (nfcip_PFBisRACK(rxPFB)) { + nfcipLogI(" NFCIP(I) Rcvd ACK \r\n"); + if (gNfcip.pni == nfcip_PBF_PNI(rxPFB)) { + /* 14.12.3.3 R-ACK with correct PNI -> Increment */ + gNfcip.pni = nfcip_PNIInc(gNfcip.pni); + + /* R-ACK while not performing chaining -> Protocol error*/ + if (!gNfcip.isTxChaining) { + return RFAL_ERR_PROTO; + } + + nfcipClearCounters(); + gNfcip.state = NFCIP_ST_INIT_DEP_IDLE; + return RFAL_ERR_NONE; /* This block has been transmitted */ + } else /* Digital 1.0 14.12.4.5 ACK with wrong PNI Initiator may + retransmit */ + { + if (gNfcip.cntTxRetrys++ >= RFAL_NFCDEP_MAX_TX_RETRYS) { + return RFAL_ERR_PROTO; + } + + /* Extended the MAY in Digital 1.0 14.12.4.5 to only reTransmit if the + * ACK is for the previous DEP, otherwise raise Protocol immediately If + * the PNI difference is more than 1 it is worthless to reTransmit 3x + * and after raise the error */ + + if (nfcip_PNIDec(gNfcip.pni) == nfcip_PBF_PNI(rxPFB)) { + /* ReTransmit */ + nfcipLogI(" NFCIP(I) Rcvd ACK prev PNI -> reTx \r\n"); + gNfcip.state = NFCIP_ST_INIT_DEP_TX; + return RFAL_ERR_BUSY; + } + + nfcipLogI(" NFCIP(I) Rcvd ACK unexpected far PNI -> Error \r\n"); + return RFAL_ERR_PROTO; + } + } else /* Digital 1.0 - 14.12.5.2 Target must never send NACK */ + { + return RFAL_ERR_PROTO; + } + } + + /*******************************************************************************/ + /* Process S-PDU */ + /*******************************************************************************/ + if (nfcip_PFBisSPDU(rxPFB)) { + nfcipLogI(" NFCIP(I) Rcvd S-PDU \r\n"); + /*******************************************************************************/ + /* S ATN */ + /*******************************************************************************/ + if (nfcip_PFBisSATN(rxPFB)) /* If is a S-ATN */ + { + nfcipLogI(" NFCIP(I) Rcvd ATN \r\n"); + if (nfcip_PFBisSATN(gNfcip.lastPFB)) /* Check if is expected */ + { + gNfcip.cntATNRetrys = 0; /* Clear ATN counter */ + + /* Although spec is not clear NFC Forum Digital test is expecting to + * retransmit upon receiving ATN_RES */ + if (nfcip_PFBisSTO(gNfcip.lastPFBnATN)) { + nfcipLogI(" NFCIP(I) Rcvd ATN -> reTx RTOX_RES \r\n"); + RFAL_EXIT_ON_ERR( + ret, nfcipDEPControlMsg(nfcip_PFBSPDU_TO(), gNfcip.lastRTOX)); + } else { + /* ReTransmit ? */ + if (gNfcip.cntTxRetrys++ >= RFAL_NFCDEP_MAX_TX_RETRYS) { + return RFAL_ERR_PROTO; + } + + nfcipLogI(" NFCIP(I) Rcvd ATN -> reTx PNI: %d \r\n", gNfcip.pni); + gNfcip.state = NFCIP_ST_INIT_DEP_TX; + } + + return RFAL_ERR_BUSY; + } else /* Digital 1.0 14.12.4.4 & 14.12.4.8 */ + { + return RFAL_ERR_PROTO; + } + } + /*******************************************************************************/ + /* S TO */ + /*******************************************************************************/ + else if (nfcip_PFBisSTO(rxPFB)) /* If is a S-TO (RTOX) */ + { + nfcipLogI(" NFCIP(I) Rcvd TO \r\n"); + + rxRTOX = gNfcip.rxBuf[rxMsgIt++]; + + /* Digital 1.1 16.12.4.3 - Initiator MAY stop accepting subsequent RTOX + * Req * + * - RTOX request to an ATN -> Protocol error */ + if ((gNfcip.cntRTOXRetrys++ > RFAL_NFCDEP_MAX_RTOX_RETRYS) || + nfcip_PFBisSATN(gNfcip.lastPFB)) { + return RFAL_ERR_PROTO; + } + + /* Digital 1.1 16.8.4.1 RTOX must be between [1,59] */ + if ((rxRTOX < NFCIP_INIT_MIN_RTOX) || (rxRTOX > NFCIP_INIT_MAX_RTOX)) { + return RFAL_ERR_PROTO; + } + + RFAL_EXIT_ON_ERR(ret, nfcipDEPControlMsg(nfcip_PFBSPDU_TO(), rxRTOX)); + gNfcip.lastRTOX = rxRTOX; + + return RFAL_ERR_BUSY; + } else { + /* Unexpected S-PDU */ + return RFAL_ERR_PROTO; + } + } + + /*******************************************************************************/ + /* Process I-PDU */ + /*******************************************************************************/ + if (nfcip_PFBisIPDU(rxPFB)) { + if (gNfcip.pni != nfcip_PBF_PNI(rxPFB)) { + nfcipLogI(" NFCIP(I) Rcvd IPDU wrong PNI curPNI: %d rxPNI: %d \r\n", + gNfcip.pni, nfcip_PBF_PNI(rxPFB)); + return RFAL_ERR_PROTO; + } + + nfcipLogD(" NFCIP(I) Rcvd IPDU OK PNI: %d \r\n", gNfcip.pni); + + /* 14.12.3.3 I-PDU with correct PNI -> Increment */ + gNfcip.pni = nfcip_PNIInc(gNfcip.pni); + + /* Successful data Exchange */ + nfcipClearCounters(); + *outActRxLen = + ((uint16_t)nfcDepLen - RFAL_NFCDEP_DEP_HEADER - (uint16_t)optHdrLen); + + if ((&gNfcip.rxBuf[gNfcip.rxBufPaylPos] != + &gNfcip.rxBuf[RFAL_NFCDEP_DEP_HEADER + optHdrLen]) && + (*outActRxLen > 0U)) { + RFAL_MEMMOVE(&gNfcip.rxBuf[gNfcip.rxBufPaylPos], + &gNfcip.rxBuf[RFAL_NFCDEP_DEP_HEADER + optHdrLen], + *outActRxLen); + } + + /*******************************************************************************/ + /* Check if target is indicating chaining MI */ + /*******************************************************************************/ + if (nfcip_PFBisIMI(rxPFB)) { + gNfcip.isRxChaining = true; + *outIsChaining = true; + + nfcipLogD(" NFCIP(I) Rcvd IPDU OK w MI -> ACK \r\n"); + RFAL_EXIT_ON_ERR(ret, nfcipDEPControlMsg(nfcip_PFBRPDU_ACK(gNfcip.pni), + gNfcip.rxBuf[rxMsgIt++])); + + return RFAL_ERR_AGAIN; /* Send Again signalling to run again, but some + chaining data has arrived*/ + } else { + gNfcip.isRxChaining = false; + gNfcip.state = NFCIP_ST_INIT_DEP_IDLE; + + ret = RFAL_ERR_NONE; /* Data exchange done */ + } + } + return ret; +} + +/*******************************************************************************/ +static ReturnCode nfcipTargetHandleRX(ReturnCode rxRes, uint16_t *outActRxLen, + bool *outIsChaining) { + ReturnCode ret; + uint8_t nfcDepLen; + uint8_t rxMsgIt; + uint8_t rxPFB; + uint8_t optHdrLen; + uint8_t resBuf[RFAL_NFCDEP_HEADER_PAD + NFCIP_TARGET_RES_MAX]; + + ret = RFAL_ERR_INTERNAL; + rxMsgIt = 0; + optHdrLen = 0; + + *outActRxLen = 0; + *outIsChaining = false; + + /*******************************************************************************/ + /* Handle reception errors */ + /*******************************************************************************/ + switch (rxRes) { + /*******************************************************************************/ + case RFAL_ERR_NONE: + break; + + case RFAL_ERR_LINK_LOSS: + nfcipLogW(" NFCIP(T) Error: %d \r\n", rxRes); + return rxRes; + + case RFAL_ERR_BUSY: + return RFAL_ERR_BUSY; /* Debug purposes */ + + case RFAL_ERR_TIMEOUT: + case RFAL_ERR_CRC: + case RFAL_ERR_PAR: + case RFAL_ERR_FRAMING: + case RFAL_ERR_PROTO: + default: + /* Digital 1.1 16.12.5.2 The Target MUST NOT attempt any error recovery. + * * The Target MUST always stay in receive mode when a * Transmission + * Error or a Protocol Error occurs. * + * * + * Do not push Transmission/Protocol Errors to upper layer in Listen Mode + * #766 */ + + nfcDepReEnableRx(gNfcip.rxBuf, gNfcip.rxBufLen, gNfcip.rxRcvdLen); + return RFAL_ERR_BUSY; + } + + /*******************************************************************************/ + /* Rx OK check if valid DEP PDU */ + /*******************************************************************************/ + + if (gNfcip.rxBuf == NULL) { + return RFAL_ERR_IO; + } + + /* Due to different modes on ST25R391x (with/without CRC) use NFC-DEP LEN + * instead of bytes retrieved */ + nfcDepLen = gNfcip.rxBuf[rxMsgIt++]; + + nfcipLogD(" NFCIP(T) rx OK: %d bytes \r\n", nfcDepLen); + + if (gNfcip.rxBuf[rxMsgIt++] != NFCIP_REQ) { + nfcDepReEnableRx(gNfcip.rxBuf, gNfcip.rxBufLen, gNfcip.rxRcvdLen); + return RFAL_ERR_BUSY; /* RFAL_ERR_PROTO - Ignore bad request */ + } + + /*******************************************************************************/ + /* Check whether target rcvd a normal DEP or deactivation request */ + /*******************************************************************************/ + switch (gNfcip.rxBuf[rxMsgIt++]) { + /*******************************************************************************/ + case (uint8_t)NFCIP_CMD_DEP_REQ: + break; /* Continue to normal DEP processing */ + + /*******************************************************************************/ + case (uint8_t)NFCIP_CMD_DSL_REQ: + + nfcipLogI(" NFCIP(T) rx DSL \r\n"); + + /* Digital 1.0 14.9.1.2 If DID is used and incorrect ignore it */ + /* [Digital 1.0, 16.9.1.2]: If DID == 0, Target SHALL ignore DSL_REQ with + * DID */ + if ((((gNfcip.rxBuf[rxMsgIt++] != gNfcip.cfg.did) || + (nfcDepLen != RFAL_NFCDEP_DSL_RLS_LEN_DID)) && + (gNfcip.cfg.did != RFAL_NFCDEP_DID_NO)) || + ((gNfcip.cfg.did == RFAL_NFCDEP_DID_NO) && + (nfcDepLen != RFAL_NFCDEP_DSL_RLS_LEN_NO_DID))) { + nfcipLogI(" NFCIP(T) DSL wrong DID, ignoring \r\n"); + return RFAL_ERR_BUSY; + } + + nfcipTx(NFCIP_CMD_DSL_RES, resBuf, NULL, 0, 0, NFCIP_NO_FWT); + + gNfcip.state = NFCIP_ST_TARG_DEP_SLEEP; + return RFAL_ERR_SLEEP_REQ; + + /*******************************************************************************/ + case (uint8_t)NFCIP_CMD_RLS_REQ: + + nfcipLogI(" NFCIP(T) rx RLS \r\n"); + + /* Digital 1.0 14.10.1.2 If DID is used and incorrect ignore it */ + /* [Digital 1.0, 16.10.2.2]: If DID == 0, Target SHALL ignore DSL_REQ with + * DID */ + if ((((gNfcip.rxBuf[rxMsgIt++] != gNfcip.cfg.did) || + (nfcDepLen != RFAL_NFCDEP_DSL_RLS_LEN_DID)) && + (gNfcip.cfg.did != RFAL_NFCDEP_DID_NO)) || + ((gNfcip.cfg.did == RFAL_NFCDEP_DID_NO) && + (nfcDepLen > RFAL_NFCDEP_DSL_RLS_LEN_NO_DID))) { + nfcipLogI(" NFCIP(T) RLS wrong DID, ignoring \r\n"); + return RFAL_ERR_BUSY; + } + + nfcipTx(NFCIP_CMD_RLS_RES, resBuf, NULL, 0, 0, NFCIP_NO_FWT); + + gNfcip.state = NFCIP_ST_TARG_DEP_IDLE; + return RFAL_ERR_RELEASE_REQ; + + /*******************************************************************************/ + /*case NFCIP_CMD_PSL_REQ: PSL must be handled in Activation + * only */ + /*case NFCIP_CMD_WUP_REQ: WUP not in NFC Forum Digital 1.0 */ + default: + + /* Don't go to NFCIP_ST_TARG_DEP_IDLE state as it needs to ignore this * + * invalid frame, and keep waiting for more frames */ + + nfcDepReEnableRx(gNfcip.rxBuf, gNfcip.rxBufLen, gNfcip.rxRcvdLen); + return RFAL_ERR_BUSY; /* RFAL_ERR_PROTO - Ignore bad frame */ + } + + /*******************************************************************************/ + + rxPFB = gNfcip.rxBuf[rxMsgIt++]; /* Store rcvd PFB */ + + /*******************************************************************************/ + /* Check for valid PFB type */ + if (!(nfcip_PFBisSPDU(rxPFB) || nfcip_PFBisRPDU(rxPFB) || + nfcip_PFBisIPDU(rxPFB))) { + nfcDepReEnableRx(gNfcip.rxBuf, gNfcip.rxBufLen, gNfcip.rxRcvdLen); + return RFAL_ERR_BUSY; /* RFAL_ERR_PROTO - Ignore invalid PFB */ + } + + /*******************************************************************************/ + if (gNfcip.cfg.did != RFAL_NFCDEP_DID_NO) { + if (!nfcip_PFBhasDID(rxPFB)) { + nfcDepReEnableRx(gNfcip.rxBuf, gNfcip.rxBufLen, gNfcip.rxRcvdLen); + return RFAL_ERR_BUSY; /* RFAL_ERR_PROTO - Ignore bad/missing DID */ + } + if (gNfcip.rxBuf[rxMsgIt++] != gNfcip.cfg.did) /* MISRA 13.5 */ + { + nfcDepReEnableRx(gNfcip.rxBuf, gNfcip.rxBufLen, gNfcip.rxRcvdLen); + return RFAL_ERR_BUSY; /* RFAL_ERR_PROTO - Ignore bad/missing DID */ + } + optHdrLen++; /* Inc header optional field cnt*/ + } else if (nfcip_PFBhasDID(rxPFB)) /* DID not expected but rcv */ + { + nfcDepReEnableRx(gNfcip.rxBuf, gNfcip.rxBufLen, gNfcip.rxRcvdLen); + return RFAL_ERR_BUSY; /* RFAL_ERR_PROTO - Ignore unexpected DID */ + } else { + /* MISRA 15.7 - Empty else */ + } + + /*******************************************************************************/ + if (gNfcip.cfg.nad != RFAL_NFCDEP_NAD_NO) { + if ((gNfcip.rxBuf[rxMsgIt++] != gNfcip.cfg.did) || + (!nfcip_PFBhasDID(rxPFB))) { + nfcDepReEnableRx(gNfcip.rxBuf, gNfcip.rxBufLen, gNfcip.rxRcvdLen); + return RFAL_ERR_BUSY; /* RFAL_ERR_PROTO - Ignore bad/missing DID */ + } + optHdrLen++; /* Inc header optional field cnt*/ + } else if (nfcip_PFBhasNAD(rxPFB)) /* NAD not expected but rcv */ + { + nfcDepReEnableRx(gNfcip.rxBuf, gNfcip.rxBufLen, gNfcip.rxRcvdLen); + return RFAL_ERR_BUSY; /* RFAL_ERR_PROTO - Ignore unexpected NAD */ + } else { + /* MISRA 15.7 - Empty else */ + } + + /*******************************************************************************/ + /* Process R-PDU */ + /*******************************************************************************/ + if (nfcip_PFBisRPDU(rxPFB)) { + nfcipLogD(" NFCIP(T) Rcvd R-PDU \r\n"); + /*******************************************************************************/ + /* R ACK */ + /*******************************************************************************/ + if (nfcip_PFBisRACK(rxPFB)) { + nfcipLogI(" NFCIP(T) Rcvd ACK \r\n"); + if (gNfcip.pni == nfcip_PBF_PNI(rxPFB)) { + /* R-ACK while not performing chaining -> Protocol error */ + if (!gNfcip.isTxChaining) { + nfcDepReEnableRx(gNfcip.rxBuf, gNfcip.rxBufLen, gNfcip.rxRcvdLen); + return RFAL_ERR_BUSY; /* RFAL_ERR_PROTO - Ignore unexpected ACK */ + } + + /* This block has been transmitted and acknowledged, perform RTOX until + * next data is provided */ + + /* Digital 1.1 16.12.4.7 - If ACK rcvd continue with chaining or an + * RTOX */ + nfcipTimerStart( + gNfcip.RTOXTimer, + nfcipRTOXAdjust(nfcipConv1FcToMs(rfalNfcDepWT2RWT(gNfcip.cfg.to)))); + gNfcip.state = NFCIP_ST_TARG_DEP_RTOX; + + return RFAL_ERR_NONE; /* This block has been transmitted */ + } + + /* Digital 1.0 14.12.3.4 - If last send was ATN and rx PNI is minus 1 */ + else if (nfcip_PFBisSATN(gNfcip.lastPFB) && + (nfcip_PNIDec(gNfcip.pni) == nfcip_PBF_PNI(rxPFB))) { + nfcipLogI(" NFCIP(T) wrong PNI, last was ATN reTx \r\n"); + /* Spec says to leave current PNI as is, but will be Inc after Tx, + * remaining the same */ + gNfcip.pni = nfcip_PNIDec(gNfcip.pni); + + gNfcip.state = NFCIP_ST_TARG_DEP_TX; + return RFAL_ERR_BUSY; + } else { + /* MISRA 15.7 - Empty else */ + } + } + /*******************************************************************************/ + /* R NACK */ + /*******************************************************************************/ + /* ISO 18092 12.6.1.3.3 When rcv NACK if PNI = prev PNI sent -> reTx */ + else if (nfcip_PFBisRNACK(rxPFB) && + (nfcip_PNIDec(gNfcip.pni) == nfcip_PBF_PNI(rxPFB))) { + nfcipLogI(" NFCIP(T) Rcvd NACK \r\n"); + + gNfcip.pni = nfcip_PNIDec(gNfcip.pni); /* Dec so that has the prev PNI */ + + gNfcip.state = NFCIP_ST_TARG_DEP_TX; + return RFAL_ERR_BUSY; + } else { + nfcipLogI(" NFCIP(T) Unexpected R-PDU \r\n"); + + nfcDepReEnableRx(gNfcip.rxBuf, gNfcip.rxBufLen, gNfcip.rxRcvdLen); + return RFAL_ERR_BUSY; /* RFAL_ERR_PROTO - Ignore unexpected R-PDU */ + } + } + + /*******************************************************************************/ + /* Process S-PDU */ + /*******************************************************************************/ + if (nfcip_PFBisSPDU(rxPFB)) { + nfcipLogD(" NFCIP(T) Rcvd S-PDU \r\n"); + + /*******************************************************************************/ + /* S ATN */ + /*******************************************************************************/ + /* ISO 18092 12.6.3 Attention */ + if (nfcip_PFBisSATN(rxPFB)) /* If is a S-ATN */ + { + nfcipLogI(" NFCIP(T) Rcvd ATN curPNI: %d \r\n", gNfcip.pni); + RFAL_EXIT_ON_ERR(ret, nfcipDEPControlMsg(nfcip_PFBSPDU_ATN(), 0)); + return RFAL_ERR_BUSY; + } + + /*******************************************************************************/ + /* S TO */ + /*******************************************************************************/ + else if (nfcip_PFBisSTO(rxPFB)) /* If is a S-TO (RTOX) */ + { + if (nfcip_PFBisSTO(gNfcip.lastPFBnATN)) { + nfcipLogI(" NFCIP(T) Rcvd TO \r\n"); + + /* Digital 1.1 16.8.4.6 RTOX value in RES different that in REQ -> + * Protocol Error */ + if (gNfcip.lastRTOX != gNfcip.rxBuf[rxMsgIt++]) { + nfcipLogI(" NFCIP(T) Mismatched RTOX value \r\n"); + + nfcDepReEnableRx(gNfcip.rxBuf, gNfcip.rxBufLen, gNfcip.rxRcvdLen); + return RFAL_ERR_BUSY; /* RFAL_ERR_PROTO - Ignore unexpected RTOX value + */ + } + + /* Clear waiting for RTOX Ack Flag */ + gNfcip.isWait4RTOX = false; + + /* Check if a Tx is already pending */ + if (gNfcip.isTxPending) { + nfcipLogW(" NFCIP(T) Tx pending, go immediately to TX \r\n"); + + gNfcip.state = NFCIP_ST_TARG_DEP_TX; + return RFAL_ERR_BUSY; + } + + /* Start RTOX timer and change to check state */ + nfcipTimerStart( + gNfcip.RTOXTimer, + nfcipRTOXAdjust(nfcipConv1FcToMs(gNfcip.lastRTOX * + rfalNfcDepWT2RWT(gNfcip.cfg.to)))); + gNfcip.state = NFCIP_ST_TARG_DEP_RTOX; + + return RFAL_ERR_BUSY; + } + } else { + /* Unexpected S-PDU */ + nfcipLogI(" NFCIP(T) Unexpected S-PDU \r\n"); + + nfcDepReEnableRx(gNfcip.rxBuf, gNfcip.rxBufLen, gNfcip.rxRcvdLen); + return RFAL_ERR_BUSY; /* RFAL_ERR_PROTO - Ignore unexpected S-PDU */ + } + } + + /*******************************************************************************/ + /* Process I-PDU */ + /*******************************************************************************/ + if (nfcip_PFBisIPDU(rxPFB)) { + if (gNfcip.pni != nfcip_PBF_PNI(rxPFB)) { + nfcipLogI(" NFCIP(T) Rcvd IPDU wrong PNI curPNI: %d rxPNI: %d \r\n", + gNfcip.pni, nfcip_PBF_PNI(rxPFB)); + + /* Digital 1.1 16.12.3.4 - If last send was ATN and rx PNI is minus 1 */ + if (nfcip_PFBisSATN(gNfcip.lastPFB) && + (nfcip_PNIDec(gNfcip.pni) == nfcip_PBF_PNI(rxPFB))) { + /* Spec says to leave current PNI as is, but will be Inc after Data Tx, + * remaining the same */ + gNfcip.pni = nfcip_PNIDec(gNfcip.pni); + + if (nfcip_PFBisIMI(rxPFB)) { + nfcipLogI( + " NFCIP(T) PNI = prevPNI && ATN before && chaining -> send ACK " + "\r\n"); + RFAL_EXIT_ON_ERR(ret, + nfcipDEPControlMsg(nfcip_PFBRPDU_ACK(gNfcip.pni), + gNfcip.rxBuf[rxMsgIt++])); + + /* Digital 1.1 16.12.3.4 (...) leave the current PNI unchanged + * afterwards */ + gNfcip.pni = nfcip_PNIInc(gNfcip.pni); + } else { + nfcipLogI( + " NFCIP(T) PNI = prevPNI && ATN before -> reTx last I-PDU \r\n"); + gNfcip.state = NFCIP_ST_TARG_DEP_TX; + } + + return RFAL_ERR_BUSY; + } + + nfcDepReEnableRx(gNfcip.rxBuf, gNfcip.rxBufLen, gNfcip.rxRcvdLen); + return RFAL_ERR_BUSY; /* RFAL_ERR_PROTO - Ignore bad PNI value */ + } + + nfcipLogD(" NFCIP(T) Rcvd IPDU OK PNI: %d \r\n", gNfcip.pni); + + /*******************************************************************************/ + /* Successful data exchange */ + /*******************************************************************************/ + *outActRxLen = + ((uint16_t)nfcDepLen - RFAL_NFCDEP_DEP_HEADER - (uint16_t)optHdrLen); + + nfcipClearCounters(); + + if ((&gNfcip.rxBuf[gNfcip.rxBufPaylPos] != + &gNfcip.rxBuf[RFAL_NFCDEP_DEP_HEADER + optHdrLen]) && + (*outActRxLen > 0U)) { + RFAL_MEMMOVE(&gNfcip.rxBuf[gNfcip.rxBufPaylPos], + &gNfcip.rxBuf[RFAL_NFCDEP_DEP_HEADER + optHdrLen], + *outActRxLen); + } + + /*******************************************************************************/ + /* Check if Initiator is indicating chaining MI */ + /*******************************************************************************/ + if (nfcip_PFBisIMI(rxPFB)) { + gNfcip.isRxChaining = true; + *outIsChaining = true; + + nfcipLogD(" NFCIP(T) Rcvd IPDU OK w MI -> ACK \r\n"); + RFAL_EXIT_ON_ERR(ret, nfcipDEPControlMsg(nfcip_PFBRPDU_ACK(gNfcip.pni), + gNfcip.rxBuf[rxMsgIt++])); + + gNfcip.pni = nfcip_PNIInc(gNfcip.pni); + + return RFAL_ERR_AGAIN; /* Send Again signalling to run again, but some + chaining data has arrived*/ + } else { + if (gNfcip.isRxChaining) { + nfcipLogI(" NFCIP(T) Rcvd last IPDU chaining finished \r\n"); + } + + /*******************************************************************************/ + /* Reception done, send to DH and start RTOX timer */ + /*******************************************************************************/ + nfcipTimerStart( + gNfcip.RTOXTimer, + nfcipRTOXAdjust(nfcipConv1FcToMs(rfalNfcDepWT2RWT(gNfcip.cfg.to)))); + gNfcip.state = NFCIP_ST_TARG_DEP_RTOX; + + gNfcip.isRxChaining = false; + ret = RFAL_ERR_NONE; /* Data exchange done */ + } + } + return ret; +} + +/*******************************************************************************/ +static ReturnCode nfcipTx(rfalNfcDepCmd cmd, uint8_t *txBuf, uint8_t *paylBuf, + uint16_t paylLen, uint8_t pfbData, uint32_t fwt) { + uint16_t txBufIt; + uint8_t *txBlock; + uint8_t *payloadBuf; + uint8_t pfb; + + if (txBuf == NULL) { + return RFAL_ERR_PARAM; + } + + payloadBuf = paylBuf; /* MISRA 17.8: Use intermediate variable */ + + if ((paylLen == 0U) || (payloadBuf == NULL)) { + payloadBuf = + (uint8_t *)&txBuf[RFAL_NFCDEP_DEPREQ_HEADER_LEN]; /* If not a DEP (no + Data) ensure enough + space for header */ + } + + txBufIt = 0; + pfb = pfbData; /* MISRA 17.8: Use intermediate variable */ + + txBlock = payloadBuf; /* Point to beginning of the Data, and go backwards */ + + gNfcip.lastCmd = (uint8_t)cmd; /* Store last cmd sent */ + gNfcip.lastPFB = NFCIP_PFB_INVALID; /* Reset last pfb sent */ + + /*******************************************************************************/ + /* Compute outgoing NFCIP message */ + /*******************************************************************************/ + switch (cmd) { + /*******************************************************************************/ + case NFCIP_CMD_ATR_RES: + case NFCIP_CMD_ATR_REQ: + + rfalNfcDepSetNFCID(payloadBuf, gNfcip.cfg.nfcid, + gNfcip.cfg.nfcidLen); /* NFCID */ + txBufIt += RFAL_NFCDEP_NFCID3_LEN; + + payloadBuf[txBufIt++] = gNfcip.cfg.did; /* DID */ + payloadBuf[txBufIt++] = gNfcip.cfg.bs; /* BS */ + payloadBuf[txBufIt++] = gNfcip.cfg.br; /* BR */ + + if (cmd == NFCIP_CMD_ATR_RES) { + payloadBuf[txBufIt++] = gNfcip.cfg.to; /* ATR_RES[ TO ] */ + } + + if (gNfcip.cfg.gbLen > 0U) { + payloadBuf[txBufIt++] = + nfcip_PPwGB(gNfcip.cfg.lr); /* PP signalling GB */ + RFAL_MEMCPY(&payloadBuf[txBufIt], gNfcip.cfg.gb, + gNfcip.cfg.gbLen); /* set General Bytes */ + txBufIt += gNfcip.cfg.gbLen; + } else { + payloadBuf[txBufIt++] = + rfalNfcDepLR2PP(gNfcip.cfg.lr); /* PP without GB */ + } + + if ((txBufIt + RFAL_NFCDEP_CMDTYPE_LEN + RFAL_NFCDEP_CMD_LEN) > + RFAL_NFCDEP_ATRREQ_MAX_LEN) /* Check max ATR length (ATR_REQ = + ATR_RES)*/ + { + return RFAL_ERR_PARAM; + } + break; + + /*******************************************************************************/ + case NFCIP_CMD_WUP_REQ: /* ISO 18092 - 12.5.2.1 */ + + rfalNfcDepSetNFCID((payloadBuf), gNfcip.cfg.nfcid, + gNfcip.cfg.nfcidLen); /* NFCID */ + txBufIt += RFAL_NFCDEP_NFCID3_LEN; + + *(--txBlock) = gNfcip.cfg.did; /* DID */ + break; + + /*******************************************************************************/ + case NFCIP_CMD_WUP_RES: /* ISO 18092 - 12.5.2.2 */ + case NFCIP_CMD_PSL_REQ: + case NFCIP_CMD_PSL_RES: + + *(--txBlock) = gNfcip.cfg.did; /* DID */ + break; + + /*******************************************************************************/ + case NFCIP_CMD_RLS_REQ: + case NFCIP_CMD_RLS_RES: + case NFCIP_CMD_DSL_REQ: + case NFCIP_CMD_DSL_RES: + + /* Digital 1.0 - 14.8.1.1 & 14.9.1.1 & 14.10.1.1 Only add DID if not 0 */ + if (gNfcip.cfg.did != RFAL_NFCDEP_DID_NO) { + *(--txBlock) = gNfcip.cfg.did; /* DID */ + } + break; + + /*******************************************************************************/ + case NFCIP_CMD_DEP_REQ: + case NFCIP_CMD_DEP_RES: + + /* Compute optional PFB bits */ + if (gNfcip.cfg.did != RFAL_NFCDEP_DID_NO) { + pfb |= NFCIP_PFB_DID_BIT; + } + if (gNfcip.cfg.nad != RFAL_NFCDEP_NAD_NO) { + pfb |= NFCIP_PFB_NAD_BIT; + } + if ((gNfcip.isTxChaining) && (nfcip_PFBisIPDU(pfb))) { + pfb |= NFCIP_PFB_MI_BIT; + } + + /* Store PFB for future handling */ + gNfcip.lastPFB = pfb; /* store PFB sent */ + + if (!nfcip_PFBisSATN(pfb)) { + gNfcip.lastPFBnATN = pfb; /* store last PFB different then ATN */ + } + + /* Add NAD if it is to be supported */ + if (gNfcip.cfg.nad != RFAL_NFCDEP_NAD_NO) { + *(--txBlock) = gNfcip.cfg.nad; /* NAD */ + } + + /* Digital 1.0 - 14.8.1.1 & 14.8.1.1 Only add DID if not 0 */ + if (gNfcip.cfg.did != RFAL_NFCDEP_DID_NO) { + *(--txBlock) = gNfcip.cfg.did; /* DID */ + } + + *(--txBlock) = pfb; /* PFB */ + + /* NCI 1.0 - Check if Empty frames are allowed */ + if ((paylLen == 0U) && nfcipIsEmptyDEPDisabled(gNfcip.cfg.oper) && + nfcip_PFBisIPDU(pfb)) { + return RFAL_ERR_PARAM; + } + break; + + /*******************************************************************************/ + default: + return RFAL_ERR_PARAM; + } + + /*******************************************************************************/ + /* Prepend Header */ + /*******************************************************************************/ + *(--txBlock) = (uint8_t)cmd; /* CMD */ + *(--txBlock) = + (uint8_t)(nfcipCmdIsReq(cmd) ? NFCIP_REQ : NFCIP_RES); /* CMDType */ + + txBufIt += paylLen + + (uint16_t)((uintptr_t)payloadBuf - + (uintptr_t)txBlock); /* Calculate overall buffer size */ + + if (txBufIt > + gNfcip + .fsc) /* Check if msg length violates the maximum payload size FSC */ + { + return RFAL_ERR_NOTSUPP; + } + + /*******************************************************************************/ + return nfcipDataTx(txBlock, txBufIt, fwt); +} + +/* + ****************************************************************************** + * GLOBAL FUNCTIONS + ****************************************************************************** + */ + +/*******************************************************************************/ +static void nfcipConfig(const rfalNfcDepConfigs *cfg) { + if (cfg == NULL) { + return; + } + + RFAL_MEMCPY(&gNfcip.cfg, cfg, + sizeof(rfalNfcDepConfigs)); /* Copy given config to local */ + + gNfcip.cfg.to = RFAL_MIN(RFAL_NFCDEP_WT_TRG_MAX, + gNfcip.cfg.to); /* Ensure proper WT value */ + gNfcip.cfg.did = nfcip_DIDMax(gNfcip.cfg.did); /* Ensure proper DID value */ + gNfcip.fsc = + rfalNfcDepLR2FS(gNfcip.cfg.lr); /* Calculate FSC based on given LR */ + + gNfcip.state = + ((gNfcip.cfg.role == RFAL_NFCDEP_ROLE_TARGET) ? NFCIP_ST_TARG_WAIT_ATR + : NFCIP_ST_INIT_IDLE); +} + +/*******************************************************************************/ +static ReturnCode nfcipRun(uint16_t *outActRxLen, bool *outIsChaining) { + ReturnCode ret; + + ret = RFAL_ERR_SYNTAX; + + nfcipLogD(" NFCIP Run() state: %d \r\n", gNfcip.state); + + switch (gNfcip.state) { + /*******************************************************************************/ + case NFCIP_ST_IDLE: + case NFCIP_ST_INIT_DEP_IDLE: + case NFCIP_ST_TARG_DEP_IDLE: + case NFCIP_ST_TARG_DEP_SLEEP: + return RFAL_ERR_NONE; + + /*******************************************************************************/ + case NFCIP_ST_INIT_DEP_TX: + + nfcipLogD(" NFCIP(I) Tx PNI: %d txLen: %d \r\n", gNfcip.pni, + gNfcip.txBufLen); + ret = nfcipTx(NFCIP_CMD_DEP_REQ, gNfcip.txBuf, + &gNfcip.txBuf[gNfcip.txBufPaylPos], gNfcip.txBufLen, + nfcip_PFBIPDU(gNfcip.pni), + (gNfcip.cfg.fwt + gNfcip.cfg.dFwt)); + + switch (ret) { + case RFAL_ERR_NONE: + gNfcip.state = NFCIP_ST_INIT_DEP_RX; + break; + + case RFAL_ERR_PARAM: + default: + gNfcip.state = NFCIP_ST_INIT_DEP_IDLE; + return ret; + } + /* fall through */ + + /*******************************************************************************/ + case NFCIP_ST_INIT_DEP_RX: /* PRQA S 2003 # MISRA 16.3 - Intentional fall + through */ + + ret = nfcipDataRx(false); + + if (ret != RFAL_ERR_BUSY) { + ret = nfcipInitiatorHandleDEP( + ret, ((gNfcip.rxRcvdLen != NULL) ? *gNfcip.rxRcvdLen : 0U), + outActRxLen, outIsChaining); + } + + break; + + /*******************************************************************************/ + case NFCIP_ST_TARG_DEP_RTOX: + + if (!nfcipTimerisExpired( + gNfcip.RTOXTimer)) /* Do nothing until RTOX timer has expired */ + { + return RFAL_ERR_BUSY; + } + + /* If we cannot send a RTOX raise a Timeout error so that we do not + * hold the field On forever in AP2P */ + if (nfcipIsRTOXReqDisabled(gNfcip.cfg.oper)) { + /* We should reEnable Rx, and measure time between our field Off to + * either report link loss or recover #287 */ + nfcipLogI( + " NFCIP(T) RTOX not sent due to config, NOT reenabling Rx \r\n"); + return RFAL_ERR_TIMEOUT; + } + + if (gNfcip.cntRTOXRetrys++ > + RFAL_NFCDEP_MAX_RTOX_RETRYS) /* Check maximum consecutive RTOX + requests */ + { + return RFAL_ERR_PROTO; + } + + nfcipLogI(" NFCIP(T) RTOX sent \r\n"); + + gNfcip.lastRTOX = nfcip_RTOXTargMax( + gNfcip.cfg.to); /* Calculate requested RTOX value, and send it */ + RFAL_EXIT_ON_ERR(ret, + nfcipDEPControlMsg(nfcip_PFBSPDU_TO(), gNfcip.lastRTOX)); + + /* Set waiting for RTOX Ack Flag */ + gNfcip.isWait4RTOX = true; + + gNfcip.state = + NFCIP_ST_TARG_DEP_RX; /* Go back to Rx to process RTOX ack */ + return RFAL_ERR_BUSY; + + /*******************************************************************************/ + case NFCIP_ST_TARG_DEP_TX: + + nfcipLogD(" NFCIP(T) Tx PNI: %d txLen: %d \r\n", gNfcip.pni, + gNfcip.txBufLen); + ret = nfcipTx(NFCIP_CMD_DEP_RES, gNfcip.txBuf, + &gNfcip.txBuf[gNfcip.txBufPaylPos], gNfcip.txBufLen, + nfcip_PFBIPDU(gNfcip.pni), NFCIP_NO_FWT); + + /* Clear flags */ + gNfcip.isTxPending = false; + gNfcip.isWait4RTOX = false; + + /* Digital 1.0 14.12.3.4 Increment the current PNI after Tx */ + gNfcip.pni = nfcip_PNIInc(gNfcip.pni); + + switch (ret) { + case RFAL_ERR_NONE: + gNfcip.state = NFCIP_ST_TARG_DEP_RX; /* All OK, goto Rx state */ + break; + + case RFAL_ERR_PARAM: + default: + gNfcip.state = + NFCIP_ST_TARG_DEP_IDLE; /* Upon Tx error, goto IDLE state */ + return ret; + } + /* fall through */ + + /*******************************************************************************/ + case NFCIP_ST_TARG_DEP_RX: /* PRQA S 2003 # MISRA 16.3 - Intentional fall + through */ + + if (gNfcip.isReqPending) /* if already has Data should be from a DEP from + nfcipTargetHandleActivation() */ + { + nfcipLogD(" NFCIP(T) Skipping Rx Using DEP from Activation \r\n"); + + gNfcip.isReqPending = false; + ret = RFAL_ERR_NONE; + } else { + ret = nfcipDataRx(false); + } + + if (ret != RFAL_ERR_BUSY) { + ret = nfcipTargetHandleRX(ret, outActRxLen, outIsChaining); + } + + break; + + /*******************************************************************************/ + default: + /* MISRA 16.4: no empty default statement (a comment being enough) */ + break; + } + return ret; +} + +/*******************************************************************************/ +void rfalNfcDepSetDeactivatingCallback(rfalNfcDepDeactCallback pFunc) { + gNfcip.isDeactivating = pFunc; +} + +/*******************************************************************************/ +void rfalNfcDepInitialize(void) { + nfcipLogD(" NFCIP Ini() \r\n"); + + gNfcip.state = NFCIP_ST_IDLE; + gNfcip.isDeactivating = NULL; + + gNfcip.isTxPending = false; + gNfcip.isWait4RTOX = false; + gNfcip.isReqPending = false; + + gNfcip.cfg.oper = + (RFAL_NFCDEP_OPER_FULL_MI_DIS | RFAL_NFCDEP_OPER_EMPTY_DEP_EN | + RFAL_NFCDEP_OPER_ATN_EN | RFAL_NFCDEP_OPER_RTOX_REQ_EN); + + gNfcip.cfg.did = RFAL_NFCDEP_DID_NO; + gNfcip.cfg.nad = RFAL_NFCDEP_NAD_NO; + + gNfcip.cfg.br = RFAL_NFCDEP_Bx_NO_HIGH_BR; + gNfcip.cfg.bs = RFAL_NFCDEP_Bx_NO_HIGH_BR; + + gNfcip.cfg.lr = RFAL_NFCDEP_LR_254; + gNfcip.fsc = rfalNfcDepLR2FS(gNfcip.cfg.lr); + + gNfcip.cfg.gbLen = 0; + + gNfcip.cfg.fwt = NFCIP_RWT_ACTIVATION; + gNfcip.cfg.dFwt = RFAL_NFCDEP_WT_DELTA; + + gNfcip.pni = 0; + + /* Destroy any ongoing RTOX timer*/ + nfcipTimerDestroy(gNfcip.RTOXTimer); + gNfcip.RTOXTimer = 0U; + + gNfcip.PDUTxPos = 0; + gNfcip.PDURxPos = 0; + gNfcip.PDUParam.rxLen = NULL; + gNfcip.PDUParam.rxBuf = NULL; + gNfcip.PDUParam.txBuf = NULL; + + nfcipClearCounters(); +} + +/*******************************************************************************/ +static void nfcipSetDEPParams(const rfalNfcDepDEPParams *DEPParams) { + nfcipLogD(" NFCIP SetDEP() txLen: %d \r\n", DEPParams->txBufLen); + + gNfcip.isTxChaining = DEPParams->txChaining; + gNfcip.txBuf = DEPParams->txBuf; + gNfcip.rxBuf = DEPParams->rxBuf; + gNfcip.txBufLen = DEPParams->txBufLen; + gNfcip.rxBufLen = DEPParams->rxBufLen; + gNfcip.txBufPaylPos = DEPParams->txBufPaylPos; + gNfcip.rxBufPaylPos = DEPParams->rxBufPaylPos; + + if (DEPParams->did != RFAL_NFCDEP_DID_KEEP) { + gNfcip.cfg.did = nfcip_DIDMax(DEPParams->did); + } + + gNfcip.cfg.fwt = DEPParams->fwt; + gNfcip.cfg.dFwt = DEPParams->dFwt; + gNfcip.fsc = DEPParams->fsc; + + if (gNfcip.cfg.role == RFAL_NFCDEP_ROLE_TARGET) { + /* If there's any data to be sent go for Tx */ + if (DEPParams->txBufLen > 0U) { + /* Ensure that an RTOX Ack is not being expected at moment */ + if (!gNfcip.isWait4RTOX) { + gNfcip.state = NFCIP_ST_TARG_DEP_TX; + return; + } else { + /* If RTOX Ack is expected, signal a pending Tx to be transmitted right + * after */ + gNfcip.isTxPending = true; + nfcipLogW(" NFCIP(T) Waiting RTOX, queueing outgoing DEP Block \r\n"); + } + } + + /*Digital 1.0 14.12.4.1 In target mode the first PDU MUST be sent by the + * Initiator */ + gNfcip.state = NFCIP_ST_TARG_DEP_RX; + return; + } + + /* New data TxRx request clear previous error counters for consecutive TxRx + * without reseting communication/protocol layer*/ + nfcipClearCounters(); + + gNfcip.state = NFCIP_ST_INIT_DEP_TX; +} + +/*******************************************************************************/ +bool rfalNfcDepTargetRcvdATR(void) { + return ((gNfcip.cfg.role == RFAL_NFCDEP_ROLE_TARGET) && + nfcipIsTarget(gNfcip.state) && + (gNfcip.state > NFCIP_ST_TARG_WAIT_ATR)); +} + +/*******************************************************************************/ +bool rfalNfcDepIsAtrReq(const uint8_t *buf, uint16_t bufLen, uint8_t *nfcid3) { + uint8_t msgIt; + + msgIt = 0; + + if ((bufLen < RFAL_NFCDEP_ATRREQ_MIN_LEN) || + (bufLen > RFAL_NFCDEP_ATRREQ_MAX_LEN)) { + return false; + } + + if (buf[msgIt++] != NFCIP_REQ) { + return false; + } + + if (buf[msgIt++] != (uint8_t)NFCIP_CMD_ATR_REQ) { + return false; + } + + /* Output NFID3 if requested */ + if (nfcid3 != NULL) { + RFAL_MEMCPY(nfcid3, &buf[RFAL_NFCDEP_ATR_REQ_NFCID3_POS], + RFAL_NFCDEP_NFCID3_LEN); + } + + return true; +} + +/*******************************************************************************/ +static ReturnCode nfcipTargetHandleActivation(rfalNfcDepDevice *nfcDepDev, + uint8_t *outBRS) { + ReturnCode ret; + uint8_t msgIt; + uint8_t txBuf[RFAL_NFCDEP_HEADER_PAD + NFCIP_PSLRES_LEN]; + + /*******************************************************************************/ + /* Check if we are in correct state */ + /*******************************************************************************/ + if (gNfcip.state != NFCIP_ST_TARG_WAIT_ACTV) { + return RFAL_ERR_WRONG_STATE; + } + + /*******************************************************************************/ + /* Check required parameters */ + /*******************************************************************************/ + if (outBRS == NULL) { + return RFAL_ERR_PARAM; + } + + /*******************************************************************************/ + /* Wait and process incoming cmd (PSL / DEP) */ + /*******************************************************************************/ + ret = nfcipDataRx(false); + + if (ret != RFAL_ERR_NONE) { + return ret; + } + + if (gNfcip.rxBuf == NULL) { + return RFAL_ERR_IO; + } + + msgIt = 0; + *outBRS = RFAL_NFCDEP_BRS_MAINTAIN; /* set out BRS to be maintained */ + + msgIt++; /* Skip LEN byte */ + + if (gNfcip.rxBuf[msgIt++] != NFCIP_REQ) { + return RFAL_ERR_PROTO; + } + + if (gNfcip.rxBuf[msgIt] == (uint8_t)NFCIP_CMD_PSL_REQ) { + msgIt++; + + if (gNfcip.rxBuf[msgIt++] != gNfcip.cfg.did) /* Checking DID */ + { + return RFAL_ERR_PROTO; + } + + nfcipLogI(" NFCIP(T) PSL REQ rcvd \r\n"); + + *outBRS = gNfcip.rxBuf[msgIt++]; /* assign output BRS value */ + + /* Store FSL(LR) and update current config */ + gNfcip.cfg.lr = (gNfcip.rxBuf[msgIt++] & RFAL_NFCDEP_LR_VAL_MASK); + gNfcip.fsc = rfalNfcDepLR2FS(gNfcip.cfg.lr); + + /*******************************************************************************/ + /* Update NFC-DDE Device info */ + if (nfcDepDev != NULL) { + /* Update Bitrate info */ + /* PRQA S 4342 2 # MISRA 10.5 - Layout of enum rfalBitRate and definition + * of rfalNfcDepBRS2DSI guarantee no invalid enum values to be created */ + nfcDepDev->info.DSI = (rfalBitRate)rfalNfcDepBRS2DSI( + *outBRS); /* DSI codes the bit rate from Initiator to Target */ + nfcDepDev->info.DRI = (rfalBitRate)rfalNfcDepBRS2DRI( + *outBRS); /* DRI codes the bit rate from Target to Initiator */ + + /* Update Length Reduction and Frame Size */ + nfcDepDev->info.LR = gNfcip.cfg.lr; + nfcDepDev->info.FS = gNfcip.fsc; + + /* Update PPi byte */ + nfcDepDev->activation.Initiator.ATR_REQ.PPi &= ~RFAL_NFCDEP_PP_LR_MASK; + nfcDepDev->activation.Initiator.ATR_REQ.PPi |= + rfalNfcDepLR2PP(gNfcip.cfg.lr); + } + + rfalSetBitRate(RFAL_BR_KEEP, gNfcip.nfcDepDev->info.DSI); + + RFAL_EXIT_ON_ERR( + ret, nfcipTx(NFCIP_CMD_PSL_RES, txBuf, NULL, 0, 0, NFCIP_NO_FWT)); + } else { + if (gNfcip.rxBuf[msgIt] == (uint8_t)NFCIP_CMD_DEP_REQ) { + msgIt++; + + /*******************************************************************************/ + /* Digital 1.0 14.12.3.1 PNI must be initialized to 0 */ + if (nfcip_PBF_PNI(gNfcip.rxBuf[msgIt]) != 0U) { + return RFAL_ERR_PROTO; + } + + /*******************************************************************************/ + /* Digital 1.0 14.8.2.1 check if DID is expected and match -> Protocol + * Error */ + if (nfcip_PFBhasDID(gNfcip.rxBuf[msgIt])) { + if (gNfcip.rxBuf[++msgIt] != gNfcip.cfg.did) { + return RFAL_ERR_PROTO; + } + } else if (gNfcip.cfg.did != + RFAL_NFCDEP_DID_NO) /* DID expected but not rcv */ + { + return RFAL_ERR_PROTO; + } else { + /* MISRA 15.7 - Empty else */ + } + } + + /* Signal Request pending to be digested on normal Handling (DEP_REQ, + * DSL_REQ, RLS_REQ) */ + gNfcip.isReqPending = true; + } + + gNfcip.state = NFCIP_ST_TARG_DEP_RX; + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalNfcDepATR(const rfalNfcDepAtrParam *param, + rfalNfcDepAtrRes *atrRes, uint8_t *atrResLen) { + ReturnCode ret; + rfalNfcDepConfigs cfg; + uint16_t rxLen; + uint8_t msgIt; + uint8_t txBuf[RFAL_NFCDEP_ATRREQ_MAX_LEN]; + uint8_t rxBuf[NFCIP_ATRRES_BUF_LEN]; + + if ((param == NULL) || (atrRes == NULL) || (atrResLen == NULL)) { + return RFAL_ERR_PARAM; + } + + RFAL_MEMSET(&cfg, 0x00, sizeof(rfalNfcDepConfigs)); + + /*******************************************************************************/ + /* Configure NFC-DEP layer */ + /*******************************************************************************/ + + cfg.did = param->DID; + cfg.nad = param->NAD; + cfg.fwt = RFAL_NFCDEP_MAX_FWT; + cfg.dFwt = RFAL_NFCDEP_WT_DELTA; + cfg.br = param->BR; + cfg.bs = param->BS; + cfg.lr = param->LR; + cfg.to = RFAL_NFCDEP_WT_TRG_MAX; /* Not used in Initiator mode */ + + cfg.gbLen = param->GBLen; + if (cfg.gbLen > 0U) /* MISRA 21.18 */ + { + RFAL_MEMCPY(cfg.gb, param->GB, cfg.gbLen); + } + + cfg.nfcidLen = param->nfcidLen; + if (cfg.nfcidLen > 0U) /* MISRA 21.18 */ + { + RFAL_MEMCPY(cfg.nfcid, param->nfcid, cfg.nfcidLen); + } + + cfg.role = RFAL_NFCDEP_ROLE_INITIATOR; + cfg.oper = param->operParam; + cfg.commMode = param->commMode; + + rfalNfcDepInitialize(); + nfcipConfig(&cfg); + + /*******************************************************************************/ + /* Send ATR_REQ */ + /*******************************************************************************/ + + RFAL_EXIT_ON_ERR( + ret, nfcipTxRx(NFCIP_CMD_ATR_REQ, txBuf, nfcipRWTActivation(), NULL, 0, + rxBuf, NFCIP_ATRRES_BUF_LEN, &rxLen)); + + /*******************************************************************************/ + /* ATR sent, check response */ + /*******************************************************************************/ + msgIt = 0; + rxLen = ((uint16_t)rxBuf[msgIt++] - RFAL_NFCDEP_LEN_LEN); /* use LEN byte */ + + if ((rxLen < RFAL_NFCDEP_ATRRES_MIN_LEN) || + (rxLen > RFAL_NFCDEP_ATRRES_MAX_LEN)) /* Checking length: ATR_RES */ + { + return RFAL_ERR_PROTO; + } + + if (rxBuf[msgIt++] != NFCIP_RES) /* Checking if is a response*/ + { + return RFAL_ERR_PROTO; + } + + if (rxBuf[msgIt++] != + (uint8_t)NFCIP_CMD_ATR_RES) /* Checking if is a ATR RES */ + { + return RFAL_ERR_PROTO; + } + + RFAL_MEMCPY((uint8_t *)atrRes, (rxBuf + RFAL_NFCDEP_LEN_LEN), rxLen); + *atrResLen = (uint8_t)rxLen; + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalNfcDepPSL(uint8_t BRS, uint8_t FSL) { + ReturnCode ret; + uint16_t rxLen; + uint8_t msgIt; + uint8_t txBuf[NFCIP_PSLREQ_LEN + NFCIP_PSLPAY_LEN]; + uint8_t rxBuf[NFCIP_PSLRES_LEN]; + + msgIt = NFCIP_PSLREQ_LEN; + + txBuf[msgIt++] = BRS; + txBuf[msgIt++] = FSL; + + /*******************************************************************************/ + /* Send PSL REQ and wait for response */ + /*******************************************************************************/ + RFAL_EXIT_ON_ERR( + ret, + nfcipTxRx(NFCIP_CMD_PSL_REQ, txBuf, (gNfcip.cfg.fwt + gNfcip.cfg.dFwt), + &txBuf[NFCIP_PSLREQ_LEN], (msgIt - NFCIP_PSLREQ_LEN), rxBuf, + NFCIP_PSLRES_LEN, &rxLen)); + + /*******************************************************************************/ + /* PSL sent, check response */ + /*******************************************************************************/ + msgIt = 0; + rxLen = (uint16_t)(rxBuf[msgIt++]); /* use LEN byte */ + + if (rxLen < NFCIP_PSLRES_LEN) /* Checking length: LEN + RLS_RES */ + { + return RFAL_ERR_PROTO; + } + + if (rxBuf[msgIt++] != NFCIP_RES) /* Checking if is a response */ + { + return RFAL_ERR_PROTO; + } + + if (rxBuf[msgIt++] != + (uint8_t)NFCIP_CMD_PSL_RES) /* Checking if is a PSL RES */ + { + return RFAL_ERR_PROTO; + } + + if (rxBuf[msgIt++] != gNfcip.cfg.did) /* Checking DID */ + { + return RFAL_ERR_PROTO; + } + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalNfcDepDSL(void) { + ReturnCode ret; + uint8_t txBuf[RFAL_NFCDEP_HEADER_PAD + NFCIP_DSLREQ_LEN]; + uint8_t rxBuf[NFCIP_DSLRES_LEN]; + uint8_t rxMsgIt; + uint16_t rxLen = 0; + + if (gNfcip.cfg.role == RFAL_NFCDEP_ROLE_TARGET) { + return RFAL_ERR_NONE; /* Target has no deselect procedure */ + } + + /* Repeating a DSL REQ is optional, not doing it */ + RFAL_EXIT_ON_ERR(ret, nfcipTxRx(NFCIP_CMD_DSL_REQ, txBuf, + (gNfcip.cfg.fwt + gNfcip.cfg.dFwt), NULL, 0, + rxBuf, (uint16_t)sizeof(rxBuf), &rxLen)); + + /*******************************************************************************/ + rxMsgIt = 0; + + if (rxBuf[rxMsgIt++] < NFCIP_DSLRES_MIN) /* Checking length: LEN + DSL_RES */ + { + return RFAL_ERR_PROTO; + } + + if (rxBuf[rxMsgIt++] != NFCIP_RES) /* Checking if is a response */ + { + return RFAL_ERR_PROTO; + } + + if (rxBuf[rxMsgIt++] != + (uint8_t)NFCIP_CMD_DSL_RES) /* Checking if is DSL RES */ + { + return RFAL_ERR_PROTO; + } + + if (gNfcip.cfg.did != RFAL_NFCDEP_DID_NO) { + if (rxBuf[rxMsgIt++] != gNfcip.cfg.did) { + return RFAL_ERR_PROTO; + } + } + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalNfcDepRLS(void) { + ReturnCode ret; + uint8_t txBuf[RFAL_NFCDEP_HEADER_PAD + NFCIP_RLSREQ_LEN]; + uint8_t rxBuf[NFCIP_RLSRES_LEN]; + uint8_t rxMsgIt; + uint16_t rxLen = 0; + + if (gNfcip.cfg.role == + RFAL_NFCDEP_ROLE_TARGET) /* Target has no release procedure */ + { + return RFAL_ERR_NONE; + } + + /* Repeating a RLS REQ is optional, not doing it */ + RFAL_EXIT_ON_ERR(ret, nfcipTxRx(NFCIP_CMD_RLS_REQ, txBuf, + (gNfcip.cfg.fwt + gNfcip.cfg.dFwt), NULL, 0, + rxBuf, (uint16_t)sizeof(rxBuf), &rxLen)); + + /*******************************************************************************/ + rxMsgIt = 0; + + if (rxBuf[rxMsgIt++] < NFCIP_RLSRES_MIN) /* Checking length: LEN + RLS_RES */ + { + return RFAL_ERR_PROTO; + } + + if (rxBuf[rxMsgIt++] != NFCIP_RES) /* Checking if is a response */ + { + return RFAL_ERR_PROTO; + } + + if (rxBuf[rxMsgIt++] != + (uint8_t)NFCIP_CMD_RLS_RES) /* Checking if is RLS RES */ + { + return RFAL_ERR_PROTO; + } + + if (gNfcip.cfg.did != RFAL_NFCDEP_DID_NO) { + if (rxBuf[rxMsgIt++] != gNfcip.cfg.did) { + return RFAL_ERR_PROTO; + } + } + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalNfcDepInitiatorHandleActivation(rfalNfcDepAtrParam *param, + rfalBitRate desiredBR, + rfalNfcDepDevice *nfcDepDev) { + ReturnCode ret; + uint8_t maxRetyrs; + uint8_t PSL_BRS; + uint8_t PSL_FSL; + bool sendPSL; + + if ((param == NULL) || (nfcDepDev == NULL)) { + return RFAL_ERR_PARAM; + } + + param->NAD = RFAL_NFCDEP_NAD_NO; /* Digital 1.1 16.6.2.9 Initiator SHALL NOT + use NAD */ + maxRetyrs = NFCIP_ATR_RETRY_MAX; + + /*******************************************************************************/ + /* Send ATR REQ and wait for response */ + /*******************************************************************************/ + do { /* Upon transmission error ATR REQ should be retried */ + + ret = rfalNfcDepATR(param, &nfcDepDev->activation.Target.ATR_RES, + &nfcDepDev->activation.Target.ATR_RESLen); + + if (nfcipIsTransmissionError(ret)) { + continue; + } + break; + } while ((maxRetyrs--) != 0U); + + if (ret != RFAL_ERR_NONE) { + return ret; + } + + /*******************************************************************************/ + /* Compute NFC-DEP device with ATR_RES */ + /*******************************************************************************/ + nfcDepDev->info.GBLen = + (nfcDepDev->activation.Target.ATR_RESLen - RFAL_NFCDEP_ATRRES_MIN_LEN); + nfcDepDev->info.DID = nfcDepDev->activation.Target.ATR_RES.DID; + nfcDepDev->info.NAD = RFAL_NFCDEP_NAD_NO; /* Digital 1.1 16.6.3.11 Initiator + SHALL ignore b1 of PPt */ + nfcDepDev->info.LR = + rfalNfcDepPP2LR(nfcDepDev->activation.Target.ATR_RES.PPt); + nfcDepDev->info.FS = rfalNfcDepLR2FS(nfcDepDev->info.LR); + nfcDepDev->info.WT = + (nfcDepDev->activation.Target.ATR_RES.TO & RFAL_NFCDEP_WT_MASK); + nfcDepDev->info.FWT = rfalNfcDepCalculateRWT(nfcDepDev->info.WT); + nfcDepDev->info.dFWT = RFAL_NFCDEP_WT_DELTA; + + rfalGetBitRate(&nfcDepDev->info.DSI, &nfcDepDev->info.DRI); + + /*******************************************************************************/ + /* Check if a PSL needs to be sent */ + /*******************************************************************************/ + sendPSL = false; + PSL_BRS = rfalNfcDepDx2BRS( + nfcDepDev->info + .DSI); /* Set current bit rate divisor on both directions */ + PSL_FSL = nfcDepDev->info.LR; /* Set current Frame Size */ + + /* Activity 1.0 9.4.4.15 & 9.4.6.3 NFC-DEP Activation PSL + * Activity 2.0 9.4.4.17 & 9.4.6.6 NFC-DEP Activation PSL + * + * PSL_REQ shall only be sent if desired bit rate is different from current + * (Activity 1.0) PSL_REQ shall be sent to update LR or bit rate + * (Activity 2.0) + * */ + +#if 0 /* PSL due to LR is disabled, can be enabled if desired*/ + /*******************************************************************************/ + /* Check Frame Size */ + /*******************************************************************************/ + if( gNfcip.cfg.lr < nfcDepDev->info.LR ) /* If our Length reduction is smaller */ + { + sendPSL = true; + + nfcDepDev->info.LR = RFAL_MIN( nfcDepDev->info.LR, gNfcip.cfg.lr ); + + gNfcip.cfg.lr = nfcDepDev->info.LR; /* Update nfcip LR to be used */ + gNfcip.fsc = rfalNfcDepLR2FS( gNfcip.cfg.lr ); /* Update nfcip FSC to be used */ + + PSL_FSL = gNfcip.cfg.lr; /* Set LR to be sent */ + + nfcipLogI( " NFCIP(I) Frame Size differ, PSL new fsc: %d \r\n", gNfcip.fsc ); + } +#endif + + /*******************************************************************************/ + /* Check Baud rates */ + /*******************************************************************************/ + if ((nfcDepDev->info.DSI != desiredBR) && + (desiredBR != RFAL_BR_KEEP)) /* if desired BR is different */ + { + if (nfcipDxIsSupported( + (uint8_t)desiredBR, nfcDepDev->activation.Target.ATR_RES.BRt, + nfcDepDev->activation.Target.ATR_RES + .BSt)) /* if desired BR is supported */ /* MISRA 13.5 */ + { + sendPSL = true; + PSL_BRS = rfalNfcDepDx2BRS(desiredBR); + + nfcipLogI(" NFCIP(I) BR differ, PSL BR: 0x%02X \r\n", PSL_BRS); + } + } + + /*******************************************************************************/ + if (sendPSL) { + /* Apply target's FWT for PSL_REQ Digital 2.2 17.11.2.5 */ + gNfcip.cfg.fwt = nfcDepDev->info.FWT; + + /*******************************************************************************/ + /* Send PSL REQ and wait for response */ + /*******************************************************************************/ + RFAL_EXIT_ON_ERR(ret, rfalNfcDepPSL(PSL_BRS, PSL_FSL)); + + /* Check if bit rate has been changed */ + if (nfcDepDev->info.DSI != desiredBR) { + /* Check if device was in Passive NFC-A and went to higher bit rates, use + * NFC-F */ + if ((nfcDepDev->info.DSI == RFAL_BR_106) && + (gNfcip.cfg.commMode == RFAL_NFCDEP_COMM_PASSIVE)) { +#if RFAL_FEATURE_NFCF + /* If Passive initialize NFC-F module */ + rfalNfcfPollerInitialize(desiredBR); +#else /* RFAL_FEATURE_NFCF */ + return RFAL_ERR_NOTSUPP; +#endif /* RFAL_FEATURE_NFCF */ + } + + nfcDepDev->info.DRI = + desiredBR; /* DSI Bit Rate coding from Initiator to Target */ + nfcDepDev->info.DSI = + desiredBR; /* DRI Bit Rate coding from Target to Initiator */ + + rfalSetBitRate(nfcDepDev->info.DSI, nfcDepDev->info.DRI); + } + + return RFAL_ERR_NONE; /* PSL has been sent */ + } + + return RFAL_ERR_NONE; /* No PSL has been sent */ +} + +/*******************************************************************************/ +uint32_t rfalNfcDepCalculateRWT(uint8_t wt) { + /* Digital 1.0 14.6.3.8 & Digital 1.1 16.6.3.9 */ + /* Digital 1.1 16.6.3.9 treat all RFU values as WT=14 */ + const uint8_t responseWaitTime = RFAL_MIN(RFAL_NFCDEP_WT_INI_MAX, wt); + + return (uint32_t)rfalNfcDepWT2RWT(responseWaitTime); +} + +/*******************************************************************************/ +static ReturnCode nfcipDataTx(uint8_t *txBuf, uint16_t txBufLen, uint32_t fwt) { + return rfalTransceiveBlockingTx( + txBuf, txBufLen, gNfcip.rxBuf, gNfcip.rxBufLen, gNfcip.rxRcvdLen, + (RFAL_TXRX_FLAGS_DEFAULT | (uint32_t)RFAL_TXRX_FLAGS_NFCIP1_ON), + ((fwt == NFCIP_NO_FWT) ? RFAL_FWT_NONE : fwt)); +} + +/*******************************************************************************/ +static ReturnCode nfcipDataRx(bool blocking) { + ReturnCode ret; + + /* Perform Rx either blocking or non-blocking */ + if (blocking) { + ret = rfalTransceiveBlockingRx(); + } else { + ret = rfalGetTransceiveStatus(); + } + + if (ret != RFAL_ERR_BUSY) { + if (gNfcip.rxRcvdLen != NULL) { + (*gNfcip.rxRcvdLen) = rfalConvBitsToBytes(*gNfcip.rxRcvdLen); + + if ((ret == RFAL_ERR_NONE) && (gNfcip.rxBuf != NULL)) { + /* Digital 1.1 16.4.1.3 - Length byte LEN SHALL have a value between 3 + * and 255 -> otherwise treat as Transmission Error * + * - Ensure that actual received and frame length + * do match, otherwise treat as Transmission error */ + if ((*gNfcip.rxRcvdLen != (uint16_t)*gNfcip.rxBuf) || + (*gNfcip.rxRcvdLen < RFAL_NFCDEP_LEN_MIN) || + (*gNfcip.rxRcvdLen > RFAL_NFCDEP_LEN_MAX)) { + return RFAL_ERR_FRAMING; + } + } + } + } + + return ret; +} + +/*******************************************************************************/ +ReturnCode rfalNfcDepListenStartActivation(const rfalNfcDepTargetParam *param, + const uint8_t *atrReq, + uint16_t atrReqLength, + rfalNfcDepListenActvParam rxParam) { + ReturnCode ret; + rfalNfcDepConfigs cfg; + + if ((param == NULL) || (atrReq == NULL) || (rxParam.rxLen == NULL)) { + return RFAL_ERR_PARAM; + } + + /*******************************************************************************/ + /* Check whether is a valid ATR_REQ Compute NFC-DEP device */ + if (!rfalNfcDepIsAtrReq(atrReq, atrReqLength, NULL)) { + return RFAL_ERR_PARAM; + } + + rxParam.nfcDepDev->activation.Initiator.ATR_REQLen = + (uint8_t)atrReqLength; /* nfcipIsAtrReq() is already checking Min and Max + buffer lengths */ + if (atrReqLength > 0U) /* MISRA 21.18 */ + { + RFAL_MEMCPY((uint8_t *)&rxParam.nfcDepDev->activation.Initiator.ATR_REQ, + atrReq, atrReqLength); + } + + rxParam.nfcDepDev->info.GBLen = + (uint8_t)(atrReqLength - RFAL_NFCDEP_ATRREQ_MIN_LEN); + rxParam.nfcDepDev->info.DID = + rxParam.nfcDepDev->activation.Initiator.ATR_REQ.DID; + rxParam.nfcDepDev->info.NAD = + RFAL_NFCDEP_NAD_NO; /* Digital 1.1 16.6.2.9 Initiator SHALL NOT use NAD + */ + rxParam.nfcDepDev->info.LR = + rfalNfcDepPP2LR(rxParam.nfcDepDev->activation.Initiator.ATR_REQ.PPi); + rxParam.nfcDepDev->info.FS = rfalNfcDepLR2FS(rxParam.nfcDepDev->info.LR); + rxParam.nfcDepDev->info.WT = 0; + rxParam.nfcDepDev->info.FWT = NFCIP_NO_FWT; + rxParam.nfcDepDev->info.dFWT = NFCIP_NO_FWT; + + rfalGetBitRate(&rxParam.nfcDepDev->info.DSI, &rxParam.nfcDepDev->info.DRI); + + /* Store Device Info location, updated upon a PSL */ + gNfcip.nfcDepDev = rxParam.nfcDepDev; + + /*******************************************************************************/ + cfg.did = rxParam.nfcDepDev->activation.Initiator.ATR_REQ.DID; + cfg.nad = RFAL_NFCDEP_NAD_NO; + + cfg.fwt = RFAL_NFCDEP_MAX_FWT; + cfg.dFwt = RFAL_NFCDEP_WT_DELTA; + + cfg.br = param->brt; + cfg.bs = param->bst; + + cfg.lr = rfalNfcDepPP2LR(param->ppt); + + cfg.gbLen = param->GBtLen; + if (cfg.gbLen > 0U) /* MISRA 21.18 */ + { + RFAL_MEMCPY(cfg.gb, param->GBt, cfg.gbLen); + } + + cfg.nfcidLen = RFAL_NFCDEP_NFCID3_LEN; + RFAL_MEMCPY(cfg.nfcid, param->nfcid3, RFAL_NFCDEP_NFCID3_LEN); + + cfg.to = param->to; + + cfg.role = RFAL_NFCDEP_ROLE_TARGET; + cfg.oper = param->operParam; + cfg.commMode = param->commMode; + + rfalNfcDepInitialize(); + nfcipConfig(&cfg); + + /*******************************************************************************/ + /* Reply with ATR RES to Initiator */ + /*******************************************************************************/ + gNfcip.rxBuf = (uint8_t *)rxParam.rxBuf; + gNfcip.rxBufLen = sizeof(rfalNfcDepBufFormat); + gNfcip.rxRcvdLen = rxParam.rxLen; + gNfcip.rxBufPaylPos = RFAL_NFCDEP_DEPREQ_HEADER_LEN; + gNfcip.isChaining = rxParam.isRxChaining; + gNfcip.txBufPaylPos = RFAL_NFCDEP_DEPREQ_HEADER_LEN; + + RFAL_EXIT_ON_ERR(ret, nfcipTx(NFCIP_CMD_ATR_RES, (uint8_t *)gNfcip.rxBuf, + NULL, 0, 0, NFCIP_NO_FWT)); + + gNfcip.state = NFCIP_ST_TARG_WAIT_ACTV; + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalNfcDepListenGetActivationStatus(void) { + ReturnCode err; + uint8_t BRS; + + BRS = RFAL_NFCDEP_BRS_MAINTAIN; + + err = nfcipTargetHandleActivation(gNfcip.nfcDepDev, &BRS); + + switch (err) { + case RFAL_ERR_NONE: + + if (BRS != RFAL_NFCDEP_BRS_MAINTAIN) { + /* DSI codes the bit rate from Initiator to Target */ + /* DRI codes the bit rate from Target to Initiator */ + + if (gNfcip.cfg.commMode == RFAL_NFCDEP_COMM_ACTIVE) { + RFAL_EXIT_ON_ERR(err, rfalSetMode(RFAL_MODE_LISTEN_ACTIVE_P2P, + gNfcip.nfcDepDev->info.DRI, + gNfcip.nfcDepDev->info.DSI)); + } else { + RFAL_EXIT_ON_ERR( + err, rfalSetMode(((RFAL_BR_106 == gNfcip.nfcDepDev->info.DRI) + ? RFAL_MODE_LISTEN_NFCA + : RFAL_MODE_LISTEN_NFCF), + gNfcip.nfcDepDev->info.DRI, + gNfcip.nfcDepDev->info.DSI)); + } + } + break; + + case RFAL_ERR_BUSY: + // do nothing + break; + + case RFAL_ERR_PROTO: + default: + // re-enable receiving of data + nfcDepReEnableRx(gNfcip.rxBuf, gNfcip.rxBufLen, gNfcip.rxRcvdLen); + break; + } + + return err; +} + +/*******************************************************************************/ +ReturnCode rfalNfcDepStartTransceive(const rfalNfcDepTxRxParam *param) { + rfalNfcDepDEPParams nfcDepParams; + + nfcDepParams.txBuf = (uint8_t *)param->txBuf; + nfcDepParams.txBufLen = param->txBufLen; + nfcDepParams.txChaining = param->isTxChaining; + nfcDepParams.txBufPaylPos = + RFAL_NFCDEP_DEPREQ_HEADER_LEN; /* position in txBuf where actual outgoing + data is located */ + nfcDepParams.did = RFAL_NFCDEP_DID_KEEP; + nfcDepParams.rxBufPaylPos = RFAL_NFCDEP_DEPREQ_HEADER_LEN; + nfcDepParams.rxBuf = (uint8_t *)param->rxBuf; + nfcDepParams.rxBufLen = sizeof(rfalNfcDepBufFormat); + nfcDepParams.fsc = param->FSx; + nfcDepParams.fwt = param->FWT; + nfcDepParams.dFwt = param->dFWT; + + gNfcip.rxRcvdLen = param->rxLen; + gNfcip.isChaining = param->isRxChaining; + + nfcipSetDEPParams(&nfcDepParams); + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalNfcDepGetTransceiveStatus(void) { + return nfcipRun(gNfcip.rxRcvdLen, gNfcip.isChaining); +} + +/*******************************************************************************/ +static void rfalNfcDepPdu2BLockParam(rfalNfcDepPduTxRxParam pduParam, + rfalNfcDepTxRxParam *blockParam, + uint16_t txPos, uint16_t rxPos) { + uint16_t maxInfLen; + + RFAL_NO_WARNING(rxPos); /* Keep this param for future use */ + + blockParam->DID = pduParam.DID; + blockParam->FSx = pduParam.FSx; + blockParam->FWT = pduParam.FWT; + blockParam->dFWT = pduParam.dFWT; + + /* Calculate max INF/Payload to be sent to other device */ + maxInfLen = + (blockParam->FSx - (RFAL_NFCDEP_HEADER + RFAL_NFCDEP_DEP_PFB_LEN)); + maxInfLen += + ((blockParam->DID != RFAL_NFCDEP_DID_NO) ? RFAL_NFCDEP_DID_LEN : 0U); + + if ((pduParam.txBufLen - txPos) > maxInfLen) { + blockParam->isTxChaining = true; + blockParam->txBufLen = maxInfLen; + } else { + blockParam->isTxChaining = false; + blockParam->txBufLen = (pduParam.txBufLen - txPos); + } + + /* TxBuf is moved to the beginning for every Block */ + blockParam->txBuf = + (rfalNfcDepBufFormat *) + pduParam.txBuf; /* PRQA S 0310 # MISRA 11.3 - Intentional safe cast + to avoiding large buffer duplication */ + blockParam->rxBuf = + pduParam.tmpBuf; /* Simply using the pdu buffer is not possible because of + current ACK handling */ + blockParam->isRxChaining = &gNfcip.isPDURxChaining; + blockParam->rxLen = pduParam.rxLen; +} + +/*******************************************************************************/ +ReturnCode rfalNfcDepStartPduTransceive(rfalNfcDepPduTxRxParam param) { + rfalNfcDepTxRxParam txRxParam; + + /* Initialize and store APDU context */ + gNfcip.PDUParam = param; + gNfcip.PDUTxPos = 0; + gNfcip.PDURxPos = 0; + + /* Convert PDU TxRxParams to Block TxRxParams */ + rfalNfcDepPdu2BLockParam(gNfcip.PDUParam, &txRxParam, gNfcip.PDUTxPos, + gNfcip.PDURxPos); + + return rfalNfcDepStartTransceive(&txRxParam); +} + +/*******************************************************************************/ +ReturnCode rfalNfcDepGetPduTransceiveStatus(void) { + ReturnCode ret; + rfalNfcDepTxRxParam txRxParam; + + ret = rfalNfcDepGetTransceiveStatus(); + switch (ret) { + /*******************************************************************************/ + case RFAL_ERR_NONE: + + /* Check if we are still doing chaining on Tx */ + if (gNfcip.isTxChaining) { + /* Add already Tx bytes */ + gNfcip.PDUTxPos += gNfcip.txBufLen; + + /* Convert APDU TxRxParams to I-Block TxRxParams */ + rfalNfcDepPdu2BLockParam(gNfcip.PDUParam, &txRxParam, gNfcip.PDUTxPos, + gNfcip.PDURxPos); + + if (txRxParam.txBufLen > 0U) /* MISRA 21.18 */ + { + /* Move next Block to beginning of APDU Tx buffer */ + RFAL_MEMCPY(gNfcip.PDUParam.txBuf->pdu, + &gNfcip.PDUParam.txBuf->pdu[gNfcip.PDUTxPos], + txRxParam.txBufLen); + } + + RFAL_EXIT_ON_ERR(ret, rfalNfcDepStartTransceive(&txRxParam)); + return RFAL_ERR_BUSY; + } + + /* PDU TxRx is done */ + /* fall through */ + + /*******************************************************************************/ + case RFAL_ERR_AGAIN: /* PRQA S 2003 # MISRA 16.3 - Intentional fall through + */ + + /* Check if no PDU transceive has been started before (data from + * rfalNfcDepListenStartActivation) */ + if (gNfcip.PDUParam.rxLen == NULL) { + /* In Listen mode first chained packet cannot be retrieved via APDU + * interface */ + if (ret == RFAL_ERR_AGAIN) { + return RFAL_ERR_NOTSUPP; + } + + /* TxRx is complete and full data is already available */ + return RFAL_ERR_NONE; + } + + if ((*gNfcip.PDUParam.rxLen) > 0U) /* MISRA 21.18 */ + { + /* Ensure that data in tmpBuf still fits into PDU buffer */ + if ((uint16_t)((uint16_t)gNfcip.PDURxPos + (*gNfcip.PDUParam.rxLen)) > + RFAL_FEATURE_NFC_DEP_PDU_MAX_LEN) { + return RFAL_ERR_NOMEM; + } + + /* Copy chained packet from tmp buffer to PDU buffer */ + RFAL_MEMCPY(&gNfcip.PDUParam.rxBuf->pdu[gNfcip.PDURxPos], + gNfcip.PDUParam.tmpBuf->inf, *gNfcip.PDUParam.rxLen); + gNfcip.PDURxPos += *gNfcip.PDUParam.rxLen; + } + + /* Update output param rxLen */ + *gNfcip.PDUParam.rxLen = gNfcip.PDURxPos; + + /* Wait for following Block or PDU TxRx is done */ + return ((ret == RFAL_ERR_AGAIN) ? RFAL_ERR_BUSY : RFAL_ERR_NONE); + + /*******************************************************************************/ + default: + /* MISRA 16.4: no empty default statement (a comment being enough) */ + break; + } + + return ret; +} + +#endif /* RFAL_FEATURE_NFC_DEP */ diff --git a/core/embed/io/nfc/rfal/source/rfal_nfca.c b/core/embed/io/nfc/rfal/source/rfal_nfca.c new file mode 100644 index 0000000000..d7dbac971e --- /dev/null +++ b/core/embed/io/nfc/rfal/source/rfal_nfca.c @@ -0,0 +1,1069 @@ + +/****************************************************************************** + * @attention + * + * COPYRIGHT 2016 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/* + * PROJECT: ST25R391x firmware + * Revision: + * LANGUAGE: ISO C99 + */ + +/*! \file rfal_nfca.c + * + * \author Gustavo Patricio + * + * \brief Provides several NFC-A convenience methods and definitions + * + * It provides a Poller (ISO14443A PCD) interface and as well as + * some NFC-A Listener (ISO14443A PICC) helpers. + * + * The definitions and helpers methods provided by this module are only + * up to ISO14443-3 layer + * + */ + +/* + ****************************************************************************** + * INCLUDES + ****************************************************************************** + */ +#include "rfal_nfca.h" +#include "rfal_utils.h" + +/* + ****************************************************************************** + * ENABLE SWITCH + ****************************************************************************** + */ + +/* Feature switch may be enabled or disabled by user at rfal_platform.h + * Default configuration (ST25R dependant) also provided at rfal_defConfig.h + * + * RFAL_FEATURE_NFCA + */ + +#if RFAL_FEATURE_NFCA + +/* + ****************************************************************************** + * GLOBAL DEFINES + ****************************************************************************** + */ + +#define RFAL_NFCA_SLP_FWT \ + rfalConvMsTo1fc(1) /*!< Check 1ms for any modulation ISO14443-3 6.4.3 */ +#define RFAL_NFCA_SLP_CMD \ + 0x50U /*!< SLP cmd (byte1) Digital 1.1 6.9.1 & Table 20 */ +#define RFAL_NFCA_SLP_BYTE2 \ + 0x00U /*!< SLP byte2 Digital 1.1 6.9.1 & Table 20 */ +#define RFAL_NFCA_SLP_CMD_POS \ + 0U /*!< SLP cmd position Digital 1.1 6.9.1 & Table 20 */ +#define RFAL_NFCA_SLP_BYTE2_POS \ + 1U /*!< SLP byte2 position Digital 1.1 6.9.1 & Table 20 */ + +#define RFAL_NFCA_SDD_CT \ + 0x88U /*!< Cascade Tag value Digital 1.1 6.7.2 */ +#define RFAL_NFCA_SDD_CT_LEN \ + 1U /*!< Cascade Tag length */ + +#define RFAL_NFCA_SLP_REQ_LEN \ + 2U /*!< SLP_REQ length */ + +#define RFAL_NFCA_SEL_CMD_LEN \ + 1U /*!< SEL_CMD length */ +#define RFAL_NFCA_SEL_PAR_LEN \ + 1U /*!< SEL_PAR length */ +#define RFAL_NFCA_SEL_SELPAR \ + rfalNfcaSelPar(7U, 0U) /*!< SEL_PAR on Select is always with 4 data/nfcid */ +#define RFAL_NFCA_BCC_LEN \ + 1U /*!< BCC length */ + +#define RFAL_NFCA_SDD_REQ_LEN \ + (RFAL_NFCA_SEL_CMD_LEN + RFAL_NFCA_SEL_PAR_LEN) /*!< SDD_REQ length */ +#define RFAL_NFCA_SDD_RES_LEN \ + (RFAL_NFCA_CASCADE_1_UID_LEN + RFAL_NFCA_BCC_LEN) /*!< SDD_RES length */ + +#define RFAL_NFCA_T_RETRANS \ + 5U /*!< t RETRANSMISSION [3, 33]ms EMVCo 2.6 A.5 */ +#define RFAL_NFCA_N_RETRANS \ + 2U /*!< Number of retries EMVCo 2.6 9.6.1.3 */ + +/*! SDD_REQ (Select) Cascade Levels */ +enum { + RFAL_NFCA_SEL_CASCADE_L1 = 0, /*!< SDD_REQ Cascade Level 1 */ + RFAL_NFCA_SEL_CASCADE_L2 = 1, /*!< SDD_REQ Cascade Level 2 */ + RFAL_NFCA_SEL_CASCADE_L3 = 2 /*!< SDD_REQ Cascade Level 3 */ +}; + +/*! SDD_REQ (Select) request Cascade Level command Digital 1.1 Table 15 */ +enum { + RFAL_NFCA_CMD_SEL_CL1 = 0x93, /*!< SDD_REQ command Cascade Level 1 */ + RFAL_NFCA_CMD_SEL_CL2 = 0x95, /*!< SDD_REQ command Cascade Level 2 */ + RFAL_NFCA_CMD_SEL_CL3 = 0x97, /*!< SDD_REQ command Cascade Level 3 */ +}; + +/* +****************************************************************************** +* GLOBAL MACROS +****************************************************************************** +*/ +#define rfalNfcaSelPar(nBy, nbi) \ + (uint8_t)((((nBy) << 4U) & 0xF0U) | \ + ((nbi) & \ + 0x0FU)) /*!< Calculates SEL_PAR with the bytes/bits to be sent */ +#define rfalNfcaCLn2SELCMD(cl) \ + (uint8_t)( \ + (uint8_t)(RFAL_NFCA_CMD_SEL_CL1) + \ + (2U * (cl))) /*!< Calculates SEL_CMD with the given cascade level */ +#define rfalNfcaNfcidLen2CL(len) \ + ((len) / 5U) /*!< Calculates cascade level by the NFCID length */ + +/* +****************************************************************************** +* GLOBAL TYPES +****************************************************************************** +*/ + +/*! Technology Detection context */ +typedef struct { + rfalComplianceMode compMode; /*!< Compliancy mode to be used */ + ReturnCode ret; /*!< Outcome of presence check */ +} rfalNfcaTechDetParams; + +/*! Colission Resolution states */ +typedef enum { + RFAL_NFCA_CR_IDLE, /*!< IDLE state */ + RFAL_NFCA_CR_CL, /*!< New Cascading Level state */ + RFAL_NFCA_CR_SDD_TX, /*!< Perform anticollsion Tx state */ + RFAL_NFCA_CR_SDD, /*!< Perform anticollsion state */ + RFAL_NFCA_CR_SEL_TX, /*!< Perform CL Selection Tx state */ + RFAL_NFCA_CR_SEL, /*!< Perform CL Selection state */ + RFAL_NFCA_CR_DONE /*!< Collision Resolution done state */ +} rfalNfcaColResState; + +/*! Full Colission Resolution states */ +typedef enum { + RFAL_NFCA_CR_FULL_START, /*!< Start Full Collision Resolution state */ + RFAL_NFCA_CR_FULL_SLPCHECK, /*!< Sleep and Check for restart state */ + RFAL_NFCA_CR_FULL_RESTART /*!< Restart Full Collision Resolution state */ +} rfalNfcaFColResState; + +/*! Colission Resolution context */ +typedef struct { + uint8_t devLimit; /*!< Device limit to be used */ + rfalComplianceMode compMode; /*!< Compliancy mode to be used */ + rfalNfcaListenDevice *nfcaDevList; /*!< Location of the device list */ + uint8_t *devCnt; /*!< Location of the device counter */ + bool collPending; /*!< Collision pending flag */ + + bool *collPend; /*!< Location of collision pending flag (Single CR) */ + rfalNfcaSelReq selReq; /*!< SelReqused during anticollision (Single CR) */ + rfalNfcaSelRes + *selRes; /*!< Location to place of the SEL_RES(SAK) (Single CR) */ + uint8_t *nfcId1; /*!< Location to place the NFCID1 (Single CR) */ + uint8_t *nfcId1Len; /*!< Location to place the NFCID1 length (Single CR) */ + uint8_t cascadeLv; /*!< Current Cascading Level (Single CR) */ + rfalNfcaColResState + state; /*!< Single Collision Resolution state (Single CR) */ + rfalNfcaFColResState fState; /*!< Full Collision Resolution state (Full CR) */ + uint8_t + bytesTxRx; /*!< TxRx bytes used during anticollision loop (Single CR) */ + uint8_t bitsTxRx; /*!< TxRx bits used during anticollision loop (Single CR) */ + uint16_t rxLen; /*!< Local reception length */ + uint32_t tmrFDT; /*!< FDT timer used between SED_REQs (Single CR) */ + uint8_t + retries; /*!< Retries to be performed upon a timeout error (Single CR)*/ + uint8_t backtrackCnt; /*!< Backtrack retries (Single CR) */ + bool doBacktrack; /*!< Backtrack flag (Single CR) */ +} rfalNfcaColResParams; + +/*! Colission Resolution context */ +typedef struct { + uint8_t cascadeLv; /*!< Current Cascading Level */ + uint8_t fCascadeLv; /*!< Final Cascading Level */ + rfalNfcaSelRes *selRes; /*!< Location to place of the SEL_RES(SAK) */ + uint16_t rxLen; /*!< Local reception length */ + const uint8_t *nfcid1; /*!< Location of the NFCID to be selected */ + uint8_t nfcidOffset; /*!< Selected NFCID offset */ + bool isRx; /*!< Selection is in reception state */ +} rfalNfcaSelParams; + +/*! SLP_REQ (HLTA) format Digital 1.1 6.9.1 & Table 20 */ +typedef struct { + uint8_t frame[RFAL_NFCA_SLP_REQ_LEN]; /*!< SLP: 0x50 0x00 */ +} rfalNfcaSlpReq; + +/*! RFAL NFC-A instance */ +typedef struct { + rfalNfcaTechDetParams DT; /*!< Technology Detection context */ + rfalNfcaColResParams CR; /*!< Collision Resolution context */ + rfalNfcaSelParams SEL; /*!< Selection|Activation context */ + + rfalNfcaSlpReq slpReq; /*!< SLP_REx buffer */ +} rfalNfca; + +/* +****************************************************************************** +* LOCAL VARIABLES +****************************************************************************** +*/ +static rfalNfca gNfca; /*!< RFAL NFC-A instance */ + +/* +****************************************************************************** +* LOCAL FUNCTION PROTOTYPES +****************************************************************************** +*/ +static uint8_t rfalNfcaCalculateBcc(const uint8_t *buf, uint8_t bufLen); +static ReturnCode rfalNfcaPollerStartSingleCollisionResolution( + uint8_t devLimit, bool *collPending, rfalNfcaSelRes *selRes, + uint8_t *nfcId1, uint8_t *nfcId1Len); +static ReturnCode rfalNfcaPollerGetSingleCollisionResolutionStatus(void); + +/* + ****************************************************************************** + * LOCAL FUNCTIONS + ****************************************************************************** + */ + +static uint8_t rfalNfcaCalculateBcc(const uint8_t *buf, uint8_t bufLen) { + uint8_t i; + uint8_t BCC; + + BCC = 0; + + /* BCC is XOR over first 4 bytes of the SDD_RES Digital 1.1 6.7.2 */ + for (i = 0; i < bufLen; i++) { + BCC ^= buf[i]; + } + + return BCC; +} + +/*******************************************************************************/ +static ReturnCode rfalNfcaPollerStartSingleCollisionResolution( + uint8_t devLimit, bool *collPending, rfalNfcaSelRes *selRes, + uint8_t *nfcId1, uint8_t *nfcId1Len) { + /* Check parameters */ + if ((collPending == NULL) || (selRes == NULL) || (nfcId1 == NULL) || + (nfcId1Len == NULL)) { + return RFAL_ERR_PARAM; + } + + /* Initialize output parameters */ + *collPending = false; /* Activity 1.1 9.3.4.6 */ + *nfcId1Len = 0; + RFAL_MEMSET(nfcId1, 0x00, RFAL_NFCA_CASCADE_3_UID_LEN); + + /* Save parameters */ + gNfca.CR.devLimit = devLimit; + gNfca.CR.collPend = collPending; + gNfca.CR.selRes = selRes; + gNfca.CR.nfcId1 = nfcId1; + gNfca.CR.nfcId1Len = nfcId1Len; + + platformTimerDestroy(gNfca.CR.tmrFDT); + gNfca.CR.tmrFDT = RFAL_TIMING_NONE; + gNfca.CR.retries = RFAL_NFCA_N_RETRANS; + gNfca.CR.cascadeLv = (uint8_t)RFAL_NFCA_SEL_CASCADE_L1; + gNfca.CR.state = RFAL_NFCA_CR_CL; + + gNfca.CR.doBacktrack = false; + gNfca.CR.backtrackCnt = 3U; + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +static ReturnCode rfalNfcaPollerGetSingleCollisionResolutionStatus(void) { + ReturnCode ret; + uint8_t collBit = + 1U; /* standards mandate or recommend collision bit to be set to One. */ + + /* Check if FDT timer is still running */ + if (gNfca.CR.tmrFDT != RFAL_TIMING_NONE) { + if ((!platformTimerIsExpired(gNfca.CR.tmrFDT))) { + return RFAL_ERR_BUSY; + } + } + + /*******************************************************************************/ + /* Go through all Cascade Levels Activity 1.1 9.3.4 */ + if (gNfca.CR.cascadeLv > (uint8_t)RFAL_NFCA_SEL_CASCADE_L3) { + return RFAL_ERR_INTERNAL; + } + + switch (gNfca.CR.state) { + /*******************************************************************************/ + case RFAL_NFCA_CR_CL: + + /* Initialize the SDD_REQ to send for the new cascade level */ + RFAL_MEMSET((uint8_t *)&gNfca.CR.selReq, 0x00, sizeof(rfalNfcaSelReq)); + + gNfca.CR.bytesTxRx = RFAL_NFCA_SDD_REQ_LEN; + gNfca.CR.bitsTxRx = 0U; + gNfca.CR.state = RFAL_NFCA_CR_SDD_TX; + + /* fall through */ + + /*******************************************************************************/ + case RFAL_NFCA_CR_SDD_TX: /* PRQA S 2003 # MISRA 16.3 - Intentional fall + through */ + + /* Calculate SEL_CMD and SEL_PAR with the bytes/bits to be sent */ + gNfca.CR.selReq.selCmd = rfalNfcaCLn2SELCMD(gNfca.CR.cascadeLv); + gNfca.CR.selReq.selPar = + rfalNfcaSelPar(gNfca.CR.bytesTxRx, gNfca.CR.bitsTxRx); + + /* Send SDD_REQ (Anticollision frame) */ + rfalISO14443AStartTransceiveAnticollisionFrame( + (uint8_t *)&gNfca.CR.selReq, &gNfca.CR.bytesTxRx, &gNfca.CR.bitsTxRx, + &gNfca.CR.rxLen, RFAL_NFCA_FDTMIN); + + gNfca.CR.state = RFAL_NFCA_CR_SDD; + break; + + /*******************************************************************************/ + case RFAL_NFCA_CR_SDD: + + RFAL_EXIT_ON_BUSY(ret, + rfalISO14443AGetTransceiveAnticollisionFrameStatus()); + + /* Retry upon timeout EMVCo 2.6 9.6.1.3 */ + if ((ret == RFAL_ERR_TIMEOUT) && (gNfca.CR.devLimit == 0U) && + (gNfca.CR.retries != 0U)) { + gNfca.CR.retries--; + platformTimerDestroy(gNfca.CR.tmrFDT); + gNfca.CR.tmrFDT = platformTimerCreate(RFAL_NFCA_T_RETRANS); + + gNfca.CR.state = RFAL_NFCA_CR_SDD_TX; + break; + } + + /* Covert rxLen into bytes */ + gNfca.CR.rxLen = rfalConvBitsToBytes(gNfca.CR.rxLen); + + if ((ret == RFAL_ERR_TIMEOUT) && (gNfca.CR.backtrackCnt != 0U) && + (!gNfca.CR.doBacktrack) && + (!((RFAL_NFCA_SDD_REQ_LEN == gNfca.CR.bytesTxRx) && + (0U == gNfca.CR.bitsTxRx)))) { + /* In multiple card scenarios it may always happen that some + * collisions of a weaker tag go unnoticed. If then a later + * collision is recognized and the strong tag has a 0 at the + * collision position then no tag will respond. Catch this + * corner case and then try with the bit being sent as zero. */ + rfalNfcaSensRes sensRes; + ret = RFAL_ERR_RF_COLLISION; + rfalNfcaPollerCheckPresence(RFAL_14443A_SHORTFRAME_CMD_REQA, &sensRes); + /* Algorithm below does a post-increment, decrement to go back to + * current position */ + if (0U == gNfca.CR.bitsTxRx) { + gNfca.CR.bitsTxRx = 7; + gNfca.CR.bytesTxRx--; + } else { + gNfca.CR.bitsTxRx--; + } + collBit = (uint8_t)(((uint8_t *)&gNfca.CR.selReq)[gNfca.CR.bytesTxRx] & + (1U << gNfca.CR.bitsTxRx)); + collBit = + (uint8_t)((0U == collBit) ? 1U : 0U); /* Invert the collision bit */ + gNfca.CR.doBacktrack = true; + gNfca.CR.backtrackCnt--; + } else { + gNfca.CR.doBacktrack = false; + } + + if (ret == RFAL_ERR_RF_COLLISION) { + /* Check received length */ + if ((gNfca.CR.bytesTxRx + ((gNfca.CR.bitsTxRx != 0U) ? 1U : 0U)) > + (RFAL_NFCA_SDD_RES_LEN + RFAL_NFCA_SDD_REQ_LEN)) { + return RFAL_ERR_PROTO; + } + + /* Collision in BCC: Anticollide only UID part */ + if (((gNfca.CR.bytesTxRx + ((gNfca.CR.bitsTxRx != 0U) ? 1U : 0U)) > + (RFAL_NFCA_CASCADE_1_UID_LEN + RFAL_NFCA_SDD_REQ_LEN)) && + (gNfca.CR.backtrackCnt != 0U)) { + gNfca.CR.backtrackCnt--; + gNfca.CR.bytesTxRx = + (RFAL_NFCA_CASCADE_1_UID_LEN + RFAL_NFCA_SDD_REQ_LEN) - 1U; + gNfca.CR.bitsTxRx = 7; + collBit = + (uint8_t)(((uint8_t *)&gNfca.CR.selReq)[gNfca.CR.bytesTxRx] & + (1U + << gNfca.CR.bitsTxRx)); /* Not a real collision, + extract the actual bit for + the subsequent code */ + } + + /* Activity 1.0 & 1.1 9.3.4.12: If CON_DEVICES_LIMIT has a value of 0, + * then NFC Forum Device is configured to perform collision detection + * only */ + if ((gNfca.CR.devLimit == 0U) && (!(*gNfca.CR.collPend))) { + *gNfca.CR.collPend = true; + return RFAL_ERR_IGNORE; + } + + *gNfca.CR.collPend = true; + + /* Set and select the collision bit, with the number of bytes/bits + * successfully TxRx */ + if (collBit != 0U) { + ((uint8_t *)&gNfca.CR.selReq)[gNfca.CR.bytesTxRx] = + (uint8_t)(((uint8_t *)&gNfca.CR.selReq)[gNfca.CR.bytesTxRx] | + (1U << gNfca.CR.bitsTxRx)); /* MISRA 10.3 */ + } else { + ((uint8_t *)&gNfca.CR.selReq)[gNfca.CR.bytesTxRx] = + (uint8_t)(((uint8_t *)&gNfca.CR.selReq)[gNfca.CR.bytesTxRx] & + ~(1U << gNfca.CR.bitsTxRx)); /* MISRA 10.3 */ + } + + gNfca.CR.bitsTxRx++; + + /* Check if number of bits form a byte */ + if (gNfca.CR.bitsTxRx == RFAL_BITS_IN_BYTE) { + gNfca.CR.bitsTxRx = 0; + gNfca.CR.bytesTxRx++; + } + + gNfca.CR.state = RFAL_NFCA_CR_SDD_TX; + break; + } + + /*******************************************************************************/ + /* Check if Collision loop has failed */ + if (ret != RFAL_ERR_NONE) { + return ret; + } + + /* If collisions are to be reported check whether the response is complete + */ + if ((gNfca.CR.devLimit == 0U) && + (gNfca.CR.rxLen != sizeof(rfalNfcaSddRes))) { + return RFAL_ERR_PROTO; + } + + /* Check if the received BCC match */ + if (gNfca.CR.selReq.bcc != + rfalNfcaCalculateBcc(gNfca.CR.selReq.nfcid1, + RFAL_NFCA_CASCADE_1_UID_LEN)) { + return RFAL_ERR_PROTO; + } + + /*******************************************************************************/ + /* Anticollision OK, Select this Cascade Level */ + gNfca.CR.selReq.selPar = RFAL_NFCA_SEL_SELPAR; + + gNfca.CR.retries = RFAL_NFCA_N_RETRANS; + gNfca.CR.state = RFAL_NFCA_CR_SEL_TX; + break; + + /*******************************************************************************/ + case RFAL_NFCA_CR_SEL_TX: + + /* Send SEL_REQ (Select command) - Retry upon timeout EMVCo 2.6 9.6.1.3 + */ + rfalTransceiveBlockingTx( + (uint8_t *)&gNfca.CR.selReq, sizeof(rfalNfcaSelReq), + (uint8_t *)gNfca.CR.selRes, sizeof(rfalNfcaSelRes), &gNfca.CR.rxLen, + RFAL_TXRX_FLAGS_DEFAULT, RFAL_NFCA_FDTMIN); + gNfca.CR.state = RFAL_NFCA_CR_SEL; + break; + + /*******************************************************************************/ + case RFAL_NFCA_CR_SEL: + + RFAL_EXIT_ON_BUSY(ret, rfalGetTransceiveStatus()); + + /* Retry upon timeout EMVCo 2.6 9.6.1.3 */ + if ((ret == RFAL_ERR_TIMEOUT) && (gNfca.CR.devLimit == 0U) && + (gNfca.CR.retries != 0U)) { + gNfca.CR.retries--; + platformTimerDestroy(gNfca.CR.tmrFDT); + gNfca.CR.tmrFDT = platformTimerCreate(RFAL_NFCA_T_RETRANS); + + gNfca.CR.state = RFAL_NFCA_CR_SEL_TX; + break; + } + + if (ret != RFAL_ERR_NONE) { + return ret; + } + + gNfca.CR.rxLen = rfalConvBitsToBytes(gNfca.CR.rxLen); + + /* Ensure proper response length */ + if (gNfca.CR.rxLen != sizeof(rfalNfcaSelRes)) { + return RFAL_ERR_PROTO; + } + + /*******************************************************************************/ + /* Check cascade byte, if cascade tag then go next cascade level */ + if (*gNfca.CR.selReq.nfcid1 == RFAL_NFCA_SDD_CT) { + /* Cascade Tag present, store nfcid1 bytes (excluding cascade tag) and + * continue for next CL */ + RFAL_MEMCPY(&gNfca.CR.nfcId1[*gNfca.CR.nfcId1Len], + &((uint8_t *)&gNfca.CR.selReq.nfcid1)[RFAL_NFCA_SDD_CT_LEN], + (RFAL_NFCA_CASCADE_1_UID_LEN - RFAL_NFCA_SDD_CT_LEN)); + *gNfca.CR.nfcId1Len += + (RFAL_NFCA_CASCADE_1_UID_LEN - RFAL_NFCA_SDD_CT_LEN); + + /* Go to next cascade level */ + gNfca.CR.state = RFAL_NFCA_CR_CL; + gNfca.CR.cascadeLv++; + } else { + /* UID Selection complete, Stop Cascade Level loop */ + RFAL_MEMCPY(&gNfca.CR.nfcId1[*gNfca.CR.nfcId1Len], + (uint8_t *)&gNfca.CR.selReq.nfcid1, + RFAL_NFCA_CASCADE_1_UID_LEN); + *gNfca.CR.nfcId1Len += RFAL_NFCA_CASCADE_1_UID_LEN; + + gNfca.CR.state = RFAL_NFCA_CR_DONE; + break; /* Only flag operation complete on the next execution */ + } + break; + + /*******************************************************************************/ + case RFAL_NFCA_CR_DONE: + return RFAL_ERR_NONE; + + /*******************************************************************************/ + default: + return RFAL_ERR_WRONG_STATE; + } + return RFAL_ERR_BUSY; +} + +/* +****************************************************************************** +* GLOBAL FUNCTIONS +****************************************************************************** +*/ + +/*******************************************************************************/ +ReturnCode rfalNfcaPollerInitialize(void) { + ReturnCode ret; + + RFAL_EXIT_ON_ERR(ret, + rfalSetMode(RFAL_MODE_POLL_NFCA, RFAL_BR_106, RFAL_BR_106)); + rfalSetErrorHandling(RFAL_ERRORHANDLING_NONE); + + rfalSetGT(RFAL_GT_NFCA); + rfalSetFDTListen(RFAL_FDT_LISTEN_NFCA_POLLER); + rfalSetFDTPoll(RFAL_FDT_POLL_NFCA_POLLER); + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalNfcaPollerCheckPresence(rfal14443AShortFrameCmd cmd, + rfalNfcaSensRes *sensRes) { + ReturnCode ret; + uint16_t rcvLen; + + /* Digital 1.1 6.10.1.3 For Commands ALL_REQ, SENS_REQ, SDD_REQ, and SEL_REQ, + * the NFC Forum Device * MUST treat receipt of a Listen Frame at a time + * after FDT(Listen, min) as a Timeour Error */ + + ret = rfalISO14443ATransceiveShortFrame( + cmd, (uint8_t *)sensRes, + (uint8_t)rfalConvBytesToBits(sizeof(rfalNfcaSensRes)), &rcvLen, + RFAL_NFCA_FDTMIN); + if ((ret == RFAL_ERR_RF_COLLISION) || (ret == RFAL_ERR_CRC) || + (ret == RFAL_ERR_NOMEM) || (ret == RFAL_ERR_FRAMING) || + (ret == RFAL_ERR_PAR) || (ret == RFAL_ERR_INCOMPLETE_BYTE)) { + ret = RFAL_ERR_NONE; + } + + return ret; +} + +/*******************************************************************************/ +ReturnCode rfalNfcaPollerTechnologyDetection(rfalComplianceMode compMode, + rfalNfcaSensRes *sensRes) { + ReturnCode ret; + + RFAL_EXIT_ON_ERR(ret, + rfalNfcaPollerStartTechnologyDetection(compMode, sensRes)); + rfalRunBlocking(ret, rfalNfcaPollerGetTechnologyDetectionStatus()); + + return ret; +} + +/*******************************************************************************/ +ReturnCode rfalNfcaPollerStartTechnologyDetection(rfalComplianceMode compMode, + rfalNfcaSensRes *sensRes) { + ReturnCode ret; + + gNfca.DT.compMode = compMode; + gNfca.DT.ret = + rfalNfcaPollerCheckPresence(((compMode == RFAL_COMPLIANCE_MODE_EMV) + ? RFAL_14443A_SHORTFRAME_CMD_WUPA + : RFAL_14443A_SHORTFRAME_CMD_REQA), + sensRes); + + /* Send SLP_REQ as Activity 1.1 9.2.3.6 and EMVCo 2.6 9.2.1.3 */ + if ((gNfca.DT.compMode != RFAL_COMPLIANCE_MODE_ISO) && + (gNfca.DT.ret == RFAL_ERR_NONE)) { + RFAL_EXIT_ON_ERR(ret, rfalNfcaPollerStartSleep()); + } + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalNfcaPollerGetTechnologyDetectionStatus(void) { + ReturnCode ret; + + /* If Sleep was sent, wait until its termination */ + if ((gNfca.DT.compMode != RFAL_COMPLIANCE_MODE_ISO) && + (gNfca.DT.ret == RFAL_ERR_NONE)) { + RFAL_EXIT_ON_BUSY(ret, rfalNfcaPollerGetSleepStatus()); + } + + return gNfca.DT.ret; +} + +/*******************************************************************************/ +ReturnCode rfalNfcaPollerSingleCollisionResolution(uint8_t devLimit, + bool *collPending, + rfalNfcaSelRes *selRes, + uint8_t *nfcId1, + uint8_t *nfcId1Len) { + ReturnCode ret; + + RFAL_EXIT_ON_ERR(ret, rfalNfcaPollerStartSingleCollisionResolution( + devLimit, collPending, selRes, nfcId1, nfcId1Len)); + rfalRunBlocking(ret, rfalNfcaPollerGetSingleCollisionResolutionStatus()); + + return ret; +} + +/*******************************************************************************/ +ReturnCode rfalNfcaPollerStartFullCollisionResolution( + rfalComplianceMode compMode, uint8_t devLimit, + rfalNfcaListenDevice *nfcaDevList, uint8_t *devCnt) { + ReturnCode ret; + rfalNfcaSensRes sensRes; + uint16_t rcvLen; + + if ((nfcaDevList == NULL) || (devCnt == NULL)) { + return RFAL_ERR_PARAM; + } + + *devCnt = 0; + ret = RFAL_ERR_NONE; + + /*******************************************************************************/ + /* Send ALL_REQ before Anticollision if a Sleep was sent before + * Activity 1.1 9.3.4.1 and EMVco 2.6 9.3.2.1 */ + if (compMode != RFAL_COMPLIANCE_MODE_ISO) { + ret = rfalISO14443ATransceiveShortFrame( + RFAL_14443A_SHORTFRAME_CMD_WUPA, (uint8_t *)&nfcaDevList->sensRes, + (uint8_t)rfalConvBytesToBits(sizeof(rfalNfcaSensRes)), &rcvLen, + RFAL_NFCA_FDTMIN); + if (ret != RFAL_ERR_NONE) { + if ((compMode == RFAL_COMPLIANCE_MODE_EMV) || + ((ret != RFAL_ERR_RF_COLLISION) && (ret != RFAL_ERR_CRC) && + (ret != RFAL_ERR_FRAMING) && (ret != RFAL_ERR_PAR) && + (ret != RFAL_ERR_INCOMPLETE_BYTE))) { + return ret; + } + } + + /* Check proper SENS_RES/ATQA size */ + if ((ret == RFAL_ERR_NONE) && + (rfalConvBytesToBits(sizeof(rfalNfcaSensRes)) != rcvLen)) { + return RFAL_ERR_PROTO; + } + } + + /*******************************************************************************/ + /* Store the SENS_RES from Technology Detection or from WUPA */ + sensRes = nfcaDevList->sensRes; + + if (devLimit > 0U) /* MISRA 21.18 */ + { + RFAL_MEMSET(nfcaDevList, 0x00, (sizeof(rfalNfcaListenDevice) * devLimit)); + } + + /* Restore the prev SENS_RES, assuming that the SENS_RES received is from + * first device When only one device is detected it's not woken up then we'll + * have no SENS_RES (ATQA) */ + nfcaDevList->sensRes = sensRes; + + /* Save parameters */ + gNfca.CR.devCnt = devCnt; + gNfca.CR.devLimit = devLimit; + gNfca.CR.nfcaDevList = nfcaDevList; + gNfca.CR.compMode = compMode; + gNfca.CR.fState = RFAL_NFCA_CR_FULL_START; + +#if RFAL_FEATURE_T1T + /*******************************************************************************/ + /* Only check for T1T if previous SENS_RES was received without a transmission + * * error. When collisions occur bits in the SENS_RES may look like a T1T */ + /* If T1T Anticollision is not supported Activity 1.1 9.3.4.3 */ + if (rfalNfcaIsSensResT1T(&nfcaDevList->sensRes) && (devLimit != 0U) && + (ret == RFAL_ERR_NONE) && (compMode != RFAL_COMPLIANCE_MODE_EMV)) { + /* RID_REQ shall be performed Activity 1.1 9.3.4.24 */ + rfalT1TPollerInitialize(); + RFAL_EXIT_ON_ERR(ret, rfalT1TPollerRid(&nfcaDevList->ridRes)); + + *devCnt = 1U; + nfcaDevList->isSleep = false; + nfcaDevList->type = RFAL_NFCA_T1T; + nfcaDevList->nfcId1Len = RFAL_NFCA_CASCADE_1_UID_LEN; + RFAL_MEMCPY(&nfcaDevList->nfcId1, &nfcaDevList->ridRes.uid, + RFAL_NFCA_CASCADE_1_UID_LEN); + + return RFAL_ERR_NONE; + } +#endif /* RFAL_FEATURE_T1T */ + + RFAL_EXIT_ON_ERR( + ret, rfalNfcaPollerStartSingleCollisionResolution( + devLimit, &gNfca.CR.collPending, &nfcaDevList->selRes, + (uint8_t *)&nfcaDevList->nfcId1, &nfcaDevList->nfcId1Len)); + + gNfca.CR.fState = RFAL_NFCA_CR_FULL_START; + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalNfcaPollerGetFullCollisionResolutionStatus(void) { + ReturnCode ret; + uint8_t newDevType; + + if ((gNfca.CR.nfcaDevList == NULL) || (gNfca.CR.devCnt == NULL)) { + return RFAL_ERR_WRONG_STATE; + } + + switch (gNfca.CR.fState) { + /*******************************************************************************/ + case RFAL_NFCA_CR_FULL_START: + + /*******************************************************************************/ + /* Check whether a T1T has already been detected */ + if (rfalNfcaIsSensResT1T(&gNfca.CR.nfcaDevList->sensRes) && + (gNfca.CR.nfcaDevList->type == RFAL_NFCA_T1T)) { + /* T1T doesn't support Anticollision */ + return RFAL_ERR_NONE; + } + + /* fall through */ + + /*******************************************************************************/ + case RFAL_NFCA_CR_FULL_RESTART: /* PRQA S 2003 # MISRA 16.3 - Intentional + fall through */ + + /*******************************************************************************/ + RFAL_EXIT_ON_ERR(ret, rfalNfcaPollerGetSingleCollisionResolutionStatus()); + + /* Assign Listen Device */ + newDevType = + ((uint8_t)gNfca.CR.nfcaDevList[*gNfca.CR.devCnt].selRes.sak) & + RFAL_NFCA_SEL_RES_CONF_MASK; /* MISRA 10.8 */ + /* PRQA S 4342 1 # MISRA 10.5 - Guaranteed that no invalid enum values are + * created: see guard_eq_RFAL_NFCA_T2T, .... */ + gNfca.CR.nfcaDevList[*gNfca.CR.devCnt].type = + (rfalNfcaListenDeviceType)newDevType; + gNfca.CR.nfcaDevList[*gNfca.CR.devCnt].isSleep = false; + (*gNfca.CR.devCnt)++; + + /* If a collision was detected and device counter is lower than limit + * Activity 1.1 9.3.4.21 */ + if ((*gNfca.CR.devCnt < gNfca.CR.devLimit) && (gNfca.CR.collPending)) { + /* Put this device to Sleep Activity 1.1 9.3.4.22 */ + RFAL_EXIT_ON_ERR(ret, rfalNfcaPollerStartSleep()); + gNfca.CR.nfcaDevList[(*gNfca.CR.devCnt - 1U)].isSleep = true; + + gNfca.CR.fState = RFAL_NFCA_CR_FULL_SLPCHECK; + return RFAL_ERR_BUSY; + } else { + /* Exit loop */ + gNfca.CR.collPending = false; + } + break; + + /*******************************************************************************/ + case RFAL_NFCA_CR_FULL_SLPCHECK: + + RFAL_EXIT_ON_BUSY(ret, rfalNfcaPollerGetSleepStatus()); + + /* Send a new SENS_REQ to check for other cards Activity 1.1 9.3.4.23 */ + ret = rfalNfcaPollerCheckPresence( + RFAL_14443A_SHORTFRAME_CMD_REQA, + &gNfca.CR.nfcaDevList[*gNfca.CR.devCnt].sensRes); + if (ret == RFAL_ERR_TIMEOUT) { + /* No more devices found, exit */ + gNfca.CR.collPending = false; + } else { + /* Another device found, restart|continue loop */ + gNfca.CR.collPending = true; + + /*******************************************************************************/ + /* Check if collision resolution shall continue */ + if ((*gNfca.CR.devCnt < gNfca.CR.devLimit) && (gNfca.CR.collPending)) { + RFAL_EXIT_ON_ERR( + ret, + rfalNfcaPollerStartSingleCollisionResolution( + gNfca.CR.devLimit, &gNfca.CR.collPending, + &gNfca.CR.nfcaDevList[*gNfca.CR.devCnt].selRes, + (uint8_t *)&gNfca.CR.nfcaDevList[*gNfca.CR.devCnt].nfcId1, + &gNfca.CR.nfcaDevList[*gNfca.CR.devCnt].nfcId1Len)); + + gNfca.CR.fState = RFAL_NFCA_CR_FULL_RESTART; + return RFAL_ERR_BUSY; + } + } + break; + + /*******************************************************************************/ + default: + return RFAL_ERR_WRONG_STATE; + } + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalNfcaPollerFullCollisionResolution( + rfalComplianceMode compMode, uint8_t devLimit, + rfalNfcaListenDevice *nfcaDevList, uint8_t *devCnt) { + ReturnCode ret; + + RFAL_EXIT_ON_ERR(ret, rfalNfcaPollerStartFullCollisionResolution( + compMode, devLimit, nfcaDevList, devCnt)); + rfalRunBlocking(ret, rfalNfcaPollerGetFullCollisionResolutionStatus()); + + return ret; +} + +ReturnCode rfalNfcaPollerSleepFullCollisionResolution( + uint8_t devLimit, rfalNfcaListenDevice *nfcaDevList, uint8_t *devCnt) { + bool firstRound; + uint8_t tmpDevCnt; + ReturnCode ret; + + if ((nfcaDevList == NULL) || (devCnt == NULL)) { + return RFAL_ERR_PARAM; + } + + /* Only use ALL_REQ (WUPA) on the first round */ + firstRound = true; + *devCnt = 0; + + /* Perform collision resolution until no new device is found */ + do { + tmpDevCnt = 0; + ret = rfalNfcaPollerFullCollisionResolution( + (firstRound ? RFAL_COMPLIANCE_MODE_NFC : RFAL_COMPLIANCE_MODE_ISO), + (devLimit - *devCnt), &nfcaDevList[*devCnt], &tmpDevCnt); + + if ((ret == RFAL_ERR_NONE) && (tmpDevCnt > 0U)) { + *devCnt += tmpDevCnt; + + /* Check whether to seacrh for more devices */ + if (*devCnt < devLimit) { + /* Set last found device to sleep (all others are slept already) */ + rfalNfcaPollerSleep(); + nfcaDevList[((*devCnt) - 1U)].isSleep = true; + + /* Check if any other device is present */ + ret = rfalNfcaPollerCheckPresence(RFAL_14443A_SHORTFRAME_CMD_REQA, + &nfcaDevList[*devCnt].sensRes); + if (ret == RFAL_ERR_NONE) { + firstRound = false; + continue; + } + } + } + break; + } while (true); + + return ((*devCnt > 0U) ? RFAL_ERR_NONE : ret); +} + +/*******************************************************************************/ +ReturnCode rfalNfcaPollerSelect(const uint8_t *nfcid1, uint8_t nfcidLen, + rfalNfcaSelRes *selRes) { + ReturnCode ret; + + RFAL_EXIT_ON_ERR(ret, rfalNfcaPollerStartSelect(nfcid1, nfcidLen, selRes)); + rfalRunBlocking(ret, rfalNfcaPollerGetSelectStatus()); + + return ret; +} + +/*******************************************************************************/ +ReturnCode rfalNfcaPollerStartSelect(const uint8_t *nfcid1, uint8_t nfcidLen, + rfalNfcaSelRes *selRes) { + if ((nfcid1 == NULL) || (nfcidLen > RFAL_NFCA_CASCADE_3_UID_LEN) || + (selRes == NULL)) { + return RFAL_ERR_PARAM; + } + + /* Calculate Cascate Level */ + gNfca.SEL.fCascadeLv = rfalNfcaNfcidLen2CL(nfcidLen); + gNfca.SEL.cascadeLv = RFAL_NFCA_SEL_CASCADE_L1; + + gNfca.SEL.nfcidOffset = 0; + gNfca.SEL.isRx = false; + gNfca.SEL.selRes = selRes; + gNfca.SEL.nfcid1 = nfcid1; + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalNfcaPollerGetSelectStatus(void) { + ReturnCode ret; + rfalNfcaSelReq selReq; + + if ((!gNfca.SEL.isRx)) { + /*******************************************************************************/ + /* Go through all Cascade Levels Activity 1.1 9.4.4 */ + if (gNfca.SEL.cascadeLv <= gNfca.SEL.fCascadeLv) { + /* Assign SEL_CMD according to the CLn and SEL_PAR*/ + selReq.selCmd = rfalNfcaCLn2SELCMD(gNfca.SEL.cascadeLv); + selReq.selPar = RFAL_NFCA_SEL_SELPAR; + + /* Compute NFCID/Data on the SEL_REQ command Digital 1.1 Table 18 */ + if (gNfca.SEL.fCascadeLv != gNfca.SEL.cascadeLv) { + *selReq.nfcid1 = RFAL_NFCA_SDD_CT; + RFAL_MEMCPY(&selReq.nfcid1[RFAL_NFCA_SDD_CT_LEN], + &gNfca.SEL.nfcid1[gNfca.SEL.nfcidOffset], + (RFAL_NFCA_CASCADE_1_UID_LEN - RFAL_NFCA_SDD_CT_LEN)); + gNfca.SEL.nfcidOffset += + (RFAL_NFCA_CASCADE_1_UID_LEN - RFAL_NFCA_SDD_CT_LEN); + } else { + RFAL_MEMCPY(selReq.nfcid1, &gNfca.SEL.nfcid1[gNfca.SEL.nfcidOffset], + RFAL_NFCA_CASCADE_1_UID_LEN); + } + + /* Calculate nfcid's BCC */ + selReq.bcc = rfalNfcaCalculateBcc((uint8_t *)&selReq.nfcid1, + sizeof(selReq.nfcid1)); + + /*******************************************************************************/ + /* Send SEL_REQ */ + RFAL_EXIT_ON_ERR(ret, rfalTransceiveBlockingTx( + (uint8_t *)&selReq, sizeof(rfalNfcaSelReq), + (uint8_t *)gNfca.SEL.selRes, + sizeof(rfalNfcaSelRes), &gNfca.SEL.rxLen, + RFAL_TXRX_FLAGS_DEFAULT, RFAL_NFCA_FDTMIN)); + + /* Wait for Rx to conclude */ + gNfca.SEL.isRx = true; + + return RFAL_ERR_BUSY; + } + } else { + RFAL_EXIT_ON_BUSY(ret, rfalGetTransceiveStatus()); + + /* Ensure proper response length */ + if (rfalConvBitsToBytes(gNfca.SEL.rxLen) != sizeof(rfalNfcaSelRes)) { + return RFAL_ERR_PROTO; + } + + /* Check if there are more level(s) to be selected */ + if (gNfca.SEL.cascadeLv < gNfca.SEL.fCascadeLv) { + /* Advance to the next cascade lavel */ + gNfca.SEL.cascadeLv++; + gNfca.SEL.isRx = false; + + return RFAL_ERR_BUSY; + } + } + + /* REMARK: Could check if NFCID1 is complete */ + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalNfcaPollerSleep(void) { + ReturnCode ret; + + RFAL_EXIT_ON_ERR(ret, rfalNfcaPollerStartSleep()); + rfalRunBlocking(ret, rfalNfcaPollerGetSleepStatus()); + + return ret; +} + +/*******************************************************************************/ +ReturnCode rfalNfcaPollerStartSleep(void) { + rfalTransceiveContext ctx; + + gNfca.slpReq.frame[RFAL_NFCA_SLP_CMD_POS] = RFAL_NFCA_SLP_CMD; + gNfca.slpReq.frame[RFAL_NFCA_SLP_BYTE2_POS] = RFAL_NFCA_SLP_BYTE2; + + rfalCreateByteFlagsTxRxContext( + ctx, (uint8_t *)&gNfca.slpReq, sizeof(rfalNfcaSlpReq), + (uint8_t *)&gNfca.slpReq, sizeof(gNfca.slpReq), NULL, + RFAL_TXRX_FLAGS_DEFAULT, RFAL_NFCA_SLP_FWT); + return rfalStartTransceive(&ctx); +} + +/*******************************************************************************/ +ReturnCode rfalNfcaPollerGetSleepStatus(void) { + ReturnCode ret; + + /* ISO14443-3 6.4.3 HLTA - If PICC responds with any modulation during 1 ms + this response shall be interpreted as not acknowledge Digital 2.0 6.9.2.1 + & EMVCo 3.0 5.6.2.1 - consider the HLTA command always acknowledged No + check to be compliant with NFC and EMVCo, and to improve interoprability + (Kovio RFID Tag) + */ + RFAL_EXIT_ON_BUSY(ret, rfalGetTransceiveStatus()); + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +bool rfalNfcaListenerIsSleepReq(const uint8_t *buf, uint16_t bufLen) { + /* Check if length and payload match */ + if ((bufLen != sizeof(rfalNfcaSlpReq)) || + (buf[RFAL_NFCA_SLP_CMD_POS] != RFAL_NFCA_SLP_CMD) || + (buf[RFAL_NFCA_SLP_BYTE2_POS] != RFAL_NFCA_SLP_BYTE2)) { + return false; + } + + return true; +} + +/* If the guards here don't compile then the code above cannot work anymore. */ +extern uint8_t + guard_eq_RFAL_NFCA_T2T[((RFAL_NFCA_SEL_RES_CONF_MASK & + (uint8_t)RFAL_NFCA_T2T) == (uint8_t)RFAL_NFCA_T2T) + ? 1 + : (-1)]; +extern uint8_t + guard_eq_RFAL_NFCA_T4T[((RFAL_NFCA_SEL_RES_CONF_MASK & + (uint8_t)RFAL_NFCA_T4T) == (uint8_t)RFAL_NFCA_T4T) + ? 1 + : (-1)]; +extern uint8_t guard_eq_RFAL_NFCA_NFCDEP[((RFAL_NFCA_SEL_RES_CONF_MASK & + (uint8_t)RFAL_NFCA_NFCDEP) == + (uint8_t)RFAL_NFCA_NFCDEP) + ? 1 + : (-1)]; +extern uint8_t guard_eq_RFAL_NFCA_T4T_NFCDEP[((RFAL_NFCA_SEL_RES_CONF_MASK & + (uint8_t)RFAL_NFCA_T4T_NFCDEP) == + (uint8_t)RFAL_NFCA_T4T_NFCDEP) + ? 1 + : (-1)]; +#endif /* RFAL_FEATURE_NFCA */ diff --git a/core/embed/io/nfc/rfal/source/rfal_nfcb.c b/core/embed/io/nfc/rfal/source/rfal_nfcb.c new file mode 100644 index 0000000000..0b9c52b549 --- /dev/null +++ b/core/embed/io/nfc/rfal/source/rfal_nfcb.c @@ -0,0 +1,751 @@ + +/****************************************************************************** + * @attention + * + * COPYRIGHT 2016 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/* + * PROJECT: ST25R391x firmware + * Revision: + * LANGUAGE: ISO C99 + */ + +/*! \file rfal_nfcb.c + * + * \author Gustavo Patricio + * + * \brief Implementation of NFC-B (ISO14443B) helpers + * + */ + +/* + ****************************************************************************** + * INCLUDES + ****************************************************************************** + */ +#include "rfal_nfcb.h" +#include "rfal_utils.h" + +/* + ****************************************************************************** + * ENABLE SWITCH + ****************************************************************************** + */ + +/* Feature switch may be enabled or disabled by user at rfal_platform.h + * Default configuration (ST25R dependant) also provided at rfal_defConfig.h + * + * RFAL_FEATURE_NFCB + */ + +#if RFAL_FEATURE_NFCB + +/* + ****************************************************************************** + * GLOBAL DEFINES + ****************************************************************************** + */ + +#define RFAL_NFCB_SENSB_REQ_EXT_SENSB_RES_SUPPORTED \ + 0x10U /*!< Bit mask for Extended SensB Response support in SENSB_REQ */ +#define RFAL_NFCB_SENSB_RES_PROT_TYPE_RFU \ + 0x08U /*!< Bit mask for Protocol Type RFU in SENSB_RES */ +#define RFAL_NFCB_SLOT_MARKER_SC_SHIFT \ + 4U /*!< Slot Code position on SLOT_MARKER APn */ + +#define RFAL_NFCB_SLOTMARKER_SLOTCODE_MIN \ + 1U /*!< SLOT_MARKER Slot Code minimum Digital 1.1 Table 37 */ +#define RFAL_NFCB_SLOTMARKER_SLOTCODE_MAX \ + 16U /*!< SLOT_MARKER Slot Code maximum Digital 1.1 Table 37 */ + +#define RFAL_NFCB_ACTIVATION_FWT \ + (RFAL_NFCB_FWTSENSB + \ + RFAL_NFCB_DTPOLL_20) /*!< FWT(SENSB) + dTbPoll Digital 2.0 7.9.1.3 */ + +/*! Advanced and Extended bit mask in Parameter of SENSB_REQ */ +#define RFAL_NFCB_SENSB_REQ_PARAM \ + (RFAL_NFCB_SENSB_REQ_ADV_FEATURE | \ + RFAL_NFCB_SENSB_REQ_EXT_SENSB_RES_SUPPORTED) + +/*! NFC-B commands definition */ +enum { + RFAL_NFCB_CMD_SENSB_REQ = + 0x05, /*!< SENSB_REQ (REQB) & SLOT_MARKER Digital 1.1 Table 24*/ + RFAL_NFCB_CMD_SENSB_RES = + 0x50, /*!< SENSB_RES (ATQB) & SLOT_MARKER Digital 1.1 Table 27*/ + RFAL_NFCB_CMD_SLPB_REQ = + 0x50, /*!< SLPB_REQ (HLTB command) Digital 1.1 Table 38 */ + RFAL_NFCB_CMD_SLPB_RES = + 0x00 /*!< SLPB_RES (HLTB Answer) Digital 1.1 Table 39 */ +}; + +/*! NFC-B Technology Detection context */ +typedef struct { + rfalNfcbSensbRes *sensbRes; /*!< Location of SENSB_RES */ + uint8_t *sensbResLen; /*!< Location of SENSB_RES length */ + uint16_t rxLen; /*!< Reception length (16bits) */ +} rfalNfcbTechDetParams; + +/*! NFC-B Collision Resolution states */ +typedef enum { + RFAL_NFCB_CR_SLOTS_TX, /*!< State where slots are open and slot markers issued + */ + RFAL_NFCB_CR_SLOTS, /*!< State where slots are open and slot markers issued */ + RFAL_NFCB_CR_SLEEP, /*!< State between slotted loop */ + RFAL_NFCB_CR_END /*!< State for terminating the collision resolution */ +} rfalNfcbColResState; + +/*! NFC-B Collision Resolution context */ +typedef struct { + rfalComplianceMode compMode; /*!< Compliancy mode to be used */ + uint8_t devLimit; /*!< Device limit to be used */ + rfalNfcbListenDevice *nfcbDevList; /*!< Location of the device list */ + uint8_t *devCnt; /*!< Location of the device counter */ + bool *colPending; /*!< Location of the Collision pending flag */ + + uint8_t curSlots; /*!< Current number of slots */ + uint8_t curSlotNum; /*!< Current Slot number (whithin slotted loop) */ + uint8_t endSlots; /*!< Maximum number of slots allowed */ + uint8_t curDevCnt; /*!< Current device counter (per slotted loop) */ + bool colPend; /*!< Internal Collision pending flag */ + uint32_t tmr; /*!< Collision Resolution timer */ + rfalNfcbColResState state; /*!< Collision Resolution state */ +} rfalNfcbColResParams; + +/* + ****************************************************************************** + * GLOBAL MACROS + ****************************************************************************** + */ + +#define rfalNfcbNI2NumberOfSlots(ni) \ + (uint8_t)( \ + 1U \ + << (ni)) /*!< Converts the Number of slots Identifier to slot number */ + +/* +****************************************************************************** +* GLOBAL TYPES +****************************************************************************** +*/ + +/*! ALLB_REQ (WUPB) and SENSB_REQ (REQB) Command Format Digital 1.1 7.6.1 */ +typedef struct { + uint8_t cmd; /*!< xxxxB_REQ: 05h */ + uint8_t AFI; /*!< NFC Identifier */ + uint8_t PARAM; /*!< Application Data */ +} rfalNfcbSensbReq; + +/*! SLOT_MARKER Command format Digital 1.1 7.7.1 */ +typedef struct { + uint8_t APn; /*!< Slot number 2..16 | 0101b */ +} rfalNfcbSlotMarker; + +/*! SLPB_REQ (HLTB) Command Format Digital 1.1 7.8.1 */ +typedef struct { + uint8_t cmd; /*!< SLPB_REQ: 50h */ + uint8_t nfcid0[RFAL_NFCB_NFCID0_LEN]; /*!< NFC Identifier (PUPI)*/ +} rfalNfcbSlpbReq; + +/*! SLPB_RES (HLTB) Response Format Digital 1.1 7.8.2 */ +typedef struct { + uint8_t cmd; /*!< SLPB_RES: 00h */ +} rfalNfcbSlpbRes; + +/*! RFAL NFC-B instance */ +typedef struct { + uint8_t AFI; /*!< AFI to be used */ + uint8_t PARAM; /*!< PARAM to be used */ + rfalNfcbColResParams CR; /*!< Collision Resolution */ + rfalNfcbTechDetParams DT; +} rfalNfcb; + +/* +****************************************************************************** +* LOCAL FUNCTION PROTOTYPES +****************************************************************************** +*/ +static ReturnCode rfalNfcbCheckSensbRes(const rfalNfcbSensbRes *sensbRes, + uint8_t sensbResLen); +static ReturnCode rfalNfcbPollerSleepTx(const uint8_t *nfcid0); + +/* +****************************************************************************** +* LOCAL VARIABLES +****************************************************************************** +*/ + +static rfalNfcb gRfalNfcb; /*!< RFAL NFC-B Instance */ + +/* +****************************************************************************** +* LOCAL FUNCTIONS +****************************************************************************** +*/ + +/*******************************************************************************/ +static ReturnCode rfalNfcbCheckSensbRes(const rfalNfcbSensbRes *sensbRes, + uint8_t sensbResLen) { + /* Check response length */ + if (((sensbResLen != RFAL_NFCB_SENSB_RES_LEN) && + (sensbResLen != RFAL_NFCB_SENSB_RES_EXT_LEN))) { + return RFAL_ERR_PROTO; + } + + /* Check SENSB_RES and Protocol Type Digital 1.1 7.6.2.19 */ + if (((sensbRes->protInfo.FsciProType & RFAL_NFCB_SENSB_RES_PROT_TYPE_RFU) != + 0U) || + (sensbRes->cmd != (uint8_t)RFAL_NFCB_CMD_SENSB_RES)) { + return RFAL_ERR_PROTO; + } + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +/* This function is used internally during Collision Resolution. Its * + * purpose is to block the state machine for minimmal time. * + * Activity 2.1 does not enforce response checking or error handling. */ +static ReturnCode rfalNfcbPollerSleepTx(const uint8_t *nfcid0) { + ReturnCode ret; + rfalNfcbSlpbReq slpbReq; + + if (nfcid0 == NULL) { + return RFAL_ERR_PARAM; + } + + /* Compute SLPB_REQ */ + slpbReq.cmd = RFAL_NFCB_CMD_SLPB_REQ; + RFAL_MEMCPY(slpbReq.nfcid0, nfcid0, RFAL_NFCB_NFCID0_LEN); + + /* Send SLPB_REQ and ignore its response and FWT*/ + RFAL_EXIT_ON_ERR( + ret, rfalTransceiveBlockingTx( + (uint8_t *)&slpbReq, sizeof(rfalNfcbSlpbReq), NULL, 0, NULL, + RFAL_TXRX_FLAGS_DEFAULT, RFAL_FDT_POLL_NFCB_POLLER)); + + return RFAL_ERR_NONE; +} + +/* +****************************************************************************** +* GLOBAL FUNCTIONS +****************************************************************************** +*/ + +/*******************************************************************************/ +ReturnCode rfalNfcbPollerInitialize(void) { + ReturnCode ret; + + RFAL_EXIT_ON_ERR(ret, + rfalSetMode(RFAL_MODE_POLL_NFCB, RFAL_BR_106, RFAL_BR_106)); + rfalSetErrorHandling(RFAL_ERRORHANDLING_NONE); + + rfalSetGT(RFAL_GT_NFCB); + rfalSetFDTListen(RFAL_FDT_LISTEN_NFCB_POLLER); + rfalSetFDTPoll(RFAL_FDT_POLL_NFCB_POLLER); + + gRfalNfcb.AFI = RFAL_NFCB_AFI; + gRfalNfcb.PARAM = RFAL_NFCB_PARAM; + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalNfcbPollerInitializeWithParams(uint8_t AFI, uint8_t PARAM) { + ReturnCode ret; + + RFAL_EXIT_ON_ERR(ret, rfalNfcbPollerInitialize()); + + gRfalNfcb.AFI = AFI; + gRfalNfcb.PARAM = (PARAM & RFAL_NFCB_SENSB_REQ_PARAM); + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalNfcbPollerCheckPresence(rfalNfcbSensCmd cmd, rfalNfcbSlots slots, + rfalNfcbSensbRes *sensbRes, + uint8_t *sensbResLen) { + ReturnCode ret; + + RFAL_EXIT_ON_ERR( + ret, rfalNfcbPollerStartCheckPresence(cmd, slots, sensbRes, sensbResLen)); + rfalRunBlocking(ret, rfalNfcbPollerGetCheckPresenceStatus()); + + return ret; +} + +/*******************************************************************************/ +ReturnCode rfalNfcbPollerStartCheckPresence(rfalNfcbSensCmd cmd, + rfalNfcbSlots slots, + rfalNfcbSensbRes *sensbRes, + uint8_t *sensbResLen) { + rfalNfcbSensbReq sensbReq; + + /* Check if the command requested and given the slot number are valid */ + if (((RFAL_NFCB_SENS_CMD_SENSB_REQ != cmd) && + (RFAL_NFCB_SENS_CMD_ALLB_REQ != cmd)) || + (slots > RFAL_NFCB_SLOT_NUM_16) || (sensbRes == NULL) || + (sensbResLen == NULL)) { + return RFAL_ERR_PARAM; + } + + *sensbResLen = 0; + RFAL_MEMSET(sensbRes, 0x00, sizeof(rfalNfcbSensbRes)); + + /* Compute SENSB_REQ */ + sensbReq.cmd = RFAL_NFCB_CMD_SENSB_REQ; + sensbReq.AFI = gRfalNfcb.AFI; + sensbReq.PARAM = (((uint8_t)gRfalNfcb.PARAM & RFAL_NFCB_SENSB_REQ_PARAM) | + (uint8_t)cmd | (uint8_t)slots); + + gRfalNfcb.DT.sensbRes = sensbRes; + gRfalNfcb.DT.sensbResLen = sensbResLen; + + /* Send SENSB_REQ */ + return rfalTransceiveBlockingTx((uint8_t *)&sensbReq, + sizeof(rfalNfcbSensbReq), (uint8_t *)sensbRes, + sizeof(rfalNfcbSensbRes), &gRfalNfcb.DT.rxLen, + RFAL_TXRX_FLAGS_DEFAULT, RFAL_NFCB_FWTSENSB); +} + +/*******************************************************************************/ +ReturnCode rfalNfcbPollerGetCheckPresenceStatus(void) { + ReturnCode ret; + + RFAL_EXIT_ON_BUSY(ret, rfalGetTransceiveStatus()); + + /* Covert bits to bytes (u8) */ + (*gRfalNfcb.DT.sensbResLen) = + (uint8_t)rfalConvBitsToBytes(gRfalNfcb.DT.rxLen); + + /* Check if a transmission error was detected */ + if ((ret == RFAL_ERR_CRC) || (ret == RFAL_ERR_FRAMING)) { + /* Invalidate received frame as an error was detected (CollisionResolution + * checks if valid) */ + (*gRfalNfcb.DT.sensbResLen) = 0; + return RFAL_ERR_NONE; + } + + if (ret == RFAL_ERR_NONE) { + return rfalNfcbCheckSensbRes(gRfalNfcb.DT.sensbRes, + *gRfalNfcb.DT.sensbResLen); + } + + return ret; +} + +/*******************************************************************************/ +ReturnCode rfalNfcbPollerSleep(const uint8_t *nfcid0) { + uint16_t rxLen; + ReturnCode ret; + rfalNfcbSlpbReq slpbReq; + rfalNfcbSlpbRes slpbRes; + + if (nfcid0 == NULL) { + return RFAL_ERR_PARAM; + } + + /* Compute SLPB_REQ */ + slpbReq.cmd = RFAL_NFCB_CMD_SLPB_REQ; + RFAL_MEMCPY(slpbReq.nfcid0, nfcid0, RFAL_NFCB_NFCID0_LEN); + + RFAL_EXIT_ON_ERR(ret, + rfalTransceiveBlockingTxRx( + (uint8_t *)&slpbReq, sizeof(rfalNfcbSlpbReq), + (uint8_t *)&slpbRes, sizeof(rfalNfcbSlpbRes), &rxLen, + RFAL_TXRX_FLAGS_DEFAULT, RFAL_NFCB_ACTIVATION_FWT)); + + /* Check SLPB_RES */ + if ((rxLen != sizeof(rfalNfcbSlpbRes)) || + (slpbRes.cmd != (uint8_t)RFAL_NFCB_CMD_SLPB_RES)) { + return RFAL_ERR_PROTO; + } + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalNfcbPollerSlotMarker(uint8_t slotCode, + rfalNfcbSensbRes *sensbRes, + uint8_t *sensbResLen) { + ReturnCode ret; + + RFAL_EXIT_ON_ERR( + ret, rfalNfcbPollerStartSlotMarker(slotCode, sensbRes, sensbResLen)); + rfalRunBlocking(ret, rfalNfcbPollerGetSlotMarkerStatus()); + + return ret; +} + +/*******************************************************************************/ +ReturnCode rfalNfcbPollerStartSlotMarker(uint8_t slotCode, + rfalNfcbSensbRes *sensbRes, + uint8_t *sensbResLen) { + rfalNfcbSlotMarker slotMarker; + + /* Check parameters */ + if ((sensbRes == NULL) || (sensbResLen == NULL) || + (slotCode < RFAL_NFCB_SLOTMARKER_SLOTCODE_MIN) || + (slotCode > RFAL_NFCB_SLOTMARKER_SLOTCODE_MAX)) { + return RFAL_ERR_PARAM; + } + /* Compose and send SLOT_MARKER with disabled AGC to detect collisions */ + slotMarker.APn = ((slotCode << RFAL_NFCB_SLOT_MARKER_SC_SHIFT) | + (uint8_t)RFAL_NFCB_CMD_SENSB_REQ); + + gRfalNfcb.DT.sensbRes = sensbRes; + gRfalNfcb.DT.sensbResLen = sensbResLen; + + return rfalTransceiveBlockingTx( + (uint8_t *)&slotMarker, sizeof(rfalNfcbSlotMarker), + (uint8_t *)gRfalNfcb.DT.sensbRes, sizeof(rfalNfcbSensbRes), + &gRfalNfcb.DT.rxLen, RFAL_TXRX_FLAGS_DEFAULT, RFAL_NFCB_FWTSENSB); +} + +/*******************************************************************************/ +ReturnCode rfalNfcbPollerGetSlotMarkerStatus(void) { + ReturnCode ret; + + RFAL_EXIT_ON_BUSY(ret, rfalGetTransceiveStatus()); + + /* Covert bits to bytes (u8) */ + (*gRfalNfcb.DT.sensbResLen) = + (uint8_t)rfalConvBitsToBytes(gRfalNfcb.DT.rxLen); + + /* Check if a transmission error was detected */ + if ((ret == RFAL_ERR_CRC) || (ret == RFAL_ERR_FRAMING)) { + return RFAL_ERR_RF_COLLISION; + } + + if (ret == RFAL_ERR_NONE) { + return rfalNfcbCheckSensbRes(gRfalNfcb.DT.sensbRes, + *gRfalNfcb.DT.sensbResLen); + } + + return ret; +} + +/*******************************************************************************/ +ReturnCode rfalNfcbPollerTechnologyDetection(rfalComplianceMode compMode, + rfalNfcbSensbRes *sensbRes, + uint8_t *sensbResLen) { + return rfalNfcbPollerCheckPresence( + ((compMode == RFAL_COMPLIANCE_MODE_EMV) ? RFAL_NFCB_SENS_CMD_ALLB_REQ + : RFAL_NFCB_SENS_CMD_SENSB_REQ), + RFAL_NFCB_SLOT_NUM_1, sensbRes, sensbResLen); +} + +/*******************************************************************************/ +ReturnCode rfalNfcbPollerStartTechnologyDetection(rfalComplianceMode compMode, + rfalNfcbSensbRes *sensbRes, + uint8_t *sensbResLen) { + return rfalNfcbPollerStartCheckPresence( + ((compMode == RFAL_COMPLIANCE_MODE_EMV) ? RFAL_NFCB_SENS_CMD_ALLB_REQ + : RFAL_NFCB_SENS_CMD_SENSB_REQ), + RFAL_NFCB_SLOT_NUM_1, sensbRes, sensbResLen); +} + +/*******************************************************************************/ +ReturnCode rfalNfcbPollerGetTechnologyDetectionStatus(void) { + return rfalNfcbPollerGetCheckPresenceStatus(); +} + +/*******************************************************************************/ +ReturnCode rfalNfcbPollerCollisionResolution(rfalComplianceMode compMode, + uint8_t devLimit, + rfalNfcbListenDevice *nfcbDevList, + uint8_t *devCnt) { + ReturnCode ret; + + RFAL_EXIT_ON_ERR(ret, rfalNfcbPollerStartCollisionResolution( + compMode, devLimit, nfcbDevList, devCnt)); + rfalRunBlocking(ret, rfalNfcbPollerGetCollisionResolutionStatus()); + + return ret; +} + +/*******************************************************************************/ +ReturnCode rfalNfcbPollerSlottedCollisionResolution( + rfalComplianceMode compMode, uint8_t devLimit, rfalNfcbSlots initSlots, + rfalNfcbSlots endSlots, rfalNfcbListenDevice *nfcbDevList, uint8_t *devCnt, + bool *colPending) { + ReturnCode ret; + + RFAL_EXIT_ON_ERR(ret, rfalNfcbPollerStartSlottedCollisionResolution( + compMode, devLimit, initSlots, endSlots, + nfcbDevList, devCnt, colPending)); + rfalRunBlocking(ret, rfalNfcbPollerGetCollisionResolutionStatus()); + + return ret; +} + +/*******************************************************************************/ +ReturnCode rfalNfcbPollerStartCollisionResolution( + rfalComplianceMode compMode, uint8_t devLimit, + rfalNfcbListenDevice *nfcbDevList, uint8_t *devCnt) { + return rfalNfcbPollerStartSlottedCollisionResolution( + compMode, devLimit, RFAL_NFCB_SLOT_NUM_1, RFAL_NFCB_SLOT_NUM_16, + nfcbDevList, devCnt, &gRfalNfcb.CR.colPend); +} + +/*******************************************************************************/ +ReturnCode rfalNfcbPollerStartSlottedCollisionResolution( + rfalComplianceMode compMode, uint8_t devLimit, rfalNfcbSlots initSlots, + rfalNfcbSlots endSlots, rfalNfcbListenDevice *nfcbDevList, uint8_t *devCnt, + bool *colPending) { + /* Check parameters. In ISO | Activity 1.0 mode the initial slots must be 1 as + * continuation of Technology Detection */ + if ((nfcbDevList == NULL) || (devCnt == NULL) || (colPending == NULL) || + (initSlots > RFAL_NFCB_SLOT_NUM_16) || + (endSlots > RFAL_NFCB_SLOT_NUM_16) || + ((compMode == RFAL_COMPLIANCE_MODE_ISO) && + (initSlots != RFAL_NFCB_SLOT_NUM_1))) { + return RFAL_ERR_PARAM; + } + + (*devCnt) = 0; + (*colPending) = false; + platformTimerDestroy(gRfalNfcb.CR.tmr); + + /* Store parameters */ + gRfalNfcb.CR.compMode = compMode; + gRfalNfcb.CR.devLimit = devLimit; + gRfalNfcb.CR.curSlots = (uint8_t)initSlots; + gRfalNfcb.CR.endSlots = (uint8_t)endSlots; + gRfalNfcb.CR.nfcbDevList = nfcbDevList; + gRfalNfcb.CR.colPending = colPending; + gRfalNfcb.CR.devCnt = devCnt; + (*gRfalNfcb.CR.devCnt) = 0U; + gRfalNfcb.CR.curDevCnt = 0U; + gRfalNfcb.CR.curSlotNum = 0U; + gRfalNfcb.CR.tmr = RFAL_TIMING_NONE; + + gRfalNfcb.CR.state = RFAL_NFCB_CR_SLOTS_TX; + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalNfcbPollerGetCollisionResolutionStatus(void) { + ReturnCode ret; + rfalNfcbSensCmd cmd; + + /* Check if operation is still not complete */ + if (gRfalNfcb.CR.tmr != RFAL_TIMING_NONE) { + if ((!platformTimerIsExpired(gRfalNfcb.CR.tmr))) { + return RFAL_ERR_BUSY; + } + } + + switch (gRfalNfcb.CR.state) { + /*******************************************************************************/ + case RFAL_NFCB_CR_SLOTS_TX: + + /* Check if it's the first iteration on ISO | Activity 1.0 mode */ + if ((gRfalNfcb.CR.compMode == RFAL_COMPLIANCE_MODE_ISO) && + (gRfalNfcb.CR.curSlots == (uint8_t)RFAL_NFCB_SLOT_NUM_1)) { + /* Do nothing in case Activity 1.0, where the previous SENSB_RES from + * technology detection should be used */ + } + /* Send SENSB_REQ with number of slots if not the first + Activity 2.1 9.3.5.24 - Symbol 23 */ + else if (gRfalNfcb.CR.curSlotNum == 0U) { + /* Send ALLB_REQ Activity 2.1 9.3.5.2 and 9.3.5.3 (Symbol 1 and 2) + */ + cmd = ((gRfalNfcb.CR.curSlots == (uint8_t)RFAL_NFCB_SLOT_NUM_1) + ? RFAL_NFCB_SENS_CMD_ALLB_REQ + : RFAL_NFCB_SENS_CMD_SENSB_REQ); + + /* PRQA S 4342 1 # MISRA 10.5 - Layout of rfalNfcbSlots and the limited + * loop guarantee that no invalid enum values are created. */ + rfalNfcbPollerStartCheckPresence( + cmd, (rfalNfcbSlots)gRfalNfcb.CR.curSlots, + &gRfalNfcb.CR.nfcbDevList[*gRfalNfcb.CR.devCnt].sensbRes, + &gRfalNfcb.CR.nfcbDevList[*gRfalNfcb.CR.devCnt].sensbResLen); + } else { + /* Activity 2.1 9.3.5.26 - Symbol 25 */ + rfalNfcbPollerStartSlotMarker( + gRfalNfcb.CR.curSlotNum, + &gRfalNfcb.CR.nfcbDevList[*gRfalNfcb.CR.devCnt].sensbRes, + &gRfalNfcb.CR.nfcbDevList[*gRfalNfcb.CR.devCnt].sensbResLen); + } + + gRfalNfcb.CR.state = RFAL_NFCB_CR_SLOTS; + return RFAL_ERR_BUSY; + + /*******************************************************************************/ + case RFAL_NFCB_CR_SLOTS: + + RFAL_EXIT_ON_BUSY(ret, rfalNfcbPollerGetSlotMarkerStatus()); + + /*******************************************************************************/ + if (gRfalNfcb.CR.compMode == RFAL_COMPLIANCE_MODE_EMV) { + /* Report (timeout) error immediately EMVCo 3.0 9.6.1.3 */ + if (ret != RFAL_ERR_NONE) { + return ret; + } + + /* Check if there was a transmission error on WUPB EMVCo 3.0 9.3.3.1 + */ + if (gRfalNfcb.CR.nfcbDevList->sensbResLen == 0U) { + return RFAL_ERR_FRAMING; + } + } + + /*******************************************************************************/ + /* Activity 2.1 9.3.5.7 and 9.3.5.8 - Symbol 6 */ + if (ret != RFAL_ERR_TIMEOUT) { + /* Activity 2.1 9.3.5.8 - Symbol 7 */ + if ((rfalNfcbCheckSensbRes( + &gRfalNfcb.CR.nfcbDevList[*gRfalNfcb.CR.devCnt].sensbRes, + gRfalNfcb.CR.nfcbDevList[*gRfalNfcb.CR.devCnt].sensbResLen) == + RFAL_ERR_NONE) && + (ret == RFAL_ERR_NONE)) { + gRfalNfcb.CR.nfcbDevList[*gRfalNfcb.CR.devCnt].isSleep = false; + + if (gRfalNfcb.CR.compMode == RFAL_COMPLIANCE_MODE_EMV) { + (*gRfalNfcb.CR.devCnt)++; + return ret; + } else if (gRfalNfcb.CR.compMode == RFAL_COMPLIANCE_MODE_ISO) { + /* Activity 1.0 9.3.5.8 - Symbol 7 */ + (*gRfalNfcb.CR.devCnt)++; + gRfalNfcb.CR.curDevCnt++; + + /* Activity 1.0 9.3.5.10 - Symbol 9 */ + if ((*gRfalNfcb.CR.devCnt >= gRfalNfcb.CR.devLimit) || + (gRfalNfcb.CR.curSlotNum == (uint8_t)RFAL_NFCB_SLOT_NUM_1)) { + return ret; + } + + /* Activity 2.1 9.3.5.11 - Symbol 10 */ + rfalNfcbPollerSleep( + gRfalNfcb.CR.nfcbDevList[(*gRfalNfcb.CR.devCnt) - 1U] + .sensbRes.nfcid0); + gRfalNfcb.CR.nfcbDevList[(*gRfalNfcb.CR.devCnt) - 1U].isSleep = + true; + } else if (gRfalNfcb.CR.compMode == RFAL_COMPLIANCE_MODE_NFC) { + /* Activity 2.1 9.3.5.10 and 9.3.5.11 - Symbol 9 and Symbol 11*/ + if (gRfalNfcb.CR.curDevCnt != 0U) { + rfalNfcbPollerSleepTx( + gRfalNfcb.CR.nfcbDevList[(*gRfalNfcb.CR.devCnt) - (uint8_t)1U] + .sensbRes.nfcid0); + gRfalNfcb.CR.nfcbDevList[(*gRfalNfcb.CR.devCnt) - (uint8_t)1U] + .isSleep = true; + + gRfalNfcb.CR.tmr = platformTimerCreate( + (uint16_t)rfalConv1fcToMs(RFAL_NFCB_ACTIVATION_FWT)); + ret = RFAL_ERR_BUSY; + } + + /* Activity 2.1 9.3.5.12 - Symbol 11 */ + (*gRfalNfcb.CR.devCnt)++; + gRfalNfcb.CR.curDevCnt++; + + /* Activity 2.1 9.3.5.6 - Symbol 13 */ + if ((*gRfalNfcb.CR.devCnt >= gRfalNfcb.CR.devLimit) || + (gRfalNfcb.CR.curSlots == (uint8_t)RFAL_NFCB_SLOT_NUM_1)) { + gRfalNfcb.CR.state = RFAL_NFCB_CR_END; + return RFAL_ERR_BUSY; + } + } else { + /* MISRA 15.7 - Empty else */ + } + } else { + /* If deviceLimit is set to 0 the NFC Forum Device is configured to + * perform collision detection only Activity 1.0 and 1.1 9.3.5.5 - + * Symbol 4 */ + if ((gRfalNfcb.CR.devLimit == 0U) && + (gRfalNfcb.CR.curSlotNum == (uint8_t)RFAL_NFCB_SLOT_NUM_1)) { + return RFAL_ERR_RF_COLLISION; + } + + /* Activity 2.1 9.3.5.9 - Symbol 8 */ + (*gRfalNfcb.CR.colPending) = true; + } + } + + /* Activity 2.1 9.3.5.15 - Symbol 14 & 15*/ + if ((gRfalNfcb.CR.curSlotNum + 1U) < + rfalNfcbNI2NumberOfSlots(gRfalNfcb.CR.curSlots)) { + gRfalNfcb.CR.curSlotNum++; + gRfalNfcb.CR.state = RFAL_NFCB_CR_SLOTS_TX; + } else { + /* Activity 2.1 9.3.5.17 - Symbol 16 */ + if (!(*gRfalNfcb.CR.colPending)) { + break; + } + + /* Activity 1.1 9.3.5.18 - Symbol 17 */ + if (gRfalNfcb.CR.curDevCnt == 0U) { + /* Activity 2.1 9.3.5.19 - Symbol 18 */ + if ((gRfalNfcb.CR.curSlotNum + 1U) >= + rfalNfcbNI2NumberOfSlots(gRfalNfcb.CR.endSlots)) { + break; + } + + /* Activity 2.1 9.3.5.20 - Symbol 19 */ + gRfalNfcb.CR.curSlots++; + } + + gRfalNfcb.CR.state = RFAL_NFCB_CR_SLEEP; + } + + return RFAL_ERR_BUSY; + + /*******************************************************************************/ + case RFAL_NFCB_CR_SLEEP: + + /* Activity 2.1 9.3.5.23 - Symbol 22 */ + if ((gRfalNfcb.CR.compMode == RFAL_COMPLIANCE_MODE_NFC) && + (gRfalNfcb.CR.curDevCnt != 0U)) { + rfalNfcbPollerSleepTx( + gRfalNfcb.CR.nfcbDevList[((*gRfalNfcb.CR.devCnt) - (uint8_t)1U)] + .sensbRes.nfcid0); + gRfalNfcb.CR.nfcbDevList[((*gRfalNfcb.CR.devCnt) - (uint8_t)1U)] + .isSleep = true; + + gRfalNfcb.CR.tmr = platformTimerCreate( + (uint16_t)rfalConv1fcToMs(RFAL_NFCB_ACTIVATION_FWT)); + } + + /* Activity 2.1 9.3.5.6 - Symbol 5 */ + gRfalNfcb.CR.curSlotNum = 0U; + gRfalNfcb.CR.curDevCnt = 0U; + (*gRfalNfcb.CR.colPending) = false; + + gRfalNfcb.CR.state = RFAL_NFCB_CR_SLOTS_TX; + return RFAL_ERR_BUSY; + + /*******************************************************************************/ + case RFAL_NFCB_CR_END: + default: + /* MISRA 16.4: no empty default statement (a comment being enough) */ + break; + } + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +uint32_t rfalNfcbTR2ToFDT(uint8_t tr2Code) { + /*******************************************************************************/ + /* MISRA 8.9 An object should be defined at block scope if its identifier only + * appears in a single function */ + /*! TR2 Table according to Digital 1.1 Table 33 */ + const uint16_t rfalNfcbTr2Table[4] = {1792, 3328, 5376, 9472}; + /*******************************************************************************/ + + return ( + uint32_t)rfalNfcbTr2Table[(tr2Code & RFAL_NFCB_SENSB_RES_PROTO_TR2_MASK)]; +} + +#endif /* RFAL_FEATURE_NFCB */ diff --git a/core/embed/io/nfc/rfal/source/rfal_nfcf.c b/core/embed/io/nfc/rfal/source/rfal_nfcf.c new file mode 100644 index 0000000000..7789032832 --- /dev/null +++ b/core/embed/io/nfc/rfal/source/rfal_nfcf.c @@ -0,0 +1,676 @@ + +/****************************************************************************** + * @attention + * + * COPYRIGHT 2016 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/* + * PROJECT: ST25R391x firmware + * Revision: + * LANGUAGE: ISO C99 + */ + +/*! \file rfal_nfcf.c + * + * \author Gustavo Patricio + * + * \brief Implementation of NFC-F Poller (FeliCa PCD) device + * + * The definitions and helpers methods provided by this module are + * aligned with NFC-F (FeliCa - JIS X6319-4) + * + */ + +/* + ****************************************************************************** + * INCLUDES + ****************************************************************************** + */ +#include "rfal_nfcf.h" +#include "rfal_utils.h" + +/* + ****************************************************************************** + * ENABLE SWITCH + ****************************************************************************** + */ + +/* Feature switch may be enabled or disabled by user at rfal_platform.h + * Default configuration (ST25R dependant) also provided at rfal_defConfig.h + * + * RFAL_FEATURE_NFCF + */ + +#if RFAL_FEATURE_NFCF + +/* + ****************************************************************************** + * GLOBAL DEFINES + ****************************************************************************** + */ +#define RFAL_NFCF_SENSF_REQ_LEN_MIN \ + 5U /*!< SENSF_RES minimum length */ + +#define RFAL_NFCF_READ_WO_ENCRYPTION_MIN_LEN \ + 15U /*!< Minimum length for a Check Command T3T 5.4.1 */ +#define RFAL_NFCF_WRITE_WO_ENCRYPTION_MIN_LEN \ + 31U /*!< Minimum length for an Update Command T3T 5.5.1 */ + +#define RFAL_NFCF_CHECK_RES_MIN_LEN \ + 11U /*!< CHECK Response minimum length T3T 1.0 Table 8 */ +#define RFAL_NFCF_UPDATE_RES_MIN_LEN \ + 11U /*!< UPDATE Response minimum length T3T 1.0 Table 8 */ + +#define RFAL_NFCF_CHECK_REQ_MAX_LEN \ + 86U /*!< Max length of a Check request T3T 1.0 Table 7 */ +#define RFAL_NFCF_CHECK_REQ_MAX_SERV \ + 15U /*!< Max Services number on Check request T3T 1.0 5.4.1.5 */ +#define RFAL_NFCF_CHECK_REQ_MAX_BLOCK \ + 15U /*!< Max Blocks number on Check request T3T 1.0 5.4.1.10 */ +#define RFAL_NFCF_UPDATE_REQ_MAX_SERV \ + 15U /*!< Max Services number Update request T3T 1.0 5.4.1.5 */ +#define RFAL_NFCF_UPDATE_REQ_MAX_BLOCK \ + 13U /*!< Max Blocks number on Update request T3T 1.0 5.4.1.10 */ + +/*! MRT Check | Update = (Tt3t x ((A+1) + n (B+1)) x 4^E) + dRWTt3t T3T 5.8 + Max values used: A = 7 ; B = 7 ; E = 3 ; n = 15 (NFC Forum n = 15, JIS n = + 32) +*/ +#define RFAL_NFCF_MRT_CHECK_UPDATE ((4096U * (8U + (15U * 8U)) * 64U) + 16U) + +/* + ****************************************************************************** + * GLOBAL MACROS + ****************************************************************************** + */ +#define rfalNfcfSlots2CardNum(s) \ + ((uint8_t)(s) + 1U) /*!< Converts Time Slot Number (TSN) into num of slots \ + */ + +/* +****************************************************************************** +* GLOBAL TYPES +****************************************************************************** +*/ + +/*! Structure/Buffer to hold the SENSF_RES with LEN byte prepended */ +typedef struct { + uint8_t LEN; /*!< NFC-F LEN byte */ + rfalNfcfSensfRes SENSF_RES; /*!< SENSF_RES */ +} rfalNfcfSensfResBuf; + +/*! Greedy collection for NFCF GRE_POLL_F Activity 1.0 Table 10 */ +typedef struct { + uint8_t pollFound; /*!< Number of devices found by the Poll */ + uint8_t pollCollision; /*!< Number of collisions detected */ + rfalFeliCaPollRes POLL_F[RFAL_NFCF_POLL_MAXCARDS]; /*!< GRE_POLL_F + Activity 1.0 Table 10 */ +} rfalNfcfGreedyF; + +/*! NFC-F SENSF_REQ format Digital 1.1 8.6.1 */ +typedef struct { + uint8_t CMD; /*!< Command code: 00h */ + uint8_t SC[RFAL_NFCF_SENSF_SC_LEN]; /*!< System Code */ + uint8_t RC; /*!< Request Code */ + uint8_t TSN; /*!< Time Slot Number */ +} rfalNfcfSensfReq; + +/*! Colission Resolution states */ +typedef enum { + RFAL_NFCF_CR_POLL, /*!< Poll Request */ + RFAL_NFCF_CR_PARSE, /*!< Parse Poll Response */ + RFAL_NFCF_CR_POLL_SC, /*!< Poll Request with RC=SC */ +} rfalNfcFColResState; + +/*! Colission Resolution context */ +typedef struct { + rfalNfcfGreedyF greedyF; + uint8_t devLimit; /*!< Device limit to be used */ + rfalComplianceMode compMode; /*!< Compliancy mode to be used */ + rfalNfcfListenDevice *nfcfDevList; /*!< Location of the device list */ + uint8_t *devCnt; /*!< Location of the device counter */ + bool collPending; /*!< Collision pending flag */ + bool nfcDepFound; + rfalNfcFColResState + state; /*!< Single Collision Resolution state (Single CR) */ +} rfalNfcfColResParams; + +/*! RFAL NFC-F instance */ +typedef struct { + rfalNfcfColResParams CR; /*!< Collision Resolution */ +} rfalNfcf; + +/* +****************************************************************************** +* LOCAL VARIABLES +****************************************************************************** +*/ +static rfalNfcf gNfcf; /*!< RFAL NFC-F instance */ + +/* +****************************************************************************** +* LOCAL FUNCTION PROTOTYPES +****************************************************************************** +*/ +static void rfalNfcfComputeValidSENF(rfalNfcfListenDevice *outDevInfo, + uint8_t *curDevIdx, uint8_t devLimit, + bool overwrite, bool *nfcDepFound); + +/* +****************************************************************************** +* LOCAL VARIABLES +****************************************************************************** +*/ + +/*******************************************************************************/ +static void rfalNfcfComputeValidSENF(rfalNfcfListenDevice *outDevInfo, + uint8_t *curDevIdx, uint8_t devLimit, + bool overwrite, bool *nfcDepFound) { + uint8_t tmpIdx; + bool duplicate; + const rfalNfcfSensfResBuf *sensfBuf; + rfalNfcfSensfResBuf sensfCopy; + + /*******************************************************************************/ + /* Go through all responses check if valid and duplicates */ + /*******************************************************************************/ + while ((gNfcf.CR.greedyF.pollFound > 0U) && ((*curDevIdx) < devLimit)) { + duplicate = false; + gNfcf.CR.greedyF.pollFound--; + + /* MISRA 11.3 - Cannot point directly into different object type, use local + * copy */ + RFAL_MEMCPY((uint8_t *)&sensfCopy, + (uint8_t *)&gNfcf.CR.greedyF.POLL_F[gNfcf.CR.greedyF.pollFound], + sizeof(rfalNfcfSensfResBuf)); + + /* Point to received SENSF_RES */ + sensfBuf = &sensfCopy; + + /* Check for devices that are already in device list */ + for (tmpIdx = 0; tmpIdx < (*curDevIdx); tmpIdx++) { + if (RFAL_BYTECMP(sensfBuf->SENSF_RES.NFCID2, + outDevInfo[tmpIdx].sensfRes.NFCID2, + RFAL_NFCF_NFCID2_LEN) == 0) { + duplicate = true; + break; + } + } + + /* If is a duplicate skip this (and not to overwrite)*/ + if (duplicate && (!overwrite)) { + continue; + } + + /* Check if response length is OK */ + if (((sensfBuf->LEN - RFAL_NFCF_HEADER_LEN) < + RFAL_NFCF_SENSF_RES_LEN_MIN) || + ((sensfBuf->LEN - RFAL_NFCF_HEADER_LEN) > + RFAL_NFCF_SENSF_RES_LEN_MAX)) { + continue; + } + + /* Check if the response is a SENSF_RES / Polling response */ + if (sensfBuf->SENSF_RES.CMD != (uint8_t)RFAL_NFCF_CMD_POLLING_RES) { + continue; + } + + /* Check if is an overwrite request or new device*/ + if (duplicate && overwrite) { + /* overwrite deviceInfo/GRE_SENSF_RES with SENSF_RES */ + outDevInfo[tmpIdx].sensfResLen = (sensfBuf->LEN - RFAL_NFCF_LENGTH_LEN); + RFAL_MEMCPY(&outDevInfo[tmpIdx].sensfRes, &sensfBuf->SENSF_RES, + outDevInfo[tmpIdx].sensfResLen); + continue; + } else { + /* fill deviceInfo/GRE_SENSF_RES with new SENSF_RES */ + outDevInfo[(*curDevIdx)].sensfResLen = + (sensfBuf->LEN - RFAL_NFCF_LENGTH_LEN); + RFAL_MEMCPY(&outDevInfo[(*curDevIdx)].sensfRes, &sensfBuf->SENSF_RES, + outDevInfo[(*curDevIdx)].sensfResLen); + } + + /* Check if this device supports NFC-DEP and signal it + * (ACTIVITY 1.1 9.3.6.63) */ + *nfcDepFound = rfalNfcfIsNfcDepSupported(&outDevInfo[(*curDevIdx)]); + + (*curDevIdx)++; + } +} + +/* +****************************************************************************** +* GLOBAL FUNCTIONS +****************************************************************************** +*/ + +/*******************************************************************************/ +ReturnCode rfalNfcfPollerInitialize(rfalBitRate bitRate) { + ReturnCode ret; + + if ((bitRate != RFAL_BR_212) && (bitRate != RFAL_BR_424)) { + return RFAL_ERR_PARAM; + } + + RFAL_EXIT_ON_ERR(ret, rfalSetMode(RFAL_MODE_POLL_NFCF, bitRate, bitRate)); + rfalSetErrorHandling(RFAL_ERRORHANDLING_NONE); + + rfalSetGT(RFAL_GT_NFCF); + rfalSetFDTListen(RFAL_FDT_LISTEN_NFCF_POLLER); + rfalSetFDTPoll(RFAL_FDT_POLL_NFCF_POLLER); + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalNfcfPollerPoll(rfalFeliCaPollSlots slots, uint16_t sysCode, + uint8_t reqCode, rfalFeliCaPollRes *cardList, + uint8_t *devCnt, uint8_t *collisions) { + return rfalFeliCaPoll(slots, sysCode, reqCode, cardList, + rfalNfcfSlots2CardNum(slots), devCnt, collisions); +} + +/*******************************************************************************/ +ReturnCode rfalNfcfPollerCheckPresence(void) { + ReturnCode ret; + + RFAL_EXIT_ON_ERR(ret, rfalNfcfPollerStartCheckPresence()); + rfalRunBlocking(ret, rfalNfcfPollerGetCheckPresenceStatus()); + + return ret; +} + +/*******************************************************************************/ +ReturnCode rfalNfcfPollerStartCheckPresence(void) { + gNfcf.CR.greedyF.pollFound = 0; + gNfcf.CR.greedyF.pollCollision = 0; + + /* ACTIVITY 1.0 & 1.1 - 9.2.3.17 SENSF_REQ must be with number of slots equal + * to 4 SC must be 0xFFFF RC must be 0x00 (No system code info required) */ + return rfalStartFeliCaPoll( + RFAL_FELICA_4_SLOTS, RFAL_NFCF_SYSTEMCODE, RFAL_FELICA_POLL_RC_NO_REQUEST, + gNfcf.CR.greedyF.POLL_F, rfalNfcfSlots2CardNum(RFAL_FELICA_4_SLOTS), + &gNfcf.CR.greedyF.pollFound, &gNfcf.CR.greedyF.pollCollision); +} + +/*******************************************************************************/ +ReturnCode rfalNfcfPollerGetCheckPresenceStatus(void) { + return rfalGetFeliCaPollStatus(); +} + +/*******************************************************************************/ +ReturnCode rfalNfcfPollerCollisionResolution(rfalComplianceMode compMode, + uint8_t devLimit, + rfalNfcfListenDevice *nfcfDevList, + uint8_t *devCnt) { + ReturnCode ret; + + RFAL_EXIT_ON_ERR(ret, rfalNfcfPollerStartCollisionResolution( + compMode, devLimit, nfcfDevList, devCnt)); + rfalRunBlocking(ret, rfalNfcfPollerGetCollisionResolutionStatus()); + + return ret; +} + +/*******************************************************************************/ +ReturnCode rfalNfcfPollerStartCollisionResolution( + rfalComplianceMode compMode, uint8_t devLimit, + rfalNfcfListenDevice *nfcfDevList, uint8_t *devCnt) { + if ((nfcfDevList == NULL) || (devCnt == NULL)) { + return RFAL_ERR_PARAM; + } + + *devCnt = 0; + + /*******************************************************************************************/ + /* ACTIVITY 1.0 - 9.3.6.3 Copy valid SENSF_RES in GRE_POLL_F into + * GRE_SENSF_RES */ + /* ACTIVITY 1.0 - 9.3.6.6 The NFC Forum Device MUST remove all entries from + * GRE_SENSF_RES[]*/ + /* ACTIVITY 2.1 - 9.3.6.2 Populate GRE_SENSF_RES with data from GRE_POLL_F */ + /* */ + /* CON_DEVICES_LIMIT = 0 Just check if devices from Tech Detection exceeds -> + * always true */ + /* Allow the number of slots open on Technology Detection */ + /*******************************************************************************************/ + rfalNfcfComputeValidSENF( + nfcfDevList, devCnt, + ((devLimit == 0U) ? rfalNfcfSlots2CardNum(RFAL_FELICA_4_SLOTS) + : devLimit), + false, &gNfcf.CR.nfcDepFound); + + /* Store context */ + gNfcf.CR.nfcfDevList = nfcfDevList; + gNfcf.CR.compMode = compMode; + gNfcf.CR.devLimit = devLimit; + gNfcf.CR.devCnt = devCnt; + gNfcf.CR.state = RFAL_NFCF_CR_POLL; + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalNfcfPollerGetCollisionResolutionStatus(void) { + ReturnCode ret; + + switch (gNfcf.CR.state) { + /*******************************************************************************/ + case RFAL_NFCF_CR_POLL: + case RFAL_NFCF_CR_POLL_SC: + + if (gNfcf.CR.state == RFAL_NFCF_CR_POLL) { + /*******************************************************************************/ + /* Activity 2.1 9.3.6.3 - Symbol 2 Check if devices found are lower + * than the limit */ + if (*gNfcf.CR.devCnt >= gNfcf.CR.devLimit) { + break; + } + + /*******************************************************************************/ + /* Activity 1.0 - 9.3.6.5 Copy valid SENSF_RES and then to remove it */ + /* Activity 1.1 - 9.3.6.65 Copy and filter duplicates */ + /* For now, due to some devices keep generating different nfcid2, we + * use 1.0 */ + /* Phones detected: Samsung Galaxy Nexus,Samsung Galaxy S3,Samsung Nexus + * S */ + /*******************************************************************************/ + *gNfcf.CR.devCnt = 0; + } + + RFAL_EXIT_ON_ERR( + ret, + rfalStartFeliCaPoll( + RFAL_FELICA_16_SLOTS, RFAL_NFCF_SYSTEMCODE, + (uint8_t)((gNfcf.CR.state == RFAL_NFCF_CR_POLL_SC) + ? RFAL_FELICA_POLL_RC_SYSTEM_CODE + : RFAL_FELICA_POLL_RC_NO_REQUEST), + gNfcf.CR.greedyF.POLL_F, + rfalNfcfSlots2CardNum((uint8_t)RFAL_FELICA_16_SLOTS), + &gNfcf.CR.greedyF.pollFound, &gNfcf.CR.greedyF.pollCollision)); + + gNfcf.CR.state = RFAL_NFCF_CR_PARSE; + return RFAL_ERR_BUSY; + + /*******************************************************************************/ + case RFAL_NFCF_CR_PARSE: + + RFAL_EXIT_ON_BUSY(ret, rfalGetFeliCaPollStatus()); + + if (ret == RFAL_ERR_NONE) { + /* Activity 2.1 9.3.6.5 - Symbol 4 Update device list */ + rfalNfcfComputeValidSENF(gNfcf.CR.nfcfDevList, gNfcf.CR.devCnt, + gNfcf.CR.devLimit, false, + &gNfcf.CR.nfcDepFound); + } + + /*******************************************************************************/ + /* Activity 2.1 9.3.6.6 - Symbol 5 Check if any device supports NFC DEP + */ + if ((gNfcf.CR.nfcDepFound) && + (gNfcf.CR.compMode == RFAL_COMPLIANCE_MODE_NFC)) { + /* Send another poll request with RC = System Code */ + gNfcf.CR.state = RFAL_NFCF_CR_POLL_SC; + + /* Set compliance mode to invalid (non NFC) to poll for NFC-DEP devices + * only once */ + gNfcf.CR.compMode = RFAL_COMPLIANCE_MODE_EMV; + return RFAL_ERR_BUSY; + } + + break; + + /*******************************************************************************/ + default: + /* MISRA 16.4: no empty default statement (a comment being enough) */ + break; + } + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalNfcfPollerCheck(const uint8_t *nfcid2, + const rfalNfcfServBlockListParam *servBlock, + uint8_t *rxBuf, uint16_t rxBufLen, + uint16_t *rcvdLen) { + uint8_t txBuf[RFAL_NFCF_CHECK_REQ_MAX_LEN]; + uint8_t msgIt; + uint8_t i; + ReturnCode ret; + const uint8_t *checkRes; + + /* Check parameters */ + if ((nfcid2 == NULL) || (rxBuf == NULL) || (servBlock == NULL) || + (servBlock->numBlock == 0U) || + (servBlock->numBlock > RFAL_NFCF_CHECK_REQ_MAX_BLOCK) || + (servBlock->numServ == 0U) || + (servBlock->numServ > RFAL_NFCF_CHECK_REQ_MAX_SERV) || + (rxBufLen < (RFAL_NFCF_LENGTH_LEN + RFAL_NFCF_CHECK_RES_MIN_LEN))) { + return RFAL_ERR_PARAM; + } + + msgIt = 0; + + /*******************************************************************************/ + /* Compose CHECK command/request */ + + txBuf[msgIt++] = RFAL_NFCF_CMD_READ_WITHOUT_ENCRYPTION; /* Command Code */ + + RFAL_MEMCPY(&txBuf[msgIt], nfcid2, RFAL_NFCF_NFCID2_LEN); /* NFCID2 */ + msgIt += RFAL_NFCF_NFCID2_LEN; + + txBuf[msgIt++] = servBlock->numServ; /* NoS */ + for (i = 0; i < servBlock->numServ; i++) { + txBuf[msgIt++] = + (uint8_t)((servBlock->servList[i] >> 0U) & 0xFFU); /* Service Code */ + txBuf[msgIt++] = (uint8_t)((servBlock->servList[i] >> 8U) & 0xFFU); + } + + txBuf[msgIt++] = servBlock->numBlock; /* NoB */ + for (i = 0; i < servBlock->numBlock; i++) { + txBuf[msgIt++] = + servBlock->blockList[i] + .conf; /* Block list element conf (Flag|Access|Service) */ + if ((servBlock->blockList[i].conf & RFAL_NFCF_BLOCKLISTELEM_LEN_BIT) != + 0U) /* Check if 2 or 3 byte block list element */ + { + txBuf[msgIt++] = (uint8_t)(servBlock->blockList[i].blockNum & + 0xFFU); /* 1byte Block Num */ + } else { + txBuf[msgIt++] = (uint8_t)((servBlock->blockList[i].blockNum >> 0U) & + 0xFFU); /* 2byte Block Num */ + txBuf[msgIt++] = + (uint8_t)((servBlock->blockList[i].blockNum >> 8U) & 0xFFU); + } + } + + /*******************************************************************************/ + /* Transceive CHECK command/request */ + ret = rfalTransceiveBlockingTxRx(txBuf, msgIt, rxBuf, rxBufLen, rcvdLen, + RFAL_TXRX_FLAGS_DEFAULT, + RFAL_NFCF_MRT_CHECK_UPDATE); + + if (ret == RFAL_ERR_NONE) { + /* Skip LEN byte */ + checkRes = (rxBuf + RFAL_NFCF_LENGTH_LEN); + + /* Check NFCID and response length T3T v1.0 5.4.2.3 */ + if ((RFAL_BYTECMP(nfcid2, &checkRes[RFAL_NFCF_CMD_LEN], + RFAL_NFCF_NFCID2_LEN) != 0) || + (*rcvdLen < + (RFAL_NFCF_LENGTH_LEN + RFAL_NFCF_CHECKUPDATE_RES_ST2_POS))) { + ret = RFAL_ERR_PROTO; + } + /* Check for a valid response */ + else if ((checkRes[RFAL_NFCF_CMD_POS] != + (uint8_t)RFAL_NFCF_CMD_READ_WITHOUT_ENCRYPTION_RES) || + (checkRes[RFAL_NFCF_CHECKUPDATE_RES_ST1_POS] != + RFAL_NFCF_STATUS_FLAG_SUCCESS) || + (checkRes[RFAL_NFCF_CHECKUPDATE_RES_ST2_POS] != + RFAL_NFCF_STATUS_FLAG_SUCCESS)) { + ret = RFAL_ERR_REQUEST; + } + /* CHECK succesfull, remove header */ + else { + (*rcvdLen) -= (RFAL_NFCF_LENGTH_LEN + RFAL_NFCF_CHECKUPDATE_RES_NOB_POS); + + if (*rcvdLen > 0U) { + RFAL_MEMMOVE(rxBuf, &checkRes[RFAL_NFCF_CHECKUPDATE_RES_NOB_POS], + (*rcvdLen)); + } + } + } + + return ret; +} + +/*******************************************************************************/ +ReturnCode rfalNfcfPollerUpdate(const uint8_t *nfcid2, + const rfalNfcfServBlockListParam *servBlock, + uint8_t *txBuf, uint16_t txBufLen, + const uint8_t *blockData, uint8_t *rxBuf, + uint16_t rxBufLen) { + uint8_t i; + uint16_t msgIt; + uint16_t rcvdLen; + uint16_t auxLen; + const uint8_t *updateRes; + ReturnCode ret; + + /* Check parameters */ + if ((nfcid2 == NULL) || (rxBuf == NULL) || (servBlock == NULL) || + (txBuf == NULL) || (servBlock->numBlock == 0U) || + (servBlock->numBlock > RFAL_NFCF_UPDATE_REQ_MAX_BLOCK) || + (servBlock->numServ == 0U) || + (servBlock->numServ > RFAL_NFCF_UPDATE_REQ_MAX_SERV) || + (rxBufLen < (RFAL_NFCF_LENGTH_LEN + RFAL_NFCF_UPDATE_RES_MIN_LEN))) { + return RFAL_ERR_PARAM; + } + + /* Calculate required txBuffer lenth T3T 1.0 Table 9 */ + auxLen = + (uint16_t)(RFAL_NFCF_CMD_LEN + RFAL_NFCF_NFCID2_LEN + RFAL_NFCF_NOS_LEN + + (servBlock->numServ * sizeof(rfalNfcfServ)) + + RFAL_NFCF_NOB_LEN + + (uint16_t)((uint16_t)servBlock->numBlock * + RFAL_NFCF_BLOCKLISTELEM_MAX_LEN) + + (uint16_t)((uint16_t)servBlock->numBlock * + RFAL_NFCF_BLOCK_LEN)); + + /* Check whether the provided buffer is sufficient for this request */ + if (txBufLen < auxLen) { + return RFAL_ERR_PARAM; + } + + msgIt = 0; + + /*******************************************************************************/ + /* Compose UPDATE command/request */ + + txBuf[msgIt++] = RFAL_NFCF_CMD_WRITE_WITHOUT_ENCRYPTION; /* Command Code */ + + RFAL_MEMCPY(&txBuf[msgIt], nfcid2, RFAL_NFCF_NFCID2_LEN); /* NFCID2 */ + msgIt += RFAL_NFCF_NFCID2_LEN; + + txBuf[msgIt++] = servBlock->numServ; /* NoS */ + for (i = 0; i < servBlock->numServ; i++) { + txBuf[msgIt++] = + (uint8_t)((servBlock->servList[i] >> 0U) & 0xFFU); /* Service Code */ + txBuf[msgIt++] = (uint8_t)((servBlock->servList[i] >> 8U) & 0xFFU); + } + + txBuf[msgIt++] = servBlock->numBlock; /* NoB */ + for (i = 0; i < servBlock->numBlock; i++) { + txBuf[msgIt++] = + servBlock->blockList[i] + .conf; /* Block list element conf (Flag|Access|Service) */ + if ((servBlock->blockList[i].conf & RFAL_NFCF_BLOCKLISTELEM_LEN_BIT) != + 0U) /* Check if 2 or 3 byte block list element */ + { + txBuf[msgIt++] = (uint8_t)(servBlock->blockList[i].blockNum & + 0xFFU); /* 1byte Block Num */ + } else { + txBuf[msgIt++] = (uint8_t)((servBlock->blockList[i].blockNum >> 0U) & + 0xFFU); /* 2byte Block Num */ + txBuf[msgIt++] = + (uint8_t)((servBlock->blockList[i].blockNum >> 8U) & 0xFFU); + } + } + + auxLen = ((uint16_t)servBlock->numBlock * RFAL_NFCF_BLOCK_LEN); + RFAL_MEMCPY(&txBuf[msgIt], blockData, auxLen); /* Block Data */ + msgIt += auxLen; + + /*******************************************************************************/ + /* Transceive UPDATE command/request */ + ret = rfalTransceiveBlockingTxRx(txBuf, msgIt, rxBuf, rxBufLen, &rcvdLen, + RFAL_TXRX_FLAGS_DEFAULT, + RFAL_NFCF_MRT_CHECK_UPDATE); + + if (ret == RFAL_ERR_NONE) { + /* Skip LEN byte */ + updateRes = (rxBuf + RFAL_NFCF_LENGTH_LEN); + + /* Check NFCID and response length T3T v1.0 5.5.2.3 */ + if ((RFAL_BYTECMP(nfcid2, &updateRes[RFAL_NFCF_CMD_LEN], + RFAL_NFCF_NFCID2_LEN) != 0) || + (rcvdLen < + (RFAL_NFCF_LENGTH_LEN + RFAL_NFCF_CHECKUPDATE_RES_ST2_POS))) { + ret = RFAL_ERR_PROTO; + } + /* Check for a valid response */ + else if ((updateRes[RFAL_NFCF_CMD_POS] != + (uint8_t)RFAL_NFCF_CMD_WRITE_WITHOUT_ENCRYPTION_RES) || + (updateRes[RFAL_NFCF_CHECKUPDATE_RES_ST1_POS] != + RFAL_NFCF_STATUS_FLAG_SUCCESS) || + (updateRes[RFAL_NFCF_CHECKUPDATE_RES_ST2_POS] != + RFAL_NFCF_STATUS_FLAG_SUCCESS)) { + ret = RFAL_ERR_REQUEST; + } else { + /* MISRA 15.7 - Empty else */ + } + } + + return ret; +} + +/*******************************************************************************/ +bool rfalNfcfListenerIsT3TReq(const uint8_t *buf, uint16_t bufLen, + uint8_t *nfcid2) { + /* Check cmd byte */ + switch (*buf) { + case RFAL_NFCF_CMD_READ_WITHOUT_ENCRYPTION: + if (bufLen < RFAL_NFCF_READ_WO_ENCRYPTION_MIN_LEN) { + return false; + } + break; + + case RFAL_NFCF_CMD_WRITE_WITHOUT_ENCRYPTION: + if (bufLen < RFAL_NFCF_WRITE_WO_ENCRYPTION_MIN_LEN) { + return false; + } + break; + + default: + return false; + } + + /* Output NFID2 if requested */ + if (nfcid2 != NULL) { + RFAL_MEMCPY(nfcid2, &buf[RFAL_NFCF_CMD_LEN], RFAL_NFCF_NFCID2_LEN); + } + + return true; +} + +#endif /* RFAL_FEATURE_NFCF */ diff --git a/core/embed/io/nfc/rfal/source/rfal_nfcv.c b/core/embed/io/nfc/rfal/source/rfal_nfcv.c new file mode 100644 index 0000000000..9cbc42a373 --- /dev/null +++ b/core/embed/io/nfc/rfal/source/rfal_nfcv.c @@ -0,0 +1,974 @@ + +/****************************************************************************** + * @attention + * + * COPYRIGHT 2016 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/* + * PROJECT: ST25R391x firmware + * Revision: + * LANGUAGE: ISO C99 + */ + +/*! \file rfal_nfcv.c + * + * \author Gustavo Patricio + * + * \brief Implementation of NFC-V Poller (ISO15693) device + * + * The definitions and helpers methods provided by this module are + * aligned with NFC-V (ISO15693) + * + * The definitions and helpers methods provided by this module + * are aligned with NFC-V Digital 2.1 + * + */ + +/* + ****************************************************************************** + * INCLUDES + ****************************************************************************** + */ +#include "rfal_nfcv.h" +#include "rfal_utils.h" + +/* + ****************************************************************************** + * ENABLE SWITCH + ****************************************************************************** + */ + +/* Feature switch may be enabled or disabled by user at rfal_platform.h + * Default configuration (ST25R dependant) also provided at rfal_defConfig.h + * + * RFAL_FEATURE_NFCV + */ + +#if RFAL_FEATURE_NFCV + +/* + ****************************************************************************** + * GLOBAL DEFINES + ****************************************************************************** + */ + +#define RFAL_NFCV_INV_REQ_FLAG \ + 0x06U /*!< INVENTORY_REQ INV_FLAG Digital 2.1 9.6.1 */ +#define RFAL_NFCV_MASKVAL_MAX_LEN \ + 8U /*!< Mask value max length: 64 bits (UID length) */ +#define RFAL_NFCV_MASKVAL_MAX_1SLOT_LEN \ + 64U /*!< Mask value max length in 1 Slot mode in bits Digital 2.1 9.6.1.6 \ + */ +#define RFAL_NFCV_MASKVAL_MAX_16SLOT_LEN \ + 60U /*!< Mask value max length in 16 Slot mode in bits Digital 2.1 9.6.1.6 \ + */ +#define RFAL_NFCV_MAX_SLOTS 16U /*!< NFC-V max number of Slots */ +#define RFAL_NFCV_INV_REQ_HEADER_LEN \ + 3U /*!< INVENTORY_REQ header length (INV_FLAG, CMD, MASK_LEN) */ +#define RFAL_NFCV_INV_RES_LEN 10U /*!< INVENTORY_RES length */ +#define RFAL_NFCV_WR_MUL_REQ_HEADER_LEN \ + 4U /*!< Write Multiple header length (INV_FLAG, CMD, [UID], BNo, Bno) */ + +#define RFAL_NFCV_CMD_LEN \ + 1U /*!< Commandbyte length */ +#define RFAL_NFCV_FLAG_POS \ + 0U /*!< Flag byte position */ +#define RFAL_NFCV_FLAG_LEN \ + 1U /*!< Flag byte length */ +#define RFAL_NFCV_PARAM_LEN \ + 1U /*!< CMD specific parameter length (e.g. Extended Get System Info) */ +#define RFAL_NFCV_DATASTART_POS \ + 1U /*!< Position of start of data */ +#define RFAL_NFCV_DSFI_LEN \ + 1U /*!< DSFID length */ +#define RFAL_NFCV_SLPREQ_REQ_FLAG \ + 0x22U /*!< SLPV_REQ request flags Digital 2.0 (Candidate) 9.7.1.1 */ +#define RFAL_NFCV_RES_FLAG_NOERROR \ + 0x00U /*!< RES_FLAG indicating no error (checked during activation) */ + +#define RFAL_NFCV_MAX_COLL_SUPPORTED \ + 16U /*!< Maximum number of collisions supported by the Anticollision loop */ + +#define RFAL_NFCV_FDT_MAX1 \ + 4394U /*!< Read alike command FWT FDTV,LISTEN,MAX1 Digital 2.0 B.5 */ + +/*! Maximum Wait time FDTV,EOF and MAX2 FDTV,LISTEN,MAX2 + Tolerance = 270644 + * + 512 = 271156 (~20ms) Digital 2.3 B.5*/ +#define RFAL_NFCV_FDT_MAX 271156U + +/*! Time from special frame to EOF + * ISO15693 2009 10.4.2 : <20ms + * NFC Forum defines Digital 2.3 9.7.4 : FDTV,EOF = [10 ; + * 20]ms + */ +#define RFAL_NFCV_FDT_EOF rfalConvMsTo1fc(16) + +/*! Time between slots - ISO 15693 defines t3min depending on modulation depth + * and data rate. With only high-bitrate supported, AM modulation and a length + * of 12 bytes (96bits) for INV_RES we get: + * - ISO t3min = 96/26 ms + 300us = 4 ms + * - NFC Forum defines FDTV,INVENT_NORES = (4394 + 2048)/fc. + * Digital 2.0 B.5*/ +#define RFAL_NFCV_FDT_V_INVENT_NORES 4U + +/* + ****************************************************************************** + * GLOBAL MACROS + ****************************************************************************** + */ + +/*! Checks if a valid INVENTORY_RES is valid Digital 2.2 9.6.2.1 & 9.6.2.3 + */ +#define rfalNfcvCheckInvRes(f, l) \ + (((l) == rfalConvBytesToBits(RFAL_NFCV_INV_RES_LEN + RFAL_NFCV_CRC_LEN)) && \ + ((f) == RFAL_NFCV_RES_FLAG_NOERROR)) + +/* +****************************************************************************** +* GLOBAL TYPES +****************************************************************************** +*/ + +/*! NFC-V INVENTORY_REQ format Digital 2.0 9.6.1 */ +typedef struct { + uint8_t INV_FLAG; /*!< Inventory Flags */ + uint8_t CMD; /*!< Command code: 01h */ + uint8_t MASK_LEN; /*!< Mask Value Length */ + uint8_t MASK_VALUE[RFAL_NFCV_MASKVAL_MAX_LEN]; /*!< Mask Value */ +} rfalNfcvInventoryReq; + +/*! NFC-V SLP_REQ format Digital 2.0 (Candidate) 9.7.1 */ +typedef struct { + uint8_t REQ_FLAG; /*!< Request Flags */ + uint8_t CMD; /*!< Command code: 02h */ + uint8_t UID[RFAL_NFCV_UID_LEN]; /*!< Mask Value */ +} rfalNfcvSlpvReq; + +/*! Container for a collision found during Anticollision loop */ +typedef struct { + uint8_t maskLen; + uint8_t maskVal[RFAL_NFCV_MASKVAL_MAX_LEN]; +} rfalNfcvCollision; + +/* +****************************************************************************** +* LOCAL FUNCTION PROTOTYPES +****************************************************************************** +*/ +static ReturnCode rfalNfcvParseError(uint8_t err); + +/* +****************************************************************************** +* LOCAL VARIABLES +****************************************************************************** +*/ + +/* +****************************************************************************** +* LOCAL FUNCTIONS +****************************************************************************** +*/ + +/*******************************************************************************/ +static ReturnCode rfalNfcvParseError(uint8_t err) { + switch (err) { + case RFAL_NFCV_ERROR_CMD_NOT_SUPPORTED: + case RFAL_NFCV_ERROR_OPTION_NOT_SUPPORTED: + return RFAL_ERR_NOTSUPP; + + case RFAL_NFCV_ERROR_CMD_NOT_RECOGNIZED: + return RFAL_ERR_PROTO; + + case RFAL_NFCV_ERROR_WRITE_FAILED: + return RFAL_ERR_WRITE; + + default: + return RFAL_ERR_REQUEST; + } +} + +/* +****************************************************************************** +* GLOBAL FUNCTIONS +****************************************************************************** +*/ + +/*******************************************************************************/ +ReturnCode rfalNfcvPollerInitialize(void) { + ReturnCode ret; + + RFAL_EXIT_ON_ERR( + ret, rfalSetMode(RFAL_MODE_POLL_NFCV, RFAL_BR_26p48, RFAL_BR_26p48)); + rfalSetErrorHandling(RFAL_ERRORHANDLING_NONE); + + rfalSetGT(RFAL_GT_NFCV); + rfalSetFDTListen(RFAL_FDT_LISTEN_NFCV_POLLER); + rfalSetFDTPoll(RFAL_FDT_POLL_NFCV_POLLER); + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalNfcvPollerCheckPresence(rfalNfcvInventoryRes* invRes) { + ReturnCode ret; + + /* INVENTORY_REQ with 1 slot and no Mask Activity 2.0 (Candidate) 9.2.3.32 + */ + ret = rfalNfcvPollerInventory(RFAL_NFCV_NUM_SLOTS_1, 0, NULL, invRes, NULL); + + if ((ret == RFAL_ERR_RF_COLLISION) || (ret == RFAL_ERR_CRC) || + (ret == RFAL_ERR_FRAMING) || (ret == RFAL_ERR_PROTO)) { + ret = RFAL_ERR_NONE; + } + + return ret; +} + +/*******************************************************************************/ +ReturnCode rfalNfcvPollerInventory(rfalNfcvNumSlots nSlots, uint8_t maskLen, + const uint8_t* maskVal, + rfalNfcvInventoryRes* invRes, + uint16_t* rcvdLen) { + ReturnCode ret; + rfalNfcvInventoryReq invReq; + uint16_t rxLen; + + if (((maskVal == NULL) && (maskLen != 0U)) || (invRes == NULL)) { + return RFAL_ERR_PARAM; + } + + invReq.INV_FLAG = (RFAL_NFCV_INV_REQ_FLAG | (uint8_t)nSlots); + invReq.CMD = RFAL_NFCV_CMD_INVENTORY; + invReq.MASK_LEN = (uint8_t)RFAL_MIN( + maskLen, + ((nSlots == RFAL_NFCV_NUM_SLOTS_1) + ? RFAL_NFCV_MASKVAL_MAX_1SLOT_LEN + : RFAL_NFCV_MASKVAL_MAX_16SLOT_LEN)); /* Digital 2.0 9.6.1.6 */ + + if ((rfalConvBitsToBytes(invReq.MASK_LEN) > 0U) && + (maskVal != NULL)) /* MISRA 21.18 & 1.3 */ + { + RFAL_MEMCPY(invReq.MASK_VALUE, maskVal, + rfalConvBitsToBytes(invReq.MASK_LEN)); + } + + ret = rfalISO15693TransceiveAnticollisionFrame( + (uint8_t*)&invReq, + (uint8_t)(RFAL_NFCV_INV_REQ_HEADER_LEN + + rfalConvBitsToBytes(invReq.MASK_LEN)), + (uint8_t*)invRes, sizeof(rfalNfcvInventoryRes), &rxLen); + + /* Check for optional output parameter */ + if (rcvdLen != NULL) { + *rcvdLen = rxLen; + } + + if (ret == RFAL_ERR_NONE) { + /* Check for valid INVENTORY_RES Digital 2.2 9.6.2.1 & 9.6.2.3 */ + if (!rfalNfcvCheckInvRes(invRes->RES_FLAG, rxLen)) { + return RFAL_ERR_PROTO; + } + } + + return ret; +} + +/*******************************************************************************/ +ReturnCode rfalNfcvPollerCollisionResolution(rfalComplianceMode compMode, + uint8_t devLimit, + rfalNfcvListenDevice* nfcvDevList, + uint8_t* devCnt) { + ReturnCode ret; + uint8_t slotNum; + uint16_t rcvdLen; + uint8_t colIt; + uint8_t colCnt; + uint8_t colPos; + bool colPending; + rfalNfcvCollision colFound[RFAL_NFCV_MAX_COLL_SUPPORTED]; + + if ((nfcvDevList == NULL) || (devCnt == NULL)) { + return RFAL_ERR_PARAM; + } + + /* Initialize parameters */ + *devCnt = 0; + colIt = 0; + colCnt = 0; + colPending = false; + RFAL_MEMSET(colFound, 0x00, + (sizeof(rfalNfcvCollision) * RFAL_NFCV_MAX_COLL_SUPPORTED)); + + if (devLimit > 0U) /* MISRA 21.18 */ + { + RFAL_MEMSET(nfcvDevList, 0x00, (sizeof(rfalNfcvListenDevice) * devLimit)); + } + + RFAL_NO_WARNING( + colPending); /* colPending is not exposed externally, in future it might + become exposed/ouput parameter */ + + if (compMode == RFAL_COMPLIANCE_MODE_NFC) { + /* Send INVENTORY_REQ with one slot Activity 2.1 9.3.7.1 (Symbol 0) */ + ret = rfalNfcvPollerInventory(RFAL_NFCV_NUM_SLOTS_1, 0, NULL, + &nfcvDevList->InvRes, NULL); + + /* Exit if no device found Activity 2.1 9.3.7.2 (Symbol 1) */ + /* Exit if no correct frame (no Transmission Error) Activity 2.1 9.3.7.3 + * (Symbol 2) */ + if ((ret == RFAL_ERR_TIMEOUT) || ((ret == RFAL_ERR_PROTO))) { + return RFAL_ERR_NONE; + } + + /* Valid Response found without transmission error/collision + * Activity 2.1 9.3.7.6 (Symbol 5) */ + if (ret == RFAL_ERR_NONE) { + (*devCnt)++; + return RFAL_ERR_NONE; + } + + /* A Collision has been identified Activity 2.1 9.3.7.4 (Symbol 3) */ + colPending = true; + colCnt = 1; + + /* Check if the Collision Resolution is set to perform only Collision + * detection Activity 2.1 9.3.7.5 (Symbol 4)*/ + if (devLimit == 0U) { + return RFAL_ERR_RF_COLLISION; + } + + platformDelay(RFAL_NFCV_FDT_V_INVENT_NORES); + + /*******************************************************************************/ + /* Collisions pending, Anticollision loop must be executed */ + /*******************************************************************************/ + } else { + /* Advance to 16 slots below without mask. Will give a good chance to + * identify multiple cards */ + colPending = true; + colCnt = 1; + } + + /* Execute until all collisions are resolved Activity 2.1 9.3.7.18 (Symbol + * 17) */ + do { + /* Activity 2.1 9.3.7.7 (Symbol 6 / 7) */ + colPending = false; + slotNum = 0; + + do { + if (slotNum == 0U) { + /* Send INVENTORY_REQ with 16 slots Activity 2.1 9.3.7.9 (Symbol 8) + */ + ret = rfalNfcvPollerInventory( + RFAL_NFCV_NUM_SLOTS_16, colFound[colIt].maskLen, + colFound[colIt].maskVal, &nfcvDevList[(*devCnt)].InvRes, &rcvdLen); + } else { + ret = rfalISO15693TransceiveEOFAnticollision( + (uint8_t*)&nfcvDevList[(*devCnt)].InvRes, + sizeof(rfalNfcvInventoryRes), &rcvdLen); + } + slotNum++; + + /*******************************************************************************/ + if (ret != RFAL_ERR_TIMEOUT) { + if (rcvdLen < + rfalConvBytesToBits( + RFAL_NFCV_INV_RES_LEN + + RFAL_NFCV_CRC_LEN)) { /* If only a partial frame was received + make sure the FDT_V_INVENT_NORES is + fulfilled */ + platformDelay(RFAL_NFCV_FDT_V_INVENT_NORES); + } + + /* Check if response is a correct frame (no TxRx error) + * Activity 2.1 9.3.7.11 (Symbol 10)*/ + if ((ret == RFAL_ERR_NONE) || (ret == RFAL_ERR_PROTO)) { + /* Check if the device found is already on the list and its response + * is a valid INVENTORY_RES */ + if (rfalNfcvCheckInvRes(nfcvDevList[(*devCnt)].InvRes.RES_FLAG, + rcvdLen)) { + /* Activity 2.1 9.3.7.12 (Symbol 11) */ + (*devCnt)++; + } + } else /* Treat everything else as collision */ + { + /* Activity 2.1 9.3.7.17 (Symbol 16) */ + colPending = true; + + /*******************************************************************************/ + /* Ensure that this collision still fits on the container */ + if (colCnt < RFAL_NFCV_MAX_COLL_SUPPORTED) { + /* Store this collision on the container to be resolved later */ + /* Activity 2.1 9.3.7.17 (Symbol 16): add the collision + * information (MASK_VAL + SN) to the list containing the collision + * information */ + RFAL_MEMCPY(colFound[colCnt].maskVal, colFound[colIt].maskVal, + RFAL_NFCV_UID_LEN); + colPos = colFound[colIt].maskLen; + colFound[colCnt].maskVal[(colPos / RFAL_BITS_IN_BYTE)] &= + (uint8_t)((1U << (colPos % RFAL_BITS_IN_BYTE)) - 1U); + colFound[colCnt].maskVal[(colPos / RFAL_BITS_IN_BYTE)] |= + (uint8_t)((slotNum - 1U) << (colPos % RFAL_BITS_IN_BYTE)); + colFound[colCnt].maskVal[((colPos / RFAL_BITS_IN_BYTE) + 1U)] = + (uint8_t)((slotNum - 1U) >> + (RFAL_BITS_IN_BYTE - (colPos % RFAL_BITS_IN_BYTE))); + + colFound[colCnt].maskLen = (colFound[colIt].maskLen + 4U); + + colCnt++; + } + } + } else { + /* Timeout */ + platformDelay(RFAL_NFCV_FDT_V_INVENT_NORES); + } + + /* Check if devices found have reached device limit Activity 2.1 9.3.7.13 + * (Symbol 12) */ + if (*devCnt >= devLimit) { + return RFAL_ERR_NONE; + } + + } while (slotNum < RFAL_NFCV_MAX_SLOTS); /* Slot loop */ + colIt++; + } while (colIt < colCnt); /* Collisions found loop */ + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalNfcvPollerSleepCollisionResolution( + uint8_t devLimit, rfalNfcvListenDevice* nfcvDevList, uint8_t* devCnt) { + uint8_t tmpDevCnt; + ReturnCode ret; + uint8_t i; + + if ((nfcvDevList == NULL) || (devCnt == NULL)) { + return RFAL_ERR_PARAM; + } + + *devCnt = 0; + + do { + tmpDevCnt = 0; + ret = rfalNfcvPollerCollisionResolution(RFAL_COMPLIANCE_MODE_ISO, + (devLimit - *devCnt), + &nfcvDevList[*devCnt], &tmpDevCnt); + + for (i = *devCnt; i < (*devCnt + tmpDevCnt); i++) { + rfalNfcvPollerSleep(0x00, nfcvDevList[i].InvRes.UID); + nfcvDevList[i].isSleep = true; + } + *devCnt += tmpDevCnt; + } while ((ret == RFAL_ERR_NONE) && (tmpDevCnt > 0U) && (*devCnt < devLimit)); + + return ret; +} + +/*******************************************************************************/ +ReturnCode rfalNfcvPollerSleep(uint8_t flags, const uint8_t* uid) { + ReturnCode ret; + rfalNfcvSlpvReq slpReq; + uint8_t rxBuf; /* dummy buffer, just to perform Rx */ + + if (uid == NULL) { + return RFAL_ERR_PARAM; + } + + /* Compute SLPV_REQ */ + slpReq.REQ_FLAG = + (flags | + (uint8_t) + RFAL_NFCV_REQ_FLAG_ADDRESS); /* Should be with UID according + Digital 2.0 (Candidate) 9.7.1.1 */ + slpReq.CMD = RFAL_NFCV_CMD_SLPV; + RFAL_MEMCPY(slpReq.UID, uid, RFAL_NFCV_UID_LEN); + + /* NFC Forum device SHALL wait at least FDTVpp to consider the SLPV + * acknowledged (FDTVpp = FDTVpoll) Digital 2.0 (Candidate) 9.7 9.8.2 */ + ret = rfalTransceiveBlockingTxRx((uint8_t*)&slpReq, sizeof(rfalNfcvSlpvReq), + &rxBuf, sizeof(rxBuf), NULL, + RFAL_TXRX_FLAGS_DEFAULT, RFAL_NFCV_FDT_MAX1); + if (ret != RFAL_ERR_TIMEOUT) { + return ret; + } + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalNfcvPollerSelect(uint8_t flags, const uint8_t* uid) { + uint16_t rcvLen; + rfalNfcvGenericRes res; + + if (uid == NULL) { + return RFAL_ERR_PARAM; + } + + return rfalNfcvPollerTransceiveReq( + RFAL_NFCV_CMD_SELECT, flags, RFAL_NFCV_PARAM_SKIP, uid, NULL, 0U, + (uint8_t*)&res, sizeof(rfalNfcvGenericRes), &rcvLen); +} + +/*******************************************************************************/ +ReturnCode rfalNfcvPollerReadSingleBlock(uint8_t flags, const uint8_t* uid, + uint8_t blockNum, uint8_t* rxBuf, + uint16_t rxBufLen, uint16_t* rcvLen) { + uint8_t bn; + + bn = blockNum; + + return rfalNfcvPollerTransceiveReq(RFAL_NFCV_CMD_READ_SINGLE_BLOCK, flags, + RFAL_NFCV_PARAM_SKIP, uid, &bn, + sizeof(uint8_t), rxBuf, rxBufLen, rcvLen); +} + +/*******************************************************************************/ +ReturnCode rfalNfcvPollerWriteSingleBlock(uint8_t flags, const uint8_t* uid, + uint8_t blockNum, + const uint8_t* wrData, + uint8_t blockLen) { + uint8_t data[(RFAL_NFCV_BLOCKNUM_LEN + RFAL_NFCV_MAX_BLOCK_LEN)]; + uint8_t dataLen; + uint16_t rcvLen; + rfalNfcvGenericRes res; + + /* Check for valid parameters */ + if ((blockLen == 0U) || (blockLen > (uint8_t)RFAL_NFCV_MAX_BLOCK_LEN) || + (wrData == NULL)) { + return RFAL_ERR_PARAM; + } + + dataLen = 0U; + + /* Compute Request Data */ + data[dataLen++] = blockNum; /* Set Block Number (8 bits) */ + RFAL_MEMCPY(&data[dataLen], wrData, + blockLen); /* Append Block data to write */ + dataLen += blockLen; + + return rfalNfcvPollerTransceiveReq( + RFAL_NFCV_CMD_WRITE_SINGLE_BLOCK, flags, RFAL_NFCV_PARAM_SKIP, uid, data, + dataLen, (uint8_t*)&res, sizeof(rfalNfcvGenericRes), &rcvLen); +} + +/*******************************************************************************/ +ReturnCode rfalNfcvPollerLockBlock(uint8_t flags, const uint8_t* uid, + uint8_t blockNum) { + uint16_t rcvLen; + rfalNfcvGenericRes res; + uint8_t bn; + + bn = blockNum; + + return rfalNfcvPollerTransceiveReq( + RFAL_NFCV_CMD_LOCK_BLOCK, flags, RFAL_NFCV_PARAM_SKIP, uid, &bn, + sizeof(uint8_t), (uint8_t*)&res, sizeof(rfalNfcvGenericRes), &rcvLen); +} + +/*******************************************************************************/ +ReturnCode rfalNfcvPollerReadMultipleBlocks(uint8_t flags, const uint8_t* uid, + uint8_t firstBlockNum, + uint8_t numOfBlocks, uint8_t* rxBuf, + uint16_t rxBufLen, + uint16_t* rcvLen) { + uint8_t data[(RFAL_NFCV_BLOCKNUM_LEN + RFAL_NFCV_BLOCKNUM_LEN)]; + uint8_t dataLen; + + dataLen = 0U; + + /* Compute Request Data */ + data[dataLen++] = firstBlockNum; /* Set first Block Number */ + data[dataLen++] = numOfBlocks; /* Set number of blocks to read */ + + return rfalNfcvPollerTransceiveReq(RFAL_NFCV_CMD_READ_MULTIPLE_BLOCKS, flags, + RFAL_NFCV_PARAM_SKIP, uid, data, dataLen, + rxBuf, rxBufLen, rcvLen); +} + +/*******************************************************************************/ +ReturnCode rfalNfcvPollerWriteMultipleBlocks( + uint8_t flags, const uint8_t* uid, uint8_t firstBlockNum, + uint8_t numOfBlocks, uint8_t* txBuf, uint16_t txBufLen, uint8_t blockLen, + const uint8_t* wrData, uint16_t wrDataLen) { + ReturnCode ret; + uint16_t rcvLen; + uint16_t reqLen; + rfalNfcvGenericRes res; + uint16_t msgIt; + + /* Calculate required buffer length */ + reqLen = + (uint16_t)((uid != NULL) ? (RFAL_NFCV_WR_MUL_REQ_HEADER_LEN + + RFAL_NFCV_UID_LEN + wrDataLen) + : (RFAL_NFCV_WR_MUL_REQ_HEADER_LEN + wrDataLen)); + + if ((reqLen > txBufLen) || (blockLen > (uint8_t)RFAL_NFCV_MAX_BLOCK_LEN) || + ((((uint16_t)numOfBlocks) * (uint16_t)blockLen) != wrDataLen) || + (numOfBlocks == 0U) || (wrData == NULL)) { + return RFAL_ERR_PARAM; + } + + msgIt = 0; + + /* Compute Request Command */ + txBuf[msgIt++] = (uint8_t)(flags & (~((uint32_t)RFAL_NFCV_REQ_FLAG_ADDRESS))); + txBuf[msgIt++] = RFAL_NFCV_CMD_WRITE_MULTIPLE_BLOCKS; + + /* Check if Request is to be sent in Addressed mode. Select mode flag shall be + * set by user */ + if (uid != NULL) { + txBuf[RFAL_NFCV_FLAG_POS] |= (uint8_t)RFAL_NFCV_REQ_FLAG_ADDRESS; + RFAL_MEMCPY(&txBuf[msgIt], uid, RFAL_NFCV_UID_LEN); + msgIt += (uint8_t)RFAL_NFCV_UID_LEN; + } + + txBuf[msgIt++] = firstBlockNum; + txBuf[msgIt++] = (numOfBlocks - 1U); + + if (wrDataLen > 0U) /* MISRA 21.18 */ + { + RFAL_MEMCPY(&txBuf[msgIt], wrData, wrDataLen); + msgIt += wrDataLen; + } + + /* Transceive Command */ + ret = rfalTransceiveBlockingTxRx(txBuf, msgIt, (uint8_t*)&res, + sizeof(rfalNfcvGenericRes), &rcvLen, + RFAL_TXRX_FLAGS_DEFAULT, RFAL_NFCV_FDT_MAX); + + if (ret != RFAL_ERR_NONE) { + return ret; + } + + /* Check if the response minimum length has been received */ + if (rcvLen < (uint8_t)RFAL_NFCV_FLAG_LEN) { + return RFAL_ERR_PROTO; + } + + /* Check if an error has been signalled */ + if ((res.RES_FLAG & (uint8_t)RFAL_NFCV_RES_FLAG_ERROR) != 0U) { + return rfalNfcvParseError(*res.data); + } + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalNfcvPollerExtendedReadSingleBlock( + uint8_t flags, const uint8_t* uid, uint16_t blockNum, uint8_t* rxBuf, + uint16_t rxBufLen, uint16_t* rcvLen) { + uint8_t data[RFAL_NFCV_BLOCKNUM_EXTENDED_LEN]; + uint8_t dataLen; + + dataLen = 0U; + + /* Compute Request Data */ + data[dataLen++] = + (uint8_t)blockNum; /* TS T5T 1.0 BNo is considered as a multi-byte field. + TS T5T 1.0 5.1.1.13 multi-byte field follows + [DIGITAL]. [DIGITAL] 9.3.1 A multiple byte field is + transmitted LSB first. */ + data[dataLen++] = (uint8_t)((blockNum >> 8U) & 0xFFU); + + return rfalNfcvPollerTransceiveReq(RFAL_NFCV_CMD_EXTENDED_READ_SINGLE_BLOCK, + flags, RFAL_NFCV_PARAM_SKIP, uid, data, + dataLen, rxBuf, rxBufLen, rcvLen); +} + +/*******************************************************************************/ +ReturnCode rfalNfcvPollerExtendedWriteSingleBlock(uint8_t flags, + const uint8_t* uid, + uint16_t blockNum, + const uint8_t* wrData, + uint8_t blockLen) { + uint8_t data[(RFAL_NFCV_BLOCKNUM_EXTENDED_LEN + RFAL_NFCV_MAX_BLOCK_LEN)]; + uint8_t dataLen; + uint16_t rcvLen; + rfalNfcvGenericRes res; + + /* Check for valid parameters */ + if ((blockLen == 0U) || (blockLen > (uint8_t)RFAL_NFCV_MAX_BLOCK_LEN)) { + return RFAL_ERR_PARAM; + } + + dataLen = 0U; + + /* Compute Request Data */ + data[dataLen++] = + (uint8_t)blockNum; /* TS T5T 1.0 BNo is considered as a multi-byte field. + TS T5T 1.0 5.1.1.13 multi-byte field follows + [DIGITAL]. [DIGITAL] 9.3.1 A multiple byte field is + transmitted LSB first. */ + data[dataLen++] = (uint8_t)((blockNum >> 8U) & 0xFFU); + RFAL_MEMCPY(&data[dataLen], wrData, + blockLen); /* Append Block data to write */ + dataLen += blockLen; + + return rfalNfcvPollerTransceiveReq( + RFAL_NFCV_CMD_EXTENDED_WRITE_SINGLE_BLOCK, flags, RFAL_NFCV_PARAM_SKIP, + uid, data, dataLen, (uint8_t*)&res, sizeof(rfalNfcvGenericRes), &rcvLen); +} + +/*******************************************************************************/ +ReturnCode rfalNfcvPollerExtendedLockSingleBlock(uint8_t flags, + const uint8_t* uid, + uint16_t blockNum) { + uint8_t data[RFAL_NFCV_BLOCKNUM_EXTENDED_LEN]; + uint8_t dataLen; + uint16_t rcvLen; + rfalNfcvGenericRes res; + + dataLen = 0U; + + /* Compute Request Data */ + data[dataLen++] = + (uint8_t)blockNum; /* TS T5T 1.0 BNo is considered as a multi-byte field. + TS T5T 1.0 5.1.1.13 multi-byte field follows + [DIGITAL]. [DIGITAL] 9.3.1 A multiple byte field is + transmitted LSB first. */ + data[dataLen++] = (uint8_t)((blockNum >> 8U) & 0xFFU); + + return rfalNfcvPollerTransceiveReq( + RFAL_NFCV_CMD_EXTENDED_LOCK_SINGLE_BLOCK, flags, RFAL_NFCV_PARAM_SKIP, + uid, data, dataLen, (uint8_t*)&res, sizeof(rfalNfcvGenericRes), &rcvLen); +} + +/*******************************************************************************/ +ReturnCode rfalNfcvPollerExtendedReadMultipleBlocks( + uint8_t flags, const uint8_t* uid, uint16_t firstBlockNum, + uint16_t numOfBlocks, uint8_t* rxBuf, uint16_t rxBufLen, uint16_t* rcvLen) { + uint8_t + data[(RFAL_NFCV_BLOCKNUM_EXTENDED_LEN + RFAL_NFCV_BLOCKNUM_EXTENDED_LEN)]; + uint8_t dataLen; + + dataLen = 0U; + + /* Compute Request Data */ + data[dataLen++] = (uint8_t)((firstBlockNum >> 0U) & 0xFFU); + data[dataLen++] = (uint8_t)((firstBlockNum >> 8U) & 0xFFU); + data[dataLen++] = (uint8_t)((numOfBlocks >> 0U) & 0xFFU); + data[dataLen++] = (uint8_t)((numOfBlocks >> 8U) & 0xFFU); + + return rfalNfcvPollerTransceiveReq(RFAL_NFCV_CMD_EXTENDED_READ_MULTIPLE_BLOCK, + flags, RFAL_NFCV_PARAM_SKIP, uid, data, + dataLen, rxBuf, rxBufLen, rcvLen); +} + +/*******************************************************************************/ +ReturnCode rfalNfcvPollerExtendedWriteMultipleBlocks( + uint8_t flags, const uint8_t* uid, uint16_t firstBlockNum, + uint16_t numOfBlocks, uint8_t* txBuf, uint16_t txBufLen, uint8_t blockLen, + const uint8_t* wrData, uint16_t wrDataLen) { + ReturnCode ret; + uint16_t rcvLen; + uint16_t reqLen; + rfalNfcvGenericRes res; + uint16_t msgIt; + uint16_t nBlocks; + + /* Calculate required buffer length */ + reqLen = + ((uid != NULL) + ? (RFAL_NFCV_WR_MUL_REQ_HEADER_LEN + RFAL_NFCV_UID_LEN + wrDataLen) + : (RFAL_NFCV_WR_MUL_REQ_HEADER_LEN + wrDataLen)); + + if ((reqLen > txBufLen) || (blockLen > (uint8_t)RFAL_NFCV_MAX_BLOCK_LEN) || + (((uint16_t)numOfBlocks * (uint16_t)blockLen) != wrDataLen) || + (numOfBlocks == 0U)) { + return RFAL_ERR_PARAM; + } + + msgIt = 0; + nBlocks = (numOfBlocks - 1U); + + /* Compute Request Command */ + txBuf[msgIt++] = (uint8_t)(flags & (~((uint32_t)RFAL_NFCV_REQ_FLAG_ADDRESS))); + txBuf[msgIt++] = RFAL_NFCV_CMD_EXTENDED_WRITE_MULTIPLE_BLOCK; + + /* Check if Request is to be sent in Addressed mode. Select mode flag shall be + * set by user */ + if (uid != NULL) { + txBuf[RFAL_NFCV_FLAG_POS] |= (uint8_t)RFAL_NFCV_REQ_FLAG_ADDRESS; + RFAL_MEMCPY(&txBuf[msgIt], uid, RFAL_NFCV_UID_LEN); + msgIt += (uint8_t)RFAL_NFCV_UID_LEN; + } + + txBuf[msgIt++] = (uint8_t)((firstBlockNum >> 0) & 0xFFU); + txBuf[msgIt++] = (uint8_t)((firstBlockNum >> 8) & 0xFFU); + txBuf[msgIt++] = (uint8_t)((nBlocks >> 0) & 0xFFU); + txBuf[msgIt++] = (uint8_t)((nBlocks >> 8) & 0xFFU); + + if (wrDataLen > 0U) /* MISRA 21.18 */ + { + RFAL_MEMCPY(&txBuf[msgIt], wrData, wrDataLen); + msgIt += wrDataLen; + } + + /* Transceive Command */ + ret = rfalTransceiveBlockingTxRx(txBuf, msgIt, (uint8_t*)&res, + sizeof(rfalNfcvGenericRes), &rcvLen, + RFAL_TXRX_FLAGS_DEFAULT, RFAL_NFCV_FDT_MAX); + + if (ret != RFAL_ERR_NONE) { + return ret; + } + + /* Check if the response minimum length has been received */ + if (rcvLen < (uint8_t)RFAL_NFCV_FLAG_LEN) { + return RFAL_ERR_PROTO; + } + + /* Check if an error has been signalled */ + if ((res.RES_FLAG & (uint8_t)RFAL_NFCV_RES_FLAG_ERROR) != 0U) { + return rfalNfcvParseError(*res.data); + } + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalNfcvPollerGetSystemInformation(uint8_t flags, const uint8_t* uid, + uint8_t* rxBuf, uint16_t rxBufLen, + uint16_t* rcvLen) { + return rfalNfcvPollerTransceiveReq(RFAL_NFCV_CMD_GET_SYS_INFO, flags, + RFAL_NFCV_PARAM_SKIP, uid, NULL, 0U, rxBuf, + rxBufLen, rcvLen); +} + +/*******************************************************************************/ +ReturnCode rfalNfcvPollerExtendedGetSystemInformation( + uint8_t flags, const uint8_t* uid, uint8_t requestField, uint8_t* rxBuf, + uint16_t rxBufLen, uint16_t* rcvLen) { + return rfalNfcvPollerTransceiveReq(RFAL_NFCV_CMD_EXTENDED_GET_SYS_INFO, flags, + requestField, uid, NULL, 0U, rxBuf, + rxBufLen, rcvLen); +} + +/*******************************************************************************/ +ReturnCode rfalNfcvPollerTransceiveReq(uint8_t cmd, uint8_t flags, + uint8_t param, const uint8_t* uid, + const uint8_t* data, uint16_t dataLen, + uint8_t* rxBuf, uint16_t rxBufLen, + uint16_t* rcvLen) { + ReturnCode ret; + rfalNfcvGenericReq req; + uint8_t msgIt; + rfalBitRate rxBR; + bool fastMode; + bool specialFrame; + + msgIt = 0; + fastMode = false; + specialFrame = false; + + /* Check for valid parameters */ + if ((rxBuf == NULL) || (rcvLen == NULL) || + ((dataLen > 0U) && (data == NULL)) || + (dataLen > ((uid != NULL) ? RFAL_NFCV_MAX_GEN_DATA_LEN + : (RFAL_NFCV_MAX_GEN_DATA_LEN - + RFAL_NFCV_UID_LEN - RFAL_NFCV_PARAM_LEN)))) { + return RFAL_ERR_PARAM; + } + + /* Check if the command is an ST's Fast command */ + if ((param == RFAL_NFCV_ST_IC_MFG_CODE) && + ((cmd == (uint8_t)RFAL_NFCV_CMD_FAST_READ_SINGLE_BLOCK) || + (cmd == (uint8_t)RFAL_NFCV_CMD_FAST_EXTENDED_READ_SINGLE_BLOCK) || + (cmd == (uint8_t)RFAL_NFCV_CMD_FAST_READ_MULTIPLE_BLOCKS) || + (cmd == (uint8_t)RFAL_NFCV_CMD_FAST_EXTENDED_READ_MULTIPLE_BLOCKS) || + (cmd == (uint8_t)RFAL_NFCV_CMD_FAST_WRITE_MESSAGE) || + (cmd == (uint8_t)RFAL_NFCV_CMD_FAST_READ_MESSAGE_LENGTH) || + (cmd == (uint8_t)RFAL_NFCV_CMD_FAST_READ_MESSAGE) || + (cmd == (uint8_t)RFAL_NFCV_CMD_FAST_READ_DYN_CONFIGURATION) || + (cmd == (uint8_t)RFAL_NFCV_CMD_FAST_WRITE_DYN_CONFIGURATION))) { + /* Store current Rx bit rate and move to fast mode */ + rfalGetBitRate(NULL, &rxBR); + rfalSetBitRate(RFAL_BR_KEEP, RFAL_BR_52p97); + + fastMode = true; + } + + /* Compute Request Command */ + req.REQ_FLAG = (uint8_t)(flags & (~((uint32_t)RFAL_NFCV_REQ_FLAG_ADDRESS))); + req.CMD = cmd; + + /* Prepend parameter on certain proprietary requests: IC Manuf, Parameters */ + if (param != RFAL_NFCV_PARAM_SKIP) { + req.payload.data[msgIt++] = param; /* RFAL_NFCV_PARAM_LEN */ + } + + /* Check if Request is to be sent in Addressed mode. Select mode flag shall be + * set by user */ + if (uid != NULL) { + req.REQ_FLAG |= (uint8_t)RFAL_NFCV_REQ_FLAG_ADDRESS; + RFAL_MEMCPY(&req.payload.data[msgIt], uid, RFAL_NFCV_UID_LEN); + msgIt += RFAL_NFCV_UID_LEN; + } + + if (dataLen > 0U) { + RFAL_MEMCPY(&req.payload.data[msgIt], data, dataLen); + msgIt += (uint8_t)dataLen; + } + + /* If the Option Flag | Special Frame is set in certain commands an EOF needs + * to be sent whithin FDTV,EOF to retrieve the VICC response + * Digital 2.3 9.7.4 ISO15693-3 2009 10.4.2 & 10.4.3 & 10.4.5 */ + if (((flags & (uint8_t)RFAL_NFCV_REQ_FLAG_OPTION) != 0U) && + ((cmd == (uint8_t)RFAL_NFCV_CMD_WRITE_SINGLE_BLOCK) || + (cmd == (uint8_t)RFAL_NFCV_CMD_WRITE_MULTIPLE_BLOCKS) || + (cmd == (uint8_t)RFAL_NFCV_CMD_LOCK_BLOCK) || + (cmd == (uint8_t)RFAL_NFCV_CMD_EXTENDED_WRITE_SINGLE_BLOCK) || + (cmd == (uint8_t)RFAL_NFCV_CMD_EXTENDED_LOCK_SINGLE_BLOCK) || + (cmd == (uint8_t)RFAL_NFCV_CMD_EXTENDED_WRITE_MULTIPLE_BLOCK))) { + specialFrame = true; + } + + /* Transceive Command */ + ret = rfalTransceiveBlockingTxRx( + (uint8_t*)&req, + (RFAL_NFCV_CMD_LEN + RFAL_NFCV_FLAG_LEN + (uint16_t)msgIt), rxBuf, + rxBufLen, rcvLen, RFAL_TXRX_FLAGS_DEFAULT, + (specialFrame ? RFAL_NFCV_FDT_EOF : RFAL_NFCV_FDT_MAX)); + + /* If the Option Flag | Special Frame is set in certain commands an EOF needs + * to be sent whithin FDTV,EOF to retrieve the VICC response + * Digital 2.3 9.7.4 ISO15693-3 2009 10.4.2 & 10.4.3 & 10.4.5 */ + if (specialFrame) { + ret = rfalISO15693TransceiveEOF(rxBuf, rxBufLen, rcvLen); + } + + /* Restore Rx BitRate */ + if (fastMode) { + rfalSetBitRate(RFAL_BR_KEEP, rxBR); + } + + if (ret != RFAL_ERR_NONE) { + return ret; + } + + /* Check if the response minimum length has been received */ + if ((*rcvLen) < (uint8_t)RFAL_NFCV_FLAG_LEN) { + return RFAL_ERR_PROTO; + } + + /* Check if an error has been signalled */ + if ((rxBuf[RFAL_NFCV_FLAG_POS] & (uint8_t)RFAL_NFCV_RES_FLAG_ERROR) != 0U) { + return rfalNfcvParseError(rxBuf[RFAL_NFCV_DATASTART_POS]); + } + + return RFAL_ERR_NONE; +} + +#endif /* RFAL_FEATURE_NFCV */ diff --git a/core/embed/io/nfc/rfal/source/rfal_st25tb.c b/core/embed/io/nfc/rfal/source/rfal_st25tb.c new file mode 100644 index 0000000000..7ee6196330 --- /dev/null +++ b/core/embed/io/nfc/rfal/source/rfal_st25tb.c @@ -0,0 +1,538 @@ + +/****************************************************************************** + * @attention + * + * COPYRIGHT 2016 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/* + * PROJECT: ST25R391x firmware + * Revision: + * LANGUAGE: ISO C99 + */ + +/*! \file rfal_st25tb.c + * + * \author Gustavo Patricio + * + * \brief Implementation of ST25TB interface + * + */ + +/* + ****************************************************************************** + * INCLUDES + ****************************************************************************** + */ +#include "rfal_st25tb.h" +#include "rfal_utils.h" + +/* + ****************************************************************************** + * ENABLE SWITCH + ****************************************************************************** + */ + +/* Feature switch may be enabled or disabled by user at rfal_platform.h + * Default configuration (ST25R dependant) also provided at rfal_defConfig.h + * + * RFAL_FEATURE_ST25TB + */ + +#if RFAL_FEATURE_ST25TB + +/* + ****************************************************************************** + * GLOBAL DEFINES + ****************************************************************************** + */ + +#define RFAL_ST25TB_CMD_LEN \ + 1U /*!< ST25TB length of a command */ +#define RFAL_ST25TB_SLOTS \ + 16U /*!< ST25TB number of slots */ +#define RFAL_ST25TB_SLOTNUM_MASK \ + 0x0FU /*!< ST25TB Slot Number bit mask on SlotMarker */ +#define RFAL_ST25TB_SLOTNUM_SHIFT \ + 4U /*!< ST25TB Slot Number shift on SlotMarker */ + +#define RFAL_ST25TB_INITIATE_CMD1 \ + 0x06U /*!< ST25TB Initiate command byte1 */ +#define RFAL_ST25TB_INITIATE_CMD2 \ + 0x00U /*!< ST25TB Initiate command byte2 */ +#define RFAL_ST25TB_PCALL_CMD1 \ + 0x06U /*!< ST25TB Pcall16 command byte1 */ +#define RFAL_ST25TB_PCALL_CMD2 \ + 0x04U /*!< ST25TB Pcall16 command byte2 */ +#define RFAL_ST25TB_SELECT_CMD \ + 0x0EU /*!< ST25TB Select command */ +#define RFAL_ST25TB_GET_UID_CMD \ + 0x0BU /*!< ST25TB Get UID command */ +#define RFAL_ST25TB_COMPLETION_CMD \ + 0x0FU /*!< ST25TB Completion command */ +#define RFAL_ST25TB_RESET_INV_CMD \ + 0x0CU /*!< ST25TB Reset to Inventory command */ +#define RFAL_ST25TB_READ_BLOCK_CMD \ + 0x08U /*!< ST25TB Read Block command */ +#define RFAL_ST25TB_WRITE_BLOCK_CMD \ + 0x09U /*!< ST25TB Write Block command */ + +#define RFAL_ST25TB_T0 \ + 2157U /*!< ST25TB t0 159 us ST25TB RF characteristics */ +#define RFAL_ST25TB_T1 \ + 2048U /*!< ST25TB t1 151 us ST25TB RF characteristics */ + +#define RFAL_ST25TB_FWT \ + (RFAL_ST25TB_T0 + RFAL_ST25TB_T1) /*!< ST25TB FWT = T0 + T1 */ +#define RFAL_ST25TB_TW \ + rfalConvMsTo1fc(7U) /*!< ST25TB TW : Programming time for write max 7ms */ + +/* + ****************************************************************************** + * GLOBAL MACROS + ****************************************************************************** + */ + +/* +****************************************************************************** +* GLOBAL TYPES +****************************************************************************** +*/ + +/*! Initiate Request */ +typedef struct { + uint8_t cmd1; /*!< Initiate Request cmd1: 0x06 */ + uint8_t cmd2; /*!< Initiate Request cmd2: 0x00 */ +} rfalSt25tbInitiateReq; + +/*! Pcall16 Request */ +typedef struct { + uint8_t cmd1; /*!< Pcal16 Request cmd1: 0x06 */ + uint8_t cmd2; /*!< Pcal16 Request cmd2: 0x04 */ +} rfalSt25tbPcallReq; + +/*! Select Request */ +typedef struct { + uint8_t cmd; /*!< Select Request cmd: 0x0E */ + uint8_t chipId; /*!< Chip ID */ +} rfalSt25tbSelectReq; + +/*! Read Block Request */ +typedef struct { + uint8_t cmd; /*!< Select Request cmd: 0x08 */ + uint8_t address; /*!< Block address */ +} rfalSt25tbReadBlockReq; + +/*! Write Block Request */ +typedef struct { + uint8_t cmd; /*!< Select Request cmd: 0x09 */ + uint8_t address; /*!< Block address */ + rfalSt25tbBlock data; /*!< Block Data */ +} rfalSt25tbWriteBlockReq; + +/* +****************************************************************************** +* LOCAL FUNCTION PROTOTYPES +****************************************************************************** +*/ +/*! + ***************************************************************************** + * \brief ST25TB Poller Do Collision Resolution + * + * This method performs ST25TB Collision resolution loop for each slot + * + * \param[in] devLimit : device limit value, and size st25tbDevList + * \param[out] st25tbDevList : ST35TB listener device info + * \param[out] devCnt : Devices found counter + * + * \return colPending : true if a collision was detected + ***************************************************************************** + */ +static bool rfalSt25tbPollerDoCollisionResolution( + uint8_t devLimit, rfalSt25tbListenDevice *st25tbDevList, uint8_t *devCnt); + +/* +****************************************************************************** +* LOCAL FUNCTION PROTOTYPES +****************************************************************************** +*/ + +static bool rfalSt25tbPollerDoCollisionResolution( + uint8_t devLimit, rfalSt25tbListenDevice *st25tbDevList, uint8_t *devCnt) { + uint8_t i; + uint8_t chipId; + ReturnCode ret; + bool col; + + col = false; + + for (i = 0; i < RFAL_ST25TB_SLOTS; i++) { + platformDelay(1); /* Wait t2: Answer to new request delay */ + + if (i == 0U) { + /* Step 2: Send Pcall16 */ + ret = rfalSt25tbPollerPcall(&chipId); + } else { + /* Step 3-17: Send Pcall16 */ + ret = rfalSt25tbPollerSlotMarker(i, &chipId); + } + + if (ret == RFAL_ERR_NONE) { + /* Found another device */ + st25tbDevList[*devCnt].chipID = chipId; + st25tbDevList[*devCnt].isDeselected = false; + + /* Select Device, retrieve its UID */ + ret = rfalSt25tbPollerSelect(chipId); + + /* By Selecting this device, the previous gets Deselected */ + if ((*devCnt) > 0U) { + st25tbDevList[(*devCnt) - 1U].isDeselected = true; + } + + if (RFAL_ERR_NONE == ret) { + ret = rfalSt25tbPollerGetUID(&st25tbDevList[*devCnt].UID); + } + + if (RFAL_ERR_NONE == ret) { + (*devCnt)++; + } + } else if ((ret == RFAL_ERR_CRC) || (ret == RFAL_ERR_FRAMING)) { + col = true; + } else { + /* MISRA 15.7 - Empty else */ + } + + if (*devCnt >= devLimit) { + break; + } + } + return col; +} + +/* +****************************************************************************** +* LOCAL VARIABLES +****************************************************************************** +*/ + +/* +****************************************************************************** +* GLOBAL FUNCTIONS +****************************************************************************** +*/ + +/*******************************************************************************/ +ReturnCode rfalSt25tbPollerInitialize(void) { + return rfalNfcbPollerInitialize(); +} + +/*******************************************************************************/ +ReturnCode rfalSt25tbPollerCheckPresence(uint8_t *chipId) { + ReturnCode ret; + uint8_t chipIdRes; + + chipIdRes = 0x00; + + /* Send Initiate Request */ + ret = rfalSt25tbPollerInitiate(&chipIdRes); + + /* Check if a transmission error was detected */ + if ((ret == RFAL_ERR_CRC) || (ret == RFAL_ERR_FRAMING)) { + return RFAL_ERR_NONE; + } + + /* Copy chip ID if requested */ + if (chipId != NULL) { + *chipId = chipIdRes; + } + + return ret; +} + +/*******************************************************************************/ +ReturnCode rfalSt25tbPollerInitiate(uint8_t *chipId) { + ReturnCode ret; + uint16_t rxLen; + rfalSt25tbInitiateReq initiateReq; + uint8_t rxBuf[RFAL_ST25TB_CHIP_ID_LEN + + RFAL_ST25TB_CRC_LEN]; /* In case we receive less data that CRC, + RF layer will not remove the CRC from + buffer */ + + /* Compute Initiate Request */ + initiateReq.cmd1 = RFAL_ST25TB_INITIATE_CMD1; + initiateReq.cmd2 = RFAL_ST25TB_INITIATE_CMD2; + + /* Send Initiate Request */ + ret = rfalTransceiveBlockingTxRx( + (uint8_t *)&initiateReq, sizeof(rfalSt25tbInitiateReq), (uint8_t *)rxBuf, + sizeof(rxBuf), &rxLen, RFAL_TXRX_FLAGS_DEFAULT, RFAL_ST25TB_FWT); + + /* Check for valid Select Response */ + if ((ret == RFAL_ERR_NONE) && (rxLen != RFAL_ST25TB_CHIP_ID_LEN)) { + return RFAL_ERR_PROTO; + } + + /* Copy chip ID if requested */ + if (chipId != NULL) { + *chipId = *rxBuf; + } + + return ret; +} + +/*******************************************************************************/ +ReturnCode rfalSt25tbPollerPcall(uint8_t *chipId) { + ReturnCode ret; + uint16_t rxLen; + rfalSt25tbPcallReq pcallReq; + + /* Compute Pcal16 Request */ + pcallReq.cmd1 = RFAL_ST25TB_PCALL_CMD1; + pcallReq.cmd2 = RFAL_ST25TB_PCALL_CMD2; + + /* Send Pcal16 Request */ + ret = rfalTransceiveBlockingTxRx( + (uint8_t *)&pcallReq, sizeof(rfalSt25tbPcallReq), (uint8_t *)chipId, + RFAL_ST25TB_CHIP_ID_LEN, &rxLen, RFAL_TXRX_FLAGS_DEFAULT, + RFAL_ST25TB_FWT); + + /* Check for valid Select Response */ + if ((ret == RFAL_ERR_NONE) && (rxLen != RFAL_ST25TB_CHIP_ID_LEN)) { + return RFAL_ERR_PROTO; + } + + return ret; +} + +/*******************************************************************************/ +ReturnCode rfalSt25tbPollerSlotMarker(uint8_t slotNum, uint8_t *chipIdRes) { + ReturnCode ret; + uint16_t rxLen; + uint8_t slotMarker; + + if ((slotNum == 0U) || (slotNum > 15U)) { + return RFAL_ERR_PARAM; + } + + /* Compute SlotMarker */ + slotMarker = + (((slotNum & RFAL_ST25TB_SLOTNUM_MASK) << RFAL_ST25TB_SLOTNUM_SHIFT) | + RFAL_ST25TB_PCALL_CMD1); + + /* Send SlotMarker */ + ret = rfalTransceiveBlockingTxRx((uint8_t *)&slotMarker, RFAL_ST25TB_CMD_LEN, + (uint8_t *)chipIdRes, + RFAL_ST25TB_CHIP_ID_LEN, &rxLen, + RFAL_TXRX_FLAGS_DEFAULT, RFAL_ST25TB_FWT); + + /* Check for valid ChipID Response */ + if ((ret == RFAL_ERR_NONE) && (rxLen != RFAL_ST25TB_CHIP_ID_LEN)) { + return RFAL_ERR_PROTO; + } + + return ret; +} + +/*******************************************************************************/ +ReturnCode rfalSt25tbPollerSelect(uint8_t chipId) { + ReturnCode ret; + uint16_t rxLen; + rfalSt25tbSelectReq selectReq; + uint8_t chipIdRes; + + /* Compute Select Request */ + selectReq.cmd = RFAL_ST25TB_SELECT_CMD; + selectReq.chipId = chipId; + + /* Send Select Request */ + ret = rfalTransceiveBlockingTxRx( + (uint8_t *)&selectReq, sizeof(rfalSt25tbSelectReq), (uint8_t *)&chipIdRes, + RFAL_ST25TB_CHIP_ID_LEN, &rxLen, RFAL_TXRX_FLAGS_DEFAULT, + RFAL_ST25TB_FWT); + + /* Check for valid Select Response */ + if ((ret == RFAL_ERR_NONE) && + ((rxLen != RFAL_ST25TB_CHIP_ID_LEN) || (chipIdRes != chipId))) { + return RFAL_ERR_PROTO; + } + + return ret; +} + +/*******************************************************************************/ +ReturnCode rfalSt25tbPollerGetUID(rfalSt25tbUID *UID) { + ReturnCode ret; + uint16_t rxLen; + uint8_t getUidReq; + + /* Compute Get UID Request */ + getUidReq = RFAL_ST25TB_GET_UID_CMD; + + /* Send Select Request */ + ret = rfalTransceiveBlockingTxRx( + (uint8_t *)&getUidReq, RFAL_ST25TB_CMD_LEN, (uint8_t *)UID, + sizeof(rfalSt25tbUID), &rxLen, RFAL_TXRX_FLAGS_DEFAULT, RFAL_ST25TB_FWT); + + /* Check for valid UID Response */ + if ((ret == RFAL_ERR_NONE) && (rxLen != RFAL_ST25TB_UID_LEN)) { + return RFAL_ERR_PROTO; + } + + return ret; +} + +/*******************************************************************************/ +ReturnCode rfalSt25tbPollerCollisionResolution( + uint8_t devLimit, rfalSt25tbListenDevice *st25tbDevList, uint8_t *devCnt) { + uint8_t chipId; + ReturnCode ret; + bool detected; /* collision or device was detected */ + + if ((st25tbDevList == NULL) || (devCnt == NULL) || (devLimit == 0U)) { + return RFAL_ERR_PARAM; + } + + *devCnt = 0; + + /* Step 1: Send Initiate */ + ret = rfalSt25tbPollerInitiate(&chipId); + if (ret == RFAL_ERR_NONE) { + /* If only 1 answer is detected */ + st25tbDevList[*devCnt].chipID = chipId; + st25tbDevList[*devCnt].isDeselected = false; + + /* Retrieve its UID and keep it Selected*/ + ret = rfalSt25tbPollerSelect(chipId); + + if (RFAL_ERR_NONE == ret) { + ret = rfalSt25tbPollerGetUID(&st25tbDevList[*devCnt].UID); + } + + if (RFAL_ERR_NONE == ret) { + (*devCnt)++; + } + } + /* Always proceed to Pcall16 anticollision as phase differences of tags can + * lead to no tag recognized, even if there is one */ + if (*devCnt < devLimit) { + /* Multiple device responses */ + do { + detected = rfalSt25tbPollerDoCollisionResolution(devLimit, st25tbDevList, + devCnt); + } while ((detected == true) && (*devCnt < devLimit)); + } + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalSt25tbPollerReadBlock(uint8_t blockAddress, + rfalSt25tbBlock *blockData) { + ReturnCode ret; + uint16_t rxLen; + rfalSt25tbReadBlockReq readBlockReq; + + /* Compute Read Block Request */ + readBlockReq.cmd = RFAL_ST25TB_READ_BLOCK_CMD; + readBlockReq.address = blockAddress; + + /* Send Read Block Request */ + ret = rfalTransceiveBlockingTxRx( + (uint8_t *)&readBlockReq, sizeof(rfalSt25tbReadBlockReq), + (uint8_t *)blockData, sizeof(rfalSt25tbBlock), &rxLen, + RFAL_TXRX_FLAGS_DEFAULT, RFAL_ST25TB_FWT); + + /* Check for valid UID Response */ + if ((ret == RFAL_ERR_NONE) && (rxLen != RFAL_ST25TB_BLOCK_LEN)) { + return RFAL_ERR_PROTO; + } + + return ret; +} + +/*******************************************************************************/ +ReturnCode rfalSt25tbPollerWriteBlock(uint8_t blockAddress, + const rfalSt25tbBlock *blockData) { + ReturnCode ret; + uint16_t rxLen; + rfalSt25tbWriteBlockReq writeBlockReq; + rfalSt25tbBlock tmpBlockData; + + /* Compute Write Block Request */ + writeBlockReq.cmd = RFAL_ST25TB_WRITE_BLOCK_CMD; + writeBlockReq.address = blockAddress; + RFAL_MEMCPY(&writeBlockReq.data, blockData, RFAL_ST25TB_BLOCK_LEN); + + /* Send Write Block Request */ + ret = rfalTransceiveBlockingTxRx( + (uint8_t *)&writeBlockReq, sizeof(rfalSt25tbWriteBlockReq), tmpBlockData, + RFAL_ST25TB_BLOCK_LEN, &rxLen, RFAL_TXRX_FLAGS_DEFAULT, + (RFAL_ST25TB_FWT + RFAL_ST25TB_TW)); + + /* Check if there was any error besides timeout */ + if (ret != RFAL_ERR_TIMEOUT) { + /* Check if an unexpected answer was received */ + if (ret == RFAL_ERR_NONE) { + return RFAL_ERR_PROTO; + } + + /* Check whether a transmission error occurred */ + if ((ret != RFAL_ERR_CRC) && (ret != RFAL_ERR_FRAMING) && + (ret != RFAL_ERR_NOMEM) && (ret != RFAL_ERR_RF_COLLISION)) { + return ret; + } + + /* If a transmission error occurred (maybe noise while commiting data) wait + * maximum programming time and verify data afterwards */ + rfalSetGT((RFAL_ST25TB_FWT + RFAL_ST25TB_TW)); + rfalFieldOnAndStartGT(); + } + + ret = rfalSt25tbPollerReadBlock(blockAddress, &tmpBlockData); + if (ret == RFAL_ERR_NONE) { + if (RFAL_BYTECMP(&tmpBlockData, blockData, RFAL_ST25TB_BLOCK_LEN) == 0) { + return RFAL_ERR_NONE; + } + return RFAL_ERR_PROTO; + } + return ret; +} + +/*******************************************************************************/ +ReturnCode rfalSt25tbPollerCompletion(void) { + uint8_t completionReq; + + /* Compute Completion Request */ + completionReq = RFAL_ST25TB_COMPLETION_CMD; + + /* Send Completion Request, no response is expected */ + return rfalTransceiveBlockingTxRx((uint8_t *)&completionReq, + RFAL_ST25TB_CMD_LEN, NULL, 0, NULL, + RFAL_TXRX_FLAGS_DEFAULT, RFAL_ST25TB_FWT); +} + +/*******************************************************************************/ +ReturnCode rfalSt25tbPollerResetToInventory(void) { + uint8_t resetInvReq; + + /* Compute Completion Request */ + resetInvReq = RFAL_ST25TB_RESET_INV_CMD; + + /* Send Completion Request, no response is expected */ + return rfalTransceiveBlockingTxRx((uint8_t *)&resetInvReq, + RFAL_ST25TB_CMD_LEN, NULL, 0, NULL, + RFAL_TXRX_FLAGS_DEFAULT, RFAL_ST25TB_FWT); +} + +#endif /* RFAL_FEATURE_ST25TB */ diff --git a/core/embed/io/nfc/rfal/source/rfal_st25xv.c b/core/embed/io/nfc/rfal/source/rfal_st25xv.c new file mode 100644 index 0000000000..463445fe2a --- /dev/null +++ b/core/embed/io/nfc/rfal/source/rfal_st25xv.c @@ -0,0 +1,678 @@ + +/****************************************************************************** + * @attention + * + * COPYRIGHT 2016 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/* + * PROJECT: ST25R391x firmware + * Revision: + * LANGUAGE: ISO C99 + */ + +/*! \file rfal_st25xv.c + * + * \author Gustavo Patricio + * + * \brief NFC-V ST25 NFC-V Tag specific features + * + * This module provides support for ST's specific features available on + * NFC-V (ISO15693) tag families: ST25D, ST25TV, M24LR + * + */ + +/* + ****************************************************************************** + * INCLUDES + ****************************************************************************** + */ +#include "rfal_st25xv.h" +#include "rfal_nfcv.h" +#include "rfal_utils.h" + +/* + ****************************************************************************** + * ENABLE SWITCH + ****************************************************************************** + */ + +/* Feature switch may be enabled or disabled by user at rfal_platform.h + * Default configuration (ST25R dependant) also provided at rfal_defConfig.h + * + * RFAL_FEATURE_ST25xV + */ + +#if RFAL_FEATURE_ST25xV + +#if !RFAL_FEATURE_NFCV +#error " RFAL: Invalid Configuration. Please Enable RFAL support for NFC-V." +#endif + +/* + ****************************************************************************** + * GLOBAL DEFINES + ****************************************************************************** + */ + +#define RFAL_ST25xV_READ_CONFIG_LEN \ + 2U /*!< READ CONFIGURATION length */ +#define RFAL_ST25xV_READ_MSG_LEN_LEN \ + 2U /*!< READ MESSAGE LENGTH length */ +#define RFAL_ST25xV_CONF_POINTER_LEN \ + 1U /*!< READ/WRITE CONFIGURATION Pointer length */ +#define RFAL_ST25xV_CONF_REGISTER_LEN \ + 1U /*!< READ/WRITE CONFIGURATION Register length */ +#define RFAL_ST25xV_PWDNUM_LEN \ + 1U /*!< Password Number length */ +#define RFAL_ST25xV_PWD_LEN \ + 8U /*!< Password length */ +#define RFAL_ST25xV_MBPOINTER_LEN \ + 1U /*!< Read Message MBPointer length */ +#define RFAL_ST25xV_NUMBYTES_LEN \ + 1U /*!< Read Message Number of Bytes length */ + +#define RFAL_ST25TV02K_TBOOT_RF \ + 1U /*!< RF Boot time (Minimum time from carrier generation to first data) */ +#define RFAL_ST25TV02K_TRF_OFF \ + 2U /*!< RF OFF time */ + +#define RFAL_ST25xV_FDT_POLL_MAX \ + rfalConvMsTo1fc( \ + 20) /*!< Maximum Wait time FDTV,EOF 20 ms Digital 2.1 B.5 */ +#define RFAL_NFCV_FLAG_POS \ + 0U /*!< Flag byte position */ +#define RFAL_NFCV_FLAG_LEN \ + 1U /*!< Flag byte length */ + +/* +****************************************************************************** +* LOCAL FUNCTION PROTOTYPES +****************************************************************************** +*/ + +static ReturnCode rfalST25xVPollerGenericReadConfiguration(uint8_t cmd, + uint8_t flags, + const uint8_t* uid, + uint8_t pointer, + uint8_t* regValue); +static ReturnCode rfalST25xVPollerGenericWriteConfiguration(uint8_t cmd, + uint8_t flags, + const uint8_t* uid, + uint8_t pointer, + uint8_t regValue); +static ReturnCode rfalST25xVPollerGenericReadMessageLength(uint8_t cmd, + uint8_t flags, + const uint8_t* uid, + uint8_t* msgLen); +static ReturnCode rfalST25xVPollerGenericReadMessage( + uint8_t cmd, uint8_t flags, const uint8_t* uid, uint8_t mbPointer, + uint8_t numBytes, uint8_t* rxBuf, uint16_t rxBufLen, uint16_t* rcvLen); +static ReturnCode rfalST25xVPollerGenericWriteMessage( + uint8_t cmd, uint8_t flags, const uint8_t* uid, uint8_t msgLen, + const uint8_t* msgData, uint8_t* txBuf, uint16_t txBufLen); +/* +****************************************************************************** +* LOCAL FUNCTIONS +****************************************************************************** +*/ + +/*******************************************************************************/ +static ReturnCode rfalST25xVPollerGenericReadConfiguration(uint8_t cmd, + uint8_t flags, + const uint8_t* uid, + uint8_t pointer, + uint8_t* regValue) { + ReturnCode ret; + uint8_t p; + uint16_t rcvLen; + rfalNfcvGenericRes res; + + if (regValue == NULL) { + return RFAL_ERR_PARAM; + } + + p = pointer; + + ret = rfalNfcvPollerTransceiveReq(cmd, flags, RFAL_NFCV_ST_IC_MFG_CODE, uid, + &p, sizeof(uint8_t), (uint8_t*)&res, + sizeof(rfalNfcvGenericRes), &rcvLen); + if (ret == RFAL_ERR_NONE) { + if (rcvLen < RFAL_ST25xV_READ_CONFIG_LEN) { + ret = RFAL_ERR_PROTO; + } else { + *regValue = res.data[0]; + } + } + return ret; +} + +/*******************************************************************************/ +static ReturnCode rfalST25xVPollerGenericWriteConfiguration(uint8_t cmd, + uint8_t flags, + const uint8_t* uid, + uint8_t pointer, + uint8_t regValue) { + uint8_t data[RFAL_ST25xV_CONF_POINTER_LEN + RFAL_ST25xV_CONF_REGISTER_LEN]; + uint8_t dataLen; + uint16_t rcvLen; + rfalNfcvGenericRes res; + + dataLen = 0U; + + data[dataLen++] = pointer; + data[dataLen++] = regValue; + + return rfalNfcvPollerTransceiveReq(cmd, flags, RFAL_NFCV_ST_IC_MFG_CODE, uid, + data, dataLen, (uint8_t*)&res, + sizeof(rfalNfcvGenericRes), &rcvLen); +} + +/*******************************************************************************/ +static ReturnCode rfalST25xVPollerGenericReadMessageLength(uint8_t cmd, + uint8_t flags, + const uint8_t* uid, + uint8_t* msgLen) { + ReturnCode ret; + uint16_t rcvLen; + rfalNfcvGenericRes res; + + if (msgLen == NULL) { + return RFAL_ERR_PARAM; + } + + ret = rfalNfcvPollerTransceiveReq(cmd, flags, RFAL_NFCV_ST_IC_MFG_CODE, uid, + NULL, 0, (uint8_t*)&res, + sizeof(rfalNfcvGenericRes), &rcvLen); + if (ret == RFAL_ERR_NONE) { + if (rcvLen < RFAL_ST25xV_READ_MSG_LEN_LEN) { + ret = RFAL_ERR_PROTO; + } else { + *msgLen = res.data[0]; + } + } + return ret; +} + +/*******************************************************************************/ +static ReturnCode rfalST25xVPollerGenericReadMessage( + uint8_t cmd, uint8_t flags, const uint8_t* uid, uint8_t mbPointer, + uint8_t numBytes, uint8_t* rxBuf, uint16_t rxBufLen, uint16_t* rcvLen) { + uint8_t data[RFAL_ST25xV_MBPOINTER_LEN + RFAL_ST25xV_NUMBYTES_LEN]; + uint8_t dataLen; + + dataLen = 0; + + /* Compute Request Data */ + data[dataLen++] = mbPointer; + data[dataLen++] = numBytes; + + return rfalNfcvPollerTransceiveReq(cmd, flags, RFAL_NFCV_ST_IC_MFG_CODE, uid, + data, dataLen, rxBuf, rxBufLen, rcvLen); +} + +/*******************************************************************************/ +static ReturnCode rfalST25xVPollerGenericWriteMessage( + uint8_t cmd, uint8_t flags, const uint8_t* uid, uint8_t msgLen, + const uint8_t* msgData, uint8_t* txBuf, uint16_t txBufLen) { + ReturnCode ret; + uint8_t reqFlag; + uint16_t msgIt; + rfalBitRate rxBR; + bool fastMode; + rfalNfcvGenericRes res; + uint16_t rcvLen; + + /* Calculate required Tx buf length: Mfg Code UID MSGLen + * MSGLen+1 */ + msgIt = (uint16_t)(msgLen + sizeof(flags) + sizeof(cmd) + 1U + + ((uid != NULL) ? RFAL_NFCV_UID_LEN : 0U) + 1U + 1U); + /* Note: MSGlength parameter of the command is the number of Data bytes minus + * - 1 (00 for 1 byte of data, FFh for 256 bytes of data) */ + + /* Check for valid parameters */ + if ((txBuf == NULL) || (msgData == NULL) || (txBufLen < msgIt)) { + return RFAL_ERR_PARAM; + } + + msgIt = 0; + fastMode = false; + + /* Check if the command is an ST's Fast command */ + if (cmd == (uint8_t)RFAL_NFCV_CMD_FAST_WRITE_MESSAGE) { + /* Store current Rx bit rate and move to fast mode */ + rfalGetBitRate(NULL, &rxBR); + rfalSetBitRate(RFAL_BR_KEEP, RFAL_BR_52p97); + + fastMode = true; + } + + /* Compute Request Command */ + reqFlag = (uint8_t)(flags & (~((uint32_t)RFAL_NFCV_REQ_FLAG_ADDRESS) & + ~((uint32_t)RFAL_NFCV_REQ_FLAG_SELECT))); + reqFlag |= ((uid != NULL) ? (uint8_t)RFAL_NFCV_REQ_FLAG_ADDRESS + : (uint8_t)RFAL_NFCV_REQ_FLAG_SELECT); + + txBuf[msgIt++] = reqFlag; + txBuf[msgIt++] = cmd; + txBuf[msgIt++] = RFAL_NFCV_ST_IC_MFG_CODE; + + if (uid != NULL) { + RFAL_MEMCPY(&txBuf[msgIt], uid, RFAL_NFCV_UID_LEN); + msgIt += RFAL_NFCV_UID_LEN; + } + txBuf[msgIt++] = msgLen; + RFAL_MEMCPY(&txBuf[msgIt], msgData, + (uint16_t)(msgLen + (uint16_t)1U)); /* Message Data contains + (MSGLength + 1) bytes */ + msgIt += (uint16_t)(msgLen + (uint16_t)1U); + + /* Transceive Command */ + ret = rfalTransceiveBlockingTxRx( + txBuf, msgIt, (uint8_t*)&res, sizeof(rfalNfcvGenericRes), &rcvLen, + RFAL_TXRX_FLAGS_DEFAULT, RFAL_ST25xV_FDT_POLL_MAX); + + /* Restore Rx BitRate */ + if (fastMode) { + rfalSetBitRate(RFAL_BR_KEEP, rxBR); + } + + if (ret != RFAL_ERR_NONE) { + return ret; + } + + /* Check if the response minimum length has been received */ + if (rcvLen < (uint8_t)RFAL_NFCV_FLAG_LEN) { + return RFAL_ERR_PROTO; + } + + /* Check if an error has been signalled */ + if ((res.RES_FLAG & (uint8_t)RFAL_NFCV_RES_FLAG_ERROR) != 0U) { + return RFAL_ERR_PROTO; + } + + return RFAL_ERR_NONE; +} + +/* +****************************************************************************** +* GLOBAL FUNCTIONS +****************************************************************************** +*/ + +/*******************************************************************************/ +ReturnCode rfalST25xVPollerM24LRReadSingleBlock( + uint8_t flags, const uint8_t* uid, uint16_t blockNum, uint8_t* rxBuf, + uint16_t rxBufLen, uint16_t* rcvLen) { + uint8_t data[RFAL_NFCV_BLOCKNUM_M24LR_LEN]; + uint8_t dataLen; + + dataLen = 0; + + /* Compute Request Data */ + data[dataLen++] = + (uint8_t)blockNum; /* Set M24LR Block Number (16 bits) LSB */ + data[dataLen++] = + (uint8_t)(blockNum >> 8U); /* Set M24LR Block Number (16 bits) MSB */ + + return rfalNfcvPollerTransceiveReq( + RFAL_NFCV_CMD_READ_SINGLE_BLOCK, + (flags | (uint8_t)RFAL_NFCV_REQ_FLAG_PROTOCOL_EXT), RFAL_NFCV_PARAM_SKIP, + uid, data, dataLen, rxBuf, rxBufLen, rcvLen); +} + +/*******************************************************************************/ +ReturnCode rfalST25xVPollerM24LRWriteSingleBlock(uint8_t flags, + const uint8_t* uid, + uint16_t blockNum, + const uint8_t* wrData, + uint8_t blockLen) { + uint8_t data[(RFAL_NFCV_BLOCKNUM_M24LR_LEN + RFAL_NFCV_MAX_BLOCK_LEN)]; + uint8_t dataLen; + uint16_t rcvLen; + rfalNfcvGenericRes res; + + /* Check for valid parameters */ + if ((blockLen == 0U) || (blockLen > (uint8_t)RFAL_NFCV_MAX_BLOCK_LEN) || + (wrData == NULL)) { + return RFAL_ERR_PARAM; + } + + dataLen = 0U; + + /* Compute Request Data */ + data[dataLen++] = + (uint8_t)blockNum; /* Set M24LR Block Number (16 bits) LSB */ + data[dataLen++] = + (uint8_t)(blockNum >> 8U); /* Set M24LR Block Number (16 bits) MSB */ + RFAL_MEMCPY(&data[dataLen], wrData, + blockLen); /* Append Block data to write */ + dataLen += blockLen; + + return rfalNfcvPollerTransceiveReq( + RFAL_NFCV_CMD_WRITE_SINGLE_BLOCK, + (flags | (uint8_t)RFAL_NFCV_REQ_FLAG_PROTOCOL_EXT), RFAL_NFCV_PARAM_SKIP, + uid, data, dataLen, (uint8_t*)&res, sizeof(rfalNfcvGenericRes), &rcvLen); +} + +/*******************************************************************************/ +ReturnCode rfalST25xVPollerM24LRReadMultipleBlocks( + uint8_t flags, const uint8_t* uid, uint16_t firstBlockNum, + uint8_t numOfBlocks, uint8_t* rxBuf, uint16_t rxBufLen, uint16_t* rcvLen) { + uint8_t data[(RFAL_NFCV_BLOCKNUM_M24LR_LEN + RFAL_NFCV_BLOCKNUM_M24LR_LEN)]; + uint8_t dataLen; + + dataLen = 0U; + + /* Compute Request Data */ + data[dataLen++] = + (uint8_t)firstBlockNum; /* Set M24LR Block Number (16 bits) LSB */ + data[dataLen++] = + (uint8_t)(firstBlockNum >> 8U); /* Set M24LR Block Number (16 bits) MSB */ + data[dataLen++] = numOfBlocks; /* Set number of blocks to read */ + + return rfalNfcvPollerTransceiveReq( + RFAL_NFCV_CMD_READ_MULTIPLE_BLOCKS, + (flags | (uint8_t)RFAL_NFCV_REQ_FLAG_PROTOCOL_EXT), RFAL_NFCV_PARAM_SKIP, + uid, data, dataLen, rxBuf, rxBufLen, rcvLen); +} + +/*******************************************************************************/ +ReturnCode rfalST25xVPollerFastReadSingleBlock(uint8_t flags, + const uint8_t* uid, + uint8_t blockNum, uint8_t* rxBuf, + uint16_t rxBufLen, + uint16_t* rcvLen) { + uint8_t bn; + + bn = blockNum; + + return rfalNfcvPollerTransceiveReq(RFAL_NFCV_CMD_FAST_READ_SINGLE_BLOCK, + flags, RFAL_NFCV_ST_IC_MFG_CODE, uid, &bn, + sizeof(uint8_t), rxBuf, rxBufLen, rcvLen); +} + +/*******************************************************************************/ +ReturnCode rfalST25xVPollerM24LRFastReadSingleBlock( + uint8_t flags, const uint8_t* uid, uint16_t blockNum, uint8_t* rxBuf, + uint16_t rxBufLen, uint16_t* rcvLen) { + uint8_t data[RFAL_NFCV_BLOCKNUM_M24LR_LEN]; + uint8_t dataLen; + + dataLen = 0; + + /* Compute Request Data */ + data[dataLen++] = + (uint8_t)blockNum; /* Set M24LR Block Number (16 bits) LSB */ + data[dataLen++] = + (uint8_t)(blockNum >> 8U); /* Set M24LR Block Number (16 bits) MSB */ + + return rfalNfcvPollerTransceiveReq( + RFAL_NFCV_CMD_FAST_READ_SINGLE_BLOCK, + (flags | (uint8_t)RFAL_NFCV_REQ_FLAG_PROTOCOL_EXT), + RFAL_NFCV_ST_IC_MFG_CODE, uid, data, dataLen, rxBuf, rxBufLen, rcvLen); +} + +/*******************************************************************************/ +ReturnCode rfalST25xVPollerM24LRFastReadMultipleBlocks( + uint8_t flags, const uint8_t* uid, uint16_t firstBlockNum, + uint8_t numOfBlocks, uint8_t* rxBuf, uint16_t rxBufLen, uint16_t* rcvLen) { + uint8_t data[(RFAL_NFCV_BLOCKNUM_M24LR_LEN + RFAL_NFCV_BLOCKNUM_M24LR_LEN)]; + uint8_t dataLen; + + dataLen = 0U; + + /* Compute Request Data */ + data[dataLen++] = + (uint8_t)firstBlockNum; /* Set M24LR Block Number (16 bits) LSB */ + data[dataLen++] = + (uint8_t)(firstBlockNum >> 8U); /* Set M24LR Block Number (16 bits) MSB */ + data[dataLen++] = numOfBlocks; /* Set number of blocks to read */ + + return rfalNfcvPollerTransceiveReq( + RFAL_NFCV_CMD_FAST_READ_MULTIPLE_BLOCKS, + (flags | (uint8_t)RFAL_NFCV_REQ_FLAG_PROTOCOL_EXT), + RFAL_NFCV_ST_IC_MFG_CODE, uid, data, dataLen, rxBuf, rxBufLen, rcvLen); +} + +/*******************************************************************************/ +ReturnCode rfalST25xVPollerFastReadMultipleBlocks( + uint8_t flags, const uint8_t* uid, uint8_t firstBlockNum, + uint8_t numOfBlocks, uint8_t* rxBuf, uint16_t rxBufLen, uint16_t* rcvLen) { + uint8_t data[(RFAL_NFCV_BLOCKNUM_LEN + RFAL_NFCV_BLOCKNUM_LEN)]; + uint8_t dataLen; + + dataLen = 0U; + + /* Compute Request Data */ + data[dataLen++] = firstBlockNum; /* Set first Block Number */ + data[dataLen++] = numOfBlocks; /* Set number of blocks to read */ + + return rfalNfcvPollerTransceiveReq(RFAL_NFCV_CMD_FAST_READ_MULTIPLE_BLOCKS, + flags, RFAL_NFCV_ST_IC_MFG_CODE, uid, data, + dataLen, rxBuf, rxBufLen, rcvLen); +} + +/*******************************************************************************/ +ReturnCode rfalST25xVPollerFastExtendedReadSingleBlock( + uint8_t flags, const uint8_t* uid, uint16_t blockNum, uint8_t* rxBuf, + uint16_t rxBufLen, uint16_t* rcvLen) { + uint8_t data[RFAL_NFCV_BLOCKNUM_EXTENDED_LEN]; + uint8_t dataLen; + + dataLen = 0U; + + /* Compute Request Data */ + data[dataLen++] = + (uint8_t)blockNum; /* TS T5T 1.0 BNo is considered as a multi-byte field. + TS T5T 1.0 5.1.1.13 multi-byte field follows + [DIGITAL]. [DIGITAL] 9.3.1 A multiple byte field is + transmitted LSB first. */ + data[dataLen++] = (uint8_t)((blockNum >> 8U) & 0xFFU); + + return rfalNfcvPollerTransceiveReq( + RFAL_NFCV_CMD_FAST_EXTENDED_READ_SINGLE_BLOCK, flags, + RFAL_NFCV_ST_IC_MFG_CODE, uid, data, dataLen, rxBuf, rxBufLen, rcvLen); +} + +/*******************************************************************************/ +ReturnCode rfalST25xVPollerFastExtReadMultipleBlocks( + uint8_t flags, const uint8_t* uid, uint16_t firstBlockNum, + uint16_t numOfBlocks, uint8_t* rxBuf, uint16_t rxBufLen, uint16_t* rcvLen) { + uint8_t + data[(RFAL_NFCV_BLOCKNUM_EXTENDED_LEN + RFAL_NFCV_BLOCKNUM_EXTENDED_LEN)]; + uint8_t dataLen; + + dataLen = 0U; + + /* Compute Request Data */ + data[dataLen++] = (uint8_t)((firstBlockNum >> 0U) & 0xFFU); + data[dataLen++] = (uint8_t)((firstBlockNum >> 8U) & 0xFFU); + data[dataLen++] = (uint8_t)((numOfBlocks >> 0U) & 0xFFU); + data[dataLen++] = (uint8_t)((numOfBlocks >> 8U) & 0xFFU); + + return rfalNfcvPollerTransceiveReq( + RFAL_NFCV_CMD_FAST_EXTENDED_READ_MULTIPLE_BLOCKS, flags, + RFAL_NFCV_ST_IC_MFG_CODE, uid, data, dataLen, rxBuf, rxBufLen, rcvLen); +} + +/*******************************************************************************/ +ReturnCode rfalST25xVPollerReadConfiguration(uint8_t flags, const uint8_t* uid, + uint8_t pointer, + uint8_t* regValue) { + return rfalST25xVPollerGenericReadConfiguration( + RFAL_NFCV_CMD_READ_CONFIGURATION, flags, uid, pointer, regValue); +} + +/*******************************************************************************/ +ReturnCode rfalST25xVPollerWriteConfiguration(uint8_t flags, const uint8_t* uid, + uint8_t pointer, + uint8_t regValue) { + return rfalST25xVPollerGenericWriteConfiguration( + RFAL_NFCV_CMD_WRITE_CONFIGURATION, flags, uid, pointer, regValue); +} + +/*******************************************************************************/ +ReturnCode rfalST25xVPollerReadDynamicConfiguration(uint8_t flags, + const uint8_t* uid, + uint8_t pointer, + uint8_t* regValue) { + return rfalST25xVPollerGenericReadConfiguration( + RFAL_NFCV_CMD_READ_DYN_CONFIGURATION, flags, uid, pointer, regValue); +} + +/*******************************************************************************/ +ReturnCode rfalST25xVPollerWriteDynamicConfiguration(uint8_t flags, + const uint8_t* uid, + uint8_t pointer, + uint8_t regValue) { + return rfalST25xVPollerGenericWriteConfiguration( + RFAL_NFCV_CMD_WRITE_DYN_CONFIGURATION, flags, uid, pointer, regValue); +} + +/*******************************************************************************/ +ReturnCode rfalST25xVPollerFastReadDynamicConfiguration(uint8_t flags, + const uint8_t* uid, + uint8_t pointer, + uint8_t* regValue) { + return rfalST25xVPollerGenericReadConfiguration( + RFAL_NFCV_CMD_FAST_READ_DYN_CONFIGURATION, flags, uid, pointer, regValue); +} + +/*******************************************************************************/ +ReturnCode rfalST25xVPollerFastWriteDynamicConfiguration(uint8_t flags, + const uint8_t* uid, + uint8_t pointer, + uint8_t regValue) { + return rfalST25xVPollerGenericWriteConfiguration( + RFAL_NFCV_CMD_FAST_WRITE_DYN_CONFIGURATION, flags, uid, pointer, + regValue); +} + +/*******************************************************************************/ +ReturnCode rfalST25xVPollerPresentPassword(uint8_t flags, const uint8_t* uid, + uint8_t pwdNum, const uint8_t* pwd, + uint8_t pwdLen) { + uint8_t data[RFAL_ST25xV_PWDNUM_LEN + RFAL_ST25xV_PWD_LEN]; + uint8_t dataLen; + uint16_t rcvLen; + rfalNfcvGenericRes res; + + if ((pwdLen > RFAL_ST25xV_PWD_LEN) || (pwd == NULL)) { + return RFAL_ERR_PARAM; + } + + dataLen = 0U; + data[dataLen++] = pwdNum; + if (pwdLen > 0U) { + RFAL_MEMCPY(&data[dataLen], pwd, pwdLen); + } + dataLen += pwdLen; + + return rfalNfcvPollerTransceiveReq( + RFAL_NFCV_CMD_PRESENT_PASSWORD, flags, RFAL_NFCV_ST_IC_MFG_CODE, uid, + data, dataLen, (uint8_t*)&res, sizeof(rfalNfcvGenericRes), &rcvLen); +} + +/*******************************************************************************/ +ReturnCode rfalST25xVPollerWritePassword(uint8_t flags, const uint8_t* uid, + uint8_t pwdNum, const uint8_t* pwd, + uint8_t pwdLen) { + uint8_t data[RFAL_ST25xV_PWDNUM_LEN + RFAL_ST25xV_PWD_LEN]; + uint8_t dataLen; + uint16_t rcvLen; + rfalNfcvGenericRes res; + + if ((pwdLen > RFAL_ST25xV_PWD_LEN) || (pwd == NULL)) { + return RFAL_ERR_PARAM; + } + + dataLen = 0U; + data[dataLen++] = pwdNum; + if (pwdLen > 0U) { + RFAL_MEMCPY(&data[dataLen], pwd, pwdLen); + } + dataLen += pwdLen; + + return rfalNfcvPollerTransceiveReq( + RFAL_NFCV_CMD_WRITE_PASSWORD, flags, RFAL_NFCV_ST_IC_MFG_CODE, uid, data, + dataLen, (uint8_t*)&res, sizeof(rfalNfcvGenericRes), &rcvLen); +} + +/*******************************************************************************/ +ReturnCode rfalST25xVPollerGetRandomNumber(uint8_t flags, const uint8_t* uid, + uint8_t* rxBuf, uint16_t rxBufLen, + uint16_t* rcvLen) { + rfalFieldOff(); + platformDelay(RFAL_ST25TV02K_TRF_OFF); + rfalNfcvPollerInitialize(); + rfalFieldOnAndStartGT(); + platformDelay(RFAL_ST25TV02K_TBOOT_RF); + return rfalNfcvPollerTransceiveReq(RFAL_NFCV_CMD_GET_RANDOM_NUMBER, flags, + RFAL_NFCV_ST_IC_MFG_CODE, uid, NULL, 0U, + rxBuf, rxBufLen, rcvLen); +} + +/*******************************************************************************/ +ReturnCode rfalST25xVPollerWriteMessage(uint8_t flags, const uint8_t* uid, + uint8_t msgLen, const uint8_t* msgData, + uint8_t* txBuf, uint16_t txBufLen) { + return rfalST25xVPollerGenericWriteMessage(RFAL_NFCV_CMD_WRITE_MESSAGE, flags, + uid, msgLen, msgData, txBuf, + txBufLen); +} + +/*******************************************************************************/ +ReturnCode rfalST25xVPollerFastWriteMessage(uint8_t flags, const uint8_t* uid, + uint8_t msgLen, + const uint8_t* msgData, + uint8_t* txBuf, uint16_t txBufLen) { + return rfalST25xVPollerGenericWriteMessage(RFAL_NFCV_CMD_FAST_WRITE_MESSAGE, + flags, uid, msgLen, msgData, txBuf, + txBufLen); +} + +/*******************************************************************************/ +ReturnCode rfalST25xVPollerReadMessageLength(uint8_t flags, const uint8_t* uid, + uint8_t* msgLen) { + return rfalST25xVPollerGenericReadMessageLength( + RFAL_NFCV_CMD_READ_MESSAGE_LENGTH, flags, uid, msgLen); +} + +/*******************************************************************************/ +ReturnCode rfalST25xVPollerFastReadMsgLength(uint8_t flags, const uint8_t* uid, + uint8_t* msgLen) { + return rfalST25xVPollerGenericReadMessageLength( + RFAL_NFCV_CMD_FAST_READ_MESSAGE_LENGTH, flags, uid, msgLen); +} + +/*******************************************************************************/ +ReturnCode rfalST25xVPollerReadMessage(uint8_t flags, const uint8_t* uid, + uint8_t mbPointer, uint8_t numBytes, + uint8_t* rxBuf, uint16_t rxBufLen, + uint16_t* rcvLen) { + return rfalST25xVPollerGenericReadMessage(RFAL_NFCV_CMD_READ_MESSAGE, flags, + uid, mbPointer, numBytes, rxBuf, + rxBufLen, rcvLen); +} + +/*******************************************************************************/ +ReturnCode rfalST25xVPollerFastReadMessage(uint8_t flags, const uint8_t* uid, + uint8_t mbPointer, uint8_t numBytes, + uint8_t* rxBuf, uint16_t rxBufLen, + uint16_t* rcvLen) { + return rfalST25xVPollerGenericReadMessage(RFAL_NFCV_CMD_FAST_READ_MESSAGE, + flags, uid, mbPointer, numBytes, + rxBuf, rxBufLen, rcvLen); +} + +#endif /* RFAL_FEATURE_ST25xV */ diff --git a/core/embed/io/nfc/rfal/source/rfal_t1t.c b/core/embed/io/nfc/rfal/source/rfal_t1t.c new file mode 100644 index 0000000000..60f4048d23 --- /dev/null +++ b/core/embed/io/nfc/rfal/source/rfal_t1t.c @@ -0,0 +1,223 @@ + +/****************************************************************************** + * @attention + * + * COPYRIGHT 2016 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/* + * PROJECT: ST25R391x firmware + * Revision: + * LANGUAGE: ISO C99 + */ + +/*! \file rfal_t1t.c + * + * \author Gustavo Patricio + * + * \brief Provides NFC-A T1T convenience methods and definitions + * + * This module provides an interface to perform as a NFC-A Reader/Writer + * to handle a Type 1 Tag T1T (Topaz) + * + */ + +/* + ****************************************************************************** + * INCLUDES + ****************************************************************************** + */ +#include "rfal_t1t.h" +#include "rfal_utils.h" + +/* + ****************************************************************************** + * ENABLE SWITCH + ****************************************************************************** + */ + +/* Feature switch may be enabled or disabled by user at rfal_platform.h + * Default configuration (ST25R dependant) also provided at rfal_defConfig.h + * + * RFAL_FEATURE_T1T + */ + +#if RFAL_FEATURE_T1T + +/* + ****************************************************************************** + * GLOBAL DEFINES + ****************************************************************************** + */ + +#define RFAL_T1T_DRD_READ \ + (1236U * 2U) /*!< DRD for Reads with n=9 => 1236/fc ~= 91 us \ + T1T 1.2 4.4.2 */ +#define RFAL_T1T_DRD_WRITE \ + 36052U /*!< DRD for Write with n=281 => 36052/fc ~= 2659 us \ + T1T 1.2 4.4.2 */ +#define RFAL_T1T_DRD_WRITE_E \ + 70996U /*!< DRD for Write/Erase with n=554 => 70996/fc ~= 5236 us \ + T1T 1.2 4.4.2 */ + +#define RFAL_T1T_RID_RES_HR0_VAL \ + 0x10U /*!< HR0 indicating NDEF support Digital 2.0 (Candidate) 11.6.2.1 */ +#define RFAL_T1T_RID_RES_HR0_MASK \ + 0xF0U /*!< HR0 most significant nibble mask \ + */ + +/* +****************************************************************************** +* GLOBAL TYPES +****************************************************************************** +*/ + +/*! NFC-A T1T (Topaz) RID_REQ Digital 1.1 10.6.1 & Table 49 */ +typedef struct { + uint8_t cmd; /*!< T1T cmd: RID */ + uint8_t add; /*!< ADD: undefined value */ + uint8_t data; /*!< DATA: undefined value */ + uint8_t uid[RFAL_T1T_UID_LEN]; /*!< UID-echo: undefined value */ +} rfalT1TRidReq; + +/*! NFC-A T1T (Topaz) RALL_REQ T1T 1.2 Table 4 */ +typedef struct { + uint8_t cmd; /*!< T1T cmd: RALL */ + uint8_t add1; /*!< ADD: 0x00 */ + uint8_t add0; /*!< ADD: 0x00 */ + uint8_t uid[RFAL_T1T_UID_LEN]; /*!< UID */ +} rfalT1TRallReq; + +/*! NFC-A T1T (Topaz) WRITE_REQ T1T 1.2 Table 4 */ +typedef struct { + uint8_t cmd; /*!< T1T cmd: RALL */ + uint8_t add; /*!< ADD */ + uint8_t data; /*!< DAT */ + uint8_t uid[RFAL_T1T_UID_LEN]; /*!< UID */ +} rfalT1TWriteReq; + +/*! NFC-A T1T (Topaz) WRITE_RES T1T 1.2 Table 4 */ +typedef struct { + uint8_t add; /*!< ADD */ + uint8_t data; /*!< DAT */ +} rfalT1TWriteRes; + +/* +****************************************************************************** +* LOCAL FUNCTION PROTOTYPES +****************************************************************************** +*/ + +/* +****************************************************************************** +* GLOBAL FUNCTIONS +****************************************************************************** +*/ + +ReturnCode rfalT1TPollerInitialize(void) { + ReturnCode ret; + + RFAL_EXIT_ON_ERR( + ret, rfalSetMode(RFAL_MODE_POLL_NFCA_T1T, RFAL_BR_106, RFAL_BR_106)); + rfalSetErrorHandling(RFAL_ERRORHANDLING_NONE); + + rfalSetGT(RFAL_GT_NONE); /* T1T should only be initialized after NFC-A mode, + therefore the GT has been fulfilled */ + rfalSetFDTListen( + RFAL_FDT_LISTEN_NFCA_POLLER); /* T1T uses NFC-A FDT Listen with n=9 + Digital 1.1 10.7.2 */ + rfalSetFDTPoll(RFAL_FDT_POLL_NFCA_T1T_POLLER); + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalT1TPollerRid(rfalT1TRidRes* ridRes) { + ReturnCode ret; + rfalT1TRidReq ridReq; + uint16_t rcvdLen; + + if (ridRes == NULL) { + return RFAL_ERR_PARAM; + } + + /* Compute RID command and set Undefined Values to 0x00 Digital 1.1 10.6.1 + */ + RFAL_MEMSET(&ridReq, 0x00, sizeof(rfalT1TRidReq)); + ridReq.cmd = (uint8_t)RFAL_T1T_CMD_RID; + + RFAL_EXIT_ON_ERR(ret, rfalTransceiveBlockingTxRx( + (uint8_t*)&ridReq, sizeof(rfalT1TRidReq), + (uint8_t*)ridRes, sizeof(rfalT1TRidRes), &rcvdLen, + RFAL_TXRX_FLAGS_DEFAULT, RFAL_T1T_DRD_READ)); + + /* Check expected RID response length and the HR0 Digital 2.0 + * (Candidate) 11.6.2.1 */ + if ((rcvdLen != sizeof(rfalT1TRidRes)) || + ((ridRes->hr0 & RFAL_T1T_RID_RES_HR0_MASK) != RFAL_T1T_RID_RES_HR0_VAL)) { + return RFAL_ERR_PROTO; + } + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalT1TPollerRall(const uint8_t* uid, uint8_t* rxBuf, + uint16_t rxBufLen, uint16_t* rxRcvdLen) { + rfalT1TRallReq rallReq; + + if ((rxBuf == NULL) || (uid == NULL) || (rxRcvdLen == NULL)) { + return RFAL_ERR_PARAM; + } + + /* Compute RALL command and set Add to 0x00 */ + RFAL_MEMSET(&rallReq, 0x00, sizeof(rfalT1TRallReq)); + rallReq.cmd = (uint8_t)RFAL_T1T_CMD_RALL; + RFAL_MEMCPY(rallReq.uid, uid, RFAL_T1T_UID_LEN); + + return rfalTransceiveBlockingTxRx((uint8_t*)&rallReq, sizeof(rfalT1TRallReq), + (uint8_t*)rxBuf, rxBufLen, rxRcvdLen, + RFAL_TXRX_FLAGS_DEFAULT, RFAL_T1T_DRD_READ); +} + +/*******************************************************************************/ +ReturnCode rfalT1TPollerWrite(const uint8_t* uid, uint8_t address, + uint8_t data) { + rfalT1TWriteReq writeReq; + rfalT1TWriteRes writeRes; + uint16_t rxRcvdLen; + ReturnCode err; + + if (uid == NULL) { + return RFAL_ERR_PARAM; + } + + writeReq.cmd = (uint8_t)RFAL_T1T_CMD_WRITE_E; + writeReq.add = address; + writeReq.data = data; + RFAL_MEMCPY(writeReq.uid, uid, RFAL_T1T_UID_LEN); + + err = rfalTransceiveBlockingTxRx((uint8_t*)&writeReq, sizeof(rfalT1TWriteReq), + (uint8_t*)&writeRes, sizeof(rfalT1TWriteRes), + &rxRcvdLen, RFAL_TXRX_FLAGS_DEFAULT, + RFAL_T1T_DRD_WRITE_E); + + if (err == RFAL_ERR_NONE) { + if ((writeReq.add != writeRes.add) || (writeReq.data != writeRes.data) || + (rxRcvdLen != sizeof(rfalT1TWriteRes))) { + return RFAL_ERR_PROTO; + } + } + return err; +} + +#endif /* RFAL_FEATURE_T1T */ diff --git a/core/embed/io/nfc/rfal/source/rfal_t2t.c b/core/embed/io/nfc/rfal/source/rfal_t2t.c new file mode 100644 index 0000000000..b0a51a035a --- /dev/null +++ b/core/embed/io/nfc/rfal/source/rfal_t2t.c @@ -0,0 +1,231 @@ + +/****************************************************************************** + * @attention + * + * COPYRIGHT 2018 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/* + * PROJECT: ST25R391x firmware + * Revision: + * LANGUAGE: ISO C99 + */ + +/*! \file rfal_t2t.c + * + * \author + * + * \brief Provides NFC-A T2T convenience methods and definitions + * + * This module provides an interface to perform as a NFC-A Reader/Writer + * to handle a Type 2 Tag T2T + * + */ + +/* + ****************************************************************************** + * INCLUDES + ****************************************************************************** + */ +#include "rfal_t2t.h" +#include "rfal_utils.h" + +/* +****************************************************************************** +* ENABLE SWITCH +****************************************************************************** +*/ + +/* Feature switch may be enabled or disabled by user at rfal_platform.h + * Default configuration (ST25R dependant) also provided at rfal_defConfig.h + * + * RFAL_FEATURE_T2T + */ + +#if RFAL_FEATURE_T2T + +/* +****************************************************************************** +* GLOBAL DEFINES +****************************************************************************** +*/ +#define RFAL_FDT_POLL_READ_MAX \ + rfalConvMsTo1fc(5U) /*!< Maximum Wait time for Read command as defined in TS \ + T2T 1.0 table 18 */ +#define RFAL_FDT_POLL_WRITE_MAX \ + rfalConvMsTo1fc(10U) /*!< Maximum Wait time for Write command as defined in \ + TS T2T 1.0 table 18 */ +#define RFAL_FDT_POLL_SL_MAX \ + rfalConvMsTo1fc(1U) /*!< Maximum Wait time for Sector Select as defined in \ + TS T2T 1.0 table 18 */ +#define RFAL_T2T_ACK_NACK_LEN 1U /*!< Len of NACK in bytes (4 bits) */ +#define RFAL_T2T_ACK 0x0AU /*!< ACK value */ +#define RFAL_T2T_ACK_MASK 0x0FU /*!< ACK value */ + +#define RFAL_T2T_SECTOR_SELECT_P1_BYTE2 \ + 0xFFU /*!< Sector Select Packet 1 byte 2 */ +#define RFAL_T2T_SECTOR_SELECT_P2_RFU_LEN 3U /*!< Sector Select RFU length */ + +/* + ****************************************************************************** + * GLOBAL TYPES + ****************************************************************************** + */ + +/*! NFC-A T2T command set T2T 1.0 5.1 */ +typedef enum { + RFAL_T2T_CMD_READ = 0x30, /*!< T2T Read */ + RFAL_T2T_CMD_WRITE = 0xA2, /*!< T2T Write */ + RFAL_T2T_CMD_SECTOR_SELECT = 0xC2 /*!< T2T Sector Select */ +} rfalT2Tcmds; + +/*! NFC-A T2T READ T2T 1.0 5.2 and table 11 */ +typedef struct { + uint8_t code; /*!< Command code */ + uint8_t blNo; /*!< Block number */ +} rfalT2TReadReq; + +/*! NFC-A T2T WRITE T2T 1.0 5.3 and table 12 */ +typedef struct { + uint8_t code; /*!< Command code */ + uint8_t blNo; /*!< Block number */ + uint8_t data[RFAL_T2T_WRITE_DATA_LEN]; /*!< Data */ +} rfalT2TWriteReq; + +/*! NFC-A T2T SECTOR SELECT Packet 1 T2T 1.0 5.4 and table 13 */ +typedef struct { + uint8_t code; /*!< Command code */ + uint8_t byte2; /*!< Sector Select Packet 1 byte 2 */ +} rfalT2TSectorSelectP1Req; + +/*! NFC-A T2T SECTOR SELECT Packet 2 T2T 1.0 5.4 and table 13 */ +typedef struct { + uint8_t secNo; /*!< Block number */ + uint8_t + rfu[RFAL_T2T_SECTOR_SELECT_P2_RFU_LEN]; /*!< Sector Select Packet RFU */ +} rfalT2TSectorSelectP2Req; + +/* + ****************************************************************************** + * GLOBAL FUNCTIONS + ****************************************************************************** + */ + +ReturnCode rfalT2TPollerRead(uint8_t blockNum, uint8_t* rxBuf, + uint16_t rxBufLen, uint16_t* rcvLen) { + ReturnCode ret; + rfalT2TReadReq req; + + if ((rxBuf == NULL) || (rcvLen == NULL)) { + return RFAL_ERR_PARAM; + } + + req.code = (uint8_t)RFAL_T2T_CMD_READ; + req.blNo = blockNum; + + /* Transceive Command */ + ret = rfalTransceiveBlockingTxRx( + (uint8_t*)&req, sizeof(rfalT2TReadReq), rxBuf, rxBufLen, rcvLen, + RFAL_TXRX_FLAGS_DEFAULT, RFAL_FDT_POLL_READ_MAX); + + /* T2T 1.0 5.2.1.7 The Reader/Writer SHALL treat a NACK in response to a READ + * Command as a Protocol Error */ + if ((ret == RFAL_ERR_INCOMPLETE_BYTE) && (*rcvLen == RFAL_T2T_ACK_NACK_LEN) && + ((*rxBuf & RFAL_T2T_ACK_MASK) != RFAL_T2T_ACK)) { + return RFAL_ERR_PROTO; + } + return ret; +} + +/*******************************************************************************/ +ReturnCode rfalT2TPollerWrite(uint8_t blockNum, const uint8_t* wrData) { + ReturnCode ret; + rfalT2TWriteReq req; + uint8_t res; + uint16_t rxLen; + + req.code = (uint8_t)RFAL_T2T_CMD_WRITE; + req.blNo = blockNum; + RFAL_MEMCPY(req.data, wrData, RFAL_T2T_WRITE_DATA_LEN); + + /* Transceive WRITE Command */ + ret = rfalTransceiveBlockingTxRx( + (uint8_t*)&req, sizeof(rfalT2TWriteReq), &res, sizeof(uint8_t), &rxLen, + RFAL_TXRX_FLAGS_DEFAULT, RFAL_FDT_POLL_WRITE_MAX); + + /* Check for a valid ACK */ + if ((ret == RFAL_ERR_INCOMPLETE_BYTE) || (ret == RFAL_ERR_NONE)) { + ret = RFAL_ERR_PROTO; + + if ((rxLen == RFAL_T2T_ACK_NACK_LEN) && + ((res & RFAL_T2T_ACK_MASK) == RFAL_T2T_ACK)) { + ret = RFAL_ERR_NONE; + } + } + + return ret; +} + +/*******************************************************************************/ +ReturnCode rfalT2TPollerSectorSelect(uint8_t sectorNum) { + rfalT2TSectorSelectP1Req p1Req; + rfalT2TSectorSelectP2Req p2Req; + ReturnCode ret; + uint8_t res; + uint16_t rxLen; + + /* Compute SECTOR SELECT Packet 1 */ + p1Req.code = (uint8_t)RFAL_T2T_CMD_SECTOR_SELECT; + p1Req.byte2 = RFAL_T2T_SECTOR_SELECT_P1_BYTE2; + + /* Transceive SECTOR SELECT Packet 1 */ + ret = rfalTransceiveBlockingTxRx( + (uint8_t*)&p1Req, sizeof(rfalT2TSectorSelectP1Req), &res, sizeof(uint8_t), + &rxLen, RFAL_TXRX_FLAGS_DEFAULT, RFAL_FDT_POLL_SL_MAX); + + /* Check and report any transmission error */ + if ((ret != RFAL_ERR_INCOMPLETE_BYTE) && (ret != RFAL_ERR_NONE)) { + return ret; + } + + /* Ensure that an ACK was received */ + if ((ret != RFAL_ERR_INCOMPLETE_BYTE) || (rxLen != RFAL_T2T_ACK_NACK_LEN) || + ((res & RFAL_T2T_ACK_MASK) != RFAL_T2T_ACK)) { + return RFAL_ERR_PROTO; + } + + /* Compute SECTOR SELECT Packet 2 */ + p2Req.secNo = sectorNum; + RFAL_MEMSET(&p2Req.rfu, 0x00, RFAL_T2T_SECTOR_SELECT_P2_RFU_LEN); + + /* Transceive SECTOR SELECT Packet 2 */ + ret = rfalTransceiveBlockingTxRx( + (uint8_t*)&p2Req, sizeof(rfalT2TSectorSelectP2Req), &res, sizeof(uint8_t), + &rxLen, RFAL_TXRX_FLAGS_DEFAULT, RFAL_FDT_POLL_SL_MAX); + + /* T2T 1.0 5.4.1.14 The Reader/Writer SHALL treat any response received before + * the end of PATT2T,SL,MAX as a Protocol Error */ + if ((ret == RFAL_ERR_NONE) || (ret == RFAL_ERR_INCOMPLETE_BYTE)) { + return RFAL_ERR_PROTO; + } + + /* T2T 1.0 5.4.1.13 The Reader/Writer SHALL treat the transmission of the + * SECTOR SELECT Command Packet 2 as being successful when it receives no + * response until PATT2T,SL,MAX. */ + if (ret == RFAL_ERR_TIMEOUT) { + return RFAL_ERR_NONE; + } + + return ret; +} + +#endif /* RFAL_FEATURE_T2T */ diff --git a/core/embed/io/nfc/rfal/source/rfal_t4t.c b/core/embed/io/nfc/rfal/source/rfal_t4t.c new file mode 100644 index 0000000000..76a9c8bf25 --- /dev/null +++ b/core/embed/io/nfc/rfal/source/rfal_t4t.c @@ -0,0 +1,417 @@ + +/****************************************************************************** + * @attention + * + * COPYRIGHT 2018 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/* + * PROJECT: ST25R391x firmware + * Revision: + * LANGUAGE: ISO C99 + */ + +/*! \file rfal_t4t.h + * + * \author Gustavo Patricio + * + * \brief Provides convenience methods and definitions for T4T (ISO7816-4) + * + * This module provides an interface to exchange T4T APDUs according to + * NFC Forum T4T and ISO7816-4 + * + * This implementation was based on the following specs: + * - ISO/IEC 7816-4 3rd Edition 2013-04-15 + * - NFC Forum T4T Technical Specification 1.0 2017-08-28 + * + */ + +/* + ****************************************************************************** + * INCLUDES + ****************************************************************************** + */ +#include "rfal_t4t.h" +#include "rfal_utils.h" + +/* +****************************************************************************** +* ENABLE SWITCH +****************************************************************************** +*/ + +/* Feature switch may be enabled or disabled by user at rfal_platform.h + * Default configuration (ST25R dependant) also provided at rfal_defConfig.h + * + * RFAL_FEATURE_T4T + */ + +#if RFAL_FEATURE_T4T + +/* +****************************************************************************** +* GLOBAL DEFINES +****************************************************************************** +*/ +#define RFAL_T4T_OFFSET_DO \ + 0x54U /*!< Tag value for offset BER-TLV data object */ +#define RFAL_T4T_LENGTH_DO \ + 0x03U /*!< Len value for offset BER-TLV data object */ +#define RFAL_T4T_DATA_DO \ + 0x53U /*!< Tag value for data BER-TLV data object */ + +#define RFAL_T4T_MAX_LC \ + 255U /*!< Maximum Lc value for short Lc coding */ + /* + ****************************************************************************** + * GLOBAL TYPES + ****************************************************************************** + */ + +/* +****************************************************************************** +* GLOBAL MACROS +****************************************************************************** +*/ + +/* + ****************************************************************************** + * LOCAL VARIABLES + ****************************************************************************** + */ + +/* + ****************************************************************************** + * GLOBAL FUNCTIONS + ****************************************************************************** + */ + +/*******************************************************************************/ +ReturnCode rfalT4TPollerComposeCAPDU(const rfalT4tCApduParam *apduParam) { + uint8_t hdrLen; + uint16_t msgIt; + + if ((apduParam == NULL) || (apduParam->cApduBuf == NULL) || + (apduParam->cApduLen == NULL)) { + return RFAL_ERR_PARAM; + } + + msgIt = 0; + *(apduParam->cApduLen) = 0; + + /*******************************************************************************/ + /* Compute Command-APDU according to the format T4T 1.0 5.1.2 & ISO7816-4 + * 2013 Table 1 */ + + /* Check if Data is present */ + if (apduParam->LcFlag) { + if (apduParam->Lc == 0U) { + /* Extented field coding not supported */ + return RFAL_ERR_PARAM; + } + + /* Check whether requested Lc fits */ + if ((uint16_t)apduParam->Lc > + (uint16_t)(RFAL_FEATURE_ISO_DEP_APDU_MAX_LEN - RFAL_T4T_LE_LEN)) { + return RFAL_ERR_PARAM; /* PRQA S 2880 # MISRA 2.1 - Unreachable code due + to configuration option being set/unset */ + } + + /* Calculate the header length a place the data/body where it should be */ + hdrLen = RFAL_T4T_MAX_CAPDU_PROLOGUE_LEN + RFAL_T4T_LC_LEN; + + /* make sure not to exceed buffer size */ + if (((uint16_t)hdrLen + (uint16_t)apduParam->Lc + + (apduParam->LeFlag ? RFAL_T4T_LC_LEN : 0U)) > + RFAL_FEATURE_ISO_DEP_APDU_MAX_LEN) { + return RFAL_ERR_NOMEM; /* PRQA S 2880 # MISRA 2.1 - Unreachable code due + to configuration option being set/unset */ + } + RFAL_MEMMOVE(&apduParam->cApduBuf->apdu[hdrLen], apduParam->cApduBuf->apdu, + apduParam->Lc); + } + + /* Prepend the ADPDU's header */ + apduParam->cApduBuf->apdu[msgIt++] = apduParam->CLA; + apduParam->cApduBuf->apdu[msgIt++] = apduParam->INS; + apduParam->cApduBuf->apdu[msgIt++] = apduParam->P1; + apduParam->cApduBuf->apdu[msgIt++] = apduParam->P2; + + /* Check if Data field length is to be added */ + if (apduParam->LcFlag) { + apduParam->cApduBuf->apdu[msgIt++] = apduParam->Lc; + msgIt += apduParam->Lc; + } + + /* Check if Expected Response Length is to be added */ + if (apduParam->LeFlag) { + apduParam->cApduBuf->apdu[msgIt++] = apduParam->Le; + } + + *(apduParam->cApduLen) = msgIt; + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalT4TPollerParseRAPDU(rfalT4tRApduParam *apduParam) { + if ((apduParam == NULL) || (apduParam->rApduBuf == NULL)) { + return RFAL_ERR_PARAM; + } + + if (apduParam->rcvdLen < RFAL_T4T_MAX_RAPDU_SW1SW2_LEN) { + return RFAL_ERR_PROTO; + } + + apduParam->rApduBodyLen = + (apduParam->rcvdLen - (uint16_t)RFAL_T4T_MAX_RAPDU_SW1SW2_LEN); + apduParam->statusWord = + RFAL_GETU16(&apduParam->rApduBuf->apdu[apduParam->rApduBodyLen]); + + /* Check SW1 SW2 T4T 1.0 5.1.3 NOTE */ + if (apduParam->statusWord == RFAL_T4T_ISO7816_STATUS_COMPLETE) { + return RFAL_ERR_NONE; + } + + return RFAL_ERR_REQUEST; +} + +/*******************************************************************************/ +ReturnCode rfalT4TPollerComposeSelectAppl(rfalIsoDepApduBufFormat *cApduBuf, + const uint8_t *aid, uint8_t aidLen, + uint16_t *cApduLen) { + rfalT4tCApduParam cAPDU; + + if (cApduBuf == NULL) { + return RFAL_ERR_PARAM; + } + + /* CLA INS P1 P2 Lc Data Le */ + /* 00h A4h 00h 00h 07h AID 00h */ + cAPDU.CLA = RFAL_T4T_CLA; + cAPDU.INS = (uint8_t)RFAL_T4T_INS_SELECT; + cAPDU.P1 = RFAL_T4T_ISO7816_P1_SELECT_BY_DF_NAME; + cAPDU.P2 = RFAL_T4T_ISO7816_P2_SELECT_FIRST_OR_ONLY_OCCURENCE | + RFAL_T4T_ISO7816_P2_SELECT_RETURN_FCI_TEMPLATE; + cAPDU.Lc = aidLen; + cAPDU.Le = 0x00; + cAPDU.LcFlag = true; + cAPDU.LeFlag = true; + cAPDU.cApduBuf = cApduBuf; + cAPDU.cApduLen = cApduLen; + + if ((aid != NULL) && (aidLen > 0U)) { + RFAL_MEMCPY(cAPDU.cApduBuf->apdu, aid, aidLen); + } + + return rfalT4TPollerComposeCAPDU(&cAPDU); +} + +/*******************************************************************************/ +ReturnCode rfalT4TPollerComposeSelectFile(rfalIsoDepApduBufFormat *cApduBuf, + const uint8_t *fid, uint8_t fidLen, + uint16_t *cApduLen) { + rfalT4tCApduParam cAPDU; + + if (cApduBuf == NULL) { + return RFAL_ERR_PARAM; + } + + /* CLA INS P1 P2 Lc Data Le */ + /* 00h A4h 00h 0Ch 02h FID - */ + cAPDU.CLA = RFAL_T4T_CLA; + cAPDU.INS = (uint8_t)RFAL_T4T_INS_SELECT; + cAPDU.P1 = RFAL_T4T_ISO7816_P1_SELECT_BY_FILEID; + cAPDU.P2 = RFAL_T4T_ISO7816_P2_SELECT_FIRST_OR_ONLY_OCCURENCE | + RFAL_T4T_ISO7816_P2_SELECT_NO_RESPONSE_DATA; + cAPDU.Lc = fidLen; + cAPDU.Le = 0x00; + cAPDU.LcFlag = true; + cAPDU.LeFlag = false; + cAPDU.cApduBuf = cApduBuf; + cAPDU.cApduLen = cApduLen; + + if ((fid != NULL) && (fidLen > 0U)) { + RFAL_MEMCPY(cAPDU.cApduBuf->apdu, fid, fidLen); + } + + return rfalT4TPollerComposeCAPDU(&cAPDU); +} + +/*******************************************************************************/ +ReturnCode rfalT4TPollerComposeSelectFileV1Mapping( + rfalIsoDepApduBufFormat *cApduBuf, const uint8_t *fid, uint8_t fidLen, + uint16_t *cApduLen) { + rfalT4tCApduParam cAPDU; + + if (cApduBuf == NULL) { + return RFAL_ERR_PARAM; + } + + /* CLA INS P1 P2 Lc Data Le */ + /* 00h A4h 00h 00h 02h FID - */ + cAPDU.CLA = RFAL_T4T_CLA; + cAPDU.INS = (uint8_t)RFAL_T4T_INS_SELECT; + cAPDU.P1 = RFAL_T4T_ISO7816_P1_SELECT_BY_FILEID; + cAPDU.P2 = RFAL_T4T_ISO7816_P2_SELECT_FIRST_OR_ONLY_OCCURENCE | + RFAL_T4T_ISO7816_P2_SELECT_RETURN_FCI_TEMPLATE; + cAPDU.Lc = fidLen; + cAPDU.Le = 0x00; + cAPDU.LcFlag = true; + cAPDU.LeFlag = false; + cAPDU.cApduBuf = cApduBuf; + cAPDU.cApduLen = cApduLen; + + if ((fid != NULL) && (fidLen > 0U)) { + RFAL_MEMCPY(cAPDU.cApduBuf->apdu, fid, fidLen); + } + + return rfalT4TPollerComposeCAPDU(&cAPDU); +} + +/*******************************************************************************/ +ReturnCode rfalT4TPollerComposeReadData(rfalIsoDepApduBufFormat *cApduBuf, + uint16_t offset, uint8_t expLen, + uint16_t *cApduLen) { + rfalT4tCApduParam cAPDU; + + RFAL_MEMSET(&cAPDU, 0x00, sizeof(rfalT4tCApduParam)); + + /* CLA INS P1 P2 Lc Data Le */ + /* 00h B0h [Offset] - - len */ + cAPDU.CLA = RFAL_T4T_CLA; + cAPDU.INS = (uint8_t)RFAL_T4T_INS_READBINARY; + cAPDU.P1 = (uint8_t)((offset >> 8U) & 0xFFU); + cAPDU.P2 = (uint8_t)((offset >> 0U) & 0xFFU); + cAPDU.Le = expLen; + cAPDU.LcFlag = false; + cAPDU.LeFlag = true; + cAPDU.cApduBuf = cApduBuf; + cAPDU.cApduLen = cApduLen; + + return rfalT4TPollerComposeCAPDU(&cAPDU); +} + +/*******************************************************************************/ +ReturnCode rfalT4TPollerComposeReadDataODO(rfalIsoDepApduBufFormat *cApduBuf, + uint32_t offset, uint8_t expLen, + uint16_t *cApduLen) { + rfalT4tCApduParam cAPDU; + uint8_t dataIt; + + /* CLA INS P1 P2 Lc Data Le */ + /* 00h B1h 00h 00h Lc 54 03 xxyyzz len */ + /* [Offset] */ + cAPDU.CLA = RFAL_T4T_CLA; + cAPDU.INS = (uint8_t)RFAL_T4T_INS_READBINARY_ODO; + cAPDU.P1 = 0x00U; + cAPDU.P2 = 0x00U; + cAPDU.Le = expLen; + cAPDU.LcFlag = true; + cAPDU.LeFlag = true; + cAPDU.cApduBuf = cApduBuf; + cAPDU.cApduLen = cApduLen; + + dataIt = 0U; + cApduBuf->apdu[dataIt++] = RFAL_T4T_OFFSET_DO; + cApduBuf->apdu[dataIt++] = RFAL_T4T_LENGTH_DO; + cApduBuf->apdu[dataIt++] = (uint8_t)(offset >> 16U); + cApduBuf->apdu[dataIt++] = (uint8_t)(offset >> 8U); + cApduBuf->apdu[dataIt++] = (uint8_t)(offset); + cAPDU.Lc = dataIt; + + return rfalT4TPollerComposeCAPDU(&cAPDU); +} + +/*******************************************************************************/ +ReturnCode rfalT4TPollerComposeWriteData(rfalIsoDepApduBufFormat *cApduBuf, + uint16_t offset, const uint8_t *data, + uint8_t dataLen, uint16_t *cApduLen) { + rfalT4tCApduParam cAPDU; + + if (cApduBuf == NULL) { + return RFAL_ERR_PARAM; + } + + RFAL_MEMSET(&cAPDU, 0x00, sizeof(rfalT4tCApduParam)); + + /* CLA INS P1 P2 Lc Data Le */ + /* 00h D6h [Offset] len Data - */ + cAPDU.CLA = RFAL_T4T_CLA; + cAPDU.INS = (uint8_t)RFAL_T4T_INS_UPDATEBINARY; + cAPDU.P1 = (uint8_t)((offset >> 8U) & 0xFFU); + cAPDU.P2 = (uint8_t)((offset >> 0U) & 0xFFU); + cAPDU.Lc = dataLen; + cAPDU.LcFlag = true; + cAPDU.LeFlag = false; + cAPDU.cApduBuf = cApduBuf; + cAPDU.cApduLen = cApduLen; + + if ((data != NULL) && (dataLen > 0U)) { + RFAL_MEMCPY(cAPDU.cApduBuf->apdu, data, dataLen); + } + + return rfalT4TPollerComposeCAPDU(&cAPDU); +} + +/*******************************************************************************/ +ReturnCode rfalT4TPollerComposeWriteDataODO(rfalIsoDepApduBufFormat *cApduBuf, + uint32_t offset, + const uint8_t *data, + uint8_t dataLen, + uint16_t *cApduLen) { + rfalT4tCApduParam cAPDU; + uint8_t dataIt; + + if (cApduBuf == NULL) { + return RFAL_ERR_PARAM; + } + + RFAL_MEMSET(&cAPDU, 0x00, sizeof(rfalT4tCApduParam)); + + /* CLA INS P1 P2 Lc Data Le */ + /* 00h D7h 00h 00h len 54 03 xxyyzz 53 Ld data - */ + /* [offset] [data] */ + cAPDU.CLA = RFAL_T4T_CLA; + cAPDU.INS = (uint8_t)RFAL_T4T_INS_UPDATEBINARY_ODO; + cAPDU.P1 = 0x00U; + cAPDU.P2 = 0x00U; + cAPDU.LcFlag = true; + cAPDU.LeFlag = false; + cAPDU.cApduBuf = cApduBuf; + cAPDU.cApduLen = cApduLen; + + dataIt = 0U; + cApduBuf->apdu[dataIt++] = RFAL_T4T_OFFSET_DO; + cApduBuf->apdu[dataIt++] = RFAL_T4T_LENGTH_DO; + cApduBuf->apdu[dataIt++] = (uint8_t)(offset >> 16U); + cApduBuf->apdu[dataIt++] = (uint8_t)(offset >> 8U); + cApduBuf->apdu[dataIt++] = (uint8_t)(offset); + cApduBuf->apdu[dataIt++] = RFAL_T4T_DATA_DO; + cApduBuf->apdu[dataIt++] = dataLen; + + if ((((uint32_t)dataLen + (uint32_t)dataIt) >= RFAL_T4T_MAX_LC) || + (((uint32_t)dataLen + (uint32_t)dataIt) >= + RFAL_FEATURE_ISO_DEP_APDU_MAX_LEN)) { + return (RFAL_ERR_NOMEM); + } + + if ((data != NULL) && (dataLen > 0U)) { + RFAL_MEMCPY(&cAPDU.cApduBuf->apdu[dataIt], data, dataLen); + } + dataIt += dataLen; + cAPDU.Lc = dataIt; + + return rfalT4TPollerComposeCAPDU(&cAPDU); +} + +#endif /* RFAL_FEATURE_T4T */ diff --git a/core/embed/io/nfc/rfal/source/st25r3916/rfal_analogConfigTbl.h b/core/embed/io/nfc/rfal/source/st25r3916/rfal_analogConfigTbl.h new file mode 100644 index 0000000000..de998a9c54 --- /dev/null +++ b/core/embed/io/nfc/rfal/source/st25r3916/rfal_analogConfigTbl.h @@ -0,0 +1,1656 @@ + +/****************************************************************************** + * @attention + * + * COPYRIGHT 2016 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/* + * PROJECT: ST25R3916 firmware + * Revision: + * LANGUAGE: ISO C99 + */ + +/*! \file rfal_analogConfig.h + * + * \author + * + * \brief ST25R3916 Analog Configuration Settings + * + */ + +#ifndef ST25R3916_ANALOGCONFIG_H +#define ST25R3916_ANALOGCONFIG_H + +/* + ****************************************************************************** + * INCLUDES + ****************************************************************************** + */ +#include "rfal_analogConfig.h" +#include "st25r3916_com.h" + +/* + ****************************************************************************** + * DEFINES + ****************************************************************************** + */ + +/* + ****************************************************************************** + * GLOBAL MACROS + ****************************************************************************** + */ + +/*! Macro for Configuration Setting with only one register-mask-value set: + * - Configuration ID[2], Number of Register sets to follow[1], Register[2], + * Mask[1], Value[1] */ +#define MODE_ENTRY_1_REG(MODE, R0, M0, V0) \ + (uint8_t)((uint16_t)(MODE) >> 8U), (uint8_t)((MODE) & 0xFFU), 1, \ + (uint8_t)((uint16_t)(R0) >> 8U), (uint8_t)((R0) & 0xFFU), (uint8_t)(M0), \ + (uint8_t)(V0) + +/*! Macro for Configuration Setting with only two register-mask-value sets: + * - Configuration ID[2], Number of Register sets to follow[1], Register[2], + * Mask[1], Value[1], Register[2], Mask[1], Value[1] */ +#define MODE_ENTRY_2_REG(MODE, R0, M0, V0, R1, M1, V1) \ + (uint8_t)((uint16_t)(MODE) >> 8U), (uint8_t)((MODE) & 0xFFU), 2, \ + (uint8_t)((uint16_t)(R0) >> 8U), (uint8_t)((R0) & 0xFFU), (uint8_t)(M0), \ + (uint8_t)(V0), (uint8_t)((uint16_t)(R1) >> 8U), (uint8_t)((R1) & 0xFFU), \ + (uint8_t)(M1), (uint8_t)(V1) + +/*! Macro for Configuration Setting with only three register-mask-value sets: + * - Configuration ID[2], Number of Register sets to follow[1], Register[2], + * Mask[1], Value[1], Register[2], Mask[1], Value[1], Register[2]... */ +#define MODE_ENTRY_3_REG(MODE, R0, M0, V0, R1, M1, V1, R2, M2, V2) \ + (uint8_t)((uint16_t)(MODE) >> 8U), (uint8_t)((MODE) & 0xFFU), 3, \ + (uint8_t)((uint16_t)(R0) >> 8U), (uint8_t)((R0) & 0xFFU), (uint8_t)(M0), \ + (uint8_t)(V0), (uint8_t)((uint16_t)(R1) >> 8U), (uint8_t)((R1) & 0xFFU), \ + (uint8_t)(M1), (uint8_t)(V1), (uint8_t)((uint16_t)(R2) >> 8U), \ + (uint8_t)((R2) & 0xFFU), (uint8_t)(M2), (uint8_t)(V2) + +/*! Macro for Configuration Setting with only four register-mask-value sets: + * - Configuration ID[2], Number of Register sets to follow[1], Register[2], + * Mask[1], Value[1], Register[2], Mask[1], Value[1], Register[2]... */ +#define MODE_ENTRY_4_REG(MODE, R0, M0, V0, R1, M1, V1, R2, M2, V2, R3, M3, V3) \ + (uint8_t)((uint16_t)(MODE) >> 8U), (uint8_t)((MODE) & 0xFFU), 4, \ + (uint8_t)((uint16_t)(R0) >> 8U), (uint8_t)((R0) & 0xFFU), (uint8_t)(M0), \ + (uint8_t)(V0), (uint8_t)((uint16_t)(R1) >> 8U), (uint8_t)((R1) & 0xFFU), \ + (uint8_t)(M1), (uint8_t)(V1), (uint8_t)((uint16_t)(R2) >> 8U), \ + (uint8_t)((R2) & 0xFFU), (uint8_t)(M2), (uint8_t)(V2), \ + (uint8_t)((uint16_t)(R3) >> 8U), (uint8_t)((R3) & 0xFFU), (uint8_t)(M3), \ + (uint8_t)(V3) + +/*! Macro for Configuration Setting with only five register-mask-value sets: + * - Configuration ID[2], Number of Register sets to follow[1], Register[2], + * Mask[1], Value[1], Register[2], Mask[1], Value[1], Register[2]... */ +#define MODE_ENTRY_5_REG(MODE, R0, M0, V0, R1, M1, V1, R2, M2, V2, R3, M3, V3, \ + R4, M4, V4) \ + (uint8_t)((uint16_t)(MODE) >> 8U), (uint8_t)((MODE) & 0xFFU), 5, \ + (uint8_t)((uint16_t)(R0) >> 8U), (uint8_t)((R0) & 0xFFU), (uint8_t)(M0), \ + (uint8_t)(V0), (uint8_t)((uint16_t)(R1) >> 8U), (uint8_t)((R1) & 0xFFU), \ + (uint8_t)(M1), (uint8_t)(V1), (uint8_t)((uint16_t)(R2) >> 8U), \ + (uint8_t)((R2) & 0xFFU), (uint8_t)(M2), (uint8_t)(V2), \ + (uint8_t)((uint16_t)(R3) >> 8U), (uint8_t)((R3) & 0xFFU), (uint8_t)(M3), \ + (uint8_t)(V3), (uint8_t)((uint16_t)(R4) >> 8U), (uint8_t)((R4) & 0xFFU), \ + (uint8_t)(M4), (uint8_t)(V4) + +/*! Macro for Configuration Setting with only six register-mask-value sets: + * - Configuration ID[2], Number of Register sets to follow[1], Register[2], + * Mask[1], Value[1], Register[2], Mask[1], Value[1], Register[2]... */ +#define MODE_ENTRY_6_REG(MODE, R0, M0, V0, R1, M1, V1, R2, M2, V2, R3, M3, V3, \ + R4, M4, V4, R5, M5, V5) \ + (uint8_t)((uint16_t)(MODE) >> 8U), (uint8_t)((MODE) & 0xFFU), 6, \ + (uint8_t)((uint16_t)(R0) >> 8U), (uint8_t)((R0) & 0xFFU), (uint8_t)(M0), \ + (uint8_t)(V0), (uint8_t)((uint16_t)(R1) >> 8U), (uint8_t)((R1) & 0xFFU), \ + (uint8_t)(M1), (uint8_t)(V1), (uint8_t)((uint16_t)(R2) >> 8U), \ + (uint8_t)((R2) & 0xFFU), (uint8_t)(M2), (uint8_t)(V2), \ + (uint8_t)((uint16_t)(R3) >> 8U), (uint8_t)((R3) & 0xFFU), (uint8_t)(M3), \ + (uint8_t)(V3), (uint8_t)((uint16_t)(R4) >> 8U), (uint8_t)((R4) & 0xFFU), \ + (uint8_t)(M4), (uint8_t)(V4), (uint8_t)((uint16_t)(R5) >> 8U), \ + (uint8_t)((R5) & 0xFFU), (uint8_t)(M5), (uint8_t)(V5) + +/*! Macro for Configuration Setting with only seven register-mask-value sets: + * - Configuration ID[2], Number of Register sets to follow[1], Register[2], + * Mask[1], Value[1], Register[2], Mask[1], Value[1], Register[2]... */ +#define MODE_ENTRY_7_REG(MODE, R0, M0, V0, R1, M1, V1, R2, M2, V2, R3, M3, V3, \ + R4, M4, V4, R5, M5, V5, R6, M6, V6) \ + (uint8_t)((uint16_t)(MODE) >> 8U), (uint8_t)((MODE) & 0xFFU), 7, \ + (uint8_t)((uint16_t)(R0) >> 8U), (uint8_t)((R0) & 0xFFU), (uint8_t)(M0), \ + (uint8_t)(V0), (uint8_t)((uint16_t)(R1) >> 8U), (uint8_t)((R1) & 0xFFU), \ + (uint8_t)(M1), (uint8_t)(V1), (uint8_t)((uint16_t)(R2) >> 8U), \ + (uint8_t)((R2) & 0xFFU), (uint8_t)(M2), (uint8_t)(V2), \ + (uint8_t)((uint16_t)(R3) >> 8U), (uint8_t)((R3) & 0xFFU), (uint8_t)(M3), \ + (uint8_t)(V3), (uint8_t)((uint16_t)(R4) >> 8U), (uint8_t)((R4) & 0xFFU), \ + (uint8_t)(M4), (uint8_t)(V4), (uint8_t)((uint16_t)(R5) >> 8U), \ + (uint8_t)((R5) & 0xFFU), (uint8_t)(M5), (uint8_t)(V5), \ + (uint8_t)((uint16_t)(R6) >> 8U), (uint8_t)((R6) & 0xFFU), (uint8_t)(M6), \ + (uint8_t)(V6) + +/*! Macro for Configuration Setting with only eight register-mask-value sets: + * - Configuration ID[2], Number of Register sets to follow[1], Register[2], + * Mask[1], Value[1], Register[2], Mask[1], Value[1], Register[2]... */ +#define MODE_ENTRY_8_REG(MODE, R0, M0, V0, R1, M1, V1, R2, M2, V2, R3, M3, V3, \ + R4, M4, V4, R5, M5, V5, R6, M6, V6, R7, M7, V7) \ + (uint8_t)((uint16_t)(MODE) >> 8U), (uint8_t)((MODE) & 0xFFU), 8, \ + (uint8_t)((uint16_t)(R0) >> 8U), (uint8_t)((R0) & 0xFFU), (uint8_t)(M0), \ + (uint8_t)(V0), (uint8_t)((uint16_t)(R1) >> 8U), (uint8_t)((R1) & 0xFFU), \ + (uint8_t)(M1), (uint8_t)(V1), (uint8_t)((uint16_t)(R2) >> 8U), \ + (uint8_t)((R2) & 0xFFU), (uint8_t)(M2), (uint8_t)(V2), \ + (uint8_t)((uint16_t)(R3) >> 8U), (uint8_t)((R3) & 0xFFU), (uint8_t)(M3), \ + (uint8_t)(V3), (uint8_t)((uint16_t)(R4) >> 8U), (uint8_t)((R4) & 0xFFU), \ + (uint8_t)(M4), (uint8_t)(V4), (uint8_t)((uint16_t)(R5) >> 8U), \ + (uint8_t)((R5) & 0xFFU), (uint8_t)(M5), (uint8_t)(V5), \ + (uint8_t)((uint16_t)(R6) >> 8U), (uint8_t)((R6) & 0xFFU), (uint8_t)(M6), \ + (uint8_t)(V6), (uint8_t)((uint16_t)(R7) >> 8U), (uint8_t)((R7) & 0xFFU), \ + (uint8_t)(M7), (uint8_t)(V7) + +/*! Macro for Configuration Setting with only nine register-mask-value sets: + * - Configuration ID[2], Number of Register sets to follow[1], Register[2], + * Mask[1], Value[1], Register[2], Mask[1], Value[1], Register[2]... */ +#define MODE_ENTRY_9_REG(MODE, R0, M0, V0, R1, M1, V1, R2, M2, V2, R3, M3, V3, \ + R4, M4, V4, R5, M5, V5, R6, M6, V6, R7, M7, V7, R8, \ + M8, V8) \ + (uint8_t)((uint16_t)(MODE) >> 8U), (uint8_t)((MODE) & 0xFFU), 9, \ + (uint8_t)((uint16_t)(R0) >> 8U), (uint8_t)((R0) & 0xFFU), (uint8_t)(M0), \ + (uint8_t)(V0), (uint8_t)((uint16_t)(R1) >> 8U), (uint8_t)((R1) & 0xFFU), \ + (uint8_t)(M1), (uint8_t)(V1), (uint8_t)((uint16_t)(R2) >> 8U), \ + (uint8_t)((R2) & 0xFFU), (uint8_t)(M2), (uint8_t)(V2), \ + (uint8_t)((uint16_t)(R3) >> 8U), (uint8_t)((R3) & 0xFFU), (uint8_t)(M3), \ + (uint8_t)(V3), (uint8_t)((uint16_t)(R4) >> 8U), (uint8_t)((R4) & 0xFFU), \ + (uint8_t)(M4), (uint8_t)(V4), (uint8_t)((uint16_t)(R5) >> 8U), \ + (uint8_t)((R5) & 0xFFU), (uint8_t)(M5), (uint8_t)(V5), \ + (uint8_t)((uint16_t)(R6) >> 8U), (uint8_t)((R6) & 0xFFU), (uint8_t)(M6), \ + (uint8_t)(V6), (uint8_t)((uint16_t)(R7) >> 8U), (uint8_t)((R7) & 0xFFU), \ + (uint8_t)(M7), (uint8_t)(V7), (uint8_t)((uint16_t)(R8) >> 8U), \ + (uint8_t)((R8) & 0xFFU), (uint8_t)(M8), (uint8_t)(V8) + +/*! Macro for Configuration Setting with only ten register-mask-value sets: + * - Configuration ID[2], Number of Register sets to follow[1], Register[2], + * Mask[1], Value[1], Register[2], Mask[1], Value[1], Register[2]... */ +#define MODE_ENTRY_10_REG(MODE, R0, M0, V0, R1, M1, V1, R2, M2, V2, R3, M3, \ + V3, R4, M4, V4, R5, M5, V5, R6, M6, V6, R7, M7, V7, \ + R8, M8, V8, R9, M9, V9) \ + (uint8_t)((uint16_t)(MODE) >> 8U), (uint8_t)((MODE) & 0xFFU), 10, \ + (uint8_t)((uint16_t)(R0) >> 8U), (uint8_t)((R0) & 0xFFU), (uint8_t)(M0), \ + (uint8_t)(V0), (uint8_t)((uint16_t)(R1) >> 8U), (uint8_t)((R1) & 0xFFU), \ + (uint8_t)(M1), (uint8_t)(V1), (uint8_t)((uint16_t)(R2) >> 8U), \ + (uint8_t)((R2) & 0xFFU), (uint8_t)(M2), (uint8_t)(V2), \ + (uint8_t)((uint16_t)(R3) >> 8U), (uint8_t)((R3) & 0xFFU), (uint8_t)(M3), \ + (uint8_t)(V3), (uint8_t)((uint16_t)(R4) >> 8U), (uint8_t)((R4) & 0xFFU), \ + (uint8_t)(M4), (uint8_t)(V4), (uint8_t)((uint16_t)(R5) >> 8U), \ + (uint8_t)((R5) & 0xFFU), (uint8_t)(M5), (uint8_t)(V5), \ + (uint8_t)((uint16_t)(R6) >> 8U), (uint8_t)((R6) & 0xFFU), (uint8_t)(M6), \ + (uint8_t)(V6), (uint8_t)((uint16_t)(R7) >> 8U), (uint8_t)((R7) & 0xFFU), \ + (uint8_t)(M7), (uint8_t)(V7), (uint8_t)((uint16_t)(R8) >> 8U), \ + (uint8_t)((R8) & 0xFFU), (uint8_t)(M8), (uint8_t)(V8), \ + (uint8_t)((uint16_t)(R9) >> 8U), (uint8_t)((R9) & 0xFFU), (uint8_t)(M9), \ + (uint8_t)(V9) + +/*! Macro for Configuration Setting with eleven register-mask-value sets: + * - Configuration ID[2], Number of Register sets to follow[1], Register[2], + * Mask[1], Value[1], Register[2], Mask[1], Value[1], Register[2]... */ +#define MODE_ENTRY_11_REG(MODE, R0, M0, V0, R1, M1, V1, R2, M2, V2, R3, M3, \ + V3, R4, M4, V4, R5, M5, V5, R6, M6, V6, R7, M7, V7, \ + R8, M8, V8, R9, M9, V9, R10, M10, V10) \ + (uint8_t)((uint16_t)(MODE) >> 8U), (uint8_t)((MODE) & 0xFFU), 11, \ + (uint8_t)((uint16_t)(R0) >> 8U), (uint8_t)((R0) & 0xFFU), (uint8_t)(M0), \ + (uint8_t)(V0), (uint8_t)((uint16_t)(R1) >> 8U), (uint8_t)((R1) & 0xFFU), \ + (uint8_t)(M1), (uint8_t)(V1), (uint8_t)((uint16_t)(R2) >> 8U), \ + (uint8_t)((R2) & 0xFFU), (uint8_t)(M2), (uint8_t)(V2), \ + (uint8_t)((uint16_t)(R3) >> 8U), (uint8_t)((R3) & 0xFFU), (uint8_t)(M3), \ + (uint8_t)(V3), (uint8_t)((uint16_t)(R4) >> 8U), (uint8_t)((R4) & 0xFFU), \ + (uint8_t)(M4), (uint8_t)(V4), (uint8_t)((uint16_t)(R5) >> 8U), \ + (uint8_t)((R5) & 0xFFU), (uint8_t)(M5), (uint8_t)(V5), \ + (uint8_t)((uint16_t)(R6) >> 8U), (uint8_t)((R6) & 0xFFU), (uint8_t)(M6), \ + (uint8_t)(V6), (uint8_t)((uint16_t)(R7) >> 8U), (uint8_t)((R7) & 0xFFU), \ + (uint8_t)(M7), (uint8_t)(V7), (uint8_t)((uint16_t)(R8) >> 8U), \ + (uint8_t)((R8) & 0xFFU), (uint8_t)(M8), (uint8_t)(V8), \ + (uint8_t)((uint16_t)(R9) >> 8U), (uint8_t)((R9) & 0xFFU), (uint8_t)(M9), \ + (uint8_t)(V9), (uint8_t)((uint16_t)(R10) >> 8U), \ + (uint8_t)((R10) & 0xFFU), (uint8_t)(M10), (uint8_t)(V10) + +/*! Macro for Configuration Setting with twelve register-mask-value sets: + * - Configuration ID[2], Number of Register sets to follow[1], Register[2], + * Mask[1], Value[1], Register[2], Mask[1], Value[1], Register[2]... */ +#define MODE_ENTRY_12_REG(MODE, R0, M0, V0, R1, M1, V1, R2, M2, V2, R3, M3, \ + V3, R4, M4, V4, R5, M5, V5, R6, M6, V6, R7, M7, V7, \ + R8, M8, V8, R9, M9, V9, R10, M10, V10, R11, M11, \ + V11) \ + (uint8_t)((uint16_t)(MODE) >> 8U), (uint8_t)((MODE) & 0xFFU), 12, \ + (uint8_t)((uint16_t)(R0) >> 8U), (uint8_t)((R0) & 0xFFU), (uint8_t)(M0), \ + (uint8_t)(V0), (uint8_t)((uint16_t)(R1) >> 8U), (uint8_t)((R1) & 0xFFU), \ + (uint8_t)(M1), (uint8_t)(V1), (uint8_t)((uint16_t)(R2) >> 8U), \ + (uint8_t)((R2) & 0xFFU), (uint8_t)(M2), (uint8_t)(V2), \ + (uint8_t)((uint16_t)(R3) >> 8U), (uint8_t)((R3) & 0xFFU), (uint8_t)(M3), \ + (uint8_t)(V3), (uint8_t)((uint16_t)(R4) >> 8U), (uint8_t)((R4) & 0xFFU), \ + (uint8_t)(M4), (uint8_t)(V4), (uint8_t)((uint16_t)(R5) >> 8U), \ + (uint8_t)((R5) & 0xFFU), (uint8_t)(M5), (uint8_t)(V5), \ + (uint8_t)((uint16_t)(R6) >> 8U), (uint8_t)((R6) & 0xFFU), (uint8_t)(M6), \ + (uint8_t)(V6), (uint8_t)((uint16_t)(R7) >> 8U), (uint8_t)((R7) & 0xFFU), \ + (uint8_t)(M7), (uint8_t)(V7), (uint8_t)((uint16_t)(R8) >> 8U), \ + (uint8_t)((R8) & 0xFFU), (uint8_t)(M8), (uint8_t)(V8), \ + (uint8_t)((uint16_t)(R9) >> 8U), (uint8_t)((R9) & 0xFFU), (uint8_t)(M9), \ + (uint8_t)(V9), (uint8_t)((uint16_t)(R10) >> 8U), \ + (uint8_t)((R10) & 0xFFU), (uint8_t)(M10), (uint8_t)(V10), \ + (uint8_t)((uint16_t)(R11) >> 8U), (uint8_t)((R11) & 0xFFU), \ + (uint8_t)(M11), (uint8_t)(V11) + +/*! Macro for Configuration Setting with thirteen register-mask-value sets: + * - Configuration ID[2], Number of Register sets to follow[1], Register[2], + * Mask[1], Value[1], Register[2], Mask[1], Value[1], Register[2]... */ +#define MODE_ENTRY_13_REG(MODE, R0, M0, V0, R1, M1, V1, R2, M2, V2, R3, M3, \ + V3, R4, M4, V4, R5, M5, V5, R6, M6, V6, R7, M7, V7, \ + R8, M8, V8, R9, M9, V9, R10, M10, V10, R11, M11, \ + V11, R12, M12, V12) \ + (uint8_t)((uint16_t)(MODE) >> 8U), (uint8_t)((MODE) & 0xFFU), 13, \ + (uint8_t)((uint16_t)(R0) >> 8U), (uint8_t)((R0) & 0xFFU), (uint8_t)(M0), \ + (uint8_t)(V0), (uint8_t)((uint16_t)(R1) >> 8U), (uint8_t)((R1) & 0xFFU), \ + (uint8_t)(M1), (uint8_t)(V1), (uint8_t)((uint16_t)(R2) >> 8U), \ + (uint8_t)((R2) & 0xFFU), (uint8_t)(M2), (uint8_t)(V2), \ + (uint8_t)((uint16_t)(R3) >> 8U), (uint8_t)((R3) & 0xFFU), (uint8_t)(M3), \ + (uint8_t)(V3), (uint8_t)((uint16_t)(R4) >> 8U), (uint8_t)((R4) & 0xFFU), \ + (uint8_t)(M4), (uint8_t)(V4), (uint8_t)((uint16_t)(R5) >> 8U), \ + (uint8_t)((R5) & 0xFFU), (uint8_t)(M5), (uint8_t)(V5), \ + (uint8_t)((uint16_t)(R6) >> 8U), (uint8_t)((R6) & 0xFFU), (uint8_t)(M6), \ + (uint8_t)(V6), (uint8_t)((uint16_t)(R7) >> 8U), (uint8_t)((R7) & 0xFFU), \ + (uint8_t)(M7), (uint8_t)(V7), (uint8_t)((uint16_t)(R8) >> 8U), \ + (uint8_t)((R8) & 0xFFU), (uint8_t)(M8), (uint8_t)(V8), \ + (uint8_t)((uint16_t)(R9) >> 8U), (uint8_t)((R9) & 0xFFU), (uint8_t)(M9), \ + (uint8_t)(V9), (uint8_t)((uint16_t)(R10) >> 8U), \ + (uint8_t)((R10) & 0xFFU), (uint8_t)(M10), (uint8_t)(V10), \ + (uint8_t)((uint16_t)(R11) >> 8U), (uint8_t)((R11) & 0xFFU), \ + (uint8_t)(M11), (uint8_t)(V11), (uint8_t)((uint16_t)(R12) >> 8U), \ + (uint8_t)((R12) & 0xFFU), (uint8_t)(M12), (uint8_t)(V12) + +/*! Macro for Configuration Setting with fourteen register-mask-value sets: + * - Configuration ID[2], Number of Register sets to follow[1], Register[2], + * Mask[1], Value[1], Register[2], Mask[1], Value[1], Register[2]... */ +#define MODE_ENTRY_14_REG(MODE, R0, M0, V0, R1, M1, V1, R2, M2, V2, R3, M3, \ + V3, R4, M4, V4, R5, M5, V5, R6, M6, V6, R7, M7, V7, \ + R8, M8, V8, R9, M9, V9, R10, M10, V10, R11, M11, \ + V11, R12, M12, V12, R13, M13, V13) \ + (uint8_t)((uint16_t)(MODE) >> 8), (uint8_t)((MODE) & 0xFFU), 14, \ + (uint8_t)((uint16_t)(R0) >> 8), (uint8_t)((R0) & 0xFFU), (uint8_t)(M0), \ + (uint8_t)(V0), (uint8_t)((uint16_t)(R1) >> 8), (uint8_t)((R1) & 0xFFU), \ + (uint8_t)(M1), (uint8_t)(V1), (uint8_t)((uint16_t)(R2) >> 8), \ + (uint8_t)((R2) & 0xFFU), (uint8_t)(M2), (uint8_t)(V2), \ + (uint8_t)((uint16_t)(R3) >> 8), (uint8_t)((R3) & 0xFFU), (uint8_t)(M3), \ + (uint8_t)(V3), (uint8_t)((uint16_t)(R4) >> 8), (uint8_t)((R4) & 0xFFU), \ + (uint8_t)(M4), (uint8_t)(V4), (uint8_t)((uint16_t)(R5) >> 8), \ + (uint8_t)((R5) & 0xFFU), (uint8_t)(M5), (uint8_t)(V5), \ + (uint8_t)((uint16_t)(R6) >> 8), (uint8_t)((R6) & 0xFFU), (uint8_t)(M6), \ + (uint8_t)(V6), (uint8_t)((uint16_t)(R7) >> 8), (uint8_t)((R7) & 0xFFU), \ + (uint8_t)(M7), (uint8_t)(V7), (uint8_t)((uint16_t)(R8) >> 8), \ + (uint8_t)((R8) & 0xFFU), (uint8_t)(M8), (uint8_t)(V8), \ + (uint8_t)((uint16_t)(R9) >> 8), (uint8_t)((R9) & 0xFFU), (uint8_t)(M9), \ + (uint8_t)(V9), (uint8_t)((uint16_t)(R10) >> 8), \ + (uint8_t)((R10) & 0xFFU), (uint8_t)(M10), (uint8_t)(V10), \ + (uint8_t)((uint16_t)(R11) >> 8), (uint8_t)((R11) & 0xFFU), \ + (uint8_t)(M11), (uint8_t)(V11), (uint8_t)((uint16_t)(R12) >> 8), \ + (uint8_t)((R12) & 0xFFU), (uint8_t)(M12), (uint8_t)(V12), \ + (uint8_t)((uint16_t)(R13) >> 8), (uint8_t)((R13) & 0xFFU), \ + (uint8_t)(M13), (uint8_t)(V13) + +/*! Macro for Configuration Setting with fifteen register-mask-value sets: + * - Configuration ID[2], Number of Register sets to follow[1], Register[2], + * Mask[1], Value[1], Register[2], Mask[1], Value[1], Register[2]... */ +#define MODE_ENTRY_15_REG(MODE, R0, M0, V0, R1, M1, V1, R2, M2, V2, R3, M3, \ + V3, R4, M4, V4, R5, M5, V5, R6, M6, V6, R7, M7, V7, \ + R8, M8, V8, R9, M9, V9, R10, M10, V10, R11, M11, \ + V11, R12, M12, V12, R13, M13, V13, R14, M14, V14) \ + (uint8_t)((uint16_t)(MODE) >> 8), (uint8_t)((MODE) & 0xFFU), 15, \ + (uint8_t)((uint16_t)(R0) >> 8), (uint8_t)((R0) & 0xFFU), (uint8_t)(M0), \ + (uint8_t)(V0), (uint8_t)((uint16_t)(R1) >> 8), (uint8_t)((R1) & 0xFFU), \ + (uint8_t)(M1), (uint8_t)(V1), (uint8_t)((uint16_t)(R2) >> 8), \ + (uint8_t)((R2) & 0xFFU), (uint8_t)(M2), (uint8_t)(V2), \ + (uint8_t)((uint16_t)(R3) >> 8), (uint8_t)((R3) & 0xFFU), (uint8_t)(M3), \ + (uint8_t)(V3), (uint8_t)((uint16_t)(R4) >> 8), (uint8_t)((R4) & 0xFFU), \ + (uint8_t)(M4), (uint8_t)(V4), (uint8_t)((uint16_t)(R5) >> 8), \ + (uint8_t)((R5) & 0xFFU), (uint8_t)(M5), (uint8_t)(V5), \ + (uint8_t)((uint16_t)(R6) >> 8), (uint8_t)((R6) & 0xFFU), (uint8_t)(M6), \ + (uint8_t)(V6), (uint8_t)((uint16_t)(R7) >> 8), (uint8_t)((R7) & 0xFFU), \ + (uint8_t)(M7), (uint8_t)(V7), (uint8_t)((uint16_t)(R8) >> 8), \ + (uint8_t)((R8) & 0xFFU), (uint8_t)(M8), (uint8_t)(V8), \ + (uint8_t)((uint16_t)(R9) >> 8), (uint8_t)((R9) & 0xFFU), (uint8_t)(M9), \ + (uint8_t)(V9), (uint8_t)((uint16_t)(R10) >> 8), \ + (uint8_t)((R10) & 0xFFU), (uint8_t)(M10), (uint8_t)(V10), \ + (uint8_t)((uint16_t)(R11) >> 8), (uint8_t)((R11) & 0xFFU), \ + (uint8_t)(M11), (uint8_t)(V11), (uint8_t)((uint16_t)(R12) >> 8), \ + (uint8_t)((R12) & 0xFFU), (uint8_t)(M12), (uint8_t)(V12), \ + (uint8_t)((uint16_t)(R13) >> 8), (uint8_t)((R13) & 0xFFU), \ + (uint8_t)(M13), (uint8_t)(V13), (uint8_t)((uint16_t)(R14) >> 8), \ + (uint8_t)((R14) & 0xFFU), (uint8_t)(M14), (uint8_t)(V14) + +/*! Macro for Configuration Setting with sixteen register-mask-value sets: + * - Configuration ID[2], Number of Register sets to follow[1], Register[2], + * Mask[1], Value[1], Register[2], Mask[1], Value[1], Register[2]... */ +#define MODE_ENTRY_16_REG( \ + MODE, R0, M0, V0, R1, M1, V1, R2, M2, V2, R3, M3, V3, R4, M4, V4, R5, M5, \ + V5, R6, M6, V6, R7, M7, V7, R8, M8, V8, R9, M9, V9, R10, M10, V10, R11, \ + M11, V11, R12, M12, V12, R13, M13, V13, R14, M14, V14, R15, M15, V15) \ + (uint8_t)((uint16_t)(MODE) >> 8), (uint8_t)((MODE) & 0xFFU), 16, \ + (uint8_t)((uint16_t)(R0) >> 8), (uint8_t)((R0) & 0xFFU), (uint8_t)(M0), \ + (uint8_t)(V0), (uint8_t)((uint16_t)(R1) >> 8), (uint8_t)((R1) & 0xFFU), \ + (uint8_t)(M1), (uint8_t)(V1), (uint8_t)((uint16_t)(R2) >> 8), \ + (uint8_t)((R2) & 0xFFU), (uint8_t)(M2), (uint8_t)(V2), \ + (uint8_t)((uint16_t)(R3) >> 8), (uint8_t)((R3) & 0xFFU), (uint8_t)(M3), \ + (uint8_t)(V3), (uint8_t)((uint16_t)(R4) >> 8), (uint8_t)((R4) & 0xFFU), \ + (uint8_t)(M4), (uint8_t)(V4), (uint8_t)((uint16_t)(R5) >> 8), \ + (uint8_t)((R5) & 0xFFU), (uint8_t)(M5), (uint8_t)(V5), \ + (uint8_t)((uint16_t)(R6) >> 8), (uint8_t)((R6) & 0xFFU), (uint8_t)(M6), \ + (uint8_t)(V6), (uint8_t)((uint16_t)(R7) >> 8), (uint8_t)((R7) & 0xFFU), \ + (uint8_t)(M7), (uint8_t)(V7), (uint8_t)((uint16_t)(R8) >> 8), \ + (uint8_t)((R8) & 0xFFU), (uint8_t)(M8), (uint8_t)(V8), \ + (uint8_t)((uint16_t)(R9) >> 8), (uint8_t)((R9) & 0xFFU), (uint8_t)(M9), \ + (uint8_t)(V9), (uint8_t)((uint16_t)(R10) >> 8), \ + (uint8_t)((R10) & 0xFFU), (uint8_t)(M10), (uint8_t)(V10), \ + (uint8_t)((uint16_t)(R11) >> 8), (uint8_t)((R11) & 0xFFU), \ + (uint8_t)(M11), (uint8_t)(V11), (uint8_t)((uint16_t)(R12) >> 8), \ + (uint8_t)((R12) & 0xFFU), (uint8_t)(M12), (uint8_t)(V12), \ + (uint8_t)((uint16_t)(R13) >> 8), (uint8_t)((R13) & 0xFFU), \ + (uint8_t)(M13), (uint8_t)(V13), (uint8_t)((uint16_t)(R14) >> 8), \ + (uint8_t)((R14) & 0xFFU), (uint8_t)(M14), (uint8_t)(V14), \ + (uint8_t)((uint16_t)(R15) >> 8), (uint8_t)((R15) & 0xFFU), \ + (uint8_t)(M15), (uint8_t)(V15) + +/*! Macro for Configuration Setting with seventeen register-mask-value sets: + * - Configuration ID[2], Number of Register sets to follow[1], Register[2], + * Mask[1], Value[1], Register[2], Mask[1], Value[1], Register[2]... */ +#define MODE_ENTRY_17_REG(MODE, R0, M0, V0, R1, M1, V1, R2, M2, V2, R3, M3, \ + V3, R4, M4, V4, R5, M5, V5, R6, M6, V6, R7, M7, V7, \ + R8, M8, V8, R9, M9, V9, R10, M10, V10, R11, M11, \ + V11, R12, M12, V12, R13, M13, V13, R14, M14, V14, \ + R15, M15, V15, R16, M16, V16) \ + (uint8_t)((uint16_t)(MODE) >> 8), (uint8_t)((MODE) & 0xFFU), 17, \ + (uint8_t)((uint16_t)(R0) >> 8), (uint8_t)((R0) & 0xFFU), (uint8_t)(M0), \ + (uint8_t)(V0), (uint8_t)((uint16_t)(R1) >> 8), (uint8_t)((R1) & 0xFFU), \ + (uint8_t)(M1), (uint8_t)(V1), (uint8_t)((uint16_t)(R2) >> 8), \ + (uint8_t)((R2) & 0xFFU), (uint8_t)(M2), (uint8_t)(V2), \ + (uint8_t)((uint16_t)(R3) >> 8), (uint8_t)((R3) & 0xFFU), (uint8_t)(M3), \ + (uint8_t)(V3), (uint8_t)((uint16_t)(R4) >> 8), (uint8_t)((R4) & 0xFFU), \ + (uint8_t)(M4), (uint8_t)(V4), (uint8_t)((uint16_t)(R5) >> 8), \ + (uint8_t)((R5) & 0xFFU), (uint8_t)(M5), (uint8_t)(V5), \ + (uint8_t)((uint16_t)(R6) >> 8), (uint8_t)((R6) & 0xFFU), (uint8_t)(M6), \ + (uint8_t)(V6), (uint8_t)((uint16_t)(R7) >> 8), (uint8_t)((R7) & 0xFFU), \ + (uint8_t)(M7), (uint8_t)(V7), (uint8_t)((uint16_t)(R8) >> 8), \ + (uint8_t)((R8) & 0xFFU), (uint8_t)(M8), (uint8_t)(V8), \ + (uint8_t)((uint16_t)(R9) >> 8), (uint8_t)((R9) & 0xFFU), (uint8_t)(M9), \ + (uint8_t)(V9), (uint8_t)((uint16_t)(R10) >> 8), \ + (uint8_t)((R10) & 0xFFU), (uint8_t)(M10), (uint8_t)(V10), \ + (uint8_t)((uint16_t)(R11) >> 8), (uint8_t)((R11) & 0xFFU), \ + (uint8_t)(M11), (uint8_t)(V11), (uint8_t)((uint16_t)(R12) >> 8), \ + (uint8_t)((R12) & 0xFFU), (uint8_t)(M12), (uint8_t)(V12), \ + (uint8_t)((uint16_t)(R13) >> 8), (uint8_t)((R13) & 0xFFU), \ + (uint8_t)(M13), (uint8_t)(V13), (uint8_t)((uint16_t)(R14) >> 8), \ + (uint8_t)((R14) & 0xFFU), (uint8_t)(M14), (uint8_t)(V14), \ + (uint8_t)((uint16_t)(R15) >> 8), (uint8_t)((R15) & 0xFFU), \ + (uint8_t)(M15), (uint8_t)(V15), (uint8_t)((uint16_t)(R16) >> 8), \ + (uint8_t)((R16) & 0xFFU), (uint8_t)(M16), (uint8_t)(V16) + +/*! Macro for Configuration Setting with seventeen register-mask-value sets: + * - Configuration ID[2], Number of Register sets to follow[1], Register[2], + * Mask[1], Value[1], Register[2], Mask[1], Value[1], Register[2]... */ +#define MODE_ENTRY_18_REG(MODE, R0, M0, V0, R1, M1, V1, R2, M2, V2, R3, M3, \ + V3, R4, M4, V4, R5, M5, V5, R6, M6, V6, R7, M7, V7, \ + R8, M8, V8, R9, M9, V9, R10, M10, V10, R11, M11, \ + V11, R12, M12, V12, R13, M13, V13, R14, M14, V14, \ + R15, M15, V15, R16, M16, V16, R17, M17, V17) \ + (uint8_t)((uint16_t)(MODE) >> 8), (uint8_t)((MODE) & 0xFFU), 18, \ + (uint8_t)((uint16_t)(R0) >> 8), (uint8_t)((R0) & 0xFFU), (uint8_t)(M0), \ + (uint8_t)(V0), (uint8_t)((uint16_t)(R1) >> 8), (uint8_t)((R1) & 0xFFU), \ + (uint8_t)(M1), (uint8_t)(V1), (uint8_t)((uint16_t)(R2) >> 8), \ + (uint8_t)((R2) & 0xFFU), (uint8_t)(M2), (uint8_t)(V2), \ + (uint8_t)((uint16_t)(R3) >> 8), (uint8_t)((R3) & 0xFFU), (uint8_t)(M3), \ + (uint8_t)(V3), (uint8_t)((uint16_t)(R4) >> 8), (uint8_t)((R4) & 0xFFU), \ + (uint8_t)(M4), (uint8_t)(V4), (uint8_t)((uint16_t)(R5) >> 8), \ + (uint8_t)((R5) & 0xFFU), (uint8_t)(M5), (uint8_t)(V5), \ + (uint8_t)((uint16_t)(R6) >> 8), (uint8_t)((R6) & 0xFFU), (uint8_t)(M6), \ + (uint8_t)(V6), (uint8_t)((uint16_t)(R7) >> 8), (uint8_t)((R7) & 0xFFU), \ + (uint8_t)(M7), (uint8_t)(V7), (uint8_t)((uint16_t)(R8) >> 8), \ + (uint8_t)((R8) & 0xFFU), (uint8_t)(M8), (uint8_t)(V8), \ + (uint8_t)((uint16_t)(R9) >> 8), (uint8_t)((R9) & 0xFFU), (uint8_t)(M9), \ + (uint8_t)(V9), (uint8_t)((uint16_t)(R10) >> 8), \ + (uint8_t)((R10) & 0xFFU), (uint8_t)(M10), (uint8_t)(V10), \ + (uint8_t)((uint16_t)(R11) >> 8), (uint8_t)((R11) & 0xFFU), \ + (uint8_t)(M11), (uint8_t)(V11), (uint8_t)((uint16_t)(R12) >> 8), \ + (uint8_t)((R12) & 0xFFU), (uint8_t)(M12), (uint8_t)(V12), \ + (uint8_t)((uint16_t)(R13) >> 8), (uint8_t)((R13) & 0xFFU), \ + (uint8_t)(M13), (uint8_t)(V13), (uint8_t)((uint16_t)(R14) >> 8), \ + (uint8_t)((R14) & 0xFFU), (uint8_t)(M14), (uint8_t)(V14), \ + (uint8_t)((uint16_t)(R15) >> 8), (uint8_t)((R15) & 0xFFU), \ + (uint8_t)(M15), (uint8_t)(V15), (uint8_t)((uint16_t)(R16) >> 8), \ + (uint8_t)((R16) & 0xFFU), (uint8_t)(M16), (uint8_t)(V16), \ + (uint8_t)((uint16_t)(R17) >> 8), (uint8_t)((R17) & 0xFFU), \ + (uint8_t)(M17), (uint8_t)(V17) + +/*! Macro for Configuration Setting with seventeen register-mask-value sets: + * - Configuration ID[2], Number of Register sets to follow[1], Register[2], + * Mask[1], Value[1], Register[2], Mask[1], Value[1], Register[2]... */ +#define MODE_ENTRY_19_REG( \ + MODE, R0, M0, V0, R1, M1, V1, R2, M2, V2, R3, M3, V3, R4, M4, V4, R5, M5, \ + V5, R6, M6, V6, R7, M7, V7, R8, M8, V8, R9, M9, V9, R10, M10, V10, R11, \ + M11, V11, R12, M12, V12, R13, M13, V13, R14, M14, V14, R15, M15, V15, R16, \ + M16, V16, R17, M17, V17, R18, M18, V18) \ + (uint8_t)((uint16_t)(MODE) >> 8), (uint8_t)((MODE) & 0xFFU), 19, \ + (uint8_t)((uint16_t)(R0) >> 8), (uint8_t)((R0) & 0xFFU), (uint8_t)(M0), \ + (uint8_t)(V0), (uint8_t)((uint16_t)(R1) >> 8), (uint8_t)((R1) & 0xFFU), \ + (uint8_t)(M1), (uint8_t)(V1), (uint8_t)((uint16_t)(R2) >> 8), \ + (uint8_t)((R2) & 0xFFU), (uint8_t)(M2), (uint8_t)(V2), \ + (uint8_t)((uint16_t)(R3) >> 8), (uint8_t)((R3) & 0xFFU), (uint8_t)(M3), \ + (uint8_t)(V3), (uint8_t)((uint16_t)(R4) >> 8), (uint8_t)((R4) & 0xFFU), \ + (uint8_t)(M4), (uint8_t)(V4), (uint8_t)((uint16_t)(R5) >> 8), \ + (uint8_t)((R5) & 0xFFU), (uint8_t)(M5), (uint8_t)(V5), \ + (uint8_t)((uint16_t)(R6) >> 8), (uint8_t)((R6) & 0xFFU), (uint8_t)(M6), \ + (uint8_t)(V6), (uint8_t)((uint16_t)(R7) >> 8), (uint8_t)((R7) & 0xFFU), \ + (uint8_t)(M7), (uint8_t)(V7), (uint8_t)((uint16_t)(R8) >> 8), \ + (uint8_t)((R8) & 0xFFU), (uint8_t)(M8), (uint8_t)(V8), \ + (uint8_t)((uint16_t)(R9) >> 8), (uint8_t)((R9) & 0xFFU), (uint8_t)(M9), \ + (uint8_t)(V9), (uint8_t)((uint16_t)(R10) >> 8), \ + (uint8_t)((R10) & 0xFFU), (uint8_t)(M10), (uint8_t)(V10), \ + (uint8_t)((uint16_t)(R11) >> 8), (uint8_t)((R11) & 0xFFU), \ + (uint8_t)(M11), (uint8_t)(V11), (uint8_t)((uint16_t)(R12) >> 8), \ + (uint8_t)((R12) & 0xFFU), (uint8_t)(M12), (uint8_t)(V12), \ + (uint8_t)((uint16_t)(R13) >> 8), (uint8_t)((R13) & 0xFFU), \ + (uint8_t)(M13), (uint8_t)(V13), (uint8_t)((uint16_t)(R14) >> 8), \ + (uint8_t)((R14) & 0xFFU), (uint8_t)(M14), (uint8_t)(V14), \ + (uint8_t)((uint16_t)(R15) >> 8), (uint8_t)((R15) & 0xFFU), \ + (uint8_t)(M15), (uint8_t)(V15), (uint8_t)((uint16_t)(R16) >> 8), \ + (uint8_t)((R16) & 0xFFU), (uint8_t)(M16), (uint8_t)(V16), \ + (uint8_t)((uint16_t)(R17) >> 8), (uint8_t)((R17) & 0xFFU), \ + (uint8_t)(M17), (uint8_t)(V17), (uint8_t)((uint16_t)(R18) >> 8), \ + (uint8_t)((R18) & 0xFFU), (uint8_t)(M18), (uint8_t)(V18) + +/*! Macro for Configuration Setting with seventeen register-mask-value sets: + * - Configuration ID[2], Number of Register sets to follow[1], Register[2], + * Mask[1], Value[1], Register[2], Mask[1], Value[1], Register[2]... */ +#define MODE_ENTRY_20_REG( \ + MODE, R0, M0, V0, R1, M1, V1, R2, M2, V2, R3, M3, V3, R4, M4, V4, R5, M5, \ + V5, R6, M6, V6, R7, M7, V7, R8, M8, V8, R9, M9, V9, R10, M10, V10, R11, \ + M11, V11, R12, M12, V12, R13, M13, V13, R14, M14, V14, R15, M15, V15, R16, \ + M16, V16, R17, M17, V17, R18, M18, V18, R19, M19, V19) \ + (uint8_t)((uint16_t)(MODE) >> 8), (uint8_t)((MODE) & 0xFFU), 20, \ + (uint8_t)((uint16_t)(R0) >> 8), (uint8_t)((R0) & 0xFFU), (uint8_t)(M0), \ + (uint8_t)(V0), (uint8_t)((uint16_t)(R1) >> 8), (uint8_t)((R1) & 0xFFU), \ + (uint8_t)(M1), (uint8_t)(V1), (uint8_t)((uint16_t)(R2) >> 8), \ + (uint8_t)((R2) & 0xFFU), (uint8_t)(M2), (uint8_t)(V2), \ + (uint8_t)((uint16_t)(R3) >> 8), (uint8_t)((R3) & 0xFFU), (uint8_t)(M3), \ + (uint8_t)(V3), (uint8_t)((uint16_t)(R4) >> 8), (uint8_t)((R4) & 0xFFU), \ + (uint8_t)(M4), (uint8_t)(V4), (uint8_t)((uint16_t)(R5) >> 8), \ + (uint8_t)((R5) & 0xFFU), (uint8_t)(M5), (uint8_t)(V5), \ + (uint8_t)((uint16_t)(R6) >> 8), (uint8_t)((R6) & 0xFFU), (uint8_t)(M6), \ + (uint8_t)(V6), (uint8_t)((uint16_t)(R7) >> 8), (uint8_t)((R7) & 0xFFU), \ + (uint8_t)(M7), (uint8_t)(V7), (uint8_t)((uint16_t)(R8) >> 8), \ + (uint8_t)((R8) & 0xFFU), (uint8_t)(M8), (uint8_t)(V8), \ + (uint8_t)((uint16_t)(R9) >> 8), (uint8_t)((R9) & 0xFFU), (uint8_t)(M9), \ + (uint8_t)(V9), (uint8_t)((uint16_t)(R10) >> 8), \ + (uint8_t)((R10) & 0xFFU), (uint8_t)(M10), (uint8_t)(V10), \ + (uint8_t)((uint16_t)(R11) >> 8), (uint8_t)((R11) & 0xFFU), \ + (uint8_t)(M11), (uint8_t)(V11), (uint8_t)((uint16_t)(R12) >> 8), \ + (uint8_t)((R12) & 0xFFU), (uint8_t)(M12), (uint8_t)(V12), \ + (uint8_t)((uint16_t)(R13) >> 8), (uint8_t)((R13) & 0xFFU), \ + (uint8_t)(M13), (uint8_t)(V13), (uint8_t)((uint16_t)(R14) >> 8), \ + (uint8_t)((R14) & 0xFFU), (uint8_t)(M14), (uint8_t)(V14), \ + (uint8_t)((uint16_t)(R15) >> 8), (uint8_t)((R15) & 0xFFU), \ + (uint8_t)(M15), (uint8_t)(V15), (uint8_t)((uint16_t)(R16) >> 8), \ + (uint8_t)((R16) & 0xFFU), (uint8_t)(M16), (uint8_t)(V16), \ + (uint8_t)((uint16_t)(R17) >> 8), (uint8_t)((R17) & 0xFFU), \ + (uint8_t)(M17), (uint8_t)(V17), (uint8_t)((uint16_t)(R18) >> 8), \ + (uint8_t)((R18) & 0xFFU), (uint8_t)(M18), (uint8_t)(V18), \ + (uint8_t)((uint16_t)(R19) >> 8), (uint8_t)((R19) & 0xFFU), \ + (uint8_t)(M19), (uint8_t)(V19) + +/*! Macro for Configuration Setting with seventeen register-mask-value sets: + * - Configuration ID[2], Number of Register sets to follow[1], Register[2], + * Mask[1], Value[1], Register[2], Mask[1], Value[1], Register[2]... */ +#define MODE_ENTRY_21_REG( \ + MODE, R0, M0, V0, R1, M1, V1, R2, M2, V2, R3, M3, V3, R4, M4, V4, R5, M5, \ + V5, R6, M6, V6, R7, M7, V7, R8, M8, V8, R9, M9, V9, R10, M10, V10, R11, \ + M11, V11, R12, M12, V12, R13, M13, V13, R14, M14, V14, R15, M15, V15, R16, \ + M16, V16, R17, M17, V17, R18, M18, V18, R19, M19, V19, R20, M20, V20) \ + (uint8_t)((uint16_t)(MODE) >> 8), (uint8_t)((MODE) & 0xFFU), 21, \ + (uint8_t)((uint16_t)(R0) >> 8), (uint8_t)((R0) & 0xFFU), (uint8_t)(M0), \ + (uint8_t)(V0), (uint8_t)((uint16_t)(R1) >> 8), (uint8_t)((R1) & 0xFFU), \ + (uint8_t)(M1), (uint8_t)(V1), (uint8_t)((uint16_t)(R2) >> 8), \ + (uint8_t)((R2) & 0xFFU), (uint8_t)(M2), (uint8_t)(V2), \ + (uint8_t)((uint16_t)(R3) >> 8), (uint8_t)((R3) & 0xFFU), (uint8_t)(M3), \ + (uint8_t)(V3), (uint8_t)((uint16_t)(R4) >> 8), (uint8_t)((R4) & 0xFFU), \ + (uint8_t)(M4), (uint8_t)(V4), (uint8_t)((uint16_t)(R5) >> 8), \ + (uint8_t)((R5) & 0xFFU), (uint8_t)(M5), (uint8_t)(V5), \ + (uint8_t)((uint16_t)(R6) >> 8), (uint8_t)((R6) & 0xFFU), (uint8_t)(M6), \ + (uint8_t)(V6), (uint8_t)((uint16_t)(R7) >> 8), (uint8_t)((R7) & 0xFFU), \ + (uint8_t)(M7), (uint8_t)(V7), (uint8_t)((uint16_t)(R8) >> 8), \ + (uint8_t)((R8) & 0xFFU), (uint8_t)(M8), (uint8_t)(V8), \ + (uint8_t)((uint16_t)(R9) >> 8), (uint8_t)((R9) & 0xFFU), (uint8_t)(M9), \ + (uint8_t)(V9), (uint8_t)((uint16_t)(R10) >> 8), \ + (uint8_t)((R10) & 0xFFU), (uint8_t)(M10), (uint8_t)(V10), \ + (uint8_t)((uint16_t)(R11) >> 8), (uint8_t)((R11) & 0xFFU), \ + (uint8_t)(M11), (uint8_t)(V11), (uint8_t)((uint16_t)(R12) >> 8), \ + (uint8_t)((R12) & 0xFFU), (uint8_t)(M12), (uint8_t)(V12), \ + (uint8_t)((uint16_t)(R13) >> 8), (uint8_t)((R13) & 0xFFU), \ + (uint8_t)(M13), (uint8_t)(V13), (uint8_t)((uint16_t)(R14) >> 8), \ + (uint8_t)((R14) & 0xFFU), (uint8_t)(M14), (uint8_t)(V14), \ + (uint8_t)((uint16_t)(R15) >> 8), (uint8_t)((R15) & 0xFFU), \ + (uint8_t)(M15), (uint8_t)(V15), (uint8_t)((uint16_t)(R16) >> 8), \ + (uint8_t)((R16) & 0xFFU), (uint8_t)(M16), (uint8_t)(V16), \ + (uint8_t)((uint16_t)(R17) >> 8), (uint8_t)((R17) & 0xFFU), \ + (uint8_t)(M17), (uint8_t)(V17), (uint8_t)((uint16_t)(R18) >> 8), \ + (uint8_t)((R18) & 0xFFU), (uint8_t)(M18), (uint8_t)(V18), \ + (uint8_t)((uint16_t)(R19) >> 8), (uint8_t)((R19) & 0xFFU), \ + (uint8_t)(M19), (uint8_t)(V19), (uint8_t)((uint16_t)(R20) >> 8), \ + (uint8_t)((R20) & 0xFFU), (uint8_t)(M20), (uint8_t)(V20) + +/*! Macro for Configuration Setting with seventeen register-mask-value sets: + * - Configuration ID[2], Number of Register sets to follow[1], Register[2], + * Mask[1], Value[1], Register[2], Mask[1], Value[1], Register[2]... */ +#define MODE_ENTRY_22_REG( \ + MODE, R0, M0, V0, R1, M1, V1, R2, M2, V2, R3, M3, V3, R4, M4, V4, R5, M5, \ + V5, R6, M6, V6, R7, M7, V7, R8, M8, V8, R9, M9, V9, R10, M10, V10, R11, \ + M11, V11, R12, M12, V12, R13, M13, V13, R14, M14, V14, R15, M15, V15, R16, \ + M16, V16, R17, M17, V17, R18, M18, V18, R19, M19, V19, R20, M20, V20, R21, \ + M21, V21) \ + (uint8_t)((uint16_t)(MODE) >> 8), (uint8_t)((MODE) & 0xFFU), 22, \ + (uint8_t)((uint16_t)(R0) >> 8), (uint8_t)((R0) & 0xFFU), (uint8_t)(M0), \ + (uint8_t)(V0), (uint8_t)((uint16_t)(R1) >> 8), (uint8_t)((R1) & 0xFFU), \ + (uint8_t)(M1), (uint8_t)(V1), (uint8_t)((uint16_t)(R2) >> 8), \ + (uint8_t)((R2) & 0xFFU), (uint8_t)(M2), (uint8_t)(V2), \ + (uint8_t)((uint16_t)(R3) >> 8), (uint8_t)((R3) & 0xFFU), (uint8_t)(M3), \ + (uint8_t)(V3), (uint8_t)((uint16_t)(R4) >> 8), (uint8_t)((R4) & 0xFFU), \ + (uint8_t)(M4), (uint8_t)(V4), (uint8_t)((uint16_t)(R5) >> 8), \ + (uint8_t)((R5) & 0xFFU), (uint8_t)(M5), (uint8_t)(V5), \ + (uint8_t)((uint16_t)(R6) >> 8), (uint8_t)((R6) & 0xFFU), (uint8_t)(M6), \ + (uint8_t)(V6), (uint8_t)((uint16_t)(R7) >> 8), (uint8_t)((R7) & 0xFFU), \ + (uint8_t)(M7), (uint8_t)(V7), (uint8_t)((uint16_t)(R8) >> 8), \ + (uint8_t)((R8) & 0xFFU), (uint8_t)(M8), (uint8_t)(V8), \ + (uint8_t)((uint16_t)(R9) >> 8), (uint8_t)((R9) & 0xFFU), (uint8_t)(M9), \ + (uint8_t)(V9), (uint8_t)((uint16_t)(R10) >> 8), \ + (uint8_t)((R10) & 0xFFU), (uint8_t)(M10), (uint8_t)(V10), \ + (uint8_t)((uint16_t)(R11) >> 8), (uint8_t)((R11) & 0xFFU), \ + (uint8_t)(M11), (uint8_t)(V11), (uint8_t)((uint16_t)(R12) >> 8), \ + (uint8_t)((R12) & 0xFFU), (uint8_t)(M12), (uint8_t)(V12), \ + (uint8_t)((uint16_t)(R13) >> 8), (uint8_t)((R13) & 0xFFU), \ + (uint8_t)(M13), (uint8_t)(V13), (uint8_t)((uint16_t)(R14) >> 8), \ + (uint8_t)((R14) & 0xFFU), (uint8_t)(M14), (uint8_t)(V14), \ + (uint8_t)((uint16_t)(R15) >> 8), (uint8_t)((R15) & 0xFFU), \ + (uint8_t)(M15), (uint8_t)(V15), (uint8_t)((uint16_t)(R16) >> 8), \ + (uint8_t)((R16) & 0xFFU), (uint8_t)(M16), (uint8_t)(V16), \ + (uint8_t)((uint16_t)(R17) >> 8), (uint8_t)((R17) & 0xFFU), \ + (uint8_t)(M17), (uint8_t)(V17), (uint8_t)((uint16_t)(R18) >> 8), \ + (uint8_t)((R18) & 0xFFU), (uint8_t)(M18), (uint8_t)(V18), \ + (uint8_t)((uint16_t)(R19) >> 8), (uint8_t)((R19) & 0xFFU), \ + (uint8_t)(M19), (uint8_t)(V19), (uint8_t)((uint16_t)(R20) >> 8), \ + (uint8_t)((R20) & 0xFFU), (uint8_t)(M20), (uint8_t)(V20), \ + (uint8_t)((uint16_t)(R21) >> 8), (uint8_t)((R21) & 0xFFU), \ + (uint8_t)(M21), (uint8_t)(V21) +/* + ****************************************************************************** + * GLOBAL DATA TYPES + ****************************************************************************** + */ + +#if defined(ST25R3916) +/* PRQA S 3674 2 # CERT ARR02 - Flexible array will be used with sizeof, on + * adding elements error-prone manual update of size would be required */ +/* PRQA S 3406 1 # MISRA 8.6 - Externally generated table included by the + * library */ /* PRQA S 1514 1 # MISRA 8.9 - Externally generated table included by the library */ +const uint8_t rfalAnalogConfigDefaultSettings[] = { + + /****** Default Analog Configuration for Chip-Specific Reset ******/ + MODE_ENTRY_17_REG( + (RFAL_ANALOG_CONFIG_TECH_CHIP | RFAL_ANALOG_CONFIG_CHIP_INIT), + ST25R3916_REG_IO_CONF1, + (ST25R3916_REG_IO_CONF1_out_cl_mask | + ST25R3916_REG_IO_CONF1_lf_clk_off), + 0x07 /* Disable MCU_CLK */ + , + ST25R3916_REG_IO_CONF2, + (ST25R3916_REG_IO_CONF2_miso_pd1 | ST25R3916_REG_IO_CONF2_miso_pd2), + 0x18 /* SPI Pull downs */ + , + ST25R3916_REG_IO_CONF2, ST25R3916_REG_IO_CONF2_aat_en, + ST25R3916_REG_IO_CONF2_aat_en /* Enable AAT */ + , + ST25R3916_REG_TX_DRIVER, ST25R3916_REG_TX_DRIVER_d_res_mask, + 0x00 /* Set RFO resistance Active Tx */ + , + ST25R3916_REG_RES_AM_MOD, 0xFF, 0x80 /* Use minimum non-overlap */ + , + ST25R3916_REG_FIELD_THRESHOLD_ACTV, + ST25R3916_REG_FIELD_THRESHOLD_ACTV_trg_mask, + ST25R3916_REG_FIELD_THRESHOLD_ACTV_trg_105mV /* Lower activation + threshold (higher than + deactivation)*/ + , + ST25R3916_REG_FIELD_THRESHOLD_ACTV, + ST25R3916_REG_FIELD_THRESHOLD_ACTV_rfe_mask, + ST25R3916_REG_FIELD_THRESHOLD_ACTV_rfe_205mV /* Activation threshold as + per DS (higher than + deactivation)*/ + , + ST25R3916_REG_FIELD_THRESHOLD_DEACTV, + ST25R3916_REG_FIELD_THRESHOLD_DEACTV_trg_mask, + ST25R3916_REG_FIELD_THRESHOLD_DEACTV_trg_75mV /* Lower deactivation + threshold */ + , + ST25R3916_REG_FIELD_THRESHOLD_DEACTV, + ST25R3916_REG_FIELD_THRESHOLD_DEACTV_rfe_mask, + ST25R3916_REG_FIELD_THRESHOLD_DEACTV_rfe_150mV /* Lower deactivation + threshold */ + , + ST25R3916_REG_AUX_MOD, ST25R3916_REG_AUX_MOD_lm_ext, + 0x00 /* Disable External Load Modulation */ + , + ST25R3916_REG_AUX_MOD, ST25R3916_REG_AUX_MOD_lm_dri, + ST25R3916_REG_AUX_MOD_lm_dri /* Use internal Load Modulation */ + , + ST25R3916_REG_PASSIVE_TARGET, ST25R3916_REG_PASSIVE_TARGET_fdel_mask, + (5U << ST25R3916_REG_PASSIVE_TARGET_fdel_shift) /* Adjust the FDT to be + aligned with the + bitgrid */ + , + ST25R3916_REG_PT_MOD, + (ST25R3916_REG_PT_MOD_ptm_res_mask | ST25R3916_REG_PT_MOD_pt_res_mask), + 0x5f /* Reduce RFO resistance in Modulated state */ + , + ST25R3916_REG_EMD_SUP_CONF, ST25R3916_REG_EMD_SUP_CONF_rx_start_emv, + ST25R3916_REG_EMD_SUP_CONF_rx_start_emv_on /* Enable start on first 4 + bits */ + , + ST25R3916_REG_ANT_TUNE_A, 0xFF, + 0x82 /* Set Antenna Tuning (Poller): ANTL */ + , + ST25R3916_REG_ANT_TUNE_B, 0xFF, + 0x82 /* Set Antenna Tuning (Poller): ANTL */ + , + 0x84U, 0x10, 0x10 /* Avoid chip internal overheat protection */ + ) + + /****** Default Analog Configuration for Chip-Specific Poll Common ******/ + , + MODE_ENTRY_9_REG( + (RFAL_ANALOG_CONFIG_TECH_CHIP | RFAL_ANALOG_CONFIG_CHIP_POLL_COMMON), + ST25R3916_REG_MODE, ST25R3916_REG_MODE_tr_am, + ST25R3916_REG_MODE_tr_am_am /* Use AM modulation */ + , + ST25R3916_REG_TX_DRIVER, ST25R3916_REG_TX_DRIVER_am_mod_mask, + ST25R3916_REG_TX_DRIVER_am_mod_12percent /* Set Modulation index */ + , + ST25R3916_REG_AUX_MOD, + (ST25R3916_REG_AUX_MOD_dis_reg_am | ST25R3916_REG_AUX_MOD_res_am), + 0x00 /* Use AM via regulator */ + , + ST25R3916_REG_ANT_TUNE_A, 0xFF, + 0x82 /* Set Antenna Tuning (Poller): ANTL */ + , + ST25R3916_REG_ANT_TUNE_B, 0xFF, + 0x82 /* Set Antenna Tuning (Poller): ANTL */ + , + ST25R3916_REG_OVERSHOOT_CONF1, 0xFF, + 0x00 /* Disable Overshoot Protection */ + , + ST25R3916_REG_OVERSHOOT_CONF2, 0xFF, + 0x00 /* Disable Overshoot Protection */ + , + ST25R3916_REG_UNDERSHOOT_CONF1, 0xFF, + 0x00 /* Disable Undershoot Protection */ + , + ST25R3916_REG_UNDERSHOOT_CONF2, 0xFF, + 0x00 /* Disable Undershoot Protection */ + ) + + /****** Default Analog Configuration for Poll NFC-A Rx Common ******/ + , + MODE_ENTRY_1_REG( + (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCA | + RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_RX), + ST25R3916_REG_AUX, ST25R3916_REG_AUX_dis_corr, + ST25R3916_REG_AUX_dis_corr_correlator /* Use Correlator Receiver */ + ) + + /****** Default Analog Configuration for Poll NFC-A Tx 106 ******/ + , + MODE_ENTRY_5_REG((RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCA | + RFAL_ANALOG_CONFIG_BITRATE_106 | RFAL_ANALOG_CONFIG_TX), + ST25R3916_REG_MODE, ST25R3916_REG_MODE_tr_am, + ST25R3916_REG_MODE_tr_am_ook /* Use OOK */ + , + ST25R3916_REG_OVERSHOOT_CONF1, 0xFF, + 0x40 /* Set default Overshoot Protection */ + , + ST25R3916_REG_OVERSHOOT_CONF2, 0xFF, + 0x03 /* Set default Overshoot Protection */ + , + ST25R3916_REG_UNDERSHOOT_CONF1, 0xFF, + 0x40 /* Set default Undershoot Protection */ + , + ST25R3916_REG_UNDERSHOOT_CONF2, 0xFF, + 0x03 /* Set default Undershoot Protection */ + ) + + /****** Default Analog Configuration for Poll NFC-A Rx 106 ******/ + , + MODE_ENTRY_6_REG((RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCA | + RFAL_ANALOG_CONFIG_BITRATE_106 | RFAL_ANALOG_CONFIG_RX), + ST25R3916_REG_RX_CONF1, 0xFF, 0x08, ST25R3916_REG_RX_CONF2, + 0xFF, 0x2D, ST25R3916_REG_RX_CONF3, 0xFF, 0x00, + ST25R3916_REG_RX_CONF4, 0xFF, 0x00, + ST25R3916_REG_CORR_CONF1, 0xFF, 0x51, + ST25R3916_REG_CORR_CONF2, 0xFF, 0x00) + + /****** Default Analog Configuration for Poll NFC-A Tx 212 ******/ + , + MODE_ENTRY_7_REG( + (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCA | + RFAL_ANALOG_CONFIG_BITRATE_212 | RFAL_ANALOG_CONFIG_TX), + ST25R3916_REG_MODE, ST25R3916_REG_MODE_tr_am, + ST25R3916_REG_MODE_tr_am_am /* Use AM modulation */ + , + ST25R3916_REG_AUX_MOD, + (ST25R3916_REG_AUX_MOD_dis_reg_am | ST25R3916_REG_AUX_MOD_res_am), + 0x88 /* Use Resistive AM */ + , + ST25R3916_REG_RES_AM_MOD, ST25R3916_REG_RES_AM_MOD_md_res_mask, + 0x7F /* Set Resistive modulation */ + , + ST25R3916_REG_OVERSHOOT_CONF1, 0xFF, + 0x40 /* Set default Overshoot Protection */ + , + ST25R3916_REG_OVERSHOOT_CONF2, 0xFF, + 0x03 /* Set default Overshoot Protection */ + , + ST25R3916_REG_UNDERSHOOT_CONF1, 0xFF, + 0x40 /* Set default Undershoot Protection */ + , + ST25R3916_REG_UNDERSHOOT_CONF2, 0xFF, + 0x03 /* Set default Undershoot Protection */ + ) + + /****** Default Analog Configuration for Poll NFC-A Rx 212 ******/ + , + MODE_ENTRY_6_REG((RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCA | + RFAL_ANALOG_CONFIG_BITRATE_212 | RFAL_ANALOG_CONFIG_RX), + ST25R3916_REG_RX_CONF1, 0xFF, 0x02, ST25R3916_REG_RX_CONF2, + 0xFF, 0x3D, ST25R3916_REG_RX_CONF3, 0xFF, 0x00, + ST25R3916_REG_RX_CONF4, 0xFF, 0x00, + ST25R3916_REG_CORR_CONF1, 0xFF, 0x14, + ST25R3916_REG_CORR_CONF2, 0xFF, 0x00) + + /****** Default Analog Configuration for Poll NFC-A Tx 424 ******/ + , + MODE_ENTRY_7_REG( + (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCA | + RFAL_ANALOG_CONFIG_BITRATE_424 | RFAL_ANALOG_CONFIG_TX), + ST25R3916_REG_MODE, ST25R3916_REG_MODE_tr_am, + ST25R3916_REG_MODE_tr_am_am /* Use AM modulation */ + , + ST25R3916_REG_AUX_MOD, + (ST25R3916_REG_AUX_MOD_dis_reg_am | ST25R3916_REG_AUX_MOD_res_am), + 0x88 /* Use Resistive AM */ + , + ST25R3916_REG_RES_AM_MOD, ST25R3916_REG_RES_AM_MOD_md_res_mask, + 0x7F /* Set Resistive modulation */ + , + ST25R3916_REG_OVERSHOOT_CONF1, 0xFF, + 0x40 /* Set default Overshoot Protection */ + , + ST25R3916_REG_OVERSHOOT_CONF2, 0xFF, + 0x03 /* Set default Overshoot Protection */ + , + ST25R3916_REG_UNDERSHOOT_CONF1, 0xFF, + 0x40 /* Set default Undershoot Protection */ + , + ST25R3916_REG_UNDERSHOOT_CONF2, 0xFF, + 0x03 /* Set default Undershoot Protection */ + ) + + /****** Default Analog Configuration for Poll NFC-A Rx 424 ******/ + , + MODE_ENTRY_6_REG((RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCA | + RFAL_ANALOG_CONFIG_BITRATE_424 | RFAL_ANALOG_CONFIG_RX), + ST25R3916_REG_RX_CONF1, 0xFF, 0x42, ST25R3916_REG_RX_CONF2, + 0xFF, 0x3D, ST25R3916_REG_RX_CONF3, 0xFF, 0x00, + ST25R3916_REG_RX_CONF4, 0xFF, 0x00, + ST25R3916_REG_CORR_CONF1, 0xFF, 0x54, + ST25R3916_REG_CORR_CONF2, 0xFF, 0x00) + + /****** Default Analog Configuration for Poll NFC-A Tx 848 ******/ + , + MODE_ENTRY_7_REG( + (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCA | + RFAL_ANALOG_CONFIG_BITRATE_848 | RFAL_ANALOG_CONFIG_TX), + ST25R3916_REG_MODE, ST25R3916_REG_MODE_tr_am, + ST25R3916_REG_MODE_tr_am_am /* Use AM modulation */ + , + ST25R3916_REG_TX_DRIVER, ST25R3916_REG_TX_DRIVER_am_mod_mask, + ST25R3916_REG_TX_DRIVER_am_mod_40percent /* Set Modulation index */ + , + ST25R3916_REG_AUX_MOD, + (ST25R3916_REG_AUX_MOD_dis_reg_am | ST25R3916_REG_AUX_MOD_res_am), + 0x00 /* Use AM via regulator */ + , + ST25R3916_REG_OVERSHOOT_CONF1, 0xFF, + 0x00 /* Disable Overshoot Protection */ + , + ST25R3916_REG_OVERSHOOT_CONF2, 0xFF, + 0x00 /* Disable Overshoot Protection */ + , + ST25R3916_REG_UNDERSHOOT_CONF1, 0xFF, + 0x00 /* Disable Undershoot Protection */ + , + ST25R3916_REG_UNDERSHOOT_CONF2, 0xFF, + 0x00 /* Disable Undershoot Protection */ + ) + + /****** Default Analog Configuration for Poll NFC-A Rx 848 ******/ + , + MODE_ENTRY_6_REG((RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCA | + RFAL_ANALOG_CONFIG_BITRATE_848 | RFAL_ANALOG_CONFIG_RX), + ST25R3916_REG_RX_CONF1, 0xFF, 0x42, ST25R3916_REG_RX_CONF2, + 0xFF, 0x3D, ST25R3916_REG_RX_CONF3, 0xFF, 0x00, + ST25R3916_REG_RX_CONF4, 0xFF, 0x00, + ST25R3916_REG_CORR_CONF1, 0xFF, 0x44, + ST25R3916_REG_CORR_CONF2, 0xFF, 0x00) + + /****** Default Analog Configuration for Poll NFC-A Anticolision setting + ******/ + , + MODE_ENTRY_1_REG( + (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCA | + RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_ANTICOL), + ST25R3916_REG_CORR_CONF1, ST25R3916_REG_CORR_CONF1_corr_s6, + 0x00 /* Set collision detection level different from data */ + ) + +#ifdef RFAL_USE_COHE + /****** Default Analog Configuration for Poll NFC-B Rx Common ******/ + , + MODE_ENTRY_1_REG( + (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCB | + RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_RX), + ST25R3916_REG_AUX, ST25R3916_REG_AUX_dis_corr, + ST25R3916_REG_AUX_dis_corr_coherent /* Use Coherent Receiver */ + ) +#else + /****** Default Analog Configuration for Poll NFC-B Rx Common ******/ + , + MODE_ENTRY_1_REG( + (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCB | + RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_RX), + ST25R3916_REG_AUX, ST25R3916_REG_AUX_dis_corr, + ST25R3916_REG_AUX_dis_corr_correlator /* Use Correlator Receiver */ + ) +#endif /*RFAL_USE_COHE*/ + + /****** Default Analog Configuration for Poll NFC-B Rx 106 ******/ + , + MODE_ENTRY_6_REG((RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCB | + RFAL_ANALOG_CONFIG_BITRATE_106 | RFAL_ANALOG_CONFIG_RX), + ST25R3916_REG_RX_CONF1, 0xFF, 0x04, ST25R3916_REG_RX_CONF2, + 0xFF, 0x3D, ST25R3916_REG_RX_CONF3, 0xFF, 0x00, + ST25R3916_REG_RX_CONF4, 0xFF, 0x00, + ST25R3916_REG_CORR_CONF1, 0xFF, 0x1B, + ST25R3916_REG_CORR_CONF2, 0xFF, 0x00) + + /****** Default Analog Configuration for Poll NFC-B Rx 212 ******/ + , + MODE_ENTRY_6_REG((RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCB | + RFAL_ANALOG_CONFIG_BITRATE_212 | RFAL_ANALOG_CONFIG_RX), + ST25R3916_REG_RX_CONF1, 0xFF, 0x02, ST25R3916_REG_RX_CONF2, + 0xFF, 0x3D, ST25R3916_REG_RX_CONF3, 0xFF, 0x00, + ST25R3916_REG_RX_CONF4, 0xFF, 0x00, + ST25R3916_REG_CORR_CONF1, 0xFF, 0x14, + ST25R3916_REG_CORR_CONF2, 0xFF, 0x00) + + /****** Default Analog Configuration for Poll NFC-B Rx 424 ******/ + , + MODE_ENTRY_6_REG((RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCB | + RFAL_ANALOG_CONFIG_BITRATE_424 | RFAL_ANALOG_CONFIG_RX), + ST25R3916_REG_RX_CONF1, 0xFF, 0x42, ST25R3916_REG_RX_CONF2, + 0xFF, 0x3D, ST25R3916_REG_RX_CONF3, 0xFF, 0x00, + ST25R3916_REG_RX_CONF4, 0xFF, 0x00, + ST25R3916_REG_CORR_CONF1, 0xFF, 0x54, + ST25R3916_REG_CORR_CONF2, 0xFF, 0x00) + + /****** Default Analog Configuration for Poll NFC-B Rx 848 ******/ + , + MODE_ENTRY_6_REG((RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCB | + RFAL_ANALOG_CONFIG_BITRATE_848 | RFAL_ANALOG_CONFIG_RX), + ST25R3916_REG_RX_CONF1, 0xFF, 0x42, ST25R3916_REG_RX_CONF2, + 0xFF, 0x3D, ST25R3916_REG_RX_CONF3, 0xFF, 0x00, + ST25R3916_REG_RX_CONF4, 0xFF, 0x00, + ST25R3916_REG_CORR_CONF1, 0xFF, 0x44, + ST25R3916_REG_CORR_CONF2, 0xFF, 0x00) +#ifdef RFAL_USE_COHE + + /****** Default Analog Configuration for Poll NFC-F Rx Common ******/ + , + MODE_ENTRY_7_REG( + (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCF | + RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_RX), + ST25R3916_REG_AUX, ST25R3916_REG_AUX_dis_corr, + ST25R3916_REG_AUX_dis_corr_coherent /* Use Pulse Receiver */ + , + ST25R3916_REG_RX_CONF1, 0xFF, 0x13, ST25R3916_REG_RX_CONF2, 0xFF, 0x3D, + ST25R3916_REG_RX_CONF3, 0xFF, 0x00, ST25R3916_REG_RX_CONF4, 0xFF, 0x00, + ST25R3916_REG_CORR_CONF1, 0xFF, 0x54, ST25R3916_REG_CORR_CONF2, 0xFF, + 0x00) +#else + /****** Default Analog Configuration for Poll NFC-F Rx Common ******/ + , + MODE_ENTRY_7_REG( + (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCF | + RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_RX), + ST25R3916_REG_AUX, ST25R3916_REG_AUX_dis_corr, + ST25R3916_REG_AUX_dis_corr_correlator /* Use Correlator Receiver */ + , + ST25R3916_REG_RX_CONF1, 0xFF, 0x13, ST25R3916_REG_RX_CONF2, 0xFF, 0x3D, + ST25R3916_REG_RX_CONF3, 0xFF, 0x00, ST25R3916_REG_RX_CONF4, 0xFF, 0x00, + ST25R3916_REG_CORR_CONF1, 0xFF, 0x54, ST25R3916_REG_CORR_CONF2, 0xFF, + 0x00) +#endif /*RFAL_USE_COHE*/ + + , + MODE_ENTRY_1_REG((RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCV | + RFAL_ANALOG_CONFIG_BITRATE_26 | RFAL_ANALOG_CONFIG_TX), + ST25R3916_REG_MODE, ST25R3916_REG_MODE_tr_am, + ST25R3916_REG_MODE_tr_am_ook /* Use OOK */ + ) + +#ifdef RFAL_USE_COHE + /****** Default Analog Configuration for Poll NFC-V Rx Common ******/ + , + MODE_ENTRY_7_REG( + (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCV | + RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_RX), + ST25R3916_REG_AUX, ST25R3916_REG_AUX_dis_corr, + ST25R3916_REG_AUX_dis_corr_coherent /* Use Pulse Receiver */ + , + ST25R3916_REG_RX_CONF1, 0xFF, 0x13, ST25R3916_REG_RX_CONF2, 0xFF, 0x2D, + ST25R3916_REG_RX_CONF3, 0xFF, 0x00, ST25R3916_REG_RX_CONF4, 0xFF, 0x00, + ST25R3916_REG_CORR_CONF1, 0xFF, 0x13, ST25R3916_REG_CORR_CONF2, 0xFF, + 0x01) +#else + /****** Default Analog Configuration for Poll NFC-V Rx Common ******/ + , + MODE_ENTRY_7_REG( + (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCV | + RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_RX), + ST25R3916_REG_AUX, ST25R3916_REG_AUX_dis_corr, + ST25R3916_REG_AUX_dis_corr_correlator /* Use Correlator Receiver */ + , + ST25R3916_REG_RX_CONF1, 0xFF, 0x13, ST25R3916_REG_RX_CONF2, 0xFF, 0x2D, + ST25R3916_REG_RX_CONF3, 0xFF, 0x00, ST25R3916_REG_RX_CONF4, 0xFF, 0x00, + ST25R3916_REG_CORR_CONF1, 0xFF, 0x13, ST25R3916_REG_CORR_CONF2, 0xFF, + 0x01) +#endif /*RFAL_USE_COHE*/ + + /****** Default Analog Configuration for Poll AP2P Tx 106 ******/ + , + MODE_ENTRY_5_REG((RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_AP2P | + RFAL_ANALOG_CONFIG_BITRATE_106 | RFAL_ANALOG_CONFIG_TX), + ST25R3916_REG_MODE, ST25R3916_REG_MODE_tr_am, + ST25R3916_REG_MODE_tr_am_ook /* Use OOK modulation */ + , + ST25R3916_REG_OVERSHOOT_CONF1, 0xFF, + 0x40 /* Set default Overshoot Protection */ + , + ST25R3916_REG_OVERSHOOT_CONF2, 0xFF, + 0x03 /* Set default Overshoot Protection */ + , + ST25R3916_REG_UNDERSHOOT_CONF1, 0xFF, + 0x40 /* Set default Undershoot Protection */ + , + ST25R3916_REG_UNDERSHOOT_CONF2, 0xFF, + 0x03 /* Set default Undershoot Protection */ + ) + + /****** Default Analog Configuration for Poll AP2P Tx 212 ******/ + , + MODE_ENTRY_1_REG((RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_AP2P | + RFAL_ANALOG_CONFIG_BITRATE_212 | RFAL_ANALOG_CONFIG_TX), + ST25R3916_REG_MODE, ST25R3916_REG_MODE_tr_am, + ST25R3916_REG_MODE_tr_am_am /* Use AM modulation */ + ) + + /****** Default Analog Configuration for Poll AP2P Tx 424 ******/ + , + MODE_ENTRY_1_REG((RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_AP2P | + RFAL_ANALOG_CONFIG_BITRATE_424 | RFAL_ANALOG_CONFIG_TX), + ST25R3916_REG_MODE, ST25R3916_REG_MODE_tr_am, + ST25R3916_REG_MODE_tr_am_am /* Use AM modulation */ + ) + + /****** Default Analog Configuration for Chip-Specific Listen On ******/ + , + MODE_ENTRY_6_REG( + (RFAL_ANALOG_CONFIG_TECH_CHIP | RFAL_ANALOG_CONFIG_CHIP_LISTEN_ON), + ST25R3916_REG_ANT_TUNE_A, 0xFF, + 0x00 /* Set Antenna Tuning (Listener): ANTL */ + , + ST25R3916_REG_ANT_TUNE_B, 0xFF, + 0xff /* Set Antenna Tuning (Listener): ANTL */ + , + ST25R3916_REG_OVERSHOOT_CONF1, 0xFF, + 0x00 /* Disable Overshoot Protection */ + , + ST25R3916_REG_OVERSHOOT_CONF2, 0xFF, + 0x00 /* Disable Overshoot Protection */ + , + ST25R3916_REG_UNDERSHOOT_CONF1, 0xFF, + 0x00 /* Disable Undershoot Protection */ + , + ST25R3916_REG_UNDERSHOOT_CONF2, 0xFF, + 0x00 /* Disable Undershoot Protection */ + ) + + /****** Default Analog Configuration for Listen AP2P Tx Common ******/ + , + MODE_ENTRY_7_REG( + (RFAL_ANALOG_CONFIG_LISTEN | RFAL_ANALOG_CONFIG_TECH_AP2P | + RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_TX), + ST25R3916_REG_ANT_TUNE_A, 0xFF, + 0x82 /* Set Antenna Tuning (Poller): ANTL */ + , + ST25R3916_REG_ANT_TUNE_B, 0xFF, + 0x82 /* Set Antenna Tuning (Poller): ANTL */ + , + ST25R3916_REG_TX_DRIVER, ST25R3916_REG_TX_DRIVER_am_mod_mask, + ST25R3916_REG_TX_DRIVER_am_mod_12percent /* Set Modulation index */ + , + ST25R3916_REG_OVERSHOOT_CONF1, 0xFF, + 0x00 /* Disable Overshoot Protection */ + , + ST25R3916_REG_OVERSHOOT_CONF2, 0xFF, + 0x00 /* Disable Overshoot Protection */ + , + ST25R3916_REG_UNDERSHOOT_CONF1, 0xFF, + 0x00 /* Disable Undershoot Protection */ + , + ST25R3916_REG_UNDERSHOOT_CONF2, 0xFF, + 0x00 /* Disable Undershoot Protection */ + ) + + /****** Default Analog Configuration for Listen AP2P Rx Common ******/ + , + MODE_ENTRY_3_REG( + (RFAL_ANALOG_CONFIG_LISTEN | RFAL_ANALOG_CONFIG_TECH_AP2P | + RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_RX), + ST25R3916_REG_RX_CONF1, ST25R3916_REG_RX_CONF1_lp_mask, + ST25R3916_REG_RX_CONF1_lp_1200khz /* Set Rx filter configuration */ + , + ST25R3916_REG_RX_CONF1, ST25R3916_REG_RX_CONF1_hz_mask, + ST25R3916_REG_RX_CONF1_hz_12_200khz /* Set Rx filter configuration */ + , + ST25R3916_REG_RX_CONF2, ST25R3916_REG_RX_CONF2_amd_sel, + ST25R3916_REG_RX_CONF2_amd_sel_mixer /* AM demodulator: mixer */ + ) + + /****** Default Analog Configuration for Listen AP2P Tx 106 ******/ + , + MODE_ENTRY_5_REG((RFAL_ANALOG_CONFIG_LISTEN | RFAL_ANALOG_CONFIG_TECH_AP2P | + RFAL_ANALOG_CONFIG_BITRATE_106 | RFAL_ANALOG_CONFIG_TX), + ST25R3916_REG_MODE, ST25R3916_REG_MODE_tr_am, + ST25R3916_REG_MODE_tr_am_ook /* Use OOK modulation */ + , + ST25R3916_REG_OVERSHOOT_CONF1, 0xFF, + 0x40 /* Set default Overshoot Protection */ + , + ST25R3916_REG_OVERSHOOT_CONF2, 0xFF, + 0x03 /* Set default Overshoot Protection */ + , + ST25R3916_REG_UNDERSHOOT_CONF1, 0xFF, + 0x40 /* Set default Undershoot Protection */ + , + ST25R3916_REG_UNDERSHOOT_CONF2, 0xFF, + 0x03 /* Set default Undershoot Protection */ + ) + + /****** Default Analog Configuration for Listen AP2P Tx 212 ******/ + , + MODE_ENTRY_1_REG((RFAL_ANALOG_CONFIG_LISTEN | RFAL_ANALOG_CONFIG_TECH_AP2P | + RFAL_ANALOG_CONFIG_BITRATE_212 | RFAL_ANALOG_CONFIG_TX), + ST25R3916_REG_MODE, ST25R3916_REG_MODE_tr_am, + ST25R3916_REG_MODE_tr_am_am /* Use AM modulation */ + ) + + /****** Default Analog Configuration for Listen AP2P Tx 424 ******/ + , + MODE_ENTRY_1_REG((RFAL_ANALOG_CONFIG_LISTEN | RFAL_ANALOG_CONFIG_TECH_AP2P | + RFAL_ANALOG_CONFIG_BITRATE_424 | RFAL_ANALOG_CONFIG_TX), + ST25R3916_REG_MODE, ST25R3916_REG_MODE_tr_am, + ST25R3916_REG_MODE_tr_am_am /* Use AM modulation */ + ) + +}; + +/*******************************************************************************/ +#elif defined(ST25R3916B) + +/* PRQA S 3674 2 # CERT ARR02 - Flexible array will be used with sizeof, on + * adding elements error-prone manual update of size would be required */ +/* PRQA S 3406 1 # MISRA 8.6 - Externally generated table included by the + * library */ /* PRQA S 1514 1 # MISRA 8.9 - Externally generated table included by the library */ +const uint8_t rfalAnalogConfigDefaultSettings[] = { + + /****** Default Analog Configuration for Chip-Specific Reset ******/ + MODE_ENTRY_20_REG( + (RFAL_ANALOG_CONFIG_TECH_CHIP | RFAL_ANALOG_CONFIG_CHIP_INIT), + ST25R3916_REG_IO_CONF1, + (ST25R3916_REG_IO_CONF1_out_cl_mask | + ST25R3916_REG_IO_CONF1_lf_clk_off), + 0x07 /* Disable MCU_CLK */ + , + ST25R3916_REG_IO_CONF2, + (ST25R3916_REG_IO_CONF2_miso_pd1 | ST25R3916_REG_IO_CONF2_miso_pd2), + 0x18 /* SPI Pull downs */ + , + ST25R3916_REG_IO_CONF2, ST25R3916_REG_IO_CONF2_aat_en, + ST25R3916_REG_IO_CONF2_aat_en /* Enable AAT */ + , + ST25R3916_REG_TX_DRIVER, ST25R3916_REG_TX_DRIVER_d_res_mask, + 0x00 /* Set RFO resistance Active Tx */ + , + ST25R3916_REG_RES_AM_MOD, ST25R3916_REG_RES_AM_MOD_fa3_f, + 0x80 /* Use minimum non-overlap */ + , + ST25R3916_REG_FIELD_THRESHOLD_ACTV, + ST25R3916_REG_FIELD_THRESHOLD_ACTV_trg_mask, + ST25R3916_REG_FIELD_THRESHOLD_ACTV_trg_105mV /* Lower activation + threshold (higher than + deactivation)*/ + , + ST25R3916_REG_FIELD_THRESHOLD_ACTV, + ST25R3916_REG_FIELD_THRESHOLD_ACTV_rfe_mask, + ST25R3916_REG_FIELD_THRESHOLD_ACTV_rfe_205mV /* Activation threshold as + per DS (higher than + deactivation)*/ + , + ST25R3916_REG_FIELD_THRESHOLD_DEACTV, + ST25R3916_REG_FIELD_THRESHOLD_DEACTV_trg_mask, + ST25R3916_REG_FIELD_THRESHOLD_DEACTV_trg_75mV /* Lower deactivation + threshold */ + , + ST25R3916_REG_FIELD_THRESHOLD_DEACTV, + ST25R3916_REG_FIELD_THRESHOLD_DEACTV_rfe_mask, + ST25R3916_REG_FIELD_THRESHOLD_DEACTV_rfe_150mV /* Lower deactivation + threshold */ + , + ST25R3916_REG_AUX_MOD, ST25R3916_REG_AUX_MOD_lm_ext, + 0x00 /* Disable External Load Modulation */ + , + ST25R3916_REG_AUX_MOD, ST25R3916_REG_AUX_MOD_lm_dri, + ST25R3916_REG_AUX_MOD_lm_dri /* Use internal Load Modulation */ + , + ST25R3916_REG_PASSIVE_TARGET, ST25R3916_REG_PASSIVE_TARGET_fdel_mask, + (5U << ST25R3916_REG_PASSIVE_TARGET_fdel_shift) /* Adjust the FDT to be + aligned with the + bitgrid */ + , + ST25R3916_REG_PT_MOD, + (ST25R3916_REG_PT_MOD_ptm_res_mask | ST25R3916_REG_PT_MOD_pt_res_mask), + 0x2e /* Card Mode LMA */ + , + ST25R3916_REG_EMD_SUP_CONF, ST25R3916_REG_EMD_SUP_CONF_rx_start_emv, + ST25R3916_REG_EMD_SUP_CONF_rx_start_emv_on /* Enable start on first 4 + bits */ + , + ST25R3916_REG_ANT_TUNE_A, 0xFF, 0xC5 /* Set Antenna Tuning (Poller) */ + , + ST25R3916_REG_ANT_TUNE_B, 0xFF, 0xE3 /* Set Antenna Tuning (Poller) */ + , + ST25R3916_REG_AUX_MOD, ST25R3916_REG_AUX_MOD_rgs_am, + ST25R3916_REG_AUX_MOD_rgs_am /* Enable AWS */ + , + ST25R3916_REG_AWS_CONF1, ST25R3916_REG_AWS_CONF1_rgs_txonoff, + ST25R3916_REG_AWS_CONF1_rgs_txonoff /* Use AWS for field transition */ + , + ST25R3916_REG_AUX_MOD, ST25R3916_REG_AUX_MOD_dis_reg_am, + ST25R3916_REG_AUX_MOD_dis_reg_am /* Set am_mode */ + , + ST25R3916_REG_AWS_CONF1, ST25R3916_REG_AWS_CONF1_vddrf_cont, + ST25R3916_REG_AWS_CONF1_vddrf_cont /* Set vddrf_cont */ + ) + + /****** Default Analog Configuration for Chip-Specific Poll Common ******/ + , + MODE_ENTRY_8_REG( + (RFAL_ANALOG_CONFIG_TECH_CHIP | RFAL_ANALOG_CONFIG_CHIP_POLL_COMMON), + ST25R3916_REG_ANT_TUNE_A, 0xFF, 0xC5 /* AAT Setting for R/W mode */ + , + ST25R3916_REG_ANT_TUNE_B, 0xFF, 0xE3 /* AAT Setting for R/W mode */ + , + ST25R3916_REG_ISO14443A_NFC, ST25R3916_REG_ISO14443A_NFC_p_len_mask, + 0x00 /* Set p_len to default */ + , + ST25R3916_REG_AUX_MOD, ST25R3916_REG_AUX_MOD_rgs_am, + ST25R3916_REG_AUX_MOD_rgs_am /* Enable new AWS */ + , + ST25R3916_REG_AWS_TIME1, ST25R3916_REG_AWS_TIME1_tmodsw1_mask, + 0x01 /* tmodsw1 */ + , + ST25R3916_REG_AWS_TIME3, ST25R3916_REG_AWS_TIME3_tentx1_mask, + 0x70 /* Time in fc periods when driver modulation stops (tr_am + dependent) */ + , + ST25R3916_REG_AWS_TIME3, ST25R3916_REG_AWS_TIME3_tmods2_mask, + 0x09 /* Time in fc periods for hard switch between VDD_DR and VDD_AM */ + , + ST25R3916_REG_AWS_TIME4, ST25R3916_REG_AWS_TIME4_tmodsw2_mask, + 0x07 /* Time in fc periods for soft switch between VDD_DR and VDD_AM */ + ) + + /****** Default Analog Configuration for Poll NFC-A Tx Common ******/ + , + MODE_ENTRY_5_REG( + (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCA | + RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_TX), + ST25R3916_REG_MODE, ST25R3916_REG_MODE_tr_am, + ST25R3916_REG_MODE_tr_am_ook /* Use OOK modulation */ + , + ST25R3916_REG_TX_DRIVER, ST25R3916_REG_TX_DRIVER_am_mod_mask, + 0xF0 /* Set modulation index for AWS */ + , + ST25R3916_REG_AWS_CONF2, ST25R3916_REG_AWS_CONF2_am_sym, + 0x00 /* Nonsymmetrical shape(for OOK) */ + , + ST25R3916_REG_AWS_CONF2, ST25R3916_REG_AWS_CONF2_en_modsink, + ST25R3916_REG_AWS_CONF2_en_modsink /* Enable strong sink during AWS mod + */ + , + ST25R3916_REG_AWS_CONF2, ST25R3916_REG_AWS_CONF2_am_filt_mask, + 0x08 /* Medium AWS filter constant */ + ) + + /****** Default Analog Configuration for Poll NFC-A Rx 106 ******/ + , + MODE_ENTRY_6_REG((RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCA | + RFAL_ANALOG_CONFIG_BITRATE_106 | RFAL_ANALOG_CONFIG_RX), + ST25R3916_REG_RX_CONF1, 0xFF, 0x08, ST25R3916_REG_RX_CONF2, + 0xFF, 0xED, ST25R3916_REG_RX_CONF3, 0xFF, 0x00, + ST25R3916_REG_RX_CONF4, 0xFF, 0x00, + ST25R3916_REG_CORR_CONF1, 0xFF, 0x51, + ST25R3916_REG_CORR_CONF2, 0xFF, 0x00) + + /****** Default Analog Configuration for Poll NFC-A Rx 212 ******/ + , + MODE_ENTRY_6_REG((RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCA | + RFAL_ANALOG_CONFIG_BITRATE_212 | RFAL_ANALOG_CONFIG_RX), + ST25R3916_REG_RX_CONF1, 0xFF, 0x02, ST25R3916_REG_RX_CONF2, + 0xFF, 0xFD, ST25R3916_REG_RX_CONF3, 0xFF, 0x00, + ST25R3916_REG_RX_CONF4, 0xFF, 0x00, + ST25R3916_REG_CORR_CONF1, 0xFF, 0x97, + ST25R3916_REG_CORR_CONF2, 0xFF, 0x00) + + /****** Default Analog Configuration for Poll NFC-A Rx 424 ******/ + , + MODE_ENTRY_6_REG((RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCA | + RFAL_ANALOG_CONFIG_BITRATE_424 | RFAL_ANALOG_CONFIG_RX), + ST25R3916_REG_RX_CONF1, 0xFF, 0x42, ST25R3916_REG_RX_CONF2, + 0xFF, 0xFD, ST25R3916_REG_RX_CONF3, 0xFF, 0x00, + ST25R3916_REG_RX_CONF4, 0xFF, 0x00, + ST25R3916_REG_CORR_CONF1, 0xFF, 0xd7, + ST25R3916_REG_CORR_CONF2, 0xFF, 0x00) + + /****** Default Analog Configuration for Poll NFC-A Tx 848 ******/ + , + MODE_ENTRY_6_REG( + (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCA | + RFAL_ANALOG_CONFIG_BITRATE_848 | RFAL_ANALOG_CONFIG_TX), + ST25R3916_REG_MODE, ST25R3916_REG_MODE_tr_am, + ST25R3916_REG_MODE_tr_am_am /* Use AM modulation */ + , + ST25R3916_REG_TX_DRIVER, ST25R3916_REG_TX_DRIVER_am_mod_mask, + 0xD0 /* Set modulation index */ + , + ST25R3916_REG_AWS_CONF2, ST25R3916_REG_AWS_CONF2_am_filt_mask, + 0x00 /* Fast AWS filter constant */ + , + ST25R3916_REG_AWS_TIME3, ST25R3916_REG_AWS_TIME3_tentx1_mask, + 0x30 /* AWS enable TX (tentx1) */ + , + ST25R3916_REG_AWS_TIME3, ST25R3916_REG_AWS_TIME3_tmods2_mask, + 0x00 /* AWS hard switch at rising edge (tmods2) : 0 fc perionds */ + , + ST25R3916_REG_AWS_TIME4, ST25R3916_REG_AWS_TIME4_tmodsw2_mask, + 0x02 /* AWS soft switch at rising edge (tmodsw2) : 2 fc perionds */ + ) + + /****** Default Analog Configuration for Poll NFC-A Rx 848 ******/ + , + MODE_ENTRY_6_REG((RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCA | + RFAL_ANALOG_CONFIG_BITRATE_848 | RFAL_ANALOG_CONFIG_RX), + ST25R3916_REG_RX_CONF1, 0xFF, 0x42, ST25R3916_REG_RX_CONF2, + 0xFF, 0xFD, ST25R3916_REG_RX_CONF3, 0xFF, 0x00, + ST25R3916_REG_RX_CONF4, 0xFF, 0x00, + ST25R3916_REG_CORR_CONF1, 0xFF, 0x47, + ST25R3916_REG_CORR_CONF2, 0xFF, 0x00) + + /****** Default Analog Configuration for Poll NFC-A Anticolision setting + ******/ + , + MODE_ENTRY_1_REG( + (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCA | + RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_ANTICOL), + ST25R3916_REG_CORR_CONF1, ST25R3916_REG_CORR_CONF1_corr_s6, + 0x00 /* Different data slicer to improve collision detection */ + ) + + /****** Default Analog Configuration for Poll NFC-B Tx Common ******/ + , + MODE_ENTRY_5_REG( + (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCB | + RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_TX), + ST25R3916_REG_MODE, ST25R3916_REG_MODE_tr_am, + ST25R3916_REG_MODE_tr_am_am /* Use AM modulation */ + , + ST25R3916_REG_TX_DRIVER, ST25R3916_REG_TX_DRIVER_am_mod_mask, + 0x40 /* Set modulation index for AWS */ + , + ST25R3916_REG_AWS_CONF2, ST25R3916_REG_AWS_CONF2_am_sym, + ST25R3916_REG_AWS_CONF2_am_sym /* AWS shaping symmetry (am_sym) */ + , + ST25R3916_REG_AWS_CONF2, ST25R3916_REG_AWS_CONF2_en_modsink, + 0x00 /* Weak sink */ + , + ST25R3916_REG_AWS_CONF2, ST25R3916_REG_AWS_CONF2_am_filt_mask, + 0x08 /* Medium AWS filter constant */ + ) + + /****** Default Analog Configuration for Poll NFC-B Rx 106 ******/ + , + MODE_ENTRY_6_REG((RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCB | + RFAL_ANALOG_CONFIG_BITRATE_106 | RFAL_ANALOG_CONFIG_RX), + ST25R3916_REG_RX_CONF1, 0xFF, 0x04, ST25R3916_REG_RX_CONF2, + 0xFF, 0xFD, ST25R3916_REG_RX_CONF3, 0xFF, 0x00, + ST25R3916_REG_RX_CONF4, 0xFF, 0x00, + ST25R3916_REG_CORR_CONF1, 0xFF, 0x97, + ST25R3916_REG_CORR_CONF2, 0xFF, 0x00) + + /****** Default Analog Configuration for Poll NFC-B Rx 212 ******/ + , + MODE_ENTRY_6_REG((RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCB | + RFAL_ANALOG_CONFIG_BITRATE_212 | RFAL_ANALOG_CONFIG_RX), + ST25R3916_REG_RX_CONF1, 0xFF, 0x02, ST25R3916_REG_RX_CONF2, + 0xFF, 0xFD, ST25R3916_REG_RX_CONF3, 0xFF, 0x00, + ST25R3916_REG_RX_CONF4, 0xFF, 0x00, + ST25R3916_REG_CORR_CONF1, 0xFF, 0x97, + ST25R3916_REG_CORR_CONF2, 0xFF, 0x00) + + /****** Default Analog Configuration for Poll NFC-B Rx 424 ******/ + , + MODE_ENTRY_6_REG((RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCB | + RFAL_ANALOG_CONFIG_BITRATE_424 | RFAL_ANALOG_CONFIG_RX), + ST25R3916_REG_RX_CONF1, 0xFF, 0x42, ST25R3916_REG_RX_CONF2, + 0xFF, 0xFD, ST25R3916_REG_RX_CONF3, 0xFF, 0x00, + ST25R3916_REG_RX_CONF4, 0xFF, 0x00, + ST25R3916_REG_CORR_CONF1, 0xFF, 0xD7, + ST25R3916_REG_CORR_CONF2, 0xFF, 0x00) + + /****** Default Analog Configuration for Poll NFC-B Tx 848 ******/ + , + MODE_ENTRY_1_REG((RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCB | + RFAL_ANALOG_CONFIG_BITRATE_848 | RFAL_ANALOG_CONFIG_TX), + ST25R3916_REG_AWS_CONF2, + ST25R3916_REG_AWS_CONF2_am_filt_mask, + 0x01 /* Fast AWS filter constant */ + ) + + /****** Default Analog Configuration for Poll NFC-B Rx 848 ******/ + , + MODE_ENTRY_6_REG((RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCB | + RFAL_ANALOG_CONFIG_BITRATE_848 | RFAL_ANALOG_CONFIG_RX), + ST25R3916_REG_RX_CONF1, 0xFF, 0x42, ST25R3916_REG_RX_CONF2, + 0xFF, 0xFD, ST25R3916_REG_RX_CONF3, 0xFF, 0x00, + ST25R3916_REG_RX_CONF4, 0xFF, 0x00, + ST25R3916_REG_CORR_CONF1, 0xFF, 0x47, + ST25R3916_REG_CORR_CONF2, 0xFF, 0x00) + + /****** Default Analog Configuration for Poll NFC-F Tx Common ******/ + , + MODE_ENTRY_5_REG( + (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCF | + RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_TX), + ST25R3916_REG_MODE, ST25R3916_REG_MODE_tr_am, + ST25R3916_REG_MODE_tr_am_am /* Use AM modulation */ + , + ST25R3916_REG_TX_DRIVER, ST25R3916_REG_TX_DRIVER_am_mod_mask, + 0x40 /* Set modulation index */ + , + ST25R3916_REG_AWS_CONF2, ST25R3916_REG_AWS_CONF2_am_sym, + ST25R3916_REG_AWS_CONF2_am_sym /* AWS shaping symmetry (am_sym) */ + , + ST25R3916_REG_AWS_CONF2, ST25R3916_REG_AWS_CONF2_en_modsink, + 0x00 /* Weak sink */ + , + ST25R3916_REG_AWS_CONF2, ST25R3916_REG_AWS_CONF2_am_filt_mask, + 0x08 /* Medium AWS filter constant */ + ) + + /****** Default Analog Configuration for Poll NFC-F Rx Common ******/ + , + MODE_ENTRY_6_REG( + (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCF | + RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_RX), + ST25R3916_REG_RX_CONF1, 0xFF, 0x13, ST25R3916_REG_RX_CONF2, 0xFF, 0xFD, + ST25R3916_REG_RX_CONF3, 0xFF, 0x00, ST25R3916_REG_RX_CONF4, 0xFF, 0x00, + ST25R3916_REG_CORR_CONF1, 0xFF, 0x54, ST25R3916_REG_CORR_CONF2, 0xFF, + 0x00) + + /****** Default Analog Configuration for Poll NFC-V Tx 26 ******/ + , + MODE_ENTRY_6_REG( + (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCV | + RFAL_ANALOG_CONFIG_BITRATE_26 | RFAL_ANALOG_CONFIG_TX), + ST25R3916_REG_MODE, ST25R3916_REG_MODE_tr_am, + ST25R3916_REG_MODE_tr_am_ook /* Use OOK modulation */ + , + ST25R3916_REG_TX_DRIVER, ST25R3916_REG_TX_DRIVER_am_mod_mask, + 0xF0 /* Set modulation index for AWS */ + , + ST25R3916_REG_ISO14443A_NFC, ST25R3916_REG_ISO14443A_NFC_p_len_mask, + 0x1c /* Set modulation pulse length p_len */ + , + ST25R3916_REG_AWS_CONF2, ST25R3916_REG_AWS_CONF2_am_sym, + 0x00 /* Nonsymerical shape (for OOK) */ + , + ST25R3916_REG_AWS_CONF2, ST25R3916_REG_AWS_CONF2_en_modsink, + ST25R3916_REG_AWS_CONF2_en_modsink /* AWS enable strong sink + (en_modsink) */ + , + ST25R3916_REG_AWS_CONF2, ST25R3916_REG_AWS_CONF2_am_filt_mask, + 0x06 /* Medium fast AWS filter constant */ + ) + + /****** Default Analog Configuration for Poll NFC-V Rx Common ******/ + , + MODE_ENTRY_6_REG( + (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCV | + RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_RX), + ST25R3916_REG_RX_CONF1, 0xFF, 0x13, ST25R3916_REG_RX_CONF2, 0xFF, 0xED, + ST25R3916_REG_RX_CONF3, 0xFF, 0x00, ST25R3916_REG_RX_CONF4, 0xFF, 0x00, + ST25R3916_REG_CORR_CONF1, 0xFF, 0x13, ST25R3916_REG_CORR_CONF2, 0xFF, + 0x01) + + /****** Default Analog Configuration for Poll AP2P Tx 106 ******/ + , + MODE_ENTRY_5_REG( + (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_AP2P | + RFAL_ANALOG_CONFIG_BITRATE_106 | RFAL_ANALOG_CONFIG_TX), + ST25R3916_REG_MODE, ST25R3916_REG_MODE_tr_am, + ST25R3916_REG_MODE_tr_am_ook /* Use OOK modulation */ + , + ST25R3916_REG_TX_DRIVER, ST25R3916_REG_TX_DRIVER_am_mod_mask, + 0xF0 /* Set modulation index for AWS */ + , + ST25R3916_REG_AWS_CONF2, ST25R3916_REG_AWS_CONF2_am_sym, + 0x00 /* Nonsymerical shape (for OOK) */ + , + ST25R3916_REG_AWS_CONF2, ST25R3916_REG_AWS_CONF2_en_modsink, + ST25R3916_REG_AWS_CONF2_en_modsink /* AWS enable strong sink + (en_modsink) */ + , + ST25R3916_REG_AWS_CONF2, ST25R3916_REG_AWS_CONF2_am_filt_mask, + 0x08 /* Medium AWS filter constant */ + ) + + /****** Default Analog Configuration for Poll AP2P Tx 212 ******/ + , + MODE_ENTRY_5_REG( + (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_AP2P | + RFAL_ANALOG_CONFIG_BITRATE_212 | RFAL_ANALOG_CONFIG_TX), + ST25R3916_REG_MODE, ST25R3916_REG_MODE_tr_am, + ST25R3916_REG_MODE_tr_am_am /* Use AM modulation */ + , + ST25R3916_REG_TX_DRIVER, ST25R3916_REG_TX_DRIVER_am_mod_mask, + 0x40 /* Set AM modulation index */ + , + ST25R3916_REG_AWS_CONF2, ST25R3916_REG_AWS_CONF2_am_sym, + ST25R3916_REG_AWS_CONF2_am_sym /* AWS shaping symmetry (am_sym) */ + , + ST25R3916_REG_AWS_CONF2, ST25R3916_REG_AWS_CONF2_en_modsink, + 0x00 /* Weak sink */ + , + ST25R3916_REG_AWS_CONF2, ST25R3916_REG_AWS_CONF2_am_filt_mask, + 0x08 /* Medium AWS filter constant */ + ) + + /****** Default Analog Configuration for Poll AP2P Tx 424 ******/ + , + MODE_ENTRY_5_REG( + (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_AP2P | + RFAL_ANALOG_CONFIG_BITRATE_424 | RFAL_ANALOG_CONFIG_TX), + ST25R3916_REG_MODE, ST25R3916_REG_MODE_tr_am, + ST25R3916_REG_MODE_tr_am_am /* Use AM modulation */ + , + ST25R3916_REG_TX_DRIVER, ST25R3916_REG_TX_DRIVER_am_mod_mask, + 0x40 /* Set AM modulation index */ + , + ST25R3916_REG_AWS_CONF2, ST25R3916_REG_AWS_CONF2_am_sym, + ST25R3916_REG_AWS_CONF2_am_sym /* AWS shaping symmetry (am_sym) */ + , + ST25R3916_REG_AWS_CONF2, ST25R3916_REG_AWS_CONF2_en_modsink, + 0x00 /* Weak sink */ + , + ST25R3916_REG_AWS_CONF2, ST25R3916_REG_AWS_CONF2_am_filt_mask, + 0x08 /* Medium AWS filter constant */ + ) + + /****** Default Analog Configuration for Listen On ******/ + , + MODE_ENTRY_3_REG( + (RFAL_ANALOG_CONFIG_TECH_CHIP | RFAL_ANALOG_CONFIG_CHIP_LISTEN_ON), + ST25R3916_REG_ANT_TUNE_A, 0xFF, 0x00 /* Set Antenna Tuning (Listener) */ + , + ST25R3916_REG_ANT_TUNE_B, 0xFF, 0x00 /* Set Antenna Tuning (Listener) */ + , + ST25R3916_REG_AUX_MOD, ST25R3916_REG_AUX_MOD_rgs_am, + 0x00 /* Disable AWS in Listen mode */ + ) + + /****** Default Analog Configuration for Listen AP2P Tx Common ******/ + , + MODE_ENTRY_3_REG( + (RFAL_ANALOG_CONFIG_LISTEN | RFAL_ANALOG_CONFIG_TECH_AP2P | + RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_TX), + ST25R3916_REG_ANT_TUNE_A, 0xFF, 0xC5 /* Set Antenna Tuning (Poller) */ + , + ST25R3916_REG_ANT_TUNE_B, 0xFF, 0xE3 /* Set Antenna Tuning (Poller) */ + , + ST25R3916_REG_AUX_MOD, ST25R3916_REG_AUX_MOD_rgs_am, + ST25R3916_REG_AUX_MOD_rgs_am /* Enable AWS for AP2P */ + ) + + /****** Default Analog Configuration for Listen AP2P Tx 106 ******/ + , + MODE_ENTRY_5_REG( + (RFAL_ANALOG_CONFIG_LISTEN | RFAL_ANALOG_CONFIG_TECH_AP2P | + RFAL_ANALOG_CONFIG_BITRATE_106 | RFAL_ANALOG_CONFIG_TX), + ST25R3916_REG_MODE, ST25R3916_REG_MODE_tr_am, + ST25R3916_REG_MODE_tr_am_ook /* Use OOK modulation */ + , + ST25R3916_REG_TX_DRIVER, ST25R3916_REG_TX_DRIVER_am_mod_mask, + 0xF0 /* Set modulation index for AWS */ + , + ST25R3916_REG_AWS_CONF2, ST25R3916_REG_AWS_CONF2_am_sym, + 0x00 /* Nonsymerical shape (for OOK) */ + , + ST25R3916_REG_AWS_CONF2, ST25R3916_REG_AWS_CONF2_en_modsink, + ST25R3916_REG_AWS_CONF2_en_modsink /* AWS enable strong sink + (en_modsink) */ + , + ST25R3916_REG_AWS_CONF2, ST25R3916_REG_AWS_CONF2_am_filt_mask, + 0x08 /* Medium AWS filter constant */ + ) + + /****** Default Analog Configuration for Listen AP2P Tx 212 ******/ + , + MODE_ENTRY_5_REG( + (RFAL_ANALOG_CONFIG_LISTEN | RFAL_ANALOG_CONFIG_TECH_AP2P | + RFAL_ANALOG_CONFIG_BITRATE_212 | RFAL_ANALOG_CONFIG_TX), + ST25R3916_REG_MODE, ST25R3916_REG_MODE_tr_am, + ST25R3916_REG_MODE_tr_am_am /* Use AM modulation */ + , + ST25R3916_REG_TX_DRIVER, ST25R3916_REG_TX_DRIVER_am_mod_mask, + 0x40 /* Set AM modulation index */ + , + ST25R3916_REG_AWS_CONF2, ST25R3916_REG_AWS_CONF2_am_sym, + ST25R3916_REG_AWS_CONF2_am_sym /* AWS shaping symmetry (am_sym) */ + , + ST25R3916_REG_AWS_CONF2, ST25R3916_REG_AWS_CONF2_en_modsink, + 0x00 /* Weak sink */ + , + ST25R3916_REG_AWS_CONF2, ST25R3916_REG_AWS_CONF2_am_filt_mask, + 0x08 /* Medium AWS filter constant */ + ) + + /****** Default Analog Configuration for Listen AP2P Tx 424 ******/ + , + MODE_ENTRY_5_REG( + (RFAL_ANALOG_CONFIG_LISTEN | RFAL_ANALOG_CONFIG_TECH_AP2P | + RFAL_ANALOG_CONFIG_BITRATE_424 | RFAL_ANALOG_CONFIG_TX), + ST25R3916_REG_MODE, ST25R3916_REG_MODE_tr_am, + ST25R3916_REG_MODE_tr_am_am /* Use AM modulation */ + , + ST25R3916_REG_TX_DRIVER, ST25R3916_REG_TX_DRIVER_am_mod_mask, + 0x40 /* Set AM modulation index */ + , + ST25R3916_REG_AWS_CONF2, ST25R3916_REG_AWS_CONF2_am_sym, + ST25R3916_REG_AWS_CONF2_am_sym /* AWS shaping symmetry (am_sym) */ + , + ST25R3916_REG_AWS_CONF2, ST25R3916_REG_AWS_CONF2_en_modsink, + 0x00 /* Weak sink */ + , + ST25R3916_REG_AWS_CONF2, ST25R3916_REG_AWS_CONF2_am_filt_mask, + 0x08 /* Medium AWS filter constant */ + ) + + /****** Default Analog Configuration for Wake-up On ******/ + , + MODE_ENTRY_1_REG( + (RFAL_ANALOG_CONFIG_TECH_CHIP | RFAL_ANALOG_CONFIG_CHIP_WAKEUP_ON), + ST25R3916_REG_AUX_MOD, ST25R3916_REG_AUX_MOD_rgs_am, + 0x00 /* Disable AWS during WU */ + ) + + /****** Default Analog Configuration for Wake-up Off ******/ + , + MODE_ENTRY_1_REG( + (RFAL_ANALOG_CONFIG_TECH_CHIP | RFAL_ANALOG_CONFIG_CHIP_WAKEUP_OFF), + ST25R3916_REG_AUX_MOD, ST25R3916_REG_AUX_MOD_rgs_am, + ST25R3916_REG_AUX_MOD_rgs_am /* Re-enable AWS after WU */ + ) + +}; + +#endif /* ST25R3916|B */ + +#endif /* ST25R3916_ANALOGCONFIG_H */ diff --git a/core/embed/io/nfc/rfal/source/st25r3916/rfal_dpoTbl.h b/core/embed/io/nfc/rfal/source/st25r3916/rfal_dpoTbl.h new file mode 100644 index 0000000000..27ac5289e8 --- /dev/null +++ b/core/embed/io/nfc/rfal/source/st25r3916/rfal_dpoTbl.h @@ -0,0 +1,69 @@ + +/****************************************************************************** + * @attention + * + * COPYRIGHT 2016 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/* + * PROJECT: ST25R391x firmware + * $Revision: $ + * LANGUAGE: ISO C99 + */ + +/*! \file + * + * \author Martin Zechleitner + * + * \brief RF Dynamic Power Table default values + */ + +#ifndef ST25R3916_DPO_H +#define ST25R3916_DPO_H + +/* + ****************************************************************************** + * INCLUDES + ****************************************************************************** + */ +#include "rfal_dpo.h" + +/* + ****************************************************************************** + * GLOBAL DATA TYPES + ****************************************************************************** + */ + +#if defined(ST25R3916) + +/*! ST25R3916 Default DPO table */ +/* PRQA S 3674 2 # CERT ARR02 - Flexible array will be used with sizeof, on + * adding elements error-prone manual update of size would be required */ +/* PRQA S 3406 1 # MISRA 8.6 - Externally generated table included by the + * library */ /* PRQA S 1514 1 # MISRA 8.9 - Externally generated table included by the library */ +const rfalDpoEntry rfalDpoDefaultSettings[] = { + {0x00, 255, 200}, {0x01, 210, 150}, {0x02, 160, 100}, {0x03, 110, 50}}; + +#elif defined(ST25R3916B) /* ST25R3916B has an increased resolution on the \ + driver resistance (d_res) */ + +/*! ST25R3916B Default DPO table */ +/* PRQA S 3674 2 # CERT ARR02 - Flexible array will be used with sizeof, on + * adding elements error-prone manual update of size would be required */ +/* PRQA S 3406 1 # MISRA 8.6 - Externally generated table included by the + * library */ /* PRQA S 1514 1 # MISRA 8.9 - Externally generated table included by the library */ +const rfalDpoEntry rfalDpoDefaultSettings[] = { + {0x00, 255, 200}, {0x05, 210, 150}, {0x09, 160, 100}, {0x0B, 110, 50}}; + +#endif /* ST25R3916 */ + +#endif /* ST25R3916_DPO_H */ diff --git a/core/embed/io/nfc/rfal/source/st25r3916/rfal_features.h b/core/embed/io/nfc/rfal/source/st25r3916/rfal_features.h new file mode 100644 index 0000000000..b5206f9795 --- /dev/null +++ b/core/embed/io/nfc/rfal/source/st25r3916/rfal_features.h @@ -0,0 +1,241 @@ + +/****************************************************************************** + * @attention + * + * COPYRIGHT 2016 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/* + * PROJECT: ST25R391x firmware + * Revision: + * LANGUAGE: ISO C99 + */ + +/*! \file + * + * \author Gustavo Patricio + * + * \brief RFAL Features/Capabilities Definition for ST25R3916 + */ + +#ifndef RFAL_FEATURES_H +#define RFAL_FEATURES_H + +/* +****************************************************************************** +* INCLUDES +****************************************************************************** +*/ +#include "rfal_platform.h" + +/* +****************************************************************************** +* GLOBAL DEFINES +****************************************************************************** +*/ + +#define RFAL_SUPPORT_MODE_POLL_NFCA \ + true /*!< RFAL Poll NFCA mode support switch */ +#define RFAL_SUPPORT_MODE_POLL_NFCB \ + true /*!< RFAL Poll NFCB mode support switch */ +#define RFAL_SUPPORT_MODE_POLL_NFCF \ + true /*!< RFAL Poll NFCF mode support switch */ +#define RFAL_SUPPORT_MODE_POLL_NFCV \ + true /*!< RFAL Poll NFCV mode support switch */ +#define RFAL_SUPPORT_MODE_POLL_ACTIVE_P2P \ + true /*!< RFAL Poll AP2P mode support switch */ +#define RFAL_SUPPORT_MODE_LISTEN_NFCA \ + true /*!< RFAL Listen NFCA mode support switch */ +#define RFAL_SUPPORT_MODE_LISTEN_NFCB \ + false /*!< RFAL Listen NFCB mode support switch */ +#define RFAL_SUPPORT_MODE_LISTEN_NFCF \ + true /*!< RFAL Listen NFCF mode support switch */ +#define RFAL_SUPPORT_MODE_LISTEN_ACTIVE_P2P \ + true /*!< RFAL Listen AP2P mode support switch */ + +/*******************************************************************************/ +/*! RFAL supported Card Emulation (CE) */ +#define RFAL_SUPPORT_CE \ + (RFAL_SUPPORT_MODE_LISTEN_NFCA || RFAL_SUPPORT_MODE_LISTEN_NFCB || \ + RFAL_SUPPORT_MODE_LISTEN_NFCF) + +/*! RFAL supported Reader/Writer (RW) */ +#define RFAL_SUPPORT_RW \ + (RFAL_SUPPORT_MODE_POLL_NFCA || RFAL_SUPPORT_MODE_POLL_NFCB || \ + RFAL_SUPPORT_MODE_POLL_NFCF || RFAL_SUPPORT_MODE_POLL_NFCV) + +/*! RFAL support for Active P2P (AP2P) */ +#define RFAL_SUPPORT_AP2P \ + (RFAL_SUPPORT_MODE_POLL_ACTIVE_P2P || RFAL_SUPPORT_MODE_LISTEN_ACTIVE_P2P) + +/*******************************************************************************/ +#define RFAL_SUPPORT_BR_RW_106 \ + true /*!< RFAL RW 106 Bit Rate support switch */ +#define RFAL_SUPPORT_BR_RW_212 \ + true /*!< RFAL RW 212 Bit Rate support switch */ +#define RFAL_SUPPORT_BR_RW_424 \ + true /*!< RFAL RW 424 Bit Rate support switch */ +#define RFAL_SUPPORT_BR_RW_848 \ + true /*!< RFAL RW 848 Bit Rate support switch */ +#define RFAL_SUPPORT_BR_RW_1695 \ + false /*!< RFAL RW 1695 Bit Rate support switch */ +#define RFAL_SUPPORT_BR_RW_3390 \ + false /*!< RFAL RW 3390 Bit Rate support switch */ +#define RFAL_SUPPORT_BR_RW_6780 \ + false /*!< RFAL RW 6780 Bit Rate support switch */ +#define RFAL_SUPPORT_BR_RW_13560 \ + false /*!< RFAL RW 6780 Bit Rate support switch */ + +/*******************************************************************************/ +#define RFAL_SUPPORT_BR_AP2P_106 \ + true /*!< RFAL AP2P 106 Bit Rate support switch */ +#define RFAL_SUPPORT_BR_AP2P_212 \ + true /*!< RFAL AP2P 212 Bit Rate support switch */ +#define RFAL_SUPPORT_BR_AP2P_424 \ + true /*!< RFAL AP2P 424 Bit Rate support switch */ +#define RFAL_SUPPORT_BR_AP2P_848 \ + false /*!< RFAL AP2P 848 Bit Rate support switch */ + +/*******************************************************************************/ +#define RFAL_SUPPORT_BR_CE_A_106 \ + true /*!< RFAL CE A 106 Bit Rate support switch */ +#define RFAL_SUPPORT_BR_CE_A_212 \ + false /*!< RFAL CE A 212 Bit Rate support switch */ +#define RFAL_SUPPORT_BR_CE_A_424 \ + false /*!< RFAL CE A 424 Bit Rate support switch */ +#define RFAL_SUPPORT_BR_CE_A_848 \ + false /*!< RFAL CE A 848 Bit Rate support switch */ + +/*******************************************************************************/ +#define RFAL_SUPPORT_BR_CE_B_106 \ + false /*!< RFAL CE B 106 Bit Rate support switch */ +#define RFAL_SUPPORT_BR_CE_B_212 \ + false /*!< RFAL CE B 212 Bit Rate support switch */ +#define RFAL_SUPPORT_BR_CE_B_424 \ + false /*!< RFAL CE B 424 Bit Rate support switch */ +#define RFAL_SUPPORT_BR_CE_B_848 \ + false /*!< RFAL CE B 848 Bit Rate support switch */ + +/*******************************************************************************/ +#define RFAL_SUPPORT_BR_CE_F_212 \ + true /*!< RFAL CE F 212 Bit Rate support switch */ +#define RFAL_SUPPORT_BR_CE_F_424 \ + true /*!< RFAL CE F 424 Bit Rate support switch */ + +/* +****************************************************************************** +* DEVICE SPECIFIC FEATURE DEFINITIONS +****************************************************************************** +*/ + +/*! RFAL Wake-Up Period/Timer */ +typedef enum { + RFAL_WUM_PERIOD_10MS = 0x00, /*!< Wake-Up timer 10ms */ + RFAL_WUM_PERIOD_20MS = 0x01, /*!< Wake-Up timer 20ms */ + RFAL_WUM_PERIOD_30MS = 0x02, /*!< Wake-Up timer 30ms */ + RFAL_WUM_PERIOD_40MS = 0x03, /*!< Wake-Up timer 40ms */ + RFAL_WUM_PERIOD_50MS = 0x04, /*!< Wake-Up timer 50ms */ + RFAL_WUM_PERIOD_60MS = 0x05, /*!< Wake-Up timer 60ms */ + RFAL_WUM_PERIOD_70MS = 0x06, /*!< Wake-Up timer 70ms */ + RFAL_WUM_PERIOD_80MS = 0x07, /*!< Wake-Up timer 80ms */ + RFAL_WUM_PERIOD_100MS = 0x10, /*!< Wake-Up timer 100ms */ + RFAL_WUM_PERIOD_200MS = 0x11, /*!< Wake-Up timer 200ms */ + RFAL_WUM_PERIOD_300MS = 0x12, /*!< Wake-Up timer 300ms */ + RFAL_WUM_PERIOD_400MS = 0x13, /*!< Wake-Up timer 400ms */ + RFAL_WUM_PERIOD_500MS = 0x14, /*!< Wake-Up timer 500ms */ + RFAL_WUM_PERIOD_600MS = 0x15, /*!< Wake-Up timer 600ms */ + RFAL_WUM_PERIOD_700MS = 0x16, /*!< Wake-Up timer 700ms */ + RFAL_WUM_PERIOD_800MS = 0x17, /*!< Wake-Up timer 800ms */ +} rfalWumPeriod; + +/*! RFAL Wake-Up Period/Timer */ +typedef enum { + RFAL_WUM_AA_WEIGHT_4 = 0x00, /*!< Wake-Up Auto Average Weight 4 */ + RFAL_WUM_AA_WEIGHT_8 = 0x01, /*!< Wake-Up Auto Average Weight 8 */ + RFAL_WUM_AA_WEIGHT_16 = 0x02, /*!< Wake-Up Auto Average Weight 16 */ + RFAL_WUM_AA_WEIGHT_32 = 0x03, /*!< Wake-Up Auto Average Weight 32 */ +} rfalWumAAWeight; + +/*! RFAL Wake-Up Mode configuration */ +typedef struct { + rfalWumPeriod + period; /*!< Wake-Up Timer period;how often measurement(s) is performed */ + bool irqTout; /*!< IRQ at every timeout will refresh the measurement(s) */ + bool swTagDetect; /*!< Use SW Tag Detection instead of HW Wake-Up mode */ + + struct { + bool enabled; /*!< Reference from WU mode enabled */ + rfalWumPeriod refDelay; /*!< Obtain reference from WU after delay time */ + } refWU; /*!< Reference obtained from PD|WU mode */ + + struct { + bool enabled; /*!< Inductive Amplitude measurement enabled */ + uint8_t + delta; /*!< Delta between the reference and measurement to wake-up */ + uint8_t fracDelta; /*!< Fractional part of the delta [0;3] 0.25 steps (SW TD + only) */ + uint16_t reference; /*!< Reference to be used;RFAL_WUM_REFERENCE_AUTO sets + it auto */ + bool autoAvg; /*!< Use the HW Auto Averaging feature */ + bool aaInclMeas; /*!< When AutoAvg is enabled, include IRQ measurement */ + rfalWumAAWeight + aaWeight; /*!< When AutoAvg is enabled, last measure weight */ + } indAmp; /*!< Inductive Amplitude Configuration */ + struct { + bool enabled; /*!< Inductive Phase measurement enabled */ + uint8_t + delta; /*!< Delta between the reference and measurement to wake-up */ + uint8_t fracDelta; /*!< Fractional part of the delta [0;3] 0.25 steps (SW TD + only) */ + uint16_t reference; /*!< Reference to be used;RFAL_WUM_REFERENCE_AUTO sets + it auto */ + bool autoAvg; /*!< Use the HW Auto Averaging feature */ + bool aaInclMeas; /*!< When AutoAvg is enabled, include IRQ measurement */ + rfalWumAAWeight + aaWeight; /*!< When AutoAvg is enabled, last measure weight */ + } indPha; /*!< Inductive Phase Configuration */ + struct { + bool enabled; /*!< Capacitive measurement enabled */ + uint8_t + delta; /*!< Delta between the reference and measurement to wake-up */ + uint16_t reference; /*!< Reference to be used;RFAL_WUM_REFERENCE_AUTO sets + it auto */ + bool autoAvg; /*!< Use the HW Auto Averaging feature */ + bool aaInclMeas; /*!< When AutoAvg is enabled, include IRQ measurement */ + rfalWumAAWeight + aaWeight; /*!< When AutoAvg is enabled, last measure weight */ + } cap; /*!< Capacitive Configuration */ +} rfalWakeUpConfig; + +/*! RFAL Wake-Up Mode information */ +typedef struct { + bool irqWut; /*!< Wake-Up Timer IRQ received (cleared upon read) */ + struct { + uint8_t lastMeas; /*!< Value of the latest measurement */ + uint16_t + reference; /*!< Current reference value (TD format if SW TD enabled) */ + bool irqWu; /*!< Amplitude WU IRQ received (cleared upon read) */ + } indAmp; /*!< Inductive Amplitude */ + struct { + uint8_t lastMeas; /*!< Value of the latest measurement */ + uint16_t + reference; /*!< Current reference value (TD format if SW TD enabled) */ + bool irqWu; /*!< Phase WU IRQ received (cleared upon read) */ + } indPha; /*!< Inductive Phase */ + struct { + uint8_t lastMeas; /*!< Value of the latest measurement */ + uint16_t reference; /*!< Current reference value */ + bool irqWu; /*!< Capacitive WU IRQ received (cleared upon read) */ + } cap; /*!< Capacitive */ +} rfalWakeUpInfo; + +#endif /* RFAL_FEATURES_H */ diff --git a/core/embed/io/nfc/rfal/source/st25r3916/rfal_rfst25r3916.c b/core/embed/io/nfc/rfal/source/st25r3916/rfal_rfst25r3916.c new file mode 100644 index 0000000000..0e712e6f10 --- /dev/null +++ b/core/embed/io/nfc/rfal/source/st25r3916/rfal_rfst25r3916.c @@ -0,0 +1,5669 @@ + +/****************************************************************************** + * @attention + * + * COPYRIGHT 2016 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/* + * PROJECT: ST25R3916 firmware + * Revision: + * LANGUAGE: ISO C99 + */ + +/*! \file + * + * \author Gustavo Patricio + * + * \brief RF Abstraction Layer (RFAL) + * + * RFAL implementation for ST25R3916 + */ + +/* +****************************************************************************** +* INCLUDES +****************************************************************************** +*/ + +#include "rfal_analogConfig.h" +#include "rfal_chip.h" +#include "rfal_crc.h" +#include "rfal_iso15693_2.h" +#include "rfal_utils.h" +#include "st25r3916.h" +#include "st25r3916_com.h" +#include "st25r3916_irq.h" + +/* + ****************************************************************************** + * ENABLE SWITCHS + ****************************************************************************** + */ + +/* Specific features may be enabled or disabled by user at rfal_platform.h + * Default configuration (ST25R dependant) also provided at rfal_defConfig.h + * + * RFAL_FEATURE_LISTEN_MODE + * RFAL_FEATURE_WAKEUP_MODE + * RFAL_FEATURE_LOWPOWER_MODE + */ + +/* +****************************************************************************** +* GLOBAL TYPES +****************************************************************************** +*/ + +/*! Struct that holds all involved on a Transceive including the context passed + * by the caller */ +typedef struct { + rfalTransceiveState state; /*!< Current transceive state */ + rfalTransceiveState lastState; /*!< Last transceive state (debug purposes) */ + ReturnCode status; /*!< Current status/error of the transceive */ + + rfalTransceiveContext ctx; /*!< The transceive context given by the caller */ +} rfalTxRx; + +/*! Struct that holds certain WU mode information to be retrieved by + * rfalWakeUpModeGetInfo */ +typedef struct { + bool irqWut; /*!< Wake-Up Timer IRQ received (cleared upon read) */ + + struct { + uint8_t lastMeas; /*!< Value of the latest measurement */ + bool irqWu; /*!< Amplitude WU IRQ received (cleared upon read) */ + } indAmp; /*!< Inductive Amplitude */ + struct { + uint8_t lastMeas; /*!< Value of the latest measurement */ + bool irqWu; /*!< Phase WU IRQ received (cleared upon read) */ + } indPha; /*!< Inductive Phase */ + struct { + uint8_t lastMeas; /*!< Value of the latest measurement */ + bool irqWu; /*!< Capacitive WU IRQ received (cleared upon read) */ + } cap; /*!< Capacitance */ +} rfalWakeUpData; + +/*! Local struct that holds context for the Listen Mode */ +typedef struct { + rfalLmState state; /*!< Current Listen Mode state */ + uint32_t mdMask; /*!< Listen Mode mask used */ + uint32_t mdReg; /*!< Listen Mode register value used */ + uint32_t mdIrqs; /*!< Listen Mode IRQs used */ + rfalBitRate brDetected; /*!< Last bit rate detected */ + + uint8_t *rxBuf; /*!< Location to store incoming data in Listen Mode */ + uint16_t rxBufLen; /*!< Length of rxBuf */ + uint16_t *rxLen; /*!< Pointer to write the data length placed into rxBuf */ + bool dataFlag; /*!< Listen Mode current Data Flag */ + bool iniFlag; /*!< Listen Mode initialized Flag (FeliCa slots) */ +} rfalLm; + +/*! Struct that holds all context for the Wake-Up Mode */ +typedef struct { + rfalWumState state; /*!< Current Wake-Up Mode state */ + rfalWakeUpConfig cfg; /*!< Current Wake-Up Mode config */ + rfalWakeUpData info; /*!< Current Wake-Up Mode info */ + uint32_t refWUTrg; /*!< Trigger used for refWU */ +} rfalWum; + +/*! Struct that holds all context for the Low Power Mode */ +typedef struct { + bool isRunning; +} rfalLpm; + +/*! Struct that holds the timings GT and FDTs */ +typedef struct { + uint32_t GT; /*!< GT in 1/fc */ + uint32_t FDTListen; /*!< FDTListen in 1/fc */ + uint32_t FDTPoll; /*!< FDTPoll in 1/fc */ + uint8_t nTRFW; /*!< n*TRFW (last two bits) used during RF CA */ +} rfalTimings; + +/*! Struct that holds the software timers */ +typedef struct { + uint32_t GT; /*!< RFAL's GT timer */ + uint32_t RXE; /*!< Timer between RXS - RXE */ + uint32_t PPON2; /*!< Timer between TXE - PPON2 */ + uint32_t txRx; /*!< Transceive sanity timer */ +} rfalTimers; + +/*! Struct that holds the RFAL's callbacks */ +typedef struct { + rfalPreTxRxCallback preTxRx; /*!< RFAL's Pre TxRx callback */ + rfalPostTxRxCallback postTxRx; /*!< RFAL's Post TxRx callback */ + rfalSyncTxRxCallback syncTxRx; /*!< RFAL's Sync TxRx callback */ + rfalLmEonCallback lmEon; /*!< RFAL's LM EON callback */ +} rfalCallbacks; + +/*! Struct that holds counters to control the FIFO on Tx and Rx */ +typedef struct { + uint16_t expWL; /*!< The amount of bytes expected to be Tx when a WL interrupt + occours */ + uint16_t bytesTotal; /*!< Total bytes to be transmitted OR the total bytes + received */ + uint16_t bytesWritten; /*!< Amount of bytes already written on FIFO (Tx) OR + read (RX) from FIFO and written on rxBuffer*/ + uint8_t status[ST25R3916_FIFO_STATUS_LEN]; /*!< FIFO Status Registers */ +} rfalFIFO; + +/*! Struct that holds RFAL's configuration settings */ +typedef struct { + uint8_t obsvModeTx; /*!< RFAL's config of the ST25R3916's observation mode + while Tx */ + uint8_t obsvModeRx; /*!< RFAL's config of the ST25R3916's observation mode + while Rx */ + rfalEHandling eHandling; /*!< RFAL's error handling config/mode */ +} rfalConfigs; + +/*! Struct that holds NFC-A data - Used only inside + * rfalISO14443ATransceiveAnticollisionFrame() */ +typedef struct { + uint8_t collByte; /*!< NFC-A Anticollision collision byte */ + uint8_t *buf; /*!< NFC-A Anticollision frame buffer */ + uint8_t *bytesToSend; /*!< NFC-A Anticollision NFCID|UID byte context */ + uint8_t *bitsToSend; /*!< NFC-A Anticollision NFCID|UID bit context */ + uint16_t *rxLength; /*!< NFC-A Anticollision received length */ +} rfalNfcaWorkingData; + +/*! Struct that holds NFC-F data - Used only inside rfalFelicaPoll() */ +typedef struct { + uint16_t actLen; /* Received length */ + rfalFeliCaPollRes *pollResList; /* Location of NFC-F device list */ + uint8_t pollResListSize; /* Size of NFC-F device list */ + uint8_t devDetected; /* Number of devices detected */ + uint8_t colDetected; /* Number of collisions detected */ + uint8_t *devicesDetected; /* Location to place number of devices */ + uint8_t *collisionsDetected; /* Location to place number of collisions */ + rfalEHandling curHandling; /* RFAL's error handling */ + rfalFeliCaPollRes + pollResponses[RFAL_FELICA_POLL_MAX_SLOTS]; /* FeliCa Poll response buffer + (16 slots) */ +} rfalNfcfWorkingData; + +/*! Struct that holds NFC-V current context + * + * This buffer has to be big enough for coping with maximum response size + * (hamming coded) + * - inventory requests responses: 14*2+2 bytes + * - read single block responses: (32+4)*2+2 bytes + * - read multiple block could be very long... -> not supported + * - current implementation expects it be written in one bulk into FIFO + * - needs to be above FIFO water level of ST25R3916 (200) + * - the coding function needs to be able to + * put more than FIFO water level bytes into it (n*64+1)>200 */ +typedef struct { + uint8_t codingBuffer[((2 + 255 + 3) * 2)]; /*!< Coding buffer, length MUST + be above 257: [257; ...] */ + uint16_t nfcvOffset; /*!< Offset needed for ISO15693 coding function */ + rfalTransceiveContext origCtx; /*!< context provided by user */ + uint16_t ignoreBits; /*!< Number of bits at the beginning of a frame to be + ignored when decoding */ +} rfalNfcvWorkingData; + +/*! RFAL instance */ +typedef struct { + rfalState state; /*!< RFAL's current state */ + rfalMode mode; /*!< RFAL's current mode */ + rfalBitRate txBR; /*!< RFAL's current Tx Bit Rate */ + rfalBitRate rxBR; /*!< RFAL's current Rx Bit Rate */ + bool field; /*!< Current field state (On / Off) */ + + rfalConfigs conf; /*!< RFAL's configuration settings */ + rfalTimings timings; /*!< RFAL's timing setting */ + rfalTxRx TxRx; /*!< RFAL's transceive management */ + rfalFIFO fifo; /*!< RFAL's FIFO management */ + rfalTimers tmr; /*!< RFAL's Software timers */ + rfalCallbacks callbacks; /*!< RFAL's callbacks */ + +#if RFAL_FEATURE_LISTEN_MODE + rfalLm Lm; /*!< RFAL's listen mode management */ +#endif /* RFAL_FEATURE_LISTEN_MODE */ + +#if RFAL_FEATURE_WAKEUP_MODE + rfalWum wum; /*!< RFAL's Wake-up mode management */ +#endif /* RFAL_FEATURE_WAKEUP_MODE */ + +#if RFAL_FEATURE_LOWPOWER_MODE + rfalLpm lpm; /*!< RFAL's Low power mode management */ +#endif /* RFAL_FEATURE_LOWPOWER_MODE */ + +#if RFAL_FEATURE_NFCA + rfalNfcaWorkingData + nfcaData; /*!< RFAL's working data when supporting NFC-A */ +#endif /* RFAL_FEATURE_NFCA */ + +#if RFAL_FEATURE_NFCF + rfalNfcfWorkingData + nfcfData; /*!< RFAL's working data when supporting NFC-F */ +#endif /* RFAL_FEATURE_NFCF */ + +#if RFAL_FEATURE_NFCV + rfalNfcvWorkingData + nfcvData; /*!< RFAL's working data when performing NFC-V */ +#endif /* RFAL_FEATURE_NFCV */ + +} rfal; + +/*! Felica's command set */ +typedef enum { + FELICA_CMD_POLLING = + 0x00, /*!< Felica Poll/REQC command (aka SENSF_REQ) to identify a card */ + FELICA_CMD_POLLING_RES = + 0x01, /*!< Felica Poll/REQC command (aka SENSF_RES) response */ + FELICA_CMD_REQUEST_SERVICE = + 0x02, /*!< verify the existence of Area and Service */ + FELICA_CMD_REQUEST_RESPONSE = 0x04, /*!< verify the existence of a card */ + FELICA_CMD_READ_WITHOUT_ENCRYPTION = + 0x06, /*!< read Block Data from a Service that requires no authentication + */ + FELICA_CMD_WRITE_WITHOUT_ENCRYPTION = + 0x08, /*!< write Block Data to a Service that requires no authentication + */ + FELICA_CMD_REQUEST_SYSTEM_CODE = + 0x0C, /*!< acquire the System Code registered to a card */ + FELICA_CMD_AUTHENTICATION1 = 0x10, /*!< authenticate a card */ + FELICA_CMD_AUTHENTICATION2 = + 0x12, /*!< allow a card to authenticate a Reader/Writer */ + FELICA_CMD_READ = + 0x14, /*!< read Block Data from a Service that requires authentication */ + FELICA_CMD_WRITE = + 0x16, /*!< write Block Data to a Service that requires authentication */ +} t_rfalFeliCaCmd; + +/*! Union representing all PTMem sections */ +typedef union { /* PRQA S 0750 # MISRA 19.2 - Both members are of the same + type, just different names. Thus no problem can occur. */ + uint8_t PTMem_A[ST25R3916_PTM_A_LEN]; /*!< PT_Memory area allocated for NFC-A + configuration */ + uint8_t PTMem_F[ST25R3916_PTM_F_LEN]; /*!< PT_Memory area allocated for NFC-F + configuration */ + uint8_t TSN[ST25R3916_PTM_TSN_LEN]; /*!< PT_Memory area allocated for TSN - + Random numbers */ +} t_rfalPTMem; + +/* +****************************************************************************** +* GLOBAL DEFINES +****************************************************************************** +*/ + +#define RFAL_FIFO_IN_WL \ + 200U /*!< Number of bytes in the FIFO when WL interrupt occurs while Tx */ +#define RFAL_FIFO_OUT_WL \ + (ST25R3916_FIFO_DEPTH - \ + RFAL_FIFO_IN_WL) /*!< Number of bytes sent/out of the FIFO when WL \ + interrupt occurs while Tx */ + +#define RFAL_FIFO_STATUS_REG1 \ + 0U /*!< Location of FIFO status register 1 in local copy */ +#define RFAL_FIFO_STATUS_REG2 \ + 1U /*!< Location of FIFO status register 2 in local copy */ +#define RFAL_FIFO_STATUS_INVALID \ + 0xFFU /*!< Value indicating that the local FIFO status in invalid|cleared */ + +#define RFAL_ST25R3916_GPT_MAX_1FC \ + rfalConv8fcTo1fc(0xFFFFU) /*!< Max GPT steps in 1fc (0xFFFF steps of 8/fc => \ + 0xFFFF * 590ns = 38,7ms) */ +#define RFAL_ST25R3916_NRT_MAX_1FC \ + rfalConv4096fcTo1fc(0xFFFFU) /*!< Max NRT steps in 1fc (0xFFFF steps of \ + 4096/fc => 0xFFFF * 302us = 19.8s ) */ +#define RFAL_ST25R3916_NRT_DISABLED \ + 0U /*!< NRT Disabled: All 0 No-response timer is not started, wait forever \ + */ +#define RFAL_ST25R3916_MRT_MAX_1FC \ + rfalConv64fcTo1fc(0x00FFU) /*!< Max MRT steps in 1fc (0x00FF steps of 64/fc \ + => 0x00FF * 4.72us = 1.2ms ) */ +#define RFAL_ST25R3916_MRT_MIN_1FC \ + rfalConv64fcTo1fc(0x0004U) /*!< Min MRT steps in 1fc ( 0<=mrt<=4 ; 4 (64/fc) \ + => 0x0004 * 4.72us = 18.88us ) */ +#define RFAL_ST25R3916_GT_MAX_1FC \ + rfalConvMsTo1fc(6000U) /*!< Max GT value allowed in 1/fc (SFGI=14 => SFGT + \ + dSFGT = 5.4s) */ +#define RFAL_ST25R3916_GT_MIN_1FC \ + rfalConvMsTo1fc( \ + RFAL_ST25R3916_SW_TMR_MIN_1MS) /*!< Min GT value allowed in 1/fc */ +#define RFAL_ST25R3916_SW_TMR_MIN_1MS 1U /*!< Min value of a SW timer in ms */ + +#define RFAL_OBSMODE_DISABLE 0x00U /*!< Observation Mode disabled */ + +#define RFAL_RX_INC_BYTE_LEN \ + (uint8_t)1U /*!< Threshold where incoming rx shall be considered incomplete \ + byte NFC - T2T */ +#define RFAL_EMVCO_RX_MAXLEN \ + (uint8_t)4U /*!< Maximum value where EMVCo to apply special error handling \ + */ + +#define RFAL_NORXE_TOUT \ + 50U /*!< Timeout to be used on a potential missing RXE - Silicon ST25R3916 \ + Errata #2.1.2 */ + +#define RFAL_ISO14443A_SDD_RES_LEN \ + 5U /*!< SDD_RES | Anticollision (UID CLn) length - rfalNfcaSddRes */ +#define RFAL_ISO14443A_CRC_INTVAL \ + 0x6363 /*!< ISO14443 CRC Initial Value|Register */ + +#define RFAL_FELICA_POLL_DELAY_TIME \ + 512U /*!< FeliCa Poll Processing time is 2.417 ms ~512*64/fc Digital 1.1 A4 \ + */ +#define RFAL_FELICA_POLL_SLOT_TIME \ + 256U /*!< FeliCa Poll Time Slot duration is 1.208 ms ~256*64/fc Digital 1.1 \ + A4 */ + +#define RFAL_LM_SENSF_RD0_POS \ + 17U /*!< FeliCa SENSF_RES Request Data RD0 position */ +#define RFAL_LM_SENSF_RD1_POS \ + 18U /*!< FeliCa SENSF_RES Request Data RD1 position */ + +#define RFAL_LM_NFCID_INCOMPLETE \ + 0x04U /*!< NFCA NFCID not complete bit in SEL_RES (SAK) */ + +#define RFAL_ISO15693_IGNORE_BITS \ + rfalConvBytesToBits( \ + 2U) /*!< Ignore collisions before the UID (RES_FLAG + DSFID) */ +#define RFAL_ISO15693_INV_RES_LEN \ + 12U /*!< ISO15693 Inventory response length with CRC (bytes) */ +#define RFAL_ISO15693_INV_RES_DUR \ + 4U /*!< ISO15693 Inventory response duration @ 26 kbps (ms) */ + +#define RFAL_WU_MIN_WEIGHT_VAL \ + 4U /*!< ST25R3916 minimum Wake-up weight value \ + */ + +/*******************************************************************************/ + +#define RFAL_LM_GT \ + rfalConvUsTo1fc(100U) /*!< Listen Mode Guard Time enforced (GT - Passive; \ + TIRFG - Active) */ +#define RFAL_FDT_POLL_ADJUSTMENT \ + rfalConvUsTo1fc(80U) /*!< FDT Poll adjustment: Time between the expiration \ + of GPT to the actual Tx */ +#define RFAL_FDT_LISTEN_MRT_ADJUSTMENT \ + 64U /*!< MRT jitter adjustment: timeout will be between [ tout ; tout + 64 \ + cycles ] */ +#define RFAL_AP2P_FIELDOFF_TCMDOFF \ + 1356U /*!< Time after TXE and Field Off t,CMD,OFF Activity 2.1 3.2.1.3 \ + & C */ + +#ifndef RFAL_ST25R3916_AAT_SETTLE +#define RFAL_ST25R3916_AAT_SETTLE \ + 5U /*!< Time in ms required for AAT pins and Osc to settle after en bit set \ + */ +#endif /* RFAL_ST25R3916_AAT_SETTLE */ + +#ifndef RFAL_ST25R3916B_AAT_SETTLE +#define RFAL_ST25R3916B_AAT_SETTLE \ + ST25R3916_REG_MEAS_TX_DELAY_meas_tx_del_4_83ms /*!< Time between Oscilator \ + stable and TX On in \ + meas_tx_del steps */ +#endif /* RFAL_ST25R3916B_AAT_SETTLE */ + +/*! FWT adjustment: + * 64 : NRT jitter between TXE and NRT start */ +#define RFAL_FWT_ADJUSTMENT 64U + +/*! FWT ISO14443A adjustment: + * 512 : 4bit length + * 64 : Half a bit duration due to ST25R3916 Coherent receiver (1/fc) */ +#define RFAL_FWT_A_ADJUSTMENT (512U + 64U) + +/*! FWT ISO14443B adjustment: + * SOF (14etu) + 1Byte (10etu) + 1etu (IRQ comes 1etu after first byte) - + * 3etu (ST25R3916 sends TXE 3etu after) */ +#define RFAL_FWT_B_ADJUSTMENT (((14U + 10U + 1U) - 3U) * 128U) + +/*! FWT FeliCa 212 adjustment: + * 1024 : Length of the two Sync bytes at 212kbps */ +#define RFAL_FWT_F_212_ADJUSTMENT 1024U + +/*! FWT FeliCa 424 adjustment: + * 512 : Length of the two Sync bytes at 424kbps */ +#define RFAL_FWT_F_424_ADJUSTMENT 512U + +/*! Time between our field Off and other peer field On : Tadt + (n x Trfw) + * Ecma 340 11.1.2 - Tadt: [56.64 , 188.72] us ; n: [0 , 3] ; Trfw = 37.76 us + * Should be: 189 + (3*38) = 303us ; we'll use a more relaxed setting: 605 us */ +#define RFAL_AP2P_FIELDON_TADTTRFW rfalConvUsTo1fc(605U) + +/*! FDT Listen adjustment for ISO14443A EMVCo 2.6 4.8.1.3 ; + * Digital 1.1 6.10 + * + * 276: Time from the rising pulse of the pause of the logic '1' (i.e. the time + * point to measure the deaftime from), to the actual end of the EOF sequence + * (the point where the MRT starts). Please note that the ST25R391x uses the + * ISO14443-2 definition where the EOF consists of logic '0' followed by + * sequence Y. -64: Further adjustment for receiver to be ready just before + * first bit + */ +#define RFAL_FDT_LISTEN_A_ADJUSTMENT (276U - 64U) + +/*! FDT Listen adjustment for ISO14443B EMVCo 2.6 4.8.1.6 ; Digital 1.1 7.9 + * + * 340: Time from the rising edge of the EoS to the starting point of the MRT + * timer (sometime after the final high part of the EoS is completed) + */ +#define RFAL_FDT_LISTEN_B_ADJUSTMENT 340U + +/*! FDT Listen adjustment for ISO15693 + * ISO15693 2000 8.4 t1 MIN = 4192/fc + * ISO15693 2009 9.1 t1 MIN = 4320/fc + * Digital 2.1 B.5 FDTV,LISTEN,MIN = 4310/fc + * Set FDT Listen one step earlier than on the more recent spec versions for + * greater interoprability + */ +#define RFAL_FDT_LISTEN_V_ADJUSTMENT 64U + +/*! FDT Poll adjustment for ISO14443B Correlator - sst 5 etu */ +#define RFAL_FDT_LISTEN_B_ADJT_CORR 128U + +/*! FDT Poll adjustment for ISO14443B Correlator sst window - 5 etu */ +#define RFAL_FDT_LISTEN_B_ADJT_CORR_SST 20U + +/* +****************************************************************************** +* GLOBAL MACROS +****************************************************************************** +*/ + +/*! Calculates Transceive Sanity Timer. It accounts for the slowest bit rate and + * the longest data format 1s for transmission and reception of a 4K message at + * 106kpbs (~425ms each direction) plus TxRx preparation and FIFO load over + * Serial Interface */ +#define rfalCalcSanityTmr(fwt) (uint16_t)(1000U + rfalConv1fcToMs((fwt))) + +#define rfalGennTRFW(n) \ + ((uint8_t)(((n) + 1U) % 7U)) /*!< Generate next n*TRFW used for RFCA: modulo \ + a prime to avoid alias effects */ + +#define rfalCalcNumBytes(nBits) \ + (((uint32_t)(nBits) + 7U) / 8U) /*!< Returns the number of bytes required to \ + fit given the number of bits */ + +#define rfalTimerStart(timer, time_ms) \ + do { \ + platformTimerDestroy(timer); \ + (timer) = platformTimerCreate((uint16_t)(time_ms)); \ + } while (0) /*!< Configures and starts timer */ +#define rfalTimerisExpired(timer) \ + platformTimerIsExpired(timer) /*!< Checks if timer has expired */ +#define rfalTimerDestroy(timer) \ + platformTimerDestroy(timer) /*!< Destroys timer */ + +#define rfalST25R3916ObsModeDisable() \ + st25r3916WriteTestRegister( \ + 0x01U, (0x40U)) /*!< Disable ST25R3916 Observation mode */ +#define rfalST25R3916ObsModeTx() \ + st25r3916WriteTestRegister( \ + 0x01U, \ + (0x40U | gRFAL.conf.obsvModeTx)) /*!< Enable Tx Observation mode */ +#define rfalST25R3916ObsModeRx() \ + st25r3916WriteTestRegister( \ + 0x01U, \ + (0x40U | gRFAL.conf.obsvModeRx)) /*!< Enable Rx Observation mode */ + +#define rfalCheckDisableObsMode() \ + if (gRFAL.conf.obsvModeRx != 0U) { \ + rfalST25R3916ObsModeDisable(); \ + } /*!< Checks if the observation mode is enabled, and applies on ST25R3916 \ + */ +#define rfalCheckEnableObsModeTx() \ + if (gRFAL.conf.obsvModeTx != 0U) { \ + rfalST25R3916ObsModeTx(); \ + } /*!< Checks if the observation mode is enabled, and applies on ST25R3916 \ + */ +#define rfalCheckEnableObsModeRx() \ + if (gRFAL.conf.obsvModeRx != 0U) { \ + rfalST25R3916ObsModeRx(); \ + } /*!< Checks if the observation mode is enabled, and applies on ST25R3916 \ + */ + +#define rfalGetIncmplBits(FIFOStatus2) \ + (((FIFOStatus2) >> 1) & \ + 0x07U) /*!< Returns the number of bits from fifo status */ +#define rfalIsIncompleteByteError(error) \ + (((error) >= RFAL_ERR_INCOMPLETE_BYTE) && \ + ((error) <= RFAL_ERR_INCOMPLETE_BYTE_07)) /*!< Checks if given error is a \ + Incomplete error */ + +#define rfalAdjACBR(b) \ + (((uint16_t)(b) >= (uint16_t)RFAL_BR_52p97) \ + ? (uint16_t)(b) \ + : ((uint16_t)(b) + \ + 1U)) /*!< Adjusts ST25R391x Bit rate to Analog Configuration */ +#define rfalConvBR2ACBR(b) \ + (((rfalAdjACBR((b))) << RFAL_ANALOG_CONFIG_BITRATE_SHIFT) & \ + RFAL_ANALOG_CONFIG_BITRATE_MASK) /*!< Converts ST25R391x Bit rate to Analog \ + Configuration bit rate id */ + +#define rfalConvTDFormat(v) \ + ((uint16_t)(v) \ + << 8U) /*!< Converts a uint8_t to the format used in SW Tag Detection */ +#define rfalAddFracTDFormat(fd) ((((uint16_t)(fd)) & 0x03U) * 64U) + +#define rfalRunBlocking(e, fn) \ + do { \ + (e) = (fn); \ + rfalWorker(); \ + } while ((e) == \ + RFAL_ERR_BUSY) /*!< Macro used for the blocking operations */ + +/* + ****************************************************************************** + * LOCAL VARIABLES + ****************************************************************************** + */ + +static rfal gRFAL; /*!< RFAL module instance */ + +/* +****************************************************************************** +* LOCAL FUNCTION PROTOTYPES +****************************************************************************** +*/ + +static void rfalTransceiveTx(void); +static void rfalTransceiveRx(void); +static ReturnCode rfalTransceiveRunBlockingTx(void); +static void rfalPrepareTransceive(void); +static void rfalCleanupTransceive(void); +static void rfalErrorHandling(void); + +static ReturnCode rfalRunTransceiveWorker(void); +#if RFAL_FEATURE_LISTEN_MODE +static ReturnCode rfalRunListenModeWorker(void); +#endif /* RFAL_FEATURE_LISTEN_MODE */ +#if RFAL_FEATURE_WAKEUP_MODE +static void rfalRunWakeUpModeWorker(void); +static uint16_t rfalWakeUpModeFilter(uint16_t curRef, uint16_t curVal, + uint8_t weight); +#endif /* RFAL_FEATURE_WAKEUP_MODE */ + +static void rfalFIFOStatusUpdate(void); +static void rfalFIFOStatusClear(void); +static bool rfalFIFOStatusIsMissingPar(void); +static bool rfalFIFOStatusIsIncompleteByte(void); +static uint16_t rfalFIFOStatusGetNumBytes(void); +static uint8_t rfalFIFOGetNumIncompleteBits(void); + +/* +****************************************************************************** +* GLOBAL FUNCTIONS +****************************************************************************** +*/ + +/*******************************************************************************/ +ReturnCode rfalInitialize(void) { + ReturnCode err; + + RFAL_EXIT_ON_ERR(err, st25r3916Initialize()); + + st25r3916ClearInterrupts(); + + /* Disable any previous observation mode */ + rfalST25R3916ObsModeDisable(); + + /*******************************************************************************/ + /* Apply RF Chip generic initialization */ + rfalSetAnalogConfig( + (RFAL_ANALOG_CONFIG_TECH_CHIP | RFAL_ANALOG_CONFIG_CHIP_INIT)); + + /*******************************************************************************/ + /* Enable External Field Detector as: Automatics */ + st25r3916ChangeRegisterBits(ST25R3916_REG_OP_CONTROL, + ST25R3916_REG_OP_CONTROL_en_fd_mask, + ST25R3916_REG_OP_CONTROL_en_fd_auto_efd); + + /* Clear FIFO status local copy */ + rfalFIFOStatusClear(); + + /*******************************************************************************/ + gRFAL.state = RFAL_STATE_INIT; + gRFAL.mode = RFAL_MODE_NONE; + gRFAL.field = false; + + /* Set RFAL default configs */ + gRFAL.conf.obsvModeRx = RFAL_OBSMODE_DISABLE; + gRFAL.conf.obsvModeTx = RFAL_OBSMODE_DISABLE; + gRFAL.conf.eHandling = RFAL_ERRORHANDLING_NONE; + + /* Transceive set to IDLE */ + gRFAL.TxRx.lastState = RFAL_TXRX_STATE_IDLE; + gRFAL.TxRx.state = RFAL_TXRX_STATE_IDLE; + + /* Disable all timings */ + gRFAL.timings.FDTListen = RFAL_TIMING_NONE; + gRFAL.timings.FDTPoll = RFAL_TIMING_NONE; + gRFAL.timings.GT = RFAL_TIMING_NONE; + gRFAL.timings.nTRFW = 0U; + + /* Destroy any previous pending timers */ + rfalTimerDestroy(gRFAL.tmr.GT); + rfalTimerDestroy(gRFAL.tmr.txRx); + rfalTimerDestroy(gRFAL.tmr.RXE); + rfalTimerDestroy(gRFAL.tmr.PPON2); + gRFAL.tmr.GT = RFAL_TIMING_NONE; + gRFAL.tmr.txRx = RFAL_TIMING_NONE; + gRFAL.tmr.RXE = RFAL_TIMING_NONE; + gRFAL.tmr.PPON2 = RFAL_TIMING_NONE; + + gRFAL.callbacks.preTxRx = NULL; + gRFAL.callbacks.postTxRx = NULL; + gRFAL.callbacks.syncTxRx = NULL; + gRFAL.callbacks.lmEon = NULL; + +#if RFAL_FEATURE_NFCV + /* Initialize NFC-V Data */ + gRFAL.nfcvData.ignoreBits = 0; +#endif /* RFAL_FEATURE_NFCV */ + +#if RFAL_FEATURE_LISTEN_MODE + /* Initialize Listen Mode */ + gRFAL.Lm.state = RFAL_LM_STATE_NOT_INIT; + gRFAL.Lm.brDetected = RFAL_BR_KEEP; + gRFAL.Lm.iniFlag = false; +#endif /* RFAL_FEATURE_LISTEN_MODE */ + +#if RFAL_FEATURE_WAKEUP_MODE + /* Initialize Wake-Up Mode */ + gRFAL.wum.state = RFAL_WUM_STATE_NOT_INIT; +#endif /* RFAL_FEATURE_WAKEUP_MODE */ + +#if RFAL_FEATURE_LOWPOWER_MODE + /* Initialize Low Power Mode */ + gRFAL.lpm.isRunning = false; +#endif /* RFAL_FEATURE_LOWPOWER_MODE */ + + /*******************************************************************************/ + /* Perform Automatic Calibration (if configured to do so). * Registers set by + * rfalSetAnalogConfig will tell rfalCalibrate what to perform*/ + rfalCalibrate(); + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalCalibrate(void) { + uint16_t resValue; + + /* Check if RFAL is not initialized */ + if (gRFAL.state == RFAL_STATE_IDLE) { + return RFAL_ERR_WRONG_STATE; + } + + /*******************************************************************************/ + /* Perform ST25R3916 regulators and antenna calibration */ + /*******************************************************************************/ + + /* Automatic regulator adjustment only performed if not set manually on Analog + * Configs */ + if (st25r3916CheckReg(ST25R3916_REG_REGULATOR_CONTROL, + ST25R3916_REG_REGULATOR_CONTROL_reg_s, 0x00)) { + /* Adjust the regulators so that Antenna Calibrate has better Regulator + * values */ + rfalAdjustRegulators(&resValue); + } + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalAdjustRegulators(uint16_t *result) { + ReturnCode err; +#ifdef ST25R3916B + uint8_t reg_auxmod; + + st25r3916ReadRegister(ST25R3916_REG_AUX_MOD, ®_auxmod); + + /* Disable AWS while adjusting regulators to have full field */ + st25r3916WriteRegister(ST25R3916_REG_AUX_MOD, + (reg_auxmod & ~ST25R3916_REG_AUX_MOD_rgs_am)); +#endif /* ST25R3916B */ + + err = st25r3916AdjustRegulators(result); + +#ifdef ST25R3916B + /* Restore AWS setting */ + st25r3916WriteRegister(ST25R3916_REG_AUX_MOD, reg_auxmod); +#endif /* ST25R3916B */ + + return err; +} + +/*******************************************************************************/ +void rfalSetUpperLayerCallback(rfalUpperLayerCallback pFunc) { + st25r3916IRQCallbackSet(pFunc); +} + +/*******************************************************************************/ +void rfalSetPreTxRxCallback(rfalPreTxRxCallback pFunc) { + gRFAL.callbacks.preTxRx = pFunc; +} + +/*******************************************************************************/ +void rfalSetSyncTxRxCallback(rfalSyncTxRxCallback pFunc) { + gRFAL.callbacks.syncTxRx = pFunc; +} + +/*******************************************************************************/ +void rfalSetPostTxRxCallback(rfalPostTxRxCallback pFunc) { + gRFAL.callbacks.postTxRx = pFunc; +} + +/*******************************************************************************/ +void rfalSetLmEonCallback(rfalLmEonCallback pFunc) { + gRFAL.callbacks.lmEon = pFunc; +} + +/*******************************************************************************/ +ReturnCode rfalDeinitialize(void) { + /* Deinitialize chip */ + st25r3916Deinitialize(); + + /* Set Analog configurations for deinitialization */ + rfalSetAnalogConfig( + (RFAL_ANALOG_CONFIG_TECH_CHIP | RFAL_ANALOG_CONFIG_CHIP_DEINIT)); + + gRFAL.state = RFAL_STATE_IDLE; + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +void rfalSetObsvMode(uint32_t txMode, uint32_t rxMode) { + gRFAL.conf.obsvModeTx = (uint8_t)txMode; + gRFAL.conf.obsvModeRx = (uint8_t)rxMode; +} + +/*******************************************************************************/ +void rfalGetObsvMode(uint8_t *txMode, uint8_t *rxMode) { + if (txMode != NULL) { + *txMode = gRFAL.conf.obsvModeTx; + } + + if (rxMode != NULL) { + *rxMode = gRFAL.conf.obsvModeRx; + } +} + +/*******************************************************************************/ +void rfalDisableObsvMode(void) { + gRFAL.conf.obsvModeTx = RFAL_OBSMODE_DISABLE; + gRFAL.conf.obsvModeRx = RFAL_OBSMODE_DISABLE; +} + +/*******************************************************************************/ +ReturnCode rfalSetMode(rfalMode mode, rfalBitRate txBR, rfalBitRate rxBR) { + /* Check if RFAL is not initialized */ + if (gRFAL.state == RFAL_STATE_IDLE) { + return RFAL_ERR_WRONG_STATE; + } + + /* Check allowed bit rate value */ + if ((txBR == RFAL_BR_KEEP) || (rxBR == RFAL_BR_KEEP)) { + return RFAL_ERR_PARAM; + } + + switch (mode) { + /*******************************************************************************/ + case RFAL_MODE_POLL_NFCA: + + /* Disable wake up mode, if set */ + st25r3916ClrRegisterBits(ST25R3916_REG_OP_CONTROL, + ST25R3916_REG_OP_CONTROL_wu); + + /* Enable ISO14443A mode */ + st25r3916WriteRegister(ST25R3916_REG_MODE, + ST25R3916_REG_MODE_om_iso14443a); + + /* Set Analog configurations for this mode and bit rate */ + rfalSetAnalogConfig( + (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCA | + RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_TX)); + rfalSetAnalogConfig( + (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCA | + RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_RX)); + break; + + /*******************************************************************************/ + case RFAL_MODE_POLL_NFCA_T1T: + /* Disable wake up mode, if set */ + st25r3916ClrRegisterBits(ST25R3916_REG_OP_CONTROL, + ST25R3916_REG_OP_CONTROL_wu); + + /* Enable Topaz mode */ + st25r3916WriteRegister(ST25R3916_REG_MODE, ST25R3916_REG_MODE_om_topaz); + + /* Set Analog configurations for this mode and bit rate */ + rfalSetAnalogConfig( + (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCA | + RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_TX)); + rfalSetAnalogConfig( + (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCA | + RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_RX)); + break; + + /*******************************************************************************/ + case RFAL_MODE_POLL_NFCB: + + /* Disable wake up mode, if set */ + st25r3916ClrRegisterBits(ST25R3916_REG_OP_CONTROL, + ST25R3916_REG_OP_CONTROL_wu); + + /* Enable ISO14443B mode */ + st25r3916WriteRegister(ST25R3916_REG_MODE, + ST25R3916_REG_MODE_om_iso14443b); + + /* Set the EGT, SOF, EOF and EOF */ + st25r3916ChangeRegisterBits( + ST25R3916_REG_ISO14443B_1, + (ST25R3916_REG_ISO14443B_1_egt_mask | + ST25R3916_REG_ISO14443B_1_sof_mask | ST25R3916_REG_ISO14443B_1_eof), + ((0U << ST25R3916_REG_ISO14443B_1_egt_shift) | + ST25R3916_REG_ISO14443B_1_sof_0_10etu | + ST25R3916_REG_ISO14443B_1_sof_1_2etu | + ST25R3916_REG_ISO14443B_1_eof_10etu)); + + /* Set the minimum TR1, SOF, EOF and EOF12 */ + st25r3916ChangeRegisterBits( + ST25R3916_REG_ISO14443B_2, + (ST25R3916_REG_ISO14443B_2_tr1_mask | + ST25R3916_REG_ISO14443B_2_no_sof | ST25R3916_REG_ISO14443B_2_no_eof), + (ST25R3916_REG_ISO14443B_2_tr1_80fs80fs)); + + /* Set Analog configurations for this mode and bit rate */ + rfalSetAnalogConfig( + (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCB | + RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_TX)); + rfalSetAnalogConfig( + (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCB | + RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_RX)); + break; + + /*******************************************************************************/ + case RFAL_MODE_POLL_B_PRIME: + + /* Disable wake up mode, if set */ + st25r3916ClrRegisterBits(ST25R3916_REG_OP_CONTROL, + ST25R3916_REG_OP_CONTROL_wu); + + /* Enable ISO14443B mode */ + st25r3916WriteRegister(ST25R3916_REG_MODE, + ST25R3916_REG_MODE_om_iso14443b); + + /* Set the EGT, SOF, EOF and EOF */ + st25r3916ChangeRegisterBits( + ST25R3916_REG_ISO14443B_1, + (ST25R3916_REG_ISO14443B_1_egt_mask | + ST25R3916_REG_ISO14443B_1_sof_mask | ST25R3916_REG_ISO14443B_1_eof), + ((0U << ST25R3916_REG_ISO14443B_1_egt_shift) | + ST25R3916_REG_ISO14443B_1_sof_0_10etu | + ST25R3916_REG_ISO14443B_1_sof_1_2etu | + ST25R3916_REG_ISO14443B_1_eof_10etu)); + + /* Set the minimum TR1, EOF and EOF12 */ + st25r3916ChangeRegisterBits( + ST25R3916_REG_ISO14443B_2, + (ST25R3916_REG_ISO14443B_2_tr1_mask | + ST25R3916_REG_ISO14443B_2_no_sof | ST25R3916_REG_ISO14443B_2_no_eof), + (ST25R3916_REG_ISO14443B_2_tr1_80fs80fs | + ST25R3916_REG_ISO14443B_2_no_sof)); + + /* Set Analog configurations for this mode and bit rate */ + rfalSetAnalogConfig( + (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCB | + RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_TX)); + rfalSetAnalogConfig( + (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCB | + RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_RX)); + break; + + /*******************************************************************************/ + case RFAL_MODE_POLL_B_CTS: + + /* Disable wake up mode, if set */ + st25r3916ClrRegisterBits(ST25R3916_REG_OP_CONTROL, + ST25R3916_REG_OP_CONTROL_wu); + + /* Enable ISO14443B mode */ + st25r3916WriteRegister(ST25R3916_REG_MODE, + ST25R3916_REG_MODE_om_iso14443b); + + /* Set the EGT, SOF, EOF and EOF */ + st25r3916ChangeRegisterBits( + ST25R3916_REG_ISO14443B_1, + (ST25R3916_REG_ISO14443B_1_egt_mask | + ST25R3916_REG_ISO14443B_1_sof_mask | ST25R3916_REG_ISO14443B_1_eof), + ((0U << ST25R3916_REG_ISO14443B_1_egt_shift) | + ST25R3916_REG_ISO14443B_1_sof_0_10etu | + ST25R3916_REG_ISO14443B_1_sof_1_2etu | + ST25R3916_REG_ISO14443B_1_eof_10etu)); + + /* Set the minimum TR1, clear SOF, EOF and EOF12 */ + st25r3916ChangeRegisterBits( + ST25R3916_REG_ISO14443B_2, + (ST25R3916_REG_ISO14443B_2_tr1_mask | + ST25R3916_REG_ISO14443B_2_no_sof | ST25R3916_REG_ISO14443B_2_no_eof), + (ST25R3916_REG_ISO14443B_2_tr1_80fs80fs | + ST25R3916_REG_ISO14443B_2_no_sof | + ST25R3916_REG_ISO14443B_2_no_eof)); + + /* Set Analog configurations for this mode and bit rate */ + rfalSetAnalogConfig( + (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCB | + RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_TX)); + rfalSetAnalogConfig( + (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCB | + RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_RX)); + break; + + /*******************************************************************************/ + case RFAL_MODE_POLL_NFCF: + + /* Disable wake up mode, if set */ + st25r3916ClrRegisterBits(ST25R3916_REG_OP_CONTROL, + ST25R3916_REG_OP_CONTROL_wu); + + /* Enable FeliCa mode */ + st25r3916WriteRegister(ST25R3916_REG_MODE, ST25R3916_REG_MODE_om_felica); + + /* Set Analog configurations for this mode and bit rate */ + rfalSetAnalogConfig( + (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCF | + RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_TX)); + rfalSetAnalogConfig( + (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCF | + RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_RX)); + break; + + /*******************************************************************************/ + case RFAL_MODE_POLL_NFCV: + case RFAL_MODE_POLL_PICOPASS: + +#if !RFAL_FEATURE_NFCV + return RFAL_ERR_DISABLED; +#else + + /* Disable wake up mode, if set */ + st25r3916ClrRegisterBits(ST25R3916_REG_OP_CONTROL, + ST25R3916_REG_OP_CONTROL_wu); + + /* Set Analog configurations for this mode and bit rate */ + rfalSetAnalogConfig( + (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCV | + RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_TX)); + rfalSetAnalogConfig( + (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCV | + RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_RX)); + break; + +#endif /* RFAL_FEATURE_NFCV */ + + /*******************************************************************************/ + case RFAL_MODE_POLL_ACTIVE_P2P: + + /* Set NFCIP1 active communication Initiator mode and Automatic Response + * RF Collision Avoidance to always after EOF */ + st25r3916WriteRegister( + ST25R3916_REG_MODE, + (ST25R3916_REG_MODE_targ_init | ST25R3916_REG_MODE_om_nfc | + ST25R3916_REG_MODE_nfc_ar_eof)); + + /* External Field Detector enabled as Automatics on rfalInitialize() */ + + /* Set NRT to start at end of TX (own) field */ + st25r3916ChangeRegisterBits(ST25R3916_REG_TIMER_EMV_CONTROL, + ST25R3916_REG_TIMER_EMV_CONTROL_nrt_nfc, + ST25R3916_REG_TIMER_EMV_CONTROL_nrt_nfc_off); + + /* Set GPT to start after end of TX, as GPT is used in active + * communication mode to timeout the field switching off after TXE */ + st25r3916SetStartGPTimer( + (uint16_t)rfalConv1fcTo8fc(RFAL_AP2P_FIELDOFF_TCMDOFF), + ST25R3916_REG_TIMER_EMV_CONTROL_gptc_etx_nfc); + + /* Set PPon2 timer with the max time between our field Off and other peer + * field On : Tadt + (n x Trfw) */ + st25r3916WriteRegister( + ST25R3916_REG_PPON2, + (uint8_t)rfalConv1fcTo64fc(RFAL_AP2P_FIELDON_TADTTRFW)); + + /* Set Analog configurations for this mode and bit rate */ + rfalSetAnalogConfig( + (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_AP2P | + RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_TX)); + rfalSetAnalogConfig( + (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_AP2P | + RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_RX)); + break; + + /*******************************************************************************/ + case RFAL_MODE_LISTEN_ACTIVE_P2P: + + /* Set NFCIP1 active communication Target mode and Automatic Response RF + * Collision Avoidance to always after EOF */ + st25r3916WriteRegister( + ST25R3916_REG_MODE, + (ST25R3916_REG_MODE_targ_targ | ST25R3916_REG_MODE_om_targ_nfcip | + ST25R3916_REG_MODE_nfc_ar_eof)); + + /* Set TARFG: 0 (75us+0ms=75us), as Target no Guard time needed */ + st25r3916WriteRegister(ST25R3916_REG_FIELD_ON_GT, 0U); + + /* External Field Detector enabled as Automatics on rfalInitialize() */ + + /* Set NRT to start at end of TX (own) field */ + st25r3916ChangeRegisterBits(ST25R3916_REG_TIMER_EMV_CONTROL, + ST25R3916_REG_TIMER_EMV_CONTROL_nrt_nfc, + ST25R3916_REG_TIMER_EMV_CONTROL_nrt_nfc_off); + + /* Set GPT to start after end of TX, as GPT is used in active + * communication mode to timeout the field switching off after TXE */ + st25r3916SetStartGPTimer( + (uint16_t)rfalConv1fcTo8fc(RFAL_AP2P_FIELDOFF_TCMDOFF), + ST25R3916_REG_TIMER_EMV_CONTROL_gptc_etx_nfc); + + /* Set PPon2 timer with the max time between our field Off and other peer + * field On : Tadt + (n x Trfw) */ + st25r3916WriteRegister( + ST25R3916_REG_PPON2, + (uint8_t)rfalConv1fcTo64fc(RFAL_AP2P_FIELDON_TADTTRFW)); + + /* Set Analog configurations for this mode and bit rate */ + rfalSetAnalogConfig( + (RFAL_ANALOG_CONFIG_LISTEN | RFAL_ANALOG_CONFIG_TECH_AP2P | + RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_TX)); + rfalSetAnalogConfig( + (RFAL_ANALOG_CONFIG_LISTEN | RFAL_ANALOG_CONFIG_TECH_AP2P | + RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_RX)); + break; + + /*******************************************************************************/ + case RFAL_MODE_LISTEN_NFCA: + + /* Disable wake up mode, if set */ + st25r3916ClrRegisterBits(ST25R3916_REG_OP_CONTROL, + ST25R3916_REG_OP_CONTROL_wu); + + /* Enable Passive Target NFC-A mode, disable any Collision Avoidance */ + st25r3916WriteRegister( + ST25R3916_REG_MODE, + (ST25R3916_REG_MODE_targ | ST25R3916_REG_MODE_om_targ_nfca | + ST25R3916_REG_MODE_nfc_ar_off)); + + /* Set Analog configurations for this mode */ + rfalSetAnalogConfig( + (RFAL_ANALOG_CONFIG_LISTEN | RFAL_ANALOG_CONFIG_TECH_NFCA | + RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_TX)); + rfalSetAnalogConfig( + (RFAL_ANALOG_CONFIG_LISTEN | RFAL_ANALOG_CONFIG_TECH_NFCA | + RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_RX)); + break; + + /*******************************************************************************/ + case RFAL_MODE_LISTEN_NFCF: + + /* Disable wake up mode, if set */ + st25r3916ClrRegisterBits(ST25R3916_REG_OP_CONTROL, + ST25R3916_REG_OP_CONTROL_wu); + + /* Enable Passive Target NFC-F mode, disable any Collision Avoidance */ + st25r3916WriteRegister( + ST25R3916_REG_MODE, + (ST25R3916_REG_MODE_targ | ST25R3916_REG_MODE_om_targ_nfcf | + ST25R3916_REG_MODE_nfc_ar_off)); + + /* Set Analog configurations for this mode */ + rfalSetAnalogConfig( + (RFAL_ANALOG_CONFIG_LISTEN | RFAL_ANALOG_CONFIG_TECH_NFCF | + RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_TX)); + rfalSetAnalogConfig( + (RFAL_ANALOG_CONFIG_LISTEN | RFAL_ANALOG_CONFIG_TECH_NFCF | + RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_RX)); + break; + + /*******************************************************************************/ + case RFAL_MODE_LISTEN_NFCB: + return RFAL_ERR_NOTSUPP; + + /*******************************************************************************/ + default: + return RFAL_ERR_NOT_IMPLEMENTED; + } + + /* Set state as STATE_MODE_SET only if not initialized yet (PSL) */ + gRFAL.state = + ((gRFAL.state < RFAL_STATE_MODE_SET) ? RFAL_STATE_MODE_SET : gRFAL.state); + gRFAL.mode = mode; + + /* Apply the given bit rate */ + return rfalSetBitRate(txBR, rxBR); +} + +/*******************************************************************************/ +rfalMode rfalGetMode(void) { return gRFAL.mode; } + +/*******************************************************************************/ +ReturnCode rfalSetBitRate(rfalBitRate txBR, rfalBitRate rxBR) { + ReturnCode ret; + + /* Check if RFAL is not initialized */ + if (gRFAL.state == RFAL_STATE_IDLE) { + return RFAL_ERR_WRONG_STATE; + } + + /* Store the new Bit Rates */ + gRFAL.txBR = ((txBR == RFAL_BR_KEEP) ? gRFAL.txBR : txBR); + gRFAL.rxBR = ((rxBR == RFAL_BR_KEEP) ? gRFAL.rxBR : rxBR); + + /* Update the bitrate reg if not in NFCV mode (streaming) */ + if ((RFAL_MODE_POLL_NFCV != gRFAL.mode) && + (RFAL_MODE_POLL_PICOPASS != gRFAL.mode)) { + /* Set bit rate register */ + RFAL_EXIT_ON_ERR( + ret, st25r3916SetBitrate((uint8_t)gRFAL.txBR, (uint8_t)gRFAL.rxBR)); + } + + switch (gRFAL.mode) { + /*******************************************************************************/ + case RFAL_MODE_POLL_NFCA: + case RFAL_MODE_POLL_NFCA_T1T: + + /* Set Analog configurations for this bit rate */ + rfalSetAnalogConfig( + (RFAL_ANALOG_CONFIG_TECH_CHIP | RFAL_ANALOG_CONFIG_CHIP_POLL_COMMON)); + rfalSetAnalogConfig((rfalAnalogConfigId)(RFAL_ANALOG_CONFIG_POLL | + RFAL_ANALOG_CONFIG_TECH_NFCA | + rfalConvBR2ACBR(gRFAL.txBR) | + RFAL_ANALOG_CONFIG_TX)); + rfalSetAnalogConfig((rfalAnalogConfigId)(RFAL_ANALOG_CONFIG_POLL | + RFAL_ANALOG_CONFIG_TECH_NFCA | + rfalConvBR2ACBR(gRFAL.rxBR) | + RFAL_ANALOG_CONFIG_RX)); + break; + + /*******************************************************************************/ + case RFAL_MODE_POLL_NFCB: + case RFAL_MODE_POLL_B_PRIME: + case RFAL_MODE_POLL_B_CTS: + + /* Set Analog configurations for this bit rate */ + rfalSetAnalogConfig( + (RFAL_ANALOG_CONFIG_TECH_CHIP | RFAL_ANALOG_CONFIG_CHIP_POLL_COMMON)); + rfalSetAnalogConfig((rfalAnalogConfigId)(RFAL_ANALOG_CONFIG_POLL | + RFAL_ANALOG_CONFIG_TECH_NFCB | + rfalConvBR2ACBR(gRFAL.txBR) | + RFAL_ANALOG_CONFIG_TX)); + rfalSetAnalogConfig((rfalAnalogConfigId)(RFAL_ANALOG_CONFIG_POLL | + RFAL_ANALOG_CONFIG_TECH_NFCB | + rfalConvBR2ACBR(gRFAL.rxBR) | + RFAL_ANALOG_CONFIG_RX)); + break; + + /*******************************************************************************/ + case RFAL_MODE_POLL_NFCF: + + /* Set Analog configurations for this bit rate */ + rfalSetAnalogConfig( + (RFAL_ANALOG_CONFIG_TECH_CHIP | RFAL_ANALOG_CONFIG_CHIP_POLL_COMMON)); + rfalSetAnalogConfig((rfalAnalogConfigId)(RFAL_ANALOG_CONFIG_POLL | + RFAL_ANALOG_CONFIG_TECH_NFCF | + rfalConvBR2ACBR(gRFAL.txBR) | + RFAL_ANALOG_CONFIG_TX)); + rfalSetAnalogConfig((rfalAnalogConfigId)(RFAL_ANALOG_CONFIG_POLL | + RFAL_ANALOG_CONFIG_TECH_NFCF | + rfalConvBR2ACBR(gRFAL.rxBR) | + RFAL_ANALOG_CONFIG_RX)); + break; + + /*******************************************************************************/ + case RFAL_MODE_POLL_NFCV: + case RFAL_MODE_POLL_PICOPASS: + +#if !RFAL_FEATURE_NFCV + return RFAL_ERR_DISABLED; +#else + + if (((gRFAL.rxBR != RFAL_BR_26p48) && (gRFAL.rxBR != RFAL_BR_52p97)) || + ((gRFAL.txBR != RFAL_BR_1p66) && (gRFAL.txBR != RFAL_BR_26p48))) { + return RFAL_ERR_PARAM; + } + + { + const struct iso15693StreamConfig *rfalIso15693StreamConfig; + struct st25r3916StreamConfig st25rStreamConf; + rfalIso15693PhyConfig_t config; + + config.coding = + ((gRFAL.txBR == RFAL_BR_1p66) ? ISO15693_VCD_CODING_1_256 + : ISO15693_VCD_CODING_1_4); + switch (gRFAL.rxBR) { + case RFAL_BR_52p97: /* PRQA S 2880 # MISRA 2.1 - Inconsistently + marked as unreachable code */ + config.speedMode = 1; + break; + default: + config.speedMode = 0; + break; + } + + rfalIso15693PhyConfigure(&config, &rfalIso15693StreamConfig); + + /* MISRA 11.3 - Cannot point directly into different object type, copy + * to local var */ + st25rStreamConf.din = rfalIso15693StreamConfig->din; + st25rStreamConf.dout = rfalIso15693StreamConfig->dout; + st25rStreamConf.report_period_length = + rfalIso15693StreamConfig->report_period_length; + st25rStreamConf.useBPSK = rfalIso15693StreamConfig->useBPSK; + st25r3916StreamConfigure(&st25rStreamConf); + } + + /* Set Analog configurations for this bit rate */ + rfalSetAnalogConfig( + (RFAL_ANALOG_CONFIG_TECH_CHIP | RFAL_ANALOG_CONFIG_CHIP_POLL_COMMON)); + rfalSetAnalogConfig((rfalAnalogConfigId)(RFAL_ANALOG_CONFIG_POLL | + RFAL_ANALOG_CONFIG_TECH_NFCV | + rfalConvBR2ACBR(gRFAL.txBR) | + RFAL_ANALOG_CONFIG_TX)); + rfalSetAnalogConfig((rfalAnalogConfigId)(RFAL_ANALOG_CONFIG_POLL | + RFAL_ANALOG_CONFIG_TECH_NFCV | + rfalConvBR2ACBR(gRFAL.rxBR) | + RFAL_ANALOG_CONFIG_RX)); + break; + +#endif /* RFAL_FEATURE_NFCV */ + + /*******************************************************************************/ + case RFAL_MODE_POLL_ACTIVE_P2P: + + /* Set Analog configurations for this bit rate */ + rfalSetAnalogConfig( + (RFAL_ANALOG_CONFIG_TECH_CHIP | RFAL_ANALOG_CONFIG_CHIP_POLL_COMMON)); + rfalSetAnalogConfig((rfalAnalogConfigId)(RFAL_ANALOG_CONFIG_POLL | + RFAL_ANALOG_CONFIG_TECH_AP2P | + rfalConvBR2ACBR(gRFAL.txBR) | + RFAL_ANALOG_CONFIG_TX)); + rfalSetAnalogConfig((rfalAnalogConfigId)(RFAL_ANALOG_CONFIG_POLL | + RFAL_ANALOG_CONFIG_TECH_AP2P | + rfalConvBR2ACBR(gRFAL.rxBR) | + RFAL_ANALOG_CONFIG_RX)); + break; + + /*******************************************************************************/ + case RFAL_MODE_LISTEN_ACTIVE_P2P: + + /* Set Analog configurations for this bit rate */ + rfalSetAnalogConfig((RFAL_ANALOG_CONFIG_TECH_CHIP | + RFAL_ANALOG_CONFIG_CHIP_LISTEN_COMMON)); + rfalSetAnalogConfig((rfalAnalogConfigId)(RFAL_ANALOG_CONFIG_LISTEN | + RFAL_ANALOG_CONFIG_TECH_AP2P | + rfalConvBR2ACBR(gRFAL.txBR) | + RFAL_ANALOG_CONFIG_TX)); + rfalSetAnalogConfig((rfalAnalogConfigId)(RFAL_ANALOG_CONFIG_LISTEN | + RFAL_ANALOG_CONFIG_TECH_AP2P | + rfalConvBR2ACBR(gRFAL.rxBR) | + RFAL_ANALOG_CONFIG_RX)); + break; + + /*******************************************************************************/ + case RFAL_MODE_LISTEN_NFCA: + + /* Set Analog configurations for this bit rate */ + rfalSetAnalogConfig((RFAL_ANALOG_CONFIG_TECH_CHIP | + RFAL_ANALOG_CONFIG_CHIP_LISTEN_COMMON)); + rfalSetAnalogConfig((rfalAnalogConfigId)(RFAL_ANALOG_CONFIG_LISTEN | + RFAL_ANALOG_CONFIG_TECH_NFCA | + rfalConvBR2ACBR(gRFAL.txBR) | + RFAL_ANALOG_CONFIG_TX)); + rfalSetAnalogConfig((rfalAnalogConfigId)(RFAL_ANALOG_CONFIG_LISTEN | + RFAL_ANALOG_CONFIG_TECH_NFCA | + rfalConvBR2ACBR(gRFAL.rxBR) | + RFAL_ANALOG_CONFIG_RX)); + break; + + /*******************************************************************************/ + case RFAL_MODE_LISTEN_NFCF: + + /* Set Analog configurations for this bit rate */ + rfalSetAnalogConfig((RFAL_ANALOG_CONFIG_TECH_CHIP | + RFAL_ANALOG_CONFIG_CHIP_LISTEN_COMMON)); + rfalSetAnalogConfig((rfalAnalogConfigId)(RFAL_ANALOG_CONFIG_LISTEN | + RFAL_ANALOG_CONFIG_TECH_NFCF | + rfalConvBR2ACBR(gRFAL.txBR) | + RFAL_ANALOG_CONFIG_TX)); + rfalSetAnalogConfig((rfalAnalogConfigId)(RFAL_ANALOG_CONFIG_LISTEN | + RFAL_ANALOG_CONFIG_TECH_NFCF | + rfalConvBR2ACBR(gRFAL.rxBR) | + RFAL_ANALOG_CONFIG_RX)); + break; + + /*******************************************************************************/ + case RFAL_MODE_LISTEN_NFCB: + case RFAL_MODE_NONE: + return RFAL_ERR_WRONG_STATE; + + /*******************************************************************************/ + default: + return RFAL_ERR_NOT_IMPLEMENTED; + } + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalGetBitRate(rfalBitRate *txBR, rfalBitRate *rxBR) { + if ((gRFAL.state == RFAL_STATE_IDLE) || (gRFAL.mode == RFAL_MODE_NONE)) { + return RFAL_ERR_WRONG_STATE; + } + + if (txBR != NULL) { + *txBR = gRFAL.txBR; + } + + if (rxBR != NULL) { + *rxBR = gRFAL.rxBR; + } + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +void rfalSetErrorHandling(rfalEHandling eHandling) { + switch (eHandling) { + case RFAL_ERRORHANDLING_NONE: + st25r3916ClrRegisterBits(ST25R3916_REG_EMD_SUP_CONF, + ST25R3916_REG_EMD_SUP_CONF_emd_emv); + break; + + case RFAL_ERRORHANDLING_EMD: + /* MISRA 16.4: no empty default statement (in case RFAL_SW_EMD is defined) + */ +#ifndef RFAL_SW_EMD + st25r3916ModifyRegister( + ST25R3916_REG_EMD_SUP_CONF, + (ST25R3916_REG_EMD_SUP_CONF_emd_emv | + ST25R3916_REG_EMD_SUP_CONF_emd_thld_mask), + (ST25R3916_REG_EMD_SUP_CONF_emd_emv_on | RFAL_EMVCO_RX_MAXLEN)); +#endif /* RFAL_SW_EMD */ + break; + default: + /* MISRA 16.4: no empty default statement (a comment being enough) */ + break; + } + + gRFAL.conf.eHandling = eHandling; +} + +/*******************************************************************************/ +rfalEHandling rfalGetErrorHandling(void) { return gRFAL.conf.eHandling; } + +/*******************************************************************************/ +void rfalSetFDTPoll(uint32_t FDTPoll) { + gRFAL.timings.FDTPoll = RFAL_MIN(FDTPoll, RFAL_ST25R3916_GPT_MAX_1FC); +} + +/*******************************************************************************/ +uint32_t rfalGetFDTPoll(void) { return gRFAL.timings.FDTPoll; } + +/*******************************************************************************/ +void rfalSetFDTListen(uint32_t FDTListen) { + gRFAL.timings.FDTListen = RFAL_MIN(FDTListen, RFAL_ST25R3916_MRT_MAX_1FC); +} + +/*******************************************************************************/ +uint32_t rfalGetFDTListen(void) { return gRFAL.timings.FDTListen; } + +/*******************************************************************************/ +void rfalSetGT(uint32_t GT) { + gRFAL.timings.GT = RFAL_MIN(GT, RFAL_ST25R3916_GT_MAX_1FC); +} + +/*******************************************************************************/ +uint32_t rfalGetGT(void) { return gRFAL.timings.GT; } + +/*******************************************************************************/ +bool rfalIsGTExpired(void) { + if (gRFAL.tmr.GT != RFAL_TIMING_NONE) { + if (!rfalTimerisExpired(gRFAL.tmr.GT)) { + return false; + } + } + return true; +} + +/*******************************************************************************/ +ReturnCode rfalFieldOnAndStartGT(void) { + ReturnCode ret; + + /* Check if RFAL has been initialized (Oscillator should be running) and also + * if a direct register access has been performed and left the Oscillator Off + */ + if ((!st25r3916IsOscOn()) || (gRFAL.state < RFAL_STATE_INIT)) { + return RFAL_ERR_WRONG_STATE; + } + + ret = RFAL_ERR_NONE; + + /* Set Analog configurations for Field On event */ + rfalSetAnalogConfig( + (RFAL_ANALOG_CONFIG_TECH_CHIP | RFAL_ANALOG_CONFIG_CHIP_FIELD_ON)); + + /*******************************************************************************/ + /* Perform collision avoidance and turn field On if not already On */ + if ((!st25r3916IsTxEnabled()) || (!gRFAL.field)) { + /* Set TARFG: 0 (75us+0ms=75us), GT is fulfilled using a SW timer */ + st25r3916WriteRegister(ST25R3916_REG_FIELD_ON_GT, 0U); + + /* Set External Field Detector as: Collision Avoidance Detection */ + st25r3916ChangeRegisterBits(ST25R3916_REG_OP_CONTROL, + ST25R3916_REG_OP_CONTROL_en_fd_mask, + ST25R3916_REG_OP_CONTROL_en_fd_manual_efd_ca); + + /* Use Thresholds set by AnalogConfig */ + ret = st25r3916PerformCollisionAvoidance( + ST25R3916_CMD_INITIAL_RF_COLLISION, ST25R3916_THRESHOLD_DO_NOT_SET, + ST25R3916_THRESHOLD_DO_NOT_SET, + (ST25R3916_REG_AUX_nfc_n_mask & gRFAL.timings.nTRFW)); + + /* Restore External Field Detector as: Automatics */ + st25r3916ChangeRegisterBits(ST25R3916_REG_OP_CONTROL, + ST25R3916_REG_OP_CONTROL_en_fd_mask, + ST25R3916_REG_OP_CONTROL_en_fd_auto_efd); + + /* n * TRFW timing shall vary Activity 2.1 3.3.1.1 */ + gRFAL.timings.nTRFW = rfalGennTRFW(gRFAL.timings.nTRFW); + + gRFAL.field = st25r3916IsTxEnabled(); + + /* Only turn on Receiver and Transmitter if field was successfully turned On + */ + if (gRFAL.field) { + st25r3916TxRxOn(); /* Enable Tx and Rx (Tx is already On)*/ + } + } + + /*******************************************************************************/ + /* Start GT timer in case the GT value is set */ + if ((gRFAL.timings.GT != RFAL_TIMING_NONE)) { + /* Ensure that a SW timer doesn't have a lower value then the minimum */ + rfalTimerStart(gRFAL.tmr.GT, + rfalConv1fcToMs(RFAL_MAX((gRFAL.timings.GT), + RFAL_ST25R3916_GT_MIN_1FC))); + } + + return ret; +} + +/*******************************************************************************/ +ReturnCode rfalFieldOff(void) { + /* Check whether a TxRx is not yet finished */ + if (gRFAL.TxRx.state != RFAL_TXRX_STATE_IDLE) { + rfalCleanupTransceive(); + } + + /* Disable Tx and Rx */ + st25r3916TxRxOff(); + + /* Set Analog configurations for Field Off event */ + rfalSetAnalogConfig( + (RFAL_ANALOG_CONFIG_TECH_CHIP | RFAL_ANALOG_CONFIG_CHIP_FIELD_OFF)); + gRFAL.field = false; + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalStartTransceive(const rfalTransceiveContext *ctx) { + uint32_t FxTAdj; /* FWT or FDT adjustment calculation */ + + /* Check for valid parameters */ + if (ctx == NULL) { + return RFAL_ERR_PARAM; + } + + /* If parity check is disabled CRC check must be disabled as well */ + if (((ctx->flags & (uint32_t)RFAL_TXRX_FLAGS_PAR_RX_KEEP) != 0U) && + ((ctx->flags & (uint32_t)RFAL_TXRX_FLAGS_CRC_RX_MANUAL) == 0U)) { + return RFAL_ERR_NOTSUPP; + } + + /* Ensure that RFAL is already Initialized and the mode has been set */ + if (gRFAL.state >= RFAL_STATE_MODE_SET) { + /*******************************************************************************/ + /* Check whether the field is already On, otherwise no TXE will be received + */ + if ((!st25r3916IsTxEnabled()) && + ((!rfalIsModePassiveListen(gRFAL.mode)) && (ctx->txBuf != NULL))) { + return RFAL_ERR_WRONG_STATE; + } + + gRFAL.TxRx.ctx = *ctx; + + /*******************************************************************************/ + if (gRFAL.timings.FDTListen != RFAL_TIMING_NONE) { + /* Calculate MRT adjustment accordingly to the current mode */ + FxTAdj = RFAL_FDT_LISTEN_MRT_ADJUSTMENT; + if (gRFAL.mode == RFAL_MODE_POLL_NFCA) { + FxTAdj += (uint32_t)RFAL_FDT_LISTEN_A_ADJUSTMENT; + } + if (gRFAL.mode == RFAL_MODE_POLL_NFCA_T1T) { + FxTAdj += (uint32_t)RFAL_FDT_LISTEN_A_ADJUSTMENT; + } + if (gRFAL.mode == RFAL_MODE_POLL_NFCB) { + FxTAdj += (uint32_t)RFAL_FDT_LISTEN_B_ADJUSTMENT; + } + if (gRFAL.mode == RFAL_MODE_POLL_NFCV) { + FxTAdj += (uint32_t)RFAL_FDT_LISTEN_V_ADJUSTMENT; + } + + /* Ensure that MRT is using 64/fc steps */ + st25r3916ClrRegisterBits(ST25R3916_REG_TIMER_EMV_CONTROL, + ST25R3916_REG_TIMER_EMV_CONTROL_mrt_step); + + /* If Correlator is being used further adjustment is required for NFCB */ + if (gRFAL.mode == RFAL_MODE_POLL_NFCB) { + if (st25r3916CheckReg(ST25R3916_REG_AUX, ST25R3916_REG_AUX_dis_corr, + 0x00U)) { + FxTAdj += + (uint32_t)RFAL_FDT_LISTEN_B_ADJT_CORR; /* Reduce FDT(Listen) */ + st25r3916SetRegisterBits( + ST25R3916_REG_CORR_CONF1, + ST25R3916_REG_CORR_CONF1_corr_s3); /* Ensure BPSK start to 33 + pilot pulses */ + st25r3916ChangeRegisterBits( + ST25R3916_REG_SUBC_START_TIME, + ST25R3916_REG_SUBC_START_TIME_sst_mask, + RFAL_FDT_LISTEN_B_ADJT_CORR_SST); /* Set sst */ + } + } + + /* Set Minimum FDT(Listen) in which PICC is not allowed to send a response + */ + st25r3916WriteRegister( + ST25R3916_REG_MASK_RX_TIMER, + (uint8_t)rfalConv1fcTo64fc((FxTAdj > gRFAL.timings.FDTListen) + ? RFAL_ST25R3916_MRT_MIN_1FC + : (gRFAL.timings.FDTListen - FxTAdj))); + } + + /*******************************************************************************/ + /* FDT Poll will be loaded in rfalPrepareTransceive() once the previous was + * expired */ + + /*******************************************************************************/ + if ((gRFAL.TxRx.ctx.fwt != RFAL_FWT_NONE) && (gRFAL.TxRx.ctx.fwt != 0U)) { + /* Ensure proper timing configuration */ + if (gRFAL.timings.FDTListen >= gRFAL.TxRx.ctx.fwt) { + return RFAL_ERR_PARAM; + } + + FxTAdj = RFAL_FWT_ADJUSTMENT; + if (gRFAL.mode == RFAL_MODE_POLL_NFCA) { + FxTAdj += (uint32_t)RFAL_FWT_A_ADJUSTMENT; + } + if (gRFAL.mode == RFAL_MODE_POLL_NFCA_T1T) { + FxTAdj += (uint32_t)RFAL_FWT_A_ADJUSTMENT; + } + if (gRFAL.mode == RFAL_MODE_POLL_NFCB) { + FxTAdj += (uint32_t)RFAL_FWT_B_ADJUSTMENT; + } + if ((gRFAL.mode == RFAL_MODE_POLL_NFCF) || + (gRFAL.mode == RFAL_MODE_POLL_ACTIVE_P2P)) { + FxTAdj += + (uint32_t)((gRFAL.txBR == RFAL_BR_212) ? RFAL_FWT_F_212_ADJUSTMENT + : RFAL_FWT_F_424_ADJUSTMENT); + } + + /* Ensure that the given FWT doesn't exceed NRT maximum */ + gRFAL.TxRx.ctx.fwt = + RFAL_MIN((gRFAL.TxRx.ctx.fwt + FxTAdj), RFAL_ST25R3916_NRT_MAX_1FC); + + /* Set FWT in the NRT */ + st25r3916SetNoResponseTime(rfalConv1fcTo64fc(gRFAL.TxRx.ctx.fwt)); + } else { + /* Disable NRT, no NRE will be triggered, therefore wait endlessly for Rx + */ + st25r3916SetNoResponseTime(RFAL_ST25R3916_NRT_DISABLED); + } + + gRFAL.state = RFAL_STATE_TXRX; + gRFAL.TxRx.state = RFAL_TXRX_STATE_TX_IDLE; + gRFAL.TxRx.status = RFAL_ERR_BUSY; + +#if RFAL_FEATURE_NFCV + /*******************************************************************************/ + if ((RFAL_MODE_POLL_NFCV == gRFAL.mode) || + (RFAL_MODE_POLL_PICOPASS == + gRFAL.mode)) { /* Exchange receive buffer with internal buffer */ + gRFAL.nfcvData.origCtx = gRFAL.TxRx.ctx; + + gRFAL.TxRx.ctx.rxBuf = + ((gRFAL.nfcvData.origCtx.rxBuf != NULL) ? gRFAL.nfcvData.codingBuffer + : NULL); + gRFAL.TxRx.ctx.rxBufLen = + (uint16_t)rfalConvBytesToBits(sizeof(gRFAL.nfcvData.codingBuffer)); + gRFAL.TxRx.ctx.flags = (uint32_t)RFAL_TXRX_FLAGS_CRC_TX_MANUAL | + (uint32_t)RFAL_TXRX_FLAGS_CRC_RX_KEEP | + (uint32_t)RFAL_TXRX_FLAGS_NFCIP1_OFF | + (uint32_t)(gRFAL.nfcvData.origCtx.flags & + (uint32_t)RFAL_TXRX_FLAGS_AGC_OFF) | + (uint32_t)RFAL_TXRX_FLAGS_PAR_RX_KEEP | + (uint32_t)RFAL_TXRX_FLAGS_PAR_TX_NONE; + + /* In NFCV a TxRx with a valid txBuf and txBufSize==0 indicates to send an + * EOF */ + /* Skip logic below that would go directly into receive */ + if (gRFAL.TxRx.ctx.txBuf != NULL) { + return RFAL_ERR_NONE; + } + } +#endif /* RFAL_FEATURE_NFCV */ + +#ifdef ST25R3916B + /* Check if ST25R3916 AWS is enabled and AP2P */ + if (st25r3916CheckReg(ST25R3916_REG_AUX_MOD, ST25R3916_REG_AUX_MOD_rgs_am, + ST25R3916_REG_AUX_MOD_rgs_am) && + rfalIsModeActiveComm(gRFAL.mode)) { + /* If ST25R3916 with AWS set again the current mode to reload AWS config + */ + rfalSetMode(gRFAL.mode, gRFAL.txBR, gRFAL.rxBR); + } +#endif /* ST25R3916B */ + + /*******************************************************************************/ + /* Check if the Transceive start performing Tx or goes directly to Rx */ + if ((gRFAL.TxRx.ctx.txBuf == NULL) || (gRFAL.TxRx.ctx.txBufLen == 0U)) { + /* Clear FIFO, Clear and Enable the Interrupts */ + rfalPrepareTransceive(); + + /* In AP2P check the field status */ + if (rfalIsModeActiveComm(gRFAL.mode)) { + /* Disable our field upon a Rx reEnable, and start PPON2 manually */ + st25r3916TxOff(); + st25r3916ExecuteCommand(ST25R3916_CMD_START_PPON2_TIMER); + } + + /* No Tx done, enable the Receiver */ + st25r3916ExecuteCommand(ST25R3916_CMD_UNMASK_RECEIVE_DATA); + + /* Start NRT manually, if FWT = 0 (wait endlessly for Rx) chip will ignore + * anyhow */ + st25r3916ExecuteCommand(ST25R3916_CMD_START_NO_RESPONSE_TIMER); + + gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_IDLE; + } + + return RFAL_ERR_NONE; + } + + return RFAL_ERR_WRONG_STATE; +} + +/*******************************************************************************/ +bool rfalIsTransceiveInTx(void) { + return ((gRFAL.TxRx.state >= RFAL_TXRX_STATE_TX_IDLE) && + (gRFAL.TxRx.state < RFAL_TXRX_STATE_RX_IDLE)); +} + +/*******************************************************************************/ +bool rfalIsTransceiveInRx(void) { + return (gRFAL.TxRx.state >= RFAL_TXRX_STATE_RX_IDLE); +} + +/*******************************************************************************/ +ReturnCode rfalTransceiveBlockingTx(uint8_t *txBuf, uint16_t txBufLen, + uint8_t *rxBuf, uint16_t rxBufLen, + uint16_t *actLen, uint32_t flags, + uint32_t fwt) { + ReturnCode ret; + rfalTransceiveContext ctx; + + rfalCreateByteFlagsTxRxContext(ctx, txBuf, txBufLen, rxBuf, rxBufLen, actLen, + flags, fwt); + RFAL_EXIT_ON_ERR(ret, rfalStartTransceive(&ctx)); + + return rfalTransceiveRunBlockingTx(); +} + +/*******************************************************************************/ +static ReturnCode rfalTransceiveRunBlockingTx(void) { + ReturnCode ret; + + do { + rfalWorker(); + ret = rfalGetTransceiveStatus(); + } while ((rfalIsTransceiveInTx()) && (ret == RFAL_ERR_BUSY)); + + if (rfalIsTransceiveInRx()) { + return RFAL_ERR_NONE; + } + + return ret; +} + +/*******************************************************************************/ +ReturnCode rfalTransceiveBlockingRx(void) { + ReturnCode ret; + + do { + rfalWorker(); + ret = rfalGetTransceiveStatus(); + } while ((rfalIsTransceiveInRx()) || (ret == RFAL_ERR_BUSY)); + + return ret; +} + +/*******************************************************************************/ +ReturnCode rfalTransceiveBlockingTxRx(uint8_t *txBuf, uint16_t txBufLen, + uint8_t *rxBuf, uint16_t rxBufLen, + uint16_t *actLen, uint32_t flags, + uint32_t fwt) { + ReturnCode ret; + + RFAL_EXIT_ON_ERR(ret, rfalTransceiveBlockingTx(txBuf, txBufLen, rxBuf, + rxBufLen, actLen, flags, fwt)); + ret = rfalTransceiveBlockingRx(); + + /* Convert received bits to bytes */ + if (actLen != NULL) { + *actLen = rfalConvBitsToBytes(*actLen); + } + + return ret; +} + +/*******************************************************************************/ +static ReturnCode rfalRunTransceiveWorker(void) { + if (gRFAL.state == RFAL_STATE_TXRX) { + /*******************************************************************************/ + /* Check Transceive Sanity Timer has expired */ + if (gRFAL.tmr.txRx != RFAL_TIMING_NONE) { + if (rfalTimerisExpired(gRFAL.tmr.txRx)) { + /* If sanity timer has expired abort ongoing transceive and signal error + */ + gRFAL.TxRx.status = RFAL_ERR_IO; + gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_FAIL; + } + } + + /*******************************************************************************/ + /* Run Tx or Rx state machines */ + if (rfalIsTransceiveInTx()) { + rfalTransceiveTx(); + return rfalGetTransceiveStatus(); + } + if (rfalIsTransceiveInRx()) { + rfalTransceiveRx(); + return rfalGetTransceiveStatus(); + } + } + return RFAL_ERR_WRONG_STATE; +} + +/*******************************************************************************/ +rfalTransceiveState rfalGetTransceiveState(void) { return gRFAL.TxRx.state; } + +/*******************************************************************************/ +ReturnCode rfalGetTransceiveStatus(void) { + return ((gRFAL.TxRx.state == RFAL_TXRX_STATE_IDLE) ? gRFAL.TxRx.status + : RFAL_ERR_BUSY); +} + +/*******************************************************************************/ +ReturnCode rfalGetTransceiveRSSI(uint16_t *rssi) { + uint16_t amRSSI; + uint16_t pmRSSI; + bool isSumMode; + + if (rssi == NULL) { + return RFAL_ERR_PARAM; + } + + st25r3916GetRSSI(&amRSSI, &pmRSSI); + + /* Check if Correlator Summation mode is being used */ + isSumMode = (st25r3916CheckReg(ST25R3916_REG_CORR_CONF1, + ST25R3916_REG_CORR_CONF1_corr_s4, + ST25R3916_REG_CORR_CONF1_corr_s4) + ? st25r3916CheckReg(ST25R3916_REG_AUX, + ST25R3916_REG_AUX_dis_corr, 0x00) + : false); + if (isSumMode) { + /*******************************************************************************/ + /* Usage of SQRT from math.h and float. Due to compiler, resources or * + * performance issues sqrt is not enabled by default. Using a less accuracy + * * accurate aproach such as: average, max value, etc */ + +#ifdef RFAL_ACCURATE_RSSI + *rssi = (uint16_t)sqrt( + ((double)amRSSI * (double)amRSSI) + + ((double)pmRSSI * + (double)pmRSSI)); /* PRQA S 5209 # MISRA 4.9 - External function + (sqrt()) requires double */ +#else + *rssi = ((amRSSI + pmRSSI) / 2U); +#endif + + } else { + /* Check which channel was used */ + *rssi = (st25r3916CheckReg(ST25R3916_REG_AUX_DISPLAY, + ST25R3916_REG_AUX_DISPLAY_a_cha, + ST25R3916_REG_AUX_DISPLAY_a_cha) + ? pmRSSI + : amRSSI); + } + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +bool rfalIsTransceiveSubcDetected(void) { return false; } + +/*******************************************************************************/ +void rfalWorker(void) { + platformProtectWorker(); /* Protect RFAL Worker/Task/Process */ + + switch (gRFAL.state) { + case RFAL_STATE_TXRX: + rfalRunTransceiveWorker(); + break; + +#if RFAL_FEATURE_LISTEN_MODE + case RFAL_STATE_LM: + rfalRunListenModeWorker(); + break; +#endif /* RFAL_FEATURE_LISTEN_MODE */ + +#if RFAL_FEATURE_WAKEUP_MODE + case RFAL_STATE_WUM: + rfalRunWakeUpModeWorker(); + break; +#endif /* RFAL_FEATURE_WAKEUP_MODE */ + + /* Nothing to be done */ + default: + /* MISRA 16.4: no empty default statement (a comment being enough) */ + break; + } + + platformUnprotectWorker(); /* Unprotect RFAL Worker/Task/Process */ +} + +/*******************************************************************************/ +static void rfalErrorHandling(void) { + uint16_t fifoBytesToRead; + + fifoBytesToRead = rfalFIFOStatusGetNumBytes(); + +#ifdef RFAL_SW_EMD + /*******************************************************************************/ + /* EMVCo */ + /*******************************************************************************/ + if (gRFAL.conf.eHandling == RFAL_ERRORHANDLING_EMD) { + bool rxHasIncParError; + + /*******************************************************************************/ + /* EMD Handling - Digital 2.1 4.1.1.1 ; EMVCo 3.0 4.9.2 ; ISO 14443-3 8.3 + */ + /* ReEnable the receiver on frames with a length < 4 bytes, upon: */ + /* - Collision or Framing error detected */ + /* - Residual bits are detected (hard framing error) */ + /* - Parity error */ + /* - CRC error */ + /*******************************************************************************/ + + /* Check if reception has incomplete bytes or parity error */ + rxHasIncParError = (rfalFIFOStatusIsIncompleteByte() + ? true + : rfalFIFOStatusIsMissingPar()); /* MISRA 13.5 */ + + /* In case there are residual bits decrement FIFO bytes */ + /* Ensure FIFO contains some byte as the FIFO might be empty upon Framing + * errors */ + if ((fifoBytesToRead > 0U) && rxHasIncParError) { + fifoBytesToRead--; + } + + if (((gRFAL.fifo.bytesTotal + fifoBytesToRead) < RFAL_EMVCO_RX_MAXLEN) && + ((gRFAL.TxRx.status == RFAL_ERR_RF_COLLISION) || + (gRFAL.TxRx.status == RFAL_ERR_FRAMING) || + (gRFAL.TxRx.status == RFAL_ERR_PAR) || + (gRFAL.TxRx.status == RFAL_ERR_CRC) || rxHasIncParError)) { + /* Ignore this reception, ReEnable receiver which also clears the FIFO */ + st25r3916ExecuteCommand(ST25R3916_CMD_UNMASK_RECEIVE_DATA); + + /* Ensure that the NRT has not expired meanwhile */ + if (st25r3916CheckReg(ST25R3916_REG_NFCIP1_BIT_RATE, + ST25R3916_REG_NFCIP1_BIT_RATE_nrt_on, 0x00)) { + if (st25r3916CheckReg(ST25R3916_REG_AUX_DISPLAY, + ST25R3916_REG_AUX_DISPLAY_rx_act, 0x00)) { + /* Abort reception */ + st25r3916ExecuteCommand(ST25R3916_CMD_MASK_RECEIVE_DATA); + gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_FAIL; + return; + } + } + + rfalFIFOStatusClear(); + gRFAL.fifo.bytesTotal = 0; + gRFAL.TxRx.status = RFAL_ERR_BUSY; + gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_WAIT_RXS; + } + return; + } +#endif + + /*******************************************************************************/ + /* ISO14443A Mode */ + /*******************************************************************************/ + if (gRFAL.mode == RFAL_MODE_POLL_NFCA) { + /*******************************************************************************/ + /* If we received a frame with a incomplete byte we`ll raise a specific + * error * ( support for T2T 4 bit ACK / NAK, MIFARE and Kovio ) */ + /*******************************************************************************/ + if ((gRFAL.TxRx.status == RFAL_ERR_PAR) || + (gRFAL.TxRx.status == RFAL_ERR_CRC)) { + if ((rfalFIFOStatusIsIncompleteByte()) && + (fifoBytesToRead == RFAL_RX_INC_BYTE_LEN)) { + st25r3916ReadFifo((uint8_t *)(gRFAL.TxRx.ctx.rxBuf), fifoBytesToRead); + if ((gRFAL.TxRx.ctx.rxRcvdLen) != NULL) { + *gRFAL.TxRx.ctx.rxRcvdLen = rfalFIFOGetNumIncompleteBits(); + } + + gRFAL.TxRx.status = RFAL_ERR_INCOMPLETE_BYTE; + gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_FAIL; + } + } + } +} + +/*******************************************************************************/ +static void rfalCleanupTransceive(void) { + /*******************************************************************************/ + /* Transceive flags */ + /*******************************************************************************/ + + /* Restore default settings for listen mode, Receiving parity + CRC bits and + * manual Tx Parity*/ + st25r3916ClrRegisterBits(ST25R3916_REG_ISO14443A_NFC, + (ST25R3916_REG_ISO14443A_NFC_no_tx_par | + ST25R3916_REG_ISO14443A_NFC_no_rx_par | + ST25R3916_REG_ISO14443A_NFC_nfc_f0)); + st25r3916ClrRegisterBits(ST25R3916_REG_AUX, ST25R3916_REG_AUX_no_crc_rx); + + /* Restore AGC enabled */ + st25r3916SetRegisterBits(ST25R3916_REG_RX_CONF2, + ST25R3916_REG_RX_CONF2_agc_en); + + /*******************************************************************************/ + + /*******************************************************************************/ + /* Transceive timers */ + /*******************************************************************************/ + rfalTimerDestroy(gRFAL.tmr.txRx); + rfalTimerDestroy(gRFAL.tmr.RXE); + rfalTimerDestroy(gRFAL.tmr.PPON2); + gRFAL.tmr.txRx = RFAL_TIMING_NONE; + gRFAL.tmr.RXE = RFAL_TIMING_NONE; + gRFAL.tmr.PPON2 = RFAL_TIMING_NONE; + /*******************************************************************************/ + + /*******************************************************************************/ + /* Execute Post Transceive Callback */ + /*******************************************************************************/ + if (gRFAL.callbacks.postTxRx != NULL) { + gRFAL.callbacks.postTxRx(); + } + /*******************************************************************************/ +} + +/*******************************************************************************/ +static void rfalPrepareTransceive(void) { + uint32_t maskInterrupts; + uint8_t reg; + + /* If we are in RW or AP2P mode */ + if (!rfalIsModePassiveListen(gRFAL.mode)) { + /* Reset receive logic with STOP command */ + st25r3916ExecuteCommand(ST25R3916_CMD_STOP); + + /* Reset Rx Gain */ + st25r3916ExecuteCommand(ST25R3916_CMD_RESET_RXGAIN); + } else { + /* In Passive Listen Mode do not use STOP as it stops FDT timer */ + st25r3916ExecuteCommand(ST25R3916_CMD_CLEAR_FIFO); + } + + /*******************************************************************************/ + /* FDT Poll */ + /*******************************************************************************/ + + if (gRFAL.timings.FDTPoll != RFAL_TIMING_NONE) { + /* In Passive communications General Purpose Timer is used to measure FDT + * Poll */ + if (rfalIsModePassiveComm(gRFAL.mode)) /* Passive Comms */ + { + /* Configure GPT to start at RX end */ + st25r3916SetStartGPTimer( + (uint16_t)rfalConv1fcTo8fc( + ((gRFAL.timings.FDTPoll < RFAL_FDT_POLL_ADJUSTMENT) + ? gRFAL.timings.FDTPoll + : (gRFAL.timings.FDTPoll - RFAL_FDT_POLL_ADJUSTMENT))), + ST25R3916_REG_TIMER_EMV_CONTROL_gptc_erx); + } + /* In Active Poller mode GT PPON1 is used to ensure FDT Poll */ + else if (gRFAL.mode == RFAL_MODE_POLL_ACTIVE_P2P) { + st25r3916WriteRegister( + ST25R3916_REG_FIELD_ON_GT, + (uint8_t)rfalConv1fcTo2018fc(gRFAL.timings.FDTPoll)); + } else { + /* MISRA 15.7 - Empty else */ + } + } + + /*******************************************************************************/ + /* Execute Pre Transceive Callback */ + /*******************************************************************************/ + if (gRFAL.callbacks.preTxRx != NULL) { + gRFAL.callbacks.preTxRx(); + } + /*******************************************************************************/ + + maskInterrupts = (ST25R3916_IRQ_MASK_FWL | ST25R3916_IRQ_MASK_TXE | + ST25R3916_IRQ_MASK_RXS | ST25R3916_IRQ_MASK_RXE | + ST25R3916_IRQ_MASK_PAR | ST25R3916_IRQ_MASK_CRC | + ST25R3916_IRQ_MASK_ERR1 | ST25R3916_IRQ_MASK_ERR2 | + ST25R3916_IRQ_MASK_NRE); + + /*******************************************************************************/ + /* Transceive flags */ + /*******************************************************************************/ + + reg = (ST25R3916_REG_ISO14443A_NFC_no_tx_par_off | + ST25R3916_REG_ISO14443A_NFC_no_rx_par_off | + ST25R3916_REG_ISO14443A_NFC_nfc_f0_off); + + /* Check if NFCIP1 mode is to be enabled */ + if ((gRFAL.TxRx.ctx.flags & (uint32_t)RFAL_TXRX_FLAGS_NFCIP1_ON) != 0U) { + reg |= ST25R3916_REG_ISO14443A_NFC_nfc_f0; + } + + /* Check if Parity check is to be skipped and to keep the parity + CRC bits in + * FIFO */ + if ((gRFAL.TxRx.ctx.flags & (uint32_t)RFAL_TXRX_FLAGS_PAR_RX_KEEP) != 0U) { + reg |= ST25R3916_REG_ISO14443A_NFC_no_rx_par; + } + + /* Check if automatic Parity bits is to be disabled */ + if ((gRFAL.TxRx.ctx.flags & (uint32_t)RFAL_TXRX_FLAGS_PAR_TX_NONE) != 0U) { + reg |= ST25R3916_REG_ISO14443A_NFC_no_tx_par; + } + + /* Apply current TxRx flags on ISO14443A and NFC 106kb/s Settings Register */ + st25r3916ChangeRegisterBits(ST25R3916_REG_ISO14443A_NFC, + (ST25R3916_REG_ISO14443A_NFC_no_tx_par | + ST25R3916_REG_ISO14443A_NFC_no_rx_par | + ST25R3916_REG_ISO14443A_NFC_nfc_f0), + reg); + + /* Check if CRC is to be checked automatically upon reception */ + if ((gRFAL.TxRx.ctx.flags & (uint32_t)RFAL_TXRX_FLAGS_CRC_RX_MANUAL) != 0U) { + st25r3916SetRegisterBits(ST25R3916_REG_AUX, ST25R3916_REG_AUX_no_crc_rx); + } else { + st25r3916ClrRegisterBits(ST25R3916_REG_AUX, ST25R3916_REG_AUX_no_crc_rx); + } + + /* Check if AGC is to be disabled */ + if ((gRFAL.TxRx.ctx.flags & (uint32_t)RFAL_TXRX_FLAGS_AGC_OFF) != 0U) { + st25r3916ClrRegisterBits(ST25R3916_REG_RX_CONF2, + ST25R3916_REG_RX_CONF2_agc_en); + } else { + st25r3916SetRegisterBits(ST25R3916_REG_RX_CONF2, + ST25R3916_REG_RX_CONF2_agc_en); + } + /*******************************************************************************/ + + /*******************************************************************************/ + /* EMVCo NRT mode */ + /*******************************************************************************/ + if (gRFAL.conf.eHandling == RFAL_ERRORHANDLING_EMD) { + st25r3916SetRegisterBits(ST25R3916_REG_TIMER_EMV_CONTROL, + ST25R3916_REG_TIMER_EMV_CONTROL_nrt_emv); + maskInterrupts |= ST25R3916_IRQ_MASK_RX_REST; + } else { + st25r3916ClrRegisterBits(ST25R3916_REG_TIMER_EMV_CONTROL, + ST25R3916_REG_TIMER_EMV_CONTROL_nrt_emv); + } + /*******************************************************************************/ + + /* In Passive Listen mode additionally enable External Field interrupts */ + if (rfalIsModePassiveListen(gRFAL.mode)) { + maskInterrupts |= + (ST25R3916_IRQ_MASK_EOF | + ST25R3916_IRQ_MASK_WU_F); /* Enable external Field interrupts to detect + Link Loss and SENF_REQ auto responses */ + } + + /* In Active comms enable also External Field interrupts and set RF Collsion + * Avoidance */ + if (rfalIsModeActiveComm(gRFAL.mode)) { + maskInterrupts |= (ST25R3916_IRQ_MASK_EOF | ST25R3916_IRQ_MASK_EON | + ST25R3916_IRQ_MASK_PPON2 | ST25R3916_IRQ_MASK_CAT | + ST25R3916_IRQ_MASK_CAC); + + /* Set n=0 for subsequent RF Collision Avoidance */ + st25r3916ChangeRegisterBits(ST25R3916_REG_AUX, ST25R3916_REG_AUX_nfc_n_mask, + 0); + } + + /*******************************************************************************/ + /* Start transceive Sanity Timer if a FWT is used */ + if ((gRFAL.TxRx.ctx.fwt != RFAL_FWT_NONE) && (gRFAL.TxRx.ctx.fwt != 0U)) { + rfalTimerStart(gRFAL.tmr.txRx, rfalCalcSanityTmr(gRFAL.TxRx.ctx.fwt)); + } + /*******************************************************************************/ + + /*******************************************************************************/ + /* Clear and enable these interrupts */ + st25r3916GetInterrupt(maskInterrupts); + st25r3916EnableInterrupts(maskInterrupts); + + /* Clear FIFO status local copy */ + rfalFIFOStatusClear(); +} + +/*******************************************************************************/ +static void rfalTransceiveTx(void) { + volatile uint32_t irqs; + uint16_t tmp; + ReturnCode ret; + + /* Suppress warning in case NFC-V feature is disabled */ + ret = RFAL_ERR_NONE; + RFAL_NO_WARNING(ret); + + irqs = ST25R3916_IRQ_MASK_NONE; + + if (gRFAL.TxRx.state != gRFAL.TxRx.lastState) { +#if 0 /* Debug purposes */ + rfalLogD( "RFAL: lastSt: %d curSt: %d \r\n", gRFAL.TxRx.lastState, gRFAL.TxRx.state ); +#endif + + gRFAL.TxRx.lastState = gRFAL.TxRx.state; + } + + switch (gRFAL.TxRx.state) { + /*******************************************************************************/ + case RFAL_TXRX_STATE_TX_IDLE: + + /* Nothing to do */ + + gRFAL.TxRx.state = RFAL_TXRX_STATE_TX_WAIT_GT; + /* fall through */ + + /*******************************************************************************/ + case RFAL_TXRX_STATE_TX_WAIT_GT: /* PRQA S 2003 # MISRA 16.3 - Intentional + fall through */ + + if (!rfalIsGTExpired()) { + break; + } + + rfalTimerDestroy(gRFAL.tmr.GT); + gRFAL.tmr.GT = RFAL_TIMING_NONE; + + gRFAL.TxRx.state = RFAL_TXRX_STATE_TX_WAIT_FDT; + /* fall through */ + + /*******************************************************************************/ + case RFAL_TXRX_STATE_TX_WAIT_FDT: /* PRQA S 2003 # MISRA 16.3 - Intentional + fall through */ + + /* Only in Passive communications GPT is used to measure FDT Poll */ + if (rfalIsModePassiveComm(gRFAL.mode)) { + if (st25r3916IsGPTRunning()) { + break; + } + } + + gRFAL.TxRx.state = RFAL_TXRX_STATE_TX_PREP_TX; + /* fall through */ + + /*******************************************************************************/ + case RFAL_TXRX_STATE_TX_PREP_TX: /* PRQA S 2003 # MISRA 16.3 - Intentional + fall through */ + + /* Clear FIFO, Clear and Enable the Interrupts */ + rfalPrepareTransceive(); + + /* ST25R3916 has a fixed FIFO water level */ + gRFAL.fifo.expWL = RFAL_FIFO_OUT_WL; + +#if RFAL_FEATURE_NFCV + /*******************************************************************************/ + /* In NFC-V streaming mode, the FIFO needs to be loaded with the coded + * bits */ + if ((RFAL_MODE_POLL_NFCV == gRFAL.mode) || + (RFAL_MODE_POLL_PICOPASS == gRFAL.mode)) { +#if 0 + /* Debugging code: output the payload bits by writing into the FIFO and subsequent clearing */ + st25r3916WriteFifo(gRFAL.TxRx.ctx.txBuf, rfalConvBitsToBytes(gRFAL.TxRx.ctx.txBufLen)); + st25r3916ExecuteCommand( ST25R3916_CMD_CLEAR_FIFO ); +#endif + /* Calculate the bytes needed to be Written into FIFO (a incomplete byte + * will be added as 1byte) */ + gRFAL.nfcvData.nfcvOffset = 0; + ret = rfalIso15693VCDCode( + gRFAL.TxRx.ctx.txBuf, rfalConvBitsToBytes(gRFAL.TxRx.ctx.txBufLen), + (((gRFAL.nfcvData.origCtx.flags & + (uint32_t)RFAL_TXRX_FLAGS_CRC_TX_MANUAL) != 0U) + ? false + : true), + (((gRFAL.nfcvData.origCtx.flags & + (uint32_t)RFAL_TXRX_FLAGS_NFCV_FLAG_MANUAL) != 0U) + ? false + : true), + (RFAL_MODE_POLL_PICOPASS == gRFAL.mode), &gRFAL.fifo.bytesTotal, + &gRFAL.nfcvData.nfcvOffset, gRFAL.nfcvData.codingBuffer, + RFAL_MIN((uint16_t)ST25R3916_FIFO_DEPTH, + (uint16_t)sizeof(gRFAL.nfcvData.codingBuffer)), + &gRFAL.fifo.bytesWritten); + + if ((ret != RFAL_ERR_NONE) && (ret != RFAL_ERR_AGAIN)) { + gRFAL.TxRx.status = ret; + gRFAL.TxRx.state = RFAL_TXRX_STATE_TX_FAIL; + break; + } + /* Set the number of full bytes and bits to be transmitted */ + st25r3916SetNumTxBits( + (uint16_t)rfalConvBytesToBits(gRFAL.fifo.bytesTotal)); + + /* Load FIFO with coded bytes */ + st25r3916WriteFifo(gRFAL.nfcvData.codingBuffer, + gRFAL.fifo.bytesWritten); + + } + /*******************************************************************************/ + else +#endif /* RFAL_FEATURE_NFCV */ + { + /* Calculate the bytes needed to be Written into FIFO (a incomplete byte + * will be added as 1byte) */ + gRFAL.fifo.bytesTotal = + (uint16_t)rfalCalcNumBytes(gRFAL.TxRx.ctx.txBufLen); + + /* Set the number of full bytes and bits to be transmitted */ + st25r3916SetNumTxBits(gRFAL.TxRx.ctx.txBufLen); + + /* Load FIFO with total length or FIFO's maximum */ + gRFAL.fifo.bytesWritten = + RFAL_MIN(gRFAL.fifo.bytesTotal, ST25R3916_FIFO_DEPTH); + st25r3916WriteFifo(gRFAL.TxRx.ctx.txBuf, gRFAL.fifo.bytesWritten); + } + + /*Check if Observation Mode is enabled and set it on ST25R391x */ + rfalCheckEnableObsModeTx(); + + /*******************************************************************************/ + /* If we're in Passive Listen mode ensure that the external field is still + * On */ + if (rfalIsModePassiveListen(gRFAL.mode)) { + if (!rfalIsExtFieldOn()) { + gRFAL.TxRx.status = RFAL_ERR_LINK_LOSS; + gRFAL.TxRx.state = RFAL_TXRX_STATE_TX_FAIL; + break; + } + } + + gRFAL.TxRx.state = RFAL_TXRX_STATE_TX_TRANSMIT; + /* fall through */ + + /*******************************************************************************/ + case RFAL_TXRX_STATE_TX_TRANSMIT: /* PRQA S 2003 # MISRA 16.3 - Intentional + fall through */ + + /*******************************************************************************/ + /* Execute Sync Transceive Callback */ + /*******************************************************************************/ + if (gRFAL.callbacks.syncTxRx != NULL) { + /* If set, wait for sync callback to signal sync/trigger transmission */ + if (!gRFAL.callbacks.syncTxRx()) { + break; + } + } + + /*******************************************************************************/ + /* Trigger/Start transmission */ + if ((gRFAL.TxRx.ctx.flags & (uint32_t)RFAL_TXRX_FLAGS_CRC_TX_MANUAL) != + 0U) { + st25r3916ExecuteCommand(ST25R3916_CMD_TRANSMIT_WITHOUT_CRC); + } else { + st25r3916ExecuteCommand(ST25R3916_CMD_TRANSMIT_WITH_CRC); + } + + /* Check if a WL level is expected or TXE should come */ + gRFAL.TxRx.state = ((gRFAL.fifo.bytesWritten < gRFAL.fifo.bytesTotal) + ? RFAL_TXRX_STATE_TX_WAIT_WL + : RFAL_TXRX_STATE_TX_WAIT_TXE); + break; + + /*******************************************************************************/ + case RFAL_TXRX_STATE_TX_WAIT_WL: + + irqs = st25r3916GetInterrupt( + (ST25R3916_IRQ_MASK_FWL | ST25R3916_IRQ_MASK_TXE)); + if (irqs == ST25R3916_IRQ_MASK_NONE) { + break; /* No interrupt to process */ + } + + if (((irqs & ST25R3916_IRQ_MASK_FWL) != 0U) && + ((irqs & ST25R3916_IRQ_MASK_TXE) == 0U)) { + gRFAL.TxRx.state = RFAL_TXRX_STATE_TX_RELOAD_FIFO; + } else { + gRFAL.TxRx.status = RFAL_ERR_IO; + gRFAL.TxRx.state = RFAL_TXRX_STATE_TX_FAIL; + break; + } + + /* fall through */ + + /*******************************************************************************/ + case RFAL_TXRX_STATE_TX_RELOAD_FIFO: /* PRQA S 2003 # MISRA 16.3 - + Intentional fall through */ + +#if RFAL_FEATURE_NFCV + /*******************************************************************************/ + /* In NFC-V streaming mode, the FIFO needs to be loaded with the coded + * bits */ + if ((RFAL_MODE_POLL_NFCV == gRFAL.mode) || + (RFAL_MODE_POLL_PICOPASS == gRFAL.mode)) { + uint16_t maxLen; + + /* Load FIFO with the remaining length or maximum available (which fit + * on the coding buffer) */ + maxLen = (uint16_t)RFAL_MIN( + (gRFAL.fifo.bytesTotal - gRFAL.fifo.bytesWritten), + gRFAL.fifo.expWL); + maxLen = + (uint16_t)RFAL_MIN(maxLen, sizeof(gRFAL.nfcvData.codingBuffer)); + tmp = 0; + + /* Calculate the bytes needed to be Written into FIFO (a incomplete byte + * will be added as 1byte) */ + ret = rfalIso15693VCDCode( + gRFAL.TxRx.ctx.txBuf, rfalConvBitsToBytes(gRFAL.TxRx.ctx.txBufLen), + (((gRFAL.nfcvData.origCtx.flags & + (uint32_t)RFAL_TXRX_FLAGS_CRC_TX_MANUAL) != 0U) + ? false + : true), + (((gRFAL.nfcvData.origCtx.flags & + (uint32_t)RFAL_TXRX_FLAGS_NFCV_FLAG_MANUAL) != 0U) + ? false + : true), + (RFAL_MODE_POLL_PICOPASS == gRFAL.mode), &gRFAL.fifo.bytesTotal, + &gRFAL.nfcvData.nfcvOffset, gRFAL.nfcvData.codingBuffer, maxLen, + &tmp); + + if ((ret != RFAL_ERR_NONE) && (ret != RFAL_ERR_AGAIN)) { + gRFAL.TxRx.status = ret; + gRFAL.TxRx.state = RFAL_TXRX_STATE_TX_FAIL; + break; + } + + /* Load FIFO with coded bytes */ + st25r3916WriteFifo(gRFAL.nfcvData.codingBuffer, tmp); + } + /*******************************************************************************/ + else +#endif /* RFAL_FEATURE_NFCV */ + { + /* Load FIFO with the remaining length or maximum available */ + tmp = RFAL_MIN((gRFAL.fifo.bytesTotal - gRFAL.fifo.bytesWritten), + gRFAL.fifo.expWL); /* tmp holds the number of bytes + written on this iteration */ + st25r3916WriteFifo(&gRFAL.TxRx.ctx.txBuf[gRFAL.fifo.bytesWritten], tmp); + } + + /* Update total written bytes to FIFO */ + gRFAL.fifo.bytesWritten += tmp; + + /* Check if a WL level is expected or TXE should come */ + gRFAL.TxRx.state = ((gRFAL.fifo.bytesWritten < gRFAL.fifo.bytesTotal) + ? RFAL_TXRX_STATE_TX_WAIT_WL + : RFAL_TXRX_STATE_TX_WAIT_TXE); + break; + + /*******************************************************************************/ + case RFAL_TXRX_STATE_TX_WAIT_TXE: + + irqs = st25r3916GetInterrupt((ST25R3916_IRQ_MASK_FWL | + ST25R3916_IRQ_MASK_TXE | + ST25R3916_IRQ_MASK_EOF)); + if (irqs == ST25R3916_IRQ_MASK_NONE) { + break; /* No interrupt to process */ + } + + if ((irqs & ST25R3916_IRQ_MASK_TXE) != 0U) { + gRFAL.TxRx.state = RFAL_TXRX_STATE_TX_DONE; + } else if ((irqs & ST25R3916_IRQ_MASK_FWL) != 0U) { + break; /* Ignore ST25R3916 FIFO WL if total TxLen is already on the FIFO + */ + } else if (!rfalIsModeActiveComm( + gRFAL.mode)) { /* ST25R3916_IRQ_MASK_EOF will fall in here, + in rare cases this may happen when field + goes during CE transmit */ + gRFAL.TxRx.status = RFAL_ERR_IO; + gRFAL.TxRx.state = RFAL_TXRX_STATE_TX_FAIL; + break; + } else { + /* MISRA 15.7 - Empty else */ + } + + /* fall through */ + + /*******************************************************************************/ + case RFAL_TXRX_STATE_TX_DONE: /* PRQA S 2003 # MISRA 16.3 - Intentional + fall through */ + + /* If no rxBuf is provided do not wait/expect Rx */ + if (gRFAL.TxRx.ctx.rxBuf == NULL) { + /*Check if Observation Mode was enabled and disable it on ST25R391x */ + rfalCheckDisableObsMode(); + + /* Clean up Transceive */ + rfalCleanupTransceive(); + + gRFAL.TxRx.status = RFAL_ERR_NONE; + gRFAL.TxRx.state = RFAL_TXRX_STATE_IDLE; + break; + } + + rfalCheckEnableObsModeRx(); + + /* Goto Rx */ + gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_IDLE; + break; + + /*******************************************************************************/ + case RFAL_TXRX_STATE_TX_FAIL: + + /* Error should be assigned by previous state */ + if (gRFAL.TxRx.status == RFAL_ERR_BUSY) { + gRFAL.TxRx.status = RFAL_ERR_SYSTEM; + } + + /*Check if Observation Mode was enabled and disable it on ST25R391x */ + rfalCheckDisableObsMode(); + + /* Clean up Transceive */ + rfalCleanupTransceive(); + + gRFAL.TxRx.state = RFAL_TXRX_STATE_IDLE; + break; + + /*******************************************************************************/ + default: + gRFAL.TxRx.status = RFAL_ERR_SYSTEM; + gRFAL.TxRx.state = RFAL_TXRX_STATE_TX_FAIL; + break; + } +} + +/*******************************************************************************/ +static void rfalTransceiveRx(void) { + volatile uint32_t irqs; + uint16_t tmp; + uint16_t aux; + + irqs = ST25R3916_IRQ_MASK_NONE; + + if (gRFAL.TxRx.state != gRFAL.TxRx.lastState) { +#if 0 /* Debug purposes */ + rfalLogD( "RFAL: lastSt: %d curSt: %d \r\n", gRFAL.TxRx.lastState, gRFAL.TxRx.state ); +#endif + + gRFAL.TxRx.lastState = gRFAL.TxRx.state; + } + + switch (gRFAL.TxRx.state) { + /*******************************************************************************/ + case RFAL_TXRX_STATE_RX_IDLE: + + /* Clear rx counters */ + gRFAL.fifo.bytesWritten = 0; /* Total bytes written on RxBuffer */ + gRFAL.fifo.bytesTotal = 0; /* Total bytes in FIFO will now be from Rx */ + if (gRFAL.TxRx.ctx.rxRcvdLen != NULL) { + *gRFAL.TxRx.ctx.rxRcvdLen = 0; + } + + /*******************************************************************************/ + /* REMARK: Silicon workaround ST25R3916 Errata #2.1.3 */ + rfalTimerStart(gRFAL.tmr.PPON2, 10U); + /*******************************************************************************/ + + gRFAL.TxRx.state = + (rfalIsModeActiveComm(gRFAL.mode) ? RFAL_TXRX_STATE_RX_WAIT_EON + : RFAL_TXRX_STATE_RX_WAIT_RXS); + break; + + /*******************************************************************************/ + case RFAL_TXRX_STATE_RX_WAIT_RXS: + + /*******************************************************************************/ + irqs = st25r3916GetInterrupt((ST25R3916_IRQ_MASK_RXS | + ST25R3916_IRQ_MASK_NRE | + ST25R3916_IRQ_MASK_EOF)); + if (irqs == ST25R3916_IRQ_MASK_NONE) { + break; /* No interrupt to process */ + } + + /* Only raise Timeout if NRE is detected with no Rx Start (NRT EMV mode) + */ + if (((irqs & ST25R3916_IRQ_MASK_NRE) != 0U) && + ((irqs & ST25R3916_IRQ_MASK_RXS) == 0U)) { + gRFAL.TxRx.status = RFAL_ERR_TIMEOUT; + gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_FAIL; + break; + } + + /* Only raise Link Loss if EOF is detected with no Rx Start */ + if (((irqs & ST25R3916_IRQ_MASK_EOF) != 0U) && + ((irqs & ST25R3916_IRQ_MASK_RXS) == 0U)) { + /* In AP2P a Field On has already occurred - treat this as timeout | + * mute */ + gRFAL.TxRx.status = + (rfalIsModeActiveComm(gRFAL.mode) ? RFAL_ERR_TIMEOUT + : RFAL_ERR_LINK_LOSS); + gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_FAIL; + break; + } + + if ((irqs & ST25R3916_IRQ_MASK_RXS) != 0U) { + /*******************************************************************************/ + /* REMARK: Silicon workaround ST25R3916 Errata #2.1.2 */ + /* Rarely on corrupted frames I_rxs gets signaled but I_rxe is not + * signaled */ + /* Use a SW timer to handle an eventual missing RXE */ + rfalTimerStart(gRFAL.tmr.RXE, RFAL_NORXE_TOUT); + /*******************************************************************************/ + + gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_WAIT_RXE; + } else { + gRFAL.TxRx.status = RFAL_ERR_IO; + gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_FAIL; + break; + } + + /* remove NRE that might appear together (NRT EMV mode), and remove RXS, + * but keep EOF if present for next state */ + irqs &= ~(ST25R3916_IRQ_MASK_RXS | ST25R3916_IRQ_MASK_NRE); + + /* fall through */ + + /*******************************************************************************/ + case RFAL_TXRX_STATE_RX_WAIT_RXE: /* PRQA S 2003 # MISRA 16.3 - Intentional + fall through */ + + irqs |= st25r3916GetInterrupt( + (ST25R3916_IRQ_MASK_RXE | ST25R3916_IRQ_MASK_FWL | + ST25R3916_IRQ_MASK_EOF | ST25R3916_IRQ_MASK_RX_REST | + ST25R3916_IRQ_MASK_WU_F)); + if (irqs == ST25R3916_IRQ_MASK_NONE) { + /*******************************************************************************/ + /* REMARK: Silicon workaround ST25R3916 Errata #2.1.2 */ + /* ST25R396 may indicate RXS without RXE afterwards, this happens rarely + * on */ + /* corrupted frames. */ + /* SW timer is used to timeout upon a missing RXE */ + if (rfalTimerisExpired(gRFAL.tmr.RXE)) { + gRFAL.TxRx.status = RFAL_ERR_FRAMING; + gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_FAIL; + } + /*******************************************************************************/ + + break; /* No interrupt to process */ + } + + if ((irqs & ST25R3916_IRQ_MASK_RX_REST) != 0U) { + /* RX_REST indicates that Receiver has been reseted due to EMD, + * therefore a RXS + RXE should * follow if a good reception is followed + * within the valid initial timeout */ + + /* Check whether NRT has expired already, if so signal a timeout */ + if (st25r3916GetInterrupt(ST25R3916_IRQ_MASK_NRE) != 0U) { + gRFAL.TxRx.status = RFAL_ERR_TIMEOUT; + gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_FAIL; + break; + } + if (st25r3916CheckReg(ST25R3916_REG_NFCIP1_BIT_RATE, + ST25R3916_REG_NFCIP1_BIT_RATE_nrt_on, + 0)) /* MISRA 13.5 */ + { + gRFAL.TxRx.status = RFAL_ERR_TIMEOUT; + gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_FAIL; + break; + } + + /* Discard any previous RXS */ + st25r3916GetInterrupt(ST25R3916_IRQ_MASK_RXS); + + /* Check whether a following reception has already started */ + if (st25r3916CheckReg(ST25R3916_REG_AUX_DISPLAY, + ST25R3916_REG_AUX_DISPLAY_rx_act, + ST25R3916_REG_AUX_DISPLAY_rx_act)) { + gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_WAIT_RXE; + break; + } + + gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_WAIT_RXS; + break; + } + + if (((irqs & ST25R3916_IRQ_MASK_FWL) != 0U) && + ((irqs & ST25R3916_IRQ_MASK_RXE) == 0U)) { + gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_READ_FIFO; + break; + } + + /* Automatic responses allowed during TxRx only for the SENSF_REQ */ + if (((irqs & ST25R3916_IRQ_MASK_WU_F) != 0U) && + ((irqs & ST25R3916_IRQ_MASK_EOF) == 0U)) { + gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_WAIT_RXS; + break; + } + + /* After RXE retrieve and check for any error irqs */ + irqs |= st25r3916GetInterrupt( + (ST25R3916_IRQ_MASK_CRC | ST25R3916_IRQ_MASK_PAR | + ST25R3916_IRQ_MASK_ERR1 | ST25R3916_IRQ_MASK_ERR2 | + ST25R3916_IRQ_MASK_COL)); + + gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_ERR_CHECK; + /* fall through */ + + /*******************************************************************************/ + case RFAL_TXRX_STATE_RX_ERR_CHECK: /* PRQA S 2003 # MISRA 16.3 - + Intentional fall through */ + + if ((irqs & ST25R3916_IRQ_MASK_ERR1) != 0U) { + gRFAL.TxRx.status = RFAL_ERR_FRAMING; + gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_READ_DATA; + + /* Check if there's a specific error handling for this */ + rfalErrorHandling(); + break; + } + /* Discard Soft Framing errors in AP2P and CE */ + /* Discard Soft Framing errors in CTS as Correlator does not support + no_eof */ + else if ((rfalIsModePassivePoll(gRFAL.mode)) && + ((irqs & ST25R3916_IRQ_MASK_ERR2) != 0U) && + (gRFAL.mode != RFAL_MODE_POLL_B_CTS)) { + gRFAL.TxRx.status = RFAL_ERR_FRAMING; + gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_READ_DATA; + + /* Check if there's a specific error handling for this */ + rfalErrorHandling(); + break; + } else if ((irqs & ST25R3916_IRQ_MASK_PAR) != 0U) { + gRFAL.TxRx.status = RFAL_ERR_PAR; + gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_READ_DATA; + + /* Check if there's a specific error handling for this */ + rfalErrorHandling(); + break; + } else if ((irqs & ST25R3916_IRQ_MASK_CRC) != 0U) { + gRFAL.TxRx.status = RFAL_ERR_CRC; + gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_READ_DATA; + + /* Check if there's a specific error handling for this */ + rfalErrorHandling(); + break; + } else if ((irqs & ST25R3916_IRQ_MASK_COL) != 0U) { + gRFAL.TxRx.status = RFAL_ERR_RF_COLLISION; + gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_READ_DATA; + + /* Check if there's a specific error handling for this */ + rfalErrorHandling(); + break; + } else if (rfalIsModePassiveListen(gRFAL.mode) && + ((irqs & ST25R3916_IRQ_MASK_EOF) != 0U)) { + gRFAL.TxRx.status = RFAL_ERR_LINK_LOSS; + gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_FAIL; + break; + } else if ((irqs & ST25R3916_IRQ_MASK_RXE) != 0U) { + /* Reception ended without any error indication, * + * check FIFO status for malformed or incomplete frames */ + + /* Check if the reception ends with an incomplete byte (residual bits) + */ + if (rfalFIFOStatusIsIncompleteByte()) { + gRFAL.TxRx.status = RFAL_ERR_INCOMPLETE_BYTE; + } + /* Check if the reception ends missing parity bit */ + else if (rfalFIFOStatusIsMissingPar()) { + gRFAL.TxRx.status = RFAL_ERR_FRAMING; + } else { + /* MISRA 15.7 - Empty else */ + } + + gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_READ_DATA; + } else { + gRFAL.TxRx.status = RFAL_ERR_IO; + gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_FAIL; + break; + } + + /* fall through */ + + /*******************************************************************************/ + case RFAL_TXRX_STATE_RX_READ_DATA: /* PRQA S 2003 # MISRA 16.3 - + Intentional fall through */ + + tmp = rfalFIFOStatusGetNumBytes(); + + /*******************************************************************************/ + /* Check if CRC should not be placed in rxBuf */ + if (((gRFAL.TxRx.ctx.flags & (uint32_t)RFAL_TXRX_FLAGS_CRC_RX_KEEP) == + 0U)) { + /* if received frame was bigger than CRC */ + if ((uint16_t)(gRFAL.fifo.bytesTotal + tmp) > 0U) { + /* By default CRC will not be placed into the rxBuffer */ + if ((tmp > RFAL_CRC_LEN)) { + tmp -= RFAL_CRC_LEN; + } + /* If the CRC was already placed into rxBuffer (due to WL interrupt + * where CRC was already in FIFO Read) cannot remove it from rxBuf. + * Can only remove it from rxBufLen not indicate the presence of CRC + */ + else if (gRFAL.fifo.bytesTotal > RFAL_CRC_LEN) { + gRFAL.fifo.bytesTotal -= RFAL_CRC_LEN; + } else { + /* MISRA 15.7 - Empty else */ + } + } + } + + gRFAL.fifo.bytesTotal += tmp; /* add to total bytes counter */ + + /*******************************************************************************/ + /* Check if remaining bytes fit on the rxBuf available */ + if (gRFAL.fifo.bytesTotal > + rfalConvBitsToBytes(gRFAL.TxRx.ctx.rxBufLen)) { + tmp = (uint16_t)(rfalConvBitsToBytes(gRFAL.TxRx.ctx.rxBufLen) - + gRFAL.fifo.bytesWritten); + + /* Transmission errors have precedence over buffer error */ + if (gRFAL.TxRx.status == RFAL_ERR_BUSY) { + gRFAL.TxRx.status = RFAL_ERR_NOMEM; + } + } + + /*******************************************************************************/ + /* Retrieve remaining bytes from FIFO to rxBuf, and assign total length + * rcvd */ + st25r3916ReadFifo(&gRFAL.TxRx.ctx.rxBuf[gRFAL.fifo.bytesWritten], tmp); + if (gRFAL.TxRx.ctx.rxRcvdLen != NULL) { + (*gRFAL.TxRx.ctx.rxRcvdLen) = + (uint16_t)rfalConvBytesToBits(gRFAL.fifo.bytesTotal); + if (rfalFIFOStatusIsIncompleteByte()) { + (*gRFAL.TxRx.ctx.rxRcvdLen) -= + (RFAL_BITS_IN_BYTE - rfalFIFOGetNumIncompleteBits()); + } + } + +#if RFAL_FEATURE_NFCV + /*******************************************************************************/ + /* Decode sub bit stream into payload bits for NFCV, if no error found so + * far */ + if (((RFAL_MODE_POLL_NFCV == gRFAL.mode) || + (RFAL_MODE_POLL_PICOPASS == gRFAL.mode)) && + (gRFAL.TxRx.status == RFAL_ERR_BUSY)) { + ReturnCode ret; + uint16_t offset = 0; /* REMARK offset not currently used */ + + ret = rfalIso15693VICCDecode( + gRFAL.TxRx.ctx.rxBuf, gRFAL.fifo.bytesTotal, + gRFAL.nfcvData.origCtx.rxBuf, + rfalConvBitsToBytes(gRFAL.nfcvData.origCtx.rxBufLen), &offset, + gRFAL.nfcvData.origCtx.rxRcvdLen, gRFAL.nfcvData.ignoreBits, + (RFAL_MODE_POLL_PICOPASS == gRFAL.mode)); + + if (((RFAL_ERR_NONE == ret) || (RFAL_ERR_CRC == ret)) && + (((uint32_t)RFAL_TXRX_FLAGS_CRC_RX_KEEP & + gRFAL.nfcvData.origCtx.flags) == 0U) && + ((*gRFAL.nfcvData.origCtx.rxRcvdLen % RFAL_BITS_IN_BYTE) == 0U) && + (*gRFAL.nfcvData.origCtx.rxRcvdLen >= + rfalConvBytesToBits(RFAL_CRC_LEN))) { + *gRFAL.nfcvData.origCtx.rxRcvdLen -= + (uint16_t)rfalConvBytesToBits(RFAL_CRC_LEN); /* Remove CRC */ + } +#if 0 + /* Debugging code: output the payload bits by writing into the FIFO and subsequent clearing */ + st25r3916WriteFifo(gRFAL.nfcvData.origCtx.rxBuf, rfalConvBitsToBytes( *gRFAL.nfcvData.origCtx.rxRcvdLen)); + st25r3916ExecuteCommand( ST25R3916_CMD_CLEAR_FIFO ); +#endif + + /* Restore original ctx */ + gRFAL.TxRx.ctx = gRFAL.nfcvData.origCtx; + gRFAL.TxRx.status = ((ret != RFAL_ERR_NONE) ? ret : RFAL_ERR_BUSY); + } +#endif /* RFAL_FEATURE_NFCV */ + + if (rfalIsModeActiveComm(gRFAL.mode)) { + gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_WAIT_EOF; + break; + } + + /*******************************************************************************/ + /* If an error as been marked/detected don't fall into to RX_DONE */ + if (gRFAL.TxRx.status != RFAL_ERR_BUSY) { + gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_FAIL; + break; + } + + gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_DONE; + /* fall through */ + + /*******************************************************************************/ + case RFAL_TXRX_STATE_RX_DONE: /* PRQA S 2003 # MISRA 16.3 - Intentional + fall through */ + + /*Check if Observation Mode was enabled and disable it on ST25R391x */ + rfalCheckDisableObsMode(); + + /* Clean up Transceive */ + rfalCleanupTransceive(); + + gRFAL.TxRx.status = RFAL_ERR_NONE; + gRFAL.TxRx.state = RFAL_TXRX_STATE_IDLE; + break; + + /*******************************************************************************/ + case RFAL_TXRX_STATE_RX_READ_FIFO: + + /*******************************************************************************/ + /* REMARK: Silicon workaround ST25R3916 Errata #2.1.2 */ + /* Rarely on corrupted frames I_rxs gets signaled but I_rxe is not + * signaled */ + /* Use a SW timer to handle an eventual missing RXE */ + rfalTimerStart(gRFAL.tmr.RXE, RFAL_NORXE_TOUT); + /*******************************************************************************/ + + tmp = rfalFIFOStatusGetNumBytes(); + gRFAL.fifo.bytesTotal += tmp; + + /*******************************************************************************/ + /* Calculate the amount of bytes that still fits in rxBuf */ + aux = ((gRFAL.fifo.bytesTotal > + rfalConvBitsToBytes(gRFAL.TxRx.ctx.rxBufLen)) + ? (rfalConvBitsToBytes(gRFAL.TxRx.ctx.rxBufLen) - + gRFAL.fifo.bytesWritten) + : tmp); + + /*******************************************************************************/ + /* Retrieve incoming bytes from FIFO to rxBuf, and store already read + * amount */ + st25r3916ReadFifo(&gRFAL.TxRx.ctx.rxBuf[gRFAL.fifo.bytesWritten], aux); + gRFAL.fifo.bytesWritten += aux; + + /*******************************************************************************/ + /* If the bytes already read were not the full FIFO WL, dump the remaining + * * FIFO so that ST25R391x can continue with reception */ + if (aux < tmp) { + st25r3916ReadFifo(NULL, (tmp - aux)); + } + + rfalFIFOStatusClear(); + gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_WAIT_RXE; + break; + + /*******************************************************************************/ + case RFAL_TXRX_STATE_RX_FAIL: + + /*Check if Observation Mode was enabled and disable it on ST25R391x */ + rfalCheckDisableObsMode(); + + /* Clean up Transceive */ + rfalCleanupTransceive(); + + /* Error should be assigned by previous state */ + if (gRFAL.TxRx.status == RFAL_ERR_BUSY) { + gRFAL.TxRx.status = RFAL_ERR_SYSTEM; + } + +#if 0 /* Debug purposes */ + rfalLogD( "RFAL: curSt: %d Error: %d \r\n", gRFAL.TxRx.state, gRFAL.TxRx.status ); +#endif + + gRFAL.TxRx.state = RFAL_TXRX_STATE_IDLE; + break; + + /*******************************************************************************/ + case RFAL_TXRX_STATE_RX_WAIT_EON: + + irqs = st25r3916GetInterrupt((ST25R3916_IRQ_MASK_EON | + ST25R3916_IRQ_MASK_NRE | + ST25R3916_IRQ_MASK_PPON2)); + if (irqs == ST25R3916_IRQ_MASK_NONE) { + /*******************************************************************************/ + /* REMARK: Silicon workaround ST25R3916 Errata #2.1.3 */ + if (rfalTimerisExpired(gRFAL.tmr.PPON2)) { + gRFAL.TxRx.status = RFAL_ERR_LINK_LOSS; + gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_FAIL; + } + /*******************************************************************************/ + + break; /* No interrupt to process */ + } + + if ((irqs & ST25R3916_IRQ_MASK_EON) != 0U) { + gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_WAIT_RXS; + +#ifdef ST25R3916B + /* Check if ST25R3916 AWS is enabled */ + if (st25r3916CheckReg(ST25R3916_REG_AUX_MOD, + ST25R3916_REG_AUX_MOD_rgs_am, + ST25R3916_REG_AUX_MOD_rgs_am)) { + /* Set Analog configurations for our own following Field On */ + rfalSetAnalogConfig((RFAL_ANALOG_CONFIG_TECH_CHIP | + RFAL_ANALOG_CONFIG_CHIP_FIELD_ON)); + } +#endif /* ST25R3916B */ + } + + if ((irqs & ST25R3916_IRQ_MASK_NRE) != 0U) { + gRFAL.TxRx.status = RFAL_ERR_TIMEOUT; + gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_FAIL; + } + if ((irqs & ST25R3916_IRQ_MASK_PPON2) != 0U) { + gRFAL.TxRx.status = RFAL_ERR_LINK_LOSS; + gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_FAIL; + } + break; + + /*******************************************************************************/ + case RFAL_TXRX_STATE_RX_WAIT_EOF: + + irqs = st25r3916GetInterrupt( + (ST25R3916_IRQ_MASK_CAT | ST25R3916_IRQ_MASK_CAC)); + if (irqs == ST25R3916_IRQ_MASK_NONE) { + break; /* No interrupt to process */ + } + + if ((irqs & ST25R3916_IRQ_MASK_CAT) != 0U) { + /* Check if an error has been marked/detected before */ + gRFAL.TxRx.state = + ((gRFAL.TxRx.status != RFAL_ERR_BUSY) ? RFAL_TXRX_STATE_RX_FAIL + : RFAL_TXRX_STATE_RX_DONE); + } else if ((irqs & ST25R3916_IRQ_MASK_CAC) != 0U) { + gRFAL.TxRx.status = RFAL_ERR_RF_COLLISION; + gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_FAIL; + } else { + gRFAL.TxRx.status = RFAL_ERR_IO; + gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_FAIL; + } + break; + + /*******************************************************************************/ + default: + gRFAL.TxRx.status = RFAL_ERR_SYSTEM; + gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_FAIL; + break; + } +} + +/*******************************************************************************/ +static void rfalFIFOStatusUpdate(void) { + if (gRFAL.fifo.status[RFAL_FIFO_STATUS_REG2] == RFAL_FIFO_STATUS_INVALID) { + st25r3916ReadMultipleRegisters(ST25R3916_REG_FIFO_STATUS1, + gRFAL.fifo.status, + ST25R3916_FIFO_STATUS_LEN); + } +} + +/*******************************************************************************/ +static void rfalFIFOStatusClear(void) { + gRFAL.fifo.status[RFAL_FIFO_STATUS_REG2] = RFAL_FIFO_STATUS_INVALID; +} + +/*******************************************************************************/ +static uint16_t rfalFIFOStatusGetNumBytes(void) { + uint16_t result; + + rfalFIFOStatusUpdate(); + + result = ((((uint16_t)gRFAL.fifo.status[RFAL_FIFO_STATUS_REG2] & + ST25R3916_REG_FIFO_STATUS2_fifo_b_mask) >> + ST25R3916_REG_FIFO_STATUS2_fifo_b_shift) + << RFAL_BITS_IN_BYTE); + result |= (((uint16_t)gRFAL.fifo.status[RFAL_FIFO_STATUS_REG1]) & 0x00FFU); + return result; +} + +/*******************************************************************************/ +static bool rfalFIFOStatusIsIncompleteByte(void) { + rfalFIFOStatusUpdate(); + return ((gRFAL.fifo.status[RFAL_FIFO_STATUS_REG2] & + ST25R3916_REG_FIFO_STATUS2_fifo_lb_mask) != 0U); +} + +/*******************************************************************************/ +static bool rfalFIFOStatusIsMissingPar(void) { + rfalFIFOStatusUpdate(); + return ((gRFAL.fifo.status[RFAL_FIFO_STATUS_REG2] & + ST25R3916_REG_FIFO_STATUS2_np_lb) != 0U); +} + +/*******************************************************************************/ +static uint8_t rfalFIFOGetNumIncompleteBits(void) { + rfalFIFOStatusUpdate(); + return ((gRFAL.fifo.status[RFAL_FIFO_STATUS_REG2] & + ST25R3916_REG_FIFO_STATUS2_fifo_lb_mask) >> + ST25R3916_REG_FIFO_STATUS2_fifo_lb_shift); +} + +#if RFAL_FEATURE_NFCA + +/*******************************************************************************/ +ReturnCode rfalISO14443ATransceiveShortFrame(rfal14443AShortFrameCmd txCmd, + uint8_t *rxBuf, uint8_t rxBufLen, + uint16_t *rxRcvdLen, + uint32_t fwt) { + ReturnCode ret; + uint8_t directCmd; + + /* Check if RFAL is properly initialized */ + if ((!st25r3916IsTxEnabled()) || (gRFAL.state < RFAL_STATE_MODE_SET) || + ((gRFAL.mode != RFAL_MODE_POLL_NFCA) && + (gRFAL.mode != RFAL_MODE_POLL_NFCA_T1T))) { + return RFAL_ERR_WRONG_STATE; + } + + /* Check for valid parameters */ + if ((rxBuf == NULL) || (rxRcvdLen == NULL) || (fwt == RFAL_FWT_NONE)) { + return RFAL_ERR_PARAM; + } + + /*******************************************************************************/ + /* Select the Direct Command to be performed */ + switch (txCmd) { + case RFAL_14443A_SHORTFRAME_CMD_WUPA: + directCmd = ST25R3916_CMD_TRANSMIT_WUPA; + break; + + case RFAL_14443A_SHORTFRAME_CMD_REQA: + directCmd = ST25R3916_CMD_TRANSMIT_REQA; + break; + + default: + return RFAL_ERR_PARAM; + } + + /*******************************************************************************/ + /* Wait for GT and FDT */ + while (!rfalIsGTExpired()) { /* MISRA 15.6: mandatory brackets */ + }; + while (st25r3916IsGPTRunning()) { /* MISRA 15.6: mandatory brackets */ + }; + + rfalTimerDestroy(gRFAL.tmr.GT); + gRFAL.tmr.GT = RFAL_TIMING_NONE; + + /*******************************************************************************/ + /* Prepare for Transceive, Receive only (bypass Tx states) */ + gRFAL.TxRx.ctx.flags = ((uint32_t)RFAL_TXRX_FLAGS_CRC_TX_MANUAL | + (uint32_t)RFAL_TXRX_FLAGS_CRC_RX_KEEP | + (uint32_t)RFAL_TXRX_FLAGS_CRC_RX_MANUAL); + gRFAL.TxRx.ctx.rxBuf = rxBuf; + gRFAL.TxRx.ctx.rxBufLen = rxBufLen; + gRFAL.TxRx.ctx.rxRcvdLen = rxRcvdLen; + gRFAL.TxRx.ctx.fwt = fwt; + + /*******************************************************************************/ + /* Load NRT with FWT */ + st25r3916SetNoResponseTime(rfalConv1fcTo64fc( + RFAL_MIN((fwt + RFAL_FWT_ADJUSTMENT + RFAL_FWT_A_ADJUSTMENT), + RFAL_ST25R3916_NRT_MAX_1FC))); + + if (gRFAL.timings.FDTListen != RFAL_TIMING_NONE) { + /* Ensure that MRT is using 64/fc steps */ + st25r3916ClrRegisterBits(ST25R3916_REG_TIMER_EMV_CONTROL, + ST25R3916_REG_TIMER_EMV_CONTROL_mrt_step); + + /* Set Minimum FDT(Listen) in which PICC is not allowed to send a response + */ + st25r3916WriteRegister( + ST25R3916_REG_MASK_RX_TIMER, + (uint8_t)rfalConv1fcTo64fc( + ((RFAL_FDT_LISTEN_MRT_ADJUSTMENT + RFAL_FDT_LISTEN_A_ADJUSTMENT) > + gRFAL.timings.FDTListen) + ? RFAL_ST25R3916_MRT_MIN_1FC + : (gRFAL.timings.FDTListen - (RFAL_FDT_LISTEN_MRT_ADJUSTMENT + + RFAL_FDT_LISTEN_A_ADJUSTMENT)))); + } + + /* In Passive communications General Purpose Timer is used to measure FDT Poll + */ + if (gRFAL.timings.FDTPoll != RFAL_TIMING_NONE) { + /* Configure GPT to start at RX end */ + st25r3916SetStartGPTimer( + (uint16_t)rfalConv1fcTo8fc( + ((gRFAL.timings.FDTPoll < RFAL_FDT_POLL_ADJUSTMENT) + ? gRFAL.timings.FDTPoll + : (gRFAL.timings.FDTPoll - RFAL_FDT_POLL_ADJUSTMENT))), + ST25R3916_REG_TIMER_EMV_CONTROL_gptc_erx); + } + + /*******************************************************************************/ + rfalPrepareTransceive(); + + /* Also enable bit collision interrupt */ + st25r3916GetInterrupt(ST25R3916_IRQ_MASK_COL); + st25r3916EnableInterrupts(ST25R3916_IRQ_MASK_COL); + + /*Check if Observation Mode is enabled and set it on ST25R391x */ + rfalCheckEnableObsModeTx(); + + /*******************************************************************************/ + /* Clear nbtx bits before sending WUPA/REQA - otherwise ST25R3916 will report + * parity error, Note2 of the register */ + st25r3916WriteRegister(ST25R3916_REG_NUM_TX_BYTES2, 0); + + /* Send either WUPA or REQA. All affected tags will backscatter ATQA and + * change to READY state */ + st25r3916ExecuteCommand(directCmd); + + /* Wait for TXE */ + if (st25r3916WaitForInterruptsTimed( + ST25R3916_IRQ_MASK_TXE, + (uint16_t)RFAL_MAX(rfalConv1fcToMs(fwt), + RFAL_ST25R3916_SW_TMR_MIN_1MS)) == 0U) { + ret = RFAL_ERR_IO; + } else { + /*Check if Observation Mode is enabled and set it on ST25R391x */ + rfalCheckEnableObsModeRx(); + + /* Jump into a transceive Rx state for reception (bypass Tx states) */ + gRFAL.state = RFAL_STATE_TXRX; + gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_IDLE; + gRFAL.TxRx.status = RFAL_ERR_BUSY; + + /* Execute Transceive Rx blocking */ + ret = rfalTransceiveBlockingRx(); + } + + /* Disable Collision interrupt */ + st25r3916DisableInterrupts((ST25R3916_IRQ_MASK_COL)); + + return ret; +} + +/*******************************************************************************/ +ReturnCode rfalISO14443ATransceiveAnticollisionFrame(uint8_t *buf, + uint8_t *bytesToSend, + uint8_t *bitsToSend, + uint16_t *rxLength, + uint32_t fwt) { + ReturnCode ret; + + RFAL_EXIT_ON_ERR(ret, rfalISO14443AStartTransceiveAnticollisionFrame( + buf, bytesToSend, bitsToSend, rxLength, fwt)); + rfalRunBlocking(ret, rfalISO14443AGetTransceiveAnticollisionFrameStatus()); + + return ret; +} + +/*******************************************************************************/ +ReturnCode rfalISO14443AStartTransceiveAnticollisionFrame(uint8_t *buf, + uint8_t *bytesToSend, + uint8_t *bitsToSend, + uint16_t *rxLength, + uint32_t fwt) { + ReturnCode ret; + rfalTransceiveContext ctx; + + /* Check if RFAL is properly initialized */ + if ((gRFAL.state < RFAL_STATE_MODE_SET) || + (gRFAL.mode != RFAL_MODE_POLL_NFCA)) { + return RFAL_ERR_WRONG_STATE; + } + + /* Check for valid parameters */ + if ((buf == NULL) || (bytesToSend == NULL) || (bitsToSend == NULL) || + (rxLength == NULL)) { + return RFAL_ERR_PARAM; + } + + /*******************************************************************************/ + /* Set speficic Analog Config for Anticolission if needed */ + rfalSetAnalogConfig((RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCA | + RFAL_ANALOG_CONFIG_BITRATE_COMMON | + RFAL_ANALOG_CONFIG_ANTICOL)); + + /*******************************************************************************/ + /* Enable anti collision to recognise collision in first byte of SENS_REQ */ + st25r3916SetRegisterBits(ST25R3916_REG_ISO14443A_NFC, + ST25R3916_REG_ISO14443A_NFC_antcl); + + /*******************************************************************************/ + /* Prepare for Transceive */ + ctx.flags = ((uint32_t)RFAL_TXRX_FLAGS_CRC_TX_MANUAL | + (uint32_t)RFAL_TXRX_FLAGS_CRC_RX_KEEP | + (uint32_t)RFAL_TXRX_FLAGS_CRC_RX_MANUAL); + ctx.txBuf = buf; + ctx.txBufLen = (uint16_t)(rfalConvBytesToBits(*bytesToSend) + *bitsToSend); + ctx.rxBuf = &buf[*bytesToSend]; + ctx.rxBufLen = (uint16_t)rfalConvBytesToBits(RFAL_ISO14443A_SDD_RES_LEN); + ctx.rxRcvdLen = rxLength; + ctx.fwt = fwt; + + /* Disable Automatic Gain Control (AGC) for better detection of collisions if + * using Coherent Receiver */ + ctx.flags |= (st25r3916CheckReg(ST25R3916_REG_AUX, ST25R3916_REG_AUX_dis_corr, + ST25R3916_REG_AUX_dis_corr) + ? (uint32_t)RFAL_TXRX_FLAGS_AGC_OFF + : 0x00U); + + RFAL_EXIT_ON_ERR(ret, rfalStartTransceive(&ctx)); + + /* Additionally enable bit collision interrupt */ + st25r3916GetInterrupt(ST25R3916_IRQ_MASK_COL); + st25r3916EnableInterrupts(ST25R3916_IRQ_MASK_COL); + + /*******************************************************************************/ + gRFAL.nfcaData.collByte = 0; + + /* Save the collision byte */ + if ((*bitsToSend) > 0U) { + buf[(*bytesToSend)] <<= (RFAL_BITS_IN_BYTE - (*bitsToSend)); + buf[(*bytesToSend)] >>= (RFAL_BITS_IN_BYTE - (*bitsToSend)); + gRFAL.nfcaData.collByte = buf[(*bytesToSend)]; + } + + gRFAL.nfcaData.buf = buf; + gRFAL.nfcaData.bytesToSend = bytesToSend; + gRFAL.nfcaData.bitsToSend = bitsToSend; + gRFAL.nfcaData.rxLength = rxLength; + + /*******************************************************************************/ + /* Run Transceive Tx */ + return rfalTransceiveRunBlockingTx(); +} + +/*******************************************************************************/ +ReturnCode rfalISO14443AGetTransceiveAnticollisionFrameStatus(void) { + ReturnCode ret; + uint8_t collData; + + RFAL_EXIT_ON_BUSY(ret, rfalGetTransceiveStatus()); + + /*******************************************************************************/ + if ((*gRFAL.nfcaData.bitsToSend) > 0U) { + gRFAL.nfcaData.buf[(*gRFAL.nfcaData.bytesToSend)] >>= + (*gRFAL.nfcaData.bitsToSend); + gRFAL.nfcaData.buf[(*gRFAL.nfcaData.bytesToSend)] <<= + (*gRFAL.nfcaData.bitsToSend); + gRFAL.nfcaData.buf[(*gRFAL.nfcaData.bytesToSend)] |= + gRFAL.nfcaData.collByte; + } + + if (ret == RFAL_ERR_RF_COLLISION) { + /* Read out collision register */ + st25r3916ReadRegister(ST25R3916_REG_COLLISION_STATUS, &collData); + + (*gRFAL.nfcaData.bytesToSend) = + ((collData >> ST25R3916_REG_COLLISION_STATUS_c_byte_shift) & + 0x0FU); // 4-bits Byte information + (*gRFAL.nfcaData.bitsToSend) = + ((collData >> ST25R3916_REG_COLLISION_STATUS_c_bit_shift) & + 0x07U); // 3-bits bit information + } + + /*******************************************************************************/ + /* Disable Collision interrupt */ + st25r3916DisableInterrupts((ST25R3916_IRQ_MASK_COL)); + + /* Disable anti collision again */ + st25r3916ClrRegisterBits(ST25R3916_REG_ISO14443A_NFC, + ST25R3916_REG_ISO14443A_NFC_antcl); + + /*******************************************************************************/ + /* Restore common Analog configurations for this mode */ + rfalSetAnalogConfig((RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCA | + rfalConvBR2ACBR(gRFAL.txBR) | RFAL_ANALOG_CONFIG_TX)); + rfalSetAnalogConfig((RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCA | + rfalConvBR2ACBR(gRFAL.rxBR) | RFAL_ANALOG_CONFIG_RX)); + + return ret; +} + +#endif /* RFAL_FEATURE_NFCA */ + +#if RFAL_FEATURE_NFCV + +/*******************************************************************************/ +ReturnCode rfalISO15693TransceiveAnticollisionFrame(uint8_t *txBuf, + uint8_t txBufLen, + uint8_t *rxBuf, + uint8_t rxBufLen, + uint16_t *actLen) { + ReturnCode ret; + rfalTransceiveContext ctx; + + /* Check if RFAL is properly initialized */ + if ((gRFAL.state < RFAL_STATE_MODE_SET) || + (gRFAL.mode != RFAL_MODE_POLL_NFCV)) { + return RFAL_ERR_WRONG_STATE; + } + + /*******************************************************************************/ + /* Set speficic Analog Config for Anticolission if needed */ + rfalSetAnalogConfig((RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCV | + RFAL_ANALOG_CONFIG_BITRATE_COMMON | + RFAL_ANALOG_CONFIG_ANTICOL)); + + /* Ignoring collisions before the UID (RES_FLAG + DSFID) */ + gRFAL.nfcvData.ignoreBits = (uint16_t)RFAL_ISO15693_IGNORE_BITS; + + /*******************************************************************************/ + /* Prepare for Transceive */ + ctx.flags = + ((txBufLen == 0U) ? (uint32_t)RFAL_TXRX_FLAGS_CRC_TX_MANUAL + : (uint32_t)RFAL_TXRX_FLAGS_CRC_TX_AUTO) | + (uint32_t)RFAL_TXRX_FLAGS_CRC_RX_KEEP | + (uint32_t)RFAL_TXRX_FLAGS_AGC_OFF | + ((txBufLen == 0U) + ? (uint32_t)RFAL_TXRX_FLAGS_NFCV_FLAG_MANUAL + : (uint32_t) + RFAL_TXRX_FLAGS_NFCV_FLAG_AUTO); /* Disable Automatic Gain + Control (AGC) for better + detection of collision */ + ctx.txBuf = txBuf; + ctx.txBufLen = (uint16_t)rfalConvBytesToBits(txBufLen); + ctx.rxBuf = rxBuf; + ctx.rxBufLen = (uint16_t)rfalConvBytesToBits(rxBufLen); + ctx.rxRcvdLen = actLen; + ctx.fwt = rfalConv64fcTo1fc(ISO15693_FWT); + + RFAL_EXIT_ON_ERR(ret, rfalStartTransceive(&ctx)); + + /*******************************************************************************/ + /* Run Transceive blocking */ + ret = rfalTransceiveRunBlockingTx(); + if (ret == RFAL_ERR_NONE) { + ret = rfalTransceiveBlockingRx(); + } + + /* Check if a Transmission error and received data is less then expected */ + if (((ret == RFAL_ERR_RF_COLLISION) || (ret == RFAL_ERR_CRC) || + (ret == RFAL_ERR_FRAMING)) && + (rfalConvBitsToBytes(*ctx.rxRcvdLen) < RFAL_ISO15693_INV_RES_LEN)) { + /* If INVENTORY_RES is shorter than expected, tag is still modulating * + * Ensure that response is complete before next frame */ + platformDelay( + (uint8_t)((RFAL_ISO15693_INV_RES_LEN - + rfalConvBitsToBytes(*ctx.rxRcvdLen)) / + ((RFAL_ISO15693_INV_RES_LEN / RFAL_ISO15693_INV_RES_DUR) + + 1U))); + } + + /* Restore common Analog configurations for this mode */ + rfalSetAnalogConfig((RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCV | + rfalConvBR2ACBR(gRFAL.txBR) | RFAL_ANALOG_CONFIG_TX)); + rfalSetAnalogConfig((RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCV | + rfalConvBR2ACBR(gRFAL.rxBR) | RFAL_ANALOG_CONFIG_RX)); + + gRFAL.nfcvData.ignoreBits = 0; + return ret; +} + +/*******************************************************************************/ +ReturnCode rfalISO15693TransceiveEOFAnticollision(uint8_t *rxBuf, + uint8_t rxBufLen, + uint16_t *actLen) { + uint8_t dummy; + + return rfalISO15693TransceiveAnticollisionFrame(&dummy, 0, rxBuf, rxBufLen, + actLen); +} + +/*******************************************************************************/ +ReturnCode rfalISO15693TransceiveEOF(uint8_t *rxBuf, uint16_t rxBufLen, + uint16_t *actLen) { + ReturnCode ret; + uint8_t dummy; + + /* Check if RFAL is properly initialized */ + if ((gRFAL.state < RFAL_STATE_MODE_SET) || + (gRFAL.mode != RFAL_MODE_POLL_NFCV)) { + return RFAL_ERR_WRONG_STATE; + } + + /*******************************************************************************/ + /* Run Transceive blocking */ + ret = rfalTransceiveBlockingTxRx(&dummy, 0, rxBuf, rxBufLen, actLen, + ((uint32_t)RFAL_TXRX_FLAGS_CRC_TX_MANUAL | + (uint32_t)RFAL_TXRX_FLAGS_CRC_RX_KEEP | + (uint32_t)RFAL_TXRX_FLAGS_AGC_ON), + rfalConv64fcTo1fc(ISO15693_FWT)); + return ret; +} + +#endif /* RFAL_FEATURE_NFCV */ + +#if RFAL_FEATURE_NFCF + +/*******************************************************************************/ +ReturnCode rfalFeliCaPoll(rfalFeliCaPollSlots slots, uint16_t sysCode, + uint8_t reqCode, rfalFeliCaPollRes *pollResList, + uint8_t pollResListSize, uint8_t *devicesDetected, + uint8_t *collisionsDetected) { + ReturnCode ret; + + RFAL_EXIT_ON_ERR( + ret, + rfalStartFeliCaPoll(slots, sysCode, reqCode, pollResList, pollResListSize, + devicesDetected, collisionsDetected)); + rfalRunBlocking(ret, rfalGetFeliCaPollStatus()); + + return ret; +} + +/*******************************************************************************/ +ReturnCode rfalStartFeliCaPoll(rfalFeliCaPollSlots slots, uint16_t sysCode, + uint8_t reqCode, rfalFeliCaPollRes *pollResList, + uint8_t pollResListSize, + uint8_t *devicesDetected, + uint8_t *collisionsDetected) { + ReturnCode ret; + uint8_t + frame[RFAL_FELICA_POLL_REQ_LEN - + RFAL_FELICA_LEN_LEN]; // LEN is added by ST25R391x automatically + uint8_t frameIdx; + + /* Check if RFAL is properly initialized */ + if ((gRFAL.state < RFAL_STATE_MODE_SET) || + (gRFAL.mode != RFAL_MODE_POLL_NFCF)) { + return RFAL_ERR_WRONG_STATE; + } + + frameIdx = 0; + gRFAL.nfcfData.colDetected = 0; + gRFAL.nfcfData.devDetected = 0; + + /*******************************************************************************/ + /* Compute SENSF_REQ frame */ + frame[frameIdx++] = (uint8_t)FELICA_CMD_POLLING; /* CMD: SENF_REQ */ + frame[frameIdx++] = (uint8_t)(sysCode >> 8); /* System Code (SC) */ + frame[frameIdx++] = (uint8_t)(sysCode & 0xFFU); /* System Code (SC) */ + frame[frameIdx++] = reqCode; /* Communication Parameter Request (RC)*/ + frame[frameIdx++] = (uint8_t)slots; /* TimeSlot (TSN) */ + + /*******************************************************************************/ + /* NRT should not stop on reception - Fake EMD which uses NRT in nrt_emv * + * RFAL_ERRORHANDLING_EMD has no special handling for NFC-F mode */ + gRFAL.nfcfData.curHandling = gRFAL.conf.eHandling; + gRFAL.conf.eHandling = RFAL_ERRORHANDLING_EMD; + + /*******************************************************************************/ + /* Run transceive blocking, + * Calculate Total Response Time in(64/fc): + * 512 PICC process time + (n * 256 Time Slot duration) + */ + RFAL_EXIT_ON_ERR(ret, rfalTransceiveBlockingTx( + frame, (uint16_t)frameIdx, + (uint8_t *)gRFAL.nfcfData.pollResponses, + RFAL_FELICA_POLL_RES_LEN, &gRFAL.nfcfData.actLen, + (RFAL_TXRX_FLAGS_DEFAULT), + rfalConv64fcTo1fc(RFAL_FELICA_POLL_DELAY_TIME + + (RFAL_FELICA_POLL_SLOT_TIME * + ((uint32_t)slots + 1U))))); + + /* Store context */ + gRFAL.nfcfData.pollResList = pollResList; + gRFAL.nfcfData.pollResListSize = pollResListSize; + gRFAL.nfcfData.devicesDetected = devicesDetected; + gRFAL.nfcfData.collisionsDetected = collisionsDetected; + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalGetFeliCaPollStatus(void) { + ReturnCode ret; + + /* Check if RFAL is properly initialized */ + if ((gRFAL.state != RFAL_STATE_TXRX) || (gRFAL.mode != RFAL_MODE_POLL_NFCF)) { + return RFAL_ERR_WRONG_STATE; + } + + /* Wait until transceive has terminated */ + RFAL_EXIT_ON_BUSY(ret, rfalGetTransceiveStatus()); + + /* Upon timeout the full Poll Delay + (Slot time)*(nbSlots) has expired */ + if (ret != RFAL_ERR_TIMEOUT) { + /* Reception done, reEnabled Rx for following Slot */ + /* The Rx reEnable is done before the check of NRT to be as fast as possible + * for the upcoming slot * Tslot = 1208us | SENSF_RES (19 payload + * bytes at 212) = 1135us -> Potentially ~75us between responses */ + st25r3916ExecuteCommand(ST25R3916_CMD_UNMASK_RECEIVE_DATA); + st25r3916ExecuteCommand(ST25R3916_CMD_RESET_RXGAIN); + rfalFIFOStatusClear(); + + /* If the reception was OK, new device found */ + if (ret == RFAL_ERR_NONE) { + gRFAL.nfcfData.devDetected++; + + /* Overwrite the Transceive context for the next reception */ + gRFAL.TxRx.ctx.rxBuf = + (uint8_t *)gRFAL.nfcfData.pollResponses[gRFAL.nfcfData.devDetected]; + } + /* If the reception was not OK, mark as collision */ + else { + gRFAL.nfcfData.colDetected++; + } + + /* Check whether that NRT has not expired meanwhile */ + if (st25r3916CheckReg(ST25R3916_REG_NFCIP1_BIT_RATE, + ST25R3916_REG_NFCIP1_BIT_RATE_nrt_on, + ST25R3916_REG_NFCIP1_BIT_RATE_nrt_on)) { + /* Jump again into transceive Rx state for the following reception */ + gRFAL.TxRx.status = RFAL_ERR_BUSY; + gRFAL.state = RFAL_STATE_TXRX; + gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_IDLE; + return RFAL_ERR_BUSY; + } + + /* In case NRT has expired meanwhile, ensure that Rx is disabled */ + st25r3916ExecuteCommand(ST25R3916_CMD_MASK_RECEIVE_DATA); + } + + /*******************************************************************************/ + /* Back to previous error handling (restore NRT to normal mode) */ + gRFAL.conf.eHandling = gRFAL.nfcfData.curHandling; + + /*******************************************************************************/ + /* Assign output parameters if requested */ + if ((gRFAL.nfcfData.pollResList != NULL) && + (gRFAL.nfcfData.pollResListSize > 0U) && + (gRFAL.nfcfData.devDetected > 0U)) { + RFAL_MEMCPY(gRFAL.nfcfData.pollResList, gRFAL.nfcfData.pollResponses, + (RFAL_FELICA_POLL_RES_LEN * + (uint32_t)RFAL_MIN(gRFAL.nfcfData.pollResListSize, + gRFAL.nfcfData.devDetected))); + } + + if (gRFAL.nfcfData.devicesDetected != NULL) { + *gRFAL.nfcfData.devicesDetected = gRFAL.nfcfData.devDetected; + } + + if (gRFAL.nfcfData.collisionsDetected != NULL) { + *gRFAL.nfcfData.collisionsDetected = gRFAL.nfcfData.colDetected; + } + + return ( + ((gRFAL.nfcfData.colDetected != 0U) || (gRFAL.nfcfData.devDetected != 0U)) + ? RFAL_ERR_NONE + : ret); +} + +#endif /* RFAL_FEATURE_NFCF */ + +/***************************************************************************** + * Listen Mode * + *****************************************************************************/ + +/*******************************************************************************/ +bool rfalIsExtFieldOn(void) { return st25r3916IsExtFieldOn(); } + +#if RFAL_FEATURE_LISTEN_MODE + +/*******************************************************************************/ +ReturnCode rfalListenStart(uint32_t lmMask, const rfalLmConfPA *confA, + const rfalLmConfPB *confB, const rfalLmConfPF *confF, + uint8_t *rxBuf, uint16_t rxBufLen, uint16_t *rxLen) { + t_rfalPTMem PTMem; /* PRQA S 0759 # MISRA 19.2 - Allocating Union where + members are of the same type, just different names. Thus + no problem can occur. */ + uint8_t *pPTMem; + uint8_t autoResp; + + /* Check if RFAL is initialized */ + if (gRFAL.state < RFAL_STATE_INIT) { + return RFAL_ERR_WRONG_STATE; + } + + gRFAL.Lm.state = RFAL_LM_STATE_NOT_INIT; + gRFAL.Lm.mdIrqs = ST25R3916_IRQ_MASK_NONE; + gRFAL.Lm.mdReg = (ST25R3916_REG_MODE_targ_init | ST25R3916_REG_MODE_om_nfc | + ST25R3916_REG_MODE_nfc_ar_off); + + /* By default disable all automatic responses */ + autoResp = (ST25R3916_REG_PASSIVE_TARGET_d_106_ac_a | + ST25R3916_REG_PASSIVE_TARGET_d_212_424_1r | + ST25R3916_REG_PASSIVE_TARGET_d_ac_ap2p); + + /*******************************************************************************/ + if ((lmMask & RFAL_LM_MASK_NFCA) != 0U) { + /* Check if the conf has been provided */ + if (confA == NULL) { + return RFAL_ERR_PARAM; + } + + pPTMem = (uint8_t *)PTMem.PTMem_A; + + /*******************************************************************************/ + /* Check and set supported NFCID Length */ + switch (confA->nfcidLen) { + case RFAL_LM_NFCID_LEN_04: + st25r3916ChangeRegisterBits(ST25R3916_REG_AUX, + ST25R3916_REG_AUX_nfc_id_mask, + ST25R3916_REG_AUX_nfc_id_4bytes); + break; + + case RFAL_LM_NFCID_LEN_07: + st25r3916ChangeRegisterBits(ST25R3916_REG_AUX, + ST25R3916_REG_AUX_nfc_id_mask, + ST25R3916_REG_AUX_nfc_id_7bytes); + break; + + default: + return RFAL_ERR_PARAM; + } + + /*******************************************************************************/ + /* Set NFCID */ + RFAL_MEMCPY(pPTMem, confA->nfcid, RFAL_NFCID1_TRIPLE_LEN); + pPTMem = &pPTMem[RFAL_NFCID1_TRIPLE_LEN]; /* MISRA 18.4 */ + + /* Set SENS_RES */ + RFAL_MEMCPY(pPTMem, confA->SENS_RES, RFAL_LM_SENS_RES_LEN); + pPTMem = &pPTMem[RFAL_LM_SENS_RES_LEN]; /* MISRA 18.4 */ + + /* Set SEL_RES */ + *(pPTMem++) = ((confA->nfcidLen == RFAL_LM_NFCID_LEN_04) + ? (confA->SEL_RES & ~RFAL_LM_NFCID_INCOMPLETE) + : (confA->SEL_RES | RFAL_LM_NFCID_INCOMPLETE)); + *(pPTMem++) = (confA->SEL_RES & ~RFAL_LM_NFCID_INCOMPLETE); + *(pPTMem++) = (confA->SEL_RES & ~RFAL_LM_NFCID_INCOMPLETE); + + /* Write into PTMem-A */ + st25r3916WritePTMem(PTMem.PTMem_A, ST25R3916_PTM_A_LEN); + + /*******************************************************************************/ + /* Enable automatic responses for A */ + autoResp &= ~ST25R3916_REG_PASSIVE_TARGET_d_106_ac_a; + + /* Set Target mode, Bit Rate detection and Listen Mode for NFC-A */ + gRFAL.Lm.mdReg |= (ST25R3916_REG_MODE_targ_targ | ST25R3916_REG_MODE_om3 | + ST25R3916_REG_MODE_om0 | ST25R3916_REG_MODE_nfc_ar_off); + + gRFAL.Lm.mdIrqs |= (ST25R3916_IRQ_MASK_WU_A | ST25R3916_IRQ_MASK_WU_A_X | + ST25R3916_IRQ_MASK_RXE_PTA); + } + + /*******************************************************************************/ + if ((lmMask & RFAL_LM_MASK_NFCB) != 0U) { + /* Check if the conf has been provided */ + if (confB == NULL) { + return RFAL_ERR_PARAM; + } + + return RFAL_ERR_NOTSUPP; + } + + /*******************************************************************************/ + if ((lmMask & RFAL_LM_MASK_NFCF) != 0U) { + pPTMem = (uint8_t *)PTMem.PTMem_F; + + /* Check if the conf has been provided */ + if (confF == NULL) { + return RFAL_ERR_PARAM; + } + + /*******************************************************************************/ + /* Set System Code */ + RFAL_MEMCPY(pPTMem, confF->SC, RFAL_LM_SENSF_SC_LEN); + pPTMem = &pPTMem[RFAL_LM_SENSF_SC_LEN]; /* MISRA 18.4 */ + + /* Set SENSF_RES */ + RFAL_MEMCPY(pPTMem, confF->SENSF_RES, RFAL_LM_SENSF_RES_LEN); + + /* Set RD bytes to 0x00 as ST25R3916 cannot support advances features */ + pPTMem[RFAL_LM_SENSF_RD0_POS] = + 0x00; /* NFC Forum Digital 1.1 Table 46: 0x00 */ + pPTMem[RFAL_LM_SENSF_RD1_POS] = + 0x00; /* NFC Forum Digital 1.1 Table 47: No automatic bit rates */ + + pPTMem = &pPTMem[RFAL_LM_SENS_RES_LEN]; /* MISRA 18.4 */ + + /* Write into PTMem-F */ + st25r3916WritePTMemF(PTMem.PTMem_F, ST25R3916_PTM_F_LEN); + + /*******************************************************************************/ + /* Write 24 TSN "Random" Numbers at first initialization and let it rollover + */ + if (!gRFAL.Lm.iniFlag) { + pPTMem = (uint8_t *)PTMem.TSN; + + *(pPTMem++) = 0x12; + *(pPTMem++) = 0x34; + *(pPTMem++) = 0x56; + *(pPTMem++) = 0x78; + *(pPTMem++) = 0x9A; + *(pPTMem++) = 0xBC; + *(pPTMem++) = 0xDF; + *(pPTMem++) = 0x21; + *(pPTMem++) = 0x43; + *(pPTMem++) = 0x65; + *(pPTMem++) = 0x87; + *(pPTMem++) = 0xA9; + + /* Write into PTMem-TSN */ + st25r3916WritePTMemTSN(PTMem.TSN, ST25R3916_PTM_TSN_LEN); + } + + /*******************************************************************************/ + /* Enable automatic responses for F */ + autoResp &= ~(ST25R3916_REG_PASSIVE_TARGET_d_212_424_1r); + + /* Set Target mode, Bit Rate detection and Listen Mode for NFC-F */ + gRFAL.Lm.mdReg |= (ST25R3916_REG_MODE_targ_targ | ST25R3916_REG_MODE_om3 | + ST25R3916_REG_MODE_om2 | ST25R3916_REG_MODE_nfc_ar_off); + + /* In CE NFC-F any data without error will be passed to FIFO, to support CUP + */ + gRFAL.Lm.mdIrqs |= (ST25R3916_IRQ_MASK_WU_F | ST25R3916_IRQ_MASK_RXE_PTA | + ST25R3916_IRQ_MASK_RXE); + } + + /*******************************************************************************/ + if ((lmMask & RFAL_LM_MASK_ACTIVE_P2P) != 0U) { + /* Enable Reception of P2P frames */ + autoResp &= ~(ST25R3916_REG_PASSIVE_TARGET_d_ac_ap2p); + + /* Set Target mode, Bit Rate detection and Automatic Response RF Collision + * Avoidance */ + gRFAL.Lm.mdReg |= (ST25R3916_REG_MODE_targ_targ | ST25R3916_REG_MODE_om3 | + ST25R3916_REG_MODE_om2 | ST25R3916_REG_MODE_om0 | + ST25R3916_REG_MODE_nfc_ar_auto_rx); + + /* Ensure CRC check is enabled */ + st25r3916ClrRegisterBits(ST25R3916_REG_AUX, ST25R3916_REG_AUX_no_crc_rx); + + /* n * TRFW timing shall vary Activity 2.1 3.4.1.1 */ + st25r3916ChangeRegisterBits(ST25R3916_REG_AUX, ST25R3916_REG_AUX_nfc_n_mask, + gRFAL.timings.nTRFW); + gRFAL.timings.nTRFW = rfalGennTRFW(gRFAL.timings.nTRFW); + + gRFAL.Lm.mdIrqs |= (ST25R3916_IRQ_MASK_RXE); + } + + /* Check if one of the modes were selected */ + if ((gRFAL.Lm.mdReg & ST25R3916_REG_MODE_targ) == + ST25R3916_REG_MODE_targ_targ) { + gRFAL.state = RFAL_STATE_LM; + gRFAL.Lm.mdMask = lmMask; + + gRFAL.Lm.rxBuf = rxBuf; + gRFAL.Lm.rxBufLen = rxBufLen; + gRFAL.Lm.rxLen = rxLen; + *gRFAL.Lm.rxLen = 0; + gRFAL.Lm.dataFlag = false; + gRFAL.Lm.iniFlag = true; + + /* Apply the Automatic Responses configuration */ + st25r3916ChangeRegisterBits(ST25R3916_REG_PASSIVE_TARGET, + (ST25R3916_REG_PASSIVE_TARGET_d_106_ac_a | + ST25R3916_REG_PASSIVE_TARGET_d_212_424_1r | + ST25R3916_REG_PASSIVE_TARGET_d_ac_ap2p), + autoResp); + + /* Disable GPT trigger source */ + st25r3916ChangeRegisterBits( + ST25R3916_REG_TIMER_EMV_CONTROL, + ST25R3916_REG_TIMER_EMV_CONTROL_gptc_mask, + ST25R3916_REG_TIMER_EMV_CONTROL_gptc_no_trigger); + + /* On Bit Rate Detection Mode ST25R391x will filter incoming frames during + * MRT time starting on External Field On event, use 512/fc steps */ + st25r3916SetRegisterBits(ST25R3916_REG_TIMER_EMV_CONTROL, + ST25R3916_REG_TIMER_EMV_CONTROL_mrt_step_512); + st25r3916WriteRegister(ST25R3916_REG_MASK_RX_TIMER, + (uint8_t)rfalConv1fcTo512fc(RFAL_LM_GT)); + + /* Restore default settings on NFCIP1 mode, Receiving parity + CRC bits and + * manual Tx Parity*/ + st25r3916ClrRegisterBits(ST25R3916_REG_ISO14443A_NFC, + (ST25R3916_REG_ISO14443A_NFC_no_tx_par | + ST25R3916_REG_ISO14443A_NFC_no_rx_par | + ST25R3916_REG_ISO14443A_NFC_nfc_f0)); + + /* External Field Detector enabled as Automatics on rfalInitialize() */ + + /* Set Analog configurations for generic Listen mode */ + /* Not on SetState(POWER OFF) as otherwise would be applied on every Field + * Event */ + rfalSetAnalogConfig( + (RFAL_ANALOG_CONFIG_TECH_CHIP | RFAL_ANALOG_CONFIG_CHIP_LISTEN_ON)); + + /* Initialize as POWER_OFF and set proper mode in RF Chip */ + rfalListenSetState(RFAL_LM_STATE_POWER_OFF); + } else { + return RFAL_ERR_REQUEST; /* Listen Start called but no mode was enabled */ + } + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +static ReturnCode rfalRunListenModeWorker(void) { + volatile uint32_t irqs; + uint8_t tmp; + + if (gRFAL.state != RFAL_STATE_LM) { + return RFAL_ERR_WRONG_STATE; + } + + switch (gRFAL.Lm.state) { + /*******************************************************************************/ + case RFAL_LM_STATE_POWER_OFF: + + irqs = st25r3916GetInterrupt((ST25R3916_IRQ_MASK_EON)); + if (irqs == ST25R3916_IRQ_MASK_NONE) { + break; /* No interrupt to process */ + } + + if ((irqs & ST25R3916_IRQ_MASK_EON) != 0U) { + rfalListenSetState(RFAL_LM_STATE_IDLE); + } else { + break; + } + /* fall through */ + + /*******************************************************************************/ + case RFAL_LM_STATE_IDLE: /* PRQA S 2003 # MISRA 16.3 - Intentional fall + through */ + + irqs = st25r3916GetInterrupt( + (ST25R3916_IRQ_MASK_NFCT | ST25R3916_IRQ_MASK_WU_F | + ST25R3916_IRQ_MASK_RXE | ST25R3916_IRQ_MASK_EOF | + ST25R3916_IRQ_MASK_RXE_PTA)); + if (irqs == ST25R3916_IRQ_MASK_NONE) { + break; /* No interrupt to process */ + } + + if ((irqs & ST25R3916_IRQ_MASK_NFCT) != 0U) { + /* Retrieve detected bitrate */ + uint8_t newBr; + st25r3916ReadRegister(ST25R3916_REG_NFCIP1_BIT_RATE, &newBr); + newBr >>= ST25R3916_REG_NFCIP1_BIT_RATE_nfc_rate_shift; + + if (newBr > ST25R3916_REG_BIT_RATE_rxrate_424) { + newBr = ST25R3916_REG_BIT_RATE_rxrate_424; + } + + gRFAL.Lm.brDetected = + (rfalBitRate)(newBr); /* PRQA S 4342 # MISRA 10.5 - Guaranteed that + no invalid enum values may be created. See + also equalityGuard_RFAL_BR_106 ff.*/ + } + + /* If EOF has already been received processing of other events is + * neglectable */ + if (((irqs & ST25R3916_IRQ_MASK_EOF) != 0U) && (!gRFAL.Lm.dataFlag)) { + rfalListenSetState(RFAL_LM_STATE_POWER_OFF); + } else if (((irqs & ST25R3916_IRQ_MASK_WU_F) != 0U) && + (gRFAL.Lm.brDetected != RFAL_BR_KEEP)) { + rfalListenSetState(RFAL_LM_STATE_READY_F); + } else if (((irqs & ST25R3916_IRQ_MASK_RXE) != 0U) && + (gRFAL.Lm.brDetected != RFAL_BR_KEEP)) { + irqs = st25r3916GetInterrupt( + (ST25R3916_IRQ_MASK_WU_F | ST25R3916_IRQ_MASK_RXE | + ST25R3916_IRQ_MASK_EOF | ST25R3916_IRQ_MASK_CRC | + ST25R3916_IRQ_MASK_PAR | ST25R3916_IRQ_MASK_ERR2 | + ST25R3916_IRQ_MASK_ERR1)); + + if (((irqs & ST25R3916_IRQ_MASK_CRC) != 0U) || + ((irqs & ST25R3916_IRQ_MASK_PAR) != 0U) || + ((irqs & ST25R3916_IRQ_MASK_ERR1) != 0U)) { + st25r3916ExecuteCommand(ST25R3916_CMD_CLEAR_FIFO); + st25r3916ExecuteCommand(ST25R3916_CMD_UNMASK_RECEIVE_DATA); + st25r3916TxOff(); + break; /* A bad reception occurred, remain in same state */ + } + + /*******************************************************************************/ + /* REMARK: Silicon workaround ST25R3916 Errata #TBD */ + /* In bitrate detection mode the automatic RF Collision Avoidance */ + /* may not be able to emit RF carrier depending on the pt_res setting */ + /* Preemptively enter AP2P before FIFO retrieval and protocol checking + */ + if ((gRFAL.Lm.mdMask & RFAL_LM_MASK_ACTIVE_P2P) != 0U) { + st25r3916WriteRegister( + ST25R3916_REG_MODE, + (ST25R3916_REG_MODE_targ_targ | ST25R3916_REG_MODE_om_targ_nfcip | + ST25R3916_REG_MODE_nfc_ar_eof)); + } + /*******************************************************************************/ + + /* Retrieve received data */ + *gRFAL.Lm.rxLen = st25r3916GetNumFIFOBytes(); + st25r3916ReadFifo( + gRFAL.Lm.rxBuf, + RFAL_MIN(*gRFAL.Lm.rxLen, rfalConvBitsToBytes(gRFAL.Lm.rxBufLen))); + +#ifdef ST25R3916 + /*******************************************************************************/ + /* REMARK: Silicon workaround ST25R3916 Errata #TBD */ + /* In bitrate detection mode CRC is not checked for NFC-A frames */ + if ((*gRFAL.Lm.rxLen > RFAL_CRC_LEN) && + (gRFAL.Lm.brDetected == RFAL_BR_106)) { + if (rfalCrcCalculateCcitt(RFAL_ISO14443A_CRC_INTVAL, gRFAL.Lm.rxBuf, + *gRFAL.Lm.rxLen) != 0U) { + st25r3916ExecuteCommand(ST25R3916_CMD_CLEAR_FIFO); + st25r3916ExecuteCommand(ST25R3916_CMD_UNMASK_RECEIVE_DATA); + st25r3916TxOff(); + break; /* A bad reception occurred, remain in same state */ + } + } + /*******************************************************************************/ +#endif /* ST25R3916 */ + + /* Check if the data we got has at least the CRC and remove it, + * otherwise leave at 0 */ + *gRFAL.Lm.rxLen -= + ((*gRFAL.Lm.rxLen > RFAL_CRC_LEN) ? RFAL_CRC_LEN : *gRFAL.Lm.rxLen); + *gRFAL.Lm.rxLen = (uint16_t)rfalConvBytesToBits(*gRFAL.Lm.rxLen); + gRFAL.Lm.dataFlag = true; + + /*Check if Observation Mode was enabled and disable it on ST25R391x */ + rfalCheckDisableObsMode(); + } else if (((irqs & ST25R3916_IRQ_MASK_RXE_PTA) != 0U) && + (gRFAL.Lm.brDetected != RFAL_BR_KEEP)) { + if (((gRFAL.Lm.mdMask & RFAL_LM_MASK_NFCA) != 0U) && + (gRFAL.Lm.brDetected == RFAL_BR_106)) { + st25r3916ReadRegister(ST25R3916_REG_PASSIVE_TARGET_STATUS, &tmp); + if (tmp > ST25R3916_REG_PASSIVE_TARGET_STATUS_pta_st_idle) { + rfalListenSetState(RFAL_LM_STATE_READY_A); + } + } + } else { + /* MISRA 15.7 - Empty else */ + } + break; + + /*******************************************************************************/ + case RFAL_LM_STATE_READY_F: + + irqs = st25r3916GetInterrupt((ST25R3916_IRQ_MASK_WU_F | + ST25R3916_IRQ_MASK_RXE | + ST25R3916_IRQ_MASK_EOF)); + if (irqs == ST25R3916_IRQ_MASK_NONE) { + break; /* No interrupt to process */ + } + + /* If EOF has already been received processing of other events is + * neglectable */ + if ((irqs & ST25R3916_IRQ_MASK_EOF) != 0U) { + rfalListenSetState(RFAL_LM_STATE_POWER_OFF); + } else if ((irqs & ST25R3916_IRQ_MASK_RXE) != 0U) { + /* Retrieve the error flags/irqs */ + irqs |= st25r3916GetInterrupt((ST25R3916_IRQ_MASK_CRC | + ST25R3916_IRQ_MASK_ERR2 | + ST25R3916_IRQ_MASK_ERR1)); + + if (((irqs & ST25R3916_IRQ_MASK_CRC) != 0U) || + ((irqs & ST25R3916_IRQ_MASK_ERR1) != 0U)) { + st25r3916ExecuteCommand(ST25R3916_CMD_CLEAR_FIFO); + st25r3916ExecuteCommand(ST25R3916_CMD_UNMASK_RECEIVE_DATA); + break; /* A bad reception occurred, remain in same state */ + } + + /* Retrieve received data */ + *gRFAL.Lm.rxLen = st25r3916GetNumFIFOBytes(); + st25r3916ReadFifo( + gRFAL.Lm.rxBuf, + RFAL_MIN(*gRFAL.Lm.rxLen, rfalConvBitsToBytes(gRFAL.Lm.rxBufLen))); + + /* Check if the data we got has at least the CRC and remove it, + * otherwise leave at 0 */ + *gRFAL.Lm.rxLen -= + ((*gRFAL.Lm.rxLen > RFAL_CRC_LEN) ? RFAL_CRC_LEN : *gRFAL.Lm.rxLen); + *gRFAL.Lm.rxLen = (uint16_t)rfalConvBytesToBits(*gRFAL.Lm.rxLen); + gRFAL.Lm.dataFlag = true; + } else if ((irqs & ST25R3916_IRQ_MASK_WU_F) != 0U) { + break; /* Remain in same state */ + } else { + /* MISRA 15.7 - Empty else */ + } + break; + + /*******************************************************************************/ + case RFAL_LM_STATE_READY_A: + + irqs = st25r3916GetInterrupt( + (ST25R3916_IRQ_MASK_EOF | ST25R3916_IRQ_MASK_WU_A)); + if (irqs == ST25R3916_IRQ_MASK_NONE) { + break; /* No interrupt to process */ + } + + /* If EOF has already been received processing of other events is + * neglectable */ + if ((irqs & ST25R3916_IRQ_MASK_EOF) != 0U) { + rfalListenSetState(RFAL_LM_STATE_POWER_OFF); + } else if ((irqs & ST25R3916_IRQ_MASK_WU_A) != 0U) { + rfalListenSetState(RFAL_LM_STATE_ACTIVE_A); + } else { + /* MISRA 15.7 - Empty else */ + } + break; + + /*******************************************************************************/ + case RFAL_LM_STATE_ACTIVE_A: + case RFAL_LM_STATE_ACTIVE_Ax: + + irqs = st25r3916GetInterrupt( + (ST25R3916_IRQ_MASK_RXE | ST25R3916_IRQ_MASK_EOF)); + if (irqs == ST25R3916_IRQ_MASK_NONE) { + break; /* No interrupt to process */ + } + + /* If EOF has already been received processing of other events is + * neglectable */ + if ((irqs & ST25R3916_IRQ_MASK_EOF) != 0U) { + rfalListenSetState(RFAL_LM_STATE_POWER_OFF); + } else if ((irqs & ST25R3916_IRQ_MASK_RXE) != 0U) { + /* Retrieve the error flags/irqs */ + irqs |= st25r3916GetInterrupt( + (ST25R3916_IRQ_MASK_PAR | ST25R3916_IRQ_MASK_CRC | + ST25R3916_IRQ_MASK_ERR2 | ST25R3916_IRQ_MASK_ERR1)); + *gRFAL.Lm.rxLen = st25r3916GetNumFIFOBytes(); + + if (((irqs & ST25R3916_IRQ_MASK_CRC) != 0U) || + ((irqs & ST25R3916_IRQ_MASK_ERR1) != 0U) || + ((irqs & ST25R3916_IRQ_MASK_PAR) != 0U) || + (*gRFAL.Lm.rxLen <= RFAL_CRC_LEN)) { + /* Clear rx context and FIFO */ + *gRFAL.Lm.rxLen = 0; + st25r3916ExecuteCommand(ST25R3916_CMD_CLEAR_FIFO); + st25r3916ExecuteCommand(ST25R3916_CMD_UNMASK_RECEIVE_DATA); + + /* Check if we should go to IDLE or Sleep */ + if (gRFAL.Lm.state == RFAL_LM_STATE_ACTIVE_Ax) { + rfalListenSleepStart(RFAL_LM_STATE_SLEEP_A, gRFAL.Lm.rxBuf, + gRFAL.Lm.rxBufLen, gRFAL.Lm.rxLen); + } else { + rfalListenSetState(RFAL_LM_STATE_IDLE); + } + + st25r3916DisableInterrupts(ST25R3916_IRQ_MASK_RXE); + break; + } + + /* Remove CRC from length */ + *gRFAL.Lm.rxLen -= RFAL_CRC_LEN; + + /* Retrieve received data */ + st25r3916ReadFifo( + gRFAL.Lm.rxBuf, + RFAL_MIN(*gRFAL.Lm.rxLen, rfalConvBitsToBytes(gRFAL.Lm.rxBufLen))); + *gRFAL.Lm.rxLen = (uint16_t)rfalConvBytesToBits(*gRFAL.Lm.rxLen); + gRFAL.Lm.dataFlag = true; + } else { + /* MISRA 15.7 - Empty else */ + } + break; + + /*******************************************************************************/ + case RFAL_LM_STATE_SLEEP_A: + case RFAL_LM_STATE_SLEEP_B: + case RFAL_LM_STATE_SLEEP_AF: + + irqs = st25r3916GetInterrupt( + (ST25R3916_IRQ_MASK_NFCT | ST25R3916_IRQ_MASK_WU_F | + ST25R3916_IRQ_MASK_RXE | ST25R3916_IRQ_MASK_EOF | + ST25R3916_IRQ_MASK_RXE_PTA)); + if (irqs == ST25R3916_IRQ_MASK_NONE) { + break; /* No interrupt to process */ + } + + if ((irqs & ST25R3916_IRQ_MASK_NFCT) != 0U) { + uint8_t newBr; + /* Retrieve detected bitrate */ + st25r3916ReadRegister(ST25R3916_REG_NFCIP1_BIT_RATE, &newBr); + newBr >>= ST25R3916_REG_NFCIP1_BIT_RATE_nfc_rate_shift; + + if (newBr > ST25R3916_REG_BIT_RATE_rxrate_424) { + newBr = ST25R3916_REG_BIT_RATE_rxrate_424; + } + + gRFAL.Lm.brDetected = + (rfalBitRate)(newBr); /* PRQA S 4342 # MISRA 10.5 - Guaranteed that + no invalid enum values may be created. See + also equalityGuard_RFAL_BR_106 ff.*/ + } + + /* If EOF has already been received processing of other events is + * neglectable */ + if ((irqs & ST25R3916_IRQ_MASK_EOF) != 0U) { + rfalListenSetState(RFAL_LM_STATE_POWER_OFF); + } else if (((irqs & ST25R3916_IRQ_MASK_WU_F) != 0U) && + (gRFAL.Lm.brDetected != RFAL_BR_KEEP)) { + rfalListenSetState(RFAL_LM_STATE_READY_F); + } else if (((irqs & ST25R3916_IRQ_MASK_RXE) != 0U) && + (gRFAL.Lm.brDetected != RFAL_BR_KEEP)) { + /* Clear rx context and FIFO */ + *gRFAL.Lm.rxLen = 0; + st25r3916ExecuteCommand(ST25R3916_CMD_CLEAR_FIFO); + st25r3916ExecuteCommand(ST25R3916_CMD_UNMASK_RECEIVE_DATA); + + /* REMARK: In order to support CUP or proprietary frames, handling could + * be added here */ + } else if (((irqs & ST25R3916_IRQ_MASK_RXE_PTA) != 0U) && + (gRFAL.Lm.brDetected != RFAL_BR_KEEP)) { + if (((gRFAL.Lm.mdMask & RFAL_LM_MASK_NFCA) != 0U) && + (gRFAL.Lm.brDetected == RFAL_BR_106)) { + st25r3916ReadRegister(ST25R3916_REG_PASSIVE_TARGET_STATUS, &tmp); + if (tmp > ST25R3916_REG_PASSIVE_TARGET_STATUS_pta_st_halt) { + rfalListenSetState(RFAL_LM_STATE_READY_Ax); + } + } + } else { + /* MISRA 15.7 - Empty else */ + } + break; + + /*******************************************************************************/ + case RFAL_LM_STATE_READY_Ax: + + irqs = st25r3916GetInterrupt( + (ST25R3916_IRQ_MASK_EOF | ST25R3916_IRQ_MASK_WU_A_X)); + if (irqs == ST25R3916_IRQ_MASK_NONE) { + break; /* No interrupt to process */ + } + + /* If EOF has already been received processing of other events is + * neglectable */ + if ((irqs & ST25R3916_IRQ_MASK_EOF) != 0U) { + rfalListenSetState(RFAL_LM_STATE_POWER_OFF); + } else if ((irqs & ST25R3916_IRQ_MASK_WU_A_X) != 0U) { + rfalListenSetState(RFAL_LM_STATE_ACTIVE_Ax); + } else { + /* MISRA 15.7 - Empty else */ + } + break; + + /*******************************************************************************/ + case RFAL_LM_STATE_CARDEMU_4A: + case RFAL_LM_STATE_CARDEMU_4B: + case RFAL_LM_STATE_CARDEMU_3: + case RFAL_LM_STATE_TARGET_F: + case RFAL_LM_STATE_TARGET_A: + break; + + /*******************************************************************************/ + default: + return RFAL_ERR_WRONG_STATE; + } + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalListenStop(void) { + /* Check if RFAL is initialized */ + if (gRFAL.state < RFAL_STATE_INIT) { + return RFAL_ERR_WRONG_STATE; + } + + gRFAL.Lm.state = RFAL_LM_STATE_NOT_INIT; + + /*Check if Observation Mode was enabled and disable it on ST25R391x */ + rfalCheckDisableObsMode(); + + /* Re-Enable the Oscillator if not running */ + st25r3916OscOn(); + + /* Disable Receiver and Transmitter */ + rfalFieldOff(); + + /* Disable all automatic responses */ + st25r3916SetRegisterBits(ST25R3916_REG_PASSIVE_TARGET, + (ST25R3916_REG_PASSIVE_TARGET_d_212_424_1r | + ST25R3916_REG_PASSIVE_TARGET_d_106_ac_a | + ST25R3916_REG_PASSIVE_TARGET_d_ac_ap2p)); + + /* As there's no Off mode, set default value: ISO14443A with automatic RF + * Collision Avoidance Off */ + st25r3916WriteRegister(ST25R3916_REG_MODE, (ST25R3916_REG_MODE_om_iso14443a | + ST25R3916_REG_MODE_tr_am_ook | + ST25R3916_REG_MODE_nfc_ar_off)); + + st25r3916DisableInterrupts( + (ST25R3916_IRQ_MASK_RXE_PTA | ST25R3916_IRQ_MASK_WU_F | + ST25R3916_IRQ_MASK_WU_A | ST25R3916_IRQ_MASK_WU_A_X | + ST25R3916_IRQ_MASK_RFU2 | ST25R3916_IRQ_MASK_OSC)); + st25r3916GetInterrupt((ST25R3916_IRQ_MASK_RXE_PTA | ST25R3916_IRQ_MASK_WU_F | + ST25R3916_IRQ_MASK_WU_A | ST25R3916_IRQ_MASK_WU_A_X | + ST25R3916_IRQ_MASK_RFU2 | ST25R3916_IRQ_MASK_TXE)); + + /* Set Analog configurations for Listen Off event */ + rfalSetAnalogConfig( + (RFAL_ANALOG_CONFIG_TECH_CHIP | RFAL_ANALOG_CONFIG_CHIP_LISTEN_OFF)); + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalListenSleepStart(rfalLmState sleepSt, uint8_t *rxBuf, + uint16_t rxBufLen, uint16_t *rxLen) { + /* Check if RFAL is not initialized */ + if (gRFAL.state < RFAL_STATE_INIT) { + return RFAL_ERR_WRONG_STATE; + } + + switch (sleepSt) { + /*******************************************************************************/ + case RFAL_LM_STATE_SLEEP_A: + + /* Enable automatic responses for A */ + st25r3916ClrRegisterBits(ST25R3916_REG_PASSIVE_TARGET, + (ST25R3916_REG_PASSIVE_TARGET_d_106_ac_a)); + + /* Reset NFCA target */ + st25r3916ExecuteCommand(ST25R3916_CMD_GOTO_SLEEP); + + /* Set Target mode, Bit Rate detection and Listen Mode for NFC-A */ + st25r3916ChangeRegisterBits( + ST25R3916_REG_MODE, + (ST25R3916_REG_MODE_targ | ST25R3916_REG_MODE_om_mask | + ST25R3916_REG_MODE_nfc_ar_mask), + (ST25R3916_REG_MODE_targ_targ | ST25R3916_REG_MODE_om3 | + ST25R3916_REG_MODE_om0 | ST25R3916_REG_MODE_nfc_ar_off)); + break; + + /*******************************************************************************/ + case RFAL_LM_STATE_SLEEP_AF: + + /* Enable automatic responses for A + F */ + st25r3916ClrRegisterBits(ST25R3916_REG_PASSIVE_TARGET, + (ST25R3916_REG_PASSIVE_TARGET_d_212_424_1r | + ST25R3916_REG_PASSIVE_TARGET_d_106_ac_a)); + + /* Reset NFCA target state */ + st25r3916ExecuteCommand(ST25R3916_CMD_GOTO_SLEEP); + + /* Set Target mode, Bit Rate detection, Listen Mode for NFC-A and NFC-F */ + st25r3916ChangeRegisterBits( + ST25R3916_REG_MODE, + (ST25R3916_REG_MODE_targ | ST25R3916_REG_MODE_om_mask | + ST25R3916_REG_MODE_nfc_ar_mask), + (ST25R3916_REG_MODE_targ_targ | ST25R3916_REG_MODE_om3 | + ST25R3916_REG_MODE_om2 | ST25R3916_REG_MODE_om0 | + ST25R3916_REG_MODE_nfc_ar_off)); + break; + + /*******************************************************************************/ + case RFAL_LM_STATE_SLEEP_B: + /* REMARK: Support for CE-B would be added here */ + return RFAL_ERR_NOT_IMPLEMENTED; + + /*******************************************************************************/ + default: + return RFAL_ERR_PARAM; + } + + /* Ensure that the NFCIP1 mode is disabled */ + st25r3916ClrRegisterBits(ST25R3916_REG_ISO14443A_NFC, + ST25R3916_REG_ISO14443A_NFC_nfc_f0); + + st25r3916ExecuteCommand(ST25R3916_CMD_UNMASK_RECEIVE_DATA); + + /* Clear and enable required IRQs */ + st25r3916ClearAndEnableInterrupts( + (ST25R3916_IRQ_MASK_NFCT | ST25R3916_IRQ_MASK_RXS | + ST25R3916_IRQ_MASK_CRC | ST25R3916_IRQ_MASK_ERR1 | + ST25R3916_IRQ_MASK_ERR2 | ST25R3916_IRQ_MASK_PAR | + ST25R3916_IRQ_MASK_EON | ST25R3916_IRQ_MASK_EOF | gRFAL.Lm.mdIrqs)); + + /* Check whether the field was turn off right after the Sleep request */ + if (!rfalIsExtFieldOn()) { +#if 0 /* Debug purposes */ + rfalLogD( "RFAL: curState: %02X newState: %02X \r\n", gRFAL.Lm.state, RFAL_LM_STATE_NOT_INIT ); +#endif + + rfalListenStop(); + return RFAL_ERR_LINK_LOSS; + } + +#if 0 /* Debug purposes */ + rfalLogD( "RFAL: curState: %02X newState: %02X \r\n", gRFAL.Lm.state, sleepSt ); +#endif + + /* Set the new Sleep State*/ + gRFAL.Lm.state = sleepSt; + gRFAL.state = RFAL_STATE_LM; + + gRFAL.Lm.rxBuf = rxBuf; + gRFAL.Lm.rxBufLen = rxBufLen; + gRFAL.Lm.rxLen = rxLen; + *gRFAL.Lm.rxLen = 0; + gRFAL.Lm.dataFlag = false; + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +rfalLmState rfalListenGetState(bool *dataFlag, rfalBitRate *lastBR) { + /* Allow state retrieval even if gRFAL.state != RFAL_STATE_LM so * + * that this Lm state can be used by caller after activation */ + + if (lastBR != NULL) { + *lastBR = gRFAL.Lm.brDetected; + } + + if (dataFlag != NULL) { + *dataFlag = gRFAL.Lm.dataFlag; + } + + return gRFAL.Lm.state; +} + +/*******************************************************************************/ +ReturnCode rfalListenSetState(rfalLmState newSt) { + ReturnCode ret; + rfalLmState newState; + bool reSetState; + + /* Check if RFAL is initialized */ + if (gRFAL.state < RFAL_STATE_INIT) { + return RFAL_ERR_WRONG_STATE; + } + + /* SetState clears the Data flag */ + gRFAL.Lm.dataFlag = false; + newState = newSt; + ret = RFAL_ERR_NONE; + + do { + reSetState = false; + + /*******************************************************************************/ + switch (newState) { + /*******************************************************************************/ + case RFAL_LM_STATE_POWER_OFF: + + /* Enable the receiver and reset logic */ + st25r3916SetRegisterBits(ST25R3916_REG_OP_CONTROL, + ST25R3916_REG_OP_CONTROL_rx_en); + st25r3916ExecuteCommand(ST25R3916_CMD_STOP); + + if ((gRFAL.Lm.mdMask & RFAL_LM_MASK_NFCA) != 0U) { + /* Enable automatic responses for A */ + st25r3916ClrRegisterBits(ST25R3916_REG_PASSIVE_TARGET, + ST25R3916_REG_PASSIVE_TARGET_d_106_ac_a); + + /* Prepares the NFCIP-1 Passive target logic to wait in the Sense/Idle + * state */ + st25r3916ExecuteCommand(ST25R3916_CMD_GOTO_SENSE); + } + + if ((gRFAL.Lm.mdMask & RFAL_LM_MASK_NFCF) != 0U) { + /* Enable automatic responses for F */ + st25r3916ClrRegisterBits(ST25R3916_REG_PASSIVE_TARGET, + (ST25R3916_REG_PASSIVE_TARGET_d_212_424_1r)); + } + + if ((gRFAL.Lm.mdMask & RFAL_LM_MASK_ACTIVE_P2P) != 0U) { + /* Ensure automatic response RF Collision Avoidance is back to only + * after Rx */ + st25r3916ChangeRegisterBits(ST25R3916_REG_MODE, + ST25R3916_REG_MODE_nfc_ar_mask, + ST25R3916_REG_MODE_nfc_ar_auto_rx); + + /* Ensure that our field is Off, as automatic response RF Collision + * Avoidance may have been triggered */ + st25r3916TxOff(); + } + + /*******************************************************************************/ + /* Ensure that the NFCIP1 mode is disabled */ + st25r3916ClrRegisterBits(ST25R3916_REG_ISO14443A_NFC, + ST25R3916_REG_ISO14443A_NFC_nfc_f0); + + /*******************************************************************************/ + /* Clear and enable required IRQs */ + st25r3916DisableInterrupts(ST25R3916_IRQ_MASK_ALL); + + st25r3916ClearAndEnableInterrupts( + (ST25R3916_IRQ_MASK_NFCT | ST25R3916_IRQ_MASK_RXS | + ST25R3916_IRQ_MASK_CRC | ST25R3916_IRQ_MASK_ERR1 | + ST25R3916_IRQ_MASK_OSC | ST25R3916_IRQ_MASK_ERR2 | + ST25R3916_IRQ_MASK_PAR | ST25R3916_IRQ_MASK_EON | + ST25R3916_IRQ_MASK_EOF | gRFAL.Lm.mdIrqs)); + + /*******************************************************************************/ + /* Clear the bitRate previously detected */ + gRFAL.Lm.brDetected = RFAL_BR_KEEP; + + /*******************************************************************************/ + /* Apply the initial mode */ + st25r3916ChangeRegisterBits( + ST25R3916_REG_MODE, + (ST25R3916_REG_MODE_targ | ST25R3916_REG_MODE_om_mask | + ST25R3916_REG_MODE_nfc_ar_mask), + (uint8_t)gRFAL.Lm.mdReg); + + /*******************************************************************************/ + /* Check if external Field is already On */ + if (rfalIsExtFieldOn()) { + reSetState = true; + newState = RFAL_LM_STATE_IDLE; /* Set IDLE state */ + } +#if 1 /* Perform bit rate detection in Low power mode */ + else { + st25r3916ClrRegisterBits( + ST25R3916_REG_OP_CONTROL, + (ST25R3916_REG_OP_CONTROL_tx_en | ST25R3916_REG_OP_CONTROL_rx_en | + ST25R3916_REG_OP_CONTROL_en)); + } +#endif + break; + + /*******************************************************************************/ + case RFAL_LM_STATE_IDLE: + + /*******************************************************************************/ + /* Check if device is coming from Low Power bit rate detection */ + if (!st25r3916CheckReg(ST25R3916_REG_OP_CONTROL, + ST25R3916_REG_OP_CONTROL_en, + ST25R3916_REG_OP_CONTROL_en)) { + /* Exit Low Power mode and confirm the temporarily enable */ + st25r3916SetRegisterBits( + ST25R3916_REG_OP_CONTROL, + (ST25R3916_REG_OP_CONTROL_en | ST25R3916_REG_OP_CONTROL_rx_en)); + + if (!st25r3916CheckReg(ST25R3916_REG_AUX_DISPLAY, + ST25R3916_REG_AUX_DISPLAY_osc_ok, + ST25R3916_REG_AUX_DISPLAY_osc_ok)) { + /* Wait for Oscilator ready */ + if (st25r3916WaitForInterruptsTimed( + ST25R3916_IRQ_MASK_OSC, ST25R3916_TOUT_OSC_STABLE) == 0U) { + ret = RFAL_ERR_IO; + break; + } + } + } else { + st25r3916GetInterrupt(ST25R3916_IRQ_MASK_OSC); + } + + /*******************************************************************************/ + /* Execute LM EON Callback */ + /*******************************************************************************/ + if (gRFAL.callbacks.lmEon != NULL) { + gRFAL.callbacks.lmEon(); + } + /*******************************************************************************/ + + /*******************************************************************************/ + /* In Active P2P the Initiator may: Turn its field On; LM goes into + * IDLE state; Initiator sends an unexpected frame raising a Protocol + * error; Initiator turns its field Off and ST25R3916 performs the + * automatic RF Collision Avoidance keeping our field On; upon a + * Protocol error upper layer sets again the state to IDLE to clear + * dataFlag and wait for next data. + * + * Ensure that when upper layer calls SetState(IDLE), it restores + * initial configuration and that check whether an external Field is + * still present */ + if ((gRFAL.Lm.mdMask & RFAL_LM_MASK_ACTIVE_P2P) != 0U) { + /* Ensure nfc_ar is reseted and back to only after Rx */ + st25r3916ExecuteCommand(ST25R3916_CMD_STOP); + st25r3916ChangeRegisterBits(ST25R3916_REG_MODE, + ST25R3916_REG_MODE_nfc_ar_mask, + ST25R3916_REG_MODE_nfc_ar_auto_rx); + + /* Ensure that our field is Off, as automatic response RF Collision + * Avoidance may have been triggered */ + st25r3916TxOff(); + + /* If external Field is no longer detected go back to POWER_OFF */ + if (!st25r3916IsExtFieldOn()) { + reSetState = true; + newState = RFAL_LM_STATE_POWER_OFF; /* Set POWER_OFF state */ + } + } + /*******************************************************************************/ + + /* If we are in ACTIVE_A, reEnable Listen for A before going to IDLE, + * otherwise do nothing */ + if (gRFAL.Lm.state == RFAL_LM_STATE_ACTIVE_A) { + /* Enable automatic responses for A and Reset NFCA target state */ + st25r3916ClrRegisterBits(ST25R3916_REG_PASSIVE_TARGET, + (ST25R3916_REG_PASSIVE_TARGET_d_106_ac_a)); + st25r3916ExecuteCommand(ST25R3916_CMD_GOTO_SENSE); + } + + /* ReEnable the receiver */ + st25r3916ExecuteCommand(ST25R3916_CMD_CLEAR_FIFO); + st25r3916ExecuteCommand(ST25R3916_CMD_UNMASK_RECEIVE_DATA); + + /*******************************************************************************/ + /*Check if Observation Mode is enabled and set it on ST25R391x */ + rfalCheckEnableObsModeRx(); + break; + + /*******************************************************************************/ + case RFAL_LM_STATE_READY_F: + + /*******************************************************************************/ + /* If we're coming from BitRate detection mode, the Bit Rate Definition + * reg still has the last bit rate used. If a frame is received between + * setting the mode to Listen NFCA and setting Bit Rate Definition reg, + * it will raise a framing error. Set the bitrate immediately, and then + * the normal SetMode procedure */ + st25r3916SetBitrate((uint8_t)gRFAL.Lm.brDetected, + (uint8_t)gRFAL.Lm.brDetected); + /*******************************************************************************/ + + /* Disable automatic responses for NFC-A */ + st25r3916SetRegisterBits(ST25R3916_REG_PASSIVE_TARGET, + (ST25R3916_REG_PASSIVE_TARGET_d_106_ac_a)); + + /* Set Mode NFC-F only */ + ret = rfalSetMode(RFAL_MODE_LISTEN_NFCF, gRFAL.Lm.brDetected, + gRFAL.Lm.brDetected); + gRFAL.state = RFAL_STATE_LM; /* Keep in Listen Mode */ + + /* ReEnable the receiver */ + st25r3916ExecuteCommand(ST25R3916_CMD_CLEAR_FIFO); + st25r3916ExecuteCommand(ST25R3916_CMD_UNMASK_RECEIVE_DATA); + + /* Clear any previous transmission errors (if Reader polled for + * other/unsupported technologies) */ + st25r3916GetInterrupt((ST25R3916_IRQ_MASK_PAR | ST25R3916_IRQ_MASK_CRC | + ST25R3916_IRQ_MASK_ERR2 | + ST25R3916_IRQ_MASK_ERR1)); + + st25r3916EnableInterrupts( + ST25R3916_IRQ_MASK_RXE); /* Start looking for any incoming data */ + break; + + /*******************************************************************************/ + case RFAL_LM_STATE_CARDEMU_3: + + /* Set Listen NFCF mode */ + ret = rfalSetMode(RFAL_MODE_LISTEN_NFCF, gRFAL.Lm.brDetected, + gRFAL.Lm.brDetected); + break; + + /*******************************************************************************/ + case RFAL_LM_STATE_READY_Ax: + case RFAL_LM_STATE_READY_A: + + /*******************************************************************************/ + /* If we're coming from BitRate detection mode, the Bit Rate Definition + * reg still has the last bit rate used. If a frame is received between + * setting the mode to Listen NFCA and setting Bit Rate Definition reg, + * it will raise a framing error. Set the bitrate immediately, and then + * the normal SetMode procedure */ + st25r3916SetBitrate((uint8_t)gRFAL.Lm.brDetected, + (uint8_t)gRFAL.Lm.brDetected); + /*******************************************************************************/ + + /* Disable automatic responses for NFC-F */ + st25r3916SetRegisterBits(ST25R3916_REG_PASSIVE_TARGET, + (ST25R3916_REG_PASSIVE_TARGET_d_212_424_1r)); + + /* Set Mode NFC-A only */ + ret = rfalSetMode(RFAL_MODE_LISTEN_NFCA, gRFAL.Lm.brDetected, + gRFAL.Lm.brDetected); + + gRFAL.state = RFAL_STATE_LM; /* Keep in Listen Mode */ + break; + + /*******************************************************************************/ + case RFAL_LM_STATE_ACTIVE_Ax: + case RFAL_LM_STATE_ACTIVE_A: + + /* Disable automatic responses for A */ + st25r3916SetRegisterBits(ST25R3916_REG_PASSIVE_TARGET, + (ST25R3916_REG_PASSIVE_TARGET_d_106_ac_a)); + + /* Clear any previous transmission errors (if Reader polled for + * other/unsupported technologies) */ + st25r3916GetInterrupt((ST25R3916_IRQ_MASK_PAR | ST25R3916_IRQ_MASK_CRC | + ST25R3916_IRQ_MASK_ERR2 | + ST25R3916_IRQ_MASK_ERR1)); + + st25r3916EnableInterrupts( + ST25R3916_IRQ_MASK_RXE); /* Start looking for any incoming data */ + break; + + case RFAL_LM_STATE_TARGET_F: + /* Disable Automatic response SENSF_REQ */ + st25r3916SetRegisterBits(ST25R3916_REG_PASSIVE_TARGET, + (ST25R3916_REG_PASSIVE_TARGET_d_212_424_1r)); + break; + + /*******************************************************************************/ + case RFAL_LM_STATE_SLEEP_A: + case RFAL_LM_STATE_SLEEP_B: + case RFAL_LM_STATE_SLEEP_AF: + /* These sleep states have to be set by the rfalListenSleepStart() + * method */ + return RFAL_ERR_REQUEST; + + /*******************************************************************************/ + case RFAL_LM_STATE_CARDEMU_4A: + case RFAL_LM_STATE_CARDEMU_4B: + case RFAL_LM_STATE_TARGET_A: + /* States not handled by the LM, just keep state context */ + break; + + /*******************************************************************************/ + default: + return RFAL_ERR_WRONG_STATE; + } + } while (reSetState); + + gRFAL.Lm.state = newState; + + return ret; +} + +#endif /* RFAL_FEATURE_LISTEN_MODE */ + +/******************************************************************************* + * Wake-Up Mode * + *******************************************************************************/ + +#if RFAL_FEATURE_WAKEUP_MODE + +/*******************************************************************************/ +ReturnCode rfalWakeUpModeStart(const rfalWakeUpConfig *config) { + uint8_t aux; + uint8_t reg; + uint32_t irqs; + + /* Check if RFAL is not initialized */ + if (gRFAL.state < RFAL_STATE_INIT) { + return RFAL_ERR_WRONG_STATE; + } + + /* The Wake-Up procedure is explained in detail in Application Note: AN5320 */ + + if (config == NULL) { + gRFAL.wum.cfg.period = RFAL_WUM_PERIOD_200MS; + gRFAL.wum.cfg.irqTout = false; + gRFAL.wum.cfg.swTagDetect = false; + + gRFAL.wum.cfg.refWU.enabled = + true; /* Obtain WU reference from WU mode, not in Ready Mode */ + gRFAL.wum.cfg.refWU.refDelay = + RFAL_WUM_PERIOD_10MS; /* WU reference after this time in WU mode */ + + gRFAL.wum.cfg.indAmp.enabled = true; + gRFAL.wum.cfg.indPha.enabled = false; + gRFAL.wum.cfg.cap.enabled = false; + + gRFAL.wum.cfg.indAmp.delta = 2U; + gRFAL.wum.cfg.indAmp.fracDelta = 0U; + gRFAL.wum.cfg.indAmp.reference = RFAL_WUM_REFERENCE_AUTO; + gRFAL.wum.cfg.indAmp.autoAvg = false; + +#ifdef ST25R3916 + /*******************************************************************************/ + /* Check if AAT is enabled and if so make use of the SW Tag Detection */ + if (st25r3916IsAATOn()) { + /* Enable SW TD with delta of 1.5 and enable auto average */ + gRFAL.wum.cfg.swTagDetect = true; + gRFAL.wum.cfg.refWU.enabled = false; + gRFAL.wum.cfg.indAmp.delta = 1U; + gRFAL.wum.cfg.indAmp.fracDelta = 2U; + gRFAL.wum.cfg.indAmp.autoAvg = true; + gRFAL.wum.cfg.indAmp.aaWeight = RFAL_WUM_AA_WEIGHT_16; + } +#endif /* ST25R3916 */ + + } else { + gRFAL.wum.cfg = *config; + } + +#ifdef ST25R3916B + /* Check for not supported features */ + if (gRFAL.wum.cfg.cap.enabled) { + return RFAL_ERR_NOTSUPP; + } + + /* Set ST25R3916B Measure Tx delay */ + st25r3916WriteRegister( + ST25R3916_REG_MEAS_TX_DELAY, + (st25r3916IsAATOn() ? RFAL_ST25R3916B_AAT_SETTLE : 0x00)); +#endif /* ST25R3916B */ + + /* Check for valid configuration */ + if (((!gRFAL.wum.cfg.cap.enabled) && (!gRFAL.wum.cfg.indAmp.enabled) && + (!gRFAL.wum.cfg.indPha.enabled)) || + ((gRFAL.wum.cfg.cap.enabled) && + ((gRFAL.wum.cfg.indAmp.enabled) || (gRFAL.wum.cfg.indPha.enabled) || + (gRFAL.wum.cfg.swTagDetect))) || + ((gRFAL.wum.cfg.indAmp.reference > RFAL_WUM_REFERENCE_AUTO) || + (gRFAL.wum.cfg.indPha.reference > RFAL_WUM_REFERENCE_AUTO) || + (gRFAL.wum.cfg.cap.reference > RFAL_WUM_REFERENCE_AUTO)) || + ((gRFAL.wum.cfg.refWU.enabled) && + ((gRFAL.wum.cfg.cap.enabled) || (gRFAL.wum.cfg.swTagDetect) || + (gRFAL.wum.cfg.indAmp.autoAvg) || (gRFAL.wum.cfg.indPha.autoAvg) || + ((gRFAL.wum.cfg.indAmp.enabled) && + (gRFAL.wum.cfg.indAmp.reference != RFAL_WUM_REFERENCE_AUTO)) || + ((gRFAL.wum.cfg.indPha.enabled) && + (gRFAL.wum.cfg.indPha.reference != RFAL_WUM_REFERENCE_AUTO))))) { + return RFAL_ERR_PARAM; + } + + irqs = ST25R3916_IRQ_MASK_NONE; + + /* Disable Tx, Rx, External Field Detector and set default ISO14443A mode */ + st25r3916TxRxOff(); + st25r3916ClrRegisterBits(ST25R3916_REG_OP_CONTROL, + ST25R3916_REG_OP_CONTROL_en_fd_mask); + st25r3916ChangeRegisterBits( + ST25R3916_REG_MODE, + (ST25R3916_REG_MODE_targ | ST25R3916_REG_MODE_om_mask), + (ST25R3916_REG_MODE_targ_init | ST25R3916_REG_MODE_om_iso14443a)); + + /* Set Analog configurations for Wake-up On event */ + rfalSetAnalogConfig( + (RFAL_ANALOG_CONFIG_TECH_CHIP | RFAL_ANALOG_CONFIG_CHIP_WAKEUP_ON)); + + /*******************************************************************************/ + /* Prepare Wake-Up Timer Control Register */ + reg = (uint8_t)(((uint8_t)gRFAL.wum.cfg.period & 0x0FU) + << ST25R3916_REG_WUP_TIMER_CONTROL_wut_shift); + reg |= + (uint8_t)(((uint8_t)gRFAL.wum.cfg.period < (uint8_t)RFAL_WUM_PERIOD_100MS) + ? ST25R3916_REG_WUP_TIMER_CONTROL_wur + : 0x00U); + + if ((gRFAL.wum.cfg.irqTout) || (gRFAL.wum.cfg.swTagDetect)) { + reg |= ST25R3916_REG_WUP_TIMER_CONTROL_wto; + irqs |= ST25R3916_IRQ_MASK_WT; + } + + /* Check if HW Wake-up is to be used or SW Tag detection */ + if (gRFAL.wum.cfg.swTagDetect) { + gRFAL.wum.cfg.indAmp.reference = 0U; + gRFAL.wum.cfg.indPha.reference = 0U; + gRFAL.wum.cfg.cap.reference = 0U; + } else { + /*******************************************************************************/ + /* Check if Inductive Amplitude is to be performed */ + if (gRFAL.wum.cfg.indAmp.enabled) { + aux = (uint8_t)((gRFAL.wum.cfg.indAmp.delta) + << ST25R3916_REG_AMPLITUDE_MEASURE_CONF_am_d_shift); + aux |= (uint8_t)(gRFAL.wum.cfg.indAmp.aaInclMeas + ? ST25R3916_REG_AMPLITUDE_MEASURE_CONF_am_aam + : 0x00U); + aux |= (uint8_t)(((uint8_t)gRFAL.wum.cfg.indAmp.aaWeight + << ST25R3916_REG_AMPLITUDE_MEASURE_CONF_am_aew_shift) & + ST25R3916_REG_AMPLITUDE_MEASURE_CONF_am_aew_mask); + aux |= (uint8_t)(gRFAL.wum.cfg.indAmp.autoAvg + ? ST25R3916_REG_AMPLITUDE_MEASURE_CONF_am_ae + : 0x00U); + + st25r3916WriteRegister(ST25R3916_REG_AMPLITUDE_MEASURE_CONF, aux); + + /* Only need to set the reference if not using Auto Average */ + if ((!gRFAL.wum.cfg.indAmp.autoAvg) && (!gRFAL.wum.cfg.refWU.enabled)) { + if (gRFAL.wum.cfg.indAmp.reference == RFAL_WUM_REFERENCE_AUTO) { + st25r3916MeasureAmplitude(&aux); + gRFAL.wum.cfg.indAmp.reference = aux; + } + st25r3916WriteRegister(ST25R3916_REG_AMPLITUDE_MEASURE_REF, + (uint8_t)gRFAL.wum.cfg.indAmp.reference); + } + + reg |= ST25R3916_REG_WUP_TIMER_CONTROL_wam; + irqs |= ST25R3916_IRQ_MASK_WAM; + } + + /*******************************************************************************/ + /* Check if Inductive Phase is to be performed */ + if (gRFAL.wum.cfg.indPha.enabled) { + aux = (uint8_t)((gRFAL.wum.cfg.indPha.delta) + << ST25R3916_REG_PHASE_MEASURE_CONF_pm_d_shift); + aux |= (uint8_t)(gRFAL.wum.cfg.indPha.aaInclMeas + ? ST25R3916_REG_PHASE_MEASURE_CONF_pm_aam + : 0x00U); + aux |= (uint8_t)(((uint8_t)gRFAL.wum.cfg.indPha.aaWeight + << ST25R3916_REG_PHASE_MEASURE_CONF_pm_aew_shift) & + ST25R3916_REG_PHASE_MEASURE_CONF_pm_aew_mask); + aux |= (uint8_t)(gRFAL.wum.cfg.indPha.autoAvg + ? ST25R3916_REG_PHASE_MEASURE_CONF_pm_ae + : 0x00U); + + st25r3916WriteRegister(ST25R3916_REG_PHASE_MEASURE_CONF, aux); + + /* Only need to set the reference if not using Auto Average */ + if ((!gRFAL.wum.cfg.indPha.autoAvg) && (!gRFAL.wum.cfg.refWU.enabled)) { + if (gRFAL.wum.cfg.indPha.reference == RFAL_WUM_REFERENCE_AUTO) { + st25r3916MeasurePhase(&aux); + gRFAL.wum.cfg.indPha.reference = aux; + } + st25r3916WriteRegister(ST25R3916_REG_PHASE_MEASURE_REF, + (uint8_t)gRFAL.wum.cfg.indPha.reference); + } + + reg |= ST25R3916_REG_WUP_TIMER_CONTROL_wph; + irqs |= ST25R3916_IRQ_MASK_WPH; + } + +#ifdef ST25R3916 + /*******************************************************************************/ + /* Check if Capacitive is to be performed */ + if (gRFAL.wum.cfg.cap.enabled) { + /*******************************************************************************/ + /* Perform Capacitive sensor calibration */ + + /* Disable Oscillator and Field */ + st25r3916ClrRegisterBits( + ST25R3916_REG_OP_CONTROL, + (ST25R3916_REG_OP_CONTROL_en | ST25R3916_REG_OP_CONTROL_tx_en)); + + /* Sensor gain should be configured on Analog Config: + * RFAL_ANALOG_CONFIG_CHIP_WAKEUP_ON */ + + /* Perform calibration procedure */ + st25r3916CalibrateCapacitiveSensor(NULL); + + /*******************************************************************************/ + aux = (uint8_t)((gRFAL.wum.cfg.cap.delta) + << ST25R3916_REG_CAPACITANCE_MEASURE_CONF_cm_d_shift); + aux |= (uint8_t)(gRFAL.wum.cfg.cap.aaInclMeas + ? ST25R3916_REG_CAPACITANCE_MEASURE_CONF_cm_aam + : 0x00U); + aux |= + (uint8_t)(((uint8_t)gRFAL.wum.cfg.cap.aaWeight + << ST25R3916_REG_CAPACITANCE_MEASURE_CONF_cm_aew_shift) & + ST25R3916_REG_CAPACITANCE_MEASURE_CONF_cm_aew_mask); + aux |= (uint8_t)(gRFAL.wum.cfg.cap.autoAvg + ? ST25R3916_REG_CAPACITANCE_MEASURE_CONF_cm_ae + : 0x00U); + + st25r3916WriteRegister(ST25R3916_REG_CAPACITANCE_MEASURE_CONF, aux); + + /* Only need to set the reference if not using Auto Average */ + if ((!gRFAL.wum.cfg.cap.autoAvg) || (gRFAL.wum.cfg.swTagDetect)) { + if (gRFAL.wum.cfg.cap.reference == RFAL_WUM_REFERENCE_AUTO) { + st25r3916MeasureCapacitance(&aux); + gRFAL.wum.cfg.cap.reference = aux; + } + st25r3916WriteRegister(ST25R3916_REG_CAPACITANCE_MEASURE_REF, + (uint8_t)gRFAL.wum.cfg.cap.reference); + } + + reg |= ST25R3916_REG_WUP_TIMER_CONTROL_wcap; + irqs |= ST25R3916_IRQ_MASK_WCAP; + } +#endif /* ST25R3916 */ + } + + /* Disable and clear all interrupts except Wake-Up IRQs */ + st25r3916DisableInterrupts(ST25R3916_IRQ_MASK_ALL); + st25r3916GetInterrupt(irqs); + st25r3916EnableInterrupts(irqs); + + /* Use WUM state to start rfal */ + RFAL_MEMSET( + &gRFAL.wum.info, 0x00, + sizeof( + gRFAL.wum.info)); /* clear info struct to avoid old data being used */ + gRFAL.wum.state = RFAL_WUM_STATE_ENABLED; + gRFAL.state = RFAL_STATE_WUM; + + /*******************************************************************************/ + /* If reference is to be obtained by WU mode, set the WU config so that + * triggers WU IRQ upon first measurement */ + if (gRFAL.wum.cfg.refWU.enabled) { + if (gRFAL.wum.cfg.indAmp.enabled) { + st25r3916WriteRegister(ST25R3916_REG_AMPLITUDE_MEASURE_REF, 0); + st25r3916ChangeRegisterBits( + ST25R3916_REG_AMPLITUDE_MEASURE_CONF, + ST25R3916_REG_AMPLITUDE_MEASURE_CONF_am_d_mask, 0); + gRFAL.wum.refWUTrg = ST25R3916_IRQ_MASK_WAM; + } + + if (gRFAL.wum.cfg.indPha.enabled) { + st25r3916WriteRegister(ST25R3916_REG_PHASE_MEASURE_REF, 0); + st25r3916ChangeRegisterBits(ST25R3916_REG_PHASE_MEASURE_CONF, + ST25R3916_REG_PHASE_MEASURE_CONF_pm_d_mask, + 0); + gRFAL.wum.refWUTrg = ST25R3916_IRQ_MASK_WPH; + } + + /* Set config timing for the ref measurement */ + reg &= ~(ST25R3916_REG_WUP_TIMER_CONTROL_wur | + ST25R3916_REG_WUP_TIMER_CONTROL_wut_mask); + reg |= (uint8_t)(((uint8_t)gRFAL.wum.cfg.refWU.refDelay & 0x0FU) + << ST25R3916_REG_WUP_TIMER_CONTROL_wut_shift); + reg |= (uint8_t)(((uint8_t)gRFAL.wum.cfg.refWU.refDelay < + (uint8_t)RFAL_WUM_PERIOD_100MS) + ? ST25R3916_REG_WUP_TIMER_CONTROL_wur + : 0x00U); + + gRFAL.wum.state = RFAL_WUM_STATE_INITIALIZING; + } + /*******************************************************************************/ + + /* Enable Low Power Wake-Up Mode (Disable: Oscilattor, Tx, Rx and External + * Field Detector) */ + st25r3916WriteRegister(ST25R3916_REG_WUP_TIMER_CONTROL, reg); + st25r3916ChangeRegisterBits( + ST25R3916_REG_OP_CONTROL, + (ST25R3916_REG_OP_CONTROL_en | ST25R3916_REG_OP_CONTROL_rx_en | + ST25R3916_REG_OP_CONTROL_tx_en | ST25R3916_REG_OP_CONTROL_en_fd_mask | + ST25R3916_REG_OP_CONTROL_wu), + ST25R3916_REG_OP_CONTROL_wu); + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +bool rfalWakeUpModeHasWoke(void) { + return (gRFAL.wum.state >= RFAL_WUM_STATE_ENABLED_WOKE); +} + +/*******************************************************************************/ +bool rfalWakeUpModeIsEnabled(void) { + return ((gRFAL.state == RFAL_STATE_WUM) && + (gRFAL.wum.state >= RFAL_WUM_STATE_ENABLED)); +} + +/*******************************************************************************/ +ReturnCode rfalWakeUpModeGetInfo(bool force, rfalWakeUpInfo *info) { + uint8_t aux; + + /* Check if WU mode is running */ + if ((gRFAL.state != RFAL_STATE_WUM) || + (gRFAL.wum.state < RFAL_WUM_STATE_ENABLED)) { + return RFAL_ERR_WRONG_STATE; + } + + /* Check for valid parameters */ + if (info == NULL) { + return RFAL_ERR_PARAM; + } + + /* Clear info structure */ + RFAL_MEMSET(info, 0x00, sizeof(rfalWakeUpInfo)); + + /* Update general information */ + info->irqWut = gRFAL.wum.info.irqWut; + gRFAL.wum.info.irqWut = false; + + /* WUT IRQ is signaled when WUT expires. Delay slightly for the actual + * measurement to be performed */ + if ((info->irqWut) && (!gRFAL.wum.cfg.swTagDetect)) { + platformDelay(1); + } + + if (gRFAL.wum.cfg.indAmp.enabled) { + /* Update measure and reference from current info */ + info->indAmp.reference = gRFAL.wum.cfg.indAmp.reference; + info->indAmp.lastMeas = + gRFAL.wum.info.indAmp.lastMeas; /* For the case of swTagDetect==1 */ + + /* Only retrieve the reference from the device if needed */ + if ((force || (info->irqWut) || (gRFAL.wum.info.indAmp.irqWu)) && + (!gRFAL.wum.cfg.swTagDetect)) { + if (gRFAL.wum.cfg.indAmp.autoAvg) { + st25r3916ReadRegister(ST25R3916_REG_AMPLITUDE_MEASURE_AA_RESULT, &aux); + info->indAmp.reference = aux; + gRFAL.wum.cfg.indAmp.reference = + aux; /* Store last value for subsequenct calls */ + } + st25r3916ReadRegister(ST25R3916_REG_AMPLITUDE_MEASURE_RESULT, + &info->indAmp.lastMeas); + gRFAL.wum.info.indAmp.lastMeas = + info->indAmp.lastMeas; /* Store last value for subsequenct calls */ + } + + /* Update IRQ information and clear flag upon retrieving */ + info->indAmp.irqWu = gRFAL.wum.info.indAmp.irqWu; + gRFAL.wum.info.indAmp.irqWu = false; + } + + if (gRFAL.wum.cfg.indPha.enabled) { + /* Update measure and reference from current info */ + info->indPha.reference = gRFAL.wum.cfg.indPha.reference; + info->indPha.lastMeas = + gRFAL.wum.info.indPha.lastMeas; /* For the case of swTagDetect==1 */ + + /* Only retrieve the reference from the device if needed */ + if ((force || (info->irqWut) || (gRFAL.wum.info.indPha.irqWu)) && + (!gRFAL.wum.cfg.swTagDetect)) { + if (gRFAL.wum.cfg.indPha.autoAvg) { + st25r3916ReadRegister(ST25R3916_REG_PHASE_MEASURE_AA_RESULT, &aux); + info->indPha.reference = aux; + gRFAL.wum.cfg.indPha.reference = + aux; /* Store last value for subsequenct calls */ + } + st25r3916ReadRegister(ST25R3916_REG_PHASE_MEASURE_RESULT, + &info->indPha.lastMeas); + gRFAL.wum.info.indPha.lastMeas = + info->indPha.lastMeas; /* Store last value for subsequenct calls */ + } + + /* Update IRQ information and clear flag upon retrieving */ + info->indPha.irqWu = gRFAL.wum.info.indPha.irqWu; + gRFAL.wum.info.indPha.irqWu = false; + } + +#ifdef ST25R3916 + if (gRFAL.wum.cfg.cap.enabled) { + /* Update measure and reference from current info */ + info->cap.reference = gRFAL.wum.cfg.cap.reference; + info->cap.lastMeas = gRFAL.wum.info.cap.lastMeas; + + /* Retrieve the measurement from the device if needed */ + if (force || (info->irqWut) || (gRFAL.wum.info.cap.irqWu)) { + /* Only retrieve the reference from the device if needed */ + if (gRFAL.wum.cfg.cap.autoAvg) { + st25r3916ReadRegister(ST25R3916_REG_CAPACITANCE_MEASURE_AA_RESULT, + &aux); + info->cap.reference = aux; + gRFAL.wum.cfg.cap.reference = + aux; /* Store last value for subsequenct calls */ + } + st25r3916ReadRegister(ST25R3916_REG_CAPACITANCE_MEASURE_RESULT, + &info->cap.lastMeas); + gRFAL.wum.info.cap.lastMeas = + info->cap.lastMeas; /* Store last value for subsequenct calls */ + } + + /* Update IRQ information and clear flag upon retrieving */ + info->cap.irqWu = gRFAL.wum.info.cap.irqWu; + gRFAL.wum.info.cap.irqWu = false; + } +#endif /* ST25R3916 */ + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +static uint16_t rfalWakeUpModeFilter(uint16_t curRef, uint16_t curVal, + uint8_t weight) { + uint16_t newRef; + + /* Perform the averaging|filter as describded in ST25R3916 DS */ + + /* Avoid signed arithmetics by spliting in two cases */ + if (curVal > curRef) { + newRef = curRef + ((curVal - curRef) / weight); + + /* In order for the reference to converge to final value * + * increment once the diff is smaller that the weight */ + if ((curVal != curRef) && (curRef == newRef)) { + newRef &= 0xFF00U; + newRef += 0x0100U; + } + } else { + newRef = curRef - ((curRef - curVal) / weight); + + /* In order for the reference to converge to final value * + * decrement once the diff is smaller that the weight */ + if ((curVal != curRef) && (curRef == newRef)) { + newRef &= 0xFF00U; + } + } + + return newRef; +} + +/*******************************************************************************/ +static void rfalRunWakeUpModeWorker(void) { + uint32_t irqs; + uint8_t reg; + uint8_t aux; + uint16_t value; + uint16_t delta; + bool woke; + + if (gRFAL.state != RFAL_STATE_WUM) { + return; + } + + switch (gRFAL.wum.state) { + /*******************************************************************************/ + case RFAL_WUM_STATE_ENABLED: + case RFAL_WUM_STATE_ENABLED_WOKE: + + irqs = st25r3916GetInterrupt( + (ST25R3916_IRQ_MASK_WT | ST25R3916_IRQ_MASK_WAM | + ST25R3916_IRQ_MASK_WPH | ST25R3916_IRQ_MASK_WCAP)); + if (irqs == ST25R3916_IRQ_MASK_NONE) { + break; /* No interrupt to process */ + } + + /*******************************************************************************/ + /* Check and mark which measurement(s) cause interrupt */ + if ((irqs & ST25R3916_IRQ_MASK_WAM) != 0U) { + st25r3916ReadRegister(ST25R3916_REG_AMPLITUDE_MEASURE_RESULT, + &aux); /* Debug purposes */ + + gRFAL.wum.info.indAmp.irqWu = true; + gRFAL.wum.state = RFAL_WUM_STATE_ENABLED_WOKE; + } + + if ((irqs & ST25R3916_IRQ_MASK_WPH) != 0U) { + st25r3916ReadRegister(ST25R3916_REG_PHASE_MEASURE_RESULT, + &aux); /* Debug purposes */ + + gRFAL.wum.info.indPha.irqWu = true; + gRFAL.wum.state = RFAL_WUM_STATE_ENABLED_WOKE; + } + +#ifdef ST25R3916 + if ((irqs & ST25R3916_IRQ_MASK_WCAP) != 0U) { + st25r3916ReadRegister(ST25R3916_REG_CAPACITANCE_MEASURE_RESULT, + &aux); /* Debug purposes */ + + gRFAL.wum.info.cap.irqWu = true; + gRFAL.wum.state = RFAL_WUM_STATE_ENABLED_WOKE; + } +#endif /* ST25R3916 */ + + if ((irqs & ST25R3916_IRQ_MASK_WT) != 0U) { + gRFAL.wum.info.irqWut = true; + + /*******************************************************************************/ + if (gRFAL.wum.cfg.swTagDetect) { + woke = false; + + /* Enable Ready mode and wait the settle time if AAT is used */ + if (st25r3916IsAATOn()) { + st25r3916ChangeRegisterBits( + ST25R3916_REG_OP_CONTROL, + (ST25R3916_REG_OP_CONTROL_en | ST25R3916_REG_OP_CONTROL_wu), + ST25R3916_REG_OP_CONTROL_en); + platformDelay(RFAL_ST25R3916_AAT_SETTLE); + } else { + /* Disable wu mode - symmetric to above */ + st25r3916ChangeRegisterBits(ST25R3916_REG_OP_CONTROL, + ST25R3916_REG_OP_CONTROL_wu, 0); + st25r3916OscOn(); + } + + /*******************************************************************************/ + if (gRFAL.wum.cfg.indAmp.enabled) { + /* Perform amplitude measurement */ + st25r3916MeasureAmplitude(®); + + /* Update last measurement info */ + gRFAL.wum.info.indAmp.lastMeas = reg; + + /* Convert inputs to TD format */ + value = rfalConvTDFormat(reg); + delta = rfalConvTDFormat(gRFAL.wum.cfg.indAmp.delta); + delta |= rfalAddFracTDFormat(gRFAL.wum.cfg.indAmp.fracDelta); + + /* Set first measurement as reference */ + if (gRFAL.wum.cfg.indAmp.reference == 0U) { + gRFAL.wum.cfg.indAmp.reference = value; + } + + /* Check if device should be woken */ + if ((value >= (gRFAL.wum.cfg.indAmp.reference + delta)) || + (value <= (gRFAL.wum.cfg.indAmp.reference - delta))) { + woke = true; + gRFAL.wum.info.indAmp.irqWu = true; + gRFAL.wum.state = RFAL_WUM_STATE_ENABLED_WOKE; + /* continue wake-up as for HW */ + } + + /* Update moving reference if enabled */ + if ((gRFAL.wum.cfg.indAmp.autoAvg) && + ((gRFAL.wum.cfg.indAmp.aaInclMeas) || (!woke))) { + gRFAL.wum.cfg.indAmp.reference = rfalWakeUpModeFilter( + gRFAL.wum.cfg.indAmp.reference, value, + (RFAL_WU_MIN_WEIGHT_VAL + << (uint8_t)gRFAL.wum.cfg.indAmp.aaWeight)); + } + } + + /*******************************************************************************/ + if (gRFAL.wum.cfg.indPha.enabled) { + /* Perform Phase measurement */ + st25r3916MeasurePhase(®); + + /* Update last measurement info */ + gRFAL.wum.info.indPha.lastMeas = reg; + + /* Convert inputs to TD format */ + value = rfalConvTDFormat(reg); + delta = rfalConvTDFormat(gRFAL.wum.cfg.indPha.delta); + delta |= rfalAddFracTDFormat(gRFAL.wum.cfg.indPha.fracDelta); + + /* Set first measurement as reference */ + if (gRFAL.wum.cfg.indPha.reference == 0U) { + gRFAL.wum.cfg.indPha.reference = value; + } + + /* Check if device should be woken */ + if ((value >= (gRFAL.wum.cfg.indPha.reference + delta)) || + (value <= (gRFAL.wum.cfg.indPha.reference - delta))) { + woke = true; + gRFAL.wum.info.indPha.irqWu = true; + gRFAL.wum.state = RFAL_WUM_STATE_ENABLED_WOKE; + /* continue wake-up as for HW */ + } + + /* Update moving reference if enabled */ + if ((gRFAL.wum.cfg.indPha.autoAvg) && + ((gRFAL.wum.cfg.indPha.aaInclMeas) || (!woke))) { + gRFAL.wum.cfg.indPha.reference = rfalWakeUpModeFilter( + gRFAL.wum.cfg.indPha.reference, value, + (RFAL_WU_MIN_WEIGHT_VAL + << (uint8_t)gRFAL.wum.cfg.indPha.aaWeight)); + } + } + + /* Re-Enable low power Wake-Up mode for wto to trigger another + * measurement(s) */ + st25r3916ChangeRegisterBits( + ST25R3916_REG_OP_CONTROL, + (ST25R3916_REG_OP_CONTROL_en | ST25R3916_REG_OP_CONTROL_wu), + (ST25R3916_REG_OP_CONTROL_wu)); + } + } + break; + + /*******************************************************************************/ + case RFAL_WUM_STATE_INITIALIZING: + + irqs = st25r3916GetInterrupt(gRFAL.wum.refWUTrg); + if (irqs == ST25R3916_IRQ_MASK_NONE) { + break; /* No interrupt to process */ + } + + /*******************************************************************************/ + /* Check if Reference measurement is to be obtained at first WU pulse */ + if ((gRFAL.wum.cfg.refWU.enabled == true) && + ((irqs & gRFAL.wum.refWUTrg) != 0U)) { + st25r3916ClrRegisterBits(ST25R3916_REG_OP_CONTROL, + ST25R3916_REG_OP_CONTROL_wu); + st25r3916GetInterrupt( + (ST25R3916_IRQ_MASK_WAM | ST25R3916_IRQ_MASK_WPH)); + + /* Set measured value(s) as reference(s) */ + if (gRFAL.wum.cfg.indAmp.enabled) { + st25r3916ReadRegister(ST25R3916_REG_AMPLITUDE_MEASURE_RESULT, &aux); + st25r3916WriteRegister(ST25R3916_REG_AMPLITUDE_MEASURE_REF, aux); + st25r3916ChangeRegisterBits( + ST25R3916_REG_AMPLITUDE_MEASURE_CONF, + ST25R3916_REG_AMPLITUDE_MEASURE_CONF_am_d_mask, + ((gRFAL.wum.cfg.indAmp.delta) + << ST25R3916_REG_AMPLITUDE_MEASURE_CONF_am_d_shift)); + } + + if (gRFAL.wum.cfg.indPha.enabled) { + st25r3916ReadRegister(ST25R3916_REG_PHASE_MEASURE_RESULT, &aux); + st25r3916WriteRegister(ST25R3916_REG_PHASE_MEASURE_REF, aux); + st25r3916ChangeRegisterBits( + ST25R3916_REG_PHASE_MEASURE_CONF, + ST25R3916_REG_PHASE_MEASURE_CONF_pm_d_mask, + ((gRFAL.wum.cfg.indPha.delta) + << ST25R3916_REG_PHASE_MEASURE_CONF_pm_d_shift)); + } + + /* Set WU period and enter WU mode */ + aux = (uint8_t)(((uint8_t)gRFAL.wum.cfg.period & 0x0FU) + << ST25R3916_REG_WUP_TIMER_CONTROL_wut_shift); + aux |= (uint8_t)(((uint8_t)gRFAL.wum.cfg.period < + (uint8_t)RFAL_WUM_PERIOD_100MS) + ? ST25R3916_REG_WUP_TIMER_CONTROL_wur + : 0x00U); + st25r3916ChangeRegisterBits(ST25R3916_REG_WUP_TIMER_CONTROL, + (ST25R3916_REG_WUP_TIMER_CONTROL_wur | + ST25R3916_REG_WUP_TIMER_CONTROL_wut_mask), + aux); + st25r3916SetRegisterBits(ST25R3916_REG_OP_CONTROL, + ST25R3916_REG_OP_CONTROL_wu); + + gRFAL.wum.state = RFAL_WUM_STATE_ENABLED; + return; + } + break; + + /*******************************************************************************/ + default: + /* MISRA 16.4: no empty default statement (a comment being enough) */ + break; + } +} + +/*******************************************************************************/ +ReturnCode rfalWakeUpModeStop(void) { + /* Check if RFAL is in Wake-up mode */ + if (gRFAL.state != RFAL_STATE_WUM) { + return RFAL_ERR_WRONG_STATE; + } + + gRFAL.wum.state = RFAL_WUM_STATE_NOT_INIT; + + /* Disable Wake-Up Mode */ + st25r3916ClrRegisterBits(ST25R3916_REG_OP_CONTROL, + ST25R3916_REG_OP_CONTROL_wu); + st25r3916DisableInterrupts((ST25R3916_IRQ_MASK_WT | ST25R3916_IRQ_MASK_WAM | + ST25R3916_IRQ_MASK_WPH | + ST25R3916_IRQ_MASK_WCAP)); + + /* Stop any ongoing activity */ + st25r3916ExecuteCommand(ST25R3916_CMD_STOP); + + /* Re-Enable External Field Detector as: Automatics */ + st25r3916ChangeRegisterBits(ST25R3916_REG_OP_CONTROL, + ST25R3916_REG_OP_CONTROL_en_fd_mask, + ST25R3916_REG_OP_CONTROL_en_fd_auto_efd); + + /* Re-Enable the Oscillator */ + st25r3916OscOn(); + + /* Set Analog configurations for Wake-up Off event */ + rfalSetAnalogConfig( + (RFAL_ANALOG_CONFIG_TECH_CHIP | RFAL_ANALOG_CONFIG_CHIP_WAKEUP_OFF)); + + return RFAL_ERR_NONE; +} + +#endif /* RFAL_FEATURE_WAKEUP_MODE */ + +/*******************************************************************************/ +ReturnCode rfalWlcPWptMonitorStart(const rfalWakeUpConfig *config) { + RFAL_NO_WARNING(config); + + return RFAL_ERR_NOTSUPP; +} + +/*******************************************************************************/ +ReturnCode rfalWlcPWptMonitorStop(void) { return RFAL_ERR_NOTSUPP; } + +/*******************************************************************************/ +bool rfalWlcPWptIsFodDetected(void) { return false; } + +/*******************************************************************************/ +bool rfalWlcPWptIsStopDetected(void) { return false; } + +/******************************************************************************* + * Low-Power Mode * + *******************************************************************************/ + +#if RFAL_FEATURE_LOWPOWER_MODE + +ReturnCode rfalLowPowerModeStart(rfalLpMode mode) { + /* Check if RFAL is not initialized */ + if (gRFAL.state < RFAL_STATE_INIT) { + return RFAL_ERR_WRONG_STATE; + } + + /* Check if mode is supported */ + if (mode != RFAL_LP_MODE_PD) { + return RFAL_ERR_NOTSUPP; + } + + /* Stop any ongoing activity and set the device in low power by disabling + * oscillator, transmitter, receiver and external field detector */ + st25r3916ExecuteCommand(ST25R3916_CMD_STOP); + st25r3916ClrRegisterBits( + ST25R3916_REG_OP_CONTROL, + (ST25R3916_REG_OP_CONTROL_en | ST25R3916_REG_OP_CONTROL_rx_en | + ST25R3916_REG_OP_CONTROL_wu | ST25R3916_REG_OP_CONTROL_tx_en | + ST25R3916_REG_OP_CONTROL_en_fd_mask)); + + rfalSetAnalogConfig( + (RFAL_ANALOG_CONFIG_TECH_CHIP | RFAL_ANALOG_CONFIG_CHIP_LOWPOWER_ON)); + + gRFAL.state = RFAL_STATE_IDLE; + gRFAL.lpm.isRunning = true; + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalLowPowerModeStop(void) { + ReturnCode ret; + + /* Check if RFAL is on right state */ + if (!gRFAL.lpm.isRunning) { + return RFAL_ERR_WRONG_STATE; + } + + /* Re-enable device */ + RFAL_EXIT_ON_ERR(ret, st25r3916OscOn()); + st25r3916ChangeRegisterBits(ST25R3916_REG_OP_CONTROL, + ST25R3916_REG_OP_CONTROL_en_fd_mask, + ST25R3916_REG_OP_CONTROL_en_fd_auto_efd); + + rfalSetAnalogConfig( + (RFAL_ANALOG_CONFIG_TECH_CHIP | RFAL_ANALOG_CONFIG_CHIP_LOWPOWER_OFF)); + + gRFAL.state = RFAL_STATE_INIT; + gRFAL.lpm.isRunning = false; + return RFAL_ERR_NONE; +} + +#endif /* RFAL_FEATURE_LOWPOWER_MODE */ + +/******************************************************************************* + * RF Chip * + *******************************************************************************/ + +/*******************************************************************************/ +ReturnCode rfalChipWriteReg(uint16_t reg, const uint8_t *values, uint8_t len) { + if (!st25r3916IsRegValid((uint8_t)reg)) { + return RFAL_ERR_PARAM; + } + + return st25r3916WriteMultipleRegisters((uint8_t)reg, values, len); +} + +/*******************************************************************************/ +ReturnCode rfalChipReadReg(uint16_t reg, uint8_t *values, uint8_t len) { + if (!st25r3916IsRegValid((uint8_t)reg)) { + return RFAL_ERR_PARAM; + } + + return st25r3916ReadMultipleRegisters((uint8_t)reg, values, len); +} + +/*******************************************************************************/ +ReturnCode rfalChipExecCmd(uint16_t cmd) { + if (!st25r3916IsCmdValid((uint8_t)cmd)) { + return RFAL_ERR_PARAM; + } + + return st25r3916ExecuteCommand((uint8_t)cmd); +} + +/*******************************************************************************/ +ReturnCode rfalChipWriteTestReg(uint16_t reg, uint8_t value) { + return st25r3916WriteTestRegister((uint8_t)reg, value); +} + +/*******************************************************************************/ +ReturnCode rfalChipReadTestReg(uint16_t reg, uint8_t *value) { + return st25r3916ReadTestRegister((uint8_t)reg, value); +} + +/*******************************************************************************/ +ReturnCode rfalChipChangeRegBits(uint16_t reg, uint8_t valueMask, + uint8_t value) { + if (!st25r3916IsRegValid((uint8_t)reg)) { + return RFAL_ERR_PARAM; + } + + return st25r3916ChangeRegisterBits((uint8_t)reg, valueMask, value); +} + +/*******************************************************************************/ +ReturnCode rfalChipChangeTestRegBits(uint16_t reg, uint8_t valueMask, + uint8_t value) { + st25r3916ChangeTestRegisterBits((uint8_t)reg, valueMask, value); + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalChipSetRFO(uint8_t rfo) { + return st25r3916ChangeRegisterBits(ST25R3916_REG_TX_DRIVER, + ST25R3916_REG_TX_DRIVER_d_res_mask, rfo); +} + +/*******************************************************************************/ +ReturnCode rfalChipGetRFO(uint8_t *result) { + ReturnCode ret; + + ret = st25r3916ReadRegister(ST25R3916_REG_TX_DRIVER, result); + + if (result != NULL) { + (*result) = ((*result) & ST25R3916_REG_TX_DRIVER_d_res_mask); + } + + return ret; +} + +/*******************************************************************************/ +ReturnCode rfalChipSetLMMod(uint8_t mod, uint8_t unmod) { + return st25r3916WriteRegister(ST25R3916_REG_PT_MOD, + (((mod << ST25R3916_REG_PT_MOD_ptm_res_shift) & + ST25R3916_REG_PT_MOD_ptm_res_mask) | + ((unmod & ST25R3916_REG_PT_MOD_pt_res_mask)))); +} + +/*******************************************************************************/ +ReturnCode rfalChipGetLMMod(uint8_t *mod, uint8_t *unmod) { + ReturnCode ret; + uint8_t reg; + + ret = st25r3916ReadRegister(ST25R3916_REG_PT_MOD, ®); + + if (mod != NULL) { + (*mod) = ((reg >> ST25R3916_REG_PT_MOD_ptm_res_shift)); + } + + if (unmod != NULL) { + (*unmod) = ((reg >> ST25R3916_REG_PT_MOD_pt_res_shift) & + ST25R3916_REG_PT_MOD_pt_res_mask); + } + + return ret; +} + +/*******************************************************************************/ +ReturnCode rfalChipMeasureAmplitude(uint8_t *result) { + ReturnCode err; + uint8_t reg_opc, reg_mode, reg_conf1, reg_conf2, reg_auxmod; + + /* Save registers which will be adjusted below */ + st25r3916ReadRegister(ST25R3916_REG_OP_CONTROL, ®_opc); + st25r3916ReadRegister(ST25R3916_REG_MODE, ®_mode); + st25r3916ReadRegister(ST25R3916_REG_RX_CONF1, ®_conf1); + st25r3916ReadRegister(ST25R3916_REG_RX_CONF2, ®_conf2); + st25r3916ReadRegister(ST25R3916_REG_AUX_MOD, ®_auxmod); + + /* Set values as per defaults of DS. These regs/bits influence receiver chain + * and change amplitude */ + /* Doing so achieves an amplitude comparable over a complete polling cylce */ + st25r3916WriteRegister(ST25R3916_REG_OP_CONTROL, + (reg_opc & ~ST25R3916_REG_OP_CONTROL_rx_chn)); + st25r3916WriteRegister( + ST25R3916_REG_MODE, + (ST25R3916_REG_MODE_om_iso14443a | ST25R3916_REG_MODE_targ_init | + ST25R3916_REG_MODE_tr_am_ook | ST25R3916_REG_MODE_nfc_ar_off)); + st25r3916WriteRegister(ST25R3916_REG_RX_CONF1, + (reg_conf1 & ~ST25R3916_REG_RX_CONF1_ch_sel_AM)); + st25r3916WriteRegister(ST25R3916_REG_RX_CONF2, + ((reg_conf2 & ~(ST25R3916_REG_RX_CONF2_demod_mode | + ST25R3916_REG_RX_CONF2_amd_sel)) | + ST25R3916_REG_RX_CONF2_amd_sel_peak)); + +#ifdef ST25R3916B + /* Disable AWS for Amplitude Measurement */ + st25r3916WriteRegister(ST25R3916_REG_AUX_MOD, + (reg_auxmod & ~ST25R3916_REG_AUX_MOD_rgs_am)); +#endif /* ST25R3916B */ + + /* Perform the actual measurement */ + err = st25r3916MeasureAmplitude(result); + + /* Restore values */ + st25r3916WriteRegister(ST25R3916_REG_OP_CONTROL, reg_opc); + st25r3916WriteRegister(ST25R3916_REG_MODE, reg_mode); + st25r3916WriteRegister(ST25R3916_REG_RX_CONF1, reg_conf1); + st25r3916WriteRegister(ST25R3916_REG_RX_CONF2, reg_conf2); + st25r3916WriteRegister(ST25R3916_REG_AUX_MOD, reg_auxmod); + + return err; +} + +/*******************************************************************************/ +ReturnCode rfalChipMeasurePhase(uint8_t *result) { + st25r3916MeasurePhase(result); + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalChipMeasureCapacitance(uint8_t *result) { + st25r3916MeasureCapacitance(result); + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalChipMeasurePowerSupply(uint8_t param, uint8_t *result) { + *result = st25r3916MeasurePowerSupply(param); + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode rfalChipMeasureIQ(int8_t *resI, int8_t *resQ) { + if (resI != NULL) { + (*resI) = 0; + } + + if (resQ != NULL) { + (*resQ) = 0; + } + + return RFAL_ERR_NOTSUPP; +} + +/*******************************************************************************/ +ReturnCode rfalChipMeasureCombinedIQ(uint8_t *result) { + if (result != NULL) { + (*result) = 0U; + } + + return RFAL_ERR_NOTSUPP; +} + +/*******************************************************************************/ +ReturnCode rfalChipSetAntennaMode(bool single, bool rfiox) { + return st25r3916SetAntennaMode(single, rfiox); +} + +/*******************************************************************************/ +extern uint8_t + invalid_size_of_stream_configs[(sizeof(struct st25r3916StreamConfig) == + sizeof(struct iso15693StreamConfig)) + ? 1 + : (-1)]; diff --git a/core/embed/io/nfc/rfal/source/st25r3916/st25r3916.c b/core/embed/io/nfc/rfal/source/st25r3916/st25r3916.c new file mode 100644 index 0000000000..0e7ccef234 --- /dev/null +++ b/core/embed/io/nfc/rfal/source/st25r3916/st25r3916.c @@ -0,0 +1,893 @@ + +/****************************************************************************** + * @attention + * + * COPYRIGHT 2016 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/* + * PROJECT: ST25R3916 firmware + * Revision: + * LANGUAGE: ISO C99 + */ + +/*! \file + * + * \author Gustavo Patricio + * + * \brief ST25R3916 high level interface + * + */ + +/* +****************************************************************************** +* INCLUDES +****************************************************************************** +*/ + +#include "st25r3916.h" +#include "rfal_utils.h" +#include "st25r3916_com.h" +#include "st25r3916_irq.h" +#include "st25r3916_led.h" + +/* + ****************************************************************************** + * ENABLE SWITCH + ****************************************************************************** + */ + +#if !defined(ST25R3916) && !defined(ST25R3916B) +#error \ + "RFAL: Missing ST25R device selection. Please globally define ST25R3916 / ST25R3916B." +#endif /* ST25R3916 | ST25R3916B */ + +/* +****************************************************************************** +* LOCAL DEFINES +****************************************************************************** +*/ + +#define ST25R3916_SUPPLY_THRESHOLD \ + 3600U /*!< Power supply measure threshold between 3.3V or 5V */ +#define ST25R3916_NRT_MAX 0xFFFFU /*!< Max Register value of NRT */ + +#define ST25R3916_TOUT_MEASURE_VDD \ + 100U /*!< Max duration time of Measure Power Supply command Datasheet: 25us \ + */ +#define ST25R3916_TOUT_MEASURE_AMPLITUDE \ + 10U /*!< Max duration time of Measure Amplitude command Datasheet: 25us \ + */ +#define ST25R3916_TOUT_MEASURE_PHASE \ + 10U /*!< Max duration time of Measure Phase command Datasheet: 25us \ + */ +#define ST25R3916_TOUT_MEASURE_CAPACITANCE \ + 10U /*!< Max duration time of Measure Capacitance command Datasheet: 25us \ + */ +#define ST25R3916_TOUT_CALIBRATE_CAP_SENSOR \ + 4U /*!< Max duration Calibrate Capacitive Sensor command Datasheet: 3ms */ +#define ST25R3916_TOUT_CALIBRATE_AWS_RC \ + 10U /*!< Max duration Calibrate RC command Datasheet: 5ms \ + */ +#define ST25R3916_TOUT_ADJUST_REGULATORS \ + 6U /*!< Max duration time of Adjust Regulators command Datasheet: 5ms */ +#define ST25R3916_TOUT_CA \ + 10U /*!< Max duration time of Collision Avoidance command */ + +#define ST25R3916_TEST_REG_PATTERN \ + 0x33U /*!< Register Read Write test pattern used during selftest */ +#define ST25R3916_TEST_WU_TOUT \ + 12U /*!< Timeout used on WU timer during self test */ +#define ST25R3916_TEST_TMR_TOUT 20U /*!< Timeout used during self test */ +#define ST25R3916_TEST_TMR_TOUT_DELTA 2U /*!< Timeout used during self test */ +#define ST25R3916_TEST_TMR_TOUT_8FC \ + (ST25R3916_TEST_TMR_TOUT * 1695U) /*!< Timeout in 8/fc */ + +/* +****************************************************************************** +* LOCAL CONSTANTS +****************************************************************************** +*/ + +/* +****************************************************************************** +* LOCAL VARIABLES +****************************************************************************** +*/ + +static uint32_t gST25R3916NRT_64fcs; + +/* +****************************************************************************** +* LOCAL FUNCTION PROTOTYPES +****************************************************************************** +*/ + +/* + ****************************************************************************** + * LOCAL FUNCTION + ****************************************************************************** + */ + +/* +****************************************************************************** +* GLOBAL FUNCTIONS +****************************************************************************** +*/ + +ReturnCode st25r3916Initialize(void) { + uint16_t vdd_mV; + ReturnCode ret; + +#ifndef RFAL_USE_I2C + /* Ensure a defined chip select state */ + platformSpiDeselect(); +#endif /* RFAL_USE_I2C */ + + /* Set default state on the ST25R3916 */ + st25r3916ExecuteCommand(ST25R3916_CMD_SET_DEFAULT); + +#ifndef RFAL_USE_I2C + /* Increase MISO driving level as SPI can go up to 10MHz */ + st25r3916WriteRegister(ST25R3916_REG_IO_CONF2, + ST25R3916_REG_IO_CONF2_io_drv_lvl); +#endif /* RFAL_USE_I2C */ + + if (!st25r3916CheckChipID(NULL)) { + platformErrorHandle(); + return RFAL_ERR_HW_MISMATCH; + } + + st25r3916InitInterrupts(); + st25r3916ledInit(); + + gST25R3916NRT_64fcs = 0; + +#ifndef RFAL_USE_I2C + /* Enable pull downs on MISO line */ + st25r3916SetRegisterBits( + ST25R3916_REG_IO_CONF2, + (ST25R3916_REG_IO_CONF2_miso_pd1 | ST25R3916_REG_IO_CONF2_miso_pd2)); +#endif /* RFAL_USE_I2C */ + +#ifdef ST25R3916 + /* Disable internal overheat protection */ + st25r3916ChangeTestRegisterBits(0x04, 0x10, 0x10); +#endif /* ST25R3916 */ + +#ifdef ST25R_SELFTEST + /****************************************************************************** + * Check communication interface: + * - write a pattern in a register + * - reads back the register value + * - return RFAL_ERR_IO in case the read value is different + */ + st25r3916WriteRegister(ST25R3916_REG_BIT_RATE, ST25R3916_TEST_REG_PATTERN); + if (!st25r3916CheckReg(ST25R3916_REG_BIT_RATE, + (ST25R3916_REG_BIT_RATE_rxrate_mask | + ST25R3916_REG_BIT_RATE_txrate_mask), + ST25R3916_TEST_REG_PATTERN)) { + platformErrorHandle(); + return RFAL_ERR_IO; + } + + /* Restore default value */ + st25r3916WriteRegister(ST25R3916_REG_BIT_RATE, 0x00); + + /* + * Check IRQ Handling: + * - use the Wake-up timer to trigger an IRQ + * - wait the Wake-up timer interrupt + * - return RFAL_ERR_TIMEOUT when the Wake-up timer interrupt is not received + */ + st25r3916WriteRegister(ST25R3916_REG_WUP_TIMER_CONTROL, + ST25R3916_REG_WUP_TIMER_CONTROL_wur | + ST25R3916_REG_WUP_TIMER_CONTROL_wto); + st25r3916EnableInterrupts(ST25R3916_IRQ_MASK_WT); + st25r3916ExecuteCommand(ST25R3916_CMD_START_WUP_TIMER); + if (st25r3916WaitForInterruptsTimed(ST25R3916_IRQ_MASK_WT, + ST25R3916_TEST_WU_TOUT) == 0U) { + platformErrorHandle(); + return RFAL_ERR_TIMEOUT; + } + st25r3916DisableInterrupts(ST25R3916_IRQ_MASK_WT); + st25r3916WriteRegister(ST25R3916_REG_WUP_TIMER_CONTROL, 0U); + /*******************************************************************************/ +#endif /* ST25R_SELFTEST */ + + /* Enable Oscillator and wait until it gets stable */ + ret = st25r3916OscOn(); + if (ret != RFAL_ERR_NONE) { + platformErrorHandle(); + return ret; + } + +#ifdef ST25R3916B + /* Trigger RC calibration */ + st25r3916ExecuteCommandAndGetResult(ST25R3916_CMD_RC_CAL, + ST25R3916_REG_AWS_RC_CAL, + ST25R3916_TOUT_CALIBRATE_AWS_RC, NULL); +#endif /* ST25R3916B */ + + /* Measure VDD and set sup3V bit according to Power supplied */ + vdd_mV = st25r3916MeasureVoltage(ST25R3916_REG_REGULATOR_CONTROL_mpsv_vdd); + st25r3916ChangeRegisterBits(ST25R3916_REG_IO_CONF2, + ST25R3916_REG_IO_CONF2_sup3V, + ((vdd_mV < ST25R3916_SUPPLY_THRESHOLD) + ? ST25R3916_REG_IO_CONF2_sup3V_3V + : ST25R3916_REG_IO_CONF2_sup3V_5V)); + + /* Make sure Transmitter and Receiver are disabled */ + st25r3916TxRxOff(); + +#ifdef ST25R_SELFTEST_TIMER + /****************************************************************************** + * Check SW timer operation : + * - use the General Purpose timer to measure an amount of time + * - test whether an interrupt is seen when less time was given + * - test whether an interrupt is seen when sufficient time was given + */ + + st25r3916EnableInterrupts(ST25R3916_IRQ_MASK_GPE); + st25r3916SetStartGPTimer((uint16_t)ST25R3916_TEST_TMR_TOUT_8FC, + ST25R3916_REG_TIMER_EMV_CONTROL_gptc_no_trigger); + if (st25r3916WaitForInterruptsTimed( + ST25R3916_IRQ_MASK_GPE, + (ST25R3916_TEST_TMR_TOUT - ST25R3916_TEST_TMR_TOUT_DELTA)) != 0U) { + platformErrorHandle(); + return RFAL_ERR_SYSTEM; + } + + /* Stop all activities to stop the GP timer */ + st25r3916ExecuteCommand(ST25R3916_CMD_STOP); + st25r3916ClearAndEnableInterrupts(ST25R3916_IRQ_MASK_GPE); + st25r3916SetStartGPTimer((uint16_t)ST25R3916_TEST_TMR_TOUT_8FC, + ST25R3916_REG_TIMER_EMV_CONTROL_gptc_no_trigger); + if (st25r3916WaitForInterruptsTimed( + ST25R3916_IRQ_MASK_GPE, + (ST25R3916_TEST_TMR_TOUT + ST25R3916_TEST_TMR_TOUT_DELTA)) == 0U) { + platformErrorHandle(); + return RFAL_ERR_SYSTEM; + } + + /* Stop all activities to stop the GP timer */ + st25r3916ExecuteCommand(ST25R3916_CMD_STOP); + /*******************************************************************************/ +#endif /* ST25R_SELFTEST_TIMER */ + + /* After reset all interrupts are enabled, so disable them at first */ + st25r3916DisableInterrupts(ST25R3916_IRQ_MASK_ALL); + + /* And clear them, just to be sure */ + st25r3916ClearInterrupts(); + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +void st25r3916Deinitialize(void) { + /* Stop any ongoing activity */ + st25r3916ExecuteCommand(ST25R3916_CMD_STOP); + st25r3916DisableInterrupts(ST25R3916_IRQ_MASK_ALL); + + /* Set the device in PD mode */ + st25r3916ClrRegisterBits( + ST25R3916_REG_OP_CONTROL, + (ST25R3916_REG_OP_CONTROL_en | ST25R3916_REG_OP_CONTROL_rx_en | + ST25R3916_REG_OP_CONTROL_wu | ST25R3916_REG_OP_CONTROL_tx_en | + ST25R3916_REG_OP_CONTROL_en_fd_mask)); + + return; +} + +/*******************************************************************************/ +ReturnCode st25r3916OscOn(void) { + /* Check if oscillator is already turned on and stable */ + /* Use ST25R3916_REG_OP_CONTROL_en instead of ST25R3916_REG_AUX_DISPLAY_osc_ok + * to be on the safe side */ + if (!st25r3916CheckReg(ST25R3916_REG_OP_CONTROL, ST25R3916_REG_OP_CONTROL_en, + ST25R3916_REG_OP_CONTROL_en)) { + /* Clear any eventual previous oscillator frequency stable IRQ and enable it + */ + st25r3916ClearAndEnableInterrupts(ST25R3916_IRQ_MASK_OSC); + + /* Clear any oscillator IRQ that was potentially pending on ST25R */ + st25r3916GetInterrupt(ST25R3916_IRQ_MASK_OSC); + + /* Enable oscillator and regulator output */ + st25r3916SetRegisterBits(ST25R3916_REG_OP_CONTROL, + ST25R3916_REG_OP_CONTROL_en); + + /* Wait for the oscillator interrupt */ + st25r3916WaitForInterruptsTimed(ST25R3916_IRQ_MASK_OSC, + ST25R3916_TOUT_OSC_STABLE); + st25r3916DisableInterrupts(ST25R3916_IRQ_MASK_OSC); + } + + if (!st25r3916CheckReg(ST25R3916_REG_AUX_DISPLAY, + ST25R3916_REG_AUX_DISPLAY_osc_ok, + ST25R3916_REG_AUX_DISPLAY_osc_ok)) { + return RFAL_ERR_SYSTEM; + } + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode st25r3916ExecuteCommandAndGetResult(uint8_t cmd, uint8_t resReg, + uint8_t tOut, uint8_t* result) { + /* Clear and enable Direct Command interrupt */ + st25r3916GetInterrupt(ST25R3916_IRQ_MASK_DCT); + st25r3916EnableInterrupts(ST25R3916_IRQ_MASK_DCT); + + st25r3916ExecuteCommand(cmd); + + st25r3916WaitForInterruptsTimed(ST25R3916_IRQ_MASK_DCT, tOut); + st25r3916DisableInterrupts(ST25R3916_IRQ_MASK_DCT); + + /* After execution read out the result if the pointer is not NULL */ + if (result != NULL) { + st25r3916ReadRegister(resReg, result); + } + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +uint8_t st25r3916MeasurePowerSupply(uint8_t mpsv) { + uint8_t result; + + /* Set the source of direct command: Measure Power Supply Voltage */ + st25r3916ChangeRegisterBits(ST25R3916_REG_REGULATOR_CONTROL, + ST25R3916_REG_REGULATOR_CONTROL_mpsv_mask, mpsv); + + /* Execute command: Measure Power Supply Voltage */ + st25r3916ExecuteCommandAndGetResult(ST25R3916_CMD_MEASURE_VDD, + ST25R3916_REG_AD_RESULT, + ST25R3916_TOUT_MEASURE_VDD, &result); + + return result; +} + +/*******************************************************************************/ +uint16_t st25r3916MeasureVoltage(uint8_t mpsv) { + uint8_t result; + uint16_t mV; + + result = st25r3916MeasurePowerSupply(mpsv); + + /* Convert cmd output into mV (each step represents 23.4 mV )*/ + mV = ((uint16_t)result) * 23U; + mV += (((((uint16_t)result) * 4U) + 5U) / 10U); + + return mV; +} + +/*******************************************************************************/ +ReturnCode st25r3916AdjustRegulators(uint16_t* result_mV) { + uint8_t result; + + /* Reset logic and set regulated voltages to be defined by result of Adjust + * Regulators command */ + st25r3916SetRegisterBits(ST25R3916_REG_REGULATOR_CONTROL, + ST25R3916_REG_REGULATOR_CONTROL_reg_s); + st25r3916ClrRegisterBits(ST25R3916_REG_REGULATOR_CONTROL, + ST25R3916_REG_REGULATOR_CONTROL_reg_s); + + /* Execute Adjust regulators cmd and retrieve result */ + st25r3916ExecuteCommandAndGetResult( + ST25R3916_CMD_ADJUST_REGULATORS, ST25R3916_REG_REGULATOR_RESULT, + ST25R3916_TOUT_ADJUST_REGULATORS, &result); + + /* Calculate result in mV */ + result >>= ST25R3916_REG_REGULATOR_RESULT_reg_shift; + + if (result_mV != NULL) { + if (st25r3916CheckReg(ST25R3916_REG_IO_CONF2, ST25R3916_REG_IO_CONF2_sup3V, + ST25R3916_REG_IO_CONF2_sup3V)) { + result -= + ((result > 4U) ? (5U) : 0U); /* In 3.3V mode [0,4] are not used */ + *result_mV = + 2400U; /* Minimum regulated voltage 2.4V in case of 3.3V supply */ + } else { + *result_mV = + 3600U; /* Minimum regulated voltage 3.6V in case of 5V supply */ + } + + *result_mV += + (uint16_t)result * 100U; /* 100mV steps in both 3.3V and 5V supply */ + } + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode st25r3916MeasureAmplitude(uint8_t* result) { + return st25r3916ExecuteCommandAndGetResult( + ST25R3916_CMD_MEASURE_AMPLITUDE, ST25R3916_REG_AD_RESULT, + ST25R3916_TOUT_MEASURE_AMPLITUDE, result); +} + +/*******************************************************************************/ +ReturnCode st25r3916MeasurePhase(uint8_t* result) { + return st25r3916ExecuteCommandAndGetResult( + ST25R3916_CMD_MEASURE_PHASE, ST25R3916_REG_AD_RESULT, + ST25R3916_TOUT_MEASURE_PHASE, result); +} + +/*******************************************************************************/ +ReturnCode st25r3916MeasureCapacitance(uint8_t* result) { +#ifdef ST25R3916B + return RFAL_ERR_NOTSUPP; +#else + return st25r3916ExecuteCommandAndGetResult( + ST25R3916_CMD_MEASURE_CAPACITANCE, ST25R3916_REG_AD_RESULT, + ST25R3916_TOUT_MEASURE_CAPACITANCE, result); +#endif /* ST25R3916B */ +} + +/*******************************************************************************/ +ReturnCode st25r3916CalibrateCapacitiveSensor(uint8_t* result) { +#ifdef ST25R3916B + return RFAL_ERR_NOTSUPP; +#else + ReturnCode ret; + uint8_t res; + + /* Clear Manual calibration values to enable automatic calibration mode */ + st25r3916ClrRegisterBits(ST25R3916_REG_CAP_SENSOR_CONTROL, + ST25R3916_REG_CAP_SENSOR_CONTROL_cs_mcal_mask); + + /* Execute automatic calibration */ + ret = st25r3916ExecuteCommandAndGetResult( + ST25R3916_CMD_CALIBRATE_C_SENSOR, ST25R3916_REG_CAP_SENSOR_RESULT, + ST25R3916_TOUT_CALIBRATE_CAP_SENSOR, &res); + + /* Check wether the calibration was successull */ + if (((res & ST25R3916_REG_CAP_SENSOR_RESULT_cs_cal_end) != + ST25R3916_REG_CAP_SENSOR_RESULT_cs_cal_end) || + ((res & ST25R3916_REG_CAP_SENSOR_RESULT_cs_cal_err) == + ST25R3916_REG_CAP_SENSOR_RESULT_cs_cal_err) || + (ret != RFAL_ERR_NONE)) { + return RFAL_ERR_IO; + } + + if (result != NULL) { + (*result) = (uint8_t)(res >> ST25R3916_REG_CAP_SENSOR_RESULT_cs_cal_shift); + } + + return RFAL_ERR_NONE; +#endif /* ST25R3916B */ +} + +/*******************************************************************************/ +ReturnCode st25r3916SetBitrate(uint8_t txrate, uint8_t rxrate) { + uint8_t reg; + + st25r3916ReadRegister(ST25R3916_REG_BIT_RATE, ®); + if (rxrate != ST25R3916_BR_DO_NOT_SET) { + if (rxrate > ST25R3916_BR_848) { + return RFAL_ERR_PARAM; + } + + reg = (uint8_t)(reg & ~ST25R3916_REG_BIT_RATE_rxrate_mask); /* MISRA 10.3 */ + reg |= rxrate << ST25R3916_REG_BIT_RATE_rxrate_shift; + } + if (txrate != ST25R3916_BR_DO_NOT_SET) { + if (txrate > ST25R3916_BR_6780) { + return RFAL_ERR_PARAM; + } + + reg = (uint8_t)(reg & ~ST25R3916_REG_BIT_RATE_txrate_mask); /* MISRA 10.3 */ + reg |= txrate << ST25R3916_REG_BIT_RATE_txrate_shift; + } + return st25r3916WriteRegister(ST25R3916_REG_BIT_RATE, reg); +} + +/*******************************************************************************/ +ReturnCode st25r3916PerformCollisionAvoidance(uint8_t FieldONCmd, + uint8_t pdThreshold, + uint8_t caThreshold, + uint8_t nTRFW) { + uint8_t treMask; + uint32_t irqs; + ReturnCode err; + + if ((FieldONCmd != ST25R3916_CMD_INITIAL_RF_COLLISION) && + (FieldONCmd != ST25R3916_CMD_RESPONSE_RF_COLLISION_N)) { + return RFAL_ERR_PARAM; + } + + err = RFAL_ERR_INTERNAL; + + /* Check if new thresholds are to be applied */ + if ((pdThreshold != ST25R3916_THRESHOLD_DO_NOT_SET) || + (caThreshold != ST25R3916_THRESHOLD_DO_NOT_SET)) { + treMask = 0; + + if (pdThreshold != ST25R3916_THRESHOLD_DO_NOT_SET) { + treMask |= ST25R3916_REG_FIELD_THRESHOLD_ACTV_trg_mask; + } + + if (caThreshold != ST25R3916_THRESHOLD_DO_NOT_SET) { + treMask |= ST25R3916_REG_FIELD_THRESHOLD_ACTV_rfe_mask; + } + + /* Set Detection Threshold and|or Collision Avoidance Threshold */ + st25r3916ChangeRegisterBits( + ST25R3916_REG_FIELD_THRESHOLD_ACTV, treMask, + (pdThreshold & ST25R3916_REG_FIELD_THRESHOLD_ACTV_trg_mask) | + (caThreshold & ST25R3916_REG_FIELD_THRESHOLD_ACTV_rfe_mask)); + } + + /* Set n x TRFW */ + st25r3916ChangeRegisterBits(ST25R3916_REG_AUX, ST25R3916_REG_AUX_nfc_n_mask, + nTRFW); + + /*******************************************************************************/ + /* Enable and clear CA specific interrupts and execute command */ + st25r3916GetInterrupt((ST25R3916_IRQ_MASK_CAC | ST25R3916_IRQ_MASK_CAT | + ST25R3916_IRQ_MASK_APON)); + st25r3916EnableInterrupts((ST25R3916_IRQ_MASK_CAC | ST25R3916_IRQ_MASK_CAT | + ST25R3916_IRQ_MASK_APON)); + + st25r3916ExecuteCommand(FieldONCmd); + + /*******************************************************************************/ + /* Wait for initial APON interrupt, indicating anticollision avoidance done + * and ST25R3916's field is now on, or a CAC indicating a collision */ + irqs = st25r3916WaitForInterruptsTimed( + (ST25R3916_IRQ_MASK_CAC | ST25R3916_IRQ_MASK_APON), ST25R3916_TOUT_CA); + + if ((ST25R3916_IRQ_MASK_CAC & irqs) != 0U) /* Collision occurred */ + { + err = RFAL_ERR_RF_COLLISION; + } else if ((ST25R3916_IRQ_MASK_APON & irqs) != 0U) { + /* After APON wait for CAT interrupt, indication field was switched on + * minimum guard time has been fulfilled */ + irqs = st25r3916WaitForInterruptsTimed((ST25R3916_IRQ_MASK_CAT), + ST25R3916_TOUT_CA); + + if ((ST25R3916_IRQ_MASK_CAT & irqs) != + 0U) /* No Collision detected, Field On */ + { + err = RFAL_ERR_NONE; + } + } else { + /* MISRA 15.7 - Empty else */ + } + + /* Clear any previous External Field events and disable CA specific interrupts + */ + st25r3916GetInterrupt((ST25R3916_IRQ_MASK_EOF | ST25R3916_IRQ_MASK_EON)); + st25r3916DisableInterrupts((ST25R3916_IRQ_MASK_CAC | ST25R3916_IRQ_MASK_CAT | + ST25R3916_IRQ_MASK_APON)); + + return err; +} + +/*******************************************************************************/ +void st25r3916SetNumTxBits(uint16_t nBits) { + st25r3916WriteRegister(ST25R3916_REG_NUM_TX_BYTES2, + (uint8_t)((nBits >> 0) & 0xFFU)); + st25r3916WriteRegister(ST25R3916_REG_NUM_TX_BYTES1, + (uint8_t)((nBits >> 8) & 0xFFU)); +} + +/*******************************************************************************/ +uint16_t st25r3916GetNumFIFOBytes(void) { + uint8_t reg; + uint16_t result; + + st25r3916ReadRegister(ST25R3916_REG_FIFO_STATUS2, ®); + reg = ((reg & ST25R3916_REG_FIFO_STATUS2_fifo_b_mask) >> + ST25R3916_REG_FIFO_STATUS2_fifo_b_shift); + result = ((uint16_t)reg << 8); + + st25r3916ReadRegister(ST25R3916_REG_FIFO_STATUS1, ®); + result |= (((uint16_t)reg) & 0x00FFU); + + return result; +} + +/*******************************************************************************/ +uint8_t st25r3916GetNumFIFOLastBits(void) { + uint8_t reg; + + st25r3916ReadRegister(ST25R3916_REG_FIFO_STATUS2, ®); + + return ((reg & ST25R3916_REG_FIFO_STATUS2_fifo_lb_mask) >> + ST25R3916_REG_FIFO_STATUS2_fifo_lb_shift); +} + +/*******************************************************************************/ +uint32_t st25r3916GetNoResponseTime(void) { return gST25R3916NRT_64fcs; } + +/*******************************************************************************/ +ReturnCode st25r3916SetNoResponseTime(uint32_t nrt_64fcs) { + ReturnCode err; + uint8_t nrt_step; + uint32_t tmpNRT; + + tmpNRT = nrt_64fcs; /* MISRA 17.8 */ + err = RFAL_ERR_NONE; + + gST25R3916NRT_64fcs = + tmpNRT; /* Store given NRT value in 64/fc into local var */ + nrt_step = + ST25R3916_REG_TIMER_EMV_CONTROL_nrt_step_64fc; /* Set default NRT in steps + of 64/fc */ + + if (tmpNRT > ST25R3916_NRT_MAX) /* Check if the given NRT value fits using + 64/fc steps */ + { + nrt_step = + ST25R3916_REG_TIMER_EMV_CONTROL_nrt_step_4096_fc; /* If not, change NRT + set to 4096/fc */ + tmpNRT = ((tmpNRT + 63U) / 64U); /* Calculate number of steps in 4096/fc */ + + if (tmpNRT > + ST25R3916_NRT_MAX) /* Check if the NRT value fits using 64/fc steps */ + { + tmpNRT = ST25R3916_NRT_MAX; /* Assign the maximum possible */ + err = RFAL_ERR_PARAM; /* Signal parameter error */ + } + gST25R3916NRT_64fcs = (64U * tmpNRT); + } + + /* Set the ST25R3916 NRT step units and the value */ + st25r3916ChangeRegisterBits(ST25R3916_REG_TIMER_EMV_CONTROL, + ST25R3916_REG_TIMER_EMV_CONTROL_nrt_step, + nrt_step); + st25r3916WriteRegister(ST25R3916_REG_NO_RESPONSE_TIMER1, + (uint8_t)(tmpNRT >> 8U)); + st25r3916WriteRegister(ST25R3916_REG_NO_RESPONSE_TIMER2, + (uint8_t)(tmpNRT & 0xFFU)); + + return err; +} + +/*******************************************************************************/ +ReturnCode st25r3916SetStartNoResponseTimer(uint32_t nrt_64fcs) { + ReturnCode err; + + err = st25r3916SetNoResponseTime(nrt_64fcs); + if (err == RFAL_ERR_NONE) { + st25r3916ExecuteCommand(ST25R3916_CMD_START_NO_RESPONSE_TIMER); + } + + return err; +} + +/*******************************************************************************/ +void st25r3916SetGPTime(uint16_t gpt_8fcs) { + st25r3916WriteRegister(ST25R3916_REG_GPT1, (uint8_t)(gpt_8fcs >> 8)); + st25r3916WriteRegister(ST25R3916_REG_GPT2, (uint8_t)(gpt_8fcs & 0xFFU)); +} + +/*******************************************************************************/ +ReturnCode st25r3916SetStartGPTimer(uint16_t gpt_8fcs, uint8_t trigger_source) { + st25r3916SetGPTime(gpt_8fcs); + st25r3916ChangeRegisterBits(ST25R3916_REG_TIMER_EMV_CONTROL, + ST25R3916_REG_TIMER_EMV_CONTROL_gptc_mask, + trigger_source); + + /* If there's no trigger source, start GPT immediately */ + if (trigger_source == ST25R3916_REG_TIMER_EMV_CONTROL_gptc_no_trigger) { + st25r3916ExecuteCommand(ST25R3916_CMD_START_GP_TIMER); + } + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +bool st25r3916CheckChipID(uint8_t* rev) { + uint8_t ID; + + ID = 0; + st25r3916ReadRegister(ST25R3916_REG_IC_IDENTITY, &ID); + + /* Check if IC Identity Register contains ST25R3916's IC type code */ +#if defined(ST25R3916) + if ((ID & ST25R3916_REG_IC_IDENTITY_ic_type_mask) != + ST25R3916_REG_IC_IDENTITY_ic_type_st25r3916) { + return false; + } +#elif defined(ST25R3916B) + if (((ID & ST25R3916_REG_IC_IDENTITY_ic_type_mask) != + ST25R3916_REG_IC_IDENTITY_ic_type_st25r3916B) || + ((ID & ST25R3916_REG_IC_IDENTITY_ic_rev_mask) < 1U)) { + return false; + } +#endif /* ST25R3916 */ + + if (rev != NULL) { + *rev = (ID & ST25R3916_REG_IC_IDENTITY_ic_rev_mask); + } + + return true; +} + +/*******************************************************************************/ +ReturnCode st25r3916GetRegsDump(t_st25r3916Regs* regDump) { + uint8_t regIt; + + if (regDump == NULL) { + return RFAL_ERR_PARAM; + } + + /* Dump Registers on space A */ + for (regIt = ST25R3916_REG_IO_CONF1; regIt <= ST25R3916_REG_IC_IDENTITY; + regIt++) { + st25r3916ReadRegister(regIt, ®Dump->RsA[regIt]); + } + + regIt = 0; + + /* Read non-consecutive Registers on space B */ + st25r3916ReadRegister(ST25R3916_REG_EMD_SUP_CONF, ®Dump->RsB[regIt++]); + st25r3916ReadRegister(ST25R3916_REG_SUBC_START_TIME, ®Dump->RsB[regIt++]); + st25r3916ReadRegister(ST25R3916_REG_P2P_RX_CONF, ®Dump->RsB[regIt++]); + st25r3916ReadRegister(ST25R3916_REG_CORR_CONF1, ®Dump->RsB[regIt++]); + st25r3916ReadRegister(ST25R3916_REG_CORR_CONF2, ®Dump->RsB[regIt++]); + st25r3916ReadRegister(ST25R3916_REG_SQUELCH_TIMER, ®Dump->RsB[regIt++]); + st25r3916ReadRegister(ST25R3916_REG_FIELD_ON_GT, ®Dump->RsB[regIt++]); + st25r3916ReadRegister(ST25R3916_REG_AUX_MOD, ®Dump->RsB[regIt++]); + st25r3916ReadRegister(ST25R3916_REG_TX_DRIVER_TIMING, ®Dump->RsB[regIt++]); + st25r3916ReadRegister(ST25R3916_REG_RES_AM_MOD, ®Dump->RsB[regIt++]); + st25r3916ReadRegister(ST25R3916_REG_TX_DRIVER_STATUS, ®Dump->RsB[regIt++]); + st25r3916ReadRegister(ST25R3916_REG_REGULATOR_RESULT, ®Dump->RsB[regIt++]); + +#ifdef ST25R3916B + st25r3916ReadRegister(ST25R3916_REG_AWS_CONF1, ®Dump->RsB[regIt++]); + st25r3916ReadRegister(ST25R3916_REG_AWS_CONF2, ®Dump->RsB[regIt++]); +#endif /* ST25R3916B */ + + st25r3916ReadRegister(ST25R3916_REG_OVERSHOOT_CONF1, ®Dump->RsB[regIt++]); + st25r3916ReadRegister(ST25R3916_REG_OVERSHOOT_CONF2, ®Dump->RsB[regIt++]); + st25r3916ReadRegister(ST25R3916_REG_UNDERSHOOT_CONF1, ®Dump->RsB[regIt++]); + st25r3916ReadRegister(ST25R3916_REG_UNDERSHOOT_CONF2, ®Dump->RsB[regIt++]); + +#ifdef ST25R3916B + st25r3916ReadRegister(ST25R3916_REG_AWS_TIME1, ®Dump->RsB[regIt++]); + st25r3916ReadRegister(ST25R3916_REG_AWS_TIME2, ®Dump->RsB[regIt++]); + st25r3916ReadRegister(ST25R3916_REG_AWS_TIME3, ®Dump->RsB[regIt++]); + st25r3916ReadRegister(ST25R3916_REG_AWS_TIME4, ®Dump->RsB[regIt++]); + st25r3916ReadRegister(ST25R3916_REG_AWS_TIME5, ®Dump->RsB[regIt++]); + st25r3916ReadRegister(ST25R3916_REG_AWS_RC_CAL, ®Dump->RsB[regIt++]); +#endif /* ST25R3916B */ + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +bool st25r3916IsCmdValid(uint8_t cmd) { + if ((!((cmd >= ST25R3916_CMD_SET_DEFAULT) && + (cmd <= ST25R3916_CMD_RESPONSE_RF_COLLISION_N))) && + (!((cmd >= ST25R3916_CMD_GOTO_SENSE) && + (cmd <= ST25R3916_CMD_GOTO_SLEEP))) && + (!((cmd >= ST25R3916_CMD_MASK_RECEIVE_DATA) && + (cmd <= ST25R3916_CMD_MEASURE_AMPLITUDE))) && + (!((cmd >= ST25R3916_CMD_RESET_RXGAIN) && + (cmd <= ST25R3916_CMD_ADJUST_REGULATORS))) && + (!((cmd >= ST25R3916_CMD_CALIBRATE_DRIVER_TIMING) && + (cmd <= ST25R3916_CMD_START_PPON2_TIMER))) && +#ifdef ST25R3916B + (cmd != ST25R3916_CMD_RC_CAL) && +#endif /* ST25R3916B */ + (cmd != ST25R3916_CMD_SPACE_B_ACCESS) && + (cmd != ST25R3916_CMD_STOP_NRT)) { + return false; + } + return true; +} + +/*******************************************************************************/ +ReturnCode st25r3916StreamConfigure( + const struct st25r3916StreamConfig* config) { + uint8_t smd; + uint8_t mode; + + smd = 0; + + if (config->useBPSK != 0U) { + mode = ST25R3916_REG_MODE_om_bpsk_stream; + if ((config->din < 2U) || (config->din > 4U)) /* not in fc/4 .. fc/16 */ + { + return RFAL_ERR_PARAM; + } + smd |= ((4U - config->din) << ST25R3916_REG_STREAM_MODE_scf_shift); + } else { + mode = ST25R3916_REG_MODE_om_subcarrier_stream; + if ((config->din < 3U) || (config->din > 6U)) /* not in fc/8 .. fc/64 */ + { + return RFAL_ERR_PARAM; + } + smd |= ((6U - config->din) << ST25R3916_REG_STREAM_MODE_scf_shift); + if (config->report_period_length == 0U) { + return RFAL_ERR_PARAM; + } + } + + if ((config->dout < 1U) || (config->dout > 7U)) /* not in fc/2 .. fc/128 */ + { + return RFAL_ERR_PARAM; + } + smd |= (7U - config->dout) << ST25R3916_REG_STREAM_MODE_stx_shift; + + if (config->report_period_length > 3U) { + return RFAL_ERR_PARAM; + } + smd |= (config->report_period_length << ST25R3916_REG_STREAM_MODE_scp_shift); + + st25r3916WriteRegister(ST25R3916_REG_STREAM_MODE, smd); + st25r3916ChangeRegisterBits(ST25R3916_REG_MODE, ST25R3916_REG_MODE_om_mask, + mode); + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode st25r3916GetRSSI(uint16_t* amRssi, uint16_t* pmRssi) { + /*******************************************************************************/ + /* MISRA 8.9 An object should be defined at block scope if its identifier only + * appears in a single function */ + /*< ST25R3916 RSSI Display Reg values: 0 1 2 3 4 5 6 7 8 + * 9 a b c d e f */ + static const uint16_t st25r3916Rssi2mV[16] = { + 0, 20, 27, 37, 52, 72, 99, 136, 190, 262, 357, 500, 686, 950, 1150, 1150}; + + /* ST25R3916 2/3 stage gain reduction [dB] 0 0 0 0 0 3 + * 6 9 12 15 18 na na na na na */ + static const uint16_t st25r3916Gain2Percent[16] = { + 100, 100, 100, 100, 100, 141, 200, 281, 398, 562, 794, 1, 1, 1, 1, 1}; + /*******************************************************************************/ + + uint8_t rssi; + uint8_t gainRed; + + st25r3916ReadRegister(ST25R3916_REG_RSSI_RESULT, &rssi); + st25r3916ReadRegister(ST25R3916_REG_GAIN_RED_STATE, &gainRed); + + if (amRssi != NULL) { + *amRssi = + (uint16_t)(((uint32_t)st25r3916Rssi2mV[( + rssi >> ST25R3916_REG_RSSI_RESULT_rssi_am_shift)] * + (uint32_t)st25r3916Gain2Percent[( + gainRed >> ST25R3916_REG_GAIN_RED_STATE_gs_am_shift)]) / + 100U); + } + + if (pmRssi != NULL) { + *pmRssi = + (uint16_t)(((uint32_t)st25r3916Rssi2mV[( + rssi & ST25R3916_REG_RSSI_RESULT_rssi_pm_mask)] * + (uint32_t)st25r3916Gain2Percent[( + gainRed & ST25R3916_REG_GAIN_RED_STATE_gs_pm_mask)]) / + 100U); + } + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode st25r3916SetAntennaMode(bool single, bool rfiox) { + uint8_t val; + + val = 0U; + val |= ((single) ? ST25R3916_REG_IO_CONF1_single : 0U); + val |= ((rfiox) ? ST25R3916_REG_IO_CONF1_rfo2 : 0U); + + st25r3916ChangeRegisterBits( + ST25R3916_REG_IO_CONF1, + (ST25R3916_REG_IO_CONF1_single | ST25R3916_REG_IO_CONF1_rfo2), val); + return RFAL_ERR_NONE; +} diff --git a/core/embed/io/nfc/rfal/source/st25r3916/st25r3916.h b/core/embed/io/nfc/rfal/source/st25r3916/st25r3916.h new file mode 100644 index 0000000000..474e172348 --- /dev/null +++ b/core/embed/io/nfc/rfal/source/st25r3916/st25r3916.h @@ -0,0 +1,705 @@ + +/****************************************************************************** + * @attention + * + * COPYRIGHT 2016 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/* + * PROJECT: ST25R3916 firmware + * Revision: + * LANGUAGE: ISO C99 + */ + +/*! \file + * + * \author Gustavo Patricio + * + * \brief ST25R3916 high level interface + * + * + * \addtogroup RFAL + * @{ + * + * \addtogroup RFAL-HAL + * \brief RFAL Hardware Abstraction Layer + * @{ + * + * \addtogroup ST25R3916 + * \brief RFAL ST25R3916 Driver + * @{ + * + * \addtogroup ST25R3916_Driver + * \brief RFAL ST25R3916 Driver + * @{ + * + */ + +#ifndef ST25R3916_H +#define ST25R3916_H + +/* +****************************************************************************** +* INCLUDES +****************************************************************************** +*/ +#include "rfal_platform.h" +#include "rfal_utils.h" +#include "st25r3916_com.h" + +/* +****************************************************************************** +* GLOBAL DATATYPES +****************************************************************************** +*/ + +/*! Struct to represent all regs on ST25R3916 */ +typedef struct { + uint8_t RsA[(ST25R3916_REG_IC_IDENTITY + + 1U)]; /*!< Registers contained on ST25R3916 space A (Rs-A) */ + uint8_t RsB[ST25R3916_SPACE_B_REG_LEN]; /*!< Registers contained on ST25R3916 + space B (Rs-B) */ +} t_st25r3916Regs; + +/*! Parameters how the stream mode should work */ +struct st25r3916StreamConfig { + uint8_t useBPSK; /*!< 0: subcarrier, 1:BPSK */ + uint8_t din; /*!< Divider for the in subcarrier frequency: fc/2^din */ + uint8_t dout; /*!< Divider for the in subcarrier frequency fc/2^dout */ + uint8_t report_period_length; /*!< Length of the reporting period + 2^report_period_length*/ +}; + +/* +****************************************************************************** +* GLOBAL DEFINES +****************************************************************************** +*/ + +/* ST25R3916 direct commands */ +#define ST25R3916_CMD_SET_DEFAULT \ + 0xC1U /*!< Puts the chip in default state (same as after power-up) */ +#define ST25R3916_CMD_STOP \ + 0xC2U /*!< Stops all activities and clears FIFO */ +#define ST25R3916_CMD_TRANSMIT_WITH_CRC \ + 0xC4U /*!< Transmit with CRC */ +#define ST25R3916_CMD_TRANSMIT_WITHOUT_CRC \ + 0xC5U /*!< Transmit without CRC */ +#define ST25R3916_CMD_TRANSMIT_REQA \ + 0xC6U /*!< Transmit REQA */ +#define ST25R3916_CMD_TRANSMIT_WUPA \ + 0xC7U /*!< Transmit WUPA */ +#define ST25R3916_CMD_INITIAL_RF_COLLISION \ + 0xC8U /*!< NFC transmit with Initial RF Collision Avoidance */ +#define ST25R3916_CMD_RESPONSE_RF_COLLISION_N \ + 0xC9U /*!< NFC transmit with Response RF Collision Avoidance */ +#define ST25R3916_CMD_GOTO_SENSE \ + 0xCDU /*!< Passive target logic to Sense/Idle state */ +#define ST25R3916_CMD_GOTO_SLEEP \ + 0xCEU /*!< Passive target logic to Sleep/Halt state */ +#define ST25R3916_CMD_MASK_RECEIVE_DATA \ + 0xD0U /*!< Mask receive data */ +#define ST25R3916_CMD_UNMASK_RECEIVE_DATA \ + 0xD1U /*!< Unmask receive data */ +#define ST25R3916_CMD_AM_MOD_STATE_CHANGE \ + 0xD2U /*!< AM Modulation state change */ +#define ST25R3916_CMD_MEASURE_AMPLITUDE \ + 0xD3U /*!< Measure singal amplitude on RFI inputs */ +#define ST25R3916_CMD_RESET_RXGAIN \ + 0xD5U /*!< Reset RX Gain */ +#define ST25R3916_CMD_ADJUST_REGULATORS \ + 0xD6U /*!< Adjust regulators */ +#define ST25R3916_CMD_CALIBRATE_DRIVER_TIMING \ + 0xD8U /*!< Starts the sequence to adjust the driver timing */ +#define ST25R3916_CMD_MEASURE_PHASE \ + 0xD9U /*!< Measure phase between RFO and RFI signal */ +#define ST25R3916_CMD_CLEAR_RSSI \ + 0xDAU /*!< Clear RSSI bits and restart the measurement */ +#define ST25R3916_CMD_CLEAR_FIFO \ + 0xDBU /*!< Clears FIFO, Collision and IRQ status */ +#define ST25R3916_CMD_TRANSPARENT_MODE \ + 0xDCU /*!< Transparent mode */ +#ifdef ST25R3916 +#define ST25R3916_CMD_CALIBRATE_C_SENSOR \ + 0xDDU /*!< Calibrate the capacitive sensor */ +#define ST25R3916_CMD_MEASURE_CAPACITANCE \ + 0xDEU /*!< Measure capacitance */ +#endif /* ST25R3916 */ +#define ST25R3916_CMD_MEASURE_VDD \ + 0xDFU /*!< Measure power supply voltage */ +#define ST25R3916_CMD_START_GP_TIMER \ + 0xE0U /*!< Start the general purpose timer */ +#define ST25R3916_CMD_START_WUP_TIMER \ + 0xE1U /*!< Start the wake-up timer */ +#define ST25R3916_CMD_START_MASK_RECEIVE_TIMER \ + 0xE2U /*!< Start the mask-receive timer */ +#define ST25R3916_CMD_START_NO_RESPONSE_TIMER \ + 0xE3U /*!< Start the no-response timer */ +#define ST25R3916_CMD_START_PPON2_TIMER \ + 0xE4U /*!< Start PPon2 timer */ +#define ST25R3916_CMD_STOP_NRT \ + 0xE8U /*!< Stop No Response Timer */ +#ifdef ST25R3916B +#define ST25R3916_CMD_RC_CAL \ + 0xEAU /*!< Trigger RC calibration */ +#endif /* ST25R3916B */ +#define ST25R3916_CMD_SPACE_B_ACCESS \ + 0xFBU /*!< Enable R/W access to the space B registers */ +#define ST25R3916_CMD_TEST_ACCESS \ + 0xFCU /*!< Enable R/W access to the test registers */ + +#define ST25R3916_THRESHOLD_DO_NOT_SET \ + 0xFFU /*!< Indicates not to change this Threshold */ + +#define ST25R3916_BR_DO_NOT_SET \ + 0xFFU /*!< Indicates not to change this Bit Rate */ +#define ST25R3916_BR_106 \ + 0x00U /*!< ST25R3916 Bit Rate 106 kbit/s (fc/128) */ +#define ST25R3916_BR_212 \ + 0x01U /*!< ST25R3916 Bit Rate 212 kbit/s (fc/64) */ +#define ST25R3916_BR_424 \ + 0x02U /*!< ST25R3916 Bit Rate 424 kbit/s (fc/32) */ +#define ST25R3916_BR_848 \ + 0x03U /*!< ST25R3916 Bit Rate 848 kbit/s (fc/16) */ +#define ST25R3916_BR_1695 \ + 0x04U /*!< ST25R3916 Bit Rate 1696 kbit/s (fc/8) */ +#define ST25R3916_BR_3390 \ + 0x05U /*!< ST25R3916 Bit Rate 3390 kbit/s (fc/4) */ +#define ST25R3916_BR_6780 \ + 0x07U /*!< ST25R3916 Bit Rate 6780 kbit/s (fc/2) */ + +#define ST25R3916_FIFO_DEPTH \ + 512U /*!< Depth of FIFO */ +#define ST25R3916_TOUT_OSC_STABLE \ + 10U /*!< Max timeout for Oscillator to get stable DS: 700us */ + +/* +****************************************************************************** +* GLOBAL MACROS +****************************************************************************** +*/ + +/*! Enables the Transmitter (Field On) and Receiver */ +#define st25r3916TxRxOn() \ + st25r3916SetRegisterBits( \ + ST25R3916_REG_OP_CONTROL, \ + (ST25R3916_REG_OP_CONTROL_rx_en | ST25R3916_REG_OP_CONTROL_tx_en)) + +/*! Disables the Transmitter (Field Off) and Receiver */ +#define st25r3916TxRxOff() \ + st25r3916ClrRegisterBits( \ + ST25R3916_REG_OP_CONTROL, \ + (ST25R3916_REG_OP_CONTROL_rx_en | ST25R3916_REG_OP_CONTROL_tx_en)) + +/*! Disables the Transmitter (Field Off) */ +#define st25r3916TxOff() \ + st25r3916ClrRegisterBits(ST25R3916_REG_OP_CONTROL, \ + ST25R3916_REG_OP_CONTROL_tx_en) + +/*! Checks if General Purpose Timer is still running by reading gpt_on flag */ +#define st25r3916IsGPTRunning() \ + st25r3916CheckReg(ST25R3916_REG_NFCIP1_BIT_RATE, \ + ST25R3916_REG_NFCIP1_BIT_RATE_gpt_on, \ + ST25R3916_REG_NFCIP1_BIT_RATE_gpt_on) + +/*! Checks if External Filed is detected by reading ST25R3916 External Field + * Detector output */ +#define st25r3916IsExtFieldOn() \ + st25r3916CheckReg(ST25R3916_REG_AUX_DISPLAY, \ + ST25R3916_REG_AUX_DISPLAY_efd_o, \ + ST25R3916_REG_AUX_DISPLAY_efd_o) + +/*! Checks if Transmitter is enabled (Field On) */ +#define st25r3916IsTxEnabled() \ + st25r3916CheckReg(ST25R3916_REG_OP_CONTROL, ST25R3916_REG_OP_CONTROL_tx_en, \ + ST25R3916_REG_OP_CONTROL_tx_en) + +/*! Checks if NRT is in EMV mode */ +#define st25r3916IsNRTinEMV() \ + st25r3916CheckReg(ST25R3916_REG_TIMER_EMV_CONTROL, \ + ST25R3916_REG_TIMER_EMV_CONTROL_nrt_emv, \ + ST25R3916_REG_TIMER_EMV_CONTROL_nrt_emv_on) + +/*! Checks if last FIFO byte is complete */ +#define st25r3916IsLastFIFOComplete() \ + st25r3916CheckReg(ST25R3916_REG_FIFO_STATUS2, \ + ST25R3916_REG_FIFO_STATUS2_fifo_lb_mask, 0) + +/*! Checks if the Oscillator is enabled */ +#define st25r3916IsOscOn() \ + st25r3916CheckReg(ST25R3916_REG_OP_CONTROL, ST25R3916_REG_OP_CONTROL_en, \ + ST25R3916_REG_OP_CONTROL_en) + +/*! Checks if the AAT is enabled */ +#define st25r3916IsAATOn() \ + st25r3916CheckReg(ST25R3916_REG_IO_CONF2, ST25R3916_REG_IO_CONF2_aat_en, \ + ST25R3916_REG_IO_CONF2_aat_en) + +/* +****************************************************************************** +* GLOBAL FUNCTION PROTOTYPES +****************************************************************************** +*/ + +/*! + ***************************************************************************** + * \brief Initialise ST25R3916 driver + * + * This function initialises the ST25R3916 driver. + * + * \return RFAL_ERR_NONE : Operation successful + * \return RFAL_ERR_HW_MISMATCH : Expected HW do not match or communication + *error \return RFAL_ERR_IO : Error during communication selftest. + *Check communication interface \return RFAL_ERR_TIMEOUT : Timeout during + *IRQ selftest. Check IRQ handling \return RFAL_ERR_SYSTEM : Failure + *during oscillator activation or timer error + * + ***************************************************************************** + */ +ReturnCode st25r3916Initialize(void); + +/*! + ***************************************************************************** + * \brief Deinitialize ST25R3916 driver + * + * Calling this function deinitializes the ST25R3916 driver. + * + ***************************************************************************** + */ +void st25r3916Deinitialize(void); + +/*! + ***************************************************************************** + * \brief Turn on Oscillator and Regulator + * + * This function turn on oscillator and regulator and waits for the + * oscillator to become stable + * + * \return RFAL_ERR_SYSTEM : Failure dusring Oscillator activation + * \return RFAL_ERR_NONE : No error, Oscillator is active and stable, + *Regulator is on + * + ***************************************************************************** + */ +ReturnCode st25r3916OscOn(void); + +/*! + ***************************************************************************** + * \brief Sets the bitrate + * + * This function sets the bitrates for rx and tx + * + * \param txrate : speed is 2^txrate * 106 kb/s + * 0xff : don't set txrate (ST25R3916_BR_DO_NOT_SET) + * \param rxrate : speed is 2^rxrate * 106 kb/s + * 0xff : don't set rxrate (ST25R3916_BR_DO_NOT_SET) + * + * \return RFAL_ERR_PARAM: At least one bit rate was invalid + * \return RFAL_ERR_NONE : No error, both bit rates were set + * + ***************************************************************************** + */ +ReturnCode st25r3916SetBitrate(uint8_t txrate, uint8_t rxrate); + +/*! + ***************************************************************************** + * \brief Adjusts supply regulators according to the current supply voltage + * + * This function the power level is measured in maximum load conditions and + * the regulated voltage reference is set to 250mV below this level. + * Execution of this function lasts arround 5ms. + * + * The regulated voltages will be set to the result of Adjust Regulators + * + * \param [out] result_mV : Result of calibration in milliVolts + * + * \return RFAL_ERR_IO : Error during communication with ST25R3916 + * \return RFAL_ERR_NONE : No error + * + ***************************************************************************** + */ +ReturnCode st25r3916AdjustRegulators(uint16_t* result_mV); + +/*! + ***************************************************************************** + * \brief Measure Amplitude + * + * This function measures the amplitude on the RFI inputs and stores the + * result in parameter \a result. + * + * \param[out] result: result of RF measurement. + * + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_NONE : No error + * + ***************************************************************************** + */ +ReturnCode st25r3916MeasureAmplitude(uint8_t* result); + +/*! + ***************************************************************************** + * \brief Measure Power Supply + * + * This function executes Measure Power Supply and returns the raw value + * + * \param[in] mpsv : one of ST25R3916_REG_REGULATOR_CONTROL_mpsv_vdd + * ST25R3916_REG_REGULATOR_CONTROL_mpsv_vdd_rf + * ST25R3916_REG_REGULATOR_CONTROL_mpsv_vdd_a + * ST25R3916_REG_REGULATOR_CONTROL_mpsv_vdd_d + * ST25R3916_REG_REGULATOR_CONTROL_mpsv_vdd_am + * + * \return the measured voltage in raw format. + * + ***************************************************************************** + */ +uint8_t st25r3916MeasurePowerSupply(uint8_t mpsv); + +/*! + ***************************************************************************** + * \brief Measure Voltage + * + * This function measures the voltage on one of VDD and VDD_* and returns + * the result in mV + * + * \param[in] mpsv : one of ST25R3916_REG_REGULATOR_CONTROL_mpsv_vdd + * ST25R3916_REG_REGULATOR_CONTROL_mpsv_vdd_rf + * ST25R3916_REG_REGULATOR_CONTROL_mpsv_vdd_a + * ST25R3916_REG_REGULATOR_CONTROL_mpsv_vdd_d + * or ST25R3916_REG_REGULATOR_CONTROL_mpsv_vdd_am + * + * \return the measured voltage in mV + * + ***************************************************************************** + */ +uint16_t st25r3916MeasureVoltage(uint8_t mpsv); + +/*! + ***************************************************************************** + * \brief Measure Phase + * + * This function performs a Phase measurement. + * The result is stored in the \a result parameter. + * + * \param[out] result: 8 bit long result of the measurement. + * + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_NONE : No error + * + ***************************************************************************** + */ +ReturnCode st25r3916MeasurePhase(uint8_t* result); + +/*! + ***************************************************************************** + * \brief Measure Capacitance + * + * This function performs the capacitance measurement and stores the + * result in parameter \a result. + * + * \param[out] result: 8 bit long result of RF measurement. + * + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_NONE : No error + * + ***************************************************************************** + */ +ReturnCode st25r3916MeasureCapacitance(uint8_t* result); + +/*! + ***************************************************************************** + * \brief Calibrates Capacitive Sensor + * + * This function performs automatic calibration of the capacitive sensor + * and stores the result in parameter \a result. + * + * \warning To avoid interference with Xtal oscillator and reader magnetic + * field, it is strongly recommended to perform calibration + * in Power-down mode only. + * This method does not modify the Oscillator nor transmitter state, + * these should be configured before by user. + * + * \param[out] result: 5 bit long result of the calibration. + * Binary weighted, step 0.1 pF, max 3.1 pF + * + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_IO : The calibration was not successful + * \return RFAL_ERR_NONE : No error + * + ***************************************************************************** + */ +ReturnCode st25r3916CalibrateCapacitiveSensor(uint8_t* result); + +/*! + ***************************************************************************** + * \brief Get NRT time + * + * This returns the last value set on the NRT + * + * \warning it does not read chip register, just the sw var that contains the + * last value set before + * + * \return the value of the NRT in 64/fc + */ +uint32_t st25r3916GetNoResponseTime(void); + +/*! + ***************************************************************************** + * \brief Set NRT time + * + * This function sets the No Response Time with the given value + * + * \param [in] nrt_64fcs : no response time in steps of 64/fc (4.72us) + * + * \return RFAL_ERR_PARAM : Invalid parameter (time is too large) + * \return RFAL_ERR_NONE : No error + * + ***************************************************************************** + */ +ReturnCode st25r3916SetNoResponseTime(uint32_t nrt_64fcs); + +/*! + ***************************************************************************** + * \brief Set and Start NRT + * + * This function sets the No Response Time with the given value and + * immediately starts it + * Used when needs to add more time before timeout without performing Tx + * + * \param [in] nrt_64fcs : no response time in steps of 64/fc (4.72us) + * + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_NONE : No error + * + ***************************************************************************** + */ +ReturnCode st25r3916SetStartNoResponseTimer(uint32_t nrt_64fcs); + +/*! + ***************************************************************************** + * \brief Set GPT time + * + * This function sets the General Purpose Timer time registers + * + * \param [in] gpt_8fcs : general purpose timer timeout in steps of 8/fc + *(590ns) + * + ***************************************************************************** + */ +void st25r3916SetGPTime(uint16_t gpt_8fcs); + +/*! + ***************************************************************************** + * \brief Set and Start GPT + * + * This function sets the General Purpose Timer with the given timeout and + * immediately starts it ONLY if the trigger source is not set to none. + * + * \param [in] gpt_8fcs : general purpose timer timeout in steps of8/fc + *(590ns) \param [in] trigger_source : no trigger, start of Rx, end of Rx, end + *of Tx in NFC mode + * + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_NONE : No error + * + ***************************************************************************** + */ +ReturnCode st25r3916SetStartGPTimer(uint16_t gpt_8fcs, uint8_t trigger_source); + +/*! + ***************************************************************************** + * \brief Sets the number Tx Bits + * + * Sets ST25R3916 internal registers with correct number of complete bytes and + * bits to be sent + * + * \param [in] nBits : number of bits to be set/transmitted + * + ***************************************************************************** + */ +void st25r3916SetNumTxBits(uint16_t nBits); + +/*! + ***************************************************************************** + * \brief Get amount of bytes in FIFO + * + * Gets the number of bytes currently in the FIFO + * + * \return the number of bytes currently in the FIFO + * + ***************************************************************************** + */ +uint16_t st25r3916GetNumFIFOBytes(void); + +/*! + ***************************************************************************** + * \brief Get amount of bits of the last FIFO byte if incomplete + * + * Gets the number of bits of the last FIFO byte if incomplete + * + * \return the number of bits of the last FIFO byte if incomplete, 0 if + * the last byte is complete + * + ***************************************************************************** + */ +uint8_t st25r3916GetNumFIFOLastBits(void); + +/*! + ***************************************************************************** + * \brief Perform Collision Avoidance + * + * Performs Collision Avoidance with the given threshold and with the + * n number of TRFW + * + * \param[in] FieldONCmd : Field ON command to be executed + *ST25R3916_CMD_INITIAL_RF_COLLISION or ST25R3916_CMD_RESPONSE_RF_COLLISION_N + * \param[in] pdThreshold : Peer Detection Threshold + *(ST25R3916_REG_FIELD_THRESHOLD_trg_xx) 0xff : don't set Threshold + *(ST25R3916_THRESHOLD_DO_NOT_SET) \param[in] caThreshold : Collision Avoidance + *Threshold (ST25R3916_REG_FIELD_THRESHOLD_rfe_xx) 0xff : don't set Threshold + *(ST25R3916_THRESHOLD_DO_NOT_SET) \param[in] nTRFW : Number of TRFW + * + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_RF_COLLISION : Collision detected + * \return RFAL_ERR_NONE : No collision detected + * + ***************************************************************************** + */ +ReturnCode st25r3916PerformCollisionAvoidance(uint8_t FieldONCmd, + uint8_t pdThreshold, + uint8_t caThreshold, + uint8_t nTRFW); + +/*! + ***************************************************************************** + * \brief Check Identity + * + * Checks if the chip ID is as expected. + * + * 5 bit IC type code for ST25R3916: 00101 + * The 3 lsb contain the IC revision code + * + * \param[out] rev : the IC revision code + * + * \return true when IC type is as expected + * \return false otherwise + */ +bool st25r3916CheckChipID(uint8_t* rev); + +/*! + ***************************************************************************** + * \brief Retrieves all internal registers from ST25R3916 + * + * \param[out] regDump : pointer to the struct/buffer where the reg dump + * will be written + * + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode st25r3916GetRegsDump(t_st25r3916Regs* regDump); + +/*! + ***************************************************************************** + * \brief Check if command is valid + * + * Checks if the given command is a valid ST25R3916 command + * + * \param[in] cmd: Command to check + * + * \return true if is a valid command + * \return false otherwise + * + ***************************************************************************** + */ +bool st25r3916IsCmdValid(uint8_t cmd); + +/*! + ***************************************************************************** + * \brief Configure the stream mode of ST25R3916 + * + * This function initializes the stream with the given parameters + * + * \param[in] config : all settings for bitrates, type, etc. + * + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_NONE : No error, stream mode driver initialized + * + ***************************************************************************** + */ +ReturnCode st25r3916StreamConfigure(const struct st25r3916StreamConfig* config); + +/*! + ***************************************************************************** + * \brief Executes a direct command and returns the result + * + * This function executes the direct command given by \a cmd waits for + * \a sleeptime for I_dct and returns the result read from register \a resreg. + * The value of cmd is not checked. + * + * \param[in] cmd : direct command to execute + * \param[in] resReg: address of the register containing the result + * \param[in] tOut : time in milliseconds to wait before reading the result + * \param[out] result: result + * + * \return RFAL_ERR_NONE : No error + * + ***************************************************************************** + */ +ReturnCode st25r3916ExecuteCommandAndGetResult(uint8_t cmd, uint8_t resReg, + uint8_t tOut, uint8_t* result); + +/*! + ***************************************************************************** + * \brief Gets the RSSI values + * + * This function gets the RSSI value of the previous reception taking into + * account the gain reductions that were used. + * RSSI value for both AM and PM channel can be retrieved. + * + * \param[out] amRssi: the RSSI on the AM channel expressed in mV + * \param[out] pmRssi: the RSSI on the PM channel expressed in mV + * + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_NONE : No error + * + ***************************************************************************** + */ +ReturnCode st25r3916GetRSSI(uint16_t* amRssi, uint16_t* pmRssi); + +/*! + ***************************************************************************** + * \brief Set Antenna mode + * + * Sets the antenna mode. + * Differential or single ended antenna mode (RFO1 or RFO2) + * + * \param[in] single: FALSE differential ; single ended mode + * \param[in] rfiox: FALSE RFI1/RFO1 ; TRUE RFI2/RFO2 + * + * \return RFAL_ERR_IO : Internal error + * \return RFAL_ERR_NOTSUPP : Feature not supported + * \return RFAL_ERR_NONE : No error + ***************************************************************************** + */ +ReturnCode st25r3916SetAntennaMode(bool single, bool rfiox); + +#endif /* ST25R3916_H */ + +/** + * @} + * + * @} + * + * @} + * + * @} + */ diff --git a/core/embed/io/nfc/rfal/source/st25r3916/st25r3916_aat.c b/core/embed/io/nfc/rfal/source/st25r3916/st25r3916_aat.c new file mode 100644 index 0000000000..e60faf37fe --- /dev/null +++ b/core/embed/io/nfc/rfal/source/st25r3916/st25r3916_aat.c @@ -0,0 +1,351 @@ +/****************************************************************************** + * @attention + * + * COPYRIGHT 2019 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/* + * PROJECT: ST25R3916 firmware + * Revision: + * LANGUAGE: ISO C99 + */ + +/*! \file st25r3916_aat.c + * + * \author + * + * \brief ST25R3916 Antenna Tuning + * + * The antenna tuning algorithm tries to find the optimal settings for + * the AAT_A and AAT_B registers, which are connected to variable capacitors + * to tune the antenna matching. + * + */ + +/* +****************************************************************************** +* INCLUDES +****************************************************************************** +*/ +#include "st25r3916_aat.h" +#include "rfal_chip.h" +#include "rfal_platform.h" +#include "rfal_utils.h" +#include "st25r3916.h" +#include "st25r3916_com.h" + +/* +****************************************************************************** +* GLOBAL DEFINES +****************************************************************************** +*/ +#define ST25R3916_AAT_CAP_DELAY_MAX \ + 10 /*!< Max Variable Capacitor settle delay */ + +/* +****************************************************************************** +* GLOBAL MACROS +****************************************************************************** +*/ +#define st25r3916AatLog( \ + ...) /* platformLog(__VA_ARGS__) */ /*!< Logging macro */ + +/* +****************************************************************************** +* LOCAL FUNCTION PROTOTYPES +****************************************************************************** +*/ +static ReturnCode aatHillClimb( + const struct st25r3916AatTuneParams *tuningParams, + struct st25r3916AatTuneResult *tuningStatus); +static int32_t aatGreedyDescent( + uint32_t *f_min, const struct st25r3916AatTuneParams *tuningParams, + struct st25r3916AatTuneResult *tuningStatus, int32_t previousDir); +static int32_t aatSteepestDescent( + uint32_t *f_min, const struct st25r3916AatTuneParams *tuningParams, + struct st25r3916AatTuneResult *tuningStatus, int32_t previousDir, + int32_t previousDir2); + +static ReturnCode aatMeasure(uint8_t serCap, uint8_t parCap, uint8_t *amplitude, + uint8_t *phase, uint16_t *measureCnt); +static uint32_t aatCalcF(const struct st25r3916AatTuneParams *tuningParams, + uint8_t amplitude, uint8_t phase); +static ReturnCode aatStepDacVals( + const struct st25r3916AatTuneParams *tuningParams, uint8_t *a, uint8_t *b, + int32_t dir); + +/*******************************************************************************/ +ReturnCode st25r3916AatTune(const struct st25r3916AatTuneParams *tuningParams, + struct st25r3916AatTuneResult *tuningStatus) { + ReturnCode err; + const struct st25r3916AatTuneParams *tp = tuningParams; + struct st25r3916AatTuneResult *ts = tuningStatus; + struct st25r3916AatTuneParams defaultTuningParams = { + .aat_a_min = 0, + .aat_a_max = 255, + .aat_a_start = 127, + .aat_a_stepWidth = 32, + .aat_b_min = 0, + .aat_b_max = 255, + .aat_b_start = 127, + .aat_b_stepWidth = 32, + + .phaTarget = 128, + .phaWeight = 2, + .ampTarget = 196, + .ampWeight = 1, + + .doDynamicSteps = true, + .measureLimit = 50, + }; + struct st25r3916AatTuneResult defaultTuneResult; + + if ((NULL != tp) && + ((tp->aat_a_min > tp->aat_a_max) || (tp->aat_a_start < tp->aat_a_min) || + (tp->aat_a_start > tp->aat_a_max) || (tp->aat_b_min > tp->aat_b_max) || + (tp->aat_b_start < tp->aat_b_min) || + (tp->aat_b_start > tp->aat_b_max))) { + return RFAL_ERR_PARAM; + } + + if (NULL == tp) { /* Start from current caps with default params */ + st25r3916ReadRegister(ST25R3916_REG_ANT_TUNE_A, + &defaultTuningParams.aat_a_start); + st25r3916ReadRegister(ST25R3916_REG_ANT_TUNE_B, + &defaultTuningParams.aat_b_start); + tp = &defaultTuningParams; + } + + if (NULL == ts) { + ts = &defaultTuneResult; + } + + ts->measureCnt = 0; /* Clear current measure count */ + + err = aatHillClimb(tp, ts); + + return err; +} + +/*******************************************************************************/ +static ReturnCode aatHillClimb( + const struct st25r3916AatTuneParams *tuningParams, + struct st25r3916AatTuneResult *tuningStatus) { + ReturnCode err = RFAL_ERR_NONE; + uint32_t f_min; + int32_t direction, gdirection; + uint8_t amp, phs; + struct st25r3916AatTuneParams tp = *tuningParams; // local copy to obey const + + tuningStatus->aat_a = tuningParams->aat_a_start; + tuningStatus->aat_b = tuningParams->aat_b_start; + + /* Get a proper start value */ + aatMeasure(tuningStatus->aat_a, tuningStatus->aat_b, &, &phs, + &tuningStatus->measureCnt); + f_min = aatCalcF(&tp, amp, phs); + direction = 0; + + st25r3916AatLog("%d %d: %d***\n", tuningStatus->aat_a, tuningStatus->aat_b, + f_min); + + do { + direction = 0; /* Initially and after reducing step sizes we don't have a + previous direction */ + do { + /* With the greedy step below always executed aftwards the -direction does + * never need to be investigated */ + direction = + aatSteepestDescent(&f_min, &tp, tuningStatus, direction, -direction); + if (tuningStatus->measureCnt > tp.measureLimit) { + err = RFAL_ERR_OVERRUN; + break; + } + do { + gdirection = aatGreedyDescent(&f_min, &tp, tuningStatus, direction); + if (tuningStatus->measureCnt > tp.measureLimit) { + err = RFAL_ERR_OVERRUN; + break; + } + } while (0 != gdirection); + } while (0 != direction); + tp.aat_a_stepWidth /= 2U; /* Reduce step sizes */ + tp.aat_b_stepWidth /= 2U; + } while ((tp.doDynamicSteps) && + ((tp.aat_a_stepWidth > 0U) || (tp.aat_b_stepWidth > 0U))); + + return err; +} + +/*******************************************************************************/ +static int32_t aatSteepestDescent( + uint32_t *f_min, const struct st25r3916AatTuneParams *tuningParams, + struct st25r3916AatTuneResult *tuningStatus, int32_t previousDir, + int32_t previousDir2) { + int32_t i; + uint8_t amp, phs; + uint32_t f; + int32_t bestdir = 0; /* Negative direction: decrease, Positive: increase. + (-)1: aat_a, (-)2: aat_b */ + + for (i = -2; i <= 2; i++) { + uint8_t a = tuningStatus->aat_a, b = tuningStatus->aat_b; + + if ((0 == i) || (i == -previousDir) || + (i == + -previousDir2)) { /* Skip no direction and avoid going backwards */ + continue; + } + if (0U != aatStepDacVals(tuningParams, &a, &b, + i)) { /* If stepping did not change the value, omit + this direction */ + continue; + } + + aatMeasure(a, b, &, &phs, &tuningStatus->measureCnt); + f = aatCalcF(tuningParams, amp, phs); + st25r3916AatLog("%d : %d %d: %d", i, a, b, f); + if (f < *f_min) { /* Value is better than all previous ones */ + st25r3916AatLog("*"); + *f_min = f; + bestdir = i; + } + st25r3916AatLog("\n"); + } + if (0 != bestdir) { /* Walk into the best direction */ + aatStepDacVals(tuningParams, &tuningStatus->aat_a, &tuningStatus->aat_b, + bestdir); + } + return bestdir; +} + +/*******************************************************************************/ +static int32_t aatGreedyDescent( + uint32_t *f_min, const struct st25r3916AatTuneParams *tuningParams, + struct st25r3916AatTuneResult *tuningStatus, int32_t previousDir) { + uint8_t amp, phs; + uint32_t f; + uint8_t a = tuningStatus->aat_a, b = tuningStatus->aat_b; + + if (0U != aatStepDacVals(tuningParams, &a, &b, + previousDir)) { /* If stepping did not change the + value, omit this direction */ + return 0; + } + + aatMeasure(a, b, &, &phs, &tuningStatus->measureCnt); + f = aatCalcF(tuningParams, amp, phs); + st25r3916AatLog("g : %d %d: %d", a, b, f); + if (f < *f_min) { /* Value is better than previous one */ + st25r3916AatLog("*\n"); + tuningStatus->aat_a = a; + tuningStatus->aat_b = b; + *f_min = f; + return previousDir; + } + + st25r3916AatLog("\n"); + return 0; +} + +/*******************************************************************************/ +static uint32_t aatCalcF(const struct st25r3916AatTuneParams *tuningParams, + uint8_t amplitude, uint8_t phase) { + /* f(amp, pha) = (ampWeight * |amp - ampTarget|) + (phaWeight * |pha - + * phaTarget|) */ + uint8_t ampTarget = tuningParams->ampTarget; + uint8_t phaTarget = tuningParams->phaTarget; + + uint32_t ampWeight = tuningParams->ampWeight; + uint32_t phaWeight = tuningParams->phaWeight; + + /* Temp variables to avoid MISRA R10.8 (cast on composite expression) */ + uint8_t ad = ((amplitude > ampTarget) ? (amplitude - ampTarget) + : (ampTarget - amplitude)); + uint8_t pd = + ((phase > phaTarget) ? (phase - phaTarget) : (phaTarget - phase)); + + uint32_t ampDelta = (uint32_t)ad; + uint32_t phaDelta = (uint32_t)pd; + + return ((ampWeight * ampDelta) + (phaWeight * phaDelta)); +} + +/*******************************************************************************/ +static ReturnCode aatStepDacVals( + const struct st25r3916AatTuneParams *tuningParams, uint8_t *a, uint8_t *b, + int32_t dir) { + int16_t aat_a = (int16_t)*a, aat_b = (int16_t)*b; + + switch (abs(dir)) { /* Advance by steps size in requested direction */ + case 1: + aat_a = (dir < 0) ? (aat_a - (int16_t)tuningParams->aat_a_stepWidth) + : (aat_a + (int16_t)tuningParams->aat_a_stepWidth); + if (aat_a < (int16_t)tuningParams->aat_a_min) { + aat_a = (int16_t)tuningParams->aat_a_min; + } + if (aat_a > (int16_t)tuningParams->aat_a_max) { + aat_a = (int16_t)tuningParams->aat_a_max; + } + if ((int16_t)*a == aat_a) { + return RFAL_ERR_PARAM; + } + break; + case 2: + aat_b = (dir < 0) ? (aat_b - (int16_t)tuningParams->aat_b_stepWidth) + : (aat_b + (int16_t)tuningParams->aat_b_stepWidth); + if (aat_b < (int16_t)tuningParams->aat_b_min) { + aat_b = (int16_t)tuningParams->aat_b_min; + } + if (aat_b > (int16_t)tuningParams->aat_b_max) { + aat_b = (int16_t)tuningParams->aat_b_max; + } + if ((int16_t)*b == aat_b) { + return RFAL_ERR_PARAM; + } + break; + default: + return RFAL_ERR_REQUEST; + } + /* We only get here if actual values have changed. In all other cases an error + * is returned */ + *a = (uint8_t)aat_a; + *b = (uint8_t)aat_b; + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +static ReturnCode aatMeasure(uint8_t serCap, uint8_t parCap, uint8_t *amplitude, + uint8_t *phase, uint16_t *measureCnt) { + ReturnCode err; + + *amplitude = 0; + *phase = 0; + + st25r3916WriteRegister(ST25R3916_REG_ANT_TUNE_A, serCap); + st25r3916WriteRegister(ST25R3916_REG_ANT_TUNE_B, parCap); + + /* Wait till caps have settled.. */ + platformDelay(ST25R3916_AAT_CAP_DELAY_MAX); + + /* Get amplitude and phase .. */ + err = rfalChipMeasureAmplitude(amplitude); + if (RFAL_ERR_NONE == err) { + err = rfalChipMeasurePhase(phase); + } + + if (measureCnt != NULL) { + (*measureCnt)++; + } + return err; +} diff --git a/core/embed/io/nfc/rfal/source/st25r3916/st25r3916_aat.h b/core/embed/io/nfc/rfal/source/st25r3916/st25r3916_aat.h new file mode 100644 index 0000000000..7454ed74df --- /dev/null +++ b/core/embed/io/nfc/rfal/source/st25r3916/st25r3916_aat.h @@ -0,0 +1,106 @@ +/****************************************************************************** + * @attention + * + * COPYRIGHT 2019 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/* + * PROJECT: ST25R3916 firmware + * Revision: + * LANGUAGE: ISO C99 + */ + +/*! \file st25r3916_aat.h + * + * \author + * + * \brief ST25R3916 Antenna Tuning + * + * The antenna tuning algorithm tries to find the optimal settings for + * the AAT_A and AAT_B registers, which are connected to variable capacitors + * to tune the antenna matching. + * + */ + +#ifndef ST25R3916_AAT_H +#define ST25R3916_AAT_H + +#include "rfal_platform.h" +#include "rfal_utils.h" + +/* +****************************************************************************** +* GLOBAL DATATYPES +****************************************************************************** +*/ + +/*! + * struct representing input parameters for the antenna tuning + */ +struct st25r3916AatTuneParams { + uint8_t aat_a_min; /*!< min value of A cap */ + uint8_t aat_a_max; /*!< max value of A cap */ + uint8_t aat_a_start; /*!< start value of A cap */ + uint8_t aat_a_stepWidth; /*!< increment stepWidth for A cap */ + uint8_t aat_b_min; /*!< min value of B cap */ + uint8_t aat_b_max; /*!< max value of B cap */ + uint8_t aat_b_start; /*!< start value of B cap */ + uint8_t aat_b_stepWidth; /*!< increment stepWidth for B cap */ + + uint8_t phaTarget; /*!< target phase */ + uint8_t phaWeight; /*!< weight of target phase */ + uint8_t ampTarget; /*!< target amplitude */ + uint8_t ampWeight; /*!< weight of target amplitude */ + + bool doDynamicSteps; /*!< dynamically reduce step size in algo */ + uint8_t measureLimit; /*!< max number of allowed steps/measurements */ +}; + +/*! + * struct representing out parameters for the antenna tuning + */ +struct st25r3916AatTuneResult { + uint8_t aat_a; /*!< serial cap after tuning */ + uint8_t aat_b; /*!< parallel cap after tuning */ + uint8_t pha; /*!< phase after tuning */ + uint8_t amp; /*!< amplitude after tuning */ + uint16_t measureCnt; /*!< number of measures performed */ +}; + +/*! + ***************************************************************************** + * \brief Perform antenna tuning + * + * This function starts an antenna tuning procedure by modifying the serial + * and parallel capacitors of the antenna matching circuit via the AAT_A + * and AAT_B registers. + * This function is best run if the field is already turned on. + * When used on ST25R3916B with new rgs_am=1 it is necessary to turn on the + * field before running this procedure or to set rgs_txonoff=0. + * + * \param[in] tuningParams : Input parameters for the tuning algorithm. If NULL + * default values will be used. + * \param[out] tuningStatus : Result information of performed tuning. If NULL + * no further information is returned, only + *registers ST25R3916 (AAT_A,B) will be adapted. + * + * \return RFAL_ERR_IO : Error during communication. + * \return RFAL_ERR_PARAM : Invalid input parameters + * \return RFAL_ERR_NONE : No error. + * + ***************************************************************************** + */ +extern ReturnCode st25r3916AatTune( + const struct st25r3916AatTuneParams *tuningParams, + struct st25r3916AatTuneResult *tuningStatus); + +#endif /* ST25R3916_AAT_H */ diff --git a/core/embed/io/nfc/rfal/source/st25r3916/st25r3916_com.c b/core/embed/io/nfc/rfal/source/st25r3916/st25r3916_com.c new file mode 100644 index 0000000000..0fa7742a5b --- /dev/null +++ b/core/embed/io/nfc/rfal/source/st25r3916/st25r3916_com.c @@ -0,0 +1,643 @@ + +/****************************************************************************** + * @attention + * + * COPYRIGHT 2016 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/* + * PROJECT: ST25R3916 firmware + * Revision: + * LANGUAGE: ISO C99 + */ + +/*! \file + * + * \author Gustavo Patricio + * + * \brief Implementation of ST25R3916 communication + * + */ + +/* +****************************************************************************** +* INCLUDES +****************************************************************************** +*/ + +#include "st25r3916_com.h" +#include "rfal_platform.h" +#include "rfal_utils.h" +#include "st25r3916.h" +#include "st25r3916_led.h" + +/* +****************************************************************************** +* LOCAL DEFINES +****************************************************************************** +*/ + +#define ST25R3916_OPTIMIZE \ + true /*!< Optimization switch: false always write value to register */ +#define ST25R3916_I2C_ADDR \ + (0xA0U >> 1) /*!< ST25R3916's default I2C address \ + */ +#define ST25R3916_REG_LEN \ + 1U /*!< Byte length of a ST25R3916 register */ +#define ST25R3916_MOSI_IDLE (0x00) /*!< ST25R3916 MOSI IDLE state */ + +#define ST25R3916_WRITE_MODE (0U << 6) /*!< ST25R3916 Operation Mode: Write */ +#define ST25R3916_READ_MODE (1U << 6) /*!< ST25R3916 Operation Mode: Read */ +#define ST25R3916_CMD_MODE \ + (3U << 6) /*!< ST25R3916 Operation Mode: Direct Command */ +#define ST25R3916_FIFO_LOAD \ + (0x80U) /*!< ST25R3916 Operation Mode: FIFO Load \ + */ +#define ST25R3916_FIFO_READ \ + (0x9FU) /*!< ST25R3916 Operation Mode: FIFO Read \ + */ +#define ST25R3916_PT_A_CONFIG_LOAD \ + (0xA0U) /*!< ST25R3916 Operation Mode: Passive Target Memory A-Config Load \ + */ +#define ST25R3916_PT_F_CONFIG_LOAD \ + (0xA8U) /*!< ST25R3916 Operation Mode: Passive Target Memory F-Config Load \ + */ +#define ST25R3916_PT_TSN_DATA_LOAD \ + (0xACU) /*!< ST25R3916 Operation Mode: Passive Target Memory TSN Load */ +#define ST25R3916_PT_MEM_READ \ + (0xBFU) /*!< ST25R3916 Operation Mode: Passive Target Memory Read */ + +#define ST25R3916_CMD_LEN \ + (1U) /*!< ST25R3916 CMD length */ +#define ST25R3916_BUF_LEN \ + (ST25R3916_CMD_LEN + ST25R3916_FIFO_DEPTH) /*!< ST25R3916 communication \ + buffer: CMD + FIFO length */ + +/* +****************************************************************************** +* MACROS +****************************************************************************** +*/ +#ifdef RFAL_USE_I2C +#define st25r3916I2CStart() \ + platformI2CStart() /*!< ST25R3916 HAL I2C driver macro to start a I2C \ + transfer */ +#define st25r3916I2CStop() \ + platformI2CStop() /*!< ST25R3916 HAL I2C driver macro to stop a I2C transfer \ + */ +#define st25r3916I2CRepeatStart() \ + platformI2CRepeatStart() /*!< ST25R3916 HAL I2C driver macro to repeat Start \ + */ +#define st25r3916I2CSlaveAddrWR(sA) \ + platformI2CSlaveAddrWR( \ + sA) /*!< ST25R3916 HAL I2C driver macro to repeat Start */ +#define st25r3916I2CSlaveAddrRD(sA) \ + platformI2CSlaveAddrRD( \ + sA) /*!< ST25R3916 HAL I2C driver macro to repeat Start */ +#endif /* RFAL_USE_I2C */ + +#if defined(ST25R_COM_SINGLETXRX) && !defined(RFAL_USE_I2C) +static uint8_t comBuf[ST25R3916_BUF_LEN]; /*!< ST25R3916 communication buffer */ +static uint16_t comBufIt; /*!< ST25R3916 communication buffer iterator */ +#endif /* ST25R_COM_SINGLETXRX */ + +/* + ****************************************************************************** + * LOCAL FUNCTION PROTOTYPES + ****************************************************************************** + */ + +/*! + ****************************************************************************** + * \brief ST25R3916 communication Start + * + * This method performs the required actions to start communications with + * ST25R3916, either by SPI or I2C + ****************************************************************************** + */ +static void st25r3916comStart(void); + +/*! + ****************************************************************************** + * \brief ST25R3916 communication Stop + * + * This method performs the required actions to terminate communications with + * ST25R3916, either by SPI or I2C + ****************************************************************************** + */ +static void st25r3916comStop(void); + +/*! + ****************************************************************************** + * \brief ST25R3916 communication Repeat Start + * + * This method performs the required actions to repeat start a transmission + * with ST25R3916, either by SPI or I2C + ****************************************************************************** + */ +#ifdef RFAL_USE_I2C +static void st25r3916comRepeatStart(void); +#else +#define st25r3916comRepeatStart() +#endif /* RFAL_USE_I2C */ + +/*! + ****************************************************************************** + * \brief ST25R3916 communication Tx + * + * This method performs the required actions to transmit the given buffer + * to ST25R3916, either by SPI or I2C + * + * \param[in] txBuf : the buffer to transmit + * \param[in] txLen : the length of the buffer to transmit + * \param[in] last : true if last data to be transmitted + * \param[in] txOnly : true no reception is to be performed + * + ****************************************************************************** + */ +static void st25r3916comTx(const uint8_t* txBuf, uint16_t txLen, bool last, + bool txOnly); + +/*! + ****************************************************************************** + * \brief ST25R3916 communication Rx + * + * This method performs the required actions to receive from ST25R3916 the given + * amount of bytes, either by SPI or I2C + * + * \param[out] rxBuf : the buffer place the received bytes + * \param[in] rxLen : the length to receive + * + ****************************************************************************** + */ +static void st25r3916comRx(uint8_t* rxBuf, uint16_t rxLen); + +/*! + ****************************************************************************** + * \brief ST25R3916 communication Tx Byte + * + * This helper method transmits a byte passed by value and not by reference + * + * \param[in] txByte : the value of the byte to be transmitted + * \param[in] last : true if last byte to be transmitted + * \param[in] txOnly : true no reception is to be performed + * + ****************************************************************************** + */ +static void st25r3916comTxByte(uint8_t txByte, bool last, bool txOnly); + +/* + ****************************************************************************** + * LOCAL FUNCTION + ****************************************************************************** + */ +static void st25r3916comStart(void) { + /* Make this operation atomic, disabling ST25R3916 interrupt during + * communications*/ + platformProtectST25RComm(); + +#ifdef RFAL_USE_I2C + /* I2C Start and send Slave Address */ + st25r3916I2CStart(); + st25r3916I2CSlaveAddrWR(ST25R3916_I2C_ADDR); +#else + /* Perform the chip select */ + platformSpiSelect(); + +#if defined(ST25R_COM_SINGLETXRX) + comBufIt = 0; /* reset local buffer position */ +#endif /* ST25R_COM_SINGLETXRX */ + +#endif /* RFAL_USE_I2C */ +} + +/*******************************************************************************/ +static void st25r3916comStop(void) { +#ifdef RFAL_USE_I2C + /* Generate Stop signal */ + st25r3916I2CStop(); +#else + /* Release the chip select */ + platformSpiDeselect(); +#endif /* RFAL_USE_I2C */ + + /* reEnable the ST25R3916 interrupt */ + platformUnprotectST25RComm(); +} + +/*******************************************************************************/ +#ifdef RFAL_USE_I2C +static void st25r3916comRepeatStart(void) { + st25r3916I2CRepeatStart(); + st25r3916I2CSlaveAddrRD(ST25R3916_I2C_ADDR); +} +#endif /* RFAL_USE_I2C */ + +/*******************************************************************************/ +static void st25r3916comTx(const uint8_t* txBuf, uint16_t txLen, bool last, + bool txOnly) { + RFAL_NO_WARNING(last); + RFAL_NO_WARNING(txOnly); + + if (txLen > 0U) { +#ifdef RFAL_USE_I2C + platformI2CTx(txBuf, txLen, last, txOnly); +#else /* RFAL_USE_I2C */ + +#ifdef ST25R_COM_SINGLETXRX + + RFAL_MEMCPY( + &comBuf[comBufIt], txBuf, + RFAL_MIN(txLen, + (uint16_t)(ST25R3916_BUF_LEN - + comBufIt))); /* copy tx data to local buffer */ + comBufIt += RFAL_MIN( + txLen, + (ST25R3916_BUF_LEN - comBufIt)); /* store position on local buffer */ + + if (last && txOnly) /* only perform SPI transaction if no Rx will follow */ + { + platformSpiTxRx(comBuf, NULL, comBufIt); + } + +#else + platformSpiTxRx(txBuf, NULL, txLen); +#endif /* ST25R_COM_SINGLETXRX */ + +#endif /* RFAL_USE_I2C */ + } +} + +/*******************************************************************************/ +static void st25r3916comRx(uint8_t* rxBuf, uint16_t rxLen) { +#ifndef ST25R_COM_SINGLETXRX + uint8_t dummyBuf; + uint16_t rxIt; +#endif /* ST25R_COM_SINGLETXRX */ + + if (rxLen > 0U) { +#ifdef RFAL_USE_I2C + platformI2CRx(rxBuf, rxLen); +#else /* RFAL_USE_I2C */ + +#ifdef ST25R_COM_SINGLETXRX + RFAL_MEMSET( + &comBuf[comBufIt], ST25R3916_MOSI_IDLE, + RFAL_MIN(rxLen, (uint16_t)(ST25R3916_BUF_LEN - + comBufIt))); /* Clear outgoing buffer */ + platformSpiTxRx( + comBuf, comBuf, + RFAL_MIN((comBufIt + rxLen), + ST25R3916_BUF_LEN)); /* Transceive as a single SPI call */ + if (rxBuf != NULL) { + RFAL_MEMCPY( + rxBuf, &comBuf[comBufIt], + RFAL_MIN(rxLen, + (uint16_t)(ST25R3916_BUF_LEN - + comBufIt))); /* Copy from local buf to output + buffer and skip cmd byte */ + } +#else + + /* In case rxBuf is not provided, ensure that SPI operation is executed. * + * Depending on the HAL used, the SPI driver may not support * + * NULL as Rx buffer, do single byte SPI transactions to a dummy buffer */ + if (rxBuf == NULL) { + for (rxIt = 0; (rxIt < rxLen); rxIt++) { + dummyBuf = ST25R3916_MOSI_IDLE; /* Clear outgoing|incoming buffer */ + platformSpiTxRx( + &dummyBuf, &dummyBuf, + 1U); /* Re-use the buffer as SPI outputs data first then reads */ + } + } else { + RFAL_MEMSET(rxBuf, ST25R3916_MOSI_IDLE, + rxLen); /* Clear outgoing|incoming buffer */ + platformSpiTxRx( + rxBuf, rxBuf, + rxLen); /* Re-use the rxBuf as SPI outputs data first then reads */ + } + +#endif /* ST25R_COM_SINGLETXRX */ +#endif /* RFAL_USE_I2C */ + } +} + +/*******************************************************************************/ +static void st25r3916comTxByte(uint8_t txByte, bool last, bool txOnly) { + uint8_t val = txByte; /* MISRA 17.8: use intermediate variable */ + st25r3916comTx(&val, ST25R3916_REG_LEN, last, txOnly); +} + +/* +****************************************************************************** +* GLOBAL FUNCTIONS +****************************************************************************** +*/ + +/*******************************************************************************/ +ReturnCode st25r3916ReadRegister(uint8_t reg, uint8_t* val) { + return st25r3916ReadMultipleRegisters(reg, val, ST25R3916_REG_LEN); +} + +/*******************************************************************************/ +ReturnCode st25r3916ReadMultipleRegisters(uint8_t reg, uint8_t* values, + uint8_t length) { + if (length > 0U) { + st25r3916comStart(); + + /* If is a space-B register send a direct command first */ + if ((reg & ST25R3916_SPACE_B) != 0U) { + st25r3916comTxByte(ST25R3916_CMD_SPACE_B_ACCESS, false, false); + } + + st25r3916comTxByte(((reg & ~ST25R3916_SPACE_B) | ST25R3916_READ_MODE), true, + false); + st25r3916comRepeatStart(); + st25r3916comRx(values, length); + st25r3916comStop(); + } + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode st25r3916WriteRegister(uint8_t reg, uint8_t val) { + uint8_t value = val; /* MISRA 17.8: use intermediate variable */ + return st25r3916WriteMultipleRegisters(reg, &value, ST25R3916_REG_LEN); +} + +/*******************************************************************************/ +ReturnCode st25r3916WriteMultipleRegisters(uint8_t reg, const uint8_t* values, + uint8_t length) { + if (length > 0U) { + st25r3916comStart(); + + if ((reg & ST25R3916_SPACE_B) != 0U) { + st25r3916comTxByte(ST25R3916_CMD_SPACE_B_ACCESS, false, true); + } + + st25r3916comTxByte(((reg & ~ST25R3916_SPACE_B) | ST25R3916_WRITE_MODE), + false, true); + st25r3916comTx(values, length, true, true); + st25r3916comStop(); + + /* Send a WriteMultiReg event to LED handling */ + st25r3916ledEvtWrMultiReg(reg, values, length); + } + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode st25r3916WriteFifo(const uint8_t* values, uint16_t length) { + if (length > ST25R3916_FIFO_DEPTH) { + return RFAL_ERR_PARAM; + } + + if (length > 0U) { + st25r3916comStart(); + st25r3916comTxByte(ST25R3916_FIFO_LOAD, false, true); + st25r3916comTx(values, length, true, true); + st25r3916comStop(); + } + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode st25r3916ReadFifo(uint8_t* buf, uint16_t length) { + if (length > 0U) { + st25r3916comStart(); + st25r3916comTxByte(ST25R3916_FIFO_READ, true, false); + + st25r3916comRepeatStart(); + st25r3916comRx(buf, length); + st25r3916comStop(); + } + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode st25r3916WritePTMem(const uint8_t* values, uint16_t length) { + if (length > ST25R3916_PTM_LEN) { + return RFAL_ERR_PARAM; + } + + if (length > 0U) { + st25r3916comStart(); + st25r3916comTxByte(ST25R3916_PT_A_CONFIG_LOAD, false, true); + st25r3916comTx(values, length, true, true); + st25r3916comStop(); + } + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode st25r3916ReadPTMem(uint8_t* values, uint16_t length) { + uint8_t tmp[ST25R3916_REG_LEN + + ST25R3916_PTM_LEN]; /* local buffer to handle prepended byte on + I2C and SPI */ + + if (length > 0U) { + if (length > ST25R3916_PTM_LEN) { + return RFAL_ERR_PARAM; + } + + st25r3916comStart(); + st25r3916comTxByte(ST25R3916_PT_MEM_READ, true, false); + + st25r3916comRepeatStart(); + st25r3916comRx(tmp, (ST25R3916_REG_LEN + length)); /* skip prepended byte */ + st25r3916comStop(); + + /* Copy PTMem content without prepended byte */ + RFAL_MEMCPY(values, (tmp + ST25R3916_REG_LEN), length); + } + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode st25r3916WritePTMemF(const uint8_t* values, uint16_t length) { + if (length > (ST25R3916_PTM_F_LEN + ST25R3916_PTM_TSN_LEN)) { + return RFAL_ERR_PARAM; + } + + if (length > 0U) { + st25r3916comStart(); + st25r3916comTxByte(ST25R3916_PT_F_CONFIG_LOAD, false, true); + st25r3916comTx(values, length, true, true); + st25r3916comStop(); + } + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode st25r3916WritePTMemTSN(const uint8_t* values, uint16_t length) { + if (length > ST25R3916_PTM_TSN_LEN) { + return RFAL_ERR_PARAM; + } + + if (length > 0U) { + st25r3916comStart(); + st25r3916comTxByte(ST25R3916_PT_TSN_DATA_LOAD, false, true); + st25r3916comTx(values, length, true, true); + st25r3916comStop(); + } + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode st25r3916ExecuteCommand(uint8_t cmd) { + st25r3916comStart(); + st25r3916comTxByte((cmd | ST25R3916_CMD_MODE), true, true); + st25r3916comStop(); + + /* Send a cmd event to LED handling */ + st25r3916ledEvtCmd(cmd); + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode st25r3916ReadTestRegister(uint8_t reg, uint8_t* val) { + st25r3916comStart(); + st25r3916comTxByte(ST25R3916_CMD_TEST_ACCESS, false, false); + st25r3916comTxByte((reg | ST25R3916_READ_MODE), true, false); + st25r3916comRepeatStart(); + st25r3916comRx(val, ST25R3916_REG_LEN); + st25r3916comStop(); + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode st25r3916WriteTestRegister(uint8_t reg, uint8_t val) { + uint8_t value = val; /* MISRA 17.8: use intermediate variable */ + + st25r3916comStart(); + st25r3916comTxByte(ST25R3916_CMD_TEST_ACCESS, false, true); + st25r3916comTxByte((reg | ST25R3916_WRITE_MODE), false, true); + st25r3916comTx(&value, ST25R3916_REG_LEN, true, true); + st25r3916comStop(); + + return RFAL_ERR_NONE; +} + +/*******************************************************************************/ +ReturnCode st25r3916ClrRegisterBits(uint8_t reg, uint8_t clr_mask) { + ReturnCode ret; + uint8_t rdVal; + + /* Read current reg value */ + RFAL_EXIT_ON_ERR(ret, st25r3916ReadRegister(reg, &rdVal)); + + /* Only perform a Write if value to be written is different */ + if (ST25R3916_OPTIMIZE && (rdVal == (uint8_t)(rdVal & ~clr_mask))) { + return RFAL_ERR_NONE; + } + + /* Write new reg value */ + return st25r3916WriteRegister(reg, (uint8_t)(rdVal & ~clr_mask)); +} + +/*******************************************************************************/ +ReturnCode st25r3916SetRegisterBits(uint8_t reg, uint8_t set_mask) { + ReturnCode ret; + uint8_t rdVal; + + /* Read current reg value */ + RFAL_EXIT_ON_ERR(ret, st25r3916ReadRegister(reg, &rdVal)); + + /* Only perform a Write if the value to be written is different */ + if (ST25R3916_OPTIMIZE && (rdVal == (rdVal | set_mask))) { + return RFAL_ERR_NONE; + } + + /* Write new reg value */ + return st25r3916WriteRegister(reg, (rdVal | set_mask)); +} + +/*******************************************************************************/ +ReturnCode st25r3916ChangeRegisterBits(uint8_t reg, uint8_t valueMask, + uint8_t value) { + return st25r3916ModifyRegister(reg, valueMask, (valueMask & value)); +} + +/*******************************************************************************/ +ReturnCode st25r3916ModifyRegister(uint8_t reg, uint8_t clr_mask, + uint8_t set_mask) { + ReturnCode ret; + uint8_t rdVal; + uint8_t wrVal; + + /* Read current reg value */ + RFAL_EXIT_ON_ERR(ret, st25r3916ReadRegister(reg, &rdVal)); + + /* Compute new value */ + wrVal = (uint8_t)(rdVal & ~clr_mask); + wrVal |= set_mask; + + /* Only perform a Write if the value to be written is different */ + if (ST25R3916_OPTIMIZE && (rdVal == wrVal)) { + return RFAL_ERR_NONE; + } + + /* Write new reg value */ + return st25r3916WriteRegister(reg, wrVal); +} + +/*******************************************************************************/ +ReturnCode st25r3916ChangeTestRegisterBits(uint8_t reg, uint8_t valueMask, + uint8_t value) { + ReturnCode ret; + uint8_t rdVal; + uint8_t wrVal; + + /* Read current reg value */ + RFAL_EXIT_ON_ERR(ret, st25r3916ReadTestRegister(reg, &rdVal)); + + /* Compute new value */ + wrVal = (uint8_t)(rdVal & ~valueMask); + wrVal |= (uint8_t)(value & valueMask); + + /* Only perform a Write if the value to be written is different */ + if (ST25R3916_OPTIMIZE && (rdVal == wrVal)) { + return RFAL_ERR_NONE; + } + + /* Write new reg value */ + return st25r3916WriteTestRegister(reg, wrVal); +} + +/*******************************************************************************/ +bool st25r3916CheckReg(uint8_t reg, uint8_t mask, uint8_t val) { + uint8_t regVal; + + regVal = 0; + st25r3916ReadRegister(reg, ®Val); + + return ((regVal & mask) == val); +} + +/*******************************************************************************/ +bool st25r3916IsRegValid(uint8_t reg) { + if (!(((int16_t)reg >= (int16_t)ST25R3916_REG_IO_CONF1) && + (reg <= (ST25R3916_SPACE_B | ST25R3916_REG_IC_IDENTITY)))) { + return false; + } + return true; +} diff --git a/core/embed/io/nfc/rfal/source/st25r3916/st25r3916_com.h b/core/embed/io/nfc/rfal/source/st25r3916/st25r3916_com.h new file mode 100644 index 0000000000..6df98e755c --- /dev/null +++ b/core/embed/io/nfc/rfal/source/st25r3916/st25r3916_com.h @@ -0,0 +1,1618 @@ + +/****************************************************************************** + * @attention + * + * COPYRIGHT 2016 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/* + * PROJECT: ST25R3916 firmware + * Revision: + * LANGUAGE: ISO C99 + */ + +/*! \file + * + * \author Gustavo Patricio + * + * \brief ST25R3916 communication declaration file + * + * This driver provides basic abstraction for communication with the ST25R3916 + * + * + * \addtogroup RFAL + * @{ + * + * \addtogroup RFAL-HAL + * \brief RFAL Hardware Abstraction Layer + * @{ + * + * \addtogroup ST25R3916 + * \brief RFAL ST25R3916 Driver + * @{ + * + * \addtogroup ST25R3916_COM + * \brief RFAL ST25R3916 Communications + * @{ + * + */ + +#ifndef ST25R3916_COM_H +#define ST25R3916_COM_H + +/* +****************************************************************************** +* INCLUDES +****************************************************************************** +*/ +#include "rfal_platform.h" +#include "rfal_utils.h" + +/* +****************************************************************************** +* GLOBAL DEFINES +****************************************************************************** +*/ +#define ST25R3916_SPACE_B \ + 0x40U /*!< ST25R3916 Space-B indicator */ +#ifdef ST25R3916B +#define ST25R3916_SPACE_B_REG_LEN \ + 24U /*!< Number of register in the space B */ +#else +#define ST25R3916_SPACE_B_REG_LEN \ + 16U /*!< Number of register in the space B */ +#endif /* ST25R3916B */ + +#define ST25R3916_FIFO_STATUS_LEN \ + 2 /*!< Number of FIFO Status Register */ + +#define ST25R3916_PTM_A_LEN \ + 15U /*!< Passive target memory A config length */ +#define ST25R3916_PTM_B_LEN \ + 0U /*!< Passive target memory B config length */ +#define ST25R3916_PTM_F_LEN \ + 21U /*!< Passive target memory F config length */ +#define ST25R3916_PTM_TSN_LEN \ + 12U /*!< Passive target memory TSN data length */ + +/*! Full Passive target memory length */ +#define ST25R3916_PTM_LEN \ + (ST25R3916_PTM_A_LEN + ST25R3916_PTM_B_LEN + ST25R3916_PTM_F_LEN + \ + ST25R3916_PTM_TSN_LEN) + +/* IO configuration registers */ +#define ST25R3916_REG_IO_CONF1 \ + 0x00U /*!< RW IO Configuration Register 1 */ +#define ST25R3916_REG_IO_CONF2 \ + 0x01U /*!< RW IO Configuration Register 2 */ + +/* Operation control and mode definition registers */ +#define ST25R3916_REG_OP_CONTROL \ + 0x02U /*!< RW Operation Control Register */ +#define ST25R3916_REG_MODE \ + 0x03U /*!< RW Mode Definition Register */ +#define ST25R3916_REG_BIT_RATE \ + 0x04U /*!< RW Bit Rate Definition Register */ + +/* Protocol Configuration registers */ +#define ST25R3916_REG_ISO14443A_NFC \ + 0x05U /*!< RW ISO14443A and NFC 106 kBit/s Settings Register */ +#define ST25R3916_REG_EMD_SUP_CONF \ + (ST25R3916_SPACE_B | \ + 0x05U) /*!< RW EMD Suppression Configuration Register */ +#define ST25R3916_REG_ISO14443B_1 \ + 0x06U /*!< RW ISO14443B Settings Register 1 */ +#define ST25R3916_REG_SUBC_START_TIME \ + (ST25R3916_SPACE_B | 0x06U) /*!< RW Subcarrier Start Time Register */ +#define ST25R3916_REG_ISO14443B_2 \ + 0x07U /*!< RW ISO14443B Settings Register 2 */ +#define ST25R3916_REG_PASSIVE_TARGET \ + 0x08U /*!< RW Passive Target Definition Register */ +#define ST25R3916_REG_STREAM_MODE \ + 0x09U /*!< RW Stream Mode Definition Register */ +#define ST25R3916_REG_AUX \ + 0x0AU /*!< RW Auxiliary Definition Register */ + +/* Receiver Configuration registers */ +#define ST25R3916_REG_RX_CONF1 \ + 0x0BU /*!< RW Receiver Configuration Register 1 */ +#define ST25R3916_REG_RX_CONF2 \ + 0x0CU /*!< RW Receiver Configuration Register 2 */ +#define ST25R3916_REG_RX_CONF3 \ + 0x0DU /*!< RW Receiver Configuration Register 3 */ +#define ST25R3916_REG_RX_CONF4 \ + 0x0EU /*!< RW Receiver Configuration Register 4 */ +#define ST25R3916_REG_P2P_RX_CONF \ + (ST25R3916_SPACE_B | 0x0BU) /*!< RW P2P Receiver Configuration Register 1 */ +#define ST25R3916_REG_CORR_CONF1 \ + (ST25R3916_SPACE_B | 0x0CU) /*!< RW Correlator configuration register 1 */ +#define ST25R3916_REG_CORR_CONF2 \ + (ST25R3916_SPACE_B | 0x0DU) /*!< RW Correlator configuration register 2 */ + +/* Timer definition registers */ +#define ST25R3916_REG_MASK_RX_TIMER \ + 0x0FU /*!< RW Mask Receive Timer Register */ +#define ST25R3916_REG_NO_RESPONSE_TIMER1 \ + 0x10U /*!< RW No-response Timer Register 1 */ +#define ST25R3916_REG_NO_RESPONSE_TIMER2 \ + 0x11U /*!< RW No-response Timer Register 2 */ +#define ST25R3916_REG_TIMER_EMV_CONTROL \ + 0x12U /*!< RW Timer and EMV Control */ +#define ST25R3916_REG_GPT1 \ + 0x13U /*!< RW General Purpose Timer Register 1 */ +#define ST25R3916_REG_GPT2 \ + 0x14U /*!< RW General Purpose Timer Register 2 */ +#define ST25R3916_REG_PPON2 \ + 0x15U /*!< RW PPON2 Field waiting Timer Register */ +#define ST25R3916_REG_SQUELCH_TIMER \ + (ST25R3916_SPACE_B | 0x0FU) /*!< RW Squelch timeout Register */ +#define ST25R3916_REG_FIELD_ON_GT \ + (ST25R3916_SPACE_B | 0x15U) /*!< RW NFC Field on guard time */ + +/* Interrupt and associated reporting registers */ +#define ST25R3916_REG_IRQ_MASK_MAIN \ + 0x16U /*!< RW Mask Main Interrupt Register */ +#define ST25R3916_REG_IRQ_MASK_TIMER_NFC \ + 0x17U /*!< RW Mask Timer and NFC Interrupt Register */ +#define ST25R3916_REG_IRQ_MASK_ERROR_WUP \ + 0x18U /*!< RW Mask Error and Wake-up Interrupt Register */ +#define ST25R3916_REG_IRQ_MASK_TARGET \ + 0x19U /*!< RW Mask 3916 Target Interrupt Register */ +#define ST25R3916_REG_IRQ_MAIN \ + 0x1AU /*!< R Main Interrupt Register */ +#define ST25R3916_REG_IRQ_TIMER_NFC \ + 0x1BU /*!< R Timer and NFC Interrupt Register */ +#define ST25R3916_REG_IRQ_ERROR_WUP \ + 0x1CU /*!< R Error and Wake-up Interrupt Register */ +#define ST25R3916_REG_IRQ_TARGET \ + 0x1DU /*!< R ST25R3916 Target Interrupt Register */ +#define ST25R3916_REG_FIFO_STATUS1 \ + 0x1EU /*!< R FIFO Status Register 1 */ +#define ST25R3916_REG_FIFO_STATUS2 \ + 0x1FU /*!< R FIFO Status Register 2 */ +#define ST25R3916_REG_COLLISION_STATUS \ + 0x20U /*!< R Collision Display Register */ +#define ST25R3916_REG_PASSIVE_TARGET_STATUS \ + 0x21U /*!< R Passive target state status */ + +/* Definition of number of transmitted bytes */ +#define ST25R3916_REG_NUM_TX_BYTES1 \ + 0x22U /*!< RW Number of Transmitted Bytes Register 1 */ +#define ST25R3916_REG_NUM_TX_BYTES2 \ + 0x23U /*!< RW Number of Transmitted Bytes Register 2 */ + +/* NFCIP Bit Rate Display Register */ +#define ST25R3916_REG_NFCIP1_BIT_RATE \ + 0x24U /*!< R NFCIP Bit Rate Detection Display Register */ + +/* A/D Converter Output Register */ +#define ST25R3916_REG_AD_RESULT \ + 0x25U /*!< R A/D Converter Output Register */ + +/* Antenna tuning registers */ +#define ST25R3916_REG_ANT_TUNE_A \ + 0x26U /*!< RW Antenna Tuning Control (AAT-A) Register 1 */ +#define ST25R3916_REG_ANT_TUNE_B \ + 0x27U /*!< RW Antenna Tuning Control (AAT-B) Register 2 */ + +/* Antenna Driver and Modulation registers */ +#define ST25R3916_REG_TX_DRIVER \ + 0x28U /*!< RW TX driver register */ +#define ST25R3916_REG_PT_MOD \ + 0x29U /*!< RW PT modulation Register */ +#define ST25R3916_REG_AUX_MOD \ + (ST25R3916_SPACE_B | 0x28U) /*!< RW Aux Modulation setting Register */ +#define ST25R3916_REG_TX_DRIVER_TIMING \ + (ST25R3916_SPACE_B | 0x29U) /*!< RW TX driver timing Register */ +#define ST25R3916_REG_RES_AM_MOD \ + (ST25R3916_SPACE_B | 0x2AU) /*!< RW Resistive AM modulation register */ +#define ST25R3916_REG_TX_DRIVER_STATUS \ + (ST25R3916_SPACE_B | 0x2BU) /*!< R TX driver timing readout Register */ + +/* External Field Detector Threshold Registers */ +#define ST25R3916_REG_FIELD_THRESHOLD_ACTV \ + 0x2AU /*!< RW External Field Detector Activation Threshold Reg */ +#define ST25R3916_REG_FIELD_THRESHOLD_DEACTV \ + 0x2BU /*!< RW External Field Detector Deactivation Threshold Reg*/ + +/* Regulator registers */ +#define ST25R3916_REG_REGULATOR_CONTROL \ + 0x2CU /*!< RW Regulated Voltage Control Register */ +#define ST25R3916_REG_REGULATOR_RESULT \ + (ST25R3916_SPACE_B | 0x2CU) /*!< R Regulator Display Register */ + +/* Receiver State Display Register */ +#define ST25R3916_REG_RSSI_RESULT \ + 0x2DU /*!< R RSSI Display Register */ +#define ST25R3916_REG_GAIN_RED_STATE \ + 0x2EU /*!< R Gain Reduction State Register */ +#define ST25R3916_REG_CAP_SENSOR_CONTROL \ + 0x2FU /*!< RW Capacitive Sensor Control Register */ +#define ST25R3916_REG_CAP_SENSOR_RESULT \ + 0x30U /*!< R Capacitive Sensor Display Register */ +#define ST25R3916_REG_AUX_DISPLAY \ + 0x31U /*!< R Auxiliary Display Register */ + +/* Over/Undershoot Protection Configuration Registers */ +#define ST25R3916_REG_OVERSHOOT_CONF1 \ + (ST25R3916_SPACE_B | \ + 0x30U) /*!< RW Overshoot Protection Configuration Register 1 */ +#define ST25R3916_REG_OVERSHOOT_CONF2 \ + (ST25R3916_SPACE_B | \ + 0x31U) /*!< RW Overshoot Protection Configuration Register 2 */ +#define ST25R3916_REG_UNDERSHOOT_CONF1 \ + (ST25R3916_SPACE_B | \ + 0x32U) /*!< RW Undershoot Protection Configuration Register 1 */ +#define ST25R3916_REG_UNDERSHOOT_CONF2 \ + (ST25R3916_SPACE_B | \ + 0x33U) /*!< RW Undershoot Protection Configuration Register 2 */ + +#ifdef ST25R3916B +/* AWS Configuration Registers */ +#define ST25R3916_REG_AWS_CONF1 \ + (ST25R3916_SPACE_B | 0x2EU) /*!< RW AWS Configuration Register 1 */ +#define ST25R3916_REG_AWS_CONF2 \ + (ST25R3916_SPACE_B | 0x2FU) /*!< RW AWS Configuration Register 2 */ +#define ST25R3916_REG_AWS_TIME1 \ + (ST25R3916_SPACE_B | 0x34U) /*!< RW AWS Time Register 1 */ +#define ST25R3916_REG_AWS_TIME2 \ + (ST25R3916_SPACE_B | 0x35U) /*!< RW AWS Time Register 2 */ +#define ST25R3916_REG_AWS_TIME3 \ + (ST25R3916_SPACE_B | 0x36U) /*!< RW AWS Time Register 1 */ +#define ST25R3916_REG_AWS_TIME4 \ + (ST25R3916_SPACE_B | 0x37U) /*!< RW AWS Time Register 2 */ +#define ST25R3916_REG_AWS_TIME5 \ + (ST25R3916_SPACE_B | 0x38U) /*!< RW AWS Time Register 1 */ +#define ST25R3916_REG_AWS_RC_CAL \ + (ST25R3916_SPACE_B | 0x39U) /*!< RW AWS Time Register 2 */ +#endif /* ST25R3916B */ + +/* Detection of card presence */ +#define ST25R3916_REG_WUP_TIMER_CONTROL \ + 0x32U /*!< RW Wake-up Timer Control Register */ +#define ST25R3916_REG_AMPLITUDE_MEASURE_CONF \ + 0x33U /*!< RW Amplitude Measurement Configuration Register */ +#define ST25R3916_REG_AMPLITUDE_MEASURE_REF \ + 0x34U /*!< RW Amplitude Measurement Reference Register */ +#define ST25R3916_REG_AMPLITUDE_MEASURE_AA_RESULT \ + 0x35U /*!< R Amplitude Measurement Auto Averaging Display Reg */ +#define ST25R3916_REG_AMPLITUDE_MEASURE_RESULT \ + 0x36U /*!< R Amplitude Measurement Display Register */ +#define ST25R3916_REG_PHASE_MEASURE_CONF \ + 0x37U /*!< RW Phase Measurement Configuration Register */ +#define ST25R3916_REG_PHASE_MEASURE_REF \ + 0x38U /*!< RW Phase Measurement Reference Register */ +#define ST25R3916_REG_PHASE_MEASURE_AA_RESULT \ + 0x39U /*!< R Phase Measurement Auto Averaging Display Register */ +#define ST25R3916_REG_PHASE_MEASURE_RESULT \ + 0x3AU /*!< R Phase Measurement Display Register */ + +#if defined(ST25R3916) +#define ST25R3916_REG_CAPACITANCE_MEASURE_CONF \ + 0x3BU /*!< RW Capacitance Measurement Configuration Register */ +#define ST25R3916_REG_CAPACITANCE_MEASURE_REF \ + 0x3CU /*!< RW Capacitance Measurement Reference Register */ +#define ST25R3916_REG_CAPACITANCE_MEASURE_AA_RESULT \ + 0x3DU /*!< R Capacitance Measurement Auto Averaging Display Reg*/ +#define ST25R3916_REG_CAPACITANCE_MEASURE_RESULT \ + 0x3EU /*!< R Capacitance Measurement Display Register */ +#elif defined(ST25R3916B) +#define ST25R3916_REG_MEAS_TX_DELAY \ + 0x3BU /*!< RW Capacitance Measurement Configuration Register */ +#endif /* ST25R3916 */ + +/* IC identity */ +#define ST25R3916_REG_IC_IDENTITY \ + 0x3FU /*!< R Chip Id: 0 for old silicon, v2 silicon: 0x09 */ + +/*! Register bit definitions \cond DOXYGEN_SUPPRESS */ + +#define ST25R3916_REG_IO_CONF1_single (1U << 7) +#define ST25R3916_REG_IO_CONF1_rfo2 (1U << 6) +#define ST25R3916_REG_IO_CONF1_i2c_thd1 (1U << 5) +#define ST25R3916_REG_IO_CONF1_i2c_thd0 (1U << 4) +#define ST25R3916_REG_IO_CONF1_i2c_thd_mask (3U << 4) +#define ST25R3916_REG_IO_CONF1_i2c_thd_shift (4U) +#define ST25R3916_REG_IO_CONF1_rfu (1U << 3) +#define ST25R3916_REG_IO_CONF1_out_cl1 (1U << 2) +#define ST25R3916_REG_IO_CONF1_out_cl0 (1U << 1) +#define ST25R3916_REG_IO_CONF1_out_cl_disabled (3U << 1) +#define ST25R3916_REG_IO_CONF1_out_cl_13_56MHZ (2U << 1) +#define ST25R3916_REG_IO_CONF1_out_cl_4_78MHZ (1U << 1) +#define ST25R3916_REG_IO_CONF1_out_cl_3_39MHZ (0U << 1) +#define ST25R3916_REG_IO_CONF1_out_cl_mask (3U << 1) +#define ST25R3916_REG_IO_CONF1_out_cl_shift (1U) +#define ST25R3916_REG_IO_CONF1_lf_clk_off (1U << 0) +#define ST25R3916_REG_IO_CONF1_lf_clk_off_on (1U << 0) +#define ST25R3916_REG_IO_CONF1_lf_clk_off_off (0U << 0) + +#define ST25R3916_REG_IO_CONF2_sup3V (1U << 7) +#define ST25R3916_REG_IO_CONF2_sup3V_3V (1U << 7) +#define ST25R3916_REG_IO_CONF2_sup3V_5V (0U << 7) +#define ST25R3916_REG_IO_CONF2_vspd_off (1U << 6) +#define ST25R3916_REG_IO_CONF2_aat_en (1U << 5) +#define ST25R3916_REG_IO_CONF2_miso_pd2 (1U << 4) +#define ST25R3916_REG_IO_CONF2_miso_pd1 (1U << 3) +#define ST25R3916_REG_IO_CONF2_io_drv_lvl (1U << 2) +#if defined(ST25R3916) +#define ST25R3916_REG_IO_CONF2_slow_up (1U << 0) +#elif defined(ST25R3916B) +#define ST25R3916_REG_IO_CONF2_act_amsink (1U << 0) +#endif /* ST25R3916B */ + +#define ST25R3916_REG_OP_CONTROL_en (1U << 7) +#define ST25R3916_REG_OP_CONTROL_rx_en (1U << 6) +#define ST25R3916_REG_OP_CONTROL_rx_chn (1U << 5) +#define ST25R3916_REG_OP_CONTROL_rx_man (1U << 4) +#define ST25R3916_REG_OP_CONTROL_tx_en (1U << 3) +#define ST25R3916_REG_OP_CONTROL_wu (1U << 2) +#define ST25R3916_REG_OP_CONTROL_en_fd_c1 (1U << 1) +#define ST25R3916_REG_OP_CONTROL_en_fd_c0 (1U << 0) +#define ST25R3916_REG_OP_CONTROL_en_fd_efd_off (0U << 0) +#define ST25R3916_REG_OP_CONTROL_en_fd_manual_efd_ca (1U << 0) +#define ST25R3916_REG_OP_CONTROL_en_fd_manual_efd_pdt (2U << 0) +#define ST25R3916_REG_OP_CONTROL_en_fd_auto_efd (3U << 0) +#define ST25R3916_REG_OP_CONTROL_en_fd_shift (0U) +#define ST25R3916_REG_OP_CONTROL_en_fd_mask (3U << 0) + +#define ST25R3916_REG_MODE_targ (1U << 7) +#define ST25R3916_REG_MODE_targ_targ (1U << 7) +#define ST25R3916_REG_MODE_targ_init (0U << 7) +#define ST25R3916_REG_MODE_om3 (1U << 6) +#define ST25R3916_REG_MODE_om2 (1U << 5) +#define ST25R3916_REG_MODE_om1 (1U << 4) +#define ST25R3916_REG_MODE_om0 (1U << 3) +#define ST25R3916_REG_MODE_om_bpsk_stream (0xfU << 3) +#define ST25R3916_REG_MODE_om_subcarrier_stream (0xeU << 3) +#define ST25R3916_REG_MODE_om_topaz (0x4U << 3) +#define ST25R3916_REG_MODE_om_felica (0x3U << 3) +#define ST25R3916_REG_MODE_om_iso14443b (0x2U << 3) +#define ST25R3916_REG_MODE_om_iso14443a (0x1U << 3) +#define ST25R3916_REG_MODE_om_targ_nfca (0x1U << 3) +#define ST25R3916_REG_MODE_om_targ_nfcb (0x2U << 3) +#define ST25R3916_REG_MODE_om_targ_nfcf (0x4U << 3) +#define ST25R3916_REG_MODE_om_targ_nfcip (0x7U << 3) +#define ST25R3916_REG_MODE_om_nfc (0x0U << 3) +#define ST25R3916_REG_MODE_om_mask (0xfU << 3) +#define ST25R3916_REG_MODE_om_shift (3U) +#define ST25R3916_REG_MODE_tr_am (1U << 2) +#define ST25R3916_REG_MODE_tr_am_ook (0U << 2) +#define ST25R3916_REG_MODE_tr_am_am (1U << 2) +#define ST25R3916_REG_MODE_nfc_ar1 (1U << 1) +#define ST25R3916_REG_MODE_nfc_ar0 (1U << 0) +#define ST25R3916_REG_MODE_nfc_ar_off (0U << 0) +#define ST25R3916_REG_MODE_nfc_ar_auto_rx (1U << 0) +#define ST25R3916_REG_MODE_nfc_ar_eof (2U << 0) +#define ST25R3916_REG_MODE_nfc_ar_rfu (3U << 0) +#define ST25R3916_REG_MODE_nfc_ar_mask (3U << 0) +#define ST25R3916_REG_MODE_nfc_ar_shift (0U) + +#define ST25R3916_REG_BIT_RATE_txrate_106 (0x0U << 4) +#define ST25R3916_REG_BIT_RATE_txrate_212 (0x1U << 4) +#define ST25R3916_REG_BIT_RATE_txrate_424 (0x2U << 4) +#define ST25R3916_REG_BIT_RATE_txrate_848 (0x3U << 4) +#define ST25R3916_REG_BIT_RATE_txrate_mask (0x3U << 4) +#define ST25R3916_REG_BIT_RATE_txrate_shift (4U) +#define ST25R3916_REG_BIT_RATE_rxrate_106 (0x0U << 0) +#define ST25R3916_REG_BIT_RATE_rxrate_212 (0x1U << 0) +#define ST25R3916_REG_BIT_RATE_rxrate_424 (0x2U << 0) +#define ST25R3916_REG_BIT_RATE_rxrate_848 (0x3U << 0) +#define ST25R3916_REG_BIT_RATE_rxrate_mask (0x3U << 0) +#define ST25R3916_REG_BIT_RATE_rxrate_shift (0U) + +#define ST25R3916_REG_ISO14443A_NFC_no_tx_par (1U << 7) +#define ST25R3916_REG_ISO14443A_NFC_no_tx_par_off (0U << 7) +#define ST25R3916_REG_ISO14443A_NFC_no_rx_par (1U << 6) +#define ST25R3916_REG_ISO14443A_NFC_no_rx_par_off (0U << 6) +#define ST25R3916_REG_ISO14443A_NFC_nfc_f0 (1U << 5) +#define ST25R3916_REG_ISO14443A_NFC_nfc_f0_off (0U << 5) +#define ST25R3916_REG_ISO14443A_NFC_p_len3 (1U << 4) +#define ST25R3916_REG_ISO14443A_NFC_p_len2 (1U << 3) +#define ST25R3916_REG_ISO14443A_NFC_p_len1 (1U << 2) +#define ST25R3916_REG_ISO14443A_NFC_p_len0 (1U << 1) +#define ST25R3916_REG_ISO14443A_NFC_p_len_mask (0xfU << 1) +#define ST25R3916_REG_ISO14443A_NFC_p_len_shift (1U) +#define ST25R3916_REG_ISO14443A_NFC_antcl (1U << 0) + +#define ST25R3916_REG_EMD_SUP_CONF_emd_emv (1U << 7) +#define ST25R3916_REG_EMD_SUP_CONF_emd_emv_on (1U << 7) +#define ST25R3916_REG_EMD_SUP_CONF_emd_emv_off (0U << 7) +#define ST25R3916_REG_EMD_SUP_CONF_rx_start_emv (1U << 6) +#define ST25R3916_REG_EMD_SUP_CONF_rx_start_emv_on (1U << 6) +#define ST25R3916_REG_EMD_SUP_CONF_rx_start_emv_off (0U << 6) +#define ST25R3916_REG_EMD_SUP_CONF_rfu1 (1U << 5) +#define ST25R3916_REG_EMD_SUP_CONF_rfu0 (1U << 4) +#define ST25R3916_REG_EMD_SUP_CONF_emd_thld3 (1U << 3) +#define ST25R3916_REG_EMD_SUP_CONF_emd_thld2 (1U << 2) +#define ST25R3916_REG_EMD_SUP_CONF_emd_thld1 (1U << 1) +#define ST25R3916_REG_EMD_SUP_CONF_emd_thld0 (1U << 0) +#define ST25R3916_REG_EMD_SUP_CONF_emd_thld_mask (0xfU << 0) +#define ST25R3916_REG_EMD_SUP_CONF_emd_thld_shift (0U) + +#define ST25R3916_REG_SUBC_START_TIME_rfu2 (1U << 7) +#define ST25R3916_REG_SUBC_START_TIME_rfu1 (1U << 6) +#define ST25R3916_REG_SUBC_START_TIME_rfu0 (1U << 5) +#define ST25R3916_REG_SUBC_START_TIME_sst4 (1U << 4) +#define ST25R3916_REG_SUBC_START_TIME_sst3 (1U << 3) +#define ST25R3916_REG_SUBC_START_TIME_sst2 (1U << 2) +#define ST25R3916_REG_SUBC_START_TIME_sst1 (1U << 1) +#define ST25R3916_REG_SUBC_START_TIME_sst0 (1U << 0) +#define ST25R3916_REG_SUBC_START_TIME_sst_mask (0x1fU << 0) +#define ST25R3916_REG_SUBC_START_TIME_sst_shift (0U) + +#define ST25R3916_REG_ISO14443B_1_egt2 (1U << 7) +#define ST25R3916_REG_ISO14443B_1_egt1 (1U << 6) +#define ST25R3916_REG_ISO14443B_1_egt0 (1U << 5) +#define ST25R3916_REG_ISO14443B_1_egt_shift (5U) +#define ST25R3916_REG_ISO14443B_1_egt_mask (7U << 5) +#define ST25R3916_REG_ISO14443B_1_sof_1 (1U << 3) +#define ST25R3916_REG_ISO14443B_1_sof_1_3etu (1U << 3) +#define ST25R3916_REG_ISO14443B_1_sof_1_2etu (0U << 3) +#define ST25R3916_REG_ISO14443B_1_sof_0 (1U << 4) +#define ST25R3916_REG_ISO14443B_1_sof_0_11etu (1U << 4) +#define ST25R3916_REG_ISO14443B_1_sof_0_10etu (0U << 4) +#define ST25R3916_REG_ISO14443B_1_sof_mask (3U << 3) +#define ST25R3916_REG_ISO14443B_1_eof (1U << 2) +#define ST25R3916_REG_ISO14443B_1_eof_11etu (1U << 2) +#define ST25R3916_REG_ISO14443B_1_eof_10etu (0U << 2) +#define ST25R3916_REG_ISO14443B_1_half (1U << 1) +#if defined(ST25R3916) +#define ST25R3916_REG_ISO14443B_1_rx_st_om (1U << 0) +#elif defined(ST25R3916B) +#define ST25R3916_REG_ISO14443B_1_rfu0 (1U << 0) +#endif /* ST25R3916B */ + +#define ST25R3916_REG_ISO14443B_2_tr1_1 (1U << 7) +#define ST25R3916_REG_ISO14443B_2_tr1_0 (1U << 6) +#define ST25R3916_REG_ISO14443B_2_tr1_64fs32fs (1U << 6) +#define ST25R3916_REG_ISO14443B_2_tr1_80fs80fs (0U << 6) +#define ST25R3916_REG_ISO14443B_2_tr1_mask (3U << 6) +#define ST25R3916_REG_ISO14443B_2_tr1_shift (6U) +#define ST25R3916_REG_ISO14443B_2_no_sof (1U << 5) +#define ST25R3916_REG_ISO14443B_2_no_eof (1U << 4) +#define ST25R3916_REG_ISO14443B_rfu1 (1U << 3) +#define ST25R3916_REG_ISO14443B_rfu0 (1U << 2) +#define ST25R3916_REG_ISO14443B_2_f_p1 (1U << 1) +#define ST25R3916_REG_ISO14443B_2_f_p0 (1U << 0) +#define ST25R3916_REG_ISO14443B_2_f_p_96 (3U << 0) +#define ST25R3916_REG_ISO14443B_2_f_p_80 (2U << 0) +#define ST25R3916_REG_ISO14443B_2_f_p_64 (1U << 0) +#define ST25R3916_REG_ISO14443B_2_f_p_48 (0U << 0) +#define ST25R3916_REG_ISO14443B_2_f_p_mask (3U << 0) +#define ST25R3916_REG_ISO14443B_2_f_p_shift (0U) + +#define ST25R3916_REG_PASSIVE_TARGET_fdel_3 (1U << 7) +#define ST25R3916_REG_PASSIVE_TARGET_fdel_2 (1U << 6) +#define ST25R3916_REG_PASSIVE_TARGET_fdel_1 (1U << 5) +#define ST25R3916_REG_PASSIVE_TARGET_fdel_0 (1U << 4) +#define ST25R3916_REG_PASSIVE_TARGET_fdel_mask (0xfU << 4) +#define ST25R3916_REG_PASSIVE_TARGET_fdel_shift (4U) +#define ST25R3916_REG_PASSIVE_TARGET_d_ac_ap2p (1U << 3) +#define ST25R3916_REG_PASSIVE_TARGET_d_212_424_1r (1U << 2) +#define ST25R3916_REG_PASSIVE_TARGET_rfu (1U << 1) +#define ST25R3916_REG_PASSIVE_TARGET_d_106_ac_a (1U << 0) + +#define ST25R3916_REG_STREAM_MODE_rfu (1U << 7) +#define ST25R3916_REG_STREAM_MODE_scf1 (1U << 6) +#define ST25R3916_REG_STREAM_MODE_scf0 (1U << 5) +#define ST25R3916_REG_STREAM_MODE_scf_sc212 (0U << 5) +#define ST25R3916_REG_STREAM_MODE_scf_sc424 (1U << 5) +#define ST25R3916_REG_STREAM_MODE_scf_sc848 (2U << 5) +#define ST25R3916_REG_STREAM_MODE_scf_sc1695 (3U << 5) +#define ST25R3916_REG_STREAM_MODE_scf_bpsk848 (0U << 5) +#define ST25R3916_REG_STREAM_MODE_scf_bpsk1695 (1U << 5) +#define ST25R3916_REG_STREAM_MODE_scf_bpsk3390 (2U << 5) +#define ST25R3916_REG_STREAM_MODE_scf_bpsk106 (3U << 5) +#define ST25R3916_REG_STREAM_MODE_scf_mask (3U << 5) +#define ST25R3916_REG_STREAM_MODE_scf_shift (5U) +#define ST25R3916_REG_STREAM_MODE_scp1 (1U << 4) +#define ST25R3916_REG_STREAM_MODE_scp0 (1U << 3) +#define ST25R3916_REG_STREAM_MODE_scp_1pulse (0U << 3) +#define ST25R3916_REG_STREAM_MODE_scp_2pulses (1U << 3) +#define ST25R3916_REG_STREAM_MODE_scp_4pulses (2U << 3) +#define ST25R3916_REG_STREAM_MODE_scp_8pulses (3U << 3) +#define ST25R3916_REG_STREAM_MODE_scp_mask (3U << 3) +#define ST25R3916_REG_STREAM_MODE_scp_shift (3U) +#define ST25R3916_REG_STREAM_MODE_stx2 (1U << 2) +#define ST25R3916_REG_STREAM_MODE_stx1 (1U << 1) +#define ST25R3916_REG_STREAM_MODE_stx0 (1U << 0) +#define ST25R3916_REG_STREAM_MODE_stx_106 (0U << 0) +#define ST25R3916_REG_STREAM_MODE_stx_212 (1U << 0) +#define ST25R3916_REG_STREAM_MODE_stx_424 (2U << 0) +#define ST25R3916_REG_STREAM_MODE_stx_848 (3U << 0) +#define ST25R3916_REG_STREAM_MODE_stx_mask (7U << 0) +#define ST25R3916_REG_STREAM_MODE_stx_shift (0U) + +#define ST25R3916_REG_AUX_no_crc_rx (1U << 7) +#define ST25R3916_REG_AUX_rfu (1U << 6) +#define ST25R3916_REG_AUX_nfc_id1 (1U << 5) +#define ST25R3916_REG_AUX_nfc_id0 (1U << 4) +#define ST25R3916_REG_AUX_nfc_id_7bytes (1U << 4) +#define ST25R3916_REG_AUX_nfc_id_4bytes (0U << 4) +#define ST25R3916_REG_AUX_nfc_id_mask (3U << 4) +#define ST25R3916_REG_AUX_nfc_id_shift (4U) +#define ST25R3916_REG_AUX_mfaz_cl90 (1U << 3) +#define ST25R3916_REG_AUX_dis_corr (1U << 2) +#define ST25R3916_REG_AUX_dis_corr_coherent (1U << 2) +#define ST25R3916_REG_AUX_dis_corr_correlator (0U << 2) +#define ST25R3916_REG_AUX_nfc_n1 (1U << 1) +#define ST25R3916_REG_AUX_nfc_n0 (1U << 0) +#define ST25R3916_REG_AUX_nfc_n_mask (3U << 0) +#define ST25R3916_REG_AUX_nfc_n_shift (0U) + +#define ST25R3916_REG_RX_CONF1_ch_sel (1U << 7) +#define ST25R3916_REG_RX_CONF1_ch_sel_PM (1U << 7) +#define ST25R3916_REG_RX_CONF1_ch_sel_AM (0U << 7) +#define ST25R3916_REG_RX_CONF1_lp2 (1U << 6) +#define ST25R3916_REG_RX_CONF1_lp1 (1U << 5) +#define ST25R3916_REG_RX_CONF1_lp0 (1U << 4) +#define ST25R3916_REG_RX_CONF1_lp_1200khz (0U << 4) +#define ST25R3916_REG_RX_CONF1_lp_600khz (1U << 4) +#define ST25R3916_REG_RX_CONF1_lp_300khz (2U << 4) +#define ST25R3916_REG_RX_CONF1_lp_2000khz (4U << 4) +#define ST25R3916_REG_RX_CONF1_lp_7000khz (5U << 4) +#define ST25R3916_REG_RX_CONF1_lp_mask (7U << 4) +#define ST25R3916_REG_RX_CONF1_lp_shift (4U) +#define ST25R3916_REG_RX_CONF1_z600k (1U << 3) +#define ST25R3916_REG_RX_CONF1_h200 (1U << 2) +#define ST25R3916_REG_RX_CONF1_h80 (1U << 1) +#define ST25R3916_REG_RX_CONF1_z12k (1U << 0) +#define ST25R3916_REG_RX_CONF1_hz_60_400khz (0U << 0) +#define ST25R3916_REG_RX_CONF1_hz_60_200khz (4U << 0) +#define ST25R3916_REG_RX_CONF1_hz_40_80khz (2U << 0) +#define ST25R3916_REG_RX_CONF1_hz_12_200khz (1U << 0) +#define ST25R3916_REG_RX_CONF1_hz_12_80khz (3U << 0) +#define ST25R3916_REG_RX_CONF1_hz_12_200khz_alt (5U << 0) +#define ST25R3916_REG_RX_CONF1_hz_600_400khz (8U << 0) +#define ST25R3916_REG_RX_CONF1_hz_600_200khz (12U << 0) +#define ST25R3916_REG_RX_CONF1_hz_mask (0xfU << 0) +#define ST25R3916_REG_RX_CONF1_hz_shift (0U) + +#define ST25R3916_REG_RX_CONF2_demod_mode (1U << 7) +#define ST25R3916_REG_RX_CONF2_amd_sel (1U << 6) +#define ST25R3916_REG_RX_CONF2_amd_sel_mixer (1U << 6) +#define ST25R3916_REG_RX_CONF2_amd_sel_peak (0U << 6) +#define ST25R3916_REG_RX_CONF2_sqm_dyn (1U << 5) +#define ST25R3916_REG_RX_CONF2_pulz_61 (1U << 4) +#define ST25R3916_REG_RX_CONF2_agc_en (1U << 3) +#define ST25R3916_REG_RX_CONF2_agc_m (1U << 2) +#define ST25R3916_REG_RX_CONF2_agc_alg (1U << 1) +#define ST25R3916_REG_RX_CONF2_agc6_3 (1U << 0) + +#define ST25R3916_REG_RX_CONF3_rg1_am2 (1U << 7) +#define ST25R3916_REG_RX_CONF3_rg1_am1 (1U << 6) +#define ST25R3916_REG_RX_CONF3_rg1_am0 (1U << 5) +#define ST25R3916_REG_RX_CONF3_rg1_am_mask (0x7U << 5) +#define ST25R3916_REG_RX_CONF3_rg1_am_shift (5U) +#define ST25R3916_REG_RX_CONF3_rg1_pm2 (1U << 4) +#define ST25R3916_REG_RX_CONF3_rg1_pm1 (1U << 3) +#define ST25R3916_REG_RX_CONF3_rg1_pm0 (1U << 2) +#define ST25R3916_REG_RX_CONF3_rg1_pm_mask (0x7U << 2) +#define ST25R3916_REG_RX_CONF3_rg1_pm_shift (2U) +#define ST25R3916_REG_RX_CONF3_lf_en (1U << 1) +#define ST25R3916_REG_RX_CONF3_lf_op (1U << 0) + +#define ST25R3916_REG_RX_CONF4_rg2_am3 (1U << 7) +#define ST25R3916_REG_RX_CONF4_rg2_am2 (1U << 6) +#define ST25R3916_REG_RX_CONF4_rg2_am1 (1U << 5) +#define ST25R3916_REG_RX_CONF4_rg2_am0 (1U << 4) +#define ST25R3916_REG_RX_CONF4_rg2_am_mask (0xfU << 4) +#define ST25R3916_REG_RX_CONF4_rg2_am_shift (4U) +#define ST25R3916_REG_RX_CONF4_rg2_pm3 (1U << 3) +#define ST25R3916_REG_RX_CONF4_rg2_pm2 (1U << 2) +#define ST25R3916_REG_RX_CONF4_rg2_pm1 (1U << 1) +#define ST25R3916_REG_RX_CONF4_rg2_pm0 (1U << 0) +#define ST25R3916_REG_RX_CONF4_rg2_pm_mask (0xfU << 0) +#define ST25R3916_REG_RX_CONF4_rg2_pm_shift (0U) + +#define ST25R3916_REG_P2P_RX_CONF_ook_fd (1U << 7) +#define ST25R3916_REG_P2P_RX_CONF_ook_rc1 (1U << 6) +#define ST25R3916_REG_P2P_RX_CONF_ook_rc0 (1U << 5) +#define ST25R3916_REG_P2P_RX_CONF_ook_thd1 (1U << 4) +#define ST25R3916_REG_P2P_RX_CONF_ook_thd0 (1U << 3) +#define ST25R3916_REG_P2P_RX_CONF_ask_rc1 (1U << 2) +#define ST25R3916_REG_P2P_RX_CONF_ask_rc0 (1U << 1) +#define ST25R3916_REG_P2P_RX_CONF_ask_thd (1U << 0) + +#define ST25R3916_REG_CORR_CONF1_corr_s7 (1U << 7) +#define ST25R3916_REG_CORR_CONF1_corr_s6 (1U << 6) +#define ST25R3916_REG_CORR_CONF1_corr_s5 (1U << 5) +#define ST25R3916_REG_CORR_CONF1_corr_s4 (1U << 4) +#define ST25R3916_REG_CORR_CONF1_corr_s3 (1U << 3) +#define ST25R3916_REG_CORR_CONF1_corr_s2 (1U << 2) +#define ST25R3916_REG_CORR_CONF1_corr_s1 (1U << 1) +#define ST25R3916_REG_CORR_CONF1_corr_s0 (1U << 0) + +#define ST25R3916_REG_CORR_CONF2_rfu5 (1U << 7) +#define ST25R3916_REG_CORR_CONF2_rfu4 (1U << 6) +#define ST25R3916_REG_CORR_CONF2_rfu3 (1U << 5) +#define ST25R3916_REG_CORR_CONF2_rfu2 (1U << 4) +#define ST25R3916_REG_CORR_CONF2_rfu1 (1U << 3) +#define ST25R3916_REG_CORR_CONF2_rfu0 (1U << 2) +#define ST25R3916_REG_CORR_CONF2_corr_s9 (1U << 1) +#define ST25R3916_REG_CORR_CONF2_corr_s8 (1U << 0) + +#define ST25R3916_REG_TIMER_EMV_CONTROL_gptc2 (1U << 7) +#define ST25R3916_REG_TIMER_EMV_CONTROL_gptc1 (1U << 6) +#define ST25R3916_REG_TIMER_EMV_CONTROL_gptc0 (1U << 5) +#define ST25R3916_REG_TIMER_EMV_CONTROL_gptc_no_trigger (0U << 5) +#define ST25R3916_REG_TIMER_EMV_CONTROL_gptc_erx (1U << 5) +#define ST25R3916_REG_TIMER_EMV_CONTROL_gptc_srx (2U << 5) +#define ST25R3916_REG_TIMER_EMV_CONTROL_gptc_etx_nfc (3U << 5) +#define ST25R3916_REG_TIMER_EMV_CONTROL_gptc_mask (7U << 5) +#define ST25R3916_REG_TIMER_EMV_CONTROL_gptc_shift (5U) +#define ST25R3916_REG_TIMER_EMV_CONTROL_rfu (1U << 4) +#define ST25R3916_REG_TIMER_EMV_CONTROL_mrt_step (1U << 3) +#define ST25R3916_REG_TIMER_EMV_CONTROL_mrt_step_512 (1U << 3) +#define ST25R3916_REG_TIMER_EMV_CONTROL_mrt_step_64 (0U << 3) +#define ST25R3916_REG_TIMER_EMV_CONTROL_nrt_nfc (1U << 2) +#define ST25R3916_REG_TIMER_EMV_CONTROL_nrt_nfc_on (1U << 2) +#define ST25R3916_REG_TIMER_EMV_CONTROL_nrt_nfc_off (0U << 2) +#define ST25R3916_REG_TIMER_EMV_CONTROL_nrt_emv (1U << 1) +#define ST25R3916_REG_TIMER_EMV_CONTROL_nrt_emv_on (1U << 1) +#define ST25R3916_REG_TIMER_EMV_CONTROL_nrt_emv_off (0U << 1) +#define ST25R3916_REG_TIMER_EMV_CONTROL_nrt_step (1U << 0) +#define ST25R3916_REG_TIMER_EMV_CONTROL_nrt_step_64fc (0U << 0) +#define ST25R3916_REG_TIMER_EMV_CONTROL_nrt_step_4096_fc (1U << 0) + +#define ST25R3916_REG_FIFO_STATUS2_fifo_b9 (1U << 7) +#define ST25R3916_REG_FIFO_STATUS2_fifo_b8 (1U << 6) +#define ST25R3916_REG_FIFO_STATUS2_fifo_b_mask (3U << 6) +#define ST25R3916_REG_FIFO_STATUS2_fifo_b_shift (6U) +#define ST25R3916_REG_FIFO_STATUS2_fifo_unf (1U << 5) +#define ST25R3916_REG_FIFO_STATUS2_fifo_ovr (1U << 4) +#define ST25R3916_REG_FIFO_STATUS2_fifo_lb2 (1U << 3) +#define ST25R3916_REG_FIFO_STATUS2_fifo_lb1 (1U << 2) +#define ST25R3916_REG_FIFO_STATUS2_fifo_lb0 (1U << 1) +#define ST25R3916_REG_FIFO_STATUS2_fifo_lb_mask (7U << 1) +#define ST25R3916_REG_FIFO_STATUS2_fifo_lb_shift (1U) +#define ST25R3916_REG_FIFO_STATUS2_np_lb (1U << 0) + +#define ST25R3916_REG_COLLISION_STATUS_c_byte3 (1U << 7) +#define ST25R3916_REG_COLLISION_STATUS_c_byte2 (1U << 6) +#define ST25R3916_REG_COLLISION_STATUS_c_byte1 (1U << 5) +#define ST25R3916_REG_COLLISION_STATUS_c_byte0 (1U << 4) +#define ST25R3916_REG_COLLISION_STATUS_c_byte_mask (0xfU << 4) +#define ST25R3916_REG_COLLISION_STATUS_c_byte_shift (4U) +#define ST25R3916_REG_COLLISION_STATUS_c_bit2 (1U << 3) +#define ST25R3916_REG_COLLISION_STATUS_c_bit1 (1U << 2) +#define ST25R3916_REG_COLLISION_STATUS_c_bit0 (1U << 1) +#define ST25R3916_REG_COLLISION_STATUS_c_pb (1U << 0) +#define ST25R3916_REG_COLLISION_STATUS_c_bit_mask (3U << 1) +#define ST25R3916_REG_COLLISION_STATUS_c_bit_shift (1U) + +#define ST25R3916_REG_PASSIVE_TARGET_STATUS_rfu (1U << 7) +#define ST25R3916_REG_PASSIVE_TARGET_STATUS_rfu1 (1U << 6) +#define ST25R3916_REG_PASSIVE_TARGET_STATUS_rfu2 (1U << 5) +#define ST25R3916_REG_PASSIVE_TARGET_STATUS_rfu3 (1U << 4) +#define ST25R3916_REG_PASSIVE_TARGET_STATUS_pta_state3 (1U << 3) +#define ST25R3916_REG_PASSIVE_TARGET_STATUS_pta_state2 (1U << 2) +#define ST25R3916_REG_PASSIVE_TARGET_STATUS_pta_state1 (1U << 1) +#define ST25R3916_REG_PASSIVE_TARGET_STATUS_pta_state0 (1U << 0) +#define ST25R3916_REG_PASSIVE_TARGET_STATUS_pta_st_power_off (0x0U << 0) +#define ST25R3916_REG_PASSIVE_TARGET_STATUS_pta_st_idle (0x1U << 0) +#define ST25R3916_REG_PASSIVE_TARGET_STATUS_pta_st_ready_l1 (0x2U << 0) +#define ST25R3916_REG_PASSIVE_TARGET_STATUS_pta_st_ready_l2 (0x3U << 0) +#define ST25R3916_REG_PASSIVE_TARGET_STATUS_pta_st_rfu4 (0x4U << 0) +#define ST25R3916_REG_PASSIVE_TARGET_STATUS_pta_st_active (0x5U << 0) +#define ST25R3916_REG_PASSIVE_TARGET_STATUS_pta_st_rfu6 (0x6U << 0) +#define ST25R3916_REG_PASSIVE_TARGET_STATUS_pta_st_rfu7 (0x7U << 0) +#define ST25R3916_REG_PASSIVE_TARGET_STATUS_pta_st_rfu8 (0x8U << 0) +#define ST25R3916_REG_PASSIVE_TARGET_STATUS_pta_st_halt (0x9U << 0) +#define ST25R3916_REG_PASSIVE_TARGET_STATUS_pta_st_ready_l1_x (0xaU << 0) +#define ST25R3916_REG_PASSIVE_TARGET_STATUS_pta_st_ready_l2_x (0xbU << 0) +#define ST25R3916_REG_PASSIVE_TARGET_STATUS_pta_st_rfu12 (0xcU << 0) +#define ST25R3916_REG_PASSIVE_TARGET_STATUS_pta_st_active_x (0xdU << 0) +#define ST25R3916_REG_PASSIVE_TARGET_STATUS_pta_state_mask (0xfU << 0) +#define ST25R3916_REG_PASSIVE_TARGET_STATUS_pta_state_shift (0U) + +#define ST25R3916_REG_NUM_TX_BYTES2_ntx4 (1U << 7) +#define ST25R3916_REG_NUM_TX_BYTES2_ntx3 (1U << 6) +#define ST25R3916_REG_NUM_TX_BYTES2_ntx2 (1U << 5) +#define ST25R3916_REG_NUM_TX_BYTES2_ntx1 (1U << 4) +#define ST25R3916_REG_NUM_TX_BYTES2_ntx0 (1U << 3) +#define ST25R3916_REG_NUM_TX_BYTES2_ntx_mask (0x1fU << 3) +#define ST25R3916_REG_NUM_TX_BYTES2_ntx_shift (3U) +#define ST25R3916_REG_NUM_TX_BYTES2_nbtx2 (1U << 2) +#define ST25R3916_REG_NUM_TX_BYTES2_nbtx1 (1U << 1) +#define ST25R3916_REG_NUM_TX_BYTES2_nbtx0 (1U << 0) +#define ST25R3916_REG_NUM_TX_BYTES2_nbtx_mask (7U << 0) +#define ST25R3916_REG_NUM_TX_BYTES2_nbtx_shift (0U) + +#define ST25R3916_REG_NFCIP1_BIT_RATE_nfc_rfu1 (1U << 7) +#define ST25R3916_REG_NFCIP1_BIT_RATE_nfc_rfu0 (1U << 6) +#define ST25R3916_REG_NFCIP1_BIT_RATE_nfc_rate1 (1U << 5) +#define ST25R3916_REG_NFCIP1_BIT_RATE_nfc_rate0 (1U << 4) +#define ST25R3916_REG_NFCIP1_BIT_RATE_nfc_rate_mask (0x3U << 4) +#define ST25R3916_REG_NFCIP1_BIT_RATE_nfc_rate_shift (4U) +#define ST25R3916_REG_NFCIP1_BIT_RATE_ppt2_on (1U << 3) +#define ST25R3916_REG_NFCIP1_BIT_RATE_gpt_on (1U << 2) +#define ST25R3916_REG_NFCIP1_BIT_RATE_nrt_on (1U << 1) +#define ST25R3916_REG_NFCIP1_BIT_RATE_mrt_on (1U << 0) + +#define ST25R3916_REG_TX_DRIVER_am_mod3 (1U << 7) +#define ST25R3916_REG_TX_DRIVER_am_mod2 (1U << 6) +#define ST25R3916_REG_TX_DRIVER_am_mod1 (1U << 5) +#define ST25R3916_REG_TX_DRIVER_am_mod0 (1U << 4) +#if defined(ST25R3916) +#define ST25R3916_REG_TX_DRIVER_am_mod_5percent (0x0U << 4) +#define ST25R3916_REG_TX_DRIVER_am_mod_6percent (0x1U << 4) +#define ST25R3916_REG_TX_DRIVER_am_mod_7percent (0x2U << 4) +#define ST25R3916_REG_TX_DRIVER_am_mod_8percent (0x3U << 4) +#define ST25R3916_REG_TX_DRIVER_am_mod_9percent (0x4U << 4) +#define ST25R3916_REG_TX_DRIVER_am_mod_10percent (0x5U << 4) +#define ST25R3916_REG_TX_DRIVER_am_mod_11percent (0x6U << 4) +#define ST25R3916_REG_TX_DRIVER_am_mod_12percent (0x7U << 4) +#define ST25R3916_REG_TX_DRIVER_am_mod_13percent (0x8U << 4) +#define ST25R3916_REG_TX_DRIVER_am_mod_14percent (0x9U << 4) +#define ST25R3916_REG_TX_DRIVER_am_mod_15percent (0xaU << 4) +#define ST25R3916_REG_TX_DRIVER_am_mod_17percent (0xbU << 4) +#define ST25R3916_REG_TX_DRIVER_am_mod_19percent (0xcU << 4) +#define ST25R3916_REG_TX_DRIVER_am_mod_22percent (0xdU << 4) +#define ST25R3916_REG_TX_DRIVER_am_mod_26percent (0xeU << 4) +#define ST25R3916_REG_TX_DRIVER_am_mod_40percent (0xfU << 4) +#elif defined(ST25R3916B) +#define ST25R3916_REG_TX_DRIVER_am_mod_0percent (0x0U << 4) +#define ST25R3916_REG_TX_DRIVER_am_mod_8percent (0x1U << 4) +#define ST25R3916_REG_TX_DRIVER_am_mod_10percent (0x2U << 4) +#define ST25R3916_REG_TX_DRIVER_am_mod_11percent (0x3U << 4) +#define ST25R3916_REG_TX_DRIVER_am_mod_12percent (0x4U << 4) +#define ST25R3916_REG_TX_DRIVER_am_mod_13percent (0x5U << 4) +#define ST25R3916_REG_TX_DRIVER_am_mod_14percent (0x6U << 4) +#define ST25R3916_REG_TX_DRIVER_am_mod_15percent (0x7U << 4) +#define ST25R3916_REG_TX_DRIVER_am_mod_20percent (0x8U << 4) +#define ST25R3916_REG_TX_DRIVER_am_mod_25percent (0x9U << 4) +#define ST25R3916_REG_TX_DRIVER_am_mod_30percent (0xaU << 4) +#define ST25R3916_REG_TX_DRIVER_am_mod_40percent (0xbU << 4) +#define ST25R3916_REG_TX_DRIVER_am_mod_50percent (0xcU << 4) +#define ST25R3916_REG_TX_DRIVER_am_mod_60percent (0xdU << 4) +#define ST25R3916_REG_TX_DRIVER_am_mod_70percent (0xeU << 4) +#define ST25R3916_REG_TX_DRIVER_am_mod_82percent (0xfU << 4) +#endif +#define ST25R3916_REG_TX_DRIVER_am_mod_mask (0xfU << 4) +#define ST25R3916_REG_TX_DRIVER_am_mod_shift (4U) +#define ST25R3916_REG_TX_DRIVER_d_res3 (1U << 3) +#define ST25R3916_REG_TX_DRIVER_d_res2 (1U << 2) +#define ST25R3916_REG_TX_DRIVER_d_res1 (1U << 1) +#define ST25R3916_REG_TX_DRIVER_d_res0 (1U << 0) +#define ST25R3916_REG_TX_DRIVER_d_res_mask (0xfU << 0) +#define ST25R3916_REG_TX_DRIVER_d_res_shift (0U) + +#define ST25R3916_REG_PT_MOD_ptm_res3 (1U << 7) +#define ST25R3916_REG_PT_MOD_ptm_res2 (1U << 6) +#define ST25R3916_REG_PT_MOD_ptm_res1 (1U << 5) +#define ST25R3916_REG_PT_MOD_ptm_res0 (1U << 4) +#define ST25R3916_REG_PT_MOD_ptm_res_mask (0xfU << 4) +#define ST25R3916_REG_PT_MOD_ptm_res_shift (4U) +#define ST25R3916_REG_PT_MOD_pt_res3 (1U << 3) +#define ST25R3916_REG_PT_MOD_pt_res2 (1U << 2) +#define ST25R3916_REG_PT_MOD_pt_res1 (1U << 1) +#define ST25R3916_REG_PT_MOD_pt_res0 (1U << 0) +#define ST25R3916_REG_PT_MOD_pt_res_mask (0xfU << 0) +#define ST25R3916_REG_PT_MOD_pt_res_shift (0U) + +#define ST25R3916_REG_AUX_MOD_dis_reg_am (1U << 7) +#define ST25R3916_REG_AUX_MOD_lm_ext_pol (1U << 6) +#define ST25R3916_REG_AUX_MOD_lm_ext (1U << 5) +#define ST25R3916_REG_AUX_MOD_lm_dri (1U << 4) +#define ST25R3916_REG_AUX_MOD_res_am (1U << 3) +#if defined(ST25R3916) +#define ST25R3916_REG_AUX_MOD_rfu2 (1U << 2) +#elif defined(ST25R3916B) +#define ST25R3916_REG_AUX_MOD_rgs_am (1U << 2) +#endif /* ST25R3916B */ +#define ST25R3916_REG_AUX_MOD_rfu1 (1U << 1) +#define ST25R3916_REG_AUX_MOD_rfu0 (1U << 0) + +#define ST25R3916_REG_TX_DRIVER_TIMING_d_rat_t3 (1U << 7) +#define ST25R3916_REG_TX_DRIVER_TIMING_d_rat_t2 (1U << 6) +#define ST25R3916_REG_TX_DRIVER_TIMING_d_rat_t1 (1U << 5) +#define ST25R3916_REG_TX_DRIVER_TIMING_d_rat_t0 (1U << 4) +#define ST25R3916_REG_TX_DRIVER_TIMING_d_rat_mask (0xfU << 4) +#define ST25R3916_REG_TX_DRIVER_TIMING_d_rat_shift (4U) +#define ST25R3916_REG_TX_DRIVER_TIMING_rfu (1U << 3) +#define ST25R3916_REG_TX_DRIVER_TIMING_d_tim_m2 (1U << 2) +#define ST25R3916_REG_TX_DRIVER_TIMING_d_tim_m1 (1U << 1) +#define ST25R3916_REG_TX_DRIVER_TIMING_d_tim_m0 (1U << 0) +#define ST25R3916_REG_TX_DRIVER_TIMING_d_tim_m_mask (0x7U << 0) +#define ST25R3916_REG_TX_DRIVER_TIMING_d_tim_m_shift (0U) + +#define ST25R3916_REG_RES_AM_MOD_fa3_f (1U << 7) +#define ST25R3916_REG_RES_AM_MOD_md_res6 (1U << 6) +#define ST25R3916_REG_RES_AM_MOD_md_res5 (1U << 5) +#define ST25R3916_REG_RES_AM_MOD_md_res4 (1U << 4) +#define ST25R3916_REG_RES_AM_MOD_md_res3 (1U << 3) +#define ST25R3916_REG_RES_AM_MOD_md_res2 (1U << 2) +#define ST25R3916_REG_RES_AM_MOD_md_res1 (1U << 1) +#define ST25R3916_REG_RES_AM_MOD_md_res0 (1U << 0) +#define ST25R3916_REG_RES_AM_MOD_md_res_mask (0x7FU << 0) +#define ST25R3916_REG_RES_AM_MOD_md_res_shift (0U) + +#define ST25R3916_REG_TX_DRIVER_STATUS_d_rat_r3 (1U << 7) +#define ST25R3916_REG_TX_DRIVER_STATUS_d_rat_r2 (1U << 6) +#define ST25R3916_REG_TX_DRIVER_STATUS_d_rat_r1 (1U << 5) +#define ST25R3916_REG_TX_DRIVER_STATUS_d_rat_r0 (1U << 4) +#define ST25R3916_REG_TX_DRIVER_STATUS_d_rat_mask (0xfU << 4) +#define ST25R3916_REG_TX_DRIVER_STATUS_d_rat_shift (4U) +#define ST25R3916_REG_TX_DRIVER_STATUS_rfu (1U << 3) +#define ST25R3916_REG_TX_DRIVER_STATUS_d_tim_r2 (1U << 2) +#define ST25R3916_REG_TX_DRIVER_STATUS_d_tim_r1 (1U << 1) +#define ST25R3916_REG_TX_DRIVER_STATUS_d_tim_r0 (1U << 0) +#define ST25R3916_REG_TX_DRIVER_STATUS_d_tim_mask (0x7U << 0) +#define ST25R3916_REG_TX_DRIVER_STATUS_d_tim_shift (0U) + +#define ST25R3916_REG_FIELD_THRESHOLD_ACTV_trg_l2a (1U << 6) +#define ST25R3916_REG_FIELD_THRESHOLD_ACTV_trg_l1a (1U << 5) +#define ST25R3916_REG_FIELD_THRESHOLD_ACTV_trg_l0a (1U << 4) +#define ST25R3916_REG_FIELD_THRESHOLD_ACTV_trg_75mV (0x0U << 4) +#define ST25R3916_REG_FIELD_THRESHOLD_ACTV_trg_105mV (0x1U << 4) +#define ST25R3916_REG_FIELD_THRESHOLD_ACTV_trg_150mV (0x2U << 4) +#define ST25R3916_REG_FIELD_THRESHOLD_ACTV_trg_205mV (0x3U << 4) +#define ST25R3916_REG_FIELD_THRESHOLD_ACTV_trg_290mV (0x4U << 4) +#define ST25R3916_REG_FIELD_THRESHOLD_ACTV_trg_400mV (0x5U << 4) +#define ST25R3916_REG_FIELD_THRESHOLD_ACTV_trg_560mV (0x6U << 4) +#define ST25R3916_REG_FIELD_THRESHOLD_ACTV_trg_800mV (0x7U << 4) +#define ST25R3916_REG_FIELD_THRESHOLD_ACTV_trg_mask (7U << 4) +#define ST25R3916_REG_FIELD_THRESHOLD_ACTV_trg_shift (4U) +#define ST25R3916_REG_FIELD_THRESHOLD_ACTV_rfe_t3a (1U << 3) +#define ST25R3916_REG_FIELD_THRESHOLD_ACTV_rfe_t2a (1U << 2) +#define ST25R3916_REG_FIELD_THRESHOLD_ACTV_rfe_t1a (1U << 1) +#define ST25R3916_REG_FIELD_THRESHOLD_ACTV_rfe_t0a (1U << 0) +#define ST25R3916_REG_FIELD_THRESHOLD_ACTV_rfe_75mV (0x0U << 0) +#define ST25R3916_REG_FIELD_THRESHOLD_ACTV_rfe_105mV (0x1U << 0) +#define ST25R3916_REG_FIELD_THRESHOLD_ACTV_rfe_150mV (0x2U << 0) +#define ST25R3916_REG_FIELD_THRESHOLD_ACTV_rfe_205mV (0x3U << 0) +#define ST25R3916_REG_FIELD_THRESHOLD_ACTV_rfe_290mV (0x4U << 0) +#define ST25R3916_REG_FIELD_THRESHOLD_ACTV_rfe_400mV (0x5U << 0) +#define ST25R3916_REG_FIELD_THRESHOLD_ACTV_rfe_560mV (0x6U << 0) +#define ST25R3916_REG_FIELD_THRESHOLD_ACTV_rfe_800mV (0x7U << 0) +#define ST25R3916_REG_FIELD_THRESHOLD_ACTV_rfe_25mV (0x8U << 0) +#define ST25R3916_REG_FIELD_THRESHOLD_ACTV_rfe_33mV (0x9U << 0) +#define ST25R3916_REG_FIELD_THRESHOLD_ACTV_rfe_47mV (0xAU << 0) +#define ST25R3916_REG_FIELD_THRESHOLD_ACTV_rfe_64mV (0xBU << 0) +#define ST25R3916_REG_FIELD_THRESHOLD_ACTV_rfe_90mV (0xCU << 0) +#define ST25R3916_REG_FIELD_THRESHOLD_ACTV_rfe_125mV (0xDU << 0) +#define ST25R3916_REG_FIELD_THRESHOLD_ACTV_rfe_175mV (0xEU << 0) +#define ST25R3916_REG_FIELD_THRESHOLD_ACTV_rfe_250mV (0xFU << 0) +#define ST25R3916_REG_FIELD_THRESHOLD_ACTV_rfe_mask (0xfU << 0) +#define ST25R3916_REG_FIELD_THRESHOLD_ACTV_rfe_shift (0U) + +#define ST25R3916_REG_FIELD_THRESHOLD_DEACTV_trg_l2d (1U << 6) +#define ST25R3916_REG_FIELD_THRESHOLD_DEACTV_trg_l1d (1U << 5) +#define ST25R3916_REG_FIELD_THRESHOLD_DEACTV_trg_l0d (1U << 4) +#define ST25R3916_REG_FIELD_THRESHOLD_DEACTV_trg_75mV (0x0U << 4) +#define ST25R3916_REG_FIELD_THRESHOLD_DEACTV_trg_105mV (0x1U << 4) +#define ST25R3916_REG_FIELD_THRESHOLD_DEACTV_trg_150mV (0x2U << 4) +#define ST25R3916_REG_FIELD_THRESHOLD_DEACTV_trg_205mV (0x3U << 4) +#define ST25R3916_REG_FIELD_THRESHOLD_DEACTV_trg_290mV (0x4U << 4) +#define ST25R3916_REG_FIELD_THRESHOLD_DEACTV_trg_400mV (0x5U << 4) +#define ST25R3916_REG_FIELD_THRESHOLD_DEACTV_trg_560mV (0x6U << 4) +#define ST25R3916_REG_FIELD_THRESHOLD_DEACTV_trg_800mV (0x7U << 4) +#define ST25R3916_REG_FIELD_THRESHOLD_DEACTV_trg_mask (7U << 4) +#define ST25R3916_REG_FIELD_THRESHOLD_DEACTV_trg_shift (4U) +#define ST25R3916_REG_FIELD_THRESHOLD_DEACTV_rfe_t3d (1U << 3) +#define ST25R3916_REG_FIELD_THRESHOLD_DEACTV_rfe_t2d (1U << 2) +#define ST25R3916_REG_FIELD_THRESHOLD_DEACTV_rfe_t1d (1U << 1) +#define ST25R3916_REG_FIELD_THRESHOLD_DEACTV_rfe_t0d (1U << 0) +#define ST25R3916_REG_FIELD_THRESHOLD_DEACTV_rfe_75mV (0x0U << 0) +#define ST25R3916_REG_FIELD_THRESHOLD_DEACTV_rfe_105mV (0x1U << 0) +#define ST25R3916_REG_FIELD_THRESHOLD_DEACTV_rfe_150mV (0x2U << 0) +#define ST25R3916_REG_FIELD_THRESHOLD_DEACTV_rfe_205mV (0x3U << 0) +#define ST25R3916_REG_FIELD_THRESHOLD_DEACTV_rfe_290mV (0x4U << 0) +#define ST25R3916_REG_FIELD_THRESHOLD_DEACTV_rfe_400mV (0x5U << 0) +#define ST25R3916_REG_FIELD_THRESHOLD_DEACTV_rfe_560mV (0x6U << 0) +#define ST25R3916_REG_FIELD_THRESHOLD_DEACTV_rfe_800mV (0x7U << 0) +#define ST25R3916_REG_FIELD_THRESHOLD_DEACTV_rfe_25mV (0x8U << 0) +#define ST25R3916_REG_FIELD_THRESHOLD_DEACTV_rfe_33mV (0x9U << 0) +#define ST25R3916_REG_FIELD_THRESHOLD_DEACTV_rfe_47mV (0xAU << 0) +#define ST25R3916_REG_FIELD_THRESHOLD_DEACTV_rfe_64mV (0xBU << 0) +#define ST25R3916_REG_FIELD_THRESHOLD_DEACTV_rfe_90mV (0xCU << 0) +#define ST25R3916_REG_FIELD_THRESHOLD_DEACTV_rfe_125mV (0xDU << 0) +#define ST25R3916_REG_FIELD_THRESHOLD_DEACTV_rfe_175mV (0xEU << 0) +#define ST25R3916_REG_FIELD_THRESHOLD_DEACTV_rfe_250mV (0xFU << 0) +#define ST25R3916_REG_FIELD_THRESHOLD_DEACTV_rfe_mask (0xfU << 0) +#define ST25R3916_REG_FIELD_THRESHOLD_DEACTV_rfe_shift (0U) + +#define ST25R3916_REG_REGULATOR_CONTROL_reg_s (1U << 7) +#define ST25R3916_REG_REGULATOR_CONTROL_rege_3 (1U << 6) +#define ST25R3916_REG_REGULATOR_CONTROL_rege_2 (1U << 5) +#define ST25R3916_REG_REGULATOR_CONTROL_rege_1 (1U << 4) +#define ST25R3916_REG_REGULATOR_CONTROL_rege_0 (1U << 3) +#define ST25R3916_REG_REGULATOR_CONTROL_rege_mask (0xfU << 3) +#define ST25R3916_REG_REGULATOR_CONTROL_rege_shift (3U) +#define ST25R3916_REG_REGULATOR_CONTROL_mpsv2 (2U << 2) +#define ST25R3916_REG_REGULATOR_CONTROL_mpsv1 (1U << 1) +#define ST25R3916_REG_REGULATOR_CONTROL_mpsv0 (1U << 0) +#define ST25R3916_REG_REGULATOR_CONTROL_mpsv_vdd (0U) +#define ST25R3916_REG_REGULATOR_CONTROL_mpsv_vdd_a (1U) +#define ST25R3916_REG_REGULATOR_CONTROL_mpsv_vdd_d (2U) +#define ST25R3916_REG_REGULATOR_CONTROL_mpsv_vdd_rf (3U) +#define ST25R3916_REG_REGULATOR_CONTROL_mpsv_vdd_am (4U) +#define ST25R3916_REG_REGULATOR_CONTROL_rfu (5U) +#define ST25R3916_REG_REGULATOR_CONTROL_rfu1 (6U) +#define ST25R3916_REG_REGULATOR_CONTROL_rfu2 (7U) +#define ST25R3916_REG_REGULATOR_CONTROL_mpsv_mask (7U) +#define ST25R3916_REG_REGULATOR_CONTROL_mpsv_shift (0U) + +#define ST25R3916_REG_REGULATOR_RESULT_reg_3 (1U << 7) +#define ST25R3916_REG_REGULATOR_RESULT_reg_2 (1U << 6) +#define ST25R3916_REG_REGULATOR_RESULT_reg_1 (1U << 5) +#define ST25R3916_REG_REGULATOR_RESULT_reg_0 (1U << 4) +#define ST25R3916_REG_REGULATOR_RESULT_reg_mask (0xfU << 4) +#define ST25R3916_REG_REGULATOR_RESULT_reg_shift (4U) +#define ST25R3916_REG_REGULATOR_RESULT_i_lim (1U << 0) + +#define ST25R3916_REG_RSSI_RESULT_rssi_am_3 (1U << 7) +#define ST25R3916_REG_RSSI_RESULT_rssi_am_2 (1U << 6) +#define ST25R3916_REG_RSSI_RESULT_rssi_am_1 (1U << 5) +#define ST25R3916_REG_RSSI_RESULT_rssi_am_0 (1U << 4) +#define ST25R3916_REG_RSSI_RESULT_rssi_am_mask (0xfU << 4) +#define ST25R3916_REG_RSSI_RESULT_rssi_am_shift (4U) +#define ST25R3916_REG_RSSI_RESULT_rssi_pm3 (1U << 3) +#define ST25R3916_REG_RSSI_RESULT_rssi_pm2 (1U << 2) +#define ST25R3916_REG_RSSI_RESULT_rssi_pm1 (1U << 1) +#define ST25R3916_REG_RSSI_RESULT_rssi_pm0 (1U << 0) +#define ST25R3916_REG_RSSI_RESULT_rssi_pm_mask (0xfU << 0) +#define ST25R3916_REG_RSSI_RESULT_rssi_pm_shift (0U) + +#define ST25R3916_REG_GAIN_RED_STATE_gs_am_3 (1U << 7) +#define ST25R3916_REG_GAIN_RED_STATE_gs_am_2 (1U << 6) +#define ST25R3916_REG_GAIN_RED_STATE_gs_am_1 (1U << 5) +#define ST25R3916_REG_GAIN_RED_STATE_gs_am_0 (1U << 4) +#define ST25R3916_REG_GAIN_RED_STATE_gs_am_mask (0xfU << 4) +#define ST25R3916_REG_GAIN_RED_STATE_gs_am_shift (4U) +#define ST25R3916_REG_GAIN_RED_STATE_gs_pm_3 (1U << 3) +#define ST25R3916_REG_GAIN_RED_STATE_gs_pm_2 (1U << 2) +#define ST25R3916_REG_GAIN_RED_STATE_gs_pm_1 (1U << 1) +#define ST25R3916_REG_GAIN_RED_STATE_gs_pm_0 (1U << 0) +#define ST25R3916_REG_GAIN_RED_STATE_gs_pm_mask (0xfU << 0) +#define ST25R3916_REG_GAIN_RED_STATE_gs_pm_shift (0U) + +#define ST25R3916_REG_CAP_SENSOR_CONTROL_cs_mcal4 (1U << 7) +#define ST25R3916_REG_CAP_SENSOR_CONTROL_cs_mcal3 (1U << 6) +#define ST25R3916_REG_CAP_SENSOR_CONTROL_cs_mcal2 (1U << 5) +#define ST25R3916_REG_CAP_SENSOR_CONTROL_cs_mcal1 (1U << 4) +#define ST25R3916_REG_CAP_SENSOR_CONTROL_cs_mcal0 (1U << 3) +#define ST25R3916_REG_CAP_SENSOR_CONTROL_cs_mcal_mask (0x1fU << 3) +#define ST25R3916_REG_CAP_SENSOR_CONTROL_cs_mcal_shift (3U) +#define ST25R3916_REG_CAP_SENSOR_CONTROL_cs_g2 (1U << 2) +#define ST25R3916_REG_CAP_SENSOR_CONTROL_cs_g1 (1U << 1) +#define ST25R3916_REG_CAP_SENSOR_CONTROL_cs_g0 (1U << 0) +#define ST25R3916_REG_CAP_SENSOR_CONTROL_cs_g_mask (7U << 0) +#define ST25R3916_REG_CAP_SENSOR_CONTROL_cs_g_shift (0U) + +#define ST25R3916_REG_CAP_SENSOR_RESULT_cs_cal4 (1U << 7) +#define ST25R3916_REG_CAP_SENSOR_RESULT_cs_cal3 (1U << 6) +#define ST25R3916_REG_CAP_SENSOR_RESULT_cs_cal2 (1U << 5) +#define ST25R3916_REG_CAP_SENSOR_RESULT_cs_cal1 (1U << 4) +#define ST25R3916_REG_CAP_SENSOR_RESULT_cs_cal0 (1U << 3) +#define ST25R3916_REG_CAP_SENSOR_RESULT_cs_cal_mask (0x1fU << 3) +#define ST25R3916_REG_CAP_SENSOR_RESULT_cs_cal_shift (3U) +#define ST25R3916_REG_CAP_SENSOR_RESULT_cs_cal_end (1U << 2) +#define ST25R3916_REG_CAP_SENSOR_RESULT_cs_cal_err (1U << 1) + +#define ST25R3916_REG_AUX_DISPLAY_a_cha (1U << 7) +#define ST25R3916_REG_AUX_DISPLAY_efd_o (1U << 6) +#define ST25R3916_REG_AUX_DISPLAY_tx_on (1U << 5) +#define ST25R3916_REG_AUX_DISPLAY_osc_ok (1U << 4) +#define ST25R3916_REG_AUX_DISPLAY_rx_on (1U << 3) +#define ST25R3916_REG_AUX_DISPLAY_rx_act (1U << 2) +#define ST25R3916_REG_AUX_DISPLAY_en_peer (1U << 1) +#define ST25R3916_REG_AUX_DISPLAY_en_ac (1U << 0) + +#define ST25R3916_REG_OVERSHOOT_CONF1_ov_tx_mode1 (1U << 7) +#define ST25R3916_REG_OVERSHOOT_CONF1_ov_tx_mode0 (1U << 6) +#define ST25R3916_REG_OVERSHOOT_CONF1_ov_pattern13 (1U << 5) +#define ST25R3916_REG_OVERSHOOT_CONF1_ov_pattern12 (1U << 4) +#define ST25R3916_REG_OVERSHOOT_CONF1_ov_pattern11 (1U << 3) +#define ST25R3916_REG_OVERSHOOT_CONF1_ov_pattern10 (1U << 2) +#define ST25R3916_REG_OVERSHOOT_CONF1_ov_pattern9 (1U << 1) +#define ST25R3916_REG_OVERSHOOT_CONF1_ov_pattern8 (1U << 0) + +#define ST25R3916_REG_OVERSHOOT_CONF2_ov_pattern7 (1U << 7) +#define ST25R3916_REG_OVERSHOOT_CONF2_ov_pattern6 (1U << 6) +#define ST25R3916_REG_OVERSHOOT_CONF2_ov_pattern5 (1U << 5) +#define ST25R3916_REG_OVERSHOOT_CONF2_ov_pattern4 (1U << 4) +#define ST25R3916_REG_OVERSHOOT_CONF2_ov_pattern3 (1U << 3) +#define ST25R3916_REG_OVERSHOOT_CONF2_ov_pattern2 (1U << 2) +#define ST25R3916_REG_OVERSHOOT_CONF2_ov_pattern1 (1U << 1) +#define ST25R3916_REG_OVERSHOOT_CONF2_ov_pattern0 (1U << 0) + +#define ST25R3916_REG_UNDERSHOOT_CONF1_un_tx_mode1 (1U << 7) +#define ST25R3916_REG_UNDERSHOOT_CONF1_un_tx_mode0 (1U << 6) +#define ST25R3916_REG_UNDERSHOOT_CONF1_un_pattern13 (1U << 5) +#define ST25R3916_REG_UNDERSHOOT_CONF1_un_pattern12 (1U << 4) +#define ST25R3916_REG_UNDERSHOOT_CONF1_un_pattern11 (1U << 3) +#define ST25R3916_REG_UNDERSHOOT_CONF1_un_pattern10 (1U << 2) +#define ST25R3916_REG_UNDERSHOOT_CONF1_un_pattern9 (1U << 1) +#define ST25R3916_REG_UNDERSHOOT_CONF1_un_pattern8 (1U << 0) + +#define ST25R3916_REG_UNDERSHOOT_CONF2_un_pattern7 (1U << 7) +#define ST25R3916_REG_UNDERSHOOT_CONF2_un_pattern6 (1U << 6) +#define ST25R3916_REG_UNDERSHOOT_CONF2_un_pattern5 (1U << 5) +#define ST25R3916_REG_UNDERSHOOT_CONF2_un_pattern4 (1U << 4) +#define ST25R3916_REG_UNDERSHOOT_CONF2_un_pattern3 (1U << 3) +#define ST25R3916_REG_UNDERSHOOT_CONF2_un_pattern2 (1U << 2) +#define ST25R3916_REG_UNDERSHOOT_CONF2_un_pattern1 (1U << 1) +#define ST25R3916_REG_UNDERSHOOT_CONF2_un_pattern0 (1U << 0) + +#ifdef ST25R3916B + +#define ST25R3916_REG_AWS_CONF1_rfu3 (1U << 7) +#define ST25R3916_REG_AWS_CONF1_rfu2 (1U << 6) +#define ST25R3916_REG_AWS_CONF1_rfu1 (1U << 5) +#define ST25R3916_REG_AWS_CONF1_rfu0 (1U << 4) +#define ST25R3916_REG_AWS_CONF1_vddrf_cont (1U << 3) +#define ST25R3916_REG_AWS_CONF1_rfam_sep_rx (1U << 2) +#define ST25R3916_REG_AWS_CONF1_vddrf_rx_only (1U << 1) +#define ST25R3916_REG_AWS_CONF1_rgs_txonoff (1U << 0) + +#define ST25R3916_REG_AWS_CONF2_rfu1 (1U << 7) +#define ST25R3916_REG_AWS_CONF2_rfu0 (1U << 6) +#define ST25R3916_REG_AWS_CONF2_am_sym (1U << 5) +#define ST25R3916_REG_AWS_CONF2_en_modsink (1U << 4) +#define ST25R3916_REG_AWS_CONF2_am_filt3 (1U << 3) +#define ST25R3916_REG_AWS_CONF2_am_filt2 (1U << 2) +#define ST25R3916_REG_AWS_CONF2_am_filt1 (1U << 1) +#define ST25R3916_REG_AWS_CONF2_am_filt0 (1U << 0) +#define ST25R3916_REG_AWS_CONF2_am_filt_mask (0xfU << 0) +#define ST25R3916_REG_AWS_CONF2_am_filt_shift (0U) + +#define ST25R3916_REG_AWS_TIME1_tholdx1_3 (1U << 7) +#define ST25R3916_REG_AWS_TIME1_tholdx1_2 (1U << 6) +#define ST25R3916_REG_AWS_TIME1_tholdx1_1 (1U << 5) +#define ST25R3916_REG_AWS_TIME1_tholdx1_0 (1U << 4) +#define ST25R3916_REG_AWS_TIME1_tmoddx1_mask (0xfU << 4) +#define ST25R3916_REG_AWS_TIME1_tmoddx1_shift (4U) +#define ST25R3916_REG_AWS_TIME1_tmodsw1_3 (1U << 3) +#define ST25R3916_REG_AWS_TIME1_tmodsw1_2 (1U << 2) +#define ST25R3916_REG_AWS_TIME1_tmodsw1_1 (1U << 1) +#define ST25R3916_REG_AWS_TIME1_tmodsw1_0 (1U << 0) +#define ST25R3916_REG_AWS_TIME1_tmodsw1_mask (0xfU << 0) +#define ST25R3916_REG_AWS_TIME1_tmodsw1_shift (0U) + +#define ST25R3916_REG_AWS_TIME2_tammod1_3 (1U << 7) +#define ST25R3916_REG_AWS_TIME2_tammod1_2 (1U << 6) +#define ST25R3916_REG_AWS_TIME2_tammod1_1 (1U << 5) +#define ST25R3916_REG_AWS_TIME2_tammod1_0 (1U << 4) +#define ST25R3916_REG_AWS_TIME2_tammod1_mask (0xfU << 4) +#define ST25R3916_REG_AWS_TIME2_tammod1_shift (4U) +#define ST25R3916_REG_AWS_TIME2_tdres1_3 (1U << 3) +#define ST25R3916_REG_AWS_TIME2_tdres1_2 (1U << 2) +#define ST25R3916_REG_AWS_TIME2_tdres1_1 (1U << 1) +#define ST25R3916_REG_AWS_TIME2_tdres1_0 (1U << 0) +#define ST25R3916_REG_AWS_TIME2_tdres1_mask (0xfU << 0) +#define ST25R3916_REG_AWS_TIME2_tdres1_shift (0U) + +#define ST25R3916_REG_AWS_TIME3_tentx1_3 (1U << 7) +#define ST25R3916_REG_AWS_TIME3_tentx1_2 (1U << 6) +#define ST25R3916_REG_AWS_TIME3_tentx1_1 (1U << 5) +#define ST25R3916_REG_AWS_TIME3_tentx1_0 (1U << 4) +#define ST25R3916_REG_AWS_TIME3_tentx1_mask (0xfU << 4) +#define ST25R3916_REG_AWS_TIME3_tentx1_shift (4U) +#define ST25R3916_REG_AWS_TIME3_tmods2_3 (1U << 3) +#define ST25R3916_REG_AWS_TIME3_tmods2_2 (1U << 2) +#define ST25R3916_REG_AWS_TIME3_tmods2_1 (1U << 1) +#define ST25R3916_REG_AWS_TIME3_tmods2_0 (1U << 0) +#define ST25R3916_REG_AWS_TIME3_tmods2_mask (0xfU << 0) +#define ST25R3916_REG_AWS_TIME3_tmods2_shift (0U) + +#define ST25R3916_REG_AWS_TIME4_tholdx2_3 (1U << 7) +#define ST25R3916_REG_AWS_TIME4_tholdx2_2 (1U << 6) +#define ST25R3916_REG_AWS_TIME4_tholdx2_1 (1U << 5) +#define ST25R3916_REG_AWS_TIME4_tholdx2_0 (1U << 4) +#define ST25R3916_REG_AWS_TIME4_tholdx2_mask (0xfU << 4) +#define ST25R3916_REG_AWS_TIME4_tholdx2_shift (4U) +#define ST25R3916_REG_AWS_TIME4_tmodsw2_3 (1U << 3) +#define ST25R3916_REG_AWS_TIME4_tmodsw2_2 (1U << 2) +#define ST25R3916_REG_AWS_TIME4_tmodsw2_1 (1U << 1) +#define ST25R3916_REG_AWS_TIME4_tmodsw2_0 (1U << 0) +#define ST25R3916_REG_AWS_TIME4_tmodsw2_mask (0xfU << 0) +#define ST25R3916_REG_AWS_TIME4_tmodsw2_shift (0U) + +#define ST25R3916_REG_AWS_TIME5_tdres2_3 (1U << 7) +#define ST25R3916_REG_AWS_TIME5_tdres2_2 (1U << 6) +#define ST25R3916_REG_AWS_TIME5_tdres2_1 (1U << 5) +#define ST25R3916_REG_AWS_TIME5_tdres2_0 (1U << 4) +#define ST25R3916_REG_AWS_TIME5_tdres2_mask (0xfU << 4) +#define ST25R3916_REG_AWS_TIME5_tdres2_shift (4U) +#define ST25R3916_REG_AWS_TIME5_trez2_3 (1U << 3) +#define ST25R3916_REG_AWS_TIME5_trez2_2 (1U << 2) +#define ST25R3916_REG_AWS_TIME5_trez2_1 (1U << 1) +#define ST25R3916_REG_AWS_TIME5_trez2_0 (1U << 0) +#define ST25R3916_REG_AWS_TIME5_trez2_mask (0xfU << 0) +#define ST25R3916_REG_AWS_TIME5_trez2_shift (0U) + +#define ST25R3916_REG_AWS_RC_CAL_rfu4 (1U << 7) +#define ST25R3916_REG_AWS_RC_CAL_rfu3 (1U << 6) +#define ST25R3916_REG_AWS_RC_CAL_rfu2 (1U << 5) +#define ST25R3916_REG_AWS_RC_CAL_rfu1 (1U << 4) +#define ST25R3916_REG_AWS_RC_CAL_rfu0 (1U << 3) +#define ST25R3916_REG_AWS_RC_CAL_rc_cal_ro_2 (1U << 2) +#define ST25R3916_REG_AWS_RC_CAL_rc_cal_ro_1 (1U << 1) +#define ST25R3916_REG_AWS_RC_CAL_rc_cal_ro_0 (1U << 0) +#define ST25R3916_REG_AWS_RC_CAL_rc_cal_ro_mask (0x7U << 0) +#define ST25R3916_REG_AWS_RC_CAL_rc_cal_ro_shift (0U) + +#endif /* ST25R3916B */ + +#define ST25R3916_REG_WUP_TIMER_CONTROL_wur (1U << 7) +#define ST25R3916_REG_WUP_TIMER_CONTROL_wut2 (1U << 6) +#define ST25R3916_REG_WUP_TIMER_CONTROL_wut1 (1U << 5) +#define ST25R3916_REG_WUP_TIMER_CONTROL_wut0 (1U << 4) +#define ST25R3916_REG_WUP_TIMER_CONTROL_wut_mask (7U << 4) +#define ST25R3916_REG_WUP_TIMER_CONTROL_wut_shift (4U) +#define ST25R3916_REG_WUP_TIMER_CONTROL_wto (1U << 3) +#define ST25R3916_REG_WUP_TIMER_CONTROL_wam (1U << 2) +#define ST25R3916_REG_WUP_TIMER_CONTROL_wph (1U << 1) +#if defined(ST25R3916) +#define ST25R3916_REG_WUP_TIMER_CONTROL_wcap (1U << 0) +#elif defined(ST25R3916B) +#define ST25R3916_REG_WUP_TIMER_CONTROL_rfu0 (1U << 0) +#endif /* ST25R3916 */ + +#define ST25R3916_REG_AMPLITUDE_MEASURE_CONF_am_d3 (1U << 7) +#define ST25R3916_REG_AMPLITUDE_MEASURE_CONF_am_d2 (1U << 6) +#define ST25R3916_REG_AMPLITUDE_MEASURE_CONF_am_d1 (1U << 5) +#define ST25R3916_REG_AMPLITUDE_MEASURE_CONF_am_d0 (1U << 4) +#define ST25R3916_REG_AMPLITUDE_MEASURE_CONF_am_d_mask (0xfU << 4) +#define ST25R3916_REG_AMPLITUDE_MEASURE_CONF_am_d_shift (4U) +#define ST25R3916_REG_AMPLITUDE_MEASURE_CONF_am_aam (1U << 3) +#define ST25R3916_REG_AMPLITUDE_MEASURE_CONF_am_aew1 (1U << 2) +#define ST25R3916_REG_AMPLITUDE_MEASURE_CONF_am_aew0 (1U << 1) +#define ST25R3916_REG_AMPLITUDE_MEASURE_CONF_am_aew_mask (0x3U << 1) +#define ST25R3916_REG_AMPLITUDE_MEASURE_CONF_am_aew_shift (1U) +#define ST25R3916_REG_AMPLITUDE_MEASURE_CONF_am_ae (1U << 0) + +#define ST25R3916_REG_PHASE_MEASURE_CONF_pm_d3 (1U << 7) +#define ST25R3916_REG_PHASE_MEASURE_CONF_pm_d2 (1U << 6) +#define ST25R3916_REG_PHASE_MEASURE_CONF_pm_d1 (1U << 5) +#define ST25R3916_REG_PHASE_MEASURE_CONF_pm_d0 (1U << 4) +#define ST25R3916_REG_PHASE_MEASURE_CONF_pm_d_mask (0xfU << 4) +#define ST25R3916_REG_PHASE_MEASURE_CONF_pm_d_shift (4U) +#define ST25R3916_REG_PHASE_MEASURE_CONF_pm_aam (1U << 3) +#define ST25R3916_REG_PHASE_MEASURE_CONF_pm_aew1 (1U << 2) +#define ST25R3916_REG_PHASE_MEASURE_CONF_pm_aew0 (1U << 1) +#define ST25R3916_REG_PHASE_MEASURE_CONF_pm_aew_mask (0x3U << 1) +#define ST25R3916_REG_PHASE_MEASURE_CONF_pm_aew_shift (1U) +#define ST25R3916_REG_PHASE_MEASURE_CONF_pm_ae (1U << 0) + +#if defined(ST25R3916) +#define ST25R3916_REG_CAPACITANCE_MEASURE_CONF_cm_d3 (1U << 7) +#define ST25R3916_REG_CAPACITANCE_MEASURE_CONF_cm_d2 (1U << 6) +#define ST25R3916_REG_CAPACITANCE_MEASURE_CONF_cm_d1 (1U << 5) +#define ST25R3916_REG_CAPACITANCE_MEASURE_CONF_cm_d0 (1U << 4) +#define ST25R3916_REG_CAPACITANCE_MEASURE_CONF_cm_d_mask (0xfU << 4) +#define ST25R3916_REG_CAPACITANCE_MEASURE_CONF_cm_d_shift (4U) +#define ST25R3916_REG_CAPACITANCE_MEASURE_CONF_cm_aam (1U << 3) +#define ST25R3916_REG_CAPACITANCE_MEASURE_CONF_cm_aew1 (1U << 2) +#define ST25R3916_REG_CAPACITANCE_MEASURE_CONF_cm_aew0 (1U << 1) +#define ST25R3916_REG_CAPACITANCE_MEASURE_CONF_cm_aew_mask (0x3U << 1) +#define ST25R3916_REG_CAPACITANCE_MEASURE_CONF_cm_aew_shift (1U) +#define ST25R3916_REG_CAPACITANCE_MEASURE_CONF_cm_ae (1U << 0) +#elif defined(ST25R3916B) +#define ST25R3916_REG_MEAS_TX_DELAY_m_phase_ana (1U << 7) +#define ST25R3916_REG_MEAS_TX_DELAY_m_amp_ana (1U << 6) +#define ST25R3916_REG_MEAS_TX_DELAY_rfu1 (1U << 5) +#define ST25R3916_REG_MEAS_TX_DELAY_rfu0 (1U << 4) +#define ST25R3916_REG_MEAS_TX_DELAY_meas_tx_del3 (1U << 3) +#define ST25R3916_REG_MEAS_TX_DELAY_meas_tx_del2 (1U << 2) +#define ST25R3916_REG_MEAS_TX_DELAY_meas_tx_del1 (1U << 1) +#define ST25R3916_REG_MEAS_TX_DELAY_meas_tx_del0 (1U << 0) +#define ST25R3916_REG_MEAS_TX_DELAY_meas_tx_del_0ms (0U << 0) +#define ST25R3916_REG_MEAS_TX_DELAY_meas_tx_del_0_60ms (1U << 0) +#define ST25R3916_REG_MEAS_TX_DELAY_meas_tx_del_1_21ms (2U << 0) +#define ST25R3916_REG_MEAS_TX_DELAY_meas_tx_del_1_81ms (3U << 0) +#define ST25R3916_REG_MEAS_TX_DELAY_meas_tx_del_2_42ms (4U << 0) +#define ST25R3916_REG_MEAS_TX_DELAY_meas_tx_del_3_02ms (5U << 0) +#define ST25R3916_REG_MEAS_TX_DELAY_meas_tx_del_3_62ms (6U << 0) +#define ST25R3916_REG_MEAS_TX_DELAY_meas_tx_del_4_23ms (7U << 0) +#define ST25R3916_REG_MEAS_TX_DELAY_meas_tx_del_4_83ms (8U << 0) +#define ST25R3916_REG_MEAS_TX_DELAY_meas_tx_del_5_44ms (9U << 0) +#define ST25R3916_REG_MEAS_TX_DELAY_meas_tx_del_6_44ms (0xAU << 0) +#define ST25R3916_REG_MEAS_TX_DELAY_meas_tx_del_7_25ms (0xBU << 0) +#define ST25R3916_REG_MEAS_TX_DELAY_meas_tx_del_8_46ms (0xCU << 0) +#define ST25R3916_REG_MEAS_TX_DELAY_meas_tx_del_9_67ms (0xDU << 0) +#define ST25R3916_REG_MEAS_TX_DELAY_meas_tx_del_10_87ms (0xEU << 0) +#define ST25R3916_REG_MEAS_TX_DELAY_meas_tx_del_12_08ms (0xFU << 0) +#define ST25R3916_REG_MEAS_TX_DELAY_meas_tx_del_mask (0xFU << 0) +#define ST25R3916_REG_MEAS_TX_DELAY_meas_tx_del_shift (0U) + +#endif /* ST25R3916 */ + +#define ST25R3916_REG_IC_IDENTITY_ic_type4 (1U << 7) +#define ST25R3916_REG_IC_IDENTITY_ic_type3 (1U << 6) +#define ST25R3916_REG_IC_IDENTITY_ic_type2 (1U << 5) +#define ST25R3916_REG_IC_IDENTITY_ic_type1 (1U << 4) +#define ST25R3916_REG_IC_IDENTITY_ic_type0 (1U << 3) +#define ST25R3916_REG_IC_IDENTITY_ic_type_st25r3916 (5U << 3) +#define ST25R3916_REG_IC_IDENTITY_ic_type_st25r3916B (6U << 3) +#define ST25R3916_REG_IC_IDENTITY_ic_type_mask (0x1fU << 3) +#define ST25R3916_REG_IC_IDENTITY_ic_type_shift (3U) +#define ST25R3916_REG_IC_IDENTITY_ic_rev2 (1U << 2) +#define ST25R3916_REG_IC_IDENTITY_ic_rev1 (1U << 1) +#define ST25R3916_REG_IC_IDENTITY_ic_rev0 (1U << 0) +#define ST25R3916_REG_IC_IDENTITY_ic_rev_v0 (0U << 0) +#define ST25R3916_REG_IC_IDENTITY_ic_rev_mask (7U << 0) +#define ST25R3916_REG_IC_IDENTITY_ic_rev_shift (0U) + +/*! \endcond DOXYGEN_SUPPRESS */ + +/* +****************************************************************************** +* GLOBAL FUNCTION PROTOTYPES +****************************************************************************** +*/ + +/*! + ***************************************************************************** + * \brief Returns the content of a register within the ST25R3916 + * + * This function is used to read out the content of ST25R3916 registers. + * + * \param[in] reg: Address of register to read. + * \param[out] val: Returned value. + * + * \return RFAL_ERR_NONE : Operation successful + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_SEND : Transmission error or acknowledge not received + ***************************************************************************** + */ +ReturnCode st25r3916ReadRegister(uint8_t reg, uint8_t* val); + +/*! + ***************************************************************************** + * \brief Reads from multiple ST25R3916 registers + * + * This function is used to read from multiple registers using the + * auto-increment feature. That is, after each read the address pointer + * inside the ST25R3916 gets incremented automatically. + * + * \param[in] reg: Address of the frist register to read from. + * \param[in] values: pointer to a buffer where the result shall be written + *to. \param[in] length: Number of registers to be read out. + * + * \return RFAL_ERR_NONE : Operation successful + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_SEND : Transmission error or acknowledge not received + ***************************************************************************** + */ +ReturnCode st25r3916ReadMultipleRegisters(uint8_t reg, uint8_t* values, + uint8_t length); + +/*! + ***************************************************************************** + * \brief Writes a given value to a register within the ST25R3916 + * + * This function is used to write \a val to address \a reg within the + *ST25R3916. + * + * \param[in] reg: Address of the register to write. + * \param[in] val: Value to be written. + * + * \return RFAL_ERR_NONE : Operation successful + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_SEND : Transmission error or acknowledge not received + ***************************************************************************** + */ +ReturnCode st25r3916WriteRegister(uint8_t reg, uint8_t val); + +/*! + ***************************************************************************** + * \brief Writes multiple values to ST25R3916 registers + * + * This function is used to write multiple values to the ST25R3916 using the + * auto-increment feature. That is, after each write the address pointer + * inside the ST25R3916 gets incremented automatically. + * + * \param[in] reg: Address of the frist register to write. + * \param[in] values: pointer to a buffer containing the values to be written. + * \param[in] length: Number of values to be written. + * + * \return RFAL_ERR_NONE : Operation successful + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_SEND : Transmission error or acknowledge not received + ***************************************************************************** + */ +ReturnCode st25r3916WriteMultipleRegisters(uint8_t reg, const uint8_t* values, + uint8_t length); + +/*! + ***************************************************************************** + * \brief Writes values to ST25R3916 FIFO + * + * This function needs to be called in order to write to the ST25R3916 FIFO. + * + * \param[in] values: pointer to a buffer containing the values to be written + * to the FIFO. + * \param[in] length: Number of values to be written. + * + * \return RFAL_ERR_NONE : Operation successful + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_SEND : Transmission error or acknowledge not received + ***************************************************************************** + */ +ReturnCode st25r3916WriteFifo(const uint8_t* values, uint16_t length); + +/*! + ***************************************************************************** + * \brief Read values from ST25R3916 FIFO + * + * This function needs to be called in order to read from ST25R3916 FIFO. + * + * \param[out] buf: pointer to a buffer where the FIFO content shall be + * written to. + * \param[in] length: Number of bytes to read. + * + * \note: This function doesn't check whether \a length is really the + * number of available bytes in FIFO + * + * \return RFAL_ERR_NONE : Operation successful + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_SEND : Transmission error or acknowledge not received + ***************************************************************************** + */ +ReturnCode st25r3916ReadFifo(uint8_t* buf, uint16_t length); + +/*! + ***************************************************************************** + * \brief Writes values to ST25R3916 PTM + * + * Accesses to the begging of ST25R3916 Passive Target Memory (PTM A Config) + * and writes the given values + * + * \param[in] values: pointer to a buffer containing the values to be written + * to the Passive Target Memory. + * \param[in] length: Number of values to be written. + * + * \return RFAL_ERR_NONE : Operation successful + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_SEND : Transmission error or acknowledge not received + ***************************************************************************** + */ +ReturnCode st25r3916WritePTMem(const uint8_t* values, uint16_t length); + +/*! + ***************************************************************************** + * \brief Reads the ST25R3916 PTM + * + * Accesses to the begging of ST25R3916 Passive Target Memory (PTM A Config) + * and reads the memory for the given length + * + * \param[out] values: pointer to a buffer where the PTM content shall be + * written to. + * \param[in] length: Number of bytes to read. + * + * \return RFAL_ERR_NONE : Operation successful + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_SEND : Transmission error or acknowledge not received + ***************************************************************************** + */ +ReturnCode st25r3916ReadPTMem(uint8_t* values, uint16_t length); + +/*! + ***************************************************************************** + * \brief Writes values to ST25R3916 PTM F config + * + * Accesses ST25R3916 Passive Target Memory F config and writes the given + *values + * + * \param[in] values: pointer to a buffer containing the values to be written + * to the Passive Target Memory + * \param[in] length: Number of values to be written. + * + * \return RFAL_ERR_NONE : Operation successful + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_SEND : Transmission error or acknowledge not received + ***************************************************************************** + */ +ReturnCode st25r3916WritePTMemF(const uint8_t* values, uint16_t length); + +/*! + ***************************************************************************** + * \brief Writes values to ST25R3916 PTM TSN Data + * + * Accesses ST25R3916 Passive Target Memory TSN data and writes the given + *values + * + * \param[in] values: pointer to a buffer containing the values to be written + * to the Passive Target Memory. + * \param[in] length: Number of values to be written. + * + * \return RFAL_ERR_NONE : Operation successful + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_SEND : Transmission error or acknowledge not received + ***************************************************************************** + */ +ReturnCode st25r3916WritePTMemTSN(const uint8_t* values, uint16_t length); + +/*! + ***************************************************************************** + * \brief Execute a direct command + * + * This function is used to start so-called direct command. These commands + * are implemented inside the chip and each command has unique code (see + * datasheet). + * + * \param[in] cmd : code of the direct command to be executed. + * + * \return RFAL_ERR_NONE : Operation successful + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_SEND : Transmission error or acknowledge not received + ***************************************************************************** + */ +ReturnCode st25r3916ExecuteCommand(uint8_t cmd); + +/*! + ***************************************************************************** + * \brief Read a test register within the ST25R3916 + * + * This function is used to read the content of test address \a reg within the + *ST25R3916 + * + * \param[in] reg: Address of the register to read + * \param[out] val: Returned read value + * + * \return RFAL_ERR_NONE : Operation successful + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_SEND : Transmission error or acknowledge not received + ***************************************************************************** + */ +ReturnCode st25r3916ReadTestRegister(uint8_t reg, uint8_t* val); + +/*! + ***************************************************************************** + * \brief Writes a given value to a test register within the ST25R3916 + * + * This function is used to write \a val to test address \a reg within the + *ST25R3916 + * + * \param[in] reg: Address of the register to write + * \param[in] val: Value to be written + * + * \return RFAL_ERR_NONE : Operation successful + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_SEND : Transmission error or acknowledge not received + ***************************************************************************** + */ +ReturnCode st25r3916WriteTestRegister(uint8_t reg, uint8_t val); + +/*! + ***************************************************************************** + * \brief Cleart bits on Register + * + * This function clears the given bitmask on the register + * + * \param[in] reg: Address of the register clear + * \param[in] clr_mask: Bitmask of bit to be cleared + * + * \return RFAL_ERR_NONE : Operation successful + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_SEND : Transmission error or acknowledge not received + ***************************************************************************** + */ +ReturnCode st25r3916ClrRegisterBits(uint8_t reg, uint8_t clr_mask); + +/*! + ***************************************************************************** + * \brief Set bits on Register + * + * This function sets the given bitmask on the register + * + * \param[in] reg: Address of the register clear + * \param[in] set_mask: Bitmask of bit to be cleared + * + * \return RFAL_ERR_NONE : Operation successful + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_SEND : Transmission error or acknowledge not received + ***************************************************************************** + */ +ReturnCode st25r3916SetRegisterBits(uint8_t reg, uint8_t set_mask); + +/*! + ***************************************************************************** + * \brief Changes the given bits on a ST25R3916 register + * + * This function is used if only a particular bits should be changed within + * an ST25R3916 register. + * + * \param[in] reg: Address of the register to change. + * \param[in] valueMask: bitmask of bits to be changed + * \param[in] value: the bits to be written on the enabled valueMask bits + * + * \return RFAL_ERR_NONE : Operation successful + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_SEND : Transmission error or acknowledge not received + ***************************************************************************** + */ +ReturnCode st25r3916ChangeRegisterBits(uint8_t reg, uint8_t valueMask, + uint8_t value); + +/*! + ***************************************************************************** + * \brief Modifies a value within a ST25R3916 register + * + * This function is used if only a particular bits should be changed within + * an ST25R3916 register. + * + * \param[in] reg: Address of the register to write. + * \param[in] clr_mask: bitmask of bits to be cleared to 0. + * \param[in] set_mask: bitmask of bits to be set to 1. + * + * \return RFAL_ERR_NONE : Operation successful + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_SEND : Transmission error or acknowledge not received + ***************************************************************************** + */ +ReturnCode st25r3916ModifyRegister(uint8_t reg, uint8_t clr_mask, + uint8_t set_mask); + +/*! + ***************************************************************************** + * \brief Changes the given bits on a ST25R3916 Test register + * + * This function is used if only a particular bits should be changed within + * an ST25R3916 register. + * + * \param[in] reg: Address of the Test register to change. + * \param[in] valueMask: bitmask of bits to be changed + * \param[in] value: the bits to be written on the enabled valueMask bits + * + * \return RFAL_ERR_NONE : Operation successful + * \return RFAL_ERR_PARAM : Invalid parameter + * \return RFAL_ERR_SEND : Transmission error or acknowledge not received + ***************************************************************************** + */ +ReturnCode st25r3916ChangeTestRegisterBits(uint8_t reg, uint8_t valueMask, + uint8_t value); + +/*! + ***************************************************************************** + * \brief Checks if register contains a expected value + * + * This function checks if the given reg contains a value that once masked + * equals the expected value + * + * \param reg : the register to check the value + * \param mask : the mask apply on register value + * \param val : expected value to be compared to + * + * \return true when reg contains the expected value | false otherwise + */ +bool st25r3916CheckReg(uint8_t reg, uint8_t mask, uint8_t val); + +/*! + ***************************************************************************** + * \brief Check if register ID is valid + * + * Checks if the given register ID a valid ST25R3916 register + * + * \param[in] reg: Address of register to check + * + * \return true if is a valid register ID + * \return false otherwise + * + ***************************************************************************** + */ +bool st25r3916IsRegValid(uint8_t reg); + +#endif /* ST25R3916_COM_H */ + +/** + * @} + * + * @} + * + * @} + * + * @} + */ diff --git a/core/embed/io/nfc/rfal/source/st25r3916/st25r3916_irq.c b/core/embed/io/nfc/rfal/source/st25r3916/st25r3916_irq.c new file mode 100644 index 0000000000..d9606555be --- /dev/null +++ b/core/embed/io/nfc/rfal/source/st25r3916/st25r3916_irq.c @@ -0,0 +1,232 @@ + +/****************************************************************************** + * @attention + * + * COPYRIGHT 2016 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/* + * PROJECT: ST25R3916 firmware + * Revision: + * LANGUAGE: ISO C99 + */ + +/*! \file + * + * \author Gustavo Patricio + * + * \brief ST25R3916 Interrupt handling + * + */ + +/* +****************************************************************************** +* INCLUDES +****************************************************************************** +*/ + +#include "st25r3916_irq.h" +#include "rfal_utils.h" +#include "st25r3916.h" +#include "st25r3916_com.h" +#include "st25r3916_led.h" + +/* + ****************************************************************************** + * LOCAL DATA TYPES + ****************************************************************************** + */ + +/*! Holds current and previous interrupt callback pointer as well as current + * Interrupt status and mask */ +typedef struct { + void (*prevCallback)(void); /*!< call back function for ST25R3916 interrupt */ + void (*callback)(void); /*!< call back function for ST25R3916 interrupt */ + uint32_t status; /*!< latest interrupt status */ + uint32_t mask; /*!< Interrupt mask. Negative mask = ST25R3916 mask regs */ +} st25r3916Interrupt; + +/* +****************************************************************************** +* GLOBAL DEFINES +****************************************************************************** +*/ + +/*! Length of the interrupt registers */ +#define ST25R3916_INT_REGS_LEN \ + ((ST25R3916_REG_IRQ_TARGET - ST25R3916_REG_IRQ_MAIN) + 1U) + +/* +****************************************************************************** +* GLOBAL VARIABLES +****************************************************************************** +*/ + +static volatile st25r3916Interrupt + st25r3916interrupt; /*!< Instance of ST25R3916 interrupt */ + +/* +****************************************************************************** +* GLOBAL FUNCTIONS +****************************************************************************** +*/ +void st25r3916InitInterrupts(void) { + platformIrqST25RPinInitialize(); + platformIrqST25RSetCallback(st25r3916Isr); + + st25r3916interrupt.callback = NULL; + st25r3916interrupt.prevCallback = NULL; + st25r3916interrupt.status = ST25R3916_IRQ_MASK_NONE; + st25r3916interrupt.mask = ST25R3916_IRQ_MASK_NONE; +} + +/*******************************************************************************/ +void st25r3916Isr(void) { + st25r3916CheckForReceivedInterrupts(); + + // Check if callback is set and run it + if (NULL != st25r3916interrupt.callback) { + st25r3916interrupt.callback(); + } +} + +/*******************************************************************************/ +void st25r3916CheckForReceivedInterrupts(void) { + uint8_t iregs[ST25R3916_INT_REGS_LEN]; + uint32_t irqStatus; + + /* Initialize iregs */ + irqStatus = ST25R3916_IRQ_MASK_NONE; + RFAL_MEMSET(iregs, (int32_t)(ST25R3916_IRQ_MASK_ALL & 0xFFU), + ST25R3916_INT_REGS_LEN); + + /* In case the IRQ is Edge (not Level) triggered read IRQs until done */ + while (platformGpioIsHigh(ST25R_INT_PORT, ST25R_INT_PIN)) { + st25r3916ReadMultipleRegisters(ST25R3916_REG_IRQ_MAIN, iregs, + ST25R3916_INT_REGS_LEN); + + irqStatus |= (uint32_t)iregs[0]; + irqStatus |= (uint32_t)iregs[1] << 8; + irqStatus |= (uint32_t)iregs[2] << 16; + irqStatus |= (uint32_t)iregs[3] << 24; + } + + /* Forward all interrupts, even masked ones to application */ + platformProtectST25RIrqStatus(); + st25r3916interrupt.status |= irqStatus; + platformUnprotectST25RIrqStatus(); + + /* Send an IRQ event to LED handling */ + st25r3916ledEvtIrq(st25r3916interrupt.status); +} + +/*******************************************************************************/ +void st25r3916ModifyInterrupts(uint32_t clr_mask, uint32_t set_mask) { + uint8_t i; + uint32_t old_mask; + uint32_t new_mask; + + old_mask = st25r3916interrupt.mask; + new_mask = ((~old_mask & set_mask) | (old_mask & clr_mask)); + st25r3916interrupt.mask &= ~clr_mask; + st25r3916interrupt.mask |= set_mask; + + for (i = 0; i < ST25R3916_INT_REGS_LEN; i++) { + if (((new_mask >> (8U * i)) & 0xFFU) == 0U) { + continue; + } + + st25r3916WriteRegister( + ST25R3916_REG_IRQ_MASK_MAIN + i, + (uint8_t)((st25r3916interrupt.mask >> (8U * i)) & 0xFFU)); + } + return; +} + +/*******************************************************************************/ +uint32_t st25r3916WaitForInterruptsTimed(uint32_t mask, uint16_t tmo) { + uint32_t tmrDelay; + uint32_t status; + + tmrDelay = platformTimerCreate(tmo); + + /* Run until specific interrupt has happen or the timer has expired */ + do { + status = (st25r3916interrupt.status & mask); + } while (((!platformTimerIsExpired(tmrDelay)) || (tmo == 0U)) && + (status == 0U)); + + platformTimerDestroy(tmrDelay); + + status = st25r3916interrupt.status & mask; + + platformProtectST25RIrqStatus(); + st25r3916interrupt.status &= ~status; + platformUnprotectST25RIrqStatus(); + + return status; +} + +/*******************************************************************************/ +uint32_t st25r3916GetInterrupt(uint32_t mask) { + uint32_t irqs; + + irqs = (st25r3916interrupt.status & mask); + if (irqs != ST25R3916_IRQ_MASK_NONE) { + platformProtectST25RIrqStatus(); + st25r3916interrupt.status &= ~irqs; + platformUnprotectST25RIrqStatus(); + } + + return irqs; +} + +/*******************************************************************************/ +void st25r3916ClearAndEnableInterrupts(uint32_t mask) { + st25r3916GetInterrupt(mask); + st25r3916EnableInterrupts(mask); +} + +/*******************************************************************************/ +void st25r3916EnableInterrupts(uint32_t mask) { + st25r3916ModifyInterrupts(mask, 0); +} + +/*******************************************************************************/ +void st25r3916DisableInterrupts(uint32_t mask) { + st25r3916ModifyInterrupts(0, mask); +} + +/*******************************************************************************/ +void st25r3916ClearInterrupts(void) { + uint8_t iregs[ST25R3916_INT_REGS_LEN]; + + st25r3916ReadMultipleRegisters(ST25R3916_REG_IRQ_MAIN, iregs, + ST25R3916_INT_REGS_LEN); + + platformProtectST25RIrqStatus(); + st25r3916interrupt.status = ST25R3916_IRQ_MASK_NONE; + platformUnprotectST25RIrqStatus(); + return; +} + +/*******************************************************************************/ +void st25r3916IRQCallbackSet(void (*cb)(void)) { + st25r3916interrupt.prevCallback = st25r3916interrupt.callback; + st25r3916interrupt.callback = cb; +} + +/*******************************************************************************/ +void st25r3916IRQCallbackRestore(void) { + st25r3916interrupt.callback = st25r3916interrupt.prevCallback; + st25r3916interrupt.prevCallback = NULL; +} diff --git a/core/embed/io/nfc/rfal/source/st25r3916/st25r3916_irq.h b/core/embed/io/nfc/rfal/source/st25r3916/st25r3916_irq.h new file mode 100644 index 0000000000..d2224d7426 --- /dev/null +++ b/core/embed/io/nfc/rfal/source/st25r3916/st25r3916_irq.h @@ -0,0 +1,312 @@ + +/****************************************************************************** + * @attention + * + * COPYRIGHT 2016 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/* + * PROJECT: ST25R3916 firmware + * Revision: + * LANGUAGE: ISO C99 + */ + +/*! \file + * + * \author Gustavo Patricio + * + * \brief ST25R3916 Interrupt handling + * + * \addtogroup RFAL + * @{ + * + * \addtogroup RFAL-HAL + * \brief RFAL Hardware Abstraction Layer + * @{ + * + * \addtogroup ST25R3916 + * \brief RFAL ST25R3916 Driver + * @{ + * + * \addtogroup ST25R3916_IRQ + * \brief RFAL ST25R3916 IRQ + * @{ + * + */ + +#ifndef ST25R3916_IRQ_H +#define ST25R3916_IRQ_H + +/* +****************************************************************************** +* INCLUDES +****************************************************************************** +*/ + +#include "rfal_platform.h" + +/* +****************************************************************************** +* GLOBAL DEFINES +****************************************************************************** +*/ + +#define ST25R3916_IRQ_MASK_ALL \ + (uint32_t)(0xFFFFFFFFUL) /*!< All ST25R3916 interrupt sources */ +#define ST25R3916_IRQ_MASK_NONE \ + (uint32_t)(0x00000000UL) /*!< No ST25R3916 interrupt source */ + +/* Main interrupt register */ +#define ST25R3916_IRQ_MASK_OSC \ + (uint32_t)(0x00000080U) /*!< ST25R3916 oscillator stable interrupt */ +#define ST25R3916_IRQ_MASK_FWL \ + (uint32_t)(0x00000040U) /*!< ST25R3916 FIFO water level interrupt */ +#define ST25R3916_IRQ_MASK_RXS \ + (uint32_t)(0x00000020U) /*!< ST25R3916 start of receive interrupt */ +#define ST25R3916_IRQ_MASK_RXE \ + (uint32_t)(0x00000010U) /*!< ST25R3916 end of receive interrupt */ +#define ST25R3916_IRQ_MASK_TXE \ + (uint32_t)(0x00000008U) /*!< ST25R3916 end of transmission interrupt */ +#define ST25R3916_IRQ_MASK_COL \ + (uint32_t)(0x00000004U) /*!< ST25R3916 bit collision interrupt */ +#define ST25R3916_IRQ_MASK_RX_REST \ + (uint32_t)( \ + 0x00000002U) /*!< ST25R3916 automatic reception restart interrupt */ +#define ST25R3916_IRQ_MASK_RFU \ + (uint32_t)(0x00000001U) /*!< ST25R3916 RFU interrupt */ + +/* Timer and NFC interrupt register */ +#define ST25R3916_IRQ_MASK_DCT \ + (uint32_t)( \ + 0x00008000U) /*!< ST25R3916 termination of direct command interrupt. */ +#define ST25R3916_IRQ_MASK_NRE \ + (uint32_t)(0x00004000U) /*!< ST25R3916 no-response timer expired interrupt \ + */ +#define ST25R3916_IRQ_MASK_GPE \ + (uint32_t)( \ + 0x00002000U) /*!< ST25R3916 general purpose timer expired interrupt */ +#define ST25R3916_IRQ_MASK_EON \ + (uint32_t)(0x00001000U) /*!< ST25R3916 external field on interrupt */ +#define ST25R3916_IRQ_MASK_EOF \ + (uint32_t)(0x00000800U) /*!< ST25R3916 external field off interrupt */ +#define ST25R3916_IRQ_MASK_CAC \ + (uint32_t)(0x00000400U) /*!< ST25R3916 collision during RF collision \ + avoidance interrupt */ +#define ST25R3916_IRQ_MASK_CAT \ + (uint32_t)(0x00000200U) /*!< ST25R3916 minimum guard time expired interrupt \ + */ +#define ST25R3916_IRQ_MASK_NFCT \ + (uint32_t)( \ + 0x00000100U) /*!< ST25R3916 initiator bit rate recognised interrupt */ + +/* Error and wake-up interrupt register */ +#define ST25R3916_IRQ_MASK_CRC \ + (uint32_t)(0x00800000U) /*!< ST25R3916 CRC error interrupt */ +#define ST25R3916_IRQ_MASK_PAR \ + (uint32_t)(0x00400000U) /*!< ST25R3916 parity error interrupt */ +#define ST25R3916_IRQ_MASK_ERR2 \ + (uint32_t)(0x00200000U) /*!< ST25R3916 soft framing error interrupt */ +#define ST25R3916_IRQ_MASK_ERR1 \ + (uint32_t)(0x00100000U) /*!< ST25R3916 hard framing error interrupt */ +#define ST25R3916_IRQ_MASK_WT \ + (uint32_t)(0x00080000U) /*!< ST25R3916 wake-up interrupt */ +#define ST25R3916_IRQ_MASK_WAM \ + (uint32_t)(0x00040000U) /*!< ST25R3916 wake-up due to amplitude interrupt */ +#define ST25R3916_IRQ_MASK_WPH \ + (uint32_t)(0x00020000U) /*!< ST25R3916 wake-up due to phase interrupt */ +#if defined(ST25R3916) +#define ST25R3916_IRQ_MASK_WCAP \ + (uint32_t)( \ + 0x00010000U) /*!< ST25R3916 wake-up due to capacitance measurement */ +#elif defined(ST25R3916B) +#define ST25R3916_IRQ_MASK_WCAP \ + ST25R3916_IRQ_MASK_NONE /*!< ST25R3916B disable capacitive WU */ +#endif /* ST25R3916 */ + +/* Passive Target Interrupt Register */ +#define ST25R3916_IRQ_MASK_PPON2 \ + (uint32_t)( \ + 0x80000000U) /*!< ST25R3916 PPON2 Field on waiting Timer interrupt */ +#define ST25R3916_IRQ_MASK_SL_WL \ + (uint32_t)(0x40000000U) /*!< ST25R3916 Passive target slot number water \ + level interrupt */ +#define ST25R3916_IRQ_MASK_APON \ + (uint32_t)( \ + 0x20000000U) /*!< ST25R3916 Anticollision done and Field On interrupt */ +#define ST25R3916_IRQ_MASK_RXE_PTA \ + (uint32_t)( \ + 0x10000000U) /*!< ST25R3916 RXE with an automatic response interrupt */ +#define ST25R3916_IRQ_MASK_WU_F \ + (uint32_t)(0x08000000U) /*!< ST25R3916 212/424b/s Passive target interrupt: \ + Active */ +#define ST25R3916_IRQ_MASK_RFU2 \ + (uint32_t)(0x04000000U) /*!< ST25R3916 RFU2 interrupt */ +#define ST25R3916_IRQ_MASK_WU_A_X \ + (uint32_t)(0x02000000U) /*!< ST25R3916 106kb/s Passive target state \ + interrupt: Active* */ +#define ST25R3916_IRQ_MASK_WU_A \ + (uint32_t)(0x01000000U) /*!< ST25R3916 106kb/s Passive target state \ + interrupt: Active */ + +/* +****************************************************************************** +* GLOBAL FUNCTION PROTOTYPES +****************************************************************************** +*/ + +/*! + ***************************************************************************** + * \brief Wait until an ST25R3916 interrupt occurs + * + * This function is used to access the ST25R3916 interrupt flags. Use this + * to wait for max. \a tmo milliseconds for the \b first interrupt indicated + * with mask \a mask to occur. + * + * \param[in] mask : mask indicating the interrupts to wait for. + * \param[in] tmo : time in milliseconds until timeout occurs. If set to 0 + * the functions waits forever. + * + * \return : 0 if timeout occured otherwise a mask indicating the cleared + * interrupts. + * + ***************************************************************************** + */ +uint32_t st25r3916WaitForInterruptsTimed(uint32_t mask, uint16_t tmo); + +/*! + ***************************************************************************** + * \brief Get status for the given interrupt + * + * This function is used to check whether the interrupt given by \a mask + * has occured. If yes the interrupt gets cleared. This function returns + * only status bits which are inside \a mask. + * + * \param[in] mask : mask indicating the interrupt to check for. + * + * \return the mask of the interrupts occurred + * + ***************************************************************************** + */ +uint32_t st25r3916GetInterrupt(uint32_t mask); + +/*! + ***************************************************************************** + * \brief Init the 3916 interrupt + * + * This function is used to check whether the interrupt given by \a mask + * has occured. + * + ***************************************************************************** + */ +void st25r3916InitInterrupts(void); + +/*! + ***************************************************************************** + * \brief Modifies the Interrupt + * + * This function modifies the interrupt + * + * \param[in] clr_mask : bit mask to be cleared on the interrupt mask + * \param[in] set_mask : bit mask to be set on the interrupt mask + ***************************************************************************** + */ +void st25r3916ModifyInterrupts(uint32_t clr_mask, uint32_t set_mask); + +/*! + ***************************************************************************** + * \brief Checks received interrupts + * + * Checks received interrupts and saves the result into global params + ***************************************************************************** + */ +void st25r3916CheckForReceivedInterrupts(void); + +/*! + ***************************************************************************** + * \brief ISR Service routine + * + * This function modiefies the interupt + ***************************************************************************** + */ +void st25r3916Isr(void); + +/*! + ***************************************************************************** + * \brief Enable a given ST25R3916 Interrupt source + * + * This function enables all interrupts given by \a mask, + * ST25R3916_IRQ_MASK_ALL enables all interrupts. + * + * \param[in] mask: mask indicating the interrupts to be enabled + * + ***************************************************************************** + */ +void st25r3916EnableInterrupts(uint32_t mask); + +/*! + ***************************************************************************** + * \brief Disable one or more a given ST25R3916 Interrupt sources + * + * This function disables all interrupts given by \a mask. 0xff disables all. + * + * \param[in] mask: mask indicating the interrupts to be disabled. + * + ***************************************************************************** + */ +void st25r3916DisableInterrupts(uint32_t mask); + +/*! + ***************************************************************************** + * \brief Clear all ST25R3916 irq flags + * + ***************************************************************************** + */ +void st25r3916ClearInterrupts(void); + +/*! + ***************************************************************************** + * \brief Clears and then enables the given ST25R3916 Interrupt sources + * + * \param[in] mask: mask indicating the interrupts to be cleared and enabled + ***************************************************************************** + */ +void st25r3916ClearAndEnableInterrupts(uint32_t mask); + +/*! + ***************************************************************************** + * \brief Sets IRQ callback for the ST25R3916 interrupt + * + * \param[in] cb: pointer to the callback method + * + ***************************************************************************** + */ +void st25r3916IRQCallbackSet(void (*cb)(void)); + +/*! + ***************************************************************************** + * \brief Sets IRQ callback for the ST25R3916 interrupt + * + ***************************************************************************** + */ +void st25r3916IRQCallbackRestore(void); + +#endif /* ST25R3916_IRQ_H */ + +/** + * @} + * + * @} + * + * @} + * + * @} + */ diff --git a/core/embed/io/nfc/rfal/source/st25r3916/st25r3916_led.c b/core/embed/io/nfc/rfal/source/st25r3916/st25r3916_led.c new file mode 100644 index 0000000000..0bf66c88a7 --- /dev/null +++ b/core/embed/io/nfc/rfal/source/st25r3916/st25r3916_led.c @@ -0,0 +1,162 @@ + +/****************************************************************************** + * @attention + * + * COPYRIGHT 2016 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/* + * PROJECT: ST25R3916 firmware + * Revision: + * LANGUAGE: ISO C99 + */ + +/*! \file + * + * \author Gustavo Patricio + * + * \brief ST25R3916 LEDs handling + * + */ + +/* +****************************************************************************** +* INCLUDES +****************************************************************************** +*/ + +#include "st25r3916_led.h" +#include "st25r3916.h" +#include "st25r3916_com.h" +#include "st25r3916_irq.h" + +/* +****************************************************************************** +* MACROS +****************************************************************************** +*/ + +#ifdef PLATFORM_LED_RX_PIN +#define st25r3916ledRxOn() \ + platformLedOn(PLATFORM_LED_RX_PORT, \ + PLATFORM_LED_RX_PIN); /*!< LED Rx Pin On from system HAL */ +#define st25r3916ledRxOff() \ + platformLedOff(PLATFORM_LED_RX_PORT, \ + PLATFORM_LED_RX_PIN); /*!< LED Rx Pin Off from system HAL */ +#else /* PLATFORM_LED_RX_PIN */ +#define st25r3916ledRxOn() +#define st25r3916ledRxOff() +#endif /* PLATFORM_LED_RX_PIN */ + +#ifdef PLATFORM_LED_FIELD_PIN +#define st25r3916ledFieldOn() \ + platformLedOn( \ + PLATFORM_LED_FIELD_PORT, \ + PLATFORM_LED_FIELD_PIN); /*!< LED Field Pin On from system HAL */ +#define st25r3916ledFieldOff() \ + platformLedOff( \ + PLATFORM_LED_FIELD_PORT, \ + PLATFORM_LED_FIELD_PIN); /*!< LED Field Pin Off from system HAL */ +#else /* PLATFORM_LED_FIELD_PIN */ +#define st25r3916ledFieldOn() +#define st25r3916ledFieldOff() +#endif /* PLATFORM_LED_FIELD_PIN */ + +#ifdef PLATFORM_LED_ERR_PIN +#define st25r3916ledErrOn() \ + platformLedOn( \ + PLATFORM_LED_ERR_PORT, \ + PLATFORM_LED_ERR_PIN); /*!< LED Field Pin On from system HAL */ +#define st25r3916ledErrOff() \ + platformLedOff( \ + PLATFORM_LED_ERR_PORT, \ + PLATFORM_LED_ERR_PIN); /*!< LED Field Pin Off from system HAL */ +#else /* PLATFORM_LED_ERR_PIN */ +#define st25r3916ledErrOn() +#define st25r3916ledErrOff() +#endif /* PLATFORM_LED_ERR_PIN */ + +/* +****************************************************************************** +* GLOBAL FUNCTIONS +****************************************************************************** +*/ + +void st25r3916ledInit(void) { + /* Initialize LEDs if existing and defined */ + platformLedsInitialize(); + + st25r3916ledRxOff(); + st25r3916ledFieldOff(); + st25r3916ledErrOff(); +} + +/*******************************************************************************/ +void st25r3916ledEvtIrq(uint32_t irqs) { + if ((irqs & (ST25R3916_IRQ_MASK_TXE | ST25R3916_IRQ_MASK_CAT)) != 0U) { + st25r3916ledFieldOn(); + st25r3916ledErrOff(); + } + + if ((irqs & (ST25R3916_IRQ_MASK_RXS | ST25R3916_IRQ_MASK_NFCT)) != 0U) { + st25r3916ledRxOn(); + } + + if ((irqs & (ST25R3916_IRQ_MASK_RXE | ST25R3916_IRQ_MASK_NRE | + ST25R3916_IRQ_MASK_RX_REST | ST25R3916_IRQ_MASK_RXE_PTA | + ST25R3916_IRQ_MASK_WU_A | ST25R3916_IRQ_MASK_WU_A_X | + ST25R3916_IRQ_MASK_WU_F | ST25R3916_IRQ_MASK_RFU2)) != 0U) { + st25r3916ledRxOff(); + } + + if (((irqs & (ST25R3916_IRQ_MASK_CRC | ST25R3916_IRQ_MASK_PAR | + ST25R3916_IRQ_MASK_ERR1 | ST25R3916_IRQ_MASK_ERR2)) != 0U)) { + st25r3916ledErrOn(); + } +} + +/*******************************************************************************/ +void st25r3916ledEvtWrReg(uint8_t reg, uint8_t val) { + if (reg == ST25R3916_REG_OP_CONTROL) { + if ((ST25R3916_REG_OP_CONTROL_tx_en & val) != 0U) { + st25r3916ledFieldOn(); + } else { + st25r3916ledFieldOff(); + } + } +} + +/*******************************************************************************/ +void st25r3916ledEvtWrMultiReg(uint8_t reg, const uint8_t* vals, uint8_t len) { + uint8_t i; + + for (i = 0; i < (len); i++) { + st25r3916ledEvtWrReg((reg + i), vals[i]); + } +} + +/*******************************************************************************/ +void st25r3916ledEvtCmd(uint8_t cmd) { + if ((cmd >= ST25R3916_CMD_TRANSMIT_WITH_CRC) && + (cmd <= ST25R3916_CMD_RESPONSE_RF_COLLISION_N)) { + st25r3916ledFieldOff(); + } + + if (cmd == ST25R3916_CMD_UNMASK_RECEIVE_DATA) { + st25r3916ledRxOff(); + } + + if (cmd == ST25R3916_CMD_SET_DEFAULT) { + st25r3916ledFieldOff(); + st25r3916ledRxOff(); + } +} diff --git a/core/embed/io/nfc/rfal/source/st25r3916/st25r3916_led.h b/core/embed/io/nfc/rfal/source/st25r3916/st25r3916_led.h new file mode 100644 index 0000000000..ffc58ba2aa --- /dev/null +++ b/core/embed/io/nfc/rfal/source/st25r3916/st25r3916_led.h @@ -0,0 +1,145 @@ + +/****************************************************************************** + * @attention + * + * COPYRIGHT 2016 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/* + * PROJECT: ST25R3916 firmware + * Revision: + * LANGUAGE: ISO C99 + */ + +/*! \file + * + * \author Gustavo Patricio + * + * \brief ST25R3916 LEDs handling + * + * + * \addtogroup RFAL + * @{ + * + * \addtogroup RFAL-HAL + * \brief RFAL Hardware Abstraction Layer + * @{ + * + * \addtogroup ST25R3916 + * \brief RFAL ST25R3916 Driver + * @{ + * + * \addtogroup ST25R3916_LED + * \brief RFAL ST25R3916 LED + * @{ + * + */ + +#ifndef ST25R3916_LED_H +#define ST25R3916_LED_H + +/* +****************************************************************************** +* INCLUDES +****************************************************************************** +*/ + +#include "rfal_platform.h" + +/* +****************************************************************************** +* GLOBAL DEFINES +****************************************************************************** +*/ + +/* +****************************************************************************** +* GLOBAL FUNCTION PROTOTYPES +****************************************************************************** +*/ + +/*! + ***************************************************************************** + * \brief ST25R3916 LED Initialize + * + * This function initializes the LEDs that represent ST25R3916 activity + * + ***************************************************************************** + */ +void st25r3916ledInit(void); + +/*! + ***************************************************************************** + * \brief ST25R3916 LED Event Interrupt + * + * This function should be called upon a ST25R3916 Interrupt, providing + * the interrupt event with ST25R3916 irq flags to update LEDs + * + * \param[in] irqs: ST25R3916 irqs mask + * + ***************************************************************************** + */ +void st25r3916ledEvtIrq(uint32_t irqs); + +/*! + ***************************************************************************** + * \brief ST25R3916 LED Event Write Register + * + * This function should be called on a ST25R3916 Write Register operation + * providing the event with the register and value to update LEDs + * + * \param[in] reg: ST25R3916 register to be written + * \param[in] val: value to be written on the register + * + ***************************************************************************** + */ +void st25r3916ledEvtWrReg(uint8_t reg, uint8_t val); + +/*! + ***************************************************************************** + * \brief ST25R3916 LED Event Write Multiple Register + * + * This function should be called upon a ST25R3916 Write Multiple Registers, + * providing the event with the registers and values to update LEDs + * + * \param[in] reg : ST25R3916 first register written + * \param[in] vals: pointer to the values written + * \param[in] len : number of registers written + * + ***************************************************************************** + */ +void st25r3916ledEvtWrMultiReg(uint8_t reg, const uint8_t* vals, uint8_t len); + +/*! + ***************************************************************************** + * \brief ST25R3916 LED Event Direct Command + * + * This function should be called upon a ST25R3916 direct command, providing + * the event with the command executed + * + * \param[in] cmd: ST25R3916 cmd executed + * + ***************************************************************************** + */ +void st25r3916ledEvtCmd(uint8_t cmd); + +#endif /* ST25R3916_LED_H */ + +/** + * @} + * + * @} + * + * @} + * + * @} + */ diff --git a/core/embed/io/nfc/st25r3916b/nfc.c b/core/embed/io/nfc/st25r3916b/nfc.c new file mode 100644 index 0000000000..95547e5ad0 --- /dev/null +++ b/core/embed/io/nfc/st25r3916b/nfc.c @@ -0,0 +1,306 @@ + +#include +#include +#include +#include + +#include "../inc/io/nfc.h" +#include "nfc_internal.h" +#include "rfal_platform.h" + +#include "../rfal/include/rfal_nfca.h" +#include "../rfal/include/rfal_rf.h" +#include "../rfal/include/rfal_utils.h" + +#include "stm32u5xx_hal.h" + +typedef struct { + bool initialized; + // SPI driver + SPI_HandleTypeDef hspi; + // NFC IRQ pin callback + void (*nfc_irq_callback)(void); + EXTI_HandleTypeDef hEXTI; +} st25r3916b_driver_t; + +static st25r3916b_driver_t g_st25r3916b_driver = { + .initialized = false, +}; + +nfc_status_t nfc_init() { + st25r3916b_driver_t *drv = &g_st25r3916b_driver; + + if (drv->initialized) { + return NFC_OK; + } + + // Enable clock of relevant peripherals + // SPI + GPIO ports + SPI_INSTANCE_3_CLK_EN(); + SPI_INSTANCE_3_MISO_CLK_EN(); + SPI_INSTANCE_3_MOSI_CLK_EN(); + SPI_INSTANCE_3_SCK_CLK_EN(); + SPI_INSTANCE_3_NSS_CLK_EN(); + + // SPI peripheral pin config + GPIO_InitTypeDef GPIO_InitStruct = {0}; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = SPI_INSTANCE_3_PIN_AF; + + GPIO_InitStruct.Pin = SPI_INSTANCE_3_MISO_PIN; + HAL_GPIO_Init(SPI_INSTANCE_3_MISO_PORT, &GPIO_InitStruct); + + GPIO_InitStruct.Pin = SPI_INSTANCE_3_MOSI_PIN; + HAL_GPIO_Init(SPI_INSTANCE_3_MOSI_PORT, &GPIO_InitStruct); + + GPIO_InitStruct.Pin = SPI_INSTANCE_3_SCK_PIN; + HAL_GPIO_Init(SPI_INSTANCE_3_SCK_PORT, &GPIO_InitStruct); + + // NSS pin controled by software, set as classical GPIO + GPIO_InitTypeDef GPIO_InitStruct_nss = {0}; + GPIO_InitStruct_nss.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct_nss.Pull = GPIO_NOPULL; + GPIO_InitStruct_nss.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct_nss.Pin = SPI_INSTANCE_3_NSS_PIN; + HAL_GPIO_Init(SPI_INSTANCE_3_NSS_PORT, &GPIO_InitStruct_nss); + + // NFC IRQ pin + GPIO_InitTypeDef GPIO_InitStructure_int = {0}; + GPIO_InitStructure_int.Mode = GPIO_MODE_INPUT; + GPIO_InitStructure_int.Pull = GPIO_PULLDOWN; + GPIO_InitStructure_int.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStructure_int.Pin = NFC_INT_PIN; + HAL_GPIO_Init(NFC_INT_PORT, &GPIO_InitStructure_int); + + memset(&(drv->hspi), 0, sizeof(drv->hspi)); + + drv->hspi.Instance = SPI_INSTANCE_3; + drv->hspi.Init.Mode = SPI_MODE_MASTER; + drv->hspi.Init.BaudRatePrescaler = + SPI_BAUDRATEPRESCALER_32; // TODO: Calculate frequency precisly. + drv->hspi.Init.DataSize = SPI_DATASIZE_8BIT; + drv->hspi.Init.Direction = SPI_DIRECTION_2LINES; + drv->hspi.Init.CLKPolarity = SPI_POLARITY_LOW; + drv->hspi.Init.CLKPhase = SPI_PHASE_2EDGE; + drv->hspi.Init.NSS = SPI_NSS_SOFT; // For rfal lib purpose, use software NSS + drv->hspi.Init.NSSPolarity = SPI_NSS_POLARITY_LOW; + drv->hspi.Init.NSSPMode = SPI_NSS_PULSE_DISABLE; + + EXTI_ConfigTypeDef EXTI_Config = {0}; + EXTI_Config.GPIOSel = NFC_EXTI_INTERRUPT_GPIOSEL; + EXTI_Config.Line = NFC_EXTI_INTERRUPT_LINE; + EXTI_Config.Mode = EXTI_MODE_INTERRUPT; + EXTI_Config.Trigger = EXTI_TRIGGER_RISING; + HAL_EXTI_SetConfigLine(&drv->hEXTI, &EXTI_Config); + NVIC_SetPriority(NFC_EXTI_INTERRUPT_NUM, IRQ_PRI_NORMAL); + __HAL_GPIO_EXTI_CLEAR_FLAG(NFC_INT_PIN); + NVIC_EnableIRQ(NFC_EXTI_INTERRUPT_NUM); + + HAL_StatusTypeDef status; + + status = HAL_SPI_Init(&(drv->hspi)); + + if (status != HAL_OK) { + return false; + } + + ReturnCode ret_code = rfalInitialize(); + if (ret_code != RFAL_ERR_NONE) { + return NFC_INITIALIZATION_FAILED; + } + + drv->initialized = true; + + return NFC_OK; +} + +nfc_status_t nfc_deinit() { + st25r3916b_driver_t *drv = &g_st25r3916b_driver; + + if (!drv->initialized) { + return NFC_OK; + } + + HAL_EXTI_ClearConfigLine(&drv->hEXTI); + NVIC_DisableIRQ(NFC_EXTI_INTERRUPT_NUM); + + ReturnCode ret_code = rfalDeinitialize(); + + if (ret_code != RFAL_ERR_NONE) { + return NFC_ERROR; + } + + HAL_SPI_DeInit(&(drv->hspi)); + + HAL_GPIO_DeInit(SPI_INSTANCE_3_MISO_PORT, SPI_INSTANCE_3_MISO_PIN); + HAL_GPIO_DeInit(SPI_INSTANCE_3_MOSI_PORT, SPI_INSTANCE_3_MOSI_PIN); + HAL_GPIO_DeInit(SPI_INSTANCE_3_SCK_PORT, SPI_INSTANCE_3_SCK_PIN); + HAL_GPIO_DeInit(SPI_INSTANCE_3_NSS_PORT, SPI_INSTANCE_3_NSS_PIN); + HAL_GPIO_DeInit(NFC_INT_PORT, NFC_INT_PIN); + + drv->initialized = false; + + return NFC_OK; +} + +HAL_StatusTypeDef nfc_spi_transmit_receive(const uint8_t *txData, + uint8_t *rxData, uint16_t length) { + st25r3916b_driver_t *drv = &g_st25r3916b_driver; + HAL_StatusTypeDef status; + + if ((txData != NULL) && (rxData == NULL)) { + status = HAL_SPI_Transmit(&(drv->hspi), (uint8_t *)txData, length, 1000); + } else if ((txData == NULL) && (rxData != NULL)) { + status = HAL_SPI_Receive(&(drv->hspi), rxData, length, 1000); + } else { + status = HAL_SPI_TransmitReceive(&(drv->hspi), (uint8_t *)txData, rxData, + length, 1000); + } + + return status; +} + +uint32_t nfc_create_timer(uint16_t time) { return (systick_ms() + time); } + +bool nfc_timer_is_expired(uint32_t timer) { + + uint32_t u_diff; + int32_t s_diff; + + u_diff = (timer - systick_ms()); // Calculate the diff between the timers + s_diff = u_diff; // Convert the diff to a signed var + + // Check if the given timer has expired already + if (s_diff < 0) { + return true; + } + + return false; +} + +void nfc_ext_irq_set_callback(void (*cb)(void)) { + st25r3916b_driver_t *drv = &g_st25r3916b_driver; + drv->nfc_irq_callback = cb; +} + +void NFC_EXTI_INTERRUPT_HANDLER(void) { + st25r3916b_driver_t *drv = &g_st25r3916b_driver; + + // Clear the EXTI line pending bit + __HAL_GPIO_EXTI_CLEAR_FLAG(NFC_INT_PIN); + if (drv->nfc_irq_callback != NULL) { + drv->nfc_irq_callback(); + } +} + +#define EXAMPLE_NFCA_DEVICES 10 + +void nfc_poll_type_A() { + /* + ****************************************************************************** + * GLOBAL FUNCTIONS + ****************************************************************************** + */ + ReturnCode err; + rfalNfcaSensRes sensRes; + rfalNfcaSelRes selRes; + rfalNfcaListenDevice nfcaDevList[EXAMPLE_NFCA_DEVICES]; + uint8_t devCnt; + uint8_t devIt; + + rfalInitialize(); + + for (;;) { + rfalFieldOff(); /* Turn the Field Off */ + platformDelay(500); + rfalNfcaPollerInitialize(); /* Initialize RFAL for NFC-A */ + rfalFieldOnAndStartGT(); /* Turns the Field On and starts GT timer */ + + /*******************************************************************************/ + /* Perform NFC-A Technology detection */ + err = rfalNfcaPollerTechnologyDetection( + RFAL_COMPLIANCE_MODE_NFC, &sensRes); /* Poll for nearby NFC-A devices */ + + if (err == RFAL_ERR_NONE) /* NFC-A type card found */ + { + return; + /*******************************************************************************/ + /* Perform NFC-A Collision Resolution */ + err = rfalNfcaPollerFullCollisionResolution( + RFAL_COMPLIANCE_MODE_NFC, EXAMPLE_NFCA_DEVICES, nfcaDevList, + &devCnt); /* Perform collision avoidance */ + if ((err == RFAL_ERR_NONE) && (devCnt > 0)) { + platformLog("NFC-A device(s) found %d\r\n", devCnt); + devIt = 0; /* Use the first device on the list */ + /*******************************************************************************/ + /* Check if desired device is in Sleep */ + if (nfcaDevList[devIt].isSleep) { + err = rfalNfcaPollerCheckPresence(RFAL_14443A_SHORTFRAME_CMD_WUPA, + &sensRes); /* Wake up all cards */ + if (err != RFAL_ERR_NONE) { + continue; + } + err = rfalNfcaPollerSelect(nfcaDevList[devIt].nfcId1, + nfcaDevList[devIt].nfcId1Len, + &selRes); /* Select specific device */ + if (err != RFAL_ERR_NONE) { + continue; + } + } + /*******************************************************************************/ + /* Perform protocol specific activation */ + switch (nfcaDevList[devIt].type) { + case RFAL_NFCA_T1T: + /* No further activation needed for a T1T (RID already + * performed)*/ + platformLog( + "NFC-A T1T device found \r\n"); /* NFC-A T1T device fained in: + t1tRidRes.uid */ + /* Following communications shall be performed using: + * - Non blocking: rfalStartTransceive() + + * rfalGetTransceiveState() + * - Blocking: rfalTransceiveBlockingTx() + + * rfalTransceiveBlockingRx() or rfalTransceiveBlockingTxRx() */ + break; + case RFAL_NFCA_T2T: + /* No specific activation needed for a T2T */ + platformLog( + "NFC-A T2T device found \r\n"); /* NFC-A T2T device found, + NFCID/UID is contained in: + nfcaDev.nfcid */ + /* Following communications shall be perforound, + NFCID/UID is contmed using: + * - Non blocking: rfalStartTransceive() + + * rfalGetTransceiveState() + * - Blocking: rfalTransceiveBlockingTx() + + * rfalTransceiveBlockingRx() or rfalTransceiveBlockingTxRx() */ + break; + case RFAL_NFCA_T4T: + platformLog( + "NFC-A T4T (ISO-DEP) device found \r\n"); /* NFC-A T4T device + found, NFCID/UID + is contained in: + nfcaDev.nfcid */ + /* Activation should continue using + * rfalIsoDepPollAHandleActivation(), see exampleRfalPoller.c */ + break; + case RFAL_NFCA_T4T_NFCDEP: /* Device supports T4T and NFC-DEP */ + case RFAL_NFCA_NFCDEP: /* Device supports NFC-DEP */ + platformLog( + "NFC-A P2P (NFC-DEP) device found \r\n"); /* NFC-A P2P device + found, NFCID/UID + is contained in: + nfcaDev.nfcid */ + /* Activation should continue using + * rfalNfcDepInitiatorHandleActivation(), see exampleRfalPoller.c + */ + break; + } + rfalNfcaPollerSleep(); /* Put device to sleep / HLTA (useless as the + field will be turned off anyhow) */ + } + } + } +} diff --git a/core/embed/io/nfc/st25r3916b/nfc_internal.h b/core/embed/io/nfc/st25r3916b/nfc_internal.h new file mode 100644 index 0000000000..579a786efa --- /dev/null +++ b/core/embed/io/nfc/st25r3916b/nfc_internal.h @@ -0,0 +1,16 @@ + +#include + +#ifndef TREZORHAL_NFC_INTERNAL_H +#define TREZORHAL_NFC_INTERNAL_H + +HAL_StatusTypeDef nfc_spi_transmit_receive(const uint8_t *txData, + uint8_t *rxData, uint16_t length); + +uint32_t nfc_create_timer(uint16_t time); + +bool nfc_timer_is_expired(uint32_t timer); + +void nfc_ext_irq_set_callback(void (*cb)(void)); + +#endif diff --git a/core/embed/io/nfc/st25r3916b/rfal_platform.h b/core/embed/io/nfc/st25r3916b/rfal_platform.h new file mode 100644 index 0000000000..d410e93001 --- /dev/null +++ b/core/embed/io/nfc/st25r3916b/rfal_platform.h @@ -0,0 +1,296 @@ +/****************************************************************************** + * @attention + * + * COPYRIGHT 2018 STMicroelectronics, all rights reserved + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, + * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ +/*! \file + * + * \author + * + * \brief Platform header file. Defining platform independent functionality. + * + */ + +#ifndef RFAL_PLATFORM_H +#define RFAL_PLATFORM_H + +#ifdef __cplusplus +extern "C" { +#endif + +/*ReturnCode +****************************************************************************** +* INCLUDES +****************************************************************************** +*/ + +#include +#include "stm32u5xx_hal.h" + +#include +#include +#include +#include +#include +#include + +#include "io/nfc.h" +#include "nfc_internal.h" + +/* +****************************************************************************** +* GLOBAL DEFINES +****************************************************************************** +*/ + +// Device type definition +#define ST25R3916B + +// GPIO pin used for ST25R SPI SS +#define ST25R_SS_PIN SPI_INSTANCE_3_NSS_PIN + +// GPIO port used for ST25R SPI SS port +#define ST25R_SS_PORT SPI_INSTANCE_3_NSS_PORT + +// GPIO pin used for ST25R External Interrupt +#define ST25R_INT_PIN NFC_INT_PIN + +// GPIO port used for ST25R External Interrupt +#define ST25R_INT_PORT NFC_INT_PORT + +/* +****************************************************************************** +* GLOBAL MACROS +****************************************************************************** +*/ +#define platformProtectST25RComm() \ + NVIC_DisableIRQ(EXTI10_IRQn) // TODO: PRobably should be irq_lock instead // + // Protect the unique access to communication + // channel (disable IRQ on single thread) + +#define platformUnprotectST25RComm() \ + NVIC_EnableIRQ(EXTI10_IRQn) // TODO: Use macro here / + +#define platformProtectST25RIrqStatus() \ + platformProtectST25RComm() /*!< Protect unique access to IRQ status var - \ + IRQ disable on single thread environment (MCU) \ + ; Mutex lock on a multi thread environment */ +#define platformUnprotectST25RIrqStatus() \ + platformUnprotectST25RComm() /*!< Unprotect the IRQ status var - IRQ enable \ + on a single thread environment (MCU) ; Mutex \ + unlock on a multi thread environment */ + +// Turns the given GPIO High +#define platformGpioSet(port, pin) HAL_GPIO_WritePin(port, pin, GPIO_PIN_SET) + +// Turns the given GPIO Low +#define platformGpioClear(port, pin) \ + HAL_GPIO_WritePin(port, pin, GPIO_PIN_RESET) + +// Toggles the given GPIO +#define platformGpioToggle(port, pin) HAL_GPIO_TogglePin(port, pin) + +// Checks if the given LED is High +#define platformGpioIsHigh(port, pin) \ + (HAL_GPIO_ReadPin(port, pin) == GPIO_PIN_SET) + +// Checks if the given LED is Low +#define platformGpioIsLow(port, pin) (!platformGpioIsHigh(port, pin)) + +// Create a timer with the given time (ms) +#define platformTimerCreate(t) nfc_create_timer(t) + +// Checks if the given timer is expired +#define platformTimerIsExpired(timer) nfc_timer_is_expired(timer) + +// Performs a delay for the given time (ms) +#define platformDelay(t) HAL_Delay(t) + +// Get System Tick ( 1 tick = 1 ms) +#define platformGetSysTick() HAL_GetTick() + +// Asserts whether the given expression is true +#define platformAssert(exp) assert_param(exp) + +#define platformErrorHandle() //_Error_Handler(__FILE__, __LINE__) /*!< Global + // error handle\trap */ + +#define platformIrqST25RSetCallback(cb) nfc_ext_irq_set_callback(cb) + +// SPI SS\CS: Chip|Slave Select +#define platformSpiSelect() \ + HAL_GPIO_WritePin(ST25R_SS_PORT, ST25R_SS_PIN, GPIO_PIN_RESET) + +// SPI SS\CS: Chip|Slave Deselect +#define platformSpiDeselect() \ + HAL_GPIO_WritePin(ST25R_SS_PORT, ST25R_SS_PIN, GPIO_PIN_SET) + +// SPI transceive +#define platformSpiTxRx(txBuf, rxBuf, len) \ + nfc_spi_transmit_receive(txBuf, rxBuf, len) + +// Log method +#define platformLog(...) // logUsart(__VA_ARGS__) + +/* +****************************************************************************** +* GLOBAL VARIABLES +****************************************************************************** +*/ +extern uint8_t globalCommProtectCnt; /* Global Protection Counter provided per + platform - instantiated in main.c */ + +/* +****************************************************************************** +* RFAL FEATURES CONFIGURATION +****************************************************************************** +*/ + +#define RFAL_FEATURE_LISTEN_MODE \ + true /*!< Enable/Disable RFAL support for Listen Mode */ +#define RFAL_FEATURE_WAKEUP_MODE \ + true /*!< Enable/Disable RFAL support for the Wake-Up mode */ +#define RFAL_FEATURE_LOWPOWER_MODE \ + false /*!< Enable/Disable RFAL support for the Low Power mode */ +#define RFAL_FEATURE_NFCA \ + true /*!< Enable/Disable RFAL support for NFC-A (ISO14443A) */ +#define RFAL_FEATURE_NFCB \ + true /*!< Enable/Disable RFAL support for NFC-B (ISO14443B) */ +#define RFAL_FEATURE_NFCF \ + true /*!< Enable/Disable RFAL support for NFC-F (FeliCa) */ +#define RFAL_FEATURE_NFCV \ + true /*!< Enable/Disable RFAL support for NFC-V (ISO15693) */ +#define RFAL_FEATURE_T1T \ + true /*!< Enable/Disable RFAL support for T1T (Topaz) */ +#define RFAL_FEATURE_T2T true /*!< Enable/Disable RFAL support for T2T */ +#define RFAL_FEATURE_T4T true /*!< Enable/Disable RFAL support for T4T */ +#define RFAL_FEATURE_ST25TB \ + true /*!< Enable/Disable RFAL support for ST25TB \ + */ +#define RFAL_FEATURE_ST25xV \ + true /*!< Enable/Disable RFAL support for ST25TV/ST25DV */ +#define RFAL_FEATURE_DYNAMIC_ANALOG_CONFIG \ + false /*!< Enable/Disable Analog Configs to be dynamically updated (RAM) */ +#define RFAL_FEATURE_DPO \ + true /*!< Enable/Disable RFAL Dynamic Power Output support */ +#define RFAL_FEATURE_ISO_DEP \ + true /*!< Enable/Disable RFAL support for ISO-DEP (ISO14443-4) */ +#define RFAL_FEATURE_ISO_DEP_POLL \ + true /*!< Enable/Disable RFAL support for Poller mode (PCD) ISO-DEP \ + (ISO14443-4) */ +#define RFAL_FEATURE_ISO_DEP_LISTEN \ + true /*!< Enable/Disable RFAL support for Listen mode (PICC) ISO-DEP \ + (ISO14443-4) */ +#define RFAL_FEATURE_NFC_DEP \ + true /*!< Enable/Disable RFAL support for NFC-DEP (NFCIP1/P2P) */ + +#define RFAL_FEATURE_ISO_DEP_IBLOCK_MAX_LEN \ + 256U /*!< ISO-DEP I-Block max length. Please use values as defined by \ + rfalIsoDepFSx */ +#define RFAL_FEATURE_NFC_DEP_BLOCK_MAX_LEN \ + 254U /*!< NFC-DEP Block/Payload length. Allowed values: 64, 128, 192, 254 */ +#define RFAL_FEATURE_NFC_RF_BUF_LEN \ + 258U /*!< RF buffer length used by RFAL NFC layer */ + +#define RFAL_FEATURE_ISO_DEP_APDU_MAX_LEN \ + 512U /*!< ISO-DEP APDU max length. Please use multiples of I-Block max \ + length */ +#define RFAL_FEATURE_NFC_DEP_PDU_MAX_LEN 512U /*!< NFC-DEP PDU max length. */ + +/* +****************************************************************************** +* RFAL CUSTOM SETTINGS +****************************************************************************** + Custom analog configs are used to cope with Automatic Antenna Tuning (AAT) + that are optimized differently for each board. +*/ +// #define RFAL_ANALOG_CONFIG_CUSTOM /*!< Use Custom +// Analog Configs when defined */ + +#ifndef platformProtectST25RIrqStatus +#define platformProtectST25RIrqStatus() /*!< Protect unique access to IRQ \ + status var - IRQ disable on single \ + thread environment (MCU) ; Mutex \ + lock on a multi thread environment \ + */ +#endif /* platformProtectST25RIrqStatus */ + +#ifndef platformUnprotectST25RIrqStatus +#define platformUnprotectST25RIrqStatus() /*!< Unprotect the IRQ status var - \ + IRQ enable on a single thread \ + environment (MCU) ; Mutex unlock \ + on a multi thread environment */ +#endif /* platformUnprotectST25RIrqStatus */ + +#ifndef platformProtectWorker +#define platformProtectWorker() /* Protect RFAL Worker/Task/Process from \ + concurrent execution on multi thread \ + platforms */ +#endif /* platformProtectWorker */ + +#ifndef platformUnprotectWorker +#define platformUnprotectWorker() /* Unprotect RFAL Worker/Task/Process from \ + concurrent execution on multi thread \ + platforms */ +#endif /* platformUnprotectWorker */ + +#ifndef platformIrqST25RPinInitialize +#define platformIrqST25RPinInitialize() /*!< Initializes ST25R IRQ pin */ +#endif /* platformIrqST25RPinInitialize */ + +#ifndef platformIrqST25RSetCallback +#define platformIrqST25RSetCallback(cb) /*!< Sets ST25R ISR callback */ +#endif /* platformIrqST25RSetCallback */ + +#ifndef platformLedsInitialize +#define platformLedsInitialize() /*!< Initializes the pins used as LEDs to \ + outputs */ +#endif /* platformLedsInitialize */ + +#ifndef platformLedOff +#define platformLedOff(port, pin) /*!< Turns the given LED Off */ +#endif /* platformLedOff */ + +#ifndef platformLedOn +#define platformLedOn(port, pin) /*!< Turns the given LED On */ +#endif /* platformLedOn */ + +#ifndef platformLedToggle +#define platformLedToggle(port, pin) /*!< Toggles the given LED */ +#endif /* platformLedToggle */ + +#ifndef platformGetSysTick +#define platformGetSysTick() /*!< Get System Tick ( 1 tick = 1 ms) */ +#endif /* platformGetSysTick */ + +#ifndef platformTimerDestroy +#define platformTimerDestroy(timer) /*!< Stops and released the given timer */ +#endif /* platformTimerDestroy */ + +#ifndef platformLog +#define platformLog(...) /*!< Log method */ +#endif /* platformLog */ + +#ifndef platformAssert +#define platformAssert(exp) /*!< Asserts whether the given expression is true \ + */ +#endif /* platformAssert */ + +#ifndef platformErrorHandle +#define platformErrorHandle() /*!< Global error handler or trap */ +#endif /* platformErrorHandle */ + +#ifdef __cplusplus +} +#endif + +#endif /* RFAL_PLATFORM_H */ diff --git a/core/embed/models/T3W1/boards/trezor_t3w1_revA.h b/core/embed/models/T3W1/boards/trezor_t3w1_revA.h index f95649d9f2..f925495484 100644 --- a/core/embed/models/T3W1/boards/trezor_t3w1_revA.h +++ b/core/embed/models/T3W1/boards/trezor_t3w1_revA.h @@ -160,4 +160,29 @@ #define NRF_OUT_FW_RUNNING_PORT GPIOE #define NRF_OUT_FW_RUNNING_CLK_ENA __HAL_RCC_GPIOE_CLK_ENABLE +#define SPI_INSTANCE_3 SPI3 +#define SPI_INSTANCE_3_PIN_AF GPIO_AF6_SPI3 +#define SPI_INSTANCE_3_CLK_EN __HAL_RCC_SPI3_CLK_ENABLE +#define SPI_INSTANCE_3_CLK_DIS __HAL_RCC_SPI3_CLK_DISABLE +#define SPI_INSTANCE_3_MISO_PORT GPIOB +#define SPI_INSTANCE_3_MISO_PIN GPIO_PIN_4 +#define SPI_INSTANCE_3_MISO_CLK_EN __HAL_RCC_GPIOB_CLK_ENABLE +#define SPI_INSTANCE_3_MOSI_PORT GPIOB +#define SPI_INSTANCE_3_MOSI_PIN GPIO_PIN_5 +#define SPI_INSTANCE_3_MOSI_CLK_EN __HAL_RCC_GPIOB_CLK_ENABLE +#define SPI_INSTANCE_3_SCK_PORT GPIOG +#define SPI_INSTANCE_3_SCK_PIN GPIO_PIN_9 +#define SPI_INSTANCE_3_SCK_CLK_EN __HAL_RCC_GPIOG_CLK_ENABLE +#define SPI_INSTANCE_3_NSS_PORT GPIOG +#define SPI_INSTANCE_3_NSS_PIN GPIO_PIN_12 +#define SPI_INSTANCE_3_NSS_CLK_EN __HAL_RCC_GPIOG_CLK_ENABLE + +#define NFC_INT_PIN GPIO_PIN_10 +#define NFC_INT_PORT GPIOG +#define NFC_INT_PIN_CLK_ENA __HAL_RCC_GPIOG_CLK_ENABLE +#define NFC_EXTI_INTERRUPT_GPIOSEL EXTI_GPIOG +#define NFC_EXTI_INTERRUPT_LINE EXTI_LINE_10 +#define NFC_EXTI_INTERRUPT_NUM EXTI10_IRQn +#define NFC_EXTI_INTERRUPT_HANDLER EXTI10_IRQHandler + #endif // TREZOR_T3W1_REVA_H_ diff --git a/core/embed/projects/prodtest/main.c b/core/embed/projects/prodtest/main.c index cbf4af7ec6..0e46cdcb00 100644 --- a/core/embed/projects/prodtest/main.c +++ b/core/embed/projects/prodtest/main.c @@ -60,6 +60,10 @@ #include #endif +#ifdef USE_NFC +#include +#endif + #ifdef USE_RGB_LED #include #endif diff --git a/core/site_scons/models/T3W1/trezor_t3w1_revA.py b/core/site_scons/models/T3W1/trezor_t3w1_revA.py index 4ad20e5cad..731e6bce31 100644 --- a/core/site_scons/models/T3W1/trezor_t3w1_revA.py +++ b/core/site_scons/models/T3W1/trezor_t3w1_revA.py @@ -91,6 +91,26 @@ def configure( "vendor/stm32u5xx_hal_driver/Src/stm32u5xx_hal_uart_ex.c", ] + if "nfc" in features_wanted: + sources += ["embed/io/nfc/st25r3916b/nfc.c"] + sources += ["embed/io/nfc/rfal/source/st25r3916/rfal_rfst25r3916.c"] + sources += ["embed/io/nfc/rfal/source/rfal_analogConfig.c"] + sources += ["embed/io/nfc/rfal/source/rfal_nfca.c"] + sources += ["embed/io/nfc/rfal/source/rfal_t1t.c"] + sources += ["embed/io/nfc/rfal/source/rfal_iso15693_2.c"] + sources += ["embed/io/nfc/rfal/source/rfal_crc.c"] + sources += ["embed/io/nfc/rfal/source/st25r3916/st25r3916.c"] + sources += ["embed/io/nfc/rfal/source/st25r3916/st25r3916_com.c"] + sources += ["embed/io/nfc/rfal/source/st25r3916/st25r3916_led.c"] + sources += ["embed/io/nfc/rfal/source/st25r3916/st25r3916_irq.c"] + paths += ["embed/io/nfc/inc/"] + paths += ["embed/io/nfc/st25r3916b/"] + paths += ["embed/io/nfc/rfal/source"] + paths += ["embed/io/nfc/rfal/source/st25r3916"] + paths += ["embed/io/nfc/rfal/include/"] + features_available.append("nfc") + defines += [("USE_NFC", "1")] + if "optiga" in features_wanted: sources += ["embed/sec/optiga/stm32/optiga_hal.c"] sources += ["embed/sec/optiga/optiga.c"]