Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 

3208 строки
100 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(((context & MMC_CONTEXT_WRITE_SINGLE_BLOCK) != 0U) || ((context & MMC_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
  1461. {
  1462. if(hmmc->hdmatx != NULL)
  1463. {
  1464. /* Set the DMA Tx abort callback */
  1465. hmmc->hdmatx->XferAbortCallback = MMC_DMATxAbort;
  1466. /* Abort DMA in IT mode */
  1467. if(HAL_DMA_Abort_IT(hmmc->hdmatx) != HAL_OK)
  1468. {
  1469. MMC_DMATxAbort(hmmc->hdmatx);
  1470. }
  1471. }
  1472. }
  1473. else if(((context & MMC_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & MMC_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
  1474. {
  1475. if(hmmc->hdmarx != NULL)
  1476. {
  1477. /* Set the DMA Rx abort callback */
  1478. hmmc->hdmarx->XferAbortCallback = MMC_DMARxAbort;
  1479. /* Abort DMA in IT mode */
  1480. if(HAL_DMA_Abort_IT(hmmc->hdmarx) != HAL_OK)
  1481. {
  1482. MMC_DMARxAbort(hmmc->hdmarx);
  1483. }
  1484. }
  1485. }
  1486. else
  1487. {
  1488. hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
  1489. hmmc->State = HAL_MMC_STATE_READY;
  1490. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  1491. hmmc->AbortCpltCallback(hmmc);
  1492. #else
  1493. HAL_MMC_AbortCallback(hmmc);
  1494. #endif
  1495. }
  1496. }
  1497. else
  1498. {
  1499. /* Nothing to do */
  1500. }
  1501. }
  1502. else
  1503. {
  1504. /* Nothing to do */
  1505. }
  1506. }
  1507. /**
  1508. * @brief return the MMC state
  1509. * @param hmmc: Pointer to mmc handle
  1510. * @retval HAL state
  1511. */
  1512. HAL_MMC_StateTypeDef HAL_MMC_GetState(MMC_HandleTypeDef *hmmc)
  1513. {
  1514. return hmmc->State;
  1515. }
  1516. /**
  1517. * @brief Return the MMC error code
  1518. * @param hmmc : Pointer to a MMC_HandleTypeDef structure that contains
  1519. * the configuration information.
  1520. * @retval MMC Error Code
  1521. */
  1522. uint32_t HAL_MMC_GetError(MMC_HandleTypeDef *hmmc)
  1523. {
  1524. return hmmc->ErrorCode;
  1525. }
  1526. /**
  1527. * @brief Tx Transfer completed callbacks
  1528. * @param hmmc: Pointer to MMC handle
  1529. * @retval None
  1530. */
  1531. __weak void HAL_MMC_TxCpltCallback(MMC_HandleTypeDef *hmmc)
  1532. {
  1533. /* Prevent unused argument(s) compilation warning */
  1534. UNUSED(hmmc);
  1535. /* NOTE : This function should not be modified, when the callback is needed,
  1536. the HAL_MMC_TxCpltCallback can be implemented in the user file
  1537. */
  1538. }
  1539. /**
  1540. * @brief Rx Transfer completed callbacks
  1541. * @param hmmc: Pointer MMC handle
  1542. * @retval None
  1543. */
  1544. __weak void HAL_MMC_RxCpltCallback(MMC_HandleTypeDef *hmmc)
  1545. {
  1546. /* Prevent unused argument(s) compilation warning */
  1547. UNUSED(hmmc);
  1548. /* NOTE : This function should not be modified, when the callback is needed,
  1549. the HAL_MMC_RxCpltCallback can be implemented in the user file
  1550. */
  1551. }
  1552. /**
  1553. * @brief MMC error callbacks
  1554. * @param hmmc: Pointer MMC handle
  1555. * @retval None
  1556. */
  1557. __weak void HAL_MMC_ErrorCallback(MMC_HandleTypeDef *hmmc)
  1558. {
  1559. /* Prevent unused argument(s) compilation warning */
  1560. UNUSED(hmmc);
  1561. /* NOTE : This function should not be modified, when the callback is needed,
  1562. the HAL_MMC_ErrorCallback can be implemented in the user file
  1563. */
  1564. }
  1565. /**
  1566. * @brief MMC Abort callbacks
  1567. * @param hmmc: Pointer MMC handle
  1568. * @retval None
  1569. */
  1570. __weak void HAL_MMC_AbortCallback(MMC_HandleTypeDef *hmmc)
  1571. {
  1572. /* Prevent unused argument(s) compilation warning */
  1573. UNUSED(hmmc);
  1574. /* NOTE : This function should not be modified, when the callback is needed,
  1575. the HAL_MMC_AbortCallback can be implemented in the user file
  1576. */
  1577. }
  1578. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  1579. /**
  1580. * @brief Register a User MMC Callback
  1581. * To be used instead of the weak (surcharged) predefined callback
  1582. * @param hmmc : MMC handle
  1583. * @param CallbackId : ID of the callback to be registered
  1584. * This parameter can be one of the following values:
  1585. * @arg @ref HAL_MMC_TX_CPLT_CB_ID MMC Tx Complete Callback ID
  1586. * @arg @ref HAL_MMC_RX_CPLT_CB_ID MMC Rx Complete Callback ID
  1587. * @arg @ref HAL_MMC_ERROR_CB_ID MMC Error Callback ID
  1588. * @arg @ref HAL_MMC_ABORT_CB_ID MMC Abort Callback ID
  1589. * @arg @ref HAL_MMC_MSP_INIT_CB_ID MMC MspInit Callback ID
  1590. * @arg @ref HAL_MMC_MSP_DEINIT_CB_ID MMC MspDeInit Callback ID
  1591. * @param pCallback : pointer to the Callback function
  1592. * @retval status
  1593. */
  1594. HAL_StatusTypeDef HAL_MMC_RegisterCallback(MMC_HandleTypeDef *hmmc, HAL_MMC_CallbackIDTypeDef CallbackId, pMMC_CallbackTypeDef pCallback)
  1595. {
  1596. HAL_StatusTypeDef status = HAL_OK;
  1597. if(pCallback == NULL)
  1598. {
  1599. /* Update the error code */
  1600. hmmc->ErrorCode |= HAL_MMC_ERROR_INVALID_CALLBACK;
  1601. return HAL_ERROR;
  1602. }
  1603. /* Process locked */
  1604. __HAL_LOCK(hmmc);
  1605. if(hmmc->State == HAL_MMC_STATE_READY)
  1606. {
  1607. switch (CallbackId)
  1608. {
  1609. case HAL_MMC_TX_CPLT_CB_ID :
  1610. hmmc->TxCpltCallback = pCallback;
  1611. break;
  1612. case HAL_MMC_RX_CPLT_CB_ID :
  1613. hmmc->RxCpltCallback = pCallback;
  1614. break;
  1615. case HAL_MMC_ERROR_CB_ID :
  1616. hmmc->ErrorCallback = pCallback;
  1617. break;
  1618. case HAL_MMC_ABORT_CB_ID :
  1619. hmmc->AbortCpltCallback = pCallback;
  1620. break;
  1621. case HAL_MMC_MSP_INIT_CB_ID :
  1622. hmmc->MspInitCallback = pCallback;
  1623. break;
  1624. case HAL_MMC_MSP_DEINIT_CB_ID :
  1625. hmmc->MspDeInitCallback = pCallback;
  1626. break;
  1627. default :
  1628. /* Update the error code */
  1629. hmmc->ErrorCode |= HAL_MMC_ERROR_INVALID_CALLBACK;
  1630. /* update return status */
  1631. status = HAL_ERROR;
  1632. break;
  1633. }
  1634. }
  1635. else if (hmmc->State == HAL_MMC_STATE_RESET)
  1636. {
  1637. switch (CallbackId)
  1638. {
  1639. case HAL_MMC_MSP_INIT_CB_ID :
  1640. hmmc->MspInitCallback = pCallback;
  1641. break;
  1642. case HAL_MMC_MSP_DEINIT_CB_ID :
  1643. hmmc->MspDeInitCallback = pCallback;
  1644. break;
  1645. default :
  1646. /* Update the error code */
  1647. hmmc->ErrorCode |= HAL_MMC_ERROR_INVALID_CALLBACK;
  1648. /* update return status */
  1649. status = HAL_ERROR;
  1650. break;
  1651. }
  1652. }
  1653. else
  1654. {
  1655. /* Update the error code */
  1656. hmmc->ErrorCode |= HAL_MMC_ERROR_INVALID_CALLBACK;
  1657. /* update return status */
  1658. status = HAL_ERROR;
  1659. }
  1660. /* Release Lock */
  1661. __HAL_UNLOCK(hmmc);
  1662. return status;
  1663. }
  1664. /**
  1665. * @brief Unregister a User MMC Callback
  1666. * MMC Callback is redirected to the weak (surcharged) predefined callback
  1667. * @param hmmc : MMC handle
  1668. * @param CallbackId : ID of the callback to be unregistered
  1669. * This parameter can be one of the following values:
  1670. * @arg @ref HAL_MMC_TX_CPLT_CB_ID MMC Tx Complete Callback ID
  1671. * @arg @ref HAL_MMC_RX_CPLT_CB_ID MMC Rx Complete Callback ID
  1672. * @arg @ref HAL_MMC_ERROR_CB_ID MMC Error Callback ID
  1673. * @arg @ref HAL_MMC_ABORT_CB_ID MMC Abort Callback ID
  1674. * @arg @ref HAL_MMC_MSP_INIT_CB_ID MMC MspInit Callback ID
  1675. * @arg @ref HAL_MMC_MSP_DEINIT_CB_ID MMC MspDeInit Callback ID
  1676. * @retval status
  1677. */
  1678. HAL_StatusTypeDef HAL_MMC_UnRegisterCallback(MMC_HandleTypeDef *hmmc, HAL_MMC_CallbackIDTypeDef CallbackId)
  1679. {
  1680. HAL_StatusTypeDef status = HAL_OK;
  1681. /* Process locked */
  1682. __HAL_LOCK(hmmc);
  1683. if(hmmc->State == HAL_MMC_STATE_READY)
  1684. {
  1685. switch (CallbackId)
  1686. {
  1687. case HAL_MMC_TX_CPLT_CB_ID :
  1688. hmmc->TxCpltCallback = HAL_MMC_TxCpltCallback;
  1689. break;
  1690. case HAL_MMC_RX_CPLT_CB_ID :
  1691. hmmc->RxCpltCallback = HAL_MMC_RxCpltCallback;
  1692. break;
  1693. case HAL_MMC_ERROR_CB_ID :
  1694. hmmc->ErrorCallback = HAL_MMC_ErrorCallback;
  1695. break;
  1696. case HAL_MMC_ABORT_CB_ID :
  1697. hmmc->AbortCpltCallback = HAL_MMC_AbortCallback;
  1698. break;
  1699. case HAL_MMC_MSP_INIT_CB_ID :
  1700. hmmc->MspInitCallback = HAL_MMC_MspInit;
  1701. break;
  1702. case HAL_MMC_MSP_DEINIT_CB_ID :
  1703. hmmc->MspDeInitCallback = HAL_MMC_MspDeInit;
  1704. break;
  1705. default :
  1706. /* Update the error code */
  1707. hmmc->ErrorCode |= HAL_MMC_ERROR_INVALID_CALLBACK;
  1708. /* update return status */
  1709. status = HAL_ERROR;
  1710. break;
  1711. }
  1712. }
  1713. else if (hmmc->State == HAL_MMC_STATE_RESET)
  1714. {
  1715. switch (CallbackId)
  1716. {
  1717. case HAL_MMC_MSP_INIT_CB_ID :
  1718. hmmc->MspInitCallback = HAL_MMC_MspInit;
  1719. break;
  1720. case HAL_MMC_MSP_DEINIT_CB_ID :
  1721. hmmc->MspDeInitCallback = HAL_MMC_MspDeInit;
  1722. break;
  1723. default :
  1724. /* Update the error code */
  1725. hmmc->ErrorCode |= HAL_MMC_ERROR_INVALID_CALLBACK;
  1726. /* update return status */
  1727. status = HAL_ERROR;
  1728. break;
  1729. }
  1730. }
  1731. else
  1732. {
  1733. /* Update the error code */
  1734. hmmc->ErrorCode |= HAL_MMC_ERROR_INVALID_CALLBACK;
  1735. /* update return status */
  1736. status = HAL_ERROR;
  1737. }
  1738. /* Release Lock */
  1739. __HAL_UNLOCK(hmmc);
  1740. return status;
  1741. }
  1742. #endif
  1743. /**
  1744. * @}
  1745. */
  1746. /** @addtogroup MMC_Exported_Functions_Group3
  1747. * @brief management functions
  1748. *
  1749. @verbatim
  1750. ==============================================================================
  1751. ##### Peripheral Control functions #####
  1752. ==============================================================================
  1753. [..]
  1754. This subsection provides a set of functions allowing to control the MMC card
  1755. operations and get the related information
  1756. @endverbatim
  1757. * @{
  1758. */
  1759. /**
  1760. * @brief Returns information the information of the card which are stored on
  1761. * the CID register.
  1762. * @param hmmc: Pointer to MMC handle
  1763. * @param pCID: Pointer to a HAL_MMC_CIDTypedef structure that
  1764. * contains all CID register parameters
  1765. * @retval HAL status
  1766. */
  1767. HAL_StatusTypeDef HAL_MMC_GetCardCID(MMC_HandleTypeDef *hmmc, HAL_MMC_CardCIDTypeDef *pCID)
  1768. {
  1769. pCID->ManufacturerID = (uint8_t)((hmmc->CID[0] & 0xFF000000U) >> 24U);
  1770. pCID->OEM_AppliID = (uint16_t)((hmmc->CID[0] & 0x00FFFF00U) >> 8U);
  1771. pCID->ProdName1 = (((hmmc->CID[0] & 0x000000FFU) << 24U) | ((hmmc->CID[1] & 0xFFFFFF00U) >> 8U));
  1772. pCID->ProdName2 = (uint8_t)(hmmc->CID[1] & 0x000000FFU);
  1773. pCID->ProdRev = (uint8_t)((hmmc->CID[2] & 0xFF000000U) >> 24U);
  1774. pCID->ProdSN = (((hmmc->CID[2] & 0x00FFFFFFU) << 8U) | ((hmmc->CID[3] & 0xFF000000U) >> 24U));
  1775. pCID->Reserved1 = (uint8_t)((hmmc->CID[3] & 0x00F00000U) >> 20U);
  1776. pCID->ManufactDate = (uint16_t)((hmmc->CID[3] & 0x000FFF00U) >> 8U);
  1777. pCID->CID_CRC = (uint8_t)((hmmc->CID[3] & 0x000000FEU) >> 1U);
  1778. pCID->Reserved2 = 1U;
  1779. return HAL_OK;
  1780. }
  1781. /**
  1782. * @brief Returns information the information of the card which are stored on
  1783. * the CSD register.
  1784. * @param hmmc: Pointer to MMC handle
  1785. * @param pCSD: Pointer to a HAL_MMC_CardCSDTypeDef structure that
  1786. * contains all CSD register parameters
  1787. * @retval HAL status
  1788. */
  1789. HAL_StatusTypeDef HAL_MMC_GetCardCSD(MMC_HandleTypeDef *hmmc, HAL_MMC_CardCSDTypeDef *pCSD)
  1790. {
  1791. uint32_t block_nbr = 0;
  1792. pCSD->CSDStruct = (uint8_t)((hmmc->CSD[0] & 0xC0000000U) >> 30U);
  1793. pCSD->SysSpecVersion = (uint8_t)((hmmc->CSD[0] & 0x3C000000U) >> 26U);
  1794. pCSD->Reserved1 = (uint8_t)((hmmc->CSD[0] & 0x03000000U) >> 24U);
  1795. pCSD->TAAC = (uint8_t)((hmmc->CSD[0] & 0x00FF0000U) >> 16U);
  1796. pCSD->NSAC = (uint8_t)((hmmc->CSD[0] & 0x0000FF00U) >> 8U);
  1797. pCSD->MaxBusClkFrec = (uint8_t)(hmmc->CSD[0] & 0x000000FFU);
  1798. pCSD->CardComdClasses = (uint16_t)((hmmc->CSD[1] & 0xFFF00000U) >> 20U);
  1799. pCSD->RdBlockLen = (uint8_t)((hmmc->CSD[1] & 0x000F0000U) >> 16U);
  1800. pCSD->PartBlockRead = (uint8_t)((hmmc->CSD[1] & 0x00008000U) >> 15U);
  1801. pCSD->WrBlockMisalign = (uint8_t)((hmmc->CSD[1] & 0x00004000U) >> 14U);
  1802. pCSD->RdBlockMisalign = (uint8_t)((hmmc->CSD[1] & 0x00002000U) >> 13U);
  1803. pCSD->DSRImpl = (uint8_t)((hmmc->CSD[1] & 0x00001000U) >> 12U);
  1804. pCSD->Reserved2 = 0U; /*!< Reserved */
  1805. pCSD->DeviceSize = (((hmmc->CSD[1] & 0x000003FFU) << 2U) | ((hmmc->CSD[2] & 0xC0000000U) >> 30U));
  1806. pCSD->MaxRdCurrentVDDMin = (uint8_t)((hmmc->CSD[2] & 0x38000000U) >> 27U);
  1807. pCSD->MaxRdCurrentVDDMax = (uint8_t)((hmmc->CSD[2] & 0x07000000U) >> 24U);
  1808. pCSD->MaxWrCurrentVDDMin = (uint8_t)((hmmc->CSD[2] & 0x00E00000U) >> 21U);
  1809. pCSD->MaxWrCurrentVDDMax = (uint8_t)((hmmc->CSD[2] & 0x001C0000U) >> 18U);
  1810. pCSD->DeviceSizeMul = (uint8_t)((hmmc->CSD[2] & 0x00038000U) >> 15U);
  1811. if(MMC_ReadExtCSD(hmmc, &block_nbr, 212, 0x0FFFFFFFU) != HAL_OK) /* Field SEC_COUNT [215:212] */
  1812. {
  1813. return HAL_ERROR;
  1814. }
  1815. if(hmmc->MmcCard.CardType == MMC_LOW_CAPACITY_CARD)
  1816. {
  1817. hmmc->MmcCard.BlockNbr = (pCSD->DeviceSize + 1U) ;
  1818. hmmc->MmcCard.BlockNbr *= (1UL << ((pCSD->DeviceSizeMul & 0x07U) + 2U));
  1819. hmmc->MmcCard.BlockSize = (1UL << (pCSD->RdBlockLen & 0x0FU));
  1820. hmmc->MmcCard.LogBlockNbr = (hmmc->MmcCard.BlockNbr) * ((hmmc->MmcCard.BlockSize) / 512U);
  1821. hmmc->MmcCard.LogBlockSize = 512U;
  1822. }
  1823. else if(hmmc->MmcCard.CardType == MMC_HIGH_CAPACITY_CARD)
  1824. {
  1825. hmmc->MmcCard.BlockNbr = block_nbr;
  1826. hmmc->MmcCard.LogBlockNbr = hmmc->MmcCard.BlockNbr;
  1827. hmmc->MmcCard.BlockSize = 512U;
  1828. hmmc->MmcCard.LogBlockSize = hmmc->MmcCard.BlockSize;
  1829. }
  1830. else
  1831. {
  1832. /* Clear all the static flags */
  1833. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
  1834. hmmc->ErrorCode |= HAL_MMC_ERROR_UNSUPPORTED_FEATURE;
  1835. hmmc->State = HAL_MMC_STATE_READY;
  1836. return HAL_ERROR;
  1837. }
  1838. pCSD->EraseGrSize = (uint8_t)((hmmc->CSD[2] & 0x00004000U) >> 14U);
  1839. pCSD->EraseGrMul = (uint8_t)((hmmc->CSD[2] & 0x00003F80U) >> 7U);
  1840. pCSD->WrProtectGrSize = (uint8_t)(hmmc->CSD[2] & 0x0000007FU);
  1841. pCSD->WrProtectGrEnable = (uint8_t)((hmmc->CSD[3] & 0x80000000U) >> 31U);
  1842. pCSD->ManDeflECC = (uint8_t)((hmmc->CSD[3] & 0x60000000U) >> 29U);
  1843. pCSD->WrSpeedFact = (uint8_t)((hmmc->CSD[3] & 0x1C000000U) >> 26U);
  1844. pCSD->MaxWrBlockLen= (uint8_t)((hmmc->CSD[3] & 0x03C00000U) >> 22U);
  1845. pCSD->WriteBlockPaPartial = (uint8_t)((hmmc->CSD[3] & 0x00200000U) >> 21U);
  1846. pCSD->Reserved3 = 0;
  1847. pCSD->ContentProtectAppli = (uint8_t)((hmmc->CSD[3] & 0x00010000U) >> 16U);
  1848. pCSD->FileFormatGroup = (uint8_t)((hmmc->CSD[3] & 0x00008000U) >> 15U);
  1849. pCSD->CopyFlag = (uint8_t)((hmmc->CSD[3] & 0x00004000U) >> 14U);
  1850. pCSD->PermWrProtect = (uint8_t)((hmmc->CSD[3] & 0x00002000U) >> 13U);
  1851. pCSD->TempWrProtect = (uint8_t)((hmmc->CSD[3] & 0x00001000U) >> 12U);
  1852. pCSD->FileFormat = (uint8_t)((hmmc->CSD[3] & 0x00000C00U) >> 10U);
  1853. pCSD->ECC= (uint8_t)((hmmc->CSD[3] & 0x00000300U) >> 8U);
  1854. pCSD->CSD_CRC = (uint8_t)((hmmc->CSD[3] & 0x000000FEU) >> 1U);
  1855. pCSD->Reserved4 = 1;
  1856. return HAL_OK;
  1857. }
  1858. /**
  1859. * @brief Gets the MMC card info.
  1860. * @param hmmc: Pointer to MMC handle
  1861. * @param pCardInfo: Pointer to the HAL_MMC_CardInfoTypeDef structure that
  1862. * will contain the MMC card status information
  1863. * @retval HAL status
  1864. */
  1865. HAL_StatusTypeDef HAL_MMC_GetCardInfo(MMC_HandleTypeDef *hmmc, HAL_MMC_CardInfoTypeDef *pCardInfo)
  1866. {
  1867. pCardInfo->CardType = (uint32_t)(hmmc->MmcCard.CardType);
  1868. pCardInfo->Class = (uint32_t)(hmmc->MmcCard.Class);
  1869. pCardInfo->RelCardAdd = (uint32_t)(hmmc->MmcCard.RelCardAdd);
  1870. pCardInfo->BlockNbr = (uint32_t)(hmmc->MmcCard.BlockNbr);
  1871. pCardInfo->BlockSize = (uint32_t)(hmmc->MmcCard.BlockSize);
  1872. pCardInfo->LogBlockNbr = (uint32_t)(hmmc->MmcCard.LogBlockNbr);
  1873. pCardInfo->LogBlockSize = (uint32_t)(hmmc->MmcCard.LogBlockSize);
  1874. return HAL_OK;
  1875. }
  1876. /**
  1877. * @brief Returns information the information of the card which are stored on
  1878. * the Extended CSD register.
  1879. * @param hmmc Pointer to MMC handle
  1880. * @param pExtCSD Pointer to a memory area (512 bytes) that contains all
  1881. * Extended CSD register parameters
  1882. * @param Timeout Specify timeout value
  1883. * @retval HAL status
  1884. */
  1885. HAL_StatusTypeDef HAL_MMC_GetCardExtCSD(MMC_HandleTypeDef *hmmc, uint32_t *pExtCSD, uint32_t Timeout)
  1886. {
  1887. SDIO_DataInitTypeDef config;
  1888. uint32_t errorstate;
  1889. uint32_t tickstart = HAL_GetTick();
  1890. uint32_t count;
  1891. uint32_t *tmp_buf;
  1892. if(NULL == pExtCSD)
  1893. {
  1894. hmmc->ErrorCode |= HAL_MMC_ERROR_PARAM;
  1895. return HAL_ERROR;
  1896. }
  1897. if(hmmc->State == HAL_MMC_STATE_READY)
  1898. {
  1899. hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
  1900. hmmc->State = HAL_MMC_STATE_BUSY;
  1901. /* Initialize data control register */
  1902. hmmc->Instance->DCTRL = 0;
  1903. /* Initiaize the destination pointer */
  1904. tmp_buf = pExtCSD;
  1905. /* Configure the MMC DPSM (Data Path State Machine) */
  1906. config.DataTimeOut = SDMMC_DATATIMEOUT;
  1907. config.DataLength = 512;
  1908. config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
  1909. config.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
  1910. config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
  1911. config.DPSM = SDIO_DPSM_ENABLE;
  1912. (void)SDIO_ConfigData(hmmc->Instance, &config);
  1913. /* Send ExtCSD Read command to Card */
  1914. errorstate = SDMMC_CmdSendEXTCSD(hmmc->Instance, 0);
  1915. if(errorstate != HAL_MMC_ERROR_NONE)
  1916. {
  1917. /* Clear all the static flags */
  1918. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
  1919. hmmc->ErrorCode |= errorstate;
  1920. hmmc->State = HAL_MMC_STATE_READY;
  1921. return HAL_ERROR;
  1922. }
  1923. /* Poll on SDMMC flags */
  1924. while(!__HAL_MMC_GET_FLAG(hmmc, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND))
  1925. {
  1926. if(__HAL_MMC_GET_FLAG(hmmc, SDIO_FLAG_RXFIFOHF))
  1927. {
  1928. /* Read data from SDMMC Rx FIFO */
  1929. for(count = 0U; count < 8U; count++)
  1930. {
  1931. *tmp_buf = SDIO_ReadFIFO(hmmc->Instance);
  1932. tmp_buf++;
  1933. }
  1934. }
  1935. if(((HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U))
  1936. {
  1937. /* Clear all the static flags */
  1938. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
  1939. hmmc->ErrorCode |= HAL_MMC_ERROR_TIMEOUT;
  1940. hmmc->State= HAL_MMC_STATE_READY;
  1941. return HAL_TIMEOUT;
  1942. }
  1943. }
  1944. /* Get error state */
  1945. if(__HAL_MMC_GET_FLAG(hmmc, SDIO_FLAG_DTIMEOUT))
  1946. {
  1947. /* Clear all the static flags */
  1948. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
  1949. hmmc->ErrorCode |= HAL_MMC_ERROR_DATA_TIMEOUT;
  1950. hmmc->State = HAL_MMC_STATE_READY;
  1951. return HAL_ERROR;
  1952. }
  1953. else if(__HAL_MMC_GET_FLAG(hmmc, SDIO_FLAG_DCRCFAIL))
  1954. {
  1955. /* Clear all the static flags */
  1956. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
  1957. hmmc->ErrorCode |= HAL_MMC_ERROR_DATA_CRC_FAIL;
  1958. hmmc->State = HAL_MMC_STATE_READY;
  1959. return HAL_ERROR;
  1960. }
  1961. else if(__HAL_MMC_GET_FLAG(hmmc, SDIO_FLAG_RXOVERR))
  1962. {
  1963. /* Clear all the static flags */
  1964. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
  1965. hmmc->ErrorCode |= HAL_MMC_ERROR_RX_OVERRUN;
  1966. hmmc->State = HAL_MMC_STATE_READY;
  1967. return HAL_ERROR;
  1968. }
  1969. else
  1970. {
  1971. /* Nothing to do */
  1972. }
  1973. /* Clear all the static flags */
  1974. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_DATA_FLAGS);
  1975. hmmc->State = HAL_MMC_STATE_READY;
  1976. }
  1977. return HAL_OK;
  1978. }
  1979. /**
  1980. * @brief Enables wide bus operation for the requested card if supported by
  1981. * card.
  1982. * @param hmmc: Pointer to MMC handle
  1983. * @param WideMode: Specifies the MMC card wide bus mode
  1984. * This parameter can be one of the following values:
  1985. * @arg SDIO_BUS_WIDE_8B: 8-bit data transfer
  1986. * @arg SDIO_BUS_WIDE_4B: 4-bit data transfer
  1987. * @arg SDIO_BUS_WIDE_1B: 1-bit data transfer
  1988. * @retval HAL status
  1989. */
  1990. HAL_StatusTypeDef HAL_MMC_ConfigWideBusOperation(MMC_HandleTypeDef *hmmc, uint32_t WideMode)
  1991. {
  1992. uint32_t count;
  1993. SDIO_InitTypeDef Init;
  1994. uint32_t errorstate;
  1995. uint32_t response = 0U;
  1996. /* Check the parameters */
  1997. assert_param(IS_SDIO_BUS_WIDE(WideMode));
  1998. /* Change State */
  1999. hmmc->State = HAL_MMC_STATE_BUSY;
  2000. errorstate = MMC_PwrClassUpdate(hmmc, WideMode);
  2001. if(errorstate == HAL_MMC_ERROR_NONE)
  2002. {
  2003. if(WideMode == SDIO_BUS_WIDE_8B)
  2004. {
  2005. errorstate = SDMMC_CmdSwitch(hmmc->Instance, 0x03B70200U);
  2006. }
  2007. else if(WideMode == SDIO_BUS_WIDE_4B)
  2008. {
  2009. errorstate = SDMMC_CmdSwitch(hmmc->Instance, 0x03B70100U);
  2010. }
  2011. else if(WideMode == SDIO_BUS_WIDE_1B)
  2012. {
  2013. errorstate = SDMMC_CmdSwitch(hmmc->Instance, 0x03B70000U);
  2014. }
  2015. else
  2016. {
  2017. /* WideMode is not a valid argument*/
  2018. errorstate = HAL_MMC_ERROR_PARAM;
  2019. }
  2020. /* Check for switch error and violation of the trial number of sending CMD 13 */
  2021. if(errorstate == HAL_MMC_ERROR_NONE)
  2022. {
  2023. /* While card is not ready for data and trial number for sending CMD13 is not exceeded */
  2024. count = SDMMC_MAX_TRIAL;
  2025. do
  2026. {
  2027. errorstate = SDMMC_CmdSendStatus(hmmc->Instance, (uint32_t)(((uint32_t)hmmc->MmcCard.RelCardAdd) << 16U));
  2028. if(errorstate != HAL_MMC_ERROR_NONE)
  2029. {
  2030. break;
  2031. }
  2032. /* Get command response */
  2033. response = SDIO_GetResponse(hmmc->Instance, SDIO_RESP1);
  2034. count--;
  2035. }while(((response & 0x100U) == 0U) && (count != 0U));
  2036. /* Check the status after the switch command execution */
  2037. if ((count != 0U) && (errorstate == HAL_MMC_ERROR_NONE))
  2038. {
  2039. /* Check the bit SWITCH_ERROR of the device status */
  2040. if ((response & 0x80U) != 0U)
  2041. {
  2042. errorstate = SDMMC_ERROR_GENERAL_UNKNOWN_ERR;
  2043. }
  2044. else
  2045. {
  2046. /* Configure the SDIO peripheral */
  2047. Init = hmmc->Init;
  2048. Init.BusWide = WideMode;
  2049. (void)SDIO_Init(hmmc->Instance, Init);
  2050. }
  2051. }
  2052. else if (count == 0U)
  2053. {
  2054. errorstate = SDMMC_ERROR_TIMEOUT;
  2055. }
  2056. else
  2057. {
  2058. /* Nothing to do */
  2059. }
  2060. }
  2061. }
  2062. /* Change State */
  2063. hmmc->State = HAL_MMC_STATE_READY;
  2064. if(errorstate != HAL_MMC_ERROR_NONE)
  2065. {
  2066. /* Clear all the static flags */
  2067. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
  2068. hmmc->ErrorCode |= errorstate;
  2069. return HAL_ERROR;
  2070. }
  2071. return HAL_OK;
  2072. }
  2073. /**
  2074. * @brief Gets the current mmc card data state.
  2075. * @param hmmc: pointer to MMC handle
  2076. * @retval Card state
  2077. */
  2078. HAL_MMC_CardStateTypeDef HAL_MMC_GetCardState(MMC_HandleTypeDef *hmmc)
  2079. {
  2080. uint32_t cardstate;
  2081. uint32_t errorstate;
  2082. uint32_t resp1 = 0U;
  2083. errorstate = MMC_SendStatus(hmmc, &resp1);
  2084. if(errorstate != HAL_MMC_ERROR_NONE)
  2085. {
  2086. hmmc->ErrorCode |= errorstate;
  2087. }
  2088. cardstate = ((resp1 >> 9U) & 0x0FU);
  2089. return (HAL_MMC_CardStateTypeDef)cardstate;
  2090. }
  2091. /**
  2092. * @brief Abort the current transfer and disable the MMC.
  2093. * @param hmmc: pointer to a MMC_HandleTypeDef structure that contains
  2094. * the configuration information for MMC module.
  2095. * @retval HAL status
  2096. */
  2097. HAL_StatusTypeDef HAL_MMC_Abort(MMC_HandleTypeDef *hmmc)
  2098. {
  2099. HAL_MMC_CardStateTypeDef CardState;
  2100. /* DIsable All interrupts */
  2101. __HAL_MMC_DISABLE_IT(hmmc, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
  2102. SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR);
  2103. /* Clear All flags */
  2104. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_DATA_FLAGS);
  2105. if((hmmc->hdmatx != NULL) || (hmmc->hdmarx != NULL))
  2106. {
  2107. /* Disable the MMC DMA request */
  2108. hmmc->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN);
  2109. /* Abort the MMC DMA Tx Stream */
  2110. if(hmmc->hdmatx != NULL)
  2111. {
  2112. if(HAL_DMA_Abort(hmmc->hdmatx) != HAL_OK)
  2113. {
  2114. hmmc->ErrorCode |= HAL_MMC_ERROR_DMA;
  2115. }
  2116. }
  2117. /* Abort the MMC DMA Rx Stream */
  2118. if(hmmc->hdmarx != NULL)
  2119. {
  2120. if(HAL_DMA_Abort(hmmc->hdmarx) != HAL_OK)
  2121. {
  2122. hmmc->ErrorCode |= HAL_MMC_ERROR_DMA;
  2123. }
  2124. }
  2125. }
  2126. hmmc->State = HAL_MMC_STATE_READY;
  2127. /* Initialize the MMC operation */
  2128. hmmc->Context = MMC_CONTEXT_NONE;
  2129. CardState = HAL_MMC_GetCardState(hmmc);
  2130. if((CardState == HAL_MMC_CARD_RECEIVING) || (CardState == HAL_MMC_CARD_SENDING))
  2131. {
  2132. hmmc->ErrorCode = SDMMC_CmdStopTransfer(hmmc->Instance);
  2133. }
  2134. if(hmmc->ErrorCode != HAL_MMC_ERROR_NONE)
  2135. {
  2136. return HAL_ERROR;
  2137. }
  2138. return HAL_OK;
  2139. }
  2140. /**
  2141. * @brief Abort the current transfer and disable the MMC (IT mode).
  2142. * @param hmmc: pointer to a MMC_HandleTypeDef structure that contains
  2143. * the configuration information for MMC module.
  2144. * @retval HAL status
  2145. */
  2146. HAL_StatusTypeDef HAL_MMC_Abort_IT(MMC_HandleTypeDef *hmmc)
  2147. {
  2148. HAL_MMC_CardStateTypeDef CardState;
  2149. /* DIsable All interrupts */
  2150. __HAL_MMC_DISABLE_IT(hmmc, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
  2151. SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR);
  2152. /* Clear All flags */
  2153. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_DATA_FLAGS);
  2154. if((hmmc->hdmatx != NULL) || (hmmc->hdmarx != NULL))
  2155. {
  2156. /* Disable the MMC DMA request */
  2157. hmmc->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN);
  2158. /* Abort the MMC DMA Tx Stream */
  2159. if(hmmc->hdmatx != NULL)
  2160. {
  2161. hmmc->hdmatx->XferAbortCallback = MMC_DMATxAbort;
  2162. if(HAL_DMA_Abort_IT(hmmc->hdmatx) != HAL_OK)
  2163. {
  2164. hmmc->hdmatx = NULL;
  2165. }
  2166. }
  2167. /* Abort the MMC DMA Rx Stream */
  2168. if(hmmc->hdmarx != NULL)
  2169. {
  2170. hmmc->hdmarx->XferAbortCallback = MMC_DMARxAbort;
  2171. if(HAL_DMA_Abort_IT(hmmc->hdmarx) != HAL_OK)
  2172. {
  2173. hmmc->hdmarx = NULL;
  2174. }
  2175. }
  2176. }
  2177. /* No transfer ongoing on both DMA channels*/
  2178. if((hmmc->hdmatx == NULL) && (hmmc->hdmarx == NULL))
  2179. {
  2180. CardState = HAL_MMC_GetCardState(hmmc);
  2181. hmmc->State = HAL_MMC_STATE_READY;
  2182. if((CardState == HAL_MMC_CARD_RECEIVING) || (CardState == HAL_MMC_CARD_SENDING))
  2183. {
  2184. hmmc->ErrorCode = SDMMC_CmdStopTransfer(hmmc->Instance);
  2185. }
  2186. if(hmmc->ErrorCode != HAL_MMC_ERROR_NONE)
  2187. {
  2188. return HAL_ERROR;
  2189. }
  2190. else
  2191. {
  2192. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  2193. hmmc->AbortCpltCallback(hmmc);
  2194. #else
  2195. HAL_MMC_AbortCallback(hmmc);
  2196. #endif
  2197. }
  2198. }
  2199. return HAL_OK;
  2200. }
  2201. /**
  2202. * @}
  2203. */
  2204. /**
  2205. * @}
  2206. */
  2207. /* Private function ----------------------------------------------------------*/
  2208. /** @addtogroup MMC_Private_Functions
  2209. * @{
  2210. */
  2211. /**
  2212. * @brief DMA MMC transmit process complete callback
  2213. * @param hdma: DMA handle
  2214. * @retval None
  2215. */
  2216. static void MMC_DMATransmitCplt(DMA_HandleTypeDef *hdma)
  2217. {
  2218. MMC_HandleTypeDef* hmmc = (MMC_HandleTypeDef* )(hdma->Parent);
  2219. /* Enable DATAEND Interrupt */
  2220. __HAL_MMC_ENABLE_IT(hmmc, (SDIO_IT_DATAEND));
  2221. }
  2222. /**
  2223. * @brief DMA MMC receive process complete callback
  2224. * @param hdma: DMA handle
  2225. * @retval None
  2226. */
  2227. static void MMC_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
  2228. {
  2229. MMC_HandleTypeDef* hmmc = (MMC_HandleTypeDef* )(hdma->Parent);
  2230. uint32_t errorstate;
  2231. /* Send stop command in multiblock write */
  2232. if(hmmc->Context == (MMC_CONTEXT_READ_MULTIPLE_BLOCK | MMC_CONTEXT_DMA))
  2233. {
  2234. errorstate = SDMMC_CmdStopTransfer(hmmc->Instance);
  2235. if(errorstate != HAL_MMC_ERROR_NONE)
  2236. {
  2237. hmmc->ErrorCode |= errorstate;
  2238. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  2239. hmmc->ErrorCallback(hmmc);
  2240. #else
  2241. HAL_MMC_ErrorCallback(hmmc);
  2242. #endif
  2243. }
  2244. }
  2245. /* Disable the DMA transfer for transmit request by setting the DMAEN bit
  2246. in the MMC DCTRL register */
  2247. hmmc->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN);
  2248. /* Clear all the static flags */
  2249. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_DATA_FLAGS);
  2250. hmmc->State = HAL_MMC_STATE_READY;
  2251. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  2252. hmmc->RxCpltCallback(hmmc);
  2253. #else
  2254. HAL_MMC_RxCpltCallback(hmmc);
  2255. #endif
  2256. }
  2257. /**
  2258. * @brief DMA MMC communication error callback
  2259. * @param hdma: DMA handle
  2260. * @retval None
  2261. */
  2262. static void MMC_DMAError(DMA_HandleTypeDef *hdma)
  2263. {
  2264. MMC_HandleTypeDef* hmmc = (MMC_HandleTypeDef* )(hdma->Parent);
  2265. HAL_MMC_CardStateTypeDef CardState;
  2266. uint32_t RxErrorCode, TxErrorCode;
  2267. /* if DMA error is FIFO error ignore it */
  2268. if(HAL_DMA_GetError(hdma) != HAL_DMA_ERROR_FE)
  2269. {
  2270. RxErrorCode = hmmc->hdmarx->ErrorCode;
  2271. TxErrorCode = hmmc->hdmatx->ErrorCode;
  2272. if((RxErrorCode == HAL_DMA_ERROR_TE) || (TxErrorCode == HAL_DMA_ERROR_TE))
  2273. {
  2274. /* Clear All flags */
  2275. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
  2276. /* Disable All interrupts */
  2277. __HAL_MMC_DISABLE_IT(hmmc, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
  2278. SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR);
  2279. hmmc->ErrorCode |= HAL_MMC_ERROR_DMA;
  2280. CardState = HAL_MMC_GetCardState(hmmc);
  2281. if((CardState == HAL_MMC_CARD_RECEIVING) || (CardState == HAL_MMC_CARD_SENDING))
  2282. {
  2283. hmmc->ErrorCode |= SDMMC_CmdStopTransfer(hmmc->Instance);
  2284. }
  2285. hmmc->State= HAL_MMC_STATE_READY;
  2286. }
  2287. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  2288. hmmc->ErrorCallback(hmmc);
  2289. #else
  2290. HAL_MMC_ErrorCallback(hmmc);
  2291. #endif
  2292. }
  2293. }
  2294. /**
  2295. * @brief DMA MMC Tx Abort callback
  2296. * @param hdma: DMA handle
  2297. * @retval None
  2298. */
  2299. static void MMC_DMATxAbort(DMA_HandleTypeDef *hdma)
  2300. {
  2301. MMC_HandleTypeDef* hmmc = (MMC_HandleTypeDef* )(hdma->Parent);
  2302. HAL_MMC_CardStateTypeDef CardState;
  2303. if(hmmc->hdmatx != NULL)
  2304. {
  2305. hmmc->hdmatx = NULL;
  2306. }
  2307. /* All DMA channels are aborted */
  2308. if(hmmc->hdmarx == NULL)
  2309. {
  2310. CardState = HAL_MMC_GetCardState(hmmc);
  2311. hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
  2312. hmmc->State = HAL_MMC_STATE_READY;
  2313. if((CardState == HAL_MMC_CARD_RECEIVING) || (CardState == HAL_MMC_CARD_SENDING))
  2314. {
  2315. hmmc->ErrorCode |= SDMMC_CmdStopTransfer(hmmc->Instance);
  2316. if(hmmc->ErrorCode != HAL_MMC_ERROR_NONE)
  2317. {
  2318. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  2319. hmmc->AbortCpltCallback(hmmc);
  2320. #else
  2321. HAL_MMC_AbortCallback(hmmc);
  2322. #endif
  2323. }
  2324. else
  2325. {
  2326. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  2327. hmmc->ErrorCallback(hmmc);
  2328. #else
  2329. HAL_MMC_ErrorCallback(hmmc);
  2330. #endif
  2331. }
  2332. }
  2333. }
  2334. }
  2335. /**
  2336. * @brief DMA MMC Rx Abort callback
  2337. * @param hdma: DMA handle
  2338. * @retval None
  2339. */
  2340. static void MMC_DMARxAbort(DMA_HandleTypeDef *hdma)
  2341. {
  2342. MMC_HandleTypeDef* hmmc = (MMC_HandleTypeDef* )(hdma->Parent);
  2343. HAL_MMC_CardStateTypeDef CardState;
  2344. if(hmmc->hdmarx != NULL)
  2345. {
  2346. hmmc->hdmarx = NULL;
  2347. }
  2348. /* All DMA channels are aborted */
  2349. if(hmmc->hdmatx == NULL)
  2350. {
  2351. CardState = HAL_MMC_GetCardState(hmmc);
  2352. hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
  2353. hmmc->State = HAL_MMC_STATE_READY;
  2354. if((CardState == HAL_MMC_CARD_RECEIVING) || (CardState == HAL_MMC_CARD_SENDING))
  2355. {
  2356. hmmc->ErrorCode |= SDMMC_CmdStopTransfer(hmmc->Instance);
  2357. if(hmmc->ErrorCode != HAL_MMC_ERROR_NONE)
  2358. {
  2359. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  2360. hmmc->AbortCpltCallback(hmmc);
  2361. #else
  2362. HAL_MMC_AbortCallback(hmmc);
  2363. #endif
  2364. }
  2365. else
  2366. {
  2367. #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
  2368. hmmc->ErrorCallback(hmmc);
  2369. #else
  2370. HAL_MMC_ErrorCallback(hmmc);
  2371. #endif
  2372. }
  2373. }
  2374. }
  2375. }
  2376. /**
  2377. * @brief Initializes the mmc card.
  2378. * @param hmmc: Pointer to MMC handle
  2379. * @retval MMC Card error state
  2380. */
  2381. static uint32_t MMC_InitCard(MMC_HandleTypeDef *hmmc)
  2382. {
  2383. HAL_MMC_CardCSDTypeDef CSD;
  2384. uint32_t errorstate;
  2385. uint16_t mmc_rca = 2U;
  2386. MMC_InitTypeDef Init;
  2387. /* Check the power State */
  2388. if(SDIO_GetPowerState(hmmc->Instance) == 0U)
  2389. {
  2390. /* Power off */
  2391. return HAL_MMC_ERROR_REQUEST_NOT_APPLICABLE;
  2392. }
  2393. /* Send CMD2 ALL_SEND_CID */
  2394. errorstate = SDMMC_CmdSendCID(hmmc->Instance);
  2395. if(errorstate != HAL_MMC_ERROR_NONE)
  2396. {
  2397. return errorstate;
  2398. }
  2399. else
  2400. {
  2401. /* Get Card identification number data */
  2402. hmmc->CID[0U] = SDIO_GetResponse(hmmc->Instance, SDIO_RESP1);
  2403. hmmc->CID[1U] = SDIO_GetResponse(hmmc->Instance, SDIO_RESP2);
  2404. hmmc->CID[2U] = SDIO_GetResponse(hmmc->Instance, SDIO_RESP3);
  2405. hmmc->CID[3U] = SDIO_GetResponse(hmmc->Instance, SDIO_RESP4);
  2406. }
  2407. /* Send CMD3 SET_REL_ADDR with RCA = 2 (should be greater than 1) */
  2408. /* MMC Card publishes its RCA. */
  2409. errorstate = SDMMC_CmdSetRelAddMmc(hmmc->Instance, mmc_rca);
  2410. if(errorstate != HAL_MMC_ERROR_NONE)
  2411. {
  2412. return errorstate;
  2413. }
  2414. /* Get the MMC card RCA */
  2415. hmmc->MmcCard.RelCardAdd = mmc_rca;
  2416. /* Send CMD9 SEND_CSD with argument as card's RCA */
  2417. errorstate = SDMMC_CmdSendCSD(hmmc->Instance, (uint32_t)(hmmc->MmcCard.RelCardAdd << 16U));
  2418. if(errorstate != HAL_MMC_ERROR_NONE)
  2419. {
  2420. return errorstate;
  2421. }
  2422. else
  2423. {
  2424. /* Get Card Specific Data */
  2425. hmmc->CSD[0U] = SDIO_GetResponse(hmmc->Instance, SDIO_RESP1);
  2426. hmmc->CSD[1U] = SDIO_GetResponse(hmmc->Instance, SDIO_RESP2);
  2427. hmmc->CSD[2U] = SDIO_GetResponse(hmmc->Instance, SDIO_RESP3);
  2428. hmmc->CSD[3U] = SDIO_GetResponse(hmmc->Instance, SDIO_RESP4);
  2429. }
  2430. /* Get the Card Class */
  2431. hmmc->MmcCard.Class = (SDIO_GetResponse(hmmc->Instance, SDIO_RESP2) >> 20U);
  2432. /* Select the Card */
  2433. errorstate = SDMMC_CmdSelDesel(hmmc->Instance, (uint32_t)(((uint32_t)hmmc->MmcCard.RelCardAdd) << 16U));
  2434. if(errorstate != HAL_MMC_ERROR_NONE)
  2435. {
  2436. return errorstate;
  2437. }
  2438. /* Get CSD parameters */
  2439. if (HAL_MMC_GetCardCSD(hmmc, &CSD) != HAL_OK)
  2440. {
  2441. return hmmc->ErrorCode;
  2442. }
  2443. /* While card is not ready for data and trial number for sending CMD13 is not exceeded */
  2444. errorstate = SDMMC_CmdSendStatus(hmmc->Instance, (uint32_t)(((uint32_t)hmmc->MmcCard.RelCardAdd) << 16U));
  2445. if(errorstate != HAL_MMC_ERROR_NONE)
  2446. {
  2447. hmmc->ErrorCode |= errorstate;
  2448. }
  2449. /* Get Extended CSD parameters */
  2450. if (HAL_MMC_GetCardExtCSD(hmmc, hmmc->Ext_CSD, SDMMC_DATATIMEOUT) != HAL_OK)
  2451. {
  2452. return hmmc->ErrorCode;
  2453. }
  2454. /* While card is not ready for data and trial number for sending CMD13 is not exceeded */
  2455. errorstate = SDMMC_CmdSendStatus(hmmc->Instance, (uint32_t)(((uint32_t)hmmc->MmcCard.RelCardAdd) << 16U));
  2456. if(errorstate != HAL_MMC_ERROR_NONE)
  2457. {
  2458. hmmc->ErrorCode |= errorstate;
  2459. }
  2460. /* Configure the SDIO peripheral */
  2461. Init = hmmc->Init;
  2462. Init.BusWide = SDIO_BUS_WIDE_1B;
  2463. (void)SDIO_Init(hmmc->Instance, Init);
  2464. /* All cards are initialized */
  2465. return HAL_MMC_ERROR_NONE;
  2466. }
  2467. /**
  2468. * @brief Enquires cards about their operating voltage and configures clock
  2469. * controls and stores MMC information that will be needed in future
  2470. * in the MMC handle.
  2471. * @param hmmc: Pointer to MMC handle
  2472. * @retval error state
  2473. */
  2474. static uint32_t MMC_PowerON(MMC_HandleTypeDef *hmmc)
  2475. {
  2476. __IO uint32_t count = 0U;
  2477. uint32_t response = 0U, validvoltage = 0U;
  2478. uint32_t errorstate;
  2479. /* CMD0: GO_IDLE_STATE */
  2480. errorstate = SDMMC_CmdGoIdleState(hmmc->Instance);
  2481. if(errorstate != HAL_MMC_ERROR_NONE)
  2482. {
  2483. return errorstate;
  2484. }
  2485. while(validvoltage == 0U)
  2486. {
  2487. if(count++ == SDMMC_MAX_VOLT_TRIAL)
  2488. {
  2489. return HAL_MMC_ERROR_INVALID_VOLTRANGE;
  2490. }
  2491. /* SEND CMD1 APP_CMD with voltage range as argument */
  2492. errorstate = SDMMC_CmdOpCondition(hmmc->Instance, MMC_VOLTAGE_RANGE);
  2493. if(errorstate != HAL_MMC_ERROR_NONE)
  2494. {
  2495. return HAL_MMC_ERROR_UNSUPPORTED_FEATURE;
  2496. }
  2497. /* Get command response */
  2498. response = SDIO_GetResponse(hmmc->Instance, SDIO_RESP1);
  2499. /* Get operating voltage*/
  2500. validvoltage = (((response >> 31U) == 1U) ? 1U : 0U);
  2501. }
  2502. /* When power routine is finished and command returns valid voltage */
  2503. if (((response & (0xFF000000U)) >> 24U) == 0xC0U)
  2504. {
  2505. hmmc->MmcCard.CardType = MMC_HIGH_CAPACITY_CARD;
  2506. }
  2507. else
  2508. {
  2509. hmmc->MmcCard.CardType = MMC_LOW_CAPACITY_CARD;
  2510. }
  2511. return HAL_MMC_ERROR_NONE;
  2512. }
  2513. /**
  2514. * @brief Turns the SDIO output signals off.
  2515. * @param hmmc: Pointer to MMC handle
  2516. * @retval None
  2517. */
  2518. static void MMC_PowerOFF(MMC_HandleTypeDef *hmmc)
  2519. {
  2520. /* Set Power State to OFF */
  2521. (void)SDIO_PowerState_OFF(hmmc->Instance);
  2522. }
  2523. /**
  2524. * @brief Returns the current card's status.
  2525. * @param hmmc: Pointer to MMC handle
  2526. * @param pCardStatus: pointer to the buffer that will contain the MMC card
  2527. * status (Card Status register)
  2528. * @retval error state
  2529. */
  2530. static uint32_t MMC_SendStatus(MMC_HandleTypeDef *hmmc, uint32_t *pCardStatus)
  2531. {
  2532. uint32_t errorstate;
  2533. if(pCardStatus == NULL)
  2534. {
  2535. return HAL_MMC_ERROR_PARAM;
  2536. }
  2537. /* Send Status command */
  2538. errorstate = SDMMC_CmdSendStatus(hmmc->Instance, (uint32_t)(hmmc->MmcCard.RelCardAdd << 16U));
  2539. if(errorstate != HAL_MMC_ERROR_NONE)
  2540. {
  2541. return errorstate;
  2542. }
  2543. /* Get MMC card status */
  2544. *pCardStatus = SDIO_GetResponse(hmmc->Instance, SDIO_RESP1);
  2545. return HAL_MMC_ERROR_NONE;
  2546. }
  2547. /**
  2548. * @brief Reads extended CSD register to get the sectors number of the device
  2549. * @param hmmc: Pointer to MMC handle
  2550. * @param pFieldData: Pointer to the read buffer
  2551. * @param FieldIndex: Index of the field to be read
  2552. * @param Timeout: Specify timeout value
  2553. * @retval HAL status
  2554. */
  2555. static uint32_t MMC_ReadExtCSD(MMC_HandleTypeDef *hmmc, uint32_t *pFieldData, uint16_t FieldIndex, uint32_t Timeout)
  2556. {
  2557. SDIO_DataInitTypeDef config;
  2558. uint32_t errorstate;
  2559. uint32_t tickstart = HAL_GetTick();
  2560. uint32_t count;
  2561. uint32_t i = 0;
  2562. uint32_t tmp_data;
  2563. hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
  2564. /* Initialize data control register */
  2565. hmmc->Instance->DCTRL = 0;
  2566. /* Configure the MMC DPSM (Data Path State Machine) */
  2567. config.DataTimeOut = SDMMC_DATATIMEOUT;
  2568. config.DataLength = 512;
  2569. config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
  2570. config.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
  2571. config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
  2572. config.DPSM = SDIO_DPSM_ENABLE;
  2573. (void)SDIO_ConfigData(hmmc->Instance, &config);
  2574. /* Set Block Size for Card */
  2575. errorstate = SDMMC_CmdSendEXTCSD(hmmc->Instance, 0);
  2576. if(errorstate != HAL_MMC_ERROR_NONE)
  2577. {
  2578. /* Clear all the static flags */
  2579. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
  2580. hmmc->ErrorCode |= errorstate;
  2581. hmmc->State = HAL_MMC_STATE_READY;
  2582. return HAL_ERROR;
  2583. }
  2584. /* Poll on SDMMC flags */
  2585. while(!__HAL_MMC_GET_FLAG(hmmc, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND))
  2586. {
  2587. if(__HAL_MMC_GET_FLAG(hmmc, SDIO_FLAG_RXFIFOHF))
  2588. {
  2589. /* Read data from SDMMC Rx FIFO */
  2590. for(count = 0U; count < 8U; count++)
  2591. {
  2592. tmp_data = SDIO_ReadFIFO(hmmc->Instance);
  2593. /* eg : SEC_COUNT : FieldIndex = 212 => i+count = 53 */
  2594. /* DEVICE_TYPE : FieldIndex = 196 => i+count = 49 */
  2595. if ((i + count) == ((uint32_t)FieldIndex/4U))
  2596. {
  2597. *pFieldData = tmp_data;
  2598. }
  2599. }
  2600. i += 8U;
  2601. }
  2602. if(((HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U))
  2603. {
  2604. /* Clear all the static flags */
  2605. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
  2606. hmmc->ErrorCode |= HAL_MMC_ERROR_TIMEOUT;
  2607. hmmc->State= HAL_MMC_STATE_READY;
  2608. return HAL_TIMEOUT;
  2609. }
  2610. }
  2611. /* While card is not ready for data and trial number for sending CMD13 is not exceeded */
  2612. errorstate = SDMMC_CmdSendStatus(hmmc->Instance, (uint32_t)(((uint32_t)hmmc->MmcCard.RelCardAdd) << 16));
  2613. if(errorstate != HAL_MMC_ERROR_NONE)
  2614. {
  2615. hmmc->ErrorCode |= errorstate;
  2616. }
  2617. /* Clear all the static flags */
  2618. __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_DATA_FLAGS);
  2619. hmmc->State = HAL_MMC_STATE_READY;
  2620. return HAL_OK;
  2621. }
  2622. /**
  2623. * @brief Wrap up reading in non-blocking mode.
  2624. * @param hmmc: pointer to a MMC_HandleTypeDef structure that contains
  2625. * the configuration information.
  2626. * @retval None
  2627. */
  2628. static void MMC_Read_IT(MMC_HandleTypeDef *hmmc)
  2629. {
  2630. uint32_t count, data, dataremaining;
  2631. uint8_t* tmp;
  2632. tmp = hmmc->pRxBuffPtr;
  2633. dataremaining = hmmc->RxXferSize;
  2634. if (dataremaining > 0U)
  2635. {
  2636. /* Read data from SDIO Rx FIFO */
  2637. for(count = 0U; count < 8U; count++)
  2638. {
  2639. data = SDIO_ReadFIFO(hmmc->Instance);
  2640. *tmp = (uint8_t)(data & 0xFFU);
  2641. tmp++;
  2642. dataremaining--;
  2643. *tmp = (uint8_t)((data >> 8U) & 0xFFU);
  2644. tmp++;
  2645. dataremaining--;
  2646. *tmp = (uint8_t)((data >> 16U) & 0xFFU);
  2647. tmp++;
  2648. dataremaining--;
  2649. *tmp = (uint8_t)((data >> 24U) & 0xFFU);
  2650. tmp++;
  2651. dataremaining--;
  2652. }
  2653. hmmc->pRxBuffPtr = tmp;
  2654. hmmc->RxXferSize = dataremaining;
  2655. }
  2656. }
  2657. /**
  2658. * @brief Wrap up writing in non-blocking mode.
  2659. * @param hmmc: pointer to a MMC_HandleTypeDef structure that contains
  2660. * the configuration information.
  2661. * @retval None
  2662. */
  2663. static void MMC_Write_IT(MMC_HandleTypeDef *hmmc)
  2664. {
  2665. uint32_t count, data, dataremaining;
  2666. uint8_t* tmp;
  2667. tmp = hmmc->pTxBuffPtr;
  2668. dataremaining = hmmc->TxXferSize;
  2669. if (dataremaining > 0U)
  2670. {
  2671. /* Write data to SDIO Tx FIFO */
  2672. for(count = 0U; count < 8U; count++)
  2673. {
  2674. data = (uint32_t)(*tmp);
  2675. tmp++;
  2676. dataremaining--;
  2677. data |= ((uint32_t)(*tmp) << 8U);
  2678. tmp++;
  2679. dataremaining--;
  2680. data |= ((uint32_t)(*tmp) << 16U);
  2681. tmp++;
  2682. dataremaining--;
  2683. data |= ((uint32_t)(*tmp) << 24U);
  2684. tmp++;
  2685. dataremaining--;
  2686. (void)SDIO_WriteFIFO(hmmc->Instance, &data);
  2687. }
  2688. hmmc->pTxBuffPtr = tmp;
  2689. hmmc->TxXferSize = dataremaining;
  2690. }
  2691. }
  2692. /**
  2693. * @brief Update the power class of the device.
  2694. * @param hmmc MMC handle
  2695. * @param Wide Wide of MMC bus
  2696. * @param Speed Speed of the MMC bus
  2697. * @retval MMC Card error state
  2698. */
  2699. static uint32_t MMC_PwrClassUpdate(MMC_HandleTypeDef *hmmc, uint32_t Wide)
  2700. {
  2701. uint32_t count;
  2702. uint32_t response = 0U;
  2703. uint32_t errorstate = HAL_MMC_ERROR_NONE;
  2704. uint32_t power_class, supported_pwr_class;
  2705. if((Wide == SDIO_BUS_WIDE_8B) || (Wide == SDIO_BUS_WIDE_4B))
  2706. {
  2707. power_class = 0U; /* Default value after power-on or software reset */
  2708. /* Read the PowerClass field of the Extended CSD register */
  2709. if(MMC_ReadExtCSD(hmmc, &power_class, 187, SDMMC_DATATIMEOUT) != HAL_OK) /* Field POWER_CLASS [187] */
  2710. {
  2711. errorstate = SDMMC_ERROR_GENERAL_UNKNOWN_ERR;
  2712. }
  2713. else
  2714. {
  2715. power_class = ((power_class >> 24U) & 0x000000FFU);
  2716. }
  2717. /* Get the supported PowerClass field of the Extended CSD register */
  2718. /* Field PWR_CL_26_xxx [201 or 203] */
  2719. supported_pwr_class = ((hmmc->Ext_CSD[(MMC_EXT_CSD_PWR_CL_26_INDEX/4)] >> MMC_EXT_CSD_PWR_CL_26_POS) & 0x000000FFU);
  2720. if(errorstate == HAL_MMC_ERROR_NONE)
  2721. {
  2722. if(Wide == SDIO_BUS_WIDE_8B)
  2723. {
  2724. /* Bit [7:4] : power class for 8-bits bus configuration - Bit [3:0] : power class for 4-bits bus configuration */
  2725. supported_pwr_class = (supported_pwr_class >> 4U);
  2726. }
  2727. if ((power_class & 0x0FU) != (supported_pwr_class & 0x0FU))
  2728. {
  2729. /* Need to change current power class */
  2730. errorstate = SDMMC_CmdSwitch(hmmc->Instance, (0x03BB0000U | ((supported_pwr_class & 0x0FU) << 8U)));
  2731. if(errorstate == HAL_MMC_ERROR_NONE)
  2732. {
  2733. /* While card is not ready for data and trial number for sending CMD13 is not exceeded */
  2734. count = SDMMC_MAX_TRIAL;
  2735. do
  2736. {
  2737. errorstate = SDMMC_CmdSendStatus(hmmc->Instance, (uint32_t)(((uint32_t)hmmc->MmcCard.RelCardAdd) << 16U));
  2738. if(errorstate != HAL_MMC_ERROR_NONE)
  2739. {
  2740. break;
  2741. }
  2742. /* Get command response */
  2743. response = SDIO_GetResponse(hmmc->Instance, SDIO_RESP1);
  2744. count--;
  2745. }while(((response & 0x100U) == 0U) && (count != 0U));
  2746. /* Check the status after the switch command execution */
  2747. if ((count != 0U) && (errorstate == HAL_MMC_ERROR_NONE))
  2748. {
  2749. /* Check the bit SWITCH_ERROR of the device status */
  2750. if ((response & 0x80U) != 0U)
  2751. {
  2752. errorstate = SDMMC_ERROR_UNSUPPORTED_FEATURE;
  2753. }
  2754. }
  2755. else if (count == 0U)
  2756. {
  2757. errorstate = SDMMC_ERROR_TIMEOUT;
  2758. }
  2759. else
  2760. {
  2761. /* Nothing to do */
  2762. }
  2763. }
  2764. }
  2765. }
  2766. }
  2767. return errorstate;
  2768. }
  2769. /**
  2770. * @}
  2771. */
  2772. #endif /* SDIO */
  2773. #endif /* HAL_MMC_MODULE_ENABLED */
  2774. /**
  2775. * @}
  2776. */
  2777. /**
  2778. * @}
  2779. */