stm32l4xx_hal_sd.c 112 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339
  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. * + SD card Control functions
  12. *
  13. @verbatim
  14. ==============================================================================
  15. ##### How to use this driver #####
  16. ==============================================================================
  17. [..]
  18. This driver implements a high level communication layer for read and write from/to
  19. this memory. The needed STM32 hardware resources (SDMMC1 and GPIO) are performed by
  20. the user in HAL_SD_MspInit() function (MSP layer).
  21. Basically, the MSP layer configuration should be the same as we provide in the
  22. examples.
  23. You can easily tailor this configuration according to hardware resources.
  24. [..]
  25. This driver is a generic layered driver for SDMMC memories which uses the HAL
  26. SDMMC driver functions to interface with SD and uSD cards devices.
  27. It is used as follows:
  28. (#)Initialize the SDMMC1 low level resources by implementing the HAL_SD_MspInit() API:
  29. (##) Call the function HAL_RCCEx_PeriphCLKConfig with RCC_PERIPHCLK_SDMMC1 for
  30. PeriphClockSelection and select SDMMC1 clock source (MSI, main PLL or PLLSAI1)
  31. (##) Enable the SDMMC1 interface clock using __HAL_RCC_SDMMC1_CLK_ENABLE();
  32. (##) SDMMC pins configuration for SD card
  33. (+++) Enable the clock for the SDMMC GPIOs using the functions __HAL_RCC_GPIOx_CLK_ENABLE();
  34. (+++) Configure these SDMMC pins as alternate function pull-up using HAL_GPIO_Init()
  35. and according to your pin assignment;
  36. (##) On STM32L4Rx/STM32L4Sxx devices, no DMA configuration is need, an internal DMA for SDMMC IP is used.
  37. (##) On other devices, perform DMA configuration if you need to use DMA process (HAL_SD_ReadBlocks_DMA()
  38. and HAL_SD_WriteBlocks_DMA() APIs).
  39. (+++) Enable the DMAx interface clock using __HAL_RCC_DMAx_CLK_ENABLE();
  40. (+++) Configure the DMA using the function HAL_DMA_Init() with predeclared and filled.
  41. (##) NVIC configuration if you need to use interrupt process when using DMA transfer.
  42. (+++) Configure the SDMMC and DMA interrupt priorities using functions
  43. HAL_NVIC_SetPriority(); DMA priority is superior to SDMMC's priority
  44. (+++) Enable the NVIC DMA and SDMMC IRQs using function HAL_NVIC_EnableIRQ()
  45. (+++) SDMMC interrupts are managed using the macros __HAL_SD_ENABLE_IT()
  46. and __HAL_SD_DISABLE_IT() inside the communication process.
  47. (+++) SDMMC interrupts pending bits are managed using the macros __HAL_SD_GET_IT()
  48. and __HAL_SD_CLEAR_IT()
  49. (##) NVIC configuration if you need to use interrupt process (HAL_SD_ReadBlocks_IT()
  50. and HAL_SD_WriteBlocks_IT() APIs).
  51. (+++) Configure the SDMMC interrupt priorities using function
  52. HAL_NVIC_SetPriority();
  53. (+++) Enable the NVIC SDMMC IRQs using function HAL_NVIC_EnableIRQ()
  54. (+++) SDMMC interrupts are managed using the macros __HAL_SD_ENABLE_IT()
  55. and __HAL_SD_DISABLE_IT() inside the communication process.
  56. (+++) SDMMC interrupts pending bits are managed using the macros __HAL_SD_GET_IT()
  57. and __HAL_SD_CLEAR_IT()
  58. (#) At this stage, you can perform SD read/write/erase operations after SD card initialization
  59. *** SD Card Initialization and configuration ***
  60. ================================================
  61. [..]
  62. To initialize the SD Card, use the HAL_SD_Init() function. It Initializes
  63. SDMMC IP(STM32 side) and the SD Card, and put it into StandBy State (Ready for data transfer).
  64. This function provide the following operations:
  65. (#) Initialize the SDMMC peripheral interface with defaullt configuration.
  66. The initialization process is done at 400KHz. You can change or adapt
  67. this frequency by adjusting the "ClockDiv" field.
  68. The SD Card frequency (SDMMC_CK) is computed as follows:
  69. SDMMC_CK = SDMMCCLK / (ClockDiv + 2)
  70. In initialization mode and according to the SD Card standard,
  71. make sure that the SDMMC_CK frequency doesn't exceed 400KHz.
  72. This phase of initialization is done through SDMMC_Init() and
  73. SDMMC_PowerState_ON() SDMMC low level APIs.
  74. (#) Initialize the SD card. The API used is HAL_SD_InitCard().
  75. This phase allows the card initialization and identification
  76. and check the SD Card type (Standard Capacity or High Capacity)
  77. The initialization flow is compatible with SD standard.
  78. This API (HAL_SD_InitCard()) could be used also to reinitialize the card in case
  79. of plug-off plug-in.
  80. (#) Configure the SD Card Data transfer frequency. By Default, the card transfer
  81. frequency is set to 24MHz. You can change or adapt this frequency by adjusting
  82. the "ClockDiv" field.
  83. In transfer mode and according to the SD Card standard, make sure that the
  84. SDMMC_CK frequency doesn't exceed 25MHz and 50MHz in High-speed mode switch.
  85. To be able to use a frequency higher than 24MHz, you should use the SDMMC
  86. peripheral in bypass mode. Refer to the corresponding reference manual
  87. for more details.
  88. (#) Select the corresponding SD Card according to the address read with the step 2.
  89. (#) Configure the SD Card in wide bus mode: 4-bits data.
  90. *** SD Card Read operation ***
  91. ==============================
  92. [..]
  93. (+) You can read from SD card in polling mode by using function HAL_SD_ReadBlocks().
  94. This function allows the read of 512 bytes blocks.
  95. You can choose either one block read operation or multiple block read operation
  96. by adjusting the "NumberOfBlocks" parameter.
  97. After this, you have to ensure that the transfer is done correctly. The check is done
  98. through HAL_SD_GetCardState() function for SD card state.
  99. (+) You can read from SD card in DMA mode by using function HAL_SD_ReadBlocks_DMA().
  100. This function allows the read of 512 bytes blocks.
  101. You can choose either one block read operation or multiple block read operation
  102. by adjusting the "NumberOfBlocks" parameter.
  103. After this, you have to ensure that the transfer is done correctly. The check is done
  104. through HAL_SD_GetCardState() function for SD card state.
  105. You could also check the DMA transfer process through the SD Rx interrupt event.
  106. (+) You can read from SD card in Interrupt mode by using function HAL_SD_ReadBlocks_IT().
  107. This function allows the read of 512 bytes blocks.
  108. You can choose either one block read operation or multiple block read operation
  109. by adjusting the "NumberOfBlocks" parameter.
  110. After this, you have to ensure that the transfer is done correctly. The check is done
  111. through HAL_SD_GetCardState() function for SD card state.
  112. You could also check the IT transfer process through the SD Rx interrupt event.
  113. *** SD Card Write operation ***
  114. ===============================
  115. [..]
  116. (+) You can write to SD card in polling mode by using function HAL_SD_WriteBlocks().
  117. This function allows the read of 512 bytes blocks.
  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 can write to SD card in DMA mode by using function HAL_SD_WriteBlocks_DMA().
  123. This function allows the read of 512 bytes blocks.
  124. You can choose either one block read operation or multiple block read operation
  125. by adjusting the "NumberOfBlocks" parameter.
  126. After this, you have to ensure that the transfer is done correctly. The check is done
  127. through HAL_SD_GetCardState() function for SD card state.
  128. You could also check the DMA transfer process through the SD Tx interrupt event.
  129. (+) You can write to SD card in Interrupt mode by using function HAL_SD_WriteBlocks_IT().
  130. This function allows the read of 512 bytes blocks.
  131. You can choose either one block read operation or multiple block read operation
  132. by adjusting the "NumberOfBlocks" parameter.
  133. After this, you have to ensure that the transfer is done correctly. The check is done
  134. through HAL_SD_GetCardState() function for SD card state.
  135. You could also check the IT transfer process through the SD Tx interrupt event.
  136. *** SD card status ***
  137. ======================
  138. [..]
  139. (+) The SD Status contains status bits that are related to the SD Memory
  140. Card proprietary features. To get SD card status use the HAL_SD_GetCardStatus().
  141. *** SD card information ***
  142. ===========================
  143. [..]
  144. (+) To get SD card information, you can use the function HAL_SD_GetCardInfo().
  145. It returns useful information about the SD card such as block size, card type,
  146. block number ...
  147. *** SD card CSD register ***
  148. ============================
  149. (+) The HAL_SD_GetCardCSD() API allows to get the parameters of the CSD register.
  150. Some of the CSD parameters are useful for card initialization and identification.
  151. *** SD card CID register ***
  152. ============================
  153. (+) The HAL_SD_GetCardCID() API allows to get the parameters of the CID register.
  154. Some of the CSD parameters are useful for card initialization and identification.
  155. *** SD HAL driver macros list ***
  156. ==================================
  157. [..]
  158. Below the list of most used macros in SD HAL driver.
  159. (+) __HAL_SD_ENABLE : Enable the SD device
  160. (+) __HAL_SD_DISABLE : Disable the SD device
  161. (+) __HAL_SD_DMA_ENABLE: Enable the SDMMC DMA transfer
  162. (+) __HAL_SD_DMA_DISABLE: Disable the SDMMC DMA transfer
  163. (+) __HAL_SD_ENABLE_IT: Enable the SD device interrupt
  164. (+) __HAL_SD_DISABLE_IT: Disable the SD device interrupt
  165. (+) __HAL_SD_GET_FLAG:Check whether the specified SD flag is set or not
  166. (+) __HAL_SD_CLEAR_FLAG: Clear the SD's pending flags
  167. (@) You can refer to the SD HAL driver header file for more useful macros
  168. @endverbatim
  169. ******************************************************************************
  170. * @attention
  171. *
  172. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  173. *
  174. * Redistribution and use in source and binary forms, with or without modification,
  175. * are permitted provided that the following conditions are met:
  176. * 1. Redistributions of source code must retain the above copyright notice,
  177. * this list of conditions and the following disclaimer.
  178. * 2. Redistributions in binary form must reproduce the above copyright notice,
  179. * this list of conditions and the following disclaimer in the documentation
  180. * and/or other materials provided with the distribution.
  181. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  182. * may be used to endorse or promote products derived from this software
  183. * without specific prior written permission.
  184. *
  185. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  186. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  187. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  188. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  189. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  190. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  191. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  192. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  193. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  194. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  195. *
  196. ******************************************************************************
  197. */
  198. /* Includes ------------------------------------------------------------------*/
  199. #include "stm32l4xx_hal.h"
  200. #if defined(SDMMC1)
  201. /** @addtogroup STM32L4xx_HAL_Driver
  202. * @{
  203. */
  204. /** @defgroup SD SD
  205. * @brief SD HAL module driver
  206. * @{
  207. */
  208. #ifdef HAL_SD_MODULE_ENABLED
  209. /* Private typedef -----------------------------------------------------------*/
  210. /* Private define ------------------------------------------------------------*/
  211. /** @addtogroup SD_Private_Defines
  212. * @{
  213. */
  214. /**
  215. * @}
  216. */
  217. /* Private macro -------------------------------------------------------------*/
  218. /* Private variables ---------------------------------------------------------*/
  219. /* Private function prototypes -----------------------------------------------*/
  220. /* Private functions ---------------------------------------------------------*/
  221. /** @defgroup SD_Private_Functions SD Private Functions
  222. * @{
  223. */
  224. static uint32_t SD_InitCard (SD_HandleTypeDef *hsd);
  225. static uint32_t SD_PowerON (SD_HandleTypeDef *hsd);
  226. static uint32_t SD_SendSDStatus (SD_HandleTypeDef *hsd, uint32_t *pSDstatus);
  227. static uint32_t SD_SendStatus (SD_HandleTypeDef *hsd, uint32_t *pCardStatus);
  228. static uint32_t SD_WideBus_Enable (SD_HandleTypeDef *hsd);
  229. static uint32_t SD_WideBus_Disable(SD_HandleTypeDef *hsd);
  230. static uint32_t SD_FindSCR (SD_HandleTypeDef *hsd, uint32_t *pSCR);
  231. static HAL_StatusTypeDef SD_PowerOFF (SD_HandleTypeDef *hsd);
  232. static HAL_StatusTypeDef SD_Write_IT (SD_HandleTypeDef *hsd);
  233. static HAL_StatusTypeDef SD_Read_IT (SD_HandleTypeDef *hsd);
  234. #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  235. static void SD_DMATransmitCplt(DMA_HandleTypeDef *hdma);
  236. static void SD_DMAReceiveCplt (DMA_HandleTypeDef *hdma);
  237. static void SD_DMAError (DMA_HandleTypeDef *hdma);
  238. static void SD_DMATxAbort (DMA_HandleTypeDef *hdma);
  239. static void SD_DMARxAbort (DMA_HandleTypeDef *hdma);
  240. #endif /* !STM32L4R5xx && !STM32L4R7xx && !STM32L4R9xx && !STM32L4S5xx && !STM32L4S7xx && !STM32L4S9xx */
  241. /**
  242. * @}
  243. */
  244. /* Exported functions --------------------------------------------------------*/
  245. /** @addtogroup SD_Exported_Functions
  246. * @{
  247. */
  248. /** @addtogroup SD_Exported_Functions_Group1
  249. * @brief Initialization and de-initialization functions
  250. *
  251. @verbatim
  252. ==============================================================================
  253. ##### Initialization and de-initialization functions #####
  254. ==============================================================================
  255. [..]
  256. This section provides functions allowing to initialize/de-initialize the SD
  257. card device to be ready for use.
  258. @endverbatim
  259. * @{
  260. */
  261. /**
  262. * @brief Initializes the SD according to the specified parameters in the
  263. SD_HandleTypeDef and create the associated handle.
  264. * @param hsd: Pointer to the SD handle
  265. * @retval HAL status
  266. */
  267. HAL_StatusTypeDef HAL_SD_Init(SD_HandleTypeDef *hsd)
  268. {
  269. /* Check the SD handle allocation */
  270. if(hsd == NULL)
  271. {
  272. return HAL_ERROR;
  273. }
  274. /* Check the parameters */
  275. assert_param(IS_SDMMC_ALL_INSTANCE(hsd->Instance));
  276. assert_param(IS_SDMMC_CLOCK_EDGE(hsd->Init.ClockEdge));
  277. #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  278. assert_param(IS_SDMMC_CLOCK_BYPASS(hsd->Init.ClockBypass));
  279. #endif /* !STM32L4R5xx && !STM32L4R7xx && !STM32L4R9xx && !STM32L4S5xx && !STM32L4S7xx && !STM32L4S9xx */
  280. assert_param(IS_SDMMC_CLOCK_POWER_SAVE(hsd->Init.ClockPowerSave));
  281. assert_param(IS_SDMMC_BUS_WIDE(hsd->Init.BusWide));
  282. assert_param(IS_SDMMC_HARDWARE_FLOW_CONTROL(hsd->Init.HardwareFlowControl));
  283. assert_param(IS_SDMMC_CLKDIV(hsd->Init.ClockDiv));
  284. if(hsd->State == HAL_SD_STATE_RESET)
  285. {
  286. /* Allocate lock resource and initialize it */
  287. hsd->Lock = HAL_UNLOCKED;
  288. /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
  289. HAL_SD_MspInit(hsd);
  290. }
  291. hsd->State = HAL_SD_STATE_BUSY;
  292. /* Initialize the Card parameters */
  293. if (HAL_SD_InitCard(hsd) != HAL_OK)
  294. {
  295. return HAL_ERROR;
  296. }
  297. #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  298. /* Configure the bus wide */
  299. if(HAL_SD_ConfigWideBusOperation(hsd, hsd->Init.BusWide) != HAL_OK)
  300. {
  301. return HAL_ERROR;
  302. }
  303. if(hsd->Init.Transceiver == SDMMC_TRANSCEIVER_ENABLE)
  304. {
  305. if((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) ||
  306. (hsd->SdCard.CardType == CARD_SDHC_SDXC))
  307. {
  308. hsd->Instance->CLKCR |= 0x00100000;
  309. /* Enable High Speed */
  310. if(HAL_SDEx_HighSpeed(hsd) != HAL_OK)
  311. {
  312. return HAL_ERROR;
  313. }
  314. }
  315. }
  316. #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  317. /* Initialize the error code */
  318. hsd->ErrorCode = HAL_SD_ERROR_NONE;
  319. /* Initialize the SD operation */
  320. hsd->Context = SD_CONTEXT_NONE;
  321. /* Initialize the SD state */
  322. hsd->State = HAL_SD_STATE_READY;
  323. return HAL_OK;
  324. }
  325. /**
  326. * @brief Initializes the SD Card.
  327. * @param hsd: Pointer to SD handle
  328. * @note This function initializes the SD card. It could be used when a card
  329. re-initialization is needed.
  330. * @retval HAL status
  331. */
  332. HAL_StatusTypeDef HAL_SD_InitCard(SD_HandleTypeDef *hsd)
  333. {
  334. uint32_t errorstate = HAL_SD_ERROR_NONE;
  335. HAL_StatusTypeDef status = HAL_OK;
  336. SD_InitTypeDef Init;
  337. /* Default SDMMC peripheral configuration for SD card initialization */
  338. Init.ClockEdge = SDMMC_CLOCK_EDGE_RISING;
  339. #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  340. Init.ClockBypass = SDMMC_CLOCK_BYPASS_DISABLE;
  341. #endif /* !STM32L4R5xx && !STM32L4R7xx && !STM32L4R9xx && !STM32L4S5xx && !STM32L4S7xx && !STM32L4S9xx */
  342. Init.ClockPowerSave = SDMMC_CLOCK_POWER_SAVE_DISABLE;
  343. Init.BusWide = SDMMC_BUS_WIDE_1B;
  344. Init.HardwareFlowControl = SDMMC_HARDWARE_FLOW_CONTROL_DISABLE;
  345. Init.ClockDiv = SDMMC_INIT_CLK_DIV;
  346. #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  347. if(hsd->Init.Transceiver == SDMMC_TRANSCEIVER_ENABLE)
  348. {
  349. /* Set Transceiver polarity */
  350. hsd->Instance->POWER |= SDMMC_POWER_DIRPOL;
  351. }
  352. #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  353. /* Initialize SDMMC peripheral interface with default configuration */
  354. status = SDMMC_Init(hsd->Instance, Init);
  355. if(status != HAL_OK)
  356. {
  357. return HAL_ERROR;
  358. }
  359. #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  360. /* Disable SDMMC Clock */
  361. __HAL_SD_DISABLE(hsd);
  362. #endif /* !STM32L4R5xx && !STM32L4R7xx && !STM32L4R9xx && !STM32L4S5xx && !STM32L4S7xx && !STM32L4S9xx */
  363. /* Set Power State to ON */
  364. status = SDMMC_PowerState_ON(hsd->Instance);
  365. if(status != HAL_OK)
  366. {
  367. return HAL_ERROR;
  368. }
  369. #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  370. /* Enable SDMMC Clock */
  371. __HAL_SD_ENABLE(hsd);
  372. #endif /* !STM32L4R5xx && !STM32L4R7xx && !STM32L4R9xx && !STM32L4S5xx && !STM32L4S7xx && !STM32L4S9xx */
  373. /* Required power up waiting time before starting the SD initialization sequence */
  374. HAL_Delay(2U);
  375. /* Identify card operating voltage */
  376. errorstate = SD_PowerON(hsd);
  377. if(errorstate != HAL_SD_ERROR_NONE)
  378. {
  379. hsd->State = HAL_SD_STATE_READY;
  380. hsd->ErrorCode |= errorstate;
  381. return HAL_ERROR;
  382. }
  383. /* Card initialization */
  384. errorstate = SD_InitCard(hsd);
  385. if(errorstate != HAL_SD_ERROR_NONE)
  386. {
  387. hsd->State = HAL_SD_STATE_READY;
  388. hsd->ErrorCode |= errorstate;
  389. return HAL_ERROR;
  390. }
  391. return HAL_OK;
  392. }
  393. /**
  394. * @brief De-Initializes the SD card.
  395. * @param hsd: Pointer to SD handle
  396. * @retval HAL status
  397. */
  398. HAL_StatusTypeDef HAL_SD_DeInit(SD_HandleTypeDef *hsd)
  399. {
  400. /* Check the SD handle allocation */
  401. if(hsd == NULL)
  402. {
  403. return HAL_ERROR;
  404. }
  405. /* Check the parameters */
  406. assert_param(IS_SDMMC_ALL_INSTANCE(hsd->Instance));
  407. hsd->State = HAL_SD_STATE_BUSY;
  408. /* Set SD power state to off */
  409. SD_PowerOFF(hsd);
  410. /* De-Initialize the MSP layer */
  411. HAL_SD_MspDeInit(hsd);
  412. hsd->ErrorCode = HAL_SD_ERROR_NONE;
  413. hsd->State = HAL_SD_STATE_RESET;
  414. return HAL_OK;
  415. }
  416. /**
  417. * @brief Initializes the SD MSP.
  418. * @param hsd: Pointer to SD handle
  419. * @retval None
  420. */
  421. __weak void HAL_SD_MspInit(SD_HandleTypeDef *hsd)
  422. {
  423. /* Prevent unused argument(s) compilation warning */
  424. UNUSED(hsd);
  425. /* NOTE : This function should not be modified, when the callback is needed,
  426. the HAL_SD_MspInit could be implemented in the user file
  427. */
  428. }
  429. /**
  430. * @brief De-Initialize SD MSP.
  431. * @param hsd: Pointer to SD handle
  432. * @retval None
  433. */
  434. __weak void HAL_SD_MspDeInit(SD_HandleTypeDef *hsd)
  435. {
  436. /* Prevent unused argument(s) compilation warning */
  437. UNUSED(hsd);
  438. /* NOTE : This function should not be modified, when the callback is needed,
  439. the HAL_SD_MspDeInit could be implemented in the user file
  440. */
  441. }
  442. /**
  443. * @}
  444. */
  445. /** @addtogroup SD_Exported_Functions_Group2
  446. * @brief Data transfer functions
  447. *
  448. @verbatim
  449. ==============================================================================
  450. ##### IO operation functions #####
  451. ==============================================================================
  452. [..]
  453. This subsection provides a set of functions allowing to manage the data
  454. transfer from/to SD card.
  455. @endverbatim
  456. * @{
  457. */
  458. /**
  459. * @brief Reads block(s) from a specified address in a card. The Data transfer
  460. * is managed by polling mode.
  461. * @note This API should be followed by a check on the card state through
  462. * HAL_SD_GetCardState().
  463. * @param hsd: Pointer to SD handle
  464. * @param pData: pointer to the buffer that will contain the received data
  465. * @param BlockAdd: Block Address from where data is to be read
  466. * @param NumberOfBlocks: Number of SD blocks to read
  467. * @param Timeout: Specify timeout value
  468. * @retval HAL status
  469. */
  470. HAL_StatusTypeDef HAL_SD_ReadBlocks(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout)
  471. {
  472. SDMMC_DataInitTypeDef config;
  473. uint32_t errorstate = HAL_SD_ERROR_NONE;
  474. uint32_t tickstart = HAL_GetTick();
  475. uint32_t count = 0, *tempbuff = (uint32_t *)pData;
  476. if(NULL == pData)
  477. {
  478. hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
  479. return HAL_ERROR;
  480. }
  481. if(hsd->State == HAL_SD_STATE_READY)
  482. {
  483. hsd->ErrorCode = HAL_SD_ERROR_NONE;
  484. if((BlockAdd + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
  485. {
  486. hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
  487. return HAL_ERROR;
  488. }
  489. hsd->State = HAL_SD_STATE_BUSY;
  490. /* Initialize data control register */
  491. hsd->Instance->DCTRL = 0;
  492. if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
  493. {
  494. BlockAdd *= 512;
  495. }
  496. /* Set Block Size for Card */
  497. errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
  498. if(errorstate != HAL_SD_ERROR_NONE)
  499. {
  500. /* Clear all the static flags */
  501. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  502. hsd->ErrorCode |= errorstate;
  503. hsd->State = HAL_SD_STATE_READY;
  504. return HAL_ERROR;
  505. }
  506. /* Configure the SD DPSM (Data Path State Machine) */
  507. config.DataTimeOut = SDMMC_DATATIMEOUT;
  508. config.DataLength = NumberOfBlocks * BLOCKSIZE;
  509. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
  510. config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
  511. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  512. #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  513. config.DPSM = SDMMC_DPSM_DISABLE;
  514. #else
  515. config.DPSM = SDMMC_DPSM_ENABLE;
  516. #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  517. SDMMC_ConfigData(hsd->Instance, &config);
  518. #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  519. __SDMMC_CMDTRANS_ENABLE( hsd->Instance);
  520. #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  521. /* Read block(s) in polling mode */
  522. if(NumberOfBlocks > 1)
  523. {
  524. hsd->Context = SD_CONTEXT_READ_MULTIPLE_BLOCK;
  525. /* Read Multi Block command */
  526. errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, BlockAdd);
  527. }
  528. else
  529. {
  530. hsd->Context = SD_CONTEXT_READ_SINGLE_BLOCK;
  531. /* Read Single Block command */
  532. errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, BlockAdd);
  533. }
  534. if(errorstate != HAL_SD_ERROR_NONE)
  535. {
  536. /* Clear all the static flags */
  537. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  538. hsd->ErrorCode |= errorstate;
  539. hsd->State = HAL_SD_STATE_READY;
  540. return HAL_ERROR;
  541. }
  542. /* Poll on SDMMC flags */
  543. while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DATAEND))
  544. {
  545. if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF))
  546. {
  547. /* Read data from SDMMC Rx FIFO */
  548. for(count = 0U; count < 8U; count++)
  549. {
  550. *(tempbuff + count) = SDMMC_ReadFIFO(hsd->Instance);
  551. }
  552. tempbuff += 8U;
  553. }
  554. if((Timeout == 0U)||((HAL_GetTick()-tickstart) >= Timeout))
  555. {
  556. /* Clear all the static flags */
  557. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  558. hsd->ErrorCode |= HAL_SD_ERROR_TIMEOUT;
  559. hsd->State= HAL_SD_STATE_READY;
  560. return HAL_TIMEOUT;
  561. }
  562. }
  563. #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  564. __SDMMC_CMDTRANS_DISABLE( hsd->Instance);
  565. #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  566. /* Send stop transmission command in case of multiblock read */
  567. if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DATAEND) && (NumberOfBlocks > 1U))
  568. {
  569. if(hsd->SdCard.CardType != CARD_SECURED)
  570. {
  571. /* Send stop transmission command */
  572. errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
  573. if(errorstate != HAL_SD_ERROR_NONE)
  574. {
  575. /* Clear all the static flags */
  576. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  577. hsd->ErrorCode |= errorstate;
  578. hsd->State = HAL_SD_STATE_READY;
  579. return HAL_ERROR;
  580. }
  581. }
  582. }
  583. /* Get error state */
  584. if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
  585. {
  586. /* Clear all the static flags */
  587. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  588. hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
  589. hsd->State = HAL_SD_STATE_READY;
  590. return HAL_ERROR;
  591. }
  592. else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
  593. {
  594. /* Clear all the static flags */
  595. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  596. hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
  597. hsd->State = HAL_SD_STATE_READY;
  598. return HAL_ERROR;
  599. }
  600. else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
  601. {
  602. /* Clear all the static flags */
  603. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  604. hsd->ErrorCode |= HAL_SD_ERROR_RX_OVERRUN;
  605. hsd->State = HAL_SD_STATE_READY;
  606. return HAL_ERROR;
  607. }
  608. #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  609. /* Empty FIFO if there is still any data */
  610. while ((__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXDAVL)))
  611. {
  612. *tempbuff = SDMMC_ReadFIFO(hsd->Instance);
  613. tempbuff++;
  614. if((Timeout == 0U)||((HAL_GetTick()-tickstart) >= Timeout))
  615. {
  616. /* Clear all the static flags */
  617. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  618. hsd->ErrorCode |= HAL_SD_ERROR_TIMEOUT;
  619. hsd->State= HAL_SD_STATE_READY;
  620. return HAL_ERROR;
  621. }
  622. }
  623. #endif /* !STM32L4R5xx && !STM32L4R7xx && !STM32L4R9xx && !STM32L4S5xx && !STM32L4S7xx && !STM32L4S9xx */
  624. /* Clear all the static flags */
  625. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
  626. hsd->State = HAL_SD_STATE_READY;
  627. return HAL_OK;
  628. }
  629. else
  630. {
  631. hsd->ErrorCode |= HAL_SD_ERROR_BUSY;
  632. return HAL_ERROR;
  633. }
  634. }
  635. /**
  636. * @brief Allows to write block(s) to a specified address in a card. The Data
  637. * transfer is managed by polling mode.
  638. * @note This API should be followed by a check on the card state through
  639. * HAL_SD_GetCardState().
  640. * @param hsd: Pointer to SD handle
  641. * @param pData: pointer to the buffer that will contain the data to transmit
  642. * @param BlockAdd: Block Address where data will be written
  643. * @param NumberOfBlocks: Number of SD blocks to write
  644. * @param Timeout: Specify timeout value
  645. * @retval HAL status
  646. */
  647. HAL_StatusTypeDef HAL_SD_WriteBlocks(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout)
  648. {
  649. SDMMC_DataInitTypeDef config;
  650. uint32_t errorstate = HAL_SD_ERROR_NONE;
  651. uint32_t tickstart = HAL_GetTick();
  652. uint32_t count = 0;
  653. uint32_t *tempbuff = (uint32_t *)pData;
  654. if(NULL == pData)
  655. {
  656. hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
  657. return HAL_ERROR;
  658. }
  659. if(hsd->State == HAL_SD_STATE_READY)
  660. {
  661. hsd->ErrorCode = HAL_SD_ERROR_NONE;
  662. if((BlockAdd + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
  663. {
  664. hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
  665. return HAL_ERROR;
  666. }
  667. hsd->State = HAL_SD_STATE_BUSY;
  668. /* Initialize data control register */
  669. hsd->Instance->DCTRL = 0;
  670. if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
  671. {
  672. BlockAdd *= 512;
  673. }
  674. /* Set Block Size for Card */
  675. errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
  676. if(errorstate != HAL_SD_ERROR_NONE)
  677. {
  678. /* Clear all the static flags */
  679. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  680. hsd->ErrorCode |= errorstate;
  681. hsd->State = HAL_SD_STATE_READY;
  682. return HAL_ERROR;
  683. }
  684. /* Configure the SD DPSM (Data Path State Machine) */
  685. config.DataTimeOut = SDMMC_DATATIMEOUT;
  686. config.DataLength = NumberOfBlocks * BLOCKSIZE;
  687. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
  688. config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
  689. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  690. #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  691. config.DPSM = SDMMC_DPSM_DISABLE;
  692. #else
  693. config.DPSM = SDMMC_DPSM_ENABLE;
  694. #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  695. SDMMC_ConfigData(hsd->Instance, &config);
  696. #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  697. __SDMMC_CMDTRANS_ENABLE( hsd->Instance);
  698. #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  699. /* Write Blocks in Polling mode */
  700. if(NumberOfBlocks > 1U)
  701. {
  702. hsd->Context = SD_CONTEXT_WRITE_MULTIPLE_BLOCK;
  703. /* Write Multi Block command */
  704. errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, BlockAdd);
  705. }
  706. else
  707. {
  708. hsd->Context = SD_CONTEXT_WRITE_SINGLE_BLOCK;
  709. /* Write Single Block command */
  710. errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, BlockAdd);
  711. }
  712. if(errorstate != HAL_SD_ERROR_NONE)
  713. {
  714. /* Clear all the static flags */
  715. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  716. hsd->ErrorCode |= errorstate;
  717. hsd->State = HAL_SD_STATE_READY;
  718. return HAL_ERROR;
  719. }
  720. /* Write block(s) in polling mode */
  721. while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXUNDERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DATAEND))
  722. {
  723. if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXFIFOHE))
  724. {
  725. /* Write data to SDMMC Tx FIFO */
  726. for(count = 0U; count < 8U; count++)
  727. {
  728. SDMMC_WriteFIFO(hsd->Instance, (tempbuff + count));
  729. }
  730. tempbuff += 8U;
  731. }
  732. if((Timeout == 0U)||((HAL_GetTick()-tickstart) >= Timeout))
  733. {
  734. /* Clear all the static flags */
  735. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  736. hsd->ErrorCode |= errorstate;
  737. hsd->State = HAL_SD_STATE_READY;
  738. return HAL_TIMEOUT;
  739. }
  740. }
  741. #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  742. __SDMMC_CMDTRANS_DISABLE( hsd->Instance);
  743. #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  744. /* Send stop transmission command in case of multiblock write */
  745. if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DATAEND) && (NumberOfBlocks > 1U))
  746. {
  747. if(hsd->SdCard.CardType != CARD_SECURED)
  748. {
  749. /* Send stop transmission command */
  750. errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
  751. if(errorstate != HAL_SD_ERROR_NONE)
  752. {
  753. /* Clear all the static flags */
  754. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  755. hsd->ErrorCode |= errorstate;
  756. hsd->State = HAL_SD_STATE_READY;
  757. return HAL_ERROR;
  758. }
  759. }
  760. }
  761. /* Get error state */
  762. if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
  763. {
  764. /* Clear all the static flags */
  765. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  766. hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
  767. hsd->State = HAL_SD_STATE_READY;
  768. return HAL_ERROR;
  769. }
  770. else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
  771. {
  772. /* Clear all the static flags */
  773. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  774. hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
  775. hsd->State = HAL_SD_STATE_READY;
  776. return HAL_ERROR;
  777. }
  778. else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXUNDERR))
  779. {
  780. /* Clear all the static flags */
  781. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  782. hsd->ErrorCode |= HAL_SD_ERROR_TX_UNDERRUN;
  783. hsd->State = HAL_SD_STATE_READY;
  784. return HAL_ERROR;
  785. }
  786. /* Clear all the static flags */
  787. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
  788. hsd->State = HAL_SD_STATE_READY;
  789. return HAL_OK;
  790. }
  791. else
  792. {
  793. hsd->ErrorCode |= HAL_SD_ERROR_BUSY;
  794. return HAL_ERROR;
  795. }
  796. }
  797. /**
  798. * @brief Reads block(s) from a specified address in a card. The Data transfer
  799. * is managed in interrupt mode.
  800. * @note This API should be followed by a check on the card state through
  801. * HAL_SD_GetCardState().
  802. * @note You could also check the IT transfer process through the SD Rx
  803. * interrupt event.
  804. * @param hsd: Pointer to SD handle
  805. * @param pData: Pointer to the buffer that will contain the received data
  806. * @param BlockAdd: Block Address from where data is to be read
  807. * @param NumberOfBlocks: Number of blocks to read.
  808. * @retval HAL status
  809. */
  810. HAL_StatusTypeDef HAL_SD_ReadBlocks_IT(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
  811. {
  812. SDMMC_DataInitTypeDef config;
  813. uint32_t errorstate = HAL_SD_ERROR_NONE;
  814. if(NULL == pData)
  815. {
  816. hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
  817. return HAL_ERROR;
  818. }
  819. if(hsd->State == HAL_SD_STATE_READY)
  820. {
  821. hsd->ErrorCode = HAL_SD_ERROR_NONE;
  822. if((BlockAdd + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
  823. {
  824. hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
  825. return HAL_ERROR;
  826. }
  827. hsd->State = HAL_SD_STATE_BUSY;
  828. /* Initialize data control register */
  829. hsd->Instance->DCTRL = 0U;
  830. hsd->pRxBuffPtr = (uint32_t *)pData;
  831. hsd->RxXferSize = BLOCKSIZE * NumberOfBlocks;
  832. __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND | SDMMC_FLAG_RXFIFOHF));
  833. if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
  834. {
  835. BlockAdd *= 512U;
  836. }
  837. /* Set Block Size for Card */
  838. errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
  839. if(errorstate != HAL_SD_ERROR_NONE)
  840. {
  841. /* Clear all the static flags */
  842. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  843. hsd->ErrorCode |= errorstate;
  844. hsd->State = HAL_SD_STATE_READY;
  845. return HAL_ERROR;
  846. }
  847. /* Configure the SD DPSM (Data Path State Machine) */
  848. config.DataTimeOut = SDMMC_DATATIMEOUT;
  849. config.DataLength = BLOCKSIZE * NumberOfBlocks;
  850. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
  851. config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
  852. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  853. #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  854. config.DPSM = SDMMC_DPSM_DISABLE;
  855. #else
  856. config.DPSM = SDMMC_DPSM_ENABLE;
  857. #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  858. SDMMC_ConfigData(hsd->Instance, &config);
  859. #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  860. __SDMMC_CMDTRANS_ENABLE( hsd->Instance);
  861. #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  862. /* Read Blocks in IT mode */
  863. if(NumberOfBlocks > 1U)
  864. {
  865. hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_IT);
  866. /* Read Multi Block command */
  867. errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, BlockAdd);
  868. }
  869. else
  870. {
  871. hsd->Context = (SD_CONTEXT_READ_SINGLE_BLOCK | SD_CONTEXT_IT);
  872. /* Read Single Block command */
  873. errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, BlockAdd);
  874. }
  875. if(errorstate != HAL_SD_ERROR_NONE)
  876. {
  877. /* Clear all the static flags */
  878. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  879. hsd->ErrorCode |= errorstate;
  880. hsd->State = HAL_SD_STATE_READY;
  881. return HAL_ERROR;
  882. }
  883. return HAL_OK;
  884. }
  885. else
  886. {
  887. return HAL_BUSY;
  888. }
  889. }
  890. /**
  891. * @brief Writes block(s) to a specified address in a card. The Data transfer
  892. * is managed in interrupt mode.
  893. * @note This API should be followed by a check on the card state through
  894. * HAL_SD_GetCardState().
  895. * @note You could also check the IT transfer process through the SD Tx
  896. * interrupt event.
  897. * @param hsd: Pointer to SD handle
  898. * @param pData: Pointer to the buffer that will contain the data to transmit
  899. * @param BlockAdd: Block Address where data will be written
  900. * @param NumberOfBlocks: Number of blocks to write
  901. * @retval HAL status
  902. */
  903. HAL_StatusTypeDef HAL_SD_WriteBlocks_IT(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
  904. {
  905. SDMMC_DataInitTypeDef config;
  906. uint32_t errorstate = HAL_SD_ERROR_NONE;
  907. if(NULL == pData)
  908. {
  909. hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
  910. return HAL_ERROR;
  911. }
  912. if(hsd->State == HAL_SD_STATE_READY)
  913. {
  914. hsd->ErrorCode = HAL_SD_ERROR_NONE;
  915. if((BlockAdd + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
  916. {
  917. hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
  918. return HAL_ERROR;
  919. }
  920. hsd->State = HAL_SD_STATE_BUSY;
  921. /* Initialize data control register */
  922. hsd->Instance->DCTRL = 0U;
  923. hsd->pTxBuffPtr = (uint32_t *)pData;
  924. hsd->TxXferSize = BLOCKSIZE * NumberOfBlocks;
  925. /* Enable transfer interrupts */
  926. __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR | SDMMC_IT_DATAEND | SDMMC_FLAG_TXFIFOHE));
  927. if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
  928. {
  929. BlockAdd *= 512U;
  930. }
  931. /* Set Block Size for Card */
  932. errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
  933. if(errorstate != HAL_SD_ERROR_NONE)
  934. {
  935. /* Clear all the static flags */
  936. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  937. hsd->ErrorCode |= errorstate;
  938. hsd->State = HAL_SD_STATE_READY;
  939. return HAL_ERROR;
  940. }
  941. #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  942. /* Configure the SD DPSM (Data Path State Machine) */
  943. config.DataTimeOut = SDMMC_DATATIMEOUT;
  944. config.DataLength = BLOCKSIZE * NumberOfBlocks;
  945. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
  946. config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
  947. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  948. config.DPSM = SDMMC_DPSM_DISABLE;
  949. SDMMC_ConfigData(hsd->Instance, &config);
  950. __SDMMC_CMDTRANS_ENABLE( hsd->Instance);
  951. #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  952. /* Write Blocks in Polling mode */
  953. if(NumberOfBlocks > 1U)
  954. {
  955. hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK| SD_CONTEXT_IT);
  956. /* Write Multi Block command */
  957. errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, BlockAdd);
  958. }
  959. else
  960. {
  961. hsd->Context = (SD_CONTEXT_WRITE_SINGLE_BLOCK | SD_CONTEXT_IT);
  962. /* Write Single Block command */
  963. errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, BlockAdd);
  964. }
  965. if(errorstate != HAL_SD_ERROR_NONE)
  966. {
  967. /* Clear all the static flags */
  968. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  969. hsd->ErrorCode |= errorstate;
  970. hsd->State = HAL_SD_STATE_READY;
  971. return HAL_ERROR;
  972. }
  973. #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  974. /* Configure the SD DPSM (Data Path State Machine) */
  975. config.DataTimeOut = SDMMC_DATATIMEOUT;
  976. config.DataLength = BLOCKSIZE * NumberOfBlocks;
  977. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
  978. config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
  979. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  980. config.DPSM = SDMMC_DPSM_ENABLE;
  981. SDMMC_ConfigData(hsd->Instance, &config);
  982. #endif /* !STM32L4R5xx && !STM32L4R7xx && !STM32L4R9xx && !STM32L4S5xx && !STM32L4S7xx && !STM32L4S9xx */
  983. return HAL_OK;
  984. }
  985. else
  986. {
  987. return HAL_BUSY;
  988. }
  989. }
  990. /**
  991. * @brief Reads block(s) from a specified address in a card. The Data transfer
  992. * is managed by DMA mode.
  993. * @note This API should be followed by a check on the card state through
  994. * HAL_SD_GetCardState().
  995. * @note You could also check the DMA transfer process through the SD Rx
  996. * interrupt event.
  997. * @param hsd: Pointer SD handle
  998. * @param pData: Pointer to the buffer that will contain the received data
  999. * @param BlockAdd: Block Address from where data is to be read
  1000. * @param NumberOfBlocks: Number of blocks to read.
  1001. * @retval HAL status
  1002. */
  1003. HAL_StatusTypeDef HAL_SD_ReadBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
  1004. {
  1005. SDMMC_DataInitTypeDef config;
  1006. uint32_t errorstate = HAL_SD_ERROR_NONE;
  1007. if(NULL == pData)
  1008. {
  1009. hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
  1010. return HAL_ERROR;
  1011. }
  1012. if(hsd->State == HAL_SD_STATE_READY)
  1013. {
  1014. hsd->ErrorCode = HAL_SD_ERROR_NONE;
  1015. if((BlockAdd + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
  1016. {
  1017. hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
  1018. return HAL_ERROR;
  1019. }
  1020. hsd->State = HAL_SD_STATE_BUSY;
  1021. /* Initialize data control register */
  1022. hsd->Instance->DCTRL = 0U;
  1023. #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  1024. __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND));
  1025. /* Set the DMA transfer complete callback */
  1026. hsd->hdmarx->XferCpltCallback = SD_DMAReceiveCplt;
  1027. /* Set the DMA error callback */
  1028. hsd->hdmarx->XferErrorCallback = SD_DMAError;
  1029. /* Set the DMA Abort callback */
  1030. hsd->hdmarx->XferAbortCallback = NULL;
  1031. /* Enable the DMA Channel */
  1032. HAL_DMA_Start_IT(hsd->hdmarx, (uint32_t)&hsd->Instance->FIFO, (uint32_t)pData, (uint32_t)(BLOCKSIZE * NumberOfBlocks)/4);
  1033. /* Enable SD DMA transfer */
  1034. __HAL_SD_DMA_ENABLE(hsd);
  1035. #else
  1036. hsd->pRxBuffPtr = (uint32_t*)pData;
  1037. hsd->RxXferSize = BLOCKSIZE * NumberOfBlocks;
  1038. #endif /* !STM32L4R5xx && !STM32L4R7xx && !STM32L4R9xx && !STM32L4S5xx && !STM32L4S7xx && !STM32L4S9xx */
  1039. if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
  1040. {
  1041. BlockAdd *= 512U;
  1042. }
  1043. /* Set Block Size for Card */
  1044. errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
  1045. if(errorstate != HAL_SD_ERROR_NONE)
  1046. {
  1047. /* Clear all the static flags */
  1048. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  1049. hsd->ErrorCode |= errorstate;
  1050. hsd->State = HAL_SD_STATE_READY;
  1051. return HAL_ERROR;
  1052. }
  1053. /* Configure the SD DPSM (Data Path State Machine) */
  1054. config.DataTimeOut = SDMMC_DATATIMEOUT;
  1055. config.DataLength = BLOCKSIZE * NumberOfBlocks;
  1056. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
  1057. config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
  1058. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  1059. #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  1060. config.DPSM = SDMMC_DPSM_DISABLE;
  1061. #else
  1062. config.DPSM = SDMMC_DPSM_ENABLE;
  1063. #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  1064. SDMMC_ConfigData(hsd->Instance, &config);
  1065. #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  1066. /* Enable transfer interrupts */
  1067. __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND));
  1068. __SDMMC_CMDTRANS_ENABLE( hsd->Instance);
  1069. hsd->Instance->IDMACTRL = SDMMC_ENABLE_IDMA_SINGLE_BUFF;
  1070. hsd->Instance->IDMABASE0 = (uint32_t) pData ;
  1071. #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  1072. /* Read Blocks in DMA mode */
  1073. if(NumberOfBlocks > 1U)
  1074. {
  1075. hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_DMA);
  1076. /* Read Multi Block command */
  1077. errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, BlockAdd);
  1078. }
  1079. else
  1080. {
  1081. hsd->Context = (SD_CONTEXT_READ_SINGLE_BLOCK | SD_CONTEXT_DMA);
  1082. /* Read Single Block command */
  1083. errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, BlockAdd);
  1084. }
  1085. if(errorstate != HAL_SD_ERROR_NONE)
  1086. {
  1087. /* Clear all the static flags */
  1088. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  1089. #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  1090. __HAL_SD_DISABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND));
  1091. #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  1092. hsd->ErrorCode |= errorstate;
  1093. hsd->State = HAL_SD_STATE_READY;
  1094. return HAL_ERROR;
  1095. }
  1096. return HAL_OK;
  1097. }
  1098. else
  1099. {
  1100. return HAL_BUSY;
  1101. }
  1102. }
  1103. /**
  1104. * @brief Writes block(s) to a specified address in a card. The Data transfer
  1105. * is managed by DMA mode.
  1106. * @note This API should be followed by a check on the card state through
  1107. * HAL_SD_GetCardState().
  1108. * @note You could also check the DMA transfer process through the SD Tx
  1109. * interrupt event.
  1110. * @param hsd: Pointer to SD handle
  1111. * @param pData: Pointer to the buffer that will contain the data to transmit
  1112. * @param BlockAdd: Block Address where data will be written
  1113. * @param NumberOfBlocks: Number of blocks to write
  1114. * @retval HAL status
  1115. */
  1116. HAL_StatusTypeDef HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
  1117. {
  1118. SDMMC_DataInitTypeDef config;
  1119. uint32_t errorstate = HAL_SD_ERROR_NONE;
  1120. if(NULL == pData)
  1121. {
  1122. hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
  1123. return HAL_ERROR;
  1124. }
  1125. if(hsd->State == HAL_SD_STATE_READY)
  1126. {
  1127. hsd->ErrorCode = HAL_SD_ERROR_NONE;
  1128. if((BlockAdd + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
  1129. {
  1130. hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
  1131. return HAL_ERROR;
  1132. }
  1133. hsd->State = HAL_SD_STATE_BUSY;
  1134. /* Initialize data control register */
  1135. hsd->Instance->DCTRL = 0U;
  1136. #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  1137. hsd->pTxBuffPtr = (uint32_t*)pData;
  1138. hsd->TxXferSize = BLOCKSIZE * NumberOfBlocks;
  1139. #else
  1140. /* Enable SD Error interrupts */
  1141. __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR));
  1142. /* Set the DMA transfer complete callback */
  1143. hsd->hdmatx->XferCpltCallback = SD_DMATransmitCplt;
  1144. /* Set the DMA error callback */
  1145. hsd->hdmatx->XferErrorCallback = SD_DMAError;
  1146. /* Set the DMA Abort callback */
  1147. hsd->hdmatx->XferAbortCallback = NULL;
  1148. #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  1149. if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
  1150. {
  1151. BlockAdd *= 512U;
  1152. }
  1153. /* Set Block Size for Card */
  1154. errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
  1155. if(errorstate != HAL_SD_ERROR_NONE)
  1156. {
  1157. /* Clear all the static flags */
  1158. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  1159. hsd->ErrorCode |= errorstate;
  1160. hsd->State = HAL_SD_STATE_READY;
  1161. return HAL_ERROR;
  1162. }
  1163. #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  1164. /* Configure the SD DPSM (Data Path State Machine) */
  1165. config.DataTimeOut = SDMMC_DATATIMEOUT;
  1166. config.DataLength = BLOCKSIZE * NumberOfBlocks;
  1167. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
  1168. config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
  1169. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  1170. config.DPSM = SDMMC_DPSM_DISABLE;
  1171. SDMMC_ConfigData(hsd->Instance, &config);
  1172. /* Enable transfer interrupts */
  1173. __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR | SDMMC_IT_DATAEND));
  1174. __SDMMC_CMDTRANS_ENABLE( hsd->Instance);
  1175. hsd->Instance->IDMACTRL = SDMMC_ENABLE_IDMA_SINGLE_BUFF;
  1176. hsd->Instance->IDMABASE0 = (uint32_t) pData ;
  1177. #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  1178. /* Write Blocks in Polling mode */
  1179. if(NumberOfBlocks > 1U)
  1180. {
  1181. hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK | SD_CONTEXT_DMA);
  1182. /* Write Multi Block command */
  1183. errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, BlockAdd);
  1184. }
  1185. else
  1186. {
  1187. hsd->Context = (SD_CONTEXT_WRITE_SINGLE_BLOCK | SD_CONTEXT_DMA);
  1188. /* Write Single Block command */
  1189. errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, BlockAdd);
  1190. }
  1191. if(errorstate != HAL_SD_ERROR_NONE)
  1192. {
  1193. /* Clear all the static flags */
  1194. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  1195. #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  1196. __HAL_SD_DISABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR | SDMMC_IT_DATAEND));
  1197. #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  1198. hsd->ErrorCode |= errorstate;
  1199. hsd->State = HAL_SD_STATE_READY;
  1200. return HAL_ERROR;
  1201. }
  1202. #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  1203. /* Enable SDMMC DMA transfer */
  1204. __HAL_SD_DMA_ENABLE(hsd);
  1205. /* Enable the DMA Channel */
  1206. HAL_DMA_Start_IT(hsd->hdmatx, (uint32_t)pData, (uint32_t)&hsd->Instance->FIFO, (uint32_t)(BLOCKSIZE * NumberOfBlocks)/4);
  1207. /* Configure the SD DPSM (Data Path State Machine) */
  1208. config.DataTimeOut = SDMMC_DATATIMEOUT;
  1209. config.DataLength = BLOCKSIZE * NumberOfBlocks;
  1210. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
  1211. config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
  1212. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  1213. config.DPSM = SDMMC_DPSM_ENABLE;
  1214. SDMMC_ConfigData(hsd->Instance, &config);
  1215. #endif /* !STM32L4R5xx && !STM32L4R7xx && !STM32L4R9xx && !STM32L4S5xx && !STM32L4S7xx && !STM32L4S9xx */
  1216. return HAL_OK;
  1217. }
  1218. else
  1219. {
  1220. return HAL_BUSY;
  1221. }
  1222. }
  1223. /**
  1224. * @brief Erases the specified memory area of the given SD card.
  1225. * @note This API should be followed by a check on the card state through
  1226. * HAL_SD_GetCardState().
  1227. * @param hsd: Pointer to SD handle
  1228. * @param BlockStartAdd: Start Block address
  1229. * @param BlockEndAdd: End Block address
  1230. * @retval HAL status
  1231. */
  1232. HAL_StatusTypeDef HAL_SD_Erase(SD_HandleTypeDef *hsd, uint32_t BlockStartAdd, uint32_t BlockEndAdd)
  1233. {
  1234. uint32_t errorstate = HAL_SD_ERROR_NONE;
  1235. if(hsd->State == HAL_SD_STATE_READY)
  1236. {
  1237. hsd->ErrorCode = HAL_SD_ERROR_NONE;
  1238. if(BlockEndAdd < BlockStartAdd)
  1239. {
  1240. hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
  1241. return HAL_ERROR;
  1242. }
  1243. if(BlockEndAdd > (hsd->SdCard.LogBlockNbr))
  1244. {
  1245. hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
  1246. return HAL_ERROR;
  1247. }
  1248. hsd->State = HAL_SD_STATE_BUSY;
  1249. /* Check if the card command class supports erase command */
  1250. if(((hsd->SdCard.Class) & SDMMC_CCCC_ERASE) == 0U)
  1251. {
  1252. /* Clear all the static flags */
  1253. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  1254. hsd->ErrorCode |= HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
  1255. hsd->State = HAL_SD_STATE_READY;
  1256. return HAL_ERROR;
  1257. }
  1258. if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
  1259. {
  1260. /* Clear all the static flags */
  1261. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  1262. hsd->ErrorCode |= HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
  1263. hsd->State = HAL_SD_STATE_READY;
  1264. return HAL_ERROR;
  1265. }
  1266. /* Get start and end block for high capacity cards */
  1267. if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
  1268. {
  1269. BlockStartAdd *= 512U;
  1270. BlockEndAdd *= 512U;
  1271. }
  1272. /* According to sd-card spec 1.0 ERASE_GROUP_START (CMD32) and erase_group_end(CMD33) */
  1273. if(hsd->SdCard.CardType != CARD_SECURED)
  1274. {
  1275. /* Send CMD32 SD_ERASE_GRP_START with argument as addr */
  1276. errorstate = SDMMC_CmdSDEraseStartAdd(hsd->Instance, BlockStartAdd);
  1277. if(errorstate != HAL_SD_ERROR_NONE)
  1278. {
  1279. /* Clear all the static flags */
  1280. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  1281. hsd->ErrorCode |= errorstate;
  1282. hsd->State = HAL_SD_STATE_READY;
  1283. return HAL_ERROR;
  1284. }
  1285. /* Send CMD33 SD_ERASE_GRP_END with argument as addr */
  1286. errorstate = SDMMC_CmdSDEraseEndAdd(hsd->Instance, BlockEndAdd);
  1287. if(errorstate != HAL_SD_ERROR_NONE)
  1288. {
  1289. /* Clear all the static flags */
  1290. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  1291. hsd->ErrorCode |= errorstate;
  1292. hsd->State = HAL_SD_STATE_READY;
  1293. return HAL_ERROR;
  1294. }
  1295. }
  1296. /* Send CMD38 ERASE */
  1297. errorstate = SDMMC_CmdErase(hsd->Instance);
  1298. if(errorstate != HAL_SD_ERROR_NONE)
  1299. {
  1300. /* Clear all the static flags */
  1301. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  1302. hsd->ErrorCode |= errorstate;
  1303. hsd->State = HAL_SD_STATE_READY;
  1304. return HAL_ERROR;
  1305. }
  1306. hsd->State = HAL_SD_STATE_READY;
  1307. return HAL_OK;
  1308. }
  1309. else
  1310. {
  1311. return HAL_BUSY;
  1312. }
  1313. }
  1314. /**
  1315. * @brief This function handles SD card interrupt request.
  1316. * @param hsd: Pointer to SD handle
  1317. * @retval None
  1318. */
  1319. void HAL_SD_IRQHandler(SD_HandleTypeDef *hsd)
  1320. {
  1321. uint32_t errorstate = HAL_SD_ERROR_NONE;
  1322. /* Check for SDMMC interrupt flags */
  1323. if(__HAL_SD_GET_FLAG(hsd, SDMMC_IT_DATAEND) != RESET)
  1324. {
  1325. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DATAEND);
  1326. __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT |\
  1327. SDMMC_IT_TXUNDERR | SDMMC_IT_RXOVERR | SDMMC_IT_TXFIFOHE |\
  1328. SDMMC_IT_RXFIFOHF);
  1329. #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  1330. __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_IDMABTC);
  1331. __SDMMC_CMDTRANS_DISABLE( hsd->Instance);
  1332. #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  1333. if((hsd->Context & SD_CONTEXT_IT) != RESET)
  1334. {
  1335. if(((hsd->Context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != RESET) || ((hsd->Context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != RESET))
  1336. {
  1337. errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
  1338. if(errorstate != HAL_SD_ERROR_NONE)
  1339. {
  1340. hsd->ErrorCode |= errorstate;
  1341. HAL_SD_ErrorCallback(hsd);
  1342. }
  1343. }
  1344. /* Clear all the static flags */
  1345. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
  1346. hsd->State = HAL_SD_STATE_READY;
  1347. if(((hsd->Context & SD_CONTEXT_READ_SINGLE_BLOCK) != RESET) || ((hsd->Context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != RESET))
  1348. {
  1349. HAL_SD_RxCpltCallback(hsd);
  1350. }
  1351. else
  1352. {
  1353. HAL_SD_TxCpltCallback(hsd);
  1354. }
  1355. }
  1356. else if((hsd->Context & SD_CONTEXT_DMA) != RESET)
  1357. {
  1358. #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  1359. hsd->Instance->DLEN = 0;
  1360. hsd->Instance->DCTRL = 0;
  1361. hsd->Instance->IDMACTRL = SDMMC_DISABLE_IDMA;
  1362. /* Stop Transfer for Write Single/Multi blocks or Read Multi blocks */
  1363. if((hsd->Context & SD_CONTEXT_READ_SINGLE_BLOCK) == RESET)
  1364. {
  1365. errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
  1366. if(errorstate != HAL_SD_ERROR_NONE)
  1367. {
  1368. hsd->ErrorCode |= errorstate;
  1369. HAL_SD_ErrorCallback(hsd);
  1370. }
  1371. }
  1372. hsd->State = HAL_SD_STATE_READY;
  1373. if(((hsd->Context & SD_CONTEXT_WRITE_SINGLE_BLOCK) != RESET) || ((hsd->Context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != RESET))
  1374. {
  1375. HAL_SD_TxCpltCallback(hsd);
  1376. }
  1377. if(((hsd->Context & SD_CONTEXT_READ_SINGLE_BLOCK) != RESET) || ((hsd->Context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != RESET))
  1378. {
  1379. HAL_SD_RxCpltCallback(hsd);
  1380. }
  1381. #else
  1382. if((hsd->Context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != RESET)
  1383. {
  1384. errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
  1385. if(errorstate != HAL_SD_ERROR_NONE)
  1386. {
  1387. hsd->ErrorCode |= errorstate;
  1388. HAL_SD_ErrorCallback(hsd);
  1389. }
  1390. }
  1391. if(((hsd->Context & SD_CONTEXT_READ_SINGLE_BLOCK) == RESET) && ((hsd->Context & SD_CONTEXT_READ_MULTIPLE_BLOCK) == RESET))
  1392. {
  1393. /* Disable the DMA transfer for transmit request by setting the DMAEN bit
  1394. in the SD DCTRL register */
  1395. hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDMMC_DCTRL_DMAEN);
  1396. hsd->State = HAL_SD_STATE_READY;
  1397. HAL_SD_TxCpltCallback(hsd);
  1398. }
  1399. #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  1400. }
  1401. }
  1402. else if(__HAL_SD_GET_FLAG(hsd, SDMMC_IT_TXFIFOHE) != RESET)
  1403. {
  1404. SD_Write_IT(hsd);
  1405. }
  1406. else if(__HAL_SD_GET_FLAG(hsd, SDMMC_IT_RXFIFOHF) != RESET)
  1407. {
  1408. SD_Read_IT(hsd);
  1409. }
  1410. else if(__HAL_SD_GET_FLAG(hsd, SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_TXUNDERR) != RESET)
  1411. {
  1412. /* Set Error code */
  1413. if(__HAL_SD_GET_FLAG(hsd, SDMMC_IT_DCRCFAIL) != RESET)
  1414. {
  1415. hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
  1416. }
  1417. if(__HAL_SD_GET_FLAG(hsd, SDMMC_IT_DTIMEOUT) != RESET)
  1418. {
  1419. hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
  1420. }
  1421. if(__HAL_SD_GET_FLAG(hsd, SDMMC_IT_RXOVERR) != RESET)
  1422. {
  1423. hsd->ErrorCode |= HAL_SD_ERROR_RX_OVERRUN;
  1424. }
  1425. if(__HAL_SD_GET_FLAG(hsd, SDMMC_IT_TXUNDERR) != RESET)
  1426. {
  1427. hsd->ErrorCode |= HAL_SD_ERROR_TX_UNDERRUN;
  1428. }
  1429. /* Clear All flags */
  1430. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
  1431. /* Disable all interrupts */
  1432. __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT|\
  1433. SDMMC_IT_TXUNDERR| SDMMC_IT_RXOVERR);
  1434. if((hsd->Context & SD_CONTEXT_IT) != RESET)
  1435. {
  1436. /* Set the SD state to ready to be able to start again the process */
  1437. hsd->State = HAL_SD_STATE_READY;
  1438. HAL_SD_ErrorCallback(hsd);
  1439. }
  1440. else if((hsd->Context & SD_CONTEXT_DMA) != RESET)
  1441. {
  1442. #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  1443. if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
  1444. {
  1445. /* Disable Internal DMA */
  1446. __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_IDMABTC);
  1447. hsd->Instance->IDMACTRL = SDMMC_DISABLE_IDMA;
  1448. /* Set the SD state to ready to be able to start again the process */
  1449. hsd->State = HAL_SD_STATE_READY;
  1450. HAL_SD_ErrorCallback(hsd);
  1451. }
  1452. #else
  1453. /* Abort the SD DMA channel */
  1454. if(hsd->hdmatx != NULL)
  1455. {
  1456. /* Set the DMA Tx abort callback */
  1457. hsd->hdmatx->XferAbortCallback = SD_DMATxAbort;
  1458. /* Abort DMA in IT mode */
  1459. if(HAL_DMA_Abort_IT(hsd->hdmatx) != HAL_OK)
  1460. {
  1461. SD_DMATxAbort(hsd->hdmatx);
  1462. }
  1463. }
  1464. else if(hsd->hdmarx != NULL)
  1465. {
  1466. /* Set the DMA Rx abort callback */
  1467. hsd->hdmarx->XferAbortCallback = SD_DMARxAbort;
  1468. /* Abort DMA in IT mode */
  1469. if(HAL_DMA_Abort_IT(hsd->hdmarx) != HAL_OK)
  1470. {
  1471. SD_DMARxAbort(hsd->hdmarx);
  1472. }
  1473. }
  1474. else
  1475. {
  1476. hsd->ErrorCode = HAL_SD_ERROR_NONE;
  1477. hsd->State = HAL_SD_STATE_READY;
  1478. HAL_SD_AbortCallback(hsd);
  1479. }
  1480. #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  1481. }
  1482. }
  1483. #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  1484. else if(__HAL_SD_GET_FLAG(hsd, SDMMC_IT_IDMABTC) != RESET)
  1485. {
  1486. if(READ_BIT(hsd->Instance->IDMACTRL, SDMMC_IDMA_IDMABACT) == SD_DMA_BUFFER0)
  1487. {
  1488. /* Current buffer is buffer0, Transfer complete for buffer1 */
  1489. if((hsd->Context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != RESET)
  1490. {
  1491. HAL_SDEx_Write_DMADoubleBuffer1CpltCallback(hsd);
  1492. }
  1493. else /* SD_CONTEXT_READ_MULTIPLE_BLOCK */
  1494. {
  1495. HAL_SDEx_Read_DMADoubleBuffer1CpltCallback(hsd);
  1496. }
  1497. }
  1498. else /* SD_DMA_BUFFER1 */
  1499. {
  1500. /* Current buffer is buffer1, Transfer complete for buffer0 */
  1501. if((hsd->Context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != RESET)
  1502. {
  1503. HAL_SDEx_Write_DMADoubleBuffer0CpltCallback(hsd);
  1504. }
  1505. else /* SD_CONTEXT_READ_MULTIPLE_BLOCK */
  1506. {
  1507. HAL_SDEx_Read_DMADoubleBuffer0CpltCallback(hsd);
  1508. }
  1509. }
  1510. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_IDMABTC);
  1511. }
  1512. #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  1513. }
  1514. /**
  1515. * @brief return the SD state
  1516. * @param hsd: Pointer to sd handle
  1517. * @retval HAL state
  1518. */
  1519. HAL_SD_StateTypeDef HAL_SD_GetState(SD_HandleTypeDef *hsd)
  1520. {
  1521. return hsd->State;
  1522. }
  1523. /**
  1524. * @brief Return the SD error code
  1525. * @param hsd : Pointer to a SD_HandleTypeDef structure that contains
  1526. * the configuration information.
  1527. * @retval SD Error Code
  1528. */
  1529. uint32_t HAL_SD_GetError(SD_HandleTypeDef *hsd)
  1530. {
  1531. return hsd->ErrorCode;
  1532. }
  1533. /**
  1534. * @brief Tx Transfer completed callbacks
  1535. * @param hsd: Pointer to SD handle
  1536. * @retval None
  1537. */
  1538. __weak void HAL_SD_TxCpltCallback(SD_HandleTypeDef *hsd)
  1539. {
  1540. /* Prevent unused argument(s) compilation warning */
  1541. UNUSED(hsd);
  1542. /* NOTE : This function should not be modified, when the callback is needed,
  1543. the HAL_SD_TxCpltCallback can be implemented in the user file
  1544. */
  1545. }
  1546. /**
  1547. * @brief Rx Transfer completed callbacks
  1548. * @param hsd: Pointer SD handle
  1549. * @retval None
  1550. */
  1551. __weak void HAL_SD_RxCpltCallback(SD_HandleTypeDef *hsd)
  1552. {
  1553. /* Prevent unused argument(s) compilation warning */
  1554. UNUSED(hsd);
  1555. /* NOTE : This function should not be modified, when the callback is needed,
  1556. the HAL_SD_RxCpltCallback can be implemented in the user file
  1557. */
  1558. }
  1559. /**
  1560. * @brief SD error callbacks
  1561. * @param hsd: Pointer SD handle
  1562. * @retval None
  1563. */
  1564. __weak void HAL_SD_ErrorCallback(SD_HandleTypeDef *hsd)
  1565. {
  1566. /* Prevent unused argument(s) compilation warning */
  1567. UNUSED(hsd);
  1568. /* NOTE : This function should not be modified, when the callback is needed,
  1569. the HAL_SD_ErrorCallback can be implemented in the user file
  1570. */
  1571. }
  1572. /**
  1573. * @brief SD Abort callbacks
  1574. * @param hsd: Pointer SD handle
  1575. * @retval None
  1576. */
  1577. __weak void HAL_SD_AbortCallback(SD_HandleTypeDef *hsd)
  1578. {
  1579. /* Prevent unused argument(s) compilation warning */
  1580. UNUSED(hsd);
  1581. /* NOTE : This function should not be modified, when the callback is needed,
  1582. the HAL_SD_ErrorCallback can be implemented in the user file
  1583. */
  1584. }
  1585. /**
  1586. * @}
  1587. */
  1588. /** @addtogroup SD_Exported_Functions_Group3
  1589. * @brief management functions
  1590. *
  1591. @verbatim
  1592. ==============================================================================
  1593. ##### Peripheral Control functions #####
  1594. ==============================================================================
  1595. [..]
  1596. This subsection provides a set of functions allowing to control the SD card
  1597. operations and get the related information
  1598. @endverbatim
  1599. * @{
  1600. */
  1601. /**
  1602. * @brief Returns information the information of the card which are stored on
  1603. * the CID register.
  1604. * @param hsd: Pointer to SD handle
  1605. * @param pCID: Pointer to a HAL_SD_CIDTypedef structure that
  1606. * contains all CID register parameters
  1607. * @retval HAL status
  1608. */
  1609. HAL_StatusTypeDef HAL_SD_GetCardCID(SD_HandleTypeDef *hsd, HAL_SD_CardCIDTypedef *pCID)
  1610. {
  1611. uint32_t tmp = 0;
  1612. /* Byte 0 */
  1613. tmp = (uint8_t)((hsd->CID[0] & 0xFF000000U) >> 24);
  1614. pCID->ManufacturerID = tmp;
  1615. /* Byte 1 */
  1616. tmp = (uint8_t)((hsd->CID[0] & 0x00FF0000) >> 16);
  1617. pCID->OEM_AppliID = tmp << 8;
  1618. /* Byte 2 */
  1619. tmp = (uint8_t)((hsd->CID[0] & 0x0000FF00) >> 8);
  1620. pCID->OEM_AppliID |= tmp;
  1621. /* Byte 3 */
  1622. tmp = (uint8_t)(hsd->CID[0] & 0x000000FF);
  1623. pCID->ProdName1 = tmp << 24;
  1624. /* Byte 4 */
  1625. tmp = (uint8_t)((hsd->CID[1] & 0xFF000000U) >> 24);
  1626. pCID->ProdName1 |= tmp << 16;
  1627. /* Byte 5 */
  1628. tmp = (uint8_t)((hsd->CID[1] & 0x00FF0000) >> 16);
  1629. pCID->ProdName1 |= tmp << 8;
  1630. /* Byte 6 */
  1631. tmp = (uint8_t)((hsd->CID[1] & 0x0000FF00) >> 8);
  1632. pCID->ProdName1 |= tmp;
  1633. /* Byte 7 */
  1634. tmp = (uint8_t)(hsd->CID[1] & 0x000000FF);
  1635. pCID->ProdName2 = tmp;
  1636. /* Byte 8 */
  1637. tmp = (uint8_t)((hsd->CID[2] & 0xFF000000U) >> 24);
  1638. pCID->ProdRev = tmp;
  1639. /* Byte 9 */
  1640. tmp = (uint8_t)((hsd->CID[2] & 0x00FF0000) >> 16);
  1641. pCID->ProdSN = tmp << 24;
  1642. /* Byte 10 */
  1643. tmp = (uint8_t)((hsd->CID[2] & 0x0000FF00) >> 8);
  1644. pCID->ProdSN |= tmp << 16;
  1645. /* Byte 11 */
  1646. tmp = (uint8_t)(hsd->CID[2] & 0x000000FF);
  1647. pCID->ProdSN |= tmp << 8;
  1648. /* Byte 12 */
  1649. tmp = (uint8_t)((hsd->CID[3] & 0xFF000000U) >> 24);
  1650. pCID->ProdSN |= tmp;
  1651. /* Byte 13 */
  1652. tmp = (uint8_t)((hsd->CID[3] & 0x00FF0000) >> 16);
  1653. pCID->Reserved1 |= (tmp & 0xF0) >> 4;
  1654. pCID->ManufactDate = (tmp & 0x0F) << 8;
  1655. /* Byte 14 */
  1656. tmp = (uint8_t)((hsd->CID[3] & 0x0000FF00) >> 8);
  1657. pCID->ManufactDate |= tmp;
  1658. /* Byte 15 */
  1659. tmp = (uint8_t)(hsd->CID[3] & 0x000000FF);
  1660. pCID->CID_CRC = (tmp & 0xFE) >> 1;
  1661. pCID->Reserved2 = 1;
  1662. return HAL_OK;
  1663. }
  1664. /**
  1665. * @brief Returns information the information of the card which are stored on
  1666. * the CSD register.
  1667. * @param hsd: Pointer to SD handle
  1668. * @param pCSD: Pointer to a HAL_SD_CardInfoTypedef structure that
  1669. * contains all CSD register parameters
  1670. * @retval HAL status
  1671. */
  1672. HAL_StatusTypeDef HAL_SD_GetCardCSD(SD_HandleTypeDef *hsd, HAL_SD_CardCSDTypedef *pCSD)
  1673. {
  1674. uint32_t tmp = 0;
  1675. /* Byte 0 */
  1676. tmp = (hsd->CSD[0] & 0xFF000000U) >> 24;
  1677. pCSD->CSDStruct = (uint8_t)((tmp & 0xC0) >> 6);
  1678. pCSD->SysSpecVersion = (uint8_t)((tmp & 0x3C) >> 2);
  1679. pCSD->Reserved1 = tmp & 0x03;
  1680. /* Byte 1 */
  1681. tmp = (hsd->CSD[0] & 0x00FF0000) >> 16;
  1682. pCSD->TAAC = (uint8_t)tmp;
  1683. /* Byte 2 */
  1684. tmp = (hsd->CSD[0] & 0x0000FF00) >> 8;
  1685. pCSD->NSAC = (uint8_t)tmp;
  1686. /* Byte 3 */
  1687. tmp = hsd->CSD[0] & 0x000000FF;
  1688. pCSD->MaxBusClkFrec = (uint8_t)tmp;
  1689. /* Byte 4 */
  1690. tmp = (hsd->CSD[1] & 0xFF000000U) >> 24;
  1691. pCSD->CardComdClasses = (uint16_t)(tmp << 4);
  1692. /* Byte 5 */
  1693. tmp = (hsd->CSD[1] & 0x00FF0000U) >> 16;
  1694. pCSD->CardComdClasses |= (uint16_t)((tmp & 0xF0) >> 4);
  1695. pCSD->RdBlockLen = (uint8_t)(tmp & 0x0F);
  1696. /* Byte 6 */
  1697. tmp = (hsd->CSD[1] & 0x0000FF00U) >> 8;
  1698. pCSD->PartBlockRead = (uint8_t)((tmp & 0x80) >> 7);
  1699. pCSD->WrBlockMisalign = (uint8_t)((tmp & 0x40) >> 6);
  1700. pCSD->RdBlockMisalign = (uint8_t)((tmp & 0x20) >> 5);
  1701. pCSD->DSRImpl = (uint8_t)((tmp & 0x10) >> 4);
  1702. pCSD->Reserved2 = 0; /*!< Reserved */
  1703. if(hsd->SdCard.CardType == CARD_SDSC)
  1704. {
  1705. pCSD->DeviceSize = (tmp & 0x03) << 10;
  1706. /* Byte 7 */
  1707. tmp = (uint8_t)(hsd->CSD[1] & 0x000000FFU);
  1708. pCSD->DeviceSize |= (tmp) << 2;
  1709. /* Byte 8 */
  1710. tmp = (uint8_t)((hsd->CSD[2] & 0xFF000000U) >> 24);
  1711. pCSD->DeviceSize |= (tmp & 0xC0) >> 6;
  1712. pCSD->MaxRdCurrentVDDMin = (tmp & 0x38) >> 3;
  1713. pCSD->MaxRdCurrentVDDMax = (tmp & 0x07);
  1714. /* Byte 9 */
  1715. tmp = (uint8_t)((hsd->CSD[2] & 0x00FF0000U) >> 16);
  1716. pCSD->MaxWrCurrentVDDMin = (tmp & 0xE0) >> 5;
  1717. pCSD->MaxWrCurrentVDDMax = (tmp & 0x1C) >> 2;
  1718. pCSD->DeviceSizeMul = (tmp & 0x03) << 1;
  1719. /* Byte 10 */
  1720. tmp = (uint8_t)((hsd->CSD[2] & 0x0000FF00U) >> 8);
  1721. pCSD->DeviceSizeMul |= (tmp & 0x80) >> 7;
  1722. hsd->SdCard.BlockNbr = (pCSD->DeviceSize + 1) ;
  1723. hsd->SdCard.BlockNbr *= (1 << (pCSD->DeviceSizeMul + 2));
  1724. hsd->SdCard.BlockSize = 1 << (pCSD->RdBlockLen);
  1725. hsd->SdCard.LogBlockNbr = (hsd->SdCard.BlockNbr) * ((hsd->SdCard.BlockSize) / 512);
  1726. hsd->SdCard.LogBlockSize = 512;
  1727. }
  1728. else if(hsd->SdCard.CardType == CARD_SDHC_SDXC)
  1729. {
  1730. /* Byte 7 */
  1731. tmp = (uint8_t)(hsd->CSD[1] & 0x000000FFU);
  1732. pCSD->DeviceSize = (tmp & 0x3F) << 16;
  1733. /* Byte 8 */
  1734. tmp = (uint8_t)((hsd->CSD[2] & 0xFF000000U) >> 24);
  1735. pCSD->DeviceSize |= (tmp << 8);
  1736. /* Byte 9 */
  1737. tmp = (uint8_t)((hsd->CSD[2] & 0x00FF0000U) >> 16);
  1738. pCSD->DeviceSize |= (tmp);
  1739. /* Byte 10 */
  1740. tmp = (uint8_t)((hsd->CSD[2] & 0x0000FF00U) >> 8);
  1741. hsd->SdCard.LogBlockNbr = hsd->SdCard.BlockNbr = (((uint64_t)pCSD->DeviceSize + 1) * 1024);
  1742. hsd->SdCard.LogBlockSize = hsd->SdCard.BlockSize = 512;
  1743. }
  1744. else
  1745. {
  1746. /* Clear all the static flags */
  1747. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  1748. hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
  1749. hsd->State = HAL_SD_STATE_READY;
  1750. return HAL_ERROR;
  1751. }
  1752. pCSD->EraseGrSize = (tmp & 0x40) >> 6;
  1753. pCSD->EraseGrMul = (tmp & 0x3F) << 1;
  1754. /* Byte 11 */
  1755. tmp = (uint8_t)(hsd->CSD[2] & 0x000000FF);
  1756. pCSD->EraseGrMul |= (tmp & 0x80) >> 7;
  1757. pCSD->WrProtectGrSize = (tmp & 0x7F);
  1758. /* Byte 12 */
  1759. tmp = (uint8_t)((hsd->CSD[3] & 0xFF000000U) >> 24);
  1760. pCSD->WrProtectGrEnable = (tmp & 0x80) >> 7;
  1761. pCSD->ManDeflECC = (tmp & 0x60) >> 5;
  1762. pCSD->WrSpeedFact = (tmp & 0x1C) >> 2;
  1763. pCSD->MaxWrBlockLen = (tmp & 0x03) << 2;
  1764. /* Byte 13 */
  1765. tmp = (uint8_t)((hsd->CSD[3] & 0x00FF0000) >> 16);
  1766. pCSD->MaxWrBlockLen |= (tmp & 0xC0) >> 6;
  1767. pCSD->WriteBlockPaPartial = (tmp & 0x20) >> 5;
  1768. pCSD->Reserved3 = 0;
  1769. pCSD->ContentProtectAppli = (tmp & 0x01);
  1770. /* Byte 14 */
  1771. tmp = (uint8_t)((hsd->CSD[3] & 0x0000FF00) >> 8);
  1772. pCSD->FileFormatGrouop = (tmp & 0x80) >> 7;
  1773. pCSD->CopyFlag = (tmp & 0x40) >> 6;
  1774. pCSD->PermWrProtect = (tmp & 0x20) >> 5;
  1775. pCSD->TempWrProtect = (tmp & 0x10) >> 4;
  1776. pCSD->FileFormat = (tmp & 0x0C) >> 2;
  1777. pCSD->ECC = (tmp & 0x03);
  1778. /* Byte 15 */
  1779. tmp = (uint8_t)(hsd->CSD[3] & 0x000000FF);
  1780. pCSD->CSD_CRC = (tmp & 0xFE) >> 1;
  1781. pCSD->Reserved4 = 1;
  1782. return HAL_OK;
  1783. }
  1784. /**
  1785. * @brief Gets the SD status info.
  1786. * @param hsd: Pointer to SD handle
  1787. * @param pStatus: Pointer to the HAL_SD_CardStatusTypedef structure that
  1788. * will contain the SD card status information
  1789. * @retval HAL status
  1790. */
  1791. HAL_StatusTypeDef HAL_SD_GetCardStatus(SD_HandleTypeDef *hsd, HAL_SD_CardStatusTypedef *pStatus)
  1792. {
  1793. uint32_t tmp = 0;
  1794. uint32_t sd_status[16];
  1795. uint32_t errorstate = HAL_SD_ERROR_NONE;
  1796. errorstate = SD_SendSDStatus(hsd, sd_status);
  1797. if(errorstate != HAL_OK)
  1798. {
  1799. /* Clear all the static flags */
  1800. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  1801. hsd->ErrorCode |= errorstate;
  1802. hsd->State = HAL_SD_STATE_READY;
  1803. return HAL_ERROR;
  1804. }
  1805. else
  1806. {
  1807. /* Byte 0 */
  1808. tmp = (sd_status[0] & 0xC0) >> 6;
  1809. pStatus->DataBusWidth = (uint8_t)tmp;
  1810. /* Byte 0 */
  1811. tmp = (sd_status[0] & 0x20) >> 5;
  1812. pStatus->SecuredMode = (uint8_t)tmp;
  1813. /* Byte 2 */
  1814. tmp = (sd_status[0] & 0x00FF0000U) >> 16;
  1815. pStatus->CardType = (uint16_t)(tmp << 8);
  1816. /* Byte 3 */
  1817. tmp = (sd_status[0] & 0xFF000000U) >> 24;
  1818. pStatus->CardType |= (uint16_t)tmp;
  1819. /* Byte 4 */
  1820. tmp = (sd_status[1] & 0xFF);
  1821. pStatus->ProtectedAreaSize = (uint32_t)(tmp << 24);
  1822. /* Byte 5 */
  1823. tmp = (sd_status[1] & 0xFF00) >> 8;
  1824. pStatus->ProtectedAreaSize |= (uint32_t)(tmp << 16);
  1825. /* Byte 6 */
  1826. tmp = (sd_status[1] & 0xFF0000) >> 16;
  1827. pStatus->ProtectedAreaSize |= (uint32_t)(tmp << 8);
  1828. /* Byte 7 */
  1829. tmp = (sd_status[1] & 0xFF000000U) >> 24;
  1830. pStatus->ProtectedAreaSize |= (uint32_t)tmp;
  1831. /* Byte 8 */
  1832. tmp = (sd_status[2] & 0xFF);
  1833. pStatus->SpeedClass = (uint8_t)tmp;
  1834. /* Byte 9 */
  1835. tmp = (sd_status[2] & 0xFF00) >> 8;
  1836. pStatus->PerformanceMove = (uint8_t)tmp;
  1837. /* Byte 10 */
  1838. tmp = (sd_status[2] & 0xF00000) >> 20;
  1839. pStatus->AllocationUnitSize = (uint8_t)tmp;
  1840. /* Byte 11 */
  1841. tmp = (sd_status[2] & 0xFF000000U) >> 24;
  1842. pStatus->EraseSize = (uint16_t)(tmp << 8);
  1843. /* Byte 12 */
  1844. tmp = (sd_status[3] & 0xFF);
  1845. pStatus->EraseSize |= (uint16_t)tmp;
  1846. /* Byte 13 */
  1847. tmp = (sd_status[3] & 0xFC00) >> 10;
  1848. pStatus->EraseTimeout = (uint8_t)tmp;
  1849. /* Byte 13 */
  1850. tmp = (sd_status[3] & 0x0300) >> 8;
  1851. pStatus->EraseOffset = (uint8_t)tmp;
  1852. }
  1853. return HAL_OK;
  1854. }
  1855. /**
  1856. * @brief Gets the SD card info.
  1857. * @param hsd: Pointer to SD handle
  1858. * @param pCardInfo: Pointer to the HAL_SD_CardInfoTypeDef structure that
  1859. * will contain the SD card status information
  1860. * @retval HAL status
  1861. */
  1862. HAL_StatusTypeDef HAL_SD_GetCardInfo(SD_HandleTypeDef *hsd, HAL_SD_CardInfoTypeDef *pCardInfo)
  1863. {
  1864. pCardInfo->CardType = (uint32_t)(hsd->SdCard.CardType);
  1865. pCardInfo->CardVersion = (uint32_t)(hsd->SdCard.CardVersion);
  1866. pCardInfo->Class = (uint32_t)(hsd->SdCard.Class);
  1867. pCardInfo->RelCardAdd = (uint32_t)(hsd->SdCard.RelCardAdd);
  1868. pCardInfo->BlockNbr = (uint32_t)(hsd->SdCard.BlockNbr);
  1869. pCardInfo->BlockSize = (uint32_t)(hsd->SdCard.BlockSize);
  1870. pCardInfo->LogBlockNbr = (uint32_t)(hsd->SdCard.LogBlockNbr);
  1871. pCardInfo->LogBlockSize = (uint32_t)(hsd->SdCard.LogBlockSize);
  1872. return HAL_OK;
  1873. }
  1874. /**
  1875. * @brief Enables wide bus operation for the requested card if supported by
  1876. * card.
  1877. * @param hsd: Pointer to SD handle
  1878. * @param WideMode: Specifies the SD card wide bus mode
  1879. * This parameter can be one of the following values:
  1880. * @arg SDMMC_BUS_WIDE_8B: 8-bit data transfer
  1881. * @arg SDMMC_BUS_WIDE_4B: 4-bit data transfer
  1882. * @arg SDMMC_BUS_WIDE_1B: 1-bit data transfer
  1883. * @retval HAL status
  1884. */
  1885. HAL_StatusTypeDef HAL_SD_ConfigWideBusOperation(SD_HandleTypeDef *hsd, uint32_t WideMode)
  1886. {
  1887. SDMMC_InitTypeDef Init;
  1888. uint32_t errorstate = HAL_SD_ERROR_NONE;
  1889. /* Check the parameters */
  1890. assert_param(IS_SDMMC_BUS_WIDE(WideMode));
  1891. /* Change State */
  1892. hsd->State = HAL_SD_STATE_BUSY;
  1893. if(hsd->SdCard.CardType != CARD_SECURED)
  1894. {
  1895. if(WideMode == SDMMC_BUS_WIDE_8B)
  1896. {
  1897. hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
  1898. }
  1899. else if(WideMode == SDMMC_BUS_WIDE_4B)
  1900. {
  1901. errorstate = SD_WideBus_Enable(hsd);
  1902. hsd->ErrorCode |= errorstate;
  1903. }
  1904. else if(WideMode == SDMMC_BUS_WIDE_1B)
  1905. {
  1906. errorstate = SD_WideBus_Disable(hsd);
  1907. hsd->ErrorCode |= errorstate;
  1908. }
  1909. else
  1910. {
  1911. /* WideMode is not a valid argument*/
  1912. hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
  1913. }
  1914. }
  1915. else
  1916. {
  1917. /* MMC Card does not support this feature */
  1918. hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
  1919. }
  1920. if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
  1921. {
  1922. /* Clear all the static flags */
  1923. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  1924. hsd->State = HAL_SD_STATE_READY;
  1925. return HAL_ERROR;
  1926. }
  1927. else
  1928. {
  1929. /* Configure the SDMMC peripheral */
  1930. Init.ClockEdge = hsd->Init.ClockEdge;
  1931. #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  1932. Init.ClockBypass = hsd->Init.ClockBypass;
  1933. #endif /* !STM32L4R5xx && !STM32L4R7xx && !STM32L4R9xx && !STM32L4S5xx && !STM32L4S7xx && !STM32L4S9xx */
  1934. Init.ClockPowerSave = hsd->Init.ClockPowerSave;
  1935. Init.BusWide = WideMode;
  1936. Init.HardwareFlowControl = hsd->Init.HardwareFlowControl;
  1937. #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  1938. #if 0
  1939. /* Check if user Clock div < Normal speed 25Mhz, no change in Clockdiv */
  1940. // if(hsd->Init.ClockDiv >= SDMMC_NSpeed_CLK_DIV)
  1941. if(hsd->Init.ClockDiv >= SDMMC_TRANSFER_CLK_DIV)
  1942. {
  1943. Init.ClockDiv = hsd->Init.ClockDiv;
  1944. }
  1945. else
  1946. {
  1947. if(hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED)
  1948. {
  1949. Init.ClockDiv = hsd->Init.ClockDiv;
  1950. }
  1951. else
  1952. {
  1953. /* No High speed SD card */
  1954. // Init.ClockDiv = SDMMC_NSpeed_CLK_DIV;
  1955. Init.ClockDiv = SDMMC_TRANSFER_CLK_DIV;
  1956. }
  1957. }
  1958. #else
  1959. Init.ClockDiv = hsd->Init.ClockDiv;
  1960. #endif
  1961. #else
  1962. Init.ClockDiv = hsd->Init.ClockDiv;
  1963. #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  1964. SDMMC_Init(hsd->Instance, Init);
  1965. }
  1966. /* Change State */
  1967. hsd->State = HAL_SD_STATE_READY;
  1968. return HAL_OK;
  1969. }
  1970. /**
  1971. * @brief Gets the current sd card data state.
  1972. * @param hsd: pointer to SD handle
  1973. * @retval Card state
  1974. */
  1975. HAL_SD_CardStateTypedef HAL_SD_GetCardState(SD_HandleTypeDef *hsd)
  1976. {
  1977. HAL_SD_CardStateTypedef cardstate = HAL_SD_CARD_TRANSFER;
  1978. uint32_t errorstate = HAL_SD_ERROR_NONE;
  1979. uint32_t resp1 = 0;
  1980. errorstate = SD_SendStatus(hsd, &resp1);
  1981. if(errorstate != HAL_SD_ERROR_NONE)
  1982. {
  1983. hsd->ErrorCode |= errorstate;
  1984. }
  1985. cardstate = (HAL_SD_CardStateTypedef)((resp1 >> 9) & 0x0F);
  1986. return cardstate;
  1987. }
  1988. /**
  1989. * @brief Abort the current transfer and disable the SD.
  1990. * @param hsd: pointer to a SD_HandleTypeDef structure that contains
  1991. * the configuration information for SD module.
  1992. * @retval HAL status
  1993. */
  1994. HAL_StatusTypeDef HAL_SD_Abort(SD_HandleTypeDef *hsd)
  1995. {
  1996. HAL_SD_CardStateTypedef CardState;
  1997. /* DIsable All interrupts */
  1998. __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT|\
  1999. SDMMC_IT_TXUNDERR| SDMMC_IT_RXOVERR);
  2000. /* Clear All flags */
  2001. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  2002. #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  2003. /* If IDMA Context, disable Internal DMA */
  2004. hsd->Instance->IDMACTRL = SDMMC_DISABLE_IDMA;
  2005. #else
  2006. if((hsd->hdmatx != NULL) || (hsd->hdmarx != NULL))
  2007. {
  2008. /* Disable the SD DMA request */
  2009. hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDMMC_DCTRL_DMAEN);
  2010. /* Abort the SD DMA Tx channel */
  2011. if(hsd->hdmatx != NULL)
  2012. {
  2013. HAL_DMA_Abort(hsd->hdmatx);
  2014. }
  2015. /* Abort the SD DMA Rx channel */
  2016. if(hsd->hdmarx != NULL)
  2017. {
  2018. HAL_DMA_Abort(hsd->hdmarx);
  2019. }
  2020. }
  2021. #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  2022. hsd->State = HAL_SD_STATE_READY;
  2023. CardState = HAL_SD_GetCardState(hsd);
  2024. if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
  2025. {
  2026. hsd->ErrorCode = SDMMC_CmdStopTransfer(hsd->Instance);
  2027. }
  2028. if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
  2029. {
  2030. return HAL_ERROR;
  2031. }
  2032. return HAL_OK;
  2033. }
  2034. /**
  2035. * @brief Abort the current transfer and disable the SD (IT mode).
  2036. * @param hsd: pointer to a SD_HandleTypeDef structure that contains
  2037. * the configuration information for SD module.
  2038. * @retval HAL status
  2039. */
  2040. HAL_StatusTypeDef HAL_SD_Abort_IT(SD_HandleTypeDef *hsd)
  2041. {
  2042. HAL_SD_CardStateTypedef CardState;
  2043. /* Disable All interrupts */
  2044. __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT|\
  2045. SDMMC_IT_TXUNDERR| SDMMC_IT_RXOVERR);
  2046. #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  2047. /* If IDMA Context, disable Internal DMA */
  2048. hsd->Instance->IDMACTRL = SDMMC_DISABLE_IDMA;
  2049. /* Clear All flags */
  2050. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
  2051. CardState = HAL_SD_GetCardState(hsd);
  2052. hsd->State = HAL_SD_STATE_READY;
  2053. if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
  2054. {
  2055. hsd->ErrorCode = SDMMC_CmdStopTransfer(hsd->Instance);
  2056. }
  2057. if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
  2058. {
  2059. return HAL_ERROR;
  2060. }
  2061. else
  2062. {
  2063. HAL_SD_AbortCallback(hsd);
  2064. }
  2065. #else
  2066. if((hsd->hdmatx != NULL) || (hsd->hdmarx != NULL))
  2067. {
  2068. /* Disable the SD DMA request */
  2069. hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDMMC_DCTRL_DMAEN);
  2070. /* Abort the SD DMA Tx channel */
  2071. if(hsd->hdmatx != NULL)
  2072. {
  2073. hsd->hdmatx->XferAbortCallback = SD_DMATxAbort;
  2074. if(HAL_DMA_Abort_IT(hsd->hdmatx) != HAL_OK)
  2075. {
  2076. hsd->hdmatx = NULL;
  2077. }
  2078. }
  2079. /* Abort the SD DMA Rx channel */
  2080. if(hsd->hdmarx != NULL)
  2081. {
  2082. hsd->hdmarx->XferAbortCallback = SD_DMARxAbort;
  2083. if(HAL_DMA_Abort_IT(hsd->hdmarx) != HAL_OK)
  2084. {
  2085. hsd->hdmarx = NULL;
  2086. }
  2087. }
  2088. }
  2089. /* No transfer ongoing on both DMA channels*/
  2090. if((hsd->hdmatx == NULL) && (hsd->hdmarx == NULL))
  2091. {
  2092. /* Clear All flags */
  2093. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
  2094. CardState = HAL_SD_GetCardState(hsd);
  2095. hsd->State = HAL_SD_STATE_READY;
  2096. if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
  2097. {
  2098. hsd->ErrorCode = SDMMC_CmdStopTransfer(hsd->Instance);
  2099. }
  2100. if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
  2101. {
  2102. return HAL_ERROR;
  2103. }
  2104. else
  2105. {
  2106. HAL_SD_AbortCallback(hsd);
  2107. }
  2108. }
  2109. #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  2110. return HAL_OK;
  2111. }
  2112. /**
  2113. * @}
  2114. */
  2115. /**
  2116. * @}
  2117. */
  2118. /* Private function ----------------------------------------------------------*/
  2119. /** @addtogroup SD_Private_Functions
  2120. * @{
  2121. */
  2122. #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  2123. /**
  2124. * @brief DMA SD transmit process complete callback
  2125. * @param hdma: DMA handle
  2126. * @retval None
  2127. */
  2128. static void SD_DMATransmitCplt(DMA_HandleTypeDef *hdma)
  2129. {
  2130. SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
  2131. /* Enable DATAEND Interrupt */
  2132. __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DATAEND));
  2133. }
  2134. /**
  2135. * @brief DMA SD receive process complete callback
  2136. * @param hdma: DMA handle
  2137. * @retval None
  2138. */
  2139. static void SD_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
  2140. {
  2141. SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
  2142. uint32_t errorstate = HAL_SD_ERROR_NONE;
  2143. /* Send stop command in multiblock write */
  2144. if(hsd->Context == (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_DMA))
  2145. {
  2146. errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
  2147. if(errorstate != HAL_SD_ERROR_NONE)
  2148. {
  2149. hsd->ErrorCode |= errorstate;
  2150. HAL_SD_ErrorCallback(hsd);
  2151. }
  2152. }
  2153. /* Disable the DMA transfer for transmit request by setting the DMAEN bit
  2154. in the SD DCTRL register */
  2155. hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDMMC_DCTRL_DMAEN);
  2156. /* Clear all the static flags */
  2157. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
  2158. hsd->State = HAL_SD_STATE_READY;
  2159. HAL_SD_RxCpltCallback(hsd);
  2160. }
  2161. /**
  2162. * @brief DMA SD communication error callback
  2163. * @param hdma: DMA handle
  2164. * @retval None
  2165. */
  2166. static void SD_DMAError(DMA_HandleTypeDef *hdma)
  2167. {
  2168. SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
  2169. HAL_SD_CardStateTypedef CardState;
  2170. if((hsd->hdmarx->ErrorCode == HAL_DMA_ERROR_TE) || (hsd->hdmatx->ErrorCode == HAL_DMA_ERROR_TE))
  2171. {
  2172. /* Clear All flags */
  2173. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  2174. /* Disable All interrupts */
  2175. __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT|\
  2176. SDMMC_IT_TXUNDERR| SDMMC_IT_RXOVERR);
  2177. hsd->ErrorCode |= HAL_SD_ERROR_DMA;
  2178. CardState = HAL_SD_GetCardState(hsd);
  2179. if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
  2180. {
  2181. hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
  2182. }
  2183. hsd->State= HAL_SD_STATE_READY;
  2184. }
  2185. HAL_SD_ErrorCallback(hsd);
  2186. }
  2187. /**
  2188. * @brief DMA SD Tx Abort callback
  2189. * @param hdma: DMA handle
  2190. * @retval None
  2191. */
  2192. static void SD_DMATxAbort(DMA_HandleTypeDef *hdma)
  2193. {
  2194. SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
  2195. HAL_SD_CardStateTypedef CardState;
  2196. if(hsd->hdmatx != NULL)
  2197. {
  2198. hsd->hdmatx = NULL;
  2199. }
  2200. /* All DMA channels are aborted */
  2201. if((hsd->hdmatx == NULL) && (hsd->hdmarx == NULL))
  2202. {
  2203. /* Clear All flags */
  2204. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
  2205. CardState = HAL_SD_GetCardState(hsd);
  2206. hsd->State = HAL_SD_STATE_READY;
  2207. if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
  2208. {
  2209. hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
  2210. if(hsd->ErrorCode == HAL_SD_ERROR_NONE)
  2211. {
  2212. HAL_SD_AbortCallback(hsd);
  2213. }
  2214. else
  2215. {
  2216. HAL_SD_ErrorCallback(hsd);
  2217. }
  2218. }
  2219. }
  2220. }
  2221. /**
  2222. * @brief DMA SD Rx Abort callback
  2223. * @param hdma: DMA handle
  2224. * @retval None
  2225. */
  2226. static void SD_DMARxAbort(DMA_HandleTypeDef *hdma)
  2227. {
  2228. SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
  2229. HAL_SD_CardStateTypedef CardState;
  2230. if(hsd->hdmarx != NULL)
  2231. {
  2232. hsd->hdmarx = NULL;
  2233. }
  2234. /* All DMA channels are aborted */
  2235. if((hsd->hdmatx == NULL) && (hsd->hdmarx == NULL))
  2236. {
  2237. /* Clear All flags */
  2238. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
  2239. CardState = HAL_SD_GetCardState(hsd);
  2240. hsd->State = HAL_SD_STATE_READY;
  2241. if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
  2242. {
  2243. hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
  2244. if(hsd->ErrorCode == HAL_SD_ERROR_NONE)
  2245. {
  2246. HAL_SD_AbortCallback(hsd);
  2247. }
  2248. else
  2249. {
  2250. HAL_SD_ErrorCallback(hsd);
  2251. }
  2252. }
  2253. }
  2254. }
  2255. #endif /* !STM32L4R5xx && !STM32L4R7xx && !STM32L4R9xx && !STM32L4S5xx && !STM32L4S7xx && !STM32L4S9xx */
  2256. /**
  2257. * @brief Initializes the sd card.
  2258. * @param hsd: Pointer to SD handle
  2259. * @retval SD Card error state
  2260. */
  2261. static uint32_t SD_InitCard(SD_HandleTypeDef *hsd)
  2262. {
  2263. HAL_SD_CardCSDTypedef CSD;
  2264. uint32_t errorstate = HAL_SD_ERROR_NONE;
  2265. uint16_t sd_rca = 1;
  2266. /* Check the power State */
  2267. if(SDMMC_GetPowerState(hsd->Instance) == 0)
  2268. {
  2269. /* Power off */
  2270. return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
  2271. }
  2272. if(hsd->SdCard.CardType != CARD_SECURED)
  2273. {
  2274. /* Send CMD2 ALL_SEND_CID */
  2275. errorstate = SDMMC_CmdSendCID(hsd->Instance);
  2276. if(errorstate != HAL_SD_ERROR_NONE)
  2277. {
  2278. return errorstate;
  2279. }
  2280. else
  2281. {
  2282. /* Get Card identification number data */
  2283. hsd->CID[0] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
  2284. hsd->CID[1] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP2);
  2285. hsd->CID[2] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP3);
  2286. hsd->CID[3] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP4);
  2287. }
  2288. }
  2289. if(hsd->SdCard.CardType != CARD_SECURED)
  2290. {
  2291. /* Send CMD3 SET_REL_ADDR with argument 0 */
  2292. /* SD Card publishes its RCA. */
  2293. errorstate = SDMMC_CmdSetRelAdd(hsd->Instance, &sd_rca);
  2294. if(errorstate != HAL_SD_ERROR_NONE)
  2295. {
  2296. return errorstate;
  2297. }
  2298. }
  2299. if(hsd->SdCard.CardType != CARD_SECURED)
  2300. {
  2301. /* Get the SD card RCA */
  2302. hsd->SdCard.RelCardAdd = sd_rca;
  2303. /* Send CMD9 SEND_CSD with argument as card's RCA */
  2304. errorstate = SDMMC_CmdSendCSD(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
  2305. if(errorstate != HAL_SD_ERROR_NONE)
  2306. {
  2307. return errorstate;
  2308. }
  2309. else
  2310. {
  2311. /* Get Card Specific Data */
  2312. hsd->CSD[0U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
  2313. hsd->CSD[1U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP2);
  2314. hsd->CSD[2U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP3);
  2315. hsd->CSD[3U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP4);
  2316. }
  2317. }
  2318. /* Get the Card Class */
  2319. hsd->SdCard.Class = (SDMMC_GetResponse(hsd->Instance, SDMMC_RESP2) >> 20);
  2320. /* Get CSD parameters */
  2321. HAL_SD_GetCardCSD(hsd, &CSD);
  2322. /* Select the Card */
  2323. errorstate = SDMMC_CmdSelDesel(hsd->Instance, (uint32_t)(((uint32_t)hsd->SdCard.RelCardAdd) << 16));
  2324. if(errorstate != HAL_SD_ERROR_NONE)
  2325. {
  2326. return errorstate;
  2327. }
  2328. #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  2329. /* Configure SDMMC peripheral interface */
  2330. SDMMC_Init(hsd->Instance, hsd->Init);
  2331. #endif /* !STM32L4R5xx && !STM32L4R7xx && !STM32L4R9xx && !STM32L4S5xx && !STM32L4S7xx && !STM32L4S9xx */
  2332. /* All cards are initialized */
  2333. return HAL_SD_ERROR_NONE;
  2334. }
  2335. /**
  2336. * @brief Enquires cards about their operating voltage and configures clock
  2337. * controls and stores SD information that will be needed in future
  2338. * in the SD handle.
  2339. * @param hsd: Pointer to SD handle
  2340. * @retval error state
  2341. */
  2342. static uint32_t SD_PowerON(SD_HandleTypeDef *hsd)
  2343. {
  2344. __IO uint32_t count = 0;
  2345. uint32_t response = 0, validvoltage = 0;
  2346. uint32_t errorstate = HAL_SD_ERROR_NONE;
  2347. /* CMD0: GO_IDLE_STATE */
  2348. errorstate = SDMMC_CmdGoIdleState(hsd->Instance);
  2349. if(errorstate != HAL_SD_ERROR_NONE)
  2350. {
  2351. return errorstate;
  2352. }
  2353. /* CMD8: SEND_IF_COND: Command available only on V2.0 cards */
  2354. errorstate = SDMMC_CmdOperCond(hsd->Instance);
  2355. if(errorstate != HAL_SD_ERROR_NONE)
  2356. {
  2357. hsd->SdCard.CardVersion = CARD_V1_X;
  2358. #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  2359. /* Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */
  2360. while(validvoltage == 0)
  2361. {
  2362. if(count++ == SDMMC_MAX_VOLT_TRIAL)
  2363. {
  2364. return HAL_SD_ERROR_INVALID_VOLTRANGE;
  2365. }
  2366. /* SEND CMD55 APP_CMD with RCA as 0 */
  2367. errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0);
  2368. if(errorstate != HAL_SD_ERROR_NONE)
  2369. {
  2370. return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
  2371. }
  2372. /* Send CMD41 */
  2373. errorstate = SDMMC_CmdAppOperCommand(hsd->Instance, SDMMC_STD_CAPACITY);
  2374. if(errorstate != HAL_SD_ERROR_NONE)
  2375. {
  2376. return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
  2377. }
  2378. /* Get command response */
  2379. response = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
  2380. /* Get operating voltage*/
  2381. validvoltage = (((response >> 31) == 1) ? 1 : 0);
  2382. }
  2383. /* Card type is SDSC */
  2384. hsd->SdCard.CardType = CARD_SDSC;
  2385. #endif /* !STM32L4R5xx && !STM32L4R7xx && !STM32L4R9xx && !STM32L4S5xx && !STM32L4S7xx && !STM32L4S9xx */
  2386. }
  2387. else
  2388. {
  2389. hsd->SdCard.CardVersion = CARD_V2_X;
  2390. #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  2391. /* Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */
  2392. while(validvoltage == 0)
  2393. {
  2394. if(count++ == SDMMC_MAX_VOLT_TRIAL)
  2395. {
  2396. return HAL_SD_ERROR_INVALID_VOLTRANGE;
  2397. }
  2398. /* SEND CMD55 APP_CMD with RCA as 0 */
  2399. errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0);
  2400. if(errorstate != HAL_SD_ERROR_NONE)
  2401. {
  2402. return errorstate;
  2403. }
  2404. /* Send CMD41 */
  2405. errorstate = SDMMC_CmdAppOperCommand(hsd->Instance, SDMMC_HIGH_CAPACITY);
  2406. if(errorstate != HAL_SD_ERROR_NONE)
  2407. {
  2408. return errorstate;
  2409. }
  2410. /* Get command response */
  2411. response = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
  2412. /* Get operating voltage*/
  2413. validvoltage = (((response >> 31) == 1) ? 1 : 0);
  2414. }
  2415. if((response & SDMMC_HIGH_CAPACITY) == SDMMC_HIGH_CAPACITY) /* (response &= SD_HIGH_CAPACITY) */
  2416. {
  2417. hsd->SdCard.CardType = CARD_SDHC_SDXC;
  2418. }
  2419. else
  2420. {
  2421. hsd->SdCard.CardType = CARD_SDSC;
  2422. }
  2423. #endif /* !STM32L4R5xx && !STM32L4R7xx && !STM32L4R9xx && !STM32L4S5xx && !STM32L4S7xx && !STM32L4S9xx */
  2424. }
  2425. #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  2426. /* SEND CMD55 APP_CMD with RCA as 0 */
  2427. errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0);
  2428. if(errorstate != HAL_SD_ERROR_NONE)
  2429. {
  2430. return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
  2431. }
  2432. else
  2433. {
  2434. /* SD CARD */
  2435. /* Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */
  2436. while((!validvoltage) && (count < SDMMC_MAX_VOLT_TRIAL))
  2437. {
  2438. /* SEND CMD55 APP_CMD with RCA as 0 */
  2439. errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0);
  2440. if(errorstate != HAL_SD_ERROR_NONE)
  2441. {
  2442. return errorstate;
  2443. }
  2444. /* Send CMD41 */
  2445. errorstate = SDMMC_CmdAppOperCommand(hsd->Instance, SDMMC_VOLTAGE_WINDOW_SD | SDMMC_HIGH_CAPACITY | SD_SWITCH_1_8V_CAPACITY);
  2446. if(errorstate != HAL_SD_ERROR_NONE)
  2447. {
  2448. return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
  2449. }
  2450. /* Get command response */
  2451. response = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
  2452. /* Get operating voltage*/
  2453. validvoltage = (((response >> 31) == 1) ? 1 : 0);
  2454. count++;
  2455. }
  2456. if(count >= SDMMC_MAX_VOLT_TRIAL)
  2457. {
  2458. return HAL_SD_ERROR_INVALID_VOLTRANGE;
  2459. }
  2460. if((response & SDMMC_HIGH_CAPACITY) == SDMMC_HIGH_CAPACITY) /* (response &= SD_HIGH_CAPACITY) */
  2461. {
  2462. hsd->SdCard.CardType = CARD_SDHC_SDXC;
  2463. if(hsd->Init.Transceiver == SDMMC_TRANSCEIVER_ENABLE)
  2464. {
  2465. if((response & SD_SWITCH_1_8V_CAPACITY) == SD_SWITCH_1_8V_CAPACITY)
  2466. {
  2467. hsd->SdCard.CardSpeed = CARD_ULTRA_HIGH_SPEED;
  2468. /* Start switching procedue */
  2469. hsd->Instance->POWER |= SDMMC_POWER_VSWITCHEN;
  2470. /* Send CMD11 to switch 1.8V mode */
  2471. errorstate = SDMMC_CmdVoltageSwitch(hsd->Instance);
  2472. if(errorstate != HAL_SD_ERROR_NONE)
  2473. {
  2474. return errorstate;
  2475. }
  2476. /* Check to CKSTOP */
  2477. while(( hsd->Instance->STA & SDMMC_FLAG_CKSTOP) != SDMMC_FLAG_CKSTOP);
  2478. /* Clear CKSTOP Flag */
  2479. hsd->Instance->ICR = SDMMC_FLAG_CKSTOP;
  2480. /* Check to BusyD0 */
  2481. if(( hsd->Instance->STA & SDMMC_FLAG_BUSYD0) != SDMMC_FLAG_BUSYD0)
  2482. {
  2483. /* Error when activate Voltage Switch in SDMMC IP */
  2484. return SDMMC_ERROR_UNSUPPORTED_FEATURE;
  2485. }
  2486. else
  2487. {
  2488. /* Enable Transceiver Switch PIN */
  2489. HAL_SDEx_DriveTransceiver_1_8V_Callback(SET);
  2490. /* Switch ready */
  2491. hsd->Instance->POWER |= SDMMC_POWER_VSWITCH;
  2492. /* Check VSWEND Flag */
  2493. while(( hsd->Instance->STA & SDMMC_FLAG_VSWEND) != SDMMC_FLAG_VSWEND);
  2494. /* Clear VSWEND Flag */
  2495. hsd->Instance->ICR = SDMMC_FLAG_VSWEND;
  2496. /* Check BusyD0 status */
  2497. if(( hsd->Instance->STA & SDMMC_FLAG_BUSYD0) == SDMMC_FLAG_BUSYD0)
  2498. {
  2499. /* Error when enabling 1.8V mode */
  2500. return HAL_SD_ERROR_INVALID_VOLTRANGE;
  2501. }
  2502. /* Switch to 1.8V OK */
  2503. /* Disable VSWITCH FLAG from SDMMC IP */
  2504. hsd->Instance->POWER = 0x13;
  2505. /* Clean Status flags */
  2506. hsd->Instance->ICR = 0xFFFFFFFF;
  2507. }
  2508. }
  2509. }
  2510. }
  2511. }
  2512. #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  2513. return HAL_SD_ERROR_NONE;
  2514. }
  2515. /**
  2516. * @brief Turns the SDMMC output signals off.
  2517. * @param hsd: Pointer to SD handle
  2518. * @retval HAL status
  2519. */
  2520. static HAL_StatusTypeDef SD_PowerOFF(SD_HandleTypeDef *hsd)
  2521. {
  2522. /* Set Power State to OFF */
  2523. SDMMC_PowerState_OFF(hsd->Instance);
  2524. return HAL_OK;
  2525. }
  2526. /**
  2527. * @brief Send Status info command.
  2528. * @param hsd: pointer to SD handle
  2529. * @param pSDstatus: Pointer to the buffer that will contain the SD card status
  2530. * SD Status register)
  2531. * @retval error state
  2532. */
  2533. static uint32_t SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus)
  2534. {
  2535. SDMMC_DataInitTypeDef config;
  2536. uint32_t errorstate = HAL_SD_ERROR_NONE;
  2537. uint32_t tickstart = HAL_GetTick();
  2538. uint32_t count = 0;
  2539. /* Check SD response */
  2540. if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
  2541. {
  2542. return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
  2543. }
  2544. /* Set block size for card if it is not equal to current block size for card */
  2545. errorstate = SDMMC_CmdBlockLength(hsd->Instance, 64);
  2546. if(errorstate != HAL_SD_ERROR_NONE)
  2547. {
  2548. hsd->ErrorCode |= HAL_SD_ERROR_NONE;
  2549. return errorstate;
  2550. }
  2551. /* Send CMD55 */
  2552. errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16));
  2553. if(errorstate != HAL_SD_ERROR_NONE)
  2554. {
  2555. hsd->ErrorCode |= HAL_SD_ERROR_NONE;
  2556. return errorstate;
  2557. }
  2558. /* Configure the SD DPSM (Data Path State Machine) */
  2559. config.DataTimeOut = SDMMC_DATATIMEOUT;
  2560. config.DataLength = 64;
  2561. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_64B;
  2562. config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
  2563. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  2564. config.DPSM = SDMMC_DPSM_ENABLE;
  2565. SDMMC_ConfigData(hsd->Instance, &config);
  2566. /* Send ACMD13 (SD_APP_STAUS) with argument as card's RCA */
  2567. errorstate = SDMMC_CmdStatusRegister(hsd->Instance);
  2568. if(errorstate != HAL_SD_ERROR_NONE)
  2569. {
  2570. hsd->ErrorCode |= HAL_SD_ERROR_NONE;
  2571. return errorstate;
  2572. }
  2573. /* Get status data */
  2574. #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  2575. while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DATAEND))
  2576. #else
  2577. while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND))
  2578. #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  2579. {
  2580. if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF))
  2581. {
  2582. for(count = 0; count < 8; count++)
  2583. {
  2584. *(pSDstatus + count) = SDMMC_ReadFIFO(hsd->Instance);
  2585. }
  2586. pSDstatus += 8;
  2587. }
  2588. if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
  2589. {
  2590. return HAL_SD_ERROR_TIMEOUT;
  2591. }
  2592. }
  2593. if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
  2594. {
  2595. return HAL_SD_ERROR_DATA_TIMEOUT;
  2596. }
  2597. else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
  2598. {
  2599. return HAL_SD_ERROR_DATA_CRC_FAIL;
  2600. }
  2601. else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
  2602. {
  2603. return HAL_SD_ERROR_RX_OVERRUN;
  2604. }
  2605. #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  2606. while ((__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DPSMACT)))
  2607. #else
  2608. while ((__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXDAVL)))
  2609. #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  2610. {
  2611. *pSDstatus = SDMMC_ReadFIFO(hsd->Instance);
  2612. pSDstatus++;
  2613. if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
  2614. {
  2615. return HAL_SD_ERROR_TIMEOUT;
  2616. }
  2617. }
  2618. /* Clear all the static status flags*/
  2619. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
  2620. return HAL_SD_ERROR_NONE;
  2621. }
  2622. /**
  2623. * @brief Returns the current card's status.
  2624. * @param hsd: Pointer to SD handle
  2625. * @param pCardStatus: pointer to the buffer that will contain the SD card
  2626. * status (Card Status register)
  2627. * @retval error state
  2628. */
  2629. static uint32_t SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus)
  2630. {
  2631. uint32_t errorstate = HAL_SD_ERROR_NONE;
  2632. if(pCardStatus == NULL)
  2633. {
  2634. return HAL_SD_ERROR_PARAM;
  2635. }
  2636. /* Send Status command */
  2637. errorstate = SDMMC_CmdSendStatus(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16));
  2638. if(errorstate != HAL_OK)
  2639. {
  2640. return errorstate;
  2641. }
  2642. /* Get SD card status */
  2643. *pCardStatus = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
  2644. return HAL_SD_ERROR_NONE;
  2645. }
  2646. /**
  2647. * @brief Enables the SDMMC wide bus mode.
  2648. * @param hsd: pointer to SD handle
  2649. * @retval error state
  2650. */
  2651. static uint32_t SD_WideBus_Enable(SD_HandleTypeDef *hsd)
  2652. {
  2653. uint32_t scr[2] = {0, 0};
  2654. uint32_t errorstate = HAL_SD_ERROR_NONE;
  2655. if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
  2656. {
  2657. return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
  2658. }
  2659. /* Get SCR Register */
  2660. errorstate = SD_FindSCR(hsd, scr);
  2661. if(errorstate != HAL_OK)
  2662. {
  2663. return errorstate;
  2664. }
  2665. /* If requested card supports wide bus operation */
  2666. if((scr[1] & SDMMC_WIDE_BUS_SUPPORT) != SDMMC_ALLZERO)
  2667. {
  2668. /* Send CMD55 APP_CMD with argument as card's RCA.*/
  2669. errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16));
  2670. if(errorstate != HAL_OK)
  2671. {
  2672. return errorstate;
  2673. }
  2674. /* Send ACMD6 APP_CMD with argument as 2 for wide bus mode */
  2675. errorstate = SDMMC_CmdBusWidth(hsd->Instance, 2);
  2676. if(errorstate != HAL_OK)
  2677. {
  2678. return errorstate;
  2679. }
  2680. return HAL_SD_ERROR_NONE;
  2681. }
  2682. else
  2683. {
  2684. return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
  2685. }
  2686. }
  2687. /**
  2688. * @brief Disables the SDMMC wide bus mode.
  2689. * @param hsd: Pointer to SD handle
  2690. * @retval error state
  2691. */
  2692. static uint32_t SD_WideBus_Disable(SD_HandleTypeDef *hsd)
  2693. {
  2694. uint32_t scr[2] = {0, 0};
  2695. uint32_t errorstate = HAL_SD_ERROR_NONE;
  2696. if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
  2697. {
  2698. return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
  2699. }
  2700. /* Get SCR Register */
  2701. errorstate = SD_FindSCR(hsd, scr);
  2702. if(errorstate != HAL_OK)
  2703. {
  2704. return errorstate;
  2705. }
  2706. /* If requested card supports 1 bit mode operation */
  2707. if((scr[1] & SDMMC_SINGLE_BUS_SUPPORT) != SDMMC_ALLZERO)
  2708. {
  2709. /* Send CMD55 APP_CMD with argument as card's RCA */
  2710. errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16));
  2711. if(errorstate != HAL_OK)
  2712. {
  2713. return errorstate;
  2714. }
  2715. /* Send ACMD6 APP_CMD with argument as 0 for single bus mode */
  2716. errorstate = SDMMC_CmdBusWidth(hsd->Instance, 0);
  2717. if(errorstate != HAL_OK)
  2718. {
  2719. return errorstate;
  2720. }
  2721. return HAL_SD_ERROR_NONE;
  2722. }
  2723. else
  2724. {
  2725. return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
  2726. }
  2727. }
  2728. /**
  2729. * @brief Finds the SD card SCR register value.
  2730. * @param hsd: Pointer to SD handle
  2731. * @param pSCR: pointer to the buffer that will contain the SCR value
  2732. * @retval error state
  2733. */
  2734. static uint32_t SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR)
  2735. {
  2736. SDMMC_DataInitTypeDef config;
  2737. uint32_t errorstate = HAL_SD_ERROR_NONE;
  2738. uint32_t tickstart = HAL_GetTick();
  2739. uint32_t index = 0;
  2740. uint32_t tempscr[2] = {0, 0};
  2741. /* Set Block Size To 8 Bytes */
  2742. errorstate = SDMMC_CmdBlockLength(hsd->Instance, 8);
  2743. if(errorstate != HAL_OK)
  2744. {
  2745. return errorstate;
  2746. }
  2747. /* Send CMD55 APP_CMD with argument as card's RCA */
  2748. errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)((hsd->SdCard.RelCardAdd) << 16));
  2749. if(errorstate != HAL_OK)
  2750. {
  2751. return errorstate;
  2752. }
  2753. config.DataTimeOut = SDMMC_DATATIMEOUT;
  2754. config.DataLength = 8;
  2755. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_8B;
  2756. config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
  2757. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  2758. config.DPSM = SDMMC_DPSM_ENABLE;
  2759. SDMMC_ConfigData(hsd->Instance, &config);
  2760. /* Send ACMD51 SD_APP_SEND_SCR with argument as 0 */
  2761. errorstate = SDMMC_CmdSendSCR(hsd->Instance);
  2762. if(errorstate != HAL_OK)
  2763. {
  2764. return errorstate;
  2765. }
  2766. #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  2767. while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND | SDMMC_FLAG_DATAEND))
  2768. {
  2769. if((!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOE)) && (index == 0))
  2770. {
  2771. tempscr[0] = SDMMC_ReadFIFO(hsd->Instance);
  2772. tempscr[1] = SDMMC_ReadFIFO(hsd->Instance);
  2773. index++;
  2774. }
  2775. if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
  2776. {
  2777. return HAL_SD_ERROR_TIMEOUT;
  2778. }
  2779. }
  2780. #else
  2781. while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND))
  2782. {
  2783. if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXDAVL))
  2784. {
  2785. *(tempscr + index) = SDMMC_ReadFIFO(hsd->Instance);
  2786. index++;
  2787. }
  2788. if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
  2789. {
  2790. return HAL_SD_ERROR_TIMEOUT;
  2791. }
  2792. }
  2793. #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  2794. if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
  2795. {
  2796. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT);
  2797. return HAL_SD_ERROR_DATA_TIMEOUT;
  2798. }
  2799. else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
  2800. {
  2801. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL);
  2802. return HAL_SD_ERROR_DATA_CRC_FAIL;
  2803. }
  2804. else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
  2805. {
  2806. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_RXOVERR);
  2807. return HAL_SD_ERROR_RX_OVERRUN;
  2808. }
  2809. else
  2810. {
  2811. /* No error flag set */
  2812. /* Clear all the static flags */
  2813. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
  2814. *(pSCR + 1) = ((tempscr[0] & SDMMC_0TO7BITS) << 24) | ((tempscr[0] & SDMMC_8TO15BITS) << 8) |\
  2815. ((tempscr[0] & SDMMC_16TO23BITS) >> 8) | ((tempscr[0] & SDMMC_24TO31BITS) >> 24);
  2816. *(pSCR) = ((tempscr[1] & SDMMC_0TO7BITS) << 24) | ((tempscr[1] & SDMMC_8TO15BITS) << 8) |\
  2817. ((tempscr[1] & SDMMC_16TO23BITS) >> 8) | ((tempscr[1] & SDMMC_24TO31BITS) >> 24);
  2818. }
  2819. return HAL_SD_ERROR_NONE;
  2820. }
  2821. /**
  2822. * @brief Wrap up reading in non-blocking mode.
  2823. * @param hsd: pointer to a SD_HandleTypeDef structure that contains
  2824. * the configuration information.
  2825. * @retval HAL status
  2826. */
  2827. static HAL_StatusTypeDef SD_Read_IT(SD_HandleTypeDef *hsd)
  2828. {
  2829. uint32_t count = 0;
  2830. uint32_t* tmp;
  2831. tmp = (uint32_t*)hsd->pRxBuffPtr;
  2832. /* Read data from SDMMC Rx FIFO */
  2833. for(count = 0; count < 8; count++)
  2834. {
  2835. *(tmp + count) = SDMMC_ReadFIFO(hsd->Instance);
  2836. }
  2837. hsd->pRxBuffPtr += 8;
  2838. return HAL_OK;
  2839. }
  2840. /**
  2841. * @brief Wrap up writing in non-blocking mode.
  2842. * @param hsd: pointer to a SD_HandleTypeDef structure that contains
  2843. * the configuration information.
  2844. * @retval HAL status
  2845. */
  2846. static HAL_StatusTypeDef SD_Write_IT(SD_HandleTypeDef *hsd)
  2847. {
  2848. uint32_t count = 0;
  2849. uint32_t* tmp;
  2850. tmp = (uint32_t*)hsd->pTxBuffPtr;
  2851. /* Write data to SDMMC Tx FIFO */
  2852. for(count = 0; count < 8; count++)
  2853. {
  2854. SDMMC_WriteFIFO(hsd->Instance, (tmp + count));
  2855. }
  2856. hsd->pTxBuffPtr += 8;
  2857. return HAL_OK;
  2858. }
  2859. /**
  2860. * @}
  2861. */
  2862. #endif /* HAL_SD_MODULE_ENABLED */
  2863. /**
  2864. * @}
  2865. */
  2866. /**
  2867. * @}
  2868. */
  2869. #endif /* SDMMC1 */
  2870. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/