You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stm32l1xx_hal_sd.c 98KB

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