stm32f1xx_hal_mmc.c 88 KB

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