rfal_rfst25r3916.c 197 KB

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