stm32h7xx_hal_i2c.c 154 KB

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