训练营PLSR题目
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

3202 行
99 KiB

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