stm32f7xx_hal_i2c.c 220 KB

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