stm32f7xx_hal_i2c.c 156 KB

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