rfal_rfst25r3916.c 198 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815
  1. /******************************************************************************
  2. * \attention
  3. *
  4. * <h2><center>&copy; COPYRIGHT 2020 STMicroelectronics</center></h2>
  5. *
  6. * Licensed under ST MYLIBERTY SOFTWARE LICENSE AGREEMENT (the "License");
  7. * You may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at:
  9. *
  10. * www.st.com/myliberty
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied,
  15. * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. *
  20. ******************************************************************************/
  21. /*
  22. * PROJECT: ST25R3916 firmware
  23. * Revision:
  24. * LANGUAGE: ISO C99
  25. */
  26. /*! \file
  27. *
  28. * \author Gustavo Patricio
  29. *
  30. * \brief RF Abstraction Layer (RFAL)
  31. *
  32. * RFAL implementation for ST25R3916
  33. */
  34. /*
  35. ******************************************************************************
  36. * INCLUDES
  37. ******************************************************************************
  38. */
  39. #include "rfal_chip.h"
  40. #include "utils.h"
  41. #include "st25r3916.h"
  42. #include "st25r3916_com.h"
  43. #include "st25r3916_irq.h"
  44. #include "rfal_analogConfig.h"
  45. #include "rfal_iso15693_2.h"
  46. #include "rfal_crc.h"
  47. /*
  48. ******************************************************************************
  49. * ENABLE SWITCHS
  50. ******************************************************************************
  51. */
  52. #ifndef RFAL_FEATURE_LISTEN_MODE
  53. #define RFAL_FEATURE_LISTEN_MODE false /* Listen Mode configuration missing. Disabled by default */
  54. #endif /* RFAL_FEATURE_LISTEN_MODE */
  55. #ifndef RFAL_FEATURE_WAKEUP_MODE
  56. #define RFAL_FEATURE_WAKEUP_MODE \
  57. false /* Wake-Up mode configuration missing. Disabled by default */
  58. #endif /* RFAL_FEATURE_WAKEUP_MODE */
  59. #ifndef RFAL_FEATURE_LOWPOWER_MODE
  60. #define RFAL_FEATURE_LOWPOWER_MODE \
  61. false /* Low Power mode configuration missing. Disabled by default */
  62. #endif /* RFAL_FEATURE_LOWPOWER_MODE */
  63. /*
  64. ******************************************************************************
  65. * GLOBAL TYPES
  66. ******************************************************************************
  67. */
  68. /*! Struct that holds all involved on a Transceive including the context passed by the caller */
  69. typedef struct {
  70. rfalTransceiveState state; /*!< Current transceive state */
  71. rfalTransceiveState lastState; /*!< Last transceive state (debug purposes) */
  72. ReturnCode status; /*!< Current status/error of the transceive */
  73. rfalTransceiveContext ctx; /*!< The transceive context given by the caller */
  74. } rfalTxRx;
  75. /*! Struct that holds all context for the Listen Mode */
  76. typedef struct {
  77. rfalLmState state; /*!< Current Listen Mode state */
  78. uint32_t mdMask; /*!< Listen Mode mask used */
  79. uint32_t mdReg; /*!< Listen Mode register value used */
  80. uint32_t mdIrqs; /*!< Listen Mode IRQs used */
  81. rfalBitRate brDetected; /*!< Last bit rate detected */
  82. uint8_t* rxBuf; /*!< Location to store incoming data in Listen Mode */
  83. uint16_t rxBufLen; /*!< Length of rxBuf */
  84. uint16_t* rxLen; /*!< Pointer to write the data length placed into rxBuf */
  85. bool dataFlag; /*!< Listen Mode current Data Flag */
  86. bool iniFlag; /*!< Listen Mode initialized Flag (FeliCa slots) */
  87. } rfalLm;
  88. /*! Struct that holds all context for the Wake-Up Mode */
  89. typedef struct {
  90. rfalWumState state; /*!< Current Wake-Up Mode state */
  91. rfalWakeUpConfig cfg; /*!< Current Wake-Up Mode context */
  92. } rfalWum;
  93. /*! Struct that holds all context for the Low Power Mode */
  94. typedef struct {
  95. bool isRunning;
  96. } rfalLpm;
  97. /*! Struct that holds the timings GT and FDTs */
  98. typedef struct {
  99. uint32_t GT; /*!< GT in 1/fc */
  100. uint32_t FDTListen; /*!< FDTListen in 1/fc */
  101. uint32_t FDTPoll; /*!< FDTPoll in 1/fc */
  102. uint8_t nTRFW; /*!< n*TRFW used during RF CA */
  103. } rfalTimings;
  104. /*! Struct that holds the software timers */
  105. typedef struct {
  106. uint32_t GT; /*!< RFAL's GT timer */
  107. uint32_t RXE; /*!< Timer between RXS and RXE */
  108. uint32_t txRx; /*!< Transceive sanity timer */
  109. } rfalTimers;
  110. /*! Struct that holds the RFAL's callbacks */
  111. typedef struct {
  112. rfalPreTxRxCallback preTxRx; /*!< RFAL's Pre TxRx callback */
  113. rfalPostTxRxCallback postTxRx; /*!< RFAL's Post TxRx callback */
  114. RfalStateChangedCallback state_changed_cb;
  115. void* ctx;
  116. } rfalCallbacks;
  117. /*! Struct that holds counters to control the FIFO on Tx and Rx */
  118. typedef struct {
  119. uint16_t
  120. expWL; /*!< The amount of bytes expected to be Tx when a WL interrupt occours */
  121. uint16_t
  122. bytesTotal; /*!< Total bytes to be transmitted OR the total bytes received */
  123. uint16_t
  124. bytesWritten; /*!< Amount of bytes already written on FIFO (Tx) OR read (RX) from FIFO and written on rxBuffer*/
  125. uint8_t status
  126. [ST25R3916_FIFO_STATUS_LEN]; /*!< FIFO Status Registers */
  127. } rfalFIFO;
  128. /*! Struct that holds RFAL's configuration settings */
  129. typedef struct {
  130. uint8_t obsvModeTx; /*!< RFAL's config of the ST25R3916's observation mode while Tx */
  131. uint8_t obsvModeRx; /*!< RFAL's config of the ST25R3916's observation mode while Rx */
  132. rfalEHandling eHandling; /*!< RFAL's error handling config/mode */
  133. } rfalConfigs;
  134. /*! Struct that holds NFC-F data - Used only inside rfalFelicaPoll() (static to avoid adding it into stack) */
  135. typedef struct {
  136. rfalFeliCaPollRes
  137. pollResponses[RFAL_FELICA_POLL_MAX_SLOTS]; /* FeliCa Poll response container for 16 slots */
  138. } rfalNfcfWorkingData;
  139. /*! Struct that holds NFC-V current context
  140. *
  141. * This buffer has to be big enough for coping with maximum response size (hamming coded)
  142. * - inventory requests responses: 14*2+2 bytes
  143. * - read single block responses: (32+4)*2+2 bytes
  144. * - read multiple block could be very long... -> not supported
  145. * - current implementation expects it be written in one bulk into FIFO
  146. * - needs to be above FIFO water level of ST25R3916 (200)
  147. * - the coding function needs to be able to
  148. * put more than FIFO water level bytes into it (n*64+1)>200 */
  149. typedef struct {
  150. uint8_t codingBuffer[(
  151. (2 + 255 + 3) * 2)]; /*!< Coding buffer, length MUST be above 257: [257; ...] */
  152. uint16_t
  153. nfcvOffset; /*!< Offset needed for ISO15693 coding function */
  154. rfalTransceiveContext
  155. origCtx; /*!< context provided by user */
  156. uint16_t
  157. ignoreBits; /*!< Number of bits at the beginning of a frame to be ignored when decoding */
  158. } rfalNfcvWorkingData;
  159. /*! RFAL instance */
  160. typedef struct {
  161. rfalState state; /*!< RFAL's current state */
  162. rfalMode mode; /*!< RFAL's current mode */
  163. rfalBitRate txBR; /*!< RFAL's current Tx Bit Rate */
  164. rfalBitRate rxBR; /*!< RFAL's current Rx Bit Rate */
  165. bool field; /*!< Current field state (On / Off) */
  166. rfalConfigs conf; /*!< RFAL's configuration settings */
  167. rfalTimings timings; /*!< RFAL's timing setting */
  168. rfalTxRx TxRx; /*!< RFAL's transceive management */
  169. rfalFIFO fifo; /*!< RFAL's FIFO management */
  170. rfalTimers tmr; /*!< RFAL's Software timers */
  171. rfalCallbacks callbacks; /*!< RFAL's callbacks */
  172. #if RFAL_FEATURE_LISTEN_MODE
  173. rfalLm Lm; /*!< RFAL's listen mode management */
  174. #endif /* RFAL_FEATURE_LISTEN_MODE */
  175. #if RFAL_FEATURE_WAKEUP_MODE
  176. rfalWum wum; /*!< RFAL's Wake-up mode management */
  177. #endif /* RFAL_FEATURE_WAKEUP_MODE */
  178. #if RFAL_FEATURE_LOWPOWER_MODE
  179. rfalLpm lpm; /*!< RFAL's Low power mode management */
  180. #endif /* RFAL_FEATURE_LOWPOWER_MODE */
  181. #if RFAL_FEATURE_NFCF
  182. rfalNfcfWorkingData nfcfData; /*!< RFAL's working data when supporting NFC-F */
  183. #endif /* RFAL_FEATURE_NFCF */
  184. #if RFAL_FEATURE_NFCV
  185. rfalNfcvWorkingData nfcvData; /*!< RFAL's working data when performing NFC-V */
  186. #endif /* RFAL_FEATURE_NFCV */
  187. } rfal;
  188. /*! Felica's command set */
  189. typedef enum {
  190. FELICA_CMD_POLLING =
  191. 0x00, /*!< Felica Poll/REQC command (aka SENSF_REQ) to identify a card */
  192. FELICA_CMD_POLLING_RES =
  193. 0x01, /*!< Felica Poll/REQC command (aka SENSF_RES) response */
  194. FELICA_CMD_REQUEST_SERVICE =
  195. 0x02, /*!< verify the existence of Area and Service */
  196. FELICA_CMD_REQUEST_RESPONSE =
  197. 0x04, /*!< verify the existence of a card */
  198. FELICA_CMD_READ_WITHOUT_ENCRYPTION =
  199. 0x06, /*!< read Block Data from a Service that requires no authentication */
  200. FELICA_CMD_WRITE_WITHOUT_ENCRYPTION =
  201. 0x08, /*!< write Block Data to a Service that requires no authentication */
  202. FELICA_CMD_REQUEST_SYSTEM_CODE =
  203. 0x0C, /*!< acquire the System Code registered to a card */
  204. FELICA_CMD_AUTHENTICATION1 =
  205. 0x10, /*!< authenticate a card */
  206. FELICA_CMD_AUTHENTICATION2 =
  207. 0x12, /*!< allow a card to authenticate a Reader/Writer */
  208. FELICA_CMD_READ = 0x14, /*!< read Block Data from a Service that requires authentication */
  209. FELICA_CMD_WRITE = 0x16, /*!< write Block Data to a Service that requires authentication */
  210. } t_rfalFeliCaCmd;
  211. /*! Union representing all PTMem sections */
  212. typedef union { /* PRQA S 0750 # MISRA 19.2 - Both members are of the same type, just different names. Thus no problem can occur. */
  213. uint8_t PTMem_A
  214. [ST25R3916_PTM_A_LEN]; /*!< PT_Memory area allocated for NFC-A configuration */
  215. uint8_t PTMem_F
  216. [ST25R3916_PTM_F_LEN]; /*!< PT_Memory area allocated for NFC-F configuration */
  217. uint8_t
  218. TSN[ST25R3916_PTM_TSN_LEN]; /*!< PT_Memory area allocated for TSN - Random numbers */
  219. } t_rfalPTMem;
  220. /*
  221. ******************************************************************************
  222. * GLOBAL DEFINES
  223. ******************************************************************************
  224. */
  225. #define RFAL_FIFO_IN_WL \
  226. 200U /*!< Number of bytes in the FIFO when WL interrupt occurs while Tx */
  227. #define RFAL_FIFO_OUT_WL \
  228. (ST25R3916_FIFO_DEPTH - \
  229. RFAL_FIFO_IN_WL) /*!< Number of bytes sent/out of the FIFO when WL interrupt occurs while Tx */
  230. #define RFAL_FIFO_STATUS_REG1 \
  231. 0U /*!< Location of FIFO status register 1 in local copy */
  232. #define RFAL_FIFO_STATUS_REG2 \
  233. 1U /*!< Location of FIFO status register 2 in local copy */
  234. #define RFAL_FIFO_STATUS_INVALID \
  235. 0xFFU /*!< Value indicating that the local FIFO status in invalid|cleared */
  236. #define RFAL_ST25R3916_GPT_MAX_1FC \
  237. rfalConv8fcTo1fc( \
  238. 0xFFFFU) /*!< Max GPT steps in 1fc (0xFFFF steps of 8/fc => 0xFFFF * 590ns = 38,7ms) */
  239. #define RFAL_ST25R3916_NRT_MAX_1FC \
  240. rfalConv4096fcTo1fc( \
  241. 0xFFFFU) /*!< Max NRT steps in 1fc (0xFFFF steps of 4096/fc => 0xFFFF * 302us = 19.8s ) */
  242. #define RFAL_ST25R3916_NRT_DISABLED \
  243. 0U /*!< NRT Disabled: All 0 No-response timer is not started, wait forever */
  244. #define RFAL_ST25R3916_MRT_MAX_1FC \
  245. rfalConv64fcTo1fc( \
  246. 0x00FFU) /*!< Max MRT steps in 1fc (0x00FF steps of 64/fc => 0x00FF * 4.72us = 1.2ms ) */
  247. #define RFAL_ST25R3916_MRT_MIN_1FC \
  248. rfalConv64fcTo1fc( \
  249. 0x0004U) /*!< Min MRT steps in 1fc ( 0<=mrt<=4 ; 4 (64/fc) => 0x0004 * 4.72us = 18.88us ) */
  250. #define RFAL_ST25R3916_GT_MAX_1FC \
  251. rfalConvMsTo1fc( \
  252. 6000U) /*!< Max GT value allowed in 1/fc (SFGI=14 => SFGT + dSFGT = 5.4s) */
  253. #define RFAL_ST25R3916_GT_MIN_1FC \
  254. rfalConvMsTo1fc( \
  255. RFAL_ST25R3916_SW_TMR_MIN_1MS) /*!< Min GT value allowed in 1/fc */
  256. #define RFAL_ST25R3916_SW_TMR_MIN_1MS \
  257. 1U /*!< Min value of a SW timer in ms */
  258. #define RFAL_OBSMODE_DISABLE \
  259. 0x00U /*!< Observation Mode disabled */
  260. #define RFAL_RX_INCOMPLETE_MAXLEN \
  261. (uint8_t)1U /*!< Threshold value where incoming rx may be considered as incomplete */
  262. #define RFAL_EMVCO_RX_MAXLEN \
  263. (uint8_t)4U /*!< Maximum value where EMVCo to apply special error handling */
  264. #define RFAL_NORXE_TOUT \
  265. 50U /*!< Timeout to be used on a potential missing RXE - Silicon ST25R3916 Errata #TBD */
  266. #define RFAL_ISO14443A_SDD_RES_LEN \
  267. 5U /*!< SDD_RES | Anticollision (UID CLn) length - rfalNfcaSddRes */
  268. #define RFAL_ISO14443A_CRC_INTVAL \
  269. 0x6363 /*!< ISO14443 CRC Initial Value|Register */
  270. #define RFAL_FELICA_POLL_DELAY_TIME \
  271. 512U /*!< FeliCa Poll Processing time is 2.417 ms ~512*64/fc Digital 1.1 A4 */
  272. #define RFAL_FELICA_POLL_SLOT_TIME \
  273. 256U /*!< FeliCa Poll Time Slot duration is 1.208 ms ~256*64/fc Digital 1.1 A4 */
  274. #define RFAL_LM_SENSF_RD0_POS \
  275. 17U /*!< FeliCa SENSF_RES Request Data RD0 position */
  276. #define RFAL_LM_SENSF_RD1_POS \
  277. 18U /*!< FeliCa SENSF_RES Request Data RD1 position */
  278. #define RFAL_LM_NFCID_INCOMPLETE \
  279. 0x04U /*!< NFCA NFCID not complete bit in SEL_RES (SAK) */
  280. #define RFAL_ISO15693_IGNORE_BITS \
  281. rfalConvBytesToBits( \
  282. 2U) /*!< Ignore collisions before the UID (RES_FLAG + DSFID) */
  283. #define RFAL_ISO15693_INV_RES_LEN \
  284. 12U /*!< ISO15693 Inventory response length with CRC (bytes) */
  285. #define RFAL_ISO15693_INV_RES_DUR \
  286. 4U /*!< ISO15693 Inventory response duration @ 26 kbps (ms) */
  287. #define RFAL_WU_MIN_WEIGHT_VAL \
  288. 4U /*!< ST25R3916 minimum Wake-up weight value */
  289. /*******************************************************************************/
  290. #define RFAL_LM_GT \
  291. rfalConvUsTo1fc( \
  292. 100U) /*!< Listen Mode Guard Time enforced (GT - Passive; TIRFG - Active) */
  293. #define RFAL_FDT_POLL_ADJUSTMENT \
  294. rfalConvUsTo1fc( \
  295. 80U) /*!< FDT Poll adjustment: Time between the expiration of GPT to the actual Tx */
  296. #define RFAL_FDT_LISTEN_MRT_ADJUSTMENT \
  297. 64U /*!< MRT jitter adjustment: timeout will be between [ tout ; tout + 64 cycles ] */
  298. #define RFAL_AP2P_FIELDOFF_TRFW \
  299. rfalConv8fcTo1fc( \
  300. 64U) /*!< Time after TXE and Field Off in AP2P Trfw: 37.76us -> 64 (8/fc) */
  301. #ifndef RFAL_ST25R3916_AAT_SETTLE
  302. #define RFAL_ST25R3916_AAT_SETTLE \
  303. 5U /*!< Time in ms required for AAT pins and Osc to settle after en bit set */
  304. #endif /* RFAL_ST25R3916_AAT_SETTLE */
  305. /*! FWT adjustment:
  306. * 64 : NRT jitter between TXE and NRT start */
  307. #define RFAL_FWT_ADJUSTMENT 64U
  308. /*! FWT ISO14443A adjustment:
  309. * 512 : 4bit length
  310. * 64 : Half a bit duration due to ST25R3916 Coherent receiver (1/fc) */
  311. #define RFAL_FWT_A_ADJUSTMENT (512U + 64U)
  312. /*! FWT ISO14443B adjustment:
  313. * SOF (14etu) + 1Byte (10etu) + 1etu (IRQ comes 1etu after first byte) - 3etu (ST25R3916 sends TXE 3etu after) */
  314. #define RFAL_FWT_B_ADJUSTMENT ((14U + 10U + 1U - 3U) * 128U)
  315. /*! FWT FeliCa 212 adjustment:
  316. * 1024 : Length of the two Sync bytes at 212kbps */
  317. #define RFAL_FWT_F_212_ADJUSTMENT 1024U
  318. /*! FWT FeliCa 424 adjustment:
  319. * 512 : Length of the two Sync bytes at 424kbps */
  320. #define RFAL_FWT_F_424_ADJUSTMENT 512U
  321. /*! Time between our field Off and other peer field On : Tadt + (n x Trfw)
  322. * Ecma 340 11.1.2 - Tadt: [56.64 , 188.72] us ; n: [0 , 3] ; Trfw = 37.76 us
  323. * Should be: 189 + (3*38) = 303us ; we'll use a more relaxed setting: 605 us */
  324. #define RFAL_AP2P_FIELDON_TADTTRFW rfalConvUsTo1fc(605U)
  325. /*! FDT Listen adjustment for ISO14443A EMVCo 2.6 4.8.1.3 ; Digital 1.1 6.10
  326. *
  327. * 276: Time from the rising pulse of the pause of the logic '1' (i.e. the time point to measure the deaftime from),
  328. * to the actual end of the EOF sequence (the point where the MRT starts). Please note that the ST25R391x uses the
  329. * ISO14443-2 definition where the EOF consists of logic '0' followed by sequence Y.
  330. * -64: Further adjustment for receiver to be ready just before first bit
  331. */
  332. #define RFAL_FDT_LISTEN_A_ADJUSTMENT (276U - 64U)
  333. /*! FDT Listen adjustment for ISO14443B EMVCo 2.6 4.8.1.6 ; Digital 1.1 7.9
  334. *
  335. * 340: Time from the rising edge of the EoS to the starting point of the MRT timer (sometime after the final high
  336. * part of the EoS is completed)
  337. */
  338. #define RFAL_FDT_LISTEN_B_ADJUSTMENT 340U
  339. /*! FDT Listen adjustment for ISO15693
  340. * ISO15693 2000 8.4 t1 MIN = 4192/fc
  341. * ISO15693 2009 9.1 t1 MIN = 4320/fc
  342. * Digital 2.1 B.5 FDTV,LISTEN,MIN = 4310/fc
  343. * Set FDT Listen one step earlier than on the more recent spec versions for greater interoprability
  344. */
  345. #define RFAL_FDT_LISTEN_V_ADJUSTMENT 64U
  346. /*! FDT Poll adjustment for ISO14443B Correlator - sst 5 etu */
  347. #define RFAL_FDT_LISTEN_B_ADJT_CORR 128U
  348. /*! FDT Poll adjustment for ISO14443B Correlator sst window - 5 etu */
  349. #define RFAL_FDT_LISTEN_B_ADJT_CORR_SST 20U
  350. /*
  351. ******************************************************************************
  352. * GLOBAL MACROS
  353. ******************************************************************************
  354. */
  355. /*! Calculates Transceive Sanity Timer. It accounts for the slowest bit rate and the longest data format
  356. * 1s for transmission and reception of a 4K message at 106kpbs (~425ms each direction)
  357. * plus TxRx preparation and FIFO load over Serial Interface */
  358. #define rfalCalcSanityTmr(fwt) (uint16_t)(1000U + rfalConv1fcToMs((fwt)))
  359. #define rfalGennTRFW(n) \
  360. (((n) + 1U) & \
  361. ST25R3916_REG_AUX_nfc_n_mask) /*!< Generates the next n*TRRW used for RFCA */
  362. #define rfalCalcNumBytes(nBits) \
  363. (((uint32_t)(nBits) + 7U) / \
  364. 8U) /*!< Returns the number of bytes required to fit given the number of bits */
  365. #define rfalTimerStart(timer, time_ms) \
  366. do { \
  367. platformTimerDestroy(timer); \
  368. (timer) = platformTimerCreate((uint16_t)(time_ms)); \
  369. } while(0) /*!< Configures and starts timer */
  370. #define rfalTimerisExpired(timer) \
  371. platformTimerIsExpired( \
  372. timer) /*!< Checks if timer has expired */
  373. #define rfalTimerDestroy(timer) \
  374. platformTimerDestroy( \
  375. timer) /*!< Destroys timer */
  376. #define rfalST25R3916ObsModeDisable() \
  377. st25r3916WriteTestRegister( \
  378. 0x01U, \
  379. (0x40U)) /*!< Disable ST25R3916 Observation mode */
  380. #define rfalST25R3916ObsModeTx() \
  381. st25r3916WriteTestRegister( \
  382. 0x01U, \
  383. (0x40U | \
  384. gRFAL.conf \
  385. .obsvModeTx)) /*!< Enable Tx Observation mode */
  386. #define rfalST25R3916ObsModeRx() \
  387. st25r3916WriteTestRegister( \
  388. 0x01U, \
  389. (0x40U | \
  390. gRFAL.conf \
  391. .obsvModeRx)) /*!< Enable Rx Observation mode */
  392. #define rfalCheckDisableObsMode() \
  393. if(gRFAL.conf.obsvModeRx != 0U) { \
  394. rfalST25R3916ObsModeDisable(); \
  395. } /*!< Checks if the observation mode is enabled, and applies on ST25R3916 */
  396. #define rfalCheckEnableObsModeTx() \
  397. if(gRFAL.conf.obsvModeTx != 0U) { \
  398. rfalST25R3916ObsModeTx(); \
  399. } /*!< Checks if the observation mode is enabled, and applies on ST25R3916 */
  400. #define rfalCheckEnableObsModeRx() \
  401. if(gRFAL.conf.obsvModeRx != 0U) { \
  402. rfalST25R3916ObsModeRx(); \
  403. } /*!< Checks if the observation mode is enabled, and applies on ST25R3916 */
  404. #define rfalGetIncmplBits(FIFOStatus2) \
  405. (((FIFOStatus2) >> 1) & \
  406. 0x07U) /*!< Returns the number of bits from fifo status */
  407. #define rfalIsIncompleteByteError(error) \
  408. (((error) >= ERR_INCOMPLETE_BYTE) && \
  409. ((error) <= \
  410. ERR_INCOMPLETE_BYTE_07)) /*!< Checks if given error is a Incomplete error */
  411. #define rfalAdjACBR(b) \
  412. (((uint16_t)(b) >= (uint16_t)RFAL_BR_52p97) ? \
  413. (uint16_t)(b) : \
  414. ((uint16_t)(b) + \
  415. 1U)) /*!< Adjusts ST25R391x Bit rate to Analog Configuration */
  416. #define rfalConvBR2ACBR(b) \
  417. (((rfalAdjACBR((b))) << RFAL_ANALOG_CONFIG_BITRATE_SHIFT) & \
  418. RFAL_ANALOG_CONFIG_BITRATE_MASK) /*!< Converts ST25R391x Bit rate to Analog Configuration bit rate id */
  419. #define rfalConvTDFormat(v) \
  420. ((uint16_t)(v) << 8U) /*!< Converts a uint8_t to the format used in SW Tag Detection */
  421. /*
  422. ******************************************************************************
  423. * LOCAL VARIABLES
  424. ******************************************************************************
  425. */
  426. static rfal gRFAL; /*!< RFAL module instance */
  427. /*
  428. ******************************************************************************
  429. * LOCAL FUNCTION PROTOTYPES
  430. ******************************************************************************
  431. */
  432. static void rfalTransceiveTx(void);
  433. static void rfalTransceiveRx(void);
  434. static ReturnCode rfalTransceiveRunBlockingTx(void);
  435. static void rfalPrepareTransceive(void);
  436. static void rfalCleanupTransceive(void);
  437. static void rfalErrorHandling(void);
  438. static ReturnCode rfalRunTransceiveWorker(void);
  439. #if RFAL_FEATURE_LISTEN_MODE
  440. static ReturnCode rfalRunListenModeWorker(void);
  441. #endif /* RFAL_FEATURE_LISTEN_MODE */
  442. #if RFAL_FEATURE_WAKEUP_MODE
  443. static void rfalRunWakeUpModeWorker(void);
  444. static uint16_t rfalWakeUpModeFilter(uint16_t curRef, uint16_t curVal, uint8_t weight);
  445. #endif /* RFAL_FEATURE_WAKEUP_MODE */
  446. static void rfalFIFOStatusUpdate(void);
  447. static void rfalFIFOStatusClear(void);
  448. static bool rfalFIFOStatusIsMissingPar(void);
  449. static bool rfalFIFOStatusIsIncompleteByte(void);
  450. static uint16_t rfalFIFOStatusGetNumBytes(void);
  451. static uint8_t rfalFIFOGetNumIncompleteBits(void);
  452. /*
  453. ******************************************************************************
  454. * GLOBAL FUNCTIONS
  455. ******************************************************************************
  456. */
  457. /*******************************************************************************/
  458. ReturnCode rfalInitialize(void) {
  459. ReturnCode err;
  460. EXIT_ON_ERR(err, st25r3916Initialize());
  461. st25r3916ClearInterrupts();
  462. /* Disable any previous observation mode */
  463. rfalST25R3916ObsModeDisable();
  464. /*******************************************************************************/
  465. /* Apply RF Chip generic initialization */
  466. rfalSetAnalogConfig((RFAL_ANALOG_CONFIG_TECH_CHIP | RFAL_ANALOG_CONFIG_CHIP_INIT));
  467. // TODO:
  468. // I don't want to mess with config table ("Default Analog Configuration for Chip-Specific Reset", rfal_analogConfigTbl.h)
  469. // so with every rfalSetAnalogConfig((RFAL_ANALOG_CONFIG_CHIP_INIT)) currently we need to clear pulldown bits
  470. // luckily for us this is done only here
  471. // disable pulldowns
  472. st25r3916ClrRegisterBits(
  473. ST25R3916_REG_IO_CONF2,
  474. (ST25R3916_REG_IO_CONF2_miso_pd1 | ST25R3916_REG_IO_CONF2_miso_pd2));
  475. /*******************************************************************************/
  476. /* Enable External Field Detector as: Automatics */
  477. st25r3916ChangeRegisterBits(
  478. ST25R3916_REG_OP_CONTROL,
  479. ST25R3916_REG_OP_CONTROL_en_fd_mask,
  480. ST25R3916_REG_OP_CONTROL_en_fd_auto_efd);
  481. /* Clear FIFO status local copy */
  482. rfalFIFOStatusClear();
  483. /*******************************************************************************/
  484. gRFAL.state = RFAL_STATE_INIT;
  485. gRFAL.mode = RFAL_MODE_NONE;
  486. gRFAL.field = false;
  487. /* Set RFAL default configs */
  488. gRFAL.conf.obsvModeRx = RFAL_OBSMODE_DISABLE;
  489. gRFAL.conf.obsvModeTx = RFAL_OBSMODE_DISABLE;
  490. gRFAL.conf.eHandling = RFAL_ERRORHANDLING_NONE;
  491. /* Transceive set to IDLE */
  492. gRFAL.TxRx.lastState = RFAL_TXRX_STATE_IDLE;
  493. gRFAL.TxRx.state = RFAL_TXRX_STATE_IDLE;
  494. /* Disable all timings */
  495. gRFAL.timings.FDTListen = RFAL_TIMING_NONE;
  496. gRFAL.timings.FDTPoll = RFAL_TIMING_NONE;
  497. gRFAL.timings.GT = RFAL_TIMING_NONE;
  498. gRFAL.timings.nTRFW = 0U;
  499. /* Destroy any previous pending timers */
  500. rfalTimerDestroy(gRFAL.tmr.GT);
  501. rfalTimerDestroy(gRFAL.tmr.txRx);
  502. rfalTimerDestroy(gRFAL.tmr.RXE);
  503. gRFAL.tmr.GT = RFAL_TIMING_NONE;
  504. gRFAL.tmr.txRx = RFAL_TIMING_NONE;
  505. gRFAL.tmr.RXE = RFAL_TIMING_NONE;
  506. gRFAL.callbacks.preTxRx = NULL;
  507. gRFAL.callbacks.postTxRx = NULL;
  508. gRFAL.callbacks.state_changed_cb = NULL;
  509. gRFAL.callbacks.ctx = NULL;
  510. #if RFAL_FEATURE_NFCV
  511. /* Initialize NFC-V Data */
  512. gRFAL.nfcvData.ignoreBits = 0;
  513. #endif /* RFAL_FEATURE_NFCV */
  514. #if RFAL_FEATURE_LISTEN_MODE
  515. /* Initialize Listen Mode */
  516. gRFAL.Lm.state = RFAL_LM_STATE_NOT_INIT;
  517. gRFAL.Lm.brDetected = RFAL_BR_KEEP;
  518. gRFAL.Lm.iniFlag = false;
  519. #endif /* RFAL_FEATURE_LISTEN_MODE */
  520. #if RFAL_FEATURE_WAKEUP_MODE
  521. /* Initialize Wake-Up Mode */
  522. gRFAL.wum.state = RFAL_WUM_STATE_NOT_INIT;
  523. #endif /* RFAL_FEATURE_WAKEUP_MODE */
  524. #if RFAL_FEATURE_LOWPOWER_MODE
  525. /* Initialize Low Power Mode */
  526. gRFAL.lpm.isRunning = false;
  527. #endif /* RFAL_FEATURE_LOWPOWER_MODE */
  528. /*******************************************************************************/
  529. /* Perform Automatic Calibration (if configured to do so). *
  530. * Registers set by rfalSetAnalogConfig will tell rfalCalibrate what to perform*/
  531. rfalCalibrate();
  532. return ERR_NONE;
  533. }
  534. /*******************************************************************************/
  535. ReturnCode rfalCalibrate(void) {
  536. uint16_t resValue;
  537. /* Check if RFAL is not initialized */
  538. if(gRFAL.state == RFAL_STATE_IDLE) {
  539. return ERR_WRONG_STATE;
  540. }
  541. /*******************************************************************************/
  542. /* Perform ST25R3916 regulators and antenna calibration */
  543. /*******************************************************************************/
  544. /* Automatic regulator adjustment only performed if not set manually on Analog Configs */
  545. if(st25r3916CheckReg(
  546. ST25R3916_REG_REGULATOR_CONTROL, ST25R3916_REG_REGULATOR_CONTROL_reg_s, 0x00)) {
  547. /* Adjust the regulators so that Antenna Calibrate has better Regulator values */
  548. st25r3916AdjustRegulators(&resValue);
  549. }
  550. return ERR_NONE;
  551. }
  552. /*******************************************************************************/
  553. ReturnCode rfalAdjustRegulators(uint16_t* result) {
  554. return st25r3916AdjustRegulators(result);
  555. }
  556. /*******************************************************************************/
  557. void rfalSetUpperLayerCallback(rfalUpperLayerCallback pFunc) {
  558. st25r3916IRQCallbackSet(pFunc);
  559. }
  560. /*******************************************************************************/
  561. void rfalSetPreTxRxCallback(rfalPreTxRxCallback pFunc) {
  562. gRFAL.callbacks.preTxRx = pFunc;
  563. }
  564. /*******************************************************************************/
  565. void rfalSetPostTxRxCallback(rfalPostTxRxCallback pFunc) {
  566. gRFAL.callbacks.postTxRx = pFunc;
  567. }
  568. void rfal_set_state_changed_callback(RfalStateChangedCallback callback) {
  569. gRFAL.callbacks.state_changed_cb = callback;
  570. }
  571. void rfal_set_callback_context(void* context) {
  572. gRFAL.callbacks.ctx = context;
  573. }
  574. /*******************************************************************************/
  575. ReturnCode rfalDeinitialize(void) {
  576. /* Deinitialize chip */
  577. st25r3916Deinitialize();
  578. /* Set Analog configurations for deinitialization */
  579. rfalSetAnalogConfig((RFAL_ANALOG_CONFIG_TECH_CHIP | RFAL_ANALOG_CONFIG_CHIP_DEINIT));
  580. gRFAL.state = RFAL_STATE_IDLE;
  581. return ERR_NONE;
  582. }
  583. /*******************************************************************************/
  584. void rfalSetObsvMode(uint8_t txMode, uint8_t rxMode) {
  585. gRFAL.conf.obsvModeTx = txMode;
  586. gRFAL.conf.obsvModeRx = rxMode;
  587. }
  588. /*******************************************************************************/
  589. void rfalGetObsvMode(uint8_t* txMode, uint8_t* rxMode) {
  590. if(txMode != NULL) {
  591. *txMode = gRFAL.conf.obsvModeTx;
  592. }
  593. if(rxMode != NULL) {
  594. *rxMode = gRFAL.conf.obsvModeRx;
  595. }
  596. }
  597. /*******************************************************************************/
  598. void rfalDisableObsvMode(void) {
  599. gRFAL.conf.obsvModeTx = RFAL_OBSMODE_DISABLE;
  600. gRFAL.conf.obsvModeRx = RFAL_OBSMODE_DISABLE;
  601. }
  602. /*******************************************************************************/
  603. ReturnCode rfalSetMode(rfalMode mode, rfalBitRate txBR, rfalBitRate rxBR) {
  604. /* Check if RFAL is not initialized */
  605. if(gRFAL.state == RFAL_STATE_IDLE) {
  606. return ERR_WRONG_STATE;
  607. }
  608. /* Check allowed bit rate value */
  609. if((txBR == RFAL_BR_KEEP) || (rxBR == RFAL_BR_KEEP)) {
  610. return ERR_PARAM;
  611. }
  612. switch(mode) {
  613. /*******************************************************************************/
  614. case RFAL_MODE_POLL_NFCA:
  615. /* Disable wake up mode, if set */
  616. st25r3916ClrRegisterBits(ST25R3916_REG_OP_CONTROL, ST25R3916_REG_OP_CONTROL_wu);
  617. /* Enable ISO14443A mode */
  618. st25r3916WriteRegister(ST25R3916_REG_MODE, ST25R3916_REG_MODE_om_iso14443a);
  619. /* Set Analog configurations for this mode and bit rate */
  620. rfalSetAnalogConfig(
  621. (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCA |
  622. RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_TX));
  623. rfalSetAnalogConfig(
  624. (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCA |
  625. RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_RX));
  626. break;
  627. /*******************************************************************************/
  628. case RFAL_MODE_POLL_NFCA_T1T:
  629. /* Disable wake up mode, if set */
  630. st25r3916ClrRegisterBits(ST25R3916_REG_OP_CONTROL, ST25R3916_REG_OP_CONTROL_wu);
  631. /* Enable Topaz mode */
  632. st25r3916WriteRegister(ST25R3916_REG_MODE, ST25R3916_REG_MODE_om_topaz);
  633. /* Set Analog configurations for this mode and bit rate */
  634. rfalSetAnalogConfig(
  635. (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCA |
  636. RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_TX));
  637. rfalSetAnalogConfig(
  638. (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCA |
  639. RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_RX));
  640. break;
  641. /*******************************************************************************/
  642. case RFAL_MODE_POLL_NFCB:
  643. /* Disable wake up mode, if set */
  644. st25r3916ClrRegisterBits(ST25R3916_REG_OP_CONTROL, ST25R3916_REG_OP_CONTROL_wu);
  645. /* Enable ISO14443B mode */
  646. st25r3916WriteRegister(ST25R3916_REG_MODE, ST25R3916_REG_MODE_om_iso14443b);
  647. /* Set the EGT, SOF, EOF and EOF */
  648. st25r3916ChangeRegisterBits(
  649. ST25R3916_REG_ISO14443B_1,
  650. (ST25R3916_REG_ISO14443B_1_egt_mask | ST25R3916_REG_ISO14443B_1_sof_mask |
  651. ST25R3916_REG_ISO14443B_1_eof),
  652. ((0U << ST25R3916_REG_ISO14443B_1_egt_shift) | ST25R3916_REG_ISO14443B_1_sof_0_10etu |
  653. ST25R3916_REG_ISO14443B_1_sof_1_2etu | ST25R3916_REG_ISO14443B_1_eof_10etu));
  654. /* Set the minimum TR1, SOF, EOF and EOF12 */
  655. st25r3916ChangeRegisterBits(
  656. ST25R3916_REG_ISO14443B_2,
  657. (ST25R3916_REG_ISO14443B_2_tr1_mask | ST25R3916_REG_ISO14443B_2_no_sof |
  658. ST25R3916_REG_ISO14443B_2_no_eof),
  659. (ST25R3916_REG_ISO14443B_2_tr1_80fs80fs));
  660. /* Set Analog configurations for this mode and bit rate */
  661. rfalSetAnalogConfig(
  662. (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCB |
  663. RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_TX));
  664. rfalSetAnalogConfig(
  665. (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCB |
  666. RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_RX));
  667. break;
  668. /*******************************************************************************/
  669. case RFAL_MODE_POLL_B_PRIME:
  670. /* Disable wake up mode, if set */
  671. st25r3916ClrRegisterBits(ST25R3916_REG_OP_CONTROL, ST25R3916_REG_OP_CONTROL_wu);
  672. /* Enable ISO14443B mode */
  673. st25r3916WriteRegister(ST25R3916_REG_MODE, ST25R3916_REG_MODE_om_iso14443b);
  674. /* Set the EGT, SOF, EOF and EOF */
  675. st25r3916ChangeRegisterBits(
  676. ST25R3916_REG_ISO14443B_1,
  677. (ST25R3916_REG_ISO14443B_1_egt_mask | ST25R3916_REG_ISO14443B_1_sof_mask |
  678. ST25R3916_REG_ISO14443B_1_eof),
  679. ((0U << ST25R3916_REG_ISO14443B_1_egt_shift) | ST25R3916_REG_ISO14443B_1_sof_0_10etu |
  680. ST25R3916_REG_ISO14443B_1_sof_1_2etu | ST25R3916_REG_ISO14443B_1_eof_10etu));
  681. /* Set the minimum TR1, EOF and EOF12 */
  682. st25r3916ChangeRegisterBits(
  683. ST25R3916_REG_ISO14443B_2,
  684. (ST25R3916_REG_ISO14443B_2_tr1_mask | ST25R3916_REG_ISO14443B_2_no_sof |
  685. ST25R3916_REG_ISO14443B_2_no_eof),
  686. (ST25R3916_REG_ISO14443B_2_tr1_80fs80fs | ST25R3916_REG_ISO14443B_2_no_sof));
  687. /* Set Analog configurations for this mode and bit rate */
  688. rfalSetAnalogConfig(
  689. (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCB |
  690. RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_TX));
  691. rfalSetAnalogConfig(
  692. (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCB |
  693. RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_RX));
  694. break;
  695. /*******************************************************************************/
  696. case RFAL_MODE_POLL_B_CTS:
  697. /* Disable wake up mode, if set */
  698. st25r3916ClrRegisterBits(ST25R3916_REG_OP_CONTROL, ST25R3916_REG_OP_CONTROL_wu);
  699. /* Enable ISO14443B mode */
  700. st25r3916WriteRegister(ST25R3916_REG_MODE, ST25R3916_REG_MODE_om_iso14443b);
  701. /* Set the EGT, SOF, EOF and EOF */
  702. st25r3916ChangeRegisterBits(
  703. ST25R3916_REG_ISO14443B_1,
  704. (ST25R3916_REG_ISO14443B_1_egt_mask | ST25R3916_REG_ISO14443B_1_sof_mask |
  705. ST25R3916_REG_ISO14443B_1_eof),
  706. ((0U << ST25R3916_REG_ISO14443B_1_egt_shift) | ST25R3916_REG_ISO14443B_1_sof_0_10etu |
  707. ST25R3916_REG_ISO14443B_1_sof_1_2etu | ST25R3916_REG_ISO14443B_1_eof_10etu));
  708. /* Set the minimum TR1, clear SOF, EOF and EOF12 */
  709. st25r3916ChangeRegisterBits(
  710. ST25R3916_REG_ISO14443B_2,
  711. (ST25R3916_REG_ISO14443B_2_tr1_mask | ST25R3916_REG_ISO14443B_2_no_sof |
  712. ST25R3916_REG_ISO14443B_2_no_eof),
  713. (ST25R3916_REG_ISO14443B_2_tr1_80fs80fs | ST25R3916_REG_ISO14443B_2_no_sof |
  714. ST25R3916_REG_ISO14443B_2_no_eof));
  715. /* Set Analog configurations for this mode and bit rate */
  716. rfalSetAnalogConfig(
  717. (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCB |
  718. RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_TX));
  719. rfalSetAnalogConfig(
  720. (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCB |
  721. RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_RX));
  722. break;
  723. /*******************************************************************************/
  724. case RFAL_MODE_POLL_NFCF:
  725. /* Disable wake up mode, if set */
  726. st25r3916ClrRegisterBits(ST25R3916_REG_OP_CONTROL, ST25R3916_REG_OP_CONTROL_wu);
  727. /* Enable FeliCa mode */
  728. st25r3916WriteRegister(ST25R3916_REG_MODE, ST25R3916_REG_MODE_om_felica);
  729. /* Set Analog configurations for this mode and bit rate */
  730. rfalSetAnalogConfig(
  731. (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCF |
  732. RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_TX));
  733. rfalSetAnalogConfig(
  734. (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCF |
  735. RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_RX));
  736. break;
  737. /*******************************************************************************/
  738. case RFAL_MODE_POLL_NFCV:
  739. case RFAL_MODE_POLL_PICOPASS:
  740. #if !RFAL_FEATURE_NFCV
  741. return ERR_DISABLED;
  742. #else
  743. /* Disable wake up mode, if set */
  744. st25r3916ClrRegisterBits(ST25R3916_REG_OP_CONTROL, ST25R3916_REG_OP_CONTROL_wu);
  745. /* Set Analog configurations for this mode and bit rate */
  746. rfalSetAnalogConfig(
  747. (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCV |
  748. RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_TX));
  749. rfalSetAnalogConfig(
  750. (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCV |
  751. RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_RX));
  752. break;
  753. #endif /* RFAL_FEATURE_NFCV */
  754. /*******************************************************************************/
  755. case RFAL_MODE_POLL_ACTIVE_P2P:
  756. /* Set NFCIP1 active communication Initiator mode and Automatic Response RF Collision Avoidance to always after EOF */
  757. st25r3916WriteRegister(
  758. ST25R3916_REG_MODE,
  759. (ST25R3916_REG_MODE_targ_init | ST25R3916_REG_MODE_om_nfc |
  760. ST25R3916_REG_MODE_nfc_ar_eof));
  761. /* External Field Detector enabled as Automatics on rfalInitialize() */
  762. /* Set NRT to start at end of TX (own) field */
  763. st25r3916ChangeRegisterBits(
  764. ST25R3916_REG_TIMER_EMV_CONTROL,
  765. ST25R3916_REG_TIMER_EMV_CONTROL_nrt_nfc,
  766. ST25R3916_REG_TIMER_EMV_CONTROL_nrt_nfc_off);
  767. /* Set GPT to start after end of TX, as GPT is used in active communication mode to timeout the field switching off */
  768. /* The field is turned off 37.76us after the end of the transmission Trfw */
  769. st25r3916SetStartGPTimer(
  770. (uint16_t)rfalConv1fcTo8fc(RFAL_AP2P_FIELDOFF_TRFW),
  771. ST25R3916_REG_TIMER_EMV_CONTROL_gptc_etx_nfc);
  772. /* Set PPon2 timer with the max time between our field Off and other peer field On : Tadt + (n x Trfw) */
  773. st25r3916WriteRegister(
  774. ST25R3916_REG_PPON2, (uint8_t)rfalConv1fcTo64fc(RFAL_AP2P_FIELDON_TADTTRFW));
  775. /* Set Analog configurations for this mode and bit rate */
  776. rfalSetAnalogConfig(
  777. (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_AP2P |
  778. RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_TX));
  779. rfalSetAnalogConfig(
  780. (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_AP2P |
  781. RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_RX));
  782. break;
  783. /*******************************************************************************/
  784. case RFAL_MODE_LISTEN_ACTIVE_P2P:
  785. /* Set NFCIP1 active communication Target mode and Automatic Response RF Collision Avoidance to always after EOF */
  786. st25r3916WriteRegister(
  787. ST25R3916_REG_MODE,
  788. (ST25R3916_REG_MODE_targ_targ | ST25R3916_REG_MODE_om_targ_nfcip |
  789. ST25R3916_REG_MODE_nfc_ar_eof));
  790. /* Set TARFG: 0 (75us+0ms=75us), as Target no Guard time needed */
  791. st25r3916WriteRegister(ST25R3916_REG_FIELD_ON_GT, 0U);
  792. /* External Field Detector enabled as Automatics on rfalInitialize() */
  793. /* Set NRT to start at end of TX (own) field */
  794. st25r3916ChangeRegisterBits(
  795. ST25R3916_REG_TIMER_EMV_CONTROL,
  796. ST25R3916_REG_TIMER_EMV_CONTROL_nrt_nfc,
  797. ST25R3916_REG_TIMER_EMV_CONTROL_nrt_nfc_off);
  798. /* Set GPT to start after end of TX, as GPT is used in active communication mode to timeout the field switching off */
  799. /* The field is turned off 37.76us after the end of the transmission Trfw */
  800. st25r3916SetStartGPTimer(
  801. (uint16_t)rfalConv1fcTo8fc(RFAL_AP2P_FIELDOFF_TRFW),
  802. ST25R3916_REG_TIMER_EMV_CONTROL_gptc_etx_nfc);
  803. /* Set PPon2 timer with the max time between our field Off and other peer field On : Tadt + (n x Trfw) */
  804. st25r3916WriteRegister(
  805. ST25R3916_REG_PPON2, (uint8_t)rfalConv1fcTo64fc(RFAL_AP2P_FIELDON_TADTTRFW));
  806. /* Set Analog configurations for this mode and bit rate */
  807. rfalSetAnalogConfig(
  808. (RFAL_ANALOG_CONFIG_LISTEN | RFAL_ANALOG_CONFIG_TECH_AP2P |
  809. RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_TX));
  810. rfalSetAnalogConfig(
  811. (RFAL_ANALOG_CONFIG_LISTEN | RFAL_ANALOG_CONFIG_TECH_AP2P |
  812. RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_RX));
  813. break;
  814. /*******************************************************************************/
  815. case RFAL_MODE_LISTEN_NFCA:
  816. /* Disable wake up mode, if set */
  817. st25r3916ClrRegisterBits(ST25R3916_REG_OP_CONTROL, ST25R3916_REG_OP_CONTROL_wu);
  818. /* Enable Passive Target NFC-A mode, disable any Collision Avoidance */
  819. st25r3916WriteRegister(
  820. ST25R3916_REG_MODE,
  821. (ST25R3916_REG_MODE_targ | ST25R3916_REG_MODE_om_targ_nfca |
  822. ST25R3916_REG_MODE_nfc_ar_off));
  823. /* Set Analog configurations for this mode */
  824. rfalSetAnalogConfig(
  825. (RFAL_ANALOG_CONFIG_LISTEN | RFAL_ANALOG_CONFIG_TECH_NFCA |
  826. RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_TX));
  827. rfalSetAnalogConfig(
  828. (RFAL_ANALOG_CONFIG_LISTEN | RFAL_ANALOG_CONFIG_TECH_NFCA |
  829. RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_RX));
  830. break;
  831. /*******************************************************************************/
  832. case RFAL_MODE_LISTEN_NFCF:
  833. /* Disable wake up mode, if set */
  834. st25r3916ClrRegisterBits(ST25R3916_REG_OP_CONTROL, ST25R3916_REG_OP_CONTROL_wu);
  835. /* Enable Passive Target NFC-F mode, disable any Collision Avoidance */
  836. st25r3916WriteRegister(
  837. ST25R3916_REG_MODE,
  838. (ST25R3916_REG_MODE_targ | ST25R3916_REG_MODE_om_targ_nfcf |
  839. ST25R3916_REG_MODE_nfc_ar_off));
  840. /* Set Analog configurations for this mode */
  841. rfalSetAnalogConfig(
  842. (RFAL_ANALOG_CONFIG_LISTEN | RFAL_ANALOG_CONFIG_TECH_NFCF |
  843. RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_TX));
  844. rfalSetAnalogConfig(
  845. (RFAL_ANALOG_CONFIG_LISTEN | RFAL_ANALOG_CONFIG_TECH_NFCF |
  846. RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_RX));
  847. break;
  848. /*******************************************************************************/
  849. case RFAL_MODE_LISTEN_NFCB:
  850. return ERR_NOTSUPP;
  851. /*******************************************************************************/
  852. default:
  853. return ERR_NOT_IMPLEMENTED;
  854. }
  855. /* Set state as STATE_MODE_SET only if not initialized yet (PSL) */
  856. gRFAL.state = ((gRFAL.state < RFAL_STATE_MODE_SET) ? RFAL_STATE_MODE_SET : gRFAL.state);
  857. gRFAL.mode = mode;
  858. /* Apply the given bit rate */
  859. return rfalSetBitRate(txBR, rxBR);
  860. }
  861. /*******************************************************************************/
  862. rfalMode rfalGetMode(void) {
  863. return gRFAL.mode;
  864. }
  865. /*******************************************************************************/
  866. ReturnCode rfalSetBitRate(rfalBitRate txBR, rfalBitRate rxBR) {
  867. ReturnCode ret;
  868. /* Check if RFAL is not initialized */
  869. if(gRFAL.state == RFAL_STATE_IDLE) {
  870. return ERR_WRONG_STATE;
  871. }
  872. /* Store the new Bit Rates */
  873. gRFAL.txBR = ((txBR == RFAL_BR_KEEP) ? gRFAL.txBR : txBR);
  874. gRFAL.rxBR = ((rxBR == RFAL_BR_KEEP) ? gRFAL.rxBR : rxBR);
  875. /* Update the bitrate reg if not in NFCV mode (streaming) */
  876. if((RFAL_MODE_POLL_NFCV != gRFAL.mode) && (RFAL_MODE_POLL_PICOPASS != gRFAL.mode)) {
  877. /* Set bit rate register */
  878. EXIT_ON_ERR(ret, st25r3916SetBitrate((uint8_t)gRFAL.txBR, (uint8_t)gRFAL.rxBR));
  879. }
  880. switch(gRFAL.mode) {
  881. /*******************************************************************************/
  882. case RFAL_MODE_POLL_NFCA:
  883. case RFAL_MODE_POLL_NFCA_T1T:
  884. /* Set Analog configurations for this bit rate */
  885. rfalSetAnalogConfig((RFAL_ANALOG_CONFIG_TECH_CHIP | RFAL_ANALOG_CONFIG_CHIP_POLL_COMMON));
  886. rfalSetAnalogConfig( (rfalAnalogConfigId)(RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCA | rfalConvBR2ACBR(gRFAL.txBR) | RFAL_ANALOG_CONFIG_TX ) );
  887. rfalSetAnalogConfig( (rfalAnalogConfigId)(RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCA | rfalConvBR2ACBR(gRFAL.rxBR) | RFAL_ANALOG_CONFIG_RX ) );
  888. break;
  889. /*******************************************************************************/
  890. case RFAL_MODE_POLL_NFCB:
  891. case RFAL_MODE_POLL_B_PRIME:
  892. case RFAL_MODE_POLL_B_CTS:
  893. /* Set Analog configurations for this bit rate */
  894. rfalSetAnalogConfig((RFAL_ANALOG_CONFIG_TECH_CHIP | RFAL_ANALOG_CONFIG_CHIP_POLL_COMMON));
  895. rfalSetAnalogConfig( (rfalAnalogConfigId)(RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCB | rfalConvBR2ACBR(gRFAL.txBR) | RFAL_ANALOG_CONFIG_TX ) );
  896. rfalSetAnalogConfig( (rfalAnalogConfigId)(RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCB | rfalConvBR2ACBR(gRFAL.rxBR) | RFAL_ANALOG_CONFIG_RX ) );
  897. break;
  898. /*******************************************************************************/
  899. case RFAL_MODE_POLL_NFCF:
  900. /* Set Analog configurations for this bit rate */
  901. rfalSetAnalogConfig((RFAL_ANALOG_CONFIG_TECH_CHIP | RFAL_ANALOG_CONFIG_CHIP_POLL_COMMON));
  902. rfalSetAnalogConfig( (rfalAnalogConfigId)(RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCF | rfalConvBR2ACBR(gRFAL.txBR) | RFAL_ANALOG_CONFIG_TX ) );
  903. rfalSetAnalogConfig( (rfalAnalogConfigId)(RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCF | rfalConvBR2ACBR(gRFAL.rxBR) | RFAL_ANALOG_CONFIG_RX ) );
  904. break;
  905. /*******************************************************************************/
  906. case RFAL_MODE_POLL_NFCV:
  907. case RFAL_MODE_POLL_PICOPASS:
  908. #if !RFAL_FEATURE_NFCV
  909. return ERR_DISABLED;
  910. #else
  911. if(((gRFAL.rxBR != RFAL_BR_26p48) && (gRFAL.rxBR != RFAL_BR_52p97)) ||
  912. ((gRFAL.txBR != RFAL_BR_1p66) && (gRFAL.txBR != RFAL_BR_26p48))) {
  913. return ERR_PARAM;
  914. }
  915. {
  916. const struct iso15693StreamConfig* isoStreamConfig;
  917. struct st25r3916StreamConfig streamConf;
  918. iso15693PhyConfig_t config;
  919. config.coding =
  920. ((gRFAL.txBR == RFAL_BR_1p66) ? ISO15693_VCD_CODING_1_256 :
  921. ISO15693_VCD_CODING_1_4);
  922. switch(gRFAL.rxBR) {
  923. case RFAL_BR_52p97:
  924. config.speedMode = 1;
  925. break;
  926. default:
  927. config.speedMode = 0;
  928. break;
  929. }
  930. iso15693PhyConfigure(&config, &isoStreamConfig);
  931. /* MISRA 11.3 - Cannot point directly into different object type, copy to local var */
  932. streamConf.din = isoStreamConfig->din;
  933. streamConf.dout = isoStreamConfig->dout;
  934. streamConf.report_period_length = isoStreamConfig->report_period_length;
  935. streamConf.useBPSK = isoStreamConfig->useBPSK;
  936. st25r3916StreamConfigure(&streamConf);
  937. }
  938. /* Set Analog configurations for this bit rate */
  939. rfalSetAnalogConfig((RFAL_ANALOG_CONFIG_TECH_CHIP | RFAL_ANALOG_CONFIG_CHIP_POLL_COMMON));
  940. rfalSetAnalogConfig( (rfalAnalogConfigId)(RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCV | rfalConvBR2ACBR(gRFAL.txBR) | RFAL_ANALOG_CONFIG_TX ) );
  941. rfalSetAnalogConfig( (rfalAnalogConfigId)(RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCV | rfalConvBR2ACBR(gRFAL.rxBR) | RFAL_ANALOG_CONFIG_RX ) );
  942. break;
  943. #endif /* RFAL_FEATURE_NFCV */
  944. /*******************************************************************************/
  945. case RFAL_MODE_POLL_ACTIVE_P2P:
  946. /* Set Analog configurations for this bit rate */
  947. rfalSetAnalogConfig((RFAL_ANALOG_CONFIG_TECH_CHIP | RFAL_ANALOG_CONFIG_CHIP_POLL_COMMON));
  948. rfalSetAnalogConfig( (rfalAnalogConfigId)(RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_AP2P | rfalConvBR2ACBR(gRFAL.txBR) | RFAL_ANALOG_CONFIG_TX ) );
  949. rfalSetAnalogConfig( (rfalAnalogConfigId)(RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_AP2P | rfalConvBR2ACBR(gRFAL.rxBR) | RFAL_ANALOG_CONFIG_RX ) );
  950. break;
  951. /*******************************************************************************/
  952. case RFAL_MODE_LISTEN_ACTIVE_P2P:
  953. /* Set Analog configurations for this bit rate */
  954. rfalSetAnalogConfig(
  955. (RFAL_ANALOG_CONFIG_TECH_CHIP | RFAL_ANALOG_CONFIG_CHIP_LISTEN_COMMON));
  956. rfalSetAnalogConfig( (rfalAnalogConfigId)(RFAL_ANALOG_CONFIG_LISTEN | RFAL_ANALOG_CONFIG_TECH_AP2P | rfalConvBR2ACBR(gRFAL.txBR) | RFAL_ANALOG_CONFIG_TX ) );
  957. rfalSetAnalogConfig( (rfalAnalogConfigId)(RFAL_ANALOG_CONFIG_LISTEN | RFAL_ANALOG_CONFIG_TECH_AP2P | rfalConvBR2ACBR(gRFAL.rxBR) | RFAL_ANALOG_CONFIG_RX ) );
  958. break;
  959. /*******************************************************************************/
  960. case RFAL_MODE_LISTEN_NFCA:
  961. /* Set Analog configurations for this bit rate */
  962. rfalSetAnalogConfig(
  963. (RFAL_ANALOG_CONFIG_TECH_CHIP | RFAL_ANALOG_CONFIG_CHIP_LISTEN_COMMON));
  964. rfalSetAnalogConfig( (rfalAnalogConfigId)(RFAL_ANALOG_CONFIG_LISTEN | RFAL_ANALOG_CONFIG_TECH_NFCA | rfalConvBR2ACBR(gRFAL.txBR) | RFAL_ANALOG_CONFIG_TX ) );
  965. rfalSetAnalogConfig( (rfalAnalogConfigId)(RFAL_ANALOG_CONFIG_LISTEN | RFAL_ANALOG_CONFIG_TECH_NFCA | rfalConvBR2ACBR(gRFAL.rxBR) | RFAL_ANALOG_CONFIG_RX ) );
  966. break;
  967. /*******************************************************************************/
  968. case RFAL_MODE_LISTEN_NFCF:
  969. /* Set Analog configurations for this bit rate */
  970. rfalSetAnalogConfig(
  971. (RFAL_ANALOG_CONFIG_TECH_CHIP | RFAL_ANALOG_CONFIG_CHIP_LISTEN_COMMON));
  972. rfalSetAnalogConfig( (rfalAnalogConfigId)(RFAL_ANALOG_CONFIG_LISTEN | RFAL_ANALOG_CONFIG_TECH_NFCF | rfalConvBR2ACBR(gRFAL.txBR) | RFAL_ANALOG_CONFIG_TX ) );
  973. rfalSetAnalogConfig( (rfalAnalogConfigId)(RFAL_ANALOG_CONFIG_LISTEN | RFAL_ANALOG_CONFIG_TECH_NFCF | rfalConvBR2ACBR(gRFAL.rxBR) | RFAL_ANALOG_CONFIG_RX ) );
  974. break;
  975. /*******************************************************************************/
  976. case RFAL_MODE_LISTEN_NFCB:
  977. case RFAL_MODE_NONE:
  978. return ERR_WRONG_STATE;
  979. /*******************************************************************************/
  980. default:
  981. return ERR_NOT_IMPLEMENTED;
  982. }
  983. return ERR_NONE;
  984. }
  985. /*******************************************************************************/
  986. ReturnCode rfalGetBitRate(rfalBitRate* txBR, rfalBitRate* rxBR) {
  987. if((gRFAL.state == RFAL_STATE_IDLE) || (gRFAL.mode == RFAL_MODE_NONE)) {
  988. return ERR_WRONG_STATE;
  989. }
  990. if(txBR != NULL) {
  991. *txBR = gRFAL.txBR;
  992. }
  993. if(rxBR != NULL) {
  994. *rxBR = gRFAL.rxBR;
  995. }
  996. return ERR_NONE;
  997. }
  998. /*******************************************************************************/
  999. void rfalSetErrorHandling(rfalEHandling eHandling) {
  1000. switch(eHandling) {
  1001. case RFAL_ERRORHANDLING_NFC:
  1002. case RFAL_ERRORHANDLING_NONE:
  1003. st25r3916ClrRegisterBits(ST25R3916_REG_EMD_SUP_CONF, ST25R3916_REG_EMD_SUP_CONF_emd_emv);
  1004. break;
  1005. case RFAL_ERRORHANDLING_EMVCO:
  1006. /* MISRA 16.4: no empty default statement (in case RFAL_SW_EMD is defined) */
  1007. #ifndef RFAL_SW_EMD
  1008. st25r3916ModifyRegister(
  1009. ST25R3916_REG_EMD_SUP_CONF,
  1010. (ST25R3916_REG_EMD_SUP_CONF_emd_emv | ST25R3916_REG_EMD_SUP_CONF_emd_thld_mask),
  1011. (ST25R3916_REG_EMD_SUP_CONF_emd_emv_on | RFAL_EMVCO_RX_MAXLEN));
  1012. #endif /* RFAL_SW_EMD */
  1013. break;
  1014. default:
  1015. /* MISRA 16.4: no empty default statement (a comment being enough) */
  1016. break;
  1017. }
  1018. gRFAL.conf.eHandling = eHandling;
  1019. }
  1020. /*******************************************************************************/
  1021. rfalEHandling rfalGetErrorHandling(void) {
  1022. return gRFAL.conf.eHandling;
  1023. }
  1024. /*******************************************************************************/
  1025. void rfalSetFDTPoll(uint32_t FDTPoll) {
  1026. gRFAL.timings.FDTPoll = MIN(FDTPoll, RFAL_ST25R3916_GPT_MAX_1FC);
  1027. }
  1028. /*******************************************************************************/
  1029. uint32_t rfalGetFDTPoll(void) {
  1030. return gRFAL.timings.FDTPoll;
  1031. }
  1032. /*******************************************************************************/
  1033. void rfalSetFDTListen(uint32_t FDTListen) {
  1034. gRFAL.timings.FDTListen = MIN(FDTListen, RFAL_ST25R3916_MRT_MAX_1FC);
  1035. }
  1036. /*******************************************************************************/
  1037. uint32_t rfalGetFDTListen(void) {
  1038. return gRFAL.timings.FDTListen;
  1039. }
  1040. /*******************************************************************************/
  1041. void rfalSetGT(uint32_t GT) {
  1042. gRFAL.timings.GT = MIN(GT, RFAL_ST25R3916_GT_MAX_1FC);
  1043. }
  1044. /*******************************************************************************/
  1045. uint32_t rfalGetGT(void) {
  1046. return gRFAL.timings.GT;
  1047. }
  1048. /*******************************************************************************/
  1049. bool rfalIsGTExpired(void) {
  1050. if(gRFAL.tmr.GT != RFAL_TIMING_NONE) {
  1051. if(!rfalTimerisExpired(gRFAL.tmr.GT)) {
  1052. return false;
  1053. }
  1054. }
  1055. return true;
  1056. }
  1057. /*******************************************************************************/
  1058. ReturnCode rfalFieldOnAndStartGT(void) {
  1059. ReturnCode ret;
  1060. /* Check if RFAL has been initialized (Oscillator should be running) and also
  1061. * if a direct register access has been performed and left the Oscillator Off */
  1062. if(!st25r3916IsOscOn() || (gRFAL.state < RFAL_STATE_INIT)) {
  1063. return ERR_WRONG_STATE;
  1064. }
  1065. ret = ERR_NONE;
  1066. /* Set Analog configurations for Field On event */
  1067. rfalSetAnalogConfig((RFAL_ANALOG_CONFIG_TECH_CHIP | RFAL_ANALOG_CONFIG_CHIP_FIELD_ON));
  1068. /*******************************************************************************/
  1069. /* Perform collision avoidance and turn field On if not already On */
  1070. if(!st25r3916IsTxEnabled() || !gRFAL.field) {
  1071. /* Set TARFG: 0 (75us+0ms=75us), GT is fulfilled using a SW timer */
  1072. st25r3916WriteRegister(ST25R3916_REG_FIELD_ON_GT, 0U);
  1073. /* Use Thresholds set by AnalogConfig */
  1074. ret = st25r3916PerformCollisionAvoidance(
  1075. ST25R3916_CMD_INITIAL_RF_COLLISION,
  1076. ST25R3916_THRESHOLD_DO_NOT_SET,
  1077. ST25R3916_THRESHOLD_DO_NOT_SET,
  1078. gRFAL.timings.nTRFW);
  1079. /* n * TRFW timing shall vary Activity 2.1 3.3.1.1 */
  1080. gRFAL.timings.nTRFW = rfalGennTRFW(gRFAL.timings.nTRFW);
  1081. gRFAL.field = st25r3916IsTxEnabled(); //(ret == ERR_NONE);
  1082. /* Only turn on Receiver and Transmitter if field was successfully turned On */
  1083. if(gRFAL.field) {
  1084. st25r3916TxRxOn(); /* Enable Tx and Rx (Tx is already On)*/
  1085. }
  1086. }
  1087. /*******************************************************************************/
  1088. /* Start GT timer in case the GT value is set */
  1089. if((gRFAL.timings.GT != RFAL_TIMING_NONE)) {
  1090. /* Ensure that a SW timer doesn't have a lower value then the minimum */
  1091. rfalTimerStart(
  1092. gRFAL.tmr.GT, rfalConv1fcToMs(MAX((gRFAL.timings.GT), RFAL_ST25R3916_GT_MIN_1FC)));
  1093. }
  1094. return ret;
  1095. }
  1096. /*******************************************************************************/
  1097. ReturnCode rfalFieldOff(void) {
  1098. /* Check whether a TxRx is not yet finished */
  1099. if(gRFAL.TxRx.state != RFAL_TXRX_STATE_IDLE) {
  1100. rfalCleanupTransceive();
  1101. }
  1102. /* Disable Tx and Rx */
  1103. st25r3916TxRxOff();
  1104. /* Set Analog configurations for Field Off event */
  1105. rfalSetAnalogConfig((RFAL_ANALOG_CONFIG_TECH_CHIP | RFAL_ANALOG_CONFIG_CHIP_FIELD_OFF));
  1106. gRFAL.field = false;
  1107. return ERR_NONE;
  1108. }
  1109. /*******************************************************************************/
  1110. ReturnCode rfalStartTransceive(const rfalTransceiveContext* ctx) {
  1111. uint32_t FxTAdj; /* FWT or FDT adjustment calculation */
  1112. /* Check for valid parameters */
  1113. if(ctx == NULL) {
  1114. return ERR_PARAM;
  1115. }
  1116. /* Ensure that RFAL is already Initialized and the mode has been set */
  1117. if((gRFAL.state >= RFAL_STATE_MODE_SET) /*&& (gRFAL.TxRx.state == RFAL_TXRX_STATE_INIT )*/) {
  1118. /*******************************************************************************/
  1119. /* Check whether the field is already On, otherwise no TXE will be received */
  1120. if(!st25r3916IsTxEnabled() &&
  1121. (!rfalIsModePassiveListen(gRFAL.mode) && (ctx->txBuf != NULL))) {
  1122. return ERR_WRONG_STATE;
  1123. }
  1124. gRFAL.TxRx.ctx = *ctx;
  1125. /*******************************************************************************/
  1126. if(gRFAL.timings.FDTListen != RFAL_TIMING_NONE) {
  1127. /* Calculate MRT adjustment accordingly to the current mode */
  1128. FxTAdj = RFAL_FDT_LISTEN_MRT_ADJUSTMENT;
  1129. if(gRFAL.mode == RFAL_MODE_POLL_NFCA) {
  1130. FxTAdj += (uint32_t)RFAL_FDT_LISTEN_A_ADJUSTMENT;
  1131. }
  1132. if(gRFAL.mode == RFAL_MODE_POLL_NFCA_T1T) {
  1133. FxTAdj += (uint32_t)RFAL_FDT_LISTEN_A_ADJUSTMENT;
  1134. }
  1135. if(gRFAL.mode == RFAL_MODE_POLL_NFCB) {
  1136. FxTAdj += (uint32_t)RFAL_FDT_LISTEN_B_ADJUSTMENT;
  1137. }
  1138. if(gRFAL.mode == RFAL_MODE_POLL_NFCV) {
  1139. FxTAdj += (uint32_t)RFAL_FDT_LISTEN_V_ADJUSTMENT;
  1140. }
  1141. /* Ensure that MRT is using 64/fc steps */
  1142. st25r3916ClrRegisterBits(
  1143. ST25R3916_REG_TIMER_EMV_CONTROL, ST25R3916_REG_TIMER_EMV_CONTROL_mrt_step);
  1144. /* If Correlator is being used further adjustment is required for NFCB */
  1145. if((st25r3916CheckReg(ST25R3916_REG_AUX, ST25R3916_REG_AUX_dis_corr, 0x00U)) &&
  1146. (gRFAL.mode == RFAL_MODE_POLL_NFCB)) {
  1147. FxTAdj += (uint32_t)
  1148. RFAL_FDT_LISTEN_B_ADJT_CORR; /* Reduce FDT(Listen) */
  1149. st25r3916SetRegisterBits(
  1150. ST25R3916_REG_CORR_CONF1,
  1151. ST25R3916_REG_CORR_CONF1_corr_s3); /* Ensure BPSK start to 33 pilot pulses */
  1152. st25r3916ChangeRegisterBits(
  1153. ST25R3916_REG_SUBC_START_TIME,
  1154. ST25R3916_REG_SUBC_START_TIME_sst_mask,
  1155. RFAL_FDT_LISTEN_B_ADJT_CORR_SST); /* Set sst */
  1156. }
  1157. /* Set Minimum FDT(Listen) in which PICC is not allowed to send a response */
  1158. st25r3916WriteRegister(
  1159. ST25R3916_REG_MASK_RX_TIMER,
  1160. (uint8_t)rfalConv1fcTo64fc(
  1161. (FxTAdj > gRFAL.timings.FDTListen) ? RFAL_ST25R3916_MRT_MIN_1FC :
  1162. (gRFAL.timings.FDTListen - FxTAdj)));
  1163. }
  1164. /*******************************************************************************/
  1165. /* FDT Poll will be loaded in rfalPrepareTransceive() once the previous was expired */
  1166. /*******************************************************************************/
  1167. if((gRFAL.TxRx.ctx.fwt != RFAL_FWT_NONE) && (gRFAL.TxRx.ctx.fwt != 0U)) {
  1168. /* Ensure proper timing configuration */
  1169. if(gRFAL.timings.FDTListen >= gRFAL.TxRx.ctx.fwt) {
  1170. return ERR_PARAM;
  1171. }
  1172. FxTAdj = RFAL_FWT_ADJUSTMENT;
  1173. if(gRFAL.mode == RFAL_MODE_POLL_NFCA) {
  1174. FxTAdj += (uint32_t)RFAL_FWT_A_ADJUSTMENT;
  1175. }
  1176. if(gRFAL.mode == RFAL_MODE_POLL_NFCA_T1T) {
  1177. FxTAdj += (uint32_t)RFAL_FWT_A_ADJUSTMENT;
  1178. }
  1179. if(gRFAL.mode == RFAL_MODE_POLL_NFCB) {
  1180. FxTAdj += (uint32_t)RFAL_FWT_B_ADJUSTMENT;
  1181. }
  1182. if((gRFAL.mode == RFAL_MODE_POLL_NFCF) || (gRFAL.mode == RFAL_MODE_POLL_ACTIVE_P2P)) {
  1183. FxTAdj +=
  1184. (uint32_t)((gRFAL.txBR == RFAL_BR_212) ? RFAL_FWT_F_212_ADJUSTMENT : RFAL_FWT_F_424_ADJUSTMENT);
  1185. }
  1186. /* Ensure that the given FWT doesn't exceed NRT maximum */
  1187. gRFAL.TxRx.ctx.fwt = MIN((gRFAL.TxRx.ctx.fwt + FxTAdj), RFAL_ST25R3916_NRT_MAX_1FC);
  1188. /* Set FWT in the NRT */
  1189. st25r3916SetNoResponseTime(rfalConv1fcTo64fc(gRFAL.TxRx.ctx.fwt));
  1190. } else {
  1191. /* Disable NRT, no NRE will be triggered, therefore wait endlessly for Rx */
  1192. st25r3916SetNoResponseTime(RFAL_ST25R3916_NRT_DISABLED);
  1193. }
  1194. gRFAL.state = RFAL_STATE_TXRX;
  1195. gRFAL.TxRx.state = RFAL_TXRX_STATE_TX_IDLE;
  1196. gRFAL.TxRx.status = ERR_BUSY;
  1197. #if RFAL_FEATURE_NFCV
  1198. /*******************************************************************************/
  1199. if((RFAL_MODE_POLL_NFCV == gRFAL.mode) ||
  1200. (RFAL_MODE_POLL_PICOPASS ==
  1201. gRFAL.mode)) { /* Exchange receive buffer with internal buffer */
  1202. gRFAL.nfcvData.origCtx = gRFAL.TxRx.ctx;
  1203. gRFAL.TxRx.ctx.rxBuf =
  1204. ((gRFAL.nfcvData.origCtx.rxBuf != NULL) ? gRFAL.nfcvData.codingBuffer : NULL);
  1205. gRFAL.TxRx.ctx.rxBufLen =
  1206. (uint16_t)rfalConvBytesToBits(sizeof(gRFAL.nfcvData.codingBuffer));
  1207. gRFAL.TxRx.ctx.flags =
  1208. (uint32_t)RFAL_TXRX_FLAGS_CRC_TX_MANUAL | (uint32_t)RFAL_TXRX_FLAGS_CRC_RX_KEEP |
  1209. (uint32_t)RFAL_TXRX_FLAGS_NFCIP1_OFF |
  1210. (uint32_t)(gRFAL.nfcvData.origCtx.flags & (uint32_t)RFAL_TXRX_FLAGS_AGC_OFF) |
  1211. (uint32_t)RFAL_TXRX_FLAGS_PAR_RX_KEEP | (uint32_t)RFAL_TXRX_FLAGS_PAR_TX_NONE;
  1212. /* In NFCV a TxRx with a valid txBuf and txBufSize==0 indicates to send an EOF */
  1213. /* Skip logic below that would go directly into receive */
  1214. if(gRFAL.TxRx.ctx.txBuf != NULL) {
  1215. return ERR_NONE;
  1216. }
  1217. }
  1218. #endif /* RFAL_FEATURE_NFCV */
  1219. /*******************************************************************************/
  1220. /* Check if the Transceive start performing Tx or goes directly to Rx */
  1221. if((gRFAL.TxRx.ctx.txBuf == NULL) || (gRFAL.TxRx.ctx.txBufLen == 0U)) {
  1222. /* Clear FIFO, Clear and Enable the Interrupts */
  1223. rfalPrepareTransceive();
  1224. /* In AP2P check the field status */
  1225. if(rfalIsModeActiveComm(gRFAL.mode)) {
  1226. /* Disable our field upon a Rx reEnable, and start PPON2 manually */
  1227. st25r3916TxOff();
  1228. st25r3916ExecuteCommand(ST25R3916_CMD_START_PPON2_TIMER);
  1229. }
  1230. /* No Tx done, enable the Receiver */
  1231. st25r3916ExecuteCommand(ST25R3916_CMD_UNMASK_RECEIVE_DATA);
  1232. /* Start NRT manually, if FWT = 0 (wait endlessly for Rx) chip will ignore anyhow */
  1233. st25r3916ExecuteCommand(ST25R3916_CMD_START_NO_RESPONSE_TIMER);
  1234. gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_IDLE;
  1235. }
  1236. return ERR_NONE;
  1237. }
  1238. return ERR_WRONG_STATE;
  1239. }
  1240. /*******************************************************************************/
  1241. bool rfalIsTransceiveInTx(void) {
  1242. return (
  1243. (gRFAL.TxRx.state >= RFAL_TXRX_STATE_TX_IDLE) &&
  1244. (gRFAL.TxRx.state < RFAL_TXRX_STATE_RX_IDLE));
  1245. }
  1246. /*******************************************************************************/
  1247. bool rfalIsTransceiveInRx(void) {
  1248. return (gRFAL.TxRx.state >= RFAL_TXRX_STATE_RX_IDLE);
  1249. }
  1250. /*******************************************************************************/
  1251. ReturnCode rfalTransceiveBlockingTx(
  1252. uint8_t* txBuf,
  1253. uint16_t txBufLen,
  1254. uint8_t* rxBuf,
  1255. uint16_t rxBufLen,
  1256. uint16_t* actLen,
  1257. uint32_t flags,
  1258. uint32_t fwt) {
  1259. ReturnCode ret;
  1260. rfalTransceiveContext ctx;
  1261. rfalCreateByteFlagsTxRxContext(ctx, txBuf, txBufLen, rxBuf, rxBufLen, actLen, flags, fwt);
  1262. EXIT_ON_ERR(ret, rfalStartTransceive(&ctx));
  1263. return rfalTransceiveRunBlockingTx();
  1264. }
  1265. ReturnCode rfalTransceiveBitsBlockingTx(
  1266. uint8_t* txBuf,
  1267. uint16_t txBufLen,
  1268. uint8_t* rxBuf,
  1269. uint16_t rxBufLen,
  1270. uint16_t* actLen,
  1271. uint32_t flags,
  1272. uint32_t fwt) {
  1273. ReturnCode ret;
  1274. rfalTransceiveContext ctx = {
  1275. .rxBuf = rxBuf,
  1276. .rxBufLen = rxBufLen,
  1277. .rxRcvdLen = actLen,
  1278. .txBuf = txBuf,
  1279. .txBufLen = txBufLen,
  1280. .flags = flags,
  1281. .fwt = fwt,
  1282. };
  1283. EXIT_ON_ERR(ret, rfalStartTransceive(&ctx));
  1284. return rfalTransceiveRunBlockingTx();
  1285. }
  1286. /*******************************************************************************/
  1287. static ReturnCode rfalTransceiveRunBlockingTx(void) {
  1288. ReturnCode ret;
  1289. do {
  1290. rfalWorker();
  1291. ret = rfalGetTransceiveStatus();
  1292. } while(rfalIsTransceiveInTx() && (ret == ERR_BUSY));
  1293. if(rfalIsTransceiveInRx()) {
  1294. return ERR_NONE;
  1295. }
  1296. return ret;
  1297. }
  1298. /*******************************************************************************/
  1299. ReturnCode rfalTransceiveBlockingRx(void) {
  1300. ReturnCode ret;
  1301. do {
  1302. rfalWorker();
  1303. ret = rfalGetTransceiveStatus();
  1304. } while(rfalIsTransceiveInRx() && (ret == ERR_BUSY));
  1305. return ret;
  1306. }
  1307. /*******************************************************************************/
  1308. ReturnCode rfalTransceiveBlockingTxRx(
  1309. uint8_t* txBuf,
  1310. uint16_t txBufLen,
  1311. uint8_t* rxBuf,
  1312. uint16_t rxBufLen,
  1313. uint16_t* actLen,
  1314. uint32_t flags,
  1315. uint32_t fwt) {
  1316. ReturnCode ret;
  1317. EXIT_ON_ERR(
  1318. ret, rfalTransceiveBlockingTx(txBuf, txBufLen, rxBuf, rxBufLen, actLen, flags, fwt));
  1319. ret = rfalTransceiveBlockingRx();
  1320. /* Convert received bits to bytes */
  1321. if(actLen != NULL) {
  1322. *actLen = rfalConvBitsToBytes(*actLen);
  1323. }
  1324. return ret;
  1325. }
  1326. ReturnCode rfalTransceiveBitsBlockingTxRx(
  1327. uint8_t* txBuf,
  1328. uint16_t txBufLen,
  1329. uint8_t* rxBuf,
  1330. uint16_t rxBufLen,
  1331. uint16_t* actLen,
  1332. uint32_t flags,
  1333. uint32_t fwt) {
  1334. ReturnCode ret;
  1335. EXIT_ON_ERR(
  1336. ret, rfalTransceiveBitsBlockingTx(txBuf, txBufLen, rxBuf, rxBufLen, actLen, flags, fwt));
  1337. ret = rfalTransceiveBlockingRx();
  1338. return ret;
  1339. }
  1340. /*******************************************************************************/
  1341. static ReturnCode rfalRunTransceiveWorker(void) {
  1342. if(gRFAL.state == RFAL_STATE_TXRX) {
  1343. /*******************************************************************************/
  1344. /* Check Transceive Sanity Timer has expired */
  1345. if(gRFAL.tmr.txRx != RFAL_TIMING_NONE) {
  1346. if(rfalTimerisExpired(gRFAL.tmr.txRx)) {
  1347. /* If sanity timer has expired abort ongoing transceive and signal error */
  1348. gRFAL.TxRx.status = ERR_IO;
  1349. gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_FAIL;
  1350. }
  1351. }
  1352. /*******************************************************************************/
  1353. /* Run Tx or Rx state machines */
  1354. if(rfalIsTransceiveInTx()) {
  1355. rfalTransceiveTx();
  1356. return rfalGetTransceiveStatus();
  1357. }
  1358. if(rfalIsTransceiveInRx()) {
  1359. rfalTransceiveRx();
  1360. return rfalGetTransceiveStatus();
  1361. }
  1362. }
  1363. return ERR_WRONG_STATE;
  1364. }
  1365. /*******************************************************************************/
  1366. rfalTransceiveState rfalGetTransceiveState(void) {
  1367. return gRFAL.TxRx.state;
  1368. }
  1369. /*******************************************************************************/
  1370. ReturnCode rfalGetTransceiveStatus(void) {
  1371. return ((gRFAL.TxRx.state == RFAL_TXRX_STATE_IDLE) ? gRFAL.TxRx.status : ERR_BUSY);
  1372. }
  1373. /*******************************************************************************/
  1374. ReturnCode rfalGetTransceiveRSSI(uint16_t* rssi) {
  1375. uint16_t amRSSI;
  1376. uint16_t pmRSSI;
  1377. bool isSumMode;
  1378. if(rssi == NULL) {
  1379. return ERR_PARAM;
  1380. }
  1381. st25r3916GetRSSI(&amRSSI, &pmRSSI);
  1382. /* Check if Correlator Summation mode is being used */
  1383. isSumMode =
  1384. (st25r3916CheckReg(
  1385. ST25R3916_REG_CORR_CONF1,
  1386. ST25R3916_REG_CORR_CONF1_corr_s4,
  1387. ST25R3916_REG_CORR_CONF1_corr_s4) ?
  1388. st25r3916CheckReg(ST25R3916_REG_AUX, ST25R3916_REG_AUX_dis_corr, 0x00) :
  1389. false);
  1390. if(isSumMode) {
  1391. /*******************************************************************************/
  1392. /* Using SQRT from math.h and float. If due to compiler, resources or performance
  1393. * issue this cannot be used, other approaches can be foreseen with less accuracy:
  1394. * Use a simpler sqrt algorithm
  1395. * *rssi = MAX( amRSSI, pmRSSI );
  1396. * *rssi = ( (amRSSI + pmRSSI) / 2);
  1397. */
  1398. *rssi = (uint16_t)sqrt(
  1399. ((double)amRSSI * (double)amRSSI) +
  1400. ((double)pmRSSI *
  1401. (double)
  1402. pmRSSI)); /* PRQA S 5209 # MISRA 4.9 - External function (sqrt()) requires double */
  1403. } else {
  1404. /* Check which channel was used */
  1405. *rssi =
  1406. (st25r3916CheckReg(
  1407. ST25R3916_REG_AUX_DISPLAY,
  1408. ST25R3916_REG_AUX_DISPLAY_a_cha,
  1409. ST25R3916_REG_AUX_DISPLAY_a_cha) ?
  1410. pmRSSI :
  1411. amRSSI);
  1412. }
  1413. return ERR_NONE;
  1414. }
  1415. /*******************************************************************************/
  1416. void rfalWorker(void) {
  1417. platformProtectWorker(); /* Protect RFAL Worker/Task/Process */
  1418. switch(gRFAL.state) {
  1419. case RFAL_STATE_TXRX:
  1420. rfalRunTransceiveWorker();
  1421. break;
  1422. #if RFAL_FEATURE_LISTEN_MODE
  1423. case RFAL_STATE_LM:
  1424. rfalRunListenModeWorker();
  1425. break;
  1426. #endif /* RFAL_FEATURE_LISTEN_MODE */
  1427. #if RFAL_FEATURE_WAKEUP_MODE
  1428. case RFAL_STATE_WUM:
  1429. rfalRunWakeUpModeWorker();
  1430. break;
  1431. #endif /* RFAL_FEATURE_WAKEUP_MODE */
  1432. /* Nothing to be done */
  1433. default:
  1434. /* MISRA 16.4: no empty default statement (a comment being enough) */
  1435. break;
  1436. }
  1437. platformUnprotectWorker(); /* Unprotect RFAL Worker/Task/Process */
  1438. }
  1439. /*******************************************************************************/
  1440. static void rfalErrorHandling(void) {
  1441. uint16_t fifoBytesToRead;
  1442. fifoBytesToRead = rfalFIFOStatusGetNumBytes();
  1443. #ifdef RFAL_SW_EMD
  1444. /*******************************************************************************/
  1445. /* EMVCo */
  1446. /*******************************************************************************/
  1447. if(gRFAL.conf.eHandling == RFAL_ERRORHANDLING_EMVCO) {
  1448. bool rxHasIncParError;
  1449. /*******************************************************************************/
  1450. /* EMD Handling - NFC Forum Digital 1.1 4.1.1.1 ; EMVCo v2.5 4.9.2 */
  1451. /* ReEnable the receiver on frames with a length < 4 bytes, upon: */
  1452. /* - Collision or Framing error detected */
  1453. /* - Residual bits are detected (hard framing error) */
  1454. /* - Parity error */
  1455. /* - CRC error */
  1456. /*******************************************************************************/
  1457. /* Check if reception has incomplete bytes or parity error */
  1458. rxHasIncParError =
  1459. (rfalFIFOStatusIsIncompleteByte() ? true :
  1460. rfalFIFOStatusIsMissingPar()); /* MISRA 13.5 */
  1461. /* In case there are residual bits decrement FIFO bytes */
  1462. /* Ensure FIFO contains some byte as the FIFO might be empty upon Framing errors */
  1463. if((fifoBytesToRead > 0U) && rxHasIncParError) {
  1464. fifoBytesToRead--;
  1465. }
  1466. if(((gRFAL.fifo.bytesTotal + fifoBytesToRead) < RFAL_EMVCO_RX_MAXLEN) &&
  1467. ((gRFAL.TxRx.status == ERR_RF_COLLISION) || (gRFAL.TxRx.status == ERR_FRAMING) ||
  1468. (gRFAL.TxRx.status == ERR_PAR) || (gRFAL.TxRx.status == ERR_CRC) ||
  1469. rxHasIncParError)) {
  1470. /* Ignore this reception, ReEnable receiver which also clears the FIFO */
  1471. st25r3916ExecuteCommand(ST25R3916_CMD_UNMASK_RECEIVE_DATA);
  1472. /* Ensure that the NRT has not expired meanwhile */
  1473. if(st25r3916CheckReg(
  1474. ST25R3916_REG_NFCIP1_BIT_RATE, ST25R3916_REG_NFCIP1_BIT_RATE_nrt_on, 0x00)) {
  1475. if(st25r3916CheckReg(
  1476. ST25R3916_REG_AUX_DISPLAY, ST25R3916_REG_AUX_DISPLAY_rx_act, 0x00)) {
  1477. /* Abort reception */
  1478. st25r3916ExecuteCommand(ST25R3916_CMD_MASK_RECEIVE_DATA);
  1479. gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_FAIL;
  1480. return;
  1481. }
  1482. }
  1483. rfalFIFOStatusClear();
  1484. gRFAL.fifo.bytesTotal = 0;
  1485. gRFAL.TxRx.status = ERR_BUSY;
  1486. gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_WAIT_RXS;
  1487. }
  1488. return;
  1489. }
  1490. #endif
  1491. /*******************************************************************************/
  1492. /* ISO14443A Mode */
  1493. /*******************************************************************************/
  1494. if(gRFAL.mode == RFAL_MODE_POLL_NFCA) {
  1495. /*******************************************************************************/
  1496. /* If we received a frame with a incomplete byte we`ll raise a specific error *
  1497. * ( support for T2T 4 bit ACK / NAK, MIFARE and Kovio ) */
  1498. /*******************************************************************************/
  1499. if((gRFAL.TxRx.status == ERR_PAR) || (gRFAL.TxRx.status == ERR_CRC)) {
  1500. if(rfalFIFOStatusIsIncompleteByte()) {
  1501. st25r3916ReadFifo((uint8_t*)(gRFAL.TxRx.ctx.rxBuf), fifoBytesToRead);
  1502. if((gRFAL.TxRx.ctx.rxRcvdLen) != NULL) {
  1503. *gRFAL.TxRx.ctx.rxRcvdLen = rfalFIFOGetNumIncompleteBits();
  1504. }
  1505. gRFAL.TxRx.status = ERR_INCOMPLETE_BYTE;
  1506. gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_FAIL;
  1507. }
  1508. }
  1509. }
  1510. }
  1511. /*******************************************************************************/
  1512. static void rfalCleanupTransceive(void) {
  1513. /*******************************************************************************/
  1514. /* Transceive flags */
  1515. /*******************************************************************************/
  1516. /* Restore default settings on NFCIP1 mode, Receiving parity + CRC bits and manual Tx Parity*/
  1517. st25r3916ClrRegisterBits(
  1518. ST25R3916_REG_ISO14443A_NFC,
  1519. (ST25R3916_REG_ISO14443A_NFC_no_tx_par | ST25R3916_REG_ISO14443A_NFC_no_rx_par |
  1520. ST25R3916_REG_ISO14443A_NFC_nfc_f0));
  1521. /* Restore AGC enabled */
  1522. st25r3916SetRegisterBits(ST25R3916_REG_RX_CONF2, ST25R3916_REG_RX_CONF2_agc_en);
  1523. /*******************************************************************************/
  1524. /*******************************************************************************/
  1525. /* Transceive timers */
  1526. /*******************************************************************************/
  1527. rfalTimerDestroy(gRFAL.tmr.txRx);
  1528. rfalTimerDestroy(gRFAL.tmr.RXE);
  1529. gRFAL.tmr.txRx = RFAL_TIMING_NONE;
  1530. gRFAL.tmr.RXE = RFAL_TIMING_NONE;
  1531. /*******************************************************************************/
  1532. /*******************************************************************************/
  1533. /* Execute Post Transceive Callback */
  1534. /*******************************************************************************/
  1535. if(gRFAL.callbacks.postTxRx != NULL) {
  1536. gRFAL.callbacks.postTxRx(gRFAL.callbacks.ctx);
  1537. }
  1538. /*******************************************************************************/
  1539. }
  1540. /*******************************************************************************/
  1541. static void rfalPrepareTransceive(void) {
  1542. uint32_t maskInterrupts;
  1543. uint8_t reg;
  1544. /* If we are in RW or AP2P mode */
  1545. if(!rfalIsModePassiveListen(gRFAL.mode)) {
  1546. /* Reset receive logic with STOP command */
  1547. st25r3916ExecuteCommand(ST25R3916_CMD_STOP);
  1548. /* Reset Rx Gain */
  1549. st25r3916ExecuteCommand(ST25R3916_CMD_RESET_RXGAIN);
  1550. } else {
  1551. /* In Passive Listen Mode do not use STOP as it stops FDT timer */
  1552. st25r3916ExecuteCommand(ST25R3916_CMD_CLEAR_FIFO);
  1553. }
  1554. /*******************************************************************************/
  1555. /* FDT Poll */
  1556. /*******************************************************************************/
  1557. if(rfalIsModePassiveComm(gRFAL.mode)) /* Passive Comms */
  1558. {
  1559. /* In Passive communications General Purpose Timer is used to measure FDT Poll */
  1560. if(gRFAL.timings.FDTPoll != RFAL_TIMING_NONE) {
  1561. /* Configure GPT to start at RX end */
  1562. st25r3916SetStartGPTimer(
  1563. (uint16_t)rfalConv1fcTo8fc(MIN(
  1564. gRFAL.timings.FDTPoll, (gRFAL.timings.FDTPoll - RFAL_FDT_POLL_ADJUSTMENT))),
  1565. ST25R3916_REG_TIMER_EMV_CONTROL_gptc_erx);
  1566. }
  1567. }
  1568. /*******************************************************************************/
  1569. /* Execute Pre Transceive Callback */
  1570. /*******************************************************************************/
  1571. if(gRFAL.callbacks.preTxRx != NULL) {
  1572. gRFAL.callbacks.preTxRx(gRFAL.callbacks.ctx);
  1573. }
  1574. /*******************************************************************************/
  1575. maskInterrupts =
  1576. (ST25R3916_IRQ_MASK_FWL | ST25R3916_IRQ_MASK_TXE | ST25R3916_IRQ_MASK_RXS |
  1577. ST25R3916_IRQ_MASK_RXE | ST25R3916_IRQ_MASK_PAR | ST25R3916_IRQ_MASK_CRC |
  1578. ST25R3916_IRQ_MASK_ERR1 | ST25R3916_IRQ_MASK_ERR2 | ST25R3916_IRQ_MASK_NRE);
  1579. /*******************************************************************************/
  1580. /* Transceive flags */
  1581. /*******************************************************************************/
  1582. reg =
  1583. (ST25R3916_REG_ISO14443A_NFC_no_tx_par_off | ST25R3916_REG_ISO14443A_NFC_no_rx_par_off |
  1584. ST25R3916_REG_ISO14443A_NFC_nfc_f0_off);
  1585. /* Check if NFCIP1 mode is to be enabled */
  1586. if((gRFAL.TxRx.ctx.flags & (uint8_t)RFAL_TXRX_FLAGS_NFCIP1_ON) != 0U) {
  1587. reg |= ST25R3916_REG_ISO14443A_NFC_nfc_f0;
  1588. }
  1589. /* Check if Parity check is to be skipped and to keep the parity + CRC bits in FIFO */
  1590. if((gRFAL.TxRx.ctx.flags & (uint8_t)RFAL_TXRX_FLAGS_PAR_RX_KEEP) != 0U) {
  1591. reg |= ST25R3916_REG_ISO14443A_NFC_no_rx_par;
  1592. }
  1593. /* Check if automatic Parity bits is to be disabled */
  1594. if((gRFAL.TxRx.ctx.flags & (uint8_t)RFAL_TXRX_FLAGS_PAR_TX_NONE) != 0U) {
  1595. reg |= ST25R3916_REG_ISO14443A_NFC_no_tx_par;
  1596. }
  1597. /* Apply current TxRx flags on ISO14443A and NFC 106kb/s Settings Register */
  1598. st25r3916ChangeRegisterBits(
  1599. ST25R3916_REG_ISO14443A_NFC,
  1600. (ST25R3916_REG_ISO14443A_NFC_no_tx_par | ST25R3916_REG_ISO14443A_NFC_no_rx_par |
  1601. ST25R3916_REG_ISO14443A_NFC_nfc_f0),
  1602. reg);
  1603. /* Check if AGC is to be disabled */
  1604. if((gRFAL.TxRx.ctx.flags & (uint8_t)RFAL_TXRX_FLAGS_AGC_OFF) != 0U) {
  1605. st25r3916ClrRegisterBits(ST25R3916_REG_RX_CONF2, ST25R3916_REG_RX_CONF2_agc_en);
  1606. } else {
  1607. st25r3916SetRegisterBits(ST25R3916_REG_RX_CONF2, ST25R3916_REG_RX_CONF2_agc_en);
  1608. }
  1609. /*******************************************************************************/
  1610. /*******************************************************************************/
  1611. /* EMVCo NRT mode */
  1612. /*******************************************************************************/
  1613. if(gRFAL.conf.eHandling == RFAL_ERRORHANDLING_EMVCO) {
  1614. st25r3916SetRegisterBits(
  1615. ST25R3916_REG_TIMER_EMV_CONTROL, ST25R3916_REG_TIMER_EMV_CONTROL_nrt_emv);
  1616. maskInterrupts |= ST25R3916_IRQ_MASK_RX_REST;
  1617. } else {
  1618. st25r3916ClrRegisterBits(
  1619. ST25R3916_REG_TIMER_EMV_CONTROL, ST25R3916_REG_TIMER_EMV_CONTROL_nrt_emv);
  1620. }
  1621. /*******************************************************************************/
  1622. /* In Passive Listen mode additionally enable External Field interrupts */
  1623. if(rfalIsModePassiveListen(gRFAL.mode)) {
  1624. maskInterrupts |=
  1625. (ST25R3916_IRQ_MASK_EOF |
  1626. ST25R3916_IRQ_MASK_WU_F); /* Enable external Field interrupts to detect Link Loss and SENF_REQ auto responses */
  1627. }
  1628. /* In Active comms enable also External Field interrupts and set RF Collsion Avoindance */
  1629. if(rfalIsModeActiveComm(gRFAL.mode)) {
  1630. maskInterrupts |=
  1631. (ST25R3916_IRQ_MASK_EOF | ST25R3916_IRQ_MASK_EON | ST25R3916_IRQ_MASK_PPON2 |
  1632. ST25R3916_IRQ_MASK_CAT | ST25R3916_IRQ_MASK_CAC);
  1633. /* Set n=0 for subsequent RF Collision Avoidance */
  1634. st25r3916ChangeRegisterBits(ST25R3916_REG_AUX, ST25R3916_REG_AUX_nfc_n_mask, 0);
  1635. }
  1636. /*******************************************************************************/
  1637. /* Start transceive Sanity Timer if a FWT is used */
  1638. if((gRFAL.TxRx.ctx.fwt != RFAL_FWT_NONE) && (gRFAL.TxRx.ctx.fwt != 0U)) {
  1639. rfalTimerStart(gRFAL.tmr.txRx, rfalCalcSanityTmr(gRFAL.TxRx.ctx.fwt));
  1640. }
  1641. /*******************************************************************************/
  1642. /*******************************************************************************/
  1643. /* Clear and enable these interrupts */
  1644. st25r3916GetInterrupt(maskInterrupts);
  1645. st25r3916EnableInterrupts(maskInterrupts);
  1646. /* Clear FIFO status local copy */
  1647. rfalFIFOStatusClear();
  1648. }
  1649. /*******************************************************************************/
  1650. static void rfalTransceiveTx(void) {
  1651. volatile uint32_t irqs;
  1652. uint16_t tmp;
  1653. ReturnCode ret;
  1654. /* Supress warning in case NFC-V feature is disabled */
  1655. ret = ERR_NONE;
  1656. NO_WARNING(ret);
  1657. irqs = ST25R3916_IRQ_MASK_NONE;
  1658. if(gRFAL.TxRx.state != gRFAL.TxRx.lastState) {
  1659. /* rfalLogD( "RFAL: lastSt: %d curSt: %d \r\n", gRFAL.TxRx.lastState, gRFAL.TxRx.state ); */
  1660. gRFAL.TxRx.lastState = gRFAL.TxRx.state;
  1661. }
  1662. switch(gRFAL.TxRx.state) {
  1663. /*******************************************************************************/
  1664. case RFAL_TXRX_STATE_TX_IDLE:
  1665. /* Nothing to do */
  1666. gRFAL.TxRx.state = RFAL_TXRX_STATE_TX_WAIT_GT;
  1667. /* fall through */
  1668. /*******************************************************************************/
  1669. case RFAL_TXRX_STATE_TX_WAIT_GT: /* PRQA S 2003 # MISRA 16.3 - Intentional fall through */
  1670. if(!rfalIsGTExpired()) {
  1671. break;
  1672. }
  1673. rfalTimerDestroy(gRFAL.tmr.GT);
  1674. gRFAL.tmr.GT = RFAL_TIMING_NONE;
  1675. gRFAL.TxRx.state = RFAL_TXRX_STATE_TX_WAIT_FDT;
  1676. /* fall through */
  1677. /*******************************************************************************/
  1678. case RFAL_TXRX_STATE_TX_WAIT_FDT: /* PRQA S 2003 # MISRA 16.3 - Intentional fall through */
  1679. /* Only in Passive communications GPT is used to measure FDT Poll */
  1680. if(rfalIsModePassiveComm(gRFAL.mode)) {
  1681. if(st25r3916IsGPTRunning()) {
  1682. break;
  1683. }
  1684. }
  1685. gRFAL.TxRx.state = RFAL_TXRX_STATE_TX_TRANSMIT;
  1686. /* fall through */
  1687. /*******************************************************************************/
  1688. case RFAL_TXRX_STATE_TX_TRANSMIT: /* PRQA S 2003 # MISRA 16.3 - Intentional fall through */
  1689. /* Clear FIFO, Clear and Enable the Interrupts */
  1690. rfalPrepareTransceive();
  1691. /* ST25R3916 has a fixed FIFO water level */
  1692. gRFAL.fifo.expWL = RFAL_FIFO_OUT_WL;
  1693. #if RFAL_FEATURE_NFCV
  1694. /*******************************************************************************/
  1695. /* In NFC-V streaming mode, the FIFO needs to be loaded with the coded bits */
  1696. if((RFAL_MODE_POLL_NFCV == gRFAL.mode) || (RFAL_MODE_POLL_PICOPASS == gRFAL.mode)) {
  1697. #if 0
  1698. /* Debugging code: output the payload bits by writing into the FIFO and subsequent clearing */
  1699. st25r3916WriteFifo(gRFAL.TxRx.ctx.txBuf, rfalConvBitsToBytes(gRFAL.TxRx.ctx.txBufLen));
  1700. st25r3916ExecuteCommand( ST25R3916_CMD_CLEAR_FIFO );
  1701. #endif
  1702. /* Calculate the bytes needed to be Written into FIFO (a incomplete byte will be added as 1byte) */
  1703. gRFAL.nfcvData.nfcvOffset = 0;
  1704. ret = iso15693VCDCode(
  1705. gRFAL.TxRx.ctx.txBuf,
  1706. rfalConvBitsToBytes(gRFAL.TxRx.ctx.txBufLen),
  1707. (((gRFAL.nfcvData.origCtx.flags & (uint32_t)RFAL_TXRX_FLAGS_CRC_TX_MANUAL) != 0U) ?
  1708. false :
  1709. true),
  1710. (((gRFAL.nfcvData.origCtx.flags & (uint32_t)RFAL_TXRX_FLAGS_NFCV_FLAG_MANUAL) !=
  1711. 0U) ?
  1712. false :
  1713. true),
  1714. (RFAL_MODE_POLL_PICOPASS == gRFAL.mode),
  1715. &gRFAL.fifo.bytesTotal,
  1716. &gRFAL.nfcvData.nfcvOffset,
  1717. gRFAL.nfcvData.codingBuffer,
  1718. MIN((uint16_t)ST25R3916_FIFO_DEPTH, (uint16_t)sizeof(gRFAL.nfcvData.codingBuffer)),
  1719. &gRFAL.fifo.bytesWritten);
  1720. if((ret != ERR_NONE) && (ret != ERR_AGAIN)) {
  1721. gRFAL.TxRx.status = ret;
  1722. gRFAL.TxRx.state = RFAL_TXRX_STATE_TX_FAIL;
  1723. break;
  1724. }
  1725. /* Set the number of full bytes and bits to be transmitted */
  1726. st25r3916SetNumTxBits((uint16_t)rfalConvBytesToBits(gRFAL.fifo.bytesTotal));
  1727. /* Load FIFO with coded bytes */
  1728. st25r3916WriteFifo(gRFAL.nfcvData.codingBuffer, gRFAL.fifo.bytesWritten);
  1729. }
  1730. /*******************************************************************************/
  1731. else
  1732. #endif /* RFAL_FEATURE_NFCV */
  1733. {
  1734. /* Calculate the bytes needed to be Written into FIFO (a incomplete byte will be added as 1byte) */
  1735. gRFAL.fifo.bytesTotal = (uint16_t)rfalCalcNumBytes(gRFAL.TxRx.ctx.txBufLen);
  1736. /* Set the number of full bytes and bits to be transmitted */
  1737. st25r3916SetNumTxBits(gRFAL.TxRx.ctx.txBufLen);
  1738. /* Load FIFO with total length or FIFO's maximum */
  1739. gRFAL.fifo.bytesWritten = MIN(gRFAL.fifo.bytesTotal, ST25R3916_FIFO_DEPTH);
  1740. st25r3916WriteFifo(gRFAL.TxRx.ctx.txBuf, gRFAL.fifo.bytesWritten);
  1741. }
  1742. /*Check if Observation Mode is enabled and set it on ST25R391x */
  1743. rfalCheckEnableObsModeTx();
  1744. /*******************************************************************************/
  1745. /* If we're in Passive Listen mode ensure that the external field is still On */
  1746. if(rfalIsModePassiveListen(gRFAL.mode)) {
  1747. if(!rfalIsExtFieldOn()) {
  1748. gRFAL.TxRx.status = ERR_LINK_LOSS;
  1749. gRFAL.TxRx.state = RFAL_TXRX_STATE_TX_FAIL;
  1750. break;
  1751. }
  1752. }
  1753. /*******************************************************************************/
  1754. /* Trigger/Start transmission */
  1755. if((gRFAL.TxRx.ctx.flags & (uint32_t)RFAL_TXRX_FLAGS_CRC_TX_MANUAL) != 0U) {
  1756. st25r3916ExecuteCommand(ST25R3916_CMD_TRANSMIT_WITHOUT_CRC);
  1757. } else {
  1758. st25r3916ExecuteCommand(ST25R3916_CMD_TRANSMIT_WITH_CRC);
  1759. }
  1760. /* Check if a WL level is expected or TXE should come */
  1761. gRFAL.TxRx.state =
  1762. ((gRFAL.fifo.bytesWritten < gRFAL.fifo.bytesTotal) ? RFAL_TXRX_STATE_TX_WAIT_WL :
  1763. RFAL_TXRX_STATE_TX_WAIT_TXE);
  1764. break;
  1765. /*******************************************************************************/
  1766. case RFAL_TXRX_STATE_TX_WAIT_WL:
  1767. irqs = st25r3916GetInterrupt((ST25R3916_IRQ_MASK_FWL | ST25R3916_IRQ_MASK_TXE));
  1768. if(irqs == ST25R3916_IRQ_MASK_NONE) {
  1769. break; /* No interrupt to process */
  1770. }
  1771. if(((irqs & ST25R3916_IRQ_MASK_FWL) != 0U) && ((irqs & ST25R3916_IRQ_MASK_TXE) == 0U)) {
  1772. gRFAL.TxRx.state = RFAL_TXRX_STATE_TX_RELOAD_FIFO;
  1773. } else {
  1774. gRFAL.TxRx.status = ERR_IO;
  1775. gRFAL.TxRx.state = RFAL_TXRX_STATE_TX_FAIL;
  1776. break;
  1777. }
  1778. /* fall through */
  1779. /*******************************************************************************/
  1780. case RFAL_TXRX_STATE_TX_RELOAD_FIFO: /* PRQA S 2003 # MISRA 16.3 - Intentional fall through */
  1781. #if RFAL_FEATURE_NFCV
  1782. /*******************************************************************************/
  1783. /* In NFC-V streaming mode, the FIFO needs to be loaded with the coded bits */
  1784. if((RFAL_MODE_POLL_NFCV == gRFAL.mode) || (RFAL_MODE_POLL_PICOPASS == gRFAL.mode)) {
  1785. uint16_t maxLen;
  1786. /* Load FIFO with the remaining length or maximum available (which fit on the coding buffer) */
  1787. maxLen =
  1788. (uint16_t)MIN((gRFAL.fifo.bytesTotal - gRFAL.fifo.bytesWritten), gRFAL.fifo.expWL);
  1789. maxLen = (uint16_t)MIN(maxLen, sizeof(gRFAL.nfcvData.codingBuffer));
  1790. tmp = 0;
  1791. /* Calculate the bytes needed to be Written into FIFO (a incomplete byte will be added as 1byte) */
  1792. ret = iso15693VCDCode(
  1793. gRFAL.TxRx.ctx.txBuf,
  1794. rfalConvBitsToBytes(gRFAL.TxRx.ctx.txBufLen),
  1795. (((gRFAL.nfcvData.origCtx.flags & (uint32_t)RFAL_TXRX_FLAGS_CRC_TX_MANUAL) != 0U) ?
  1796. false :
  1797. true),
  1798. (((gRFAL.nfcvData.origCtx.flags & (uint32_t)RFAL_TXRX_FLAGS_NFCV_FLAG_MANUAL) !=
  1799. 0U) ?
  1800. false :
  1801. true),
  1802. (RFAL_MODE_POLL_PICOPASS == gRFAL.mode),
  1803. &gRFAL.fifo.bytesTotal,
  1804. &gRFAL.nfcvData.nfcvOffset,
  1805. gRFAL.nfcvData.codingBuffer,
  1806. maxLen,
  1807. &tmp);
  1808. if((ret != ERR_NONE) && (ret != ERR_AGAIN)) {
  1809. gRFAL.TxRx.status = ret;
  1810. gRFAL.TxRx.state = RFAL_TXRX_STATE_TX_FAIL;
  1811. break;
  1812. }
  1813. /* Load FIFO with coded bytes */
  1814. st25r3916WriteFifo(gRFAL.nfcvData.codingBuffer, tmp);
  1815. }
  1816. /*******************************************************************************/
  1817. else
  1818. #endif /* RFAL_FEATURE_NFCV */
  1819. {
  1820. /* Load FIFO with the remaining length or maximum available */
  1821. tmp = MIN(
  1822. (gRFAL.fifo.bytesTotal - gRFAL.fifo.bytesWritten),
  1823. gRFAL.fifo.expWL); /* tmp holds the number of bytes written on this iteration */
  1824. st25r3916WriteFifo(&gRFAL.TxRx.ctx.txBuf[gRFAL.fifo.bytesWritten], tmp);
  1825. }
  1826. /* Update total written bytes to FIFO */
  1827. gRFAL.fifo.bytesWritten += tmp;
  1828. /* Check if a WL level is expected or TXE should come */
  1829. gRFAL.TxRx.state =
  1830. ((gRFAL.fifo.bytesWritten < gRFAL.fifo.bytesTotal) ? RFAL_TXRX_STATE_TX_WAIT_WL :
  1831. RFAL_TXRX_STATE_TX_WAIT_TXE);
  1832. break;
  1833. /*******************************************************************************/
  1834. case RFAL_TXRX_STATE_TX_WAIT_TXE:
  1835. irqs = st25r3916GetInterrupt((ST25R3916_IRQ_MASK_FWL | ST25R3916_IRQ_MASK_TXE));
  1836. if(irqs == ST25R3916_IRQ_MASK_NONE) {
  1837. break; /* No interrupt to process */
  1838. }
  1839. if((irqs & ST25R3916_IRQ_MASK_TXE) != 0U) {
  1840. gRFAL.TxRx.state = RFAL_TXRX_STATE_TX_DONE;
  1841. } else if((irqs & ST25R3916_IRQ_MASK_FWL) != 0U) {
  1842. break; /* Ignore ST25R3916 FIFO WL if total TxLen is already on the FIFO */
  1843. } else {
  1844. gRFAL.TxRx.status = ERR_IO;
  1845. gRFAL.TxRx.state = RFAL_TXRX_STATE_TX_FAIL;
  1846. break;
  1847. }
  1848. /* fall through */
  1849. /*******************************************************************************/
  1850. case RFAL_TXRX_STATE_TX_DONE: /* PRQA S 2003 # MISRA 16.3 - Intentional fall through */
  1851. /* If no rxBuf is provided do not wait/expect Rx */
  1852. if(gRFAL.TxRx.ctx.rxBuf == NULL) {
  1853. /*Check if Observation Mode was enabled and disable it on ST25R391x */
  1854. rfalCheckDisableObsMode();
  1855. /* Clean up Transceive */
  1856. rfalCleanupTransceive();
  1857. gRFAL.TxRx.status = ERR_NONE;
  1858. gRFAL.TxRx.state = RFAL_TXRX_STATE_IDLE;
  1859. break;
  1860. }
  1861. rfalCheckEnableObsModeRx();
  1862. /* Goto Rx */
  1863. gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_IDLE;
  1864. break;
  1865. /*******************************************************************************/
  1866. case RFAL_TXRX_STATE_TX_FAIL:
  1867. /* Error should be assigned by previous state */
  1868. if(gRFAL.TxRx.status == ERR_BUSY) {
  1869. gRFAL.TxRx.status = ERR_SYSTEM;
  1870. }
  1871. /*Check if Observation Mode was enabled and disable it on ST25R391x */
  1872. rfalCheckDisableObsMode();
  1873. /* Clean up Transceive */
  1874. rfalCleanupTransceive();
  1875. gRFAL.TxRx.state = RFAL_TXRX_STATE_IDLE;
  1876. break;
  1877. /*******************************************************************************/
  1878. default:
  1879. gRFAL.TxRx.status = ERR_SYSTEM;
  1880. gRFAL.TxRx.state = RFAL_TXRX_STATE_TX_FAIL;
  1881. break;
  1882. }
  1883. }
  1884. /*******************************************************************************/
  1885. static void rfalTransceiveRx(void) {
  1886. volatile uint32_t irqs;
  1887. uint16_t tmp;
  1888. uint16_t aux;
  1889. irqs = ST25R3916_IRQ_MASK_NONE;
  1890. if(gRFAL.TxRx.state != gRFAL.TxRx.lastState) {
  1891. /* rfalLogD( "RFAL: lastSt: %d curSt: %d \r\n", gRFAL.TxRx.lastState, gRFAL.TxRx.state ); */
  1892. gRFAL.TxRx.lastState = gRFAL.TxRx.state;
  1893. }
  1894. switch(gRFAL.TxRx.state) {
  1895. /*******************************************************************************/
  1896. case RFAL_TXRX_STATE_RX_IDLE:
  1897. /* Clear rx counters */
  1898. gRFAL.fifo.bytesWritten = 0; /* Total bytes written on RxBuffer */
  1899. gRFAL.fifo.bytesTotal = 0; /* Total bytes in FIFO will now be from Rx */
  1900. if(gRFAL.TxRx.ctx.rxRcvdLen != NULL) {
  1901. *gRFAL.TxRx.ctx.rxRcvdLen = 0;
  1902. }
  1903. gRFAL.TxRx.state =
  1904. (rfalIsModeActiveComm(gRFAL.mode) ? RFAL_TXRX_STATE_RX_WAIT_EON :
  1905. RFAL_TXRX_STATE_RX_WAIT_RXS);
  1906. break;
  1907. /*******************************************************************************/
  1908. case RFAL_TXRX_STATE_RX_WAIT_RXS:
  1909. /*******************************************************************************/
  1910. irqs = st25r3916GetInterrupt(
  1911. (ST25R3916_IRQ_MASK_RXS | ST25R3916_IRQ_MASK_NRE | ST25R3916_IRQ_MASK_EOF));
  1912. if(irqs == ST25R3916_IRQ_MASK_NONE) {
  1913. break; /* No interrupt to process */
  1914. }
  1915. /* Only raise Timeout if NRE is detected with no Rx Start (NRT EMV mode) */
  1916. if(((irqs & ST25R3916_IRQ_MASK_NRE) != 0U) && ((irqs & ST25R3916_IRQ_MASK_RXS) == 0U)) {
  1917. gRFAL.TxRx.status = ERR_TIMEOUT;
  1918. gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_FAIL;
  1919. break;
  1920. }
  1921. /* Only raise Link Loss if EOF is detected with no Rx Start */
  1922. if(((irqs & ST25R3916_IRQ_MASK_EOF) != 0U) && ((irqs & ST25R3916_IRQ_MASK_RXS) == 0U)) {
  1923. /* In AP2P a Field On has already occurred - treat this as timeout | mute */
  1924. gRFAL.TxRx.status = (rfalIsModeActiveComm(gRFAL.mode) ? ERR_TIMEOUT : ERR_LINK_LOSS);
  1925. gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_FAIL;
  1926. break;
  1927. }
  1928. if((irqs & ST25R3916_IRQ_MASK_RXS) != 0U) {
  1929. /*******************************************************************************/
  1930. /* REMARK: Silicon workaround ST25R3916 Errata #TBD */
  1931. /* Rarely on corrupted frames I_rxs gets signaled but I_rxe is not signaled */
  1932. /* Use a SW timer to handle an eventual missing RXE */
  1933. rfalTimerStart(gRFAL.tmr.RXE, RFAL_NORXE_TOUT);
  1934. /*******************************************************************************/
  1935. gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_WAIT_RXE;
  1936. } else {
  1937. gRFAL.TxRx.status = ERR_IO;
  1938. gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_FAIL;
  1939. break;
  1940. }
  1941. /* remove NRE that might appear together (NRT EMV mode), and remove RXS, but keep EOF if present for next state */
  1942. irqs &= ~(ST25R3916_IRQ_MASK_RXS | ST25R3916_IRQ_MASK_NRE);
  1943. /* fall through */
  1944. /*******************************************************************************/
  1945. case RFAL_TXRX_STATE_RX_WAIT_RXE: /* PRQA S 2003 # MISRA 16.3 - Intentional fall through */
  1946. /*******************************************************************************/
  1947. /* REMARK: Silicon workaround ST25R3916 Errata #TBD */
  1948. /* ST25R396 may indicate RXS without RXE afterwards, this happens rarely on */
  1949. /* corrupted frames. */
  1950. /* SW timer is used to timeout upon a missing RXE */
  1951. if(rfalTimerisExpired(gRFAL.tmr.RXE)) {
  1952. gRFAL.TxRx.status = ERR_FRAMING;
  1953. gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_FAIL;
  1954. }
  1955. /*******************************************************************************/
  1956. irqs |= st25r3916GetInterrupt(
  1957. (ST25R3916_IRQ_MASK_RXE | ST25R3916_IRQ_MASK_FWL | ST25R3916_IRQ_MASK_EOF |
  1958. ST25R3916_IRQ_MASK_RX_REST | ST25R3916_IRQ_MASK_WU_F));
  1959. if(irqs == ST25R3916_IRQ_MASK_NONE) {
  1960. break; /* No interrupt to process */
  1961. }
  1962. if((irqs & ST25R3916_IRQ_MASK_RX_REST) != 0U) {
  1963. /* RX_REST indicates that Receiver has been reseted due to EMD, therefore a RXS + RXE should *
  1964. * follow if a good reception is followed within the valid initial timeout */
  1965. /* Check whether NRT has expired already, if so signal a timeout */
  1966. if(st25r3916GetInterrupt(ST25R3916_IRQ_MASK_NRE) != 0U) {
  1967. gRFAL.TxRx.status = ERR_TIMEOUT;
  1968. gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_FAIL;
  1969. break;
  1970. }
  1971. if(st25r3916CheckReg(
  1972. ST25R3916_REG_NFCIP1_BIT_RATE,
  1973. ST25R3916_REG_NFCIP1_BIT_RATE_nrt_on,
  1974. 0)) /* MISRA 13.5 */
  1975. {
  1976. gRFAL.TxRx.status = ERR_TIMEOUT;
  1977. gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_FAIL;
  1978. break;
  1979. }
  1980. /* Discard any previous RXS */
  1981. st25r3916GetInterrupt(ST25R3916_IRQ_MASK_RXS);
  1982. /* Check whether a following reception has already started */
  1983. if(st25r3916CheckReg(
  1984. ST25R3916_REG_AUX_DISPLAY,
  1985. ST25R3916_REG_AUX_DISPLAY_rx_act,
  1986. ST25R3916_REG_AUX_DISPLAY_rx_act)) {
  1987. gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_WAIT_RXE;
  1988. break;
  1989. }
  1990. gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_WAIT_RXS;
  1991. break;
  1992. }
  1993. if(((irqs & ST25R3916_IRQ_MASK_FWL) != 0U) && ((irqs & ST25R3916_IRQ_MASK_RXE) == 0U)) {
  1994. gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_READ_FIFO;
  1995. break;
  1996. }
  1997. /* Automatic responses allowed during TxRx only for the SENSF_REQ */
  1998. if((irqs & ST25R3916_IRQ_MASK_WU_F) != 0U) {
  1999. gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_WAIT_RXS;
  2000. break;
  2001. }
  2002. /* After RXE retrieve and check for any error irqs */
  2003. irqs |= st25r3916GetInterrupt(
  2004. (ST25R3916_IRQ_MASK_CRC | ST25R3916_IRQ_MASK_PAR | ST25R3916_IRQ_MASK_ERR1 |
  2005. ST25R3916_IRQ_MASK_ERR2 | ST25R3916_IRQ_MASK_COL));
  2006. gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_ERR_CHECK;
  2007. /* fall through */
  2008. /*******************************************************************************/
  2009. case RFAL_TXRX_STATE_RX_ERR_CHECK: /* PRQA S 2003 # MISRA 16.3 - Intentional fall through */
  2010. if((irqs & ST25R3916_IRQ_MASK_ERR1) != 0U) {
  2011. gRFAL.TxRx.status = ERR_FRAMING;
  2012. gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_READ_DATA;
  2013. /* Check if there's a specific error handling for this */
  2014. rfalErrorHandling();
  2015. break;
  2016. }
  2017. /* Discard Soft Framing errors in AP2P and CE */
  2018. else if(rfalIsModePassivePoll(gRFAL.mode) && ((irqs & ST25R3916_IRQ_MASK_ERR2) != 0U)) {
  2019. gRFAL.TxRx.status = ERR_FRAMING;
  2020. gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_READ_DATA;
  2021. /* Check if there's a specific error handling for this */
  2022. rfalErrorHandling();
  2023. break;
  2024. } else if((irqs & ST25R3916_IRQ_MASK_PAR) != 0U) {
  2025. gRFAL.TxRx.status = ERR_PAR;
  2026. gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_READ_DATA;
  2027. /* Check if there's a specific error handling for this */
  2028. rfalErrorHandling();
  2029. break;
  2030. } else if((irqs & ST25R3916_IRQ_MASK_CRC) != 0U) {
  2031. gRFAL.TxRx.status = ERR_CRC;
  2032. gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_READ_DATA;
  2033. /* Check if there's a specific error handling for this */
  2034. rfalErrorHandling();
  2035. break;
  2036. } else if((irqs & ST25R3916_IRQ_MASK_COL) != 0U) {
  2037. gRFAL.TxRx.status = ERR_RF_COLLISION;
  2038. gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_READ_DATA;
  2039. /* Check if there's a specific error handling for this */
  2040. rfalErrorHandling();
  2041. break;
  2042. } else if(rfalIsModePassiveListen(gRFAL.mode) && ((irqs & ST25R3916_IRQ_MASK_EOF) != 0U)) {
  2043. gRFAL.TxRx.status = ERR_LINK_LOSS;
  2044. gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_FAIL;
  2045. break;
  2046. } else if((irqs & ST25R3916_IRQ_MASK_RXE) != 0U) {
  2047. /* Reception ended without any error indication, *
  2048. * check FIFO status for malformed or incomplete frames */
  2049. /* Check if the reception ends with an incomplete byte (residual bits) */
  2050. if(rfalFIFOStatusIsIncompleteByte()) {
  2051. gRFAL.TxRx.status = ERR_INCOMPLETE_BYTE;
  2052. }
  2053. /* Check if the reception ends missing parity bit */
  2054. else if(rfalFIFOStatusIsMissingPar()) {
  2055. gRFAL.TxRx.status = ERR_FRAMING;
  2056. } else {
  2057. /* MISRA 15.7 - Empty else */
  2058. }
  2059. gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_READ_DATA;
  2060. } else {
  2061. gRFAL.TxRx.status = ERR_IO;
  2062. gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_FAIL;
  2063. break;
  2064. }
  2065. /* fall through */
  2066. /*******************************************************************************/
  2067. case RFAL_TXRX_STATE_RX_READ_DATA: /* PRQA S 2003 # MISRA 16.3 - Intentional fall through */
  2068. tmp = rfalFIFOStatusGetNumBytes();
  2069. /*******************************************************************************/
  2070. /* Check if CRC should not be placed in rxBuf */
  2071. if(((gRFAL.TxRx.ctx.flags & (uint32_t)RFAL_TXRX_FLAGS_CRC_RX_KEEP) == 0U)) {
  2072. /* if received frame was bigger than CRC */
  2073. if((uint16_t)(gRFAL.fifo.bytesTotal + tmp) > 0U) {
  2074. /* By default CRC will not be placed into the rxBuffer */
  2075. if((tmp > RFAL_CRC_LEN)) {
  2076. tmp -= RFAL_CRC_LEN;
  2077. }
  2078. /* If the CRC was already placed into rxBuffer (due to WL interrupt where CRC was already in FIFO Read)
  2079. * cannot remove it from rxBuf. Can only remove it from rxBufLen not indicate the presence of CRC */
  2080. else if(gRFAL.fifo.bytesTotal > RFAL_CRC_LEN) {
  2081. gRFAL.fifo.bytesTotal -= RFAL_CRC_LEN;
  2082. } else {
  2083. /* MISRA 15.7 - Empty else */
  2084. }
  2085. }
  2086. }
  2087. gRFAL.fifo.bytesTotal += tmp; /* add to total bytes counter */
  2088. /*******************************************************************************/
  2089. /* Check if remaining bytes fit on the rxBuf available */
  2090. if(gRFAL.fifo.bytesTotal > rfalConvBitsToBytes(gRFAL.TxRx.ctx.rxBufLen)) {
  2091. tmp =
  2092. (uint16_t)(rfalConvBitsToBytes(gRFAL.TxRx.ctx.rxBufLen) - gRFAL.fifo.bytesWritten);
  2093. /* Transmission errors have precedence over buffer error */
  2094. if(gRFAL.TxRx.status == ERR_BUSY) {
  2095. gRFAL.TxRx.status = ERR_NOMEM;
  2096. }
  2097. }
  2098. /*******************************************************************************/
  2099. /* Retrieve remaining bytes from FIFO to rxBuf, and assign total length rcvd */
  2100. st25r3916ReadFifo(&gRFAL.TxRx.ctx.rxBuf[gRFAL.fifo.bytesWritten], tmp);
  2101. if(gRFAL.TxRx.ctx.rxRcvdLen != NULL) {
  2102. (*gRFAL.TxRx.ctx.rxRcvdLen) = (uint16_t)rfalConvBytesToBits(gRFAL.fifo.bytesTotal);
  2103. if(rfalFIFOStatusIsIncompleteByte()) {
  2104. (*gRFAL.TxRx.ctx.rxRcvdLen) -=
  2105. (RFAL_BITS_IN_BYTE - rfalFIFOGetNumIncompleteBits());
  2106. }
  2107. }
  2108. #if RFAL_FEATURE_NFCV
  2109. /*******************************************************************************/
  2110. /* Decode sub bit stream into payload bits for NFCV, if no error found so far */
  2111. if(((RFAL_MODE_POLL_NFCV == gRFAL.mode) || (RFAL_MODE_POLL_PICOPASS == gRFAL.mode)) &&
  2112. (gRFAL.TxRx.status == ERR_BUSY)) {
  2113. ReturnCode ret;
  2114. uint16_t offset = 0; /* REMARK offset not currently used */
  2115. ret = iso15693VICCDecode(
  2116. gRFAL.TxRx.ctx.rxBuf,
  2117. gRFAL.fifo.bytesTotal,
  2118. gRFAL.nfcvData.origCtx.rxBuf,
  2119. rfalConvBitsToBytes(gRFAL.nfcvData.origCtx.rxBufLen),
  2120. &offset,
  2121. gRFAL.nfcvData.origCtx.rxRcvdLen,
  2122. gRFAL.nfcvData.ignoreBits,
  2123. (RFAL_MODE_POLL_PICOPASS == gRFAL.mode));
  2124. if(((ERR_NONE == ret) || (ERR_CRC == ret)) &&
  2125. (((uint32_t)RFAL_TXRX_FLAGS_CRC_RX_KEEP & gRFAL.nfcvData.origCtx.flags) == 0U) &&
  2126. ((*gRFAL.nfcvData.origCtx.rxRcvdLen % RFAL_BITS_IN_BYTE) == 0U) &&
  2127. (*gRFAL.nfcvData.origCtx.rxRcvdLen >= rfalConvBytesToBits(RFAL_CRC_LEN))) {
  2128. *gRFAL.nfcvData.origCtx.rxRcvdLen -=
  2129. (uint16_t)rfalConvBytesToBits(RFAL_CRC_LEN); /* Remove CRC */
  2130. }
  2131. #if 0
  2132. /* Debugging code: output the payload bits by writing into the FIFO and subsequent clearing */
  2133. st25r3916WriteFifo(gRFAL.nfcvData.origCtx.rxBuf, rfalConvBitsToBytes( *gRFAL.nfcvData.origCtx.rxRcvdLen));
  2134. st25r3916ExecuteCommand( ST25R3916_CMD_CLEAR_FIFO );
  2135. #endif
  2136. /* Restore original ctx */
  2137. gRFAL.TxRx.ctx = gRFAL.nfcvData.origCtx;
  2138. gRFAL.TxRx.status = ((ret != ERR_NONE) ? ret : ERR_BUSY);
  2139. }
  2140. #endif /* RFAL_FEATURE_NFCV */
  2141. /*******************************************************************************/
  2142. /* If an error as been marked/detected don't fall into to RX_DONE */
  2143. if(gRFAL.TxRx.status != ERR_BUSY) {
  2144. gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_FAIL;
  2145. break;
  2146. }
  2147. if(rfalIsModeActiveComm(gRFAL.mode)) {
  2148. gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_WAIT_EOF;
  2149. break;
  2150. }
  2151. gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_DONE;
  2152. /* fall through */
  2153. /*******************************************************************************/
  2154. case RFAL_TXRX_STATE_RX_DONE: /* PRQA S 2003 # MISRA 16.3 - Intentional fall through */
  2155. /*Check if Observation Mode was enabled and disable it on ST25R391x */
  2156. rfalCheckDisableObsMode();
  2157. /* Clean up Transceive */
  2158. rfalCleanupTransceive();
  2159. gRFAL.TxRx.status = ERR_NONE;
  2160. gRFAL.TxRx.state = RFAL_TXRX_STATE_IDLE;
  2161. break;
  2162. /*******************************************************************************/
  2163. case RFAL_TXRX_STATE_RX_READ_FIFO:
  2164. /*******************************************************************************/
  2165. /* REMARK: Silicon workaround ST25R3916 Errata #TBD */
  2166. /* Rarely on corrupted frames I_rxs gets signaled but I_rxe is not signaled */
  2167. /* Use a SW timer to handle an eventual missing RXE */
  2168. rfalTimerStart(gRFAL.tmr.RXE, RFAL_NORXE_TOUT);
  2169. /*******************************************************************************/
  2170. tmp = rfalFIFOStatusGetNumBytes();
  2171. gRFAL.fifo.bytesTotal += tmp;
  2172. /*******************************************************************************/
  2173. /* Calculate the amount of bytes that still fits in rxBuf */
  2174. aux =
  2175. ((gRFAL.fifo.bytesTotal > rfalConvBitsToBytes(gRFAL.TxRx.ctx.rxBufLen)) ?
  2176. (rfalConvBitsToBytes(gRFAL.TxRx.ctx.rxBufLen) - gRFAL.fifo.bytesWritten) :
  2177. tmp);
  2178. /*******************************************************************************/
  2179. /* Retrieve incoming bytes from FIFO to rxBuf, and store already read amount */
  2180. st25r3916ReadFifo(&gRFAL.TxRx.ctx.rxBuf[gRFAL.fifo.bytesWritten], aux);
  2181. gRFAL.fifo.bytesWritten += aux;
  2182. /*******************************************************************************/
  2183. /* If the bytes already read were not the full FIFO WL, dump the remaining *
  2184. * FIFO so that ST25R391x can continue with reception */
  2185. if(aux < tmp) {
  2186. st25r3916ReadFifo(NULL, (tmp - aux));
  2187. }
  2188. rfalFIFOStatusClear();
  2189. gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_WAIT_RXE;
  2190. break;
  2191. /*******************************************************************************/
  2192. case RFAL_TXRX_STATE_RX_FAIL:
  2193. /*Check if Observation Mode was enabled and disable it on ST25R391x */
  2194. rfalCheckDisableObsMode();
  2195. /* Clean up Transceive */
  2196. rfalCleanupTransceive();
  2197. /* Error should be assigned by previous state */
  2198. if(gRFAL.TxRx.status == ERR_BUSY) {
  2199. gRFAL.TxRx.status = ERR_SYSTEM;
  2200. }
  2201. /*rfalLogD( "RFAL: curSt: %d Error: %d \r\n", gRFAL.TxRx.state, gRFAL.TxRx.status );*/
  2202. gRFAL.TxRx.state = RFAL_TXRX_STATE_IDLE;
  2203. break;
  2204. /*******************************************************************************/
  2205. case RFAL_TXRX_STATE_RX_WAIT_EON:
  2206. irqs = st25r3916GetInterrupt(
  2207. (ST25R3916_IRQ_MASK_EON | ST25R3916_IRQ_MASK_NRE | ST25R3916_IRQ_MASK_PPON2));
  2208. if(irqs == ST25R3916_IRQ_MASK_NONE) {
  2209. break; /* No interrupt to process */
  2210. }
  2211. if((irqs & ST25R3916_IRQ_MASK_EON) != 0U) {
  2212. gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_WAIT_RXS;
  2213. }
  2214. if((irqs & ST25R3916_IRQ_MASK_NRE) != 0U) {
  2215. gRFAL.TxRx.status = ERR_TIMEOUT;
  2216. gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_FAIL;
  2217. }
  2218. if((irqs & ST25R3916_IRQ_MASK_PPON2) != 0U) {
  2219. gRFAL.TxRx.status = ERR_LINK_LOSS;
  2220. gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_FAIL;
  2221. }
  2222. break;
  2223. /*******************************************************************************/
  2224. case RFAL_TXRX_STATE_RX_WAIT_EOF:
  2225. irqs = st25r3916GetInterrupt((ST25R3916_IRQ_MASK_CAT | ST25R3916_IRQ_MASK_CAC));
  2226. if(irqs == ST25R3916_IRQ_MASK_NONE) {
  2227. break; /* No interrupt to process */
  2228. }
  2229. if((irqs & ST25R3916_IRQ_MASK_CAT) != 0U) {
  2230. gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_DONE;
  2231. } else if((irqs & ST25R3916_IRQ_MASK_CAC) != 0U) {
  2232. gRFAL.TxRx.status = ERR_RF_COLLISION;
  2233. gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_FAIL;
  2234. } else {
  2235. gRFAL.TxRx.status = ERR_IO;
  2236. gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_FAIL;
  2237. }
  2238. break;
  2239. /*******************************************************************************/
  2240. default:
  2241. gRFAL.TxRx.status = ERR_SYSTEM;
  2242. gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_FAIL;
  2243. break;
  2244. }
  2245. }
  2246. /*******************************************************************************/
  2247. static void rfalFIFOStatusUpdate(void) {
  2248. if(gRFAL.fifo.status[RFAL_FIFO_STATUS_REG2] == RFAL_FIFO_STATUS_INVALID) {
  2249. st25r3916ReadMultipleRegisters(
  2250. ST25R3916_REG_FIFO_STATUS1, gRFAL.fifo.status, ST25R3916_FIFO_STATUS_LEN);
  2251. }
  2252. }
  2253. /*******************************************************************************/
  2254. static void rfalFIFOStatusClear(void) {
  2255. gRFAL.fifo.status[RFAL_FIFO_STATUS_REG2] = RFAL_FIFO_STATUS_INVALID;
  2256. }
  2257. /*******************************************************************************/
  2258. static uint16_t rfalFIFOStatusGetNumBytes(void) {
  2259. uint16_t result;
  2260. rfalFIFOStatusUpdate();
  2261. result =
  2262. ((((uint16_t)gRFAL.fifo.status[RFAL_FIFO_STATUS_REG2] &
  2263. ST25R3916_REG_FIFO_STATUS2_fifo_b_mask) >>
  2264. ST25R3916_REG_FIFO_STATUS2_fifo_b_shift)
  2265. << RFAL_BITS_IN_BYTE);
  2266. result |= (((uint16_t)gRFAL.fifo.status[RFAL_FIFO_STATUS_REG1]) & 0x00FFU);
  2267. return result;
  2268. }
  2269. /*******************************************************************************/
  2270. static bool rfalFIFOStatusIsIncompleteByte(void) {
  2271. rfalFIFOStatusUpdate();
  2272. return (
  2273. (gRFAL.fifo.status[RFAL_FIFO_STATUS_REG2] & ST25R3916_REG_FIFO_STATUS2_fifo_lb_mask) !=
  2274. 0U);
  2275. }
  2276. /*******************************************************************************/
  2277. static bool rfalFIFOStatusIsMissingPar(void) {
  2278. rfalFIFOStatusUpdate();
  2279. return ((gRFAL.fifo.status[RFAL_FIFO_STATUS_REG2] & ST25R3916_REG_FIFO_STATUS2_np_lb) != 0U);
  2280. }
  2281. /*******************************************************************************/
  2282. static uint8_t rfalFIFOGetNumIncompleteBits(void) {
  2283. rfalFIFOStatusUpdate();
  2284. return (
  2285. (gRFAL.fifo.status[RFAL_FIFO_STATUS_REG2] & ST25R3916_REG_FIFO_STATUS2_fifo_lb_mask) >>
  2286. ST25R3916_REG_FIFO_STATUS2_fifo_lb_shift);
  2287. }
  2288. #if RFAL_FEATURE_NFCA
  2289. /*******************************************************************************/
  2290. ReturnCode rfalISO14443ATransceiveShortFrame(
  2291. rfal14443AShortFrameCmd txCmd,
  2292. uint8_t* rxBuf,
  2293. uint8_t rxBufLen,
  2294. uint16_t* rxRcvdLen,
  2295. uint32_t fwt) {
  2296. ReturnCode ret;
  2297. uint8_t directCmd;
  2298. /* Check if RFAL is properly initialized */
  2299. if(!st25r3916IsTxEnabled() || (gRFAL.state < RFAL_STATE_MODE_SET) ||
  2300. ((gRFAL.mode != RFAL_MODE_POLL_NFCA) && (gRFAL.mode != RFAL_MODE_POLL_NFCA_T1T))) {
  2301. return ERR_WRONG_STATE;
  2302. }
  2303. /* Check for valid parameters */
  2304. if((rxBuf == NULL) || (rxRcvdLen == NULL) || (fwt == RFAL_FWT_NONE)) {
  2305. return ERR_PARAM;
  2306. }
  2307. /*******************************************************************************/
  2308. /* Select the Direct Command to be performed */
  2309. switch(txCmd) {
  2310. case RFAL_14443A_SHORTFRAME_CMD_WUPA:
  2311. directCmd = ST25R3916_CMD_TRANSMIT_WUPA;
  2312. break;
  2313. case RFAL_14443A_SHORTFRAME_CMD_REQA:
  2314. directCmd = ST25R3916_CMD_TRANSMIT_REQA;
  2315. break;
  2316. default:
  2317. return ERR_PARAM;
  2318. }
  2319. /* Disable CRC while receiving since ATQA has no CRC included */
  2320. st25r3916SetRegisterBits(ST25R3916_REG_AUX, ST25R3916_REG_AUX_no_crc_rx);
  2321. /*******************************************************************************/
  2322. /* Wait for GT and FDT */
  2323. while(!rfalIsGTExpired()) { /* MISRA 15.6: mandatory brackets */
  2324. };
  2325. while(st25r3916IsGPTRunning()) { /* MISRA 15.6: mandatory brackets */
  2326. };
  2327. rfalTimerDestroy(gRFAL.tmr.GT);
  2328. gRFAL.tmr.GT = RFAL_TIMING_NONE;
  2329. /*******************************************************************************/
  2330. /* Prepare for Transceive, Receive only (bypass Tx states) */
  2331. gRFAL.TxRx.ctx.flags =
  2332. ((uint32_t)RFAL_TXRX_FLAGS_CRC_TX_MANUAL | (uint32_t)RFAL_TXRX_FLAGS_CRC_RX_KEEP);
  2333. gRFAL.TxRx.ctx.rxBuf = rxBuf;
  2334. gRFAL.TxRx.ctx.rxBufLen = rxBufLen;
  2335. gRFAL.TxRx.ctx.rxRcvdLen = rxRcvdLen;
  2336. gRFAL.TxRx.ctx.fwt = fwt;
  2337. /*******************************************************************************/
  2338. /* Load NRT with FWT */
  2339. st25r3916SetNoResponseTime(rfalConv1fcTo64fc(
  2340. MIN((fwt + RFAL_FWT_ADJUSTMENT + RFAL_FWT_A_ADJUSTMENT), RFAL_ST25R3916_NRT_MAX_1FC)));
  2341. if(gRFAL.timings.FDTListen != RFAL_TIMING_NONE) {
  2342. /* Ensure that MRT is using 64/fc steps */
  2343. st25r3916ClrRegisterBits(
  2344. ST25R3916_REG_TIMER_EMV_CONTROL, ST25R3916_REG_TIMER_EMV_CONTROL_mrt_step);
  2345. /* Set Minimum FDT(Listen) in which PICC is not allowed to send a response */
  2346. st25r3916WriteRegister(
  2347. ST25R3916_REG_MASK_RX_TIMER,
  2348. (uint8_t)rfalConv1fcTo64fc(
  2349. ((RFAL_FDT_LISTEN_MRT_ADJUSTMENT + RFAL_FDT_LISTEN_A_ADJUSTMENT) >
  2350. gRFAL.timings.FDTListen) ?
  2351. RFAL_ST25R3916_MRT_MIN_1FC :
  2352. (gRFAL.timings.FDTListen -
  2353. (RFAL_FDT_LISTEN_MRT_ADJUSTMENT + RFAL_FDT_LISTEN_A_ADJUSTMENT))));
  2354. }
  2355. /* In Passive communications General Purpose Timer is used to measure FDT Poll */
  2356. if(gRFAL.timings.FDTPoll != RFAL_TIMING_NONE) {
  2357. /* Configure GPT to start at RX end */
  2358. st25r3916SetStartGPTimer(
  2359. (uint16_t)rfalConv1fcTo8fc(
  2360. MIN(gRFAL.timings.FDTPoll, (gRFAL.timings.FDTPoll - RFAL_FDT_POLL_ADJUSTMENT))),
  2361. ST25R3916_REG_TIMER_EMV_CONTROL_gptc_erx);
  2362. }
  2363. /*******************************************************************************/
  2364. rfalPrepareTransceive();
  2365. /* Also enable bit collision interrupt */
  2366. st25r3916GetInterrupt(ST25R3916_IRQ_MASK_COL);
  2367. st25r3916EnableInterrupts(ST25R3916_IRQ_MASK_COL);
  2368. /*Check if Observation Mode is enabled and set it on ST25R391x */
  2369. rfalCheckEnableObsModeTx();
  2370. /*******************************************************************************/
  2371. /* Clear nbtx bits before sending WUPA/REQA - otherwise ST25R3916 will report parity error, Note2 of the register */
  2372. st25r3916WriteRegister(ST25R3916_REG_NUM_TX_BYTES2, 0);
  2373. /* Send either WUPA or REQA. All affected tags will backscatter ATQA and change to READY state */
  2374. st25r3916ExecuteCommand(directCmd);
  2375. /* Wait for TXE */
  2376. if(st25r3916WaitForInterruptsTimed(
  2377. ST25R3916_IRQ_MASK_TXE,
  2378. (uint16_t)MAX(rfalConv1fcToMs(fwt), RFAL_ST25R3916_SW_TMR_MIN_1MS)) == 0U) {
  2379. ret = ERR_IO;
  2380. } else {
  2381. /*Check if Observation Mode is enabled and set it on ST25R391x */
  2382. rfalCheckEnableObsModeRx();
  2383. /* Jump into a transceive Rx state for reception (bypass Tx states) */
  2384. gRFAL.state = RFAL_STATE_TXRX;
  2385. gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_IDLE;
  2386. gRFAL.TxRx.status = ERR_BUSY;
  2387. /* Execute Transceive Rx blocking */
  2388. ret = rfalTransceiveBlockingRx();
  2389. }
  2390. /* Disable Collision interrupt */
  2391. st25r3916DisableInterrupts((ST25R3916_IRQ_MASK_COL));
  2392. /* ReEnable CRC on Rx */
  2393. st25r3916ClrRegisterBits(ST25R3916_REG_AUX, ST25R3916_REG_AUX_no_crc_rx);
  2394. return ret;
  2395. }
  2396. /*******************************************************************************/
  2397. ReturnCode rfalISO14443ATransceiveAnticollisionFrame(
  2398. uint8_t* buf,
  2399. uint8_t* bytesToSend,
  2400. uint8_t* bitsToSend,
  2401. uint16_t* rxLength,
  2402. uint32_t fwt) {
  2403. ReturnCode ret;
  2404. rfalTransceiveContext ctx;
  2405. uint8_t collByte;
  2406. uint8_t collData;
  2407. /* Check if RFAL is properly initialized */
  2408. if((gRFAL.state < RFAL_STATE_MODE_SET) || (gRFAL.mode != RFAL_MODE_POLL_NFCA)) {
  2409. return ERR_WRONG_STATE;
  2410. }
  2411. /* Check for valid parameters */
  2412. if((buf == NULL) || (bytesToSend == NULL) || (bitsToSend == NULL) || (rxLength == NULL)) {
  2413. return ERR_PARAM;
  2414. }
  2415. /*******************************************************************************/
  2416. /* Set speficic Analog Config for Anticolission if needed */
  2417. rfalSetAnalogConfig(
  2418. (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCA |
  2419. RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_ANTICOL));
  2420. /*******************************************************************************/
  2421. /* Enable anti collision to recognise collision in first byte of SENS_REQ */
  2422. st25r3916SetRegisterBits(ST25R3916_REG_ISO14443A_NFC, ST25R3916_REG_ISO14443A_NFC_antcl);
  2423. /* Disable CRC while receiving */
  2424. st25r3916SetRegisterBits(ST25R3916_REG_AUX, ST25R3916_REG_AUX_no_crc_rx);
  2425. /*******************************************************************************/
  2426. /* Prepare for Transceive */
  2427. ctx.flags = ((uint32_t)RFAL_TXRX_FLAGS_CRC_TX_MANUAL | (uint32_t)RFAL_TXRX_FLAGS_CRC_RX_KEEP);
  2428. ctx.txBuf = buf;
  2429. ctx.txBufLen = (uint16_t)(rfalConvBytesToBits(*bytesToSend) + *bitsToSend);
  2430. ctx.rxBuf = &buf[*bytesToSend];
  2431. ctx.rxBufLen = (uint16_t)rfalConvBytesToBits(RFAL_ISO14443A_SDD_RES_LEN);
  2432. ctx.rxRcvdLen = rxLength;
  2433. ctx.fwt = fwt;
  2434. /* Disable Automatic Gain Control (AGC) for better detection of collisions if using Coherent Receiver */
  2435. ctx.flags |=
  2436. (st25r3916CheckReg(
  2437. ST25R3916_REG_AUX, ST25R3916_REG_AUX_dis_corr, ST25R3916_REG_AUX_dis_corr) ?
  2438. (uint32_t)RFAL_TXRX_FLAGS_AGC_OFF :
  2439. 0x00U);
  2440. rfalStartTransceive(&ctx);
  2441. /* Additionally enable bit collision interrupt */
  2442. st25r3916GetInterrupt(ST25R3916_IRQ_MASK_COL);
  2443. st25r3916EnableInterrupts(ST25R3916_IRQ_MASK_COL);
  2444. /*******************************************************************************/
  2445. collByte = 0;
  2446. /* save the collision byte */
  2447. if((*bitsToSend) > 0U) {
  2448. buf[(*bytesToSend)] <<= (RFAL_BITS_IN_BYTE - (*bitsToSend));
  2449. buf[(*bytesToSend)] >>= (RFAL_BITS_IN_BYTE - (*bitsToSend));
  2450. collByte = buf[(*bytesToSend)];
  2451. }
  2452. /*******************************************************************************/
  2453. /* Run Transceive blocking */
  2454. ret = rfalTransceiveRunBlockingTx();
  2455. if(ret == ERR_NONE) {
  2456. ret = rfalTransceiveBlockingRx();
  2457. /*******************************************************************************/
  2458. if((*bitsToSend) > 0U) {
  2459. buf[(*bytesToSend)] >>= (*bitsToSend);
  2460. buf[(*bytesToSend)] <<= (*bitsToSend);
  2461. buf[(*bytesToSend)] |= collByte;
  2462. }
  2463. if((ERR_RF_COLLISION == ret)) {
  2464. /* read out collision register */
  2465. st25r3916ReadRegister(ST25R3916_REG_COLLISION_STATUS, &collData);
  2466. (*bytesToSend) =
  2467. ((collData >> ST25R3916_REG_COLLISION_STATUS_c_byte_shift) &
  2468. 0x0FU); // 4-bits Byte information
  2469. (*bitsToSend) =
  2470. ((collData >> ST25R3916_REG_COLLISION_STATUS_c_bit_shift) &
  2471. 0x07U); // 3-bits bit information
  2472. }
  2473. }
  2474. /*******************************************************************************/
  2475. /* Disable Collision interrupt */
  2476. st25r3916DisableInterrupts((ST25R3916_IRQ_MASK_COL));
  2477. /* Disable anti collision again */
  2478. st25r3916ClrRegisterBits(ST25R3916_REG_ISO14443A_NFC, ST25R3916_REG_ISO14443A_NFC_antcl);
  2479. /* ReEnable CRC on Rx */
  2480. st25r3916ClrRegisterBits(ST25R3916_REG_AUX, ST25R3916_REG_AUX_no_crc_rx);
  2481. /*******************************************************************************/
  2482. /* Restore common Analog configurations for this mode */
  2483. rfalSetAnalogConfig(
  2484. (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCA | rfalConvBR2ACBR(gRFAL.txBR) |
  2485. RFAL_ANALOG_CONFIG_TX));
  2486. rfalSetAnalogConfig(
  2487. (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCA | rfalConvBR2ACBR(gRFAL.rxBR) |
  2488. RFAL_ANALOG_CONFIG_RX));
  2489. return ret;
  2490. }
  2491. #endif /* RFAL_FEATURE_NFCA */
  2492. #if RFAL_FEATURE_NFCV
  2493. /*******************************************************************************/
  2494. ReturnCode rfalISO15693TransceiveAnticollisionFrame(
  2495. uint8_t* txBuf,
  2496. uint8_t txBufLen,
  2497. uint8_t* rxBuf,
  2498. uint8_t rxBufLen,
  2499. uint16_t* actLen) {
  2500. ReturnCode ret;
  2501. rfalTransceiveContext ctx;
  2502. /* Check if RFAL is properly initialized */
  2503. if((gRFAL.state < RFAL_STATE_MODE_SET) || (gRFAL.mode != RFAL_MODE_POLL_NFCV)) {
  2504. return ERR_WRONG_STATE;
  2505. }
  2506. /*******************************************************************************/
  2507. /* Set speficic Analog Config for Anticolission if needed */
  2508. rfalSetAnalogConfig(
  2509. (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCV |
  2510. RFAL_ANALOG_CONFIG_BITRATE_COMMON | RFAL_ANALOG_CONFIG_ANTICOL));
  2511. /* Ignoring collisions before the UID (RES_FLAG + DSFID) */
  2512. gRFAL.nfcvData.ignoreBits = (uint16_t)RFAL_ISO15693_IGNORE_BITS;
  2513. /*******************************************************************************/
  2514. /* Prepare for Transceive */
  2515. ctx.flags =
  2516. ((txBufLen == 0U) ? (uint32_t)RFAL_TXRX_FLAGS_CRC_TX_MANUAL :
  2517. (uint32_t)RFAL_TXRX_FLAGS_CRC_TX_AUTO) |
  2518. (uint32_t)RFAL_TXRX_FLAGS_CRC_RX_KEEP | (uint32_t)RFAL_TXRX_FLAGS_AGC_OFF |
  2519. ((txBufLen == 0U) ?
  2520. (uint32_t)RFAL_TXRX_FLAGS_NFCV_FLAG_MANUAL :
  2521. (uint32_t)
  2522. RFAL_TXRX_FLAGS_NFCV_FLAG_AUTO); /* Disable Automatic Gain Control (AGC) for better detection of collision */
  2523. ctx.txBuf = txBuf;
  2524. ctx.txBufLen = (uint16_t)rfalConvBytesToBits(txBufLen);
  2525. ctx.rxBuf = rxBuf;
  2526. ctx.rxBufLen = (uint16_t)rfalConvBytesToBits(rxBufLen);
  2527. ctx.rxRcvdLen = actLen;
  2528. ctx.fwt = rfalConv64fcTo1fc(ISO15693_FWT);
  2529. rfalStartTransceive(&ctx);
  2530. /*******************************************************************************/
  2531. /* Run Transceive blocking */
  2532. ret = rfalTransceiveRunBlockingTx();
  2533. if(ret == ERR_NONE) {
  2534. ret = rfalTransceiveBlockingRx();
  2535. }
  2536. /* Check if a Transmission error and received data is less then expected */
  2537. if(((ret == ERR_RF_COLLISION) || (ret == ERR_CRC) || (ret == ERR_FRAMING)) &&
  2538. (rfalConvBitsToBytes(*ctx.rxRcvdLen) < RFAL_ISO15693_INV_RES_LEN)) {
  2539. /* If INVENTORY_RES is shorter than expected, tag is still modulating *
  2540. * Ensure that response is complete before next frame */
  2541. platformDelay((
  2542. uint8_t)((RFAL_ISO15693_INV_RES_LEN - rfalConvBitsToBytes(*ctx.rxRcvdLen)) / ((RFAL_ISO15693_INV_RES_LEN / RFAL_ISO15693_INV_RES_DUR) + 1U)));
  2543. }
  2544. /* Restore common Analog configurations for this mode */
  2545. rfalSetAnalogConfig(
  2546. (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCV | rfalConvBR2ACBR(gRFAL.txBR) |
  2547. RFAL_ANALOG_CONFIG_TX));
  2548. rfalSetAnalogConfig(
  2549. (RFAL_ANALOG_CONFIG_POLL | RFAL_ANALOG_CONFIG_TECH_NFCV | rfalConvBR2ACBR(gRFAL.rxBR) |
  2550. RFAL_ANALOG_CONFIG_RX));
  2551. gRFAL.nfcvData.ignoreBits = 0;
  2552. return ret;
  2553. }
  2554. /*******************************************************************************/
  2555. ReturnCode
  2556. rfalISO15693TransceiveEOFAnticollision(uint8_t* rxBuf, uint8_t rxBufLen, uint16_t* actLen) {
  2557. uint8_t dummy;
  2558. return rfalISO15693TransceiveAnticollisionFrame(&dummy, 0, rxBuf, rxBufLen, actLen);
  2559. }
  2560. /*******************************************************************************/
  2561. ReturnCode rfalISO15693TransceiveEOF(uint8_t* rxBuf, uint8_t rxBufLen, uint16_t* actLen) {
  2562. ReturnCode ret;
  2563. uint8_t dummy;
  2564. /* Check if RFAL is properly initialized */
  2565. if((gRFAL.state < RFAL_STATE_MODE_SET) || (gRFAL.mode != RFAL_MODE_POLL_NFCV)) {
  2566. return ERR_WRONG_STATE;
  2567. }
  2568. /*******************************************************************************/
  2569. /* Run Transceive blocking */
  2570. ret = rfalTransceiveBlockingTxRx(
  2571. &dummy,
  2572. 0,
  2573. rxBuf,
  2574. rxBufLen,
  2575. actLen,
  2576. ((uint32_t)RFAL_TXRX_FLAGS_CRC_TX_MANUAL | (uint32_t)RFAL_TXRX_FLAGS_CRC_RX_KEEP |
  2577. (uint32_t)RFAL_TXRX_FLAGS_AGC_ON),
  2578. rfalConv64fcTo1fc(ISO15693_FWT));
  2579. return ret;
  2580. }
  2581. #endif /* RFAL_FEATURE_NFCV */
  2582. #if RFAL_FEATURE_NFCF
  2583. /*******************************************************************************/
  2584. ReturnCode rfalFeliCaPoll(
  2585. rfalFeliCaPollSlots slots,
  2586. uint16_t sysCode,
  2587. uint8_t reqCode,
  2588. rfalFeliCaPollRes* pollResList,
  2589. uint8_t pollResListSize,
  2590. uint8_t* devicesDetected,
  2591. uint8_t* collisionsDetected) {
  2592. ReturnCode ret;
  2593. uint8_t frame
  2594. [RFAL_FELICA_POLL_REQ_LEN - RFAL_FELICA_LEN_LEN]; // LEN is added by ST25R391x automatically
  2595. uint16_t actLen;
  2596. uint8_t frameIdx;
  2597. uint8_t devDetected;
  2598. uint8_t colDetected;
  2599. rfalEHandling curHandling;
  2600. uint8_t nbSlots;
  2601. /* Check if RFAL is properly initialized */
  2602. if((gRFAL.state < RFAL_STATE_MODE_SET) || (gRFAL.mode != RFAL_MODE_POLL_NFCF)) {
  2603. return ERR_WRONG_STATE;
  2604. }
  2605. frameIdx = 0;
  2606. colDetected = 0;
  2607. devDetected = 0;
  2608. nbSlots = (uint8_t)slots;
  2609. /*******************************************************************************/
  2610. /* Compute SENSF_REQ frame */
  2611. frame[frameIdx++] = (uint8_t)FELICA_CMD_POLLING; /* CMD: SENF_REQ */
  2612. frame[frameIdx++] = (uint8_t)(sysCode >> 8); /* System Code (SC) */
  2613. frame[frameIdx++] = (uint8_t)(sysCode & 0xFFU); /* System Code (SC) */
  2614. frame[frameIdx++] = reqCode; /* Communication Parameter Request (RC)*/
  2615. frame[frameIdx++] = nbSlots; /* TimeSlot (TSN) */
  2616. /*******************************************************************************/
  2617. /* NRT should not stop on reception - Use EMVCo mode to run NRT in nrt_emv *
  2618. * ERRORHANDLING_EMVCO has no special handling for NFC-F mode */
  2619. curHandling = gRFAL.conf.eHandling;
  2620. rfalSetErrorHandling(RFAL_ERRORHANDLING_EMVCO);
  2621. /*******************************************************************************/
  2622. /* Run transceive blocking,
  2623. * Calculate Total Response Time in(64/fc):
  2624. * 512 PICC process time + (n * 256 Time Slot duration) */
  2625. ret = rfalTransceiveBlockingTx(
  2626. frame,
  2627. (uint16_t)frameIdx,
  2628. (uint8_t*)gRFAL.nfcfData.pollResponses,
  2629. RFAL_FELICA_POLL_RES_LEN,
  2630. &actLen,
  2631. (RFAL_TXRX_FLAGS_DEFAULT),
  2632. rfalConv64fcTo1fc(
  2633. RFAL_FELICA_POLL_DELAY_TIME +
  2634. (RFAL_FELICA_POLL_SLOT_TIME * ((uint32_t)nbSlots + 1U))));
  2635. /*******************************************************************************/
  2636. /* If Tx OK, Wait for all responses, store them as soon as they appear */
  2637. if(ret == ERR_NONE) {
  2638. bool timeout;
  2639. do {
  2640. ret = rfalTransceiveBlockingRx();
  2641. if(ret == ERR_TIMEOUT) {
  2642. /* Upon timeout the full Poll Delay + (Slot time)*(nbSlots) has expired */
  2643. timeout = true;
  2644. } else {
  2645. /* Reception done, reEnabled Rx for following Slot */
  2646. st25r3916ExecuteCommand(ST25R3916_CMD_UNMASK_RECEIVE_DATA);
  2647. st25r3916ExecuteCommand(ST25R3916_CMD_RESET_RXGAIN);
  2648. /* If the reception was OK, new device found */
  2649. if(ret == ERR_NONE) {
  2650. devDetected++;
  2651. /* Overwrite the Transceive context for the next reception */
  2652. gRFAL.TxRx.ctx.rxBuf = (uint8_t*)gRFAL.nfcfData.pollResponses[devDetected];
  2653. }
  2654. /* If the reception was not OK, mark as collision */
  2655. else {
  2656. colDetected++;
  2657. }
  2658. /* Check whether NRT has expired meanwhile */
  2659. timeout = st25r3916CheckReg(
  2660. ST25R3916_REG_NFCIP1_BIT_RATE, ST25R3916_REG_NFCIP1_BIT_RATE_nrt_on, 0x00);
  2661. if(!timeout) {
  2662. /* Jump again into transceive Rx state for the following reception */
  2663. gRFAL.TxRx.status = ERR_BUSY;
  2664. gRFAL.state = RFAL_STATE_TXRX;
  2665. gRFAL.TxRx.state = RFAL_TXRX_STATE_RX_IDLE;
  2666. }
  2667. }
  2668. } while(((nbSlots--) != 0U) && !timeout);
  2669. }
  2670. /*******************************************************************************/
  2671. /* Restore NRT to normal mode - back to previous error handling */
  2672. rfalSetErrorHandling(curHandling);
  2673. /*******************************************************************************/
  2674. /* Assign output parameters if requested */
  2675. if((pollResList != NULL) && (pollResListSize > 0U) && (devDetected > 0U)) {
  2676. ST_MEMCPY(
  2677. pollResList,
  2678. gRFAL.nfcfData.pollResponses,
  2679. (RFAL_FELICA_POLL_RES_LEN * (uint32_t)MIN(pollResListSize, devDetected)));
  2680. }
  2681. if(devicesDetected != NULL) {
  2682. *devicesDetected = devDetected;
  2683. }
  2684. if(collisionsDetected != NULL) {
  2685. *collisionsDetected = colDetected;
  2686. }
  2687. return (((colDetected != 0U) || (devDetected != 0U)) ? ERR_NONE : ret);
  2688. }
  2689. #endif /* RFAL_FEATURE_NFCF */
  2690. /*****************************************************************************
  2691. * Listen Mode *
  2692. *****************************************************************************/
  2693. /*******************************************************************************/
  2694. bool rfalIsExtFieldOn(void) {
  2695. return st25r3916IsExtFieldOn();
  2696. }
  2697. #if RFAL_FEATURE_LISTEN_MODE
  2698. /*******************************************************************************/
  2699. ReturnCode rfalListenStart(
  2700. uint32_t lmMask,
  2701. const rfalLmConfPA* confA,
  2702. const rfalLmConfPB* confB,
  2703. const rfalLmConfPF* confF,
  2704. uint8_t* rxBuf,
  2705. uint16_t rxBufLen,
  2706. uint16_t* rxLen) {
  2707. t_rfalPTMem
  2708. PTMem; /* PRQA S 0759 # MISRA 19.2 - Allocating Union where members are of the same type, just different names. Thus no problem can occur. */
  2709. uint8_t* pPTMem;
  2710. uint8_t autoResp;
  2711. /* Check if RFAL is initialized */
  2712. if(gRFAL.state < RFAL_STATE_INIT) {
  2713. return ERR_WRONG_STATE;
  2714. }
  2715. gRFAL.Lm.state = RFAL_LM_STATE_NOT_INIT;
  2716. gRFAL.Lm.mdIrqs = ST25R3916_IRQ_MASK_NONE;
  2717. gRFAL.Lm.mdReg =
  2718. (ST25R3916_REG_MODE_targ_init | ST25R3916_REG_MODE_om_nfc | ST25R3916_REG_MODE_nfc_ar_off);
  2719. /* By default disable all automatic responses */
  2720. autoResp =
  2721. (ST25R3916_REG_PASSIVE_TARGET_d_106_ac_a | ST25R3916_REG_PASSIVE_TARGET_rfu |
  2722. ST25R3916_REG_PASSIVE_TARGET_d_212_424_1r | ST25R3916_REG_PASSIVE_TARGET_d_ac_ap2p);
  2723. /*******************************************************************************/
  2724. if((lmMask & RFAL_LM_MASK_NFCA) != 0U) {
  2725. /* Check if the conf has been provided */
  2726. if(confA == NULL) {
  2727. return ERR_PARAM;
  2728. }
  2729. pPTMem = (uint8_t*)PTMem.PTMem_A;
  2730. /*******************************************************************************/
  2731. /* Check and set supported NFCID Length */
  2732. switch(confA->nfcidLen) {
  2733. case RFAL_LM_NFCID_LEN_04:
  2734. st25r3916ChangeRegisterBits(
  2735. ST25R3916_REG_AUX, ST25R3916_REG_AUX_nfc_id_mask, ST25R3916_REG_AUX_nfc_id_4bytes);
  2736. break;
  2737. case RFAL_LM_NFCID_LEN_07:
  2738. st25r3916ChangeRegisterBits(
  2739. ST25R3916_REG_AUX, ST25R3916_REG_AUX_nfc_id_mask, ST25R3916_REG_AUX_nfc_id_7bytes);
  2740. break;
  2741. default:
  2742. return ERR_PARAM;
  2743. }
  2744. /*******************************************************************************/
  2745. /* Set NFCID */
  2746. ST_MEMCPY(pPTMem, confA->nfcid, RFAL_NFCID1_TRIPLE_LEN);
  2747. pPTMem = &pPTMem[RFAL_NFCID1_TRIPLE_LEN]; /* MISRA 18.4 */
  2748. /* Set SENS_RES */
  2749. ST_MEMCPY(pPTMem, confA->SENS_RES, RFAL_LM_SENS_RES_LEN);
  2750. pPTMem = &pPTMem[RFAL_LM_SENS_RES_LEN]; /* MISRA 18.4 */
  2751. /* Set SEL_RES */
  2752. *pPTMem++ =
  2753. ((confA->nfcidLen == RFAL_LM_NFCID_LEN_04) ?
  2754. (confA->SEL_RES & ~RFAL_LM_NFCID_INCOMPLETE) :
  2755. (confA->SEL_RES | RFAL_LM_NFCID_INCOMPLETE));
  2756. *pPTMem++ = (confA->SEL_RES & ~RFAL_LM_NFCID_INCOMPLETE);
  2757. *pPTMem++ = (confA->SEL_RES & ~RFAL_LM_NFCID_INCOMPLETE);
  2758. /* Write into PTMem-A */
  2759. st25r3916WritePTMem(PTMem.PTMem_A, ST25R3916_PTM_A_LEN);
  2760. /*******************************************************************************/
  2761. /* Enable automatic responses for A */
  2762. autoResp &= ~ST25R3916_REG_PASSIVE_TARGET_d_106_ac_a;
  2763. /* Set Target mode, Bit Rate detection and Listen Mode for NFC-F */
  2764. gRFAL.Lm.mdReg |=
  2765. (ST25R3916_REG_MODE_targ_targ | ST25R3916_REG_MODE_om3 | ST25R3916_REG_MODE_om0 |
  2766. ST25R3916_REG_MODE_nfc_ar_off);
  2767. gRFAL.Lm.mdIrqs |=
  2768. (ST25R3916_IRQ_MASK_WU_A | ST25R3916_IRQ_MASK_WU_A_X | ST25R3916_IRQ_MASK_RXE_PTA);
  2769. }
  2770. /*******************************************************************************/
  2771. if((lmMask & RFAL_LM_MASK_NFCB) != 0U) {
  2772. /* Check if the conf has been provided */
  2773. if(confB == NULL) {
  2774. return ERR_PARAM;
  2775. }
  2776. return ERR_NOTSUPP;
  2777. }
  2778. /*******************************************************************************/
  2779. if((lmMask & RFAL_LM_MASK_NFCF) != 0U) {
  2780. pPTMem = (uint8_t*)PTMem.PTMem_F;
  2781. /* Check if the conf has been provided */
  2782. if(confF == NULL) {
  2783. return ERR_PARAM;
  2784. }
  2785. /*******************************************************************************/
  2786. /* Set System Code */
  2787. ST_MEMCPY(pPTMem, confF->SC, RFAL_LM_SENSF_SC_LEN);
  2788. pPTMem = &pPTMem[RFAL_LM_SENSF_SC_LEN]; /* MISRA 18.4 */
  2789. /* Set SENSF_RES */
  2790. ST_MEMCPY(pPTMem, confF->SENSF_RES, RFAL_LM_SENSF_RES_LEN);
  2791. /* Set RD bytes to 0x00 as ST25R3916 cannot support advances features */
  2792. pPTMem[RFAL_LM_SENSF_RD0_POS] =
  2793. 0x00; /* NFC Forum Digital 1.1 Table 46: 0x00 */
  2794. pPTMem[RFAL_LM_SENSF_RD1_POS] =
  2795. 0x00; /* NFC Forum Digital 1.1 Table 47: No automatic bit rates */
  2796. pPTMem = &pPTMem[RFAL_LM_SENS_RES_LEN]; /* MISRA 18.4 */
  2797. /* Write into PTMem-F */
  2798. st25r3916WritePTMemF(PTMem.PTMem_F, ST25R3916_PTM_F_LEN);
  2799. /*******************************************************************************/
  2800. /* Write 24 TSN "Random" Numbers at first initialization and let it rollover */
  2801. if(!gRFAL.Lm.iniFlag) {
  2802. pPTMem = (uint8_t*)PTMem.TSN;
  2803. *pPTMem++ = 0x12;
  2804. *pPTMem++ = 0x34;
  2805. *pPTMem++ = 0x56;
  2806. *pPTMem++ = 0x78;
  2807. *pPTMem++ = 0x9A;
  2808. *pPTMem++ = 0xBC;
  2809. *pPTMem++ = 0xDF;
  2810. *pPTMem++ = 0x21;
  2811. *pPTMem++ = 0x43;
  2812. *pPTMem++ = 0x65;
  2813. *pPTMem++ = 0x87;
  2814. *pPTMem++ = 0xA9;
  2815. /* Write into PTMem-TSN */
  2816. st25r3916WritePTMemTSN(PTMem.TSN, ST25R3916_PTM_TSN_LEN);
  2817. }
  2818. /*******************************************************************************/
  2819. /* Enable automatic responses for F */
  2820. autoResp &= ~(ST25R3916_REG_PASSIVE_TARGET_d_212_424_1r);
  2821. /* Set Target mode, Bit Rate detection and Listen Mode for NFC-F */
  2822. gRFAL.Lm.mdReg |=
  2823. (ST25R3916_REG_MODE_targ_targ | ST25R3916_REG_MODE_om3 | ST25R3916_REG_MODE_om2 |
  2824. ST25R3916_REG_MODE_nfc_ar_off);
  2825. /* In CE NFC-F any data without error will be passed to FIFO, to support CUP */
  2826. gRFAL.Lm.mdIrqs |=
  2827. (ST25R3916_IRQ_MASK_WU_F | ST25R3916_IRQ_MASK_RXE_PTA | ST25R3916_IRQ_MASK_RXE);
  2828. }
  2829. /*******************************************************************************/
  2830. if((lmMask & RFAL_LM_MASK_ACTIVE_P2P) != 0U) {
  2831. /* Enable Reception of P2P frames */
  2832. autoResp &= ~(ST25R3916_REG_PASSIVE_TARGET_d_ac_ap2p);
  2833. /* Set Target mode, Bit Rate detection and Automatic Response RF Collision Avoidance */
  2834. gRFAL.Lm.mdReg |=
  2835. (ST25R3916_REG_MODE_targ_targ | ST25R3916_REG_MODE_om3 | ST25R3916_REG_MODE_om2 |
  2836. ST25R3916_REG_MODE_om0 | ST25R3916_REG_MODE_nfc_ar_auto_rx);
  2837. /* n * TRFW timing shall vary Activity 2.1 3.4.1.1 */
  2838. st25r3916ChangeRegisterBits(
  2839. ST25R3916_REG_AUX, ST25R3916_REG_AUX_nfc_n_mask, gRFAL.timings.nTRFW);
  2840. gRFAL.timings.nTRFW = rfalGennTRFW(gRFAL.timings.nTRFW);
  2841. gRFAL.Lm.mdIrqs |= (ST25R3916_IRQ_MASK_RXE);
  2842. }
  2843. /* Check if one of the modes were selected */
  2844. if((gRFAL.Lm.mdReg & ST25R3916_REG_MODE_targ) == ST25R3916_REG_MODE_targ_targ) {
  2845. gRFAL.state = RFAL_STATE_LM;
  2846. gRFAL.Lm.mdMask = lmMask;
  2847. gRFAL.Lm.rxBuf = rxBuf;
  2848. gRFAL.Lm.rxBufLen = rxBufLen;
  2849. gRFAL.Lm.rxLen = rxLen;
  2850. *gRFAL.Lm.rxLen = 0;
  2851. gRFAL.Lm.dataFlag = false;
  2852. gRFAL.Lm.iniFlag = true;
  2853. /* Apply the Automatic Responses configuration */
  2854. st25r3916ChangeRegisterBits(
  2855. ST25R3916_REG_PASSIVE_TARGET,
  2856. (ST25R3916_REG_PASSIVE_TARGET_d_106_ac_a | ST25R3916_REG_PASSIVE_TARGET_rfu |
  2857. ST25R3916_REG_PASSIVE_TARGET_d_212_424_1r | ST25R3916_REG_PASSIVE_TARGET_d_ac_ap2p),
  2858. autoResp);
  2859. /* Disable GPT trigger source */
  2860. st25r3916ChangeRegisterBits(
  2861. ST25R3916_REG_TIMER_EMV_CONTROL,
  2862. ST25R3916_REG_TIMER_EMV_CONTROL_gptc_mask,
  2863. ST25R3916_REG_TIMER_EMV_CONTROL_gptc_no_trigger);
  2864. /* On Bit Rate Detection Mode ST25R391x will filter incoming frames during MRT time starting on External Field On event, use 512/fc steps */
  2865. st25r3916SetRegisterBits(
  2866. ST25R3916_REG_TIMER_EMV_CONTROL, ST25R3916_REG_TIMER_EMV_CONTROL_mrt_step_512);
  2867. st25r3916WriteRegister(
  2868. ST25R3916_REG_MASK_RX_TIMER, (uint8_t)rfalConv1fcTo512fc(RFAL_LM_GT));
  2869. /* Restore default settings on NFCIP1 mode, Receiving parity + CRC bits and manual Tx Parity*/
  2870. st25r3916ClrRegisterBits(
  2871. ST25R3916_REG_ISO14443A_NFC,
  2872. (ST25R3916_REG_ISO14443A_NFC_no_tx_par | ST25R3916_REG_ISO14443A_NFC_no_rx_par |
  2873. ST25R3916_REG_ISO14443A_NFC_nfc_f0));
  2874. /* External Field Detector enabled as Automatics on rfalInitialize() */
  2875. /* Set Analog configurations for generic Listen mode */
  2876. /* Not on SetState(POWER OFF) as otherwise would be applied on every Field Event */
  2877. rfalSetAnalogConfig((RFAL_ANALOG_CONFIG_TECH_CHIP | RFAL_ANALOG_CONFIG_CHIP_LISTEN_ON));
  2878. /* Initialize as POWER_OFF and set proper mode in RF Chip */
  2879. rfalListenSetState(RFAL_LM_STATE_POWER_OFF);
  2880. } else {
  2881. return ERR_REQUEST; /* Listen Start called but no mode was enabled */
  2882. }
  2883. return ERR_NONE;
  2884. }
  2885. /*******************************************************************************/
  2886. static ReturnCode rfalRunListenModeWorker(void) {
  2887. volatile uint32_t irqs;
  2888. uint8_t tmp;
  2889. if(gRFAL.state != RFAL_STATE_LM) {
  2890. return ERR_WRONG_STATE;
  2891. }
  2892. switch(gRFAL.Lm.state) {
  2893. /*******************************************************************************/
  2894. case RFAL_LM_STATE_POWER_OFF:
  2895. irqs = st25r3916GetInterrupt((ST25R3916_IRQ_MASK_EON));
  2896. if(irqs == ST25R3916_IRQ_MASK_NONE) {
  2897. break; /* No interrupt to process */
  2898. }
  2899. if((irqs & ST25R3916_IRQ_MASK_EON) != 0U) {
  2900. rfalListenSetState(RFAL_LM_STATE_IDLE);
  2901. } else {
  2902. break;
  2903. }
  2904. /* fall through */
  2905. /*******************************************************************************/
  2906. case RFAL_LM_STATE_IDLE: /* PRQA S 2003 # MISRA 16.3 - Intentional fall through */
  2907. irqs = st25r3916GetInterrupt(
  2908. (ST25R3916_IRQ_MASK_NFCT | ST25R3916_IRQ_MASK_WU_F | ST25R3916_IRQ_MASK_RXE |
  2909. ST25R3916_IRQ_MASK_EOF | ST25R3916_IRQ_MASK_RXE_PTA));
  2910. if(irqs == ST25R3916_IRQ_MASK_NONE) {
  2911. break; /* No interrupt to process */
  2912. }
  2913. if((irqs & ST25R3916_IRQ_MASK_NFCT) != 0U) {
  2914. /* Retrieve detected bitrate */
  2915. uint8_t newBr;
  2916. st25r3916ReadRegister(ST25R3916_REG_NFCIP1_BIT_RATE, &newBr);
  2917. newBr >>= ST25R3916_REG_NFCIP1_BIT_RATE_nfc_rate_shift;
  2918. if(newBr > ST25R3916_REG_BIT_RATE_rxrate_424) {
  2919. newBr = ST25R3916_REG_BIT_RATE_rxrate_424;
  2920. }
  2921. gRFAL.Lm.brDetected =
  2922. (rfalBitRate)(newBr); /* PRQA S 4342 # MISRA 10.5 - Guaranteed that no invalid enum values may be created. See also equalityGuard_RFAL_BR_106 ff.*/
  2923. }
  2924. if(((irqs & ST25R3916_IRQ_MASK_WU_F) != 0U) && (gRFAL.Lm.brDetected != RFAL_BR_KEEP)) {
  2925. rfalListenSetState(RFAL_LM_STATE_READY_F);
  2926. } else if(((irqs & ST25R3916_IRQ_MASK_RXE) != 0U) && (gRFAL.Lm.brDetected != RFAL_BR_KEEP)) {
  2927. irqs = st25r3916GetInterrupt(
  2928. (ST25R3916_IRQ_MASK_WU_F | ST25R3916_IRQ_MASK_RXE | ST25R3916_IRQ_MASK_EOF |
  2929. ST25R3916_IRQ_MASK_CRC | ST25R3916_IRQ_MASK_PAR | ST25R3916_IRQ_MASK_ERR2 |
  2930. ST25R3916_IRQ_MASK_ERR1));
  2931. if(((irqs & ST25R3916_IRQ_MASK_CRC) != 0U) ||
  2932. ((irqs & ST25R3916_IRQ_MASK_PAR) != 0U) ||
  2933. ((irqs & ST25R3916_IRQ_MASK_ERR1) != 0U)) {
  2934. st25r3916ExecuteCommand(ST25R3916_CMD_CLEAR_FIFO);
  2935. st25r3916ExecuteCommand(ST25R3916_CMD_UNMASK_RECEIVE_DATA);
  2936. st25r3916TxOff();
  2937. break; /* A bad reception occurred, remain in same state */
  2938. }
  2939. /* Retrieve received data */
  2940. *gRFAL.Lm.rxLen = st25r3916GetNumFIFOBytes();
  2941. st25r3916ReadFifo(
  2942. gRFAL.Lm.rxBuf, MIN(*gRFAL.Lm.rxLen, rfalConvBitsToBytes(gRFAL.Lm.rxBufLen)));
  2943. /*******************************************************************************/
  2944. /* REMARK: Silicon workaround ST25R3916 Errata #TBD */
  2945. /* In bitrate detection mode CRC is now checked for NFC-A frames */
  2946. if((*gRFAL.Lm.rxLen > RFAL_CRC_LEN) && (gRFAL.Lm.brDetected == RFAL_BR_106)) {
  2947. if(rfalCrcCalculateCcitt(
  2948. RFAL_ISO14443A_CRC_INTVAL, gRFAL.Lm.rxBuf, *gRFAL.Lm.rxLen) != 0U) {
  2949. st25r3916ExecuteCommand(ST25R3916_CMD_CLEAR_FIFO);
  2950. st25r3916ExecuteCommand(ST25R3916_CMD_UNMASK_RECEIVE_DATA);
  2951. st25r3916TxOff();
  2952. break; /* A bad reception occurred, remain in same state */
  2953. }
  2954. }
  2955. /*******************************************************************************/
  2956. /* Check if the data we got has at least the CRC and remove it, otherwise leave at 0 */
  2957. *gRFAL.Lm.rxLen -= ((*gRFAL.Lm.rxLen > RFAL_CRC_LEN) ? RFAL_CRC_LEN : *gRFAL.Lm.rxLen);
  2958. *gRFAL.Lm.rxLen = (uint16_t)rfalConvBytesToBits(*gRFAL.Lm.rxLen);
  2959. gRFAL.Lm.dataFlag = true;
  2960. /*Check if Observation Mode was enabled and disable it on ST25R391x */
  2961. rfalCheckDisableObsMode();
  2962. } else if(
  2963. ((irqs & ST25R3916_IRQ_MASK_RXE_PTA) != 0U) && (gRFAL.Lm.brDetected != RFAL_BR_KEEP)) {
  2964. if(((gRFAL.Lm.mdMask & RFAL_LM_MASK_NFCA) != 0U) &&
  2965. (gRFAL.Lm.brDetected == RFAL_BR_106)) {
  2966. st25r3916ReadRegister(ST25R3916_REG_PASSIVE_TARGET_STATUS, &tmp);
  2967. if(tmp > ST25R3916_REG_PASSIVE_TARGET_STATUS_pta_st_idle) {
  2968. rfalListenSetState(RFAL_LM_STATE_READY_A);
  2969. }
  2970. }
  2971. } else if(((irqs & ST25R3916_IRQ_MASK_EOF) != 0U) && (!gRFAL.Lm.dataFlag)) {
  2972. rfalListenSetState(RFAL_LM_STATE_POWER_OFF);
  2973. } else {
  2974. /* MISRA 15.7 - Empty else */
  2975. }
  2976. break;
  2977. /*******************************************************************************/
  2978. case RFAL_LM_STATE_READY_F:
  2979. irqs = st25r3916GetInterrupt(
  2980. (ST25R3916_IRQ_MASK_WU_F | ST25R3916_IRQ_MASK_RXE | ST25R3916_IRQ_MASK_EOF));
  2981. if(irqs == ST25R3916_IRQ_MASK_NONE) {
  2982. break; /* No interrupt to process */
  2983. }
  2984. if((irqs & ST25R3916_IRQ_MASK_WU_F) != 0U) {
  2985. break;
  2986. } else if((irqs & ST25R3916_IRQ_MASK_RXE) != 0U) {
  2987. /* Retrieve the error flags/irqs */
  2988. irqs |= st25r3916GetInterrupt(
  2989. (ST25R3916_IRQ_MASK_CRC | ST25R3916_IRQ_MASK_ERR2 | ST25R3916_IRQ_MASK_ERR1));
  2990. if(((irqs & ST25R3916_IRQ_MASK_CRC) != 0U) ||
  2991. ((irqs & ST25R3916_IRQ_MASK_ERR1) != 0U)) {
  2992. st25r3916ExecuteCommand(ST25R3916_CMD_CLEAR_FIFO);
  2993. st25r3916ExecuteCommand(ST25R3916_CMD_UNMASK_RECEIVE_DATA);
  2994. break; /* A bad reception occurred, remain in same state */
  2995. }
  2996. /* Retrieve received data */
  2997. *gRFAL.Lm.rxLen = st25r3916GetNumFIFOBytes();
  2998. st25r3916ReadFifo(
  2999. gRFAL.Lm.rxBuf, MIN(*gRFAL.Lm.rxLen, rfalConvBitsToBytes(gRFAL.Lm.rxBufLen)));
  3000. /* Check if the data we got has at least the CRC and remove it, otherwise leave at 0 */
  3001. *gRFAL.Lm.rxLen -= ((*gRFAL.Lm.rxLen > RFAL_CRC_LEN) ? RFAL_CRC_LEN : *gRFAL.Lm.rxLen);
  3002. *gRFAL.Lm.rxLen = (uint16_t)rfalConvBytesToBits(*gRFAL.Lm.rxLen);
  3003. gRFAL.Lm.dataFlag = true;
  3004. } else if((irqs & ST25R3916_IRQ_MASK_EOF) != 0U) {
  3005. rfalListenSetState(RFAL_LM_STATE_POWER_OFF);
  3006. } else {
  3007. /* MISRA 15.7 - Empty else */
  3008. }
  3009. break;
  3010. /*******************************************************************************/
  3011. case RFAL_LM_STATE_READY_A:
  3012. irqs = st25r3916GetInterrupt((ST25R3916_IRQ_MASK_EOF | ST25R3916_IRQ_MASK_WU_A));
  3013. if(irqs == ST25R3916_IRQ_MASK_NONE) {
  3014. break; /* No interrupt to process */
  3015. }
  3016. if((irqs & ST25R3916_IRQ_MASK_WU_A) != 0U) {
  3017. rfalListenSetState(RFAL_LM_STATE_ACTIVE_A);
  3018. } else if((irqs & ST25R3916_IRQ_MASK_EOF) != 0U) {
  3019. rfalListenSetState(RFAL_LM_STATE_POWER_OFF);
  3020. } else {
  3021. /* MISRA 15.7 - Empty else */
  3022. }
  3023. break;
  3024. /*******************************************************************************/
  3025. case RFAL_LM_STATE_ACTIVE_A:
  3026. case RFAL_LM_STATE_ACTIVE_Ax:
  3027. irqs = st25r3916GetInterrupt((ST25R3916_IRQ_MASK_RXE | ST25R3916_IRQ_MASK_EOF));
  3028. if(irqs == ST25R3916_IRQ_MASK_NONE) {
  3029. break; /* No interrupt to process */
  3030. }
  3031. if((irqs & ST25R3916_IRQ_MASK_RXE) != 0U) {
  3032. /* Retrieve the error flags/irqs */
  3033. irqs |= st25r3916GetInterrupt(
  3034. (ST25R3916_IRQ_MASK_PAR | ST25R3916_IRQ_MASK_CRC | ST25R3916_IRQ_MASK_ERR2 |
  3035. ST25R3916_IRQ_MASK_ERR1));
  3036. *gRFAL.Lm.rxLen = st25r3916GetNumFIFOBytes();
  3037. if(((irqs & ST25R3916_IRQ_MASK_CRC) != 0U) ||
  3038. ((irqs & ST25R3916_IRQ_MASK_ERR1) != 0U) ||
  3039. ((irqs & ST25R3916_IRQ_MASK_PAR) != 0U) || (*gRFAL.Lm.rxLen <= RFAL_CRC_LEN)) {
  3040. /* Clear rx context and FIFO */
  3041. *gRFAL.Lm.rxLen = 0;
  3042. st25r3916ExecuteCommand(ST25R3916_CMD_CLEAR_FIFO);
  3043. st25r3916ExecuteCommand(ST25R3916_CMD_UNMASK_RECEIVE_DATA);
  3044. /* Check if we should go to IDLE or Sleep */
  3045. if(gRFAL.Lm.state == RFAL_LM_STATE_ACTIVE_Ax) {
  3046. rfalListenSleepStart(
  3047. RFAL_LM_STATE_SLEEP_A, gRFAL.Lm.rxBuf, gRFAL.Lm.rxBufLen, gRFAL.Lm.rxLen);
  3048. } else {
  3049. rfalListenSetState(RFAL_LM_STATE_IDLE);
  3050. }
  3051. st25r3916DisableInterrupts(ST25R3916_IRQ_MASK_RXE);
  3052. break;
  3053. }
  3054. /* Remove CRC from length */
  3055. *gRFAL.Lm.rxLen -= RFAL_CRC_LEN;
  3056. /* Retrieve received data */
  3057. st25r3916ReadFifo(
  3058. gRFAL.Lm.rxBuf, MIN(*gRFAL.Lm.rxLen, rfalConvBitsToBytes(gRFAL.Lm.rxBufLen)));
  3059. *gRFAL.Lm.rxLen = (uint16_t)rfalConvBytesToBits(*gRFAL.Lm.rxLen);
  3060. gRFAL.Lm.dataFlag = true;
  3061. } else if((irqs & ST25R3916_IRQ_MASK_EOF) != 0U) {
  3062. rfalListenSetState(RFAL_LM_STATE_POWER_OFF);
  3063. } else {
  3064. /* MISRA 15.7 - Empty else */
  3065. }
  3066. break;
  3067. /*******************************************************************************/
  3068. case RFAL_LM_STATE_SLEEP_A:
  3069. case RFAL_LM_STATE_SLEEP_B:
  3070. case RFAL_LM_STATE_SLEEP_AF:
  3071. irqs = st25r3916GetInterrupt(
  3072. (ST25R3916_IRQ_MASK_NFCT | ST25R3916_IRQ_MASK_WU_F | ST25R3916_IRQ_MASK_RXE |
  3073. ST25R3916_IRQ_MASK_EOF | ST25R3916_IRQ_MASK_RXE_PTA));
  3074. if(irqs == ST25R3916_IRQ_MASK_NONE) {
  3075. break; /* No interrupt to process */
  3076. }
  3077. if((irqs & ST25R3916_IRQ_MASK_NFCT) != 0U) {
  3078. uint8_t newBr;
  3079. /* Retrieve detected bitrate */
  3080. st25r3916ReadRegister(ST25R3916_REG_NFCIP1_BIT_RATE, &newBr);
  3081. newBr >>= ST25R3916_REG_NFCIP1_BIT_RATE_nfc_rate_shift;
  3082. if(newBr > ST25R3916_REG_BIT_RATE_rxrate_424) {
  3083. newBr = ST25R3916_REG_BIT_RATE_rxrate_424;
  3084. }
  3085. gRFAL.Lm.brDetected =
  3086. (rfalBitRate)(newBr); /* PRQA S 4342 # MISRA 10.5 - Guaranteed that no invalid enum values may be created. See also equalityGuard_RFAL_BR_106 ff.*/
  3087. }
  3088. if(((irqs & ST25R3916_IRQ_MASK_WU_F) != 0U) && (gRFAL.Lm.brDetected != RFAL_BR_KEEP)) {
  3089. rfalListenSetState(RFAL_LM_STATE_READY_F);
  3090. } else if(((irqs & ST25R3916_IRQ_MASK_RXE) != 0U) && (gRFAL.Lm.brDetected != RFAL_BR_KEEP)) {
  3091. /* Clear rx context and FIFO */
  3092. *gRFAL.Lm.rxLen = 0;
  3093. st25r3916ExecuteCommand(ST25R3916_CMD_CLEAR_FIFO);
  3094. st25r3916ExecuteCommand(ST25R3916_CMD_UNMASK_RECEIVE_DATA);
  3095. /* REMARK: In order to support CUP or proprietary frames, handling could be added here */
  3096. } else if(
  3097. ((irqs & ST25R3916_IRQ_MASK_RXE_PTA) != 0U) && (gRFAL.Lm.brDetected != RFAL_BR_KEEP)) {
  3098. if(((gRFAL.Lm.mdMask & RFAL_LM_MASK_NFCA) != 0U) &&
  3099. (gRFAL.Lm.brDetected == RFAL_BR_106)) {
  3100. st25r3916ReadRegister(ST25R3916_REG_PASSIVE_TARGET_STATUS, &tmp);
  3101. if(tmp > ST25R3916_REG_PASSIVE_TARGET_STATUS_pta_st_halt) {
  3102. rfalListenSetState(RFAL_LM_STATE_READY_Ax);
  3103. }
  3104. }
  3105. } else if((irqs & ST25R3916_IRQ_MASK_EOF) != 0U) {
  3106. rfalListenSetState(RFAL_LM_STATE_POWER_OFF);
  3107. } else {
  3108. /* MISRA 15.7 - Empty else */
  3109. }
  3110. break;
  3111. /*******************************************************************************/
  3112. case RFAL_LM_STATE_READY_Ax:
  3113. irqs = st25r3916GetInterrupt((ST25R3916_IRQ_MASK_EOF | ST25R3916_IRQ_MASK_WU_A_X));
  3114. if(irqs == ST25R3916_IRQ_MASK_NONE) {
  3115. break; /* No interrupt to process */
  3116. }
  3117. if((irqs & ST25R3916_IRQ_MASK_WU_A_X) != 0U) {
  3118. rfalListenSetState(RFAL_LM_STATE_ACTIVE_Ax);
  3119. } else if((irqs & ST25R3916_IRQ_MASK_EOF) != 0U) {
  3120. rfalListenSetState(RFAL_LM_STATE_POWER_OFF);
  3121. } else {
  3122. /* MISRA 15.7 - Empty else */
  3123. }
  3124. break;
  3125. /*******************************************************************************/
  3126. case RFAL_LM_STATE_CARDEMU_4A:
  3127. case RFAL_LM_STATE_CARDEMU_4B:
  3128. case RFAL_LM_STATE_CARDEMU_3:
  3129. case RFAL_LM_STATE_TARGET_F:
  3130. case RFAL_LM_STATE_TARGET_A:
  3131. break;
  3132. /*******************************************************************************/
  3133. default:
  3134. return ERR_WRONG_STATE;
  3135. }
  3136. return ERR_NONE;
  3137. }
  3138. /*******************************************************************************/
  3139. ReturnCode rfalListenStop(void) {
  3140. /* Check if RFAL is initialized */
  3141. if(gRFAL.state < RFAL_STATE_INIT) {
  3142. return ERR_WRONG_STATE;
  3143. }
  3144. gRFAL.Lm.state = RFAL_LM_STATE_NOT_INIT;
  3145. /*Check if Observation Mode was enabled and disable it on ST25R391x */
  3146. rfalCheckDisableObsMode();
  3147. /* Re-Enable the Oscillator if not running */
  3148. st25r3916OscOn();
  3149. /* Disable Receiver and Transmitter */
  3150. rfalFieldOff();
  3151. /* Disable all automatic responses */
  3152. st25r3916SetRegisterBits(
  3153. ST25R3916_REG_PASSIVE_TARGET,
  3154. (ST25R3916_REG_PASSIVE_TARGET_d_212_424_1r | ST25R3916_REG_PASSIVE_TARGET_rfu |
  3155. ST25R3916_REG_PASSIVE_TARGET_d_106_ac_a | ST25R3916_REG_PASSIVE_TARGET_d_ac_ap2p));
  3156. /* As there's no Off mode, set default value: ISO14443A with automatic RF Collision Avoidance Off */
  3157. st25r3916WriteRegister(
  3158. ST25R3916_REG_MODE,
  3159. (ST25R3916_REG_MODE_om_iso14443a | ST25R3916_REG_MODE_tr_am_ook |
  3160. ST25R3916_REG_MODE_nfc_ar_off));
  3161. st25r3916DisableInterrupts(
  3162. (ST25R3916_IRQ_MASK_RXE_PTA | ST25R3916_IRQ_MASK_WU_F | ST25R3916_IRQ_MASK_WU_A |
  3163. ST25R3916_IRQ_MASK_WU_A_X | ST25R3916_IRQ_MASK_RFU2 | ST25R3916_IRQ_MASK_OSC));
  3164. st25r3916GetInterrupt(
  3165. (ST25R3916_IRQ_MASK_RXE_PTA | ST25R3916_IRQ_MASK_WU_F | ST25R3916_IRQ_MASK_WU_A |
  3166. ST25R3916_IRQ_MASK_WU_A_X | ST25R3916_IRQ_MASK_RFU2));
  3167. /* Set Analog configurations for Listen Off event */
  3168. rfalSetAnalogConfig((RFAL_ANALOG_CONFIG_TECH_CHIP | RFAL_ANALOG_CONFIG_CHIP_LISTEN_OFF));
  3169. return ERR_NONE;
  3170. }
  3171. /*******************************************************************************/
  3172. ReturnCode
  3173. rfalListenSleepStart(rfalLmState sleepSt, uint8_t* rxBuf, uint16_t rxBufLen, uint16_t* rxLen) {
  3174. /* Check if RFAL is not initialized */
  3175. if(gRFAL.state < RFAL_STATE_INIT) {
  3176. return ERR_WRONG_STATE;
  3177. }
  3178. switch(sleepSt) {
  3179. /*******************************************************************************/
  3180. case RFAL_LM_STATE_SLEEP_A:
  3181. /* Enable automatic responses for A */
  3182. st25r3916ClrRegisterBits(
  3183. ST25R3916_REG_PASSIVE_TARGET, (ST25R3916_REG_PASSIVE_TARGET_d_106_ac_a));
  3184. /* Reset NFCA target */
  3185. st25r3916ExecuteCommand(ST25R3916_CMD_GOTO_SLEEP);
  3186. /* Set Target mode, Bit Rate detection and Listen Mode for NFC-A */
  3187. st25r3916ChangeRegisterBits(
  3188. ST25R3916_REG_MODE,
  3189. (ST25R3916_REG_MODE_targ | ST25R3916_REG_MODE_om_mask |
  3190. ST25R3916_REG_MODE_nfc_ar_mask),
  3191. (ST25R3916_REG_MODE_targ_targ | ST25R3916_REG_MODE_om3 | ST25R3916_REG_MODE_om0 |
  3192. ST25R3916_REG_MODE_nfc_ar_off));
  3193. break;
  3194. /*******************************************************************************/
  3195. case RFAL_LM_STATE_SLEEP_AF:
  3196. /* Enable automatic responses for A + F */
  3197. st25r3916ClrRegisterBits(
  3198. ST25R3916_REG_PASSIVE_TARGET,
  3199. (ST25R3916_REG_PASSIVE_TARGET_d_212_424_1r | ST25R3916_REG_PASSIVE_TARGET_d_106_ac_a));
  3200. /* Reset NFCA target state */
  3201. st25r3916ExecuteCommand(ST25R3916_CMD_GOTO_SLEEP);
  3202. /* Set Target mode, Bit Rate detection, Listen Mode for NFC-A and NFC-F */
  3203. st25r3916ChangeRegisterBits(
  3204. ST25R3916_REG_MODE,
  3205. (ST25R3916_REG_MODE_targ | ST25R3916_REG_MODE_om_mask |
  3206. ST25R3916_REG_MODE_nfc_ar_mask),
  3207. (ST25R3916_REG_MODE_targ_targ | ST25R3916_REG_MODE_om3 | ST25R3916_REG_MODE_om2 |
  3208. ST25R3916_REG_MODE_om0 | ST25R3916_REG_MODE_nfc_ar_off));
  3209. break;
  3210. /*******************************************************************************/
  3211. case RFAL_LM_STATE_SLEEP_B:
  3212. /* REMARK: Support for CE-B would be added here */
  3213. return ERR_NOT_IMPLEMENTED;
  3214. /*******************************************************************************/
  3215. default:
  3216. return ERR_PARAM;
  3217. }
  3218. /* Ensure that the NFCIP1 mode is disabled */
  3219. st25r3916ClrRegisterBits(ST25R3916_REG_ISO14443A_NFC, ST25R3916_REG_ISO14443A_NFC_nfc_f0);
  3220. st25r3916ExecuteCommand(ST25R3916_CMD_UNMASK_RECEIVE_DATA);
  3221. /* Clear and enable required IRQs */
  3222. st25r3916ClearAndEnableInterrupts(
  3223. (ST25R3916_IRQ_MASK_NFCT | ST25R3916_IRQ_MASK_RXS | ST25R3916_IRQ_MASK_CRC |
  3224. ST25R3916_IRQ_MASK_ERR1 | ST25R3916_IRQ_MASK_ERR2 | ST25R3916_IRQ_MASK_PAR |
  3225. ST25R3916_IRQ_MASK_EON | ST25R3916_IRQ_MASK_EOF | gRFAL.Lm.mdIrqs));
  3226. /* Check whether the field was turn off right after the Sleep request */
  3227. if(!rfalIsExtFieldOn()) {
  3228. /*rfalLogD( "RFAL: curState: %02X newState: %02X \r\n", gRFAL.Lm.state, RFAL_LM_STATE_NOT_INIT );*/
  3229. rfalListenStop();
  3230. return ERR_LINK_LOSS;
  3231. }
  3232. /*rfalLogD( "RFAL: curState: %02X newState: %02X \r\n", gRFAL.Lm.state, sleepSt );*/
  3233. /* Set the new Sleep State*/
  3234. gRFAL.Lm.state = sleepSt;
  3235. gRFAL.state = RFAL_STATE_LM;
  3236. gRFAL.Lm.rxBuf = rxBuf;
  3237. gRFAL.Lm.rxBufLen = rxBufLen;
  3238. gRFAL.Lm.rxLen = rxLen;
  3239. *gRFAL.Lm.rxLen = 0;
  3240. gRFAL.Lm.dataFlag = false;
  3241. return ERR_NONE;
  3242. }
  3243. /*******************************************************************************/
  3244. rfalLmState rfalListenGetState(bool* dataFlag, rfalBitRate* lastBR) {
  3245. /* Allow state retrieval even if gRFAL.state != RFAL_STATE_LM so *
  3246. * that this Lm state can be used by caller after activation */
  3247. if(lastBR != NULL) {
  3248. *lastBR = gRFAL.Lm.brDetected;
  3249. }
  3250. if(dataFlag != NULL) {
  3251. *dataFlag = gRFAL.Lm.dataFlag;
  3252. }
  3253. return gRFAL.Lm.state;
  3254. }
  3255. /*******************************************************************************/
  3256. ReturnCode rfalListenSetState(rfalLmState newSt) {
  3257. ReturnCode ret;
  3258. rfalLmState newState;
  3259. bool reSetState;
  3260. /* Check if RFAL is initialized */
  3261. if(gRFAL.state < RFAL_STATE_INIT) {
  3262. return ERR_WRONG_STATE;
  3263. }
  3264. /* SetState clears the Data flag */
  3265. gRFAL.Lm.dataFlag = false;
  3266. newState = newSt;
  3267. ret = ERR_NONE;
  3268. do {
  3269. reSetState = false;
  3270. /*******************************************************************************/
  3271. switch(newState) {
  3272. /*******************************************************************************/
  3273. case RFAL_LM_STATE_POWER_OFF:
  3274. /* Enable the receiver and reset logic */
  3275. st25r3916SetRegisterBits(ST25R3916_REG_OP_CONTROL, ST25R3916_REG_OP_CONTROL_rx_en);
  3276. st25r3916ExecuteCommand(ST25R3916_CMD_STOP);
  3277. if((gRFAL.Lm.mdMask & RFAL_LM_MASK_NFCA) != 0U) {
  3278. /* Enable automatic responses for A */
  3279. st25r3916ClrRegisterBits(
  3280. ST25R3916_REG_PASSIVE_TARGET, ST25R3916_REG_PASSIVE_TARGET_d_106_ac_a);
  3281. /* Prepares the NFCIP-1 Passive target logic to wait in the Sense/Idle state */
  3282. st25r3916ExecuteCommand(ST25R3916_CMD_GOTO_SENSE);
  3283. }
  3284. if((gRFAL.Lm.mdMask & RFAL_LM_MASK_NFCF) != 0U) {
  3285. /* Enable automatic responses for F */
  3286. st25r3916ClrRegisterBits(
  3287. ST25R3916_REG_PASSIVE_TARGET, (ST25R3916_REG_PASSIVE_TARGET_d_212_424_1r));
  3288. }
  3289. if((gRFAL.Lm.mdMask & RFAL_LM_MASK_ACTIVE_P2P) != 0U) {
  3290. /* Ensure automatic response RF Collision Avoidance is back to only after Rx */
  3291. st25r3916ChangeRegisterBits(
  3292. ST25R3916_REG_MODE,
  3293. ST25R3916_REG_MODE_nfc_ar_mask,
  3294. ST25R3916_REG_MODE_nfc_ar_auto_rx);
  3295. /* Ensure that our field is Off, as automatic response RF Collision Avoidance may have been triggered */
  3296. st25r3916TxOff();
  3297. }
  3298. /*******************************************************************************/
  3299. /* Ensure that the NFCIP1 mode is disabled */
  3300. st25r3916ClrRegisterBits(
  3301. ST25R3916_REG_ISO14443A_NFC, ST25R3916_REG_ISO14443A_NFC_nfc_f0);
  3302. /*******************************************************************************/
  3303. /* Clear and enable required IRQs */
  3304. st25r3916DisableInterrupts(ST25R3916_IRQ_MASK_ALL);
  3305. st25r3916ClearAndEnableInterrupts(
  3306. (ST25R3916_IRQ_MASK_NFCT | ST25R3916_IRQ_MASK_RXS | ST25R3916_IRQ_MASK_CRC |
  3307. ST25R3916_IRQ_MASK_ERR1 | ST25R3916_IRQ_MASK_OSC | ST25R3916_IRQ_MASK_ERR2 |
  3308. ST25R3916_IRQ_MASK_PAR | ST25R3916_IRQ_MASK_EON | ST25R3916_IRQ_MASK_EOF |
  3309. gRFAL.Lm.mdIrqs));
  3310. /*******************************************************************************/
  3311. /* Clear the bitRate previously detected */
  3312. gRFAL.Lm.brDetected = RFAL_BR_KEEP;
  3313. /*******************************************************************************/
  3314. /* Apply the initial mode */
  3315. st25r3916ChangeRegisterBits(
  3316. ST25R3916_REG_MODE,
  3317. (ST25R3916_REG_MODE_targ | ST25R3916_REG_MODE_om_mask |
  3318. ST25R3916_REG_MODE_nfc_ar_mask),
  3319. (uint8_t)gRFAL.Lm.mdReg);
  3320. /*******************************************************************************/
  3321. /* Check if external Field is already On */
  3322. if(rfalIsExtFieldOn()) {
  3323. reSetState = true;
  3324. newState = RFAL_LM_STATE_IDLE; /* Set IDLE state */
  3325. }
  3326. #if 1 /* Perform bit rate detection in Low power mode */
  3327. else {
  3328. st25r3916ClrRegisterBits(
  3329. ST25R3916_REG_OP_CONTROL,
  3330. (ST25R3916_REG_OP_CONTROL_tx_en | ST25R3916_REG_OP_CONTROL_rx_en |
  3331. ST25R3916_REG_OP_CONTROL_en));
  3332. }
  3333. #endif
  3334. break;
  3335. /*******************************************************************************/
  3336. case RFAL_LM_STATE_IDLE:
  3337. /*******************************************************************************/
  3338. /* Check if device is coming from Low Power bit rate detection */
  3339. if(!st25r3916CheckReg(
  3340. ST25R3916_REG_OP_CONTROL,
  3341. ST25R3916_REG_OP_CONTROL_en,
  3342. ST25R3916_REG_OP_CONTROL_en)) {
  3343. /* Exit Low Power mode and confirm the temporarily enable */
  3344. st25r3916SetRegisterBits(
  3345. ST25R3916_REG_OP_CONTROL,
  3346. (ST25R3916_REG_OP_CONTROL_en | ST25R3916_REG_OP_CONTROL_rx_en));
  3347. if(!st25r3916CheckReg(
  3348. ST25R3916_REG_AUX_DISPLAY,
  3349. ST25R3916_REG_AUX_DISPLAY_osc_ok,
  3350. ST25R3916_REG_AUX_DISPLAY_osc_ok)) {
  3351. /* Wait for Oscilator ready */
  3352. if(st25r3916WaitForInterruptsTimed(
  3353. ST25R3916_IRQ_MASK_OSC, ST25R3916_TOUT_OSC_STABLE) == 0U) {
  3354. ret = ERR_IO;
  3355. break;
  3356. }
  3357. }
  3358. } else {
  3359. st25r3916GetInterrupt(ST25R3916_IRQ_MASK_OSC);
  3360. }
  3361. /*******************************************************************************/
  3362. /* In Active P2P the Initiator may: Turn its field On; LM goes into IDLE state;
  3363. * Initiator sends an unexpected frame raising a Protocol error; Initiator
  3364. * turns its field Off and ST25R3916 performs the automatic RF Collision
  3365. * Avoidance keeping our field On; upon a Protocol error upper layer sets
  3366. * again the state to IDLE to clear dataFlag and wait for next data.
  3367. *
  3368. * Ensure that when upper layer calls SetState(IDLE), it restores initial
  3369. * configuration and that check whether an external Field is still present */
  3370. if((gRFAL.Lm.mdMask & RFAL_LM_MASK_ACTIVE_P2P) != 0U) {
  3371. /* Ensure nfc_ar is reseted and back to only after Rx */
  3372. st25r3916ExecuteCommand(ST25R3916_CMD_STOP);
  3373. st25r3916ChangeRegisterBits(
  3374. ST25R3916_REG_MODE,
  3375. ST25R3916_REG_MODE_nfc_ar_mask,
  3376. ST25R3916_REG_MODE_nfc_ar_auto_rx);
  3377. /* Ensure that our field is Off, as automatic response RF Collision Avoidance may have been triggered */
  3378. st25r3916TxOff();
  3379. /* If external Field is no longer detected go back to POWER_OFF */
  3380. if(!st25r3916IsExtFieldOn()) {
  3381. reSetState = true;
  3382. newState = RFAL_LM_STATE_POWER_OFF; /* Set POWER_OFF state */
  3383. }
  3384. }
  3385. /*******************************************************************************/
  3386. /* If we are in ACTIVE_A, reEnable Listen for A before going to IDLE, otherwise do nothing */
  3387. if(gRFAL.Lm.state == RFAL_LM_STATE_ACTIVE_A) {
  3388. /* Enable automatic responses for A and Reset NFCA target state */
  3389. st25r3916ClrRegisterBits(
  3390. ST25R3916_REG_PASSIVE_TARGET, (ST25R3916_REG_PASSIVE_TARGET_d_106_ac_a));
  3391. st25r3916ExecuteCommand(ST25R3916_CMD_GOTO_SENSE);
  3392. }
  3393. /* ReEnable the receiver */
  3394. st25r3916ExecuteCommand(ST25R3916_CMD_CLEAR_FIFO);
  3395. st25r3916ExecuteCommand(ST25R3916_CMD_UNMASK_RECEIVE_DATA);
  3396. /*******************************************************************************/
  3397. /*Check if Observation Mode is enabled and set it on ST25R391x */
  3398. rfalCheckEnableObsModeRx();
  3399. break;
  3400. /*******************************************************************************/
  3401. case RFAL_LM_STATE_READY_F:
  3402. /*******************************************************************************/
  3403. /* If we're coming from BitRate detection mode, the Bit Rate Definition reg
  3404. * still has the last bit rate used.
  3405. * If a frame is received between setting the mode to Listen NFCA and
  3406. * setting Bit Rate Definition reg, it will raise a framing error.
  3407. * Set the bitrate immediately, and then the normal SetMode procedure */
  3408. st25r3916SetBitrate((uint8_t)gRFAL.Lm.brDetected, (uint8_t)gRFAL.Lm.brDetected);
  3409. /*******************************************************************************/
  3410. /* Disable automatic responses for NFC-A */
  3411. st25r3916SetRegisterBits(
  3412. ST25R3916_REG_PASSIVE_TARGET, (ST25R3916_REG_PASSIVE_TARGET_d_106_ac_a));
  3413. /* Set Mode NFC-F only */
  3414. ret = rfalSetMode(RFAL_MODE_LISTEN_NFCF, gRFAL.Lm.brDetected, gRFAL.Lm.brDetected);
  3415. gRFAL.state = RFAL_STATE_LM; /* Keep in Listen Mode */
  3416. /* ReEnable the receiver */
  3417. st25r3916ExecuteCommand(ST25R3916_CMD_CLEAR_FIFO);
  3418. st25r3916ExecuteCommand(ST25R3916_CMD_UNMASK_RECEIVE_DATA);
  3419. /* Clear any previous transmission errors (if Reader polled for other/unsupported technologies) */
  3420. st25r3916GetInterrupt(
  3421. (ST25R3916_IRQ_MASK_PAR | ST25R3916_IRQ_MASK_CRC | ST25R3916_IRQ_MASK_ERR2 |
  3422. ST25R3916_IRQ_MASK_ERR1));
  3423. st25r3916EnableInterrupts(
  3424. ST25R3916_IRQ_MASK_RXE); /* Start looking for any incoming data */
  3425. break;
  3426. /*******************************************************************************/
  3427. case RFAL_LM_STATE_CARDEMU_3:
  3428. /* Set Listen NFCF mode */
  3429. ret = rfalSetMode(RFAL_MODE_LISTEN_NFCF, gRFAL.Lm.brDetected, gRFAL.Lm.brDetected);
  3430. break;
  3431. /*******************************************************************************/
  3432. case RFAL_LM_STATE_READY_Ax:
  3433. case RFAL_LM_STATE_READY_A:
  3434. /*******************************************************************************/
  3435. /* If we're coming from BitRate detection mode, the Bit Rate Definition reg
  3436. * still has the last bit rate used.
  3437. * If a frame is received between setting the mode to Listen NFCA and
  3438. * setting Bit Rate Definition reg, it will raise a framing error.
  3439. * Set the bitrate immediately, and then the normal SetMode procedure */
  3440. st25r3916SetBitrate((uint8_t)gRFAL.Lm.brDetected, (uint8_t)gRFAL.Lm.brDetected);
  3441. /*******************************************************************************/
  3442. /* Disable automatic responses for NFC-F */
  3443. st25r3916SetRegisterBits(
  3444. ST25R3916_REG_PASSIVE_TARGET, (ST25R3916_REG_PASSIVE_TARGET_d_212_424_1r));
  3445. /* Set Mode NFC-A only */
  3446. ret = rfalSetMode(RFAL_MODE_LISTEN_NFCA, gRFAL.Lm.brDetected, gRFAL.Lm.brDetected);
  3447. gRFAL.state = RFAL_STATE_LM; /* Keep in Listen Mode */
  3448. break;
  3449. /*******************************************************************************/
  3450. case RFAL_LM_STATE_ACTIVE_Ax:
  3451. case RFAL_LM_STATE_ACTIVE_A:
  3452. /* Disable automatic responses for A */
  3453. st25r3916SetRegisterBits(
  3454. ST25R3916_REG_PASSIVE_TARGET, (ST25R3916_REG_PASSIVE_TARGET_d_106_ac_a));
  3455. /* Clear any previous transmission errors (if Reader polled for other/unsupported technologies) */
  3456. st25r3916GetInterrupt(
  3457. (ST25R3916_IRQ_MASK_PAR | ST25R3916_IRQ_MASK_CRC | ST25R3916_IRQ_MASK_ERR2 |
  3458. ST25R3916_IRQ_MASK_ERR1));
  3459. st25r3916EnableInterrupts(
  3460. ST25R3916_IRQ_MASK_RXE); /* Start looking for any incoming data */
  3461. break;
  3462. case RFAL_LM_STATE_TARGET_F:
  3463. /* Disable Automatic response SENSF_REQ */
  3464. st25r3916SetRegisterBits(
  3465. ST25R3916_REG_PASSIVE_TARGET, (ST25R3916_REG_PASSIVE_TARGET_d_212_424_1r));
  3466. break;
  3467. /*******************************************************************************/
  3468. case RFAL_LM_STATE_SLEEP_A:
  3469. case RFAL_LM_STATE_SLEEP_B:
  3470. case RFAL_LM_STATE_SLEEP_AF:
  3471. /* These sleep states have to be set by the rfalListenSleepStart() method */
  3472. return ERR_REQUEST;
  3473. /*******************************************************************************/
  3474. case RFAL_LM_STATE_CARDEMU_4A:
  3475. case RFAL_LM_STATE_CARDEMU_4B:
  3476. case RFAL_LM_STATE_TARGET_A:
  3477. /* States not handled by the LM, just keep state context */
  3478. break;
  3479. /*******************************************************************************/
  3480. default:
  3481. return ERR_WRONG_STATE;
  3482. }
  3483. } while(reSetState);
  3484. gRFAL.Lm.state = newState;
  3485. // Call callback on state change
  3486. if(gRFAL.callbacks.state_changed_cb) {
  3487. gRFAL.callbacks.state_changed_cb(gRFAL.callbacks.ctx);
  3488. }
  3489. return ret;
  3490. }
  3491. #endif /* RFAL_FEATURE_LISTEN_MODE */
  3492. /*******************************************************************************
  3493. * Wake-Up Mode *
  3494. *******************************************************************************/
  3495. #if RFAL_FEATURE_WAKEUP_MODE
  3496. /*******************************************************************************/
  3497. ReturnCode rfalWakeUpModeStart(const rfalWakeUpConfig* config) {
  3498. uint8_t aux;
  3499. uint8_t reg;
  3500. uint32_t irqs;
  3501. /* Check if RFAL is not initialized */
  3502. if(gRFAL.state < RFAL_STATE_INIT) {
  3503. return ERR_WRONG_STATE;
  3504. }
  3505. /* The Wake-Up procedure is explained in detail in Application Note: AN4985 */
  3506. if(config == NULL) {
  3507. gRFAL.wum.cfg.period = RFAL_WUM_PERIOD_200MS;
  3508. gRFAL.wum.cfg.irqTout = false;
  3509. gRFAL.wum.cfg.indAmp.enabled = true;
  3510. gRFAL.wum.cfg.indPha.enabled = false;
  3511. gRFAL.wum.cfg.cap.enabled = false;
  3512. gRFAL.wum.cfg.indAmp.delta = 2U;
  3513. gRFAL.wum.cfg.indAmp.reference = RFAL_WUM_REFERENCE_AUTO;
  3514. gRFAL.wum.cfg.indAmp.autoAvg = false;
  3515. /*******************************************************************************/
  3516. /* Check if AAT is enabled and if so make use of the SW Tag Detection */
  3517. if(st25r3916CheckReg(
  3518. ST25R3916_REG_IO_CONF2,
  3519. ST25R3916_REG_IO_CONF2_aat_en,
  3520. ST25R3916_REG_IO_CONF2_aat_en)) {
  3521. gRFAL.wum.cfg.swTagDetect = true;
  3522. gRFAL.wum.cfg.indAmp.autoAvg = true;
  3523. gRFAL.wum.cfg.indAmp.aaWeight = RFAL_WUM_AA_WEIGHT_16;
  3524. }
  3525. } else {
  3526. gRFAL.wum.cfg = *config;
  3527. }
  3528. /* Check for valid configuration */
  3529. if((!gRFAL.wum.cfg.cap.enabled && !gRFAL.wum.cfg.indAmp.enabled &&
  3530. !gRFAL.wum.cfg.indPha.enabled) ||
  3531. (gRFAL.wum.cfg.cap.enabled &&
  3532. (gRFAL.wum.cfg.indAmp.enabled || gRFAL.wum.cfg.indPha.enabled)) ||
  3533. (gRFAL.wum.cfg.cap.enabled && gRFAL.wum.cfg.swTagDetect) ||
  3534. ((gRFAL.wum.cfg.indAmp.reference > RFAL_WUM_REFERENCE_AUTO) ||
  3535. (gRFAL.wum.cfg.indPha.reference > RFAL_WUM_REFERENCE_AUTO) ||
  3536. (gRFAL.wum.cfg.cap.reference > RFAL_WUM_REFERENCE_AUTO))) {
  3537. return ERR_PARAM;
  3538. }
  3539. irqs = ST25R3916_IRQ_MASK_NONE;
  3540. /* Disable Tx, Rx, External Field Detector and set default ISO14443A mode */
  3541. st25r3916TxRxOff();
  3542. st25r3916ClrRegisterBits(ST25R3916_REG_OP_CONTROL, ST25R3916_REG_OP_CONTROL_en_fd_mask);
  3543. st25r3916ChangeRegisterBits(
  3544. ST25R3916_REG_MODE,
  3545. (ST25R3916_REG_MODE_targ | ST25R3916_REG_MODE_om_mask),
  3546. (ST25R3916_REG_MODE_targ_init | ST25R3916_REG_MODE_om_iso14443a));
  3547. /* Set Analog configurations for Wake-up On event */
  3548. rfalSetAnalogConfig((RFAL_ANALOG_CONFIG_TECH_CHIP | RFAL_ANALOG_CONFIG_CHIP_WAKEUP_ON));
  3549. /*******************************************************************************/
  3550. /* Prepare Wake-Up Timer Control Register */
  3551. reg =
  3552. (uint8_t)(((uint8_t)gRFAL.wum.cfg.period & 0x0FU) << ST25R3916_REG_WUP_TIMER_CONTROL_wut_shift);
  3553. reg |=
  3554. (uint8_t)(((uint8_t)gRFAL.wum.cfg.period < (uint8_t)RFAL_WUM_PERIOD_100MS) ? ST25R3916_REG_WUP_TIMER_CONTROL_wur : 0x00U);
  3555. if(gRFAL.wum.cfg.irqTout || gRFAL.wum.cfg.swTagDetect) {
  3556. reg |= ST25R3916_REG_WUP_TIMER_CONTROL_wto;
  3557. irqs |= ST25R3916_IRQ_MASK_WT;
  3558. }
  3559. /* Check if HW Wake-up is to be used or SW Tag detection */
  3560. if(gRFAL.wum.cfg.swTagDetect) {
  3561. gRFAL.wum.cfg.indAmp.reference = 0U;
  3562. gRFAL.wum.cfg.indPha.reference = 0U;
  3563. gRFAL.wum.cfg.cap.reference = 0U;
  3564. } else {
  3565. /*******************************************************************************/
  3566. /* Check if Inductive Amplitude is to be performed */
  3567. if(gRFAL.wum.cfg.indAmp.enabled) {
  3568. aux =
  3569. (uint8_t)((gRFAL.wum.cfg.indAmp.delta) << ST25R3916_REG_AMPLITUDE_MEASURE_CONF_am_d_shift);
  3570. aux |=
  3571. (uint8_t)(gRFAL.wum.cfg.indAmp.aaInclMeas ? ST25R3916_REG_AMPLITUDE_MEASURE_CONF_am_aam : 0x00U);
  3572. aux |=
  3573. (uint8_t)(((uint8_t)gRFAL.wum.cfg.indAmp.aaWeight << ST25R3916_REG_AMPLITUDE_MEASURE_CONF_am_aew_shift) & ST25R3916_REG_AMPLITUDE_MEASURE_CONF_am_aew_mask);
  3574. aux |=
  3575. (uint8_t)(gRFAL.wum.cfg.indAmp.autoAvg ? ST25R3916_REG_AMPLITUDE_MEASURE_CONF_am_ae : 0x00U);
  3576. st25r3916WriteRegister(ST25R3916_REG_AMPLITUDE_MEASURE_CONF, aux);
  3577. /* Only need to set the reference if not using Auto Average */
  3578. if(!gRFAL.wum.cfg.indAmp.autoAvg) {
  3579. if(gRFAL.wum.cfg.indAmp.reference == RFAL_WUM_REFERENCE_AUTO) {
  3580. st25r3916MeasureAmplitude(&aux);
  3581. gRFAL.wum.cfg.indAmp.reference = aux;
  3582. }
  3583. st25r3916WriteRegister(
  3584. ST25R3916_REG_AMPLITUDE_MEASURE_REF, (uint8_t)gRFAL.wum.cfg.indAmp.reference);
  3585. }
  3586. reg |= ST25R3916_REG_WUP_TIMER_CONTROL_wam;
  3587. irqs |= ST25R3916_IRQ_MASK_WAM;
  3588. }
  3589. /*******************************************************************************/
  3590. /* Check if Inductive Phase is to be performed */
  3591. if(gRFAL.wum.cfg.indPha.enabled) {
  3592. aux =
  3593. (uint8_t)((gRFAL.wum.cfg.indPha.delta) << ST25R3916_REG_PHASE_MEASURE_CONF_pm_d_shift);
  3594. aux |=
  3595. (uint8_t)(gRFAL.wum.cfg.indPha.aaInclMeas ? ST25R3916_REG_PHASE_MEASURE_CONF_pm_aam : 0x00U);
  3596. aux |=
  3597. (uint8_t)(((uint8_t)gRFAL.wum.cfg.indPha.aaWeight << ST25R3916_REG_PHASE_MEASURE_CONF_pm_aew_shift) & ST25R3916_REG_PHASE_MEASURE_CONF_pm_aew_mask);
  3598. aux |=
  3599. (uint8_t)(gRFAL.wum.cfg.indPha.autoAvg ? ST25R3916_REG_PHASE_MEASURE_CONF_pm_ae : 0x00U);
  3600. st25r3916WriteRegister(ST25R3916_REG_PHASE_MEASURE_CONF, aux);
  3601. /* Only need to set the reference if not using Auto Average */
  3602. if(!gRFAL.wum.cfg.indPha.autoAvg) {
  3603. if(gRFAL.wum.cfg.indPha.reference == RFAL_WUM_REFERENCE_AUTO) {
  3604. st25r3916MeasurePhase(&aux);
  3605. gRFAL.wum.cfg.indPha.reference = aux;
  3606. }
  3607. st25r3916WriteRegister(
  3608. ST25R3916_REG_PHASE_MEASURE_REF, (uint8_t)gRFAL.wum.cfg.indPha.reference);
  3609. }
  3610. reg |= ST25R3916_REG_WUP_TIMER_CONTROL_wph;
  3611. irqs |= ST25R3916_IRQ_MASK_WPH;
  3612. }
  3613. /*******************************************************************************/
  3614. /* Check if Capacitive is to be performed */
  3615. if(gRFAL.wum.cfg.cap.enabled) {
  3616. /*******************************************************************************/
  3617. /* Perform Capacitive sensor calibration */
  3618. /* Disable Oscillator and Field */
  3619. st25r3916ClrRegisterBits(
  3620. ST25R3916_REG_OP_CONTROL,
  3621. (ST25R3916_REG_OP_CONTROL_en | ST25R3916_REG_OP_CONTROL_tx_en));
  3622. /* Sensor gain should be configured on Analog Config: RFAL_ANALOG_CONFIG_CHIP_WAKEUP_ON */
  3623. /* Perform calibration procedure */
  3624. st25r3916CalibrateCapacitiveSensor(NULL);
  3625. /*******************************************************************************/
  3626. aux =
  3627. (uint8_t)((gRFAL.wum.cfg.cap.delta) << ST25R3916_REG_CAPACITANCE_MEASURE_CONF_cm_d_shift);
  3628. aux |=
  3629. (uint8_t)(gRFAL.wum.cfg.cap.aaInclMeas ? ST25R3916_REG_CAPACITANCE_MEASURE_CONF_cm_aam : 0x00U);
  3630. aux |=
  3631. (uint8_t)(((uint8_t)gRFAL.wum.cfg.cap.aaWeight << ST25R3916_REG_CAPACITANCE_MEASURE_CONF_cm_aew_shift) & ST25R3916_REG_CAPACITANCE_MEASURE_CONF_cm_aew_mask);
  3632. aux |=
  3633. (uint8_t)(gRFAL.wum.cfg.cap.autoAvg ? ST25R3916_REG_CAPACITANCE_MEASURE_CONF_cm_ae : 0x00U);
  3634. st25r3916WriteRegister(ST25R3916_REG_CAPACITANCE_MEASURE_CONF, aux);
  3635. /* Only need to set the reference if not using Auto Average */
  3636. if(!gRFAL.wum.cfg.cap.autoAvg || gRFAL.wum.cfg.swTagDetect) {
  3637. if(gRFAL.wum.cfg.indPha.reference == RFAL_WUM_REFERENCE_AUTO) {
  3638. st25r3916MeasureCapacitance(&aux);
  3639. gRFAL.wum.cfg.cap.reference = aux;
  3640. }
  3641. st25r3916WriteRegister(
  3642. ST25R3916_REG_CAPACITANCE_MEASURE_REF, (uint8_t)gRFAL.wum.cfg.cap.reference);
  3643. }
  3644. reg |= ST25R3916_REG_WUP_TIMER_CONTROL_wcap;
  3645. irqs |= ST25R3916_IRQ_MASK_WCAP;
  3646. }
  3647. }
  3648. /* Disable and clear all interrupts except Wake-Up IRQs */
  3649. st25r3916DisableInterrupts(ST25R3916_IRQ_MASK_ALL);
  3650. st25r3916GetInterrupt(irqs);
  3651. st25r3916EnableInterrupts(irqs);
  3652. /* Enable Low Power Wake-Up Mode (Disable: Oscilattor, Tx, Rx and External Field Detector) */
  3653. st25r3916WriteRegister(ST25R3916_REG_WUP_TIMER_CONTROL, reg);
  3654. st25r3916ChangeRegisterBits(
  3655. ST25R3916_REG_OP_CONTROL,
  3656. (ST25R3916_REG_OP_CONTROL_en | ST25R3916_REG_OP_CONTROL_rx_en |
  3657. ST25R3916_REG_OP_CONTROL_tx_en | ST25R3916_REG_OP_CONTROL_en_fd_mask |
  3658. ST25R3916_REG_OP_CONTROL_wu),
  3659. ST25R3916_REG_OP_CONTROL_wu);
  3660. gRFAL.wum.state = RFAL_WUM_STATE_ENABLED;
  3661. gRFAL.state = RFAL_STATE_WUM;
  3662. return ERR_NONE;
  3663. }
  3664. /*******************************************************************************/
  3665. bool rfalWakeUpModeHasWoke(void) {
  3666. return (gRFAL.wum.state >= RFAL_WUM_STATE_ENABLED_WOKE);
  3667. }
  3668. /*******************************************************************************/
  3669. static uint16_t rfalWakeUpModeFilter(uint16_t curRef, uint16_t curVal, uint8_t weight) {
  3670. uint16_t newRef;
  3671. /* Perform the averaging|filter as describded in ST25R3916 DS */
  3672. /* Avoid signed arithmetics by spliting in two cases */
  3673. if(curVal > curRef) {
  3674. newRef = curRef + ((curVal - curRef) / weight);
  3675. /* In order for the reference to converge to final value *
  3676. * increment once the diff is smaller that the weight */
  3677. if((curVal != curRef) && (curRef == newRef)) {
  3678. newRef &= 0xFF00U;
  3679. newRef += 0x0100U;
  3680. }
  3681. } else {
  3682. newRef = curRef - ((curRef - curVal) / weight);
  3683. /* In order for the reference to converge to final value *
  3684. * decrement once the diff is smaller that the weight */
  3685. if((curVal != curRef) && (curRef == newRef)) {
  3686. newRef &= 0xFF00U;
  3687. }
  3688. }
  3689. return newRef;
  3690. }
  3691. /*******************************************************************************/
  3692. static void rfalRunWakeUpModeWorker(void) {
  3693. uint32_t irqs;
  3694. uint8_t reg;
  3695. uint16_t value;
  3696. uint16_t delta;
  3697. if(gRFAL.state != RFAL_STATE_WUM) {
  3698. return;
  3699. }
  3700. switch(gRFAL.wum.state) {
  3701. case RFAL_WUM_STATE_ENABLED:
  3702. case RFAL_WUM_STATE_ENABLED_WOKE:
  3703. irqs = st25r3916GetInterrupt(
  3704. (ST25R3916_IRQ_MASK_WT | ST25R3916_IRQ_MASK_WAM | ST25R3916_IRQ_MASK_WPH |
  3705. ST25R3916_IRQ_MASK_WCAP));
  3706. if(irqs == ST25R3916_IRQ_MASK_NONE) {
  3707. break; /* No interrupt to process */
  3708. }
  3709. /*******************************************************************************/
  3710. /* Check and mark which measurement(s) cause interrupt */
  3711. if((irqs & ST25R3916_IRQ_MASK_WAM) != 0U) {
  3712. st25r3916ReadRegister(ST25R3916_REG_AMPLITUDE_MEASURE_RESULT, &reg);
  3713. gRFAL.wum.state = RFAL_WUM_STATE_ENABLED_WOKE;
  3714. }
  3715. if((irqs & ST25R3916_IRQ_MASK_WPH) != 0U) {
  3716. st25r3916ReadRegister(ST25R3916_REG_PHASE_MEASURE_RESULT, &reg);
  3717. gRFAL.wum.state = RFAL_WUM_STATE_ENABLED_WOKE;
  3718. }
  3719. if((irqs & ST25R3916_IRQ_MASK_WCAP) != 0U) {
  3720. st25r3916ReadRegister(ST25R3916_REG_CAPACITANCE_MEASURE_RESULT, &reg);
  3721. gRFAL.wum.state = RFAL_WUM_STATE_ENABLED_WOKE;
  3722. }
  3723. if((irqs & ST25R3916_IRQ_MASK_WT) != 0U) {
  3724. /*******************************************************************************/
  3725. if(gRFAL.wum.cfg.swTagDetect) {
  3726. /* Enable Ready mode and wait the settle time */
  3727. st25r3916ChangeRegisterBits(
  3728. ST25R3916_REG_OP_CONTROL,
  3729. (ST25R3916_REG_OP_CONTROL_en | ST25R3916_REG_OP_CONTROL_wu),
  3730. ST25R3916_REG_OP_CONTROL_en);
  3731. platformDelay(RFAL_ST25R3916_AAT_SETTLE);
  3732. /*******************************************************************************/
  3733. if(gRFAL.wum.cfg.indAmp.enabled) {
  3734. /* Perform amplitude measurement */
  3735. st25r3916MeasureAmplitude(&reg);
  3736. /* Convert inputs to TD format */
  3737. value = rfalConvTDFormat(reg);
  3738. delta = rfalConvTDFormat(gRFAL.wum.cfg.indAmp.delta);
  3739. /* Set first measurement as reference */
  3740. if(gRFAL.wum.cfg.indAmp.reference == 0U) {
  3741. gRFAL.wum.cfg.indAmp.reference = value;
  3742. }
  3743. /* Check if device should be woken */
  3744. if((value >= (gRFAL.wum.cfg.indAmp.reference + delta)) ||
  3745. (value <= (gRFAL.wum.cfg.indAmp.reference - delta))) {
  3746. gRFAL.wum.state = RFAL_WUM_STATE_ENABLED_WOKE;
  3747. break;
  3748. }
  3749. /* Update moving reference if enabled */
  3750. if(gRFAL.wum.cfg.indAmp.autoAvg) {
  3751. gRFAL.wum.cfg.indAmp.reference = rfalWakeUpModeFilter(
  3752. gRFAL.wum.cfg.indAmp.reference,
  3753. value,
  3754. (RFAL_WU_MIN_WEIGHT_VAL << (uint8_t)gRFAL.wum.cfg.indAmp.aaWeight));
  3755. }
  3756. }
  3757. /*******************************************************************************/
  3758. if(gRFAL.wum.cfg.indPha.enabled) {
  3759. /* Perform Phase measurement */
  3760. st25r3916MeasurePhase(&reg);
  3761. /* Convert inputs to TD format */
  3762. value = rfalConvTDFormat(reg);
  3763. delta = rfalConvTDFormat(gRFAL.wum.cfg.indPha.delta);
  3764. /* Set first measurement as reference */
  3765. if(gRFAL.wum.cfg.indPha.reference == 0U) {
  3766. gRFAL.wum.cfg.indPha.reference = value;
  3767. }
  3768. /* Check if device should be woken */
  3769. if((value >= (gRFAL.wum.cfg.indPha.reference + delta)) ||
  3770. (value <= (gRFAL.wum.cfg.indPha.reference - delta))) {
  3771. gRFAL.wum.state = RFAL_WUM_STATE_ENABLED_WOKE;
  3772. break;
  3773. }
  3774. /* Update moving reference if enabled */
  3775. if(gRFAL.wum.cfg.indPha.autoAvg) {
  3776. gRFAL.wum.cfg.indPha.reference = rfalWakeUpModeFilter(
  3777. gRFAL.wum.cfg.indPha.reference,
  3778. value,
  3779. (RFAL_WU_MIN_WEIGHT_VAL << (uint8_t)gRFAL.wum.cfg.indPha.aaWeight));
  3780. }
  3781. }
  3782. /* Re-Enable low power Wake-Up mode for wto to trigger another measurement(s) */
  3783. st25r3916ChangeRegisterBits(
  3784. ST25R3916_REG_OP_CONTROL,
  3785. (ST25R3916_REG_OP_CONTROL_en | ST25R3916_REG_OP_CONTROL_wu),
  3786. (ST25R3916_REG_OP_CONTROL_wu));
  3787. }
  3788. }
  3789. break;
  3790. default:
  3791. /* MISRA 16.4: no empty default statement (a comment being enough) */
  3792. break;
  3793. }
  3794. }
  3795. /*******************************************************************************/
  3796. ReturnCode rfalWakeUpModeStop(void) {
  3797. /* Check if RFAL is in Wake-up mode */
  3798. if(gRFAL.state != RFAL_STATE_WUM) {
  3799. return ERR_WRONG_STATE;
  3800. }
  3801. gRFAL.wum.state = RFAL_WUM_STATE_NOT_INIT;
  3802. /* Disable Wake-Up Mode */
  3803. st25r3916ClrRegisterBits(ST25R3916_REG_OP_CONTROL, ST25R3916_REG_OP_CONTROL_wu);
  3804. st25r3916DisableInterrupts(
  3805. (ST25R3916_IRQ_MASK_WT | ST25R3916_IRQ_MASK_WAM | ST25R3916_IRQ_MASK_WPH |
  3806. ST25R3916_IRQ_MASK_WCAP));
  3807. /* Re-Enable External Field Detector as: Automatics */
  3808. st25r3916ChangeRegisterBits(
  3809. ST25R3916_REG_OP_CONTROL,
  3810. ST25R3916_REG_OP_CONTROL_en_fd_mask,
  3811. ST25R3916_REG_OP_CONTROL_en_fd_auto_efd);
  3812. /* Re-Enable the Oscillator */
  3813. st25r3916OscOn();
  3814. /* Set Analog configurations for Wake-up Off event */
  3815. rfalSetAnalogConfig((RFAL_ANALOG_CONFIG_TECH_CHIP | RFAL_ANALOG_CONFIG_CHIP_WAKEUP_OFF));
  3816. return ERR_NONE;
  3817. }
  3818. #endif /* RFAL_FEATURE_WAKEUP_MODE */
  3819. /*******************************************************************************
  3820. * Low-Power Mode *
  3821. *******************************************************************************/
  3822. #if RFAL_FEATURE_LOWPOWER_MODE
  3823. /*******************************************************************************/
  3824. ReturnCode rfalLowPowerModeStart(void) {
  3825. /* Check if RFAL is not initialized */
  3826. if(gRFAL.state < RFAL_STATE_INIT) {
  3827. return ERR_WRONG_STATE;
  3828. }
  3829. /* Stop any ongoing activity and set the device in low power by disabling oscillator, transmitter, receiver and external field detector */
  3830. st25r3916ExecuteCommand(ST25R3916_CMD_STOP);
  3831. st25r3916ClrRegisterBits(
  3832. ST25R3916_REG_OP_CONTROL,
  3833. (ST25R3916_REG_OP_CONTROL_en | ST25R3916_REG_OP_CONTROL_rx_en |
  3834. ST25R3916_REG_OP_CONTROL_wu | ST25R3916_REG_OP_CONTROL_tx_en |
  3835. ST25R3916_REG_OP_CONTROL_en_fd_mask));
  3836. rfalSetAnalogConfig((RFAL_ANALOG_CONFIG_TECH_CHIP | RFAL_ANALOG_CONFIG_CHIP_LOWPOWER_ON));
  3837. gRFAL.state = RFAL_STATE_IDLE;
  3838. gRFAL.lpm.isRunning = true;
  3839. platformDisableIrqCallback();
  3840. return ERR_NONE;
  3841. }
  3842. /*******************************************************************************/
  3843. ReturnCode rfalLowPowerModeStop(void) {
  3844. ReturnCode ret;
  3845. platformEnableIrqCallback();
  3846. /* Check if RFAL is on right state */
  3847. if(!gRFAL.lpm.isRunning) {
  3848. return ERR_WRONG_STATE;
  3849. }
  3850. /* Re-enable device */
  3851. EXIT_ON_ERR(ret, st25r3916OscOn());
  3852. st25r3916ChangeRegisterBits(
  3853. ST25R3916_REG_OP_CONTROL,
  3854. ST25R3916_REG_OP_CONTROL_en_fd_mask,
  3855. ST25R3916_REG_OP_CONTROL_en_fd_auto_efd);
  3856. rfalSetAnalogConfig((RFAL_ANALOG_CONFIG_TECH_CHIP | RFAL_ANALOG_CONFIG_CHIP_LOWPOWER_OFF));
  3857. gRFAL.state = RFAL_STATE_INIT;
  3858. return ERR_NONE;
  3859. }
  3860. #endif /* RFAL_FEATURE_LOWPOWER_MODE */
  3861. /*******************************************************************************
  3862. * RF Chip *
  3863. *******************************************************************************/
  3864. /*******************************************************************************/
  3865. ReturnCode rfalChipWriteReg(uint16_t reg, const uint8_t* values, uint8_t len) {
  3866. if(!st25r3916IsRegValid((uint8_t)reg)) {
  3867. return ERR_PARAM;
  3868. }
  3869. return st25r3916WriteMultipleRegisters((uint8_t)reg, values, len);
  3870. }
  3871. /*******************************************************************************/
  3872. ReturnCode rfalChipReadReg(uint16_t reg, uint8_t* values, uint8_t len) {
  3873. if(!st25r3916IsRegValid((uint8_t)reg)) {
  3874. return ERR_PARAM;
  3875. }
  3876. return st25r3916ReadMultipleRegisters((uint8_t)reg, values, len);
  3877. }
  3878. /*******************************************************************************/
  3879. ReturnCode rfalChipExecCmd(uint16_t cmd) {
  3880. if(!st25r3916IsCmdValid((uint8_t)cmd)) {
  3881. return ERR_PARAM;
  3882. }
  3883. return st25r3916ExecuteCommand((uint8_t)cmd);
  3884. }
  3885. /*******************************************************************************/
  3886. ReturnCode rfalChipWriteTestReg(uint16_t reg, uint8_t value) {
  3887. return st25r3916WriteTestRegister((uint8_t)reg, value);
  3888. }
  3889. /*******************************************************************************/
  3890. ReturnCode rfalChipReadTestReg(uint16_t reg, uint8_t* value) {
  3891. return st25r3916ReadTestRegister((uint8_t)reg, value);
  3892. }
  3893. /*******************************************************************************/
  3894. ReturnCode rfalChipChangeRegBits(uint16_t reg, uint8_t valueMask, uint8_t value) {
  3895. if(!st25r3916IsRegValid((uint8_t)reg)) {
  3896. return ERR_PARAM;
  3897. }
  3898. return st25r3916ChangeRegisterBits((uint8_t)reg, valueMask, value);
  3899. }
  3900. /*******************************************************************************/
  3901. ReturnCode rfalChipChangeTestRegBits(uint16_t reg, uint8_t valueMask, uint8_t value) {
  3902. st25r3916ChangeTestRegisterBits((uint8_t)reg, valueMask, value);
  3903. return ERR_NONE;
  3904. }
  3905. /*******************************************************************************/
  3906. ReturnCode rfalChipSetRFO(uint8_t rfo) {
  3907. return st25r3916ChangeRegisterBits(
  3908. ST25R3916_REG_TX_DRIVER, ST25R3916_REG_TX_DRIVER_d_res_mask, rfo);
  3909. }
  3910. /*******************************************************************************/
  3911. ReturnCode rfalChipGetRFO(uint8_t* result) {
  3912. ReturnCode ret;
  3913. ret = st25r3916ReadRegister(ST25R3916_REG_TX_DRIVER, result);
  3914. (*result) = ((*result) & ST25R3916_REG_TX_DRIVER_d_res_mask);
  3915. return ret;
  3916. }
  3917. /*******************************************************************************/
  3918. ReturnCode rfalChipMeasureAmplitude(uint8_t* result) {
  3919. ReturnCode err;
  3920. uint8_t reg_opc, reg_mode, reg_conf1, reg_conf2;
  3921. /* Save registers which will be adjusted below */
  3922. st25r3916ReadRegister(ST25R3916_REG_OP_CONTROL, &reg_opc);
  3923. st25r3916ReadRegister(ST25R3916_REG_MODE, &reg_mode);
  3924. st25r3916ReadRegister(ST25R3916_REG_RX_CONF1, &reg_conf1);
  3925. st25r3916ReadRegister(ST25R3916_REG_RX_CONF2, &reg_conf2);
  3926. /* Set values as per defaults of DS. These regs/bits influence receiver chain and change amplitude */
  3927. /* Doing so achieves an amplitude comparable over a complete polling cylce */
  3928. st25r3916WriteRegister(ST25R3916_REG_OP_CONTROL, (reg_opc & ~ST25R3916_REG_OP_CONTROL_rx_chn));
  3929. st25r3916WriteRegister(
  3930. ST25R3916_REG_MODE,
  3931. ST25R3916_REG_MODE_om_iso14443a | ST25R3916_REG_MODE_targ_init |
  3932. ST25R3916_REG_MODE_tr_am_ook | ST25R3916_REG_MODE_nfc_ar_off);
  3933. st25r3916WriteRegister(
  3934. ST25R3916_REG_RX_CONF1, (reg_conf1 & ~ST25R3916_REG_RX_CONF1_ch_sel_AM));
  3935. st25r3916WriteRegister(
  3936. ST25R3916_REG_RX_CONF2,
  3937. ((reg_conf2 & ~(ST25R3916_REG_RX_CONF2_demod_mode | ST25R3916_REG_RX_CONF2_amd_sel)) |
  3938. ST25R3916_REG_RX_CONF2_amd_sel_peak));
  3939. /* Perform the actual measurement */
  3940. err = st25r3916MeasureAmplitude(result);
  3941. /* Restore values */
  3942. st25r3916WriteRegister(ST25R3916_REG_OP_CONTROL, reg_opc);
  3943. st25r3916WriteRegister(ST25R3916_REG_MODE, reg_mode);
  3944. st25r3916WriteRegister(ST25R3916_REG_RX_CONF1, reg_conf1);
  3945. st25r3916WriteRegister(ST25R3916_REG_RX_CONF2, reg_conf2);
  3946. return err;
  3947. }
  3948. /*******************************************************************************/
  3949. ReturnCode rfalChipMeasurePhase(uint8_t* result) {
  3950. st25r3916MeasurePhase(result);
  3951. return ERR_NONE;
  3952. }
  3953. /*******************************************************************************/
  3954. ReturnCode rfalChipMeasureCapacitance(uint8_t* result) {
  3955. st25r3916MeasureCapacitance(result);
  3956. return ERR_NONE;
  3957. }
  3958. /*******************************************************************************/
  3959. ReturnCode rfalChipMeasurePowerSupply(uint8_t param, uint8_t* result) {
  3960. *result = st25r3916MeasurePowerSupply(param);
  3961. return ERR_NONE;
  3962. }
  3963. /*******************************************************************************/
  3964. extern uint8_t invalid_size_of_stream_configs
  3965. [(sizeof(struct st25r3916StreamConfig) == sizeof(struct iso15693StreamConfig)) ? 1 : (-1)];