stm32f4xx_hal_fmpi2c.c 129 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_fmpi2c.c
  4. * @author MCD Application Team
  5. * @version V1.3.0
  6. * @date 09-March-2015
  7. * @brief FMPI2C HAL module driver.
  8. *
  9. * This file provides firmware functions to manage the following
  10. * functionalities of the Inter Integrated Circuit (FMPI2C) peripheral:
  11. * + Initialization and de-initialization functions
  12. * + IO operation functions
  13. * + Peripheral State functions
  14. *
  15. @verbatim
  16. ==============================================================================
  17. ##### How to use this driver #####
  18. ==============================================================================
  19. [..]
  20. The FMPI2C HAL driver can be used as follows:
  21. (#) Declare a FMPI2C_HandleTypeDef handle structure, for example:
  22. FMPI2C_HandleTypeDef hfmpi2c;
  23. (#)Initialize the FMPI2C low level resources by implement the HAL_FMPI2C_MspInit ()API:
  24. (##) Enable the FMPI2Cx interface clock
  25. (##) FMPI2C pins configuration
  26. (+++) Enable the clock for the FMPI2C GPIOs
  27. (+++) Configure FMPI2C pins as alternate function open-drain
  28. (##) NVIC configuration if you need to use interrupt process
  29. (+++) Configure the FMPI2Cx interrupt priority
  30. (+++) Enable the NVIC FMPI2C IRQ Channel
  31. (##) DMA Configuration if you need to use DMA process
  32. (+++) Declare a DMA_HandleTypeDef handle structure for the transmit or receive stream
  33. (+++) Enable the DMAx interface clock using
  34. (+++) Configure the DMA handle parameters
  35. (+++) Configure the DMA Tx or Rx Stream
  36. (+++) Associate the initilalized DMA handle to the hfmpi2c DMA Tx or Rx handle
  37. (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx or Rx Stream
  38. (#) Configure the Communication Clock Timing, Own Address1, Master Adressing 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() API:
  41. (+++) These API's configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
  42. by calling the customed HAL_FMPI2C_MspInit(&hfmpi2c) API.
  43. (#) To check if target device is ready for communication, use the function HAL_FMPI2C_IsDeviceReady()
  44. (#) For FMPI2C IO and IO MEM operations, three mode of operations are available within this driver :
  45. *** Polling mode IO operation ***
  46. =================================
  47. [..]
  48. (+) Transmit in master mode an amount of data in blocking mode using HAL_FMPI2C_Master_Transmit()
  49. (+) Receive in master mode an amount of data in blocking mode using HAL_FMPI2C_Master_Receive()
  50. (+) Transmit in slave mode an amount of data in blocking mode using HAL_FMPI2C_Slave_Transmit()
  51. (+) Receive in slave mode an amount of data in blocking mode using HAL_FMPI2C_Slave_Receive()
  52. *** Polling mode IO MEM operation ***
  53. =====================================
  54. [..]
  55. (+) Write an amount of data in blocking mode to a specific memory address using HAL_FMPI2C_Mem_Write()
  56. (+) Read an amount of data in blocking mode from a specific memory address using HAL_FMPI2C_Mem_Read()
  57. *** Interrupt mode IO operation ***
  58. ===================================
  59. [..]
  60. (+) Transmit in master mode an amount of data in non blocking mode using HAL_FMPI2C_Master_Transmit_IT()
  61. (+) At transmission end of transfer HAL_FMPI2C_MasterTxCpltCallback is executed and user can
  62. add his own code by customization of function pointer HAL_FMPI2C_MasterTxCpltCallback
  63. (+) Receive in master mode an amount of data in non blocking mode using HAL_FMPI2C_Master_Receive_IT()
  64. (+) At reception end of transfer HAL_FMPI2C_MasterRxCpltCallback is executed and user can
  65. add his own code by customization of function pointer HAL_FMPI2C_MasterRxCpltCallback
  66. (+) Transmit in slave mode an amount of data in non blocking mode using HAL_FMPI2C_Slave_Transmit_IT()
  67. (+) At transmission end of transfer HAL_FMPI2C_SlaveTxCpltCallback is executed and user can
  68. add his own code by customization of function pointer HAL_FMPI2C_SlaveTxCpltCallback
  69. (+) Receive in slave mode an amount of data in non blocking mode using HAL_FMPI2C_Slave_Receive_IT()
  70. (+) At reception end of transfer HAL_FMPI2C_SlaveRxCpltCallback is executed and user can
  71. add his own code by customization of function pointer HAL_FMPI2C_SlaveRxCpltCallback
  72. (+) In case of transfer Error, HAL_FMPI2C_ErrorCallback() function is executed and user can
  73. add his own code by customization of function pointer HAL_FMPI2C_ErrorCallback
  74. *** Interrupt mode IO MEM operation ***
  75. =======================================
  76. [..]
  77. (+) Write an amount of data in no-blocking mode with Interrupt to a specific memory address using
  78. HAL_FMPI2C_Mem_Write_IT()
  79. (+) At MEM end of write transfer HAL_FMPI2C_MemTxCpltCallback is executed and user can
  80. add his own code by customization of function pointer HAL_FMPI2C_MemTxCpltCallback
  81. (+) Read an amount of data in no-blocking mode with Interrupt from a specific memory address using
  82. HAL_FMPI2C_Mem_Read_IT()
  83. (+) At MEM end of read transfer HAL_FMPI2C_MemRxCpltCallback is executed and user can
  84. add his own code by customization of function pointer HAL_FMPI2C_MemRxCpltCallback
  85. (+) In case of transfer Error, HAL_FMPI2C_ErrorCallback() function is executed and user can
  86. add his own code by customization of function pointer HAL_FMPI2C_ErrorCallback
  87. *** DMA mode IO operation ***
  88. ==============================
  89. [..]
  90. (+) Transmit in master mode an amount of data in non blocking mode (DMA) using
  91. HAL_FMPI2C_Master_Transmit_DMA()
  92. (+) At transmission end of transfer HAL_FMPI2C_MasterTxCpltCallback is executed and user can
  93. add his own code by customization of function pointer HAL_FMPI2C_MasterTxCpltCallback
  94. (+) Receive in master mode an amount of data in non blocking mode (DMA) using
  95. HAL_FMPI2C_Master_Receive_DMA()
  96. (+) At reception end of transfer HAL_FMPI2C_MasterRxCpltCallback is executed and user can
  97. add his own code by customization of function pointer HAL_FMPI2C_MasterRxCpltCallback
  98. (+) Transmit in slave mode an amount of data in non blocking mode (DMA) using
  99. HAL_FMPI2C_Slave_Transmit_DMA()
  100. (+) At transmission end of transfer HAL_FMPI2C_SlaveTxCpltCallback is executed and user can
  101. add his own code by customization of function pointer HAL_FMPI2C_SlaveTxCpltCallback
  102. (+) Receive in slave mode an amount of data in non blocking mode (DMA) using
  103. HAL_FMPI2C_Slave_Receive_DMA()
  104. (+) At reception end of transfer HAL_FMPI2C_SlaveRxCpltCallback is executed and user can
  105. add his own code by customization of function pointer HAL_FMPI2C_SlaveRxCpltCallback
  106. (+) In case of transfer Error, HAL_FMPI2C_ErrorCallback() function is executed and user can
  107. add his own code by customization of function pointer HAL_FMPI2C_ErrorCallback
  108. *** DMA mode IO MEM operation ***
  109. =================================
  110. [..]
  111. (+) Write an amount of data in no-blocking mode with DMA to a specific memory address using
  112. HAL_FMPI2C_Mem_Write_DMA()
  113. (+) At MEM end of write transfer HAL_FMPI2C_MemTxCpltCallback is executed and user can
  114. add his own code by customization of function pointer HAL_FMPI2C_MemTxCpltCallback
  115. (+) Read an amount of data in no-blocking mode with DMA from a specific memory address using
  116. HAL_FMPI2C_Mem_Read_DMA()
  117. (+) At MEM end of read transfer HAL_FMPI2C_MemRxCpltCallback is executed and user can
  118. add his own code by customization of function pointer HAL_FMPI2C_MemRxCpltCallback
  119. (+) In case of transfer Error, HAL_FMPI2C_ErrorCallback() function is executed and user can
  120. add his own code by customization of function pointer HAL_FMPI2C_ErrorCallback
  121. *** FMPI2C HAL driver macros list ***
  122. ==================================
  123. [..]
  124. Below the list of most used macros in FMPI2C HAL driver.
  125. (+) __HAL_FMPI2C_ENABLE: Enable the FMPI2C peripheral
  126. (+) __HAL_FMPI2C_DISABLE: Disable the FMPI2C peripheral
  127. (+) __HAL_FMPI2C_GET_FLAG : Checks whether the specified FMPI2C flag is set or not
  128. (+) __HAL_FMPI2C_CLEAR_FLAG : Clears the specified FMPI2C pending flag
  129. (+) __HAL_FMPI2C_ENABLE_IT: Enables the specified FMPI2C interrupt
  130. (+) __HAL_FMPI2C_DISABLE_IT: Disables the specified FMPI2C interrupt
  131. [..]
  132. (@) You can refer to the FMPI2C HAL driver header file for more useful macros
  133. @endverbatim
  134. ******************************************************************************
  135. * @attention
  136. *
  137. * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
  138. *
  139. * Redistribution and use in source and binary forms, with or without modification,
  140. * are permitted provided that the following conditions are met:
  141. * 1. Redistributions of source code must retain the above copyright notice,
  142. * this list of conditions and the following disclaimer.
  143. * 2. Redistributions in binary form must reproduce the above copyright notice,
  144. * this list of conditions and the following disclaimer in the documentation
  145. * and/or other materials provided with the distribution.
  146. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  147. * may be used to endorse or promote products derived from this software
  148. * without specific prior written permission.
  149. *
  150. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  151. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  152. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  153. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  154. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  155. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  156. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  157. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  158. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  159. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  160. *
  161. ******************************************************************************
  162. */
  163. /* Includes ------------------------------------------------------------------*/
  164. #include "stm32f4xx_hal.h"
  165. /** @addtogroup STM32F4xx_HAL_Driver
  166. * @{
  167. */
  168. /** @defgroup FMPI2C FMPI2C
  169. * @brief FMPI2C HAL module driver
  170. * @{
  171. */
  172. #ifdef HAL_FMPI2C_MODULE_ENABLED
  173. #if defined(STM32F446xx)
  174. /* Private typedef -----------------------------------------------------------*/
  175. /* Private define ------------------------------------------------------------*/
  176. /** @addtogroup FMPI2C_Private_Constants
  177. * @{
  178. */
  179. #define TIMING_CLEAR_MASK ((uint32_t)0xF0FFFFFF) /*<! FMPI2C TIMING clear register Mask */
  180. #define FMPI2C_TIMEOUT_ADDR ((uint32_t)10000) /* 10 s */
  181. #define FMPI2C_TIMEOUT_BUSY ((uint32_t)25) /* 25 ms */
  182. #define FMPI2C_TIMEOUT_DIR ((uint32_t)25) /* 25 ms */
  183. #define FMPI2C_TIMEOUT_RXNE ((uint32_t)25) /* 25 ms */
  184. #define FMPI2C_TIMEOUT_STOPF ((uint32_t)25) /* 25 ms */
  185. #define FMPI2C_TIMEOUT_TC ((uint32_t)25) /* 25 ms */
  186. #define FMPI2C_TIMEOUT_TCR ((uint32_t)25) /* 25 ms */
  187. #define FMPI2C_TIMEOUT_TXIS ((uint32_t)25) /* 25 ms */
  188. #define FMPI2C_TIMEOUT_FLAG ((uint32_t)25) /* 25 ms */
  189. /**
  190. * @}
  191. */
  192. /* Private macro -------------------------------------------------------------*/
  193. /* Private variables ---------------------------------------------------------*/
  194. /* Private function prototypes -----------------------------------------------*/
  195. /** @addtogroup FMPI2C_Private_Functions
  196. * @brief FMPI2C private functions
  197. * @{
  198. */
  199. static void FMPI2C_DMAMasterTransmitCplt(DMA_HandleTypeDef *hdma);
  200. static void FMPI2C_DMAMasterReceiveCplt(DMA_HandleTypeDef *hdma);
  201. static void FMPI2C_DMASlaveTransmitCplt(DMA_HandleTypeDef *hdma);
  202. static void FMPI2C_DMASlaveReceiveCplt(DMA_HandleTypeDef *hdma);
  203. static void FMPI2C_DMAMemTransmitCplt(DMA_HandleTypeDef *hdma);
  204. static void FMPI2C_DMAMemReceiveCplt(DMA_HandleTypeDef *hdma);
  205. static void FMPI2C_DMAError(DMA_HandleTypeDef *hdma);
  206. static HAL_StatusTypeDef FMPI2C_RequestMemoryWrite(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout);
  207. static HAL_StatusTypeDef FMPI2C_RequestMemoryRead(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout);
  208. static HAL_StatusTypeDef FMPI2C_WaitOnFlagUntilTimeout(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t Flag, FlagStatus Status, uint32_t Timeout);
  209. static HAL_StatusTypeDef FMPI2C_WaitOnTXISFlagUntilTimeout(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t Timeout);
  210. static HAL_StatusTypeDef FMPI2C_WaitOnRXNEFlagUntilTimeout(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t Timeout);
  211. static HAL_StatusTypeDef FMPI2C_WaitOnSTOPFlagUntilTimeout(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t Timeout);
  212. static HAL_StatusTypeDef FMPI2C_IsAcknowledgeFailed(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t Timeout);
  213. static HAL_StatusTypeDef FMPI2C_MasterTransmit_ISR(FMPI2C_HandleTypeDef *hfmpi2c);
  214. static HAL_StatusTypeDef FMPI2C_MasterReceive_ISR(FMPI2C_HandleTypeDef *hfmpi2c);
  215. static HAL_StatusTypeDef FMPI2C_SlaveTransmit_ISR(FMPI2C_HandleTypeDef *hfmpi2c);
  216. static HAL_StatusTypeDef FMPI2C_SlaveReceive_ISR(FMPI2C_HandleTypeDef *hfmpi2c);
  217. static void FMPI2C_TransferConfig(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint8_t Size, uint32_t Mode, uint32_t Request);
  218. /**
  219. * @}
  220. */
  221. /* Exported functions --------------------------------------------------------*/
  222. /** @defgroup FMPI2C_Exported_Functions FMPI2C Exported Functions
  223. * @{
  224. */
  225. /** @defgroup FMPI2C_Exported_Functions_Group1 Initialization/de-initialization functions
  226. * @brief Initialization and Configuration functions
  227. *
  228. @verbatim
  229. ===============================================================================
  230. ##### Initialization/de-initialization functions #####
  231. ===============================================================================
  232. [..] This subsection provides a set of functions allowing to initialize and
  233. de-initialiaze the FMPI2Cx peripheral:
  234. (+) User must Implement HAL_FMPI2C_MspInit() function in which he configures
  235. all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC ).
  236. (+) Call the function HAL_FMPI2C_Init() to configure the selected device with
  237. the selected configuration:
  238. (++) Clock Timing
  239. (++) Own Address 1
  240. (++) Addressing mode (Master, Slave)
  241. (++) Dual Addressing mode
  242. (++) Own Address 2
  243. (++) Own Address 2 Mask
  244. (++) General call mode
  245. (++) Nostretch mode
  246. (+) Call the function HAL_FMPI2C_DeInit() to restore the default configuration
  247. of the selected FMPI2Cx periperal.
  248. @endverbatim
  249. * @{
  250. */
  251. /**
  252. * @brief Initializes the FMPI2C according to the specified parameters
  253. * in the FMPI2C_InitTypeDef and create the associated handle.
  254. * @param hfmpi2c : Pointer to a FMPI2C_HandleTypeDef structure that contains
  255. * the configuration information for the specified FMPI2C.
  256. * @retval HAL status
  257. */
  258. HAL_StatusTypeDef HAL_FMPI2C_Init(FMPI2C_HandleTypeDef *hfmpi2c)
  259. {
  260. /* Check the FMPI2C handle allocation */
  261. if(hfmpi2c == NULL)
  262. {
  263. return HAL_ERROR;
  264. }
  265. /* Check the parameters */
  266. assert_param(IS_FMPI2C_ALL_INSTANCE(hfmpi2c->Instance));
  267. assert_param(IS_FMPI2C_OWN_ADDRESS1(hfmpi2c->Init.OwnAddress1));
  268. assert_param(IS_FMPI2C_ADDRESSING_MODE(hfmpi2c->Init.AddressingMode));
  269. assert_param(IS_FMPI2C_DUAL_ADDRESS(hfmpi2c->Init.DualAddressMode));
  270. assert_param(IS_FMPI2C_OWN_ADDRESS2(hfmpi2c->Init.OwnAddress2));
  271. assert_param(IS_FMPI2C_OWN_ADDRESS2_MASK(hfmpi2c->Init.OwnAddress2Masks));
  272. assert_param(IS_FMPI2C_GENERAL_CALL(hfmpi2c->Init.GeneralCallMode));
  273. assert_param(IS_FMPI2C_NO_STRETCH(hfmpi2c->Init.NoStretchMode));
  274. if(hfmpi2c->State == HAL_FMPI2C_STATE_RESET)
  275. {
  276. /* Allocate lock resource and initialize it */
  277. hfmpi2c->Lock = HAL_UNLOCKED;
  278. /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
  279. HAL_FMPI2C_MspInit(hfmpi2c);
  280. }
  281. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY;
  282. /* Disable the selected FMPI2C peripheral */
  283. __HAL_FMPI2C_DISABLE(hfmpi2c);
  284. /*---------------------------- FMPI2Cx TIMINGR Configuration ------------------*/
  285. /* Configure FMPI2Cx: Frequency range */
  286. hfmpi2c->Instance->TIMINGR = hfmpi2c->Init.Timing & TIMING_CLEAR_MASK;
  287. /*---------------------------- FMPI2Cx OAR1 Configuration ---------------------*/
  288. /* Configure FMPI2Cx: Own Address1 and ack own address1 mode */
  289. hfmpi2c->Instance->OAR1 &= ~FMPI2C_OAR1_OA1EN;
  290. if(hfmpi2c->Init.OwnAddress1 != 0)
  291. {
  292. if(hfmpi2c->Init.AddressingMode == FMPI2C_ADDRESSINGMODE_7BIT)
  293. {
  294. hfmpi2c->Instance->OAR1 = (FMPI2C_OAR1_OA1EN | hfmpi2c->Init.OwnAddress1);
  295. }
  296. else /* FMPI2C_ADDRESSINGMODE_10BIT */
  297. {
  298. hfmpi2c->Instance->OAR1 = (FMPI2C_OAR1_OA1EN | FMPI2C_OAR1_OA1MODE | hfmpi2c->Init.OwnAddress1);
  299. }
  300. }
  301. /*---------------------------- FMPI2Cx CR2 Configuration ----------------------*/
  302. /* Configure FMPI2Cx: Addressing Master mode */
  303. if(hfmpi2c->Init.AddressingMode == FMPI2C_ADDRESSINGMODE_10BIT)
  304. {
  305. hfmpi2c->Instance->CR2 = (FMPI2C_CR2_ADD10);
  306. }
  307. /* Enable the AUTOEND by default, and enable NACK (should be disable only during Slave process */
  308. hfmpi2c->Instance->CR2 |= (FMPI2C_CR2_AUTOEND | FMPI2C_CR2_NACK);
  309. /*---------------------------- FMPI2Cx OAR2 Configuration ---------------------*/
  310. /* Configure FMPI2Cx: Dual mode and Own Address2 */
  311. hfmpi2c->Instance->OAR2 = (hfmpi2c->Init.DualAddressMode | hfmpi2c->Init.OwnAddress2 | (hfmpi2c->Init.OwnAddress2Masks << 8));
  312. /*---------------------------- FMPI2Cx CR1 Configuration ----------------------*/
  313. /* Configure FMPI2Cx: Generalcall and NoStretch mode */
  314. hfmpi2c->Instance->CR1 = (hfmpi2c->Init.GeneralCallMode | hfmpi2c->Init.NoStretchMode);
  315. /* Enable the selected FMPI2C peripheral */
  316. __HAL_FMPI2C_ENABLE(hfmpi2c);
  317. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  318. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  319. return HAL_OK;
  320. }
  321. /**
  322. * @brief DeInitializes the FMPI2C peripheral.
  323. * @param hfmpi2c : Pointer to a FMPI2C_HandleTypeDef structure that contains
  324. * the configuration information for the specified FMPI2C.
  325. * @retval HAL status
  326. */
  327. HAL_StatusTypeDef HAL_FMPI2C_DeInit(FMPI2C_HandleTypeDef *hfmpi2c)
  328. {
  329. /* Check the FMPI2C handle allocation */
  330. if(hfmpi2c == NULL)
  331. {
  332. return HAL_ERROR;
  333. }
  334. /* Check the parameters */
  335. assert_param(IS_FMPI2C_ALL_INSTANCE(hfmpi2c->Instance));
  336. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY;
  337. /* Disable the FMPI2C Peripheral Clock */
  338. __HAL_FMPI2C_DISABLE(hfmpi2c);
  339. /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
  340. HAL_FMPI2C_MspDeInit(hfmpi2c);
  341. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  342. hfmpi2c->State = HAL_FMPI2C_STATE_RESET;
  343. /* Release Lock */
  344. __HAL_UNLOCK(hfmpi2c);
  345. return HAL_OK;
  346. }
  347. /**
  348. * @brief FMPI2C MSP Init.
  349. * @param hfmpi2c : Pointer to a FMPI2C_HandleTypeDef structure that contains
  350. * the configuration information for the specified FMPI2C.
  351. * @retval None
  352. */
  353. __weak void HAL_FMPI2C_MspInit(FMPI2C_HandleTypeDef *hfmpi2c)
  354. {
  355. /* NOTE : This function Should not be modified, when the callback is needed,
  356. the HAL_FMPI2C_MspInit could be implemented in the user file
  357. */
  358. }
  359. /**
  360. * @brief FMPI2C MSP DeInit
  361. * @param hfmpi2c : Pointer to a FMPI2C_HandleTypeDef structure that contains
  362. * the configuration information for the specified FMPI2C.
  363. * @retval None
  364. */
  365. __weak void HAL_FMPI2C_MspDeInit(FMPI2C_HandleTypeDef *hfmpi2c)
  366. {
  367. /* NOTE : This function Should not be modified, when the callback is needed,
  368. the HAL_FMPI2C_MspDeInit could be implemented in the user file
  369. */
  370. }
  371. /**
  372. * @}
  373. */
  374. /** @defgroup FMPI2C_Exported_Functions_Group2 I/O operation functions
  375. * @brief Data transfers functions
  376. *
  377. @verbatim
  378. ===============================================================================
  379. ##### IO operation functions #####
  380. ===============================================================================
  381. [..]
  382. This subsection provides a set of functions allowing to manage the FMPI2C data
  383. transfers.
  384. (#) There is two mode of transfer:
  385. (++) Blocking mode : The communication is performed in the polling mode.
  386. The status of all data processing is returned by the same function
  387. after finishing transfer.
  388. (++) No-Blocking mode : The communication is performed using Interrupts
  389. or DMA. These functions return the status of the transfer startup.
  390. The end of the data processing will be indicated through the
  391. dedicated FMPI2C IRQ when using Interrupt mode or the DMA IRQ when
  392. using DMA mode.
  393. (#) Blocking mode functions are :
  394. (++) HAL_FMPI2C_Master_Transmit()
  395. (++) HAL_FMPI2C_Master_Receive()
  396. (++) HAL_FMPI2C_Slave_Transmit()
  397. (++) HAL_FMPI2C_Slave_Receive()
  398. (++) HAL_FMPI2C_Mem_Write()
  399. (++) HAL_FMPI2C_Mem_Read()
  400. (++) HAL_FMPI2C_IsDeviceReady()
  401. (#) No-Blocking mode functions with Interrupt are :
  402. (++) HAL_FMPI2C_Master_Transmit_IT()
  403. (++) HAL_FMPI2C_Master_Receive_IT()
  404. (++) HAL_FMPI2C_Slave_Transmit_IT()
  405. (++) HAL_FMPI2C_Slave_Receive_IT()
  406. (++) HAL_FMPI2C_Mem_Write_IT()
  407. (++) HAL_FMPI2C_Mem_Read_IT()
  408. (#) No-Blocking mode functions with DMA are :
  409. (++) HAL_FMPI2C_Master_Transmit_DMA()
  410. (++) HAL_FMPI2C_Master_Receive_DMA()
  411. (++) HAL_FMPI2C_Slave_Transmit_DMA()
  412. (++) HAL_FMPI2C_Slave_Receive_DMA()
  413. (++) HAL_FMPI2C_Mem_Write_DMA()
  414. (++) HAL_FMPI2C_Mem_Read_DMA()
  415. (#) A set of Transfer Complete Callbacks are provided in No_Blocking mode:
  416. (++) HAL_FMPI2C_MemTxCpltCallback()
  417. (++) HAL_FMPI2C_MemRxCpltCallback()
  418. (++) HAL_FMPI2C_MasterTxCpltCallback()
  419. (++) HAL_FMPI2C_MasterRxCpltCallback()
  420. (++) HAL_FMPI2C_SlaveTxCpltCallback()
  421. (++) HAL_FMPI2C_SlaveRxCpltCallback()
  422. (++) HAL_FMPI2C_ErrorCallback()
  423. @endverbatim
  424. * @{
  425. */
  426. /**
  427. * @brief Transmits in master mode an amount of data in blocking mode.
  428. * @param hfmpi2c : Pointer to a FMPI2C_HandleTypeDef structure that contains
  429. * the configuration information for the specified FMPI2C.
  430. * @param DevAddress: Target device address
  431. * @param pData: Pointer to data buffer
  432. * @param Size: Amount of data to be sent
  433. * @param Timeout: Timeout duration
  434. * @retval HAL status
  435. */
  436. HAL_StatusTypeDef HAL_FMPI2C_Master_Transmit(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  437. {
  438. uint32_t sizetmp = 0;
  439. if(hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  440. {
  441. if((pData == NULL ) || (Size == 0))
  442. {
  443. return HAL_ERROR;
  444. }
  445. if(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_BUSY) == SET)
  446. {
  447. return HAL_BUSY;
  448. }
  449. /* Process Locked */
  450. __HAL_LOCK(hfmpi2c);
  451. hfmpi2c->State = HAL_FMPI2C_STATE_MASTER_BUSY_TX;
  452. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  453. /* Send Slave Address */
  454. /* Set NBYTES to write and reload if size > 255 and generate RESTART */
  455. /* Size > 255, need to set RELOAD bit */
  456. if(Size > 255)
  457. {
  458. FMPI2C_TransferConfig(hfmpi2c,DevAddress,255, FMPI2C_RELOAD_MODE, FMPI2C_GENERATE_START_WRITE);
  459. sizetmp = 255;
  460. }
  461. else
  462. {
  463. FMPI2C_TransferConfig(hfmpi2c,DevAddress,Size, FMPI2C_AUTOEND_MODE, FMPI2C_GENERATE_START_WRITE);
  464. sizetmp = Size;
  465. }
  466. do
  467. {
  468. /* Wait until TXIS flag is set */
  469. if(FMPI2C_WaitOnTXISFlagUntilTimeout(hfmpi2c, Timeout) != HAL_OK)
  470. {
  471. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  472. {
  473. return HAL_ERROR;
  474. }
  475. else
  476. {
  477. return HAL_TIMEOUT;
  478. }
  479. }
  480. /* Write data to TXDR */
  481. hfmpi2c->Instance->TXDR = (*pData++);
  482. sizetmp--;
  483. Size--;
  484. if((sizetmp == 0)&&(Size!=0))
  485. {
  486. /* Wait until TXE flag is set */
  487. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_TCR, RESET, Timeout) != HAL_OK)
  488. {
  489. return HAL_TIMEOUT;
  490. }
  491. if(Size > 255)
  492. {
  493. FMPI2C_TransferConfig(hfmpi2c,DevAddress,255, FMPI2C_RELOAD_MODE, FMPI2C_NO_STARTSTOP);
  494. sizetmp = 255;
  495. }
  496. else
  497. {
  498. FMPI2C_TransferConfig(hfmpi2c,DevAddress,Size, FMPI2C_AUTOEND_MODE, FMPI2C_NO_STARTSTOP);
  499. sizetmp = Size;
  500. }
  501. }
  502. }while(Size > 0);
  503. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  504. /* Wait until STOPF flag is set */
  505. if(FMPI2C_WaitOnSTOPFlagUntilTimeout(hfmpi2c, Timeout) != HAL_OK)
  506. {
  507. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  508. {
  509. return HAL_ERROR;
  510. }
  511. else
  512. {
  513. return HAL_TIMEOUT;
  514. }
  515. }
  516. /* Clear STOP Flag */
  517. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  518. /* Clear Configuration Register 2 */
  519. __HAL_FMPI2C_RESET_CR2(hfmpi2c);
  520. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  521. /* Process Unlocked */
  522. __HAL_UNLOCK(hfmpi2c);
  523. return HAL_OK;
  524. }
  525. else
  526. {
  527. return HAL_BUSY;
  528. }
  529. }
  530. /**
  531. * @brief Receives in master mode an amount of data in blocking mode.
  532. * @param hfmpi2c : Pointer to a FMPI2C_HandleTypeDef structure that contains
  533. * the configuration information for the specified FMPI2C.
  534. * @param DevAddress: Target device address
  535. * @param pData: Pointer to data buffer
  536. * @param Size: Amount of data to be sent
  537. * @param Timeout: Timeout duration
  538. * @retval HAL status
  539. */
  540. HAL_StatusTypeDef HAL_FMPI2C_Master_Receive(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  541. {
  542. uint32_t sizetmp = 0;
  543. if(hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  544. {
  545. if((pData == NULL ) || (Size == 0))
  546. {
  547. return HAL_ERROR;
  548. }
  549. if(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_BUSY) == SET)
  550. {
  551. return HAL_BUSY;
  552. }
  553. /* Process Locked */
  554. __HAL_LOCK(hfmpi2c);
  555. hfmpi2c->State = HAL_FMPI2C_STATE_MASTER_BUSY_RX;
  556. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  557. /* Send Slave Address */
  558. /* Set NBYTES to write and reload if size > 255 and generate RESTART */
  559. /* Size > 255, need to set RELOAD bit */
  560. if(Size > 255)
  561. {
  562. FMPI2C_TransferConfig(hfmpi2c,DevAddress,255, FMPI2C_RELOAD_MODE, FMPI2C_GENERATE_START_READ);
  563. sizetmp = 255;
  564. }
  565. else
  566. {
  567. FMPI2C_TransferConfig(hfmpi2c,DevAddress,Size, FMPI2C_AUTOEND_MODE, FMPI2C_GENERATE_START_READ);
  568. sizetmp = Size;
  569. }
  570. do
  571. {
  572. /* Wait until RXNE flag is set */
  573. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_RXNE, RESET, Timeout) != HAL_OK)
  574. {
  575. return HAL_TIMEOUT;
  576. }
  577. /* Write data to RXDR */
  578. (*pData++) =hfmpi2c->Instance->RXDR;
  579. sizetmp--;
  580. Size--;
  581. if((sizetmp == 0)&&(Size!=0))
  582. {
  583. /* Wait until TCR flag is set */
  584. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_TCR, RESET, Timeout) != HAL_OK)
  585. {
  586. return HAL_TIMEOUT;
  587. }
  588. if(Size > 255)
  589. {
  590. FMPI2C_TransferConfig(hfmpi2c,DevAddress,255, FMPI2C_RELOAD_MODE, FMPI2C_NO_STARTSTOP);
  591. sizetmp = 255;
  592. }
  593. else
  594. {
  595. FMPI2C_TransferConfig(hfmpi2c,DevAddress,Size, FMPI2C_AUTOEND_MODE, FMPI2C_NO_STARTSTOP);
  596. sizetmp = Size;
  597. }
  598. }
  599. }while(Size > 0);
  600. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  601. /* Wait until STOPF flag is set */
  602. if(FMPI2C_WaitOnSTOPFlagUntilTimeout(hfmpi2c, FMPI2C_TIMEOUT_STOPF) != HAL_OK)
  603. {
  604. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  605. {
  606. return HAL_ERROR;
  607. }
  608. else
  609. {
  610. return HAL_TIMEOUT;
  611. }
  612. }
  613. /* Clear STOP Flag */
  614. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  615. /* Clear Configuration Register 2 */
  616. __HAL_FMPI2C_RESET_CR2(hfmpi2c);
  617. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  618. /* Process Unlocked */
  619. __HAL_UNLOCK(hfmpi2c);
  620. return HAL_OK;
  621. }
  622. else
  623. {
  624. return HAL_BUSY;
  625. }
  626. }
  627. /**
  628. * @brief Transmits in slave mode an amount of data in blocking mode.
  629. * @param hfmpi2c : Pointer to a FMPI2C_HandleTypeDef structure that contains
  630. * the configuration information for the specified FMPI2C.
  631. * @param pData: Pointer to data buffer
  632. * @param Size: Amount of data to be sent
  633. * @param Timeout: Timeout duration
  634. * @retval HAL status
  635. */
  636. HAL_StatusTypeDef HAL_FMPI2C_Slave_Transmit(FMPI2C_HandleTypeDef *hfmpi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  637. {
  638. if(hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  639. {
  640. if((pData == NULL ) || (Size == 0))
  641. {
  642. return HAL_ERROR;
  643. }
  644. /* Process Locked */
  645. __HAL_LOCK(hfmpi2c);
  646. hfmpi2c->State = HAL_FMPI2C_STATE_SLAVE_BUSY_RX;
  647. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  648. /* Enable Address Acknowledge */
  649. hfmpi2c->Instance->CR2 &= ~FMPI2C_CR2_NACK;
  650. /* Wait until ADDR flag is set */
  651. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_ADDR, RESET, Timeout) != HAL_OK)
  652. {
  653. /* Disable Address Acknowledge */
  654. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  655. return HAL_TIMEOUT;
  656. }
  657. /* Clear ADDR flag */
  658. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c,FMPI2C_FLAG_ADDR);
  659. /* If 10bit addressing mode is selected */
  660. if(hfmpi2c->Init.AddressingMode == FMPI2C_ADDRESSINGMODE_10BIT)
  661. {
  662. /* Wait until ADDR flag is set */
  663. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_ADDR, RESET, Timeout) != HAL_OK)
  664. {
  665. /* Disable Address Acknowledge */
  666. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  667. return HAL_TIMEOUT;
  668. }
  669. /* Clear ADDR flag */
  670. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c,FMPI2C_FLAG_ADDR);
  671. }
  672. /* Wait until DIR flag is set Transmitter mode */
  673. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_DIR, RESET, Timeout) != HAL_OK)
  674. {
  675. /* Disable Address Acknowledge */
  676. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  677. return HAL_TIMEOUT;
  678. }
  679. do
  680. {
  681. /* Wait until TXIS flag is set */
  682. if(FMPI2C_WaitOnTXISFlagUntilTimeout(hfmpi2c, Timeout) != HAL_OK)
  683. {
  684. /* Disable Address Acknowledge */
  685. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  686. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  687. {
  688. return HAL_ERROR;
  689. }
  690. else
  691. {
  692. return HAL_TIMEOUT;
  693. }
  694. }
  695. /* Read data from TXDR */
  696. hfmpi2c->Instance->TXDR = (*pData++);
  697. Size--;
  698. }while(Size > 0);
  699. /* Wait until STOP flag is set */
  700. if(FMPI2C_WaitOnSTOPFlagUntilTimeout(hfmpi2c, FMPI2C_TIMEOUT_STOPF) != HAL_OK)
  701. {
  702. /* Disable Address Acknowledge */
  703. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  704. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  705. {
  706. /* Normal use case for Transmitter mode */
  707. /* A NACK is generated to confirm the end of transfer */
  708. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  709. }
  710. else
  711. {
  712. return HAL_TIMEOUT;
  713. }
  714. }
  715. /* Clear STOP flag */
  716. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c,FMPI2C_FLAG_STOPF);
  717. /* Wait until BUSY flag is reset */
  718. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_BUSY, SET, Timeout) != HAL_OK)
  719. {
  720. /* Disable Address Acknowledge */
  721. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  722. return HAL_TIMEOUT;
  723. }
  724. /* Disable Address Acknowledge */
  725. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  726. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  727. /* Process Unlocked */
  728. __HAL_UNLOCK(hfmpi2c);
  729. return HAL_OK;
  730. }
  731. else
  732. {
  733. return HAL_BUSY;
  734. }
  735. }
  736. /**
  737. * @brief Receive in slave mode an amount of data in blocking mode
  738. * @param hfmpi2c : Pointer to a FMPI2C_HandleTypeDef structure that contains
  739. * the configuration information for the specified FMPI2C.
  740. * @param pData: Pointer to data buffer
  741. * @param Size: Amount of data to be sent
  742. * @param Timeout: Timeout duration
  743. * @retval HAL status
  744. */
  745. HAL_StatusTypeDef HAL_FMPI2C_Slave_Receive(FMPI2C_HandleTypeDef *hfmpi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  746. {
  747. if(hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  748. {
  749. if((pData == NULL ) || (Size == 0))
  750. {
  751. return HAL_ERROR;
  752. }
  753. /* Process Locked */
  754. __HAL_LOCK(hfmpi2c);
  755. hfmpi2c->State = HAL_FMPI2C_STATE_SLAVE_BUSY_RX;
  756. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  757. /* Enable Address Acknowledge */
  758. hfmpi2c->Instance->CR2 &= ~FMPI2C_CR2_NACK;
  759. /* Wait until ADDR flag is set */
  760. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_ADDR, RESET, Timeout) != HAL_OK)
  761. {
  762. /* Disable Address Acknowledge */
  763. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  764. return HAL_TIMEOUT;
  765. }
  766. /* Clear ADDR flag */
  767. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c,FMPI2C_FLAG_ADDR);
  768. /* Wait until DIR flag is reset Receiver mode */
  769. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_DIR, SET, Timeout) != HAL_OK)
  770. {
  771. /* Disable Address Acknowledge */
  772. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  773. return HAL_TIMEOUT;
  774. }
  775. while(Size > 0)
  776. {
  777. /* Wait until RXNE flag is set */
  778. if(FMPI2C_WaitOnRXNEFlagUntilTimeout(hfmpi2c, Timeout) != HAL_OK)
  779. {
  780. /* Disable Address Acknowledge */
  781. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  782. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_TIMEOUT)
  783. {
  784. return HAL_TIMEOUT;
  785. }
  786. else
  787. {
  788. return HAL_ERROR;
  789. }
  790. }
  791. /* Read data from RXDR */
  792. (*pData++) = hfmpi2c->Instance->RXDR;
  793. Size--;
  794. }
  795. /* Wait until STOP flag is set */
  796. if(FMPI2C_WaitOnSTOPFlagUntilTimeout(hfmpi2c, FMPI2C_TIMEOUT_STOPF) != HAL_OK)
  797. {
  798. /* Disable Address Acknowledge */
  799. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  800. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  801. {
  802. return HAL_ERROR;
  803. }
  804. else
  805. {
  806. return HAL_TIMEOUT;
  807. }
  808. }
  809. /* Clear STOP flag */
  810. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c,FMPI2C_FLAG_STOPF);
  811. /* Wait until BUSY flag is reset */
  812. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_BUSY, SET, Timeout) != HAL_OK)
  813. {
  814. /* Disable Address Acknowledge */
  815. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  816. return HAL_TIMEOUT;
  817. }
  818. /* Disable Address Acknowledge */
  819. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  820. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  821. /* Process Unlocked */
  822. __HAL_UNLOCK(hfmpi2c);
  823. return HAL_OK;
  824. }
  825. else
  826. {
  827. return HAL_BUSY;
  828. }
  829. }
  830. /**
  831. * @brief Transmit in master mode an amount of data in no-blocking mode with Interrupt
  832. * @param hfmpi2c : Pointer to a FMPI2C_HandleTypeDef structure that contains
  833. * the configuration information for the specified FMPI2C.
  834. * @param DevAddress: Target device address
  835. * @param pData: Pointer to data buffer
  836. * @param Size: Amount of data to be sent
  837. * @retval HAL status
  838. */
  839. HAL_StatusTypeDef HAL_FMPI2C_Master_Transmit_IT(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
  840. {
  841. if(hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  842. {
  843. if((pData == NULL) || (Size == 0))
  844. {
  845. return HAL_ERROR;
  846. }
  847. if(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_BUSY) == SET)
  848. {
  849. return HAL_BUSY;
  850. }
  851. /* Process Locked */
  852. __HAL_LOCK(hfmpi2c);
  853. hfmpi2c->State = HAL_FMPI2C_STATE_MASTER_BUSY_TX;
  854. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  855. hfmpi2c->pBuffPtr = pData;
  856. hfmpi2c->XferCount = Size;
  857. if(Size > 255)
  858. {
  859. hfmpi2c->XferSize = 255;
  860. }
  861. else
  862. {
  863. hfmpi2c->XferSize = Size;
  864. }
  865. /* Send Slave Address */
  866. /* Set NBYTES to write and reload if size > 255 and generate RESTART */
  867. if( (hfmpi2c->XferSize == 255) && (hfmpi2c->XferSize < hfmpi2c->XferCount) )
  868. {
  869. FMPI2C_TransferConfig(hfmpi2c,DevAddress,hfmpi2c->XferSize, FMPI2C_RELOAD_MODE, FMPI2C_GENERATE_START_WRITE);
  870. }
  871. else
  872. {
  873. FMPI2C_TransferConfig(hfmpi2c,DevAddress,hfmpi2c->XferSize, FMPI2C_AUTOEND_MODE, FMPI2C_GENERATE_START_WRITE);
  874. }
  875. /* Process Unlocked */
  876. __HAL_UNLOCK(hfmpi2c);
  877. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  878. to avoid the risk of FMPI2C interrupt handle execution before current
  879. process unlock */
  880. /* Enable ERR, TC, STOP, NACK, TXI interrupt */
  881. /* possible to enable all of these */
  882. /* FMPI2C_IT_ERRI | FMPI2C_IT_TCI| FMPI2C_IT_STOPI| FMPI2C_IT_NACKI | FMPI2C_IT_ADDRI | FMPI2C_IT_RXI | FMPI2C_IT_TXI */
  883. __HAL_FMPI2C_ENABLE_IT(hfmpi2c,FMPI2C_IT_ERRI | FMPI2C_IT_TCI| FMPI2C_IT_STOPI| FMPI2C_IT_NACKI | FMPI2C_IT_TXI );
  884. return HAL_OK;
  885. }
  886. else
  887. {
  888. return HAL_BUSY;
  889. }
  890. }
  891. /**
  892. * @brief Receive in master mode an amount of data in no-blocking mode with Interrupt
  893. * @param hfmpi2c : Pointer to a FMPI2C_HandleTypeDef structure that contains
  894. * the configuration information for the specified FMPI2C.
  895. * @param DevAddress: Target device address
  896. * @param pData: Pointer to data buffer
  897. * @param Size: Amount of data to be sent
  898. * @retval HAL status
  899. */
  900. HAL_StatusTypeDef HAL_FMPI2C_Master_Receive_IT(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
  901. {
  902. if(hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  903. {
  904. if((pData == NULL) || (Size == 0))
  905. {
  906. return HAL_ERROR;
  907. }
  908. if(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_BUSY) == SET)
  909. {
  910. return HAL_BUSY;
  911. }
  912. /* Process Locked */
  913. __HAL_LOCK(hfmpi2c);
  914. hfmpi2c->State = HAL_FMPI2C_STATE_MASTER_BUSY_RX;
  915. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  916. hfmpi2c->pBuffPtr = pData;
  917. hfmpi2c->XferCount = Size;
  918. if(Size > 255)
  919. {
  920. hfmpi2c->XferSize = 255;
  921. }
  922. else
  923. {
  924. hfmpi2c->XferSize = Size;
  925. }
  926. /* Send Slave Address */
  927. /* Set NBYTES to write and reload if size > 255 and generate RESTART */
  928. if( (hfmpi2c->XferSize == 255) && (hfmpi2c->XferSize < hfmpi2c->XferCount) )
  929. {
  930. FMPI2C_TransferConfig(hfmpi2c,DevAddress,hfmpi2c->XferSize, FMPI2C_RELOAD_MODE, FMPI2C_GENERATE_START_READ);
  931. }
  932. else
  933. {
  934. FMPI2C_TransferConfig(hfmpi2c,DevAddress,hfmpi2c->XferSize, FMPI2C_AUTOEND_MODE, FMPI2C_GENERATE_START_READ);
  935. }
  936. /* Process Unlocked */
  937. __HAL_UNLOCK(hfmpi2c);
  938. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  939. to avoid the risk of FMPI2C interrupt handle execution before current
  940. process unlock */
  941. /* Enable ERR, TC, STOP, NACK, RXI interrupt */
  942. /* possible to enable all of these */
  943. /* FMPI2C_IT_ERRI | FMPI2C_IT_TCI| FMPI2C_IT_STOPI| FMPI2C_IT_NACKI | FMPI2C_IT_ADDRI | FMPI2C_IT_RXI | FMPI2C_IT_TXI */
  944. __HAL_FMPI2C_ENABLE_IT(hfmpi2c,FMPI2C_IT_ERRI | FMPI2C_IT_TCI | FMPI2C_IT_STOPI | FMPI2C_IT_NACKI | FMPI2C_IT_RXI );
  945. return HAL_OK;
  946. }
  947. else
  948. {
  949. return HAL_BUSY;
  950. }
  951. }
  952. /**
  953. * @brief Transmit in slave mode an amount of data in no-blocking mode with Interrupt
  954. * @param hfmpi2c : Pointer to a FMPI2C_HandleTypeDef structure that contains
  955. * the configuration information for the specified FMPI2C.
  956. * @param pData: Pointer to data buffer
  957. * @param Size: Amount of data to be sent
  958. * @retval HAL status
  959. */
  960. HAL_StatusTypeDef HAL_FMPI2C_Slave_Transmit_IT(FMPI2C_HandleTypeDef *hfmpi2c, uint8_t *pData, uint16_t Size)
  961. {
  962. if(hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  963. {
  964. if((pData == NULL) || (Size == 0))
  965. {
  966. return HAL_ERROR;
  967. }
  968. /* Process Locked */
  969. __HAL_LOCK(hfmpi2c);
  970. hfmpi2c->State = HAL_FMPI2C_STATE_SLAVE_BUSY_TX;
  971. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  972. /* Enable Address Acknowledge */
  973. hfmpi2c->Instance->CR2 &= ~FMPI2C_CR2_NACK;
  974. hfmpi2c->pBuffPtr = pData;
  975. hfmpi2c->XferSize = Size;
  976. hfmpi2c->XferCount = Size;
  977. /* Process Unlocked */
  978. __HAL_UNLOCK(hfmpi2c);
  979. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  980. to avoid the risk of FMPI2C interrupt handle execution before current
  981. process unlock */
  982. /* Enable ERR, TC, STOP, NACK, TXI interrupt */
  983. /* possible to enable all of these */
  984. /* FMPI2C_IT_ERRI | FMPI2C_IT_TCI| FMPI2C_IT_STOPI| FMPI2C_IT_NACKI | FMPI2C_IT_ADDRI | FMPI2C_IT_RXI | FMPI2C_IT_TXI */
  985. __HAL_FMPI2C_ENABLE_IT(hfmpi2c,FMPI2C_IT_ERRI | FMPI2C_IT_TCI| FMPI2C_IT_STOPI | FMPI2C_IT_NACKI | FMPI2C_IT_ADDRI | FMPI2C_IT_TXI );
  986. return HAL_OK;
  987. }
  988. else
  989. {
  990. return HAL_BUSY;
  991. }
  992. }
  993. /**
  994. * @brief Receive in slave mode an amount of data in no-blocking mode with Interrupt
  995. * @param hfmpi2c : Pointer to a FMPI2C_HandleTypeDef structure that contains
  996. * the configuration information for the specified FMPI2C.
  997. * @param pData: Pointer to data buffer
  998. * @param Size: Amount of data to be sent
  999. * @retval HAL status
  1000. */
  1001. HAL_StatusTypeDef HAL_FMPI2C_Slave_Receive_IT(FMPI2C_HandleTypeDef *hfmpi2c, uint8_t *pData, uint16_t Size)
  1002. {
  1003. if(hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1004. {
  1005. if((pData == NULL) || (Size == 0))
  1006. {
  1007. return HAL_ERROR;
  1008. }
  1009. /* Process Locked */
  1010. __HAL_LOCK(hfmpi2c);
  1011. hfmpi2c->State = HAL_FMPI2C_STATE_SLAVE_BUSY_RX;
  1012. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1013. /* Enable Address Acknowledge */
  1014. hfmpi2c->Instance->CR2 &= ~FMPI2C_CR2_NACK;
  1015. hfmpi2c->pBuffPtr = pData;
  1016. hfmpi2c->XferSize = Size;
  1017. hfmpi2c->XferCount = Size;
  1018. /* Process Unlocked */
  1019. __HAL_UNLOCK(hfmpi2c);
  1020. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  1021. to avoid the risk of FMPI2C interrupt handle execution before current
  1022. process unlock */
  1023. /* Enable ERR, TC, STOP, NACK, RXI interrupt */
  1024. /* possible to enable all of these */
  1025. /* FMPI2C_IT_ERRI | FMPI2C_IT_TCI| FMPI2C_IT_STOPI| FMPI2C_IT_NACKI | FMPI2C_IT_ADDRI | FMPI2C_IT_RXI | FMPI2C_IT_TXI */
  1026. __HAL_FMPI2C_ENABLE_IT(hfmpi2c,FMPI2C_IT_ERRI | FMPI2C_IT_TCI | FMPI2C_IT_STOPI | FMPI2C_IT_NACKI | FMPI2C_IT_ADDRI | FMPI2C_IT_RXI);
  1027. return HAL_OK;
  1028. }
  1029. else
  1030. {
  1031. return HAL_BUSY;
  1032. }
  1033. }
  1034. /**
  1035. * @brief Transmit in master mode an amount of data in no-blocking mode with DMA
  1036. * @param hfmpi2c : Pointer to a FMPI2C_HandleTypeDef structure that contains
  1037. * the configuration information for the specified FMPI2C.
  1038. * @param DevAddress: Target device address
  1039. * @param pData: Pointer to data buffer
  1040. * @param Size: Amount of data to be sent
  1041. * @retval HAL status
  1042. */
  1043. HAL_StatusTypeDef HAL_FMPI2C_Master_Transmit_DMA(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
  1044. {
  1045. if(hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1046. {
  1047. if((pData == NULL) || (Size == 0))
  1048. {
  1049. return HAL_ERROR;
  1050. }
  1051. if(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_BUSY) == SET)
  1052. {
  1053. return HAL_BUSY;
  1054. }
  1055. /* Process Locked */
  1056. __HAL_LOCK(hfmpi2c);
  1057. hfmpi2c->State = HAL_FMPI2C_STATE_MASTER_BUSY_TX;
  1058. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1059. hfmpi2c->pBuffPtr = pData;
  1060. hfmpi2c->XferCount = Size;
  1061. if(Size > 255)
  1062. {
  1063. hfmpi2c->XferSize = 255;
  1064. }
  1065. else
  1066. {
  1067. hfmpi2c->XferSize = Size;
  1068. }
  1069. /* Set the FMPI2C DMA transfer complete callback */
  1070. hfmpi2c->hdmatx->XferCpltCallback = FMPI2C_DMAMasterTransmitCplt;
  1071. /* Set the DMA error callback */
  1072. hfmpi2c->hdmatx->XferErrorCallback = FMPI2C_DMAError;
  1073. /* Enable the DMA channel */
  1074. HAL_DMA_Start_IT(hfmpi2c->hdmatx, (uint32_t)pData, (uint32_t)&hfmpi2c->Instance->TXDR, hfmpi2c->XferSize);
  1075. /* Send Slave Address */
  1076. /* Set NBYTES to write and reload if size > 255 and generate RESTART */
  1077. if( (hfmpi2c->XferSize == 255) && (hfmpi2c->XferSize < hfmpi2c->XferCount) )
  1078. {
  1079. FMPI2C_TransferConfig(hfmpi2c,DevAddress,hfmpi2c->XferSize, FMPI2C_RELOAD_MODE, FMPI2C_GENERATE_START_WRITE);
  1080. }
  1081. else
  1082. {
  1083. FMPI2C_TransferConfig(hfmpi2c,DevAddress,hfmpi2c->XferSize, FMPI2C_AUTOEND_MODE, FMPI2C_GENERATE_START_WRITE);
  1084. }
  1085. /* Wait until TXIS flag is set */
  1086. if(FMPI2C_WaitOnTXISFlagUntilTimeout(hfmpi2c, FMPI2C_TIMEOUT_TXIS) != HAL_OK)
  1087. {
  1088. /* Disable Address Acknowledge */
  1089. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  1090. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  1091. {
  1092. return HAL_ERROR;
  1093. }
  1094. else
  1095. {
  1096. return HAL_TIMEOUT;
  1097. }
  1098. }
  1099. /* Enable DMA Request */
  1100. hfmpi2c->Instance->CR1 |= FMPI2C_CR1_TXDMAEN;
  1101. /* Process Unlocked */
  1102. __HAL_UNLOCK(hfmpi2c);
  1103. return HAL_OK;
  1104. }
  1105. else
  1106. {
  1107. return HAL_BUSY;
  1108. }
  1109. }
  1110. /**
  1111. * @brief Receive in master mode an amount of data in no-blocking mode with DMA
  1112. * @param hfmpi2c : Pointer to a FMPI2C_HandleTypeDef structure that contains
  1113. * the configuration information for the specified FMPI2C.
  1114. * @param DevAddress: Target device address
  1115. * @param pData: Pointer to data buffer
  1116. * @param Size: Amount of data to be sent
  1117. * @retval HAL status
  1118. */
  1119. HAL_StatusTypeDef HAL_FMPI2C_Master_Receive_DMA(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
  1120. {
  1121. if(hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1122. {
  1123. if((pData == NULL) || (Size == 0))
  1124. {
  1125. return HAL_ERROR;
  1126. }
  1127. if(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_BUSY) == SET)
  1128. {
  1129. return HAL_BUSY;
  1130. }
  1131. /* Process Locked */
  1132. __HAL_LOCK(hfmpi2c);
  1133. hfmpi2c->State = HAL_FMPI2C_STATE_MASTER_BUSY_RX;
  1134. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1135. hfmpi2c->pBuffPtr = pData;
  1136. hfmpi2c->XferCount = Size;
  1137. if(Size > 255)
  1138. {
  1139. hfmpi2c->XferSize = 255;
  1140. }
  1141. else
  1142. {
  1143. hfmpi2c->XferSize = Size;
  1144. }
  1145. /* Set the FMPI2C DMA transfer complete callback */
  1146. hfmpi2c->hdmarx->XferCpltCallback = FMPI2C_DMAMasterReceiveCplt;
  1147. /* Set the DMA error callback */
  1148. hfmpi2c->hdmarx->XferErrorCallback = FMPI2C_DMAError;
  1149. /* Enable the DMA channel */
  1150. HAL_DMA_Start_IT(hfmpi2c->hdmarx, (uint32_t)&hfmpi2c->Instance->RXDR, (uint32_t)pData, hfmpi2c->XferSize);
  1151. /* Send Slave Address */
  1152. /* Set NBYTES to write and reload if size > 255 and generate RESTART */
  1153. if( (hfmpi2c->XferSize == 255) && (hfmpi2c->XferSize < hfmpi2c->XferCount) )
  1154. {
  1155. FMPI2C_TransferConfig(hfmpi2c,DevAddress,hfmpi2c->XferSize, FMPI2C_RELOAD_MODE, FMPI2C_GENERATE_START_READ);
  1156. }
  1157. else
  1158. {
  1159. FMPI2C_TransferConfig(hfmpi2c,DevAddress,hfmpi2c->XferSize, FMPI2C_AUTOEND_MODE, FMPI2C_GENERATE_START_READ);
  1160. }
  1161. /* Wait until RXNE flag is set */
  1162. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_RXNE, RESET, FMPI2C_TIMEOUT_RXNE) != HAL_OK)
  1163. {
  1164. return HAL_TIMEOUT;
  1165. }
  1166. /* Enable DMA Request */
  1167. hfmpi2c->Instance->CR1 |= FMPI2C_CR1_RXDMAEN;
  1168. /* Process Unlocked */
  1169. __HAL_UNLOCK(hfmpi2c);
  1170. return HAL_OK;
  1171. }
  1172. else
  1173. {
  1174. return HAL_BUSY;
  1175. }
  1176. }
  1177. /**
  1178. * @brief Transmit in slave mode an amount of data in no-blocking mode with DMA
  1179. * @param hfmpi2c : Pointer to a FMPI2C_HandleTypeDef structure that contains
  1180. * the configuration information for the specified FMPI2C.
  1181. * @param pData: Pointer to data buffer
  1182. * @param Size: Amount of data to be sent
  1183. * @retval HAL status
  1184. */
  1185. HAL_StatusTypeDef HAL_FMPI2C_Slave_Transmit_DMA(FMPI2C_HandleTypeDef *hfmpi2c, uint8_t *pData, uint16_t Size)
  1186. {
  1187. if(hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1188. {
  1189. if((pData == NULL) || (Size == 0))
  1190. {
  1191. return HAL_ERROR;
  1192. }
  1193. /* Process Locked */
  1194. __HAL_LOCK(hfmpi2c);
  1195. hfmpi2c->State = HAL_FMPI2C_STATE_SLAVE_BUSY_TX;
  1196. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1197. hfmpi2c->pBuffPtr = pData;
  1198. hfmpi2c->XferCount = Size;
  1199. hfmpi2c->XferSize = Size;
  1200. /* Set the FMPI2C DMA transfer complete callback */
  1201. hfmpi2c->hdmatx->XferCpltCallback = FMPI2C_DMASlaveTransmitCplt;
  1202. /* Set the DMA error callback */
  1203. hfmpi2c->hdmatx->XferErrorCallback = FMPI2C_DMAError;
  1204. /* Enable the DMA channel */
  1205. HAL_DMA_Start_IT(hfmpi2c->hdmatx, (uint32_t)pData, (uint32_t)&hfmpi2c->Instance->TXDR, hfmpi2c->XferSize);
  1206. /* Enable Address Acknowledge */
  1207. hfmpi2c->Instance->CR2 &= ~FMPI2C_CR2_NACK;
  1208. /* Wait until ADDR flag is set */
  1209. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_ADDR, RESET, FMPI2C_TIMEOUT_ADDR) != HAL_OK)
  1210. {
  1211. /* Disable Address Acknowledge */
  1212. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  1213. return HAL_TIMEOUT;
  1214. }
  1215. /* Clear ADDR flag */
  1216. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c,FMPI2C_FLAG_ADDR);
  1217. /* If 10bits addressing mode is selected */
  1218. if(hfmpi2c->Init.AddressingMode == FMPI2C_ADDRESSINGMODE_10BIT)
  1219. {
  1220. /* Wait until ADDR flag is set */
  1221. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_ADDR, RESET, FMPI2C_TIMEOUT_ADDR) != HAL_OK)
  1222. {
  1223. /* Disable Address Acknowledge */
  1224. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  1225. return HAL_TIMEOUT;
  1226. }
  1227. /* Clear ADDR flag */
  1228. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c,FMPI2C_FLAG_ADDR);
  1229. }
  1230. /* Wait until DIR flag is set Transmitter mode */
  1231. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_DIR, RESET, FMPI2C_TIMEOUT_BUSY) != HAL_OK)
  1232. {
  1233. /* Disable Address Acknowledge */
  1234. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  1235. return HAL_TIMEOUT;
  1236. }
  1237. /* Enable DMA Request */
  1238. hfmpi2c->Instance->CR1 |= FMPI2C_CR1_TXDMAEN;
  1239. /* Process Unlocked */
  1240. __HAL_UNLOCK(hfmpi2c);
  1241. return HAL_OK;
  1242. }
  1243. else
  1244. {
  1245. return HAL_BUSY;
  1246. }
  1247. }
  1248. /**
  1249. * @brief Receive in slave mode an amount of data in no-blocking mode with DMA
  1250. * @param hfmpi2c : Pointer to a FMPI2C_HandleTypeDef structure that contains
  1251. * the configuration information for the specified FMPI2C.
  1252. * @param pData: Pointer to data buffer
  1253. * @param Size: Amount of data to be sent
  1254. * @retval HAL status
  1255. */
  1256. HAL_StatusTypeDef HAL_FMPI2C_Slave_Receive_DMA(FMPI2C_HandleTypeDef *hfmpi2c, uint8_t *pData, uint16_t Size)
  1257. {
  1258. if(hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1259. {
  1260. if((pData == NULL) || (Size == 0))
  1261. {
  1262. return HAL_ERROR;
  1263. }
  1264. /* Process Locked */
  1265. __HAL_LOCK(hfmpi2c);
  1266. hfmpi2c->State = HAL_FMPI2C_STATE_SLAVE_BUSY_RX;
  1267. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1268. hfmpi2c->pBuffPtr = pData;
  1269. hfmpi2c->XferSize = Size;
  1270. hfmpi2c->XferCount = Size;
  1271. /* Set the FMPI2C DMA transfer complete callback */
  1272. hfmpi2c->hdmarx->XferCpltCallback = FMPI2C_DMASlaveReceiveCplt;
  1273. /* Set the DMA error callback */
  1274. hfmpi2c->hdmarx->XferErrorCallback = FMPI2C_DMAError;
  1275. /* Enable the DMA channel */
  1276. HAL_DMA_Start_IT(hfmpi2c->hdmarx, (uint32_t)&hfmpi2c->Instance->RXDR, (uint32_t)pData, Size);
  1277. /* Enable Address Acknowledge */
  1278. hfmpi2c->Instance->CR2 &= ~FMPI2C_CR2_NACK;
  1279. /* Wait until ADDR flag is set */
  1280. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_ADDR, RESET, FMPI2C_TIMEOUT_ADDR) != HAL_OK)
  1281. {
  1282. /* Disable Address Acknowledge */
  1283. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  1284. return HAL_TIMEOUT;
  1285. }
  1286. /* Clear ADDR flag */
  1287. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c,FMPI2C_FLAG_ADDR);
  1288. /* Wait until DIR flag is set Receiver mode */
  1289. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_DIR, SET, FMPI2C_TIMEOUT_DIR) != HAL_OK)
  1290. {
  1291. /* Disable Address Acknowledge */
  1292. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  1293. return HAL_TIMEOUT;
  1294. }
  1295. /* Enable DMA Request */
  1296. hfmpi2c->Instance->CR1 |= FMPI2C_CR1_RXDMAEN;
  1297. /* Process Unlocked */
  1298. __HAL_UNLOCK(hfmpi2c);
  1299. return HAL_OK;
  1300. }
  1301. else
  1302. {
  1303. return HAL_BUSY;
  1304. }
  1305. }
  1306. /**
  1307. * @brief Write an amount of data in blocking mode to a specific memory address
  1308. * @param hfmpi2c : Pointer to a FMPI2C_HandleTypeDef structure that contains
  1309. * the configuration information for the specified FMPI2C.
  1310. * @param DevAddress: Target device address
  1311. * @param MemAddress: Internal memory address
  1312. * @param MemAddSize: Size of internal memory address
  1313. * @param pData: Pointer to data buffer
  1314. * @param Size: Amount of data to be sent
  1315. * @param Timeout: Timeout duration
  1316. * @retval HAL status
  1317. */
  1318. 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)
  1319. {
  1320. uint32_t Sizetmp = 0;
  1321. /* Check the parameters */
  1322. assert_param(IS_FMPI2C_MEMADD_SIZE(MemAddSize));
  1323. if(hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1324. {
  1325. if((pData == NULL) || (Size == 0))
  1326. {
  1327. return HAL_ERROR;
  1328. }
  1329. if(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_BUSY) == SET)
  1330. {
  1331. return HAL_BUSY;
  1332. }
  1333. /* Process Locked */
  1334. __HAL_LOCK(hfmpi2c);
  1335. hfmpi2c->State = HAL_FMPI2C_STATE_MEM_BUSY_TX;
  1336. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1337. /* Send Slave Address and Memory Address */
  1338. if(FMPI2C_RequestMemoryWrite(hfmpi2c, DevAddress, MemAddress, MemAddSize, Timeout) != HAL_OK)
  1339. {
  1340. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  1341. {
  1342. /* Process Unlocked */
  1343. __HAL_UNLOCK(hfmpi2c);
  1344. return HAL_ERROR;
  1345. }
  1346. else
  1347. {
  1348. /* Process Unlocked */
  1349. __HAL_UNLOCK(hfmpi2c);
  1350. return HAL_TIMEOUT;
  1351. }
  1352. }
  1353. /* Set NBYTES to write and reload if size > 255 */
  1354. /* Size > 255, need to set RELOAD bit */
  1355. if(Size > 255)
  1356. {
  1357. FMPI2C_TransferConfig(hfmpi2c,DevAddress,255, FMPI2C_RELOAD_MODE, FMPI2C_NO_STARTSTOP);
  1358. Sizetmp = 255;
  1359. }
  1360. else
  1361. {
  1362. FMPI2C_TransferConfig(hfmpi2c,DevAddress,Size, FMPI2C_AUTOEND_MODE, FMPI2C_NO_STARTSTOP);
  1363. Sizetmp = Size;
  1364. }
  1365. do
  1366. {
  1367. /* Wait until TXIS flag is set */
  1368. if(FMPI2C_WaitOnTXISFlagUntilTimeout(hfmpi2c, Timeout) != HAL_OK)
  1369. {
  1370. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  1371. {
  1372. return HAL_ERROR;
  1373. }
  1374. else
  1375. {
  1376. return HAL_TIMEOUT;
  1377. }
  1378. }
  1379. /* Write data to DR */
  1380. hfmpi2c->Instance->TXDR = (*pData++);
  1381. Sizetmp--;
  1382. Size--;
  1383. if((Sizetmp == 0)&&(Size!=0))
  1384. {
  1385. /* Wait until TCR flag is set */
  1386. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_TCR, RESET, Timeout) != HAL_OK)
  1387. {
  1388. return HAL_TIMEOUT;
  1389. }
  1390. if(Size > 255)
  1391. {
  1392. FMPI2C_TransferConfig(hfmpi2c,DevAddress,255, FMPI2C_RELOAD_MODE, FMPI2C_NO_STARTSTOP);
  1393. Sizetmp = 255;
  1394. }
  1395. else
  1396. {
  1397. FMPI2C_TransferConfig(hfmpi2c,DevAddress,Size, FMPI2C_AUTOEND_MODE, FMPI2C_NO_STARTSTOP);
  1398. Sizetmp = Size;
  1399. }
  1400. }
  1401. }while(Size > 0);
  1402. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  1403. /* Wait until STOPF flag is reset */
  1404. if(FMPI2C_WaitOnSTOPFlagUntilTimeout(hfmpi2c, FMPI2C_TIMEOUT_STOPF) != HAL_OK)
  1405. {
  1406. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  1407. {
  1408. return HAL_ERROR;
  1409. }
  1410. else
  1411. {
  1412. return HAL_TIMEOUT;
  1413. }
  1414. }
  1415. /* Clear STOP Flag */
  1416. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  1417. /* Clear Configuration Register 2 */
  1418. __HAL_FMPI2C_RESET_CR2(hfmpi2c);
  1419. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  1420. /* Process Unlocked */
  1421. __HAL_UNLOCK(hfmpi2c);
  1422. return HAL_OK;
  1423. }
  1424. else
  1425. {
  1426. return HAL_BUSY;
  1427. }
  1428. }
  1429. /**
  1430. * @brief Read an amount of data in blocking mode from a specific memory address
  1431. * @param hfmpi2c : Pointer to a FMPI2C_HandleTypeDef structure that contains
  1432. * the configuration information for the specified FMPI2C.
  1433. * @param DevAddress: Target device address
  1434. * @param MemAddress: Internal memory address
  1435. * @param MemAddSize: Size of internal memory address
  1436. * @param pData: Pointer to data buffer
  1437. * @param Size: Amount of data to be sent
  1438. * @param Timeout: Timeout duration
  1439. * @retval HAL status
  1440. */
  1441. 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)
  1442. {
  1443. uint32_t Sizetmp = 0;
  1444. /* Check the parameters */
  1445. assert_param(IS_FMPI2C_MEMADD_SIZE(MemAddSize));
  1446. if(hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1447. {
  1448. if((pData == NULL) || (Size == 0))
  1449. {
  1450. return HAL_ERROR;
  1451. }
  1452. if(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_BUSY) == SET)
  1453. {
  1454. return HAL_BUSY;
  1455. }
  1456. /* Process Locked */
  1457. __HAL_LOCK(hfmpi2c);
  1458. hfmpi2c->State = HAL_FMPI2C_STATE_MEM_BUSY_RX;
  1459. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1460. /* Send Slave Address and Memory Address */
  1461. if(FMPI2C_RequestMemoryRead(hfmpi2c, DevAddress, MemAddress, MemAddSize, Timeout) != HAL_OK)
  1462. {
  1463. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  1464. {
  1465. /* Process Unlocked */
  1466. __HAL_UNLOCK(hfmpi2c);
  1467. return HAL_ERROR;
  1468. }
  1469. else
  1470. {
  1471. /* Process Unlocked */
  1472. __HAL_UNLOCK(hfmpi2c);
  1473. return HAL_TIMEOUT;
  1474. }
  1475. }
  1476. /* Send Slave Address */
  1477. /* Set NBYTES to write and reload if size > 255 and generate RESTART */
  1478. /* Size > 255, need to set RELOAD bit */
  1479. if(Size > 255)
  1480. {
  1481. FMPI2C_TransferConfig(hfmpi2c,DevAddress,255, FMPI2C_RELOAD_MODE, FMPI2C_GENERATE_START_READ);
  1482. Sizetmp = 255;
  1483. }
  1484. else
  1485. {
  1486. FMPI2C_TransferConfig(hfmpi2c,DevAddress,Size, FMPI2C_AUTOEND_MODE, FMPI2C_GENERATE_START_READ);
  1487. Sizetmp = Size;
  1488. }
  1489. do
  1490. {
  1491. /* Wait until RXNE flag is set */
  1492. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_RXNE, RESET, Timeout) != HAL_OK)
  1493. {
  1494. return HAL_TIMEOUT;
  1495. }
  1496. /* Read data from RXDR */
  1497. (*pData++) = hfmpi2c->Instance->RXDR;
  1498. /* Decrement the Size counter */
  1499. Sizetmp--;
  1500. Size--;
  1501. if((Sizetmp == 0)&&(Size!=0))
  1502. {
  1503. /* Wait until TCR flag is set */
  1504. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_TCR, RESET, Timeout) != HAL_OK)
  1505. {
  1506. return HAL_TIMEOUT;
  1507. }
  1508. if(Size > 255)
  1509. {
  1510. FMPI2C_TransferConfig(hfmpi2c,DevAddress,255, FMPI2C_RELOAD_MODE, FMPI2C_NO_STARTSTOP);
  1511. Sizetmp = 255;
  1512. }
  1513. else
  1514. {
  1515. FMPI2C_TransferConfig(hfmpi2c,DevAddress,Size, FMPI2C_AUTOEND_MODE, FMPI2C_NO_STARTSTOP);
  1516. Sizetmp = Size;
  1517. }
  1518. }
  1519. }while(Size > 0);
  1520. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  1521. /* Wait until STOPF flag is reset */
  1522. if(FMPI2C_WaitOnSTOPFlagUntilTimeout(hfmpi2c, FMPI2C_TIMEOUT_STOPF) != HAL_OK)
  1523. {
  1524. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  1525. {
  1526. return HAL_ERROR;
  1527. }
  1528. else
  1529. {
  1530. return HAL_TIMEOUT;
  1531. }
  1532. }
  1533. /* Clear STOP Flag */
  1534. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  1535. /* Clear Configuration Register 2 */
  1536. __HAL_FMPI2C_RESET_CR2(hfmpi2c);
  1537. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  1538. /* Process Unlocked */
  1539. __HAL_UNLOCK(hfmpi2c);
  1540. return HAL_OK;
  1541. }
  1542. else
  1543. {
  1544. return HAL_BUSY;
  1545. }
  1546. }
  1547. /**
  1548. * @brief Write an amount of data in no-blocking mode with Interrupt to a specific memory address
  1549. * @param hfmpi2c : Pointer to a FMPI2C_HandleTypeDef structure that contains
  1550. * the configuration information for the specified FMPI2C.
  1551. * @param DevAddress: Target device address
  1552. * @param MemAddress: Internal memory address
  1553. * @param MemAddSize: Size of internal memory address
  1554. * @param pData: Pointer to data buffer
  1555. * @param Size: Amount of data to be sent
  1556. * @retval HAL status
  1557. */
  1558. 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)
  1559. {
  1560. /* Check the parameters */
  1561. assert_param(IS_FMPI2C_MEMADD_SIZE(MemAddSize));
  1562. if(hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1563. {
  1564. if((pData == NULL) || (Size == 0))
  1565. {
  1566. return HAL_ERROR;
  1567. }
  1568. if(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_BUSY) == SET)
  1569. {
  1570. return HAL_BUSY;
  1571. }
  1572. /* Process Locked */
  1573. __HAL_LOCK(hfmpi2c);
  1574. hfmpi2c->State = HAL_FMPI2C_STATE_MEM_BUSY_TX;
  1575. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1576. hfmpi2c->pBuffPtr = pData;
  1577. hfmpi2c->XferCount = Size;
  1578. if(Size > 255)
  1579. {
  1580. hfmpi2c->XferSize = 255;
  1581. }
  1582. else
  1583. {
  1584. hfmpi2c->XferSize = Size;
  1585. }
  1586. /* Send Slave Address and Memory Address */
  1587. if(FMPI2C_RequestMemoryWrite(hfmpi2c, DevAddress, MemAddress, MemAddSize, FMPI2C_TIMEOUT_FLAG) != HAL_OK)
  1588. {
  1589. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  1590. {
  1591. /* Process Unlocked */
  1592. __HAL_UNLOCK(hfmpi2c);
  1593. return HAL_ERROR;
  1594. }
  1595. else
  1596. {
  1597. /* Process Unlocked */
  1598. __HAL_UNLOCK(hfmpi2c);
  1599. return HAL_TIMEOUT;
  1600. }
  1601. }
  1602. /* Set NBYTES to write and reload if size > 255 */
  1603. /* Size > 255, need to set RELOAD bit */
  1604. if( (hfmpi2c->XferSize == 255) && (hfmpi2c->XferSize < hfmpi2c->XferCount) )
  1605. {
  1606. FMPI2C_TransferConfig(hfmpi2c,DevAddress,hfmpi2c->XferSize, FMPI2C_RELOAD_MODE, FMPI2C_NO_STARTSTOP);
  1607. }
  1608. else
  1609. {
  1610. FMPI2C_TransferConfig(hfmpi2c,DevAddress,hfmpi2c->XferSize, FMPI2C_AUTOEND_MODE, FMPI2C_NO_STARTSTOP);
  1611. }
  1612. /* Process Unlocked */
  1613. __HAL_UNLOCK(hfmpi2c);
  1614. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  1615. to avoid the risk of FMPI2C interrupt handle execution before current
  1616. process unlock */
  1617. /* Enable ERR, TC, STOP, NACK, TXI interrupt */
  1618. /* possible to enable all of these */
  1619. /* FMPI2C_IT_ERRI | FMPI2C_IT_TCI| FMPI2C_IT_STOPI| FMPI2C_IT_NACKI | FMPI2C_IT_ADDRI | FMPI2C_IT_RXI | FMPI2C_IT_TXI */
  1620. __HAL_FMPI2C_ENABLE_IT(hfmpi2c,FMPI2C_IT_ERRI | FMPI2C_IT_TCI| FMPI2C_IT_STOPI| FMPI2C_IT_NACKI | FMPI2C_IT_TXI );
  1621. return HAL_OK;
  1622. }
  1623. else
  1624. {
  1625. return HAL_BUSY;
  1626. }
  1627. }
  1628. /**
  1629. * @brief Read an amount of data in no-blocking mode with Interrupt from a specific memory address
  1630. * @param hfmpi2c : Pointer to a FMPI2C_HandleTypeDef structure that contains
  1631. * the configuration information for the specified FMPI2C.
  1632. * @param DevAddress: Target device address
  1633. * @param MemAddress: Internal memory address
  1634. * @param MemAddSize: Size of internal memory address
  1635. * @param pData: Pointer to data buffer
  1636. * @param Size: Amount of data to be sent
  1637. * @retval HAL status
  1638. */
  1639. 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)
  1640. {
  1641. /* Check the parameters */
  1642. assert_param(IS_FMPI2C_MEMADD_SIZE(MemAddSize));
  1643. if(hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1644. {
  1645. if((pData == NULL) || (Size == 0))
  1646. {
  1647. return HAL_ERROR;
  1648. }
  1649. if(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_BUSY) == SET)
  1650. {
  1651. return HAL_BUSY;
  1652. }
  1653. /* Process Locked */
  1654. __HAL_LOCK(hfmpi2c);
  1655. hfmpi2c->State = HAL_FMPI2C_STATE_MEM_BUSY_RX;
  1656. hfmpi2c->pBuffPtr = pData;
  1657. hfmpi2c->XferCount = Size;
  1658. if(Size > 255)
  1659. {
  1660. hfmpi2c->XferSize = 255;
  1661. }
  1662. else
  1663. {
  1664. hfmpi2c->XferSize = Size;
  1665. }
  1666. /* Send Slave Address and Memory Address */
  1667. if(FMPI2C_RequestMemoryRead(hfmpi2c, DevAddress, MemAddress, MemAddSize, FMPI2C_TIMEOUT_FLAG) != HAL_OK)
  1668. {
  1669. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  1670. {
  1671. /* Process Unlocked */
  1672. __HAL_UNLOCK(hfmpi2c);
  1673. return HAL_ERROR;
  1674. }
  1675. else
  1676. {
  1677. /* Process Unlocked */
  1678. __HAL_UNLOCK(hfmpi2c);
  1679. return HAL_TIMEOUT;
  1680. }
  1681. }
  1682. /* Set NBYTES to write and reload if size > 255 and generate RESTART */
  1683. /* Size > 255, need to set RELOAD bit */
  1684. if( (hfmpi2c->XferSize == 255) && (hfmpi2c->XferSize < hfmpi2c->XferCount) )
  1685. {
  1686. FMPI2C_TransferConfig(hfmpi2c,DevAddress,hfmpi2c->XferSize, FMPI2C_RELOAD_MODE, FMPI2C_GENERATE_START_READ);
  1687. }
  1688. else
  1689. {
  1690. FMPI2C_TransferConfig(hfmpi2c,DevAddress,hfmpi2c->XferSize, FMPI2C_AUTOEND_MODE, FMPI2C_GENERATE_START_READ);
  1691. }
  1692. /* Process Unlocked */
  1693. __HAL_UNLOCK(hfmpi2c);
  1694. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  1695. to avoid the risk of FMPI2C interrupt handle execution before current
  1696. process unlock */
  1697. /* Enable ERR, TC, STOP, NACK, RXI interrupt */
  1698. /* possible to enable all of these */
  1699. /* FMPI2C_IT_ERRI | FMPI2C_IT_TCI| FMPI2C_IT_STOPI| FMPI2C_IT_NACKI | FMPI2C_IT_ADDRI | FMPI2C_IT_RXI | FMPI2C_IT_TXI */
  1700. __HAL_FMPI2C_ENABLE_IT(hfmpi2c, FMPI2C_IT_ERRI | FMPI2C_IT_TCI| FMPI2C_IT_STOPI| FMPI2C_IT_NACKI | FMPI2C_IT_RXI );
  1701. return HAL_OK;
  1702. }
  1703. else
  1704. {
  1705. return HAL_BUSY;
  1706. }
  1707. }
  1708. /**
  1709. * @brief Write an amount of data in no-blocking mode with DMA to a specific memory address
  1710. * @param hfmpi2c : Pointer to a FMPI2C_HandleTypeDef structure that contains
  1711. * the configuration information for the specified FMPI2C.
  1712. * @param DevAddress: Target device address
  1713. * @param MemAddress: Internal memory address
  1714. * @param MemAddSize: Size of internal memory address
  1715. * @param pData: Pointer to data buffer
  1716. * @param Size: Amount of data to be sent
  1717. * @retval HAL status
  1718. */
  1719. 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)
  1720. {
  1721. /* Check the parameters */
  1722. assert_param(IS_FMPI2C_MEMADD_SIZE(MemAddSize));
  1723. if(hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1724. {
  1725. if((pData == NULL) || (Size == 0))
  1726. {
  1727. return HAL_ERROR;
  1728. }
  1729. if(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_BUSY) == SET)
  1730. {
  1731. return HAL_BUSY;
  1732. }
  1733. /* Process Locked */
  1734. __HAL_LOCK(hfmpi2c);
  1735. hfmpi2c->State = HAL_FMPI2C_STATE_MEM_BUSY_TX;
  1736. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1737. hfmpi2c->pBuffPtr = pData;
  1738. hfmpi2c->XferCount = Size;
  1739. if(Size > 255)
  1740. {
  1741. hfmpi2c->XferSize = 255;
  1742. }
  1743. else
  1744. {
  1745. hfmpi2c->XferSize = Size;
  1746. }
  1747. /* Set the FMPI2C DMA transfer complete callback */
  1748. hfmpi2c->hdmatx->XferCpltCallback = FMPI2C_DMAMemTransmitCplt;
  1749. /* Set the DMA error callback */
  1750. hfmpi2c->hdmatx->XferErrorCallback = FMPI2C_DMAError;
  1751. /* Enable the DMA channel */
  1752. HAL_DMA_Start_IT(hfmpi2c->hdmatx, (uint32_t)pData, (uint32_t)&hfmpi2c->Instance->TXDR, hfmpi2c->XferSize);
  1753. /* Send Slave Address and Memory Address */
  1754. if(FMPI2C_RequestMemoryWrite(hfmpi2c, DevAddress, MemAddress, MemAddSize, FMPI2C_TIMEOUT_FLAG) != HAL_OK)
  1755. {
  1756. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  1757. {
  1758. /* Process Unlocked */
  1759. __HAL_UNLOCK(hfmpi2c);
  1760. return HAL_ERROR;
  1761. }
  1762. else
  1763. {
  1764. /* Process Unlocked */
  1765. __HAL_UNLOCK(hfmpi2c);
  1766. return HAL_TIMEOUT;
  1767. }
  1768. }
  1769. /* Send Slave Address */
  1770. /* Set NBYTES to write and reload if size > 255 */
  1771. if( (hfmpi2c->XferSize == 255) && (hfmpi2c->XferSize < hfmpi2c->XferCount) )
  1772. {
  1773. FMPI2C_TransferConfig(hfmpi2c,DevAddress,hfmpi2c->XferSize, FMPI2C_RELOAD_MODE, FMPI2C_NO_STARTSTOP);
  1774. }
  1775. else
  1776. {
  1777. FMPI2C_TransferConfig(hfmpi2c,DevAddress,hfmpi2c->XferSize, FMPI2C_AUTOEND_MODE, FMPI2C_NO_STARTSTOP);
  1778. }
  1779. /* Wait until TXIS flag is set */
  1780. if(FMPI2C_WaitOnTXISFlagUntilTimeout(hfmpi2c, FMPI2C_TIMEOUT_TXIS) != HAL_OK)
  1781. {
  1782. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  1783. {
  1784. return HAL_ERROR;
  1785. }
  1786. else
  1787. {
  1788. return HAL_TIMEOUT;
  1789. }
  1790. }
  1791. /* Enable DMA Request */
  1792. hfmpi2c->Instance->CR1 |= FMPI2C_CR1_TXDMAEN;
  1793. /* Process Unlocked */
  1794. __HAL_UNLOCK(hfmpi2c);
  1795. return HAL_OK;
  1796. }
  1797. else
  1798. {
  1799. return HAL_BUSY;
  1800. }
  1801. }
  1802. /**
  1803. * @brief Reads an amount of data in no-blocking mode with DMA from a specific memory address.
  1804. * @param hfmpi2c : Pointer to a FMPI2C_HandleTypeDef structure that contains
  1805. * the configuration information for the specified FMPI2C.
  1806. * @param DevAddress: Target device address
  1807. * @param MemAddress: Internal memory address
  1808. * @param MemAddSize: Size of internal memory address
  1809. * @param pData: Pointer to data buffer
  1810. * @param Size: Amount of data to be read
  1811. * @retval HAL status
  1812. */
  1813. 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)
  1814. {
  1815. /* Check the parameters */
  1816. assert_param(IS_FMPI2C_MEMADD_SIZE(MemAddSize));
  1817. if(hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1818. {
  1819. if((pData == NULL) || (Size == 0))
  1820. {
  1821. return HAL_ERROR;
  1822. }
  1823. if(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_BUSY) == SET)
  1824. {
  1825. return HAL_BUSY;
  1826. }
  1827. /* Process Locked */
  1828. __HAL_LOCK(hfmpi2c);
  1829. hfmpi2c->State = HAL_FMPI2C_STATE_MEM_BUSY_RX;
  1830. hfmpi2c->pBuffPtr = pData;
  1831. hfmpi2c->XferCount = Size;
  1832. if(Size > 255)
  1833. {
  1834. hfmpi2c->XferSize = 255;
  1835. }
  1836. else
  1837. {
  1838. hfmpi2c->XferSize = Size;
  1839. }
  1840. /* Set the FMPI2C DMA transfer complete callback */
  1841. hfmpi2c->hdmarx->XferCpltCallback = FMPI2C_DMAMemReceiveCplt;
  1842. /* Set the DMA error callback */
  1843. hfmpi2c->hdmarx->XferErrorCallback = FMPI2C_DMAError;
  1844. /* Enable the DMA channel */
  1845. HAL_DMA_Start_IT(hfmpi2c->hdmarx, (uint32_t)&hfmpi2c->Instance->RXDR, (uint32_t)pData, hfmpi2c->XferSize);
  1846. /* Send Slave Address and Memory Address */
  1847. if(FMPI2C_RequestMemoryRead(hfmpi2c, DevAddress, MemAddress, MemAddSize, FMPI2C_TIMEOUT_FLAG) != HAL_OK)
  1848. {
  1849. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  1850. {
  1851. /* Process Unlocked */
  1852. __HAL_UNLOCK(hfmpi2c);
  1853. return HAL_ERROR;
  1854. }
  1855. else
  1856. {
  1857. /* Process Unlocked */
  1858. __HAL_UNLOCK(hfmpi2c);
  1859. return HAL_TIMEOUT;
  1860. }
  1861. }
  1862. /* Set NBYTES to write and reload if size > 255 and generate RESTART */
  1863. if( (hfmpi2c->XferSize == 255) && (hfmpi2c->XferSize < hfmpi2c->XferCount) )
  1864. {
  1865. FMPI2C_TransferConfig(hfmpi2c,DevAddress,hfmpi2c->XferSize, FMPI2C_RELOAD_MODE, FMPI2C_GENERATE_START_READ);
  1866. }
  1867. else
  1868. {
  1869. FMPI2C_TransferConfig(hfmpi2c,DevAddress,hfmpi2c->XferSize, FMPI2C_AUTOEND_MODE, FMPI2C_GENERATE_START_READ);
  1870. }
  1871. /* Wait until RXNE flag is set */
  1872. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_RXNE, RESET, FMPI2C_TIMEOUT_RXNE) != HAL_OK)
  1873. {
  1874. return HAL_TIMEOUT;
  1875. }
  1876. /* Enable DMA Request */
  1877. hfmpi2c->Instance->CR1 |= FMPI2C_CR1_RXDMAEN;
  1878. /* Process Unlocked */
  1879. __HAL_UNLOCK(hfmpi2c);
  1880. return HAL_OK;
  1881. }
  1882. else
  1883. {
  1884. return HAL_BUSY;
  1885. }
  1886. }
  1887. /**
  1888. * @brief Checks if target device is ready for communication.
  1889. * @note This function is used with Memory devices
  1890. * @param hfmpi2c : Pointer to a FMPI2C_HandleTypeDef structure that contains
  1891. * the configuration information for the specified FMPI2C.
  1892. * @param DevAddress: Target device address
  1893. * @param Trials: Number of trials
  1894. * @param Timeout: Timeout duration
  1895. * @retval HAL status
  1896. */
  1897. HAL_StatusTypeDef HAL_FMPI2C_IsDeviceReady(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint32_t Trials, uint32_t Timeout)
  1898. {
  1899. uint32_t tickstart = 0;
  1900. __IO uint32_t FMPI2C_Trials = 0;
  1901. if(hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1902. {
  1903. if(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_BUSY) == SET)
  1904. {
  1905. return HAL_BUSY;
  1906. }
  1907. /* Process Locked */
  1908. __HAL_LOCK(hfmpi2c);
  1909. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY;
  1910. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1911. do
  1912. {
  1913. /* Generate Start */
  1914. hfmpi2c->Instance->CR2 = __HAL_FMPI2C_GENERATE_START(hfmpi2c->Init.AddressingMode,DevAddress);
  1915. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  1916. /* Wait until STOPF flag is set or a NACK flag is set*/
  1917. tickstart = HAL_GetTick();
  1918. 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))
  1919. {
  1920. if(Timeout != HAL_MAX_DELAY)
  1921. {
  1922. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  1923. {
  1924. /* Device is ready */
  1925. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  1926. /* Process Unlocked */
  1927. __HAL_UNLOCK(hfmpi2c);
  1928. return HAL_TIMEOUT;
  1929. }
  1930. }
  1931. }
  1932. /* Check if the NACKF flag has not been set */
  1933. if (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_AF) == RESET)
  1934. {
  1935. /* Wait until STOPF flag is reset */
  1936. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_STOPF, RESET, Timeout) != HAL_OK)
  1937. {
  1938. return HAL_TIMEOUT;
  1939. }
  1940. /* Clear STOP Flag */
  1941. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  1942. /* Device is ready */
  1943. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  1944. /* Process Unlocked */
  1945. __HAL_UNLOCK(hfmpi2c);
  1946. return HAL_OK;
  1947. }
  1948. else
  1949. {
  1950. /* Wait until STOPF flag is reset */
  1951. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_STOPF, RESET, Timeout) != HAL_OK)
  1952. {
  1953. return HAL_TIMEOUT;
  1954. }
  1955. /* Clear NACK Flag */
  1956. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_AF);
  1957. /* Clear STOP Flag, auto generated with autoend*/
  1958. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  1959. }
  1960. /* Check if the maximum allowed number of trials has been reached */
  1961. if (FMPI2C_Trials++ == Trials)
  1962. {
  1963. /* Generate Stop */
  1964. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_STOP;
  1965. /* Wait until STOPF flag is reset */
  1966. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_STOPF, RESET, Timeout) != HAL_OK)
  1967. {
  1968. return HAL_TIMEOUT;
  1969. }
  1970. /* Clear STOP Flag */
  1971. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  1972. }
  1973. }while(FMPI2C_Trials < Trials);
  1974. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  1975. /* Process Unlocked */
  1976. __HAL_UNLOCK(hfmpi2c);
  1977. return HAL_TIMEOUT;
  1978. }
  1979. else
  1980. {
  1981. return HAL_BUSY;
  1982. }
  1983. }
  1984. /**
  1985. * @brief This function handles FMPI2C event interrupt request.
  1986. * @param hfmpi2c : Pointer to a FMPI2C_HandleTypeDef structure that contains
  1987. * the configuration information for the specified FMPI2C.
  1988. * @retval None
  1989. */
  1990. void HAL_FMPI2C_EV_IRQHandler(FMPI2C_HandleTypeDef *hfmpi2c)
  1991. {
  1992. /* FMPI2C in mode Transmitter ---------------------------------------------------*/
  1993. if (((__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_TXIS) == SET) || (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_TCR) == SET) || (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_TC) == SET) || (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF) == SET) || (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_AF) == SET) || (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_ADDR) == SET)) && (__HAL_FMPI2C_GET_IT_SOURCE(hfmpi2c, (FMPI2C_IT_TCI | FMPI2C_IT_STOPI | FMPI2C_IT_NACKI | FMPI2C_IT_TXI | FMPI2C_IT_ADDRI)) == SET))
  1994. {
  1995. /* Slave mode selected */
  1996. if (hfmpi2c->State == HAL_FMPI2C_STATE_SLAVE_BUSY_TX)
  1997. {
  1998. FMPI2C_SlaveTransmit_ISR(hfmpi2c);
  1999. }
  2000. }
  2001. if (((__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_TXIS) == SET) || (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_TCR) == SET) || (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_TC) == SET) || (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF) == SET) || (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_AF) == SET)) && (__HAL_FMPI2C_GET_IT_SOURCE(hfmpi2c, (FMPI2C_IT_TCI | FMPI2C_IT_STOPI | FMPI2C_IT_NACKI | FMPI2C_IT_TXI)) == SET))
  2002. {
  2003. /* Master mode selected */
  2004. if ((hfmpi2c->State == HAL_FMPI2C_STATE_MASTER_BUSY_TX) || (hfmpi2c->State == HAL_FMPI2C_STATE_MEM_BUSY_TX))
  2005. {
  2006. FMPI2C_MasterTransmit_ISR(hfmpi2c);
  2007. }
  2008. }
  2009. /* FMPI2C in mode Receiver ----------------------------------------------------*/
  2010. if (((__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_RXNE) == SET) || (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_TCR) == SET) || (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_TC) == SET) || (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF) == SET) || (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_AF) == SET) || (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_ADDR) == SET)) && (__HAL_FMPI2C_GET_IT_SOURCE(hfmpi2c, (FMPI2C_IT_TCI| FMPI2C_IT_STOPI| FMPI2C_IT_NACKI | FMPI2C_IT_RXI | FMPI2C_IT_ADDRI)) == SET))
  2011. {
  2012. /* Slave mode selected */
  2013. if (hfmpi2c->State == HAL_FMPI2C_STATE_SLAVE_BUSY_RX)
  2014. {
  2015. FMPI2C_SlaveReceive_ISR(hfmpi2c);
  2016. }
  2017. }
  2018. if (((__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_RXNE) == SET) || (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_TCR) == SET) || (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_TC) == SET) || (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF) == SET) || (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_AF) == SET)) && (__HAL_FMPI2C_GET_IT_SOURCE(hfmpi2c, (FMPI2C_IT_TCI| FMPI2C_IT_STOPI| FMPI2C_IT_NACKI | FMPI2C_IT_RXI)) == SET))
  2019. {
  2020. /* Master mode selected */
  2021. if ((hfmpi2c->State == HAL_FMPI2C_STATE_MASTER_BUSY_RX) || (hfmpi2c->State == HAL_FMPI2C_STATE_MEM_BUSY_RX))
  2022. {
  2023. FMPI2C_MasterReceive_ISR(hfmpi2c);
  2024. }
  2025. }
  2026. }
  2027. /**
  2028. * @brief This function handles FMPI2C error interrupt request.
  2029. * @param hfmpi2c : Pointer to a FMPI2C_HandleTypeDef structure that contains
  2030. * the configuration information for the specified FMPI2C.
  2031. * @retval None
  2032. */
  2033. void HAL_FMPI2C_ER_IRQHandler(FMPI2C_HandleTypeDef *hfmpi2c)
  2034. {
  2035. /* FMPI2C Bus error interrupt occurred ------------------------------------*/
  2036. if((__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_BERR) == SET) && (__HAL_FMPI2C_GET_IT_SOURCE(hfmpi2c, FMPI2C_IT_ERRI) == SET))
  2037. {
  2038. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_BERR;
  2039. /* Clear BERR flag */
  2040. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_BERR);
  2041. }
  2042. /* FMPI2C Over-Run/Under-Run interrupt occurred ----------------------------------------*/
  2043. if((__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_OVR) == SET) && (__HAL_FMPI2C_GET_IT_SOURCE(hfmpi2c, FMPI2C_IT_ERRI) == SET))
  2044. {
  2045. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_OVR;
  2046. /* Clear OVR flag */
  2047. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_OVR);
  2048. }
  2049. /* FMPI2C Arbitration Loss error interrupt occurred -------------------------------------*/
  2050. if((__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_ARLO) == SET) && (__HAL_FMPI2C_GET_IT_SOURCE(hfmpi2c, FMPI2C_IT_ERRI) == SET))
  2051. {
  2052. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_ARLO;
  2053. /* Clear ARLO flag */
  2054. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_ARLO);
  2055. }
  2056. /* Call the Error Callback in case of Error detected */
  2057. if(hfmpi2c->ErrorCode != HAL_FMPI2C_ERROR_NONE)
  2058. {
  2059. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  2060. HAL_FMPI2C_ErrorCallback(hfmpi2c);
  2061. }
  2062. }
  2063. /**
  2064. * @brief Master Tx Transfer completed callbacks.
  2065. * @param hfmpi2c : Pointer to a FMPI2C_HandleTypeDef structure that contains
  2066. * the configuration information for the specified FMPI2C.
  2067. * @retval None
  2068. */
  2069. __weak void HAL_FMPI2C_MasterTxCpltCallback(FMPI2C_HandleTypeDef *hfmpi2c)
  2070. {
  2071. /* NOTE : This function Should not be modified, when the callback is needed,
  2072. the HAL_FMPI2C_TxCpltCallback could be implemented in the user file
  2073. */
  2074. }
  2075. /**
  2076. * @brief Master Rx Transfer completed callbacks.
  2077. * @param hfmpi2c : Pointer to a FMPI2C_HandleTypeDef structure that contains
  2078. * the configuration information for the specified FMPI2C.
  2079. * @retval None
  2080. */
  2081. __weak void HAL_FMPI2C_MasterRxCpltCallback(FMPI2C_HandleTypeDef *hfmpi2c)
  2082. {
  2083. /* NOTE : This function Should not be modified, when the callback is needed,
  2084. the HAL_FMPI2C_TxCpltCallback could be implemented in the user file
  2085. */
  2086. }
  2087. /** @brief Slave Tx Transfer completed callbacks.
  2088. * @param hfmpi2c : Pointer to a FMPI2C_HandleTypeDef structure that contains
  2089. * the configuration information for the specified FMPI2C.
  2090. * @retval None
  2091. */
  2092. __weak void HAL_FMPI2C_SlaveTxCpltCallback(FMPI2C_HandleTypeDef *hfmpi2c)
  2093. {
  2094. /* NOTE : This function Should not be modified, when the callback is needed,
  2095. the HAL_FMPI2C_TxCpltCallback could be implemented in the user file
  2096. */
  2097. }
  2098. /**
  2099. * @brief Slave Rx Transfer completed callbacks.
  2100. * @param hfmpi2c : Pointer to a FMPI2C_HandleTypeDef structure that contains
  2101. * the configuration information for the specified FMPI2C.
  2102. * @retval None
  2103. */
  2104. __weak void HAL_FMPI2C_SlaveRxCpltCallback(FMPI2C_HandleTypeDef *hfmpi2c)
  2105. {
  2106. /* NOTE : This function Should not be modified, when the callback is needed,
  2107. the HAL_FMPI2C_TxCpltCallback could be implemented in the user file
  2108. */
  2109. }
  2110. /**
  2111. * @brief Memory Tx Transfer completed callbacks.
  2112. * @param hfmpi2c : Pointer to a FMPI2C_HandleTypeDef structure that contains
  2113. * the configuration information for the specified FMPI2C.
  2114. * @retval None
  2115. */
  2116. __weak void HAL_FMPI2C_MemTxCpltCallback(FMPI2C_HandleTypeDef *hfmpi2c)
  2117. {
  2118. /* NOTE : This function Should not be modified, when the callback is needed,
  2119. the HAL_FMPI2C_TxCpltCallback could be implemented in the user file
  2120. */
  2121. }
  2122. /**
  2123. * @brief Memory Rx Transfer completed callbacks.
  2124. * @param hfmpi2c : Pointer to a FMPI2C_HandleTypeDef structure that contains
  2125. * the configuration information for the specified FMPI2C.
  2126. * @retval None
  2127. */
  2128. __weak void HAL_FMPI2C_MemRxCpltCallback(FMPI2C_HandleTypeDef *hfmpi2c)
  2129. {
  2130. /* NOTE : This function Should not be modified, when the callback is needed,
  2131. the HAL_FMPI2C_TxCpltCallback could be implemented in the user file
  2132. */
  2133. }
  2134. /**
  2135. * @brief FMPI2C error callbacks.
  2136. * @param hfmpi2c : Pointer to a FMPI2C_HandleTypeDef structure that contains
  2137. * the configuration information for the specified FMPI2C.
  2138. * @retval None
  2139. */
  2140. __weak void HAL_FMPI2C_ErrorCallback(FMPI2C_HandleTypeDef *hfmpi2c)
  2141. {
  2142. /* NOTE : This function Should not be modified, when the callback is needed,
  2143. the HAL_FMPI2C_ErrorCallback could be implemented in the user file
  2144. */
  2145. }
  2146. /**
  2147. * @}
  2148. */
  2149. /** @defgroup FMPI2C_Exported_Functions_Group3 Peripheral State and Errors functions
  2150. * @brief Peripheral State and Errors functions
  2151. *
  2152. @verbatim
  2153. ===============================================================================
  2154. ##### Peripheral State and Errors functions #####
  2155. ===============================================================================
  2156. [..]
  2157. This subsection permit to get in run-time the status of the peripheral
  2158. and the data flow.
  2159. @endverbatim
  2160. * @{
  2161. */
  2162. /**
  2163. * @brief Returns the FMPI2C state.
  2164. * @param hfmpi2c : FMPI2C handle
  2165. * @retval HAL state
  2166. */
  2167. HAL_FMPI2C_StateTypeDef HAL_FMPI2C_GetState(FMPI2C_HandleTypeDef *hfmpi2c)
  2168. {
  2169. return hfmpi2c->State;
  2170. }
  2171. /**
  2172. * @brief Return the FMPI2C error code
  2173. * @param hfmpi2c : pointer to a FMPI2C_HandleTypeDef structure that contains
  2174. * the configuration information for the specified FMPI2C.
  2175. * @retval FMPI2C Error Code
  2176. */
  2177. uint32_t HAL_FMPI2C_GetError(FMPI2C_HandleTypeDef *hfmpi2c)
  2178. {
  2179. return hfmpi2c->ErrorCode;
  2180. }
  2181. /**
  2182. * @}
  2183. */
  2184. /**
  2185. * @brief Handle Interrupt Flags Master Transmit Mode
  2186. * @param hfmpi2c : Pointer to a FMPI2C_HandleTypeDef structure that contains
  2187. * the configuration information for the specified FMPI2C.
  2188. * @retval HAL status
  2189. */
  2190. static HAL_StatusTypeDef FMPI2C_MasterTransmit_ISR(FMPI2C_HandleTypeDef *hfmpi2c)
  2191. {
  2192. uint16_t DevAddress;
  2193. /* Process Locked */
  2194. __HAL_LOCK(hfmpi2c);
  2195. if(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_TXIS) == SET)
  2196. {
  2197. /* Write data to TXDR */
  2198. hfmpi2c->Instance->TXDR = (*hfmpi2c->pBuffPtr++);
  2199. hfmpi2c->XferSize--;
  2200. hfmpi2c->XferCount--;
  2201. }
  2202. else if(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_TCR) == SET)
  2203. {
  2204. if((hfmpi2c->XferSize == 0)&&(hfmpi2c->XferCount!=0))
  2205. {
  2206. DevAddress = (hfmpi2c->Instance->CR2 & FMPI2C_CR2_SADD);
  2207. if(hfmpi2c->XferCount > 255)
  2208. {
  2209. FMPI2C_TransferConfig(hfmpi2c,DevAddress,255, FMPI2C_RELOAD_MODE, FMPI2C_NO_STARTSTOP);
  2210. hfmpi2c->XferSize = 255;
  2211. }
  2212. else
  2213. {
  2214. FMPI2C_TransferConfig(hfmpi2c,DevAddress,hfmpi2c->XferCount, FMPI2C_AUTOEND_MODE, FMPI2C_NO_STARTSTOP);
  2215. hfmpi2c->XferSize = hfmpi2c->XferCount;
  2216. }
  2217. }
  2218. else
  2219. {
  2220. /* Process Unlocked */
  2221. __HAL_UNLOCK(hfmpi2c);
  2222. /* Wrong size Status regarding TCR flag event */
  2223. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_SIZE;
  2224. HAL_FMPI2C_ErrorCallback(hfmpi2c);
  2225. }
  2226. }
  2227. else if(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_TC) == SET)
  2228. {
  2229. if(hfmpi2c->XferCount == 0)
  2230. {
  2231. /* Generate Stop */
  2232. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_STOP;
  2233. }
  2234. else
  2235. {
  2236. /* Process Unlocked */
  2237. __HAL_UNLOCK(hfmpi2c);
  2238. /* Wrong size Status regarding TCR flag event */
  2239. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_SIZE;
  2240. HAL_FMPI2C_ErrorCallback(hfmpi2c);
  2241. }
  2242. }
  2243. else if(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF) == SET)
  2244. {
  2245. /* Disable ERR, TC, STOP, NACK, TXI interrupt */
  2246. __HAL_FMPI2C_DISABLE_IT(hfmpi2c,FMPI2C_IT_ERRI | FMPI2C_IT_TCI| FMPI2C_IT_STOPI| FMPI2C_IT_NACKI | FMPI2C_IT_TXI );
  2247. /* Clear STOP Flag */
  2248. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  2249. /* Clear Configuration Register 2 */
  2250. __HAL_FMPI2C_RESET_CR2(hfmpi2c);
  2251. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  2252. /* Process Unlocked */
  2253. __HAL_UNLOCK(hfmpi2c);
  2254. if(hfmpi2c->State == HAL_FMPI2C_STATE_MEM_BUSY_TX)
  2255. {
  2256. HAL_FMPI2C_MemTxCpltCallback(hfmpi2c);
  2257. }
  2258. else
  2259. {
  2260. HAL_FMPI2C_MasterTxCpltCallback(hfmpi2c);
  2261. }
  2262. }
  2263. else if(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_AF) == SET)
  2264. {
  2265. /* Clear NACK Flag */
  2266. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_AF);
  2267. /* Process Unlocked */
  2268. __HAL_UNLOCK(hfmpi2c);
  2269. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_AF;
  2270. HAL_FMPI2C_ErrorCallback(hfmpi2c);
  2271. }
  2272. /* Process Unlocked */
  2273. __HAL_UNLOCK(hfmpi2c);
  2274. return HAL_OK;
  2275. }
  2276. /**
  2277. * @brief Handle Interrupt Flags Master Receive Mode
  2278. * @param hfmpi2c : Pointer to a FMPI2C_HandleTypeDef structure that contains
  2279. * the configuration information for the specified FMPI2C.
  2280. * @retval HAL status
  2281. */
  2282. static HAL_StatusTypeDef FMPI2C_MasterReceive_ISR(FMPI2C_HandleTypeDef *hfmpi2c)
  2283. {
  2284. uint16_t DevAddress;
  2285. /* Process Locked */
  2286. __HAL_LOCK(hfmpi2c);
  2287. if(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_RXNE) == SET)
  2288. {
  2289. /* Read data from RXDR */
  2290. (*hfmpi2c->pBuffPtr++) = hfmpi2c->Instance->RXDR;
  2291. hfmpi2c->XferSize--;
  2292. hfmpi2c->XferCount--;
  2293. }
  2294. else if(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_TCR) == SET)
  2295. {
  2296. if((hfmpi2c->XferSize == 0)&&(hfmpi2c->XferCount!=0))
  2297. {
  2298. DevAddress = (hfmpi2c->Instance->CR2 & FMPI2C_CR2_SADD);
  2299. if(hfmpi2c->XferCount > 255)
  2300. {
  2301. FMPI2C_TransferConfig(hfmpi2c,DevAddress,255, FMPI2C_RELOAD_MODE, FMPI2C_NO_STARTSTOP);
  2302. hfmpi2c->XferSize = 255;
  2303. }
  2304. else
  2305. {
  2306. FMPI2C_TransferConfig(hfmpi2c,DevAddress,hfmpi2c->XferCount, FMPI2C_AUTOEND_MODE, FMPI2C_NO_STARTSTOP);
  2307. hfmpi2c->XferSize = hfmpi2c->XferCount;
  2308. }
  2309. }
  2310. else
  2311. {
  2312. /* Process Unlocked */
  2313. __HAL_UNLOCK(hfmpi2c);
  2314. /* Wrong size Status regarding TCR flag event */
  2315. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_SIZE;
  2316. HAL_FMPI2C_ErrorCallback(hfmpi2c);
  2317. }
  2318. }
  2319. else if(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_TC) == SET)
  2320. {
  2321. if(hfmpi2c->XferCount == 0)
  2322. {
  2323. /* Generate Stop */
  2324. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_STOP;
  2325. }
  2326. else
  2327. {
  2328. /* Process Unlocked */
  2329. __HAL_UNLOCK(hfmpi2c);
  2330. /* Wrong size Status regarding TCR flag event */
  2331. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_SIZE;
  2332. HAL_FMPI2C_ErrorCallback(hfmpi2c);
  2333. }
  2334. }
  2335. else if(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF) == SET)
  2336. {
  2337. /* Disable ERR, TC, STOP, NACK, TXI interrupt */
  2338. __HAL_FMPI2C_DISABLE_IT(hfmpi2c,FMPI2C_IT_ERRI | FMPI2C_IT_TCI| FMPI2C_IT_STOPI| FMPI2C_IT_NACKI | FMPI2C_IT_RXI );
  2339. /* Clear STOP Flag */
  2340. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  2341. /* Clear Configuration Register 2 */
  2342. __HAL_FMPI2C_RESET_CR2(hfmpi2c);
  2343. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  2344. /* Process Unlocked */
  2345. __HAL_UNLOCK(hfmpi2c);
  2346. if(hfmpi2c->State == HAL_FMPI2C_STATE_MEM_BUSY_RX)
  2347. {
  2348. HAL_FMPI2C_MemRxCpltCallback(hfmpi2c);
  2349. }
  2350. else
  2351. {
  2352. HAL_FMPI2C_MasterRxCpltCallback(hfmpi2c);
  2353. }
  2354. }
  2355. else if(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_AF) == SET)
  2356. {
  2357. /* Clear NACK Flag */
  2358. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_AF);
  2359. /* Process Unlocked */
  2360. __HAL_UNLOCK(hfmpi2c);
  2361. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_AF;
  2362. HAL_FMPI2C_ErrorCallback(hfmpi2c);
  2363. }
  2364. /* Process Unlocked */
  2365. __HAL_UNLOCK(hfmpi2c);
  2366. return HAL_OK;
  2367. }
  2368. /**
  2369. * @brief Handle Interrupt Flags Slave Transmit Mode
  2370. * @param hfmpi2c : Pointer to a FMPI2C_HandleTypeDef structure that contains
  2371. * the configuration information for the specified FMPI2C.
  2372. * @retval HAL status
  2373. */
  2374. static HAL_StatusTypeDef FMPI2C_SlaveTransmit_ISR(FMPI2C_HandleTypeDef *hfmpi2c)
  2375. {
  2376. /* Process locked */
  2377. __HAL_LOCK(hfmpi2c);
  2378. if(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_AF) != RESET)
  2379. {
  2380. /* Check that FMPI2C transfer finished */
  2381. /* if yes, normal usecase, a NACK is sent by the MASTER when Transfer is finished */
  2382. /* Mean XferCount == 0*/
  2383. /* So clear Flag NACKF only */
  2384. if(hfmpi2c->XferCount == 0)
  2385. {
  2386. /* Clear NACK Flag */
  2387. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_AF);
  2388. /* Process Unlocked */
  2389. __HAL_UNLOCK(hfmpi2c);
  2390. }
  2391. else
  2392. {
  2393. /* if no, error usecase, a Non-Acknowledge of last Data is generated by the MASTER*/
  2394. /* Clear NACK Flag */
  2395. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_AF);
  2396. /* Set ErrorCode corresponding to a Non-Acknowledge */
  2397. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_AF;
  2398. /* Process Unlocked */
  2399. __HAL_UNLOCK(hfmpi2c);
  2400. /* Call the Error callback to prevent upper layer */
  2401. HAL_FMPI2C_ErrorCallback(hfmpi2c);
  2402. }
  2403. }
  2404. else if(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_ADDR) == SET)
  2405. {
  2406. /* Clear ADDR flag */
  2407. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_ADDR);
  2408. }
  2409. /* Check first if STOPF is set */
  2410. /* to prevent a Write Data in TX buffer */
  2411. /* which is stuck in TXDR until next */
  2412. /* communication with Master */
  2413. else if(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF) == SET)
  2414. {
  2415. /* Disable ERRI, TCI, STOPI, NACKI, ADDRI, RXI, TXI interrupt */
  2416. __HAL_FMPI2C_DISABLE_IT(hfmpi2c,FMPI2C_IT_ERRI | FMPI2C_IT_TCI| FMPI2C_IT_STOPI| FMPI2C_IT_NACKI | FMPI2C_IT_ADDRI | FMPI2C_IT_RXI | FMPI2C_IT_TXI );
  2417. /* Disable Address Acknowledge */
  2418. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  2419. /* Clear STOP Flag */
  2420. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  2421. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  2422. /* Process Unlocked */
  2423. __HAL_UNLOCK(hfmpi2c);
  2424. HAL_FMPI2C_SlaveTxCpltCallback(hfmpi2c);
  2425. }
  2426. else if(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_TXIS) == SET)
  2427. {
  2428. /* Write data to TXDR only if XferCount not reach "0" */
  2429. /* A TXIS flag can be set, during STOP treatment */
  2430. if(hfmpi2c->XferCount > 0)
  2431. {
  2432. /* Write data to TXDR */
  2433. hfmpi2c->Instance->TXDR = (*hfmpi2c->pBuffPtr++);
  2434. hfmpi2c->XferCount--;
  2435. }
  2436. }
  2437. /* Process Unlocked */
  2438. __HAL_UNLOCK(hfmpi2c);
  2439. return HAL_OK;
  2440. }
  2441. /**
  2442. * @brief Handle Interrupt Flags Slave Receive Mode
  2443. * @param hfmpi2c : Pointer to a FMPI2C_HandleTypeDef structure that contains
  2444. * the configuration information for the specified FMPI2C.
  2445. * @retval HAL status
  2446. */
  2447. static HAL_StatusTypeDef FMPI2C_SlaveReceive_ISR(FMPI2C_HandleTypeDef *hfmpi2c)
  2448. {
  2449. /* Process Locked */
  2450. __HAL_LOCK(hfmpi2c);
  2451. if(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_AF) != RESET)
  2452. {
  2453. /* Clear NACK Flag */
  2454. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_AF);
  2455. /* Process Unlocked */
  2456. __HAL_UNLOCK(hfmpi2c);
  2457. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_AF;
  2458. HAL_FMPI2C_ErrorCallback(hfmpi2c);
  2459. }
  2460. else if(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_ADDR) == SET)
  2461. {
  2462. /* Clear ADDR flag */
  2463. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_ADDR);
  2464. }
  2465. else if(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_RXNE) == SET)
  2466. {
  2467. /* Read data from RXDR */
  2468. (*hfmpi2c->pBuffPtr++) = hfmpi2c->Instance->RXDR;
  2469. hfmpi2c->XferSize--;
  2470. hfmpi2c->XferCount--;
  2471. }
  2472. else if(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF) == SET)
  2473. {
  2474. /* Disable ERRI, TCI, STOPI, NACKI, ADDRI, RXI, TXI interrupt */
  2475. __HAL_FMPI2C_DISABLE_IT(hfmpi2c,FMPI2C_IT_ERRI | FMPI2C_IT_TCI| FMPI2C_IT_STOPI| FMPI2C_IT_NACKI | FMPI2C_IT_ADDRI | FMPI2C_IT_RXI | FMPI2C_IT_RXI );
  2476. /* Disable Address Acknowledge */
  2477. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  2478. /* Clear STOP Flag */
  2479. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  2480. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  2481. /* Process Unlocked */
  2482. __HAL_UNLOCK(hfmpi2c);
  2483. HAL_FMPI2C_SlaveRxCpltCallback(hfmpi2c);
  2484. }
  2485. /* Process Unlocked */
  2486. __HAL_UNLOCK(hfmpi2c);
  2487. return HAL_OK;
  2488. }
  2489. /**
  2490. * @brief Master sends target device address followed by internal memory address for write request.
  2491. * @param hfmpi2c : Pointer to a FMPI2C_HandleTypeDef structure that contains
  2492. * the configuration information for the specified FMPI2C.
  2493. * @param DevAddress: Target device address
  2494. * @param MemAddress: Internal memory address
  2495. * @param MemAddSize: Size of internal memory address
  2496. * @param Timeout: Timeout duration
  2497. * @retval HAL status
  2498. */
  2499. static HAL_StatusTypeDef FMPI2C_RequestMemoryWrite(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout)
  2500. {
  2501. FMPI2C_TransferConfig(hfmpi2c,DevAddress,MemAddSize, FMPI2C_RELOAD_MODE, FMPI2C_GENERATE_START_WRITE);
  2502. /* Wait until TXIS flag is set */
  2503. if(FMPI2C_WaitOnTXISFlagUntilTimeout(hfmpi2c, Timeout) != HAL_OK)
  2504. {
  2505. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  2506. {
  2507. return HAL_ERROR;
  2508. }
  2509. else
  2510. {
  2511. return HAL_TIMEOUT;
  2512. }
  2513. }
  2514. /* If Memory address size is 8Bit */
  2515. if(MemAddSize == FMPI2C_MEMADD_SIZE_8BIT)
  2516. {
  2517. /* Send Memory Address */
  2518. hfmpi2c->Instance->TXDR = __HAL_FMPI2C_MEM_ADD_LSB(MemAddress);
  2519. }
  2520. /* If Memory address size is 16Bit */
  2521. else
  2522. {
  2523. /* Send MSB of Memory Address */
  2524. hfmpi2c->Instance->TXDR = __HAL_FMPI2C_MEM_ADD_MSB(MemAddress);
  2525. /* Wait until TXIS flag is set */
  2526. if(FMPI2C_WaitOnTXISFlagUntilTimeout(hfmpi2c, Timeout) != HAL_OK)
  2527. {
  2528. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  2529. {
  2530. return HAL_ERROR;
  2531. }
  2532. else
  2533. {
  2534. return HAL_TIMEOUT;
  2535. }
  2536. }
  2537. /* Send LSB of Memory Address */
  2538. hfmpi2c->Instance->TXDR = __HAL_FMPI2C_MEM_ADD_LSB(MemAddress);
  2539. }
  2540. /* Wait until TCR flag is set */
  2541. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_TCR, RESET, Timeout) != HAL_OK)
  2542. {
  2543. return HAL_TIMEOUT;
  2544. }
  2545. return HAL_OK;
  2546. }
  2547. /**
  2548. * @brief Master sends target device address followed by internal memory address for read request.
  2549. * @param hfmpi2c : Pointer to a FMPI2C_HandleTypeDef structure that contains
  2550. * the configuration information for the specified FMPI2C.
  2551. * @param DevAddress: Target device address
  2552. * @param MemAddress: Internal memory address
  2553. * @param MemAddSize: Size of internal memory address
  2554. * @param Timeout: Timeout duration
  2555. * @retval HAL status
  2556. */
  2557. static HAL_StatusTypeDef FMPI2C_RequestMemoryRead(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout)
  2558. {
  2559. FMPI2C_TransferConfig(hfmpi2c,DevAddress,MemAddSize, FMPI2C_SOFTEND_MODE, FMPI2C_GENERATE_START_WRITE);
  2560. /* Wait until TXIS flag is set */
  2561. if(FMPI2C_WaitOnTXISFlagUntilTimeout(hfmpi2c, Timeout) != HAL_OK)
  2562. {
  2563. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  2564. {
  2565. return HAL_ERROR;
  2566. }
  2567. else
  2568. {
  2569. return HAL_TIMEOUT;
  2570. }
  2571. }
  2572. /* If Memory address size is 8Bit */
  2573. if(MemAddSize == FMPI2C_MEMADD_SIZE_8BIT)
  2574. {
  2575. /* Send Memory Address */
  2576. hfmpi2c->Instance->TXDR = __HAL_FMPI2C_MEM_ADD_LSB(MemAddress);
  2577. }
  2578. /* If Mememory address size is 16Bit */
  2579. else
  2580. {
  2581. /* Send MSB of Memory Address */
  2582. hfmpi2c->Instance->TXDR = __HAL_FMPI2C_MEM_ADD_MSB(MemAddress);
  2583. /* Wait until TXIS flag is set */
  2584. if(FMPI2C_WaitOnTXISFlagUntilTimeout(hfmpi2c, Timeout) != HAL_OK)
  2585. {
  2586. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  2587. {
  2588. return HAL_ERROR;
  2589. }
  2590. else
  2591. {
  2592. return HAL_TIMEOUT;
  2593. }
  2594. }
  2595. /* Send LSB of Memory Address */
  2596. hfmpi2c->Instance->TXDR = __HAL_FMPI2C_MEM_ADD_LSB(MemAddress);
  2597. }
  2598. /* Wait until TC flag is set */
  2599. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_TC, RESET, Timeout) != HAL_OK)
  2600. {
  2601. return HAL_TIMEOUT;
  2602. }
  2603. return HAL_OK;
  2604. }
  2605. /**
  2606. * @brief DMA FMPI2C master transmit process complete callback.
  2607. * @param hdma: DMA handle
  2608. * @retval None
  2609. */
  2610. static void FMPI2C_DMAMasterTransmitCplt(DMA_HandleTypeDef *hdma)
  2611. {
  2612. uint16_t DevAddress;
  2613. FMPI2C_HandleTypeDef* hfmpi2c = (FMPI2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  2614. /* Check if last DMA request was done with RELOAD */
  2615. /* Set NBYTES to write and reload if size > 255 */
  2616. if( (hfmpi2c->XferSize == 255) && (hfmpi2c->XferSize < hfmpi2c->XferCount) )
  2617. {
  2618. /* Wait until TCR flag is set */
  2619. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_TCR, RESET, FMPI2C_TIMEOUT_TCR) != HAL_OK)
  2620. {
  2621. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_TIMEOUT;
  2622. }
  2623. /* Disable DMA Request */
  2624. hfmpi2c->Instance->CR1 &= ~FMPI2C_CR1_TXDMAEN;
  2625. /* Check if Errors has been detected during transfer */
  2626. if(hfmpi2c->ErrorCode != HAL_FMPI2C_ERROR_NONE)
  2627. {
  2628. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  2629. /* Wait until STOPF flag is reset */
  2630. if(FMPI2C_WaitOnSTOPFlagUntilTimeout(hfmpi2c, FMPI2C_TIMEOUT_STOPF) != HAL_OK)
  2631. {
  2632. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  2633. {
  2634. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_AF;
  2635. }
  2636. else
  2637. {
  2638. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_TIMEOUT;
  2639. }
  2640. }
  2641. /* Clear STOP Flag */
  2642. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  2643. /* Clear Configuration Register 2 */
  2644. __HAL_FMPI2C_RESET_CR2(hfmpi2c);
  2645. hfmpi2c->XferCount = 0;
  2646. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  2647. HAL_FMPI2C_ErrorCallback(hfmpi2c);
  2648. }
  2649. else
  2650. {
  2651. hfmpi2c->pBuffPtr += hfmpi2c->XferSize;
  2652. hfmpi2c->XferCount -= hfmpi2c->XferSize;
  2653. if(hfmpi2c->XferCount > 255)
  2654. {
  2655. hfmpi2c->XferSize = 255;
  2656. }
  2657. else
  2658. {
  2659. hfmpi2c->XferSize = hfmpi2c->XferCount;
  2660. }
  2661. DevAddress = (hfmpi2c->Instance->CR2 & FMPI2C_CR2_SADD);
  2662. /* Enable the DMA channel */
  2663. HAL_DMA_Start_IT(hfmpi2c->hdmatx, (uint32_t)hfmpi2c->pBuffPtr, (uint32_t)&hfmpi2c->Instance->TXDR, hfmpi2c->XferSize);
  2664. /* Send Slave Address */
  2665. /* Set NBYTES to write and reload if size > 255 */
  2666. if( (hfmpi2c->XferSize == 255) && (hfmpi2c->XferSize < hfmpi2c->XferCount) )
  2667. {
  2668. FMPI2C_TransferConfig(hfmpi2c,DevAddress,hfmpi2c->XferSize, FMPI2C_RELOAD_MODE, FMPI2C_NO_STARTSTOP);
  2669. }
  2670. else
  2671. {
  2672. FMPI2C_TransferConfig(hfmpi2c,DevAddress,hfmpi2c->XferSize, FMPI2C_AUTOEND_MODE, FMPI2C_NO_STARTSTOP);
  2673. }
  2674. /* Wait until TXIS flag is set */
  2675. if(FMPI2C_WaitOnTXISFlagUntilTimeout(hfmpi2c, FMPI2C_TIMEOUT_TXIS) != HAL_OK)
  2676. {
  2677. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  2678. /* Wait until STOPF flag is reset */
  2679. if(FMPI2C_WaitOnSTOPFlagUntilTimeout(hfmpi2c, FMPI2C_TIMEOUT_STOPF) != HAL_OK)
  2680. {
  2681. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  2682. {
  2683. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_AF;
  2684. }
  2685. else
  2686. {
  2687. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_TIMEOUT;
  2688. }
  2689. }
  2690. /* Clear STOP Flag */
  2691. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  2692. /* Clear Configuration Register 2 */
  2693. __HAL_FMPI2C_RESET_CR2(hfmpi2c);
  2694. hfmpi2c->XferCount = 0;
  2695. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  2696. HAL_FMPI2C_ErrorCallback(hfmpi2c);
  2697. }
  2698. else
  2699. {
  2700. /* Enable DMA Request */
  2701. hfmpi2c->Instance->CR1 |= FMPI2C_CR1_TXDMAEN;
  2702. }
  2703. }
  2704. }
  2705. else
  2706. {
  2707. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  2708. /* Wait until STOPF flag is reset */
  2709. if(FMPI2C_WaitOnSTOPFlagUntilTimeout(hfmpi2c, FMPI2C_TIMEOUT_STOPF) != HAL_OK)
  2710. {
  2711. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  2712. {
  2713. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_AF;
  2714. }
  2715. else
  2716. {
  2717. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_TIMEOUT;
  2718. }
  2719. }
  2720. /* Clear STOP Flag */
  2721. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  2722. /* Clear Configuration Register 2 */
  2723. __HAL_FMPI2C_RESET_CR2(hfmpi2c);
  2724. /* Disable DMA Request */
  2725. hfmpi2c->Instance->CR1 &= ~FMPI2C_CR1_TXDMAEN;
  2726. hfmpi2c->XferCount = 0;
  2727. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  2728. /* Check if Errors has been detected during transfer */
  2729. if(hfmpi2c->ErrorCode != HAL_FMPI2C_ERROR_NONE)
  2730. {
  2731. HAL_FMPI2C_ErrorCallback(hfmpi2c);
  2732. }
  2733. else
  2734. {
  2735. HAL_FMPI2C_MasterTxCpltCallback(hfmpi2c);
  2736. }
  2737. }
  2738. }
  2739. /**
  2740. * @brief DMA FMPI2C slave transmit process complete callback.
  2741. * @param hdma: DMA handle
  2742. * @retval None
  2743. */
  2744. static void FMPI2C_DMASlaveTransmitCplt(DMA_HandleTypeDef *hdma)
  2745. {
  2746. FMPI2C_HandleTypeDef* hfmpi2c = (FMPI2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  2747. /* Wait until STOP flag is set */
  2748. if(FMPI2C_WaitOnSTOPFlagUntilTimeout(hfmpi2c, FMPI2C_TIMEOUT_STOPF) != HAL_OK)
  2749. {
  2750. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  2751. {
  2752. /* Normal Use case, a AF is generated by master */
  2753. /* to inform slave the end of transfer */
  2754. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  2755. }
  2756. else
  2757. {
  2758. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_TIMEOUT;
  2759. }
  2760. }
  2761. /* Clear STOP flag */
  2762. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c,FMPI2C_FLAG_STOPF);
  2763. /* Wait until BUSY flag is reset */
  2764. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_BUSY, SET, FMPI2C_TIMEOUT_BUSY) != HAL_OK)
  2765. {
  2766. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_TIMEOUT;
  2767. }
  2768. /* Disable DMA Request */
  2769. hfmpi2c->Instance->CR1 &= ~FMPI2C_CR1_TXDMAEN;
  2770. hfmpi2c->XferCount = 0;
  2771. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  2772. /* Check if Errors has been detected during transfer */
  2773. if(hfmpi2c->ErrorCode != HAL_FMPI2C_ERROR_NONE)
  2774. {
  2775. HAL_FMPI2C_ErrorCallback(hfmpi2c);
  2776. }
  2777. else
  2778. {
  2779. HAL_FMPI2C_SlaveTxCpltCallback(hfmpi2c);
  2780. }
  2781. }
  2782. /**
  2783. * @brief DMA FMPI2C master receive process complete callback
  2784. * @param hdma: DMA handle
  2785. * @retval None
  2786. */
  2787. static void FMPI2C_DMAMasterReceiveCplt(DMA_HandleTypeDef *hdma)
  2788. {
  2789. FMPI2C_HandleTypeDef* hfmpi2c = (FMPI2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  2790. uint16_t DevAddress;
  2791. /* Check if last DMA request was done with RELOAD */
  2792. /* Set NBYTES to write and reload if size > 255 */
  2793. if( (hfmpi2c->XferSize == 255) && (hfmpi2c->XferSize < hfmpi2c->XferCount) )
  2794. {
  2795. /* Wait until TCR flag is set */
  2796. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_TCR, RESET, FMPI2C_TIMEOUT_TCR) != HAL_OK)
  2797. {
  2798. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_TIMEOUT;
  2799. }
  2800. /* Disable DMA Request */
  2801. hfmpi2c->Instance->CR1 &= ~FMPI2C_CR1_RXDMAEN;
  2802. /* Check if Errors has been detected during transfer */
  2803. if(hfmpi2c->ErrorCode != HAL_FMPI2C_ERROR_NONE)
  2804. {
  2805. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  2806. /* Wait until STOPF flag is reset */
  2807. if(FMPI2C_WaitOnSTOPFlagUntilTimeout(hfmpi2c, FMPI2C_TIMEOUT_STOPF) != HAL_OK)
  2808. {
  2809. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  2810. {
  2811. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_AF;
  2812. }
  2813. else
  2814. {
  2815. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_TIMEOUT;
  2816. }
  2817. }
  2818. /* Clear STOP Flag */
  2819. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  2820. /* Clear Configuration Register 2 */
  2821. __HAL_FMPI2C_RESET_CR2(hfmpi2c);
  2822. hfmpi2c->XferCount = 0;
  2823. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  2824. HAL_FMPI2C_ErrorCallback(hfmpi2c);
  2825. }
  2826. else
  2827. {
  2828. hfmpi2c->pBuffPtr += hfmpi2c->XferSize;
  2829. hfmpi2c->XferCount -= hfmpi2c->XferSize;
  2830. if(hfmpi2c->XferCount > 255)
  2831. {
  2832. hfmpi2c->XferSize = 255;
  2833. }
  2834. else
  2835. {
  2836. hfmpi2c->XferSize = hfmpi2c->XferCount;
  2837. }
  2838. DevAddress = (hfmpi2c->Instance->CR2 & FMPI2C_CR2_SADD);
  2839. /* Enable the DMA channel */
  2840. HAL_DMA_Start_IT(hfmpi2c->hdmarx, (uint32_t)&hfmpi2c->Instance->RXDR, (uint32_t)hfmpi2c->pBuffPtr, hfmpi2c->XferSize);
  2841. /* Send Slave Address */
  2842. /* Set NBYTES to write and reload if size > 255 */
  2843. if( (hfmpi2c->XferSize == 255) && (hfmpi2c->XferSize < hfmpi2c->XferCount) )
  2844. {
  2845. FMPI2C_TransferConfig(hfmpi2c,DevAddress,hfmpi2c->XferSize, FMPI2C_RELOAD_MODE, FMPI2C_NO_STARTSTOP);
  2846. }
  2847. else
  2848. {
  2849. FMPI2C_TransferConfig(hfmpi2c,DevAddress,hfmpi2c->XferSize, FMPI2C_AUTOEND_MODE, FMPI2C_NO_STARTSTOP);
  2850. }
  2851. /* Wait until RXNE flag is set */
  2852. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_RXNE, RESET, FMPI2C_TIMEOUT_RXNE) != HAL_OK)
  2853. {
  2854. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_TIMEOUT;
  2855. }
  2856. /* Check if Errors has been detected during transfer */
  2857. if(hfmpi2c->ErrorCode != HAL_FMPI2C_ERROR_NONE)
  2858. {
  2859. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  2860. /* Wait until STOPF flag is reset */
  2861. if(FMPI2C_WaitOnSTOPFlagUntilTimeout(hfmpi2c, FMPI2C_TIMEOUT_STOPF) != HAL_OK)
  2862. {
  2863. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  2864. {
  2865. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_AF;
  2866. }
  2867. else
  2868. {
  2869. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_TIMEOUT;
  2870. }
  2871. }
  2872. /* Clear STOP Flag */
  2873. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  2874. /* Clear Configuration Register 2 */
  2875. __HAL_FMPI2C_RESET_CR2(hfmpi2c);
  2876. hfmpi2c->XferCount = 0;
  2877. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  2878. HAL_FMPI2C_ErrorCallback(hfmpi2c);
  2879. }
  2880. else
  2881. {
  2882. /* Enable DMA Request */
  2883. hfmpi2c->Instance->CR1 |= FMPI2C_CR1_RXDMAEN;
  2884. }
  2885. }
  2886. }
  2887. else
  2888. {
  2889. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  2890. /* Wait until STOPF flag is reset */
  2891. if(FMPI2C_WaitOnSTOPFlagUntilTimeout(hfmpi2c, FMPI2C_TIMEOUT_STOPF) != HAL_OK)
  2892. {
  2893. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  2894. {
  2895. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_AF;
  2896. }
  2897. else
  2898. {
  2899. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_TIMEOUT;
  2900. }
  2901. }
  2902. /* Clear STOP Flag */
  2903. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  2904. /* Clear Configuration Register 2 */
  2905. __HAL_FMPI2C_RESET_CR2(hfmpi2c);
  2906. /* Disable DMA Request */
  2907. hfmpi2c->Instance->CR1 &= ~FMPI2C_CR1_RXDMAEN;
  2908. hfmpi2c->XferCount = 0;
  2909. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  2910. /* Check if Errors has been detected during transfer */
  2911. if(hfmpi2c->ErrorCode != HAL_FMPI2C_ERROR_NONE)
  2912. {
  2913. HAL_FMPI2C_ErrorCallback(hfmpi2c);
  2914. }
  2915. else
  2916. {
  2917. HAL_FMPI2C_MasterRxCpltCallback(hfmpi2c);
  2918. }
  2919. }
  2920. }
  2921. /**
  2922. * @brief DMA FMPI2C slave receive process complete callback.
  2923. * @param hdma: DMA handle
  2924. * @retval None
  2925. */
  2926. static void FMPI2C_DMASlaveReceiveCplt(DMA_HandleTypeDef *hdma)
  2927. {
  2928. FMPI2C_HandleTypeDef* hfmpi2c = (FMPI2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  2929. /* Wait until STOPF flag is reset */
  2930. if(FMPI2C_WaitOnSTOPFlagUntilTimeout(hfmpi2c, FMPI2C_TIMEOUT_STOPF) != HAL_OK)
  2931. {
  2932. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  2933. {
  2934. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_AF;
  2935. }
  2936. else
  2937. {
  2938. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_TIMEOUT;
  2939. }
  2940. }
  2941. /* Clear STOPF flag */
  2942. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  2943. /* Wait until BUSY flag is reset */
  2944. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_BUSY, SET, FMPI2C_TIMEOUT_BUSY) != HAL_OK)
  2945. {
  2946. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_TIMEOUT;
  2947. }
  2948. /* Disable DMA Request */
  2949. hfmpi2c->Instance->CR1 &= ~FMPI2C_CR1_RXDMAEN;
  2950. /* Disable Address Acknowledge */
  2951. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  2952. hfmpi2c->XferCount = 0;
  2953. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  2954. /* Check if Errors has been detected during transfer */
  2955. if(hfmpi2c->ErrorCode != HAL_FMPI2C_ERROR_NONE)
  2956. {
  2957. HAL_FMPI2C_ErrorCallback(hfmpi2c);
  2958. }
  2959. else
  2960. {
  2961. HAL_FMPI2C_SlaveRxCpltCallback(hfmpi2c);
  2962. }
  2963. }
  2964. /**
  2965. * @brief DMA FMPI2C Memory Write process complete callback
  2966. * @param hdma : DMA handle
  2967. * @retval None
  2968. */
  2969. static void FMPI2C_DMAMemTransmitCplt(DMA_HandleTypeDef *hdma)
  2970. {
  2971. uint16_t DevAddress;
  2972. FMPI2C_HandleTypeDef* hfmpi2c = ( FMPI2C_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  2973. /* Check if last DMA request was done with RELOAD */
  2974. /* Set NBYTES to write and reload if size > 255 */
  2975. if( (hfmpi2c->XferSize == 255) && (hfmpi2c->XferSize < hfmpi2c->XferCount) )
  2976. {
  2977. /* Wait until TCR flag is set */
  2978. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_TCR, RESET, FMPI2C_TIMEOUT_TCR) != HAL_OK)
  2979. {
  2980. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_TIMEOUT;
  2981. }
  2982. /* Disable DMA Request */
  2983. hfmpi2c->Instance->CR1 &= ~FMPI2C_CR1_TXDMAEN;
  2984. /* Check if Errors has been detected during transfer */
  2985. if(hfmpi2c->ErrorCode != HAL_FMPI2C_ERROR_NONE)
  2986. {
  2987. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  2988. /* Wait until STOPF flag is reset */
  2989. if(FMPI2C_WaitOnSTOPFlagUntilTimeout(hfmpi2c, FMPI2C_TIMEOUT_STOPF) != HAL_OK)
  2990. {
  2991. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  2992. {
  2993. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_AF;
  2994. }
  2995. else
  2996. {
  2997. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_TIMEOUT;
  2998. }
  2999. }
  3000. /* Clear STOP Flag */
  3001. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  3002. /* Clear Configuration Register 2 */
  3003. __HAL_FMPI2C_RESET_CR2(hfmpi2c);
  3004. hfmpi2c->XferCount = 0;
  3005. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  3006. HAL_FMPI2C_ErrorCallback(hfmpi2c);
  3007. }
  3008. else
  3009. {
  3010. hfmpi2c->pBuffPtr += hfmpi2c->XferSize;
  3011. hfmpi2c->XferCount -= hfmpi2c->XferSize;
  3012. if(hfmpi2c->XferCount > 255)
  3013. {
  3014. hfmpi2c->XferSize = 255;
  3015. }
  3016. else
  3017. {
  3018. hfmpi2c->XferSize = hfmpi2c->XferCount;
  3019. }
  3020. DevAddress = (hfmpi2c->Instance->CR2 & FMPI2C_CR2_SADD);
  3021. /* Enable the DMA channel */
  3022. HAL_DMA_Start_IT(hfmpi2c->hdmatx, (uint32_t)hfmpi2c->pBuffPtr, (uint32_t)&hfmpi2c->Instance->TXDR, hfmpi2c->XferSize);
  3023. /* Send Slave Address */
  3024. /* Set NBYTES to write and reload if size > 255 */
  3025. if( (hfmpi2c->XferSize == 255) && (hfmpi2c->XferSize < hfmpi2c->XferCount) )
  3026. {
  3027. FMPI2C_TransferConfig(hfmpi2c,DevAddress,hfmpi2c->XferSize, FMPI2C_RELOAD_MODE, FMPI2C_NO_STARTSTOP);
  3028. }
  3029. else
  3030. {
  3031. FMPI2C_TransferConfig(hfmpi2c,DevAddress,hfmpi2c->XferSize, FMPI2C_AUTOEND_MODE, FMPI2C_NO_STARTSTOP);
  3032. }
  3033. /* Wait until TXIS flag is set */
  3034. if(FMPI2C_WaitOnTXISFlagUntilTimeout(hfmpi2c, FMPI2C_TIMEOUT_TXIS) != HAL_OK)
  3035. {
  3036. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  3037. /* Wait until STOPF flag is reset */
  3038. if(FMPI2C_WaitOnSTOPFlagUntilTimeout(hfmpi2c, FMPI2C_TIMEOUT_STOPF) != HAL_OK)
  3039. {
  3040. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  3041. {
  3042. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_AF;
  3043. }
  3044. else
  3045. {
  3046. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_TIMEOUT;
  3047. }
  3048. }
  3049. /* Clear STOP Flag */
  3050. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  3051. /* Clear Configuration Register 2 */
  3052. __HAL_FMPI2C_RESET_CR2(hfmpi2c);
  3053. hfmpi2c->XferCount = 0;
  3054. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  3055. HAL_FMPI2C_ErrorCallback(hfmpi2c);
  3056. }
  3057. else
  3058. {
  3059. /* Enable DMA Request */
  3060. hfmpi2c->Instance->CR1 |= FMPI2C_CR1_TXDMAEN;
  3061. }
  3062. }
  3063. }
  3064. else
  3065. {
  3066. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  3067. /* Wait until STOPF flag is reset */
  3068. if(FMPI2C_WaitOnSTOPFlagUntilTimeout(hfmpi2c, FMPI2C_TIMEOUT_STOPF) != HAL_OK)
  3069. {
  3070. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  3071. {
  3072. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_AF;
  3073. }
  3074. else
  3075. {
  3076. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_TIMEOUT;
  3077. }
  3078. }
  3079. /* Clear STOP Flag */
  3080. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  3081. /* Clear Configuration Register 2 */
  3082. __HAL_FMPI2C_RESET_CR2(hfmpi2c);
  3083. /* Disable DMA Request */
  3084. hfmpi2c->Instance->CR1 &= ~FMPI2C_CR1_TXDMAEN;
  3085. hfmpi2c->XferCount = 0;
  3086. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  3087. /* Check if Errors has been detected during transfer */
  3088. if(hfmpi2c->ErrorCode != HAL_FMPI2C_ERROR_NONE)
  3089. {
  3090. HAL_FMPI2C_ErrorCallback(hfmpi2c);
  3091. }
  3092. else
  3093. {
  3094. HAL_FMPI2C_MemTxCpltCallback(hfmpi2c);
  3095. }
  3096. }
  3097. }
  3098. /**
  3099. * @brief DMA FMPI2C Memory Read process complete callback
  3100. * @param hdma: DMA handle
  3101. * @retval None
  3102. */
  3103. static void FMPI2C_DMAMemReceiveCplt(DMA_HandleTypeDef *hdma)
  3104. {
  3105. FMPI2C_HandleTypeDef* hfmpi2c = ( FMPI2C_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  3106. uint16_t DevAddress;
  3107. /* Check if last DMA request was done with RELOAD */
  3108. /* Set NBYTES to write and reload if size > 255 */
  3109. if( (hfmpi2c->XferSize == 255) && (hfmpi2c->XferSize < hfmpi2c->XferCount) )
  3110. {
  3111. /* Wait until TCR flag is set */
  3112. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_TCR, RESET, FMPI2C_TIMEOUT_TCR) != HAL_OK)
  3113. {
  3114. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_TIMEOUT;
  3115. }
  3116. /* Disable DMA Request */
  3117. hfmpi2c->Instance->CR1 &= ~FMPI2C_CR1_RXDMAEN;
  3118. /* Check if Errors has been detected during transfer */
  3119. if(hfmpi2c->ErrorCode != HAL_FMPI2C_ERROR_NONE)
  3120. {
  3121. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  3122. /* Wait until STOPF flag is reset */
  3123. if(FMPI2C_WaitOnSTOPFlagUntilTimeout(hfmpi2c, FMPI2C_TIMEOUT_STOPF) != HAL_OK)
  3124. {
  3125. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  3126. {
  3127. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_AF;
  3128. }
  3129. else
  3130. {
  3131. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_TIMEOUT;
  3132. }
  3133. }
  3134. /* Clear STOP Flag */
  3135. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  3136. /* Clear Configuration Register 2 */
  3137. __HAL_FMPI2C_RESET_CR2(hfmpi2c);
  3138. hfmpi2c->XferCount = 0;
  3139. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  3140. HAL_FMPI2C_ErrorCallback(hfmpi2c);
  3141. }
  3142. else
  3143. {
  3144. hfmpi2c->pBuffPtr += hfmpi2c->XferSize;
  3145. hfmpi2c->XferCount -= hfmpi2c->XferSize;
  3146. if(hfmpi2c->XferCount > 255)
  3147. {
  3148. hfmpi2c->XferSize = 255;
  3149. }
  3150. else
  3151. {
  3152. hfmpi2c->XferSize = hfmpi2c->XferCount;
  3153. }
  3154. DevAddress = (hfmpi2c->Instance->CR2 & FMPI2C_CR2_SADD);
  3155. /* Enable the DMA channel */
  3156. HAL_DMA_Start_IT(hfmpi2c->hdmarx, (uint32_t)&hfmpi2c->Instance->RXDR, (uint32_t)hfmpi2c->pBuffPtr, hfmpi2c->XferSize);
  3157. /* Send Slave Address */
  3158. /* Set NBYTES to write and reload if size > 255 */
  3159. if( (hfmpi2c->XferSize == 255) && (hfmpi2c->XferSize < hfmpi2c->XferCount) )
  3160. {
  3161. FMPI2C_TransferConfig(hfmpi2c,DevAddress,hfmpi2c->XferSize, FMPI2C_RELOAD_MODE, FMPI2C_NO_STARTSTOP);
  3162. }
  3163. else
  3164. {
  3165. FMPI2C_TransferConfig(hfmpi2c,DevAddress,hfmpi2c->XferSize, FMPI2C_AUTOEND_MODE, FMPI2C_NO_STARTSTOP);
  3166. }
  3167. /* Wait until RXNE flag is set */
  3168. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_RXNE, RESET, FMPI2C_TIMEOUT_RXNE) != HAL_OK)
  3169. {
  3170. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_TIMEOUT;
  3171. }
  3172. /* Check if Errors has been detected during transfer */
  3173. if(hfmpi2c->ErrorCode != HAL_FMPI2C_ERROR_NONE)
  3174. {
  3175. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  3176. /* Wait until STOPF flag is reset */
  3177. if(FMPI2C_WaitOnSTOPFlagUntilTimeout(hfmpi2c, FMPI2C_TIMEOUT_STOPF) != HAL_OK)
  3178. {
  3179. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  3180. {
  3181. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_AF;
  3182. }
  3183. else
  3184. {
  3185. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_TIMEOUT;
  3186. }
  3187. }
  3188. /* Clear STOP Flag */
  3189. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  3190. /* Clear Configuration Register 2 */
  3191. __HAL_FMPI2C_RESET_CR2(hfmpi2c);
  3192. hfmpi2c->XferCount = 0;
  3193. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  3194. HAL_FMPI2C_ErrorCallback(hfmpi2c);
  3195. }
  3196. else
  3197. {
  3198. /* Enable DMA Request */
  3199. hfmpi2c->Instance->CR1 |= FMPI2C_CR1_RXDMAEN;
  3200. }
  3201. }
  3202. }
  3203. else
  3204. {
  3205. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  3206. /* Wait until STOPF flag is reset */
  3207. if(FMPI2C_WaitOnSTOPFlagUntilTimeout(hfmpi2c, FMPI2C_TIMEOUT_STOPF) != HAL_OK)
  3208. {
  3209. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  3210. {
  3211. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_AF;
  3212. }
  3213. else
  3214. {
  3215. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_TIMEOUT;
  3216. }
  3217. }
  3218. /* Clear STOP Flag */
  3219. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  3220. /* Clear Configuration Register 2 */
  3221. __HAL_FMPI2C_RESET_CR2(hfmpi2c);
  3222. /* Disable DMA Request */
  3223. hfmpi2c->Instance->CR1 &= ~FMPI2C_CR1_RXDMAEN;
  3224. hfmpi2c->XferCount = 0;
  3225. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  3226. /* Check if Errors has been detected during transfer */
  3227. if(hfmpi2c->ErrorCode != HAL_FMPI2C_ERROR_NONE)
  3228. {
  3229. HAL_FMPI2C_ErrorCallback(hfmpi2c);
  3230. }
  3231. else
  3232. {
  3233. HAL_FMPI2C_MemRxCpltCallback(hfmpi2c);
  3234. }
  3235. }
  3236. }
  3237. /**
  3238. * @brief DMA FMPI2C communication error callback.
  3239. * @param hdma : DMA handle
  3240. * @retval None
  3241. */
  3242. static void FMPI2C_DMAError(DMA_HandleTypeDef *hdma)
  3243. {
  3244. FMPI2C_HandleTypeDef* hfmpi2c = ( FMPI2C_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  3245. /* Disable Acknowledge */
  3246. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  3247. hfmpi2c->XferCount = 0;
  3248. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  3249. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_DMA;
  3250. HAL_FMPI2C_ErrorCallback(hfmpi2c);
  3251. }
  3252. /**
  3253. * @brief This function handles FMPI2C Communication Timeout.
  3254. * @param hfmpi2c : Pointer to a FMPI2C_HandleTypeDef structure that contains
  3255. * the configuration information for the specified FMPI2C.
  3256. * @param Flag: specifies the FMPI2C flag to check.
  3257. * @param Status: The new Flag status (SET or RESET).
  3258. * @param Timeout: Timeout duration
  3259. * @retval HAL status
  3260. */
  3261. static HAL_StatusTypeDef FMPI2C_WaitOnFlagUntilTimeout(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t Flag, FlagStatus Status, uint32_t Timeout)
  3262. {
  3263. uint32_t tickstart = HAL_GetTick();
  3264. /* Wait until flag is set */
  3265. if(Status == RESET)
  3266. {
  3267. while(__HAL_FMPI2C_GET_FLAG(hfmpi2c, Flag) == RESET)
  3268. {
  3269. /* Check for the Timeout */
  3270. if(Timeout != HAL_MAX_DELAY)
  3271. {
  3272. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  3273. {
  3274. hfmpi2c->State= HAL_FMPI2C_STATE_READY;
  3275. /* Process Unlocked */
  3276. __HAL_UNLOCK(hfmpi2c);
  3277. return HAL_TIMEOUT;
  3278. }
  3279. }
  3280. }
  3281. }
  3282. else
  3283. {
  3284. while(__HAL_FMPI2C_GET_FLAG(hfmpi2c, Flag) != RESET)
  3285. {
  3286. /* Check for the Timeout */
  3287. if(Timeout != HAL_MAX_DELAY)
  3288. {
  3289. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  3290. {
  3291. hfmpi2c->State= HAL_FMPI2C_STATE_READY;
  3292. /* Process Unlocked */
  3293. __HAL_UNLOCK(hfmpi2c);
  3294. return HAL_TIMEOUT;
  3295. }
  3296. }
  3297. }
  3298. }
  3299. return HAL_OK;
  3300. }
  3301. /**
  3302. * @brief This function handles FMPI2C Communication Timeout for specific usage of TXIS flag.
  3303. * @param hfmpi2c : Pointer to a FMPI2C_HandleTypeDef structure that contains
  3304. * the configuration information for the specified FMPI2C.
  3305. * @param Timeout: Timeout duration
  3306. * @retval HAL status
  3307. */
  3308. static HAL_StatusTypeDef FMPI2C_WaitOnTXISFlagUntilTimeout(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t Timeout)
  3309. {
  3310. uint32_t tickstart = HAL_GetTick();
  3311. while(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_TXIS) == RESET)
  3312. {
  3313. /* Check if a NACK is detected */
  3314. if(FMPI2C_IsAcknowledgeFailed(hfmpi2c, Timeout) != HAL_OK)
  3315. {
  3316. return HAL_ERROR;
  3317. }
  3318. /* Check for the Timeout */
  3319. if(Timeout != HAL_MAX_DELAY)
  3320. {
  3321. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  3322. {
  3323. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_TIMEOUT;
  3324. hfmpi2c->State= HAL_FMPI2C_STATE_READY;
  3325. /* Process Unlocked */
  3326. __HAL_UNLOCK(hfmpi2c);
  3327. return HAL_TIMEOUT;
  3328. }
  3329. }
  3330. }
  3331. return HAL_OK;
  3332. }
  3333. /**
  3334. * @brief This function handles FMPI2C Communication Timeout for specific usage of STOP flag.
  3335. * @param hfmpi2c : Pointer to a FMPI2C_HandleTypeDef structure that contains
  3336. * the configuration information for the specified FMPI2C.
  3337. * @param Timeout: Timeout duration
  3338. * @retval HAL status
  3339. */
  3340. static HAL_StatusTypeDef FMPI2C_WaitOnSTOPFlagUntilTimeout(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t Timeout)
  3341. {
  3342. uint32_t tickstart = 0x00;
  3343. tickstart = HAL_GetTick();
  3344. while(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF) == RESET)
  3345. {
  3346. /* Check if a NACK is detected */
  3347. if(FMPI2C_IsAcknowledgeFailed(hfmpi2c, Timeout) != HAL_OK)
  3348. {
  3349. return HAL_ERROR;
  3350. }
  3351. /* Check for the Timeout */
  3352. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  3353. {
  3354. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_TIMEOUT;
  3355. hfmpi2c->State= HAL_FMPI2C_STATE_READY;
  3356. /* Process Unlocked */
  3357. __HAL_UNLOCK(hfmpi2c);
  3358. return HAL_TIMEOUT;
  3359. }
  3360. }
  3361. return HAL_OK;
  3362. }
  3363. /**
  3364. * @brief This function handles FMPI2C Communication Timeout for specific usage of RXNE flag.
  3365. * @param hfmpi2c : Pointer to a FMPI2C_HandleTypeDef structure that contains
  3366. * the configuration information for the specified FMPI2C.
  3367. * @param Timeout: Timeout duration
  3368. * @retval HAL status
  3369. */
  3370. static HAL_StatusTypeDef FMPI2C_WaitOnRXNEFlagUntilTimeout(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t Timeout)
  3371. {
  3372. uint32_t tickstart = 0x00;
  3373. tickstart = HAL_GetTick();
  3374. while(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_RXNE) == RESET)
  3375. {
  3376. /* Check if a STOPF is detected */
  3377. if(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF) == SET)
  3378. {
  3379. /* Clear STOP Flag */
  3380. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  3381. /* Clear Configuration Register 2 */
  3382. __HAL_FMPI2C_RESET_CR2(hfmpi2c);
  3383. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  3384. hfmpi2c->State= HAL_FMPI2C_STATE_READY;
  3385. /* Process Unlocked */
  3386. __HAL_UNLOCK(hfmpi2c);
  3387. return HAL_ERROR;
  3388. }
  3389. /* Check for the Timeout */
  3390. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  3391. {
  3392. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_TIMEOUT;
  3393. hfmpi2c->State= HAL_FMPI2C_STATE_READY;
  3394. /* Process Unlocked */
  3395. __HAL_UNLOCK(hfmpi2c);
  3396. return HAL_TIMEOUT;
  3397. }
  3398. }
  3399. return HAL_OK;
  3400. }
  3401. /**
  3402. * @brief This function handles Acknowledge failed detection during an FMPI2C Communication.
  3403. * @param hfmpi2c : Pointer to a FMPI2C_HandleTypeDef structure that contains
  3404. * the configuration information for the specified FMPI2C.
  3405. * @param Timeout: Timeout duration
  3406. * @retval HAL status
  3407. */
  3408. static HAL_StatusTypeDef FMPI2C_IsAcknowledgeFailed(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t Timeout)
  3409. {
  3410. uint32_t tickstart = 0x00;
  3411. tickstart = HAL_GetTick();
  3412. if(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_AF) == SET)
  3413. {
  3414. /* Generate stop if necessary only in case of FMPI2C peripheral in MASTER mode */
  3415. if((hfmpi2c->State == HAL_FMPI2C_STATE_MASTER_BUSY_TX) || (hfmpi2c->State == HAL_FMPI2C_STATE_MEM_BUSY_TX)
  3416. || (hfmpi2c->State == HAL_FMPI2C_STATE_MEM_BUSY_RX))
  3417. {
  3418. /* No need to generate the STOP condition if AUTOEND mode is enabled */
  3419. /* Generate the STOP condition only in case of SOFTEND mode is enabled */
  3420. if((hfmpi2c->Instance->CR2 & FMPI2C_AUTOEND_MODE) != FMPI2C_AUTOEND_MODE)
  3421. {
  3422. /* Generate Stop */
  3423. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_STOP;
  3424. }
  3425. }
  3426. /* Wait until STOP Flag is reset */
  3427. /* AutoEnd should be initiate after AF */
  3428. while(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF) == RESET)
  3429. {
  3430. /* Check for the Timeout */
  3431. if(Timeout != HAL_MAX_DELAY)
  3432. {
  3433. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  3434. {
  3435. hfmpi2c->State= HAL_FMPI2C_STATE_READY;
  3436. /* Process Unlocked */
  3437. __HAL_UNLOCK(hfmpi2c);
  3438. return HAL_TIMEOUT;
  3439. }
  3440. }
  3441. }
  3442. /* Clear NACKF Flag */
  3443. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_AF);
  3444. /* Clear STOP Flag */
  3445. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  3446. /* Clear Configuration Register 2 */
  3447. __HAL_FMPI2C_RESET_CR2(hfmpi2c);
  3448. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_AF;
  3449. hfmpi2c->State= HAL_FMPI2C_STATE_READY;
  3450. /* Process Unlocked */
  3451. __HAL_UNLOCK(hfmpi2c);
  3452. return HAL_ERROR;
  3453. }
  3454. return HAL_OK;
  3455. }
  3456. /**
  3457. * @brief Handles FMPI2Cx communication when starting transfer or during transfer (TC or TCR flag are set).
  3458. * @param hfmpi2c: FMPI2C handle.
  3459. * @param DevAddress: specifies the slave address to be programmed.
  3460. * @param Size: specifies the number of bytes to be programmed.
  3461. * This parameter must be a value between 0 and 255.
  3462. * @param Mode: new state of the FMPI2C START condition generation.
  3463. * This parameter can be one of the following values:
  3464. * @arg FMPI2C_RELOAD_MODE: Enable Reload mode .
  3465. * @arg FMPI2C_AUTOEND_MODE: Enable Automatic end mode.
  3466. * @arg FMPI2C_SOFTEND_MODE: Enable Software end mode.
  3467. * @param Request: new state of the FMPI2C START condition generation.
  3468. * This parameter can be one of the following values:
  3469. * @arg FMPI2C_NO_STARTSTOP: Don't Generate stop and start condition.
  3470. * @arg FMPI2C_GENERATE_STOP: Generate stop condition (Size should be set to 0).
  3471. * @arg FMPI2C_GENERATE_START_READ: Generate Restart for read request.
  3472. * @arg FMPI2C_GENERATE_START_WRITE: Generate Restart for write request.
  3473. * @retval None
  3474. */
  3475. static void FMPI2C_TransferConfig(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint8_t Size, uint32_t Mode, uint32_t Request)
  3476. {
  3477. uint32_t tmpreg = 0;
  3478. /* Check the parameters */
  3479. assert_param(IS_FMPI2C_ALL_INSTANCE(hfmpi2c->Instance));
  3480. assert_param(IS_TRANSFER_MODE(Mode));
  3481. assert_param(IS_TRANSFER_REQUEST(Request));
  3482. /* Get the CR2 register value */
  3483. tmpreg = hfmpi2c->Instance->CR2;
  3484. /* clear tmpreg specific bits */
  3485. 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));
  3486. /* update tmpreg */
  3487. tmpreg |= (uint32_t)(((uint32_t)DevAddress & FMPI2C_CR2_SADD) | (((uint32_t)Size << 16 ) & FMPI2C_CR2_NBYTES) | \
  3488. (uint32_t)Mode | (uint32_t)Request);
  3489. /* update CR2 register */
  3490. hfmpi2c->Instance->CR2 = tmpreg;
  3491. }
  3492. /**
  3493. * @}
  3494. */
  3495. /**
  3496. * @}
  3497. */
  3498. #endif /* STM32F446xx */
  3499. #endif /* HAL_FMPI2C_MODULE_ENABLED */
  3500. /**
  3501. * @}
  3502. */
  3503. /**
  3504. * @}
  3505. */
  3506. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/