stm32f1xx_hal_i2c.c 227 KB

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