stm32f1xx_hal_i2c.c 234 KB

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