stm32f4xx_hal_mmc.c 84 KB

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