stm32l1xx_hal_i2c.c 162 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343
  1. /**
  2. ******************************************************************************
  3. * @file stm32l1xx_hal_i2c.c
  4. * @author MCD Application Team
  5. * @brief I2C HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Inter Integrated Circuit (I2C) peripheral:
  8. * + Initialization and de-initialization functions
  9. * + IO operation functions
  10. * + Peripheral State, Mode and Error functions
  11. *
  12. @verbatim
  13. ==============================================================================
  14. ##### How to use this driver #####
  15. ==============================================================================
  16. [..]
  17. The I2C HAL driver can be used as follows:
  18. (#) Declare a I2C_HandleTypeDef handle structure, for example:
  19. I2C_HandleTypeDef hi2c;
  20. (#)Initialize the I2C low level resources by implementing the HAL_I2C_MspInit() API:
  21. (##) Enable the I2Cx interface clock
  22. (##) I2C pins configuration
  23. (+++) Enable the clock for the I2C GPIOs
  24. (+++) Configure I2C pins as alternate function open-drain
  25. (##) NVIC configuration if you need to use interrupt process
  26. (+++) Configure the I2Cx interrupt priority
  27. (+++) Enable the NVIC I2C IRQ Channel
  28. (##) DMA Configuration if you need to use DMA process
  29. (+++) Declare a DMA_HandleTypeDef handle structure for the transmit or receive channel
  30. (+++) Enable the DMAx interface clock using
  31. (+++) Configure the DMA handle parameters
  32. (+++) Configure the DMA Tx or Rx channel
  33. (+++) Associate the initialized DMA handle to the hi2c DMA Tx or Rx handle
  34. (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on
  35. the DMA Tx or Rx channel
  36. (#) Configure the Communication Speed, Duty cycle, Addressing mode, Own Address1,
  37. Dual Addressing mode, Own Address2, General call and Nostretch mode in the hi2c Init structure.
  38. (#) Initialize the I2C registers by calling the HAL_I2C_Init(), configures also the low level Hardware
  39. (GPIO, CLOCK, NVIC...etc) by calling the customized HAL_I2C_MspInit(&hi2c) API.
  40. (#) To check if target device is ready for communication, use the function HAL_I2C_IsDeviceReady()
  41. (#) For I2C IO and IO MEM operations, three operation modes are available within this driver :
  42. *** Polling mode IO operation ***
  43. =================================
  44. [..]
  45. (+) Transmit in master mode an amount of data in blocking mode using HAL_I2C_Master_Transmit()
  46. (+) Receive in master mode an amount of data in blocking mode using HAL_I2C_Master_Receive()
  47. (+) Transmit in slave mode an amount of data in blocking mode using HAL_I2C_Slave_Transmit()
  48. (+) Receive in slave mode an amount of data in blocking mode using HAL_I2C_Slave_Receive()
  49. *** Polling mode IO MEM operation ***
  50. =====================================
  51. [..]
  52. (+) Write an amount of data in blocking mode to a specific memory address using HAL_I2C_Mem_Write()
  53. (+) Read an amount of data in blocking mode from a specific memory address using HAL_I2C_Mem_Read()
  54. *** Interrupt mode IO operation ***
  55. ===================================
  56. [..]
  57. (+) Transmit in master mode an amount of data in non-blocking mode using HAL_I2C_Master_Transmit_IT()
  58. (+) At transmission end of transfer, HAL_I2C_MasterTxCpltCallback() is executed and user can
  59. add his own code by customization of function pointer HAL_I2C_MasterTxCpltCallback()
  60. (+) Receive in master mode an amount of data in non-blocking mode using HAL_I2C_Master_Receive_IT()
  61. (+) At reception end of transfer, HAL_I2C_MasterRxCpltCallback() is executed and user can
  62. add his own code by customization of function pointer HAL_I2C_MasterRxCpltCallback()
  63. (+) Transmit in slave mode an amount of data in non-blocking mode using HAL_I2C_Slave_Transmit_IT()
  64. (+) At transmission end of transfer, HAL_I2C_SlaveTxCpltCallback() is executed and user can
  65. add his own code by customization of function pointer HAL_I2C_SlaveTxCpltCallback()
  66. (+) Receive in slave mode an amount of data in non-blocking mode using HAL_I2C_Slave_Receive_IT()
  67. (+) At reception end of transfer, HAL_I2C_SlaveRxCpltCallback() is executed and user can
  68. add his own code by customization of function pointer HAL_I2C_SlaveRxCpltCallback()
  69. (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
  70. add his own code by customization of function pointer HAL_I2C_ErrorCallback()
  71. (+) Abort a master I2C process communication with Interrupt using HAL_I2C_Master_Abort_IT()
  72. (+) End of abort process, HAL_I2C_AbortCpltCallback() is executed and user can
  73. add his own code by customization of function pointer HAL_I2C_AbortCpltCallback()
  74. (+) Discard a slave I2C process communication using __HAL_I2C_GENERATE_NACK() macro.
  75. This action will inform Master to generate a Stop condition to discard the communication.
  76. *** Interrupt mode IO sequential operation ***
  77. ==============================================
  78. [..]
  79. (@) These interfaces allow to manage a sequential transfer with a repeated start condition
  80. when a direction change during transfer
  81. [..]
  82. (+) A specific option field manage the different steps of a sequential transfer
  83. (+) Option field values are defined through @ref I2C_XFEROPTIONS and are listed below:
  84. (++) I2C_FIRST_AND_LAST_FRAME: No sequential usage, functionnal is same as associated interfaces in no sequential mode
  85. (++) I2C_FIRST_FRAME: Sequential usage, this option allow to manage a sequence with start condition, address
  86. and data to transfer without a final stop condition
  87. (++) I2C_NEXT_FRAME: Sequential usage, this option allow to manage a sequence with a restart condition, address
  88. and with new data to transfer if the direction change or manage only the new data to transfer
  89. if no direction change and without a final stop condition in both cases
  90. (++) I2C_LAST_FRAME: Sequential usage, this option allow to manage a sequance with a restart condition, address
  91. and with new data to transfer if the direction change or manage only the new data to transfer
  92. if no direction change and with a final stop condition in both cases
  93. (+) Differents sequential I2C interfaces are listed below:
  94. (++) Sequential transmit in master I2C mode an amount of data in non-blocking mode using HAL_I2C_Master_Sequential_Transmit_IT()
  95. (+++) At transmission end of current frame transfer, HAL_I2C_MasterTxCpltCallback() is executed and user can
  96. add his own code by customization of function pointer HAL_I2C_MasterTxCpltCallback()
  97. (++) Sequential receive in master I2C mode an amount of data in non-blocking mode using HAL_I2C_Master_Sequential_Receive_IT()
  98. (+++) At reception end of current frame transfer, HAL_I2C_MasterRxCpltCallback() is executed and user can
  99. add his own code by customization of function pointer HAL_I2C_MasterRxCpltCallback()
  100. (++) Abort a master I2C process communication with Interrupt using HAL_I2C_Master_Abort_IT()
  101. (+++) End of abort process, HAL_I2C_AbortCpltCallback() is executed and user can
  102. add his own code by customization of function pointer HAL_I2C_AbortCpltCallback()
  103. (++) Enable/disable the Address listen mode in slave I2C mode using HAL_I2C_EnableListen_IT() HAL_I2C_DisableListen_IT()
  104. (+++) When address slave I2C match, HAL_I2C_AddrCallback() is executed and user can
  105. add his own code to check the Address Match Code and the transmission direction request by master (Write/Read).
  106. (+++) At Listen mode end HAL_I2C_ListenCpltCallback() is executed and user can
  107. add his own code by customization of function pointer HAL_I2C_ListenCpltCallback()
  108. (++) Sequential transmit in slave I2C mode an amount of data in non-blocking mode using HAL_I2C_Slave_Sequential_Transmit_IT()
  109. (+++) At transmission end of current frame transfer, HAL_I2C_SlaveTxCpltCallback() is executed and user can
  110. add his own code by customization of function pointer HAL_I2C_SlaveTxCpltCallback()
  111. (++) Sequential receive in slave I2C mode an amount of data in non-blocking mode using HAL_I2C_Slave_Sequential_Receive_IT()
  112. (+++) At reception end of current frame transfer, HAL_I2C_SlaveRxCpltCallback() is executed and user can
  113. add his own code by customization of function pointer HAL_I2C_SlaveRxCpltCallback()
  114. (++) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
  115. add his own code by customization of function pointer HAL_I2C_ErrorCallback()
  116. (++) Abort a master I2C process communication with Interrupt using HAL_I2C_Master_Abort_IT()
  117. (++) End of abort process, HAL_I2C_AbortCpltCallback() is executed and user can
  118. add his own code by customization of function pointer HAL_I2C_AbortCpltCallback()
  119. (++) Discard a slave I2C process communication using __HAL_I2C_GENERATE_NACK() macro.
  120. This action will inform Master to generate a Stop condition to discard the communication.
  121. *** Interrupt mode IO MEM operation ***
  122. =======================================
  123. [..]
  124. (+) Write an amount of data in non-blocking mode with Interrupt to a specific memory address using
  125. HAL_I2C_Mem_Write_IT()
  126. (+) At Memory end of write transfer, HAL_I2C_MemTxCpltCallback() is executed and user can
  127. add his own code by customization of function pointer HAL_I2C_MemTxCpltCallback()
  128. (+) Read an amount of data in non-blocking mode with Interrupt from a specific memory address using
  129. HAL_I2C_Mem_Read_IT()
  130. (+) At Memory end of read transfer, HAL_I2C_MemRxCpltCallback() is executed and user can
  131. add his own code by customization of function pointer HAL_I2C_MemRxCpltCallback()
  132. (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
  133. add his own code by customization of function pointer HAL_I2C_ErrorCallback()
  134. *** DMA mode IO operation ***
  135. ==============================
  136. [..]
  137. (+) Transmit in master mode an amount of data in non-blocking mode (DMA) using
  138. HAL_I2C_Master_Transmit_DMA()
  139. (+) At transmission end of transfer, HAL_I2C_MasterTxCpltCallback() is executed and user can
  140. add his own code by customization of function pointer HAL_I2C_MasterTxCpltCallback()
  141. (+) Receive in master mode an amount of data in non-blocking mode (DMA) using
  142. HAL_I2C_Master_Receive_DMA()
  143. (+) At reception end of transfer, HAL_I2C_MasterRxCpltCallback() is executed and user can
  144. add his own code by customization of function pointer HAL_I2C_MasterRxCpltCallback()
  145. (+) Transmit in slave mode an amount of data in non-blocking mode (DMA) using
  146. HAL_I2C_Slave_Transmit_DMA()
  147. (+) At transmission end of transfer, HAL_I2C_SlaveTxCpltCallback() is executed and user can
  148. add his own code by customization of function pointer HAL_I2C_SlaveTxCpltCallback()
  149. (+) Receive in slave mode an amount of data in non-blocking mode (DMA) using
  150. HAL_I2C_Slave_Receive_DMA()
  151. (+) At reception end of transfer, HAL_I2C_SlaveRxCpltCallback() is executed and user can
  152. add his own code by customization of function pointer HAL_I2C_SlaveRxCpltCallback()
  153. (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
  154. add his own code by customization of function pointer HAL_I2C_ErrorCallback()
  155. (+) Abort a master I2C process communication with Interrupt using HAL_I2C_Master_Abort_IT()
  156. (+) End of abort process, HAL_I2C_AbortCpltCallback() is executed and user can
  157. add his own code by customization of function pointer HAL_I2C_AbortCpltCallback()
  158. (+) Discard a slave I2C process communication using __HAL_I2C_GENERATE_NACK() macro.
  159. This action will inform Master to generate a Stop condition to discard the communication.
  160. *** DMA mode IO MEM operation ***
  161. =================================
  162. [..]
  163. (+) Write an amount of data in non-blocking mode with DMA to a specific memory address using
  164. HAL_I2C_Mem_Write_DMA()
  165. (+) At Memory end of write transfer, HAL_I2C_MemTxCpltCallback() is executed and user can
  166. add his own code by customization of function pointer HAL_I2C_MemTxCpltCallback()
  167. (+) Read an amount of data in non-blocking mode with DMA from a specific memory address using
  168. HAL_I2C_Mem_Read_DMA()
  169. (+) At Memory end of read transfer, HAL_I2C_MemRxCpltCallback() is executed and user can
  170. add his own code by customization of function pointer HAL_I2C_MemRxCpltCallback()
  171. (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
  172. add his own code by customization of function pointer HAL_I2C_ErrorCallback()
  173. *** I2C HAL driver macros list ***
  174. ==================================
  175. [..]
  176. Below the list of most used macros in I2C HAL driver.
  177. (+) __HAL_I2C_ENABLE: Enable the I2C peripheral
  178. (+) __HAL_I2C_DISABLE: Disable the I2C peripheral
  179. (+) __HAL_I2C_GENERATE_NACK: Generate a Non-Acknowledge I2C peripheral in Slave mode
  180. (+) __HAL_I2C_GET_FLAG: Check whether the specified I2C flag is set or not
  181. (+) __HAL_I2C_CLEAR_FLAG: Clear the specified I2C pending flag
  182. (+) __HAL_I2C_ENABLE_IT: Enable the specified I2C interrupt
  183. (+) __HAL_I2C_DISABLE_IT: Disable the specified I2C interrupt
  184. [..]
  185. (@) You can refer to the I2C HAL driver header file for more useful macros
  186. @endverbatim
  187. ******************************************************************************
  188. * @attention
  189. *
  190. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  191. *
  192. * Redistribution and use in source and binary forms, with or without modification,
  193. * are permitted provided that the following conditions are met:
  194. * 1. Redistributions of source code must retain the above copyright notice,
  195. * this list of conditions and the following disclaimer.
  196. * 2. Redistributions in binary form must reproduce the above copyright notice,
  197. * this list of conditions and the following disclaimer in the documentation
  198. * and/or other materials provided with the distribution.
  199. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  200. * may be used to endorse or promote products derived from this software
  201. * without specific prior written permission.
  202. *
  203. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  204. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  205. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  206. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  207. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  208. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  209. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  210. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  211. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  212. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  213. *
  214. ******************************************************************************
  215. */
  216. /* Includes ------------------------------------------------------------------*/
  217. #include "stm32l1xx_hal.h"
  218. #include "core/debugpins.h"
  219. /** @addtogroup STM32L1xx_HAL_Driver
  220. * @{
  221. */
  222. /** @defgroup I2C I2C
  223. * @brief I2C HAL module driver
  224. * @{
  225. */
  226. #ifdef HAL_I2C_MODULE_ENABLED
  227. /* Private typedef -----------------------------------------------------------*/
  228. /* Private define ------------------------------------------------------------*/
  229. /** @defgroup I2C_Private_Define I2C Private Define
  230. * @{
  231. */
  232. #define I2C_TIMEOUT_FLAG (35U) /*!< Timeout 35 ms */
  233. #define I2C_TIMEOUT_ADDR_SLAVE (10000U) /*!< Timeout 10 s */
  234. #define I2C_TIMEOUT_BUSY_FLAG (25U) /*!< Timeout 25 ms */
  235. #define I2C_NO_OPTION_FRAME (0xFFFF0000U) /*!< XferOptions default value */
  236. #define I2C_MIN_PCLK_FREQ (2000000U) /*!< 2 MHz */
  237. /* Private define for @ref PreviousState usage */
  238. #define I2C_STATE_MSK ((uint32_t)((HAL_I2C_STATE_BUSY_TX | HAL_I2C_STATE_BUSY_RX) & (~(uint32_t)HAL_I2C_STATE_READY))) /*!< Mask State define, keep only RX and TX bits */
  239. #define I2C_STATE_NONE ((uint32_t)(HAL_I2C_MODE_NONE)) /*!< Default Value */
  240. #define I2C_STATE_MASTER_BUSY_TX ((uint32_t)((HAL_I2C_STATE_BUSY_TX & I2C_STATE_MSK) | HAL_I2C_MODE_MASTER)) /*!< Master Busy TX, combinaison of State LSB and Mode enum */
  241. #define I2C_STATE_MASTER_BUSY_RX ((uint32_t)((HAL_I2C_STATE_BUSY_RX & I2C_STATE_MSK) | HAL_I2C_MODE_MASTER)) /*!< Master Busy RX, combinaison of State LSB and Mode enum */
  242. #define I2C_STATE_SLAVE_BUSY_TX ((uint32_t)((HAL_I2C_STATE_BUSY_TX & I2C_STATE_MSK) | HAL_I2C_MODE_SLAVE)) /*!< Slave Busy TX, combinaison of State LSB and Mode enum */
  243. #define I2C_STATE_SLAVE_BUSY_RX ((uint32_t)((HAL_I2C_STATE_BUSY_RX & I2C_STATE_MSK) | HAL_I2C_MODE_SLAVE)) /*!< Slave Busy RX, combinaison of State LSB and Mode enum */
  244. /**
  245. * @}
  246. */
  247. /* Private macro -------------------------------------------------------------*/
  248. /* Private variables ---------------------------------------------------------*/
  249. /* Private function prototypes -----------------------------------------------*/
  250. /** @defgroup I2C_Private_Functions I2C Private Functions
  251. * @{
  252. */
  253. /* Private functions to handle DMA transfer */
  254. static void I2C_DMAXferCplt(DMA_HandleTypeDef *hdma);
  255. static void I2C_DMAError(DMA_HandleTypeDef *hdma);
  256. static void I2C_DMAAbort(DMA_HandleTypeDef *hdma);
  257. static void I2C_ITError(I2C_HandleTypeDef *hi2c);
  258. static HAL_StatusTypeDef I2C_MasterRequestWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Timeout, uint32_t Tickstart);
  259. static HAL_StatusTypeDef I2C_MasterRequestRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Timeout, uint32_t Tickstart);
  260. static HAL_StatusTypeDef I2C_RequestMemoryWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart);
  261. static HAL_StatusTypeDef I2C_RequestMemoryRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart);
  262. static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status, uint32_t Timeout, uint32_t Tickstart);
  263. static HAL_StatusTypeDef I2C_WaitOnMasterAddressFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, uint32_t Timeout, uint32_t Tickstart);
  264. static HAL_StatusTypeDef I2C_WaitOnTXEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart);
  265. static HAL_StatusTypeDef I2C_WaitOnBTFFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart);
  266. static HAL_StatusTypeDef I2C_WaitOnRXNEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart);
  267. static HAL_StatusTypeDef I2C_WaitOnSTOPFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart);
  268. static HAL_StatusTypeDef I2C_IsAcknowledgeFailed(I2C_HandleTypeDef *hi2c);
  269. /* Private functions for I2C transfer IRQ handler */
  270. static HAL_StatusTypeDef I2C_MasterTransmit_TXE(I2C_HandleTypeDef *hi2c);
  271. static HAL_StatusTypeDef I2C_MasterTransmit_BTF(I2C_HandleTypeDef *hi2c);
  272. static HAL_StatusTypeDef I2C_MasterReceive_RXNE(I2C_HandleTypeDef *hi2c);
  273. static HAL_StatusTypeDef I2C_MasterReceive_BTF(I2C_HandleTypeDef *hi2c);
  274. static HAL_StatusTypeDef I2C_Master_SB(I2C_HandleTypeDef *hi2c);
  275. static HAL_StatusTypeDef I2C_Master_ADD10(I2C_HandleTypeDef *hi2c);
  276. static HAL_StatusTypeDef I2C_Master_ADDR(I2C_HandleTypeDef *hi2c);
  277. static HAL_StatusTypeDef I2C_SlaveTransmit_TXE(I2C_HandleTypeDef *hi2c);
  278. static HAL_StatusTypeDef I2C_SlaveTransmit_BTF(I2C_HandleTypeDef *hi2c);
  279. static HAL_StatusTypeDef I2C_SlaveReceive_RXNE(I2C_HandleTypeDef *hi2c);
  280. static HAL_StatusTypeDef I2C_SlaveReceive_BTF(I2C_HandleTypeDef *hi2c);
  281. static HAL_StatusTypeDef I2C_Slave_ADDR(I2C_HandleTypeDef *hi2c);
  282. static HAL_StatusTypeDef I2C_Slave_STOPF(I2C_HandleTypeDef *hi2c);
  283. static HAL_StatusTypeDef I2C_Slave_AF(I2C_HandleTypeDef *hi2c);
  284. /**
  285. * @}
  286. */
  287. /* Exported functions --------------------------------------------------------*/
  288. /** @defgroup I2C_Exported_Functions I2C Exported Functions
  289. * @{
  290. */
  291. /** @defgroup I2C_Exported_Functions_Group1 Initialization and de-initialization functions
  292. * @brief Initialization and Configuration functions
  293. *
  294. @verbatim
  295. ===============================================================================
  296. ##### Initialization and de-initialization functions #####
  297. ===============================================================================
  298. [..] This subsection provides a set of functions allowing to initialize and
  299. deinitialize the I2Cx peripheral:
  300. (+) User must Implement HAL_I2C_MspInit() function in which he configures
  301. all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC ).
  302. (+) Call the function HAL_I2C_Init() to configure the selected device with
  303. the selected configuration:
  304. (++) Communication Speed
  305. (++) Duty cycle
  306. (++) Addressing mode
  307. (++) Own Address 1
  308. (++) Dual Addressing mode
  309. (++) Own Address 2
  310. (++) General call mode
  311. (++) Nostretch mode
  312. (+) Call the function HAL_I2C_DeInit() to restore the default configuration
  313. of the selected I2Cx peripheral.
  314. @endverbatim
  315. * @{
  316. */
  317. /**
  318. * @brief Initializes the I2C according to the specified parameters
  319. * in the I2C_InitTypeDef and initialize the associated handle.
  320. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  321. * the configuration information for the specified I2C.
  322. * @retval HAL status
  323. */
  324. HAL_StatusTypeDef HAL_I2C_Init(I2C_HandleTypeDef *hi2c)
  325. {
  326. uint32_t freqrange = 0U;
  327. uint32_t pclk1 = 0U;
  328. /* Check the I2C handle allocation */
  329. if(hi2c == NULL)
  330. {
  331. return HAL_ERROR;
  332. }
  333. /* Check the parameters */
  334. assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
  335. assert_param(IS_I2C_CLOCK_SPEED(hi2c->Init.ClockSpeed));
  336. assert_param(IS_I2C_DUTY_CYCLE(hi2c->Init.DutyCycle));
  337. assert_param(IS_I2C_OWN_ADDRESS1(hi2c->Init.OwnAddress1));
  338. assert_param(IS_I2C_ADDRESSING_MODE(hi2c->Init.AddressingMode));
  339. assert_param(IS_I2C_DUAL_ADDRESS(hi2c->Init.DualAddressMode));
  340. assert_param(IS_I2C_OWN_ADDRESS2(hi2c->Init.OwnAddress2));
  341. assert_param(IS_I2C_GENERAL_CALL(hi2c->Init.GeneralCallMode));
  342. assert_param(IS_I2C_NO_STRETCH(hi2c->Init.NoStretchMode));
  343. if(hi2c->State == HAL_I2C_STATE_RESET)
  344. {
  345. /* Allocate lock resource and initialize it */
  346. hi2c->Lock = HAL_UNLOCKED;
  347. /* Init the low level hardware : GPIO, CLOCK, NVIC */
  348. HAL_I2C_MspInit(hi2c);
  349. }
  350. /* Get PCLK1 frequency */
  351. pclk1 = HAL_RCC_GetPCLK1Freq();
  352. /* The minimum allowed frequency is 2 MHz */
  353. if(pclk1 < I2C_MIN_PCLK_FREQ)
  354. {
  355. return HAL_ERROR;
  356. }
  357. hi2c->State = HAL_I2C_STATE_BUSY;
  358. /* Disable the selected I2C peripheral */
  359. __HAL_I2C_DISABLE(hi2c);
  360. /* Calculate frequency range */
  361. freqrange = I2C_FREQ_RANGE(pclk1);
  362. /*---------------------------- I2Cx CR2 Configuration ----------------------*/
  363. /* Configure I2Cx: Frequency range */
  364. MODIFY_REG(hi2c->Instance->CR2, I2C_CR2_FREQ, freqrange);
  365. /*---------------------------- I2Cx TRISE Configuration --------------------*/
  366. /* Configure I2Cx: Rise Time */
  367. MODIFY_REG(hi2c->Instance->TRISE, I2C_TRISE_TRISE, I2C_RISE_TIME(freqrange, hi2c->Init.ClockSpeed));
  368. /*---------------------------- I2Cx CCR Configuration ----------------------*/
  369. /* Configure I2Cx: Speed */
  370. MODIFY_REG(hi2c->Instance->CCR, (I2C_CCR_FS | I2C_CCR_DUTY | I2C_CCR_CCR), I2C_SPEED(pclk1, hi2c->Init.ClockSpeed, hi2c->Init.DutyCycle));
  371. /*---------------------------- I2Cx CR1 Configuration ----------------------*/
  372. /* Configure I2Cx: Generalcall and NoStretch mode */
  373. MODIFY_REG(hi2c->Instance->CR1, (I2C_CR1_ENGC | I2C_CR1_NOSTRETCH), (hi2c->Init.GeneralCallMode | hi2c->Init.NoStretchMode));
  374. /*---------------------------- I2Cx OAR1 Configuration ---------------------*/
  375. /* Configure I2Cx: Own Address1 and addressing mode */
  376. MODIFY_REG(hi2c->Instance->OAR1, (I2C_OAR1_ADDMODE | I2C_OAR1_ADD8_9 | I2C_OAR1_ADD1_7 | I2C_OAR1_ADD0), (hi2c->Init.AddressingMode | hi2c->Init.OwnAddress1));
  377. /*---------------------------- I2Cx OAR2 Configuration ---------------------*/
  378. /* Configure I2Cx: Dual mode and Own Address2 */
  379. MODIFY_REG(hi2c->Instance->OAR2, (I2C_OAR2_ENDUAL | I2C_OAR2_ADD2), (hi2c->Init.DualAddressMode | hi2c->Init.OwnAddress2));
  380. /* Enable the selected I2C peripheral */
  381. __HAL_I2C_ENABLE(hi2c);
  382. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  383. hi2c->State = HAL_I2C_STATE_READY;
  384. hi2c->PreviousState = I2C_STATE_NONE;
  385. hi2c->Mode = HAL_I2C_MODE_NONE;
  386. return HAL_OK;
  387. }
  388. /**
  389. * @brief DeInitialize the I2C peripheral.
  390. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  391. * the configuration information for the specified I2C.
  392. * @retval HAL status
  393. */
  394. HAL_StatusTypeDef HAL_I2C_DeInit(I2C_HandleTypeDef *hi2c)
  395. {
  396. /* Check the I2C handle allocation */
  397. if(hi2c == NULL)
  398. {
  399. return HAL_ERROR;
  400. }
  401. /* Check the parameters */
  402. assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
  403. hi2c->State = HAL_I2C_STATE_BUSY;
  404. /* Disable the I2C Peripheral Clock */
  405. __HAL_I2C_DISABLE(hi2c);
  406. /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
  407. HAL_I2C_MspDeInit(hi2c);
  408. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  409. hi2c->State = HAL_I2C_STATE_RESET;
  410. hi2c->PreviousState = I2C_STATE_NONE;
  411. hi2c->Mode = HAL_I2C_MODE_NONE;
  412. /* Release Lock */
  413. __HAL_UNLOCK(hi2c);
  414. return HAL_OK;
  415. }
  416. /**
  417. * @brief Initialize the I2C MSP.
  418. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  419. * the configuration information for the specified I2C.
  420. * @retval None
  421. */
  422. __weak void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c)
  423. {
  424. /* Prevent unused argument(s) compilation warning */
  425. UNUSED(hi2c);
  426. /* NOTE : This function should not be modified, when the callback is needed,
  427. the HAL_I2C_MspInit could be implemented in the user file
  428. */
  429. }
  430. /**
  431. * @brief DeInitialize the I2C MSP.
  432. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  433. * the configuration information for the specified I2C.
  434. * @retval None
  435. */
  436. __weak void HAL_I2C_MspDeInit(I2C_HandleTypeDef *hi2c)
  437. {
  438. /* Prevent unused argument(s) compilation warning */
  439. UNUSED(hi2c);
  440. /* NOTE : This function should not be modified, when the callback is needed,
  441. the HAL_I2C_MspDeInit could be implemented in the user file
  442. */
  443. }
  444. /**
  445. * @}
  446. */
  447. /** @defgroup I2C_Exported_Functions_Group2 Input and Output operation functions
  448. * @brief Data transfers functions
  449. *
  450. @verbatim
  451. ===============================================================================
  452. ##### IO operation functions #####
  453. ===============================================================================
  454. [..]
  455. This subsection provides a set of functions allowing to manage the I2C data
  456. transfers.
  457. (#) There are two modes of transfer:
  458. (++) Blocking mode : The communication is performed in the polling mode.
  459. The status of all data processing is returned by the same function
  460. after finishing transfer.
  461. (++) No-Blocking mode : The communication is performed using Interrupts
  462. or DMA. These functions return the status of the transfer startup.
  463. The end of the data processing will be indicated through the
  464. dedicated I2C IRQ when using Interrupt mode or the DMA IRQ when
  465. using DMA mode.
  466. (#) Blocking mode functions are :
  467. (++) HAL_I2C_Master_Transmit()
  468. (++) HAL_I2C_Master_Receive()
  469. (++) HAL_I2C_Slave_Transmit()
  470. (++) HAL_I2C_Slave_Receive()
  471. (++) HAL_I2C_Mem_Write()
  472. (++) HAL_I2C_Mem_Read()
  473. (++) HAL_I2C_IsDeviceReady()
  474. (#) No-Blocking mode functions with Interrupt are :
  475. (++) HAL_I2C_Master_Transmit_IT()
  476. (++) HAL_I2C_Master_Receive_IT()
  477. (++) HAL_I2C_Slave_Transmit_IT()
  478. (++) HAL_I2C_Slave_Receive_IT()
  479. (++) HAL_I2C_Master_Sequential_Transmit_IT()
  480. (++) HAL_I2C_Master_Sequential_Receive_IT()
  481. (++) HAL_I2C_Slave_Sequential_Transmit_IT()
  482. (++) HAL_I2C_Slave_Sequential_Receive_IT()
  483. (++) HAL_I2C_Mem_Write_IT()
  484. (++) HAL_I2C_Mem_Read_IT()
  485. (#) No-Blocking mode functions with DMA are :
  486. (++) HAL_I2C_Master_Transmit_DMA()
  487. (++) HAL_I2C_Master_Receive_DMA()
  488. (++) HAL_I2C_Slave_Transmit_DMA()
  489. (++) HAL_I2C_Slave_Receive_DMA()
  490. (++) HAL_I2C_Mem_Write_DMA()
  491. (++) HAL_I2C_Mem_Read_DMA()
  492. (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
  493. (++) HAL_I2C_MemTxCpltCallback()
  494. (++) HAL_I2C_MemRxCpltCallback()
  495. (++) HAL_I2C_MasterTxCpltCallback()
  496. (++) HAL_I2C_MasterRxCpltCallback()
  497. (++) HAL_I2C_SlaveTxCpltCallback()
  498. (++) HAL_I2C_SlaveRxCpltCallback()
  499. (++) HAL_I2C_ErrorCallback()
  500. (++) HAL_I2C_AbortCpltCallback()
  501. @endverbatim
  502. * @{
  503. */
  504. /**
  505. * @brief Transmits in master mode an amount of data in blocking mode.
  506. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  507. * the configuration information for the specified I2C.
  508. * @param DevAddress Target device address: The device 7 bits address value
  509. * in datasheet must be shift at right before call interface
  510. * @param pData Pointer to data buffer
  511. * @param Size Amount of data to be sent
  512. * @param Timeout Timeout duration
  513. * @retval HAL status
  514. */
  515. HAL_StatusTypeDef HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  516. {
  517. uint32_t tickstart = 0x00U;
  518. /* Init tickstart for timeout management*/
  519. tickstart = HAL_GetTick();
  520. if(hi2c->State == HAL_I2C_STATE_READY)
  521. {
  522. /* Wait until BUSY flag is reset */
  523. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK)
  524. {
  525. return HAL_BUSY;
  526. }
  527. /* Process Locked */
  528. __HAL_LOCK(hi2c);
  529. /* Check if the I2C is already enabled */
  530. if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  531. {
  532. /* Enable I2C peripheral */
  533. __HAL_I2C_ENABLE(hi2c);
  534. }
  535. /* Disable Pos */
  536. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  537. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  538. hi2c->Mode = HAL_I2C_MODE_MASTER;
  539. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  540. /* Prepare transfer parameters */
  541. hi2c->pBuffPtr = pData;
  542. hi2c->XferCount = Size;
  543. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  544. hi2c->XferSize = hi2c->XferCount;
  545. /* Send Slave Address */
  546. if(I2C_MasterRequestWrite(hi2c, DevAddress, Timeout, tickstart) != HAL_OK)
  547. {
  548. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  549. {
  550. /* Process Unlocked */
  551. __HAL_UNLOCK(hi2c);
  552. return HAL_ERROR;
  553. }
  554. else
  555. {
  556. /* Process Unlocked */
  557. __HAL_UNLOCK(hi2c);
  558. return HAL_TIMEOUT;
  559. }
  560. }
  561. /* Clear ADDR flag */
  562. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  563. while(hi2c->XferSize > 0U)
  564. {
  565. /* Wait until TXE flag is set */
  566. if(I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
  567. {
  568. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  569. {
  570. /* Generate Stop */
  571. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  572. return HAL_ERROR;
  573. }
  574. else
  575. {
  576. return HAL_TIMEOUT;
  577. }
  578. }
  579. /* Write data to DR */
  580. hi2c->Instance->DR = (*hi2c->pBuffPtr++);
  581. hi2c->XferCount--;
  582. hi2c->XferSize--;
  583. //if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (Size != 0U))
  584. //Bug Fixed, Alexander Sychov, 23/05/19
  585. if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (hi2c->XferSize != 0U))
  586. { // !!!!!!!!!!!!!!!!!!!!!
  587. /* Write data to DR */
  588. hi2c->Instance->DR = (*hi2c->pBuffPtr++);
  589. hi2c->XferCount--;
  590. hi2c->XferSize--;
  591. }
  592. /* Wait until BTF flag is set */
  593. if(I2C_WaitOnBTFFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
  594. {
  595. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  596. {
  597. /* Generate Stop */
  598. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  599. return HAL_ERROR;
  600. }
  601. else
  602. {
  603. return HAL_TIMEOUT;
  604. }
  605. }
  606. }
  607. /* Generate Stop */
  608. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  609. hi2c->State = HAL_I2C_STATE_READY;
  610. hi2c->Mode = HAL_I2C_MODE_NONE;
  611. /* Process Unlocked */
  612. __HAL_UNLOCK(hi2c);
  613. return HAL_OK;
  614. }
  615. else
  616. {
  617. return HAL_BUSY;
  618. }
  619. }
  620. /**
  621. * @brief Receives in master mode an amount of data in blocking mode.
  622. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  623. * the configuration information for the specified I2C.
  624. * @param DevAddress Target device address: The device 7 bits address value
  625. * in datasheet must be shift at right before call interface
  626. * @param pData Pointer to data buffer
  627. * @param Size Amount of data to be sent
  628. * @param Timeout Timeout duration
  629. * @retval HAL status
  630. */
  631. HAL_StatusTypeDef HAL_I2C_Master_Receive(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  632. {
  633. uint32_t tickstart = 0x00U;
  634. /* Init tickstart for timeout management*/
  635. tickstart = HAL_GetTick();
  636. if(hi2c->State == HAL_I2C_STATE_READY)
  637. {
  638. /* Wait until BUSY flag is reset */
  639. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK)
  640. {
  641. return HAL_BUSY;
  642. }
  643. /* Process Locked */
  644. __HAL_LOCK(hi2c);
  645. /* Check if the I2C is already enabled */
  646. if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  647. {
  648. /* Enable I2C peripheral */
  649. __HAL_I2C_ENABLE(hi2c);
  650. }
  651. /* Disable Pos */
  652. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  653. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  654. hi2c->Mode = HAL_I2C_MODE_MASTER;
  655. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  656. /* Prepare transfer parameters */
  657. hi2c->pBuffPtr = pData;
  658. hi2c->XferCount = Size;
  659. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  660. hi2c->XferSize = hi2c->XferCount;
  661. /* Send Slave Address */
  662. if(I2C_MasterRequestRead(hi2c, DevAddress, Timeout, tickstart) != HAL_OK)
  663. {
  664. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  665. {
  666. /* Process Unlocked */
  667. __HAL_UNLOCK(hi2c);
  668. return HAL_ERROR;
  669. }
  670. else
  671. {
  672. /* Process Unlocked */
  673. __HAL_UNLOCK(hi2c);
  674. return HAL_TIMEOUT;
  675. }
  676. }
  677. if(hi2c->XferSize == 0U)
  678. {
  679. /* Clear ADDR flag */
  680. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  681. /* Generate Stop */
  682. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  683. }
  684. else if(hi2c->XferSize == 1U)
  685. {
  686. /* Disable Acknowledge */
  687. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  688. /* Clear ADDR flag */
  689. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  690. /* Generate Stop */
  691. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  692. }
  693. else if(hi2c->XferSize == 2U)
  694. {
  695. /* Disable Acknowledge */
  696. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  697. /* Enable Pos */
  698. SET_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  699. /* Clear ADDR flag */
  700. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  701. }
  702. else
  703. {
  704. /* Enable Acknowledge */
  705. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  706. /* Clear ADDR flag */
  707. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  708. }
  709. while(hi2c->XferSize > 0U)
  710. {
  711. if(hi2c->XferSize <= 3U)
  712. {
  713. /* One byte */
  714. if(hi2c->XferSize == 1U)
  715. {
  716. /* Wait until RXNE flag is set */
  717. if(I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
  718. {
  719. if(hi2c->ErrorCode == HAL_I2C_ERROR_TIMEOUT)
  720. {
  721. return HAL_TIMEOUT;
  722. }
  723. else
  724. {
  725. return HAL_ERROR;
  726. }
  727. }
  728. /* Read data from DR */
  729. (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
  730. hi2c->XferSize--;
  731. hi2c->XferCount--;
  732. }
  733. /* Two bytes */
  734. else if(hi2c->XferSize == 2U)
  735. {
  736. /* Wait until BTF flag is set */
  737. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout, tickstart) != HAL_OK)
  738. {
  739. return HAL_TIMEOUT;
  740. }
  741. /* Generate Stop */
  742. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  743. /* Read data from DR */
  744. (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
  745. hi2c->XferSize--;
  746. hi2c->XferCount--;
  747. /* Read data from DR */
  748. (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
  749. hi2c->XferSize--;
  750. hi2c->XferCount--;
  751. }
  752. /* 3 Last bytes */
  753. else
  754. {
  755. /* Wait until BTF flag is set */
  756. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout, tickstart) != HAL_OK)
  757. {
  758. return HAL_TIMEOUT;
  759. }
  760. /* Disable Acknowledge */
  761. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  762. /* Read data from DR */
  763. (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
  764. hi2c->XferSize--;
  765. hi2c->XferCount--;
  766. /* Wait until BTF flag is set */
  767. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout, tickstart) != HAL_OK)
  768. {
  769. return HAL_TIMEOUT;
  770. }
  771. /* Generate Stop */
  772. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  773. /* Read data from DR */
  774. (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
  775. hi2c->XferSize--;
  776. hi2c->XferCount--;
  777. /* Read data from DR */
  778. (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
  779. hi2c->XferSize--;
  780. hi2c->XferCount--;
  781. }
  782. }
  783. else
  784. {
  785. /* Wait until RXNE flag is set */
  786. if(I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
  787. {
  788. if(hi2c->ErrorCode == HAL_I2C_ERROR_TIMEOUT)
  789. {
  790. return HAL_TIMEOUT;
  791. }
  792. else
  793. {
  794. return HAL_ERROR;
  795. }
  796. }
  797. /* Read data from DR */
  798. (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
  799. hi2c->XferSize--;
  800. hi2c->XferCount--;
  801. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET)
  802. {
  803. /* Read data from DR */
  804. (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
  805. hi2c->XferSize--;
  806. hi2c->XferCount--;
  807. }
  808. }
  809. }
  810. hi2c->State = HAL_I2C_STATE_READY;
  811. hi2c->Mode = HAL_I2C_MODE_NONE;
  812. /* Process Unlocked */
  813. __HAL_UNLOCK(hi2c);
  814. return HAL_OK;
  815. }
  816. else
  817. {
  818. return HAL_BUSY;
  819. }
  820. }
  821. /**
  822. * @brief Transmits in slave mode an amount of data in blocking mode.
  823. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  824. * the configuration information for the specified I2C.
  825. * @param pData Pointer to data buffer
  826. * @param Size Amount of data to be sent
  827. * @param Timeout Timeout duration
  828. * @retval HAL status
  829. */
  830. HAL_StatusTypeDef HAL_I2C_Slave_Transmit(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  831. {
  832. uint32_t tickstart = 0x00U;
  833. /* Init tickstart for timeout management*/
  834. tickstart = HAL_GetTick();
  835. if(hi2c->State == HAL_I2C_STATE_READY)
  836. {
  837. if((pData == NULL) || (Size == 0U))
  838. {
  839. return HAL_ERROR;
  840. }
  841. /* Process Locked */
  842. __HAL_LOCK(hi2c);
  843. /* Check if the I2C is already enabled */
  844. if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  845. {
  846. /* Enable I2C peripheral */
  847. __HAL_I2C_ENABLE(hi2c);
  848. }
  849. /* Disable Pos */
  850. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  851. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  852. hi2c->Mode = HAL_I2C_MODE_SLAVE;
  853. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  854. /* Prepare transfer parameters */
  855. hi2c->pBuffPtr = pData;
  856. hi2c->XferCount = Size;
  857. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  858. hi2c->XferSize = hi2c->XferCount;
  859. /* Enable Address Acknowledge */
  860. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  861. /* Wait until ADDR flag is set */
  862. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout, tickstart) != HAL_OK)
  863. {
  864. return HAL_TIMEOUT;
  865. }
  866. /* Clear ADDR flag */
  867. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  868. /* If 10bit addressing mode is selected */
  869. if(hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT)
  870. {
  871. /* Wait until ADDR flag is set */
  872. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout, tickstart) != HAL_OK)
  873. {
  874. return HAL_TIMEOUT;
  875. }
  876. /* Clear ADDR flag */
  877. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  878. }
  879. while(hi2c->XferSize > 0U)
  880. {
  881. /* Wait until TXE flag is set */
  882. if(I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
  883. {
  884. /* Disable Address Acknowledge */
  885. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  886. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  887. {
  888. return HAL_ERROR;
  889. }
  890. else
  891. {
  892. return HAL_TIMEOUT;
  893. }
  894. }
  895. /* Write data to DR */
  896. hi2c->Instance->DR = (*hi2c->pBuffPtr++);
  897. hi2c->XferCount--;
  898. hi2c->XferSize--;
  899. //if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (Size != 0U))
  900. //Bug Fixed, Alexander Sychov, 23/05/19
  901. if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (hi2c->XferSize != 0U))
  902. {
  903. /* Write data to DR */
  904. hi2c->Instance->DR = (*hi2c->pBuffPtr++);
  905. hi2c->XferCount--;
  906. hi2c->XferSize--;
  907. }
  908. }
  909. /* Wait until AF flag is set */
  910. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_AF, RESET, Timeout, tickstart) != HAL_OK)
  911. {
  912. return HAL_TIMEOUT;
  913. }
  914. /* Clear AF flag */
  915. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  916. /* Disable Address Acknowledge */
  917. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  918. hi2c->State = HAL_I2C_STATE_READY;
  919. hi2c->Mode = HAL_I2C_MODE_NONE;
  920. /* Process Unlocked */
  921. __HAL_UNLOCK(hi2c);
  922. return HAL_OK;
  923. }
  924. else
  925. {
  926. return HAL_BUSY;
  927. }
  928. }
  929. /**
  930. * @brief Receive in slave mode an amount of data in blocking mode
  931. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  932. * the configuration information for the specified I2C.
  933. * @param pData Pointer to data buffer
  934. * @param Size Amount of data to be sent
  935. * @param Timeout Timeout duration
  936. * @retval HAL status
  937. */
  938. HAL_StatusTypeDef HAL_I2C_Slave_Receive(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  939. {
  940. uint32_t tickstart = 0x00U;
  941. /* Init tickstart for timeout management*/
  942. tickstart = HAL_GetTick();
  943. if(hi2c->State == HAL_I2C_STATE_READY)
  944. {
  945. if((pData == NULL) || (Size == 0U))
  946. {
  947. return HAL_ERROR;
  948. }
  949. /* Process Locked */
  950. __HAL_LOCK(hi2c);
  951. /* Check if the I2C is already enabled */
  952. if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  953. {
  954. /* Enable I2C peripheral */
  955. __HAL_I2C_ENABLE(hi2c);
  956. }
  957. /* Disable Pos */
  958. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  959. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  960. hi2c->Mode = HAL_I2C_MODE_SLAVE;
  961. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  962. /* Prepare transfer parameters */
  963. hi2c->pBuffPtr = pData;
  964. hi2c->XferCount = Size;
  965. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  966. hi2c->XferSize = hi2c->XferCount;
  967. /* Enable Address Acknowledge */
  968. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  969. /* Wait until ADDR flag is set */
  970. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout, tickstart) != HAL_OK)
  971. {
  972. return HAL_TIMEOUT;
  973. }
  974. /* Clear ADDR flag */
  975. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  976. while(hi2c->XferSize > 0U)
  977. {
  978. /* Wait until RXNE flag is set */
  979. if(I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
  980. {
  981. /* Disable Address Acknowledge */
  982. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  983. if(hi2c->ErrorCode == HAL_I2C_ERROR_TIMEOUT)
  984. {
  985. return HAL_TIMEOUT;
  986. }
  987. else
  988. {
  989. return HAL_ERROR;
  990. }
  991. }
  992. /* Read data from DR */
  993. (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
  994. hi2c->XferSize--;
  995. hi2c->XferCount--;
  996. if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (Size != 0U))
  997. {
  998. /* Read data from DR */
  999. (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
  1000. hi2c->XferSize--;
  1001. hi2c->XferCount--;
  1002. }
  1003. }
  1004. /* Wait until STOP flag is set */
  1005. if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
  1006. {
  1007. /* Disable Address Acknowledge */
  1008. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  1009. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  1010. {
  1011. return HAL_ERROR;
  1012. }
  1013. else
  1014. {
  1015. return HAL_TIMEOUT;
  1016. }
  1017. }
  1018. /* Clear STOP flag */
  1019. __HAL_I2C_CLEAR_STOPFLAG(hi2c);
  1020. /* Disable Address Acknowledge */
  1021. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  1022. hi2c->State = HAL_I2C_STATE_READY;
  1023. hi2c->Mode = HAL_I2C_MODE_NONE;
  1024. /* Process Unlocked */
  1025. __HAL_UNLOCK(hi2c);
  1026. return HAL_OK;
  1027. }
  1028. else
  1029. {
  1030. return HAL_BUSY;
  1031. }
  1032. }
  1033. /**
  1034. * @brief Transmit in master mode an amount of data in non-blocking mode with Interrupt
  1035. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1036. * the configuration information for the specified I2C.
  1037. * @param DevAddress Target device address: The device 7 bits address value
  1038. * in datasheet must be shift at right before call interface
  1039. * @param pData Pointer to data buffer
  1040. * @param Size Amount of data to be sent
  1041. * @retval HAL status
  1042. */
  1043. HAL_StatusTypeDef HAL_I2C_Master_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
  1044. {
  1045. __IO uint32_t count = 0U;
  1046. if(hi2c->State == HAL_I2C_STATE_READY)
  1047. {
  1048. /* Wait until BUSY flag is reset */
  1049. count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock /25U /1000U);
  1050. do
  1051. {
  1052. if(count-- == 0U)
  1053. {
  1054. hi2c->PreviousState = I2C_STATE_NONE;
  1055. hi2c->State= HAL_I2C_STATE_READY;
  1056. /* Process Unlocked */
  1057. __HAL_UNLOCK(hi2c);
  1058. return HAL_TIMEOUT;
  1059. }
  1060. }
  1061. while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
  1062. /* Process Locked */
  1063. __HAL_LOCK(hi2c);
  1064. /* Check if the I2C is already enabled */
  1065. if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  1066. {
  1067. /* Enable I2C peripheral */
  1068. __HAL_I2C_ENABLE(hi2c);
  1069. }
  1070. /* Disable Pos */
  1071. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  1072. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  1073. hi2c->Mode = HAL_I2C_MODE_MASTER;
  1074. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1075. /* Prepare transfer parameters */
  1076. hi2c->pBuffPtr = pData;
  1077. hi2c->XferCount = Size;
  1078. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  1079. hi2c->XferSize = hi2c->XferCount;
  1080. hi2c->Devaddress = DevAddress;
  1081. /* Generate Start */
  1082. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  1083. /* Process Unlocked */
  1084. __HAL_UNLOCK(hi2c);
  1085. /* Note : The I2C interrupts must be enabled after unlocking current process
  1086. to avoid the risk of I2C interrupt handle execution before current
  1087. process unlock */
  1088. /* Enable EVT, BUF and ERR interrupt */
  1089. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  1090. return HAL_OK;
  1091. }
  1092. else
  1093. {
  1094. return HAL_BUSY;
  1095. }
  1096. }
  1097. /**
  1098. * @brief Receive in master mode an amount of data in non-blocking mode with Interrupt
  1099. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1100. * the configuration information for the specified I2C.
  1101. * @param DevAddress Target device address: The device 7 bits address value
  1102. * in datasheet must be shift at right before call interface
  1103. * @param pData Pointer to data buffer
  1104. * @param Size Amount of data to be sent
  1105. * @retval HAL status
  1106. */
  1107. HAL_StatusTypeDef HAL_I2C_Master_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
  1108. {
  1109. __IO uint32_t count = 0U;
  1110. if(hi2c->State == HAL_I2C_STATE_READY)
  1111. {
  1112. /* Wait until BUSY flag is reset */
  1113. count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock /25U /1000U);
  1114. do
  1115. {
  1116. if(count-- == 0U)
  1117. {
  1118. hi2c->PreviousState = I2C_STATE_NONE;
  1119. hi2c->State= HAL_I2C_STATE_READY;
  1120. /* Process Unlocked */
  1121. __HAL_UNLOCK(hi2c);
  1122. return HAL_TIMEOUT;
  1123. }
  1124. }
  1125. while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
  1126. /* Process Locked */
  1127. __HAL_LOCK(hi2c);
  1128. /* Check if the I2C is already enabled */
  1129. if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  1130. {
  1131. /* Enable I2C peripheral */
  1132. __HAL_I2C_ENABLE(hi2c);
  1133. }
  1134. /* Disable Pos */
  1135. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  1136. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  1137. hi2c->Mode = HAL_I2C_MODE_MASTER;
  1138. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1139. /* Prepare transfer parameters */
  1140. hi2c->pBuffPtr = pData;
  1141. hi2c->XferCount = Size;
  1142. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  1143. hi2c->XferSize = hi2c->XferCount;
  1144. hi2c->Devaddress = DevAddress;
  1145. /* Enable Acknowledge */
  1146. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  1147. /* Generate Start */
  1148. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  1149. /* Process Unlocked */
  1150. __HAL_UNLOCK(hi2c);
  1151. /* Note : The I2C interrupts must be enabled after unlocking current process
  1152. to avoid the risk of I2C interrupt handle execution before current
  1153. process unlock */
  1154. /* Enable EVT, BUF and ERR interrupt */
  1155. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  1156. return HAL_OK;
  1157. }
  1158. else
  1159. {
  1160. return HAL_BUSY;
  1161. }
  1162. }
  1163. /**
  1164. * @brief Sequential transmit in master mode an amount of data in non-blocking mode with Interrupt
  1165. * @note This interface allow to manage repeated start condition when a direction change during transfer
  1166. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1167. * the configuration information for the specified I2C.
  1168. * @param DevAddress Target device address: The device 7 bits address value
  1169. * in datasheet must be shift at right before call interface
  1170. * @param pData Pointer to data buffer
  1171. * @param Size Amount of data to be sent
  1172. * @param XferOptions Options of Transfer, value of @ref I2C_XferOptions_definition
  1173. * @retval HAL status
  1174. */
  1175. HAL_StatusTypeDef HAL_I2C_Master_Sequential_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
  1176. {
  1177. __IO uint32_t count = 0U;
  1178. /* Check the parameters */
  1179. assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  1180. if(hi2c->State == HAL_I2C_STATE_READY)
  1181. {
  1182. /* Check Busy Flag only if FIRST call of Master interface */
  1183. if((XferOptions == I2C_FIRST_AND_LAST_FRAME) || (XferOptions == I2C_FIRST_FRAME))
  1184. {
  1185. /* Wait until BUSY flag is reset */
  1186. count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock /25U /1000U);
  1187. do
  1188. {
  1189. if(count-- == 0U)
  1190. {
  1191. hi2c->PreviousState = I2C_STATE_NONE;
  1192. hi2c->State= HAL_I2C_STATE_READY;
  1193. /* Process Unlocked */
  1194. __HAL_UNLOCK(hi2c);
  1195. return HAL_TIMEOUT;
  1196. }
  1197. }
  1198. while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
  1199. }
  1200. /* Process Locked */
  1201. __HAL_LOCK(hi2c);
  1202. /* Check if the I2C is already enabled */
  1203. if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  1204. {
  1205. /* Enable I2C peripheral */
  1206. __HAL_I2C_ENABLE(hi2c);
  1207. }
  1208. /* Disable Pos */
  1209. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  1210. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  1211. hi2c->Mode = HAL_I2C_MODE_MASTER;
  1212. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1213. /* Prepare transfer parameters */
  1214. hi2c->pBuffPtr = pData;
  1215. hi2c->XferCount = Size;
  1216. hi2c->XferOptions = XferOptions;
  1217. hi2c->XferSize = hi2c->XferCount;
  1218. hi2c->Devaddress = DevAddress;
  1219. /* Generate Start */
  1220. if((hi2c->PreviousState == I2C_STATE_MASTER_BUSY_RX) || (hi2c->PreviousState == I2C_STATE_NONE))
  1221. {
  1222. /* Generate Start condition if first transfer */
  1223. if((XferOptions == I2C_FIRST_AND_LAST_FRAME) || (XferOptions == I2C_FIRST_FRAME))
  1224. {
  1225. /* Generate Start */
  1226. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  1227. }
  1228. else if(hi2c->PreviousState == I2C_STATE_MASTER_BUSY_RX)
  1229. {
  1230. /* Generate ReStart */
  1231. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  1232. }
  1233. }
  1234. /* Process Unlocked */
  1235. __HAL_UNLOCK(hi2c);
  1236. /* Note : The I2C interrupts must be enabled after unlocking current process
  1237. to avoid the risk of I2C interrupt handle execution before current
  1238. process unlock */
  1239. /* Enable EVT, BUF and ERR interrupt */
  1240. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  1241. return HAL_OK;
  1242. }
  1243. else
  1244. {
  1245. return HAL_BUSY;
  1246. }
  1247. }
  1248. /**
  1249. * @brief Sequential receive in master mode an amount of data in non-blocking mode with Interrupt
  1250. * @note This interface allow to manage repeated start condition when a direction change during transfer
  1251. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1252. * the configuration information for the specified I2C.
  1253. * @param DevAddress Target device address: The device 7 bits address value
  1254. * in datasheet must be shift at right before call interface
  1255. * @param pData Pointer to data buffer
  1256. * @param Size Amount of data to be sent
  1257. * @param XferOptions Options of Transfer, value of @ref I2C_XferOptions_definition
  1258. * @retval HAL status
  1259. */
  1260. HAL_StatusTypeDef HAL_I2C_Master_Sequential_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
  1261. {
  1262. __IO uint32_t count = 0U;
  1263. /* Check the parameters */
  1264. assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  1265. if(hi2c->State == HAL_I2C_STATE_READY)
  1266. {
  1267. /* Check Busy Flag only if FIRST call of Master interface */
  1268. if((XferOptions == I2C_FIRST_AND_LAST_FRAME) || (XferOptions == I2C_FIRST_FRAME))
  1269. {
  1270. /* Wait until BUSY flag is reset */
  1271. count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock /25U /1000U);
  1272. do
  1273. {
  1274. if(count-- == 0U)
  1275. {
  1276. hi2c->PreviousState = I2C_STATE_NONE;
  1277. hi2c->State= HAL_I2C_STATE_READY;
  1278. /* Process Unlocked */
  1279. __HAL_UNLOCK(hi2c);
  1280. return HAL_TIMEOUT;
  1281. }
  1282. }
  1283. while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
  1284. }
  1285. /* Process Locked */
  1286. __HAL_LOCK(hi2c);
  1287. /* Check if the I2C is already enabled */
  1288. if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  1289. {
  1290. /* Enable I2C peripheral */
  1291. __HAL_I2C_ENABLE(hi2c);
  1292. }
  1293. /* Disable Pos */
  1294. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  1295. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  1296. hi2c->Mode = HAL_I2C_MODE_MASTER;
  1297. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1298. /* Prepare transfer parameters */
  1299. hi2c->pBuffPtr = pData;
  1300. hi2c->XferCount = Size;
  1301. hi2c->XferOptions = XferOptions;
  1302. hi2c->XferSize = hi2c->XferCount;
  1303. hi2c->Devaddress = DevAddress;
  1304. if((hi2c->PreviousState == I2C_STATE_MASTER_BUSY_TX) || (hi2c->PreviousState == I2C_STATE_NONE))
  1305. {
  1306. /* Generate Start condition if first transfer */
  1307. if((XferOptions == I2C_FIRST_AND_LAST_FRAME) || (XferOptions == I2C_FIRST_FRAME) || (XferOptions == I2C_NO_OPTION_FRAME))
  1308. {
  1309. /* Enable Acknowledge */
  1310. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  1311. /* Generate Start */
  1312. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  1313. }
  1314. else if(hi2c->PreviousState == I2C_STATE_MASTER_BUSY_TX)
  1315. {
  1316. /* Enable Acknowledge */
  1317. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  1318. /* Generate ReStart */
  1319. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  1320. }
  1321. }
  1322. /* Process Unlocked */
  1323. __HAL_UNLOCK(hi2c);
  1324. /* Note : The I2C interrupts must be enabled after unlocking current process
  1325. to avoid the risk of I2C interrupt handle execution before current
  1326. process unlock */
  1327. /* Enable EVT, BUF and ERR interrupt */
  1328. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  1329. return HAL_OK;
  1330. }
  1331. else
  1332. {
  1333. return HAL_BUSY;
  1334. }
  1335. }
  1336. /**
  1337. * @brief Transmit in slave mode an amount of data in non-blocking mode with Interrupt
  1338. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1339. * the configuration information for the specified I2C.
  1340. * @param pData Pointer to data buffer
  1341. * @param Size Amount of data to be sent
  1342. * @retval HAL status
  1343. */
  1344. HAL_StatusTypeDef HAL_I2C_Slave_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
  1345. {
  1346. if(hi2c->State == HAL_I2C_STATE_READY)
  1347. {
  1348. if((pData == NULL) || (Size == 0U))
  1349. {
  1350. return HAL_ERROR;
  1351. }
  1352. /* Process Locked */
  1353. __HAL_LOCK(hi2c);
  1354. /* Check if the I2C is already enabled */
  1355. if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  1356. {
  1357. /* Enable I2C peripheral */
  1358. __HAL_I2C_ENABLE(hi2c);
  1359. }
  1360. /* Disable Pos */
  1361. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  1362. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  1363. hi2c->Mode = HAL_I2C_MODE_SLAVE;
  1364. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1365. /* Prepare transfer parameters */
  1366. hi2c->pBuffPtr = pData;
  1367. hi2c->XferCount = Size;
  1368. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  1369. hi2c->XferSize = hi2c->XferCount;
  1370. /* Enable Address Acknowledge */
  1371. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  1372. /* Process Unlocked */
  1373. __HAL_UNLOCK(hi2c);
  1374. /* Note : The I2C interrupts must be enabled after unlocking current process
  1375. to avoid the risk of I2C interrupt handle execution before current
  1376. process unlock */
  1377. /* Enable EVT, BUF and ERR interrupt */
  1378. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  1379. return HAL_OK;
  1380. }
  1381. else
  1382. {
  1383. return HAL_BUSY;
  1384. }
  1385. }
  1386. /**
  1387. * @brief Receive in slave mode an amount of data in non-blocking mode with Interrupt
  1388. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1389. * the configuration information for the specified I2C.
  1390. * @param pData Pointer to data buffer
  1391. * @param Size Amount of data to be sent
  1392. * @retval HAL status
  1393. */
  1394. HAL_StatusTypeDef HAL_I2C_Slave_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
  1395. {
  1396. if(hi2c->State == HAL_I2C_STATE_READY)
  1397. {
  1398. if((pData == NULL) || (Size == 0U))
  1399. {
  1400. return HAL_ERROR;
  1401. }
  1402. /* Process Locked */
  1403. __HAL_LOCK(hi2c);
  1404. /* Check if the I2C is already enabled */
  1405. if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  1406. {
  1407. /* Enable I2C peripheral */
  1408. __HAL_I2C_ENABLE(hi2c);
  1409. }
  1410. /* Disable Pos */
  1411. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  1412. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  1413. hi2c->Mode = HAL_I2C_MODE_SLAVE;
  1414. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1415. /* Prepare transfer parameters */
  1416. hi2c->pBuffPtr = pData;
  1417. hi2c->XferCount = Size;
  1418. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  1419. hi2c->XferSize = Size;
  1420. /* Enable Address Acknowledge */
  1421. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  1422. /* Process Unlocked */
  1423. __HAL_UNLOCK(hi2c);
  1424. /* Note : The I2C interrupts must be enabled after unlocking current process
  1425. to avoid the risk of I2C interrupt handle execution before current
  1426. process unlock */
  1427. /* Enable EVT, BUF and ERR interrupt */
  1428. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  1429. return HAL_OK;
  1430. }
  1431. else
  1432. {
  1433. return HAL_BUSY;
  1434. }
  1435. }
  1436. /**
  1437. * @brief Sequential transmit in slave mode an amount of data in non-blocking mode with Interrupt
  1438. * @note This interface allow to manage repeated start condition when a direction change during transfer
  1439. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1440. * the configuration information for the specified I2C.
  1441. * @param pData Pointer to data buffer
  1442. * @param Size Amount of data to be sent
  1443. * @param XferOptions Options of Transfer, value of @ref I2C_XferOptions_definition
  1444. * @retval HAL status
  1445. */
  1446. HAL_StatusTypeDef HAL_I2C_Slave_Sequential_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
  1447. {
  1448. /* Check the parameters */
  1449. assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  1450. if(hi2c->State == HAL_I2C_STATE_LISTEN)
  1451. {
  1452. if((pData == NULL) || (Size == 0U))
  1453. {
  1454. return HAL_ERROR;
  1455. }
  1456. /* Process Locked */
  1457. __HAL_LOCK(hi2c);
  1458. /* Check if the I2C is already enabled */
  1459. if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  1460. {
  1461. /* Enable I2C peripheral */
  1462. __HAL_I2C_ENABLE(hi2c);
  1463. }
  1464. /* Disable Pos */
  1465. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  1466. hi2c->State = HAL_I2C_STATE_BUSY_TX_LISTEN;
  1467. hi2c->Mode = HAL_I2C_MODE_SLAVE;
  1468. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1469. /* Prepare transfer parameters */
  1470. hi2c->pBuffPtr = pData;
  1471. hi2c->XferCount = Size;
  1472. hi2c->XferOptions = XferOptions;
  1473. hi2c->XferSize = hi2c->XferCount;
  1474. /* Clear ADDR flag */
  1475. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  1476. /* Process Unlocked */
  1477. __HAL_UNLOCK(hi2c);
  1478. /* Note : The I2C interrupts must be enabled after unlocking current process
  1479. to avoid the risk of I2C interrupt handle execution before current
  1480. process unlock */
  1481. /* Enable EVT, BUF and ERR interrupt */
  1482. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  1483. return HAL_OK;
  1484. }
  1485. else
  1486. {
  1487. return HAL_BUSY;
  1488. }
  1489. }
  1490. /**
  1491. * @brief Sequential receive in slave mode an amount of data in non-blocking mode with Interrupt
  1492. * @note This interface allow to manage repeated start condition when a direction change during transfer
  1493. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1494. * the configuration information for the specified I2C.
  1495. * @param pData Pointer to data buffer
  1496. * @param Size Amount of data to be sent
  1497. * @param XferOptions Options of Transfer, value of @ref I2C_XferOptions_definition
  1498. * @retval HAL status
  1499. */
  1500. HAL_StatusTypeDef HAL_I2C_Slave_Sequential_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
  1501. {
  1502. /* Check the parameters */
  1503. assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  1504. if(hi2c->State == HAL_I2C_STATE_LISTEN)
  1505. {
  1506. if((pData == NULL) || (Size == 0U))
  1507. {
  1508. return HAL_ERROR;
  1509. }
  1510. /* Process Locked */
  1511. __HAL_LOCK(hi2c);
  1512. /* Check if the I2C is already enabled */
  1513. if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  1514. {
  1515. /* Enable I2C peripheral */
  1516. __HAL_I2C_ENABLE(hi2c);
  1517. }
  1518. /* Disable Pos */
  1519. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  1520. hi2c->State = HAL_I2C_STATE_BUSY_RX_LISTEN;
  1521. hi2c->Mode = HAL_I2C_MODE_SLAVE;
  1522. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1523. /* Prepare transfer parameters */
  1524. hi2c->pBuffPtr = pData;
  1525. hi2c->XferCount = Size;
  1526. hi2c->XferOptions = XferOptions;
  1527. hi2c->XferSize = hi2c->XferCount;
  1528. /* Clear ADDR flag */
  1529. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  1530. /* Process Unlocked */
  1531. __HAL_UNLOCK(hi2c);
  1532. /* Note : The I2C interrupts must be enabled after unlocking current process
  1533. to avoid the risk of I2C interrupt handle execution before current
  1534. process unlock */
  1535. /* Enable EVT, BUF and ERR interrupt */
  1536. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  1537. return HAL_OK;
  1538. }
  1539. else
  1540. {
  1541. return HAL_BUSY;
  1542. }
  1543. }
  1544. /**
  1545. * @brief Enable the Address listen mode with Interrupt.
  1546. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1547. * the configuration information for the specified I2C.
  1548. * @retval HAL status
  1549. */
  1550. HAL_StatusTypeDef HAL_I2C_EnableListen_IT(I2C_HandleTypeDef *hi2c)
  1551. {
  1552. if(hi2c->State == HAL_I2C_STATE_READY)
  1553. {
  1554. hi2c->State = HAL_I2C_STATE_LISTEN;
  1555. /* Check if the I2C is already enabled */
  1556. if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  1557. {
  1558. /* Enable I2C peripheral */
  1559. __HAL_I2C_ENABLE(hi2c);
  1560. }
  1561. /* Enable Address Acknowledge */
  1562. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  1563. /* Enable EVT and ERR interrupt */
  1564. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
  1565. return HAL_OK;
  1566. }
  1567. else
  1568. {
  1569. return HAL_BUSY;
  1570. }
  1571. }
  1572. /**
  1573. * @brief Disable the Address listen mode with Interrupt.
  1574. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1575. * the configuration information for the specified I2C.
  1576. * @retval HAL status
  1577. */
  1578. HAL_StatusTypeDef HAL_I2C_DisableListen_IT(I2C_HandleTypeDef *hi2c)
  1579. {
  1580. /* Declaration of tmp to prevent undefined behavior of volatile usage */
  1581. uint32_t tmp;
  1582. /* Disable Address listen mode only if a transfer is not ongoing */
  1583. if(hi2c->State == HAL_I2C_STATE_LISTEN)
  1584. {
  1585. tmp = (uint32_t)(hi2c->State) & I2C_STATE_MSK;
  1586. hi2c->PreviousState = tmp | (uint32_t)(hi2c->Mode);
  1587. hi2c->State = HAL_I2C_STATE_READY;
  1588. hi2c->Mode = HAL_I2C_MODE_NONE;
  1589. /* Disable Address Acknowledge */
  1590. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  1591. /* Disable EVT and ERR interrupt */
  1592. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
  1593. return HAL_OK;
  1594. }
  1595. else
  1596. {
  1597. return HAL_BUSY;
  1598. }
  1599. }
  1600. /**
  1601. * @brief Transmit in master mode an amount of data in non-blocking mode with DMA
  1602. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1603. * the configuration information for the specified I2C.
  1604. * @param DevAddress Target device address: The device 7 bits address value
  1605. * in datasheet must be shift at right before call interface
  1606. * @param pData Pointer to data buffer
  1607. * @param Size Amount of data to be sent
  1608. * @retval HAL status
  1609. */
  1610. HAL_StatusTypeDef HAL_I2C_Master_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
  1611. {
  1612. __IO uint32_t count = 0U;
  1613. if(hi2c->State == HAL_I2C_STATE_READY)
  1614. {
  1615. /* Wait until BUSY flag is reset */
  1616. count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock /25U /1000U);
  1617. do
  1618. {
  1619. if(count-- == 0U)
  1620. {
  1621. hi2c->PreviousState = I2C_STATE_NONE;
  1622. hi2c->State= HAL_I2C_STATE_READY;
  1623. /* Process Unlocked */
  1624. __HAL_UNLOCK(hi2c);
  1625. return HAL_TIMEOUT;
  1626. }
  1627. }
  1628. while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
  1629. /* Process Locked */
  1630. __HAL_LOCK(hi2c);
  1631. /* Check if the I2C is already enabled */
  1632. if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  1633. {
  1634. /* Enable I2C peripheral */
  1635. __HAL_I2C_ENABLE(hi2c);
  1636. }
  1637. /* Disable Pos */
  1638. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  1639. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  1640. hi2c->Mode = HAL_I2C_MODE_MASTER;
  1641. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1642. /* Prepare transfer parameters */
  1643. hi2c->pBuffPtr = pData;
  1644. hi2c->XferCount = Size;
  1645. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  1646. hi2c->XferSize = hi2c->XferCount;
  1647. hi2c->Devaddress = DevAddress;
  1648. if(hi2c->XferSize > 0U)
  1649. {
  1650. /* Set the I2C DMA transfer complete callback */
  1651. hi2c->hdmatx->XferCpltCallback = I2C_DMAXferCplt;
  1652. /* Set the DMA error callback */
  1653. hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
  1654. /* Set the unused DMA callbacks to NULL */
  1655. hi2c->hdmatx->XferHalfCpltCallback = NULL;
  1656. hi2c->hdmatx->XferAbortCallback = NULL;
  1657. /* Enable the DMA Channel */
  1658. HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->DR, hi2c->XferSize);
  1659. /* Enable Acknowledge */
  1660. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  1661. /* Generate Start */
  1662. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  1663. /* Process Unlocked */
  1664. __HAL_UNLOCK(hi2c);
  1665. /* Note : The I2C interrupts must be enabled after unlocking current process
  1666. to avoid the risk of I2C interrupt handle execution before current
  1667. process unlock */
  1668. /* Enable EVT and ERR interrupt */
  1669. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
  1670. /* Enable DMA Request */
  1671. SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
  1672. }
  1673. else
  1674. {
  1675. /* Enable Acknowledge */
  1676. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  1677. /* Generate Start */
  1678. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  1679. /* Process Unlocked */
  1680. __HAL_UNLOCK(hi2c);
  1681. /* Note : The I2C interrupts must be enabled after unlocking current process
  1682. to avoid the risk of I2C interrupt handle execution before current
  1683. process unlock */
  1684. /* Enable EVT, BUF and ERR interrupt */
  1685. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  1686. }
  1687. return HAL_OK;
  1688. }
  1689. else
  1690. {
  1691. return HAL_BUSY;
  1692. }
  1693. }
  1694. /**
  1695. * @brief Receive in master mode an amount of data in non-blocking mode with DMA
  1696. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1697. * the configuration information for the specified I2C.
  1698. * @param DevAddress Target device address: The device 7 bits address value
  1699. * in datasheet must be shift at right before call interface
  1700. * @param pData Pointer to data buffer
  1701. * @param Size Amount of data to be sent
  1702. * @retval HAL status
  1703. */
  1704. HAL_StatusTypeDef HAL_I2C_Master_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
  1705. {
  1706. __IO uint32_t count = 0U;
  1707. if(hi2c->State == HAL_I2C_STATE_READY)
  1708. {
  1709. /* Wait until BUSY flag is reset */
  1710. count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock /25U /1000U);
  1711. do
  1712. {
  1713. if(count-- == 0U)
  1714. {
  1715. hi2c->PreviousState = I2C_STATE_NONE;
  1716. hi2c->State= HAL_I2C_STATE_READY;
  1717. /* Process Unlocked */
  1718. __HAL_UNLOCK(hi2c);
  1719. return HAL_TIMEOUT;
  1720. }
  1721. }
  1722. while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
  1723. /* Process Locked */
  1724. __HAL_LOCK(hi2c);
  1725. /* Check if the I2C is already enabled */
  1726. if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  1727. {
  1728. /* Enable I2C peripheral */
  1729. __HAL_I2C_ENABLE(hi2c);
  1730. }
  1731. /* Disable Pos */
  1732. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  1733. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  1734. hi2c->Mode = HAL_I2C_MODE_MASTER;
  1735. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1736. /* Prepare transfer parameters */
  1737. hi2c->pBuffPtr = pData;
  1738. hi2c->XferCount = Size;
  1739. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  1740. hi2c->XferSize = hi2c->XferCount;
  1741. hi2c->Devaddress = DevAddress;
  1742. if(hi2c->XferSize > 0U)
  1743. {
  1744. /* Set the I2C DMA transfer complete callback */
  1745. hi2c->hdmarx->XferCpltCallback = I2C_DMAXferCplt;
  1746. /* Set the DMA error callback */
  1747. hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
  1748. /* Set the unused DMA callbacks to NULL */
  1749. hi2c->hdmarx->XferHalfCpltCallback = NULL;
  1750. hi2c->hdmarx->XferAbortCallback = NULL;
  1751. /* Enable the DMA Channel */
  1752. HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->DR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize);
  1753. /* Enable Acknowledge */
  1754. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  1755. /* Generate Start */
  1756. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  1757. /* Process Unlocked */
  1758. __HAL_UNLOCK(hi2c);
  1759. /* Note : The I2C interrupts must be enabled after unlocking current process
  1760. to avoid the risk of I2C interrupt handle execution before current
  1761. process unlock */
  1762. /* Enable EVT and ERR interrupt */
  1763. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
  1764. /* Enable DMA Request */
  1765. SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
  1766. }
  1767. else
  1768. {
  1769. /* Enable Acknowledge */
  1770. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  1771. /* Generate Start */
  1772. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  1773. /* Process Unlocked */
  1774. __HAL_UNLOCK(hi2c);
  1775. /* Note : The I2C interrupts must be enabled after unlocking current process
  1776. to avoid the risk of I2C interrupt handle execution before current
  1777. process unlock */
  1778. /* Enable EVT, BUF and ERR interrupt */
  1779. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  1780. }
  1781. return HAL_OK;
  1782. }
  1783. else
  1784. {
  1785. return HAL_BUSY;
  1786. }
  1787. }
  1788. /**
  1789. * @brief Abort a master I2C process communication with Interrupt.
  1790. * @note This abort can be called only if state is ready
  1791. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1792. * the configuration information for the specified I2C.
  1793. * @param DevAddress Target device address: The device 7 bits address value
  1794. * in datasheet must be shift at right before call interface
  1795. * @retval HAL status
  1796. */
  1797. HAL_StatusTypeDef HAL_I2C_Master_Abort_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress)
  1798. {
  1799. /* Abort Master transfer during Receive or Transmit process */
  1800. if(hi2c->Mode == HAL_I2C_MODE_MASTER)
  1801. {
  1802. /* Process Locked */
  1803. __HAL_LOCK(hi2c);
  1804. hi2c->PreviousState = I2C_STATE_NONE;
  1805. hi2c->State = HAL_I2C_STATE_ABORT;
  1806. /* Disable Acknowledge */
  1807. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  1808. /* Generate Stop */
  1809. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  1810. hi2c->XferCount = 0U;
  1811. /* Disable EVT, BUF and ERR interrupt */
  1812. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  1813. /* Process Unlocked */
  1814. __HAL_UNLOCK(hi2c);
  1815. /* Call the corresponding callback to inform upper layer of End of Transfer */
  1816. I2C_ITError(hi2c);
  1817. return HAL_OK;
  1818. }
  1819. else
  1820. {
  1821. /* Wrong usage of abort function */
  1822. /* This function should be used only in case of abort monitored by master device */
  1823. return HAL_ERROR;
  1824. }
  1825. }
  1826. /**
  1827. * @brief Transmit in slave mode an amount of data in non-blocking mode with DMA
  1828. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1829. * the configuration information for the specified I2C.
  1830. * @param pData Pointer to data buffer
  1831. * @param Size Amount of data to be sent
  1832. * @retval HAL status
  1833. */
  1834. HAL_StatusTypeDef HAL_I2C_Slave_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
  1835. {
  1836. if(hi2c->State == HAL_I2C_STATE_READY)
  1837. {
  1838. if((pData == NULL) || (Size == 0U))
  1839. {
  1840. return HAL_ERROR;
  1841. }
  1842. /* Process Locked */
  1843. __HAL_LOCK(hi2c);
  1844. /* Check if the I2C is already enabled */
  1845. if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  1846. {
  1847. /* Enable I2C peripheral */
  1848. __HAL_I2C_ENABLE(hi2c);
  1849. }
  1850. /* Disable Pos */
  1851. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  1852. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  1853. hi2c->Mode = HAL_I2C_MODE_SLAVE;
  1854. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1855. /* Prepare transfer parameters */
  1856. hi2c->pBuffPtr = pData;
  1857. hi2c->XferCount = Size;
  1858. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  1859. hi2c->XferSize = hi2c->XferCount;
  1860. /* Set the I2C DMA transfer complete callback */
  1861. hi2c->hdmatx->XferCpltCallback = I2C_DMAXferCplt;
  1862. /* Set the DMA error callback */
  1863. hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
  1864. /* Set the unused DMA callbacks to NULL */
  1865. hi2c->hdmatx->XferHalfCpltCallback = NULL;
  1866. hi2c->hdmatx->XferAbortCallback = NULL;
  1867. /* Enable the DMA Channel */
  1868. HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->DR, hi2c->XferSize);
  1869. /* Enable Address Acknowledge */
  1870. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  1871. /* Process Unlocked */
  1872. __HAL_UNLOCK(hi2c);
  1873. /* Note : The I2C interrupts must be enabled after unlocking current process
  1874. to avoid the risk of I2C interrupt handle execution before current
  1875. process unlock */
  1876. /* Enable EVT and ERR interrupt */
  1877. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
  1878. /* Enable DMA Request */
  1879. hi2c->Instance->CR2 |= I2C_CR2_DMAEN;
  1880. return HAL_OK;
  1881. }
  1882. else
  1883. {
  1884. return HAL_BUSY;
  1885. }
  1886. }
  1887. /**
  1888. * @brief Receive in slave mode an amount of data in non-blocking mode with DMA
  1889. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1890. * the configuration information for the specified I2C.
  1891. * @param pData Pointer to data buffer
  1892. * @param Size Amount of data to be sent
  1893. * @retval HAL status
  1894. */
  1895. HAL_StatusTypeDef HAL_I2C_Slave_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
  1896. {
  1897. if(hi2c->State == HAL_I2C_STATE_READY)
  1898. {
  1899. if((pData == NULL) || (Size == 0U))
  1900. {
  1901. return HAL_ERROR;
  1902. }
  1903. /* Process Locked */
  1904. __HAL_LOCK(hi2c);
  1905. /* Check if the I2C is already enabled */
  1906. if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  1907. {
  1908. /* Enable I2C peripheral */
  1909. __HAL_I2C_ENABLE(hi2c);
  1910. }
  1911. /* Disable Pos */
  1912. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  1913. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  1914. hi2c->Mode = HAL_I2C_MODE_SLAVE;
  1915. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1916. /* Prepare transfer parameters */
  1917. hi2c->pBuffPtr = pData;
  1918. hi2c->XferCount = Size;
  1919. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  1920. hi2c->XferSize = hi2c->XferCount;
  1921. /* Set the I2C DMA transfer complete callback */
  1922. hi2c->hdmarx->XferCpltCallback = I2C_DMAXferCplt;
  1923. /* Set the DMA error callback */
  1924. hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
  1925. /* Set the unused DMA callbacks to NULL */
  1926. hi2c->hdmarx->XferHalfCpltCallback = NULL;
  1927. hi2c->hdmarx->XferAbortCallback = NULL;
  1928. /* Enable the DMA Channel */
  1929. HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->DR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize);
  1930. /* Enable Address Acknowledge */
  1931. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  1932. /* Process Unlocked */
  1933. __HAL_UNLOCK(hi2c);
  1934. /* Note : The I2C interrupts must be enabled after unlocking current process
  1935. to avoid the risk of I2C interrupt handle execution before current
  1936. process unlock */
  1937. /* Enable EVT and ERR interrupt */
  1938. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
  1939. /* Enable DMA Request */
  1940. SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
  1941. return HAL_OK;
  1942. }
  1943. else
  1944. {
  1945. return HAL_BUSY;
  1946. }
  1947. }
  1948. /**
  1949. * @brief Write an amount of data in blocking mode to a specific memory address
  1950. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1951. * the configuration information for the specified I2C.
  1952. * @param DevAddress Target device address: The device 7 bits address value
  1953. * in datasheet must be shift at right before call interface
  1954. * @param MemAddress Internal memory address
  1955. * @param MemAddSize Size of internal memory address
  1956. * @param pData Pointer to data buffer
  1957. * @param Size Amount of data to be sent
  1958. * @param Timeout Timeout duration
  1959. * @retval HAL status
  1960. */
  1961. HAL_StatusTypeDef HAL_I2C_Mem_Write(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  1962. {
  1963. uint32_t tickstart = 0x00U;
  1964. /* Init tickstart for timeout management*/
  1965. tickstart = HAL_GetTick();
  1966. /* Check the parameters */
  1967. assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
  1968. if(hi2c->State == HAL_I2C_STATE_READY)
  1969. {
  1970. if((pData == NULL) || (Size == 0U))
  1971. {
  1972. return HAL_ERROR;
  1973. }
  1974. /* Wait until BUSY flag is reset */
  1975. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK)
  1976. {
  1977. return HAL_BUSY;
  1978. }
  1979. /* Process Locked */
  1980. __HAL_LOCK(hi2c);
  1981. /* Check if the I2C is already enabled */
  1982. if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  1983. {
  1984. /* Enable I2C peripheral */
  1985. __HAL_I2C_ENABLE(hi2c);
  1986. }
  1987. /* Disable Pos */
  1988. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  1989. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  1990. hi2c->Mode = HAL_I2C_MODE_MEM;
  1991. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1992. /* Prepare transfer parameters */
  1993. hi2c->pBuffPtr = pData;
  1994. hi2c->XferCount = Size;
  1995. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  1996. hi2c->XferSize = hi2c->XferCount;
  1997. /* Send Slave Address and Memory Address */
  1998. if(I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, Timeout, tickstart) != HAL_OK)
  1999. {
  2000. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  2001. {
  2002. /* Process Unlocked */
  2003. __HAL_UNLOCK(hi2c);
  2004. return HAL_ERROR;
  2005. }
  2006. else
  2007. {
  2008. /* Process Unlocked */
  2009. __HAL_UNLOCK(hi2c);
  2010. return HAL_TIMEOUT;
  2011. }
  2012. }
  2013. while(hi2c->XferSize > 0U)
  2014. {
  2015. /* Wait until TXE flag is set */
  2016. if(I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
  2017. {
  2018. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  2019. {
  2020. /* Generate Stop */
  2021. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  2022. return HAL_ERROR;
  2023. }
  2024. else
  2025. {
  2026. return HAL_TIMEOUT;
  2027. }
  2028. }
  2029. /* Write data to DR */
  2030. hi2c->Instance->DR = (*hi2c->pBuffPtr++);
  2031. hi2c->XferSize--;
  2032. hi2c->XferCount--;
  2033. if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (hi2c->XferSize != 0U))
  2034. {
  2035. /* Write data to DR */
  2036. hi2c->Instance->DR = (*hi2c->pBuffPtr++);
  2037. hi2c->XferSize--;
  2038. hi2c->XferCount--;
  2039. }
  2040. }
  2041. /* Wait until BTF flag is set */
  2042. if(I2C_WaitOnBTFFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
  2043. {
  2044. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  2045. {
  2046. /* Generate Stop */
  2047. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  2048. return HAL_ERROR;
  2049. }
  2050. else
  2051. {
  2052. return HAL_TIMEOUT;
  2053. }
  2054. }
  2055. /* Generate Stop */
  2056. SET_BIT(hi2c->Instance->CR1,I2C_CR1_STOP);
  2057. hi2c->State = HAL_I2C_STATE_READY;
  2058. hi2c->Mode = HAL_I2C_MODE_NONE;
  2059. /* Process Unlocked */
  2060. __HAL_UNLOCK(hi2c);
  2061. return HAL_OK;
  2062. }
  2063. else
  2064. {
  2065. return HAL_BUSY;
  2066. }
  2067. }
  2068. /**
  2069. * @brief Read an amount of data in blocking mode from a specific memory address
  2070. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2071. * the configuration information for the specified I2C.
  2072. * @param DevAddress Target device address: The device 7 bits address value
  2073. * in datasheet must be shift at right before call interface
  2074. * @param MemAddress Internal memory address
  2075. * @param MemAddSize Size of internal memory address
  2076. * @param pData Pointer to data buffer
  2077. * @param Size Amount of data to be sent
  2078. * @param Timeout Timeout duration
  2079. * @retval HAL status
  2080. */
  2081. HAL_StatusTypeDef HAL_I2C_Mem_Read(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  2082. {
  2083. uint32_t tickstart = 0x00U;
  2084. /* Init tickstart for timeout management*/
  2085. tickstart = HAL_GetTick();
  2086. /* Check the parameters */
  2087. assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
  2088. if(hi2c->State == HAL_I2C_STATE_READY)
  2089. {
  2090. if((pData == NULL) || (Size == 0U))
  2091. {
  2092. return HAL_ERROR;
  2093. }
  2094. /* Wait until BUSY flag is reset */
  2095. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK)
  2096. {
  2097. return HAL_BUSY;
  2098. }
  2099. /* Process Locked */
  2100. __HAL_LOCK(hi2c);
  2101. /* Check if the I2C is already enabled */
  2102. if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  2103. {
  2104. /* Enable I2C peripheral */
  2105. __HAL_I2C_ENABLE(hi2c);
  2106. }
  2107. /* Disable Pos */
  2108. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  2109. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  2110. hi2c->Mode = HAL_I2C_MODE_MEM;
  2111. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2112. /* Prepare transfer parameters */
  2113. hi2c->pBuffPtr = pData;
  2114. hi2c->XferCount = Size;
  2115. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  2116. hi2c->XferSize = hi2c->XferCount;
  2117. /* Send Slave Address and Memory Address */
  2118. if(I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, Timeout, tickstart) != HAL_OK)
  2119. {
  2120. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  2121. {
  2122. /* Process Unlocked */
  2123. __HAL_UNLOCK(hi2c);
  2124. return HAL_ERROR;
  2125. }
  2126. else
  2127. {
  2128. /* Process Unlocked */
  2129. __HAL_UNLOCK(hi2c);
  2130. return HAL_TIMEOUT;
  2131. }
  2132. }
  2133. if(hi2c->XferSize == 1U)
  2134. {
  2135. /* Disable Acknowledge */
  2136. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  2137. /* Clear ADDR flag */
  2138. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  2139. /* Generate Stop */
  2140. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  2141. }
  2142. else if(hi2c->XferSize == 2U)
  2143. {
  2144. /* Disable Acknowledge */
  2145. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  2146. /* Enable Pos */
  2147. SET_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  2148. /* Clear ADDR flag */
  2149. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  2150. }
  2151. else
  2152. {
  2153. /* Clear ADDR flag */
  2154. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  2155. }
  2156. while(hi2c->XferSize > 0U)
  2157. {
  2158. if(hi2c->XferSize <= 3U)
  2159. {
  2160. /* One byte */
  2161. if(hi2c->XferSize== 1U)
  2162. {
  2163. /* Wait until RXNE flag is set */
  2164. if(I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
  2165. {
  2166. if(hi2c->ErrorCode == HAL_I2C_ERROR_TIMEOUT)
  2167. {
  2168. return HAL_TIMEOUT;
  2169. }
  2170. else
  2171. {
  2172. return HAL_ERROR;
  2173. }
  2174. }
  2175. /* Read data from DR */
  2176. (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
  2177. hi2c->XferSize--;
  2178. hi2c->XferCount--;
  2179. }
  2180. /* Two bytes */
  2181. else if(Size == 2U)
  2182. {
  2183. /* Wait until BTF flag is set */
  2184. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout, tickstart) != HAL_OK)
  2185. {
  2186. return HAL_TIMEOUT;
  2187. }
  2188. /* Generate Stop */
  2189. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  2190. /* Read data from DR */
  2191. (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
  2192. hi2c->XferSize--;
  2193. hi2c->XferCount--;
  2194. /* Read data from DR */
  2195. (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
  2196. hi2c->XferSize--;
  2197. hi2c->XferCount--;
  2198. }
  2199. /* 3 Last bytes */
  2200. else
  2201. {
  2202. /* Wait until BTF flag is set */
  2203. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout, tickstart) != HAL_OK)
  2204. {
  2205. return HAL_TIMEOUT;
  2206. }
  2207. /* Disable Acknowledge */
  2208. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  2209. /* Read data from DR */
  2210. (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
  2211. hi2c->XferSize--;
  2212. hi2c->XferCount--;
  2213. /* Wait until BTF flag is set */
  2214. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout, tickstart) != HAL_OK)
  2215. {
  2216. return HAL_TIMEOUT;
  2217. }
  2218. /* Generate Stop */
  2219. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  2220. /* Read data from DR */
  2221. (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
  2222. hi2c->XferSize--;
  2223. hi2c->XferCount--;
  2224. /* Read data from DR */
  2225. (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
  2226. hi2c->XferSize--;
  2227. hi2c->XferCount--;
  2228. }
  2229. }
  2230. else
  2231. {
  2232. /* Wait until RXNE flag is set */
  2233. if(I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
  2234. {
  2235. if(hi2c->ErrorCode == HAL_I2C_ERROR_TIMEOUT)
  2236. {
  2237. return HAL_TIMEOUT;
  2238. }
  2239. else
  2240. {
  2241. return HAL_ERROR;
  2242. }
  2243. }
  2244. /* Read data from DR */
  2245. (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
  2246. hi2c->XferSize--;
  2247. hi2c->XferCount--;
  2248. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET)
  2249. {
  2250. /* Read data from DR */
  2251. (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
  2252. hi2c->XferSize--;
  2253. hi2c->XferCount--;
  2254. }
  2255. }
  2256. }
  2257. hi2c->State = HAL_I2C_STATE_READY;
  2258. hi2c->Mode = HAL_I2C_MODE_NONE;
  2259. /* Process Unlocked */
  2260. __HAL_UNLOCK(hi2c);
  2261. return HAL_OK;
  2262. }
  2263. else
  2264. {
  2265. return HAL_BUSY;
  2266. }
  2267. }
  2268. /**
  2269. * @brief Write an amount of data in non-blocking mode with Interrupt to a specific memory address
  2270. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2271. * the configuration information for the specified I2C.
  2272. * @param DevAddress Target device address: The device 7 bits address value
  2273. * in datasheet must be shift at right before call interface
  2274. * @param MemAddress Internal memory address
  2275. * @param MemAddSize Size of internal memory address
  2276. * @param pData Pointer to data buffer
  2277. * @param Size Amount of data to be sent
  2278. * @retval HAL status
  2279. */
  2280. HAL_StatusTypeDef HAL_I2C_Mem_Write_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
  2281. {
  2282. uint32_t tickstart = 0x00U;
  2283. /* Init tickstart for timeout management*/
  2284. tickstart = HAL_GetTick();
  2285. /* Check the parameters */
  2286. assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
  2287. if(hi2c->State == HAL_I2C_STATE_READY)
  2288. {
  2289. if((pData == NULL) || (Size == 0U))
  2290. {
  2291. return HAL_ERROR;
  2292. }
  2293. /* Wait until BUSY flag is reset */
  2294. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK)
  2295. {
  2296. return HAL_BUSY;
  2297. }
  2298. /* Process Locked */
  2299. __HAL_LOCK(hi2c);
  2300. /* Check if the I2C is already enabled */
  2301. if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  2302. {
  2303. /* Enable I2C peripheral */
  2304. __HAL_I2C_ENABLE(hi2c);
  2305. }
  2306. /* Disable Pos */
  2307. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  2308. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  2309. hi2c->Mode = HAL_I2C_MODE_MEM;
  2310. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2311. /* Prepare transfer parameters */
  2312. hi2c->pBuffPtr = pData;
  2313. hi2c->XferCount = Size;
  2314. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  2315. hi2c->XferSize = hi2c->XferCount;
  2316. /* Send Slave Address and Memory Address */
  2317. if(I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG, tickstart) != HAL_OK)
  2318. {
  2319. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  2320. {
  2321. /* Process Unlocked */
  2322. __HAL_UNLOCK(hi2c);
  2323. return HAL_ERROR;
  2324. }
  2325. else
  2326. {
  2327. /* Process Unlocked */
  2328. __HAL_UNLOCK(hi2c);
  2329. return HAL_TIMEOUT;
  2330. }
  2331. }
  2332. /* Process Unlocked */
  2333. __HAL_UNLOCK(hi2c);
  2334. /* Note : The I2C interrupts must be enabled after unlocking current process
  2335. to avoid the risk of I2C interrupt handle execution before current
  2336. process unlock */
  2337. /* Enable EVT, BUF and ERR interrupt */
  2338. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  2339. return HAL_OK;
  2340. }
  2341. else
  2342. {
  2343. return HAL_BUSY;
  2344. }
  2345. }
  2346. /**
  2347. * @brief Read an amount of data in non-blocking mode with Interrupt from a specific memory address
  2348. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2349. * the configuration information for the specified I2C.
  2350. * @param DevAddress Target device address: The device 7 bits address value
  2351. * in datasheet must be shift at right before call interface
  2352. * @param MemAddress Internal memory address
  2353. * @param MemAddSize Size of internal memory address
  2354. * @param pData Pointer to data buffer
  2355. * @param Size Amount of data to be sent
  2356. * @retval HAL status
  2357. */
  2358. HAL_StatusTypeDef HAL_I2C_Mem_Read_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
  2359. {
  2360. uint32_t tickstart = 0x00U;
  2361. /* Init tickstart for timeout management*/
  2362. tickstart = HAL_GetTick();
  2363. /* Check the parameters */
  2364. assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
  2365. if(hi2c->State == HAL_I2C_STATE_READY)
  2366. {
  2367. if((pData == NULL) || (Size == 0U))
  2368. {
  2369. return HAL_ERROR;
  2370. }
  2371. /* Wait until BUSY flag is reset */
  2372. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK)
  2373. {
  2374. return HAL_BUSY;
  2375. }
  2376. /* Process Locked */
  2377. __HAL_LOCK(hi2c);
  2378. /* Check if the I2C is already enabled */
  2379. if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  2380. {
  2381. /* Enable I2C peripheral */
  2382. __HAL_I2C_ENABLE(hi2c);
  2383. }
  2384. /* Disable Pos */
  2385. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  2386. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  2387. hi2c->Mode = HAL_I2C_MODE_MEM;
  2388. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2389. /* Prepare transfer parameters */
  2390. hi2c->pBuffPtr = pData;
  2391. hi2c->XferCount = Size;
  2392. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  2393. hi2c->XferSize = hi2c->XferCount;
  2394. /* Send Slave Address and Memory Address */
  2395. if(I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG, tickstart) != HAL_OK)
  2396. {
  2397. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  2398. {
  2399. /* Process Unlocked */
  2400. __HAL_UNLOCK(hi2c);
  2401. return HAL_ERROR;
  2402. }
  2403. else
  2404. {
  2405. /* Process Unlocked */
  2406. __HAL_UNLOCK(hi2c);
  2407. return HAL_TIMEOUT;
  2408. }
  2409. }
  2410. if(hi2c->XferCount == 1U)
  2411. {
  2412. /* Disable Acknowledge */
  2413. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  2414. /* Clear ADDR flag */
  2415. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  2416. /* Generate Stop */
  2417. SET_BIT(hi2c->Instance->CR1,I2C_CR1_STOP);
  2418. }
  2419. else if(hi2c->XferCount == 2U)
  2420. {
  2421. /* Disable Acknowledge */
  2422. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  2423. /* Enable Pos */
  2424. SET_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  2425. /* Clear ADDR flag */
  2426. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  2427. }
  2428. else
  2429. {
  2430. /* Enable Acknowledge */
  2431. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  2432. /* Clear ADDR flag */
  2433. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  2434. }
  2435. /* Process Unlocked */
  2436. __HAL_UNLOCK(hi2c);
  2437. /* Note : The I2C interrupts must be enabled after unlocking current process
  2438. to avoid the risk of I2C interrupt handle execution before current
  2439. process unlock */
  2440. /* Enable EVT, BUF and ERR interrupt */
  2441. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  2442. return HAL_OK;
  2443. }
  2444. else
  2445. {
  2446. return HAL_BUSY;
  2447. }
  2448. }
  2449. /**
  2450. * @brief Write an amount of data in non-blocking mode with DMA to a specific memory address
  2451. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2452. * the configuration information for the specified I2C.
  2453. * @param DevAddress Target device address: The device 7 bits address value
  2454. * in datasheet must be shift at right before call interface
  2455. * @param MemAddress Internal memory address
  2456. * @param MemAddSize Size of internal memory address
  2457. * @param pData Pointer to data buffer
  2458. * @param Size Amount of data to be sent
  2459. * @retval HAL status
  2460. */
  2461. HAL_StatusTypeDef HAL_I2C_Mem_Write_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
  2462. {
  2463. uint32_t tickstart = 0x00U;
  2464. /* Init tickstart for timeout management*/
  2465. tickstart = HAL_GetTick();
  2466. /* Check the parameters */
  2467. assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
  2468. if(hi2c->State == HAL_I2C_STATE_READY)
  2469. {
  2470. if((pData == NULL) || (Size == 0U))
  2471. {
  2472. return HAL_ERROR;
  2473. }
  2474. /* Wait until BUSY flag is reset */
  2475. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK)
  2476. {
  2477. return HAL_BUSY;
  2478. }
  2479. /* Process Locked */
  2480. __HAL_LOCK(hi2c);
  2481. /* Check if the I2C is already enabled */
  2482. if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  2483. {
  2484. /* Enable I2C peripheral */
  2485. __HAL_I2C_ENABLE(hi2c);
  2486. }
  2487. /* Disable Pos */
  2488. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  2489. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  2490. hi2c->Mode = HAL_I2C_MODE_MEM;
  2491. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2492. /* Prepare transfer parameters */
  2493. hi2c->pBuffPtr = pData;
  2494. hi2c->XferCount = Size;
  2495. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  2496. hi2c->XferSize = hi2c->XferCount;
  2497. /* Set the I2C DMA transfer complete callback */
  2498. hi2c->hdmatx->XferCpltCallback = I2C_DMAXferCplt;
  2499. /* Set the DMA error callback */
  2500. hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
  2501. /* Set the unused DMA callbacks to NULL */
  2502. hi2c->hdmatx->XferHalfCpltCallback = NULL;
  2503. hi2c->hdmatx->XferAbortCallback = NULL;
  2504. /* Enable the DMA Channel */
  2505. HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->DR, hi2c->XferSize);
  2506. /* Send Slave Address and Memory Address */
  2507. if(I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG, tickstart) != HAL_OK)
  2508. {
  2509. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  2510. {
  2511. /* Process Unlocked */
  2512. __HAL_UNLOCK(hi2c);
  2513. return HAL_ERROR;
  2514. }
  2515. else
  2516. {
  2517. /* Process Unlocked */
  2518. __HAL_UNLOCK(hi2c);
  2519. return HAL_TIMEOUT;
  2520. }
  2521. }
  2522. /* Clear ADDR flag */
  2523. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  2524. /* Process Unlocked */
  2525. __HAL_UNLOCK(hi2c);
  2526. /* Note : The I2C interrupts must be enabled after unlocking current process
  2527. to avoid the risk of I2C interrupt handle execution before current
  2528. process unlock */
  2529. /* Enable ERR interrupt */
  2530. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_ERR);
  2531. /* Enable DMA Request */
  2532. SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
  2533. return HAL_OK;
  2534. }
  2535. else
  2536. {
  2537. return HAL_BUSY;
  2538. }
  2539. }
  2540. /**
  2541. * @brief Reads an amount of data in non-blocking mode with DMA from a specific memory address.
  2542. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2543. * the configuration information for the specified I2C.
  2544. * @param DevAddress Target device address: The device 7 bits address value
  2545. * in datasheet must be shift at right before call interface
  2546. * @param MemAddress Internal memory address
  2547. * @param MemAddSize Size of internal memory address
  2548. * @param pData Pointer to data buffer
  2549. * @param Size Amount of data to be read
  2550. * @retval HAL status
  2551. */
  2552. HAL_StatusTypeDef HAL_I2C_Mem_Read_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
  2553. {
  2554. uint32_t tickstart = 0x00U;
  2555. /* Init tickstart for timeout management*/
  2556. tickstart = HAL_GetTick();
  2557. /* Check the parameters */
  2558. assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
  2559. if(hi2c->State == HAL_I2C_STATE_READY)
  2560. {
  2561. if((pData == NULL) || (Size == 0U))
  2562. {
  2563. return HAL_ERROR;
  2564. }
  2565. /* Wait until BUSY flag is reset */
  2566. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK)
  2567. {
  2568. return HAL_BUSY;
  2569. }
  2570. /* Process Locked */
  2571. __HAL_LOCK(hi2c);
  2572. /* Check if the I2C is already enabled */
  2573. if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  2574. {
  2575. /* Enable I2C peripheral */
  2576. __HAL_I2C_ENABLE(hi2c);
  2577. }
  2578. /* Disable Pos */
  2579. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  2580. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  2581. hi2c->Mode = HAL_I2C_MODE_MEM;
  2582. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2583. /* Prepare transfer parameters */
  2584. hi2c->pBuffPtr = pData;
  2585. hi2c->XferCount = Size;
  2586. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  2587. hi2c->XferSize = hi2c->XferCount;
  2588. /* Set the I2C DMA transfer complete callback */
  2589. hi2c->hdmarx->XferCpltCallback = I2C_DMAXferCplt;
  2590. /* Set the DMA error callback */
  2591. hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
  2592. /* Set the unused DMA callbacks to NULL */
  2593. hi2c->hdmarx->XferHalfCpltCallback = NULL;
  2594. hi2c->hdmarx->XferAbortCallback = NULL;
  2595. /* Enable the DMA Channel */
  2596. HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->DR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize);
  2597. /* Send Slave Address and Memory Address */
  2598. if(I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG, tickstart) != HAL_OK)
  2599. {
  2600. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  2601. {
  2602. /* Process Unlocked */
  2603. __HAL_UNLOCK(hi2c);
  2604. return HAL_ERROR;
  2605. }
  2606. else
  2607. {
  2608. /* Process Unlocked */
  2609. __HAL_UNLOCK(hi2c);
  2610. return HAL_TIMEOUT;
  2611. }
  2612. }
  2613. if(Size == 1U)
  2614. {
  2615. /* Disable Acknowledge */
  2616. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  2617. }
  2618. else
  2619. {
  2620. /* Enable Last DMA bit */
  2621. SET_BIT(hi2c->Instance->CR2, I2C_CR2_LAST);
  2622. }
  2623. /* Clear ADDR flag */
  2624. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  2625. /* Process Unlocked */
  2626. __HAL_UNLOCK(hi2c);
  2627. /* Note : The I2C interrupts must be enabled after unlocking current process
  2628. to avoid the risk of I2C interrupt handle execution before current
  2629. process unlock */
  2630. /* Enable ERR interrupt */
  2631. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_ERR);
  2632. /* Enable DMA Request */
  2633. SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
  2634. return HAL_OK;
  2635. }
  2636. else
  2637. {
  2638. return HAL_BUSY;
  2639. }
  2640. }
  2641. /**
  2642. * @brief Checks if target device is ready for communication.
  2643. * @note This function is used with Memory devices
  2644. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2645. * the configuration information for the specified I2C.
  2646. * @param DevAddress Target device address: The device 7 bits address value
  2647. * in datasheet must be shift at right before call interface
  2648. * @param Trials Number of trials
  2649. * @param Timeout Timeout duration
  2650. * @retval HAL status
  2651. */
  2652. HAL_StatusTypeDef HAL_I2C_IsDeviceReady(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Trials, uint32_t Timeout)
  2653. {
  2654. uint32_t tickstart = 0U, I2C_Trials = 1U;
  2655. /* Get tick */
  2656. tickstart = HAL_GetTick();
  2657. if(hi2c->State == HAL_I2C_STATE_READY)
  2658. {
  2659. /* Wait until BUSY flag is reset */
  2660. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK)
  2661. {
  2662. return HAL_BUSY;
  2663. }
  2664. /* Process Locked */
  2665. __HAL_LOCK(hi2c);
  2666. /* Check if the I2C is already enabled */
  2667. if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  2668. {
  2669. /* Enable I2C peripheral */
  2670. __HAL_I2C_ENABLE(hi2c);
  2671. }
  2672. /* Disable Pos */
  2673. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  2674. hi2c->State = HAL_I2C_STATE_BUSY;
  2675. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2676. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  2677. do
  2678. {
  2679. /* Generate Start */
  2680. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  2681. /* Wait until SB flag is set */
  2682. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, tickstart) != HAL_OK)
  2683. {
  2684. return HAL_TIMEOUT;
  2685. }
  2686. /* Send slave address */
  2687. hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(DevAddress);
  2688. /* Wait until ADDR or AF flag are set */
  2689. /* Get tick */
  2690. tickstart = HAL_GetTick();
  2691. while((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR) == RESET) && \
  2692. (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == RESET) && \
  2693. (hi2c->State != HAL_I2C_STATE_TIMEOUT))
  2694. {
  2695. if(Timeout != HAL_MAX_DELAY)
  2696. {
  2697. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  2698. {
  2699. hi2c->State = HAL_I2C_STATE_TIMEOUT;
  2700. }
  2701. }
  2702. }
  2703. hi2c->State = HAL_I2C_STATE_READY;
  2704. /* Check if the ADDR flag has been set */
  2705. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR) == SET)
  2706. {
  2707. /* Generate Stop */
  2708. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  2709. /* Clear ADDR Flag */
  2710. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  2711. /* Wait until BUSY flag is reset */
  2712. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK)
  2713. {
  2714. return HAL_TIMEOUT;
  2715. }
  2716. hi2c->State = HAL_I2C_STATE_READY;
  2717. /* Process Unlocked */
  2718. __HAL_UNLOCK(hi2c);
  2719. return HAL_OK;
  2720. }
  2721. else
  2722. {
  2723. /* Generate Stop */
  2724. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  2725. /* Clear AF Flag */
  2726. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  2727. /* Wait until BUSY flag is reset */
  2728. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK)
  2729. {
  2730. return HAL_TIMEOUT;
  2731. }
  2732. }
  2733. }while(I2C_Trials++ < Trials);
  2734. hi2c->State = HAL_I2C_STATE_READY;
  2735. /* Process Unlocked */
  2736. __HAL_UNLOCK(hi2c);
  2737. return HAL_ERROR;
  2738. }
  2739. else
  2740. {
  2741. return HAL_BUSY;
  2742. }
  2743. }
  2744. /**
  2745. * @brief This function handles I2C event interrupt request.
  2746. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2747. * the configuration information for the specified I2C.
  2748. * @retval None
  2749. */
  2750. void HAL_I2C_EV_IRQHandler(I2C_HandleTypeDef *hi2c)
  2751. {
  2752. uint32_t sr2itflags = READ_REG(hi2c->Instance->SR2);
  2753. uint32_t sr1itflags = READ_REG(hi2c->Instance->SR1);
  2754. uint32_t itsources = READ_REG(hi2c->Instance->CR2);
  2755. /* Master or Memory mode selected */
  2756. if((hi2c->Mode == HAL_I2C_MODE_MASTER) || \
  2757. (hi2c->Mode == HAL_I2C_MODE_MEM))
  2758. {
  2759. /* SB Set ----------------------------------------------------------------*/
  2760. if(((sr1itflags & I2C_FLAG_SB) != RESET) && ((itsources & I2C_IT_EVT) != RESET))
  2761. {
  2762. I2C_Master_SB(hi2c);
  2763. }
  2764. /* ADD10 Set -------------------------------------------------------------*/
  2765. else if(((sr1itflags & I2C_FLAG_ADD10) != RESET) && ((itsources & I2C_IT_EVT) != RESET))
  2766. {
  2767. I2C_Master_ADD10(hi2c);
  2768. }
  2769. /* ADDR Set --------------------------------------------------------------*/
  2770. else if(((sr1itflags & I2C_FLAG_ADDR) != RESET) && ((itsources & I2C_IT_EVT) != RESET))
  2771. {
  2772. I2C_Master_ADDR(hi2c);
  2773. }
  2774. /* I2C in mode Transmitter -----------------------------------------------*/
  2775. if((hi2c->EventCount == 0U) && ((sr2itflags & I2C_FLAG_TRA) != RESET))
  2776. {
  2777. /* TXE set and BTF reset -----------------------------------------------*/
  2778. if(((sr1itflags & I2C_FLAG_TXE) != RESET) && ((itsources & I2C_IT_BUF) != RESET) && ((sr1itflags & I2C_FLAG_BTF) == RESET))
  2779. {
  2780. I2C_MasterTransmit_TXE(hi2c);
  2781. }
  2782. /* BTF set -------------------------------------------------------------*/
  2783. else if(((sr1itflags & I2C_FLAG_BTF) != RESET) && ((itsources & I2C_IT_EVT) != RESET))
  2784. {
  2785. I2C_MasterTransmit_BTF(hi2c);
  2786. }
  2787. }
  2788. /* I2C in mode Receiver --------------------------------------------------*/
  2789. else
  2790. {
  2791. /* RXNE set and BTF reset -----------------------------------------------*/
  2792. if(((sr1itflags & I2C_FLAG_RXNE) != RESET) && ((itsources & I2C_IT_BUF) != RESET) && ((sr1itflags & I2C_FLAG_BTF) == RESET))
  2793. {
  2794. I2C_MasterReceive_RXNE(hi2c);
  2795. }
  2796. /* BTF set -------------------------------------------------------------*/
  2797. else if(((sr1itflags & I2C_FLAG_BTF) != RESET) && ((itsources & I2C_IT_EVT) != RESET))
  2798. {
  2799. I2C_MasterReceive_BTF(hi2c);
  2800. }
  2801. }
  2802. }
  2803. /* Slave mode selected */
  2804. else
  2805. {
  2806. /* ADDR set --------------------------------------------------------------*/
  2807. if(((sr1itflags & I2C_FLAG_ADDR) != RESET) && ((itsources & I2C_IT_EVT) != RESET))
  2808. {
  2809. I2C_Slave_ADDR(hi2c);
  2810. }
  2811. /* STOPF set --------------------------------------------------------------*/
  2812. else if(((sr1itflags & I2C_FLAG_STOPF) != RESET) && ((itsources & I2C_IT_EVT) != RESET))
  2813. {
  2814. I2C_Slave_STOPF(hi2c);
  2815. }
  2816. /* I2C in mode Transmitter -----------------------------------------------*/
  2817. else if((sr2itflags & I2C_FLAG_TRA) != RESET)
  2818. {
  2819. /* TXE set and BTF reset -----------------------------------------------*/
  2820. if(((sr1itflags & I2C_FLAG_TXE) != RESET) && ((itsources & I2C_IT_BUF) != RESET) && ((sr1itflags & I2C_FLAG_BTF) == RESET))
  2821. {
  2822. I2C_SlaveTransmit_TXE(hi2c);
  2823. }
  2824. /* BTF set -------------------------------------------------------------*/
  2825. else if(((sr1itflags & I2C_FLAG_BTF) != RESET) && ((itsources & I2C_IT_EVT) != RESET))
  2826. {
  2827. I2C_SlaveTransmit_BTF(hi2c);
  2828. }
  2829. }
  2830. /* I2C in mode Receiver --------------------------------------------------*/
  2831. else
  2832. {
  2833. /* RXNE set and BTF reset ----------------------------------------------*/
  2834. if(((sr1itflags & I2C_FLAG_RXNE) != RESET) && ((itsources & I2C_IT_BUF) != RESET) && ((sr1itflags & I2C_FLAG_BTF) == RESET))
  2835. {
  2836. I2C_SlaveReceive_RXNE(hi2c);
  2837. }
  2838. /* BTF set -------------------------------------------------------------*/
  2839. else if(((sr1itflags & I2C_FLAG_BTF) != RESET) && ((itsources & I2C_IT_EVT) != RESET))
  2840. {
  2841. I2C_SlaveReceive_BTF(hi2c);
  2842. }
  2843. }
  2844. }
  2845. }
  2846. /**
  2847. * @brief This function handles I2C error interrupt request.
  2848. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2849. * the configuration information for the specified I2C.
  2850. * @retval None
  2851. */
  2852. void HAL_I2C_ER_IRQHandler(I2C_HandleTypeDef *hi2c)
  2853. {
  2854. uint32_t sr1itflags = READ_REG(hi2c->Instance->SR1);
  2855. uint32_t itsources = READ_REG(hi2c->Instance->CR2);
  2856. /* I2C Bus error interrupt occurred ----------------------------------------*/
  2857. if(((sr1itflags & I2C_FLAG_BERR) != RESET) && ((itsources & I2C_IT_ERR) != RESET))
  2858. {
  2859. hi2c->ErrorCode |= HAL_I2C_ERROR_BERR;
  2860. /* Clear BERR flag */
  2861. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_BERR);
  2862. }
  2863. /* I2C Arbitration Loss error interrupt occurred ---------------------------*/
  2864. if(((sr1itflags & I2C_FLAG_ARLO) != RESET) && ((itsources & I2C_IT_ERR) != RESET))
  2865. {
  2866. hi2c->ErrorCode |= HAL_I2C_ERROR_ARLO;
  2867. /* Clear ARLO flag */
  2868. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ARLO);
  2869. }
  2870. /* I2C Acknowledge failure error interrupt occurred ------------------------*/
  2871. if(((sr1itflags & I2C_FLAG_AF) != RESET) && ((itsources & I2C_IT_ERR) != RESET))
  2872. {
  2873. if((hi2c->Mode == HAL_I2C_MODE_SLAVE) && \
  2874. (hi2c->XferCount == 0U) && \
  2875. ((hi2c->State == HAL_I2C_STATE_BUSY_TX) || (hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN) || \
  2876. ((hi2c->State == HAL_I2C_STATE_LISTEN) && (hi2c->PreviousState == HAL_I2C_STATE_BUSY_TX))))
  2877. {
  2878. I2C_Slave_AF(hi2c);
  2879. }
  2880. else
  2881. {
  2882. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  2883. /* Do not generate a STOP in case of Slave receive non acknowledge during transfer (mean not at the end of transfer) */
  2884. if(hi2c->Mode == HAL_I2C_MODE_MASTER)
  2885. {
  2886. /* Generate Stop */
  2887. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  2888. }
  2889. /* Clear AF flag */
  2890. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  2891. }
  2892. }
  2893. /* I2C Over-Run/Under-Run interrupt occurred -------------------------------*/
  2894. if(((sr1itflags & I2C_FLAG_OVR) != RESET) && ((itsources & I2C_IT_ERR) != RESET))
  2895. {
  2896. hi2c->ErrorCode |= HAL_I2C_ERROR_OVR;
  2897. /* Clear OVR flag */
  2898. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_OVR);
  2899. }
  2900. /* Call the Error Callback in case of Error detected -----------------------*/
  2901. if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  2902. {
  2903. I2C_ITError(hi2c);
  2904. }
  2905. }
  2906. /**
  2907. * @brief Master Tx Transfer completed callback.
  2908. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2909. * the configuration information for the specified I2C.
  2910. * @retval None
  2911. */
  2912. __weak void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c)
  2913. {
  2914. /* Prevent unused argument(s) compilation warning */
  2915. UNUSED(hi2c);
  2916. /* NOTE : This function should not be modified, when the callback is needed,
  2917. the HAL_I2C_MasterTxCpltCallback can be implemented in the user file
  2918. */
  2919. }
  2920. /**
  2921. * @brief Master Rx Transfer completed callback.
  2922. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2923. * the configuration information for the specified I2C.
  2924. * @retval None
  2925. */
  2926. __weak void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c)
  2927. {
  2928. /* Prevent unused argument(s) compilation warning */
  2929. UNUSED(hi2c);
  2930. /* NOTE : This function should not be modified, when the callback is needed,
  2931. the HAL_I2C_MasterRxCpltCallback can be implemented in the user file
  2932. */
  2933. }
  2934. /** @brief Slave Tx Transfer completed callback.
  2935. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2936. * the configuration information for the specified I2C.
  2937. * @retval None
  2938. */
  2939. __weak void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *hi2c)
  2940. {
  2941. /* Prevent unused argument(s) compilation warning */
  2942. UNUSED(hi2c);
  2943. /* NOTE : This function should not be modified, when the callback is needed,
  2944. the HAL_I2C_SlaveTxCpltCallback can be implemented in the user file
  2945. */
  2946. }
  2947. /**
  2948. * @brief Slave Rx Transfer completed callback.
  2949. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2950. * the configuration information for the specified I2C.
  2951. * @retval None
  2952. */
  2953. __weak void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c)
  2954. {
  2955. /* Prevent unused argument(s) compilation warning */
  2956. UNUSED(hi2c);
  2957. /* NOTE : This function should not be modified, when the callback is needed,
  2958. the HAL_I2C_SlaveRxCpltCallback can be implemented in the user file
  2959. */
  2960. }
  2961. /**
  2962. * @brief Slave Address Match callback.
  2963. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2964. * the configuration information for the specified I2C.
  2965. * @param TransferDirection Master request Transfer Direction (Write/Read), value of @ref I2C_XferDirection_definition
  2966. * @param AddrMatchCode Address Match Code
  2967. * @retval None
  2968. */
  2969. __weak void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode)
  2970. {
  2971. /* Prevent unused argument(s) compilation warning */
  2972. UNUSED(hi2c);
  2973. UNUSED(TransferDirection);
  2974. UNUSED(AddrMatchCode);
  2975. /* NOTE : This function should not be modified, when the callback is needed,
  2976. the HAL_I2C_AddrCallback can be implemented in the user file
  2977. */
  2978. }
  2979. /**
  2980. * @brief Listen Complete callback.
  2981. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2982. * the configuration information for the specified I2C.
  2983. * @retval None
  2984. */
  2985. __weak void HAL_I2C_ListenCpltCallback(I2C_HandleTypeDef *hi2c)
  2986. {
  2987. /* Prevent unused argument(s) compilation warning */
  2988. UNUSED(hi2c);
  2989. /* NOTE : This function should not be modified, when the callback is needed,
  2990. the HAL_I2C_ListenCpltCallback can be implemented in the user file
  2991. */
  2992. }
  2993. /**
  2994. * @brief Memory Tx Transfer completed callback.
  2995. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2996. * the configuration information for the specified I2C.
  2997. * @retval None
  2998. */
  2999. __weak void HAL_I2C_MemTxCpltCallback(I2C_HandleTypeDef *hi2c)
  3000. {
  3001. /* Prevent unused argument(s) compilation warning */
  3002. UNUSED(hi2c);
  3003. /* NOTE : This function should not be modified, when the callback is needed,
  3004. the HAL_I2C_MemTxCpltCallback can be implemented in the user file
  3005. */
  3006. }
  3007. /**
  3008. * @brief Memory Rx Transfer completed callback.
  3009. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3010. * the configuration information for the specified I2C.
  3011. * @retval None
  3012. */
  3013. __weak void HAL_I2C_MemRxCpltCallback(I2C_HandleTypeDef *hi2c)
  3014. {
  3015. /* Prevent unused argument(s) compilation warning */
  3016. UNUSED(hi2c);
  3017. /* NOTE : This function should not be modified, when the callback is needed,
  3018. the HAL_I2C_MemRxCpltCallback can be implemented in the user file
  3019. */
  3020. }
  3021. /**
  3022. * @brief I2C error callback.
  3023. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3024. * the configuration information for the specified I2C.
  3025. * @retval None
  3026. */
  3027. __weak void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c)
  3028. {
  3029. /* Prevent unused argument(s) compilation warning */
  3030. UNUSED(hi2c);
  3031. /* NOTE : This function should not be modified, when the callback is needed,
  3032. the HAL_I2C_ErrorCallback can be implemented in the user file
  3033. */
  3034. }
  3035. /**
  3036. * @brief I2C abort callback.
  3037. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3038. * the configuration information for the specified I2C.
  3039. * @retval None
  3040. */
  3041. __weak void HAL_I2C_AbortCpltCallback(I2C_HandleTypeDef *hi2c)
  3042. {
  3043. /* Prevent unused argument(s) compilation warning */
  3044. UNUSED(hi2c);
  3045. /* NOTE : This function should not be modified, when the callback is needed,
  3046. the HAL_I2C_AbortCpltCallback could be implemented in the user file
  3047. */
  3048. }
  3049. /**
  3050. * @}
  3051. */
  3052. /** @defgroup I2C_Exported_Functions_Group3 Peripheral State, Mode and Error functions
  3053. * @brief Peripheral State and Errors functions
  3054. *
  3055. @verbatim
  3056. ===============================================================================
  3057. ##### Peripheral State, Mode and Error functions #####
  3058. ===============================================================================
  3059. [..]
  3060. This subsection permits to get in run-time the status of the peripheral
  3061. and the data flow.
  3062. @endverbatim
  3063. * @{
  3064. */
  3065. /**
  3066. * @brief Return the I2C handle state.
  3067. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3068. * the configuration information for the specified I2C.
  3069. * @retval HAL state
  3070. */
  3071. HAL_I2C_StateTypeDef HAL_I2C_GetState(I2C_HandleTypeDef *hi2c)
  3072. {
  3073. /* Return I2C handle state */
  3074. return hi2c->State;
  3075. }
  3076. /**
  3077. * @brief Return the I2C Master, Slave, Memory or no mode.
  3078. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3079. * the configuration information for I2C module
  3080. * @retval HAL mode
  3081. */
  3082. HAL_I2C_ModeTypeDef HAL_I2C_GetMode(I2C_HandleTypeDef *hi2c)
  3083. {
  3084. return hi2c->Mode;
  3085. }
  3086. /**
  3087. * @brief Return the I2C error code.
  3088. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3089. * the configuration information for the specified I2C.
  3090. * @retval I2C Error Code
  3091. */
  3092. uint32_t HAL_I2C_GetError(I2C_HandleTypeDef *hi2c)
  3093. {
  3094. return hi2c->ErrorCode;
  3095. }
  3096. /**
  3097. * @}
  3098. */
  3099. /**
  3100. * @}
  3101. */
  3102. /** @addtogroup I2C_Private_Functions
  3103. * @{
  3104. */
  3105. /**
  3106. * @brief Handle TXE flag for Master
  3107. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3108. * the configuration information for I2C module
  3109. * @retval HAL status
  3110. */
  3111. static HAL_StatusTypeDef I2C_MasterTransmit_TXE(I2C_HandleTypeDef *hi2c)
  3112. {
  3113. if((hi2c->XferSize == 0U) && (hi2c->State == HAL_I2C_STATE_BUSY_TX))
  3114. {
  3115. /* Call TxCpltCallback() directly if no stop mode is set */
  3116. if((hi2c->XferOptions != I2C_FIRST_AND_LAST_FRAME) && (hi2c->XferOptions != I2C_LAST_FRAME) && (hi2c->XferOptions != I2C_NO_OPTION_FRAME))
  3117. {
  3118. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  3119. hi2c->PreviousState = I2C_STATE_MASTER_BUSY_TX;
  3120. hi2c->Mode = HAL_I2C_MODE_NONE;
  3121. hi2c->State = HAL_I2C_STATE_READY;
  3122. HAL_I2C_MasterTxCpltCallback(hi2c);
  3123. }
  3124. else /* Generate Stop condition then Call TxCpltCallback() */
  3125. {
  3126. /* Disable EVT, BUF and ERR interrupt */
  3127. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  3128. /* Generate Stop */
  3129. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  3130. hi2c->PreviousState = I2C_STATE_NONE;
  3131. hi2c->State = HAL_I2C_STATE_READY;
  3132. if(hi2c->Mode == HAL_I2C_MODE_MEM)
  3133. {
  3134. hi2c->Mode = HAL_I2C_MODE_NONE;
  3135. HAL_I2C_MemTxCpltCallback(hi2c);
  3136. }
  3137. else
  3138. {
  3139. hi2c->Mode = HAL_I2C_MODE_NONE;
  3140. HAL_I2C_MasterTxCpltCallback(hi2c);
  3141. }
  3142. }
  3143. }
  3144. else if((hi2c->State == HAL_I2C_STATE_BUSY_TX) || \
  3145. ((hi2c->Mode == HAL_I2C_MODE_MEM) && (hi2c->State == HAL_I2C_STATE_BUSY_RX)))
  3146. {
  3147. if(hi2c->XferCount == 0U)
  3148. {
  3149. /* Disable BUF interrupt */
  3150. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_BUF);
  3151. }
  3152. else
  3153. {
  3154. /* Write data to DR */
  3155. hi2c->Instance->DR = (*hi2c->pBuffPtr++);
  3156. hi2c->XferCount--;
  3157. }
  3158. }
  3159. return HAL_OK;
  3160. }
  3161. /**
  3162. * @brief Handle BTF flag for Master transmitter
  3163. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3164. * the configuration information for I2C module
  3165. * @retval HAL status
  3166. */
  3167. static HAL_StatusTypeDef I2C_MasterTransmit_BTF(I2C_HandleTypeDef *hi2c)
  3168. {
  3169. if(hi2c->State == HAL_I2C_STATE_BUSY_TX)
  3170. {
  3171. if(hi2c->XferCount != 0U)
  3172. {
  3173. /* Write data to DR */
  3174. hi2c->Instance->DR = (*hi2c->pBuffPtr++);
  3175. hi2c->XferCount--;
  3176. }
  3177. else
  3178. {
  3179. /* Call TxCpltCallback() directly if no stop mode is set */
  3180. if((hi2c->XferOptions != I2C_FIRST_AND_LAST_FRAME) && (hi2c->XferOptions != I2C_LAST_FRAME) && (hi2c->XferOptions != I2C_NO_OPTION_FRAME))
  3181. {
  3182. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  3183. hi2c->PreviousState = I2C_STATE_MASTER_BUSY_TX;
  3184. hi2c->Mode = HAL_I2C_MODE_NONE;
  3185. hi2c->State = HAL_I2C_STATE_READY;
  3186. HAL_I2C_MasterTxCpltCallback(hi2c);
  3187. }
  3188. else /* Generate Stop condition then Call TxCpltCallback() */
  3189. {
  3190. /* Disable EVT, BUF and ERR interrupt */
  3191. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  3192. /* Generate Stop */
  3193. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  3194. hi2c->PreviousState = I2C_STATE_NONE;
  3195. hi2c->State = HAL_I2C_STATE_READY;
  3196. if(hi2c->Mode == HAL_I2C_MODE_MEM)
  3197. {
  3198. hi2c->Mode = HAL_I2C_MODE_NONE;
  3199. HAL_I2C_MemTxCpltCallback(hi2c);
  3200. }
  3201. else
  3202. {
  3203. hi2c->Mode = HAL_I2C_MODE_NONE;
  3204. HAL_I2C_MasterTxCpltCallback(hi2c);
  3205. }
  3206. }
  3207. }
  3208. }
  3209. return HAL_OK;
  3210. }
  3211. /**
  3212. * @brief Handle RXNE flag for Master
  3213. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3214. * the configuration information for I2C module
  3215. * @retval HAL status
  3216. */
  3217. static HAL_StatusTypeDef I2C_MasterReceive_RXNE(I2C_HandleTypeDef *hi2c)
  3218. {
  3219. if(hi2c->State == HAL_I2C_STATE_BUSY_RX)
  3220. {
  3221. if(hi2c->XferCount > 3U)
  3222. {
  3223. /* Read data from DR */
  3224. (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
  3225. hi2c->XferCount--;
  3226. }
  3227. else if((hi2c->XferCount == 2U) || (hi2c->XferCount == 3U))
  3228. {
  3229. if(hi2c->XferOptions != I2C_NEXT_FRAME)
  3230. {
  3231. /* Disable Acknowledge */
  3232. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  3233. /* Enable Pos */
  3234. SET_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  3235. }
  3236. else
  3237. {
  3238. /* Enable Acknowledge */
  3239. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  3240. }
  3241. /* Disable BUF interrupt */
  3242. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_BUF);
  3243. }
  3244. else
  3245. {
  3246. if(hi2c->XferOptions != I2C_NEXT_FRAME)
  3247. {
  3248. /* Disable Acknowledge */
  3249. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  3250. }
  3251. else
  3252. {
  3253. /* Enable Acknowledge */
  3254. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  3255. }
  3256. /* Disable EVT, BUF and ERR interrupt */
  3257. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  3258. /* Read data from DR */
  3259. (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
  3260. hi2c->XferCount--;
  3261. hi2c->State = HAL_I2C_STATE_READY;
  3262. if(hi2c->Mode == HAL_I2C_MODE_MEM)
  3263. {
  3264. hi2c->PreviousState = I2C_STATE_NONE;
  3265. hi2c->Mode = HAL_I2C_MODE_NONE;
  3266. HAL_I2C_MemRxCpltCallback(hi2c);
  3267. }
  3268. else
  3269. {
  3270. hi2c->PreviousState = I2C_STATE_MASTER_BUSY_RX;
  3271. hi2c->Mode = HAL_I2C_MODE_NONE;
  3272. HAL_I2C_MasterRxCpltCallback(hi2c);
  3273. }
  3274. }
  3275. }
  3276. return HAL_OK;
  3277. }
  3278. /**
  3279. * @brief Handle BTF flag for Master receiver
  3280. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3281. * the configuration information for I2C module
  3282. * @retval HAL status
  3283. */
  3284. static HAL_StatusTypeDef I2C_MasterReceive_BTF(I2C_HandleTypeDef *hi2c)
  3285. {
  3286. if(hi2c->XferCount == 3U)
  3287. {
  3288. if((hi2c->XferOptions == I2C_FIRST_AND_LAST_FRAME) || (hi2c->XferOptions == I2C_LAST_FRAME) || (hi2c->XferOptions == I2C_NO_OPTION_FRAME))
  3289. {
  3290. /* Disable Acknowledge */
  3291. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  3292. }
  3293. /* Read data from DR */
  3294. (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
  3295. hi2c->XferCount--;
  3296. }
  3297. else if(hi2c->XferCount == 2U)
  3298. {
  3299. /* Prepare next transfer or stop current transfer */
  3300. if((hi2c->XferOptions != I2C_FIRST_AND_LAST_FRAME) && (hi2c->XferOptions != I2C_LAST_FRAME) && (hi2c->XferOptions != I2C_NO_OPTION_FRAME))
  3301. {
  3302. if(hi2c->XferOptions != I2C_NEXT_FRAME)
  3303. {
  3304. /* Disable Acknowledge */
  3305. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  3306. }
  3307. else
  3308. {
  3309. /* Enable Acknowledge */
  3310. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  3311. }
  3312. }
  3313. else
  3314. {
  3315. /* Generate Stop */
  3316. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  3317. }
  3318. /* Read data from DR */
  3319. (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
  3320. hi2c->XferCount--;
  3321. /* Read data from DR */
  3322. (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
  3323. hi2c->XferCount--;
  3324. /* Disable EVT and ERR interrupt */
  3325. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
  3326. hi2c->State = HAL_I2C_STATE_READY;
  3327. if(hi2c->Mode == HAL_I2C_MODE_MEM)
  3328. {
  3329. hi2c->PreviousState = I2C_STATE_NONE;
  3330. hi2c->Mode = HAL_I2C_MODE_NONE;
  3331. HAL_I2C_MemRxCpltCallback(hi2c);
  3332. }
  3333. else
  3334. {
  3335. hi2c->PreviousState = I2C_STATE_MASTER_BUSY_RX;
  3336. hi2c->Mode = HAL_I2C_MODE_NONE;
  3337. HAL_I2C_MasterRxCpltCallback(hi2c);
  3338. }
  3339. }
  3340. else
  3341. {
  3342. /* Read data from DR */
  3343. (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
  3344. hi2c->XferCount--;
  3345. }
  3346. return HAL_OK;
  3347. }
  3348. /**
  3349. * @brief Handle SB flag for Master
  3350. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3351. * the configuration information for I2C module
  3352. * @retval HAL status
  3353. */
  3354. static HAL_StatusTypeDef I2C_Master_SB(I2C_HandleTypeDef *hi2c)
  3355. {
  3356. if(hi2c->Mode == HAL_I2C_MODE_MEM)
  3357. {
  3358. if(hi2c->EventCount == 0U)
  3359. {
  3360. /* Send slave address */
  3361. hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(hi2c->Devaddress);
  3362. }
  3363. else
  3364. {
  3365. hi2c->Instance->DR = I2C_7BIT_ADD_READ(hi2c->Devaddress);
  3366. }
  3367. }
  3368. else
  3369. {
  3370. if(hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_7BIT)
  3371. {
  3372. /* Send slave 7 Bits address */
  3373. if(hi2c->State == HAL_I2C_STATE_BUSY_TX)
  3374. {
  3375. hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(hi2c->Devaddress);
  3376. }
  3377. else
  3378. {
  3379. hi2c->Instance->DR = I2C_7BIT_ADD_READ(hi2c->Devaddress);
  3380. }
  3381. }
  3382. else
  3383. {
  3384. if(hi2c->EventCount == 0U)
  3385. {
  3386. /* Send header of slave address */
  3387. hi2c->Instance->DR = I2C_10BIT_HEADER_WRITE(hi2c->Devaddress);
  3388. }
  3389. else if(hi2c->EventCount == 1U)
  3390. {
  3391. /* Send header of slave address */
  3392. hi2c->Instance->DR = I2C_10BIT_HEADER_READ(hi2c->Devaddress);
  3393. }
  3394. }
  3395. }
  3396. return HAL_OK;
  3397. }
  3398. /**
  3399. * @brief Handle ADD10 flag for Master
  3400. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3401. * the configuration information for I2C module
  3402. * @retval HAL status
  3403. */
  3404. static HAL_StatusTypeDef I2C_Master_ADD10(I2C_HandleTypeDef *hi2c)
  3405. {
  3406. /* Send slave address */
  3407. hi2c->Instance->DR = I2C_10BIT_ADDRESS(hi2c->Devaddress);
  3408. return HAL_OK;
  3409. }
  3410. /**
  3411. * @brief Handle ADDR flag for Master
  3412. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3413. * the configuration information for I2C module
  3414. * @retval HAL status
  3415. */
  3416. static HAL_StatusTypeDef I2C_Master_ADDR(I2C_HandleTypeDef *hi2c)
  3417. {
  3418. if(hi2c->State == HAL_I2C_STATE_BUSY_RX)
  3419. {
  3420. if((hi2c->EventCount == 0U) && (hi2c->Mode == HAL_I2C_MODE_MEM))
  3421. {
  3422. /* Clear ADDR flag */
  3423. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  3424. }
  3425. else if((hi2c->EventCount == 0U) && (hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT))
  3426. {
  3427. /* Clear ADDR flag */
  3428. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  3429. /* Generate Restart */
  3430. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  3431. hi2c->EventCount++;
  3432. }
  3433. else
  3434. {
  3435. if(hi2c->XferCount == 0U)
  3436. {
  3437. /* Clear ADDR flag */
  3438. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  3439. /* Generate Stop */
  3440. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  3441. }
  3442. else if(hi2c->XferCount == 1U)
  3443. {
  3444. if(hi2c->XferOptions == I2C_NO_OPTION_FRAME)
  3445. {
  3446. /* Disable Acknowledge */
  3447. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  3448. if((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN)
  3449. {
  3450. /* Disable Acknowledge */
  3451. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  3452. /* Clear ADDR flag */
  3453. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  3454. }
  3455. else
  3456. {
  3457. /* Clear ADDR flag */
  3458. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  3459. /* Generate Stop */
  3460. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  3461. }
  3462. }
  3463. /* Prepare next transfer or stop current transfer */
  3464. else if((hi2c->XferOptions != I2C_FIRST_AND_LAST_FRAME) && (hi2c->XferOptions != I2C_LAST_FRAME) \
  3465. && (hi2c->PreviousState != I2C_STATE_MASTER_BUSY_RX))
  3466. {
  3467. if(hi2c->XferOptions != I2C_NEXT_FRAME)
  3468. {
  3469. /* Disable Acknowledge */
  3470. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  3471. }
  3472. else
  3473. {
  3474. /* Enable Acknowledge */
  3475. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  3476. }
  3477. /* Clear ADDR flag */
  3478. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  3479. }
  3480. else
  3481. {
  3482. /* Disable Acknowledge */
  3483. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  3484. /* Clear ADDR flag */
  3485. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  3486. /* Generate Stop */
  3487. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  3488. }
  3489. }
  3490. else if(hi2c->XferCount == 2U)
  3491. {
  3492. if(hi2c->XferOptions != I2C_NEXT_FRAME)
  3493. {
  3494. /* Disable Acknowledge */
  3495. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  3496. /* Enable Pos */
  3497. SET_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  3498. }
  3499. else
  3500. {
  3501. /* Enable Acknowledge */
  3502. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  3503. }
  3504. if((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN)
  3505. {
  3506. /* Enable Last DMA bit */
  3507. SET_BIT(hi2c->Instance->CR2, I2C_CR2_LAST);
  3508. }
  3509. /* Clear ADDR flag */
  3510. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  3511. }
  3512. else
  3513. {
  3514. /* Enable Acknowledge */
  3515. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  3516. if((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN)
  3517. {
  3518. /* Enable Last DMA bit */
  3519. SET_BIT(hi2c->Instance->CR2, I2C_CR2_LAST);
  3520. }
  3521. /* Clear ADDR flag */
  3522. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  3523. }
  3524. /* Reset Event counter */
  3525. hi2c->EventCount = 0U;
  3526. }
  3527. }
  3528. else
  3529. {
  3530. /* Clear ADDR flag */
  3531. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  3532. }
  3533. return HAL_OK;
  3534. }
  3535. /**
  3536. * @brief Handle TXE flag for Slave
  3537. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3538. * the configuration information for I2C module
  3539. * @retval HAL status
  3540. */
  3541. static HAL_StatusTypeDef I2C_SlaveTransmit_TXE(I2C_HandleTypeDef *hi2c)
  3542. {
  3543. if(hi2c->XferCount != 0U)
  3544. {
  3545. /* Write data to DR */
  3546. hi2c->Instance->DR = (*hi2c->pBuffPtr++);
  3547. hi2c->XferCount--;
  3548. if((hi2c->XferCount == 0U) && (hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN))
  3549. {
  3550. /* Last Byte is received, disable Interrupt */
  3551. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_BUF);
  3552. /* Set state at HAL_I2C_STATE_LISTEN */
  3553. hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_TX;
  3554. hi2c->State = HAL_I2C_STATE_LISTEN;
  3555. /* Call the Tx complete callback to inform upper layer of the end of receive process */
  3556. HAL_I2C_SlaveTxCpltCallback(hi2c);
  3557. }
  3558. }
  3559. return HAL_OK;
  3560. }
  3561. /**
  3562. * @brief Handle BTF flag for Slave transmitter
  3563. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3564. * the configuration information for I2C module
  3565. * @retval HAL status
  3566. */
  3567. static HAL_StatusTypeDef I2C_SlaveTransmit_BTF(I2C_HandleTypeDef *hi2c)
  3568. {
  3569. if(hi2c->XferCount != 0U)
  3570. {
  3571. /* Write data to DR */
  3572. hi2c->Instance->DR = (*hi2c->pBuffPtr++);
  3573. hi2c->XferCount--;
  3574. }
  3575. return HAL_OK;
  3576. }
  3577. /**
  3578. * @brief Handle RXNE flag for Slave
  3579. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3580. * the configuration information for I2C module
  3581. * @retval HAL status
  3582. */
  3583. static HAL_StatusTypeDef I2C_SlaveReceive_RXNE(I2C_HandleTypeDef *hi2c)
  3584. {
  3585. if(hi2c->XferCount != 0U)
  3586. {
  3587. /* Read data from DR */
  3588. (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
  3589. hi2c->XferCount--;
  3590. if((hi2c->XferCount == 0U) && (hi2c->State == HAL_I2C_STATE_BUSY_RX_LISTEN))
  3591. {
  3592. /* Last Byte is received, disable Interrupt */
  3593. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_BUF);
  3594. /* Set state at HAL_I2C_STATE_LISTEN */
  3595. hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_RX;
  3596. hi2c->State = HAL_I2C_STATE_LISTEN;
  3597. /* Call the Rx complete callback to inform upper layer of the end of receive process */
  3598. HAL_I2C_SlaveRxCpltCallback(hi2c);
  3599. }
  3600. }
  3601. return HAL_OK;
  3602. }
  3603. /**
  3604. * @brief Handle BTF flag for Slave receiver
  3605. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3606. * the configuration information for I2C module
  3607. * @retval HAL status
  3608. */
  3609. static HAL_StatusTypeDef I2C_SlaveReceive_BTF(I2C_HandleTypeDef *hi2c)
  3610. {
  3611. if(hi2c->XferCount != 0U)
  3612. {
  3613. /* Read data from DR */
  3614. (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
  3615. hi2c->XferCount--;
  3616. }
  3617. return HAL_OK;
  3618. }
  3619. /**
  3620. * @brief Handle ADD flag for Slave
  3621. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3622. * the configuration information for I2C module
  3623. * @retval HAL status
  3624. */
  3625. static HAL_StatusTypeDef I2C_Slave_ADDR(I2C_HandleTypeDef *hi2c)
  3626. {
  3627. uint8_t TransferDirection = I2C_DIRECTION_RECEIVE;
  3628. uint16_t SlaveAddrCode = 0U;
  3629. /* Transfer Direction requested by Master */
  3630. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TRA) == RESET)
  3631. {
  3632. TransferDirection = I2C_DIRECTION_TRANSMIT;
  3633. }
  3634. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_DUALF) == RESET)
  3635. {
  3636. SlaveAddrCode = hi2c->Init.OwnAddress1;
  3637. }
  3638. else
  3639. {
  3640. SlaveAddrCode = hi2c->Init.OwnAddress2;
  3641. }
  3642. /* Call Slave Addr callback */
  3643. HAL_I2C_AddrCallback(hi2c, TransferDirection, SlaveAddrCode);
  3644. return HAL_OK;
  3645. }
  3646. /**
  3647. * @brief Handle STOPF flag for Slave
  3648. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3649. * the configuration information for I2C module
  3650. * @retval HAL status
  3651. */
  3652. static HAL_StatusTypeDef I2C_Slave_STOPF(I2C_HandleTypeDef *hi2c)
  3653. {
  3654. /* Disable EVT, BUF and ERR interrupt */
  3655. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  3656. /* Clear STOPF flag */
  3657. __HAL_I2C_CLEAR_STOPFLAG(hi2c);
  3658. /* Disable Acknowledge */
  3659. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  3660. /* If a DMA is ongoing, Update handle size context */
  3661. if((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN)
  3662. {
  3663. if((hi2c->State == HAL_I2C_STATE_BUSY_RX) || (hi2c->State == HAL_I2C_STATE_BUSY_RX_LISTEN))
  3664. {
  3665. hi2c->XferCount = __HAL_DMA_GET_COUNTER(hi2c->hdmarx);
  3666. }
  3667. else
  3668. {
  3669. hi2c->XferCount = __HAL_DMA_GET_COUNTER(hi2c->hdmatx);
  3670. }
  3671. }
  3672. /* All data are not transferred, so set error code accordingly */
  3673. if(hi2c->XferCount != 0U)
  3674. {
  3675. /* Store Last receive data if any */
  3676. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET)
  3677. {
  3678. /* Read data from DR */
  3679. (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
  3680. hi2c->XferCount--;
  3681. }
  3682. /* Store Last receive data if any */
  3683. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET)
  3684. {
  3685. /* Read data from DR */
  3686. (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
  3687. hi2c->XferCount--;
  3688. }
  3689. /* Set ErrorCode corresponding to a Non-Acknowledge */
  3690. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  3691. }
  3692. if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  3693. {
  3694. /* Call the corresponding callback to inform upper layer of End of Transfer */
  3695. I2C_ITError(hi2c);
  3696. }
  3697. else
  3698. {
  3699. if((hi2c->State == HAL_I2C_STATE_LISTEN ) || (hi2c->State == HAL_I2C_STATE_BUSY_RX_LISTEN) || \
  3700. (hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN))
  3701. {
  3702. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  3703. hi2c->PreviousState = I2C_STATE_NONE;
  3704. hi2c->State = HAL_I2C_STATE_READY;
  3705. hi2c->Mode = HAL_I2C_MODE_NONE;
  3706. /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */
  3707. HAL_I2C_ListenCpltCallback(hi2c);
  3708. }
  3709. else
  3710. {
  3711. if((hi2c->PreviousState == I2C_STATE_SLAVE_BUSY_RX) || (hi2c->State == HAL_I2C_STATE_BUSY_RX))
  3712. {
  3713. hi2c->PreviousState = I2C_STATE_NONE;
  3714. hi2c->State = HAL_I2C_STATE_READY;
  3715. hi2c->Mode = HAL_I2C_MODE_NONE;
  3716. HAL_I2C_SlaveRxCpltCallback(hi2c);
  3717. }
  3718. }
  3719. }
  3720. return HAL_OK;
  3721. }
  3722. /**
  3723. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3724. * the configuration information for I2C module
  3725. * @retval HAL status
  3726. */
  3727. static HAL_StatusTypeDef I2C_Slave_AF(I2C_HandleTypeDef *hi2c)
  3728. {
  3729. if(((hi2c->XferOptions == I2C_FIRST_AND_LAST_FRAME) || (hi2c->XferOptions == I2C_LAST_FRAME)) && \
  3730. (hi2c->State == HAL_I2C_STATE_LISTEN))
  3731. {
  3732. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  3733. /* Disable EVT, BUF and ERR interrupt */
  3734. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  3735. /* Clear AF flag */
  3736. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  3737. /* Disable Acknowledge */
  3738. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  3739. hi2c->PreviousState = I2C_STATE_NONE;
  3740. hi2c->State = HAL_I2C_STATE_READY;
  3741. hi2c->Mode = HAL_I2C_MODE_NONE;
  3742. /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */
  3743. HAL_I2C_ListenCpltCallback(hi2c);
  3744. }
  3745. else if(hi2c->State == HAL_I2C_STATE_BUSY_TX)
  3746. {
  3747. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  3748. hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_TX;
  3749. hi2c->State = HAL_I2C_STATE_READY;
  3750. hi2c->Mode = HAL_I2C_MODE_NONE;
  3751. /* Disable EVT, BUF and ERR interrupt */
  3752. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  3753. /* Clear AF flag */
  3754. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  3755. /* Disable Acknowledge */
  3756. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  3757. HAL_I2C_SlaveTxCpltCallback(hi2c);
  3758. }
  3759. else
  3760. {
  3761. /* Clear AF flag only */
  3762. /* State Listen, but XferOptions == FIRST or NEXT */
  3763. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  3764. }
  3765. return HAL_OK;
  3766. }
  3767. /**
  3768. * @brief I2C interrupts error process
  3769. * @param hi2c I2C handle.
  3770. * @retval None
  3771. */
  3772. static void I2C_ITError(I2C_HandleTypeDef *hi2c)
  3773. {
  3774. /* Declaration of temporary variable to prevent undefined behavior of volatile usage */
  3775. uint32_t CurrentState = hi2c->State;
  3776. if((CurrentState == HAL_I2C_STATE_BUSY_TX_LISTEN) || (CurrentState == HAL_I2C_STATE_BUSY_RX_LISTEN))
  3777. {
  3778. /* keep HAL_I2C_STATE_LISTEN */
  3779. hi2c->PreviousState = I2C_STATE_NONE;
  3780. hi2c->State = HAL_I2C_STATE_LISTEN;
  3781. }
  3782. else
  3783. {
  3784. /* If state is an abort treatment on going, don't change state */
  3785. /* This change will be do later */
  3786. if((hi2c->State != HAL_I2C_STATE_ABORT) && ((hi2c->Instance->CR2 & I2C_CR2_DMAEN) != I2C_CR2_DMAEN))
  3787. {
  3788. hi2c->State = HAL_I2C_STATE_READY;
  3789. }
  3790. hi2c->PreviousState = I2C_STATE_NONE;
  3791. hi2c->Mode = HAL_I2C_MODE_NONE;
  3792. }
  3793. /* Disable Pos bit in I2C CR1 when error occurred in Master/Mem Receive IT Process */
  3794. hi2c->Instance->CR1 &= ~I2C_CR1_POS;
  3795. /* Abort DMA transfer */
  3796. if((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN)
  3797. {
  3798. hi2c->Instance->CR2 &= ~I2C_CR2_DMAEN;
  3799. if(hi2c->hdmatx->State != HAL_DMA_STATE_READY)
  3800. {
  3801. /* Set the DMA Abort callback :
  3802. will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
  3803. hi2c->hdmatx->XferAbortCallback = I2C_DMAAbort;
  3804. if(HAL_DMA_Abort_IT(hi2c->hdmatx) != HAL_OK)
  3805. {
  3806. /* Disable I2C peripheral to prevent dummy data in buffer */
  3807. __HAL_I2C_DISABLE(hi2c);
  3808. hi2c->State = HAL_I2C_STATE_READY;
  3809. /* Call Directly XferAbortCallback function in case of error */
  3810. hi2c->hdmatx->XferAbortCallback(hi2c->hdmatx);
  3811. }
  3812. }
  3813. else
  3814. {
  3815. /* Set the DMA Abort callback :
  3816. will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
  3817. hi2c->hdmarx->XferAbortCallback = I2C_DMAAbort;
  3818. if(HAL_DMA_Abort_IT(hi2c->hdmarx) != HAL_OK)
  3819. {
  3820. /* Store Last receive data if any */
  3821. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET)
  3822. {
  3823. /* Read data from DR */
  3824. (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
  3825. }
  3826. /* Disable I2C peripheral to prevent dummy data in buffer */
  3827. __HAL_I2C_DISABLE(hi2c);
  3828. hi2c->State = HAL_I2C_STATE_READY;
  3829. /* Call Directly hi2c->hdmarx->XferAbortCallback function in case of error */
  3830. hi2c->hdmarx->XferAbortCallback(hi2c->hdmarx);
  3831. }
  3832. }
  3833. }
  3834. else if(hi2c->State == HAL_I2C_STATE_ABORT)
  3835. {
  3836. hi2c->State = HAL_I2C_STATE_READY;
  3837. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  3838. /* Store Last receive data if any */
  3839. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET)
  3840. {
  3841. /* Read data from DR */
  3842. (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
  3843. }
  3844. /* Disable I2C peripheral to prevent dummy data in buffer */
  3845. __HAL_I2C_DISABLE(hi2c);
  3846. /* Call the corresponding callback to inform upper layer of End of Transfer */
  3847. HAL_I2C_AbortCpltCallback(hi2c);
  3848. }
  3849. else
  3850. {
  3851. /* Store Last receive data if any */
  3852. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET)
  3853. {
  3854. /* Read data from DR */
  3855. (*hi2c->pBuffPtr++) = hi2c->Instance->DR;
  3856. }
  3857. /* Call user error callback */
  3858. HAL_I2C_ErrorCallback(hi2c);
  3859. }
  3860. /* STOP Flag is not set after a NACK reception */
  3861. /* So may inform upper layer that listen phase is stopped */
  3862. /* during NACK error treatment */
  3863. if((hi2c->State == HAL_I2C_STATE_LISTEN) && ((hi2c->ErrorCode & HAL_I2C_ERROR_AF) == HAL_I2C_ERROR_AF))
  3864. {
  3865. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  3866. hi2c->PreviousState = I2C_STATE_NONE;
  3867. hi2c->State = HAL_I2C_STATE_READY;
  3868. hi2c->Mode = HAL_I2C_MODE_NONE;
  3869. /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */
  3870. HAL_I2C_ListenCpltCallback(hi2c);
  3871. }
  3872. }
  3873. /**
  3874. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3875. * the configuration information for I2C module
  3876. * @param DevAddress Target device address: The device 7 bits address value
  3877. * in datasheet must be shift at right before call interface
  3878. * @param Timeout Timeout duration
  3879. * @param Tickstart Tick start value
  3880. * @retval HAL status
  3881. */
  3882. static HAL_StatusTypeDef I2C_MasterRequestWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Timeout, uint32_t Tickstart)
  3883. {
  3884. /* Generate Start condition if first transfer */
  3885. if((hi2c->XferOptions == I2C_FIRST_AND_LAST_FRAME) || (hi2c->XferOptions == I2C_FIRST_FRAME) || (hi2c->XferOptions == I2C_NO_OPTION_FRAME))
  3886. {
  3887. /* Generate Start */
  3888. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  3889. }
  3890. else if(hi2c->PreviousState == I2C_STATE_MASTER_BUSY_RX)
  3891. {
  3892. /* Generate ReStart */
  3893. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  3894. }
  3895. /* Wait until SB flag is set */
  3896. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, Tickstart) != HAL_OK)
  3897. {
  3898. return HAL_TIMEOUT;
  3899. }
  3900. if(hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_7BIT)
  3901. {
  3902. /* Send slave address */
  3903. hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(DevAddress);
  3904. }
  3905. else
  3906. {
  3907. /* Send header of slave address */
  3908. hi2c->Instance->DR = I2C_10BIT_HEADER_WRITE(DevAddress);
  3909. /* Wait until ADD10 flag is set */
  3910. if(I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADD10, Timeout, Tickstart) != HAL_OK)
  3911. {
  3912. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  3913. {
  3914. return HAL_ERROR;
  3915. }
  3916. else
  3917. {
  3918. return HAL_TIMEOUT;
  3919. }
  3920. }
  3921. /* Send slave address */
  3922. hi2c->Instance->DR = I2C_10BIT_ADDRESS(DevAddress);
  3923. }
  3924. /* Wait until ADDR flag is set */
  3925. if(I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK)
  3926. {
  3927. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  3928. {
  3929. return HAL_ERROR;
  3930. }
  3931. else
  3932. {
  3933. return HAL_TIMEOUT;
  3934. }
  3935. }
  3936. return HAL_OK;
  3937. }
  3938. /**
  3939. * @brief Master sends target device address for read request.
  3940. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3941. * the configuration information for I2C module
  3942. * @param DevAddress Target device address: The device 7 bits address value
  3943. * in datasheet must be shift at right before call interface
  3944. * @param Timeout Timeout duration
  3945. * @param Tickstart Tick start value
  3946. * @retval HAL status
  3947. */
  3948. static HAL_StatusTypeDef I2C_MasterRequestRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Timeout, uint32_t Tickstart)
  3949. {
  3950. /* Enable Acknowledge */
  3951. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  3952. /* Generate Start condition if first transfer */
  3953. if((hi2c->XferOptions == I2C_FIRST_AND_LAST_FRAME) || (hi2c->XferOptions == I2C_FIRST_FRAME) || (hi2c->XferOptions == I2C_NO_OPTION_FRAME))
  3954. {
  3955. /* Generate Start */
  3956. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  3957. }
  3958. else if(hi2c->PreviousState == I2C_STATE_MASTER_BUSY_TX)
  3959. {
  3960. /* Generate ReStart */
  3961. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  3962. }
  3963. /* Wait until SB flag is set */
  3964. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, Tickstart) != HAL_OK)
  3965. {
  3966. return HAL_TIMEOUT;
  3967. }
  3968. if(hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_7BIT)
  3969. {
  3970. /* Send slave address */
  3971. hi2c->Instance->DR = I2C_7BIT_ADD_READ(DevAddress);
  3972. }
  3973. else
  3974. {
  3975. /* Send header of slave address */
  3976. hi2c->Instance->DR = I2C_10BIT_HEADER_WRITE(DevAddress);
  3977. /* Wait until ADD10 flag is set */
  3978. if(I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADD10, Timeout, Tickstart) != HAL_OK)
  3979. {
  3980. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  3981. {
  3982. return HAL_ERROR;
  3983. }
  3984. else
  3985. {
  3986. return HAL_TIMEOUT;
  3987. }
  3988. }
  3989. /* Send slave address */
  3990. hi2c->Instance->DR = I2C_10BIT_ADDRESS(DevAddress);
  3991. /* Wait until ADDR flag is set */
  3992. if(I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK)
  3993. {
  3994. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  3995. {
  3996. return HAL_ERROR;
  3997. }
  3998. else
  3999. {
  4000. return HAL_TIMEOUT;
  4001. }
  4002. }
  4003. /* Clear ADDR flag */
  4004. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  4005. /* Generate Restart */
  4006. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  4007. /* Wait until SB flag is set */
  4008. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, Tickstart) != HAL_OK)
  4009. {
  4010. return HAL_TIMEOUT;
  4011. }
  4012. /* Send header of slave address */
  4013. hi2c->Instance->DR = I2C_10BIT_HEADER_READ(DevAddress);
  4014. }
  4015. /* Wait until ADDR flag is set */
  4016. if(I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK)
  4017. {
  4018. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  4019. {
  4020. return HAL_ERROR;
  4021. }
  4022. else
  4023. {
  4024. return HAL_TIMEOUT;
  4025. }
  4026. }
  4027. return HAL_OK;
  4028. }
  4029. /**
  4030. * @brief Master sends target device address followed by internal memory address for write request.
  4031. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4032. * the configuration information for I2C module
  4033. * @param DevAddress Target device address: The device 7 bits address value
  4034. * in datasheet must be shift at right before call interface
  4035. * @param MemAddress Internal memory address
  4036. * @param MemAddSize Size of internal memory address
  4037. * @param Timeout Timeout duration
  4038. * @param Tickstart Tick start value
  4039. * @retval HAL status
  4040. */
  4041. static HAL_StatusTypeDef I2C_RequestMemoryWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart)
  4042. {
  4043. /* Generate Start */
  4044. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  4045. /* Wait until SB flag is set */
  4046. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, Tickstart) != HAL_OK)
  4047. {
  4048. return HAL_TIMEOUT;
  4049. }
  4050. /* Send slave address */
  4051. hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(DevAddress);
  4052. /* Wait until ADDR flag is set */
  4053. if(I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK)
  4054. {
  4055. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  4056. {
  4057. return HAL_ERROR;
  4058. }
  4059. else
  4060. {
  4061. return HAL_TIMEOUT;
  4062. }
  4063. }
  4064. /* Clear ADDR flag */
  4065. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  4066. /* Wait until TXE flag is set */
  4067. if(I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
  4068. {
  4069. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  4070. {
  4071. /* Generate Stop */
  4072. SET_BIT(hi2c->Instance->CR1,I2C_CR1_STOP);
  4073. return HAL_ERROR;
  4074. }
  4075. else
  4076. {
  4077. return HAL_TIMEOUT;
  4078. }
  4079. }
  4080. /* If Memory address size is 8Bit */
  4081. if(MemAddSize == I2C_MEMADD_SIZE_8BIT)
  4082. {
  4083. /* Send Memory Address */
  4084. hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress);
  4085. }
  4086. /* If Memory address size is 16Bit */
  4087. else
  4088. {
  4089. /* Send MSB of Memory Address */
  4090. hi2c->Instance->DR = I2C_MEM_ADD_MSB(MemAddress);
  4091. /* Wait until TXE flag is set */
  4092. if(I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
  4093. {
  4094. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  4095. {
  4096. /* Generate Stop */
  4097. SET_BIT(hi2c->Instance->CR1,I2C_CR1_STOP);
  4098. return HAL_ERROR;
  4099. }
  4100. else
  4101. {
  4102. return HAL_TIMEOUT;
  4103. }
  4104. }
  4105. /* Send LSB of Memory Address */
  4106. hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress);
  4107. }
  4108. return HAL_OK;
  4109. }
  4110. /**
  4111. * @brief Master sends target device address followed by internal memory address for read request.
  4112. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4113. * the configuration information for I2C module
  4114. * @param DevAddress Target device address: The device 7 bits address value
  4115. * in datasheet must be shift at right before call interface
  4116. * @param MemAddress Internal memory address
  4117. * @param MemAddSize Size of internal memory address
  4118. * @param Timeout Timeout duration
  4119. * @param Tickstart Tick start value
  4120. * @retval HAL status
  4121. */
  4122. static HAL_StatusTypeDef I2C_RequestMemoryRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart)
  4123. {
  4124. /* Enable Acknowledge */
  4125. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  4126. /* Generate Start */
  4127. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  4128. /* Wait until SB flag is set */
  4129. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, Tickstart) != HAL_OK)
  4130. {
  4131. return HAL_TIMEOUT;
  4132. }
  4133. /* Send slave address */
  4134. hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(DevAddress);
  4135. /* Wait until ADDR flag is set */
  4136. if(I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK)
  4137. {
  4138. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  4139. {
  4140. return HAL_ERROR;
  4141. }
  4142. else
  4143. {
  4144. return HAL_TIMEOUT;
  4145. }
  4146. }
  4147. /* Clear ADDR flag */
  4148. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  4149. /* Wait until TXE flag is set */
  4150. if(I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
  4151. {
  4152. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  4153. {
  4154. /* Generate Stop */
  4155. SET_BIT(hi2c->Instance->CR1,I2C_CR1_STOP);
  4156. return HAL_ERROR;
  4157. }
  4158. else
  4159. {
  4160. return HAL_TIMEOUT;
  4161. }
  4162. }
  4163. /* If Memory address size is 8Bit */
  4164. if(MemAddSize == I2C_MEMADD_SIZE_8BIT)
  4165. {
  4166. /* Send Memory Address */
  4167. hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress);
  4168. }
  4169. /* If Memory address size is 16Bit */
  4170. else
  4171. {
  4172. /* Send MSB of Memory Address */
  4173. hi2c->Instance->DR = I2C_MEM_ADD_MSB(MemAddress);
  4174. /* Wait until TXE flag is set */
  4175. if(I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
  4176. {
  4177. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  4178. {
  4179. /* Generate Stop */
  4180. SET_BIT(hi2c->Instance->CR1,I2C_CR1_STOP);
  4181. return HAL_ERROR;
  4182. }
  4183. else
  4184. {
  4185. return HAL_TIMEOUT;
  4186. }
  4187. }
  4188. /* Send LSB of Memory Address */
  4189. hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress);
  4190. }
  4191. /* Wait until TXE flag is set */
  4192. if(I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
  4193. {
  4194. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  4195. {
  4196. /* Generate Stop */
  4197. SET_BIT(hi2c->Instance->CR1,I2C_CR1_STOP);
  4198. return HAL_ERROR;
  4199. }
  4200. else
  4201. {
  4202. return HAL_TIMEOUT;
  4203. }
  4204. }
  4205. /* Generate Restart */
  4206. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  4207. /* Wait until SB flag is set */
  4208. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, Tickstart) != HAL_OK)
  4209. {
  4210. return HAL_TIMEOUT;
  4211. }
  4212. /* Send slave address */
  4213. hi2c->Instance->DR = I2C_7BIT_ADD_READ(DevAddress);
  4214. /* Wait until ADDR flag is set */
  4215. if(I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK)
  4216. {
  4217. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  4218. {
  4219. return HAL_ERROR;
  4220. }
  4221. else
  4222. {
  4223. return HAL_TIMEOUT;
  4224. }
  4225. }
  4226. return HAL_OK;
  4227. }
  4228. /**
  4229. * @brief DMA I2C process complete callback.
  4230. * @param hdma DMA handle
  4231. * @retval None
  4232. */
  4233. static void I2C_DMAXferCplt(DMA_HandleTypeDef *hdma)
  4234. {
  4235. I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  4236. if((hi2c->State == HAL_I2C_STATE_BUSY_TX) || ((hi2c->State == HAL_I2C_STATE_BUSY_RX) && (hi2c->Mode == HAL_I2C_MODE_SLAVE)))
  4237. {
  4238. /* Disable DMA Request */
  4239. CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
  4240. hi2c->XferCount = 0U;
  4241. /* Enable EVT and ERR interrupt */
  4242. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
  4243. }
  4244. else
  4245. {
  4246. /* Disable Acknowledge */
  4247. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  4248. /* Generate Stop */
  4249. SET_BIT(hi2c->Instance->CR1,I2C_CR1_STOP);
  4250. /* Disable Last DMA */
  4251. CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_LAST);
  4252. /* Disable DMA Request */
  4253. CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
  4254. hi2c->XferCount = 0U;
  4255. /* Check if Errors has been detected during transfer */
  4256. if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  4257. {
  4258. HAL_I2C_ErrorCallback(hi2c);
  4259. }
  4260. else
  4261. {
  4262. hi2c->State = HAL_I2C_STATE_READY;
  4263. if(hi2c->Mode == HAL_I2C_MODE_MEM)
  4264. {
  4265. hi2c->Mode = HAL_I2C_MODE_NONE;
  4266. HAL_I2C_MemRxCpltCallback(hi2c);
  4267. }
  4268. else
  4269. {
  4270. hi2c->Mode = HAL_I2C_MODE_NONE;
  4271. HAL_I2C_MasterRxCpltCallback(hi2c);
  4272. }
  4273. }
  4274. }
  4275. }
  4276. /**
  4277. * @brief DMA I2C communication error callback.
  4278. * @param hdma DMA handle
  4279. * @retval None
  4280. */
  4281. static void I2C_DMAError(DMA_HandleTypeDef *hdma)
  4282. {
  4283. I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  4284. /* Disable Acknowledge */
  4285. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  4286. hi2c->XferCount = 0U;
  4287. hi2c->State = HAL_I2C_STATE_READY;
  4288. hi2c->Mode = HAL_I2C_MODE_NONE;
  4289. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  4290. HAL_I2C_ErrorCallback(hi2c);
  4291. }
  4292. /**
  4293. * @brief DMA I2C communication abort callback
  4294. * (To be called at end of DMA Abort procedure).
  4295. * @param hdma: DMA handle.
  4296. * @retval None
  4297. */
  4298. static void I2C_DMAAbort(DMA_HandleTypeDef *hdma)
  4299. {
  4300. I2C_HandleTypeDef* hi2c = ( I2C_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  4301. /* Disable Acknowledge */
  4302. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  4303. hi2c->XferCount = 0U;
  4304. /* Reset XferAbortCallback */
  4305. hi2c->hdmatx->XferAbortCallback = NULL;
  4306. hi2c->hdmarx->XferAbortCallback = NULL;
  4307. /* Check if come from abort from user */
  4308. if(hi2c->State == HAL_I2C_STATE_ABORT)
  4309. {
  4310. hi2c->State = HAL_I2C_STATE_READY;
  4311. hi2c->Mode = HAL_I2C_MODE_NONE;
  4312. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  4313. /* Disable I2C peripheral to prevent dummy data in buffer */
  4314. __HAL_I2C_DISABLE(hi2c);
  4315. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4316. HAL_I2C_AbortCpltCallback(hi2c);
  4317. }
  4318. else
  4319. {
  4320. hi2c->State = HAL_I2C_STATE_READY;
  4321. hi2c->Mode = HAL_I2C_MODE_NONE;
  4322. /* Disable I2C peripheral to prevent dummy data in buffer */
  4323. __HAL_I2C_DISABLE(hi2c);
  4324. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4325. HAL_I2C_ErrorCallback(hi2c);
  4326. }
  4327. }
  4328. /**
  4329. * @brief This function handles I2C Communication Timeout.
  4330. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4331. * the configuration information for I2C module
  4332. * @param Flag specifies the I2C flag to check.
  4333. * @param Status The new Flag status (SET or RESET).
  4334. * @param Timeout Timeout duration
  4335. * @param Tickstart Tick start value
  4336. * @retval HAL status
  4337. */
  4338. static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status, uint32_t Timeout, uint32_t Tickstart)
  4339. {
  4340. while(__HAL_I2C_GET_FLAG(hi2c, Flag) == Status)
  4341. {
  4342. /* Check for the Timeout */
  4343. if(Timeout != HAL_MAX_DELAY)
  4344. {
  4345. if((Timeout == 0U)||((HAL_GetTick() - Tickstart ) > Timeout))
  4346. {
  4347. hi2c->PreviousState = I2C_STATE_NONE;
  4348. hi2c->State= HAL_I2C_STATE_READY;
  4349. hi2c->Mode = HAL_I2C_MODE_NONE;
  4350. /* Process Unlocked */
  4351. __HAL_UNLOCK(hi2c);
  4352. return HAL_TIMEOUT;
  4353. }
  4354. }
  4355. }
  4356. return HAL_OK;
  4357. }
  4358. /**
  4359. * @brief This function handles I2C Communication Timeout for Master addressing phase.
  4360. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4361. * the configuration information for I2C module
  4362. * @param Flag specifies the I2C flag to check.
  4363. * @param Timeout Timeout duration
  4364. * @param Tickstart Tick start value
  4365. * @retval HAL status
  4366. */
  4367. static HAL_StatusTypeDef I2C_WaitOnMasterAddressFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, uint32_t Timeout, uint32_t Tickstart)
  4368. {
  4369. while(__HAL_I2C_GET_FLAG(hi2c, Flag) == RESET)
  4370. {
  4371. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
  4372. {
  4373. /* Generate Stop */
  4374. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  4375. /* Clear AF Flag */
  4376. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  4377. hi2c->ErrorCode = HAL_I2C_ERROR_AF;
  4378. hi2c->PreviousState = I2C_STATE_NONE;
  4379. hi2c->State= HAL_I2C_STATE_READY;
  4380. /* Process Unlocked */
  4381. __HAL_UNLOCK(hi2c);
  4382. return HAL_ERROR;
  4383. }
  4384. /* Check for the Timeout */
  4385. if(Timeout != HAL_MAX_DELAY)
  4386. {
  4387. if((Timeout == 0U)||((HAL_GetTick() - Tickstart ) > Timeout))
  4388. {
  4389. hi2c->PreviousState = I2C_STATE_NONE;
  4390. hi2c->State= HAL_I2C_STATE_READY;
  4391. /* Process Unlocked */
  4392. __HAL_UNLOCK(hi2c);
  4393. return HAL_TIMEOUT;
  4394. }
  4395. }
  4396. }
  4397. return HAL_OK;
  4398. }
  4399. /**
  4400. * @brief This function handles I2C Communication Timeout for specific usage of TXE flag.
  4401. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4402. * the configuration information for the specified I2C.
  4403. * @param Timeout Timeout duration
  4404. * @param Tickstart Tick start value
  4405. * @retval HAL status
  4406. */
  4407. static HAL_StatusTypeDef I2C_WaitOnTXEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart)
  4408. {
  4409. while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXE) == RESET)
  4410. {
  4411. /* Check if a NACK is detected */
  4412. if(I2C_IsAcknowledgeFailed(hi2c) != HAL_OK)
  4413. {
  4414. return HAL_ERROR;
  4415. }
  4416. /* Check for the Timeout */
  4417. if(Timeout != HAL_MAX_DELAY)
  4418. {
  4419. if((Timeout == 0U) || ((HAL_GetTick()-Tickstart) > Timeout))
  4420. {
  4421. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  4422. hi2c->PreviousState = I2C_STATE_NONE;
  4423. hi2c->State= HAL_I2C_STATE_READY;
  4424. /* Process Unlocked */
  4425. __HAL_UNLOCK(hi2c);
  4426. return HAL_TIMEOUT;
  4427. }
  4428. }
  4429. }
  4430. return HAL_OK;
  4431. }
  4432. /**
  4433. * @brief This function handles I2C Communication Timeout for specific usage of BTF flag.
  4434. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4435. * the configuration information for the specified I2C.
  4436. * @param Timeout Timeout duration
  4437. * @param Tickstart Tick start value
  4438. * @retval HAL status
  4439. */
  4440. static HAL_StatusTypeDef I2C_WaitOnBTFFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart)
  4441. {
  4442. while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == RESET)
  4443. {
  4444. /* Check if a NACK is detected */
  4445. if(I2C_IsAcknowledgeFailed(hi2c) != HAL_OK)
  4446. {
  4447. return HAL_ERROR;
  4448. }
  4449. /* Check for the Timeout */
  4450. if(Timeout != HAL_MAX_DELAY)
  4451. {
  4452. if((Timeout == 0U) || ((HAL_GetTick()-Tickstart) > Timeout))
  4453. {
  4454. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  4455. hi2c->PreviousState = I2C_STATE_NONE;
  4456. hi2c->State= HAL_I2C_STATE_READY;
  4457. /* Process Unlocked */
  4458. __HAL_UNLOCK(hi2c);
  4459. return HAL_TIMEOUT;
  4460. }
  4461. }
  4462. }
  4463. return HAL_OK;
  4464. }
  4465. /**
  4466. * @brief This function handles I2C Communication Timeout for specific usage of STOP flag.
  4467. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4468. * the configuration information for the specified I2C.
  4469. * @param Timeout Timeout duration
  4470. * @param Tickstart Tick start value
  4471. * @retval HAL status
  4472. */
  4473. static HAL_StatusTypeDef I2C_WaitOnSTOPFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart)
  4474. {
  4475. while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET)
  4476. {
  4477. /* Check if a NACK is detected */
  4478. if(I2C_IsAcknowledgeFailed(hi2c) != HAL_OK)
  4479. {
  4480. return HAL_ERROR;
  4481. }
  4482. /* Check for the Timeout */
  4483. if((Timeout == 0U) || ((HAL_GetTick()-Tickstart) > Timeout))
  4484. {
  4485. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  4486. hi2c->PreviousState = I2C_STATE_NONE;
  4487. hi2c->State= HAL_I2C_STATE_READY;
  4488. /* Process Unlocked */
  4489. __HAL_UNLOCK(hi2c);
  4490. return HAL_TIMEOUT;
  4491. }
  4492. }
  4493. return HAL_OK;
  4494. }
  4495. /**
  4496. * @brief This function handles I2C Communication Timeout for specific usage of RXNE flag.
  4497. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4498. * the configuration information for the specified I2C.
  4499. * @param Timeout Timeout duration
  4500. * @param Tickstart Tick start value
  4501. * @retval HAL status
  4502. */
  4503. static HAL_StatusTypeDef I2C_WaitOnRXNEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart)
  4504. {
  4505. while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == RESET)
  4506. {
  4507. /* Check if a STOPF is detected */
  4508. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET)
  4509. {
  4510. /* Clear STOP Flag */
  4511. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  4512. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  4513. hi2c->PreviousState = I2C_STATE_NONE;
  4514. hi2c->State= HAL_I2C_STATE_READY;
  4515. /* Process Unlocked */
  4516. __HAL_UNLOCK(hi2c);
  4517. return HAL_ERROR;
  4518. }
  4519. /* Check for the Timeout */
  4520. if((Timeout == 0U) || ((HAL_GetTick()-Tickstart) > Timeout))
  4521. {
  4522. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  4523. hi2c->State= HAL_I2C_STATE_READY;
  4524. /* Process Unlocked */
  4525. __HAL_UNLOCK(hi2c);
  4526. return HAL_TIMEOUT;
  4527. }
  4528. }
  4529. return HAL_OK;
  4530. }
  4531. /**
  4532. * @brief This function handles Acknowledge failed detection during an I2C Communication.
  4533. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4534. * the configuration information for the specified I2C.
  4535. * @retval HAL status
  4536. */
  4537. static HAL_StatusTypeDef I2C_IsAcknowledgeFailed(I2C_HandleTypeDef *hi2c)
  4538. {
  4539. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
  4540. {
  4541. /* Clear NACKF Flag */
  4542. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  4543. hi2c->ErrorCode = HAL_I2C_ERROR_AF;
  4544. hi2c->PreviousState = I2C_STATE_NONE;
  4545. hi2c->State= HAL_I2C_STATE_READY;
  4546. /* Process Unlocked */
  4547. __HAL_UNLOCK(hi2c);
  4548. return HAL_ERROR;
  4549. }
  4550. return HAL_OK;
  4551. }
  4552. /**
  4553. * @}
  4554. */
  4555. #endif /* HAL_I2C_MODULE_ENABLED */
  4556. /**
  4557. * @}
  4558. */
  4559. /**
  4560. * @}
  4561. */
  4562. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/