stm32f4xx_hal_fmpi2c.c 165 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_fmpi2c.c
  4. * @author MCD Application Team
  5. * @brief FMPI2C HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Inter Integrated Circuit (FMPI2C) peripheral:
  8. * + Initialization and de-initialization functions
  9. * + IO operation functions
  10. * + Peripheral State and Errors functions
  11. *
  12. @verbatim
  13. ==============================================================================
  14. ##### How to use this driver #####
  15. ==============================================================================
  16. [..]
  17. The FMPI2C HAL driver can be used as follows:
  18. (#) Declare a FMPI2C_HandleTypeDef handle structure, for example:
  19. FMPI2C_HandleTypeDef hfmpi2c;
  20. (#)Initialize the FMPI2C low level resources by implementing the HAL_FMPI2C_MspInit() API:
  21. (##) Enable the FMPI2Cx interface clock
  22. (##) FMPI2C pins configuration
  23. (+++) Enable the clock for the FMPI2C GPIOs
  24. (+++) Configure FMPI2C pins as alternate function open-drain
  25. (##) NVIC configuration if you need to use interrupt process
  26. (+++) Configure the FMPI2Cx interrupt priority
  27. (+++) Enable the NVIC FMPI2C IRQ Channel
  28. (##) DMA Configuration if you need to use DMA process
  29. (+++) Declare a DMA_HandleTypeDef handle structure for the transmit or receive stream
  30. (+++) Enable the DMAx interface clock using
  31. (+++) Configure the DMA handle parameters
  32. (+++) Configure the DMA Tx or Rx stream
  33. (+++) Associate the initialized DMA handle to the hfmpi2c DMA Tx or Rx handle
  34. (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on
  35. the DMA Tx or Rx stream
  36. (#) Configure the Communication Clock Timing, Own Address1, Master Addressing mode, Dual Addressing mode,
  37. Own Address2, Own Address2 Mask, General call and Nostretch mode in the hfmpi2c Init structure.
  38. (#) Initialize the FMPI2C registers by calling the HAL_FMPI2C_Init(), configures also the low level Hardware
  39. (GPIO, CLOCK, NVIC...etc) by calling the customized HAL_FMPI2C_MspInit(&hfmpi2c) API.
  40. (#) To check if target device is ready for communication, use the function HAL_FMPI2C_IsDeviceReady()
  41. (#) For FMPI2C IO and IO MEM operations, three operation modes are available within this driver :
  42. *** Polling mode IO operation ***
  43. =================================
  44. [..]
  45. (+) Transmit in master mode an amount of data in blocking mode using HAL_FMPI2C_Master_Transmit()
  46. (+) Receive in master mode an amount of data in blocking mode using HAL_FMPI2C_Master_Receive()
  47. (+) Transmit in slave mode an amount of data in blocking mode using HAL_FMPI2C_Slave_Transmit()
  48. (+) Receive in slave mode an amount of data in blocking mode using HAL_FMPI2C_Slave_Receive()
  49. *** Polling mode IO MEM operation ***
  50. =====================================
  51. [..]
  52. (+) Write an amount of data in blocking mode to a specific memory address using HAL_FMPI2C_Mem_Write()
  53. (+) Read an amount of data in blocking mode from a specific memory address using HAL_FMPI2C_Mem_Read()
  54. *** Interrupt mode IO operation ***
  55. ===================================
  56. [..]
  57. (+) Transmit in master mode an amount of data in non-blocking mode using HAL_FMPI2C_Master_Transmit_IT()
  58. (+) At transmission end of transfer, HAL_FMPI2C_MasterTxCpltCallback() is executed and user can
  59. add his own code by customization of function pointer HAL_FMPI2C_MasterTxCpltCallback()
  60. (+) Receive in master mode an amount of data in non-blocking mode using HAL_FMPI2C_Master_Receive_IT()
  61. (+) At reception end of transfer, HAL_FMPI2C_MasterRxCpltCallback() is executed and user can
  62. add his own code by customization of function pointer HAL_FMPI2C_MasterRxCpltCallback()
  63. (+) Transmit in slave mode an amount of data in non-blocking mode using HAL_FMPI2C_Slave_Transmit_IT()
  64. (+) At transmission end of transfer, HAL_FMPI2C_SlaveTxCpltCallback() is executed and user can
  65. add his own code by customization of function pointer HAL_FMPI2C_SlaveTxCpltCallback()
  66. (+) Receive in slave mode an amount of data in non-blocking mode using HAL_FMPI2C_Slave_Receive_IT()
  67. (+) At reception end of transfer, HAL_FMPI2C_SlaveRxCpltCallback() is executed and user can
  68. add his own code by customization of function pointer HAL_FMPI2C_SlaveRxCpltCallback()
  69. (+) In case of transfer Error, HAL_FMPI2C_ErrorCallback() function is executed and user can
  70. add his own code by customization of function pointer HAL_FMPI2C_ErrorCallback()
  71. (+) Abort a master FMPI2C process communication with Interrupt using HAL_FMPI2C_Master_Abort_IT()
  72. (+) End of abort process, HAL_FMPI2C_AbortCpltCallback() is executed and user can
  73. add his own code by customization of function pointer HAL_FMPI2C_AbortCpltCallback()
  74. (+) Discard a slave FMPI2C process communication using __HAL_FMPI2C_GENERATE_NACK() macro.
  75. This action will inform Master to generate a Stop condition to discard the communication.
  76. *** Interrupt mode IO sequential operation ***
  77. ==============================================
  78. [..]
  79. (@) These interfaces allow to manage a sequential transfer with a repeated start condition
  80. when a direction change during transfer
  81. [..]
  82. (+) A specific option field manage the different steps of a sequential transfer
  83. (+) Option field values are defined through @ref FMPI2C_XFEROPTIONS and are listed below:
  84. (++) FMPI2C_FIRST_AND_LAST_FRAME: No sequential usage, functionnal is same as associated interfaces in no sequential mode
  85. (++) FMPI2C_FIRST_FRAME: Sequential usage, this option allow to manage a sequence with start condition, address
  86. and data to transfer without a final stop condition
  87. (++) FMPI2C_FIRST_AND_NEXT_FRAME: Sequential usage (Master only), this option allow to manage a sequence with start condition, address
  88. and data to transfer without a final stop condition, an then permit a call the same master sequential interface
  89. several times (like HAL_FMPI2C_Master_Sequential_Transmit_IT() then HAL_FMPI2C_Master_Sequential_Transmit_IT())
  90. (++) FMPI2C_NEXT_FRAME: Sequential usage, this option allow to manage a sequence with a restart condition, address
  91. and with new data to transfer if the direction change or manage only the new data to transfer
  92. if no direction change and without a final stop condition in both cases
  93. (++) FMPI2C_LAST_FRAME: Sequential usage, this option allow to manage a sequance with a restart condition, address
  94. and with new data to transfer if the direction change or manage only the new data to transfer
  95. if no direction change and with a final stop condition in both cases
  96. (+) Differents sequential FMPI2C interfaces are listed below:
  97. (++) Sequential transmit in master FMPI2C mode an amount of data in non-blocking mode using HAL_FMPI2C_Master_Sequential_Transmit_IT()
  98. (+++) At transmission end of current frame transfer, HAL_FMPI2C_MasterTxCpltCallback() is executed and user can
  99. add his own code by customization of function pointer HAL_FMPI2C_MasterTxCpltCallback()
  100. (++) Sequential receive in master FMPI2C mode an amount of data in non-blocking mode using HAL_FMPI2C_Master_Sequential_Receive_IT()
  101. (+++) At reception end of current frame transfer, HAL_FMPI2C_MasterRxCpltCallback() is executed and user can
  102. add his own code by customization of function pointer HAL_FMPI2C_MasterRxCpltCallback()
  103. (++) Abort a master FMPI2C process communication with Interrupt using HAL_FMPI2C_Master_Abort_IT()
  104. (+++) End of abort process, HAL_FMPI2C_AbortCpltCallback() is executed and user can
  105. add his own code by customization of function pointer HAL_FMPI2C_AbortCpltCallback()
  106. (++) Enable/disable the Address listen mode in slave FMPI2C mode using HAL_FMPI2C_EnableListen_IT() HAL_FMPI2C_DisableListen_IT()
  107. (+++) When address slave FMPI2C match, HAL_FMPI2C_AddrCallback() is executed and user can
  108. add his own code to check the Address Match Code and the transmission direction request by master (Write/Read).
  109. (+++) At Listen mode end HAL_FMPI2C_ListenCpltCallback() is executed and user can
  110. add his own code by customization of function pointer HAL_FMPI2C_ListenCpltCallback()
  111. (++) Sequential transmit in slave FMPI2C mode an amount of data in non-blocking mode using HAL_FMPI2C_Slave_Sequential_Transmit_IT()
  112. (+++) At transmission end of current frame transfer, HAL_FMPI2C_SlaveTxCpltCallback() is executed and user can
  113. add his own code by customization of function pointer HAL_FMPI2C_SlaveTxCpltCallback()
  114. (++) Sequential receive in slave FMPI2C mode an amount of data in non-blocking mode using HAL_FMPI2C_Slave_Sequential_Receive_IT()
  115. (+++) At reception end of current frame transfer, HAL_FMPI2C_SlaveRxCpltCallback() is executed and user can
  116. add his own code by customization of function pointer HAL_FMPI2C_SlaveRxCpltCallback()
  117. (++) In case of transfer Error, HAL_FMPI2C_ErrorCallback() function is executed and user can
  118. add his own code by customization of function pointer HAL_FMPI2C_ErrorCallback()
  119. (++) Abort a master FMPI2C process communication with Interrupt using HAL_FMPI2C_Master_Abort_IT()
  120. (++) End of abort process, HAL_FMPI2C_AbortCpltCallback() is executed and user can
  121. add his own code by customization of function pointer HAL_FMPI2C_AbortCpltCallback()
  122. (++) Discard a slave FMPI2C process communication using __HAL_FMPI2C_GENERATE_NACK() macro.
  123. This action will inform Master to generate a Stop condition to discard the communication.
  124. *** Interrupt mode IO MEM operation ***
  125. =======================================
  126. [..]
  127. (+) Write an amount of data in non-blocking mode with Interrupt to a specific memory address using
  128. HAL_FMPI2C_Mem_Write_IT()
  129. (+) At Memory end of write transfer, HAL_FMPI2C_MemTxCpltCallback() is executed and user can
  130. add his own code by customization of function pointer HAL_FMPI2C_MemTxCpltCallback()
  131. (+) Read an amount of data in non-blocking mode with Interrupt from a specific memory address using
  132. HAL_FMPI2C_Mem_Read_IT()
  133. (+) At Memory end of read transfer, HAL_FMPI2C_MemRxCpltCallback() is executed and user can
  134. add his own code by customization of function pointer HAL_FMPI2C_MemRxCpltCallback()
  135. (+) In case of transfer Error, HAL_FMPI2C_ErrorCallback() function is executed and user can
  136. add his own code by customization of function pointer HAL_FMPI2C_ErrorCallback()
  137. *** DMA mode IO operation ***
  138. ==============================
  139. [..]
  140. (+) Transmit in master mode an amount of data in non-blocking mode (DMA) using
  141. HAL_FMPI2C_Master_Transmit_DMA()
  142. (+) At transmission end of transfer, HAL_FMPI2C_MasterTxCpltCallback() is executed and user can
  143. add his own code by customization of function pointer HAL_FMPI2C_MasterTxCpltCallback()
  144. (+) Receive in master mode an amount of data in non-blocking mode (DMA) using
  145. HAL_FMPI2C_Master_Receive_DMA()
  146. (+) At reception end of transfer, HAL_FMPI2C_MasterRxCpltCallback() is executed and user can
  147. add his own code by customization of function pointer HAL_FMPI2C_MasterRxCpltCallback()
  148. (+) Transmit in slave mode an amount of data in non-blocking mode (DMA) using
  149. HAL_FMPI2C_Slave_Transmit_DMA()
  150. (+) At transmission end of transfer, HAL_FMPI2C_SlaveTxCpltCallback() is executed and user can
  151. add his own code by customization of function pointer HAL_FMPI2C_SlaveTxCpltCallback()
  152. (+) Receive in slave mode an amount of data in non-blocking mode (DMA) using
  153. HAL_FMPI2C_Slave_Receive_DMA()
  154. (+) At reception end of transfer, HAL_FMPI2C_SlaveRxCpltCallback() is executed and user can
  155. add his own code by customization of function pointer HAL_FMPI2C_SlaveRxCpltCallback()
  156. (+) In case of transfer Error, HAL_FMPI2C_ErrorCallback() function is executed and user can
  157. add his own code by customization of function pointer HAL_FMPI2C_ErrorCallback()
  158. (+) Abort a master FMPI2C process communication with Interrupt using HAL_FMPI2C_Master_Abort_IT()
  159. (+) End of abort process, HAL_FMPI2C_AbortCpltCallback() is executed and user can
  160. add his own code by customization of function pointer HAL_FMPI2C_AbortCpltCallback()
  161. (+) Discard a slave FMPI2C process communication using __HAL_FMPI2C_GENERATE_NACK() macro.
  162. This action will inform Master to generate a Stop condition to discard the communication.
  163. *** DMA mode IO MEM operation ***
  164. =================================
  165. [..]
  166. (+) Write an amount of data in non-blocking mode with DMA to a specific memory address using
  167. HAL_FMPI2C_Mem_Write_DMA()
  168. (+) At Memory end of write transfer, HAL_FMPI2C_MemTxCpltCallback() is executed and user can
  169. add his own code by customization of function pointer HAL_FMPI2C_MemTxCpltCallback()
  170. (+) Read an amount of data in non-blocking mode with DMA from a specific memory address using
  171. HAL_FMPI2C_Mem_Read_DMA()
  172. (+) At Memory end of read transfer, HAL_FMPI2C_MemRxCpltCallback() is executed and user can
  173. add his own code by customization of function pointer HAL_FMPI2C_MemRxCpltCallback()
  174. (+) In case of transfer Error, HAL_FMPI2C_ErrorCallback() function is executed and user can
  175. add his own code by customization of function pointer HAL_FMPI2C_ErrorCallback()
  176. *** FMPI2C HAL driver macros list ***
  177. ==================================
  178. [..]
  179. Below the list of most used macros in FMPI2C HAL driver.
  180. (+) __HAL_FMPI2C_ENABLE: Enable the FMPI2C peripheral
  181. (+) __HAL_FMPI2C_DISABLE: Disable the FMPI2C peripheral
  182. (+) __HAL_FMPI2C_GENERATE_NACK: Generate a Non-Acknowledge FMPI2C peripheral in Slave mode
  183. (+) __HAL_FMPI2C_GET_FLAG: Check whether the specified FMPI2C flag is set or not
  184. (+) __HAL_FMPI2C_CLEAR_FLAG: Clear the specified FMPI2C pending flag
  185. (+) __HAL_FMPI2C_ENABLE_IT: Enable the specified FMPI2C interrupt
  186. (+) __HAL_FMPI2C_DISABLE_IT: Disable the specified FMPI2C interrupt
  187. [..]
  188. (@) You can refer to the FMPI2C HAL driver header file for more useful macros
  189. @endverbatim
  190. ******************************************************************************
  191. * @attention
  192. *
  193. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  194. *
  195. * Redistribution and use in source and binary forms, with or without modification,
  196. * are permitted provided that the following conditions are met:
  197. * 1. Redistributions of source code must retain the above copyright notice,
  198. * this list of conditions and the following disclaimer.
  199. * 2. Redistributions in binary form must reproduce the above copyright notice,
  200. * this list of conditions and the following disclaimer in the documentation
  201. * and/or other materials provided with the distribution.
  202. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  203. * may be used to endorse or promote products derived from this software
  204. * without specific prior written permission.
  205. *
  206. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  207. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  208. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  209. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  210. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  211. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  212. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  213. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  214. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  215. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  216. *
  217. ******************************************************************************
  218. */
  219. /* Includes ------------------------------------------------------------------*/
  220. #include "stm32f4xx_hal.h"
  221. /** @addtogroup STM32F4xx_HAL_Driver
  222. * @{
  223. */
  224. /** @defgroup FMPI2C FMPI2C
  225. * @brief FMPI2C HAL module driver
  226. * @{
  227. */
  228. #ifdef HAL_FMPI2C_MODULE_ENABLED
  229. #if defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx) || defined(STM32F446xx) || defined(STM32F412Zx) ||\
  230. defined(STM32F412Vx) || defined(STM32F412Rx) || defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx)
  231. /* Private typedef -----------------------------------------------------------*/
  232. /* Private define ------------------------------------------------------------*/
  233. /** @defgroup FMPI2C_Private_Define FMPI2C Private Define
  234. * @{
  235. */
  236. #define TIMING_CLEAR_MASK (0xF0FFFFFFU) /*!< FMPI2C TIMING clear register Mask */
  237. #define FMPI2C_TIMEOUT_ADDR (10000U) /*!< 10 s */
  238. #define FMPI2C_TIMEOUT_BUSY (25U) /*!< 25 ms */
  239. #define FMPI2C_TIMEOUT_DIR (25U) /*!< 25 ms */
  240. #define FMPI2C_TIMEOUT_RXNE (25U) /*!< 25 ms */
  241. #define FMPI2C_TIMEOUT_STOPF (25U) /*!< 25 ms */
  242. #define FMPI2C_TIMEOUT_TC (25U) /*!< 25 ms */
  243. #define FMPI2C_TIMEOUT_TCR (25U) /*!< 25 ms */
  244. #define FMPI2C_TIMEOUT_TXIS (25U) /*!< 25 ms */
  245. #define FMPI2C_TIMEOUT_FLAG (25U) /*!< 25 ms */
  246. #define MAX_NBYTE_SIZE 255U
  247. #define SlaveAddr_SHIFT 7U
  248. #define SlaveAddr_MSK 0x06U
  249. /* Private define for @ref PreviousState usage */
  250. #define FMPI2C_STATE_MSK ((uint32_t)((HAL_FMPI2C_STATE_BUSY_TX | HAL_FMPI2C_STATE_BUSY_RX) & (~((uint32_t)HAL_FMPI2C_STATE_READY)))) /*!< Mask State define, keep only RX and TX bits */
  251. #define FMPI2C_STATE_NONE ((uint32_t)(HAL_FMPI2C_MODE_NONE)) /*!< Default Value */
  252. #define FMPI2C_STATE_MASTER_BUSY_TX ((uint32_t)((HAL_FMPI2C_STATE_BUSY_TX & FMPI2C_STATE_MSK) | HAL_FMPI2C_MODE_MASTER)) /*!< Master Busy TX, combinaison of State LSB and Mode enum */
  253. #define FMPI2C_STATE_MASTER_BUSY_RX ((uint32_t)((HAL_FMPI2C_STATE_BUSY_RX & FMPI2C_STATE_MSK) | HAL_FMPI2C_MODE_MASTER)) /*!< Master Busy RX, combinaison of State LSB and Mode enum */
  254. #define FMPI2C_STATE_SLAVE_BUSY_TX ((uint32_t)((HAL_FMPI2C_STATE_BUSY_TX & FMPI2C_STATE_MSK) | HAL_FMPI2C_MODE_SLAVE)) /*!< Slave Busy TX, combinaison of State LSB and Mode enum */
  255. #define FMPI2C_STATE_SLAVE_BUSY_RX ((uint32_t)((HAL_FMPI2C_STATE_BUSY_RX & FMPI2C_STATE_MSK) | HAL_FMPI2C_MODE_SLAVE)) /*!< Slave Busy RX, combinaison of State LSB and Mode enum */
  256. #define FMPI2C_STATE_MEM_BUSY_TX ((uint32_t)((HAL_FMPI2C_STATE_BUSY_TX & FMPI2C_STATE_MSK) | HAL_FMPI2C_MODE_MEM)) /*!< Memory Busy TX, combinaison of State LSB and Mode enum */
  257. #define FMPI2C_STATE_MEM_BUSY_RX ((uint32_t)((HAL_FMPI2C_STATE_BUSY_RX & FMPI2C_STATE_MSK) | HAL_FMPI2C_MODE_MEM)) /*!< Memory Busy RX, combinaison of State LSB and Mode enum */
  258. /* Private define to centralize the enable/disable of Interrupts */
  259. #define FMPI2C_XFER_TX_IT (0x00000001U)
  260. #define FMPI2C_XFER_RX_IT (0x00000002U)
  261. #define FMPI2C_XFER_LISTEN_IT (0x00000004U)
  262. #define FMPI2C_XFER_ERROR_IT (0x00000011U)
  263. #define FMPI2C_XFER_CPLT_IT (0x00000012U)
  264. #define FMPI2C_XFER_RELOAD_IT (0x00000012U)
  265. /* Private define Sequential Transfer Options default/reset value */
  266. #define FMPI2C_NO_OPTION_FRAME (0xFFFF0000U)
  267. /**
  268. * @}
  269. */
  270. /* Private macro -------------------------------------------------------------*/
  271. #define FMPI2C_GET_DMA_REMAIN_DATA(__HANDLE__) ((((__HANDLE__)->State) == HAL_FMPI2C_STATE_BUSY_TX) ? \
  272. ((uint32_t)(((DMA_Stream_TypeDef *)(__HANDLE__)->hdmatx->Instance)->NDTR)) : \
  273. ((uint32_t)(((DMA_Stream_TypeDef *)(__HANDLE__)->hdmarx->Instance)->NDTR)))
  274. /* Private variables ---------------------------------------------------------*/
  275. /* Private function prototypes -----------------------------------------------*/
  276. /** @defgroup FMPI2C_Private_Functions FMPI2C Private Functions
  277. * @{
  278. */
  279. /* Private functions to handle DMA transfer */
  280. static void FMPI2C_DMAMasterTransmitCplt(DMA_HandleTypeDef *hdma);
  281. static void FMPI2C_DMAMasterReceiveCplt(DMA_HandleTypeDef *hdma);
  282. static void FMPI2C_DMASlaveTransmitCplt(DMA_HandleTypeDef *hdma);
  283. static void FMPI2C_DMASlaveReceiveCplt(DMA_HandleTypeDef *hdma);
  284. static void FMPI2C_DMAError(DMA_HandleTypeDef *hdma);
  285. static void FMPI2C_DMAAbort(DMA_HandleTypeDef *hdma);
  286. /* Private functions to handle IT transfer */
  287. static void FMPI2C_ITAddrCplt(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags);
  288. static void FMPI2C_ITMasterSequentialCplt(FMPI2C_HandleTypeDef *hfmpi2c);
  289. static void FMPI2C_ITSlaveSequentialCplt(FMPI2C_HandleTypeDef *hfmpi2c);
  290. static void FMPI2C_ITMasterCplt(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags);
  291. static void FMPI2C_ITSlaveCplt(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags);
  292. static void FMPI2C_ITListenCplt(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags);
  293. static void FMPI2C_ITError(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ErrorCode);
  294. /* Private functions to handle IT transfer */
  295. static HAL_StatusTypeDef FMPI2C_RequestMemoryWrite(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart);
  296. static HAL_StatusTypeDef FMPI2C_RequestMemoryRead(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart);
  297. /* Private functions for FMPI2C transfer IRQ handler */
  298. static HAL_StatusTypeDef FMPI2C_Master_ISR_IT(struct __FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags, uint32_t ITSources);
  299. static HAL_StatusTypeDef FMPI2C_Slave_ISR_IT(struct __FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags, uint32_t ITSources);
  300. static HAL_StatusTypeDef FMPI2C_Master_ISR_DMA(struct __FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags, uint32_t ITSources);
  301. static HAL_StatusTypeDef FMPI2C_Slave_ISR_DMA(struct __FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags, uint32_t ITSources);
  302. /* Private functions to handle flags during polling transfer */
  303. static HAL_StatusTypeDef FMPI2C_WaitOnFlagUntilTimeout(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t Flag, FlagStatus Status, uint32_t Timeout, uint32_t Tickstart);
  304. static HAL_StatusTypeDef FMPI2C_WaitOnTXISFlagUntilTimeout(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t Timeout, uint32_t Tickstart);
  305. static HAL_StatusTypeDef FMPI2C_WaitOnRXNEFlagUntilTimeout(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t Timeout, uint32_t Tickstart);
  306. static HAL_StatusTypeDef FMPI2C_WaitOnSTOPFlagUntilTimeout(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t Timeout, uint32_t Tickstart);
  307. static HAL_StatusTypeDef FMPI2C_IsAcknowledgeFailed(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t Timeout, uint32_t Tickstart);
  308. /* Private functions to centralize the enable/disable of Interrupts */
  309. static HAL_StatusTypeDef FMPI2C_Enable_IRQ(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t InterruptRequest);
  310. static HAL_StatusTypeDef FMPI2C_Disable_IRQ(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t InterruptRequest);
  311. /* Private functions to flush TXDR register */
  312. static void FMPI2C_Flush_TXDR(FMPI2C_HandleTypeDef *hfmpi2c);
  313. /* Private functions to handle start, restart or stop a transfer */
  314. static void FMPI2C_TransferConfig(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint8_t Size, uint32_t Mode, uint32_t Request);
  315. /**
  316. * @}
  317. */
  318. /* Exported functions --------------------------------------------------------*/
  319. /** @defgroup FMPI2C_Exported_Functions FMPI2C Exported Functions
  320. * @{
  321. */
  322. /** @defgroup FMPI2C_Exported_Functions_Group1 Initialization and de-initialization functions
  323. * @brief Initialization and Configuration functions
  324. *
  325. @verbatim
  326. ===============================================================================
  327. ##### Initialization and de-initialization functions #####
  328. ===============================================================================
  329. [..] This subsection provides a set of functions allowing to initialize and
  330. deinitialize the FMPI2Cx peripheral:
  331. (+) User must Implement HAL_FMPI2C_MspInit() function in which he configures
  332. all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC ).
  333. (+) Call the function HAL_FMPI2C_Init() to configure the selected device with
  334. the selected configuration:
  335. (++) Clock Timing
  336. (++) Own Address 1
  337. (++) Addressing mode (Master, Slave)
  338. (++) Dual Addressing mode
  339. (++) Own Address 2
  340. (++) Own Address 2 Mask
  341. (++) General call mode
  342. (++) Nostretch mode
  343. (+) Call the function HAL_FMPI2C_DeInit() to restore the default configuration
  344. of the selected FMPI2Cx peripheral.
  345. @endverbatim
  346. * @{
  347. */
  348. /**
  349. * @brief Initializes the FMPI2C according to the specified parameters
  350. * in the FMPI2C_InitTypeDef and initialize the associated handle.
  351. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  352. * the configuration information for the specified FMPI2C.
  353. * @retval HAL status
  354. */
  355. HAL_StatusTypeDef HAL_FMPI2C_Init(FMPI2C_HandleTypeDef *hfmpi2c)
  356. {
  357. /* Check the FMPI2C handle allocation */
  358. if (hfmpi2c == NULL)
  359. {
  360. return HAL_ERROR;
  361. }
  362. /* Check the parameters */
  363. assert_param(IS_FMPI2C_ALL_INSTANCE(hfmpi2c->Instance));
  364. assert_param(IS_FMPI2C_OWN_ADDRESS1(hfmpi2c->Init.OwnAddress1));
  365. assert_param(IS_FMPI2C_ADDRESSING_MODE(hfmpi2c->Init.AddressingMode));
  366. assert_param(IS_FMPI2C_DUAL_ADDRESS(hfmpi2c->Init.DualAddressMode));
  367. assert_param(IS_FMPI2C_OWN_ADDRESS2(hfmpi2c->Init.OwnAddress2));
  368. assert_param(IS_FMPI2C_OWN_ADDRESS2_MASK(hfmpi2c->Init.OwnAddress2Masks));
  369. assert_param(IS_FMPI2C_GENERAL_CALL(hfmpi2c->Init.GeneralCallMode));
  370. assert_param(IS_FMPI2C_NO_STRETCH(hfmpi2c->Init.NoStretchMode));
  371. if (hfmpi2c->State == HAL_FMPI2C_STATE_RESET)
  372. {
  373. /* Allocate lock resource and initialize it */
  374. hfmpi2c->Lock = HAL_UNLOCKED;
  375. /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
  376. HAL_FMPI2C_MspInit(hfmpi2c);
  377. }
  378. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY;
  379. /* Disable the selected FMPI2C peripheral */
  380. __HAL_FMPI2C_DISABLE(hfmpi2c);
  381. /*---------------------------- FMPI2Cx TIMINGR Configuration ------------------*/
  382. /* Configure FMPI2Cx: Frequency range */
  383. hfmpi2c->Instance->TIMINGR = hfmpi2c->Init.Timing & TIMING_CLEAR_MASK;
  384. /*---------------------------- FMPI2Cx OAR1 Configuration ---------------------*/
  385. /* Disable Own Address1 before set the Own Address1 configuration */
  386. hfmpi2c->Instance->OAR1 &= ~FMPI2C_OAR1_OA1EN;
  387. /* Configure FMPI2Cx: Own Address1 and ack own address1 mode */
  388. if (hfmpi2c->Init.AddressingMode == FMPI2C_ADDRESSINGMODE_7BIT)
  389. {
  390. hfmpi2c->Instance->OAR1 = (FMPI2C_OAR1_OA1EN | hfmpi2c->Init.OwnAddress1);
  391. }
  392. else /* FMPI2C_ADDRESSINGMODE_10BIT */
  393. {
  394. hfmpi2c->Instance->OAR1 = (FMPI2C_OAR1_OA1EN | FMPI2C_OAR1_OA1MODE | hfmpi2c->Init.OwnAddress1);
  395. }
  396. /*---------------------------- FMPI2Cx CR2 Configuration ----------------------*/
  397. /* Configure FMPI2Cx: Addressing Master mode */
  398. if (hfmpi2c->Init.AddressingMode == FMPI2C_ADDRESSINGMODE_10BIT)
  399. {
  400. hfmpi2c->Instance->CR2 = (FMPI2C_CR2_ADD10);
  401. }
  402. /* Enable the AUTOEND by default, and enable NACK (should be disable only during Slave process */
  403. hfmpi2c->Instance->CR2 |= (FMPI2C_CR2_AUTOEND | FMPI2C_CR2_NACK);
  404. /*---------------------------- FMPI2Cx OAR2 Configuration ---------------------*/
  405. /* Disable Own Address2 before set the Own Address2 configuration */
  406. hfmpi2c->Instance->OAR2 &= ~FMPI2C_DUALADDRESS_ENABLE;
  407. /* Configure FMPI2Cx: Dual mode and Own Address2 */
  408. hfmpi2c->Instance->OAR2 = (hfmpi2c->Init.DualAddressMode | hfmpi2c->Init.OwnAddress2 | (hfmpi2c->Init.OwnAddress2Masks << 8));
  409. /*---------------------------- FMPI2Cx CR1 Configuration ----------------------*/
  410. /* Configure FMPI2Cx: Generalcall and NoStretch mode */
  411. hfmpi2c->Instance->CR1 = (hfmpi2c->Init.GeneralCallMode | hfmpi2c->Init.NoStretchMode);
  412. /* Enable the selected FMPI2C peripheral */
  413. __HAL_FMPI2C_ENABLE(hfmpi2c);
  414. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  415. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  416. hfmpi2c->PreviousState = FMPI2C_STATE_NONE;
  417. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  418. return HAL_OK;
  419. }
  420. /**
  421. * @brief DeInitialize the FMPI2C peripheral.
  422. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  423. * the configuration information for the specified FMPI2C.
  424. * @retval HAL status
  425. */
  426. HAL_StatusTypeDef HAL_FMPI2C_DeInit(FMPI2C_HandleTypeDef *hfmpi2c)
  427. {
  428. /* Check the FMPI2C handle allocation */
  429. if (hfmpi2c == NULL)
  430. {
  431. return HAL_ERROR;
  432. }
  433. /* Check the parameters */
  434. assert_param(IS_FMPI2C_ALL_INSTANCE(hfmpi2c->Instance));
  435. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY;
  436. /* Disable the FMPI2C Peripheral Clock */
  437. __HAL_FMPI2C_DISABLE(hfmpi2c);
  438. /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
  439. HAL_FMPI2C_MspDeInit(hfmpi2c);
  440. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  441. hfmpi2c->State = HAL_FMPI2C_STATE_RESET;
  442. hfmpi2c->PreviousState = FMPI2C_STATE_NONE;
  443. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  444. /* Release Lock */
  445. __HAL_UNLOCK(hfmpi2c);
  446. return HAL_OK;
  447. }
  448. /**
  449. * @brief Initialize the FMPI2C MSP.
  450. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  451. * the configuration information for the specified FMPI2C.
  452. * @retval None
  453. */
  454. __weak void HAL_FMPI2C_MspInit(FMPI2C_HandleTypeDef *hfmpi2c)
  455. {
  456. /* Prevent unused argument(s) compilation warning */
  457. UNUSED(hfmpi2c);
  458. /* NOTE : This function should not be modified, when the callback is needed,
  459. the HAL_FMPI2C_MspInit could be implemented in the user file
  460. */
  461. }
  462. /**
  463. * @brief DeInitialize the FMPI2C MSP.
  464. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  465. * the configuration information for the specified FMPI2C.
  466. * @retval None
  467. */
  468. __weak void HAL_FMPI2C_MspDeInit(FMPI2C_HandleTypeDef *hfmpi2c)
  469. {
  470. /* Prevent unused argument(s) compilation warning */
  471. UNUSED(hfmpi2c);
  472. /* NOTE : This function should not be modified, when the callback is needed,
  473. the HAL_FMPI2C_MspDeInit could be implemented in the user file
  474. */
  475. }
  476. /**
  477. * @}
  478. */
  479. /** @defgroup FMPI2C_Exported_Functions_Group2 Input and Output operation functions
  480. * @brief Data transfers functions
  481. *
  482. @verbatim
  483. ===============================================================================
  484. ##### IO operation functions #####
  485. ===============================================================================
  486. [..]
  487. This subsection provides a set of functions allowing to manage the FMPI2C data
  488. transfers.
  489. (#) There are two modes of transfer:
  490. (++) Blocking mode : The communication is performed in the polling mode.
  491. The status of all data processing is returned by the same function
  492. after finishing transfer.
  493. (++) No-Blocking mode : The communication is performed using Interrupts
  494. or DMA. These functions return the status of the transfer startup.
  495. The end of the data processing will be indicated through the
  496. dedicated FMPI2C IRQ when using Interrupt mode or the DMA IRQ when
  497. using DMA mode.
  498. (#) Blocking mode functions are :
  499. (++) HAL_FMPI2C_Master_Transmit()
  500. (++) HAL_FMPI2C_Master_Receive()
  501. (++) HAL_FMPI2C_Slave_Transmit()
  502. (++) HAL_FMPI2C_Slave_Receive()
  503. (++) HAL_FMPI2C_Mem_Write()
  504. (++) HAL_FMPI2C_Mem_Read()
  505. (++) HAL_FMPI2C_IsDeviceReady()
  506. (#) No-Blocking mode functions with Interrupt are :
  507. (++) HAL_FMPI2C_Master_Transmit_IT()
  508. (++) HAL_FMPI2C_Master_Receive_IT()
  509. (++) HAL_FMPI2C_Slave_Transmit_IT()
  510. (++) HAL_FMPI2C_Slave_Receive_IT()
  511. (++) HAL_FMPI2C_Master_Sequential_Transmit_IT()
  512. (++) HAL_FMPI2C_Master_Sequential_Receive_IT()
  513. (++) HAL_FMPI2C_Slave_Sequential_Transmit_IT()
  514. (++) HAL_FMPI2C_Slave_Sequential_Receive_IT()
  515. (++) HAL_FMPI2C_Mem_Write_IT()
  516. (++) HAL_FMPI2C_Mem_Read_IT()
  517. (#) No-Blocking mode functions with DMA are :
  518. (++) HAL_FMPI2C_Master_Transmit_DMA()
  519. (++) HAL_FMPI2C_Master_Receive_DMA()
  520. (++) HAL_FMPI2C_Slave_Transmit_DMA()
  521. (++) HAL_FMPI2C_Slave_Receive_DMA()
  522. (++) HAL_FMPI2C_Mem_Write_DMA()
  523. (++) HAL_FMPI2C_Mem_Read_DMA()
  524. (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
  525. (++) HAL_FMPI2C_MemTxCpltCallback()
  526. (++) HAL_FMPI2C_MemRxCpltCallback()
  527. (++) HAL_FMPI2C_MasterTxCpltCallback()
  528. (++) HAL_FMPI2C_MasterRxCpltCallback()
  529. (++) HAL_FMPI2C_SlaveTxCpltCallback()
  530. (++) HAL_FMPI2C_SlaveRxCpltCallback()
  531. (++) HAL_FMPI2C_ErrorCallback()
  532. @endverbatim
  533. * @{
  534. */
  535. /**
  536. * @brief Transmits in master mode an amount of data in blocking mode.
  537. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  538. * the configuration information for the specified FMPI2C.
  539. * @param DevAddress Target device address The device 7 bits address value
  540. * in datasheet must be shifted to the left before calling the interface
  541. * @param pData Pointer to data buffer
  542. * @param Size Amount of data to be sent
  543. * @param Timeout Timeout duration
  544. * @retval HAL status
  545. */
  546. HAL_StatusTypeDef HAL_FMPI2C_Master_Transmit(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  547. {
  548. uint32_t tickstart = 0U;
  549. if (hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  550. {
  551. /* Process Locked */
  552. __HAL_LOCK(hfmpi2c);
  553. /* Init tickstart for timeout management*/
  554. tickstart = HAL_GetTick();
  555. if (FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_BUSY, SET, FMPI2C_TIMEOUT_BUSY, tickstart) != HAL_OK)
  556. {
  557. return HAL_TIMEOUT;
  558. }
  559. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_TX;
  560. hfmpi2c->Mode = HAL_FMPI2C_MODE_MASTER;
  561. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  562. /* Prepare transfer parameters */
  563. hfmpi2c->pBuffPtr = pData;
  564. hfmpi2c->XferCount = Size;
  565. hfmpi2c->XferISR = NULL;
  566. /* Send Slave Address */
  567. /* Set NBYTES to write and reload if hfmpi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
  568. if (hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  569. {
  570. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  571. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, FMPI2C_RELOAD_MODE, FMPI2C_GENERATE_START_WRITE);
  572. }
  573. else
  574. {
  575. hfmpi2c->XferSize = hfmpi2c->XferCount;
  576. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, FMPI2C_AUTOEND_MODE, FMPI2C_GENERATE_START_WRITE);
  577. }
  578. while (hfmpi2c->XferCount > 0U)
  579. {
  580. /* Wait until TXIS flag is set */
  581. if (FMPI2C_WaitOnTXISFlagUntilTimeout(hfmpi2c, Timeout, tickstart) != HAL_OK)
  582. {
  583. if (hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  584. {
  585. return HAL_ERROR;
  586. }
  587. else
  588. {
  589. return HAL_TIMEOUT;
  590. }
  591. }
  592. /* Write data to TXDR */
  593. hfmpi2c->Instance->TXDR = (*hfmpi2c->pBuffPtr++);
  594. hfmpi2c->XferCount--;
  595. hfmpi2c->XferSize--;
  596. if ((hfmpi2c->XferSize == 0U) && (hfmpi2c->XferCount != 0U))
  597. {
  598. /* Wait until TCR flag is set */
  599. if (FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_TCR, RESET, Timeout, tickstart) != HAL_OK)
  600. {
  601. return HAL_TIMEOUT;
  602. }
  603. if (hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  604. {
  605. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  606. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, FMPI2C_RELOAD_MODE, FMPI2C_NO_STARTSTOP);
  607. }
  608. else
  609. {
  610. hfmpi2c->XferSize = hfmpi2c->XferCount;
  611. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, FMPI2C_AUTOEND_MODE, FMPI2C_NO_STARTSTOP);
  612. }
  613. }
  614. }
  615. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  616. /* Wait until STOPF flag is set */
  617. if (FMPI2C_WaitOnSTOPFlagUntilTimeout(hfmpi2c, Timeout, tickstart) != HAL_OK)
  618. {
  619. if (hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  620. {
  621. return HAL_ERROR;
  622. }
  623. else
  624. {
  625. return HAL_TIMEOUT;
  626. }
  627. }
  628. /* Clear STOP Flag */
  629. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  630. /* Clear Configuration Register 2 */
  631. FMPI2C_RESET_CR2(hfmpi2c);
  632. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  633. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  634. /* Process Unlocked */
  635. __HAL_UNLOCK(hfmpi2c);
  636. return HAL_OK;
  637. }
  638. else
  639. {
  640. return HAL_BUSY;
  641. }
  642. }
  643. /**
  644. * @brief Receives in master mode an amount of data in blocking mode.
  645. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  646. * the configuration information for the specified FMPI2C.
  647. * @param DevAddress Target device address The device 7 bits address value
  648. * in datasheet must be shifted to the left before calling the interface
  649. * @param pData Pointer to data buffer
  650. * @param Size Amount of data to be sent
  651. * @param Timeout Timeout duration
  652. * @retval HAL status
  653. */
  654. HAL_StatusTypeDef HAL_FMPI2C_Master_Receive(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  655. {
  656. uint32_t tickstart = 0U;
  657. if (hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  658. {
  659. /* Process Locked */
  660. __HAL_LOCK(hfmpi2c);
  661. /* Init tickstart for timeout management*/
  662. tickstart = HAL_GetTick();
  663. if (FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_BUSY, SET, FMPI2C_TIMEOUT_BUSY, tickstart) != HAL_OK)
  664. {
  665. return HAL_TIMEOUT;
  666. }
  667. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_RX;
  668. hfmpi2c->Mode = HAL_FMPI2C_MODE_MASTER;
  669. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  670. /* Prepare transfer parameters */
  671. hfmpi2c->pBuffPtr = pData;
  672. hfmpi2c->XferCount = Size;
  673. hfmpi2c->XferISR = NULL;
  674. /* Send Slave Address */
  675. /* Set NBYTES to write and reload if hfmpi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
  676. if (hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  677. {
  678. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  679. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, FMPI2C_RELOAD_MODE, FMPI2C_GENERATE_START_READ);
  680. }
  681. else
  682. {
  683. hfmpi2c->XferSize = hfmpi2c->XferCount;
  684. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, FMPI2C_AUTOEND_MODE, FMPI2C_GENERATE_START_READ);
  685. }
  686. while (hfmpi2c->XferCount > 0U)
  687. {
  688. /* Wait until RXNE flag is set */
  689. if (FMPI2C_WaitOnRXNEFlagUntilTimeout(hfmpi2c, Timeout, tickstart) != HAL_OK)
  690. {
  691. if (hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  692. {
  693. return HAL_ERROR;
  694. }
  695. else
  696. {
  697. return HAL_TIMEOUT;
  698. }
  699. }
  700. /* Read data from RXDR */
  701. (*hfmpi2c->pBuffPtr++) = hfmpi2c->Instance->RXDR;
  702. hfmpi2c->XferSize--;
  703. hfmpi2c->XferCount--;
  704. if ((hfmpi2c->XferSize == 0U) && (hfmpi2c->XferCount != 0U))
  705. {
  706. /* Wait until TCR flag is set */
  707. if (FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_TCR, RESET, Timeout, tickstart) != HAL_OK)
  708. {
  709. return HAL_TIMEOUT;
  710. }
  711. if (hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  712. {
  713. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  714. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, FMPI2C_RELOAD_MODE, FMPI2C_NO_STARTSTOP);
  715. }
  716. else
  717. {
  718. hfmpi2c->XferSize = hfmpi2c->XferCount;
  719. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, FMPI2C_AUTOEND_MODE, FMPI2C_NO_STARTSTOP);
  720. }
  721. }
  722. }
  723. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  724. /* Wait until STOPF flag is set */
  725. if (FMPI2C_WaitOnSTOPFlagUntilTimeout(hfmpi2c, Timeout, tickstart) != HAL_OK)
  726. {
  727. if (hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  728. {
  729. return HAL_ERROR;
  730. }
  731. else
  732. {
  733. return HAL_TIMEOUT;
  734. }
  735. }
  736. /* Clear STOP Flag */
  737. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  738. /* Clear Configuration Register 2 */
  739. FMPI2C_RESET_CR2(hfmpi2c);
  740. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  741. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  742. /* Process Unlocked */
  743. __HAL_UNLOCK(hfmpi2c);
  744. return HAL_OK;
  745. }
  746. else
  747. {
  748. return HAL_BUSY;
  749. }
  750. }
  751. /**
  752. * @brief Transmits in slave mode an amount of data in blocking mode.
  753. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  754. * the configuration information for the specified FMPI2C.
  755. * @param pData Pointer to data buffer
  756. * @param Size Amount of data to be sent
  757. * @param Timeout Timeout duration
  758. * @retval HAL status
  759. */
  760. HAL_StatusTypeDef HAL_FMPI2C_Slave_Transmit(FMPI2C_HandleTypeDef *hfmpi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  761. {
  762. uint32_t tickstart = 0U;
  763. if (hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  764. {
  765. if ((pData == NULL) || (Size == 0U))
  766. {
  767. return HAL_ERROR;
  768. }
  769. /* Process Locked */
  770. __HAL_LOCK(hfmpi2c);
  771. /* Init tickstart for timeout management*/
  772. tickstart = HAL_GetTick();
  773. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_TX;
  774. hfmpi2c->Mode = HAL_FMPI2C_MODE_SLAVE;
  775. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  776. /* Prepare transfer parameters */
  777. hfmpi2c->pBuffPtr = pData;
  778. hfmpi2c->XferCount = Size;
  779. hfmpi2c->XferISR = NULL;
  780. /* Enable Address Acknowledge */
  781. hfmpi2c->Instance->CR2 &= ~FMPI2C_CR2_NACK;
  782. /* Wait until ADDR flag is set */
  783. if (FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_ADDR, RESET, Timeout, tickstart) != HAL_OK)
  784. {
  785. /* Disable Address Acknowledge */
  786. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  787. return HAL_TIMEOUT;
  788. }
  789. /* Clear ADDR flag */
  790. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_ADDR);
  791. /* If 10bit addressing mode is selected */
  792. if (hfmpi2c->Init.AddressingMode == FMPI2C_ADDRESSINGMODE_10BIT)
  793. {
  794. /* Wait until ADDR flag is set */
  795. if (FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_ADDR, RESET, Timeout, tickstart) != HAL_OK)
  796. {
  797. /* Disable Address Acknowledge */
  798. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  799. return HAL_TIMEOUT;
  800. }
  801. /* Clear ADDR flag */
  802. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_ADDR);
  803. }
  804. /* Wait until DIR flag is set Transmitter mode */
  805. if (FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_DIR, RESET, Timeout, tickstart) != HAL_OK)
  806. {
  807. /* Disable Address Acknowledge */
  808. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  809. return HAL_TIMEOUT;
  810. }
  811. while (hfmpi2c->XferCount > 0U)
  812. {
  813. /* Wait until TXIS flag is set */
  814. if (FMPI2C_WaitOnTXISFlagUntilTimeout(hfmpi2c, Timeout, tickstart) != HAL_OK)
  815. {
  816. /* Disable Address Acknowledge */
  817. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  818. if (hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  819. {
  820. return HAL_ERROR;
  821. }
  822. else
  823. {
  824. return HAL_TIMEOUT;
  825. }
  826. }
  827. /* Write data to TXDR */
  828. hfmpi2c->Instance->TXDR = (*hfmpi2c->pBuffPtr++);
  829. hfmpi2c->XferCount--;
  830. }
  831. /* Wait until STOP flag is set */
  832. if (FMPI2C_WaitOnSTOPFlagUntilTimeout(hfmpi2c, Timeout, tickstart) != HAL_OK)
  833. {
  834. /* Disable Address Acknowledge */
  835. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  836. if (hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  837. {
  838. /* Normal use case for Transmitter mode */
  839. /* A NACK is generated to confirm the end of transfer */
  840. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  841. }
  842. else
  843. {
  844. return HAL_TIMEOUT;
  845. }
  846. }
  847. /* Clear STOP flag */
  848. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  849. /* Wait until BUSY flag is reset */
  850. if (FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_BUSY, SET, Timeout, tickstart) != HAL_OK)
  851. {
  852. /* Disable Address Acknowledge */
  853. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  854. return HAL_TIMEOUT;
  855. }
  856. /* Disable Address Acknowledge */
  857. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  858. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  859. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  860. /* Process Unlocked */
  861. __HAL_UNLOCK(hfmpi2c);
  862. return HAL_OK;
  863. }
  864. else
  865. {
  866. return HAL_BUSY;
  867. }
  868. }
  869. /**
  870. * @brief Receive in slave mode an amount of data in blocking mode
  871. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  872. * the configuration information for the specified FMPI2C.
  873. * @param pData Pointer to data buffer
  874. * @param Size Amount of data to be sent
  875. * @param Timeout Timeout duration
  876. * @retval HAL status
  877. */
  878. HAL_StatusTypeDef HAL_FMPI2C_Slave_Receive(FMPI2C_HandleTypeDef *hfmpi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  879. {
  880. uint32_t tickstart = 0U;
  881. if (hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  882. {
  883. if ((pData == NULL) || (Size == 0U))
  884. {
  885. return HAL_ERROR;
  886. }
  887. /* Process Locked */
  888. __HAL_LOCK(hfmpi2c);
  889. /* Init tickstart for timeout management*/
  890. tickstart = HAL_GetTick();
  891. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_RX;
  892. hfmpi2c->Mode = HAL_FMPI2C_MODE_SLAVE;
  893. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  894. /* Prepare transfer parameters */
  895. hfmpi2c->pBuffPtr = pData;
  896. hfmpi2c->XferCount = Size;
  897. hfmpi2c->XferISR = NULL;
  898. /* Enable Address Acknowledge */
  899. hfmpi2c->Instance->CR2 &= ~FMPI2C_CR2_NACK;
  900. /* Wait until ADDR flag is set */
  901. if (FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_ADDR, RESET, Timeout, tickstart) != HAL_OK)
  902. {
  903. /* Disable Address Acknowledge */
  904. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  905. return HAL_TIMEOUT;
  906. }
  907. /* Clear ADDR flag */
  908. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_ADDR);
  909. /* Wait until DIR flag is reset Receiver mode */
  910. if (FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_DIR, SET, Timeout, tickstart) != HAL_OK)
  911. {
  912. /* Disable Address Acknowledge */
  913. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  914. return HAL_TIMEOUT;
  915. }
  916. while (hfmpi2c->XferCount > 0U)
  917. {
  918. /* Wait until RXNE flag is set */
  919. if (FMPI2C_WaitOnRXNEFlagUntilTimeout(hfmpi2c, Timeout, tickstart) != HAL_OK)
  920. {
  921. /* Disable Address Acknowledge */
  922. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  923. /* Store Last receive data if any */
  924. if (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_RXNE) == SET)
  925. {
  926. /* Read data from RXDR */
  927. (*hfmpi2c->pBuffPtr++) = hfmpi2c->Instance->RXDR;
  928. hfmpi2c->XferCount--;
  929. }
  930. if (hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_TIMEOUT)
  931. {
  932. return HAL_TIMEOUT;
  933. }
  934. else
  935. {
  936. return HAL_ERROR;
  937. }
  938. }
  939. /* Read data from RXDR */
  940. (*hfmpi2c->pBuffPtr++) = hfmpi2c->Instance->RXDR;
  941. hfmpi2c->XferCount--;
  942. }
  943. /* Wait until STOP flag is set */
  944. if (FMPI2C_WaitOnSTOPFlagUntilTimeout(hfmpi2c, Timeout, tickstart) != HAL_OK)
  945. {
  946. /* Disable Address Acknowledge */
  947. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  948. if (hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  949. {
  950. return HAL_ERROR;
  951. }
  952. else
  953. {
  954. return HAL_TIMEOUT;
  955. }
  956. }
  957. /* Clear STOP flag */
  958. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  959. /* Wait until BUSY flag is reset */
  960. if (FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_BUSY, SET, Timeout, tickstart) != HAL_OK)
  961. {
  962. /* Disable Address Acknowledge */
  963. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  964. return HAL_TIMEOUT;
  965. }
  966. /* Disable Address Acknowledge */
  967. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  968. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  969. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  970. /* Process Unlocked */
  971. __HAL_UNLOCK(hfmpi2c);
  972. return HAL_OK;
  973. }
  974. else
  975. {
  976. return HAL_BUSY;
  977. }
  978. }
  979. /**
  980. * @brief Transmit in master mode an amount of data in non-blocking mode with Interrupt
  981. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  982. * the configuration information for the specified FMPI2C.
  983. * @param DevAddress Target device address The device 7 bits address value
  984. * in datasheet must be shifted to the left before calling the interface
  985. * @param pData Pointer to data buffer
  986. * @param Size Amount of data to be sent
  987. * @retval HAL status
  988. */
  989. HAL_StatusTypeDef HAL_FMPI2C_Master_Transmit_IT(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
  990. {
  991. uint32_t xfermode = 0U;
  992. if (hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  993. {
  994. if (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_BUSY) == SET)
  995. {
  996. return HAL_BUSY;
  997. }
  998. /* Process Locked */
  999. __HAL_LOCK(hfmpi2c);
  1000. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_TX;
  1001. hfmpi2c->Mode = HAL_FMPI2C_MODE_MASTER;
  1002. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1003. /* Prepare transfer parameters */
  1004. hfmpi2c->pBuffPtr = pData;
  1005. hfmpi2c->XferCount = Size;
  1006. hfmpi2c->XferOptions = FMPI2C_NO_OPTION_FRAME;
  1007. hfmpi2c->XferISR = FMPI2C_Master_ISR_IT;
  1008. if (hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  1009. {
  1010. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  1011. xfermode = FMPI2C_RELOAD_MODE;
  1012. }
  1013. else
  1014. {
  1015. hfmpi2c->XferSize = hfmpi2c->XferCount;
  1016. xfermode = FMPI2C_AUTOEND_MODE;
  1017. }
  1018. /* Send Slave Address */
  1019. /* Set NBYTES to write and reload if hfmpi2c->XferCount > MAX_NBYTE_SIZE */
  1020. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, xfermode, FMPI2C_GENERATE_START_WRITE);
  1021. /* Process Unlocked */
  1022. __HAL_UNLOCK(hfmpi2c);
  1023. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  1024. to avoid the risk of FMPI2C interrupt handle execution before current
  1025. process unlock */
  1026. /* Enable ERR, TC, STOP, NACK, TXI interrupt */
  1027. /* possible to enable all of these */
  1028. /* FMPI2C_IT_ERRI | FMPI2C_IT_TCI| FMPI2C_IT_STOPI| FMPI2C_IT_NACKI | FMPI2C_IT_ADDRI | FMPI2C_IT_RXI | FMPI2C_IT_TXI */
  1029. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_TX_IT);
  1030. return HAL_OK;
  1031. }
  1032. else
  1033. {
  1034. return HAL_BUSY;
  1035. }
  1036. }
  1037. /**
  1038. * @brief Receive in master mode an amount of data in non-blocking mode with Interrupt
  1039. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  1040. * the configuration information for the specified FMPI2C.
  1041. * @param DevAddress Target device address The device 7 bits address value
  1042. * in datasheet must be shifted to the left before calling the interface
  1043. * @param pData Pointer to data buffer
  1044. * @param Size Amount of data to be sent
  1045. * @retval HAL status
  1046. */
  1047. HAL_StatusTypeDef HAL_FMPI2C_Master_Receive_IT(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
  1048. {
  1049. uint32_t xfermode = 0U;
  1050. if (hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1051. {
  1052. if (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_BUSY) == SET)
  1053. {
  1054. return HAL_BUSY;
  1055. }
  1056. /* Process Locked */
  1057. __HAL_LOCK(hfmpi2c);
  1058. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_RX;
  1059. hfmpi2c->Mode = HAL_FMPI2C_MODE_MASTER;
  1060. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1061. /* Prepare transfer parameters */
  1062. hfmpi2c->pBuffPtr = pData;
  1063. hfmpi2c->XferCount = Size;
  1064. hfmpi2c->XferOptions = FMPI2C_NO_OPTION_FRAME;
  1065. hfmpi2c->XferISR = FMPI2C_Master_ISR_IT;
  1066. if (hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  1067. {
  1068. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  1069. xfermode = FMPI2C_RELOAD_MODE;
  1070. }
  1071. else
  1072. {
  1073. hfmpi2c->XferSize = hfmpi2c->XferCount;
  1074. xfermode = FMPI2C_AUTOEND_MODE;
  1075. }
  1076. /* Send Slave Address */
  1077. /* Set NBYTES to write and reload if hfmpi2c->XferCount > MAX_NBYTE_SIZE */
  1078. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, xfermode, FMPI2C_GENERATE_START_READ);
  1079. /* Process Unlocked */
  1080. __HAL_UNLOCK(hfmpi2c);
  1081. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  1082. to avoid the risk of FMPI2C interrupt handle execution before current
  1083. process unlock */
  1084. /* Enable ERR, TC, STOP, NACK, RXI interrupt */
  1085. /* possible to enable all of these */
  1086. /* FMPI2C_IT_ERRI | FMPI2C_IT_TCI| FMPI2C_IT_STOPI| FMPI2C_IT_NACKI | FMPI2C_IT_ADDRI | FMPI2C_IT_RXI | FMPI2C_IT_TXI */
  1087. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_RX_IT);
  1088. return HAL_OK;
  1089. }
  1090. else
  1091. {
  1092. return HAL_BUSY;
  1093. }
  1094. }
  1095. /**
  1096. * @brief Transmit in slave mode an amount of data in non-blocking mode with Interrupt
  1097. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  1098. * the configuration information for the specified FMPI2C.
  1099. * @param pData Pointer to data buffer
  1100. * @param Size Amount of data to be sent
  1101. * @retval HAL status
  1102. */
  1103. HAL_StatusTypeDef HAL_FMPI2C_Slave_Transmit_IT(FMPI2C_HandleTypeDef *hfmpi2c, uint8_t *pData, uint16_t Size)
  1104. {
  1105. if (hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1106. {
  1107. /* Process Locked */
  1108. __HAL_LOCK(hfmpi2c);
  1109. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_TX;
  1110. hfmpi2c->Mode = HAL_FMPI2C_MODE_SLAVE;
  1111. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1112. /* Enable Address Acknowledge */
  1113. hfmpi2c->Instance->CR2 &= ~FMPI2C_CR2_NACK;
  1114. /* Prepare transfer parameters */
  1115. hfmpi2c->pBuffPtr = pData;
  1116. hfmpi2c->XferCount = Size;
  1117. hfmpi2c->XferSize = hfmpi2c->XferCount;
  1118. hfmpi2c->XferOptions = FMPI2C_NO_OPTION_FRAME;
  1119. hfmpi2c->XferISR = FMPI2C_Slave_ISR_IT;
  1120. /* Process Unlocked */
  1121. __HAL_UNLOCK(hfmpi2c);
  1122. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  1123. to avoid the risk of FMPI2C interrupt handle execution before current
  1124. process unlock */
  1125. /* Enable ERR, TC, STOP, NACK, TXI interrupt */
  1126. /* possible to enable all of these */
  1127. /* FMPI2C_IT_ERRI | FMPI2C_IT_TCI| FMPI2C_IT_STOPI| FMPI2C_IT_NACKI | FMPI2C_IT_ADDRI | FMPI2C_IT_RXI | FMPI2C_IT_TXI */
  1128. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_TX_IT | FMPI2C_XFER_LISTEN_IT);
  1129. return HAL_OK;
  1130. }
  1131. else
  1132. {
  1133. return HAL_BUSY;
  1134. }
  1135. }
  1136. /**
  1137. * @brief Receive in slave mode an amount of data in non-blocking mode with Interrupt
  1138. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  1139. * the configuration information for the specified FMPI2C.
  1140. * @param pData Pointer to data buffer
  1141. * @param Size Amount of data to be sent
  1142. * @retval HAL status
  1143. */
  1144. HAL_StatusTypeDef HAL_FMPI2C_Slave_Receive_IT(FMPI2C_HandleTypeDef *hfmpi2c, uint8_t *pData, uint16_t Size)
  1145. {
  1146. if (hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1147. {
  1148. /* Process Locked */
  1149. __HAL_LOCK(hfmpi2c);
  1150. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_RX;
  1151. hfmpi2c->Mode = HAL_FMPI2C_MODE_SLAVE;
  1152. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1153. /* Enable Address Acknowledge */
  1154. hfmpi2c->Instance->CR2 &= ~FMPI2C_CR2_NACK;
  1155. /* Prepare transfer parameters */
  1156. hfmpi2c->pBuffPtr = pData;
  1157. hfmpi2c->XferCount = Size;
  1158. hfmpi2c->XferSize = hfmpi2c->XferCount;
  1159. hfmpi2c->XferOptions = FMPI2C_NO_OPTION_FRAME;
  1160. hfmpi2c->XferISR = FMPI2C_Slave_ISR_IT;
  1161. /* Process Unlocked */
  1162. __HAL_UNLOCK(hfmpi2c);
  1163. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  1164. to avoid the risk of FMPI2C interrupt handle execution before current
  1165. process unlock */
  1166. /* Enable ERR, TC, STOP, NACK, RXI interrupt */
  1167. /* possible to enable all of these */
  1168. /* FMPI2C_IT_ERRI | FMPI2C_IT_TCI| FMPI2C_IT_STOPI| FMPI2C_IT_NACKI | FMPI2C_IT_ADDRI | FMPI2C_IT_RXI | FMPI2C_IT_TXI */
  1169. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_RX_IT | FMPI2C_XFER_LISTEN_IT);
  1170. return HAL_OK;
  1171. }
  1172. else
  1173. {
  1174. return HAL_BUSY;
  1175. }
  1176. }
  1177. /**
  1178. * @brief Transmit in master mode an amount of data in non-blocking mode with DMA
  1179. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  1180. * the configuration information for the specified FMPI2C.
  1181. * @param DevAddress Target device address The device 7 bits address value
  1182. * in datasheet must be shifted to the left before calling the interface
  1183. * @param pData Pointer to data buffer
  1184. * @param Size Amount of data to be sent
  1185. * @retval HAL status
  1186. */
  1187. HAL_StatusTypeDef HAL_FMPI2C_Master_Transmit_DMA(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
  1188. {
  1189. uint32_t xfermode = 0U;
  1190. if (hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1191. {
  1192. if (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_BUSY) == SET)
  1193. {
  1194. return HAL_BUSY;
  1195. }
  1196. /* Process Locked */
  1197. __HAL_LOCK(hfmpi2c);
  1198. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_TX;
  1199. hfmpi2c->Mode = HAL_FMPI2C_MODE_MASTER;
  1200. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1201. /* Prepare transfer parameters */
  1202. hfmpi2c->pBuffPtr = pData;
  1203. hfmpi2c->XferCount = Size;
  1204. hfmpi2c->XferOptions = FMPI2C_NO_OPTION_FRAME;
  1205. hfmpi2c->XferISR = FMPI2C_Master_ISR_DMA;
  1206. if (hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  1207. {
  1208. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  1209. xfermode = FMPI2C_RELOAD_MODE;
  1210. }
  1211. else
  1212. {
  1213. hfmpi2c->XferSize = hfmpi2c->XferCount;
  1214. xfermode = FMPI2C_AUTOEND_MODE;
  1215. }
  1216. if (hfmpi2c->XferSize > 0U)
  1217. {
  1218. /* Set the FMPI2C DMA transfer complete callback */
  1219. hfmpi2c->hdmatx->XferCpltCallback = FMPI2C_DMAMasterTransmitCplt;
  1220. /* Set the DMA error callback */
  1221. hfmpi2c->hdmatx->XferErrorCallback = FMPI2C_DMAError;
  1222. /* Set the unused DMA callbacks to NULL */
  1223. hfmpi2c->hdmatx->XferHalfCpltCallback = NULL;
  1224. hfmpi2c->hdmatx->XferAbortCallback = NULL;
  1225. /* Enable the DMA stream */
  1226. HAL_DMA_Start_IT(hfmpi2c->hdmatx, (uint32_t)pData, (uint32_t)&hfmpi2c->Instance->TXDR, hfmpi2c->XferSize);
  1227. /* Send Slave Address */
  1228. /* Set NBYTES to write and reload if hfmpi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
  1229. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, xfermode, FMPI2C_GENERATE_START_WRITE);
  1230. /* Update XferCount value */
  1231. hfmpi2c->XferCount -= hfmpi2c->XferSize;
  1232. /* Process Unlocked */
  1233. __HAL_UNLOCK(hfmpi2c);
  1234. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  1235. to avoid the risk of FMPI2C interrupt handle execution before current
  1236. process unlock */
  1237. /* Enable ERR and NACK interrupts */
  1238. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_ERROR_IT);
  1239. /* Enable DMA Request */
  1240. hfmpi2c->Instance->CR1 |= FMPI2C_CR1_TXDMAEN;
  1241. }
  1242. else
  1243. {
  1244. /* Update Transfer ISR function pointer */
  1245. hfmpi2c->XferISR = FMPI2C_Master_ISR_IT;
  1246. /* Send Slave Address */
  1247. /* Set NBYTES to write and generate START condition */
  1248. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, FMPI2C_AUTOEND_MODE, FMPI2C_GENERATE_START_WRITE);
  1249. /* Process Unlocked */
  1250. __HAL_UNLOCK(hfmpi2c);
  1251. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  1252. to avoid the risk of FMPI2C interrupt handle execution before current
  1253. process unlock */
  1254. /* Enable ERR, TC, STOP, NACK, TXI interrupt */
  1255. /* possible to enable all of these */
  1256. /* FMPI2C_IT_ERRI | FMPI2C_IT_TCI| FMPI2C_IT_STOPI| FMPI2C_IT_NACKI | FMPI2C_IT_ADDRI | FMPI2C_IT_RXI | FMPI2C_IT_TXI */
  1257. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_TX_IT);
  1258. }
  1259. return HAL_OK;
  1260. }
  1261. else
  1262. {
  1263. return HAL_BUSY;
  1264. }
  1265. }
  1266. /**
  1267. * @brief Receive in master mode an amount of data in non-blocking mode with DMA
  1268. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  1269. * the configuration information for the specified FMPI2C.
  1270. * @param DevAddress Target device address The device 7 bits address value
  1271. * in datasheet must be shifted to the left before calling the interface
  1272. * @param pData Pointer to data buffer
  1273. * @param Size Amount of data to be sent
  1274. * @retval HAL status
  1275. */
  1276. HAL_StatusTypeDef HAL_FMPI2C_Master_Receive_DMA(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
  1277. {
  1278. uint32_t xfermode = 0U;
  1279. if (hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1280. {
  1281. if (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_BUSY) == SET)
  1282. {
  1283. return HAL_BUSY;
  1284. }
  1285. /* Process Locked */
  1286. __HAL_LOCK(hfmpi2c);
  1287. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_RX;
  1288. hfmpi2c->Mode = HAL_FMPI2C_MODE_MASTER;
  1289. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1290. /* Prepare transfer parameters */
  1291. hfmpi2c->pBuffPtr = pData;
  1292. hfmpi2c->XferCount = Size;
  1293. hfmpi2c->XferOptions = FMPI2C_NO_OPTION_FRAME;
  1294. hfmpi2c->XferISR = FMPI2C_Master_ISR_DMA;
  1295. if (hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  1296. {
  1297. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  1298. xfermode = FMPI2C_RELOAD_MODE;
  1299. }
  1300. else
  1301. {
  1302. hfmpi2c->XferSize = hfmpi2c->XferCount;
  1303. xfermode = FMPI2C_AUTOEND_MODE;
  1304. }
  1305. if (hfmpi2c->XferSize > 0U)
  1306. {
  1307. /* Set the FMPI2C DMA transfer complete callback */
  1308. hfmpi2c->hdmarx->XferCpltCallback = FMPI2C_DMAMasterReceiveCplt;
  1309. /* Set the DMA error callback */
  1310. hfmpi2c->hdmarx->XferErrorCallback = FMPI2C_DMAError;
  1311. /* Set the unused DMA callbacks to NULL */
  1312. hfmpi2c->hdmarx->XferHalfCpltCallback = NULL;
  1313. hfmpi2c->hdmarx->XferAbortCallback = NULL;
  1314. /* Enable the DMA stream */
  1315. HAL_DMA_Start_IT(hfmpi2c->hdmarx, (uint32_t)&hfmpi2c->Instance->RXDR, (uint32_t)pData, hfmpi2c->XferSize);
  1316. /* Send Slave Address */
  1317. /* Set NBYTES to read and reload if hfmpi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
  1318. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, xfermode, FMPI2C_GENERATE_START_READ);
  1319. /* Update XferCount value */
  1320. hfmpi2c->XferCount -= hfmpi2c->XferSize;
  1321. /* Process Unlocked */
  1322. __HAL_UNLOCK(hfmpi2c);
  1323. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  1324. to avoid the risk of FMPI2C interrupt handle execution before current
  1325. process unlock */
  1326. /* Enable ERR and NACK interrupts */
  1327. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_ERROR_IT);
  1328. /* Enable DMA Request */
  1329. hfmpi2c->Instance->CR1 |= FMPI2C_CR1_RXDMAEN;
  1330. }
  1331. else
  1332. {
  1333. /* Update Transfer ISR function pointer */
  1334. hfmpi2c->XferISR = FMPI2C_Master_ISR_IT;
  1335. /* Send Slave Address */
  1336. /* Set NBYTES to read and generate START condition */
  1337. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, FMPI2C_AUTOEND_MODE, FMPI2C_GENERATE_START_READ);
  1338. /* Process Unlocked */
  1339. __HAL_UNLOCK(hfmpi2c);
  1340. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  1341. to avoid the risk of FMPI2C interrupt handle execution before current
  1342. process unlock */
  1343. /* Enable ERR, TC, STOP, NACK, TXI interrupt */
  1344. /* possible to enable all of these */
  1345. /* FMPI2C_IT_ERRI | FMPI2C_IT_TCI| FMPI2C_IT_STOPI| FMPI2C_IT_NACKI | FMPI2C_IT_ADDRI | FMPI2C_IT_RXI | FMPI2C_IT_TXI */
  1346. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_TX_IT);
  1347. }
  1348. return HAL_OK;
  1349. }
  1350. else
  1351. {
  1352. return HAL_BUSY;
  1353. }
  1354. }
  1355. /**
  1356. * @brief Transmit in slave mode an amount of data in non-blocking mode with DMA
  1357. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  1358. * the configuration information for the specified FMPI2C.
  1359. * @param pData Pointer to data buffer
  1360. * @param Size Amount of data to be sent
  1361. * @retval HAL status
  1362. */
  1363. HAL_StatusTypeDef HAL_FMPI2C_Slave_Transmit_DMA(FMPI2C_HandleTypeDef *hfmpi2c, uint8_t *pData, uint16_t Size)
  1364. {
  1365. if (hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1366. {
  1367. if ((pData == NULL) || (Size == 0U))
  1368. {
  1369. return HAL_ERROR;
  1370. }
  1371. /* Process Locked */
  1372. __HAL_LOCK(hfmpi2c);
  1373. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_TX;
  1374. hfmpi2c->Mode = HAL_FMPI2C_MODE_SLAVE;
  1375. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1376. /* Prepare transfer parameters */
  1377. hfmpi2c->pBuffPtr = pData;
  1378. hfmpi2c->XferCount = Size;
  1379. hfmpi2c->XferSize = hfmpi2c->XferCount;
  1380. hfmpi2c->XferOptions = FMPI2C_NO_OPTION_FRAME;
  1381. hfmpi2c->XferISR = FMPI2C_Slave_ISR_DMA;
  1382. /* Set the FMPI2C DMA transfer complete callback */
  1383. hfmpi2c->hdmatx->XferCpltCallback = FMPI2C_DMASlaveTransmitCplt;
  1384. /* Set the DMA error callback */
  1385. hfmpi2c->hdmatx->XferErrorCallback = FMPI2C_DMAError;
  1386. /* Set the unused DMA callbacks to NULL */
  1387. hfmpi2c->hdmatx->XferHalfCpltCallback = NULL;
  1388. hfmpi2c->hdmatx->XferAbortCallback = NULL;
  1389. /* Enable the DMA stream */
  1390. HAL_DMA_Start_IT(hfmpi2c->hdmatx, (uint32_t)pData, (uint32_t)&hfmpi2c->Instance->TXDR, hfmpi2c->XferSize);
  1391. /* Enable Address Acknowledge */
  1392. hfmpi2c->Instance->CR2 &= ~FMPI2C_CR2_NACK;
  1393. /* Process Unlocked */
  1394. __HAL_UNLOCK(hfmpi2c);
  1395. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  1396. to avoid the risk of FMPI2C interrupt handle execution before current
  1397. process unlock */
  1398. /* Enable ERR, STOP, NACK, ADDR interrupts */
  1399. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_LISTEN_IT);
  1400. /* Enable DMA Request */
  1401. hfmpi2c->Instance->CR1 |= FMPI2C_CR1_TXDMAEN;
  1402. return HAL_OK;
  1403. }
  1404. else
  1405. {
  1406. return HAL_BUSY;
  1407. }
  1408. }
  1409. /**
  1410. * @brief Receive in slave mode an amount of data in non-blocking mode with DMA
  1411. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  1412. * the configuration information for the specified FMPI2C.
  1413. * @param pData Pointer to data buffer
  1414. * @param Size Amount of data to be sent
  1415. * @retval HAL status
  1416. */
  1417. HAL_StatusTypeDef HAL_FMPI2C_Slave_Receive_DMA(FMPI2C_HandleTypeDef *hfmpi2c, uint8_t *pData, uint16_t Size)
  1418. {
  1419. if (hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1420. {
  1421. if ((pData == NULL) || (Size == 0U))
  1422. {
  1423. return HAL_ERROR;
  1424. }
  1425. /* Process Locked */
  1426. __HAL_LOCK(hfmpi2c);
  1427. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_RX;
  1428. hfmpi2c->Mode = HAL_FMPI2C_MODE_SLAVE;
  1429. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1430. /* Prepare transfer parameters */
  1431. hfmpi2c->pBuffPtr = pData;
  1432. hfmpi2c->XferCount = Size;
  1433. hfmpi2c->XferSize = hfmpi2c->XferCount;
  1434. hfmpi2c->XferOptions = FMPI2C_NO_OPTION_FRAME;
  1435. hfmpi2c->XferISR = FMPI2C_Slave_ISR_DMA;
  1436. /* Set the FMPI2C DMA transfer complete callback */
  1437. hfmpi2c->hdmarx->XferCpltCallback = FMPI2C_DMASlaveReceiveCplt;
  1438. /* Set the DMA error callback */
  1439. hfmpi2c->hdmarx->XferErrorCallback = FMPI2C_DMAError;
  1440. /* Set the unused DMA callbacks to NULL */
  1441. hfmpi2c->hdmarx->XferHalfCpltCallback = NULL;
  1442. hfmpi2c->hdmarx->XferAbortCallback = NULL;
  1443. /* Enable the DMA stream */
  1444. HAL_DMA_Start_IT(hfmpi2c->hdmarx, (uint32_t)&hfmpi2c->Instance->RXDR, (uint32_t)pData, hfmpi2c->XferSize);
  1445. /* Enable Address Acknowledge */
  1446. hfmpi2c->Instance->CR2 &= ~FMPI2C_CR2_NACK;
  1447. /* Process Unlocked */
  1448. __HAL_UNLOCK(hfmpi2c);
  1449. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  1450. to avoid the risk of FMPI2C interrupt handle execution before current
  1451. process unlock */
  1452. /* Enable ERR, STOP, NACK, ADDR interrupts */
  1453. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_LISTEN_IT);
  1454. /* Enable DMA Request */
  1455. hfmpi2c->Instance->CR1 |= FMPI2C_CR1_RXDMAEN;
  1456. return HAL_OK;
  1457. }
  1458. else
  1459. {
  1460. return HAL_BUSY;
  1461. }
  1462. }
  1463. /**
  1464. * @brief Write an amount of data in blocking mode to a specific memory address
  1465. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  1466. * the configuration information for the specified FMPI2C.
  1467. * @param DevAddress Target device address The device 7 bits address value
  1468. * in datasheet must be shifted to the left before calling the interface
  1469. * @param MemAddress Internal memory address
  1470. * @param MemAddSize Size of internal memory address
  1471. * @param pData Pointer to data buffer
  1472. * @param Size Amount of data to be sent
  1473. * @param Timeout Timeout duration
  1474. * @retval HAL status
  1475. */
  1476. HAL_StatusTypeDef HAL_FMPI2C_Mem_Write(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  1477. {
  1478. uint32_t tickstart = 0U;
  1479. /* Check the parameters */
  1480. assert_param(IS_FMPI2C_MEMADD_SIZE(MemAddSize));
  1481. if (hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1482. {
  1483. if ((pData == NULL) || (Size == 0U))
  1484. {
  1485. return HAL_ERROR;
  1486. }
  1487. /* Process Locked */
  1488. __HAL_LOCK(hfmpi2c);
  1489. /* Init tickstart for timeout management*/
  1490. tickstart = HAL_GetTick();
  1491. if (FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_BUSY, SET, FMPI2C_TIMEOUT_BUSY, tickstart) != HAL_OK)
  1492. {
  1493. return HAL_TIMEOUT;
  1494. }
  1495. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_TX;
  1496. hfmpi2c->Mode = HAL_FMPI2C_MODE_MEM;
  1497. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1498. /* Prepare transfer parameters */
  1499. hfmpi2c->pBuffPtr = pData;
  1500. hfmpi2c->XferCount = Size;
  1501. hfmpi2c->XferISR = NULL;
  1502. /* Send Slave Address and Memory Address */
  1503. if (FMPI2C_RequestMemoryWrite(hfmpi2c, DevAddress, MemAddress, MemAddSize, Timeout, tickstart) != HAL_OK)
  1504. {
  1505. if (hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  1506. {
  1507. /* Process Unlocked */
  1508. __HAL_UNLOCK(hfmpi2c);
  1509. return HAL_ERROR;
  1510. }
  1511. else
  1512. {
  1513. /* Process Unlocked */
  1514. __HAL_UNLOCK(hfmpi2c);
  1515. return HAL_TIMEOUT;
  1516. }
  1517. }
  1518. /* Set NBYTES to write and reload if hfmpi2c->XferCount > MAX_NBYTE_SIZE */
  1519. if (hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  1520. {
  1521. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  1522. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, FMPI2C_RELOAD_MODE, FMPI2C_NO_STARTSTOP);
  1523. }
  1524. else
  1525. {
  1526. hfmpi2c->XferSize = hfmpi2c->XferCount;
  1527. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, FMPI2C_AUTOEND_MODE, FMPI2C_NO_STARTSTOP);
  1528. }
  1529. do
  1530. {
  1531. /* Wait until TXIS flag is set */
  1532. if (FMPI2C_WaitOnTXISFlagUntilTimeout(hfmpi2c, Timeout, tickstart) != HAL_OK)
  1533. {
  1534. if (hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  1535. {
  1536. return HAL_ERROR;
  1537. }
  1538. else
  1539. {
  1540. return HAL_TIMEOUT;
  1541. }
  1542. }
  1543. /* Write data to TXDR */
  1544. hfmpi2c->Instance->TXDR = (*hfmpi2c->pBuffPtr++);
  1545. hfmpi2c->XferCount--;
  1546. hfmpi2c->XferSize--;
  1547. if ((hfmpi2c->XferSize == 0U) && (hfmpi2c->XferCount != 0U))
  1548. {
  1549. /* Wait until TCR flag is set */
  1550. if (FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_TCR, RESET, Timeout, tickstart) != HAL_OK)
  1551. {
  1552. return HAL_TIMEOUT;
  1553. }
  1554. if (hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  1555. {
  1556. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  1557. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, FMPI2C_RELOAD_MODE, FMPI2C_NO_STARTSTOP);
  1558. }
  1559. else
  1560. {
  1561. hfmpi2c->XferSize = hfmpi2c->XferCount;
  1562. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, FMPI2C_AUTOEND_MODE, FMPI2C_NO_STARTSTOP);
  1563. }
  1564. }
  1565. }
  1566. while (hfmpi2c->XferCount > 0U);
  1567. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  1568. /* Wait until STOPF flag is reset */
  1569. if (FMPI2C_WaitOnSTOPFlagUntilTimeout(hfmpi2c, Timeout, tickstart) != HAL_OK)
  1570. {
  1571. if (hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  1572. {
  1573. return HAL_ERROR;
  1574. }
  1575. else
  1576. {
  1577. return HAL_TIMEOUT;
  1578. }
  1579. }
  1580. /* Clear STOP Flag */
  1581. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  1582. /* Clear Configuration Register 2 */
  1583. FMPI2C_RESET_CR2(hfmpi2c);
  1584. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  1585. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  1586. /* Process Unlocked */
  1587. __HAL_UNLOCK(hfmpi2c);
  1588. return HAL_OK;
  1589. }
  1590. else
  1591. {
  1592. return HAL_BUSY;
  1593. }
  1594. }
  1595. /**
  1596. * @brief Read an amount of data in blocking mode from a specific memory address
  1597. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  1598. * the configuration information for the specified FMPI2C.
  1599. * @param DevAddress Target device address The device 7 bits address value
  1600. * in datasheet must be shifted to the left before calling the interface
  1601. * @param MemAddress Internal memory address
  1602. * @param MemAddSize Size of internal memory address
  1603. * @param pData Pointer to data buffer
  1604. * @param Size Amount of data to be sent
  1605. * @param Timeout Timeout duration
  1606. * @retval HAL status
  1607. */
  1608. HAL_StatusTypeDef HAL_FMPI2C_Mem_Read(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  1609. {
  1610. uint32_t tickstart = 0U;
  1611. /* Check the parameters */
  1612. assert_param(IS_FMPI2C_MEMADD_SIZE(MemAddSize));
  1613. if (hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1614. {
  1615. if ((pData == NULL) || (Size == 0U))
  1616. {
  1617. return HAL_ERROR;
  1618. }
  1619. /* Process Locked */
  1620. __HAL_LOCK(hfmpi2c);
  1621. /* Init tickstart for timeout management*/
  1622. tickstart = HAL_GetTick();
  1623. if (FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_BUSY, SET, FMPI2C_TIMEOUT_BUSY, tickstart) != HAL_OK)
  1624. {
  1625. return HAL_TIMEOUT;
  1626. }
  1627. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_RX;
  1628. hfmpi2c->Mode = HAL_FMPI2C_MODE_MEM;
  1629. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1630. /* Prepare transfer parameters */
  1631. hfmpi2c->pBuffPtr = pData;
  1632. hfmpi2c->XferCount = Size;
  1633. hfmpi2c->XferISR = NULL;
  1634. /* Send Slave Address and Memory Address */
  1635. if (FMPI2C_RequestMemoryRead(hfmpi2c, DevAddress, MemAddress, MemAddSize, Timeout, tickstart) != HAL_OK)
  1636. {
  1637. if (hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  1638. {
  1639. /* Process Unlocked */
  1640. __HAL_UNLOCK(hfmpi2c);
  1641. return HAL_ERROR;
  1642. }
  1643. else
  1644. {
  1645. /* Process Unlocked */
  1646. __HAL_UNLOCK(hfmpi2c);
  1647. return HAL_TIMEOUT;
  1648. }
  1649. }
  1650. /* Send Slave Address */
  1651. /* Set NBYTES to write and reload if hfmpi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
  1652. if (hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  1653. {
  1654. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  1655. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, FMPI2C_RELOAD_MODE, FMPI2C_GENERATE_START_READ);
  1656. }
  1657. else
  1658. {
  1659. hfmpi2c->XferSize = hfmpi2c->XferCount;
  1660. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, FMPI2C_AUTOEND_MODE, FMPI2C_GENERATE_START_READ);
  1661. }
  1662. do
  1663. {
  1664. /* Wait until RXNE flag is set */
  1665. if (FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_RXNE, RESET, Timeout, tickstart) != HAL_OK)
  1666. {
  1667. return HAL_TIMEOUT;
  1668. }
  1669. /* Read data from RXDR */
  1670. (*hfmpi2c->pBuffPtr++) = hfmpi2c->Instance->RXDR;
  1671. hfmpi2c->XferSize--;
  1672. hfmpi2c->XferCount--;
  1673. if ((hfmpi2c->XferSize == 0U) && (hfmpi2c->XferCount != 0U))
  1674. {
  1675. /* Wait until TCR flag is set */
  1676. if (FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_TCR, RESET, Timeout, tickstart) != HAL_OK)
  1677. {
  1678. return HAL_TIMEOUT;
  1679. }
  1680. if (hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  1681. {
  1682. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  1683. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, FMPI2C_RELOAD_MODE, FMPI2C_NO_STARTSTOP);
  1684. }
  1685. else
  1686. {
  1687. hfmpi2c->XferSize = hfmpi2c->XferCount;
  1688. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, FMPI2C_AUTOEND_MODE, FMPI2C_NO_STARTSTOP);
  1689. }
  1690. }
  1691. }
  1692. while (hfmpi2c->XferCount > 0U);
  1693. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  1694. /* Wait until STOPF flag is reset */
  1695. if (FMPI2C_WaitOnSTOPFlagUntilTimeout(hfmpi2c, Timeout, tickstart) != HAL_OK)
  1696. {
  1697. if (hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  1698. {
  1699. return HAL_ERROR;
  1700. }
  1701. else
  1702. {
  1703. return HAL_TIMEOUT;
  1704. }
  1705. }
  1706. /* Clear STOP Flag */
  1707. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  1708. /* Clear Configuration Register 2 */
  1709. FMPI2C_RESET_CR2(hfmpi2c);
  1710. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  1711. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  1712. /* Process Unlocked */
  1713. __HAL_UNLOCK(hfmpi2c);
  1714. return HAL_OK;
  1715. }
  1716. else
  1717. {
  1718. return HAL_BUSY;
  1719. }
  1720. }
  1721. /**
  1722. * @brief Write an amount of data in non-blocking mode with Interrupt to a specific memory address
  1723. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  1724. * the configuration information for the specified FMPI2C.
  1725. * @param DevAddress Target device address The device 7 bits address value
  1726. * in datasheet must be shifted to the left before calling the interface
  1727. * @param MemAddress Internal memory address
  1728. * @param MemAddSize Size of internal memory address
  1729. * @param pData Pointer to data buffer
  1730. * @param Size Amount of data to be sent
  1731. * @retval HAL status
  1732. */
  1733. HAL_StatusTypeDef HAL_FMPI2C_Mem_Write_IT(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
  1734. {
  1735. uint32_t tickstart = 0U;
  1736. uint32_t xfermode = 0U;
  1737. /* Check the parameters */
  1738. assert_param(IS_FMPI2C_MEMADD_SIZE(MemAddSize));
  1739. if (hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1740. {
  1741. if ((pData == NULL) || (Size == 0U))
  1742. {
  1743. return HAL_ERROR;
  1744. }
  1745. if (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_BUSY) == SET)
  1746. {
  1747. return HAL_BUSY;
  1748. }
  1749. /* Process Locked */
  1750. __HAL_LOCK(hfmpi2c);
  1751. /* Init tickstart for timeout management*/
  1752. tickstart = HAL_GetTick();
  1753. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_TX;
  1754. hfmpi2c->Mode = HAL_FMPI2C_MODE_MEM;
  1755. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1756. /* Prepare transfer parameters */
  1757. hfmpi2c->pBuffPtr = pData;
  1758. hfmpi2c->XferCount = Size;
  1759. hfmpi2c->XferOptions = FMPI2C_NO_OPTION_FRAME;
  1760. hfmpi2c->XferISR = FMPI2C_Master_ISR_IT;
  1761. if (hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  1762. {
  1763. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  1764. xfermode = FMPI2C_RELOAD_MODE;
  1765. }
  1766. else
  1767. {
  1768. hfmpi2c->XferSize = hfmpi2c->XferCount;
  1769. xfermode = FMPI2C_AUTOEND_MODE;
  1770. }
  1771. /* Send Slave Address and Memory Address */
  1772. if (FMPI2C_RequestMemoryWrite(hfmpi2c, DevAddress, MemAddress, MemAddSize, FMPI2C_TIMEOUT_FLAG, tickstart) != HAL_OK)
  1773. {
  1774. if (hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  1775. {
  1776. /* Process Unlocked */
  1777. __HAL_UNLOCK(hfmpi2c);
  1778. return HAL_ERROR;
  1779. }
  1780. else
  1781. {
  1782. /* Process Unlocked */
  1783. __HAL_UNLOCK(hfmpi2c);
  1784. return HAL_TIMEOUT;
  1785. }
  1786. }
  1787. /* Set NBYTES to write and reload if hfmpi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
  1788. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, xfermode, FMPI2C_NO_STARTSTOP);
  1789. /* Process Unlocked */
  1790. __HAL_UNLOCK(hfmpi2c);
  1791. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  1792. to avoid the risk of FMPI2C interrupt handle execution before current
  1793. process unlock */
  1794. /* Enable ERR, TC, STOP, NACK, TXI interrupt */
  1795. /* possible to enable all of these */
  1796. /* FMPI2C_IT_ERRI | FMPI2C_IT_TCI| FMPI2C_IT_STOPI| FMPI2C_IT_NACKI | FMPI2C_IT_ADDRI | FMPI2C_IT_RXI | FMPI2C_IT_TXI */
  1797. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_TX_IT);
  1798. return HAL_OK;
  1799. }
  1800. else
  1801. {
  1802. return HAL_BUSY;
  1803. }
  1804. }
  1805. /**
  1806. * @brief Read an amount of data in non-blocking mode with Interrupt from a specific memory address
  1807. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  1808. * the configuration information for the specified FMPI2C.
  1809. * @param DevAddress Target device address The device 7 bits address value
  1810. * in datasheet must be shifted to the left before calling the interface
  1811. * @param MemAddress Internal memory address
  1812. * @param MemAddSize Size of internal memory address
  1813. * @param pData Pointer to data buffer
  1814. * @param Size Amount of data to be sent
  1815. * @retval HAL status
  1816. */
  1817. HAL_StatusTypeDef HAL_FMPI2C_Mem_Read_IT(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
  1818. {
  1819. uint32_t tickstart = 0U;
  1820. uint32_t xfermode = 0U;
  1821. /* Check the parameters */
  1822. assert_param(IS_FMPI2C_MEMADD_SIZE(MemAddSize));
  1823. if (hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1824. {
  1825. if ((pData == NULL) || (Size == 0U))
  1826. {
  1827. return HAL_ERROR;
  1828. }
  1829. if (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_BUSY) == SET)
  1830. {
  1831. return HAL_BUSY;
  1832. }
  1833. /* Process Locked */
  1834. __HAL_LOCK(hfmpi2c);
  1835. /* Init tickstart for timeout management*/
  1836. tickstart = HAL_GetTick();
  1837. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_RX;
  1838. hfmpi2c->Mode = HAL_FMPI2C_MODE_MEM;
  1839. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1840. /* Prepare transfer parameters */
  1841. hfmpi2c->pBuffPtr = pData;
  1842. hfmpi2c->XferCount = Size;
  1843. hfmpi2c->XferOptions = FMPI2C_NO_OPTION_FRAME;
  1844. hfmpi2c->XferISR = FMPI2C_Master_ISR_IT;
  1845. if (hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  1846. {
  1847. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  1848. xfermode = FMPI2C_RELOAD_MODE;
  1849. }
  1850. else
  1851. {
  1852. hfmpi2c->XferSize = hfmpi2c->XferCount;
  1853. xfermode = FMPI2C_AUTOEND_MODE;
  1854. }
  1855. /* Send Slave Address and Memory Address */
  1856. if (FMPI2C_RequestMemoryRead(hfmpi2c, DevAddress, MemAddress, MemAddSize, FMPI2C_TIMEOUT_FLAG, tickstart) != HAL_OK)
  1857. {
  1858. if (hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  1859. {
  1860. /* Process Unlocked */
  1861. __HAL_UNLOCK(hfmpi2c);
  1862. return HAL_ERROR;
  1863. }
  1864. else
  1865. {
  1866. /* Process Unlocked */
  1867. __HAL_UNLOCK(hfmpi2c);
  1868. return HAL_TIMEOUT;
  1869. }
  1870. }
  1871. /* Set NBYTES to write and reload if hfmpi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
  1872. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, xfermode, FMPI2C_GENERATE_START_READ);
  1873. /* Process Unlocked */
  1874. __HAL_UNLOCK(hfmpi2c);
  1875. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  1876. to avoid the risk of FMPI2C interrupt handle execution before current
  1877. process unlock */
  1878. /* Enable ERR, TC, STOP, NACK, RXI interrupt */
  1879. /* possible to enable all of these */
  1880. /* FMPI2C_IT_ERRI | FMPI2C_IT_TCI| FMPI2C_IT_STOPI| FMPI2C_IT_NACKI | FMPI2C_IT_ADDRI | FMPI2C_IT_RXI | FMPI2C_IT_TXI */
  1881. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_RX_IT);
  1882. return HAL_OK;
  1883. }
  1884. else
  1885. {
  1886. return HAL_BUSY;
  1887. }
  1888. }
  1889. /**
  1890. * @brief Write an amount of data in non-blocking mode with DMA to a specific memory address
  1891. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  1892. * the configuration information for the specified FMPI2C.
  1893. * @param DevAddress Target device address The device 7 bits address value
  1894. * in datasheet must be shifted to the left before calling the interface
  1895. * @param MemAddress Internal memory address
  1896. * @param MemAddSize Size of internal memory address
  1897. * @param pData Pointer to data buffer
  1898. * @param Size Amount of data to be sent
  1899. * @retval HAL status
  1900. */
  1901. HAL_StatusTypeDef HAL_FMPI2C_Mem_Write_DMA(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
  1902. {
  1903. uint32_t tickstart = 0U;
  1904. uint32_t xfermode = 0U;
  1905. /* Check the parameters */
  1906. assert_param(IS_FMPI2C_MEMADD_SIZE(MemAddSize));
  1907. if (hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1908. {
  1909. if ((pData == NULL) || (Size == 0U))
  1910. {
  1911. return HAL_ERROR;
  1912. }
  1913. if (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_BUSY) == SET)
  1914. {
  1915. return HAL_BUSY;
  1916. }
  1917. /* Process Locked */
  1918. __HAL_LOCK(hfmpi2c);
  1919. /* Init tickstart for timeout management*/
  1920. tickstart = HAL_GetTick();
  1921. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_TX;
  1922. hfmpi2c->Mode = HAL_FMPI2C_MODE_MEM;
  1923. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1924. /* Prepare transfer parameters */
  1925. hfmpi2c->pBuffPtr = pData;
  1926. hfmpi2c->XferCount = Size;
  1927. hfmpi2c->XferOptions = FMPI2C_NO_OPTION_FRAME;
  1928. hfmpi2c->XferISR = FMPI2C_Master_ISR_DMA;
  1929. if (hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  1930. {
  1931. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  1932. xfermode = FMPI2C_RELOAD_MODE;
  1933. }
  1934. else
  1935. {
  1936. hfmpi2c->XferSize = hfmpi2c->XferCount;
  1937. xfermode = FMPI2C_AUTOEND_MODE;
  1938. }
  1939. /* Send Slave Address and Memory Address */
  1940. if (FMPI2C_RequestMemoryWrite(hfmpi2c, DevAddress, MemAddress, MemAddSize, FMPI2C_TIMEOUT_FLAG, tickstart) != HAL_OK)
  1941. {
  1942. if (hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  1943. {
  1944. /* Process Unlocked */
  1945. __HAL_UNLOCK(hfmpi2c);
  1946. return HAL_ERROR;
  1947. }
  1948. else
  1949. {
  1950. /* Process Unlocked */
  1951. __HAL_UNLOCK(hfmpi2c);
  1952. return HAL_TIMEOUT;
  1953. }
  1954. }
  1955. /* Set the FMPI2C DMA transfer complete callback */
  1956. hfmpi2c->hdmatx->XferCpltCallback = FMPI2C_DMAMasterTransmitCplt;
  1957. /* Set the DMA error callback */
  1958. hfmpi2c->hdmatx->XferErrorCallback = FMPI2C_DMAError;
  1959. /* Set the unused DMA callbacks to NULL */
  1960. hfmpi2c->hdmatx->XferHalfCpltCallback = NULL;
  1961. hfmpi2c->hdmatx->XferAbortCallback = NULL;
  1962. /* Enable the DMA stream */
  1963. HAL_DMA_Start_IT(hfmpi2c->hdmatx, (uint32_t)pData, (uint32_t)&hfmpi2c->Instance->TXDR, hfmpi2c->XferSize);
  1964. /* Send Slave Address */
  1965. /* Set NBYTES to write and reload if hfmpi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
  1966. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, xfermode, FMPI2C_NO_STARTSTOP);
  1967. /* Update XferCount value */
  1968. hfmpi2c->XferCount -= hfmpi2c->XferSize;
  1969. /* Process Unlocked */
  1970. __HAL_UNLOCK(hfmpi2c);
  1971. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  1972. to avoid the risk of FMPI2C interrupt handle execution before current
  1973. process unlock */
  1974. /* Enable ERR and NACK interrupts */
  1975. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_ERROR_IT);
  1976. /* Enable DMA Request */
  1977. hfmpi2c->Instance->CR1 |= FMPI2C_CR1_TXDMAEN;
  1978. return HAL_OK;
  1979. }
  1980. else
  1981. {
  1982. return HAL_BUSY;
  1983. }
  1984. }
  1985. /**
  1986. * @brief Reads an amount of data in non-blocking mode with DMA from a specific memory address.
  1987. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  1988. * the configuration information for the specified FMPI2C.
  1989. * @param DevAddress Target device address The device 7 bits address value
  1990. * in datasheet must be shifted to the left before calling the interface
  1991. * @param MemAddress Internal memory address
  1992. * @param MemAddSize Size of internal memory address
  1993. * @param pData Pointer to data buffer
  1994. * @param Size Amount of data to be read
  1995. * @retval HAL status
  1996. */
  1997. HAL_StatusTypeDef HAL_FMPI2C_Mem_Read_DMA(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
  1998. {
  1999. uint32_t tickstart = 0U;
  2000. uint32_t xfermode = 0U;
  2001. /* Check the parameters */
  2002. assert_param(IS_FMPI2C_MEMADD_SIZE(MemAddSize));
  2003. if (hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  2004. {
  2005. if ((pData == NULL) || (Size == 0U))
  2006. {
  2007. return HAL_ERROR;
  2008. }
  2009. if (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_BUSY) == SET)
  2010. {
  2011. return HAL_BUSY;
  2012. }
  2013. /* Process Locked */
  2014. __HAL_LOCK(hfmpi2c);
  2015. /* Init tickstart for timeout management*/
  2016. tickstart = HAL_GetTick();
  2017. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_RX;
  2018. hfmpi2c->Mode = HAL_FMPI2C_MODE_MEM;
  2019. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  2020. /* Prepare transfer parameters */
  2021. hfmpi2c->pBuffPtr = pData;
  2022. hfmpi2c->XferCount = Size;
  2023. hfmpi2c->XferOptions = FMPI2C_NO_OPTION_FRAME;
  2024. hfmpi2c->XferISR = FMPI2C_Master_ISR_DMA;
  2025. if (hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  2026. {
  2027. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  2028. xfermode = FMPI2C_RELOAD_MODE;
  2029. }
  2030. else
  2031. {
  2032. hfmpi2c->XferSize = hfmpi2c->XferCount;
  2033. xfermode = FMPI2C_AUTOEND_MODE;
  2034. }
  2035. /* Send Slave Address and Memory Address */
  2036. if (FMPI2C_RequestMemoryRead(hfmpi2c, DevAddress, MemAddress, MemAddSize, FMPI2C_TIMEOUT_FLAG, tickstart) != HAL_OK)
  2037. {
  2038. if (hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  2039. {
  2040. /* Process Unlocked */
  2041. __HAL_UNLOCK(hfmpi2c);
  2042. return HAL_ERROR;
  2043. }
  2044. else
  2045. {
  2046. /* Process Unlocked */
  2047. __HAL_UNLOCK(hfmpi2c);
  2048. return HAL_TIMEOUT;
  2049. }
  2050. }
  2051. /* Set the FMPI2C DMA transfer complete callback */
  2052. hfmpi2c->hdmarx->XferCpltCallback = FMPI2C_DMAMasterReceiveCplt;
  2053. /* Set the DMA error callback */
  2054. hfmpi2c->hdmarx->XferErrorCallback = FMPI2C_DMAError;
  2055. /* Set the unused DMA callbacks to NULL */
  2056. hfmpi2c->hdmarx->XferHalfCpltCallback = NULL;
  2057. hfmpi2c->hdmarx->XferAbortCallback = NULL;
  2058. /* Enable the DMA stream */
  2059. HAL_DMA_Start_IT(hfmpi2c->hdmarx, (uint32_t)&hfmpi2c->Instance->RXDR, (uint32_t)pData, hfmpi2c->XferSize);
  2060. /* Set NBYTES to write and reload if hfmpi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
  2061. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, xfermode, FMPI2C_GENERATE_START_READ);
  2062. /* Update XferCount value */
  2063. hfmpi2c->XferCount -= hfmpi2c->XferSize;
  2064. /* Process Unlocked */
  2065. __HAL_UNLOCK(hfmpi2c);
  2066. /* Enable DMA Request */
  2067. hfmpi2c->Instance->CR1 |= FMPI2C_CR1_RXDMAEN;
  2068. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  2069. to avoid the risk of FMPI2C interrupt handle execution before current
  2070. process unlock */
  2071. /* Enable ERR and NACK interrupts */
  2072. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_ERROR_IT);
  2073. return HAL_OK;
  2074. }
  2075. else
  2076. {
  2077. return HAL_BUSY;
  2078. }
  2079. }
  2080. /**
  2081. * @brief Checks if target device is ready for communication.
  2082. * @note This function is used with Memory devices
  2083. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2084. * the configuration information for the specified FMPI2C.
  2085. * @param DevAddress Target device address The device 7 bits address value
  2086. * in datasheet must be shifted to the left before calling the interface
  2087. * @param Trials Number of trials
  2088. * @param Timeout Timeout duration
  2089. * @retval HAL status
  2090. */
  2091. HAL_StatusTypeDef HAL_FMPI2C_IsDeviceReady(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint32_t Trials, uint32_t Timeout)
  2092. {
  2093. uint32_t tickstart = 0U;
  2094. __IO uint32_t FMPI2C_Trials = 0U;
  2095. if (hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  2096. {
  2097. if (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_BUSY) == SET)
  2098. {
  2099. return HAL_BUSY;
  2100. }
  2101. /* Process Locked */
  2102. __HAL_LOCK(hfmpi2c);
  2103. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY;
  2104. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  2105. do
  2106. {
  2107. /* Generate Start */
  2108. hfmpi2c->Instance->CR2 = FMPI2C_GENERATE_START(hfmpi2c->Init.AddressingMode, DevAddress);
  2109. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  2110. /* Wait until STOPF flag is set or a NACK flag is set*/
  2111. tickstart = HAL_GetTick();
  2112. while ((__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF) == RESET) && (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_AF) == RESET) && (hfmpi2c->State != HAL_FMPI2C_STATE_TIMEOUT))
  2113. {
  2114. if (Timeout != HAL_MAX_DELAY)
  2115. {
  2116. if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
  2117. {
  2118. /* Device is ready */
  2119. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  2120. /* Process Unlocked */
  2121. __HAL_UNLOCK(hfmpi2c);
  2122. return HAL_TIMEOUT;
  2123. }
  2124. }
  2125. }
  2126. /* Check if the NACKF flag has not been set */
  2127. if (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_AF) == RESET)
  2128. {
  2129. /* Wait until STOPF flag is reset */
  2130. if (FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_STOPF, RESET, Timeout, tickstart) != HAL_OK)
  2131. {
  2132. return HAL_TIMEOUT;
  2133. }
  2134. /* Clear STOP Flag */
  2135. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  2136. /* Device is ready */
  2137. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  2138. /* Process Unlocked */
  2139. __HAL_UNLOCK(hfmpi2c);
  2140. return HAL_OK;
  2141. }
  2142. else
  2143. {
  2144. /* Wait until STOPF flag is reset */
  2145. if (FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_STOPF, RESET, Timeout, tickstart) != HAL_OK)
  2146. {
  2147. return HAL_TIMEOUT;
  2148. }
  2149. /* Clear NACK Flag */
  2150. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_AF);
  2151. /* Clear STOP Flag, auto generated with autoend*/
  2152. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  2153. }
  2154. /* Check if the maximum allowed number of trials has been reached */
  2155. if (FMPI2C_Trials++ == Trials)
  2156. {
  2157. /* Generate Stop */
  2158. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_STOP;
  2159. /* Wait until STOPF flag is reset */
  2160. if (FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_STOPF, RESET, Timeout, tickstart) != HAL_OK)
  2161. {
  2162. return HAL_TIMEOUT;
  2163. }
  2164. /* Clear STOP Flag */
  2165. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  2166. }
  2167. }
  2168. while (FMPI2C_Trials < Trials);
  2169. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  2170. /* Process Unlocked */
  2171. __HAL_UNLOCK(hfmpi2c);
  2172. return HAL_TIMEOUT;
  2173. }
  2174. else
  2175. {
  2176. return HAL_BUSY;
  2177. }
  2178. }
  2179. /**
  2180. * @brief Sequential transmit in master FMPI2C mode an amount of data in non-blocking mode with Interrupt.
  2181. * @note This interface allow to manage repeated start condition when a direction change during transfer
  2182. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2183. * the configuration information for the specified FMPI2C.
  2184. * @param DevAddress Target device address The device 7 bits address value
  2185. * in datasheet must be shifted to the left before calling the interface
  2186. * @param pData Pointer to data buffer
  2187. * @param Size Amount of data to be sent
  2188. * @param XferOptions Options of Transfer, value of @ref FMPI2C_XFEROPTIONS
  2189. * @retval HAL status
  2190. */
  2191. HAL_StatusTypeDef HAL_FMPI2C_Master_Sequential_Transmit_IT(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
  2192. {
  2193. uint32_t xfermode = 0U;
  2194. uint32_t xferrequest = FMPI2C_GENERATE_START_WRITE;
  2195. /* Check the parameters */
  2196. assert_param(IS_FMPI2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  2197. if (hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  2198. {
  2199. /* Process Locked */
  2200. __HAL_LOCK(hfmpi2c);
  2201. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_TX;
  2202. hfmpi2c->Mode = HAL_FMPI2C_MODE_MASTER;
  2203. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  2204. /* Prepare transfer parameters */
  2205. hfmpi2c->pBuffPtr = pData;
  2206. hfmpi2c->XferCount = Size;
  2207. hfmpi2c->XferOptions = XferOptions;
  2208. hfmpi2c->XferISR = FMPI2C_Master_ISR_IT;
  2209. /* If size > MAX_NBYTE_SIZE, use reload mode */
  2210. if (hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  2211. {
  2212. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  2213. xfermode = FMPI2C_RELOAD_MODE;
  2214. }
  2215. else
  2216. {
  2217. hfmpi2c->XferSize = hfmpi2c->XferCount;
  2218. xfermode = hfmpi2c->XferOptions;
  2219. }
  2220. /* If transfer direction not change, do not generate Restart Condition */
  2221. /* Mean Previous state is same as current state */
  2222. if (hfmpi2c->PreviousState == FMPI2C_STATE_MASTER_BUSY_TX)
  2223. {
  2224. xferrequest = FMPI2C_NO_STARTSTOP;
  2225. }
  2226. /* Send Slave Address and set NBYTES to write */
  2227. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, xfermode, xferrequest);
  2228. /* Process Unlocked */
  2229. __HAL_UNLOCK(hfmpi2c);
  2230. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  2231. to avoid the risk of FMPI2C interrupt handle execution before current
  2232. process unlock */
  2233. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_TX_IT);
  2234. return HAL_OK;
  2235. }
  2236. else
  2237. {
  2238. return HAL_BUSY;
  2239. }
  2240. }
  2241. /**
  2242. * @brief Sequential receive in master FMPI2C mode an amount of data in non-blocking mode with Interrupt
  2243. * @note This interface allow to manage repeated start condition when a direction change during transfer
  2244. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2245. * the configuration information for the specified FMPI2C.
  2246. * @param DevAddress Target device address The device 7 bits address value
  2247. * in datasheet must be shifted to the left before calling the interface
  2248. * @param pData Pointer to data buffer
  2249. * @param Size Amount of data to be sent
  2250. * @param XferOptions Options of Transfer, value of @ref FMPI2C_XFEROPTIONS
  2251. * @retval HAL status
  2252. */
  2253. HAL_StatusTypeDef HAL_FMPI2C_Master_Sequential_Receive_IT(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
  2254. {
  2255. uint32_t xfermode = 0U;
  2256. uint32_t xferrequest = FMPI2C_GENERATE_START_READ;
  2257. /* Check the parameters */
  2258. assert_param(IS_FMPI2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  2259. if (hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  2260. {
  2261. /* Process Locked */
  2262. __HAL_LOCK(hfmpi2c);
  2263. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_RX;
  2264. hfmpi2c->Mode = HAL_FMPI2C_MODE_MASTER;
  2265. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  2266. /* Prepare transfer parameters */
  2267. hfmpi2c->pBuffPtr = pData;
  2268. hfmpi2c->XferCount = Size;
  2269. hfmpi2c->XferOptions = XferOptions;
  2270. hfmpi2c->XferISR = FMPI2C_Master_ISR_IT;
  2271. /* If hfmpi2c->XferCount > MAX_NBYTE_SIZE, use reload mode */
  2272. if (hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  2273. {
  2274. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  2275. xfermode = FMPI2C_RELOAD_MODE;
  2276. }
  2277. else
  2278. {
  2279. hfmpi2c->XferSize = hfmpi2c->XferCount;
  2280. xfermode = hfmpi2c->XferOptions;
  2281. }
  2282. /* If transfer direction not change, do not generate Restart Condition */
  2283. /* Mean Previous state is same as current state */
  2284. if (hfmpi2c->PreviousState == FMPI2C_STATE_MASTER_BUSY_RX)
  2285. {
  2286. xferrequest = FMPI2C_NO_STARTSTOP;
  2287. }
  2288. /* Send Slave Address and set NBYTES to read */
  2289. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, xfermode, xferrequest);
  2290. /* Process Unlocked */
  2291. __HAL_UNLOCK(hfmpi2c);
  2292. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  2293. to avoid the risk of FMPI2C interrupt handle execution before current
  2294. process unlock */
  2295. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_RX_IT);
  2296. return HAL_OK;
  2297. }
  2298. else
  2299. {
  2300. return HAL_BUSY;
  2301. }
  2302. }
  2303. /**
  2304. * @brief Sequential transmit in slave/device FMPI2C mode an amount of data in non-blocking mode with Interrupt
  2305. * @note This interface allow to manage repeated start condition when a direction change during transfer
  2306. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2307. * the configuration information for the specified FMPI2C.
  2308. * @param pData Pointer to data buffer
  2309. * @param Size Amount of data to be sent
  2310. * @param XferOptions Options of Transfer, value of @ref FMPI2C_XFEROPTIONS
  2311. * @retval HAL status
  2312. */
  2313. HAL_StatusTypeDef HAL_FMPI2C_Slave_Sequential_Transmit_IT(FMPI2C_HandleTypeDef *hfmpi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
  2314. {
  2315. /* Check the parameters */
  2316. assert_param(IS_FMPI2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  2317. if ((hfmpi2c->State & HAL_FMPI2C_STATE_LISTEN) == HAL_FMPI2C_STATE_LISTEN)
  2318. {
  2319. if ((pData == NULL) || (Size == 0U))
  2320. {
  2321. return HAL_ERROR;
  2322. }
  2323. /* Disable Interrupts, to prevent preemption during treatment in case of multicall */
  2324. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_LISTEN_IT | FMPI2C_XFER_TX_IT);
  2325. /* Process Locked */
  2326. __HAL_LOCK(hfmpi2c);
  2327. /* FMPI2C cannot manage full duplex exchange so disable previous IT enabled if any */
  2328. /* and then toggle the HAL slave RX state to TX state */
  2329. if (hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_RX_LISTEN)
  2330. {
  2331. /* Disable associated Interrupts */
  2332. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_RX_IT);
  2333. }
  2334. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_TX_LISTEN;
  2335. hfmpi2c->Mode = HAL_FMPI2C_MODE_SLAVE;
  2336. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  2337. /* Enable Address Acknowledge */
  2338. hfmpi2c->Instance->CR2 &= ~FMPI2C_CR2_NACK;
  2339. /* Prepare transfer parameters */
  2340. hfmpi2c->pBuffPtr = pData;
  2341. hfmpi2c->XferCount = Size;
  2342. hfmpi2c->XferSize = hfmpi2c->XferCount;
  2343. hfmpi2c->XferOptions = XferOptions;
  2344. hfmpi2c->XferISR = FMPI2C_Slave_ISR_IT;
  2345. if (FMPI2C_GET_DIR(hfmpi2c) == FMPI2C_DIRECTION_RECEIVE)
  2346. {
  2347. /* Clear ADDR flag after prepare the transfer parameters */
  2348. /* This action will generate an acknowledge to the Master */
  2349. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_ADDR);
  2350. }
  2351. /* Process Unlocked */
  2352. __HAL_UNLOCK(hfmpi2c);
  2353. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  2354. to avoid the risk of FMPI2C interrupt handle execution before current
  2355. process unlock */
  2356. /* REnable ADDR interrupt */
  2357. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_TX_IT | FMPI2C_XFER_LISTEN_IT);
  2358. return HAL_OK;
  2359. }
  2360. else
  2361. {
  2362. return HAL_ERROR;
  2363. }
  2364. }
  2365. /**
  2366. * @brief Sequential receive in slave/device FMPI2C mode an amount of data in non-blocking mode with Interrupt
  2367. * @note This interface allow to manage repeated start condition when a direction change during transfer
  2368. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2369. * the configuration information for the specified FMPI2C.
  2370. * @param pData Pointer to data buffer
  2371. * @param Size Amount of data to be sent
  2372. * @param XferOptions Options of Transfer, value of @ref FMPI2C_XFEROPTIONS
  2373. * @retval HAL status
  2374. */
  2375. HAL_StatusTypeDef HAL_FMPI2C_Slave_Sequential_Receive_IT(FMPI2C_HandleTypeDef *hfmpi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
  2376. {
  2377. /* Check the parameters */
  2378. assert_param(IS_FMPI2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  2379. if ((hfmpi2c->State & HAL_FMPI2C_STATE_LISTEN) == HAL_FMPI2C_STATE_LISTEN)
  2380. {
  2381. if ((pData == NULL) || (Size == 0U))
  2382. {
  2383. return HAL_ERROR;
  2384. }
  2385. /* Disable Interrupts, to prevent preemption during treatment in case of multicall */
  2386. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_LISTEN_IT | FMPI2C_XFER_RX_IT);
  2387. /* Process Locked */
  2388. __HAL_LOCK(hfmpi2c);
  2389. /* FMPI2C cannot manage full duplex exchange so disable previous IT enabled if any */
  2390. /* and then toggle the HAL slave TX state to RX state */
  2391. if (hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_TX_LISTEN)
  2392. {
  2393. /* Disable associated Interrupts */
  2394. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_TX_IT);
  2395. }
  2396. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_RX_LISTEN;
  2397. hfmpi2c->Mode = HAL_FMPI2C_MODE_SLAVE;
  2398. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  2399. /* Enable Address Acknowledge */
  2400. hfmpi2c->Instance->CR2 &= ~FMPI2C_CR2_NACK;
  2401. /* Prepare transfer parameters */
  2402. hfmpi2c->pBuffPtr = pData;
  2403. hfmpi2c->XferCount = Size;
  2404. hfmpi2c->XferSize = hfmpi2c->XferCount;
  2405. hfmpi2c->XferOptions = XferOptions;
  2406. hfmpi2c->XferISR = FMPI2C_Slave_ISR_IT;
  2407. if (FMPI2C_GET_DIR(hfmpi2c) == FMPI2C_DIRECTION_TRANSMIT)
  2408. {
  2409. /* Clear ADDR flag after prepare the transfer parameters */
  2410. /* This action will generate an acknowledge to the Master */
  2411. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_ADDR);
  2412. }
  2413. /* Process Unlocked */
  2414. __HAL_UNLOCK(hfmpi2c);
  2415. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  2416. to avoid the risk of FMPI2C interrupt handle execution before current
  2417. process unlock */
  2418. /* REnable ADDR interrupt */
  2419. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_RX_IT | FMPI2C_XFER_LISTEN_IT);
  2420. return HAL_OK;
  2421. }
  2422. else
  2423. {
  2424. return HAL_ERROR;
  2425. }
  2426. }
  2427. /**
  2428. * @brief Enable the Address listen mode with Interrupt.
  2429. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2430. * the configuration information for the specified FMPI2C.
  2431. * @retval HAL status
  2432. */
  2433. HAL_StatusTypeDef HAL_FMPI2C_EnableListen_IT(FMPI2C_HandleTypeDef *hfmpi2c)
  2434. {
  2435. if (hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  2436. {
  2437. hfmpi2c->State = HAL_FMPI2C_STATE_LISTEN;
  2438. hfmpi2c->XferISR = FMPI2C_Slave_ISR_IT;
  2439. /* Enable the Address Match interrupt */
  2440. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_LISTEN_IT);
  2441. return HAL_OK;
  2442. }
  2443. else
  2444. {
  2445. return HAL_BUSY;
  2446. }
  2447. }
  2448. /**
  2449. * @brief Disable the Address listen mode with Interrupt.
  2450. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2451. * the configuration information for the specified FMPI2C
  2452. * @retval HAL status
  2453. */
  2454. HAL_StatusTypeDef HAL_FMPI2C_DisableListen_IT(FMPI2C_HandleTypeDef *hfmpi2c)
  2455. {
  2456. /* Declaration of tmp to prevent undefined behavior of volatile usage */
  2457. uint32_t tmp;
  2458. /* Disable Address listen mode only if a transfer is not ongoing */
  2459. if (hfmpi2c->State == HAL_FMPI2C_STATE_LISTEN)
  2460. {
  2461. tmp = (uint32_t)(hfmpi2c->State) & FMPI2C_STATE_MSK;
  2462. hfmpi2c->PreviousState = tmp | (uint32_t)(hfmpi2c->Mode);
  2463. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  2464. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  2465. hfmpi2c->XferISR = NULL;
  2466. /* Disable the Address Match interrupt */
  2467. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_LISTEN_IT);
  2468. return HAL_OK;
  2469. }
  2470. else
  2471. {
  2472. return HAL_BUSY;
  2473. }
  2474. }
  2475. /**
  2476. * @brief Abort a master FMPI2C IT or DMA process communication with Interrupt.
  2477. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2478. * the configuration information for the specified FMPI2C.
  2479. * @param DevAddress Target device address The device 7 bits address value
  2480. * in datasheet must be shifted to the left before calling the interface
  2481. * @retval HAL status
  2482. */
  2483. HAL_StatusTypeDef HAL_FMPI2C_Master_Abort_IT(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress)
  2484. {
  2485. if (hfmpi2c->Mode == HAL_FMPI2C_MODE_MASTER)
  2486. {
  2487. /* Process Locked */
  2488. __HAL_LOCK(hfmpi2c);
  2489. /* Disable Interrupts */
  2490. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_RX_IT);
  2491. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_TX_IT);
  2492. /* Set State at HAL_FMPI2C_STATE_ABORT */
  2493. hfmpi2c->State = HAL_FMPI2C_STATE_ABORT;
  2494. /* Set NBYTES to 1 to generate a dummy read on FMPI2C peripheral */
  2495. /* Set AUTOEND mode, this will generate a NACK then STOP condition to abort the current transfer */
  2496. FMPI2C_TransferConfig(hfmpi2c, DevAddress, 1, FMPI2C_AUTOEND_MODE, FMPI2C_GENERATE_STOP);
  2497. /* Process Unlocked */
  2498. __HAL_UNLOCK(hfmpi2c);
  2499. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  2500. to avoid the risk of FMPI2C interrupt handle execution before current
  2501. process unlock */
  2502. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_CPLT_IT);
  2503. return HAL_OK;
  2504. }
  2505. else
  2506. {
  2507. /* Wrong usage of abort function */
  2508. /* This function should be used only in case of abort monitored by master device */
  2509. return HAL_ERROR;
  2510. }
  2511. }
  2512. /**
  2513. * @}
  2514. */
  2515. /** @defgroup FMPI2C_IRQ_Handler_and_Callbacks IRQ Handler and Callbacks
  2516. * @{
  2517. */
  2518. /**
  2519. * @brief This function handles FMPI2C event interrupt request.
  2520. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2521. * the configuration information for the specified FMPI2C.
  2522. * @retval None
  2523. */
  2524. void HAL_FMPI2C_EV_IRQHandler(FMPI2C_HandleTypeDef *hfmpi2c)
  2525. {
  2526. /* Get current IT Flags and IT sources value */
  2527. uint32_t itflags = READ_REG(hfmpi2c->Instance->ISR);
  2528. uint32_t itsources = READ_REG(hfmpi2c->Instance->CR1);
  2529. /* FMPI2C events treatment -------------------------------------*/
  2530. if (hfmpi2c->XferISR != NULL)
  2531. {
  2532. hfmpi2c->XferISR(hfmpi2c, itflags, itsources);
  2533. }
  2534. }
  2535. /**
  2536. * @brief This function handles FMPI2C error interrupt request.
  2537. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2538. * the configuration information for the specified FMPI2C.
  2539. * @retval None
  2540. */
  2541. void HAL_FMPI2C_ER_IRQHandler(FMPI2C_HandleTypeDef *hfmpi2c)
  2542. {
  2543. uint32_t itflags = READ_REG(hfmpi2c->Instance->ISR);
  2544. uint32_t itsources = READ_REG(hfmpi2c->Instance->CR1);
  2545. /* FMPI2C Bus error interrupt occurred ------------------------------------*/
  2546. if (((itflags & FMPI2C_FLAG_BERR) != RESET) && ((itsources & FMPI2C_IT_ERRI) != RESET))
  2547. {
  2548. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_BERR;
  2549. /* Clear BERR flag */
  2550. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_BERR);
  2551. }
  2552. /* FMPI2C Over-Run/Under-Run interrupt occurred ----------------------------------------*/
  2553. if (((itflags & FMPI2C_FLAG_OVR) != RESET) && ((itsources & FMPI2C_IT_ERRI) != RESET))
  2554. {
  2555. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_OVR;
  2556. /* Clear OVR flag */
  2557. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_OVR);
  2558. }
  2559. /* FMPI2C Arbitration Loss error interrupt occurred -------------------------------------*/
  2560. if (((itflags & FMPI2C_FLAG_ARLO) != RESET) && ((itsources & FMPI2C_IT_ERRI) != RESET))
  2561. {
  2562. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_ARLO;
  2563. /* Clear ARLO flag */
  2564. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_ARLO);
  2565. }
  2566. /* Call the Error Callback in case of Error detected */
  2567. if ((hfmpi2c->ErrorCode & (HAL_FMPI2C_ERROR_BERR | HAL_FMPI2C_ERROR_OVR | HAL_FMPI2C_ERROR_ARLO)) != HAL_FMPI2C_ERROR_NONE)
  2568. {
  2569. FMPI2C_ITError(hfmpi2c, hfmpi2c->ErrorCode);
  2570. }
  2571. }
  2572. /**
  2573. * @brief Master Tx Transfer completed callback.
  2574. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2575. * the configuration information for the specified FMPI2C.
  2576. * @retval None
  2577. */
  2578. __weak void HAL_FMPI2C_MasterTxCpltCallback(FMPI2C_HandleTypeDef *hfmpi2c)
  2579. {
  2580. /* Prevent unused argument(s) compilation warning */
  2581. UNUSED(hfmpi2c);
  2582. /* NOTE : This function should not be modified, when the callback is needed,
  2583. the HAL_FMPI2C_MasterTxCpltCallback could be implemented in the user file
  2584. */
  2585. }
  2586. /**
  2587. * @brief Master Rx Transfer completed callback.
  2588. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2589. * the configuration information for the specified FMPI2C.
  2590. * @retval None
  2591. */
  2592. __weak void HAL_FMPI2C_MasterRxCpltCallback(FMPI2C_HandleTypeDef *hfmpi2c)
  2593. {
  2594. /* Prevent unused argument(s) compilation warning */
  2595. UNUSED(hfmpi2c);
  2596. /* NOTE : This function should not be modified, when the callback is needed,
  2597. the HAL_FMPI2C_MasterRxCpltCallback could be implemented in the user file
  2598. */
  2599. }
  2600. /** @brief Slave Tx Transfer completed callback.
  2601. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2602. * the configuration information for the specified FMPI2C.
  2603. * @retval None
  2604. */
  2605. __weak void HAL_FMPI2C_SlaveTxCpltCallback(FMPI2C_HandleTypeDef *hfmpi2c)
  2606. {
  2607. /* Prevent unused argument(s) compilation warning */
  2608. UNUSED(hfmpi2c);
  2609. /* NOTE : This function should not be modified, when the callback is needed,
  2610. the HAL_FMPI2C_SlaveTxCpltCallback could be implemented in the user file
  2611. */
  2612. }
  2613. /**
  2614. * @brief Slave Rx Transfer completed callback.
  2615. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2616. * the configuration information for the specified FMPI2C.
  2617. * @retval None
  2618. */
  2619. __weak void HAL_FMPI2C_SlaveRxCpltCallback(FMPI2C_HandleTypeDef *hfmpi2c)
  2620. {
  2621. /* Prevent unused argument(s) compilation warning */
  2622. UNUSED(hfmpi2c);
  2623. /* NOTE : This function should not be modified, when the callback is needed,
  2624. the HAL_FMPI2C_SlaveRxCpltCallback could be implemented in the user file
  2625. */
  2626. }
  2627. /**
  2628. * @brief Slave Address Match callback.
  2629. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2630. * the configuration information for the specified FMPI2C.
  2631. * @param TransferDirection Master request Transfer Direction (Write/Read), value of @ref FMPI2C_XFERDIRECTION
  2632. * @param AddrMatchCode Address Match Code
  2633. * @retval None
  2634. */
  2635. __weak void HAL_FMPI2C_AddrCallback(FMPI2C_HandleTypeDef *hfmpi2c, uint8_t TransferDirection, uint16_t AddrMatchCode)
  2636. {
  2637. /* Prevent unused argument(s) compilation warning */
  2638. UNUSED(hfmpi2c);
  2639. UNUSED(TransferDirection);
  2640. UNUSED(AddrMatchCode);
  2641. /* NOTE : This function should not be modified, when the callback is needed,
  2642. the HAL_FMPI2C_AddrCallback() could be implemented in the user file
  2643. */
  2644. }
  2645. /**
  2646. * @brief Listen Complete callback.
  2647. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2648. * the configuration information for the specified FMPI2C.
  2649. * @retval None
  2650. */
  2651. __weak void HAL_FMPI2C_ListenCpltCallback(FMPI2C_HandleTypeDef *hfmpi2c)
  2652. {
  2653. /* Prevent unused argument(s) compilation warning */
  2654. UNUSED(hfmpi2c);
  2655. /* NOTE : This function should not be modified, when the callback is needed,
  2656. the HAL_FMPI2C_ListenCpltCallback() could be implemented in the user file
  2657. */
  2658. }
  2659. /**
  2660. * @brief Memory Tx Transfer completed callback.
  2661. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2662. * the configuration information for the specified FMPI2C.
  2663. * @retval None
  2664. */
  2665. __weak void HAL_FMPI2C_MemTxCpltCallback(FMPI2C_HandleTypeDef *hfmpi2c)
  2666. {
  2667. /* Prevent unused argument(s) compilation warning */
  2668. UNUSED(hfmpi2c);
  2669. /* NOTE : This function should not be modified, when the callback is needed,
  2670. the HAL_FMPI2C_MemTxCpltCallback could be implemented in the user file
  2671. */
  2672. }
  2673. /**
  2674. * @brief Memory Rx Transfer completed callback.
  2675. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2676. * the configuration information for the specified FMPI2C.
  2677. * @retval None
  2678. */
  2679. __weak void HAL_FMPI2C_MemRxCpltCallback(FMPI2C_HandleTypeDef *hfmpi2c)
  2680. {
  2681. /* Prevent unused argument(s) compilation warning */
  2682. UNUSED(hfmpi2c);
  2683. /* NOTE : This function should not be modified, when the callback is needed,
  2684. the HAL_FMPI2C_MemRxCpltCallback could be implemented in the user file
  2685. */
  2686. }
  2687. /**
  2688. * @brief FMPI2C error callback.
  2689. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2690. * the configuration information for the specified FMPI2C.
  2691. * @retval None
  2692. */
  2693. __weak void HAL_FMPI2C_ErrorCallback(FMPI2C_HandleTypeDef *hfmpi2c)
  2694. {
  2695. /* Prevent unused argument(s) compilation warning */
  2696. UNUSED(hfmpi2c);
  2697. /* NOTE : This function should not be modified, when the callback is needed,
  2698. the HAL_FMPI2C_ErrorCallback could be implemented in the user file
  2699. */
  2700. }
  2701. /**
  2702. * @brief FMPI2C abort callback.
  2703. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2704. * the configuration information for the specified FMPI2C.
  2705. * @retval None
  2706. */
  2707. __weak void HAL_FMPI2C_AbortCpltCallback(FMPI2C_HandleTypeDef *hfmpi2c)
  2708. {
  2709. /* Prevent unused argument(s) compilation warning */
  2710. UNUSED(hfmpi2c);
  2711. /* NOTE : This function should not be modified, when the callback is needed,
  2712. the HAL_FMPI2C_AbortCpltCallback could be implemented in the user file
  2713. */
  2714. }
  2715. /**
  2716. * @}
  2717. */
  2718. /** @defgroup FMPI2C_Exported_Functions_Group3 Peripheral State, Mode and Error functions
  2719. * @brief Peripheral State, Mode and Error functions
  2720. *
  2721. @verbatim
  2722. ===============================================================================
  2723. ##### Peripheral State, Mode and Error functions #####
  2724. ===============================================================================
  2725. [..]
  2726. This subsection permit to get in run-time the status of the peripheral
  2727. and the data flow.
  2728. @endverbatim
  2729. * @{
  2730. */
  2731. /**
  2732. * @brief Return the FMPI2C handle state.
  2733. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2734. * the configuration information for the specified FMPI2C.
  2735. * @retval HAL state
  2736. */
  2737. HAL_FMPI2C_StateTypeDef HAL_FMPI2C_GetState(FMPI2C_HandleTypeDef *hfmpi2c)
  2738. {
  2739. /* Return FMPI2C handle state */
  2740. return hfmpi2c->State;
  2741. }
  2742. /**
  2743. * @brief Returns the FMPI2C Master, Slave, Memory or no mode.
  2744. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2745. * the configuration information for FMPI2C module
  2746. * @retval HAL mode
  2747. */
  2748. HAL_FMPI2C_ModeTypeDef HAL_FMPI2C_GetMode(FMPI2C_HandleTypeDef *hfmpi2c)
  2749. {
  2750. return hfmpi2c->Mode;
  2751. }
  2752. /**
  2753. * @brief Return the FMPI2C error code.
  2754. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2755. * the configuration information for the specified FMPI2C.
  2756. * @retval FMPI2C Error Code
  2757. */
  2758. uint32_t HAL_FMPI2C_GetError(FMPI2C_HandleTypeDef *hfmpi2c)
  2759. {
  2760. return hfmpi2c->ErrorCode;
  2761. }
  2762. /**
  2763. * @}
  2764. */
  2765. /**
  2766. * @}
  2767. */
  2768. /** @addtogroup FMPI2C_Private_Functions
  2769. * @{
  2770. */
  2771. /**
  2772. * @brief Interrupt Sub-Routine which handle the Interrupt Flags Master Mode with Interrupt.
  2773. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2774. * the configuration information for the specified FMPI2C.
  2775. * @param ITFlags Interrupt flags to handle.
  2776. * @param ITSources Interrupt sources enabled.
  2777. * @retval HAL status
  2778. */
  2779. static HAL_StatusTypeDef FMPI2C_Master_ISR_IT(struct __FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags, uint32_t ITSources)
  2780. {
  2781. uint16_t devaddress = 0U;
  2782. /* Process Locked */
  2783. __HAL_LOCK(hfmpi2c);
  2784. if (((ITFlags & FMPI2C_FLAG_AF) != RESET) && ((ITSources & FMPI2C_IT_NACKI) != RESET))
  2785. {
  2786. /* Clear NACK Flag */
  2787. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_AF);
  2788. /* Set corresponding Error Code */
  2789. /* No need to generate STOP, it is automatically done */
  2790. /* Error callback will be send during stop flag treatment */
  2791. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_AF;
  2792. /* Flush TX register */
  2793. FMPI2C_Flush_TXDR(hfmpi2c);
  2794. }
  2795. else if (((ITFlags & FMPI2C_FLAG_RXNE) != RESET) && ((ITSources & FMPI2C_IT_RXI) != RESET))
  2796. {
  2797. /* Read data from RXDR */
  2798. (*hfmpi2c->pBuffPtr++) = hfmpi2c->Instance->RXDR;
  2799. hfmpi2c->XferSize--;
  2800. hfmpi2c->XferCount--;
  2801. }
  2802. else if (((ITFlags & FMPI2C_FLAG_TXIS) != RESET) && ((ITSources & FMPI2C_IT_TXI) != RESET))
  2803. {
  2804. /* Write data to TXDR */
  2805. hfmpi2c->Instance->TXDR = (*hfmpi2c->pBuffPtr++);
  2806. hfmpi2c->XferSize--;
  2807. hfmpi2c->XferCount--;
  2808. }
  2809. else if (((ITFlags & FMPI2C_FLAG_TCR) != RESET) && ((ITSources & FMPI2C_IT_TCI) != RESET))
  2810. {
  2811. if ((hfmpi2c->XferSize == 0U) && (hfmpi2c->XferCount != 0U))
  2812. {
  2813. devaddress = (hfmpi2c->Instance->CR2 & FMPI2C_CR2_SADD);
  2814. if (hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  2815. {
  2816. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  2817. FMPI2C_TransferConfig(hfmpi2c, devaddress, hfmpi2c->XferSize, FMPI2C_RELOAD_MODE, FMPI2C_NO_STARTSTOP);
  2818. }
  2819. else
  2820. {
  2821. hfmpi2c->XferSize = hfmpi2c->XferCount;
  2822. if (hfmpi2c->XferOptions != FMPI2C_NO_OPTION_FRAME)
  2823. {
  2824. FMPI2C_TransferConfig(hfmpi2c, devaddress, hfmpi2c->XferSize, hfmpi2c->XferOptions, FMPI2C_NO_STARTSTOP);
  2825. }
  2826. else
  2827. {
  2828. FMPI2C_TransferConfig(hfmpi2c, devaddress, hfmpi2c->XferSize, FMPI2C_AUTOEND_MODE, FMPI2C_NO_STARTSTOP);
  2829. }
  2830. }
  2831. }
  2832. else
  2833. {
  2834. /* Call TxCpltCallback() if no stop mode is set */
  2835. if (FMPI2C_GET_STOP_MODE(hfmpi2c) != FMPI2C_AUTOEND_MODE)
  2836. {
  2837. /* Call FMPI2C Master Sequential complete process */
  2838. FMPI2C_ITMasterSequentialCplt(hfmpi2c);
  2839. }
  2840. else
  2841. {
  2842. /* Wrong size Status regarding TCR flag event */
  2843. /* Call the corresponding callback to inform upper layer of End of Transfer */
  2844. FMPI2C_ITError(hfmpi2c, HAL_FMPI2C_ERROR_SIZE);
  2845. }
  2846. }
  2847. }
  2848. else if (((ITFlags & FMPI2C_FLAG_TC) != RESET) && ((ITSources & FMPI2C_IT_TCI) != RESET))
  2849. {
  2850. if (hfmpi2c->XferCount == 0U)
  2851. {
  2852. if (FMPI2C_GET_STOP_MODE(hfmpi2c) != FMPI2C_AUTOEND_MODE)
  2853. {
  2854. /* Generate a stop condition in case of no transfer option */
  2855. if (hfmpi2c->XferOptions == FMPI2C_NO_OPTION_FRAME)
  2856. {
  2857. /* Generate Stop */
  2858. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_STOP;
  2859. }
  2860. else
  2861. {
  2862. /* Call FMPI2C Master Sequential complete process */
  2863. FMPI2C_ITMasterSequentialCplt(hfmpi2c);
  2864. }
  2865. }
  2866. }
  2867. else
  2868. {
  2869. /* Wrong size Status regarding TC flag event */
  2870. /* Call the corresponding callback to inform upper layer of End of Transfer */
  2871. FMPI2C_ITError(hfmpi2c, HAL_FMPI2C_ERROR_SIZE);
  2872. }
  2873. }
  2874. if (((ITFlags & FMPI2C_FLAG_STOPF) != RESET) && ((ITSources & FMPI2C_IT_STOPI) != RESET))
  2875. {
  2876. /* Call FMPI2C Master complete process */
  2877. FMPI2C_ITMasterCplt(hfmpi2c, ITFlags);
  2878. }
  2879. /* Process Unlocked */
  2880. __HAL_UNLOCK(hfmpi2c);
  2881. return HAL_OK;
  2882. }
  2883. /**
  2884. * @brief Interrupt Sub-Routine which handle the Interrupt Flags Slave Mode with Interrupt.
  2885. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2886. * the configuration information for the specified FMPI2C.
  2887. * @param ITFlags Interrupt flags to handle.
  2888. * @param ITSources Interrupt sources enabled.
  2889. * @retval HAL status
  2890. */
  2891. static HAL_StatusTypeDef FMPI2C_Slave_ISR_IT(struct __FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags, uint32_t ITSources)
  2892. {
  2893. /* Process locked */
  2894. __HAL_LOCK(hfmpi2c);
  2895. if (((ITFlags & FMPI2C_FLAG_AF) != RESET) && ((ITSources & FMPI2C_IT_NACKI) != RESET))
  2896. {
  2897. /* Check that FMPI2C transfer finished */
  2898. /* if yes, normal use case, a NACK is sent by the MASTER when Transfer is finished */
  2899. /* Mean XferCount == 0*/
  2900. /* So clear Flag NACKF only */
  2901. if (hfmpi2c->XferCount == 0U)
  2902. {
  2903. if (((hfmpi2c->XferOptions == FMPI2C_FIRST_AND_LAST_FRAME) || (hfmpi2c->XferOptions == FMPI2C_LAST_FRAME)) && \
  2904. (hfmpi2c->State == HAL_FMPI2C_STATE_LISTEN))
  2905. {
  2906. /* Call FMPI2C Listen complete process */
  2907. FMPI2C_ITListenCplt(hfmpi2c, ITFlags);
  2908. }
  2909. else if ((hfmpi2c->XferOptions != FMPI2C_NO_OPTION_FRAME) && (hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_TX_LISTEN))
  2910. {
  2911. /* Clear NACK Flag */
  2912. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_AF);
  2913. /* Flush TX register */
  2914. FMPI2C_Flush_TXDR(hfmpi2c);
  2915. /* Last Byte is Transmitted */
  2916. /* Call FMPI2C Slave Sequential complete process */
  2917. FMPI2C_ITSlaveSequentialCplt(hfmpi2c);
  2918. }
  2919. else
  2920. {
  2921. /* Clear NACK Flag */
  2922. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_AF);
  2923. }
  2924. }
  2925. else
  2926. {
  2927. /* if no, error use case, a Non-Acknowledge of last Data is generated by the MASTER*/
  2928. /* Clear NACK Flag */
  2929. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_AF);
  2930. /* Set ErrorCode corresponding to a Non-Acknowledge */
  2931. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_AF;
  2932. }
  2933. }
  2934. else if (((ITFlags & FMPI2C_FLAG_RXNE) != RESET) && ((ITSources & FMPI2C_IT_RXI) != RESET))
  2935. {
  2936. if (hfmpi2c->XferCount > 0U)
  2937. {
  2938. /* Read data from RXDR */
  2939. (*hfmpi2c->pBuffPtr++) = hfmpi2c->Instance->RXDR;
  2940. hfmpi2c->XferSize--;
  2941. hfmpi2c->XferCount--;
  2942. }
  2943. if ((hfmpi2c->XferCount == 0U) && \
  2944. (hfmpi2c->XferOptions != FMPI2C_NO_OPTION_FRAME))
  2945. {
  2946. /* Call FMPI2C Slave Sequential complete process */
  2947. FMPI2C_ITSlaveSequentialCplt(hfmpi2c);
  2948. }
  2949. }
  2950. else if (((ITFlags & FMPI2C_FLAG_ADDR) != RESET) && ((ITSources & FMPI2C_IT_ADDRI) != RESET))
  2951. {
  2952. FMPI2C_ITAddrCplt(hfmpi2c, ITFlags);
  2953. }
  2954. else if (((ITFlags & FMPI2C_FLAG_TXIS) != RESET) && ((ITSources & FMPI2C_IT_TXI) != RESET))
  2955. {
  2956. /* Write data to TXDR only if XferCount not reach "0" */
  2957. /* A TXIS flag can be set, during STOP treatment */
  2958. /* Check if all Datas have already been sent */
  2959. /* If it is the case, this last write in TXDR is not sent, correspond to a dummy TXIS event */
  2960. if (hfmpi2c->XferCount > 0U)
  2961. {
  2962. /* Write data to TXDR */
  2963. hfmpi2c->Instance->TXDR = (*hfmpi2c->pBuffPtr++);
  2964. hfmpi2c->XferCount--;
  2965. hfmpi2c->XferSize--;
  2966. }
  2967. else
  2968. {
  2969. if ((hfmpi2c->XferOptions == FMPI2C_NEXT_FRAME) || (hfmpi2c->XferOptions == FMPI2C_FIRST_FRAME))
  2970. {
  2971. /* Last Byte is Transmitted */
  2972. /* Call FMPI2C Slave Sequential complete process */
  2973. FMPI2C_ITSlaveSequentialCplt(hfmpi2c);
  2974. }
  2975. }
  2976. }
  2977. /* Check if STOPF is set */
  2978. if (((ITFlags & FMPI2C_FLAG_STOPF) != RESET) && ((ITSources & FMPI2C_IT_STOPI) != RESET))
  2979. {
  2980. /* Call FMPI2C Slave complete process */
  2981. FMPI2C_ITSlaveCplt(hfmpi2c, ITFlags);
  2982. }
  2983. /* Process Unlocked */
  2984. __HAL_UNLOCK(hfmpi2c);
  2985. return HAL_OK;
  2986. }
  2987. /**
  2988. * @brief Interrupt Sub-Routine which handle the Interrupt Flags Master Mode with DMA.
  2989. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2990. * the configuration information for the specified FMPI2C.
  2991. * @param ITFlags Interrupt flags to handle.
  2992. * @param ITSources Interrupt sources enabled.
  2993. * @retval HAL status
  2994. */
  2995. static HAL_StatusTypeDef FMPI2C_Master_ISR_DMA(struct __FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags, uint32_t ITSources)
  2996. {
  2997. uint16_t devaddress = 0U;
  2998. uint32_t xfermode = 0U;
  2999. /* Process Locked */
  3000. __HAL_LOCK(hfmpi2c);
  3001. if (((ITFlags & FMPI2C_FLAG_AF) != RESET) && ((ITSources & FMPI2C_IT_NACKI) != RESET))
  3002. {
  3003. /* Clear NACK Flag */
  3004. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_AF);
  3005. /* Set corresponding Error Code */
  3006. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_AF;
  3007. /* No need to generate STOP, it is automatically done */
  3008. /* But enable STOP interrupt, to treat it */
  3009. /* Error callback will be send during stop flag treatment */
  3010. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_CPLT_IT);
  3011. /* Flush TX register */
  3012. FMPI2C_Flush_TXDR(hfmpi2c);
  3013. }
  3014. else if (((ITFlags & FMPI2C_FLAG_TCR) != RESET) && ((ITSources & FMPI2C_IT_TCI) != RESET))
  3015. {
  3016. /* Disable TC interrupt */
  3017. __HAL_FMPI2C_DISABLE_IT(hfmpi2c, FMPI2C_IT_TCI);
  3018. if (hfmpi2c->XferCount != 0U)
  3019. {
  3020. /* Recover Slave address */
  3021. devaddress = (hfmpi2c->Instance->CR2 & FMPI2C_CR2_SADD);
  3022. /* Prepare the new XferSize to transfer */
  3023. if (hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  3024. {
  3025. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  3026. xfermode = FMPI2C_RELOAD_MODE;
  3027. }
  3028. else
  3029. {
  3030. hfmpi2c->XferSize = hfmpi2c->XferCount;
  3031. xfermode = FMPI2C_AUTOEND_MODE;
  3032. }
  3033. /* Set the new XferSize in Nbytes register */
  3034. FMPI2C_TransferConfig(hfmpi2c, devaddress, hfmpi2c->XferSize, xfermode, FMPI2C_NO_STARTSTOP);
  3035. /* Update XferCount value */
  3036. hfmpi2c->XferCount -= hfmpi2c->XferSize;
  3037. /* Enable DMA Request */
  3038. if (hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_RX)
  3039. {
  3040. hfmpi2c->Instance->CR1 |= FMPI2C_CR1_RXDMAEN;
  3041. }
  3042. else
  3043. {
  3044. hfmpi2c->Instance->CR1 |= FMPI2C_CR1_TXDMAEN;
  3045. }
  3046. }
  3047. else
  3048. {
  3049. /* Wrong size Status regarding TCR flag event */
  3050. /* Call the corresponding callback to inform upper layer of End of Transfer */
  3051. FMPI2C_ITError(hfmpi2c, HAL_FMPI2C_ERROR_SIZE);
  3052. }
  3053. }
  3054. else if (((ITFlags & FMPI2C_FLAG_STOPF) != RESET) && ((ITSources & FMPI2C_IT_STOPI) != RESET))
  3055. {
  3056. /* Call FMPI2C Master complete process */
  3057. FMPI2C_ITMasterCplt(hfmpi2c, ITFlags);
  3058. }
  3059. /* Process Unlocked */
  3060. __HAL_UNLOCK(hfmpi2c);
  3061. return HAL_OK;
  3062. }
  3063. /**
  3064. * @brief Interrupt Sub-Routine which handle the Interrupt Flags Slave Mode with DMA.
  3065. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  3066. * the configuration information for the specified FMPI2C.
  3067. * @param ITFlags Interrupt flags to handle.
  3068. * @param ITSources Interrupt sources enabled.
  3069. * @retval HAL status
  3070. */
  3071. static HAL_StatusTypeDef FMPI2C_Slave_ISR_DMA(struct __FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags, uint32_t ITSources)
  3072. {
  3073. /* Process locked */
  3074. __HAL_LOCK(hfmpi2c);
  3075. if (((ITFlags & FMPI2C_FLAG_AF) != RESET) && ((ITSources & FMPI2C_IT_NACKI) != RESET))
  3076. {
  3077. /* Check that FMPI2C transfer finished */
  3078. /* if yes, normal use case, a NACK is sent by the MASTER when Transfer is finished */
  3079. /* Mean XferCount == 0 */
  3080. /* So clear Flag NACKF only */
  3081. if (FMPI2C_GET_DMA_REMAIN_DATA(hfmpi2c) == 0U)
  3082. {
  3083. /* Clear NACK Flag */
  3084. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_AF);
  3085. }
  3086. else
  3087. {
  3088. /* if no, error use case, a Non-Acknowledge of last Data is generated by the MASTER*/
  3089. /* Clear NACK Flag */
  3090. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_AF);
  3091. /* Set ErrorCode corresponding to a Non-Acknowledge */
  3092. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_AF;
  3093. }
  3094. }
  3095. else if (((ITFlags & FMPI2C_FLAG_ADDR) != RESET) && ((ITSources & FMPI2C_IT_ADDRI) != RESET))
  3096. {
  3097. /* Clear ADDR flag */
  3098. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_ADDR);
  3099. }
  3100. else if (((ITFlags & FMPI2C_FLAG_STOPF) != RESET) && ((ITSources & FMPI2C_IT_STOPI) != RESET))
  3101. {
  3102. /* Call FMPI2C Slave complete process */
  3103. FMPI2C_ITSlaveCplt(hfmpi2c, ITFlags);
  3104. }
  3105. /* Process Unlocked */
  3106. __HAL_UNLOCK(hfmpi2c);
  3107. return HAL_OK;
  3108. }
  3109. /**
  3110. * @brief Master sends target device address followed by internal memory address for write request.
  3111. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  3112. * the configuration information for the specified FMPI2C.
  3113. * @param DevAddress Target device address The device 7 bits address value
  3114. * in datasheet must be shifted to the left before calling the interface
  3115. * @param MemAddress Internal memory address
  3116. * @param MemAddSize Size of internal memory address
  3117. * @param Timeout Timeout duration
  3118. * @param Tickstart Tick start value
  3119. * @retval HAL status
  3120. */
  3121. static HAL_StatusTypeDef FMPI2C_RequestMemoryWrite(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart)
  3122. {
  3123. FMPI2C_TransferConfig(hfmpi2c, DevAddress, MemAddSize, FMPI2C_RELOAD_MODE, FMPI2C_GENERATE_START_WRITE);
  3124. /* Wait until TXIS flag is set */
  3125. if (FMPI2C_WaitOnTXISFlagUntilTimeout(hfmpi2c, Timeout, Tickstart) != HAL_OK)
  3126. {
  3127. if (hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  3128. {
  3129. return HAL_ERROR;
  3130. }
  3131. else
  3132. {
  3133. return HAL_TIMEOUT;
  3134. }
  3135. }
  3136. /* If Memory address size is 8Bit */
  3137. if (MemAddSize == FMPI2C_MEMADD_SIZE_8BIT)
  3138. {
  3139. /* Send Memory Address */
  3140. hfmpi2c->Instance->TXDR = FMPI2C_MEM_ADD_LSB(MemAddress);
  3141. }
  3142. /* If Memory address size is 16Bit */
  3143. else
  3144. {
  3145. /* Send MSB of Memory Address */
  3146. hfmpi2c->Instance->TXDR = FMPI2C_MEM_ADD_MSB(MemAddress);
  3147. /* Wait until TXIS flag is set */
  3148. if (FMPI2C_WaitOnTXISFlagUntilTimeout(hfmpi2c, Timeout, Tickstart) != HAL_OK)
  3149. {
  3150. if (hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  3151. {
  3152. return HAL_ERROR;
  3153. }
  3154. else
  3155. {
  3156. return HAL_TIMEOUT;
  3157. }
  3158. }
  3159. /* Send LSB of Memory Address */
  3160. hfmpi2c->Instance->TXDR = FMPI2C_MEM_ADD_LSB(MemAddress);
  3161. }
  3162. /* Wait until TCR flag is set */
  3163. if (FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_TCR, RESET, Timeout, Tickstart) != HAL_OK)
  3164. {
  3165. return HAL_TIMEOUT;
  3166. }
  3167. return HAL_OK;
  3168. }
  3169. /**
  3170. * @brief Master sends target device address followed by internal memory address for read request.
  3171. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  3172. * the configuration information for the specified FMPI2C.
  3173. * @param DevAddress Target device address The device 7 bits address value
  3174. * in datasheet must be shifted to the left before calling the interface
  3175. * @param MemAddress Internal memory address
  3176. * @param MemAddSize Size of internal memory address
  3177. * @param Timeout Timeout duration
  3178. * @param Tickstart Tick start value
  3179. * @retval HAL status
  3180. */
  3181. static HAL_StatusTypeDef FMPI2C_RequestMemoryRead(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart)
  3182. {
  3183. FMPI2C_TransferConfig(hfmpi2c, DevAddress, MemAddSize, FMPI2C_SOFTEND_MODE, FMPI2C_GENERATE_START_WRITE);
  3184. /* Wait until TXIS flag is set */
  3185. if (FMPI2C_WaitOnTXISFlagUntilTimeout(hfmpi2c, Timeout, Tickstart) != HAL_OK)
  3186. {
  3187. if (hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  3188. {
  3189. return HAL_ERROR;
  3190. }
  3191. else
  3192. {
  3193. return HAL_TIMEOUT;
  3194. }
  3195. }
  3196. /* If Memory address size is 8Bit */
  3197. if (MemAddSize == FMPI2C_MEMADD_SIZE_8BIT)
  3198. {
  3199. /* Send Memory Address */
  3200. hfmpi2c->Instance->TXDR = FMPI2C_MEM_ADD_LSB(MemAddress);
  3201. }
  3202. /* If Memory address size is 16Bit */
  3203. else
  3204. {
  3205. /* Send MSB of Memory Address */
  3206. hfmpi2c->Instance->TXDR = FMPI2C_MEM_ADD_MSB(MemAddress);
  3207. /* Wait until TXIS flag is set */
  3208. if (FMPI2C_WaitOnTXISFlagUntilTimeout(hfmpi2c, Timeout, Tickstart) != HAL_OK)
  3209. {
  3210. if (hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  3211. {
  3212. return HAL_ERROR;
  3213. }
  3214. else
  3215. {
  3216. return HAL_TIMEOUT;
  3217. }
  3218. }
  3219. /* Send LSB of Memory Address */
  3220. hfmpi2c->Instance->TXDR = FMPI2C_MEM_ADD_LSB(MemAddress);
  3221. }
  3222. /* Wait until TC flag is set */
  3223. if (FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_TC, RESET, Timeout, Tickstart) != HAL_OK)
  3224. {
  3225. return HAL_TIMEOUT;
  3226. }
  3227. return HAL_OK;
  3228. }
  3229. /**
  3230. * @brief FMPI2C Address complete process callback.
  3231. * @param hfmpi2c FMPI2C handle.
  3232. * @param ITFlags Interrupt flags to handle.
  3233. * @retval None
  3234. */
  3235. static void FMPI2C_ITAddrCplt(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags)
  3236. {
  3237. uint8_t transferdirection = 0;
  3238. uint16_t slaveaddrcode = 0;
  3239. uint16_t ownadd1code = 0;
  3240. uint16_t ownadd2code = 0;
  3241. /* Prevent unused argument(s) compilation warning */
  3242. UNUSED(ITFlags);
  3243. /* In case of Listen state, need to inform upper layer of address match code event */
  3244. if ((hfmpi2c->State & HAL_FMPI2C_STATE_LISTEN) == HAL_FMPI2C_STATE_LISTEN)
  3245. {
  3246. transferdirection = FMPI2C_GET_DIR(hfmpi2c);
  3247. slaveaddrcode = FMPI2C_GET_ADDR_MATCH(hfmpi2c);
  3248. ownadd1code = FMPI2C_GET_OWN_ADDRESS1(hfmpi2c);
  3249. ownadd2code = FMPI2C_GET_OWN_ADDRESS2(hfmpi2c);
  3250. /* If 10bits addressing mode is selected */
  3251. if (hfmpi2c->Init.AddressingMode == FMPI2C_ADDRESSINGMODE_10BIT)
  3252. {
  3253. if ((slaveaddrcode & SlaveAddr_MSK) == ((ownadd1code >> SlaveAddr_SHIFT) & SlaveAddr_MSK))
  3254. {
  3255. slaveaddrcode = ownadd1code;
  3256. hfmpi2c->AddrEventCount++;
  3257. if (hfmpi2c->AddrEventCount == 2U)
  3258. {
  3259. /* Reset Address Event counter */
  3260. hfmpi2c->AddrEventCount = 0U;
  3261. /* Clear ADDR flag */
  3262. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_ADDR);
  3263. /* Process Unlocked */
  3264. __HAL_UNLOCK(hfmpi2c);
  3265. /* Call Slave Addr callback */
  3266. HAL_FMPI2C_AddrCallback(hfmpi2c, transferdirection, slaveaddrcode);
  3267. }
  3268. }
  3269. else
  3270. {
  3271. slaveaddrcode = ownadd2code;
  3272. /* Disable ADDR Interrupts */
  3273. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_LISTEN_IT);
  3274. /* Process Unlocked */
  3275. __HAL_UNLOCK(hfmpi2c);
  3276. /* Call Slave Addr callback */
  3277. HAL_FMPI2C_AddrCallback(hfmpi2c, transferdirection, slaveaddrcode);
  3278. }
  3279. }
  3280. /* else 7 bits addressing mode is selected */
  3281. else
  3282. {
  3283. /* Disable ADDR Interrupts */
  3284. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_LISTEN_IT);
  3285. /* Process Unlocked */
  3286. __HAL_UNLOCK(hfmpi2c);
  3287. /* Call Slave Addr callback */
  3288. HAL_FMPI2C_AddrCallback(hfmpi2c, transferdirection, slaveaddrcode);
  3289. }
  3290. }
  3291. /* Else clear address flag only */
  3292. else
  3293. {
  3294. /* Clear ADDR flag */
  3295. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_ADDR);
  3296. /* Process Unlocked */
  3297. __HAL_UNLOCK(hfmpi2c);
  3298. }
  3299. }
  3300. /**
  3301. * @brief FMPI2C Master sequential complete process.
  3302. * @param hfmpi2c FMPI2C handle.
  3303. * @retval None
  3304. */
  3305. static void FMPI2C_ITMasterSequentialCplt(FMPI2C_HandleTypeDef *hfmpi2c)
  3306. {
  3307. /* Reset FMPI2C handle mode */
  3308. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  3309. /* No Generate Stop, to permit restart mode */
  3310. /* The stop will be done at the end of transfer, when FMPI2C_AUTOEND_MODE enable */
  3311. if (hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_TX)
  3312. {
  3313. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  3314. hfmpi2c->PreviousState = FMPI2C_STATE_MASTER_BUSY_TX;
  3315. hfmpi2c->XferISR = NULL;
  3316. /* Disable Interrupts */
  3317. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_TX_IT);
  3318. /* Process Unlocked */
  3319. __HAL_UNLOCK(hfmpi2c);
  3320. /* Call the corresponding callback to inform upper layer of End of Transfer */
  3321. HAL_FMPI2C_MasterTxCpltCallback(hfmpi2c);
  3322. }
  3323. /* hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_RX */
  3324. else
  3325. {
  3326. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  3327. hfmpi2c->PreviousState = FMPI2C_STATE_MASTER_BUSY_RX;
  3328. hfmpi2c->XferISR = NULL;
  3329. /* Disable Interrupts */
  3330. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_RX_IT);
  3331. /* Process Unlocked */
  3332. __HAL_UNLOCK(hfmpi2c);
  3333. /* Call the corresponding callback to inform upper layer of End of Transfer */
  3334. HAL_FMPI2C_MasterRxCpltCallback(hfmpi2c);
  3335. }
  3336. }
  3337. /**
  3338. * @brief FMPI2C Slave sequential complete process.
  3339. * @param hfmpi2c FMPI2C handle.
  3340. * @retval None
  3341. */
  3342. static void FMPI2C_ITSlaveSequentialCplt(FMPI2C_HandleTypeDef *hfmpi2c)
  3343. {
  3344. /* Reset FMPI2C handle mode */
  3345. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  3346. if (hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_TX_LISTEN)
  3347. {
  3348. /* Remove HAL_FMPI2C_STATE_SLAVE_BUSY_TX, keep only HAL_FMPI2C_STATE_LISTEN */
  3349. hfmpi2c->State = HAL_FMPI2C_STATE_LISTEN;
  3350. hfmpi2c->PreviousState = FMPI2C_STATE_SLAVE_BUSY_TX;
  3351. /* Disable Interrupts */
  3352. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_TX_IT);
  3353. /* Process Unlocked */
  3354. __HAL_UNLOCK(hfmpi2c);
  3355. /* Call the Tx complete callback to inform upper layer of the end of transmit process */
  3356. HAL_FMPI2C_SlaveTxCpltCallback(hfmpi2c);
  3357. }
  3358. else if (hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_RX_LISTEN)
  3359. {
  3360. /* Remove HAL_FMPI2C_STATE_SLAVE_BUSY_RX, keep only HAL_FMPI2C_STATE_LISTEN */
  3361. hfmpi2c->State = HAL_FMPI2C_STATE_LISTEN;
  3362. hfmpi2c->PreviousState = FMPI2C_STATE_SLAVE_BUSY_RX;
  3363. /* Disable Interrupts */
  3364. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_RX_IT);
  3365. /* Process Unlocked */
  3366. __HAL_UNLOCK(hfmpi2c);
  3367. /* Call the Rx complete callback to inform upper layer of the end of receive process */
  3368. HAL_FMPI2C_SlaveRxCpltCallback(hfmpi2c);
  3369. }
  3370. }
  3371. /**
  3372. * @brief FMPI2C Master complete process.
  3373. * @param hfmpi2c FMPI2C handle.
  3374. * @param ITFlags Interrupt flags to handle.
  3375. * @retval None
  3376. */
  3377. static void FMPI2C_ITMasterCplt(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags)
  3378. {
  3379. /* Clear STOP Flag */
  3380. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  3381. /* Clear Configuration Register 2 */
  3382. FMPI2C_RESET_CR2(hfmpi2c);
  3383. /* Reset handle parameters */
  3384. hfmpi2c->PreviousState = FMPI2C_STATE_NONE;
  3385. hfmpi2c->XferISR = NULL;
  3386. hfmpi2c->XferOptions = FMPI2C_NO_OPTION_FRAME;
  3387. if ((ITFlags & FMPI2C_FLAG_AF) != RESET)
  3388. {
  3389. /* Clear NACK Flag */
  3390. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_AF);
  3391. /* Set acknowledge error code */
  3392. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_AF;
  3393. }
  3394. /* Flush TX register */
  3395. FMPI2C_Flush_TXDR(hfmpi2c);
  3396. /* Disable Interrupts */
  3397. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_TX_IT | FMPI2C_XFER_RX_IT);
  3398. /* Call the corresponding callback to inform upper layer of End of Transfer */
  3399. if ((hfmpi2c->ErrorCode != HAL_FMPI2C_ERROR_NONE) || (hfmpi2c->State == HAL_FMPI2C_STATE_ABORT))
  3400. {
  3401. /* Call the corresponding callback to inform upper layer of End of Transfer */
  3402. FMPI2C_ITError(hfmpi2c, hfmpi2c->ErrorCode);
  3403. }
  3404. /* hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_TX */
  3405. else if (hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_TX)
  3406. {
  3407. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  3408. if (hfmpi2c->Mode == HAL_FMPI2C_MODE_MEM)
  3409. {
  3410. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  3411. /* Process Unlocked */
  3412. __HAL_UNLOCK(hfmpi2c);
  3413. /* Call the corresponding callback to inform upper layer of End of Transfer */
  3414. HAL_FMPI2C_MemTxCpltCallback(hfmpi2c);
  3415. }
  3416. else
  3417. {
  3418. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  3419. /* Process Unlocked */
  3420. __HAL_UNLOCK(hfmpi2c);
  3421. /* Call the corresponding callback to inform upper layer of End of Transfer */
  3422. HAL_FMPI2C_MasterTxCpltCallback(hfmpi2c);
  3423. }
  3424. }
  3425. /* hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_RX */
  3426. else if (hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_RX)
  3427. {
  3428. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  3429. if (hfmpi2c->Mode == HAL_FMPI2C_MODE_MEM)
  3430. {
  3431. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  3432. /* Process Unlocked */
  3433. __HAL_UNLOCK(hfmpi2c);
  3434. HAL_FMPI2C_MemRxCpltCallback(hfmpi2c);
  3435. }
  3436. else
  3437. {
  3438. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  3439. /* Process Unlocked */
  3440. __HAL_UNLOCK(hfmpi2c);
  3441. HAL_FMPI2C_MasterRxCpltCallback(hfmpi2c);
  3442. }
  3443. }
  3444. }
  3445. /**
  3446. * @brief FMPI2C Slave complete process.
  3447. * @param hfmpi2c FMPI2C handle.
  3448. * @param ITFlags Interrupt flags to handle.
  3449. * @retval None
  3450. */
  3451. static void FMPI2C_ITSlaveCplt(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags)
  3452. {
  3453. /* Clear STOP Flag */
  3454. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  3455. /* Clear ADDR flag */
  3456. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_ADDR);
  3457. /* Disable all interrupts */
  3458. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_LISTEN_IT | FMPI2C_XFER_TX_IT | FMPI2C_XFER_RX_IT);
  3459. /* Disable Address Acknowledge */
  3460. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  3461. /* Clear Configuration Register 2 */
  3462. FMPI2C_RESET_CR2(hfmpi2c);
  3463. /* Flush TX register */
  3464. FMPI2C_Flush_TXDR(hfmpi2c);
  3465. /* If a DMA is ongoing, Update handle size context */
  3466. if (((hfmpi2c->Instance->CR1 & FMPI2C_CR1_TXDMAEN) == FMPI2C_CR1_TXDMAEN) ||
  3467. ((hfmpi2c->Instance->CR1 & FMPI2C_CR1_RXDMAEN) == FMPI2C_CR1_RXDMAEN))
  3468. {
  3469. hfmpi2c->XferCount = FMPI2C_GET_DMA_REMAIN_DATA(hfmpi2c);
  3470. }
  3471. /* All data are not transferred, so set error code accordingly */
  3472. if (hfmpi2c->XferCount != 0U)
  3473. {
  3474. /* Set ErrorCode corresponding to a Non-Acknowledge */
  3475. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_AF;
  3476. }
  3477. /* Store Last receive data if any */
  3478. if (((ITFlags & FMPI2C_FLAG_RXNE) != RESET))
  3479. {
  3480. /* Read data from RXDR */
  3481. (*hfmpi2c->pBuffPtr++) = hfmpi2c->Instance->RXDR;
  3482. if ((hfmpi2c->XferSize > 0U))
  3483. {
  3484. hfmpi2c->XferSize--;
  3485. hfmpi2c->XferCount--;
  3486. /* Set ErrorCode corresponding to a Non-Acknowledge */
  3487. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_AF;
  3488. }
  3489. }
  3490. hfmpi2c->PreviousState = FMPI2C_STATE_NONE;
  3491. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  3492. hfmpi2c->XferISR = NULL;
  3493. if (hfmpi2c->ErrorCode != HAL_FMPI2C_ERROR_NONE)
  3494. {
  3495. /* Call the corresponding callback to inform upper layer of End of Transfer */
  3496. FMPI2C_ITError(hfmpi2c, hfmpi2c->ErrorCode);
  3497. /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */
  3498. if (hfmpi2c->State == HAL_FMPI2C_STATE_LISTEN)
  3499. {
  3500. /* Call FMPI2C Listen complete process */
  3501. FMPI2C_ITListenCplt(hfmpi2c, ITFlags);
  3502. }
  3503. }
  3504. else if (hfmpi2c->XferOptions != FMPI2C_NO_OPTION_FRAME)
  3505. {
  3506. hfmpi2c->XferOptions = FMPI2C_NO_OPTION_FRAME;
  3507. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  3508. /* Process Unlocked */
  3509. __HAL_UNLOCK(hfmpi2c);
  3510. /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */
  3511. HAL_FMPI2C_ListenCpltCallback(hfmpi2c);
  3512. }
  3513. /* Call the corresponding callback to inform upper layer of End of Transfer */
  3514. else if (hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_RX)
  3515. {
  3516. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  3517. /* Process Unlocked */
  3518. __HAL_UNLOCK(hfmpi2c);
  3519. /* Call the Slave Rx Complete callback */
  3520. HAL_FMPI2C_SlaveRxCpltCallback(hfmpi2c);
  3521. }
  3522. else
  3523. {
  3524. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  3525. /* Process Unlocked */
  3526. __HAL_UNLOCK(hfmpi2c);
  3527. /* Call the Slave Tx Complete callback */
  3528. HAL_FMPI2C_SlaveTxCpltCallback(hfmpi2c);
  3529. }
  3530. }
  3531. /**
  3532. * @brief FMPI2C Listen complete process.
  3533. * @param hfmpi2c FMPI2C handle.
  3534. * @param ITFlags Interrupt flags to handle.
  3535. * @retval None
  3536. */
  3537. static void FMPI2C_ITListenCplt(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags)
  3538. {
  3539. /* Reset handle parameters */
  3540. hfmpi2c->XferOptions = FMPI2C_NO_OPTION_FRAME;
  3541. hfmpi2c->PreviousState = FMPI2C_STATE_NONE;
  3542. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  3543. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  3544. hfmpi2c->XferISR = NULL;
  3545. /* Store Last receive data if any */
  3546. if (((ITFlags & FMPI2C_FLAG_RXNE) != RESET))
  3547. {
  3548. /* Read data from RXDR */
  3549. (*hfmpi2c->pBuffPtr++) = hfmpi2c->Instance->RXDR;
  3550. if ((hfmpi2c->XferSize > 0U))
  3551. {
  3552. hfmpi2c->XferSize--;
  3553. hfmpi2c->XferCount--;
  3554. /* Set ErrorCode corresponding to a Non-Acknowledge */
  3555. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_AF;
  3556. }
  3557. }
  3558. /* Disable all Interrupts*/
  3559. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_LISTEN_IT | FMPI2C_XFER_RX_IT | FMPI2C_XFER_TX_IT);
  3560. /* Clear NACK Flag */
  3561. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_AF);
  3562. /* Process Unlocked */
  3563. __HAL_UNLOCK(hfmpi2c);
  3564. /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */
  3565. HAL_FMPI2C_ListenCpltCallback(hfmpi2c);
  3566. }
  3567. /**
  3568. * @brief FMPI2C interrupts error process.
  3569. * @param hfmpi2c FMPI2C handle.
  3570. * @param ErrorCode Error code to handle.
  3571. * @retval None
  3572. */
  3573. static void FMPI2C_ITError(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ErrorCode)
  3574. {
  3575. /* Reset handle parameters */
  3576. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  3577. hfmpi2c->XferOptions = FMPI2C_NO_OPTION_FRAME;
  3578. hfmpi2c->XferCount = 0U;
  3579. /* Set new error code */
  3580. hfmpi2c->ErrorCode |= ErrorCode;
  3581. /* Disable Interrupts */
  3582. if ((hfmpi2c->State == HAL_FMPI2C_STATE_LISTEN) ||
  3583. (hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_TX_LISTEN) ||
  3584. (hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_RX_LISTEN))
  3585. {
  3586. /* Disable all interrupts, except interrupts related to LISTEN state */
  3587. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_RX_IT | FMPI2C_XFER_TX_IT);
  3588. /* keep HAL_FMPI2C_STATE_LISTEN if set */
  3589. hfmpi2c->State = HAL_FMPI2C_STATE_LISTEN;
  3590. hfmpi2c->PreviousState = FMPI2C_STATE_NONE;
  3591. hfmpi2c->XferISR = FMPI2C_Slave_ISR_IT;
  3592. }
  3593. else
  3594. {
  3595. /* Disable all interrupts */
  3596. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_LISTEN_IT | FMPI2C_XFER_RX_IT | FMPI2C_XFER_TX_IT);
  3597. /* If state is an abort treatment on goind, don't change state */
  3598. /* This change will be do later */
  3599. if (hfmpi2c->State != HAL_FMPI2C_STATE_ABORT)
  3600. {
  3601. /* Set HAL_FMPI2C_STATE_READY */
  3602. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  3603. }
  3604. hfmpi2c->PreviousState = FMPI2C_STATE_NONE;
  3605. hfmpi2c->XferISR = NULL;
  3606. }
  3607. /* Abort DMA TX transfer if any */
  3608. if ((hfmpi2c->Instance->CR1 & FMPI2C_CR1_TXDMAEN) == FMPI2C_CR1_TXDMAEN)
  3609. {
  3610. hfmpi2c->Instance->CR1 &= ~FMPI2C_CR1_TXDMAEN;
  3611. /* Set the FMPI2C DMA Abort callback :
  3612. will lead to call HAL_FMPI2C_ErrorCallback() at end of DMA abort procedure */
  3613. hfmpi2c->hdmatx->XferAbortCallback = FMPI2C_DMAAbort;
  3614. /* Process Unlocked */
  3615. __HAL_UNLOCK(hfmpi2c);
  3616. /* Abort DMA TX */
  3617. if (HAL_DMA_Abort_IT(hfmpi2c->hdmatx) != HAL_OK)
  3618. {
  3619. /* Call Directly XferAbortCallback function in case of error */
  3620. hfmpi2c->hdmatx->XferAbortCallback(hfmpi2c->hdmatx);
  3621. }
  3622. }
  3623. /* Abort DMA RX transfer if any */
  3624. else if ((hfmpi2c->Instance->CR1 & FMPI2C_CR1_RXDMAEN) == FMPI2C_CR1_RXDMAEN)
  3625. {
  3626. hfmpi2c->Instance->CR1 &= ~FMPI2C_CR1_RXDMAEN;
  3627. /* Set the FMPI2C DMA Abort callback :
  3628. will lead to call HAL_FMPI2C_ErrorCallback() at end of DMA abort procedure */
  3629. hfmpi2c->hdmarx->XferAbortCallback = FMPI2C_DMAAbort;
  3630. /* Process Unlocked */
  3631. __HAL_UNLOCK(hfmpi2c);
  3632. /* Abort DMA RX */
  3633. if (HAL_DMA_Abort_IT(hfmpi2c->hdmarx) != HAL_OK)
  3634. {
  3635. /* Call Directly hfmpi2c->hdmarx->XferAbortCallback function in case of error */
  3636. hfmpi2c->hdmarx->XferAbortCallback(hfmpi2c->hdmarx);
  3637. }
  3638. }
  3639. else if (hfmpi2c->State == HAL_FMPI2C_STATE_ABORT)
  3640. {
  3641. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  3642. /* Process Unlocked */
  3643. __HAL_UNLOCK(hfmpi2c);
  3644. /* Call the corresponding callback to inform upper layer of End of Transfer */
  3645. HAL_FMPI2C_AbortCpltCallback(hfmpi2c);
  3646. }
  3647. else
  3648. {
  3649. /* Process Unlocked */
  3650. __HAL_UNLOCK(hfmpi2c);
  3651. /* Call the corresponding callback to inform upper layer of End of Transfer */
  3652. HAL_FMPI2C_ErrorCallback(hfmpi2c);
  3653. }
  3654. }
  3655. /**
  3656. * @brief FMPI2C Tx data register flush process.
  3657. * @param hfmpi2c FMPI2C handle.
  3658. * @retval None
  3659. */
  3660. static void FMPI2C_Flush_TXDR(FMPI2C_HandleTypeDef *hfmpi2c)
  3661. {
  3662. /* If a pending TXIS flag is set */
  3663. /* Write a dummy data in TXDR to clear it */
  3664. if (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_TXIS) != RESET)
  3665. {
  3666. hfmpi2c->Instance->TXDR = 0x00U;
  3667. }
  3668. /* Flush TX register if not empty */
  3669. if (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_TXE) == RESET)
  3670. {
  3671. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_TXE);
  3672. }
  3673. }
  3674. /**
  3675. * @brief DMA FMPI2C master transmit process complete callback.
  3676. * @param hdma DMA handle
  3677. * @retval None
  3678. */
  3679. static void FMPI2C_DMAMasterTransmitCplt(DMA_HandleTypeDef *hdma)
  3680. {
  3681. FMPI2C_HandleTypeDef *hfmpi2c = (FMPI2C_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  3682. /* Disable DMA Request */
  3683. hfmpi2c->Instance->CR1 &= ~FMPI2C_CR1_TXDMAEN;
  3684. /* If last transfer, enable STOP interrupt */
  3685. if (hfmpi2c->XferCount == 0U)
  3686. {
  3687. /* Enable STOP interrupt */
  3688. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_CPLT_IT);
  3689. }
  3690. /* else prepare a new DMA transfer and enable TCReload interrupt */
  3691. else
  3692. {
  3693. /* Update Buffer pointer */
  3694. hfmpi2c->pBuffPtr += hfmpi2c->XferSize;
  3695. /* Set the XferSize to transfer */
  3696. if (hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  3697. {
  3698. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  3699. }
  3700. else
  3701. {
  3702. hfmpi2c->XferSize = hfmpi2c->XferCount;
  3703. }
  3704. /* Enable the DMA stream */
  3705. HAL_DMA_Start_IT(hfmpi2c->hdmatx, (uint32_t)hfmpi2c->pBuffPtr, (uint32_t)&hfmpi2c->Instance->TXDR, hfmpi2c->XferSize);
  3706. /* Enable TC interrupts */
  3707. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_RELOAD_IT);
  3708. }
  3709. }
  3710. /**
  3711. * @brief DMA FMPI2C slave transmit process complete callback.
  3712. * @param hdma DMA handle
  3713. * @retval None
  3714. */
  3715. static void FMPI2C_DMASlaveTransmitCplt(DMA_HandleTypeDef *hdma)
  3716. {
  3717. /* Prevent unused argument(s) compilation warning */
  3718. UNUSED(hdma);
  3719. /* No specific action, Master fully manage the generation of STOP condition */
  3720. /* Mean that this generation can arrive at any time, at the end or during DMA process */
  3721. /* So STOP condition should be manage through Interrupt treatment */
  3722. }
  3723. /**
  3724. * @brief DMA FMPI2C master receive process complete callback.
  3725. * @param hdma DMA handle
  3726. * @retval None
  3727. */
  3728. static void FMPI2C_DMAMasterReceiveCplt(DMA_HandleTypeDef *hdma)
  3729. {
  3730. FMPI2C_HandleTypeDef *hfmpi2c = (FMPI2C_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  3731. /* Disable DMA Request */
  3732. hfmpi2c->Instance->CR1 &= ~FMPI2C_CR1_RXDMAEN;
  3733. /* If last transfer, enable STOP interrupt */
  3734. if (hfmpi2c->XferCount == 0U)
  3735. {
  3736. /* Enable STOP interrupt */
  3737. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_CPLT_IT);
  3738. }
  3739. /* else prepare a new DMA transfer and enable TCReload interrupt */
  3740. else
  3741. {
  3742. /* Update Buffer pointer */
  3743. hfmpi2c->pBuffPtr += hfmpi2c->XferSize;
  3744. /* Set the XferSize to transfer */
  3745. if (hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  3746. {
  3747. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  3748. }
  3749. else
  3750. {
  3751. hfmpi2c->XferSize = hfmpi2c->XferCount;
  3752. }
  3753. /* Enable the DMA stream */
  3754. HAL_DMA_Start_IT(hfmpi2c->hdmarx, (uint32_t)&hfmpi2c->Instance->RXDR, (uint32_t)hfmpi2c->pBuffPtr, hfmpi2c->XferSize);
  3755. /* Enable TC interrupts */
  3756. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_RELOAD_IT);
  3757. }
  3758. }
  3759. /**
  3760. * @brief DMA FMPI2C slave receive process complete callback.
  3761. * @param hdma DMA handle
  3762. * @retval None
  3763. */
  3764. static void FMPI2C_DMASlaveReceiveCplt(DMA_HandleTypeDef *hdma)
  3765. {
  3766. /* Prevent unused argument(s) compilation warning */
  3767. UNUSED(hdma);
  3768. /* No specific action, Master fully manage the generation of STOP condition */
  3769. /* Mean that this generation can arrive at any time, at the end or during DMA process */
  3770. /* So STOP condition should be manage through Interrupt treatment */
  3771. }
  3772. /**
  3773. * @brief DMA FMPI2C communication error callback.
  3774. * @param hdma DMA handle
  3775. * @retval None
  3776. */
  3777. static void FMPI2C_DMAError(DMA_HandleTypeDef *hdma)
  3778. {
  3779. FMPI2C_HandleTypeDef *hfmpi2c = (FMPI2C_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  3780. /* Disable Acknowledge */
  3781. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  3782. /* Call the corresponding callback to inform upper layer of End of Transfer */
  3783. FMPI2C_ITError(hfmpi2c, HAL_FMPI2C_ERROR_DMA);
  3784. }
  3785. /**
  3786. * @brief DMA FMPI2C communication abort callback
  3787. * (To be called at end of DMA Abort procedure).
  3788. * @param hdma DMA handle.
  3789. * @retval None
  3790. */
  3791. static void FMPI2C_DMAAbort(DMA_HandleTypeDef *hdma)
  3792. {
  3793. FMPI2C_HandleTypeDef *hfmpi2c = (FMPI2C_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  3794. /* Disable Acknowledge */
  3795. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  3796. /* Reset AbortCpltCallback */
  3797. hfmpi2c->hdmatx->XferAbortCallback = NULL;
  3798. hfmpi2c->hdmarx->XferAbortCallback = NULL;
  3799. /* Check if come from abort from user */
  3800. if (hfmpi2c->State == HAL_FMPI2C_STATE_ABORT)
  3801. {
  3802. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  3803. /* Call the corresponding callback to inform upper layer of End of Transfer */
  3804. HAL_FMPI2C_AbortCpltCallback(hfmpi2c);
  3805. }
  3806. else
  3807. {
  3808. /* Call the corresponding callback to inform upper layer of End of Transfer */
  3809. HAL_FMPI2C_ErrorCallback(hfmpi2c);
  3810. }
  3811. }
  3812. /**
  3813. * @brief This function handles FMPI2C Communication Timeout.
  3814. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  3815. * the configuration information for the specified FMPI2C.
  3816. * @param Flag Specifies the FMPI2C flag to check.
  3817. * @param Status The new Flag status (SET or RESET).
  3818. * @param Timeout Timeout duration
  3819. * @param Tickstart Tick start value
  3820. * @retval HAL status
  3821. */
  3822. static HAL_StatusTypeDef FMPI2C_WaitOnFlagUntilTimeout(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t Flag, FlagStatus Status, uint32_t Timeout, uint32_t Tickstart)
  3823. {
  3824. while (__HAL_FMPI2C_GET_FLAG(hfmpi2c, Flag) == Status)
  3825. {
  3826. /* Check for the Timeout */
  3827. if (Timeout != HAL_MAX_DELAY)
  3828. {
  3829. if ((Timeout == 0U) || ((HAL_GetTick() - Tickstart) > Timeout))
  3830. {
  3831. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  3832. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  3833. /* Process Unlocked */
  3834. __HAL_UNLOCK(hfmpi2c);
  3835. return HAL_TIMEOUT;
  3836. }
  3837. }
  3838. }
  3839. return HAL_OK;
  3840. }
  3841. /**
  3842. * @brief This function handles FMPI2C Communication Timeout for specific usage of TXIS flag.
  3843. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  3844. * the configuration information for the specified FMPI2C.
  3845. * @param Timeout Timeout duration
  3846. * @param Tickstart Tick start value
  3847. * @retval HAL status
  3848. */
  3849. static HAL_StatusTypeDef FMPI2C_WaitOnTXISFlagUntilTimeout(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t Timeout, uint32_t Tickstart)
  3850. {
  3851. while (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_TXIS) == RESET)
  3852. {
  3853. /* Check if a NACK is detected */
  3854. if (FMPI2C_IsAcknowledgeFailed(hfmpi2c, Timeout, Tickstart) != HAL_OK)
  3855. {
  3856. return HAL_ERROR;
  3857. }
  3858. /* Check for the Timeout */
  3859. if (Timeout != HAL_MAX_DELAY)
  3860. {
  3861. if ((Timeout == 0U) || ((HAL_GetTick() - Tickstart) > Timeout))
  3862. {
  3863. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_TIMEOUT;
  3864. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  3865. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  3866. /* Process Unlocked */
  3867. __HAL_UNLOCK(hfmpi2c);
  3868. return HAL_TIMEOUT;
  3869. }
  3870. }
  3871. }
  3872. return HAL_OK;
  3873. }
  3874. /**
  3875. * @brief This function handles FMPI2C Communication Timeout for specific usage of STOP flag.
  3876. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  3877. * the configuration information for the specified FMPI2C.
  3878. * @param Timeout Timeout duration
  3879. * @param Tickstart Tick start value
  3880. * @retval HAL status
  3881. */
  3882. static HAL_StatusTypeDef FMPI2C_WaitOnSTOPFlagUntilTimeout(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t Timeout, uint32_t Tickstart)
  3883. {
  3884. while (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF) == RESET)
  3885. {
  3886. /* Check if a NACK is detected */
  3887. if (FMPI2C_IsAcknowledgeFailed(hfmpi2c, Timeout, Tickstart) != HAL_OK)
  3888. {
  3889. return HAL_ERROR;
  3890. }
  3891. /* Check for the Timeout */
  3892. if ((Timeout == 0U) || ((HAL_GetTick() - Tickstart) > Timeout))
  3893. {
  3894. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_TIMEOUT;
  3895. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  3896. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  3897. /* Process Unlocked */
  3898. __HAL_UNLOCK(hfmpi2c);
  3899. return HAL_TIMEOUT;
  3900. }
  3901. }
  3902. return HAL_OK;
  3903. }
  3904. /**
  3905. * @brief This function handles FMPI2C Communication Timeout for specific usage of RXNE flag.
  3906. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  3907. * the configuration information for the specified FMPI2C.
  3908. * @param Timeout Timeout duration
  3909. * @param Tickstart Tick start value
  3910. * @retval HAL status
  3911. */
  3912. static HAL_StatusTypeDef FMPI2C_WaitOnRXNEFlagUntilTimeout(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t Timeout, uint32_t Tickstart)
  3913. {
  3914. while (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_RXNE) == RESET)
  3915. {
  3916. /* Check if a NACK is detected */
  3917. if (FMPI2C_IsAcknowledgeFailed(hfmpi2c, Timeout, Tickstart) != HAL_OK)
  3918. {
  3919. return HAL_ERROR;
  3920. }
  3921. /* Check if a STOPF is detected */
  3922. if (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF) == SET)
  3923. {
  3924. /* Check if an RXNE is pending */
  3925. /* Store Last receive data if any */
  3926. if ((__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_RXNE) == SET) && (hfmpi2c->XferSize > 0U))
  3927. {
  3928. /* Return HAL_OK */
  3929. /* The Reading of data from RXDR will be done in caller function */
  3930. return HAL_OK;
  3931. }
  3932. else
  3933. {
  3934. /* Clear STOP Flag */
  3935. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  3936. /* Clear Configuration Register 2 */
  3937. FMPI2C_RESET_CR2(hfmpi2c);
  3938. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  3939. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  3940. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  3941. /* Process Unlocked */
  3942. __HAL_UNLOCK(hfmpi2c);
  3943. return HAL_ERROR;
  3944. }
  3945. }
  3946. /* Check for the Timeout */
  3947. if ((Timeout == 0U) || ((HAL_GetTick() - Tickstart) > Timeout))
  3948. {
  3949. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_TIMEOUT;
  3950. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  3951. /* Process Unlocked */
  3952. __HAL_UNLOCK(hfmpi2c);
  3953. return HAL_TIMEOUT;
  3954. }
  3955. }
  3956. return HAL_OK;
  3957. }
  3958. /**
  3959. * @brief This function handles Acknowledge failed detection during an FMPI2C Communication.
  3960. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  3961. * the configuration information for the specified FMPI2C.
  3962. * @param Timeout Timeout duration
  3963. * @param Tickstart Tick start value
  3964. * @retval HAL status
  3965. */
  3966. static HAL_StatusTypeDef FMPI2C_IsAcknowledgeFailed(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t Timeout, uint32_t Tickstart)
  3967. {
  3968. if (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_AF) == SET)
  3969. {
  3970. /* Wait until STOP Flag is reset */
  3971. /* AutoEnd should be initiate after AF */
  3972. while (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF) == RESET)
  3973. {
  3974. /* Check for the Timeout */
  3975. if (Timeout != HAL_MAX_DELAY)
  3976. {
  3977. if ((Timeout == 0U) || ((HAL_GetTick() - Tickstart) > Timeout))
  3978. {
  3979. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  3980. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  3981. /* Process Unlocked */
  3982. __HAL_UNLOCK(hfmpi2c);
  3983. return HAL_TIMEOUT;
  3984. }
  3985. }
  3986. }
  3987. /* Clear NACKF Flag */
  3988. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_AF);
  3989. /* Clear STOP Flag */
  3990. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  3991. /* Flush TX register */
  3992. FMPI2C_Flush_TXDR(hfmpi2c);
  3993. /* Clear Configuration Register 2 */
  3994. FMPI2C_RESET_CR2(hfmpi2c);
  3995. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_AF;
  3996. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  3997. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  3998. /* Process Unlocked */
  3999. __HAL_UNLOCK(hfmpi2c);
  4000. return HAL_ERROR;
  4001. }
  4002. return HAL_OK;
  4003. }
  4004. /**
  4005. * @brief Handles FMPI2Cx communication when starting transfer or during transfer (TC or TCR flag are set).
  4006. * @param hfmpi2c FMPI2C handle.
  4007. * @param DevAddress Specifies the slave address to be programmed.
  4008. * @param Size Specifies the number of bytes to be programmed.
  4009. * This parameter must be a value between 0 and 255.
  4010. * @param Mode New state of the FMPI2C START condition generation.
  4011. * This parameter can be one of the following values:
  4012. * @arg @ref FMPI2C_RELOAD_MODE Enable Reload mode .
  4013. * @arg @ref FMPI2C_AUTOEND_MODE Enable Automatic end mode.
  4014. * @arg @ref FMPI2C_SOFTEND_MODE Enable Software end mode.
  4015. * @param Request New state of the FMPI2C START condition generation.
  4016. * This parameter can be one of the following values:
  4017. * @arg @ref FMPI2C_NO_STARTSTOP Don't Generate stop and start condition.
  4018. * @arg @ref FMPI2C_GENERATE_STOP Generate stop condition (Size should be set to 0).
  4019. * @arg @ref FMPI2C_GENERATE_START_READ Generate Restart for read request.
  4020. * @arg @ref FMPI2C_GENERATE_START_WRITE Generate Restart for write request.
  4021. * @retval None
  4022. */
  4023. static void FMPI2C_TransferConfig(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint8_t Size, uint32_t Mode, uint32_t Request)
  4024. {
  4025. /* Check the parameters */
  4026. assert_param(IS_FMPI2C_ALL_INSTANCE(hfmpi2c->Instance));
  4027. assert_param(IS_TRANSFER_MODE(Mode));
  4028. assert_param(IS_TRANSFER_REQUEST(Request));
  4029. /* update CR2 register */
  4030. MODIFY_REG(hfmpi2c->Instance->CR2, ((FMPI2C_CR2_SADD | FMPI2C_CR2_NBYTES | FMPI2C_CR2_RELOAD | FMPI2C_CR2_AUTOEND | (FMPI2C_CR2_RD_WRN & (uint32_t)(Request >> (31U - FMPI2C_CR2_RD_WRN_Pos))) | FMPI2C_CR2_START | FMPI2C_CR2_STOP)), \
  4031. (uint32_t)(((uint32_t)DevAddress & FMPI2C_CR2_SADD) | (((uint32_t)Size << FMPI2C_CR2_NBYTES_Pos) & FMPI2C_CR2_NBYTES) | (uint32_t)Mode | (uint32_t)Request));
  4032. }
  4033. /**
  4034. * @brief Manage the enabling of Interrupts.
  4035. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  4036. * the configuration information for the specified FMPI2C.
  4037. * @param InterruptRequest Value of @ref FMPI2C_Interrupt_configuration_definition.
  4038. * @retval HAL status
  4039. */
  4040. static HAL_StatusTypeDef FMPI2C_Enable_IRQ(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t InterruptRequest)
  4041. {
  4042. uint32_t tmpisr = 0U;
  4043. if ((hfmpi2c->XferISR == FMPI2C_Master_ISR_DMA) || \
  4044. (hfmpi2c->XferISR == FMPI2C_Slave_ISR_DMA))
  4045. {
  4046. if ((InterruptRequest & FMPI2C_XFER_LISTEN_IT) == FMPI2C_XFER_LISTEN_IT)
  4047. {
  4048. /* Enable ERR, STOP, NACK and ADDR interrupts */
  4049. tmpisr |= FMPI2C_IT_ADDRI | FMPI2C_IT_STOPI | FMPI2C_IT_NACKI | FMPI2C_IT_ERRI;
  4050. }
  4051. if ((InterruptRequest & FMPI2C_XFER_ERROR_IT) == FMPI2C_XFER_ERROR_IT)
  4052. {
  4053. /* Enable ERR and NACK interrupts */
  4054. tmpisr |= FMPI2C_IT_ERRI | FMPI2C_IT_NACKI;
  4055. }
  4056. if ((InterruptRequest & FMPI2C_XFER_CPLT_IT) == FMPI2C_XFER_CPLT_IT)
  4057. {
  4058. /* Enable STOP interrupts */
  4059. tmpisr |= FMPI2C_IT_STOPI;
  4060. }
  4061. if ((InterruptRequest & FMPI2C_XFER_RELOAD_IT) == FMPI2C_XFER_RELOAD_IT)
  4062. {
  4063. /* Enable TC interrupts */
  4064. tmpisr |= FMPI2C_IT_TCI;
  4065. }
  4066. }
  4067. else
  4068. {
  4069. if ((InterruptRequest & FMPI2C_XFER_LISTEN_IT) == FMPI2C_XFER_LISTEN_IT)
  4070. {
  4071. /* Enable ERR, STOP, NACK, and ADDR interrupts */
  4072. tmpisr |= FMPI2C_IT_ADDRI | FMPI2C_IT_STOPI | FMPI2C_IT_NACKI | FMPI2C_IT_ERRI;
  4073. }
  4074. if ((InterruptRequest & FMPI2C_XFER_TX_IT) == FMPI2C_XFER_TX_IT)
  4075. {
  4076. /* Enable ERR, TC, STOP, NACK and RXI interrupts */
  4077. tmpisr |= FMPI2C_IT_ERRI | FMPI2C_IT_TCI | FMPI2C_IT_STOPI | FMPI2C_IT_NACKI | FMPI2C_IT_TXI;
  4078. }
  4079. if ((InterruptRequest & FMPI2C_XFER_RX_IT) == FMPI2C_XFER_RX_IT)
  4080. {
  4081. /* Enable ERR, TC, STOP, NACK and TXI interrupts */
  4082. tmpisr |= FMPI2C_IT_ERRI | FMPI2C_IT_TCI | FMPI2C_IT_STOPI | FMPI2C_IT_NACKI | FMPI2C_IT_RXI;
  4083. }
  4084. if ((InterruptRequest & FMPI2C_XFER_CPLT_IT) == FMPI2C_XFER_CPLT_IT)
  4085. {
  4086. /* Enable STOP interrupts */
  4087. tmpisr |= FMPI2C_IT_STOPI;
  4088. }
  4089. }
  4090. /* Enable interrupts only at the end */
  4091. /* to avoid the risk of FMPI2C interrupt handle execution before */
  4092. /* all interrupts requested done */
  4093. __HAL_FMPI2C_ENABLE_IT(hfmpi2c, tmpisr);
  4094. return HAL_OK;
  4095. }
  4096. /**
  4097. * @brief Manage the disabling of Interrupts.
  4098. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  4099. * the configuration information for the specified FMPI2C.
  4100. * @param InterruptRequest Value of @ref FMPI2C_Interrupt_configuration_definition.
  4101. * @retval HAL status
  4102. */
  4103. static HAL_StatusTypeDef FMPI2C_Disable_IRQ(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t InterruptRequest)
  4104. {
  4105. uint32_t tmpisr = 0U;
  4106. if ((InterruptRequest & FMPI2C_XFER_TX_IT) == FMPI2C_XFER_TX_IT)
  4107. {
  4108. /* Disable TC and TXI interrupts */
  4109. tmpisr |= FMPI2C_IT_TCI | FMPI2C_IT_TXI;
  4110. if ((hfmpi2c->State & HAL_FMPI2C_STATE_LISTEN) != HAL_FMPI2C_STATE_LISTEN)
  4111. {
  4112. /* Disable NACK and STOP interrupts */
  4113. tmpisr |= FMPI2C_IT_STOPI | FMPI2C_IT_NACKI | FMPI2C_IT_ERRI;
  4114. }
  4115. }
  4116. if ((InterruptRequest & FMPI2C_XFER_RX_IT) == FMPI2C_XFER_RX_IT)
  4117. {
  4118. /* Disable TC and RXI interrupts */
  4119. tmpisr |= FMPI2C_IT_TCI | FMPI2C_IT_RXI;
  4120. if ((hfmpi2c->State & HAL_FMPI2C_STATE_LISTEN) != HAL_FMPI2C_STATE_LISTEN)
  4121. {
  4122. /* Disable NACK and STOP interrupts */
  4123. tmpisr |= FMPI2C_IT_STOPI | FMPI2C_IT_NACKI | FMPI2C_IT_ERRI;
  4124. }
  4125. }
  4126. if ((InterruptRequest & FMPI2C_XFER_LISTEN_IT) == FMPI2C_XFER_LISTEN_IT)
  4127. {
  4128. /* Disable ADDR, NACK and STOP interrupts */
  4129. tmpisr |= FMPI2C_IT_ADDRI | FMPI2C_IT_STOPI | FMPI2C_IT_NACKI | FMPI2C_IT_ERRI;
  4130. }
  4131. if ((InterruptRequest & FMPI2C_XFER_ERROR_IT) == FMPI2C_XFER_ERROR_IT)
  4132. {
  4133. /* Enable ERR and NACK interrupts */
  4134. tmpisr |= FMPI2C_IT_ERRI | FMPI2C_IT_NACKI;
  4135. }
  4136. if ((InterruptRequest & FMPI2C_XFER_CPLT_IT) == FMPI2C_XFER_CPLT_IT)
  4137. {
  4138. /* Enable STOP interrupts */
  4139. tmpisr |= FMPI2C_IT_STOPI;
  4140. }
  4141. if ((InterruptRequest & FMPI2C_XFER_RELOAD_IT) == FMPI2C_XFER_RELOAD_IT)
  4142. {
  4143. /* Enable TC interrupts */
  4144. tmpisr |= FMPI2C_IT_TCI;
  4145. }
  4146. /* Disable interrupts only at the end */
  4147. /* to avoid a breaking situation like at "t" time */
  4148. /* all disable interrupts request are not done */
  4149. __HAL_FMPI2C_DISABLE_IT(hfmpi2c, tmpisr);
  4150. return HAL_OK;
  4151. }
  4152. /**
  4153. * @}
  4154. */
  4155. #endif /* STM32F410xx || STM32F446xx || STM32F412Zx || STM32F412Vx || STM32F412Rx || STM32F412Cx || STM32F413xx || STM32F423xx */
  4156. #endif /* HAL_FMPI2C_MODULE_ENABLED */
  4157. /**
  4158. * @}
  4159. */
  4160. /**
  4161. * @}
  4162. */
  4163. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/