stm32f0xx_hal_i2c.c 159 KB

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