stm32l4xx_hal_i2c.c 240 KB

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