stm32f1xx_hal_mmc.c 81 KB

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