stm32f4xx_hal_fmpi2c.c 130 KB

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