You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

3160 lines
98 KiB

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