stm32f4xx_hal_sd.c 95 KB

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