stm32f1xx_hal_sd.c 88 KB

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