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