stm32f4xx_hal_mmc.c 92 KB

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