stm32l4xx_hal_mmc.c 151 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_mmc.c
  4. * @author MCD Application Team
  5. * @brief MMC card HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Secure Digital (MMC) peripheral:
  8. * + Initialization and de-initialization functions
  9. * + IO operation functions
  10. * + Peripheral Control functions
  11. * + MMC card Control functions
  12. *
  13. ******************************************************************************
  14. * @attention
  15. *
  16. * Copyright (c) 2017 STMicroelectronics.
  17. * All rights reserved.
  18. *
  19. * This software is licensed under terms that can be found in the LICENSE file
  20. * in the root directory of this software component.
  21. * If no LICENSE file comes with this software, it is provided AS-IS.
  22. *
  23. ******************************************************************************
  24. @verbatim
  25. ==============================================================================
  26. ##### How to use this driver #####
  27. ==============================================================================
  28. [..]
  29. This driver implements a high level communication layer for read and write from/to
  30. this memory. The needed STM32 hardware resources (SDMMC and GPIO) are performed by
  31. the user in HAL_MMC_MspInit() function (MSP layer).
  32. Basically, the MSP layer configuration should be the same as we provide in the
  33. examples.
  34. You can easily tailor this configuration according to hardware resources.
  35. [..]
  36. This driver is a generic layered driver for SDMMC memories which uses the HAL
  37. SDMMC driver functions to interface with MMC and eMMC cards devices.
  38. It is used as follows:
  39. (#)Initialize the SDMMC low level resources by implement the HAL_MMC_MspInit() API:
  40. (##) Enable the SDMMC interface clock using __HAL_RCC_SDMMC_CLK_ENABLE();
  41. (##) SDMMC pins configuration for MMC card
  42. (+++) Enable the clock for the SDMMC GPIOs using the functions __HAL_RCC_GPIOx_CLK_ENABLE();
  43. (+++) Configure these SDMMC pins as alternate function pull-up using HAL_GPIO_Init()
  44. and according to your pin assignment;
  45. (##) On STM32L4Rx/STM32L4Sxx devices, no DMA configuration is need, an internal DMA for SDMMC Peripheral is used.
  46. (##) On other devices, perform DMA Configuration if you need to use DMA process (HAL_MMC_ReadBlocks_DMA()
  47. and HAL_MMC_WriteBlocks_DMA() APIs).
  48. (+++) Enable the DMAx interface clock using __HAL_RCC_DMAx_CLK_ENABLE();
  49. (+++) Configure the DMA using the function HAL_DMA_Init() with predeclared and filled.
  50. (##) NVIC configuration if you need to use interrupt process when using DMA transfer.
  51. (+++) Configure the SDMMC and DMA interrupt priorities using function HAL_NVIC_SetPriority();
  52. DMA priority is superior to SDMMC's priority
  53. (+++) Enable the NVIC DMA and SDMMC IRQs using function HAL_NVIC_EnableIRQ()
  54. (+++) SDMMC interrupts are managed using the macros __HAL_MMC_ENABLE_IT()
  55. and __HAL_MMC_DISABLE_IT() inside the communication process.
  56. (+++) SDMMC interrupts pending bits are managed using the macros __HAL_MMC_GET_IT()
  57. and __HAL_MMC_CLEAR_IT()
  58. (##) NVIC configuration if you need to use interrupt process (HAL_MMC_ReadBlocks_IT()
  59. and HAL_MMC_WriteBlocks_IT() APIs).
  60. (+++) Configure the SDMMC interrupt priorities using function HAL_NVIC_SetPriority();
  61. (+++) Enable the NVIC SDMMC IRQs using function HAL_NVIC_EnableIRQ()
  62. (+++) SDMMC interrupts are managed using the macros __HAL_MMC_ENABLE_IT()
  63. and __HAL_MMC_DISABLE_IT() inside the communication process.
  64. (+++) SDMMC interrupts pending bits are managed using the macros __HAL_MMC_GET_IT()
  65. and __HAL_MMC_CLEAR_IT()
  66. (#) At this stage, you can perform MMC read/write/erase operations after MMC card initialization
  67. *** MMC Card Initialization and configuration ***
  68. ================================================
  69. [..]
  70. To initialize the MMC Card, use the HAL_MMC_Init() function. It Initializes
  71. SDMMC Peripheral (STM32 side) and the MMC Card, and put it into StandBy State (Ready for data transfer).
  72. This function provide the following operations:
  73. (#) Initialize the SDMMC peripheral interface with default configuration.
  74. The initialization process is done at 400KHz. You can change or adapt
  75. this frequency by adjusting the "ClockDiv" field.
  76. The MMC Card frequency (SDMMC_CK) is computed as follows:
  77. SDMMC_CK = SDMMCCLK / (2 * ClockDiv) on STM32L4Rx/STM32L4Sxx devices
  78. SDMMC_CK = SDMMCCLK / (ClockDiv + 2) on other devices
  79. In initialization mode and according to the MMC Card standard,
  80. make sure that the SDMMC_CK frequency doesn't exceed 400KHz.
  81. This phase of initialization is done through SDMMC_Init() and
  82. SDMMC_PowerState_ON() SDMMC low level APIs.
  83. (#) Initialize the MMC card. The API used is HAL_MMC_InitCard().
  84. This phase allows the card initialization and identification
  85. and check the MMC Card type (Standard Capacity or High Capacity)
  86. The initialization flow is compatible with MMC standard.
  87. This API (HAL_MMC_InitCard()) could be used also to reinitialize the card in case
  88. of plug-off plug-in.
  89. (#) Configure the MMC Card Data transfer frequency. By Default, the card transfer
  90. frequency by adjusting the "ClockDiv" field.
  91. In transfer mode and according to the MMC Card standard, make sure that the
  92. SDMMC_CK frequency doesn't exceed 25MHz and 100MHz in High-speed mode switch.
  93. (#) Select the corresponding MMC Card according to the address read with the step 2.
  94. (#) Configure the MMC Card in wide bus mode: 4-bits data.
  95. *** MMC Card Read operation ***
  96. ==============================
  97. [..]
  98. (+) You can read from MMC card in polling mode by using function HAL_MMC_ReadBlocks().
  99. This function support only 512-bytes block length (the block size should be
  100. chosen as 512 bytes).
  101. You can choose either one block read operation or multiple block read operation
  102. by adjusting the "NumberOfBlocks" parameter.
  103. After this, you have to ensure that the transfer is done correctly. The check is done
  104. through HAL_MMC_GetCardState() function for MMC card state.
  105. (+) You can read from MMC card in DMA mode by using function HAL_MMC_ReadBlocks_DMA().
  106. This function support only 512-bytes block length (the block size should be
  107. chosen as 512 bytes).
  108. You can choose either one block read operation or multiple block read operation
  109. by adjusting the "NumberOfBlocks" parameter.
  110. After this, you have to ensure that the transfer is done correctly. The check is done
  111. through HAL_MMC_GetCardState() function for MMC card state.
  112. You could also check the DMA transfer process through the MMC Rx interrupt event.
  113. (+) You can read from MMC card in Interrupt mode by using function HAL_MMC_ReadBlocks_IT().
  114. This function allows the read of 512 bytes blocks.
  115. You can choose either one block read operation or multiple block read operation
  116. by adjusting the "NumberOfBlocks" parameter.
  117. After this, you have to ensure that the transfer is done correctly. The check is done
  118. through HAL_MMC_GetCardState() function for MMC card state.
  119. You could also check the IT transfer process through the MMC Rx interrupt event.
  120. *** MMC Card Write operation ***
  121. ===============================
  122. [..]
  123. (+) You can write to MMC card in polling mode by using function HAL_MMC_WriteBlocks().
  124. This function support only 512-bytes block length (the block size should be
  125. chosen as 512 bytes).
  126. You can choose either one block read operation or multiple block read operation
  127. by adjusting the "NumberOfBlocks" parameter.
  128. After this, you have to ensure that the transfer is done correctly. The check is done
  129. through HAL_MMC_GetCardState() function for MMC card state.
  130. (+) You can write to MMC card in DMA mode by using function HAL_MMC_WriteBlocks_DMA().
  131. This function support only 512-bytes block length (the block size should be
  132. chosen as 512 byte).
  133. You can choose either one block read operation or multiple block read operation
  134. by adjusting the "NumberOfBlocks" parameter.
  135. After this, you have to ensure that the transfer is done correctly. The check is done
  136. through HAL_MMC_GetCardState() function for MMC card state.
  137. You could also check the DMA transfer process through the MMC Tx interrupt event.
  138. (+) You can write to MMC card in Interrupt mode by using function HAL_MMC_WriteBlocks_IT().
  139. This function allows the read of 512 bytes blocks.
  140. You can choose either one block read operation or multiple block read operation
  141. by adjusting the "NumberOfBlocks" parameter.
  142. After this, you have to ensure that the transfer is done correctly. The check is done
  143. through HAL_MMC_GetCardState() function for MMC card state.
  144. You could also check the IT transfer process through the MMC Tx interrupt event.
  145. *** MMC card information ***
  146. ===========================
  147. [..]
  148. (+) To get MMC card information, you can use the function HAL_MMC_GetCardInfo().
  149. It returns useful information about the MMC card such as block size, card type,
  150. block number ...
  151. *** MMC card CSD register ***
  152. ============================
  153. [..]
  154. (+) The HAL_MMC_GetCardCSD() API allows to get the parameters of the CSD register.
  155. Some of the CSD parameters are useful for card initialization and identification.
  156. *** MMC card CID register ***
  157. ============================
  158. [..]
  159. (+) The HAL_MMC_GetCardCID() API allows to get the parameters of the CID register.
  160. Some of the CID parameters are useful for card initialization and identification.
  161. *** MMC HAL driver macros list ***
  162. ==================================
  163. [..]
  164. Below the list of most used macros in MMC HAL driver.
  165. (+) __HAL_MMC_ENABLE : Enable the MMC device
  166. (+) __HAL_MMC_DISABLE : Disable the MMC device
  167. (+) __HAL_MMC_DMA_ENABLE: Enable the SDMMC DMA transfer
  168. (+) __HAL_MMC_DMA_DISABLE: Disable the SDMMC DMA transfer
  169. (+) __HAL_MMC_ENABLE_IT: Enable the MMC device interrupt
  170. (+) __HAL_MMC_DISABLE_IT: Disable the MMC device interrupt
  171. (+) __HAL_MMC_GET_FLAG:Check whether the specified MMC flag is set or not
  172. (+) __HAL_MMC_CLEAR_FLAG: Clear the MMC's pending flags
  173. [..]
  174. (@) You can refer to the MMC HAL driver header file for more useful macros
  175. *** Callback registration ***
  176. =============================================
  177. [..]
  178. The compilation define USE_HAL_MMC_REGISTER_CALLBACKS when set to 1
  179. allows the user to configure dynamically the driver callbacks.
  180. Use Functions HAL_MMC_RegisterCallback() to register a user callback,
  181. it allows to register following callbacks:
  182. (+) TxCpltCallback : callback when a transmission transfer is completed.
  183. (+) RxCpltCallback : callback when a reception transfer is completed.
  184. (+) ErrorCallback : callback when error occurs.
  185. (+) AbortCpltCallback : callback when abort is completed.
  186. (+) Read_DMADblBuf0CpltCallback : callback when the DMA reception of first buffer is completed.
  187. (+) Read_DMADblBuf1CpltCallback : callback when the DMA reception of second buffer is completed.
  188. (+) Write_DMADblBuf0CpltCallback : callback when the DMA transmission of first buffer is completed.
  189. (+) Write_DMADblBuf1CpltCallback : callback when the DMA transmission of second buffer is completed.
  190. (+) MspInitCallback : MMC MspInit.
  191. (+) MspDeInitCallback : MMC MspDeInit.
  192. This function takes as parameters the HAL peripheral handle, the Callback ID
  193. and a pointer to the user callback function.
  194. Use function HAL_MMC_UnRegisterCallback() to reset a callback to the default
  195. weak (surcharged) function. It allows to reset following callbacks:
  196. (+) TxCpltCallback : callback when a transmission transfer is completed.
  197. (+) RxCpltCallback : callback when a reception transfer is completed.
  198. (+) ErrorCallback : callback when error occurs.
  199. (+) AbortCpltCallback : callback when abort is completed.
  200. (+) Read_DMADblBuf0CpltCallback : callback when the DMA reception of first buffer is completed.
  201. (+) Read_DMADblBuf1CpltCallback : callback when the DMA reception of second buffer is completed.
  202. (+) Write_DMADblBuf0CpltCallback : callback when the DMA transmission of first buffer is completed.
  203. (+) Write_DMADblBuf1CpltCallback : callback when the DMA transmission of second buffer is completed.
  204. (+) MspInitCallback : MMC MspInit.
  205. (+) MspDeInitCallback : MMC MspDeInit.
  206. This function) takes as parameters the HAL peripheral handle and the Callback ID.
  207. By default, after the HAL_MMC_Init and if the state is HAL_MMC_STATE_RESET
  208. all callbacks are reset to the corresponding legacy weak (surcharged) functions.
  209. Exception done for MspInit and MspDeInit callbacks that are respectively
  210. reset to the legacy weak (surcharged) functions in the HAL_MMC_Init
  211. and HAL_MMC_DeInit only when these callbacks are null (not registered beforehand).
  212. If not, MspInit or MspDeInit are not null, the HAL_MMC_Init and HAL_MMC_DeInit
  213. keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
  214. Callbacks can be registered/unregistered in READY state only.
  215. Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
  216. in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
  217. during the Init/DeInit.
  218. In that case first register the MspInit/MspDeInit user callbacks
  219. using HAL_MMC_RegisterCallback before calling HAL_MMC_DeInit
  220. or HAL_MMC_Init function.
  221. When The compilation define USE_HAL_MMC_REGISTER_CALLBACKS is set to 0 or
  222. not defined, the callback registering feature is not available
  223. and weak (surcharged) callbacks are used.
  224. @endverbatim
  225. ******************************************************************************
  226. */
  227. /* Includes ------------------------------------------------------------------*/
  228. #include "stm32l4xx_hal.h"
  229. #ifdef HAL_MMC_MODULE_ENABLED
  230. #if defined(SDMMC1)
  231. /** @addtogroup STM32L4xx_HAL_Driver
  232. * @{
  233. */
  234. /** @defgroup MMC MMC
  235. * @{
  236. */
  237. /* Private typedef -----------------------------------------------------------*/
  238. /* Private define ------------------------------------------------------------*/
  239. /** @addtogroup MMC_Private_Defines
  240. * @{
  241. */
  242. #if defined (VDD_VALUE) && (VDD_VALUE <= 1950U)
  243. #define MMC_VOLTAGE_RANGE EMMC_LOW_VOLTAGE_RANGE
  244. #define MMC_EXT_CSD_PWR_CL_26_INDEX 201
  245. #define MMC_EXT_CSD_PWR_CL_52_INDEX 200
  246. #define MMC_EXT_CSD_PWR_CL_DDR_52_INDEX 238
  247. #define MMC_EXT_CSD_PWR_CL_26_POS 8
  248. #define MMC_EXT_CSD_PWR_CL_52_POS 0
  249. #define MMC_EXT_CSD_PWR_CL_DDR_52_POS 16
  250. #else
  251. #define MMC_VOLTAGE_RANGE EMMC_HIGH_VOLTAGE_RANGE
  252. #define MMC_EXT_CSD_PWR_CL_26_INDEX 203
  253. #define MMC_EXT_CSD_PWR_CL_52_INDEX 202
  254. #define MMC_EXT_CSD_PWR_CL_DDR_52_INDEX 239
  255. #define MMC_EXT_CSD_PWR_CL_26_POS 24
  256. #define MMC_EXT_CSD_PWR_CL_52_POS 16
  257. #define MMC_EXT_CSD_PWR_CL_DDR_52_POS 24
  258. #endif
  259. #define MMC_EXT_CSD_SLEEP_NOTIFICATION_TIME_INDEX 216
  260. #define MMC_EXT_CSD_SLEEP_NOTIFICATION_TIME_POS 0
  261. #define MMC_EXT_CSD_S_A_TIMEOUT_INDEX 217
  262. #define MMC_EXT_CSD_S_A_TIMEOUT_POS 8
  263. /* Frequencies used in the driver for clock divider calculation */
  264. #define MMC_INIT_FREQ 400000U /* Initialization phase : 400 kHz max */
  265. #define MMC_HIGH_SPEED_FREQ 52000000U /* High speed phase : 52 MHz max */
  266. /**
  267. * @}
  268. */
  269. /* Private macro -------------------------------------------------------------*/
  270. /* Private variables ---------------------------------------------------------*/
  271. /* Private function prototypes -----------------------------------------------*/
  272. /* Private functions ---------------------------------------------------------*/
  273. /** @defgroup MMC_Private_Functions MMC Private Functions
  274. * @{
  275. */
  276. static uint32_t MMC_InitCard(MMC_HandleTypeDef *hmmc);
  277. static uint32_t MMC_PowerON(MMC_HandleTypeDef *hmmc);
  278. static uint32_t MMC_SendStatus(MMC_HandleTypeDef *hmmc, uint32_t *pCardStatus);
  279. static void MMC_PowerOFF(MMC_HandleTypeDef *hmmc);
  280. static void MMC_Write_IT(MMC_HandleTypeDef *hmmc);
  281. static void MMC_Read_IT(MMC_HandleTypeDef *hmmc);
  282. #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  283. static void MMC_DMATransmitCplt(DMA_HandleTypeDef *hdma);
  284. static void MMC_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
  285. static void MMC_DMAError(DMA_HandleTypeDef *hdma);
  286. static void MMC_DMATxAbort(DMA_HandleTypeDef *hdma);
  287. static void MMC_DMARxAbort(DMA_HandleTypeDef *hdma);
  288. #else
  289. static uint32_t MMC_HighSpeed(MMC_HandleTypeDef *hmmc, FunctionalState state);
  290. static uint32_t MMC_DDR_Mode(MMC_HandleTypeDef *hmmc, FunctionalState state);
  291. #endif
  292. static HAL_StatusTypeDef MMC_ReadExtCSD(MMC_HandleTypeDef *hmmc, uint32_t *pFieldData, uint16_t FieldIndex, uint32_t Timeout);
  293. static uint32_t MMC_PwrClassUpdate(MMC_HandleTypeDef *hmmc, uint32_t Wide, uint32_t Speed);
  294. /**
  295. * @}
  296. */
  297. /* Exported functions --------------------------------------------------------*/
  298. /** @defgroup MMC_Exported_Functions MMC Exported Functions
  299. * @{
  300. */
  301. /** @defgroup MMC_Exported_Functions_Group1 MMC_Exported_Functions_Group1
  302. * @brief Initialization and de-initialization functions
  303. *
  304. @verbatim
  305. ==============================================================================
  306. ##### Initialization and de-initialization functions #####
  307. ==============================================================================
  308. [..]
  309. This section provides functions allowing to initialize/de-initialize the MMC
  310. card device to be ready for use.
  311. @endverbatim
  312. * @{
  313. */
  314. /**
  315. * @brief Initializes the MMC according to the specified parameters in the
  316. MMC_HandleTypeDef and create the associated handle.
  317. * @param hmmc Pointer to the MMC handle
  318. * @retval HAL status
  319. */
  320. HAL_StatusTypeDef HAL_MMC_Init(MMC_HandleTypeDef *hmmc)
  321. {
  322. /* Check the MMC handle allocation */
  323. if(hmmc == NULL)
  324. {
  325. return HAL_ERROR;
  326. }
  327. /* Check the parameters */
  328. assert_param(IS_SDMMC_ALL_INSTANCE(hmmc->Instance));
  329. assert_param(IS_SDMMC_CLOCK_EDGE(hmmc->Init.ClockEdge));
  330. #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  331. assert_param(IS_SDMMC_CLOCK_BYPASS(hmmc->Init.ClockBypass));
  332. #endif
  333. assert_param(IS_SDMMC_CLOCK_POWER_SAVE(hmmc->Init.ClockPowerSave));
  334. assert_param(IS_SDMMC_BUS_WIDE(hmmc->Init.BusWide));
  335. assert_param(IS_SDMMC_HARDWARE_FLOW_CONTROL(hmmc->Init.HardwareFlowControl));
  336. assert_param(IS_SDMMC_CLKDIV(hmmc->Init.ClockDiv));
  337. if(hmmc->State == HAL_MMC_STATE_RESET)
  338. {
  339. /* Allocate lock resource and initialize it */
  340. hmmc->Lock = HAL_UNLOCKED;
  341. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  342. /* Reset Callback pointers in HAL_MMC_STATE_RESET only */
  343. hmmc->TxCpltCallback = HAL_MMC_TxCpltCallback;
  344. hmmc->RxCpltCallback = HAL_MMC_RxCpltCallback;
  345. hmmc->ErrorCallback = HAL_MMC_ErrorCallback;
  346. hmmc->AbortCpltCallback = HAL_MMC_AbortCallback;
  347. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  348. hmmc->Read_DMADblBuf0CpltCallback = HAL_MMCEx_Read_DMADoubleBuffer0CpltCallback;
  349. hmmc->Read_DMADblBuf1CpltCallback = HAL_MMCEx_Read_DMADoubleBuffer1CpltCallback;
  350. hmmc->Write_DMADblBuf0CpltCallback = HAL_MMCEx_Write_DMADoubleBuffer0CpltCallback;
  351. hmmc->Write_DMADblBuf1CpltCallback = HAL_MMCEx_Write_DMADoubleBuffer1CpltCallback;
  352. #endif
  353. if(hmmc->MspInitCallback == NULL)
  354. {
  355. hmmc->MspInitCallback = HAL_MMC_MspInit;
  356. }
  357. /* Init the low level hardware */
  358. hmmc->MspInitCallback(hmmc);
  359. #else
  360. /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
  361. HAL_MMC_MspInit(hmmc);
  362. #endif
  363. }
  364. hmmc->State = HAL_MMC_STATE_BUSY;
  365. /* Initialize the Card parameters */
  366. if(HAL_MMC_InitCard(hmmc) == HAL_ERROR)
  367. {
  368. return HAL_ERROR;
  369. }
  370. /* Initialize the error code */
  371. hmmc->ErrorCode = HAL_DMA_ERROR_NONE;
  372. /* Initialize the MMC operation */
  373. hmmc->Context = MMC_CONTEXT_NONE;
  374. /* Initialize the MMC state */
  375. hmmc->State = HAL_MMC_STATE_READY;
  376. /* Configure bus width */
  377. if (hmmc->Init.BusWide != SDMMC_BUS_WIDE_1B)
  378. {
  379. if (HAL_MMC_ConfigWideBusOperation(hmmc, hmmc->Init.BusWide) != HAL_OK)
  380. {
  381. return HAL_ERROR;
  382. }
  383. }
  384. return HAL_OK;
  385. }
  386. /**
  387. * @brief Initializes the MMC Card.
  388. * @param hmmc Pointer to MMC handle
  389. * @note This function initializes the MMC card. It could be used when a card
  390. re-initialization is needed.
  391. * @retval HAL status
  392. */
  393. HAL_StatusTypeDef HAL_MMC_InitCard(MMC_HandleTypeDef *hmmc)
  394. {
  395. uint32_t errorstate;
  396. MMC_InitTypeDef Init;
  397. uint32_t sdmmc_clk;
  398. /* Default SDMMC peripheral configuration for MMC card initialization */
  399. Init.ClockEdge = SDMMC_CLOCK_EDGE_RISING;
  400. #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  401. Init.ClockBypass = SDMMC_CLOCK_BYPASS_DISABLE;
  402. #endif
  403. Init.ClockPowerSave = SDMMC_CLOCK_POWER_SAVE_DISABLE;
  404. Init.BusWide = SDMMC_BUS_WIDE_1B;
  405. Init.HardwareFlowControl = SDMMC_HARDWARE_FLOW_CONTROL_DISABLE;
  406. /* Init Clock should be less or equal to 400Khz*/
  407. sdmmc_clk = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_SDMMC1);
  408. if (sdmmc_clk == 0U)
  409. {
  410. hmmc->State = HAL_MMC_STATE_READY;
  411. hmmc->ErrorCode = SDMMC_ERROR_INVALID_PARAMETER;
  412. return HAL_ERROR;
  413. }
  414. #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  415. Init.ClockDiv = ((sdmmc_clk/MMC_INIT_FREQ) - 2U);
  416. #else
  417. Init.ClockDiv = sdmmc_clk/(2U*MMC_INIT_FREQ);
  418. Init.Transceiver = SDMMC_TRANSCEIVER_DISABLE;
  419. #endif
  420. /* Initialize SDMMC peripheral interface with default configuration */
  421. (void)SDMMC_Init(hmmc->Instance, Init);
  422. #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  423. /* Disable SDMMC Clock */
  424. __HAL_MMC_DISABLE(hmmc);
  425. #endif
  426. /* Set Power State to ON */
  427. (void)SDMMC_PowerState_ON(hmmc->Instance);
  428. #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  429. /* Enable MMC Clock */
  430. __HAL_MMC_ENABLE(hmmc);
  431. #endif
  432. /* wait 74 Cycles: required power up waiting time before starting
  433. the MMC initialization sequence */
  434. #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  435. sdmmc_clk = sdmmc_clk/(Init.ClockDiv + 2U);
  436. #else
  437. sdmmc_clk = sdmmc_clk/(2U*Init.ClockDiv);
  438. #endif
  439. HAL_Delay(1U+ (74U*1000U/(sdmmc_clk)));
  440. /* Identify card operating voltage */
  441. errorstate = MMC_PowerON(hmmc);
  442. if(errorstate != HAL_MMC_ERROR_NONE)
  443. {
  444. hmmc->State = HAL_MMC_STATE_READY;
  445. hmmc->ErrorCode |= errorstate;
  446. return HAL_ERROR;
  447. }
  448. /* Card initialization */
  449. errorstate = MMC_InitCard(hmmc);
  450. if(errorstate != HAL_MMC_ERROR_NONE)
  451. {
  452. hmmc->State = HAL_MMC_STATE_READY;
  453. hmmc->ErrorCode |= errorstate;
  454. return HAL_ERROR;
  455. }
  456. /* Set Block Size for Card */
  457. errorstate = SDMMC_CmdBlockLength(hmmc->Instance, MMC_BLOCKSIZE);
  458. if(errorstate != HAL_MMC_ERROR_NONE)
  459. {
  460. /* Clear all the static flags */
  461. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
  462. hmmc->ErrorCode |= errorstate;
  463. hmmc->State = HAL_MMC_STATE_READY;
  464. return HAL_ERROR;
  465. }
  466. return HAL_OK;
  467. }
  468. /**
  469. * @brief De-Initializes the MMC card.
  470. * @param hmmc Pointer to MMC handle
  471. * @retval HAL status
  472. */
  473. HAL_StatusTypeDef HAL_MMC_DeInit(MMC_HandleTypeDef *hmmc)
  474. {
  475. /* Check the MMC handle allocation */
  476. if(hmmc == NULL)
  477. {
  478. return HAL_ERROR;
  479. }
  480. /* Check the parameters */
  481. assert_param(IS_SDMMC_ALL_INSTANCE(hmmc->Instance));
  482. hmmc->State = HAL_MMC_STATE_BUSY;
  483. /* Set MMC power state to off */
  484. MMC_PowerOFF(hmmc);
  485. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  486. if(hmmc->MspDeInitCallback == NULL)
  487. {
  488. hmmc->MspDeInitCallback = HAL_MMC_MspDeInit;
  489. }
  490. /* DeInit the low level hardware */
  491. hmmc->MspDeInitCallback(hmmc);
  492. #else
  493. /* De-Initialize the MSP layer */
  494. HAL_MMC_MspDeInit(hmmc);
  495. #endif
  496. hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
  497. hmmc->State = HAL_MMC_STATE_RESET;
  498. return HAL_OK;
  499. }
  500. /**
  501. * @brief Initializes the MMC MSP.
  502. * @param hmmc Pointer to MMC handle
  503. * @retval None
  504. */
  505. __weak void HAL_MMC_MspInit(MMC_HandleTypeDef *hmmc)
  506. {
  507. /* Prevent unused argument(s) compilation warning */
  508. UNUSED(hmmc);
  509. /* NOTE : This function Should not be modified, when the callback is needed,
  510. the HAL_MMC_MspInit could be implemented in the user file
  511. */
  512. }
  513. /**
  514. * @brief De-Initialize MMC MSP.
  515. * @param hmmc Pointer to MMC handle
  516. * @retval None
  517. */
  518. __weak void HAL_MMC_MspDeInit(MMC_HandleTypeDef *hmmc)
  519. {
  520. /* Prevent unused argument(s) compilation warning */
  521. UNUSED(hmmc);
  522. /* NOTE : This function Should not be modified, when the callback is needed,
  523. the HAL_MMC_MspDeInit could be implemented in the user file
  524. */
  525. }
  526. /**
  527. * @}
  528. */
  529. /** @addtogroup MMC_Exported_Functions_Group2
  530. * @brief Data transfer functions
  531. *
  532. @verbatim
  533. ==============================================================================
  534. ##### IO operation functions #####
  535. ==============================================================================
  536. [..]
  537. This subsection provides a set of functions allowing to manage the data
  538. transfer from/to MMC card.
  539. @endverbatim
  540. * @{
  541. */
  542. /**
  543. * @brief Reads block(s) from a specified address in a card. The Data transfer
  544. * is managed by polling mode.
  545. * @note This API should be followed by a check on the card state through
  546. * HAL_MMC_GetCardState().
  547. * @param hmmc Pointer to MMC handle
  548. * @param pData pointer to the buffer that will contain the received data
  549. * @param BlockAdd Block Address from where data is to be read
  550. * @param NumberOfBlocks Number of MMC blocks to read
  551. * @param Timeout Specify timeout value
  552. * @retval HAL status
  553. */
  554. HAL_StatusTypeDef HAL_MMC_ReadBlocks(MMC_HandleTypeDef *hmmc, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout)
  555. {
  556. SDMMC_DataInitTypeDef config;
  557. uint32_t errorstate;
  558. uint32_t tickstart = HAL_GetTick();
  559. uint32_t count, data, dataremaining;
  560. uint32_t add = BlockAdd;
  561. uint8_t *tempbuff = pData;
  562. if(NULL == pData)
  563. {
  564. hmmc->ErrorCode |= HAL_MMC_ERROR_PARAM;
  565. return HAL_ERROR;
  566. }
  567. if(hmmc->State == HAL_MMC_STATE_READY)
  568. {
  569. hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
  570. if((add + NumberOfBlocks) > (hmmc->MmcCard.LogBlockNbr))
  571. {
  572. hmmc->ErrorCode |= HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
  573. return HAL_ERROR;
  574. }
  575. hmmc->State = HAL_MMC_STATE_BUSY;
  576. /* Initialize data control register */
  577. hmmc->Instance->DCTRL = 0U;
  578. if ((hmmc->MmcCard.CardType) != MMC_HIGH_CAPACITY_CARD)
  579. {
  580. add *= 512U;
  581. }
  582. /* Configure the MMC DPSM (Data Path State Machine) */
  583. config.DataTimeOut = SDMMC_DATATIMEOUT;
  584. config.DataLength = NumberOfBlocks * MMC_BLOCKSIZE;
  585. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
  586. config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
  587. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  588. #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  589. config.DPSM = SDMMC_DPSM_ENABLE;
  590. #else
  591. config.DPSM = SDMMC_DPSM_DISABLE;
  592. #endif
  593. (void)SDMMC_ConfigData(hmmc->Instance, &config);
  594. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  595. __SDMMC_CMDTRANS_ENABLE( hmmc->Instance);
  596. #endif
  597. /* Read block(s) in polling mode */
  598. if(NumberOfBlocks > 1U)
  599. {
  600. hmmc->Context = MMC_CONTEXT_READ_MULTIPLE_BLOCK;
  601. /* Read Multi Block command */
  602. errorstate = SDMMC_CmdReadMultiBlock(hmmc->Instance, add);
  603. }
  604. else
  605. {
  606. hmmc->Context = MMC_CONTEXT_READ_SINGLE_BLOCK;
  607. /* Read Single Block command */
  608. errorstate = SDMMC_CmdReadSingleBlock(hmmc->Instance, add);
  609. }
  610. if(errorstate != HAL_MMC_ERROR_NONE)
  611. {
  612. /* Clear all the static flags */
  613. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
  614. hmmc->ErrorCode |= errorstate;
  615. hmmc->State = HAL_MMC_STATE_READY;
  616. hmmc->Context = MMC_CONTEXT_NONE;
  617. return HAL_ERROR;
  618. }
  619. /* Poll on SDMMC flags */
  620. dataremaining = config.DataLength;
  621. while(!__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DATAEND))
  622. {
  623. if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_RXFIFOHF) && (dataremaining > 0U))
  624. {
  625. /* Read data from SDMMC Rx FIFO */
  626. for(count = 0U; count < 8U; count++)
  627. {
  628. data = SDMMC_ReadFIFO(hmmc->Instance);
  629. *tempbuff = (uint8_t)(data & 0xFFU);
  630. tempbuff++;
  631. dataremaining--;
  632. *tempbuff = (uint8_t)((data >> 8U) & 0xFFU);
  633. tempbuff++;
  634. dataremaining--;
  635. *tempbuff = (uint8_t)((data >> 16U) & 0xFFU);
  636. tempbuff++;
  637. dataremaining--;
  638. *tempbuff = (uint8_t)((data >> 24U) & 0xFFU);
  639. tempbuff++;
  640. dataremaining--;
  641. }
  642. }
  643. if(((HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U))
  644. {
  645. /* Clear all the static flags */
  646. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
  647. hmmc->ErrorCode |= HAL_MMC_ERROR_TIMEOUT;
  648. hmmc->State= HAL_MMC_STATE_READY;
  649. hmmc->Context = MMC_CONTEXT_NONE;
  650. return HAL_TIMEOUT;
  651. }
  652. }
  653. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  654. __SDMMC_CMDTRANS_DISABLE( hmmc->Instance);
  655. #endif
  656. /* Send stop transmission command in case of multiblock read */
  657. if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_DATAEND) && (NumberOfBlocks > 1U))
  658. {
  659. /* Send stop transmission command */
  660. errorstate = SDMMC_CmdStopTransfer(hmmc->Instance);
  661. if(errorstate != HAL_MMC_ERROR_NONE)
  662. {
  663. /* Clear all the static flags */
  664. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
  665. hmmc->ErrorCode |= errorstate;
  666. hmmc->State = HAL_MMC_STATE_READY;
  667. hmmc->Context = MMC_CONTEXT_NONE;
  668. return HAL_ERROR;
  669. }
  670. }
  671. /* Get error state */
  672. if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_DTIMEOUT))
  673. {
  674. /* Clear all the static flags */
  675. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
  676. hmmc->ErrorCode |= HAL_MMC_ERROR_DATA_TIMEOUT;
  677. hmmc->State = HAL_MMC_STATE_READY;
  678. hmmc->Context = MMC_CONTEXT_NONE;
  679. return HAL_ERROR;
  680. }
  681. else if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_DCRCFAIL))
  682. {
  683. /* Clear all the static flags */
  684. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
  685. hmmc->ErrorCode |= HAL_MMC_ERROR_DATA_CRC_FAIL;
  686. hmmc->State = HAL_MMC_STATE_READY;
  687. hmmc->Context = MMC_CONTEXT_NONE;
  688. return HAL_ERROR;
  689. }
  690. else if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_RXOVERR))
  691. {
  692. /* Clear all the static flags */
  693. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
  694. hmmc->ErrorCode |= HAL_MMC_ERROR_RX_OVERRUN;
  695. hmmc->State = HAL_MMC_STATE_READY;
  696. hmmc->Context = MMC_CONTEXT_NONE;
  697. return HAL_ERROR;
  698. }
  699. else
  700. {
  701. /* Nothing to do */
  702. }
  703. #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  704. /* Empty FIFO if there is still any data */
  705. while ((__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_RXDAVL)) && (dataremaining > 0U))
  706. {
  707. data = SDMMC_ReadFIFO(hmmc->Instance);
  708. *tempbuff = (uint8_t)(data & 0xFFU);
  709. tempbuff++;
  710. dataremaining--;
  711. *tempbuff = (uint8_t)((data >> 8U) & 0xFFU);
  712. tempbuff++;
  713. dataremaining--;
  714. *tempbuff = (uint8_t)((data >> 16U) & 0xFFU);
  715. tempbuff++;
  716. dataremaining--;
  717. *tempbuff = (uint8_t)((data >> 24U) & 0xFFU);
  718. tempbuff++;
  719. dataremaining--;
  720. if(((HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U))
  721. {
  722. /* Clear all the static flags */
  723. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
  724. hmmc->ErrorCode |= HAL_MMC_ERROR_TIMEOUT;
  725. hmmc->State= HAL_MMC_STATE_READY;
  726. hmmc->Context = MMC_CONTEXT_NONE;
  727. return HAL_ERROR;
  728. }
  729. }
  730. #endif
  731. /* Clear all the static flags */
  732. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_DATA_FLAGS);
  733. hmmc->State = HAL_MMC_STATE_READY;
  734. return HAL_OK;
  735. }
  736. else
  737. {
  738. hmmc->ErrorCode |= HAL_MMC_ERROR_BUSY;
  739. return HAL_ERROR;
  740. }
  741. }
  742. /**
  743. * @brief Allows to write block(s) to a specified address in a card. The Data
  744. * transfer is managed by polling mode.
  745. * @note This API should be followed by a check on the card state through
  746. * HAL_MMC_GetCardState().
  747. * @param hmmc Pointer to MMC handle
  748. * @param pData pointer to the buffer that will contain the data to transmit
  749. * @param BlockAdd Block Address where data will be written
  750. * @param NumberOfBlocks Number of MMC blocks to write
  751. * @param Timeout Specify timeout value
  752. * @retval HAL status
  753. */
  754. HAL_StatusTypeDef HAL_MMC_WriteBlocks(MMC_HandleTypeDef *hmmc, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout)
  755. {
  756. SDMMC_DataInitTypeDef config;
  757. uint32_t errorstate;
  758. uint32_t tickstart = HAL_GetTick();
  759. uint32_t count, data, dataremaining;
  760. uint32_t add = BlockAdd;
  761. uint8_t *tempbuff = pData;
  762. if(NULL == pData)
  763. {
  764. hmmc->ErrorCode |= HAL_MMC_ERROR_PARAM;
  765. return HAL_ERROR;
  766. }
  767. if(hmmc->State == HAL_MMC_STATE_READY)
  768. {
  769. hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
  770. if((add + NumberOfBlocks) > (hmmc->MmcCard.LogBlockNbr))
  771. {
  772. hmmc->ErrorCode |= HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
  773. return HAL_ERROR;
  774. }
  775. hmmc->State = HAL_MMC_STATE_BUSY;
  776. /* Initialize data control register */
  777. hmmc->Instance->DCTRL = 0U;
  778. if ((hmmc->MmcCard.CardType) != MMC_HIGH_CAPACITY_CARD)
  779. {
  780. add *= 512U;
  781. }
  782. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  783. /* Configure the MMC DPSM (Data Path State Machine) */
  784. config.DataTimeOut = SDMMC_DATATIMEOUT;
  785. config.DataLength = NumberOfBlocks * MMC_BLOCKSIZE;
  786. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
  787. config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
  788. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  789. config.DPSM = SDMMC_DPSM_DISABLE;
  790. (void)SDMMC_ConfigData(hmmc->Instance, &config);
  791. __SDMMC_CMDTRANS_ENABLE( hmmc->Instance);
  792. #endif
  793. /* Write Blocks in Polling mode */
  794. if(NumberOfBlocks > 1U)
  795. {
  796. hmmc->Context = MMC_CONTEXT_WRITE_MULTIPLE_BLOCK;
  797. /* Write Multi Block command */
  798. errorstate = SDMMC_CmdWriteMultiBlock(hmmc->Instance, add);
  799. }
  800. else
  801. {
  802. hmmc->Context = MMC_CONTEXT_WRITE_SINGLE_BLOCK;
  803. /* Write Single Block command */
  804. errorstate = SDMMC_CmdWriteSingleBlock(hmmc->Instance, add);
  805. }
  806. if(errorstate != HAL_MMC_ERROR_NONE)
  807. {
  808. /* Clear all the static flags */
  809. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
  810. hmmc->ErrorCode |= errorstate;
  811. hmmc->State = HAL_MMC_STATE_READY;
  812. hmmc->Context = MMC_CONTEXT_NONE;
  813. return HAL_ERROR;
  814. }
  815. #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  816. /* Configure the MMC DPSM (Data Path State Machine) */
  817. config.DataTimeOut = SDMMC_DATATIMEOUT;
  818. config.DataLength = NumberOfBlocks * MMC_BLOCKSIZE;
  819. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
  820. config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
  821. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  822. config.DPSM = SDMMC_DPSM_ENABLE;
  823. (void)SDMMC_ConfigData(hmmc->Instance, &config);
  824. #endif
  825. /* Write block(s) in polling mode */
  826. dataremaining = config.DataLength;
  827. while(!__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_TXUNDERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DATAEND))
  828. {
  829. if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_TXFIFOHE) && (dataremaining > 0U))
  830. {
  831. /* Write data to SDMMC Tx FIFO */
  832. for(count = 0U; count < 8U; count++)
  833. {
  834. data = (uint32_t)(*tempbuff);
  835. tempbuff++;
  836. dataremaining--;
  837. data |= ((uint32_t)(*tempbuff) << 8U);
  838. tempbuff++;
  839. dataremaining--;
  840. data |= ((uint32_t)(*tempbuff) << 16U);
  841. tempbuff++;
  842. dataremaining--;
  843. data |= ((uint32_t)(*tempbuff) << 24U);
  844. tempbuff++;
  845. dataremaining--;
  846. (void)SDMMC_WriteFIFO(hmmc->Instance, &data);
  847. }
  848. }
  849. if(((HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U))
  850. {
  851. /* Clear all the static flags */
  852. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
  853. hmmc->ErrorCode |= errorstate;
  854. hmmc->State = HAL_MMC_STATE_READY;
  855. hmmc->Context = MMC_CONTEXT_NONE;
  856. return HAL_TIMEOUT;
  857. }
  858. }
  859. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  860. __SDMMC_CMDTRANS_DISABLE( hmmc->Instance);
  861. #endif
  862. /* Send stop transmission command in case of multiblock write */
  863. if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_DATAEND) && (NumberOfBlocks > 1U))
  864. {
  865. /* Send stop transmission command */
  866. errorstate = SDMMC_CmdStopTransfer(hmmc->Instance);
  867. if(errorstate != HAL_MMC_ERROR_NONE)
  868. {
  869. /* Clear all the static flags */
  870. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
  871. hmmc->ErrorCode |= errorstate;
  872. hmmc->State = HAL_MMC_STATE_READY;
  873. hmmc->Context = MMC_CONTEXT_NONE;
  874. return HAL_ERROR;
  875. }
  876. }
  877. /* Get error state */
  878. if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_DTIMEOUT))
  879. {
  880. /* Clear all the static flags */
  881. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
  882. hmmc->ErrorCode |= HAL_MMC_ERROR_DATA_TIMEOUT;
  883. hmmc->State = HAL_MMC_STATE_READY;
  884. hmmc->Context = MMC_CONTEXT_NONE;
  885. return HAL_ERROR;
  886. }
  887. else if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_DCRCFAIL))
  888. {
  889. /* Clear all the static flags */
  890. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
  891. hmmc->ErrorCode |= HAL_MMC_ERROR_DATA_CRC_FAIL;
  892. hmmc->State = HAL_MMC_STATE_READY;
  893. hmmc->Context = MMC_CONTEXT_NONE;
  894. return HAL_ERROR;
  895. }
  896. else if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_TXUNDERR))
  897. {
  898. /* Clear all the static flags */
  899. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
  900. hmmc->ErrorCode |= HAL_MMC_ERROR_TX_UNDERRUN;
  901. hmmc->State = HAL_MMC_STATE_READY;
  902. hmmc->Context = MMC_CONTEXT_NONE;
  903. return HAL_ERROR;
  904. }
  905. else
  906. {
  907. /* Nothing to do */
  908. }
  909. /* Clear all the static flags */
  910. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_DATA_FLAGS);
  911. hmmc->State = HAL_MMC_STATE_READY;
  912. return HAL_OK;
  913. }
  914. else
  915. {
  916. hmmc->ErrorCode |= HAL_MMC_ERROR_BUSY;
  917. return HAL_ERROR;
  918. }
  919. }
  920. /**
  921. * @brief Reads block(s) from a specified address in a card. The Data transfer
  922. * is managed in interrupt mode.
  923. * @note This API should be followed by a check on the card state through
  924. * HAL_MMC_GetCardState().
  925. * @note You could also check the IT transfer process through the MMC Rx
  926. * interrupt event.
  927. * @param hmmc Pointer to MMC handle
  928. * @param pData Pointer to the buffer that will contain the received data
  929. * @param BlockAdd Block Address from where data is to be read
  930. * @param NumberOfBlocks Number of blocks to read.
  931. * @retval HAL status
  932. */
  933. HAL_StatusTypeDef HAL_MMC_ReadBlocks_IT(MMC_HandleTypeDef *hmmc, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
  934. {
  935. SDMMC_DataInitTypeDef config;
  936. uint32_t errorstate;
  937. uint32_t add = BlockAdd;
  938. if(NULL == pData)
  939. {
  940. hmmc->ErrorCode |= HAL_MMC_ERROR_PARAM;
  941. return HAL_ERROR;
  942. }
  943. if(hmmc->State == HAL_MMC_STATE_READY)
  944. {
  945. hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
  946. if((add + NumberOfBlocks) > (hmmc->MmcCard.LogBlockNbr))
  947. {
  948. hmmc->ErrorCode |= HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
  949. return HAL_ERROR;
  950. }
  951. hmmc->State = HAL_MMC_STATE_BUSY;
  952. /* Initialize data control register */
  953. hmmc->Instance->DCTRL = 0U;
  954. hmmc->pRxBuffPtr = pData;
  955. hmmc->RxXferSize = MMC_BLOCKSIZE * NumberOfBlocks;
  956. if ((hmmc->MmcCard.CardType) != MMC_HIGH_CAPACITY_CARD)
  957. {
  958. add *= 512U;
  959. }
  960. /* Configure the MMC DPSM (Data Path State Machine) */
  961. config.DataTimeOut = SDMMC_DATATIMEOUT;
  962. config.DataLength = MMC_BLOCKSIZE * NumberOfBlocks;
  963. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
  964. config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
  965. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  966. #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  967. config.DPSM = SDMMC_DPSM_ENABLE;
  968. #else
  969. config.DPSM = SDMMC_DPSM_DISABLE;
  970. #endif
  971. (void)SDMMC_ConfigData(hmmc->Instance, &config);
  972. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  973. __SDMMC_CMDTRANS_ENABLE( hmmc->Instance);
  974. #endif
  975. /* Read Blocks in IT mode */
  976. if(NumberOfBlocks > 1U)
  977. {
  978. hmmc->Context = (MMC_CONTEXT_READ_MULTIPLE_BLOCK | MMC_CONTEXT_IT);
  979. /* Read Multi Block command */
  980. errorstate = SDMMC_CmdReadMultiBlock(hmmc->Instance, add);
  981. }
  982. else
  983. {
  984. hmmc->Context = (MMC_CONTEXT_READ_SINGLE_BLOCK | MMC_CONTEXT_IT);
  985. /* Read Single Block command */
  986. errorstate = SDMMC_CmdReadSingleBlock(hmmc->Instance, add);
  987. }
  988. if(errorstate != HAL_MMC_ERROR_NONE)
  989. {
  990. /* Clear all the static flags */
  991. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
  992. hmmc->ErrorCode |= errorstate;
  993. hmmc->State = HAL_MMC_STATE_READY;
  994. hmmc->Context = MMC_CONTEXT_NONE;
  995. return HAL_ERROR;
  996. }
  997. __HAL_MMC_ENABLE_IT(hmmc, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND | SDMMC_FLAG_RXFIFOHF));
  998. return HAL_OK;
  999. }
  1000. else
  1001. {
  1002. return HAL_BUSY;
  1003. }
  1004. }
  1005. /**
  1006. * @brief Writes block(s) to a specified address in a card. The Data transfer
  1007. * is managed in interrupt mode.
  1008. * @note This API should be followed by a check on the card state through
  1009. * HAL_MMC_GetCardState().
  1010. * @note You could also check the IT transfer process through the MMC Tx
  1011. * interrupt event.
  1012. * @param hmmc Pointer to MMC handle
  1013. * @param pData Pointer to the buffer that will contain the data to transmit
  1014. * @param BlockAdd Block Address where data will be written
  1015. * @param NumberOfBlocks Number of blocks to write
  1016. * @retval HAL status
  1017. */
  1018. HAL_StatusTypeDef HAL_MMC_WriteBlocks_IT(MMC_HandleTypeDef *hmmc, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
  1019. {
  1020. SDMMC_DataInitTypeDef config;
  1021. uint32_t errorstate;
  1022. uint32_t add = BlockAdd;
  1023. if(NULL == pData)
  1024. {
  1025. hmmc->ErrorCode |= HAL_MMC_ERROR_PARAM;
  1026. return HAL_ERROR;
  1027. }
  1028. if(hmmc->State == HAL_MMC_STATE_READY)
  1029. {
  1030. hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
  1031. if((add + NumberOfBlocks) > (hmmc->MmcCard.LogBlockNbr))
  1032. {
  1033. hmmc->ErrorCode |= HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
  1034. return HAL_ERROR;
  1035. }
  1036. hmmc->State = HAL_MMC_STATE_BUSY;
  1037. /* Initialize data control register */
  1038. hmmc->Instance->DCTRL = 0U;
  1039. hmmc->pTxBuffPtr = pData;
  1040. hmmc->TxXferSize = MMC_BLOCKSIZE * NumberOfBlocks;
  1041. if ((hmmc->MmcCard.CardType) != MMC_HIGH_CAPACITY_CARD)
  1042. {
  1043. add *= 512U;
  1044. }
  1045. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  1046. /* Configure the MMC DPSM (Data Path State Machine) */
  1047. config.DataTimeOut = SDMMC_DATATIMEOUT;
  1048. config.DataLength = MMC_BLOCKSIZE * NumberOfBlocks;
  1049. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
  1050. config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
  1051. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  1052. config.DPSM = SDMMC_DPSM_DISABLE;
  1053. (void)SDMMC_ConfigData(hmmc->Instance, &config);
  1054. __SDMMC_CMDTRANS_ENABLE( hmmc->Instance);
  1055. #endif
  1056. /* Write Blocks in Polling mode */
  1057. if(NumberOfBlocks > 1U)
  1058. {
  1059. hmmc->Context = (MMC_CONTEXT_WRITE_MULTIPLE_BLOCK| MMC_CONTEXT_IT);
  1060. /* Write Multi Block command */
  1061. errorstate = SDMMC_CmdWriteMultiBlock(hmmc->Instance, add);
  1062. }
  1063. else
  1064. {
  1065. hmmc->Context = (MMC_CONTEXT_WRITE_SINGLE_BLOCK | MMC_CONTEXT_IT);
  1066. /* Write Single Block command */
  1067. errorstate = SDMMC_CmdWriteSingleBlock(hmmc->Instance, add);
  1068. }
  1069. if(errorstate != HAL_MMC_ERROR_NONE)
  1070. {
  1071. /* Clear all the static flags */
  1072. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
  1073. hmmc->ErrorCode |= errorstate;
  1074. hmmc->State = HAL_MMC_STATE_READY;
  1075. hmmc->Context = MMC_CONTEXT_NONE;
  1076. return HAL_ERROR;
  1077. }
  1078. #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  1079. /* Configure the MMC DPSM (Data Path State Machine) */
  1080. config.DataTimeOut = SDMMC_DATATIMEOUT;
  1081. config.DataLength = MMC_BLOCKSIZE * NumberOfBlocks;
  1082. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
  1083. config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
  1084. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  1085. config.DPSM = SDMMC_DPSM_ENABLE;
  1086. (void)SDMMC_ConfigData(hmmc->Instance, &config);
  1087. #endif
  1088. /* Enable transfer interrupts */
  1089. __HAL_MMC_ENABLE_IT(hmmc, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR | SDMMC_IT_DATAEND | SDMMC_FLAG_TXFIFOHE));
  1090. return HAL_OK;
  1091. }
  1092. else
  1093. {
  1094. return HAL_BUSY;
  1095. }
  1096. }
  1097. /**
  1098. * @brief Reads block(s) from a specified address in a card. The Data transfer
  1099. * is managed by DMA mode.
  1100. * @note This API should be followed by a check on the card state through
  1101. * HAL_MMC_GetCardState().
  1102. * @note You could also check the DMA transfer process through the MMC Rx
  1103. * interrupt event.
  1104. * @param hmmc Pointer MMC handle
  1105. * @param pData Pointer to the buffer that will contain the received data
  1106. * @param BlockAdd Block Address from where data is to be read
  1107. * @param NumberOfBlocks Number of blocks to read.
  1108. * @retval HAL status
  1109. */
  1110. HAL_StatusTypeDef HAL_MMC_ReadBlocks_DMA(MMC_HandleTypeDef *hmmc, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
  1111. {
  1112. SDMMC_DataInitTypeDef config;
  1113. uint32_t errorstate;
  1114. uint32_t add = BlockAdd;
  1115. if(NULL == pData)
  1116. {
  1117. hmmc->ErrorCode |= HAL_MMC_ERROR_PARAM;
  1118. return HAL_ERROR;
  1119. }
  1120. if(hmmc->State == HAL_MMC_STATE_READY)
  1121. {
  1122. hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
  1123. if((add + NumberOfBlocks) > (hmmc->MmcCard.LogBlockNbr))
  1124. {
  1125. hmmc->ErrorCode |= HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
  1126. return HAL_ERROR;
  1127. }
  1128. hmmc->State = HAL_MMC_STATE_BUSY;
  1129. /* Initialize data control register */
  1130. hmmc->Instance->DCTRL = 0U;
  1131. #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  1132. /* Set the DMA transfer complete callback */
  1133. hmmc->hdmarx->XferCpltCallback = MMC_DMAReceiveCplt;
  1134. /* Set the DMA error callback */
  1135. hmmc->hdmarx->XferErrorCallback = MMC_DMAError;
  1136. /* Set the DMA Abort callback */
  1137. hmmc->hdmarx->XferAbortCallback = NULL;
  1138. #else
  1139. hmmc->pRxBuffPtr = pData;
  1140. hmmc->RxXferSize = MMC_BLOCKSIZE * NumberOfBlocks;
  1141. #endif
  1142. if ((hmmc->MmcCard.CardType) != MMC_HIGH_CAPACITY_CARD)
  1143. {
  1144. add *= 512U;
  1145. }
  1146. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  1147. /* Configure the MMC DPSM (Data Path State Machine) */
  1148. config.DataTimeOut = SDMMC_DATATIMEOUT;
  1149. config.DataLength = MMC_BLOCKSIZE * NumberOfBlocks;
  1150. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
  1151. config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
  1152. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  1153. config.DPSM = SDMMC_DPSM_DISABLE;
  1154. (void)SDMMC_ConfigData(hmmc->Instance, &config);
  1155. __SDMMC_CMDTRANS_ENABLE( hmmc->Instance);
  1156. hmmc->Instance->IDMABASE0 = (uint32_t) pData ;
  1157. hmmc->Instance->IDMACTRL = SDMMC_ENABLE_IDMA_SINGLE_BUFF;
  1158. #else
  1159. /* Enable the DMA Channel */
  1160. if(HAL_DMA_Start_IT(hmmc->hdmarx, (uint32_t)&hmmc->Instance->FIFO, (uint32_t)pData, (uint32_t)(MMC_BLOCKSIZE * NumberOfBlocks)/4) != HAL_OK)
  1161. {
  1162. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
  1163. hmmc->ErrorCode = HAL_MMC_ERROR_DMA;
  1164. hmmc->State = HAL_MMC_STATE_READY;
  1165. return HAL_ERROR;
  1166. }
  1167. else
  1168. {
  1169. /* Enable MMC DMA transfer */
  1170. __HAL_MMC_DMA_ENABLE(hmmc);
  1171. /* Configure the MMC DPSM (Data Path State Machine) */
  1172. config.DataTimeOut = SDMMC_DATATIMEOUT;
  1173. config.DataLength = MMC_BLOCKSIZE * NumberOfBlocks;
  1174. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
  1175. config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
  1176. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  1177. config.DPSM = SDMMC_DPSM_ENABLE;
  1178. (void)SDMMC_ConfigData(hmmc->Instance, &config);
  1179. #endif
  1180. /* Read Blocks in DMA mode */
  1181. if(NumberOfBlocks > 1U)
  1182. {
  1183. hmmc->Context = (MMC_CONTEXT_READ_MULTIPLE_BLOCK | MMC_CONTEXT_DMA);
  1184. /* Read Multi Block command */
  1185. errorstate = SDMMC_CmdReadMultiBlock(hmmc->Instance, add);
  1186. }
  1187. else
  1188. {
  1189. hmmc->Context = (MMC_CONTEXT_READ_SINGLE_BLOCK | MMC_CONTEXT_DMA);
  1190. /* Read Single Block command */
  1191. errorstate = SDMMC_CmdReadSingleBlock(hmmc->Instance, add);
  1192. }
  1193. if(errorstate != HAL_MMC_ERROR_NONE)
  1194. {
  1195. /* Clear all the static flags */
  1196. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
  1197. hmmc->ErrorCode = errorstate;
  1198. hmmc->State = HAL_MMC_STATE_READY;
  1199. hmmc->Context = MMC_CONTEXT_NONE;
  1200. return HAL_ERROR;
  1201. }
  1202. /* Enable transfer interrupts */
  1203. __HAL_MMC_ENABLE_IT(hmmc, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND));
  1204. return HAL_OK;
  1205. #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  1206. }
  1207. #endif
  1208. }
  1209. else
  1210. {
  1211. return HAL_BUSY;
  1212. }
  1213. }
  1214. /**
  1215. * @brief Writes block(s) to a specified address in a card. The Data transfer
  1216. * is managed by DMA mode.
  1217. * @note This API should be followed by a check on the card state through
  1218. * HAL_MMC_GetCardState().
  1219. * @note You could also check the DMA transfer process through the MMC Tx
  1220. * interrupt event.
  1221. * @param hmmc Pointer to MMC handle
  1222. * @param pData Pointer to the buffer that will contain the data to transmit
  1223. * @param BlockAdd Block Address where data will be written
  1224. * @param NumberOfBlocks Number of blocks to write
  1225. * @retval HAL status
  1226. */
  1227. HAL_StatusTypeDef HAL_MMC_WriteBlocks_DMA(MMC_HandleTypeDef *hmmc, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
  1228. {
  1229. SDMMC_DataInitTypeDef config;
  1230. uint32_t errorstate;
  1231. uint32_t add = BlockAdd;
  1232. if(NULL == pData)
  1233. {
  1234. hmmc->ErrorCode |= HAL_MMC_ERROR_PARAM;
  1235. return HAL_ERROR;
  1236. }
  1237. if(hmmc->State == HAL_MMC_STATE_READY)
  1238. {
  1239. hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
  1240. if((add + NumberOfBlocks) > (hmmc->MmcCard.LogBlockNbr))
  1241. {
  1242. hmmc->ErrorCode |= HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
  1243. return HAL_ERROR;
  1244. }
  1245. hmmc->State = HAL_MMC_STATE_BUSY;
  1246. /* Initialize data control register */
  1247. hmmc->Instance->DCTRL = 0U;
  1248. #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  1249. /* Set the DMA transfer complete callback */
  1250. hmmc->hdmatx->XferCpltCallback = MMC_DMATransmitCplt;
  1251. /* Set the DMA error callback */
  1252. hmmc->hdmatx->XferErrorCallback = MMC_DMAError;
  1253. /* Set the DMA Abort callback */
  1254. hmmc->hdmatx->XferAbortCallback = NULL;
  1255. #else
  1256. hmmc->pTxBuffPtr = pData;
  1257. hmmc->TxXferSize = MMC_BLOCKSIZE * NumberOfBlocks;
  1258. #endif
  1259. if ((hmmc->MmcCard.CardType) != MMC_HIGH_CAPACITY_CARD)
  1260. {
  1261. add *= 512U;
  1262. }
  1263. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  1264. /* Configure the MMC DPSM (Data Path State Machine) */
  1265. config.DataTimeOut = SDMMC_DATATIMEOUT;
  1266. config.DataLength = MMC_BLOCKSIZE * NumberOfBlocks;
  1267. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
  1268. config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
  1269. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  1270. config.DPSM = SDMMC_DPSM_DISABLE;
  1271. (void)SDMMC_ConfigData(hmmc->Instance, &config);
  1272. __SDMMC_CMDTRANS_ENABLE( hmmc->Instance);
  1273. hmmc->Instance->IDMABASE0 = (uint32_t) pData ;
  1274. hmmc->Instance->IDMACTRL = SDMMC_ENABLE_IDMA_SINGLE_BUFF;
  1275. #endif
  1276. /* Write Blocks in Polling mode */
  1277. if(NumberOfBlocks > 1U)
  1278. {
  1279. hmmc->Context = (MMC_CONTEXT_WRITE_MULTIPLE_BLOCK | MMC_CONTEXT_DMA);
  1280. /* Write Multi Block command */
  1281. errorstate = SDMMC_CmdWriteMultiBlock(hmmc->Instance, add);
  1282. }
  1283. else
  1284. {
  1285. hmmc->Context = (MMC_CONTEXT_WRITE_SINGLE_BLOCK | MMC_CONTEXT_DMA);
  1286. /* Write Single Block command */
  1287. errorstate = SDMMC_CmdWriteSingleBlock(hmmc->Instance, add);
  1288. }
  1289. if(errorstate != HAL_MMC_ERROR_NONE)
  1290. {
  1291. /* Clear all the static flags */
  1292. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
  1293. hmmc->ErrorCode |= errorstate;
  1294. hmmc->State = HAL_MMC_STATE_READY;
  1295. hmmc->Context = MMC_CONTEXT_NONE;
  1296. return HAL_ERROR;
  1297. }
  1298. #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  1299. /* Enable SDMMC DMA transfer */
  1300. __HAL_MMC_DMA_ENABLE(hmmc);
  1301. /* Enable the DMA Channel */
  1302. if(HAL_DMA_Start_IT(hmmc->hdmatx, (uint32_t)pData, (uint32_t)&hmmc->Instance->FIFO, (uint32_t)(MMC_BLOCKSIZE * NumberOfBlocks)/4) != HAL_OK)
  1303. {
  1304. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
  1305. hmmc->ErrorCode |= HAL_MMC_ERROR_DMA;
  1306. hmmc->State = HAL_MMC_STATE_READY;
  1307. hmmc->Context = MMC_CONTEXT_NONE;
  1308. return HAL_ERROR;
  1309. }
  1310. else
  1311. {
  1312. /* Configure the MMC DPSM (Data Path State Machine) */
  1313. config.DataTimeOut = SDMMC_DATATIMEOUT;
  1314. config.DataLength = MMC_BLOCKSIZE * NumberOfBlocks;
  1315. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
  1316. config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
  1317. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  1318. config.DPSM = SDMMC_DPSM_ENABLE;
  1319. (void)SDMMC_ConfigData(hmmc->Instance, &config);
  1320. /* Enable MMC Error interrupts */
  1321. __HAL_MMC_ENABLE_IT(hmmc, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR));
  1322. return HAL_OK;
  1323. }
  1324. #else
  1325. /* Enable MMC Error interrupts */
  1326. __HAL_MMC_ENABLE_IT(hmmc, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR | SDMMC_IT_DATAEND));
  1327. return HAL_OK;
  1328. #endif
  1329. }
  1330. else
  1331. {
  1332. return HAL_BUSY;
  1333. }
  1334. }
  1335. /**
  1336. * @brief Erases the specified memory area of the given MMC card.
  1337. * @note This API should be followed by a check on the card state through
  1338. * HAL_MMC_GetCardState().
  1339. * @param hmmc Pointer to MMC handle
  1340. * @param BlockStartAdd Start Block address
  1341. * @param BlockEndAdd End Block address
  1342. * @retval HAL status
  1343. */
  1344. HAL_StatusTypeDef HAL_MMC_Erase(MMC_HandleTypeDef *hmmc, uint32_t BlockStartAdd, uint32_t BlockEndAdd)
  1345. {
  1346. uint32_t errorstate;
  1347. uint32_t start_add = BlockStartAdd;
  1348. uint32_t end_add = BlockEndAdd;
  1349. if(hmmc->State == HAL_MMC_STATE_READY)
  1350. {
  1351. hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
  1352. if(end_add < start_add)
  1353. {
  1354. hmmc->ErrorCode |= HAL_MMC_ERROR_PARAM;
  1355. return HAL_ERROR;
  1356. }
  1357. if(end_add > (hmmc->MmcCard.LogBlockNbr))
  1358. {
  1359. hmmc->ErrorCode |= HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
  1360. return HAL_ERROR;
  1361. }
  1362. hmmc->State = HAL_MMC_STATE_BUSY;
  1363. /* Check if the card command class supports erase command */
  1364. if(((hmmc->MmcCard.Class) & SDMMC_CCCC_ERASE) == 0U)
  1365. {
  1366. /* Clear all the static flags */
  1367. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
  1368. hmmc->ErrorCode |= HAL_MMC_ERROR_REQUEST_NOT_APPLICABLE;
  1369. hmmc->State = HAL_MMC_STATE_READY;
  1370. return HAL_ERROR;
  1371. }
  1372. if((SDMMC_GetResponse(hmmc->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
  1373. {
  1374. /* Clear all the static flags */
  1375. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
  1376. hmmc->ErrorCode |= HAL_MMC_ERROR_LOCK_UNLOCK_FAILED;
  1377. hmmc->State = HAL_MMC_STATE_READY;
  1378. return HAL_ERROR;
  1379. }
  1380. if ((hmmc->MmcCard.CardType) != MMC_HIGH_CAPACITY_CARD)
  1381. {
  1382. start_add *= 512U;
  1383. end_add *= 512U;
  1384. }
  1385. /* Send CMD35 MMC_ERASE_GRP_START with argument as addr */
  1386. errorstate = SDMMC_CmdEraseStartAdd(hmmc->Instance, start_add);
  1387. if(errorstate != HAL_MMC_ERROR_NONE)
  1388. {
  1389. /* Clear all the static flags */
  1390. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
  1391. hmmc->ErrorCode |= errorstate;
  1392. hmmc->State = HAL_MMC_STATE_READY;
  1393. return HAL_ERROR;
  1394. }
  1395. /* Send CMD36 MMC_ERASE_GRP_END with argument as addr */
  1396. errorstate = SDMMC_CmdEraseEndAdd(hmmc->Instance, end_add);
  1397. if(errorstate != HAL_MMC_ERROR_NONE)
  1398. {
  1399. /* Clear all the static flags */
  1400. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
  1401. hmmc->ErrorCode |= errorstate;
  1402. hmmc->State = HAL_MMC_STATE_READY;
  1403. return HAL_ERROR;
  1404. }
  1405. /* Send CMD38 ERASE */
  1406. errorstate = SDMMC_CmdErase(hmmc->Instance, 0UL);
  1407. if(errorstate != HAL_MMC_ERROR_NONE)
  1408. {
  1409. /* Clear all the static flags */
  1410. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
  1411. hmmc->ErrorCode |= errorstate;
  1412. hmmc->State = HAL_MMC_STATE_READY;
  1413. return HAL_ERROR;
  1414. }
  1415. hmmc->State = HAL_MMC_STATE_READY;
  1416. return HAL_OK;
  1417. }
  1418. else
  1419. {
  1420. return HAL_BUSY;
  1421. }
  1422. }
  1423. /**
  1424. * @brief This function handles MMC card interrupt request.
  1425. * @param hmmc Pointer to MMC handle
  1426. * @retval None
  1427. */
  1428. void HAL_MMC_IRQHandler(MMC_HandleTypeDef *hmmc)
  1429. {
  1430. uint32_t errorstate;
  1431. uint32_t context = hmmc->Context;
  1432. /* Check for SDMMC interrupt flags */
  1433. if((__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_RXFIFOHF) != RESET) && ((context & MMC_CONTEXT_IT) != 0U))
  1434. {
  1435. MMC_Read_IT(hmmc);
  1436. }
  1437. else if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_DATAEND) != RESET)
  1438. {
  1439. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_FLAG_DATAEND);
  1440. __HAL_MMC_DISABLE_IT(hmmc, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT |\
  1441. SDMMC_IT_TXUNDERR | SDMMC_IT_RXOVERR | SDMMC_IT_TXFIFOHE |\
  1442. SDMMC_IT_RXFIFOHF);
  1443. #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  1444. hmmc->Instance->DCTRL &= ~(SDMMC_DCTRL_DTEN);
  1445. #else
  1446. __HAL_MMC_DISABLE_IT(hmmc, SDMMC_IT_IDMABTC);
  1447. __SDMMC_CMDTRANS_DISABLE( hmmc->Instance);
  1448. #endif
  1449. if((context & MMC_CONTEXT_DMA) != 0U)
  1450. {
  1451. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  1452. hmmc->Instance->DLEN = 0;
  1453. hmmc->Instance->DCTRL = 0;
  1454. hmmc->Instance->IDMACTRL = SDMMC_DISABLE_IDMA ;
  1455. /* Stop Transfer for Write Multi blocks or Read Multi blocks */
  1456. if(((context & MMC_CONTEXT_READ_MULTIPLE_BLOCK) != 0U) || ((context & MMC_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
  1457. {
  1458. errorstate = SDMMC_CmdStopTransfer(hmmc->Instance);
  1459. if(errorstate != HAL_MMC_ERROR_NONE)
  1460. {
  1461. hmmc->ErrorCode |= errorstate;
  1462. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  1463. hmmc->ErrorCallback(hmmc);
  1464. #else
  1465. HAL_MMC_ErrorCallback(hmmc);
  1466. #endif
  1467. }
  1468. }
  1469. /* Clear all the static flags */
  1470. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_DATA_FLAGS);
  1471. hmmc->State = HAL_MMC_STATE_READY;
  1472. hmmc->Context = MMC_CONTEXT_NONE;
  1473. if(((context & MMC_CONTEXT_WRITE_SINGLE_BLOCK) != 0U) || ((context & MMC_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
  1474. {
  1475. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  1476. hmmc->TxCpltCallback(hmmc);
  1477. #else
  1478. HAL_MMC_TxCpltCallback(hmmc);
  1479. #endif
  1480. }
  1481. if(((context & MMC_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & MMC_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
  1482. {
  1483. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  1484. hmmc->RxCpltCallback(hmmc);
  1485. #else
  1486. HAL_MMC_RxCpltCallback(hmmc);
  1487. #endif
  1488. }
  1489. #else
  1490. if((context & MMC_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U)
  1491. {
  1492. errorstate = SDMMC_CmdStopTransfer(hmmc->Instance);
  1493. if(errorstate != HAL_MMC_ERROR_NONE)
  1494. {
  1495. hmmc->ErrorCode |= errorstate;
  1496. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  1497. hmmc->ErrorCallback(hmmc);
  1498. #else
  1499. HAL_MMC_ErrorCallback(hmmc);
  1500. #endif
  1501. }
  1502. }
  1503. /* Disable the DMA transfer for transmit request by setting the DMAEN bit
  1504. in the MMC DCTRL register */
  1505. hmmc->Instance->DCTRL &= (uint32_t)~((uint32_t)SDMMC_DCTRL_DMAEN);
  1506. /* Clear all the static flags */
  1507. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_DATA_FLAGS);
  1508. hmmc->State = HAL_MMC_STATE_READY;
  1509. hmmc->Context = MMC_CONTEXT_NONE;
  1510. if(((context & MMC_CONTEXT_READ_SINGLE_BLOCK) == 0U) && ((context & MMC_CONTEXT_READ_MULTIPLE_BLOCK) == 0U))
  1511. {
  1512. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  1513. hmmc->TxCpltCallback(hmmc);
  1514. #else
  1515. HAL_MMC_TxCpltCallback(hmmc);
  1516. #endif
  1517. }
  1518. #endif
  1519. }
  1520. else if((context & MMC_CONTEXT_IT) != 0U)
  1521. {
  1522. /* Stop Transfer for Write Multi blocks or Read Multi blocks */
  1523. if(((context & MMC_CONTEXT_READ_MULTIPLE_BLOCK) != 0U) || ((context & MMC_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
  1524. {
  1525. errorstate = SDMMC_CmdStopTransfer(hmmc->Instance);
  1526. if(errorstate != HAL_MMC_ERROR_NONE)
  1527. {
  1528. hmmc->ErrorCode |= errorstate;
  1529. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  1530. hmmc->ErrorCallback(hmmc);
  1531. #else
  1532. HAL_MMC_ErrorCallback(hmmc);
  1533. #endif
  1534. }
  1535. }
  1536. /* Clear all the static flags */
  1537. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_DATA_FLAGS);
  1538. hmmc->State = HAL_MMC_STATE_READY;
  1539. hmmc->Context = MMC_CONTEXT_NONE;
  1540. if(((context & MMC_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & MMC_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
  1541. {
  1542. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  1543. hmmc->RxCpltCallback(hmmc);
  1544. #else
  1545. HAL_MMC_RxCpltCallback(hmmc);
  1546. #endif
  1547. }
  1548. else
  1549. {
  1550. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  1551. hmmc->TxCpltCallback(hmmc);
  1552. #else
  1553. HAL_MMC_TxCpltCallback(hmmc);
  1554. #endif
  1555. }
  1556. }
  1557. else
  1558. {
  1559. /* Nothing to do */
  1560. }
  1561. }
  1562. else if((__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_TXFIFOHE) != RESET) && ((context & MMC_CONTEXT_IT) != 0U))
  1563. {
  1564. MMC_Write_IT(hmmc);
  1565. }
  1566. else if (__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_DCRCFAIL| SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_RXOVERR | SDMMC_FLAG_TXUNDERR) != RESET)
  1567. {
  1568. /* Set Error code */
  1569. if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_IT_DCRCFAIL) != RESET)
  1570. {
  1571. hmmc->ErrorCode |= HAL_MMC_ERROR_DATA_CRC_FAIL;
  1572. }
  1573. if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_IT_DTIMEOUT) != RESET)
  1574. {
  1575. hmmc->ErrorCode |= HAL_MMC_ERROR_DATA_TIMEOUT;
  1576. }
  1577. if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_IT_RXOVERR) != RESET)
  1578. {
  1579. hmmc->ErrorCode |= HAL_MMC_ERROR_RX_OVERRUN;
  1580. }
  1581. if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_IT_TXUNDERR) != RESET)
  1582. {
  1583. hmmc->ErrorCode |= HAL_MMC_ERROR_TX_UNDERRUN;
  1584. }
  1585. /* Clear All flags */
  1586. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_DATA_FLAGS);
  1587. /* Disable all interrupts */
  1588. __HAL_MMC_DISABLE_IT(hmmc, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT|\
  1589. SDMMC_IT_TXUNDERR| SDMMC_IT_RXOVERR);
  1590. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  1591. __SDMMC_CMDTRANS_DISABLE( hmmc->Instance);
  1592. hmmc->Instance->DCTRL |= SDMMC_DCTRL_FIFORST;
  1593. hmmc->Instance->CMD |= SDMMC_CMD_CMDSTOP;
  1594. #endif
  1595. hmmc->ErrorCode |= SDMMC_CmdStopTransfer(hmmc->Instance);
  1596. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  1597. hmmc->Instance->CMD &= ~(SDMMC_CMD_CMDSTOP);
  1598. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_FLAG_DABORT);
  1599. #endif
  1600. if((context & MMC_CONTEXT_IT) != 0U)
  1601. {
  1602. /* Set the MMC state to ready to be able to start again the process */
  1603. hmmc->State = HAL_MMC_STATE_READY;
  1604. hmmc->Context = MMC_CONTEXT_NONE;
  1605. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  1606. hmmc->ErrorCallback(hmmc);
  1607. #else
  1608. HAL_MMC_ErrorCallback(hmmc);
  1609. #endif /* USE_HAL_MMC_REGISTER_CALLBACKS */
  1610. }
  1611. else if((context & MMC_CONTEXT_DMA) != 0U)
  1612. {
  1613. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  1614. if(hmmc->ErrorCode != HAL_MMC_ERROR_NONE)
  1615. {
  1616. /* Disable Internal DMA */
  1617. __HAL_MMC_DISABLE_IT(hmmc, SDMMC_IT_IDMABTC);
  1618. hmmc->Instance->IDMACTRL = SDMMC_DISABLE_IDMA;
  1619. /* Set the MMC state to ready to be able to start again the process */
  1620. hmmc->State = HAL_MMC_STATE_READY;
  1621. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  1622. hmmc->ErrorCallback(hmmc);
  1623. #else
  1624. HAL_MMC_ErrorCallback(hmmc);
  1625. #endif /* USE_HAL_MMC_REGISTER_CALLBACKS */
  1626. }
  1627. #else
  1628. /* Abort the MMC DMA Streams */
  1629. if(hmmc->hdmatx != NULL)
  1630. {
  1631. /* Set the DMA Tx abort callback */
  1632. hmmc->hdmatx->XferAbortCallback = MMC_DMATxAbort;
  1633. /* Abort DMA in IT mode */
  1634. if(HAL_DMA_Abort_IT(hmmc->hdmatx) != HAL_OK)
  1635. {
  1636. MMC_DMATxAbort(hmmc->hdmatx);
  1637. }
  1638. }
  1639. else if(hmmc->hdmarx != NULL)
  1640. {
  1641. /* Set the DMA Rx abort callback */
  1642. hmmc->hdmarx->XferAbortCallback = MMC_DMARxAbort;
  1643. /* Abort DMA in IT mode */
  1644. if(HAL_DMA_Abort_IT(hmmc->hdmarx) != HAL_OK)
  1645. {
  1646. MMC_DMARxAbort(hmmc->hdmarx);
  1647. }
  1648. }
  1649. else
  1650. {
  1651. hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
  1652. hmmc->State = HAL_MMC_STATE_READY;
  1653. hmmc->Context = MMC_CONTEXT_NONE;
  1654. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  1655. hmmc->AbortCpltCallback(hmmc);
  1656. #else
  1657. HAL_MMC_AbortCallback(hmmc);
  1658. #endif
  1659. }
  1660. #endif
  1661. }
  1662. else
  1663. {
  1664. /* Nothing to do */
  1665. }
  1666. }
  1667. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  1668. else if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_IDMABTC) != RESET)
  1669. {
  1670. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_IT_IDMABTC);
  1671. if(READ_BIT(hmmc->Instance->IDMACTRL, SDMMC_IDMA_IDMABACT) == 0U)
  1672. {
  1673. /* Current buffer is buffer0, Transfer complete for buffer1 */
  1674. if((context & MMC_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U)
  1675. {
  1676. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  1677. hmmc->Write_DMADblBuf1CpltCallback(hmmc);
  1678. #else
  1679. HAL_MMCEx_Write_DMADoubleBuffer1CpltCallback(hmmc);
  1680. #endif
  1681. }
  1682. else /* MMC_CONTEXT_READ_MULTIPLE_BLOCK */
  1683. {
  1684. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  1685. hmmc->Read_DMADblBuf1CpltCallback(hmmc);
  1686. #else
  1687. HAL_MMCEx_Read_DMADoubleBuffer1CpltCallback(hmmc);
  1688. #endif
  1689. }
  1690. }
  1691. else /* MMC_DMA_BUFFER1 */
  1692. {
  1693. /* Current buffer is buffer1, Transfer complete for buffer0 */
  1694. if((context & MMC_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U)
  1695. {
  1696. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  1697. hmmc->Write_DMADblBuf0CpltCallback(hmmc);
  1698. #else
  1699. HAL_MMCEx_Write_DMADoubleBuffer0CpltCallback(hmmc);
  1700. #endif
  1701. }
  1702. else /* MMC_CONTEXT_READ_MULTIPLE_BLOCK */
  1703. {
  1704. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  1705. hmmc->Read_DMADblBuf0CpltCallback(hmmc);
  1706. #else
  1707. HAL_MMCEx_Read_DMADoubleBuffer0CpltCallback(hmmc);
  1708. #endif
  1709. }
  1710. }
  1711. }
  1712. #endif
  1713. else
  1714. {
  1715. /* Nothing to do */
  1716. }
  1717. }
  1718. /**
  1719. * @brief return the MMC state
  1720. * @param hmmc Pointer to mmc handle
  1721. * @retval HAL state
  1722. */
  1723. HAL_MMC_StateTypeDef HAL_MMC_GetState(MMC_HandleTypeDef *hmmc)
  1724. {
  1725. return hmmc->State;
  1726. }
  1727. /**
  1728. * @brief Return the MMC error code
  1729. * @param hmmc : Pointer to a MMC_HandleTypeDef structure that contains
  1730. * the configuration information.
  1731. * @retval MMC Error Code
  1732. */
  1733. uint32_t HAL_MMC_GetError(MMC_HandleTypeDef *hmmc)
  1734. {
  1735. return hmmc->ErrorCode;
  1736. }
  1737. /**
  1738. * @brief Tx Transfer completed callbacks
  1739. * @param hmmc Pointer to MMC handle
  1740. * @retval None
  1741. */
  1742. __weak void HAL_MMC_TxCpltCallback(MMC_HandleTypeDef *hmmc)
  1743. {
  1744. /* Prevent unused argument(s) compilation warning */
  1745. UNUSED(hmmc);
  1746. /* NOTE : This function should not be modified, when the callback is needed,
  1747. the HAL_MMC_TxCpltCallback can be implemented in the user file
  1748. */
  1749. }
  1750. /**
  1751. * @brief Rx Transfer completed callbacks
  1752. * @param hmmc Pointer MMC handle
  1753. * @retval None
  1754. */
  1755. __weak void HAL_MMC_RxCpltCallback(MMC_HandleTypeDef *hmmc)
  1756. {
  1757. /* Prevent unused argument(s) compilation warning */
  1758. UNUSED(hmmc);
  1759. /* NOTE : This function should not be modified, when the callback is needed,
  1760. the HAL_MMC_RxCpltCallback can be implemented in the user file
  1761. */
  1762. }
  1763. /**
  1764. * @brief MMC error callbacks
  1765. * @param hmmc Pointer MMC handle
  1766. * @retval None
  1767. */
  1768. __weak void HAL_MMC_ErrorCallback(MMC_HandleTypeDef *hmmc)
  1769. {
  1770. /* Prevent unused argument(s) compilation warning */
  1771. UNUSED(hmmc);
  1772. /* NOTE : This function should not be modified, when the callback is needed,
  1773. the HAL_MMC_ErrorCallback can be implemented in the user file
  1774. */
  1775. }
  1776. /**
  1777. * @brief MMC Abort callbacks
  1778. * @param hmmc Pointer MMC handle
  1779. * @retval None
  1780. */
  1781. __weak void HAL_MMC_AbortCallback(MMC_HandleTypeDef *hmmc)
  1782. {
  1783. /* Prevent unused argument(s) compilation warning */
  1784. UNUSED(hmmc);
  1785. /* NOTE : This function should not be modified, when the callback is needed,
  1786. the HAL_MMC_AbortCallback can be implemented in the user file
  1787. */
  1788. }
  1789. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  1790. /**
  1791. * @brief Register a User MMC Callback
  1792. * To be used instead of the weak (surcharged) predefined callback
  1793. * @param hmmc : MMC handle
  1794. * @param CallbackId : ID of the callback to be registered
  1795. * This parameter can be one of the following values:
  1796. * @arg @ref HAL_MMC_TX_CPLT_CB_ID MMC Tx Complete Callback ID
  1797. * @arg @ref HAL_MMC_RX_CPLT_CB_ID MMC Rx Complete Callback ID
  1798. * @arg @ref HAL_MMC_ERROR_CB_ID MMC Error Callback ID
  1799. * @arg @ref HAL_MMC_ABORT_CB_ID MMC Abort Callback ID
  1800. * @arg @ref HAL_MMC_READ_DMA_DBL_BUF0_CPLT_CB_ID MMC DMA Rx Double buffer 0 Callback ID
  1801. * @arg @ref HAL_MMC_READ_DMA_DBL_BUF1_CPLT_CB_ID MMC DMA Rx Double buffer 1 Callback ID
  1802. * @arg @ref HAL_MMC_WRITE_DMA_DBL_BUF0_CPLT_CB_ID MMC DMA Tx Double buffer 0 Callback ID
  1803. * @arg @ref HAL_MMC_WRITE_DMA_DBL_BUF1_CPLT_CB_ID MMC DMA Tx Double buffer 1 Callback ID
  1804. * @arg @ref HAL_MMC_MSP_INIT_CB_ID MMC MspInit Callback ID
  1805. * @arg @ref HAL_MMC_MSP_DEINIT_CB_ID MMC MspDeInit Callback ID
  1806. * @param pCallback : pointer to the Callback function
  1807. * @retval status
  1808. */
  1809. HAL_StatusTypeDef HAL_MMC_RegisterCallback(MMC_HandleTypeDef *hmmc, HAL_MMC_CallbackIDTypeDef CallbackId, pMMC_CallbackTypeDef pCallback)
  1810. {
  1811. HAL_StatusTypeDef status = HAL_OK;
  1812. if(pCallback == NULL)
  1813. {
  1814. /* Update the error code */
  1815. hmmc->ErrorCode |= HAL_MMC_ERROR_INVALID_CALLBACK;
  1816. return HAL_ERROR;
  1817. }
  1818. /* Process locked */
  1819. __HAL_LOCK(hmmc);
  1820. if(hmmc->State == HAL_MMC_STATE_READY)
  1821. {
  1822. switch (CallbackId)
  1823. {
  1824. case HAL_MMC_TX_CPLT_CB_ID :
  1825. hmmc->TxCpltCallback = pCallback;
  1826. break;
  1827. case HAL_MMC_RX_CPLT_CB_ID :
  1828. hmmc->RxCpltCallback = pCallback;
  1829. break;
  1830. case HAL_MMC_ERROR_CB_ID :
  1831. hmmc->ErrorCallback = pCallback;
  1832. break;
  1833. case HAL_MMC_ABORT_CB_ID :
  1834. hmmc->AbortCpltCallback = pCallback;
  1835. break;
  1836. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  1837. case HAL_MMC_READ_DMA_DBL_BUF0_CPLT_CB_ID :
  1838. hmmc->Read_DMADblBuf0CpltCallback = pCallback;
  1839. break;
  1840. case HAL_MMC_READ_DMA_DBL_BUF1_CPLT_CB_ID :
  1841. hmmc->Read_DMADblBuf1CpltCallback = pCallback;
  1842. break;
  1843. case HAL_MMC_WRITE_DMA_DBL_BUF0_CPLT_CB_ID :
  1844. hmmc->Write_DMADblBuf0CpltCallback = pCallback;
  1845. break;
  1846. case HAL_MMC_WRITE_DMA_DBL_BUF1_CPLT_CB_ID :
  1847. hmmc->Write_DMADblBuf1CpltCallback = pCallback;
  1848. break;
  1849. #endif
  1850. case HAL_MMC_MSP_INIT_CB_ID :
  1851. hmmc->MspInitCallback = pCallback;
  1852. break;
  1853. case HAL_MMC_MSP_DEINIT_CB_ID :
  1854. hmmc->MspDeInitCallback = pCallback;
  1855. break;
  1856. default :
  1857. /* Update the error code */
  1858. hmmc->ErrorCode |= HAL_MMC_ERROR_INVALID_CALLBACK;
  1859. /* update return status */
  1860. status = HAL_ERROR;
  1861. break;
  1862. }
  1863. }
  1864. else if (hmmc->State == HAL_MMC_STATE_RESET)
  1865. {
  1866. switch (CallbackId)
  1867. {
  1868. case HAL_MMC_MSP_INIT_CB_ID :
  1869. hmmc->MspInitCallback = pCallback;
  1870. break;
  1871. case HAL_MMC_MSP_DEINIT_CB_ID :
  1872. hmmc->MspDeInitCallback = pCallback;
  1873. break;
  1874. default :
  1875. /* Update the error code */
  1876. hmmc->ErrorCode |= HAL_MMC_ERROR_INVALID_CALLBACK;
  1877. /* update return status */
  1878. status = HAL_ERROR;
  1879. break;
  1880. }
  1881. }
  1882. else
  1883. {
  1884. /* Update the error code */
  1885. hmmc->ErrorCode |= HAL_MMC_ERROR_INVALID_CALLBACK;
  1886. /* update return status */
  1887. status = HAL_ERROR;
  1888. }
  1889. /* Release Lock */
  1890. __HAL_UNLOCK(hmmc);
  1891. return status;
  1892. }
  1893. /**
  1894. * @brief Unregister a User MMC Callback
  1895. * MMC Callback is redirected to the weak (surcharged) predefined callback
  1896. * @param hmmc : MMC handle
  1897. * @param CallbackId : ID of the callback to be unregistered
  1898. * This parameter can be one of the following values:
  1899. * @arg @ref HAL_MMC_TX_CPLT_CB_ID MMC Tx Complete Callback ID
  1900. * @arg @ref HAL_MMC_RX_CPLT_CB_ID MMC Rx Complete Callback ID
  1901. * @arg @ref HAL_MMC_ERROR_CB_ID MMC Error Callback ID
  1902. * @arg @ref HAL_MMC_ABORT_CB_ID MMC Abort Callback ID
  1903. * @arg @ref HAL_MMC_READ_DMA_DBL_BUF0_CPLT_CB_ID MMC DMA Rx Double buffer 0 Callback ID
  1904. * @arg @ref HAL_MMC_READ_DMA_DBL_BUF1_CPLT_CB_ID MMC DMA Rx Double buffer 1 Callback ID
  1905. * @arg @ref HAL_MMC_WRITE_DMA_DBL_BUF0_CPLT_CB_ID MMC DMA Tx Double buffer 0 Callback ID
  1906. * @arg @ref HAL_MMC_WRITE_DMA_DBL_BUF1_CPLT_CB_ID MMC DMA Tx Double buffer 1 Callback ID
  1907. * @arg @ref HAL_MMC_MSP_INIT_CB_ID MMC MspInit Callback ID
  1908. * @arg @ref HAL_MMC_MSP_DEINIT_CB_ID MMC MspDeInit Callback ID
  1909. * @retval status
  1910. */
  1911. HAL_StatusTypeDef HAL_MMC_UnRegisterCallback(MMC_HandleTypeDef *hmmc, HAL_MMC_CallbackIDTypeDef CallbackId)
  1912. {
  1913. HAL_StatusTypeDef status = HAL_OK;
  1914. /* Process locked */
  1915. __HAL_LOCK(hmmc);
  1916. if(hmmc->State == HAL_MMC_STATE_READY)
  1917. {
  1918. switch (CallbackId)
  1919. {
  1920. case HAL_MMC_TX_CPLT_CB_ID :
  1921. hmmc->TxCpltCallback = HAL_MMC_TxCpltCallback;
  1922. break;
  1923. case HAL_MMC_RX_CPLT_CB_ID :
  1924. hmmc->RxCpltCallback = HAL_MMC_RxCpltCallback;
  1925. break;
  1926. case HAL_MMC_ERROR_CB_ID :
  1927. hmmc->ErrorCallback = HAL_MMC_ErrorCallback;
  1928. break;
  1929. case HAL_MMC_ABORT_CB_ID :
  1930. hmmc->AbortCpltCallback = HAL_MMC_AbortCallback;
  1931. break;
  1932. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  1933. case HAL_MMC_READ_DMA_DBL_BUF0_CPLT_CB_ID :
  1934. hmmc->Read_DMADblBuf0CpltCallback = HAL_MMCEx_Read_DMADoubleBuffer0CpltCallback;
  1935. break;
  1936. case HAL_MMC_READ_DMA_DBL_BUF1_CPLT_CB_ID :
  1937. hmmc->Read_DMADblBuf1CpltCallback = HAL_MMCEx_Read_DMADoubleBuffer1CpltCallback;
  1938. break;
  1939. case HAL_MMC_WRITE_DMA_DBL_BUF0_CPLT_CB_ID :
  1940. hmmc->Write_DMADblBuf0CpltCallback = HAL_MMCEx_Write_DMADoubleBuffer0CpltCallback;
  1941. break;
  1942. case HAL_MMC_WRITE_DMA_DBL_BUF1_CPLT_CB_ID :
  1943. hmmc->Write_DMADblBuf1CpltCallback = HAL_MMCEx_Write_DMADoubleBuffer1CpltCallback;
  1944. break;
  1945. #endif
  1946. case HAL_MMC_MSP_INIT_CB_ID :
  1947. hmmc->MspInitCallback = HAL_MMC_MspInit;
  1948. break;
  1949. case HAL_MMC_MSP_DEINIT_CB_ID :
  1950. hmmc->MspDeInitCallback = HAL_MMC_MspDeInit;
  1951. break;
  1952. default :
  1953. /* Update the error code */
  1954. hmmc->ErrorCode |= HAL_MMC_ERROR_INVALID_CALLBACK;
  1955. /* update return status */
  1956. status = HAL_ERROR;
  1957. break;
  1958. }
  1959. }
  1960. else if (hmmc->State == HAL_MMC_STATE_RESET)
  1961. {
  1962. switch (CallbackId)
  1963. {
  1964. case HAL_MMC_MSP_INIT_CB_ID :
  1965. hmmc->MspInitCallback = HAL_MMC_MspInit;
  1966. break;
  1967. case HAL_MMC_MSP_DEINIT_CB_ID :
  1968. hmmc->MspDeInitCallback = HAL_MMC_MspDeInit;
  1969. break;
  1970. default :
  1971. /* Update the error code */
  1972. hmmc->ErrorCode |= HAL_MMC_ERROR_INVALID_CALLBACK;
  1973. /* update return status */
  1974. status = HAL_ERROR;
  1975. break;
  1976. }
  1977. }
  1978. else
  1979. {
  1980. /* Update the error code */
  1981. hmmc->ErrorCode |= HAL_MMC_ERROR_INVALID_CALLBACK;
  1982. /* update return status */
  1983. status = HAL_ERROR;
  1984. }
  1985. /* Release Lock */
  1986. __HAL_UNLOCK(hmmc);
  1987. return status;
  1988. }
  1989. #endif
  1990. /**
  1991. * @}
  1992. */
  1993. /** @addtogroup MMC_Exported_Functions_Group3
  1994. * @brief management functions
  1995. *
  1996. @verbatim
  1997. ==============================================================================
  1998. ##### Peripheral Control functions #####
  1999. ==============================================================================
  2000. [..]
  2001. This subsection provides a set of functions allowing to control the MMC card
  2002. operations and get the related information
  2003. @endverbatim
  2004. * @{
  2005. */
  2006. /**
  2007. * @brief Returns information the information of the card which are stored on
  2008. * the CID register.
  2009. * @param hmmc Pointer to MMC handle
  2010. * @param pCID Pointer to a HAL_MMC_CIDTypedef structure that
  2011. * contains all CID register parameters
  2012. * @retval HAL status
  2013. */
  2014. HAL_StatusTypeDef HAL_MMC_GetCardCID(MMC_HandleTypeDef *hmmc, HAL_MMC_CardCIDTypeDef *pCID)
  2015. {
  2016. pCID->ManufacturerID = (uint8_t)((hmmc->CID[0] & 0xFF000000U) >> 24U);
  2017. pCID->OEM_AppliID = (uint16_t)((hmmc->CID[0] & 0x00FFFF00U) >> 8U);
  2018. pCID->ProdName1 = (((hmmc->CID[0] & 0x000000FFU) << 24U) | ((hmmc->CID[1] & 0xFFFFFF00U) >> 8U));
  2019. pCID->ProdName2 = (uint8_t)(hmmc->CID[1] & 0x000000FFU);
  2020. pCID->ProdRev = (uint8_t)((hmmc->CID[2] & 0xFF000000U) >> 24U);
  2021. pCID->ProdSN = (((hmmc->CID[2] & 0x00FFFFFFU) << 8U) | ((hmmc->CID[3] & 0xFF000000U) >> 24U));
  2022. pCID->Reserved1 = (uint8_t)((hmmc->CID[3] & 0x00F00000U) >> 20U);
  2023. pCID->ManufactDate = (uint16_t)((hmmc->CID[3] & 0x000FFF00U) >> 8U);
  2024. pCID->CID_CRC = (uint8_t)((hmmc->CID[3] & 0x000000FEU) >> 1U);
  2025. pCID->Reserved2 = 1U;
  2026. return HAL_OK;
  2027. }
  2028. /**
  2029. * @brief Returns information the information of the card which are stored on
  2030. * the CSD register.
  2031. * @param hmmc Pointer to MMC handle
  2032. * @param pCSD Pointer to a HAL_MMC_CardCSDTypeDef structure that
  2033. * contains all CSD register parameters
  2034. * @retval HAL status
  2035. */
  2036. HAL_StatusTypeDef HAL_MMC_GetCardCSD(MMC_HandleTypeDef *hmmc, HAL_MMC_CardCSDTypeDef *pCSD)
  2037. {
  2038. uint32_t block_nbr = 0;
  2039. pCSD->CSDStruct = (uint8_t)((hmmc->CSD[0] & 0xC0000000U) >> 30U);
  2040. pCSD->SysSpecVersion = (uint8_t)((hmmc->CSD[0] & 0x3C000000U) >> 26U);
  2041. pCSD->Reserved1 = (uint8_t)((hmmc->CSD[0] & 0x03000000U) >> 24U);
  2042. pCSD->TAAC = (uint8_t)((hmmc->CSD[0] & 0x00FF0000U) >> 16U);
  2043. pCSD->NSAC = (uint8_t)((hmmc->CSD[0] & 0x0000FF00U) >> 8U);
  2044. pCSD->MaxBusClkFrec = (uint8_t)(hmmc->CSD[0] & 0x000000FFU);
  2045. pCSD->CardComdClasses = (uint16_t)((hmmc->CSD[1] & 0xFFF00000U) >> 20U);
  2046. pCSD->RdBlockLen = (uint8_t)((hmmc->CSD[1] & 0x000F0000U) >> 16U);
  2047. pCSD->PartBlockRead = (uint8_t)((hmmc->CSD[1] & 0x00008000U) >> 15U);
  2048. pCSD->WrBlockMisalign = (uint8_t)((hmmc->CSD[1] & 0x00004000U) >> 14U);
  2049. pCSD->RdBlockMisalign = (uint8_t)((hmmc->CSD[1] & 0x00002000U) >> 13U);
  2050. pCSD->DSRImpl = (uint8_t)((hmmc->CSD[1] & 0x00001000U) >> 12U);
  2051. pCSD->Reserved2 = 0U; /*!< Reserved */
  2052. if(MMC_ReadExtCSD(hmmc, &block_nbr, 212, 0x0FFFFFFFU) != HAL_OK) /* Field SEC_COUNT [215:212] */
  2053. {
  2054. return HAL_ERROR;
  2055. }
  2056. if(hmmc->MmcCard.CardType == MMC_LOW_CAPACITY_CARD)
  2057. {
  2058. pCSD->DeviceSize = (((hmmc->CSD[1] & 0x000003FFU) << 2U) | ((hmmc->CSD[2] & 0xC0000000U) >> 30U));
  2059. pCSD->MaxRdCurrentVDDMin = (uint8_t)((hmmc->CSD[2] & 0x38000000U) >> 27U);
  2060. pCSD->MaxRdCurrentVDDMax = (uint8_t)((hmmc->CSD[2] & 0x07000000U) >> 24U);
  2061. pCSD->MaxWrCurrentVDDMin = (uint8_t)((hmmc->CSD[2] & 0x00E00000U) >> 21U);
  2062. pCSD->MaxWrCurrentVDDMax = (uint8_t)((hmmc->CSD[2] & 0x001C0000U) >> 18U);
  2063. pCSD->DeviceSizeMul = (uint8_t)((hmmc->CSD[2] & 0x00038000U) >> 15U);
  2064. hmmc->MmcCard.BlockNbr = (pCSD->DeviceSize + 1U) ;
  2065. hmmc->MmcCard.BlockNbr *= (1UL << ((pCSD->DeviceSizeMul & 0x07U) + 2U));
  2066. hmmc->MmcCard.BlockSize = (1UL << (pCSD->RdBlockLen & 0x0FU));
  2067. hmmc->MmcCard.LogBlockNbr = (hmmc->MmcCard.BlockNbr) * ((hmmc->MmcCard.BlockSize) / 512U);
  2068. hmmc->MmcCard.LogBlockSize = 512U;
  2069. }
  2070. else if(hmmc->MmcCard.CardType == MMC_HIGH_CAPACITY_CARD)
  2071. {
  2072. hmmc->MmcCard.BlockNbr = block_nbr;
  2073. hmmc->MmcCard.LogBlockNbr = hmmc->MmcCard.BlockNbr;
  2074. hmmc->MmcCard.BlockSize = 512U;
  2075. hmmc->MmcCard.LogBlockSize = hmmc->MmcCard.BlockSize;
  2076. }
  2077. else
  2078. {
  2079. /* Clear all the static flags */
  2080. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
  2081. hmmc->ErrorCode |= HAL_MMC_ERROR_UNSUPPORTED_FEATURE;
  2082. hmmc->State = HAL_MMC_STATE_READY;
  2083. return HAL_ERROR;
  2084. }
  2085. pCSD->EraseGrSize = (uint8_t)((hmmc->CSD[2] & 0x00004000U) >> 14U);
  2086. pCSD->EraseGrMul = (uint8_t)((hmmc->CSD[2] & 0x00003F80U) >> 7U);
  2087. pCSD->WrProtectGrSize = (uint8_t)(hmmc->CSD[2] & 0x0000007FU);
  2088. pCSD->WrProtectGrEnable = (uint8_t)((hmmc->CSD[3] & 0x80000000U) >> 31U);
  2089. pCSD->ManDeflECC = (uint8_t)((hmmc->CSD[3] & 0x60000000U) >> 29U);
  2090. pCSD->WrSpeedFact = (uint8_t)((hmmc->CSD[3] & 0x1C000000U) >> 26U);
  2091. pCSD->MaxWrBlockLen= (uint8_t)((hmmc->CSD[3] & 0x03C00000U) >> 22U);
  2092. pCSD->WriteBlockPaPartial = (uint8_t)((hmmc->CSD[3] & 0x00200000U) >> 21U);
  2093. pCSD->Reserved3 = 0;
  2094. pCSD->ContentProtectAppli = (uint8_t)((hmmc->CSD[3] & 0x00010000U) >> 16U);
  2095. pCSD->FileFormatGroup = (uint8_t)((hmmc->CSD[3] & 0x00008000U) >> 15U);
  2096. pCSD->CopyFlag = (uint8_t)((hmmc->CSD[3] & 0x00004000U) >> 14U);
  2097. pCSD->PermWrProtect = (uint8_t)((hmmc->CSD[3] & 0x00002000U) >> 13U);
  2098. pCSD->TempWrProtect = (uint8_t)((hmmc->CSD[3] & 0x00001000U) >> 12U);
  2099. pCSD->FileFormat = (uint8_t)((hmmc->CSD[3] & 0x00000C00U) >> 10U);
  2100. pCSD->ECC= (uint8_t)((hmmc->CSD[3] & 0x00000300U) >> 8U);
  2101. pCSD->CSD_CRC = (uint8_t)((hmmc->CSD[3] & 0x000000FEU) >> 1U);
  2102. pCSD->Reserved4 = 1;
  2103. return HAL_OK;
  2104. }
  2105. /**
  2106. * @brief Gets the MMC card info.
  2107. * @param hmmc Pointer to MMC handle
  2108. * @param pCardInfo Pointer to the HAL_MMC_CardInfoTypeDef structure that
  2109. * will contain the MMC card status information
  2110. * @retval HAL status
  2111. */
  2112. HAL_StatusTypeDef HAL_MMC_GetCardInfo(MMC_HandleTypeDef *hmmc, HAL_MMC_CardInfoTypeDef *pCardInfo)
  2113. {
  2114. pCardInfo->CardType = (uint32_t)(hmmc->MmcCard.CardType);
  2115. pCardInfo->Class = (uint32_t)(hmmc->MmcCard.Class);
  2116. pCardInfo->RelCardAdd = (uint32_t)(hmmc->MmcCard.RelCardAdd);
  2117. pCardInfo->BlockNbr = (uint32_t)(hmmc->MmcCard.BlockNbr);
  2118. pCardInfo->BlockSize = (uint32_t)(hmmc->MmcCard.BlockSize);
  2119. pCardInfo->LogBlockNbr = (uint32_t)(hmmc->MmcCard.LogBlockNbr);
  2120. pCardInfo->LogBlockSize = (uint32_t)(hmmc->MmcCard.LogBlockSize);
  2121. return HAL_OK;
  2122. }
  2123. /**
  2124. * @brief Returns information the information of the card which are stored on
  2125. * the Extended CSD register.
  2126. * @param hmmc Pointer to MMC handle
  2127. * @param pExtCSD Pointer to a memory area (512 bytes) that contains all
  2128. * Extended CSD register parameters
  2129. * @param Timeout Specify timeout value
  2130. * @retval HAL status
  2131. */
  2132. HAL_StatusTypeDef HAL_MMC_GetCardExtCSD(MMC_HandleTypeDef *hmmc, uint32_t *pExtCSD, uint32_t Timeout)
  2133. {
  2134. SDMMC_DataInitTypeDef config;
  2135. uint32_t errorstate;
  2136. uint32_t tickstart = HAL_GetTick();
  2137. uint32_t count;
  2138. uint32_t *tmp_buf;
  2139. if(NULL == pExtCSD)
  2140. {
  2141. hmmc->ErrorCode |= HAL_MMC_ERROR_PARAM;
  2142. return HAL_ERROR;
  2143. }
  2144. if(hmmc->State == HAL_MMC_STATE_READY)
  2145. {
  2146. hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
  2147. hmmc->State = HAL_MMC_STATE_BUSY;
  2148. /* Initialize data control register */
  2149. hmmc->Instance->DCTRL = 0;
  2150. /* Initiaize the destination pointer */
  2151. tmp_buf = pExtCSD;
  2152. /* Configure the MMC DPSM (Data Path State Machine) */
  2153. config.DataTimeOut = SDMMC_DATATIMEOUT;
  2154. config.DataLength = 512;
  2155. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
  2156. config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
  2157. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  2158. #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  2159. config.DPSM = SDMMC_DPSM_ENABLE;
  2160. #else
  2161. config.DPSM = SDMMC_DPSM_DISABLE;
  2162. #endif
  2163. (void)SDMMC_ConfigData(hmmc->Instance, &config);
  2164. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  2165. __SDMMC_CMDTRANS_ENABLE( hmmc->Instance);
  2166. #endif
  2167. /* Send ExtCSD Read command to Card */
  2168. errorstate = SDMMC_CmdSendEXTCSD(hmmc->Instance, 0);
  2169. if(errorstate != HAL_MMC_ERROR_NONE)
  2170. {
  2171. /* Clear all the static flags */
  2172. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
  2173. hmmc->ErrorCode |= errorstate;
  2174. hmmc->State = HAL_MMC_STATE_READY;
  2175. return HAL_ERROR;
  2176. }
  2177. /* Poll on SDMMC flags */
  2178. while(!__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DATAEND))
  2179. {
  2180. if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_RXFIFOHF))
  2181. {
  2182. /* Read data from SDMMC Rx FIFO */
  2183. for(count = 0U; count < 8U; count++)
  2184. {
  2185. *tmp_buf = SDMMC_ReadFIFO(hmmc->Instance);
  2186. tmp_buf++;
  2187. }
  2188. }
  2189. if(((HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U))
  2190. {
  2191. /* Clear all the static flags */
  2192. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
  2193. hmmc->ErrorCode |= HAL_MMC_ERROR_TIMEOUT;
  2194. hmmc->State= HAL_MMC_STATE_READY;
  2195. return HAL_TIMEOUT;
  2196. }
  2197. }
  2198. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  2199. __SDMMC_CMDTRANS_DISABLE( hmmc->Instance);
  2200. #endif
  2201. /* Get error state */
  2202. if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_DTIMEOUT))
  2203. {
  2204. /* Clear all the static flags */
  2205. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
  2206. hmmc->ErrorCode |= HAL_MMC_ERROR_DATA_TIMEOUT;
  2207. hmmc->State = HAL_MMC_STATE_READY;
  2208. return HAL_ERROR;
  2209. }
  2210. else if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_DCRCFAIL))
  2211. {
  2212. /* Clear all the static flags */
  2213. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
  2214. hmmc->ErrorCode |= HAL_MMC_ERROR_DATA_CRC_FAIL;
  2215. hmmc->State = HAL_MMC_STATE_READY;
  2216. return HAL_ERROR;
  2217. }
  2218. else if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_RXOVERR))
  2219. {
  2220. /* Clear all the static flags */
  2221. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
  2222. hmmc->ErrorCode |= HAL_MMC_ERROR_RX_OVERRUN;
  2223. hmmc->State = HAL_MMC_STATE_READY;
  2224. return HAL_ERROR;
  2225. }
  2226. else
  2227. {
  2228. /* Nothing to do */
  2229. }
  2230. /* Clear all the static flags */
  2231. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_DATA_FLAGS);
  2232. hmmc->State = HAL_MMC_STATE_READY;
  2233. }
  2234. return HAL_OK;
  2235. }
  2236. /**
  2237. * @brief Enables wide bus operation for the requested card if supported by
  2238. * card.
  2239. * @param hmmc Pointer to MMC handle
  2240. * @param WideMode Specifies the MMC card wide bus mode
  2241. * This parameter can be one of the following values:
  2242. * @arg SDMMC_BUS_WIDE_8B: 8-bit data transfer
  2243. * @arg SDMMC_BUS_WIDE_4B: 4-bit data transfer
  2244. * @arg SDMMC_BUS_WIDE_1B: 1-bit data transfer
  2245. * @retval HAL status
  2246. */
  2247. HAL_StatusTypeDef HAL_MMC_ConfigWideBusOperation(MMC_HandleTypeDef *hmmc, uint32_t WideMode)
  2248. {
  2249. uint32_t count;
  2250. SDMMC_InitTypeDef Init;
  2251. uint32_t errorstate;
  2252. uint32_t response = 0U;
  2253. /* Check the parameters */
  2254. assert_param(IS_SDMMC_BUS_WIDE(WideMode));
  2255. /* Change State */
  2256. hmmc->State = HAL_MMC_STATE_BUSY;
  2257. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  2258. /* Check and update the power class if needed */
  2259. if ((hmmc->Instance->CLKCR & SDMMC_CLKCR_BUSSPEED) != 0U)
  2260. {
  2261. if ((hmmc->Instance->CLKCR & SDMMC_CLKCR_DDR) != 0U)
  2262. {
  2263. errorstate = MMC_PwrClassUpdate(hmmc, WideMode, SDMMC_SPEED_MODE_DDR);
  2264. }
  2265. else
  2266. {
  2267. errorstate = MMC_PwrClassUpdate(hmmc, WideMode, SDMMC_SPEED_MODE_HIGH);
  2268. }
  2269. }
  2270. else
  2271. {
  2272. errorstate = MMC_PwrClassUpdate(hmmc, WideMode, SDMMC_SPEED_MODE_DEFAULT);
  2273. }
  2274. #else
  2275. errorstate = MMC_PwrClassUpdate(hmmc, WideMode, 0U);
  2276. #endif
  2277. if(errorstate == HAL_MMC_ERROR_NONE)
  2278. {
  2279. if(WideMode == SDMMC_BUS_WIDE_8B)
  2280. {
  2281. errorstate = SDMMC_CmdSwitch(hmmc->Instance, 0x03B70200U);
  2282. }
  2283. else if(WideMode == SDMMC_BUS_WIDE_4B)
  2284. {
  2285. errorstate = SDMMC_CmdSwitch(hmmc->Instance, 0x03B70100U);
  2286. }
  2287. else if(WideMode == SDMMC_BUS_WIDE_1B)
  2288. {
  2289. errorstate = SDMMC_CmdSwitch(hmmc->Instance, 0x03B70000U);
  2290. }
  2291. else
  2292. {
  2293. /* WideMode is not a valid argument*/
  2294. errorstate = HAL_MMC_ERROR_PARAM;
  2295. }
  2296. /* Check for switch error and violation of the trial number of sending CMD 13 */
  2297. if(errorstate == HAL_MMC_ERROR_NONE)
  2298. {
  2299. /* While card is not ready for data and trial number for sending CMD13 is not exceeded */
  2300. count = SDMMC_MAX_TRIAL;
  2301. do
  2302. {
  2303. errorstate = SDMMC_CmdSendStatus(hmmc->Instance, (uint32_t)(((uint32_t)hmmc->MmcCard.RelCardAdd) << 16U));
  2304. if(errorstate != HAL_MMC_ERROR_NONE)
  2305. {
  2306. break;
  2307. }
  2308. /* Get command response */
  2309. response = SDMMC_GetResponse(hmmc->Instance, SDMMC_RESP1);
  2310. count--;
  2311. }while(((response & 0x100U) == 0U) && (count != 0U));
  2312. /* Check the status after the switch command execution */
  2313. if ((count != 0U) && (errorstate == HAL_MMC_ERROR_NONE))
  2314. {
  2315. /* Check the bit SWITCH_ERROR of the device status */
  2316. if ((response & 0x80U) != 0U)
  2317. {
  2318. errorstate = SDMMC_ERROR_GENERAL_UNKNOWN_ERR;
  2319. }
  2320. else
  2321. {
  2322. /* Configure the SDMMC peripheral */
  2323. Init = hmmc->Init;
  2324. Init.BusWide = WideMode;
  2325. (void)SDMMC_Init(hmmc->Instance, Init);
  2326. }
  2327. }
  2328. else if (count == 0U)
  2329. {
  2330. errorstate = SDMMC_ERROR_TIMEOUT;
  2331. }
  2332. else
  2333. {
  2334. /* Nothing to do */
  2335. }
  2336. }
  2337. }
  2338. /* Change State */
  2339. hmmc->State = HAL_MMC_STATE_READY;
  2340. if(errorstate != HAL_MMC_ERROR_NONE)
  2341. {
  2342. /* Clear all the static flags */
  2343. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
  2344. hmmc->ErrorCode |= errorstate;
  2345. return HAL_ERROR;
  2346. }
  2347. return HAL_OK;
  2348. }
  2349. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  2350. /**
  2351. * @brief Configure the speed bus mode
  2352. * @param hmmc Pointer to the MMC handle
  2353. * @param SpeedMode Specifies the MMC card speed bus mode
  2354. * This parameter can be one of the following values:
  2355. * @arg SDMMC_SPEED_MODE_AUTO: Max speed mode supported by the card
  2356. * @arg SDMMC_SPEED_MODE_DEFAULT: Default Speed (MMC @ 26MHz)
  2357. * @arg SDMMC_SPEED_MODE_HIGH: High Speed (MMC @ 52 MHz)
  2358. * @arg SDMMC_SPEED_MODE_DDR: High Speed DDR (MMC DDR @ 52 MHz)
  2359. * @retval HAL status
  2360. */
  2361. HAL_StatusTypeDef HAL_MMC_ConfigSpeedBusOperation(MMC_HandleTypeDef *hmmc, uint32_t SpeedMode)
  2362. {
  2363. uint32_t tickstart;
  2364. HAL_StatusTypeDef status = HAL_OK;
  2365. uint32_t device_type;
  2366. uint32_t errorstate;
  2367. /* Check the parameters */
  2368. assert_param(IS_SDMMC_SPEED_MODE(SpeedMode));
  2369. /* Change State */
  2370. hmmc->State = HAL_MMC_STATE_BUSY;
  2371. /* Field DEVICE_TYPE [196 = 49*4] of Extended CSD register */
  2372. device_type = (hmmc->Ext_CSD[49] & 0x000000FFU);
  2373. switch (SpeedMode)
  2374. {
  2375. case SDMMC_SPEED_MODE_AUTO:
  2376. {
  2377. if (((hmmc->Instance->CLKCR & SDMMC_CLKCR_WIDBUS) != 0U) && ((device_type & 0x04U) != 0U))
  2378. {
  2379. /* High Speed DDR mode allowed */
  2380. errorstate = MMC_HighSpeed(hmmc, ENABLE);
  2381. if(errorstate != HAL_MMC_ERROR_NONE)
  2382. {
  2383. hmmc->ErrorCode |= errorstate;
  2384. }
  2385. else
  2386. {
  2387. if ((hmmc->Instance->CLKCR & SDMMC_CLKCR_CLKDIV) != 0U)
  2388. {
  2389. /* DDR mode not supported with CLKDIV = 0 */
  2390. errorstate = MMC_DDR_Mode(hmmc, ENABLE);
  2391. if(errorstate != HAL_MMC_ERROR_NONE)
  2392. {
  2393. hmmc->ErrorCode |= errorstate;
  2394. }
  2395. }
  2396. }
  2397. }
  2398. else if ((device_type & 0x02U) != 0U)
  2399. {
  2400. /* High Speed mode allowed */
  2401. errorstate = MMC_HighSpeed(hmmc, ENABLE);
  2402. if(errorstate != HAL_MMC_ERROR_NONE)
  2403. {
  2404. hmmc->ErrorCode |= errorstate;
  2405. }
  2406. }
  2407. else
  2408. {
  2409. /* Nothing to do : keep current speed */
  2410. }
  2411. break;
  2412. }
  2413. case SDMMC_SPEED_MODE_DDR:
  2414. {
  2415. if (((hmmc->Instance->CLKCR & SDMMC_CLKCR_WIDBUS) != 0U) && ((device_type & 0x04U) != 0U))
  2416. {
  2417. /* High Speed DDR mode allowed */
  2418. errorstate = MMC_HighSpeed(hmmc, ENABLE);
  2419. if(errorstate != HAL_MMC_ERROR_NONE)
  2420. {
  2421. hmmc->ErrorCode |= errorstate;
  2422. }
  2423. else
  2424. {
  2425. if ((hmmc->Instance->CLKCR & SDMMC_CLKCR_CLKDIV) != 0U)
  2426. {
  2427. /* DDR mode not supported with CLKDIV = 0 */
  2428. errorstate = MMC_DDR_Mode(hmmc, ENABLE);
  2429. if(errorstate != HAL_MMC_ERROR_NONE)
  2430. {
  2431. hmmc->ErrorCode |= errorstate;
  2432. }
  2433. }
  2434. }
  2435. }
  2436. else
  2437. {
  2438. /* High Speed DDR mode not allowed */
  2439. hmmc->ErrorCode |= HAL_MMC_ERROR_UNSUPPORTED_FEATURE;
  2440. status = HAL_ERROR;
  2441. }
  2442. break;
  2443. }
  2444. case SDMMC_SPEED_MODE_HIGH:
  2445. {
  2446. if ((device_type & 0x02U) != 0U)
  2447. {
  2448. /* High Speed mode allowed */
  2449. errorstate = MMC_HighSpeed(hmmc, ENABLE);
  2450. if(errorstate != HAL_MMC_ERROR_NONE)
  2451. {
  2452. hmmc->ErrorCode |= errorstate;
  2453. }
  2454. }
  2455. else
  2456. {
  2457. /* High Speed mode not allowed */
  2458. hmmc->ErrorCode |= HAL_MMC_ERROR_UNSUPPORTED_FEATURE;
  2459. status = HAL_ERROR;
  2460. }
  2461. break;
  2462. }
  2463. case SDMMC_SPEED_MODE_DEFAULT:
  2464. {
  2465. if ((hmmc->Instance->CLKCR & SDMMC_CLKCR_DDR) != 0U)
  2466. {
  2467. /* High Speed DDR mode activated */
  2468. errorstate = MMC_DDR_Mode(hmmc, DISABLE);
  2469. if(errorstate != HAL_MMC_ERROR_NONE)
  2470. {
  2471. hmmc->ErrorCode |= errorstate;
  2472. }
  2473. }
  2474. if ((hmmc->Instance->CLKCR & SDMMC_CLKCR_BUSSPEED) != 0U)
  2475. {
  2476. /* High Speed mode activated */
  2477. errorstate = MMC_HighSpeed(hmmc, DISABLE);
  2478. if(errorstate != HAL_MMC_ERROR_NONE)
  2479. {
  2480. hmmc->ErrorCode |= errorstate;
  2481. }
  2482. }
  2483. break;
  2484. }
  2485. default:
  2486. hmmc->ErrorCode |= HAL_MMC_ERROR_PARAM;
  2487. status = HAL_ERROR;
  2488. break;
  2489. }
  2490. /* Verify that MMC card is ready to use after Speed mode switch*/
  2491. tickstart = HAL_GetTick();
  2492. while ((HAL_MMC_GetCardState(hmmc) != HAL_MMC_CARD_TRANSFER))
  2493. {
  2494. if ((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
  2495. {
  2496. hmmc->ErrorCode = HAL_MMC_ERROR_TIMEOUT;
  2497. hmmc->State = HAL_MMC_STATE_READY;
  2498. return HAL_TIMEOUT;
  2499. }
  2500. }
  2501. /* Change State */
  2502. hmmc->State = HAL_MMC_STATE_READY;
  2503. return status;
  2504. }
  2505. #endif
  2506. /**
  2507. * @brief Gets the current mmc card data state.
  2508. * @param hmmc pointer to MMC handle
  2509. * @retval Card state
  2510. */
  2511. HAL_MMC_CardStateTypeDef HAL_MMC_GetCardState(MMC_HandleTypeDef *hmmc)
  2512. {
  2513. uint32_t cardstate;
  2514. uint32_t errorstate;
  2515. uint32_t resp1 = 0U;
  2516. errorstate = MMC_SendStatus(hmmc, &resp1);
  2517. if(errorstate != HAL_MMC_ERROR_NONE)
  2518. {
  2519. hmmc->ErrorCode |= errorstate;
  2520. }
  2521. cardstate = ((resp1 >> 9U) & 0x0FU);
  2522. return (HAL_MMC_CardStateTypeDef)cardstate;
  2523. }
  2524. /**
  2525. * @brief Abort the current transfer and disable the MMC.
  2526. * @param hmmc pointer to a MMC_HandleTypeDef structure that contains
  2527. * the configuration information for MMC module.
  2528. * @retval HAL status
  2529. */
  2530. HAL_StatusTypeDef HAL_MMC_Abort(MMC_HandleTypeDef *hmmc)
  2531. {
  2532. HAL_MMC_CardStateTypeDef CardState;
  2533. /* DIsable All interrupts */
  2534. __HAL_MMC_DISABLE_IT(hmmc, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT|\
  2535. SDMMC_IT_TXUNDERR| SDMMC_IT_RXOVERR);
  2536. /* Clear All flags */
  2537. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_DATA_FLAGS);
  2538. #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  2539. if((hmmc->hdmatx != NULL) || (hmmc->hdmarx != NULL))
  2540. {
  2541. /* Disable the MMC DMA request */
  2542. hmmc->Instance->DCTRL &= (uint32_t)~((uint32_t)SDMMC_DCTRL_DMAEN);
  2543. /* Abort the MMC DMA Tx Stream */
  2544. if(hmmc->hdmatx != NULL)
  2545. {
  2546. if(HAL_DMA_Abort(hmmc->hdmatx) != HAL_OK)
  2547. {
  2548. hmmc->ErrorCode |= HAL_MMC_ERROR_DMA;
  2549. }
  2550. }
  2551. /* Abort the MMC DMA Rx Stream */
  2552. if(hmmc->hdmarx != NULL)
  2553. {
  2554. if(HAL_DMA_Abort(hmmc->hdmarx) != HAL_OK)
  2555. {
  2556. hmmc->ErrorCode |= HAL_MMC_ERROR_DMA;
  2557. }
  2558. }
  2559. }
  2560. #else
  2561. /* If IDMA Context, disable Internal DMA */
  2562. hmmc->Instance->IDMACTRL = SDMMC_DISABLE_IDMA;
  2563. #endif
  2564. hmmc->State = HAL_MMC_STATE_READY;
  2565. /* Initialize the MMC operation */
  2566. hmmc->Context = MMC_CONTEXT_NONE;
  2567. CardState = HAL_MMC_GetCardState(hmmc);
  2568. if((CardState == HAL_MMC_CARD_RECEIVING) || (CardState == HAL_MMC_CARD_SENDING))
  2569. {
  2570. hmmc->ErrorCode = SDMMC_CmdStopTransfer(hmmc->Instance);
  2571. }
  2572. if(hmmc->ErrorCode != HAL_MMC_ERROR_NONE)
  2573. {
  2574. return HAL_ERROR;
  2575. }
  2576. return HAL_OK;
  2577. }
  2578. /**
  2579. * @brief Abort the current transfer and disable the MMC (IT mode).
  2580. * @param hmmc pointer to a MMC_HandleTypeDef structure that contains
  2581. * the configuration information for MMC module.
  2582. * @retval HAL status
  2583. */
  2584. HAL_StatusTypeDef HAL_MMC_Abort_IT(MMC_HandleTypeDef *hmmc)
  2585. {
  2586. HAL_MMC_CardStateTypeDef CardState;
  2587. /* DIsable All interrupts */
  2588. __HAL_MMC_DISABLE_IT(hmmc, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT|\
  2589. SDMMC_IT_TXUNDERR| SDMMC_IT_RXOVERR);
  2590. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  2591. /* If IDMA Context, disable Internal DMA */
  2592. hmmc->Instance->IDMACTRL = SDMMC_DISABLE_IDMA;
  2593. #endif
  2594. /* Clear All flags */
  2595. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_DATA_FLAGS);
  2596. #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  2597. if((hmmc->hdmatx != NULL) || (hmmc->hdmarx != NULL))
  2598. {
  2599. /* Disable the MMC DMA request */
  2600. hmmc->Instance->DCTRL &= (uint32_t)~((uint32_t)SDMMC_DCTRL_DMAEN);
  2601. /* Abort the MMC DMA Tx Stream */
  2602. if(hmmc->hdmatx != NULL)
  2603. {
  2604. hmmc->hdmatx->XferAbortCallback = MMC_DMATxAbort;
  2605. if(HAL_DMA_Abort_IT(hmmc->hdmatx) != HAL_OK)
  2606. {
  2607. hmmc->hdmatx = NULL;
  2608. }
  2609. }
  2610. /* Abort the MMC DMA Rx Stream */
  2611. if(hmmc->hdmarx != NULL)
  2612. {
  2613. hmmc->hdmarx->XferAbortCallback = MMC_DMARxAbort;
  2614. if(HAL_DMA_Abort_IT(hmmc->hdmarx) != HAL_OK)
  2615. {
  2616. hmmc->hdmarx = NULL;
  2617. }
  2618. }
  2619. }
  2620. /* No transfer ongoing on both DMA channels*/
  2621. if((hmmc->hdmatx == NULL) && (hmmc->hdmarx == NULL))
  2622. {
  2623. #endif
  2624. CardState = HAL_MMC_GetCardState(hmmc);
  2625. hmmc->State = HAL_MMC_STATE_READY;
  2626. if((CardState == HAL_MMC_CARD_RECEIVING) || (CardState == HAL_MMC_CARD_SENDING))
  2627. {
  2628. hmmc->ErrorCode = SDMMC_CmdStopTransfer(hmmc->Instance);
  2629. }
  2630. if(hmmc->ErrorCode != HAL_MMC_ERROR_NONE)
  2631. {
  2632. return HAL_ERROR;
  2633. }
  2634. else
  2635. {
  2636. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  2637. hmmc->AbortCpltCallback(hmmc);
  2638. #else
  2639. HAL_MMC_AbortCallback(hmmc);
  2640. #endif
  2641. #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  2642. }
  2643. #endif
  2644. }
  2645. return HAL_OK;
  2646. }
  2647. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  2648. /**
  2649. * @brief Perform specific commands sequence for the different type of erase.
  2650. * @note This API should be followed by a check on the card state through
  2651. * HAL_MMC_GetCardState().
  2652. * @param hmmc Pointer to MMC handle
  2653. * @param EraseType Specifies the type of erase to be performed
  2654. * This parameter can be one of the following values:
  2655. * @arg HAL_MMC_ERASE Erase the erase groups identified by CMD35 & 36
  2656. * @arg HAL_MMC_TRIM Erase the write blocks identified by CMD35 & 36
  2657. * @arg HAL_MMC_DISCARD Discard the write blocks identified by CMD35 & 36
  2658. * @arg HAL_MMC_SECURE_ERASE Perform a secure purge according SRT on the erase groups identified by CMD35 & 36
  2659. * @arg HAL_MMC_SECURE_TRIM_STEP1 Mark the write blocks identified by CMD35 & 36 for secure erase
  2660. * @arg HAL_MMC_SECURE_TRIM_STEP2 Perform a secure purge according SRT on the write blocks previously identified
  2661. * @param BlockStartAdd Start Block address
  2662. * @param BlockEndAdd End Block address
  2663. * @retval HAL status
  2664. */
  2665. HAL_StatusTypeDef HAL_MMC_EraseSequence(MMC_HandleTypeDef *hmmc, uint32_t EraseType, uint32_t BlockStartAdd, uint32_t BlockEndAdd)
  2666. {
  2667. uint32_t errorstate;
  2668. uint32_t start_add = BlockStartAdd;
  2669. uint32_t end_add = BlockEndAdd;
  2670. uint32_t tickstart = HAL_GetTick();
  2671. /* Check the erase type value is correct */
  2672. assert_param(IS_MMC_ERASE_TYPE(EraseType));
  2673. /* Check the coherence between start and end address */
  2674. if(end_add < start_add)
  2675. {
  2676. hmmc->ErrorCode |= HAL_MMC_ERROR_PARAM;
  2677. return HAL_ERROR;
  2678. }
  2679. /* Check that the end address is not out of range of device memory */
  2680. if(end_add > (hmmc->MmcCard.LogBlockNbr))
  2681. {
  2682. hmmc->ErrorCode |= HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
  2683. return HAL_ERROR;
  2684. }
  2685. /* Check if the card command class supports erase command */
  2686. if(((hmmc->MmcCard.Class) & SDMMC_CCCC_ERASE) == 0U)
  2687. {
  2688. hmmc->ErrorCode |= HAL_MMC_ERROR_REQUEST_NOT_APPLICABLE;
  2689. return HAL_ERROR;
  2690. }
  2691. /* Check the state of the driver */
  2692. if(hmmc->State == HAL_MMC_STATE_READY)
  2693. {
  2694. /* Change State */
  2695. hmmc->State = HAL_MMC_STATE_BUSY;
  2696. /* Check that the card is not locked */
  2697. if((SDMMC_GetResponse(hmmc->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
  2698. {
  2699. hmmc->ErrorCode |= HAL_MMC_ERROR_LOCK_UNLOCK_FAILED;
  2700. hmmc->State = HAL_MMC_STATE_READY;
  2701. return HAL_ERROR;
  2702. }
  2703. /* In case of low capacity card, the address is not block number but bytes */
  2704. if ((hmmc->MmcCard.CardType) != MMC_HIGH_CAPACITY_CARD)
  2705. {
  2706. start_add *= 512U;
  2707. end_add *= 512U;
  2708. }
  2709. /* Send CMD35 MMC_ERASE_GRP_START with start address as argument */
  2710. errorstate = SDMMC_CmdEraseStartAdd(hmmc->Instance, start_add);
  2711. if(errorstate == HAL_MMC_ERROR_NONE)
  2712. {
  2713. /* Send CMD36 MMC_ERASE_GRP_END with end address as argument */
  2714. errorstate = SDMMC_CmdEraseEndAdd(hmmc->Instance, end_add);
  2715. if(errorstate == HAL_MMC_ERROR_NONE)
  2716. {
  2717. /* Send CMD38 ERASE with erase type as argument */
  2718. errorstate = SDMMC_CmdErase(hmmc->Instance, EraseType);
  2719. if(errorstate == HAL_MMC_ERROR_NONE)
  2720. {
  2721. if ((EraseType == HAL_MMC_SECURE_ERASE) || (EraseType == HAL_MMC_SECURE_TRIM_STEP2))
  2722. {
  2723. /* Wait that the device is ready by checking the D0 line */
  2724. while((!__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_BUSYD0END)) && (errorstate == HAL_MMC_ERROR_NONE))
  2725. {
  2726. if((HAL_GetTick()-tickstart) >= SDMMC_MAXERASETIMEOUT)
  2727. {
  2728. errorstate = HAL_MMC_ERROR_TIMEOUT;
  2729. }
  2730. }
  2731. /* Clear the flag corresponding to end D0 bus line */
  2732. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_FLAG_BUSYD0END);
  2733. }
  2734. }
  2735. }
  2736. }
  2737. /* Change State */
  2738. hmmc->State = HAL_MMC_STATE_READY;
  2739. /* Manage errors */
  2740. if(errorstate != HAL_MMC_ERROR_NONE)
  2741. {
  2742. /* Clear all the static flags */
  2743. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
  2744. hmmc->ErrorCode |= errorstate;
  2745. if(errorstate != HAL_MMC_ERROR_TIMEOUT)
  2746. {
  2747. return HAL_ERROR;
  2748. }
  2749. else
  2750. {
  2751. return HAL_TIMEOUT;
  2752. }
  2753. }
  2754. else
  2755. {
  2756. return HAL_OK;
  2757. }
  2758. }
  2759. else
  2760. {
  2761. return HAL_BUSY;
  2762. }
  2763. }
  2764. /**
  2765. * @brief Perform sanitize operation on the device.
  2766. * @note This API should be followed by a check on the card state through
  2767. * HAL_MMC_GetCardState().
  2768. * @param hmmc Pointer to MMC handle
  2769. * @retval HAL status
  2770. */
  2771. HAL_StatusTypeDef HAL_MMC_Sanitize(MMC_HandleTypeDef *hmmc)
  2772. {
  2773. uint32_t errorstate, response = 0U, count;
  2774. uint32_t tickstart = HAL_GetTick();
  2775. /* Check the state of the driver */
  2776. if(hmmc->State == HAL_MMC_STATE_READY)
  2777. {
  2778. /* Change State */
  2779. hmmc->State = HAL_MMC_STATE_BUSY;
  2780. /* Index : 165 - Value : 0x01 */
  2781. errorstate = SDMMC_CmdSwitch(hmmc->Instance, 0x03A50100U);
  2782. if(errorstate == HAL_MMC_ERROR_NONE)
  2783. {
  2784. /* Wait that the device is ready by checking the D0 line */
  2785. while((!__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_BUSYD0END)) && (errorstate == HAL_MMC_ERROR_NONE))
  2786. {
  2787. if((HAL_GetTick()-tickstart) >= SDMMC_MAXERASETIMEOUT)
  2788. {
  2789. errorstate = HAL_MMC_ERROR_TIMEOUT;
  2790. }
  2791. }
  2792. /* Clear the flag corresponding to end D0 bus line */
  2793. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_FLAG_BUSYD0END);
  2794. if(errorstate == HAL_MMC_ERROR_NONE)
  2795. {
  2796. /* While card is not ready for data and trial number for sending CMD13 is not exceeded */
  2797. count = SDMMC_MAX_TRIAL;
  2798. do
  2799. {
  2800. errorstate = SDMMC_CmdSendStatus(hmmc->Instance, (uint32_t)(((uint32_t)hmmc->MmcCard.RelCardAdd) << 16U));
  2801. if(errorstate != HAL_MMC_ERROR_NONE)
  2802. {
  2803. break;
  2804. }
  2805. /* Get command response */
  2806. response = SDMMC_GetResponse(hmmc->Instance, SDMMC_RESP1);
  2807. count--;
  2808. }while(((response & 0x100U) == 0U) && (count != 0U));
  2809. /* Check the status after the switch command execution */
  2810. if ((count != 0U) && (errorstate == HAL_MMC_ERROR_NONE))
  2811. {
  2812. /* Check the bit SWITCH_ERROR of the device status */
  2813. if ((response & 0x80U) != 0U)
  2814. {
  2815. errorstate = SDMMC_ERROR_GENERAL_UNKNOWN_ERR;
  2816. }
  2817. }
  2818. else if (count == 0U)
  2819. {
  2820. errorstate = SDMMC_ERROR_TIMEOUT;
  2821. }
  2822. else
  2823. {
  2824. /* Nothing to do */
  2825. }
  2826. }
  2827. }
  2828. /* Change State */
  2829. hmmc->State = HAL_MMC_STATE_READY;
  2830. /* Manage errors */
  2831. if(errorstate != HAL_MMC_ERROR_NONE)
  2832. {
  2833. /* Clear all the static flags */
  2834. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
  2835. hmmc->ErrorCode |= errorstate;
  2836. if(errorstate != HAL_MMC_ERROR_TIMEOUT)
  2837. {
  2838. return HAL_ERROR;
  2839. }
  2840. else
  2841. {
  2842. return HAL_TIMEOUT;
  2843. }
  2844. }
  2845. else
  2846. {
  2847. return HAL_OK;
  2848. }
  2849. }
  2850. else
  2851. {
  2852. return HAL_BUSY;
  2853. }
  2854. }
  2855. /**
  2856. * @brief Configure the Secure Removal Type (SRT) in the Extended CSD register.
  2857. * @note This API should be followed by a check on the card state through
  2858. * HAL_MMC_GetCardState().
  2859. * @param hmmc Pointer to MMC handle
  2860. * @param SRTMode Specifies the type of erase to be performed
  2861. * This parameter can be one of the following values:
  2862. * @arg HAL_MMC_SRT_ERASE Information removed by an erase
  2863. * @arg HAL_MMC_SRT_WRITE_CHAR_ERASE Information removed by an overwriting with a character followed by an erase
  2864. * @arg HAL_MMC_SRT_WRITE_CHAR_COMPL_RANDOM Information removed by an overwriting with a character, its complement then a random character
  2865. * @arg HAL_MMC_SRT_VENDOR_DEFINED Information removed using a vendor defined
  2866. * @retval HAL status
  2867. */
  2868. HAL_StatusTypeDef HAL_MMC_ConfigSecRemovalType(MMC_HandleTypeDef *hmmc, uint32_t SRTMode)
  2869. {
  2870. uint32_t srt, errorstate, response = 0U, count;
  2871. /* Check the erase type value is correct */
  2872. assert_param(IS_MMC_SRT_TYPE(SRTMode));
  2873. /* Check the state of the driver */
  2874. if(hmmc->State == HAL_MMC_STATE_READY)
  2875. {
  2876. /* Get the supported values by the device */
  2877. if(HAL_MMC_GetSupportedSecRemovalType(hmmc, &srt) == HAL_OK)
  2878. {
  2879. /* Change State */
  2880. hmmc->State = HAL_MMC_STATE_BUSY;
  2881. /* Check the value passed as parameter is supported by the device */
  2882. if((SRTMode & srt) != 0U)
  2883. {
  2884. /* Index : 16 - Value : SRTMode */
  2885. srt |= ((POSITION_VAL(SRTMode)) << 4U);
  2886. errorstate = SDMMC_CmdSwitch(hmmc->Instance, (0x03100000U | (srt << 8U)));
  2887. if(errorstate == HAL_MMC_ERROR_NONE)
  2888. {
  2889. /* While card is not ready for data and trial number for sending CMD13 is not exceeded */
  2890. count = SDMMC_MAX_TRIAL;
  2891. do
  2892. {
  2893. errorstate = SDMMC_CmdSendStatus(hmmc->Instance, (uint32_t)(((uint32_t)hmmc->MmcCard.RelCardAdd) << 16U));
  2894. if(errorstate != HAL_MMC_ERROR_NONE)
  2895. {
  2896. break;
  2897. }
  2898. /* Get command response */
  2899. response = SDMMC_GetResponse(hmmc->Instance, SDMMC_RESP1);
  2900. count--;
  2901. }while(((response & 0x100U) == 0U) && (count != 0U));
  2902. /* Check the status after the switch command execution */
  2903. if ((count != 0U) && (errorstate == HAL_MMC_ERROR_NONE))
  2904. {
  2905. /* Check the bit SWITCH_ERROR of the device status */
  2906. if ((response & 0x80U) != 0U)
  2907. {
  2908. errorstate = SDMMC_ERROR_GENERAL_UNKNOWN_ERR;
  2909. }
  2910. }
  2911. else if (count == 0U)
  2912. {
  2913. errorstate = SDMMC_ERROR_TIMEOUT;
  2914. }
  2915. else
  2916. {
  2917. /* Nothing to do */
  2918. }
  2919. }
  2920. }
  2921. else
  2922. {
  2923. errorstate = SDMMC_ERROR_UNSUPPORTED_FEATURE;
  2924. }
  2925. /* Change State */
  2926. hmmc->State = HAL_MMC_STATE_READY;
  2927. }
  2928. else
  2929. {
  2930. errorstate = SDMMC_ERROR_GENERAL_UNKNOWN_ERR;
  2931. }
  2932. /* Manage errors */
  2933. if(errorstate != HAL_MMC_ERROR_NONE)
  2934. {
  2935. /* Clear all the static flags */
  2936. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
  2937. hmmc->ErrorCode |= errorstate;
  2938. return HAL_ERROR;
  2939. }
  2940. else
  2941. {
  2942. return HAL_OK;
  2943. }
  2944. }
  2945. else
  2946. {
  2947. return HAL_BUSY;
  2948. }
  2949. }
  2950. /**
  2951. * @brief Gets the supported values of the the Secure Removal Type (SRT).
  2952. * @param hmmc pointer to MMC handle
  2953. * @param SupportedSRT pointer for supported SRT value
  2954. * This parameter is a bit field of the following values:
  2955. * @arg HAL_MMC_SRT_ERASE Information removed by an erase
  2956. * @arg HAL_MMC_SRT_WRITE_CHAR_ERASE Information removed by an overwriting with a character followed by an erase
  2957. * @arg HAL_MMC_SRT_WRITE_CHAR_COMPL_RANDOM Information removed by an overwriting with a character, its complement then a random character
  2958. * @arg HAL_MMC_SRT_VENDOR_DEFINED Information removed using a vendor defined
  2959. * @retval HAL status
  2960. */
  2961. HAL_StatusTypeDef HAL_MMC_GetSupportedSecRemovalType(MMC_HandleTypeDef *hmmc, uint32_t *SupportedSRT)
  2962. {
  2963. /* Check the state of the driver */
  2964. if(hmmc->State == HAL_MMC_STATE_READY)
  2965. {
  2966. /* Change State */
  2967. hmmc->State = HAL_MMC_STATE_BUSY;
  2968. /* Read field SECURE_REMOVAL_TYPE [16 = 4*4] of the Extended CSD register */
  2969. *SupportedSRT = (hmmc->Ext_CSD[4] & 0x0000000FU); /* Bits [3:0] of field 16 */
  2970. /* Change State */
  2971. hmmc->State = HAL_MMC_STATE_READY;
  2972. return HAL_OK;
  2973. }
  2974. else
  2975. {
  2976. return HAL_BUSY;
  2977. }
  2978. }
  2979. /**
  2980. * @brief Switch the device from Standby State to Sleep State.
  2981. * @param hmmc pointer to MMC handle
  2982. * @retval HAL status
  2983. */
  2984. HAL_StatusTypeDef HAL_MMC_SleepDevice(MMC_HandleTypeDef *hmmc)
  2985. {
  2986. uint32_t errorstate, sleep_timeout, timeout, count, response = 0U;
  2987. uint32_t tickstart = HAL_GetTick();
  2988. /* Check the state of the driver */
  2989. if(hmmc->State == HAL_MMC_STATE_READY)
  2990. {
  2991. /* Change State */
  2992. hmmc->State = HAL_MMC_STATE_BUSY;
  2993. /* Set the power-off notification to powered-on : Ext_CSD[34] = 1 */
  2994. errorstate = SDMMC_CmdSwitch(hmmc->Instance, (0x03220100U));
  2995. if (errorstate == HAL_MMC_ERROR_NONE)
  2996. {
  2997. /* While card is not ready for data and trial number for sending CMD13 is not exceeded */
  2998. count = SDMMC_MAX_TRIAL;
  2999. do
  3000. {
  3001. errorstate = SDMMC_CmdSendStatus(hmmc->Instance, (uint32_t)(((uint32_t)hmmc->MmcCard.RelCardAdd) << 16U));
  3002. if(errorstate != HAL_MMC_ERROR_NONE)
  3003. {
  3004. break;
  3005. }
  3006. /* Get command response */
  3007. response = SDMMC_GetResponse(hmmc->Instance, SDMMC_RESP1);
  3008. count--;
  3009. }while(((response & 0x100U) == 0U) && (count != 0U));
  3010. /* Check the status after the switch command execution */
  3011. if (count == 0U)
  3012. {
  3013. errorstate = SDMMC_ERROR_TIMEOUT;
  3014. }
  3015. else if (errorstate == HAL_MMC_ERROR_NONE)
  3016. {
  3017. /* Check the bit SWITCH_ERROR of the device status */
  3018. if ((response & 0x80U) != 0U)
  3019. {
  3020. errorstate = SDMMC_ERROR_UNSUPPORTED_FEATURE;
  3021. }
  3022. else
  3023. {
  3024. /* Set the power-off notification to sleep notification : Ext_CSD[34] = 4 */
  3025. errorstate = SDMMC_CmdSwitch(hmmc->Instance, (0x03220400U));
  3026. if (errorstate == HAL_MMC_ERROR_NONE)
  3027. {
  3028. /* Field SLEEP_NOTIFICATION_TIME [216] */
  3029. sleep_timeout = ((hmmc->Ext_CSD[(MMC_EXT_CSD_SLEEP_NOTIFICATION_TIME_INDEX/4)] >> MMC_EXT_CSD_SLEEP_NOTIFICATION_TIME_POS) & 0x000000FFU);
  3030. /* Sleep/Awake Timeout = 10µs * 2^SLEEP_NOTIFICATION_TIME, max value of SLEEP_NOTIFICATION_TIME is 0x17 */
  3031. /* In HAL, the tick interrupt occurs each ms */
  3032. timeout = (((1UL << (sleep_timeout & 0x1FU)) / 100U) + 1U);
  3033. /* Wait that the device is ready by checking the D0 line */
  3034. while((!__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_BUSYD0END)) && (errorstate == HAL_MMC_ERROR_NONE))
  3035. {
  3036. if((HAL_GetTick() - tickstart) >= timeout)
  3037. {
  3038. errorstate = SDMMC_ERROR_TIMEOUT;
  3039. }
  3040. }
  3041. /* Clear the flag corresponding to end D0 bus line */
  3042. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_FLAG_BUSYD0END);
  3043. if (errorstate == HAL_MMC_ERROR_NONE)
  3044. {
  3045. /* While card is not ready for data and trial number for sending CMD13 is not exceeded */
  3046. count = SDMMC_MAX_TRIAL;
  3047. do
  3048. {
  3049. errorstate = SDMMC_CmdSendStatus(hmmc->Instance, (uint32_t)(((uint32_t)hmmc->MmcCard.RelCardAdd) << 16U));
  3050. if(errorstate != HAL_MMC_ERROR_NONE)
  3051. {
  3052. break;
  3053. }
  3054. /* Get command response */
  3055. response = SDMMC_GetResponse(hmmc->Instance, SDMMC_RESP1);
  3056. count--;
  3057. }while(((response & 0x100U) == 0U) && (count != 0U));
  3058. /* Check the status after the switch command execution */
  3059. if (count == 0U)
  3060. {
  3061. errorstate = SDMMC_ERROR_TIMEOUT;
  3062. }
  3063. else if (errorstate == HAL_MMC_ERROR_NONE)
  3064. {
  3065. /* Check the bit SWITCH_ERROR of the device status */
  3066. if ((response & 0x80U) != 0U)
  3067. {
  3068. errorstate = SDMMC_ERROR_UNSUPPORTED_FEATURE;
  3069. }
  3070. else
  3071. {
  3072. /* Switch the device in stand-by mode */
  3073. (void)SDMMC_CmdSelDesel(hmmc->Instance, 0U);
  3074. /* Field S_A_TIEMOUT [217] */
  3075. sleep_timeout = ((hmmc->Ext_CSD[(MMC_EXT_CSD_S_A_TIMEOUT_INDEX/4)] >> MMC_EXT_CSD_S_A_TIMEOUT_POS) & 0x000000FFU);
  3076. /* Sleep/Awake Timeout = 100ns * 2^S_A_TIMEOUT, max value of S_A_TIMEOUT is 0x17 */
  3077. /* In HAL, the tick interrupt occurs each ms */
  3078. timeout = (((1UL << (sleep_timeout & 0x1FU)) / 10000U) + 1U);
  3079. if (HAL_MMC_GetCardState(hmmc) == HAL_MMC_CARD_STANDBY)
  3080. {
  3081. /* Send CMD5 CMD_MMC_SLEEP_AWAKE with RCA and SLEEP as argument */
  3082. errorstate = SDMMC_CmdSleepMmc(hmmc->Instance, ((hmmc->MmcCard.RelCardAdd << 16U) | (0x1U << 15U)));
  3083. if (errorstate == HAL_MMC_ERROR_NONE)
  3084. {
  3085. /* Wait that the device is ready by checking the D0 line */
  3086. while((!__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_BUSYD0END)) && (errorstate == HAL_MMC_ERROR_NONE))
  3087. {
  3088. if((HAL_GetTick() - tickstart) >= timeout)
  3089. {
  3090. errorstate = SDMMC_ERROR_TIMEOUT;
  3091. }
  3092. }
  3093. /* Clear the flag corresponding to end D0 bus line */
  3094. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_FLAG_BUSYD0END);
  3095. }
  3096. }
  3097. else
  3098. {
  3099. errorstate = SDMMC_ERROR_REQUEST_NOT_APPLICABLE;
  3100. }
  3101. }
  3102. }
  3103. else
  3104. {
  3105. /* Nothing to do */
  3106. }
  3107. }
  3108. }
  3109. }
  3110. }
  3111. else
  3112. {
  3113. /* Nothing to do */
  3114. }
  3115. }
  3116. /* Change State */
  3117. hmmc->State = HAL_MMC_STATE_READY;
  3118. /* Manage errors */
  3119. if (errorstate != HAL_MMC_ERROR_NONE)
  3120. {
  3121. /* Clear all the static flags */
  3122. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
  3123. hmmc->ErrorCode |= errorstate;
  3124. if (errorstate != HAL_MMC_ERROR_TIMEOUT)
  3125. {
  3126. return HAL_ERROR;
  3127. }
  3128. else
  3129. {
  3130. return HAL_TIMEOUT;
  3131. }
  3132. }
  3133. else
  3134. {
  3135. return HAL_OK;
  3136. }
  3137. }
  3138. else
  3139. {
  3140. return HAL_BUSY;
  3141. }
  3142. }
  3143. /**
  3144. * @brief Switch the device from Sleep State to Standby State.
  3145. * @param hmmc pointer to MMC handle
  3146. * @retval HAL status
  3147. */
  3148. HAL_StatusTypeDef HAL_MMC_AwakeDevice(MMC_HandleTypeDef *hmmc)
  3149. {
  3150. uint32_t errorstate, sleep_timeout, timeout, count, response = 0U;
  3151. uint32_t tickstart = HAL_GetTick();
  3152. /* Check the state of the driver */
  3153. if (hmmc->State == HAL_MMC_STATE_READY)
  3154. {
  3155. /* Change State */
  3156. hmmc->State = HAL_MMC_STATE_BUSY;
  3157. /* Field S_A_TIEMOUT [217] */
  3158. sleep_timeout = ((hmmc->Ext_CSD[(MMC_EXT_CSD_S_A_TIMEOUT_INDEX/4)] >> MMC_EXT_CSD_S_A_TIMEOUT_POS) & 0x000000FFU);
  3159. /* Sleep/Awake Timeout = 100ns * 2^S_A_TIMEOUT, max value of S_A_TIMEOUT is 0x17 */
  3160. /* In HAL, the tick interrupt occurs each ms */
  3161. timeout = (((1UL << (sleep_timeout & 0x1FU)) / 10000U) + 1U);
  3162. /* Send CMD5 CMD_MMC_SLEEP_AWAKE with RCA and AWAKE as argument */
  3163. errorstate = SDMMC_CmdSleepMmc(hmmc->Instance, (hmmc->MmcCard.RelCardAdd << 16U));
  3164. if (errorstate == HAL_MMC_ERROR_NONE)
  3165. {
  3166. /* Wait that the device is ready by checking the D0 line */
  3167. while ((!__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_BUSYD0END)) && (errorstate == HAL_MMC_ERROR_NONE))
  3168. {
  3169. if((HAL_GetTick() - tickstart) >= timeout)
  3170. {
  3171. errorstate = SDMMC_ERROR_TIMEOUT;
  3172. }
  3173. }
  3174. /* Clear the flag corresponding to end D0 bus line */
  3175. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_FLAG_BUSYD0END);
  3176. if (errorstate == HAL_MMC_ERROR_NONE)
  3177. {
  3178. if (HAL_MMC_GetCardState(hmmc) == HAL_MMC_CARD_STANDBY)
  3179. {
  3180. /* Switch the device in transfer mode */
  3181. errorstate = SDMMC_CmdSelDesel(hmmc->Instance, (uint32_t)(((uint32_t)hmmc->MmcCard.RelCardAdd) << 16U));
  3182. if (errorstate == HAL_MMC_ERROR_NONE)
  3183. {
  3184. if (HAL_MMC_GetCardState(hmmc) == HAL_MMC_CARD_TRANSFER)
  3185. {
  3186. /* Set the power-off notification to powered-on : Ext_CSD[34] = 1 */
  3187. errorstate = SDMMC_CmdSwitch(hmmc->Instance, (0x03220100U));
  3188. if (errorstate == HAL_MMC_ERROR_NONE)
  3189. {
  3190. /* While card is not ready for data and trial number for sending CMD13 is not exceeded */
  3191. count = SDMMC_MAX_TRIAL;
  3192. do
  3193. {
  3194. errorstate = SDMMC_CmdSendStatus(hmmc->Instance, (uint32_t)(((uint32_t)hmmc->MmcCard.RelCardAdd) << 16U));
  3195. if(errorstate != HAL_MMC_ERROR_NONE)
  3196. {
  3197. break;
  3198. }
  3199. /* Get command response */
  3200. response = SDMMC_GetResponse(hmmc->Instance, SDMMC_RESP1);
  3201. count--;
  3202. }while(((response & 0x100U) == 0U) && (count != 0U));
  3203. /* Check the status after the switch command execution */
  3204. if (count == 0U)
  3205. {
  3206. errorstate = SDMMC_ERROR_TIMEOUT;
  3207. }
  3208. else if (errorstate == HAL_MMC_ERROR_NONE)
  3209. {
  3210. /* Check the bit SWITCH_ERROR of the device status */
  3211. if ((response & 0x80U) != 0U)
  3212. {
  3213. errorstate = SDMMC_ERROR_UNSUPPORTED_FEATURE;
  3214. }
  3215. }
  3216. else
  3217. {
  3218. /* Nothing to do */
  3219. }
  3220. }
  3221. }
  3222. else
  3223. {
  3224. errorstate = SDMMC_ERROR_REQUEST_NOT_APPLICABLE;
  3225. }
  3226. }
  3227. }
  3228. else
  3229. {
  3230. errorstate = SDMMC_ERROR_REQUEST_NOT_APPLICABLE;
  3231. }
  3232. }
  3233. }
  3234. /* Change State */
  3235. hmmc->State = HAL_MMC_STATE_READY;
  3236. /* Manage errors */
  3237. if (errorstate != HAL_MMC_ERROR_NONE)
  3238. {
  3239. /* Clear all the static flags */
  3240. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
  3241. hmmc->ErrorCode |= errorstate;
  3242. if (errorstate != HAL_MMC_ERROR_TIMEOUT)
  3243. {
  3244. return HAL_ERROR;
  3245. }
  3246. else
  3247. {
  3248. return HAL_TIMEOUT;
  3249. }
  3250. }
  3251. else
  3252. {
  3253. return HAL_OK;
  3254. }
  3255. }
  3256. else
  3257. {
  3258. return HAL_BUSY;
  3259. }
  3260. }
  3261. #endif /* defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx) */
  3262. /**
  3263. * @}
  3264. */
  3265. /**
  3266. * @}
  3267. */
  3268. /* Private function ----------------------------------------------------------*/
  3269. /** @addtogroup MMC_Private_Functions
  3270. * @{
  3271. */
  3272. #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  3273. /**
  3274. * @brief DMA MMC transmit process complete callback
  3275. * @param hdma DMA handle
  3276. * @retval None
  3277. */
  3278. static void MMC_DMATransmitCplt(DMA_HandleTypeDef *hdma)
  3279. {
  3280. MMC_HandleTypeDef* hmmc = (MMC_HandleTypeDef* )(hdma->Parent);
  3281. /* Enable DATAEND Interrupt */
  3282. __HAL_MMC_ENABLE_IT(hmmc, (SDMMC_IT_DATAEND));
  3283. }
  3284. /**
  3285. * @brief DMA MMC receive process complete callback
  3286. * @param hdma DMA handle
  3287. * @retval None
  3288. */
  3289. static void MMC_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
  3290. {
  3291. MMC_HandleTypeDef* hmmc = (MMC_HandleTypeDef* )(hdma->Parent);
  3292. uint32_t errorstate;
  3293. /* Send stop command in multiblock write */
  3294. if(hmmc->Context == (MMC_CONTEXT_READ_MULTIPLE_BLOCK | MMC_CONTEXT_DMA))
  3295. {
  3296. errorstate = SDMMC_CmdStopTransfer(hmmc->Instance);
  3297. if(errorstate != HAL_MMC_ERROR_NONE)
  3298. {
  3299. hmmc->ErrorCode |= errorstate;
  3300. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  3301. hmmc->ErrorCallback(hmmc);
  3302. #else
  3303. HAL_MMC_ErrorCallback(hmmc);
  3304. #endif
  3305. }
  3306. }
  3307. /* Disable the DMA transfer for transmit request by setting the DMAEN bit
  3308. in the MMC DCTRL register */
  3309. hmmc->Instance->DCTRL &= (uint32_t)~((uint32_t)SDMMC_DCTRL_DMAEN);
  3310. /* Clear all the static flags */
  3311. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_DATA_FLAGS);
  3312. hmmc->State = HAL_MMC_STATE_READY;
  3313. hmmc->Context = MMC_CONTEXT_NONE;
  3314. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  3315. hmmc->RxCpltCallback(hmmc);
  3316. #else
  3317. HAL_MMC_RxCpltCallback(hmmc);
  3318. #endif
  3319. }
  3320. /**
  3321. * @brief DMA MMC communication error callback
  3322. * @param hdma DMA handle
  3323. * @retval None
  3324. */
  3325. static void MMC_DMAError(DMA_HandleTypeDef *hdma)
  3326. {
  3327. MMC_HandleTypeDef* hmmc = (MMC_HandleTypeDef* )(hdma->Parent);
  3328. HAL_MMC_CardStateTypeDef CardState;
  3329. uint32_t RxErrorCode, TxErrorCode;
  3330. RxErrorCode = hmmc->hdmarx->ErrorCode;
  3331. TxErrorCode = hmmc->hdmatx->ErrorCode;
  3332. if((RxErrorCode == HAL_DMA_ERROR_TE) || (TxErrorCode == HAL_DMA_ERROR_TE))
  3333. {
  3334. /* Clear All flags */
  3335. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
  3336. /* Disable All interrupts */
  3337. __HAL_MMC_DISABLE_IT(hmmc, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT|\
  3338. SDMMC_IT_TXUNDERR| SDMMC_IT_RXOVERR);
  3339. hmmc->ErrorCode |= HAL_MMC_ERROR_DMA;
  3340. CardState = HAL_MMC_GetCardState(hmmc);
  3341. if((CardState == HAL_MMC_CARD_RECEIVING) || (CardState == HAL_MMC_CARD_SENDING))
  3342. {
  3343. hmmc->ErrorCode |= SDMMC_CmdStopTransfer(hmmc->Instance);
  3344. }
  3345. hmmc->State= HAL_MMC_STATE_READY;
  3346. hmmc->Context = MMC_CONTEXT_NONE;
  3347. }
  3348. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  3349. hmmc->ErrorCallback(hmmc);
  3350. #else
  3351. HAL_MMC_ErrorCallback(hmmc);
  3352. #endif
  3353. }
  3354. /**
  3355. * @brief DMA MMC Tx Abort callback
  3356. * @param hdma DMA handle
  3357. * @retval None
  3358. */
  3359. static void MMC_DMATxAbort(DMA_HandleTypeDef *hdma)
  3360. {
  3361. MMC_HandleTypeDef* hmmc = (MMC_HandleTypeDef* )(hdma->Parent);
  3362. HAL_MMC_CardStateTypeDef CardState;
  3363. if(hmmc->hdmatx != NULL)
  3364. {
  3365. hmmc->hdmatx = NULL;
  3366. }
  3367. /* All DMA channels are aborted */
  3368. if(hmmc->hdmarx == NULL)
  3369. {
  3370. CardState = HAL_MMC_GetCardState(hmmc);
  3371. hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
  3372. hmmc->State = HAL_MMC_STATE_READY;
  3373. hmmc->Context = MMC_CONTEXT_NONE;
  3374. if((CardState == HAL_MMC_CARD_RECEIVING) || (CardState == HAL_MMC_CARD_SENDING))
  3375. {
  3376. hmmc->ErrorCode |= SDMMC_CmdStopTransfer(hmmc->Instance);
  3377. if(hmmc->ErrorCode != HAL_MMC_ERROR_NONE)
  3378. {
  3379. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  3380. hmmc->AbortCpltCallback(hmmc);
  3381. #else
  3382. HAL_MMC_AbortCallback(hmmc);
  3383. #endif
  3384. }
  3385. else
  3386. {
  3387. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  3388. hmmc->ErrorCallback(hmmc);
  3389. #else
  3390. HAL_MMC_ErrorCallback(hmmc);
  3391. #endif
  3392. }
  3393. }
  3394. }
  3395. }
  3396. /**
  3397. * @brief DMA MMC Rx Abort callback
  3398. * @param hdma DMA handle
  3399. * @retval None
  3400. */
  3401. static void MMC_DMARxAbort(DMA_HandleTypeDef *hdma)
  3402. {
  3403. MMC_HandleTypeDef* hmmc = (MMC_HandleTypeDef* )(hdma->Parent);
  3404. HAL_MMC_CardStateTypeDef CardState;
  3405. if(hmmc->hdmarx != NULL)
  3406. {
  3407. hmmc->hdmarx = NULL;
  3408. }
  3409. /* All DMA channels are aborted */
  3410. if(hmmc->hdmatx == NULL)
  3411. {
  3412. CardState = HAL_MMC_GetCardState(hmmc);
  3413. hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
  3414. hmmc->State = HAL_MMC_STATE_READY;
  3415. hmmc->Context = MMC_CONTEXT_NONE;
  3416. if((CardState == HAL_MMC_CARD_RECEIVING) || (CardState == HAL_MMC_CARD_SENDING))
  3417. {
  3418. hmmc->ErrorCode |= SDMMC_CmdStopTransfer(hmmc->Instance);
  3419. if(hmmc->ErrorCode != HAL_MMC_ERROR_NONE)
  3420. {
  3421. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  3422. hmmc->AbortCpltCallback(hmmc);
  3423. #else
  3424. HAL_MMC_AbortCallback(hmmc);
  3425. #endif
  3426. }
  3427. else
  3428. {
  3429. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  3430. hmmc->ErrorCallback(hmmc);
  3431. #else
  3432. HAL_MMC_ErrorCallback(hmmc);
  3433. #endif
  3434. }
  3435. }
  3436. }
  3437. }
  3438. #endif
  3439. /**
  3440. * @brief Initializes the mmc card.
  3441. * @param hmmc Pointer to MMC handle
  3442. * @retval MMC Card error state
  3443. */
  3444. static uint32_t MMC_InitCard(MMC_HandleTypeDef *hmmc)
  3445. {
  3446. HAL_MMC_CardCSDTypeDef CSD;
  3447. uint32_t errorstate;
  3448. uint16_t mmc_rca = 2U;
  3449. MMC_InitTypeDef Init;
  3450. /* Check the power State */
  3451. if(SDMMC_GetPowerState(hmmc->Instance) == 0U)
  3452. {
  3453. /* Power off */
  3454. return HAL_MMC_ERROR_REQUEST_NOT_APPLICABLE;
  3455. }
  3456. /* Send CMD2 ALL_SEND_CID */
  3457. errorstate = SDMMC_CmdSendCID(hmmc->Instance);
  3458. if(errorstate != HAL_MMC_ERROR_NONE)
  3459. {
  3460. return errorstate;
  3461. }
  3462. else
  3463. {
  3464. /* Get Card identification number data */
  3465. hmmc->CID[0U] = SDMMC_GetResponse(hmmc->Instance, SDMMC_RESP1);
  3466. hmmc->CID[1U] = SDMMC_GetResponse(hmmc->Instance, SDMMC_RESP2);
  3467. hmmc->CID[2U] = SDMMC_GetResponse(hmmc->Instance, SDMMC_RESP3);
  3468. hmmc->CID[3U] = SDMMC_GetResponse(hmmc->Instance, SDMMC_RESP4);
  3469. }
  3470. /* Send CMD3 SET_REL_ADDR with RCA = 2 (should be greater than 1) */
  3471. /* MMC Card publishes its RCA. */
  3472. errorstate = SDMMC_CmdSetRelAddMmc(hmmc->Instance, mmc_rca);
  3473. if(errorstate != HAL_MMC_ERROR_NONE)
  3474. {
  3475. return errorstate;
  3476. }
  3477. /* Get the MMC card RCA */
  3478. hmmc->MmcCard.RelCardAdd = mmc_rca;
  3479. /* Send CMD9 SEND_CSD with argument as card's RCA */
  3480. errorstate = SDMMC_CmdSendCSD(hmmc->Instance, (uint32_t)(hmmc->MmcCard.RelCardAdd << 16U));
  3481. if(errorstate != HAL_MMC_ERROR_NONE)
  3482. {
  3483. return errorstate;
  3484. }
  3485. else
  3486. {
  3487. /* Get Card Specific Data */
  3488. hmmc->CSD[0U] = SDMMC_GetResponse(hmmc->Instance, SDMMC_RESP1);
  3489. hmmc->CSD[1U] = SDMMC_GetResponse(hmmc->Instance, SDMMC_RESP2);
  3490. hmmc->CSD[2U] = SDMMC_GetResponse(hmmc->Instance, SDMMC_RESP3);
  3491. hmmc->CSD[3U] = SDMMC_GetResponse(hmmc->Instance, SDMMC_RESP4);
  3492. }
  3493. /* Get the Card Class */
  3494. hmmc->MmcCard.Class = (SDMMC_GetResponse(hmmc->Instance, SDMMC_RESP2) >> 20U);
  3495. /* Select the Card */
  3496. errorstate = SDMMC_CmdSelDesel(hmmc->Instance, (uint32_t)(((uint32_t)hmmc->MmcCard.RelCardAdd) << 16U));
  3497. if(errorstate != HAL_MMC_ERROR_NONE)
  3498. {
  3499. return errorstate;
  3500. }
  3501. /* Get CSD parameters */
  3502. if (HAL_MMC_GetCardCSD(hmmc, &CSD) != HAL_OK)
  3503. {
  3504. return hmmc->ErrorCode;
  3505. }
  3506. /* While card is not ready for data and trial number for sending CMD13 is not exceeded */
  3507. errorstate = SDMMC_CmdSendStatus(hmmc->Instance, (uint32_t)(((uint32_t)hmmc->MmcCard.RelCardAdd) << 16U));
  3508. if(errorstate != HAL_MMC_ERROR_NONE)
  3509. {
  3510. hmmc->ErrorCode |= errorstate;
  3511. }
  3512. /* Get Extended CSD parameters */
  3513. if (HAL_MMC_GetCardExtCSD(hmmc, hmmc->Ext_CSD, SDMMC_DATATIMEOUT) != HAL_OK)
  3514. {
  3515. return hmmc->ErrorCode;
  3516. }
  3517. /* While card is not ready for data and trial number for sending CMD13 is not exceeded */
  3518. errorstate = SDMMC_CmdSendStatus(hmmc->Instance, (uint32_t)(((uint32_t)hmmc->MmcCard.RelCardAdd) << 16U));
  3519. if(errorstate != HAL_MMC_ERROR_NONE)
  3520. {
  3521. hmmc->ErrorCode |= errorstate;
  3522. }
  3523. /* Configure the SDMMC peripheral */
  3524. Init = hmmc->Init;
  3525. Init.BusWide = SDMMC_BUS_WIDE_1B;
  3526. (void)SDMMC_Init(hmmc->Instance, Init);
  3527. /* All cards are initialized */
  3528. return HAL_MMC_ERROR_NONE;
  3529. }
  3530. /**
  3531. * @brief Enquires cards about their operating voltage and configures clock
  3532. * controls and stores MMC information that will be needed in future
  3533. * in the MMC handle.
  3534. * @param hmmc Pointer to MMC handle
  3535. * @retval error state
  3536. */
  3537. static uint32_t MMC_PowerON(MMC_HandleTypeDef *hmmc)
  3538. {
  3539. __IO uint32_t count = 0U;
  3540. uint32_t response = 0U, validvoltage = 0U;
  3541. uint32_t errorstate;
  3542. /* CMD0: GO_IDLE_STATE */
  3543. errorstate = SDMMC_CmdGoIdleState(hmmc->Instance);
  3544. if(errorstate != HAL_MMC_ERROR_NONE)
  3545. {
  3546. return errorstate;
  3547. }
  3548. while(validvoltage == 0U)
  3549. {
  3550. if(count++ == SDMMC_MAX_VOLT_TRIAL)
  3551. {
  3552. return HAL_MMC_ERROR_INVALID_VOLTRANGE;
  3553. }
  3554. /* SEND CMD1 APP_CMD with voltage range as argument */
  3555. errorstate = SDMMC_CmdOpCondition(hmmc->Instance, MMC_VOLTAGE_RANGE);
  3556. if(errorstate != HAL_MMC_ERROR_NONE)
  3557. {
  3558. return HAL_MMC_ERROR_UNSUPPORTED_FEATURE;
  3559. }
  3560. /* Get command response */
  3561. response = SDMMC_GetResponse(hmmc->Instance, SDMMC_RESP1);
  3562. /* Get operating voltage*/
  3563. validvoltage = (((response >> 31U) == 1U) ? 1U : 0U);
  3564. }
  3565. /* When power routine is finished and command returns valid voltage */
  3566. if (((response & (0xFF000000U)) >> 24) == 0xC0U)
  3567. {
  3568. hmmc->MmcCard.CardType = MMC_HIGH_CAPACITY_CARD;
  3569. }
  3570. else
  3571. {
  3572. hmmc->MmcCard.CardType = MMC_LOW_CAPACITY_CARD;
  3573. }
  3574. return HAL_MMC_ERROR_NONE;
  3575. }
  3576. /**
  3577. * @brief Turns the SDMMC output signals off.
  3578. * @param hmmc Pointer to MMC handle
  3579. * @retval None
  3580. */
  3581. static void MMC_PowerOFF(MMC_HandleTypeDef *hmmc)
  3582. {
  3583. /* Set Power State to OFF */
  3584. (void)SDMMC_PowerState_OFF(hmmc->Instance);
  3585. }
  3586. /**
  3587. * @brief Returns the current card's status.
  3588. * @param hmmc Pointer to MMC handle
  3589. * @param pCardStatus pointer to the buffer that will contain the MMC card
  3590. * status (Card Status register)
  3591. * @retval error state
  3592. */
  3593. static uint32_t MMC_SendStatus(MMC_HandleTypeDef *hmmc, uint32_t *pCardStatus)
  3594. {
  3595. uint32_t errorstate;
  3596. if(pCardStatus == NULL)
  3597. {
  3598. return HAL_MMC_ERROR_PARAM;
  3599. }
  3600. /* Send Status command */
  3601. errorstate = SDMMC_CmdSendStatus(hmmc->Instance, (uint32_t)(hmmc->MmcCard.RelCardAdd << 16U));
  3602. if(errorstate != HAL_MMC_ERROR_NONE)
  3603. {
  3604. return errorstate;
  3605. }
  3606. /* Get MMC card status */
  3607. *pCardStatus = SDMMC_GetResponse(hmmc->Instance, SDMMC_RESP1);
  3608. return HAL_MMC_ERROR_NONE;
  3609. }
  3610. /**
  3611. * @brief Reads extended CSD register to get the sectors number of the device
  3612. * @param hmmc Pointer to MMC handle
  3613. * @param pFieldData Pointer to the read buffer
  3614. * @param FieldIndex Index of the field to be read
  3615. * @param Timeout Specify timeout value
  3616. * @retval HAL status
  3617. */
  3618. static HAL_StatusTypeDef MMC_ReadExtCSD(MMC_HandleTypeDef *hmmc, uint32_t *pFieldData, uint16_t FieldIndex, uint32_t Timeout)
  3619. {
  3620. SDMMC_DataInitTypeDef config;
  3621. uint32_t errorstate;
  3622. uint32_t tickstart = HAL_GetTick();
  3623. uint32_t count;
  3624. uint32_t i = 0;
  3625. uint32_t tmp_data;
  3626. hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
  3627. /* Initialize data control register */
  3628. hmmc->Instance->DCTRL = 0;
  3629. /* Configure the MMC DPSM (Data Path State Machine) */
  3630. config.DataTimeOut = SDMMC_DATATIMEOUT;
  3631. config.DataLength = 512;
  3632. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
  3633. config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
  3634. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  3635. config.DPSM = SDMMC_DPSM_ENABLE;
  3636. (void)SDMMC_ConfigData(hmmc->Instance, &config);
  3637. /* Set Block Size for Card */
  3638. errorstate = SDMMC_CmdSendEXTCSD(hmmc->Instance, 0);
  3639. if(errorstate != HAL_MMC_ERROR_NONE)
  3640. {
  3641. /* Clear all the static flags */
  3642. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
  3643. hmmc->ErrorCode |= errorstate;
  3644. hmmc->State = HAL_MMC_STATE_READY;
  3645. return HAL_ERROR;
  3646. }
  3647. /* Poll on SDMMC flags */
  3648. while(!__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DATAEND))
  3649. {
  3650. if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_RXFIFOHF))
  3651. {
  3652. /* Read data from SDMMC Rx FIFO */
  3653. for(count = 0U; count < 8U; count++)
  3654. {
  3655. tmp_data = SDMMC_ReadFIFO(hmmc->Instance);
  3656. /* eg : SEC_COUNT : FieldIndex = 212 => i+count = 53 */
  3657. /* DEVICE_TYPE : FieldIndex = 196 => i+count = 49 */
  3658. if ((i + count) == ((uint32_t)FieldIndex/4U))
  3659. {
  3660. *pFieldData = tmp_data;
  3661. }
  3662. }
  3663. i += 8U;
  3664. }
  3665. if(((HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U))
  3666. {
  3667. /* Clear all the static flags */
  3668. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
  3669. hmmc->ErrorCode |= HAL_MMC_ERROR_TIMEOUT;
  3670. hmmc->State= HAL_MMC_STATE_READY;
  3671. return HAL_TIMEOUT;
  3672. }
  3673. }
  3674. /* Get error state */
  3675. if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_DTIMEOUT))
  3676. {
  3677. /* Clear all the static flags */
  3678. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
  3679. hmmc->ErrorCode |= HAL_MMC_ERROR_DATA_TIMEOUT;
  3680. hmmc->State = HAL_MMC_STATE_READY;
  3681. return HAL_ERROR;
  3682. }
  3683. else if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_DCRCFAIL))
  3684. {
  3685. /* Clear all the static flags */
  3686. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
  3687. hmmc->ErrorCode |= HAL_MMC_ERROR_DATA_CRC_FAIL;
  3688. hmmc->State = HAL_MMC_STATE_READY;
  3689. return HAL_ERROR;
  3690. }
  3691. else if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_RXOVERR))
  3692. {
  3693. /* Clear all the static flags */
  3694. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
  3695. hmmc->ErrorCode |= HAL_MMC_ERROR_RX_OVERRUN;
  3696. hmmc->State = HAL_MMC_STATE_READY;
  3697. return HAL_ERROR;
  3698. }
  3699. else
  3700. {
  3701. /* Nothing to do */
  3702. }
  3703. /* While card is not ready for data and trial number for sending CMD13 is not exceeded */
  3704. errorstate = SDMMC_CmdSendStatus(hmmc->Instance, (uint32_t)(((uint32_t)hmmc->MmcCard.RelCardAdd) << 16));
  3705. if(errorstate != HAL_MMC_ERROR_NONE)
  3706. {
  3707. hmmc->ErrorCode |= errorstate;
  3708. }
  3709. /* Clear all the static flags */
  3710. __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_DATA_FLAGS);
  3711. hmmc->State = HAL_MMC_STATE_READY;
  3712. return HAL_OK;
  3713. }
  3714. /**
  3715. * @brief Wrap up reading in non-blocking mode.
  3716. * @param hmmc pointer to a MMC_HandleTypeDef structure that contains
  3717. * the configuration information.
  3718. * @retval None
  3719. */
  3720. static void MMC_Read_IT(MMC_HandleTypeDef *hmmc)
  3721. {
  3722. uint32_t count, data, dataremaining;
  3723. uint8_t* tmp;
  3724. tmp = hmmc->pRxBuffPtr;
  3725. dataremaining = hmmc->RxXferSize;
  3726. if (dataremaining > 0U)
  3727. {
  3728. /* Read data from SDMMC Rx FIFO */
  3729. for(count = 0U; count < 8U; count++)
  3730. {
  3731. data = SDMMC_ReadFIFO(hmmc->Instance);
  3732. *tmp = (uint8_t)(data & 0xFFU);
  3733. tmp++;
  3734. dataremaining--;
  3735. *tmp = (uint8_t)((data >> 8U) & 0xFFU);
  3736. tmp++;
  3737. dataremaining--;
  3738. *tmp = (uint8_t)((data >> 16U) & 0xFFU);
  3739. tmp++;
  3740. dataremaining--;
  3741. *tmp = (uint8_t)((data >> 24U) & 0xFFU);
  3742. tmp++;
  3743. dataremaining--;
  3744. }
  3745. hmmc->pRxBuffPtr = tmp;
  3746. hmmc->RxXferSize = dataremaining;
  3747. }
  3748. }
  3749. /**
  3750. * @brief Wrap up writing in non-blocking mode.
  3751. * @param hmmc pointer to a MMC_HandleTypeDef structure that contains
  3752. * the configuration information.
  3753. * @retval None
  3754. */
  3755. static void MMC_Write_IT(MMC_HandleTypeDef *hmmc)
  3756. {
  3757. uint32_t count, data, dataremaining;
  3758. uint8_t* tmp;
  3759. tmp = hmmc->pTxBuffPtr;
  3760. dataremaining = hmmc->TxXferSize;
  3761. if (dataremaining > 0U)
  3762. {
  3763. /* Write data to SDMMC Tx FIFO */
  3764. for(count = 0U; count < 8U; count++)
  3765. {
  3766. data = (uint32_t)(*tmp);
  3767. tmp++;
  3768. dataremaining--;
  3769. data |= ((uint32_t)(*tmp) << 8U);
  3770. tmp++;
  3771. dataremaining--;
  3772. data |= ((uint32_t)(*tmp) << 16U);
  3773. tmp++;
  3774. dataremaining--;
  3775. data |= ((uint32_t)(*tmp) << 24U);
  3776. tmp++;
  3777. dataremaining--;
  3778. (void)SDMMC_WriteFIFO(hmmc->Instance, &data);
  3779. }
  3780. hmmc->pTxBuffPtr = tmp;
  3781. hmmc->TxXferSize = dataremaining;
  3782. }
  3783. }
  3784. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  3785. /**
  3786. * @brief Switches the MMC card to high speed mode.
  3787. * @param hmmc MMC handle
  3788. * @param state State of high speed mode
  3789. * @retval MMC Card error state
  3790. */
  3791. static uint32_t MMC_HighSpeed(MMC_HandleTypeDef *hmmc, FunctionalState state)
  3792. {
  3793. uint32_t errorstate = HAL_MMC_ERROR_NONE;
  3794. uint32_t response = 0U, count;
  3795. uint32_t sdmmc_clk;
  3796. SDMMC_InitTypeDef Init;
  3797. if (((hmmc->Instance->CLKCR & SDMMC_CLKCR_BUSSPEED) != 0U) && (state == DISABLE))
  3798. {
  3799. errorstate = MMC_PwrClassUpdate(hmmc, (hmmc->Instance->CLKCR & SDMMC_CLKCR_WIDBUS), SDMMC_SPEED_MODE_DEFAULT);
  3800. if(errorstate == HAL_MMC_ERROR_NONE)
  3801. {
  3802. /* Index : 185 - Value : 0 */
  3803. errorstate = SDMMC_CmdSwitch(hmmc->Instance, 0x03B90000U);
  3804. }
  3805. }
  3806. if (((hmmc->Instance->CLKCR & SDMMC_CLKCR_BUSSPEED) == 0U) && (state != DISABLE))
  3807. {
  3808. errorstate = MMC_PwrClassUpdate(hmmc, (hmmc->Instance->CLKCR & SDMMC_CLKCR_WIDBUS), SDMMC_SPEED_MODE_HIGH);
  3809. if(errorstate == HAL_MMC_ERROR_NONE)
  3810. {
  3811. /* Index : 185 - Value : 1 */
  3812. errorstate = SDMMC_CmdSwitch(hmmc->Instance, 0x03B90100U);
  3813. }
  3814. }
  3815. if(errorstate == HAL_MMC_ERROR_NONE)
  3816. {
  3817. /* While card is not ready for data and trial number for sending CMD13 is not exceeded */
  3818. count = SDMMC_MAX_TRIAL;
  3819. do
  3820. {
  3821. errorstate = SDMMC_CmdSendStatus(hmmc->Instance, (uint32_t)(((uint32_t)hmmc->MmcCard.RelCardAdd) << 16U));
  3822. if(errorstate != HAL_MMC_ERROR_NONE)
  3823. {
  3824. break;
  3825. }
  3826. /* Get command response */
  3827. response = SDMMC_GetResponse(hmmc->Instance, SDMMC_RESP1);
  3828. count--;
  3829. }while(((response & 0x100U) == 0U) && (count != 0U));
  3830. /* Check the status after the switch command execution */
  3831. if ((count != 0U) && (errorstate == HAL_MMC_ERROR_NONE))
  3832. {
  3833. /* Check the bit SWITCH_ERROR of the device status */
  3834. if ((response & 0x80U) != 0U)
  3835. {
  3836. errorstate = SDMMC_ERROR_UNSUPPORTED_FEATURE;
  3837. }
  3838. else
  3839. {
  3840. /* Configure high speed */
  3841. Init.ClockEdge = hmmc->Init.ClockEdge;
  3842. Init.ClockPowerSave = hmmc->Init.ClockPowerSave;
  3843. Init.BusWide = (hmmc->Instance->CLKCR & SDMMC_CLKCR_WIDBUS);
  3844. Init.HardwareFlowControl = hmmc->Init.HardwareFlowControl;
  3845. if (state == DISABLE)
  3846. {
  3847. Init.ClockDiv = hmmc->Init.ClockDiv;
  3848. (void)SDMMC_Init(hmmc->Instance, Init);
  3849. CLEAR_BIT(hmmc->Instance->CLKCR, SDMMC_CLKCR_BUSSPEED);
  3850. }
  3851. else
  3852. {
  3853. /* High Speed Clock should be less or equal to 52MHz*/
  3854. sdmmc_clk = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_SDMMC1);
  3855. if (sdmmc_clk == 0U)
  3856. {
  3857. errorstate = SDMMC_ERROR_INVALID_PARAMETER;
  3858. }
  3859. else
  3860. {
  3861. if (sdmmc_clk <= MMC_HIGH_SPEED_FREQ)
  3862. {
  3863. Init.ClockDiv = 0;
  3864. }
  3865. else
  3866. {
  3867. Init.ClockDiv = (sdmmc_clk / (2U * MMC_HIGH_SPEED_FREQ)) + 1U;
  3868. }
  3869. (void)SDMMC_Init(hmmc->Instance, Init);
  3870. SET_BIT(hmmc->Instance->CLKCR, SDMMC_CLKCR_BUSSPEED);
  3871. }
  3872. }
  3873. }
  3874. }
  3875. else if (count == 0U)
  3876. {
  3877. errorstate = SDMMC_ERROR_TIMEOUT;
  3878. }
  3879. else
  3880. {
  3881. /* Nothing to do */
  3882. }
  3883. }
  3884. return errorstate;
  3885. }
  3886. /**
  3887. * @brief Switches the MMC card to Double Data Rate (DDR) mode.
  3888. * @param hmmc MMC handle
  3889. * @param state State of DDR mode
  3890. * @retval MMC Card error state
  3891. */
  3892. static uint32_t MMC_DDR_Mode(MMC_HandleTypeDef *hmmc, FunctionalState state)
  3893. {
  3894. uint32_t errorstate = HAL_MMC_ERROR_NONE;
  3895. uint32_t response = 0U, count;
  3896. if (((hmmc->Instance->CLKCR & SDMMC_CLKCR_DDR) != 0U) && (state == DISABLE))
  3897. {
  3898. if ((hmmc->Instance->CLKCR & SDMMC_CLKCR_WIDBUS_0) != 0U)
  3899. {
  3900. errorstate = MMC_PwrClassUpdate(hmmc, SDMMC_BUS_WIDE_4B, SDMMC_SPEED_MODE_HIGH);
  3901. if(errorstate == HAL_MMC_ERROR_NONE)
  3902. {
  3903. /* Index : 183 - Value : 1 */
  3904. errorstate = SDMMC_CmdSwitch(hmmc->Instance, 0x03B70100U);
  3905. }
  3906. }
  3907. else
  3908. {
  3909. errorstate = MMC_PwrClassUpdate(hmmc, SDMMC_BUS_WIDE_8B, SDMMC_SPEED_MODE_HIGH);
  3910. if(errorstate == HAL_MMC_ERROR_NONE)
  3911. {
  3912. /* Index : 183 - Value : 2 */
  3913. errorstate = SDMMC_CmdSwitch(hmmc->Instance, 0x03B70200U);
  3914. }
  3915. }
  3916. }
  3917. if (((hmmc->Instance->CLKCR & SDMMC_CLKCR_DDR) == 0U) && (state != DISABLE))
  3918. {
  3919. if ((hmmc->Instance->CLKCR & SDMMC_CLKCR_WIDBUS_0) != 0U)
  3920. {
  3921. errorstate = MMC_PwrClassUpdate(hmmc, SDMMC_BUS_WIDE_4B, SDMMC_SPEED_MODE_DDR);
  3922. if(errorstate == HAL_MMC_ERROR_NONE)
  3923. {
  3924. /* Index : 183 - Value : 5 */
  3925. errorstate = SDMMC_CmdSwitch(hmmc->Instance, 0x03B70500U);
  3926. }
  3927. }
  3928. else
  3929. {
  3930. errorstate = MMC_PwrClassUpdate(hmmc, SDMMC_BUS_WIDE_8B, SDMMC_SPEED_MODE_DDR);
  3931. if(errorstate == HAL_MMC_ERROR_NONE)
  3932. {
  3933. /* Index : 183 - Value : 6 */
  3934. errorstate = SDMMC_CmdSwitch(hmmc->Instance, 0x03B70600U);
  3935. }
  3936. }
  3937. }
  3938. if(errorstate == HAL_MMC_ERROR_NONE)
  3939. {
  3940. /* While card is not ready for data and trial number for sending CMD13 is not exceeded */
  3941. count = SDMMC_MAX_TRIAL;
  3942. do
  3943. {
  3944. errorstate = SDMMC_CmdSendStatus(hmmc->Instance, (uint32_t)(((uint32_t)hmmc->MmcCard.RelCardAdd) << 16U));
  3945. if(errorstate != HAL_MMC_ERROR_NONE)
  3946. {
  3947. break;
  3948. }
  3949. /* Get command response */
  3950. response = SDMMC_GetResponse(hmmc->Instance, SDMMC_RESP1);
  3951. count--;
  3952. }while(((response & 0x100U) == 0U) && (count != 0U));
  3953. /* Check the status after the switch command execution */
  3954. if ((count != 0U) && (errorstate == HAL_MMC_ERROR_NONE))
  3955. {
  3956. /* Check the bit SWITCH_ERROR of the device status */
  3957. if ((response & 0x80U) != 0U)
  3958. {
  3959. errorstate = SDMMC_ERROR_UNSUPPORTED_FEATURE;
  3960. }
  3961. else
  3962. {
  3963. /* Configure DDR mode */
  3964. if ((count != 0U) && (errorstate == HAL_MMC_ERROR_NONE))
  3965. {
  3966. if (state == DISABLE)
  3967. {
  3968. CLEAR_BIT(hmmc->Instance->CLKCR, SDMMC_CLKCR_DDR);
  3969. }
  3970. else
  3971. {
  3972. SET_BIT(hmmc->Instance->CLKCR, SDMMC_CLKCR_DDR);
  3973. }
  3974. }
  3975. }
  3976. }
  3977. else if (count == 0U)
  3978. {
  3979. errorstate = SDMMC_ERROR_TIMEOUT;
  3980. }
  3981. else
  3982. {
  3983. /* Nothing to do */
  3984. }
  3985. }
  3986. return errorstate;
  3987. }
  3988. #endif
  3989. /**
  3990. * @brief Update the power class of the device.
  3991. * @param hmmc MMC handle
  3992. * @param Wide Wide of MMC bus
  3993. * @param Speed Speed of the MMC bus
  3994. * @retval MMC Card error state
  3995. */
  3996. static uint32_t MMC_PwrClassUpdate(MMC_HandleTypeDef *hmmc, uint32_t Wide, uint32_t Speed)
  3997. {
  3998. uint32_t count;
  3999. uint32_t response = 0U;
  4000. uint32_t errorstate = HAL_MMC_ERROR_NONE;
  4001. uint32_t power_class, supported_pwr_class;
  4002. if((Wide == SDMMC_BUS_WIDE_8B) || (Wide == SDMMC_BUS_WIDE_4B))
  4003. {
  4004. power_class = 0U; /* Default value after power-on or software reset */
  4005. /* Read the PowerClass field of the Extended CSD register */
  4006. if(MMC_ReadExtCSD(hmmc, &power_class, 187, SDMMC_DATATIMEOUT) != HAL_OK) /* Field POWER_CLASS [187] */
  4007. {
  4008. errorstate = SDMMC_ERROR_GENERAL_UNKNOWN_ERR;
  4009. }
  4010. else
  4011. {
  4012. power_class = ((power_class >> 24U) & 0x000000FFU);
  4013. }
  4014. /* Get the supported PowerClass field of the Extended CSD register */
  4015. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  4016. if (Speed == SDMMC_SPEED_MODE_DDR)
  4017. {
  4018. /* Field PWR_CL_DDR_52_xxx [238 or 239] */
  4019. supported_pwr_class = ((hmmc->Ext_CSD[(MMC_EXT_CSD_PWR_CL_DDR_52_INDEX/4)] >> MMC_EXT_CSD_PWR_CL_DDR_52_POS) & 0x000000FFU);
  4020. }
  4021. else if (Speed == SDMMC_SPEED_MODE_HIGH)
  4022. {
  4023. /* Field PWR_CL_52_xxx [200 or 202] */
  4024. supported_pwr_class = ((hmmc->Ext_CSD[(MMC_EXT_CSD_PWR_CL_52_INDEX/4)] >> MMC_EXT_CSD_PWR_CL_52_POS) & 0x000000FFU);
  4025. }
  4026. else
  4027. #else /* Prevent compiler warning in case of -Wextra */
  4028. UNUSED(Speed);
  4029. #endif
  4030. {
  4031. /* Field PWR_CL_26_xxx [201 or 203] */
  4032. supported_pwr_class = ((hmmc->Ext_CSD[(MMC_EXT_CSD_PWR_CL_26_INDEX/4)] >> MMC_EXT_CSD_PWR_CL_26_POS) & 0x000000FFU);
  4033. }
  4034. if(errorstate == HAL_MMC_ERROR_NONE)
  4035. {
  4036. if(Wide == SDMMC_BUS_WIDE_8B)
  4037. {
  4038. /* Bit [7:4] : power class for 8-bits bus configuration - Bit [3:0] : power class for 4-bits bus configuration */
  4039. supported_pwr_class = (supported_pwr_class >> 4U);
  4040. }
  4041. if ((power_class & 0x0FU) != (supported_pwr_class & 0x0FU))
  4042. {
  4043. /* Need to change current power class */
  4044. errorstate = SDMMC_CmdSwitch(hmmc->Instance, (0x03BB0000U | ((supported_pwr_class & 0x0FU) << 8U)));
  4045. if(errorstate == HAL_MMC_ERROR_NONE)
  4046. {
  4047. /* While card is not ready for data and trial number for sending CMD13 is not exceeded */
  4048. count = SDMMC_MAX_TRIAL;
  4049. do
  4050. {
  4051. errorstate = SDMMC_CmdSendStatus(hmmc->Instance, (uint32_t)(((uint32_t)hmmc->MmcCard.RelCardAdd) << 16U));
  4052. if(errorstate != HAL_MMC_ERROR_NONE)
  4053. {
  4054. break;
  4055. }
  4056. /* Get command response */
  4057. response = SDMMC_GetResponse(hmmc->Instance, SDMMC_RESP1);
  4058. count--;
  4059. }while(((response & 0x100U) == 0U) && (count != 0U));
  4060. /* Check the status after the switch command execution */
  4061. if ((count != 0U) && (errorstate == HAL_MMC_ERROR_NONE))
  4062. {
  4063. /* Check the bit SWITCH_ERROR of the device status */
  4064. if ((response & 0x80U) != 0U)
  4065. {
  4066. errorstate = SDMMC_ERROR_UNSUPPORTED_FEATURE;
  4067. }
  4068. }
  4069. else if (count == 0U)
  4070. {
  4071. errorstate = SDMMC_ERROR_TIMEOUT;
  4072. }
  4073. else
  4074. {
  4075. /* Nothing to do */
  4076. }
  4077. }
  4078. }
  4079. }
  4080. }
  4081. return errorstate;
  4082. }
  4083. /**
  4084. * @}
  4085. */
  4086. /**
  4087. * @}
  4088. */
  4089. /**
  4090. * @}
  4091. */
  4092. #endif /* HAL_MMC_MODULE_ENABLED */
  4093. #endif /* SDMMC1 */