stm32f4xx_hal_fmpi2c.c 227 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669
  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 @ref 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 @ref HAL_FMPI2C_Init(), configures also the low level Hardware
  39. (GPIO, CLOCK, NVIC...etc) by calling the customized @ref HAL_FMPI2C_MspInit(&hfmpi2c) API.
  40. (#) To check if target device is ready for communication, use the function @ref 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 @ref HAL_FMPI2C_Master_Transmit()
  46. (+) Receive in master mode an amount of data in blocking mode using @ref HAL_FMPI2C_Master_Receive()
  47. (+) Transmit in slave mode an amount of data in blocking mode using @ref HAL_FMPI2C_Slave_Transmit()
  48. (+) Receive in slave mode an amount of data in blocking mode using @ref 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 @ref HAL_FMPI2C_Mem_Write()
  53. (+) Read an amount of data in blocking mode from a specific memory address using @ref 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 @ref HAL_FMPI2C_Master_Transmit_IT()
  58. (+) At transmission end of transfer, @ref HAL_FMPI2C_MasterTxCpltCallback() is executed and user can
  59. add his own code by customization of function pointer @ref HAL_FMPI2C_MasterTxCpltCallback()
  60. (+) Receive in master mode an amount of data in non-blocking mode using @ref HAL_FMPI2C_Master_Receive_IT()
  61. (+) At reception end of transfer, @ref HAL_FMPI2C_MasterRxCpltCallback() is executed and user can
  62. add his own code by customization of function pointer @ref HAL_FMPI2C_MasterRxCpltCallback()
  63. (+) Transmit in slave mode an amount of data in non-blocking mode using @ref HAL_FMPI2C_Slave_Transmit_IT()
  64. (+) At transmission end of transfer, @ref HAL_FMPI2C_SlaveTxCpltCallback() is executed and user can
  65. add his own code by customization of function pointer @ref HAL_FMPI2C_SlaveTxCpltCallback()
  66. (+) Receive in slave mode an amount of data in non-blocking mode using @ref HAL_FMPI2C_Slave_Receive_IT()
  67. (+) At reception end of transfer, @ref HAL_FMPI2C_SlaveRxCpltCallback() is executed and user can
  68. add his own code by customization of function pointer @ref HAL_FMPI2C_SlaveRxCpltCallback()
  69. (+) In case of transfer Error, @ref HAL_FMPI2C_ErrorCallback() function is executed and user can
  70. add his own code by customization of function pointer @ref HAL_FMPI2C_ErrorCallback()
  71. (+) Abort a master FMPI2C process communication with Interrupt using @ref HAL_FMPI2C_Master_Abort_IT()
  72. (+) End of abort process, @ref HAL_FMPI2C_AbortCpltCallback() is executed and user can
  73. add his own code by customization of function pointer @ref HAL_FMPI2C_AbortCpltCallback()
  74. (+) Discard a slave FMPI2C process communication using @ref __HAL_FMPI2C_GENERATE_NACK() macro.
  75. This action will inform Master to generate a Stop condition to discard the communication.
  76. *** Interrupt mode or DMA 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, functional 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 @ref HAL_FMPI2C_Master_Seq_Transmit_IT() then @ref HAL_FMPI2C_Master_Seq_Transmit_IT()
  90. or @ref HAL_FMPI2C_Master_Seq_Transmit_DMA() then @ref HAL_FMPI2C_Master_Seq_Transmit_DMA())
  91. (++) FMPI2C_NEXT_FRAME: Sequential usage, this option allow to manage a sequence with a restart condition, address
  92. and with new data to transfer if the direction change or manage only the new data to transfer
  93. if no direction change and without a final stop condition in both cases
  94. (++) FMPI2C_LAST_FRAME: Sequential usage, this option allow to manage a sequance with a restart condition, address
  95. and with new data to transfer if the direction change or manage only the new data to transfer
  96. if no direction change and with a final stop condition in both cases
  97. (++) 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
  98. interface several times (link with option FMPI2C_FIRST_AND_NEXT_FRAME).
  99. Usage can, transfer several bytes one by one using HAL_FMPI2C_Master_Seq_Transmit_IT(option FMPI2C_FIRST_AND_NEXT_FRAME then FMPI2C_NEXT_FRAME)
  100. or HAL_FMPI2C_Master_Seq_Receive_IT(option FMPI2C_FIRST_AND_NEXT_FRAME then FMPI2C_NEXT_FRAME)
  101. or HAL_FMPI2C_Master_Seq_Transmit_DMA(option FMPI2C_FIRST_AND_NEXT_FRAME then FMPI2C_NEXT_FRAME)
  102. or HAL_FMPI2C_Master_Seq_Receive_DMA(option FMPI2C_FIRST_AND_NEXT_FRAME then FMPI2C_NEXT_FRAME).
  103. Then usage of this option FMPI2C_LAST_FRAME_NO_STOP at the last Transmit or Receive sequence permit to call the opposite interface Receive or Transmit
  104. without stopping the communication and so generate a restart condition.
  105. (++) FMPI2C_OTHER_FRAME: Sequential usage (Master only), this option allow to manage a restart condition after each call of the same master sequential
  106. interface.
  107. Usage can, transfer several bytes one by one with a restart with slave address between each bytes using HAL_FMPI2C_Master_Seq_Transmit_IT(option FMPI2C_FIRST_FRAME then FMPI2C_OTHER_FRAME)
  108. or HAL_FMPI2C_Master_Seq_Receive_IT(option FMPI2C_FIRST_FRAME then FMPI2C_OTHER_FRAME)
  109. or HAL_FMPI2C_Master_Seq_Transmit_DMA(option FMPI2C_FIRST_FRAME then FMPI2C_OTHER_FRAME)
  110. or HAL_FMPI2C_Master_Seq_Receive_DMA(option FMPI2C_FIRST_FRAME then FMPI2C_OTHER_FRAME).
  111. Then usage of this option FMPI2C_OTHER_AND_LAST_FRAME at the last frame to help automatic generation of STOP condition.
  112. (+) Different sequential FMPI2C interfaces are listed below:
  113. (++) Sequential transmit in master FMPI2C mode an amount of data in non-blocking mode using @ref HAL_FMPI2C_Master_Seq_Transmit_IT()
  114. or using @ref HAL_FMPI2C_Master_Seq_Transmit_DMA()
  115. (+++) At transmission end of current frame transfer, @ref HAL_FMPI2C_MasterTxCpltCallback() is executed and user can
  116. add his own code by customization of function pointer @ref HAL_FMPI2C_MasterTxCpltCallback()
  117. (++) Sequential receive in master FMPI2C mode an amount of data in non-blocking mode using @ref HAL_FMPI2C_Master_Seq_Receive_IT()
  118. or using @ref HAL_FMPI2C_Master_Seq_Receive_DMA()
  119. (+++) At reception end of current frame transfer, @ref HAL_FMPI2C_MasterRxCpltCallback() is executed and user can
  120. add his own code by customization of function pointer @ref HAL_FMPI2C_MasterRxCpltCallback()
  121. (++) Abort a master IT or DMA FMPI2C process communication with Interrupt using @ref HAL_FMPI2C_Master_Abort_IT()
  122. (+++) End of abort process, @ref HAL_FMPI2C_AbortCpltCallback() is executed and user can
  123. add his own code by customization of function pointer @ref HAL_FMPI2C_AbortCpltCallback()
  124. (++) Enable/disable the Address listen mode in slave FMPI2C mode using @ref HAL_FMPI2C_EnableListen_IT() @ref HAL_FMPI2C_DisableListen_IT()
  125. (+++) When address slave FMPI2C match, @ref HAL_FMPI2C_AddrCallback() is executed and user can
  126. add his own code to check the Address Match Code and the transmission direction request by master (Write/Read).
  127. (+++) At Listen mode end @ref HAL_FMPI2C_ListenCpltCallback() is executed and user can
  128. add his own code by customization of function pointer @ref HAL_FMPI2C_ListenCpltCallback()
  129. (++) Sequential transmit in slave FMPI2C mode an amount of data in non-blocking mode using @ref HAL_FMPI2C_Slave_Seq_Transmit_IT()
  130. or using @ref HAL_FMPI2C_Slave_Seq_Transmit_DMA()
  131. (+++) At transmission end of current frame transfer, @ref HAL_FMPI2C_SlaveTxCpltCallback() is executed and user can
  132. add his own code by customization of function pointer @ref HAL_FMPI2C_SlaveTxCpltCallback()
  133. (++) Sequential receive in slave FMPI2C mode an amount of data in non-blocking mode using @ref HAL_FMPI2C_Slave_Seq_Receive_IT()
  134. or using @ref HAL_FMPI2C_Slave_Seq_Receive_DMA()
  135. (+++) At reception end of current frame transfer, @ref HAL_FMPI2C_SlaveRxCpltCallback() is executed and user can
  136. add his own code by customization of function pointer @ref HAL_FMPI2C_SlaveRxCpltCallback()
  137. (++) In case of transfer Error, @ref HAL_FMPI2C_ErrorCallback() function is executed and user can
  138. add his own code by customization of function pointer @ref HAL_FMPI2C_ErrorCallback()
  139. (++) Discard a slave FMPI2C process communication using @ref __HAL_FMPI2C_GENERATE_NACK() macro.
  140. This action will inform Master to generate a Stop condition to discard the communication.
  141. *** Interrupt mode IO MEM operation ***
  142. =======================================
  143. [..]
  144. (+) Write an amount of data in non-blocking mode with Interrupt to a specific memory address using
  145. @ref HAL_FMPI2C_Mem_Write_IT()
  146. (+) At Memory end of write transfer, @ref HAL_FMPI2C_MemTxCpltCallback() is executed and user can
  147. add his own code by customization of function pointer @ref HAL_FMPI2C_MemTxCpltCallback()
  148. (+) Read an amount of data in non-blocking mode with Interrupt from a specific memory address using
  149. @ref HAL_FMPI2C_Mem_Read_IT()
  150. (+) At Memory end of read transfer, @ref HAL_FMPI2C_MemRxCpltCallback() is executed and user can
  151. add his own code by customization of function pointer @ref HAL_FMPI2C_MemRxCpltCallback()
  152. (+) In case of transfer Error, @ref HAL_FMPI2C_ErrorCallback() function is executed and user can
  153. add his own code by customization of function pointer @ref HAL_FMPI2C_ErrorCallback()
  154. *** DMA mode IO operation ***
  155. ==============================
  156. [..]
  157. (+) Transmit in master mode an amount of data in non-blocking mode (DMA) using
  158. @ref HAL_FMPI2C_Master_Transmit_DMA()
  159. (+) At transmission end of transfer, @ref HAL_FMPI2C_MasterTxCpltCallback() is executed and user can
  160. add his own code by customization of function pointer @ref HAL_FMPI2C_MasterTxCpltCallback()
  161. (+) Receive in master mode an amount of data in non-blocking mode (DMA) using
  162. @ref HAL_FMPI2C_Master_Receive_DMA()
  163. (+) At reception end of transfer, @ref HAL_FMPI2C_MasterRxCpltCallback() is executed and user can
  164. add his own code by customization of function pointer @ref HAL_FMPI2C_MasterRxCpltCallback()
  165. (+) Transmit in slave mode an amount of data in non-blocking mode (DMA) using
  166. @ref HAL_FMPI2C_Slave_Transmit_DMA()
  167. (+) At transmission end of transfer, @ref HAL_FMPI2C_SlaveTxCpltCallback() is executed and user can
  168. add his own code by customization of function pointer @ref HAL_FMPI2C_SlaveTxCpltCallback()
  169. (+) Receive in slave mode an amount of data in non-blocking mode (DMA) using
  170. @ref HAL_FMPI2C_Slave_Receive_DMA()
  171. (+) At reception end of transfer, @ref HAL_FMPI2C_SlaveRxCpltCallback() is executed and user can
  172. add his own code by customization of function pointer @ref HAL_FMPI2C_SlaveRxCpltCallback()
  173. (+) In case of transfer Error, @ref HAL_FMPI2C_ErrorCallback() function is executed and user can
  174. add his own code by customization of function pointer @ref HAL_FMPI2C_ErrorCallback()
  175. (+) Abort a master FMPI2C process communication with Interrupt using @ref HAL_FMPI2C_Master_Abort_IT()
  176. (+) End of abort process, @ref HAL_FMPI2C_AbortCpltCallback() is executed and user can
  177. add his own code by customization of function pointer @ref HAL_FMPI2C_AbortCpltCallback()
  178. (+) Discard a slave FMPI2C process communication using @ref __HAL_FMPI2C_GENERATE_NACK() macro.
  179. This action will inform Master to generate a Stop condition to discard the communication.
  180. *** DMA mode IO MEM operation ***
  181. =================================
  182. [..]
  183. (+) Write an amount of data in non-blocking mode with DMA to a specific memory address using
  184. @ref HAL_FMPI2C_Mem_Write_DMA()
  185. (+) At Memory end of write transfer, @ref HAL_FMPI2C_MemTxCpltCallback() is executed and user can
  186. add his own code by customization of function pointer @ref HAL_FMPI2C_MemTxCpltCallback()
  187. (+) Read an amount of data in non-blocking mode with DMA from a specific memory address using
  188. @ref HAL_FMPI2C_Mem_Read_DMA()
  189. (+) At Memory end of read transfer, @ref HAL_FMPI2C_MemRxCpltCallback() is executed and user can
  190. add his own code by customization of function pointer @ref HAL_FMPI2C_MemRxCpltCallback()
  191. (+) In case of transfer Error, @ref HAL_FMPI2C_ErrorCallback() function is executed and user can
  192. add his own code by customization of function pointer @ref HAL_FMPI2C_ErrorCallback()
  193. *** FMPI2C HAL driver macros list ***
  194. ==================================
  195. [..]
  196. Below the list of most used macros in FMPI2C HAL driver.
  197. (+) @ref __HAL_FMPI2C_ENABLE: Enable the FMPI2C peripheral
  198. (+) @ref __HAL_FMPI2C_DISABLE: Disable the FMPI2C peripheral
  199. (+) @ref __HAL_FMPI2C_GENERATE_NACK: Generate a Non-Acknowledge FMPI2C peripheral in Slave mode
  200. (+) @ref __HAL_FMPI2C_GET_FLAG: Check whether the specified FMPI2C flag is set or not
  201. (+) @ref __HAL_FMPI2C_CLEAR_FLAG: Clear the specified FMPI2C pending flag
  202. (+) @ref __HAL_FMPI2C_ENABLE_IT: Enable the specified FMPI2C interrupt
  203. (+) @ref __HAL_FMPI2C_DISABLE_IT: Disable the specified FMPI2C interrupt
  204. *** Callback registration ***
  205. =============================================
  206. [..]
  207. The compilation flag USE_HAL_FMPI2C_REGISTER_CALLBACKS when set to 1
  208. allows the user to configure dynamically the driver callbacks.
  209. Use Functions @ref HAL_FMPI2C_RegisterCallback() or @ref HAL_FMPI2C_RegisterAddrCallback()
  210. to register an interrupt callback.
  211. [..]
  212. Function @ref HAL_FMPI2C_RegisterCallback() allows to register following callbacks:
  213. (+) MasterTxCpltCallback : callback for Master transmission end of transfer.
  214. (+) MasterRxCpltCallback : callback for Master reception end of transfer.
  215. (+) SlaveTxCpltCallback : callback for Slave transmission end of transfer.
  216. (+) SlaveRxCpltCallback : callback for Slave reception end of transfer.
  217. (+) ListenCpltCallback : callback for end of listen mode.
  218. (+) MemTxCpltCallback : callback for Memory transmission end of transfer.
  219. (+) MemRxCpltCallback : callback for Memory reception end of transfer.
  220. (+) ErrorCallback : callback for error detection.
  221. (+) AbortCpltCallback : callback for abort completion process.
  222. (+) MspInitCallback : callback for Msp Init.
  223. (+) MspDeInitCallback : callback for Msp DeInit.
  224. This function takes as parameters the HAL peripheral handle, the Callback ID
  225. and a pointer to the user callback function.
  226. [..]
  227. For specific callback AddrCallback use dedicated register callbacks : @ref HAL_FMPI2C_RegisterAddrCallback().
  228. [..]
  229. Use function @ref HAL_FMPI2C_UnRegisterCallback to reset a callback to the default
  230. weak function.
  231. @ref HAL_FMPI2C_UnRegisterCallback takes as parameters the HAL peripheral handle,
  232. and the Callback ID.
  233. This function allows to reset following callbacks:
  234. (+) MasterTxCpltCallback : callback for Master transmission end of transfer.
  235. (+) MasterRxCpltCallback : callback for Master reception end of transfer.
  236. (+) SlaveTxCpltCallback : callback for Slave transmission end of transfer.
  237. (+) SlaveRxCpltCallback : callback for Slave reception end of transfer.
  238. (+) ListenCpltCallback : callback for end of listen mode.
  239. (+) MemTxCpltCallback : callback for Memory transmission end of transfer.
  240. (+) MemRxCpltCallback : callback for Memory reception end of transfer.
  241. (+) ErrorCallback : callback for error detection.
  242. (+) AbortCpltCallback : callback for abort completion process.
  243. (+) MspInitCallback : callback for Msp Init.
  244. (+) MspDeInitCallback : callback for Msp DeInit.
  245. [..]
  246. For callback AddrCallback use dedicated register callbacks : @ref HAL_FMPI2C_UnRegisterAddrCallback().
  247. [..]
  248. By default, after the @ref HAL_FMPI2C_Init() and when the state is @ref HAL_FMPI2C_STATE_RESET
  249. all callbacks are set to the corresponding weak functions:
  250. examples @ref HAL_FMPI2C_MasterTxCpltCallback(), @ref HAL_FMPI2C_MasterRxCpltCallback().
  251. Exception done for MspInit and MspDeInit functions that are
  252. reset to the legacy weak functions in the @ref HAL_FMPI2C_Init()/ @ref HAL_FMPI2C_DeInit() only when
  253. these callbacks are null (not registered beforehand).
  254. If MspInit or MspDeInit are not null, the @ref HAL_FMPI2C_Init()/ @ref HAL_FMPI2C_DeInit()
  255. keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
  256. [..]
  257. Callbacks can be registered/unregistered in @ref HAL_FMPI2C_STATE_READY state only.
  258. Exception done MspInit/MspDeInit functions that can be registered/unregistered
  259. in @ref HAL_FMPI2C_STATE_READY or @ref HAL_FMPI2C_STATE_RESET state,
  260. thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
  261. Then, the user first registers the MspInit/MspDeInit user callbacks
  262. using @ref HAL_FMPI2C_RegisterCallback() before calling @ref HAL_FMPI2C_DeInit()
  263. or @ref HAL_FMPI2C_Init() function.
  264. [..]
  265. When the compilation flag USE_HAL_FMPI2C_REGISTER_CALLBACKS is set to 0 or
  266. not defined, the callback registration feature is not available and all callbacks
  267. are set to the corresponding weak functions.
  268. [..]
  269. (@) You can refer to the FMPI2C HAL driver header file for more useful macros
  270. @endverbatim
  271. ******************************************************************************
  272. * @attention
  273. *
  274. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  275. * All rights reserved.</center></h2>
  276. *
  277. * This software component is licensed by ST under BSD 3-Clause license,
  278. * the "License"; You may not use this file except in compliance with the
  279. * License. You may obtain a copy of the License at:
  280. * opensource.org/licenses/BSD-3-Clause
  281. *
  282. ******************************************************************************
  283. */
  284. /* Includes ------------------------------------------------------------------*/
  285. #include "stm32f4xx_hal.h"
  286. /** @addtogroup STM32F4xx_HAL_Driver
  287. * @{
  288. */
  289. /** @defgroup FMPI2C FMPI2C
  290. * @brief FMPI2C HAL module driver
  291. * @{
  292. */
  293. #ifdef HAL_FMPI2C_MODULE_ENABLED
  294. #if defined(FMPI2C_CR1_PE)
  295. /* Private typedef -----------------------------------------------------------*/
  296. /* Private define ------------------------------------------------------------*/
  297. /** @defgroup FMPI2C_Private_Define FMPI2C Private Define
  298. * @{
  299. */
  300. #define TIMING_CLEAR_MASK (0xF0FFFFFFU) /*!< FMPI2C TIMING clear register Mask */
  301. #define FMPI2C_TIMEOUT_ADDR (10000U) /*!< 10 s */
  302. #define FMPI2C_TIMEOUT_BUSY (25U) /*!< 25 ms */
  303. #define FMPI2C_TIMEOUT_DIR (25U) /*!< 25 ms */
  304. #define FMPI2C_TIMEOUT_RXNE (25U) /*!< 25 ms */
  305. #define FMPI2C_TIMEOUT_STOPF (25U) /*!< 25 ms */
  306. #define FMPI2C_TIMEOUT_TC (25U) /*!< 25 ms */
  307. #define FMPI2C_TIMEOUT_TCR (25U) /*!< 25 ms */
  308. #define FMPI2C_TIMEOUT_TXIS (25U) /*!< 25 ms */
  309. #define FMPI2C_TIMEOUT_FLAG (25U) /*!< 25 ms */
  310. #define MAX_NBYTE_SIZE 255U
  311. #define SlaveAddr_SHIFT 7U
  312. #define SlaveAddr_MSK 0x06U
  313. /* Private define for @ref PreviousState usage */
  314. #define FMPI2C_STATE_MSK ((uint32_t)((uint32_t)((uint32_t)HAL_FMPI2C_STATE_BUSY_TX | (uint32_t)HAL_FMPI2C_STATE_BUSY_RX) & (uint32_t)(~((uint32_t)HAL_FMPI2C_STATE_READY)))) /*!< Mask State define, keep only RX and TX bits */
  315. #define FMPI2C_STATE_NONE ((uint32_t)(HAL_FMPI2C_MODE_NONE)) /*!< Default Value */
  316. #define FMPI2C_STATE_MASTER_BUSY_TX ((uint32_t)(((uint32_t)HAL_FMPI2C_STATE_BUSY_TX & FMPI2C_STATE_MSK) | (uint32_t)HAL_FMPI2C_MODE_MASTER)) /*!< Master Busy TX, combinaison of State LSB and Mode enum */
  317. #define FMPI2C_STATE_MASTER_BUSY_RX ((uint32_t)(((uint32_t)HAL_FMPI2C_STATE_BUSY_RX & FMPI2C_STATE_MSK) | (uint32_t)HAL_FMPI2C_MODE_MASTER)) /*!< Master Busy RX, combinaison of State LSB and Mode enum */
  318. #define FMPI2C_STATE_SLAVE_BUSY_TX ((uint32_t)(((uint32_t)HAL_FMPI2C_STATE_BUSY_TX & FMPI2C_STATE_MSK) | (uint32_t)HAL_FMPI2C_MODE_SLAVE)) /*!< Slave Busy TX, combinaison of State LSB and Mode enum */
  319. #define FMPI2C_STATE_SLAVE_BUSY_RX ((uint32_t)(((uint32_t)HAL_FMPI2C_STATE_BUSY_RX & FMPI2C_STATE_MSK) | (uint32_t)HAL_FMPI2C_MODE_SLAVE)) /*!< Slave Busy RX, combinaison of State LSB and Mode enum */
  320. #define FMPI2C_STATE_MEM_BUSY_TX ((uint32_t)(((uint32_t)HAL_FMPI2C_STATE_BUSY_TX & FMPI2C_STATE_MSK) | (uint32_t)HAL_FMPI2C_MODE_MEM)) /*!< Memory Busy TX, combinaison of State LSB and Mode enum */
  321. #define FMPI2C_STATE_MEM_BUSY_RX ((uint32_t)(((uint32_t)HAL_FMPI2C_STATE_BUSY_RX & FMPI2C_STATE_MSK) | (uint32_t)HAL_FMPI2C_MODE_MEM)) /*!< Memory Busy RX, combinaison of State LSB and Mode enum */
  322. /* Private define to centralize the enable/disable of Interrupts */
  323. #define FMPI2C_XFER_TX_IT (uint16_t)(0x0001U) /* Bit field can be combinated with @ref FMPI2C_XFER_LISTEN_IT */
  324. #define FMPI2C_XFER_RX_IT (uint16_t)(0x0002U) /* Bit field can be combinated with @ref FMPI2C_XFER_LISTEN_IT */
  325. #define FMPI2C_XFER_LISTEN_IT (uint16_t)(0x8000U) /* Bit field can be combinated with @ref FMPI2C_XFER_TX_IT and @ref FMPI2C_XFER_RX_IT */
  326. #define FMPI2C_XFER_ERROR_IT (uint16_t)(0x0010U) /* Bit definition to manage addition of global Error and NACK treatment */
  327. #define FMPI2C_XFER_CPLT_IT (uint16_t)(0x0020U) /* Bit definition to manage only STOP evenement */
  328. #define FMPI2C_XFER_RELOAD_IT (uint16_t)(0x0040U) /* Bit definition to manage only Reload of NBYTE */
  329. /* Private define Sequential Transfer Options default/reset value */
  330. #define FMPI2C_NO_OPTION_FRAME (0xFFFF0000U)
  331. /**
  332. * @}
  333. */
  334. /* Private macro -------------------------------------------------------------*/
  335. /* Private variables ---------------------------------------------------------*/
  336. /* Private function prototypes -----------------------------------------------*/
  337. /** @defgroup FMPI2C_Private_Functions FMPI2C Private Functions
  338. * @{
  339. */
  340. /* Private functions to handle DMA transfer */
  341. static void FMPI2C_DMAMasterTransmitCplt(DMA_HandleTypeDef *hdma);
  342. static void FMPI2C_DMAMasterReceiveCplt(DMA_HandleTypeDef *hdma);
  343. static void FMPI2C_DMASlaveTransmitCplt(DMA_HandleTypeDef *hdma);
  344. static void FMPI2C_DMASlaveReceiveCplt(DMA_HandleTypeDef *hdma);
  345. static void FMPI2C_DMAError(DMA_HandleTypeDef *hdma);
  346. static void FMPI2C_DMAAbort(DMA_HandleTypeDef *hdma);
  347. /* Private functions to handle IT transfer */
  348. static void FMPI2C_ITAddrCplt(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags);
  349. static void FMPI2C_ITMasterSeqCplt(FMPI2C_HandleTypeDef *hfmpi2c);
  350. static void FMPI2C_ITSlaveSeqCplt(FMPI2C_HandleTypeDef *hfmpi2c);
  351. static void FMPI2C_ITMasterCplt(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags);
  352. static void FMPI2C_ITSlaveCplt(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags);
  353. static void FMPI2C_ITListenCplt(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags);
  354. static void FMPI2C_ITError(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ErrorCode);
  355. /* Private functions to handle IT transfer */
  356. static HAL_StatusTypeDef FMPI2C_RequestMemoryWrite(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint16_t MemAddress,
  357. uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart);
  358. static HAL_StatusTypeDef FMPI2C_RequestMemoryRead(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint16_t MemAddress,
  359. uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart);
  360. /* Private functions for FMPI2C transfer IRQ handler */
  361. static HAL_StatusTypeDef FMPI2C_Master_ISR_IT(struct __FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags, uint32_t ITSources);
  362. static HAL_StatusTypeDef FMPI2C_Slave_ISR_IT(struct __FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags, uint32_t ITSources);
  363. static HAL_StatusTypeDef FMPI2C_Master_ISR_DMA(struct __FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags, uint32_t ITSources);
  364. static HAL_StatusTypeDef FMPI2C_Slave_ISR_DMA(struct __FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags, uint32_t ITSources);
  365. /* Private functions to handle flags during polling transfer */
  366. static HAL_StatusTypeDef FMPI2C_WaitOnFlagUntilTimeout(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t Flag, FlagStatus Status,
  367. uint32_t Timeout, uint32_t Tickstart);
  368. static HAL_StatusTypeDef FMPI2C_WaitOnTXISFlagUntilTimeout(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t Timeout, uint32_t Tickstart);
  369. static HAL_StatusTypeDef FMPI2C_WaitOnRXNEFlagUntilTimeout(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t Timeout, uint32_t Tickstart);
  370. static HAL_StatusTypeDef FMPI2C_WaitOnSTOPFlagUntilTimeout(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t Timeout, uint32_t Tickstart);
  371. static HAL_StatusTypeDef FMPI2C_IsAcknowledgeFailed(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t Timeout, uint32_t Tickstart);
  372. /* Private functions to centralize the enable/disable of Interrupts */
  373. static void FMPI2C_Enable_IRQ(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t InterruptRequest);
  374. static void FMPI2C_Disable_IRQ(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t InterruptRequest);
  375. /* Private function to treat different error callback */
  376. static void FMPI2C_TreatErrorCallback(FMPI2C_HandleTypeDef *hfmpi2c);
  377. /* Private function to flush TXDR register */
  378. static void FMPI2C_Flush_TXDR(FMPI2C_HandleTypeDef *hfmpi2c);
  379. /* Private function to handle start, restart or stop a transfer */
  380. static void FMPI2C_TransferConfig(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint8_t Size, uint32_t Mode,
  381. uint32_t Request);
  382. /* Private function to Convert Specific options */
  383. static void FMPI2C_ConvertOtherXferOptions(FMPI2C_HandleTypeDef *hfmpi2c);
  384. /**
  385. * @}
  386. */
  387. /* Exported functions --------------------------------------------------------*/
  388. /** @defgroup FMPI2C_Exported_Functions FMPI2C Exported Functions
  389. * @{
  390. */
  391. /** @defgroup FMPI2C_Exported_Functions_Group1 Initialization and de-initialization functions
  392. * @brief Initialization and Configuration functions
  393. *
  394. @verbatim
  395. ===============================================================================
  396. ##### Initialization and de-initialization functions #####
  397. ===============================================================================
  398. [..] This subsection provides a set of functions allowing to initialize and
  399. deinitialize the FMPI2Cx peripheral:
  400. (+) User must Implement HAL_FMPI2C_MspInit() function in which he configures
  401. all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC ).
  402. (+) Call the function HAL_FMPI2C_Init() to configure the selected device with
  403. the selected configuration:
  404. (++) Clock Timing
  405. (++) Own Address 1
  406. (++) Addressing mode (Master, Slave)
  407. (++) Dual Addressing mode
  408. (++) Own Address 2
  409. (++) Own Address 2 Mask
  410. (++) General call mode
  411. (++) Nostretch mode
  412. (+) Call the function HAL_FMPI2C_DeInit() to restore the default configuration
  413. of the selected FMPI2Cx peripheral.
  414. @endverbatim
  415. * @{
  416. */
  417. /**
  418. * @brief Initializes the FMPI2C according to the specified parameters
  419. * in the FMPI2C_InitTypeDef and initialize the associated handle.
  420. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  421. * the configuration information for the specified FMPI2C.
  422. * @retval HAL status
  423. */
  424. HAL_StatusTypeDef HAL_FMPI2C_Init(FMPI2C_HandleTypeDef *hfmpi2c)
  425. {
  426. /* Check the FMPI2C handle allocation */
  427. if (hfmpi2c == NULL)
  428. {
  429. return HAL_ERROR;
  430. }
  431. /* Check the parameters */
  432. assert_param(IS_FMPI2C_ALL_INSTANCE(hfmpi2c->Instance));
  433. assert_param(IS_FMPI2C_OWN_ADDRESS1(hfmpi2c->Init.OwnAddress1));
  434. assert_param(IS_FMPI2C_ADDRESSING_MODE(hfmpi2c->Init.AddressingMode));
  435. assert_param(IS_FMPI2C_DUAL_ADDRESS(hfmpi2c->Init.DualAddressMode));
  436. assert_param(IS_FMPI2C_OWN_ADDRESS2(hfmpi2c->Init.OwnAddress2));
  437. assert_param(IS_FMPI2C_OWN_ADDRESS2_MASK(hfmpi2c->Init.OwnAddress2Masks));
  438. assert_param(IS_FMPI2C_GENERAL_CALL(hfmpi2c->Init.GeneralCallMode));
  439. assert_param(IS_FMPI2C_NO_STRETCH(hfmpi2c->Init.NoStretchMode));
  440. if (hfmpi2c->State == HAL_FMPI2C_STATE_RESET)
  441. {
  442. /* Allocate lock resource and initialize it */
  443. hfmpi2c->Lock = HAL_UNLOCKED;
  444. #if (USE_HAL_FMPI2C_REGISTER_CALLBACKS == 1)
  445. /* Init the FMPI2C Callback settings */
  446. hfmpi2c->MasterTxCpltCallback = HAL_FMPI2C_MasterTxCpltCallback; /* Legacy weak MasterTxCpltCallback */
  447. hfmpi2c->MasterRxCpltCallback = HAL_FMPI2C_MasterRxCpltCallback; /* Legacy weak MasterRxCpltCallback */
  448. hfmpi2c->SlaveTxCpltCallback = HAL_FMPI2C_SlaveTxCpltCallback; /* Legacy weak SlaveTxCpltCallback */
  449. hfmpi2c->SlaveRxCpltCallback = HAL_FMPI2C_SlaveRxCpltCallback; /* Legacy weak SlaveRxCpltCallback */
  450. hfmpi2c->ListenCpltCallback = HAL_FMPI2C_ListenCpltCallback; /* Legacy weak ListenCpltCallback */
  451. hfmpi2c->MemTxCpltCallback = HAL_FMPI2C_MemTxCpltCallback; /* Legacy weak MemTxCpltCallback */
  452. hfmpi2c->MemRxCpltCallback = HAL_FMPI2C_MemRxCpltCallback; /* Legacy weak MemRxCpltCallback */
  453. hfmpi2c->ErrorCallback = HAL_FMPI2C_ErrorCallback; /* Legacy weak ErrorCallback */
  454. hfmpi2c->AbortCpltCallback = HAL_FMPI2C_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
  455. hfmpi2c->AddrCallback = HAL_FMPI2C_AddrCallback; /* Legacy weak AddrCallback */
  456. if (hfmpi2c->MspInitCallback == NULL)
  457. {
  458. hfmpi2c->MspInitCallback = HAL_FMPI2C_MspInit; /* Legacy weak MspInit */
  459. }
  460. /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
  461. hfmpi2c->MspInitCallback(hfmpi2c);
  462. #else
  463. /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
  464. HAL_FMPI2C_MspInit(hfmpi2c);
  465. #endif /* USE_HAL_FMPI2C_REGISTER_CALLBACKS */
  466. }
  467. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY;
  468. /* Disable the selected FMPI2C peripheral */
  469. __HAL_FMPI2C_DISABLE(hfmpi2c);
  470. /*---------------------------- FMPI2Cx TIMINGR Configuration ------------------*/
  471. /* Configure FMPI2Cx: Frequency range */
  472. hfmpi2c->Instance->TIMINGR = hfmpi2c->Init.Timing & TIMING_CLEAR_MASK;
  473. /*---------------------------- FMPI2Cx OAR1 Configuration ---------------------*/
  474. /* Disable Own Address1 before set the Own Address1 configuration */
  475. hfmpi2c->Instance->OAR1 &= ~FMPI2C_OAR1_OA1EN;
  476. /* Configure FMPI2Cx: Own Address1 and ack own address1 mode */
  477. if (hfmpi2c->Init.AddressingMode == FMPI2C_ADDRESSINGMODE_7BIT)
  478. {
  479. hfmpi2c->Instance->OAR1 = (FMPI2C_OAR1_OA1EN | hfmpi2c->Init.OwnAddress1);
  480. }
  481. else /* FMPI2C_ADDRESSINGMODE_10BIT */
  482. {
  483. hfmpi2c->Instance->OAR1 = (FMPI2C_OAR1_OA1EN | FMPI2C_OAR1_OA1MODE | hfmpi2c->Init.OwnAddress1);
  484. }
  485. /*---------------------------- FMPI2Cx CR2 Configuration ----------------------*/
  486. /* Configure FMPI2Cx: Addressing Master mode */
  487. if (hfmpi2c->Init.AddressingMode == FMPI2C_ADDRESSINGMODE_10BIT)
  488. {
  489. hfmpi2c->Instance->CR2 = (FMPI2C_CR2_ADD10);
  490. }
  491. /* Enable the AUTOEND by default, and enable NACK (should be disable only during Slave process */
  492. hfmpi2c->Instance->CR2 |= (FMPI2C_CR2_AUTOEND | FMPI2C_CR2_NACK);
  493. /*---------------------------- FMPI2Cx OAR2 Configuration ---------------------*/
  494. /* Disable Own Address2 before set the Own Address2 configuration */
  495. hfmpi2c->Instance->OAR2 &= ~FMPI2C_DUALADDRESS_ENABLE;
  496. /* Configure FMPI2Cx: Dual mode and Own Address2 */
  497. hfmpi2c->Instance->OAR2 = (hfmpi2c->Init.DualAddressMode | hfmpi2c->Init.OwnAddress2 | (hfmpi2c->Init.OwnAddress2Masks << 8));
  498. /*---------------------------- FMPI2Cx CR1 Configuration ----------------------*/
  499. /* Configure FMPI2Cx: Generalcall and NoStretch mode */
  500. hfmpi2c->Instance->CR1 = (hfmpi2c->Init.GeneralCallMode | hfmpi2c->Init.NoStretchMode);
  501. /* Enable the selected FMPI2C peripheral */
  502. __HAL_FMPI2C_ENABLE(hfmpi2c);
  503. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  504. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  505. hfmpi2c->PreviousState = FMPI2C_STATE_NONE;
  506. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  507. return HAL_OK;
  508. }
  509. /**
  510. * @brief DeInitialize the FMPI2C peripheral.
  511. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  512. * the configuration information for the specified FMPI2C.
  513. * @retval HAL status
  514. */
  515. HAL_StatusTypeDef HAL_FMPI2C_DeInit(FMPI2C_HandleTypeDef *hfmpi2c)
  516. {
  517. /* Check the FMPI2C handle allocation */
  518. if (hfmpi2c == NULL)
  519. {
  520. return HAL_ERROR;
  521. }
  522. /* Check the parameters */
  523. assert_param(IS_FMPI2C_ALL_INSTANCE(hfmpi2c->Instance));
  524. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY;
  525. /* Disable the FMPI2C Peripheral Clock */
  526. __HAL_FMPI2C_DISABLE(hfmpi2c);
  527. #if (USE_HAL_FMPI2C_REGISTER_CALLBACKS == 1)
  528. if (hfmpi2c->MspDeInitCallback == NULL)
  529. {
  530. hfmpi2c->MspDeInitCallback = HAL_FMPI2C_MspDeInit; /* Legacy weak MspDeInit */
  531. }
  532. /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
  533. hfmpi2c->MspDeInitCallback(hfmpi2c);
  534. #else
  535. /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
  536. HAL_FMPI2C_MspDeInit(hfmpi2c);
  537. #endif /* USE_HAL_FMPI2C_REGISTER_CALLBACKS */
  538. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  539. hfmpi2c->State = HAL_FMPI2C_STATE_RESET;
  540. hfmpi2c->PreviousState = FMPI2C_STATE_NONE;
  541. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  542. /* Release Lock */
  543. __HAL_UNLOCK(hfmpi2c);
  544. return HAL_OK;
  545. }
  546. /**
  547. * @brief Initialize the FMPI2C MSP.
  548. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  549. * the configuration information for the specified FMPI2C.
  550. * @retval None
  551. */
  552. __weak void HAL_FMPI2C_MspInit(FMPI2C_HandleTypeDef *hfmpi2c)
  553. {
  554. /* Prevent unused argument(s) compilation warning */
  555. UNUSED(hfmpi2c);
  556. /* NOTE : This function should not be modified, when the callback is needed,
  557. the HAL_FMPI2C_MspInit could be implemented in the user file
  558. */
  559. }
  560. /**
  561. * @brief DeInitialize the FMPI2C MSP.
  562. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  563. * the configuration information for the specified FMPI2C.
  564. * @retval None
  565. */
  566. __weak void HAL_FMPI2C_MspDeInit(FMPI2C_HandleTypeDef *hfmpi2c)
  567. {
  568. /* Prevent unused argument(s) compilation warning */
  569. UNUSED(hfmpi2c);
  570. /* NOTE : This function should not be modified, when the callback is needed,
  571. the HAL_FMPI2C_MspDeInit could be implemented in the user file
  572. */
  573. }
  574. #if (USE_HAL_FMPI2C_REGISTER_CALLBACKS == 1)
  575. /**
  576. * @brief Register a User FMPI2C Callback
  577. * To be used instead of the weak predefined callback
  578. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  579. * the configuration information for the specified FMPI2C.
  580. * @param CallbackID ID of the callback to be registered
  581. * This parameter can be one of the following values:
  582. * @arg @ref HAL_FMPI2C_MASTER_TX_COMPLETE_CB_ID Master Tx Transfer completed callback ID
  583. * @arg @ref HAL_FMPI2C_MASTER_RX_COMPLETE_CB_ID Master Rx Transfer completed callback ID
  584. * @arg @ref HAL_FMPI2C_SLAVE_TX_COMPLETE_CB_ID Slave Tx Transfer completed callback ID
  585. * @arg @ref HAL_FMPI2C_SLAVE_RX_COMPLETE_CB_ID Slave Rx Transfer completed callback ID
  586. * @arg @ref HAL_FMPI2C_LISTEN_COMPLETE_CB_ID Listen Complete callback ID
  587. * @arg @ref HAL_FMPI2C_MEM_TX_COMPLETE_CB_ID Memory Tx Transfer callback ID
  588. * @arg @ref HAL_FMPI2C_MEM_RX_COMPLETE_CB_ID Memory Rx Transfer completed callback ID
  589. * @arg @ref HAL_FMPI2C_ERROR_CB_ID Error callback ID
  590. * @arg @ref HAL_FMPI2C_ABORT_CB_ID Abort callback ID
  591. * @arg @ref HAL_FMPI2C_MSPINIT_CB_ID MspInit callback ID
  592. * @arg @ref HAL_FMPI2C_MSPDEINIT_CB_ID MspDeInit callback ID
  593. * @param pCallback pointer to the Callback function
  594. * @retval HAL status
  595. */
  596. HAL_StatusTypeDef HAL_FMPI2C_RegisterCallback(FMPI2C_HandleTypeDef *hfmpi2c, HAL_FMPI2C_CallbackIDTypeDef CallbackID,
  597. pFMPI2C_CallbackTypeDef pCallback)
  598. {
  599. HAL_StatusTypeDef status = HAL_OK;
  600. if (pCallback == NULL)
  601. {
  602. /* Update the error code */
  603. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_INVALID_CALLBACK;
  604. return HAL_ERROR;
  605. }
  606. /* Process locked */
  607. __HAL_LOCK(hfmpi2c);
  608. if (HAL_FMPI2C_STATE_READY == hfmpi2c->State)
  609. {
  610. switch (CallbackID)
  611. {
  612. case HAL_FMPI2C_MASTER_TX_COMPLETE_CB_ID :
  613. hfmpi2c->MasterTxCpltCallback = pCallback;
  614. break;
  615. case HAL_FMPI2C_MASTER_RX_COMPLETE_CB_ID :
  616. hfmpi2c->MasterRxCpltCallback = pCallback;
  617. break;
  618. case HAL_FMPI2C_SLAVE_TX_COMPLETE_CB_ID :
  619. hfmpi2c->SlaveTxCpltCallback = pCallback;
  620. break;
  621. case HAL_FMPI2C_SLAVE_RX_COMPLETE_CB_ID :
  622. hfmpi2c->SlaveRxCpltCallback = pCallback;
  623. break;
  624. case HAL_FMPI2C_LISTEN_COMPLETE_CB_ID :
  625. hfmpi2c->ListenCpltCallback = pCallback;
  626. break;
  627. case HAL_FMPI2C_MEM_TX_COMPLETE_CB_ID :
  628. hfmpi2c->MemTxCpltCallback = pCallback;
  629. break;
  630. case HAL_FMPI2C_MEM_RX_COMPLETE_CB_ID :
  631. hfmpi2c->MemRxCpltCallback = pCallback;
  632. break;
  633. case HAL_FMPI2C_ERROR_CB_ID :
  634. hfmpi2c->ErrorCallback = pCallback;
  635. break;
  636. case HAL_FMPI2C_ABORT_CB_ID :
  637. hfmpi2c->AbortCpltCallback = pCallback;
  638. break;
  639. case HAL_FMPI2C_MSPINIT_CB_ID :
  640. hfmpi2c->MspInitCallback = pCallback;
  641. break;
  642. case HAL_FMPI2C_MSPDEINIT_CB_ID :
  643. hfmpi2c->MspDeInitCallback = pCallback;
  644. break;
  645. default :
  646. /* Update the error code */
  647. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_INVALID_CALLBACK;
  648. /* Return error status */
  649. status = HAL_ERROR;
  650. break;
  651. }
  652. }
  653. else if (HAL_FMPI2C_STATE_RESET == hfmpi2c->State)
  654. {
  655. switch (CallbackID)
  656. {
  657. case HAL_FMPI2C_MSPINIT_CB_ID :
  658. hfmpi2c->MspInitCallback = pCallback;
  659. break;
  660. case HAL_FMPI2C_MSPDEINIT_CB_ID :
  661. hfmpi2c->MspDeInitCallback = pCallback;
  662. break;
  663. default :
  664. /* Update the error code */
  665. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_INVALID_CALLBACK;
  666. /* Return error status */
  667. status = HAL_ERROR;
  668. break;
  669. }
  670. }
  671. else
  672. {
  673. /* Update the error code */
  674. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_INVALID_CALLBACK;
  675. /* Return error status */
  676. status = HAL_ERROR;
  677. }
  678. /* Release Lock */
  679. __HAL_UNLOCK(hfmpi2c);
  680. return status;
  681. }
  682. /**
  683. * @brief Unregister an FMPI2C Callback
  684. * FMPI2C callback is redirected to the weak predefined callback
  685. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  686. * the configuration information for the specified FMPI2C.
  687. * @param CallbackID ID of the callback to be unregistered
  688. * This parameter can be one of the following values:
  689. * This parameter can be one of the following values:
  690. * @arg @ref HAL_FMPI2C_MASTER_TX_COMPLETE_CB_ID Master Tx Transfer completed callback ID
  691. * @arg @ref HAL_FMPI2C_MASTER_RX_COMPLETE_CB_ID Master Rx Transfer completed callback ID
  692. * @arg @ref HAL_FMPI2C_SLAVE_TX_COMPLETE_CB_ID Slave Tx Transfer completed callback ID
  693. * @arg @ref HAL_FMPI2C_SLAVE_RX_COMPLETE_CB_ID Slave Rx Transfer completed callback ID
  694. * @arg @ref HAL_FMPI2C_LISTEN_COMPLETE_CB_ID Listen Complete callback ID
  695. * @arg @ref HAL_FMPI2C_MEM_TX_COMPLETE_CB_ID Memory Tx Transfer callback ID
  696. * @arg @ref HAL_FMPI2C_MEM_RX_COMPLETE_CB_ID Memory Rx Transfer completed callback ID
  697. * @arg @ref HAL_FMPI2C_ERROR_CB_ID Error callback ID
  698. * @arg @ref HAL_FMPI2C_ABORT_CB_ID Abort callback ID
  699. * @arg @ref HAL_FMPI2C_MSPINIT_CB_ID MspInit callback ID
  700. * @arg @ref HAL_FMPI2C_MSPDEINIT_CB_ID MspDeInit callback ID
  701. * @retval HAL status
  702. */
  703. HAL_StatusTypeDef HAL_FMPI2C_UnRegisterCallback(FMPI2C_HandleTypeDef *hfmpi2c, HAL_FMPI2C_CallbackIDTypeDef CallbackID)
  704. {
  705. HAL_StatusTypeDef status = HAL_OK;
  706. /* Process locked */
  707. __HAL_LOCK(hfmpi2c);
  708. if (HAL_FMPI2C_STATE_READY == hfmpi2c->State)
  709. {
  710. switch (CallbackID)
  711. {
  712. case HAL_FMPI2C_MASTER_TX_COMPLETE_CB_ID :
  713. hfmpi2c->MasterTxCpltCallback = HAL_FMPI2C_MasterTxCpltCallback; /* Legacy weak MasterTxCpltCallback */
  714. break;
  715. case HAL_FMPI2C_MASTER_RX_COMPLETE_CB_ID :
  716. hfmpi2c->MasterRxCpltCallback = HAL_FMPI2C_MasterRxCpltCallback; /* Legacy weak MasterRxCpltCallback */
  717. break;
  718. case HAL_FMPI2C_SLAVE_TX_COMPLETE_CB_ID :
  719. hfmpi2c->SlaveTxCpltCallback = HAL_FMPI2C_SlaveTxCpltCallback; /* Legacy weak SlaveTxCpltCallback */
  720. break;
  721. case HAL_FMPI2C_SLAVE_RX_COMPLETE_CB_ID :
  722. hfmpi2c->SlaveRxCpltCallback = HAL_FMPI2C_SlaveRxCpltCallback; /* Legacy weak SlaveRxCpltCallback */
  723. break;
  724. case HAL_FMPI2C_LISTEN_COMPLETE_CB_ID :
  725. hfmpi2c->ListenCpltCallback = HAL_FMPI2C_ListenCpltCallback; /* Legacy weak ListenCpltCallback */
  726. break;
  727. case HAL_FMPI2C_MEM_TX_COMPLETE_CB_ID :
  728. hfmpi2c->MemTxCpltCallback = HAL_FMPI2C_MemTxCpltCallback; /* Legacy weak MemTxCpltCallback */
  729. break;
  730. case HAL_FMPI2C_MEM_RX_COMPLETE_CB_ID :
  731. hfmpi2c->MemRxCpltCallback = HAL_FMPI2C_MemRxCpltCallback; /* Legacy weak MemRxCpltCallback */
  732. break;
  733. case HAL_FMPI2C_ERROR_CB_ID :
  734. hfmpi2c->ErrorCallback = HAL_FMPI2C_ErrorCallback; /* Legacy weak ErrorCallback */
  735. break;
  736. case HAL_FMPI2C_ABORT_CB_ID :
  737. hfmpi2c->AbortCpltCallback = HAL_FMPI2C_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
  738. break;
  739. case HAL_FMPI2C_MSPINIT_CB_ID :
  740. hfmpi2c->MspInitCallback = HAL_FMPI2C_MspInit; /* Legacy weak MspInit */
  741. break;
  742. case HAL_FMPI2C_MSPDEINIT_CB_ID :
  743. hfmpi2c->MspDeInitCallback = HAL_FMPI2C_MspDeInit; /* Legacy weak MspDeInit */
  744. break;
  745. default :
  746. /* Update the error code */
  747. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_INVALID_CALLBACK;
  748. /* Return error status */
  749. status = HAL_ERROR;
  750. break;
  751. }
  752. }
  753. else if (HAL_FMPI2C_STATE_RESET == hfmpi2c->State)
  754. {
  755. switch (CallbackID)
  756. {
  757. case HAL_FMPI2C_MSPINIT_CB_ID :
  758. hfmpi2c->MspInitCallback = HAL_FMPI2C_MspInit; /* Legacy weak MspInit */
  759. break;
  760. case HAL_FMPI2C_MSPDEINIT_CB_ID :
  761. hfmpi2c->MspDeInitCallback = HAL_FMPI2C_MspDeInit; /* Legacy weak MspDeInit */
  762. break;
  763. default :
  764. /* Update the error code */
  765. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_INVALID_CALLBACK;
  766. /* Return error status */
  767. status = HAL_ERROR;
  768. break;
  769. }
  770. }
  771. else
  772. {
  773. /* Update the error code */
  774. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_INVALID_CALLBACK;
  775. /* Return error status */
  776. status = HAL_ERROR;
  777. }
  778. /* Release Lock */
  779. __HAL_UNLOCK(hfmpi2c);
  780. return status;
  781. }
  782. /**
  783. * @brief Register the Slave Address Match FMPI2C Callback
  784. * To be used instead of the weak HAL_FMPI2C_AddrCallback() predefined callback
  785. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  786. * the configuration information for the specified FMPI2C.
  787. * @param pCallback pointer to the Address Match Callback function
  788. * @retval HAL status
  789. */
  790. HAL_StatusTypeDef HAL_FMPI2C_RegisterAddrCallback(FMPI2C_HandleTypeDef *hfmpi2c, pFMPI2C_AddrCallbackTypeDef pCallback)
  791. {
  792. HAL_StatusTypeDef status = HAL_OK;
  793. if (pCallback == NULL)
  794. {
  795. /* Update the error code */
  796. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_INVALID_CALLBACK;
  797. return HAL_ERROR;
  798. }
  799. /* Process locked */
  800. __HAL_LOCK(hfmpi2c);
  801. if (HAL_FMPI2C_STATE_READY == hfmpi2c->State)
  802. {
  803. hfmpi2c->AddrCallback = pCallback;
  804. }
  805. else
  806. {
  807. /* Update the error code */
  808. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_INVALID_CALLBACK;
  809. /* Return error status */
  810. status = HAL_ERROR;
  811. }
  812. /* Release Lock */
  813. __HAL_UNLOCK(hfmpi2c);
  814. return status;
  815. }
  816. /**
  817. * @brief UnRegister the Slave Address Match FMPI2C Callback
  818. * Info Ready FMPI2C Callback is redirected to the weak HAL_FMPI2C_AddrCallback() predefined callback
  819. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  820. * the configuration information for the specified FMPI2C.
  821. * @retval HAL status
  822. */
  823. HAL_StatusTypeDef HAL_FMPI2C_UnRegisterAddrCallback(FMPI2C_HandleTypeDef *hfmpi2c)
  824. {
  825. HAL_StatusTypeDef status = HAL_OK;
  826. /* Process locked */
  827. __HAL_LOCK(hfmpi2c);
  828. if (HAL_FMPI2C_STATE_READY == hfmpi2c->State)
  829. {
  830. hfmpi2c->AddrCallback = HAL_FMPI2C_AddrCallback; /* Legacy weak AddrCallback */
  831. }
  832. else
  833. {
  834. /* Update the error code */
  835. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_INVALID_CALLBACK;
  836. /* Return error status */
  837. status = HAL_ERROR;
  838. }
  839. /* Release Lock */
  840. __HAL_UNLOCK(hfmpi2c);
  841. return status;
  842. }
  843. #endif /* USE_HAL_FMPI2C_REGISTER_CALLBACKS */
  844. /**
  845. * @}
  846. */
  847. /** @defgroup FMPI2C_Exported_Functions_Group2 Input and Output operation functions
  848. * @brief Data transfers functions
  849. *
  850. @verbatim
  851. ===============================================================================
  852. ##### IO operation functions #####
  853. ===============================================================================
  854. [..]
  855. This subsection provides a set of functions allowing to manage the FMPI2C data
  856. transfers.
  857. (#) There are two modes of transfer:
  858. (++) Blocking mode : The communication is performed in the polling mode.
  859. The status of all data processing is returned by the same function
  860. after finishing transfer.
  861. (++) No-Blocking mode : The communication is performed using Interrupts
  862. or DMA. These functions return the status of the transfer startup.
  863. The end of the data processing will be indicated through the
  864. dedicated FMPI2C IRQ when using Interrupt mode or the DMA IRQ when
  865. using DMA mode.
  866. (#) Blocking mode functions are :
  867. (++) HAL_FMPI2C_Master_Transmit()
  868. (++) HAL_FMPI2C_Master_Receive()
  869. (++) HAL_FMPI2C_Slave_Transmit()
  870. (++) HAL_FMPI2C_Slave_Receive()
  871. (++) HAL_FMPI2C_Mem_Write()
  872. (++) HAL_FMPI2C_Mem_Read()
  873. (++) HAL_FMPI2C_IsDeviceReady()
  874. (#) No-Blocking mode functions with Interrupt are :
  875. (++) HAL_FMPI2C_Master_Transmit_IT()
  876. (++) HAL_FMPI2C_Master_Receive_IT()
  877. (++) HAL_FMPI2C_Slave_Transmit_IT()
  878. (++) HAL_FMPI2C_Slave_Receive_IT()
  879. (++) HAL_FMPI2C_Mem_Write_IT()
  880. (++) HAL_FMPI2C_Mem_Read_IT()
  881. (++) HAL_FMPI2C_Master_Seq_Transmit_IT()
  882. (++) HAL_FMPI2C_Master_Seq_Receive_IT()
  883. (++) HAL_FMPI2C_Slave_Seq_Transmit_IT()
  884. (++) HAL_FMPI2C_Slave_Seq_Receive_IT()
  885. (++) HAL_FMPI2C_EnableListen_IT()
  886. (++) HAL_FMPI2C_DisableListen_IT()
  887. (++) HAL_FMPI2C_Master_Abort_IT()
  888. (#) No-Blocking mode functions with DMA are :
  889. (++) HAL_FMPI2C_Master_Transmit_DMA()
  890. (++) HAL_FMPI2C_Master_Receive_DMA()
  891. (++) HAL_FMPI2C_Slave_Transmit_DMA()
  892. (++) HAL_FMPI2C_Slave_Receive_DMA()
  893. (++) HAL_FMPI2C_Mem_Write_DMA()
  894. (++) HAL_FMPI2C_Mem_Read_DMA()
  895. (++) HAL_FMPI2C_Master_Seq_Transmit_DMA()
  896. (++) HAL_FMPI2C_Master_Seq_Receive_DMA()
  897. (++) HAL_FMPI2C_Slave_Seq_Transmit_DMA()
  898. (++) HAL_FMPI2C_Slave_Seq_Receive_DMA()
  899. (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
  900. (++) HAL_FMPI2C_MasterTxCpltCallback()
  901. (++) HAL_FMPI2C_MasterRxCpltCallback()
  902. (++) HAL_FMPI2C_SlaveTxCpltCallback()
  903. (++) HAL_FMPI2C_SlaveRxCpltCallback()
  904. (++) HAL_FMPI2C_MemTxCpltCallback()
  905. (++) HAL_FMPI2C_MemRxCpltCallback()
  906. (++) HAL_FMPI2C_AddrCallback()
  907. (++) HAL_FMPI2C_ListenCpltCallback()
  908. (++) HAL_FMPI2C_ErrorCallback()
  909. (++) HAL_FMPI2C_AbortCpltCallback()
  910. @endverbatim
  911. * @{
  912. */
  913. /**
  914. * @brief Transmits in master mode an amount of data in blocking mode.
  915. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  916. * the configuration information for the specified FMPI2C.
  917. * @param DevAddress Target device address: The device 7 bits address value
  918. * in datasheet must be shifted to the left before calling the interface
  919. * @param pData Pointer to data buffer
  920. * @param Size Amount of data to be sent
  921. * @param Timeout Timeout duration
  922. * @retval HAL status
  923. */
  924. HAL_StatusTypeDef HAL_FMPI2C_Master_Transmit(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size,
  925. uint32_t Timeout)
  926. {
  927. uint32_t tickstart;
  928. if (hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  929. {
  930. /* Process Locked */
  931. __HAL_LOCK(hfmpi2c);
  932. /* Init tickstart for timeout management*/
  933. tickstart = HAL_GetTick();
  934. if (FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_BUSY, SET, FMPI2C_TIMEOUT_BUSY, tickstart) != HAL_OK)
  935. {
  936. return HAL_ERROR;
  937. }
  938. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_TX;
  939. hfmpi2c->Mode = HAL_FMPI2C_MODE_MASTER;
  940. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  941. /* Prepare transfer parameters */
  942. hfmpi2c->pBuffPtr = pData;
  943. hfmpi2c->XferCount = Size;
  944. hfmpi2c->XferISR = NULL;
  945. /* Send Slave Address */
  946. /* Set NBYTES to write and reload if hfmpi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
  947. if (hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  948. {
  949. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  950. FMPI2C_TransferConfig(hfmpi2c, DevAddress, (uint8_t)hfmpi2c->XferSize, FMPI2C_RELOAD_MODE, FMPI2C_GENERATE_START_WRITE);
  951. }
  952. else
  953. {
  954. hfmpi2c->XferSize = hfmpi2c->XferCount;
  955. FMPI2C_TransferConfig(hfmpi2c, DevAddress, (uint8_t)hfmpi2c->XferSize, FMPI2C_AUTOEND_MODE, FMPI2C_GENERATE_START_WRITE);
  956. }
  957. while (hfmpi2c->XferCount > 0U)
  958. {
  959. /* Wait until TXIS flag is set */
  960. if (FMPI2C_WaitOnTXISFlagUntilTimeout(hfmpi2c, Timeout, tickstart) != HAL_OK)
  961. {
  962. return HAL_ERROR;
  963. }
  964. /* Write data to TXDR */
  965. hfmpi2c->Instance->TXDR = *hfmpi2c->pBuffPtr;
  966. /* Increment Buffer pointer */
  967. hfmpi2c->pBuffPtr++;
  968. hfmpi2c->XferCount--;
  969. hfmpi2c->XferSize--;
  970. if ((hfmpi2c->XferCount != 0U) && (hfmpi2c->XferSize == 0U))
  971. {
  972. /* Wait until TCR flag is set */
  973. if (FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_TCR, RESET, Timeout, tickstart) != HAL_OK)
  974. {
  975. return HAL_ERROR;
  976. }
  977. if (hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  978. {
  979. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  980. FMPI2C_TransferConfig(hfmpi2c, DevAddress, (uint8_t)hfmpi2c->XferSize, FMPI2C_RELOAD_MODE, FMPI2C_NO_STARTSTOP);
  981. }
  982. else
  983. {
  984. hfmpi2c->XferSize = hfmpi2c->XferCount;
  985. FMPI2C_TransferConfig(hfmpi2c, DevAddress, (uint8_t)hfmpi2c->XferSize, FMPI2C_AUTOEND_MODE, FMPI2C_NO_STARTSTOP);
  986. }
  987. }
  988. }
  989. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  990. /* Wait until STOPF flag is set */
  991. if (FMPI2C_WaitOnSTOPFlagUntilTimeout(hfmpi2c, Timeout, tickstart) != HAL_OK)
  992. {
  993. return HAL_ERROR;
  994. }
  995. /* Clear STOP Flag */
  996. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  997. /* Clear Configuration Register 2 */
  998. FMPI2C_RESET_CR2(hfmpi2c);
  999. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  1000. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  1001. /* Process Unlocked */
  1002. __HAL_UNLOCK(hfmpi2c);
  1003. return HAL_OK;
  1004. }
  1005. else
  1006. {
  1007. return HAL_BUSY;
  1008. }
  1009. }
  1010. /**
  1011. * @brief Receives in master mode an amount of data in blocking mode.
  1012. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  1013. * the configuration information for the specified FMPI2C.
  1014. * @param DevAddress Target device address: The device 7 bits address value
  1015. * in datasheet must be shifted to the left before calling the interface
  1016. * @param pData Pointer to data buffer
  1017. * @param Size Amount of data to be sent
  1018. * @param Timeout Timeout duration
  1019. * @retval HAL status
  1020. */
  1021. HAL_StatusTypeDef HAL_FMPI2C_Master_Receive(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size,
  1022. uint32_t Timeout)
  1023. {
  1024. uint32_t tickstart;
  1025. if (hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1026. {
  1027. /* Process Locked */
  1028. __HAL_LOCK(hfmpi2c);
  1029. /* Init tickstart for timeout management*/
  1030. tickstart = HAL_GetTick();
  1031. if (FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_BUSY, SET, FMPI2C_TIMEOUT_BUSY, tickstart) != HAL_OK)
  1032. {
  1033. return HAL_ERROR;
  1034. }
  1035. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_RX;
  1036. hfmpi2c->Mode = HAL_FMPI2C_MODE_MASTER;
  1037. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1038. /* Prepare transfer parameters */
  1039. hfmpi2c->pBuffPtr = pData;
  1040. hfmpi2c->XferCount = Size;
  1041. hfmpi2c->XferISR = NULL;
  1042. /* Send Slave Address */
  1043. /* Set NBYTES to write and reload if hfmpi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
  1044. if (hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  1045. {
  1046. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  1047. FMPI2C_TransferConfig(hfmpi2c, DevAddress, (uint8_t)hfmpi2c->XferSize, FMPI2C_RELOAD_MODE, FMPI2C_GENERATE_START_READ);
  1048. }
  1049. else
  1050. {
  1051. hfmpi2c->XferSize = hfmpi2c->XferCount;
  1052. FMPI2C_TransferConfig(hfmpi2c, DevAddress, (uint8_t)hfmpi2c->XferSize, FMPI2C_AUTOEND_MODE, FMPI2C_GENERATE_START_READ);
  1053. }
  1054. while (hfmpi2c->XferCount > 0U)
  1055. {
  1056. /* Wait until RXNE flag is set */
  1057. if (FMPI2C_WaitOnRXNEFlagUntilTimeout(hfmpi2c, Timeout, tickstart) != HAL_OK)
  1058. {
  1059. return HAL_ERROR;
  1060. }
  1061. /* Read data from RXDR */
  1062. *hfmpi2c->pBuffPtr = (uint8_t)hfmpi2c->Instance->RXDR;
  1063. /* Increment Buffer pointer */
  1064. hfmpi2c->pBuffPtr++;
  1065. hfmpi2c->XferSize--;
  1066. hfmpi2c->XferCount--;
  1067. if ((hfmpi2c->XferCount != 0U) && (hfmpi2c->XferSize == 0U))
  1068. {
  1069. /* Wait until TCR flag is set */
  1070. if (FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_TCR, RESET, Timeout, tickstart) != HAL_OK)
  1071. {
  1072. return HAL_ERROR;
  1073. }
  1074. if (hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  1075. {
  1076. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  1077. FMPI2C_TransferConfig(hfmpi2c, DevAddress, (uint8_t)hfmpi2c->XferSize, FMPI2C_RELOAD_MODE, FMPI2C_NO_STARTSTOP);
  1078. }
  1079. else
  1080. {
  1081. hfmpi2c->XferSize = hfmpi2c->XferCount;
  1082. FMPI2C_TransferConfig(hfmpi2c, DevAddress, (uint8_t)hfmpi2c->XferSize, FMPI2C_AUTOEND_MODE, FMPI2C_NO_STARTSTOP);
  1083. }
  1084. }
  1085. }
  1086. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  1087. /* Wait until STOPF flag is set */
  1088. if (FMPI2C_WaitOnSTOPFlagUntilTimeout(hfmpi2c, Timeout, tickstart) != HAL_OK)
  1089. {
  1090. return HAL_ERROR;
  1091. }
  1092. /* Clear STOP Flag */
  1093. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  1094. /* Clear Configuration Register 2 */
  1095. FMPI2C_RESET_CR2(hfmpi2c);
  1096. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  1097. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  1098. /* Process Unlocked */
  1099. __HAL_UNLOCK(hfmpi2c);
  1100. return HAL_OK;
  1101. }
  1102. else
  1103. {
  1104. return HAL_BUSY;
  1105. }
  1106. }
  1107. /**
  1108. * @brief Transmits in slave mode an amount of data in blocking mode.
  1109. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  1110. * the configuration information for the specified FMPI2C.
  1111. * @param pData Pointer to data buffer
  1112. * @param Size Amount of data to be sent
  1113. * @param Timeout Timeout duration
  1114. * @retval HAL status
  1115. */
  1116. HAL_StatusTypeDef HAL_FMPI2C_Slave_Transmit(FMPI2C_HandleTypeDef *hfmpi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  1117. {
  1118. uint32_t tickstart;
  1119. if (hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1120. {
  1121. if ((pData == NULL) || (Size == 0U))
  1122. {
  1123. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_INVALID_PARAM;
  1124. return HAL_ERROR;
  1125. }
  1126. /* Process Locked */
  1127. __HAL_LOCK(hfmpi2c);
  1128. /* Init tickstart for timeout management*/
  1129. tickstart = HAL_GetTick();
  1130. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_TX;
  1131. hfmpi2c->Mode = HAL_FMPI2C_MODE_SLAVE;
  1132. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1133. /* Prepare transfer parameters */
  1134. hfmpi2c->pBuffPtr = pData;
  1135. hfmpi2c->XferCount = Size;
  1136. hfmpi2c->XferISR = NULL;
  1137. /* Enable Address Acknowledge */
  1138. hfmpi2c->Instance->CR2 &= ~FMPI2C_CR2_NACK;
  1139. /* Wait until ADDR flag is set */
  1140. if (FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_ADDR, RESET, Timeout, tickstart) != HAL_OK)
  1141. {
  1142. /* Disable Address Acknowledge */
  1143. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  1144. return HAL_ERROR;
  1145. }
  1146. /* Clear ADDR flag */
  1147. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_ADDR);
  1148. /* If 10bit addressing mode is selected */
  1149. if (hfmpi2c->Init.AddressingMode == FMPI2C_ADDRESSINGMODE_10BIT)
  1150. {
  1151. /* Wait until ADDR flag is set */
  1152. if (FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_ADDR, RESET, Timeout, tickstart) != HAL_OK)
  1153. {
  1154. /* Disable Address Acknowledge */
  1155. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  1156. return HAL_ERROR;
  1157. }
  1158. /* Clear ADDR flag */
  1159. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_ADDR);
  1160. }
  1161. /* Wait until DIR flag is set Transmitter mode */
  1162. if (FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_DIR, RESET, Timeout, tickstart) != HAL_OK)
  1163. {
  1164. /* Disable Address Acknowledge */
  1165. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  1166. return HAL_ERROR;
  1167. }
  1168. while (hfmpi2c->XferCount > 0U)
  1169. {
  1170. /* Wait until TXIS flag is set */
  1171. if (FMPI2C_WaitOnTXISFlagUntilTimeout(hfmpi2c, Timeout, tickstart) != HAL_OK)
  1172. {
  1173. /* Disable Address Acknowledge */
  1174. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  1175. return HAL_ERROR;
  1176. }
  1177. /* Write data to TXDR */
  1178. hfmpi2c->Instance->TXDR = *hfmpi2c->pBuffPtr;
  1179. /* Increment Buffer pointer */
  1180. hfmpi2c->pBuffPtr++;
  1181. hfmpi2c->XferCount--;
  1182. }
  1183. /* Wait until STOP flag is set */
  1184. if (FMPI2C_WaitOnSTOPFlagUntilTimeout(hfmpi2c, Timeout, tickstart) != HAL_OK)
  1185. {
  1186. /* Disable Address Acknowledge */
  1187. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  1188. if (hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  1189. {
  1190. /* Normal use case for Transmitter mode */
  1191. /* A NACK is generated to confirm the end of transfer */
  1192. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1193. }
  1194. else
  1195. {
  1196. return HAL_ERROR;
  1197. }
  1198. }
  1199. /* Clear STOP flag */
  1200. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  1201. /* Wait until BUSY flag is reset */
  1202. if (FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_BUSY, SET, Timeout, tickstart) != HAL_OK)
  1203. {
  1204. /* Disable Address Acknowledge */
  1205. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  1206. return HAL_ERROR;
  1207. }
  1208. /* Disable Address Acknowledge */
  1209. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  1210. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  1211. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  1212. /* Process Unlocked */
  1213. __HAL_UNLOCK(hfmpi2c);
  1214. return HAL_OK;
  1215. }
  1216. else
  1217. {
  1218. return HAL_BUSY;
  1219. }
  1220. }
  1221. /**
  1222. * @brief Receive in slave mode an amount of data in blocking mode
  1223. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  1224. * the configuration information for the specified FMPI2C.
  1225. * @param pData Pointer to data buffer
  1226. * @param Size Amount of data to be sent
  1227. * @param Timeout Timeout duration
  1228. * @retval HAL status
  1229. */
  1230. HAL_StatusTypeDef HAL_FMPI2C_Slave_Receive(FMPI2C_HandleTypeDef *hfmpi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  1231. {
  1232. uint32_t tickstart;
  1233. if (hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1234. {
  1235. if ((pData == NULL) || (Size == 0U))
  1236. {
  1237. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_INVALID_PARAM;
  1238. return HAL_ERROR;
  1239. }
  1240. /* Process Locked */
  1241. __HAL_LOCK(hfmpi2c);
  1242. /* Init tickstart for timeout management*/
  1243. tickstart = HAL_GetTick();
  1244. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_RX;
  1245. hfmpi2c->Mode = HAL_FMPI2C_MODE_SLAVE;
  1246. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1247. /* Prepare transfer parameters */
  1248. hfmpi2c->pBuffPtr = pData;
  1249. hfmpi2c->XferCount = Size;
  1250. hfmpi2c->XferISR = NULL;
  1251. /* Enable Address Acknowledge */
  1252. hfmpi2c->Instance->CR2 &= ~FMPI2C_CR2_NACK;
  1253. /* Wait until ADDR flag is set */
  1254. if (FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_ADDR, RESET, Timeout, tickstart) != HAL_OK)
  1255. {
  1256. /* Disable Address Acknowledge */
  1257. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  1258. return HAL_ERROR;
  1259. }
  1260. /* Clear ADDR flag */
  1261. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_ADDR);
  1262. /* Wait until DIR flag is reset Receiver mode */
  1263. if (FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_DIR, SET, Timeout, tickstart) != HAL_OK)
  1264. {
  1265. /* Disable Address Acknowledge */
  1266. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  1267. return HAL_ERROR;
  1268. }
  1269. while (hfmpi2c->XferCount > 0U)
  1270. {
  1271. /* Wait until RXNE flag is set */
  1272. if (FMPI2C_WaitOnRXNEFlagUntilTimeout(hfmpi2c, Timeout, tickstart) != HAL_OK)
  1273. {
  1274. /* Disable Address Acknowledge */
  1275. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  1276. /* Store Last receive data if any */
  1277. if (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_RXNE) == SET)
  1278. {
  1279. /* Read data from RXDR */
  1280. *hfmpi2c->pBuffPtr = (uint8_t)hfmpi2c->Instance->RXDR;
  1281. /* Increment Buffer pointer */
  1282. hfmpi2c->pBuffPtr++;
  1283. hfmpi2c->XferCount--;
  1284. }
  1285. return HAL_ERROR;
  1286. }
  1287. /* Read data from RXDR */
  1288. *hfmpi2c->pBuffPtr = (uint8_t)hfmpi2c->Instance->RXDR;
  1289. /* Increment Buffer pointer */
  1290. hfmpi2c->pBuffPtr++;
  1291. hfmpi2c->XferCount--;
  1292. }
  1293. /* Wait until STOP flag is set */
  1294. if (FMPI2C_WaitOnSTOPFlagUntilTimeout(hfmpi2c, Timeout, tickstart) != HAL_OK)
  1295. {
  1296. /* Disable Address Acknowledge */
  1297. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  1298. return HAL_ERROR;
  1299. }
  1300. /* Clear STOP flag */
  1301. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  1302. /* Wait until BUSY flag is reset */
  1303. if (FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_BUSY, SET, Timeout, tickstart) != HAL_OK)
  1304. {
  1305. /* Disable Address Acknowledge */
  1306. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  1307. return HAL_ERROR;
  1308. }
  1309. /* Disable Address Acknowledge */
  1310. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  1311. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  1312. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  1313. /* Process Unlocked */
  1314. __HAL_UNLOCK(hfmpi2c);
  1315. return HAL_OK;
  1316. }
  1317. else
  1318. {
  1319. return HAL_BUSY;
  1320. }
  1321. }
  1322. /**
  1323. * @brief Transmit in master mode an amount of data in non-blocking mode with Interrupt
  1324. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  1325. * the configuration information for the specified FMPI2C.
  1326. * @param DevAddress Target device address: The device 7 bits address value
  1327. * in datasheet must be shifted to the left before calling the interface
  1328. * @param pData Pointer to data buffer
  1329. * @param Size Amount of data to be sent
  1330. * @retval HAL status
  1331. */
  1332. HAL_StatusTypeDef HAL_FMPI2C_Master_Transmit_IT(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint8_t *pData,
  1333. uint16_t Size)
  1334. {
  1335. uint32_t xfermode;
  1336. if (hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1337. {
  1338. if (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_BUSY) == SET)
  1339. {
  1340. return HAL_BUSY;
  1341. }
  1342. /* Process Locked */
  1343. __HAL_LOCK(hfmpi2c);
  1344. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_TX;
  1345. hfmpi2c->Mode = HAL_FMPI2C_MODE_MASTER;
  1346. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1347. /* Prepare transfer parameters */
  1348. hfmpi2c->pBuffPtr = pData;
  1349. hfmpi2c->XferCount = Size;
  1350. hfmpi2c->XferOptions = FMPI2C_NO_OPTION_FRAME;
  1351. hfmpi2c->XferISR = FMPI2C_Master_ISR_IT;
  1352. if (hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  1353. {
  1354. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  1355. xfermode = FMPI2C_RELOAD_MODE;
  1356. }
  1357. else
  1358. {
  1359. hfmpi2c->XferSize = hfmpi2c->XferCount;
  1360. xfermode = FMPI2C_AUTOEND_MODE;
  1361. }
  1362. /* Send Slave Address */
  1363. /* Set NBYTES to write and reload if hfmpi2c->XferCount > MAX_NBYTE_SIZE */
  1364. FMPI2C_TransferConfig(hfmpi2c, DevAddress, (uint8_t)hfmpi2c->XferSize, xfermode, FMPI2C_GENERATE_START_WRITE);
  1365. /* Process Unlocked */
  1366. __HAL_UNLOCK(hfmpi2c);
  1367. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  1368. to avoid the risk of FMPI2C interrupt handle execution before current
  1369. process unlock */
  1370. /* Enable ERR, TC, STOP, NACK, TXI interrupt */
  1371. /* possible to enable all of these */
  1372. /* FMPI2C_IT_ERRI | FMPI2C_IT_TCI| FMPI2C_IT_STOPI| FMPI2C_IT_NACKI | FMPI2C_IT_ADDRI | FMPI2C_IT_RXI | FMPI2C_IT_TXI */
  1373. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_TX_IT);
  1374. return HAL_OK;
  1375. }
  1376. else
  1377. {
  1378. return HAL_BUSY;
  1379. }
  1380. }
  1381. /**
  1382. * @brief Receive in master mode an amount of data in non-blocking mode with Interrupt
  1383. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  1384. * the configuration information for the specified FMPI2C.
  1385. * @param DevAddress Target device address: The device 7 bits address value
  1386. * in datasheet must be shifted to the left before calling the interface
  1387. * @param pData Pointer to data buffer
  1388. * @param Size Amount of data to be sent
  1389. * @retval HAL status
  1390. */
  1391. HAL_StatusTypeDef HAL_FMPI2C_Master_Receive_IT(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
  1392. {
  1393. uint32_t xfermode;
  1394. if (hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1395. {
  1396. if (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_BUSY) == SET)
  1397. {
  1398. return HAL_BUSY;
  1399. }
  1400. /* Process Locked */
  1401. __HAL_LOCK(hfmpi2c);
  1402. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_RX;
  1403. hfmpi2c->Mode = HAL_FMPI2C_MODE_MASTER;
  1404. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1405. /* Prepare transfer parameters */
  1406. hfmpi2c->pBuffPtr = pData;
  1407. hfmpi2c->XferCount = Size;
  1408. hfmpi2c->XferOptions = FMPI2C_NO_OPTION_FRAME;
  1409. hfmpi2c->XferISR = FMPI2C_Master_ISR_IT;
  1410. if (hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  1411. {
  1412. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  1413. xfermode = FMPI2C_RELOAD_MODE;
  1414. }
  1415. else
  1416. {
  1417. hfmpi2c->XferSize = hfmpi2c->XferCount;
  1418. xfermode = FMPI2C_AUTOEND_MODE;
  1419. }
  1420. /* Send Slave Address */
  1421. /* Set NBYTES to write and reload if hfmpi2c->XferCount > MAX_NBYTE_SIZE */
  1422. FMPI2C_TransferConfig(hfmpi2c, DevAddress, (uint8_t)hfmpi2c->XferSize, xfermode, FMPI2C_GENERATE_START_READ);
  1423. /* Process Unlocked */
  1424. __HAL_UNLOCK(hfmpi2c);
  1425. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  1426. to avoid the risk of FMPI2C interrupt handle execution before current
  1427. process unlock */
  1428. /* Enable ERR, TC, STOP, NACK, RXI interrupt */
  1429. /* possible to enable all of these */
  1430. /* FMPI2C_IT_ERRI | FMPI2C_IT_TCI| FMPI2C_IT_STOPI| FMPI2C_IT_NACKI | FMPI2C_IT_ADDRI | FMPI2C_IT_RXI | FMPI2C_IT_TXI */
  1431. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_RX_IT);
  1432. return HAL_OK;
  1433. }
  1434. else
  1435. {
  1436. return HAL_BUSY;
  1437. }
  1438. }
  1439. /**
  1440. * @brief Transmit in slave mode an amount of data in non-blocking mode with Interrupt
  1441. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  1442. * the configuration information for the specified FMPI2C.
  1443. * @param pData Pointer to data buffer
  1444. * @param Size Amount of data to be sent
  1445. * @retval HAL status
  1446. */
  1447. HAL_StatusTypeDef HAL_FMPI2C_Slave_Transmit_IT(FMPI2C_HandleTypeDef *hfmpi2c, uint8_t *pData, uint16_t Size)
  1448. {
  1449. if (hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1450. {
  1451. /* Process Locked */
  1452. __HAL_LOCK(hfmpi2c);
  1453. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_TX;
  1454. hfmpi2c->Mode = HAL_FMPI2C_MODE_SLAVE;
  1455. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1456. /* Enable Address Acknowledge */
  1457. hfmpi2c->Instance->CR2 &= ~FMPI2C_CR2_NACK;
  1458. /* Prepare transfer parameters */
  1459. hfmpi2c->pBuffPtr = pData;
  1460. hfmpi2c->XferCount = Size;
  1461. hfmpi2c->XferSize = hfmpi2c->XferCount;
  1462. hfmpi2c->XferOptions = FMPI2C_NO_OPTION_FRAME;
  1463. hfmpi2c->XferISR = FMPI2C_Slave_ISR_IT;
  1464. /* Process Unlocked */
  1465. __HAL_UNLOCK(hfmpi2c);
  1466. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  1467. to avoid the risk of FMPI2C interrupt handle execution before current
  1468. process unlock */
  1469. /* Enable ERR, TC, STOP, NACK, TXI interrupt */
  1470. /* possible to enable all of these */
  1471. /* FMPI2C_IT_ERRI | FMPI2C_IT_TCI| FMPI2C_IT_STOPI| FMPI2C_IT_NACKI | FMPI2C_IT_ADDRI | FMPI2C_IT_RXI | FMPI2C_IT_TXI */
  1472. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_TX_IT | FMPI2C_XFER_LISTEN_IT);
  1473. return HAL_OK;
  1474. }
  1475. else
  1476. {
  1477. return HAL_BUSY;
  1478. }
  1479. }
  1480. /**
  1481. * @brief Receive in slave mode an amount of data in non-blocking mode with Interrupt
  1482. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  1483. * the configuration information for the specified FMPI2C.
  1484. * @param pData Pointer to data buffer
  1485. * @param Size Amount of data to be sent
  1486. * @retval HAL status
  1487. */
  1488. HAL_StatusTypeDef HAL_FMPI2C_Slave_Receive_IT(FMPI2C_HandleTypeDef *hfmpi2c, uint8_t *pData, uint16_t Size)
  1489. {
  1490. if (hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1491. {
  1492. /* Process Locked */
  1493. __HAL_LOCK(hfmpi2c);
  1494. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_RX;
  1495. hfmpi2c->Mode = HAL_FMPI2C_MODE_SLAVE;
  1496. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1497. /* Enable Address Acknowledge */
  1498. hfmpi2c->Instance->CR2 &= ~FMPI2C_CR2_NACK;
  1499. /* Prepare transfer parameters */
  1500. hfmpi2c->pBuffPtr = pData;
  1501. hfmpi2c->XferCount = Size;
  1502. hfmpi2c->XferSize = hfmpi2c->XferCount;
  1503. hfmpi2c->XferOptions = FMPI2C_NO_OPTION_FRAME;
  1504. hfmpi2c->XferISR = FMPI2C_Slave_ISR_IT;
  1505. /* Process Unlocked */
  1506. __HAL_UNLOCK(hfmpi2c);
  1507. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  1508. to avoid the risk of FMPI2C interrupt handle execution before current
  1509. process unlock */
  1510. /* Enable ERR, TC, STOP, NACK, RXI interrupt */
  1511. /* possible to enable all of these */
  1512. /* FMPI2C_IT_ERRI | FMPI2C_IT_TCI| FMPI2C_IT_STOPI| FMPI2C_IT_NACKI | FMPI2C_IT_ADDRI | FMPI2C_IT_RXI | FMPI2C_IT_TXI */
  1513. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_RX_IT | FMPI2C_XFER_LISTEN_IT);
  1514. return HAL_OK;
  1515. }
  1516. else
  1517. {
  1518. return HAL_BUSY;
  1519. }
  1520. }
  1521. /**
  1522. * @brief Transmit in master mode an amount of data in non-blocking mode with DMA
  1523. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  1524. * the configuration information for the specified FMPI2C.
  1525. * @param DevAddress Target device address: The device 7 bits address value
  1526. * in datasheet must be shifted to the left before calling the interface
  1527. * @param pData Pointer to data buffer
  1528. * @param Size Amount of data to be sent
  1529. * @retval HAL status
  1530. */
  1531. HAL_StatusTypeDef HAL_FMPI2C_Master_Transmit_DMA(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint8_t *pData,
  1532. uint16_t Size)
  1533. {
  1534. uint32_t xfermode;
  1535. HAL_StatusTypeDef dmaxferstatus;
  1536. if (hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1537. {
  1538. if (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_BUSY) == SET)
  1539. {
  1540. return HAL_BUSY;
  1541. }
  1542. /* Process Locked */
  1543. __HAL_LOCK(hfmpi2c);
  1544. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_TX;
  1545. hfmpi2c->Mode = HAL_FMPI2C_MODE_MASTER;
  1546. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1547. /* Prepare transfer parameters */
  1548. hfmpi2c->pBuffPtr = pData;
  1549. hfmpi2c->XferCount = Size;
  1550. hfmpi2c->XferOptions = FMPI2C_NO_OPTION_FRAME;
  1551. hfmpi2c->XferISR = FMPI2C_Master_ISR_DMA;
  1552. if (hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  1553. {
  1554. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  1555. xfermode = FMPI2C_RELOAD_MODE;
  1556. }
  1557. else
  1558. {
  1559. hfmpi2c->XferSize = hfmpi2c->XferCount;
  1560. xfermode = FMPI2C_AUTOEND_MODE;
  1561. }
  1562. if (hfmpi2c->XferSize > 0U)
  1563. {
  1564. if (hfmpi2c->hdmatx != NULL)
  1565. {
  1566. /* Set the FMPI2C DMA transfer complete callback */
  1567. hfmpi2c->hdmatx->XferCpltCallback = FMPI2C_DMAMasterTransmitCplt;
  1568. /* Set the DMA error callback */
  1569. hfmpi2c->hdmatx->XferErrorCallback = FMPI2C_DMAError;
  1570. /* Set the unused DMA callbacks to NULL */
  1571. hfmpi2c->hdmatx->XferHalfCpltCallback = NULL;
  1572. hfmpi2c->hdmatx->XferAbortCallback = NULL;
  1573. /* Enable the DMA stream */
  1574. dmaxferstatus = HAL_DMA_Start_IT(hfmpi2c->hdmatx, (uint32_t)pData, (uint32_t)&hfmpi2c->Instance->TXDR, hfmpi2c->XferSize);
  1575. }
  1576. else
  1577. {
  1578. /* Update FMPI2C state */
  1579. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  1580. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  1581. /* Update FMPI2C error code */
  1582. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_DMA_PARAM;
  1583. /* Process Unlocked */
  1584. __HAL_UNLOCK(hfmpi2c);
  1585. return HAL_ERROR;
  1586. }
  1587. if (dmaxferstatus == HAL_OK)
  1588. {
  1589. /* Send Slave Address */
  1590. /* Set NBYTES to write and reload if hfmpi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
  1591. FMPI2C_TransferConfig(hfmpi2c, DevAddress, (uint8_t)hfmpi2c->XferSize, xfermode, FMPI2C_GENERATE_START_WRITE);
  1592. /* Update XferCount value */
  1593. hfmpi2c->XferCount -= hfmpi2c->XferSize;
  1594. /* Process Unlocked */
  1595. __HAL_UNLOCK(hfmpi2c);
  1596. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  1597. to avoid the risk of FMPI2C interrupt handle execution before current
  1598. process unlock */
  1599. /* Enable ERR and NACK interrupts */
  1600. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_ERROR_IT);
  1601. /* Enable DMA Request */
  1602. hfmpi2c->Instance->CR1 |= FMPI2C_CR1_TXDMAEN;
  1603. }
  1604. else
  1605. {
  1606. /* Update FMPI2C state */
  1607. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  1608. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  1609. /* Update FMPI2C error code */
  1610. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_DMA;
  1611. /* Process Unlocked */
  1612. __HAL_UNLOCK(hfmpi2c);
  1613. return HAL_ERROR;
  1614. }
  1615. }
  1616. else
  1617. {
  1618. /* Update Transfer ISR function pointer */
  1619. hfmpi2c->XferISR = FMPI2C_Master_ISR_IT;
  1620. /* Send Slave Address */
  1621. /* Set NBYTES to write and generate START condition */
  1622. FMPI2C_TransferConfig(hfmpi2c, DevAddress, (uint8_t)hfmpi2c->XferSize, FMPI2C_AUTOEND_MODE, FMPI2C_GENERATE_START_WRITE);
  1623. /* Process Unlocked */
  1624. __HAL_UNLOCK(hfmpi2c);
  1625. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  1626. to avoid the risk of FMPI2C interrupt handle execution before current
  1627. process unlock */
  1628. /* Enable ERR, TC, STOP, NACK, TXI interrupt */
  1629. /* possible to enable all of these */
  1630. /* FMPI2C_IT_ERRI | FMPI2C_IT_TCI| FMPI2C_IT_STOPI| FMPI2C_IT_NACKI | FMPI2C_IT_ADDRI | FMPI2C_IT_RXI | FMPI2C_IT_TXI */
  1631. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_TX_IT);
  1632. }
  1633. return HAL_OK;
  1634. }
  1635. else
  1636. {
  1637. return HAL_BUSY;
  1638. }
  1639. }
  1640. /**
  1641. * @brief Receive in master mode an amount of data in non-blocking mode with DMA
  1642. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  1643. * the configuration information for the specified FMPI2C.
  1644. * @param DevAddress Target device address: The device 7 bits address value
  1645. * in datasheet must be shifted to the left before calling the interface
  1646. * @param pData Pointer to data buffer
  1647. * @param Size Amount of data to be sent
  1648. * @retval HAL status
  1649. */
  1650. HAL_StatusTypeDef HAL_FMPI2C_Master_Receive_DMA(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint8_t *pData,
  1651. uint16_t Size)
  1652. {
  1653. uint32_t xfermode;
  1654. HAL_StatusTypeDef dmaxferstatus;
  1655. if (hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1656. {
  1657. if (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_BUSY) == SET)
  1658. {
  1659. return HAL_BUSY;
  1660. }
  1661. /* Process Locked */
  1662. __HAL_LOCK(hfmpi2c);
  1663. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_RX;
  1664. hfmpi2c->Mode = HAL_FMPI2C_MODE_MASTER;
  1665. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1666. /* Prepare transfer parameters */
  1667. hfmpi2c->pBuffPtr = pData;
  1668. hfmpi2c->XferCount = Size;
  1669. hfmpi2c->XferOptions = FMPI2C_NO_OPTION_FRAME;
  1670. hfmpi2c->XferISR = FMPI2C_Master_ISR_DMA;
  1671. if (hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  1672. {
  1673. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  1674. xfermode = FMPI2C_RELOAD_MODE;
  1675. }
  1676. else
  1677. {
  1678. hfmpi2c->XferSize = hfmpi2c->XferCount;
  1679. xfermode = FMPI2C_AUTOEND_MODE;
  1680. }
  1681. if (hfmpi2c->XferSize > 0U)
  1682. {
  1683. if (hfmpi2c->hdmarx != NULL)
  1684. {
  1685. /* Set the FMPI2C DMA transfer complete callback */
  1686. hfmpi2c->hdmarx->XferCpltCallback = FMPI2C_DMAMasterReceiveCplt;
  1687. /* Set the DMA error callback */
  1688. hfmpi2c->hdmarx->XferErrorCallback = FMPI2C_DMAError;
  1689. /* Set the unused DMA callbacks to NULL */
  1690. hfmpi2c->hdmarx->XferHalfCpltCallback = NULL;
  1691. hfmpi2c->hdmarx->XferAbortCallback = NULL;
  1692. /* Enable the DMA stream */
  1693. dmaxferstatus = HAL_DMA_Start_IT(hfmpi2c->hdmarx, (uint32_t)&hfmpi2c->Instance->RXDR, (uint32_t)pData, hfmpi2c->XferSize);
  1694. }
  1695. else
  1696. {
  1697. /* Update FMPI2C state */
  1698. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  1699. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  1700. /* Update FMPI2C error code */
  1701. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_DMA_PARAM;
  1702. /* Process Unlocked */
  1703. __HAL_UNLOCK(hfmpi2c);
  1704. return HAL_ERROR;
  1705. }
  1706. if (dmaxferstatus == HAL_OK)
  1707. {
  1708. /* Send Slave Address */
  1709. /* Set NBYTES to read and reload if hfmpi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
  1710. FMPI2C_TransferConfig(hfmpi2c, DevAddress, (uint8_t)hfmpi2c->XferSize, xfermode, FMPI2C_GENERATE_START_READ);
  1711. /* Update XferCount value */
  1712. hfmpi2c->XferCount -= hfmpi2c->XferSize;
  1713. /* Process Unlocked */
  1714. __HAL_UNLOCK(hfmpi2c);
  1715. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  1716. to avoid the risk of FMPI2C interrupt handle execution before current
  1717. process unlock */
  1718. /* Enable ERR and NACK interrupts */
  1719. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_ERROR_IT);
  1720. /* Enable DMA Request */
  1721. hfmpi2c->Instance->CR1 |= FMPI2C_CR1_RXDMAEN;
  1722. }
  1723. else
  1724. {
  1725. /* Update FMPI2C state */
  1726. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  1727. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  1728. /* Update FMPI2C error code */
  1729. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_DMA;
  1730. /* Process Unlocked */
  1731. __HAL_UNLOCK(hfmpi2c);
  1732. return HAL_ERROR;
  1733. }
  1734. }
  1735. else
  1736. {
  1737. /* Update Transfer ISR function pointer */
  1738. hfmpi2c->XferISR = FMPI2C_Master_ISR_IT;
  1739. /* Send Slave Address */
  1740. /* Set NBYTES to read and generate START condition */
  1741. FMPI2C_TransferConfig(hfmpi2c, DevAddress, (uint8_t)hfmpi2c->XferSize, FMPI2C_AUTOEND_MODE, FMPI2C_GENERATE_START_READ);
  1742. /* Process Unlocked */
  1743. __HAL_UNLOCK(hfmpi2c);
  1744. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  1745. to avoid the risk of FMPI2C interrupt handle execution before current
  1746. process unlock */
  1747. /* Enable ERR, TC, STOP, NACK, TXI interrupt */
  1748. /* possible to enable all of these */
  1749. /* FMPI2C_IT_ERRI | FMPI2C_IT_TCI| FMPI2C_IT_STOPI| FMPI2C_IT_NACKI | FMPI2C_IT_ADDRI | FMPI2C_IT_RXI | FMPI2C_IT_TXI */
  1750. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_TX_IT);
  1751. }
  1752. return HAL_OK;
  1753. }
  1754. else
  1755. {
  1756. return HAL_BUSY;
  1757. }
  1758. }
  1759. /**
  1760. * @brief Transmit in slave mode an amount of data in non-blocking mode with DMA
  1761. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  1762. * the configuration information for the specified FMPI2C.
  1763. * @param pData Pointer to data buffer
  1764. * @param Size Amount of data to be sent
  1765. * @retval HAL status
  1766. */
  1767. HAL_StatusTypeDef HAL_FMPI2C_Slave_Transmit_DMA(FMPI2C_HandleTypeDef *hfmpi2c, uint8_t *pData, uint16_t Size)
  1768. {
  1769. HAL_StatusTypeDef dmaxferstatus;
  1770. if (hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1771. {
  1772. if ((pData == NULL) || (Size == 0U))
  1773. {
  1774. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_INVALID_PARAM;
  1775. return HAL_ERROR;
  1776. }
  1777. /* Process Locked */
  1778. __HAL_LOCK(hfmpi2c);
  1779. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_TX;
  1780. hfmpi2c->Mode = HAL_FMPI2C_MODE_SLAVE;
  1781. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1782. /* Prepare transfer parameters */
  1783. hfmpi2c->pBuffPtr = pData;
  1784. hfmpi2c->XferCount = Size;
  1785. hfmpi2c->XferSize = hfmpi2c->XferCount;
  1786. hfmpi2c->XferOptions = FMPI2C_NO_OPTION_FRAME;
  1787. hfmpi2c->XferISR = FMPI2C_Slave_ISR_DMA;
  1788. if (hfmpi2c->hdmatx != NULL)
  1789. {
  1790. /* Set the FMPI2C DMA transfer complete callback */
  1791. hfmpi2c->hdmatx->XferCpltCallback = FMPI2C_DMASlaveTransmitCplt;
  1792. /* Set the DMA error callback */
  1793. hfmpi2c->hdmatx->XferErrorCallback = FMPI2C_DMAError;
  1794. /* Set the unused DMA callbacks to NULL */
  1795. hfmpi2c->hdmatx->XferHalfCpltCallback = NULL;
  1796. hfmpi2c->hdmatx->XferAbortCallback = NULL;
  1797. /* Enable the DMA stream */
  1798. dmaxferstatus = HAL_DMA_Start_IT(hfmpi2c->hdmatx, (uint32_t)pData, (uint32_t)&hfmpi2c->Instance->TXDR, hfmpi2c->XferSize);
  1799. }
  1800. else
  1801. {
  1802. /* Update FMPI2C state */
  1803. hfmpi2c->State = HAL_FMPI2C_STATE_LISTEN;
  1804. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  1805. /* Update FMPI2C error code */
  1806. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_DMA_PARAM;
  1807. /* Process Unlocked */
  1808. __HAL_UNLOCK(hfmpi2c);
  1809. return HAL_ERROR;
  1810. }
  1811. if (dmaxferstatus == HAL_OK)
  1812. {
  1813. /* Enable Address Acknowledge */
  1814. hfmpi2c->Instance->CR2 &= ~FMPI2C_CR2_NACK;
  1815. /* Process Unlocked */
  1816. __HAL_UNLOCK(hfmpi2c);
  1817. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  1818. to avoid the risk of FMPI2C interrupt handle execution before current
  1819. process unlock */
  1820. /* Enable ERR, STOP, NACK, ADDR interrupts */
  1821. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_LISTEN_IT);
  1822. /* Enable DMA Request */
  1823. hfmpi2c->Instance->CR1 |= FMPI2C_CR1_TXDMAEN;
  1824. }
  1825. else
  1826. {
  1827. /* Update FMPI2C state */
  1828. hfmpi2c->State = HAL_FMPI2C_STATE_LISTEN;
  1829. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  1830. /* Update FMPI2C error code */
  1831. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_DMA;
  1832. /* Process Unlocked */
  1833. __HAL_UNLOCK(hfmpi2c);
  1834. return HAL_ERROR;
  1835. }
  1836. return HAL_OK;
  1837. }
  1838. else
  1839. {
  1840. return HAL_BUSY;
  1841. }
  1842. }
  1843. /**
  1844. * @brief Receive in slave mode an amount of data in non-blocking mode with DMA
  1845. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  1846. * the configuration information for the specified FMPI2C.
  1847. * @param pData Pointer to data buffer
  1848. * @param Size Amount of data to be sent
  1849. * @retval HAL status
  1850. */
  1851. HAL_StatusTypeDef HAL_FMPI2C_Slave_Receive_DMA(FMPI2C_HandleTypeDef *hfmpi2c, uint8_t *pData, uint16_t Size)
  1852. {
  1853. HAL_StatusTypeDef dmaxferstatus;
  1854. if (hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1855. {
  1856. if ((pData == NULL) || (Size == 0U))
  1857. {
  1858. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_INVALID_PARAM;
  1859. return HAL_ERROR;
  1860. }
  1861. /* Process Locked */
  1862. __HAL_LOCK(hfmpi2c);
  1863. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_RX;
  1864. hfmpi2c->Mode = HAL_FMPI2C_MODE_SLAVE;
  1865. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1866. /* Prepare transfer parameters */
  1867. hfmpi2c->pBuffPtr = pData;
  1868. hfmpi2c->XferCount = Size;
  1869. hfmpi2c->XferSize = hfmpi2c->XferCount;
  1870. hfmpi2c->XferOptions = FMPI2C_NO_OPTION_FRAME;
  1871. hfmpi2c->XferISR = FMPI2C_Slave_ISR_DMA;
  1872. if (hfmpi2c->hdmarx != NULL)
  1873. {
  1874. /* Set the FMPI2C DMA transfer complete callback */
  1875. hfmpi2c->hdmarx->XferCpltCallback = FMPI2C_DMASlaveReceiveCplt;
  1876. /* Set the DMA error callback */
  1877. hfmpi2c->hdmarx->XferErrorCallback = FMPI2C_DMAError;
  1878. /* Set the unused DMA callbacks to NULL */
  1879. hfmpi2c->hdmarx->XferHalfCpltCallback = NULL;
  1880. hfmpi2c->hdmarx->XferAbortCallback = NULL;
  1881. /* Enable the DMA stream */
  1882. dmaxferstatus = HAL_DMA_Start_IT(hfmpi2c->hdmarx, (uint32_t)&hfmpi2c->Instance->RXDR, (uint32_t)pData, hfmpi2c->XferSize);
  1883. }
  1884. else
  1885. {
  1886. /* Update FMPI2C state */
  1887. hfmpi2c->State = HAL_FMPI2C_STATE_LISTEN;
  1888. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  1889. /* Update FMPI2C error code */
  1890. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_DMA_PARAM;
  1891. /* Process Unlocked */
  1892. __HAL_UNLOCK(hfmpi2c);
  1893. return HAL_ERROR;
  1894. }
  1895. if (dmaxferstatus == HAL_OK)
  1896. {
  1897. /* Enable Address Acknowledge */
  1898. hfmpi2c->Instance->CR2 &= ~FMPI2C_CR2_NACK;
  1899. /* Process Unlocked */
  1900. __HAL_UNLOCK(hfmpi2c);
  1901. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  1902. to avoid the risk of FMPI2C interrupt handle execution before current
  1903. process unlock */
  1904. /* Enable ERR, STOP, NACK, ADDR interrupts */
  1905. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_LISTEN_IT);
  1906. /* Enable DMA Request */
  1907. hfmpi2c->Instance->CR1 |= FMPI2C_CR1_RXDMAEN;
  1908. }
  1909. else
  1910. {
  1911. /* Update FMPI2C state */
  1912. hfmpi2c->State = HAL_FMPI2C_STATE_LISTEN;
  1913. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  1914. /* Update FMPI2C error code */
  1915. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_DMA;
  1916. /* Process Unlocked */
  1917. __HAL_UNLOCK(hfmpi2c);
  1918. return HAL_ERROR;
  1919. }
  1920. return HAL_OK;
  1921. }
  1922. else
  1923. {
  1924. return HAL_BUSY;
  1925. }
  1926. }
  1927. /**
  1928. * @brief Write an amount of data in blocking mode to a specific memory address
  1929. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  1930. * the configuration information for the specified FMPI2C.
  1931. * @param DevAddress Target device address: The device 7 bits address value
  1932. * in datasheet must be shifted to the left before calling the interface
  1933. * @param MemAddress Internal memory address
  1934. * @param MemAddSize Size of internal memory address
  1935. * @param pData Pointer to data buffer
  1936. * @param Size Amount of data to be sent
  1937. * @param Timeout Timeout duration
  1938. * @retval HAL status
  1939. */
  1940. HAL_StatusTypeDef HAL_FMPI2C_Mem_Write(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint16_t MemAddress,
  1941. uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  1942. {
  1943. uint32_t tickstart;
  1944. /* Check the parameters */
  1945. assert_param(IS_FMPI2C_MEMADD_SIZE(MemAddSize));
  1946. if (hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1947. {
  1948. if ((pData == NULL) || (Size == 0U))
  1949. {
  1950. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_INVALID_PARAM;
  1951. return HAL_ERROR;
  1952. }
  1953. /* Process Locked */
  1954. __HAL_LOCK(hfmpi2c);
  1955. /* Init tickstart for timeout management*/
  1956. tickstart = HAL_GetTick();
  1957. if (FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_BUSY, SET, FMPI2C_TIMEOUT_BUSY, tickstart) != HAL_OK)
  1958. {
  1959. return HAL_ERROR;
  1960. }
  1961. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_TX;
  1962. hfmpi2c->Mode = HAL_FMPI2C_MODE_MEM;
  1963. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1964. /* Prepare transfer parameters */
  1965. hfmpi2c->pBuffPtr = pData;
  1966. hfmpi2c->XferCount = Size;
  1967. hfmpi2c->XferISR = NULL;
  1968. /* Send Slave Address and Memory Address */
  1969. if (FMPI2C_RequestMemoryWrite(hfmpi2c, DevAddress, MemAddress, MemAddSize, Timeout, tickstart) != HAL_OK)
  1970. {
  1971. /* Process Unlocked */
  1972. __HAL_UNLOCK(hfmpi2c);
  1973. return HAL_ERROR;
  1974. }
  1975. /* Set NBYTES to write and reload if hfmpi2c->XferCount > MAX_NBYTE_SIZE */
  1976. if (hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  1977. {
  1978. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  1979. FMPI2C_TransferConfig(hfmpi2c, DevAddress, (uint8_t)hfmpi2c->XferSize, FMPI2C_RELOAD_MODE, FMPI2C_NO_STARTSTOP);
  1980. }
  1981. else
  1982. {
  1983. hfmpi2c->XferSize = hfmpi2c->XferCount;
  1984. FMPI2C_TransferConfig(hfmpi2c, DevAddress, (uint8_t)hfmpi2c->XferSize, FMPI2C_AUTOEND_MODE, FMPI2C_NO_STARTSTOP);
  1985. }
  1986. do
  1987. {
  1988. /* Wait until TXIS flag is set */
  1989. if (FMPI2C_WaitOnTXISFlagUntilTimeout(hfmpi2c, Timeout, tickstart) != HAL_OK)
  1990. {
  1991. return HAL_ERROR;
  1992. }
  1993. /* Write data to TXDR */
  1994. hfmpi2c->Instance->TXDR = *hfmpi2c->pBuffPtr;
  1995. /* Increment Buffer pointer */
  1996. hfmpi2c->pBuffPtr++;
  1997. hfmpi2c->XferCount--;
  1998. hfmpi2c->XferSize--;
  1999. if ((hfmpi2c->XferCount != 0U) && (hfmpi2c->XferSize == 0U))
  2000. {
  2001. /* Wait until TCR flag is set */
  2002. if (FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_TCR, RESET, Timeout, tickstart) != HAL_OK)
  2003. {
  2004. return HAL_ERROR;
  2005. }
  2006. if (hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  2007. {
  2008. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  2009. FMPI2C_TransferConfig(hfmpi2c, DevAddress, (uint8_t)hfmpi2c->XferSize, FMPI2C_RELOAD_MODE, FMPI2C_NO_STARTSTOP);
  2010. }
  2011. else
  2012. {
  2013. hfmpi2c->XferSize = hfmpi2c->XferCount;
  2014. FMPI2C_TransferConfig(hfmpi2c, DevAddress, (uint8_t)hfmpi2c->XferSize, FMPI2C_AUTOEND_MODE, FMPI2C_NO_STARTSTOP);
  2015. }
  2016. }
  2017. } while (hfmpi2c->XferCount > 0U);
  2018. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  2019. /* Wait until STOPF flag is reset */
  2020. if (FMPI2C_WaitOnSTOPFlagUntilTimeout(hfmpi2c, Timeout, tickstart) != HAL_OK)
  2021. {
  2022. return HAL_ERROR;
  2023. }
  2024. /* Clear STOP Flag */
  2025. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  2026. /* Clear Configuration Register 2 */
  2027. FMPI2C_RESET_CR2(hfmpi2c);
  2028. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  2029. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  2030. /* Process Unlocked */
  2031. __HAL_UNLOCK(hfmpi2c);
  2032. return HAL_OK;
  2033. }
  2034. else
  2035. {
  2036. return HAL_BUSY;
  2037. }
  2038. }
  2039. /**
  2040. * @brief Read an amount of data in blocking mode from a specific memory address
  2041. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2042. * the configuration information for the specified FMPI2C.
  2043. * @param DevAddress Target device address: The device 7 bits address value
  2044. * in datasheet must be shifted to the left before calling the interface
  2045. * @param MemAddress Internal memory address
  2046. * @param MemAddSize Size of internal memory address
  2047. * @param pData Pointer to data buffer
  2048. * @param Size Amount of data to be sent
  2049. * @param Timeout Timeout duration
  2050. * @retval HAL status
  2051. */
  2052. HAL_StatusTypeDef HAL_FMPI2C_Mem_Read(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint16_t MemAddress,
  2053. uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  2054. {
  2055. uint32_t tickstart;
  2056. /* Check the parameters */
  2057. assert_param(IS_FMPI2C_MEMADD_SIZE(MemAddSize));
  2058. if (hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  2059. {
  2060. if ((pData == NULL) || (Size == 0U))
  2061. {
  2062. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_INVALID_PARAM;
  2063. return HAL_ERROR;
  2064. }
  2065. /* Process Locked */
  2066. __HAL_LOCK(hfmpi2c);
  2067. /* Init tickstart for timeout management*/
  2068. tickstart = HAL_GetTick();
  2069. if (FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_BUSY, SET, FMPI2C_TIMEOUT_BUSY, tickstart) != HAL_OK)
  2070. {
  2071. return HAL_ERROR;
  2072. }
  2073. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_RX;
  2074. hfmpi2c->Mode = HAL_FMPI2C_MODE_MEM;
  2075. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  2076. /* Prepare transfer parameters */
  2077. hfmpi2c->pBuffPtr = pData;
  2078. hfmpi2c->XferCount = Size;
  2079. hfmpi2c->XferISR = NULL;
  2080. /* Send Slave Address and Memory Address */
  2081. if (FMPI2C_RequestMemoryRead(hfmpi2c, DevAddress, MemAddress, MemAddSize, Timeout, tickstart) != HAL_OK)
  2082. {
  2083. /* Process Unlocked */
  2084. __HAL_UNLOCK(hfmpi2c);
  2085. return HAL_ERROR;
  2086. }
  2087. /* Send Slave Address */
  2088. /* Set NBYTES to write and reload if hfmpi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
  2089. if (hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  2090. {
  2091. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  2092. FMPI2C_TransferConfig(hfmpi2c, DevAddress, (uint8_t)hfmpi2c->XferSize, FMPI2C_RELOAD_MODE, FMPI2C_GENERATE_START_READ);
  2093. }
  2094. else
  2095. {
  2096. hfmpi2c->XferSize = hfmpi2c->XferCount;
  2097. FMPI2C_TransferConfig(hfmpi2c, DevAddress, (uint8_t)hfmpi2c->XferSize, FMPI2C_AUTOEND_MODE, FMPI2C_GENERATE_START_READ);
  2098. }
  2099. do
  2100. {
  2101. /* Wait until RXNE flag is set */
  2102. if (FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_RXNE, RESET, Timeout, tickstart) != HAL_OK)
  2103. {
  2104. return HAL_ERROR;
  2105. }
  2106. /* Read data from RXDR */
  2107. *hfmpi2c->pBuffPtr = (uint8_t)hfmpi2c->Instance->RXDR;
  2108. /* Increment Buffer pointer */
  2109. hfmpi2c->pBuffPtr++;
  2110. hfmpi2c->XferSize--;
  2111. hfmpi2c->XferCount--;
  2112. if ((hfmpi2c->XferCount != 0U) && (hfmpi2c->XferSize == 0U))
  2113. {
  2114. /* Wait until TCR flag is set */
  2115. if (FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_TCR, RESET, Timeout, tickstart) != HAL_OK)
  2116. {
  2117. return HAL_ERROR;
  2118. }
  2119. if (hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  2120. {
  2121. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  2122. FMPI2C_TransferConfig(hfmpi2c, DevAddress, (uint8_t) hfmpi2c->XferSize, FMPI2C_RELOAD_MODE, FMPI2C_NO_STARTSTOP);
  2123. }
  2124. else
  2125. {
  2126. hfmpi2c->XferSize = hfmpi2c->XferCount;
  2127. FMPI2C_TransferConfig(hfmpi2c, DevAddress, (uint8_t)hfmpi2c->XferSize, FMPI2C_AUTOEND_MODE, FMPI2C_NO_STARTSTOP);
  2128. }
  2129. }
  2130. } while (hfmpi2c->XferCount > 0U);
  2131. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  2132. /* Wait until STOPF flag is reset */
  2133. if (FMPI2C_WaitOnSTOPFlagUntilTimeout(hfmpi2c, Timeout, tickstart) != HAL_OK)
  2134. {
  2135. return HAL_ERROR;
  2136. }
  2137. /* Clear STOP Flag */
  2138. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  2139. /* Clear Configuration Register 2 */
  2140. FMPI2C_RESET_CR2(hfmpi2c);
  2141. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  2142. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  2143. /* Process Unlocked */
  2144. __HAL_UNLOCK(hfmpi2c);
  2145. return HAL_OK;
  2146. }
  2147. else
  2148. {
  2149. return HAL_BUSY;
  2150. }
  2151. }
  2152. /**
  2153. * @brief Write an amount of data in non-blocking mode with Interrupt to a specific memory address
  2154. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2155. * the configuration information for the specified FMPI2C.
  2156. * @param DevAddress Target device address: The device 7 bits address value
  2157. * in datasheet must be shifted to the left before calling the interface
  2158. * @param MemAddress Internal memory address
  2159. * @param MemAddSize Size of internal memory address
  2160. * @param pData Pointer to data buffer
  2161. * @param Size Amount of data to be sent
  2162. * @retval HAL status
  2163. */
  2164. HAL_StatusTypeDef HAL_FMPI2C_Mem_Write_IT(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint16_t MemAddress,
  2165. uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
  2166. {
  2167. uint32_t tickstart;
  2168. uint32_t xfermode;
  2169. /* Check the parameters */
  2170. assert_param(IS_FMPI2C_MEMADD_SIZE(MemAddSize));
  2171. if (hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  2172. {
  2173. if ((pData == NULL) || (Size == 0U))
  2174. {
  2175. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_INVALID_PARAM;
  2176. return HAL_ERROR;
  2177. }
  2178. if (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_BUSY) == SET)
  2179. {
  2180. return HAL_BUSY;
  2181. }
  2182. /* Process Locked */
  2183. __HAL_LOCK(hfmpi2c);
  2184. /* Init tickstart for timeout management*/
  2185. tickstart = HAL_GetTick();
  2186. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_TX;
  2187. hfmpi2c->Mode = HAL_FMPI2C_MODE_MEM;
  2188. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  2189. /* Prepare transfer parameters */
  2190. hfmpi2c->pBuffPtr = pData;
  2191. hfmpi2c->XferCount = Size;
  2192. hfmpi2c->XferOptions = FMPI2C_NO_OPTION_FRAME;
  2193. hfmpi2c->XferISR = FMPI2C_Master_ISR_IT;
  2194. if (hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  2195. {
  2196. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  2197. xfermode = FMPI2C_RELOAD_MODE;
  2198. }
  2199. else
  2200. {
  2201. hfmpi2c->XferSize = hfmpi2c->XferCount;
  2202. xfermode = FMPI2C_AUTOEND_MODE;
  2203. }
  2204. /* Send Slave Address and Memory Address */
  2205. if (FMPI2C_RequestMemoryWrite(hfmpi2c, DevAddress, MemAddress, MemAddSize, FMPI2C_TIMEOUT_FLAG, tickstart) != HAL_OK)
  2206. {
  2207. /* Process Unlocked */
  2208. __HAL_UNLOCK(hfmpi2c);
  2209. return HAL_ERROR;
  2210. }
  2211. /* Set NBYTES to write and reload if hfmpi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
  2212. FMPI2C_TransferConfig(hfmpi2c, DevAddress, (uint8_t)hfmpi2c->XferSize, xfermode, FMPI2C_NO_STARTSTOP);
  2213. /* Process Unlocked */
  2214. __HAL_UNLOCK(hfmpi2c);
  2215. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  2216. to avoid the risk of FMPI2C interrupt handle execution before current
  2217. process unlock */
  2218. /* Enable ERR, TC, STOP, NACK, TXI interrupt */
  2219. /* possible to enable all of these */
  2220. /* FMPI2C_IT_ERRI | FMPI2C_IT_TCI| FMPI2C_IT_STOPI| FMPI2C_IT_NACKI | FMPI2C_IT_ADDRI | FMPI2C_IT_RXI | FMPI2C_IT_TXI */
  2221. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_TX_IT);
  2222. return HAL_OK;
  2223. }
  2224. else
  2225. {
  2226. return HAL_BUSY;
  2227. }
  2228. }
  2229. /**
  2230. * @brief Read an amount of data in non-blocking mode with Interrupt from a specific memory address
  2231. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2232. * the configuration information for the specified FMPI2C.
  2233. * @param DevAddress Target device address: The device 7 bits address value
  2234. * in datasheet must be shifted to the left before calling the interface
  2235. * @param MemAddress Internal memory address
  2236. * @param MemAddSize Size of internal memory address
  2237. * @param pData Pointer to data buffer
  2238. * @param Size Amount of data to be sent
  2239. * @retval HAL status
  2240. */
  2241. HAL_StatusTypeDef HAL_FMPI2C_Mem_Read_IT(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint16_t MemAddress,
  2242. uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
  2243. {
  2244. uint32_t tickstart;
  2245. uint32_t xfermode;
  2246. /* Check the parameters */
  2247. assert_param(IS_FMPI2C_MEMADD_SIZE(MemAddSize));
  2248. if (hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  2249. {
  2250. if ((pData == NULL) || (Size == 0U))
  2251. {
  2252. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_INVALID_PARAM;
  2253. return HAL_ERROR;
  2254. }
  2255. if (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_BUSY) == SET)
  2256. {
  2257. return HAL_BUSY;
  2258. }
  2259. /* Process Locked */
  2260. __HAL_LOCK(hfmpi2c);
  2261. /* Init tickstart for timeout management*/
  2262. tickstart = HAL_GetTick();
  2263. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_RX;
  2264. hfmpi2c->Mode = HAL_FMPI2C_MODE_MEM;
  2265. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  2266. /* Prepare transfer parameters */
  2267. hfmpi2c->pBuffPtr = pData;
  2268. hfmpi2c->XferCount = Size;
  2269. hfmpi2c->XferOptions = FMPI2C_NO_OPTION_FRAME;
  2270. hfmpi2c->XferISR = FMPI2C_Master_ISR_IT;
  2271. if (hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  2272. {
  2273. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  2274. xfermode = FMPI2C_RELOAD_MODE;
  2275. }
  2276. else
  2277. {
  2278. hfmpi2c->XferSize = hfmpi2c->XferCount;
  2279. xfermode = FMPI2C_AUTOEND_MODE;
  2280. }
  2281. /* Send Slave Address and Memory Address */
  2282. if (FMPI2C_RequestMemoryRead(hfmpi2c, DevAddress, MemAddress, MemAddSize, FMPI2C_TIMEOUT_FLAG, tickstart) != HAL_OK)
  2283. {
  2284. /* Process Unlocked */
  2285. __HAL_UNLOCK(hfmpi2c);
  2286. return HAL_ERROR;
  2287. }
  2288. /* Set NBYTES to write and reload if hfmpi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
  2289. FMPI2C_TransferConfig(hfmpi2c, DevAddress, (uint8_t)hfmpi2c->XferSize, xfermode, FMPI2C_GENERATE_START_READ);
  2290. /* Process Unlocked */
  2291. __HAL_UNLOCK(hfmpi2c);
  2292. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  2293. to avoid the risk of FMPI2C interrupt handle execution before current
  2294. process unlock */
  2295. /* Enable ERR, TC, STOP, NACK, RXI interrupt */
  2296. /* possible to enable all of these */
  2297. /* FMPI2C_IT_ERRI | FMPI2C_IT_TCI| FMPI2C_IT_STOPI| FMPI2C_IT_NACKI | FMPI2C_IT_ADDRI | FMPI2C_IT_RXI | FMPI2C_IT_TXI */
  2298. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_RX_IT);
  2299. return HAL_OK;
  2300. }
  2301. else
  2302. {
  2303. return HAL_BUSY;
  2304. }
  2305. }
  2306. /**
  2307. * @brief Write an amount of data in non-blocking mode with DMA to a specific memory address
  2308. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2309. * the configuration information for the specified FMPI2C.
  2310. * @param DevAddress Target device address: The device 7 bits address value
  2311. * in datasheet must be shifted to the left before calling the interface
  2312. * @param MemAddress Internal memory address
  2313. * @param MemAddSize Size of internal memory address
  2314. * @param pData Pointer to data buffer
  2315. * @param Size Amount of data to be sent
  2316. * @retval HAL status
  2317. */
  2318. HAL_StatusTypeDef HAL_FMPI2C_Mem_Write_DMA(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint16_t MemAddress,
  2319. uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
  2320. {
  2321. uint32_t tickstart;
  2322. uint32_t xfermode;
  2323. HAL_StatusTypeDef dmaxferstatus;
  2324. /* Check the parameters */
  2325. assert_param(IS_FMPI2C_MEMADD_SIZE(MemAddSize));
  2326. if (hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  2327. {
  2328. if ((pData == NULL) || (Size == 0U))
  2329. {
  2330. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_INVALID_PARAM;
  2331. return HAL_ERROR;
  2332. }
  2333. if (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_BUSY) == SET)
  2334. {
  2335. return HAL_BUSY;
  2336. }
  2337. /* Process Locked */
  2338. __HAL_LOCK(hfmpi2c);
  2339. /* Init tickstart for timeout management*/
  2340. tickstart = HAL_GetTick();
  2341. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_TX;
  2342. hfmpi2c->Mode = HAL_FMPI2C_MODE_MEM;
  2343. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  2344. /* Prepare transfer parameters */
  2345. hfmpi2c->pBuffPtr = pData;
  2346. hfmpi2c->XferCount = Size;
  2347. hfmpi2c->XferOptions = FMPI2C_NO_OPTION_FRAME;
  2348. hfmpi2c->XferISR = FMPI2C_Master_ISR_DMA;
  2349. if (hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  2350. {
  2351. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  2352. xfermode = FMPI2C_RELOAD_MODE;
  2353. }
  2354. else
  2355. {
  2356. hfmpi2c->XferSize = hfmpi2c->XferCount;
  2357. xfermode = FMPI2C_AUTOEND_MODE;
  2358. }
  2359. /* Send Slave Address and Memory Address */
  2360. if (FMPI2C_RequestMemoryWrite(hfmpi2c, DevAddress, MemAddress, MemAddSize, FMPI2C_TIMEOUT_FLAG, tickstart) != HAL_OK)
  2361. {
  2362. /* Process Unlocked */
  2363. __HAL_UNLOCK(hfmpi2c);
  2364. return HAL_ERROR;
  2365. }
  2366. if (hfmpi2c->hdmatx != NULL)
  2367. {
  2368. /* Set the FMPI2C DMA transfer complete callback */
  2369. hfmpi2c->hdmatx->XferCpltCallback = FMPI2C_DMAMasterTransmitCplt;
  2370. /* Set the DMA error callback */
  2371. hfmpi2c->hdmatx->XferErrorCallback = FMPI2C_DMAError;
  2372. /* Set the unused DMA callbacks to NULL */
  2373. hfmpi2c->hdmatx->XferHalfCpltCallback = NULL;
  2374. hfmpi2c->hdmatx->XferAbortCallback = NULL;
  2375. /* Enable the DMA stream */
  2376. dmaxferstatus = HAL_DMA_Start_IT(hfmpi2c->hdmatx, (uint32_t)pData, (uint32_t)&hfmpi2c->Instance->TXDR, hfmpi2c->XferSize);
  2377. }
  2378. else
  2379. {
  2380. /* Update FMPI2C state */
  2381. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  2382. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  2383. /* Update FMPI2C error code */
  2384. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_DMA_PARAM;
  2385. /* Process Unlocked */
  2386. __HAL_UNLOCK(hfmpi2c);
  2387. return HAL_ERROR;
  2388. }
  2389. if (dmaxferstatus == HAL_OK)
  2390. {
  2391. /* Send Slave Address */
  2392. /* Set NBYTES to write and reload if hfmpi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
  2393. FMPI2C_TransferConfig(hfmpi2c, DevAddress, (uint8_t)hfmpi2c->XferSize, xfermode, FMPI2C_NO_STARTSTOP);
  2394. /* Update XferCount value */
  2395. hfmpi2c->XferCount -= hfmpi2c->XferSize;
  2396. /* Process Unlocked */
  2397. __HAL_UNLOCK(hfmpi2c);
  2398. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  2399. to avoid the risk of FMPI2C interrupt handle execution before current
  2400. process unlock */
  2401. /* Enable ERR and NACK interrupts */
  2402. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_ERROR_IT);
  2403. /* Enable DMA Request */
  2404. hfmpi2c->Instance->CR1 |= FMPI2C_CR1_TXDMAEN;
  2405. }
  2406. else
  2407. {
  2408. /* Update FMPI2C state */
  2409. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  2410. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  2411. /* Update FMPI2C error code */
  2412. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_DMA;
  2413. /* Process Unlocked */
  2414. __HAL_UNLOCK(hfmpi2c);
  2415. return HAL_ERROR;
  2416. }
  2417. return HAL_OK;
  2418. }
  2419. else
  2420. {
  2421. return HAL_BUSY;
  2422. }
  2423. }
  2424. /**
  2425. * @brief Reads an amount of data in non-blocking mode with DMA from a specific memory address.
  2426. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2427. * the configuration information for the specified FMPI2C.
  2428. * @param DevAddress Target device address: The device 7 bits address value
  2429. * in datasheet must be shifted to the left before calling the interface
  2430. * @param MemAddress Internal memory address
  2431. * @param MemAddSize Size of internal memory address
  2432. * @param pData Pointer to data buffer
  2433. * @param Size Amount of data to be read
  2434. * @retval HAL status
  2435. */
  2436. HAL_StatusTypeDef HAL_FMPI2C_Mem_Read_DMA(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint16_t MemAddress,
  2437. uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
  2438. {
  2439. uint32_t tickstart;
  2440. uint32_t xfermode;
  2441. HAL_StatusTypeDef dmaxferstatus;
  2442. /* Check the parameters */
  2443. assert_param(IS_FMPI2C_MEMADD_SIZE(MemAddSize));
  2444. if (hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  2445. {
  2446. if ((pData == NULL) || (Size == 0U))
  2447. {
  2448. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_INVALID_PARAM;
  2449. return HAL_ERROR;
  2450. }
  2451. if (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_BUSY) == SET)
  2452. {
  2453. return HAL_BUSY;
  2454. }
  2455. /* Process Locked */
  2456. __HAL_LOCK(hfmpi2c);
  2457. /* Init tickstart for timeout management*/
  2458. tickstart = HAL_GetTick();
  2459. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_RX;
  2460. hfmpi2c->Mode = HAL_FMPI2C_MODE_MEM;
  2461. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  2462. /* Prepare transfer parameters */
  2463. hfmpi2c->pBuffPtr = pData;
  2464. hfmpi2c->XferCount = Size;
  2465. hfmpi2c->XferOptions = FMPI2C_NO_OPTION_FRAME;
  2466. hfmpi2c->XferISR = FMPI2C_Master_ISR_DMA;
  2467. if (hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  2468. {
  2469. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  2470. xfermode = FMPI2C_RELOAD_MODE;
  2471. }
  2472. else
  2473. {
  2474. hfmpi2c->XferSize = hfmpi2c->XferCount;
  2475. xfermode = FMPI2C_AUTOEND_MODE;
  2476. }
  2477. /* Send Slave Address and Memory Address */
  2478. if (FMPI2C_RequestMemoryRead(hfmpi2c, DevAddress, MemAddress, MemAddSize, FMPI2C_TIMEOUT_FLAG, tickstart) != HAL_OK)
  2479. {
  2480. /* Process Unlocked */
  2481. __HAL_UNLOCK(hfmpi2c);
  2482. return HAL_ERROR;
  2483. }
  2484. if (hfmpi2c->hdmarx != NULL)
  2485. {
  2486. /* Set the FMPI2C DMA transfer complete callback */
  2487. hfmpi2c->hdmarx->XferCpltCallback = FMPI2C_DMAMasterReceiveCplt;
  2488. /* Set the DMA error callback */
  2489. hfmpi2c->hdmarx->XferErrorCallback = FMPI2C_DMAError;
  2490. /* Set the unused DMA callbacks to NULL */
  2491. hfmpi2c->hdmarx->XferHalfCpltCallback = NULL;
  2492. hfmpi2c->hdmarx->XferAbortCallback = NULL;
  2493. /* Enable the DMA stream */
  2494. dmaxferstatus = HAL_DMA_Start_IT(hfmpi2c->hdmarx, (uint32_t)&hfmpi2c->Instance->RXDR, (uint32_t)pData, hfmpi2c->XferSize);
  2495. }
  2496. else
  2497. {
  2498. /* Update FMPI2C state */
  2499. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  2500. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  2501. /* Update FMPI2C error code */
  2502. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_DMA_PARAM;
  2503. /* Process Unlocked */
  2504. __HAL_UNLOCK(hfmpi2c);
  2505. return HAL_ERROR;
  2506. }
  2507. if (dmaxferstatus == HAL_OK)
  2508. {
  2509. /* Set NBYTES to write and reload if hfmpi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
  2510. FMPI2C_TransferConfig(hfmpi2c, DevAddress, (uint8_t)hfmpi2c->XferSize, xfermode, FMPI2C_GENERATE_START_READ);
  2511. /* Update XferCount value */
  2512. hfmpi2c->XferCount -= hfmpi2c->XferSize;
  2513. /* Process Unlocked */
  2514. __HAL_UNLOCK(hfmpi2c);
  2515. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  2516. to avoid the risk of FMPI2C interrupt handle execution before current
  2517. process unlock */
  2518. /* Enable ERR and NACK interrupts */
  2519. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_ERROR_IT);
  2520. /* Enable DMA Request */
  2521. hfmpi2c->Instance->CR1 |= FMPI2C_CR1_RXDMAEN;
  2522. }
  2523. else
  2524. {
  2525. /* Update FMPI2C state */
  2526. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  2527. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  2528. /* Update FMPI2C error code */
  2529. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_DMA;
  2530. /* Process Unlocked */
  2531. __HAL_UNLOCK(hfmpi2c);
  2532. return HAL_ERROR;
  2533. }
  2534. return HAL_OK;
  2535. }
  2536. else
  2537. {
  2538. return HAL_BUSY;
  2539. }
  2540. }
  2541. /**
  2542. * @brief Checks if target device is ready for communication.
  2543. * @note This function is used with Memory devices
  2544. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2545. * the configuration information for the specified FMPI2C.
  2546. * @param DevAddress Target device address: The device 7 bits address value
  2547. * in datasheet must be shifted to the left before calling the interface
  2548. * @param Trials Number of trials
  2549. * @param Timeout Timeout duration
  2550. * @retval HAL status
  2551. */
  2552. HAL_StatusTypeDef HAL_FMPI2C_IsDeviceReady(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint32_t Trials, uint32_t Timeout)
  2553. {
  2554. uint32_t tickstart;
  2555. __IO uint32_t FMPI2C_Trials = 0UL;
  2556. FlagStatus tmp1;
  2557. FlagStatus tmp2;
  2558. if (hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  2559. {
  2560. if (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_BUSY) == SET)
  2561. {
  2562. return HAL_BUSY;
  2563. }
  2564. /* Process Locked */
  2565. __HAL_LOCK(hfmpi2c);
  2566. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY;
  2567. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  2568. do
  2569. {
  2570. /* Generate Start */
  2571. hfmpi2c->Instance->CR2 = FMPI2C_GENERATE_START(hfmpi2c->Init.AddressingMode, DevAddress);
  2572. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  2573. /* Wait until STOPF flag is set or a NACK flag is set*/
  2574. tickstart = HAL_GetTick();
  2575. tmp1 = __HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  2576. tmp2 = __HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_AF);
  2577. while ((tmp1 == RESET) && (tmp2 == RESET))
  2578. {
  2579. if (Timeout != HAL_MAX_DELAY)
  2580. {
  2581. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  2582. {
  2583. /* Update FMPI2C state */
  2584. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  2585. /* Update FMPI2C error code */
  2586. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_TIMEOUT;
  2587. /* Process Unlocked */
  2588. __HAL_UNLOCK(hfmpi2c);
  2589. return HAL_ERROR;
  2590. }
  2591. }
  2592. tmp1 = __HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  2593. tmp2 = __HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_AF);
  2594. }
  2595. /* Check if the NACKF flag has not been set */
  2596. if (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_AF) == RESET)
  2597. {
  2598. /* Wait until STOPF flag is reset */
  2599. if (FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_STOPF, RESET, Timeout, tickstart) != HAL_OK)
  2600. {
  2601. return HAL_ERROR;
  2602. }
  2603. /* Clear STOP Flag */
  2604. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  2605. /* Device is ready */
  2606. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  2607. /* Process Unlocked */
  2608. __HAL_UNLOCK(hfmpi2c);
  2609. return HAL_OK;
  2610. }
  2611. else
  2612. {
  2613. /* Wait until STOPF flag is reset */
  2614. if (FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_STOPF, RESET, Timeout, tickstart) != HAL_OK)
  2615. {
  2616. return HAL_ERROR;
  2617. }
  2618. /* Clear NACK Flag */
  2619. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_AF);
  2620. /* Clear STOP Flag, auto generated with autoend*/
  2621. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  2622. }
  2623. /* Check if the maximum allowed number of trials has been reached */
  2624. if (FMPI2C_Trials == Trials)
  2625. {
  2626. /* Generate Stop */
  2627. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_STOP;
  2628. /* Wait until STOPF flag is reset */
  2629. if (FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_STOPF, RESET, Timeout, tickstart) != HAL_OK)
  2630. {
  2631. return HAL_ERROR;
  2632. }
  2633. /* Clear STOP Flag */
  2634. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  2635. }
  2636. /* Increment Trials */
  2637. FMPI2C_Trials++;
  2638. } while (FMPI2C_Trials < Trials);
  2639. /* Update FMPI2C state */
  2640. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  2641. /* Update FMPI2C error code */
  2642. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_TIMEOUT;
  2643. /* Process Unlocked */
  2644. __HAL_UNLOCK(hfmpi2c);
  2645. return HAL_ERROR;
  2646. }
  2647. else
  2648. {
  2649. return HAL_BUSY;
  2650. }
  2651. }
  2652. /**
  2653. * @brief Sequential transmit in master FMPI2C mode an amount of data in non-blocking mode with Interrupt.
  2654. * @note This interface allow to manage repeated start condition when a direction change during transfer
  2655. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2656. * the configuration information for the specified FMPI2C.
  2657. * @param DevAddress Target device address: The device 7 bits address value
  2658. * in datasheet must be shifted to the left before calling the interface
  2659. * @param pData Pointer to data buffer
  2660. * @param Size Amount of data to be sent
  2661. * @param XferOptions Options of Transfer, value of @ref FMPI2C_XFEROPTIONS
  2662. * @retval HAL status
  2663. */
  2664. HAL_StatusTypeDef HAL_FMPI2C_Master_Seq_Transmit_IT(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint8_t *pData,
  2665. uint16_t Size, uint32_t XferOptions)
  2666. {
  2667. uint32_t xfermode;
  2668. uint32_t xferrequest = FMPI2C_GENERATE_START_WRITE;
  2669. /* Check the parameters */
  2670. assert_param(IS_FMPI2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  2671. if (hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  2672. {
  2673. /* Process Locked */
  2674. __HAL_LOCK(hfmpi2c);
  2675. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_TX;
  2676. hfmpi2c->Mode = HAL_FMPI2C_MODE_MASTER;
  2677. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  2678. /* Prepare transfer parameters */
  2679. hfmpi2c->pBuffPtr = pData;
  2680. hfmpi2c->XferCount = Size;
  2681. hfmpi2c->XferOptions = XferOptions;
  2682. hfmpi2c->XferISR = FMPI2C_Master_ISR_IT;
  2683. /* If hfmpi2c->XferCount > MAX_NBYTE_SIZE, use reload mode */
  2684. if (hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  2685. {
  2686. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  2687. xfermode = FMPI2C_RELOAD_MODE;
  2688. }
  2689. else
  2690. {
  2691. hfmpi2c->XferSize = hfmpi2c->XferCount;
  2692. xfermode = hfmpi2c->XferOptions;
  2693. }
  2694. /* If transfer direction not change and there is no request to start another frame, do not generate Restart Condition */
  2695. /* Mean Previous state is same as current state */
  2696. if ((hfmpi2c->PreviousState == FMPI2C_STATE_MASTER_BUSY_TX) && (IS_FMPI2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 0))
  2697. {
  2698. xferrequest = FMPI2C_NO_STARTSTOP;
  2699. }
  2700. else
  2701. {
  2702. /* Convert OTHER_xxx XferOptions if any */
  2703. FMPI2C_ConvertOtherXferOptions(hfmpi2c);
  2704. /* Update xfermode accordingly if no reload is necessary */
  2705. if (hfmpi2c->XferCount <= MAX_NBYTE_SIZE)
  2706. {
  2707. xfermode = hfmpi2c->XferOptions;
  2708. }
  2709. }
  2710. /* Send Slave Address and set NBYTES to write */
  2711. FMPI2C_TransferConfig(hfmpi2c, DevAddress, (uint8_t)hfmpi2c->XferSize, xfermode, xferrequest);
  2712. /* Process Unlocked */
  2713. __HAL_UNLOCK(hfmpi2c);
  2714. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  2715. to avoid the risk of FMPI2C interrupt handle execution before current
  2716. process unlock */
  2717. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_TX_IT);
  2718. return HAL_OK;
  2719. }
  2720. else
  2721. {
  2722. return HAL_BUSY;
  2723. }
  2724. }
  2725. /**
  2726. * @brief Sequential transmit in master FMPI2C mode an amount of data in non-blocking mode with DMA.
  2727. * @note This interface allow to manage repeated start condition when a direction change during transfer
  2728. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2729. * the configuration information for the specified FMPI2C.
  2730. * @param DevAddress Target device address: The device 7 bits address value
  2731. * in datasheet must be shifted to the left before calling the interface
  2732. * @param pData Pointer to data buffer
  2733. * @param Size Amount of data to be sent
  2734. * @param XferOptions Options of Transfer, value of @ref FMPI2C_XFEROPTIONS
  2735. * @retval HAL status
  2736. */
  2737. HAL_StatusTypeDef HAL_FMPI2C_Master_Seq_Transmit_DMA(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint8_t *pData,
  2738. uint16_t Size, uint32_t XferOptions)
  2739. {
  2740. uint32_t xfermode;
  2741. uint32_t xferrequest = FMPI2C_GENERATE_START_WRITE;
  2742. HAL_StatusTypeDef dmaxferstatus;
  2743. /* Check the parameters */
  2744. assert_param(IS_FMPI2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  2745. if (hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  2746. {
  2747. /* Process Locked */
  2748. __HAL_LOCK(hfmpi2c);
  2749. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_TX;
  2750. hfmpi2c->Mode = HAL_FMPI2C_MODE_MASTER;
  2751. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  2752. /* Prepare transfer parameters */
  2753. hfmpi2c->pBuffPtr = pData;
  2754. hfmpi2c->XferCount = Size;
  2755. hfmpi2c->XferOptions = XferOptions;
  2756. hfmpi2c->XferISR = FMPI2C_Master_ISR_DMA;
  2757. /* If hfmpi2c->XferCount > MAX_NBYTE_SIZE, use reload mode */
  2758. if (hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  2759. {
  2760. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  2761. xfermode = FMPI2C_RELOAD_MODE;
  2762. }
  2763. else
  2764. {
  2765. hfmpi2c->XferSize = hfmpi2c->XferCount;
  2766. xfermode = hfmpi2c->XferOptions;
  2767. }
  2768. /* If transfer direction not change and there is no request to start another frame, do not generate Restart Condition */
  2769. /* Mean Previous state is same as current state */
  2770. if ((hfmpi2c->PreviousState == FMPI2C_STATE_MASTER_BUSY_TX) && (IS_FMPI2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 0))
  2771. {
  2772. xferrequest = FMPI2C_NO_STARTSTOP;
  2773. }
  2774. else
  2775. {
  2776. /* Convert OTHER_xxx XferOptions if any */
  2777. FMPI2C_ConvertOtherXferOptions(hfmpi2c);
  2778. /* Update xfermode accordingly if no reload is necessary */
  2779. if (hfmpi2c->XferCount <= MAX_NBYTE_SIZE)
  2780. {
  2781. xfermode = hfmpi2c->XferOptions;
  2782. }
  2783. }
  2784. if (hfmpi2c->XferSize > 0U)
  2785. {
  2786. if (hfmpi2c->hdmatx != NULL)
  2787. {
  2788. /* Set the FMPI2C DMA transfer complete callback */
  2789. hfmpi2c->hdmatx->XferCpltCallback = FMPI2C_DMAMasterTransmitCplt;
  2790. /* Set the DMA error callback */
  2791. hfmpi2c->hdmatx->XferErrorCallback = FMPI2C_DMAError;
  2792. /* Set the unused DMA callbacks to NULL */
  2793. hfmpi2c->hdmatx->XferHalfCpltCallback = NULL;
  2794. hfmpi2c->hdmatx->XferAbortCallback = NULL;
  2795. /* Enable the DMA stream */
  2796. dmaxferstatus = HAL_DMA_Start_IT(hfmpi2c->hdmatx, (uint32_t)pData, (uint32_t)&hfmpi2c->Instance->TXDR, hfmpi2c->XferSize);
  2797. }
  2798. else
  2799. {
  2800. /* Update FMPI2C state */
  2801. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  2802. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  2803. /* Update FMPI2C error code */
  2804. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_DMA_PARAM;
  2805. /* Process Unlocked */
  2806. __HAL_UNLOCK(hfmpi2c);
  2807. return HAL_ERROR;
  2808. }
  2809. if (dmaxferstatus == HAL_OK)
  2810. {
  2811. /* Send Slave Address and set NBYTES to write */
  2812. FMPI2C_TransferConfig(hfmpi2c, DevAddress, (uint8_t)hfmpi2c->XferSize, xfermode, xferrequest);
  2813. /* Update XferCount value */
  2814. hfmpi2c->XferCount -= hfmpi2c->XferSize;
  2815. /* Process Unlocked */
  2816. __HAL_UNLOCK(hfmpi2c);
  2817. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  2818. to avoid the risk of FMPI2C interrupt handle execution before current
  2819. process unlock */
  2820. /* Enable ERR and NACK interrupts */
  2821. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_ERROR_IT);
  2822. /* Enable DMA Request */
  2823. hfmpi2c->Instance->CR1 |= FMPI2C_CR1_TXDMAEN;
  2824. }
  2825. else
  2826. {
  2827. /* Update FMPI2C state */
  2828. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  2829. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  2830. /* Update FMPI2C error code */
  2831. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_DMA;
  2832. /* Process Unlocked */
  2833. __HAL_UNLOCK(hfmpi2c);
  2834. return HAL_ERROR;
  2835. }
  2836. }
  2837. else
  2838. {
  2839. /* Update Transfer ISR function pointer */
  2840. hfmpi2c->XferISR = FMPI2C_Master_ISR_IT;
  2841. /* Send Slave Address */
  2842. /* Set NBYTES to write and generate START condition */
  2843. FMPI2C_TransferConfig(hfmpi2c, DevAddress, (uint8_t)hfmpi2c->XferSize, FMPI2C_AUTOEND_MODE, FMPI2C_GENERATE_START_WRITE);
  2844. /* Process Unlocked */
  2845. __HAL_UNLOCK(hfmpi2c);
  2846. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  2847. to avoid the risk of FMPI2C interrupt handle execution before current
  2848. process unlock */
  2849. /* Enable ERR, TC, STOP, NACK, TXI interrupt */
  2850. /* possible to enable all of these */
  2851. /* FMPI2C_IT_ERRI | FMPI2C_IT_TCI| FMPI2C_IT_STOPI| FMPI2C_IT_NACKI | FMPI2C_IT_ADDRI | FMPI2C_IT_RXI | FMPI2C_IT_TXI */
  2852. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_TX_IT);
  2853. }
  2854. return HAL_OK;
  2855. }
  2856. else
  2857. {
  2858. return HAL_BUSY;
  2859. }
  2860. }
  2861. /**
  2862. * @brief Sequential receive in master FMPI2C mode an amount of data in non-blocking mode with Interrupt
  2863. * @note This interface allow to manage repeated start condition when a direction change during transfer
  2864. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2865. * the configuration information for the specified FMPI2C.
  2866. * @param DevAddress Target device address: The device 7 bits address value
  2867. * in datasheet must be shifted to the left before calling the interface
  2868. * @param pData Pointer to data buffer
  2869. * @param Size Amount of data to be sent
  2870. * @param XferOptions Options of Transfer, value of @ref FMPI2C_XFEROPTIONS
  2871. * @retval HAL status
  2872. */
  2873. HAL_StatusTypeDef HAL_FMPI2C_Master_Seq_Receive_IT(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint8_t *pData,
  2874. uint16_t Size, uint32_t XferOptions)
  2875. {
  2876. uint32_t xfermode;
  2877. uint32_t xferrequest = FMPI2C_GENERATE_START_READ;
  2878. /* Check the parameters */
  2879. assert_param(IS_FMPI2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  2880. if (hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  2881. {
  2882. /* Process Locked */
  2883. __HAL_LOCK(hfmpi2c);
  2884. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_RX;
  2885. hfmpi2c->Mode = HAL_FMPI2C_MODE_MASTER;
  2886. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  2887. /* Prepare transfer parameters */
  2888. hfmpi2c->pBuffPtr = pData;
  2889. hfmpi2c->XferCount = Size;
  2890. hfmpi2c->XferOptions = XferOptions;
  2891. hfmpi2c->XferISR = FMPI2C_Master_ISR_IT;
  2892. /* If hfmpi2c->XferCount > MAX_NBYTE_SIZE, use reload mode */
  2893. if (hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  2894. {
  2895. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  2896. xfermode = FMPI2C_RELOAD_MODE;
  2897. }
  2898. else
  2899. {
  2900. hfmpi2c->XferSize = hfmpi2c->XferCount;
  2901. xfermode = hfmpi2c->XferOptions;
  2902. }
  2903. /* If transfer direction not change and there is no request to start another frame, do not generate Restart Condition */
  2904. /* Mean Previous state is same as current state */
  2905. if ((hfmpi2c->PreviousState == FMPI2C_STATE_MASTER_BUSY_RX) && (IS_FMPI2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 0))
  2906. {
  2907. xferrequest = FMPI2C_NO_STARTSTOP;
  2908. }
  2909. else
  2910. {
  2911. /* Convert OTHER_xxx XferOptions if any */
  2912. FMPI2C_ConvertOtherXferOptions(hfmpi2c);
  2913. /* Update xfermode accordingly if no reload is necessary */
  2914. if (hfmpi2c->XferCount <= MAX_NBYTE_SIZE)
  2915. {
  2916. xfermode = hfmpi2c->XferOptions;
  2917. }
  2918. }
  2919. /* Send Slave Address and set NBYTES to read */
  2920. FMPI2C_TransferConfig(hfmpi2c, DevAddress, (uint8_t)hfmpi2c->XferSize, xfermode, xferrequest);
  2921. /* Process Unlocked */
  2922. __HAL_UNLOCK(hfmpi2c);
  2923. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  2924. to avoid the risk of FMPI2C interrupt handle execution before current
  2925. process unlock */
  2926. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_RX_IT);
  2927. return HAL_OK;
  2928. }
  2929. else
  2930. {
  2931. return HAL_BUSY;
  2932. }
  2933. }
  2934. /**
  2935. * @brief Sequential receive in master FMPI2C mode an amount of data in non-blocking mode with DMA
  2936. * @note This interface allow to manage repeated start condition when a direction change during transfer
  2937. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2938. * the configuration information for the specified FMPI2C.
  2939. * @param DevAddress Target device address: The device 7 bits address value
  2940. * in datasheet must be shifted to the left before calling the interface
  2941. * @param pData Pointer to data buffer
  2942. * @param Size Amount of data to be sent
  2943. * @param XferOptions Options of Transfer, value of @ref FMPI2C_XFEROPTIONS
  2944. * @retval HAL status
  2945. */
  2946. HAL_StatusTypeDef HAL_FMPI2C_Master_Seq_Receive_DMA(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint8_t *pData,
  2947. uint16_t Size, uint32_t XferOptions)
  2948. {
  2949. uint32_t xfermode;
  2950. uint32_t xferrequest = FMPI2C_GENERATE_START_READ;
  2951. HAL_StatusTypeDef dmaxferstatus;
  2952. /* Check the parameters */
  2953. assert_param(IS_FMPI2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  2954. if (hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  2955. {
  2956. /* Process Locked */
  2957. __HAL_LOCK(hfmpi2c);
  2958. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_RX;
  2959. hfmpi2c->Mode = HAL_FMPI2C_MODE_MASTER;
  2960. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  2961. /* Prepare transfer parameters */
  2962. hfmpi2c->pBuffPtr = pData;
  2963. hfmpi2c->XferCount = Size;
  2964. hfmpi2c->XferOptions = XferOptions;
  2965. hfmpi2c->XferISR = FMPI2C_Master_ISR_DMA;
  2966. /* If hfmpi2c->XferCount > MAX_NBYTE_SIZE, use reload mode */
  2967. if (hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  2968. {
  2969. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  2970. xfermode = FMPI2C_RELOAD_MODE;
  2971. }
  2972. else
  2973. {
  2974. hfmpi2c->XferSize = hfmpi2c->XferCount;
  2975. xfermode = hfmpi2c->XferOptions;
  2976. }
  2977. /* If transfer direction not change and there is no request to start another frame, do not generate Restart Condition */
  2978. /* Mean Previous state is same as current state */
  2979. if ((hfmpi2c->PreviousState == FMPI2C_STATE_MASTER_BUSY_RX) && (IS_FMPI2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 0))
  2980. {
  2981. xferrequest = FMPI2C_NO_STARTSTOP;
  2982. }
  2983. else
  2984. {
  2985. /* Convert OTHER_xxx XferOptions if any */
  2986. FMPI2C_ConvertOtherXferOptions(hfmpi2c);
  2987. /* Update xfermode accordingly if no reload is necessary */
  2988. if (hfmpi2c->XferCount <= MAX_NBYTE_SIZE)
  2989. {
  2990. xfermode = hfmpi2c->XferOptions;
  2991. }
  2992. }
  2993. if (hfmpi2c->XferSize > 0U)
  2994. {
  2995. if (hfmpi2c->hdmarx != NULL)
  2996. {
  2997. /* Set the FMPI2C DMA transfer complete callback */
  2998. hfmpi2c->hdmarx->XferCpltCallback = FMPI2C_DMAMasterReceiveCplt;
  2999. /* Set the DMA error callback */
  3000. hfmpi2c->hdmarx->XferErrorCallback = FMPI2C_DMAError;
  3001. /* Set the unused DMA callbacks to NULL */
  3002. hfmpi2c->hdmarx->XferHalfCpltCallback = NULL;
  3003. hfmpi2c->hdmarx->XferAbortCallback = NULL;
  3004. /* Enable the DMA stream */
  3005. dmaxferstatus = HAL_DMA_Start_IT(hfmpi2c->hdmarx, (uint32_t)&hfmpi2c->Instance->RXDR, (uint32_t)pData, hfmpi2c->XferSize);
  3006. }
  3007. else
  3008. {
  3009. /* Update FMPI2C state */
  3010. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  3011. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  3012. /* Update FMPI2C error code */
  3013. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_DMA_PARAM;
  3014. /* Process Unlocked */
  3015. __HAL_UNLOCK(hfmpi2c);
  3016. return HAL_ERROR;
  3017. }
  3018. if (dmaxferstatus == HAL_OK)
  3019. {
  3020. /* Send Slave Address and set NBYTES to read */
  3021. FMPI2C_TransferConfig(hfmpi2c, DevAddress, (uint8_t)hfmpi2c->XferSize, xfermode, xferrequest);
  3022. /* Update XferCount value */
  3023. hfmpi2c->XferCount -= hfmpi2c->XferSize;
  3024. /* Process Unlocked */
  3025. __HAL_UNLOCK(hfmpi2c);
  3026. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  3027. to avoid the risk of FMPI2C interrupt handle execution before current
  3028. process unlock */
  3029. /* Enable ERR and NACK interrupts */
  3030. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_ERROR_IT);
  3031. /* Enable DMA Request */
  3032. hfmpi2c->Instance->CR1 |= FMPI2C_CR1_RXDMAEN;
  3033. }
  3034. else
  3035. {
  3036. /* Update FMPI2C state */
  3037. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  3038. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  3039. /* Update FMPI2C error code */
  3040. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_DMA;
  3041. /* Process Unlocked */
  3042. __HAL_UNLOCK(hfmpi2c);
  3043. return HAL_ERROR;
  3044. }
  3045. }
  3046. else
  3047. {
  3048. /* Update Transfer ISR function pointer */
  3049. hfmpi2c->XferISR = FMPI2C_Master_ISR_IT;
  3050. /* Send Slave Address */
  3051. /* Set NBYTES to read and generate START condition */
  3052. FMPI2C_TransferConfig(hfmpi2c, DevAddress, (uint8_t)hfmpi2c->XferSize, FMPI2C_AUTOEND_MODE, FMPI2C_GENERATE_START_READ);
  3053. /* Process Unlocked */
  3054. __HAL_UNLOCK(hfmpi2c);
  3055. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  3056. to avoid the risk of FMPI2C interrupt handle execution before current
  3057. process unlock */
  3058. /* Enable ERR, TC, STOP, NACK, TXI interrupt */
  3059. /* possible to enable all of these */
  3060. /* FMPI2C_IT_ERRI | FMPI2C_IT_TCI| FMPI2C_IT_STOPI| FMPI2C_IT_NACKI | FMPI2C_IT_ADDRI | FMPI2C_IT_RXI | FMPI2C_IT_TXI */
  3061. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_TX_IT);
  3062. }
  3063. return HAL_OK;
  3064. }
  3065. else
  3066. {
  3067. return HAL_BUSY;
  3068. }
  3069. }
  3070. /**
  3071. * @brief Sequential transmit in slave/device FMPI2C mode an amount of data in non-blocking mode with Interrupt
  3072. * @note This interface allow to manage repeated start condition when a direction change during transfer
  3073. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  3074. * the configuration information for the specified FMPI2C.
  3075. * @param pData Pointer to data buffer
  3076. * @param Size Amount of data to be sent
  3077. * @param XferOptions Options of Transfer, value of @ref FMPI2C_XFEROPTIONS
  3078. * @retval HAL status
  3079. */
  3080. HAL_StatusTypeDef HAL_FMPI2C_Slave_Seq_Transmit_IT(FMPI2C_HandleTypeDef *hfmpi2c, uint8_t *pData, uint16_t Size,
  3081. uint32_t XferOptions)
  3082. {
  3083. /* Check the parameters */
  3084. assert_param(IS_FMPI2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  3085. if (((uint32_t)hfmpi2c->State & (uint32_t)HAL_FMPI2C_STATE_LISTEN) == (uint32_t)HAL_FMPI2C_STATE_LISTEN)
  3086. {
  3087. if ((pData == NULL) || (Size == 0U))
  3088. {
  3089. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_INVALID_PARAM;
  3090. return HAL_ERROR;
  3091. }
  3092. /* Disable Interrupts, to prevent preemption during treatment in case of multicall */
  3093. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_LISTEN_IT | FMPI2C_XFER_TX_IT);
  3094. /* Process Locked */
  3095. __HAL_LOCK(hfmpi2c);
  3096. /* FMPI2C cannot manage full duplex exchange so disable previous IT enabled if any */
  3097. /* and then toggle the HAL slave RX state to TX state */
  3098. if (hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_RX_LISTEN)
  3099. {
  3100. /* Disable associated Interrupts */
  3101. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_RX_IT);
  3102. /* Abort DMA Xfer if any */
  3103. if ((hfmpi2c->Instance->CR1 & FMPI2C_CR1_RXDMAEN) == FMPI2C_CR1_RXDMAEN)
  3104. {
  3105. hfmpi2c->Instance->CR1 &= ~FMPI2C_CR1_RXDMAEN;
  3106. if (hfmpi2c->hdmarx != NULL)
  3107. {
  3108. /* Set the FMPI2C DMA Abort callback :
  3109. will lead to call HAL_FMPI2C_ErrorCallback() at end of DMA abort procedure */
  3110. hfmpi2c->hdmarx->XferAbortCallback = FMPI2C_DMAAbort;
  3111. /* Abort DMA RX */
  3112. if (HAL_DMA_Abort_IT(hfmpi2c->hdmarx) != HAL_OK)
  3113. {
  3114. /* Call Directly XferAbortCallback function in case of error */
  3115. hfmpi2c->hdmarx->XferAbortCallback(hfmpi2c->hdmarx);
  3116. }
  3117. }
  3118. }
  3119. }
  3120. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_TX_LISTEN;
  3121. hfmpi2c->Mode = HAL_FMPI2C_MODE_SLAVE;
  3122. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  3123. /* Enable Address Acknowledge */
  3124. hfmpi2c->Instance->CR2 &= ~FMPI2C_CR2_NACK;
  3125. /* Prepare transfer parameters */
  3126. hfmpi2c->pBuffPtr = pData;
  3127. hfmpi2c->XferCount = Size;
  3128. hfmpi2c->XferSize = hfmpi2c->XferCount;
  3129. hfmpi2c->XferOptions = XferOptions;
  3130. hfmpi2c->XferISR = FMPI2C_Slave_ISR_IT;
  3131. if (FMPI2C_GET_DIR(hfmpi2c) == FMPI2C_DIRECTION_RECEIVE)
  3132. {
  3133. /* Clear ADDR flag after prepare the transfer parameters */
  3134. /* This action will generate an acknowledge to the Master */
  3135. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_ADDR);
  3136. }
  3137. /* Process Unlocked */
  3138. __HAL_UNLOCK(hfmpi2c);
  3139. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  3140. to avoid the risk of FMPI2C interrupt handle execution before current
  3141. process unlock */
  3142. /* REnable ADDR interrupt */
  3143. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_TX_IT | FMPI2C_XFER_LISTEN_IT);
  3144. return HAL_OK;
  3145. }
  3146. else
  3147. {
  3148. return HAL_ERROR;
  3149. }
  3150. }
  3151. /**
  3152. * @brief Sequential transmit in slave/device FMPI2C mode an amount of data in non-blocking mode with DMA
  3153. * @note This interface allow to manage repeated start condition when a direction change during transfer
  3154. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  3155. * the configuration information for the specified FMPI2C.
  3156. * @param pData Pointer to data buffer
  3157. * @param Size Amount of data to be sent
  3158. * @param XferOptions Options of Transfer, value of @ref FMPI2C_XFEROPTIONS
  3159. * @retval HAL status
  3160. */
  3161. HAL_StatusTypeDef HAL_FMPI2C_Slave_Seq_Transmit_DMA(FMPI2C_HandleTypeDef *hfmpi2c, uint8_t *pData, uint16_t Size,
  3162. uint32_t XferOptions)
  3163. {
  3164. HAL_StatusTypeDef dmaxferstatus;
  3165. /* Check the parameters */
  3166. assert_param(IS_FMPI2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  3167. if (((uint32_t)hfmpi2c->State & (uint32_t)HAL_FMPI2C_STATE_LISTEN) == (uint32_t)HAL_FMPI2C_STATE_LISTEN)
  3168. {
  3169. if ((pData == NULL) || (Size == 0U))
  3170. {
  3171. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_INVALID_PARAM;
  3172. return HAL_ERROR;
  3173. }
  3174. /* Process Locked */
  3175. __HAL_LOCK(hfmpi2c);
  3176. /* Disable Interrupts, to prevent preemption during treatment in case of multicall */
  3177. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_LISTEN_IT | FMPI2C_XFER_TX_IT);
  3178. /* FMPI2C cannot manage full duplex exchange so disable previous IT enabled if any */
  3179. /* and then toggle the HAL slave RX state to TX state */
  3180. if (hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_RX_LISTEN)
  3181. {
  3182. /* Disable associated Interrupts */
  3183. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_RX_IT);
  3184. if ((hfmpi2c->Instance->CR1 & FMPI2C_CR1_RXDMAEN) == FMPI2C_CR1_RXDMAEN)
  3185. {
  3186. /* Abort DMA Xfer if any */
  3187. if (hfmpi2c->hdmarx != NULL)
  3188. {
  3189. hfmpi2c->Instance->CR1 &= ~FMPI2C_CR1_RXDMAEN;
  3190. /* Set the FMPI2C DMA Abort callback :
  3191. will lead to call HAL_FMPI2C_ErrorCallback() at end of DMA abort procedure */
  3192. hfmpi2c->hdmarx->XferAbortCallback = FMPI2C_DMAAbort;
  3193. /* Abort DMA RX */
  3194. if (HAL_DMA_Abort_IT(hfmpi2c->hdmarx) != HAL_OK)
  3195. {
  3196. /* Call Directly XferAbortCallback function in case of error */
  3197. hfmpi2c->hdmarx->XferAbortCallback(hfmpi2c->hdmarx);
  3198. }
  3199. }
  3200. }
  3201. }
  3202. else if (hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_TX_LISTEN)
  3203. {
  3204. if ((hfmpi2c->Instance->CR1 & FMPI2C_CR1_TXDMAEN) == FMPI2C_CR1_TXDMAEN)
  3205. {
  3206. hfmpi2c->Instance->CR1 &= ~FMPI2C_CR1_TXDMAEN;
  3207. /* Abort DMA Xfer if any */
  3208. if (hfmpi2c->hdmatx != NULL)
  3209. {
  3210. /* Set the FMPI2C DMA Abort callback :
  3211. will lead to call HAL_FMPI2C_ErrorCallback() at end of DMA abort procedure */
  3212. hfmpi2c->hdmatx->XferAbortCallback = FMPI2C_DMAAbort;
  3213. /* Abort DMA TX */
  3214. if (HAL_DMA_Abort_IT(hfmpi2c->hdmatx) != HAL_OK)
  3215. {
  3216. /* Call Directly XferAbortCallback function in case of error */
  3217. hfmpi2c->hdmatx->XferAbortCallback(hfmpi2c->hdmatx);
  3218. }
  3219. }
  3220. }
  3221. }
  3222. else
  3223. {
  3224. /* Nothing to do */
  3225. }
  3226. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_TX_LISTEN;
  3227. hfmpi2c->Mode = HAL_FMPI2C_MODE_SLAVE;
  3228. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  3229. /* Enable Address Acknowledge */
  3230. hfmpi2c->Instance->CR2 &= ~FMPI2C_CR2_NACK;
  3231. /* Prepare transfer parameters */
  3232. hfmpi2c->pBuffPtr = pData;
  3233. hfmpi2c->XferCount = Size;
  3234. hfmpi2c->XferSize = hfmpi2c->XferCount;
  3235. hfmpi2c->XferOptions = XferOptions;
  3236. hfmpi2c->XferISR = FMPI2C_Slave_ISR_DMA;
  3237. if (hfmpi2c->hdmatx != NULL)
  3238. {
  3239. /* Set the FMPI2C DMA transfer complete callback */
  3240. hfmpi2c->hdmatx->XferCpltCallback = FMPI2C_DMASlaveTransmitCplt;
  3241. /* Set the DMA error callback */
  3242. hfmpi2c->hdmatx->XferErrorCallback = FMPI2C_DMAError;
  3243. /* Set the unused DMA callbacks to NULL */
  3244. hfmpi2c->hdmatx->XferHalfCpltCallback = NULL;
  3245. hfmpi2c->hdmatx->XferAbortCallback = NULL;
  3246. /* Enable the DMA stream */
  3247. dmaxferstatus = HAL_DMA_Start_IT(hfmpi2c->hdmatx, (uint32_t)pData, (uint32_t)&hfmpi2c->Instance->TXDR, hfmpi2c->XferSize);
  3248. }
  3249. else
  3250. {
  3251. /* Update FMPI2C state */
  3252. hfmpi2c->State = HAL_FMPI2C_STATE_LISTEN;
  3253. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  3254. /* Update FMPI2C error code */
  3255. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_DMA_PARAM;
  3256. /* Process Unlocked */
  3257. __HAL_UNLOCK(hfmpi2c);
  3258. return HAL_ERROR;
  3259. }
  3260. if (dmaxferstatus == HAL_OK)
  3261. {
  3262. /* Update XferCount value */
  3263. hfmpi2c->XferCount -= hfmpi2c->XferSize;
  3264. /* Reset XferSize */
  3265. hfmpi2c->XferSize = 0;
  3266. }
  3267. else
  3268. {
  3269. /* Update FMPI2C state */
  3270. hfmpi2c->State = HAL_FMPI2C_STATE_LISTEN;
  3271. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  3272. /* Update FMPI2C error code */
  3273. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_DMA;
  3274. /* Process Unlocked */
  3275. __HAL_UNLOCK(hfmpi2c);
  3276. return HAL_ERROR;
  3277. }
  3278. if (FMPI2C_GET_DIR(hfmpi2c) == FMPI2C_DIRECTION_RECEIVE)
  3279. {
  3280. /* Clear ADDR flag after prepare the transfer parameters */
  3281. /* This action will generate an acknowledge to the Master */
  3282. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_ADDR);
  3283. }
  3284. /* Process Unlocked */
  3285. __HAL_UNLOCK(hfmpi2c);
  3286. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  3287. to avoid the risk of FMPI2C interrupt handle execution before current
  3288. process unlock */
  3289. /* Enable ERR, STOP, NACK, ADDR interrupts */
  3290. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_LISTEN_IT);
  3291. /* Enable DMA Request */
  3292. hfmpi2c->Instance->CR1 |= FMPI2C_CR1_TXDMAEN;
  3293. return HAL_OK;
  3294. }
  3295. else
  3296. {
  3297. return HAL_ERROR;
  3298. }
  3299. }
  3300. /**
  3301. * @brief Sequential receive in slave/device FMPI2C mode an amount of data in non-blocking mode with Interrupt
  3302. * @note This interface allow to manage repeated start condition when a direction change during transfer
  3303. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  3304. * the configuration information for the specified FMPI2C.
  3305. * @param pData Pointer to data buffer
  3306. * @param Size Amount of data to be sent
  3307. * @param XferOptions Options of Transfer, value of @ref FMPI2C_XFEROPTIONS
  3308. * @retval HAL status
  3309. */
  3310. HAL_StatusTypeDef HAL_FMPI2C_Slave_Seq_Receive_IT(FMPI2C_HandleTypeDef *hfmpi2c, uint8_t *pData, uint16_t Size,
  3311. uint32_t XferOptions)
  3312. {
  3313. /* Check the parameters */
  3314. assert_param(IS_FMPI2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  3315. if (((uint32_t)hfmpi2c->State & (uint32_t)HAL_FMPI2C_STATE_LISTEN) == (uint32_t)HAL_FMPI2C_STATE_LISTEN)
  3316. {
  3317. if ((pData == NULL) || (Size == 0U))
  3318. {
  3319. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_INVALID_PARAM;
  3320. return HAL_ERROR;
  3321. }
  3322. /* Disable Interrupts, to prevent preemption during treatment in case of multicall */
  3323. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_LISTEN_IT | FMPI2C_XFER_RX_IT);
  3324. /* Process Locked */
  3325. __HAL_LOCK(hfmpi2c);
  3326. /* FMPI2C cannot manage full duplex exchange so disable previous IT enabled if any */
  3327. /* and then toggle the HAL slave TX state to RX state */
  3328. if (hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_TX_LISTEN)
  3329. {
  3330. /* Disable associated Interrupts */
  3331. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_TX_IT);
  3332. if ((hfmpi2c->Instance->CR1 & FMPI2C_CR1_TXDMAEN) == FMPI2C_CR1_TXDMAEN)
  3333. {
  3334. hfmpi2c->Instance->CR1 &= ~FMPI2C_CR1_TXDMAEN;
  3335. /* Abort DMA Xfer if any */
  3336. if (hfmpi2c->hdmatx != NULL)
  3337. {
  3338. /* Set the FMPI2C DMA Abort callback :
  3339. will lead to call HAL_FMPI2C_ErrorCallback() at end of DMA abort procedure */
  3340. hfmpi2c->hdmatx->XferAbortCallback = FMPI2C_DMAAbort;
  3341. /* Abort DMA TX */
  3342. if (HAL_DMA_Abort_IT(hfmpi2c->hdmatx) != HAL_OK)
  3343. {
  3344. /* Call Directly XferAbortCallback function in case of error */
  3345. hfmpi2c->hdmatx->XferAbortCallback(hfmpi2c->hdmatx);
  3346. }
  3347. }
  3348. }
  3349. }
  3350. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_RX_LISTEN;
  3351. hfmpi2c->Mode = HAL_FMPI2C_MODE_SLAVE;
  3352. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  3353. /* Enable Address Acknowledge */
  3354. hfmpi2c->Instance->CR2 &= ~FMPI2C_CR2_NACK;
  3355. /* Prepare transfer parameters */
  3356. hfmpi2c->pBuffPtr = pData;
  3357. hfmpi2c->XferCount = Size;
  3358. hfmpi2c->XferSize = hfmpi2c->XferCount;
  3359. hfmpi2c->XferOptions = XferOptions;
  3360. hfmpi2c->XferISR = FMPI2C_Slave_ISR_IT;
  3361. if (FMPI2C_GET_DIR(hfmpi2c) == FMPI2C_DIRECTION_TRANSMIT)
  3362. {
  3363. /* Clear ADDR flag after prepare the transfer parameters */
  3364. /* This action will generate an acknowledge to the Master */
  3365. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_ADDR);
  3366. }
  3367. /* Process Unlocked */
  3368. __HAL_UNLOCK(hfmpi2c);
  3369. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  3370. to avoid the risk of FMPI2C interrupt handle execution before current
  3371. process unlock */
  3372. /* REnable ADDR interrupt */
  3373. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_RX_IT | FMPI2C_XFER_LISTEN_IT);
  3374. return HAL_OK;
  3375. }
  3376. else
  3377. {
  3378. return HAL_ERROR;
  3379. }
  3380. }
  3381. /**
  3382. * @brief Sequential receive in slave/device FMPI2C mode an amount of data in non-blocking mode with DMA
  3383. * @note This interface allow to manage repeated start condition when a direction change during transfer
  3384. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  3385. * the configuration information for the specified FMPI2C.
  3386. * @param pData Pointer to data buffer
  3387. * @param Size Amount of data to be sent
  3388. * @param XferOptions Options of Transfer, value of @ref FMPI2C_XFEROPTIONS
  3389. * @retval HAL status
  3390. */
  3391. HAL_StatusTypeDef HAL_FMPI2C_Slave_Seq_Receive_DMA(FMPI2C_HandleTypeDef *hfmpi2c, uint8_t *pData, uint16_t Size,
  3392. uint32_t XferOptions)
  3393. {
  3394. HAL_StatusTypeDef dmaxferstatus;
  3395. /* Check the parameters */
  3396. assert_param(IS_FMPI2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  3397. if (((uint32_t)hfmpi2c->State & (uint32_t)HAL_FMPI2C_STATE_LISTEN) == (uint32_t)HAL_FMPI2C_STATE_LISTEN)
  3398. {
  3399. if ((pData == NULL) || (Size == 0U))
  3400. {
  3401. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_INVALID_PARAM;
  3402. return HAL_ERROR;
  3403. }
  3404. /* Disable Interrupts, to prevent preemption during treatment in case of multicall */
  3405. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_LISTEN_IT | FMPI2C_XFER_RX_IT);
  3406. /* Process Locked */
  3407. __HAL_LOCK(hfmpi2c);
  3408. /* FMPI2C cannot manage full duplex exchange so disable previous IT enabled if any */
  3409. /* and then toggle the HAL slave TX state to RX state */
  3410. if (hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_TX_LISTEN)
  3411. {
  3412. /* Disable associated Interrupts */
  3413. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_TX_IT);
  3414. if ((hfmpi2c->Instance->CR1 & FMPI2C_CR1_TXDMAEN) == FMPI2C_CR1_TXDMAEN)
  3415. {
  3416. /* Abort DMA Xfer if any */
  3417. if (hfmpi2c->hdmatx != NULL)
  3418. {
  3419. hfmpi2c->Instance->CR1 &= ~FMPI2C_CR1_TXDMAEN;
  3420. /* Set the FMPI2C DMA Abort callback :
  3421. will lead to call HAL_FMPI2C_ErrorCallback() at end of DMA abort procedure */
  3422. hfmpi2c->hdmatx->XferAbortCallback = FMPI2C_DMAAbort;
  3423. /* Abort DMA TX */
  3424. if (HAL_DMA_Abort_IT(hfmpi2c->hdmatx) != HAL_OK)
  3425. {
  3426. /* Call Directly XferAbortCallback function in case of error */
  3427. hfmpi2c->hdmatx->XferAbortCallback(hfmpi2c->hdmatx);
  3428. }
  3429. }
  3430. }
  3431. }
  3432. else if (hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_RX_LISTEN)
  3433. {
  3434. if ((hfmpi2c->Instance->CR1 & FMPI2C_CR1_RXDMAEN) == FMPI2C_CR1_RXDMAEN)
  3435. {
  3436. hfmpi2c->Instance->CR1 &= ~FMPI2C_CR1_RXDMAEN;
  3437. /* Abort DMA Xfer if any */
  3438. if (hfmpi2c->hdmarx != NULL)
  3439. {
  3440. /* Set the FMPI2C DMA Abort callback :
  3441. will lead to call HAL_FMPI2C_ErrorCallback() at end of DMA abort procedure */
  3442. hfmpi2c->hdmarx->XferAbortCallback = FMPI2C_DMAAbort;
  3443. /* Abort DMA RX */
  3444. if (HAL_DMA_Abort_IT(hfmpi2c->hdmarx) != HAL_OK)
  3445. {
  3446. /* Call Directly XferAbortCallback function in case of error */
  3447. hfmpi2c->hdmarx->XferAbortCallback(hfmpi2c->hdmarx);
  3448. }
  3449. }
  3450. }
  3451. }
  3452. else
  3453. {
  3454. /* Nothing to do */
  3455. }
  3456. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_RX_LISTEN;
  3457. hfmpi2c->Mode = HAL_FMPI2C_MODE_SLAVE;
  3458. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  3459. /* Enable Address Acknowledge */
  3460. hfmpi2c->Instance->CR2 &= ~FMPI2C_CR2_NACK;
  3461. /* Prepare transfer parameters */
  3462. hfmpi2c->pBuffPtr = pData;
  3463. hfmpi2c->XferCount = Size;
  3464. hfmpi2c->XferSize = hfmpi2c->XferCount;
  3465. hfmpi2c->XferOptions = XferOptions;
  3466. hfmpi2c->XferISR = FMPI2C_Slave_ISR_DMA;
  3467. if (hfmpi2c->hdmarx != NULL)
  3468. {
  3469. /* Set the FMPI2C DMA transfer complete callback */
  3470. hfmpi2c->hdmarx->XferCpltCallback = FMPI2C_DMASlaveReceiveCplt;
  3471. /* Set the DMA error callback */
  3472. hfmpi2c->hdmarx->XferErrorCallback = FMPI2C_DMAError;
  3473. /* Set the unused DMA callbacks to NULL */
  3474. hfmpi2c->hdmarx->XferHalfCpltCallback = NULL;
  3475. hfmpi2c->hdmarx->XferAbortCallback = NULL;
  3476. /* Enable the DMA stream */
  3477. dmaxferstatus = HAL_DMA_Start_IT(hfmpi2c->hdmarx, (uint32_t)&hfmpi2c->Instance->RXDR, (uint32_t)pData, hfmpi2c->XferSize);
  3478. }
  3479. else
  3480. {
  3481. /* Update FMPI2C state */
  3482. hfmpi2c->State = HAL_FMPI2C_STATE_LISTEN;
  3483. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  3484. /* Update FMPI2C error code */
  3485. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_DMA_PARAM;
  3486. /* Process Unlocked */
  3487. __HAL_UNLOCK(hfmpi2c);
  3488. return HAL_ERROR;
  3489. }
  3490. if (dmaxferstatus == HAL_OK)
  3491. {
  3492. /* Update XferCount value */
  3493. hfmpi2c->XferCount -= hfmpi2c->XferSize;
  3494. /* Reset XferSize */
  3495. hfmpi2c->XferSize = 0;
  3496. }
  3497. else
  3498. {
  3499. /* Update FMPI2C state */
  3500. hfmpi2c->State = HAL_FMPI2C_STATE_LISTEN;
  3501. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  3502. /* Update FMPI2C error code */
  3503. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_DMA;
  3504. /* Process Unlocked */
  3505. __HAL_UNLOCK(hfmpi2c);
  3506. return HAL_ERROR;
  3507. }
  3508. if (FMPI2C_GET_DIR(hfmpi2c) == FMPI2C_DIRECTION_TRANSMIT)
  3509. {
  3510. /* Clear ADDR flag after prepare the transfer parameters */
  3511. /* This action will generate an acknowledge to the Master */
  3512. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_ADDR);
  3513. }
  3514. /* Process Unlocked */
  3515. __HAL_UNLOCK(hfmpi2c);
  3516. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  3517. to avoid the risk of FMPI2C interrupt handle execution before current
  3518. process unlock */
  3519. /* REnable ADDR interrupt */
  3520. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_RX_IT | FMPI2C_XFER_LISTEN_IT);
  3521. /* Enable DMA Request */
  3522. hfmpi2c->Instance->CR1 |= FMPI2C_CR1_RXDMAEN;
  3523. return HAL_OK;
  3524. }
  3525. else
  3526. {
  3527. return HAL_ERROR;
  3528. }
  3529. }
  3530. /**
  3531. * @brief Enable the Address listen mode with Interrupt.
  3532. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  3533. * the configuration information for the specified FMPI2C.
  3534. * @retval HAL status
  3535. */
  3536. HAL_StatusTypeDef HAL_FMPI2C_EnableListen_IT(FMPI2C_HandleTypeDef *hfmpi2c)
  3537. {
  3538. if (hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  3539. {
  3540. hfmpi2c->State = HAL_FMPI2C_STATE_LISTEN;
  3541. hfmpi2c->XferISR = FMPI2C_Slave_ISR_IT;
  3542. /* Enable the Address Match interrupt */
  3543. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_LISTEN_IT);
  3544. return HAL_OK;
  3545. }
  3546. else
  3547. {
  3548. return HAL_BUSY;
  3549. }
  3550. }
  3551. /**
  3552. * @brief Disable the Address listen mode with Interrupt.
  3553. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  3554. * the configuration information for the specified FMPI2C
  3555. * @retval HAL status
  3556. */
  3557. HAL_StatusTypeDef HAL_FMPI2C_DisableListen_IT(FMPI2C_HandleTypeDef *hfmpi2c)
  3558. {
  3559. /* Declaration of tmp to prevent undefined behavior of volatile usage */
  3560. uint32_t tmp;
  3561. /* Disable Address listen mode only if a transfer is not ongoing */
  3562. if (hfmpi2c->State == HAL_FMPI2C_STATE_LISTEN)
  3563. {
  3564. tmp = (uint32_t)(hfmpi2c->State) & FMPI2C_STATE_MSK;
  3565. hfmpi2c->PreviousState = tmp | (uint32_t)(hfmpi2c->Mode);
  3566. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  3567. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  3568. hfmpi2c->XferISR = NULL;
  3569. /* Disable the Address Match interrupt */
  3570. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_LISTEN_IT);
  3571. return HAL_OK;
  3572. }
  3573. else
  3574. {
  3575. return HAL_BUSY;
  3576. }
  3577. }
  3578. /**
  3579. * @brief Abort a master FMPI2C IT or DMA process communication with Interrupt.
  3580. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  3581. * the configuration information for the specified FMPI2C.
  3582. * @param DevAddress Target device address: The device 7 bits address value
  3583. * in datasheet must be shifted to the left before calling the interface
  3584. * @retval HAL status
  3585. */
  3586. HAL_StatusTypeDef HAL_FMPI2C_Master_Abort_IT(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress)
  3587. {
  3588. if (hfmpi2c->Mode == HAL_FMPI2C_MODE_MASTER)
  3589. {
  3590. /* Process Locked */
  3591. __HAL_LOCK(hfmpi2c);
  3592. /* Disable Interrupts and Store Previous state */
  3593. if (hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_TX)
  3594. {
  3595. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_TX_IT);
  3596. hfmpi2c->PreviousState = FMPI2C_STATE_MASTER_BUSY_TX;
  3597. }
  3598. else if (hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_RX)
  3599. {
  3600. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_RX_IT);
  3601. hfmpi2c->PreviousState = FMPI2C_STATE_MASTER_BUSY_RX;
  3602. }
  3603. else
  3604. {
  3605. /* Do nothing */
  3606. }
  3607. /* Set State at HAL_FMPI2C_STATE_ABORT */
  3608. hfmpi2c->State = HAL_FMPI2C_STATE_ABORT;
  3609. /* Set NBYTES to 1 to generate a dummy read on FMPI2C peripheral */
  3610. /* Set AUTOEND mode, this will generate a NACK then STOP condition to abort the current transfer */
  3611. FMPI2C_TransferConfig(hfmpi2c, DevAddress, 1, FMPI2C_AUTOEND_MODE, FMPI2C_GENERATE_STOP);
  3612. /* Process Unlocked */
  3613. __HAL_UNLOCK(hfmpi2c);
  3614. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  3615. to avoid the risk of FMPI2C interrupt handle execution before current
  3616. process unlock */
  3617. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_CPLT_IT);
  3618. return HAL_OK;
  3619. }
  3620. else
  3621. {
  3622. /* Wrong usage of abort function */
  3623. /* This function should be used only in case of abort monitored by master device */
  3624. return HAL_ERROR;
  3625. }
  3626. }
  3627. /**
  3628. * @}
  3629. */
  3630. /** @defgroup FMPI2C_IRQ_Handler_and_Callbacks IRQ Handler and Callbacks
  3631. * @{
  3632. */
  3633. /**
  3634. * @brief This function handles FMPI2C event interrupt request.
  3635. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  3636. * the configuration information for the specified FMPI2C.
  3637. * @retval None
  3638. */
  3639. void HAL_FMPI2C_EV_IRQHandler(FMPI2C_HandleTypeDef *hfmpi2c)
  3640. {
  3641. /* Get current IT Flags and IT sources value */
  3642. uint32_t itflags = READ_REG(hfmpi2c->Instance->ISR);
  3643. uint32_t itsources = READ_REG(hfmpi2c->Instance->CR1);
  3644. /* FMPI2C events treatment -------------------------------------*/
  3645. if (hfmpi2c->XferISR != NULL)
  3646. {
  3647. hfmpi2c->XferISR(hfmpi2c, itflags, itsources);
  3648. }
  3649. }
  3650. /**
  3651. * @brief This function handles FMPI2C error interrupt request.
  3652. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  3653. * the configuration information for the specified FMPI2C.
  3654. * @retval None
  3655. */
  3656. void HAL_FMPI2C_ER_IRQHandler(FMPI2C_HandleTypeDef *hfmpi2c)
  3657. {
  3658. uint32_t itflags = READ_REG(hfmpi2c->Instance->ISR);
  3659. uint32_t itsources = READ_REG(hfmpi2c->Instance->CR1);
  3660. uint32_t tmperror;
  3661. /* FMPI2C Bus error interrupt occurred ------------------------------------*/
  3662. if ((FMPI2C_CHECK_FLAG(itflags, FMPI2C_FLAG_BERR) != RESET) && (FMPI2C_CHECK_IT_SOURCE(itsources, FMPI2C_IT_ERRI) != RESET))
  3663. {
  3664. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_BERR;
  3665. /* Clear BERR flag */
  3666. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_BERR);
  3667. }
  3668. /* FMPI2C Over-Run/Under-Run interrupt occurred ----------------------------------------*/
  3669. if ((FMPI2C_CHECK_FLAG(itflags, FMPI2C_FLAG_OVR) != RESET) && (FMPI2C_CHECK_IT_SOURCE(itsources, FMPI2C_IT_ERRI) != RESET))
  3670. {
  3671. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_OVR;
  3672. /* Clear OVR flag */
  3673. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_OVR);
  3674. }
  3675. /* FMPI2C Arbitration Loss error interrupt occurred -------------------------------------*/
  3676. if ((FMPI2C_CHECK_FLAG(itflags, FMPI2C_FLAG_ARLO) != RESET) && (FMPI2C_CHECK_IT_SOURCE(itsources, FMPI2C_IT_ERRI) != RESET))
  3677. {
  3678. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_ARLO;
  3679. /* Clear ARLO flag */
  3680. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_ARLO);
  3681. }
  3682. /* Store current volatile hfmpi2c->ErrorCode, misra rule */
  3683. tmperror = hfmpi2c->ErrorCode;
  3684. /* Call the Error Callback in case of Error detected */
  3685. if ((tmperror & (HAL_FMPI2C_ERROR_BERR | HAL_FMPI2C_ERROR_OVR | HAL_FMPI2C_ERROR_ARLO)) != HAL_FMPI2C_ERROR_NONE)
  3686. {
  3687. FMPI2C_ITError(hfmpi2c, tmperror);
  3688. }
  3689. }
  3690. /**
  3691. * @brief Master Tx Transfer completed callback.
  3692. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  3693. * the configuration information for the specified FMPI2C.
  3694. * @retval None
  3695. */
  3696. __weak void HAL_FMPI2C_MasterTxCpltCallback(FMPI2C_HandleTypeDef *hfmpi2c)
  3697. {
  3698. /* Prevent unused argument(s) compilation warning */
  3699. UNUSED(hfmpi2c);
  3700. /* NOTE : This function should not be modified, when the callback is needed,
  3701. the HAL_FMPI2C_MasterTxCpltCallback could be implemented in the user file
  3702. */
  3703. }
  3704. /**
  3705. * @brief Master Rx Transfer completed callback.
  3706. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  3707. * the configuration information for the specified FMPI2C.
  3708. * @retval None
  3709. */
  3710. __weak void HAL_FMPI2C_MasterRxCpltCallback(FMPI2C_HandleTypeDef *hfmpi2c)
  3711. {
  3712. /* Prevent unused argument(s) compilation warning */
  3713. UNUSED(hfmpi2c);
  3714. /* NOTE : This function should not be modified, when the callback is needed,
  3715. the HAL_FMPI2C_MasterRxCpltCallback could be implemented in the user file
  3716. */
  3717. }
  3718. /** @brief Slave Tx Transfer completed callback.
  3719. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  3720. * the configuration information for the specified FMPI2C.
  3721. * @retval None
  3722. */
  3723. __weak void HAL_FMPI2C_SlaveTxCpltCallback(FMPI2C_HandleTypeDef *hfmpi2c)
  3724. {
  3725. /* Prevent unused argument(s) compilation warning */
  3726. UNUSED(hfmpi2c);
  3727. /* NOTE : This function should not be modified, when the callback is needed,
  3728. the HAL_FMPI2C_SlaveTxCpltCallback could be implemented in the user file
  3729. */
  3730. }
  3731. /**
  3732. * @brief Slave Rx Transfer completed callback.
  3733. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  3734. * the configuration information for the specified FMPI2C.
  3735. * @retval None
  3736. */
  3737. __weak void HAL_FMPI2C_SlaveRxCpltCallback(FMPI2C_HandleTypeDef *hfmpi2c)
  3738. {
  3739. /* Prevent unused argument(s) compilation warning */
  3740. UNUSED(hfmpi2c);
  3741. /* NOTE : This function should not be modified, when the callback is needed,
  3742. the HAL_FMPI2C_SlaveRxCpltCallback could be implemented in the user file
  3743. */
  3744. }
  3745. /**
  3746. * @brief Slave Address Match callback.
  3747. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  3748. * the configuration information for the specified FMPI2C.
  3749. * @param TransferDirection Master request Transfer Direction (Write/Read), value of @ref FMPI2C_XFERDIRECTION
  3750. * @param AddrMatchCode Address Match Code
  3751. * @retval None
  3752. */
  3753. __weak void HAL_FMPI2C_AddrCallback(FMPI2C_HandleTypeDef *hfmpi2c, uint8_t TransferDirection, uint16_t AddrMatchCode)
  3754. {
  3755. /* Prevent unused argument(s) compilation warning */
  3756. UNUSED(hfmpi2c);
  3757. UNUSED(TransferDirection);
  3758. UNUSED(AddrMatchCode);
  3759. /* NOTE : This function should not be modified, when the callback is needed,
  3760. the HAL_FMPI2C_AddrCallback() could be implemented in the user file
  3761. */
  3762. }
  3763. /**
  3764. * @brief Listen Complete callback.
  3765. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  3766. * the configuration information for the specified FMPI2C.
  3767. * @retval None
  3768. */
  3769. __weak void HAL_FMPI2C_ListenCpltCallback(FMPI2C_HandleTypeDef *hfmpi2c)
  3770. {
  3771. /* Prevent unused argument(s) compilation warning */
  3772. UNUSED(hfmpi2c);
  3773. /* NOTE : This function should not be modified, when the callback is needed,
  3774. the HAL_FMPI2C_ListenCpltCallback() could be implemented in the user file
  3775. */
  3776. }
  3777. /**
  3778. * @brief Memory Tx Transfer completed callback.
  3779. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  3780. * the configuration information for the specified FMPI2C.
  3781. * @retval None
  3782. */
  3783. __weak void HAL_FMPI2C_MemTxCpltCallback(FMPI2C_HandleTypeDef *hfmpi2c)
  3784. {
  3785. /* Prevent unused argument(s) compilation warning */
  3786. UNUSED(hfmpi2c);
  3787. /* NOTE : This function should not be modified, when the callback is needed,
  3788. the HAL_FMPI2C_MemTxCpltCallback could be implemented in the user file
  3789. */
  3790. }
  3791. /**
  3792. * @brief Memory Rx Transfer completed callback.
  3793. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  3794. * the configuration information for the specified FMPI2C.
  3795. * @retval None
  3796. */
  3797. __weak void HAL_FMPI2C_MemRxCpltCallback(FMPI2C_HandleTypeDef *hfmpi2c)
  3798. {
  3799. /* Prevent unused argument(s) compilation warning */
  3800. UNUSED(hfmpi2c);
  3801. /* NOTE : This function should not be modified, when the callback is needed,
  3802. the HAL_FMPI2C_MemRxCpltCallback could be implemented in the user file
  3803. */
  3804. }
  3805. /**
  3806. * @brief FMPI2C error callback.
  3807. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  3808. * the configuration information for the specified FMPI2C.
  3809. * @retval None
  3810. */
  3811. __weak void HAL_FMPI2C_ErrorCallback(FMPI2C_HandleTypeDef *hfmpi2c)
  3812. {
  3813. /* Prevent unused argument(s) compilation warning */
  3814. UNUSED(hfmpi2c);
  3815. /* NOTE : This function should not be modified, when the callback is needed,
  3816. the HAL_FMPI2C_ErrorCallback could be implemented in the user file
  3817. */
  3818. }
  3819. /**
  3820. * @brief FMPI2C abort callback.
  3821. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  3822. * the configuration information for the specified FMPI2C.
  3823. * @retval None
  3824. */
  3825. __weak void HAL_FMPI2C_AbortCpltCallback(FMPI2C_HandleTypeDef *hfmpi2c)
  3826. {
  3827. /* Prevent unused argument(s) compilation warning */
  3828. UNUSED(hfmpi2c);
  3829. /* NOTE : This function should not be modified, when the callback is needed,
  3830. the HAL_FMPI2C_AbortCpltCallback could be implemented in the user file
  3831. */
  3832. }
  3833. /**
  3834. * @}
  3835. */
  3836. /** @defgroup FMPI2C_Exported_Functions_Group3 Peripheral State, Mode and Error functions
  3837. * @brief Peripheral State, Mode and Error functions
  3838. *
  3839. @verbatim
  3840. ===============================================================================
  3841. ##### Peripheral State, Mode and Error functions #####
  3842. ===============================================================================
  3843. [..]
  3844. This subsection permit to get in run-time the status of the peripheral
  3845. and the data flow.
  3846. @endverbatim
  3847. * @{
  3848. */
  3849. /**
  3850. * @brief Return the FMPI2C handle state.
  3851. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  3852. * the configuration information for the specified FMPI2C.
  3853. * @retval HAL state
  3854. */
  3855. HAL_FMPI2C_StateTypeDef HAL_FMPI2C_GetState(FMPI2C_HandleTypeDef *hfmpi2c)
  3856. {
  3857. /* Return FMPI2C handle state */
  3858. return hfmpi2c->State;
  3859. }
  3860. /**
  3861. * @brief Returns the FMPI2C Master, Slave, Memory or no mode.
  3862. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  3863. * the configuration information for FMPI2C module
  3864. * @retval HAL mode
  3865. */
  3866. HAL_FMPI2C_ModeTypeDef HAL_FMPI2C_GetMode(FMPI2C_HandleTypeDef *hfmpi2c)
  3867. {
  3868. return hfmpi2c->Mode;
  3869. }
  3870. /**
  3871. * @brief Return the FMPI2C error code.
  3872. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  3873. * the configuration information for the specified FMPI2C.
  3874. * @retval FMPI2C Error Code
  3875. */
  3876. uint32_t HAL_FMPI2C_GetError(FMPI2C_HandleTypeDef *hfmpi2c)
  3877. {
  3878. return hfmpi2c->ErrorCode;
  3879. }
  3880. /**
  3881. * @}
  3882. */
  3883. /**
  3884. * @}
  3885. */
  3886. /** @addtogroup FMPI2C_Private_Functions
  3887. * @{
  3888. */
  3889. /**
  3890. * @brief Interrupt Sub-Routine which handle the Interrupt Flags Master Mode with Interrupt.
  3891. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  3892. * the configuration information for the specified FMPI2C.
  3893. * @param ITFlags Interrupt flags to handle.
  3894. * @param ITSources Interrupt sources enabled.
  3895. * @retval HAL status
  3896. */
  3897. static HAL_StatusTypeDef FMPI2C_Master_ISR_IT(struct __FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags, uint32_t ITSources)
  3898. {
  3899. uint16_t devaddress;
  3900. uint32_t tmpITFlags = ITFlags;
  3901. /* Process Locked */
  3902. __HAL_LOCK(hfmpi2c);
  3903. if ((FMPI2C_CHECK_FLAG(tmpITFlags, FMPI2C_FLAG_AF) != RESET) && (FMPI2C_CHECK_IT_SOURCE(ITSources, FMPI2C_IT_NACKI) != RESET))
  3904. {
  3905. /* Clear NACK Flag */
  3906. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_AF);
  3907. /* Set corresponding Error Code */
  3908. /* No need to generate STOP, it is automatically done */
  3909. /* Error callback will be send during stop flag treatment */
  3910. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_AF;
  3911. /* Flush TX register */
  3912. FMPI2C_Flush_TXDR(hfmpi2c);
  3913. }
  3914. else if ((FMPI2C_CHECK_FLAG(tmpITFlags, FMPI2C_FLAG_RXNE) != RESET) && (FMPI2C_CHECK_IT_SOURCE(ITSources, FMPI2C_IT_RXI) != RESET))
  3915. {
  3916. /* Remove RXNE flag on temporary variable as read done */
  3917. tmpITFlags &= ~FMPI2C_FLAG_RXNE;
  3918. /* Read data from RXDR */
  3919. *hfmpi2c->pBuffPtr = (uint8_t)hfmpi2c->Instance->RXDR;
  3920. /* Increment Buffer pointer */
  3921. hfmpi2c->pBuffPtr++;
  3922. hfmpi2c->XferSize--;
  3923. hfmpi2c->XferCount--;
  3924. }
  3925. else if ((FMPI2C_CHECK_FLAG(tmpITFlags, FMPI2C_FLAG_TXIS) != RESET) && (FMPI2C_CHECK_IT_SOURCE(ITSources, FMPI2C_IT_TXI) != RESET))
  3926. {
  3927. /* Write data to TXDR */
  3928. hfmpi2c->Instance->TXDR = *hfmpi2c->pBuffPtr;
  3929. /* Increment Buffer pointer */
  3930. hfmpi2c->pBuffPtr++;
  3931. hfmpi2c->XferSize--;
  3932. hfmpi2c->XferCount--;
  3933. }
  3934. else if ((FMPI2C_CHECK_FLAG(tmpITFlags, FMPI2C_FLAG_TCR) != RESET) && (FMPI2C_CHECK_IT_SOURCE(ITSources, FMPI2C_IT_TCI) != RESET))
  3935. {
  3936. if ((hfmpi2c->XferCount != 0U) && (hfmpi2c->XferSize == 0U))
  3937. {
  3938. devaddress = (uint16_t)(hfmpi2c->Instance->CR2 & FMPI2C_CR2_SADD);
  3939. if (hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  3940. {
  3941. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  3942. FMPI2C_TransferConfig(hfmpi2c, devaddress, (uint8_t)hfmpi2c->XferSize, FMPI2C_RELOAD_MODE, FMPI2C_NO_STARTSTOP);
  3943. }
  3944. else
  3945. {
  3946. hfmpi2c->XferSize = hfmpi2c->XferCount;
  3947. if (hfmpi2c->XferOptions != FMPI2C_NO_OPTION_FRAME)
  3948. {
  3949. FMPI2C_TransferConfig(hfmpi2c, devaddress, (uint8_t)hfmpi2c->XferSize, hfmpi2c->XferOptions, FMPI2C_NO_STARTSTOP);
  3950. }
  3951. else
  3952. {
  3953. FMPI2C_TransferConfig(hfmpi2c, devaddress, (uint8_t)hfmpi2c->XferSize, FMPI2C_AUTOEND_MODE, FMPI2C_NO_STARTSTOP);
  3954. }
  3955. }
  3956. }
  3957. else
  3958. {
  3959. /* Call TxCpltCallback() if no stop mode is set */
  3960. if (FMPI2C_GET_STOP_MODE(hfmpi2c) != FMPI2C_AUTOEND_MODE)
  3961. {
  3962. /* Call FMPI2C Master Sequential complete process */
  3963. FMPI2C_ITMasterSeqCplt(hfmpi2c);
  3964. }
  3965. else
  3966. {
  3967. /* Wrong size Status regarding TCR flag event */
  3968. /* Call the corresponding callback to inform upper layer of End of Transfer */
  3969. FMPI2C_ITError(hfmpi2c, HAL_FMPI2C_ERROR_SIZE);
  3970. }
  3971. }
  3972. }
  3973. else if ((FMPI2C_CHECK_FLAG(tmpITFlags, FMPI2C_FLAG_TC) != RESET) && (FMPI2C_CHECK_IT_SOURCE(ITSources, FMPI2C_IT_TCI) != RESET))
  3974. {
  3975. if (hfmpi2c->XferCount == 0U)
  3976. {
  3977. if (FMPI2C_GET_STOP_MODE(hfmpi2c) != FMPI2C_AUTOEND_MODE)
  3978. {
  3979. /* Generate a stop condition in case of no transfer option */
  3980. if (hfmpi2c->XferOptions == FMPI2C_NO_OPTION_FRAME)
  3981. {
  3982. /* Generate Stop */
  3983. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_STOP;
  3984. }
  3985. else
  3986. {
  3987. /* Call FMPI2C Master Sequential complete process */
  3988. FMPI2C_ITMasterSeqCplt(hfmpi2c);
  3989. }
  3990. }
  3991. }
  3992. else
  3993. {
  3994. /* Wrong size Status regarding TC flag event */
  3995. /* Call the corresponding callback to inform upper layer of End of Transfer */
  3996. FMPI2C_ITError(hfmpi2c, HAL_FMPI2C_ERROR_SIZE);
  3997. }
  3998. }
  3999. else
  4000. {
  4001. /* Nothing to do */
  4002. }
  4003. if ((FMPI2C_CHECK_FLAG(tmpITFlags, FMPI2C_FLAG_STOPF) != RESET) && (FMPI2C_CHECK_IT_SOURCE(ITSources, FMPI2C_IT_STOPI) != RESET))
  4004. {
  4005. /* Call FMPI2C Master complete process */
  4006. FMPI2C_ITMasterCplt(hfmpi2c, tmpITFlags);
  4007. }
  4008. /* Process Unlocked */
  4009. __HAL_UNLOCK(hfmpi2c);
  4010. return HAL_OK;
  4011. }
  4012. /**
  4013. * @brief Interrupt Sub-Routine which handle the Interrupt Flags Slave Mode with Interrupt.
  4014. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  4015. * the configuration information for the specified FMPI2C.
  4016. * @param ITFlags Interrupt flags to handle.
  4017. * @param ITSources Interrupt sources enabled.
  4018. * @retval HAL status
  4019. */
  4020. static HAL_StatusTypeDef FMPI2C_Slave_ISR_IT(struct __FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags, uint32_t ITSources)
  4021. {
  4022. uint32_t tmpoptions = hfmpi2c->XferOptions;
  4023. uint32_t tmpITFlags = ITFlags;
  4024. /* Process locked */
  4025. __HAL_LOCK(hfmpi2c);
  4026. /* Check if STOPF is set */
  4027. if ((FMPI2C_CHECK_FLAG(tmpITFlags, FMPI2C_FLAG_STOPF) != RESET) && (FMPI2C_CHECK_IT_SOURCE(ITSources, FMPI2C_IT_STOPI) != RESET))
  4028. {
  4029. /* Call FMPI2C Slave complete process */
  4030. FMPI2C_ITSlaveCplt(hfmpi2c, tmpITFlags);
  4031. }
  4032. if ((FMPI2C_CHECK_FLAG(tmpITFlags, FMPI2C_FLAG_AF) != RESET) && (FMPI2C_CHECK_IT_SOURCE(ITSources, FMPI2C_IT_NACKI) != RESET))
  4033. {
  4034. /* Check that FMPI2C transfer finished */
  4035. /* if yes, normal use case, a NACK is sent by the MASTER when Transfer is finished */
  4036. /* Mean XferCount == 0*/
  4037. /* So clear Flag NACKF only */
  4038. if (hfmpi2c->XferCount == 0U)
  4039. {
  4040. /* Same action must be done for (tmpoptions == FMPI2C_LAST_FRAME) which removed for Warning[Pa134]: left and right operands are identical */
  4041. if ((hfmpi2c->State == HAL_FMPI2C_STATE_LISTEN) && (tmpoptions == FMPI2C_FIRST_AND_LAST_FRAME))
  4042. {
  4043. /* Call FMPI2C Listen complete process */
  4044. FMPI2C_ITListenCplt(hfmpi2c, tmpITFlags);
  4045. }
  4046. else if ((hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_TX_LISTEN) && (tmpoptions != FMPI2C_NO_OPTION_FRAME))
  4047. {
  4048. /* Clear NACK Flag */
  4049. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_AF);
  4050. /* Flush TX register */
  4051. FMPI2C_Flush_TXDR(hfmpi2c);
  4052. /* Last Byte is Transmitted */
  4053. /* Call FMPI2C Slave Sequential complete process */
  4054. FMPI2C_ITSlaveSeqCplt(hfmpi2c);
  4055. }
  4056. else
  4057. {
  4058. /* Clear NACK Flag */
  4059. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_AF);
  4060. }
  4061. }
  4062. else
  4063. {
  4064. /* if no, error use case, a Non-Acknowledge of last Data is generated by the MASTER*/
  4065. /* Clear NACK Flag */
  4066. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_AF);
  4067. /* Set ErrorCode corresponding to a Non-Acknowledge */
  4068. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_AF;
  4069. if ((tmpoptions == FMPI2C_FIRST_FRAME) || (tmpoptions == FMPI2C_NEXT_FRAME))
  4070. {
  4071. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4072. FMPI2C_ITError(hfmpi2c, hfmpi2c->ErrorCode);
  4073. }
  4074. }
  4075. }
  4076. else if ((FMPI2C_CHECK_FLAG(tmpITFlags, FMPI2C_FLAG_RXNE) != RESET) && (FMPI2C_CHECK_IT_SOURCE(ITSources, FMPI2C_IT_RXI) != RESET))
  4077. {
  4078. if (hfmpi2c->XferCount > 0U)
  4079. {
  4080. /* Read data from RXDR */
  4081. *hfmpi2c->pBuffPtr = (uint8_t)hfmpi2c->Instance->RXDR;
  4082. /* Increment Buffer pointer */
  4083. hfmpi2c->pBuffPtr++;
  4084. hfmpi2c->XferSize--;
  4085. hfmpi2c->XferCount--;
  4086. }
  4087. if ((hfmpi2c->XferCount == 0U) && \
  4088. (tmpoptions != FMPI2C_NO_OPTION_FRAME))
  4089. {
  4090. /* Call FMPI2C Slave Sequential complete process */
  4091. FMPI2C_ITSlaveSeqCplt(hfmpi2c);
  4092. }
  4093. }
  4094. else if ((FMPI2C_CHECK_FLAG(tmpITFlags, FMPI2C_FLAG_ADDR) != RESET) && \
  4095. (FMPI2C_CHECK_IT_SOURCE(ITSources, FMPI2C_IT_ADDRI) != RESET))
  4096. {
  4097. FMPI2C_ITAddrCplt(hfmpi2c, tmpITFlags);
  4098. }
  4099. else if ((FMPI2C_CHECK_FLAG(tmpITFlags, FMPI2C_FLAG_TXIS) != RESET) && (FMPI2C_CHECK_IT_SOURCE(ITSources, FMPI2C_IT_TXI) != RESET))
  4100. {
  4101. /* Write data to TXDR only if XferCount not reach "0" */
  4102. /* A TXIS flag can be set, during STOP treatment */
  4103. /* Check if all data have already been sent */
  4104. /* If it is the case, this last write in TXDR is not sent, correspond to a dummy TXIS event */
  4105. if (hfmpi2c->XferCount > 0U)
  4106. {
  4107. /* Write data to TXDR */
  4108. hfmpi2c->Instance->TXDR = *hfmpi2c->pBuffPtr;
  4109. /* Increment Buffer pointer */
  4110. hfmpi2c->pBuffPtr++;
  4111. hfmpi2c->XferCount--;
  4112. hfmpi2c->XferSize--;
  4113. }
  4114. else
  4115. {
  4116. if ((tmpoptions == FMPI2C_NEXT_FRAME) || (tmpoptions == FMPI2C_FIRST_FRAME))
  4117. {
  4118. /* Last Byte is Transmitted */
  4119. /* Call FMPI2C Slave Sequential complete process */
  4120. FMPI2C_ITSlaveSeqCplt(hfmpi2c);
  4121. }
  4122. }
  4123. }
  4124. else
  4125. {
  4126. /* Nothing to do */
  4127. }
  4128. /* Process Unlocked */
  4129. __HAL_UNLOCK(hfmpi2c);
  4130. return HAL_OK;
  4131. }
  4132. /**
  4133. * @brief Interrupt Sub-Routine which handle the Interrupt Flags Master Mode with DMA.
  4134. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  4135. * the configuration information for the specified FMPI2C.
  4136. * @param ITFlags Interrupt flags to handle.
  4137. * @param ITSources Interrupt sources enabled.
  4138. * @retval HAL status
  4139. */
  4140. static HAL_StatusTypeDef FMPI2C_Master_ISR_DMA(struct __FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags, uint32_t ITSources)
  4141. {
  4142. uint16_t devaddress;
  4143. uint32_t xfermode;
  4144. /* Process Locked */
  4145. __HAL_LOCK(hfmpi2c);
  4146. if ((FMPI2C_CHECK_FLAG(ITFlags, FMPI2C_FLAG_AF) != RESET) && (FMPI2C_CHECK_IT_SOURCE(ITSources, FMPI2C_IT_NACKI) != RESET))
  4147. {
  4148. /* Clear NACK Flag */
  4149. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_AF);
  4150. /* Set corresponding Error Code */
  4151. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_AF;
  4152. /* No need to generate STOP, it is automatically done */
  4153. /* But enable STOP interrupt, to treat it */
  4154. /* Error callback will be send during stop flag treatment */
  4155. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_CPLT_IT);
  4156. /* Flush TX register */
  4157. FMPI2C_Flush_TXDR(hfmpi2c);
  4158. }
  4159. else if ((FMPI2C_CHECK_FLAG(ITFlags, FMPI2C_FLAG_TCR) != RESET) && (FMPI2C_CHECK_IT_SOURCE(ITSources, FMPI2C_IT_TCI) != RESET))
  4160. {
  4161. /* Disable TC interrupt */
  4162. __HAL_FMPI2C_DISABLE_IT(hfmpi2c, FMPI2C_IT_TCI);
  4163. if (hfmpi2c->XferCount != 0U)
  4164. {
  4165. /* Recover Slave address */
  4166. devaddress = (uint16_t)(hfmpi2c->Instance->CR2 & FMPI2C_CR2_SADD);
  4167. /* Prepare the new XferSize to transfer */
  4168. if (hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  4169. {
  4170. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  4171. xfermode = FMPI2C_RELOAD_MODE;
  4172. }
  4173. else
  4174. {
  4175. hfmpi2c->XferSize = hfmpi2c->XferCount;
  4176. if (hfmpi2c->XferOptions != FMPI2C_NO_OPTION_FRAME)
  4177. {
  4178. xfermode = hfmpi2c->XferOptions;
  4179. }
  4180. else
  4181. {
  4182. xfermode = FMPI2C_AUTOEND_MODE;
  4183. }
  4184. }
  4185. /* Set the new XferSize in Nbytes register */
  4186. FMPI2C_TransferConfig(hfmpi2c, devaddress, (uint8_t)hfmpi2c->XferSize, xfermode, FMPI2C_NO_STARTSTOP);
  4187. /* Update XferCount value */
  4188. hfmpi2c->XferCount -= hfmpi2c->XferSize;
  4189. /* Enable DMA Request */
  4190. if (hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_RX)
  4191. {
  4192. hfmpi2c->Instance->CR1 |= FMPI2C_CR1_RXDMAEN;
  4193. }
  4194. else
  4195. {
  4196. hfmpi2c->Instance->CR1 |= FMPI2C_CR1_TXDMAEN;
  4197. }
  4198. }
  4199. else
  4200. {
  4201. /* Call TxCpltCallback() if no stop mode is set */
  4202. if (FMPI2C_GET_STOP_MODE(hfmpi2c) != FMPI2C_AUTOEND_MODE)
  4203. {
  4204. /* Call FMPI2C Master Sequential complete process */
  4205. FMPI2C_ITMasterSeqCplt(hfmpi2c);
  4206. }
  4207. else
  4208. {
  4209. /* Wrong size Status regarding TCR flag event */
  4210. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4211. FMPI2C_ITError(hfmpi2c, HAL_FMPI2C_ERROR_SIZE);
  4212. }
  4213. }
  4214. }
  4215. else if ((FMPI2C_CHECK_FLAG(ITFlags, FMPI2C_FLAG_TC) != RESET) && (FMPI2C_CHECK_IT_SOURCE(ITSources, FMPI2C_IT_TCI) != RESET))
  4216. {
  4217. if (hfmpi2c->XferCount == 0U)
  4218. {
  4219. if (FMPI2C_GET_STOP_MODE(hfmpi2c) != FMPI2C_AUTOEND_MODE)
  4220. {
  4221. /* Generate a stop condition in case of no transfer option */
  4222. if (hfmpi2c->XferOptions == FMPI2C_NO_OPTION_FRAME)
  4223. {
  4224. /* Generate Stop */
  4225. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_STOP;
  4226. }
  4227. else
  4228. {
  4229. /* Call FMPI2C Master Sequential complete process */
  4230. FMPI2C_ITMasterSeqCplt(hfmpi2c);
  4231. }
  4232. }
  4233. }
  4234. else
  4235. {
  4236. /* Wrong size Status regarding TC flag event */
  4237. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4238. FMPI2C_ITError(hfmpi2c, HAL_FMPI2C_ERROR_SIZE);
  4239. }
  4240. }
  4241. else if ((FMPI2C_CHECK_FLAG(ITFlags, FMPI2C_FLAG_STOPF) != RESET) && (FMPI2C_CHECK_IT_SOURCE(ITSources, FMPI2C_IT_STOPI) != RESET))
  4242. {
  4243. /* Call FMPI2C Master complete process */
  4244. FMPI2C_ITMasterCplt(hfmpi2c, ITFlags);
  4245. }
  4246. else
  4247. {
  4248. /* Nothing to do */
  4249. }
  4250. /* Process Unlocked */
  4251. __HAL_UNLOCK(hfmpi2c);
  4252. return HAL_OK;
  4253. }
  4254. /**
  4255. * @brief Interrupt Sub-Routine which handle the Interrupt Flags Slave Mode with DMA.
  4256. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  4257. * the configuration information for the specified FMPI2C.
  4258. * @param ITFlags Interrupt flags to handle.
  4259. * @param ITSources Interrupt sources enabled.
  4260. * @retval HAL status
  4261. */
  4262. static HAL_StatusTypeDef FMPI2C_Slave_ISR_DMA(struct __FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags, uint32_t ITSources)
  4263. {
  4264. uint32_t tmpoptions = hfmpi2c->XferOptions;
  4265. uint32_t treatdmanack = 0U;
  4266. HAL_FMPI2C_StateTypeDef tmpstate;
  4267. /* Process locked */
  4268. __HAL_LOCK(hfmpi2c);
  4269. /* Check if STOPF is set */
  4270. if ((FMPI2C_CHECK_FLAG(ITFlags, FMPI2C_FLAG_STOPF) != RESET) && (FMPI2C_CHECK_IT_SOURCE(ITSources, FMPI2C_IT_STOPI) != RESET))
  4271. {
  4272. /* Call FMPI2C Slave complete process */
  4273. FMPI2C_ITSlaveCplt(hfmpi2c, ITFlags);
  4274. }
  4275. if ((FMPI2C_CHECK_FLAG(ITFlags, FMPI2C_FLAG_AF) != RESET) && (FMPI2C_CHECK_IT_SOURCE(ITSources, FMPI2C_IT_NACKI) != RESET))
  4276. {
  4277. /* Check that FMPI2C transfer finished */
  4278. /* if yes, normal use case, a NACK is sent by the MASTER when Transfer is finished */
  4279. /* Mean XferCount == 0 */
  4280. /* So clear Flag NACKF only */
  4281. if ((FMPI2C_CHECK_IT_SOURCE(ITSources, FMPI2C_CR1_TXDMAEN) != RESET) ||
  4282. (FMPI2C_CHECK_IT_SOURCE(ITSources, FMPI2C_CR1_RXDMAEN) != RESET))
  4283. {
  4284. /* Split check of hdmarx, for MISRA compliance */
  4285. if (hfmpi2c->hdmarx != NULL)
  4286. {
  4287. if (FMPI2C_CHECK_IT_SOURCE(ITSources, FMPI2C_CR1_RXDMAEN) != RESET)
  4288. {
  4289. if (__HAL_DMA_GET_COUNTER(hfmpi2c->hdmarx) == 0U)
  4290. {
  4291. treatdmanack = 1U;
  4292. }
  4293. }
  4294. }
  4295. /* Split check of hdmatx, for MISRA compliance */
  4296. if (hfmpi2c->hdmatx != NULL)
  4297. {
  4298. if (FMPI2C_CHECK_IT_SOURCE(ITSources, FMPI2C_CR1_TXDMAEN) != RESET)
  4299. {
  4300. if (__HAL_DMA_GET_COUNTER(hfmpi2c->hdmatx) == 0U)
  4301. {
  4302. treatdmanack = 1U;
  4303. }
  4304. }
  4305. }
  4306. if (treatdmanack == 1U)
  4307. {
  4308. /* Same action must be done for (tmpoptions == FMPI2C_LAST_FRAME) which removed for Warning[Pa134]: left and right operands are identical */
  4309. if ((hfmpi2c->State == HAL_FMPI2C_STATE_LISTEN) && (tmpoptions == FMPI2C_FIRST_AND_LAST_FRAME))
  4310. {
  4311. /* Call FMPI2C Listen complete process */
  4312. FMPI2C_ITListenCplt(hfmpi2c, ITFlags);
  4313. }
  4314. else if ((hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_TX_LISTEN) && (tmpoptions != FMPI2C_NO_OPTION_FRAME))
  4315. {
  4316. /* Clear NACK Flag */
  4317. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_AF);
  4318. /* Flush TX register */
  4319. FMPI2C_Flush_TXDR(hfmpi2c);
  4320. /* Last Byte is Transmitted */
  4321. /* Call FMPI2C Slave Sequential complete process */
  4322. FMPI2C_ITSlaveSeqCplt(hfmpi2c);
  4323. }
  4324. else
  4325. {
  4326. /* Clear NACK Flag */
  4327. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_AF);
  4328. }
  4329. }
  4330. else
  4331. {
  4332. /* if no, error use case, a Non-Acknowledge of last Data is generated by the MASTER*/
  4333. /* Clear NACK Flag */
  4334. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_AF);
  4335. /* Set ErrorCode corresponding to a Non-Acknowledge */
  4336. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_AF;
  4337. /* Store current hfmpi2c->State, solve MISRA2012-Rule-13.5 */
  4338. tmpstate = hfmpi2c->State;
  4339. if ((tmpoptions == FMPI2C_FIRST_FRAME) || (tmpoptions == FMPI2C_NEXT_FRAME))
  4340. {
  4341. if ((tmpstate == HAL_FMPI2C_STATE_BUSY_TX) || (tmpstate == HAL_FMPI2C_STATE_BUSY_TX_LISTEN))
  4342. {
  4343. hfmpi2c->PreviousState = FMPI2C_STATE_SLAVE_BUSY_TX;
  4344. }
  4345. else if ((tmpstate == HAL_FMPI2C_STATE_BUSY_RX) || (tmpstate == HAL_FMPI2C_STATE_BUSY_RX_LISTEN))
  4346. {
  4347. hfmpi2c->PreviousState = FMPI2C_STATE_SLAVE_BUSY_RX;
  4348. }
  4349. else
  4350. {
  4351. /* Do nothing */
  4352. }
  4353. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4354. FMPI2C_ITError(hfmpi2c, hfmpi2c->ErrorCode);
  4355. }
  4356. }
  4357. }
  4358. else
  4359. {
  4360. /* Only Clear NACK Flag, no DMA treatment is pending */
  4361. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_AF);
  4362. }
  4363. }
  4364. else if ((FMPI2C_CHECK_FLAG(ITFlags, FMPI2C_FLAG_ADDR) != RESET) && (FMPI2C_CHECK_IT_SOURCE(ITSources, FMPI2C_IT_ADDRI) != RESET))
  4365. {
  4366. FMPI2C_ITAddrCplt(hfmpi2c, ITFlags);
  4367. }
  4368. else
  4369. {
  4370. /* Nothing to do */
  4371. }
  4372. /* Process Unlocked */
  4373. __HAL_UNLOCK(hfmpi2c);
  4374. return HAL_OK;
  4375. }
  4376. /**
  4377. * @brief Master sends target device address followed by internal memory address for write request.
  4378. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  4379. * the configuration information for the specified FMPI2C.
  4380. * @param DevAddress Target device address: The device 7 bits address value
  4381. * in datasheet must be shifted to the left before calling the interface
  4382. * @param MemAddress Internal memory address
  4383. * @param MemAddSize Size of internal memory address
  4384. * @param Timeout Timeout duration
  4385. * @param Tickstart Tick start value
  4386. * @retval HAL status
  4387. */
  4388. static HAL_StatusTypeDef FMPI2C_RequestMemoryWrite(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint16_t MemAddress,
  4389. uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart)
  4390. {
  4391. FMPI2C_TransferConfig(hfmpi2c, DevAddress, (uint8_t)MemAddSize, FMPI2C_RELOAD_MODE, FMPI2C_GENERATE_START_WRITE);
  4392. /* Wait until TXIS flag is set */
  4393. if (FMPI2C_WaitOnTXISFlagUntilTimeout(hfmpi2c, Timeout, Tickstart) != HAL_OK)
  4394. {
  4395. return HAL_ERROR;
  4396. }
  4397. /* If Memory address size is 8Bit */
  4398. if (MemAddSize == FMPI2C_MEMADD_SIZE_8BIT)
  4399. {
  4400. /* Send Memory Address */
  4401. hfmpi2c->Instance->TXDR = FMPI2C_MEM_ADD_LSB(MemAddress);
  4402. }
  4403. /* If Memory address size is 16Bit */
  4404. else
  4405. {
  4406. /* Send MSB of Memory Address */
  4407. hfmpi2c->Instance->TXDR = FMPI2C_MEM_ADD_MSB(MemAddress);
  4408. /* Wait until TXIS flag is set */
  4409. if (FMPI2C_WaitOnTXISFlagUntilTimeout(hfmpi2c, Timeout, Tickstart) != HAL_OK)
  4410. {
  4411. return HAL_ERROR;
  4412. }
  4413. /* Send LSB of Memory Address */
  4414. hfmpi2c->Instance->TXDR = FMPI2C_MEM_ADD_LSB(MemAddress);
  4415. }
  4416. /* Wait until TCR flag is set */
  4417. if (FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_TCR, RESET, Timeout, Tickstart) != HAL_OK)
  4418. {
  4419. return HAL_ERROR;
  4420. }
  4421. return HAL_OK;
  4422. }
  4423. /**
  4424. * @brief Master sends target device address followed by internal memory address for read request.
  4425. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  4426. * the configuration information for the specified FMPI2C.
  4427. * @param DevAddress Target device address: The device 7 bits address value
  4428. * in datasheet must be shifted to the left before calling the interface
  4429. * @param MemAddress Internal memory address
  4430. * @param MemAddSize Size of internal memory address
  4431. * @param Timeout Timeout duration
  4432. * @param Tickstart Tick start value
  4433. * @retval HAL status
  4434. */
  4435. static HAL_StatusTypeDef FMPI2C_RequestMemoryRead(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint16_t MemAddress,
  4436. uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart)
  4437. {
  4438. FMPI2C_TransferConfig(hfmpi2c, DevAddress, (uint8_t)MemAddSize, FMPI2C_SOFTEND_MODE, FMPI2C_GENERATE_START_WRITE);
  4439. /* Wait until TXIS flag is set */
  4440. if (FMPI2C_WaitOnTXISFlagUntilTimeout(hfmpi2c, Timeout, Tickstart) != HAL_OK)
  4441. {
  4442. return HAL_ERROR;
  4443. }
  4444. /* If Memory address size is 8Bit */
  4445. if (MemAddSize == FMPI2C_MEMADD_SIZE_8BIT)
  4446. {
  4447. /* Send Memory Address */
  4448. hfmpi2c->Instance->TXDR = FMPI2C_MEM_ADD_LSB(MemAddress);
  4449. }
  4450. /* If Memory address size is 16Bit */
  4451. else
  4452. {
  4453. /* Send MSB of Memory Address */
  4454. hfmpi2c->Instance->TXDR = FMPI2C_MEM_ADD_MSB(MemAddress);
  4455. /* Wait until TXIS flag is set */
  4456. if (FMPI2C_WaitOnTXISFlagUntilTimeout(hfmpi2c, Timeout, Tickstart) != HAL_OK)
  4457. {
  4458. return HAL_ERROR;
  4459. }
  4460. /* Send LSB of Memory Address */
  4461. hfmpi2c->Instance->TXDR = FMPI2C_MEM_ADD_LSB(MemAddress);
  4462. }
  4463. /* Wait until TC flag is set */
  4464. if (FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_TC, RESET, Timeout, Tickstart) != HAL_OK)
  4465. {
  4466. return HAL_ERROR;
  4467. }
  4468. return HAL_OK;
  4469. }
  4470. /**
  4471. * @brief FMPI2C Address complete process callback.
  4472. * @param hfmpi2c FMPI2C handle.
  4473. * @param ITFlags Interrupt flags to handle.
  4474. * @retval None
  4475. */
  4476. static void FMPI2C_ITAddrCplt(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags)
  4477. {
  4478. uint8_t transferdirection;
  4479. uint16_t slaveaddrcode;
  4480. uint16_t ownadd1code;
  4481. uint16_t ownadd2code;
  4482. /* Prevent unused argument(s) compilation warning */
  4483. UNUSED(ITFlags);
  4484. /* In case of Listen state, need to inform upper layer of address match code event */
  4485. if (((uint32_t)hfmpi2c->State & (uint32_t)HAL_FMPI2C_STATE_LISTEN) == (uint32_t)HAL_FMPI2C_STATE_LISTEN)
  4486. {
  4487. transferdirection = FMPI2C_GET_DIR(hfmpi2c);
  4488. slaveaddrcode = FMPI2C_GET_ADDR_MATCH(hfmpi2c);
  4489. ownadd1code = FMPI2C_GET_OWN_ADDRESS1(hfmpi2c);
  4490. ownadd2code = FMPI2C_GET_OWN_ADDRESS2(hfmpi2c);
  4491. /* If 10bits addressing mode is selected */
  4492. if (hfmpi2c->Init.AddressingMode == FMPI2C_ADDRESSINGMODE_10BIT)
  4493. {
  4494. if ((slaveaddrcode & SlaveAddr_MSK) == ((ownadd1code >> SlaveAddr_SHIFT) & SlaveAddr_MSK))
  4495. {
  4496. slaveaddrcode = ownadd1code;
  4497. hfmpi2c->AddrEventCount++;
  4498. if (hfmpi2c->AddrEventCount == 2U)
  4499. {
  4500. /* Reset Address Event counter */
  4501. hfmpi2c->AddrEventCount = 0U;
  4502. /* Clear ADDR flag */
  4503. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_ADDR);
  4504. /* Process Unlocked */
  4505. __HAL_UNLOCK(hfmpi2c);
  4506. /* Call Slave Addr callback */
  4507. #if (USE_HAL_FMPI2C_REGISTER_CALLBACKS == 1)
  4508. hfmpi2c->AddrCallback(hfmpi2c, transferdirection, slaveaddrcode);
  4509. #else
  4510. HAL_FMPI2C_AddrCallback(hfmpi2c, transferdirection, slaveaddrcode);
  4511. #endif /* USE_HAL_FMPI2C_REGISTER_CALLBACKS */
  4512. }
  4513. }
  4514. else
  4515. {
  4516. slaveaddrcode = ownadd2code;
  4517. /* Disable ADDR Interrupts */
  4518. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_LISTEN_IT);
  4519. /* Process Unlocked */
  4520. __HAL_UNLOCK(hfmpi2c);
  4521. /* Call Slave Addr callback */
  4522. #if (USE_HAL_FMPI2C_REGISTER_CALLBACKS == 1)
  4523. hfmpi2c->AddrCallback(hfmpi2c, transferdirection, slaveaddrcode);
  4524. #else
  4525. HAL_FMPI2C_AddrCallback(hfmpi2c, transferdirection, slaveaddrcode);
  4526. #endif /* USE_HAL_FMPI2C_REGISTER_CALLBACKS */
  4527. }
  4528. }
  4529. /* else 7 bits addressing mode is selected */
  4530. else
  4531. {
  4532. /* Disable ADDR Interrupts */
  4533. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_LISTEN_IT);
  4534. /* Process Unlocked */
  4535. __HAL_UNLOCK(hfmpi2c);
  4536. /* Call Slave Addr callback */
  4537. #if (USE_HAL_FMPI2C_REGISTER_CALLBACKS == 1)
  4538. hfmpi2c->AddrCallback(hfmpi2c, transferdirection, slaveaddrcode);
  4539. #else
  4540. HAL_FMPI2C_AddrCallback(hfmpi2c, transferdirection, slaveaddrcode);
  4541. #endif /* USE_HAL_FMPI2C_REGISTER_CALLBACKS */
  4542. }
  4543. }
  4544. /* Else clear address flag only */
  4545. else
  4546. {
  4547. /* Clear ADDR flag */
  4548. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_ADDR);
  4549. /* Process Unlocked */
  4550. __HAL_UNLOCK(hfmpi2c);
  4551. }
  4552. }
  4553. /**
  4554. * @brief FMPI2C Master sequential complete process.
  4555. * @param hfmpi2c FMPI2C handle.
  4556. * @retval None
  4557. */
  4558. static void FMPI2C_ITMasterSeqCplt(FMPI2C_HandleTypeDef *hfmpi2c)
  4559. {
  4560. /* Reset FMPI2C handle mode */
  4561. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  4562. /* No Generate Stop, to permit restart mode */
  4563. /* The stop will be done at the end of transfer, when FMPI2C_AUTOEND_MODE enable */
  4564. if (hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_TX)
  4565. {
  4566. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  4567. hfmpi2c->PreviousState = FMPI2C_STATE_MASTER_BUSY_TX;
  4568. hfmpi2c->XferISR = NULL;
  4569. /* Disable Interrupts */
  4570. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_TX_IT);
  4571. /* Process Unlocked */
  4572. __HAL_UNLOCK(hfmpi2c);
  4573. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4574. #if (USE_HAL_FMPI2C_REGISTER_CALLBACKS == 1)
  4575. hfmpi2c->MasterTxCpltCallback(hfmpi2c);
  4576. #else
  4577. HAL_FMPI2C_MasterTxCpltCallback(hfmpi2c);
  4578. #endif /* USE_HAL_FMPI2C_REGISTER_CALLBACKS */
  4579. }
  4580. /* hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_RX */
  4581. else
  4582. {
  4583. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  4584. hfmpi2c->PreviousState = FMPI2C_STATE_MASTER_BUSY_RX;
  4585. hfmpi2c->XferISR = NULL;
  4586. /* Disable Interrupts */
  4587. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_RX_IT);
  4588. /* Process Unlocked */
  4589. __HAL_UNLOCK(hfmpi2c);
  4590. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4591. #if (USE_HAL_FMPI2C_REGISTER_CALLBACKS == 1)
  4592. hfmpi2c->MasterRxCpltCallback(hfmpi2c);
  4593. #else
  4594. HAL_FMPI2C_MasterRxCpltCallback(hfmpi2c);
  4595. #endif /* USE_HAL_FMPI2C_REGISTER_CALLBACKS */
  4596. }
  4597. }
  4598. /**
  4599. * @brief FMPI2C Slave sequential complete process.
  4600. * @param hfmpi2c FMPI2C handle.
  4601. * @retval None
  4602. */
  4603. static void FMPI2C_ITSlaveSeqCplt(FMPI2C_HandleTypeDef *hfmpi2c)
  4604. {
  4605. uint32_t tmpcr1value = READ_REG(hfmpi2c->Instance->CR1);
  4606. /* Reset FMPI2C handle mode */
  4607. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  4608. /* If a DMA is ongoing, Update handle size context */
  4609. if (FMPI2C_CHECK_IT_SOURCE(tmpcr1value, FMPI2C_CR1_TXDMAEN) != RESET)
  4610. {
  4611. /* Disable DMA Request */
  4612. hfmpi2c->Instance->CR1 &= ~FMPI2C_CR1_TXDMAEN;
  4613. }
  4614. else if (FMPI2C_CHECK_IT_SOURCE(tmpcr1value, FMPI2C_CR1_RXDMAEN) != RESET)
  4615. {
  4616. /* Disable DMA Request */
  4617. hfmpi2c->Instance->CR1 &= ~FMPI2C_CR1_RXDMAEN;
  4618. }
  4619. else
  4620. {
  4621. /* Do nothing */
  4622. }
  4623. if (hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_TX_LISTEN)
  4624. {
  4625. /* Remove HAL_FMPI2C_STATE_SLAVE_BUSY_TX, keep only HAL_FMPI2C_STATE_LISTEN */
  4626. hfmpi2c->State = HAL_FMPI2C_STATE_LISTEN;
  4627. hfmpi2c->PreviousState = FMPI2C_STATE_SLAVE_BUSY_TX;
  4628. /* Disable Interrupts */
  4629. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_TX_IT);
  4630. /* Process Unlocked */
  4631. __HAL_UNLOCK(hfmpi2c);
  4632. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4633. #if (USE_HAL_FMPI2C_REGISTER_CALLBACKS == 1)
  4634. hfmpi2c->SlaveTxCpltCallback(hfmpi2c);
  4635. #else
  4636. HAL_FMPI2C_SlaveTxCpltCallback(hfmpi2c);
  4637. #endif /* USE_HAL_FMPI2C_REGISTER_CALLBACKS */
  4638. }
  4639. else if (hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_RX_LISTEN)
  4640. {
  4641. /* Remove HAL_FMPI2C_STATE_SLAVE_BUSY_RX, keep only HAL_FMPI2C_STATE_LISTEN */
  4642. hfmpi2c->State = HAL_FMPI2C_STATE_LISTEN;
  4643. hfmpi2c->PreviousState = FMPI2C_STATE_SLAVE_BUSY_RX;
  4644. /* Disable Interrupts */
  4645. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_RX_IT);
  4646. /* Process Unlocked */
  4647. __HAL_UNLOCK(hfmpi2c);
  4648. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4649. #if (USE_HAL_FMPI2C_REGISTER_CALLBACKS == 1)
  4650. hfmpi2c->SlaveRxCpltCallback(hfmpi2c);
  4651. #else
  4652. HAL_FMPI2C_SlaveRxCpltCallback(hfmpi2c);
  4653. #endif /* USE_HAL_FMPI2C_REGISTER_CALLBACKS */
  4654. }
  4655. else
  4656. {
  4657. /* Nothing to do */
  4658. }
  4659. }
  4660. /**
  4661. * @brief FMPI2C Master complete process.
  4662. * @param hfmpi2c FMPI2C handle.
  4663. * @param ITFlags Interrupt flags to handle.
  4664. * @retval None
  4665. */
  4666. static void FMPI2C_ITMasterCplt(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags)
  4667. {
  4668. uint32_t tmperror;
  4669. uint32_t tmpITFlags = ITFlags;
  4670. __IO uint32_t tmpreg;
  4671. /* Clear STOP Flag */
  4672. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  4673. /* Disable Interrupts and Store Previous state */
  4674. if (hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_TX)
  4675. {
  4676. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_TX_IT);
  4677. hfmpi2c->PreviousState = FMPI2C_STATE_MASTER_BUSY_TX;
  4678. }
  4679. else if (hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_RX)
  4680. {
  4681. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_RX_IT);
  4682. hfmpi2c->PreviousState = FMPI2C_STATE_MASTER_BUSY_RX;
  4683. }
  4684. else
  4685. {
  4686. /* Do nothing */
  4687. }
  4688. /* Clear Configuration Register 2 */
  4689. FMPI2C_RESET_CR2(hfmpi2c);
  4690. /* Reset handle parameters */
  4691. hfmpi2c->XferISR = NULL;
  4692. hfmpi2c->XferOptions = FMPI2C_NO_OPTION_FRAME;
  4693. if (FMPI2C_CHECK_FLAG(tmpITFlags, FMPI2C_FLAG_AF) != RESET)
  4694. {
  4695. /* Clear NACK Flag */
  4696. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_AF);
  4697. /* Set acknowledge error code */
  4698. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_AF;
  4699. }
  4700. /* Fetch Last receive data if any */
  4701. if ((hfmpi2c->State == HAL_FMPI2C_STATE_ABORT) && (FMPI2C_CHECK_FLAG(tmpITFlags, FMPI2C_FLAG_RXNE) != RESET))
  4702. {
  4703. /* Read data from RXDR */
  4704. tmpreg = (uint8_t)hfmpi2c->Instance->RXDR;
  4705. UNUSED(tmpreg);
  4706. }
  4707. /* Flush TX register */
  4708. FMPI2C_Flush_TXDR(hfmpi2c);
  4709. /* Store current volatile hfmpi2c->ErrorCode, misra rule */
  4710. tmperror = hfmpi2c->ErrorCode;
  4711. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4712. if ((hfmpi2c->State == HAL_FMPI2C_STATE_ABORT) || (tmperror != HAL_FMPI2C_ERROR_NONE))
  4713. {
  4714. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4715. FMPI2C_ITError(hfmpi2c, hfmpi2c->ErrorCode);
  4716. }
  4717. /* hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_TX */
  4718. else if (hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_TX)
  4719. {
  4720. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  4721. hfmpi2c->PreviousState = FMPI2C_STATE_NONE;
  4722. if (hfmpi2c->Mode == HAL_FMPI2C_MODE_MEM)
  4723. {
  4724. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  4725. /* Process Unlocked */
  4726. __HAL_UNLOCK(hfmpi2c);
  4727. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4728. #if (USE_HAL_FMPI2C_REGISTER_CALLBACKS == 1)
  4729. hfmpi2c->MemTxCpltCallback(hfmpi2c);
  4730. #else
  4731. HAL_FMPI2C_MemTxCpltCallback(hfmpi2c);
  4732. #endif /* USE_HAL_FMPI2C_REGISTER_CALLBACKS */
  4733. }
  4734. else
  4735. {
  4736. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  4737. /* Process Unlocked */
  4738. __HAL_UNLOCK(hfmpi2c);
  4739. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4740. #if (USE_HAL_FMPI2C_REGISTER_CALLBACKS == 1)
  4741. hfmpi2c->MasterTxCpltCallback(hfmpi2c);
  4742. #else
  4743. HAL_FMPI2C_MasterTxCpltCallback(hfmpi2c);
  4744. #endif /* USE_HAL_FMPI2C_REGISTER_CALLBACKS */
  4745. }
  4746. }
  4747. /* hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_RX */
  4748. else if (hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_RX)
  4749. {
  4750. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  4751. hfmpi2c->PreviousState = FMPI2C_STATE_NONE;
  4752. if (hfmpi2c->Mode == HAL_FMPI2C_MODE_MEM)
  4753. {
  4754. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  4755. /* Process Unlocked */
  4756. __HAL_UNLOCK(hfmpi2c);
  4757. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4758. #if (USE_HAL_FMPI2C_REGISTER_CALLBACKS == 1)
  4759. hfmpi2c->MemRxCpltCallback(hfmpi2c);
  4760. #else
  4761. HAL_FMPI2C_MemRxCpltCallback(hfmpi2c);
  4762. #endif /* USE_HAL_FMPI2C_REGISTER_CALLBACKS */
  4763. }
  4764. else
  4765. {
  4766. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  4767. /* Process Unlocked */
  4768. __HAL_UNLOCK(hfmpi2c);
  4769. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4770. #if (USE_HAL_FMPI2C_REGISTER_CALLBACKS == 1)
  4771. hfmpi2c->MasterRxCpltCallback(hfmpi2c);
  4772. #else
  4773. HAL_FMPI2C_MasterRxCpltCallback(hfmpi2c);
  4774. #endif /* USE_HAL_FMPI2C_REGISTER_CALLBACKS */
  4775. }
  4776. }
  4777. else
  4778. {
  4779. /* Nothing to do */
  4780. }
  4781. }
  4782. /**
  4783. * @brief FMPI2C Slave complete process.
  4784. * @param hfmpi2c FMPI2C handle.
  4785. * @param ITFlags Interrupt flags to handle.
  4786. * @retval None
  4787. */
  4788. static void FMPI2C_ITSlaveCplt(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags)
  4789. {
  4790. uint32_t tmpcr1value = READ_REG(hfmpi2c->Instance->CR1);
  4791. uint32_t tmpITFlags = ITFlags;
  4792. HAL_FMPI2C_StateTypeDef tmpstate = hfmpi2c->State;
  4793. /* Clear STOP Flag */
  4794. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  4795. /* Disable Interrupts and Store Previous state */
  4796. if ((tmpstate == HAL_FMPI2C_STATE_BUSY_TX) || (tmpstate == HAL_FMPI2C_STATE_BUSY_TX_LISTEN))
  4797. {
  4798. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_LISTEN_IT | FMPI2C_XFER_TX_IT);
  4799. hfmpi2c->PreviousState = FMPI2C_STATE_SLAVE_BUSY_TX;
  4800. }
  4801. else if ((tmpstate == HAL_FMPI2C_STATE_BUSY_RX) || (tmpstate == HAL_FMPI2C_STATE_BUSY_RX_LISTEN))
  4802. {
  4803. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_LISTEN_IT | FMPI2C_XFER_RX_IT);
  4804. hfmpi2c->PreviousState = FMPI2C_STATE_SLAVE_BUSY_RX;
  4805. }
  4806. else
  4807. {
  4808. /* Do nothing */
  4809. }
  4810. /* Disable Address Acknowledge */
  4811. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  4812. /* Clear Configuration Register 2 */
  4813. FMPI2C_RESET_CR2(hfmpi2c);
  4814. /* Flush TX register */
  4815. FMPI2C_Flush_TXDR(hfmpi2c);
  4816. /* If a DMA is ongoing, Update handle size context */
  4817. if (FMPI2C_CHECK_IT_SOURCE(tmpcr1value, FMPI2C_CR1_TXDMAEN) != RESET)
  4818. {
  4819. /* Disable DMA Request */
  4820. hfmpi2c->Instance->CR1 &= ~FMPI2C_CR1_TXDMAEN;
  4821. if (hfmpi2c->hdmatx != NULL)
  4822. {
  4823. hfmpi2c->XferCount = (uint16_t)__HAL_DMA_GET_COUNTER(hfmpi2c->hdmatx);
  4824. }
  4825. }
  4826. else if (FMPI2C_CHECK_IT_SOURCE(tmpcr1value, FMPI2C_CR1_RXDMAEN) != RESET)
  4827. {
  4828. /* Disable DMA Request */
  4829. hfmpi2c->Instance->CR1 &= ~FMPI2C_CR1_RXDMAEN;
  4830. if (hfmpi2c->hdmarx != NULL)
  4831. {
  4832. hfmpi2c->XferCount = (uint16_t)__HAL_DMA_GET_COUNTER(hfmpi2c->hdmarx);
  4833. }
  4834. }
  4835. else
  4836. {
  4837. /* Do nothing */
  4838. }
  4839. /* Store Last receive data if any */
  4840. if (FMPI2C_CHECK_FLAG(tmpITFlags, FMPI2C_FLAG_RXNE) != RESET)
  4841. {
  4842. /* Remove RXNE flag on temporary variable as read done */
  4843. tmpITFlags &= ~FMPI2C_FLAG_RXNE;
  4844. /* Read data from RXDR */
  4845. *hfmpi2c->pBuffPtr = (uint8_t)hfmpi2c->Instance->RXDR;
  4846. /* Increment Buffer pointer */
  4847. hfmpi2c->pBuffPtr++;
  4848. if ((hfmpi2c->XferSize > 0U))
  4849. {
  4850. hfmpi2c->XferSize--;
  4851. hfmpi2c->XferCount--;
  4852. }
  4853. }
  4854. /* All data are not transferred, so set error code accordingly */
  4855. if (hfmpi2c->XferCount != 0U)
  4856. {
  4857. /* Set ErrorCode corresponding to a Non-Acknowledge */
  4858. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_AF;
  4859. }
  4860. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  4861. hfmpi2c->XferISR = NULL;
  4862. if (hfmpi2c->ErrorCode != HAL_FMPI2C_ERROR_NONE)
  4863. {
  4864. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4865. FMPI2C_ITError(hfmpi2c, hfmpi2c->ErrorCode);
  4866. /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */
  4867. if (hfmpi2c->State == HAL_FMPI2C_STATE_LISTEN)
  4868. {
  4869. /* Call FMPI2C Listen complete process */
  4870. FMPI2C_ITListenCplt(hfmpi2c, tmpITFlags);
  4871. }
  4872. }
  4873. else if (hfmpi2c->XferOptions != FMPI2C_NO_OPTION_FRAME)
  4874. {
  4875. /* Call the Sequential Complete callback, to inform upper layer of the end of Transfer */
  4876. FMPI2C_ITSlaveSeqCplt(hfmpi2c);
  4877. hfmpi2c->XferOptions = FMPI2C_NO_OPTION_FRAME;
  4878. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  4879. hfmpi2c->PreviousState = FMPI2C_STATE_NONE;
  4880. /* Process Unlocked */
  4881. __HAL_UNLOCK(hfmpi2c);
  4882. /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */
  4883. #if (USE_HAL_FMPI2C_REGISTER_CALLBACKS == 1)
  4884. hfmpi2c->ListenCpltCallback(hfmpi2c);
  4885. #else
  4886. HAL_FMPI2C_ListenCpltCallback(hfmpi2c);
  4887. #endif /* USE_HAL_FMPI2C_REGISTER_CALLBACKS */
  4888. }
  4889. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4890. else if (hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_RX)
  4891. {
  4892. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  4893. hfmpi2c->PreviousState = FMPI2C_STATE_NONE;
  4894. /* Process Unlocked */
  4895. __HAL_UNLOCK(hfmpi2c);
  4896. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4897. #if (USE_HAL_FMPI2C_REGISTER_CALLBACKS == 1)
  4898. hfmpi2c->SlaveRxCpltCallback(hfmpi2c);
  4899. #else
  4900. HAL_FMPI2C_SlaveRxCpltCallback(hfmpi2c);
  4901. #endif /* USE_HAL_FMPI2C_REGISTER_CALLBACKS */
  4902. }
  4903. else
  4904. {
  4905. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  4906. hfmpi2c->PreviousState = FMPI2C_STATE_NONE;
  4907. /* Process Unlocked */
  4908. __HAL_UNLOCK(hfmpi2c);
  4909. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4910. #if (USE_HAL_FMPI2C_REGISTER_CALLBACKS == 1)
  4911. hfmpi2c->SlaveTxCpltCallback(hfmpi2c);
  4912. #else
  4913. HAL_FMPI2C_SlaveTxCpltCallback(hfmpi2c);
  4914. #endif /* USE_HAL_FMPI2C_REGISTER_CALLBACKS */
  4915. }
  4916. }
  4917. /**
  4918. * @brief FMPI2C Listen complete process.
  4919. * @param hfmpi2c FMPI2C handle.
  4920. * @param ITFlags Interrupt flags to handle.
  4921. * @retval None
  4922. */
  4923. static void FMPI2C_ITListenCplt(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags)
  4924. {
  4925. /* Reset handle parameters */
  4926. hfmpi2c->XferOptions = FMPI2C_NO_OPTION_FRAME;
  4927. hfmpi2c->PreviousState = FMPI2C_STATE_NONE;
  4928. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  4929. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  4930. hfmpi2c->XferISR = NULL;
  4931. /* Store Last receive data if any */
  4932. if (FMPI2C_CHECK_FLAG(ITFlags, FMPI2C_FLAG_RXNE) != RESET)
  4933. {
  4934. /* Read data from RXDR */
  4935. *hfmpi2c->pBuffPtr = (uint8_t)hfmpi2c->Instance->RXDR;
  4936. /* Increment Buffer pointer */
  4937. hfmpi2c->pBuffPtr++;
  4938. if ((hfmpi2c->XferSize > 0U))
  4939. {
  4940. hfmpi2c->XferSize--;
  4941. hfmpi2c->XferCount--;
  4942. /* Set ErrorCode corresponding to a Non-Acknowledge */
  4943. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_AF;
  4944. }
  4945. }
  4946. /* Disable all Interrupts*/
  4947. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_LISTEN_IT | FMPI2C_XFER_RX_IT | FMPI2C_XFER_TX_IT);
  4948. /* Clear NACK Flag */
  4949. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_AF);
  4950. /* Process Unlocked */
  4951. __HAL_UNLOCK(hfmpi2c);
  4952. /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */
  4953. #if (USE_HAL_FMPI2C_REGISTER_CALLBACKS == 1)
  4954. hfmpi2c->ListenCpltCallback(hfmpi2c);
  4955. #else
  4956. HAL_FMPI2C_ListenCpltCallback(hfmpi2c);
  4957. #endif /* USE_HAL_FMPI2C_REGISTER_CALLBACKS */
  4958. }
  4959. /**
  4960. * @brief FMPI2C interrupts error process.
  4961. * @param hfmpi2c FMPI2C handle.
  4962. * @param ErrorCode Error code to handle.
  4963. * @retval None
  4964. */
  4965. static void FMPI2C_ITError(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ErrorCode)
  4966. {
  4967. HAL_FMPI2C_StateTypeDef tmpstate = hfmpi2c->State;
  4968. uint32_t tmppreviousstate;
  4969. /* Reset handle parameters */
  4970. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  4971. hfmpi2c->XferOptions = FMPI2C_NO_OPTION_FRAME;
  4972. hfmpi2c->XferCount = 0U;
  4973. /* Set new error code */
  4974. hfmpi2c->ErrorCode |= ErrorCode;
  4975. /* Disable Interrupts */
  4976. if ((tmpstate == HAL_FMPI2C_STATE_LISTEN) ||
  4977. (tmpstate == HAL_FMPI2C_STATE_BUSY_TX_LISTEN) ||
  4978. (tmpstate == HAL_FMPI2C_STATE_BUSY_RX_LISTEN))
  4979. {
  4980. /* Disable all interrupts, except interrupts related to LISTEN state */
  4981. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_RX_IT | FMPI2C_XFER_TX_IT);
  4982. /* keep HAL_FMPI2C_STATE_LISTEN if set */
  4983. hfmpi2c->State = HAL_FMPI2C_STATE_LISTEN;
  4984. hfmpi2c->XferISR = FMPI2C_Slave_ISR_IT;
  4985. }
  4986. else
  4987. {
  4988. /* Disable all interrupts */
  4989. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_LISTEN_IT | FMPI2C_XFER_RX_IT | FMPI2C_XFER_TX_IT);
  4990. /* If state is an abort treatment on going, don't change state */
  4991. /* This change will be do later */
  4992. if (hfmpi2c->State != HAL_FMPI2C_STATE_ABORT)
  4993. {
  4994. /* Set HAL_FMPI2C_STATE_READY */
  4995. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  4996. }
  4997. hfmpi2c->XferISR = NULL;
  4998. }
  4999. /* Abort DMA TX transfer if any */
  5000. tmppreviousstate = hfmpi2c->PreviousState;
  5001. if ((hfmpi2c->hdmatx != NULL) && ((tmppreviousstate == FMPI2C_STATE_MASTER_BUSY_TX) || \
  5002. (tmppreviousstate == FMPI2C_STATE_SLAVE_BUSY_TX)))
  5003. {
  5004. if ((hfmpi2c->Instance->CR1 & FMPI2C_CR1_TXDMAEN) == FMPI2C_CR1_TXDMAEN)
  5005. {
  5006. hfmpi2c->Instance->CR1 &= ~FMPI2C_CR1_TXDMAEN;
  5007. }
  5008. if (HAL_DMA_GetState(hfmpi2c->hdmatx) != HAL_DMA_STATE_READY)
  5009. {
  5010. /* Set the FMPI2C DMA Abort callback :
  5011. will lead to call HAL_FMPI2C_ErrorCallback() at end of DMA abort procedure */
  5012. hfmpi2c->hdmatx->XferAbortCallback = FMPI2C_DMAAbort;
  5013. /* Process Unlocked */
  5014. __HAL_UNLOCK(hfmpi2c);
  5015. /* Abort DMA TX */
  5016. if (HAL_DMA_Abort_IT(hfmpi2c->hdmatx) != HAL_OK)
  5017. {
  5018. /* Call Directly XferAbortCallback function in case of error */
  5019. hfmpi2c->hdmatx->XferAbortCallback(hfmpi2c->hdmatx);
  5020. }
  5021. }
  5022. else
  5023. {
  5024. FMPI2C_TreatErrorCallback(hfmpi2c);
  5025. }
  5026. }
  5027. /* Abort DMA RX transfer if any */
  5028. else if ((hfmpi2c->hdmarx != NULL) && ((tmppreviousstate == FMPI2C_STATE_MASTER_BUSY_RX) || \
  5029. (tmppreviousstate == FMPI2C_STATE_SLAVE_BUSY_RX)))
  5030. {
  5031. if ((hfmpi2c->Instance->CR1 & FMPI2C_CR1_RXDMAEN) == FMPI2C_CR1_RXDMAEN)
  5032. {
  5033. hfmpi2c->Instance->CR1 &= ~FMPI2C_CR1_RXDMAEN;
  5034. }
  5035. if (HAL_DMA_GetState(hfmpi2c->hdmarx) != HAL_DMA_STATE_READY)
  5036. {
  5037. /* Set the FMPI2C DMA Abort callback :
  5038. will lead to call HAL_FMPI2C_ErrorCallback() at end of DMA abort procedure */
  5039. hfmpi2c->hdmarx->XferAbortCallback = FMPI2C_DMAAbort;
  5040. /* Process Unlocked */
  5041. __HAL_UNLOCK(hfmpi2c);
  5042. /* Abort DMA RX */
  5043. if (HAL_DMA_Abort_IT(hfmpi2c->hdmarx) != HAL_OK)
  5044. {
  5045. /* Call Directly hfmpi2c->hdmarx->XferAbortCallback function in case of error */
  5046. hfmpi2c->hdmarx->XferAbortCallback(hfmpi2c->hdmarx);
  5047. }
  5048. }
  5049. else
  5050. {
  5051. FMPI2C_TreatErrorCallback(hfmpi2c);
  5052. }
  5053. }
  5054. else
  5055. {
  5056. FMPI2C_TreatErrorCallback(hfmpi2c);
  5057. }
  5058. }
  5059. /**
  5060. * @brief FMPI2C Error callback treatment.
  5061. * @param hfmpi2c FMPI2C handle.
  5062. * @retval None
  5063. */
  5064. static void FMPI2C_TreatErrorCallback(FMPI2C_HandleTypeDef *hfmpi2c)
  5065. {
  5066. if (hfmpi2c->State == HAL_FMPI2C_STATE_ABORT)
  5067. {
  5068. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  5069. hfmpi2c->PreviousState = FMPI2C_STATE_NONE;
  5070. /* Process Unlocked */
  5071. __HAL_UNLOCK(hfmpi2c);
  5072. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5073. #if (USE_HAL_FMPI2C_REGISTER_CALLBACKS == 1)
  5074. hfmpi2c->AbortCpltCallback(hfmpi2c);
  5075. #else
  5076. HAL_FMPI2C_AbortCpltCallback(hfmpi2c);
  5077. #endif /* USE_HAL_FMPI2C_REGISTER_CALLBACKS */
  5078. }
  5079. else
  5080. {
  5081. hfmpi2c->PreviousState = FMPI2C_STATE_NONE;
  5082. /* Process Unlocked */
  5083. __HAL_UNLOCK(hfmpi2c);
  5084. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5085. #if (USE_HAL_FMPI2C_REGISTER_CALLBACKS == 1)
  5086. hfmpi2c->ErrorCallback(hfmpi2c);
  5087. #else
  5088. HAL_FMPI2C_ErrorCallback(hfmpi2c);
  5089. #endif /* USE_HAL_FMPI2C_REGISTER_CALLBACKS */
  5090. }
  5091. }
  5092. /**
  5093. * @brief FMPI2C Tx data register flush process.
  5094. * @param hfmpi2c FMPI2C handle.
  5095. * @retval None
  5096. */
  5097. static void FMPI2C_Flush_TXDR(FMPI2C_HandleTypeDef *hfmpi2c)
  5098. {
  5099. /* If a pending TXIS flag is set */
  5100. /* Write a dummy data in TXDR to clear it */
  5101. if (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_TXIS) != RESET)
  5102. {
  5103. hfmpi2c->Instance->TXDR = 0x00U;
  5104. }
  5105. /* Flush TX register if not empty */
  5106. if (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_TXE) == RESET)
  5107. {
  5108. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_TXE);
  5109. }
  5110. }
  5111. /**
  5112. * @brief DMA FMPI2C master transmit process complete callback.
  5113. * @param hdma DMA handle
  5114. * @retval None
  5115. */
  5116. static void FMPI2C_DMAMasterTransmitCplt(DMA_HandleTypeDef *hdma)
  5117. {
  5118. FMPI2C_HandleTypeDef *hfmpi2c = (FMPI2C_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
  5119. /* Disable DMA Request */
  5120. hfmpi2c->Instance->CR1 &= ~FMPI2C_CR1_TXDMAEN;
  5121. /* If last transfer, enable STOP interrupt */
  5122. if (hfmpi2c->XferCount == 0U)
  5123. {
  5124. /* Enable STOP interrupt */
  5125. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_CPLT_IT);
  5126. }
  5127. /* else prepare a new DMA transfer and enable TCReload interrupt */
  5128. else
  5129. {
  5130. /* Update Buffer pointer */
  5131. hfmpi2c->pBuffPtr += hfmpi2c->XferSize;
  5132. /* Set the XferSize to transfer */
  5133. if (hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  5134. {
  5135. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  5136. }
  5137. else
  5138. {
  5139. hfmpi2c->XferSize = hfmpi2c->XferCount;
  5140. }
  5141. /* Enable the DMA stream */
  5142. if (HAL_DMA_Start_IT(hfmpi2c->hdmatx, (uint32_t)hfmpi2c->pBuffPtr, (uint32_t)&hfmpi2c->Instance->TXDR, hfmpi2c->XferSize) != HAL_OK)
  5143. {
  5144. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5145. FMPI2C_ITError(hfmpi2c, HAL_FMPI2C_ERROR_DMA);
  5146. }
  5147. else
  5148. {
  5149. /* Enable TC interrupts */
  5150. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_RELOAD_IT);
  5151. }
  5152. }
  5153. }
  5154. /**
  5155. * @brief DMA FMPI2C slave transmit process complete callback.
  5156. * @param hdma DMA handle
  5157. * @retval None
  5158. */
  5159. static void FMPI2C_DMASlaveTransmitCplt(DMA_HandleTypeDef *hdma)
  5160. {
  5161. FMPI2C_HandleTypeDef *hfmpi2c = (FMPI2C_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
  5162. uint32_t tmpoptions = hfmpi2c->XferOptions;
  5163. if ((tmpoptions == FMPI2C_NEXT_FRAME) || (tmpoptions == FMPI2C_FIRST_FRAME))
  5164. {
  5165. /* Disable DMA Request */
  5166. hfmpi2c->Instance->CR1 &= ~FMPI2C_CR1_TXDMAEN;
  5167. /* Last Byte is Transmitted */
  5168. /* Call FMPI2C Slave Sequential complete process */
  5169. FMPI2C_ITSlaveSeqCplt(hfmpi2c);
  5170. }
  5171. else
  5172. {
  5173. /* No specific action, Master fully manage the generation of STOP condition */
  5174. /* Mean that this generation can arrive at any time, at the end or during DMA process */
  5175. /* So STOP condition should be manage through Interrupt treatment */
  5176. }
  5177. }
  5178. /**
  5179. * @brief DMA FMPI2C master receive process complete callback.
  5180. * @param hdma DMA handle
  5181. * @retval None
  5182. */
  5183. static void FMPI2C_DMAMasterReceiveCplt(DMA_HandleTypeDef *hdma)
  5184. {
  5185. FMPI2C_HandleTypeDef *hfmpi2c = (FMPI2C_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
  5186. /* Disable DMA Request */
  5187. hfmpi2c->Instance->CR1 &= ~FMPI2C_CR1_RXDMAEN;
  5188. /* If last transfer, enable STOP interrupt */
  5189. if (hfmpi2c->XferCount == 0U)
  5190. {
  5191. /* Enable STOP interrupt */
  5192. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_CPLT_IT);
  5193. }
  5194. /* else prepare a new DMA transfer and enable TCReload interrupt */
  5195. else
  5196. {
  5197. /* Update Buffer pointer */
  5198. hfmpi2c->pBuffPtr += hfmpi2c->XferSize;
  5199. /* Set the XferSize to transfer */
  5200. if (hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  5201. {
  5202. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  5203. }
  5204. else
  5205. {
  5206. hfmpi2c->XferSize = hfmpi2c->XferCount;
  5207. }
  5208. /* Enable the DMA stream */
  5209. if (HAL_DMA_Start_IT(hfmpi2c->hdmarx, (uint32_t)&hfmpi2c->Instance->RXDR, (uint32_t)hfmpi2c->pBuffPtr, hfmpi2c->XferSize) != HAL_OK)
  5210. {
  5211. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5212. FMPI2C_ITError(hfmpi2c, HAL_FMPI2C_ERROR_DMA);
  5213. }
  5214. else
  5215. {
  5216. /* Enable TC interrupts */
  5217. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_RELOAD_IT);
  5218. }
  5219. }
  5220. }
  5221. /**
  5222. * @brief DMA FMPI2C slave receive process complete callback.
  5223. * @param hdma DMA handle
  5224. * @retval None
  5225. */
  5226. static void FMPI2C_DMASlaveReceiveCplt(DMA_HandleTypeDef *hdma)
  5227. {
  5228. FMPI2C_HandleTypeDef *hfmpi2c = (FMPI2C_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
  5229. uint32_t tmpoptions = hfmpi2c->XferOptions;
  5230. if ((__HAL_DMA_GET_COUNTER(hfmpi2c->hdmarx) == 0U) && \
  5231. (tmpoptions != FMPI2C_NO_OPTION_FRAME))
  5232. {
  5233. /* Disable DMA Request */
  5234. hfmpi2c->Instance->CR1 &= ~FMPI2C_CR1_RXDMAEN;
  5235. /* Call FMPI2C Slave Sequential complete process */
  5236. FMPI2C_ITSlaveSeqCplt(hfmpi2c);
  5237. }
  5238. else
  5239. {
  5240. /* No specific action, Master fully manage the generation of STOP condition */
  5241. /* Mean that this generation can arrive at any time, at the end or during DMA process */
  5242. /* So STOP condition should be manage through Interrupt treatment */
  5243. }
  5244. }
  5245. /**
  5246. * @brief DMA FMPI2C communication error callback.
  5247. * @param hdma DMA handle
  5248. * @retval None
  5249. */
  5250. static void FMPI2C_DMAError(DMA_HandleTypeDef *hdma)
  5251. {
  5252. uint32_t treatdmaerror = 0U;
  5253. FMPI2C_HandleTypeDef *hfmpi2c = (FMPI2C_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
  5254. if (hfmpi2c->hdmatx != NULL)
  5255. {
  5256. if (__HAL_DMA_GET_COUNTER(hfmpi2c->hdmatx) == 0U)
  5257. {
  5258. treatdmaerror = 1U;
  5259. }
  5260. }
  5261. if (hfmpi2c->hdmarx != NULL)
  5262. {
  5263. if (__HAL_DMA_GET_COUNTER(hfmpi2c->hdmarx) == 0U)
  5264. {
  5265. treatdmaerror = 1U;
  5266. }
  5267. }
  5268. /* Check if a FIFO error is detected, if true normal use case, so no specific action to perform */
  5269. if (!((HAL_DMA_GetError(hdma) == HAL_DMA_ERROR_FE)) && (treatdmaerror != 0U))
  5270. {
  5271. /* Disable Acknowledge */
  5272. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  5273. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5274. FMPI2C_ITError(hfmpi2c, HAL_FMPI2C_ERROR_DMA);
  5275. }
  5276. }
  5277. /**
  5278. * @brief DMA FMPI2C communication abort callback
  5279. * (To be called at end of DMA Abort procedure).
  5280. * @param hdma DMA handle.
  5281. * @retval None
  5282. */
  5283. static void FMPI2C_DMAAbort(DMA_HandleTypeDef *hdma)
  5284. {
  5285. FMPI2C_HandleTypeDef *hfmpi2c = (FMPI2C_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
  5286. /* Reset AbortCpltCallback */
  5287. if (hfmpi2c->hdmatx != NULL)
  5288. {
  5289. hfmpi2c->hdmatx->XferAbortCallback = NULL;
  5290. }
  5291. if (hfmpi2c->hdmarx != NULL)
  5292. {
  5293. hfmpi2c->hdmarx->XferAbortCallback = NULL;
  5294. }
  5295. FMPI2C_TreatErrorCallback(hfmpi2c);
  5296. }
  5297. /**
  5298. * @brief This function handles FMPI2C Communication Timeout.
  5299. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  5300. * the configuration information for the specified FMPI2C.
  5301. * @param Flag Specifies the FMPI2C flag to check.
  5302. * @param Status The new Flag status (SET or RESET).
  5303. * @param Timeout Timeout duration
  5304. * @param Tickstart Tick start value
  5305. * @retval HAL status
  5306. */
  5307. static HAL_StatusTypeDef FMPI2C_WaitOnFlagUntilTimeout(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t Flag, FlagStatus Status,
  5308. uint32_t Timeout, uint32_t Tickstart)
  5309. {
  5310. while (__HAL_FMPI2C_GET_FLAG(hfmpi2c, Flag) == Status)
  5311. {
  5312. /* Check for the Timeout */
  5313. if (Timeout != HAL_MAX_DELAY)
  5314. {
  5315. if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
  5316. {
  5317. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_TIMEOUT;
  5318. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  5319. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  5320. /* Process Unlocked */
  5321. __HAL_UNLOCK(hfmpi2c);
  5322. return HAL_ERROR;
  5323. }
  5324. }
  5325. }
  5326. return HAL_OK;
  5327. }
  5328. /**
  5329. * @brief This function handles FMPI2C Communication Timeout for specific usage of TXIS flag.
  5330. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  5331. * the configuration information for the specified FMPI2C.
  5332. * @param Timeout Timeout duration
  5333. * @param Tickstart Tick start value
  5334. * @retval HAL status
  5335. */
  5336. static HAL_StatusTypeDef FMPI2C_WaitOnTXISFlagUntilTimeout(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t Timeout, uint32_t Tickstart)
  5337. {
  5338. while (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_TXIS) == RESET)
  5339. {
  5340. /* Check if a NACK is detected */
  5341. if (FMPI2C_IsAcknowledgeFailed(hfmpi2c, Timeout, Tickstart) != HAL_OK)
  5342. {
  5343. return HAL_ERROR;
  5344. }
  5345. /* Check for the Timeout */
  5346. if (Timeout != HAL_MAX_DELAY)
  5347. {
  5348. if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
  5349. {
  5350. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_TIMEOUT;
  5351. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  5352. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  5353. /* Process Unlocked */
  5354. __HAL_UNLOCK(hfmpi2c);
  5355. return HAL_ERROR;
  5356. }
  5357. }
  5358. }
  5359. return HAL_OK;
  5360. }
  5361. /**
  5362. * @brief This function handles FMPI2C Communication Timeout for specific usage of STOP flag.
  5363. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  5364. * the configuration information for the specified FMPI2C.
  5365. * @param Timeout Timeout duration
  5366. * @param Tickstart Tick start value
  5367. * @retval HAL status
  5368. */
  5369. static HAL_StatusTypeDef FMPI2C_WaitOnSTOPFlagUntilTimeout(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t Timeout, uint32_t Tickstart)
  5370. {
  5371. while (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF) == RESET)
  5372. {
  5373. /* Check if a NACK is detected */
  5374. if (FMPI2C_IsAcknowledgeFailed(hfmpi2c, Timeout, Tickstart) != HAL_OK)
  5375. {
  5376. return HAL_ERROR;
  5377. }
  5378. /* Check for the Timeout */
  5379. if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
  5380. {
  5381. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_TIMEOUT;
  5382. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  5383. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  5384. /* Process Unlocked */
  5385. __HAL_UNLOCK(hfmpi2c);
  5386. return HAL_ERROR;
  5387. }
  5388. }
  5389. return HAL_OK;
  5390. }
  5391. /**
  5392. * @brief This function handles FMPI2C Communication Timeout for specific usage of RXNE flag.
  5393. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  5394. * the configuration information for the specified FMPI2C.
  5395. * @param Timeout Timeout duration
  5396. * @param Tickstart Tick start value
  5397. * @retval HAL status
  5398. */
  5399. static HAL_StatusTypeDef FMPI2C_WaitOnRXNEFlagUntilTimeout(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t Timeout, uint32_t Tickstart)
  5400. {
  5401. while (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_RXNE) == RESET)
  5402. {
  5403. /* Check if a NACK is detected */
  5404. if (FMPI2C_IsAcknowledgeFailed(hfmpi2c, Timeout, Tickstart) != HAL_OK)
  5405. {
  5406. return HAL_ERROR;
  5407. }
  5408. /* Check if a STOPF is detected */
  5409. if (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF) == SET)
  5410. {
  5411. /* Check if an RXNE is pending */
  5412. /* Store Last receive data if any */
  5413. if ((__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_RXNE) == SET) && (hfmpi2c->XferSize > 0U))
  5414. {
  5415. /* Return HAL_OK */
  5416. /* The Reading of data from RXDR will be done in caller function */
  5417. return HAL_OK;
  5418. }
  5419. else
  5420. {
  5421. /* Clear STOP Flag */
  5422. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  5423. /* Clear Configuration Register 2 */
  5424. FMPI2C_RESET_CR2(hfmpi2c);
  5425. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  5426. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  5427. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  5428. /* Process Unlocked */
  5429. __HAL_UNLOCK(hfmpi2c);
  5430. return HAL_ERROR;
  5431. }
  5432. }
  5433. /* Check for the Timeout */
  5434. if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
  5435. {
  5436. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_TIMEOUT;
  5437. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  5438. /* Process Unlocked */
  5439. __HAL_UNLOCK(hfmpi2c);
  5440. return HAL_ERROR;
  5441. }
  5442. }
  5443. return HAL_OK;
  5444. }
  5445. /**
  5446. * @brief This function handles Acknowledge failed detection during an FMPI2C Communication.
  5447. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  5448. * the configuration information for the specified FMPI2C.
  5449. * @param Timeout Timeout duration
  5450. * @param Tickstart Tick start value
  5451. * @retval HAL status
  5452. */
  5453. static HAL_StatusTypeDef FMPI2C_IsAcknowledgeFailed(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t Timeout, uint32_t Tickstart)
  5454. {
  5455. if (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_AF) == SET)
  5456. {
  5457. /* Wait until STOP Flag is reset */
  5458. /* AutoEnd should be initiate after AF */
  5459. while (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF) == RESET)
  5460. {
  5461. /* Check for the Timeout */
  5462. if (Timeout != HAL_MAX_DELAY)
  5463. {
  5464. if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
  5465. {
  5466. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_TIMEOUT;
  5467. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  5468. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  5469. /* Process Unlocked */
  5470. __HAL_UNLOCK(hfmpi2c);
  5471. return HAL_ERROR;
  5472. }
  5473. }
  5474. }
  5475. /* Clear NACKF Flag */
  5476. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_AF);
  5477. /* Clear STOP Flag */
  5478. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  5479. /* Flush TX register */
  5480. FMPI2C_Flush_TXDR(hfmpi2c);
  5481. /* Clear Configuration Register 2 */
  5482. FMPI2C_RESET_CR2(hfmpi2c);
  5483. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_AF;
  5484. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  5485. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  5486. /* Process Unlocked */
  5487. __HAL_UNLOCK(hfmpi2c);
  5488. return HAL_ERROR;
  5489. }
  5490. return HAL_OK;
  5491. }
  5492. /**
  5493. * @brief Handles FMPI2Cx communication when starting transfer or during transfer (TC or TCR flag are set).
  5494. * @param hfmpi2c FMPI2C handle.
  5495. * @param DevAddress Specifies the slave address to be programmed.
  5496. * @param Size Specifies the number of bytes to be programmed.
  5497. * This parameter must be a value between 0 and 255.
  5498. * @param Mode New state of the FMPI2C START condition generation.
  5499. * This parameter can be one of the following values:
  5500. * @arg @ref FMPI2C_RELOAD_MODE Enable Reload mode .
  5501. * @arg @ref FMPI2C_AUTOEND_MODE Enable Automatic end mode.
  5502. * @arg @ref FMPI2C_SOFTEND_MODE Enable Software end mode.
  5503. * @param Request New state of the FMPI2C START condition generation.
  5504. * This parameter can be one of the following values:
  5505. * @arg @ref FMPI2C_NO_STARTSTOP Don't Generate stop and start condition.
  5506. * @arg @ref FMPI2C_GENERATE_STOP Generate stop condition (Size should be set to 0).
  5507. * @arg @ref FMPI2C_GENERATE_START_READ Generate Restart for read request.
  5508. * @arg @ref FMPI2C_GENERATE_START_WRITE Generate Restart for write request.
  5509. * @retval None
  5510. */
  5511. static void FMPI2C_TransferConfig(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint8_t Size, uint32_t Mode,
  5512. uint32_t Request)
  5513. {
  5514. /* Check the parameters */
  5515. assert_param(IS_FMPI2C_ALL_INSTANCE(hfmpi2c->Instance));
  5516. assert_param(IS_TRANSFER_MODE(Mode));
  5517. assert_param(IS_TRANSFER_REQUEST(Request));
  5518. /* update CR2 register */
  5519. MODIFY_REG(hfmpi2c->Instance->CR2,
  5520. ((FMPI2C_CR2_SADD | FMPI2C_CR2_NBYTES | FMPI2C_CR2_RELOAD | FMPI2C_CR2_AUTOEND | \
  5521. (FMPI2C_CR2_RD_WRN & (uint32_t)(Request >> (31U - FMPI2C_CR2_RD_WRN_Pos))) | FMPI2C_CR2_START | FMPI2C_CR2_STOP)), \
  5522. (uint32_t)(((uint32_t)DevAddress & FMPI2C_CR2_SADD) |
  5523. (((uint32_t)Size << FMPI2C_CR2_NBYTES_Pos) & FMPI2C_CR2_NBYTES) | (uint32_t)Mode | (uint32_t)Request));
  5524. }
  5525. /**
  5526. * @brief Manage the enabling of Interrupts.
  5527. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  5528. * the configuration information for the specified FMPI2C.
  5529. * @param InterruptRequest Value of @ref FMPI2C_Interrupt_configuration_definition.
  5530. * @retval None
  5531. */
  5532. static void FMPI2C_Enable_IRQ(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t InterruptRequest)
  5533. {
  5534. uint32_t tmpisr = 0U;
  5535. if ((hfmpi2c->XferISR == FMPI2C_Master_ISR_DMA) || \
  5536. (hfmpi2c->XferISR == FMPI2C_Slave_ISR_DMA))
  5537. {
  5538. if ((InterruptRequest & FMPI2C_XFER_LISTEN_IT) == FMPI2C_XFER_LISTEN_IT)
  5539. {
  5540. /* Enable ERR, STOP, NACK and ADDR interrupts */
  5541. tmpisr |= FMPI2C_IT_ADDRI | FMPI2C_IT_STOPI | FMPI2C_IT_NACKI | FMPI2C_IT_ERRI;
  5542. }
  5543. if (InterruptRequest == FMPI2C_XFER_ERROR_IT)
  5544. {
  5545. /* Enable ERR and NACK interrupts */
  5546. tmpisr |= FMPI2C_IT_ERRI | FMPI2C_IT_NACKI;
  5547. }
  5548. if (InterruptRequest == FMPI2C_XFER_CPLT_IT)
  5549. {
  5550. /* Enable STOP interrupts */
  5551. tmpisr |= (FMPI2C_IT_STOPI | FMPI2C_IT_TCI);
  5552. }
  5553. if (InterruptRequest == FMPI2C_XFER_RELOAD_IT)
  5554. {
  5555. /* Enable TC interrupts */
  5556. tmpisr |= FMPI2C_IT_TCI;
  5557. }
  5558. }
  5559. else
  5560. {
  5561. if ((InterruptRequest & FMPI2C_XFER_LISTEN_IT) == FMPI2C_XFER_LISTEN_IT)
  5562. {
  5563. /* Enable ERR, STOP, NACK, and ADDR interrupts */
  5564. tmpisr |= FMPI2C_IT_ADDRI | FMPI2C_IT_STOPI | FMPI2C_IT_NACKI | FMPI2C_IT_ERRI;
  5565. }
  5566. if ((InterruptRequest & FMPI2C_XFER_TX_IT) == FMPI2C_XFER_TX_IT)
  5567. {
  5568. /* Enable ERR, TC, STOP, NACK and RXI interrupts */
  5569. tmpisr |= FMPI2C_IT_ERRI | FMPI2C_IT_TCI | FMPI2C_IT_STOPI | FMPI2C_IT_NACKI | FMPI2C_IT_TXI;
  5570. }
  5571. if ((InterruptRequest & FMPI2C_XFER_RX_IT) == FMPI2C_XFER_RX_IT)
  5572. {
  5573. /* Enable ERR, TC, STOP, NACK and TXI interrupts */
  5574. tmpisr |= FMPI2C_IT_ERRI | FMPI2C_IT_TCI | FMPI2C_IT_STOPI | FMPI2C_IT_NACKI | FMPI2C_IT_RXI;
  5575. }
  5576. if (InterruptRequest == FMPI2C_XFER_CPLT_IT)
  5577. {
  5578. /* Enable STOP interrupts */
  5579. tmpisr |= FMPI2C_IT_STOPI;
  5580. }
  5581. }
  5582. /* Enable interrupts only at the end */
  5583. /* to avoid the risk of FMPI2C interrupt handle execution before */
  5584. /* all interrupts requested done */
  5585. __HAL_FMPI2C_ENABLE_IT(hfmpi2c, tmpisr);
  5586. }
  5587. /**
  5588. * @brief Manage the disabling of Interrupts.
  5589. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  5590. * the configuration information for the specified FMPI2C.
  5591. * @param InterruptRequest Value of @ref FMPI2C_Interrupt_configuration_definition.
  5592. * @retval None
  5593. */
  5594. static void FMPI2C_Disable_IRQ(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t InterruptRequest)
  5595. {
  5596. uint32_t tmpisr = 0U;
  5597. if ((InterruptRequest & FMPI2C_XFER_TX_IT) == FMPI2C_XFER_TX_IT)
  5598. {
  5599. /* Disable TC and TXI interrupts */
  5600. tmpisr |= FMPI2C_IT_TCI | FMPI2C_IT_TXI;
  5601. if (((uint32_t)hfmpi2c->State & (uint32_t)HAL_FMPI2C_STATE_LISTEN) != (uint32_t)HAL_FMPI2C_STATE_LISTEN)
  5602. {
  5603. /* Disable NACK and STOP interrupts */
  5604. tmpisr |= FMPI2C_IT_STOPI | FMPI2C_IT_NACKI | FMPI2C_IT_ERRI;
  5605. }
  5606. }
  5607. if ((InterruptRequest & FMPI2C_XFER_RX_IT) == FMPI2C_XFER_RX_IT)
  5608. {
  5609. /* Disable TC and RXI interrupts */
  5610. tmpisr |= FMPI2C_IT_TCI | FMPI2C_IT_RXI;
  5611. if (((uint32_t)hfmpi2c->State & (uint32_t)HAL_FMPI2C_STATE_LISTEN) != (uint32_t)HAL_FMPI2C_STATE_LISTEN)
  5612. {
  5613. /* Disable NACK and STOP interrupts */
  5614. tmpisr |= FMPI2C_IT_STOPI | FMPI2C_IT_NACKI | FMPI2C_IT_ERRI;
  5615. }
  5616. }
  5617. if ((InterruptRequest & FMPI2C_XFER_LISTEN_IT) == FMPI2C_XFER_LISTEN_IT)
  5618. {
  5619. /* Disable ADDR, NACK and STOP interrupts */
  5620. tmpisr |= FMPI2C_IT_ADDRI | FMPI2C_IT_STOPI | FMPI2C_IT_NACKI | FMPI2C_IT_ERRI;
  5621. }
  5622. if (InterruptRequest == FMPI2C_XFER_ERROR_IT)
  5623. {
  5624. /* Enable ERR and NACK interrupts */
  5625. tmpisr |= FMPI2C_IT_ERRI | FMPI2C_IT_NACKI;
  5626. }
  5627. if (InterruptRequest == FMPI2C_XFER_CPLT_IT)
  5628. {
  5629. /* Enable STOP interrupts */
  5630. tmpisr |= FMPI2C_IT_STOPI;
  5631. }
  5632. if (InterruptRequest == FMPI2C_XFER_RELOAD_IT)
  5633. {
  5634. /* Enable TC interrupts */
  5635. tmpisr |= FMPI2C_IT_TCI;
  5636. }
  5637. /* Disable interrupts only at the end */
  5638. /* to avoid a breaking situation like at "t" time */
  5639. /* all disable interrupts request are not done */
  5640. __HAL_FMPI2C_DISABLE_IT(hfmpi2c, tmpisr);
  5641. }
  5642. /**
  5643. * @brief Convert FMPI2Cx OTHER_xxx XferOptions to functional XferOptions.
  5644. * @param hfmpi2c FMPI2C handle.
  5645. * @retval None
  5646. */
  5647. static void FMPI2C_ConvertOtherXferOptions(FMPI2C_HandleTypeDef *hfmpi2c)
  5648. {
  5649. /* if user set XferOptions to FMPI2C_OTHER_FRAME */
  5650. /* it request implicitly to generate a restart condition */
  5651. /* set XferOptions to FMPI2C_FIRST_FRAME */
  5652. if (hfmpi2c->XferOptions == FMPI2C_OTHER_FRAME)
  5653. {
  5654. hfmpi2c->XferOptions = FMPI2C_FIRST_FRAME;
  5655. }
  5656. /* else if user set XferOptions to FMPI2C_OTHER_AND_LAST_FRAME */
  5657. /* it request implicitly to generate a restart condition */
  5658. /* then generate a stop condition at the end of transfer */
  5659. /* set XferOptions to FMPI2C_FIRST_AND_LAST_FRAME */
  5660. else if (hfmpi2c->XferOptions == FMPI2C_OTHER_AND_LAST_FRAME)
  5661. {
  5662. hfmpi2c->XferOptions = FMPI2C_FIRST_AND_LAST_FRAME;
  5663. }
  5664. else
  5665. {
  5666. /* Nothing to do */
  5667. }
  5668. }
  5669. /**
  5670. * @}
  5671. */
  5672. #endif /* FMPI2C_CR1_PE */
  5673. #endif /* HAL_FMPI2C_MODULE_ENABLED */
  5674. /**
  5675. * @}
  5676. */
  5677. /**
  5678. * @}
  5679. */
  5680. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/