py32f0xx_hal_i2c.c 238 KB

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