stm32f1xx_hal_i2c.c 247 KB

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