stm32f4xx_hal_i2c.c 101 KB

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