You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stm32l1xx_hal_i2c.c 223KB

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