stm32f4xx_hal_i2c.c 102 KB

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