stm32f7xx_hal_i2c.c 236 KB

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