1
0

stm32f1xx_hal_mmc.c 81 KB

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