stm32l0xx_hal_i2c.c 122 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224
  1. /**
  2. ******************************************************************************
  3. * @file stm32l0xx_hal_i2c.c
  4. * @author MCD Application Team
  5. * @version V1.7.0
  6. * @date 31-May-2016
  7. * @brief I2C HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of the Inter Integrated Circuit (I2C) peripheral:
  10. * + Initialization and de-initialization functions
  11. * + IO operation functions
  12. * + Peripheral State and Errors functions
  13. *
  14. @verbatim
  15. ==============================================================================
  16. ##### How to use this driver #####
  17. ==============================================================================
  18. [..]
  19. The I2C HAL driver can be used as follows:
  20. (#) Declare a I2C_HandleTypeDef handle structure, for example:
  21. I2C_HandleTypeDef hi2c;
  22. (#)Initialize the I2C low level resources by implement the HAL_I2C_MspInit ()API:
  23. (##) Enable the I2Cx interface clock
  24. (##) I2C pins configuration
  25. (+++) Enable the clock for the I2C GPIOs
  26. (+++) Configure I2C pins as alternate function open-drain
  27. (##) NVIC configuration if you need to use interrupt process
  28. (+++) Configure the I2Cx interrupt priority
  29. (+++) Enable the NVIC I2C IRQ Channel
  30. (##) DMA Configuration if you need to use DMA process
  31. (+++) Declare a DMA_HandleTypeDef handle structure for the transmit or receive channel
  32. (+++) Enable the DMAx interface clock using
  33. (+++) Configure the DMA handle parameters
  34. (+++) Configure the DMA Tx or Rx channel
  35. (+++) Associate the initilalized DMA handle to the hi2c DMA Tx or Rx handle
  36. (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on
  37. the DMA Tx or Rx channel
  38. (#) Configure the Communication Clock Timing, Own Address1, Master Adressing Mode, Dual Addressing mode,
  39. Own Address2, Own Address2 Mask, General call and Nostretch mode in the hi2c Init structure.
  40. (#) Initialize the I2C registers by calling the HAL_I2C_Init(), configures also the low level Hardware
  41. (GPIO, CLOCK, NVIC...etc) by calling the customed HAL_I2C_MspInit(&hi2c) API.
  42. (#) To check if target device is ready for communication, use the function HAL_I2C_IsDeviceReady()
  43. (#) For I2C IO and IO MEM operations, three operation modes are available within this driver :
  44. *** Polling mode IO operation ***
  45. =================================
  46. [..]
  47. (+) Transmit in master mode an amount of data in blocking mode using HAL_I2C_Master_Transmit()
  48. (+) Receive in master mode an amount of data in blocking mode using HAL_I2C_Master_Receive()
  49. (+) Transmit in slave mode an amount of data in blocking mode using HAL_I2C_Slave_Transmit()
  50. (+) Receive in slave mode an amount of data in blocking mode using HAL_I2C_Slave_Receive()
  51. *** Polling mode IO MEM operation ***
  52. =====================================
  53. [..]
  54. (+) Write an amount of data in blocking mode to a specific memory address using HAL_I2C_Mem_Write()
  55. (+) Read an amount of data in blocking mode from a specific memory address using HAL_I2C_Mem_Read()
  56. *** Interrupt mode IO operation ***
  57. ===================================
  58. [..]
  59. (+) Transmit in master mode an amount of data in non blocking mode using HAL_I2C_Master_Transmit_IT()
  60. (+) At transmission end of transfer HAL_I2C_MasterTxCpltCallback is executed and user can
  61. add his own code by customization of function pointer HAL_I2C_MasterTxCpltCallback
  62. (+) Receive in master mode an amount of data in non blocking mode using HAL_I2C_Master_Receive_IT()
  63. (+) At reception end of transfer HAL_I2C_MasterRxCpltCallback is executed and user can
  64. add his own code by customization of function pointer HAL_I2C_MasterRxCpltCallback
  65. (+) Transmit in slave mode an amount of data in non blocking mode using HAL_I2C_Slave_Transmit_IT()
  66. (+) At transmission end of transfer HAL_I2C_SlaveTxCpltCallback is executed and user can
  67. add his own code by customization of function pointer HAL_I2C_SlaveTxCpltCallback
  68. (+) Receive in slave mode an amount of data in non blocking mode using HAL_I2C_Slave_Receive_IT()
  69. (+) At reception end of transfer HAL_I2C_SlaveRxCpltCallback is executed and user can
  70. add his own code by customization of function pointer HAL_I2C_SlaveRxCpltCallback
  71. (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
  72. add his own code by customization of function pointer HAL_I2C_ErrorCallback
  73. *** Interrupt mode IO MEM operation ***
  74. =======================================
  75. [..]
  76. (+) Write an amount of data in no-blocking mode with Interrupt to a specific memory address using
  77. HAL_I2C_Mem_Write_IT()
  78. (+) At MEM end of write transfer HAL_I2C_MemTxCpltCallback is executed and user can
  79. add his own code by customization of function pointer HAL_I2C_MemTxCpltCallback
  80. (+) Read an amount of data in no-blocking mode with Interrupt from a specific memory address using
  81. HAL_I2C_Mem_Read_IT()
  82. (+) At MEM end of read transfer HAL_I2C_MemRxCpltCallback is executed and user can
  83. add his own code by customization of function pointer HAL_I2C_MemRxCpltCallback
  84. (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
  85. add his own code by customization of function pointer HAL_I2C_ErrorCallback
  86. *** DMA mode IO operation ***
  87. ==============================
  88. [..]
  89. (+) Transmit in master mode an amount of data in non blocking mode (DMA) using
  90. HAL_I2C_Master_Transmit_DMA()
  91. (+) At transmission end of transfer HAL_I2C_MasterTxCpltCallback is executed and user can
  92. add his own code by customization of function pointer HAL_I2C_MasterTxCpltCallback
  93. (+) Receive in master mode an amount of data in non blocking mode (DMA) using
  94. HAL_I2C_Master_Receive_DMA()
  95. (+) At reception end of transfer HAL_I2C_MasterRxCpltCallback is executed and user can
  96. add his own code by customization of function pointer HAL_I2C_MasterRxCpltCallback
  97. (+) Transmit in slave mode an amount of data in non blocking mode (DMA) using
  98. HAL_I2C_Slave_Transmit_DMA()
  99. (+) At transmission end of transfer HAL_I2C_SlaveTxCpltCallback is executed and user can
  100. add his own code by customization of function pointer HAL_I2C_SlaveTxCpltCallback
  101. (+) Receive in slave mode an amount of data in non blocking mode (DMA) using
  102. HAL_I2C_Slave_Receive_DMA()
  103. (+) At reception end of transfer HAL_I2C_SlaveRxCpltCallback is executed and user can
  104. add his own code by customization of function pointer HAL_I2C_SlaveRxCpltCallback
  105. (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
  106. add his own code by customization of function pointer HAL_I2C_ErrorCallback
  107. *** DMA mode IO MEM operation ***
  108. =================================
  109. [..]
  110. (+) Write an amount of data in no-blocking mode with DMA to a specific memory address using
  111. HAL_I2C_Mem_Write_DMA()
  112. (+) At MEM end of write transfer HAL_I2C_MemTxCpltCallback is executed and user can
  113. add his own code by customization of function pointer HAL_I2C_MemTxCpltCallback
  114. (+) Read an amount of data in no-blocking mode with DMA from a specific memory address using
  115. HAL_I2C_Mem_Read_DMA()
  116. (+) At MEM end of read transfer HAL_I2C_MemRxCpltCallback is executed and user can
  117. add his own code by customization of function pointer HAL_I2C_MemRxCpltCallback
  118. (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
  119. add his own code by customization of function pointer HAL_I2C_ErrorCallback
  120. *** I2C HAL driver macros list ***
  121. ==================================
  122. [..]
  123. Below the list of most used macros in I2C HAL driver.
  124. (+) __HAL_I2C_ENABLE: Enable the I2C peripheral
  125. (+) __HAL_I2C_DISABLE: Disable the I2C peripheral
  126. (+) __HAL_I2C_GET_FLAG : Check whether the specified I2C flag is set or not
  127. (+) __HAL_I2C_CLEAR_FLAG : Clear the specified I2C pending flag
  128. (+) __HAL_I2C_ENABLE_IT: Enable the specified I2C interrupt
  129. (+) __HAL_I2C_DISABLE_IT: Disable the specified I2C interrupt
  130. [..]
  131. (@) You can refer to the I2C HAL driver header file for more useful macros
  132. @endverbatim
  133. ******************************************************************************
  134. * @attention
  135. *
  136. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  137. *
  138. * Redistribution and use in source and binary forms, with or without modification,
  139. * are permitted provided that the following conditions are met:
  140. * 1. Redistributions of source code must retain the above copyright notice,
  141. * this list of conditions and the following disclaimer.
  142. * 2. Redistributions in binary form must reproduce the above copyright notice,
  143. * this list of conditions and the following disclaimer in the documentation
  144. * and/or other materials provided with the distribution.
  145. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  146. * may be used to endorse or promote products derived from this software
  147. * without specific prior written permission.
  148. *
  149. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  150. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  151. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  152. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  153. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  154. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  155. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  156. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  157. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  158. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  159. *
  160. ******************************************************************************
  161. */
  162. /* Includes ------------------------------------------------------------------*/
  163. #include "stm32l0xx_hal.h"
  164. /** @addtogroup STM32L0xx_HAL_Driver
  165. * @{
  166. */
  167. #ifdef HAL_I2C_MODULE_ENABLED
  168. /** @addtogroup I2C I2C
  169. * @brief I2C HAL module driver
  170. * @{
  171. */
  172. /* Private typedef -----------------------------------------------------------*/
  173. /* Private constants ---------------------------------------------------------*/
  174. /** @addtogroup I2C_Private
  175. * @{
  176. */
  177. #define TIMING_CLEAR_MASK ((uint32_t)0xF0FFFFFFU) /*<! I2C TIMING clear register Mask */
  178. #define I2C_TIMEOUT_ADDR ((uint32_t)10000U) /* 10 s */
  179. #define I2C_TIMEOUT_BUSY ((uint32_t)25U) /* 25 ms */
  180. #define I2C_TIMEOUT_DIR ((uint32_t)25U) /* 25 ms */
  181. #define I2C_TIMEOUT_RXNE ((uint32_t)25U) /* 25 ms */
  182. #define I2C_TIMEOUT_STOPF ((uint32_t)25U) /* 25 ms */
  183. #define I2C_TIMEOUT_TC ((uint32_t)25U) /* 25 ms */
  184. #define I2C_TIMEOUT_TCR ((uint32_t)25U) /* 25 ms */
  185. #define I2C_TIMEOUT_TXIS ((uint32_t)25U) /* 25 ms */
  186. #define I2C_TIMEOUT_FLAG ((uint32_t)25U) /* 25 ms */
  187. /**
  188. * @}
  189. */
  190. /* Private macro -------------------------------------------------------------*/
  191. /* Private variables ---------------------------------------------------------*/
  192. /* Private function prototypes -----------------------------------------------*/
  193. /** @addtogroup I2C_Private
  194. * @{
  195. */
  196. static void I2C_DMAMasterTransmitCplt(DMA_HandleTypeDef *hdma);
  197. static void I2C_DMAMasterReceiveCplt(DMA_HandleTypeDef *hdma);
  198. static void I2C_DMASlaveTransmitCplt(DMA_HandleTypeDef *hdma);
  199. static void I2C_DMASlaveReceiveCplt(DMA_HandleTypeDef *hdma);
  200. static void I2C_DMAMemTransmitCplt(DMA_HandleTypeDef *hdma);
  201. static void I2C_DMAMemReceiveCplt(DMA_HandleTypeDef *hdma);
  202. static void I2C_DMAError(DMA_HandleTypeDef *hdma);
  203. static HAL_StatusTypeDef I2C_RequestMemoryWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout);
  204. static HAL_StatusTypeDef I2C_RequestMemoryRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout);
  205. static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status, uint32_t Timeout);
  206. static HAL_StatusTypeDef I2C_WaitOnTXISFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout);
  207. static HAL_StatusTypeDef I2C_WaitOnRXNEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout);
  208. static HAL_StatusTypeDef I2C_WaitOnSTOPFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout);
  209. static HAL_StatusTypeDef I2C_IsAcknowledgeFailed(I2C_HandleTypeDef *hi2c, uint32_t Timeout);
  210. static HAL_StatusTypeDef I2C_MasterTransmit_ISR(I2C_HandleTypeDef *hi2c);
  211. static HAL_StatusTypeDef I2C_MasterReceive_ISR(I2C_HandleTypeDef *hi2c);
  212. static HAL_StatusTypeDef I2C_SlaveTransmit_ISR(I2C_HandleTypeDef *hi2c);
  213. static HAL_StatusTypeDef I2C_SlaveReceive_ISR(I2C_HandleTypeDef *hi2c);
  214. static void I2C_TransferConfig(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t Size, uint32_t Mode, uint32_t Request);
  215. /**
  216. * @}
  217. */
  218. /* Exported functions --------------------------------------------------------*/
  219. /** @addtogroup I2C_Exported_Functions
  220. * @{
  221. */
  222. /** @addtogroup I2C_Exported_Functions_Group1
  223. * @brief Initialization and Configuration functions
  224. *
  225. @verbatim
  226. ===============================================================================
  227. ##### Initialization and de-initialization functions #####
  228. ===============================================================================
  229. [..] This subsection provides a set of functions allowing to initialize and
  230. de-initialiaze the I2Cx peripheral:
  231. (+) User must Implement HAL_I2C_MspInit() function in which he configures
  232. all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC ).
  233. (+) Call the function HAL_I2C_Init() to configure the selected device with
  234. the selected configuration:
  235. (++) Clock Timing
  236. (++) Own Address 1
  237. (++) Addressing mode (Master, Slave)
  238. (++) Dual Addressing mode
  239. (++) Own Address 2
  240. (++) Own Address 2 Mask
  241. (++) General call mode
  242. (++) Nostretch mode
  243. (+) Call the function HAL_I2C_DeInit() to restore the default configuration
  244. of the selected I2Cx periperal.
  245. @endverbatim
  246. * @{
  247. */
  248. /**
  249. * @brief Initializes the I2C according to the specified parameters
  250. * in the I2C_InitTypeDef and create the associated handle.
  251. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  252. * the configuration information for the specified I2C.
  253. * @retval HAL status
  254. */
  255. HAL_StatusTypeDef HAL_I2C_Init(I2C_HandleTypeDef *hi2c)
  256. {
  257. /* Check the I2C handle allocation */
  258. if(hi2c == NULL)
  259. {
  260. return HAL_ERROR;
  261. }
  262. /* Check the parameters */
  263. assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
  264. assert_param(IS_I2C_OWN_ADDRESS1(hi2c->Init.OwnAddress1));
  265. assert_param(IS_I2C_ADDRESSING_MODE(hi2c->Init.AddressingMode));
  266. assert_param(IS_I2C_DUAL_ADDRESS(hi2c->Init.DualAddressMode));
  267. assert_param(IS_I2C_OWN_ADDRESS2(hi2c->Init.OwnAddress2));
  268. assert_param(IS_I2C_OWN_ADDRESS2_MASK(hi2c->Init.OwnAddress2Masks));
  269. assert_param(IS_I2C_GENERAL_CALL(hi2c->Init.GeneralCallMode));
  270. assert_param(IS_I2C_NO_STRETCH(hi2c->Init.NoStretchMode));
  271. if(hi2c->State == HAL_I2C_STATE_RESET)
  272. {
  273. /* Allocate lock resource and initialize it */
  274. hi2c->Lock = HAL_UNLOCKED;
  275. /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
  276. HAL_I2C_MspInit(hi2c);
  277. }
  278. hi2c->State = HAL_I2C_STATE_BUSY;
  279. /* Disable the selected I2C peripheral */
  280. __HAL_I2C_DISABLE(hi2c);
  281. /*---------------------------- I2Cx TIMINGR Configuration ------------------*/
  282. /* Configure I2Cx: Frequency range */
  283. hi2c->Instance->TIMINGR = hi2c->Init.Timing & TIMING_CLEAR_MASK;
  284. /*---------------------------- I2Cx OAR1 Configuration ---------------------*/
  285. /* Configure I2Cx: Own Address1 and ack own address1 mode */
  286. hi2c->Instance->OAR1 &= ~I2C_OAR1_OA1EN;
  287. if(hi2c->Init.OwnAddress1 != 0U)
  288. {
  289. if(hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_7BIT)
  290. {
  291. hi2c->Instance->OAR1 = (I2C_OAR1_OA1EN | hi2c->Init.OwnAddress1);
  292. }
  293. else /* I2C_ADDRESSINGMODE_10BIT */
  294. {
  295. hi2c->Instance->OAR1 = (I2C_OAR1_OA1EN | I2C_OAR1_OA1MODE | hi2c->Init.OwnAddress1);
  296. }
  297. }
  298. /*---------------------------- I2Cx CR2 Configuration ----------------------*/
  299. /* Configure I2Cx: Addressing Master mode */
  300. if(hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT)
  301. {
  302. hi2c->Instance->CR2 = (I2C_CR2_ADD10);
  303. }
  304. /* Enable the AUTOEND by default, and enable NACK (should be disable only during Slave process */
  305. hi2c->Instance->CR2 |= (I2C_CR2_AUTOEND | I2C_CR2_NACK);
  306. /*---------------------------- I2Cx OAR2 Configuration ---------------------*/
  307. /* Configure I2Cx: Dual mode and Own Address2 */
  308. hi2c->Instance->OAR2 = (hi2c->Init.DualAddressMode | hi2c->Init.OwnAddress2 | (hi2c->Init.OwnAddress2Masks << 8));
  309. /*---------------------------- I2Cx CR1 Configuration ----------------------*/
  310. /* Configure I2Cx: Generalcall and NoStretch mode */
  311. hi2c->Instance->CR1 = (hi2c->Init.GeneralCallMode | hi2c->Init.NoStretchMode);
  312. /* Enable the selected I2C peripheral */
  313. __HAL_I2C_ENABLE(hi2c);
  314. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  315. hi2c->State = HAL_I2C_STATE_READY;
  316. return HAL_OK;
  317. }
  318. /**
  319. * @brief DeInitializes the I2C peripheral.
  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_DeInit(I2C_HandleTypeDef *hi2c)
  325. {
  326. /* Check the I2C handle allocation */
  327. if(hi2c == NULL)
  328. {
  329. return HAL_ERROR;
  330. }
  331. /* Check the parameters */
  332. assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
  333. hi2c->State = HAL_I2C_STATE_BUSY;
  334. /* Disable the I2C Peripheral Clock */
  335. __HAL_I2C_DISABLE(hi2c);
  336. /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
  337. HAL_I2C_MspDeInit(hi2c);
  338. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  339. hi2c->State = HAL_I2C_STATE_RESET;
  340. /* Release Lock */
  341. __HAL_UNLOCK(hi2c);
  342. return HAL_OK;
  343. }
  344. /**
  345. * @brief I2C MSP Init.
  346. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  347. * the configuration information for the specified I2C.
  348. * @retval None
  349. */
  350. __weak void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c)
  351. {
  352. /* Prevent unused argument(s) compilation warning */
  353. UNUSED(hi2c);
  354. /* NOTE : This function Should not be modified, when the callback is needed,
  355. the HAL_I2C_MspInit could be implemented in the user file
  356. */
  357. }
  358. /**
  359. * @brief I2C MSP DeInit
  360. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  361. * the configuration information for the specified I2C.
  362. * @retval None
  363. */
  364. __weak void HAL_I2C_MspDeInit(I2C_HandleTypeDef *hi2c)
  365. {
  366. /* Prevent unused argument(s) compilation warning */
  367. UNUSED(hi2c);
  368. /* NOTE : This function Should not be modified, when the callback is needed,
  369. the HAL_I2C_MspDeInit could be implemented in the user file
  370. */
  371. }
  372. /**
  373. * @}
  374. */
  375. /** @addtogroup I2C_Exported_Functions_Group2
  376. * @brief Data transfers functions
  377. *
  378. @verbatim
  379. ===============================================================================
  380. ##### IO operation functions #####
  381. ===============================================================================
  382. [..]
  383. This subsection provides a set of functions allowing to manage the I2C data
  384. transfers.
  385. (#) There are two modes of transfer:
  386. (++) Blocking mode : The communication is performed in the polling mode.
  387. The status of all data processing is returned by the same function
  388. after finishing transfer.
  389. (++) No-Blocking mode : The communication is performed using Interrupts
  390. or DMA. These functions return the status of the transfer startup.
  391. The end of the data processing will be indicated through the
  392. dedicated I2C IRQ when using Interrupt mode or the DMA IRQ when
  393. using DMA mode.
  394. (#) Blocking mode functions are :
  395. (++) HAL_I2C_Master_Transmit()
  396. (++) HAL_I2C_Master_Receive()
  397. (++) HAL_I2C_Slave_Transmit()
  398. (++) HAL_I2C_Slave_Receive()
  399. (++) HAL_I2C_Mem_Write()
  400. (++) HAL_I2C_Mem_Read()
  401. (++) HAL_I2C_IsDeviceReady()
  402. (#) No-Blocking mode functions with Interrupt are :
  403. (++) HAL_I2C_Master_Transmit_IT()
  404. (++) HAL_I2C_Master_Receive_IT()
  405. (++) HAL_I2C_Slave_Transmit_IT()
  406. (++) HAL_I2C_Slave_Receive_IT()
  407. (++) HAL_I2C_Mem_Write_IT()
  408. (++) HAL_I2C_Mem_Read_IT()
  409. (#) No-Blocking mode functions with DMA are :
  410. (++) HAL_I2C_Master_Transmit_DMA()
  411. (++) HAL_I2C_Master_Receive_DMA()
  412. (++) HAL_I2C_Slave_Transmit_DMA()
  413. (++) HAL_I2C_Slave_Receive_DMA()
  414. (++) HAL_I2C_Mem_Write_DMA()
  415. (++) HAL_I2C_Mem_Read_DMA()
  416. (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
  417. (++) HAL_I2C_MemTxCpltCallback()
  418. (++) HAL_I2C_MemRxCpltCallback()
  419. (++) HAL_I2C_MasterTxCpltCallback()
  420. (++) HAL_I2C_MasterRxCpltCallback()
  421. (++) HAL_I2C_SlaveTxCpltCallback()
  422. (++) HAL_I2C_SlaveRxCpltCallback()
  423. (++) HAL_I2C_ErrorCallback()
  424. @endverbatim
  425. * @{
  426. */
  427. /**
  428. * @brief Transmits in master mode an amount of data in blocking mode.
  429. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  430. * the configuration information for the specified I2C.
  431. * @param DevAddress: Target device address
  432. * @param pData: Pointer to data buffer
  433. * @param Size: Amount of data to be sent
  434. * @param Timeout: Timeout duration
  435. * @retval HAL status
  436. */
  437. HAL_StatusTypeDef HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  438. {
  439. uint32_t sizetmp = 0U;
  440. if(hi2c->State == HAL_I2C_STATE_READY)
  441. {
  442. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  443. {
  444. return HAL_BUSY;
  445. }
  446. /* Process Locked */
  447. __HAL_LOCK(hi2c);
  448. hi2c->State = HAL_I2C_STATE_MASTER_BUSY_TX;
  449. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  450. /* Send Slave Address */
  451. /* Set NBYTES to write and reload if size > 255 and generate RESTART */
  452. /* Size > 255, need to set RELOAD bit */
  453. if(Size > 255U)
  454. {
  455. I2C_TransferConfig(hi2c,DevAddress,255U, I2C_RELOAD_MODE, I2C_GENERATE_START_WRITE);
  456. sizetmp = 255U;
  457. }
  458. else
  459. {
  460. I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_GENERATE_START_WRITE);
  461. sizetmp = Size;
  462. }
  463. do
  464. {
  465. /* Wait until TXIS flag is set */
  466. if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
  467. {
  468. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  469. {
  470. return HAL_ERROR;
  471. }
  472. else
  473. {
  474. return HAL_TIMEOUT;
  475. }
  476. }
  477. /* Write data to TXDR */
  478. hi2c->Instance->TXDR = (*pData++);
  479. sizetmp--;
  480. Size--;
  481. if((sizetmp == 0U)&&(Size!=0U))
  482. {
  483. /* Wait until TCR flag is set */
  484. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout) != HAL_OK)
  485. {
  486. return HAL_TIMEOUT;
  487. }
  488. if(Size > 255U)
  489. {
  490. I2C_TransferConfig(hi2c,DevAddress,255U, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  491. sizetmp = 255U;
  492. }
  493. else
  494. {
  495. I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  496. sizetmp = Size;
  497. }
  498. }
  499. }while(Size > 0U);
  500. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  501. /* Wait until STOPF flag is set */
  502. if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
  503. {
  504. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  505. {
  506. return HAL_ERROR;
  507. }
  508. else
  509. {
  510. return HAL_TIMEOUT;
  511. }
  512. }
  513. /* Clear STOP Flag */
  514. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  515. /* Clear Configuration Register 2 */
  516. __I2C_RESET_CR2(hi2c);
  517. hi2c->State = HAL_I2C_STATE_READY;
  518. /* Process Unlocked */
  519. __HAL_UNLOCK(hi2c);
  520. return HAL_OK;
  521. }
  522. else
  523. {
  524. return HAL_BUSY;
  525. }
  526. }
  527. /**
  528. * @brief Receives in master mode an amount of data in blocking mode.
  529. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  530. * the configuration information for the specified I2C.
  531. * @param DevAddress: Target device address
  532. * @param pData: Pointer to data buffer
  533. * @param Size: Amount of data to be sent
  534. * @param Timeout: Timeout duration
  535. * @retval HAL status
  536. */
  537. HAL_StatusTypeDef HAL_I2C_Master_Receive(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  538. {
  539. uint32_t sizetmp = 0U;
  540. if(hi2c->State == HAL_I2C_STATE_READY)
  541. {
  542. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  543. {
  544. return HAL_BUSY;
  545. }
  546. /* Process Locked */
  547. __HAL_LOCK(hi2c);
  548. hi2c->State = HAL_I2C_STATE_MASTER_BUSY_RX;
  549. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  550. /* Send Slave Address */
  551. /* Set NBYTES to write and reload if size > 255 and generate RESTART */
  552. /* Size > 255, need to set RELOAD bit */
  553. if(Size > 255U)
  554. {
  555. I2C_TransferConfig(hi2c,DevAddress,255U, I2C_RELOAD_MODE, I2C_GENERATE_START_READ);
  556. sizetmp = 255U;
  557. }
  558. else
  559. {
  560. I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
  561. sizetmp = Size;
  562. }
  563. do
  564. {
  565. /* Wait until RXNE flag is set */
  566. if(I2C_WaitOnRXNEFlagUntilTimeout(hi2c, I2C_FLAG_RXNE) != HAL_OK)
  567. {
  568. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  569. {
  570. return HAL_ERROR;
  571. }
  572. else
  573. {
  574. return HAL_TIMEOUT;
  575. }
  576. }
  577. /* Write data to RXDR */
  578. (*pData++) =hi2c->Instance->RXDR;
  579. sizetmp--;
  580. Size--;
  581. if((sizetmp == 0U)&&(Size!=0U))
  582. {
  583. /* Wait until TCR flag is set */
  584. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout) != HAL_OK)
  585. {
  586. return HAL_TIMEOUT;
  587. }
  588. if(Size > 255U)
  589. {
  590. I2C_TransferConfig(hi2c,DevAddress,255U, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  591. sizetmp = 255U;
  592. }
  593. else
  594. {
  595. I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  596. sizetmp = Size;
  597. }
  598. }
  599. }while(Size > 0U);
  600. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  601. /* Wait until STOPF flag is set */
  602. if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  603. {
  604. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  605. {
  606. return HAL_ERROR;
  607. }
  608. else
  609. {
  610. return HAL_TIMEOUT;
  611. }
  612. }
  613. /* Clear STOP Flag */
  614. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  615. /* Clear Configuration Register 2 */
  616. __I2C_RESET_CR2(hi2c);
  617. hi2c->State = HAL_I2C_STATE_READY;
  618. /* Process Unlocked */
  619. __HAL_UNLOCK(hi2c);
  620. return HAL_OK;
  621. }
  622. else
  623. {
  624. return HAL_BUSY;
  625. }
  626. }
  627. /**
  628. * @brief Transmits in slave mode an amount of data in blocking mode.
  629. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  630. * the configuration information for the specified I2C.
  631. * @param pData: Pointer to data buffer
  632. * @param Size: Amount of data to be sent
  633. * @param Timeout: Timeout duration
  634. * @retval HAL status
  635. */
  636. HAL_StatusTypeDef HAL_I2C_Slave_Transmit(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  637. {
  638. if(hi2c->State == HAL_I2C_STATE_READY)
  639. {
  640. if((pData == NULL ) || (Size == 0U))
  641. {
  642. return HAL_ERROR;
  643. }
  644. /* Process Locked */
  645. __HAL_LOCK(hi2c);
  646. hi2c->State = HAL_I2C_STATE_SLAVE_BUSY_RX;
  647. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  648. /* Enable Address Acknowledge */
  649. hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
  650. /* Wait until ADDR flag is set */
  651. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout) != HAL_OK)
  652. {
  653. /* Disable Address Acknowledge */
  654. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  655. return HAL_TIMEOUT;
  656. }
  657. /* Clear ADDR flag */
  658. __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_ADDR);
  659. /* If 10bit addressing mode is selected */
  660. if(hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT)
  661. {
  662. /* Wait until ADDR flag is set */
  663. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout) != HAL_OK)
  664. {
  665. /* Disable Address Acknowledge */
  666. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  667. return HAL_TIMEOUT;
  668. }
  669. /* Clear ADDR flag */
  670. __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_ADDR);
  671. }
  672. /* Wait until DIR flag is set Transmitter mode */
  673. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_DIR, RESET, Timeout) != HAL_OK)
  674. {
  675. /* Disable Address Acknowledge */
  676. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  677. return HAL_TIMEOUT;
  678. }
  679. do
  680. {
  681. /* Wait until TXIS flag is set */
  682. if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
  683. {
  684. /* Disable Address Acknowledge */
  685. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  686. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  687. {
  688. return HAL_ERROR;
  689. }
  690. else
  691. {
  692. return HAL_TIMEOUT;
  693. }
  694. }
  695. /* Read data from TXDR */
  696. hi2c->Instance->TXDR = (*pData++);
  697. Size--;
  698. }while(Size > 0U);
  699. /* Wait until STOP flag is set */
  700. if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  701. {
  702. /* Disable Address Acknowledge */
  703. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  704. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  705. {
  706. /* Normal use case for Transmitter mode */
  707. /* A NACK is generated to confirm the end of transfer */
  708. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  709. }
  710. else
  711. {
  712. return HAL_TIMEOUT;
  713. }
  714. }
  715. /* Clear STOP flag */
  716. __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_STOPF);
  717. /* Wait until BUSY flag is reset */
  718. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, Timeout) != HAL_OK)
  719. {
  720. /* Disable Address Acknowledge */
  721. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  722. return HAL_TIMEOUT;
  723. }
  724. /* Disable Address Acknowledge */
  725. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  726. hi2c->State = HAL_I2C_STATE_READY;
  727. /* Process Unlocked */
  728. __HAL_UNLOCK(hi2c);
  729. return HAL_OK;
  730. }
  731. else
  732. {
  733. return HAL_BUSY;
  734. }
  735. }
  736. /**
  737. * @brief Receive in slave mode an amount of data in blocking mode
  738. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  739. * the configuration information for the specified I2C.
  740. * @param pData: Pointer to data buffer
  741. * @param Size: Amount of data to be sent
  742. * @param Timeout: Timeout duration
  743. * @retval HAL status
  744. */
  745. HAL_StatusTypeDef HAL_I2C_Slave_Receive(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  746. {
  747. if(hi2c->State == HAL_I2C_STATE_READY)
  748. {
  749. if((pData == NULL ) || (Size == 0U))
  750. {
  751. return HAL_ERROR;
  752. }
  753. /* Process Locked */
  754. __HAL_LOCK(hi2c);
  755. hi2c->State = HAL_I2C_STATE_SLAVE_BUSY_RX;
  756. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  757. /* Enable Address Acknowledge */
  758. hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
  759. /* Wait until ADDR flag is set */
  760. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout) != HAL_OK)
  761. {
  762. /* Disable Address Acknowledge */
  763. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  764. return HAL_TIMEOUT;
  765. }
  766. /* Clear ADDR flag */
  767. __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_ADDR);
  768. /* Wait until DIR flag is reset Receiver mode */
  769. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_DIR, SET, Timeout) != HAL_OK)
  770. {
  771. /* Disable Address Acknowledge */
  772. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  773. return HAL_TIMEOUT;
  774. }
  775. while(Size > 0U)
  776. {
  777. /* Wait until RXNE flag is set */
  778. if(I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
  779. {
  780. /* Disable Address Acknowledge */
  781. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  782. /* Store Last receive data if any */
  783. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET)
  784. {
  785. /* Read data from RXDR */
  786. (*pData++) = hi2c->Instance->RXDR;
  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 RXDR */
  798. (*pData++) = hi2c->Instance->RXDR;
  799. Size--;
  800. }
  801. /* Wait until STOP flag is set */
  802. if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  803. {
  804. /* Disable Address Acknowledge */
  805. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  806. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  807. {
  808. return HAL_ERROR;
  809. }
  810. else
  811. {
  812. return HAL_TIMEOUT;
  813. }
  814. }
  815. /* Clear STOP flag */
  816. __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_STOPF);
  817. /* Wait until BUSY flag is reset */
  818. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, Timeout) != HAL_OK)
  819. {
  820. /* Disable Address Acknowledge */
  821. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  822. return HAL_TIMEOUT;
  823. }
  824. /* Disable Address Acknowledge */
  825. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  826. hi2c->State = HAL_I2C_STATE_READY;
  827. /* Process Unlocked */
  828. __HAL_UNLOCK(hi2c);
  829. return HAL_OK;
  830. }
  831. else
  832. {
  833. return HAL_BUSY;
  834. }
  835. }
  836. /**
  837. * @brief Transmit in master mode an amount of data in no-blocking mode with Interrupt
  838. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  839. * the configuration information for the specified I2C.
  840. * @param DevAddress: Target device address
  841. * @param pData: Pointer to data buffer
  842. * @param Size: Amount of data to be sent
  843. * @retval HAL status
  844. */
  845. HAL_StatusTypeDef HAL_I2C_Master_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
  846. {
  847. if(hi2c->State == HAL_I2C_STATE_READY)
  848. {
  849. if((pData == NULL) || (Size == 0U))
  850. {
  851. return HAL_ERROR;
  852. }
  853. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  854. {
  855. return HAL_BUSY;
  856. }
  857. /* Process Locked */
  858. __HAL_LOCK(hi2c);
  859. hi2c->State = HAL_I2C_STATE_MASTER_BUSY_TX;
  860. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  861. hi2c->pBuffPtr = pData;
  862. hi2c->XferCount = Size;
  863. if(Size > 255U)
  864. {
  865. hi2c->XferSize = 255U;
  866. }
  867. else
  868. {
  869. hi2c->XferSize = Size;
  870. }
  871. /* Send Slave Address */
  872. /* Set NBYTES to write and reload if size > 255 and generate RESTART */
  873. if( (hi2c->XferSize == 255U) && (hi2c->XferSize < hi2c->XferCount) )
  874. {
  875. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_GENERATE_START_WRITE);
  876. }
  877. else
  878. {
  879. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_WRITE);
  880. }
  881. /* Process Unlocked */
  882. __HAL_UNLOCK(hi2c);
  883. /* Note : The I2C interrupts must be enabled after unlocking current process
  884. to avoid the risk of I2C interrupt handle execution before current
  885. process unlock */
  886. /* Enable ERR, TC, STOP, NACK, TXI interrupt */
  887. /* possible to enable all of these */
  888. /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
  889. __HAL_I2C_ENABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_TXI );
  890. return HAL_OK;
  891. }
  892. else
  893. {
  894. return HAL_BUSY;
  895. }
  896. }
  897. /**
  898. * @brief Receive in master mode an amount of data in no-blocking mode with Interrupt
  899. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  900. * the configuration information for the specified I2C.
  901. * @param DevAddress: Target device address
  902. * @param pData: Pointer to data buffer
  903. * @param Size: Amount of data to be sent
  904. * @retval HAL status
  905. */
  906. HAL_StatusTypeDef HAL_I2C_Master_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
  907. {
  908. if(hi2c->State == HAL_I2C_STATE_READY)
  909. {
  910. if((pData == NULL) || (Size == 0U))
  911. {
  912. return HAL_ERROR;
  913. }
  914. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  915. {
  916. return HAL_BUSY;
  917. }
  918. /* Process Locked */
  919. __HAL_LOCK(hi2c);
  920. hi2c->State = HAL_I2C_STATE_MASTER_BUSY_RX;
  921. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  922. hi2c->pBuffPtr = pData;
  923. hi2c->XferCount = Size;
  924. if(Size > 255U)
  925. {
  926. hi2c->XferSize = 255U;
  927. }
  928. else
  929. {
  930. hi2c->XferSize = Size;
  931. }
  932. /* Send Slave Address */
  933. /* Set NBYTES to write and reload if size > 255 and generate RESTART */
  934. if( (hi2c->XferSize == 255U) && (hi2c->XferSize < hi2c->XferCount) )
  935. {
  936. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_GENERATE_START_READ);
  937. }
  938. else
  939. {
  940. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
  941. }
  942. /* Process Unlocked */
  943. __HAL_UNLOCK(hi2c);
  944. /* Note : The I2C interrupts must be enabled after unlocking current process
  945. to avoid the risk of I2C interrupt handle execution before current
  946. process unlock */
  947. /* Enable ERR, TC, STOP, NACK, RXI interrupt */
  948. /* possible to enable all of these */
  949. /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
  950. __HAL_I2C_ENABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_RXI );
  951. return HAL_OK;
  952. }
  953. else
  954. {
  955. return HAL_BUSY;
  956. }
  957. }
  958. /**
  959. * @brief Transmit in slave mode an amount of data in no-blocking mode with Interrupt
  960. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  961. * the configuration information for the specified I2C.
  962. * @param pData: Pointer to data buffer
  963. * @param Size: Amount of data to be sent
  964. * @retval HAL status
  965. */
  966. HAL_StatusTypeDef HAL_I2C_Slave_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
  967. {
  968. if(hi2c->State == HAL_I2C_STATE_READY)
  969. {
  970. if((pData == NULL) || (Size == 0U))
  971. {
  972. return HAL_ERROR;
  973. }
  974. /* Process Locked */
  975. __HAL_LOCK(hi2c);
  976. hi2c->State = HAL_I2C_STATE_SLAVE_BUSY_TX;
  977. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  978. /* Enable Address Acknowledge */
  979. hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
  980. hi2c->pBuffPtr = pData;
  981. hi2c->XferSize = Size;
  982. hi2c->XferCount = Size;
  983. /* Process Unlocked */
  984. __HAL_UNLOCK(hi2c);
  985. /* Note : The I2C interrupts must be enabled after unlocking current process
  986. to avoid the risk of I2C interrupt handle execution before current
  987. process unlock */
  988. /* Enable ERR, TC, STOP, NACK, TXI interrupt */
  989. /* possible to enable all of these */
  990. /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
  991. __HAL_I2C_ENABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_TXI );
  992. return HAL_OK;
  993. }
  994. else
  995. {
  996. return HAL_BUSY;
  997. }
  998. }
  999. /**
  1000. * @brief Receive in slave mode an amount of data in no-blocking mode with Interrupt
  1001. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  1002. * the configuration information for the specified I2C.
  1003. * @param pData: Pointer to data buffer
  1004. * @param Size: Amount of data to be sent
  1005. * @retval HAL status
  1006. */
  1007. HAL_StatusTypeDef HAL_I2C_Slave_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
  1008. {
  1009. if(hi2c->State == HAL_I2C_STATE_READY)
  1010. {
  1011. if((pData == NULL) || (Size == 0U))
  1012. {
  1013. return HAL_ERROR;
  1014. }
  1015. /* Process Locked */
  1016. __HAL_LOCK(hi2c);
  1017. hi2c->State = HAL_I2C_STATE_SLAVE_BUSY_RX;
  1018. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1019. /* Enable Address Acknowledge */
  1020. hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
  1021. hi2c->pBuffPtr = pData;
  1022. hi2c->XferSize = Size;
  1023. hi2c->XferCount = Size;
  1024. /* Process Unlocked */
  1025. __HAL_UNLOCK(hi2c);
  1026. /* Note : The I2C interrupts must be enabled after unlocking current process
  1027. to avoid the risk of I2C interrupt handle execution before current
  1028. process unlock */
  1029. /* Enable ERR, TC, STOP, NACK, RXI interrupt */
  1030. /* possible to enable all of these */
  1031. /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
  1032. __HAL_I2C_ENABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI);
  1033. return HAL_OK;
  1034. }
  1035. else
  1036. {
  1037. return HAL_BUSY;
  1038. }
  1039. }
  1040. /**
  1041. * @brief Transmit in master mode an amount of data in no-blocking mode with DMA
  1042. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  1043. * the configuration information for the specified I2C.
  1044. * @param DevAddress: Target device address
  1045. * @param pData: Pointer to data buffer
  1046. * @param Size: Amount of data to be sent
  1047. * @retval HAL status
  1048. */
  1049. HAL_StatusTypeDef HAL_I2C_Master_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
  1050. {
  1051. if(hi2c->State == HAL_I2C_STATE_READY)
  1052. {
  1053. if((pData == NULL) || (Size == 0U))
  1054. {
  1055. return HAL_ERROR;
  1056. }
  1057. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  1058. {
  1059. return HAL_BUSY;
  1060. }
  1061. /* Process Locked */
  1062. __HAL_LOCK(hi2c);
  1063. hi2c->State = HAL_I2C_STATE_MASTER_BUSY_TX;
  1064. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1065. hi2c->pBuffPtr = pData;
  1066. hi2c->XferCount = Size;
  1067. if(Size > 255U)
  1068. {
  1069. hi2c->XferSize = 255U;
  1070. }
  1071. else
  1072. {
  1073. hi2c->XferSize = Size;
  1074. }
  1075. /* Set the I2C DMA transfer complete callback */
  1076. hi2c->hdmatx->XferCpltCallback = I2C_DMAMasterTransmitCplt;
  1077. /* Set the DMA error callback */
  1078. hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
  1079. /* Enable the DMA channel */
  1080. HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);
  1081. /* Send Slave Address */
  1082. /* Set NBYTES to write and reload if size > 255 and generate RESTART */
  1083. if( (hi2c->XferSize == 255U) && (hi2c->XferSize < hi2c->XferCount) )
  1084. {
  1085. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_GENERATE_START_WRITE);
  1086. }
  1087. else
  1088. {
  1089. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_WRITE);
  1090. }
  1091. /* Wait until TXIS flag is set */
  1092. if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, I2C_TIMEOUT_TXIS) != HAL_OK)
  1093. {
  1094. /* Disable Address Acknowledge */
  1095. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  1096. /* Abort DMA */
  1097. HAL_DMA_Abort(hi2c->hdmatx);
  1098. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  1099. {
  1100. return HAL_ERROR;
  1101. }
  1102. else
  1103. {
  1104. return HAL_TIMEOUT;
  1105. }
  1106. }
  1107. /* Enable DMA Request */
  1108. hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
  1109. /* Process Unlocked */
  1110. __HAL_UNLOCK(hi2c);
  1111. return HAL_OK;
  1112. }
  1113. else
  1114. {
  1115. return HAL_BUSY;
  1116. }
  1117. }
  1118. /**
  1119. * @brief Receive in master mode an amount of data in no-blocking mode with DMA
  1120. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  1121. * the configuration information for the specified I2C.
  1122. * @param DevAddress: Target device address
  1123. * @param pData: Pointer to data buffer
  1124. * @param Size: Amount of data to be sent
  1125. * @retval HAL status
  1126. */
  1127. HAL_StatusTypeDef HAL_I2C_Master_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
  1128. {
  1129. if(hi2c->State == HAL_I2C_STATE_READY)
  1130. {
  1131. if((pData == NULL) || (Size == 0U))
  1132. {
  1133. return HAL_ERROR;
  1134. }
  1135. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  1136. {
  1137. return HAL_BUSY;
  1138. }
  1139. /* Process Locked */
  1140. __HAL_LOCK(hi2c);
  1141. hi2c->State = HAL_I2C_STATE_MASTER_BUSY_RX;
  1142. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1143. hi2c->pBuffPtr = pData;
  1144. hi2c->XferCount = Size;
  1145. if(Size > 255U)
  1146. {
  1147. hi2c->XferSize = 255U;
  1148. }
  1149. else
  1150. {
  1151. hi2c->XferSize = Size;
  1152. }
  1153. /* Set the I2C DMA transfer complete callback */
  1154. hi2c->hdmarx->XferCpltCallback = I2C_DMAMasterReceiveCplt;
  1155. /* Set the DMA error callback */
  1156. hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
  1157. /* Enable the DMA channel */
  1158. HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)pData, hi2c->XferSize);
  1159. /* Send Slave Address */
  1160. /* Set NBYTES to write and reload if size > 255 and generate RESTART */
  1161. if( (hi2c->XferSize == 255U) && (hi2c->XferSize < hi2c->XferCount) )
  1162. {
  1163. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_GENERATE_START_READ);
  1164. }
  1165. else
  1166. {
  1167. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
  1168. }
  1169. /* Wait until RXNE flag is set */
  1170. if(I2C_WaitOnRXNEFlagUntilTimeout(hi2c, I2C_FLAG_RXNE) != HAL_OK)
  1171. {
  1172. /* Abort DMA */
  1173. HAL_DMA_Abort(hi2c->hdmarx);
  1174. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  1175. {
  1176. return HAL_ERROR;
  1177. }
  1178. else
  1179. {
  1180. return HAL_TIMEOUT;
  1181. }
  1182. }
  1183. /* Enable DMA Request */
  1184. hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
  1185. /* Process Unlocked */
  1186. __HAL_UNLOCK(hi2c);
  1187. return HAL_OK;
  1188. }
  1189. else
  1190. {
  1191. return HAL_BUSY;
  1192. }
  1193. }
  1194. /**
  1195. * @brief Transmit in slave mode an amount of data in no-blocking mode with DMA
  1196. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  1197. * the configuration information for the specified I2C.
  1198. * @param pData: Pointer to data buffer
  1199. * @param Size: Amount of data to be sent
  1200. * @retval HAL status
  1201. */
  1202. HAL_StatusTypeDef HAL_I2C_Slave_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
  1203. {
  1204. if(hi2c->State == HAL_I2C_STATE_READY)
  1205. {
  1206. if((pData == NULL) || (Size == 0U))
  1207. {
  1208. return HAL_ERROR;
  1209. }
  1210. /* Process Locked */
  1211. __HAL_LOCK(hi2c);
  1212. hi2c->State = HAL_I2C_STATE_SLAVE_BUSY_TX;
  1213. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1214. hi2c->pBuffPtr = pData;
  1215. hi2c->XferCount = Size;
  1216. hi2c->XferSize = Size;
  1217. /* Set the I2C DMA transfer complete callback */
  1218. hi2c->hdmatx->XferCpltCallback = I2C_DMASlaveTransmitCplt;
  1219. /* Set the DMA error callback */
  1220. hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
  1221. /* Enable the DMA channel */
  1222. HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);
  1223. /* Enable Address Acknowledge */
  1224. hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
  1225. /* Wait until ADDR flag is set */
  1226. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, I2C_TIMEOUT_ADDR) != HAL_OK)
  1227. {
  1228. /* Disable Address Acknowledge */
  1229. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  1230. return HAL_TIMEOUT;
  1231. }
  1232. /* Clear ADDR flag */
  1233. __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_ADDR);
  1234. /* If 10bits addressing mode is selected */
  1235. if(hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT)
  1236. {
  1237. /* Wait until ADDR flag is set */
  1238. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, I2C_TIMEOUT_ADDR) != HAL_OK)
  1239. {
  1240. /* Disable Address Acknowledge */
  1241. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  1242. return HAL_TIMEOUT;
  1243. }
  1244. /* Clear ADDR flag */
  1245. __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_ADDR);
  1246. }
  1247. /* Wait until DIR flag is set Transmitter mode */
  1248. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_DIR, RESET, I2C_TIMEOUT_BUSY) != HAL_OK)
  1249. {
  1250. /* Disable Address Acknowledge */
  1251. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  1252. return HAL_TIMEOUT;
  1253. }
  1254. /* Enable DMA Request */
  1255. hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
  1256. /* Process Unlocked */
  1257. __HAL_UNLOCK(hi2c);
  1258. return HAL_OK;
  1259. }
  1260. else
  1261. {
  1262. return HAL_BUSY;
  1263. }
  1264. }
  1265. /**
  1266. * @brief Receive in slave mode an amount of data in no-blocking mode with DMA
  1267. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  1268. * the configuration information for the specified I2C.
  1269. * @param pData: Pointer to data buffer
  1270. * @param Size: Amount of data to be sent
  1271. * @retval HAL status
  1272. */
  1273. HAL_StatusTypeDef HAL_I2C_Slave_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
  1274. {
  1275. if(hi2c->State == HAL_I2C_STATE_READY)
  1276. {
  1277. if((pData == NULL) || (Size == 0U))
  1278. {
  1279. return HAL_ERROR;
  1280. }
  1281. /* Process Locked */
  1282. __HAL_LOCK(hi2c);
  1283. hi2c->State = HAL_I2C_STATE_SLAVE_BUSY_RX;
  1284. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1285. hi2c->pBuffPtr = pData;
  1286. hi2c->XferSize = Size;
  1287. hi2c->XferCount = Size;
  1288. /* Set the I2C DMA transfer complete callback */
  1289. hi2c->hdmarx->XferCpltCallback = I2C_DMASlaveReceiveCplt;
  1290. /* Set the DMA error callback */
  1291. hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
  1292. /* Enable the DMA channel */
  1293. HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)pData, Size);
  1294. /* Enable Address Acknowledge */
  1295. hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
  1296. /* Wait until ADDR flag is set */
  1297. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, I2C_TIMEOUT_ADDR) != HAL_OK)
  1298. {
  1299. /* Disable Address Acknowledge */
  1300. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  1301. return HAL_TIMEOUT;
  1302. }
  1303. /* Clear ADDR flag */
  1304. __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_ADDR);
  1305. /* Wait until DIR flag is set Receiver mode */
  1306. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_DIR, SET, I2C_TIMEOUT_DIR) != HAL_OK)
  1307. {
  1308. /* Disable Address Acknowledge */
  1309. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  1310. return HAL_TIMEOUT;
  1311. }
  1312. /* Enable DMA Request */
  1313. hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
  1314. /* Process Unlocked */
  1315. __HAL_UNLOCK(hi2c);
  1316. return HAL_OK;
  1317. }
  1318. else
  1319. {
  1320. return HAL_BUSY;
  1321. }
  1322. }
  1323. /**
  1324. * @brief Write an amount of data in blocking mode to a specific memory address
  1325. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  1326. * the configuration information for the specified I2C.
  1327. * @param DevAddress: Target device address
  1328. * @param MemAddress: Internal memory address
  1329. * @param MemAddSize: Size of internal memory address
  1330. * @param pData: Pointer to data buffer
  1331. * @param Size: Amount of data to be sent
  1332. * @param Timeout: Timeout duration
  1333. * @retval HAL status
  1334. */
  1335. 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)
  1336. {
  1337. uint32_t Sizetmp = 0U;
  1338. /* Check the parameters */
  1339. assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
  1340. if(hi2c->State == HAL_I2C_STATE_READY)
  1341. {
  1342. if((pData == NULL) || (Size == 0U))
  1343. {
  1344. return HAL_ERROR;
  1345. }
  1346. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  1347. {
  1348. return HAL_BUSY;
  1349. }
  1350. /* Process Locked */
  1351. __HAL_LOCK(hi2c);
  1352. hi2c->State = HAL_I2C_STATE_MEM_BUSY_TX;
  1353. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1354. /* Send Slave Address and Memory Address */
  1355. if(I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, Timeout) != HAL_OK)
  1356. {
  1357. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  1358. {
  1359. /* Process Unlocked */
  1360. __HAL_UNLOCK(hi2c);
  1361. return HAL_ERROR;
  1362. }
  1363. else
  1364. {
  1365. /* Process Unlocked */
  1366. __HAL_UNLOCK(hi2c);
  1367. return HAL_TIMEOUT;
  1368. }
  1369. }
  1370. /* Set NBYTES to write and reload if size > 255 */
  1371. /* Size > 255, need to set RELOAD bit */
  1372. if(Size > 255U)
  1373. {
  1374. I2C_TransferConfig(hi2c,DevAddress,255U, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  1375. Sizetmp = 255U;
  1376. }
  1377. else
  1378. {
  1379. I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  1380. Sizetmp = Size;
  1381. }
  1382. do
  1383. {
  1384. /* Wait until TXIS flag is set */
  1385. if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
  1386. {
  1387. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  1388. {
  1389. return HAL_ERROR;
  1390. }
  1391. else
  1392. {
  1393. return HAL_TIMEOUT;
  1394. }
  1395. }
  1396. /* Write data to DR */
  1397. hi2c->Instance->TXDR = (*pData++);
  1398. Sizetmp--;
  1399. Size--;
  1400. if((Sizetmp == 0U)&&(Size!=0U))
  1401. {
  1402. /* Wait until TCR flag is set */
  1403. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout) != HAL_OK)
  1404. {
  1405. return HAL_TIMEOUT;
  1406. }
  1407. if(Size > 255U)
  1408. {
  1409. I2C_TransferConfig(hi2c,DevAddress,255U, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  1410. Sizetmp = 255U;
  1411. }
  1412. else
  1413. {
  1414. I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  1415. Sizetmp = Size;
  1416. }
  1417. }
  1418. }while(Size > 0U);
  1419. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  1420. /* Wait until STOPF flag is reset */
  1421. if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  1422. {
  1423. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  1424. {
  1425. return HAL_ERROR;
  1426. }
  1427. else
  1428. {
  1429. return HAL_TIMEOUT;
  1430. }
  1431. }
  1432. /* Clear STOP Flag */
  1433. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  1434. /* Clear Configuration Register 2 */
  1435. __I2C_RESET_CR2(hi2c);
  1436. hi2c->State = HAL_I2C_STATE_READY;
  1437. /* Process Unlocked */
  1438. __HAL_UNLOCK(hi2c);
  1439. return HAL_OK;
  1440. }
  1441. else
  1442. {
  1443. return HAL_BUSY;
  1444. }
  1445. }
  1446. /**
  1447. * @brief Read an amount of data in blocking mode from a specific memory address
  1448. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  1449. * the configuration information for the specified I2C.
  1450. * @param DevAddress: Target device address
  1451. * @param MemAddress: Internal memory address
  1452. * @param MemAddSize: Size of internal memory address
  1453. * @param pData: Pointer to data buffer
  1454. * @param Size: Amount of data to be sent
  1455. * @param Timeout: Timeout duration
  1456. * @retval HAL status
  1457. */
  1458. 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)
  1459. {
  1460. uint32_t Sizetmp = 0U;
  1461. /* Check the parameters */
  1462. assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
  1463. if(hi2c->State == HAL_I2C_STATE_READY)
  1464. {
  1465. if((pData == NULL) || (Size == 0U))
  1466. {
  1467. return HAL_ERROR;
  1468. }
  1469. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  1470. {
  1471. return HAL_BUSY;
  1472. }
  1473. /* Process Locked */
  1474. __HAL_LOCK(hi2c);
  1475. hi2c->State = HAL_I2C_STATE_MEM_BUSY_RX;
  1476. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1477. /* Send Slave Address and Memory Address */
  1478. if(I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, Timeout) != HAL_OK)
  1479. {
  1480. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  1481. {
  1482. /* Process Unlocked */
  1483. __HAL_UNLOCK(hi2c);
  1484. return HAL_ERROR;
  1485. }
  1486. else
  1487. {
  1488. /* Process Unlocked */
  1489. __HAL_UNLOCK(hi2c);
  1490. return HAL_TIMEOUT;
  1491. }
  1492. }
  1493. /* Send Slave Address */
  1494. /* Set NBYTES to write and reload if size > 255 and generate RESTART */
  1495. /* Size > 255, need to set RELOAD bit */
  1496. if(Size > 255U)
  1497. {
  1498. I2C_TransferConfig(hi2c,DevAddress,255U, I2C_RELOAD_MODE, I2C_GENERATE_START_READ);
  1499. Sizetmp = 255U;
  1500. }
  1501. else
  1502. {
  1503. I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
  1504. Sizetmp = Size;
  1505. }
  1506. do
  1507. {
  1508. /* Wait until RXNE flag is set */
  1509. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, Timeout) != HAL_OK)
  1510. {
  1511. return HAL_TIMEOUT;
  1512. }
  1513. /* Read data from RXDR */
  1514. (*pData++) = hi2c->Instance->RXDR;
  1515. /* Decrement the Size counter */
  1516. Sizetmp--;
  1517. Size--;
  1518. if((Sizetmp == 0U)&&(Size!=0U))
  1519. {
  1520. /* Wait until TCR flag is set */
  1521. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout) != HAL_OK)
  1522. {
  1523. return HAL_TIMEOUT;
  1524. }
  1525. if(Size > 255U)
  1526. {
  1527. I2C_TransferConfig(hi2c,DevAddress,255U, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  1528. Sizetmp = 255U;
  1529. }
  1530. else
  1531. {
  1532. I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  1533. Sizetmp = Size;
  1534. }
  1535. }
  1536. }while(Size > 0U);
  1537. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  1538. /* Wait until STOPF flag is reset */
  1539. if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  1540. {
  1541. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  1542. {
  1543. return HAL_ERROR;
  1544. }
  1545. else
  1546. {
  1547. return HAL_TIMEOUT;
  1548. }
  1549. }
  1550. /* Clear STOP Flag */
  1551. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  1552. /* Clear Configuration Register 2 */
  1553. __I2C_RESET_CR2(hi2c);
  1554. hi2c->State = HAL_I2C_STATE_READY;
  1555. /* Process Unlocked */
  1556. __HAL_UNLOCK(hi2c);
  1557. return HAL_OK;
  1558. }
  1559. else
  1560. {
  1561. return HAL_BUSY;
  1562. }
  1563. }
  1564. /**
  1565. * @brief Write an amount of data in no-blocking mode with Interrupt to a specific memory address
  1566. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  1567. * the configuration information for the specified I2C.
  1568. * @param DevAddress: Target device address
  1569. * @param MemAddress: Internal memory address
  1570. * @param MemAddSize: Size of internal memory address
  1571. * @param pData: Pointer to data buffer
  1572. * @param Size: Amount of data to be sent
  1573. * @retval HAL status
  1574. */
  1575. 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)
  1576. {
  1577. /* Check the parameters */
  1578. assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
  1579. if(hi2c->State == HAL_I2C_STATE_READY)
  1580. {
  1581. if((pData == NULL) || (Size == 0U))
  1582. {
  1583. return HAL_ERROR;
  1584. }
  1585. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  1586. {
  1587. return HAL_BUSY;
  1588. }
  1589. /* Process Locked */
  1590. __HAL_LOCK(hi2c);
  1591. hi2c->State = HAL_I2C_STATE_MEM_BUSY_TX;
  1592. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1593. hi2c->pBuffPtr = pData;
  1594. hi2c->XferCount = Size;
  1595. if(Size > 255U)
  1596. {
  1597. hi2c->XferSize = 255U;
  1598. }
  1599. else
  1600. {
  1601. hi2c->XferSize = Size;
  1602. }
  1603. /* Send Slave Address and Memory Address */
  1604. if(I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG) != HAL_OK)
  1605. {
  1606. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  1607. {
  1608. /* Process Unlocked */
  1609. __HAL_UNLOCK(hi2c);
  1610. return HAL_ERROR;
  1611. }
  1612. else
  1613. {
  1614. /* Process Unlocked */
  1615. __HAL_UNLOCK(hi2c);
  1616. return HAL_TIMEOUT;
  1617. }
  1618. }
  1619. /* Set NBYTES to write and reload if size > 255 */
  1620. /* Size > 255, need to set RELOAD bit */
  1621. if( (hi2c->XferSize == 255U) && (hi2c->XferSize < hi2c->XferCount) )
  1622. {
  1623. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  1624. }
  1625. else
  1626. {
  1627. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  1628. }
  1629. /* Process Unlocked */
  1630. __HAL_UNLOCK(hi2c);
  1631. /* Note : The I2C interrupts must be enabled after unlocking current process
  1632. to avoid the risk of I2C interrupt handle execution before current
  1633. process unlock */
  1634. /* Enable ERR, TC, STOP, NACK, TXI interrupt */
  1635. /* possible to enable all of these */
  1636. /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
  1637. __HAL_I2C_ENABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_TXI );
  1638. return HAL_OK;
  1639. }
  1640. else
  1641. {
  1642. return HAL_BUSY;
  1643. }
  1644. }
  1645. /**
  1646. * @brief Read an amount of data in no-blocking mode with Interrupt from a specific memory address
  1647. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  1648. * the configuration information for the specified I2C.
  1649. * @param DevAddress: Target device address
  1650. * @param MemAddress: Internal memory address
  1651. * @param MemAddSize: Size of internal memory address
  1652. * @param pData: Pointer to data buffer
  1653. * @param Size: Amount of data to be sent
  1654. * @retval HAL status
  1655. */
  1656. 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)
  1657. {
  1658. /* Check the parameters */
  1659. assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
  1660. if(hi2c->State == HAL_I2C_STATE_READY)
  1661. {
  1662. if((pData == NULL) || (Size == 0U))
  1663. {
  1664. return HAL_ERROR;
  1665. }
  1666. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  1667. {
  1668. return HAL_BUSY;
  1669. }
  1670. /* Process Locked */
  1671. __HAL_LOCK(hi2c);
  1672. hi2c->State = HAL_I2C_STATE_MEM_BUSY_RX;
  1673. hi2c->pBuffPtr = pData;
  1674. hi2c->XferCount = Size;
  1675. if(Size > 255U)
  1676. {
  1677. hi2c->XferSize = 255U;
  1678. }
  1679. else
  1680. {
  1681. hi2c->XferSize = Size;
  1682. }
  1683. /* Send Slave Address and Memory Address */
  1684. if(I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG) != HAL_OK)
  1685. {
  1686. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  1687. {
  1688. /* Process Unlocked */
  1689. __HAL_UNLOCK(hi2c);
  1690. return HAL_ERROR;
  1691. }
  1692. else
  1693. {
  1694. /* Process Unlocked */
  1695. __HAL_UNLOCK(hi2c);
  1696. return HAL_TIMEOUT;
  1697. }
  1698. }
  1699. /* Set NBYTES to write and reload if size > 255 and generate RESTART */
  1700. /* Size > 255, need to set RELOAD bit */
  1701. if( (hi2c->XferSize == 255U) && (hi2c->XferSize < hi2c->XferCount) )
  1702. {
  1703. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_GENERATE_START_READ);
  1704. }
  1705. else
  1706. {
  1707. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
  1708. }
  1709. /* Process Unlocked */
  1710. __HAL_UNLOCK(hi2c);
  1711. /* Note : The I2C interrupts must be enabled after unlocking current process
  1712. to avoid the risk of I2C interrupt handle execution before current
  1713. process unlock */
  1714. /* Enable ERR, TC, STOP, NACK, RXI interrupt */
  1715. /* possible to enable all of these */
  1716. /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
  1717. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_RXI );
  1718. return HAL_OK;
  1719. }
  1720. else
  1721. {
  1722. return HAL_BUSY;
  1723. }
  1724. }
  1725. /**
  1726. * @brief Write an amount of data in no-blocking mode with DMA to a specific memory address
  1727. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  1728. * the configuration information for the specified I2C.
  1729. * @param DevAddress: Target device address
  1730. * @param MemAddress: Internal memory address
  1731. * @param MemAddSize: Size of internal memory address
  1732. * @param pData: Pointer to data buffer
  1733. * @param Size: Amount of data to be sent
  1734. * @retval HAL status
  1735. */
  1736. 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)
  1737. {
  1738. /* Check the parameters */
  1739. assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
  1740. if(hi2c->State == HAL_I2C_STATE_READY)
  1741. {
  1742. if((pData == NULL) || (Size == 0U))
  1743. {
  1744. return HAL_ERROR;
  1745. }
  1746. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  1747. {
  1748. return HAL_BUSY;
  1749. }
  1750. /* Process Locked */
  1751. __HAL_LOCK(hi2c);
  1752. hi2c->State = HAL_I2C_STATE_MEM_BUSY_TX;
  1753. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1754. hi2c->pBuffPtr = pData;
  1755. hi2c->XferCount = Size;
  1756. if(Size > 255U)
  1757. {
  1758. hi2c->XferSize = 255U;
  1759. }
  1760. else
  1761. {
  1762. hi2c->XferSize = Size;
  1763. }
  1764. /* Set the I2C DMA transfer complete callback */
  1765. hi2c->hdmatx->XferCpltCallback = I2C_DMAMemTransmitCplt;
  1766. /* Set the DMA error callback */
  1767. hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
  1768. /* Enable the DMA channel */
  1769. HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);
  1770. /* Send Slave Address and Memory Address */
  1771. if(I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG) != HAL_OK)
  1772. {
  1773. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  1774. {
  1775. /* Process Unlocked */
  1776. __HAL_UNLOCK(hi2c);
  1777. return HAL_ERROR;
  1778. }
  1779. else
  1780. {
  1781. /* Process Unlocked */
  1782. __HAL_UNLOCK(hi2c);
  1783. return HAL_TIMEOUT;
  1784. }
  1785. }
  1786. /* Send Slave Address */
  1787. /* Set NBYTES to write and reload if size > 255 */
  1788. if( (hi2c->XferSize == 255U) && (hi2c->XferSize < hi2c->XferCount) )
  1789. {
  1790. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  1791. }
  1792. else
  1793. {
  1794. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  1795. }
  1796. /* Wait until TXIS flag is set */
  1797. if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, I2C_TIMEOUT_TXIS) != HAL_OK)
  1798. {
  1799. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  1800. {
  1801. return HAL_ERROR;
  1802. }
  1803. else
  1804. {
  1805. return HAL_TIMEOUT;
  1806. }
  1807. }
  1808. /* Enable DMA Request */
  1809. hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
  1810. /* Process Unlocked */
  1811. __HAL_UNLOCK(hi2c);
  1812. return HAL_OK;
  1813. }
  1814. else
  1815. {
  1816. return HAL_BUSY;
  1817. }
  1818. }
  1819. /**
  1820. * @brief Reads an amount of data in no-blocking mode with DMA from a specific memory address.
  1821. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  1822. * the configuration information for the specified I2C.
  1823. * @param DevAddress: Target device address
  1824. * @param MemAddress: Internal memory address
  1825. * @param MemAddSize: Size of internal memory address
  1826. * @param pData: Pointer to data buffer
  1827. * @param Size: Amount of data to be read
  1828. * @retval HAL status
  1829. */
  1830. 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)
  1831. {
  1832. /* Check the parameters */
  1833. assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
  1834. if(hi2c->State == HAL_I2C_STATE_READY)
  1835. {
  1836. if((pData == NULL) || (Size == 0U))
  1837. {
  1838. return HAL_ERROR;
  1839. }
  1840. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  1841. {
  1842. return HAL_BUSY;
  1843. }
  1844. /* Process Locked */
  1845. __HAL_LOCK(hi2c);
  1846. hi2c->State = HAL_I2C_STATE_MEM_BUSY_RX;
  1847. hi2c->pBuffPtr = pData;
  1848. hi2c->XferCount = Size;
  1849. if(Size > 255U)
  1850. {
  1851. hi2c->XferSize = 255U;
  1852. }
  1853. else
  1854. {
  1855. hi2c->XferSize = Size;
  1856. }
  1857. /* Set the I2C DMA transfer complete callback */
  1858. hi2c->hdmarx->XferCpltCallback = I2C_DMAMemReceiveCplt;
  1859. /* Set the DMA error callback */
  1860. hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
  1861. /* Enable the DMA channel */
  1862. HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)pData, hi2c->XferSize);
  1863. /* Send Slave Address and Memory Address */
  1864. if(I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG) != HAL_OK)
  1865. {
  1866. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  1867. {
  1868. /* Process Unlocked */
  1869. __HAL_UNLOCK(hi2c);
  1870. return HAL_ERROR;
  1871. }
  1872. else
  1873. {
  1874. /* Process Unlocked */
  1875. __HAL_UNLOCK(hi2c);
  1876. return HAL_TIMEOUT;
  1877. }
  1878. }
  1879. /* Set NBYTES to write and reload if size > 255 and generate RESTART */
  1880. if( (hi2c->XferSize == 255U) && (hi2c->XferSize < hi2c->XferCount) )
  1881. {
  1882. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_GENERATE_START_READ);
  1883. }
  1884. else
  1885. {
  1886. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
  1887. }
  1888. /* Wait until RXNE flag is set */
  1889. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, I2C_TIMEOUT_RXNE) != HAL_OK)
  1890. {
  1891. return HAL_TIMEOUT;
  1892. }
  1893. /* Enable DMA Request */
  1894. hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
  1895. /* Process Unlocked */
  1896. __HAL_UNLOCK(hi2c);
  1897. return HAL_OK;
  1898. }
  1899. else
  1900. {
  1901. return HAL_BUSY;
  1902. }
  1903. }
  1904. /**
  1905. * @brief Checks if target device is ready for communication.
  1906. * @note This function is used with Memory devices
  1907. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  1908. * the configuration information for the specified I2C.
  1909. * @param DevAddress: Target device address
  1910. * @param Trials: Number of trials
  1911. * @param Timeout: Timeout duration
  1912. * @retval HAL status
  1913. */
  1914. HAL_StatusTypeDef HAL_I2C_IsDeviceReady(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Trials, uint32_t Timeout)
  1915. {
  1916. uint32_t tickstart = 0U;
  1917. __IO uint32_t I2C_Trials = 0U;
  1918. if(hi2c->State == HAL_I2C_STATE_READY)
  1919. {
  1920. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  1921. {
  1922. return HAL_BUSY;
  1923. }
  1924. /* Process Locked */
  1925. __HAL_LOCK(hi2c);
  1926. hi2c->State = HAL_I2C_STATE_BUSY;
  1927. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1928. do
  1929. {
  1930. /* Generate Start */
  1931. hi2c->Instance->CR2 = __I2C_GENERATE_START(hi2c->Init.AddressingMode,DevAddress);
  1932. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  1933. /* Wait until STOPF flag is set or a NACK flag is set*/
  1934. tickstart = HAL_GetTick();
  1935. while((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET) && (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == RESET) && (hi2c->State != HAL_I2C_STATE_TIMEOUT))
  1936. {
  1937. if(Timeout != HAL_MAX_DELAY)
  1938. {
  1939. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  1940. {
  1941. /* Device is ready */
  1942. hi2c->State = HAL_I2C_STATE_READY;
  1943. /* Process Unlocked */
  1944. __HAL_UNLOCK(hi2c);
  1945. return HAL_TIMEOUT;
  1946. }
  1947. }
  1948. }
  1949. /* Check if the NACKF flag has not been set */
  1950. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == RESET)
  1951. {
  1952. /* Wait until STOPF flag is reset */
  1953. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_STOPF, RESET, Timeout) != HAL_OK)
  1954. {
  1955. return HAL_TIMEOUT;
  1956. }
  1957. /* Clear STOP Flag */
  1958. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  1959. /* Device is ready */
  1960. hi2c->State = HAL_I2C_STATE_READY;
  1961. /* Process Unlocked */
  1962. __HAL_UNLOCK(hi2c);
  1963. return HAL_OK;
  1964. }
  1965. else
  1966. {
  1967. /* Wait until STOPF flag is reset */
  1968. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_STOPF, RESET, Timeout) != HAL_OK)
  1969. {
  1970. return HAL_TIMEOUT;
  1971. }
  1972. /* Clear NACK Flag */
  1973. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  1974. /* Clear STOP Flag, auto generated with autoend*/
  1975. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  1976. }
  1977. /* Check if the maximum allowed number of trials has been reached */
  1978. if (I2C_Trials++ == Trials)
  1979. {
  1980. /* Generate Stop */
  1981. hi2c->Instance->CR2 |= I2C_CR2_STOP;
  1982. /* Wait until STOPF flag is reset */
  1983. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_STOPF, RESET, Timeout) != HAL_OK)
  1984. {
  1985. return HAL_TIMEOUT;
  1986. }
  1987. /* Clear STOP Flag */
  1988. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  1989. }
  1990. }while(I2C_Trials < Trials);
  1991. hi2c->State = HAL_I2C_STATE_READY;
  1992. /* Process Unlocked */
  1993. __HAL_UNLOCK(hi2c);
  1994. return HAL_TIMEOUT;
  1995. }
  1996. else
  1997. {
  1998. return HAL_BUSY;
  1999. }
  2000. }
  2001. /**
  2002. * @}
  2003. */
  2004. /** @addtogroup IRQ_Handler_and_Callbacks
  2005. * @{
  2006. */
  2007. /**
  2008. * @brief This function handles I2C event interrupt request.
  2009. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  2010. * the configuration information for the specified I2C.
  2011. * @retval None
  2012. */
  2013. void HAL_I2C_EV_IRQHandler(I2C_HandleTypeDef *hi2c)
  2014. {
  2015. /* I2C in mode Transmitter ---------------------------------------------------*/
  2016. if (((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXIS) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TCR) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TC) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR) == SET)) && (__HAL_I2C_GET_IT_SOURCE(hi2c, (I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_TXI | I2C_IT_ADDRI)) == SET))
  2017. {
  2018. /* Slave mode selected */
  2019. if (hi2c->State == HAL_I2C_STATE_SLAVE_BUSY_TX)
  2020. {
  2021. I2C_SlaveTransmit_ISR(hi2c);
  2022. }
  2023. }
  2024. if (((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXIS) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TCR) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TC) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)) && (__HAL_I2C_GET_IT_SOURCE(hi2c, (I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_TXI)) == SET))
  2025. {
  2026. /* Master mode selected */
  2027. if ((hi2c->State == HAL_I2C_STATE_MASTER_BUSY_TX) || (hi2c->State == HAL_I2C_STATE_MEM_BUSY_TX))
  2028. {
  2029. I2C_MasterTransmit_ISR(hi2c);
  2030. }
  2031. }
  2032. /* I2C in mode Receiver ----------------------------------------------------*/
  2033. if (((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TCR) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TC) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR) == SET)) && (__HAL_I2C_GET_IT_SOURCE(hi2c, (I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_RXI | I2C_IT_ADDRI)) == SET))
  2034. {
  2035. /* Slave mode selected */
  2036. if (hi2c->State == HAL_I2C_STATE_SLAVE_BUSY_RX)
  2037. {
  2038. I2C_SlaveReceive_ISR(hi2c);
  2039. }
  2040. }
  2041. if (((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TCR) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TC) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)) && (__HAL_I2C_GET_IT_SOURCE(hi2c, (I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_RXI)) == SET))
  2042. {
  2043. /* Master mode selected */
  2044. if ((hi2c->State == HAL_I2C_STATE_MASTER_BUSY_RX) || (hi2c->State == HAL_I2C_STATE_MEM_BUSY_RX))
  2045. {
  2046. I2C_MasterReceive_ISR(hi2c);
  2047. }
  2048. }
  2049. }
  2050. /**
  2051. * @brief This function handles I2C error interrupt request.
  2052. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  2053. * the configuration information for the specified I2C.
  2054. * @retval None
  2055. */
  2056. void HAL_I2C_ER_IRQHandler(I2C_HandleTypeDef *hi2c)
  2057. {
  2058. /* I2C Bus error interrupt occurred ------------------------------------*/
  2059. if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BERR) == SET) && (__HAL_I2C_GET_IT_SOURCE(hi2c, I2C_IT_ERRI) == SET))
  2060. {
  2061. hi2c->ErrorCode |= HAL_I2C_ERROR_BERR;
  2062. /* Clear BERR flag */
  2063. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_BERR);
  2064. }
  2065. /* I2C Over-Run/Under-Run interrupt occurred ----------------------------------------*/
  2066. if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_OVR) == SET) && (__HAL_I2C_GET_IT_SOURCE(hi2c, I2C_IT_ERRI) == SET))
  2067. {
  2068. hi2c->ErrorCode |= HAL_I2C_ERROR_OVR;
  2069. /* Clear OVR flag */
  2070. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_OVR);
  2071. }
  2072. /* I2C Arbitration Loss error interrupt occurred -------------------------------------*/
  2073. if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ARLO) == SET) && (__HAL_I2C_GET_IT_SOURCE(hi2c, I2C_IT_ERRI) == SET))
  2074. {
  2075. hi2c->ErrorCode |= HAL_I2C_ERROR_ARLO;
  2076. /* Clear ARLO flag */
  2077. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ARLO);
  2078. }
  2079. /* Call the Error Callback in case of Error detected */
  2080. if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  2081. {
  2082. hi2c->State = HAL_I2C_STATE_READY;
  2083. HAL_I2C_ErrorCallback(hi2c);
  2084. }
  2085. }
  2086. /**
  2087. * @brief Master Tx Transfer completed callbacks.
  2088. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  2089. * the configuration information for the specified I2C.
  2090. * @retval None
  2091. */
  2092. __weak void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c)
  2093. {
  2094. /* Prevent unused argument(s) compilation warning */
  2095. UNUSED(hi2c);
  2096. /* NOTE : This function Should not be modified, when the callback is needed,
  2097. the HAL_I2C_TxCpltCallback could be implemented in the user file
  2098. */
  2099. }
  2100. /**
  2101. * @brief Master Rx Transfer completed callbacks.
  2102. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  2103. * the configuration information for the specified I2C.
  2104. * @retval None
  2105. */
  2106. __weak void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c)
  2107. {
  2108. /* Prevent unused argument(s) compilation warning */
  2109. UNUSED(hi2c);
  2110. /* NOTE : This function Should not be modified, when the callback is needed,
  2111. the HAL_I2C_TxCpltCallback could be implemented in the user file
  2112. */
  2113. }
  2114. /** @brief Slave Tx Transfer completed callbacks.
  2115. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  2116. * the configuration information for the specified I2C.
  2117. * @retval None
  2118. */
  2119. __weak void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *hi2c)
  2120. {
  2121. /* Prevent unused argument(s) compilation warning */
  2122. UNUSED(hi2c);
  2123. /* NOTE : This function Should not be modified, when the callback is needed,
  2124. the HAL_I2C_TxCpltCallback could be implemented in the user file
  2125. */
  2126. }
  2127. /**
  2128. * @brief Slave Rx Transfer completed callbacks.
  2129. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  2130. * the configuration information for the specified I2C.
  2131. * @retval None
  2132. */
  2133. __weak void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c)
  2134. {
  2135. /* Prevent unused argument(s) compilation warning */
  2136. UNUSED(hi2c);
  2137. /* NOTE : This function Should not be modified, when the callback is needed,
  2138. the HAL_I2C_TxCpltCallback could be implemented in the user file
  2139. */
  2140. }
  2141. /**
  2142. * @brief Memory Tx Transfer completed callbacks.
  2143. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  2144. * the configuration information for the specified I2C.
  2145. * @retval None
  2146. */
  2147. __weak void HAL_I2C_MemTxCpltCallback(I2C_HandleTypeDef *hi2c)
  2148. {
  2149. /* Prevent unused argument(s) compilation warning */
  2150. UNUSED(hi2c);
  2151. /* NOTE : This function Should not be modified, when the callback is needed,
  2152. the HAL_I2C_TxCpltCallback could be implemented in the user file
  2153. */
  2154. }
  2155. /**
  2156. * @brief Memory Rx Transfer completed callbacks.
  2157. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  2158. * the configuration information for the specified I2C.
  2159. * @retval None
  2160. */
  2161. __weak void HAL_I2C_MemRxCpltCallback(I2C_HandleTypeDef *hi2c)
  2162. {
  2163. /* Prevent unused argument(s) compilation warning */
  2164. UNUSED(hi2c);
  2165. /* NOTE : This function Should not be modified, when the callback is needed,
  2166. the HAL_I2C_TxCpltCallback could be implemented in the user file
  2167. */
  2168. }
  2169. /**
  2170. * @brief I2C error callbacks.
  2171. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  2172. * the configuration information for the specified I2C.
  2173. * @retval None
  2174. */
  2175. __weak void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c)
  2176. {
  2177. /* Prevent unused argument(s) compilation warning */
  2178. UNUSED(hi2c);
  2179. /* NOTE : This function Should not be modified, when the callback is needed,
  2180. the HAL_I2C_ErrorCallback could be implemented in the user file
  2181. */
  2182. }
  2183. /**
  2184. * @}
  2185. */
  2186. /** @addtogroup I2C_Exported_Functions_Group3
  2187. * @brief Peripheral State and Errors functions
  2188. *
  2189. @verbatim
  2190. ===============================================================================
  2191. ##### Peripheral State and Errors functions #####
  2192. ===============================================================================
  2193. [..]
  2194. This subsection permit to get in run-time the status of the peripheral
  2195. and the data flow.
  2196. @endverbatim
  2197. * @{
  2198. */
  2199. /**
  2200. * @brief Returns the I2C state.
  2201. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  2202. * the configuration information for the specified I2C.
  2203. * @retval HAL state
  2204. */
  2205. HAL_I2C_StateTypeDef HAL_I2C_GetState(I2C_HandleTypeDef *hi2c)
  2206. {
  2207. return hi2c->State;
  2208. }
  2209. /**
  2210. * @brief Return the I2C error code
  2211. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  2212. * the configuration information for the specified I2C.
  2213. * @retval I2C Error Code
  2214. */
  2215. uint32_t HAL_I2C_GetError(I2C_HandleTypeDef *hi2c)
  2216. {
  2217. return hi2c->ErrorCode;
  2218. }
  2219. /**
  2220. * @}
  2221. */
  2222. /**
  2223. * @}
  2224. */
  2225. /** @addtogroup I2C_Private
  2226. * @{
  2227. */
  2228. /**
  2229. * @brief Handle Interrupt Flags Master Transmit Mode
  2230. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  2231. * the configuration information for the specified I2C.
  2232. * @retval HAL status
  2233. */
  2234. static HAL_StatusTypeDef I2C_MasterTransmit_ISR(I2C_HandleTypeDef *hi2c)
  2235. {
  2236. uint16_t DevAddress;
  2237. /* Process Locked */
  2238. __HAL_LOCK(hi2c);
  2239. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXIS) == SET)
  2240. {
  2241. /* Write data to TXDR */
  2242. hi2c->Instance->TXDR = (*hi2c->pBuffPtr++);
  2243. hi2c->XferSize--;
  2244. hi2c->XferCount--;
  2245. }
  2246. else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TCR) == SET)
  2247. {
  2248. if((hi2c->XferSize == 0U)&&(hi2c->XferCount!=0U))
  2249. {
  2250. DevAddress = (hi2c->Instance->CR2 & I2C_CR2_SADD);
  2251. if(hi2c->XferCount > 255U)
  2252. {
  2253. I2C_TransferConfig(hi2c,DevAddress,255U, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  2254. hi2c->XferSize = 255U;
  2255. }
  2256. else
  2257. {
  2258. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferCount, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  2259. hi2c->XferSize = hi2c->XferCount;
  2260. }
  2261. }
  2262. else
  2263. {
  2264. /* Process Unlocked */
  2265. __HAL_UNLOCK(hi2c);
  2266. /* Wrong size Status regarding TCR flag event */
  2267. hi2c->ErrorCode |= HAL_I2C_ERROR_SIZE;
  2268. HAL_I2C_ErrorCallback(hi2c);
  2269. }
  2270. }
  2271. else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TC) == SET)
  2272. {
  2273. if(hi2c->XferCount == 0U)
  2274. {
  2275. /* Generate Stop */
  2276. hi2c->Instance->CR2 |= I2C_CR2_STOP;
  2277. }
  2278. else
  2279. {
  2280. /* Process Unlocked */
  2281. __HAL_UNLOCK(hi2c);
  2282. /* Wrong size Status regarding TCR flag event */
  2283. hi2c->ErrorCode |= HAL_I2C_ERROR_SIZE;
  2284. HAL_I2C_ErrorCallback(hi2c);
  2285. }
  2286. }
  2287. else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET)
  2288. {
  2289. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
  2290. {
  2291. /* Clear NACK Flag */
  2292. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  2293. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  2294. }
  2295. /* Disable ERR, TC, STOP, NACK, TXI interrupts */
  2296. __HAL_I2C_DISABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_TXI );
  2297. /* Clear STOP Flag */
  2298. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  2299. /* Clear Configuration Register 2 */
  2300. __I2C_RESET_CR2(hi2c);
  2301. /* Flush TX register if not empty */
  2302. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXE) == RESET)
  2303. {
  2304. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_TXE);
  2305. }
  2306. /* Call the correct callback to inform upper layer */
  2307. if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  2308. {
  2309. hi2c->State = HAL_I2C_STATE_READY;
  2310. /* Process Unlocked */
  2311. __HAL_UNLOCK(hi2c);
  2312. HAL_I2C_ErrorCallback(hi2c);
  2313. }
  2314. else
  2315. {
  2316. if(hi2c->State == HAL_I2C_STATE_MEM_BUSY_TX)
  2317. {
  2318. hi2c->State = HAL_I2C_STATE_READY;
  2319. /* Process Unlocked */
  2320. __HAL_UNLOCK(hi2c);
  2321. HAL_I2C_MemTxCpltCallback(hi2c);
  2322. }
  2323. else
  2324. {
  2325. hi2c->State = HAL_I2C_STATE_READY;
  2326. /* Process Unlocked */
  2327. __HAL_UNLOCK(hi2c);
  2328. HAL_I2C_MasterTxCpltCallback(hi2c);
  2329. }
  2330. }
  2331. }
  2332. else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
  2333. {
  2334. /* Clear NACK Flag */
  2335. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  2336. /* Process Unlocked */
  2337. __HAL_UNLOCK(hi2c);
  2338. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  2339. HAL_I2C_ErrorCallback(hi2c);
  2340. }
  2341. /* Process Unlocked */
  2342. __HAL_UNLOCK(hi2c);
  2343. return HAL_OK;
  2344. }
  2345. /**
  2346. * @brief Handle Interrupt Flags Master Receive Mode
  2347. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  2348. * the configuration information for the specified I2C.
  2349. * @retval HAL status
  2350. */
  2351. static HAL_StatusTypeDef I2C_MasterReceive_ISR(I2C_HandleTypeDef *hi2c)
  2352. {
  2353. uint16_t DevAddress;
  2354. /* Process Locked */
  2355. __HAL_LOCK(hi2c);
  2356. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET)
  2357. {
  2358. /* Read data from RXDR */
  2359. (*hi2c->pBuffPtr++) = hi2c->Instance->RXDR;
  2360. hi2c->XferSize--;
  2361. hi2c->XferCount--;
  2362. }
  2363. else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TCR) == SET)
  2364. {
  2365. if((hi2c->XferSize == 0U)&&(hi2c->XferCount!=0U))
  2366. {
  2367. DevAddress = (hi2c->Instance->CR2 & I2C_CR2_SADD);
  2368. if(hi2c->XferCount > 255U)
  2369. {
  2370. I2C_TransferConfig(hi2c,DevAddress,255U, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  2371. hi2c->XferSize = 255U;
  2372. }
  2373. else
  2374. {
  2375. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferCount, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  2376. hi2c->XferSize = hi2c->XferCount;
  2377. }
  2378. }
  2379. else
  2380. {
  2381. /* Process Unlocked */
  2382. __HAL_UNLOCK(hi2c);
  2383. /* Wrong size Status regarding TCR flag event */
  2384. hi2c->ErrorCode |= HAL_I2C_ERROR_SIZE;
  2385. HAL_I2C_ErrorCallback(hi2c);
  2386. }
  2387. }
  2388. else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TC) == SET)
  2389. {
  2390. if(hi2c->XferCount == 0U)
  2391. {
  2392. /* Generate Stop */
  2393. hi2c->Instance->CR2 |= I2C_CR2_STOP;
  2394. }
  2395. else
  2396. {
  2397. /* Process Unlocked */
  2398. __HAL_UNLOCK(hi2c);
  2399. /* Wrong size Status regarding TCR flag event */
  2400. hi2c->ErrorCode |= HAL_I2C_ERROR_SIZE;
  2401. HAL_I2C_ErrorCallback(hi2c);
  2402. }
  2403. }
  2404. else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET)
  2405. {
  2406. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
  2407. {
  2408. /* Clear NACK Flag */
  2409. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  2410. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  2411. }
  2412. /* Disable ERR, TC, STOP, NACK, RXI interrupts */
  2413. __HAL_I2C_DISABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_RXI );
  2414. /* Clear STOP Flag */
  2415. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  2416. /* Clear Configuration Register 2 */
  2417. __I2C_RESET_CR2(hi2c);
  2418. /* Call the correct callback to inform upper layer */
  2419. if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  2420. {
  2421. hi2c->State = HAL_I2C_STATE_READY;
  2422. /* Process Unlocked */
  2423. __HAL_UNLOCK(hi2c);
  2424. HAL_I2C_ErrorCallback(hi2c);
  2425. }
  2426. else
  2427. {
  2428. if(hi2c->State == HAL_I2C_STATE_MEM_BUSY_RX)
  2429. {
  2430. hi2c->State = HAL_I2C_STATE_READY;
  2431. /* Process Unlocked */
  2432. __HAL_UNLOCK(hi2c);
  2433. HAL_I2C_MemRxCpltCallback(hi2c);
  2434. }
  2435. else
  2436. {
  2437. hi2c->State = HAL_I2C_STATE_READY;
  2438. /* Process Unlocked */
  2439. __HAL_UNLOCK(hi2c);
  2440. HAL_I2C_MasterRxCpltCallback(hi2c);
  2441. }
  2442. }
  2443. }
  2444. else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
  2445. {
  2446. /* Clear NACK Flag */
  2447. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  2448. /* Process Unlocked */
  2449. __HAL_UNLOCK(hi2c);
  2450. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  2451. HAL_I2C_ErrorCallback(hi2c);
  2452. }
  2453. /* Process Unlocked */
  2454. __HAL_UNLOCK(hi2c);
  2455. return HAL_OK;
  2456. }
  2457. /**
  2458. * @brief Handle Interrupt Flags Slave Transmit Mode
  2459. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  2460. * the configuration information for the specified I2C.
  2461. * @retval HAL status
  2462. */
  2463. static HAL_StatusTypeDef I2C_SlaveTransmit_ISR(I2C_HandleTypeDef *hi2c)
  2464. {
  2465. /* Process locked */
  2466. __HAL_LOCK(hi2c);
  2467. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) != RESET)
  2468. {
  2469. /* Check that I2C transfer finished */
  2470. /* if yes, normal usecase, a NACK is sent by the MASTER when Transfer is finished */
  2471. /* Mean XferCount == 0*/
  2472. /* So clear Flag NACKF only */
  2473. if(hi2c->XferCount == 0U)
  2474. {
  2475. /* Clear NACK Flag */
  2476. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  2477. /* Process Unlocked */
  2478. __HAL_UNLOCK(hi2c);
  2479. }
  2480. else
  2481. {
  2482. /* if no, error usecase, a Non-Acknowledge of last Data is generated by the MASTER*/
  2483. /* Clear NACK Flag */
  2484. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  2485. /* Set ErrorCode corresponding to a Non-Acknowledge */
  2486. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  2487. /* Process Unlocked */
  2488. __HAL_UNLOCK(hi2c);
  2489. /* Call the Error callback to prevent upper layer */
  2490. HAL_I2C_ErrorCallback(hi2c);
  2491. }
  2492. }
  2493. else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR) == SET)
  2494. {
  2495. /* Clear ADDR flag */
  2496. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
  2497. }
  2498. /* Check first if STOPF is set */
  2499. /* to prevent a Write Data in TX buffer */
  2500. /* which is stuck in TXDR until next */
  2501. /* communication with Master */
  2502. else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET)
  2503. {
  2504. /* Disable ERRI, TCI, STOPI, NACKI, ADDRI, RXI, TXI interrupts */
  2505. __HAL_I2C_DISABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI );
  2506. /* Disable Address Acknowledge */
  2507. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  2508. /* Clear STOP Flag */
  2509. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  2510. hi2c->State = HAL_I2C_STATE_READY;
  2511. /* Process Unlocked */
  2512. __HAL_UNLOCK(hi2c);
  2513. HAL_I2C_SlaveTxCpltCallback(hi2c);
  2514. }
  2515. else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXIS) == SET)
  2516. {
  2517. /* Write data to TXDR only if XferCount not reach "0" */
  2518. /* A TXIS flag can be set, during STOP treatment */
  2519. if(hi2c->XferCount > 0U)
  2520. {
  2521. /* Write data to TXDR */
  2522. hi2c->Instance->TXDR = (*hi2c->pBuffPtr++);
  2523. hi2c->XferCount--;
  2524. }
  2525. }
  2526. /* Process Unlocked */
  2527. __HAL_UNLOCK(hi2c);
  2528. return HAL_OK;
  2529. }
  2530. /**
  2531. * @brief Handle Interrupt Flags Slave Receive Mode
  2532. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  2533. * the configuration information for the specified I2C.
  2534. * @retval HAL status
  2535. */
  2536. static HAL_StatusTypeDef I2C_SlaveReceive_ISR(I2C_HandleTypeDef *hi2c)
  2537. {
  2538. /* Process Locked */
  2539. __HAL_LOCK(hi2c);
  2540. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) != RESET)
  2541. {
  2542. /* Clear NACK Flag */
  2543. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  2544. /* Process Unlocked */
  2545. __HAL_UNLOCK(hi2c);
  2546. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  2547. HAL_I2C_ErrorCallback(hi2c);
  2548. }
  2549. else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR) == SET)
  2550. {
  2551. /* Clear ADDR flag */
  2552. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
  2553. }
  2554. else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET)
  2555. {
  2556. /* Read data from RXDR */
  2557. (*hi2c->pBuffPtr++) = hi2c->Instance->RXDR;
  2558. hi2c->XferSize--;
  2559. hi2c->XferCount--;
  2560. }
  2561. else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET)
  2562. {
  2563. /* Disable ERRI, TCI, STOPI, NACKI, ADDRI, RXI, TXI interrupts */
  2564. __HAL_I2C_DISABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI );
  2565. /* Disable Address Acknowledge */
  2566. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  2567. /* Clear STOP Flag */
  2568. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  2569. hi2c->State = HAL_I2C_STATE_READY;
  2570. /* Process Unlocked */
  2571. __HAL_UNLOCK(hi2c);
  2572. HAL_I2C_SlaveRxCpltCallback(hi2c);
  2573. }
  2574. /* Process Unlocked */
  2575. __HAL_UNLOCK(hi2c);
  2576. return HAL_OK;
  2577. }
  2578. /**
  2579. * @brief Master sends target device address followed by internal memory address for write request.
  2580. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  2581. * the configuration information for the specified I2C.
  2582. * @param DevAddress: Target device address
  2583. * @param MemAddress: Internal memory address
  2584. * @param MemAddSize: Size of internal memory address
  2585. * @param Timeout: Timeout duration
  2586. * @retval HAL status
  2587. */
  2588. static HAL_StatusTypeDef I2C_RequestMemoryWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout)
  2589. {
  2590. I2C_TransferConfig(hi2c,DevAddress,MemAddSize, I2C_RELOAD_MODE, I2C_GENERATE_START_WRITE);
  2591. /* Wait until TXIS flag is set */
  2592. if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
  2593. {
  2594. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  2595. {
  2596. return HAL_ERROR;
  2597. }
  2598. else
  2599. {
  2600. return HAL_TIMEOUT;
  2601. }
  2602. }
  2603. /* If Memory address size is 8Bit */
  2604. if(MemAddSize == I2C_MEMADD_SIZE_8BIT)
  2605. {
  2606. /* Send Memory Address */
  2607. hi2c->Instance->TXDR = __I2C_MEM_ADD_LSB(MemAddress);
  2608. }
  2609. /* If Memory address size is 16Bit */
  2610. else
  2611. {
  2612. /* Send MSB of Memory Address */
  2613. hi2c->Instance->TXDR = __I2C_MEM_ADD_MSB(MemAddress);
  2614. /* Wait until TXIS flag is set */
  2615. if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
  2616. {
  2617. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  2618. {
  2619. return HAL_ERROR;
  2620. }
  2621. else
  2622. {
  2623. return HAL_TIMEOUT;
  2624. }
  2625. }
  2626. /* Send LSB of Memory Address */
  2627. hi2c->Instance->TXDR = __I2C_MEM_ADD_LSB(MemAddress);
  2628. }
  2629. /* Wait until TCR flag is set */
  2630. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout) != HAL_OK)
  2631. {
  2632. return HAL_TIMEOUT;
  2633. }
  2634. return HAL_OK;
  2635. }
  2636. /**
  2637. * @brief Master sends target device address followed by internal memory address for read request.
  2638. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  2639. * the configuration information for the specified I2C.
  2640. * @param DevAddress: Target device address
  2641. * @param MemAddress: Internal memory address
  2642. * @param MemAddSize: Size of internal memory address
  2643. * @param Timeout: Timeout duration
  2644. * @retval HAL status
  2645. */
  2646. static HAL_StatusTypeDef I2C_RequestMemoryRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout)
  2647. {
  2648. I2C_TransferConfig(hi2c,DevAddress,MemAddSize, I2C_SOFTEND_MODE, I2C_GENERATE_START_WRITE);
  2649. /* Wait until TXIS flag is set */
  2650. if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
  2651. {
  2652. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  2653. {
  2654. return HAL_ERROR;
  2655. }
  2656. else
  2657. {
  2658. return HAL_TIMEOUT;
  2659. }
  2660. }
  2661. /* If Memory address size is 8Bit */
  2662. if(MemAddSize == I2C_MEMADD_SIZE_8BIT)
  2663. {
  2664. /* Send Memory Address */
  2665. hi2c->Instance->TXDR = __I2C_MEM_ADD_LSB(MemAddress);
  2666. }
  2667. /* If Memory address size is 16Bit */
  2668. else
  2669. {
  2670. /* Send MSB of Memory Address */
  2671. hi2c->Instance->TXDR = __I2C_MEM_ADD_MSB(MemAddress);
  2672. /* Wait until TXIS flag is set */
  2673. if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
  2674. {
  2675. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  2676. {
  2677. return HAL_ERROR;
  2678. }
  2679. else
  2680. {
  2681. return HAL_TIMEOUT;
  2682. }
  2683. }
  2684. /* Send LSB of Memory Address */
  2685. hi2c->Instance->TXDR = __I2C_MEM_ADD_LSB(MemAddress);
  2686. }
  2687. /* Wait until TC flag is set */
  2688. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TC, RESET, Timeout) != HAL_OK)
  2689. {
  2690. return HAL_TIMEOUT;
  2691. }
  2692. return HAL_OK;
  2693. }
  2694. /**
  2695. * @brief DMA I2C master transmit process complete callback.
  2696. * @param hdma: DMA handle
  2697. * @retval None
  2698. */
  2699. static void I2C_DMAMasterTransmitCplt(DMA_HandleTypeDef *hdma)
  2700. {
  2701. uint16_t DevAddress;
  2702. I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  2703. /* Check if last DMA request was done with RELOAD */
  2704. /* Set NBYTES to write and reload if size > 255 */
  2705. if( (hi2c->XferSize == 255U) && (hi2c->XferSize < hi2c->XferCount) )
  2706. {
  2707. /* Wait until TCR flag is set */
  2708. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, I2C_TIMEOUT_TCR) != HAL_OK)
  2709. {
  2710. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  2711. }
  2712. /* Disable DMA Request */
  2713. hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
  2714. /* Check if Errors has been detected during transfer */
  2715. if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  2716. {
  2717. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  2718. /* Wait until STOPF flag is reset */
  2719. if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  2720. {
  2721. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  2722. {
  2723. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  2724. }
  2725. else
  2726. {
  2727. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  2728. }
  2729. }
  2730. /* Clear STOP Flag */
  2731. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  2732. /* Clear Configuration Register 2 */
  2733. __I2C_RESET_CR2(hi2c);
  2734. hi2c->XferCount = 0U;
  2735. hi2c->State = HAL_I2C_STATE_READY;
  2736. HAL_I2C_ErrorCallback(hi2c);
  2737. }
  2738. else
  2739. {
  2740. hi2c->pBuffPtr += hi2c->XferSize;
  2741. hi2c->XferCount -= hi2c->XferSize;
  2742. if(hi2c->XferCount > 255U)
  2743. {
  2744. hi2c->XferSize = 255U;
  2745. }
  2746. else
  2747. {
  2748. hi2c->XferSize = hi2c->XferCount;
  2749. }
  2750. DevAddress = (hi2c->Instance->CR2 & I2C_CR2_SADD);
  2751. /* Enable the DMA channel */
  2752. HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);
  2753. /* Send Slave Address */
  2754. /* Set NBYTES to write and reload if size > 255 */
  2755. if( (hi2c->XferSize == 255U) && (hi2c->XferSize < hi2c->XferCount) )
  2756. {
  2757. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  2758. }
  2759. else
  2760. {
  2761. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  2762. }
  2763. /* Wait until TXIS flag is set */
  2764. if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, I2C_TIMEOUT_TXIS) != HAL_OK)
  2765. {
  2766. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  2767. /* Wait until STOPF flag is reset */
  2768. if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  2769. {
  2770. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  2771. {
  2772. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  2773. }
  2774. else
  2775. {
  2776. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  2777. }
  2778. }
  2779. /* Clear STOP Flag */
  2780. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  2781. /* Clear Configuration Register 2 */
  2782. __I2C_RESET_CR2(hi2c);
  2783. hi2c->XferCount = 0U;
  2784. hi2c->State = HAL_I2C_STATE_READY;
  2785. HAL_I2C_ErrorCallback(hi2c);
  2786. }
  2787. else
  2788. {
  2789. /* Enable DMA Request */
  2790. hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
  2791. }
  2792. }
  2793. }
  2794. else
  2795. {
  2796. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  2797. /* Wait until STOPF flag is reset */
  2798. if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  2799. {
  2800. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  2801. {
  2802. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  2803. }
  2804. else
  2805. {
  2806. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  2807. }
  2808. }
  2809. /* Clear STOP Flag */
  2810. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  2811. /* Clear Configuration Register 2 */
  2812. __I2C_RESET_CR2(hi2c);
  2813. /* Disable DMA Request */
  2814. hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
  2815. hi2c->XferCount = 0U;
  2816. hi2c->State = HAL_I2C_STATE_READY;
  2817. /* Check if Errors has been detected during transfer */
  2818. if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  2819. {
  2820. HAL_I2C_ErrorCallback(hi2c);
  2821. }
  2822. else
  2823. {
  2824. HAL_I2C_MasterTxCpltCallback(hi2c);
  2825. }
  2826. }
  2827. }
  2828. /**
  2829. * @brief DMA I2C slave transmit process complete callback.
  2830. * @param hdma: DMA handle
  2831. * @retval None
  2832. */
  2833. static void I2C_DMASlaveTransmitCplt(DMA_HandleTypeDef *hdma)
  2834. {
  2835. I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  2836. /* Wait until STOP flag is set */
  2837. if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  2838. {
  2839. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  2840. {
  2841. /* Normal Use case, a AF is generated by master */
  2842. /* to inform slave the end of transfer */
  2843. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2844. }
  2845. else
  2846. {
  2847. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  2848. }
  2849. }
  2850. /* Clear STOP flag */
  2851. __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_STOPF);
  2852. /* Wait until BUSY flag is reset */
  2853. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY) != HAL_OK)
  2854. {
  2855. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  2856. }
  2857. /* Disable DMA Request */
  2858. hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
  2859. hi2c->XferCount = 0U;
  2860. hi2c->State = HAL_I2C_STATE_READY;
  2861. /* Check if Errors has been detected during transfer */
  2862. if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  2863. {
  2864. HAL_I2C_ErrorCallback(hi2c);
  2865. }
  2866. else
  2867. {
  2868. HAL_I2C_SlaveTxCpltCallback(hi2c);
  2869. }
  2870. }
  2871. /**
  2872. * @brief DMA I2C master receive process complete callback
  2873. * @param hdma: DMA handle
  2874. * @retval None
  2875. */
  2876. static void I2C_DMAMasterReceiveCplt(DMA_HandleTypeDef *hdma)
  2877. {
  2878. I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  2879. uint16_t DevAddress;
  2880. /* Check if last DMA request was done with RELOAD */
  2881. /* Set NBYTES to write and reload if size > 255 */
  2882. if( (hi2c->XferSize == 255U) && (hi2c->XferSize < hi2c->XferCount) )
  2883. {
  2884. /* Wait until TCR flag is set */
  2885. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, I2C_TIMEOUT_TCR) != HAL_OK)
  2886. {
  2887. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  2888. }
  2889. /* Disable DMA Request */
  2890. hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
  2891. /* Check if Errors has been detected during transfer */
  2892. if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  2893. {
  2894. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  2895. /* Wait until STOPF flag is reset */
  2896. if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  2897. {
  2898. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  2899. {
  2900. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  2901. }
  2902. else
  2903. {
  2904. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  2905. }
  2906. }
  2907. /* Clear STOP Flag */
  2908. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  2909. /* Clear Configuration Register 2 */
  2910. __I2C_RESET_CR2(hi2c);
  2911. hi2c->XferCount = 0U;
  2912. hi2c->State = HAL_I2C_STATE_READY;
  2913. HAL_I2C_ErrorCallback(hi2c);
  2914. }
  2915. else
  2916. {
  2917. hi2c->pBuffPtr += hi2c->XferSize;
  2918. hi2c->XferCount -= hi2c->XferSize;
  2919. if(hi2c->XferCount > 255U)
  2920. {
  2921. hi2c->XferSize = 255U;
  2922. }
  2923. else
  2924. {
  2925. hi2c->XferSize = hi2c->XferCount;
  2926. }
  2927. DevAddress = (hi2c->Instance->CR2 & I2C_CR2_SADD);
  2928. /* Enable the DMA channel */
  2929. HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize);
  2930. /* Send Slave Address */
  2931. /* Set NBYTES to write and reload if size > 255 */
  2932. if( (hi2c->XferSize == 255U) && (hi2c->XferSize < hi2c->XferCount) )
  2933. {
  2934. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  2935. }
  2936. else
  2937. {
  2938. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  2939. }
  2940. /* Wait until RXNE flag is set */
  2941. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, I2C_TIMEOUT_RXNE) != HAL_OK)
  2942. {
  2943. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  2944. }
  2945. /* Check if Errors has been detected during transfer */
  2946. if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  2947. {
  2948. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  2949. /* Wait until STOPF flag is reset */
  2950. if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  2951. {
  2952. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  2953. {
  2954. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  2955. }
  2956. else
  2957. {
  2958. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  2959. }
  2960. }
  2961. /* Clear STOP Flag */
  2962. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  2963. /* Clear Configuration Register 2 */
  2964. __I2C_RESET_CR2(hi2c);
  2965. hi2c->XferCount = 0U;
  2966. hi2c->State = HAL_I2C_STATE_READY;
  2967. HAL_I2C_ErrorCallback(hi2c);
  2968. }
  2969. else
  2970. {
  2971. /* Enable DMA Request */
  2972. hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
  2973. }
  2974. }
  2975. }
  2976. else
  2977. {
  2978. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  2979. /* Wait until STOPF flag is reset */
  2980. if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  2981. {
  2982. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  2983. {
  2984. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  2985. }
  2986. else
  2987. {
  2988. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  2989. }
  2990. }
  2991. /* Clear STOP Flag */
  2992. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  2993. /* Clear Configuration Register 2 */
  2994. __I2C_RESET_CR2(hi2c);
  2995. /* Disable DMA Request */
  2996. hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
  2997. hi2c->XferCount = 0U;
  2998. hi2c->State = HAL_I2C_STATE_READY;
  2999. /* Check if Errors has been detected during transfer */
  3000. if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  3001. {
  3002. HAL_I2C_ErrorCallback(hi2c);
  3003. }
  3004. else
  3005. {
  3006. HAL_I2C_MasterRxCpltCallback(hi2c);
  3007. }
  3008. }
  3009. }
  3010. /**
  3011. * @brief DMA I2C slave receive process complete callback.
  3012. * @param hdma: DMA handle
  3013. * @retval None
  3014. */
  3015. static void I2C_DMASlaveReceiveCplt(DMA_HandleTypeDef *hdma)
  3016. {
  3017. I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  3018. /* Wait until STOPF flag is reset */
  3019. if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  3020. {
  3021. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  3022. {
  3023. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  3024. }
  3025. else
  3026. {
  3027. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3028. }
  3029. }
  3030. /* Clear STOPF flag */
  3031. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  3032. /* Wait until BUSY flag is reset */
  3033. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY) != HAL_OK)
  3034. {
  3035. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3036. }
  3037. /* Disable DMA Request */
  3038. hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
  3039. /* Disable Address Acknowledge */
  3040. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  3041. hi2c->XferCount = 0U;
  3042. hi2c->State = HAL_I2C_STATE_READY;
  3043. /* Check if Errors has been detected during transfer */
  3044. if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  3045. {
  3046. HAL_I2C_ErrorCallback(hi2c);
  3047. }
  3048. else
  3049. {
  3050. HAL_I2C_SlaveRxCpltCallback(hi2c);
  3051. }
  3052. }
  3053. /**
  3054. * @brief DMA I2C Memory Write process complete callback
  3055. * @param hdma : DMA handle
  3056. * @retval None
  3057. */
  3058. static void I2C_DMAMemTransmitCplt(DMA_HandleTypeDef *hdma)
  3059. {
  3060. uint16_t DevAddress;
  3061. I2C_HandleTypeDef* hi2c = ( I2C_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  3062. /* Check if last DMA request was done with RELOAD */
  3063. /* Set NBYTES to write and reload if size > 255 */
  3064. if( (hi2c->XferSize == 255U) && (hi2c->XferSize < hi2c->XferCount) )
  3065. {
  3066. /* Wait until TCR flag is set */
  3067. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, I2C_TIMEOUT_TCR) != HAL_OK)
  3068. {
  3069. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3070. }
  3071. /* Disable DMA Request */
  3072. hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
  3073. /* Check if Errors has been detected during transfer */
  3074. if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  3075. {
  3076. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  3077. /* Wait until STOPF flag is reset */
  3078. if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  3079. {
  3080. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  3081. {
  3082. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  3083. }
  3084. else
  3085. {
  3086. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3087. }
  3088. }
  3089. /* Clear STOP Flag */
  3090. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  3091. /* Clear Configuration Register 2 */
  3092. __I2C_RESET_CR2(hi2c);
  3093. hi2c->XferCount = 0U;
  3094. hi2c->State = HAL_I2C_STATE_READY;
  3095. HAL_I2C_ErrorCallback(hi2c);
  3096. }
  3097. else
  3098. {
  3099. hi2c->pBuffPtr += hi2c->XferSize;
  3100. hi2c->XferCount -= hi2c->XferSize;
  3101. if(hi2c->XferCount > 255U)
  3102. {
  3103. hi2c->XferSize = 255U;
  3104. }
  3105. else
  3106. {
  3107. hi2c->XferSize = hi2c->XferCount;
  3108. }
  3109. DevAddress = (hi2c->Instance->CR2 & I2C_CR2_SADD);
  3110. /* Enable the DMA channel */
  3111. HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);
  3112. /* Send Slave Address */
  3113. /* Set NBYTES to write and reload if size > 255 */
  3114. if( (hi2c->XferSize == 255U) && (hi2c->XferSize < hi2c->XferCount) )
  3115. {
  3116. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  3117. }
  3118. else
  3119. {
  3120. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  3121. }
  3122. /* Wait until TXIS flag is set */
  3123. if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, I2C_TIMEOUT_TXIS) != HAL_OK)
  3124. {
  3125. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  3126. /* Wait until STOPF flag is reset */
  3127. if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  3128. {
  3129. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  3130. {
  3131. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  3132. }
  3133. else
  3134. {
  3135. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3136. }
  3137. }
  3138. /* Clear STOP Flag */
  3139. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  3140. /* Clear Configuration Register 2 */
  3141. __I2C_RESET_CR2(hi2c);
  3142. hi2c->XferCount = 0U;
  3143. hi2c->State = HAL_I2C_STATE_READY;
  3144. HAL_I2C_ErrorCallback(hi2c);
  3145. }
  3146. else
  3147. {
  3148. /* Enable DMA Request */
  3149. hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
  3150. }
  3151. }
  3152. }
  3153. else
  3154. {
  3155. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  3156. /* Wait until STOPF flag is reset */
  3157. if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  3158. {
  3159. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  3160. {
  3161. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  3162. }
  3163. else
  3164. {
  3165. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3166. }
  3167. }
  3168. /* Clear STOP Flag */
  3169. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  3170. /* Clear Configuration Register 2 */
  3171. __I2C_RESET_CR2(hi2c);
  3172. /* Disable DMA Request */
  3173. hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
  3174. hi2c->XferCount = 0U;
  3175. hi2c->State = HAL_I2C_STATE_READY;
  3176. /* Check if Errors has been detected during transfer */
  3177. if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  3178. {
  3179. HAL_I2C_ErrorCallback(hi2c);
  3180. }
  3181. else
  3182. {
  3183. HAL_I2C_MemTxCpltCallback(hi2c);
  3184. }
  3185. }
  3186. }
  3187. /**
  3188. * @brief DMA I2C Memory Read process complete callback
  3189. * @param hdma: DMA handle
  3190. * @retval None
  3191. */
  3192. static void I2C_DMAMemReceiveCplt(DMA_HandleTypeDef *hdma)
  3193. {
  3194. I2C_HandleTypeDef* hi2c = ( I2C_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  3195. uint16_t DevAddress;
  3196. /* Check if last DMA request was done with RELOAD */
  3197. /* Set NBYTES to write and reload if size > 255 */
  3198. if( (hi2c->XferSize == 255U) && (hi2c->XferSize < hi2c->XferCount) )
  3199. {
  3200. /* Wait until TCR flag is set */
  3201. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, I2C_TIMEOUT_TCR) != HAL_OK)
  3202. {
  3203. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3204. }
  3205. /* Disable DMA Request */
  3206. hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
  3207. /* Check if Errors has been detected during transfer */
  3208. if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  3209. {
  3210. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  3211. /* Wait until STOPF flag is reset */
  3212. if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  3213. {
  3214. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  3215. {
  3216. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  3217. }
  3218. else
  3219. {
  3220. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3221. }
  3222. }
  3223. /* Clear STOP Flag */
  3224. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  3225. /* Clear Configuration Register 2 */
  3226. __I2C_RESET_CR2(hi2c);
  3227. hi2c->XferCount = 0U;
  3228. hi2c->State = HAL_I2C_STATE_READY;
  3229. HAL_I2C_ErrorCallback(hi2c);
  3230. }
  3231. else
  3232. {
  3233. hi2c->pBuffPtr += hi2c->XferSize;
  3234. hi2c->XferCount -= hi2c->XferSize;
  3235. if(hi2c->XferCount > 255U)
  3236. {
  3237. hi2c->XferSize = 255U;
  3238. }
  3239. else
  3240. {
  3241. hi2c->XferSize = hi2c->XferCount;
  3242. }
  3243. DevAddress = (hi2c->Instance->CR2 & I2C_CR2_SADD);
  3244. /* Enable the DMA channel */
  3245. HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize);
  3246. /* Send Slave Address */
  3247. /* Set NBYTES to write and reload if size > 255 */
  3248. if( (hi2c->XferSize == 255U) && (hi2c->XferSize < hi2c->XferCount) )
  3249. {
  3250. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  3251. }
  3252. else
  3253. {
  3254. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  3255. }
  3256. /* Wait until RXNE flag is set */
  3257. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, I2C_TIMEOUT_RXNE) != HAL_OK)
  3258. {
  3259. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3260. }
  3261. /* Check if Errors has been detected during transfer */
  3262. if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  3263. {
  3264. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  3265. /* Wait until STOPF flag is reset */
  3266. if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  3267. {
  3268. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  3269. {
  3270. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  3271. }
  3272. else
  3273. {
  3274. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3275. }
  3276. }
  3277. /* Clear STOP Flag */
  3278. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  3279. /* Clear Configuration Register 2 */
  3280. __I2C_RESET_CR2(hi2c);
  3281. hi2c->XferCount = 0U;
  3282. hi2c->State = HAL_I2C_STATE_READY;
  3283. HAL_I2C_ErrorCallback(hi2c);
  3284. }
  3285. else
  3286. {
  3287. /* Enable DMA Request */
  3288. hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
  3289. }
  3290. }
  3291. }
  3292. else
  3293. {
  3294. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  3295. /* Wait until STOPF flag is reset */
  3296. if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  3297. {
  3298. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  3299. {
  3300. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  3301. }
  3302. else
  3303. {
  3304. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3305. }
  3306. }
  3307. /* Clear STOP Flag */
  3308. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  3309. /* Clear Configuration Register 2 */
  3310. __I2C_RESET_CR2(hi2c);
  3311. /* Disable DMA Request */
  3312. hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
  3313. hi2c->XferCount = 0U;
  3314. hi2c->State = HAL_I2C_STATE_READY;
  3315. /* Check if Errors has been detected during transfer */
  3316. if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  3317. {
  3318. HAL_I2C_ErrorCallback(hi2c);
  3319. }
  3320. else
  3321. {
  3322. HAL_I2C_MemRxCpltCallback(hi2c);
  3323. }
  3324. }
  3325. }
  3326. /**
  3327. * @brief DMA I2C communication error callback.
  3328. * @param hdma : DMA handle
  3329. * @retval None
  3330. */
  3331. static void I2C_DMAError(DMA_HandleTypeDef *hdma)
  3332. {
  3333. I2C_HandleTypeDef* hi2c = ( I2C_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  3334. /* Disable Acknowledge */
  3335. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  3336. hi2c->XferCount = 0U;
  3337. hi2c->State = HAL_I2C_STATE_READY;
  3338. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  3339. HAL_I2C_ErrorCallback(hi2c);
  3340. }
  3341. /**
  3342. * @brief This function handles I2C Communication Timeout.
  3343. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  3344. * the configuration information for the specified I2C.
  3345. * @param Flag: specifies the I2C flag to check.
  3346. * @param Status: The new Flag status (SET or RESET).
  3347. * @param Timeout: Timeout duration
  3348. * @retval HAL status
  3349. */
  3350. static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status, uint32_t Timeout)
  3351. {
  3352. uint32_t tickstart = HAL_GetTick();
  3353. /* Wait until flag is set */
  3354. if(Status == RESET)
  3355. {
  3356. while(__HAL_I2C_GET_FLAG(hi2c, Flag) == RESET)
  3357. {
  3358. /* Check for the Timeout */
  3359. if(Timeout != HAL_MAX_DELAY)
  3360. {
  3361. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  3362. {
  3363. hi2c->State= HAL_I2C_STATE_READY;
  3364. /* Process Unlocked */
  3365. __HAL_UNLOCK(hi2c);
  3366. return HAL_TIMEOUT;
  3367. }
  3368. }
  3369. }
  3370. }
  3371. else
  3372. {
  3373. while(__HAL_I2C_GET_FLAG(hi2c, Flag) != RESET)
  3374. {
  3375. /* Check for the Timeout */
  3376. if(Timeout != HAL_MAX_DELAY)
  3377. {
  3378. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  3379. {
  3380. hi2c->State= HAL_I2C_STATE_READY;
  3381. /* Process Unlocked */
  3382. __HAL_UNLOCK(hi2c);
  3383. return HAL_TIMEOUT;
  3384. }
  3385. }
  3386. }
  3387. }
  3388. return HAL_OK;
  3389. }
  3390. /**
  3391. * @brief This function handles I2C Communication Timeout for specific usage of TXIS flag.
  3392. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  3393. * the configuration information for the specified I2C.
  3394. * @param Timeout: Timeout duration
  3395. * @retval HAL status
  3396. */
  3397. static HAL_StatusTypeDef I2C_WaitOnTXISFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout)
  3398. {
  3399. uint32_t tickstart = HAL_GetTick();
  3400. while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXIS) == RESET)
  3401. {
  3402. /* Check if a NACK is detected */
  3403. if(I2C_IsAcknowledgeFailed(hi2c, Timeout) != HAL_OK)
  3404. {
  3405. return HAL_ERROR;
  3406. }
  3407. /* Check for the Timeout */
  3408. if(Timeout != HAL_MAX_DELAY)
  3409. {
  3410. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  3411. {
  3412. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3413. hi2c->State= HAL_I2C_STATE_READY;
  3414. /* Process Unlocked */
  3415. __HAL_UNLOCK(hi2c);
  3416. return HAL_TIMEOUT;
  3417. }
  3418. }
  3419. }
  3420. return HAL_OK;
  3421. }
  3422. /**
  3423. * @brief This function handles I2C Communication Timeout for specific usage of STOP flag.
  3424. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  3425. * the configuration information for the specified I2C.
  3426. * @param Timeout: Timeout duration
  3427. * @retval HAL status
  3428. */
  3429. static HAL_StatusTypeDef I2C_WaitOnSTOPFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout)
  3430. {
  3431. uint32_t tickstart = 0x00U;
  3432. tickstart = HAL_GetTick();
  3433. while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET)
  3434. {
  3435. /* Check if a NACK is detected */
  3436. if(I2C_IsAcknowledgeFailed(hi2c, Timeout) != HAL_OK)
  3437. {
  3438. return HAL_ERROR;
  3439. }
  3440. /* Check for the Timeout */
  3441. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  3442. {
  3443. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3444. hi2c->State= HAL_I2C_STATE_READY;
  3445. /* Process Unlocked */
  3446. __HAL_UNLOCK(hi2c);
  3447. return HAL_TIMEOUT;
  3448. }
  3449. }
  3450. return HAL_OK;
  3451. }
  3452. /**
  3453. * @brief This function handles I2C Communication Timeout for specific usage of RXNE flag.
  3454. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  3455. * the configuration information for the specified I2C.
  3456. * @param Timeout: Timeout duration
  3457. * @retval HAL status
  3458. */
  3459. static HAL_StatusTypeDef I2C_WaitOnRXNEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout)
  3460. {
  3461. uint32_t tickstart = 0x00U;
  3462. tickstart = HAL_GetTick();
  3463. while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == RESET)
  3464. {
  3465. /* Check if a NACK is detected */
  3466. if(I2C_IsAcknowledgeFailed(hi2c, Timeout) != HAL_OK)
  3467. {
  3468. return HAL_ERROR;
  3469. }
  3470. /* Check if a STOPF is detected */
  3471. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET)
  3472. {
  3473. /* Clear STOP Flag */
  3474. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  3475. /* Clear Configuration Register 2 */
  3476. __I2C_RESET_CR2(hi2c);
  3477. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  3478. hi2c->State= HAL_I2C_STATE_READY;
  3479. /* Process Unlocked */
  3480. __HAL_UNLOCK(hi2c);
  3481. return HAL_ERROR;
  3482. }
  3483. /* Check for the Timeout */
  3484. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  3485. {
  3486. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3487. hi2c->State= HAL_I2C_STATE_READY;
  3488. /* Process Unlocked */
  3489. __HAL_UNLOCK(hi2c);
  3490. return HAL_TIMEOUT;
  3491. }
  3492. }
  3493. return HAL_OK;
  3494. }
  3495. /**
  3496. * @brief This function handles Acknowledge failed detection during an I2C Communication.
  3497. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  3498. * the configuration information for the specified I2C.
  3499. * @param Timeout: Timeout duration
  3500. * @retval HAL status
  3501. */
  3502. static HAL_StatusTypeDef I2C_IsAcknowledgeFailed(I2C_HandleTypeDef *hi2c, uint32_t Timeout)
  3503. {
  3504. uint32_t tickstart = 0x00U;
  3505. tickstart = HAL_GetTick();
  3506. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
  3507. {
  3508. /* Generate stop if necessary only in case of I2C peripheral in MASTER mode */
  3509. if((hi2c->State == HAL_I2C_STATE_MASTER_BUSY_TX) || (hi2c->State == HAL_I2C_STATE_MEM_BUSY_TX)
  3510. || (hi2c->State == HAL_I2C_STATE_MEM_BUSY_RX))
  3511. {
  3512. /* No need to generate the STOP condition if AUTOEND mode is enabled */
  3513. /* Generate the STOP condition only in case of SOFTEND mode is enabled */
  3514. if((hi2c->Instance->CR2 & I2C_AUTOEND_MODE) != I2C_AUTOEND_MODE)
  3515. {
  3516. /* Generate Stop */
  3517. hi2c->Instance->CR2 |= I2C_CR2_STOP;
  3518. }
  3519. }
  3520. /* Wait until STOP Flag is reset */
  3521. /* AutoEnd should be initiate after AF */
  3522. while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET)
  3523. {
  3524. /* Check for the Timeout */
  3525. if(Timeout != HAL_MAX_DELAY)
  3526. {
  3527. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  3528. {
  3529. hi2c->State= HAL_I2C_STATE_READY;
  3530. /* Process Unlocked */
  3531. __HAL_UNLOCK(hi2c);
  3532. return HAL_TIMEOUT;
  3533. }
  3534. }
  3535. }
  3536. /* Clear NACKF Flag */
  3537. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  3538. /* Clear STOP Flag */
  3539. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  3540. /* Flush TX register if not empty */
  3541. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXE) == RESET)
  3542. {
  3543. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_TXE);
  3544. }
  3545. /* Clear Configuration Register 2 */
  3546. __I2C_RESET_CR2(hi2c);
  3547. hi2c->ErrorCode = HAL_I2C_ERROR_AF;
  3548. hi2c->State= HAL_I2C_STATE_READY;
  3549. /* Process Unlocked */
  3550. __HAL_UNLOCK(hi2c);
  3551. return HAL_ERROR;
  3552. }
  3553. return HAL_OK;
  3554. }
  3555. /**
  3556. * @brief Handles I2Cx communication when starting transfer or during transfer (TC or TCR flag are set).
  3557. * @param hi2c: I2C handle.
  3558. * @param DevAddress: specifies the slave address to be programmed.
  3559. * @param Size: specifies the number of bytes to be programmed.
  3560. * This parameter must be a value between 0 and 255.
  3561. * @param Mode: new state of the I2C START condition generation.
  3562. * This parameter can be one of the following values:
  3563. * @arg I2C_RELOAD_MODE: Enable Reload mode .
  3564. * @arg I2C_AUTOEND_MODE: Enable Automatic end mode.
  3565. * @arg I2C_SOFTEND_MODE: Enable Software end mode.
  3566. * @param Request: new state of the I2C START condition generation.
  3567. * This parameter can be one of the following values:
  3568. * @arg I2C_NO_STARTSTOP: Do not Generate stop and start condition.
  3569. * @arg I2C_GENERATE_STOP: Generate stop condition (Size should be set to 0).
  3570. * @arg I2C_GENERATE_START_READ: Generate Restart for read request.
  3571. * @arg I2C_GENERATE_START_WRITE: Generate Restart for write request.
  3572. * @retval None
  3573. */
  3574. static void I2C_TransferConfig(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t Size, uint32_t Mode, uint32_t Request)
  3575. {
  3576. uint32_t tmpreg = 0U;
  3577. /* Check the parameters */
  3578. assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
  3579. assert_param(IS_TRANSFER_MODE(Mode));
  3580. assert_param(IS_TRANSFER_REQUEST(Request));
  3581. /* Get the CR2 register value */
  3582. tmpreg = hi2c->Instance->CR2;
  3583. /* clear tmpreg specific bits */
  3584. tmpreg &= (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | I2C_CR2_RD_WRN | I2C_CR2_START | I2C_CR2_STOP));
  3585. /* update tmpreg */
  3586. tmpreg |= (uint32_t)(((uint32_t)DevAddress & I2C_CR2_SADD) | (((uint32_t)Size << 16U ) & I2C_CR2_NBYTES) | \
  3587. (uint32_t)Mode | (uint32_t)Request);
  3588. /* update CR2 register */
  3589. hi2c->Instance->CR2 = tmpreg;
  3590. }
  3591. /**
  3592. * @}
  3593. */
  3594. /**
  3595. * @}
  3596. */
  3597. #endif /* HAL_I2C_MODULE_ENABLED */
  3598. /**
  3599. * @}
  3600. */
  3601. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/