stm32f2xx_hal_i2c.c 224 KB

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