stm32f4xx_hal_fmpi2c.c 222 KB

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