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.
 
 
 

3413 lines
104 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_sd.c
  4. * @author MCD Application Team
  5. * @version V1.3.0
  6. * @date 29-January-2016
  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. * + Peripheral State 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 (SDMMC1 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 SDMMC1 low level resources by implementing the HAL_SD_MspInit() API:
  31. (##) Call the function HAL_RCCEx_PeriphCLKConfig with RCC_PERIPHCLK_SDMMC1 for
  32. PeriphClockSelection and select SDMMC1 clock source (MSI, main PLL or PLLSAI1)
  33. (##) Enable the SDMMC1 interface clock using __HAL_RCC_SDMMC1_CLK_ENABLE();
  34. (##) SDMMC pins configuration for SD card
  35. (+++) Enable the clock for the SDMMC GPIOs using the functions __HAL_RCC_GPIOx_CLK_ENABLE();
  36. (+++) Configure these SDMMC pins as alternate function pull-up using HAL_GPIO_Init()
  37. and according to your pin assignment;
  38. (##) DMA Configuration if you need to use DMA process (HAL_SD_ReadBlocks_DMA()
  39. and HAL_SD_WriteBlocks_DMA() APIs).
  40. (+++) Enable the DMAx interface clock using __HAL_RCC_DMAx_CLK_ENABLE();
  41. (+++) Configure the DMA using the function HAL_DMA_Init() with predeclared and filled.
  42. (##) NVIC configuration if you need to use interrupt process when using DMA transfer.
  43. (+++) Configure the SDMMC and DMA interrupt priorities using functions
  44. HAL_NVIC_SetPriority(); DMA priority is superior to SDMMC's priority
  45. (+++) Enable the NVIC DMA and SDMMC IRQs using function HAL_NVIC_EnableIRQ()
  46. (+++) SDMMC interrupts are managed using the macros __HAL_SD_SDMMC_ENABLE_IT()
  47. and __HAL_SD_SDMMC_DISABLE_IT() inside the communication process.
  48. (+++) SDMMC interrupts pending bits are managed using the macros __HAL_SD_SDMMC_GET_IT()
  49. and __HAL_SD_SDMMC_CLEAR_IT()
  50. (#) At this stage, you can perform SD read/write/erase operations after SD card initialization
  51. *** SD Card Initialization and configuration ***
  52. ================================================
  53. [..]
  54. To initialize the SD Card, use the HAL_SD_Init() function. It Initializes
  55. the SD Card and put it into StandBy State (Ready for data transfer).
  56. This function provide the following operations:
  57. (#) Apply the SD Card initialization process at 400KHz and check the SD Card
  58. type (Standard Capacity or High Capacity). You can change or adapt this
  59. frequency by adjusting the "ClockDiv" field.
  60. The SD Card frequency (SDMMC_CK) is computed as follows:
  61. (++)
  62. SDMMC_CK = SDMMCCLK / (ClockDiv + 2)
  63. -@@- In initialization mode and according to the SD Card standard,
  64. make sure that the SDMMC_CK frequency doesn't exceed 400KHz.
  65. (#) Get the SD CID and CSD data. All these information are managed by the SDCardInfo
  66. structure. This structure provide also ready computed SD Card capacity
  67. and Block size.
  68. -@- These information are stored in SD handle structure in case of future use.
  69. (#) Configure the SD Card Data transfer frequency. By Default, the card transfer
  70. frequency is set to 24MHz. You can change or adapt this frequency by adjusting
  71. the "ClockDiv" field.
  72. In transfer mode and according to the SD Card standard, make sure that the
  73. SDMMC_CK frequency doesn't exceed 25MHz and 50MHz in High-speed mode switch.
  74. To be able to use a frequency higher than 24MHz, you should use the SDMMC
  75. peripheral in bypass mode. Refer to the corresponding reference manual
  76. for more details.
  77. (#) Select the corresponding SD Card according to the address read with the step 2.
  78. (#) Configure the SD Card in wide bus mode: 4-bits data.
  79. *** SD Card Read operation ***
  80. ==============================
  81. [..]
  82. (+) You can read from SD card in polling mode by using function HAL_SD_ReadBlocks().
  83. This function support only 512-bytes block length (the block size should be
  84. chosen as 512 bytes).
  85. You can choose either one block read operation or multiple block read operation
  86. by adjusting the "NumberOfBlocks" parameter.
  87. (+) You can read from SD card in DMA mode by using function HAL_SD_ReadBlocks_DMA().
  88. This function support only 512-bytes block length (the block size should be
  89. chosen as 512 bytes).
  90. You can choose either one block read operation or multiple block read operation
  91. by adjusting the "NumberOfBlocks" parameter.
  92. After this, you have to call the function HAL_SD_CheckReadOperation(), to insure
  93. that the read transfer is done correctly in both DMA and SD sides.
  94. *** SD Card Write operation ***
  95. ===============================
  96. [..]
  97. (+) You can write to SD card in polling mode by using function HAL_SD_WriteBlocks().
  98. This function support only 512-bytes block length (the block size should be
  99. chosen as 512 bytes).
  100. You can choose either one block read operation or multiple block read operation
  101. by adjusting the "NumberOfBlocks" parameter.
  102. (+) You can write to SD card in DMA mode by using function HAL_SD_WriteBlocks_DMA().
  103. This function support only 512-bytes block length (the block size should be
  104. chosen as 512 byte).
  105. You can choose either one block read operation or multiple block read operation
  106. by adjusting the "NumberOfBlocks" parameter.
  107. After this, you have to call the function HAL_SD_CheckWriteOperation(), to insure
  108. that the write transfer is done correctly in both DMA and SD sides.
  109. *** SD card status ***
  110. ======================
  111. [..]
  112. (+) At any time, you can check the SD Card status and get the SD card state
  113. by using the HAL_SD_GetStatus() function. This function checks first if the
  114. SD card is still connected and then get the internal SD Card transfer state.
  115. (+) You can also get the SD card SD Status register by using the HAL_SD_SendSDStatus()
  116. function.
  117. *** SD HAL driver macros list ***
  118. ==================================
  119. [..]
  120. Below the list of most used macros in SD HAL driver.
  121. (+) __HAL_SD_SDMMC_ENABLE : Enable the SD device
  122. (+) __HAL_SD_SDMMC_DISABLE : Disable the SD device
  123. (+) __HAL_SD_SDMMC_DMA_ENABLE: Enable the SDMMC DMA transfer
  124. (+) __HAL_SD_SDMMC_DMA_DISABLE: Disable the SDMMC DMA transfer
  125. (+) __HAL_SD_SDMMC_ENABLE_IT: Enable the SD device interrupt
  126. (+) __HAL_SD_SDMMC_DISABLE_IT: Disable the SD device interrupt
  127. (+) __HAL_SD_SDMMC_GET_FLAG:Check whether the specified SD flag is set or not
  128. (+) __HAL_SD_SDMMC_CLEAR_FLAG: Clear the SD's pending flags
  129. [..]
  130. (@) You can refer to the SD HAL driver header file for more useful macros
  131. @endverbatim
  132. ******************************************************************************
  133. * @attention
  134. *
  135. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  136. *
  137. * Redistribution and use in source and binary forms, with or without modification,
  138. * are permitted provided that the following conditions are met:
  139. * 1. Redistributions of source code must retain the above copyright notice,
  140. * this list of conditions and the following disclaimer.
  141. * 2. Redistributions in binary form must reproduce the above copyright notice,
  142. * this list of conditions and the following disclaimer in the documentation
  143. * and/or other materials provided with the distribution.
  144. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  145. * may be used to endorse or promote products derived from this software
  146. * without specific prior written permission.
  147. *
  148. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  149. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  150. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  151. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  152. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  153. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  154. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  155. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  156. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  157. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  158. *
  159. ******************************************************************************
  160. */
  161. /* Includes ------------------------------------------------------------------*/
  162. #include "stm32l4xx_hal.h"
  163. /** @addtogroup STM32L4xx_HAL_Driver
  164. * @{
  165. */
  166. /** @addtogroup SD
  167. * @{
  168. */
  169. #ifdef HAL_SD_MODULE_ENABLED
  170. /* Private typedef -----------------------------------------------------------*/
  171. /* Private define ------------------------------------------------------------*/
  172. /** @addtogroup SD_Private_Defines
  173. * @{
  174. */
  175. /**
  176. * @brief SDMMC Data block size
  177. */
  178. #define DATA_BLOCK_SIZE ((uint32_t)(9 << 4))
  179. /**
  180. * @brief SDMMC Static flags, Timeout, FIFO Address
  181. */
  182. #define SDMMC_STATIC_FLAGS ((uint32_t)(SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_CTIMEOUT |\
  183. SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_TXUNDERR | SDMMC_FLAG_RXOVERR |\
  184. SDMMC_FLAG_CMDREND | SDMMC_FLAG_CMDSENT | SDMMC_FLAG_DATAEND |\
  185. SDMMC_FLAG_DBCKEND))
  186. #define SDMMC_CMD0TIMEOUT ((uint32_t)0x00010000)
  187. /**
  188. * @brief Mask for errors Card Status R1 (OCR Register)
  189. */
  190. #define SD_OCR_ADDR_OUT_OF_RANGE ((uint32_t)0x80000000)
  191. #define SD_OCR_ADDR_MISALIGNED ((uint32_t)0x40000000)
  192. #define SD_OCR_BLOCK_LEN_ERR ((uint32_t)0x20000000)
  193. #define SD_OCR_ERASE_SEQ_ERR ((uint32_t)0x10000000)
  194. #define SD_OCR_BAD_ERASE_PARAM ((uint32_t)0x08000000)
  195. #define SD_OCR_WRITE_PROT_VIOLATION ((uint32_t)0x04000000)
  196. #define SD_OCR_LOCK_UNLOCK_FAILED ((uint32_t)0x01000000)
  197. #define SD_OCR_COM_CRC_FAILED ((uint32_t)0x00800000)
  198. #define SD_OCR_ILLEGAL_CMD ((uint32_t)0x00400000)
  199. #define SD_OCR_CARD_ECC_FAILED ((uint32_t)0x00200000)
  200. #define SD_OCR_CC_ERROR ((uint32_t)0x00100000)
  201. #define SD_OCR_GENERAL_UNKNOWN_ERROR ((uint32_t)0x00080000)
  202. #define SD_OCR_STREAM_READ_UNDERRUN ((uint32_t)0x00040000)
  203. #define SD_OCR_STREAM_WRITE_OVERRUN ((uint32_t)0x00020000)
  204. #define SD_OCR_CID_CSD_OVERWRITE ((uint32_t)0x00010000)
  205. #define SD_OCR_WP_ERASE_SKIP ((uint32_t)0x00008000)
  206. #define SD_OCR_CARD_ECC_DISABLED ((uint32_t)0x00004000)
  207. #define SD_OCR_ERASE_RESET ((uint32_t)0x00002000)
  208. #define SD_OCR_AKE_SEQ_ERROR ((uint32_t)0x00000008)
  209. #define SD_OCR_ERRORBITS ((uint32_t)0xFDFFE008)
  210. /**
  211. * @brief Masks for R6 Response
  212. */
  213. #define SD_R6_GENERAL_UNKNOWN_ERROR ((uint32_t)0x00002000)
  214. #define SD_R6_ILLEGAL_CMD ((uint32_t)0x00004000)
  215. #define SD_R6_COM_CRC_FAILED ((uint32_t)0x00008000)
  216. #define SD_VOLTAGE_WINDOW_SD ((uint32_t)0x80100000)
  217. #define SD_HIGH_CAPACITY ((uint32_t)0x40000000)
  218. #define SD_STD_CAPACITY ((uint32_t)0x00000000)
  219. #define SD_CHECK_PATTERN ((uint32_t)0x000001AA)
  220. #define SD_MAX_VOLT_TRIAL ((uint32_t)0x0000FFFF)
  221. #define SD_ALLZERO ((uint32_t)0x00000000)
  222. #define SD_WIDE_BUS_SUPPORT ((uint32_t)0x00040000)
  223. #define SD_SINGLE_BUS_SUPPORT ((uint32_t)0x00010000)
  224. #define SD_CARD_LOCKED ((uint32_t)0x02000000)
  225. #define SD_DATATIMEOUT ((uint32_t)0xFFFFFFFF)
  226. #define SD_0TO7BITS ((uint32_t)0x000000FF)
  227. #define SD_8TO15BITS ((uint32_t)0x0000FF00)
  228. #define SD_16TO23BITS ((uint32_t)0x00FF0000)
  229. #define SD_24TO31BITS ((uint32_t)0xFF000000)
  230. #define SD_MAX_DATA_LENGTH ((uint32_t)0x01FFFFFF)
  231. #define SD_HALFFIFO ((uint32_t)0x00000008)
  232. #define SD_HALFFIFOBYTES ((uint32_t)0x00000020)
  233. /**
  234. * @brief Command Class Supported
  235. */
  236. #define SD_CCCC_LOCK_UNLOCK ((uint32_t)0x00000080)
  237. #define SD_CCCC_WRITE_PROT ((uint32_t)0x00000040)
  238. #define SD_CCCC_ERASE ((uint32_t)0x00000020)
  239. /**
  240. * @brief Following commands are SD Card Specific commands.
  241. * SDMMC_APP_CMD should be sent before sending these commands.
  242. */
  243. #define SD_SDMMC_SEND_IF_COND ((uint32_t)SD_CMD_HS_SEND_EXT_CSD)
  244. /**
  245. * @}
  246. */
  247. /* Private macro -------------------------------------------------------------*/
  248. /* Private variables ---------------------------------------------------------*/
  249. /* Private function prototypes -----------------------------------------------*/
  250. /** @addtogroup SD_Private_Functions_Prototypes
  251. * @{
  252. */
  253. static HAL_SD_ErrorTypedef SD_Initialize_Cards(SD_HandleTypeDef *hsd);
  254. static HAL_SD_ErrorTypedef SD_Select_Deselect(SD_HandleTypeDef *hsd, uint64_t addr);
  255. static HAL_SD_ErrorTypedef SD_PowerON(SD_HandleTypeDef *hsd);
  256. static HAL_SD_ErrorTypedef SD_PowerOFF(SD_HandleTypeDef *hsd);
  257. static HAL_SD_ErrorTypedef SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus);
  258. static HAL_SD_CardStateTypedef SD_GetState(SD_HandleTypeDef *hsd);
  259. static HAL_SD_ErrorTypedef SD_IsCardProgramming(SD_HandleTypeDef *hsd, uint8_t *pStatus);
  260. static HAL_SD_ErrorTypedef SD_CmdError(SD_HandleTypeDef *hsd);
  261. static HAL_SD_ErrorTypedef SD_CmdResp1Error(SD_HandleTypeDef *hsd, uint8_t SD_CMD);
  262. static HAL_SD_ErrorTypedef SD_CmdResp7Error(SD_HandleTypeDef *hsd);
  263. static HAL_SD_ErrorTypedef SD_CmdResp3Error(SD_HandleTypeDef *hsd);
  264. static HAL_SD_ErrorTypedef SD_CmdResp2Error(SD_HandleTypeDef *hsd);
  265. static HAL_SD_ErrorTypedef SD_CmdResp6Error(SD_HandleTypeDef *hsd, uint8_t SD_CMD, uint16_t *pRCA);
  266. static HAL_SD_ErrorTypedef SD_WideBus_Enable(SD_HandleTypeDef *hsd);
  267. static HAL_SD_ErrorTypedef SD_WideBus_Disable(SD_HandleTypeDef *hsd);
  268. static HAL_SD_ErrorTypedef SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR);
  269. static void SD_DMA_RxCplt(DMA_HandleTypeDef *hdma);
  270. static void SD_DMA_RxError(DMA_HandleTypeDef *hdma);
  271. static void SD_DMA_TxCplt(DMA_HandleTypeDef *hdma);
  272. static void SD_DMA_TxError(DMA_HandleTypeDef *hdma);
  273. /**
  274. * @}
  275. */
  276. /* Exported functions --------------------------------------------------------*/
  277. /** @addtogroup SD_Exported_Functions
  278. * @{
  279. */
  280. /** @addtogroup SD_Exported_Functions_Group1
  281. * @brief Initialization and de-initialization functions
  282. *
  283. @verbatim
  284. ==============================================================================
  285. ##### Initialization and de-initialization functions #####
  286. ==============================================================================
  287. [..]
  288. This section provides functions allowing to initialize/de-initialize the SD
  289. card device to be ready for use.
  290. @endverbatim
  291. * @{
  292. */
  293. /**
  294. * @brief Initializes the SD card according to the specified parameters in the
  295. SD_HandleTypeDef and initialize the associated handle.
  296. * @param hsd: SD handle
  297. * @param SDCardInfo: HAL_SD_CardInfoTypedef structure for SD card information
  298. * @retval HAL SD error state
  299. */
  300. HAL_SD_ErrorTypedef HAL_SD_Init(SD_HandleTypeDef *hsd, HAL_SD_CardInfoTypedef *SDCardInfo)
  301. {
  302. __IO HAL_SD_ErrorTypedef errorstate = SD_OK;
  303. SD_InitTypeDef tmpinit;
  304. /* Initialize the low level hardware (MSP) */
  305. HAL_SD_MspInit(hsd);
  306. /* Default SDMMC peripheral configuration for SD card initialization */
  307. tmpinit.ClockEdge = SDMMC_CLOCK_EDGE_RISING;
  308. tmpinit.ClockBypass = SDMMC_CLOCK_BYPASS_DISABLE;
  309. tmpinit.ClockPowerSave = SDMMC_CLOCK_POWER_SAVE_DISABLE;
  310. tmpinit.BusWide = SDMMC_BUS_WIDE_1B;
  311. tmpinit.HardwareFlowControl = SDMMC_HARDWARE_FLOW_CONTROL_DISABLE;
  312. tmpinit.ClockDiv = SDMMC_INIT_CLK_DIV;
  313. /* Initialize SDMMC peripheral interface with default configuration */
  314. SDMMC_Init(hsd->Instance, tmpinit);
  315. /* Identify card operating voltage */
  316. errorstate = SD_PowerON(hsd);
  317. if(errorstate != SD_OK)
  318. {
  319. return errorstate;
  320. }
  321. /* Initialize the present SDMMC card(s) and put them in idle state */
  322. errorstate = SD_Initialize_Cards(hsd);
  323. if (errorstate != SD_OK)
  324. {
  325. return errorstate;
  326. }
  327. /* Read CSD/CID MSD registers */
  328. errorstate = HAL_SD_Get_CardInfo(hsd, SDCardInfo);
  329. if (errorstate == SD_OK)
  330. {
  331. /* Select the Card */
  332. errorstate = SD_Select_Deselect(hsd, (uint32_t)(((uint32_t)SDCardInfo->RCA) << 16));
  333. }
  334. /* Configure SDMMC peripheral interface */
  335. SDMMC_Init(hsd->Instance, hsd->Init);
  336. return errorstate;
  337. }
  338. /**
  339. * @brief De-Initializes the SD card.
  340. * @param hsd: SD handle
  341. * @retval HAL status
  342. */
  343. HAL_StatusTypeDef HAL_SD_DeInit(SD_HandleTypeDef *hsd)
  344. {
  345. /* Set SD power state to off */
  346. SD_PowerOFF(hsd);
  347. /* De-Initialize the MSP layer */
  348. HAL_SD_MspDeInit(hsd);
  349. return HAL_OK;
  350. }
  351. /**
  352. * @brief Initializes the SD MSP.
  353. * @param hsd: SD handle
  354. * @retval None
  355. */
  356. __weak void HAL_SD_MspInit(SD_HandleTypeDef *hsd)
  357. {
  358. /* Prevent unused argument(s) compilation warning */
  359. UNUSED(hsd);
  360. /* NOTE : This function should not be modified, when the callback is needed,
  361. the HAL_SD_MspInit could be implemented in the user file
  362. */
  363. }
  364. /**
  365. * @brief De-Initialize SD MSP.
  366. * @param hsd: SD handle
  367. * @retval None
  368. */
  369. __weak void HAL_SD_MspDeInit(SD_HandleTypeDef *hsd)
  370. {
  371. /* Prevent unused argument(s) compilation warning */
  372. UNUSED(hsd);
  373. /* NOTE : This function should not be modified, when the callback is needed,
  374. the HAL_SD_MspDeInit could be implemented in the user file
  375. */
  376. }
  377. /**
  378. * @}
  379. */
  380. /** @addtogroup SD_Exported_Functions_Group2
  381. * @brief Data transfer functions
  382. *
  383. @verbatim
  384. ==============================================================================
  385. ##### IO operation functions #####
  386. ==============================================================================
  387. [..]
  388. This subsection provides a set of functions allowing to manage the data
  389. transfer from/to SD card.
  390. @endverbatim
  391. * @{
  392. */
  393. /**
  394. * @brief Reads block(s) from a specified address in a card. The Data transfer
  395. * is managed by polling mode.
  396. * @param hsd: SD handle
  397. * @param pReadBuffer: pointer to the buffer that will contain the received data
  398. * @param ReadAddr: Address from where data is to be read
  399. * @param BlockSize: SD card Data block size
  400. * @note BlockSize must be 512 bytes.
  401. * @param NumberOfBlocks: Number of SD blocks to read
  402. * @retval SD Card error state
  403. */
  404. HAL_SD_ErrorTypedef HAL_SD_ReadBlocks(SD_HandleTypeDef *hsd, uint32_t *pReadBuffer, uint64_t ReadAddr, uint32_t BlockSize, uint32_t NumberOfBlocks)
  405. {
  406. SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure;
  407. SDMMC_DataInitTypeDef sdmmc_datainitstructure;
  408. HAL_SD_ErrorTypedef errorstate = SD_OK;
  409. uint32_t count = 0, *tempbuff = (uint32_t *)pReadBuffer;
  410. /* Initialize data control register */
  411. hsd->Instance->DCTRL = 0;
  412. if (hsd->CardType == HIGH_CAPACITY_SD_CARD)
  413. {
  414. BlockSize = 512;
  415. ReadAddr /= 512;
  416. }
  417. /* Set Block Size for Card */
  418. sdmmc_cmdinitstructure.Argument = (uint32_t) BlockSize;
  419. sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN;
  420. sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT;
  421. sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO;
  422. sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE;
  423. SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
  424. /* Check for error conditions */
  425. errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN);
  426. if (errorstate != SD_OK)
  427. {
  428. return errorstate;
  429. }
  430. /* Configure the SD DPSM (Data Path State Machine) */
  431. sdmmc_datainitstructure.DataTimeOut = SD_DATATIMEOUT;
  432. sdmmc_datainitstructure.DataLength = NumberOfBlocks * BlockSize;
  433. sdmmc_datainitstructure.DataBlockSize = DATA_BLOCK_SIZE;
  434. sdmmc_datainitstructure.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
  435. sdmmc_datainitstructure.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  436. sdmmc_datainitstructure.DPSM = SDMMC_DPSM_ENABLE;
  437. SDMMC_DataConfig(hsd->Instance, &sdmmc_datainitstructure);
  438. if(NumberOfBlocks > 1)
  439. {
  440. /* Send CMD18 READ_MULT_BLOCK with argument data address */
  441. sdmmc_cmdinitstructure.CmdIndex = SD_CMD_READ_MULT_BLOCK;
  442. }
  443. else
  444. {
  445. /* Send CMD17 READ_SINGLE_BLOCK */
  446. sdmmc_cmdinitstructure.CmdIndex = SD_CMD_READ_SINGLE_BLOCK;
  447. }
  448. sdmmc_cmdinitstructure.Argument = (uint32_t)ReadAddr;
  449. SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
  450. /* Read block(s) in polling mode */
  451. if(NumberOfBlocks > 1)
  452. {
  453. /* Check for error conditions */
  454. errorstate = SD_CmdResp1Error(hsd, SD_CMD_READ_MULT_BLOCK);
  455. if (errorstate != SD_OK)
  456. {
  457. return errorstate;
  458. }
  459. /* Poll on SDMMC flags */
  460. while(!__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DATAEND))
  461. {
  462. if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF))
  463. {
  464. /* Read data from SDMMC Rx FIFO */
  465. for (count = 0; count < 8; count++)
  466. {
  467. *(tempbuff + count) = SDMMC_ReadFIFO(hsd->Instance);
  468. }
  469. tempbuff += 8;
  470. }
  471. }
  472. }
  473. else
  474. {
  475. /* Check for error conditions */
  476. errorstate = SD_CmdResp1Error(hsd, SD_CMD_READ_SINGLE_BLOCK);
  477. if (errorstate != SD_OK)
  478. {
  479. return errorstate;
  480. }
  481. /* In case of single block transfer, no need of stop transfer at all */
  482. while(!__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND))
  483. {
  484. if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF))
  485. {
  486. /* Read data from SDMMC Rx FIFO */
  487. for (count = 0; count < 8; count++)
  488. {
  489. *(tempbuff + count) = SDMMC_ReadFIFO(hsd->Instance);
  490. }
  491. tempbuff += 8;
  492. }
  493. }
  494. }
  495. /* Send stop transmission command in case of multiblock read */
  496. if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_DATAEND) && (NumberOfBlocks > 1))
  497. {
  498. if ((hsd->CardType == STD_CAPACITY_SD_CARD_V1_1) ||\
  499. (hsd->CardType == STD_CAPACITY_SD_CARD_V2_0) ||\
  500. (hsd->CardType == HIGH_CAPACITY_SD_CARD))
  501. {
  502. /* Send stop transmission command */
  503. errorstate = HAL_SD_StopTransfer(hsd);
  504. }
  505. }
  506. /* Get error state */
  507. if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
  508. {
  509. __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT);
  510. errorstate = SD_DATA_TIMEOUT;
  511. return errorstate;
  512. }
  513. else if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
  514. {
  515. __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL);
  516. errorstate = SD_DATA_CRC_FAIL;
  517. return errorstate;
  518. }
  519. else if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
  520. {
  521. __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_RXOVERR);
  522. errorstate = SD_RX_OVERRUN;
  523. return errorstate;
  524. }
  525. else
  526. {
  527. /* No error flag set */
  528. }
  529. count = SD_DATATIMEOUT;
  530. /* Empty FIFO if there is still any data */
  531. while ((__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXDAVL)) && (count > 0))
  532. {
  533. *tempbuff = SDMMC_ReadFIFO(hsd->Instance);
  534. tempbuff++;
  535. count--;
  536. }
  537. /* Clear all the static flags */
  538. __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  539. return errorstate;
  540. }
  541. /**
  542. * @brief Allows to write block(s) to a specified address in a card. The Data
  543. * transfer is managed by polling mode.
  544. * @param hsd: SD handle
  545. * @param pWriteBuffer: pointer to the buffer that will contain the data to transmit
  546. * @param WriteAddr: Address from where data is to be written
  547. * @param BlockSize: SD card Data block size
  548. * @note BlockSize must be 512 bytes.
  549. * @param NumberOfBlocks: Number of SD blocks to write
  550. * @retval SD Card error state
  551. */
  552. HAL_SD_ErrorTypedef HAL_SD_WriteBlocks(SD_HandleTypeDef *hsd, uint32_t *pWriteBuffer, uint64_t WriteAddr, uint32_t BlockSize, uint32_t NumberOfBlocks)
  553. {
  554. SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure;
  555. SDMMC_DataInitTypeDef sdmmc_datainitstructure;
  556. HAL_SD_ErrorTypedef errorstate = SD_OK;
  557. uint32_t totalnumberofbytes = 0, bytestransferred = 0, count = 0, restwords = 0;
  558. uint32_t *tempbuff = (uint32_t *)pWriteBuffer;
  559. uint8_t cardstate = 0;
  560. /* Initialize data control register */
  561. hsd->Instance->DCTRL = 0;
  562. if (hsd->CardType == HIGH_CAPACITY_SD_CARD)
  563. {
  564. BlockSize = 512;
  565. WriteAddr /= 512;
  566. }
  567. /* Set Block Size for Card */
  568. sdmmc_cmdinitstructure.Argument = (uint32_t)BlockSize;
  569. sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN;
  570. sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT;
  571. sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO;
  572. sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE;
  573. SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
  574. /* Check for error conditions */
  575. errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN);
  576. if (errorstate != SD_OK)
  577. {
  578. return errorstate;
  579. }
  580. if(NumberOfBlocks > 1)
  581. {
  582. /* Send CMD25 WRITE_MULT_BLOCK with argument data address */
  583. sdmmc_cmdinitstructure.CmdIndex = SD_CMD_WRITE_MULT_BLOCK;
  584. }
  585. else
  586. {
  587. /* Send CMD24 WRITE_SINGLE_BLOCK */
  588. sdmmc_cmdinitstructure.CmdIndex = SD_CMD_WRITE_SINGLE_BLOCK;
  589. }
  590. sdmmc_cmdinitstructure.Argument = (uint32_t)WriteAddr;
  591. SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
  592. /* Check for error conditions */
  593. if(NumberOfBlocks > 1)
  594. {
  595. errorstate = SD_CmdResp1Error(hsd, SD_CMD_WRITE_MULT_BLOCK);
  596. }
  597. else
  598. {
  599. errorstate = SD_CmdResp1Error(hsd, SD_CMD_WRITE_SINGLE_BLOCK);
  600. }
  601. if (errorstate != SD_OK)
  602. {
  603. return errorstate;
  604. }
  605. /* Set total number of bytes to write */
  606. totalnumberofbytes = NumberOfBlocks * BlockSize;
  607. /* Configure the SD DPSM (Data Path State Machine) */
  608. sdmmc_datainitstructure.DataTimeOut = SD_DATATIMEOUT;
  609. sdmmc_datainitstructure.DataLength = NumberOfBlocks * BlockSize;
  610. sdmmc_datainitstructure.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
  611. sdmmc_datainitstructure.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
  612. sdmmc_datainitstructure.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  613. sdmmc_datainitstructure.DPSM = SDMMC_DPSM_ENABLE;
  614. SDMMC_DataConfig(hsd->Instance, &sdmmc_datainitstructure);
  615. /* Write block(s) in polling mode */
  616. if(NumberOfBlocks > 1)
  617. {
  618. while(!__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_TXUNDERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DATAEND))
  619. {
  620. if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_TXFIFOHE))
  621. {
  622. if ((totalnumberofbytes - bytestransferred) < 32)
  623. {
  624. restwords = ((totalnumberofbytes - bytestransferred) % 4 == 0) ? ((totalnumberofbytes - bytestransferred) / 4) : (( totalnumberofbytes - bytestransferred) / 4 + 1);
  625. /* Write data to SDMMC Tx FIFO */
  626. for (count = 0; count < restwords; count++)
  627. {
  628. SDMMC_WriteFIFO(hsd->Instance, tempbuff);
  629. tempbuff++;
  630. bytestransferred += 4;
  631. }
  632. }
  633. else
  634. {
  635. /* Write data to SDMMC Tx FIFO */
  636. for (count = 0; count < 8; count++)
  637. {
  638. SDMMC_WriteFIFO(hsd->Instance, (tempbuff + count));
  639. }
  640. tempbuff += 8;
  641. bytestransferred += 32;
  642. }
  643. }
  644. }
  645. }
  646. else
  647. {
  648. /* In case of single data block transfer no need of stop command at all */
  649. while(!__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_TXUNDERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND))
  650. {
  651. if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_TXFIFOHE))
  652. {
  653. if ((totalnumberofbytes - bytestransferred) < 32)
  654. {
  655. restwords = ((totalnumberofbytes - bytestransferred) % 4 == 0) ? ((totalnumberofbytes - bytestransferred) / 4) : (( totalnumberofbytes - bytestransferred) / 4 + 1);
  656. /* Write data to SDMMC Tx FIFO */
  657. for (count = 0; count < restwords; count++)
  658. {
  659. SDMMC_WriteFIFO(hsd->Instance, tempbuff);
  660. tempbuff++;
  661. bytestransferred += 4;
  662. }
  663. }
  664. else
  665. {
  666. /* Write data to SDMMC Tx FIFO */
  667. for (count = 0; count < 8; count++)
  668. {
  669. SDMMC_WriteFIFO(hsd->Instance, (tempbuff + count));
  670. }
  671. tempbuff += 8;
  672. bytestransferred += 32;
  673. }
  674. }
  675. }
  676. }
  677. /* Send stop transmission command in case of multiblock write */
  678. if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_DATAEND) && (NumberOfBlocks > 1))
  679. {
  680. if ((hsd->CardType == STD_CAPACITY_SD_CARD_V1_1) || (hsd->CardType == STD_CAPACITY_SD_CARD_V2_0) ||\
  681. (hsd->CardType == HIGH_CAPACITY_SD_CARD))
  682. {
  683. /* Send stop transmission command */
  684. errorstate = HAL_SD_StopTransfer(hsd);
  685. }
  686. }
  687. /* Get error state */
  688. if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
  689. {
  690. __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT);
  691. errorstate = SD_DATA_TIMEOUT;
  692. return errorstate;
  693. }
  694. else if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
  695. {
  696. __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL);
  697. errorstate = SD_DATA_CRC_FAIL;
  698. return errorstate;
  699. }
  700. else if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_TXUNDERR))
  701. {
  702. __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_TXUNDERR);
  703. errorstate = SD_TX_UNDERRUN;
  704. return errorstate;
  705. }
  706. else
  707. {
  708. /* No error flag set */
  709. }
  710. /* Clear all the static flags */
  711. __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  712. /* Wait till the card is in programming state */
  713. errorstate = SD_IsCardProgramming(hsd, &cardstate);
  714. while ((errorstate == SD_OK) && ((cardstate == SD_CARD_PROGRAMMING) || (cardstate == SD_CARD_RECEIVING)))
  715. {
  716. errorstate = SD_IsCardProgramming(hsd, &cardstate);
  717. }
  718. return errorstate;
  719. }
  720. /**
  721. * @brief Reads block(s) from a specified address in a card. The Data transfer
  722. * is managed by DMA mode.
  723. * @note This API should be followed by the function HAL_SD_CheckReadOperation()
  724. * to check the completion of the read process
  725. * @param hsd: SD handle
  726. * @param pReadBuffer: Pointer to the buffer that will contain the received data
  727. * @param ReadAddr: Address from where data is to be read
  728. * @param BlockSize: SD card Data block size
  729. * @note BlockSize must be 512 bytes.
  730. * @param NumberOfBlocks: Number of blocks to read.
  731. * @retval SD Card error state
  732. */
  733. HAL_SD_ErrorTypedef HAL_SD_ReadBlocks_DMA(SD_HandleTypeDef *hsd, uint32_t *pReadBuffer, uint64_t ReadAddr, uint32_t BlockSize, uint32_t NumberOfBlocks)
  734. {
  735. SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure;
  736. SDMMC_DataInitTypeDef sdmmc_datainitstructure;
  737. HAL_SD_ErrorTypedef errorstate = SD_OK;
  738. /* Initialize data control register */
  739. hsd->Instance->DCTRL = 0;
  740. /* Initialize handle flags */
  741. hsd->SdTransferCplt = 0;
  742. hsd->DmaTransferCplt = 0;
  743. hsd->SdTransferErr = SD_OK;
  744. /* Initialize SD Read operation */
  745. if(NumberOfBlocks > 1)
  746. {
  747. hsd->SdOperation = SD_READ_MULTIPLE_BLOCK;
  748. }
  749. else
  750. {
  751. hsd->SdOperation = SD_READ_SINGLE_BLOCK;
  752. }
  753. /* Enable transfer interrupts */
  754. __HAL_SD_SDMMC_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL |\
  755. SDMMC_IT_DTIMEOUT |\
  756. SDMMC_IT_DATAEND |\
  757. SDMMC_IT_RXOVERR));
  758. /* Enable SDMMC DMA transfer */
  759. __HAL_SD_SDMMC_DMA_ENABLE(hsd);
  760. /* Configure DMA user callbacks */
  761. hsd->hdmarx->XferCpltCallback = SD_DMA_RxCplt;
  762. hsd->hdmarx->XferErrorCallback = SD_DMA_RxError;
  763. /* Change DMA direction Periph to Memory */
  764. hsd->hdmarx->Init.Direction = DMA_PERIPH_TO_MEMORY;
  765. hsd->hdmarx->Instance->CCR &= ~DMA_MEMORY_TO_PERIPH;
  766. /* Enable the DMA Channel */
  767. HAL_DMA_Start_IT(hsd->hdmarx, (uint32_t)&hsd->Instance->FIFO, (uint32_t)pReadBuffer, (uint32_t)(BlockSize * NumberOfBlocks)/4);
  768. if (hsd->CardType == HIGH_CAPACITY_SD_CARD)
  769. {
  770. BlockSize = 512;
  771. ReadAddr /= 512;
  772. }
  773. /* Set Block Size for Card */
  774. sdmmc_cmdinitstructure.Argument = (uint32_t)BlockSize;
  775. sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN;
  776. sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT;
  777. sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO;
  778. sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE;
  779. SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
  780. /* Check for error conditions */
  781. errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN);
  782. if (errorstate != SD_OK)
  783. {
  784. return errorstate;
  785. }
  786. /* Configure the SD DPSM (Data Path State Machine) */
  787. sdmmc_datainitstructure.DataTimeOut = SD_DATATIMEOUT;
  788. sdmmc_datainitstructure.DataLength = BlockSize * NumberOfBlocks;
  789. sdmmc_datainitstructure.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
  790. sdmmc_datainitstructure.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
  791. sdmmc_datainitstructure.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  792. sdmmc_datainitstructure.DPSM = SDMMC_DPSM_ENABLE;
  793. SDMMC_DataConfig(hsd->Instance, &sdmmc_datainitstructure);
  794. /* Check number of blocks command */
  795. if(NumberOfBlocks > 1)
  796. {
  797. /* Send CMD18 READ_MULT_BLOCK with argument data address */
  798. sdmmc_cmdinitstructure.CmdIndex = SD_CMD_READ_MULT_BLOCK;
  799. }
  800. else
  801. {
  802. /* Send CMD17 READ_SINGLE_BLOCK */
  803. sdmmc_cmdinitstructure.CmdIndex = SD_CMD_READ_SINGLE_BLOCK;
  804. }
  805. sdmmc_cmdinitstructure.Argument = (uint32_t)ReadAddr;
  806. SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
  807. /* Check for error conditions */
  808. if(NumberOfBlocks > 1)
  809. {
  810. errorstate = SD_CmdResp1Error(hsd, SD_CMD_READ_MULT_BLOCK);
  811. }
  812. else
  813. {
  814. errorstate = SD_CmdResp1Error(hsd, SD_CMD_READ_SINGLE_BLOCK);
  815. }
  816. /* Update the SD transfer error in SD handle */
  817. hsd->SdTransferErr = errorstate;
  818. return errorstate;
  819. }
  820. /**
  821. * @brief Writes block(s) to a specified address in a card. The Data transfer
  822. * is managed by DMA mode.
  823. * @note This API should be followed by the function HAL_SD_CheckWriteOperation()
  824. * to check the completion of the write process (by SD current status polling).
  825. * @param hsd: SD handle
  826. * @param pWriteBuffer: pointer to the buffer that will contain the data to transmit
  827. * @param WriteAddr: Address from where data is to be read
  828. * @param BlockSize: the SD card Data block size
  829. * @note BlockSize must be 512 bytes.
  830. * @param NumberOfBlocks: Number of blocks to write
  831. * @retval SD Card error state
  832. */
  833. HAL_SD_ErrorTypedef HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef *hsd, uint32_t *pWriteBuffer, uint64_t WriteAddr, uint32_t BlockSize, uint32_t NumberOfBlocks)
  834. {
  835. SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure;
  836. SDMMC_DataInitTypeDef sdmmc_datainitstructure;
  837. HAL_SD_ErrorTypedef errorstate = SD_OK;
  838. /* Initialize data control register */
  839. hsd->Instance->DCTRL = 0;
  840. /* Initialize handle flags */
  841. hsd->SdTransferCplt = 0;
  842. hsd->DmaTransferCplt = 0;
  843. hsd->SdTransferErr = SD_OK;
  844. /* Initialize SD Write operation */
  845. if(NumberOfBlocks > 1)
  846. {
  847. hsd->SdOperation = SD_WRITE_MULTIPLE_BLOCK;
  848. }
  849. else
  850. {
  851. hsd->SdOperation = SD_WRITE_SINGLE_BLOCK;
  852. }
  853. /* Enable transfer interrupts */
  854. __HAL_SD_SDMMC_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL |\
  855. SDMMC_IT_DTIMEOUT |\
  856. SDMMC_IT_DATAEND |\
  857. SDMMC_IT_TXUNDERR));
  858. /* Configure DMA user callbacks */
  859. hsd->hdmatx->XferCpltCallback = SD_DMA_TxCplt;
  860. hsd->hdmatx->XferErrorCallback = SD_DMA_TxError;
  861. /* Change DMA direction Memory to Periph */
  862. hsd->hdmatx->Init.Direction = DMA_MEMORY_TO_PERIPH;
  863. hsd->hdmatx->Instance->CCR |= DMA_MEMORY_TO_PERIPH;
  864. /* Enable the DMA Channel */
  865. HAL_DMA_Start_IT(hsd->hdmatx, (uint32_t)pWriteBuffer, (uint32_t)&hsd->Instance->FIFO, (uint32_t)(BlockSize * NumberOfBlocks)/4);
  866. /* Enable SDMMC DMA transfer */
  867. __HAL_SD_SDMMC_DMA_ENABLE(hsd);
  868. if (hsd->CardType == HIGH_CAPACITY_SD_CARD)
  869. {
  870. BlockSize = 512;
  871. WriteAddr /= 512;
  872. }
  873. /* Set Block Size for Card */
  874. sdmmc_cmdinitstructure.Argument = (uint32_t)BlockSize;
  875. sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN;
  876. sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT;
  877. sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO;
  878. sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE;
  879. SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
  880. /* Check for error conditions */
  881. errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN);
  882. if (errorstate != SD_OK)
  883. {
  884. return errorstate;
  885. }
  886. /* Check number of blocks command */
  887. if(NumberOfBlocks <= 1)
  888. {
  889. /* Send CMD24 WRITE_SINGLE_BLOCK */
  890. sdmmc_cmdinitstructure.CmdIndex = SD_CMD_WRITE_SINGLE_BLOCK;
  891. }
  892. else
  893. {
  894. /* Send CMD25 WRITE_MULT_BLOCK with argument data address */
  895. sdmmc_cmdinitstructure.CmdIndex = SD_CMD_WRITE_MULT_BLOCK;
  896. }
  897. sdmmc_cmdinitstructure.Argument = (uint32_t)WriteAddr;
  898. SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
  899. /* Check for error conditions */
  900. if(NumberOfBlocks > 1)
  901. {
  902. errorstate = SD_CmdResp1Error(hsd, SD_CMD_WRITE_MULT_BLOCK);
  903. }
  904. else
  905. {
  906. errorstate = SD_CmdResp1Error(hsd, SD_CMD_WRITE_SINGLE_BLOCK);
  907. }
  908. if (errorstate != SD_OK)
  909. {
  910. return errorstate;
  911. }
  912. /* Configure the SD DPSM (Data Path State Machine) */
  913. sdmmc_datainitstructure.DataTimeOut = SD_DATATIMEOUT;
  914. sdmmc_datainitstructure.DataLength = BlockSize * NumberOfBlocks;
  915. sdmmc_datainitstructure.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
  916. sdmmc_datainitstructure.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
  917. sdmmc_datainitstructure.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  918. sdmmc_datainitstructure.DPSM = SDMMC_DPSM_ENABLE;
  919. SDMMC_DataConfig(hsd->Instance, &sdmmc_datainitstructure);
  920. hsd->SdTransferErr = errorstate;
  921. return errorstate;
  922. }
  923. /**
  924. * @brief This function waits until the SD DMA data read transfer is finished.
  925. * This API should be called after HAL_SD_ReadBlocks_DMA() function
  926. * to insure that all data sent by the card is already transferred by the
  927. * DMA controller.
  928. * @param hsd: SD handle
  929. * @param Timeout: Timeout duration
  930. * @retval SD Card error state
  931. */
  932. HAL_SD_ErrorTypedef HAL_SD_CheckReadOperation(SD_HandleTypeDef *hsd, uint32_t Timeout)
  933. {
  934. HAL_SD_ErrorTypedef errorstate = SD_OK;
  935. uint32_t timeout = Timeout;
  936. uint32_t tmp1, tmp2;
  937. HAL_SD_ErrorTypedef tmp3;
  938. /* Wait for DMA/SD transfer end or SD error variables to be in SD handle */
  939. tmp1 = hsd->DmaTransferCplt;
  940. tmp2 = hsd->SdTransferCplt;
  941. tmp3 = (HAL_SD_ErrorTypedef)hsd->SdTransferErr;
  942. while (((tmp1 & tmp2) == 0) && (tmp3 == SD_OK) && (timeout > 0))
  943. {
  944. tmp1 = hsd->DmaTransferCplt;
  945. tmp2 = hsd->SdTransferCplt;
  946. tmp3 = (HAL_SD_ErrorTypedef)hsd->SdTransferErr;
  947. timeout--;
  948. }
  949. timeout = Timeout;
  950. /* Wait until the Rx transfer is no longer active */
  951. while((__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXACT)) && (timeout > 0))
  952. {
  953. timeout--;
  954. }
  955. /* Send stop command in multiblock read */
  956. if (hsd->SdOperation == SD_READ_MULTIPLE_BLOCK)
  957. {
  958. errorstate = HAL_SD_StopTransfer(hsd);
  959. }
  960. if ((timeout == 0) && (errorstate == SD_OK))
  961. {
  962. errorstate = SD_DATA_TIMEOUT;
  963. }
  964. /* Clear all the static flags */
  965. __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  966. /* Return error state */
  967. if (hsd->SdTransferErr != SD_OK)
  968. {
  969. return (HAL_SD_ErrorTypedef)(hsd->SdTransferErr);
  970. }
  971. return errorstate;
  972. }
  973. /**
  974. * @brief This function waits until the SD DMA data write transfer is finished.
  975. * This API should be called after HAL_SD_WriteBlocks_DMA() function
  976. * to insure that all data sent by the card is already transferred by the
  977. * DMA controller.
  978. * @param hsd: SD handle
  979. * @param Timeout: Timeout duration
  980. * @retval SD Card error state
  981. */
  982. HAL_SD_ErrorTypedef HAL_SD_CheckWriteOperation(SD_HandleTypeDef *hsd, uint32_t Timeout)
  983. {
  984. HAL_SD_ErrorTypedef errorstate = SD_OK;
  985. uint32_t timeout = Timeout;
  986. uint32_t tmp1, tmp2;
  987. HAL_SD_ErrorTypedef tmp3;
  988. /* Wait for DMA/SD transfer end or SD error variables to be in SD handle */
  989. tmp1 = hsd->DmaTransferCplt;
  990. tmp2 = hsd->SdTransferCplt;
  991. tmp3 = (HAL_SD_ErrorTypedef)hsd->SdTransferErr;
  992. while (((tmp1 & tmp2) == 0) && (tmp3 == SD_OK) && (timeout > 0))
  993. {
  994. tmp1 = hsd->DmaTransferCplt;
  995. tmp2 = hsd->SdTransferCplt;
  996. tmp3 = (HAL_SD_ErrorTypedef)hsd->SdTransferErr;
  997. timeout--;
  998. }
  999. timeout = Timeout;
  1000. /* Wait until the Tx transfer is no longer active */
  1001. while((__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_TXACT)) && (timeout > 0))
  1002. {
  1003. timeout--;
  1004. }
  1005. /* Send stop command in multiblock write */
  1006. if (hsd->SdOperation == SD_WRITE_MULTIPLE_BLOCK)
  1007. {
  1008. errorstate = HAL_SD_StopTransfer(hsd);
  1009. }
  1010. if ((timeout == 0) && (errorstate == SD_OK))
  1011. {
  1012. errorstate = SD_DATA_TIMEOUT;
  1013. }
  1014. /* Clear all the static flags */
  1015. __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  1016. /* Return error state */
  1017. if (hsd->SdTransferErr != SD_OK)
  1018. {
  1019. return (HAL_SD_ErrorTypedef)(hsd->SdTransferErr);
  1020. }
  1021. /* Wait until write is complete */
  1022. while(HAL_SD_GetStatus(hsd) != SD_TRANSFER_OK)
  1023. {
  1024. }
  1025. return errorstate;
  1026. }
  1027. /**
  1028. * @brief Erases the specified memory area of the given SD card.
  1029. * @param hsd: SD handle
  1030. * @param startaddr: Start byte address
  1031. * @param endaddr: End byte address
  1032. * @retval SD Card error state
  1033. */
  1034. HAL_SD_ErrorTypedef HAL_SD_Erase(SD_HandleTypeDef *hsd, uint64_t startaddr, uint64_t endaddr)
  1035. {
  1036. HAL_SD_ErrorTypedef errorstate = SD_OK;
  1037. SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure;
  1038. uint32_t delay = 0;
  1039. __IO uint32_t maxdelay = 0;
  1040. uint8_t cardstate = 0;
  1041. /* Check if the card command class supports erase command */
  1042. if (((hsd->CSD[1] >> 20) & SD_CCCC_ERASE) == 0)
  1043. {
  1044. errorstate = SD_REQUEST_NOT_APPLICABLE;
  1045. return errorstate;
  1046. }
  1047. /* Get max delay value */
  1048. maxdelay = 120000 / (((hsd->Instance->CLKCR) & 0xFF) + 2);
  1049. if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SD_CARD_LOCKED) == SD_CARD_LOCKED)
  1050. {
  1051. errorstate = SD_LOCK_UNLOCK_FAILED;
  1052. return errorstate;
  1053. }
  1054. /* Get start and end block for high capacity cards */
  1055. if (hsd->CardType == HIGH_CAPACITY_SD_CARD)
  1056. {
  1057. startaddr /= 512;
  1058. endaddr /= 512;
  1059. }
  1060. /* According to sd-card spec 1.0 ERASE_GROUP_START (CMD32) and erase_group_end(CMD33) */
  1061. if ((hsd->CardType == STD_CAPACITY_SD_CARD_V1_1) || (hsd->CardType == STD_CAPACITY_SD_CARD_V2_0) ||\
  1062. (hsd->CardType == HIGH_CAPACITY_SD_CARD))
  1063. {
  1064. /* Send CMD32 SD_ERASE_GRP_START with argument as addr */
  1065. sdmmc_cmdinitstructure.Argument =(uint32_t)startaddr;
  1066. sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SD_ERASE_GRP_START;
  1067. sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT;
  1068. sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO;
  1069. sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE;
  1070. SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
  1071. /* Check for error conditions */
  1072. errorstate = SD_CmdResp1Error(hsd, SD_CMD_SD_ERASE_GRP_START);
  1073. if (errorstate != SD_OK)
  1074. {
  1075. return errorstate;
  1076. }
  1077. /* Send CMD33 SD_ERASE_GRP_END with argument as addr */
  1078. sdmmc_cmdinitstructure.Argument = (uint32_t)endaddr;
  1079. sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SD_ERASE_GRP_END;
  1080. SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
  1081. /* Check for error conditions */
  1082. errorstate = SD_CmdResp1Error(hsd, SD_CMD_SD_ERASE_GRP_END);
  1083. if (errorstate != SD_OK)
  1084. {
  1085. return errorstate;
  1086. }
  1087. }
  1088. /* Send CMD38 ERASE */
  1089. sdmmc_cmdinitstructure.Argument = 0;
  1090. sdmmc_cmdinitstructure.CmdIndex = SD_CMD_ERASE;
  1091. SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
  1092. /* Check for error conditions */
  1093. errorstate = SD_CmdResp1Error(hsd, SD_CMD_ERASE);
  1094. if (errorstate != SD_OK)
  1095. {
  1096. return errorstate;
  1097. }
  1098. for (; delay < maxdelay; delay++)
  1099. {
  1100. }
  1101. /* Wait until the card is in programming state */
  1102. errorstate = SD_IsCardProgramming(hsd, &cardstate);
  1103. delay = SD_DATATIMEOUT;
  1104. while ((delay > 0) && (errorstate == SD_OK) && ((cardstate == SD_CARD_PROGRAMMING) || (cardstate == SD_CARD_RECEIVING)))
  1105. {
  1106. errorstate = SD_IsCardProgramming(hsd, &cardstate);
  1107. delay--;
  1108. }
  1109. return errorstate;
  1110. }
  1111. /**
  1112. * @brief This function handles SD card interrupt request.
  1113. * @param hsd: SD handle
  1114. * @retval None
  1115. */
  1116. void HAL_SD_IRQHandler(SD_HandleTypeDef *hsd)
  1117. {
  1118. /* Check for SDMMC interrupt flags */
  1119. if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_IT_DATAEND))
  1120. {
  1121. __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_IT_DATAEND);
  1122. /* SD transfer is complete */
  1123. hsd->SdTransferCplt = 1;
  1124. /* No transfer error */
  1125. hsd->SdTransferErr = SD_OK;
  1126. HAL_SD_XferCpltCallback(hsd);
  1127. }
  1128. else if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_IT_DCRCFAIL))
  1129. {
  1130. __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL);
  1131. hsd->SdTransferErr = SD_DATA_CRC_FAIL;
  1132. HAL_SD_XferErrorCallback(hsd);
  1133. }
  1134. else if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_IT_DTIMEOUT))
  1135. {
  1136. __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT);
  1137. hsd->SdTransferErr = SD_DATA_TIMEOUT;
  1138. HAL_SD_XferErrorCallback(hsd);
  1139. }
  1140. else if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_IT_RXOVERR))
  1141. {
  1142. __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_RXOVERR);
  1143. hsd->SdTransferErr = SD_RX_OVERRUN;
  1144. HAL_SD_XferErrorCallback(hsd);
  1145. }
  1146. else if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_IT_TXUNDERR))
  1147. {
  1148. __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_TXUNDERR);
  1149. hsd->SdTransferErr = SD_TX_UNDERRUN;
  1150. HAL_SD_XferErrorCallback(hsd);
  1151. }
  1152. else
  1153. {
  1154. /* No error flag set */
  1155. }
  1156. /* Disable all SDMMC peripheral interrupt sources */
  1157. __HAL_SD_SDMMC_DISABLE_IT(hsd, SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_DATAEND |\
  1158. SDMMC_IT_TXFIFOHE | SDMMC_IT_RXFIFOHF | SDMMC_IT_TXUNDERR |\
  1159. SDMMC_IT_RXOVERR);
  1160. }
  1161. /**
  1162. * @brief SD end of transfer callback.
  1163. * @param hsd: SD handle
  1164. * @retval None
  1165. */
  1166. __weak void HAL_SD_XferCpltCallback(SD_HandleTypeDef *hsd)
  1167. {
  1168. /* Prevent unused argument(s) compilation warning */
  1169. UNUSED(hsd);
  1170. /* NOTE : This function should not be modified, when the callback is needed,
  1171. the HAL_SD_XferCpltCallback could be implemented in the user file
  1172. */
  1173. }
  1174. /**
  1175. * @brief SD Transfer Error callback.
  1176. * @param hsd: SD handle
  1177. * @retval None
  1178. */
  1179. __weak void HAL_SD_XferErrorCallback(SD_HandleTypeDef *hsd)
  1180. {
  1181. /* Prevent unused argument(s) compilation warning */
  1182. UNUSED(hsd);
  1183. /* NOTE : This function should not be modified, when the callback is needed,
  1184. the HAL_SD_XferErrorCallback could be implemented in the user file
  1185. */
  1186. }
  1187. /**
  1188. * @brief SD Transfer complete Rx callback in non-blocking mode.
  1189. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1190. * the configuration information for the specified DMA module.
  1191. * @retval None
  1192. */
  1193. __weak void HAL_SD_DMA_RxCpltCallback(DMA_HandleTypeDef *hdma)
  1194. {
  1195. /* Prevent unused argument(s) compilation warning */
  1196. UNUSED(hdma);
  1197. /* NOTE : This function should not be modified, when the callback is needed,
  1198. the HAL_SD_DMA_RxCpltCallback could be implemented in the user file
  1199. */
  1200. }
  1201. /**
  1202. * @brief SD DMA transfer complete Rx error callback.
  1203. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1204. * the configuration information for the specified DMA module.
  1205. * @retval None
  1206. */
  1207. __weak void HAL_SD_DMA_RxErrorCallback(DMA_HandleTypeDef *hdma)
  1208. {
  1209. /* Prevent unused argument(s) compilation warning */
  1210. UNUSED(hdma);
  1211. /* NOTE : This function should not be modified, when the callback is needed,
  1212. the HAL_SD_DMA_RxErrorCallback could be implemented in the user file
  1213. */
  1214. }
  1215. /**
  1216. * @brief SD Transfer complete Tx callback in non-blocking mode.
  1217. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1218. * the configuration information for the specified DMA module.
  1219. * @retval None
  1220. */
  1221. __weak void HAL_SD_DMA_TxCpltCallback(DMA_HandleTypeDef *hdma)
  1222. {
  1223. /* Prevent unused argument(s) compilation warning */
  1224. UNUSED(hdma);
  1225. /* NOTE : This function should not be modified, when the callback is needed,
  1226. the HAL_SD_DMA_TxCpltCallback could be implemented in the user file
  1227. */
  1228. }
  1229. /**
  1230. * @brief SD DMA transfer complete error Tx callback.
  1231. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1232. * the configuration information for the specified DMA module.
  1233. * @retval None
  1234. */
  1235. __weak void HAL_SD_DMA_TxErrorCallback(DMA_HandleTypeDef *hdma)
  1236. {
  1237. /* Prevent unused argument(s) compilation warning */
  1238. UNUSED(hdma);
  1239. /* NOTE : This function should not be modified, when the callback is needed,
  1240. the HAL_SD_DMA_TxErrorCallback could be implemented in the user file
  1241. */
  1242. }
  1243. /**
  1244. * @}
  1245. */
  1246. /** @addtogroup SD_Exported_Functions_Group3
  1247. * @brief management functions
  1248. *
  1249. @verbatim
  1250. ==============================================================================
  1251. ##### Peripheral Control functions #####
  1252. ==============================================================================
  1253. [..]
  1254. This subsection provides a set of functions allowing to control the SD card
  1255. operations.
  1256. @endverbatim
  1257. * @{
  1258. */
  1259. /**
  1260. * @brief Returns information about specific card.
  1261. * @param hsd: SD handle
  1262. * @param pCardInfo: Pointer to a HAL_SD_CardInfoTypedef structure that
  1263. * contains all SD cardinformation
  1264. * @retval SD Card error state
  1265. */
  1266. HAL_SD_ErrorTypedef HAL_SD_Get_CardInfo(SD_HandleTypeDef *hsd, HAL_SD_CardInfoTypedef *pCardInfo)
  1267. {
  1268. HAL_SD_ErrorTypedef errorstate = SD_OK;
  1269. uint32_t tmp = 0;
  1270. pCardInfo->CardType = (uint8_t)(hsd->CardType);
  1271. pCardInfo->RCA = (uint16_t)(hsd->RCA);
  1272. /* Byte 0 */
  1273. tmp = (hsd->CSD[0] & 0xFF000000) >> 24;
  1274. pCardInfo->SD_csd.CSDStruct = (uint8_t)((tmp & 0xC0) >> 6);
  1275. pCardInfo->SD_csd.SysSpecVersion = (uint8_t)((tmp & 0x3C) >> 2);
  1276. pCardInfo->SD_csd.Reserved1 = tmp & 0x03;
  1277. /* Byte 1 */
  1278. tmp = (hsd->CSD[0] & 0x00FF0000) >> 16;
  1279. pCardInfo->SD_csd.TAAC = (uint8_t)tmp;
  1280. /* Byte 2 */
  1281. tmp = (hsd->CSD[0] & 0x0000FF00) >> 8;
  1282. pCardInfo->SD_csd.NSAC = (uint8_t)tmp;
  1283. /* Byte 3 */
  1284. tmp = hsd->CSD[0] & 0x000000FF;
  1285. pCardInfo->SD_csd.MaxBusClkFrec = (uint8_t)tmp;
  1286. /* Byte 4 */
  1287. tmp = (hsd->CSD[1] & 0xFF000000) >> 24;
  1288. pCardInfo->SD_csd.CardComdClasses = (uint16_t)(tmp << 4);
  1289. /* Byte 5 */
  1290. tmp = (hsd->CSD[1] & 0x00FF0000) >> 16;
  1291. pCardInfo->SD_csd.CardComdClasses |= (uint16_t)((tmp & 0xF0) >> 4);
  1292. pCardInfo->SD_csd.RdBlockLen = (uint8_t)(tmp & 0x0F);
  1293. /* Byte 6 */
  1294. tmp = (hsd->CSD[1] & 0x0000FF00) >> 8;
  1295. pCardInfo->SD_csd.PartBlockRead = (uint8_t)((tmp & 0x80) >> 7);
  1296. pCardInfo->SD_csd.WrBlockMisalign = (uint8_t)((tmp & 0x40) >> 6);
  1297. pCardInfo->SD_csd.RdBlockMisalign = (uint8_t)((tmp & 0x20) >> 5);
  1298. pCardInfo->SD_csd.DSRImpl = (uint8_t)((tmp & 0x10) >> 4);
  1299. pCardInfo->SD_csd.Reserved2 = 0; /*!< Reserved */
  1300. if ((hsd->CardType == STD_CAPACITY_SD_CARD_V1_1) || (hsd->CardType == STD_CAPACITY_SD_CARD_V2_0))
  1301. {
  1302. pCardInfo->SD_csd.DeviceSize = (tmp & 0x03) << 10;
  1303. /* Byte 7 */
  1304. tmp = (uint8_t)(hsd->CSD[1] & 0x000000FF);
  1305. pCardInfo->SD_csd.DeviceSize |= (tmp) << 2;
  1306. /* Byte 8 */
  1307. tmp = (uint8_t)((hsd->CSD[2] & 0xFF000000) >> 24);
  1308. pCardInfo->SD_csd.DeviceSize |= (tmp & 0xC0) >> 6;
  1309. pCardInfo->SD_csd.MaxRdCurrentVDDMin = (tmp & 0x38) >> 3;
  1310. pCardInfo->SD_csd.MaxRdCurrentVDDMax = (tmp & 0x07);
  1311. /* Byte 9 */
  1312. tmp = (uint8_t)((hsd->CSD[2] & 0x00FF0000) >> 16);
  1313. pCardInfo->SD_csd.MaxWrCurrentVDDMin = (tmp & 0xE0) >> 5;
  1314. pCardInfo->SD_csd.MaxWrCurrentVDDMax = (tmp & 0x1C) >> 2;
  1315. pCardInfo->SD_csd.DeviceSizeMul = (tmp & 0x03) << 1;
  1316. /* Byte 10 */
  1317. tmp = (uint8_t)((hsd->CSD[2] & 0x0000FF00) >> 8);
  1318. pCardInfo->SD_csd.DeviceSizeMul |= (tmp & 0x80) >> 7;
  1319. pCardInfo->CardCapacity = (pCardInfo->SD_csd.DeviceSize + 1) ;
  1320. pCardInfo->CardCapacity *= (1 << (pCardInfo->SD_csd.DeviceSizeMul + 2));
  1321. pCardInfo->CardBlockSize = 1 << (pCardInfo->SD_csd.RdBlockLen);
  1322. pCardInfo->CardCapacity *= pCardInfo->CardBlockSize;
  1323. }
  1324. else if (hsd->CardType == HIGH_CAPACITY_SD_CARD)
  1325. {
  1326. /* Byte 7 */
  1327. tmp = (uint8_t)(hsd->CSD[1] & 0x000000FF);
  1328. pCardInfo->SD_csd.DeviceSize = (tmp & 0x3F) << 16;
  1329. /* Byte 8 */
  1330. tmp = (uint8_t)((hsd->CSD[2] & 0xFF000000) >> 24);
  1331. pCardInfo->SD_csd.DeviceSize |= (tmp << 8);
  1332. /* Byte 9 */
  1333. tmp = (uint8_t)((hsd->CSD[2] & 0x00FF0000) >> 16);
  1334. pCardInfo->SD_csd.DeviceSize |= (tmp);
  1335. /* Byte 10 */
  1336. tmp = (uint8_t)((hsd->CSD[2] & 0x0000FF00) >> 8);
  1337. pCardInfo->CardCapacity = (uint64_t)(((uint64_t)pCardInfo->SD_csd.DeviceSize + 1) * 512 * 1024);
  1338. pCardInfo->CardBlockSize = 512;
  1339. }
  1340. else
  1341. {
  1342. /* Not supported card type */
  1343. errorstate = SD_ERROR;
  1344. }
  1345. pCardInfo->SD_csd.EraseGrSize = (tmp & 0x40) >> 6;
  1346. pCardInfo->SD_csd.EraseGrMul = (tmp & 0x3F) << 1;
  1347. /* Byte 11 */
  1348. tmp = (uint8_t)(hsd->CSD[2] & 0x000000FF);
  1349. pCardInfo->SD_csd.EraseGrMul |= (tmp & 0x80) >> 7;
  1350. pCardInfo->SD_csd.WrProtectGrSize = (tmp & 0x7F);
  1351. /* Byte 12 */
  1352. tmp = (uint8_t)((hsd->CSD[3] & 0xFF000000) >> 24);
  1353. pCardInfo->SD_csd.WrProtectGrEnable = (tmp & 0x80) >> 7;
  1354. pCardInfo->SD_csd.ManDeflECC = (tmp & 0x60) >> 5;
  1355. pCardInfo->SD_csd.WrSpeedFact = (tmp & 0x1C) >> 2;
  1356. pCardInfo->SD_csd.MaxWrBlockLen = (tmp & 0x03) << 2;
  1357. /* Byte 13 */
  1358. tmp = (uint8_t)((hsd->CSD[3] & 0x00FF0000) >> 16);
  1359. pCardInfo->SD_csd.MaxWrBlockLen |= (tmp & 0xC0) >> 6;
  1360. pCardInfo->SD_csd.WriteBlockPaPartial = (tmp & 0x20) >> 5;
  1361. pCardInfo->SD_csd.Reserved3 = 0;
  1362. pCardInfo->SD_csd.ContentProtectAppli = (tmp & 0x01);
  1363. /* Byte 14 */
  1364. tmp = (uint8_t)((hsd->CSD[3] & 0x0000FF00) >> 8);
  1365. pCardInfo->SD_csd.FileFormatGrouop = (tmp & 0x80) >> 7;
  1366. pCardInfo->SD_csd.CopyFlag = (tmp & 0x40) >> 6;
  1367. pCardInfo->SD_csd.PermWrProtect = (tmp & 0x20) >> 5;
  1368. pCardInfo->SD_csd.TempWrProtect = (tmp & 0x10) >> 4;
  1369. pCardInfo->SD_csd.FileFormat = (tmp & 0x0C) >> 2;
  1370. pCardInfo->SD_csd.ECC = (tmp & 0x03);
  1371. /* Byte 15 */
  1372. tmp = (uint8_t)(hsd->CSD[3] & 0x000000FF);
  1373. pCardInfo->SD_csd.CSD_CRC = (tmp & 0xFE) >> 1;
  1374. pCardInfo->SD_csd.Reserved4 = 1;
  1375. /* Byte 0 */
  1376. tmp = (uint8_t)((hsd->CID[0] & 0xFF000000) >> 24);
  1377. pCardInfo->SD_cid.ManufacturerID = tmp;
  1378. /* Byte 1 */
  1379. tmp = (uint8_t)((hsd->CID[0] & 0x00FF0000) >> 16);
  1380. pCardInfo->SD_cid.OEM_AppliID = tmp << 8;
  1381. /* Byte 2 */
  1382. tmp = (uint8_t)((hsd->CID[0] & 0x000000FF00) >> 8);
  1383. pCardInfo->SD_cid.OEM_AppliID |= tmp;
  1384. /* Byte 3 */
  1385. tmp = (uint8_t)(hsd->CID[0] & 0x000000FF);
  1386. pCardInfo->SD_cid.ProdName1 = tmp << 24;
  1387. /* Byte 4 */
  1388. tmp = (uint8_t)((hsd->CID[1] & 0xFF000000) >> 24);
  1389. pCardInfo->SD_cid.ProdName1 |= tmp << 16;
  1390. /* Byte 5 */
  1391. tmp = (uint8_t)((hsd->CID[1] & 0x00FF0000) >> 16);
  1392. pCardInfo->SD_cid.ProdName1 |= tmp << 8;
  1393. /* Byte 6 */
  1394. tmp = (uint8_t)((hsd->CID[1] & 0x0000FF00) >> 8);
  1395. pCardInfo->SD_cid.ProdName1 |= tmp;
  1396. /* Byte 7 */
  1397. tmp = (uint8_t)(hsd->CID[1] & 0x000000FF);
  1398. pCardInfo->SD_cid.ProdName2 = tmp;
  1399. /* Byte 8 */
  1400. tmp = (uint8_t)((hsd->CID[2] & 0xFF000000) >> 24);
  1401. pCardInfo->SD_cid.ProdRev = tmp;
  1402. /* Byte 9 */
  1403. tmp = (uint8_t)((hsd->CID[2] & 0x00FF0000) >> 16);
  1404. pCardInfo->SD_cid.ProdSN = tmp << 24;
  1405. /* Byte 10 */
  1406. tmp = (uint8_t)((hsd->CID[2] & 0x0000FF00) >> 8);
  1407. pCardInfo->SD_cid.ProdSN |= tmp << 16;
  1408. /* Byte 11 */
  1409. tmp = (uint8_t)(hsd->CID[2] & 0x000000FF);
  1410. pCardInfo->SD_cid.ProdSN |= tmp << 8;
  1411. /* Byte 12 */
  1412. tmp = (uint8_t)((hsd->CID[3] & 0xFF000000) >> 24);
  1413. pCardInfo->SD_cid.ProdSN |= tmp;
  1414. /* Byte 13 */
  1415. tmp = (uint8_t)((hsd->CID[3] & 0x00FF0000) >> 16);
  1416. pCardInfo->SD_cid.Reserved1 |= (tmp & 0xF0) >> 4;
  1417. pCardInfo->SD_cid.ManufactDate = (tmp & 0x0F) << 8;
  1418. /* Byte 14 */
  1419. tmp = (uint8_t)((hsd->CID[3] & 0x0000FF00) >> 8);
  1420. pCardInfo->SD_cid.ManufactDate |= tmp;
  1421. /* Byte 15 */
  1422. tmp = (uint8_t)(hsd->CID[3] & 0x000000FF);
  1423. pCardInfo->SD_cid.CID_CRC = (tmp & 0xFE) >> 1;
  1424. pCardInfo->SD_cid.Reserved2 = 1;
  1425. return errorstate;
  1426. }
  1427. /**
  1428. * @brief Enables wide bus operation for the requested card if supported by
  1429. * card.
  1430. * @param hsd: SD handle
  1431. * @param WideMode: Specifies the SD card wide bus mode
  1432. * This parameter can be one of the following values:
  1433. * @arg SDMMC_BUS_WIDE_8B: 8-bit data transfer (Only for MMC)
  1434. * @arg SDMMC_BUS_WIDE_4B: 4-bit data transfer
  1435. * @arg SDMMC_BUS_WIDE_1B: 1-bit data transfer
  1436. * @retval SD Card error state
  1437. */
  1438. HAL_SD_ErrorTypedef HAL_SD_WideBusOperation_Config(SD_HandleTypeDef *hsd, uint32_t WideMode)
  1439. {
  1440. HAL_SD_ErrorTypedef errorstate = SD_OK;
  1441. SDMMC_InitTypeDef tmpinit;
  1442. /* MMC Card does not support this feature */
  1443. if (hsd->CardType == MULTIMEDIA_CARD)
  1444. {
  1445. errorstate = SD_UNSUPPORTED_FEATURE;
  1446. return errorstate;
  1447. }
  1448. else if ((hsd->CardType == STD_CAPACITY_SD_CARD_V1_1) || (hsd->CardType == STD_CAPACITY_SD_CARD_V2_0) ||\
  1449. (hsd->CardType == HIGH_CAPACITY_SD_CARD))
  1450. {
  1451. if (WideMode == SDMMC_BUS_WIDE_8B)
  1452. {
  1453. errorstate = SD_UNSUPPORTED_FEATURE;
  1454. }
  1455. else if (WideMode == SDMMC_BUS_WIDE_4B)
  1456. {
  1457. errorstate = SD_WideBus_Enable(hsd);
  1458. }
  1459. else if (WideMode == SDMMC_BUS_WIDE_1B)
  1460. {
  1461. errorstate = SD_WideBus_Disable(hsd);
  1462. }
  1463. else
  1464. {
  1465. /* WideMode is not a valid argument*/
  1466. errorstate = SD_INVALID_PARAMETER;
  1467. }
  1468. if (errorstate == SD_OK)
  1469. {
  1470. /* Configure the SDMMC peripheral */
  1471. tmpinit.ClockEdge = hsd->Init.ClockEdge;
  1472. tmpinit.ClockBypass = hsd->Init.ClockBypass;
  1473. tmpinit.ClockPowerSave = hsd->Init.ClockPowerSave;
  1474. tmpinit.BusWide = WideMode;
  1475. tmpinit.HardwareFlowControl = hsd->Init.HardwareFlowControl;
  1476. tmpinit.ClockDiv = hsd->Init.ClockDiv;
  1477. SDMMC_Init(hsd->Instance, tmpinit);
  1478. }
  1479. }
  1480. return errorstate;
  1481. }
  1482. /**
  1483. * @brief Aborts an ongoing data transfer.
  1484. * @param hsd: SD handle
  1485. * @retval SD Card error state
  1486. */
  1487. HAL_SD_ErrorTypedef HAL_SD_StopTransfer(SD_HandleTypeDef *hsd)
  1488. {
  1489. SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure;
  1490. HAL_SD_ErrorTypedef errorstate = SD_OK;
  1491. /* Send CMD12 STOP_TRANSMISSION */
  1492. sdmmc_cmdinitstructure.Argument = 0;
  1493. sdmmc_cmdinitstructure.CmdIndex = SD_CMD_STOP_TRANSMISSION;
  1494. sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT;
  1495. sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO;
  1496. sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE;
  1497. SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
  1498. /* Check for error conditions */
  1499. errorstate = SD_CmdResp1Error(hsd, SD_CMD_STOP_TRANSMISSION);
  1500. return errorstate;
  1501. }
  1502. /**
  1503. * @brief Switches the SD card to High Speed mode.
  1504. * This API must be used after "Transfer State"
  1505. * @note This operation should be followed by the configuration
  1506. * of PLL to have SDMMCCK clock between 67 and 75 MHz
  1507. * @param hsd: SD handle
  1508. * @retval SD Card error state
  1509. */
  1510. HAL_SD_ErrorTypedef HAL_SD_HighSpeed (SD_HandleTypeDef *hsd)
  1511. {
  1512. HAL_SD_ErrorTypedef errorstate = SD_OK;
  1513. SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure;
  1514. SDMMC_DataInitTypeDef sdmmc_datainitstructure;
  1515. uint8_t SD_hs[64] = {0};
  1516. uint32_t SD_scr[2] = {0, 0};
  1517. uint32_t SD_SPEC = 0 ;
  1518. uint32_t count = 0, *tempbuff = (uint32_t *)SD_hs;
  1519. /* Initialize the Data control register */
  1520. hsd->Instance->DCTRL = 0;
  1521. /* Get SCR Register */
  1522. errorstate = SD_FindSCR(hsd, SD_scr);
  1523. if (errorstate != SD_OK)
  1524. {
  1525. return errorstate;
  1526. }
  1527. /* Test the Version supported by the card*/
  1528. SD_SPEC = (SD_scr[1] & 0x01000000) | (SD_scr[1] & 0x02000000);
  1529. if (SD_SPEC != SD_ALLZERO)
  1530. {
  1531. /* Set Block Size for Card */
  1532. sdmmc_cmdinitstructure.Argument = (uint32_t)64;
  1533. sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN;
  1534. sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT;
  1535. sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO;
  1536. sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE;
  1537. SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
  1538. /* Check for error conditions */
  1539. errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN);
  1540. if (errorstate != SD_OK)
  1541. {
  1542. return errorstate;
  1543. }
  1544. /* Configure the SD DPSM (Data Path State Machine) */
  1545. sdmmc_datainitstructure.DataTimeOut = SD_DATATIMEOUT;
  1546. sdmmc_datainitstructure.DataLength = 64;
  1547. sdmmc_datainitstructure.DataBlockSize = SDMMC_DATABLOCK_SIZE_64B ;
  1548. sdmmc_datainitstructure.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
  1549. sdmmc_datainitstructure.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  1550. sdmmc_datainitstructure.DPSM = SDMMC_DPSM_ENABLE;
  1551. SDMMC_DataConfig(hsd->Instance, &sdmmc_datainitstructure);
  1552. /* Send CMD6 switch mode */
  1553. sdmmc_cmdinitstructure.Argument = 0x80FFFF01;
  1554. sdmmc_cmdinitstructure.CmdIndex = SD_CMD_HS_SWITCH;
  1555. SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
  1556. /* Check for error conditions */
  1557. errorstate = SD_CmdResp1Error(hsd, SD_CMD_HS_SWITCH);
  1558. if (errorstate != SD_OK)
  1559. {
  1560. return errorstate;
  1561. }
  1562. while(!__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND))
  1563. {
  1564. if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF))
  1565. {
  1566. for (count = 0; count < 8; count++)
  1567. {
  1568. *(tempbuff + count) = SDMMC_ReadFIFO(hsd->Instance);
  1569. }
  1570. tempbuff += 8;
  1571. }
  1572. }
  1573. if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
  1574. {
  1575. __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT);
  1576. errorstate = SD_DATA_TIMEOUT;
  1577. return errorstate;
  1578. }
  1579. else if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
  1580. {
  1581. __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL);
  1582. errorstate = SD_DATA_CRC_FAIL;
  1583. return errorstate;
  1584. }
  1585. else if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
  1586. {
  1587. __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_RXOVERR);
  1588. errorstate = SD_RX_OVERRUN;
  1589. return errorstate;
  1590. }
  1591. else
  1592. {
  1593. /* No error flag set */
  1594. }
  1595. count = SD_DATATIMEOUT;
  1596. while ((__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXDAVL)) && (count > 0))
  1597. {
  1598. *tempbuff = SDMMC_ReadFIFO(hsd->Instance);
  1599. tempbuff++;
  1600. count--;
  1601. }
  1602. /* Clear all the static flags */
  1603. __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  1604. /* Test if the switch mode HS is ok */
  1605. if ((SD_hs[13]& 2) != 2)
  1606. {
  1607. errorstate = SD_UNSUPPORTED_FEATURE;
  1608. }
  1609. }
  1610. return errorstate;
  1611. }
  1612. /**
  1613. * @}
  1614. */
  1615. /** @addtogroup SD_Exported_Functions_Group4
  1616. * @brief Peripheral State functions
  1617. *
  1618. @verbatim
  1619. ==============================================================================
  1620. ##### Peripheral State functions #####
  1621. ==============================================================================
  1622. [..]
  1623. This subsection permits to get in runtime the status of the peripheral
  1624. and the data flow.
  1625. @endverbatim
  1626. * @{
  1627. */
  1628. /**
  1629. * @brief Returns the current SD card's status.
  1630. * @param hsd: SD handle
  1631. * @param pSDstatus: Pointer to the buffer that will contain the SD card status
  1632. * SD Status register)
  1633. * @retval SD Card error state
  1634. */
  1635. HAL_SD_ErrorTypedef HAL_SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus)
  1636. {
  1637. SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure;
  1638. SDMMC_DataInitTypeDef sdmmc_datainitstructure;
  1639. HAL_SD_ErrorTypedef errorstate = SD_OK;
  1640. uint32_t count = 0;
  1641. /* Check SD response */
  1642. if ((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SD_CARD_LOCKED) == SD_CARD_LOCKED)
  1643. {
  1644. errorstate = SD_LOCK_UNLOCK_FAILED;
  1645. return errorstate;
  1646. }
  1647. /* Set block size for card if it is not equal to current block size for card */
  1648. sdmmc_cmdinitstructure.Argument = 64;
  1649. sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN;
  1650. sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT;
  1651. sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO;
  1652. sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE;
  1653. SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
  1654. /* Check for error conditions */
  1655. errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN);
  1656. if (errorstate != SD_OK)
  1657. {
  1658. return errorstate;
  1659. }
  1660. /* Send CMD55 */
  1661. sdmmc_cmdinitstructure.Argument = (uint32_t)(hsd->RCA << 16);
  1662. sdmmc_cmdinitstructure.CmdIndex = SD_CMD_APP_CMD;
  1663. SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
  1664. /* Check for error conditions */
  1665. errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD);
  1666. if (errorstate != SD_OK)
  1667. {
  1668. return errorstate;
  1669. }
  1670. /* Configure the SD DPSM (Data Path State Machine) */
  1671. sdmmc_datainitstructure.DataTimeOut = SD_DATATIMEOUT;
  1672. sdmmc_datainitstructure.DataLength = 64;
  1673. sdmmc_datainitstructure.DataBlockSize = SDMMC_DATABLOCK_SIZE_64B;
  1674. sdmmc_datainitstructure.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
  1675. sdmmc_datainitstructure.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  1676. sdmmc_datainitstructure.DPSM = SDMMC_DPSM_ENABLE;
  1677. SDMMC_DataConfig(hsd->Instance, &sdmmc_datainitstructure);
  1678. /* Send ACMD13 (SD_APP_STAUS) with argument as card's RCA */
  1679. sdmmc_cmdinitstructure.Argument = 0;
  1680. sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SD_APP_STATUS;
  1681. SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
  1682. /* Check for error conditions */
  1683. errorstate = SD_CmdResp1Error(hsd, SD_CMD_SD_APP_STATUS);
  1684. if (errorstate != SD_OK)
  1685. {
  1686. return errorstate;
  1687. }
  1688. /* Get status data */
  1689. while(!__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND))
  1690. {
  1691. if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF))
  1692. {
  1693. for (count = 0; count < 8; count++)
  1694. {
  1695. *(pSDstatus + count) = SDMMC_ReadFIFO(hsd->Instance);
  1696. }
  1697. pSDstatus += 8;
  1698. }
  1699. }
  1700. if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
  1701. {
  1702. __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT);
  1703. errorstate = SD_DATA_TIMEOUT;
  1704. return errorstate;
  1705. }
  1706. else if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
  1707. {
  1708. __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL);
  1709. errorstate = SD_DATA_CRC_FAIL;
  1710. return errorstate;
  1711. }
  1712. else if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
  1713. {
  1714. __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_RXOVERR);
  1715. errorstate = SD_RX_OVERRUN;
  1716. return errorstate;
  1717. }
  1718. else
  1719. {
  1720. /* No error flag set */
  1721. }
  1722. count = SD_DATATIMEOUT;
  1723. while ((__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXDAVL)) && (count > 0))
  1724. {
  1725. *pSDstatus = SDMMC_ReadFIFO(hsd->Instance);
  1726. pSDstatus++;
  1727. count--;
  1728. }
  1729. /* Clear all the static status flags*/
  1730. __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  1731. return errorstate;
  1732. }
  1733. /**
  1734. * @brief Gets the current sd card data status.
  1735. * @param hsd: SD handle
  1736. * @retval Data Transfer state
  1737. */
  1738. HAL_SD_TransferStateTypedef HAL_SD_GetStatus(SD_HandleTypeDef *hsd)
  1739. {
  1740. HAL_SD_CardStateTypedef cardstate = SD_CARD_TRANSFER;
  1741. /* Get SD card state */
  1742. cardstate = SD_GetState(hsd);
  1743. /* Find SD status according to card state*/
  1744. if (cardstate == SD_CARD_TRANSFER)
  1745. {
  1746. return SD_TRANSFER_OK;
  1747. }
  1748. else if(cardstate == SD_CARD_ERROR)
  1749. {
  1750. return SD_TRANSFER_ERROR;
  1751. }
  1752. else
  1753. {
  1754. return SD_TRANSFER_BUSY;
  1755. }
  1756. }
  1757. /**
  1758. * @brief Gets the SD card status.
  1759. * @param hsd: SD handle
  1760. * @param pCardStatus: Pointer to the HAL_SD_CardStatusTypedef structure that
  1761. * will contain the SD card status information
  1762. * @retval SD Card error state
  1763. */
  1764. HAL_SD_ErrorTypedef HAL_SD_GetCardStatus(SD_HandleTypeDef *hsd, HAL_SD_CardStatusTypedef *pCardStatus)
  1765. {
  1766. HAL_SD_ErrorTypedef errorstate = SD_OK;
  1767. uint32_t tmp = 0;
  1768. uint32_t sd_status[16];
  1769. errorstate = HAL_SD_SendSDStatus(hsd, sd_status);
  1770. if (errorstate != SD_OK)
  1771. {
  1772. return errorstate;
  1773. }
  1774. /* Byte 0 */
  1775. tmp = (sd_status[0] & 0xC0) >> 6;
  1776. pCardStatus->DAT_BUS_WIDTH = (uint8_t)tmp;
  1777. /* Byte 0 */
  1778. tmp = (sd_status[0] & 0x20) >> 5;
  1779. pCardStatus->SECURED_MODE = (uint8_t)tmp;
  1780. /* Byte 2 */
  1781. tmp = (sd_status[2] & 0xFF);
  1782. pCardStatus->SD_CARD_TYPE = (uint8_t)(tmp << 8);
  1783. /* Byte 3 */
  1784. tmp = (sd_status[3] & 0xFF);
  1785. pCardStatus->SD_CARD_TYPE |= (uint8_t)tmp;
  1786. /* Byte 4 */
  1787. tmp = (sd_status[4] & 0xFF);
  1788. pCardStatus->SIZE_OF_PROTECTED_AREA = (uint8_t)(tmp << 24);
  1789. /* Byte 5 */
  1790. tmp = (sd_status[5] & 0xFF);
  1791. pCardStatus->SIZE_OF_PROTECTED_AREA |= (uint8_t)(tmp << 16);
  1792. /* Byte 6 */
  1793. tmp = (sd_status[6] & 0xFF);
  1794. pCardStatus->SIZE_OF_PROTECTED_AREA |= (uint8_t)(tmp << 8);
  1795. /* Byte 7 */
  1796. tmp = (sd_status[7] & 0xFF);
  1797. pCardStatus->SIZE_OF_PROTECTED_AREA |= (uint8_t)tmp;
  1798. /* Byte 8 */
  1799. tmp = (sd_status[8] & 0xFF);
  1800. pCardStatus->SPEED_CLASS = (uint8_t)tmp;
  1801. /* Byte 9 */
  1802. tmp = (sd_status[9] & 0xFF);
  1803. pCardStatus->PERFORMANCE_MOVE = (uint8_t)tmp;
  1804. /* Byte 10 */
  1805. tmp = (sd_status[10] & 0xF0) >> 4;
  1806. pCardStatus->AU_SIZE = (uint8_t)tmp;
  1807. /* Byte 11 */
  1808. tmp = (sd_status[11] & 0xFF);
  1809. pCardStatus->ERASE_SIZE = (uint8_t)(tmp << 8);
  1810. /* Byte 12 */
  1811. tmp = (sd_status[12] & 0xFF);
  1812. pCardStatus->ERASE_SIZE |= (uint8_t)tmp;
  1813. /* Byte 13 */
  1814. tmp = (sd_status[13] & 0xFC) >> 2;
  1815. pCardStatus->ERASE_TIMEOUT = (uint8_t)tmp;
  1816. /* Byte 13 */
  1817. tmp = (sd_status[13] & 0x3);
  1818. pCardStatus->ERASE_OFFSET = (uint8_t)tmp;
  1819. return errorstate;
  1820. }
  1821. /**
  1822. * @}
  1823. */
  1824. /**
  1825. * @}
  1826. */
  1827. /* Private function ----------------------------------------------------------*/
  1828. /** @addtogroup SD_Private_Functions
  1829. * @{
  1830. */
  1831. /**
  1832. * @brief SD DMA transfer complete Rx callback.
  1833. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1834. * the configuration information for the specified DMA module.
  1835. * @retval None
  1836. */
  1837. static void SD_DMA_RxCplt(DMA_HandleTypeDef *hdma)
  1838. {
  1839. SD_HandleTypeDef *hsd = (SD_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1840. /* DMA transfer is complete */
  1841. hsd->DmaTransferCplt = 1;
  1842. /* Wait until SD transfer is complete */
  1843. while(hsd->SdTransferCplt == 0)
  1844. {
  1845. }
  1846. /* Disable the DMA channel */
  1847. HAL_DMA_Abort(hdma);
  1848. /* Transfer complete user callback */
  1849. HAL_SD_DMA_RxCpltCallback(hsd->hdmarx);
  1850. }
  1851. /**
  1852. * @brief SD DMA transfer Error Rx callback.
  1853. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1854. * the configuration information for the specified DMA module.
  1855. * @retval None
  1856. */
  1857. static void SD_DMA_RxError(DMA_HandleTypeDef *hdma)
  1858. {
  1859. SD_HandleTypeDef *hsd = (SD_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1860. /* Transfer complete user callback */
  1861. HAL_SD_DMA_RxErrorCallback(hsd->hdmarx);
  1862. }
  1863. /**
  1864. * @brief SD DMA transfer complete Tx callback.
  1865. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1866. * the configuration information for the specified DMA module.
  1867. * @retval None
  1868. */
  1869. static void SD_DMA_TxCplt(DMA_HandleTypeDef *hdma)
  1870. {
  1871. SD_HandleTypeDef *hsd = (SD_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1872. /* DMA transfer is complete */
  1873. hsd->DmaTransferCplt = 1;
  1874. /* Wait until SD transfer is complete */
  1875. while(hsd->SdTransferCplt == 0)
  1876. {
  1877. }
  1878. /* Disable the DMA channel */
  1879. HAL_DMA_Abort(hdma);
  1880. /* Transfer complete user callback */
  1881. HAL_SD_DMA_TxCpltCallback(hsd->hdmatx);
  1882. }
  1883. /**
  1884. * @brief SD DMA transfer Error Tx callback.
  1885. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1886. * the configuration information for the specified DMA module.
  1887. * @retval None
  1888. */
  1889. static void SD_DMA_TxError(DMA_HandleTypeDef *hdma)
  1890. {
  1891. SD_HandleTypeDef *hsd = ( SD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1892. /* Transfer complete user callback */
  1893. HAL_SD_DMA_TxErrorCallback(hsd->hdmatx);
  1894. }
  1895. /**
  1896. * @brief Returns the SD current state.
  1897. * @param hsd: SD handle
  1898. * @retval SD card current state
  1899. */
  1900. static HAL_SD_CardStateTypedef SD_GetState(SD_HandleTypeDef *hsd)
  1901. {
  1902. uint32_t resp1 = 0;
  1903. if (SD_SendStatus(hsd, &resp1) != SD_OK)
  1904. {
  1905. return SD_CARD_ERROR;
  1906. }
  1907. else
  1908. {
  1909. return (HAL_SD_CardStateTypedef)((resp1 >> 9) & 0x0F);
  1910. }
  1911. }
  1912. /**
  1913. * @brief Initializes all cards or single card as the case may be Card(s) come
  1914. * into standby state.
  1915. * @param hsd: SD handle
  1916. * @retval SD Card error state
  1917. */
  1918. static HAL_SD_ErrorTypedef SD_Initialize_Cards(SD_HandleTypeDef *hsd)
  1919. {
  1920. SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure;
  1921. HAL_SD_ErrorTypedef errorstate = SD_OK;
  1922. uint16_t sd_rca = 1;
  1923. if(SDMMC_GetPowerState(hsd->Instance) == 0) /* Power off */
  1924. {
  1925. errorstate = SD_REQUEST_NOT_APPLICABLE;
  1926. return errorstate;
  1927. }
  1928. if(hsd->CardType != SECURE_DIGITAL_IO_CARD)
  1929. {
  1930. /* Send CMD2 ALL_SEND_CID */
  1931. sdmmc_cmdinitstructure.Argument = 0;
  1932. sdmmc_cmdinitstructure.CmdIndex = SD_CMD_ALL_SEND_CID;
  1933. sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_LONG;
  1934. sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO;
  1935. sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE;
  1936. SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
  1937. /* Check for error conditions */
  1938. errorstate = SD_CmdResp2Error(hsd);
  1939. if(errorstate != SD_OK)
  1940. {
  1941. return errorstate;
  1942. }
  1943. /* Get Card identification number data */
  1944. hsd->CID[0] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
  1945. hsd->CID[1] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP2);
  1946. hsd->CID[2] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP3);
  1947. hsd->CID[3] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP4);
  1948. }
  1949. if((hsd->CardType == STD_CAPACITY_SD_CARD_V1_1) || (hsd->CardType == STD_CAPACITY_SD_CARD_V2_0) ||\
  1950. (hsd->CardType == SECURE_DIGITAL_IO_COMBO_CARD) || (hsd->CardType == HIGH_CAPACITY_SD_CARD))
  1951. {
  1952. /* Send CMD3 SET_REL_ADDR with argument 0 */
  1953. /* SD Card publishes its RCA. */
  1954. sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SET_REL_ADDR;
  1955. sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT;
  1956. SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
  1957. /* Check for error conditions */
  1958. errorstate = SD_CmdResp6Error(hsd, SD_CMD_SET_REL_ADDR, &sd_rca);
  1959. if(errorstate != SD_OK)
  1960. {
  1961. return errorstate;
  1962. }
  1963. }
  1964. if (hsd->CardType != SECURE_DIGITAL_IO_CARD)
  1965. {
  1966. /* Get the SD card RCA */
  1967. hsd->RCA = sd_rca;
  1968. /* Send CMD9 SEND_CSD with argument as card's RCA */
  1969. sdmmc_cmdinitstructure.Argument = (uint32_t)(hsd->RCA << 16);
  1970. sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SEND_CSD;
  1971. sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_LONG;
  1972. SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
  1973. /* Check for error conditions */
  1974. errorstate = SD_CmdResp2Error(hsd);
  1975. if(errorstate != SD_OK)
  1976. {
  1977. return errorstate;
  1978. }
  1979. /* Get Card Specific Data */
  1980. hsd->CSD[0] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
  1981. hsd->CSD[1] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP2);
  1982. hsd->CSD[2] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP3);
  1983. hsd->CSD[3] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP4);
  1984. }
  1985. /* All cards are initialized */
  1986. return errorstate;
  1987. }
  1988. /**
  1989. * @brief Selects or Deselects the corresponding card.
  1990. * @param hsd: SD handle
  1991. * @param addr: Address of the card to be selected
  1992. * @retval SD Card error state
  1993. */
  1994. static HAL_SD_ErrorTypedef SD_Select_Deselect(SD_HandleTypeDef *hsd, uint64_t addr)
  1995. {
  1996. SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure;
  1997. HAL_SD_ErrorTypedef errorstate = SD_OK;
  1998. /* Send CMD7 SDMMC_SEL_DESEL_CARD */
  1999. sdmmc_cmdinitstructure.Argument = (uint32_t)addr;
  2000. sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SEL_DESEL_CARD;
  2001. sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT;
  2002. sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO;
  2003. sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE;
  2004. SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
  2005. /* Check for error conditions */
  2006. errorstate = SD_CmdResp1Error(hsd, SD_CMD_SEL_DESEL_CARD);
  2007. return errorstate;
  2008. }
  2009. /**
  2010. * @brief Enquires cards about their operating voltage and configures clock
  2011. * controls and stores SD information that will be needed in future
  2012. * in the SD handle.
  2013. * @param hsd: SD handle
  2014. * @retval SD Card error state
  2015. */
  2016. static HAL_SD_ErrorTypedef SD_PowerON(SD_HandleTypeDef *hsd)
  2017. {
  2018. SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure;
  2019. __IO HAL_SD_ErrorTypedef errorstate = SD_OK;
  2020. uint32_t response = 0, count = 0, validvoltage = 0;
  2021. uint32_t sdtype = SD_STD_CAPACITY;
  2022. /* Power ON Sequence -------------------------------------------------------*/
  2023. /* Disable SDMMC Clock */
  2024. __HAL_SD_SDMMC_DISABLE(hsd);
  2025. /* Set Power State to ON */
  2026. SDMMC_PowerState_ON(hsd->Instance);
  2027. /* 1ms: required power up waiting time before starting the SD initialization
  2028. sequence */
  2029. HAL_Delay(1);
  2030. /* Enable SDMMC Clock */
  2031. __HAL_SD_SDMMC_ENABLE(hsd);
  2032. /* CMD0: GO_IDLE_STATE -----------------------------------------------------*/
  2033. /* No CMD response required */
  2034. sdmmc_cmdinitstructure.Argument = 0;
  2035. sdmmc_cmdinitstructure.CmdIndex = SD_CMD_GO_IDLE_STATE;
  2036. sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_NO;
  2037. sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO;
  2038. sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE;
  2039. SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
  2040. /* Check for error conditions */
  2041. errorstate = SD_CmdError(hsd);
  2042. if(errorstate != SD_OK)
  2043. {
  2044. /* CMD Response Timeout (wait for CMDSENT flag) */
  2045. return errorstate;
  2046. }
  2047. /* CMD8: SEND_IF_COND ------------------------------------------------------*/
  2048. /* Send CMD8 to verify SD card interface operating condition */
  2049. /* Argument: - [31:12]: Reserved (shall be set to '0')
  2050. - [11:8]: Supply Voltage (VHS) 0x1 (Range: 2.7-3.6 V)
  2051. - [7:0]: Check Pattern (recommended 0xAA) */
  2052. /* CMD Response: R7 */
  2053. sdmmc_cmdinitstructure.Argument = SD_CHECK_PATTERN;
  2054. sdmmc_cmdinitstructure.CmdIndex = SD_SDMMC_SEND_IF_COND;
  2055. sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT;
  2056. SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
  2057. /* Check for error conditions */
  2058. errorstate = SD_CmdResp7Error(hsd);
  2059. if (errorstate == SD_OK)
  2060. {
  2061. /* SD Card 2.0 */
  2062. hsd->CardType = STD_CAPACITY_SD_CARD_V2_0;
  2063. sdtype = SD_HIGH_CAPACITY;
  2064. }
  2065. /* Send CMD55 */
  2066. sdmmc_cmdinitstructure.Argument = 0;
  2067. sdmmc_cmdinitstructure.CmdIndex = SD_CMD_APP_CMD;
  2068. SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
  2069. /* Check for error conditions */
  2070. errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD);
  2071. /* If errorstate is Command Timeout, it is a MMC card */
  2072. /* If errorstate is SD_OK it is a SD card: SD card 2.0 (voltage range mismatch)
  2073. or SD card 1.x */
  2074. if(errorstate == SD_OK)
  2075. {
  2076. /* SD CARD */
  2077. /* Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */
  2078. while((!validvoltage) && (count < SD_MAX_VOLT_TRIAL))
  2079. {
  2080. /* SEND CMD55 APP_CMD with RCA as 0 */
  2081. sdmmc_cmdinitstructure.Argument = 0;
  2082. sdmmc_cmdinitstructure.CmdIndex = SD_CMD_APP_CMD;
  2083. sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT;
  2084. sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO;
  2085. sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE;
  2086. SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
  2087. /* Check for error conditions */
  2088. errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD);
  2089. if(errorstate != SD_OK)
  2090. {
  2091. return errorstate;
  2092. }
  2093. /* Send CMD41 */
  2094. sdmmc_cmdinitstructure.Argument = SD_VOLTAGE_WINDOW_SD | sdtype;
  2095. sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SD_APP_OP_COND;
  2096. sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT;
  2097. sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO;
  2098. sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE;
  2099. SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
  2100. /* Check for error conditions */
  2101. errorstate = SD_CmdResp3Error(hsd);
  2102. if(errorstate != SD_OK)
  2103. {
  2104. return errorstate;
  2105. }
  2106. /* Get command response */
  2107. response = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
  2108. /* Get operating voltage*/
  2109. validvoltage = (((response >> 31) == 1) ? 1 : 0);
  2110. count++;
  2111. }
  2112. if(count >= SD_MAX_VOLT_TRIAL)
  2113. {
  2114. errorstate = SD_INVALID_VOLTRANGE;
  2115. return errorstate;
  2116. }
  2117. if((response & SD_HIGH_CAPACITY) == SD_HIGH_CAPACITY) /* (response &= SD_HIGH_CAPACITY) */
  2118. {
  2119. hsd->CardType = HIGH_CAPACITY_SD_CARD;
  2120. }
  2121. } /* else MMC Card */
  2122. return errorstate;
  2123. }
  2124. /**
  2125. * @brief Turns the SDMMC output signals off.
  2126. * @param hsd: SD handle
  2127. * @retval SD Card error state
  2128. */
  2129. static HAL_SD_ErrorTypedef SD_PowerOFF(SD_HandleTypeDef *hsd)
  2130. {
  2131. HAL_SD_ErrorTypedef errorstate = SD_OK;
  2132. /* Set Power State to OFF */
  2133. SDMMC_PowerState_OFF(hsd->Instance);
  2134. return errorstate;
  2135. }
  2136. /**
  2137. * @brief Returns the current card's status.
  2138. * @param hsd: SD handle
  2139. * @param pCardStatus: pointer to the buffer that will contain the SD card
  2140. * status (Card Status register)
  2141. * @retval SD Card error state
  2142. */
  2143. static HAL_SD_ErrorTypedef SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus)
  2144. {
  2145. SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure;
  2146. HAL_SD_ErrorTypedef errorstate = SD_OK;
  2147. if(pCardStatus == NULL)
  2148. {
  2149. errorstate = SD_INVALID_PARAMETER;
  2150. return errorstate;
  2151. }
  2152. /* Send Status command */
  2153. sdmmc_cmdinitstructure.Argument = (uint32_t)(hsd->RCA << 16);
  2154. sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SEND_STATUS;
  2155. sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT;
  2156. sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO;
  2157. sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE;
  2158. SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
  2159. /* Check for error conditions */
  2160. errorstate = SD_CmdResp1Error(hsd, SD_CMD_SEND_STATUS);
  2161. if(errorstate != SD_OK)
  2162. {
  2163. return errorstate;
  2164. }
  2165. /* Get SD card status */
  2166. *pCardStatus = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
  2167. return errorstate;
  2168. }
  2169. /**
  2170. * @brief Checks for error conditions for CMD0.
  2171. * @param hsd: SD handle
  2172. * @retval SD Card error state
  2173. */
  2174. static HAL_SD_ErrorTypedef SD_CmdError(SD_HandleTypeDef *hsd)
  2175. {
  2176. HAL_SD_ErrorTypedef errorstate = SD_OK;
  2177. uint32_t timeout, tmp;
  2178. timeout = SDMMC_CMD0TIMEOUT;
  2179. tmp = __HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CMDSENT);
  2180. while((timeout > 0) && (!tmp))
  2181. {
  2182. tmp = __HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CMDSENT);
  2183. timeout--;
  2184. }
  2185. if(timeout == 0)
  2186. {
  2187. errorstate = SD_CMD_RSP_TIMEOUT;
  2188. return errorstate;
  2189. }
  2190. /* Clear all the static flags */
  2191. __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  2192. return errorstate;
  2193. }
  2194. /**
  2195. * @brief Checks for error conditions for R7 response.
  2196. * @param hsd: SD handle
  2197. * @retval SD Card error state
  2198. */
  2199. static HAL_SD_ErrorTypedef SD_CmdResp7Error(SD_HandleTypeDef *hsd)
  2200. {
  2201. HAL_SD_ErrorTypedef errorstate = SD_ERROR;
  2202. uint32_t timeout = SDMMC_CMD0TIMEOUT, tmp;
  2203. tmp = __HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT);
  2204. while((!tmp) && (timeout > 0))
  2205. {
  2206. tmp = __HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT);
  2207. timeout--;
  2208. }
  2209. tmp = __HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CTIMEOUT);
  2210. if((timeout == 0) || tmp)
  2211. {
  2212. /* Card is not V2.0 compliant or card does not support the set voltage range */
  2213. errorstate = SD_CMD_RSP_TIMEOUT;
  2214. __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_CTIMEOUT);
  2215. return errorstate;
  2216. }
  2217. if(__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CMDREND))
  2218. {
  2219. /* Card is SD V2.0 compliant */
  2220. errorstate = SD_OK;
  2221. __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_CMDREND);
  2222. return errorstate;
  2223. }
  2224. return errorstate;
  2225. }
  2226. /**
  2227. * @brief Checks for error conditions for R1 response.
  2228. * @param hsd: SD handle
  2229. * @param SD_CMD: The sent command index
  2230. * @retval SD Card error state
  2231. */
  2232. static HAL_SD_ErrorTypedef SD_CmdResp1Error(SD_HandleTypeDef *hsd, uint8_t SD_CMD)
  2233. {
  2234. HAL_SD_ErrorTypedef errorstate = SD_OK;
  2235. uint32_t response_r1;
  2236. while(!__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT))
  2237. {
  2238. }
  2239. if(__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CTIMEOUT))
  2240. {
  2241. errorstate = SD_CMD_RSP_TIMEOUT;
  2242. __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_CTIMEOUT);
  2243. return errorstate;
  2244. }
  2245. else if(__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CCRCFAIL))
  2246. {
  2247. errorstate = SD_CMD_CRC_FAIL;
  2248. __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_CCRCFAIL);
  2249. return errorstate;
  2250. }
  2251. /* Check response received is of desired command */
  2252. if(SDMMC_GetCommandResponse(hsd->Instance) != SD_CMD)
  2253. {
  2254. errorstate = SD_ILLEGAL_CMD;
  2255. return errorstate;
  2256. }
  2257. /* Clear all the static flags */
  2258. __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  2259. /* We have received response, retrieve it for analysis */
  2260. response_r1 = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
  2261. if((response_r1 & SD_OCR_ERRORBITS) == SD_ALLZERO)
  2262. {
  2263. return errorstate;
  2264. }
  2265. if((response_r1 & SD_OCR_ADDR_OUT_OF_RANGE) == SD_OCR_ADDR_OUT_OF_RANGE)
  2266. {
  2267. return(SD_ADDR_OUT_OF_RANGE);
  2268. }
  2269. if((response_r1 & SD_OCR_ADDR_MISALIGNED) == SD_OCR_ADDR_MISALIGNED)
  2270. {
  2271. return(SD_ADDR_MISALIGNED);
  2272. }
  2273. if((response_r1 & SD_OCR_BLOCK_LEN_ERR) == SD_OCR_BLOCK_LEN_ERR)
  2274. {
  2275. return(SD_BLOCK_LEN_ERR);
  2276. }
  2277. if((response_r1 & SD_OCR_ERASE_SEQ_ERR) == SD_OCR_ERASE_SEQ_ERR)
  2278. {
  2279. return(SD_ERASE_SEQ_ERR);
  2280. }
  2281. if((response_r1 & SD_OCR_BAD_ERASE_PARAM) == SD_OCR_BAD_ERASE_PARAM)
  2282. {
  2283. return(SD_BAD_ERASE_PARAM);
  2284. }
  2285. if((response_r1 & SD_OCR_WRITE_PROT_VIOLATION) == SD_OCR_WRITE_PROT_VIOLATION)
  2286. {
  2287. return(SD_WRITE_PROT_VIOLATION);
  2288. }
  2289. if((response_r1 & SD_OCR_LOCK_UNLOCK_FAILED) == SD_OCR_LOCK_UNLOCK_FAILED)
  2290. {
  2291. return(SD_LOCK_UNLOCK_FAILED);
  2292. }
  2293. if((response_r1 & SD_OCR_COM_CRC_FAILED) == SD_OCR_COM_CRC_FAILED)
  2294. {
  2295. return(SD_COM_CRC_FAILED);
  2296. }
  2297. if((response_r1 & SD_OCR_ILLEGAL_CMD) == SD_OCR_ILLEGAL_CMD)
  2298. {
  2299. return(SD_ILLEGAL_CMD);
  2300. }
  2301. if((response_r1 & SD_OCR_CARD_ECC_FAILED) == SD_OCR_CARD_ECC_FAILED)
  2302. {
  2303. return(SD_CARD_ECC_FAILED);
  2304. }
  2305. if((response_r1 & SD_OCR_CC_ERROR) == SD_OCR_CC_ERROR)
  2306. {
  2307. return(SD_CC_ERROR);
  2308. }
  2309. if((response_r1 & SD_OCR_GENERAL_UNKNOWN_ERROR) == SD_OCR_GENERAL_UNKNOWN_ERROR)
  2310. {
  2311. return(SD_GENERAL_UNKNOWN_ERROR);
  2312. }
  2313. if((response_r1 & SD_OCR_STREAM_READ_UNDERRUN) == SD_OCR_STREAM_READ_UNDERRUN)
  2314. {
  2315. return(SD_STREAM_READ_UNDERRUN);
  2316. }
  2317. if((response_r1 & SD_OCR_STREAM_WRITE_OVERRUN) == SD_OCR_STREAM_WRITE_OVERRUN)
  2318. {
  2319. return(SD_STREAM_WRITE_OVERRUN);
  2320. }
  2321. if((response_r1 & SD_OCR_CID_CSD_OVERWRITE) == SD_OCR_CID_CSD_OVERWRITE)
  2322. {
  2323. return(SD_CID_CSD_OVERWRITE);
  2324. }
  2325. if((response_r1 & SD_OCR_WP_ERASE_SKIP) == SD_OCR_WP_ERASE_SKIP)
  2326. {
  2327. return(SD_WP_ERASE_SKIP);
  2328. }
  2329. if((response_r1 & SD_OCR_CARD_ECC_DISABLED) == SD_OCR_CARD_ECC_DISABLED)
  2330. {
  2331. return(SD_CARD_ECC_DISABLED);
  2332. }
  2333. if((response_r1 & SD_OCR_ERASE_RESET) == SD_OCR_ERASE_RESET)
  2334. {
  2335. return(SD_ERASE_RESET);
  2336. }
  2337. if((response_r1 & SD_OCR_AKE_SEQ_ERROR) == SD_OCR_AKE_SEQ_ERROR)
  2338. {
  2339. return(SD_AKE_SEQ_ERROR);
  2340. }
  2341. return errorstate;
  2342. }
  2343. /**
  2344. * @brief Checks for error conditions for R3 (OCR) response.
  2345. * @param hsd: SD handle
  2346. * @retval SD Card error state
  2347. */
  2348. static HAL_SD_ErrorTypedef SD_CmdResp3Error(SD_HandleTypeDef *hsd)
  2349. {
  2350. HAL_SD_ErrorTypedef errorstate = SD_OK;
  2351. while (!__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT))
  2352. {
  2353. }
  2354. if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CTIMEOUT))
  2355. {
  2356. errorstate = SD_CMD_RSP_TIMEOUT;
  2357. __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_CTIMEOUT);
  2358. return errorstate;
  2359. }
  2360. /* Clear all the static flags */
  2361. __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  2362. return errorstate;
  2363. }
  2364. /**
  2365. * @brief Checks for error conditions for R2 (CID or CSD) response.
  2366. * @param hsd: SD handle
  2367. * @retval SD Card error state
  2368. */
  2369. static HAL_SD_ErrorTypedef SD_CmdResp2Error(SD_HandleTypeDef *hsd)
  2370. {
  2371. HAL_SD_ErrorTypedef errorstate = SD_OK;
  2372. while (!__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT))
  2373. {
  2374. }
  2375. if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CTIMEOUT))
  2376. {
  2377. errorstate = SD_CMD_RSP_TIMEOUT;
  2378. __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_CTIMEOUT);
  2379. return errorstate;
  2380. }
  2381. else if (__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CCRCFAIL))
  2382. {
  2383. errorstate = SD_CMD_CRC_FAIL;
  2384. __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_CCRCFAIL);
  2385. return errorstate;
  2386. }
  2387. else
  2388. {
  2389. /* No error flag set */
  2390. }
  2391. /* Clear all the static flags */
  2392. __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  2393. return errorstate;
  2394. }
  2395. /**
  2396. * @brief Checks for error conditions for R6 (RCA) response.
  2397. * @param hsd: SD handle
  2398. * @param SD_CMD: The sent command index
  2399. * @param pRCA: Pointer to the variable that will contain the SD card relative
  2400. * address RCA
  2401. * @retval SD Card error state
  2402. */
  2403. static HAL_SD_ErrorTypedef SD_CmdResp6Error(SD_HandleTypeDef *hsd, uint8_t SD_CMD, uint16_t *pRCA)
  2404. {
  2405. HAL_SD_ErrorTypedef errorstate = SD_OK;
  2406. uint32_t response_r1;
  2407. while(!__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT))
  2408. {
  2409. }
  2410. if(__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CTIMEOUT))
  2411. {
  2412. errorstate = SD_CMD_RSP_TIMEOUT;
  2413. __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_CTIMEOUT);
  2414. return errorstate;
  2415. }
  2416. else if(__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CCRCFAIL))
  2417. {
  2418. errorstate = SD_CMD_CRC_FAIL;
  2419. __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_CCRCFAIL);
  2420. return errorstate;
  2421. }
  2422. else
  2423. {
  2424. /* No error flag set */
  2425. }
  2426. /* Check response received is of desired command */
  2427. if(SDMMC_GetCommandResponse(hsd->Instance) != SD_CMD)
  2428. {
  2429. errorstate = SD_ILLEGAL_CMD;
  2430. return errorstate;
  2431. }
  2432. /* Clear all the static flags */
  2433. __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  2434. /* We have received response, retrieve it. */
  2435. response_r1 = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
  2436. if((response_r1 & (SD_R6_GENERAL_UNKNOWN_ERROR | SD_R6_ILLEGAL_CMD | SD_R6_COM_CRC_FAILED)) == SD_ALLZERO)
  2437. {
  2438. *pRCA = (uint16_t) (response_r1 >> 16);
  2439. return errorstate;
  2440. }
  2441. if((response_r1 & SD_R6_GENERAL_UNKNOWN_ERROR) == SD_R6_GENERAL_UNKNOWN_ERROR)
  2442. {
  2443. return(SD_GENERAL_UNKNOWN_ERROR);
  2444. }
  2445. if((response_r1 & SD_R6_ILLEGAL_CMD) == SD_R6_ILLEGAL_CMD)
  2446. {
  2447. return(SD_ILLEGAL_CMD);
  2448. }
  2449. if((response_r1 & SD_R6_COM_CRC_FAILED) == SD_R6_COM_CRC_FAILED)
  2450. {
  2451. return(SD_COM_CRC_FAILED);
  2452. }
  2453. return errorstate;
  2454. }
  2455. /**
  2456. * @brief Enables the SDMMC wide bus mode.
  2457. * @param hsd: SD handle
  2458. * @retval SD Card error state
  2459. */
  2460. static HAL_SD_ErrorTypedef SD_WideBus_Enable(SD_HandleTypeDef *hsd)
  2461. {
  2462. SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure;
  2463. HAL_SD_ErrorTypedef errorstate = SD_OK;
  2464. uint32_t scr[2] = {0, 0};
  2465. if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SD_CARD_LOCKED) == SD_CARD_LOCKED)
  2466. {
  2467. errorstate = SD_LOCK_UNLOCK_FAILED;
  2468. return errorstate;
  2469. }
  2470. /* Get SCR Register */
  2471. errorstate = SD_FindSCR(hsd, scr);
  2472. if(errorstate != SD_OK)
  2473. {
  2474. return errorstate;
  2475. }
  2476. /* If requested card supports wide bus operation */
  2477. if((scr[1] & SD_WIDE_BUS_SUPPORT) != SD_ALLZERO)
  2478. {
  2479. /* Send CMD55 APP_CMD with argument as card's RCA.*/
  2480. sdmmc_cmdinitstructure.Argument = (uint32_t)(hsd->RCA << 16);
  2481. sdmmc_cmdinitstructure.CmdIndex = SD_CMD_APP_CMD;
  2482. sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT;
  2483. sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO;
  2484. sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE;
  2485. SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
  2486. /* Check for error conditions */
  2487. errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD);
  2488. if(errorstate != SD_OK)
  2489. {
  2490. return errorstate;
  2491. }
  2492. /* Send ACMD6 APP_CMD with argument as 2 for wide bus mode */
  2493. sdmmc_cmdinitstructure.Argument = 2;
  2494. sdmmc_cmdinitstructure.CmdIndex = SD_CMD_APP_SD_SET_BUSWIDTH;
  2495. SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
  2496. /* Check for error conditions */
  2497. errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_SD_SET_BUSWIDTH);
  2498. if(errorstate != SD_OK)
  2499. {
  2500. return errorstate;
  2501. }
  2502. return errorstate;
  2503. }
  2504. else
  2505. {
  2506. errorstate = SD_REQUEST_NOT_APPLICABLE;
  2507. return errorstate;
  2508. }
  2509. }
  2510. /**
  2511. * @brief Disables the SDMMC wide bus mode.
  2512. * @param hsd: SD handle
  2513. * @retval SD Card error state
  2514. */
  2515. static HAL_SD_ErrorTypedef SD_WideBus_Disable(SD_HandleTypeDef *hsd)
  2516. {
  2517. SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure;
  2518. HAL_SD_ErrorTypedef errorstate = SD_OK;
  2519. uint32_t scr[2] = {0, 0};
  2520. if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SD_CARD_LOCKED) == SD_CARD_LOCKED)
  2521. {
  2522. errorstate = SD_LOCK_UNLOCK_FAILED;
  2523. return errorstate;
  2524. }
  2525. /* Get SCR Register */
  2526. errorstate = SD_FindSCR(hsd, scr);
  2527. if(errorstate != SD_OK)
  2528. {
  2529. return errorstate;
  2530. }
  2531. /* If requested card supports 1 bit mode operation */
  2532. if((scr[1] & SD_SINGLE_BUS_SUPPORT) != SD_ALLZERO)
  2533. {
  2534. /* Send CMD55 APP_CMD with argument as card's RCA */
  2535. sdmmc_cmdinitstructure.Argument = (uint32_t)(hsd->RCA << 16);
  2536. sdmmc_cmdinitstructure.CmdIndex = SD_CMD_APP_CMD;
  2537. sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT;
  2538. sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO;
  2539. sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE;
  2540. SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
  2541. /* Check for error conditions */
  2542. errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD);
  2543. if(errorstate != SD_OK)
  2544. {
  2545. return errorstate;
  2546. }
  2547. /* Send ACMD6 APP_CMD with argument as 0 for single bus mode */
  2548. sdmmc_cmdinitstructure.Argument = 0;
  2549. sdmmc_cmdinitstructure.CmdIndex = SD_CMD_APP_SD_SET_BUSWIDTH;
  2550. SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
  2551. /* Check for error conditions */
  2552. errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_SD_SET_BUSWIDTH);
  2553. if(errorstate != SD_OK)
  2554. {
  2555. return errorstate;
  2556. }
  2557. return errorstate;
  2558. }
  2559. else
  2560. {
  2561. errorstate = SD_REQUEST_NOT_APPLICABLE;
  2562. return errorstate;
  2563. }
  2564. }
  2565. /**
  2566. * @brief Finds the SD card SCR register value.
  2567. * @param hsd: SD handle
  2568. * @param pSCR: pointer to the buffer that will contain the SCR value
  2569. * @retval SD Card error state
  2570. */
  2571. static HAL_SD_ErrorTypedef SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR)
  2572. {
  2573. SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure;
  2574. SDMMC_DataInitTypeDef sdmmc_datainitstructure;
  2575. HAL_SD_ErrorTypedef errorstate = SD_OK;
  2576. uint32_t index = 0;
  2577. uint32_t tempscr[2] = {0, 0};
  2578. /* Set Block Size To 8 Bytes */
  2579. /* Send CMD55 APP_CMD with argument as card's RCA */
  2580. sdmmc_cmdinitstructure.Argument = (uint32_t)8;
  2581. sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN;
  2582. sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT;
  2583. sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO;
  2584. sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE;
  2585. SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
  2586. /* Check for error conditions */
  2587. errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN);
  2588. if(errorstate != SD_OK)
  2589. {
  2590. return errorstate;
  2591. }
  2592. /* Send CMD55 APP_CMD with argument as card's RCA */
  2593. sdmmc_cmdinitstructure.Argument = (uint32_t)((hsd->RCA) << 16);
  2594. sdmmc_cmdinitstructure.CmdIndex = SD_CMD_APP_CMD;
  2595. SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
  2596. /* Check for error conditions */
  2597. errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD);
  2598. if(errorstate != SD_OK)
  2599. {
  2600. return errorstate;
  2601. }
  2602. sdmmc_datainitstructure.DataTimeOut = SD_DATATIMEOUT;
  2603. sdmmc_datainitstructure.DataLength = 8;
  2604. sdmmc_datainitstructure.DataBlockSize = SDMMC_DATABLOCK_SIZE_8B;
  2605. sdmmc_datainitstructure.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
  2606. sdmmc_datainitstructure.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  2607. sdmmc_datainitstructure.DPSM = SDMMC_DPSM_ENABLE;
  2608. SDMMC_DataConfig(hsd->Instance, &sdmmc_datainitstructure);
  2609. /* Send ACMD51 SD_APP_SEND_SCR with argument as 0 */
  2610. sdmmc_cmdinitstructure.Argument = 0;
  2611. sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SD_APP_SEND_SCR;
  2612. SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
  2613. /* Check for error conditions */
  2614. errorstate = SD_CmdResp1Error(hsd, SD_CMD_SD_APP_SEND_SCR);
  2615. if(errorstate != SD_OK)
  2616. {
  2617. return errorstate;
  2618. }
  2619. while(!__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND))
  2620. {
  2621. if(__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXDAVL))
  2622. {
  2623. *(tempscr + index) = SDMMC_ReadFIFO(hsd->Instance);
  2624. index++;
  2625. }
  2626. }
  2627. if(__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
  2628. {
  2629. __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT);
  2630. errorstate = SD_DATA_TIMEOUT;
  2631. return errorstate;
  2632. }
  2633. else if(__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
  2634. {
  2635. __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL);
  2636. errorstate = SD_DATA_CRC_FAIL;
  2637. return errorstate;
  2638. }
  2639. else if(__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
  2640. {
  2641. __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_RXOVERR);
  2642. errorstate = SD_RX_OVERRUN;
  2643. return errorstate;
  2644. }
  2645. else
  2646. {
  2647. /* No error flag set */
  2648. }
  2649. /* Clear all the static flags */
  2650. __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  2651. *(pSCR + 1) = ((tempscr[0] & SD_0TO7BITS) << 24) | ((tempscr[0] & SD_8TO15BITS) << 8) |\
  2652. ((tempscr[0] & SD_16TO23BITS) >> 8) | ((tempscr[0] & SD_24TO31BITS) >> 24);
  2653. *(pSCR) = ((tempscr[1] & SD_0TO7BITS) << 24) | ((tempscr[1] & SD_8TO15BITS) << 8) |\
  2654. ((tempscr[1] & SD_16TO23BITS) >> 8) | ((tempscr[1] & SD_24TO31BITS) >> 24);
  2655. return errorstate;
  2656. }
  2657. /**
  2658. * @brief Checks if the SD card is in programming state.
  2659. * @param hsd: SD handle
  2660. * @param pStatus: pointer to the variable that will contain the SD card state
  2661. * @retval SD Card error state
  2662. */
  2663. static HAL_SD_ErrorTypedef SD_IsCardProgramming(SD_HandleTypeDef *hsd, uint8_t *pStatus)
  2664. {
  2665. SDMMC_CmdInitTypeDef sdmmc_cmdinitstructure;
  2666. HAL_SD_ErrorTypedef errorstate = SD_OK;
  2667. __IO uint32_t responseR1 = 0;
  2668. sdmmc_cmdinitstructure.Argument = (uint32_t)(hsd->RCA << 16);
  2669. sdmmc_cmdinitstructure.CmdIndex = SD_CMD_SEND_STATUS;
  2670. sdmmc_cmdinitstructure.Response = SDMMC_RESPONSE_SHORT;
  2671. sdmmc_cmdinitstructure.WaitForInterrupt = SDMMC_WAIT_NO;
  2672. sdmmc_cmdinitstructure.CPSM = SDMMC_CPSM_ENABLE;
  2673. SDMMC_SendCommand(hsd->Instance, &sdmmc_cmdinitstructure);
  2674. while(!__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT))
  2675. {
  2676. }
  2677. if(__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CTIMEOUT))
  2678. {
  2679. errorstate = SD_CMD_RSP_TIMEOUT;
  2680. __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_CTIMEOUT);
  2681. return errorstate;
  2682. }
  2683. else if(__HAL_SD_SDMMC_GET_FLAG(hsd, SDMMC_FLAG_CCRCFAIL))
  2684. {
  2685. errorstate = SD_CMD_CRC_FAIL;
  2686. __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_FLAG_CCRCFAIL);
  2687. return errorstate;
  2688. }
  2689. else
  2690. {
  2691. /* No error flag set */
  2692. }
  2693. /* Check response received is of desired command */
  2694. if((uint32_t)SDMMC_GetCommandResponse(hsd->Instance) != SD_CMD_SEND_STATUS)
  2695. {
  2696. errorstate = SD_ILLEGAL_CMD;
  2697. return errorstate;
  2698. }
  2699. /* Clear all the static flags */
  2700. __HAL_SD_SDMMC_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  2701. /* We have received response, retrieve it for analysis */
  2702. responseR1 = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
  2703. /* Find out card status */
  2704. *pStatus = (uint8_t)((responseR1 >> 9) & 0x0000000F);
  2705. if((responseR1 & SD_OCR_ERRORBITS) == SD_ALLZERO)
  2706. {
  2707. return errorstate;
  2708. }
  2709. if((responseR1 & SD_OCR_ADDR_OUT_OF_RANGE) == SD_OCR_ADDR_OUT_OF_RANGE)
  2710. {
  2711. return(SD_ADDR_OUT_OF_RANGE);
  2712. }
  2713. if((responseR1 & SD_OCR_ADDR_MISALIGNED) == SD_OCR_ADDR_MISALIGNED)
  2714. {
  2715. return(SD_ADDR_MISALIGNED);
  2716. }
  2717. if((responseR1 & SD_OCR_BLOCK_LEN_ERR) == SD_OCR_BLOCK_LEN_ERR)
  2718. {
  2719. return(SD_BLOCK_LEN_ERR);
  2720. }
  2721. if((responseR1 & SD_OCR_ERASE_SEQ_ERR) == SD_OCR_ERASE_SEQ_ERR)
  2722. {
  2723. return(SD_ERASE_SEQ_ERR);
  2724. }
  2725. if((responseR1 & SD_OCR_BAD_ERASE_PARAM) == SD_OCR_BAD_ERASE_PARAM)
  2726. {
  2727. return(SD_BAD_ERASE_PARAM);
  2728. }
  2729. if((responseR1 & SD_OCR_WRITE_PROT_VIOLATION) == SD_OCR_WRITE_PROT_VIOLATION)
  2730. {
  2731. return(SD_WRITE_PROT_VIOLATION);
  2732. }
  2733. if((responseR1 & SD_OCR_LOCK_UNLOCK_FAILED) == SD_OCR_LOCK_UNLOCK_FAILED)
  2734. {
  2735. return(SD_LOCK_UNLOCK_FAILED);
  2736. }
  2737. if((responseR1 & SD_OCR_COM_CRC_FAILED) == SD_OCR_COM_CRC_FAILED)
  2738. {
  2739. return(SD_COM_CRC_FAILED);
  2740. }
  2741. if((responseR1 & SD_OCR_ILLEGAL_CMD) == SD_OCR_ILLEGAL_CMD)
  2742. {
  2743. return(SD_ILLEGAL_CMD);
  2744. }
  2745. if((responseR1 & SD_OCR_CARD_ECC_FAILED) == SD_OCR_CARD_ECC_FAILED)
  2746. {
  2747. return(SD_CARD_ECC_FAILED);
  2748. }
  2749. if((responseR1 & SD_OCR_CC_ERROR) == SD_OCR_CC_ERROR)
  2750. {
  2751. return(SD_CC_ERROR);
  2752. }
  2753. if((responseR1 & SD_OCR_GENERAL_UNKNOWN_ERROR) == SD_OCR_GENERAL_UNKNOWN_ERROR)
  2754. {
  2755. return(SD_GENERAL_UNKNOWN_ERROR);
  2756. }
  2757. if((responseR1 & SD_OCR_STREAM_READ_UNDERRUN) == SD_OCR_STREAM_READ_UNDERRUN)
  2758. {
  2759. return(SD_STREAM_READ_UNDERRUN);
  2760. }
  2761. if((responseR1 & SD_OCR_STREAM_WRITE_OVERRUN) == SD_OCR_STREAM_WRITE_OVERRUN)
  2762. {
  2763. return(SD_STREAM_WRITE_OVERRUN);
  2764. }
  2765. if((responseR1 & SD_OCR_CID_CSD_OVERWRITE) == SD_OCR_CID_CSD_OVERWRITE)
  2766. {
  2767. return(SD_CID_CSD_OVERWRITE);
  2768. }
  2769. if((responseR1 & SD_OCR_WP_ERASE_SKIP) == SD_OCR_WP_ERASE_SKIP)
  2770. {
  2771. return(SD_WP_ERASE_SKIP);
  2772. }
  2773. if((responseR1 & SD_OCR_CARD_ECC_DISABLED) == SD_OCR_CARD_ECC_DISABLED)
  2774. {
  2775. return(SD_CARD_ECC_DISABLED);
  2776. }
  2777. if((responseR1 & SD_OCR_ERASE_RESET) == SD_OCR_ERASE_RESET)
  2778. {
  2779. return(SD_ERASE_RESET);
  2780. }
  2781. if((responseR1 & SD_OCR_AKE_SEQ_ERROR) == SD_OCR_AKE_SEQ_ERROR)
  2782. {
  2783. return(SD_AKE_SEQ_ERROR);
  2784. }
  2785. return errorstate;
  2786. }
  2787. /**
  2788. * @}
  2789. */
  2790. #endif /* HAL_SD_MODULE_ENABLED */
  2791. /**
  2792. * @}
  2793. */
  2794. /**
  2795. * @}
  2796. */
  2797. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/