stm32f7xx_hal_sd.c 87 KB

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