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.
 
 
 

2501 lines
77 KiB

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