stm32f4xx_hal_fmpi2c.c 171 KB

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