stm32l4xx_hal_sd.c 148 KB

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