stm32f0xx_hal_i2c.c 239 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548
  1. /**
  2. ******************************************************************************
  3. * @file stm32f0xx_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) 2016 STMicroelectronics.
  16. * All rights reserved.
  17. *
  18. * This software is licensed under terms that can be found in the LICENSE file
  19. * in the root directory of this software component.
  20. * If no LICENSE file comes with this software, it is provided AS-IS.
  21. *
  22. ******************************************************************************
  23. @verbatim
  24. ==============================================================================
  25. ##### How to use this driver #####
  26. ==============================================================================
  27. [..]
  28. The I2C HAL driver can be used as follows:
  29. (#) Declare a I2C_HandleTypeDef handle structure, for example:
  30. I2C_HandleTypeDef hi2c;
  31. (#)Initialize the I2C low level resources by implementing the HAL_I2C_MspInit() API:
  32. (##) Enable the I2Cx interface clock
  33. (##) I2C pins configuration
  34. (+++) Enable the clock for the I2C GPIOs
  35. (+++) Configure I2C pins as alternate function open-drain
  36. (##) NVIC configuration if you need to use interrupt process
  37. (+++) Configure the I2Cx interrupt priority
  38. (+++) Enable the NVIC I2C IRQ Channel
  39. (##) DMA Configuration if you need to use DMA process
  40. (+++) Declare a DMA_HandleTypeDef handle structure for
  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 "stm32f0xx_hal.h"
  301. /** @addtogroup STM32F0xx_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. return HAL_ERROR;
  2808. }
  2809. /* Clear STOP Flag */
  2810. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  2811. /* Device is ready */
  2812. hi2c->State = HAL_I2C_STATE_READY;
  2813. /* Process Unlocked */
  2814. __HAL_UNLOCK(hi2c);
  2815. return HAL_OK;
  2816. }
  2817. else
  2818. {
  2819. /* Wait until STOPF flag is reset */
  2820. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_STOPF, RESET, Timeout, tickstart) != HAL_OK)
  2821. {
  2822. return HAL_ERROR;
  2823. }
  2824. /* Clear NACK Flag */
  2825. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  2826. /* Clear STOP Flag, auto generated with autoend*/
  2827. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  2828. }
  2829. /* Increment Trials */
  2830. I2C_Trials++;
  2831. } while (I2C_Trials < Trials);
  2832. /* Update I2C state */
  2833. hi2c->State = HAL_I2C_STATE_READY;
  2834. /* Update I2C error code */
  2835. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  2836. /* Process Unlocked */
  2837. __HAL_UNLOCK(hi2c);
  2838. return HAL_ERROR;
  2839. }
  2840. else
  2841. {
  2842. return HAL_BUSY;
  2843. }
  2844. }
  2845. /**
  2846. * @brief Sequential transmit in master I2C mode an amount of data in non-blocking mode with Interrupt.
  2847. * @note This interface allow to manage repeated start condition when a direction change during transfer
  2848. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2849. * the configuration information for the specified I2C.
  2850. * @param DevAddress Target device address: The device 7 bits address value
  2851. * in datasheet must be shifted to the left before calling the interface
  2852. * @param pData Pointer to data buffer
  2853. * @param Size Amount of data to be sent
  2854. * @param XferOptions Options of Transfer, value of @ref I2C_XFEROPTIONS
  2855. * @retval HAL status
  2856. */
  2857. HAL_StatusTypeDef HAL_I2C_Master_Seq_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData,
  2858. uint16_t Size, uint32_t XferOptions)
  2859. {
  2860. uint32_t xfermode;
  2861. uint32_t xferrequest = I2C_GENERATE_START_WRITE;
  2862. uint32_t sizetoxfer = 0U;
  2863. /* Check the parameters */
  2864. assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  2865. if (hi2c->State == HAL_I2C_STATE_READY)
  2866. {
  2867. /* Process Locked */
  2868. __HAL_LOCK(hi2c);
  2869. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  2870. hi2c->Mode = HAL_I2C_MODE_MASTER;
  2871. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2872. /* Prepare transfer parameters */
  2873. hi2c->pBuffPtr = pData;
  2874. hi2c->XferCount = Size;
  2875. hi2c->XferOptions = XferOptions;
  2876. hi2c->XferISR = I2C_Master_ISR_IT;
  2877. /* If hi2c->XferCount > MAX_NBYTE_SIZE, use reload mode */
  2878. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  2879. {
  2880. hi2c->XferSize = MAX_NBYTE_SIZE;
  2881. xfermode = I2C_RELOAD_MODE;
  2882. }
  2883. else
  2884. {
  2885. hi2c->XferSize = hi2c->XferCount;
  2886. xfermode = hi2c->XferOptions;
  2887. }
  2888. if ((hi2c->XferSize > 0U) && ((XferOptions == I2C_FIRST_FRAME) || \
  2889. (XferOptions == I2C_FIRST_AND_LAST_FRAME)))
  2890. {
  2891. /* Preload TX register */
  2892. /* Write data to TXDR */
  2893. hi2c->Instance->TXDR = *hi2c->pBuffPtr;
  2894. /* Increment Buffer pointer */
  2895. hi2c->pBuffPtr++;
  2896. sizetoxfer = hi2c->XferSize;
  2897. hi2c->XferCount--;
  2898. hi2c->XferSize--;
  2899. }
  2900. /* If transfer direction not change and there is no request to start another frame,
  2901. do not generate Restart Condition */
  2902. /* Mean Previous state is same as current state */
  2903. if ((hi2c->PreviousState == I2C_STATE_MASTER_BUSY_TX) && \
  2904. (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 0))
  2905. {
  2906. xferrequest = I2C_NO_STARTSTOP;
  2907. }
  2908. else
  2909. {
  2910. /* Convert OTHER_xxx XferOptions if any */
  2911. I2C_ConvertOtherXferOptions(hi2c);
  2912. /* Update xfermode accordingly if no reload is necessary */
  2913. if (hi2c->XferCount <= MAX_NBYTE_SIZE)
  2914. {
  2915. xfermode = hi2c->XferOptions;
  2916. }
  2917. }
  2918. /* Send Slave Address and set NBYTES to write */
  2919. if ((XferOptions == I2C_FIRST_FRAME) || (XferOptions == I2C_FIRST_AND_LAST_FRAME))
  2920. {
  2921. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)sizetoxfer, xfermode, xferrequest);
  2922. }
  2923. else
  2924. {
  2925. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, xferrequest);
  2926. }
  2927. /* Process Unlocked */
  2928. __HAL_UNLOCK(hi2c);
  2929. /* Note : The I2C interrupts must be enabled after unlocking current process
  2930. to avoid the risk of I2C interrupt handle execution before current
  2931. process unlock */
  2932. /* Enable ERR, TC, STOP, NACK, TXI interrupt */
  2933. /* possible to enable all of these */
  2934. /* I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI |
  2935. I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
  2936. I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT);
  2937. return HAL_OK;
  2938. }
  2939. else
  2940. {
  2941. return HAL_BUSY;
  2942. }
  2943. }
  2944. /**
  2945. * @brief Sequential transmit in master I2C mode an amount of data in non-blocking mode with DMA.
  2946. * @note This interface allow to manage repeated start condition when a direction change during transfer
  2947. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2948. * the configuration information for the specified I2C.
  2949. * @param DevAddress Target device address: The device 7 bits address value
  2950. * in datasheet must be shifted to the left before calling the interface
  2951. * @param pData Pointer to data buffer
  2952. * @param Size Amount of data to be sent
  2953. * @param XferOptions Options of Transfer, value of @ref I2C_XFEROPTIONS
  2954. * @retval HAL status
  2955. */
  2956. HAL_StatusTypeDef HAL_I2C_Master_Seq_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData,
  2957. uint16_t Size, uint32_t XferOptions)
  2958. {
  2959. uint32_t xfermode;
  2960. uint32_t xferrequest = I2C_GENERATE_START_WRITE;
  2961. HAL_StatusTypeDef dmaxferstatus;
  2962. uint32_t sizetoxfer = 0U;
  2963. /* Check the parameters */
  2964. assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  2965. if (hi2c->State == HAL_I2C_STATE_READY)
  2966. {
  2967. /* Process Locked */
  2968. __HAL_LOCK(hi2c);
  2969. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  2970. hi2c->Mode = HAL_I2C_MODE_MASTER;
  2971. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2972. /* Prepare transfer parameters */
  2973. hi2c->pBuffPtr = pData;
  2974. hi2c->XferCount = Size;
  2975. hi2c->XferOptions = XferOptions;
  2976. hi2c->XferISR = I2C_Master_ISR_DMA;
  2977. /* If hi2c->XferCount > MAX_NBYTE_SIZE, use reload mode */
  2978. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  2979. {
  2980. hi2c->XferSize = MAX_NBYTE_SIZE;
  2981. xfermode = I2C_RELOAD_MODE;
  2982. }
  2983. else
  2984. {
  2985. hi2c->XferSize = hi2c->XferCount;
  2986. xfermode = hi2c->XferOptions;
  2987. }
  2988. if ((hi2c->XferSize > 0U) && ((XferOptions == I2C_FIRST_FRAME) || \
  2989. (XferOptions == I2C_FIRST_AND_LAST_FRAME)))
  2990. {
  2991. /* Preload TX register */
  2992. /* Write data to TXDR */
  2993. hi2c->Instance->TXDR = *hi2c->pBuffPtr;
  2994. /* Increment Buffer pointer */
  2995. hi2c->pBuffPtr++;
  2996. sizetoxfer = hi2c->XferSize;
  2997. hi2c->XferCount--;
  2998. hi2c->XferSize--;
  2999. }
  3000. /* If transfer direction not change and there is no request to start another frame,
  3001. do not generate Restart Condition */
  3002. /* Mean Previous state is same as current state */
  3003. if ((hi2c->PreviousState == I2C_STATE_MASTER_BUSY_TX) && \
  3004. (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 0))
  3005. {
  3006. xferrequest = I2C_NO_STARTSTOP;
  3007. }
  3008. else
  3009. {
  3010. /* Convert OTHER_xxx XferOptions if any */
  3011. I2C_ConvertOtherXferOptions(hi2c);
  3012. /* Update xfermode accordingly if no reload is necessary */
  3013. if (hi2c->XferCount <= MAX_NBYTE_SIZE)
  3014. {
  3015. xfermode = hi2c->XferOptions;
  3016. }
  3017. }
  3018. if (hi2c->XferSize > 0U)
  3019. {
  3020. if (hi2c->hdmatx != NULL)
  3021. {
  3022. /* Set the I2C DMA transfer complete callback */
  3023. hi2c->hdmatx->XferCpltCallback = I2C_DMAMasterTransmitCplt;
  3024. /* Set the DMA error callback */
  3025. hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
  3026. /* Set the unused DMA callbacks to NULL */
  3027. hi2c->hdmatx->XferHalfCpltCallback = NULL;
  3028. hi2c->hdmatx->XferAbortCallback = NULL;
  3029. /* Enable the DMA channel */
  3030. dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr,
  3031. (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);
  3032. }
  3033. else
  3034. {
  3035. /* Update I2C state */
  3036. hi2c->State = HAL_I2C_STATE_READY;
  3037. hi2c->Mode = HAL_I2C_MODE_NONE;
  3038. /* Update I2C error code */
  3039. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
  3040. /* Process Unlocked */
  3041. __HAL_UNLOCK(hi2c);
  3042. return HAL_ERROR;
  3043. }
  3044. if (dmaxferstatus == HAL_OK)
  3045. {
  3046. /* Send Slave Address and set NBYTES to write */
  3047. if ((XferOptions == I2C_FIRST_FRAME) || (XferOptions == I2C_FIRST_AND_LAST_FRAME))
  3048. {
  3049. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)sizetoxfer, xfermode, xferrequest);
  3050. }
  3051. else
  3052. {
  3053. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, xferrequest);
  3054. }
  3055. /* Update XferCount value */
  3056. hi2c->XferCount -= hi2c->XferSize;
  3057. /* Process Unlocked */
  3058. __HAL_UNLOCK(hi2c);
  3059. /* Note : The I2C interrupts must be enabled after unlocking current process
  3060. to avoid the risk of I2C interrupt handle execution before current
  3061. process unlock */
  3062. /* Enable ERR and NACK interrupts */
  3063. I2C_Enable_IRQ(hi2c, I2C_XFER_ERROR_IT);
  3064. /* Enable DMA Request */
  3065. hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
  3066. }
  3067. else
  3068. {
  3069. /* Update I2C state */
  3070. hi2c->State = HAL_I2C_STATE_READY;
  3071. hi2c->Mode = HAL_I2C_MODE_NONE;
  3072. /* Update I2C error code */
  3073. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  3074. /* Process Unlocked */
  3075. __HAL_UNLOCK(hi2c);
  3076. return HAL_ERROR;
  3077. }
  3078. }
  3079. else
  3080. {
  3081. /* Update Transfer ISR function pointer */
  3082. hi2c->XferISR = I2C_Master_ISR_IT;
  3083. /* Send Slave Address */
  3084. /* Set NBYTES to write and generate START condition */
  3085. if ((XferOptions == I2C_FIRST_FRAME) || (XferOptions == I2C_FIRST_AND_LAST_FRAME))
  3086. {
  3087. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)sizetoxfer, xfermode, xferrequest);
  3088. }
  3089. else
  3090. {
  3091. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, xferrequest);
  3092. }
  3093. /* Process Unlocked */
  3094. __HAL_UNLOCK(hi2c);
  3095. /* Note : The I2C interrupts must be enabled after unlocking current process
  3096. to avoid the risk of I2C interrupt handle execution before current
  3097. process unlock */
  3098. /* Enable ERR, TC, STOP, NACK, TXI interrupt */
  3099. /* possible to enable all of these */
  3100. /* I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI |
  3101. I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
  3102. I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT);
  3103. }
  3104. return HAL_OK;
  3105. }
  3106. else
  3107. {
  3108. return HAL_BUSY;
  3109. }
  3110. }
  3111. /**
  3112. * @brief Sequential receive in master I2C mode an amount of data in non-blocking mode with Interrupt
  3113. * @note This interface allow to manage repeated start condition when a direction change during transfer
  3114. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3115. * the configuration information for the specified I2C.
  3116. * @param DevAddress Target device address: The device 7 bits address value
  3117. * in datasheet must be shifted to the left before calling the interface
  3118. * @param pData Pointer to data buffer
  3119. * @param Size Amount of data to be sent
  3120. * @param XferOptions Options of Transfer, value of @ref I2C_XFEROPTIONS
  3121. * @retval HAL status
  3122. */
  3123. HAL_StatusTypeDef HAL_I2C_Master_Seq_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData,
  3124. uint16_t Size, uint32_t XferOptions)
  3125. {
  3126. uint32_t xfermode;
  3127. uint32_t xferrequest = I2C_GENERATE_START_READ;
  3128. /* Check the parameters */
  3129. assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  3130. if (hi2c->State == HAL_I2C_STATE_READY)
  3131. {
  3132. /* Process Locked */
  3133. __HAL_LOCK(hi2c);
  3134. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  3135. hi2c->Mode = HAL_I2C_MODE_MASTER;
  3136. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  3137. /* Prepare transfer parameters */
  3138. hi2c->pBuffPtr = pData;
  3139. hi2c->XferCount = Size;
  3140. hi2c->XferOptions = XferOptions;
  3141. hi2c->XferISR = I2C_Master_ISR_IT;
  3142. /* If hi2c->XferCount > MAX_NBYTE_SIZE, use reload mode */
  3143. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  3144. {
  3145. hi2c->XferSize = MAX_NBYTE_SIZE;
  3146. xfermode = I2C_RELOAD_MODE;
  3147. }
  3148. else
  3149. {
  3150. hi2c->XferSize = hi2c->XferCount;
  3151. xfermode = hi2c->XferOptions;
  3152. }
  3153. /* If transfer direction not change and there is no request to start another frame,
  3154. do not generate Restart Condition */
  3155. /* Mean Previous state is same as current state */
  3156. if ((hi2c->PreviousState == I2C_STATE_MASTER_BUSY_RX) && \
  3157. (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 0))
  3158. {
  3159. xferrequest = I2C_NO_STARTSTOP;
  3160. }
  3161. else
  3162. {
  3163. /* Convert OTHER_xxx XferOptions if any */
  3164. I2C_ConvertOtherXferOptions(hi2c);
  3165. /* Update xfermode accordingly if no reload is necessary */
  3166. if (hi2c->XferCount <= MAX_NBYTE_SIZE)
  3167. {
  3168. xfermode = hi2c->XferOptions;
  3169. }
  3170. }
  3171. /* Send Slave Address and set NBYTES to read */
  3172. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, xferrequest);
  3173. /* Process Unlocked */
  3174. __HAL_UNLOCK(hi2c);
  3175. /* Note : The I2C interrupts must be enabled after unlocking current process
  3176. to avoid the risk of I2C interrupt handle execution before current
  3177. process unlock */
  3178. I2C_Enable_IRQ(hi2c, I2C_XFER_RX_IT);
  3179. return HAL_OK;
  3180. }
  3181. else
  3182. {
  3183. return HAL_BUSY;
  3184. }
  3185. }
  3186. /**
  3187. * @brief Sequential receive in master I2C mode an amount of data in non-blocking mode with DMA
  3188. * @note This interface allow to manage repeated start condition when a direction change during transfer
  3189. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3190. * the configuration information for the specified I2C.
  3191. * @param DevAddress Target device address: The device 7 bits address value
  3192. * in datasheet must be shifted to the left before calling the interface
  3193. * @param pData Pointer to data buffer
  3194. * @param Size Amount of data to be sent
  3195. * @param XferOptions Options of Transfer, value of @ref I2C_XFEROPTIONS
  3196. * @retval HAL status
  3197. */
  3198. HAL_StatusTypeDef HAL_I2C_Master_Seq_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData,
  3199. uint16_t Size, uint32_t XferOptions)
  3200. {
  3201. uint32_t xfermode;
  3202. uint32_t xferrequest = I2C_GENERATE_START_READ;
  3203. HAL_StatusTypeDef dmaxferstatus;
  3204. /* Check the parameters */
  3205. assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  3206. if (hi2c->State == HAL_I2C_STATE_READY)
  3207. {
  3208. /* Process Locked */
  3209. __HAL_LOCK(hi2c);
  3210. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  3211. hi2c->Mode = HAL_I2C_MODE_MASTER;
  3212. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  3213. /* Prepare transfer parameters */
  3214. hi2c->pBuffPtr = pData;
  3215. hi2c->XferCount = Size;
  3216. hi2c->XferOptions = XferOptions;
  3217. hi2c->XferISR = I2C_Master_ISR_DMA;
  3218. /* If hi2c->XferCount > MAX_NBYTE_SIZE, use reload mode */
  3219. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  3220. {
  3221. hi2c->XferSize = MAX_NBYTE_SIZE;
  3222. xfermode = I2C_RELOAD_MODE;
  3223. }
  3224. else
  3225. {
  3226. hi2c->XferSize = hi2c->XferCount;
  3227. xfermode = hi2c->XferOptions;
  3228. }
  3229. /* If transfer direction not change and there is no request to start another frame,
  3230. do not generate Restart Condition */
  3231. /* Mean Previous state is same as current state */
  3232. if ((hi2c->PreviousState == I2C_STATE_MASTER_BUSY_RX) && \
  3233. (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 0))
  3234. {
  3235. xferrequest = I2C_NO_STARTSTOP;
  3236. }
  3237. else
  3238. {
  3239. /* Convert OTHER_xxx XferOptions if any */
  3240. I2C_ConvertOtherXferOptions(hi2c);
  3241. /* Update xfermode accordingly if no reload is necessary */
  3242. if (hi2c->XferCount <= MAX_NBYTE_SIZE)
  3243. {
  3244. xfermode = hi2c->XferOptions;
  3245. }
  3246. }
  3247. if (hi2c->XferSize > 0U)
  3248. {
  3249. if (hi2c->hdmarx != NULL)
  3250. {
  3251. /* Set the I2C DMA transfer complete callback */
  3252. hi2c->hdmarx->XferCpltCallback = I2C_DMAMasterReceiveCplt;
  3253. /* Set the DMA error callback */
  3254. hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
  3255. /* Set the unused DMA callbacks to NULL */
  3256. hi2c->hdmarx->XferHalfCpltCallback = NULL;
  3257. hi2c->hdmarx->XferAbortCallback = NULL;
  3258. /* Enable the DMA channel */
  3259. dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)pData,
  3260. hi2c->XferSize);
  3261. }
  3262. else
  3263. {
  3264. /* Update I2C state */
  3265. hi2c->State = HAL_I2C_STATE_READY;
  3266. hi2c->Mode = HAL_I2C_MODE_NONE;
  3267. /* Update I2C error code */
  3268. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
  3269. /* Process Unlocked */
  3270. __HAL_UNLOCK(hi2c);
  3271. return HAL_ERROR;
  3272. }
  3273. if (dmaxferstatus == HAL_OK)
  3274. {
  3275. /* Send Slave Address and set NBYTES to read */
  3276. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, xferrequest);
  3277. /* Update XferCount value */
  3278. hi2c->XferCount -= hi2c->XferSize;
  3279. /* Process Unlocked */
  3280. __HAL_UNLOCK(hi2c);
  3281. /* Note : The I2C interrupts must be enabled after unlocking current process
  3282. to avoid the risk of I2C interrupt handle execution before current
  3283. process unlock */
  3284. /* Enable ERR and NACK interrupts */
  3285. I2C_Enable_IRQ(hi2c, I2C_XFER_ERROR_IT);
  3286. /* Enable DMA Request */
  3287. hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
  3288. }
  3289. else
  3290. {
  3291. /* Update I2C state */
  3292. hi2c->State = HAL_I2C_STATE_READY;
  3293. hi2c->Mode = HAL_I2C_MODE_NONE;
  3294. /* Update I2C error code */
  3295. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  3296. /* Process Unlocked */
  3297. __HAL_UNLOCK(hi2c);
  3298. return HAL_ERROR;
  3299. }
  3300. }
  3301. else
  3302. {
  3303. /* Update Transfer ISR function pointer */
  3304. hi2c->XferISR = I2C_Master_ISR_IT;
  3305. /* Send Slave Address */
  3306. /* Set NBYTES to read and generate START condition */
  3307. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE,
  3308. I2C_GENERATE_START_READ);
  3309. /* Process Unlocked */
  3310. __HAL_UNLOCK(hi2c);
  3311. /* Note : The I2C interrupts must be enabled after unlocking current process
  3312. to avoid the risk of I2C interrupt handle execution before current
  3313. process unlock */
  3314. /* Enable ERR, TC, STOP, NACK, RXI interrupt */
  3315. /* possible to enable all of these */
  3316. /* I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI |
  3317. I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
  3318. I2C_Enable_IRQ(hi2c, I2C_XFER_RX_IT);
  3319. }
  3320. return HAL_OK;
  3321. }
  3322. else
  3323. {
  3324. return HAL_BUSY;
  3325. }
  3326. }
  3327. /**
  3328. * @brief Sequential transmit in slave/device I2C mode an amount of data in non-blocking mode with Interrupt
  3329. * @note This interface allow to manage repeated start condition when a direction change during transfer
  3330. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3331. * the configuration information for the specified I2C.
  3332. * @param pData Pointer to data buffer
  3333. * @param Size Amount of data to be sent
  3334. * @param XferOptions Options of Transfer, value of @ref I2C_XFEROPTIONS
  3335. * @retval HAL status
  3336. */
  3337. HAL_StatusTypeDef HAL_I2C_Slave_Seq_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size,
  3338. uint32_t XferOptions)
  3339. {
  3340. /* Declaration of tmp to prevent undefined behavior of volatile usage */
  3341. FlagStatus tmp;
  3342. /* Check the parameters */
  3343. assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  3344. if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
  3345. {
  3346. if ((pData == NULL) || (Size == 0U))
  3347. {
  3348. hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM;
  3349. return HAL_ERROR;
  3350. }
  3351. /* Disable Interrupts, to prevent preemption during treatment in case of multicall */
  3352. I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_TX_IT);
  3353. /* Process Locked */
  3354. __HAL_LOCK(hi2c);
  3355. /* I2C cannot manage full duplex exchange so disable previous IT enabled if any */
  3356. /* and then toggle the HAL slave RX state to TX state */
  3357. if (hi2c->State == HAL_I2C_STATE_BUSY_RX_LISTEN)
  3358. {
  3359. /* Disable associated Interrupts */
  3360. I2C_Disable_IRQ(hi2c, I2C_XFER_RX_IT);
  3361. /* Abort DMA Xfer if any */
  3362. if ((hi2c->Instance->CR1 & I2C_CR1_RXDMAEN) == I2C_CR1_RXDMAEN)
  3363. {
  3364. hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
  3365. if (hi2c->hdmarx != NULL)
  3366. {
  3367. /* Set the I2C DMA Abort callback :
  3368. will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
  3369. hi2c->hdmarx->XferAbortCallback = I2C_DMAAbort;
  3370. /* Abort DMA RX */
  3371. if (HAL_DMA_Abort_IT(hi2c->hdmarx) != HAL_OK)
  3372. {
  3373. /* Call Directly XferAbortCallback function in case of error */
  3374. hi2c->hdmarx->XferAbortCallback(hi2c->hdmarx);
  3375. }
  3376. }
  3377. }
  3378. }
  3379. hi2c->State = HAL_I2C_STATE_BUSY_TX_LISTEN;
  3380. hi2c->Mode = HAL_I2C_MODE_SLAVE;
  3381. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  3382. /* Enable Address Acknowledge */
  3383. hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
  3384. /* Prepare transfer parameters */
  3385. hi2c->pBuffPtr = pData;
  3386. hi2c->XferCount = Size;
  3387. hi2c->XferSize = hi2c->XferCount;
  3388. hi2c->XferOptions = XferOptions;
  3389. hi2c->XferISR = I2C_Slave_ISR_IT;
  3390. tmp = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR);
  3391. if ((I2C_GET_DIR(hi2c) == I2C_DIRECTION_RECEIVE) && (tmp != RESET))
  3392. {
  3393. /* Clear ADDR flag after prepare the transfer parameters */
  3394. /* This action will generate an acknowledge to the Master */
  3395. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
  3396. }
  3397. /* Process Unlocked */
  3398. __HAL_UNLOCK(hi2c);
  3399. /* Note : The I2C interrupts must be enabled after unlocking current process
  3400. to avoid the risk of I2C interrupt handle execution before current
  3401. process unlock */
  3402. /* REnable ADDR interrupt */
  3403. I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT | I2C_XFER_LISTEN_IT);
  3404. return HAL_OK;
  3405. }
  3406. else
  3407. {
  3408. return HAL_ERROR;
  3409. }
  3410. }
  3411. /**
  3412. * @brief Sequential transmit in slave/device I2C mode an amount of data in non-blocking mode with DMA
  3413. * @note This interface allow to manage repeated start condition when a direction change during transfer
  3414. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3415. * the configuration information for the specified I2C.
  3416. * @param pData Pointer to data buffer
  3417. * @param Size Amount of data to be sent
  3418. * @param XferOptions Options of Transfer, value of @ref I2C_XFEROPTIONS
  3419. * @retval HAL status
  3420. */
  3421. HAL_StatusTypeDef HAL_I2C_Slave_Seq_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size,
  3422. uint32_t XferOptions)
  3423. {
  3424. /* Declaration of tmp to prevent undefined behavior of volatile usage */
  3425. FlagStatus tmp;
  3426. HAL_StatusTypeDef dmaxferstatus;
  3427. /* Check the parameters */
  3428. assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  3429. if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
  3430. {
  3431. if ((pData == NULL) || (Size == 0U))
  3432. {
  3433. hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM;
  3434. return HAL_ERROR;
  3435. }
  3436. /* Process Locked */
  3437. __HAL_LOCK(hi2c);
  3438. /* Disable Interrupts, to prevent preemption during treatment in case of multicall */
  3439. I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_TX_IT);
  3440. /* I2C cannot manage full duplex exchange so disable previous IT enabled if any */
  3441. /* and then toggle the HAL slave RX state to TX state */
  3442. if (hi2c->State == HAL_I2C_STATE_BUSY_RX_LISTEN)
  3443. {
  3444. /* Disable associated Interrupts */
  3445. I2C_Disable_IRQ(hi2c, I2C_XFER_RX_IT);
  3446. if ((hi2c->Instance->CR1 & I2C_CR1_RXDMAEN) == I2C_CR1_RXDMAEN)
  3447. {
  3448. /* Abort DMA Xfer if any */
  3449. if (hi2c->hdmarx != NULL)
  3450. {
  3451. hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
  3452. /* Set the I2C DMA Abort callback :
  3453. will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
  3454. hi2c->hdmarx->XferAbortCallback = I2C_DMAAbort;
  3455. /* Abort DMA RX */
  3456. if (HAL_DMA_Abort_IT(hi2c->hdmarx) != HAL_OK)
  3457. {
  3458. /* Call Directly XferAbortCallback function in case of error */
  3459. hi2c->hdmarx->XferAbortCallback(hi2c->hdmarx);
  3460. }
  3461. }
  3462. }
  3463. }
  3464. else if (hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN)
  3465. {
  3466. if ((hi2c->Instance->CR1 & I2C_CR1_TXDMAEN) == I2C_CR1_TXDMAEN)
  3467. {
  3468. hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
  3469. /* Abort DMA Xfer if any */
  3470. if (hi2c->hdmatx != NULL)
  3471. {
  3472. /* Set the I2C DMA Abort callback :
  3473. will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
  3474. hi2c->hdmatx->XferAbortCallback = I2C_DMAAbort;
  3475. /* Abort DMA TX */
  3476. if (HAL_DMA_Abort_IT(hi2c->hdmatx) != HAL_OK)
  3477. {
  3478. /* Call Directly XferAbortCallback function in case of error */
  3479. hi2c->hdmatx->XferAbortCallback(hi2c->hdmatx);
  3480. }
  3481. }
  3482. }
  3483. }
  3484. else
  3485. {
  3486. /* Nothing to do */
  3487. }
  3488. hi2c->State = HAL_I2C_STATE_BUSY_TX_LISTEN;
  3489. hi2c->Mode = HAL_I2C_MODE_SLAVE;
  3490. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  3491. /* Enable Address Acknowledge */
  3492. hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
  3493. /* Prepare transfer parameters */
  3494. hi2c->pBuffPtr = pData;
  3495. hi2c->XferCount = Size;
  3496. hi2c->XferSize = hi2c->XferCount;
  3497. hi2c->XferOptions = XferOptions;
  3498. hi2c->XferISR = I2C_Slave_ISR_DMA;
  3499. if (hi2c->hdmatx != NULL)
  3500. {
  3501. /* Set the I2C DMA transfer complete callback */
  3502. hi2c->hdmatx->XferCpltCallback = I2C_DMASlaveTransmitCplt;
  3503. /* Set the DMA error callback */
  3504. hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
  3505. /* Set the unused DMA callbacks to NULL */
  3506. hi2c->hdmatx->XferHalfCpltCallback = NULL;
  3507. hi2c->hdmatx->XferAbortCallback = NULL;
  3508. /* Enable the DMA channel */
  3509. dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->TXDR,
  3510. hi2c->XferSize);
  3511. }
  3512. else
  3513. {
  3514. /* Update I2C state */
  3515. hi2c->State = HAL_I2C_STATE_LISTEN;
  3516. hi2c->Mode = HAL_I2C_MODE_NONE;
  3517. /* Update I2C error code */
  3518. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
  3519. /* Process Unlocked */
  3520. __HAL_UNLOCK(hi2c);
  3521. return HAL_ERROR;
  3522. }
  3523. if (dmaxferstatus == HAL_OK)
  3524. {
  3525. /* Update XferCount value */
  3526. hi2c->XferCount -= hi2c->XferSize;
  3527. /* Reset XferSize */
  3528. hi2c->XferSize = 0;
  3529. }
  3530. else
  3531. {
  3532. /* Update I2C state */
  3533. hi2c->State = HAL_I2C_STATE_LISTEN;
  3534. hi2c->Mode = HAL_I2C_MODE_NONE;
  3535. /* Update I2C error code */
  3536. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  3537. /* Process Unlocked */
  3538. __HAL_UNLOCK(hi2c);
  3539. return HAL_ERROR;
  3540. }
  3541. tmp = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR);
  3542. if ((I2C_GET_DIR(hi2c) == I2C_DIRECTION_RECEIVE) && (tmp != RESET))
  3543. {
  3544. /* Clear ADDR flag after prepare the transfer parameters */
  3545. /* This action will generate an acknowledge to the Master */
  3546. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
  3547. }
  3548. /* Process Unlocked */
  3549. __HAL_UNLOCK(hi2c);
  3550. /* Enable DMA Request */
  3551. hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
  3552. /* Note : The I2C interrupts must be enabled after unlocking current process
  3553. to avoid the risk of I2C interrupt handle execution before current
  3554. process unlock */
  3555. /* Enable ERR, STOP, NACK, ADDR interrupts */
  3556. I2C_Enable_IRQ(hi2c, I2C_XFER_LISTEN_IT);
  3557. return HAL_OK;
  3558. }
  3559. else
  3560. {
  3561. return HAL_ERROR;
  3562. }
  3563. }
  3564. /**
  3565. * @brief Sequential receive in slave/device I2C mode an amount of data in non-blocking mode with Interrupt
  3566. * @note This interface allow to manage repeated start condition when a direction change during transfer
  3567. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3568. * the configuration information for the specified I2C.
  3569. * @param pData Pointer to data buffer
  3570. * @param Size Amount of data to be sent
  3571. * @param XferOptions Options of Transfer, value of @ref I2C_XFEROPTIONS
  3572. * @retval HAL status
  3573. */
  3574. HAL_StatusTypeDef HAL_I2C_Slave_Seq_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size,
  3575. uint32_t XferOptions)
  3576. {
  3577. /* Declaration of tmp to prevent undefined behavior of volatile usage */
  3578. FlagStatus tmp;
  3579. /* Check the parameters */
  3580. assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  3581. if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
  3582. {
  3583. if ((pData == NULL) || (Size == 0U))
  3584. {
  3585. hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM;
  3586. return HAL_ERROR;
  3587. }
  3588. /* Disable Interrupts, to prevent preemption during treatment in case of multicall */
  3589. I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_RX_IT);
  3590. /* Process Locked */
  3591. __HAL_LOCK(hi2c);
  3592. /* I2C cannot manage full duplex exchange so disable previous IT enabled if any */
  3593. /* and then toggle the HAL slave TX state to RX state */
  3594. if (hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN)
  3595. {
  3596. /* Disable associated Interrupts */
  3597. I2C_Disable_IRQ(hi2c, I2C_XFER_TX_IT);
  3598. if ((hi2c->Instance->CR1 & I2C_CR1_TXDMAEN) == I2C_CR1_TXDMAEN)
  3599. {
  3600. hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
  3601. /* Abort DMA Xfer if any */
  3602. if (hi2c->hdmatx != NULL)
  3603. {
  3604. /* Set the I2C DMA Abort callback :
  3605. will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
  3606. hi2c->hdmatx->XferAbortCallback = I2C_DMAAbort;
  3607. /* Abort DMA TX */
  3608. if (HAL_DMA_Abort_IT(hi2c->hdmatx) != HAL_OK)
  3609. {
  3610. /* Call Directly XferAbortCallback function in case of error */
  3611. hi2c->hdmatx->XferAbortCallback(hi2c->hdmatx);
  3612. }
  3613. }
  3614. }
  3615. }
  3616. hi2c->State = HAL_I2C_STATE_BUSY_RX_LISTEN;
  3617. hi2c->Mode = HAL_I2C_MODE_SLAVE;
  3618. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  3619. /* Enable Address Acknowledge */
  3620. hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
  3621. /* Prepare transfer parameters */
  3622. hi2c->pBuffPtr = pData;
  3623. hi2c->XferCount = Size;
  3624. hi2c->XferSize = hi2c->XferCount;
  3625. hi2c->XferOptions = XferOptions;
  3626. hi2c->XferISR = I2C_Slave_ISR_IT;
  3627. tmp = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR);
  3628. if ((I2C_GET_DIR(hi2c) == I2C_DIRECTION_TRANSMIT) && (tmp != RESET))
  3629. {
  3630. /* Clear ADDR flag after prepare the transfer parameters */
  3631. /* This action will generate an acknowledge to the Master */
  3632. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
  3633. }
  3634. /* Process Unlocked */
  3635. __HAL_UNLOCK(hi2c);
  3636. /* Note : The I2C interrupts must be enabled after unlocking current process
  3637. to avoid the risk of I2C interrupt handle execution before current
  3638. process unlock */
  3639. /* REnable ADDR interrupt */
  3640. I2C_Enable_IRQ(hi2c, I2C_XFER_RX_IT | I2C_XFER_LISTEN_IT);
  3641. return HAL_OK;
  3642. }
  3643. else
  3644. {
  3645. return HAL_ERROR;
  3646. }
  3647. }
  3648. /**
  3649. * @brief Sequential receive in slave/device I2C mode an amount of data in non-blocking mode with DMA
  3650. * @note This interface allow to manage repeated start condition when a direction change during transfer
  3651. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3652. * the configuration information for the specified I2C.
  3653. * @param pData Pointer to data buffer
  3654. * @param Size Amount of data to be sent
  3655. * @param XferOptions Options of Transfer, value of @ref I2C_XFEROPTIONS
  3656. * @retval HAL status
  3657. */
  3658. HAL_StatusTypeDef HAL_I2C_Slave_Seq_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size,
  3659. uint32_t XferOptions)
  3660. {
  3661. /* Declaration of tmp to prevent undefined behavior of volatile usage */
  3662. FlagStatus tmp;
  3663. HAL_StatusTypeDef dmaxferstatus;
  3664. /* Check the parameters */
  3665. assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  3666. if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
  3667. {
  3668. if ((pData == NULL) || (Size == 0U))
  3669. {
  3670. hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM;
  3671. return HAL_ERROR;
  3672. }
  3673. /* Disable Interrupts, to prevent preemption during treatment in case of multicall */
  3674. I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_RX_IT);
  3675. /* Process Locked */
  3676. __HAL_LOCK(hi2c);
  3677. /* I2C cannot manage full duplex exchange so disable previous IT enabled if any */
  3678. /* and then toggle the HAL slave TX state to RX state */
  3679. if (hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN)
  3680. {
  3681. /* Disable associated Interrupts */
  3682. I2C_Disable_IRQ(hi2c, I2C_XFER_TX_IT);
  3683. if ((hi2c->Instance->CR1 & I2C_CR1_TXDMAEN) == I2C_CR1_TXDMAEN)
  3684. {
  3685. /* Abort DMA Xfer if any */
  3686. if (hi2c->hdmatx != NULL)
  3687. {
  3688. hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
  3689. /* Set the I2C DMA Abort callback :
  3690. will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
  3691. hi2c->hdmatx->XferAbortCallback = I2C_DMAAbort;
  3692. /* Abort DMA TX */
  3693. if (HAL_DMA_Abort_IT(hi2c->hdmatx) != HAL_OK)
  3694. {
  3695. /* Call Directly XferAbortCallback function in case of error */
  3696. hi2c->hdmatx->XferAbortCallback(hi2c->hdmatx);
  3697. }
  3698. }
  3699. }
  3700. }
  3701. else if (hi2c->State == HAL_I2C_STATE_BUSY_RX_LISTEN)
  3702. {
  3703. if ((hi2c->Instance->CR1 & I2C_CR1_RXDMAEN) == I2C_CR1_RXDMAEN)
  3704. {
  3705. hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
  3706. /* Abort DMA Xfer if any */
  3707. if (hi2c->hdmarx != NULL)
  3708. {
  3709. /* Set the I2C DMA Abort callback :
  3710. will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
  3711. hi2c->hdmarx->XferAbortCallback = I2C_DMAAbort;
  3712. /* Abort DMA RX */
  3713. if (HAL_DMA_Abort_IT(hi2c->hdmarx) != HAL_OK)
  3714. {
  3715. /* Call Directly XferAbortCallback function in case of error */
  3716. hi2c->hdmarx->XferAbortCallback(hi2c->hdmarx);
  3717. }
  3718. }
  3719. }
  3720. }
  3721. else
  3722. {
  3723. /* Nothing to do */
  3724. }
  3725. hi2c->State = HAL_I2C_STATE_BUSY_RX_LISTEN;
  3726. hi2c->Mode = HAL_I2C_MODE_SLAVE;
  3727. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  3728. /* Enable Address Acknowledge */
  3729. hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
  3730. /* Prepare transfer parameters */
  3731. hi2c->pBuffPtr = pData;
  3732. hi2c->XferCount = Size;
  3733. hi2c->XferSize = hi2c->XferCount;
  3734. hi2c->XferOptions = XferOptions;
  3735. hi2c->XferISR = I2C_Slave_ISR_DMA;
  3736. if (hi2c->hdmarx != NULL)
  3737. {
  3738. /* Set the I2C DMA transfer complete callback */
  3739. hi2c->hdmarx->XferCpltCallback = I2C_DMASlaveReceiveCplt;
  3740. /* Set the DMA error callback */
  3741. hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
  3742. /* Set the unused DMA callbacks to NULL */
  3743. hi2c->hdmarx->XferHalfCpltCallback = NULL;
  3744. hi2c->hdmarx->XferAbortCallback = NULL;
  3745. /* Enable the DMA channel */
  3746. dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR,
  3747. (uint32_t)pData, hi2c->XferSize);
  3748. }
  3749. else
  3750. {
  3751. /* Update I2C state */
  3752. hi2c->State = HAL_I2C_STATE_LISTEN;
  3753. hi2c->Mode = HAL_I2C_MODE_NONE;
  3754. /* Update I2C error code */
  3755. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
  3756. /* Process Unlocked */
  3757. __HAL_UNLOCK(hi2c);
  3758. return HAL_ERROR;
  3759. }
  3760. if (dmaxferstatus == HAL_OK)
  3761. {
  3762. /* Update XferCount value */
  3763. hi2c->XferCount -= hi2c->XferSize;
  3764. /* Reset XferSize */
  3765. hi2c->XferSize = 0;
  3766. }
  3767. else
  3768. {
  3769. /* Update I2C state */
  3770. hi2c->State = HAL_I2C_STATE_LISTEN;
  3771. hi2c->Mode = HAL_I2C_MODE_NONE;
  3772. /* Update I2C error code */
  3773. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  3774. /* Process Unlocked */
  3775. __HAL_UNLOCK(hi2c);
  3776. return HAL_ERROR;
  3777. }
  3778. tmp = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR);
  3779. if ((I2C_GET_DIR(hi2c) == I2C_DIRECTION_TRANSMIT) && (tmp != RESET))
  3780. {
  3781. /* Clear ADDR flag after prepare the transfer parameters */
  3782. /* This action will generate an acknowledge to the Master */
  3783. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
  3784. }
  3785. /* Process Unlocked */
  3786. __HAL_UNLOCK(hi2c);
  3787. /* Enable DMA Request */
  3788. hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
  3789. /* Note : The I2C interrupts must be enabled after unlocking current process
  3790. to avoid the risk of I2C interrupt handle execution before current
  3791. process unlock */
  3792. /* REnable ADDR interrupt */
  3793. I2C_Enable_IRQ(hi2c, I2C_XFER_RX_IT | I2C_XFER_LISTEN_IT);
  3794. return HAL_OK;
  3795. }
  3796. else
  3797. {
  3798. return HAL_ERROR;
  3799. }
  3800. }
  3801. /**
  3802. * @brief Enable the Address listen mode with Interrupt.
  3803. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3804. * the configuration information for the specified I2C.
  3805. * @retval HAL status
  3806. */
  3807. HAL_StatusTypeDef HAL_I2C_EnableListen_IT(I2C_HandleTypeDef *hi2c)
  3808. {
  3809. if (hi2c->State == HAL_I2C_STATE_READY)
  3810. {
  3811. hi2c->State = HAL_I2C_STATE_LISTEN;
  3812. hi2c->XferISR = I2C_Slave_ISR_IT;
  3813. /* Enable the Address Match interrupt */
  3814. I2C_Enable_IRQ(hi2c, I2C_XFER_LISTEN_IT);
  3815. return HAL_OK;
  3816. }
  3817. else
  3818. {
  3819. return HAL_BUSY;
  3820. }
  3821. }
  3822. /**
  3823. * @brief Disable the Address listen mode with Interrupt.
  3824. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3825. * the configuration information for the specified I2C
  3826. * @retval HAL status
  3827. */
  3828. HAL_StatusTypeDef HAL_I2C_DisableListen_IT(I2C_HandleTypeDef *hi2c)
  3829. {
  3830. /* Declaration of tmp to prevent undefined behavior of volatile usage */
  3831. uint32_t tmp;
  3832. /* Disable Address listen mode only if a transfer is not ongoing */
  3833. if (hi2c->State == HAL_I2C_STATE_LISTEN)
  3834. {
  3835. tmp = (uint32_t)(hi2c->State) & I2C_STATE_MSK;
  3836. hi2c->PreviousState = tmp | (uint32_t)(hi2c->Mode);
  3837. hi2c->State = HAL_I2C_STATE_READY;
  3838. hi2c->Mode = HAL_I2C_MODE_NONE;
  3839. hi2c->XferISR = NULL;
  3840. /* Disable the Address Match interrupt */
  3841. I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT);
  3842. return HAL_OK;
  3843. }
  3844. else
  3845. {
  3846. return HAL_BUSY;
  3847. }
  3848. }
  3849. /**
  3850. * @brief Abort a master or memory I2C IT or DMA process communication with Interrupt.
  3851. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3852. * the configuration information for the specified I2C.
  3853. * @param DevAddress Target device address: The device 7 bits address value
  3854. * in datasheet must be shifted to the left before calling the interface
  3855. * @retval HAL status
  3856. */
  3857. HAL_StatusTypeDef HAL_I2C_Master_Abort_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress)
  3858. {
  3859. HAL_I2C_ModeTypeDef tmp_mode = hi2c->Mode;
  3860. if ((tmp_mode == HAL_I2C_MODE_MASTER) || (tmp_mode == HAL_I2C_MODE_MEM))
  3861. {
  3862. /* Process Locked */
  3863. __HAL_LOCK(hi2c);
  3864. /* Disable Interrupts and Store Previous state */
  3865. if (hi2c->State == HAL_I2C_STATE_BUSY_TX)
  3866. {
  3867. I2C_Disable_IRQ(hi2c, I2C_XFER_TX_IT);
  3868. hi2c->PreviousState = I2C_STATE_MASTER_BUSY_TX;
  3869. }
  3870. else if (hi2c->State == HAL_I2C_STATE_BUSY_RX)
  3871. {
  3872. I2C_Disable_IRQ(hi2c, I2C_XFER_RX_IT);
  3873. hi2c->PreviousState = I2C_STATE_MASTER_BUSY_RX;
  3874. }
  3875. else
  3876. {
  3877. /* Do nothing */
  3878. }
  3879. /* Set State at HAL_I2C_STATE_ABORT */
  3880. hi2c->State = HAL_I2C_STATE_ABORT;
  3881. /* Set NBYTES to 1 to generate a dummy read on I2C peripheral */
  3882. /* Set AUTOEND mode, this will generate a NACK then STOP condition to abort the current transfer */
  3883. I2C_TransferConfig(hi2c, DevAddress, 1, I2C_AUTOEND_MODE, I2C_GENERATE_STOP);
  3884. /* Process Unlocked */
  3885. __HAL_UNLOCK(hi2c);
  3886. /* Note : The I2C interrupts must be enabled after unlocking current process
  3887. to avoid the risk of I2C interrupt handle execution before current
  3888. process unlock */
  3889. I2C_Enable_IRQ(hi2c, I2C_XFER_CPLT_IT);
  3890. return HAL_OK;
  3891. }
  3892. else
  3893. {
  3894. /* Wrong usage of abort function */
  3895. /* This function should be used only in case of abort monitored by master device */
  3896. return HAL_ERROR;
  3897. }
  3898. }
  3899. /**
  3900. * @}
  3901. */
  3902. /** @defgroup I2C_IRQ_Handler_and_Callbacks IRQ Handler and Callbacks
  3903. * @{
  3904. */
  3905. /**
  3906. * @brief This function handles I2C event interrupt request.
  3907. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3908. * the configuration information for the specified I2C.
  3909. * @retval None
  3910. */
  3911. void HAL_I2C_EV_IRQHandler(I2C_HandleTypeDef *hi2c) /* Derogation MISRAC2012-Rule-8.13 */
  3912. {
  3913. /* Get current IT Flags and IT sources value */
  3914. uint32_t itflags = READ_REG(hi2c->Instance->ISR);
  3915. uint32_t itsources = READ_REG(hi2c->Instance->CR1);
  3916. /* I2C events treatment -------------------------------------*/
  3917. if (hi2c->XferISR != NULL)
  3918. {
  3919. hi2c->XferISR(hi2c, itflags, itsources);
  3920. }
  3921. }
  3922. /**
  3923. * @brief This function handles I2C error interrupt request.
  3924. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3925. * the configuration information for the specified I2C.
  3926. * @retval None
  3927. */
  3928. void HAL_I2C_ER_IRQHandler(I2C_HandleTypeDef *hi2c)
  3929. {
  3930. uint32_t itflags = READ_REG(hi2c->Instance->ISR);
  3931. uint32_t itsources = READ_REG(hi2c->Instance->CR1);
  3932. uint32_t tmperror;
  3933. /* I2C Bus error interrupt occurred ------------------------------------*/
  3934. if ((I2C_CHECK_FLAG(itflags, I2C_FLAG_BERR) != RESET) && \
  3935. (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_ERRI) != RESET))
  3936. {
  3937. hi2c->ErrorCode |= HAL_I2C_ERROR_BERR;
  3938. /* Clear BERR flag */
  3939. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_BERR);
  3940. }
  3941. /* I2C Over-Run/Under-Run interrupt occurred ----------------------------------------*/
  3942. if ((I2C_CHECK_FLAG(itflags, I2C_FLAG_OVR) != RESET) && \
  3943. (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_ERRI) != RESET))
  3944. {
  3945. hi2c->ErrorCode |= HAL_I2C_ERROR_OVR;
  3946. /* Clear OVR flag */
  3947. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_OVR);
  3948. }
  3949. /* I2C Arbitration Loss error interrupt occurred -------------------------------------*/
  3950. if ((I2C_CHECK_FLAG(itflags, I2C_FLAG_ARLO) != RESET) && \
  3951. (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_ERRI) != RESET))
  3952. {
  3953. hi2c->ErrorCode |= HAL_I2C_ERROR_ARLO;
  3954. /* Clear ARLO flag */
  3955. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ARLO);
  3956. }
  3957. /* Store current volatile hi2c->ErrorCode, misra rule */
  3958. tmperror = hi2c->ErrorCode;
  3959. /* Call the Error Callback in case of Error detected */
  3960. if ((tmperror & (HAL_I2C_ERROR_BERR | HAL_I2C_ERROR_OVR | HAL_I2C_ERROR_ARLO)) != HAL_I2C_ERROR_NONE)
  3961. {
  3962. I2C_ITError(hi2c, tmperror);
  3963. }
  3964. }
  3965. /**
  3966. * @brief Master Tx Transfer completed callback.
  3967. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3968. * the configuration information for the specified I2C.
  3969. * @retval None
  3970. */
  3971. __weak void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c)
  3972. {
  3973. /* Prevent unused argument(s) compilation warning */
  3974. UNUSED(hi2c);
  3975. /* NOTE : This function should not be modified, when the callback is needed,
  3976. the HAL_I2C_MasterTxCpltCallback could be implemented in the user file
  3977. */
  3978. }
  3979. /**
  3980. * @brief Master Rx Transfer completed callback.
  3981. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3982. * the configuration information for the specified I2C.
  3983. * @retval None
  3984. */
  3985. __weak void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c)
  3986. {
  3987. /* Prevent unused argument(s) compilation warning */
  3988. UNUSED(hi2c);
  3989. /* NOTE : This function should not be modified, when the callback is needed,
  3990. the HAL_I2C_MasterRxCpltCallback could be implemented in the user file
  3991. */
  3992. }
  3993. /** @brief Slave Tx Transfer completed callback.
  3994. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3995. * the configuration information for the specified I2C.
  3996. * @retval None
  3997. */
  3998. __weak void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *hi2c)
  3999. {
  4000. /* Prevent unused argument(s) compilation warning */
  4001. UNUSED(hi2c);
  4002. /* NOTE : This function should not be modified, when the callback is needed,
  4003. the HAL_I2C_SlaveTxCpltCallback could be implemented in the user file
  4004. */
  4005. }
  4006. /**
  4007. * @brief Slave Rx Transfer completed callback.
  4008. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4009. * the configuration information for the specified I2C.
  4010. * @retval None
  4011. */
  4012. __weak void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c)
  4013. {
  4014. /* Prevent unused argument(s) compilation warning */
  4015. UNUSED(hi2c);
  4016. /* NOTE : This function should not be modified, when the callback is needed,
  4017. the HAL_I2C_SlaveRxCpltCallback could be implemented in the user file
  4018. */
  4019. }
  4020. /**
  4021. * @brief Slave Address Match callback.
  4022. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4023. * the configuration information for the specified I2C.
  4024. * @param TransferDirection Master request Transfer Direction (Write/Read), value of @ref I2C_XFERDIRECTION
  4025. * @param AddrMatchCode Address Match Code
  4026. * @retval None
  4027. */
  4028. __weak void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode)
  4029. {
  4030. /* Prevent unused argument(s) compilation warning */
  4031. UNUSED(hi2c);
  4032. UNUSED(TransferDirection);
  4033. UNUSED(AddrMatchCode);
  4034. /* NOTE : This function should not be modified, when the callback is needed,
  4035. the HAL_I2C_AddrCallback() could be implemented in the user file
  4036. */
  4037. }
  4038. /**
  4039. * @brief Listen Complete callback.
  4040. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4041. * the configuration information for the specified I2C.
  4042. * @retval None
  4043. */
  4044. __weak void HAL_I2C_ListenCpltCallback(I2C_HandleTypeDef *hi2c)
  4045. {
  4046. /* Prevent unused argument(s) compilation warning */
  4047. UNUSED(hi2c);
  4048. /* NOTE : This function should not be modified, when the callback is needed,
  4049. the HAL_I2C_ListenCpltCallback() could be implemented in the user file
  4050. */
  4051. }
  4052. /**
  4053. * @brief Memory Tx Transfer completed callback.
  4054. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4055. * the configuration information for the specified I2C.
  4056. * @retval None
  4057. */
  4058. __weak void HAL_I2C_MemTxCpltCallback(I2C_HandleTypeDef *hi2c)
  4059. {
  4060. /* Prevent unused argument(s) compilation warning */
  4061. UNUSED(hi2c);
  4062. /* NOTE : This function should not be modified, when the callback is needed,
  4063. the HAL_I2C_MemTxCpltCallback could be implemented in the user file
  4064. */
  4065. }
  4066. /**
  4067. * @brief Memory Rx Transfer completed callback.
  4068. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4069. * the configuration information for the specified I2C.
  4070. * @retval None
  4071. */
  4072. __weak void HAL_I2C_MemRxCpltCallback(I2C_HandleTypeDef *hi2c)
  4073. {
  4074. /* Prevent unused argument(s) compilation warning */
  4075. UNUSED(hi2c);
  4076. /* NOTE : This function should not be modified, when the callback is needed,
  4077. the HAL_I2C_MemRxCpltCallback could be implemented in the user file
  4078. */
  4079. }
  4080. /**
  4081. * @brief I2C error callback.
  4082. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4083. * the configuration information for the specified I2C.
  4084. * @retval None
  4085. */
  4086. __weak void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c)
  4087. {
  4088. /* Prevent unused argument(s) compilation warning */
  4089. UNUSED(hi2c);
  4090. /* NOTE : This function should not be modified, when the callback is needed,
  4091. the HAL_I2C_ErrorCallback could be implemented in the user file
  4092. */
  4093. }
  4094. /**
  4095. * @brief I2C abort callback.
  4096. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4097. * the configuration information for the specified I2C.
  4098. * @retval None
  4099. */
  4100. __weak void HAL_I2C_AbortCpltCallback(I2C_HandleTypeDef *hi2c)
  4101. {
  4102. /* Prevent unused argument(s) compilation warning */
  4103. UNUSED(hi2c);
  4104. /* NOTE : This function should not be modified, when the callback is needed,
  4105. the HAL_I2C_AbortCpltCallback could be implemented in the user file
  4106. */
  4107. }
  4108. /**
  4109. * @}
  4110. */
  4111. /** @defgroup I2C_Exported_Functions_Group3 Peripheral State, Mode and Error functions
  4112. * @brief Peripheral State, Mode and Error functions
  4113. *
  4114. @verbatim
  4115. ===============================================================================
  4116. ##### Peripheral State, Mode and Error functions #####
  4117. ===============================================================================
  4118. [..]
  4119. This subsection permit to get in run-time the status of the peripheral
  4120. and the data flow.
  4121. @endverbatim
  4122. * @{
  4123. */
  4124. /**
  4125. * @brief Return the I2C handle state.
  4126. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4127. * the configuration information for the specified I2C.
  4128. * @retval HAL state
  4129. */
  4130. HAL_I2C_StateTypeDef HAL_I2C_GetState(const I2C_HandleTypeDef *hi2c)
  4131. {
  4132. /* Return I2C handle state */
  4133. return hi2c->State;
  4134. }
  4135. /**
  4136. * @brief Returns the I2C Master, Slave, Memory or no mode.
  4137. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4138. * the configuration information for I2C module
  4139. * @retval HAL mode
  4140. */
  4141. HAL_I2C_ModeTypeDef HAL_I2C_GetMode(const I2C_HandleTypeDef *hi2c)
  4142. {
  4143. return hi2c->Mode;
  4144. }
  4145. /**
  4146. * @brief Return the I2C error code.
  4147. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4148. * the configuration information for the specified I2C.
  4149. * @retval I2C Error Code
  4150. */
  4151. uint32_t HAL_I2C_GetError(const I2C_HandleTypeDef *hi2c)
  4152. {
  4153. return hi2c->ErrorCode;
  4154. }
  4155. /**
  4156. * @}
  4157. */
  4158. /**
  4159. * @}
  4160. */
  4161. /** @addtogroup I2C_Private_Functions
  4162. * @{
  4163. */
  4164. /**
  4165. * @brief Interrupt Sub-Routine which handle the Interrupt Flags Master Mode with Interrupt.
  4166. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4167. * the configuration information for the specified I2C.
  4168. * @param ITFlags Interrupt flags to handle.
  4169. * @param ITSources Interrupt sources enabled.
  4170. * @retval HAL status
  4171. */
  4172. static HAL_StatusTypeDef I2C_Master_ISR_IT(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags,
  4173. uint32_t ITSources)
  4174. {
  4175. uint16_t devaddress;
  4176. uint32_t tmpITFlags = ITFlags;
  4177. /* Process Locked */
  4178. __HAL_LOCK(hi2c);
  4179. if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_AF) != RESET) && \
  4180. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_NACKI) != RESET))
  4181. {
  4182. /* Clear NACK Flag */
  4183. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  4184. /* Set corresponding Error Code */
  4185. /* No need to generate STOP, it is automatically done */
  4186. /* Error callback will be send during stop flag treatment */
  4187. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  4188. /* Flush TX register */
  4189. I2C_Flush_TXDR(hi2c);
  4190. }
  4191. else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_RXNE) != RESET) && \
  4192. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_RXI) != RESET))
  4193. {
  4194. /* Remove RXNE flag on temporary variable as read done */
  4195. tmpITFlags &= ~I2C_FLAG_RXNE;
  4196. /* Read data from RXDR */
  4197. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->RXDR;
  4198. /* Increment Buffer pointer */
  4199. hi2c->pBuffPtr++;
  4200. hi2c->XferSize--;
  4201. hi2c->XferCount--;
  4202. }
  4203. else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_TC) == RESET) && \
  4204. ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_TXIS) != RESET) && \
  4205. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TXI) != RESET)))
  4206. {
  4207. /* Write data to TXDR */
  4208. if (hi2c->XferCount != 0U)
  4209. {
  4210. /* Write data to TXDR */
  4211. hi2c->Instance->TXDR = *hi2c->pBuffPtr;
  4212. /* Increment Buffer pointer */
  4213. hi2c->pBuffPtr++;
  4214. hi2c->XferSize--;
  4215. hi2c->XferCount--;
  4216. }
  4217. }
  4218. else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_TCR) != RESET) && \
  4219. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TCI) != RESET))
  4220. {
  4221. if ((hi2c->XferCount != 0U) && (hi2c->XferSize == 0U))
  4222. {
  4223. devaddress = (uint16_t)(hi2c->Instance->CR2 & I2C_CR2_SADD);
  4224. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  4225. {
  4226. /* Errata workaround 170323 */
  4227. if (I2C_GET_DIR(hi2c) == I2C_DIRECTION_RECEIVE)
  4228. {
  4229. hi2c->XferSize = 1U;
  4230. }
  4231. else
  4232. {
  4233. hi2c->XferSize = MAX_NBYTE_SIZE;
  4234. }
  4235. I2C_TransferConfig(hi2c, devaddress, (uint8_t)hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  4236. }
  4237. else
  4238. {
  4239. hi2c->XferSize = hi2c->XferCount;
  4240. if (hi2c->XferOptions != I2C_NO_OPTION_FRAME)
  4241. {
  4242. I2C_TransferConfig(hi2c, devaddress, (uint8_t)hi2c->XferSize,
  4243. hi2c->XferOptions, I2C_NO_STARTSTOP);
  4244. }
  4245. else
  4246. {
  4247. I2C_TransferConfig(hi2c, devaddress, (uint8_t)hi2c->XferSize,
  4248. I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  4249. }
  4250. }
  4251. }
  4252. else
  4253. {
  4254. /* Call TxCpltCallback() if no stop mode is set */
  4255. if (I2C_GET_STOP_MODE(hi2c) != I2C_AUTOEND_MODE)
  4256. {
  4257. /* Call I2C Master Sequential complete process */
  4258. I2C_ITMasterSeqCplt(hi2c);
  4259. }
  4260. else
  4261. {
  4262. /* Wrong size Status regarding TCR flag event */
  4263. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4264. I2C_ITError(hi2c, HAL_I2C_ERROR_SIZE);
  4265. }
  4266. }
  4267. }
  4268. else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_TC) != RESET) && \
  4269. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TCI) != RESET))
  4270. {
  4271. if (hi2c->XferCount == 0U)
  4272. {
  4273. if (I2C_GET_STOP_MODE(hi2c) != I2C_AUTOEND_MODE)
  4274. {
  4275. /* Generate a stop condition in case of no transfer option */
  4276. if (hi2c->XferOptions == I2C_NO_OPTION_FRAME)
  4277. {
  4278. /* Generate Stop */
  4279. hi2c->Instance->CR2 |= I2C_CR2_STOP;
  4280. }
  4281. else
  4282. {
  4283. /* Call I2C Master Sequential complete process */
  4284. I2C_ITMasterSeqCplt(hi2c);
  4285. }
  4286. }
  4287. }
  4288. else
  4289. {
  4290. /* Wrong size Status regarding TC flag event */
  4291. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4292. I2C_ITError(hi2c, HAL_I2C_ERROR_SIZE);
  4293. }
  4294. }
  4295. else
  4296. {
  4297. /* Nothing to do */
  4298. }
  4299. if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_STOPF) != RESET) && \
  4300. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_STOPI) != RESET))
  4301. {
  4302. /* Call I2C Master complete process */
  4303. I2C_ITMasterCplt(hi2c, tmpITFlags);
  4304. }
  4305. /* Process Unlocked */
  4306. __HAL_UNLOCK(hi2c);
  4307. return HAL_OK;
  4308. }
  4309. /**
  4310. * @brief Interrupt Sub-Routine which handle the Interrupt Flags Memory Mode with Interrupt.
  4311. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4312. * the configuration information for the specified I2C.
  4313. * @param ITFlags Interrupt flags to handle.
  4314. * @param ITSources Interrupt sources enabled.
  4315. * @retval HAL status
  4316. */
  4317. static HAL_StatusTypeDef I2C_Mem_ISR_IT(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags,
  4318. uint32_t ITSources)
  4319. {
  4320. uint32_t direction = I2C_GENERATE_START_WRITE;
  4321. uint32_t tmpITFlags = ITFlags;
  4322. /* Process Locked */
  4323. __HAL_LOCK(hi2c);
  4324. if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_AF) != RESET) && \
  4325. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_NACKI) != RESET))
  4326. {
  4327. /* Clear NACK Flag */
  4328. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  4329. /* Set corresponding Error Code */
  4330. /* No need to generate STOP, it is automatically done */
  4331. /* Error callback will be send during stop flag treatment */
  4332. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  4333. /* Flush TX register */
  4334. I2C_Flush_TXDR(hi2c);
  4335. }
  4336. else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_RXNE) != RESET) && \
  4337. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_RXI) != RESET))
  4338. {
  4339. /* Remove RXNE flag on temporary variable as read done */
  4340. tmpITFlags &= ~I2C_FLAG_RXNE;
  4341. /* Read data from RXDR */
  4342. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->RXDR;
  4343. /* Increment Buffer pointer */
  4344. hi2c->pBuffPtr++;
  4345. hi2c->XferSize--;
  4346. hi2c->XferCount--;
  4347. }
  4348. else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_TXIS) != RESET) && \
  4349. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TXI) != RESET))
  4350. {
  4351. if (hi2c->Memaddress == 0xFFFFFFFFU)
  4352. {
  4353. /* Write data to TXDR */
  4354. hi2c->Instance->TXDR = *hi2c->pBuffPtr;
  4355. /* Increment Buffer pointer */
  4356. hi2c->pBuffPtr++;
  4357. hi2c->XferSize--;
  4358. hi2c->XferCount--;
  4359. }
  4360. else
  4361. {
  4362. /* Write LSB part of Memory Address */
  4363. hi2c->Instance->TXDR = hi2c->Memaddress;
  4364. /* Reset Memaddress content */
  4365. hi2c->Memaddress = 0xFFFFFFFFU;
  4366. }
  4367. }
  4368. else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_TCR) != RESET) && \
  4369. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TCI) != RESET))
  4370. {
  4371. if ((hi2c->XferCount != 0U) && (hi2c->XferSize == 0U))
  4372. {
  4373. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  4374. {
  4375. /* Errata workaround 170323 */
  4376. if (I2C_GET_DIR(hi2c) == I2C_DIRECTION_RECEIVE)
  4377. {
  4378. hi2c->XferSize = 1U;
  4379. }
  4380. else
  4381. {
  4382. hi2c->XferSize = MAX_NBYTE_SIZE;
  4383. }
  4384. I2C_TransferConfig(hi2c, (uint16_t)hi2c->Devaddress, (uint8_t)hi2c->XferSize,
  4385. I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  4386. }
  4387. else
  4388. {
  4389. hi2c->XferSize = hi2c->XferCount;
  4390. I2C_TransferConfig(hi2c, (uint16_t)hi2c->Devaddress, (uint8_t)hi2c->XferSize,
  4391. I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  4392. }
  4393. }
  4394. else
  4395. {
  4396. /* Wrong size Status regarding TCR flag event */
  4397. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4398. I2C_ITError(hi2c, HAL_I2C_ERROR_SIZE);
  4399. }
  4400. }
  4401. else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_TC) != RESET) && \
  4402. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TCI) != RESET))
  4403. {
  4404. /* Disable Interrupt related to address step */
  4405. I2C_Disable_IRQ(hi2c, I2C_XFER_TX_IT);
  4406. /* Enable ERR, TC, STOP, NACK and RXI interrupts */
  4407. I2C_Enable_IRQ(hi2c, I2C_XFER_RX_IT);
  4408. if (hi2c->State == HAL_I2C_STATE_BUSY_RX)
  4409. {
  4410. direction = I2C_GENERATE_START_READ;
  4411. }
  4412. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  4413. {
  4414. /* Errata workaround 170323 */
  4415. if (I2C_GET_DIR(hi2c) == I2C_DIRECTION_RECEIVE)
  4416. {
  4417. hi2c->XferSize = 1U;
  4418. }
  4419. else
  4420. {
  4421. hi2c->XferSize = MAX_NBYTE_SIZE;
  4422. }
  4423. /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
  4424. I2C_TransferConfig(hi2c, (uint16_t)hi2c->Devaddress, (uint8_t)hi2c->XferSize,
  4425. I2C_RELOAD_MODE, direction);
  4426. }
  4427. else
  4428. {
  4429. hi2c->XferSize = hi2c->XferCount;
  4430. /* Set NBYTES to write and generate RESTART */
  4431. I2C_TransferConfig(hi2c, (uint16_t)hi2c->Devaddress, (uint8_t)hi2c->XferSize,
  4432. I2C_AUTOEND_MODE, direction);
  4433. }
  4434. }
  4435. else
  4436. {
  4437. /* Nothing to do */
  4438. }
  4439. if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_STOPF) != RESET) && \
  4440. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_STOPI) != RESET))
  4441. {
  4442. /* Call I2C Master complete process */
  4443. I2C_ITMasterCplt(hi2c, tmpITFlags);
  4444. }
  4445. /* Process Unlocked */
  4446. __HAL_UNLOCK(hi2c);
  4447. return HAL_OK;
  4448. }
  4449. /**
  4450. * @brief Interrupt Sub-Routine which handle the Interrupt Flags Slave Mode with Interrupt.
  4451. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4452. * the configuration information for the specified I2C.
  4453. * @param ITFlags Interrupt flags to handle.
  4454. * @param ITSources Interrupt sources enabled.
  4455. * @retval HAL status
  4456. */
  4457. static HAL_StatusTypeDef I2C_Slave_ISR_IT(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags,
  4458. uint32_t ITSources)
  4459. {
  4460. uint32_t tmpoptions = hi2c->XferOptions;
  4461. uint32_t tmpITFlags = ITFlags;
  4462. /* Process locked */
  4463. __HAL_LOCK(hi2c);
  4464. /* Check if STOPF is set */
  4465. if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_STOPF) != RESET) && \
  4466. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_STOPI) != RESET))
  4467. {
  4468. /* Call I2C Slave complete process */
  4469. I2C_ITSlaveCplt(hi2c, tmpITFlags);
  4470. }
  4471. else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_AF) != RESET) && \
  4472. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_NACKI) != RESET))
  4473. {
  4474. /* Check that I2C transfer finished */
  4475. /* if yes, normal use case, a NACK is sent by the MASTER when Transfer is finished */
  4476. /* Mean XferCount == 0*/
  4477. /* So clear Flag NACKF only */
  4478. if (hi2c->XferCount == 0U)
  4479. {
  4480. if ((hi2c->State == HAL_I2C_STATE_LISTEN) && (tmpoptions == I2C_FIRST_AND_LAST_FRAME))
  4481. /* Same action must be done for (tmpoptions == I2C_LAST_FRAME) which removed for
  4482. Warning[Pa134]: left and right operands are identical */
  4483. {
  4484. /* Call I2C Listen complete process */
  4485. I2C_ITListenCplt(hi2c, tmpITFlags);
  4486. }
  4487. else if ((hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN) && (tmpoptions != I2C_NO_OPTION_FRAME))
  4488. {
  4489. /* Clear NACK Flag */
  4490. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  4491. /* Flush TX register */
  4492. I2C_Flush_TXDR(hi2c);
  4493. /* Last Byte is Transmitted */
  4494. /* Call I2C Slave Sequential complete process */
  4495. I2C_ITSlaveSeqCplt(hi2c);
  4496. }
  4497. else
  4498. {
  4499. /* Clear NACK Flag */
  4500. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  4501. }
  4502. }
  4503. else
  4504. {
  4505. /* if no, error use case, a Non-Acknowledge of last Data is generated by the MASTER*/
  4506. /* Clear NACK Flag */
  4507. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  4508. /* Set ErrorCode corresponding to a Non-Acknowledge */
  4509. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  4510. if ((tmpoptions == I2C_FIRST_FRAME) || (tmpoptions == I2C_NEXT_FRAME))
  4511. {
  4512. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4513. I2C_ITError(hi2c, hi2c->ErrorCode);
  4514. }
  4515. }
  4516. }
  4517. else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_RXNE) != RESET) && \
  4518. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_RXI) != RESET))
  4519. {
  4520. if (hi2c->XferCount > 0U)
  4521. {
  4522. /* Read data from RXDR */
  4523. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->RXDR;
  4524. /* Increment Buffer pointer */
  4525. hi2c->pBuffPtr++;
  4526. hi2c->XferSize--;
  4527. hi2c->XferCount--;
  4528. }
  4529. if ((hi2c->XferCount == 0U) && \
  4530. (tmpoptions != I2C_NO_OPTION_FRAME))
  4531. {
  4532. /* Call I2C Slave Sequential complete process */
  4533. I2C_ITSlaveSeqCplt(hi2c);
  4534. }
  4535. }
  4536. else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_ADDR) != RESET) && \
  4537. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_ADDRI) != RESET))
  4538. {
  4539. I2C_ITAddrCplt(hi2c, tmpITFlags);
  4540. }
  4541. else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_TXIS) != RESET) && \
  4542. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TXI) != RESET))
  4543. {
  4544. /* Write data to TXDR only if XferCount not reach "0" */
  4545. /* A TXIS flag can be set, during STOP treatment */
  4546. /* Check if all Data have already been sent */
  4547. /* If it is the case, this last write in TXDR is not sent, correspond to a dummy TXIS event */
  4548. if (hi2c->XferCount > 0U)
  4549. {
  4550. /* Write data to TXDR */
  4551. hi2c->Instance->TXDR = *hi2c->pBuffPtr;
  4552. /* Increment Buffer pointer */
  4553. hi2c->pBuffPtr++;
  4554. hi2c->XferCount--;
  4555. hi2c->XferSize--;
  4556. }
  4557. else
  4558. {
  4559. if ((tmpoptions == I2C_NEXT_FRAME) || (tmpoptions == I2C_FIRST_FRAME))
  4560. {
  4561. /* Last Byte is Transmitted */
  4562. /* Call I2C Slave Sequential complete process */
  4563. I2C_ITSlaveSeqCplt(hi2c);
  4564. }
  4565. }
  4566. }
  4567. else
  4568. {
  4569. /* Nothing to do */
  4570. }
  4571. /* Process Unlocked */
  4572. __HAL_UNLOCK(hi2c);
  4573. return HAL_OK;
  4574. }
  4575. /**
  4576. * @brief Interrupt Sub-Routine which handle the Interrupt Flags Master Mode with DMA.
  4577. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4578. * the configuration information for the specified I2C.
  4579. * @param ITFlags Interrupt flags to handle.
  4580. * @param ITSources Interrupt sources enabled.
  4581. * @retval HAL status
  4582. */
  4583. static HAL_StatusTypeDef I2C_Master_ISR_DMA(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags,
  4584. uint32_t ITSources)
  4585. {
  4586. uint16_t devaddress;
  4587. uint32_t xfermode;
  4588. /* Process Locked */
  4589. __HAL_LOCK(hi2c);
  4590. if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_AF) != RESET) && \
  4591. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_NACKI) != RESET))
  4592. {
  4593. /* Clear NACK Flag */
  4594. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  4595. /* Set corresponding Error Code */
  4596. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  4597. /* No need to generate STOP, it is automatically done */
  4598. /* But enable STOP interrupt, to treat it */
  4599. /* Error callback will be send during stop flag treatment */
  4600. I2C_Enable_IRQ(hi2c, I2C_XFER_CPLT_IT);
  4601. /* Flush TX register */
  4602. I2C_Flush_TXDR(hi2c);
  4603. }
  4604. else if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_TCR) != RESET) && \
  4605. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TCI) != RESET))
  4606. {
  4607. /* Disable TC interrupt */
  4608. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_TCI);
  4609. if (hi2c->XferCount != 0U)
  4610. {
  4611. /* Recover Slave address */
  4612. devaddress = (uint16_t)(hi2c->Instance->CR2 & I2C_CR2_SADD);
  4613. /* Prepare the new XferSize to transfer */
  4614. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  4615. {
  4616. /* Errata workaround 170323 */
  4617. if (I2C_GET_DIR(hi2c) == I2C_DIRECTION_RECEIVE)
  4618. {
  4619. hi2c->XferSize = 1U;
  4620. }
  4621. else
  4622. {
  4623. hi2c->XferSize = MAX_NBYTE_SIZE;
  4624. }
  4625. xfermode = I2C_RELOAD_MODE;
  4626. }
  4627. else
  4628. {
  4629. hi2c->XferSize = hi2c->XferCount;
  4630. if (hi2c->XferOptions != I2C_NO_OPTION_FRAME)
  4631. {
  4632. xfermode = hi2c->XferOptions;
  4633. }
  4634. else
  4635. {
  4636. xfermode = I2C_AUTOEND_MODE;
  4637. }
  4638. }
  4639. /* Set the new XferSize in Nbytes register */
  4640. I2C_TransferConfig(hi2c, devaddress, (uint8_t)hi2c->XferSize, xfermode, I2C_NO_STARTSTOP);
  4641. /* Update XferCount value */
  4642. hi2c->XferCount -= hi2c->XferSize;
  4643. /* Enable DMA Request */
  4644. if (hi2c->State == HAL_I2C_STATE_BUSY_RX)
  4645. {
  4646. hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
  4647. }
  4648. else
  4649. {
  4650. hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
  4651. }
  4652. }
  4653. else
  4654. {
  4655. /* Call TxCpltCallback() if no stop mode is set */
  4656. if (I2C_GET_STOP_MODE(hi2c) != I2C_AUTOEND_MODE)
  4657. {
  4658. /* Call I2C Master Sequential complete process */
  4659. I2C_ITMasterSeqCplt(hi2c);
  4660. }
  4661. else
  4662. {
  4663. /* Wrong size Status regarding TCR flag event */
  4664. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4665. I2C_ITError(hi2c, HAL_I2C_ERROR_SIZE);
  4666. }
  4667. }
  4668. }
  4669. else if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_TC) != RESET) && \
  4670. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TCI) != RESET))
  4671. {
  4672. if (hi2c->XferCount == 0U)
  4673. {
  4674. if (I2C_GET_STOP_MODE(hi2c) != I2C_AUTOEND_MODE)
  4675. {
  4676. /* Generate a stop condition in case of no transfer option */
  4677. if (hi2c->XferOptions == I2C_NO_OPTION_FRAME)
  4678. {
  4679. /* Generate Stop */
  4680. hi2c->Instance->CR2 |= I2C_CR2_STOP;
  4681. }
  4682. else
  4683. {
  4684. /* Call I2C Master Sequential complete process */
  4685. I2C_ITMasterSeqCplt(hi2c);
  4686. }
  4687. }
  4688. }
  4689. else
  4690. {
  4691. /* Wrong size Status regarding TC flag event */
  4692. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4693. I2C_ITError(hi2c, HAL_I2C_ERROR_SIZE);
  4694. }
  4695. }
  4696. else if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_STOPF) != RESET) && \
  4697. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_STOPI) != RESET))
  4698. {
  4699. /* Call I2C Master complete process */
  4700. I2C_ITMasterCplt(hi2c, ITFlags);
  4701. }
  4702. else
  4703. {
  4704. /* Nothing to do */
  4705. }
  4706. /* Process Unlocked */
  4707. __HAL_UNLOCK(hi2c);
  4708. return HAL_OK;
  4709. }
  4710. /**
  4711. * @brief Interrupt Sub-Routine which handle the Interrupt Flags Memory Mode with DMA.
  4712. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4713. * the configuration information for the specified I2C.
  4714. * @param ITFlags Interrupt flags to handle.
  4715. * @param ITSources Interrupt sources enabled.
  4716. * @retval HAL status
  4717. */
  4718. static HAL_StatusTypeDef I2C_Mem_ISR_DMA(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags,
  4719. uint32_t ITSources)
  4720. {
  4721. uint32_t direction = I2C_GENERATE_START_WRITE;
  4722. /* Process Locked */
  4723. __HAL_LOCK(hi2c);
  4724. if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_AF) != RESET) && \
  4725. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_NACKI) != RESET))
  4726. {
  4727. /* Clear NACK Flag */
  4728. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  4729. /* Set corresponding Error Code */
  4730. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  4731. /* No need to generate STOP, it is automatically done */
  4732. /* But enable STOP interrupt, to treat it */
  4733. /* Error callback will be send during stop flag treatment */
  4734. I2C_Enable_IRQ(hi2c, I2C_XFER_CPLT_IT);
  4735. /* Flush TX register */
  4736. I2C_Flush_TXDR(hi2c);
  4737. }
  4738. else if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_TXIS) != RESET) && \
  4739. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TXI) != RESET))
  4740. {
  4741. /* Write LSB part of Memory Address */
  4742. hi2c->Instance->TXDR = hi2c->Memaddress;
  4743. /* Reset Memaddress content */
  4744. hi2c->Memaddress = 0xFFFFFFFFU;
  4745. }
  4746. else if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_TCR) != RESET) && \
  4747. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TCI) != RESET))
  4748. {
  4749. /* Disable Interrupt related to address step */
  4750. I2C_Disable_IRQ(hi2c, I2C_XFER_TX_IT);
  4751. /* Enable only Error interrupt */
  4752. I2C_Enable_IRQ(hi2c, I2C_XFER_ERROR_IT);
  4753. if (hi2c->XferCount != 0U)
  4754. {
  4755. /* Prepare the new XferSize to transfer */
  4756. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  4757. {
  4758. /* Errata workaround 170323 */
  4759. if (I2C_GET_DIR(hi2c) == I2C_DIRECTION_RECEIVE)
  4760. {
  4761. hi2c->XferSize = 1U;
  4762. }
  4763. else
  4764. {
  4765. hi2c->XferSize = MAX_NBYTE_SIZE;
  4766. }
  4767. I2C_TransferConfig(hi2c, (uint16_t)hi2c->Devaddress, (uint8_t)hi2c->XferSize,
  4768. I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  4769. }
  4770. else
  4771. {
  4772. hi2c->XferSize = hi2c->XferCount;
  4773. I2C_TransferConfig(hi2c, (uint16_t)hi2c->Devaddress, (uint8_t)hi2c->XferSize,
  4774. I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  4775. }
  4776. /* Update XferCount value */
  4777. hi2c->XferCount -= hi2c->XferSize;
  4778. /* Enable DMA Request */
  4779. if (hi2c->State == HAL_I2C_STATE_BUSY_RX)
  4780. {
  4781. hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
  4782. }
  4783. else
  4784. {
  4785. hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
  4786. }
  4787. }
  4788. else
  4789. {
  4790. /* Wrong size Status regarding TCR flag event */
  4791. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4792. I2C_ITError(hi2c, HAL_I2C_ERROR_SIZE);
  4793. }
  4794. }
  4795. else if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_TC) != RESET) && \
  4796. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TCI) != RESET))
  4797. {
  4798. /* Disable Interrupt related to address step */
  4799. I2C_Disable_IRQ(hi2c, I2C_XFER_TX_IT);
  4800. /* Enable only Error and NACK interrupt for data transfer */
  4801. I2C_Enable_IRQ(hi2c, I2C_XFER_ERROR_IT);
  4802. if (hi2c->State == HAL_I2C_STATE_BUSY_RX)
  4803. {
  4804. direction = I2C_GENERATE_START_READ;
  4805. }
  4806. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  4807. {
  4808. /* Errata workaround 170323 */
  4809. if (I2C_GET_DIR(hi2c) == I2C_DIRECTION_RECEIVE)
  4810. {
  4811. hi2c->XferSize = 1U;
  4812. }
  4813. else
  4814. {
  4815. hi2c->XferSize = MAX_NBYTE_SIZE;
  4816. }
  4817. /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
  4818. I2C_TransferConfig(hi2c, (uint16_t)hi2c->Devaddress, (uint8_t)hi2c->XferSize,
  4819. I2C_RELOAD_MODE, direction);
  4820. }
  4821. else
  4822. {
  4823. hi2c->XferSize = hi2c->XferCount;
  4824. /* Set NBYTES to write and generate RESTART */
  4825. I2C_TransferConfig(hi2c, (uint16_t)hi2c->Devaddress, (uint8_t)hi2c->XferSize,
  4826. I2C_AUTOEND_MODE, direction);
  4827. }
  4828. /* Update XferCount value */
  4829. hi2c->XferCount -= hi2c->XferSize;
  4830. /* Enable DMA Request */
  4831. if (hi2c->State == HAL_I2C_STATE_BUSY_RX)
  4832. {
  4833. hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
  4834. }
  4835. else
  4836. {
  4837. hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
  4838. }
  4839. }
  4840. else if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_STOPF) != RESET) && \
  4841. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_STOPI) != RESET))
  4842. {
  4843. /* Call I2C Master complete process */
  4844. I2C_ITMasterCplt(hi2c, ITFlags);
  4845. }
  4846. else
  4847. {
  4848. /* Nothing to do */
  4849. }
  4850. /* Process Unlocked */
  4851. __HAL_UNLOCK(hi2c);
  4852. return HAL_OK;
  4853. }
  4854. /**
  4855. * @brief Interrupt Sub-Routine which handle the Interrupt Flags Slave Mode with DMA.
  4856. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4857. * the configuration information for the specified I2C.
  4858. * @param ITFlags Interrupt flags to handle.
  4859. * @param ITSources Interrupt sources enabled.
  4860. * @retval HAL status
  4861. */
  4862. static HAL_StatusTypeDef I2C_Slave_ISR_DMA(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags,
  4863. uint32_t ITSources)
  4864. {
  4865. uint32_t tmpoptions = hi2c->XferOptions;
  4866. uint32_t treatdmanack = 0U;
  4867. HAL_I2C_StateTypeDef tmpstate;
  4868. /* Process locked */
  4869. __HAL_LOCK(hi2c);
  4870. /* Check if STOPF is set */
  4871. if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_STOPF) != RESET) && \
  4872. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_STOPI) != RESET))
  4873. {
  4874. /* Call I2C Slave complete process */
  4875. I2C_ITSlaveCplt(hi2c, ITFlags);
  4876. }
  4877. else if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_AF) != RESET) && \
  4878. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_NACKI) != RESET))
  4879. {
  4880. /* Check that I2C transfer finished */
  4881. /* if yes, normal use case, a NACK is sent by the MASTER when Transfer is finished */
  4882. /* Mean XferCount == 0 */
  4883. /* So clear Flag NACKF only */
  4884. if ((I2C_CHECK_IT_SOURCE(ITSources, I2C_CR1_TXDMAEN) != RESET) ||
  4885. (I2C_CHECK_IT_SOURCE(ITSources, I2C_CR1_RXDMAEN) != RESET))
  4886. {
  4887. /* Split check of hdmarx, for MISRA compliance */
  4888. if (hi2c->hdmarx != NULL)
  4889. {
  4890. if (I2C_CHECK_IT_SOURCE(ITSources, I2C_CR1_RXDMAEN) != RESET)
  4891. {
  4892. if (I2C_GET_DMA_REMAIN_DATA(hi2c->hdmarx) == 0U)
  4893. {
  4894. treatdmanack = 1U;
  4895. }
  4896. }
  4897. }
  4898. /* Split check of hdmatx, for MISRA compliance */
  4899. if (hi2c->hdmatx != NULL)
  4900. {
  4901. if (I2C_CHECK_IT_SOURCE(ITSources, I2C_CR1_TXDMAEN) != RESET)
  4902. {
  4903. if (I2C_GET_DMA_REMAIN_DATA(hi2c->hdmatx) == 0U)
  4904. {
  4905. treatdmanack = 1U;
  4906. }
  4907. }
  4908. }
  4909. if (treatdmanack == 1U)
  4910. {
  4911. if ((hi2c->State == HAL_I2C_STATE_LISTEN) && (tmpoptions == I2C_FIRST_AND_LAST_FRAME))
  4912. /* Same action must be done for (tmpoptions == I2C_LAST_FRAME) which removed for
  4913. Warning[Pa134]: left and right operands are identical */
  4914. {
  4915. /* Call I2C Listen complete process */
  4916. I2C_ITListenCplt(hi2c, ITFlags);
  4917. }
  4918. else if ((hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN) && (tmpoptions != I2C_NO_OPTION_FRAME))
  4919. {
  4920. /* Clear NACK Flag */
  4921. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  4922. /* Flush TX register */
  4923. I2C_Flush_TXDR(hi2c);
  4924. /* Last Byte is Transmitted */
  4925. /* Call I2C Slave Sequential complete process */
  4926. I2C_ITSlaveSeqCplt(hi2c);
  4927. }
  4928. else
  4929. {
  4930. /* Clear NACK Flag */
  4931. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  4932. }
  4933. }
  4934. else
  4935. {
  4936. /* if no, error use case, a Non-Acknowledge of last Data is generated by the MASTER*/
  4937. /* Clear NACK Flag */
  4938. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  4939. /* Set ErrorCode corresponding to a Non-Acknowledge */
  4940. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  4941. /* Store current hi2c->State, solve MISRA2012-Rule-13.5 */
  4942. tmpstate = hi2c->State;
  4943. if ((tmpoptions == I2C_FIRST_FRAME) || (tmpoptions == I2C_NEXT_FRAME))
  4944. {
  4945. if ((tmpstate == HAL_I2C_STATE_BUSY_TX) || (tmpstate == HAL_I2C_STATE_BUSY_TX_LISTEN))
  4946. {
  4947. hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_TX;
  4948. }
  4949. else if ((tmpstate == HAL_I2C_STATE_BUSY_RX) || (tmpstate == HAL_I2C_STATE_BUSY_RX_LISTEN))
  4950. {
  4951. hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_RX;
  4952. }
  4953. else
  4954. {
  4955. /* Do nothing */
  4956. }
  4957. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4958. I2C_ITError(hi2c, hi2c->ErrorCode);
  4959. }
  4960. }
  4961. }
  4962. else
  4963. {
  4964. /* Only Clear NACK Flag, no DMA treatment is pending */
  4965. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  4966. }
  4967. }
  4968. else if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_ADDR) != RESET) && \
  4969. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_ADDRI) != RESET))
  4970. {
  4971. I2C_ITAddrCplt(hi2c, ITFlags);
  4972. }
  4973. else
  4974. {
  4975. /* Nothing to do */
  4976. }
  4977. /* Process Unlocked */
  4978. __HAL_UNLOCK(hi2c);
  4979. return HAL_OK;
  4980. }
  4981. /**
  4982. * @brief Master sends target device address followed by internal memory address for write request.
  4983. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4984. * the configuration information for the specified I2C.
  4985. * @param DevAddress Target device address: The device 7 bits address value
  4986. * in datasheet must be shifted to the left before calling the interface
  4987. * @param MemAddress Internal memory address
  4988. * @param MemAddSize Size of internal memory address
  4989. * @param Timeout Timeout duration
  4990. * @param Tickstart Tick start value
  4991. * @retval HAL status
  4992. */
  4993. static HAL_StatusTypeDef I2C_RequestMemoryWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress,
  4994. uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout,
  4995. uint32_t Tickstart)
  4996. {
  4997. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)MemAddSize, I2C_RELOAD_MODE, I2C_GENERATE_START_WRITE);
  4998. /* Wait until TXIS flag is set */
  4999. if (I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
  5000. {
  5001. return HAL_ERROR;
  5002. }
  5003. /* If Memory address size is 8Bit */
  5004. if (MemAddSize == I2C_MEMADD_SIZE_8BIT)
  5005. {
  5006. /* Send Memory Address */
  5007. hi2c->Instance->TXDR = I2C_MEM_ADD_LSB(MemAddress);
  5008. }
  5009. /* If Memory address size is 16Bit */
  5010. else
  5011. {
  5012. /* Send MSB of Memory Address */
  5013. hi2c->Instance->TXDR = I2C_MEM_ADD_MSB(MemAddress);
  5014. /* Wait until TXIS flag is set */
  5015. if (I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
  5016. {
  5017. return HAL_ERROR;
  5018. }
  5019. /* Send LSB of Memory Address */
  5020. hi2c->Instance->TXDR = I2C_MEM_ADD_LSB(MemAddress);
  5021. }
  5022. /* Wait until TCR flag is set */
  5023. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout, Tickstart) != HAL_OK)
  5024. {
  5025. return HAL_ERROR;
  5026. }
  5027. return HAL_OK;
  5028. }
  5029. /**
  5030. * @brief Master sends target device address followed by internal memory address for read request.
  5031. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  5032. * the configuration information for the specified I2C.
  5033. * @param DevAddress Target device address: The device 7 bits address value
  5034. * in datasheet must be shifted to the left before calling the interface
  5035. * @param MemAddress Internal memory address
  5036. * @param MemAddSize Size of internal memory address
  5037. * @param Timeout Timeout duration
  5038. * @param Tickstart Tick start value
  5039. * @retval HAL status
  5040. */
  5041. static HAL_StatusTypeDef I2C_RequestMemoryRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress,
  5042. uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout,
  5043. uint32_t Tickstart)
  5044. {
  5045. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)MemAddSize, I2C_SOFTEND_MODE, I2C_GENERATE_START_WRITE);
  5046. /* Wait until TXIS flag is set */
  5047. if (I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
  5048. {
  5049. return HAL_ERROR;
  5050. }
  5051. /* If Memory address size is 8Bit */
  5052. if (MemAddSize == I2C_MEMADD_SIZE_8BIT)
  5053. {
  5054. /* Send Memory Address */
  5055. hi2c->Instance->TXDR = I2C_MEM_ADD_LSB(MemAddress);
  5056. }
  5057. /* If Memory address size is 16Bit */
  5058. else
  5059. {
  5060. /* Send MSB of Memory Address */
  5061. hi2c->Instance->TXDR = I2C_MEM_ADD_MSB(MemAddress);
  5062. /* Wait until TXIS flag is set */
  5063. if (I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
  5064. {
  5065. return HAL_ERROR;
  5066. }
  5067. /* Send LSB of Memory Address */
  5068. hi2c->Instance->TXDR = I2C_MEM_ADD_LSB(MemAddress);
  5069. }
  5070. /* Wait until TC flag is set */
  5071. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TC, RESET, Timeout, Tickstart) != HAL_OK)
  5072. {
  5073. return HAL_ERROR;
  5074. }
  5075. return HAL_OK;
  5076. }
  5077. /**
  5078. * @brief I2C Address complete process callback.
  5079. * @param hi2c I2C handle.
  5080. * @param ITFlags Interrupt flags to handle.
  5081. * @retval None
  5082. */
  5083. static void I2C_ITAddrCplt(I2C_HandleTypeDef *hi2c, uint32_t ITFlags)
  5084. {
  5085. uint8_t transferdirection;
  5086. uint16_t slaveaddrcode;
  5087. uint16_t ownadd1code;
  5088. uint16_t ownadd2code;
  5089. /* Prevent unused argument(s) compilation warning */
  5090. UNUSED(ITFlags);
  5091. /* In case of Listen state, need to inform upper layer of address match code event */
  5092. if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
  5093. {
  5094. transferdirection = I2C_GET_DIR(hi2c);
  5095. slaveaddrcode = I2C_GET_ADDR_MATCH(hi2c);
  5096. ownadd1code = I2C_GET_OWN_ADDRESS1(hi2c);
  5097. ownadd2code = I2C_GET_OWN_ADDRESS2(hi2c);
  5098. /* If 10bits addressing mode is selected */
  5099. if (hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT)
  5100. {
  5101. if ((slaveaddrcode & SLAVE_ADDR_MSK) == ((ownadd1code >> SLAVE_ADDR_SHIFT) & SLAVE_ADDR_MSK))
  5102. {
  5103. slaveaddrcode = ownadd1code;
  5104. hi2c->AddrEventCount++;
  5105. if (hi2c->AddrEventCount == 2U)
  5106. {
  5107. /* Reset Address Event counter */
  5108. hi2c->AddrEventCount = 0U;
  5109. /* Clear ADDR flag */
  5110. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
  5111. /* Process Unlocked */
  5112. __HAL_UNLOCK(hi2c);
  5113. /* Call Slave Addr callback */
  5114. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5115. hi2c->AddrCallback(hi2c, transferdirection, slaveaddrcode);
  5116. #else
  5117. HAL_I2C_AddrCallback(hi2c, transferdirection, slaveaddrcode);
  5118. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5119. }
  5120. }
  5121. else
  5122. {
  5123. slaveaddrcode = ownadd2code;
  5124. /* Disable ADDR Interrupts */
  5125. I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT);
  5126. /* Process Unlocked */
  5127. __HAL_UNLOCK(hi2c);
  5128. /* Call Slave Addr callback */
  5129. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5130. hi2c->AddrCallback(hi2c, transferdirection, slaveaddrcode);
  5131. #else
  5132. HAL_I2C_AddrCallback(hi2c, transferdirection, slaveaddrcode);
  5133. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5134. }
  5135. }
  5136. /* else 7 bits addressing mode is selected */
  5137. else
  5138. {
  5139. /* Disable ADDR Interrupts */
  5140. I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT);
  5141. /* Process Unlocked */
  5142. __HAL_UNLOCK(hi2c);
  5143. /* Call Slave Addr callback */
  5144. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5145. hi2c->AddrCallback(hi2c, transferdirection, slaveaddrcode);
  5146. #else
  5147. HAL_I2C_AddrCallback(hi2c, transferdirection, slaveaddrcode);
  5148. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5149. }
  5150. }
  5151. /* Else clear address flag only */
  5152. else
  5153. {
  5154. /* Clear ADDR flag */
  5155. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
  5156. /* Process Unlocked */
  5157. __HAL_UNLOCK(hi2c);
  5158. }
  5159. }
  5160. /**
  5161. * @brief I2C Master sequential complete process.
  5162. * @param hi2c I2C handle.
  5163. * @retval None
  5164. */
  5165. static void I2C_ITMasterSeqCplt(I2C_HandleTypeDef *hi2c)
  5166. {
  5167. /* Reset I2C handle mode */
  5168. hi2c->Mode = HAL_I2C_MODE_NONE;
  5169. /* No Generate Stop, to permit restart mode */
  5170. /* The stop will be done at the end of transfer, when I2C_AUTOEND_MODE enable */
  5171. if (hi2c->State == HAL_I2C_STATE_BUSY_TX)
  5172. {
  5173. hi2c->State = HAL_I2C_STATE_READY;
  5174. hi2c->PreviousState = I2C_STATE_MASTER_BUSY_TX;
  5175. hi2c->XferISR = NULL;
  5176. /* Disable Interrupts */
  5177. I2C_Disable_IRQ(hi2c, I2C_XFER_TX_IT);
  5178. /* Process Unlocked */
  5179. __HAL_UNLOCK(hi2c);
  5180. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5181. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5182. hi2c->MasterTxCpltCallback(hi2c);
  5183. #else
  5184. HAL_I2C_MasterTxCpltCallback(hi2c);
  5185. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5186. }
  5187. /* hi2c->State == HAL_I2C_STATE_BUSY_RX */
  5188. else
  5189. {
  5190. hi2c->State = HAL_I2C_STATE_READY;
  5191. hi2c->PreviousState = I2C_STATE_MASTER_BUSY_RX;
  5192. hi2c->XferISR = NULL;
  5193. /* Disable Interrupts */
  5194. I2C_Disable_IRQ(hi2c, I2C_XFER_RX_IT);
  5195. /* Process Unlocked */
  5196. __HAL_UNLOCK(hi2c);
  5197. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5198. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5199. hi2c->MasterRxCpltCallback(hi2c);
  5200. #else
  5201. HAL_I2C_MasterRxCpltCallback(hi2c);
  5202. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5203. }
  5204. }
  5205. /**
  5206. * @brief I2C Slave sequential complete process.
  5207. * @param hi2c I2C handle.
  5208. * @retval None
  5209. */
  5210. static void I2C_ITSlaveSeqCplt(I2C_HandleTypeDef *hi2c)
  5211. {
  5212. uint32_t tmpcr1value = READ_REG(hi2c->Instance->CR1);
  5213. /* Reset I2C handle mode */
  5214. hi2c->Mode = HAL_I2C_MODE_NONE;
  5215. /* If a DMA is ongoing, Update handle size context */
  5216. if (I2C_CHECK_IT_SOURCE(tmpcr1value, I2C_CR1_TXDMAEN) != RESET)
  5217. {
  5218. /* Disable DMA Request */
  5219. hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
  5220. }
  5221. else if (I2C_CHECK_IT_SOURCE(tmpcr1value, I2C_CR1_RXDMAEN) != RESET)
  5222. {
  5223. /* Disable DMA Request */
  5224. hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
  5225. }
  5226. else
  5227. {
  5228. /* Do nothing */
  5229. }
  5230. if (hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN)
  5231. {
  5232. /* Remove HAL_I2C_STATE_SLAVE_BUSY_TX, keep only HAL_I2C_STATE_LISTEN */
  5233. hi2c->State = HAL_I2C_STATE_LISTEN;
  5234. hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_TX;
  5235. /* Disable Interrupts */
  5236. I2C_Disable_IRQ(hi2c, I2C_XFER_TX_IT);
  5237. /* Process Unlocked */
  5238. __HAL_UNLOCK(hi2c);
  5239. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5240. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5241. hi2c->SlaveTxCpltCallback(hi2c);
  5242. #else
  5243. HAL_I2C_SlaveTxCpltCallback(hi2c);
  5244. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5245. }
  5246. else if (hi2c->State == HAL_I2C_STATE_BUSY_RX_LISTEN)
  5247. {
  5248. /* Remove HAL_I2C_STATE_SLAVE_BUSY_RX, keep only HAL_I2C_STATE_LISTEN */
  5249. hi2c->State = HAL_I2C_STATE_LISTEN;
  5250. hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_RX;
  5251. /* Disable Interrupts */
  5252. I2C_Disable_IRQ(hi2c, I2C_XFER_RX_IT);
  5253. /* Process Unlocked */
  5254. __HAL_UNLOCK(hi2c);
  5255. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5256. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5257. hi2c->SlaveRxCpltCallback(hi2c);
  5258. #else
  5259. HAL_I2C_SlaveRxCpltCallback(hi2c);
  5260. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5261. }
  5262. else
  5263. {
  5264. /* Nothing to do */
  5265. }
  5266. }
  5267. /**
  5268. * @brief I2C Master complete process.
  5269. * @param hi2c I2C handle.
  5270. * @param ITFlags Interrupt flags to handle.
  5271. * @retval None
  5272. */
  5273. static void I2C_ITMasterCplt(I2C_HandleTypeDef *hi2c, uint32_t ITFlags)
  5274. {
  5275. uint32_t tmperror;
  5276. uint32_t tmpITFlags = ITFlags;
  5277. __IO uint32_t tmpreg;
  5278. /* Clear STOP Flag */
  5279. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  5280. /* Disable Interrupts and Store Previous state */
  5281. if (hi2c->State == HAL_I2C_STATE_BUSY_TX)
  5282. {
  5283. I2C_Disable_IRQ(hi2c, I2C_XFER_TX_IT);
  5284. hi2c->PreviousState = I2C_STATE_MASTER_BUSY_TX;
  5285. }
  5286. else if (hi2c->State == HAL_I2C_STATE_BUSY_RX)
  5287. {
  5288. I2C_Disable_IRQ(hi2c, I2C_XFER_RX_IT);
  5289. hi2c->PreviousState = I2C_STATE_MASTER_BUSY_RX;
  5290. }
  5291. else
  5292. {
  5293. /* Do nothing */
  5294. }
  5295. /* Clear Configuration Register 2 */
  5296. I2C_RESET_CR2(hi2c);
  5297. /* Reset handle parameters */
  5298. hi2c->XferISR = NULL;
  5299. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  5300. if (I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_AF) != RESET)
  5301. {
  5302. /* Clear NACK Flag */
  5303. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  5304. /* Set acknowledge error code */
  5305. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  5306. }
  5307. /* Fetch Last receive data if any */
  5308. if ((hi2c->State == HAL_I2C_STATE_ABORT) && (I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_RXNE) != RESET))
  5309. {
  5310. /* Read data from RXDR */
  5311. tmpreg = (uint8_t)hi2c->Instance->RXDR;
  5312. UNUSED(tmpreg);
  5313. }
  5314. /* Flush TX register */
  5315. I2C_Flush_TXDR(hi2c);
  5316. /* Store current volatile hi2c->ErrorCode, misra rule */
  5317. tmperror = hi2c->ErrorCode;
  5318. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5319. if ((hi2c->State == HAL_I2C_STATE_ABORT) || (tmperror != HAL_I2C_ERROR_NONE))
  5320. {
  5321. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5322. I2C_ITError(hi2c, hi2c->ErrorCode);
  5323. }
  5324. /* hi2c->State == HAL_I2C_STATE_BUSY_TX */
  5325. else if (hi2c->State == HAL_I2C_STATE_BUSY_TX)
  5326. {
  5327. hi2c->State = HAL_I2C_STATE_READY;
  5328. hi2c->PreviousState = I2C_STATE_NONE;
  5329. if (hi2c->Mode == HAL_I2C_MODE_MEM)
  5330. {
  5331. hi2c->Mode = HAL_I2C_MODE_NONE;
  5332. /* Process Unlocked */
  5333. __HAL_UNLOCK(hi2c);
  5334. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5335. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5336. hi2c->MemTxCpltCallback(hi2c);
  5337. #else
  5338. HAL_I2C_MemTxCpltCallback(hi2c);
  5339. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5340. }
  5341. else
  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->MasterTxCpltCallback(hi2c);
  5349. #else
  5350. HAL_I2C_MasterTxCpltCallback(hi2c);
  5351. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5352. }
  5353. }
  5354. /* hi2c->State == HAL_I2C_STATE_BUSY_RX */
  5355. else if (hi2c->State == HAL_I2C_STATE_BUSY_RX)
  5356. {
  5357. hi2c->State = HAL_I2C_STATE_READY;
  5358. hi2c->PreviousState = I2C_STATE_NONE;
  5359. if (hi2c->Mode == HAL_I2C_MODE_MEM)
  5360. {
  5361. hi2c->Mode = HAL_I2C_MODE_NONE;
  5362. /* Process Unlocked */
  5363. __HAL_UNLOCK(hi2c);
  5364. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5365. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5366. hi2c->MemRxCpltCallback(hi2c);
  5367. #else
  5368. HAL_I2C_MemRxCpltCallback(hi2c);
  5369. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5370. }
  5371. else
  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->MasterRxCpltCallback(hi2c);
  5379. #else
  5380. HAL_I2C_MasterRxCpltCallback(hi2c);
  5381. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5382. }
  5383. }
  5384. else
  5385. {
  5386. /* Nothing to do */
  5387. }
  5388. }
  5389. /**
  5390. * @brief I2C Slave complete process.
  5391. * @param hi2c I2C handle.
  5392. * @param ITFlags Interrupt flags to handle.
  5393. * @retval None
  5394. */
  5395. static void I2C_ITSlaveCplt(I2C_HandleTypeDef *hi2c, uint32_t ITFlags)
  5396. {
  5397. uint32_t tmpcr1value = READ_REG(hi2c->Instance->CR1);
  5398. uint32_t tmpITFlags = ITFlags;
  5399. uint32_t tmpoptions = hi2c->XferOptions;
  5400. HAL_I2C_StateTypeDef tmpstate = hi2c->State;
  5401. /* Clear STOP Flag */
  5402. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  5403. /* Disable Interrupts and Store Previous state */
  5404. if ((tmpstate == HAL_I2C_STATE_BUSY_TX) || (tmpstate == HAL_I2C_STATE_BUSY_TX_LISTEN))
  5405. {
  5406. I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_TX_IT);
  5407. hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_TX;
  5408. }
  5409. else if ((tmpstate == HAL_I2C_STATE_BUSY_RX) || (tmpstate == HAL_I2C_STATE_BUSY_RX_LISTEN))
  5410. {
  5411. I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_RX_IT);
  5412. hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_RX;
  5413. }
  5414. else if (tmpstate == HAL_I2C_STATE_LISTEN)
  5415. {
  5416. I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_TX_IT | I2C_XFER_RX_IT);
  5417. hi2c->PreviousState = I2C_STATE_NONE;
  5418. }
  5419. else
  5420. {
  5421. /* Do nothing */
  5422. }
  5423. /* Disable Address Acknowledge */
  5424. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  5425. /* Clear Configuration Register 2 */
  5426. I2C_RESET_CR2(hi2c);
  5427. /* Flush TX register */
  5428. I2C_Flush_TXDR(hi2c);
  5429. /* If a DMA is ongoing, Update handle size context */
  5430. if (I2C_CHECK_IT_SOURCE(tmpcr1value, I2C_CR1_TXDMAEN) != RESET)
  5431. {
  5432. /* Disable DMA Request */
  5433. hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
  5434. if (hi2c->hdmatx != NULL)
  5435. {
  5436. hi2c->XferCount = (uint16_t)I2C_GET_DMA_REMAIN_DATA(hi2c->hdmatx);
  5437. }
  5438. }
  5439. else if (I2C_CHECK_IT_SOURCE(tmpcr1value, I2C_CR1_RXDMAEN) != RESET)
  5440. {
  5441. /* Disable DMA Request */
  5442. hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
  5443. if (hi2c->hdmarx != NULL)
  5444. {
  5445. hi2c->XferCount = (uint16_t)I2C_GET_DMA_REMAIN_DATA(hi2c->hdmarx);
  5446. }
  5447. }
  5448. else
  5449. {
  5450. /* Do nothing */
  5451. }
  5452. /* Store Last receive data if any */
  5453. if (I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_RXNE) != RESET)
  5454. {
  5455. /* Remove RXNE flag on temporary variable as read done */
  5456. tmpITFlags &= ~I2C_FLAG_RXNE;
  5457. /* Read data from RXDR */
  5458. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->RXDR;
  5459. /* Increment Buffer pointer */
  5460. hi2c->pBuffPtr++;
  5461. if ((hi2c->XferSize > 0U))
  5462. {
  5463. hi2c->XferSize--;
  5464. hi2c->XferCount--;
  5465. }
  5466. }
  5467. /* All data are not transferred, so set error code accordingly */
  5468. if (hi2c->XferCount != 0U)
  5469. {
  5470. /* Set ErrorCode corresponding to a Non-Acknowledge */
  5471. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  5472. }
  5473. if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_AF) != RESET) && \
  5474. (I2C_CHECK_IT_SOURCE(tmpcr1value, I2C_IT_NACKI) != RESET))
  5475. {
  5476. /* Check that I2C transfer finished */
  5477. /* if yes, normal use case, a NACK is sent by the MASTER when Transfer is finished */
  5478. /* Mean XferCount == 0*/
  5479. /* So clear Flag NACKF only */
  5480. if (hi2c->XferCount == 0U)
  5481. {
  5482. if ((hi2c->State == HAL_I2C_STATE_LISTEN) && (tmpoptions == I2C_FIRST_AND_LAST_FRAME))
  5483. /* Same action must be done for (tmpoptions == I2C_LAST_FRAME) which removed for
  5484. Warning[Pa134]: left and right operands are identical */
  5485. {
  5486. /* Call I2C Listen complete process */
  5487. I2C_ITListenCplt(hi2c, tmpITFlags);
  5488. }
  5489. else if ((hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN) && (tmpoptions != I2C_NO_OPTION_FRAME))
  5490. {
  5491. /* Clear NACK Flag */
  5492. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  5493. /* Flush TX register */
  5494. I2C_Flush_TXDR(hi2c);
  5495. /* Last Byte is Transmitted */
  5496. /* Call I2C Slave Sequential complete process */
  5497. I2C_ITSlaveSeqCplt(hi2c);
  5498. }
  5499. else
  5500. {
  5501. /* Clear NACK Flag */
  5502. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  5503. }
  5504. }
  5505. else
  5506. {
  5507. /* if no, error use case, a Non-Acknowledge of last Data is generated by the MASTER*/
  5508. /* Clear NACK Flag */
  5509. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  5510. /* Set ErrorCode corresponding to a Non-Acknowledge */
  5511. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  5512. if ((tmpoptions == I2C_FIRST_FRAME) || (tmpoptions == I2C_NEXT_FRAME))
  5513. {
  5514. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5515. I2C_ITError(hi2c, hi2c->ErrorCode);
  5516. }
  5517. }
  5518. }
  5519. hi2c->Mode = HAL_I2C_MODE_NONE;
  5520. hi2c->XferISR = NULL;
  5521. if (hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  5522. {
  5523. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5524. I2C_ITError(hi2c, hi2c->ErrorCode);
  5525. /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */
  5526. if (hi2c->State == HAL_I2C_STATE_LISTEN)
  5527. {
  5528. /* Call I2C Listen complete process */
  5529. I2C_ITListenCplt(hi2c, tmpITFlags);
  5530. }
  5531. }
  5532. else if (hi2c->XferOptions != I2C_NO_OPTION_FRAME)
  5533. {
  5534. /* Call the Sequential Complete callback, to inform upper layer of the end of Transfer */
  5535. I2C_ITSlaveSeqCplt(hi2c);
  5536. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  5537. hi2c->State = HAL_I2C_STATE_READY;
  5538. hi2c->PreviousState = I2C_STATE_NONE;
  5539. /* Process Unlocked */
  5540. __HAL_UNLOCK(hi2c);
  5541. /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */
  5542. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5543. hi2c->ListenCpltCallback(hi2c);
  5544. #else
  5545. HAL_I2C_ListenCpltCallback(hi2c);
  5546. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5547. }
  5548. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5549. else if (hi2c->State == HAL_I2C_STATE_BUSY_RX)
  5550. {
  5551. hi2c->State = HAL_I2C_STATE_READY;
  5552. hi2c->PreviousState = I2C_STATE_NONE;
  5553. /* Process Unlocked */
  5554. __HAL_UNLOCK(hi2c);
  5555. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5556. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5557. hi2c->SlaveRxCpltCallback(hi2c);
  5558. #else
  5559. HAL_I2C_SlaveRxCpltCallback(hi2c);
  5560. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5561. }
  5562. else
  5563. {
  5564. hi2c->State = HAL_I2C_STATE_READY;
  5565. hi2c->PreviousState = I2C_STATE_NONE;
  5566. /* Process Unlocked */
  5567. __HAL_UNLOCK(hi2c);
  5568. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5569. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5570. hi2c->SlaveTxCpltCallback(hi2c);
  5571. #else
  5572. HAL_I2C_SlaveTxCpltCallback(hi2c);
  5573. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5574. }
  5575. }
  5576. /**
  5577. * @brief I2C Listen complete process.
  5578. * @param hi2c I2C handle.
  5579. * @param ITFlags Interrupt flags to handle.
  5580. * @retval None
  5581. */
  5582. static void I2C_ITListenCplt(I2C_HandleTypeDef *hi2c, uint32_t ITFlags)
  5583. {
  5584. /* Reset handle parameters */
  5585. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  5586. hi2c->PreviousState = I2C_STATE_NONE;
  5587. hi2c->State = HAL_I2C_STATE_READY;
  5588. hi2c->Mode = HAL_I2C_MODE_NONE;
  5589. hi2c->XferISR = NULL;
  5590. /* Store Last receive data if any */
  5591. if (I2C_CHECK_FLAG(ITFlags, I2C_FLAG_RXNE) != RESET)
  5592. {
  5593. /* Read data from RXDR */
  5594. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->RXDR;
  5595. /* Increment Buffer pointer */
  5596. hi2c->pBuffPtr++;
  5597. if ((hi2c->XferSize > 0U))
  5598. {
  5599. hi2c->XferSize--;
  5600. hi2c->XferCount--;
  5601. /* Set ErrorCode corresponding to a Non-Acknowledge */
  5602. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  5603. }
  5604. }
  5605. /* Disable all Interrupts*/
  5606. I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_RX_IT | I2C_XFER_TX_IT);
  5607. /* Clear NACK Flag */
  5608. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  5609. /* Process Unlocked */
  5610. __HAL_UNLOCK(hi2c);
  5611. /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */
  5612. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5613. hi2c->ListenCpltCallback(hi2c);
  5614. #else
  5615. HAL_I2C_ListenCpltCallback(hi2c);
  5616. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5617. }
  5618. /**
  5619. * @brief I2C interrupts error process.
  5620. * @param hi2c I2C handle.
  5621. * @param ErrorCode Error code to handle.
  5622. * @retval None
  5623. */
  5624. static void I2C_ITError(I2C_HandleTypeDef *hi2c, uint32_t ErrorCode)
  5625. {
  5626. HAL_I2C_StateTypeDef tmpstate = hi2c->State;
  5627. uint32_t tmppreviousstate;
  5628. /* Reset handle parameters */
  5629. hi2c->Mode = HAL_I2C_MODE_NONE;
  5630. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  5631. hi2c->XferCount = 0U;
  5632. /* Set new error code */
  5633. hi2c->ErrorCode |= ErrorCode;
  5634. /* Disable Interrupts */
  5635. if ((tmpstate == HAL_I2C_STATE_LISTEN) ||
  5636. (tmpstate == HAL_I2C_STATE_BUSY_TX_LISTEN) ||
  5637. (tmpstate == HAL_I2C_STATE_BUSY_RX_LISTEN))
  5638. {
  5639. /* Disable all interrupts, except interrupts related to LISTEN state */
  5640. I2C_Disable_IRQ(hi2c, I2C_XFER_RX_IT | I2C_XFER_TX_IT);
  5641. /* keep HAL_I2C_STATE_LISTEN if set */
  5642. hi2c->State = HAL_I2C_STATE_LISTEN;
  5643. hi2c->XferISR = I2C_Slave_ISR_IT;
  5644. }
  5645. else
  5646. {
  5647. /* Disable all interrupts */
  5648. I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_RX_IT | I2C_XFER_TX_IT);
  5649. /* Flush TX register */
  5650. I2C_Flush_TXDR(hi2c);
  5651. /* If state is an abort treatment on going, don't change state */
  5652. /* This change will be do later */
  5653. if (hi2c->State != HAL_I2C_STATE_ABORT)
  5654. {
  5655. /* Set HAL_I2C_STATE_READY */
  5656. hi2c->State = HAL_I2C_STATE_READY;
  5657. /* Check if a STOPF is detected */
  5658. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET)
  5659. {
  5660. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
  5661. {
  5662. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  5663. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  5664. }
  5665. /* Clear STOP Flag */
  5666. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  5667. }
  5668. }
  5669. hi2c->XferISR = NULL;
  5670. }
  5671. /* Abort DMA TX transfer if any */
  5672. tmppreviousstate = hi2c->PreviousState;
  5673. if ((hi2c->hdmatx != NULL) && ((tmppreviousstate == I2C_STATE_MASTER_BUSY_TX) || \
  5674. (tmppreviousstate == I2C_STATE_SLAVE_BUSY_TX)))
  5675. {
  5676. if ((hi2c->Instance->CR1 & I2C_CR1_TXDMAEN) == I2C_CR1_TXDMAEN)
  5677. {
  5678. hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
  5679. }
  5680. if (HAL_DMA_GetState(hi2c->hdmatx) != HAL_DMA_STATE_READY)
  5681. {
  5682. /* Set the I2C DMA Abort callback :
  5683. will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
  5684. hi2c->hdmatx->XferAbortCallback = I2C_DMAAbort;
  5685. /* Process Unlocked */
  5686. __HAL_UNLOCK(hi2c);
  5687. /* Abort DMA TX */
  5688. if (HAL_DMA_Abort_IT(hi2c->hdmatx) != HAL_OK)
  5689. {
  5690. /* Call Directly XferAbortCallback function in case of error */
  5691. hi2c->hdmatx->XferAbortCallback(hi2c->hdmatx);
  5692. }
  5693. }
  5694. else
  5695. {
  5696. I2C_TreatErrorCallback(hi2c);
  5697. }
  5698. }
  5699. /* Abort DMA RX transfer if any */
  5700. else if ((hi2c->hdmarx != NULL) && ((tmppreviousstate == I2C_STATE_MASTER_BUSY_RX) || \
  5701. (tmppreviousstate == I2C_STATE_SLAVE_BUSY_RX)))
  5702. {
  5703. if ((hi2c->Instance->CR1 & I2C_CR1_RXDMAEN) == I2C_CR1_RXDMAEN)
  5704. {
  5705. hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
  5706. }
  5707. if (HAL_DMA_GetState(hi2c->hdmarx) != HAL_DMA_STATE_READY)
  5708. {
  5709. /* Set the I2C DMA Abort callback :
  5710. will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
  5711. hi2c->hdmarx->XferAbortCallback = I2C_DMAAbort;
  5712. /* Process Unlocked */
  5713. __HAL_UNLOCK(hi2c);
  5714. /* Abort DMA RX */
  5715. if (HAL_DMA_Abort_IT(hi2c->hdmarx) != HAL_OK)
  5716. {
  5717. /* Call Directly hi2c->hdmarx->XferAbortCallback function in case of error */
  5718. hi2c->hdmarx->XferAbortCallback(hi2c->hdmarx);
  5719. }
  5720. }
  5721. else
  5722. {
  5723. I2C_TreatErrorCallback(hi2c);
  5724. }
  5725. }
  5726. else
  5727. {
  5728. I2C_TreatErrorCallback(hi2c);
  5729. }
  5730. }
  5731. /**
  5732. * @brief I2C Error callback treatment.
  5733. * @param hi2c I2C handle.
  5734. * @retval None
  5735. */
  5736. static void I2C_TreatErrorCallback(I2C_HandleTypeDef *hi2c)
  5737. {
  5738. if (hi2c->State == HAL_I2C_STATE_ABORT)
  5739. {
  5740. hi2c->State = HAL_I2C_STATE_READY;
  5741. hi2c->PreviousState = I2C_STATE_NONE;
  5742. /* Process Unlocked */
  5743. __HAL_UNLOCK(hi2c);
  5744. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5745. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5746. hi2c->AbortCpltCallback(hi2c);
  5747. #else
  5748. HAL_I2C_AbortCpltCallback(hi2c);
  5749. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5750. }
  5751. else
  5752. {
  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->ErrorCallback(hi2c);
  5759. #else
  5760. HAL_I2C_ErrorCallback(hi2c);
  5761. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5762. }
  5763. }
  5764. /**
  5765. * @brief I2C Tx data register flush process.
  5766. * @param hi2c I2C handle.
  5767. * @retval None
  5768. */
  5769. static void I2C_Flush_TXDR(I2C_HandleTypeDef *hi2c)
  5770. {
  5771. /* If a pending TXIS flag is set */
  5772. /* Write a dummy data in TXDR to clear it */
  5773. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXIS) != RESET)
  5774. {
  5775. hi2c->Instance->TXDR = 0x00U;
  5776. }
  5777. /* Flush TX register if not empty */
  5778. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXE) == RESET)
  5779. {
  5780. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_TXE);
  5781. }
  5782. }
  5783. /**
  5784. * @brief DMA I2C master transmit process complete callback.
  5785. * @param hdma DMA handle
  5786. * @retval None
  5787. */
  5788. static void I2C_DMAMasterTransmitCplt(DMA_HandleTypeDef *hdma)
  5789. {
  5790. /* Derogation MISRAC2012-Rule-11.5 */
  5791. I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent);
  5792. /* Disable DMA Request */
  5793. hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
  5794. /* If last transfer, enable STOP interrupt */
  5795. if (hi2c->XferCount == 0U)
  5796. {
  5797. /* Enable STOP interrupt */
  5798. I2C_Enable_IRQ(hi2c, I2C_XFER_CPLT_IT);
  5799. }
  5800. /* else prepare a new DMA transfer and enable TCReload interrupt */
  5801. else
  5802. {
  5803. /* Update Buffer pointer */
  5804. hi2c->pBuffPtr += hi2c->XferSize;
  5805. /* Set the XferSize to transfer */
  5806. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  5807. {
  5808. hi2c->XferSize = MAX_NBYTE_SIZE;
  5809. }
  5810. else
  5811. {
  5812. hi2c->XferSize = hi2c->XferCount;
  5813. }
  5814. /* Enable the DMA channel */
  5815. if (HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->TXDR,
  5816. hi2c->XferSize) != HAL_OK)
  5817. {
  5818. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5819. I2C_ITError(hi2c, HAL_I2C_ERROR_DMA);
  5820. }
  5821. else
  5822. {
  5823. /* Enable TC interrupts */
  5824. I2C_Enable_IRQ(hi2c, I2C_XFER_RELOAD_IT);
  5825. }
  5826. }
  5827. }
  5828. /**
  5829. * @brief DMA I2C slave transmit process complete callback.
  5830. * @param hdma DMA handle
  5831. * @retval None
  5832. */
  5833. static void I2C_DMASlaveTransmitCplt(DMA_HandleTypeDef *hdma)
  5834. {
  5835. /* Derogation MISRAC2012-Rule-11.5 */
  5836. I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent);
  5837. uint32_t tmpoptions = hi2c->XferOptions;
  5838. if ((tmpoptions == I2C_NEXT_FRAME) || (tmpoptions == I2C_FIRST_FRAME))
  5839. {
  5840. /* Disable DMA Request */
  5841. hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
  5842. /* Last Byte is Transmitted */
  5843. /* Call I2C Slave Sequential complete process */
  5844. I2C_ITSlaveSeqCplt(hi2c);
  5845. }
  5846. else
  5847. {
  5848. /* No specific action, Master fully manage the generation of STOP condition */
  5849. /* Mean that this generation can arrive at any time, at the end or during DMA process */
  5850. /* So STOP condition should be manage through Interrupt treatment */
  5851. }
  5852. }
  5853. /**
  5854. * @brief DMA I2C master receive process complete callback.
  5855. * @param hdma DMA handle
  5856. * @retval None
  5857. */
  5858. static void I2C_DMAMasterReceiveCplt(DMA_HandleTypeDef *hdma)
  5859. {
  5860. /* Derogation MISRAC2012-Rule-11.5 */
  5861. I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent);
  5862. /* Disable DMA Request */
  5863. hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
  5864. /* If last transfer, enable STOP interrupt */
  5865. if (hi2c->XferCount == 0U)
  5866. {
  5867. /* Enable STOP interrupt */
  5868. I2C_Enable_IRQ(hi2c, I2C_XFER_CPLT_IT);
  5869. }
  5870. /* else prepare a new DMA transfer and enable TCReload interrupt */
  5871. else
  5872. {
  5873. /* Update Buffer pointer */
  5874. hi2c->pBuffPtr += hi2c->XferSize;
  5875. /* Set the XferSize to transfer */
  5876. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  5877. {
  5878. /* Errata workaround 170323 */
  5879. if (I2C_GET_DIR(hi2c) == I2C_DIRECTION_RECEIVE)
  5880. {
  5881. hi2c->XferSize = 1U;
  5882. }
  5883. else
  5884. {
  5885. hi2c->XferSize = MAX_NBYTE_SIZE;
  5886. }
  5887. }
  5888. else
  5889. {
  5890. hi2c->XferSize = hi2c->XferCount;
  5891. }
  5892. /* Enable the DMA channel */
  5893. if (HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)hi2c->pBuffPtr,
  5894. hi2c->XferSize) != HAL_OK)
  5895. {
  5896. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5897. I2C_ITError(hi2c, HAL_I2C_ERROR_DMA);
  5898. }
  5899. else
  5900. {
  5901. /* Enable TC interrupts */
  5902. I2C_Enable_IRQ(hi2c, I2C_XFER_RELOAD_IT);
  5903. }
  5904. }
  5905. }
  5906. /**
  5907. * @brief DMA I2C slave receive process complete callback.
  5908. * @param hdma DMA handle
  5909. * @retval None
  5910. */
  5911. static void I2C_DMASlaveReceiveCplt(DMA_HandleTypeDef *hdma)
  5912. {
  5913. /* Derogation MISRAC2012-Rule-11.5 */
  5914. I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent);
  5915. uint32_t tmpoptions = hi2c->XferOptions;
  5916. if ((I2C_GET_DMA_REMAIN_DATA(hi2c->hdmarx) == 0U) && \
  5917. (tmpoptions != I2C_NO_OPTION_FRAME))
  5918. {
  5919. /* Disable DMA Request */
  5920. hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
  5921. /* Call I2C Slave Sequential complete process */
  5922. I2C_ITSlaveSeqCplt(hi2c);
  5923. }
  5924. else
  5925. {
  5926. /* No specific action, Master fully manage the generation of STOP condition */
  5927. /* Mean that this generation can arrive at any time, at the end or during DMA process */
  5928. /* So STOP condition should be manage through Interrupt treatment */
  5929. }
  5930. }
  5931. /**
  5932. * @brief DMA I2C communication error callback.
  5933. * @param hdma DMA handle
  5934. * @retval None
  5935. */
  5936. static void I2C_DMAError(DMA_HandleTypeDef *hdma)
  5937. {
  5938. /* Derogation MISRAC2012-Rule-11.5 */
  5939. I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent);
  5940. /* Disable Acknowledge */
  5941. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  5942. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5943. I2C_ITError(hi2c, HAL_I2C_ERROR_DMA);
  5944. }
  5945. /**
  5946. * @brief DMA I2C communication abort callback
  5947. * (To be called at end of DMA Abort procedure).
  5948. * @param hdma DMA handle.
  5949. * @retval None
  5950. */
  5951. static void I2C_DMAAbort(DMA_HandleTypeDef *hdma)
  5952. {
  5953. /* Derogation MISRAC2012-Rule-11.5 */
  5954. I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent);
  5955. /* Reset AbortCpltCallback */
  5956. if (hi2c->hdmatx != NULL)
  5957. {
  5958. hi2c->hdmatx->XferAbortCallback = NULL;
  5959. }
  5960. if (hi2c->hdmarx != NULL)
  5961. {
  5962. hi2c->hdmarx->XferAbortCallback = NULL;
  5963. }
  5964. I2C_TreatErrorCallback(hi2c);
  5965. }
  5966. /**
  5967. * @brief This function handles I2C Communication Timeout. It waits
  5968. * until a flag is no longer in the specified status.
  5969. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  5970. * the configuration information for the specified I2C.
  5971. * @param Flag Specifies the I2C flag to check.
  5972. * @param Status The actual Flag status (SET or RESET).
  5973. * @param Timeout Timeout duration
  5974. * @param Tickstart Tick start value
  5975. * @retval HAL status
  5976. */
  5977. static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status,
  5978. uint32_t Timeout, uint32_t Tickstart)
  5979. {
  5980. while (__HAL_I2C_GET_FLAG(hi2c, Flag) == Status)
  5981. {
  5982. /* Check if an error is detected */
  5983. if (I2C_IsErrorOccurred(hi2c, Timeout, Tickstart) != HAL_OK)
  5984. {
  5985. return HAL_ERROR;
  5986. }
  5987. /* Check for the Timeout */
  5988. if (Timeout != HAL_MAX_DELAY)
  5989. {
  5990. if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
  5991. {
  5992. if ((__HAL_I2C_GET_FLAG(hi2c, Flag) == Status))
  5993. {
  5994. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  5995. hi2c->State = HAL_I2C_STATE_READY;
  5996. hi2c->Mode = HAL_I2C_MODE_NONE;
  5997. /* Process Unlocked */
  5998. __HAL_UNLOCK(hi2c);
  5999. return HAL_ERROR;
  6000. }
  6001. }
  6002. }
  6003. }
  6004. return HAL_OK;
  6005. }
  6006. /**
  6007. * @brief This function handles I2C Communication Timeout for specific usage of TXIS flag.
  6008. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  6009. * the configuration information for the specified I2C.
  6010. * @param Timeout Timeout duration
  6011. * @param Tickstart Tick start value
  6012. * @retval HAL status
  6013. */
  6014. static HAL_StatusTypeDef I2C_WaitOnTXISFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout,
  6015. uint32_t Tickstart)
  6016. {
  6017. while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXIS) == RESET)
  6018. {
  6019. /* Check if an error is detected */
  6020. if (I2C_IsErrorOccurred(hi2c, Timeout, Tickstart) != HAL_OK)
  6021. {
  6022. return HAL_ERROR;
  6023. }
  6024. /* Check for the Timeout */
  6025. if (Timeout != HAL_MAX_DELAY)
  6026. {
  6027. if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
  6028. {
  6029. if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXIS) == RESET))
  6030. {
  6031. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  6032. hi2c->State = HAL_I2C_STATE_READY;
  6033. hi2c->Mode = HAL_I2C_MODE_NONE;
  6034. /* Process Unlocked */
  6035. __HAL_UNLOCK(hi2c);
  6036. return HAL_ERROR;
  6037. }
  6038. }
  6039. }
  6040. }
  6041. return HAL_OK;
  6042. }
  6043. /**
  6044. * @brief This function handles I2C Communication Timeout for specific usage of STOP flag.
  6045. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  6046. * the configuration information for the specified I2C.
  6047. * @param Timeout Timeout duration
  6048. * @param Tickstart Tick start value
  6049. * @retval HAL status
  6050. */
  6051. static HAL_StatusTypeDef I2C_WaitOnSTOPFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout,
  6052. uint32_t Tickstart)
  6053. {
  6054. while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET)
  6055. {
  6056. /* Check if an error is detected */
  6057. if (I2C_IsErrorOccurred(hi2c, Timeout, Tickstart) != HAL_OK)
  6058. {
  6059. return HAL_ERROR;
  6060. }
  6061. /* Check for the Timeout */
  6062. if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
  6063. {
  6064. if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET))
  6065. {
  6066. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  6067. hi2c->State = HAL_I2C_STATE_READY;
  6068. hi2c->Mode = HAL_I2C_MODE_NONE;
  6069. /* Process Unlocked */
  6070. __HAL_UNLOCK(hi2c);
  6071. return HAL_ERROR;
  6072. }
  6073. }
  6074. }
  6075. return HAL_OK;
  6076. }
  6077. /**
  6078. * @brief This function handles I2C Communication Timeout for specific usage of RXNE flag.
  6079. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  6080. * the configuration information for the specified I2C.
  6081. * @param Timeout Timeout duration
  6082. * @param Tickstart Tick start value
  6083. * @retval HAL status
  6084. */
  6085. static HAL_StatusTypeDef I2C_WaitOnRXNEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout,
  6086. uint32_t Tickstart)
  6087. {
  6088. HAL_StatusTypeDef status = HAL_OK;
  6089. while ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == RESET) && (status == HAL_OK))
  6090. {
  6091. /* Check if an error is detected */
  6092. if (I2C_IsErrorOccurred(hi2c, Timeout, Tickstart) != HAL_OK)
  6093. {
  6094. status = HAL_ERROR;
  6095. }
  6096. /* Check if a STOPF is detected */
  6097. if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET) && (status == HAL_OK))
  6098. {
  6099. /* Check if an RXNE is pending */
  6100. /* Store Last receive data if any */
  6101. if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET) && (hi2c->XferSize > 0U))
  6102. {
  6103. /* Return HAL_OK */
  6104. /* The Reading of data from RXDR will be done in caller function */
  6105. status = HAL_OK;
  6106. }
  6107. /* Check a no-acknowledge have been detected */
  6108. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
  6109. {
  6110. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  6111. hi2c->ErrorCode = HAL_I2C_ERROR_AF;
  6112. /* Clear STOP Flag */
  6113. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  6114. /* Clear Configuration Register 2 */
  6115. I2C_RESET_CR2(hi2c);
  6116. hi2c->State = HAL_I2C_STATE_READY;
  6117. hi2c->Mode = HAL_I2C_MODE_NONE;
  6118. /* Process Unlocked */
  6119. __HAL_UNLOCK(hi2c);
  6120. status = HAL_ERROR;
  6121. }
  6122. else
  6123. {
  6124. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  6125. }
  6126. }
  6127. /* Check for the Timeout */
  6128. if ((((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U)) && (status == HAL_OK))
  6129. {
  6130. if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == RESET))
  6131. {
  6132. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  6133. hi2c->State = HAL_I2C_STATE_READY;
  6134. /* Process Unlocked */
  6135. __HAL_UNLOCK(hi2c);
  6136. status = HAL_ERROR;
  6137. }
  6138. }
  6139. }
  6140. return status;
  6141. }
  6142. /**
  6143. * @brief This function handles errors detection during an I2C Communication.
  6144. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  6145. * the configuration information for the specified I2C.
  6146. * @param Timeout Timeout duration
  6147. * @param Tickstart Tick start value
  6148. * @retval HAL status
  6149. */
  6150. static HAL_StatusTypeDef I2C_IsErrorOccurred(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart)
  6151. {
  6152. HAL_StatusTypeDef status = HAL_OK;
  6153. uint32_t itflag = hi2c->Instance->ISR;
  6154. uint32_t error_code = 0;
  6155. uint32_t tickstart = Tickstart;
  6156. uint32_t tmp1;
  6157. HAL_I2C_ModeTypeDef tmp2;
  6158. if (HAL_IS_BIT_SET(itflag, I2C_FLAG_AF))
  6159. {
  6160. /* Clear NACKF Flag */
  6161. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  6162. /* Wait until STOP Flag is set or timeout occurred */
  6163. /* AutoEnd should be initiate after AF */
  6164. while ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET) && (status == HAL_OK))
  6165. {
  6166. /* Check for the Timeout */
  6167. if (Timeout != HAL_MAX_DELAY)
  6168. {
  6169. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  6170. {
  6171. tmp1 = (uint32_t)(hi2c->Instance->CR2 & I2C_CR2_STOP);
  6172. tmp2 = hi2c->Mode;
  6173. /* In case of I2C still busy, try to regenerate a STOP manually */
  6174. if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET) && \
  6175. (tmp1 != I2C_CR2_STOP) && \
  6176. (tmp2 != HAL_I2C_MODE_SLAVE))
  6177. {
  6178. /* Generate Stop */
  6179. hi2c->Instance->CR2 |= I2C_CR2_STOP;
  6180. /* Update Tick with new reference */
  6181. tickstart = HAL_GetTick();
  6182. }
  6183. while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET)
  6184. {
  6185. /* Check for the Timeout */
  6186. if ((HAL_GetTick() - tickstart) > I2C_TIMEOUT_STOPF)
  6187. {
  6188. error_code |= HAL_I2C_ERROR_TIMEOUT;
  6189. status = HAL_ERROR;
  6190. break;
  6191. }
  6192. }
  6193. }
  6194. }
  6195. }
  6196. /* In case STOP Flag is detected, clear it */
  6197. if (status == HAL_OK)
  6198. {
  6199. /* Clear STOP Flag */
  6200. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  6201. }
  6202. error_code |= HAL_I2C_ERROR_AF;
  6203. status = HAL_ERROR;
  6204. }
  6205. /* Refresh Content of Status register */
  6206. itflag = hi2c->Instance->ISR;
  6207. /* Then verify if an additional errors occurs */
  6208. /* Check if a Bus error occurred */
  6209. if (HAL_IS_BIT_SET(itflag, I2C_FLAG_BERR))
  6210. {
  6211. error_code |= HAL_I2C_ERROR_BERR;
  6212. /* Clear BERR flag */
  6213. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_BERR);
  6214. status = HAL_ERROR;
  6215. }
  6216. /* Check if an Over-Run/Under-Run error occurred */
  6217. if (HAL_IS_BIT_SET(itflag, I2C_FLAG_OVR))
  6218. {
  6219. error_code |= HAL_I2C_ERROR_OVR;
  6220. /* Clear OVR flag */
  6221. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_OVR);
  6222. status = HAL_ERROR;
  6223. }
  6224. /* Check if an Arbitration Loss error occurred */
  6225. if (HAL_IS_BIT_SET(itflag, I2C_FLAG_ARLO))
  6226. {
  6227. error_code |= HAL_I2C_ERROR_ARLO;
  6228. /* Clear ARLO flag */
  6229. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ARLO);
  6230. status = HAL_ERROR;
  6231. }
  6232. if (status != HAL_OK)
  6233. {
  6234. /* Flush TX register */
  6235. I2C_Flush_TXDR(hi2c);
  6236. /* Clear Configuration Register 2 */
  6237. I2C_RESET_CR2(hi2c);
  6238. hi2c->ErrorCode |= error_code;
  6239. hi2c->State = HAL_I2C_STATE_READY;
  6240. hi2c->Mode = HAL_I2C_MODE_NONE;
  6241. /* Process Unlocked */
  6242. __HAL_UNLOCK(hi2c);
  6243. }
  6244. return status;
  6245. }
  6246. /**
  6247. * @brief Handles I2Cx communication when starting transfer or during transfer (TC or TCR flag are set).
  6248. * @param hi2c I2C handle.
  6249. * @param DevAddress Specifies the slave address to be programmed.
  6250. * @param Size Specifies the number of bytes to be programmed.
  6251. * This parameter must be a value between 0 and 255.
  6252. * @param Mode New state of the I2C START condition generation.
  6253. * This parameter can be one of the following values:
  6254. * @arg @ref I2C_RELOAD_MODE Enable Reload mode .
  6255. * @arg @ref I2C_AUTOEND_MODE Enable Automatic end mode.
  6256. * @arg @ref I2C_SOFTEND_MODE Enable Software end mode.
  6257. * @param Request New state of the I2C START condition generation.
  6258. * This parameter can be one of the following values:
  6259. * @arg @ref I2C_NO_STARTSTOP Don't Generate stop and start condition.
  6260. * @arg @ref I2C_GENERATE_STOP Generate stop condition (Size should be set to 0).
  6261. * @arg @ref I2C_GENERATE_START_READ Generate Restart for read request.
  6262. * @arg @ref I2C_GENERATE_START_WRITE Generate Restart for write request.
  6263. * @retval None
  6264. */
  6265. static void I2C_TransferConfig(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t Size, uint32_t Mode,
  6266. uint32_t Request)
  6267. {
  6268. /* Check the parameters */
  6269. assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
  6270. assert_param(IS_TRANSFER_MODE(Mode));
  6271. assert_param(IS_TRANSFER_REQUEST(Request));
  6272. /* Declaration of tmp to prevent undefined behavior of volatile usage */
  6273. uint32_t tmp = ((uint32_t)(((uint32_t)DevAddress & I2C_CR2_SADD) | \
  6274. (((uint32_t)Size << I2C_CR2_NBYTES_Pos) & I2C_CR2_NBYTES) | \
  6275. (uint32_t)Mode | (uint32_t)Request) & (~0x80000000U));
  6276. /* update CR2 register */
  6277. MODIFY_REG(hi2c->Instance->CR2, \
  6278. ((I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | \
  6279. (I2C_CR2_RD_WRN & (uint32_t)(Request >> (31U - I2C_CR2_RD_WRN_Pos))) | \
  6280. I2C_CR2_START | I2C_CR2_STOP)), tmp);
  6281. }
  6282. /**
  6283. * @brief Manage the enabling of Interrupts.
  6284. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  6285. * the configuration information for the specified I2C.
  6286. * @param InterruptRequest Value of @ref I2C_Interrupt_configuration_definition.
  6287. * @retval None
  6288. */
  6289. static void I2C_Enable_IRQ(I2C_HandleTypeDef *hi2c, uint16_t InterruptRequest)
  6290. {
  6291. uint32_t tmpisr = 0U;
  6292. if ((hi2c->XferISR != I2C_Master_ISR_DMA) && \
  6293. (hi2c->XferISR != I2C_Slave_ISR_DMA) && \
  6294. (hi2c->XferISR != I2C_Mem_ISR_DMA))
  6295. {
  6296. if ((InterruptRequest & I2C_XFER_LISTEN_IT) == I2C_XFER_LISTEN_IT)
  6297. {
  6298. /* Enable ERR, STOP, NACK and ADDR interrupts */
  6299. tmpisr |= I2C_IT_ADDRI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_ERRI;
  6300. }
  6301. if ((InterruptRequest & I2C_XFER_TX_IT) == I2C_XFER_TX_IT)
  6302. {
  6303. /* Enable ERR, TC, STOP, NACK and TXI interrupts */
  6304. tmpisr |= I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_TXI;
  6305. }
  6306. if ((InterruptRequest & I2C_XFER_RX_IT) == I2C_XFER_RX_IT)
  6307. {
  6308. /* Enable ERR, TC, STOP, NACK and RXI interrupts */
  6309. tmpisr |= I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_RXI;
  6310. }
  6311. if (InterruptRequest == I2C_XFER_ERROR_IT)
  6312. {
  6313. /* Enable ERR and NACK interrupts */
  6314. tmpisr |= I2C_IT_ERRI | I2C_IT_NACKI;
  6315. }
  6316. if (InterruptRequest == I2C_XFER_CPLT_IT)
  6317. {
  6318. /* Enable STOP interrupts */
  6319. tmpisr |= I2C_IT_STOPI;
  6320. }
  6321. }
  6322. else
  6323. {
  6324. if ((InterruptRequest & I2C_XFER_LISTEN_IT) == I2C_XFER_LISTEN_IT)
  6325. {
  6326. /* Enable ERR, STOP, NACK and ADDR interrupts */
  6327. tmpisr |= I2C_IT_ADDRI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_ERRI;
  6328. }
  6329. if ((InterruptRequest & I2C_XFER_TX_IT) == I2C_XFER_TX_IT)
  6330. {
  6331. /* Enable ERR, TC, STOP, NACK and TXI interrupts */
  6332. tmpisr |= I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_TXI;
  6333. }
  6334. if ((InterruptRequest & I2C_XFER_RX_IT) == I2C_XFER_RX_IT)
  6335. {
  6336. /* Enable ERR, TC, STOP, NACK and RXI interrupts */
  6337. tmpisr |= I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_RXI;
  6338. }
  6339. if (InterruptRequest == I2C_XFER_ERROR_IT)
  6340. {
  6341. /* Enable ERR and NACK interrupts */
  6342. tmpisr |= I2C_IT_ERRI | I2C_IT_NACKI;
  6343. }
  6344. if (InterruptRequest == I2C_XFER_CPLT_IT)
  6345. {
  6346. /* Enable STOP interrupts */
  6347. tmpisr |= (I2C_IT_STOPI | I2C_IT_TCI);
  6348. }
  6349. if (InterruptRequest == I2C_XFER_RELOAD_IT)
  6350. {
  6351. /* Enable TC interrupts */
  6352. tmpisr |= I2C_IT_TCI;
  6353. }
  6354. }
  6355. /* Enable interrupts only at the end */
  6356. /* to avoid the risk of I2C interrupt handle execution before */
  6357. /* all interrupts requested done */
  6358. __HAL_I2C_ENABLE_IT(hi2c, tmpisr);
  6359. }
  6360. /**
  6361. * @brief Manage the disabling of Interrupts.
  6362. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  6363. * the configuration information for the specified I2C.
  6364. * @param InterruptRequest Value of @ref I2C_Interrupt_configuration_definition.
  6365. * @retval None
  6366. */
  6367. static void I2C_Disable_IRQ(I2C_HandleTypeDef *hi2c, uint16_t InterruptRequest)
  6368. {
  6369. uint32_t tmpisr = 0U;
  6370. if ((InterruptRequest & I2C_XFER_TX_IT) == I2C_XFER_TX_IT)
  6371. {
  6372. /* Disable TC and TXI interrupts */
  6373. tmpisr |= I2C_IT_TCI | I2C_IT_TXI;
  6374. if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) != (uint32_t)HAL_I2C_STATE_LISTEN)
  6375. {
  6376. /* Disable NACK and STOP interrupts */
  6377. tmpisr |= I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_ERRI;
  6378. }
  6379. }
  6380. if ((InterruptRequest & I2C_XFER_RX_IT) == I2C_XFER_RX_IT)
  6381. {
  6382. /* Disable TC and RXI interrupts */
  6383. tmpisr |= I2C_IT_TCI | I2C_IT_RXI;
  6384. if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) != (uint32_t)HAL_I2C_STATE_LISTEN)
  6385. {
  6386. /* Disable NACK and STOP interrupts */
  6387. tmpisr |= I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_ERRI;
  6388. }
  6389. }
  6390. if ((InterruptRequest & I2C_XFER_LISTEN_IT) == I2C_XFER_LISTEN_IT)
  6391. {
  6392. /* Disable ADDR, NACK and STOP interrupts */
  6393. tmpisr |= I2C_IT_ADDRI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_ERRI;
  6394. }
  6395. if (InterruptRequest == I2C_XFER_ERROR_IT)
  6396. {
  6397. /* Enable ERR and NACK interrupts */
  6398. tmpisr |= I2C_IT_ERRI | I2C_IT_NACKI;
  6399. }
  6400. if (InterruptRequest == I2C_XFER_CPLT_IT)
  6401. {
  6402. /* Enable STOP interrupts */
  6403. tmpisr |= I2C_IT_STOPI;
  6404. }
  6405. if (InterruptRequest == I2C_XFER_RELOAD_IT)
  6406. {
  6407. /* Enable TC interrupts */
  6408. tmpisr |= I2C_IT_TCI;
  6409. }
  6410. /* Disable interrupts only at the end */
  6411. /* to avoid a breaking situation like at "t" time */
  6412. /* all disable interrupts request are not done */
  6413. __HAL_I2C_DISABLE_IT(hi2c, tmpisr);
  6414. }
  6415. /**
  6416. * @brief Convert I2Cx OTHER_xxx XferOptions to functional XferOptions.
  6417. * @param hi2c I2C handle.
  6418. * @retval None
  6419. */
  6420. static void I2C_ConvertOtherXferOptions(I2C_HandleTypeDef *hi2c)
  6421. {
  6422. /* if user set XferOptions to I2C_OTHER_FRAME */
  6423. /* it request implicitly to generate a restart condition */
  6424. /* set XferOptions to I2C_FIRST_FRAME */
  6425. if (hi2c->XferOptions == I2C_OTHER_FRAME)
  6426. {
  6427. hi2c->XferOptions = I2C_FIRST_FRAME;
  6428. }
  6429. /* else if user set XferOptions to I2C_OTHER_AND_LAST_FRAME */
  6430. /* it request implicitly to generate a restart condition */
  6431. /* then generate a stop condition at the end of transfer */
  6432. /* set XferOptions to I2C_FIRST_AND_LAST_FRAME */
  6433. else if (hi2c->XferOptions == I2C_OTHER_AND_LAST_FRAME)
  6434. {
  6435. hi2c->XferOptions = I2C_FIRST_AND_LAST_FRAME;
  6436. }
  6437. else
  6438. {
  6439. /* Nothing to do */
  6440. }
  6441. }
  6442. /**
  6443. * @}
  6444. */
  6445. #endif /* HAL_I2C_MODULE_ENABLED */
  6446. /**
  6447. * @}
  6448. */
  6449. /**
  6450. * @}
  6451. */