stm32f4xx_hal_fmpi2c.c 161 KB

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