rfal_rfst25r3916.c 198 KB

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