ST Expansion SW Team / RFAL

Dependents:   mbed-os-nfc05a1

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers rfal_nfcDep.cpp Source File

rfal_nfcDep.cpp

00001 
00002 /******************************************************************************
00003   * @attention
00004   *
00005   * <h2><center>&copy; COPYRIGHT 2016 STMicroelectronics</center></h2>
00006   *
00007   * Licensed under ST MYLIBERTY SOFTWARE LICENSE AGREEMENT (the "License");
00008   * You may not use this file except in compliance with the License.
00009   * You may obtain a copy of the License at:
00010   *
00011   *        http://www.st.com/myliberty
00012   *
00013   * Unless required by applicable law or agreed to in writing, software 
00014   * distributed under the License is distributed on an "AS IS" BASIS, 
00015   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied,
00016   * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY,
00017   * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
00018   * See the License for the specific language governing permissions and
00019   * limitations under the License.
00020   *
00021 ******************************************************************************/
00022 
00023 
00024 /*
00025  *      PROJECT:   NFCC firmware
00026  *      LANGUAGE:  ISO C99
00027  */
00028 
00029 /*! \file rfal_nfcDep.c
00030  *
00031  *  \author  Gustavo Patricio
00032  *
00033  *  \brief Implementation of NFC-DEP protocol
00034  *  
00035  *  NFC-DEP is also known as NFCIP - Near Field Communication 
00036  *  Interface and Protocol
00037  *  
00038  *  This implementation was based on the following specs:
00039  *    - NFC Forum Digital 1.1
00040  *    - ECMA 340 3rd Edition 2013
00041  *
00042  */
00043 
00044 /*
00045  ******************************************************************************
00046  * INCLUDES
00047  ******************************************************************************
00048  */
00049 #include <platform1.h>
00050 #include "rfal_nfcDep.h"
00051 #include "rfal_nfcf.h"
00052 #include "utils.h"
00053 
00054 /*
00055  ******************************************************************************
00056  * ENABLE SWITCH
00057  ******************************************************************************
00058  */
00059 
00060 #ifndef RFAL_FEATURE_NFC_DEP
00061     #error " RFAL: Module configuration missing. Please enable/disable NFC-DEP module by setting: RFAL_FEATURE_NFC_DEP "
00062 #endif
00063 
00064 #if RFAL_FEATURE_NFC_DEP
00065 
00066 /*
00067  ******************************************************************************
00068  * DEFINES
00069  ******************************************************************************
00070  */
00071 
00072 #define NFCIP_LOOP_MAX                  0x0005FFFF                     /*!< Max blocking reRuns                                   */
00073 #define NFCIP_ATR_RETRY_MAX             2                              /*!< Max consecutive retrys of an ATR REQ with transm error*/
00074 
00075 #define NFCIP_PSLPAY_LEN                (2)                            /*!< PSL Payload length (BRS + FSL)                        */
00076 #define NFCIP_PSLREQ_LEN                (3 + RFAL_NFCDEP_LEN_LEN)      /*!< PSL REQ length (incl LEN)                             */
00077 #define NFCIP_PSLRES_LEN                (3 + RFAL_NFCDEP_LEN_LEN)      /*!< PSL RES length (incl LEN)                             */
00078 
00079 #define NFCIP_ATRREQ_BUF_LEN            (RFAL_NFCDEP_ATRREQ_MAX_LEN + RFAL_NFCDEP_LEN_LEN) /*!< ATR REQ max length (incl LEN)     */
00080 #define NFCIP_ATRRES_BUF_LEN            (RFAL_NFCDEP_ATRRES_MAX_LEN + RFAL_NFCDEP_LEN_LEN) /*!< ATR RES max length (incl LEN)     */
00081 
00082 #define NFCIP_RLSREQ_LEN                (3 + RFAL_NFCDEP_LEN_LEN)      /*!< RLS REQ length (incl LEN)                             */
00083 #define NFCIP_RLSRES_LEN                (3 + RFAL_NFCDEP_LEN_LEN)      /*!< RSL RES length (incl LEN)                             */
00084 #define NFCIP_RLSRES_MIN                (2 + RFAL_NFCDEP_LEN_LEN)      /*!< Minimum length for a RLS RES (incl LEN)               */
00085 
00086 #define NFCIP_DSLREQ_LEN                (3 + RFAL_NFCDEP_LEN_LEN)      /*!< DSL REQ length (incl LEN)                             */
00087 #define NFCIP_DSLRES_LEN                (3 + RFAL_NFCDEP_LEN_LEN)      /*!< DSL RES length (incl LEN)                             */
00088 #define NFCIP_DSLRES_MIN                (2 + RFAL_NFCDEP_LEN_LEN)      /*!< Minimum length for a DSL RES (incl LEN)               */
00089 
00090 #define NFCIP_DSLRES_MAX_LEN            (3 + RFAL_NFCDEP_LEN_LEN)      /*!< Maximum length for a DSL RES (incl LEN)               */
00091 #define NFCIP_RLSRES_MAX_LEN            (3 + RFAL_NFCDEP_LEN_LEN)      /*!< Minimum length for a RLS RES (incl LEN)               */
00092 #define NFCIP_TARGET_RES_MAX            ( MAX( NFCIP_RLSRES_MAX_LEN, NFCIP_DSLRES_MAX_LEN) ) /*!< Max target control res length   */
00093 
00094 
00095 
00096 #define NFCIP_NO_FWT                    RFAL_FWT_NONE                  /*!< No FWT value - Target Mode                            */
00097 #define NFCIP_INIT_MIN_RTOX             1                              /*!< Minimum RTOX value  Digital 1.0  14.8.4.1             */
00098 #define NFCIP_INIT_MAX_RTOX             59                             /*!< Maximum RTOX value  Digital 1.0  14.8.4.1             */
00099 
00100 #define NFCIP_TARG_MIN_RTOX             1                              /*!< Minimum target RTOX value  Digital 1.0  14.8.4.1      */
00101 #define NFCIP_TARG_MAX_RTOX             59                             /*!< Maximum target RTOX value  Digital 1.0  14.8.4.1      */
00102 
00103 #define NFCIP_TRECOV                    (1280 / 64)                    /*!< Digital 1.0  A.10  Trecov                             */
00104 
00105 #define NFCIP_TIMEOUT_ADJUSTMENT        8                              /*!< Timeout Adjustment to compensate timing from end of Tx to end of frame: (512/64)/fc */
00106 #define NFCIP_RWT_ACTIVATION            (0x40001 + NFCIP_TIMEOUT_ADJUSTMENT) /*!< Digital 1.0  A.10  RWT ACTIVATION   2^24/64[64/fc] + RWT Delta + Adjustment   */
00107 
00108 #define RFAL_NFCDEP_HEADER_PAD          (RFAL_NFCDEP_DEPREQ_HEADER_LEN - RFAL_NFCDEP_LEN_MIN) /*!< Difference between expected rcvd header len and max foreseen */
00109 
00110 
00111 #define NFCIP_MAX_TX_RETRYS             3               /*!< Number of retransmit retyrs                           */
00112 #define NFCIP_MAX_TO_RETRYS             3               /*!< Number of retrys for Timeout                          */
00113 #define NFCIP_MAX_RTOX_RETRYS           3               /*!< Number of retrys for RTOX                             */
00114 #define NFCIP_MAX_NACK_RETRYS           3               /*!< Number of retrys for NACK                             */
00115 #define NFCIP_MAX_ATN_RETRYS            3               /*!< Number of retrys for ATN                              */
00116 
00117 #define NFCIP_MIN_TXERROR_LEN           4               /*!< Minimum frame length with error to be ignored  Digital 1.0 14.12.5.4 */
00118 
00119 #define NFCIP_REQ                       0xD4            /*!<NFCIP REQuest code                                     */
00120 #define NFCIP_RES                       0xD5            /*!<NFCIP RESponce code                                    */
00121 
00122 #define NFCIP_BS_MASK                   0x0F            /*!< Bit mask for BS value on a ATR REQ/RES                */
00123 #define NFCIP_BR_MASK                   NFCIP_BS_MASK   /*!< Bit mask for BR value on a ATR REQ/RES                */
00124 
00125 #define NFCIP_PP_GB_MASK                0x02            /*!< Bit mask for GB value in PP byte on a ATR REQ/RES     */
00126 #define NFCIP_PP_NAD_MASK               0x01            /*!< Bit mask for NAD value in PP byte on a ATR REQ/RES    */
00127 
00128 #define NFCIP_PFB_xPDU_MASK             0xE0            /*!< Bit mask for PDU type                                 */
00129 #define NFCIP_PFB_IPDU                  0x00            /*!< Bit mask indicating a Information PDU                 */
00130 #define NFCIP_PFB_RPDU                  0x40            /*!< Bit mask indicating a Response PDU                    */
00131 #define NFCIP_PFB_SPDU                  0x80            /*!< Bit mask indicating a Supervisory PDU                 */
00132 
00133 #define NFCIP_PFB_MI_BIT                0x10            /*!< Bit mask for the chaining bit (MI) of PFB             */
00134 #define NFCIP_PFB_DID_BIT               0x04            /*!< Bit mask for the DID presence bit of PFB              */
00135 #define NFCIP_PFB_NAD_BIT               0x08            /*!< Bit mask for the NAD presence bit of PFB              */
00136 #define NFCIP_PFB_PNI_MASK              0x03            /*!< Bit mask for the Packet Number Information            */
00137 
00138 #define NFCIP_PFB_Rx_MASK               0x10            /*!< Bit mask for the R-PDU type                           */
00139 #define NFCIP_PFB_ACK                   0x00            /*!< Bit mask for R-PDU indicating ACK                     */
00140 #define NFCIP_PFB_NACK                  0x10            /*!< Bit mask for R-PDU indicating NAK                     */
00141 
00142 #define NFCIP_PFB_Sx_MASK               0x10            /*!< Bit mask for the R-PDU type                           */
00143 #define NFCIP_PFB_ATN                   0x00            /*!< Bit mask for R-PDU indicating ACK                     */
00144 #define NFCIP_PFB_TO                    0x10            /*!< Bit mask for R-PDU indicating NAK                     */
00145 
00146 #define NFCIP_PFB_INVALID               0xFF            /*!< Invalid PFB value                                     */
00147 
00148 /*
00149  ******************************************************************************
00150  * MACROS
00151  ******************************************************************************
00152  */
00153 
00154 #define nfcipIsTransmissionError(e)    ( (ERR_NO_MASK(e) == ERR_CRC) || (ERR_NO_MASK(e) == ERR_FRAMING) || (ERR_NO_MASK(e) == ERR_PAR) ) /*!< Checks if is a Trasmission error */
00155 
00156 
00157 #define nfcipConv1FcToMs( v )          (((v * 64) / 13560) + 1)                                       /*!< Converts value v 1fc into milliseconds (fc=13.56)     */
00158 
00159 #define nfcipCmdIsReq( cmd )           ((cmd % 2) == 0)                                               /*!< Checks if the nfcip cmd is a REQ                      */
00160 
00161 #define nfcip_PFBhasDID( pfb )         ( (pfb & NFCIP_PFB_DID_BIT) == NFCIP_PFB_DID_BIT)              /*!< Checks if pfb is signalling DID                       */
00162 #define nfcip_PFBhasNAD( pfb )         ( (pfb & NFCIP_PFB_NAD_BIT) == NFCIP_PFB_NAD_BIT)              /*!< Checks if pfb is signalling NAD                       */
00163 
00164 #define nfcip_PFBisIPDU( pfb )         ( (pfb & NFCIP_PFB_xPDU_MASK) == NFCIP_PFB_IPDU)               /*!< Checks if pfb is a Information PDU                    */
00165 #define nfcip_PFBisRPDU( pfb )         ( (pfb & NFCIP_PFB_xPDU_MASK) == NFCIP_PFB_RPDU)               /*!< Checks if pfb is Response PDU                         */
00166 #define nfcip_PFBisSPDU( pfb )         ( (pfb & NFCIP_PFB_xPDU_MASK) == NFCIP_PFB_SPDU)               /*!< Checks if pfb is a Supervisory PDU                    */
00167 
00168 #define nfcip_PFBisIMI( pfb )          ( nfcip_PFBisIPDU( pfb ) && (pfb & NFCIP_PFB_MI_BIT) == NFCIP_PFB_MI_BIT)  /*!< Checks if pfb is a Information PDU indicating MI chaining */
00169 
00170 #define nfcip_PFBisRNACK( pfb )        ( nfcip_PFBisRPDU( pfb ) && ((pfb & NFCIP_PFB_Rx_MASK) == NFCIP_PFB_NACK)) /*!< Checks if pfb is a R-PDU indicating NACK  */
00171 #define nfcip_PFBisRACK( pfb )         ( nfcip_PFBisRPDU( pfb ) && ((pfb & NFCIP_PFB_Rx_MASK) == NFCIP_PFB_ACK )) /*!< Checks if pfb is a R-PDU indicating ACK   */
00172 
00173 #define nfcip_PFBisSATN( pfb )         ( nfcip_PFBisSPDU( pfb ) && ((pfb & NFCIP_PFB_Sx_MASK) == NFCIP_PFB_ATN))  /*!< Checks if pfb is a R-PDU indicating ATN   */
00174 #define nfcip_PFBisSTO( pfb )          ( nfcip_PFBisSPDU( pfb ) && ((pfb & NFCIP_PFB_Sx_MASK) == NFCIP_PFB_TO) )  /*!< Checks if pfb is a R-PDU indicating TO    */
00175 
00176 
00177 #define nfcip_PFBIPDU( pni )           ( (uint8_t)( 0x00 | NFCIP_PFB_IPDU | (pni & NFCIP_PFB_PNI_MASK) ))/*!< Returns a PFB I-PDU with the given packet number (pni)                   */
00178 #define nfcip_PFBIPDU_MI( pni )        ( (uint8_t)(isoDep_PCBIBlock(pni) | NFCIP_PFB_MI_BIT))            /*!< Returns a PFB I-PDU with the given packet number (pni) indicating chaing */
00179 
00180 #define nfcip_PFBRPDU( pni )           ( (uint8_t)( 0x00 | NFCIP_PFB_RPDU | (pni & NFCIP_PFB_PNI_MASK) ))/*!< Returns a PFB R-PDU with the given packet number (pni)                   */
00181 #define nfcip_PFBRPDU_NACK( pni )      ( (uint8_t)(nfcip_PFBRPDU(pni) | NFCIP_PFB_NACK))                 /*!< Returns a PFB R-PDU with the given packet number (pni) indicating NACK   */
00182 #define nfcip_PFBRPDU_ACK( pni )       ( (uint8_t)(nfcip_PFBRPDU(pni) | NFCIP_PFB_ACK))                  /*!< Returns a PFB R-PDU with the given packet number (pni) indicating ACK    */
00183 
00184 #define nfcip_PFBSPDU()                ( (uint8_t)( 0x00 | NFCIP_PFB_SPDU ))                          /*!< Returns a PFB S-PDU                               */
00185 #define nfcip_PFBSPDU_ATN()            ( (uint8_t)(nfcip_PFBSPDU() | NFCIP_PFB_ATN))                  /*!< Returns a PFB S-PDU indicating ATN                */
00186 #define nfcip_PFBSPDU_TO()             ( (uint8_t)(nfcip_PFBSPDU() | NFCIP_PFB_TO))                   /*!< Returns a PFB S-PDU indicating TO                 */
00187 
00188 
00189 #define nfcip_PNIInc( pni )            ( (uint8_t) ((pni+1) & NFCIP_PFB_PNI_MASK) )                   /*!< Returns a incremented PNI from the given (pni)    */
00190 #define nfcip_PNIDec( pni )            ( (uint8_t) ((pni-1) & NFCIP_PFB_PNI_MASK) )                   /*!< Returns a decremented PNI from the given (pni)    */
00191 
00192 #define nfcip_PBF_PNI( pfb )           ( (uint8_t) (pfb & NFCIP_PFB_PNI_MASK ))                       /*!< Returns the Packet Number Information (pni)       */
00193 
00194 #define nfcip_PPwGB( lr )              ( rfalNfcDepLR2PP( lr ) | NFCIP_PP_GB_MASK)                    /*!< Returns a PP byte containing the given PP value indicating GB                  */
00195 
00196 #define nfcip_DIDMax( did )            ( MIN( (did), RFAL_NFCDEP_DID_MAX) )                           /*!< Ensures that the given did has proper value  Digital 14.6.2.3 DID [0 14]       */
00197 #define nfcip_RTOXTargMax( wt )        ( MIN( (RFAL_NFCDEP_RWT_TRG_MAX / rfalNfcDepWT2RWT(wt)), NFCIP_TARG_MAX_RTOX) )/*!< Calculates the Maximum RTOX value for the given wt as a Target */
00198 
00199 #define nfcipIsInitiator( st )         ( ((st) >= NFCIP_ST_INIT_IDLE) && ((st) <= NFCIP_ST_INIT_RLS) )/*!< Checks if module is set as Initiator                                           */
00200 #define nfcipIsTarget( st )            (!nfcipIsInitiator(st))                                        /*!< Checks if module is set as Target                                              */
00201 
00202 #define nfcipIsBRAllowed( br, mBR )    (((1<<(br)) & mBR) != 0)                                       /*!< Checks bit rate is allowed by given mask                                       */
00203 
00204 #define nfcipIsEmptyDEPEnabled( op )   (!nfcipIsEmptyDEPDisabled(op))                                 /*!< Checks if empty payload is allowed by operation config  NCI 1.0 Table 81       */
00205 #define nfcipIsEmptyDEPDisabled( op )  ((op & RFAL_NFCDEP_OPER_EMPTY_DEP_DIS) != 0)                   /*!< Checks if empty payload is not allowed by operation config  NCI 1.0 Table 81   */
00206 
00207 #define nfcipIsRTOXReqEnabled( op )    (!nfcipIsRTOXReqDisabled(op))                                  /*!< Checks if send a RTOX_REQ is allowed by operation config  NCI 1.0 Table 81     */
00208 #define nfcipIsRTOXReqDisabled( op )   ((op & RFAL_NFCDEP_OPER_RTOX_REQ_DIS) != 0)                    /*!< Checks if send a RTOX_REQ is not allowed by operation config  NCI 1.0 Table 81 */
00209 
00210 
00211 /*! Checks if isDeactivating callback is set and calls it, otherwise returns false */
00212 #define nfcipIsDeactivationPending()   ( (gNfcip.isDeactivating == NULL) ? false : gNfcip.isDeactivating() )
00213 
00214 
00215 #define nfcipRTOXAdjust( v )           (v - (v>>3))                                                   /*!< Adjust RTOX timer value to a percentage of the total, current 88% */ 
00216 
00217 /*******************************************************************************/
00218 
00219 /* timerPollTimeoutValue is necessary after timerCalculateTimeout so that system will wake up upon timer timeout. */
00220 #define nfcipTimerStart( timer, time_ms ) timer = platformTimerCreate(time_ms)                        /*!< Configures and starts the RTOX timer   */
00221 #define nfcipTimerisExpired( timer )      platformTimerIsExpired( timer )                             /*!< Checks RTOX timer has expired                   */
00222 
00223 #define nfcipLogE(...)                                                                                /*!< Macro for the error log method                  */
00224 #define nfcipLogW(...)                                                                                /*!< Macro for the warning log method                */
00225 #define nfcipLogI(...)                                                                                /*!< Macro for the info log method                   */
00226 #define nfcipLogD(...)                                                                                /*!< Macro for the debug log method                  */
00227 
00228 
00229 /*! 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 */
00230 #define nfcDepReEnableRx( rxB, rxBL, rxL )       rfalTransceiveBlockingTx( NULL, 0, rxB, rxBL, rxL, ( RFAL_TXRX_FLAGS_DEFAULT | RFAL_TXRX_FLAGS_NFCIP1_ON ), RFAL_FWT_NONE, mspiChannel, mST25, gpio_cs, IRQ, fieldLED_01, fieldLED_02, fieldLED_03, fieldLED_04, fieldLED_05, fieldLED_06 ) 
00231 
00232 
00233 uint8_t           txBuf[RFAL_NFCDEP_ATRREQ_MAX_LEN];
00234 uint8_t     rxBuf[NFCIP_ATRRES_BUF_LEN];
00235 
00236 uint8_t    txBufPSL[NFCIP_PSLREQ_LEN];
00237 uint8_t    pslBufPSL[NFCIP_PSLPAY_LEN];
00238 uint8_t    rxBufPSL[NFCIP_PSLRES_LEN];
00239 
00240 uint8_t  txBufDSL[ RFAL_NFCDEP_HEADER_PAD + NFCIP_DSLREQ_LEN];
00241 uint8_t  rxBufDSL[NFCIP_DSLRES_LEN];
00242 
00243 uint8_t    txBufRLS[RFAL_NFCDEP_HEADER_PAD + NFCIP_RLSREQ_LEN];
00244 uint8_t    rxBufRLS[NFCIP_RLSRES_LEN];
00245 
00246 /*
00247  ******************************************************************************
00248  * LOCAL DATA TYPES
00249  ******************************************************************************
00250  */
00251 
00252 /*! Struct that holds all DEP parameters/configs for the following communications */
00253 typedef struct{
00254     uint8_t   did;           /*!< Device ID (DID) to be used                      */
00255     
00256     uint8_t*  txBuf;         /*!< Pointer to the Tx buffer to be sent             */
00257     uint16_t  txBufLen;      /*!< Length of the data in the txBuf                 */
00258     uint8_t   txBufPaylPos;  /*!< Position inside txBuf where data starts         */
00259     uint8_t   txChaining;    /*!< Flag indicating chaining on transmission        */
00260     
00261     uint8_t*  rxBuf;         /*!< Pointer to the Rx buffer for incoming data      */
00262     uint16_t  rxBufLen;      /*!< Length of the data in the rxBuf                 */
00263     uint8_t   rxBufPaylPos;  /*!< Position inside rxBuf where data is to be placed*/
00264     
00265     uint32_t  fwt;           /*!< Frame Waiting Time (FWT) to be used             */
00266     uint32_t  dFwt;          /*!< Delta Frame Waiting Time (dFWT) to be used      */
00267     uint16_t  fsc;           /*!< Frame Size (FSC) to be used                     */
00268     
00269 } rfalNfcDepDEPParams;
00270 
00271 /*! NFCIP module states */
00272 typedef enum NfcipState
00273 {
00274     NFCIP_ST_IDLE,
00275     NFCIP_ST_INIT_IDLE,
00276     NFCIP_ST_INIT_ATR,
00277     NFCIP_ST_INIT_PSL,
00278     NFCIP_ST_INIT_DEP_IDLE,
00279     NFCIP_ST_INIT_DEP_TX,
00280     NFCIP_ST_INIT_DEP_RX,
00281     NFCIP_ST_INIT_DEP_ATN,
00282     NFCIP_ST_INIT_DSL,
00283     NFCIP_ST_INIT_RLS,
00284         
00285     NFCIP_ST_TARG_WAIT_ATR,
00286     NFCIP_ST_TARG_WAIT_ACTV,
00287     NFCIP_ST_TARG_DEP_IDLE,
00288     NFCIP_ST_TARG_DEP_RX,
00289     NFCIP_ST_TARG_DEP_RTOX,
00290     NFCIP_ST_TARG_DEP_TX,
00291     NFCIP_ST_TARG_DEP_SLEEP
00292 } rfalNfcDepState;
00293 
00294 /*! NFCIP commands (Request, Response) */
00295 typedef enum{
00296     NFCIP_CMD_ATR_REQ = 0x00,
00297     NFCIP_CMD_ATR_RES = 0x01,
00298     NFCIP_CMD_WUP_REQ = 0x02,
00299     NFCIP_CMD_WUP_RES = 0x03,
00300     NFCIP_CMD_PSL_REQ = 0x04,
00301     NFCIP_CMD_PSL_RES = 0x05,
00302     NFCIP_CMD_DEP_REQ = 0x06,
00303     NFCIP_CMD_DEP_RES = 0x07,
00304     NFCIP_CMD_DSL_REQ = 0x08,
00305     NFCIP_CMD_DSL_RES = 0x09,
00306     NFCIP_CMD_RLS_REQ = 0x0A,
00307     NFCIP_CMD_RLS_RES = 0x0B
00308 } rfalNfcDepCmd;
00309 
00310 
00311 /*! Struct that holds all NFCIP data */
00312 typedef struct{  
00313   rfalNfcDepConfigs        cfg;               /*!< Holds the current configuration to be used   */
00314   
00315   rfalNfcDepState         state;             /*!< Current state of the NFCIP module             */
00316   uint8_t                 pni;               /*!< Packet Number Information (PNI) counter       */
00317   
00318   uint8_t                 lastCmd;           /*!< Last command sent                             */
00319   uint8_t                 lastPFB;           /*!< Last PFB sent                                 */
00320   uint8_t                 lastPFBnATN;       /*!< Last PFB sent (excluding  ATN)                */
00321   uint8_t                 lastRTOX;          /*!< Last RTOX value sent                          */
00322   
00323   uint8_t                 cntTxRetrys;       /*!< Retransmissions counter                       */
00324   uint8_t                 cntTORetrys;       /*!< Timeouts counter                              */
00325   uint8_t                 cntRTOXRetrys;     /*!< RTOX counter                                  */
00326   uint8_t                 cntNACKRetrys;     /*!< NACK counter                                  */
00327   uint8_t                 cntATNRetrys;      /*!< Attention (ATN) counter                       */
00328   
00329   uint16_t                fsc;               /*!< Current Frame Size (FSC) to be used           */
00330   bool                    isTxChaining;      /*!< Flag for chaining on Transmission             */
00331   bool                    isRxChaining;      /*!< Flag for chaining on Reception                */
00332   uint8_t*                txBuf;             /*!< Pointer to the Tx buffer to be sent           */
00333   uint8_t*                rxBuf;             /*!< Pointer to the Rx buffer for incoming data    */
00334   uint16_t                txBufLen;          /*!< Length of the data in the txBuf               */
00335   uint16_t                rxBufLen;          /*!< Length of rxBuf buffer                        */
00336   uint16_t*               rxRcvdLen;         /*!< Length of the data in the rxBuf               */
00337   uint8_t                 txBufPaylPos;      /*!< Position in txBuf where data starts           */
00338   uint8_t                 rxBufPaylPos;      /*!< Position in rxBuf where data is to be placed  */
00339   bool                    *isChaining;       /*!< Flag for chaining on Reception                */
00340   
00341   rfalNfcDepDevice         *nfcDepDev;        /*!< Pointer to NFC-DEP device info                */
00342 
00343   uint32_t                RTOXTimer;         /*!< Timer used for RTOX                           */  
00344   rfalNfcDepDeactCallback  isDeactivating;    /*!< Deactivating flag check callback              */
00345   
00346   bool                    isReqPending;      /*!< Flag pending REQ from Target activation       */
00347   bool                    isTxPending;       /*!< Flag pending DEP Block while waiting RTOX Ack */
00348   bool                    isWait4RTOX;       /*!< Flag for waiting RTOX Ack                     */
00349 }rfalNfcDep;
00350 
00351 
00352 /*
00353  ******************************************************************************
00354  * LOCAL VARIABLES
00355  ******************************************************************************
00356  */
00357 
00358 static rfalNfcDep gNfcip;              /*!< NFCIP module instance                          */
00359 
00360 
00361 /*
00362  ******************************************************************************
00363  * LOCAL FUNCTION PROTOTYPES
00364  ******************************************************************************
00365  */
00366 
00367 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, SPI* mspiChannel, ST25R3911* mST25, DigitalOut* gpio_cs, InterruptIn* IRQ, DigitalOut* fieldLED_01, DigitalOut* fieldLED_02, DigitalOut* fieldLED_03, DigitalOut* fieldLED_04, DigitalOut* fieldLED_05, DigitalOut* fieldLED_06 );
00368 static ReturnCode nfcipTx( rfalNfcDepCmd cmd, uint8_t* txBuf, uint8_t *paylBuf, uint16_t paylLen, uint8_t pfb, uint32_t fwt, SPI* mspiChannel, ST25R3911* mST25, DigitalOut* gpio_cs, InterruptIn* IRQ, DigitalOut* fieldLED_01, DigitalOut* fieldLED_02, DigitalOut* fieldLED_03, DigitalOut* fieldLED_04, DigitalOut* fieldLED_05, DigitalOut* fieldLED_06 );
00369 static ReturnCode nfcipDEPControlMsg( uint8_t pfb, uint8_t RTOX, SPI* mspiChannel, ST25R3911* mST25, DigitalOut* gpio_cs, InterruptIn* IRQ, DigitalOut* fieldLED_01, DigitalOut* fieldLED_02, DigitalOut* fieldLED_03, DigitalOut* fieldLED_04, DigitalOut* fieldLED_05, DigitalOut* fieldLED_06 );
00370 static ReturnCode nfcipInitiatorHandleDEP( ReturnCode rxRes, uint16_t rxLen, uint16_t *outActRxLen, bool *outIsChaining, SPI* mspiChannel, ST25R3911* mST25, DigitalOut* gpio_cs, InterruptIn* IRQ, DigitalOut* fieldLED_01, DigitalOut* fieldLED_02, DigitalOut* fieldLED_03, DigitalOut* fieldLED_04, DigitalOut* fieldLED_05, DigitalOut* fieldLED_06 );
00371 static ReturnCode nfcipTargetHandleRX( ReturnCode rxRes, uint16_t *outActRxLen, bool *outIsChaining, SPI* mspiChannel, ST25R3911* mST25, DigitalOut* gpio_cs, InterruptIn* IRQ, DigitalOut* fieldLED_01, DigitalOut* fieldLED_02, DigitalOut* fieldLED_03, DigitalOut* fieldLED_04, DigitalOut* fieldLED_05, DigitalOut* fieldLED_06 );
00372 static ReturnCode nfcipTargetHandleActivation( rfalNfcDepDevice  *nfcDepDev, uint8_t *outBRS, SPI* mspiChannel, ST25R3911* mST25, DigitalOut* gpio_cs, InterruptIn* IRQ, DigitalOut* fieldLED_01, DigitalOut* fieldLED_02, DigitalOut* fieldLED_03, DigitalOut* fieldLED_04, DigitalOut* fieldLED_05, DigitalOut* fieldLED_06 );
00373 
00374 
00375 /*!
00376  ******************************************************************************
00377  * \brief NFCIP Congigure
00378  * 
00379  * Configures the nfcip layer with the given configurations 
00380  * 
00381  * \param[in] cfg   : nfcip configuration for following communication
00382  ******************************************************************************
00383  */
00384 static void nfcipConfig( rfalNfcDepConfigs  cfg );
00385 
00386 
00387 /*!
00388  ******************************************************************************
00389  * \brief Set DEP parameters
00390  * 
00391  * This method sets the parameters/configs for following Data Exchange
00392  * Sets the nfcip module state according to the role it is configured
00393  * 
00394  * 
00395  * \warning To be used only after proper Initiator/Target activation: 
00396  *           nfcipTargetHandleActivation() or nfcipInitiatorActivate() has
00397  *           returned success
00398  *           
00399  *  This must be called before  nfcipRun() in case of Target to pass 
00400  *  rxBuffer
00401  *  
00402  *  Everytime some data needs to be transmitted call this to set it and 
00403  *  call nfcipRun() until done or error
00404  * 
00405  * \param[in]  DEPParams  : the parameters to be used during Data Exchange 
00406  ******************************************************************************
00407  */
00408 static void nfcipSetDEPParams( rfalNfcDepDEPParams *DEPParams );
00409 
00410 
00411 /*!
00412  ******************************************************************************
00413  * \brief NFCIP run protocol
00414  * 
00415  * This method handles all the nfcip protocol during Data Exchange (DEP
00416  *  requests and responses).
00417  *  
00418  *  A data exchange cycle is considered a DEP REQ and a DEP RES.
00419  *  
00420  *  In case of Tx chaining(MI) must signal it with  nfcipSetDEPParams()
00421  *  In case of Rx chaining(MI) outIsChaining will be set to true and the 
00422  *  current data returned
00423  *  
00424  * \param[out] outActRxLen   : data received length
00425  * \param[out] outIsChaining : true if other peer is performing chaining(MI)
00426  *  
00427  * \return ERR_NONE    : Data exchange cycle completed successfully
00428  * \return ERR_TIMEOUT : Timeout occurred
00429  * \return ERR_PROTO   : Protocol error occurred
00430  * \return ERR_AGAIN   : Other peer is doing chaining(MI), current block 
00431  *                       was received successfully call again until complete
00432  * 
00433  ******************************************************************************
00434  */
00435 static ReturnCode nfcipRun( uint16_t *outActRxLen, bool *outIsChaining, SPI* mspiChannel, ST25R3911* mST25, DigitalOut* gpio_cs, InterruptIn* IRQ, DigitalOut* fieldLED_01, DigitalOut* fieldLED_02, DigitalOut* fieldLED_03, DigitalOut* fieldLED_04, DigitalOut* fieldLED_05, DigitalOut* fieldLED_06 );
00436 
00437 
00438 /*!
00439  ******************************************************************************
00440  * \brief Transmission method
00441  * 
00442  * This method checks if the current communication is Active or Passive
00443  * and performs the necessary procedures for each communication type
00444  * 
00445  * Transmits the data hold in txBuf 
00446  * 
00447  * \param[in]  txBuf    : buffer to transmit
00448  * \param[in]  txBufLen : txBuffer capacity 
00449  * \param[in]  fwt      : fwt for current Tx
00450  * 
00451  * \return ERR_NONE       : No error
00452  ******************************************************************************
00453  */
00454 static ReturnCode nfcipDataTx( uint8_t* txBuf, uint16_t txBufLen, uint32_t fwt, SPI* mspiChannel, ST25R3911* mST25, DigitalOut* gpio_cs, InterruptIn* IRQ, DigitalOut* fieldLED_01, DigitalOut* fieldLED_02, DigitalOut* fieldLED_03, DigitalOut* fieldLED_04, DigitalOut* fieldLED_05, DigitalOut* fieldLED_06 );
00455 
00456 
00457 /*!
00458  ******************************************************************************
00459  * \brief Reception method
00460  * 
00461  * This method checks if the current communication is Active or Passive
00462  * and calls the appropriate reception method
00463  * 
00464  * Copies incoming data to rxBuf
00465  * 
00466  * \param[out] rxBuf       : buffer to hold incomming data 
00467  * \param[in]  rxBufLen    : rxBuf capacity 
00468  * \param[out] actualRxLen : length of the data received  
00469  * 
00470  * \return ERR_NONE       : No error
00471  ******************************************************************************
00472  */
00473 static ReturnCode nfcipDataRx( void );
00474 
00475 
00476 /*
00477  ******************************************************************************
00478  * LOCAL FUNCTIONS
00479  ******************************************************************************
00480  */
00481 
00482 /*******************************************************************************/
00483 
00484 
00485 /*******************************************************************************/
00486 static bool nfcipDxIsSupported( uint8_t Dx, uint8_t BRx, uint8_t BSx )
00487 {
00488     uint8_t Bx;
00489     
00490     /* Take the min of the possible bit rates, we'll use one for both directions */
00491     Bx = MIN(BRx, BSx);
00492     
00493     /* Lower bit rates must be supported for P2P */
00494     if( (Dx <= RFAL_NFCDEP_Dx_04_424 ) )
00495     {
00496         return true;
00497     }
00498     
00499     if( (Dx == RFAL_NFCDEP_Dx_08_848 ) && (Bx >= RFAL_NFCDEP_Bx_08_848 ) )
00500     {
00501         return true;
00502     }
00503 
00504     return false;
00505 }
00506 
00507 
00508 /*******************************************************************************/
00509 static ReturnCode nfcipTxRx( rfalNfcDepCmd cmd, uint8_t* txBuf, uint32_t fwt, uint8_t* paylBuf,
00510         uint8_t paylBufLen, uint8_t* rxBuf, uint16_t rxBufLen, uint16_t *rxActLen,
00511         SPI* mspiChannel, ST25R3911* mST25, DigitalOut* gpio_cs, InterruptIn* IRQ,
00512         DigitalOut* fieldLED_01, DigitalOut* fieldLED_02, DigitalOut* fieldLED_03,
00513         DigitalOut* fieldLED_04, DigitalOut* fieldLED_05, DigitalOut* fieldLED_06 )
00514 {
00515     ReturnCode ret;
00516     uint32_t   reRun;
00517     
00518     reRun = NFCIP_LOOP_MAX;                                          /* set maximum loop reRuns */
00519     
00520     if( (cmd == NFCIP_CMD_DEP_REQ) || (cmd == NFCIP_CMD_DEP_RES) ) /* this method cannot be used for DEPs */
00521     {
00522         return ERR_PARAM;
00523     }
00524     
00525     /* Assign the global params for this TxRx */
00526     gNfcip.rxBuf       = rxBuf;
00527     gNfcip.rxBufLen    = rxBufLen;
00528     gNfcip.rxRcvdLen   = rxActLen;
00529     
00530     
00531     /*******************************************************************************/
00532     /* Transmission                                                                */
00533     /*******************************************************************************/
00534     if(txBuf != NULL)                                                /* if nothing to Tx, just do Rx */               
00535     {
00536         EXIT_ON_ERR( ret, nfcipTx( cmd, txBuf, paylBuf, paylBufLen, 0, fwt, mspiChannel, mST25, gpio_cs, IRQ, fieldLED_01, fieldLED_02, fieldLED_03, fieldLED_04, fieldLED_05, fieldLED_06 )  );
00537     }
00538     
00539     /*******************************************************************************/
00540     /* Reception                                                                   */
00541     /*******************************************************************************/
00542     do                                                               /* call Rx() until done or max reRuns reached */
00543     {
00544         ret = nfcipDataRx();
00545         
00546 
00547         rfalWorker( mspiChannel, mST25, gpio_cs, IRQ, fieldLED_01, fieldLED_02, fieldLED_03, fieldLED_04, fieldLED_05, fieldLED_06 ) ;
00548              
00549         if( !reRun-- )                                               /* if max reRuns reached return error */
00550         {
00551             return ERR_MAX_RERUNS;
00552         }
00553     }
00554     while( ret == ERR_NO_MASK(ERR_BUSY) );
00555     
00556     if( ret != ERR_NONE )
00557     {
00558         return ret;
00559     }
00560     
00561     /*******************************************************************************/    
00562     *rxActLen = *rxBuf;                                             /* Use LEN byte instead due to with/without CRC modes */
00563     return ERR_NONE;                                                /* Tx and Rx completed successfully                   */
00564 }
00565 
00566 
00567 /*******************************************************************************/
00568 static ReturnCode nfcipDEPControlMsg( uint8_t pfb, uint8_t RTOX, SPI* mspiChannel, ST25R3911* mST25, DigitalOut* gpio_cs, InterruptIn* IRQ, DigitalOut* fieldLED_01, DigitalOut* fieldLED_02, DigitalOut* fieldLED_03, DigitalOut* fieldLED_04, DigitalOut* fieldLED_05, DigitalOut* fieldLED_06 )
00569 {
00570     uint8_t        ctrlMsg[20];
00571     rfalNfcDepCmd  depCmd;
00572     uint32_t       fwt;
00573 
00574     /*******************************************************************************/
00575     /* Calculate Cmd and fwt to be used                                            */
00576     /*******************************************************************************/
00577     depCmd = ((gNfcip.cfg.role == RFAL_NFCDEP_ROLE_TARGET ) ? NFCIP_CMD_DEP_RES : NFCIP_CMD_DEP_REQ);        
00578     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) ) );
00579     
00580     if( nfcip_PFBisSTO( pfb ) )
00581     {
00582         return nfcipTx( depCmd, ctrlMsg, &RTOX, sizeof(RTOX), pfb, fwt, mspiChannel, mST25, gpio_cs, IRQ, fieldLED_01, fieldLED_02, fieldLED_03, fieldLED_04, fieldLED_05, fieldLED_06 ) ;
00583     }
00584     else
00585     {
00586         return nfcipTx( depCmd, ctrlMsg, 0, 0, pfb, fwt, mspiChannel, mST25, gpio_cs, IRQ, fieldLED_01, fieldLED_02, fieldLED_03, fieldLED_04, fieldLED_05, fieldLED_06 ) ;
00587     }
00588 }
00589 
00590 /*******************************************************************************/
00591 static void nfcipClearCounters( void )
00592 {
00593     gNfcip.cntATNRetrys  = 0;
00594     gNfcip.cntNACKRetrys = 0;
00595     gNfcip.cntTORetrys   = 0;
00596     gNfcip.cntTxRetrys   = 0;
00597     gNfcip.cntRTOXRetrys = 0;
00598 }
00599 
00600 /*******************************************************************************/
00601 static ReturnCode nfcipInitiatorHandleDEP( ReturnCode rxRes, uint16_t rxLen, uint16_t *outActRxLen, bool *outIsChaining, SPI* mspiChannel, ST25R3911* mST25, DigitalOut* gpio_cs, InterruptIn* IRQ, DigitalOut* fieldLED_01, DigitalOut* fieldLED_02, DigitalOut* fieldLED_03, DigitalOut* fieldLED_04, DigitalOut* fieldLED_05, DigitalOut* fieldLED_06 )
00602 {
00603     ReturnCode ret;
00604     uint8_t    nfcDepLen;
00605     uint8_t    rxMsgIt;
00606     uint8_t    rxPFB;
00607     uint8_t    rxRTOX;
00608     uint8_t    optHdrLen;
00609     
00610     ret        = ERR_INTERNAL;
00611     rxMsgIt    = 0;
00612     optHdrLen  = 0;
00613     
00614     *outActRxLen    = 0;
00615     *outIsChaining  = false;
00616     
00617     
00618     /*******************************************************************************/
00619     /* Handle reception errors                                                     */
00620     /*******************************************************************************/
00621     switch( rxRes )
00622     {
00623         /*******************************************************************************/
00624         /* Timeout ->  Digital 1.0 14.15.5.6 */
00625         case ERR_TIMEOUT:
00626             
00627             nfcipLogI( " NFCIP(I) TIMEOUT  TORetrys:%d \r\n", gNfcip.cntTORetrys );
00628             
00629             /* Digital 1.0 14.15.5.6 - If nTO >= Max raise protocol error */
00630             if( gNfcip.cntTORetrys++ >= NFCIP_MAX_TO_RETRYS )
00631             {
00632                 return ERR_PROTO;
00633             }
00634 
00635             /*******************************************************************************/
00636             /* Upon Timeout error, if Deactivation is pending, no more error recovery 
00637              * will be done #54. 
00638              * This is used to address the issue some devices that havea big TO. 
00639              * Normally LLCP layer has timeout already, and NFCIP layer is still
00640              * running error handling, retrying ATN/NACKs                                  */
00641             /*******************************************************************************/
00642             if( nfcipIsDeactivationPending() )
00643             {
00644                 nfcipLogI( " skipping error recovery due deactivation pending \r\n");
00645                 return ERR_TIMEOUT;
00646             }
00647             
00648             /* Digital 1.0 14.15.5.6 1)  If last PDU was NACK */
00649             if( nfcip_PFBisRNACK(gNfcip.lastPFB) )
00650             {
00651                 /* Digital 1.0 14.15.5.6 2)  if NACKs failed raise protocol error  */
00652                 if( gNfcip.cntNACKRetrys++ >= NFCIP_MAX_NACK_RETRYS )
00653                 {
00654                     return ERR_PROTO;
00655                 }
00656                 
00657                 /* Send NACK */
00658                 nfcipLogI( " NFCIP(I) Sending NACK retry: %d \r\n", gNfcip.cntNACKRetrys );
00659                 EXIT_ON_ERR( ret, nfcipDEPControlMsg( nfcip_PFBRPDU_NACK(gNfcip.pni), 0, mspiChannel, mST25, gpio_cs, IRQ, fieldLED_01, fieldLED_02, fieldLED_03, fieldLED_04, fieldLED_05, fieldLED_06 )  );
00660                 return ERR_BUSY;
00661             }
00662             
00663             nfcipLogI( " NFCIP(I) Checking if to send ATN  ATNRetrys: %d \r\n", gNfcip.cntATNRetrys );
00664             
00665             /* Digital 1.0 14.15.5.6 3)  Otherwise send ATN */                            
00666             if( gNfcip.cntATNRetrys++ >= NFCIP_MAX_NACK_RETRYS )
00667             {
00668                 return ERR_PROTO;
00669             }
00670                             
00671             /* Send ATN */
00672             nfcipLogI( " NFCIP(I) Sending ATN \r\n" );
00673             EXIT_ON_ERR( ret, nfcipDEPControlMsg( nfcip_PFBSPDU_ATN(), 0, mspiChannel, mST25, gpio_cs, IRQ, fieldLED_01, fieldLED_02, fieldLED_03, fieldLED_04, fieldLED_05, fieldLED_06 )  );
00674             return ERR_BUSY;
00675         
00676         /*******************************************************************************/
00677         /* Data rcvd with error ->  Digital 1.0 14.12.5.4 */
00678         case ERR_CRC:
00679         case ERR_PAR:
00680         case ERR_FRAMING:
00681         case ERR_RF_COLLISION:
00682             
00683             nfcipLogI( " NFCIP(I) rx Error: %d \r\n", rxRes );
00684            
00685             /* Digital 1.0 14.12.5.4 Tx Error with data, ignore */
00686             if( rxLen < NFCIP_MIN_TXERROR_LEN )
00687             {
00688                 nfcipLogI( " NFCIP(I) Transmission error w data  \r\n" );
00689 #if 0
00690                 if(gNfcip.cfg.commMode == RFAL_NFCDEP_COMM_PASSIVE )
00691                 {
00692                     nfcipLogI( " NFCIP(I) Transmission error w data -> reEnabling Rx \r\n" );
00693                     nfcipReEnableRxTout( NFCIP_TRECOV );
00694                     return ERR_BUSY;
00695                 }
00696 #endif /* 0 */
00697             }
00698 
00699             /* Digital 1.1 16.12.5.4  if NACKs failed raise Transmission error  */
00700             if( gNfcip.cntNACKRetrys++ >= NFCIP_MAX_NACK_RETRYS )
00701             {
00702                 return ERR_FRAMING;
00703             }
00704                             
00705             /* Send NACK */
00706             nfcipLogI( " NFCIP(I) Sending NACK  \r\n" );
00707             EXIT_ON_ERR( ret, nfcipDEPControlMsg( nfcip_PFBRPDU_NACK(gNfcip.pni), 0, mspiChannel, mST25, gpio_cs, IRQ, fieldLED_01, fieldLED_02, fieldLED_03, fieldLED_04, fieldLED_05, fieldLED_06 )  );
00708             return ERR_BUSY;
00709             
00710         case ERR_NONE:
00711             break;
00712             
00713         case ERR_BUSY:
00714             return ERR_BUSY;  /* Debug purposes */
00715             
00716         default:
00717             nfcipLogW( " NFCIP(I) Error: %d \r\n", rxRes );
00718             return rxRes;
00719     }    
00720         
00721     /*******************************************************************************/
00722     /* Rx OK check if valid DEP PDU                                                */
00723     /*******************************************************************************/
00724     
00725     /* Due to different modes on ST25R391x (with/without CRC) use NFC-DEP LEN instead of bytes retrieved */
00726     nfcDepLen = gNfcip.rxBuf[rxMsgIt++];
00727     
00728     nfcipLogD( " NFCIP(I) rx OK: %d bytes \r\n", nfcDepLen );
00729     
00730     /* Digital 1.0 14.15.5.5 Protocol Error  */
00731     if( gNfcip.rxBuf[rxMsgIt++] != NFCIP_RES )
00732     {
00733         nfcipLogW( " NFCIP(I) error %02X instead of %02X \r\n", gNfcip.rxBuf[--rxMsgIt], NFCIP_RES );
00734         return ERR_PROTO;
00735     }
00736     
00737     /* Digital 1.0 14.15.5.5 Protocol Error  */
00738     if( gNfcip.rxBuf[rxMsgIt++] != NFCIP_CMD_DEP_RES )
00739     {
00740         nfcipLogW( " NFCIP(I) error %02X instead of %02X \r\n", gNfcip.rxBuf[--rxMsgIt], NFCIP_CMD_DEP_RES );
00741         return ERR_PROTO;
00742     }
00743     
00744     rxPFB = gNfcip.rxBuf[rxMsgIt++];
00745     
00746     /*******************************************************************************/
00747     /* Check for valid PFB type                                                    */
00748     if( !(nfcip_PFBisSPDU( rxPFB ) || nfcip_PFBisRPDU( rxPFB ) || nfcip_PFBisIPDU( rxPFB )) )
00749     {
00750         return ERR_PROTO;
00751     }
00752         
00753     /*******************************************************************************/
00754     /* Digital 1.0 14.8.2.1  check if DID is expected and match -> Protocol Error  */
00755     if( gNfcip.cfg.did != RFAL_NFCDEP_DID_NO ) 
00756     {
00757         if( !nfcip_PFBhasDID( rxPFB ) || gNfcip.rxBuf[rxMsgIt++] != gNfcip.cfg.did )
00758         {
00759             return ERR_PROTO;
00760         }
00761         optHdrLen++;                                    /* Inc header optional field cnt*/
00762     }
00763     else if( nfcip_PFBhasDID( rxPFB ) )                 /* DID not expected but rcv */
00764     {
00765         return ERR_PROTO;
00766     }
00767     
00768     /*******************************************************************************/
00769     /* Digital 1.0 14.6.2.8 & 14.6.3.11 NAD must not be used  */
00770     if( gNfcip.cfg.nad != RFAL_NFCDEP_NAD_NO ) 
00771     {
00772         if( !nfcip_PFBhasNAD( rxPFB ) || gNfcip.rxBuf[rxMsgIt++] != gNfcip.cfg.nad )
00773         {
00774             return ERR_PROTO;
00775         }
00776         optHdrLen++;                                    /* Inc header optional field cnt*/
00777     }
00778     else if( nfcip_PFBhasNAD( rxPFB ) )                 /* NAD not expected but rcv */
00779     {
00780         return ERR_PROTO;
00781     }
00782        
00783     /*******************************************************************************/
00784     /* Process R-PDU                                                               */
00785     /*******************************************************************************/
00786     if( nfcip_PFBisRPDU( rxPFB ) )
00787     {
00788         /*******************************************************************************/
00789         /* R ACK                                                                       */
00790         /*******************************************************************************/
00791         if( nfcip_PFBisRACK( rxPFB ) )
00792         {            
00793             nfcipLogI( " NFCIP(I) Rcvd ACK  \r\n" );
00794             if( gNfcip.pni == nfcip_PBF_PNI( rxPFB ) )
00795             {
00796                 /* 14.12.3.3 R-ACK with correct PNI -> Increment */
00797                 gNfcip.pni = nfcip_PNIInc( gNfcip.pni );
00798                                 
00799                 /* R-ACK while not performing chaining -> Protocol error*/
00800                 if( !gNfcip.isTxChaining )
00801                 {
00802                     return ERR_PROTO;
00803                 }
00804                 
00805                 nfcipClearCounters();
00806                 gNfcip.state = NFCIP_ST_INIT_DEP_IDLE;
00807                 return ERR_NONE;                            /* This block has been transmitted */
00808             }
00809             else  /* Digital 1.0 14.12.4.5 ACK with wrong PNI Initiator may retransmit */
00810             {
00811                 if( gNfcip.cntTxRetrys++ >= NFCIP_MAX_TX_RETRYS )
00812                 {
00813                     return ERR_PROTO;
00814                 }
00815                 
00816                 /* Extended the MAY in Digital 1.0 14.12.4.5 to only reTransmit if the ACK
00817                  * is for the previous DEP, otherwise raise Protocol immediately 
00818                  * If the PNI difference is more than 1 it`s worthless to reTransmit 3x
00819                  * and after raise the error                                              */
00820                 
00821                 if( nfcip_PNIDec( gNfcip.pni ) ==  nfcip_PBF_PNI( rxPFB ) )
00822                 {
00823                     /* ReTransmit */
00824                     nfcipLogI( " NFCIP(I) Rcvd ACK prev PNI -> reTx \r\n" );
00825                     gNfcip.state = NFCIP_ST_INIT_DEP_TX;
00826                     return ERR_BUSY;
00827                 }
00828                 
00829                 nfcipLogI( " NFCIP(I) Rcvd ACK unexpected far PNI -> Error \r\n" );
00830                 return ERR_PROTO;
00831             }
00832         }
00833         else /* Digital 1.0 - 14.12.5.2 Target must never send NACK  */
00834         {            
00835             return ERR_PROTO;
00836         }
00837     }
00838     
00839     /*******************************************************************************/
00840     /* Process S-PDU                                                               */
00841     /*******************************************************************************/
00842     if( nfcip_PFBisSPDU( rxPFB ) )                                
00843     {       
00844         nfcipLogI( " NFCIP(I) Rcvd S-PDU  \r\n" );
00845         /*******************************************************************************/
00846         /* S ATN                                                                       */
00847         /*******************************************************************************/
00848         if( nfcip_PFBisSATN( rxPFB ) )                         /*    If is a S-ATN     */
00849         {            
00850             nfcipLogI( " NFCIP(I) Rcvd ATN  \r\n" );
00851             if( nfcip_PFBisSATN( gNfcip.lastPFB ) )            /* Check if is expected */
00852             {                
00853                 gNfcip.cntATNRetrys = 0;                       /* Clear ATN counter    */
00854                 
00855                 /* Although spec is not clear NFC Forum Digital test is expecting to
00856                  * retransmit upon receiving ATN_RES */
00857                 if( nfcip_PFBisSTO( gNfcip.lastPFBnATN ) )
00858                 {
00859                     nfcipLogI( " NFCIP(I) Rcvd ATN  -> reTx RTOX_RES \r\n" );
00860                     EXIT_ON_ERR( ret, nfcipDEPControlMsg( nfcip_PFBSPDU_TO(), gNfcip.lastRTOX, mspiChannel, mST25, gpio_cs, IRQ, fieldLED_01, fieldLED_02, fieldLED_03, fieldLED_04, fieldLED_05, fieldLED_06 )  );
00861                 }
00862                 else
00863                 {
00864                     /* ReTransmit ? */
00865                     if( gNfcip.cntTxRetrys++ >= NFCIP_MAX_TX_RETRYS )
00866                     {
00867                         return ERR_PROTO;
00868                     }
00869                     
00870                     nfcipLogI( " NFCIP(I) Rcvd ATN  -> reTx  PNI: %d \r\n", gNfcip.pni );
00871                     gNfcip.state = NFCIP_ST_INIT_DEP_TX;
00872                 }
00873                 
00874                 return ERR_BUSY;
00875             }
00876             else                                                 /* Digital 1.0  14.12.4.4 & 14.12.4.8 */
00877             {
00878                 return ERR_PROTO;
00879             }
00880         }
00881         /*******************************************************************************/
00882         /* S TO                                                                        */
00883         /*******************************************************************************/
00884         else if( nfcip_PFBisSTO( rxPFB ) )                        /* If is a S-TO (RTOX)  */
00885         {
00886             nfcipLogI( " NFCIP(I) Rcvd TO  \r\n" );
00887             
00888             rxRTOX = gNfcip.rxBuf[rxMsgIt++];
00889             
00890             /* Digital 1.1 16.12.4.3 - Initiator MAY stop accepting subsequent RTOX Req   *
00891              *                       - RTOX request to an ATN -> Protocol error           */
00892             if( (gNfcip.cntRTOXRetrys++ > NFCIP_MAX_RTOX_RETRYS) || nfcip_PFBisSATN( gNfcip.lastPFB ) )
00893             {
00894                 return ERR_PROTO;
00895             }
00896             
00897             /* Digital 1.1 16.8.4.1 RTOX must be between [1,59] */
00898             if( (rxRTOX < NFCIP_INIT_MIN_RTOX) || (rxRTOX > NFCIP_INIT_MAX_RTOX) )
00899             {
00900                 return ERR_PROTO;
00901             }
00902             
00903             EXIT_ON_ERR( ret, nfcipDEPControlMsg( nfcip_PFBSPDU_TO(), rxRTOX, mspiChannel, mST25, gpio_cs, IRQ, fieldLED_01, fieldLED_02, fieldLED_03, fieldLED_04, fieldLED_05, fieldLED_06 )  );
00904             gNfcip.lastRTOX = rxRTOX;
00905             
00906             return ERR_BUSY;
00907         }
00908         
00909         /* Unexpected S-PDU */
00910         return ERR_PROTO;
00911     }
00912     
00913     /*******************************************************************************/
00914     /* Process I-PDU                                                               */
00915     /*******************************************************************************/
00916     if( nfcip_PFBisIPDU( rxPFB ) )
00917     {
00918         if( gNfcip.pni != nfcip_PBF_PNI( rxPFB ) )
00919         {
00920             nfcipLogI( " NFCIP(I) Rcvd IPDU wrong PNI     curPNI: %d rxPNI: %d \r\n", gNfcip.pni  , nfcip_PBF_PNI( rxPFB ) );
00921             return ERR_PROTO;
00922         }
00923         
00924         nfcipLogD( " NFCIP(I) Rcvd IPDU OK    PNI: %d \r\n", gNfcip.pni );
00925         
00926         /* 14.12.3.3 I-PDU with correct PNI -> Increment */
00927         gNfcip.pni = nfcip_PNIInc( gNfcip.pni );
00928                 
00929         
00930         /* Successful data Exchange */
00931         nfcipClearCounters();
00932         *outActRxLen  = (nfcDepLen - RFAL_NFCDEP_DEP_HEADER - optHdrLen);
00933         
00934         if( (gNfcip.rxBuf + gNfcip.rxBufPaylPos) != (gNfcip.rxBuf + RFAL_NFCDEP_DEP_HEADER + optHdrLen) )
00935         {
00936             ST_MEMMOVE( (gNfcip.rxBuf + gNfcip.rxBufPaylPos), (gNfcip.rxBuf + RFAL_NFCDEP_DEP_HEADER + optHdrLen), *outActRxLen );
00937         }
00938 
00939         /*******************************************************************************/
00940         /* Check if target is indicating chaining MI                                   */
00941         /*******************************************************************************/
00942         if( nfcip_PFBisIMI( rxPFB ) )
00943         {
00944             gNfcip.isRxChaining = true;
00945             *outIsChaining      = true;
00946             
00947             nfcipLogD( " NFCIP(I) Rcvd IPDU OK w MI -> ACK \r\n" );
00948             EXIT_ON_ERR( ret, nfcipDEPControlMsg( nfcip_PFBRPDU_ACK( gNfcip.pni ), gNfcip.rxBuf[rxMsgIt++], mspiChannel, mST25, gpio_cs, IRQ, fieldLED_01, fieldLED_02, fieldLED_03, fieldLED_04, fieldLED_05, fieldLED_06 )  );
00949             
00950             return ERR_AGAIN;  /* Send Again signalling to run again, but some chaining data has arrived*/
00951         }
00952         else
00953         {
00954             gNfcip.isRxChaining = false;
00955             gNfcip.state        = NFCIP_ST_INIT_DEP_IDLE;
00956             
00957             return ERR_NONE;                            /* Data exchange done */
00958         }
00959     }
00960     return ret;
00961 }
00962 
00963 
00964 /*******************************************************************************/
00965 static ReturnCode nfcipTargetHandleRX( ReturnCode rxRes, uint16_t *outActRxLen, bool *outIsChaining, SPI* mspiChannel, ST25R3911* mST25, DigitalOut* gpio_cs, InterruptIn* IRQ, DigitalOut* fieldLED_01, DigitalOut* fieldLED_02, DigitalOut* fieldLED_03, DigitalOut* fieldLED_04, DigitalOut* fieldLED_05, DigitalOut* fieldLED_06 )
00966 {
00967     ReturnCode ret;
00968     uint8_t    nfcDepLen;
00969     uint8_t    rxMsgIt;
00970     uint8_t    rxPFB;
00971     uint8_t    optHdrLen;
00972     uint8_t    resBuf[NFCIP_TARGET_RES_MAX];
00973         
00974     
00975     ret        = ERR_INTERNAL;
00976     rxMsgIt    = 0;
00977     optHdrLen  = 0;
00978     
00979     *outActRxLen    = 0;
00980     *outIsChaining  = false;
00981     
00982     
00983     /*******************************************************************************/
00984     /* Handle reception errors                                                     */
00985     /*******************************************************************************/
00986     switch( rxRes )
00987     {
00988         /*******************************************************************************/        
00989         case ERR_NONE:
00990             break;
00991             
00992         case ERR_TIMEOUT:
00993         case ERR_CRC:
00994         case ERR_PAR:
00995         case ERR_FRAMING:
00996         case ERR_PROTO:
00997         default:
00998             /* Digital 1.1  16.12.5.2 The Target MUST NOT attempt any error recovery.      *
00999              * The Target MUST always stay in receive mode when a                          *
01000              * Transmission Error or a Protocol Error occurs.                              *
01001              *                                                                             *
01002              * Do not push Transmission/Protocol Errors to upper layer in Listen Mode #766 */
01003             
01004             nfcDepReEnableRx( gNfcip.rxBuf, gNfcip.rxBufLen, gNfcip.rxRcvdLen );
01005             return ERR_BUSY;
01006         
01007         case ERR_LINK_LOSS:
01008             nfcipLogW( " NFCIP(T) Error: %d \r\n", rxRes );
01009             return rxRes;
01010             
01011         case ERR_BUSY:
01012             return ERR_BUSY;  /* Debug purposes */
01013             
01014     }    
01015         
01016     /*******************************************************************************/
01017     /* Rx OK check if valid DEP PDU                                                */
01018     /*******************************************************************************/
01019     
01020     /* Due to different modes on ST25R391x (with/without CRC) use NFC-DEP LEN instead of bytes retrieved */
01021     nfcDepLen = gNfcip.rxBuf[rxMsgIt++];
01022         
01023     nfcipLogD( " NFCIP(T) rx OK: %d bytes \r\n", nfcDepLen );
01024     
01025     if( gNfcip.rxBuf[rxMsgIt++] != NFCIP_REQ )
01026     {
01027         nfcDepReEnableRx( gNfcip.rxBuf, gNfcip.rxBufLen, gNfcip.rxRcvdLen );
01028         return ERR_BUSY; /* ERR_PROTO - Ignore bad request */
01029     }
01030     
01031     
01032     /*******************************************************************************/
01033     /* Check whether target rcvd a normal DEP or deactivation request              */
01034     /*******************************************************************************/
01035     switch( gNfcip.rxBuf[rxMsgIt++] )
01036     {               
01037         /*******************************************************************************/
01038         case NFCIP_CMD_DEP_REQ:
01039             break;                                /* Continue to normal DEP processing */
01040     
01041         /*******************************************************************************/
01042         case NFCIP_CMD_DSL_REQ:
01043             
01044             nfcipLogI( " NFCIP(T) rx DSL \r\n" );
01045             
01046             /* Digital 1.0  14.9.1.2 If DID is used and incorrect ignore it */
01047             /* [Digital 1.0, 16.9.1.2]: If DID == 0, Target SHALL ignore DSL_REQ with DID */
01048             if ( ((gNfcip.cfg.did != RFAL_NFCDEP_DID_NO) && ((nfcDepLen != RFAL_NFCDEP_DSL_RLS_LEN_DID) || (gNfcip.rxBuf[rxMsgIt++] != gNfcip.cfg.did)) )
01049                ||((gNfcip.cfg.did == RFAL_NFCDEP_DID_NO) && (nfcDepLen != RFAL_NFCDEP_DSL_RLS_LEN_NO_DID))
01050                )
01051             {                
01052                 nfcipLogI( " NFCIP(T) DSL wrong DID, ignoring \r\n" );
01053                 return ERR_BUSY;
01054             }
01055             
01056             nfcipTx( NFCIP_CMD_DSL_RES, resBuf, 0, 0, 0, NFCIP_NO_FWT, mspiChannel, mST25, gpio_cs, IRQ, fieldLED_01, fieldLED_02, fieldLED_03, fieldLED_04, fieldLED_05, fieldLED_06 ) ;
01057             
01058             gNfcip.state = NFCIP_ST_TARG_DEP_SLEEP;
01059             return ERR_SLEEP_REQ;
01060             
01061         /*******************************************************************************/
01062         case NFCIP_CMD_RLS_REQ:
01063             
01064             nfcipLogI( " NFCIP(T) rx RLS \r\n" );
01065             
01066             /* Digital 1.0  14.10.1.2 If DID is used and incorrect ignore it */
01067             /* [Digital 1.0, 16.10.2.2]: If DID == 0, Target SHALL ignore DSL_REQ with DID */
01068             if ( ( (gNfcip.cfg.did != RFAL_NFCDEP_DID_NO) && ((nfcDepLen != RFAL_NFCDEP_DSL_RLS_LEN_DID) || (gNfcip.rxBuf[rxMsgIt++] != gNfcip.cfg.did)) )
01069                ||( (gNfcip.cfg.did == RFAL_NFCDEP_DID_NO) && (nfcDepLen > RFAL_NFCDEP_DSL_RLS_LEN_NO_DID))
01070                )
01071             {                
01072                 nfcipLogI( " NFCIP(T) RLS wrong DID, ignoring \r\n" );
01073                 return ERR_BUSY;
01074             }
01075                         
01076             nfcipTx( NFCIP_CMD_RLS_RES, resBuf, 0, 0, 0, NFCIP_NO_FWT, mspiChannel, mST25, gpio_cs, IRQ, fieldLED_01, fieldLED_02, fieldLED_03, fieldLED_04, fieldLED_05, fieldLED_06 ) ;
01077             
01078             gNfcip.state = NFCIP_ST_TARG_DEP_IDLE;
01079             return ERR_RELEASE_REQ;
01080             
01081         /*******************************************************************************/
01082         /*case NFCIP_CMD_PSL_REQ:              PSL must be handled in Activation only */
01083         /*case NFCIP_CMD_WUP_REQ:              WUP not in NFC Forum Digital 1.0       */
01084         default:
01085             
01086             /* Don't go to NFCIP_ST_TARG_DEP_IDLE state as it needs to ignore this    *
01087              * invalid frame, and keep waiting for more frames                        */
01088             
01089             nfcDepReEnableRx( gNfcip.rxBuf, gNfcip.rxBufLen, gNfcip.rxRcvdLen );
01090             return ERR_BUSY; /* ERR_PROTO - Ignore bad frame */
01091     }
01092     
01093     /*******************************************************************************/
01094     
01095     rxPFB = gNfcip.rxBuf[rxMsgIt++];                    /* Store rcvd PFB  */
01096     
01097     /*******************************************************************************/
01098     /* Check for valid PFB type                                                    */
01099     if( !(nfcip_PFBisSPDU( rxPFB ) || nfcip_PFBisRPDU( rxPFB ) || nfcip_PFBisIPDU( rxPFB )) )
01100     {
01101         nfcDepReEnableRx( gNfcip.rxBuf, gNfcip.rxBufLen, gNfcip.rxRcvdLen );
01102         return ERR_BUSY; /* ERR_PROTO - Ignore invalid PFB  */
01103     }
01104     
01105     /*******************************************************************************/   
01106     if( gNfcip.cfg.did != RFAL_NFCDEP_DID_NO ) 
01107     {
01108         if( !nfcip_PFBhasDID( rxPFB ) || gNfcip.rxBuf[rxMsgIt++] != gNfcip.cfg.did )
01109         {
01110             nfcDepReEnableRx( gNfcip.rxBuf, gNfcip.rxBufLen, gNfcip.rxRcvdLen );
01111             return ERR_BUSY; /* ERR_PROTO - Ignore bad/missing DID  */
01112         }
01113         optHdrLen++;                                    /* Inc header optional field cnt*/
01114     }
01115     else if( nfcip_PFBhasDID( rxPFB ) )                 /* DID not expected but rcv     */
01116     {
01117         nfcDepReEnableRx( gNfcip.rxBuf, gNfcip.rxBufLen, gNfcip.rxRcvdLen );
01118         return ERR_BUSY; /* ERR_PROTO - Ignore unexpected DID  */
01119     }
01120                                   
01121         
01122     /*******************************************************************************/
01123     if( gNfcip.cfg.nad != RFAL_NFCDEP_NAD_NO ) 
01124     {
01125         if( !nfcip_PFBhasNAD( rxPFB ) || gNfcip.rxBuf[rxMsgIt++] != gNfcip.cfg.nad )
01126         {
01127             nfcDepReEnableRx( gNfcip.rxBuf, gNfcip.rxBufLen, gNfcip.rxRcvdLen );
01128             return ERR_BUSY; /* ERR_PROTO - Ignore bad/missing NAD  */
01129         }
01130         optHdrLen++;                                    /* Inc header optional field cnt*/
01131     }
01132     else if( nfcip_PFBhasNAD( rxPFB ) )                 /* NAD not expected but rcv */
01133     {
01134         nfcDepReEnableRx( gNfcip.rxBuf, gNfcip.rxBufLen, gNfcip.rxRcvdLen );
01135         return ERR_BUSY; /* ERR_PROTO - Ignore unexpected NAD  */
01136     }
01137     
01138        
01139     /*******************************************************************************/
01140     /* Process R-PDU                                                               */
01141     /*******************************************************************************/
01142     if( nfcip_PFBisRPDU( rxPFB ) )
01143     {
01144         nfcipLogD( " NFCIP(T) Rcvd R-PDU  \r\n" );
01145         /*******************************************************************************/
01146         /* R ACK                                                                       */
01147         /*******************************************************************************/
01148         if( nfcip_PFBisRACK( rxPFB ) )
01149         {            
01150             nfcipLogI( " NFCIP(T) Rcvd ACK  \r\n" );
01151             if( gNfcip.pni == nfcip_PBF_PNI( rxPFB ) )
01152             {                
01153                 /* R-ACK while not performing chaining -> Protocol error */
01154                 if( !gNfcip.isTxChaining )
01155                 {
01156                     nfcDepReEnableRx( gNfcip.rxBuf, gNfcip.rxBufLen, gNfcip.rxRcvdLen );
01157                     return ERR_BUSY; /* ERR_PROTO - Ignore unexpected ACK  */
01158                 }
01159                 
01160                 /* This block has been transmitted and acknowledged, perform RTOX until next data is provided  */
01161                 
01162                 /* Digital 1.1  16.12.4.7 - If ACK rcvd continue with chaining or an RTOX */
01163                 nfcipTimerStart( gNfcip.RTOXTimer, nfcipRTOXAdjust( nfcipConv1FcToMs( rfalNfcDepWT2RWT( gNfcip.cfg.to ) )) );
01164                 gNfcip.state = NFCIP_ST_TARG_DEP_RTOX;
01165                 
01166                 return ERR_NONE;                            /* This block has been transmitted */
01167             }
01168             
01169             /* Digital 1.0 14.12.3.4 - If last send was ATN and rx PNI is minus 1 */
01170             else if( nfcip_PFBisSATN( gNfcip.lastPFB ) &&  (nfcip_PNIDec(gNfcip.pni) == nfcip_PBF_PNI( rxPFB )) )    
01171             {   
01172                 nfcipLogI( " NFCIP(T) wrong PNI, last was ATN reTx  \r\n" );
01173                 /* Spec says to leave current PNI as is, but will be Inc after Tx, remaining the same */
01174                 gNfcip.pni = nfcip_PNIDec( gNfcip.pni );
01175                 
01176                 gNfcip.state = NFCIP_ST_TARG_DEP_TX;
01177                 return ERR_BUSY;
01178             }
01179         }
01180         /*******************************************************************************/
01181         /* R NACK                                                                      */
01182         /*******************************************************************************/
01183         /* ISO 18092 12.6.1.3.3 When rcv NACK if PNI = prev PNI sent ->  reTx          */
01184         else if( nfcip_PFBisRNACK( rxPFB ) && (nfcip_PNIDec(gNfcip.pni) == nfcip_PBF_PNI( rxPFB ) ) )
01185         {
01186             nfcipLogI( " NFCIP(T) Rcvd NACK  \r\n" );
01187             
01188             gNfcip.pni = nfcip_PNIDec( gNfcip.pni );   /* Dec so that has the prev PNI */
01189             
01190             gNfcip.state = NFCIP_ST_TARG_DEP_TX;
01191             return ERR_BUSY;
01192         }
01193         
01194         nfcipLogI( " NFCIP(T) Unexpected R-PDU \r\n" );
01195         
01196         nfcDepReEnableRx( gNfcip.rxBuf, gNfcip.rxBufLen, gNfcip.rxRcvdLen );
01197         return ERR_BUSY; /* ERR_PROTO - Ignore unexpected R-PDU  */
01198     }
01199     
01200     /*******************************************************************************/
01201     /* Process S-PDU                                                               */
01202     /*******************************************************************************/
01203     if( nfcip_PFBisSPDU( rxPFB ) )
01204     {        
01205         nfcipLogD( " NFCIP(T) Rcvd S-PDU  \r\n" );
01206         
01207         /*******************************************************************************/
01208         /* S ATN                                                                       */
01209         /*******************************************************************************/
01210         /* ISO 18092 12.6.3 Attention                                                  */
01211         if( nfcip_PFBisSATN( rxPFB ) )                         /*    If is a S-ATN     */
01212         {            
01213             nfcipLogI( " NFCIP(T) Rcvd ATN  curPNI: %d \r\n", gNfcip.pni );
01214             EXIT_ON_ERR( ret, nfcipDEPControlMsg( nfcip_PFBSPDU_ATN(), 0, mspiChannel, mST25, gpio_cs, IRQ, fieldLED_01, fieldLED_02, fieldLED_03, fieldLED_04, fieldLED_05, fieldLED_06 )  );
01215             return ERR_BUSY;
01216         }
01217         
01218         /*******************************************************************************/
01219         /* S TO                                                                        */
01220         /*******************************************************************************/
01221         else if( nfcip_PFBisSTO( rxPFB ) )                     /* If is a S-TO (RTOX)  */
01222         {
01223             if( nfcip_PFBisSTO( gNfcip.lastPFBnATN ) )
01224             {
01225                 nfcipLogI( " NFCIP(T) Rcvd TO  \r\n" );
01226                 
01227                 /* Digital 1.1  16.8.4.6  RTOX value in RES different that in REQ -> Protocol Error */
01228                 if( gNfcip.lastRTOX != gNfcip.rxBuf[rxMsgIt++] )
01229                 {
01230                     nfcipLogI( " NFCIP(T) Mismatched RTOX value \r\n" );
01231                     
01232                     nfcDepReEnableRx( gNfcip.rxBuf, gNfcip.rxBufLen, gNfcip.rxRcvdLen );
01233                     return ERR_BUSY; /* ERR_PROTO - Ignore unexpected RTOX value  */
01234                 }
01235                 
01236                 /* Clear waiting for RTOX Ack Flag */
01237                 gNfcip.isWait4RTOX = false;
01238                 
01239                 /* Check if a Tx is already pending */
01240                 if( gNfcip.isTxPending )
01241                 {
01242                     nfcipLogW( " NFCIP(T) Tx pending, go immediately to TX \r\n" );
01243                     
01244                     gNfcip.state = NFCIP_ST_TARG_DEP_TX;
01245                     return ERR_BUSY;
01246                 }
01247                 
01248                 /* Start RTOX timer and change to check state  */
01249                 nfcipTimerStart( gNfcip.RTOXTimer, nfcipRTOXAdjust( nfcipConv1FcToMs( gNfcip.lastRTOX * rfalNfcDepWT2RWT(gNfcip.cfg.to ) ) ) );
01250                 gNfcip.state = NFCIP_ST_TARG_DEP_RTOX;
01251                 
01252                 return ERR_BUSY;
01253             }
01254         }
01255         /* Unexpected S-PDU */
01256         nfcipLogI( " NFCIP(T) Unexpected S-PDU \r\n" );
01257         
01258         nfcDepReEnableRx( gNfcip.rxBuf, gNfcip.rxBufLen, gNfcip.rxRcvdLen );
01259         return ERR_BUSY; /* ERR_PROTO - Ignore unexpected S-PDU  */
01260     }
01261     
01262     /*******************************************************************************/
01263     /* Process I-PDU                                                               */
01264     /*******************************************************************************/
01265     if( nfcip_PFBisIPDU( rxPFB ) )
01266     {
01267         if( gNfcip.pni != nfcip_PBF_PNI( rxPFB ) )
01268         {
01269             nfcipLogI( " NFCIP(T) Rcvd IPDU wrong PNI     curPNI: %d rxPNI: %d \r\n", gNfcip.pni, nfcip_PBF_PNI( rxPFB ) );
01270             
01271             /* Digital 1.1 16.12.3.4 - If last send was ATN and rx PNI is minus 1 */
01272             if( nfcip_PFBisSATN(gNfcip.lastPFB ) &&  (nfcip_PNIDec(gNfcip.pni) == nfcip_PBF_PNI( rxPFB )) ) 
01273             {
01274                 /* Spec says to leave current PNI as is, but will be Inc after Data Tx, remaining the same */
01275                 gNfcip.pni = nfcip_PNIDec(gNfcip.pni);
01276                 
01277                 if( nfcip_PFBisIMI( rxPFB ) )
01278                 {
01279                     nfcipLogI( " NFCIP(T) PNI = prevPNI && ATN before && chaining -> send ACK  \r\n" );
01280                     EXIT_ON_ERR( ret, nfcipDEPControlMsg( nfcip_PFBRPDU_ACK( gNfcip.pni ), gNfcip.rxBuf[rxMsgIt++], mspiChannel, mST25, gpio_cs, IRQ, fieldLED_01, fieldLED_02, fieldLED_03, fieldLED_04, fieldLED_05, fieldLED_06 )  );
01281                     
01282                     /* Digital 1.1 16.12.3.4 (...) leave the current PNI unchanged afterwards */
01283                     gNfcip.pni = nfcip_PNIInc( gNfcip.pni );
01284                 }
01285                 else
01286                 {
01287                     nfcipLogI( " NFCIP(T) PNI = prevPNI && ATN before -> reTx last I-PDU  \r\n" );
01288                     gNfcip.state = NFCIP_ST_TARG_DEP_TX;
01289                 }
01290                 
01291                 return ERR_BUSY;
01292             }
01293                         
01294             nfcDepReEnableRx( gNfcip.rxBuf, gNfcip.rxBufLen, gNfcip.rxRcvdLen );
01295             return ERR_BUSY; /* ERR_PROTO - Ignore bad PNI value  */
01296         }
01297         
01298         nfcipLogD( " NFCIP(T) Rcvd IPDU OK PNI: %d  \r\n", gNfcip.pni );
01299         
01300         /*******************************************************************************/
01301         /* Successful data exchange                                                    */
01302         /*******************************************************************************/
01303         *outActRxLen  = (nfcDepLen - RFAL_NFCDEP_DEP_HEADER - optHdrLen);
01304         
01305         nfcipClearCounters();
01306 
01307         if( (gNfcip.rxBuf + gNfcip.rxBufPaylPos) != (gNfcip.rxBuf + RFAL_NFCDEP_DEP_HEADER + optHdrLen) )
01308         {
01309             ST_MEMMOVE( (gNfcip.rxBuf + gNfcip.rxBufPaylPos), (gNfcip.rxBuf + RFAL_NFCDEP_DEP_HEADER + optHdrLen), *outActRxLen );
01310         }
01311         
01312         
01313         /*******************************************************************************/
01314         /* Check if Initiator is indicating chaining MI                                */
01315         /*******************************************************************************/
01316         if( nfcip_PFBisIMI( rxPFB ) )
01317         {
01318             gNfcip.isRxChaining = true;
01319             *outIsChaining      = true;
01320             
01321             nfcipLogD( " NFCIP(T) Rcvd IPDU OK w MI -> ACK \r\n" );
01322             EXIT_ON_ERR( ret, nfcipDEPControlMsg( nfcip_PFBRPDU_ACK( gNfcip.pni ), gNfcip.rxBuf[rxMsgIt++], mspiChannel, mST25, gpio_cs, IRQ, fieldLED_01, fieldLED_02, fieldLED_03, fieldLED_04, fieldLED_05, fieldLED_06 )  );
01323             
01324             gNfcip.pni = nfcip_PNIInc( gNfcip.pni );
01325             
01326             return ERR_AGAIN;  /* Send Again signalling to run again, but some chaining data has arrived*/
01327         }
01328         else
01329         {
01330             if(gNfcip.isRxChaining)
01331             {
01332                 nfcipLogI( " NFCIP(T) Rcvd last IPDU chaining finished \r\n" );
01333             }
01334             
01335             /*******************************************************************************/
01336             /* Reception done, send to DH and start RTOX timer                             */
01337             /*******************************************************************************/
01338             nfcipTimerStart( gNfcip.RTOXTimer, nfcipRTOXAdjust( nfcipConv1FcToMs( rfalNfcDepWT2RWT( gNfcip.cfg.to ) )) );
01339             gNfcip.state = NFCIP_ST_TARG_DEP_RTOX;
01340             
01341             gNfcip.isRxChaining = false;
01342             return ERR_NONE;                            /* Data exchange done */
01343         }
01344     }
01345     return ret;
01346 }
01347 
01348 
01349 /*******************************************************************************/
01350 static ReturnCode nfcipTx( rfalNfcDepCmd cmd, uint8_t* txBuf, uint8_t *paylBuf, uint16_t paylLen, uint8_t pfb, uint32_t fwt, SPI* mspiChannel, ST25R3911* mST25, DigitalOut* gpio_cs, InterruptIn* IRQ, DigitalOut* fieldLED_01, DigitalOut* fieldLED_02, DigitalOut* fieldLED_03, DigitalOut* fieldLED_04, DigitalOut* fieldLED_05, DigitalOut* fieldLED_06 )
01351 {
01352     uint16_t txBufIt;
01353     uint8_t *txBlock;
01354     
01355     if( txBuf == NULL )
01356     {
01357         return ERR_PARAM;
01358     }
01359     
01360     
01361     if( (paylLen == 0) || (paylBuf == NULL) )
01362     {
01363         paylBuf = (txBuf + RFAL_NFCDEP_DEPREQ_HEADER_LEN);  /* If not a DEP (no Data) ensure enough space for header */
01364     }
01365     
01366     
01367     txBufIt  = 0;
01368     txBlock  = paylBuf;                                  /* Point to beginning of the Data, and go backwards     */    
01369         
01370     
01371     gNfcip.lastCmd     = cmd;                            /* store last cmd sent    */
01372     gNfcip.lastPFB     = NFCIP_PFB_INVALID;              /* reset last pfb sent    */
01373     
01374     /*******************************************************************************/
01375     /* Compute outgoing NFCIP message                                              */
01376     /*******************************************************************************/
01377     switch( cmd )
01378     {
01379         /*******************************************************************************/
01380         case NFCIP_CMD_ATR_RES:
01381         case NFCIP_CMD_ATR_REQ:
01382             
01383             rfalNfcDepSetNFCID( paylBuf, gNfcip.cfg.nfcid, gNfcip.cfg.nfcidLen );        /* NFCID */
01384             txBufIt += RFAL_NFCDEP_NFCID3_LEN;
01385             
01386             *(paylBuf + txBufIt++) = gNfcip.cfg.did;                                     /* DID   */
01387             *(paylBuf + txBufIt++) = gNfcip.cfg.bs;                                      /* BS    */
01388             *(paylBuf + txBufIt++) = gNfcip.cfg.br;                                      /* BR    */
01389             
01390             if( cmd == NFCIP_CMD_ATR_RES )
01391             {
01392                 *(paylBuf + txBufIt++) = gNfcip.cfg.to;                                  /* ATR_RES[ TO ] */
01393             }
01394                                     
01395             if( gNfcip.cfg.gbLen > 0)
01396             {
01397                 *(paylBuf + txBufIt++) = nfcip_PPwGB( gNfcip.cfg.lr );                   /* PP signalling GB  */
01398                 ST_MEMCPY( (paylBuf + txBufIt), gNfcip.cfg.gb, gNfcip.cfg.gbLen );      /* set General Bytes */
01399                 txBufIt += gNfcip.cfg.gbLen;
01400             }
01401             else
01402             {
01403                 *(paylBuf + txBufIt++) = rfalNfcDepLR2PP( gNfcip.cfg.lr );               /* PP without GB        */
01404             }
01405             
01406             if( (txBufIt + RFAL_NFCDEP_CMDTYPE_LEN + RFAL_NFCDEP_CMD_LEN) > RFAL_NFCDEP_ATRREQ_MAX_LEN )   /* Check max ATR length (ATR_REQ = ATR_RES)*/
01407             {
01408                 return ERR_PARAM;
01409             }
01410             
01411             break;
01412             
01413         /*******************************************************************************/
01414         case NFCIP_CMD_WUP_REQ:                               /* ISO 18092 - 12.5.2.1 */
01415             
01416             rfalNfcDepSetNFCID( (paylBuf), gNfcip.cfg.nfcid, gNfcip.cfg.nfcidLen );      /* NFCID */
01417             txBufIt += RFAL_NFCDEP_NFCID3_LEN;
01418             
01419             *(--txBlock) = gNfcip.cfg.did;                                               /* DID   */
01420             break;
01421                     
01422         /*******************************************************************************/
01423         case NFCIP_CMD_WUP_RES:                               /* ISO 18092 - 12.5.2.2 */
01424         case NFCIP_CMD_PSL_REQ:
01425         case NFCIP_CMD_PSL_RES:
01426             
01427             *(--txBlock) = gNfcip.cfg.did;                                               /* DID   */
01428             break;
01429             
01430         /*******************************************************************************/
01431         case NFCIP_CMD_RLS_REQ:
01432         case NFCIP_CMD_RLS_RES:
01433         case NFCIP_CMD_DSL_REQ:
01434         case NFCIP_CMD_DSL_RES:
01435             
01436             /* Digital 1.0 - 14.8.1.1 & 14.9.1.1 & 14.10.1.1 Only add DID if not 0 */
01437             if( gNfcip.cfg.did != RFAL_NFCDEP_DID_NO )
01438             {
01439                 *(--txBlock) = gNfcip.cfg.did;                                           /* DID   */
01440             }
01441             break;
01442             
01443         /*******************************************************************************/
01444         case NFCIP_CMD_DEP_REQ:
01445         case NFCIP_CMD_DEP_RES:
01446             
01447             /* Compute optional PFB bits */
01448             if(gNfcip.cfg.did != RFAL_NFCDEP_DID_NO)                 pfb |= NFCIP_PFB_DID_BIT;
01449             if(gNfcip.cfg.nad != RFAL_NFCDEP_NAD_NO)                 pfb |= NFCIP_PFB_NAD_BIT;
01450             if((gNfcip.isTxChaining) && (nfcip_PFBisIPDU(pfb)) )     pfb |= NFCIP_PFB_MI_BIT;
01451             
01452             /* Store PFB for future handling */
01453             gNfcip.lastPFB       = pfb;                                                  /* store PFB sent */
01454             
01455             if( !nfcip_PFBisSATN(pfb) )
01456             {
01457                 gNfcip.lastPFBnATN   = pfb;                                              /* store last PFB different then ATN */
01458             }
01459             
01460             
01461             /* Add NAD if it is to be supported */
01462             if( gNfcip.cfg.nad != RFAL_NFCDEP_NAD_NO )      
01463             {
01464                 *(--txBlock) = gNfcip.cfg.nad;                                           /* NAD   */
01465             }
01466             
01467             /* Digital 1.0 - 14.8.1.1 & 14.8.1.1 Only add DID if not 0 */
01468             if( gNfcip.cfg.did != RFAL_NFCDEP_DID_NO )
01469             {
01470                 *(--txBlock) = gNfcip.cfg.did;                                           /* DID   */
01471             }
01472             
01473             *(--txBlock) = pfb;                                                          /* PFB */
01474                         
01475             
01476             /* NCI 1.0 - Check if Empty frames are allowed */
01477             if( (paylLen == 0) && nfcipIsEmptyDEPDisabled(gNfcip.cfg.oper) && nfcip_PFBisIPDU(pfb) )
01478             {
01479                 return ERR_PARAM;
01480             }
01481             break;
01482 
01483         /*******************************************************************************/
01484         default:
01485             return ERR_PARAM;
01486     }
01487     
01488     /*******************************************************************************/
01489     /* Prepend Header                                                              */
01490     /*******************************************************************************/    
01491     *(--txBlock) = (uint8_t)cmd;                                                        /* CMD     */
01492     *(--txBlock) = ( nfcipCmdIsReq(cmd) ? NFCIP_REQ : NFCIP_RES );                      /* CMDType */
01493         
01494     
01495     txBufIt += paylLen + (paylBuf - txBlock);                                           /* Calculate overall buffer size */
01496     
01497     
01498     if( txBufIt > gNfcip.fsc )                                                          /* Check if msg length violates the maximum payload size FSC */
01499     {
01500         return ERR_NOTSUPP;
01501     }
01502         
01503     /*******************************************************************************/
01504     return nfcipDataTx( txBlock, txBufIt, fwt, mspiChannel, mST25, gpio_cs, IRQ, fieldLED_01, fieldLED_02, fieldLED_03, fieldLED_04, fieldLED_05, fieldLED_06 ) ;
01505 }
01506 
01507 /*
01508  ******************************************************************************
01509  * GLOBAL FUNCTIONS
01510  ******************************************************************************
01511  */
01512 
01513 /*******************************************************************************/
01514 static void nfcipConfig( rfalNfcDepConfigs  cfg )
01515 {    
01516     ST_MEMCPY(&gNfcip.cfg, &cfg, sizeof(rfalNfcDepConfigs ));      /* Copy given config to local       */
01517     
01518     gNfcip.cfg.to   = MIN( RFAL_NFCDEP_WT_TRG_MAX, gNfcip.cfg.to); /* Ensure proper WT value           */
01519     gNfcip.cfg.did  = nfcip_DIDMax( gNfcip.cfg.did );              /* Ensure proper DID value          */
01520     gNfcip.fsc      = rfalNfcDepLR2FS( gNfcip.cfg.lr );            /* Calculate FSC based on given LR  */
01521     
01522     gNfcip.state = ( ( gNfcip.cfg.role ==  RFAL_NFCDEP_ROLE_TARGET ) ? NFCIP_ST_TARG_WAIT_ATR : NFCIP_ST_INIT_IDLE );
01523 }
01524 
01525 
01526 /*******************************************************************************/
01527 static ReturnCode nfcipRun( uint16_t *outActRxLen, bool *outIsChaining, SPI* mspiChannel, ST25R3911* mST25, DigitalOut* gpio_cs, InterruptIn* IRQ, DigitalOut* fieldLED_01, DigitalOut* fieldLED_02, DigitalOut* fieldLED_03, DigitalOut* fieldLED_04, DigitalOut* fieldLED_05, DigitalOut* fieldLED_06 )
01528 {
01529     ReturnCode ret;
01530     
01531     ret = ERR_SYNTAX;
01532     
01533     nfcipLogD( " NFCIP Run() state: %d \r\n", gNfcip.state );
01534     
01535     switch( gNfcip.state )
01536     {
01537         /*******************************************************************************/
01538         case NFCIP_ST_IDLE:
01539         case NFCIP_ST_INIT_DEP_IDLE:
01540         case NFCIP_ST_TARG_DEP_IDLE:
01541         case NFCIP_ST_TARG_DEP_SLEEP:
01542             return ERR_NONE;
01543             
01544         /*******************************************************************************/
01545         case NFCIP_ST_INIT_DEP_TX:
01546             
01547             nfcipLogD( " NFCIP(I) Tx PNI: %d txLen: %d \r\n", gNfcip.pni, gNfcip.txBufLen );
01548             ret = nfcipTx( NFCIP_CMD_DEP_REQ, gNfcip.txBuf, (gNfcip.txBuf + gNfcip.txBufPaylPos), gNfcip.txBufLen, nfcip_PFBIPDU( gNfcip.pni ), (gNfcip.cfg.fwt + gNfcip.cfg.dFwt), mspiChannel, mST25, gpio_cs, IRQ, fieldLED_01, fieldLED_02, fieldLED_03, fieldLED_04, fieldLED_05, fieldLED_06 ) ;
01549                         
01550             switch( ERR_NO_MASK(ret) )
01551             {
01552                 case ERR_PARAM:
01553                 default:
01554                   gNfcip.state = NFCIP_ST_INIT_DEP_IDLE;
01555                   return ret;
01556                   
01557                 case ERR_NONE:
01558                   gNfcip.state = NFCIP_ST_INIT_DEP_RX;
01559 
01560             }
01561             /* fall through */
01562             
01563         /*******************************************************************************/
01564         case NFCIP_ST_INIT_DEP_RX:
01565 
01566             ret = nfcipDataRx();
01567             
01568             if( ret != ERR_NO_MASK(ERR_BUSY) )
01569             {
01570                 ret = nfcipInitiatorHandleDEP( ret, *gNfcip.rxRcvdLen, outActRxLen, outIsChaining, mspiChannel, mST25, gpio_cs, IRQ, fieldLED_01, fieldLED_02, fieldLED_03, fieldLED_04, fieldLED_05, fieldLED_06 ) ;
01571             }
01572             
01573             break;
01574             
01575         /*******************************************************************************/    
01576         case NFCIP_ST_TARG_DEP_RTOX:
01577             
01578             if( !nfcipTimerisExpired( gNfcip.RTOXTimer ) )                     /* Do nothing until RTOX timer has expired */
01579             {
01580                 return ERR_BUSY;
01581             }
01582             
01583             /* If we cannot send a RTOX raise a Timeout error so that we do not   
01584              * hold the field On forever in AP2P                                  */
01585             if( nfcipIsRTOXReqDisabled(gNfcip.cfg.oper) )
01586             {
01587                 /* We should reEnable Rx, and measure time between our field Off to 
01588                  * either report link loss or recover               #287          */
01589                 nfcipLogI( " NFCIP(T) RTOX not sent due to config, NOT reenabling Rx \r\n" );
01590                 return ERR_TIMEOUT;
01591             } 
01592 
01593             if( gNfcip.cntRTOXRetrys++ > NFCIP_MAX_RTOX_RETRYS )                   /* Check maximum consecutive RTOX requests */
01594             {
01595                 return ERR_PROTO;
01596             }
01597             
01598             nfcipLogI( " NFCIP(T) RTOX sent \r\n" );
01599             
01600             gNfcip.lastRTOX = nfcip_RTOXTargMax(gNfcip.cfg.to);               /* Calculate requested RTOX value, and send it   */                        
01601             EXIT_ON_ERR( ret, nfcipDEPControlMsg( nfcip_PFBSPDU_TO(), gNfcip.lastRTOX, mspiChannel, mST25, gpio_cs, IRQ, fieldLED_01, fieldLED_02, fieldLED_03, fieldLED_04, fieldLED_05, fieldLED_06 )  );
01602             
01603             /* Set waiting for RTOX Ack Flag */
01604             gNfcip.isWait4RTOX = true;
01605             
01606             gNfcip.state = NFCIP_ST_TARG_DEP_RX;                              /* Go back to Rx to process RTOX ack        */
01607             return ERR_BUSY;
01608             
01609         /*******************************************************************************/
01610         case NFCIP_ST_TARG_DEP_TX:
01611             
01612             nfcipLogD( " NFCIP(T) Tx PNI: %d txLen: %d \r\n", gNfcip.pni, gNfcip.txBufLen );
01613             ret = nfcipTx( NFCIP_CMD_DEP_RES, gNfcip.txBuf, (gNfcip.txBuf + gNfcip.txBufPaylPos), gNfcip.txBufLen, nfcip_PFBIPDU( gNfcip.pni ), NFCIP_NO_FWT, mspiChannel, mST25, gpio_cs, IRQ, fieldLED_01, fieldLED_02, fieldLED_03, fieldLED_04, fieldLED_05, fieldLED_06 ) ;
01614             
01615             /* Clear flags */
01616             gNfcip.isTxPending = false;
01617             gNfcip.isWait4RTOX = false;
01618             
01619             /* Digital 1.0 14.12.3.4 Increment the current PNI after Tx */
01620             gNfcip.pni = nfcip_PNIInc( gNfcip.pni );
01621             
01622             switch( ERR_NO_MASK(ret) )
01623             {
01624                 case ERR_PARAM:
01625                 default:
01626                   gNfcip.state = NFCIP_ST_TARG_DEP_IDLE;                      /* Upon Tx error, goto IDLE state         */
01627                   return ret;
01628                   
01629               case ERR_NONE:
01630                   gNfcip.state = NFCIP_ST_TARG_DEP_RX;                       /* All OK, goto Rx state                   */
01631             }
01632             /* fall through */
01633             
01634         /*******************************************************************************/
01635         case NFCIP_ST_TARG_DEP_RX:
01636             
01637             if( gNfcip.isReqPending )    /* if already has Data should be from a DEP from nfcipTargetHandleActivation()  */
01638             {
01639                 nfcipLogD( " NFCIP(T) Skipping Rx Using DEP from Activation \r\n" );
01640                 
01641                 gNfcip.isReqPending = false;
01642                 ret = ERR_NONE;
01643             }
01644             else
01645             {
01646                 ret = nfcipDataRx();
01647             }
01648             
01649             if( ret != ERR_NO_MASK(ERR_BUSY) )
01650             {
01651                 ret = nfcipTargetHandleRX( ret, outActRxLen, outIsChaining, mspiChannel, mST25, gpio_cs, IRQ, fieldLED_01, fieldLED_02, fieldLED_03, fieldLED_04, fieldLED_05, fieldLED_06 ) ;
01652             }
01653             
01654             break;
01655             
01656         /*******************************************************************************/
01657         default:
01658             break;
01659     }
01660     return ret;
01661 }
01662 
01663 
01664 /*******************************************************************************/
01665 void rfalNfcDepSetDeactivatingCallback( rfalNfcDepDeactCallback  pFunc )
01666 {
01667     gNfcip.isDeactivating = pFunc;
01668 }
01669 
01670 
01671 /*******************************************************************************/
01672 void rfalNfcDepInitialize( void )
01673 {
01674     nfcipLogD( " NFCIP Ini() \r\n" );
01675     
01676     gNfcip.state          = NFCIP_ST_IDLE;
01677     gNfcip.isDeactivating = NULL;
01678     
01679     gNfcip.isTxPending    = false;
01680     gNfcip.isWait4RTOX    = false;
01681     gNfcip.isReqPending   = false;
01682     
01683             
01684     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);
01685     
01686     gNfcip.cfg.did   = RFAL_NFCDEP_DID_NO;
01687     gNfcip.cfg.nad   = RFAL_NFCDEP_NAD_NO;
01688     
01689     gNfcip.cfg.br    = RFAL_NFCDEP_Bx_NO_HIGH_BR ;
01690     gNfcip.cfg.bs    = RFAL_NFCDEP_Bx_NO_HIGH_BR ;
01691     
01692     gNfcip.cfg.lr    = RFAL_NFCDEP_LR_254 ;
01693     gNfcip.fsc       = rfalNfcDepLR2FS( gNfcip.cfg.lr );
01694     
01695     gNfcip.cfg.gbLen = 0;    
01696     
01697     gNfcip.cfg.fwt   = RFAL_NFCDEP_MAX_FWT;
01698     gNfcip.cfg.dFwt  = RFAL_NFCDEP_MAX_FWT;
01699     
01700     gNfcip.pni       = 0;    
01701     gNfcip.RTOXTimer = 0;
01702 
01703     nfcipClearCounters();
01704 }
01705 
01706 
01707 /*******************************************************************************/
01708 static void nfcipSetDEPParams( rfalNfcDepDEPParams *DEPParams )
01709 {
01710     nfcipLogD( " NFCIP SetDEP() txLen: %d \r\n", DEPParams->txBufLen );
01711     
01712     gNfcip.isTxChaining = DEPParams->txChaining;
01713     gNfcip.txBuf        = DEPParams->txBuf;
01714     gNfcip.rxBuf        = DEPParams->rxBuf;
01715     gNfcip.txBufLen     = DEPParams->txBufLen;
01716     gNfcip.rxBufLen     = DEPParams->rxBufLen;
01717     gNfcip.txBufPaylPos = DEPParams->txBufPaylPos;
01718     gNfcip.rxBufPaylPos = DEPParams->rxBufPaylPos;
01719     
01720     if( DEPParams->did != RFAL_NFCDEP_DID_KEEP )
01721     {
01722         gNfcip.cfg.did  = nfcip_DIDMax( DEPParams->did );
01723     }
01724     
01725     gNfcip.cfg.fwt      = DEPParams->fwt;
01726     gNfcip.cfg.dFwt     = DEPParams->dFwt;
01727     gNfcip.fsc          = DEPParams->fsc;
01728     
01729     
01730     
01731     if(gNfcip.cfg.role == RFAL_NFCDEP_ROLE_TARGET )
01732     {
01733         /* If there's any data to be sent go for Tx */
01734         if(DEPParams->txBufLen > 0)
01735         {
01736             /* Ensure that an RTOX Ack is not being expected at moment */
01737             if( !gNfcip.isWait4RTOX )
01738             {
01739                 gNfcip.state = NFCIP_ST_TARG_DEP_TX;
01740                 return;
01741             }
01742             else
01743             {
01744                 /* If RTOX Ack is expected, signal a pending Tx to be transmitted right after */
01745                 gNfcip.isTxPending = true;
01746                 nfcipLogW( " NFCIP(T) Waiting RTOX, queueing outgoing DEP Block \r\n" );                
01747             }
01748         }    
01749     
01750         /*Digital 1.0  14.12.4.1 In target mode the first PDU MUST be sent by the Initiator */
01751         gNfcip.state = NFCIP_ST_TARG_DEP_RX;
01752         return;
01753     }
01754 
01755     /* New data TxRx request clear previous error counters for consecutive TxRx without reseting communication/protocol layer*/
01756     nfcipClearCounters();
01757     
01758     gNfcip.state = NFCIP_ST_INIT_DEP_TX;
01759 }
01760 
01761 
01762 /*******************************************************************************/
01763 bool rfalNfcDepTargetRcvdATR( void )
01764 {
01765     return ( (gNfcip.cfg.role == RFAL_NFCDEP_ROLE_TARGET ) && nfcipIsTarget(gNfcip.state) && (gNfcip.state > NFCIP_ST_TARG_WAIT_ATR) );
01766 }
01767 
01768 
01769 /*******************************************************************************/
01770 bool rfalNfcDepIsAtrReq( uint8_t* buf, uint16_t bufLen, uint8_t* nfcid3 )
01771 {
01772     uint8_t msgIt;
01773     
01774     msgIt = 0;
01775     
01776     if ( (bufLen < RFAL_NFCDEP_ATRREQ_MIN_LEN) || (bufLen > RFAL_NFCDEP_ATRREQ_MAX_LEN) )
01777     {
01778         return false;
01779     }
01780     
01781     if ( *(buf + msgIt++) != NFCIP_REQ )
01782     {
01783         return false;
01784     }
01785     
01786     if( *(buf + msgIt++) != NFCIP_CMD_ATR_REQ )
01787     {
01788         return false;
01789     }
01790     
01791     /* Output NFID2 if requested */
01792     if( nfcid3 != NULL )
01793     {
01794         ST_MEMCPY( nfcid3, (uint8_t*)(buf + RFAL_NFCDEP_ATR_REQ_NFCID3_POS), RFAL_NFCDEP_NFCID3_LEN );
01795     }
01796      
01797     return true;
01798 }
01799 
01800 
01801 /*******************************************************************************/
01802 static ReturnCode nfcipTargetHandleActivation( rfalNfcDepDevice  *nfcDepDev, uint8_t *outBRS, SPI* mspiChannel, ST25R3911* mST25, DigitalOut* gpio_cs, InterruptIn* IRQ, DigitalOut* fieldLED_01, DigitalOut* fieldLED_02, DigitalOut* fieldLED_03, DigitalOut* fieldLED_04, DigitalOut* fieldLED_05, DigitalOut* fieldLED_06 )
01803 {
01804     ReturnCode ret;
01805     uint8_t    msgIt;
01806     uint8_t    txBuf[RFAL_NFCDEP_HEADER_PAD + NFCIP_PSLRES_LEN];
01807     
01808     /*******************************************************************************/
01809     /*  Check if we are in correct state                                           */
01810     /*******************************************************************************/
01811     if( gNfcip.state != NFCIP_ST_TARG_WAIT_ACTV )
01812     {
01813         return ERR_WRONG_STATE;
01814     }
01815     
01816     
01817     /*******************************************************************************/
01818     /*  Check required parameters                                                  */
01819     /*******************************************************************************/
01820     if( outBRS == NULL )
01821     {
01822         return ERR_PARAM;
01823     }
01824     
01825     /*******************************************************************************/
01826     /*  Wait and process incoming cmd (PSL / DEP)                                  */
01827     /*******************************************************************************/        
01828     ret = nfcipDataRx();
01829 
01830     if( ret != ERR_NONE )
01831     {
01832         return ret;
01833     }
01834     
01835     msgIt   = 0;
01836     *outBRS = RFAL_NFCDEP_BRS_MAINTAIN;                   /* set out BRS to be maintained */
01837  
01838     msgIt++;                                              /* Skip LEN byte                */
01839     
01840     if ( *(gNfcip.rxBuf + msgIt++) != NFCIP_REQ )
01841     {
01842         return ERR_PROTO;
01843     }
01844     
01845     if( *(gNfcip.rxBuf + msgIt) == NFCIP_CMD_PSL_REQ )
01846     {
01847         msgIt++;
01848         
01849         if( *(gNfcip.rxBuf + msgIt++) != gNfcip.cfg.did ) /* Checking DID                 */
01850         {
01851             return ERR_PROTO;
01852         }
01853         
01854         nfcipLogI( " NFCIP(T) PSL REQ rcvd \r\n" );
01855         
01856         *outBRS = *(gNfcip.rxBuf + msgIt++);              /* assign output BRS value      */
01857         
01858         /* Store FSL(LR) and update current config */
01859         gNfcip.cfg.lr = (*(gNfcip.rxBuf + msgIt++) & RFAL_NFCDEP_LR_VAL_MASK);
01860         gNfcip.fsc    = rfalNfcDepLR2FS( gNfcip.cfg.lr );
01861         
01862         /*******************************************************************************/
01863         /* Update NFC-DDE Device info */
01864         if( nfcDepDev != NULL )
01865         {
01866             /* Update Bitrate info */
01867             nfcDepDev->info .DSI  = (rfalBitRate )rfalNfcDepBRS2DSI( *outBRS );   /* DSI codes the bit rate from Initiator to Target */
01868             nfcDepDev->info .DRI  = (rfalBitRate )rfalNfcDepBRS2DRI( *outBRS );   /* DRI codes the bit rate from Target to Initiator */
01869             
01870             /* Update Length Reduction and Frame Size */
01871             nfcDepDev->info .LR  = gNfcip.cfg.lr;
01872             nfcDepDev->info .FS  = gNfcip.fsc;
01873             
01874             /* Update PPi byte */
01875             nfcDepDev->activation .Initiator .ATR_REQ .PPi  &= ~RFAL_NFCDEP_PP_LR_MASK;
01876             nfcDepDev->activation .Initiator .ATR_REQ .PPi  |= rfalNfcDepLR2PP( gNfcip.cfg.lr );
01877         }
01878         
01879         EXIT_ON_ERR( ret, nfcipTx( NFCIP_CMD_PSL_RES, txBuf, 0, 0, 0, NFCIP_NO_FWT, mspiChannel, mST25, gpio_cs, IRQ, fieldLED_01, fieldLED_02, fieldLED_03, fieldLED_04, fieldLED_05, fieldLED_06 )  );
01880     }
01881     else
01882     {
01883         if( *(gNfcip.rxBuf + msgIt) == NFCIP_CMD_DEP_REQ )
01884         {
01885             msgIt++;
01886                     
01887             /*******************************************************************************/
01888             /* Digital 1.0 14.12.3.1 PNI must be initialized to 0 */
01889             if( nfcip_PBF_PNI( *(gNfcip.rxBuf + msgIt) ) != 0 )
01890             {
01891                 return ERR_PROTO;
01892             }
01893             
01894             /*******************************************************************************/
01895             /* Digital 1.0 14.8.2.1  check if DID is expected and match -> Protocol Error  */
01896             if( nfcip_PFBhasDID( *(gNfcip.rxBuf + msgIt) ) ) 
01897             {
01898                 if( *(gNfcip.rxBuf + (++msgIt)) != gNfcip.cfg.did)
01899                 {
01900                     return ERR_PROTO;
01901                 }
01902             }
01903             else if( gNfcip.cfg.did != RFAL_NFCDEP_DID_NO )          /* DID expected but not rcv */
01904             {
01905                 return ERR_PROTO;
01906             }
01907         }
01908         
01909         /* Signal Request pending to be digested on normal Handling (DEP_REQ, DSL_REQ, RLS_REQ) */
01910         gNfcip.isReqPending = true;
01911     }
01912     
01913     gNfcip.state = NFCIP_ST_TARG_DEP_RX;
01914     return ERR_NONE;
01915 }
01916 
01917 
01918 /*******************************************************************************/
01919 ReturnCode rfalNfcDepATR( rfalNfcDepAtrParam * param, rfalNfcDepAtrRes  *atrRes,
01920         uint8_t* atrResLen, SPI* mspiChannel, ST25R3911* mST25,
01921         DigitalOut* gpio_cs, InterruptIn* IRQ, DigitalOut* fieldLED_01,
01922         DigitalOut* fieldLED_02, DigitalOut* fieldLED_03, DigitalOut* fieldLED_04,
01923         DigitalOut* fieldLED_05, DigitalOut* fieldLED_06 )
01924 {
01925     ReturnCode        ret;
01926     rfalNfcDepConfigs  cfg;
01927     uint16_t          rxLen;
01928     uint8_t           msgIt;
01929 
01930     
01931     
01932     if( (param == NULL) || (atrRes == NULL) || (atrResLen == NULL) )
01933     {
01934         return ERR_PARAM;
01935     }
01936     
01937     /*******************************************************************************/
01938     /* Configure NFC-DEP layer                                                     */
01939     /*******************************************************************************/
01940     
01941     cfg.did   = param->DID ;
01942     cfg.nad   = param->NAD ;
01943     cfg.fwt   = RFAL_NFCDEP_MAX_FWT;
01944     cfg.dFwt  = RFAL_NFCDEP_MAX_FWT;
01945     cfg.br    = param->BR ;
01946     cfg.bs    = param->BS ;
01947     cfg.lr    = param->LR ;
01948     cfg.to    = RFAL_NFCDEP_WT_TRG_MAX;            /* Not used in Initiator mode */
01949     
01950     
01951     cfg.gbLen  = param->GBLen ;
01952     ST_MEMCPY( cfg.gb , param->GB , cfg.gbLen  );
01953     
01954     cfg.nfcidLen  = param->nfcidLen ;
01955     ST_MEMCPY( cfg.nfcid , param->nfcid , param->nfcidLen  );
01956     
01957     cfg.role      = RFAL_NFCDEP_ROLE_INITIATOR ;
01958     cfg.oper      = param->operParam ;
01959     cfg.commMode  = param->commMode ;
01960 
01961     rfalNfcDepInitialize();
01962     nfcipConfig( cfg );
01963     
01964     /*******************************************************************************/
01965     /* Send ATR_REQ                                                                */
01966     /*******************************************************************************/
01967     
01968     EXIT_ON_ERR( ret, nfcipTxRx(NFCIP_CMD_ATR_REQ, txBuf, NFCIP_RWT_ACTIVATION, NULL,
01969             0, rxBuf, NFCIP_ATRRES_BUF_LEN, &rxLen, mspiChannel, mST25, gpio_cs,
01970             IRQ, fieldLED_01, fieldLED_02, fieldLED_03, fieldLED_04, fieldLED_05, fieldLED_06 )  );
01971     
01972     
01973     /*******************************************************************************/
01974     /* ATR sent, check response                                                    */
01975     /*******************************************************************************/
01976     msgIt = 0;
01977     rxLen = (uint16_t)(*(rxBuf + msgIt++) - RFAL_NFCDEP_LEN_LEN);                /* use LEN byte             */
01978     
01979     if( (rxLen < RFAL_NFCDEP_ATRRES_MIN_LEN) || (rxLen > RFAL_NFCDEP_ATRRES_MAX_LEN) )  /* Checking length: ATR_RES */
01980     {
01981         return ERR_PROTO;
01982     }
01983     
01984     if( *(rxBuf + msgIt++) != NFCIP_RES )                                   /* Checking if is a response*/
01985     {
01986         return ERR_PROTO;
01987     }
01988     
01989     if( *(rxBuf + msgIt++) != NFCIP_CMD_ATR_RES )                           /* Checking if is a ATR RES */
01990     {
01991         return ERR_PROTO;
01992     }
01993     
01994     ST_MEMCPY(atrRes, (rxBuf + RFAL_NFCDEP_LEN_LEN), rxLen);
01995     *atrResLen = (uint8_t)rxLen;
01996     
01997     return ERR_NONE;
01998 }
01999 
02000 
02001 /*******************************************************************************/
02002 ReturnCode rfalNfcDepPSL( uint8_t BRS, uint8_t FSL, SPI* mspiChannel, ST25R3911* mST25, DigitalOut* gpio_cs, InterruptIn* IRQ, DigitalOut* fieldLED_01, DigitalOut* fieldLED_02, DigitalOut* fieldLED_03, DigitalOut* fieldLED_04, DigitalOut* fieldLED_05, DigitalOut* fieldLED_06 )
02003 {
02004     ReturnCode ret;
02005     uint16_t   rxLen;
02006     uint8_t    msgIt;
02007 
02008     
02009     msgIt = 0;
02010 
02011 
02012     //changed here in plsbufPSL
02013     pslBufPSL[msgIt++] = BRS;
02014     pslBufPSL[msgIt++] = FSL;
02015     
02016     /*******************************************************************************/
02017     /* Send PSL REQ and wait for response                                          */
02018     /*******************************************************************************/
02019     EXIT_ON_ERR( ret, nfcipTxRx( NFCIP_CMD_PSL_REQ, txBufPSL, NFCIP_RWT_ACTIVATION, pslBufPSL, msgIt, rxBufPSL, NFCIP_PSLRES_LEN, &rxLen, mspiChannel, mST25, gpio_cs, IRQ, fieldLED_01, fieldLED_02, fieldLED_03, fieldLED_04, fieldLED_05, fieldLED_06 )  );
02020     
02021     
02022     /*******************************************************************************/
02023     /* PSL sent, check response                                                    */
02024     /*******************************************************************************/
02025     msgIt = 0;
02026     rxLen = (uint16_t)(*(rxBuf + msgIt++) );               /* use LEN byte                   */
02027         
02028     if( rxLen < NFCIP_PSLRES_LEN )                    /* Checking length: LEN + RLS_RES */
02029     {
02030         return ERR_PROTO;
02031     }
02032     
02033     if( *(rxBuf + msgIt++) != NFCIP_RES )             /* Checking if is a response      */
02034     {
02035         return ERR_PROTO;
02036     }
02037     
02038     if( *(rxBuf + msgIt++) != NFCIP_CMD_PSL_RES )     /* Checking if is a PSL RES       */
02039     {
02040         return ERR_PROTO;
02041     }
02042     
02043     if( *(rxBuf + msgIt++) != gNfcip.cfg.did )        /* Checking DID                   */
02044     {
02045         return ERR_PROTO;
02046     }
02047     
02048     return ERR_NONE;
02049 }
02050 
02051 
02052 /*******************************************************************************/
02053 ReturnCode rfalNfcDepDSL( SPI* mspiChannel, ST25R3911* mST25, DigitalOut* gpio_cs, InterruptIn* IRQ, DigitalOut* fieldLED_01, DigitalOut* fieldLED_02, DigitalOut* fieldLED_03, DigitalOut* fieldLED_04, DigitalOut* fieldLED_05, DigitalOut* fieldLED_06 )
02054 {   
02055     ReturnCode ret;
02056 
02057     uint8_t  rxMsgIt;
02058     uint16_t rxLen = 0;
02059         
02060     if( gNfcip.cfg.role == RFAL_NFCDEP_ROLE_TARGET  )
02061     {
02062         return ERR_NONE;                                  /* Target has no deselect procedure */
02063     }
02064     
02065     /* Repeating a DSL REQ is optional, not doing it */
02066     EXIT_ON_ERR( ret, nfcipTxRx( NFCIP_CMD_DSL_REQ, txBufDSL, NFCIP_RWT_ACTIVATION, 0, 0, rxBufDSL, RFAL_NFCDEP_ATRRES_MAX_LEN, &rxLen, mspiChannel, mST25, gpio_cs, IRQ, fieldLED_01, fieldLED_02, fieldLED_03, fieldLED_04, fieldLED_05, fieldLED_06 )  );
02067     
02068     /*******************************************************************************/
02069     rxMsgIt = 0;
02070     
02071     if( *(rxBuf + rxMsgIt++) < NFCIP_DSLRES_MIN )     /* Checking length: LEN + DSL_RES */
02072     {
02073         return ERR_PROTO;
02074     }
02075         
02076     if( *(rxBuf + rxMsgIt++) != NFCIP_RES )           /* Checking if is a response      */
02077     {
02078         return ERR_PROTO;
02079     }
02080     
02081     if( *(rxBuf + rxMsgIt++) != NFCIP_CMD_DSL_RES )  /* Checking if is DSL RES          */
02082     {
02083         return ERR_PROTO;
02084     }
02085      
02086     if( (gNfcip.cfg.did != RFAL_NFCDEP_DID_NO) && (*(rxBuf + rxMsgIt++) != gNfcip.cfg.did )  )
02087     {
02088         return ERR_PROTO;
02089     }
02090     
02091     return ERR_NONE;
02092 }
02093 
02094 
02095 /*******************************************************************************/
02096 ReturnCode rfalNfcDepRLS( SPI* mspiChannel, ST25R3911* mST25, DigitalOut* gpio_cs, InterruptIn* IRQ, DigitalOut* fieldLED_01, DigitalOut* fieldLED_02, DigitalOut* fieldLED_03, DigitalOut* fieldLED_04, DigitalOut* fieldLED_05, DigitalOut* fieldLED_06 )
02097 {   
02098     ReturnCode ret;
02099 
02100     uint8_t    rxMsgIt;
02101     uint16_t   rxLen = 0;
02102     
02103     if( gNfcip.cfg.role == RFAL_NFCDEP_ROLE_TARGET  )
02104         return ERR_NONE;                                  /* Target has no release procedure */
02105         
02106     /* Repeating a RLS REQ is optional, not doing it */
02107     EXIT_ON_ERR( ret, nfcipTxRx( NFCIP_CMD_RLS_REQ, txBufRLS, NFCIP_RWT_ACTIVATION, 0, 0, rxBufRLS, RFAL_NFCDEP_ATRRES_MAX_LEN, &rxLen, mspiChannel, mST25, gpio_cs, IRQ, fieldLED_01, fieldLED_02, fieldLED_03, fieldLED_04, fieldLED_05, fieldLED_06 )  );
02108     
02109     /*******************************************************************************/
02110     rxMsgIt = 0;
02111        
02112     if( *(rxBuf + rxMsgIt++) < NFCIP_RLSRES_MIN )     /* Checking length: LEN + RLS_RES */
02113     {
02114         return ERR_PROTO;
02115     }
02116         
02117     if( *(rxBuf + rxMsgIt++) != NFCIP_RES )           /* Checking if is a response      */
02118     {
02119         return ERR_PROTO;
02120     }
02121     
02122     if( *(rxBuf + rxMsgIt++) != NFCIP_CMD_RLS_RES )  /* Checking if is RLS RES        */
02123     {
02124         return ERR_PROTO;
02125     }
02126      
02127     if( (gNfcip.cfg.did != RFAL_NFCDEP_DID_NO) && (*(rxBuf + rxMsgIt++) != gNfcip.cfg.did )  )
02128     {
02129         return ERR_PROTO;
02130     }
02131     
02132     return ERR_NONE;
02133 }
02134 
02135 
02136 /*******************************************************************************/
02137 ReturnCode rfalNfcDepInitiatorHandleActivation( rfalNfcDepAtrParam * param, rfalBitRate  desiredBR, rfalNfcDepDevice * nfcDepDev, SPI* mspiChannel, ST25R3911* mST25, DigitalOut* gpio_cs, InterruptIn* IRQ, DigitalOut* fieldLED_01, DigitalOut* fieldLED_02, DigitalOut* fieldLED_03, DigitalOut* fieldLED_04, DigitalOut* fieldLED_05, DigitalOut* fieldLED_06 )
02138 {
02139     ReturnCode ret;
02140     uint8_t    maxRetyrs;
02141     uint8_t    PSL_BRS;
02142     uint8_t    PSL_FSL;
02143     bool       sendPSL;
02144     
02145     if( (param == NULL) || (nfcDepDev == NULL) )
02146     {
02147         return ERR_PARAM;
02148     }
02149     
02150     param->NAD  = RFAL_NFCDEP_NAD_NO;          /* Digital 1.1  16.6.2.9  Initiator SHALL NOT use NAD */
02151     maxRetyrs  = NFCIP_ATR_RETRY_MAX;
02152         
02153     /*******************************************************************************/
02154     /* Send ATR REQ and wait for response                                          */
02155     /*******************************************************************************/
02156     do{  /* Upon transmission error ATR REQ should be retried */
02157         
02158          ret = rfalNfcDepATR( param, &nfcDepDev->activation .Target .ATR_RES , &nfcDepDev->activation .Target .ATR_RESLen ,
02159                  mspiChannel, mST25, gpio_cs, IRQ, fieldLED_01, fieldLED_02, fieldLED_03, fieldLED_04,
02160                  fieldLED_05, fieldLED_06 ) ;
02161          
02162          if( nfcipIsTransmissionError(ret) )
02163          {
02164              continue;
02165          }
02166          break;
02167     }
02168     while( (maxRetyrs--) );
02169     
02170     if( ERR_NO_MASK(ret) != ERR_NONE )
02171     {
02172         return ret;
02173     }
02174     
02175     /*******************************************************************************/
02176     /* Compute NFC-DEP device with ATR_RES                                         */
02177     /*******************************************************************************/
02178     nfcDepDev->info .GBLen  = (nfcDepDev->activation .Target .ATR_RESLen  - RFAL_NFCDEP_ATRRES_MIN_LEN);
02179     nfcDepDev->info .DID    = nfcDepDev->activation .Target .ATR_RES .DID ;
02180     nfcDepDev->info .NAD    = RFAL_NFCDEP_NAD_NO;                                      /* Digital 1.1  16.6.3.11 Initiator SHALL ignore b1 of PPt */
02181     nfcDepDev->info .LR     = rfalNfcDepPP2LR( nfcDepDev->activation .Target .ATR_RES .PPt  );
02182     nfcDepDev->info .FS     = rfalNfcDepLR2FS( nfcDepDev->info .LR  );
02183     nfcDepDev->info .WT     = (nfcDepDev->activation .Target .ATR_RES .TO  & RFAL_NFCDEP_WT_MASK);
02184     nfcDepDev->info .FWT    = rfalNfcDepCalculateRWT( nfcDepDev->info .WT  );
02185     nfcDepDev->info .dFWT   = rfalNfcDepCalculateRWT( RFAL_NFCDEP_WT_DELTA );
02186     
02187     rfalGetBitRate( &nfcDepDev->info .DSI , &nfcDepDev->info .DRI  );
02188     
02189     
02190     
02191     /*******************************************************************************/
02192     /* Check if a PSL needs to be sent                                                */
02193     /*******************************************************************************/
02194     sendPSL = false;
02195     PSL_BRS = rfalNfcDepDx2BRS( nfcDepDev->info .DSI  );  /* Set current bit rate divisor on both directions  */
02196     PSL_FSL = nfcDepDev->info .LR ;                       /* Set current Frame Size                           */
02197     
02198     
02199 #if 0
02200     /* Activity 1.0 
02201     *     9.4.6.3   NFC-F NFC-DEP Activation
02202     *     9.4.4.15  NFC-A NFC-DEP Activation
02203     *     
02204     *  PSL_REQ shall only be sent if desired bit rate is different from current
02205     * */
02206     
02207     /*******************************************************************************/
02208     /* Check Frame Size                                                            */
02209     /*******************************************************************************/
02210     if( gNfcip.cfg.lr < nfcDepDev->info .LR  )  /* If our Length reduction is smaller */   
02211     {
02212         sendPSL = true;
02213         
02214         nfcDepDev->info .LR    = MIN( nfcDepDev->info .LR , gNfcip.cfg.lr );
02215         
02216         gNfcip.cfg.lr = nfcDepDev->info .LR                  /* Update nfcip LR  to be used */
02217         gNfcip.fsc    = rfalNfcDepLR2FS( gNfcip.cfg.lr );  /* Update nfcip FSC to be used */     
02218         
02219         PSL_FSL       = gNfcip.cfg.lr;                     /* Set LR to be sent           */
02220         
02221         nfcipLogI( " NFCIP(I) Frame Size differ, PSL new fsc: %d \r\n", gNfcip.fsc );
02222     }
02223 #endif
02224     
02225     
02226     /*******************************************************************************/
02227     /* Check Baud rates                                                            */
02228     /*******************************************************************************/
02229     if( ((nfcDepDev->info .DSI  != desiredBR) && nfcipDxIsSupported( desiredBR, nfcDepDev->activation .Target .ATR_RES .BRt , nfcDepDev->activation .Target .ATR_RES .BSt  ))    /* if desired BR is different and supported     */
02230        /* || (target->brt != RFAL_NFCDEP_Bx_NO_HIGH_BR) || (target->bst != RFAL_NFCDEP_Bx_NO_HIGH_BR)  */  )  /* if target supports higher BR, must send PSL? */
02231     {
02232         sendPSL = true;
02233         PSL_BRS = rfalNfcDepDx2BRS( desiredBR );
02234         
02235         nfcipLogI( " NFCIP(I) BR differ, PSL BR: 0x%02X \r\n", PSL_BRS );
02236     }
02237 
02238     
02239     /*******************************************************************************/
02240     if( sendPSL )
02241     {
02242         /*******************************************************************************/
02243         /* Send PSL REQ and wait for response                                          */
02244         /*******************************************************************************/
02245         EXIT_ON_ERR( ret, rfalNfcDepPSL(PSL_BRS, PSL_FSL, mspiChannel, mST25, gpio_cs, IRQ, fieldLED_01, fieldLED_02, fieldLED_03, fieldLED_04, fieldLED_05, fieldLED_06 )  );
02246         
02247         /* Check if bit rate has been changed */
02248         if( nfcDepDev->info .DSI  != desiredBR )
02249         {
02250             /* Check if device was in Passive NFC-A and went to higher bit rates, use NFC-F */
02251             if( (nfcDepDev->info .DSI  == RFAL_BR_106 ) && (gNfcip.cfg.commMode == RFAL_NFCDEP_COMM_PASSIVE ) )
02252             {
02253                 
02254             #if RFAL_FEATURE_NFCF 
02255                 /* If Passive initialize NFC-F module */
02256                 rfalNfcfPollerInitialize( desiredBR, mspiChannel, mST25, gpio_cs, IRQ, fieldLED_01, fieldLED_02, fieldLED_03, fieldLED_04, fieldLED_05, fieldLED_06 ) ;
02257             #else /* RFAL_FEATURE_NFCF */
02258                 return ERR_NOTSUPP;
02259             #endif /* RFAL_FEATURE_NFCF */
02260                 
02261             }
02262             
02263             nfcDepDev->info .DRI   = desiredBR;  /* DSI Bit Rate coding from Initiator  to Target  */
02264             nfcDepDev->info .DSI   = desiredBR;  /* DRI Bit Rate coding from Target to Initiator   */
02265             
02266             rfalSetBitRate( nfcDepDev->info .DSI , nfcDepDev->info .DRI , mspiChannel, mST25, gpio_cs, IRQ, fieldLED_01, fieldLED_02, fieldLED_03, fieldLED_04, fieldLED_05, fieldLED_06 ) ;
02267         }
02268         
02269         
02270         return ERR_NONE;   /* PSL has been sent    */
02271     }
02272     
02273     return ERR_NONE;       /* No PSL has been sent */
02274 }
02275 
02276 
02277 /*******************************************************************************/
02278 uint32_t rfalNfcDepCalculateRWT( uint8_t wt )
02279 {
02280     /* Digital 1.0  14.6.3.8  &  Digital 1.1  16.6.3.9     */
02281     /* Digital 1.1  16.6.3.9 treat all RFU values as WT=14 */
02282     wt = MIN( RFAL_NFCDEP_WT_INI_MAX, wt );
02283         
02284     return rfalNfcDepWT2RWT(wt);
02285 }
02286 
02287 
02288 
02289 /*******************************************************************************/
02290 static ReturnCode nfcipDataTx( uint8_t* txBuf, uint16_t txBufLen, uint32_t fwt, SPI* mspiChannel, ST25R3911* mST25, DigitalOut* gpio_cs, InterruptIn* IRQ, DigitalOut* fieldLED_01, DigitalOut* fieldLED_02, DigitalOut* fieldLED_03, DigitalOut* fieldLED_04, DigitalOut* fieldLED_05, DigitalOut* fieldLED_06 )
02291 {
02292    return rfalTransceiveBlockingTx( txBuf, txBufLen, gNfcip.rxBuf, gNfcip.rxBufLen, gNfcip.rxRcvdLen, (RFAL_TXRX_FLAGS_DEFAULT | RFAL_TXRX_FLAGS_NFCIP1_ON ), ((fwt == NFCIP_NO_FWT) ? RFAL_FWT_NONE : rfalConv64fcTo1fc(fwt)), mspiChannel, mST25, gpio_cs, IRQ, fieldLED_01, fieldLED_02, fieldLED_03, fieldLED_04, fieldLED_05, fieldLED_06 ) ;
02293 }
02294 
02295 
02296 /*******************************************************************************/
02297 static ReturnCode nfcipDataRx( void )
02298 {
02299     ReturnCode ret;
02300     
02301     ret = rfalGetTransceiveStatus();
02302     
02303     if( ret != ERR_BUSY )
02304     {
02305         (*gNfcip.rxRcvdLen) = rfalConvBitsToBytes( *gNfcip.rxRcvdLen );
02306     
02307         if( (ret == ERR_NONE) && (gNfcip.rxRcvdLen != NULL) && (gNfcip.rxBuf != NULL) )
02308         {
02309             /* Digital 1.1  16.4.1.3 - Length byte LEN SHALL have a value between 3 and 255 ->
02310              *  otherwise treat as Transmission Error *
02311              *                       - Ensure that actual received and frame length do match,
02312              *                        otherwise treat as Transmission error  */
02313 
02314             if( (*gNfcip.rxRcvdLen != (uint16_t)*gNfcip.rxBuf) || (*gNfcip.rxRcvdLen < RFAL_NFCDEP_LEN_MIN) || (*gNfcip.rxRcvdLen > RFAL_NFCDEP_LEN_MAX) )
02315             {
02316                 return ERR_FRAMING;
02317 
02318             }
02319         }
02320     }
02321     
02322     return ret;
02323 }
02324 
02325 
02326 /*******************************************************************************/
02327 ReturnCode rfalNfcDepListenStartActivation( rfalNfcDepTargetParam  *param, uint8_t *atrReq, uint16_t atrReqLength, rfalNfcDepListenActvParam  rxParam, SPI* mspiChannel, ST25R3911* mST25, DigitalOut* gpio_cs, InterruptIn* IRQ, DigitalOut* fieldLED_01, DigitalOut* fieldLED_02, DigitalOut* fieldLED_03, DigitalOut* fieldLED_04, DigitalOut* fieldLED_05, DigitalOut* fieldLED_06 )
02328 {
02329     ReturnCode        ret;
02330     rfalNfcDepConfigs  cfg;
02331     
02332     
02333     if( (param == NULL) || (atrReq == NULL) || (rxParam.rxLen  == NULL) )
02334     {
02335         return ERR_PARAM;
02336     }
02337     
02338     
02339     /*******************************************************************************/
02340     /*  Check whether is a valid ATR_REQ Compute NFC-DEP device                    */
02341     if( !rfalNfcDepIsAtrReq( atrReq, atrReqLength, NULL ) )
02342     {
02343         return ERR_PARAM;
02344     }
02345     
02346     rxParam.nfcDepDev ->activation .Initiator .ATR_REQLen  = atrReqLength;                   /* nfcipIsAtrReq() is already checking Min and Max buffer lengths */ 
02347     ST_MEMCPY( (uint8_t*)&rxParam.nfcDepDev ->activation .Initiator .ATR_REQ , atrReq, atrReqLength );
02348     
02349     rxParam.nfcDepDev ->info .GBLen  = (atrReqLength - RFAL_NFCDEP_ATRREQ_MIN_LEN);
02350     rxParam.nfcDepDev ->info .DID    = rxParam.nfcDepDev ->activation .Initiator .ATR_REQ .DID ;
02351     rxParam.nfcDepDev ->info .NAD    = RFAL_NFCDEP_NAD_NO;                        /* Digital 1.1  16.6.2.9  Initiator SHALL NOT use NAD */
02352     rxParam.nfcDepDev ->info .LR     = rfalNfcDepPP2LR( rxParam.nfcDepDev ->activation .Initiator .ATR_REQ .PPi  );
02353     rxParam.nfcDepDev ->info .FS     = rfalNfcDepLR2FS( rxParam.nfcDepDev ->info .LR  );
02354     rxParam.nfcDepDev ->info .WT     = 0;
02355     rxParam.nfcDepDev ->info .FWT    = NFCIP_NO_FWT;
02356     rxParam.nfcDepDev ->info .dFWT   = NFCIP_NO_FWT;
02357     
02358     rfalGetBitRate( &rxParam.nfcDepDev ->info .DSI , &rxParam.nfcDepDev ->info .DRI  );
02359     
02360     
02361     /* Store Device Info location, updated upon a PSL  */
02362     gNfcip.nfcDepDev = rxParam.nfcDepDev ;
02363     
02364     
02365     /*******************************************************************************/
02366     cfg.did  = rxParam.nfcDepDev ->activation .Initiator .ATR_REQ .DID ;
02367     cfg.nad  = RFAL_NFCDEP_NAD_NO;
02368     
02369     cfg.fwt    = RFAL_NFCDEP_MAX_FWT;
02370     cfg.dFwt   = RFAL_NFCDEP_MAX_FWT;
02371 
02372     cfg.br  = param->brt ;
02373     cfg.bs  = param->bst ;
02374     
02375     cfg.lr  = rfalNfcDepPP2LR(param->ppt );
02376     
02377     cfg.gbLen  = param->GBtLen ;
02378     ST_MEMCPY(cfg.gb , param->GBt , cfg.gbLen );
02379     
02380     cfg.nfcidLen  = RFAL_NFCDEP_NFCID3_LEN;
02381     ST_MEMCPY(cfg.nfcid , param->nfcid3 , RFAL_NFCDEP_NFCID3_LEN);
02382 
02383     cfg.to  = param->to ;
02384     
02385     cfg.role      = RFAL_NFCDEP_ROLE_TARGET ;
02386     cfg.oper      = param->operParam ;
02387     cfg.commMode  = param->commMode ;
02388 
02389     rfalNfcDepInitialize();
02390     nfcipConfig( cfg );
02391         
02392     
02393     /*******************************************************************************/
02394     /*  Reply with ATR RES to Initiator                                            */
02395     /*******************************************************************************/
02396     gNfcip.rxBuf        = (uint8_t*)rxParam.rxBuf ;
02397     gNfcip.rxBufLen     = sizeof(rfalNfcDepBufFormat );
02398     gNfcip.rxRcvdLen    = rxParam.rxLen ;
02399     gNfcip.rxBufPaylPos = RFAL_NFCDEP_DEPREQ_HEADER_LEN;
02400     gNfcip.isChaining   = rxParam.isRxChaining ;
02401     gNfcip.txBufPaylPos = RFAL_NFCDEP_DEPREQ_HEADER_LEN;
02402    
02403     EXIT_ON_ERR( ret, nfcipTx( NFCIP_CMD_ATR_RES, (uint8_t*) gNfcip.rxBuf, NULL, 0, 0, NFCIP_NO_FWT, mspiChannel, mST25, gpio_cs, IRQ, fieldLED_01, fieldLED_02, fieldLED_03, fieldLED_04, fieldLED_05, fieldLED_06 )  );
02404     
02405     gNfcip.state = NFCIP_ST_TARG_WAIT_ACTV;
02406     
02407     return ERR_NONE;
02408 }
02409 
02410 
02411 /*******************************************************************************/
02412 ReturnCode rfalNfcDepListenGetActivationStatus( SPI*  mspiChannel, ST25R3911* mST25, DigitalOut* gpio_cs, InterruptIn* IRQ, DigitalOut* fieldLED_01, DigitalOut* fieldLED_02, DigitalOut* fieldLED_03, DigitalOut* fieldLED_04, DigitalOut* fieldLED_05, DigitalOut* fieldLED_06 )
02413 {
02414     ReturnCode err;
02415     uint8_t    BRS;
02416     
02417     BRS = RFAL_NFCDEP_BRS_MAINTAIN;
02418     
02419     err = nfcipTargetHandleActivation( gNfcip.nfcDepDev, &BRS, mspiChannel, mST25, gpio_cs, IRQ, fieldLED_01, fieldLED_02, fieldLED_03, fieldLED_04, fieldLED_05, fieldLED_06 ) ;
02420     
02421     switch (err)
02422     {
02423         case ERR_NONE:
02424             
02425             if( BRS != RFAL_NFCDEP_BRS_MAINTAIN )
02426             {
02427                 /* DSI codes the bit rate from Initiator to Target */
02428                 /* DRI codes the bit rate from Target to Initiator */
02429                 
02430                 if( gNfcip.cfg.commMode == RFAL_NFCDEP_COMM_ACTIVE  )
02431                 {
02432                     EXIT_ON_ERR( err, rfalSetMode( RFAL_MODE_LISTEN_ACTIVE_P2P , gNfcip.nfcDepDev->info.DRI, gNfcip.nfcDepDev->info.DSI, mspiChannel, mST25, gpio_cs, IRQ, fieldLED_01, fieldLED_02, fieldLED_03, fieldLED_04, fieldLED_05, fieldLED_06 ) );
02433                 }
02434                 else
02435                 {
02436                     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, mspiChannel, mST25, gpio_cs, IRQ, fieldLED_01, fieldLED_02, fieldLED_03, fieldLED_04, fieldLED_05, fieldLED_06 )  );
02437                 }
02438             }
02439             break;
02440         
02441         case ERR_BUSY:
02442             /* do nothing */
02443             break;
02444             
02445         case ERR_PROTO:
02446         default:
02447             /* re-enable receiving of data */
02448             nfcDepReEnableRx( gNfcip.rxBuf, gNfcip.rxBufLen, gNfcip.rxRcvdLen );
02449             break;
02450     } 
02451     
02452     return err;
02453 }
02454 
02455 
02456 /*******************************************************************************/
02457 ReturnCode rfalNfcDepStartTransceive( rfalNfcDepTxRxParam  *param )
02458 {
02459     rfalNfcDepDEPParams nfcDepParams;
02460     
02461     nfcDepParams.txBuf        = (uint8_t *)param->txBuf ;
02462     nfcDepParams.txBufLen     = param->txBufLen ;
02463     nfcDepParams.txChaining   = param->isTxChaining ;
02464     nfcDepParams.txBufPaylPos = RFAL_NFCDEP_DEPREQ_HEADER_LEN;  /* position in txBuf where actual outgoing data is located */
02465     nfcDepParams.did          = RFAL_NFCDEP_DID_KEEP;
02466     nfcDepParams.rxBufPaylPos = RFAL_NFCDEP_DEPREQ_HEADER_LEN;
02467     nfcDepParams.rxBuf        = (uint8_t *)param->rxBuf ;
02468     nfcDepParams.rxBufLen     = sizeof(rfalNfcDepBufFormat );
02469     nfcDepParams.fsc          = param->FSx ;
02470     nfcDepParams.fwt          = param->FWT ;
02471     nfcDepParams.dFwt         = param->dFWT ;
02472 
02473     gNfcip.rxRcvdLen          = param->rxLen ;
02474     gNfcip.isChaining         = param->isRxChaining ;
02475 
02476     nfcipSetDEPParams(&nfcDepParams);
02477     
02478     return ERR_NONE;
02479 }
02480 
02481 
02482 /*******************************************************************************/
02483 ReturnCode rfalNfcDepGetTransceiveStatus( SPI* mspiChannel, ST25R3911* mST25, DigitalOut* gpio_cs, InterruptIn* IRQ, DigitalOut* fieldLED_01, DigitalOut* fieldLED_02, DigitalOut* fieldLED_03, DigitalOut* fieldLED_04, DigitalOut* fieldLED_05, DigitalOut* fieldLED_06 )
02484 {
02485     return nfcipRun( gNfcip.rxRcvdLen, gNfcip.isChaining, mspiChannel, mST25, gpio_cs, IRQ, fieldLED_01, fieldLED_02, fieldLED_03, fieldLED_04, fieldLED_05, fieldLED_06 ) ;
02486 }
02487 
02488 #endif /* RFAL_FEATURE_NFC_DEP */