stm32f1xx_hal_sd.c 96 KB

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