stm32f4xx_hal_i2c.c 226 KB

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