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.
 
 
 

2920 lines
86 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f7xx_hal_sd.c
  4. * @author MCD Application Team
  5. * @version V1.2.2
  6. * @date 14-April-2017
  7. * @brief SD card HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of the Secure Digital (SD) peripheral:
  10. * + Initialization and de-initialization functions
  11. * + IO operation functions
  12. * + Peripheral Control functions
  13. * + SD 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_SD_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 SD and uSD cards devices.
  29. It is used as follows:
  30. (#)Initialize the SDMMC low level resources by implement the HAL_SD_MspInit() API:
  31. (##) Enable the SDMMC interface clock using __HAL_RCC_SDMMC_CLK_ENABLE();
  32. (##) SDMMC pins configuration for SD 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_SD_ReadBlocks_DMA()
  37. and HAL_SD_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_SD_ENABLE_IT()
  45. and __HAL_SD_DISABLE_IT() inside the communication process.
  46. (+++) SDMMC interrupts pending bits are managed using the macros __HAL_SD_GET_IT()
  47. and __HAL_SD_CLEAR_IT()
  48. (##) NVIC configuration if you need to use interrupt process (HAL_SD_ReadBlocks_IT()
  49. and HAL_SD_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_SD_ENABLE_IT()
  54. and __HAL_SD_DISABLE_IT() inside the communication process.
  55. (+++) SDMMC interrupts pending bits are managed using the macros __HAL_SD_GET_IT()
  56. and __HAL_SD_CLEAR_IT()
  57. (#) At this stage, you can perform SD read/write/erase operations after SD card initialization
  58. *** SD Card Initialization and configuration ***
  59. ================================================
  60. [..]
  61. To initialize the SD Card, use the HAL_SD_Init() function. It Initializes
  62. SDMMC IP (STM32 side) and the SD 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 SD Card frequency (SDMMC_CK) is computed as follows:
  68. SDMMC_CK = SDMMCCLK / (ClockDiv + 2)
  69. In initialization mode and according to the SD 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 SD card. The API used is HAL_SD_InitCard().
  74. This phase allows the card initialization and identification
  75. and check the SD Card type (Standard Capacity or High Capacity)
  76. The initialization flow is compatible with SD standard.
  77. This API (HAL_SD_InitCard()) could be used also to reinitialize the card in case
  78. of plug-off plug-in.
  79. (#) Configure the SD 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 SD 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 SD Card according to the address read with the step 2.
  88. (#) Configure the SD Card in wide bus mode: 4-bits data.
  89. *** SD Card Read operation ***
  90. ==============================
  91. [..]
  92. (+) You can read from SD card in polling mode by using function HAL_SD_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_SD_GetCardState() function for SD card state.
  98. (+) You can read from SD card in DMA mode by using function HAL_SD_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_SD_GetCardState() function for SD card state.
  104. You could also check the DMA transfer process through the SD Rx interrupt event.
  105. (+) You can read from SD card in Interrupt mode by using function HAL_SD_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_SD_GetCardState() function for SD card state.
  111. You could also check the IT transfer process through the SD Rx interrupt event.
  112. *** SD Card Write operation ***
  113. ===============================
  114. [..]
  115. (+) You can write to SD card in polling mode by using function HAL_SD_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_SD_GetCardState() function for SD card state.
  121. (+) You can write to SD card in DMA mode by using function HAL_SD_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_SD_GetCardState() function for SD card state.
  127. You could also check the DMA transfer process through the SD Tx interrupt event.
  128. (+) You can write to SD card in Interrupt mode by using function HAL_SD_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_SD_GetCardState() function for SD card state.
  134. You could also check the IT transfer process through the SD Tx interrupt event.
  135. *** SD card status ***
  136. ======================
  137. [..]
  138. (+) The SD Status contains status bits that are related to the SD Memory
  139. Card proprietary features. To get SD card status use the HAL_SD_GetCardStatus().
  140. *** SD card information ***
  141. ===========================
  142. [..]
  143. (+) To get SD card information, you can use the function HAL_SD_GetCardInfo().
  144. It returns useful information about the SD card such as block size, card type,
  145. block number ...
  146. *** SD card CSD register ***
  147. ============================
  148. [..]
  149. (+) The HAL_SD_GetCardCSD() API allows to get the parameters of the CSD register.
  150. Some of the CSD parameters are useful for card initialization and identification.
  151. *** SD card CID register ***
  152. ============================
  153. [..]
  154. (+) The HAL_SD_GetCardCID() API allows to get the parameters of the CID register.
  155. Some of the CSD parameters are useful for card initialization and identification.
  156. *** SD HAL driver macros list ***
  157. ==================================
  158. [..]
  159. Below the list of most used macros in SD HAL driver.
  160. (+) __HAL_SD_ENABLE : Enable the SD device
  161. (+) __HAL_SD_DISABLE : Disable the SD device
  162. (+) __HAL_SD_DMA_ENABLE: Enable the SDMMC DMA transfer
  163. (+) __HAL_SD_DMA_DISABLE: Disable the SDMMC DMA transfer
  164. (+) __HAL_SD_ENABLE_IT: Enable the SD device interrupt
  165. (+) __HAL_SD_DISABLE_IT: Disable the SD device interrupt
  166. (+) __HAL_SD_GET_FLAG:Check whether the specified SD flag is set or not
  167. (+) __HAL_SD_CLEAR_FLAG: Clear the SD's pending flags
  168. [..]
  169. (@) You can refer to the SD HAL driver header file for more useful macros
  170. @endverbatim
  171. ******************************************************************************
  172. * @attention
  173. *
  174. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  175. *
  176. * Redistribution and use in source and binary forms, with or without modification,
  177. * are permitted provided that the following conditions are met:
  178. * 1. Redistributions of source code must retain the above copyright notice,
  179. * this list of conditions and the following disclaimer.
  180. * 2. Redistributions in binary form must reproduce the above copyright notice,
  181. * this list of conditions and the following disclaimer in the documentation
  182. * and/or other materials provided with the distribution.
  183. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  184. * may be used to endorse or promote products derived from this software
  185. * without specific prior written permission.
  186. *
  187. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  188. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  189. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  190. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  191. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  192. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  193. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  194. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  195. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  196. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  197. *
  198. ******************************************************************************
  199. */
  200. /* Includes ------------------------------------------------------------------*/
  201. #include "stm32f7xx_hal.h"
  202. /** @addtogroup STM32F7xx_HAL_Driver
  203. * @{
  204. */
  205. /** @addtogroup SD
  206. * @{
  207. */
  208. #ifdef HAL_SD_MODULE_ENABLED
  209. /* Private typedef -----------------------------------------------------------*/
  210. /* Private define ------------------------------------------------------------*/
  211. /** @addtogroup SD_Private_Defines
  212. * @{
  213. */
  214. /**
  215. * @}
  216. */
  217. /* Private macro -------------------------------------------------------------*/
  218. /* Private variables ---------------------------------------------------------*/
  219. /* Private function prototypes -----------------------------------------------*/
  220. /* Private functions ---------------------------------------------------------*/
  221. /** @defgroup SD_Private_Functions SD Private Functions
  222. * @{
  223. */
  224. static uint32_t SD_InitCard(SD_HandleTypeDef *hsd);
  225. static uint32_t SD_PowerON(SD_HandleTypeDef *hsd);
  226. static uint32_t SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus);
  227. static uint32_t SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus);
  228. static uint32_t SD_WideBus_Enable(SD_HandleTypeDef *hsd);
  229. static uint32_t SD_WideBus_Disable(SD_HandleTypeDef *hsd);
  230. static uint32_t SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR);
  231. static HAL_StatusTypeDef SD_PowerOFF(SD_HandleTypeDef *hsd);
  232. static HAL_StatusTypeDef SD_Write_IT(SD_HandleTypeDef *hsd);
  233. static HAL_StatusTypeDef SD_Read_IT(SD_HandleTypeDef *hsd);
  234. static void SD_DMATransmitCplt(DMA_HandleTypeDef *hdma);
  235. static void SD_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
  236. static void SD_DMAError(DMA_HandleTypeDef *hdma);
  237. static void SD_DMATxAbort(DMA_HandleTypeDef *hdma);
  238. static void SD_DMARxAbort(DMA_HandleTypeDef *hdma);
  239. /**
  240. * @}
  241. */
  242. /* Exported functions --------------------------------------------------------*/
  243. /** @addtogroup SD_Exported_Functions
  244. * @{
  245. */
  246. /** @addtogroup SD_Exported_Functions_Group1
  247. * @brief Initialization and de-initialization functions
  248. *
  249. @verbatim
  250. ==============================================================================
  251. ##### Initialization and de-initialization functions #####
  252. ==============================================================================
  253. [..]
  254. This section provides functions allowing to initialize/de-initialize the SD
  255. card device to be ready for use.
  256. @endverbatim
  257. * @{
  258. */
  259. /**
  260. * @brief Initializes the SD according to the specified parameters in the
  261. SD_HandleTypeDef and create the associated handle.
  262. * @param hsd: Pointer to the SD handle
  263. * @retval HAL status
  264. */
  265. HAL_StatusTypeDef HAL_SD_Init(SD_HandleTypeDef *hsd)
  266. {
  267. /* Check the SD handle allocation */
  268. if(hsd == NULL)
  269. {
  270. return HAL_ERROR;
  271. }
  272. /* Check the parameters */
  273. assert_param(IS_SDMMC_ALL_INSTANCE(hsd->Instance));
  274. assert_param(IS_SDMMC_CLOCK_EDGE(hsd->Init.ClockEdge));
  275. assert_param(IS_SDMMC_CLOCK_BYPASS(hsd->Init.ClockBypass));
  276. assert_param(IS_SDMMC_CLOCK_POWER_SAVE(hsd->Init.ClockPowerSave));
  277. assert_param(IS_SDMMC_BUS_WIDE(hsd->Init.BusWide));
  278. assert_param(IS_SDMMC_HARDWARE_FLOW_CONTROL(hsd->Init.HardwareFlowControl));
  279. assert_param(IS_SDMMC_CLKDIV(hsd->Init.ClockDiv));
  280. if(hsd->State == HAL_SD_STATE_RESET)
  281. {
  282. /* Allocate lock resource and initialize it */
  283. hsd->Lock = HAL_UNLOCKED;
  284. /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
  285. HAL_SD_MspInit(hsd);
  286. }
  287. hsd->State = HAL_SD_STATE_BUSY;
  288. /* Initialize the Card parameters */
  289. HAL_SD_InitCard(hsd);
  290. /* Initialize the error code */
  291. hsd->ErrorCode = HAL_DMA_ERROR_NONE;
  292. /* Initialize the SD operation */
  293. hsd->Context = SD_CONTEXT_NONE;
  294. /* Initialize the SD state */
  295. hsd->State = HAL_SD_STATE_READY;
  296. return HAL_OK;
  297. }
  298. /**
  299. * @brief Initializes the SD Card.
  300. * @param hsd: Pointer to SD handle
  301. * @note This function initializes the SD card. It could be used when a card
  302. re-initialization is needed.
  303. * @retval HAL status
  304. */
  305. HAL_StatusTypeDef HAL_SD_InitCard(SD_HandleTypeDef *hsd)
  306. {
  307. uint32_t errorstate = HAL_SD_ERROR_NONE;
  308. SD_InitTypeDef Init;
  309. /* Default SDMMC peripheral configuration for SD card initialization */
  310. Init.ClockEdge = SDMMC_CLOCK_EDGE_RISING;
  311. Init.ClockBypass = SDMMC_CLOCK_BYPASS_DISABLE;
  312. Init.ClockPowerSave = SDMMC_CLOCK_POWER_SAVE_DISABLE;
  313. Init.BusWide = SDMMC_BUS_WIDE_1B;
  314. Init.HardwareFlowControl = SDMMC_HARDWARE_FLOW_CONTROL_DISABLE;
  315. Init.ClockDiv = SDMMC_INIT_CLK_DIV;
  316. /* Initialize SDMMC peripheral interface with default configuration */
  317. SDMMC_Init(hsd->Instance, Init);
  318. /* Disable SDMMC Clock */
  319. __HAL_SD_DISABLE(hsd);
  320. /* Set Power State to ON */
  321. SDMMC_PowerState_ON(hsd->Instance);
  322. /* Enable SDMMC Clock */
  323. __HAL_SD_ENABLE(hsd);
  324. /* Required power up waiting time before starting the SD initialization sequence */
  325. HAL_Delay(2);
  326. /* Identify card operating voltage */
  327. errorstate = SD_PowerON(hsd);
  328. if(errorstate != HAL_SD_ERROR_NONE)
  329. {
  330. hsd->State = HAL_SD_STATE_READY;
  331. hsd->ErrorCode |= errorstate;
  332. return HAL_ERROR;
  333. }
  334. /* Card initialization */
  335. errorstate = SD_InitCard(hsd);
  336. if(errorstate != HAL_SD_ERROR_NONE)
  337. {
  338. hsd->State = HAL_SD_STATE_READY;
  339. hsd->ErrorCode |= errorstate;
  340. return HAL_ERROR;
  341. }
  342. return HAL_OK;
  343. }
  344. /**
  345. * @brief De-Initializes the SD card.
  346. * @param hsd: Pointer to SD handle
  347. * @retval HAL status
  348. */
  349. HAL_StatusTypeDef HAL_SD_DeInit(SD_HandleTypeDef *hsd)
  350. {
  351. /* Check the SD handle allocation */
  352. if(hsd == NULL)
  353. {
  354. return HAL_ERROR;
  355. }
  356. /* Check the parameters */
  357. assert_param(IS_SDMMC_ALL_INSTANCE(hsd->Instance));
  358. hsd->State = HAL_SD_STATE_BUSY;
  359. /* Set SD power state to off */
  360. SD_PowerOFF(hsd);
  361. /* De-Initialize the MSP layer */
  362. HAL_SD_MspDeInit(hsd);
  363. hsd->ErrorCode = HAL_SD_ERROR_NONE;
  364. hsd->State = HAL_SD_STATE_RESET;
  365. return HAL_OK;
  366. }
  367. /**
  368. * @brief Initializes the SD MSP.
  369. * @param hsd: Pointer to SD handle
  370. * @retval None
  371. */
  372. __weak void HAL_SD_MspInit(SD_HandleTypeDef *hsd)
  373. {
  374. /* Prevent unused argument(s) compilation warning */
  375. UNUSED(hsd);
  376. /* NOTE : This function Should not be modified, when the callback is needed,
  377. the HAL_SD_MspInit could be implemented in the user file
  378. */
  379. }
  380. /**
  381. * @brief De-Initialize SD MSP.
  382. * @param hsd: Pointer to SD handle
  383. * @retval None
  384. */
  385. __weak void HAL_SD_MspDeInit(SD_HandleTypeDef *hsd)
  386. {
  387. /* Prevent unused argument(s) compilation warning */
  388. UNUSED(hsd);
  389. /* NOTE : This function Should not be modified, when the callback is needed,
  390. the HAL_SD_MspDeInit could be implemented in the user file
  391. */
  392. }
  393. /**
  394. * @}
  395. */
  396. /** @addtogroup SD_Exported_Functions_Group2
  397. * @brief Data transfer functions
  398. *
  399. @verbatim
  400. ==============================================================================
  401. ##### IO operation functions #####
  402. ==============================================================================
  403. [..]
  404. This subsection provides a set of functions allowing to manage the data
  405. transfer from/to SD card.
  406. @endverbatim
  407. * @{
  408. */
  409. /**
  410. * @brief Reads block(s) from a specified address in a card. The Data transfer
  411. * is managed by polling mode.
  412. * @note This API should be followed by a check on the card state through
  413. * HAL_SD_GetCardState().
  414. * @param hsd: Pointer to SD handle
  415. * @param pData: pointer to the buffer that will contain the received data
  416. * @param BlockAdd: Block Address from where data is to be read
  417. * @param NumberOfBlocks: Number of SD blocks to read
  418. * @param Timeout: Specify timeout value
  419. * @retval HAL status
  420. */
  421. HAL_StatusTypeDef HAL_SD_ReadBlocks(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout)
  422. {
  423. SDMMC_DataInitTypeDef config;
  424. uint32_t errorstate = HAL_SD_ERROR_NONE;
  425. uint32_t tickstart = HAL_GetTick();
  426. uint32_t count = 0, *tempbuff = (uint32_t *)pData;
  427. if(NULL == pData)
  428. {
  429. hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
  430. return HAL_ERROR;
  431. }
  432. if(hsd->State == HAL_SD_STATE_READY)
  433. {
  434. hsd->ErrorCode = HAL_DMA_ERROR_NONE;
  435. if((BlockAdd + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
  436. {
  437. hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
  438. return HAL_ERROR;
  439. }
  440. hsd->State = HAL_SD_STATE_BUSY;
  441. /* Initialize data control register */
  442. hsd->Instance->DCTRL = 0;
  443. if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
  444. {
  445. BlockAdd *= 512;
  446. }
  447. /* Set Block Size for Card */
  448. errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
  449. if(errorstate != HAL_SD_ERROR_NONE)
  450. {
  451. /* Clear all the static flags */
  452. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  453. hsd->ErrorCode |= errorstate;
  454. hsd->State = HAL_SD_STATE_READY;
  455. return HAL_ERROR;
  456. }
  457. /* Configure the SD DPSM (Data Path State Machine) */
  458. config.DataTimeOut = SDMMC_DATATIMEOUT;
  459. config.DataLength = NumberOfBlocks * BLOCKSIZE;
  460. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
  461. config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
  462. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  463. config.DPSM = SDMMC_DPSM_ENABLE;
  464. SDMMC_ConfigData(hsd->Instance, &config);
  465. /* Read block(s) in polling mode */
  466. if(NumberOfBlocks > 1)
  467. {
  468. hsd->Context = SD_CONTEXT_READ_MULTIPLE_BLOCK;
  469. /* Read Multi Block command */
  470. errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, BlockAdd);
  471. }
  472. else
  473. {
  474. hsd->Context = SD_CONTEXT_READ_SINGLE_BLOCK;
  475. /* Read Single Block command */
  476. errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, BlockAdd);
  477. }
  478. if(errorstate != HAL_SD_ERROR_NONE)
  479. {
  480. /* Clear all the static flags */
  481. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  482. hsd->ErrorCode |= errorstate;
  483. hsd->State = HAL_SD_STATE_READY;
  484. return HAL_ERROR;
  485. }
  486. /* Poll on SDMMC flags */
  487. while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DATAEND))
  488. {
  489. if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF))
  490. {
  491. /* Read data from SDMMC Rx FIFO */
  492. for(count = 0U; count < 8U; count++)
  493. {
  494. *(tempbuff + count) = SDMMC_ReadFIFO(hsd->Instance);
  495. }
  496. tempbuff += 8U;
  497. }
  498. if((Timeout == 0U)||((HAL_GetTick()-tickstart) >= Timeout))
  499. {
  500. /* Clear all the static flags */
  501. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  502. hsd->ErrorCode |= HAL_SD_ERROR_TIMEOUT;
  503. hsd->State= HAL_SD_STATE_READY;
  504. return HAL_TIMEOUT;
  505. }
  506. }
  507. /* Send stop transmission command in case of multiblock read */
  508. if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DATAEND) && (NumberOfBlocks > 1U))
  509. {
  510. if(hsd->SdCard.CardType != CARD_SECURED)
  511. {
  512. /* Send stop transmission command */
  513. errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
  514. if(errorstate != HAL_SD_ERROR_NONE)
  515. {
  516. /* Clear all the static flags */
  517. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  518. hsd->ErrorCode |= errorstate;
  519. hsd->State = HAL_SD_STATE_READY;
  520. return HAL_ERROR;
  521. }
  522. }
  523. }
  524. /* Get error state */
  525. if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
  526. {
  527. /* Clear all the static flags */
  528. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  529. hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
  530. hsd->State = HAL_SD_STATE_READY;
  531. return HAL_ERROR;
  532. }
  533. else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
  534. {
  535. /* Clear all the static flags */
  536. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  537. hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
  538. hsd->State = HAL_SD_STATE_READY;
  539. return HAL_ERROR;
  540. }
  541. else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
  542. {
  543. /* Clear all the static flags */
  544. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  545. hsd->ErrorCode |= HAL_SD_ERROR_RX_OVERRUN;
  546. hsd->State = HAL_SD_STATE_READY;
  547. return HAL_ERROR;
  548. }
  549. /* Empty FIFO if there is still any data */
  550. while ((__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXDAVL)))
  551. {
  552. *tempbuff = SDMMC_ReadFIFO(hsd->Instance);
  553. tempbuff++;
  554. if((Timeout == 0U)||((HAL_GetTick()-tickstart) >= Timeout))
  555. {
  556. /* Clear all the static flags */
  557. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  558. hsd->ErrorCode |= HAL_SD_ERROR_TIMEOUT;
  559. hsd->State= HAL_SD_STATE_READY;
  560. return HAL_ERROR;
  561. }
  562. }
  563. /* Clear all the static flags */
  564. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  565. hsd->State = HAL_SD_STATE_READY;
  566. return HAL_OK;
  567. }
  568. else
  569. {
  570. hsd->ErrorCode |= HAL_SD_ERROR_BUSY;
  571. return HAL_ERROR;
  572. }
  573. }
  574. /**
  575. * @brief Allows to write block(s) to a specified address in a card. The Data
  576. * transfer is managed by polling mode.
  577. * @note This API should be followed by a check on the card state through
  578. * HAL_SD_GetCardState().
  579. * @param hsd: Pointer to SD handle
  580. * @param pData: pointer to the buffer that will contain the data to transmit
  581. * @param BlockAdd: Block Address where data will be written
  582. * @param NumberOfBlocks: Number of SD blocks to write
  583. * @param Timeout: Specify timeout value
  584. * @retval HAL status
  585. */
  586. HAL_StatusTypeDef HAL_SD_WriteBlocks(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout)
  587. {
  588. SDMMC_DataInitTypeDef config;
  589. uint32_t errorstate = HAL_SD_ERROR_NONE;
  590. uint32_t tickstart = HAL_GetTick();
  591. uint32_t count = 0;
  592. uint32_t *tempbuff = (uint32_t *)pData;
  593. if(NULL == pData)
  594. {
  595. hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
  596. return HAL_ERROR;
  597. }
  598. if(hsd->State == HAL_SD_STATE_READY)
  599. {
  600. hsd->ErrorCode = HAL_DMA_ERROR_NONE;
  601. if((BlockAdd + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
  602. {
  603. hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
  604. return HAL_ERROR;
  605. }
  606. hsd->State = HAL_SD_STATE_BUSY;
  607. /* Initialize data control register */
  608. hsd->Instance->DCTRL = 0;
  609. if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
  610. {
  611. BlockAdd *= 512;
  612. }
  613. /* Set Block Size for Card */
  614. errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
  615. if(errorstate != HAL_SD_ERROR_NONE)
  616. {
  617. /* Clear all the static flags */
  618. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  619. hsd->ErrorCode |= errorstate;
  620. hsd->State = HAL_SD_STATE_READY;
  621. return HAL_ERROR;
  622. }
  623. /* Write Blocks in Polling mode */
  624. if(NumberOfBlocks > 1U)
  625. {
  626. hsd->Context = SD_CONTEXT_WRITE_MULTIPLE_BLOCK;
  627. /* Write Multi Block command */
  628. errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, BlockAdd);
  629. }
  630. else
  631. {
  632. hsd->Context = SD_CONTEXT_WRITE_SINGLE_BLOCK;
  633. /* Write Single Block command */
  634. errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, BlockAdd);
  635. }
  636. if(errorstate != HAL_SD_ERROR_NONE)
  637. {
  638. /* Clear all the static flags */
  639. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  640. hsd->ErrorCode |= errorstate;
  641. hsd->State = HAL_SD_STATE_READY;
  642. return HAL_ERROR;
  643. }
  644. /* Configure the SD DPSM (Data Path State Machine) */
  645. config.DataTimeOut = SDMMC_DATATIMEOUT;
  646. config.DataLength = NumberOfBlocks * BLOCKSIZE;
  647. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
  648. config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
  649. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  650. config.DPSM = SDMMC_DPSM_ENABLE;
  651. SDMMC_ConfigData(hsd->Instance, &config);
  652. /* Write block(s) in polling mode */
  653. while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXUNDERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DATAEND))
  654. {
  655. if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXFIFOHE))
  656. {
  657. /* Write data to SDMMC Tx FIFO */
  658. for(count = 0U; count < 8U; count++)
  659. {
  660. SDMMC_WriteFIFO(hsd->Instance, (tempbuff + count));
  661. }
  662. tempbuff += 8U;
  663. }
  664. if((Timeout == 0U)||((HAL_GetTick()-tickstart) >= Timeout))
  665. {
  666. /* Clear all the static flags */
  667. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  668. hsd->ErrorCode |= errorstate;
  669. hsd->State = HAL_SD_STATE_READY;
  670. return HAL_TIMEOUT;
  671. }
  672. }
  673. /* Send stop transmission command in case of multiblock write */
  674. if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DATAEND) && (NumberOfBlocks > 1U))
  675. {
  676. if(hsd->SdCard.CardType != CARD_SECURED)
  677. {
  678. /* Send stop transmission command */
  679. errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
  680. if(errorstate != HAL_SD_ERROR_NONE)
  681. {
  682. /* Clear all the static flags */
  683. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  684. hsd->ErrorCode |= errorstate;
  685. hsd->State = HAL_SD_STATE_READY;
  686. return HAL_ERROR;
  687. }
  688. }
  689. }
  690. /* Get error state */
  691. if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
  692. {
  693. /* Clear all the static flags */
  694. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  695. hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
  696. hsd->State = HAL_SD_STATE_READY;
  697. return HAL_ERROR;
  698. }
  699. else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
  700. {
  701. /* Clear all the static flags */
  702. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  703. hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
  704. hsd->State = HAL_SD_STATE_READY;
  705. return HAL_ERROR;
  706. }
  707. else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXUNDERR))
  708. {
  709. /* Clear all the static flags */
  710. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  711. hsd->ErrorCode |= HAL_SD_ERROR_TX_UNDERRUN;
  712. hsd->State = HAL_SD_STATE_READY;
  713. return HAL_ERROR;
  714. }
  715. /* Clear all the static flags */
  716. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  717. hsd->State = HAL_SD_STATE_READY;
  718. return HAL_OK;
  719. }
  720. else
  721. {
  722. hsd->ErrorCode |= HAL_SD_ERROR_BUSY;
  723. return HAL_ERROR;
  724. }
  725. }
  726. /**
  727. * @brief Reads block(s) from a specified address in a card. The Data transfer
  728. * is managed in interrupt mode.
  729. * @note This API should be followed by a check on the card state through
  730. * HAL_SD_GetCardState().
  731. * @note You could also check the IT transfer process through the SD Rx
  732. * interrupt event.
  733. * @param hsd: Pointer to SD handle
  734. * @param pData: Pointer to the buffer that will contain the received data
  735. * @param BlockAdd: Block Address from where data is to be read
  736. * @param NumberOfBlocks: Number of blocks to read.
  737. * @retval HAL status
  738. */
  739. HAL_StatusTypeDef HAL_SD_ReadBlocks_IT(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
  740. {
  741. SDMMC_DataInitTypeDef config;
  742. uint32_t errorstate = HAL_SD_ERROR_NONE;
  743. if(NULL == pData)
  744. {
  745. hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
  746. return HAL_ERROR;
  747. }
  748. if(hsd->State == HAL_SD_STATE_READY)
  749. {
  750. hsd->ErrorCode = HAL_DMA_ERROR_NONE;
  751. if((BlockAdd + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
  752. {
  753. hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
  754. return HAL_ERROR;
  755. }
  756. hsd->State = HAL_SD_STATE_BUSY;
  757. /* Initialize data control register */
  758. hsd->Instance->DCTRL = 0U;
  759. hsd->pRxBuffPtr = (uint32_t *)pData;
  760. hsd->RxXferSize = BLOCKSIZE * NumberOfBlocks;
  761. __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND | SDMMC_FLAG_RXFIFOHF));
  762. if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
  763. {
  764. BlockAdd *= 512U;
  765. }
  766. /* Configure the SD DPSM (Data Path State Machine) */
  767. config.DataTimeOut = SDMMC_DATATIMEOUT;
  768. config.DataLength = BLOCKSIZE * NumberOfBlocks;
  769. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
  770. config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
  771. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  772. config.DPSM = SDMMC_DPSM_ENABLE;
  773. SDMMC_ConfigData(hsd->Instance, &config);
  774. /* Set Block Size for Card */
  775. errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
  776. if(errorstate != HAL_SD_ERROR_NONE)
  777. {
  778. /* Clear all the static flags */
  779. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  780. hsd->ErrorCode |= errorstate;
  781. hsd->State = HAL_SD_STATE_READY;
  782. return HAL_ERROR;
  783. }
  784. /* Read Blocks in IT mode */
  785. if(NumberOfBlocks > 1U)
  786. {
  787. hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_IT);
  788. /* Read Multi Block command */
  789. errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, BlockAdd);
  790. }
  791. else
  792. {
  793. hsd->Context = (SD_CONTEXT_READ_SINGLE_BLOCK | SD_CONTEXT_IT);
  794. /* Read Single Block command */
  795. errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, BlockAdd);
  796. }
  797. if(errorstate != HAL_SD_ERROR_NONE)
  798. {
  799. /* Clear all the static flags */
  800. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  801. hsd->ErrorCode |= errorstate;
  802. hsd->State = HAL_SD_STATE_READY;
  803. return HAL_ERROR;
  804. }
  805. return HAL_OK;
  806. }
  807. else
  808. {
  809. return HAL_BUSY;
  810. }
  811. }
  812. /**
  813. * @brief Writes block(s) to a specified address in a card. The Data transfer
  814. * is managed in interrupt mode.
  815. * @note This API should be followed by a check on the card state through
  816. * HAL_SD_GetCardState().
  817. * @note You could also check the IT transfer process through the SD Tx
  818. * interrupt event.
  819. * @param hsd: Pointer to SD handle
  820. * @param pData: Pointer to the buffer that will contain the data to transmit
  821. * @param BlockAdd: Block Address where data will be written
  822. * @param NumberOfBlocks: Number of blocks to write
  823. * @retval HAL status
  824. */
  825. HAL_StatusTypeDef HAL_SD_WriteBlocks_IT(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
  826. {
  827. SDMMC_DataInitTypeDef config;
  828. uint32_t errorstate = HAL_SD_ERROR_NONE;
  829. if(NULL == pData)
  830. {
  831. hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
  832. return HAL_ERROR;
  833. }
  834. if(hsd->State == HAL_SD_STATE_READY)
  835. {
  836. hsd->ErrorCode = HAL_DMA_ERROR_NONE;
  837. if((BlockAdd + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
  838. {
  839. hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
  840. return HAL_ERROR;
  841. }
  842. hsd->State = HAL_SD_STATE_BUSY;
  843. /* Initialize data control register */
  844. hsd->Instance->DCTRL = 0U;
  845. hsd->pTxBuffPtr = (uint32_t *)pData;
  846. hsd->TxXferSize = BLOCKSIZE * NumberOfBlocks;
  847. /* Enable transfer interrupts */
  848. __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR | SDMMC_IT_DATAEND | SDMMC_FLAG_TXFIFOHE));
  849. if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
  850. {
  851. BlockAdd *= 512U;
  852. }
  853. /* Set Block Size for Card */
  854. errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
  855. if(errorstate != HAL_SD_ERROR_NONE)
  856. {
  857. /* Clear all the static flags */
  858. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  859. hsd->ErrorCode |= errorstate;
  860. hsd->State = HAL_SD_STATE_READY;
  861. return HAL_ERROR;
  862. }
  863. /* Write Blocks in Polling mode */
  864. if(NumberOfBlocks > 1U)
  865. {
  866. hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK| SD_CONTEXT_IT);
  867. /* Write Multi Block command */
  868. errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, BlockAdd);
  869. }
  870. else
  871. {
  872. hsd->Context = (SD_CONTEXT_WRITE_SINGLE_BLOCK | SD_CONTEXT_IT);
  873. /* Write Single Block command */
  874. errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, BlockAdd);
  875. }
  876. if(errorstate != HAL_SD_ERROR_NONE)
  877. {
  878. /* Clear all the static flags */
  879. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  880. hsd->ErrorCode |= errorstate;
  881. hsd->State = HAL_SD_STATE_READY;
  882. return HAL_ERROR;
  883. }
  884. /* Configure the SD DPSM (Data Path State Machine) */
  885. config.DataTimeOut = SDMMC_DATATIMEOUT;
  886. config.DataLength = BLOCKSIZE * NumberOfBlocks;
  887. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
  888. config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
  889. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  890. config.DPSM = SDMMC_DPSM_ENABLE;
  891. SDMMC_ConfigData(hsd->Instance, &config);
  892. return HAL_OK;
  893. }
  894. else
  895. {
  896. return HAL_BUSY;
  897. }
  898. }
  899. /**
  900. * @brief Reads block(s) from a specified address in a card. The Data transfer
  901. * is managed by DMA mode.
  902. * @note This API should be followed by a check on the card state through
  903. * HAL_SD_GetCardState().
  904. * @note You could also check the DMA transfer process through the SD Rx
  905. * interrupt event.
  906. * @param hsd: Pointer SD handle
  907. * @param pData: Pointer to the buffer that will contain the received data
  908. * @param BlockAdd: Block Address from where data is to be read
  909. * @param NumberOfBlocks: Number of blocks to read.
  910. * @retval HAL status
  911. */
  912. HAL_StatusTypeDef HAL_SD_ReadBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
  913. {
  914. SDMMC_DataInitTypeDef config;
  915. uint32_t errorstate = HAL_SD_ERROR_NONE;
  916. if(NULL == pData)
  917. {
  918. hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
  919. return HAL_ERROR;
  920. }
  921. if(hsd->State == HAL_SD_STATE_READY)
  922. {
  923. hsd->ErrorCode = HAL_DMA_ERROR_NONE;
  924. if((BlockAdd + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
  925. {
  926. hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
  927. return HAL_ERROR;
  928. }
  929. hsd->State = HAL_SD_STATE_BUSY;
  930. /* Initialize data control register */
  931. hsd->Instance->DCTRL = 0U;
  932. __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND));
  933. /* Set the DMA transfer complete callback */
  934. hsd->hdmarx->XferCpltCallback = SD_DMAReceiveCplt;
  935. /* Set the DMA error callback */
  936. hsd->hdmarx->XferErrorCallback = SD_DMAError;
  937. /* Set the DMA Abort callback */
  938. hsd->hdmarx->XferAbortCallback = NULL;
  939. /* Enable the DMA Channel */
  940. HAL_DMA_Start_IT(hsd->hdmarx, (uint32_t)&hsd->Instance->FIFO, (uint32_t)pData, (uint32_t)(BLOCKSIZE * NumberOfBlocks)/4);
  941. /* Enable SD DMA transfer */
  942. __HAL_SD_DMA_ENABLE(hsd);
  943. if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
  944. {
  945. BlockAdd *= 512U;
  946. }
  947. /* Configure the SD DPSM (Data Path State Machine) */
  948. config.DataTimeOut = SDMMC_DATATIMEOUT;
  949. config.DataLength = BLOCKSIZE * NumberOfBlocks;
  950. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
  951. config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
  952. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  953. config.DPSM = SDMMC_DPSM_ENABLE;
  954. SDMMC_ConfigData(hsd->Instance, &config);
  955. /* Set Block Size for Card */
  956. errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
  957. if(errorstate != HAL_SD_ERROR_NONE)
  958. {
  959. /* Clear all the static flags */
  960. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  961. hsd->ErrorCode |= errorstate;
  962. hsd->State = HAL_SD_STATE_READY;
  963. return HAL_ERROR;
  964. }
  965. /* Read Blocks in DMA mode */
  966. if(NumberOfBlocks > 1U)
  967. {
  968. hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_DMA);
  969. /* Read Multi Block command */
  970. errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, BlockAdd);
  971. }
  972. else
  973. {
  974. hsd->Context = (SD_CONTEXT_READ_SINGLE_BLOCK | SD_CONTEXT_DMA);
  975. /* Read Single Block command */
  976. errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, BlockAdd);
  977. }
  978. if(errorstate != HAL_SD_ERROR_NONE)
  979. {
  980. /* Clear all the static flags */
  981. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  982. hsd->ErrorCode |= errorstate;
  983. hsd->State = HAL_SD_STATE_READY;
  984. return HAL_ERROR;
  985. }
  986. return HAL_OK;
  987. }
  988. else
  989. {
  990. return HAL_BUSY;
  991. }
  992. }
  993. /**
  994. * @brief Writes block(s) to a specified address in a card. The Data transfer
  995. * is managed by DMA mode.
  996. * @note This API should be followed by a check on the card state through
  997. * HAL_SD_GetCardState().
  998. * @note You could also check the DMA transfer process through the SD Tx
  999. * interrupt event.
  1000. * @param hsd: Pointer to SD handle
  1001. * @param pData: Pointer to the buffer that will contain the data to transmit
  1002. * @param BlockAdd: Block Address where data will be written
  1003. * @param NumberOfBlocks: Number of blocks to write
  1004. * @retval HAL status
  1005. */
  1006. HAL_StatusTypeDef HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
  1007. {
  1008. SDMMC_DataInitTypeDef config;
  1009. uint32_t errorstate = HAL_SD_ERROR_NONE;
  1010. if(NULL == pData)
  1011. {
  1012. hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
  1013. return HAL_ERROR;
  1014. }
  1015. if(hsd->State == HAL_SD_STATE_READY)
  1016. {
  1017. hsd->ErrorCode = HAL_DMA_ERROR_NONE;
  1018. if((BlockAdd + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
  1019. {
  1020. hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
  1021. return HAL_ERROR;
  1022. }
  1023. hsd->State = HAL_SD_STATE_BUSY;
  1024. /* Initialize data control register */
  1025. hsd->Instance->DCTRL = 0U;
  1026. /* Enable SD Error interrupts */
  1027. __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR));
  1028. /* Set the DMA transfer complete callback */
  1029. hsd->hdmatx->XferCpltCallback = SD_DMATransmitCplt;
  1030. /* Set the DMA error callback */
  1031. hsd->hdmatx->XferErrorCallback = SD_DMAError;
  1032. /* Set the DMA Abort callback */
  1033. hsd->hdmatx->XferAbortCallback = NULL;
  1034. if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
  1035. {
  1036. BlockAdd *= 512U;
  1037. }
  1038. /* Set Block Size for Card */
  1039. errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
  1040. if(errorstate != HAL_SD_ERROR_NONE)
  1041. {
  1042. /* Clear all the static flags */
  1043. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  1044. hsd->ErrorCode |= errorstate;
  1045. hsd->State = HAL_SD_STATE_READY;
  1046. return HAL_ERROR;
  1047. }
  1048. /* Write Blocks in Polling mode */
  1049. if(NumberOfBlocks > 1U)
  1050. {
  1051. hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK | SD_CONTEXT_DMA);
  1052. /* Write Multi Block command */
  1053. errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, BlockAdd);
  1054. }
  1055. else
  1056. {
  1057. hsd->Context = (SD_CONTEXT_WRITE_SINGLE_BLOCK | SD_CONTEXT_DMA);
  1058. /* Write Single Block command */
  1059. errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, BlockAdd);
  1060. }
  1061. if(errorstate != HAL_SD_ERROR_NONE)
  1062. {
  1063. /* Clear all the static flags */
  1064. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  1065. hsd->ErrorCode |= errorstate;
  1066. hsd->State = HAL_SD_STATE_READY;
  1067. return HAL_ERROR;
  1068. }
  1069. /* Enable SDMMC DMA transfer */
  1070. __HAL_SD_DMA_ENABLE(hsd);
  1071. /* Enable the DMA Channel */
  1072. HAL_DMA_Start_IT(hsd->hdmatx, (uint32_t)pData, (uint32_t)&hsd->Instance->FIFO, (uint32_t)(BLOCKSIZE * NumberOfBlocks)/4);
  1073. /* Configure the SD DPSM (Data Path State Machine) */
  1074. config.DataTimeOut = SDMMC_DATATIMEOUT;
  1075. config.DataLength = BLOCKSIZE * NumberOfBlocks;
  1076. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
  1077. config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
  1078. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  1079. config.DPSM = SDMMC_DPSM_ENABLE;
  1080. SDMMC_ConfigData(hsd->Instance, &config);
  1081. return HAL_OK;
  1082. }
  1083. else
  1084. {
  1085. return HAL_BUSY;
  1086. }
  1087. }
  1088. /**
  1089. * @brief Erases the specified memory area of the given SD card.
  1090. * @note This API should be followed by a check on the card state through
  1091. * HAL_SD_GetCardState().
  1092. * @param hsd: Pointer to SD handle
  1093. * @param BlockStartAdd: Start Block address
  1094. * @param BlockEndAdd: End Block address
  1095. * @retval HAL status
  1096. */
  1097. HAL_StatusTypeDef HAL_SD_Erase(SD_HandleTypeDef *hsd, uint32_t BlockStartAdd, uint32_t BlockEndAdd)
  1098. {
  1099. uint32_t errorstate = HAL_SD_ERROR_NONE;
  1100. if(hsd->State == HAL_SD_STATE_READY)
  1101. {
  1102. hsd->ErrorCode = HAL_DMA_ERROR_NONE;
  1103. if(BlockEndAdd < BlockStartAdd)
  1104. {
  1105. hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
  1106. return HAL_ERROR;
  1107. }
  1108. if(BlockEndAdd > (hsd->SdCard.LogBlockNbr))
  1109. {
  1110. hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
  1111. return HAL_ERROR;
  1112. }
  1113. hsd->State = HAL_SD_STATE_BUSY;
  1114. /* Check if the card command class supports erase command */
  1115. if(((hsd->SdCard.Class) & SDMMC_CCCC_ERASE) == 0U)
  1116. {
  1117. /* Clear all the static flags */
  1118. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  1119. hsd->ErrorCode |= HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
  1120. hsd->State = HAL_SD_STATE_READY;
  1121. return HAL_ERROR;
  1122. }
  1123. if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
  1124. {
  1125. /* Clear all the static flags */
  1126. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  1127. hsd->ErrorCode |= HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
  1128. hsd->State = HAL_SD_STATE_READY;
  1129. return HAL_ERROR;
  1130. }
  1131. /* Get start and end block for high capacity cards */
  1132. if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
  1133. {
  1134. BlockStartAdd *= 512U;
  1135. BlockEndAdd *= 512U;
  1136. }
  1137. /* According to sd-card spec 1.0 ERASE_GROUP_START (CMD32) and erase_group_end(CMD33) */
  1138. if(hsd->SdCard.CardType != CARD_SECURED)
  1139. {
  1140. /* Send CMD32 SD_ERASE_GRP_START with argument as addr */
  1141. errorstate = SDMMC_CmdSDEraseStartAdd(hsd->Instance, BlockStartAdd);
  1142. if(errorstate != HAL_SD_ERROR_NONE)
  1143. {
  1144. /* Clear all the static flags */
  1145. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  1146. hsd->ErrorCode |= errorstate;
  1147. hsd->State = HAL_SD_STATE_READY;
  1148. return HAL_ERROR;
  1149. }
  1150. /* Send CMD33 SD_ERASE_GRP_END with argument as addr */
  1151. errorstate = SDMMC_CmdSDEraseEndAdd(hsd->Instance, BlockEndAdd);
  1152. if(errorstate != HAL_SD_ERROR_NONE)
  1153. {
  1154. /* Clear all the static flags */
  1155. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  1156. hsd->ErrorCode |= errorstate;
  1157. hsd->State = HAL_SD_STATE_READY;
  1158. return HAL_ERROR;
  1159. }
  1160. }
  1161. /* Send CMD38 ERASE */
  1162. errorstate = SDMMC_CmdErase(hsd->Instance);
  1163. if(errorstate != HAL_SD_ERROR_NONE)
  1164. {
  1165. /* Clear all the static flags */
  1166. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  1167. hsd->ErrorCode |= errorstate;
  1168. hsd->State = HAL_SD_STATE_READY;
  1169. return HAL_ERROR;
  1170. }
  1171. hsd->State = HAL_SD_STATE_READY;
  1172. return HAL_OK;
  1173. }
  1174. else
  1175. {
  1176. return HAL_BUSY;
  1177. }
  1178. }
  1179. /**
  1180. * @brief This function handles SD card interrupt request.
  1181. * @param hsd: Pointer to SD handle
  1182. * @retval None
  1183. */
  1184. void HAL_SD_IRQHandler(SD_HandleTypeDef *hsd)
  1185. {
  1186. uint32_t errorstate = HAL_SD_ERROR_NONE;
  1187. /* Check for SDMMC interrupt flags */
  1188. if(__HAL_SD_GET_FLAG(hsd, SDMMC_IT_DATAEND) != RESET)
  1189. {
  1190. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DATAEND);
  1191. __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT|\
  1192. SDMMC_IT_TXUNDERR| SDMMC_IT_RXOVERR);
  1193. if((hsd->Context & SD_CONTEXT_IT) != RESET)
  1194. {
  1195. if(((hsd->Context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != RESET) || ((hsd->Context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != RESET))
  1196. {
  1197. errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
  1198. if(errorstate != HAL_SD_ERROR_NONE)
  1199. {
  1200. hsd->ErrorCode |= errorstate;
  1201. HAL_SD_ErrorCallback(hsd);
  1202. }
  1203. }
  1204. /* Clear all the static flags */
  1205. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  1206. hsd->State = HAL_SD_STATE_READY;
  1207. if(((hsd->Context & SD_CONTEXT_READ_SINGLE_BLOCK) != RESET) || ((hsd->Context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != RESET))
  1208. {
  1209. HAL_SD_RxCpltCallback(hsd);
  1210. }
  1211. else
  1212. {
  1213. HAL_SD_TxCpltCallback(hsd);
  1214. }
  1215. }
  1216. else if((hsd->Context & SD_CONTEXT_DMA) != RESET)
  1217. {
  1218. if((hsd->Context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != RESET)
  1219. {
  1220. errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
  1221. if(errorstate != HAL_SD_ERROR_NONE)
  1222. {
  1223. hsd->ErrorCode |= errorstate;
  1224. HAL_SD_ErrorCallback(hsd);
  1225. }
  1226. }
  1227. if(((hsd->Context & SD_CONTEXT_READ_SINGLE_BLOCK) == RESET) && ((hsd->Context & SD_CONTEXT_READ_MULTIPLE_BLOCK) == RESET))
  1228. {
  1229. /* Disable the DMA transfer for transmit request by setting the DMAEN bit
  1230. in the SD DCTRL register */
  1231. hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDMMC_DCTRL_DMAEN);
  1232. hsd->State = HAL_SD_STATE_READY;
  1233. HAL_SD_TxCpltCallback(hsd);
  1234. }
  1235. }
  1236. }
  1237. else if(__HAL_SD_GET_FLAG(hsd, SDMMC_IT_TXFIFOHE) != RESET)
  1238. {
  1239. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_TXFIFOHE);
  1240. SD_Write_IT(hsd);
  1241. }
  1242. else if(__HAL_SD_GET_FLAG(hsd, SDMMC_IT_RXFIFOHF) != RESET)
  1243. {
  1244. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_RXFIFOHF);
  1245. SD_Read_IT(hsd);
  1246. }
  1247. else if(__HAL_SD_GET_FLAG(hsd, SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_TXUNDERR) != RESET)
  1248. {
  1249. /* Set Error code */
  1250. if(__HAL_SD_GET_FLAG(hsd, SDMMC_IT_DCRCFAIL) != RESET)
  1251. {
  1252. hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
  1253. }
  1254. if(__HAL_SD_GET_FLAG(hsd, SDMMC_IT_DTIMEOUT) != RESET)
  1255. {
  1256. hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
  1257. }
  1258. if(__HAL_SD_GET_FLAG(hsd, SDMMC_IT_RXOVERR) != RESET)
  1259. {
  1260. hsd->ErrorCode |= HAL_SD_ERROR_RX_OVERRUN;
  1261. }
  1262. if(__HAL_SD_GET_FLAG(hsd, SDMMC_IT_TXUNDERR) != RESET)
  1263. {
  1264. hsd->ErrorCode |= HAL_SD_ERROR_TX_UNDERRUN;
  1265. }
  1266. /* Clear All flags */
  1267. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  1268. /* Disable all interrupts */
  1269. __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT|\
  1270. SDMMC_IT_TXUNDERR| SDMMC_IT_RXOVERR);
  1271. if((hsd->Context & SD_CONTEXT_DMA) != RESET)
  1272. {
  1273. /* Abort the SD DMA Streams */
  1274. if(hsd->hdmatx != NULL)
  1275. {
  1276. /* Set the DMA Tx abort callback */
  1277. hsd->hdmatx->XferAbortCallback = SD_DMATxAbort;
  1278. /* Abort DMA in IT mode */
  1279. if(HAL_DMA_Abort_IT(hsd->hdmatx) != HAL_OK)
  1280. {
  1281. SD_DMATxAbort(hsd->hdmatx);
  1282. }
  1283. }
  1284. else if(hsd->hdmarx != NULL)
  1285. {
  1286. /* Set the DMA Rx abort callback */
  1287. hsd->hdmarx->XferAbortCallback = SD_DMARxAbort;
  1288. /* Abort DMA in IT mode */
  1289. if(HAL_DMA_Abort_IT(hsd->hdmarx) != HAL_OK)
  1290. {
  1291. SD_DMARxAbort(hsd->hdmarx);
  1292. }
  1293. }
  1294. else
  1295. {
  1296. hsd->ErrorCode = HAL_SD_ERROR_NONE;
  1297. hsd->State = HAL_SD_STATE_READY;
  1298. HAL_SD_AbortCallback(hsd);
  1299. }
  1300. }
  1301. else if((hsd->Context & SD_CONTEXT_IT) != RESET)
  1302. {
  1303. /* Set the SD state to ready to be able to start again the process */
  1304. hsd->State = HAL_SD_STATE_READY;
  1305. HAL_SD_ErrorCallback(hsd);
  1306. }
  1307. }
  1308. }
  1309. /**
  1310. * @brief return the SD state
  1311. * @param hsd: Pointer to sd handle
  1312. * @retval HAL state
  1313. */
  1314. HAL_SD_StateTypeDef HAL_SD_GetState(SD_HandleTypeDef *hsd)
  1315. {
  1316. return hsd->State;
  1317. }
  1318. /**
  1319. * @brief Return the SD error code
  1320. * @param hsd : Pointer to a SD_HandleTypeDef structure that contains
  1321. * the configuration information.
  1322. * @retval SD Error Code
  1323. */
  1324. uint32_t HAL_SD_GetError(SD_HandleTypeDef *hsd)
  1325. {
  1326. return hsd->ErrorCode;
  1327. }
  1328. /**
  1329. * @brief Tx Transfer completed callbacks
  1330. * @param hsd: Pointer to SD handle
  1331. * @retval None
  1332. */
  1333. __weak void HAL_SD_TxCpltCallback(SD_HandleTypeDef *hsd)
  1334. {
  1335. /* Prevent unused argument(s) compilation warning */
  1336. UNUSED(hsd);
  1337. /* NOTE : This function should not be modified, when the callback is needed,
  1338. the HAL_SD_TxCpltCallback can be implemented in the user file
  1339. */
  1340. }
  1341. /**
  1342. * @brief Rx Transfer completed callbacks
  1343. * @param hsd: Pointer SD handle
  1344. * @retval None
  1345. */
  1346. __weak void HAL_SD_RxCpltCallback(SD_HandleTypeDef *hsd)
  1347. {
  1348. /* Prevent unused argument(s) compilation warning */
  1349. UNUSED(hsd);
  1350. /* NOTE : This function should not be modified, when the callback is needed,
  1351. the HAL_SD_RxCpltCallback can be implemented in the user file
  1352. */
  1353. }
  1354. /**
  1355. * @brief SD error callbacks
  1356. * @param hsd: Pointer SD handle
  1357. * @retval None
  1358. */
  1359. __weak void HAL_SD_ErrorCallback(SD_HandleTypeDef *hsd)
  1360. {
  1361. /* Prevent unused argument(s) compilation warning */
  1362. UNUSED(hsd);
  1363. /* NOTE : This function should not be modified, when the callback is needed,
  1364. the HAL_SD_ErrorCallback can be implemented in the user file
  1365. */
  1366. }
  1367. /**
  1368. * @brief SD Abort callbacks
  1369. * @param hsd: Pointer SD handle
  1370. * @retval None
  1371. */
  1372. __weak void HAL_SD_AbortCallback(SD_HandleTypeDef *hsd)
  1373. {
  1374. /* Prevent unused argument(s) compilation warning */
  1375. UNUSED(hsd);
  1376. /* NOTE : This function should not be modified, when the callback is needed,
  1377. the HAL_SD_ErrorCallback can be implemented in the user file
  1378. */
  1379. }
  1380. /**
  1381. * @}
  1382. */
  1383. /** @addtogroup SD_Exported_Functions_Group3
  1384. * @brief management functions
  1385. *
  1386. @verbatim
  1387. ==============================================================================
  1388. ##### Peripheral Control functions #####
  1389. ==============================================================================
  1390. [..]
  1391. This subsection provides a set of functions allowing to control the SD card
  1392. operations and get the related information
  1393. @endverbatim
  1394. * @{
  1395. */
  1396. /**
  1397. * @brief Returns information the information of the card which are stored on
  1398. * the CID register.
  1399. * @param hsd: Pointer to SD handle
  1400. * @param pCID: Pointer to a HAL_SD_CardCIDTypeDef structure that
  1401. * contains all CID register parameters
  1402. * @retval HAL status
  1403. */
  1404. HAL_StatusTypeDef HAL_SD_GetCardCID(SD_HandleTypeDef *hsd, HAL_SD_CardCIDTypeDef *pCID)
  1405. {
  1406. uint32_t tmp = 0;
  1407. /* Byte 0 */
  1408. tmp = (uint8_t)((hsd->CID[0] & 0xFF000000U) >> 24);
  1409. pCID->ManufacturerID = tmp;
  1410. /* Byte 1 */
  1411. tmp = (uint8_t)((hsd->CID[0] & 0x00FF0000) >> 16);
  1412. pCID->OEM_AppliID = tmp << 8;
  1413. /* Byte 2 */
  1414. tmp = (uint8_t)((hsd->CID[0] & 0x000000FF00) >> 8);
  1415. pCID->OEM_AppliID |= tmp;
  1416. /* Byte 3 */
  1417. tmp = (uint8_t)(hsd->CID[0] & 0x000000FF);
  1418. pCID->ProdName1 = tmp << 24;
  1419. /* Byte 4 */
  1420. tmp = (uint8_t)((hsd->CID[1] & 0xFF000000U) >> 24);
  1421. pCID->ProdName1 |= tmp << 16;
  1422. /* Byte 5 */
  1423. tmp = (uint8_t)((hsd->CID[1] & 0x00FF0000) >> 16);
  1424. pCID->ProdName1 |= tmp << 8;
  1425. /* Byte 6 */
  1426. tmp = (uint8_t)((hsd->CID[1] & 0x0000FF00) >> 8);
  1427. pCID->ProdName1 |= tmp;
  1428. /* Byte 7 */
  1429. tmp = (uint8_t)(hsd->CID[1] & 0x000000FF);
  1430. pCID->ProdName2 = tmp;
  1431. /* Byte 8 */
  1432. tmp = (uint8_t)((hsd->CID[2] & 0xFF000000U) >> 24);
  1433. pCID->ProdRev = tmp;
  1434. /* Byte 9 */
  1435. tmp = (uint8_t)((hsd->CID[2] & 0x00FF0000) >> 16);
  1436. pCID->ProdSN = tmp << 24;
  1437. /* Byte 10 */
  1438. tmp = (uint8_t)((hsd->CID[2] & 0x0000FF00) >> 8);
  1439. pCID->ProdSN |= tmp << 16;
  1440. /* Byte 11 */
  1441. tmp = (uint8_t)(hsd->CID[2] & 0x000000FF);
  1442. pCID->ProdSN |= tmp << 8;
  1443. /* Byte 12 */
  1444. tmp = (uint8_t)((hsd->CID[3] & 0xFF000000U) >> 24);
  1445. pCID->ProdSN |= tmp;
  1446. /* Byte 13 */
  1447. tmp = (uint8_t)((hsd->CID[3] & 0x00FF0000) >> 16);
  1448. pCID->Reserved1 |= (tmp & 0xF0) >> 4;
  1449. pCID->ManufactDate = (tmp & 0x0F) << 8;
  1450. /* Byte 14 */
  1451. tmp = (uint8_t)((hsd->CID[3] & 0x0000FF00) >> 8);
  1452. pCID->ManufactDate |= tmp;
  1453. /* Byte 15 */
  1454. tmp = (uint8_t)(hsd->CID[3] & 0x000000FF);
  1455. pCID->CID_CRC = (tmp & 0xFE) >> 1;
  1456. pCID->Reserved2 = 1;
  1457. return HAL_OK;
  1458. }
  1459. /**
  1460. * @brief Returns information the information of the card which are stored on
  1461. * the CSD register.
  1462. * @param hsd: Pointer to SD handle
  1463. * @param pCSD: Pointer to a HAL_SD_CardCSDTypeDef structure that
  1464. * contains all CSD register parameters
  1465. * @retval HAL status
  1466. */
  1467. HAL_StatusTypeDef HAL_SD_GetCardCSD(SD_HandleTypeDef *hsd, HAL_SD_CardCSDTypeDef *pCSD)
  1468. {
  1469. uint32_t tmp = 0;
  1470. /* Byte 0 */
  1471. tmp = (hsd->CSD[0] & 0xFF000000U) >> 24;
  1472. pCSD->CSDStruct = (uint8_t)((tmp & 0xC0) >> 6);
  1473. pCSD->SysSpecVersion = (uint8_t)((tmp & 0x3C) >> 2);
  1474. pCSD->Reserved1 = tmp & 0x03;
  1475. /* Byte 1 */
  1476. tmp = (hsd->CSD[0] & 0x00FF0000) >> 16;
  1477. pCSD->TAAC = (uint8_t)tmp;
  1478. /* Byte 2 */
  1479. tmp = (hsd->CSD[0] & 0x0000FF00) >> 8;
  1480. pCSD->NSAC = (uint8_t)tmp;
  1481. /* Byte 3 */
  1482. tmp = hsd->CSD[0] & 0x000000FF;
  1483. pCSD->MaxBusClkFrec = (uint8_t)tmp;
  1484. /* Byte 4 */
  1485. tmp = (hsd->CSD[1] & 0xFF000000U) >> 24;
  1486. pCSD->CardComdClasses = (uint16_t)(tmp << 4);
  1487. /* Byte 5 */
  1488. tmp = (hsd->CSD[1] & 0x00FF0000U) >> 16;
  1489. pCSD->CardComdClasses |= (uint16_t)((tmp & 0xF0) >> 4);
  1490. pCSD->RdBlockLen = (uint8_t)(tmp & 0x0F);
  1491. /* Byte 6 */
  1492. tmp = (hsd->CSD[1] & 0x0000FF00U) >> 8;
  1493. pCSD->PartBlockRead = (uint8_t)((tmp & 0x80) >> 7);
  1494. pCSD->WrBlockMisalign = (uint8_t)((tmp & 0x40) >> 6);
  1495. pCSD->RdBlockMisalign = (uint8_t)((tmp & 0x20) >> 5);
  1496. pCSD->DSRImpl = (uint8_t)((tmp & 0x10) >> 4);
  1497. pCSD->Reserved2 = 0; /*!< Reserved */
  1498. if(hsd->SdCard.CardType == CARD_SDSC)
  1499. {
  1500. pCSD->DeviceSize = (tmp & 0x03) << 10;
  1501. /* Byte 7 */
  1502. tmp = (uint8_t)(hsd->CSD[1] & 0x000000FFU);
  1503. pCSD->DeviceSize |= (tmp) << 2;
  1504. /* Byte 8 */
  1505. tmp = (uint8_t)((hsd->CSD[2] & 0xFF000000U) >> 24);
  1506. pCSD->DeviceSize |= (tmp & 0xC0) >> 6;
  1507. pCSD->MaxRdCurrentVDDMin = (tmp & 0x38) >> 3;
  1508. pCSD->MaxRdCurrentVDDMax = (tmp & 0x07);
  1509. /* Byte 9 */
  1510. tmp = (uint8_t)((hsd->CSD[2] & 0x00FF0000U) >> 16);
  1511. pCSD->MaxWrCurrentVDDMin = (tmp & 0xE0) >> 5;
  1512. pCSD->MaxWrCurrentVDDMax = (tmp & 0x1C) >> 2;
  1513. pCSD->DeviceSizeMul = (tmp & 0x03) << 1;
  1514. /* Byte 10 */
  1515. tmp = (uint8_t)((hsd->CSD[2] & 0x0000FF00U) >> 8);
  1516. pCSD->DeviceSizeMul |= (tmp & 0x80) >> 7;
  1517. hsd->SdCard.BlockNbr = (pCSD->DeviceSize + 1) ;
  1518. hsd->SdCard.BlockNbr *= (1 << (pCSD->DeviceSizeMul + 2));
  1519. hsd->SdCard.BlockSize = 1 << (pCSD->RdBlockLen);
  1520. hsd->SdCard.LogBlockNbr = (hsd->SdCard.BlockNbr) * ((hsd->SdCard.BlockSize) / 512);
  1521. hsd->SdCard.LogBlockSize = 512;
  1522. }
  1523. else if(hsd->SdCard.CardType == CARD_SDHC_SDXC)
  1524. {
  1525. /* Byte 7 */
  1526. tmp = (uint8_t)(hsd->CSD[1] & 0x000000FFU);
  1527. pCSD->DeviceSize = (tmp & 0x3F) << 16;
  1528. /* Byte 8 */
  1529. tmp = (uint8_t)((hsd->CSD[2] & 0xFF000000U) >> 24);
  1530. pCSD->DeviceSize |= (tmp << 8);
  1531. /* Byte 9 */
  1532. tmp = (uint8_t)((hsd->CSD[2] & 0x00FF0000U) >> 16);
  1533. pCSD->DeviceSize |= (tmp);
  1534. /* Byte 10 */
  1535. tmp = (uint8_t)((hsd->CSD[2] & 0x0000FF00U) >> 8);
  1536. hsd->SdCard.LogBlockNbr = hsd->SdCard.BlockNbr = (((uint64_t)pCSD->DeviceSize + 1) * 1024);
  1537. hsd->SdCard.LogBlockSize = hsd->SdCard.BlockSize = 512;
  1538. }
  1539. else
  1540. {
  1541. /* Clear all the static flags */
  1542. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  1543. hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
  1544. hsd->State = HAL_SD_STATE_READY;
  1545. return HAL_ERROR;
  1546. }
  1547. pCSD->EraseGrSize = (tmp & 0x40) >> 6;
  1548. pCSD->EraseGrMul = (tmp & 0x3F) << 1;
  1549. /* Byte 11 */
  1550. tmp = (uint8_t)(hsd->CSD[2] & 0x000000FF);
  1551. pCSD->EraseGrMul |= (tmp & 0x80) >> 7;
  1552. pCSD->WrProtectGrSize = (tmp & 0x7F);
  1553. /* Byte 12 */
  1554. tmp = (uint8_t)((hsd->CSD[3] & 0xFF000000U) >> 24);
  1555. pCSD->WrProtectGrEnable = (tmp & 0x80) >> 7;
  1556. pCSD->ManDeflECC = (tmp & 0x60) >> 5;
  1557. pCSD->WrSpeedFact = (tmp & 0x1C) >> 2;
  1558. pCSD->MaxWrBlockLen = (tmp & 0x03) << 2;
  1559. /* Byte 13 */
  1560. tmp = (uint8_t)((hsd->CSD[3] & 0x00FF0000) >> 16);
  1561. pCSD->MaxWrBlockLen |= (tmp & 0xC0) >> 6;
  1562. pCSD->WriteBlockPaPartial = (tmp & 0x20) >> 5;
  1563. pCSD->Reserved3 = 0;
  1564. pCSD->ContentProtectAppli = (tmp & 0x01);
  1565. /* Byte 14 */
  1566. tmp = (uint8_t)((hsd->CSD[3] & 0x0000FF00) >> 8);
  1567. pCSD->FileFormatGrouop = (tmp & 0x80) >> 7;
  1568. pCSD->CopyFlag = (tmp & 0x40) >> 6;
  1569. pCSD->PermWrProtect = (tmp & 0x20) >> 5;
  1570. pCSD->TempWrProtect = (tmp & 0x10) >> 4;
  1571. pCSD->FileFormat = (tmp & 0x0C) >> 2;
  1572. pCSD->ECC = (tmp & 0x03);
  1573. /* Byte 15 */
  1574. tmp = (uint8_t)(hsd->CSD[3] & 0x000000FF);
  1575. pCSD->CSD_CRC = (tmp & 0xFE) >> 1;
  1576. pCSD->Reserved4 = 1;
  1577. return HAL_OK;
  1578. }
  1579. /**
  1580. * @brief Gets the SD status info.
  1581. * @param hsd: Pointer to SD handle
  1582. * @param pStatus: Pointer to the HAL_SD_CardStatusTypeDef structure that
  1583. * will contain the SD card status information
  1584. * @retval HAL status
  1585. */
  1586. HAL_StatusTypeDef HAL_SD_GetCardStatus(SD_HandleTypeDef *hsd, HAL_SD_CardStatusTypeDef *pStatus)
  1587. {
  1588. uint32_t tmp = 0;
  1589. uint32_t sd_status[16];
  1590. uint32_t errorstate = HAL_SD_ERROR_NONE;
  1591. errorstate = SD_SendSDStatus(hsd, sd_status);
  1592. if(errorstate != HAL_OK)
  1593. {
  1594. /* Clear all the static flags */
  1595. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  1596. hsd->ErrorCode |= errorstate;
  1597. hsd->State = HAL_SD_STATE_READY;
  1598. return HAL_ERROR;
  1599. }
  1600. else
  1601. {
  1602. /* Byte 0 */
  1603. tmp = (sd_status[0] & 0xC0) >> 6;
  1604. pStatus->DataBusWidth = (uint8_t)tmp;
  1605. /* Byte 0 */
  1606. tmp = (sd_status[0] & 0x20) >> 5;
  1607. pStatus->SecuredMode = (uint8_t)tmp;
  1608. /* Byte 2 */
  1609. tmp = (sd_status[0] & 0x00FF0000U) >> 16;
  1610. pStatus->CardType = (uint16_t)(tmp << 8);
  1611. /* Byte 3 */
  1612. tmp = (sd_status[0] & 0xFF000000U) >> 24;
  1613. pStatus->CardType |= (uint16_t)tmp;
  1614. /* Byte 4 */
  1615. tmp = (sd_status[1] & 0xFF);
  1616. pStatus->ProtectedAreaSize = (uint32_t)(tmp << 24);
  1617. /* Byte 5 */
  1618. tmp = (sd_status[1] & 0xFF00) >> 8;
  1619. pStatus->ProtectedAreaSize |= (uint32_t)(tmp << 16);
  1620. /* Byte 6 */
  1621. tmp = (sd_status[1] & 0xFF0000) >> 16;
  1622. pStatus->ProtectedAreaSize |= (uint32_t)(tmp << 8);
  1623. /* Byte 7 */
  1624. tmp = (sd_status[1] & 0xFF000000U) >> 24;
  1625. pStatus->ProtectedAreaSize |= (uint32_t)tmp;
  1626. /* Byte 8 */
  1627. tmp = (sd_status[2] & 0xFF);
  1628. pStatus->SpeedClass = (uint8_t)tmp;
  1629. /* Byte 9 */
  1630. tmp = (sd_status[2] & 0xFF00) >> 8;
  1631. pStatus->PerformanceMove = (uint8_t)tmp;
  1632. /* Byte 10 */
  1633. tmp = (sd_status[2] & 0xF00000) >> 20;
  1634. pStatus->AllocationUnitSize = (uint8_t)tmp;
  1635. /* Byte 11 */
  1636. tmp = (sd_status[2] & 0xFF000000U) >> 24;
  1637. pStatus->EraseSize = (uint16_t)(tmp << 8);
  1638. /* Byte 12 */
  1639. tmp = (sd_status[3] & 0xFF);
  1640. pStatus->EraseSize |= (uint16_t)tmp;
  1641. /* Byte 13 */
  1642. tmp = (sd_status[3] & 0xFC00) >> 10;
  1643. pStatus->EraseTimeout = (uint8_t)tmp;
  1644. /* Byte 13 */
  1645. tmp = (sd_status[3] & 0x0300) >> 8;
  1646. pStatus->EraseOffset = (uint8_t)tmp;
  1647. }
  1648. return HAL_OK;
  1649. }
  1650. /**
  1651. * @brief Gets the SD card info.
  1652. * @param hsd: Pointer to SD handle
  1653. * @param pCardInfo: Pointer to the HAL_SD_CardInfoTypeDef structure that
  1654. * will contain the SD card status information
  1655. * @retval HAL status
  1656. */
  1657. HAL_StatusTypeDef HAL_SD_GetCardInfo(SD_HandleTypeDef *hsd, HAL_SD_CardInfoTypeDef *pCardInfo)
  1658. {
  1659. pCardInfo->CardType = (uint32_t)(hsd->SdCard.CardType);
  1660. pCardInfo->CardVersion = (uint32_t)(hsd->SdCard.CardVersion);
  1661. pCardInfo->Class = (uint32_t)(hsd->SdCard.Class);
  1662. pCardInfo->RelCardAdd = (uint32_t)(hsd->SdCard.RelCardAdd);
  1663. pCardInfo->BlockNbr = (uint32_t)(hsd->SdCard.BlockNbr);
  1664. pCardInfo->BlockSize = (uint32_t)(hsd->SdCard.BlockSize);
  1665. pCardInfo->LogBlockNbr = (uint32_t)(hsd->SdCard.LogBlockNbr);
  1666. pCardInfo->LogBlockSize = (uint32_t)(hsd->SdCard.LogBlockSize);
  1667. return HAL_OK;
  1668. }
  1669. /**
  1670. * @brief Enables wide bus operation for the requested card if supported by
  1671. * card.
  1672. * @param hsd: Pointer to SD handle
  1673. * @param WideMode: Specifies the SD card wide bus mode
  1674. * This parameter can be one of the following values:
  1675. * @arg SDMMC_BUS_WIDE_8B: 8-bit data transfer
  1676. * @arg SDMMC_BUS_WIDE_4B: 4-bit data transfer
  1677. * @arg SDMMC_BUS_WIDE_1B: 1-bit data transfer
  1678. * @retval HAL status
  1679. */
  1680. HAL_StatusTypeDef HAL_SD_ConfigWideBusOperation(SD_HandleTypeDef *hsd, uint32_t WideMode)
  1681. {
  1682. SDMMC_InitTypeDef Init;
  1683. uint32_t errorstate = HAL_SD_ERROR_NONE;
  1684. /* Check the parameters */
  1685. assert_param(IS_SDMMC_BUS_WIDE(WideMode));
  1686. /* Chnage Satte */
  1687. hsd->State = HAL_SD_STATE_BUSY;
  1688. if(hsd->SdCard.CardType != CARD_SECURED)
  1689. {
  1690. if(WideMode == SDMMC_BUS_WIDE_8B)
  1691. {
  1692. hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
  1693. }
  1694. else if(WideMode == SDMMC_BUS_WIDE_4B)
  1695. {
  1696. errorstate = SD_WideBus_Enable(hsd);
  1697. hsd->ErrorCode |= errorstate;
  1698. }
  1699. else if(WideMode == SDMMC_BUS_WIDE_1B)
  1700. {
  1701. errorstate = SD_WideBus_Disable(hsd);
  1702. hsd->ErrorCode |= errorstate;
  1703. }
  1704. else
  1705. {
  1706. /* WideMode is not a valid argument*/
  1707. hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
  1708. }
  1709. }
  1710. else
  1711. {
  1712. /* MMC Card does not support this feature */
  1713. hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
  1714. }
  1715. if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
  1716. {
  1717. /* Clear all the static flags */
  1718. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  1719. hsd->State = HAL_SD_STATE_READY;
  1720. return HAL_ERROR;
  1721. }
  1722. else
  1723. {
  1724. /* Configure the SDMMC peripheral */
  1725. Init.ClockEdge = hsd->Init.ClockEdge;
  1726. Init.ClockBypass = hsd->Init.ClockBypass;
  1727. Init.ClockPowerSave = hsd->Init.ClockPowerSave;
  1728. Init.BusWide = WideMode;
  1729. Init.HardwareFlowControl = hsd->Init.HardwareFlowControl;
  1730. Init.ClockDiv = hsd->Init.ClockDiv;
  1731. SDMMC_Init(hsd->Instance, Init);
  1732. }
  1733. /* Change State */
  1734. hsd->State = HAL_SD_STATE_READY;
  1735. return HAL_OK;
  1736. }
  1737. /**
  1738. * @brief Gets the current sd card data state.
  1739. * @param hsd: pointer to SD handle
  1740. * @retval Card state
  1741. */
  1742. HAL_SD_CardStateTypeDef HAL_SD_GetCardState(SD_HandleTypeDef *hsd)
  1743. {
  1744. HAL_SD_CardStateTypeDef cardstate = HAL_SD_CARD_TRANSFER;
  1745. uint32_t errorstate = HAL_SD_ERROR_NONE;
  1746. uint32_t resp1 = 0;
  1747. errorstate = SD_SendStatus(hsd, &resp1);
  1748. if(errorstate != HAL_OK)
  1749. {
  1750. hsd->ErrorCode |= errorstate;
  1751. }
  1752. cardstate = (HAL_SD_CardStateTypeDef)((resp1 >> 9) & 0x0F);
  1753. return cardstate;
  1754. }
  1755. /**
  1756. * @brief Abort the current transfer and disable the SD.
  1757. * @param hsd: pointer to a SD_HandleTypeDef structure that contains
  1758. * the configuration information for SD module.
  1759. * @retval HAL status
  1760. */
  1761. HAL_StatusTypeDef HAL_SD_Abort(SD_HandleTypeDef *hsd)
  1762. {
  1763. HAL_SD_CardStateTypeDef CardState;
  1764. /* DIsable All interrupts */
  1765. __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT|\
  1766. SDMMC_IT_TXUNDERR| SDMMC_IT_RXOVERR);
  1767. /* Clear All flags */
  1768. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  1769. if((hsd->hdmatx != NULL) || (hsd->hdmarx != NULL))
  1770. {
  1771. /* Disable the SD DMA request */
  1772. hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDMMC_DCTRL_DMAEN);
  1773. /* Abort the SD DMA Tx Stream */
  1774. if(hsd->hdmatx != NULL)
  1775. {
  1776. HAL_DMA_Abort(hsd->hdmatx);
  1777. }
  1778. /* Abort the SD DMA Rx Stream */
  1779. if(hsd->hdmarx != NULL)
  1780. {
  1781. HAL_DMA_Abort(hsd->hdmarx);
  1782. }
  1783. }
  1784. hsd->State = HAL_SD_STATE_READY;
  1785. CardState = HAL_SD_GetCardState(hsd);
  1786. if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
  1787. {
  1788. hsd->ErrorCode = SDMMC_CmdStopTransfer(hsd->Instance);
  1789. }
  1790. if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
  1791. {
  1792. return HAL_ERROR;
  1793. }
  1794. return HAL_OK;
  1795. }
  1796. /**
  1797. * @brief Abort the current transfer and disable the SD (IT mode).
  1798. * @param hsd: pointer to a SD_HandleTypeDef structure that contains
  1799. * the configuration information for SD module.
  1800. * @retval HAL status
  1801. */
  1802. HAL_StatusTypeDef HAL_SD_Abort_IT(SD_HandleTypeDef *hsd)
  1803. {
  1804. HAL_SD_CardStateTypeDef CardState;
  1805. /* DIsable All interrupts */
  1806. __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT|\
  1807. SDMMC_IT_TXUNDERR| SDMMC_IT_RXOVERR);
  1808. /* Clear All flags */
  1809. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  1810. if((hsd->hdmatx != NULL) || (hsd->hdmarx != NULL))
  1811. {
  1812. /* Disable the SD DMA request */
  1813. hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDMMC_DCTRL_DMAEN);
  1814. /* Abort the SD DMA Tx Stream */
  1815. if(hsd->hdmatx != NULL)
  1816. {
  1817. hsd->hdmatx->XferAbortCallback = SD_DMATxAbort;
  1818. if(HAL_DMA_Abort_IT(hsd->hdmatx) != HAL_OK)
  1819. {
  1820. hsd->hdmatx = NULL;
  1821. }
  1822. }
  1823. /* Abort the SD DMA Rx Stream */
  1824. if(hsd->hdmarx != NULL)
  1825. {
  1826. hsd->hdmarx->XferAbortCallback = SD_DMARxAbort;
  1827. if(HAL_DMA_Abort_IT(hsd->hdmarx) != HAL_OK)
  1828. {
  1829. hsd->hdmarx = NULL;
  1830. }
  1831. }
  1832. }
  1833. /* No transfer ongoing on both DMA channels*/
  1834. if((hsd->hdmatx == NULL) && (hsd->hdmarx == NULL))
  1835. {
  1836. CardState = HAL_SD_GetCardState(hsd);
  1837. hsd->State = HAL_SD_STATE_READY;
  1838. if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
  1839. {
  1840. hsd->ErrorCode = SDMMC_CmdStopTransfer(hsd->Instance);
  1841. }
  1842. if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
  1843. {
  1844. return HAL_ERROR;
  1845. }
  1846. else
  1847. {
  1848. HAL_SD_AbortCallback(hsd);
  1849. }
  1850. }
  1851. return HAL_OK;
  1852. }
  1853. /**
  1854. * @}
  1855. */
  1856. /**
  1857. * @}
  1858. */
  1859. /* Private function ----------------------------------------------------------*/
  1860. /** @addtogroup SD_Private_Functions
  1861. * @{
  1862. */
  1863. /**
  1864. * @brief DMA SD transmit process complete callback
  1865. * @param hdma: DMA handle
  1866. * @retval None
  1867. */
  1868. static void SD_DMATransmitCplt(DMA_HandleTypeDef *hdma)
  1869. {
  1870. SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
  1871. /* Enable DATAEND Interrupt */
  1872. __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DATAEND));
  1873. }
  1874. /**
  1875. * @brief DMA SD receive process complete callback
  1876. * @param hdma: DMA handle
  1877. * @retval None
  1878. */
  1879. static void SD_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
  1880. {
  1881. SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
  1882. uint32_t errorstate = HAL_SD_ERROR_NONE;
  1883. /* Send stop command in multiblock write */
  1884. if(hsd->Context == (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_DMA))
  1885. {
  1886. errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
  1887. if(errorstate != HAL_SD_ERROR_NONE)
  1888. {
  1889. hsd->ErrorCode |= errorstate;
  1890. HAL_SD_ErrorCallback(hsd);
  1891. }
  1892. }
  1893. /* Disable the DMA transfer for transmit request by setting the DMAEN bit
  1894. in the SD DCTRL register */
  1895. hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDMMC_DCTRL_DMAEN);
  1896. /* Clear all the static flags */
  1897. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  1898. hsd->State = HAL_SD_STATE_READY;
  1899. HAL_SD_RxCpltCallback(hsd);
  1900. }
  1901. /**
  1902. * @brief DMA SD communication error callback
  1903. * @param hdma: DMA handle
  1904. * @retval None
  1905. */
  1906. static void SD_DMAError(DMA_HandleTypeDef *hdma)
  1907. {
  1908. SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
  1909. HAL_SD_CardStateTypeDef CardState;
  1910. /* if DMA error is FIFO error ignore it */
  1911. if(HAL_DMA_GetError(hdma) != HAL_DMA_ERROR_FE)
  1912. {
  1913. if((hsd->hdmarx->ErrorCode == HAL_DMA_ERROR_TE) || (hsd->hdmatx->ErrorCode == HAL_DMA_ERROR_TE))
  1914. {
  1915. /* Clear All flags */
  1916. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  1917. /* Disable All interrupts */
  1918. __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT|\
  1919. SDMMC_IT_TXUNDERR| SDMMC_IT_RXOVERR);
  1920. hsd->ErrorCode |= HAL_SD_ERROR_DMA;
  1921. CardState = HAL_SD_GetCardState(hsd);
  1922. if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
  1923. {
  1924. hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
  1925. }
  1926. hsd->State= HAL_SD_STATE_READY;
  1927. }
  1928. HAL_SD_ErrorCallback(hsd);
  1929. }
  1930. }
  1931. /**
  1932. * @brief DMA SD Tx Abort callback
  1933. * @param hdma: DMA handle
  1934. * @retval None
  1935. */
  1936. static void SD_DMATxAbort(DMA_HandleTypeDef *hdma)
  1937. {
  1938. SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
  1939. HAL_SD_CardStateTypeDef CardState;
  1940. if(hsd->hdmatx != NULL)
  1941. {
  1942. hsd->hdmatx = NULL;
  1943. }
  1944. /* All DMA channels are aborted */
  1945. if(hsd->hdmarx == NULL)
  1946. {
  1947. CardState = HAL_SD_GetCardState(hsd);
  1948. hsd->ErrorCode = HAL_SD_ERROR_NONE;
  1949. hsd->State = HAL_SD_STATE_READY;
  1950. if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
  1951. {
  1952. hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
  1953. if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
  1954. {
  1955. HAL_SD_AbortCallback(hsd);
  1956. }
  1957. else
  1958. {
  1959. HAL_SD_ErrorCallback(hsd);
  1960. }
  1961. }
  1962. }
  1963. }
  1964. /**
  1965. * @brief DMA SD Rx Abort callback
  1966. * @param hdma: DMA handle
  1967. * @retval None
  1968. */
  1969. static void SD_DMARxAbort(DMA_HandleTypeDef *hdma)
  1970. {
  1971. SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
  1972. HAL_SD_CardStateTypeDef CardState;
  1973. if(hsd->hdmarx != NULL)
  1974. {
  1975. hsd->hdmarx = NULL;
  1976. }
  1977. /* All DMA channels are aborted */
  1978. if(hsd->hdmatx == NULL)
  1979. {
  1980. CardState = HAL_SD_GetCardState(hsd);
  1981. hsd->ErrorCode = HAL_SD_ERROR_NONE;
  1982. hsd->State = HAL_SD_STATE_READY;
  1983. if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
  1984. {
  1985. hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
  1986. if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
  1987. {
  1988. HAL_SD_AbortCallback(hsd);
  1989. }
  1990. else
  1991. {
  1992. HAL_SD_ErrorCallback(hsd);
  1993. }
  1994. }
  1995. }
  1996. }
  1997. /**
  1998. * @brief Initializes the sd card.
  1999. * @param hsd: Pointer to SD handle
  2000. * @retval SD Card error state
  2001. */
  2002. static uint32_t SD_InitCard(SD_HandleTypeDef *hsd)
  2003. {
  2004. HAL_SD_CardCSDTypeDef CSD;
  2005. uint32_t errorstate = HAL_SD_ERROR_NONE;
  2006. uint16_t sd_rca = 1;
  2007. /* Check the power State */
  2008. if(SDMMC_GetPowerState(hsd->Instance) == 0)
  2009. {
  2010. /* Power off */
  2011. return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
  2012. }
  2013. if(hsd->SdCard.CardType != CARD_SECURED)
  2014. {
  2015. /* Send CMD2 ALL_SEND_CID */
  2016. errorstate = SDMMC_CmdSendCID(hsd->Instance);
  2017. if(errorstate != HAL_SD_ERROR_NONE)
  2018. {
  2019. return errorstate;
  2020. }
  2021. else
  2022. {
  2023. /* Get Card identification number data */
  2024. hsd->CID[0] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
  2025. hsd->CID[1] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP2);
  2026. hsd->CID[2] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP3);
  2027. hsd->CID[3] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP4);
  2028. }
  2029. }
  2030. if(hsd->SdCard.CardType != CARD_SECURED)
  2031. {
  2032. /* Send CMD3 SET_REL_ADDR with argument 0 */
  2033. /* SD Card publishes its RCA. */
  2034. errorstate = SDMMC_CmdSetRelAdd(hsd->Instance, &sd_rca);
  2035. if(errorstate != HAL_SD_ERROR_NONE)
  2036. {
  2037. return errorstate;
  2038. }
  2039. }
  2040. if(hsd->SdCard.CardType != CARD_SECURED)
  2041. {
  2042. /* Get the SD card RCA */
  2043. hsd->SdCard.RelCardAdd = sd_rca;
  2044. /* Send CMD9 SEND_CSD with argument as card's RCA */
  2045. errorstate = SDMMC_CmdSendCSD(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
  2046. if(errorstate != HAL_SD_ERROR_NONE)
  2047. {
  2048. return errorstate;
  2049. }
  2050. else
  2051. {
  2052. /* Get Card Specific Data */
  2053. hsd->CSD[0U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
  2054. hsd->CSD[1U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP2);
  2055. hsd->CSD[2U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP3);
  2056. hsd->CSD[3U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP4);
  2057. }
  2058. }
  2059. /* Get the Card Class */
  2060. hsd->SdCard.Class = (SDMMC_GetResponse(hsd->Instance, SDMMC_RESP2) >> 20);
  2061. /* Get CSD parameters */
  2062. HAL_SD_GetCardCSD(hsd, &CSD);
  2063. /* Select the Card */
  2064. errorstate = SDMMC_CmdSelDesel(hsd->Instance, (uint32_t)(((uint32_t)hsd->SdCard.RelCardAdd) << 16));
  2065. if(errorstate != HAL_SD_ERROR_NONE)
  2066. {
  2067. return errorstate;
  2068. }
  2069. /* Configure SDMMC peripheral interface */
  2070. SDMMC_Init(hsd->Instance, hsd->Init);
  2071. /* All cards are initialized */
  2072. return HAL_SD_ERROR_NONE;
  2073. }
  2074. /**
  2075. * @brief Enquires cards about their operating voltage and configures clock
  2076. * controls and stores SD information that will be needed in future
  2077. * in the SD handle.
  2078. * @param hsd: Pointer to SD handle
  2079. * @retval error state
  2080. */
  2081. static uint32_t SD_PowerON(SD_HandleTypeDef *hsd)
  2082. {
  2083. __IO uint32_t count = 0;
  2084. uint32_t response = 0, validvoltage = 0;
  2085. uint32_t errorstate = HAL_SD_ERROR_NONE;
  2086. /* CMD0: GO_IDLE_STATE */
  2087. errorstate = SDMMC_CmdGoIdleState(hsd->Instance);
  2088. if(errorstate != HAL_SD_ERROR_NONE)
  2089. {
  2090. return errorstate;
  2091. }
  2092. /* CMD8: SEND_IF_COND: Command available only on V2.0 cards */
  2093. errorstate = SDMMC_CmdOperCond(hsd->Instance);
  2094. if(errorstate != HAL_SD_ERROR_NONE)
  2095. {
  2096. hsd->SdCard.CardVersion = CARD_V1_X;
  2097. /* Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */
  2098. while(validvoltage == 0)
  2099. {
  2100. if(count++ == SDMMC_MAX_VOLT_TRIAL)
  2101. {
  2102. return HAL_SD_ERROR_INVALID_VOLTRANGE;
  2103. }
  2104. /* SEND CMD55 APP_CMD with RCA as 0 */
  2105. errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0);
  2106. if(errorstate != HAL_SD_ERROR_NONE)
  2107. {
  2108. return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
  2109. }
  2110. /* Send CMD41 */
  2111. errorstate = SDMMC_CmdAppOperCommand(hsd->Instance, SDMMC_STD_CAPACITY);
  2112. if(errorstate != HAL_SD_ERROR_NONE)
  2113. {
  2114. return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
  2115. }
  2116. /* Get command response */
  2117. response = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
  2118. /* Get operating voltage*/
  2119. validvoltage = (((response >> 31) == 1) ? 1 : 0);
  2120. }
  2121. /* Card type is SDSC */
  2122. hsd->SdCard.CardType = CARD_SDSC;
  2123. }
  2124. else
  2125. {
  2126. hsd->SdCard.CardVersion = CARD_V2_X;
  2127. /* Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */
  2128. while(validvoltage == 0)
  2129. {
  2130. if(count++ == SDMMC_MAX_VOLT_TRIAL)
  2131. {
  2132. return HAL_SD_ERROR_INVALID_VOLTRANGE;
  2133. }
  2134. /* SEND CMD55 APP_CMD with RCA as 0 */
  2135. errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0);
  2136. if(errorstate != HAL_SD_ERROR_NONE)
  2137. {
  2138. return errorstate;
  2139. }
  2140. /* Send CMD41 */
  2141. errorstate = SDMMC_CmdAppOperCommand(hsd->Instance, SDMMC_HIGH_CAPACITY);
  2142. if(errorstate != HAL_SD_ERROR_NONE)
  2143. {
  2144. return errorstate;
  2145. }
  2146. /* Get command response */
  2147. response = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
  2148. /* Get operating voltage*/
  2149. validvoltage = (((response >> 31) == 1) ? 1 : 0);
  2150. }
  2151. if((response & SDMMC_HIGH_CAPACITY) == SDMMC_HIGH_CAPACITY) /* (response &= SD_HIGH_CAPACITY) */
  2152. {
  2153. hsd->SdCard.CardType = CARD_SDHC_SDXC;
  2154. }
  2155. else
  2156. {
  2157. hsd->SdCard.CardType = CARD_SDSC;
  2158. }
  2159. }
  2160. return HAL_SD_ERROR_NONE;
  2161. }
  2162. /**
  2163. * @brief Turns the SDMMC output signals off.
  2164. * @param hsd: Pointer to SD handle
  2165. * @retval HAL status
  2166. */
  2167. static HAL_StatusTypeDef SD_PowerOFF(SD_HandleTypeDef *hsd)
  2168. {
  2169. /* Set Power State to OFF */
  2170. SDMMC_PowerState_OFF(hsd->Instance);
  2171. return HAL_OK;
  2172. }
  2173. /**
  2174. * @brief Send Status info command.
  2175. * @param hsd: pointer to SD handle
  2176. * @param pSDstatus: Pointer to the buffer that will contain the SD card status
  2177. * SD Status register)
  2178. * @retval error state
  2179. */
  2180. static uint32_t SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus)
  2181. {
  2182. SDMMC_DataInitTypeDef config;
  2183. uint32_t errorstate = HAL_SD_ERROR_NONE;
  2184. uint32_t tickstart = HAL_GetTick();
  2185. uint32_t count = 0;
  2186. /* Check SD response */
  2187. if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
  2188. {
  2189. return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
  2190. }
  2191. /* Set block size for card if it is not equal to current block size for card */
  2192. errorstate = SDMMC_CmdBlockLength(hsd->Instance, 64);
  2193. if(errorstate != HAL_SD_ERROR_NONE)
  2194. {
  2195. hsd->ErrorCode |= HAL_SD_ERROR_NONE;
  2196. return errorstate;
  2197. }
  2198. /* Send CMD55 */
  2199. errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16));
  2200. if(errorstate != HAL_SD_ERROR_NONE)
  2201. {
  2202. hsd->ErrorCode |= HAL_SD_ERROR_NONE;
  2203. return errorstate;
  2204. }
  2205. /* Configure the SD DPSM (Data Path State Machine) */
  2206. config.DataTimeOut = SDMMC_DATATIMEOUT;
  2207. config.DataLength = 64;
  2208. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_64B;
  2209. config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
  2210. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  2211. config.DPSM = SDMMC_DPSM_ENABLE;
  2212. SDMMC_ConfigData(hsd->Instance, &config);
  2213. /* Send ACMD13 (SD_APP_STAUS) with argument as card's RCA */
  2214. errorstate = SDMMC_CmdStatusRegister(hsd->Instance);
  2215. if(errorstate != HAL_SD_ERROR_NONE)
  2216. {
  2217. hsd->ErrorCode |= HAL_SD_ERROR_NONE;
  2218. return errorstate;
  2219. }
  2220. /* Get status data */
  2221. while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND))
  2222. {
  2223. if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF))
  2224. {
  2225. for(count = 0; count < 8; count++)
  2226. {
  2227. *(pSDstatus + count) = SDMMC_ReadFIFO(hsd->Instance);
  2228. }
  2229. pSDstatus += 8;
  2230. }
  2231. if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
  2232. {
  2233. return HAL_SD_ERROR_TIMEOUT;
  2234. }
  2235. }
  2236. if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
  2237. {
  2238. return HAL_SD_ERROR_DATA_TIMEOUT;
  2239. }
  2240. else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
  2241. {
  2242. return HAL_SD_ERROR_DATA_CRC_FAIL;
  2243. }
  2244. else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
  2245. {
  2246. return HAL_SD_ERROR_RX_OVERRUN;
  2247. }
  2248. while ((__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXDAVL)))
  2249. {
  2250. *pSDstatus = SDMMC_ReadFIFO(hsd->Instance);
  2251. pSDstatus++;
  2252. if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
  2253. {
  2254. return HAL_SD_ERROR_TIMEOUT;
  2255. }
  2256. }
  2257. /* Clear all the static status flags*/
  2258. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  2259. return HAL_SD_ERROR_NONE;
  2260. }
  2261. /**
  2262. * @brief Returns the current card's status.
  2263. * @param hsd: Pointer to SD handle
  2264. * @param pCardStatus: pointer to the buffer that will contain the SD card
  2265. * status (Card Status register)
  2266. * @retval error state
  2267. */
  2268. static uint32_t SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus)
  2269. {
  2270. uint32_t errorstate = HAL_SD_ERROR_NONE;
  2271. if(pCardStatus == NULL)
  2272. {
  2273. return HAL_SD_ERROR_PARAM;
  2274. }
  2275. /* Send Status command */
  2276. errorstate = SDMMC_CmdSendStatus(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16));
  2277. if(errorstate != HAL_OK)
  2278. {
  2279. return errorstate;
  2280. }
  2281. /* Get SD card status */
  2282. *pCardStatus = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
  2283. return HAL_SD_ERROR_NONE;
  2284. }
  2285. /**
  2286. * @brief Enables the SDMMC wide bus mode.
  2287. * @param hsd: pointer to SD handle
  2288. * @retval error state
  2289. */
  2290. static uint32_t SD_WideBus_Enable(SD_HandleTypeDef *hsd)
  2291. {
  2292. uint32_t scr[2] = {0, 0};
  2293. uint32_t errorstate = HAL_SD_ERROR_NONE;
  2294. if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
  2295. {
  2296. return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
  2297. }
  2298. /* Get SCR Register */
  2299. errorstate = SD_FindSCR(hsd, scr);
  2300. if(errorstate != HAL_OK)
  2301. {
  2302. return errorstate;
  2303. }
  2304. /* If requested card supports wide bus operation */
  2305. if((scr[1] & SDMMC_WIDE_BUS_SUPPORT) != SDMMC_ALLZERO)
  2306. {
  2307. /* Send CMD55 APP_CMD with argument as card's RCA.*/
  2308. errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16));
  2309. if(errorstate != HAL_OK)
  2310. {
  2311. return errorstate;
  2312. }
  2313. /* Send ACMD6 APP_CMD with argument as 2 for wide bus mode */
  2314. errorstate = SDMMC_CmdBusWidth(hsd->Instance, 2);
  2315. if(errorstate != HAL_OK)
  2316. {
  2317. return errorstate;
  2318. }
  2319. return HAL_SD_ERROR_NONE;
  2320. }
  2321. else
  2322. {
  2323. return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
  2324. }
  2325. }
  2326. /**
  2327. * @brief Disables the SDMMC wide bus mode.
  2328. * @param hsd: Pointer to SD handle
  2329. * @retval error state
  2330. */
  2331. static uint32_t SD_WideBus_Disable(SD_HandleTypeDef *hsd)
  2332. {
  2333. uint32_t scr[2] = {0, 0};
  2334. uint32_t errorstate = HAL_SD_ERROR_NONE;
  2335. if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
  2336. {
  2337. return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
  2338. }
  2339. /* Get SCR Register */
  2340. errorstate = SD_FindSCR(hsd, scr);
  2341. if(errorstate != HAL_OK)
  2342. {
  2343. return errorstate;
  2344. }
  2345. /* If requested card supports 1 bit mode operation */
  2346. if((scr[1] & SDMMC_SINGLE_BUS_SUPPORT) != SDMMC_ALLZERO)
  2347. {
  2348. /* Send CMD55 APP_CMD with argument as card's RCA */
  2349. errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16));
  2350. if(errorstate != HAL_OK)
  2351. {
  2352. return errorstate;
  2353. }
  2354. /* Send ACMD6 APP_CMD with argument as 0 for single bus mode */
  2355. errorstate = SDMMC_CmdBusWidth(hsd->Instance, 0);
  2356. if(errorstate != HAL_OK)
  2357. {
  2358. return errorstate;
  2359. }
  2360. return HAL_SD_ERROR_NONE;
  2361. }
  2362. else
  2363. {
  2364. return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
  2365. }
  2366. }
  2367. /**
  2368. * @brief Finds the SD card SCR register value.
  2369. * @param hsd: Pointer to SD handle
  2370. * @param pSCR: pointer to the buffer that will contain the SCR value
  2371. * @retval error state
  2372. */
  2373. static uint32_t SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR)
  2374. {
  2375. SDMMC_DataInitTypeDef config;
  2376. uint32_t errorstate = HAL_SD_ERROR_NONE;
  2377. uint32_t tickstart = HAL_GetTick();
  2378. uint32_t index = 0;
  2379. uint32_t tempscr[2] = {0, 0};
  2380. /* Set Block Size To 8 Bytes */
  2381. errorstate = SDMMC_CmdBlockLength(hsd->Instance, 8);
  2382. if(errorstate != HAL_OK)
  2383. {
  2384. return errorstate;
  2385. }
  2386. /* Send CMD55 APP_CMD with argument as card's RCA */
  2387. errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)((hsd->SdCard.RelCardAdd) << 16));
  2388. if(errorstate != HAL_OK)
  2389. {
  2390. return errorstate;
  2391. }
  2392. config.DataTimeOut = SDMMC_DATATIMEOUT;
  2393. config.DataLength = 8;
  2394. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_8B;
  2395. config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
  2396. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  2397. config.DPSM = SDMMC_DPSM_ENABLE;
  2398. SDMMC_ConfigData(hsd->Instance, &config);
  2399. /* Send ACMD51 SD_APP_SEND_SCR with argument as 0 */
  2400. errorstate = SDMMC_CmdSendSCR(hsd->Instance);
  2401. if(errorstate != HAL_OK)
  2402. {
  2403. return errorstate;
  2404. }
  2405. while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND))
  2406. {
  2407. if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXDAVL))
  2408. {
  2409. *(tempscr + index) = SDMMC_ReadFIFO(hsd->Instance);
  2410. index++;
  2411. }
  2412. if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
  2413. {
  2414. return HAL_SD_ERROR_TIMEOUT;
  2415. }
  2416. }
  2417. if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
  2418. {
  2419. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT);
  2420. return HAL_SD_ERROR_DATA_TIMEOUT;
  2421. }
  2422. else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
  2423. {
  2424. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL);
  2425. return HAL_SD_ERROR_DATA_CRC_FAIL;
  2426. }
  2427. else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
  2428. {
  2429. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_RXOVERR);
  2430. return HAL_SD_ERROR_RX_OVERRUN;
  2431. }
  2432. else
  2433. {
  2434. /* No error flag set */
  2435. /* Clear all the static flags */
  2436. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  2437. *(pSCR + 1) = ((tempscr[0] & SDMMC_0TO7BITS) << 24) | ((tempscr[0] & SDMMC_8TO15BITS) << 8) |\
  2438. ((tempscr[0] & SDMMC_16TO23BITS) >> 8) | ((tempscr[0] & SDMMC_24TO31BITS) >> 24);
  2439. *(pSCR) = ((tempscr[1] & SDMMC_0TO7BITS) << 24) | ((tempscr[1] & SDMMC_8TO15BITS) << 8) |\
  2440. ((tempscr[1] & SDMMC_16TO23BITS) >> 8) | ((tempscr[1] & SDMMC_24TO31BITS) >> 24);
  2441. }
  2442. return HAL_SD_ERROR_NONE;
  2443. }
  2444. /**
  2445. * @brief Wrap up reading in non-blocking mode.
  2446. * @param hsd: pointer to a SD_HandleTypeDef structure that contains
  2447. * the configuration information.
  2448. * @retval HAL status
  2449. */
  2450. static HAL_StatusTypeDef SD_Read_IT(SD_HandleTypeDef *hsd)
  2451. {
  2452. uint32_t count = 0;
  2453. uint32_t* tmp;
  2454. tmp = (uint32_t*)hsd->pRxBuffPtr;
  2455. /* Read data from SDMMC Rx FIFO */
  2456. for(count = 0; count < 8; count++)
  2457. {
  2458. *(tmp + count) = SDMMC_ReadFIFO(hsd->Instance);
  2459. }
  2460. hsd->pRxBuffPtr += 8;
  2461. return HAL_OK;
  2462. }
  2463. /**
  2464. * @brief Wrap up writing in non-blocking mode.
  2465. * @param hsd: pointer to a SD_HandleTypeDef structure that contains
  2466. * the configuration information.
  2467. * @retval HAL status
  2468. */
  2469. static HAL_StatusTypeDef SD_Write_IT(SD_HandleTypeDef *hsd)
  2470. {
  2471. uint32_t count = 0;
  2472. uint32_t* tmp;
  2473. tmp = (uint32_t*)hsd->pTxBuffPtr;
  2474. /* Write data to SDMMC Tx FIFO */
  2475. for(count = 0; count < 8; count++)
  2476. {
  2477. SDMMC_WriteFIFO(hsd->Instance, (tmp + count));
  2478. }
  2479. hsd->pTxBuffPtr += 8;
  2480. return HAL_OK;
  2481. }
  2482. /**
  2483. * @}
  2484. */
  2485. #endif /* HAL_SD_MODULE_ENABLED */
  2486. /**
  2487. * @}
  2488. */
  2489. /**
  2490. * @}
  2491. */
  2492. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/