stm32f0xx_hal_i2c.c 158 KB

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