stm32f1xx_hal_sd.c 88 KB

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