stm32h7xx_hal_i2c.c 246 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499
  1. /**
  2. ******************************************************************************
  3. * @file stm32h7xx_hal_i2c.c
  4. * @author MCD Application Team
  5. * @brief I2C HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Inter Integrated Circuit (I2C) peripheral:
  8. * + Initialization and de-initialization functions
  9. * + IO operation functions
  10. * + Peripheral State and Errors functions
  11. *
  12. ******************************************************************************
  13. * @attention
  14. *
  15. * Copyright (c) 2017 STMicroelectronics.
  16. * All rights reserved.
  17. *
  18. * This software is licensed under terms that can be found in the LICENSE file
  19. * in the root directory of this software component.
  20. * If no LICENSE file comes with this software, it is provided AS-IS.
  21. *
  22. ******************************************************************************
  23. @verbatim
  24. ==============================================================================
  25. ##### How to use this driver #####
  26. ==============================================================================
  27. [..]
  28. The I2C HAL driver can be used as follows:
  29. (#) Declare a I2C_HandleTypeDef handle structure, for example:
  30. I2C_HandleTypeDef hi2c;
  31. (#)Initialize the I2C low level resources by implementing the HAL_I2C_MspInit() API:
  32. (##) Enable the I2Cx interface clock
  33. (##) I2C pins configuration
  34. (+++) Enable the clock for the I2C GPIOs
  35. (+++) Configure I2C pins as alternate function open-drain
  36. (##) NVIC configuration if you need to use interrupt process
  37. (+++) Configure the I2Cx interrupt priority
  38. (+++) Enable the NVIC I2C IRQ Channel
  39. (##) DMA Configuration if you need to use DMA process
  40. (+++) Declare a DMA_HandleTypeDef handle structure for
  41. the transmit or receive stream or channel depends on Instance
  42. (+++) Enable the DMAx interface clock using
  43. (+++) Configure the DMA handle parameters
  44. (+++) Configure the DMA Tx or Rx stream or channel depends on Instance
  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 stream or channel depends on Instance
  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 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 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 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 "stm32h7xx_hal.h"
  301. /** @addtogroup STM32H7xx_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 = MAX_NBYTE_SIZE;
  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. /* Wait until ADDR flag is set */
  1219. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout, tickstart) != HAL_OK)
  1220. {
  1221. /* Disable Address Acknowledge */
  1222. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  1223. return HAL_ERROR;
  1224. }
  1225. /* Preload TX data if no stretch enable */
  1226. if (hi2c->Init.NoStretchMode == I2C_NOSTRETCH_ENABLE)
  1227. {
  1228. /* Preload TX register */
  1229. /* Write data to TXDR */
  1230. hi2c->Instance->TXDR = *hi2c->pBuffPtr;
  1231. /* Increment Buffer pointer */
  1232. hi2c->pBuffPtr++;
  1233. hi2c->XferCount--;
  1234. }
  1235. /* Clear ADDR flag */
  1236. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
  1237. /* If 10bit addressing mode is selected */
  1238. if (hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT)
  1239. {
  1240. /* Wait until ADDR flag is set */
  1241. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout, tickstart) != HAL_OK)
  1242. {
  1243. /* Disable Address Acknowledge */
  1244. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  1245. return HAL_ERROR;
  1246. }
  1247. /* Clear ADDR flag */
  1248. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
  1249. }
  1250. /* Wait until DIR flag is set Transmitter mode */
  1251. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_DIR, RESET, Timeout, tickstart) != HAL_OK)
  1252. {
  1253. /* Disable Address Acknowledge */
  1254. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  1255. return HAL_ERROR;
  1256. }
  1257. while (hi2c->XferCount > 0U)
  1258. {
  1259. /* Wait until TXIS flag is set */
  1260. if (I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
  1261. {
  1262. /* Disable Address Acknowledge */
  1263. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  1264. return HAL_ERROR;
  1265. }
  1266. /* Write data to TXDR */
  1267. hi2c->Instance->TXDR = *hi2c->pBuffPtr;
  1268. /* Increment Buffer pointer */
  1269. hi2c->pBuffPtr++;
  1270. hi2c->XferCount--;
  1271. }
  1272. /* Wait until AF flag is set */
  1273. error = I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_AF, RESET, Timeout, tickstart);
  1274. if (error != HAL_OK)
  1275. {
  1276. /* Check that I2C transfer finished */
  1277. /* if yes, normal use case, a NACK is sent by the MASTER when Transfer is finished */
  1278. /* Mean XferCount == 0 */
  1279. tmpXferCount = hi2c->XferCount;
  1280. if ((hi2c->ErrorCode == HAL_I2C_ERROR_AF) && (tmpXferCount == 0U))
  1281. {
  1282. /* Reset ErrorCode to NONE */
  1283. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1284. }
  1285. else
  1286. {
  1287. /* Disable Address Acknowledge */
  1288. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  1289. return HAL_ERROR;
  1290. }
  1291. }
  1292. else
  1293. {
  1294. /* Flush TX register */
  1295. I2C_Flush_TXDR(hi2c);
  1296. /* Clear AF flag */
  1297. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  1298. /* Wait until STOP flag is set */
  1299. if (I2C_WaitOnSTOPFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
  1300. {
  1301. /* Disable Address Acknowledge */
  1302. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  1303. return HAL_ERROR;
  1304. }
  1305. /* Clear STOP flag */
  1306. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  1307. }
  1308. /* Wait until BUSY flag is reset */
  1309. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, Timeout, tickstart) != HAL_OK)
  1310. {
  1311. /* Disable Address Acknowledge */
  1312. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  1313. return HAL_ERROR;
  1314. }
  1315. /* Disable Address Acknowledge */
  1316. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  1317. hi2c->State = HAL_I2C_STATE_READY;
  1318. hi2c->Mode = HAL_I2C_MODE_NONE;
  1319. /* Process Unlocked */
  1320. __HAL_UNLOCK(hi2c);
  1321. return HAL_OK;
  1322. }
  1323. else
  1324. {
  1325. return HAL_BUSY;
  1326. }
  1327. }
  1328. /**
  1329. * @brief Receive in slave mode an amount of data in blocking mode
  1330. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1331. * the configuration information for the specified I2C.
  1332. * @param pData Pointer to data buffer
  1333. * @param Size Amount of data to be sent
  1334. * @param Timeout Timeout duration
  1335. * @retval HAL status
  1336. */
  1337. HAL_StatusTypeDef HAL_I2C_Slave_Receive(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size,
  1338. uint32_t Timeout)
  1339. {
  1340. uint32_t tickstart;
  1341. if (hi2c->State == HAL_I2C_STATE_READY)
  1342. {
  1343. if ((pData == NULL) || (Size == 0U))
  1344. {
  1345. hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM;
  1346. return HAL_ERROR;
  1347. }
  1348. /* Process Locked */
  1349. __HAL_LOCK(hi2c);
  1350. /* Init tickstart for timeout management*/
  1351. tickstart = HAL_GetTick();
  1352. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  1353. hi2c->Mode = HAL_I2C_MODE_SLAVE;
  1354. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1355. /* Prepare transfer parameters */
  1356. hi2c->pBuffPtr = pData;
  1357. hi2c->XferCount = Size;
  1358. hi2c->XferSize = hi2c->XferCount;
  1359. hi2c->XferISR = NULL;
  1360. /* Enable Address Acknowledge */
  1361. hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
  1362. /* Wait until ADDR flag is set */
  1363. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout, tickstart) != HAL_OK)
  1364. {
  1365. /* Disable Address Acknowledge */
  1366. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  1367. return HAL_ERROR;
  1368. }
  1369. /* Clear ADDR flag */
  1370. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
  1371. /* Wait until DIR flag is reset Receiver mode */
  1372. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_DIR, SET, Timeout, tickstart) != HAL_OK)
  1373. {
  1374. /* Disable Address Acknowledge */
  1375. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  1376. return HAL_ERROR;
  1377. }
  1378. while (hi2c->XferCount > 0U)
  1379. {
  1380. /* Wait until RXNE flag is set */
  1381. if (I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
  1382. {
  1383. /* Disable Address Acknowledge */
  1384. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  1385. /* Store Last receive data if any */
  1386. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET)
  1387. {
  1388. /* Read data from RXDR */
  1389. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->RXDR;
  1390. /* Increment Buffer pointer */
  1391. hi2c->pBuffPtr++;
  1392. hi2c->XferCount--;
  1393. hi2c->XferSize--;
  1394. }
  1395. return HAL_ERROR;
  1396. }
  1397. /* Read data from RXDR */
  1398. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->RXDR;
  1399. /* Increment Buffer pointer */
  1400. hi2c->pBuffPtr++;
  1401. hi2c->XferCount--;
  1402. hi2c->XferSize--;
  1403. }
  1404. /* Wait until STOP flag is set */
  1405. if (I2C_WaitOnSTOPFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
  1406. {
  1407. /* Disable Address Acknowledge */
  1408. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  1409. return HAL_ERROR;
  1410. }
  1411. /* Clear STOP flag */
  1412. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  1413. /* Wait until BUSY flag is reset */
  1414. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, Timeout, tickstart) != HAL_OK)
  1415. {
  1416. /* Disable Address Acknowledge */
  1417. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  1418. return HAL_ERROR;
  1419. }
  1420. /* Disable Address Acknowledge */
  1421. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  1422. hi2c->State = HAL_I2C_STATE_READY;
  1423. hi2c->Mode = HAL_I2C_MODE_NONE;
  1424. /* Process Unlocked */
  1425. __HAL_UNLOCK(hi2c);
  1426. return HAL_OK;
  1427. }
  1428. else
  1429. {
  1430. return HAL_BUSY;
  1431. }
  1432. }
  1433. /**
  1434. * @brief Transmit in master mode an amount of data in non-blocking mode with Interrupt
  1435. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1436. * the configuration information for the specified I2C.
  1437. * @param DevAddress Target device address: The device 7 bits address value
  1438. * in datasheet must be shifted to the left before calling the interface
  1439. * @param pData Pointer to data buffer
  1440. * @param Size Amount of data to be sent
  1441. * @retval HAL status
  1442. */
  1443. HAL_StatusTypeDef HAL_I2C_Master_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData,
  1444. uint16_t Size)
  1445. {
  1446. uint32_t xfermode;
  1447. if (hi2c->State == HAL_I2C_STATE_READY)
  1448. {
  1449. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  1450. {
  1451. return HAL_BUSY;
  1452. }
  1453. /* Process Locked */
  1454. __HAL_LOCK(hi2c);
  1455. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  1456. hi2c->Mode = HAL_I2C_MODE_MASTER;
  1457. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1458. /* Prepare transfer parameters */
  1459. hi2c->pBuffPtr = pData;
  1460. hi2c->XferCount = Size;
  1461. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  1462. hi2c->XferISR = I2C_Master_ISR_IT;
  1463. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  1464. {
  1465. hi2c->XferSize = MAX_NBYTE_SIZE;
  1466. xfermode = I2C_RELOAD_MODE;
  1467. }
  1468. else
  1469. {
  1470. hi2c->XferSize = hi2c->XferCount;
  1471. xfermode = I2C_AUTOEND_MODE;
  1472. }
  1473. /* Send Slave Address */
  1474. /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE */
  1475. if (hi2c->XferSize > 0U)
  1476. {
  1477. /* Preload TX register */
  1478. /* Write data to TXDR */
  1479. hi2c->Instance->TXDR = *hi2c->pBuffPtr;
  1480. /* Increment Buffer pointer */
  1481. hi2c->pBuffPtr++;
  1482. hi2c->XferCount--;
  1483. hi2c->XferSize--;
  1484. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)(hi2c->XferSize + 1U), xfermode,
  1485. I2C_GENERATE_START_WRITE);
  1486. }
  1487. else
  1488. {
  1489. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode,
  1490. I2C_GENERATE_START_WRITE);
  1491. }
  1492. /* Process Unlocked */
  1493. __HAL_UNLOCK(hi2c);
  1494. /* Note : The I2C interrupts must be enabled after unlocking current process
  1495. to avoid the risk of I2C interrupt handle execution before current
  1496. process unlock */
  1497. /* Enable ERR, TC, STOP, NACK, TXI interrupt */
  1498. /* possible to enable all of these */
  1499. /* I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI |
  1500. I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
  1501. I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT);
  1502. return HAL_OK;
  1503. }
  1504. else
  1505. {
  1506. return HAL_BUSY;
  1507. }
  1508. }
  1509. /**
  1510. * @brief Receive in master mode an amount of data in non-blocking mode with Interrupt
  1511. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1512. * the configuration information for the specified I2C.
  1513. * @param DevAddress Target device address: The device 7 bits address value
  1514. * in datasheet must be shifted to the left before calling the interface
  1515. * @param pData Pointer to data buffer
  1516. * @param Size Amount of data to be sent
  1517. * @retval HAL status
  1518. */
  1519. HAL_StatusTypeDef HAL_I2C_Master_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData,
  1520. uint16_t Size)
  1521. {
  1522. uint32_t xfermode;
  1523. if (hi2c->State == HAL_I2C_STATE_READY)
  1524. {
  1525. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  1526. {
  1527. return HAL_BUSY;
  1528. }
  1529. /* Process Locked */
  1530. __HAL_LOCK(hi2c);
  1531. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  1532. hi2c->Mode = HAL_I2C_MODE_MASTER;
  1533. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1534. /* Prepare transfer parameters */
  1535. hi2c->pBuffPtr = pData;
  1536. hi2c->XferCount = Size;
  1537. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  1538. hi2c->XferISR = I2C_Master_ISR_IT;
  1539. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  1540. {
  1541. hi2c->XferSize = MAX_NBYTE_SIZE;
  1542. xfermode = I2C_RELOAD_MODE;
  1543. }
  1544. else
  1545. {
  1546. hi2c->XferSize = hi2c->XferCount;
  1547. xfermode = I2C_AUTOEND_MODE;
  1548. }
  1549. /* Send Slave Address */
  1550. /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE */
  1551. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, I2C_GENERATE_START_READ);
  1552. /* Process Unlocked */
  1553. __HAL_UNLOCK(hi2c);
  1554. /* Note : The I2C interrupts must be enabled after unlocking current process
  1555. to avoid the risk of I2C interrupt handle execution before current
  1556. process unlock */
  1557. /* Enable ERR, TC, STOP, NACK, RXI interrupt */
  1558. /* possible to enable all of these */
  1559. /* I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI |
  1560. I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
  1561. I2C_Enable_IRQ(hi2c, I2C_XFER_RX_IT);
  1562. return HAL_OK;
  1563. }
  1564. else
  1565. {
  1566. return HAL_BUSY;
  1567. }
  1568. }
  1569. /**
  1570. * @brief Transmit in slave mode an amount of data in non-blocking mode with Interrupt
  1571. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1572. * the configuration information for the specified I2C.
  1573. * @param pData Pointer to data buffer
  1574. * @param Size Amount of data to be sent
  1575. * @retval HAL status
  1576. */
  1577. HAL_StatusTypeDef HAL_I2C_Slave_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
  1578. {
  1579. if (hi2c->State == HAL_I2C_STATE_READY)
  1580. {
  1581. /* Process Locked */
  1582. __HAL_LOCK(hi2c);
  1583. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  1584. hi2c->Mode = HAL_I2C_MODE_SLAVE;
  1585. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1586. /* Enable Address Acknowledge */
  1587. hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
  1588. /* Prepare transfer parameters */
  1589. hi2c->pBuffPtr = pData;
  1590. hi2c->XferCount = Size;
  1591. hi2c->XferSize = hi2c->XferCount;
  1592. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  1593. hi2c->XferISR = I2C_Slave_ISR_IT;
  1594. /* Preload TX data if no stretch enable */
  1595. if (hi2c->Init.NoStretchMode == I2C_NOSTRETCH_ENABLE)
  1596. {
  1597. /* Preload TX register */
  1598. /* Write data to TXDR */
  1599. hi2c->Instance->TXDR = *hi2c->pBuffPtr;
  1600. /* Increment Buffer pointer */
  1601. hi2c->pBuffPtr++;
  1602. hi2c->XferCount--;
  1603. hi2c->XferSize--;
  1604. }
  1605. /* Process Unlocked */
  1606. __HAL_UNLOCK(hi2c);
  1607. /* Note : The I2C interrupts must be enabled after unlocking current process
  1608. to avoid the risk of I2C interrupt handle execution before current
  1609. process unlock */
  1610. /* Enable ERR, TC, STOP, NACK, TXI interrupt */
  1611. /* possible to enable all of these */
  1612. /* I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI |
  1613. I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
  1614. I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT | I2C_XFER_LISTEN_IT);
  1615. return HAL_OK;
  1616. }
  1617. else
  1618. {
  1619. return HAL_BUSY;
  1620. }
  1621. }
  1622. /**
  1623. * @brief Receive in slave mode an amount of data in non-blocking mode with Interrupt
  1624. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1625. * the configuration information for the specified I2C.
  1626. * @param pData Pointer to data buffer
  1627. * @param Size Amount of data to be sent
  1628. * @retval HAL status
  1629. */
  1630. HAL_StatusTypeDef HAL_I2C_Slave_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
  1631. {
  1632. if (hi2c->State == HAL_I2C_STATE_READY)
  1633. {
  1634. /* Process Locked */
  1635. __HAL_LOCK(hi2c);
  1636. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  1637. hi2c->Mode = HAL_I2C_MODE_SLAVE;
  1638. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1639. /* Enable Address Acknowledge */
  1640. hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
  1641. /* Prepare transfer parameters */
  1642. hi2c->pBuffPtr = pData;
  1643. hi2c->XferCount = Size;
  1644. hi2c->XferSize = hi2c->XferCount;
  1645. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  1646. hi2c->XferISR = I2C_Slave_ISR_IT;
  1647. /* Process Unlocked */
  1648. __HAL_UNLOCK(hi2c);
  1649. /* Note : The I2C interrupts must be enabled after unlocking current process
  1650. to avoid the risk of I2C interrupt handle execution before current
  1651. process unlock */
  1652. /* Enable ERR, TC, STOP, NACK, RXI interrupt */
  1653. /* possible to enable all of these */
  1654. /* I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI |
  1655. I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
  1656. I2C_Enable_IRQ(hi2c, I2C_XFER_RX_IT | I2C_XFER_LISTEN_IT);
  1657. return HAL_OK;
  1658. }
  1659. else
  1660. {
  1661. return HAL_BUSY;
  1662. }
  1663. }
  1664. /**
  1665. * @brief Transmit in master mode an amount of data in non-blocking mode with DMA
  1666. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1667. * the configuration information for the specified I2C.
  1668. * @param DevAddress Target device address: The device 7 bits address value
  1669. * in datasheet must be shifted to the left before calling the interface
  1670. * @param pData Pointer to data buffer
  1671. * @param Size Amount of data to be sent
  1672. * @retval HAL status
  1673. */
  1674. HAL_StatusTypeDef HAL_I2C_Master_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData,
  1675. uint16_t Size)
  1676. {
  1677. uint32_t xfermode;
  1678. HAL_StatusTypeDef dmaxferstatus;
  1679. uint32_t sizetoxfer = 0U;
  1680. if (hi2c->State == HAL_I2C_STATE_READY)
  1681. {
  1682. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  1683. {
  1684. return HAL_BUSY;
  1685. }
  1686. /* Process Locked */
  1687. __HAL_LOCK(hi2c);
  1688. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  1689. hi2c->Mode = HAL_I2C_MODE_MASTER;
  1690. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1691. /* Prepare transfer parameters */
  1692. hi2c->pBuffPtr = pData;
  1693. hi2c->XferCount = Size;
  1694. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  1695. hi2c->XferISR = I2C_Master_ISR_DMA;
  1696. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  1697. {
  1698. hi2c->XferSize = MAX_NBYTE_SIZE;
  1699. xfermode = I2C_RELOAD_MODE;
  1700. }
  1701. else
  1702. {
  1703. hi2c->XferSize = hi2c->XferCount;
  1704. xfermode = I2C_AUTOEND_MODE;
  1705. }
  1706. if (hi2c->XferSize > 0U)
  1707. {
  1708. /* Preload TX register */
  1709. /* Write data to TXDR */
  1710. hi2c->Instance->TXDR = *hi2c->pBuffPtr;
  1711. /* Increment Buffer pointer */
  1712. hi2c->pBuffPtr++;
  1713. sizetoxfer = hi2c->XferSize;
  1714. hi2c->XferCount--;
  1715. hi2c->XferSize--;
  1716. }
  1717. if (hi2c->XferSize > 0U)
  1718. {
  1719. if (hi2c->hdmatx != NULL)
  1720. {
  1721. /* Set the I2C DMA transfer complete callback */
  1722. hi2c->hdmatx->XferCpltCallback = I2C_DMAMasterTransmitCplt;
  1723. /* Set the DMA error callback */
  1724. hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
  1725. /* Set the unused DMA callbacks to NULL */
  1726. hi2c->hdmatx->XferHalfCpltCallback = NULL;
  1727. hi2c->hdmatx->XferAbortCallback = NULL;
  1728. /* Enable the DMA stream or channel depends on Instance */
  1729. dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr,
  1730. (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);
  1731. }
  1732. else
  1733. {
  1734. /* Update I2C state */
  1735. hi2c->State = HAL_I2C_STATE_READY;
  1736. hi2c->Mode = HAL_I2C_MODE_NONE;
  1737. /* Update I2C error code */
  1738. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
  1739. /* Process Unlocked */
  1740. __HAL_UNLOCK(hi2c);
  1741. return HAL_ERROR;
  1742. }
  1743. if (dmaxferstatus == HAL_OK)
  1744. {
  1745. /* Send Slave Address */
  1746. /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
  1747. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)(hi2c->XferSize + 1U),
  1748. xfermode, I2C_GENERATE_START_WRITE);
  1749. /* Update XferCount value */
  1750. hi2c->XferCount -= hi2c->XferSize;
  1751. /* Process Unlocked */
  1752. __HAL_UNLOCK(hi2c);
  1753. /* Note : The I2C interrupts must be enabled after unlocking current process
  1754. to avoid the risk of I2C interrupt handle execution before current
  1755. process unlock */
  1756. /* Enable ERR and NACK interrupts */
  1757. I2C_Enable_IRQ(hi2c, I2C_XFER_ERROR_IT);
  1758. /* Enable DMA Request */
  1759. hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
  1760. }
  1761. else
  1762. {
  1763. /* Update I2C state */
  1764. hi2c->State = HAL_I2C_STATE_READY;
  1765. hi2c->Mode = HAL_I2C_MODE_NONE;
  1766. /* Update I2C error code */
  1767. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  1768. /* Process Unlocked */
  1769. __HAL_UNLOCK(hi2c);
  1770. return HAL_ERROR;
  1771. }
  1772. }
  1773. else
  1774. {
  1775. /* Update Transfer ISR function pointer */
  1776. hi2c->XferISR = I2C_Master_ISR_IT;
  1777. /* Send Slave Address */
  1778. /* Set NBYTES to write and generate START condition */
  1779. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)sizetoxfer, I2C_AUTOEND_MODE,
  1780. I2C_GENERATE_START_WRITE);
  1781. /* Process Unlocked */
  1782. __HAL_UNLOCK(hi2c);
  1783. /* Note : The I2C interrupts must be enabled after unlocking current process
  1784. to avoid the risk of I2C interrupt handle execution before current
  1785. process unlock */
  1786. /* Enable ERR, TC, STOP, NACK, TXI interrupt */
  1787. /* possible to enable all of these */
  1788. /* I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI |
  1789. I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
  1790. I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT);
  1791. }
  1792. return HAL_OK;
  1793. }
  1794. else
  1795. {
  1796. return HAL_BUSY;
  1797. }
  1798. }
  1799. /**
  1800. * @brief Receive in master mode an amount of data in non-blocking mode with DMA
  1801. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1802. * the configuration information for the specified I2C.
  1803. * @param DevAddress Target device address: The device 7 bits address value
  1804. * in datasheet must be shifted to the left before calling the interface
  1805. * @param pData Pointer to data buffer
  1806. * @param Size Amount of data to be sent
  1807. * @retval HAL status
  1808. */
  1809. HAL_StatusTypeDef HAL_I2C_Master_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData,
  1810. uint16_t Size)
  1811. {
  1812. uint32_t xfermode;
  1813. HAL_StatusTypeDef dmaxferstatus;
  1814. if (hi2c->State == HAL_I2C_STATE_READY)
  1815. {
  1816. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  1817. {
  1818. return HAL_BUSY;
  1819. }
  1820. /* Process Locked */
  1821. __HAL_LOCK(hi2c);
  1822. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  1823. hi2c->Mode = HAL_I2C_MODE_MASTER;
  1824. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1825. /* Prepare transfer parameters */
  1826. hi2c->pBuffPtr = pData;
  1827. hi2c->XferCount = Size;
  1828. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  1829. hi2c->XferISR = I2C_Master_ISR_DMA;
  1830. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  1831. {
  1832. hi2c->XferSize = MAX_NBYTE_SIZE;
  1833. xfermode = I2C_RELOAD_MODE;
  1834. }
  1835. else
  1836. {
  1837. hi2c->XferSize = hi2c->XferCount;
  1838. xfermode = I2C_AUTOEND_MODE;
  1839. }
  1840. if (hi2c->XferSize > 0U)
  1841. {
  1842. if (hi2c->hdmarx != NULL)
  1843. {
  1844. /* Set the I2C DMA transfer complete callback */
  1845. hi2c->hdmarx->XferCpltCallback = I2C_DMAMasterReceiveCplt;
  1846. /* Set the DMA error callback */
  1847. hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
  1848. /* Set the unused DMA callbacks to NULL */
  1849. hi2c->hdmarx->XferHalfCpltCallback = NULL;
  1850. hi2c->hdmarx->XferAbortCallback = NULL;
  1851. /* Enable the DMA stream or channel depends on Instance */
  1852. dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)pData,
  1853. hi2c->XferSize);
  1854. }
  1855. else
  1856. {
  1857. /* Update I2C state */
  1858. hi2c->State = HAL_I2C_STATE_READY;
  1859. hi2c->Mode = HAL_I2C_MODE_NONE;
  1860. /* Update I2C error code */
  1861. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
  1862. /* Process Unlocked */
  1863. __HAL_UNLOCK(hi2c);
  1864. return HAL_ERROR;
  1865. }
  1866. if (dmaxferstatus == HAL_OK)
  1867. {
  1868. /* Send Slave Address */
  1869. /* Set NBYTES to read and reload if hi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
  1870. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, I2C_GENERATE_START_READ);
  1871. /* Update XferCount value */
  1872. hi2c->XferCount -= hi2c->XferSize;
  1873. /* Process Unlocked */
  1874. __HAL_UNLOCK(hi2c);
  1875. /* Note : The I2C interrupts must be enabled after unlocking current process
  1876. to avoid the risk of I2C interrupt handle execution before current
  1877. process unlock */
  1878. /* Enable ERR and NACK interrupts */
  1879. I2C_Enable_IRQ(hi2c, I2C_XFER_ERROR_IT);
  1880. /* Enable DMA Request */
  1881. hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
  1882. }
  1883. else
  1884. {
  1885. /* Update I2C state */
  1886. hi2c->State = HAL_I2C_STATE_READY;
  1887. hi2c->Mode = HAL_I2C_MODE_NONE;
  1888. /* Update I2C error code */
  1889. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  1890. /* Process Unlocked */
  1891. __HAL_UNLOCK(hi2c);
  1892. return HAL_ERROR;
  1893. }
  1894. }
  1895. else
  1896. {
  1897. /* Update Transfer ISR function pointer */
  1898. hi2c->XferISR = I2C_Master_ISR_IT;
  1899. /* Send Slave Address */
  1900. /* Set NBYTES to read and generate START condition */
  1901. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE,
  1902. I2C_GENERATE_START_READ);
  1903. /* Process Unlocked */
  1904. __HAL_UNLOCK(hi2c);
  1905. /* Note : The I2C interrupts must be enabled after unlocking current process
  1906. to avoid the risk of I2C interrupt handle execution before current
  1907. process unlock */
  1908. /* Enable ERR, TC, STOP, NACK, RXI interrupt */
  1909. /* possible to enable all of these */
  1910. /* I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI |
  1911. I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
  1912. I2C_Enable_IRQ(hi2c, I2C_XFER_RX_IT);
  1913. }
  1914. return HAL_OK;
  1915. }
  1916. else
  1917. {
  1918. return HAL_BUSY;
  1919. }
  1920. }
  1921. /**
  1922. * @brief Transmit in slave mode an amount of data in non-blocking mode with DMA
  1923. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1924. * the configuration information for the specified I2C.
  1925. * @param pData Pointer to data buffer
  1926. * @param Size Amount of data to be sent
  1927. * @retval HAL status
  1928. */
  1929. HAL_StatusTypeDef HAL_I2C_Slave_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
  1930. {
  1931. HAL_StatusTypeDef dmaxferstatus;
  1932. if (hi2c->State == HAL_I2C_STATE_READY)
  1933. {
  1934. if ((pData == NULL) || (Size == 0U))
  1935. {
  1936. hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM;
  1937. return HAL_ERROR;
  1938. }
  1939. /* Process Locked */
  1940. __HAL_LOCK(hi2c);
  1941. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  1942. hi2c->Mode = HAL_I2C_MODE_SLAVE;
  1943. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1944. /* Prepare transfer parameters */
  1945. hi2c->pBuffPtr = pData;
  1946. hi2c->XferCount = Size;
  1947. hi2c->XferSize = hi2c->XferCount;
  1948. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  1949. hi2c->XferISR = I2C_Slave_ISR_DMA;
  1950. /* Preload TX data if no stretch enable */
  1951. if (hi2c->Init.NoStretchMode == I2C_NOSTRETCH_ENABLE)
  1952. {
  1953. /* Preload TX register */
  1954. /* Write data to TXDR */
  1955. hi2c->Instance->TXDR = *hi2c->pBuffPtr;
  1956. /* Increment Buffer pointer */
  1957. hi2c->pBuffPtr++;
  1958. hi2c->XferCount--;
  1959. hi2c->XferSize--;
  1960. }
  1961. if (hi2c->XferCount != 0U)
  1962. {
  1963. if (hi2c->hdmatx != NULL)
  1964. {
  1965. /* Set the I2C DMA transfer complete callback */
  1966. hi2c->hdmatx->XferCpltCallback = I2C_DMASlaveTransmitCplt;
  1967. /* Set the DMA error callback */
  1968. hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
  1969. /* Set the unused DMA callbacks to NULL */
  1970. hi2c->hdmatx->XferHalfCpltCallback = NULL;
  1971. hi2c->hdmatx->XferAbortCallback = NULL;
  1972. /* Enable the DMA stream or channel depends on Instance */
  1973. dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx,
  1974. (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->TXDR,
  1975. hi2c->XferSize);
  1976. }
  1977. else
  1978. {
  1979. /* Update I2C state */
  1980. hi2c->State = HAL_I2C_STATE_LISTEN;
  1981. hi2c->Mode = HAL_I2C_MODE_NONE;
  1982. /* Update I2C error code */
  1983. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
  1984. /* Process Unlocked */
  1985. __HAL_UNLOCK(hi2c);
  1986. return HAL_ERROR;
  1987. }
  1988. if (dmaxferstatus == HAL_OK)
  1989. {
  1990. /* Enable Address Acknowledge */
  1991. hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
  1992. /* Process Unlocked */
  1993. __HAL_UNLOCK(hi2c);
  1994. /* Note : The I2C interrupts must be enabled after unlocking current process
  1995. to avoid the risk of I2C interrupt handle execution before current
  1996. process unlock */
  1997. /* Enable ERR, STOP, NACK, ADDR interrupts */
  1998. I2C_Enable_IRQ(hi2c, I2C_XFER_LISTEN_IT);
  1999. /* Enable DMA Request */
  2000. hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
  2001. }
  2002. else
  2003. {
  2004. /* Update I2C state */
  2005. hi2c->State = HAL_I2C_STATE_LISTEN;
  2006. hi2c->Mode = HAL_I2C_MODE_NONE;
  2007. /* Update I2C error code */
  2008. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  2009. /* Process Unlocked */
  2010. __HAL_UNLOCK(hi2c);
  2011. return HAL_ERROR;
  2012. }
  2013. }
  2014. else
  2015. {
  2016. /* Enable Address Acknowledge */
  2017. hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
  2018. /* Process Unlocked */
  2019. __HAL_UNLOCK(hi2c);
  2020. /* Note : The I2C interrupts must be enabled after unlocking current process
  2021. to avoid the risk of I2C interrupt handle execution before current
  2022. process unlock */
  2023. /* Enable ERR, STOP, NACK, ADDR interrupts */
  2024. I2C_Enable_IRQ(hi2c, I2C_XFER_LISTEN_IT);
  2025. }
  2026. return HAL_OK;
  2027. }
  2028. else
  2029. {
  2030. return HAL_BUSY;
  2031. }
  2032. }
  2033. /**
  2034. * @brief Receive in slave mode an amount of data in non-blocking mode with DMA
  2035. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2036. * the configuration information for the specified I2C.
  2037. * @param pData Pointer to data buffer
  2038. * @param Size Amount of data to be sent
  2039. * @retval HAL status
  2040. */
  2041. HAL_StatusTypeDef HAL_I2C_Slave_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
  2042. {
  2043. HAL_StatusTypeDef dmaxferstatus;
  2044. if (hi2c->State == HAL_I2C_STATE_READY)
  2045. {
  2046. if ((pData == NULL) || (Size == 0U))
  2047. {
  2048. hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM;
  2049. return HAL_ERROR;
  2050. }
  2051. /* Process Locked */
  2052. __HAL_LOCK(hi2c);
  2053. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  2054. hi2c->Mode = HAL_I2C_MODE_SLAVE;
  2055. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2056. /* Prepare transfer parameters */
  2057. hi2c->pBuffPtr = pData;
  2058. hi2c->XferCount = Size;
  2059. hi2c->XferSize = hi2c->XferCount;
  2060. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  2061. hi2c->XferISR = I2C_Slave_ISR_DMA;
  2062. if (hi2c->hdmarx != NULL)
  2063. {
  2064. /* Set the I2C DMA transfer complete callback */
  2065. hi2c->hdmarx->XferCpltCallback = I2C_DMASlaveReceiveCplt;
  2066. /* Set the DMA error callback */
  2067. hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
  2068. /* Set the unused DMA callbacks to NULL */
  2069. hi2c->hdmarx->XferHalfCpltCallback = NULL;
  2070. hi2c->hdmarx->XferAbortCallback = NULL;
  2071. /* Enable the DMA stream or channel depends on Instance */
  2072. dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)pData,
  2073. hi2c->XferSize);
  2074. }
  2075. else
  2076. {
  2077. /* Update I2C state */
  2078. hi2c->State = HAL_I2C_STATE_LISTEN;
  2079. hi2c->Mode = HAL_I2C_MODE_NONE;
  2080. /* Update I2C error code */
  2081. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
  2082. /* Process Unlocked */
  2083. __HAL_UNLOCK(hi2c);
  2084. return HAL_ERROR;
  2085. }
  2086. if (dmaxferstatus == HAL_OK)
  2087. {
  2088. /* Enable Address Acknowledge */
  2089. hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
  2090. /* Process Unlocked */
  2091. __HAL_UNLOCK(hi2c);
  2092. /* Note : The I2C interrupts must be enabled after unlocking current process
  2093. to avoid the risk of I2C interrupt handle execution before current
  2094. process unlock */
  2095. /* Enable ERR, STOP, NACK, ADDR interrupts */
  2096. I2C_Enable_IRQ(hi2c, I2C_XFER_LISTEN_IT);
  2097. /* Enable DMA Request */
  2098. hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
  2099. }
  2100. else
  2101. {
  2102. /* Update I2C state */
  2103. hi2c->State = HAL_I2C_STATE_LISTEN;
  2104. hi2c->Mode = HAL_I2C_MODE_NONE;
  2105. /* Update I2C error code */
  2106. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  2107. /* Process Unlocked */
  2108. __HAL_UNLOCK(hi2c);
  2109. return HAL_ERROR;
  2110. }
  2111. return HAL_OK;
  2112. }
  2113. else
  2114. {
  2115. return HAL_BUSY;
  2116. }
  2117. }
  2118. /**
  2119. * @brief Write an amount of data in blocking mode to a specific memory address
  2120. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2121. * the configuration information for the specified I2C.
  2122. * @param DevAddress Target device address: The device 7 bits address value
  2123. * in datasheet must be shifted to the left before calling the interface
  2124. * @param MemAddress Internal memory address
  2125. * @param MemAddSize Size of internal memory address
  2126. * @param pData Pointer to data buffer
  2127. * @param Size Amount of data to be sent
  2128. * @param Timeout Timeout duration
  2129. * @retval HAL status
  2130. */
  2131. HAL_StatusTypeDef HAL_I2C_Mem_Write(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress,
  2132. uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  2133. {
  2134. uint32_t tickstart;
  2135. /* Check the parameters */
  2136. assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
  2137. if (hi2c->State == HAL_I2C_STATE_READY)
  2138. {
  2139. if ((pData == NULL) || (Size == 0U))
  2140. {
  2141. hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM;
  2142. return HAL_ERROR;
  2143. }
  2144. /* Process Locked */
  2145. __HAL_LOCK(hi2c);
  2146. /* Init tickstart for timeout management*/
  2147. tickstart = HAL_GetTick();
  2148. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY, tickstart) != HAL_OK)
  2149. {
  2150. return HAL_ERROR;
  2151. }
  2152. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  2153. hi2c->Mode = HAL_I2C_MODE_MEM;
  2154. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2155. /* Prepare transfer parameters */
  2156. hi2c->pBuffPtr = pData;
  2157. hi2c->XferCount = Size;
  2158. hi2c->XferISR = NULL;
  2159. /* Send Slave Address and Memory Address */
  2160. if (I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, Timeout, tickstart) != HAL_OK)
  2161. {
  2162. /* Process Unlocked */
  2163. __HAL_UNLOCK(hi2c);
  2164. return HAL_ERROR;
  2165. }
  2166. /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE */
  2167. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  2168. {
  2169. hi2c->XferSize = MAX_NBYTE_SIZE;
  2170. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  2171. }
  2172. else
  2173. {
  2174. hi2c->XferSize = hi2c->XferCount;
  2175. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  2176. }
  2177. do
  2178. {
  2179. /* Wait until TXIS flag is set */
  2180. if (I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
  2181. {
  2182. return HAL_ERROR;
  2183. }
  2184. /* Write data to TXDR */
  2185. hi2c->Instance->TXDR = *hi2c->pBuffPtr;
  2186. /* Increment Buffer pointer */
  2187. hi2c->pBuffPtr++;
  2188. hi2c->XferCount--;
  2189. hi2c->XferSize--;
  2190. if ((hi2c->XferCount != 0U) && (hi2c->XferSize == 0U))
  2191. {
  2192. /* Wait until TCR flag is set */
  2193. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout, tickstart) != HAL_OK)
  2194. {
  2195. return HAL_ERROR;
  2196. }
  2197. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  2198. {
  2199. hi2c->XferSize = MAX_NBYTE_SIZE;
  2200. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_RELOAD_MODE,
  2201. I2C_NO_STARTSTOP);
  2202. }
  2203. else
  2204. {
  2205. hi2c->XferSize = hi2c->XferCount;
  2206. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE,
  2207. I2C_NO_STARTSTOP);
  2208. }
  2209. }
  2210. } while (hi2c->XferCount > 0U);
  2211. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  2212. /* Wait until STOPF flag is reset */
  2213. if (I2C_WaitOnSTOPFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
  2214. {
  2215. return HAL_ERROR;
  2216. }
  2217. /* Clear STOP Flag */
  2218. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  2219. /* Clear Configuration Register 2 */
  2220. I2C_RESET_CR2(hi2c);
  2221. hi2c->State = HAL_I2C_STATE_READY;
  2222. hi2c->Mode = HAL_I2C_MODE_NONE;
  2223. /* Process Unlocked */
  2224. __HAL_UNLOCK(hi2c);
  2225. return HAL_OK;
  2226. }
  2227. else
  2228. {
  2229. return HAL_BUSY;
  2230. }
  2231. }
  2232. /**
  2233. * @brief Read an amount of data in blocking mode from a specific memory address
  2234. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2235. * the configuration information for the specified I2C.
  2236. * @param DevAddress Target device address: The device 7 bits address value
  2237. * in datasheet must be shifted to the left before calling the interface
  2238. * @param MemAddress Internal memory address
  2239. * @param MemAddSize Size of internal memory address
  2240. * @param pData Pointer to data buffer
  2241. * @param Size Amount of data to be sent
  2242. * @param Timeout Timeout duration
  2243. * @retval HAL status
  2244. */
  2245. HAL_StatusTypeDef HAL_I2C_Mem_Read(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress,
  2246. uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  2247. {
  2248. uint32_t tickstart;
  2249. /* Check the parameters */
  2250. assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
  2251. if (hi2c->State == HAL_I2C_STATE_READY)
  2252. {
  2253. if ((pData == NULL) || (Size == 0U))
  2254. {
  2255. hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM;
  2256. return HAL_ERROR;
  2257. }
  2258. /* Process Locked */
  2259. __HAL_LOCK(hi2c);
  2260. /* Init tickstart for timeout management*/
  2261. tickstart = HAL_GetTick();
  2262. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY, tickstart) != HAL_OK)
  2263. {
  2264. return HAL_ERROR;
  2265. }
  2266. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  2267. hi2c->Mode = HAL_I2C_MODE_MEM;
  2268. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2269. /* Prepare transfer parameters */
  2270. hi2c->pBuffPtr = pData;
  2271. hi2c->XferCount = Size;
  2272. hi2c->XferISR = NULL;
  2273. /* Send Slave Address and Memory Address */
  2274. if (I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, Timeout, tickstart) != HAL_OK)
  2275. {
  2276. /* Process Unlocked */
  2277. __HAL_UNLOCK(hi2c);
  2278. return HAL_ERROR;
  2279. }
  2280. /* Send Slave Address */
  2281. /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
  2282. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  2283. {
  2284. hi2c->XferSize = MAX_NBYTE_SIZE;
  2285. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_RELOAD_MODE,
  2286. I2C_GENERATE_START_READ);
  2287. }
  2288. else
  2289. {
  2290. hi2c->XferSize = hi2c->XferCount;
  2291. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE,
  2292. I2C_GENERATE_START_READ);
  2293. }
  2294. do
  2295. {
  2296. /* Wait until RXNE flag is set */
  2297. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, Timeout, tickstart) != HAL_OK)
  2298. {
  2299. return HAL_ERROR;
  2300. }
  2301. /* Read data from RXDR */
  2302. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->RXDR;
  2303. /* Increment Buffer pointer */
  2304. hi2c->pBuffPtr++;
  2305. hi2c->XferSize--;
  2306. hi2c->XferCount--;
  2307. if ((hi2c->XferCount != 0U) && (hi2c->XferSize == 0U))
  2308. {
  2309. /* Wait until TCR flag is set */
  2310. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout, tickstart) != HAL_OK)
  2311. {
  2312. return HAL_ERROR;
  2313. }
  2314. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  2315. {
  2316. hi2c->XferSize = MAX_NBYTE_SIZE;
  2317. I2C_TransferConfig(hi2c, DevAddress, (uint8_t) hi2c->XferSize, I2C_RELOAD_MODE,
  2318. I2C_NO_STARTSTOP);
  2319. }
  2320. else
  2321. {
  2322. hi2c->XferSize = hi2c->XferCount;
  2323. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE,
  2324. I2C_NO_STARTSTOP);
  2325. }
  2326. }
  2327. } while (hi2c->XferCount > 0U);
  2328. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  2329. /* Wait until STOPF flag is reset */
  2330. if (I2C_WaitOnSTOPFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
  2331. {
  2332. return HAL_ERROR;
  2333. }
  2334. /* Clear STOP Flag */
  2335. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  2336. /* Clear Configuration Register 2 */
  2337. I2C_RESET_CR2(hi2c);
  2338. hi2c->State = HAL_I2C_STATE_READY;
  2339. hi2c->Mode = HAL_I2C_MODE_NONE;
  2340. /* Process Unlocked */
  2341. __HAL_UNLOCK(hi2c);
  2342. return HAL_OK;
  2343. }
  2344. else
  2345. {
  2346. return HAL_BUSY;
  2347. }
  2348. }
  2349. /**
  2350. * @brief Write an amount of data in non-blocking mode with Interrupt to a specific memory address
  2351. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2352. * the configuration information for the specified I2C.
  2353. * @param DevAddress Target device address: The device 7 bits address value
  2354. * in datasheet must be shifted to the left before calling the interface
  2355. * @param MemAddress Internal memory address
  2356. * @param MemAddSize Size of internal memory address
  2357. * @param pData Pointer to data buffer
  2358. * @param Size Amount of data to be sent
  2359. * @retval HAL status
  2360. */
  2361. HAL_StatusTypeDef HAL_I2C_Mem_Write_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress,
  2362. uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
  2363. {
  2364. /* Check the parameters */
  2365. assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
  2366. if (hi2c->State == HAL_I2C_STATE_READY)
  2367. {
  2368. if ((pData == NULL) || (Size == 0U))
  2369. {
  2370. hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM;
  2371. return HAL_ERROR;
  2372. }
  2373. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  2374. {
  2375. return HAL_BUSY;
  2376. }
  2377. /* Process Locked */
  2378. __HAL_LOCK(hi2c);
  2379. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  2380. hi2c->Mode = HAL_I2C_MODE_MEM;
  2381. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2382. /* Prepare transfer parameters */
  2383. hi2c->XferSize = 0U;
  2384. hi2c->pBuffPtr = pData;
  2385. hi2c->XferCount = Size;
  2386. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  2387. hi2c->XferISR = I2C_Mem_ISR_IT;
  2388. hi2c->Devaddress = DevAddress;
  2389. /* If Memory address size is 8Bit */
  2390. if (MemAddSize == I2C_MEMADD_SIZE_8BIT)
  2391. {
  2392. /* Prefetch Memory Address */
  2393. hi2c->Instance->TXDR = I2C_MEM_ADD_LSB(MemAddress);
  2394. /* Reset Memaddress content */
  2395. hi2c->Memaddress = 0xFFFFFFFFU;
  2396. }
  2397. /* If Memory address size is 16Bit */
  2398. else
  2399. {
  2400. /* Prefetch Memory Address (MSB part, LSB will be manage through interrupt) */
  2401. hi2c->Instance->TXDR = I2C_MEM_ADD_MSB(MemAddress);
  2402. /* Prepare Memaddress buffer for LSB part */
  2403. hi2c->Memaddress = I2C_MEM_ADD_LSB(MemAddress);
  2404. }
  2405. /* Send Slave Address and Memory Address */
  2406. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)MemAddSize, I2C_RELOAD_MODE, I2C_GENERATE_START_WRITE);
  2407. /* Process Unlocked */
  2408. __HAL_UNLOCK(hi2c);
  2409. /* Note : The I2C interrupts must be enabled after unlocking current process
  2410. to avoid the risk of I2C interrupt handle execution before current
  2411. process unlock */
  2412. /* Enable ERR, TC, STOP, NACK, TXI interrupt */
  2413. /* possible to enable all of these */
  2414. /* I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI |
  2415. I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
  2416. I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT);
  2417. return HAL_OK;
  2418. }
  2419. else
  2420. {
  2421. return HAL_BUSY;
  2422. }
  2423. }
  2424. /**
  2425. * @brief Read an amount of data in non-blocking mode with Interrupt from a specific memory address
  2426. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2427. * the configuration information for the specified I2C.
  2428. * @param DevAddress Target device address: The device 7 bits address value
  2429. * in datasheet must be shifted to the left before calling the interface
  2430. * @param MemAddress Internal memory address
  2431. * @param MemAddSize Size of internal memory address
  2432. * @param pData Pointer to data buffer
  2433. * @param Size Amount of data to be sent
  2434. * @retval HAL status
  2435. */
  2436. HAL_StatusTypeDef HAL_I2C_Mem_Read_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress,
  2437. uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
  2438. {
  2439. /* Check the parameters */
  2440. assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
  2441. if (hi2c->State == HAL_I2C_STATE_READY)
  2442. {
  2443. if ((pData == NULL) || (Size == 0U))
  2444. {
  2445. hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM;
  2446. return HAL_ERROR;
  2447. }
  2448. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  2449. {
  2450. return HAL_BUSY;
  2451. }
  2452. /* Process Locked */
  2453. __HAL_LOCK(hi2c);
  2454. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  2455. hi2c->Mode = HAL_I2C_MODE_MEM;
  2456. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2457. /* Prepare transfer parameters */
  2458. hi2c->pBuffPtr = pData;
  2459. hi2c->XferCount = Size;
  2460. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  2461. hi2c->XferISR = I2C_Mem_ISR_IT;
  2462. hi2c->Devaddress = DevAddress;
  2463. /* If Memory address size is 8Bit */
  2464. if (MemAddSize == I2C_MEMADD_SIZE_8BIT)
  2465. {
  2466. /* Prefetch Memory Address */
  2467. hi2c->Instance->TXDR = I2C_MEM_ADD_LSB(MemAddress);
  2468. /* Reset Memaddress content */
  2469. hi2c->Memaddress = 0xFFFFFFFFU;
  2470. }
  2471. /* If Memory address size is 16Bit */
  2472. else
  2473. {
  2474. /* Prefetch Memory Address (MSB part, LSB will be manage through interrupt) */
  2475. hi2c->Instance->TXDR = I2C_MEM_ADD_MSB(MemAddress);
  2476. /* Prepare Memaddress buffer for LSB part */
  2477. hi2c->Memaddress = I2C_MEM_ADD_LSB(MemAddress);
  2478. }
  2479. /* Send Slave Address and Memory Address */
  2480. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)MemAddSize, I2C_SOFTEND_MODE, I2C_GENERATE_START_WRITE);
  2481. /* Process Unlocked */
  2482. __HAL_UNLOCK(hi2c);
  2483. /* Note : The I2C interrupts must be enabled after unlocking current process
  2484. to avoid the risk of I2C interrupt handle execution before current
  2485. process unlock */
  2486. /* Enable ERR, TC, STOP, NACK, TXI interrupt */
  2487. /* possible to enable all of these */
  2488. /* I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI |
  2489. I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
  2490. I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT);
  2491. return HAL_OK;
  2492. }
  2493. else
  2494. {
  2495. return HAL_BUSY;
  2496. }
  2497. }
  2498. /**
  2499. * @brief Write an amount of data in non-blocking mode with DMA to a specific memory address
  2500. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2501. * the configuration information for the specified I2C.
  2502. * @param DevAddress Target device address: The device 7 bits address value
  2503. * in datasheet must be shifted to the left before calling the interface
  2504. * @param MemAddress Internal memory address
  2505. * @param MemAddSize Size of internal memory address
  2506. * @param pData Pointer to data buffer
  2507. * @param Size Amount of data to be sent
  2508. * @retval HAL status
  2509. */
  2510. HAL_StatusTypeDef HAL_I2C_Mem_Write_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress,
  2511. uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
  2512. {
  2513. HAL_StatusTypeDef dmaxferstatus;
  2514. /* Check the parameters */
  2515. assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
  2516. if (hi2c->State == HAL_I2C_STATE_READY)
  2517. {
  2518. if ((pData == NULL) || (Size == 0U))
  2519. {
  2520. hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM;
  2521. return HAL_ERROR;
  2522. }
  2523. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  2524. {
  2525. return HAL_BUSY;
  2526. }
  2527. /* Process Locked */
  2528. __HAL_LOCK(hi2c);
  2529. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  2530. hi2c->Mode = HAL_I2C_MODE_MEM;
  2531. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2532. /* Prepare transfer parameters */
  2533. hi2c->pBuffPtr = pData;
  2534. hi2c->XferCount = Size;
  2535. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  2536. hi2c->XferISR = I2C_Mem_ISR_DMA;
  2537. hi2c->Devaddress = DevAddress;
  2538. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  2539. {
  2540. hi2c->XferSize = MAX_NBYTE_SIZE;
  2541. }
  2542. else
  2543. {
  2544. hi2c->XferSize = hi2c->XferCount;
  2545. }
  2546. /* If Memory address size is 8Bit */
  2547. if (MemAddSize == I2C_MEMADD_SIZE_8BIT)
  2548. {
  2549. /* Prefetch Memory Address */
  2550. hi2c->Instance->TXDR = I2C_MEM_ADD_LSB(MemAddress);
  2551. /* Reset Memaddress content */
  2552. hi2c->Memaddress = 0xFFFFFFFFU;
  2553. }
  2554. /* If Memory address size is 16Bit */
  2555. else
  2556. {
  2557. /* Prefetch Memory Address (MSB part, LSB will be manage through interrupt) */
  2558. hi2c->Instance->TXDR = I2C_MEM_ADD_MSB(MemAddress);
  2559. /* Prepare Memaddress buffer for LSB part */
  2560. hi2c->Memaddress = I2C_MEM_ADD_LSB(MemAddress);
  2561. }
  2562. if (hi2c->hdmatx != NULL)
  2563. {
  2564. /* Set the I2C DMA transfer complete callback */
  2565. hi2c->hdmatx->XferCpltCallback = I2C_DMAMasterTransmitCplt;
  2566. /* Set the DMA error callback */
  2567. hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
  2568. /* Set the unused DMA callbacks to NULL */
  2569. hi2c->hdmatx->XferHalfCpltCallback = NULL;
  2570. hi2c->hdmatx->XferAbortCallback = NULL;
  2571. /* Enable the DMA stream or channel depends on Instance */
  2572. dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->TXDR,
  2573. hi2c->XferSize);
  2574. }
  2575. else
  2576. {
  2577. /* Update I2C state */
  2578. hi2c->State = HAL_I2C_STATE_READY;
  2579. hi2c->Mode = HAL_I2C_MODE_NONE;
  2580. /* Update I2C error code */
  2581. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
  2582. /* Process Unlocked */
  2583. __HAL_UNLOCK(hi2c);
  2584. return HAL_ERROR;
  2585. }
  2586. if (dmaxferstatus == HAL_OK)
  2587. {
  2588. /* Send Slave Address and Memory Address */
  2589. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)MemAddSize, I2C_RELOAD_MODE, I2C_GENERATE_START_WRITE);
  2590. /* Process Unlocked */
  2591. __HAL_UNLOCK(hi2c);
  2592. /* Note : The I2C interrupts must be enabled after unlocking current process
  2593. to avoid the risk of I2C interrupt handle execution before current
  2594. process unlock */
  2595. /* Enable ERR, TC, STOP, NACK, TXI interrupt */
  2596. /* possible to enable all of these */
  2597. /* I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI |
  2598. I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
  2599. I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT);
  2600. }
  2601. else
  2602. {
  2603. /* Update I2C state */
  2604. hi2c->State = HAL_I2C_STATE_READY;
  2605. hi2c->Mode = HAL_I2C_MODE_NONE;
  2606. /* Update I2C error code */
  2607. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  2608. /* Process Unlocked */
  2609. __HAL_UNLOCK(hi2c);
  2610. return HAL_ERROR;
  2611. }
  2612. return HAL_OK;
  2613. }
  2614. else
  2615. {
  2616. return HAL_BUSY;
  2617. }
  2618. }
  2619. /**
  2620. * @brief Reads an amount of data in non-blocking mode with DMA from a specific memory address.
  2621. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2622. * the configuration information for the specified I2C.
  2623. * @param DevAddress Target device address: The device 7 bits address value
  2624. * in datasheet must be shifted to the left before calling the interface
  2625. * @param MemAddress Internal memory address
  2626. * @param MemAddSize Size of internal memory address
  2627. * @param pData Pointer to data buffer
  2628. * @param Size Amount of data to be read
  2629. * @retval HAL status
  2630. */
  2631. HAL_StatusTypeDef HAL_I2C_Mem_Read_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress,
  2632. uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
  2633. {
  2634. HAL_StatusTypeDef dmaxferstatus;
  2635. /* Check the parameters */
  2636. assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
  2637. if (hi2c->State == HAL_I2C_STATE_READY)
  2638. {
  2639. if ((pData == NULL) || (Size == 0U))
  2640. {
  2641. hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM;
  2642. return HAL_ERROR;
  2643. }
  2644. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  2645. {
  2646. return HAL_BUSY;
  2647. }
  2648. /* Process Locked */
  2649. __HAL_LOCK(hi2c);
  2650. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  2651. hi2c->Mode = HAL_I2C_MODE_MEM;
  2652. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2653. /* Prepare transfer parameters */
  2654. hi2c->pBuffPtr = pData;
  2655. hi2c->XferCount = Size;
  2656. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  2657. hi2c->XferISR = I2C_Mem_ISR_DMA;
  2658. hi2c->Devaddress = DevAddress;
  2659. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  2660. {
  2661. hi2c->XferSize = MAX_NBYTE_SIZE;
  2662. }
  2663. else
  2664. {
  2665. hi2c->XferSize = hi2c->XferCount;
  2666. }
  2667. /* If Memory address size is 8Bit */
  2668. if (MemAddSize == I2C_MEMADD_SIZE_8BIT)
  2669. {
  2670. /* Prefetch Memory Address */
  2671. hi2c->Instance->TXDR = I2C_MEM_ADD_LSB(MemAddress);
  2672. /* Reset Memaddress content */
  2673. hi2c->Memaddress = 0xFFFFFFFFU;
  2674. }
  2675. /* If Memory address size is 16Bit */
  2676. else
  2677. {
  2678. /* Prefetch Memory Address (MSB part, LSB will be manage through interrupt) */
  2679. hi2c->Instance->TXDR = I2C_MEM_ADD_MSB(MemAddress);
  2680. /* Prepare Memaddress buffer for LSB part */
  2681. hi2c->Memaddress = I2C_MEM_ADD_LSB(MemAddress);
  2682. }
  2683. if (hi2c->hdmarx != NULL)
  2684. {
  2685. /* Set the I2C DMA transfer complete callback */
  2686. hi2c->hdmarx->XferCpltCallback = I2C_DMAMasterReceiveCplt;
  2687. /* Set the DMA error callback */
  2688. hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
  2689. /* Set the unused DMA callbacks to NULL */
  2690. hi2c->hdmarx->XferHalfCpltCallback = NULL;
  2691. hi2c->hdmarx->XferAbortCallback = NULL;
  2692. /* Enable the DMA stream or channel depends on Instance */
  2693. dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)pData,
  2694. hi2c->XferSize);
  2695. }
  2696. else
  2697. {
  2698. /* Update I2C state */
  2699. hi2c->State = HAL_I2C_STATE_READY;
  2700. hi2c->Mode = HAL_I2C_MODE_NONE;
  2701. /* Update I2C error code */
  2702. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
  2703. /* Process Unlocked */
  2704. __HAL_UNLOCK(hi2c);
  2705. return HAL_ERROR;
  2706. }
  2707. if (dmaxferstatus == HAL_OK)
  2708. {
  2709. /* Send Slave Address and Memory Address */
  2710. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)MemAddSize, I2C_SOFTEND_MODE, I2C_GENERATE_START_WRITE);
  2711. /* Process Unlocked */
  2712. __HAL_UNLOCK(hi2c);
  2713. /* Note : The I2C interrupts must be enabled after unlocking current process
  2714. to avoid the risk of I2C interrupt handle execution before current
  2715. process unlock */
  2716. /* Enable ERR, TC, STOP, NACK, TXI interrupt */
  2717. /* possible to enable all of these */
  2718. /* I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI |
  2719. I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
  2720. I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT);
  2721. }
  2722. else
  2723. {
  2724. /* Update I2C state */
  2725. hi2c->State = HAL_I2C_STATE_READY;
  2726. hi2c->Mode = HAL_I2C_MODE_NONE;
  2727. /* Update I2C error code */
  2728. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  2729. /* Process Unlocked */
  2730. __HAL_UNLOCK(hi2c);
  2731. return HAL_ERROR;
  2732. }
  2733. return HAL_OK;
  2734. }
  2735. else
  2736. {
  2737. return HAL_BUSY;
  2738. }
  2739. }
  2740. /**
  2741. * @brief Checks if target device is ready for communication.
  2742. * @note This function is used with Memory devices
  2743. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2744. * the configuration information for the specified I2C.
  2745. * @param DevAddress Target device address: The device 7 bits address value
  2746. * in datasheet must be shifted to the left before calling the interface
  2747. * @param Trials Number of trials
  2748. * @param Timeout Timeout duration
  2749. * @retval HAL status
  2750. */
  2751. HAL_StatusTypeDef HAL_I2C_IsDeviceReady(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Trials,
  2752. uint32_t Timeout)
  2753. {
  2754. uint32_t tickstart;
  2755. __IO uint32_t I2C_Trials = 0UL;
  2756. FlagStatus tmp1;
  2757. FlagStatus tmp2;
  2758. if (hi2c->State == HAL_I2C_STATE_READY)
  2759. {
  2760. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  2761. {
  2762. return HAL_BUSY;
  2763. }
  2764. /* Process Locked */
  2765. __HAL_LOCK(hi2c);
  2766. hi2c->State = HAL_I2C_STATE_BUSY;
  2767. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2768. do
  2769. {
  2770. /* Generate Start */
  2771. hi2c->Instance->CR2 = I2C_GENERATE_START(hi2c->Init.AddressingMode, DevAddress);
  2772. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  2773. /* Wait until STOPF flag is set or a NACK flag is set*/
  2774. tickstart = HAL_GetTick();
  2775. tmp1 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF);
  2776. tmp2 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF);
  2777. while ((tmp1 == RESET) && (tmp2 == RESET))
  2778. {
  2779. if (Timeout != HAL_MAX_DELAY)
  2780. {
  2781. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  2782. {
  2783. /* Update I2C state */
  2784. hi2c->State = HAL_I2C_STATE_READY;
  2785. /* Update I2C error code */
  2786. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  2787. /* Process Unlocked */
  2788. __HAL_UNLOCK(hi2c);
  2789. return HAL_ERROR;
  2790. }
  2791. }
  2792. tmp1 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF);
  2793. tmp2 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF);
  2794. }
  2795. /* Check if the NACKF flag has not been set */
  2796. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == RESET)
  2797. {
  2798. /* Wait until STOPF flag is reset */
  2799. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_STOPF, RESET, Timeout, tickstart) != HAL_OK)
  2800. {
  2801. return HAL_ERROR;
  2802. }
  2803. /* Clear STOP Flag */
  2804. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  2805. /* Device is ready */
  2806. hi2c->State = HAL_I2C_STATE_READY;
  2807. /* Process Unlocked */
  2808. __HAL_UNLOCK(hi2c);
  2809. return HAL_OK;
  2810. }
  2811. else
  2812. {
  2813. /* Wait until STOPF flag is reset */
  2814. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_STOPF, RESET, Timeout, tickstart) != HAL_OK)
  2815. {
  2816. return HAL_ERROR;
  2817. }
  2818. /* Clear NACK Flag */
  2819. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  2820. /* Clear STOP Flag, auto generated with autoend*/
  2821. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  2822. }
  2823. /* Increment Trials */
  2824. I2C_Trials++;
  2825. } while (I2C_Trials < Trials);
  2826. /* Update I2C state */
  2827. hi2c->State = HAL_I2C_STATE_READY;
  2828. /* Update I2C error code */
  2829. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  2830. /* Process Unlocked */
  2831. __HAL_UNLOCK(hi2c);
  2832. return HAL_ERROR;
  2833. }
  2834. else
  2835. {
  2836. return HAL_BUSY;
  2837. }
  2838. }
  2839. /**
  2840. * @brief Sequential transmit in master I2C mode an amount of data in non-blocking mode with Interrupt.
  2841. * @note This interface allow to manage repeated start condition when a direction change during transfer
  2842. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2843. * the configuration information for the specified I2C.
  2844. * @param DevAddress Target device address: The device 7 bits address value
  2845. * in datasheet must be shifted to the left before calling the interface
  2846. * @param pData Pointer to data buffer
  2847. * @param Size Amount of data to be sent
  2848. * @param XferOptions Options of Transfer, value of @ref I2C_XFEROPTIONS
  2849. * @retval HAL status
  2850. */
  2851. HAL_StatusTypeDef HAL_I2C_Master_Seq_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData,
  2852. uint16_t Size, uint32_t XferOptions)
  2853. {
  2854. uint32_t xfermode;
  2855. uint32_t xferrequest = I2C_GENERATE_START_WRITE;
  2856. uint32_t sizetoxfer = 0U;
  2857. /* Check the parameters */
  2858. assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  2859. if (hi2c->State == HAL_I2C_STATE_READY)
  2860. {
  2861. /* Process Locked */
  2862. __HAL_LOCK(hi2c);
  2863. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  2864. hi2c->Mode = HAL_I2C_MODE_MASTER;
  2865. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2866. /* Prepare transfer parameters */
  2867. hi2c->pBuffPtr = pData;
  2868. hi2c->XferCount = Size;
  2869. hi2c->XferOptions = XferOptions;
  2870. hi2c->XferISR = I2C_Master_ISR_IT;
  2871. /* If hi2c->XferCount > MAX_NBYTE_SIZE, use reload mode */
  2872. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  2873. {
  2874. hi2c->XferSize = MAX_NBYTE_SIZE;
  2875. xfermode = I2C_RELOAD_MODE;
  2876. }
  2877. else
  2878. {
  2879. hi2c->XferSize = hi2c->XferCount;
  2880. xfermode = hi2c->XferOptions;
  2881. }
  2882. if ((hi2c->XferSize > 0U) && ((XferOptions == I2C_FIRST_FRAME) || \
  2883. (XferOptions == I2C_FIRST_AND_LAST_FRAME)))
  2884. {
  2885. /* Preload TX register */
  2886. /* Write data to TXDR */
  2887. hi2c->Instance->TXDR = *hi2c->pBuffPtr;
  2888. /* Increment Buffer pointer */
  2889. hi2c->pBuffPtr++;
  2890. sizetoxfer = hi2c->XferSize;
  2891. hi2c->XferCount--;
  2892. hi2c->XferSize--;
  2893. }
  2894. /* If transfer direction not change and there is no request to start another frame,
  2895. do not generate Restart Condition */
  2896. /* Mean Previous state is same as current state */
  2897. if ((hi2c->PreviousState == I2C_STATE_MASTER_BUSY_TX) && \
  2898. (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 0))
  2899. {
  2900. xferrequest = I2C_NO_STARTSTOP;
  2901. }
  2902. else
  2903. {
  2904. /* Convert OTHER_xxx XferOptions if any */
  2905. I2C_ConvertOtherXferOptions(hi2c);
  2906. /* Update xfermode accordingly if no reload is necessary */
  2907. if (hi2c->XferCount <= MAX_NBYTE_SIZE)
  2908. {
  2909. xfermode = hi2c->XferOptions;
  2910. }
  2911. }
  2912. /* Send Slave Address and set NBYTES to write */
  2913. if ((XferOptions == I2C_FIRST_FRAME) || (XferOptions == I2C_FIRST_AND_LAST_FRAME))
  2914. {
  2915. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)sizetoxfer, xfermode, xferrequest);
  2916. }
  2917. else
  2918. {
  2919. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, xferrequest);
  2920. }
  2921. /* Process Unlocked */
  2922. __HAL_UNLOCK(hi2c);
  2923. /* Note : The I2C interrupts must be enabled after unlocking current process
  2924. to avoid the risk of I2C interrupt handle execution before current
  2925. process unlock */
  2926. /* Enable ERR, TC, STOP, NACK, TXI interrupt */
  2927. /* possible to enable all of these */
  2928. /* I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI |
  2929. I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
  2930. I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT);
  2931. return HAL_OK;
  2932. }
  2933. else
  2934. {
  2935. return HAL_BUSY;
  2936. }
  2937. }
  2938. /**
  2939. * @brief Sequential transmit in master I2C mode an amount of data in non-blocking mode with DMA.
  2940. * @note This interface allow to manage repeated start condition when a direction change during transfer
  2941. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2942. * the configuration information for the specified I2C.
  2943. * @param DevAddress Target device address: The device 7 bits address value
  2944. * in datasheet must be shifted to the left before calling the interface
  2945. * @param pData Pointer to data buffer
  2946. * @param Size Amount of data to be sent
  2947. * @param XferOptions Options of Transfer, value of @ref I2C_XFEROPTIONS
  2948. * @retval HAL status
  2949. */
  2950. HAL_StatusTypeDef HAL_I2C_Master_Seq_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData,
  2951. uint16_t Size, uint32_t XferOptions)
  2952. {
  2953. uint32_t xfermode;
  2954. uint32_t xferrequest = I2C_GENERATE_START_WRITE;
  2955. HAL_StatusTypeDef dmaxferstatus;
  2956. uint32_t sizetoxfer = 0U;
  2957. /* Check the parameters */
  2958. assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  2959. if (hi2c->State == HAL_I2C_STATE_READY)
  2960. {
  2961. /* Process Locked */
  2962. __HAL_LOCK(hi2c);
  2963. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  2964. hi2c->Mode = HAL_I2C_MODE_MASTER;
  2965. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2966. /* Prepare transfer parameters */
  2967. hi2c->pBuffPtr = pData;
  2968. hi2c->XferCount = Size;
  2969. hi2c->XferOptions = XferOptions;
  2970. hi2c->XferISR = I2C_Master_ISR_DMA;
  2971. /* If hi2c->XferCount > MAX_NBYTE_SIZE, use reload mode */
  2972. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  2973. {
  2974. hi2c->XferSize = MAX_NBYTE_SIZE;
  2975. xfermode = I2C_RELOAD_MODE;
  2976. }
  2977. else
  2978. {
  2979. hi2c->XferSize = hi2c->XferCount;
  2980. xfermode = hi2c->XferOptions;
  2981. }
  2982. if ((hi2c->XferSize > 0U) && ((XferOptions == I2C_FIRST_FRAME) || \
  2983. (XferOptions == I2C_FIRST_AND_LAST_FRAME)))
  2984. {
  2985. /* Preload TX register */
  2986. /* Write data to TXDR */
  2987. hi2c->Instance->TXDR = *hi2c->pBuffPtr;
  2988. /* Increment Buffer pointer */
  2989. hi2c->pBuffPtr++;
  2990. sizetoxfer = hi2c->XferSize;
  2991. hi2c->XferCount--;
  2992. hi2c->XferSize--;
  2993. }
  2994. /* If transfer direction not change and there is no request to start another frame,
  2995. do not generate Restart Condition */
  2996. /* Mean Previous state is same as current state */
  2997. if ((hi2c->PreviousState == I2C_STATE_MASTER_BUSY_TX) && \
  2998. (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 0))
  2999. {
  3000. xferrequest = I2C_NO_STARTSTOP;
  3001. }
  3002. else
  3003. {
  3004. /* Convert OTHER_xxx XferOptions if any */
  3005. I2C_ConvertOtherXferOptions(hi2c);
  3006. /* Update xfermode accordingly if no reload is necessary */
  3007. if (hi2c->XferCount <= MAX_NBYTE_SIZE)
  3008. {
  3009. xfermode = hi2c->XferOptions;
  3010. }
  3011. }
  3012. if (hi2c->XferSize > 0U)
  3013. {
  3014. if (hi2c->hdmatx != NULL)
  3015. {
  3016. /* Set the I2C DMA transfer complete callback */
  3017. hi2c->hdmatx->XferCpltCallback = I2C_DMAMasterTransmitCplt;
  3018. /* Set the DMA error callback */
  3019. hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
  3020. /* Set the unused DMA callbacks to NULL */
  3021. hi2c->hdmatx->XferHalfCpltCallback = NULL;
  3022. hi2c->hdmatx->XferAbortCallback = NULL;
  3023. /* Enable the DMA stream or channel depends on Instance */
  3024. dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr,
  3025. (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);
  3026. }
  3027. else
  3028. {
  3029. /* Update I2C state */
  3030. hi2c->State = HAL_I2C_STATE_READY;
  3031. hi2c->Mode = HAL_I2C_MODE_NONE;
  3032. /* Update I2C error code */
  3033. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
  3034. /* Process Unlocked */
  3035. __HAL_UNLOCK(hi2c);
  3036. return HAL_ERROR;
  3037. }
  3038. if (dmaxferstatus == HAL_OK)
  3039. {
  3040. /* Send Slave Address and set NBYTES to write */
  3041. if ((XferOptions == I2C_FIRST_FRAME) || (XferOptions == I2C_FIRST_AND_LAST_FRAME))
  3042. {
  3043. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)sizetoxfer, xfermode, xferrequest);
  3044. }
  3045. else
  3046. {
  3047. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, xferrequest);
  3048. }
  3049. /* Update XferCount value */
  3050. hi2c->XferCount -= hi2c->XferSize;
  3051. /* Process Unlocked */
  3052. __HAL_UNLOCK(hi2c);
  3053. /* Note : The I2C interrupts must be enabled after unlocking current process
  3054. to avoid the risk of I2C interrupt handle execution before current
  3055. process unlock */
  3056. /* Enable ERR and NACK interrupts */
  3057. I2C_Enable_IRQ(hi2c, I2C_XFER_ERROR_IT);
  3058. /* Enable DMA Request */
  3059. hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
  3060. }
  3061. else
  3062. {
  3063. /* Update I2C state */
  3064. hi2c->State = HAL_I2C_STATE_READY;
  3065. hi2c->Mode = HAL_I2C_MODE_NONE;
  3066. /* Update I2C error code */
  3067. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  3068. /* Process Unlocked */
  3069. __HAL_UNLOCK(hi2c);
  3070. return HAL_ERROR;
  3071. }
  3072. }
  3073. else
  3074. {
  3075. /* Update Transfer ISR function pointer */
  3076. hi2c->XferISR = I2C_Master_ISR_IT;
  3077. /* Send Slave Address */
  3078. /* Set NBYTES to write and generate START condition */
  3079. if ((XferOptions == I2C_FIRST_FRAME) || (XferOptions == I2C_FIRST_AND_LAST_FRAME))
  3080. {
  3081. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)sizetoxfer, xfermode, xferrequest);
  3082. }
  3083. else
  3084. {
  3085. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, xferrequest);
  3086. }
  3087. /* Process Unlocked */
  3088. __HAL_UNLOCK(hi2c);
  3089. /* Note : The I2C interrupts must be enabled after unlocking current process
  3090. to avoid the risk of I2C interrupt handle execution before current
  3091. process unlock */
  3092. /* Enable ERR, TC, STOP, NACK, TXI interrupt */
  3093. /* possible to enable all of these */
  3094. /* I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI |
  3095. I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
  3096. I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT);
  3097. }
  3098. return HAL_OK;
  3099. }
  3100. else
  3101. {
  3102. return HAL_BUSY;
  3103. }
  3104. }
  3105. /**
  3106. * @brief Sequential receive in master I2C mode an amount of data in non-blocking mode with Interrupt
  3107. * @note This interface allow to manage repeated start condition when a direction change during transfer
  3108. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3109. * the configuration information for the specified I2C.
  3110. * @param DevAddress Target device address: The device 7 bits address value
  3111. * in datasheet must be shifted to the left before calling the interface
  3112. * @param pData Pointer to data buffer
  3113. * @param Size Amount of data to be sent
  3114. * @param XferOptions Options of Transfer, value of @ref I2C_XFEROPTIONS
  3115. * @retval HAL status
  3116. */
  3117. HAL_StatusTypeDef HAL_I2C_Master_Seq_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData,
  3118. uint16_t Size, uint32_t XferOptions)
  3119. {
  3120. uint32_t xfermode;
  3121. uint32_t xferrequest = I2C_GENERATE_START_READ;
  3122. /* Check the parameters */
  3123. assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  3124. if (hi2c->State == HAL_I2C_STATE_READY)
  3125. {
  3126. /* Process Locked */
  3127. __HAL_LOCK(hi2c);
  3128. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  3129. hi2c->Mode = HAL_I2C_MODE_MASTER;
  3130. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  3131. /* Prepare transfer parameters */
  3132. hi2c->pBuffPtr = pData;
  3133. hi2c->XferCount = Size;
  3134. hi2c->XferOptions = XferOptions;
  3135. hi2c->XferISR = I2C_Master_ISR_IT;
  3136. /* If hi2c->XferCount > MAX_NBYTE_SIZE, use reload mode */
  3137. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  3138. {
  3139. hi2c->XferSize = MAX_NBYTE_SIZE;
  3140. xfermode = I2C_RELOAD_MODE;
  3141. }
  3142. else
  3143. {
  3144. hi2c->XferSize = hi2c->XferCount;
  3145. xfermode = hi2c->XferOptions;
  3146. }
  3147. /* If transfer direction not change and there is no request to start another frame,
  3148. do not generate Restart Condition */
  3149. /* Mean Previous state is same as current state */
  3150. if ((hi2c->PreviousState == I2C_STATE_MASTER_BUSY_RX) && \
  3151. (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 0))
  3152. {
  3153. xferrequest = I2C_NO_STARTSTOP;
  3154. }
  3155. else
  3156. {
  3157. /* Convert OTHER_xxx XferOptions if any */
  3158. I2C_ConvertOtherXferOptions(hi2c);
  3159. /* Update xfermode accordingly if no reload is necessary */
  3160. if (hi2c->XferCount <= MAX_NBYTE_SIZE)
  3161. {
  3162. xfermode = hi2c->XferOptions;
  3163. }
  3164. }
  3165. /* Send Slave Address and set NBYTES to read */
  3166. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, xferrequest);
  3167. /* Process Unlocked */
  3168. __HAL_UNLOCK(hi2c);
  3169. /* Note : The I2C interrupts must be enabled after unlocking current process
  3170. to avoid the risk of I2C interrupt handle execution before current
  3171. process unlock */
  3172. I2C_Enable_IRQ(hi2c, I2C_XFER_RX_IT);
  3173. return HAL_OK;
  3174. }
  3175. else
  3176. {
  3177. return HAL_BUSY;
  3178. }
  3179. }
  3180. /**
  3181. * @brief Sequential receive in master I2C mode an amount of data in non-blocking mode with DMA
  3182. * @note This interface allow to manage repeated start condition when a direction change during transfer
  3183. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3184. * the configuration information for the specified I2C.
  3185. * @param DevAddress Target device address: The device 7 bits address value
  3186. * in datasheet must be shifted to the left before calling the interface
  3187. * @param pData Pointer to data buffer
  3188. * @param Size Amount of data to be sent
  3189. * @param XferOptions Options of Transfer, value of @ref I2C_XFEROPTIONS
  3190. * @retval HAL status
  3191. */
  3192. HAL_StatusTypeDef HAL_I2C_Master_Seq_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData,
  3193. uint16_t Size, uint32_t XferOptions)
  3194. {
  3195. uint32_t xfermode;
  3196. uint32_t xferrequest = I2C_GENERATE_START_READ;
  3197. HAL_StatusTypeDef dmaxferstatus;
  3198. /* Check the parameters */
  3199. assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  3200. if (hi2c->State == HAL_I2C_STATE_READY)
  3201. {
  3202. /* Process Locked */
  3203. __HAL_LOCK(hi2c);
  3204. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  3205. hi2c->Mode = HAL_I2C_MODE_MASTER;
  3206. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  3207. /* Prepare transfer parameters */
  3208. hi2c->pBuffPtr = pData;
  3209. hi2c->XferCount = Size;
  3210. hi2c->XferOptions = XferOptions;
  3211. hi2c->XferISR = I2C_Master_ISR_DMA;
  3212. /* If hi2c->XferCount > MAX_NBYTE_SIZE, use reload mode */
  3213. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  3214. {
  3215. hi2c->XferSize = MAX_NBYTE_SIZE;
  3216. xfermode = I2C_RELOAD_MODE;
  3217. }
  3218. else
  3219. {
  3220. hi2c->XferSize = hi2c->XferCount;
  3221. xfermode = hi2c->XferOptions;
  3222. }
  3223. /* If transfer direction not change and there is no request to start another frame,
  3224. do not generate Restart Condition */
  3225. /* Mean Previous state is same as current state */
  3226. if ((hi2c->PreviousState == I2C_STATE_MASTER_BUSY_RX) && \
  3227. (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 0))
  3228. {
  3229. xferrequest = I2C_NO_STARTSTOP;
  3230. }
  3231. else
  3232. {
  3233. /* Convert OTHER_xxx XferOptions if any */
  3234. I2C_ConvertOtherXferOptions(hi2c);
  3235. /* Update xfermode accordingly if no reload is necessary */
  3236. if (hi2c->XferCount <= MAX_NBYTE_SIZE)
  3237. {
  3238. xfermode = hi2c->XferOptions;
  3239. }
  3240. }
  3241. if (hi2c->XferSize > 0U)
  3242. {
  3243. if (hi2c->hdmarx != NULL)
  3244. {
  3245. /* Set the I2C DMA transfer complete callback */
  3246. hi2c->hdmarx->XferCpltCallback = I2C_DMAMasterReceiveCplt;
  3247. /* Set the DMA error callback */
  3248. hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
  3249. /* Set the unused DMA callbacks to NULL */
  3250. hi2c->hdmarx->XferHalfCpltCallback = NULL;
  3251. hi2c->hdmarx->XferAbortCallback = NULL;
  3252. /* Enable the DMA stream or channel depends on Instance */
  3253. dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)pData,
  3254. hi2c->XferSize);
  3255. }
  3256. else
  3257. {
  3258. /* Update I2C state */
  3259. hi2c->State = HAL_I2C_STATE_READY;
  3260. hi2c->Mode = HAL_I2C_MODE_NONE;
  3261. /* Update I2C error code */
  3262. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
  3263. /* Process Unlocked */
  3264. __HAL_UNLOCK(hi2c);
  3265. return HAL_ERROR;
  3266. }
  3267. if (dmaxferstatus == HAL_OK)
  3268. {
  3269. /* Send Slave Address and set NBYTES to read */
  3270. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, xferrequest);
  3271. /* Update XferCount value */
  3272. hi2c->XferCount -= hi2c->XferSize;
  3273. /* Process Unlocked */
  3274. __HAL_UNLOCK(hi2c);
  3275. /* Note : The I2C interrupts must be enabled after unlocking current process
  3276. to avoid the risk of I2C interrupt handle execution before current
  3277. process unlock */
  3278. /* Enable ERR and NACK interrupts */
  3279. I2C_Enable_IRQ(hi2c, I2C_XFER_ERROR_IT);
  3280. /* Enable DMA Request */
  3281. hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
  3282. }
  3283. else
  3284. {
  3285. /* Update I2C state */
  3286. hi2c->State = HAL_I2C_STATE_READY;
  3287. hi2c->Mode = HAL_I2C_MODE_NONE;
  3288. /* Update I2C error code */
  3289. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  3290. /* Process Unlocked */
  3291. __HAL_UNLOCK(hi2c);
  3292. return HAL_ERROR;
  3293. }
  3294. }
  3295. else
  3296. {
  3297. /* Update Transfer ISR function pointer */
  3298. hi2c->XferISR = I2C_Master_ISR_IT;
  3299. /* Send Slave Address */
  3300. /* Set NBYTES to read and generate START condition */
  3301. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE,
  3302. I2C_GENERATE_START_READ);
  3303. /* Process Unlocked */
  3304. __HAL_UNLOCK(hi2c);
  3305. /* Note : The I2C interrupts must be enabled after unlocking current process
  3306. to avoid the risk of I2C interrupt handle execution before current
  3307. process unlock */
  3308. /* Enable ERR, TC, STOP, NACK, RXI interrupt */
  3309. /* possible to enable all of these */
  3310. /* I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI |
  3311. I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
  3312. I2C_Enable_IRQ(hi2c, I2C_XFER_RX_IT);
  3313. }
  3314. return HAL_OK;
  3315. }
  3316. else
  3317. {
  3318. return HAL_BUSY;
  3319. }
  3320. }
  3321. /**
  3322. * @brief Sequential transmit in slave/device I2C mode an amount of data in non-blocking mode with Interrupt
  3323. * @note This interface allow to manage repeated start condition when a direction change during transfer
  3324. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3325. * the configuration information for the specified I2C.
  3326. * @param pData Pointer to data buffer
  3327. * @param Size Amount of data to be sent
  3328. * @param XferOptions Options of Transfer, value of @ref I2C_XFEROPTIONS
  3329. * @retval HAL status
  3330. */
  3331. HAL_StatusTypeDef HAL_I2C_Slave_Seq_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size,
  3332. uint32_t XferOptions)
  3333. {
  3334. /* Declaration of tmp to prevent undefined behavior of volatile usage */
  3335. FlagStatus tmp;
  3336. /* Check the parameters */
  3337. assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  3338. if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
  3339. {
  3340. if ((pData == NULL) || (Size == 0U))
  3341. {
  3342. hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM;
  3343. return HAL_ERROR;
  3344. }
  3345. /* Disable Interrupts, to prevent preemption during treatment in case of multicall */
  3346. I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_TX_IT);
  3347. /* Process Locked */
  3348. __HAL_LOCK(hi2c);
  3349. /* I2C cannot manage full duplex exchange so disable previous IT enabled if any */
  3350. /* and then toggle the HAL slave RX state to TX state */
  3351. if (hi2c->State == HAL_I2C_STATE_BUSY_RX_LISTEN)
  3352. {
  3353. /* Disable associated Interrupts */
  3354. I2C_Disable_IRQ(hi2c, I2C_XFER_RX_IT);
  3355. /* Abort DMA Xfer if any */
  3356. if ((hi2c->Instance->CR1 & I2C_CR1_RXDMAEN) == I2C_CR1_RXDMAEN)
  3357. {
  3358. hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
  3359. if (hi2c->hdmarx != NULL)
  3360. {
  3361. /* Set the I2C DMA Abort callback :
  3362. will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
  3363. hi2c->hdmarx->XferAbortCallback = I2C_DMAAbort;
  3364. /* Abort DMA RX */
  3365. if (HAL_DMA_Abort_IT(hi2c->hdmarx) != HAL_OK)
  3366. {
  3367. /* Call Directly XferAbortCallback function in case of error */
  3368. hi2c->hdmarx->XferAbortCallback(hi2c->hdmarx);
  3369. }
  3370. }
  3371. }
  3372. }
  3373. hi2c->State = HAL_I2C_STATE_BUSY_TX_LISTEN;
  3374. hi2c->Mode = HAL_I2C_MODE_SLAVE;
  3375. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  3376. /* Enable Address Acknowledge */
  3377. hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
  3378. /* Prepare transfer parameters */
  3379. hi2c->pBuffPtr = pData;
  3380. hi2c->XferCount = Size;
  3381. hi2c->XferSize = hi2c->XferCount;
  3382. hi2c->XferOptions = XferOptions;
  3383. hi2c->XferISR = I2C_Slave_ISR_IT;
  3384. tmp = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR);
  3385. if ((I2C_GET_DIR(hi2c) == I2C_DIRECTION_RECEIVE) && (tmp != RESET))
  3386. {
  3387. /* Clear ADDR flag after prepare the transfer parameters */
  3388. /* This action will generate an acknowledge to the Master */
  3389. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
  3390. }
  3391. /* Process Unlocked */
  3392. __HAL_UNLOCK(hi2c);
  3393. /* Note : The I2C interrupts must be enabled after unlocking current process
  3394. to avoid the risk of I2C interrupt handle execution before current
  3395. process unlock */
  3396. /* REnable ADDR interrupt */
  3397. I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT | I2C_XFER_LISTEN_IT);
  3398. return HAL_OK;
  3399. }
  3400. else
  3401. {
  3402. return HAL_ERROR;
  3403. }
  3404. }
  3405. /**
  3406. * @brief Sequential transmit in slave/device I2C mode an amount of data in non-blocking mode with DMA
  3407. * @note This interface allow to manage repeated start condition when a direction change during transfer
  3408. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3409. * the configuration information for the specified I2C.
  3410. * @param pData Pointer to data buffer
  3411. * @param Size Amount of data to be sent
  3412. * @param XferOptions Options of Transfer, value of @ref I2C_XFEROPTIONS
  3413. * @retval HAL status
  3414. */
  3415. HAL_StatusTypeDef HAL_I2C_Slave_Seq_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size,
  3416. uint32_t XferOptions)
  3417. {
  3418. /* Declaration of tmp to prevent undefined behavior of volatile usage */
  3419. FlagStatus tmp;
  3420. HAL_StatusTypeDef dmaxferstatus;
  3421. /* Check the parameters */
  3422. assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  3423. if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
  3424. {
  3425. if ((pData == NULL) || (Size == 0U))
  3426. {
  3427. hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM;
  3428. return HAL_ERROR;
  3429. }
  3430. /* Process Locked */
  3431. __HAL_LOCK(hi2c);
  3432. /* Disable Interrupts, to prevent preemption during treatment in case of multicall */
  3433. I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_TX_IT);
  3434. /* I2C cannot manage full duplex exchange so disable previous IT enabled if any */
  3435. /* and then toggle the HAL slave RX state to TX state */
  3436. if (hi2c->State == HAL_I2C_STATE_BUSY_RX_LISTEN)
  3437. {
  3438. /* Disable associated Interrupts */
  3439. I2C_Disable_IRQ(hi2c, I2C_XFER_RX_IT);
  3440. if ((hi2c->Instance->CR1 & I2C_CR1_RXDMAEN) == I2C_CR1_RXDMAEN)
  3441. {
  3442. /* Abort DMA Xfer if any */
  3443. if (hi2c->hdmarx != NULL)
  3444. {
  3445. hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
  3446. /* Set the I2C DMA Abort callback :
  3447. will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
  3448. hi2c->hdmarx->XferAbortCallback = I2C_DMAAbort;
  3449. /* Abort DMA RX */
  3450. if (HAL_DMA_Abort_IT(hi2c->hdmarx) != HAL_OK)
  3451. {
  3452. /* Call Directly XferAbortCallback function in case of error */
  3453. hi2c->hdmarx->XferAbortCallback(hi2c->hdmarx);
  3454. }
  3455. }
  3456. }
  3457. }
  3458. else if (hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN)
  3459. {
  3460. if ((hi2c->Instance->CR1 & I2C_CR1_TXDMAEN) == I2C_CR1_TXDMAEN)
  3461. {
  3462. hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
  3463. /* Abort DMA Xfer if any */
  3464. if (hi2c->hdmatx != NULL)
  3465. {
  3466. /* Set the I2C DMA Abort callback :
  3467. will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
  3468. hi2c->hdmatx->XferAbortCallback = I2C_DMAAbort;
  3469. /* Abort DMA TX */
  3470. if (HAL_DMA_Abort_IT(hi2c->hdmatx) != HAL_OK)
  3471. {
  3472. /* Call Directly XferAbortCallback function in case of error */
  3473. hi2c->hdmatx->XferAbortCallback(hi2c->hdmatx);
  3474. }
  3475. }
  3476. }
  3477. }
  3478. else
  3479. {
  3480. /* Nothing to do */
  3481. }
  3482. hi2c->State = HAL_I2C_STATE_BUSY_TX_LISTEN;
  3483. hi2c->Mode = HAL_I2C_MODE_SLAVE;
  3484. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  3485. /* Enable Address Acknowledge */
  3486. hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
  3487. /* Prepare transfer parameters */
  3488. hi2c->pBuffPtr = pData;
  3489. hi2c->XferCount = Size;
  3490. hi2c->XferSize = hi2c->XferCount;
  3491. hi2c->XferOptions = XferOptions;
  3492. hi2c->XferISR = I2C_Slave_ISR_DMA;
  3493. if (hi2c->hdmatx != NULL)
  3494. {
  3495. /* Set the I2C DMA transfer complete callback */
  3496. hi2c->hdmatx->XferCpltCallback = I2C_DMASlaveTransmitCplt;
  3497. /* Set the DMA error callback */
  3498. hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
  3499. /* Set the unused DMA callbacks to NULL */
  3500. hi2c->hdmatx->XferHalfCpltCallback = NULL;
  3501. hi2c->hdmatx->XferAbortCallback = NULL;
  3502. /* Enable the DMA stream or channel depends on Instance */
  3503. dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->TXDR,
  3504. hi2c->XferSize);
  3505. }
  3506. else
  3507. {
  3508. /* Update I2C state */
  3509. hi2c->State = HAL_I2C_STATE_LISTEN;
  3510. hi2c->Mode = HAL_I2C_MODE_NONE;
  3511. /* Update I2C error code */
  3512. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
  3513. /* Process Unlocked */
  3514. __HAL_UNLOCK(hi2c);
  3515. return HAL_ERROR;
  3516. }
  3517. if (dmaxferstatus == HAL_OK)
  3518. {
  3519. /* Update XferCount value */
  3520. hi2c->XferCount -= hi2c->XferSize;
  3521. /* Reset XferSize */
  3522. hi2c->XferSize = 0;
  3523. }
  3524. else
  3525. {
  3526. /* Update I2C state */
  3527. hi2c->State = HAL_I2C_STATE_LISTEN;
  3528. hi2c->Mode = HAL_I2C_MODE_NONE;
  3529. /* Update I2C error code */
  3530. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  3531. /* Process Unlocked */
  3532. __HAL_UNLOCK(hi2c);
  3533. return HAL_ERROR;
  3534. }
  3535. tmp = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR);
  3536. if ((I2C_GET_DIR(hi2c) == I2C_DIRECTION_RECEIVE) && (tmp != RESET))
  3537. {
  3538. /* Clear ADDR flag after prepare the transfer parameters */
  3539. /* This action will generate an acknowledge to the Master */
  3540. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
  3541. }
  3542. /* Process Unlocked */
  3543. __HAL_UNLOCK(hi2c);
  3544. /* Enable DMA Request */
  3545. hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
  3546. /* Note : The I2C interrupts must be enabled after unlocking current process
  3547. to avoid the risk of I2C interrupt handle execution before current
  3548. process unlock */
  3549. /* Enable ERR, STOP, NACK, ADDR interrupts */
  3550. I2C_Enable_IRQ(hi2c, I2C_XFER_LISTEN_IT);
  3551. return HAL_OK;
  3552. }
  3553. else
  3554. {
  3555. return HAL_ERROR;
  3556. }
  3557. }
  3558. /**
  3559. * @brief Sequential receive in slave/device I2C mode an amount of data in non-blocking mode with Interrupt
  3560. * @note This interface allow to manage repeated start condition when a direction change during transfer
  3561. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3562. * the configuration information for the specified I2C.
  3563. * @param pData Pointer to data buffer
  3564. * @param Size Amount of data to be sent
  3565. * @param XferOptions Options of Transfer, value of @ref I2C_XFEROPTIONS
  3566. * @retval HAL status
  3567. */
  3568. HAL_StatusTypeDef HAL_I2C_Slave_Seq_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size,
  3569. uint32_t XferOptions)
  3570. {
  3571. /* Declaration of tmp to prevent undefined behavior of volatile usage */
  3572. FlagStatus tmp;
  3573. /* Check the parameters */
  3574. assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  3575. if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
  3576. {
  3577. if ((pData == NULL) || (Size == 0U))
  3578. {
  3579. hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM;
  3580. return HAL_ERROR;
  3581. }
  3582. /* Disable Interrupts, to prevent preemption during treatment in case of multicall */
  3583. I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_RX_IT);
  3584. /* Process Locked */
  3585. __HAL_LOCK(hi2c);
  3586. /* I2C cannot manage full duplex exchange so disable previous IT enabled if any */
  3587. /* and then toggle the HAL slave TX state to RX state */
  3588. if (hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN)
  3589. {
  3590. /* Disable associated Interrupts */
  3591. I2C_Disable_IRQ(hi2c, I2C_XFER_TX_IT);
  3592. if ((hi2c->Instance->CR1 & I2C_CR1_TXDMAEN) == I2C_CR1_TXDMAEN)
  3593. {
  3594. hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
  3595. /* Abort DMA Xfer if any */
  3596. if (hi2c->hdmatx != NULL)
  3597. {
  3598. /* Set the I2C DMA Abort callback :
  3599. will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
  3600. hi2c->hdmatx->XferAbortCallback = I2C_DMAAbort;
  3601. /* Abort DMA TX */
  3602. if (HAL_DMA_Abort_IT(hi2c->hdmatx) != HAL_OK)
  3603. {
  3604. /* Call Directly XferAbortCallback function in case of error */
  3605. hi2c->hdmatx->XferAbortCallback(hi2c->hdmatx);
  3606. }
  3607. }
  3608. }
  3609. }
  3610. hi2c->State = HAL_I2C_STATE_BUSY_RX_LISTEN;
  3611. hi2c->Mode = HAL_I2C_MODE_SLAVE;
  3612. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  3613. /* Enable Address Acknowledge */
  3614. hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
  3615. /* Prepare transfer parameters */
  3616. hi2c->pBuffPtr = pData;
  3617. hi2c->XferCount = Size;
  3618. hi2c->XferSize = hi2c->XferCount;
  3619. hi2c->XferOptions = XferOptions;
  3620. hi2c->XferISR = I2C_Slave_ISR_IT;
  3621. tmp = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR);
  3622. if ((I2C_GET_DIR(hi2c) == I2C_DIRECTION_TRANSMIT) && (tmp != RESET))
  3623. {
  3624. /* Clear ADDR flag after prepare the transfer parameters */
  3625. /* This action will generate an acknowledge to the Master */
  3626. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
  3627. }
  3628. /* Process Unlocked */
  3629. __HAL_UNLOCK(hi2c);
  3630. /* Note : The I2C interrupts must be enabled after unlocking current process
  3631. to avoid the risk of I2C interrupt handle execution before current
  3632. process unlock */
  3633. /* REnable ADDR interrupt */
  3634. I2C_Enable_IRQ(hi2c, I2C_XFER_RX_IT | I2C_XFER_LISTEN_IT);
  3635. return HAL_OK;
  3636. }
  3637. else
  3638. {
  3639. return HAL_ERROR;
  3640. }
  3641. }
  3642. /**
  3643. * @brief Sequential receive in slave/device I2C mode an amount of data in non-blocking mode with DMA
  3644. * @note This interface allow to manage repeated start condition when a direction change during transfer
  3645. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3646. * the configuration information for the specified I2C.
  3647. * @param pData Pointer to data buffer
  3648. * @param Size Amount of data to be sent
  3649. * @param XferOptions Options of Transfer, value of @ref I2C_XFEROPTIONS
  3650. * @retval HAL status
  3651. */
  3652. HAL_StatusTypeDef HAL_I2C_Slave_Seq_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size,
  3653. uint32_t XferOptions)
  3654. {
  3655. /* Declaration of tmp to prevent undefined behavior of volatile usage */
  3656. FlagStatus tmp;
  3657. HAL_StatusTypeDef dmaxferstatus;
  3658. /* Check the parameters */
  3659. assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  3660. if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
  3661. {
  3662. if ((pData == NULL) || (Size == 0U))
  3663. {
  3664. hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM;
  3665. return HAL_ERROR;
  3666. }
  3667. /* Disable Interrupts, to prevent preemption during treatment in case of multicall */
  3668. I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_RX_IT);
  3669. /* Process Locked */
  3670. __HAL_LOCK(hi2c);
  3671. /* I2C cannot manage full duplex exchange so disable previous IT enabled if any */
  3672. /* and then toggle the HAL slave TX state to RX state */
  3673. if (hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN)
  3674. {
  3675. /* Disable associated Interrupts */
  3676. I2C_Disable_IRQ(hi2c, I2C_XFER_TX_IT);
  3677. if ((hi2c->Instance->CR1 & I2C_CR1_TXDMAEN) == I2C_CR1_TXDMAEN)
  3678. {
  3679. /* Abort DMA Xfer if any */
  3680. if (hi2c->hdmatx != NULL)
  3681. {
  3682. hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
  3683. /* Set the I2C DMA Abort callback :
  3684. will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
  3685. hi2c->hdmatx->XferAbortCallback = I2C_DMAAbort;
  3686. /* Abort DMA TX */
  3687. if (HAL_DMA_Abort_IT(hi2c->hdmatx) != HAL_OK)
  3688. {
  3689. /* Call Directly XferAbortCallback function in case of error */
  3690. hi2c->hdmatx->XferAbortCallback(hi2c->hdmatx);
  3691. }
  3692. }
  3693. }
  3694. }
  3695. else if (hi2c->State == HAL_I2C_STATE_BUSY_RX_LISTEN)
  3696. {
  3697. if ((hi2c->Instance->CR1 & I2C_CR1_RXDMAEN) == I2C_CR1_RXDMAEN)
  3698. {
  3699. hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
  3700. /* Abort DMA Xfer if any */
  3701. if (hi2c->hdmarx != NULL)
  3702. {
  3703. /* Set the I2C DMA Abort callback :
  3704. will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
  3705. hi2c->hdmarx->XferAbortCallback = I2C_DMAAbort;
  3706. /* Abort DMA RX */
  3707. if (HAL_DMA_Abort_IT(hi2c->hdmarx) != HAL_OK)
  3708. {
  3709. /* Call Directly XferAbortCallback function in case of error */
  3710. hi2c->hdmarx->XferAbortCallback(hi2c->hdmarx);
  3711. }
  3712. }
  3713. }
  3714. }
  3715. else
  3716. {
  3717. /* Nothing to do */
  3718. }
  3719. hi2c->State = HAL_I2C_STATE_BUSY_RX_LISTEN;
  3720. hi2c->Mode = HAL_I2C_MODE_SLAVE;
  3721. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  3722. /* Enable Address Acknowledge */
  3723. hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
  3724. /* Prepare transfer parameters */
  3725. hi2c->pBuffPtr = pData;
  3726. hi2c->XferCount = Size;
  3727. hi2c->XferSize = hi2c->XferCount;
  3728. hi2c->XferOptions = XferOptions;
  3729. hi2c->XferISR = I2C_Slave_ISR_DMA;
  3730. if (hi2c->hdmarx != NULL)
  3731. {
  3732. /* Set the I2C DMA transfer complete callback */
  3733. hi2c->hdmarx->XferCpltCallback = I2C_DMASlaveReceiveCplt;
  3734. /* Set the DMA error callback */
  3735. hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
  3736. /* Set the unused DMA callbacks to NULL */
  3737. hi2c->hdmarx->XferHalfCpltCallback = NULL;
  3738. hi2c->hdmarx->XferAbortCallback = NULL;
  3739. /* Enable the DMA stream or channel depends on Instance */
  3740. dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR,
  3741. (uint32_t)pData, hi2c->XferSize);
  3742. }
  3743. else
  3744. {
  3745. /* Update I2C state */
  3746. hi2c->State = HAL_I2C_STATE_LISTEN;
  3747. hi2c->Mode = HAL_I2C_MODE_NONE;
  3748. /* Update I2C error code */
  3749. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
  3750. /* Process Unlocked */
  3751. __HAL_UNLOCK(hi2c);
  3752. return HAL_ERROR;
  3753. }
  3754. if (dmaxferstatus == HAL_OK)
  3755. {
  3756. /* Update XferCount value */
  3757. hi2c->XferCount -= hi2c->XferSize;
  3758. /* Reset XferSize */
  3759. hi2c->XferSize = 0;
  3760. }
  3761. else
  3762. {
  3763. /* Update I2C state */
  3764. hi2c->State = HAL_I2C_STATE_LISTEN;
  3765. hi2c->Mode = HAL_I2C_MODE_NONE;
  3766. /* Update I2C error code */
  3767. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  3768. /* Process Unlocked */
  3769. __HAL_UNLOCK(hi2c);
  3770. return HAL_ERROR;
  3771. }
  3772. tmp = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR);
  3773. if ((I2C_GET_DIR(hi2c) == I2C_DIRECTION_TRANSMIT) && (tmp != RESET))
  3774. {
  3775. /* Clear ADDR flag after prepare the transfer parameters */
  3776. /* This action will generate an acknowledge to the Master */
  3777. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
  3778. }
  3779. /* Process Unlocked */
  3780. __HAL_UNLOCK(hi2c);
  3781. /* Enable DMA Request */
  3782. hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
  3783. /* Note : The I2C interrupts must be enabled after unlocking current process
  3784. to avoid the risk of I2C interrupt handle execution before current
  3785. process unlock */
  3786. /* REnable ADDR interrupt */
  3787. I2C_Enable_IRQ(hi2c, I2C_XFER_RX_IT | I2C_XFER_LISTEN_IT);
  3788. return HAL_OK;
  3789. }
  3790. else
  3791. {
  3792. return HAL_ERROR;
  3793. }
  3794. }
  3795. /**
  3796. * @brief Enable the Address listen mode with Interrupt.
  3797. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3798. * the configuration information for the specified I2C.
  3799. * @retval HAL status
  3800. */
  3801. HAL_StatusTypeDef HAL_I2C_EnableListen_IT(I2C_HandleTypeDef *hi2c)
  3802. {
  3803. if (hi2c->State == HAL_I2C_STATE_READY)
  3804. {
  3805. hi2c->State = HAL_I2C_STATE_LISTEN;
  3806. hi2c->XferISR = I2C_Slave_ISR_IT;
  3807. /* Enable the Address Match interrupt */
  3808. I2C_Enable_IRQ(hi2c, I2C_XFER_LISTEN_IT);
  3809. return HAL_OK;
  3810. }
  3811. else
  3812. {
  3813. return HAL_BUSY;
  3814. }
  3815. }
  3816. /**
  3817. * @brief Disable the Address listen mode with Interrupt.
  3818. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3819. * the configuration information for the specified I2C
  3820. * @retval HAL status
  3821. */
  3822. HAL_StatusTypeDef HAL_I2C_DisableListen_IT(I2C_HandleTypeDef *hi2c)
  3823. {
  3824. /* Declaration of tmp to prevent undefined behavior of volatile usage */
  3825. uint32_t tmp;
  3826. /* Disable Address listen mode only if a transfer is not ongoing */
  3827. if (hi2c->State == HAL_I2C_STATE_LISTEN)
  3828. {
  3829. tmp = (uint32_t)(hi2c->State) & I2C_STATE_MSK;
  3830. hi2c->PreviousState = tmp | (uint32_t)(hi2c->Mode);
  3831. hi2c->State = HAL_I2C_STATE_READY;
  3832. hi2c->Mode = HAL_I2C_MODE_NONE;
  3833. hi2c->XferISR = NULL;
  3834. /* Disable the Address Match interrupt */
  3835. I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT);
  3836. return HAL_OK;
  3837. }
  3838. else
  3839. {
  3840. return HAL_BUSY;
  3841. }
  3842. }
  3843. /**
  3844. * @brief Abort a master I2C IT or DMA process communication with Interrupt.
  3845. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3846. * the configuration information for the specified I2C.
  3847. * @param DevAddress Target device address: The device 7 bits address value
  3848. * in datasheet must be shifted to the left before calling the interface
  3849. * @retval HAL status
  3850. */
  3851. HAL_StatusTypeDef HAL_I2C_Master_Abort_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress)
  3852. {
  3853. if (hi2c->Mode == HAL_I2C_MODE_MASTER)
  3854. {
  3855. /* Process Locked */
  3856. __HAL_LOCK(hi2c);
  3857. /* Disable Interrupts and Store Previous state */
  3858. if (hi2c->State == HAL_I2C_STATE_BUSY_TX)
  3859. {
  3860. I2C_Disable_IRQ(hi2c, I2C_XFER_TX_IT);
  3861. hi2c->PreviousState = I2C_STATE_MASTER_BUSY_TX;
  3862. }
  3863. else if (hi2c->State == HAL_I2C_STATE_BUSY_RX)
  3864. {
  3865. I2C_Disable_IRQ(hi2c, I2C_XFER_RX_IT);
  3866. hi2c->PreviousState = I2C_STATE_MASTER_BUSY_RX;
  3867. }
  3868. else
  3869. {
  3870. /* Do nothing */
  3871. }
  3872. /* Set State at HAL_I2C_STATE_ABORT */
  3873. hi2c->State = HAL_I2C_STATE_ABORT;
  3874. /* Set NBYTES to 1 to generate a dummy read on I2C peripheral */
  3875. /* Set AUTOEND mode, this will generate a NACK then STOP condition to abort the current transfer */
  3876. I2C_TransferConfig(hi2c, DevAddress, 1, I2C_AUTOEND_MODE, I2C_GENERATE_STOP);
  3877. /* Process Unlocked */
  3878. __HAL_UNLOCK(hi2c);
  3879. /* Note : The I2C interrupts must be enabled after unlocking current process
  3880. to avoid the risk of I2C interrupt handle execution before current
  3881. process unlock */
  3882. I2C_Enable_IRQ(hi2c, I2C_XFER_CPLT_IT);
  3883. return HAL_OK;
  3884. }
  3885. else
  3886. {
  3887. /* Wrong usage of abort function */
  3888. /* This function should be used only in case of abort monitored by master device */
  3889. return HAL_ERROR;
  3890. }
  3891. }
  3892. /**
  3893. * @}
  3894. */
  3895. /** @defgroup I2C_IRQ_Handler_and_Callbacks IRQ Handler and Callbacks
  3896. * @{
  3897. */
  3898. /**
  3899. * @brief This function handles I2C event interrupt request.
  3900. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3901. * the configuration information for the specified I2C.
  3902. * @retval None
  3903. */
  3904. void HAL_I2C_EV_IRQHandler(I2C_HandleTypeDef *hi2c) /* Derogation MISRAC2012-Rule-8.13 */
  3905. {
  3906. /* Get current IT Flags and IT sources value */
  3907. uint32_t itflags = READ_REG(hi2c->Instance->ISR);
  3908. uint32_t itsources = READ_REG(hi2c->Instance->CR1);
  3909. /* I2C events treatment -------------------------------------*/
  3910. if (hi2c->XferISR != NULL)
  3911. {
  3912. hi2c->XferISR(hi2c, itflags, itsources);
  3913. }
  3914. }
  3915. /**
  3916. * @brief This function handles I2C error interrupt request.
  3917. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3918. * the configuration information for the specified I2C.
  3919. * @retval None
  3920. */
  3921. void HAL_I2C_ER_IRQHandler(I2C_HandleTypeDef *hi2c)
  3922. {
  3923. uint32_t itflags = READ_REG(hi2c->Instance->ISR);
  3924. uint32_t itsources = READ_REG(hi2c->Instance->CR1);
  3925. uint32_t tmperror;
  3926. /* I2C Bus error interrupt occurred ------------------------------------*/
  3927. if ((I2C_CHECK_FLAG(itflags, I2C_FLAG_BERR) != RESET) && \
  3928. (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_ERRI) != RESET))
  3929. {
  3930. hi2c->ErrorCode |= HAL_I2C_ERROR_BERR;
  3931. /* Clear BERR flag */
  3932. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_BERR);
  3933. }
  3934. /* I2C Over-Run/Under-Run interrupt occurred ----------------------------------------*/
  3935. if ((I2C_CHECK_FLAG(itflags, I2C_FLAG_OVR) != RESET) && \
  3936. (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_ERRI) != RESET))
  3937. {
  3938. hi2c->ErrorCode |= HAL_I2C_ERROR_OVR;
  3939. /* Clear OVR flag */
  3940. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_OVR);
  3941. }
  3942. /* I2C Arbitration Loss error interrupt occurred -------------------------------------*/
  3943. if ((I2C_CHECK_FLAG(itflags, I2C_FLAG_ARLO) != RESET) && \
  3944. (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_ERRI) != RESET))
  3945. {
  3946. hi2c->ErrorCode |= HAL_I2C_ERROR_ARLO;
  3947. /* Clear ARLO flag */
  3948. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ARLO);
  3949. }
  3950. /* Store current volatile hi2c->ErrorCode, misra rule */
  3951. tmperror = hi2c->ErrorCode;
  3952. /* Call the Error Callback in case of Error detected */
  3953. if ((tmperror & (HAL_I2C_ERROR_BERR | HAL_I2C_ERROR_OVR | HAL_I2C_ERROR_ARLO)) != HAL_I2C_ERROR_NONE)
  3954. {
  3955. I2C_ITError(hi2c, tmperror);
  3956. }
  3957. }
  3958. /**
  3959. * @brief Master Tx Transfer completed callback.
  3960. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3961. * the configuration information for the specified I2C.
  3962. * @retval None
  3963. */
  3964. __weak void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c)
  3965. {
  3966. /* Prevent unused argument(s) compilation warning */
  3967. UNUSED(hi2c);
  3968. /* NOTE : This function should not be modified, when the callback is needed,
  3969. the HAL_I2C_MasterTxCpltCallback could be implemented in the user file
  3970. */
  3971. }
  3972. /**
  3973. * @brief Master Rx Transfer completed callback.
  3974. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3975. * the configuration information for the specified I2C.
  3976. * @retval None
  3977. */
  3978. __weak void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c)
  3979. {
  3980. /* Prevent unused argument(s) compilation warning */
  3981. UNUSED(hi2c);
  3982. /* NOTE : This function should not be modified, when the callback is needed,
  3983. the HAL_I2C_MasterRxCpltCallback could be implemented in the user file
  3984. */
  3985. }
  3986. /** @brief Slave Tx Transfer completed callback.
  3987. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3988. * the configuration information for the specified I2C.
  3989. * @retval None
  3990. */
  3991. __weak void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *hi2c)
  3992. {
  3993. /* Prevent unused argument(s) compilation warning */
  3994. UNUSED(hi2c);
  3995. /* NOTE : This function should not be modified, when the callback is needed,
  3996. the HAL_I2C_SlaveTxCpltCallback could be implemented in the user file
  3997. */
  3998. }
  3999. /**
  4000. * @brief Slave Rx Transfer completed callback.
  4001. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4002. * the configuration information for the specified I2C.
  4003. * @retval None
  4004. */
  4005. __weak void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c)
  4006. {
  4007. /* Prevent unused argument(s) compilation warning */
  4008. UNUSED(hi2c);
  4009. /* NOTE : This function should not be modified, when the callback is needed,
  4010. the HAL_I2C_SlaveRxCpltCallback could be implemented in the user file
  4011. */
  4012. }
  4013. /**
  4014. * @brief Slave Address Match callback.
  4015. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4016. * the configuration information for the specified I2C.
  4017. * @param TransferDirection Master request Transfer Direction (Write/Read), value of @ref I2C_XFERDIRECTION
  4018. * @param AddrMatchCode Address Match Code
  4019. * @retval None
  4020. */
  4021. __weak void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode)
  4022. {
  4023. /* Prevent unused argument(s) compilation warning */
  4024. UNUSED(hi2c);
  4025. UNUSED(TransferDirection);
  4026. UNUSED(AddrMatchCode);
  4027. /* NOTE : This function should not be modified, when the callback is needed,
  4028. the HAL_I2C_AddrCallback() could be implemented in the user file
  4029. */
  4030. }
  4031. /**
  4032. * @brief Listen Complete callback.
  4033. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4034. * the configuration information for the specified I2C.
  4035. * @retval None
  4036. */
  4037. __weak void HAL_I2C_ListenCpltCallback(I2C_HandleTypeDef *hi2c)
  4038. {
  4039. /* Prevent unused argument(s) compilation warning */
  4040. UNUSED(hi2c);
  4041. /* NOTE : This function should not be modified, when the callback is needed,
  4042. the HAL_I2C_ListenCpltCallback() could be implemented in the user file
  4043. */
  4044. }
  4045. /**
  4046. * @brief Memory Tx Transfer completed callback.
  4047. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4048. * the configuration information for the specified I2C.
  4049. * @retval None
  4050. */
  4051. __weak void HAL_I2C_MemTxCpltCallback(I2C_HandleTypeDef *hi2c)
  4052. {
  4053. /* Prevent unused argument(s) compilation warning */
  4054. UNUSED(hi2c);
  4055. /* NOTE : This function should not be modified, when the callback is needed,
  4056. the HAL_I2C_MemTxCpltCallback could be implemented in the user file
  4057. */
  4058. }
  4059. /**
  4060. * @brief Memory Rx Transfer completed callback.
  4061. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4062. * the configuration information for the specified I2C.
  4063. * @retval None
  4064. */
  4065. __weak void HAL_I2C_MemRxCpltCallback(I2C_HandleTypeDef *hi2c)
  4066. {
  4067. /* Prevent unused argument(s) compilation warning */
  4068. UNUSED(hi2c);
  4069. /* NOTE : This function should not be modified, when the callback is needed,
  4070. the HAL_I2C_MemRxCpltCallback could be implemented in the user file
  4071. */
  4072. }
  4073. /**
  4074. * @brief I2C error callback.
  4075. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4076. * the configuration information for the specified I2C.
  4077. * @retval None
  4078. */
  4079. __weak void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c)
  4080. {
  4081. /* Prevent unused argument(s) compilation warning */
  4082. UNUSED(hi2c);
  4083. /* NOTE : This function should not be modified, when the callback is needed,
  4084. the HAL_I2C_ErrorCallback could be implemented in the user file
  4085. */
  4086. }
  4087. /**
  4088. * @brief I2C abort callback.
  4089. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4090. * the configuration information for the specified I2C.
  4091. * @retval None
  4092. */
  4093. __weak void HAL_I2C_AbortCpltCallback(I2C_HandleTypeDef *hi2c)
  4094. {
  4095. /* Prevent unused argument(s) compilation warning */
  4096. UNUSED(hi2c);
  4097. /* NOTE : This function should not be modified, when the callback is needed,
  4098. the HAL_I2C_AbortCpltCallback could be implemented in the user file
  4099. */
  4100. }
  4101. /**
  4102. * @}
  4103. */
  4104. /** @defgroup I2C_Exported_Functions_Group3 Peripheral State, Mode and Error functions
  4105. * @brief Peripheral State, Mode and Error functions
  4106. *
  4107. @verbatim
  4108. ===============================================================================
  4109. ##### Peripheral State, Mode and Error functions #####
  4110. ===============================================================================
  4111. [..]
  4112. This subsection permit to get in run-time the status of the peripheral
  4113. and the data flow.
  4114. @endverbatim
  4115. * @{
  4116. */
  4117. /**
  4118. * @brief Return the I2C handle state.
  4119. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4120. * the configuration information for the specified I2C.
  4121. * @retval HAL state
  4122. */
  4123. HAL_I2C_StateTypeDef HAL_I2C_GetState(const I2C_HandleTypeDef *hi2c)
  4124. {
  4125. /* Return I2C handle state */
  4126. return hi2c->State;
  4127. }
  4128. /**
  4129. * @brief Returns the I2C Master, Slave, Memory or no mode.
  4130. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4131. * the configuration information for I2C module
  4132. * @retval HAL mode
  4133. */
  4134. HAL_I2C_ModeTypeDef HAL_I2C_GetMode(const I2C_HandleTypeDef *hi2c)
  4135. {
  4136. return hi2c->Mode;
  4137. }
  4138. /**
  4139. * @brief Return the I2C error code.
  4140. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4141. * the configuration information for the specified I2C.
  4142. * @retval I2C Error Code
  4143. */
  4144. uint32_t HAL_I2C_GetError(const I2C_HandleTypeDef *hi2c)
  4145. {
  4146. return hi2c->ErrorCode;
  4147. }
  4148. /**
  4149. * @}
  4150. */
  4151. /**
  4152. * @}
  4153. */
  4154. /** @addtogroup I2C_Private_Functions
  4155. * @{
  4156. */
  4157. /**
  4158. * @brief Interrupt Sub-Routine which handle the Interrupt Flags Master Mode with Interrupt.
  4159. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4160. * the configuration information for the specified I2C.
  4161. * @param ITFlags Interrupt flags to handle.
  4162. * @param ITSources Interrupt sources enabled.
  4163. * @retval HAL status
  4164. */
  4165. static HAL_StatusTypeDef I2C_Master_ISR_IT(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags,
  4166. uint32_t ITSources)
  4167. {
  4168. uint16_t devaddress;
  4169. uint32_t tmpITFlags = ITFlags;
  4170. /* Process Locked */
  4171. __HAL_LOCK(hi2c);
  4172. if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_AF) != RESET) && \
  4173. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_NACKI) != RESET))
  4174. {
  4175. /* Clear NACK Flag */
  4176. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  4177. /* Set corresponding Error Code */
  4178. /* No need to generate STOP, it is automatically done */
  4179. /* Error callback will be send during stop flag treatment */
  4180. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  4181. /* Flush TX register */
  4182. I2C_Flush_TXDR(hi2c);
  4183. }
  4184. else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_RXNE) != RESET) && \
  4185. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_RXI) != RESET))
  4186. {
  4187. /* Remove RXNE flag on temporary variable as read done */
  4188. tmpITFlags &= ~I2C_FLAG_RXNE;
  4189. /* Read data from RXDR */
  4190. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->RXDR;
  4191. /* Increment Buffer pointer */
  4192. hi2c->pBuffPtr++;
  4193. hi2c->XferSize--;
  4194. hi2c->XferCount--;
  4195. }
  4196. else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_TC) == RESET) && \
  4197. ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_TXIS) != RESET) && \
  4198. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TXI) != RESET)))
  4199. {
  4200. /* Write data to TXDR */
  4201. if (hi2c->XferCount != 0U)
  4202. {
  4203. /* Write data to TXDR */
  4204. hi2c->Instance->TXDR = *hi2c->pBuffPtr;
  4205. /* Increment Buffer pointer */
  4206. hi2c->pBuffPtr++;
  4207. hi2c->XferSize--;
  4208. hi2c->XferCount--;
  4209. }
  4210. }
  4211. else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_TCR) != RESET) && \
  4212. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TCI) != RESET))
  4213. {
  4214. if ((hi2c->XferCount != 0U) && (hi2c->XferSize == 0U))
  4215. {
  4216. devaddress = (uint16_t)(hi2c->Instance->CR2 & I2C_CR2_SADD);
  4217. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  4218. {
  4219. hi2c->XferSize = MAX_NBYTE_SIZE;
  4220. I2C_TransferConfig(hi2c, devaddress, (uint8_t)hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  4221. }
  4222. else
  4223. {
  4224. hi2c->XferSize = hi2c->XferCount;
  4225. if (hi2c->XferOptions != I2C_NO_OPTION_FRAME)
  4226. {
  4227. I2C_TransferConfig(hi2c, devaddress, (uint8_t)hi2c->XferSize,
  4228. hi2c->XferOptions, I2C_NO_STARTSTOP);
  4229. }
  4230. else
  4231. {
  4232. I2C_TransferConfig(hi2c, devaddress, (uint8_t)hi2c->XferSize,
  4233. I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  4234. }
  4235. }
  4236. }
  4237. else
  4238. {
  4239. /* Call TxCpltCallback() if no stop mode is set */
  4240. if (I2C_GET_STOP_MODE(hi2c) != I2C_AUTOEND_MODE)
  4241. {
  4242. /* Call I2C Master Sequential complete process */
  4243. I2C_ITMasterSeqCplt(hi2c);
  4244. }
  4245. else
  4246. {
  4247. /* Wrong size Status regarding TCR flag event */
  4248. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4249. I2C_ITError(hi2c, HAL_I2C_ERROR_SIZE);
  4250. }
  4251. }
  4252. }
  4253. else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_TC) != RESET) && \
  4254. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TCI) != RESET))
  4255. {
  4256. if (hi2c->XferCount == 0U)
  4257. {
  4258. if (I2C_GET_STOP_MODE(hi2c) != I2C_AUTOEND_MODE)
  4259. {
  4260. /* Generate a stop condition in case of no transfer option */
  4261. if (hi2c->XferOptions == I2C_NO_OPTION_FRAME)
  4262. {
  4263. /* Generate Stop */
  4264. hi2c->Instance->CR2 |= I2C_CR2_STOP;
  4265. }
  4266. else
  4267. {
  4268. /* Call I2C Master Sequential complete process */
  4269. I2C_ITMasterSeqCplt(hi2c);
  4270. }
  4271. }
  4272. }
  4273. else
  4274. {
  4275. /* Wrong size Status regarding TC flag event */
  4276. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4277. I2C_ITError(hi2c, HAL_I2C_ERROR_SIZE);
  4278. }
  4279. }
  4280. else
  4281. {
  4282. /* Nothing to do */
  4283. }
  4284. if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_STOPF) != RESET) && \
  4285. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_STOPI) != RESET))
  4286. {
  4287. /* Call I2C Master complete process */
  4288. I2C_ITMasterCplt(hi2c, tmpITFlags);
  4289. }
  4290. /* Process Unlocked */
  4291. __HAL_UNLOCK(hi2c);
  4292. return HAL_OK;
  4293. }
  4294. /**
  4295. * @brief Interrupt Sub-Routine which handle the Interrupt Flags Memory Mode with Interrupt.
  4296. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4297. * the configuration information for the specified I2C.
  4298. * @param ITFlags Interrupt flags to handle.
  4299. * @param ITSources Interrupt sources enabled.
  4300. * @retval HAL status
  4301. */
  4302. static HAL_StatusTypeDef I2C_Mem_ISR_IT(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags,
  4303. uint32_t ITSources)
  4304. {
  4305. uint32_t direction = I2C_GENERATE_START_WRITE;
  4306. uint32_t tmpITFlags = ITFlags;
  4307. /* Process Locked */
  4308. __HAL_LOCK(hi2c);
  4309. if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_AF) != RESET) && \
  4310. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_NACKI) != RESET))
  4311. {
  4312. /* Clear NACK Flag */
  4313. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  4314. /* Set corresponding Error Code */
  4315. /* No need to generate STOP, it is automatically done */
  4316. /* Error callback will be send during stop flag treatment */
  4317. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  4318. /* Flush TX register */
  4319. I2C_Flush_TXDR(hi2c);
  4320. }
  4321. else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_RXNE) != RESET) && \
  4322. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_RXI) != RESET))
  4323. {
  4324. /* Remove RXNE flag on temporary variable as read done */
  4325. tmpITFlags &= ~I2C_FLAG_RXNE;
  4326. /* Read data from RXDR */
  4327. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->RXDR;
  4328. /* Increment Buffer pointer */
  4329. hi2c->pBuffPtr++;
  4330. hi2c->XferSize--;
  4331. hi2c->XferCount--;
  4332. }
  4333. else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_TXIS) != RESET) && \
  4334. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TXI) != RESET))
  4335. {
  4336. if (hi2c->Memaddress == 0xFFFFFFFFU)
  4337. {
  4338. /* Write data to TXDR */
  4339. hi2c->Instance->TXDR = *hi2c->pBuffPtr;
  4340. /* Increment Buffer pointer */
  4341. hi2c->pBuffPtr++;
  4342. hi2c->XferSize--;
  4343. hi2c->XferCount--;
  4344. }
  4345. else
  4346. {
  4347. /* Write LSB part of Memory Address */
  4348. hi2c->Instance->TXDR = hi2c->Memaddress;
  4349. /* Reset Memaddress content */
  4350. hi2c->Memaddress = 0xFFFFFFFFU;
  4351. }
  4352. }
  4353. else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_TCR) != RESET) && \
  4354. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TCI) != RESET))
  4355. {
  4356. if ((hi2c->XferCount != 0U) && (hi2c->XferSize == 0U))
  4357. {
  4358. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  4359. {
  4360. hi2c->XferSize = MAX_NBYTE_SIZE;
  4361. I2C_TransferConfig(hi2c, (uint16_t)hi2c->Devaddress, (uint8_t)hi2c->XferSize,
  4362. I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  4363. }
  4364. else
  4365. {
  4366. hi2c->XferSize = hi2c->XferCount;
  4367. I2C_TransferConfig(hi2c, (uint16_t)hi2c->Devaddress, (uint8_t)hi2c->XferSize,
  4368. I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  4369. }
  4370. }
  4371. else
  4372. {
  4373. /* Wrong size Status regarding TCR flag event */
  4374. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4375. I2C_ITError(hi2c, HAL_I2C_ERROR_SIZE);
  4376. }
  4377. }
  4378. else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_TC) != RESET) && \
  4379. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TCI) != RESET))
  4380. {
  4381. /* Disable Interrupt related to address step */
  4382. I2C_Disable_IRQ(hi2c, I2C_XFER_TX_IT);
  4383. /* Enable ERR, TC, STOP, NACK and RXI interrupts */
  4384. I2C_Enable_IRQ(hi2c, I2C_XFER_RX_IT);
  4385. if (hi2c->State == HAL_I2C_STATE_BUSY_RX)
  4386. {
  4387. direction = I2C_GENERATE_START_READ;
  4388. }
  4389. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  4390. {
  4391. hi2c->XferSize = MAX_NBYTE_SIZE;
  4392. /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
  4393. I2C_TransferConfig(hi2c, (uint16_t)hi2c->Devaddress, (uint8_t)hi2c->XferSize,
  4394. I2C_RELOAD_MODE, direction);
  4395. }
  4396. else
  4397. {
  4398. hi2c->XferSize = hi2c->XferCount;
  4399. /* Set NBYTES to write and generate RESTART */
  4400. I2C_TransferConfig(hi2c, (uint16_t)hi2c->Devaddress, (uint8_t)hi2c->XferSize,
  4401. I2C_AUTOEND_MODE, direction);
  4402. }
  4403. }
  4404. else
  4405. {
  4406. /* Nothing to do */
  4407. }
  4408. if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_STOPF) != RESET) && \
  4409. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_STOPI) != RESET))
  4410. {
  4411. /* Call I2C Master complete process */
  4412. I2C_ITMasterCplt(hi2c, tmpITFlags);
  4413. }
  4414. /* Process Unlocked */
  4415. __HAL_UNLOCK(hi2c);
  4416. return HAL_OK;
  4417. }
  4418. /**
  4419. * @brief Interrupt Sub-Routine which handle the Interrupt Flags Slave Mode with Interrupt.
  4420. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4421. * the configuration information for the specified I2C.
  4422. * @param ITFlags Interrupt flags to handle.
  4423. * @param ITSources Interrupt sources enabled.
  4424. * @retval HAL status
  4425. */
  4426. static HAL_StatusTypeDef I2C_Slave_ISR_IT(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags,
  4427. uint32_t ITSources)
  4428. {
  4429. uint32_t tmpoptions = hi2c->XferOptions;
  4430. uint32_t tmpITFlags = ITFlags;
  4431. /* Process locked */
  4432. __HAL_LOCK(hi2c);
  4433. /* Check if STOPF is set */
  4434. if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_STOPF) != RESET) && \
  4435. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_STOPI) != RESET))
  4436. {
  4437. /* Call I2C Slave complete process */
  4438. I2C_ITSlaveCplt(hi2c, tmpITFlags);
  4439. }
  4440. else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_AF) != RESET) && \
  4441. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_NACKI) != RESET))
  4442. {
  4443. /* Check that I2C transfer finished */
  4444. /* if yes, normal use case, a NACK is sent by the MASTER when Transfer is finished */
  4445. /* Mean XferCount == 0*/
  4446. /* So clear Flag NACKF only */
  4447. if (hi2c->XferCount == 0U)
  4448. {
  4449. if ((hi2c->State == HAL_I2C_STATE_LISTEN) && (tmpoptions == I2C_FIRST_AND_LAST_FRAME))
  4450. /* Same action must be done for (tmpoptions == I2C_LAST_FRAME) which removed for
  4451. Warning[Pa134]: left and right operands are identical */
  4452. {
  4453. /* Call I2C Listen complete process */
  4454. I2C_ITListenCplt(hi2c, tmpITFlags);
  4455. }
  4456. else if ((hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN) && (tmpoptions != I2C_NO_OPTION_FRAME))
  4457. {
  4458. /* Clear NACK Flag */
  4459. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  4460. /* Flush TX register */
  4461. I2C_Flush_TXDR(hi2c);
  4462. /* Last Byte is Transmitted */
  4463. /* Call I2C Slave Sequential complete process */
  4464. I2C_ITSlaveSeqCplt(hi2c);
  4465. }
  4466. else
  4467. {
  4468. /* Clear NACK Flag */
  4469. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  4470. }
  4471. }
  4472. else
  4473. {
  4474. /* if no, error use case, a Non-Acknowledge of last Data is generated by the MASTER*/
  4475. /* Clear NACK Flag */
  4476. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  4477. /* Set ErrorCode corresponding to a Non-Acknowledge */
  4478. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  4479. if ((tmpoptions == I2C_FIRST_FRAME) || (tmpoptions == I2C_NEXT_FRAME))
  4480. {
  4481. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4482. I2C_ITError(hi2c, hi2c->ErrorCode);
  4483. }
  4484. }
  4485. }
  4486. else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_RXNE) != RESET) && \
  4487. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_RXI) != RESET))
  4488. {
  4489. if (hi2c->XferCount > 0U)
  4490. {
  4491. /* Read data from RXDR */
  4492. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->RXDR;
  4493. /* Increment Buffer pointer */
  4494. hi2c->pBuffPtr++;
  4495. hi2c->XferSize--;
  4496. hi2c->XferCount--;
  4497. }
  4498. if ((hi2c->XferCount == 0U) && \
  4499. (tmpoptions != I2C_NO_OPTION_FRAME))
  4500. {
  4501. /* Call I2C Slave Sequential complete process */
  4502. I2C_ITSlaveSeqCplt(hi2c);
  4503. }
  4504. }
  4505. else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_ADDR) != RESET) && \
  4506. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_ADDRI) != RESET))
  4507. {
  4508. I2C_ITAddrCplt(hi2c, tmpITFlags);
  4509. }
  4510. else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_TXIS) != RESET) && \
  4511. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TXI) != RESET))
  4512. {
  4513. /* Write data to TXDR only if XferCount not reach "0" */
  4514. /* A TXIS flag can be set, during STOP treatment */
  4515. /* Check if all Data have already been sent */
  4516. /* If it is the case, this last write in TXDR is not sent, correspond to a dummy TXIS event */
  4517. if (hi2c->XferCount > 0U)
  4518. {
  4519. /* Write data to TXDR */
  4520. hi2c->Instance->TXDR = *hi2c->pBuffPtr;
  4521. /* Increment Buffer pointer */
  4522. hi2c->pBuffPtr++;
  4523. hi2c->XferCount--;
  4524. hi2c->XferSize--;
  4525. }
  4526. else
  4527. {
  4528. if ((tmpoptions == I2C_NEXT_FRAME) || (tmpoptions == I2C_FIRST_FRAME))
  4529. {
  4530. /* Last Byte is Transmitted */
  4531. /* Call I2C Slave Sequential complete process */
  4532. I2C_ITSlaveSeqCplt(hi2c);
  4533. }
  4534. }
  4535. }
  4536. else
  4537. {
  4538. /* Nothing to do */
  4539. }
  4540. /* Process Unlocked */
  4541. __HAL_UNLOCK(hi2c);
  4542. return HAL_OK;
  4543. }
  4544. /**
  4545. * @brief Interrupt Sub-Routine which handle the Interrupt Flags Master Mode with DMA.
  4546. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4547. * the configuration information for the specified I2C.
  4548. * @param ITFlags Interrupt flags to handle.
  4549. * @param ITSources Interrupt sources enabled.
  4550. * @retval HAL status
  4551. */
  4552. static HAL_StatusTypeDef I2C_Master_ISR_DMA(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags,
  4553. uint32_t ITSources)
  4554. {
  4555. uint16_t devaddress;
  4556. uint32_t xfermode;
  4557. /* Process Locked */
  4558. __HAL_LOCK(hi2c);
  4559. if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_AF) != RESET) && \
  4560. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_NACKI) != RESET))
  4561. {
  4562. /* Clear NACK Flag */
  4563. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  4564. /* Set corresponding Error Code */
  4565. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  4566. /* No need to generate STOP, it is automatically done */
  4567. /* But enable STOP interrupt, to treat it */
  4568. /* Error callback will be send during stop flag treatment */
  4569. I2C_Enable_IRQ(hi2c, I2C_XFER_CPLT_IT);
  4570. /* Flush TX register */
  4571. I2C_Flush_TXDR(hi2c);
  4572. }
  4573. else if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_TCR) != RESET) && \
  4574. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TCI) != RESET))
  4575. {
  4576. /* Disable TC interrupt */
  4577. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_TCI);
  4578. if (hi2c->XferCount != 0U)
  4579. {
  4580. /* Recover Slave address */
  4581. devaddress = (uint16_t)(hi2c->Instance->CR2 & I2C_CR2_SADD);
  4582. /* Prepare the new XferSize to transfer */
  4583. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  4584. {
  4585. hi2c->XferSize = MAX_NBYTE_SIZE;
  4586. xfermode = I2C_RELOAD_MODE;
  4587. }
  4588. else
  4589. {
  4590. hi2c->XferSize = hi2c->XferCount;
  4591. if (hi2c->XferOptions != I2C_NO_OPTION_FRAME)
  4592. {
  4593. xfermode = hi2c->XferOptions;
  4594. }
  4595. else
  4596. {
  4597. xfermode = I2C_AUTOEND_MODE;
  4598. }
  4599. }
  4600. /* Set the new XferSize in Nbytes register */
  4601. I2C_TransferConfig(hi2c, devaddress, (uint8_t)hi2c->XferSize, xfermode, I2C_NO_STARTSTOP);
  4602. /* Update XferCount value */
  4603. hi2c->XferCount -= hi2c->XferSize;
  4604. /* Enable DMA Request */
  4605. if (hi2c->State == HAL_I2C_STATE_BUSY_RX)
  4606. {
  4607. hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
  4608. }
  4609. else
  4610. {
  4611. hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
  4612. }
  4613. }
  4614. else
  4615. {
  4616. /* Call TxCpltCallback() if no stop mode is set */
  4617. if (I2C_GET_STOP_MODE(hi2c) != I2C_AUTOEND_MODE)
  4618. {
  4619. /* Call I2C Master Sequential complete process */
  4620. I2C_ITMasterSeqCplt(hi2c);
  4621. }
  4622. else
  4623. {
  4624. /* Wrong size Status regarding TCR flag event */
  4625. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4626. I2C_ITError(hi2c, HAL_I2C_ERROR_SIZE);
  4627. }
  4628. }
  4629. }
  4630. else if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_TC) != RESET) && \
  4631. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TCI) != RESET))
  4632. {
  4633. if (hi2c->XferCount == 0U)
  4634. {
  4635. if (I2C_GET_STOP_MODE(hi2c) != I2C_AUTOEND_MODE)
  4636. {
  4637. /* Generate a stop condition in case of no transfer option */
  4638. if (hi2c->XferOptions == I2C_NO_OPTION_FRAME)
  4639. {
  4640. /* Generate Stop */
  4641. hi2c->Instance->CR2 |= I2C_CR2_STOP;
  4642. }
  4643. else
  4644. {
  4645. /* Call I2C Master Sequential complete process */
  4646. I2C_ITMasterSeqCplt(hi2c);
  4647. }
  4648. }
  4649. }
  4650. else
  4651. {
  4652. /* Wrong size Status regarding TC flag event */
  4653. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4654. I2C_ITError(hi2c, HAL_I2C_ERROR_SIZE);
  4655. }
  4656. }
  4657. else if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_STOPF) != RESET) && \
  4658. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_STOPI) != RESET))
  4659. {
  4660. /* Call I2C Master complete process */
  4661. I2C_ITMasterCplt(hi2c, ITFlags);
  4662. }
  4663. else
  4664. {
  4665. /* Nothing to do */
  4666. }
  4667. /* Process Unlocked */
  4668. __HAL_UNLOCK(hi2c);
  4669. return HAL_OK;
  4670. }
  4671. /**
  4672. * @brief Interrupt Sub-Routine which handle the Interrupt Flags Memory Mode with DMA.
  4673. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4674. * the configuration information for the specified I2C.
  4675. * @param ITFlags Interrupt flags to handle.
  4676. * @param ITSources Interrupt sources enabled.
  4677. * @retval HAL status
  4678. */
  4679. static HAL_StatusTypeDef I2C_Mem_ISR_DMA(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags,
  4680. uint32_t ITSources)
  4681. {
  4682. uint32_t direction = I2C_GENERATE_START_WRITE;
  4683. /* Process Locked */
  4684. __HAL_LOCK(hi2c);
  4685. if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_AF) != RESET) && \
  4686. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_NACKI) != RESET))
  4687. {
  4688. /* Clear NACK Flag */
  4689. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  4690. /* Set corresponding Error Code */
  4691. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  4692. /* No need to generate STOP, it is automatically done */
  4693. /* But enable STOP interrupt, to treat it */
  4694. /* Error callback will be send during stop flag treatment */
  4695. I2C_Enable_IRQ(hi2c, I2C_XFER_CPLT_IT);
  4696. /* Flush TX register */
  4697. I2C_Flush_TXDR(hi2c);
  4698. }
  4699. else if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_TXIS) != RESET) && \
  4700. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TXI) != RESET))
  4701. {
  4702. /* Write LSB part of Memory Address */
  4703. hi2c->Instance->TXDR = hi2c->Memaddress;
  4704. /* Reset Memaddress content */
  4705. hi2c->Memaddress = 0xFFFFFFFFU;
  4706. }
  4707. else if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_TCR) != RESET) && \
  4708. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TCI) != RESET))
  4709. {
  4710. /* Disable Interrupt related to address step */
  4711. I2C_Disable_IRQ(hi2c, I2C_XFER_TX_IT);
  4712. /* Enable only Error interrupt */
  4713. I2C_Enable_IRQ(hi2c, I2C_XFER_ERROR_IT);
  4714. if (hi2c->XferCount != 0U)
  4715. {
  4716. /* Prepare the new XferSize to transfer */
  4717. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  4718. {
  4719. hi2c->XferSize = MAX_NBYTE_SIZE;
  4720. I2C_TransferConfig(hi2c, (uint16_t)hi2c->Devaddress, (uint8_t)hi2c->XferSize,
  4721. I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  4722. }
  4723. else
  4724. {
  4725. hi2c->XferSize = hi2c->XferCount;
  4726. I2C_TransferConfig(hi2c, (uint16_t)hi2c->Devaddress, (uint8_t)hi2c->XferSize,
  4727. I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  4728. }
  4729. /* Update XferCount value */
  4730. hi2c->XferCount -= hi2c->XferSize;
  4731. /* Enable DMA Request */
  4732. if (hi2c->State == HAL_I2C_STATE_BUSY_RX)
  4733. {
  4734. hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
  4735. }
  4736. else
  4737. {
  4738. hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
  4739. }
  4740. }
  4741. else
  4742. {
  4743. /* Wrong size Status regarding TCR flag event */
  4744. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4745. I2C_ITError(hi2c, HAL_I2C_ERROR_SIZE);
  4746. }
  4747. }
  4748. else if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_TC) != RESET) && \
  4749. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TCI) != RESET))
  4750. {
  4751. /* Disable Interrupt related to address step */
  4752. I2C_Disable_IRQ(hi2c, I2C_XFER_TX_IT);
  4753. /* Enable only Error and NACK interrupt for data transfer */
  4754. I2C_Enable_IRQ(hi2c, I2C_XFER_ERROR_IT);
  4755. if (hi2c->State == HAL_I2C_STATE_BUSY_RX)
  4756. {
  4757. direction = I2C_GENERATE_START_READ;
  4758. }
  4759. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  4760. {
  4761. hi2c->XferSize = MAX_NBYTE_SIZE;
  4762. /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
  4763. I2C_TransferConfig(hi2c, (uint16_t)hi2c->Devaddress, (uint8_t)hi2c->XferSize,
  4764. I2C_RELOAD_MODE, direction);
  4765. }
  4766. else
  4767. {
  4768. hi2c->XferSize = hi2c->XferCount;
  4769. /* Set NBYTES to write and generate RESTART */
  4770. I2C_TransferConfig(hi2c, (uint16_t)hi2c->Devaddress, (uint8_t)hi2c->XferSize,
  4771. I2C_AUTOEND_MODE, direction);
  4772. }
  4773. /* Update XferCount value */
  4774. hi2c->XferCount -= hi2c->XferSize;
  4775. /* Enable DMA Request */
  4776. if (hi2c->State == HAL_I2C_STATE_BUSY_RX)
  4777. {
  4778. hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
  4779. }
  4780. else
  4781. {
  4782. hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
  4783. }
  4784. }
  4785. else if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_STOPF) != RESET) && \
  4786. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_STOPI) != RESET))
  4787. {
  4788. /* Call I2C Master complete process */
  4789. I2C_ITMasterCplt(hi2c, ITFlags);
  4790. }
  4791. else
  4792. {
  4793. /* Nothing to do */
  4794. }
  4795. /* Process Unlocked */
  4796. __HAL_UNLOCK(hi2c);
  4797. return HAL_OK;
  4798. }
  4799. /**
  4800. * @brief Interrupt Sub-Routine which handle the Interrupt Flags Slave Mode with DMA.
  4801. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4802. * the configuration information for the specified I2C.
  4803. * @param ITFlags Interrupt flags to handle.
  4804. * @param ITSources Interrupt sources enabled.
  4805. * @retval HAL status
  4806. */
  4807. static HAL_StatusTypeDef I2C_Slave_ISR_DMA(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags,
  4808. uint32_t ITSources)
  4809. {
  4810. uint32_t tmpoptions = hi2c->XferOptions;
  4811. uint32_t treatdmanack = 0U;
  4812. HAL_I2C_StateTypeDef tmpstate;
  4813. /* Process locked */
  4814. __HAL_LOCK(hi2c);
  4815. /* Check if STOPF is set */
  4816. if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_STOPF) != RESET) && \
  4817. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_STOPI) != RESET))
  4818. {
  4819. /* Call I2C Slave complete process */
  4820. I2C_ITSlaveCplt(hi2c, ITFlags);
  4821. }
  4822. else if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_AF) != RESET) && \
  4823. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_NACKI) != RESET))
  4824. {
  4825. /* Check that I2C transfer finished */
  4826. /* if yes, normal use case, a NACK is sent by the MASTER when Transfer is finished */
  4827. /* Mean XferCount == 0 */
  4828. /* So clear Flag NACKF only */
  4829. if ((I2C_CHECK_IT_SOURCE(ITSources, I2C_CR1_TXDMAEN) != RESET) ||
  4830. (I2C_CHECK_IT_SOURCE(ITSources, I2C_CR1_RXDMAEN) != RESET))
  4831. {
  4832. /* Split check of hdmarx, for MISRA compliance */
  4833. if (hi2c->hdmarx != NULL)
  4834. {
  4835. if (I2C_CHECK_IT_SOURCE(ITSources, I2C_CR1_RXDMAEN) != RESET)
  4836. {
  4837. if (I2C_GET_DMA_REMAIN_DATA(hi2c->hdmarx) == 0U)
  4838. {
  4839. treatdmanack = 1U;
  4840. }
  4841. }
  4842. }
  4843. /* Split check of hdmatx, for MISRA compliance */
  4844. if (hi2c->hdmatx != NULL)
  4845. {
  4846. if (I2C_CHECK_IT_SOURCE(ITSources, I2C_CR1_TXDMAEN) != RESET)
  4847. {
  4848. if (I2C_GET_DMA_REMAIN_DATA(hi2c->hdmatx) == 0U)
  4849. {
  4850. treatdmanack = 1U;
  4851. }
  4852. }
  4853. }
  4854. if (treatdmanack == 1U)
  4855. {
  4856. if ((hi2c->State == HAL_I2C_STATE_LISTEN) && (tmpoptions == I2C_FIRST_AND_LAST_FRAME))
  4857. /* Same action must be done for (tmpoptions == I2C_LAST_FRAME) which removed for
  4858. Warning[Pa134]: left and right operands are identical */
  4859. {
  4860. /* Call I2C Listen complete process */
  4861. I2C_ITListenCplt(hi2c, ITFlags);
  4862. }
  4863. else if ((hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN) && (tmpoptions != I2C_NO_OPTION_FRAME))
  4864. {
  4865. /* Clear NACK Flag */
  4866. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  4867. /* Flush TX register */
  4868. I2C_Flush_TXDR(hi2c);
  4869. /* Last Byte is Transmitted */
  4870. /* Call I2C Slave Sequential complete process */
  4871. I2C_ITSlaveSeqCplt(hi2c);
  4872. }
  4873. else
  4874. {
  4875. /* Clear NACK Flag */
  4876. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  4877. }
  4878. }
  4879. else
  4880. {
  4881. /* if no, error use case, a Non-Acknowledge of last Data is generated by the MASTER*/
  4882. /* Clear NACK Flag */
  4883. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  4884. /* Set ErrorCode corresponding to a Non-Acknowledge */
  4885. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  4886. /* Store current hi2c->State, solve MISRA2012-Rule-13.5 */
  4887. tmpstate = hi2c->State;
  4888. if ((tmpoptions == I2C_FIRST_FRAME) || (tmpoptions == I2C_NEXT_FRAME))
  4889. {
  4890. if ((tmpstate == HAL_I2C_STATE_BUSY_TX) || (tmpstate == HAL_I2C_STATE_BUSY_TX_LISTEN))
  4891. {
  4892. hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_TX;
  4893. }
  4894. else if ((tmpstate == HAL_I2C_STATE_BUSY_RX) || (tmpstate == HAL_I2C_STATE_BUSY_RX_LISTEN))
  4895. {
  4896. hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_RX;
  4897. }
  4898. else
  4899. {
  4900. /* Do nothing */
  4901. }
  4902. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4903. I2C_ITError(hi2c, hi2c->ErrorCode);
  4904. }
  4905. }
  4906. }
  4907. else
  4908. {
  4909. /* Only Clear NACK Flag, no DMA treatment is pending */
  4910. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  4911. }
  4912. }
  4913. else if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_ADDR) != RESET) && \
  4914. (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_ADDRI) != RESET))
  4915. {
  4916. I2C_ITAddrCplt(hi2c, ITFlags);
  4917. }
  4918. else
  4919. {
  4920. /* Nothing to do */
  4921. }
  4922. /* Process Unlocked */
  4923. __HAL_UNLOCK(hi2c);
  4924. return HAL_OK;
  4925. }
  4926. /**
  4927. * @brief Master sends target device address followed by internal memory address for write request.
  4928. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4929. * the configuration information for the specified I2C.
  4930. * @param DevAddress Target device address: The device 7 bits address value
  4931. * in datasheet must be shifted to the left before calling the interface
  4932. * @param MemAddress Internal memory address
  4933. * @param MemAddSize Size of internal memory address
  4934. * @param Timeout Timeout duration
  4935. * @param Tickstart Tick start value
  4936. * @retval HAL status
  4937. */
  4938. static HAL_StatusTypeDef I2C_RequestMemoryWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress,
  4939. uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout,
  4940. uint32_t Tickstart)
  4941. {
  4942. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)MemAddSize, I2C_RELOAD_MODE, I2C_GENERATE_START_WRITE);
  4943. /* Wait until TXIS flag is set */
  4944. if (I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
  4945. {
  4946. return HAL_ERROR;
  4947. }
  4948. /* If Memory address size is 8Bit */
  4949. if (MemAddSize == I2C_MEMADD_SIZE_8BIT)
  4950. {
  4951. /* Send Memory Address */
  4952. hi2c->Instance->TXDR = I2C_MEM_ADD_LSB(MemAddress);
  4953. }
  4954. /* If Memory address size is 16Bit */
  4955. else
  4956. {
  4957. /* Send MSB of Memory Address */
  4958. hi2c->Instance->TXDR = I2C_MEM_ADD_MSB(MemAddress);
  4959. /* Wait until TXIS flag is set */
  4960. if (I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
  4961. {
  4962. return HAL_ERROR;
  4963. }
  4964. /* Send LSB of Memory Address */
  4965. hi2c->Instance->TXDR = I2C_MEM_ADD_LSB(MemAddress);
  4966. }
  4967. /* Wait until TCR flag is set */
  4968. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout, Tickstart) != HAL_OK)
  4969. {
  4970. return HAL_ERROR;
  4971. }
  4972. return HAL_OK;
  4973. }
  4974. /**
  4975. * @brief Master sends target device address followed by internal memory address for read request.
  4976. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4977. * the configuration information for the specified I2C.
  4978. * @param DevAddress Target device address: The device 7 bits address value
  4979. * in datasheet must be shifted to the left before calling the interface
  4980. * @param MemAddress Internal memory address
  4981. * @param MemAddSize Size of internal memory address
  4982. * @param Timeout Timeout duration
  4983. * @param Tickstart Tick start value
  4984. * @retval HAL status
  4985. */
  4986. static HAL_StatusTypeDef I2C_RequestMemoryRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress,
  4987. uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout,
  4988. uint32_t Tickstart)
  4989. {
  4990. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)MemAddSize, I2C_SOFTEND_MODE, I2C_GENERATE_START_WRITE);
  4991. /* Wait until TXIS flag is set */
  4992. if (I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
  4993. {
  4994. return HAL_ERROR;
  4995. }
  4996. /* If Memory address size is 8Bit */
  4997. if (MemAddSize == I2C_MEMADD_SIZE_8BIT)
  4998. {
  4999. /* Send Memory Address */
  5000. hi2c->Instance->TXDR = I2C_MEM_ADD_LSB(MemAddress);
  5001. }
  5002. /* If Memory address size is 16Bit */
  5003. else
  5004. {
  5005. /* Send MSB of Memory Address */
  5006. hi2c->Instance->TXDR = I2C_MEM_ADD_MSB(MemAddress);
  5007. /* Wait until TXIS flag is set */
  5008. if (I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
  5009. {
  5010. return HAL_ERROR;
  5011. }
  5012. /* Send LSB of Memory Address */
  5013. hi2c->Instance->TXDR = I2C_MEM_ADD_LSB(MemAddress);
  5014. }
  5015. /* Wait until TC flag is set */
  5016. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TC, RESET, Timeout, Tickstart) != HAL_OK)
  5017. {
  5018. return HAL_ERROR;
  5019. }
  5020. return HAL_OK;
  5021. }
  5022. /**
  5023. * @brief I2C Address complete process callback.
  5024. * @param hi2c I2C handle.
  5025. * @param ITFlags Interrupt flags to handle.
  5026. * @retval None
  5027. */
  5028. static void I2C_ITAddrCplt(I2C_HandleTypeDef *hi2c, uint32_t ITFlags)
  5029. {
  5030. uint8_t transferdirection;
  5031. uint16_t slaveaddrcode;
  5032. uint16_t ownadd1code;
  5033. uint16_t ownadd2code;
  5034. /* Prevent unused argument(s) compilation warning */
  5035. UNUSED(ITFlags);
  5036. /* In case of Listen state, need to inform upper layer of address match code event */
  5037. if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
  5038. {
  5039. transferdirection = I2C_GET_DIR(hi2c);
  5040. slaveaddrcode = I2C_GET_ADDR_MATCH(hi2c);
  5041. ownadd1code = I2C_GET_OWN_ADDRESS1(hi2c);
  5042. ownadd2code = I2C_GET_OWN_ADDRESS2(hi2c);
  5043. /* If 10bits addressing mode is selected */
  5044. if (hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT)
  5045. {
  5046. if ((slaveaddrcode & SLAVE_ADDR_MSK) == ((ownadd1code >> SLAVE_ADDR_SHIFT) & SLAVE_ADDR_MSK))
  5047. {
  5048. slaveaddrcode = ownadd1code;
  5049. hi2c->AddrEventCount++;
  5050. if (hi2c->AddrEventCount == 2U)
  5051. {
  5052. /* Reset Address Event counter */
  5053. hi2c->AddrEventCount = 0U;
  5054. /* Clear ADDR flag */
  5055. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
  5056. /* Process Unlocked */
  5057. __HAL_UNLOCK(hi2c);
  5058. /* Call Slave Addr callback */
  5059. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5060. hi2c->AddrCallback(hi2c, transferdirection, slaveaddrcode);
  5061. #else
  5062. HAL_I2C_AddrCallback(hi2c, transferdirection, slaveaddrcode);
  5063. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5064. }
  5065. }
  5066. else
  5067. {
  5068. slaveaddrcode = ownadd2code;
  5069. /* Disable ADDR Interrupts */
  5070. I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT);
  5071. /* Process Unlocked */
  5072. __HAL_UNLOCK(hi2c);
  5073. /* Call Slave Addr callback */
  5074. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5075. hi2c->AddrCallback(hi2c, transferdirection, slaveaddrcode);
  5076. #else
  5077. HAL_I2C_AddrCallback(hi2c, transferdirection, slaveaddrcode);
  5078. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5079. }
  5080. }
  5081. /* else 7 bits addressing mode is selected */
  5082. else
  5083. {
  5084. /* Disable ADDR Interrupts */
  5085. I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT);
  5086. /* Process Unlocked */
  5087. __HAL_UNLOCK(hi2c);
  5088. /* Call Slave Addr callback */
  5089. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5090. hi2c->AddrCallback(hi2c, transferdirection, slaveaddrcode);
  5091. #else
  5092. HAL_I2C_AddrCallback(hi2c, transferdirection, slaveaddrcode);
  5093. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5094. }
  5095. }
  5096. /* Else clear address flag only */
  5097. else
  5098. {
  5099. /* Clear ADDR flag */
  5100. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
  5101. /* Process Unlocked */
  5102. __HAL_UNLOCK(hi2c);
  5103. }
  5104. }
  5105. /**
  5106. * @brief I2C Master sequential complete process.
  5107. * @param hi2c I2C handle.
  5108. * @retval None
  5109. */
  5110. static void I2C_ITMasterSeqCplt(I2C_HandleTypeDef *hi2c)
  5111. {
  5112. /* Reset I2C handle mode */
  5113. hi2c->Mode = HAL_I2C_MODE_NONE;
  5114. /* No Generate Stop, to permit restart mode */
  5115. /* The stop will be done at the end of transfer, when I2C_AUTOEND_MODE enable */
  5116. if (hi2c->State == HAL_I2C_STATE_BUSY_TX)
  5117. {
  5118. hi2c->State = HAL_I2C_STATE_READY;
  5119. hi2c->PreviousState = I2C_STATE_MASTER_BUSY_TX;
  5120. hi2c->XferISR = NULL;
  5121. /* Disable Interrupts */
  5122. I2C_Disable_IRQ(hi2c, I2C_XFER_TX_IT);
  5123. /* Process Unlocked */
  5124. __HAL_UNLOCK(hi2c);
  5125. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5126. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5127. hi2c->MasterTxCpltCallback(hi2c);
  5128. #else
  5129. HAL_I2C_MasterTxCpltCallback(hi2c);
  5130. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5131. }
  5132. /* hi2c->State == HAL_I2C_STATE_BUSY_RX */
  5133. else
  5134. {
  5135. hi2c->State = HAL_I2C_STATE_READY;
  5136. hi2c->PreviousState = I2C_STATE_MASTER_BUSY_RX;
  5137. hi2c->XferISR = NULL;
  5138. /* Disable Interrupts */
  5139. I2C_Disable_IRQ(hi2c, I2C_XFER_RX_IT);
  5140. /* Process Unlocked */
  5141. __HAL_UNLOCK(hi2c);
  5142. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5143. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5144. hi2c->MasterRxCpltCallback(hi2c);
  5145. #else
  5146. HAL_I2C_MasterRxCpltCallback(hi2c);
  5147. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5148. }
  5149. }
  5150. /**
  5151. * @brief I2C Slave sequential complete process.
  5152. * @param hi2c I2C handle.
  5153. * @retval None
  5154. */
  5155. static void I2C_ITSlaveSeqCplt(I2C_HandleTypeDef *hi2c)
  5156. {
  5157. uint32_t tmpcr1value = READ_REG(hi2c->Instance->CR1);
  5158. /* Reset I2C handle mode */
  5159. hi2c->Mode = HAL_I2C_MODE_NONE;
  5160. /* If a DMA is ongoing, Update handle size context */
  5161. if (I2C_CHECK_IT_SOURCE(tmpcr1value, I2C_CR1_TXDMAEN) != RESET)
  5162. {
  5163. /* Disable DMA Request */
  5164. hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
  5165. }
  5166. else if (I2C_CHECK_IT_SOURCE(tmpcr1value, I2C_CR1_RXDMAEN) != RESET)
  5167. {
  5168. /* Disable DMA Request */
  5169. hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
  5170. }
  5171. else
  5172. {
  5173. /* Do nothing */
  5174. }
  5175. if (hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN)
  5176. {
  5177. /* Remove HAL_I2C_STATE_SLAVE_BUSY_TX, keep only HAL_I2C_STATE_LISTEN */
  5178. hi2c->State = HAL_I2C_STATE_LISTEN;
  5179. hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_TX;
  5180. /* Disable Interrupts */
  5181. I2C_Disable_IRQ(hi2c, I2C_XFER_TX_IT);
  5182. /* Process Unlocked */
  5183. __HAL_UNLOCK(hi2c);
  5184. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5185. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5186. hi2c->SlaveTxCpltCallback(hi2c);
  5187. #else
  5188. HAL_I2C_SlaveTxCpltCallback(hi2c);
  5189. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5190. }
  5191. else if (hi2c->State == HAL_I2C_STATE_BUSY_RX_LISTEN)
  5192. {
  5193. /* Remove HAL_I2C_STATE_SLAVE_BUSY_RX, keep only HAL_I2C_STATE_LISTEN */
  5194. hi2c->State = HAL_I2C_STATE_LISTEN;
  5195. hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_RX;
  5196. /* Disable Interrupts */
  5197. I2C_Disable_IRQ(hi2c, I2C_XFER_RX_IT);
  5198. /* Process Unlocked */
  5199. __HAL_UNLOCK(hi2c);
  5200. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5201. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5202. hi2c->SlaveRxCpltCallback(hi2c);
  5203. #else
  5204. HAL_I2C_SlaveRxCpltCallback(hi2c);
  5205. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5206. }
  5207. else
  5208. {
  5209. /* Nothing to do */
  5210. }
  5211. }
  5212. /**
  5213. * @brief I2C Master complete process.
  5214. * @param hi2c I2C handle.
  5215. * @param ITFlags Interrupt flags to handle.
  5216. * @retval None
  5217. */
  5218. static void I2C_ITMasterCplt(I2C_HandleTypeDef *hi2c, uint32_t ITFlags)
  5219. {
  5220. uint32_t tmperror;
  5221. uint32_t tmpITFlags = ITFlags;
  5222. __IO uint32_t tmpreg;
  5223. /* Clear STOP Flag */
  5224. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  5225. /* Disable Interrupts and Store Previous state */
  5226. if (hi2c->State == HAL_I2C_STATE_BUSY_TX)
  5227. {
  5228. I2C_Disable_IRQ(hi2c, I2C_XFER_TX_IT);
  5229. hi2c->PreviousState = I2C_STATE_MASTER_BUSY_TX;
  5230. }
  5231. else if (hi2c->State == HAL_I2C_STATE_BUSY_RX)
  5232. {
  5233. I2C_Disable_IRQ(hi2c, I2C_XFER_RX_IT);
  5234. hi2c->PreviousState = I2C_STATE_MASTER_BUSY_RX;
  5235. }
  5236. else
  5237. {
  5238. /* Do nothing */
  5239. }
  5240. /* Clear Configuration Register 2 */
  5241. I2C_RESET_CR2(hi2c);
  5242. /* Reset handle parameters */
  5243. hi2c->XferISR = NULL;
  5244. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  5245. if (I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_AF) != RESET)
  5246. {
  5247. /* Clear NACK Flag */
  5248. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  5249. /* Set acknowledge error code */
  5250. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  5251. }
  5252. /* Fetch Last receive data if any */
  5253. if ((hi2c->State == HAL_I2C_STATE_ABORT) && (I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_RXNE) != RESET))
  5254. {
  5255. /* Read data from RXDR */
  5256. tmpreg = (uint8_t)hi2c->Instance->RXDR;
  5257. UNUSED(tmpreg);
  5258. }
  5259. /* Flush TX register */
  5260. I2C_Flush_TXDR(hi2c);
  5261. /* Store current volatile hi2c->ErrorCode, misra rule */
  5262. tmperror = hi2c->ErrorCode;
  5263. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5264. if ((hi2c->State == HAL_I2C_STATE_ABORT) || (tmperror != HAL_I2C_ERROR_NONE))
  5265. {
  5266. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5267. I2C_ITError(hi2c, hi2c->ErrorCode);
  5268. }
  5269. /* hi2c->State == HAL_I2C_STATE_BUSY_TX */
  5270. else if (hi2c->State == HAL_I2C_STATE_BUSY_TX)
  5271. {
  5272. hi2c->State = HAL_I2C_STATE_READY;
  5273. hi2c->PreviousState = I2C_STATE_NONE;
  5274. if (hi2c->Mode == HAL_I2C_MODE_MEM)
  5275. {
  5276. hi2c->Mode = HAL_I2C_MODE_NONE;
  5277. /* Process Unlocked */
  5278. __HAL_UNLOCK(hi2c);
  5279. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5280. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5281. hi2c->MemTxCpltCallback(hi2c);
  5282. #else
  5283. HAL_I2C_MemTxCpltCallback(hi2c);
  5284. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5285. }
  5286. else
  5287. {
  5288. hi2c->Mode = HAL_I2C_MODE_NONE;
  5289. /* Process Unlocked */
  5290. __HAL_UNLOCK(hi2c);
  5291. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5292. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5293. hi2c->MasterTxCpltCallback(hi2c);
  5294. #else
  5295. HAL_I2C_MasterTxCpltCallback(hi2c);
  5296. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5297. }
  5298. }
  5299. /* hi2c->State == HAL_I2C_STATE_BUSY_RX */
  5300. else if (hi2c->State == HAL_I2C_STATE_BUSY_RX)
  5301. {
  5302. hi2c->State = HAL_I2C_STATE_READY;
  5303. hi2c->PreviousState = I2C_STATE_NONE;
  5304. if (hi2c->Mode == HAL_I2C_MODE_MEM)
  5305. {
  5306. hi2c->Mode = HAL_I2C_MODE_NONE;
  5307. /* Process Unlocked */
  5308. __HAL_UNLOCK(hi2c);
  5309. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5310. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5311. hi2c->MemRxCpltCallback(hi2c);
  5312. #else
  5313. HAL_I2C_MemRxCpltCallback(hi2c);
  5314. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5315. }
  5316. else
  5317. {
  5318. hi2c->Mode = HAL_I2C_MODE_NONE;
  5319. /* Process Unlocked */
  5320. __HAL_UNLOCK(hi2c);
  5321. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5322. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5323. hi2c->MasterRxCpltCallback(hi2c);
  5324. #else
  5325. HAL_I2C_MasterRxCpltCallback(hi2c);
  5326. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5327. }
  5328. }
  5329. else
  5330. {
  5331. /* Nothing to do */
  5332. }
  5333. }
  5334. /**
  5335. * @brief I2C Slave complete process.
  5336. * @param hi2c I2C handle.
  5337. * @param ITFlags Interrupt flags to handle.
  5338. * @retval None
  5339. */
  5340. static void I2C_ITSlaveCplt(I2C_HandleTypeDef *hi2c, uint32_t ITFlags)
  5341. {
  5342. uint32_t tmpcr1value = READ_REG(hi2c->Instance->CR1);
  5343. uint32_t tmpITFlags = ITFlags;
  5344. uint32_t tmpoptions = hi2c->XferOptions;
  5345. HAL_I2C_StateTypeDef tmpstate = hi2c->State;
  5346. /* Clear STOP Flag */
  5347. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  5348. /* Disable Interrupts and Store Previous state */
  5349. if ((tmpstate == HAL_I2C_STATE_BUSY_TX) || (tmpstate == HAL_I2C_STATE_BUSY_TX_LISTEN))
  5350. {
  5351. I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_TX_IT);
  5352. hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_TX;
  5353. }
  5354. else if ((tmpstate == HAL_I2C_STATE_BUSY_RX) || (tmpstate == HAL_I2C_STATE_BUSY_RX_LISTEN))
  5355. {
  5356. I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_RX_IT);
  5357. hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_RX;
  5358. }
  5359. else if (tmpstate == HAL_I2C_STATE_LISTEN)
  5360. {
  5361. I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_TX_IT | I2C_XFER_RX_IT);
  5362. hi2c->PreviousState = I2C_STATE_NONE;
  5363. }
  5364. else
  5365. {
  5366. /* Do nothing */
  5367. }
  5368. /* Disable Address Acknowledge */
  5369. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  5370. /* Clear Configuration Register 2 */
  5371. I2C_RESET_CR2(hi2c);
  5372. /* Flush TX register */
  5373. I2C_Flush_TXDR(hi2c);
  5374. /* If a DMA is ongoing, Update handle size context */
  5375. if (I2C_CHECK_IT_SOURCE(tmpcr1value, I2C_CR1_TXDMAEN) != RESET)
  5376. {
  5377. /* Disable DMA Request */
  5378. hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
  5379. if (hi2c->hdmatx != NULL)
  5380. {
  5381. hi2c->XferCount = (uint16_t)I2C_GET_DMA_REMAIN_DATA(hi2c->hdmatx);
  5382. }
  5383. }
  5384. else if (I2C_CHECK_IT_SOURCE(tmpcr1value, I2C_CR1_RXDMAEN) != RESET)
  5385. {
  5386. /* Disable DMA Request */
  5387. hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
  5388. if (hi2c->hdmarx != NULL)
  5389. {
  5390. hi2c->XferCount = (uint16_t)I2C_GET_DMA_REMAIN_DATA(hi2c->hdmarx);
  5391. }
  5392. }
  5393. else
  5394. {
  5395. /* Do nothing */
  5396. }
  5397. /* Store Last receive data if any */
  5398. if (I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_RXNE) != RESET)
  5399. {
  5400. /* Remove RXNE flag on temporary variable as read done */
  5401. tmpITFlags &= ~I2C_FLAG_RXNE;
  5402. /* Read data from RXDR */
  5403. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->RXDR;
  5404. /* Increment Buffer pointer */
  5405. hi2c->pBuffPtr++;
  5406. if ((hi2c->XferSize > 0U))
  5407. {
  5408. hi2c->XferSize--;
  5409. hi2c->XferCount--;
  5410. }
  5411. }
  5412. /* All data are not transferred, so set error code accordingly */
  5413. if (hi2c->XferCount != 0U)
  5414. {
  5415. /* Set ErrorCode corresponding to a Non-Acknowledge */
  5416. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  5417. }
  5418. if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_AF) != RESET) && \
  5419. (I2C_CHECK_IT_SOURCE(tmpcr1value, I2C_IT_NACKI) != RESET))
  5420. {
  5421. /* Check that I2C transfer finished */
  5422. /* if yes, normal use case, a NACK is sent by the MASTER when Transfer is finished */
  5423. /* Mean XferCount == 0*/
  5424. /* So clear Flag NACKF only */
  5425. if (hi2c->XferCount == 0U)
  5426. {
  5427. if ((hi2c->State == HAL_I2C_STATE_LISTEN) && (tmpoptions == I2C_FIRST_AND_LAST_FRAME))
  5428. /* Same action must be done for (tmpoptions == I2C_LAST_FRAME) which removed for
  5429. Warning[Pa134]: left and right operands are identical */
  5430. {
  5431. /* Call I2C Listen complete process */
  5432. I2C_ITListenCplt(hi2c, tmpITFlags);
  5433. }
  5434. else if ((hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN) && (tmpoptions != I2C_NO_OPTION_FRAME))
  5435. {
  5436. /* Clear NACK Flag */
  5437. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  5438. /* Flush TX register */
  5439. I2C_Flush_TXDR(hi2c);
  5440. /* Last Byte is Transmitted */
  5441. /* Call I2C Slave Sequential complete process */
  5442. I2C_ITSlaveSeqCplt(hi2c);
  5443. }
  5444. else
  5445. {
  5446. /* Clear NACK Flag */
  5447. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  5448. }
  5449. }
  5450. else
  5451. {
  5452. /* if no, error use case, a Non-Acknowledge of last Data is generated by the MASTER*/
  5453. /* Clear NACK Flag */
  5454. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  5455. /* Set ErrorCode corresponding to a Non-Acknowledge */
  5456. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  5457. if ((tmpoptions == I2C_FIRST_FRAME) || (tmpoptions == I2C_NEXT_FRAME))
  5458. {
  5459. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5460. I2C_ITError(hi2c, hi2c->ErrorCode);
  5461. }
  5462. }
  5463. }
  5464. hi2c->Mode = HAL_I2C_MODE_NONE;
  5465. hi2c->XferISR = NULL;
  5466. if (hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  5467. {
  5468. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5469. I2C_ITError(hi2c, hi2c->ErrorCode);
  5470. /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */
  5471. if (hi2c->State == HAL_I2C_STATE_LISTEN)
  5472. {
  5473. /* Call I2C Listen complete process */
  5474. I2C_ITListenCplt(hi2c, tmpITFlags);
  5475. }
  5476. }
  5477. else if (hi2c->XferOptions != I2C_NO_OPTION_FRAME)
  5478. {
  5479. /* Call the Sequential Complete callback, to inform upper layer of the end of Transfer */
  5480. I2C_ITSlaveSeqCplt(hi2c);
  5481. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  5482. hi2c->State = HAL_I2C_STATE_READY;
  5483. hi2c->PreviousState = I2C_STATE_NONE;
  5484. /* Process Unlocked */
  5485. __HAL_UNLOCK(hi2c);
  5486. /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */
  5487. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5488. hi2c->ListenCpltCallback(hi2c);
  5489. #else
  5490. HAL_I2C_ListenCpltCallback(hi2c);
  5491. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5492. }
  5493. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5494. else if (hi2c->State == HAL_I2C_STATE_BUSY_RX)
  5495. {
  5496. hi2c->State = HAL_I2C_STATE_READY;
  5497. hi2c->PreviousState = I2C_STATE_NONE;
  5498. /* Process Unlocked */
  5499. __HAL_UNLOCK(hi2c);
  5500. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5501. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5502. hi2c->SlaveRxCpltCallback(hi2c);
  5503. #else
  5504. HAL_I2C_SlaveRxCpltCallback(hi2c);
  5505. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5506. }
  5507. else
  5508. {
  5509. hi2c->State = HAL_I2C_STATE_READY;
  5510. hi2c->PreviousState = I2C_STATE_NONE;
  5511. /* Process Unlocked */
  5512. __HAL_UNLOCK(hi2c);
  5513. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5514. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5515. hi2c->SlaveTxCpltCallback(hi2c);
  5516. #else
  5517. HAL_I2C_SlaveTxCpltCallback(hi2c);
  5518. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5519. }
  5520. }
  5521. /**
  5522. * @brief I2C Listen complete process.
  5523. * @param hi2c I2C handle.
  5524. * @param ITFlags Interrupt flags to handle.
  5525. * @retval None
  5526. */
  5527. static void I2C_ITListenCplt(I2C_HandleTypeDef *hi2c, uint32_t ITFlags)
  5528. {
  5529. /* Reset handle parameters */
  5530. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  5531. hi2c->PreviousState = I2C_STATE_NONE;
  5532. hi2c->State = HAL_I2C_STATE_READY;
  5533. hi2c->Mode = HAL_I2C_MODE_NONE;
  5534. hi2c->XferISR = NULL;
  5535. /* Store Last receive data if any */
  5536. if (I2C_CHECK_FLAG(ITFlags, I2C_FLAG_RXNE) != RESET)
  5537. {
  5538. /* Read data from RXDR */
  5539. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->RXDR;
  5540. /* Increment Buffer pointer */
  5541. hi2c->pBuffPtr++;
  5542. if ((hi2c->XferSize > 0U))
  5543. {
  5544. hi2c->XferSize--;
  5545. hi2c->XferCount--;
  5546. /* Set ErrorCode corresponding to a Non-Acknowledge */
  5547. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  5548. }
  5549. }
  5550. /* Disable all Interrupts*/
  5551. I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_RX_IT | I2C_XFER_TX_IT);
  5552. /* Clear NACK Flag */
  5553. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  5554. /* Process Unlocked */
  5555. __HAL_UNLOCK(hi2c);
  5556. /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */
  5557. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5558. hi2c->ListenCpltCallback(hi2c);
  5559. #else
  5560. HAL_I2C_ListenCpltCallback(hi2c);
  5561. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5562. }
  5563. /**
  5564. * @brief I2C interrupts error process.
  5565. * @param hi2c I2C handle.
  5566. * @param ErrorCode Error code to handle.
  5567. * @retval None
  5568. */
  5569. static void I2C_ITError(I2C_HandleTypeDef *hi2c, uint32_t ErrorCode)
  5570. {
  5571. HAL_I2C_StateTypeDef tmpstate = hi2c->State;
  5572. uint32_t tmppreviousstate;
  5573. /* Reset handle parameters */
  5574. hi2c->Mode = HAL_I2C_MODE_NONE;
  5575. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  5576. hi2c->XferCount = 0U;
  5577. /* Set new error code */
  5578. hi2c->ErrorCode |= ErrorCode;
  5579. /* Disable Interrupts */
  5580. if ((tmpstate == HAL_I2C_STATE_LISTEN) ||
  5581. (tmpstate == HAL_I2C_STATE_BUSY_TX_LISTEN) ||
  5582. (tmpstate == HAL_I2C_STATE_BUSY_RX_LISTEN))
  5583. {
  5584. /* Disable all interrupts, except interrupts related to LISTEN state */
  5585. I2C_Disable_IRQ(hi2c, I2C_XFER_RX_IT | I2C_XFER_TX_IT);
  5586. /* keep HAL_I2C_STATE_LISTEN if set */
  5587. hi2c->State = HAL_I2C_STATE_LISTEN;
  5588. hi2c->XferISR = I2C_Slave_ISR_IT;
  5589. }
  5590. else
  5591. {
  5592. /* Disable all interrupts */
  5593. I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_RX_IT | I2C_XFER_TX_IT);
  5594. /* Flush TX register */
  5595. I2C_Flush_TXDR(hi2c);
  5596. /* If state is an abort treatment on going, don't change state */
  5597. /* This change will be do later */
  5598. if (hi2c->State != HAL_I2C_STATE_ABORT)
  5599. {
  5600. /* Set HAL_I2C_STATE_READY */
  5601. hi2c->State = HAL_I2C_STATE_READY;
  5602. /* Check if a STOPF is detected */
  5603. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET)
  5604. {
  5605. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
  5606. {
  5607. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  5608. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  5609. }
  5610. /* Clear STOP Flag */
  5611. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  5612. }
  5613. }
  5614. hi2c->XferISR = NULL;
  5615. }
  5616. /* Abort DMA TX transfer if any */
  5617. tmppreviousstate = hi2c->PreviousState;
  5618. if ((hi2c->hdmatx != NULL) && ((tmppreviousstate == I2C_STATE_MASTER_BUSY_TX) || \
  5619. (tmppreviousstate == I2C_STATE_SLAVE_BUSY_TX)))
  5620. {
  5621. if ((hi2c->Instance->CR1 & I2C_CR1_TXDMAEN) == I2C_CR1_TXDMAEN)
  5622. {
  5623. hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
  5624. }
  5625. if (HAL_DMA_GetState(hi2c->hdmatx) != HAL_DMA_STATE_READY)
  5626. {
  5627. /* Set the I2C DMA Abort callback :
  5628. will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
  5629. hi2c->hdmatx->XferAbortCallback = I2C_DMAAbort;
  5630. /* Process Unlocked */
  5631. __HAL_UNLOCK(hi2c);
  5632. /* Abort DMA TX */
  5633. if (HAL_DMA_Abort_IT(hi2c->hdmatx) != HAL_OK)
  5634. {
  5635. /* Call Directly XferAbortCallback function in case of error */
  5636. hi2c->hdmatx->XferAbortCallback(hi2c->hdmatx);
  5637. }
  5638. }
  5639. else
  5640. {
  5641. I2C_TreatErrorCallback(hi2c);
  5642. }
  5643. }
  5644. /* Abort DMA RX transfer if any */
  5645. else if ((hi2c->hdmarx != NULL) && ((tmppreviousstate == I2C_STATE_MASTER_BUSY_RX) || \
  5646. (tmppreviousstate == I2C_STATE_SLAVE_BUSY_RX)))
  5647. {
  5648. if ((hi2c->Instance->CR1 & I2C_CR1_RXDMAEN) == I2C_CR1_RXDMAEN)
  5649. {
  5650. hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
  5651. }
  5652. if (HAL_DMA_GetState(hi2c->hdmarx) != HAL_DMA_STATE_READY)
  5653. {
  5654. /* Set the I2C DMA Abort callback :
  5655. will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
  5656. hi2c->hdmarx->XferAbortCallback = I2C_DMAAbort;
  5657. /* Process Unlocked */
  5658. __HAL_UNLOCK(hi2c);
  5659. /* Abort DMA RX */
  5660. if (HAL_DMA_Abort_IT(hi2c->hdmarx) != HAL_OK)
  5661. {
  5662. /* Call Directly hi2c->hdmarx->XferAbortCallback function in case of error */
  5663. hi2c->hdmarx->XferAbortCallback(hi2c->hdmarx);
  5664. }
  5665. }
  5666. else
  5667. {
  5668. I2C_TreatErrorCallback(hi2c);
  5669. }
  5670. }
  5671. else
  5672. {
  5673. I2C_TreatErrorCallback(hi2c);
  5674. }
  5675. }
  5676. /**
  5677. * @brief I2C Error callback treatment.
  5678. * @param hi2c I2C handle.
  5679. * @retval None
  5680. */
  5681. static void I2C_TreatErrorCallback(I2C_HandleTypeDef *hi2c)
  5682. {
  5683. if (hi2c->State == HAL_I2C_STATE_ABORT)
  5684. {
  5685. hi2c->State = HAL_I2C_STATE_READY;
  5686. hi2c->PreviousState = I2C_STATE_NONE;
  5687. /* Process Unlocked */
  5688. __HAL_UNLOCK(hi2c);
  5689. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5690. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5691. hi2c->AbortCpltCallback(hi2c);
  5692. #else
  5693. HAL_I2C_AbortCpltCallback(hi2c);
  5694. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5695. }
  5696. else
  5697. {
  5698. hi2c->PreviousState = I2C_STATE_NONE;
  5699. /* Process Unlocked */
  5700. __HAL_UNLOCK(hi2c);
  5701. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5702. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5703. hi2c->ErrorCallback(hi2c);
  5704. #else
  5705. HAL_I2C_ErrorCallback(hi2c);
  5706. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5707. }
  5708. }
  5709. /**
  5710. * @brief I2C Tx data register flush process.
  5711. * @param hi2c I2C handle.
  5712. * @retval None
  5713. */
  5714. static void I2C_Flush_TXDR(I2C_HandleTypeDef *hi2c)
  5715. {
  5716. /* If a pending TXIS flag is set */
  5717. /* Write a dummy data in TXDR to clear it */
  5718. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXIS) != RESET)
  5719. {
  5720. hi2c->Instance->TXDR = 0x00U;
  5721. }
  5722. /* Flush TX register if not empty */
  5723. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXE) == RESET)
  5724. {
  5725. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_TXE);
  5726. }
  5727. }
  5728. /**
  5729. * @brief DMA I2C master transmit process complete callback.
  5730. * @param hdma DMA handle
  5731. * @retval None
  5732. */
  5733. static void I2C_DMAMasterTransmitCplt(DMA_HandleTypeDef *hdma)
  5734. {
  5735. /* Derogation MISRAC2012-Rule-11.5 */
  5736. I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent);
  5737. /* Disable DMA Request */
  5738. hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
  5739. /* If last transfer, enable STOP interrupt */
  5740. if (hi2c->XferCount == 0U)
  5741. {
  5742. /* Enable STOP interrupt */
  5743. I2C_Enable_IRQ(hi2c, I2C_XFER_CPLT_IT);
  5744. }
  5745. /* else prepare a new DMA transfer and enable TCReload interrupt */
  5746. else
  5747. {
  5748. /* Update Buffer pointer */
  5749. hi2c->pBuffPtr += hi2c->XferSize;
  5750. /* Set the XferSize to transfer */
  5751. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  5752. {
  5753. hi2c->XferSize = MAX_NBYTE_SIZE;
  5754. }
  5755. else
  5756. {
  5757. hi2c->XferSize = hi2c->XferCount;
  5758. }
  5759. /* Enable the DMA stream or channel depends on Instance */
  5760. if (HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->TXDR,
  5761. hi2c->XferSize) != HAL_OK)
  5762. {
  5763. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5764. I2C_ITError(hi2c, HAL_I2C_ERROR_DMA);
  5765. }
  5766. else
  5767. {
  5768. /* Enable TC interrupts */
  5769. I2C_Enable_IRQ(hi2c, I2C_XFER_RELOAD_IT);
  5770. }
  5771. }
  5772. }
  5773. /**
  5774. * @brief DMA I2C slave transmit process complete callback.
  5775. * @param hdma DMA handle
  5776. * @retval None
  5777. */
  5778. static void I2C_DMASlaveTransmitCplt(DMA_HandleTypeDef *hdma)
  5779. {
  5780. /* Derogation MISRAC2012-Rule-11.5 */
  5781. I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent);
  5782. uint32_t tmpoptions = hi2c->XferOptions;
  5783. if ((tmpoptions == I2C_NEXT_FRAME) || (tmpoptions == I2C_FIRST_FRAME))
  5784. {
  5785. /* Disable DMA Request */
  5786. hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
  5787. /* Last Byte is Transmitted */
  5788. /* Call I2C Slave Sequential complete process */
  5789. I2C_ITSlaveSeqCplt(hi2c);
  5790. }
  5791. else
  5792. {
  5793. /* No specific action, Master fully manage the generation of STOP condition */
  5794. /* Mean that this generation can arrive at any time, at the end or during DMA process */
  5795. /* So STOP condition should be manage through Interrupt treatment */
  5796. }
  5797. }
  5798. /**
  5799. * @brief DMA I2C master receive process complete callback.
  5800. * @param hdma DMA handle
  5801. * @retval None
  5802. */
  5803. static void I2C_DMAMasterReceiveCplt(DMA_HandleTypeDef *hdma)
  5804. {
  5805. /* Derogation MISRAC2012-Rule-11.5 */
  5806. I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent);
  5807. /* Disable DMA Request */
  5808. hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
  5809. /* If last transfer, enable STOP interrupt */
  5810. if (hi2c->XferCount == 0U)
  5811. {
  5812. /* Enable STOP interrupt */
  5813. I2C_Enable_IRQ(hi2c, I2C_XFER_CPLT_IT);
  5814. }
  5815. /* else prepare a new DMA transfer and enable TCReload interrupt */
  5816. else
  5817. {
  5818. /* Update Buffer pointer */
  5819. hi2c->pBuffPtr += hi2c->XferSize;
  5820. /* Set the XferSize to transfer */
  5821. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  5822. {
  5823. hi2c->XferSize = MAX_NBYTE_SIZE;
  5824. }
  5825. else
  5826. {
  5827. hi2c->XferSize = hi2c->XferCount;
  5828. }
  5829. /* Enable the DMA stream or channel depends on Instance */
  5830. if (HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)hi2c->pBuffPtr,
  5831. hi2c->XferSize) != HAL_OK)
  5832. {
  5833. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5834. I2C_ITError(hi2c, HAL_I2C_ERROR_DMA);
  5835. }
  5836. else
  5837. {
  5838. /* Enable TC interrupts */
  5839. I2C_Enable_IRQ(hi2c, I2C_XFER_RELOAD_IT);
  5840. }
  5841. }
  5842. }
  5843. /**
  5844. * @brief DMA I2C slave receive process complete callback.
  5845. * @param hdma DMA handle
  5846. * @retval None
  5847. */
  5848. static void I2C_DMASlaveReceiveCplt(DMA_HandleTypeDef *hdma)
  5849. {
  5850. /* Derogation MISRAC2012-Rule-11.5 */
  5851. I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent);
  5852. uint32_t tmpoptions = hi2c->XferOptions;
  5853. if ((I2C_GET_DMA_REMAIN_DATA(hi2c->hdmarx) == 0U) && \
  5854. (tmpoptions != I2C_NO_OPTION_FRAME))
  5855. {
  5856. /* Disable DMA Request */
  5857. hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
  5858. /* Call I2C Slave Sequential complete process */
  5859. I2C_ITSlaveSeqCplt(hi2c);
  5860. }
  5861. else
  5862. {
  5863. /* No specific action, Master fully manage the generation of STOP condition */
  5864. /* Mean that this generation can arrive at any time, at the end or during DMA process */
  5865. /* So STOP condition should be manage through Interrupt treatment */
  5866. }
  5867. }
  5868. /**
  5869. * @brief DMA I2C communication error callback.
  5870. * @param hdma DMA handle
  5871. * @retval None
  5872. */
  5873. static void I2C_DMAError(DMA_HandleTypeDef *hdma)
  5874. {
  5875. uint32_t treatdmaerror = 0U;
  5876. /* Derogation MISRAC2012-Rule-11.5 */
  5877. I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent);
  5878. if (hi2c->hdmatx != NULL)
  5879. {
  5880. if (I2C_GET_DMA_REMAIN_DATA(hi2c->hdmatx) == 0U)
  5881. {
  5882. treatdmaerror = 1U;
  5883. }
  5884. }
  5885. if (hi2c->hdmarx != NULL)
  5886. {
  5887. if (I2C_GET_DMA_REMAIN_DATA(hi2c->hdmarx) == 0U)
  5888. {
  5889. treatdmaerror = 1U;
  5890. }
  5891. }
  5892. /* Check if a FIFO error is detected, if true normal use case, so no specific action to perform */
  5893. if (!((HAL_DMA_GetError(hdma) == HAL_DMA_ERROR_FE)) && (treatdmaerror != 0U))
  5894. {
  5895. /* Disable Acknowledge */
  5896. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  5897. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5898. I2C_ITError(hi2c, HAL_I2C_ERROR_DMA);
  5899. }
  5900. }
  5901. /**
  5902. * @brief DMA I2C communication abort callback
  5903. * (To be called at end of DMA Abort procedure).
  5904. * @param hdma DMA handle.
  5905. * @retval None
  5906. */
  5907. static void I2C_DMAAbort(DMA_HandleTypeDef *hdma)
  5908. {
  5909. /* Derogation MISRAC2012-Rule-11.5 */
  5910. I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent);
  5911. /* Reset AbortCpltCallback */
  5912. if (hi2c->hdmatx != NULL)
  5913. {
  5914. hi2c->hdmatx->XferAbortCallback = NULL;
  5915. }
  5916. if (hi2c->hdmarx != NULL)
  5917. {
  5918. hi2c->hdmarx->XferAbortCallback = NULL;
  5919. }
  5920. I2C_TreatErrorCallback(hi2c);
  5921. }
  5922. /**
  5923. * @brief This function handles I2C Communication Timeout. It waits
  5924. * until a flag is no longer in the specified status.
  5925. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  5926. * the configuration information for the specified I2C.
  5927. * @param Flag Specifies the I2C flag to check.
  5928. * @param Status The actual Flag status (SET or RESET).
  5929. * @param Timeout Timeout duration
  5930. * @param Tickstart Tick start value
  5931. * @retval HAL status
  5932. */
  5933. static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status,
  5934. uint32_t Timeout, uint32_t Tickstart)
  5935. {
  5936. while (__HAL_I2C_GET_FLAG(hi2c, Flag) == Status)
  5937. {
  5938. /* Check if an error is detected */
  5939. if (I2C_IsErrorOccurred(hi2c, Timeout, Tickstart) != HAL_OK)
  5940. {
  5941. return HAL_ERROR;
  5942. }
  5943. /* Check for the Timeout */
  5944. if (Timeout != HAL_MAX_DELAY)
  5945. {
  5946. if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
  5947. {
  5948. if ((__HAL_I2C_GET_FLAG(hi2c, Flag) == Status))
  5949. {
  5950. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  5951. hi2c->State = HAL_I2C_STATE_READY;
  5952. hi2c->Mode = HAL_I2C_MODE_NONE;
  5953. /* Process Unlocked */
  5954. __HAL_UNLOCK(hi2c);
  5955. return HAL_ERROR;
  5956. }
  5957. }
  5958. }
  5959. }
  5960. return HAL_OK;
  5961. }
  5962. /**
  5963. * @brief This function handles I2C Communication Timeout for specific usage of TXIS flag.
  5964. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  5965. * the configuration information for the specified I2C.
  5966. * @param Timeout Timeout duration
  5967. * @param Tickstart Tick start value
  5968. * @retval HAL status
  5969. */
  5970. static HAL_StatusTypeDef I2C_WaitOnTXISFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout,
  5971. uint32_t Tickstart)
  5972. {
  5973. while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXIS) == RESET)
  5974. {
  5975. /* Check if an error is detected */
  5976. if (I2C_IsErrorOccurred(hi2c, Timeout, Tickstart) != HAL_OK)
  5977. {
  5978. return HAL_ERROR;
  5979. }
  5980. /* Check for the Timeout */
  5981. if (Timeout != HAL_MAX_DELAY)
  5982. {
  5983. if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
  5984. {
  5985. if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXIS) == RESET))
  5986. {
  5987. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  5988. hi2c->State = HAL_I2C_STATE_READY;
  5989. hi2c->Mode = HAL_I2C_MODE_NONE;
  5990. /* Process Unlocked */
  5991. __HAL_UNLOCK(hi2c);
  5992. return HAL_ERROR;
  5993. }
  5994. }
  5995. }
  5996. }
  5997. return HAL_OK;
  5998. }
  5999. /**
  6000. * @brief This function handles I2C Communication Timeout for specific usage of STOP flag.
  6001. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  6002. * the configuration information for the specified I2C.
  6003. * @param Timeout Timeout duration
  6004. * @param Tickstart Tick start value
  6005. * @retval HAL status
  6006. */
  6007. static HAL_StatusTypeDef I2C_WaitOnSTOPFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout,
  6008. uint32_t Tickstart)
  6009. {
  6010. while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET)
  6011. {
  6012. /* Check if an error is detected */
  6013. if (I2C_IsErrorOccurred(hi2c, Timeout, Tickstart) != HAL_OK)
  6014. {
  6015. return HAL_ERROR;
  6016. }
  6017. /* Check for the Timeout */
  6018. if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
  6019. {
  6020. if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET))
  6021. {
  6022. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  6023. hi2c->State = HAL_I2C_STATE_READY;
  6024. hi2c->Mode = HAL_I2C_MODE_NONE;
  6025. /* Process Unlocked */
  6026. __HAL_UNLOCK(hi2c);
  6027. return HAL_ERROR;
  6028. }
  6029. }
  6030. }
  6031. return HAL_OK;
  6032. }
  6033. /**
  6034. * @brief This function handles I2C Communication Timeout for specific usage of RXNE flag.
  6035. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  6036. * the configuration information for the specified I2C.
  6037. * @param Timeout Timeout duration
  6038. * @param Tickstart Tick start value
  6039. * @retval HAL status
  6040. */
  6041. static HAL_StatusTypeDef I2C_WaitOnRXNEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout,
  6042. uint32_t Tickstart)
  6043. {
  6044. HAL_StatusTypeDef status = HAL_OK;
  6045. while ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == RESET) && (status == HAL_OK))
  6046. {
  6047. /* Check if an error is detected */
  6048. if (I2C_IsErrorOccurred(hi2c, Timeout, Tickstart) != HAL_OK)
  6049. {
  6050. status = HAL_ERROR;
  6051. }
  6052. /* Check if a STOPF is detected */
  6053. if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET) && (status == HAL_OK))
  6054. {
  6055. /* Check if an RXNE is pending */
  6056. /* Store Last receive data if any */
  6057. if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET) && (hi2c->XferSize > 0U))
  6058. {
  6059. /* Return HAL_OK */
  6060. /* The Reading of data from RXDR will be done in caller function */
  6061. status = HAL_OK;
  6062. }
  6063. /* Check a no-acknowledge have been detected */
  6064. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
  6065. {
  6066. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  6067. hi2c->ErrorCode = HAL_I2C_ERROR_AF;
  6068. /* Clear STOP Flag */
  6069. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  6070. /* Clear Configuration Register 2 */
  6071. I2C_RESET_CR2(hi2c);
  6072. hi2c->State = HAL_I2C_STATE_READY;
  6073. hi2c->Mode = HAL_I2C_MODE_NONE;
  6074. /* Process Unlocked */
  6075. __HAL_UNLOCK(hi2c);
  6076. status = HAL_ERROR;
  6077. }
  6078. else
  6079. {
  6080. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  6081. }
  6082. }
  6083. /* Check for the Timeout */
  6084. if ((((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U)) && (status == HAL_OK))
  6085. {
  6086. if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == RESET))
  6087. {
  6088. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  6089. hi2c->State = HAL_I2C_STATE_READY;
  6090. /* Process Unlocked */
  6091. __HAL_UNLOCK(hi2c);
  6092. status = HAL_ERROR;
  6093. }
  6094. }
  6095. }
  6096. return status;
  6097. }
  6098. /**
  6099. * @brief This function handles errors detection during an I2C Communication.
  6100. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  6101. * the configuration information for the specified I2C.
  6102. * @param Timeout Timeout duration
  6103. * @param Tickstart Tick start value
  6104. * @retval HAL status
  6105. */
  6106. static HAL_StatusTypeDef I2C_IsErrorOccurred(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart)
  6107. {
  6108. HAL_StatusTypeDef status = HAL_OK;
  6109. uint32_t itflag = hi2c->Instance->ISR;
  6110. uint32_t error_code = 0;
  6111. uint32_t tickstart = Tickstart;
  6112. uint32_t tmp1;
  6113. HAL_I2C_ModeTypeDef tmp2;
  6114. if (HAL_IS_BIT_SET(itflag, I2C_FLAG_AF))
  6115. {
  6116. /* Clear NACKF Flag */
  6117. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  6118. /* Wait until STOP Flag is set or timeout occurred */
  6119. /* AutoEnd should be initiate after AF */
  6120. while ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET) && (status == HAL_OK))
  6121. {
  6122. /* Check for the Timeout */
  6123. if (Timeout != HAL_MAX_DELAY)
  6124. {
  6125. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  6126. {
  6127. tmp1 = (uint32_t)(hi2c->Instance->CR2 & I2C_CR2_STOP);
  6128. tmp2 = hi2c->Mode;
  6129. /* In case of I2C still busy, try to regenerate a STOP manually */
  6130. if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET) && \
  6131. (tmp1 != I2C_CR2_STOP) && \
  6132. (tmp2 != HAL_I2C_MODE_SLAVE))
  6133. {
  6134. /* Generate Stop */
  6135. hi2c->Instance->CR2 |= I2C_CR2_STOP;
  6136. /* Update Tick with new reference */
  6137. tickstart = HAL_GetTick();
  6138. }
  6139. while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET)
  6140. {
  6141. /* Check for the Timeout */
  6142. if ((HAL_GetTick() - tickstart) > I2C_TIMEOUT_STOPF)
  6143. {
  6144. error_code |= HAL_I2C_ERROR_TIMEOUT;
  6145. status = HAL_ERROR;
  6146. break;
  6147. }
  6148. }
  6149. }
  6150. }
  6151. }
  6152. /* In case STOP Flag is detected, clear it */
  6153. if (status == HAL_OK)
  6154. {
  6155. /* Clear STOP Flag */
  6156. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  6157. }
  6158. error_code |= HAL_I2C_ERROR_AF;
  6159. status = HAL_ERROR;
  6160. }
  6161. /* Refresh Content of Status register */
  6162. itflag = hi2c->Instance->ISR;
  6163. /* Then verify if an additional errors occurs */
  6164. /* Check if a Bus error occurred */
  6165. if (HAL_IS_BIT_SET(itflag, I2C_FLAG_BERR))
  6166. {
  6167. error_code |= HAL_I2C_ERROR_BERR;
  6168. /* Clear BERR flag */
  6169. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_BERR);
  6170. status = HAL_ERROR;
  6171. }
  6172. /* Check if an Over-Run/Under-Run error occurred */
  6173. if (HAL_IS_BIT_SET(itflag, I2C_FLAG_OVR))
  6174. {
  6175. error_code |= HAL_I2C_ERROR_OVR;
  6176. /* Clear OVR flag */
  6177. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_OVR);
  6178. status = HAL_ERROR;
  6179. }
  6180. /* Check if an Arbitration Loss error occurred */
  6181. if (HAL_IS_BIT_SET(itflag, I2C_FLAG_ARLO))
  6182. {
  6183. error_code |= HAL_I2C_ERROR_ARLO;
  6184. /* Clear ARLO flag */
  6185. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ARLO);
  6186. status = HAL_ERROR;
  6187. }
  6188. if (status != HAL_OK)
  6189. {
  6190. /* Flush TX register */
  6191. I2C_Flush_TXDR(hi2c);
  6192. /* Clear Configuration Register 2 */
  6193. I2C_RESET_CR2(hi2c);
  6194. hi2c->ErrorCode |= error_code;
  6195. hi2c->State = HAL_I2C_STATE_READY;
  6196. hi2c->Mode = HAL_I2C_MODE_NONE;
  6197. /* Process Unlocked */
  6198. __HAL_UNLOCK(hi2c);
  6199. }
  6200. return status;
  6201. }
  6202. /**
  6203. * @brief Handles I2Cx communication when starting transfer or during transfer (TC or TCR flag are set).
  6204. * @param hi2c I2C handle.
  6205. * @param DevAddress Specifies the slave address to be programmed.
  6206. * @param Size Specifies the number of bytes to be programmed.
  6207. * This parameter must be a value between 0 and 255.
  6208. * @param Mode New state of the I2C START condition generation.
  6209. * This parameter can be one of the following values:
  6210. * @arg @ref I2C_RELOAD_MODE Enable Reload mode .
  6211. * @arg @ref I2C_AUTOEND_MODE Enable Automatic end mode.
  6212. * @arg @ref I2C_SOFTEND_MODE Enable Software end mode.
  6213. * @param Request New state of the I2C START condition generation.
  6214. * This parameter can be one of the following values:
  6215. * @arg @ref I2C_NO_STARTSTOP Don't Generate stop and start condition.
  6216. * @arg @ref I2C_GENERATE_STOP Generate stop condition (Size should be set to 0).
  6217. * @arg @ref I2C_GENERATE_START_READ Generate Restart for read request.
  6218. * @arg @ref I2C_GENERATE_START_WRITE Generate Restart for write request.
  6219. * @retval None
  6220. */
  6221. static void I2C_TransferConfig(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t Size, uint32_t Mode,
  6222. uint32_t Request)
  6223. {
  6224. /* Check the parameters */
  6225. assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
  6226. assert_param(IS_TRANSFER_MODE(Mode));
  6227. assert_param(IS_TRANSFER_REQUEST(Request));
  6228. /* Declaration of tmp to prevent undefined behavior of volatile usage */
  6229. uint32_t tmp = ((uint32_t)(((uint32_t)DevAddress & I2C_CR2_SADD) | \
  6230. (((uint32_t)Size << I2C_CR2_NBYTES_Pos) & I2C_CR2_NBYTES) | \
  6231. (uint32_t)Mode | (uint32_t)Request) & (~0x80000000U));
  6232. /* update CR2 register */
  6233. MODIFY_REG(hi2c->Instance->CR2, \
  6234. ((I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | \
  6235. (I2C_CR2_RD_WRN & (uint32_t)(Request >> (31U - I2C_CR2_RD_WRN_Pos))) | \
  6236. I2C_CR2_START | I2C_CR2_STOP)), tmp);
  6237. }
  6238. /**
  6239. * @brief Manage the enabling of Interrupts.
  6240. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  6241. * the configuration information for the specified I2C.
  6242. * @param InterruptRequest Value of @ref I2C_Interrupt_configuration_definition.
  6243. * @retval None
  6244. */
  6245. static void I2C_Enable_IRQ(I2C_HandleTypeDef *hi2c, uint16_t InterruptRequest)
  6246. {
  6247. uint32_t tmpisr = 0U;
  6248. if ((hi2c->XferISR != I2C_Master_ISR_DMA) && \
  6249. (hi2c->XferISR != I2C_Slave_ISR_DMA) && \
  6250. (hi2c->XferISR != I2C_Mem_ISR_DMA))
  6251. {
  6252. if ((InterruptRequest & I2C_XFER_LISTEN_IT) == I2C_XFER_LISTEN_IT)
  6253. {
  6254. /* Enable ERR, STOP, NACK and ADDR interrupts */
  6255. tmpisr |= I2C_IT_ADDRI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_ERRI;
  6256. }
  6257. if ((InterruptRequest & I2C_XFER_TX_IT) == I2C_XFER_TX_IT)
  6258. {
  6259. /* Enable ERR, TC, STOP, NACK and TXI interrupts */
  6260. tmpisr |= I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_TXI;
  6261. }
  6262. if ((InterruptRequest & I2C_XFER_RX_IT) == I2C_XFER_RX_IT)
  6263. {
  6264. /* Enable ERR, TC, STOP, NACK and RXI interrupts */
  6265. tmpisr |= I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_RXI;
  6266. }
  6267. if (InterruptRequest == I2C_XFER_ERROR_IT)
  6268. {
  6269. /* Enable ERR and NACK interrupts */
  6270. tmpisr |= I2C_IT_ERRI | I2C_IT_NACKI;
  6271. }
  6272. if (InterruptRequest == I2C_XFER_CPLT_IT)
  6273. {
  6274. /* Enable STOP interrupts */
  6275. tmpisr |= I2C_IT_STOPI;
  6276. }
  6277. }
  6278. else
  6279. {
  6280. if ((InterruptRequest & I2C_XFER_LISTEN_IT) == I2C_XFER_LISTEN_IT)
  6281. {
  6282. /* Enable ERR, STOP, NACK and ADDR interrupts */
  6283. tmpisr |= I2C_IT_ADDRI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_ERRI;
  6284. }
  6285. if ((InterruptRequest & I2C_XFER_TX_IT) == I2C_XFER_TX_IT)
  6286. {
  6287. /* Enable ERR, TC, STOP, NACK and TXI interrupts */
  6288. tmpisr |= I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_TXI;
  6289. }
  6290. if ((InterruptRequest & I2C_XFER_RX_IT) == I2C_XFER_RX_IT)
  6291. {
  6292. /* Enable ERR, TC, STOP, NACK and RXI interrupts */
  6293. tmpisr |= I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_RXI;
  6294. }
  6295. if (InterruptRequest == I2C_XFER_ERROR_IT)
  6296. {
  6297. /* Enable ERR and NACK interrupts */
  6298. tmpisr |= I2C_IT_ERRI | I2C_IT_NACKI;
  6299. }
  6300. if (InterruptRequest == I2C_XFER_CPLT_IT)
  6301. {
  6302. /* Enable STOP interrupts */
  6303. tmpisr |= (I2C_IT_STOPI | I2C_IT_TCI);
  6304. }
  6305. if (InterruptRequest == I2C_XFER_RELOAD_IT)
  6306. {
  6307. /* Enable TC interrupts */
  6308. tmpisr |= I2C_IT_TCI;
  6309. }
  6310. }
  6311. /* Enable interrupts only at the end */
  6312. /* to avoid the risk of I2C interrupt handle execution before */
  6313. /* all interrupts requested done */
  6314. __HAL_I2C_ENABLE_IT(hi2c, tmpisr);
  6315. }
  6316. /**
  6317. * @brief Manage the disabling of Interrupts.
  6318. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  6319. * the configuration information for the specified I2C.
  6320. * @param InterruptRequest Value of @ref I2C_Interrupt_configuration_definition.
  6321. * @retval None
  6322. */
  6323. static void I2C_Disable_IRQ(I2C_HandleTypeDef *hi2c, uint16_t InterruptRequest)
  6324. {
  6325. uint32_t tmpisr = 0U;
  6326. if ((InterruptRequest & I2C_XFER_TX_IT) == I2C_XFER_TX_IT)
  6327. {
  6328. /* Disable TC and TXI interrupts */
  6329. tmpisr |= I2C_IT_TCI | I2C_IT_TXI;
  6330. if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) != (uint32_t)HAL_I2C_STATE_LISTEN)
  6331. {
  6332. /* Disable NACK and STOP interrupts */
  6333. tmpisr |= I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_ERRI;
  6334. }
  6335. }
  6336. if ((InterruptRequest & I2C_XFER_RX_IT) == I2C_XFER_RX_IT)
  6337. {
  6338. /* Disable TC and RXI interrupts */
  6339. tmpisr |= I2C_IT_TCI | I2C_IT_RXI;
  6340. if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) != (uint32_t)HAL_I2C_STATE_LISTEN)
  6341. {
  6342. /* Disable NACK and STOP interrupts */
  6343. tmpisr |= I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_ERRI;
  6344. }
  6345. }
  6346. if ((InterruptRequest & I2C_XFER_LISTEN_IT) == I2C_XFER_LISTEN_IT)
  6347. {
  6348. /* Disable ADDR, NACK and STOP interrupts */
  6349. tmpisr |= I2C_IT_ADDRI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_ERRI;
  6350. }
  6351. if (InterruptRequest == I2C_XFER_ERROR_IT)
  6352. {
  6353. /* Enable ERR and NACK interrupts */
  6354. tmpisr |= I2C_IT_ERRI | I2C_IT_NACKI;
  6355. }
  6356. if (InterruptRequest == I2C_XFER_CPLT_IT)
  6357. {
  6358. /* Enable STOP interrupts */
  6359. tmpisr |= I2C_IT_STOPI;
  6360. }
  6361. if (InterruptRequest == I2C_XFER_RELOAD_IT)
  6362. {
  6363. /* Enable TC interrupts */
  6364. tmpisr |= I2C_IT_TCI;
  6365. }
  6366. /* Disable interrupts only at the end */
  6367. /* to avoid a breaking situation like at "t" time */
  6368. /* all disable interrupts request are not done */
  6369. __HAL_I2C_DISABLE_IT(hi2c, tmpisr);
  6370. }
  6371. /**
  6372. * @brief Convert I2Cx OTHER_xxx XferOptions to functional XferOptions.
  6373. * @param hi2c I2C handle.
  6374. * @retval None
  6375. */
  6376. static void I2C_ConvertOtherXferOptions(I2C_HandleTypeDef *hi2c)
  6377. {
  6378. /* if user set XferOptions to I2C_OTHER_FRAME */
  6379. /* it request implicitly to generate a restart condition */
  6380. /* set XferOptions to I2C_FIRST_FRAME */
  6381. if (hi2c->XferOptions == I2C_OTHER_FRAME)
  6382. {
  6383. hi2c->XferOptions = I2C_FIRST_FRAME;
  6384. }
  6385. /* else if user set XferOptions to I2C_OTHER_AND_LAST_FRAME */
  6386. /* it request implicitly to generate a restart condition */
  6387. /* then generate a stop condition at the end of transfer */
  6388. /* set XferOptions to I2C_FIRST_AND_LAST_FRAME */
  6389. else if (hi2c->XferOptions == I2C_OTHER_AND_LAST_FRAME)
  6390. {
  6391. hi2c->XferOptions = I2C_FIRST_AND_LAST_FRAME;
  6392. }
  6393. else
  6394. {
  6395. /* Nothing to do */
  6396. }
  6397. }
  6398. /**
  6399. * @}
  6400. */
  6401. #endif /* HAL_I2C_MODULE_ENABLED */
  6402. /**
  6403. * @}
  6404. */
  6405. /**
  6406. * @}
  6407. */