stm32f1xx_hal_i2c.c 168 KB

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