stm32f4xx_hal_mmc.c 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_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) 2017 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 "stm32f4xx_hal.h"
  223. /** @addtogroup STM32F4xx_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. #if defined(SDIO_STA_STBITERR)
  543. while(!__HAL_MMC_GET_FLAG(hmmc, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND | SDIO_FLAG_STBITERR))
  544. #else /* SDIO_STA_STBITERR not defined */
  545. while(!__HAL_MMC_GET_FLAG(hmmc, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND))
  546. #endif /* SDIO_STA_STBITERR */
  547. {
  548. if(__HAL_MMC_GET_FLAG(hmmc, SDIO_FLAG_RXFIFOHF) && (dataremaining > 0U))
  549. {
  550. /* Read data from SDIO Rx FIFO */
  551. for(count = 0U; count < 8U; count++)
  552. {
  553. data = SDIO_ReadFIFO(hmmc->Instance);
  554. *tempbuff = (uint8_t)(data & 0xFFU);
  555. tempbuff++;
  556. dataremaining--;
  557. *tempbuff = (uint8_t)((data >> 8U) & 0xFFU);
  558. tempbuff++;
  559. dataremaining--;
  560. *tempbuff = (uint8_t)((data >> 16U) & 0xFFU);
  561. tempbuff++;
  562. dataremaining--;
  563. *tempbuff = (uint8_t)((data >> 24U) & 0xFFU);
  564. tempbuff++;
  565. dataremaining--;
  566. }
  567. }
  568. if(((HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U))
  569. {
  570. /* Clear all the static flags */
  571. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
  572. hmmc->ErrorCode |= HAL_MMC_ERROR_TIMEOUT;
  573. hmmc->State= HAL_MMC_STATE_READY;
  574. return HAL_TIMEOUT;
  575. }
  576. }
  577. /* Send stop transmission command in case of multiblock read */
  578. if(__HAL_MMC_GET_FLAG(hmmc, SDIO_FLAG_DATAEND) && (NumberOfBlocks > 1U))
  579. {
  580. /* Send stop transmission command */
  581. errorstate = SDMMC_CmdStopTransfer(hmmc->Instance);
  582. if(errorstate != HAL_MMC_ERROR_NONE)
  583. {
  584. /* Clear all the static flags */
  585. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
  586. hmmc->ErrorCode |= errorstate;
  587. hmmc->State = HAL_MMC_STATE_READY;
  588. return HAL_ERROR;
  589. }
  590. }
  591. /* Get error state */
  592. if(__HAL_MMC_GET_FLAG(hmmc, SDIO_FLAG_DTIMEOUT))
  593. {
  594. /* Clear all the static flags */
  595. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
  596. hmmc->ErrorCode |= HAL_MMC_ERROR_DATA_TIMEOUT;
  597. hmmc->State = HAL_MMC_STATE_READY;
  598. return HAL_ERROR;
  599. }
  600. else if(__HAL_MMC_GET_FLAG(hmmc, SDIO_FLAG_DCRCFAIL))
  601. {
  602. /* Clear all the static flags */
  603. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
  604. hmmc->ErrorCode |= HAL_MMC_ERROR_DATA_CRC_FAIL;
  605. hmmc->State = HAL_MMC_STATE_READY;
  606. return HAL_ERROR;
  607. }
  608. else if(__HAL_MMC_GET_FLAG(hmmc, SDIO_FLAG_RXOVERR))
  609. {
  610. /* Clear all the static flags */
  611. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
  612. hmmc->ErrorCode |= HAL_MMC_ERROR_RX_OVERRUN;
  613. hmmc->State = HAL_MMC_STATE_READY;
  614. return HAL_ERROR;
  615. }
  616. else
  617. {
  618. /* Nothing to do */
  619. }
  620. /* Empty FIFO if there is still any data */
  621. while ((__HAL_MMC_GET_FLAG(hmmc, SDIO_FLAG_RXDAVL)) && (dataremaining > 0U))
  622. {
  623. data = SDIO_ReadFIFO(hmmc->Instance);
  624. *tempbuff = (uint8_t)(data & 0xFFU);
  625. tempbuff++;
  626. dataremaining--;
  627. *tempbuff = (uint8_t)((data >> 8U) & 0xFFU);
  628. tempbuff++;
  629. dataremaining--;
  630. *tempbuff = (uint8_t)((data >> 16U) & 0xFFU);
  631. tempbuff++;
  632. dataremaining--;
  633. *tempbuff = (uint8_t)((data >> 24U) & 0xFFU);
  634. tempbuff++;
  635. dataremaining--;
  636. if(((HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U))
  637. {
  638. /* Clear all the static flags */
  639. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
  640. hmmc->ErrorCode |= HAL_MMC_ERROR_TIMEOUT;
  641. hmmc->State= HAL_MMC_STATE_READY;
  642. return HAL_ERROR;
  643. }
  644. }
  645. /* Clear all the static flags */
  646. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_DATA_FLAGS);
  647. hmmc->State = HAL_MMC_STATE_READY;
  648. return HAL_OK;
  649. }
  650. else
  651. {
  652. hmmc->ErrorCode |= HAL_MMC_ERROR_BUSY;
  653. return HAL_ERROR;
  654. }
  655. }
  656. /**
  657. * @brief Allows to write block(s) to a specified address in a card. The Data
  658. * transfer is managed by polling mode.
  659. * @note This API should be followed by a check on the card state through
  660. * HAL_MMC_GetCardState().
  661. * @param hmmc: Pointer to MMC handle
  662. * @param pData: pointer to the buffer that will contain the data to transmit
  663. * @param BlockAdd: Block Address where data will be written
  664. * @param NumberOfBlocks: Number of MMC blocks to write
  665. * @param Timeout: Specify timeout value
  666. * @retval HAL status
  667. */
  668. HAL_StatusTypeDef HAL_MMC_WriteBlocks(MMC_HandleTypeDef *hmmc, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout)
  669. {
  670. SDIO_DataInitTypeDef config;
  671. uint32_t errorstate;
  672. uint32_t tickstart = HAL_GetTick();
  673. uint32_t count, data, dataremaining;
  674. uint32_t add = BlockAdd;
  675. uint8_t *tempbuff = pData;
  676. if(NULL == pData)
  677. {
  678. hmmc->ErrorCode |= HAL_MMC_ERROR_PARAM;
  679. return HAL_ERROR;
  680. }
  681. if(hmmc->State == HAL_MMC_STATE_READY)
  682. {
  683. hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
  684. if((BlockAdd + NumberOfBlocks) > (hmmc->MmcCard.LogBlockNbr))
  685. {
  686. hmmc->ErrorCode |= HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
  687. return HAL_ERROR;
  688. }
  689. hmmc->State = HAL_MMC_STATE_BUSY;
  690. /* Initialize data control register */
  691. hmmc->Instance->DCTRL = 0U;
  692. if ((hmmc->MmcCard.CardType) != MMC_HIGH_CAPACITY_CARD)
  693. {
  694. add *= 512U;
  695. }
  696. /* Set Block Size for Card */
  697. errorstate = SDMMC_CmdBlockLength(hmmc->Instance, MMC_BLOCKSIZE);
  698. if(errorstate != HAL_MMC_ERROR_NONE)
  699. {
  700. /* Clear all the static flags */
  701. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
  702. hmmc->ErrorCode |= errorstate;
  703. hmmc->State = HAL_MMC_STATE_READY;
  704. return HAL_ERROR;
  705. }
  706. /* Write Blocks in Polling mode */
  707. if(NumberOfBlocks > 1U)
  708. {
  709. hmmc->Context = MMC_CONTEXT_WRITE_MULTIPLE_BLOCK;
  710. /* Write Multi Block command */
  711. errorstate = SDMMC_CmdWriteMultiBlock(hmmc->Instance, add);
  712. }
  713. else
  714. {
  715. hmmc->Context = MMC_CONTEXT_WRITE_SINGLE_BLOCK;
  716. /* Write Single Block command */
  717. errorstate = SDMMC_CmdWriteSingleBlock(hmmc->Instance, add);
  718. }
  719. if(errorstate != HAL_MMC_ERROR_NONE)
  720. {
  721. /* Clear all the static flags */
  722. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
  723. hmmc->ErrorCode |= errorstate;
  724. hmmc->State = HAL_MMC_STATE_READY;
  725. return HAL_ERROR;
  726. }
  727. /* Configure the MMC DPSM (Data Path State Machine) */
  728. config.DataTimeOut = SDMMC_DATATIMEOUT;
  729. config.DataLength = NumberOfBlocks * MMC_BLOCKSIZE;
  730. config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
  731. config.TransferDir = SDIO_TRANSFER_DIR_TO_CARD;
  732. config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
  733. config.DPSM = SDIO_DPSM_ENABLE;
  734. (void)SDIO_ConfigData(hmmc->Instance, &config);
  735. /* Write block(s) in polling mode */
  736. dataremaining = config.DataLength;
  737. #if defined(SDIO_STA_STBITERR)
  738. while(!__HAL_MMC_GET_FLAG(hmmc, SDIO_FLAG_TXUNDERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND | SDIO_FLAG_STBITERR))
  739. #else /* SDIO_STA_STBITERR not defined */
  740. while(!__HAL_MMC_GET_FLAG(hmmc, SDIO_FLAG_TXUNDERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND))
  741. #endif /* SDIO_STA_STBITERR */
  742. {
  743. if(__HAL_MMC_GET_FLAG(hmmc, SDIO_FLAG_TXFIFOHE) && (dataremaining > 0U))
  744. {
  745. /* Write data to SDIO Tx FIFO */
  746. for(count = 0U; count < 8U; count++)
  747. {
  748. data = (uint32_t)(*tempbuff);
  749. tempbuff++;
  750. dataremaining--;
  751. data |= ((uint32_t)(*tempbuff) << 8U);
  752. tempbuff++;
  753. dataremaining--;
  754. data |= ((uint32_t)(*tempbuff) << 16U);
  755. tempbuff++;
  756. dataremaining--;
  757. data |= ((uint32_t)(*tempbuff) << 24U);
  758. tempbuff++;
  759. dataremaining--;
  760. (void)SDIO_WriteFIFO(hmmc->Instance, &data);
  761. }
  762. }
  763. if(((HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U))
  764. {
  765. /* Clear all the static flags */
  766. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
  767. hmmc->ErrorCode |= errorstate;
  768. hmmc->State = HAL_MMC_STATE_READY;
  769. return HAL_TIMEOUT;
  770. }
  771. }
  772. /* Send stop transmission command in case of multiblock write */
  773. if(__HAL_MMC_GET_FLAG(hmmc, SDIO_FLAG_DATAEND) && (NumberOfBlocks > 1U))
  774. {
  775. /* Send stop transmission command */
  776. errorstate = SDMMC_CmdStopTransfer(hmmc->Instance);
  777. if(errorstate != HAL_MMC_ERROR_NONE)
  778. {
  779. /* Clear all the static flags */
  780. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
  781. hmmc->ErrorCode |= errorstate;
  782. hmmc->State = HAL_MMC_STATE_READY;
  783. return HAL_ERROR;
  784. }
  785. }
  786. /* Get error state */
  787. if(__HAL_MMC_GET_FLAG(hmmc, SDIO_FLAG_DTIMEOUT))
  788. {
  789. /* Clear all the static flags */
  790. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
  791. hmmc->ErrorCode |= HAL_MMC_ERROR_DATA_TIMEOUT;
  792. hmmc->State = HAL_MMC_STATE_READY;
  793. return HAL_ERROR;
  794. }
  795. else if(__HAL_MMC_GET_FLAG(hmmc, SDIO_FLAG_DCRCFAIL))
  796. {
  797. /* Clear all the static flags */
  798. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
  799. hmmc->ErrorCode |= HAL_MMC_ERROR_DATA_CRC_FAIL;
  800. hmmc->State = HAL_MMC_STATE_READY;
  801. return HAL_ERROR;
  802. }
  803. else if(__HAL_MMC_GET_FLAG(hmmc, SDIO_FLAG_TXUNDERR))
  804. {
  805. /* Clear all the static flags */
  806. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
  807. hmmc->ErrorCode |= HAL_MMC_ERROR_TX_UNDERRUN;
  808. hmmc->State = HAL_MMC_STATE_READY;
  809. return HAL_ERROR;
  810. }
  811. else
  812. {
  813. /* Nothing to do */
  814. }
  815. /* Clear all the static flags */
  816. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_DATA_FLAGS);
  817. hmmc->State = HAL_MMC_STATE_READY;
  818. return HAL_OK;
  819. }
  820. else
  821. {
  822. hmmc->ErrorCode |= HAL_MMC_ERROR_BUSY;
  823. return HAL_ERROR;
  824. }
  825. }
  826. /**
  827. * @brief Reads block(s) from a specified address in a card. The Data transfer
  828. * is managed in interrupt mode.
  829. * @note This API should be followed by a check on the card state through
  830. * HAL_MMC_GetCardState().
  831. * @note You could also check the IT transfer process through the MMC Rx
  832. * interrupt event.
  833. * @param hmmc: Pointer to MMC handle
  834. * @param pData: Pointer to the buffer that will contain the received data
  835. * @param BlockAdd: Block Address from where data is to be read
  836. * @param NumberOfBlocks: Number of blocks to read.
  837. * @retval HAL status
  838. */
  839. HAL_StatusTypeDef HAL_MMC_ReadBlocks_IT(MMC_HandleTypeDef *hmmc, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
  840. {
  841. SDIO_DataInitTypeDef config;
  842. uint32_t errorstate;
  843. uint32_t add = BlockAdd;
  844. if(NULL == pData)
  845. {
  846. hmmc->ErrorCode |= HAL_MMC_ERROR_PARAM;
  847. return HAL_ERROR;
  848. }
  849. if(hmmc->State == HAL_MMC_STATE_READY)
  850. {
  851. hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
  852. if((BlockAdd + NumberOfBlocks) > (hmmc->MmcCard.LogBlockNbr))
  853. {
  854. hmmc->ErrorCode |= HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
  855. return HAL_ERROR;
  856. }
  857. hmmc->State = HAL_MMC_STATE_BUSY;
  858. /* Initialize data control register */
  859. hmmc->Instance->DCTRL = 0U;
  860. hmmc->pRxBuffPtr = pData;
  861. hmmc->RxXferSize = MMC_BLOCKSIZE * NumberOfBlocks;
  862. #if defined(SDIO_STA_STBITERR)
  863. __HAL_MMC_ENABLE_IT(hmmc, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_DATAEND | SDIO_FLAG_RXFIFOHF | SDIO_IT_STBITERR));
  864. #else /* SDIO_STA_STBITERR not defined */
  865. __HAL_MMC_ENABLE_IT(hmmc, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_DATAEND | SDIO_FLAG_RXFIFOHF));
  866. #endif /* SDIO_STA_STBITERR */
  867. if ((hmmc->MmcCard.CardType) != MMC_HIGH_CAPACITY_CARD)
  868. {
  869. add *= 512U;
  870. }
  871. /* Set Block Size for Card */
  872. errorstate = SDMMC_CmdBlockLength(hmmc->Instance, MMC_BLOCKSIZE);
  873. if(errorstate != HAL_MMC_ERROR_NONE)
  874. {
  875. /* Clear all the static flags */
  876. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
  877. hmmc->ErrorCode |= errorstate;
  878. hmmc->State = HAL_MMC_STATE_READY;
  879. return HAL_ERROR;
  880. }
  881. /* Configure the MMC DPSM (Data Path State Machine) */
  882. config.DataTimeOut = SDMMC_DATATIMEOUT;
  883. config.DataLength = MMC_BLOCKSIZE * NumberOfBlocks;
  884. config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
  885. config.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
  886. config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
  887. config.DPSM = SDIO_DPSM_ENABLE;
  888. (void)SDIO_ConfigData(hmmc->Instance, &config);
  889. /* Read Blocks in IT mode */
  890. if(NumberOfBlocks > 1U)
  891. {
  892. hmmc->Context = (MMC_CONTEXT_READ_MULTIPLE_BLOCK | MMC_CONTEXT_IT);
  893. /* Read Multi Block command */
  894. errorstate = SDMMC_CmdReadMultiBlock(hmmc->Instance, add);
  895. }
  896. else
  897. {
  898. hmmc->Context = (MMC_CONTEXT_READ_SINGLE_BLOCK | MMC_CONTEXT_IT);
  899. /* Read Single Block command */
  900. errorstate = SDMMC_CmdReadSingleBlock(hmmc->Instance, add);
  901. }
  902. if(errorstate != HAL_MMC_ERROR_NONE)
  903. {
  904. /* Clear all the static flags */
  905. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
  906. hmmc->ErrorCode |= errorstate;
  907. hmmc->State = HAL_MMC_STATE_READY;
  908. return HAL_ERROR;
  909. }
  910. return HAL_OK;
  911. }
  912. else
  913. {
  914. return HAL_BUSY;
  915. }
  916. }
  917. /**
  918. * @brief Writes block(s) to a specified address in a card. The Data transfer
  919. * is managed in interrupt mode.
  920. * @note This API should be followed by a check on the card state through
  921. * HAL_MMC_GetCardState().
  922. * @note You could also check the IT transfer process through the MMC Tx
  923. * interrupt event.
  924. * @param hmmc: Pointer to MMC handle
  925. * @param pData: Pointer to the buffer that will contain the data to transmit
  926. * @param BlockAdd: Block Address where data will be written
  927. * @param NumberOfBlocks: Number of blocks to write
  928. * @retval HAL status
  929. */
  930. HAL_StatusTypeDef HAL_MMC_WriteBlocks_IT(MMC_HandleTypeDef *hmmc, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
  931. {
  932. SDIO_DataInitTypeDef config;
  933. uint32_t errorstate;
  934. uint32_t add = BlockAdd;
  935. if(NULL == pData)
  936. {
  937. hmmc->ErrorCode |= HAL_MMC_ERROR_PARAM;
  938. return HAL_ERROR;
  939. }
  940. if(hmmc->State == HAL_MMC_STATE_READY)
  941. {
  942. hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
  943. if((BlockAdd + NumberOfBlocks) > (hmmc->MmcCard.LogBlockNbr))
  944. {
  945. hmmc->ErrorCode |= HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
  946. return HAL_ERROR;
  947. }
  948. hmmc->State = HAL_MMC_STATE_BUSY;
  949. /* Initialize data control register */
  950. hmmc->Instance->DCTRL = 0U;
  951. hmmc->pTxBuffPtr = pData;
  952. hmmc->TxXferSize = MMC_BLOCKSIZE * NumberOfBlocks;
  953. /* Enable transfer interrupts */
  954. #if defined(SDIO_STA_STBITERR)
  955. __HAL_MMC_ENABLE_IT(hmmc, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR | SDIO_IT_DATAEND | SDIO_FLAG_TXFIFOHE | SDIO_IT_STBITERR));
  956. #else /* SDIO_STA_STBITERR not defined */
  957. __HAL_MMC_ENABLE_IT(hmmc, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR | SDIO_IT_DATAEND | SDIO_FLAG_TXFIFOHE));
  958. #endif /* SDIO_STA_STBITERR */
  959. if ((hmmc->MmcCard.CardType) != MMC_HIGH_CAPACITY_CARD)
  960. {
  961. add *= 512U;
  962. }
  963. /* Set Block Size for Card */
  964. errorstate = SDMMC_CmdBlockLength(hmmc->Instance, MMC_BLOCKSIZE);
  965. if(errorstate != HAL_MMC_ERROR_NONE)
  966. {
  967. /* Clear all the static flags */
  968. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
  969. hmmc->ErrorCode |= errorstate;
  970. hmmc->State = HAL_MMC_STATE_READY;
  971. return HAL_ERROR;
  972. }
  973. /* Write Blocks in Polling mode */
  974. if(NumberOfBlocks > 1U)
  975. {
  976. hmmc->Context = (MMC_CONTEXT_WRITE_MULTIPLE_BLOCK| MMC_CONTEXT_IT);
  977. /* Write Multi Block command */
  978. errorstate = SDMMC_CmdWriteMultiBlock(hmmc->Instance, add);
  979. }
  980. else
  981. {
  982. hmmc->Context = (MMC_CONTEXT_WRITE_SINGLE_BLOCK | MMC_CONTEXT_IT);
  983. /* Write Single Block command */
  984. errorstate = SDMMC_CmdWriteSingleBlock(hmmc->Instance, add);
  985. }
  986. if(errorstate != HAL_MMC_ERROR_NONE)
  987. {
  988. /* Clear all the static flags */
  989. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
  990. hmmc->ErrorCode |= errorstate;
  991. hmmc->State = HAL_MMC_STATE_READY;
  992. return HAL_ERROR;
  993. }
  994. /* Configure the MMC DPSM (Data Path State Machine) */
  995. config.DataTimeOut = SDMMC_DATATIMEOUT;
  996. config.DataLength = MMC_BLOCKSIZE * NumberOfBlocks;
  997. config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
  998. config.TransferDir = SDIO_TRANSFER_DIR_TO_CARD;
  999. config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
  1000. config.DPSM = SDIO_DPSM_ENABLE;
  1001. (void)SDIO_ConfigData(hmmc->Instance, &config);
  1002. return HAL_OK;
  1003. }
  1004. else
  1005. {
  1006. return HAL_BUSY;
  1007. }
  1008. }
  1009. /**
  1010. * @brief Reads block(s) from a specified address in a card. The Data transfer
  1011. * is managed by DMA mode.
  1012. * @note This API should be followed by a check on the card state through
  1013. * HAL_MMC_GetCardState().
  1014. * @note You could also check the DMA transfer process through the MMC Rx
  1015. * interrupt event.
  1016. * @param hmmc: Pointer MMC handle
  1017. * @param pData: Pointer to the buffer that will contain the received data
  1018. * @param BlockAdd: Block Address from where data is to be read
  1019. * @param NumberOfBlocks: Number of blocks to read.
  1020. * @retval HAL status
  1021. */
  1022. HAL_StatusTypeDef HAL_MMC_ReadBlocks_DMA(MMC_HandleTypeDef *hmmc, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
  1023. {
  1024. SDIO_DataInitTypeDef config;
  1025. uint32_t errorstate;
  1026. uint32_t add = BlockAdd;
  1027. if(NULL == pData)
  1028. {
  1029. hmmc->ErrorCode |= HAL_MMC_ERROR_PARAM;
  1030. return HAL_ERROR;
  1031. }
  1032. if(hmmc->State == HAL_MMC_STATE_READY)
  1033. {
  1034. hmmc->ErrorCode = HAL_DMA_ERROR_NONE;
  1035. if((BlockAdd + NumberOfBlocks) > (hmmc->MmcCard.LogBlockNbr))
  1036. {
  1037. hmmc->ErrorCode |= HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
  1038. return HAL_ERROR;
  1039. }
  1040. hmmc->State = HAL_MMC_STATE_BUSY;
  1041. /* Initialize data control register */
  1042. hmmc->Instance->DCTRL = 0U;
  1043. #if defined(SDIO_STA_STBITERR)
  1044. __HAL_MMC_ENABLE_IT(hmmc, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_DATAEND | SDIO_IT_STBITERR));
  1045. #else /* SDIO_STA_STBITERR not defined */
  1046. __HAL_MMC_ENABLE_IT(hmmc, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_DATAEND));
  1047. #endif /* SDIO_STA_STBITERR */
  1048. /* Set the DMA transfer complete callback */
  1049. hmmc->hdmarx->XferCpltCallback = MMC_DMAReceiveCplt;
  1050. /* Set the DMA error callback */
  1051. hmmc->hdmarx->XferErrorCallback = MMC_DMAError;
  1052. /* Set the DMA Abort callback */
  1053. hmmc->hdmarx->XferAbortCallback = NULL;
  1054. if ((hmmc->MmcCard.CardType) != MMC_HIGH_CAPACITY_CARD)
  1055. {
  1056. add *= 512U;
  1057. }
  1058. /* Set Block Size for Card */
  1059. errorstate = SDMMC_CmdBlockLength(hmmc->Instance, MMC_BLOCKSIZE);
  1060. if(errorstate != HAL_MMC_ERROR_NONE)
  1061. {
  1062. /* Clear all the static flags */
  1063. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
  1064. hmmc->ErrorCode = errorstate;
  1065. hmmc->State = HAL_MMC_STATE_READY;
  1066. return HAL_ERROR;
  1067. }
  1068. /* Enable the DMA Channel */
  1069. if(HAL_DMA_Start_IT(hmmc->hdmarx, (uint32_t)&hmmc->Instance->FIFO, (uint32_t)pData, (uint32_t)(MMC_BLOCKSIZE * NumberOfBlocks)/4) != HAL_OK)
  1070. {
  1071. __HAL_MMC_DISABLE_IT(hmmc, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_DATAEND));
  1072. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
  1073. hmmc->ErrorCode = HAL_MMC_ERROR_DMA;
  1074. hmmc->State = HAL_MMC_STATE_READY;
  1075. return HAL_ERROR;
  1076. }
  1077. else
  1078. {
  1079. /* Enable MMC DMA transfer */
  1080. __HAL_MMC_DMA_ENABLE(hmmc);
  1081. /* Configure the MMC DPSM (Data Path State Machine) */
  1082. config.DataTimeOut = SDMMC_DATATIMEOUT;
  1083. config.DataLength = MMC_BLOCKSIZE * NumberOfBlocks;
  1084. config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
  1085. config.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
  1086. config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
  1087. config.DPSM = SDIO_DPSM_ENABLE;
  1088. (void)SDIO_ConfigData(hmmc->Instance, &config);
  1089. /* Read Blocks in DMA mode */
  1090. if(NumberOfBlocks > 1U)
  1091. {
  1092. hmmc->Context = (MMC_CONTEXT_READ_MULTIPLE_BLOCK | MMC_CONTEXT_DMA);
  1093. /* Read Multi Block command */
  1094. errorstate = SDMMC_CmdReadMultiBlock(hmmc->Instance, add);
  1095. }
  1096. else
  1097. {
  1098. hmmc->Context = (MMC_CONTEXT_READ_SINGLE_BLOCK | MMC_CONTEXT_DMA);
  1099. /* Read Single Block command */
  1100. errorstate = SDMMC_CmdReadSingleBlock(hmmc->Instance, add);
  1101. }
  1102. if(errorstate != HAL_MMC_ERROR_NONE)
  1103. {
  1104. /* Clear all the static flags */
  1105. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
  1106. __HAL_MMC_DISABLE_IT(hmmc, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_DATAEND));
  1107. hmmc->ErrorCode = errorstate;
  1108. hmmc->State = HAL_MMC_STATE_READY;
  1109. return HAL_ERROR;
  1110. }
  1111. return HAL_OK;
  1112. }
  1113. }
  1114. else
  1115. {
  1116. return HAL_BUSY;
  1117. }
  1118. }
  1119. /**
  1120. * @brief Writes block(s) to a specified address in a card. The Data transfer
  1121. * is managed by DMA mode.
  1122. * @note This API should be followed by a check on the card state through
  1123. * HAL_MMC_GetCardState().
  1124. * @note You could also check the DMA transfer process through the MMC Tx
  1125. * interrupt event.
  1126. * @param hmmc: Pointer to MMC handle
  1127. * @param pData: Pointer to the buffer that will contain the data to transmit
  1128. * @param BlockAdd: Block Address where data will be written
  1129. * @param NumberOfBlocks: Number of blocks to write
  1130. * @retval HAL status
  1131. */
  1132. HAL_StatusTypeDef HAL_MMC_WriteBlocks_DMA(MMC_HandleTypeDef *hmmc, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
  1133. {
  1134. SDIO_DataInitTypeDef config;
  1135. uint32_t errorstate;
  1136. uint32_t add = BlockAdd;
  1137. if(NULL == pData)
  1138. {
  1139. hmmc->ErrorCode |= HAL_MMC_ERROR_PARAM;
  1140. return HAL_ERROR;
  1141. }
  1142. if(hmmc->State == HAL_MMC_STATE_READY)
  1143. {
  1144. hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
  1145. if((BlockAdd + NumberOfBlocks) > (hmmc->MmcCard.LogBlockNbr))
  1146. {
  1147. hmmc->ErrorCode |= HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
  1148. return HAL_ERROR;
  1149. }
  1150. hmmc->State = HAL_MMC_STATE_BUSY;
  1151. /* Initialize data control register */
  1152. hmmc->Instance->DCTRL = 0U;
  1153. /* Enable MMC Error interrupts */
  1154. #if defined(SDIO_STA_STBITERR)
  1155. __HAL_MMC_ENABLE_IT(hmmc, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR | SDIO_IT_STBITERR));
  1156. #else /* SDIO_STA_STBITERR not defined */
  1157. __HAL_MMC_ENABLE_IT(hmmc, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR));
  1158. #endif /* SDIO_STA_STBITERR */
  1159. /* Set the DMA transfer complete callback */
  1160. hmmc->hdmatx->XferCpltCallback = MMC_DMATransmitCplt;
  1161. /* Set the DMA error callback */
  1162. hmmc->hdmatx->XferErrorCallback = MMC_DMAError;
  1163. /* Set the DMA Abort callback */
  1164. hmmc->hdmatx->XferAbortCallback = NULL;
  1165. if ((hmmc->MmcCard.CardType) != MMC_HIGH_CAPACITY_CARD)
  1166. {
  1167. add *= 512U;
  1168. }
  1169. /* Set Block Size for Card */
  1170. errorstate = SDMMC_CmdBlockLength(hmmc->Instance, MMC_BLOCKSIZE);
  1171. if(errorstate != HAL_MMC_ERROR_NONE)
  1172. {
  1173. /* Clear all the static flags */
  1174. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
  1175. hmmc->ErrorCode |= errorstate;
  1176. hmmc->State = HAL_MMC_STATE_READY;
  1177. return HAL_ERROR;
  1178. }
  1179. /* Write Blocks in Polling mode */
  1180. if(NumberOfBlocks > 1U)
  1181. {
  1182. hmmc->Context = (MMC_CONTEXT_WRITE_MULTIPLE_BLOCK | MMC_CONTEXT_DMA);
  1183. /* Write Multi Block command */
  1184. errorstate = SDMMC_CmdWriteMultiBlock(hmmc->Instance, add);
  1185. }
  1186. else
  1187. {
  1188. hmmc->Context = (MMC_CONTEXT_WRITE_SINGLE_BLOCK | MMC_CONTEXT_DMA);
  1189. /* Write Single Block command */
  1190. errorstate = SDMMC_CmdWriteSingleBlock(hmmc->Instance, add);
  1191. }
  1192. if(errorstate != HAL_MMC_ERROR_NONE)
  1193. {
  1194. /* Clear all the static flags */
  1195. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
  1196. __HAL_MMC_DISABLE_IT(hmmc, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR | SDIO_IT_DATAEND));
  1197. hmmc->ErrorCode |= errorstate;
  1198. hmmc->State = HAL_MMC_STATE_READY;
  1199. return HAL_ERROR;
  1200. }
  1201. /* Enable SDIO DMA transfer */
  1202. __HAL_MMC_DMA_ENABLE(hmmc);
  1203. /* Enable the DMA Channel */
  1204. if(HAL_DMA_Start_IT(hmmc->hdmatx, (uint32_t)pData, (uint32_t)&hmmc->Instance->FIFO, (uint32_t)(MMC_BLOCKSIZE * NumberOfBlocks)/4) != HAL_OK)
  1205. {
  1206. __HAL_MMC_DISABLE_IT(hmmc, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR | SDIO_IT_DATAEND));
  1207. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
  1208. hmmc->ErrorCode |= HAL_MMC_ERROR_DMA;
  1209. hmmc->State = HAL_MMC_STATE_READY;
  1210. return HAL_ERROR;
  1211. }
  1212. else
  1213. {
  1214. /* Configure the MMC DPSM (Data Path State Machine) */
  1215. config.DataTimeOut = SDMMC_DATATIMEOUT;
  1216. config.DataLength = MMC_BLOCKSIZE * NumberOfBlocks;
  1217. config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
  1218. config.TransferDir = SDIO_TRANSFER_DIR_TO_CARD;
  1219. config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
  1220. config.DPSM = SDIO_DPSM_ENABLE;
  1221. (void)SDIO_ConfigData(hmmc->Instance, &config);
  1222. return HAL_OK;
  1223. }
  1224. }
  1225. else
  1226. {
  1227. return HAL_BUSY;
  1228. }
  1229. }
  1230. /**
  1231. * @brief Erases the specified memory area of the given MMC card.
  1232. * @note This API should be followed by a check on the card state through
  1233. * HAL_MMC_GetCardState().
  1234. * @param hmmc: Pointer to MMC handle
  1235. * @param BlockStartAdd: Start Block address
  1236. * @param BlockEndAdd: End Block address
  1237. * @retval HAL status
  1238. */
  1239. HAL_StatusTypeDef HAL_MMC_Erase(MMC_HandleTypeDef *hmmc, uint32_t BlockStartAdd, uint32_t BlockEndAdd)
  1240. {
  1241. uint32_t errorstate;
  1242. uint32_t start_add = BlockStartAdd;
  1243. uint32_t end_add = BlockEndAdd;
  1244. if(hmmc->State == HAL_MMC_STATE_READY)
  1245. {
  1246. hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
  1247. if(end_add < start_add)
  1248. {
  1249. hmmc->ErrorCode |= HAL_MMC_ERROR_PARAM;
  1250. return HAL_ERROR;
  1251. }
  1252. if(end_add > (hmmc->MmcCard.LogBlockNbr))
  1253. {
  1254. hmmc->ErrorCode |= HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
  1255. return HAL_ERROR;
  1256. }
  1257. hmmc->State = HAL_MMC_STATE_BUSY;
  1258. /* Check if the card command class supports erase command */
  1259. if(((hmmc->MmcCard.Class) & SDIO_CCCC_ERASE) == 0U)
  1260. {
  1261. /* Clear all the static flags */
  1262. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
  1263. hmmc->ErrorCode |= HAL_MMC_ERROR_REQUEST_NOT_APPLICABLE;
  1264. hmmc->State = HAL_MMC_STATE_READY;
  1265. return HAL_ERROR;
  1266. }
  1267. if((SDIO_GetResponse(hmmc->Instance, SDIO_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
  1268. {
  1269. /* Clear all the static flags */
  1270. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
  1271. hmmc->ErrorCode |= HAL_MMC_ERROR_LOCK_UNLOCK_FAILED;
  1272. hmmc->State = HAL_MMC_STATE_READY;
  1273. return HAL_ERROR;
  1274. }
  1275. if ((hmmc->MmcCard.CardType) != MMC_HIGH_CAPACITY_CARD)
  1276. {
  1277. start_add *= 512U;
  1278. end_add *= 512U;
  1279. }
  1280. /* Send CMD35 MMC_ERASE_GRP_START with argument as addr */
  1281. errorstate = SDMMC_CmdEraseStartAdd(hmmc->Instance, start_add);
  1282. if(errorstate != HAL_MMC_ERROR_NONE)
  1283. {
  1284. /* Clear all the static flags */
  1285. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
  1286. hmmc->ErrorCode |= errorstate;
  1287. hmmc->State = HAL_MMC_STATE_READY;
  1288. return HAL_ERROR;
  1289. }
  1290. /* Send CMD36 MMC_ERASE_GRP_END with argument as addr */
  1291. errorstate = SDMMC_CmdEraseEndAdd(hmmc->Instance, end_add);
  1292. if(errorstate != HAL_MMC_ERROR_NONE)
  1293. {
  1294. /* Clear all the static flags */
  1295. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
  1296. hmmc->ErrorCode |= errorstate;
  1297. hmmc->State = HAL_MMC_STATE_READY;
  1298. return HAL_ERROR;
  1299. }
  1300. /* Send CMD38 ERASE */
  1301. errorstate = SDMMC_CmdErase(hmmc->Instance);
  1302. if(errorstate != HAL_MMC_ERROR_NONE)
  1303. {
  1304. /* Clear all the static flags */
  1305. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
  1306. hmmc->ErrorCode |= errorstate;
  1307. hmmc->State = HAL_MMC_STATE_READY;
  1308. return HAL_ERROR;
  1309. }
  1310. hmmc->State = HAL_MMC_STATE_READY;
  1311. return HAL_OK;
  1312. }
  1313. else
  1314. {
  1315. return HAL_BUSY;
  1316. }
  1317. }
  1318. /**
  1319. * @brief This function handles MMC card interrupt request.
  1320. * @param hmmc: Pointer to MMC handle
  1321. * @retval None
  1322. */
  1323. void HAL_MMC_IRQHandler(MMC_HandleTypeDef *hmmc)
  1324. {
  1325. uint32_t errorstate;
  1326. uint32_t context = hmmc->Context;
  1327. /* Check for SDIO interrupt flags */
  1328. if((__HAL_MMC_GET_FLAG(hmmc, SDIO_FLAG_RXFIFOHF) != RESET) && ((context & MMC_CONTEXT_IT) != 0U))
  1329. {
  1330. MMC_Read_IT(hmmc);
  1331. }
  1332. else if(__HAL_MMC_GET_FLAG(hmmc, SDIO_FLAG_DATAEND) != RESET)
  1333. {
  1334. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_FLAG_DATAEND);
  1335. #if defined(SDIO_STA_STBITERR)
  1336. __HAL_MMC_DISABLE_IT(hmmc, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
  1337. SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR | SDIO_IT_STBITERR);
  1338. #else /* SDIO_STA_STBITERR not defined */
  1339. __HAL_MMC_DISABLE_IT(hmmc, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT |\
  1340. SDIO_IT_TXUNDERR | SDIO_IT_RXOVERR | SDIO_IT_TXFIFOHE |\
  1341. SDIO_IT_RXFIFOHF);
  1342. #endif /* SDIO_STA_STBITERR */
  1343. hmmc->Instance->DCTRL &= ~(SDIO_DCTRL_DTEN);
  1344. if((context & MMC_CONTEXT_DMA) != 0U)
  1345. {
  1346. if((context & MMC_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U)
  1347. {
  1348. errorstate = SDMMC_CmdStopTransfer(hmmc->Instance);
  1349. if(errorstate != HAL_MMC_ERROR_NONE)
  1350. {
  1351. hmmc->ErrorCode |= errorstate;
  1352. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  1353. hmmc->ErrorCallback(hmmc);
  1354. #else
  1355. HAL_MMC_ErrorCallback(hmmc);
  1356. #endif
  1357. }
  1358. }
  1359. if(((context & MMC_CONTEXT_READ_SINGLE_BLOCK) == 0U) && ((context & MMC_CONTEXT_READ_MULTIPLE_BLOCK) == 0U))
  1360. {
  1361. /* Disable the DMA transfer for transmit request by setting the DMAEN bit
  1362. in the MMC DCTRL register */
  1363. hmmc->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN);
  1364. hmmc->State = HAL_MMC_STATE_READY;
  1365. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  1366. hmmc->TxCpltCallback(hmmc);
  1367. #else
  1368. HAL_MMC_TxCpltCallback(hmmc);
  1369. #endif
  1370. }
  1371. }
  1372. else if((context & MMC_CONTEXT_IT) != 0U)
  1373. {
  1374. /* Stop Transfer for Write Multi blocks or Read Multi blocks */
  1375. if(((context & MMC_CONTEXT_READ_MULTIPLE_BLOCK) != 0U) || ((context & MMC_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
  1376. {
  1377. errorstate = SDMMC_CmdStopTransfer(hmmc->Instance);
  1378. if(errorstate != HAL_MMC_ERROR_NONE)
  1379. {
  1380. hmmc->ErrorCode |= errorstate;
  1381. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  1382. hmmc->ErrorCallback(hmmc);
  1383. #else
  1384. HAL_MMC_ErrorCallback(hmmc);
  1385. #endif
  1386. }
  1387. }
  1388. /* Clear all the static flags */
  1389. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_DATA_FLAGS);
  1390. hmmc->State = HAL_MMC_STATE_READY;
  1391. if(((context & MMC_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & MMC_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
  1392. {
  1393. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  1394. hmmc->RxCpltCallback(hmmc);
  1395. #else
  1396. HAL_MMC_RxCpltCallback(hmmc);
  1397. #endif
  1398. }
  1399. else
  1400. {
  1401. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  1402. hmmc->TxCpltCallback(hmmc);
  1403. #else
  1404. HAL_MMC_TxCpltCallback(hmmc);
  1405. #endif
  1406. }
  1407. }
  1408. else
  1409. {
  1410. /* Nothing to do */
  1411. }
  1412. }
  1413. else if((__HAL_MMC_GET_FLAG(hmmc, SDIO_FLAG_TXFIFOHE) != RESET) && ((context & MMC_CONTEXT_IT) != 0U))
  1414. {
  1415. MMC_Write_IT(hmmc);
  1416. }
  1417. #if defined(SDIO_STA_STBITERR)
  1418. else if(__HAL_MMC_GET_FLAG(hmmc, SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_RXOVERR | SDIO_FLAG_TXUNDERR | SDIO_FLAG_STBITERR) != RESET)
  1419. #else /* SDIO_STA_STBITERR not defined */
  1420. else if(__HAL_MMC_GET_FLAG(hmmc, SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_RXOVERR | SDIO_FLAG_TXUNDERR) != RESET)
  1421. #endif /* SDIO_STA_STBITERR */
  1422. {
  1423. /* Set Error code */
  1424. if(__HAL_MMC_GET_FLAG(hmmc, SDIO_FLAG_DCRCFAIL) != RESET)
  1425. {
  1426. hmmc->ErrorCode |= HAL_MMC_ERROR_DATA_CRC_FAIL;
  1427. }
  1428. if(__HAL_MMC_GET_FLAG(hmmc, SDIO_FLAG_DTIMEOUT) != RESET)
  1429. {
  1430. hmmc->ErrorCode |= HAL_MMC_ERROR_DATA_TIMEOUT;
  1431. }
  1432. if(__HAL_MMC_GET_FLAG(hmmc, SDIO_FLAG_RXOVERR) != RESET)
  1433. {
  1434. hmmc->ErrorCode |= HAL_MMC_ERROR_RX_OVERRUN;
  1435. }
  1436. if(__HAL_MMC_GET_FLAG(hmmc, SDIO_FLAG_TXUNDERR) != RESET)
  1437. {
  1438. hmmc->ErrorCode |= HAL_MMC_ERROR_TX_UNDERRUN;
  1439. }
  1440. #if defined(SDIO_STA_STBITERR)
  1441. if(__HAL_MMC_GET_FLAG(hmmc, SDIO_FLAG_STBITERR) != RESET)
  1442. {
  1443. hmmc->ErrorCode |= HAL_MMC_ERROR_DATA_TIMEOUT;
  1444. }
  1445. #endif /* SDIO_STA_STBITERR */
  1446. #if defined(SDIO_STA_STBITERR)
  1447. /* Clear All flags */
  1448. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_DATA_FLAGS | SDIO_FLAG_STBITERR);
  1449. /* Disable all interrupts */
  1450. __HAL_MMC_DISABLE_IT(hmmc, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
  1451. SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR | SDIO_IT_STBITERR);
  1452. #else /* SDIO_STA_STBITERR */
  1453. /* Clear All flags */
  1454. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_DATA_FLAGS);
  1455. /* Disable all interrupts */
  1456. __HAL_MMC_DISABLE_IT(hmmc, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
  1457. SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR);
  1458. #endif /* SDIO_STA_STBITERR */
  1459. hmmc->ErrorCode |= SDMMC_CmdStopTransfer(hmmc->Instance);
  1460. if((context & MMC_CONTEXT_IT) != 0U)
  1461. {
  1462. /* Set the MMC state to ready to be able to start again the process */
  1463. hmmc->State = HAL_MMC_STATE_READY;
  1464. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  1465. hmmc->ErrorCallback(hmmc);
  1466. #else
  1467. HAL_MMC_ErrorCallback(hmmc);
  1468. #endif /* USE_HAL_MMC_REGISTER_CALLBACKS */
  1469. }
  1470. else if((context & MMC_CONTEXT_DMA) != 0U)
  1471. {
  1472. /* Abort the MMC DMA Streams */
  1473. if(hmmc->hdmatx != NULL)
  1474. {
  1475. /* Set the DMA Tx abort callback */
  1476. hmmc->hdmatx->XferAbortCallback = MMC_DMATxAbort;
  1477. /* Abort DMA in IT mode */
  1478. if(HAL_DMA_Abort_IT(hmmc->hdmatx) != HAL_OK)
  1479. {
  1480. MMC_DMATxAbort(hmmc->hdmatx);
  1481. }
  1482. }
  1483. else if(hmmc->hdmarx != NULL)
  1484. {
  1485. /* Set the DMA Rx abort callback */
  1486. hmmc->hdmarx->XferAbortCallback = MMC_DMARxAbort;
  1487. /* Abort DMA in IT mode */
  1488. if(HAL_DMA_Abort_IT(hmmc->hdmarx) != HAL_OK)
  1489. {
  1490. MMC_DMARxAbort(hmmc->hdmarx);
  1491. }
  1492. }
  1493. else
  1494. {
  1495. hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
  1496. hmmc->State = HAL_MMC_STATE_READY;
  1497. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  1498. hmmc->AbortCpltCallback(hmmc);
  1499. #else
  1500. HAL_MMC_AbortCallback(hmmc);
  1501. #endif
  1502. }
  1503. }
  1504. else
  1505. {
  1506. /* Nothing to do */
  1507. }
  1508. }
  1509. else
  1510. {
  1511. /* Nothing to do */
  1512. }
  1513. }
  1514. /**
  1515. * @brief return the MMC state
  1516. * @param hmmc: Pointer to mmc handle
  1517. * @retval HAL state
  1518. */
  1519. HAL_MMC_StateTypeDef HAL_MMC_GetState(MMC_HandleTypeDef *hmmc)
  1520. {
  1521. return hmmc->State;
  1522. }
  1523. /**
  1524. * @brief Return the MMC error code
  1525. * @param hmmc : Pointer to a MMC_HandleTypeDef structure that contains
  1526. * the configuration information.
  1527. * @retval MMC Error Code
  1528. */
  1529. uint32_t HAL_MMC_GetError(MMC_HandleTypeDef *hmmc)
  1530. {
  1531. return hmmc->ErrorCode;
  1532. }
  1533. /**
  1534. * @brief Tx Transfer completed callbacks
  1535. * @param hmmc: Pointer to MMC handle
  1536. * @retval None
  1537. */
  1538. __weak void HAL_MMC_TxCpltCallback(MMC_HandleTypeDef *hmmc)
  1539. {
  1540. /* Prevent unused argument(s) compilation warning */
  1541. UNUSED(hmmc);
  1542. /* NOTE : This function should not be modified, when the callback is needed,
  1543. the HAL_MMC_TxCpltCallback can be implemented in the user file
  1544. */
  1545. }
  1546. /**
  1547. * @brief Rx Transfer completed callbacks
  1548. * @param hmmc: Pointer MMC handle
  1549. * @retval None
  1550. */
  1551. __weak void HAL_MMC_RxCpltCallback(MMC_HandleTypeDef *hmmc)
  1552. {
  1553. /* Prevent unused argument(s) compilation warning */
  1554. UNUSED(hmmc);
  1555. /* NOTE : This function should not be modified, when the callback is needed,
  1556. the HAL_MMC_RxCpltCallback can be implemented in the user file
  1557. */
  1558. }
  1559. /**
  1560. * @brief MMC error callbacks
  1561. * @param hmmc: Pointer MMC handle
  1562. * @retval None
  1563. */
  1564. __weak void HAL_MMC_ErrorCallback(MMC_HandleTypeDef *hmmc)
  1565. {
  1566. /* Prevent unused argument(s) compilation warning */
  1567. UNUSED(hmmc);
  1568. /* NOTE : This function should not be modified, when the callback is needed,
  1569. the HAL_MMC_ErrorCallback can be implemented in the user file
  1570. */
  1571. }
  1572. /**
  1573. * @brief MMC Abort callbacks
  1574. * @param hmmc: Pointer MMC handle
  1575. * @retval None
  1576. */
  1577. __weak void HAL_MMC_AbortCallback(MMC_HandleTypeDef *hmmc)
  1578. {
  1579. /* Prevent unused argument(s) compilation warning */
  1580. UNUSED(hmmc);
  1581. /* NOTE : This function should not be modified, when the callback is needed,
  1582. the HAL_MMC_AbortCallback can be implemented in the user file
  1583. */
  1584. }
  1585. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  1586. /**
  1587. * @brief Register a User MMC Callback
  1588. * To be used instead of the weak (surcharged) predefined callback
  1589. * @param hmmc : MMC handle
  1590. * @param CallbackId : ID of the callback to be registered
  1591. * This parameter can be one of the following values:
  1592. * @arg @ref HAL_MMC_TX_CPLT_CB_ID MMC Tx Complete Callback ID
  1593. * @arg @ref HAL_MMC_RX_CPLT_CB_ID MMC Rx Complete Callback ID
  1594. * @arg @ref HAL_MMC_ERROR_CB_ID MMC Error Callback ID
  1595. * @arg @ref HAL_MMC_ABORT_CB_ID MMC Abort Callback ID
  1596. * @arg @ref HAL_MMC_MSP_INIT_CB_ID MMC MspInit Callback ID
  1597. * @arg @ref HAL_MMC_MSP_DEINIT_CB_ID MMC MspDeInit Callback ID
  1598. * @param pCallback : pointer to the Callback function
  1599. * @retval status
  1600. */
  1601. HAL_StatusTypeDef HAL_MMC_RegisterCallback(MMC_HandleTypeDef *hmmc, HAL_MMC_CallbackIDTypeDef CallbackId, pMMC_CallbackTypeDef pCallback)
  1602. {
  1603. HAL_StatusTypeDef status = HAL_OK;
  1604. if(pCallback == NULL)
  1605. {
  1606. /* Update the error code */
  1607. hmmc->ErrorCode |= HAL_MMC_ERROR_INVALID_CALLBACK;
  1608. return HAL_ERROR;
  1609. }
  1610. /* Process locked */
  1611. __HAL_LOCK(hmmc);
  1612. if(hmmc->State == HAL_MMC_STATE_READY)
  1613. {
  1614. switch (CallbackId)
  1615. {
  1616. case HAL_MMC_TX_CPLT_CB_ID :
  1617. hmmc->TxCpltCallback = pCallback;
  1618. break;
  1619. case HAL_MMC_RX_CPLT_CB_ID :
  1620. hmmc->RxCpltCallback = pCallback;
  1621. break;
  1622. case HAL_MMC_ERROR_CB_ID :
  1623. hmmc->ErrorCallback = pCallback;
  1624. break;
  1625. case HAL_MMC_ABORT_CB_ID :
  1626. hmmc->AbortCpltCallback = pCallback;
  1627. break;
  1628. case HAL_MMC_MSP_INIT_CB_ID :
  1629. hmmc->MspInitCallback = pCallback;
  1630. break;
  1631. case HAL_MMC_MSP_DEINIT_CB_ID :
  1632. hmmc->MspDeInitCallback = pCallback;
  1633. break;
  1634. default :
  1635. /* Update the error code */
  1636. hmmc->ErrorCode |= HAL_MMC_ERROR_INVALID_CALLBACK;
  1637. /* update return status */
  1638. status = HAL_ERROR;
  1639. break;
  1640. }
  1641. }
  1642. else if (hmmc->State == HAL_MMC_STATE_RESET)
  1643. {
  1644. switch (CallbackId)
  1645. {
  1646. case HAL_MMC_MSP_INIT_CB_ID :
  1647. hmmc->MspInitCallback = pCallback;
  1648. break;
  1649. case HAL_MMC_MSP_DEINIT_CB_ID :
  1650. hmmc->MspDeInitCallback = pCallback;
  1651. break;
  1652. default :
  1653. /* Update the error code */
  1654. hmmc->ErrorCode |= HAL_MMC_ERROR_INVALID_CALLBACK;
  1655. /* update return status */
  1656. status = HAL_ERROR;
  1657. break;
  1658. }
  1659. }
  1660. else
  1661. {
  1662. /* Update the error code */
  1663. hmmc->ErrorCode |= HAL_MMC_ERROR_INVALID_CALLBACK;
  1664. /* update return status */
  1665. status = HAL_ERROR;
  1666. }
  1667. /* Release Lock */
  1668. __HAL_UNLOCK(hmmc);
  1669. return status;
  1670. }
  1671. /**
  1672. * @brief Unregister a User MMC Callback
  1673. * MMC Callback is redirected to the weak (surcharged) predefined callback
  1674. * @param hmmc : MMC handle
  1675. * @param CallbackId : ID of the callback to be unregistered
  1676. * This parameter can be one of the following values:
  1677. * @arg @ref HAL_MMC_TX_CPLT_CB_ID MMC Tx Complete Callback ID
  1678. * @arg @ref HAL_MMC_RX_CPLT_CB_ID MMC Rx Complete Callback ID
  1679. * @arg @ref HAL_MMC_ERROR_CB_ID MMC Error Callback ID
  1680. * @arg @ref HAL_MMC_ABORT_CB_ID MMC Abort Callback ID
  1681. * @arg @ref HAL_MMC_MSP_INIT_CB_ID MMC MspInit Callback ID
  1682. * @arg @ref HAL_MMC_MSP_DEINIT_CB_ID MMC MspDeInit Callback ID
  1683. * @retval status
  1684. */
  1685. HAL_StatusTypeDef HAL_MMC_UnRegisterCallback(MMC_HandleTypeDef *hmmc, HAL_MMC_CallbackIDTypeDef CallbackId)
  1686. {
  1687. HAL_StatusTypeDef status = HAL_OK;
  1688. /* Process locked */
  1689. __HAL_LOCK(hmmc);
  1690. if(hmmc->State == HAL_MMC_STATE_READY)
  1691. {
  1692. switch (CallbackId)
  1693. {
  1694. case HAL_MMC_TX_CPLT_CB_ID :
  1695. hmmc->TxCpltCallback = HAL_MMC_TxCpltCallback;
  1696. break;
  1697. case HAL_MMC_RX_CPLT_CB_ID :
  1698. hmmc->RxCpltCallback = HAL_MMC_RxCpltCallback;
  1699. break;
  1700. case HAL_MMC_ERROR_CB_ID :
  1701. hmmc->ErrorCallback = HAL_MMC_ErrorCallback;
  1702. break;
  1703. case HAL_MMC_ABORT_CB_ID :
  1704. hmmc->AbortCpltCallback = HAL_MMC_AbortCallback;
  1705. break;
  1706. case HAL_MMC_MSP_INIT_CB_ID :
  1707. hmmc->MspInitCallback = HAL_MMC_MspInit;
  1708. break;
  1709. case HAL_MMC_MSP_DEINIT_CB_ID :
  1710. hmmc->MspDeInitCallback = HAL_MMC_MspDeInit;
  1711. break;
  1712. default :
  1713. /* Update the error code */
  1714. hmmc->ErrorCode |= HAL_MMC_ERROR_INVALID_CALLBACK;
  1715. /* update return status */
  1716. status = HAL_ERROR;
  1717. break;
  1718. }
  1719. }
  1720. else if (hmmc->State == HAL_MMC_STATE_RESET)
  1721. {
  1722. switch (CallbackId)
  1723. {
  1724. case HAL_MMC_MSP_INIT_CB_ID :
  1725. hmmc->MspInitCallback = HAL_MMC_MspInit;
  1726. break;
  1727. case HAL_MMC_MSP_DEINIT_CB_ID :
  1728. hmmc->MspDeInitCallback = HAL_MMC_MspDeInit;
  1729. break;
  1730. default :
  1731. /* Update the error code */
  1732. hmmc->ErrorCode |= HAL_MMC_ERROR_INVALID_CALLBACK;
  1733. /* update return status */
  1734. status = HAL_ERROR;
  1735. break;
  1736. }
  1737. }
  1738. else
  1739. {
  1740. /* Update the error code */
  1741. hmmc->ErrorCode |= HAL_MMC_ERROR_INVALID_CALLBACK;
  1742. /* update return status */
  1743. status = HAL_ERROR;
  1744. }
  1745. /* Release Lock */
  1746. __HAL_UNLOCK(hmmc);
  1747. return status;
  1748. }
  1749. #endif
  1750. /**
  1751. * @}
  1752. */
  1753. /** @addtogroup MMC_Exported_Functions_Group3
  1754. * @brief management functions
  1755. *
  1756. @verbatim
  1757. ==============================================================================
  1758. ##### Peripheral Control functions #####
  1759. ==============================================================================
  1760. [..]
  1761. This subsection provides a set of functions allowing to control the MMC card
  1762. operations and get the related information
  1763. @endverbatim
  1764. * @{
  1765. */
  1766. /**
  1767. * @brief Returns information the information of the card which are stored on
  1768. * the CID register.
  1769. * @param hmmc: Pointer to MMC handle
  1770. * @param pCID: Pointer to a HAL_MMC_CIDTypedef structure that
  1771. * contains all CID register parameters
  1772. * @retval HAL status
  1773. */
  1774. HAL_StatusTypeDef HAL_MMC_GetCardCID(MMC_HandleTypeDef *hmmc, HAL_MMC_CardCIDTypeDef *pCID)
  1775. {
  1776. pCID->ManufacturerID = (uint8_t)((hmmc->CID[0] & 0xFF000000U) >> 24U);
  1777. pCID->OEM_AppliID = (uint16_t)((hmmc->CID[0] & 0x00FFFF00U) >> 8U);
  1778. pCID->ProdName1 = (((hmmc->CID[0] & 0x000000FFU) << 24U) | ((hmmc->CID[1] & 0xFFFFFF00U) >> 8U));
  1779. pCID->ProdName2 = (uint8_t)(hmmc->CID[1] & 0x000000FFU);
  1780. pCID->ProdRev = (uint8_t)((hmmc->CID[2] & 0xFF000000U) >> 24U);
  1781. pCID->ProdSN = (((hmmc->CID[2] & 0x00FFFFFFU) << 8U) | ((hmmc->CID[3] & 0xFF000000U) >> 24U));
  1782. pCID->Reserved1 = (uint8_t)((hmmc->CID[3] & 0x00F00000U) >> 20U);
  1783. pCID->ManufactDate = (uint16_t)((hmmc->CID[3] & 0x000FFF00U) >> 8U);
  1784. pCID->CID_CRC = (uint8_t)((hmmc->CID[3] & 0x000000FEU) >> 1U);
  1785. pCID->Reserved2 = 1U;
  1786. return HAL_OK;
  1787. }
  1788. /**
  1789. * @brief Returns information the information of the card which are stored on
  1790. * the CSD register.
  1791. * @param hmmc: Pointer to MMC handle
  1792. * @param pCSD: Pointer to a HAL_MMC_CardCSDTypeDef structure that
  1793. * contains all CSD register parameters
  1794. * @retval HAL status
  1795. */
  1796. HAL_StatusTypeDef HAL_MMC_GetCardCSD(MMC_HandleTypeDef *hmmc, HAL_MMC_CardCSDTypeDef *pCSD)
  1797. {
  1798. pCSD->CSDStruct = (uint8_t)((hmmc->CSD[0] & 0xC0000000U) >> 30U);
  1799. pCSD->SysSpecVersion = (uint8_t)((hmmc->CSD[0] & 0x3C000000U) >> 26U);
  1800. pCSD->Reserved1 = (uint8_t)((hmmc->CSD[0] & 0x03000000U) >> 24U);
  1801. pCSD->TAAC = (uint8_t)((hmmc->CSD[0] & 0x00FF0000U) >> 16U);
  1802. pCSD->NSAC = (uint8_t)((hmmc->CSD[0] & 0x0000FF00U) >> 8U);
  1803. pCSD->MaxBusClkFrec = (uint8_t)(hmmc->CSD[0] & 0x000000FFU);
  1804. pCSD->CardComdClasses = (uint16_t)((hmmc->CSD[1] & 0xFFF00000U) >> 20U);
  1805. pCSD->RdBlockLen = (uint8_t)((hmmc->CSD[1] & 0x000F0000U) >> 16U);
  1806. pCSD->PartBlockRead = (uint8_t)((hmmc->CSD[1] & 0x00008000U) >> 15U);
  1807. pCSD->WrBlockMisalign = (uint8_t)((hmmc->CSD[1] & 0x00004000U) >> 14U);
  1808. pCSD->RdBlockMisalign = (uint8_t)((hmmc->CSD[1] & 0x00002000U) >> 13U);
  1809. pCSD->DSRImpl = (uint8_t)((hmmc->CSD[1] & 0x00001000U) >> 12U);
  1810. pCSD->Reserved2 = 0U; /*!< Reserved */
  1811. pCSD->DeviceSize = (((hmmc->CSD[1] & 0x000003FFU) << 2U) | ((hmmc->CSD[2] & 0xC0000000U) >> 30U));
  1812. pCSD->MaxRdCurrentVDDMin = (uint8_t)((hmmc->CSD[2] & 0x38000000U) >> 27U);
  1813. pCSD->MaxRdCurrentVDDMax = (uint8_t)((hmmc->CSD[2] & 0x07000000U) >> 24U);
  1814. pCSD->MaxWrCurrentVDDMin = (uint8_t)((hmmc->CSD[2] & 0x00E00000U) >> 21U);
  1815. pCSD->MaxWrCurrentVDDMax = (uint8_t)((hmmc->CSD[2] & 0x001C0000U) >> 18U);
  1816. pCSD->DeviceSizeMul = (uint8_t)((hmmc->CSD[2] & 0x00038000U) >> 15U);
  1817. hmmc->MmcCard.BlockNbr = (pCSD->DeviceSize + 1U) ;
  1818. hmmc->MmcCard.BlockNbr *= (1UL << ((pCSD->DeviceSizeMul & 0x07U) + 2U));
  1819. hmmc->MmcCard.BlockSize = (1UL << (pCSD->RdBlockLen & 0x0FU));
  1820. hmmc->MmcCard.LogBlockNbr = (hmmc->MmcCard.BlockNbr) * ((hmmc->MmcCard.BlockSize) / 512U);
  1821. hmmc->MmcCard.LogBlockSize = 512U;
  1822. pCSD->EraseGrSize = (uint8_t)((hmmc->CSD[2] & 0x00004000U) >> 14U);
  1823. pCSD->EraseGrMul = (uint8_t)((hmmc->CSD[2] & 0x00003F80U) >> 7U);
  1824. pCSD->WrProtectGrSize = (uint8_t)(hmmc->CSD[2] & 0x0000007FU);
  1825. pCSD->WrProtectGrEnable = (uint8_t)((hmmc->CSD[3] & 0x80000000U) >> 31U);
  1826. pCSD->ManDeflECC = (uint8_t)((hmmc->CSD[3] & 0x60000000U) >> 29U);
  1827. pCSD->WrSpeedFact = (uint8_t)((hmmc->CSD[3] & 0x1C000000U) >> 26U);
  1828. pCSD->MaxWrBlockLen= (uint8_t)((hmmc->CSD[3] & 0x03C00000U) >> 22U);
  1829. pCSD->WriteBlockPaPartial = (uint8_t)((hmmc->CSD[3] & 0x00200000U) >> 21U);
  1830. pCSD->Reserved3 = 0;
  1831. pCSD->ContentProtectAppli = (uint8_t)((hmmc->CSD[3] & 0x00010000U) >> 16U);
  1832. pCSD->FileFormatGroup = (uint8_t)((hmmc->CSD[3] & 0x00008000U) >> 15U);
  1833. pCSD->CopyFlag = (uint8_t)((hmmc->CSD[3] & 0x00004000U) >> 14U);
  1834. pCSD->PermWrProtect = (uint8_t)((hmmc->CSD[3] & 0x00002000U) >> 13U);
  1835. pCSD->TempWrProtect = (uint8_t)((hmmc->CSD[3] & 0x00001000U) >> 12U);
  1836. pCSD->FileFormat = (uint8_t)((hmmc->CSD[3] & 0x00000C00U) >> 10U);
  1837. pCSD->ECC= (uint8_t)((hmmc->CSD[3] & 0x00000300U) >> 8U);
  1838. pCSD->CSD_CRC = (uint8_t)((hmmc->CSD[3] & 0x000000FEU) >> 1U);
  1839. pCSD->Reserved4 = 1;
  1840. return HAL_OK;
  1841. }
  1842. /**
  1843. * @brief Gets the MMC card info.
  1844. * @param hmmc: Pointer to MMC handle
  1845. * @param pCardInfo: Pointer to the HAL_MMC_CardInfoTypeDef structure that
  1846. * will contain the MMC card status information
  1847. * @retval HAL status
  1848. */
  1849. HAL_StatusTypeDef HAL_MMC_GetCardInfo(MMC_HandleTypeDef *hmmc, HAL_MMC_CardInfoTypeDef *pCardInfo)
  1850. {
  1851. pCardInfo->CardType = (uint32_t)(hmmc->MmcCard.CardType);
  1852. pCardInfo->Class = (uint32_t)(hmmc->MmcCard.Class);
  1853. pCardInfo->RelCardAdd = (uint32_t)(hmmc->MmcCard.RelCardAdd);
  1854. pCardInfo->BlockNbr = (uint32_t)(hmmc->MmcCard.BlockNbr);
  1855. pCardInfo->BlockSize = (uint32_t)(hmmc->MmcCard.BlockSize);
  1856. pCardInfo->LogBlockNbr = (uint32_t)(hmmc->MmcCard.LogBlockNbr);
  1857. pCardInfo->LogBlockSize = (uint32_t)(hmmc->MmcCard.LogBlockSize);
  1858. return HAL_OK;
  1859. }
  1860. /**
  1861. * @brief Enables wide bus operation for the requested card if supported by
  1862. * card.
  1863. * @param hmmc: Pointer to MMC handle
  1864. * @param WideMode: Specifies the MMC card wide bus mode
  1865. * This parameter can be one of the following values:
  1866. * @arg SDIO_BUS_WIDE_8B: 8-bit data transfer
  1867. * @arg SDIO_BUS_WIDE_4B: 4-bit data transfer
  1868. * @arg SDIO_BUS_WIDE_1B: 1-bit data transfer
  1869. * @retval HAL status
  1870. */
  1871. HAL_StatusTypeDef HAL_MMC_ConfigWideBusOperation(MMC_HandleTypeDef *hmmc, uint32_t WideMode)
  1872. {
  1873. __IO uint32_t count = 0U;
  1874. SDIO_InitTypeDef Init;
  1875. uint32_t errorstate;
  1876. uint32_t response = 0U, busy = 0U;
  1877. /* Check the parameters */
  1878. assert_param(IS_SDIO_BUS_WIDE(WideMode));
  1879. /* Chnage Satte */
  1880. hmmc->State = HAL_MMC_STATE_BUSY;
  1881. /* Update Clock for Bus mode update */
  1882. Init.ClockEdge = SDIO_CLOCK_EDGE_RISING;
  1883. Init.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE;
  1884. Init.ClockPowerSave = SDIO_CLOCK_POWER_SAVE_DISABLE;
  1885. Init.BusWide = WideMode;
  1886. Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE;
  1887. Init.ClockDiv = SDIO_INIT_CLK_DIV;
  1888. /* Initialize SDIO*/
  1889. (void)SDIO_Init(hmmc->Instance, Init);
  1890. if(WideMode == SDIO_BUS_WIDE_8B)
  1891. {
  1892. errorstate = SDMMC_CmdSwitch(hmmc->Instance, 0x03B70200U);
  1893. if(errorstate != HAL_MMC_ERROR_NONE)
  1894. {
  1895. hmmc->ErrorCode |= errorstate;
  1896. }
  1897. }
  1898. else if(WideMode == SDIO_BUS_WIDE_4B)
  1899. {
  1900. errorstate = SDMMC_CmdSwitch(hmmc->Instance, 0x03B70100U);
  1901. if(errorstate != HAL_MMC_ERROR_NONE)
  1902. {
  1903. hmmc->ErrorCode |= errorstate;
  1904. }
  1905. }
  1906. else if(WideMode == SDIO_BUS_WIDE_1B)
  1907. {
  1908. errorstate = SDMMC_CmdSwitch(hmmc->Instance, 0x03B70000U);
  1909. if(errorstate != HAL_MMC_ERROR_NONE)
  1910. {
  1911. hmmc->ErrorCode |= errorstate;
  1912. }
  1913. }
  1914. else
  1915. {
  1916. /* WideMode is not a valid argument*/
  1917. hmmc->ErrorCode |= HAL_MMC_ERROR_PARAM;
  1918. }
  1919. /* Check for switch error and violation of the trial number of sending CMD 13 */
  1920. while(busy == 0U)
  1921. {
  1922. if(count == SDMMC_MAX_TRIAL)
  1923. {
  1924. hmmc->State = HAL_MMC_STATE_READY;
  1925. hmmc->ErrorCode |= HAL_MMC_ERROR_REQUEST_NOT_APPLICABLE;
  1926. return HAL_ERROR;
  1927. }
  1928. count++;
  1929. /* While card is not ready for data and trial number for sending CMD13 is not exceeded */
  1930. errorstate = SDMMC_CmdSendStatus(hmmc->Instance, (uint32_t)(((uint32_t)hmmc->MmcCard.RelCardAdd) << 16U));
  1931. if(errorstate != HAL_MMC_ERROR_NONE)
  1932. {
  1933. hmmc->ErrorCode |= errorstate;
  1934. }
  1935. /* Get command response */
  1936. response = SDIO_GetResponse(hmmc->Instance, SDIO_RESP1);
  1937. /* Get operating voltage*/
  1938. busy = (((response >> 7U) == 1U) ? 0U : 1U);
  1939. }
  1940. /* While card is not ready for data and trial number for sending CMD13 is not exceeded */
  1941. count = SDMMC_DATATIMEOUT;
  1942. while((response & 0x00000100U) == 0U)
  1943. {
  1944. if(count == 0U)
  1945. {
  1946. hmmc->State = HAL_MMC_STATE_READY;
  1947. hmmc->ErrorCode |= HAL_MMC_ERROR_REQUEST_NOT_APPLICABLE;
  1948. return HAL_ERROR;
  1949. }
  1950. count--;
  1951. /* While card is not ready for data and trial number for sending CMD13 is not exceeded */
  1952. errorstate = SDMMC_CmdSendStatus(hmmc->Instance, (uint32_t)(((uint32_t)hmmc->MmcCard.RelCardAdd) << 16U));
  1953. if(errorstate != HAL_MMC_ERROR_NONE)
  1954. {
  1955. hmmc->ErrorCode |= errorstate;
  1956. }
  1957. /* Get command response */
  1958. response = SDIO_GetResponse(hmmc->Instance, SDIO_RESP1);
  1959. }
  1960. if(hmmc->ErrorCode != HAL_MMC_ERROR_NONE)
  1961. {
  1962. /* Clear all the static flags */
  1963. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
  1964. hmmc->State = HAL_MMC_STATE_READY;
  1965. return HAL_ERROR;
  1966. }
  1967. else
  1968. {
  1969. /* Configure the SDIO peripheral */
  1970. Init.ClockEdge = hmmc->Init.ClockEdge;
  1971. Init.ClockBypass = hmmc->Init.ClockBypass;
  1972. Init.ClockPowerSave = hmmc->Init.ClockPowerSave;
  1973. Init.BusWide = WideMode;
  1974. Init.HardwareFlowControl = hmmc->Init.HardwareFlowControl;
  1975. Init.ClockDiv = hmmc->Init.ClockDiv;
  1976. (void)SDIO_Init(hmmc->Instance, Init);
  1977. }
  1978. /* Change State */
  1979. hmmc->State = HAL_MMC_STATE_READY;
  1980. return HAL_OK;
  1981. }
  1982. /**
  1983. * @brief Gets the current mmc card data state.
  1984. * @param hmmc: pointer to MMC handle
  1985. * @retval Card state
  1986. */
  1987. HAL_MMC_CardStateTypeDef HAL_MMC_GetCardState(MMC_HandleTypeDef *hmmc)
  1988. {
  1989. uint32_t cardstate;
  1990. uint32_t errorstate;
  1991. uint32_t resp1 = 0U;
  1992. errorstate = MMC_SendStatus(hmmc, &resp1);
  1993. if(errorstate != HAL_MMC_ERROR_NONE)
  1994. {
  1995. hmmc->ErrorCode |= errorstate;
  1996. }
  1997. cardstate = ((resp1 >> 9U) & 0x0FU);
  1998. return (HAL_MMC_CardStateTypeDef)cardstate;
  1999. }
  2000. /**
  2001. * @brief Abort the current transfer and disable the MMC.
  2002. * @param hmmc: pointer to a MMC_HandleTypeDef structure that contains
  2003. * the configuration information for MMC module.
  2004. * @retval HAL status
  2005. */
  2006. HAL_StatusTypeDef HAL_MMC_Abort(MMC_HandleTypeDef *hmmc)
  2007. {
  2008. HAL_MMC_CardStateTypeDef CardState;
  2009. /* DIsable All interrupts */
  2010. __HAL_MMC_DISABLE_IT(hmmc, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
  2011. SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR);
  2012. /* Clear All flags */
  2013. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_DATA_FLAGS);
  2014. if((hmmc->hdmatx != NULL) || (hmmc->hdmarx != NULL))
  2015. {
  2016. /* Disable the MMC DMA request */
  2017. hmmc->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN);
  2018. /* Abort the MMC DMA Tx Stream */
  2019. if(hmmc->hdmatx != NULL)
  2020. {
  2021. if(HAL_DMA_Abort(hmmc->hdmatx) != HAL_OK)
  2022. {
  2023. hmmc->ErrorCode |= HAL_MMC_ERROR_DMA;
  2024. }
  2025. }
  2026. /* Abort the MMC DMA Rx Stream */
  2027. if(hmmc->hdmarx != NULL)
  2028. {
  2029. if(HAL_DMA_Abort(hmmc->hdmarx) != HAL_OK)
  2030. {
  2031. hmmc->ErrorCode |= HAL_MMC_ERROR_DMA;
  2032. }
  2033. }
  2034. }
  2035. hmmc->State = HAL_MMC_STATE_READY;
  2036. /* Initialize the MMC operation */
  2037. hmmc->Context = MMC_CONTEXT_NONE;
  2038. CardState = HAL_MMC_GetCardState(hmmc);
  2039. if((CardState == HAL_MMC_CARD_RECEIVING) || (CardState == HAL_MMC_CARD_SENDING))
  2040. {
  2041. hmmc->ErrorCode = SDMMC_CmdStopTransfer(hmmc->Instance);
  2042. }
  2043. if(hmmc->ErrorCode != HAL_MMC_ERROR_NONE)
  2044. {
  2045. return HAL_ERROR;
  2046. }
  2047. return HAL_OK;
  2048. }
  2049. /**
  2050. * @brief Abort the current transfer and disable the MMC (IT mode).
  2051. * @param hmmc: pointer to a MMC_HandleTypeDef structure that contains
  2052. * the configuration information for MMC module.
  2053. * @retval HAL status
  2054. */
  2055. HAL_StatusTypeDef HAL_MMC_Abort_IT(MMC_HandleTypeDef *hmmc)
  2056. {
  2057. HAL_MMC_CardStateTypeDef CardState;
  2058. /* DIsable All interrupts */
  2059. __HAL_MMC_DISABLE_IT(hmmc, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
  2060. SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR);
  2061. /* Clear All flags */
  2062. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_DATA_FLAGS);
  2063. if((hmmc->hdmatx != NULL) || (hmmc->hdmarx != NULL))
  2064. {
  2065. /* Disable the MMC DMA request */
  2066. hmmc->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN);
  2067. /* Abort the MMC DMA Tx Stream */
  2068. if(hmmc->hdmatx != NULL)
  2069. {
  2070. hmmc->hdmatx->XferAbortCallback = MMC_DMATxAbort;
  2071. if(HAL_DMA_Abort_IT(hmmc->hdmatx) != HAL_OK)
  2072. {
  2073. hmmc->hdmatx = NULL;
  2074. }
  2075. }
  2076. /* Abort the MMC DMA Rx Stream */
  2077. if(hmmc->hdmarx != NULL)
  2078. {
  2079. hmmc->hdmarx->XferAbortCallback = MMC_DMARxAbort;
  2080. if(HAL_DMA_Abort_IT(hmmc->hdmarx) != HAL_OK)
  2081. {
  2082. hmmc->hdmarx = NULL;
  2083. }
  2084. }
  2085. }
  2086. /* No transfer ongoing on both DMA channels*/
  2087. if((hmmc->hdmatx == NULL) && (hmmc->hdmarx == NULL))
  2088. {
  2089. CardState = HAL_MMC_GetCardState(hmmc);
  2090. hmmc->State = HAL_MMC_STATE_READY;
  2091. if((CardState == HAL_MMC_CARD_RECEIVING) || (CardState == HAL_MMC_CARD_SENDING))
  2092. {
  2093. hmmc->ErrorCode = SDMMC_CmdStopTransfer(hmmc->Instance);
  2094. }
  2095. if(hmmc->ErrorCode != HAL_MMC_ERROR_NONE)
  2096. {
  2097. return HAL_ERROR;
  2098. }
  2099. else
  2100. {
  2101. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  2102. hmmc->AbortCpltCallback(hmmc);
  2103. #else
  2104. HAL_MMC_AbortCallback(hmmc);
  2105. #endif
  2106. }
  2107. }
  2108. return HAL_OK;
  2109. }
  2110. /**
  2111. * @}
  2112. */
  2113. /**
  2114. * @}
  2115. */
  2116. /* Private function ----------------------------------------------------------*/
  2117. /** @addtogroup MMC_Private_Functions
  2118. * @{
  2119. */
  2120. /**
  2121. * @brief DMA MMC transmit process complete callback
  2122. * @param hdma: DMA handle
  2123. * @retval None
  2124. */
  2125. static void MMC_DMATransmitCplt(DMA_HandleTypeDef *hdma)
  2126. {
  2127. MMC_HandleTypeDef* hmmc = (MMC_HandleTypeDef* )(hdma->Parent);
  2128. /* Enable DATAEND Interrupt */
  2129. __HAL_MMC_ENABLE_IT(hmmc, (SDIO_IT_DATAEND));
  2130. }
  2131. /**
  2132. * @brief DMA MMC receive process complete callback
  2133. * @param hdma: DMA handle
  2134. * @retval None
  2135. */
  2136. static void MMC_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
  2137. {
  2138. MMC_HandleTypeDef* hmmc = (MMC_HandleTypeDef* )(hdma->Parent);
  2139. uint32_t errorstate;
  2140. /* Send stop command in multiblock write */
  2141. if(hmmc->Context == (MMC_CONTEXT_READ_MULTIPLE_BLOCK | MMC_CONTEXT_DMA))
  2142. {
  2143. errorstate = SDMMC_CmdStopTransfer(hmmc->Instance);
  2144. if(errorstate != HAL_MMC_ERROR_NONE)
  2145. {
  2146. hmmc->ErrorCode |= errorstate;
  2147. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  2148. hmmc->ErrorCallback(hmmc);
  2149. #else
  2150. HAL_MMC_ErrorCallback(hmmc);
  2151. #endif
  2152. }
  2153. }
  2154. /* Disable the DMA transfer for transmit request by setting the DMAEN bit
  2155. in the MMC DCTRL register */
  2156. hmmc->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN);
  2157. /* Clear all the static flags */
  2158. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_DATA_FLAGS);
  2159. hmmc->State = HAL_MMC_STATE_READY;
  2160. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  2161. hmmc->RxCpltCallback(hmmc);
  2162. #else
  2163. HAL_MMC_RxCpltCallback(hmmc);
  2164. #endif
  2165. }
  2166. /**
  2167. * @brief DMA MMC communication error callback
  2168. * @param hdma: DMA handle
  2169. * @retval None
  2170. */
  2171. static void MMC_DMAError(DMA_HandleTypeDef *hdma)
  2172. {
  2173. MMC_HandleTypeDef* hmmc = (MMC_HandleTypeDef* )(hdma->Parent);
  2174. HAL_MMC_CardStateTypeDef CardState;
  2175. uint32_t RxErrorCode, TxErrorCode;
  2176. /* if DMA error is FIFO error ignore it */
  2177. if(HAL_DMA_GetError(hdma) != HAL_DMA_ERROR_FE)
  2178. {
  2179. RxErrorCode = hmmc->hdmarx->ErrorCode;
  2180. TxErrorCode = hmmc->hdmatx->ErrorCode;
  2181. if((RxErrorCode == HAL_DMA_ERROR_TE) || (TxErrorCode == HAL_DMA_ERROR_TE))
  2182. {
  2183. /* Clear All flags */
  2184. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
  2185. /* Disable All interrupts */
  2186. __HAL_MMC_DISABLE_IT(hmmc, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
  2187. SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR);
  2188. hmmc->ErrorCode |= HAL_MMC_ERROR_DMA;
  2189. CardState = HAL_MMC_GetCardState(hmmc);
  2190. if((CardState == HAL_MMC_CARD_RECEIVING) || (CardState == HAL_MMC_CARD_SENDING))
  2191. {
  2192. hmmc->ErrorCode |= SDMMC_CmdStopTransfer(hmmc->Instance);
  2193. }
  2194. hmmc->State= HAL_MMC_STATE_READY;
  2195. }
  2196. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  2197. hmmc->ErrorCallback(hmmc);
  2198. #else
  2199. HAL_MMC_ErrorCallback(hmmc);
  2200. #endif
  2201. }
  2202. }
  2203. /**
  2204. * @brief DMA MMC Tx Abort callback
  2205. * @param hdma: DMA handle
  2206. * @retval None
  2207. */
  2208. static void MMC_DMATxAbort(DMA_HandleTypeDef *hdma)
  2209. {
  2210. MMC_HandleTypeDef* hmmc = (MMC_HandleTypeDef* )(hdma->Parent);
  2211. HAL_MMC_CardStateTypeDef CardState;
  2212. if(hmmc->hdmatx != NULL)
  2213. {
  2214. hmmc->hdmatx = NULL;
  2215. }
  2216. /* All DMA channels are aborted */
  2217. if(hmmc->hdmarx == NULL)
  2218. {
  2219. CardState = HAL_MMC_GetCardState(hmmc);
  2220. hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
  2221. hmmc->State = HAL_MMC_STATE_READY;
  2222. if((CardState == HAL_MMC_CARD_RECEIVING) || (CardState == HAL_MMC_CARD_SENDING))
  2223. {
  2224. hmmc->ErrorCode |= SDMMC_CmdStopTransfer(hmmc->Instance);
  2225. if(hmmc->ErrorCode != HAL_MMC_ERROR_NONE)
  2226. {
  2227. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  2228. hmmc->AbortCpltCallback(hmmc);
  2229. #else
  2230. HAL_MMC_AbortCallback(hmmc);
  2231. #endif
  2232. }
  2233. else
  2234. {
  2235. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  2236. hmmc->ErrorCallback(hmmc);
  2237. #else
  2238. HAL_MMC_ErrorCallback(hmmc);
  2239. #endif
  2240. }
  2241. }
  2242. }
  2243. }
  2244. /**
  2245. * @brief DMA MMC Rx Abort callback
  2246. * @param hdma: DMA handle
  2247. * @retval None
  2248. */
  2249. static void MMC_DMARxAbort(DMA_HandleTypeDef *hdma)
  2250. {
  2251. MMC_HandleTypeDef* hmmc = (MMC_HandleTypeDef* )(hdma->Parent);
  2252. HAL_MMC_CardStateTypeDef CardState;
  2253. if(hmmc->hdmarx != NULL)
  2254. {
  2255. hmmc->hdmarx = NULL;
  2256. }
  2257. /* All DMA channels are aborted */
  2258. if(hmmc->hdmatx == NULL)
  2259. {
  2260. CardState = HAL_MMC_GetCardState(hmmc);
  2261. hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
  2262. hmmc->State = HAL_MMC_STATE_READY;
  2263. if((CardState == HAL_MMC_CARD_RECEIVING) || (CardState == HAL_MMC_CARD_SENDING))
  2264. {
  2265. hmmc->ErrorCode |= SDMMC_CmdStopTransfer(hmmc->Instance);
  2266. if(hmmc->ErrorCode != HAL_MMC_ERROR_NONE)
  2267. {
  2268. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  2269. hmmc->AbortCpltCallback(hmmc);
  2270. #else
  2271. HAL_MMC_AbortCallback(hmmc);
  2272. #endif
  2273. }
  2274. else
  2275. {
  2276. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  2277. hmmc->ErrorCallback(hmmc);
  2278. #else
  2279. HAL_MMC_ErrorCallback(hmmc);
  2280. #endif
  2281. }
  2282. }
  2283. }
  2284. }
  2285. /**
  2286. * @brief Initializes the mmc card.
  2287. * @param hmmc: Pointer to MMC handle
  2288. * @retval MMC Card error state
  2289. */
  2290. static uint32_t MMC_InitCard(MMC_HandleTypeDef *hmmc)
  2291. {
  2292. HAL_MMC_CardCSDTypeDef CSD;
  2293. uint32_t errorstate;
  2294. uint16_t mmc_rca = 1U;
  2295. /* Check the power State */
  2296. if(SDIO_GetPowerState(hmmc->Instance) == 0U)
  2297. {
  2298. /* Power off */
  2299. return HAL_MMC_ERROR_REQUEST_NOT_APPLICABLE;
  2300. }
  2301. /* Send CMD2 ALL_SEND_CID */
  2302. errorstate = SDMMC_CmdSendCID(hmmc->Instance);
  2303. if(errorstate != HAL_MMC_ERROR_NONE)
  2304. {
  2305. return errorstate;
  2306. }
  2307. else
  2308. {
  2309. /* Get Card identification number data */
  2310. hmmc->CID[0U] = SDIO_GetResponse(hmmc->Instance, SDIO_RESP1);
  2311. hmmc->CID[1U] = SDIO_GetResponse(hmmc->Instance, SDIO_RESP2);
  2312. hmmc->CID[2U] = SDIO_GetResponse(hmmc->Instance, SDIO_RESP3);
  2313. hmmc->CID[3U] = SDIO_GetResponse(hmmc->Instance, SDIO_RESP4);
  2314. }
  2315. /* Send CMD3 SET_REL_ADDR with argument 0 */
  2316. /* MMC Card publishes its RCA. */
  2317. errorstate = SDMMC_CmdSetRelAdd(hmmc->Instance, &mmc_rca);
  2318. if(errorstate != HAL_MMC_ERROR_NONE)
  2319. {
  2320. return errorstate;
  2321. }
  2322. /* Get the MMC card RCA */
  2323. hmmc->MmcCard.RelCardAdd = mmc_rca;
  2324. /* Send CMD9 SEND_CSD with argument as card's RCA */
  2325. errorstate = SDMMC_CmdSendCSD(hmmc->Instance, (uint32_t)(hmmc->MmcCard.RelCardAdd << 16U));
  2326. if(errorstate != HAL_MMC_ERROR_NONE)
  2327. {
  2328. return errorstate;
  2329. }
  2330. else
  2331. {
  2332. /* Get Card Specific Data */
  2333. hmmc->CSD[0U] = SDIO_GetResponse(hmmc->Instance, SDIO_RESP1);
  2334. hmmc->CSD[1U] = SDIO_GetResponse(hmmc->Instance, SDIO_RESP2);
  2335. hmmc->CSD[2U] = SDIO_GetResponse(hmmc->Instance, SDIO_RESP3);
  2336. hmmc->CSD[3U] = SDIO_GetResponse(hmmc->Instance, SDIO_RESP4);
  2337. }
  2338. /* Get the Card Class */
  2339. hmmc->MmcCard.Class = (SDIO_GetResponse(hmmc->Instance, SDIO_RESP2) >> 20U);
  2340. /* Get CSD parameters */
  2341. if (HAL_MMC_GetCardCSD(hmmc, &CSD) != HAL_OK)
  2342. {
  2343. return hmmc->ErrorCode;
  2344. }
  2345. /* Select the Card */
  2346. errorstate = SDMMC_CmdSelDesel(hmmc->Instance, (uint32_t)(((uint32_t)hmmc->MmcCard.RelCardAdd) << 16U));
  2347. if(errorstate != HAL_MMC_ERROR_NONE)
  2348. {
  2349. return errorstate;
  2350. }
  2351. /* Configure SDIO peripheral interface */
  2352. (void)SDIO_Init(hmmc->Instance, hmmc->Init);
  2353. /* All cards are initialized */
  2354. return HAL_MMC_ERROR_NONE;
  2355. }
  2356. /**
  2357. * @brief Enquires cards about their operating voltage and configures clock
  2358. * controls and stores MMC information that will be needed in future
  2359. * in the MMC handle.
  2360. * @param hmmc: Pointer to MMC handle
  2361. * @retval error state
  2362. */
  2363. static uint32_t MMC_PowerON(MMC_HandleTypeDef *hmmc)
  2364. {
  2365. __IO uint32_t count = 0U;
  2366. uint32_t response = 0U, validvoltage = 0U;
  2367. uint32_t errorstate;
  2368. /* CMD0: GO_IDLE_STATE */
  2369. errorstate = SDMMC_CmdGoIdleState(hmmc->Instance);
  2370. if(errorstate != HAL_MMC_ERROR_NONE)
  2371. {
  2372. return errorstate;
  2373. }
  2374. while(validvoltage == 0U)
  2375. {
  2376. if(count++ == SDMMC_MAX_VOLT_TRIAL)
  2377. {
  2378. return HAL_MMC_ERROR_INVALID_VOLTRANGE;
  2379. }
  2380. /* SEND CMD1 APP_CMD with MMC_HIGH_VOLTAGE_RANGE(0xC0FF8000) as argument */
  2381. errorstate = SDMMC_CmdOpCondition(hmmc->Instance, eMMC_HIGH_VOLTAGE_RANGE);
  2382. if(errorstate != HAL_MMC_ERROR_NONE)
  2383. {
  2384. return HAL_MMC_ERROR_UNSUPPORTED_FEATURE;
  2385. }
  2386. /* Get command response */
  2387. response = SDIO_GetResponse(hmmc->Instance, SDIO_RESP1);
  2388. /* Get operating voltage*/
  2389. validvoltage = (((response >> 31U) == 1U) ? 1U : 0U);
  2390. }
  2391. /* When power routine is finished and command returns valid voltage */
  2392. if (((response & (0xFF000000U)) >> 24U) == 0xC0U)
  2393. {
  2394. hmmc->MmcCard.CardType = MMC_HIGH_CAPACITY_CARD;
  2395. }
  2396. else
  2397. {
  2398. hmmc->MmcCard.CardType = MMC_LOW_CAPACITY_CARD;
  2399. }
  2400. return HAL_MMC_ERROR_NONE;
  2401. }
  2402. /**
  2403. * @brief Turns the SDIO output signals off.
  2404. * @param hmmc: Pointer to MMC handle
  2405. * @retval None
  2406. */
  2407. static void MMC_PowerOFF(MMC_HandleTypeDef *hmmc)
  2408. {
  2409. /* Set Power State to OFF */
  2410. (void)SDIO_PowerState_OFF(hmmc->Instance);
  2411. }
  2412. /**
  2413. * @brief Returns the current card's status.
  2414. * @param hmmc: Pointer to MMC handle
  2415. * @param pCardStatus: pointer to the buffer that will contain the MMC card
  2416. * status (Card Status register)
  2417. * @retval error state
  2418. */
  2419. static uint32_t MMC_SendStatus(MMC_HandleTypeDef *hmmc, uint32_t *pCardStatus)
  2420. {
  2421. uint32_t errorstate;
  2422. if(pCardStatus == NULL)
  2423. {
  2424. return HAL_MMC_ERROR_PARAM;
  2425. }
  2426. /* Send Status command */
  2427. errorstate = SDMMC_CmdSendStatus(hmmc->Instance, (uint32_t)(hmmc->MmcCard.RelCardAdd << 16U));
  2428. if(errorstate != HAL_MMC_ERROR_NONE)
  2429. {
  2430. return errorstate;
  2431. }
  2432. /* Get MMC card status */
  2433. *pCardStatus = SDIO_GetResponse(hmmc->Instance, SDIO_RESP1);
  2434. return HAL_MMC_ERROR_NONE;
  2435. }
  2436. /**
  2437. * @brief Wrap up reading in non-blocking mode.
  2438. * @param hmmc: pointer to a MMC_HandleTypeDef structure that contains
  2439. * the configuration information.
  2440. * @retval None
  2441. */
  2442. static void MMC_Read_IT(MMC_HandleTypeDef *hmmc)
  2443. {
  2444. uint32_t count, data, dataremaining;
  2445. uint8_t* tmp;
  2446. tmp = hmmc->pRxBuffPtr;
  2447. dataremaining = hmmc->RxXferSize;
  2448. if (dataremaining > 0U)
  2449. {
  2450. /* Read data from SDIO Rx FIFO */
  2451. for(count = 0U; count < 8U; count++)
  2452. {
  2453. data = SDIO_ReadFIFO(hmmc->Instance);
  2454. *tmp = (uint8_t)(data & 0xFFU);
  2455. tmp++;
  2456. dataremaining--;
  2457. *tmp = (uint8_t)((data >> 8U) & 0xFFU);
  2458. tmp++;
  2459. dataremaining--;
  2460. *tmp = (uint8_t)((data >> 16U) & 0xFFU);
  2461. tmp++;
  2462. dataremaining--;
  2463. *tmp = (uint8_t)((data >> 24U) & 0xFFU);
  2464. tmp++;
  2465. dataremaining--;
  2466. }
  2467. hmmc->pRxBuffPtr = tmp;
  2468. hmmc->RxXferSize = dataremaining;
  2469. }
  2470. }
  2471. /**
  2472. * @brief Wrap up writing in non-blocking mode.
  2473. * @param hmmc: pointer to a MMC_HandleTypeDef structure that contains
  2474. * the configuration information.
  2475. * @retval None
  2476. */
  2477. static void MMC_Write_IT(MMC_HandleTypeDef *hmmc)
  2478. {
  2479. uint32_t count, data, dataremaining;
  2480. uint8_t* tmp;
  2481. tmp = hmmc->pTxBuffPtr;
  2482. dataremaining = hmmc->TxXferSize;
  2483. if (dataremaining > 0U)
  2484. {
  2485. /* Write data to SDIO Tx FIFO */
  2486. for(count = 0U; count < 8U; count++)
  2487. {
  2488. data = (uint32_t)(*tmp);
  2489. tmp++;
  2490. dataremaining--;
  2491. data |= ((uint32_t)(*tmp) << 8U);
  2492. tmp++;
  2493. dataremaining--;
  2494. data |= ((uint32_t)(*tmp) << 16U);
  2495. tmp++;
  2496. dataremaining--;
  2497. data |= ((uint32_t)(*tmp) << 24U);
  2498. tmp++;
  2499. dataremaining--;
  2500. (void)SDIO_WriteFIFO(hmmc->Instance, &data);
  2501. }
  2502. hmmc->pTxBuffPtr = tmp;
  2503. hmmc->TxXferSize = dataremaining;
  2504. }
  2505. }
  2506. /**
  2507. * @}
  2508. */
  2509. #endif /* SDIO */
  2510. #endif /* HAL_MMC_MODULE_ENABLED */
  2511. /**
  2512. * @}
  2513. */
  2514. /**
  2515. * @}
  2516. */
  2517. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/