stm32f1xx_hal_i2c.c 239 KB

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