stm32l4xx_hal_i2c.c 160 KB

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