stm32f4xx_hal_i2c.c 234 KB

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