stm32f1xx_hal_mmc.c 97 KB

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