stm32l4xx_hal_sd.c 88 KB

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