stm32f1xx_hal_i2c.c 118 KB

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